newt/asm/grid/button_bar.rs
1//
2// Copyright (C) 2019 Robert Gill <rtgill82@gmail.com>
3//
4// This file is a part of newt-rs.
5//
6// This library is free software; you can redistribute it and/or
7// modify it under the terms of the GNU Lesser General Public
8// License version 2.1 as published by the Free Software Foundation.
9//
10// This library is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13// Lesser General Public License for more details.
14//
15// You should have received a copy of the GNU Lesser General Public
16// License along with this library; if not, write to the Free Software
17// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18//
19
20use std::cell::Cell;
21use newt_sys::*;
22use crate::component::Component;
23use crate::grid::Parent;
24use crate::widgets::Button;
25
26use crate::asm;
27
28///
29/// Creates a row of buttons.
30///
31#[derive(Grid)]
32pub struct ButtonBar {
33 co: Cell<newtGrid>,
34 added_to_parent: Cell<bool>,
35 children: Vec<Button>
36}
37
38impl ButtonBar {
39 ///
40 /// Create a new grid containing a row of buttons. The buttons will
41 /// be labeled with the strings provided in `buttons`.
42 ///
43 /// * `buttons` - A list of strings to use as button labels.
44 ///
45 pub fn new(buttons: &[&str]) -> ButtonBar {
46 let grid: newtGrid;
47
48 let len = buttons.len();
49 let mut buttons_buf: Vec<newtComponent> = Vec::with_capacity(len);
50
51 unsafe {
52 let buttons_ptr: *mut newtComponent = buttons_buf.as_mut_ptr();
53 grid = asm::button_bar_new(buttons, buttons_ptr);
54 buttons_buf.set_len(len);
55 }
56
57 let mut buttons = Vec::with_capacity(len);
58 for co in buttons_buf {
59 buttons.push(Button::new_co(co));
60 }
61
62 ButtonBar {
63 co: Cell::new(grid),
64 added_to_parent: Cell::new(false),
65 children: buttons
66 }
67 }
68
69 ///
70 /// `Returns` the array of buttons contained by the grid.
71 ///
72 pub fn buttons(&self) -> &[Button] {
73 self.children.as_slice()
74 }
75}
76
77impl Parent for ButtonBar {
78 fn children(&self) -> Vec<&dyn Component> {
79 let len = self.children.len();
80 let mut vec: Vec<&dyn Component> = Vec::with_capacity(len);
81 for child in self.children.iter() {
82 vec.push(child);
83 }
84 vec
85 }
86}