4 * Copyright 2006 CodeWeavers, Aric Stewart
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(twain
);
36 #define ID_EDIT_BASE 0x1000
37 #define ID_STATIC_BASE 0x2000
39 static INT_PTR CALLBACK
DialogProc (HWND
, UINT
, WPARAM
, LPARAM
);
40 static INT CALLBACK
PropSheetProc(HWND
, UINT
,LPARAM
);
42 static int create_leading_static(HDC hdc
, const WCHAR
*text
,
43 LPDLGITEMTEMPLATEW
* template_out
, int y
, int id
)
45 LPDLGITEMTEMPLATEW tpl
= NULL
;
56 base
= GetDialogBaseUnits();
58 len
= lstrlenW(text
) * sizeof(WCHAR
);
59 len
+= sizeof(DLGITEMTEMPLATE
);
60 len
+= 4*sizeof(WORD
);
63 tpl
->style
=WS_VISIBLE
;
64 tpl
->dwExtendedStyle
= 0;
69 GetTextExtentPoint32W(hdc
,text
,lstrlenW(text
),&size
);
71 tpl
->cx
= MulDiv(size
.cx
,4,LOWORD(base
));
72 tpl
->cy
= MulDiv(size
.cy
,8,HIWORD(base
)) * 2;
73 ptr
= (WORD
*)(tpl
+ 1);
76 lstrcpyW( ptr
, text
);
77 ptr
+= lstrlenW(ptr
) + 1;
84 static int create_trailing_edit(HDC hdc
, LPDLGITEMTEMPLATEW
* template_out
, int id
,
85 int y
, const WCHAR
*text
,BOOL is_int
)
87 LPDLGITEMTEMPLATEW tpl
= NULL
;
92 static const char int_base
[] = "0000 xxx";
93 static const char float_base
[] = "0000.0000 xxx";
95 base
= GetDialogBaseUnits();
97 len
= lstrlenW(text
) * sizeof(WCHAR
);
98 len
+= sizeof(DLGITEMTEMPLATE
);
99 len
+= 4*sizeof(WORD
);
102 tpl
->style
=WS_VISIBLE
|ES_READONLY
|WS_BORDER
;
103 tpl
->dwExtendedStyle
= 0;
109 GetTextExtentPoint32A(hdc
,int_base
,lstrlenA(int_base
),&size
);
111 GetTextExtentPoint32A(hdc
,float_base
,lstrlenA(float_base
),&size
);
113 tpl
->cx
= MulDiv(size
.cx
*2,4,LOWORD(base
));
114 tpl
->cy
= MulDiv(size
.cy
,8,HIWORD(base
)) * 2;
116 ptr
= (WORD
*)(tpl
+ 1);
119 lstrcpyW( ptr
, text
);
120 ptr
+= lstrlenW(ptr
) + 1;
128 static int create_item(HDC hdc
, const struct option_descriptor
*opt
,
129 INT id
, LPDLGITEMTEMPLATEW
*template_out
, int y
, int *cx
, int* count
)
131 LPDLGITEMTEMPLATEW tpl
= NULL
,rc
= NULL
;
133 DWORD styles
= WS_VISIBLE
;
135 LPDLGITEMTEMPLATEW lead_static
= NULL
;
136 LPDLGITEMTEMPLATEW trail_edit
= NULL
;
137 DWORD leading_len
= 0;
140 const WCHAR
*title
= NULL
;
149 GetTextExtentPoint32A(hdc
,"X",1,&size
);
150 base
= GetDialogBaseUnits();
151 base_x
= MulDiv(size
.cx
,4,LOWORD(base
));
156 class = 0x0080; /* Button */
157 styles
|= BS_AUTOCHECKBOX
;
163 sane_option_get_value( id
- ID_BASE
, &i
);
164 swprintf(buffer
, ARRAY_SIZE(buffer
), L
"%i", i
);
166 switch (opt
->constraint_type
)
168 case CONSTRAINT_NONE
:
169 class = 0x0081; /* Edit*/
173 case CONSTRAINT_RANGE
:
174 class = 0x0084; /* scroll */
175 ctl_cx
= 10 * base_x
;
176 trail_len
+= create_trailing_edit(hdc
, &trail_edit
, id
+
177 ID_EDIT_BASE
, y
,buffer
,TRUE
);
180 class= 0x0085; /* Combo */
181 ctl_cx
= 10 * base_x
;
182 styles
|= CBS_DROPDOWNLIST
;
185 leading_len
+= create_leading_static(hdc
, opt
->title
, &lead_static
, y
, id
+ID_STATIC_BASE
);
190 int *i
= calloc( opt
->size
, sizeof(int) );
192 sane_option_get_value( id
- ID_BASE
, i
);
194 swprintf(buffer
, ARRAY_SIZE(buffer
), L
"%f", *i
/ 65536.0);
197 switch (opt
->constraint_type
)
199 case CONSTRAINT_NONE
:
200 class = 0x0081; /* Edit */
203 case CONSTRAINT_RANGE
:
204 class= 0x0084; /* scroll */
205 ctl_cx
= 10 * base_x
;
206 trail_len
+= create_trailing_edit(hdc
, &trail_edit
, id
+ ID_EDIT_BASE
, y
,buffer
,FALSE
);
209 class= 0x0085; /* Combo */
210 ctl_cx
= 10 * base_x
;
211 styles
|= CBS_DROPDOWNLIST
;
214 leading_len
+= create_leading_static(hdc
, opt
->title
, &lead_static
, y
, id
+ID_STATIC_BASE
);
220 switch (opt
->constraint_type
)
222 case CONSTRAINT_NONE
:
223 class = 0x0081; /* Edit*/
226 class= 0x0085; /* Combo */
227 ctl_cx
= opt
->size
* base_x
;
228 styles
|= CBS_DROPDOWNLIST
;
231 leading_len
+= create_leading_static(hdc
, opt
->title
, &lead_static
, y
, id
+ID_STATIC_BASE
);
232 sane_option_get_value( id
- ID_BASE
, str
);
233 MultiByteToWideChar( CP_UNIXCP
, 0, str
, -1, buffer
, ARRAY_SIZE(buffer
) );
238 class = 0x0080; /* Button */
242 class = 0x0080; /* Button */
243 styles
|= BS_GROUPBOX
;
248 local_len
+= sizeof(DLGITEMTEMPLATE
);
249 if (title
) local_len
+= lstrlenW(title
) * sizeof(WCHAR
);
250 local_len
+= 4*sizeof(WORD
);
252 padding
= leading_len
% sizeof(DWORD
);
253 rc
= realloc(lead_static
, leading_len
+ local_len
+ padding
);
254 tpl
= (LPDLGITEMTEMPLATEW
)((LPBYTE
)rc
+ leading_len
+ padding
);
256 tpl
->dwExtendedStyle
= 0;
258 tpl
->x
= rc
->x
+ rc
->cx
+ 1;
259 else if (opt
->type
== TYPE_GROUP
)
268 GetTextExtentPoint32W(hdc
,title
,lstrlenW(title
),&size
);
284 ptr
= (WORD
*)(tpl
+ 1);
289 lstrcpyW( ptr
, title
);
290 ptr
+= lstrlenW(ptr
);
297 trail_edit
->x
= tpl
->cx
+ tpl
->x
+ 2;
298 *cx
= trail_edit
->x
+ trail_edit
->cx
;
300 padding2
= (leading_len
+ local_len
+ padding
)% sizeof(DWORD
);
302 rc
= realloc( rc
, leading_len
+ local_len
+ padding
+padding2
+ trail_len
);
304 memcpy(((LPBYTE
)rc
) + leading_len
+ local_len
+ padding
+ padding2
,
305 trail_edit
,trail_len
);
308 *cx
= tpl
->cx
+ tpl
->x
;
320 return leading_len
+ local_len
+ padding
+ padding2
+ trail_len
;
324 static LPDLGTEMPLATEW
create_options_page(HDC hdc
, int *from_index
,
325 int optcount
, BOOL split_tabs
)
329 LPDLGTEMPLATEW tpl
= NULL
;
330 LPBYTE all_controls
= NULL
;
331 DWORD control_len
= 0;
333 int group_max_cx
= 0;
335 int group_offset
= -1;
336 INT control_count
= 0;
338 for (i
= *from_index
; i
< optcount
; i
++)
340 LPDLGITEMTEMPLATEW item_tpl
= NULL
;
341 struct option_descriptor opt
;
346 int hold_for_group
= 0;
349 if (SANE_CALL( option_get_descriptor
, &opt
)) continue;
350 if (opt
.type
== TYPE_GROUP
&& split_tabs
)
366 len
= create_item(hdc
, &opt
, ID_BASE
+ i
, &item_tpl
, y
, &x
, &count
);
368 control_count
+= count
;
376 y
+= item_tpl
->cy
+ 1;
377 max_cx
= max(max_cx
, x
+ 2);
378 group_max_cx
= max(group_max_cx
, x
);
380 padding
= len
% sizeof(DWORD
);
384 all_controls
= realloc(all_controls
, control_len
+ len
+ padding
);
385 memcpy(all_controls
+control_len
,item_tpl
,len
);
386 memset(all_controls
+control_len
+len
,0xca,padding
);
393 all_controls
= (LPBYTE
)item_tpl
;
397 all_controls
= malloc(len
+ padding
);
398 memcpy(all_controls
,item_tpl
,len
);
399 memset(all_controls
+len
,0xcb,padding
);
404 if (opt
.type
== TYPE_GROUP
)
406 if (group_offset
== -1)
408 group_offset
= control_len
;
413 LPDLGITEMTEMPLATEW group
=
414 (LPDLGITEMTEMPLATEW
)(all_controls
+group_offset
);
416 group
->cy
= hold_for_group
- group
->y
;
417 group
->cx
= group_max_cx
;
419 group
= (LPDLGITEMTEMPLATEW
)(all_controls
+control_len
);
423 group_offset
= control_len
;
427 control_len
+= len
+ padding
;
430 if ( group_offset
&& !split_tabs
)
432 LPDLGITEMTEMPLATEW group
=
433 (LPDLGITEMTEMPLATEW
)(all_controls
+group_offset
);
434 group
->cy
= y
- group
->y
;
435 group
->cx
= group_max_cx
;
442 tpl
= malloc(sizeof(DLGTEMPLATE
) + 3*sizeof(WORD
) + control_len
);
444 tpl
->style
= WS_VISIBLE
| WS_OVERLAPPEDWINDOW
;
445 tpl
->dwExtendedStyle
= 0;
446 tpl
->cdit
= control_count
;
449 tpl
->cx
= max_cx
+ 10;
451 ptr
= (LPBYTE
)tpl
+ sizeof(DLGTEMPLATE
);
452 *(LPWORD
)ptr
= 0x0000;
454 *(LPWORD
)ptr
= 0x0000;
456 *(LPWORD
)ptr
= 0x0000;
458 memcpy(ptr
,all_controls
,control_len
);
465 BOOL
DoScannerUI(void)
468 PROPSHEETPAGEW psp
[10];
470 PROPSHEETHEADERW psh
;
480 memset(psp
,0,sizeof(psp
));
481 rc
= sane_option_get_value( 0, &optcount
);
482 if (rc
!= TWCC_SUCCESS
)
484 ERR("Unable to read number of options\n");
488 while (index
< optcount
)
490 struct option_descriptor opt
;
492 psp
[page_count
].pResource
= create_options_page(hdc
, &index
,
495 SANE_CALL( option_get_descriptor
, &opt
);
497 if (opt
.type
== TYPE_GROUP
)
499 psp
[page_count
].pszTitle
= wcsdup( opt
.title
);
502 if (psp
[page_count
].pResource
)
504 psp
[page_count
].dwSize
= sizeof(PROPSHEETPAGEW
);
505 psp
[page_count
].dwFlags
= PSP_DLGINDIRECT
| PSP_USETITLE
;
506 psp
[page_count
].hInstance
= SANE_instance
;
507 psp
[page_count
].pfnDlgProc
= DialogProc
;
508 psp
[page_count
].lParam
= (LPARAM
)&activeDS
;
515 len
= lstrlenA(activeDS
.identity
.Manufacturer
)
516 + lstrlenA(activeDS
.identity
.ProductName
) + 2;
517 szCaption
= malloc(len
*sizeof(WCHAR
));
518 MultiByteToWideChar(CP_ACP
,0,activeDS
.identity
.Manufacturer
,-1,
520 szCaption
[lstrlenA(activeDS
.identity
.Manufacturer
)] = ' ';
521 MultiByteToWideChar(CP_ACP
,0,activeDS
.identity
.ProductName
,-1,
522 &szCaption
[lstrlenA(activeDS
.identity
.Manufacturer
)+1],len
);
523 psh
.dwSize
= sizeof(PROPSHEETHEADERW
);
524 psh
.dwFlags
= PSH_PROPSHEETPAGE
|PSH_PROPTITLE
|PSH_USECALLBACK
;
525 psh
.hwndParent
= activeDS
.hwndOwner
;
526 psh
.hInstance
= SANE_instance
;
528 psh
.pszCaption
= szCaption
;
529 psh
.nPages
= page_count
;
531 psh
.ppsp
= (LPCPROPSHEETPAGEW
)psp
;
532 psh
.pfnCallback
= PropSheetProc
;
534 psrc
= PropertySheetW(&psh
);
536 for(index
= 0; index
< page_count
; index
++)
538 free((LPBYTE
)psp
[index
].pResource
);
539 free((LPBYTE
)psp
[index
].pszTitle
);
549 static void UpdateRelevantEdit(HWND hwnd
, const struct option_descriptor
*opt
, int position
)
561 if (opt
->constraint
.range
.quant
)
562 si
= position
* opt
->constraint
.range
.quant
;
566 len
= swprintf( buffer
, ARRAY_SIZE(buffer
), L
"%i", si
);
573 if (opt
->constraint
.range
.quant
)
574 dd
= position
* (opt
->constraint
.range
.quant
/ 65536.0);
576 dd
= position
* 0.01;
578 len
= swprintf( buffer
, ARRAY_SIZE(buffer
), L
"%f", dd
);
586 LoadStringW( SANE_instance
, opt
->unit
, buffer
+ len
, ARRAY_SIZE( buffer
) - len
);
588 edit_w
= GetDlgItem(hwnd
,opt
->optno
+ ID_BASE
+ ID_EDIT_BASE
);
589 if (edit_w
) SetWindowTextW(edit_w
,buffer
);
593 static BOOL
UpdateSaneScrollOption(const struct option_descriptor
*opt
, DWORD position
)
602 if (opt
->constraint
.range
.quant
)
603 si
= position
* opt
->constraint
.range
.quant
;
607 sane_option_set_value( opt
->optno
, &si
, &result
);
611 if (opt
->constraint
.range
.quant
)
612 si
= position
* opt
->constraint
.range
.quant
;
614 si
= MulDiv( position
, 65536, 100 );
616 sane_option_set_value( opt
->optno
, &si
, &result
);
625 static INT_PTR
InitializeDialog(HWND hwnd
)
632 rc
= sane_option_get_value( 0, &optcount
);
633 if (rc
!= TWCC_SUCCESS
)
635 ERR("Unable to read number of options\n");
639 for ( i
= 1; i
< optcount
; i
++)
641 struct option_descriptor opt
;
643 control
= GetDlgItem(hwnd
,i
+ID_BASE
);
649 SANE_CALL( option_get_descriptor
, &opt
);
651 TRACE("%i %s %i %i\n",i
,debugstr_w(opt
.title
),opt
.type
,opt
.constraint_type
);
652 EnableWindow(control
,opt
.is_active
);
654 SendMessageA(control
,CB_RESETCONTENT
,0,0);
655 /* initialize values */
656 if (opt
.type
== TYPE_STRING
&& opt
.constraint_type
!= CONSTRAINT_NONE
)
661 for (p
= opt
.constraint
.strings
; *p
; p
+= lstrlenW(p
) + 1)
662 SendMessageW( control
,CB_ADDSTRING
,0, (LPARAM
)p
);
663 sane_option_get_value( i
, buffer
);
664 SendMessageA(control
,CB_SELECTSTRING
,0,(LPARAM
)buffer
);
666 else if (opt
.type
== TYPE_BOOL
)
669 sane_option_get_value( i
, &b
);
671 SendMessageA(control
,BM_SETCHECK
,BST_CHECKED
,0);
674 else if (opt
.type
== TYPE_INT
&& opt
.constraint_type
== CONSTRAINT_WORD_LIST
)
676 int j
, count
= opt
.constraint
.word_list
[0];
679 for (j
=1; j
<=count
; j
++)
681 sprintf(buffer
, "%d", opt
.constraint
.word_list
[j
]);
682 SendMessageA(control
, CB_ADDSTRING
, 0, (LPARAM
)buffer
);
684 sane_option_get_value( i
, &val
);
685 sprintf(buffer
, "%d", val
);
686 SendMessageA(control
,CB_SELECTSTRING
,0,(LPARAM
)buffer
);
688 else if (opt
.constraint_type
== CONSTRAINT_RANGE
)
690 if (opt
.type
== TYPE_INT
)
695 min
= opt
.constraint
.range
.min
/
696 (opt
.constraint
.range
.quant
? opt
.constraint
.range
.quant
: 1);
698 max
= opt
.constraint
.range
.max
/
699 (opt
.constraint
.range
.quant
? opt
.constraint
.range
.quant
: 1);
701 SendMessageA(control
,SBM_SETRANGE
,min
,max
);
703 sane_option_get_value( i
, &si
);
704 if (opt
.constraint
.range
.quant
)
705 si
= si
/ opt
.constraint
.range
.quant
;
707 SendMessageW(control
,SBM_SETPOS
, si
, TRUE
);
708 UpdateRelevantEdit(hwnd
, &opt
, si
);
710 else if (opt
.type
== TYPE_FIXED
)
712 int pos
, min
, max
, *sf
;
714 if (opt
.constraint
.range
.quant
)
716 min
= opt
.constraint
.range
.min
/ opt
.constraint
.range
.quant
;
717 max
= opt
.constraint
.range
.max
/ opt
.constraint
.range
.quant
;
721 min
= MulDiv( opt
.constraint
.range
.min
, 100, 65536 );
722 max
= MulDiv( opt
.constraint
.range
.max
, 100, 65536 );
725 SendMessageA(control
,SBM_SETRANGE
,min
,max
);
728 sf
= calloc( opt
.size
, sizeof(int) );
729 sane_option_get_value( i
, sf
);
731 /* Note that conversion of float -> SANE_Fixed is lossy;
732 * and when you truncate it into an integer, you can get
733 * unfortunate results. This calculation attempts
734 * to mitigate that harm */
735 if (opt
.constraint
.range
.quant
)
736 pos
= *sf
/ opt
.constraint
.range
.quant
;
738 pos
= MulDiv( *sf
, 100, 65536 );
741 SendMessageW(control
, SBM_SETPOS
, pos
, TRUE
);
742 UpdateRelevantEdit(hwnd
, &opt
, pos
);
750 static INT_PTR
ProcessScroll(HWND hwnd
, WPARAM wParam
, LPARAM lParam
)
752 struct option_descriptor opt
;
756 opt
.optno
= GetDlgCtrlID((HWND
)lParam
)- ID_BASE
;
760 if (SANE_CALL( option_get_descriptor
, &opt
)) return FALSE
;
762 scroll
= LOWORD(wParam
);
767 case SB_THUMBPOSITION
:
770 si
.cbSize
= sizeof(SCROLLINFO
);
771 si
.fMask
= SIF_TRACKPOS
;
772 GetScrollInfo((HWND
)lParam
,SB_CTL
, &si
);
773 position
= si
.nTrackPos
;
779 position
= SendMessageW((HWND
)lParam
,SBM_GETPOS
,0,0);
785 position
= SendMessageW((HWND
)lParam
,SBM_GETPOS
,0,0);
789 position
= SendMessageW((HWND
)lParam
,SBM_GETPOS
,0,0);
792 SendMessageW((HWND
)lParam
,SBM_SETPOS
, position
, TRUE
);
793 position
= SendMessageW((HWND
)lParam
,SBM_GETPOS
,0,0);
795 UpdateRelevantEdit(hwnd
, &opt
, position
);
796 if (UpdateSaneScrollOption(&opt
, position
))
797 InitializeDialog(hwnd
);
803 static void ButtonClicked(HWND hwnd
, INT id
, HWND control
)
805 struct option_descriptor opt
;
806 BOOL changed
= FALSE
;
808 opt
.optno
= id
- ID_BASE
;
812 if (SANE_CALL( option_get_descriptor
, &opt
)) return;
814 if (opt
.type
== TYPE_BOOL
)
816 BOOL r
= SendMessageW(control
,BM_GETCHECK
,0,0)==BST_CHECKED
;
817 sane_option_set_value( opt
.optno
, &r
, &changed
);
818 if (changed
) InitializeDialog(hwnd
);
822 static void ComboChanged(HWND hwnd
, INT id
, HWND control
)
826 struct option_descriptor opt
;
828 BOOL changed
= FALSE
;
830 opt
.optno
= id
- ID_BASE
;
834 if (SANE_CALL( option_get_descriptor
, &opt
)) return;
836 selection
= SendMessageW(control
,CB_GETCURSEL
,0,0);
837 len
= SendMessageW(control
,CB_GETLBTEXTLEN
,selection
,0);
841 SendMessageA(control
,CB_GETLBTEXT
,selection
,(LPARAM
)value
);
843 if (opt
.type
== TYPE_STRING
)
845 sane_option_set_value( opt
.optno
, value
, &changed
);
847 else if (opt
.type
== TYPE_INT
)
849 int val
= atoi( value
);
850 sane_option_set_value( opt
.optno
, &val
, &changed
);
852 if (changed
) InitializeDialog(hwnd
);
857 static INT_PTR CALLBACK
DialogProc(HWND hwndDlg
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
862 return InitializeDialog(hwndDlg
);
864 return ProcessScroll(hwndDlg
, wParam
, lParam
);
867 LPPSHNOTIFY psn
= (LPPSHNOTIFY
)lParam
;
868 switch (((NMHDR
*)lParam
)->code
)
873 activeDS
.currentState
= 6;
874 SANE_Notify(MSG_XFERREADY
);
877 case PSN_QUERYCANCEL
:
878 SANE_Notify(MSG_CLOSEDSREQ
);
881 InitializeDialog(hwndDlg
);
887 switch (HIWORD(wParam
))
890 ButtonClicked(hwndDlg
,LOWORD(wParam
), (HWND
)lParam
);
893 ComboChanged(hwndDlg
,LOWORD(wParam
), (HWND
)lParam
);
900 static int CALLBACK
PropSheetProc(HWND hwnd
, UINT msg
, LPARAM lParam
)
902 if (msg
== PSCB_INITIALIZED
)
904 /* rename OK button to Scan */
905 HWND scan
= GetDlgItem(hwnd
,IDOK
);
906 SetWindowTextA(scan
,"Scan");
912 static INT_PTR CALLBACK
ScanningProc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
917 HWND
ScanningDialogBox(HWND dialog
, LONG progress
)
920 dialog
= CreateDialogW(SANE_instance
,
921 (LPWSTR
)MAKEINTRESOURCE(IDD_DIALOG1
), NULL
, ScanningProc
);
929 RedrawWindow(dialog
,NULL
,NULL
,
930 RDW_INTERNALPAINT
|RDW_UPDATENOW
|RDW_ALLCHILDREN
);