gabdube / native-windows-gui

A light windows GUI toolkit for rust
https://gabdube.github.io/native-windows-gui/
MIT License
1.96k stars 127 forks source link

Listview second insert_column for ListViewStyle::Detailed style lists freezes app #242

Open asarang74 opened 2 years ago

asarang74 commented 2 years ago

If I call nwg::ListView::insert_column for a ListViewStyle::Detailed list view twice, Rust app freezes at the 2nd call. (Environment: Windows 10 PC, GNU compiler under MSYS2, Rust installed from MSYS2) Eg:

    let dv = &self.data_view;

    dv.set_headers_enabled(true);

    dv.insert_column(nwg::InsertListViewColumn{
        index: Some(0),
        fmt: None,
        width: Some(200),
        text: Some("Currency".to_string())
    });

    dv.insert_column(nwg::InsertListViewColumn{
        index: Some(1),
        fmt: None,
        width: Some(50),
        text: Some("Exchange rate".to_string())
    });

If I experiment and change listview.rs insert_column and use let col_count = 0 instead of let col_count = self.column_len() as i32; and my app specifies index 0 and 1, than no issue.

It seems listview.rs column_len() can enter an infinite loop, as Windows LVM_GETCOLUMNWIDTH call might return anything for LVS_REPORT style lists if a column does not exist.

pub fn column_len(&self) -> usize {
    use winapi::um::commctrl::LVM_GETCOLUMNWIDTH;

    let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);

    let mut count = 0;
    while wh::send_message(handle, LVM_GETCOLUMNWIDTH, count, 0) != 0 {
        count += 1;
    }

    count
}

MS documentation for LVM_GETCOLUMNWIDTH ( https://docs.microsoft.com/en-us/windows/win32/controls/lvm-getcolumnwidth documentation ) states "Returns the column width if successful, or zero otherwise. If this message is sent to a list-view control with the LVS_REPORT style and the specified column does not exist, the return value is undefined."

Could not you rely somehow on LVM_GETCOLUMN instead of LVM_GETCOLUMNWIDTH to avoid infinite loops?

Airtz commented 1 year ago

ListViewStyle::Detailed is unusable at the moment because of this.

@gabdube Any chance of getting a fix?

Airtz commented 1 year ago

Follow up: it actually works fine with COMCTL v6.