rustty_oxide/
stdbutton.rs

1use rustty::{Attr, CellAccessor, Size, HasSize};
2
3use core::{
4    Alignable, 
5    HorizontalAlign, 
6    VerticalAlign,
7    Widget,
8    Painter,
9    Frame,
10    Button,
11    ButtonResult,
12    find_accel_char_index
13};
14
15/// Standard button that returns some result based on
16/// whether a key is pressed
17/// 
18/// # Examples
19///
20/// ```
21/// use oxide::core::{HorizontalAlign, VerticalAlign, ButtonResult, Widget};
22/// use oxide::{Dialog, StdButton};
23///
24/// let mut dlg = Dialog::new(60, 10);
25///
26/// let mut b1 = StdButton::new("Foo", 'f', ButtonResult::Ok);
27/// let mut b2 = StdButton::new("Bar" ,'b', ButtonResult::Cancel);
28///
29/// b1.pack(&dlg, HorizontalAlign::Left, VerticalAlign::Middle, (1,1));
30/// b2.pack(&dlg, HorizontalAlign::Middle, VerticalAlign::Middle, (1,1));
31///
32/// dlg.add_button(b1);
33/// dlg.add_button(b2);
34/// ```
35///
36pub struct StdButton {
37    frame: Frame,
38    accel: char,
39    result: ButtonResult,
40    text: String
41}
42
43impl StdButton {    
44    /// Constructs a new `StdButton`, asking for the text to be displayed 
45    /// by the button, the key to map to, and the result returned when the
46    /// key is detected
47    ///
48    /// # Examples
49    ///
50    /// ```
51    /// use oxide::core::ButtonResult;
52    /// use oxide::StdButton;
53    ///
54    /// let mut b1 = StdButton::new("Foo", 'f', ButtonResult::Ok);
55    /// ```
56    ///
57    pub fn new(text: &str, accel: char, result: ButtonResult) -> StdButton {
58        let s = format!("< {} >", text);
59        let width = s.chars().count();
60        let mut button = 
61            StdButton { frame: Frame::new(width, 1), 
62                        accel: accel.to_lowercase().next().unwrap_or(accel),
63                        result: result,
64                        text: s };
65        button.frame.printline(0, 0, &button.text[..]);
66        match find_accel_char_index(text, button.accel) {
67            Some(i) => {
68                button.frame.get_mut(i+2, 0).unwrap().set_attrs(Attr::Bold);
69            },
70            None    => (),
71        }
72        button
73    }
74}
75
76impl Button for StdButton {
77    fn accel(&self) -> char {
78        self.accel
79    }
80
81    fn result(&self) -> ButtonResult {
82        self.result
83    }
84}
85
86impl Widget for StdButton {
87    fn draw(&mut self, parent: &mut CellAccessor) {
88        self.frame.draw_into(parent);
89    }
90
91    fn pack(&mut self, parent: &HasSize, halign: HorizontalAlign, valign: VerticalAlign,
92                margin: (usize,usize)) {
93        self.frame.align(parent, halign, valign, margin);
94    }
95
96    fn draw_box(&mut self) {
97        self.frame.draw_box();
98    }
99
100    fn resize(&mut self, new_size: Size) {
101        self.frame.resize(new_size);
102    }
103
104    fn frame(&self) -> &Frame {
105        &self.frame
106    }
107
108    fn frame_mut(&mut self) -> &mut Frame {
109        &mut self.frame
110    }
111}
112