@@ -3,6 +3,7 @@ use winapi::ctypes::c_int;
3
3
use winapi:: um:: winnt:: HANDLE ;
4
4
5
5
use crate :: resources:: OemImage ;
6
+ use crate :: win32:: high_dpi;
6
7
use super :: base_helper:: { get_system_error, to_utf16} ;
7
8
8
9
#[ allow( unused_imports) ] use std:: { ptr, mem} ;
@@ -25,23 +26,23 @@ pub fn is_bitmap(handle: HBITMAP) -> bool {
25
26
26
27
pub fn destroy_icon ( icon : HANDLE ) {
27
28
unsafe { winapi:: um:: winuser:: DestroyIcon ( icon as _ ) ; }
28
- }
29
+ }
29
30
30
31
pub fn destroy_cursor ( cursor : HANDLE ) {
31
32
unsafe { winapi:: um:: winuser:: DestroyCursor ( cursor as _ ) ; }
32
- }
33
+ }
33
34
34
35
pub fn destroy_obj ( obj : HANDLE ) {
35
36
unsafe { winapi:: um:: wingdi:: DeleteObject ( obj as _ ) ; }
36
- }
37
+ }
37
38
38
39
pub unsafe fn build_font (
39
40
size : i32 ,
40
41
weight : u32 ,
41
42
style : [ bool ; 3 ] ,
42
43
family_name : Option < & str > ,
43
- ) -> Result < HFONT , NwgError >
44
- {
44
+ ) -> Result < HFONT , NwgError >
45
+ {
45
46
use winapi:: um:: wingdi:: { DEFAULT_CHARSET , OUT_DEFAULT_PRECIS , CLIP_DEFAULT_PRECIS , CLEARTYPE_QUALITY , VARIABLE_PITCH } ;
46
47
use winapi:: um:: wingdi:: CreateFontW ;
47
48
let [ use_italic, use_underline, use_strikeout] = style;
@@ -56,7 +57,7 @@ pub unsafe fn build_font(
56
57
family_name_ptr = ptr:: null ( ) ;
57
58
}
58
59
59
- let ( size, _) = super :: high_dpi:: logical_to_physical ( size as i32 , 0 ) ;
60
+ let ( size, _) = high_dpi:: logical_to_physical ( size as i32 , 0 ) ;
60
61
61
62
let handle = CreateFontW (
62
63
size as c_int , // nHeight
@@ -95,8 +96,9 @@ pub unsafe fn build_image<'a>(
95
96
96
97
let filepath = to_utf16 ( source) ;
97
98
let ( width, height) = size. unwrap_or ( ( 0 , 0 ) ) ;
99
+ let ( width, height) = high_dpi:: logical_to_physical ( width as i32 , height as i32 ) ;
98
100
99
- let mut handle = LoadImageW ( ptr:: null_mut ( ) , filepath. as_ptr ( ) , image_type, width as i32 , height as i32 , LR_LOADFROMFILE ) ;
101
+ let mut handle = LoadImageW ( ptr:: null_mut ( ) , filepath. as_ptr ( ) , image_type, width, height, LR_LOADFROMFILE ) ;
100
102
if handle. is_null ( ) {
101
103
let ( code, _) = get_system_error ( ) ;
102
104
if code == 2 && !strict {
@@ -116,7 +118,7 @@ pub unsafe fn build_image<'a>(
116
118
} ,
117
119
_ => { unreachable ! ( ) }
118
120
} ;
119
-
121
+
120
122
}
121
123
}
122
124
@@ -138,15 +140,16 @@ pub unsafe fn build_image_decoder<'a>(
138
140
use crate :: ImageDecoder ;
139
141
140
142
let decoder = ImageDecoder :: new ( ) ?;
141
-
143
+
142
144
let mut image_frame = decoder
143
145
. from_filename ( source) ?
144
146
. frame ( 0 ) ?;
145
147
146
148
if let Some ( ( width, height) ) = size {
147
- image_frame = decoder. resize_image ( & image_frame, [ width, height] ) ?;
149
+ let ( width, height) = high_dpi:: logical_to_physical ( width as i32 , height as i32 ) ;
150
+ image_frame = decoder. resize_image ( & image_frame, [ width as u32 , height as u32 ] ) ?;
148
151
}
149
-
152
+
150
153
let mut bitmap = image_frame. as_bitmap ( ) ?;
151
154
152
155
bitmap. owned = false ;
@@ -163,15 +166,16 @@ pub unsafe fn build_image_decoder_from_memory<'a>(
163
166
use crate :: ImageDecoder ;
164
167
165
168
let decoder = ImageDecoder :: new ( ) ?;
166
-
169
+
167
170
let mut image_frame = decoder
168
171
. from_stream ( src) ?
169
172
. frame ( 0 ) ?;
170
173
171
174
if let Some ( ( width, height) ) = size {
172
- image_frame = decoder. resize_image ( & image_frame, [ width, height] ) ?;
175
+ let ( width, height) = high_dpi:: logical_to_physical ( width as i32 , height as i32 ) ;
176
+ image_frame = decoder. resize_image ( & image_frame, [ width as u32 , height as u32 ] ) ?;
173
177
}
174
-
178
+
175
179
let mut bitmap = image_frame. as_bitmap ( ) ?;
176
180
177
181
bitmap. owned = false ;
@@ -182,13 +186,14 @@ pub unsafe fn build_image_decoder_from_memory<'a>(
182
186
pub unsafe fn build_oem_image (
183
187
source : OemImage ,
184
188
size : Option < ( u32 , u32 ) > ,
185
- ) -> Result < HANDLE , NwgError >
189
+ ) -> Result < HANDLE , NwgError >
186
190
{
187
191
use winapi:: um:: winuser:: { LR_DEFAULTSIZE , LR_SHARED , IMAGE_ICON , IMAGE_CURSOR , IMAGE_BITMAP } ;
188
192
use winapi:: um:: winuser:: LoadImageW ;
189
193
use winapi:: shared:: ntdef:: LPCWSTR ;
190
194
191
195
let ( width, height) = size. unwrap_or ( ( 0 , 0 ) ) ;
196
+ let ( width, height) = high_dpi:: logical_to_physical ( width as i32 , height as i32 ) ;
192
197
193
198
let ( c_res_type, res_identifier) = match source {
194
199
OemImage :: Bitmap ( b) => {
@@ -208,7 +213,7 @@ pub unsafe fn build_oem_image(
208
213
LR_SHARED
209
214
} ;
210
215
211
- let handle = LoadImageW ( ptr:: null_mut ( ) , res_identifier, c_res_type, width as i32 , height as i32 , flags) ;
216
+ let handle = LoadImageW ( ptr:: null_mut ( ) , res_identifier, c_res_type, width, height, flags) ;
212
217
213
218
if handle. is_null ( ) {
214
219
Err ( NwgError :: resource_create ( "Failed to create image from system resource" ) )
@@ -218,7 +223,7 @@ pub unsafe fn build_oem_image(
218
223
}
219
224
220
225
221
- /**
226
+ /**
222
227
Create a bitmap from memory. Only supports bitmap. Enable the `image-decoder` to load more image type from memory
223
228
The memory must contain the whole file (including the bitmap header).
224
229
*/
@@ -256,7 +261,7 @@ pub unsafe fn bitmap_from_memory(source: &[u8]) -> Result<HANDLE, NwgError> {
256
261
257
262
let header = BITMAPINFOHEADER {
258
263
biSize : mem:: size_of :: < BITMAPINFOHEADER > ( ) as DWORD ,
259
- biWidth : w as LONG , biHeight : h as LONG ,
264
+ biWidth : w as LONG , biHeight : h as LONG ,
260
265
biPlanes : 1 , biBitCount : 24 , biCompression : BI_RGB ,
261
266
biSizeImage : ( w * h * 3 ) as u32 ,
262
267
biXPelsPerMeter : 0 , biYPelsPerMeter : 0 ,
@@ -278,7 +283,7 @@ pub unsafe fn bitmap_from_memory(source: &[u8]) -> Result<HANDLE, NwgError> {
278
283
return Ok ( bitmap as HANDLE ) ;
279
284
}
280
285
281
- /**
286
+ /**
282
287
Create a bitmap from memory. The source can be any image type supported by the windows imaging component.
283
288
The memory must contain the whole file (including the file header).
284
289
*/
@@ -343,7 +348,7 @@ pub unsafe fn create_file_dialog<'a, 'b>(
343
348
multiselect : bool ,
344
349
default_folder : Option < String > ,
345
350
filters : Option < String >
346
- ) -> Result < * mut IFileDialog , NwgError >
351
+ ) -> Result < * mut IFileDialog , NwgError >
347
352
{
348
353
use winapi:: um:: shobjidl_core:: { CLSID_FileSaveDialog , CLSID_FileOpenDialog } ;
349
354
use winapi:: um:: shobjidl:: { FOS_PICKFOLDERS , FOS_ALLOWMULTISELECT , FOS_FORCEFILESYSTEM } ;
@@ -366,18 +371,18 @@ pub unsafe fn create_file_dialog<'a, 'b>(
366
371
367
372
// Set dialog options
368
373
if file_dialog. GetOptions ( & mut flags) != S_OK {
369
- file_dialog. Release ( ) ;
374
+ file_dialog. Release ( ) ;
370
375
return Err ( NwgError :: file_dialog ( "Filedialog creation failed" ) ) ;
371
376
}
372
-
377
+
373
378
let use_dir = if action == FileDialogAction :: OpenDirectory { FOS_PICKFOLDERS } else { 0 } ;
374
379
let multiselect = if multiselect { FOS_ALLOWMULTISELECT } else { 0 } ;
375
380
if file_dialog. SetOptions ( flags | FOS_FORCEFILESYSTEM | use_dir | multiselect) != S_OK {
376
381
file_dialog. Release ( ) ;
377
382
return Err ( NwgError :: file_dialog ( "Filedialog creation failed" ) ) ;
378
383
}
379
384
380
-
385
+
381
386
// Set the default folder
382
387
match & default_folder {
383
388
& Some ( ref f) => match file_dialog_set_default_folder ( file_dialog, f) {
@@ -424,7 +429,7 @@ pub unsafe fn file_dialog_set_default_folder<'a>(dialog: &mut IFileDialog, folde
424
429
425
430
let shellitem = & mut * shellitem;
426
431
let mut file_properties: SFGAOF = 0 ;
427
-
432
+
428
433
let results = shellitem. GetAttributes ( SFGAO_FOLDER , & mut file_properties) ;
429
434
430
435
if results != S_OK && results != S_FALSE {
@@ -466,7 +471,7 @@ pub unsafe fn file_dialog_set_filters<'a>(dialog: &mut IFileDialog, filters: &'a
466
471
467
472
let ( _name, _filter) = f. split_at ( end. unwrap ( ) ) ;
468
473
let ( name, filter) = ( to_utf16 ( _name) , to_utf16 ( & _filter[ 1 .._filter. len ( ) -1 ] ) ) ;
469
-
474
+
470
475
raw_filters. push ( COMDLG_FILTERSPEC { pszName : name. as_ptr ( ) , pszSpec : filter. as_ptr ( ) } ) ;
471
476
keep_alive. push ( ( name, filter) ) ;
472
477
}
@@ -483,7 +488,7 @@ pub unsafe fn file_dialog_set_filters<'a>(dialog: &mut IFileDialog, filters: &'a
483
488
#[ cfg( feature = "file-dialog" ) ]
484
489
pub unsafe fn filedialog_get_item ( dialog : & mut IFileDialog ) -> Result < OsString , NwgError > {
485
490
use winapi:: shared:: winerror:: S_OK ;
486
-
491
+
487
492
let mut _item: * mut IShellItem = ptr:: null_mut ( ) ;
488
493
489
494
if dialog. GetResult ( & mut _item) != S_OK {
@@ -500,7 +505,7 @@ pub unsafe fn filedialog_get_item(dialog: &mut IFileDialog) -> Result<OsString,
500
505
pub unsafe fn filedialog_get_items ( dialog : & mut IFileOpenDialog ) -> Result < Vec < OsString > , NwgError > {
501
506
use winapi:: um:: shobjidl:: IShellItemArray ;
502
507
use winapi:: shared:: { winerror:: S_OK , minwindef:: DWORD } ;
503
-
508
+
504
509
let mut _item: * mut IShellItem = ptr:: null_mut ( ) ;
505
510
let mut _items: * mut IShellItemArray = ptr:: null_mut ( ) ;
506
511
@@ -511,7 +516,7 @@ pub unsafe fn filedialog_get_items(dialog: &mut IFileOpenDialog) -> Result<Vec<O
511
516
let items = & mut * _items;
512
517
let mut count: DWORD = 0 ;
513
518
items. GetCount ( & mut count) ;
514
-
519
+
515
520
let mut item_names: Vec < OsString > = Vec :: with_capacity ( count as usize ) ;
516
521
for i in 0 ..count {
517
522
items. GetItemAt ( i, & mut _item) ;
@@ -560,7 +565,7 @@ pub unsafe fn file_dialog_options(dialog: &mut IFileDialog) -> Result<u32, NwgEr
560
565
#[ cfg( feature = "file-dialog" ) ]
561
566
pub unsafe fn toggle_dialog_flags ( dialog : & mut IFileDialog , flag : u32 , enabled : bool ) -> Result < ( ) , NwgError > {
562
567
use winapi:: shared:: winerror:: S_OK ;
563
-
568
+
564
569
let mut flags = file_dialog_options ( dialog) ?;
565
570
flags = match enabled {
566
571
true => flags | flag,
0 commit comments