pub struct ListBox<D: Display + Default> {
pub handle: ControlHandle,
/* private fields */
}Expand description
A list box is a control window that contains a simple list of items from which the user can choose.
Requires the list-box feature.
Builder parameters:
parent: Required. The listbox parent container.size: The listbox size.position: The listbox position.enabled: If the listbox can be used by the user. It also has a grayed out look if disabled.focus: The control receive focus after being createdflags: A combination of the ListBoxFlags values.ex_flags: A combination of win32 window extended flags. Unlikeflags, ex_flags must be used straight from winapifont: The font used for the listbox textcollection: The default collections of the listboxselected_index: The default selected index in the listbox collectionmulti_selection: The collections of indices to set as selected in a multi selection listbox
Control events:
OnListBoxSelect: When the current listbox selection is changedOnListBoxDoubleClick: When a listbox item is clicked twice rapidlyMousePress(_): Generic mouse press events on the listboxOnMouseMove: Generic mouse mouse eventOnMouseWheel: Generic mouse wheel event
use native_windows_gui2 as nwg;
fn build_listbox(listb: &mut nwg::ListBox<&'static str>, window: &nwg::Window, font: &nwg::Font) {
nwg::ListBox::builder()
.flags(nwg::ListBoxFlags::VISIBLE | nwg::ListBoxFlags::MULTI_SELECT)
.collection(vec!["Hello", "World", "!!!!"])
.multi_selection(vec![0, 1, 2])
.font(Some(font))
.parent(window)
.build(listb);
}Fields§
§handle: ControlHandleImplementations§
Source§impl<D: Display + Default> ListBox<D>
impl<D: Display + Default> ListBox<D>
Sourcepub fn builder<'a>() -> ListBoxBuilder<'a, D>
pub fn builder<'a>() -> ListBoxBuilder<'a, D>
Examples found in repository?
141 fn build_ui(mut data: PartialDemo) -> Result<Rc<PartialDemoUi>, nwg::NwgError> {
142 use nwg::Event as E;
143
144 // Controls
145 nwg::Window::builder()
146 .size((500, 400))
147 .position((300, 300))
148 .title("Many UI")
149 .build(&mut data.window)?;
150
151 nwg::ListBox::builder()
152 .collection(vec!["People", "Animals", "Food"])
153 .focus(true)
154 .parent(&data.window)
155 .build(&mut data.menu)?;
156
157 nwg::Frame::builder()
158 .parent(&data.window)
159 .build(&mut data.frame1)?;
160
161 nwg::Frame::builder()
162 .flags(nwg::FrameFlags::BORDER)
163 .parent(&data.window)
164 .build(&mut data.frame2)?;
165
166 nwg::Frame::builder()
167 .flags(nwg::FrameFlags::BORDER)
168 .parent(&data.window)
169 .build(&mut data.frame3)?;
170
171 // Partials
172 PeopleUi::build_partial(&mut data.people_ui, Some(&data.frame1))?;
173 AnimalUi::build_partial(&mut data.animal_ui, Some(&data.frame2))?;
174 FoodUi::build_partial(&mut data.food_ui, Some(&data.frame3))?;
175
176 // Wrap-up
177 let ui = Rc::new(PartialDemoUi {
178 inner: data,
179 default_handler: Default::default(),
180 });
181
182 // Events
183 let mut window_handles = vec![&ui.window.handle];
184 window_handles.append(&mut ui.people_ui.handles());
185 window_handles.append(&mut ui.animal_ui.handles());
186 window_handles.append(&mut ui.food_ui.handles());
187
188 for handle in window_handles.iter() {
189 let evt_ui = ui.clone();
190 let handle_events = move |evt, evt_data, handle| {
191 evt_ui.people_ui.process_event(evt, &evt_data, handle);
192 evt_ui.animal_ui.process_event(evt, &evt_data, handle);
193 evt_ui.food_ui.process_event(evt, &evt_data, handle);
194
195 match evt {
196 E::OnListBoxSelect => {
197 if &handle == &evt_ui.menu {
198 PartialDemo::change_interface(&evt_ui.inner);
199 }
200 }
201 E::OnWindowClose => {
202 if &handle == &evt_ui.window {
203 PartialDemo::exit(&evt_ui.inner);
204 }
205 }
206 E::OnButtonClick => {
207 if &handle == &evt_ui.people_ui.save_btn
208 || &handle == &evt_ui.animal_ui.save_btn
209 || &handle == &evt_ui.food_ui.save_btn
210 {
211 PartialDemo::save(&evt_ui.inner);
212 }
213 }
214 _ => {}
215 }
216 };
217
218 ui.default_handler
219 .borrow_mut()
220 .push(nwg::full_bind_event_handler(handle, handle_events));
221 }
222
223 // Layout
224 use nwg::stretch::{geometry::Size, style::Dimension as D};
225
226 nwg::FlexboxLayout::builder()
227 .parent(&ui.window)
228 .child(&ui.menu)
229 .child_size(Size {
230 width: D::Percent(0.3),
231 height: D::Auto,
232 })
233 .child(&ui.frame1)
234 .child_size(Size {
235 width: D::Percent(1.0),
236 height: D::Auto,
237 })
238 .build(&ui.layout)?;
239
240 return Ok(ui);
241 }Sourcepub fn push(&self, item: D)
pub fn push(&self, item: D)
Add a new item to the listbox. Sort the collection if the listbox is sorted.
Sourcepub fn insert(&self, index: usize, item: D)
pub fn insert(&self, index: usize, item: D)
Insert an item in the collection and the control.
SPECIAL behaviour! If index is std::usize::MAX, the item is added at the end of the collection.
The method will still panic if index > len with every other values.
Sourcepub fn remove(&self, index: usize) -> D
pub fn remove(&self, index: usize) -> D
Remove the item at the selected index and returns it. Panic of the index is out of bounds
Sourcepub fn selection(&self) -> Option<usize>
pub fn selection(&self) -> Option<usize>
Return the index of the currencty selected item for single value list box.
Return None if no item is selected.
Examples found in repository?
26 fn change_interface(&self) {
27 self.frame1.set_visible(false);
28 self.frame2.set_visible(false);
29 self.frame3.set_visible(false);
30
31 let layout = &self.layout;
32 if layout.has_child(&self.frame1) {
33 layout.remove_child(&self.frame1);
34 }
35 if layout.has_child(&self.frame2) {
36 layout.remove_child(&self.frame2);
37 }
38 if layout.has_child(&self.frame3) {
39 layout.remove_child(&self.frame3);
40 }
41
42 use nwg::stretch::{
43 geometry::Size,
44 style::{Dimension as D, Style},
45 };
46 let mut style = Style::default();
47 style.size = Size {
48 width: D::Percent(1.0),
49 height: D::Auto,
50 };
51
52 match self.menu.selection() {
53 None | Some(0) => {
54 layout.add_child(&self.frame1, style).unwrap();
55 self.frame1.set_visible(true);
56 }
57 Some(1) => {
58 layout.add_child(&self.frame2, style).unwrap();
59 self.frame2.set_visible(true);
60 }
61 Some(2) => {
62 layout.add_child(&self.frame3, style).unwrap();
63 self.frame3.set_visible(true);
64 }
65 Some(_) => unreachable!(),
66 }
67 }Sourcepub fn multi_selection_len(&self) -> usize
pub fn multi_selection_len(&self) -> usize
Return the number of selected item in the list box Returns 0 for single select list box
Sourcepub fn multi_selection(&self) -> Vec<usize>
pub fn multi_selection(&self) -> Vec<usize>
Return a list index Returns an empty vector for single select list box.
Sourcepub fn selection_string(&self) -> Option<String>
pub fn selection_string(&self) -> Option<String>
Return the display value of the currenctly selected item for single value
Return None if no item is selected. This reads the visual value.
Sourcepub fn set_selection(&self, index: Option<usize>)
pub fn set_selection(&self, index: Option<usize>)
Set the currently selected item in the list box for single value list box. Does nothing if the index is out of bound If the value is None, remove the selected value
Sourcepub fn multi_add_selection(&self, index: usize)
pub fn multi_add_selection(&self, index: usize)
Select the item as index index in a multi item list box
Sourcepub fn multi_remove_selection(&self, index: usize)
pub fn multi_remove_selection(&self, index: usize)
Unselect the item as index index in a multi item list box
Sourcepub fn unselect_all(&self)
pub fn unselect_all(&self)
Unselect every item in the list box
Sourcepub fn select_all(&self)
pub fn select_all(&self)
Select every item in the list box
Sourcepub fn multi_select_range(&self, range: Range<usize>)
pub fn multi_select_range(&self, range: Range<usize>)
Select a range of items in a multi list box
Sourcepub fn multi_unselect_range(&self, range: Range<usize>)
pub fn multi_unselect_range(&self, range: Range<usize>)
Unselect a range of items in a multi list box
Sourcepub fn set_selection_string(&self, value: &str) -> Option<usize>
pub fn set_selection_string(&self, value: &str) -> Option<usize>
Search an item that begins by the value and select the first one found. The search is not case sensitive, so this string can contain any combination of uppercase and lowercase letters. Return the index of the selected string or None if the search was not successful
Sourcepub fn selected(&self, index: usize) -> bool
pub fn selected(&self, index: usize) -> bool
Check if the item at index is selected by the user
Return false if the index is out of range.
Sourcepub fn sync(&self)
pub fn sync(&self)
Update the visual of the control with the inner collection. This rebuild every item in the list box and can take some time on big collections.
Sourcepub fn set_collection(&self, col: Vec<D>) -> Vec<D>
pub fn set_collection(&self, col: Vec<D>) -> Vec<D>
Set the item collection of the list box. Return the old collection
Sourcepub fn clear(&self)
pub fn clear(&self)
Clears the control and free the underlying collection. Same as set_collection(Vec::new())
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Return the number of items in the control. NOT the inner rust collection
Sourcepub fn enabled(&self) -> bool
pub fn enabled(&self) -> bool
Return true if the control user can interact with the control, return false otherwise
Sourcepub fn set_enabled(&self, v: bool)
pub fn set_enabled(&self, v: bool)
Enable or disable the control
Sourcepub fn visible(&self) -> bool
pub fn visible(&self) -> bool
Return true if the control is visible to the user. Will return true even if the control is outside of the parent client view (ex: at the position (10000, 10000))
Sourcepub fn set_visible(&self, v: bool)
pub fn set_visible(&self, v: bool)
Show or hide the control to the user
Sourcepub fn set_position(&self, x: i32, y: i32)
pub fn set_position(&self, x: i32, y: i32)
Set the position of the button in the parent window
Sourcepub fn collection(&self) -> Ref<'_, Vec<D>>
pub fn collection(&self) -> Ref<'_, Vec<D>>
Get read-only access to the inner collection of the list box This call refcell.borrow under the hood. Be sure to drop the value before calling other list box methods
Sourcepub fn collection_mut(&self) -> RefMut<'_, Vec<D>>
pub fn collection_mut(&self) -> RefMut<'_, Vec<D>>
Get mutable access to the inner collection of the list box. Does not update the visual
control. Call sync to update the view. This call refcell.borrow_mut under the hood.
Be sure to drop the value before calling other list box methods
Sourcepub fn class_name(&self) -> &'static str
pub fn class_name(&self) -> &'static str
Winapi class name used during control creation
Sourcepub fn forced_flags(&self) -> u32
pub fn forced_flags(&self) -> u32
Winapi flags required by the control
Trait Implementations§
Source§impl<D: Display + Default> From<&ListBox<D>> for ControlHandle
Available on crate feature listbox only.
impl<D: Display + Default> From<&ListBox<D>> for ControlHandle
listbox only.Source§impl<D: Display + Default> PartialEq<ControlHandle> for ListBox<D>
Available on crate feature listbox only.
impl<D: Display + Default> PartialEq<ControlHandle> for ListBox<D>
listbox only.Source§impl<D: Display + Default> PartialEq<ListBox<D>> for ControlHandle
Available on crate feature listbox only.
impl<D: Display + Default> PartialEq<ListBox<D>> for ControlHandle
listbox only.