Skip to main content

pacenotes_installer\ui/
list_box.rs

1const LB_ADDSTRING: u32 = 0x0180;
2const LB_RESETCONTENT: u32 = 0x0184;
3const LB_SETCURSEL: u32 = 0x0186;
4const LB_GETCURSEL: u32 = 0x0188;
5/// `LB_SETCURSEL`'s index for "nothing selected".
6const NO_SELECTION: isize = -1;
7
8/// The notification a list box sends its parent when the user changes the
9/// selection; windows-sys does not export it.
10pub const LBN_SELCHANGE: u16 = 1;
11
12pub unsafe fn list_reset(dialog: windows_sys::Win32::Foundation::HWND, control: u16) {
13    windows_sys::Win32::UI::WindowsAndMessaging::SendDlgItemMessageW(
14        dialog,
15        control as i32,
16        LB_RESETCONTENT,
17        0,
18        0,
19    );
20}
21
22pub unsafe fn list_add(dialog: windows_sys::Win32::Foundation::HWND, control: u16, text: &str) {
23    let wide = crate::i18n::to_wide(text);
24    windows_sys::Win32::UI::WindowsAndMessaging::SendDlgItemMessageW(
25        dialog,
26        control as i32,
27        LB_ADDSTRING,
28        0,
29        wide.as_ptr() as windows_sys::Win32::Foundation::LPARAM,
30    );
31}
32
33/// Selects an item, or clears the selection for `None`.
34pub unsafe fn list_select(
35    dialog: windows_sys::Win32::Foundation::HWND,
36    control: u16,
37    index: Option<usize>,
38) {
39    let item = index.map_or(NO_SELECTION, |index| index as isize);
40    windows_sys::Win32::UI::WindowsAndMessaging::SendDlgItemMessageW(
41        dialog,
42        control as i32,
43        LB_SETCURSEL,
44        item as windows_sys::Win32::Foundation::WPARAM,
45        0,
46    );
47}
48
49/// `None` when nothing is selected.
50pub unsafe fn list_selection(
51    dialog: windows_sys::Win32::Foundation::HWND,
52    control: u16,
53) -> Option<usize> {
54    let selection = windows_sys::Win32::UI::WindowsAndMessaging::SendDlgItemMessageW(
55        dialog,
56        control as i32,
57        LB_GETCURSEL,
58        0,
59        0,
60    );
61    usize::try_from(selection).ok()
62}