1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use std::ffi::CString;

use crate::bind;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum ButtonKind {
    Normal,
    Confirm,
    Cancel,
}

impl ButtonKind {
    #[allow(clippy::unnecessary_cast)]
    fn as_flags(self) -> u32 {
        (match self {
            ButtonKind::Normal => 0,
            ButtonKind::Confirm => bind::SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT,
            ButtonKind::Cancel => bind::SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT,
        }) as u32
    }
}

/// An id type for a button.
pub type ButtonId = i32;

/// A button in a [`super::MessageBox`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Button {
    kind: ButtonKind,
    id: ButtonId,
    text: CString,
}

impl Button {
    /// Constructs a normal button.
    ///
    /// # Panics
    ///
    /// Panics if `text` contains a null character.
    #[must_use]
    pub fn normal(id: ButtonId, text: &str) -> Self {
        Self {
            kind: ButtonKind::Normal,
            id,
            text: CString::new(text).unwrap(),
        }
    }
    /// Constructs a confirm button.
    ///
    /// # Panics
    ///
    /// Panics if `text` contains a null character.
    #[must_use]
    pub fn confirm(id: ButtonId, text: &str) -> Self {
        Self {
            kind: ButtonKind::Confirm,
            id,
            text: CString::new(text).unwrap(),
        }
    }
    /// Constructs a cancel button.
    ///
    /// # Panics
    ///
    /// Panics if `text` contains a null character.
    #[must_use]
    pub fn cancel(id: ButtonId, text: &str) -> Self {
        Self {
            kind: ButtonKind::Cancel,
            id,
            text: CString::new(text).unwrap(),
        }
    }

    pub(super) fn as_raw(&self) -> bind::SDL_MessageBoxButtonData {
        bind::SDL_MessageBoxButtonData {
            flags: self.kind.as_flags(),
            buttonid: self.id,
            text: self.text.as_ptr(),
        }
    }
}