Struct ListBox

Source
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 created
  • flags: A combination of the ListBoxFlags values.
  • ex_flags: A combination of win32 window extended flags. Unlike flags, ex_flags must be used straight from winapi
  • font: The font used for the listbox text
  • collection: The default collections of the listbox
  • selected_index: The default selected index in the listbox collection
  • multi_selection: The collections of indices to set as selected in a multi selection listbox

Control events:

  • OnListBoxSelect: When the current listbox selection is changed
  • OnListBoxDoubleClick: When a listbox item is clicked twice rapidly
  • MousePress(_): Generic mouse press events on the listbox
  • OnMouseMove: Generic mouse mouse event
  • OnMouseWheel: Generic mouse wheel event
use native_windows_gui 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: ControlHandle

Implementations§

Source§

impl<D: Display + Default> ListBox<D>

Source

pub fn builder<'a>() -> ListBoxBuilder<'a, D>

Examples found in repository?
examples/partials.rs (line 142)
132        fn build_ui(mut data: PartialDemo) -> Result<Rc<PartialDemoUi>, nwg::NwgError> {
133            use nwg::Event as E;
134            
135            // Controls
136            nwg::Window::builder()
137                .size((500, 400))
138                .position((300, 300))
139                .title("Many UI")
140                .build(&mut data.window)?;
141
142            nwg::ListBox::builder()
143                .collection(vec!["People", "Animals", "Food"])
144                .focus(true)
145                .parent(&data.window)
146                .build(&mut data.menu)?;
147
148            nwg::Frame::builder()
149                .parent(&data.window)
150                .build(&mut data.frame1)?;
151
152            nwg::Frame::builder()
153                .flags(nwg::FrameFlags::BORDER)
154                .parent(&data.window)
155                .build(&mut data.frame2)?;
156
157            nwg::Frame::builder()
158                .flags(nwg::FrameFlags::BORDER)
159                .parent(&data.window)
160                .build(&mut data.frame3)?;
161
162            // Partials
163            PeopleUi::build_partial(&mut data.people_ui, Some(&data.frame1))?;
164            AnimalUi::build_partial(&mut data.animal_ui, Some(&data.frame2))?;
165            FoodUi::build_partial(&mut data.food_ui, Some(&data.frame3))?;
166
167            // Wrap-up
168            let ui = Rc::new(PartialDemoUi {
169                inner: data,
170                default_handler: Default::default()
171            });
172
173            // Events
174            let mut window_handles = vec![&ui.window.handle];
175            window_handles.append(&mut ui.people_ui.handles());
176            window_handles.append(&mut ui.animal_ui.handles());
177            window_handles.append(&mut ui.food_ui.handles());
178
179            for handle in window_handles.iter() {
180                let evt_ui = ui.clone();
181                let handle_events = move |evt, evt_data, handle| {
182                    evt_ui.people_ui.process_event(evt, &evt_data, handle);
183                    evt_ui.animal_ui.process_event(evt, &evt_data, handle);
184                    evt_ui.food_ui.process_event(evt, &evt_data, handle);
185                    
186                    match evt {
187                        E::OnListBoxSelect => 
188                            if &handle == &evt_ui.menu {
189                                PartialDemo::change_interface(&evt_ui.inner);
190                            },
191                        E::OnWindowClose => 
192                            if &handle == &evt_ui.window {
193                                PartialDemo::exit(&evt_ui.inner);
194                            },
195                        E::OnButtonClick => 
196                            if &handle == &evt_ui.people_ui.save_btn || &handle == &evt_ui.animal_ui.save_btn ||&handle == &evt_ui.food_ui.save_btn  {
197                                PartialDemo::save(&evt_ui.inner);
198                            },
199                        _ => {}
200                    }
201                };
202
203                ui.default_handler.borrow_mut().push(
204                    nwg::full_bind_event_handler(handle, handle_events)
205                );
206            }
207
208            // Layout
209            use nwg::stretch::{geometry::Size, style::Dimension as D};
210
211           nwg::FlexboxLayout::builder()
212                .parent(&ui.window)
213                .child(&ui.menu)
214                    .child_size(Size { width: D::Percent(0.3), height: D::Auto })
215                .child(&ui.frame1)
216                    .child_size(Size { width: D::Percent(1.0), height: D::Auto })
217                .build(&ui.layout)?;
218            
219            return Ok(ui);
220        }
Source

pub fn push(&self, item: D)

Add a new item to the listbox. Sort the collection if the listbox is sorted.

Source

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.

Source

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

