diff --git a/native-windows-gui/examples/image_decoder_d.rs b/native-windows-gui/examples/image_decoder_d.rs index 8cc9b29e..f9b693e8 100644 --- a/native-windows-gui/examples/image_decoder_d.rs +++ b/native-windows-gui/examples/image_decoder_d.rs @@ -29,7 +29,7 @@ pub struct ImageDecoderApp { #[nwg_resource] decoder: nwg::ImageDecoder, - #[nwg_resource(title: "Open File", action: nwg::FileDialogAction::Open, filters: "Png(*.png)|Jpeg(*.jpg;*.jpeg)|DDS(*.dds)|TIFF(*.tiff)|BMP(*.bmp)|Any (*.*)")] + #[nwg_resource(title: "Open File", action: nwg::FileDialogAction::Open, filters: "Png(*.png)|Jpeg(*.jpg;*.jpeg)|DDS(*.dds)|TIFF(*.tiff)|BMP(*.bmp)|>Any (*.*)")] dialog: nwg::FileDialog, #[nwg_control(text: "Open", focus: true)] diff --git a/native-windows-gui/src/resources/file_dialog.rs b/native-windows-gui/src/resources/file_dialog.rs index 95ccb74d..8ee566eb 100644 --- a/native-windows-gui/src/resources/file_dialog.rs +++ b/native-windows-gui/src/resources/file_dialog.rs @@ -29,7 +29,8 @@ pub enum FileDialogAction { * multiselect: Whether the user can select more than one file. Only supported with the Open action * default_folder: Default folder to show in the dialog. * filters: If defined, filter the files that the user can select (In a Open dialog) or which extension to add to the saved file (in a Save dialog) - The `filters` value must be a '|' separated string having this format: "Test(*.txt;*.rs)|Any(*.*)" + The `filters` value must be a '|' separated string having this format: "Test(*.txt;*.rs)|Any(*.*)". + If one of the filters has its name prefixed with '>' (like ">Test"), it will become the default filter. ```rust use native_windows_gui as nwg; @@ -168,7 +169,8 @@ impl FileDialog { This can only be set ONCE (the initialization counts) and won't work if the dialog is `OpenDirectory`. The `filters` value must be a '|' separated string having this format: "Test(*.txt;*.rs)|Any(*.*)" - Where the fist part is the "human name" and the second part is a filter for the system. + Where the first part is the "human name" and the second part is a filter for the system. If the + human name starts with a '>', the '>' is removed and the filter associated with it becomes the default. */ pub fn set_filters<'a>(&self, filters: &'a str) -> Result<(), NwgError> { unsafe{ diff --git a/native-windows-gui/src/win32/resources_helper.rs b/native-windows-gui/src/win32/resources_helper.rs index 027ef802..a63d99cb 100644 --- a/native-windows-gui/src/win32/resources_helper.rs +++ b/native-windows-gui/src/win32/resources_helper.rs @@ -391,7 +391,7 @@ pub unsafe fn create_file_dialog<'a, 'b>( match &filters { &Some(ref f) => match file_dialog_set_filters(file_dialog, f) { Ok(_) => (), - Err(e) => { println!("set filters"); file_dialog.Release(); return Err(e); } + Err(e) => { file_dialog.Release(); return Err(e); } }, &None => () } @@ -456,8 +456,16 @@ pub unsafe fn file_dialog_set_filters<'a>(dialog: &mut IFileDialog, filters: &'a let mut raw_filters: Vec = Vec::with_capacity(3); let mut keep_alive: Vec<(Vec, Vec)> = Vec::with_capacity(3); + let mut default_filter = None; for f in filters.split('|') { + let f = if f.starts_with('>') { + default_filter = Some(raw_filters.len()); + f.split_at(1).1 + } else { + f + }; + let end = f.rfind('('); if end.is_none() { let err = format!("Bad extension filter format: {:?}", filters); @@ -473,7 +481,16 @@ pub unsafe fn file_dialog_set_filters<'a>(dialog: &mut IFileDialog, filters: &'a let filters_count = raw_filters.len() as UINT; if dialog.SetFileTypes(filters_count, raw_filters.as_ptr()) == S_OK { - Ok(()) + if let Some(i) = default_filter { + if dialog.SetFileTypeIndex((i + 1) as u32) == S_OK { + Ok(()) + } else { + let err = format!("Failed to set the default filter using {:?}", filters); + return Err(NwgError::file_dialog(&err)) + } + } else { + Ok(()) + } } else { let err = format!("Failed to set the filters using {:?}", filters); return Err(NwgError::file_dialog(&err));