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
25 #define NONAMELESSUNION
32 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(twain
);
38 #define ID_EDIT_BASE 0x1000
39 #define ID_STATIC_BASE 0x2000
41 static INT_PTR CALLBACK
DialogProc (HWND
, UINT
, WPARAM
, LPARAM
);
42 static INT CALLBACK
PropSheetProc(HWND
, UINT
,LPARAM
);
44 static int create_leading_static(HDC hdc
, const WCHAR
*text
,
45 LPDLGITEMTEMPLATEW
* template_out
, int y
, int id
)
47 LPDLGITEMTEMPLATEW tpl
= NULL
;
58 base
= GetDialogBaseUnits();
60 len
= lstrlenW(text
) * sizeof(WCHAR
);
61 len
+= sizeof(DLGITEMTEMPLATE
);
62 len
+= 4*sizeof(WORD
);
65 tpl
->style
=WS_VISIBLE
;
66 tpl
->dwExtendedStyle
= 0;
71 GetTextExtentPoint32W(hdc
,text
,lstrlenW(text
),&size
);
73 tpl
->cx
= MulDiv(size
.cx
,4,LOWORD(base
));
74 tpl
->cy
= MulDiv(size
.cy
,8,HIWORD(base
)) * 2;
75 ptr
= (WORD
*)(tpl
+ 1);
78 lstrcpyW( ptr
, text
);
79 ptr
+= lstrlenW(ptr
) + 1;
86 static int create_trailing_edit(HDC hdc
, LPDLGITEMTEMPLATEW
* template_out
, int id
,
87 int y
, const WCHAR
*text
,BOOL is_int
)
89 LPDLGITEMTEMPLATEW tpl
= NULL
;
94 static const char int_base
[] = "0000 xxx";
95 static const char float_base
[] = "0000.0000 xxx";
97 base
= GetDialogBaseUnits();
99 len
= lstrlenW(text
) * sizeof(WCHAR
);
100 len
+= sizeof(DLGITEMTEMPLATE
);
101 len
+= 4*sizeof(WORD
);
104 tpl
->style
=WS_VISIBLE
|ES_READONLY
|WS_BORDER
;
105 tpl
->dwExtendedStyle
= 0;
111 GetTextExtentPoint32A(hdc
,int_base
,lstrlenA(int_base
),&size
);
113 GetTextExtentPoint32A(hdc
,float_base
,lstrlenA(float_base
),&size
);
115 tpl
->cx
= MulDiv(size
.cx
*2,4,LOWORD(base
));
116 tpl
->cy
= MulDiv(size
.cy
,8,HIWORD(base
)) * 2;
118 ptr
= (WORD
*)(tpl
+ 1);
121 lstrcpyW( ptr
, text
);
122 ptr
+= lstrlenW(ptr
) + 1;
130 static int create_item(HDC hdc
, const struct option_descriptor
*opt
,
131 INT id
, LPDLGITEMTEMPLATEW
*template_out
, int y
, int *cx
, int* count
)
133 LPDLGITEMTEMPLATEW tpl
= NULL
,rc
= NULL
;
135 DWORD styles
= WS_VISIBLE
;
137 LPDLGITEMTEMPLATEW lead_static
= NULL
;
138 LPDLGITEMTEMPLATEW trail_edit
= NULL
;
139 DWORD leading_len
= 0;
142 const WCHAR
*title
= NULL
;
151 GetTextExtentPoint32A(hdc
,"X",1,&size
);
152 base
= GetDialogBaseUnits();
153 base_x
= MulDiv(size
.cx
,4,LOWORD(base
));
158 class = 0x0080; /* Button */
159 styles
|= BS_AUTOCHECKBOX
;
165 sane_option_get_value( id
- ID_BASE
, &i
);
166 swprintf(buffer
, ARRAY_SIZE(buffer
), L
"%i", i
);
168 switch (opt
->constraint_type
)
170 case CONSTRAINT_NONE
:
171 class = 0x0081; /* Edit*/
175 case CONSTRAINT_RANGE
:
176 class = 0x0084; /* scroll */
177 ctl_cx
= 10 * base_x
;
178 trail_len
+= create_trailing_edit(hdc
, &trail_edit
, id
+
179 ID_EDIT_BASE
, y
,buffer
,TRUE
);
182 class= 0x0085; /* Combo */
183 ctl_cx
= 10 * base_x
;
184 styles
|= CBS_DROPDOWNLIST
;
187 leading_len
+= create_leading_static(hdc
, opt
->title
, &lead_static
, y
, id
+ID_STATIC_BASE
);
192 int *i
= calloc( opt
->size
, sizeof(int) );
194 sane_option_get_value( id
- ID_BASE
, i
);
196 swprintf(buffer
, ARRAY_SIZE(buffer
), L
"%f", *i
/ 65536.0);
199 switch (opt
->constraint_type
)
201 case CONSTRAINT_NONE
:
202 class = 0x0081; /* Edit */
205 case CONSTRAINT_RANGE
:
206 class= 0x0084; /* scroll */
207 ctl_cx
= 10 * base_x
;
208 trail_len
+= create_trailing_edit(hdc
, &trail_edit
, id
+ ID_EDIT_BASE
, y
,buffer
,FALSE
);
211 class= 0x0085; /* Combo */
212 ctl_cx
= 10 * base_x
;
213 styles
|= CBS_DROPDOWNLIST
;
216 leading_len
+= create_leading_static(hdc
, opt
->title
, &lead_static
, y
, id
+ID_STATIC_BASE
);
222 switch (opt
->constraint_type
)
224 case CONSTRAINT_NONE
:
225 class = 0x0081; /* Edit*/
228 class= 0x0085; /* Combo */
229 ctl_cx
= opt
->size
* base_x
;
230 styles
|= CBS_DROPDOWNLIST
;
233 leading_len
+= create_leading_static(hdc
, opt
->title
, &lead_static
, y
, id
+ID_STATIC_BASE
);
234 sane_option_get_value( id
- ID_BASE
, str
);
235 MultiByteToWideChar( CP_UNIXCP
, 0, str
, -1, buffer
, ARRAY_SIZE(buffer
) );
240 class = 0x0080; /* Button */
244 class = 0x0080; /* Button */
245 styles
|= BS_GROUPBOX
;
250 local_len
+= sizeof(DLGITEMTEMPLATE
);
251 if (title
) local_len
+= lstrlenW(title
) * sizeof(WCHAR
);
252 local_len
+= 4*sizeof(WORD
);
254 padding
= leading_len
% sizeof(DWORD
);
255 rc
= realloc(lead_static
, leading_len
+ local_len
+ padding
);
256 tpl
= (LPDLGITEMTEMPLATEW
)((LPBYTE
)rc
+ leading_len
+ padding
);
258 tpl
->dwExtendedStyle
= 0;
260 tpl
->x
= lead_static
->x
+ lead_static
->cx
+ 1;
261 else if (opt
->type
== TYPE_GROUP
)
270 GetTextExtentPoint32W(hdc
,title
,lstrlenW(title
),&size
);
277 tpl
->cy
= lead_static
->cy
;
286 ptr
= (WORD
*)(tpl
+ 1);
291 lstrcpyW( ptr
, title
);
292 ptr
+= lstrlenW(ptr
);
299 trail_edit
->x
= tpl
->cx
+ tpl
->x
+ 2;
300 *cx
= trail_edit
->x
+ trail_edit
->cx
;
302 padding2
= (leading_len
+ local_len
+ padding
)% sizeof(DWORD
);
304 rc
= realloc( rc
, leading_len
+ local_len
+ padding
+padding2
+ trail_len
);
306 memcpy(((LPBYTE
)rc
) + leading_len
+ local_len
+ padding
+ padding2
,
307 trail_edit
,trail_len
);
310 *cx
= tpl
->cx
+ tpl
->x
;
321 return leading_len
+ local_len
+ padding
+ padding2
+ trail_len
;
325 static LPDLGTEMPLATEW
create_options_page(HDC hdc
, int *from_index
,
326 int optcount
, BOOL split_tabs
)
330 LPDLGTEMPLATEW tpl
= NULL
;
331 LPBYTE all_controls
= NULL
;
332 DWORD control_len
= 0;
334 int group_max_cx
= 0;
336 int group_offset
= -1;
337 INT control_count
= 0;
339 for (i
= *from_index
; i
< optcount
; i
++)
341 LPDLGITEMTEMPLATEW item_tpl
= NULL
;
342 struct option_descriptor opt
;
347 int hold_for_group
= 0;
350 if (SANE_CALL( option_get_descriptor
, &opt
)) continue;
351 if (opt
.type
== TYPE_GROUP
&& split_tabs
)
367 len
= create_item(hdc
, &opt
, ID_BASE
+ i
, &item_tpl
, y
, &x
, &count
);
369 control_count
+= count
;
377 y
+= item_tpl
->cy
+ 1;
378 max_cx
= max(max_cx
, x
+ 2);
379 group_max_cx
= max(group_max_cx
, x
);
381 padding
= len
% sizeof(DWORD
);
385 all_controls
= realloc(all_controls
, control_len
+ len
+ padding
);
386 memcpy(all_controls
+control_len
,item_tpl
,len
);
387 memset(all_controls
+control_len
+len
,0xca,padding
);
394 all_controls
= (LPBYTE
)item_tpl
;
398 all_controls
= malloc(len
+ padding
);
399 memcpy(all_controls
,item_tpl
,len
);
400 memset(all_controls
+len
,0xcb,padding
);
405 if (opt
.type
== TYPE_GROUP
)
407 if (group_offset
== -1)
409 group_offset
= control_len
;
414 LPDLGITEMTEMPLATEW group
=
415 (LPDLGITEMTEMPLATEW
)(all_controls
+group_offset
);
417 group
->cy
= hold_for_group
- group
->y
;
418 group
->cx
= group_max_cx
;
420 group
= (LPDLGITEMTEMPLATEW
)(all_controls
+control_len
);
424 group_offset
= control_len
;
428 control_len
+= len
+ padding
;
431 if ( group_offset
&& !split_tabs
)
433 LPDLGITEMTEMPLATEW group
=
434 (LPDLGITEMTEMPLATEW
)(all_controls
+group_offset
);
435 group
->cy
= y
- group
->y
;
436 group
->cx
= group_max_cx
;
443 tpl
= malloc(sizeof(DLGTEMPLATE
) + 3*sizeof(WORD
) + control_len
);
445 tpl
->style
= WS_VISIBLE
| WS_OVERLAPPEDWINDOW
;
446 tpl
->dwExtendedStyle
= 0;
447 tpl
->cdit
= control_count
;
450 tpl
->cx
= max_cx
+ 10;
452 ptr
= (LPBYTE
)tpl
+ sizeof(DLGTEMPLATE
);
453 *(LPWORD
)ptr
= 0x0000;
455 *(LPWORD
)ptr
= 0x0000;
457 *(LPWORD
)ptr
= 0x0000;
459 memcpy(ptr
,all_controls
,control_len
);
466 BOOL
DoScannerUI(void)
469 PROPSHEETPAGEW psp
[10];
471 PROPSHEETHEADERW psh
;
481 memset(psp
,0,sizeof(psp
));
482 rc
= sane_option_get_value( 0, &optcount
);
483 if (rc
!= TWCC_SUCCESS
)
485 ERR("Unable to read number of options\n");
489 while (index
< optcount
)
491 struct option_descriptor opt
;
493 psp
[page_count
].u
.pResource
= create_options_page(hdc
, &index
,
496 SANE_CALL( option_get_descriptor
, &opt
);
498 if (opt
.type
== TYPE_GROUP
)
500 psp
[page_count
].pszTitle
= wcsdup( opt
.title
);
503 if (psp
[page_count
].u
.pResource
)
505 psp
[page_count
].dwSize
= sizeof(PROPSHEETPAGEW
);
506 psp
[page_count
].dwFlags
= PSP_DLGINDIRECT
| PSP_USETITLE
;
507 psp
[page_count
].hInstance
= SANE_instance
;
508 psp
[page_count
].pfnDlgProc
= DialogProc
;
509 psp
[page_count
].lParam
= (LPARAM
)&activeDS
;
516 len
= lstrlenA(activeDS
.identity
.Manufacturer
)
517 + lstrlenA(activeDS
.identity
.ProductName
) + 2;
518 szCaption
= malloc(len
*sizeof(WCHAR
));
519 MultiByteToWideChar(CP_ACP
,0,activeDS
.identity
.Manufacturer
,-1,
521 szCaption
[lstrlenA(activeDS
.identity
.Manufacturer
)] = ' ';
522 MultiByteToWideChar(CP_ACP
,0,activeDS
.identity
.ProductName
,-1,
523 &szCaption
[lstrlenA(activeDS
.identity
.Manufacturer
)+1],len
);
524 psh
.dwSize
= sizeof(PROPSHEETHEADERW
);
525 psh
.dwFlags
= PSH_PROPSHEETPAGE
|PSH_PROPTITLE
|PSH_USECALLBACK
;
526 psh
.hwndParent
= activeDS
.hwndOwner
;
527 psh
.hInstance
= SANE_instance
;
529 psh
.pszCaption
= szCaption
;
530 psh
.nPages
= page_count
;
531 psh
.u2
.nStartPage
= 0;
532 psh
.u3
.ppsp
= (LPCPROPSHEETPAGEW
)psp
;
533 psh
.pfnCallback
= PropSheetProc
;
535 psrc
= PropertySheetW(&psh
);
537 for(index
= 0; index
< page_count
; index
++)
539 free((LPBYTE
)psp
[index
].u
.pResource
);
540 free((LPBYTE
)psp
[index
].pszTitle
);
550 static void UpdateRelevantEdit(HWND hwnd
, const struct option_descriptor
*opt
, int position
)
562 if (opt
->constraint
.range
.quant
)
563 si
= position
* opt
->constraint
.range
.quant
;
567 len
= swprintf( buffer
, ARRAY_SIZE(buffer
), L
"%i", si
);
574 if (opt
->constraint
.range
.quant
)
575 dd
= position
* (opt
->constraint
.range
.quant
/ 65536.0);
577 dd
= position
* 0.01;
579 len
= swprintf( buffer
, ARRAY_SIZE(buffer
), L
"%f", dd
);
587 LoadStringW( SANE_instance
, opt
->unit
, buffer
+ len
, ARRAY_SIZE( buffer
) - len
);
589 edit_w
= GetDlgItem(hwnd
,opt
->optno
+ ID_BASE
+ ID_EDIT_BASE
);
590 if (edit_w
) SetWindowTextW(edit_w
,buffer
);
594 static BOOL
UpdateSaneScrollOption(const struct option_descriptor
*opt
, DWORD position
)
603 if (opt
->constraint
.range
.quant
)
604 si
= position
* opt
->constraint
.range
.quant
;
608 sane_option_set_value( opt
->optno
, &si
, &result
);
612 if (opt
->constraint
.range
.quant
)
613 si
= position
* opt
->constraint
.range
.quant
;
615 si
= MulDiv( position
, 65536, 100 );
617 sane_option_set_value( opt
->optno
, &si
, &result
);
626 static INT_PTR
InitializeDialog(HWND hwnd
)
633 rc
= sane_option_get_value( 0, &optcount
);
634 if (rc
!= TWCC_SUCCESS
)
636 ERR("Unable to read number of options\n");
640 for ( i
= 1; i
< optcount
; i
++)
642 struct option_descriptor opt
;
644 control
= GetDlgItem(hwnd
,i
+ID_BASE
);
650 SANE_CALL( option_get_descriptor
, &opt
);
652 TRACE("%i %s %i %i\n",i
,debugstr_w(opt
.title
),opt
.type
,opt
.constraint_type
);
653 EnableWindow(control
,opt
.is_active
);
655 SendMessageA(control
,CB_RESETCONTENT
,0,0);
656 /* initialize values */
657 if (opt
.type
== TYPE_STRING
&& opt
.constraint_type
!= CONSTRAINT_NONE
)
662 for (p
= opt
.constraint
.strings
; *p
; p
+= lstrlenW(p
) + 1)
663 SendMessageW( control
,CB_ADDSTRING
,0, (LPARAM
)p
);
664 sane_option_get_value( i
, buffer
);
665 SendMessageA(control
,CB_SELECTSTRING
,0,(LPARAM
)buffer
);
667 else if (opt
.type
== TYPE_BOOL
)
670 sane_option_get_value( i
, &b
);
672 SendMessageA(control
,BM_SETCHECK
,BST_CHECKED
,0);
675 else if (opt
.type
== TYPE_INT
&& opt
.constraint_type
== CONSTRAINT_WORD_LIST
)
677 int j
, count
= opt
.constraint
.word_list
[0];
680 for (j
=1; j
<=count
; j
++)
682 sprintf(buffer
, "%d", opt
.constraint
.word_list
[j
]);
683 SendMessageA(control
, CB_ADDSTRING
, 0, (LPARAM
)buffer
);
685 sane_option_get_value( i
, &val
);
686 sprintf(buffer
, "%d", val
);
687 SendMessageA(control
,CB_SELECTSTRING
,0,(LPARAM
)buffer
);
689 else if (opt
.constraint_type
== CONSTRAINT_RANGE
)
691 if (opt
.type
== TYPE_INT
)
696 min
= opt
.constraint
.range
.min
/
697 (opt
.constraint
.range
.quant
? opt
.constraint
.range
.quant
: 1);
699 max
= opt
.constraint
.range
.max
/
700 (opt
.constraint
.range
.quant
? opt
.constraint
.range
.quant
: 1);
702 SendMessageA(control
,SBM_SETRANGE
,min
,max
);
704 sane_option_get_value( i
, &si
);
705 if (opt
.constraint
.range
.quant
)
706 si
= si
/ opt
.constraint
.range
.quant
;
708 SendMessageW(control
,SBM_SETPOS
, si
, TRUE
);
709 UpdateRelevantEdit(hwnd
, &opt
, si
);
711 else if (opt
.type
== TYPE_FIXED
)
713 int pos
, min
, max
, *sf
;
715 if (opt
.constraint
.range
.quant
)
717 min
= opt
.constraint
.range
.min
/ opt
.constraint
.range
.quant
;
718 max
= opt
.constraint
.range
.max
/ opt
.constraint
.range
.quant
;
722 min
= MulDiv( opt
.constraint
.range
.min
, 100, 65536 );
723 max
= MulDiv( opt
.constraint
.range
.max
, 100, 65536 );
726 SendMessageA(control
,SBM_SETRANGE
,min
,max
);
729 sf
= calloc( opt
.size
, sizeof(int) );
730 sane_option_get_value( i
, sf
);
732 /* Note that conversion of float -> SANE_Fixed is lossy;
733 * and when you truncate it into an integer, you can get
734 * unfortunate results. This calculation attempts
735 * to mitigate that harm */
736 if (opt
.constraint
.range
.quant
)
737 pos
= *sf
/ opt
.constraint
.range
.quant
;
739 pos
= MulDiv( *sf
, 100, 65536 );
742 SendMessageW(control
, SBM_SETPOS
, pos
, TRUE
);
743 UpdateRelevantEdit(hwnd
, &opt
, pos
);
751 static INT_PTR
ProcessScroll(HWND hwnd
, WPARAM wParam
, LPARAM lParam
)
753 struct option_descriptor opt
;
757 opt
.optno
= GetDlgCtrlID((HWND
)lParam
)- ID_BASE
;
761 if (SANE_CALL( option_get_descriptor
, &opt
)) return FALSE
;
763 scroll
= LOWORD(wParam
);
768 case SB_THUMBPOSITION
:
771 si
.cbSize
= sizeof(SCROLLINFO
);
772 si
.fMask
= SIF_TRACKPOS
;
773 GetScrollInfo((HWND
)lParam
,SB_CTL
, &si
);
774 position
= si
.nTrackPos
;
780 position
= SendMessageW((HWND
)lParam
,SBM_GETPOS
,0,0);
786 position
= SendMessageW((HWND
)lParam
,SBM_GETPOS
,0,0);
790 position
= SendMessageW((HWND
)lParam
,SBM_GETPOS
,0,0);
793 SendMessageW((HWND
)lParam
,SBM_SETPOS
, position
, TRUE
);
794 position
= SendMessageW((HWND
)lParam
,SBM_GETPOS
,0,0);
796 UpdateRelevantEdit(hwnd
, &opt
, position
);
797 if (UpdateSaneScrollOption(&opt
, position
))
798 InitializeDialog(hwnd
);
804 static void ButtonClicked(HWND hwnd
, INT id
, HWND control
)
806 struct option_descriptor opt
;
807 BOOL changed
= FALSE
;
809 opt
.optno
= id
- ID_BASE
;
813 if (SANE_CALL( option_get_descriptor
, &opt
)) return;
815 if (opt
.type
== TYPE_BOOL
)
817 BOOL r
= SendMessageW(control
,BM_GETCHECK
,0,0)==BST_CHECKED
;
818 sane_option_set_value( opt
.optno
, &r
, &changed
);
819 if (changed
) InitializeDialog(hwnd
);
823 static void ComboChanged(HWND hwnd
, INT id
, HWND control
)
827 struct option_descriptor opt
;
829 BOOL changed
= FALSE
;
831 opt
.optno
= id
- ID_BASE
;
835 if (SANE_CALL( option_get_descriptor
, &opt
)) return;
837 selection
= SendMessageW(control
,CB_GETCURSEL
,0,0);
838 len
= SendMessageW(control
,CB_GETLBTEXTLEN
,selection
,0);
842 SendMessageA(control
,CB_GETLBTEXT
,selection
,(LPARAM
)value
);
844 if (opt
.type
== TYPE_STRING
)
846 sane_option_set_value( opt
.optno
, value
, &changed
);
848 else if (opt
.type
== TYPE_INT
)
850 int val
= atoi( value
);
851 sane_option_set_value( opt
.optno
, &val
, &changed
);
853 if (changed
) InitializeDialog(hwnd
);
858 static INT_PTR CALLBACK
DialogProc(HWND hwndDlg
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
863 return InitializeDialog(hwndDlg
);
865 return ProcessScroll(hwndDlg
, wParam
, lParam
);
868 LPPSHNOTIFY psn
= (LPPSHNOTIFY
)lParam
;
869 switch (((NMHDR
*)lParam
)->code
)
874 activeDS
.currentState
= 6;
875 SANE_Notify(MSG_XFERREADY
);
878 case PSN_QUERYCANCEL
:
879 SANE_Notify(MSG_CLOSEDSREQ
);
882 InitializeDialog(hwndDlg
);
888 switch (HIWORD(wParam
))
891 ButtonClicked(hwndDlg
,LOWORD(wParam
), (HWND
)lParam
);
894 ComboChanged(hwndDlg
,LOWORD(wParam
), (HWND
)lParam
);
901 static int CALLBACK
PropSheetProc(HWND hwnd
, UINT msg
, LPARAM lParam
)
903 if (msg
== PSCB_INITIALIZED
)
905 /* rename OK button to Scan */
906 HWND scan
= GetDlgItem(hwnd
,IDOK
);
907 SetWindowTextA(scan
,"Scan");
913 static INT_PTR CALLBACK
ScanningProc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
918 HWND
ScanningDialogBox(HWND dialog
, LONG progress
)
921 dialog
= CreateDialogW(SANE_instance
,
922 (LPWSTR
)MAKEINTRESOURCE(IDD_DIALOG1
), NULL
, ScanningProc
);
930 RedrawWindow(dialog
,NULL
,NULL
,
931 RDW_INTERNALPAINT
|RDW_UPDATENOW
|RDW_ALLCHILDREN
);