Source

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?
examples/partials.rs (line 42)
28    fn change_interface(&self) {
29        self.frame1.set_visible(false);
30        self.frame2.set_visible(false);
31        self.frame3.set_visible(false);
32
33        let layout = &self.layout;
34        if layout.has_child(&self.frame1) { layout.remove_child(&self.frame1); }
35        if layout.has_child(&self.frame2) { layout.remove_child(&self.frame2); }
36        if layout.has_child(&self.frame3) { layout.remove_child(&self.frame3); }
37
38        use nwg::stretch::{geometry::Size, style::{Style, Dimension as D}};
39        let mut style = Style::default();
40        style.size = Size { width: D::Percent(1.0), height: D::Auto };
41
42        match self.menu.selection() {
43            None | Some(0) => {
44                layout.add_child(&self.frame1, style).unwrap();
45                self.frame1.set_visible(true);
46            },
47            Some(1) => {
48                layout.add_child(&self.frame2, style).unwrap();
49                self.frame2.set_visible(true);
50            },
51            Some(2) => {
52                layout.add_child(&self.frame3, style).unwrap();
53                self.frame3.set_visible(true);
54            },
55            Some(_) => unreachable!()
56        }
57    }
Source

pub fn multi_selection_len(&self) -> usize

Return the number of selected item in the list box Returns 0 for single select list box

Source

pub fn multi_selection(&self) -> Vec<usize>

Return a list index Returns an empty vector for single select list box.

Source

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.

Source

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

Source

pub fn multi_add_selection(&self, index: usize)

Select the item as index index in a multi item list box

Source

pub fn multi_remove_selection(&self, index: usize)

Unselect the item as index index in a multi item list box

Source

pub fn unselect_all(&self)

Unselect every item in the list box

Source

pub fn select_all(&self)

Select every item in the list box

Source

pub fn multi_select_range(&self, range: Range<usize>)

Select a range of items in a multi list box

Source

pub fn multi_unselect_range(&self, range: Range<usize>)

Unselect a range of items in a multi list box

Source

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

Source

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.

Source

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.

Source

pub fn set_collection(&self, col: Vec<D>) -> Vec<D>

Set the item collection of the list box. Return the old collection

Source

pub fn clear(&self)

Clears the control and free the underlying collection. Same as set_collection(Vec::new())

Source

pub fn len(&self) -> usize

Return the number of items in the control. NOT the inner rust collection

Source

pub fn font(&self) -> Option<Font>

Return the font of the control

Source

pub fn set_font(&self, font: Option<&Font>)

Set the font of the control

Source

pub fn focus(&self) -> bool

Return true if the control currently has the keyboard focus

Source

pub fn set_focus(&self)

Set the keyboard focus on the button.

Source

pub fn enabled(&self) -> bool

Return true if the control user can interact with the control, return false otherwise

Source

pub fn set_enabled(&self, v: bool)

Enable or disable the control

Source

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))

Source

pub fn set_visible(&self, v: bool)

Show or hide the control to the user

Source

pub fn size(&self) -> (u32, u32)

Return the size of the button in the parent window

Source

pub fn set_size(&self, x: u32, y: u32)

Set the size of the button in the parent window

Source

pub fn position(&self) -> (i32, i32)

Return the position of the button in the parent window

Source

pub fn set_position(&self, x: i32, y: i32)

Set the position of the button in the parent window

Source

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

Source

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

Source

pub fn class_name(&self) -> &'static str

Winapi class name used during control creation

Source

pub fn flags(&self) -> u32

Winapi base flags used during window creation

Source

pub fn forced_flags(&self) -> u32

Winapi flags required by the control

Trait Implementations§

Source§

impl<D: Default + Display + Default> Default for ListBox<D>

Source§

fn default() -> ListBox<D>

Returns the “default value” for a type. Read more
Source§

impl<D: Display + Default> Drop for ListBox<D>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<D: Display + Default> From<&ListBox<D>> for ControlHandle

Source§

fn from(control: &ListBox<D>) -> Self

Converts to this type from the input type.
Source§

impl<D: Display + Default> PartialEq<ControlHandle> for ListBox<D>

Source§

fn eq(&self, other: &ControlHandle) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<D: Display + Default> PartialEq<ListBox<D>> for ControlHandle

Source§

fn eq(&self, other: &ListBox<D>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<D: Display + Default> PartialEq for ListBox<D>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

§

impl<D> !Freeze for ListBox<D>

§

impl<D> !RefUnwindSafe for ListBox<D>

§

impl<D> !Send for ListBox<D>

§

impl<D> !Sync for ListBox<D>

§

impl<D> Unpin for ListBox<D>
where D: Unpin,

§

impl<D> UnwindSafe for ListBox<D>
where D: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.