winex11: Add 1280x960 resolution.
[wine.git] / dlls / sane.ds / ui.c
blob5574c5ab507d4ccde42a25983dc2884d53cb6485
1 /*
2 * TWAIN32 Options UI
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
21 #include "config.h"
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <stdio.h>
27 #define NONAMELESSUNION
29 #include "sane_i.h"
30 #include "winuser.h"
31 #include "winnls.h"
32 #include "wingdi.h"
33 #include "prsht.h"
34 #include "wine/debug.h"
35 #include "resource.h"
36 #include "wine/unicode.h"
38 #ifdef SONAME_LIBSANE
40 WINE_DEFAULT_DEBUG_CHANNEL(twain);
42 #define ID_BASE 0x100
43 #define ID_EDIT_BASE 0x1000
44 #define ID_STATIC_BASE 0x2000
46 static INT_PTR CALLBACK DialogProc (HWND , UINT , WPARAM , LPARAM );
47 static INT CALLBACK PropSheetProc(HWND, UINT,LPARAM);
49 static int create_leading_static(HDC hdc, LPCSTR text,
50 LPDLGITEMTEMPLATEW* template_out, int y, int id)
52 LPDLGITEMTEMPLATEW tpl = NULL;
53 INT len;
54 SIZE size;
55 LPBYTE ptr;
56 LONG base;
58 *template_out = NULL;
60 if (!text)
61 return 0;
63 base = GetDialogBaseUnits();
65 len = MultiByteToWideChar(CP_ACP,0,text,-1,NULL,0);
66 len *= sizeof(WCHAR);
67 len += sizeof(DLGITEMTEMPLATE);
68 len += 3*sizeof(WORD);
70 tpl = HeapAlloc(GetProcessHeap(),0,len);
71 tpl->style=WS_VISIBLE;
72 tpl->dwExtendedStyle = 0;
73 tpl->x = 4;
74 tpl->y = y;
75 tpl->id = ID_BASE;
77 GetTextExtentPoint32A(hdc,text,lstrlenA(text),&size);
79 tpl->cx = MulDiv(size.cx,4,LOWORD(base));
80 tpl->cy = MulDiv(size.cy,8,HIWORD(base)) * 2;
81 ptr = (LPBYTE)tpl + sizeof(DLGITEMTEMPLATE);
82 *(LPWORD)ptr = 0xffff;
83 ptr += sizeof(WORD);
84 *(LPWORD)ptr = 0x0082;
85 ptr += sizeof(WORD);
86 ptr += MultiByteToWideChar(CP_ACP,0,text,-1,(LPWSTR)ptr,len) * sizeof(WCHAR);
87 *(LPWORD)ptr = 0x0000;
89 *template_out = tpl;
90 return len;
93 static int create_trailing_edit(HDC hdc, LPDLGITEMTEMPLATEW* template_out, int id,
94 int y, LPCSTR text,BOOL is_int)
96 LPDLGITEMTEMPLATEW tpl = NULL;
97 INT len;
98 LPBYTE ptr;
99 SIZE size;
100 LONG base;
101 static const char int_base[] = "0000 xxx";
102 static const char float_base[] = "0000.0000 xxx";
104 base = GetDialogBaseUnits();
106 len = MultiByteToWideChar(CP_ACP,0,text,-1,NULL,0);
107 len *= sizeof(WCHAR);
108 len += sizeof(DLGITEMTEMPLATE);
109 len += 3*sizeof(WORD);
111 tpl = HeapAlloc(GetProcessHeap(),0,len);
112 tpl->style=WS_VISIBLE|ES_READONLY|WS_BORDER;
113 tpl->dwExtendedStyle = 0;
114 tpl->x = 1;
115 tpl->y = y;
116 tpl->id = id;
118 if (is_int)
119 GetTextExtentPoint32A(hdc,int_base,lstrlenA(int_base),&size);
120 else
121 GetTextExtentPoint32A(hdc,float_base,lstrlenA(float_base),&size);
123 tpl->cx = MulDiv(size.cx*2,4,LOWORD(base));
124 tpl->cy = MulDiv(size.cy,8,HIWORD(base)) * 2;
126 ptr = (LPBYTE)tpl + sizeof(DLGITEMTEMPLATE);
127 *(LPWORD)ptr = 0xffff;
128 ptr += sizeof(WORD);
129 *(LPWORD)ptr = 0x0081;
130 ptr += sizeof(WORD);
131 ptr += MultiByteToWideChar(CP_ACP,0,text,-1,(LPWSTR)ptr,len) * sizeof(WCHAR);
132 *(LPWORD)ptr = 0x0000;
134 *template_out = tpl;
135 return len;
139 static int create_item(HDC hdc, const SANE_Option_Descriptor *opt,
140 INT id, LPDLGITEMTEMPLATEW *template_out, int y, int *cx, int* count)
142 LPDLGITEMTEMPLATEW tpl = NULL,rc = NULL;
143 WORD class = 0xffff;
144 DWORD styles = WS_VISIBLE;
145 LPBYTE ptr = NULL;
146 LPDLGITEMTEMPLATEW lead_static = NULL;
147 LPDLGITEMTEMPLATEW trail_edit = NULL;
148 DWORD leading_len = 0;
149 DWORD trail_len = 0;
150 DWORD local_len = 0;
151 LPCSTR title = NULL;
152 CHAR buffer[255];
153 int padding = 0;
154 int padding2 = 0;
155 int base_x = 0;
156 LONG base;
157 int ctl_cx = 0;
158 SIZE size;
160 GetTextExtentPoint32A(hdc,"X",1,&size);
161 base = GetDialogBaseUnits();
162 base_x = MulDiv(size.cx,4,LOWORD(base));
164 if (opt->type == SANE_TYPE_BOOL)
166 class = 0x0080; /* Button */
167 styles |= BS_AUTOCHECKBOX;
168 local_len += MultiByteToWideChar(CP_ACP,0,opt->title,-1,NULL,0);
169 local_len *= sizeof(WCHAR);
170 title = opt->title;
172 else if (opt->type == SANE_TYPE_INT)
174 SANE_Int i;
176 psane_control_option(activeDS.deviceHandle, id-ID_BASE,
177 SANE_ACTION_GET_VALUE, &i,NULL);
179 sprintf(buffer,"%i",i);
181 if (opt->constraint_type == SANE_CONSTRAINT_NONE)
183 class = 0x0081; /* Edit*/
184 styles |= ES_NUMBER;
185 title = buffer;
186 local_len += MultiByteToWideChar(CP_ACP,0,title,-1,NULL,0);
187 local_len *= sizeof(WCHAR);
189 else if (opt->constraint_type == SANE_CONSTRAINT_RANGE)
191 class = 0x0084; /* scroll */
192 ctl_cx = 10 * base_x;
193 trail_len += create_trailing_edit(hdc, &trail_edit, id +
194 ID_EDIT_BASE, y,buffer,TRUE);
196 else
198 class= 0x0085; /* Combo */
199 ctl_cx = 10 * base_x;
200 styles |= CBS_DROPDOWNLIST;
202 leading_len += create_leading_static(hdc, opt->title, &lead_static, y,
203 id+ID_STATIC_BASE);
205 else if (opt->type == SANE_TYPE_FIXED)
207 SANE_Fixed *i;
208 double dd;
210 i = HeapAlloc(GetProcessHeap(),0,opt->size*sizeof(SANE_Word));
212 psane_control_option(activeDS.deviceHandle, id-ID_BASE,
213 SANE_ACTION_GET_VALUE, i, NULL);
215 dd = SANE_UNFIX(*i);
216 sprintf(buffer,"%f",dd);
217 HeapFree(GetProcessHeap(),0,i);
219 if (opt->constraint_type == SANE_CONSTRAINT_NONE)
221 class = 0x0081; /* Edit */
222 title = buffer;
223 local_len += MultiByteToWideChar(CP_ACP,0,title,-1,NULL,0);
224 local_len *= sizeof(WCHAR);
226 else if (opt->constraint_type == SANE_CONSTRAINT_RANGE)
228 class= 0x0084; /* scroll */
229 ctl_cx = 10 * base_x;
230 trail_len += create_trailing_edit(hdc, &trail_edit, id +
231 ID_EDIT_BASE, y,buffer,FALSE);
233 else
235 class= 0x0085; /* Combo */
236 ctl_cx = 10 * base_x;
237 styles |= CBS_DROPDOWNLIST;
239 leading_len += create_leading_static(hdc, opt->title, &lead_static, y,
240 id+ID_STATIC_BASE);
242 else if (opt->type == SANE_TYPE_STRING)
244 if (opt->constraint_type == SANE_CONSTRAINT_NONE)
246 class = 0x0081; /* Edit*/
248 else
250 class= 0x0085; /* Combo */
251 ctl_cx = opt->size * base_x;
252 styles |= CBS_DROPDOWNLIST;
254 leading_len += create_leading_static(hdc, opt->title, &lead_static, y,
255 id+ID_STATIC_BASE);
256 psane_control_option(activeDS.deviceHandle, id-ID_BASE,
257 SANE_ACTION_GET_VALUE, buffer,NULL);
259 title = buffer;
260 local_len += MultiByteToWideChar(CP_ACP,0,title,-1,NULL,0);
261 local_len *= sizeof(WCHAR);
263 else if (opt->type == SANE_TYPE_BUTTON)
265 class = 0x0080; /* Button */
266 local_len += MultiByteToWideChar(CP_ACP,0,opt->title,-1,NULL,0);
267 local_len *= sizeof(WCHAR);
268 title = opt->title;
270 else if (opt->type == SANE_TYPE_GROUP)
272 class = 0x0080; /* Button */
273 styles |= BS_GROUPBOX;
274 local_len += MultiByteToWideChar(CP_ACP,0,opt->title,-1,NULL,0);
275 local_len *= sizeof(WCHAR);
276 title = opt->title;
279 local_len += sizeof(DLGITEMTEMPLATE);
280 if (title)
281 local_len += 3*sizeof(WORD);
282 else
283 local_len += 4*sizeof(WORD);
285 if (lead_static)
287 padding = leading_len % sizeof(DWORD);
288 rc = HeapReAlloc(GetProcessHeap(),0,lead_static,leading_len+local_len + padding);
289 tpl = (LPDLGITEMTEMPLATEW)((LPBYTE)rc + leading_len + padding);
291 else
292 rc = tpl = HeapAlloc(GetProcessHeap(),0,local_len);
294 tpl->style=styles;
295 tpl->dwExtendedStyle = 0;
296 if (lead_static)
297 tpl->x = lead_static->x + lead_static->cx + 1;
298 else if (opt->type == SANE_TYPE_GROUP)
299 tpl->x = 2;
300 else
301 tpl->x = 4;
302 tpl->y = y;
303 tpl->id = id;
305 if (title)
307 GetTextExtentPoint32A(hdc,title,lstrlenA(title),&size);
308 tpl->cx = size.cx;
309 tpl->cy = size.cy;
311 else
313 if (lead_static)
314 tpl->cy = lead_static->cy;
315 else
316 tpl->cy = 15;
318 if (!ctl_cx)
319 ctl_cx = 15;
321 tpl->cx = ctl_cx;
323 ptr = (LPBYTE)tpl + sizeof(DLGITEMTEMPLATE);
324 *(LPWORD)ptr = 0xffff;
325 ptr += sizeof(WORD);
326 *(LPWORD)ptr = class;
327 ptr += sizeof(WORD);
328 if (title)
330 ptr += MultiByteToWideChar(CP_ACP,0,title,-1,(LPWSTR)ptr,local_len) * sizeof(WCHAR);
332 else
334 *(LPWORD)ptr = 0x0000;
335 ptr += sizeof(WORD);
338 *((LPWORD)ptr) = 0x0000;
339 ptr += sizeof(WORD);
341 if (trail_edit)
343 trail_edit->x = tpl->cx + tpl->x + 2;
344 *cx = trail_edit->x + trail_edit->cx;
346 padding2 = (leading_len + local_len + padding)% sizeof(DWORD);
348 rc = HeapReAlloc(GetProcessHeap(),0,rc,leading_len+local_len + padding
349 +padding2+trail_len);
351 memcpy(((LPBYTE)rc) + leading_len + local_len + padding + padding2,
352 trail_edit,trail_len);
354 else
355 *cx = tpl->cx + tpl->x;
357 *template_out = rc;
358 if (leading_len)
359 *count = 2;
360 else
361 *count = 1;
363 if (trail_edit)
364 *count+=1;
366 return leading_len + local_len + padding + padding2 + trail_len;
370 static LPDLGTEMPLATEW create_options_page(HDC hdc, int *from_index,
371 SANE_Int optcount, BOOL split_tabs)
373 int i;
374 INT y = 2;
375 LPDLGTEMPLATEW tpl = NULL;
376 LPBYTE all_controls = NULL;
377 DWORD control_len = 0;
378 int max_cx = 0;
379 int group_max_cx = 0;
380 LPBYTE ptr;
381 int group_offset = -1;
382 INT control_count = 0;
384 for (i = *from_index; i < optcount; i++)
386 LPDLGITEMTEMPLATEW item_tpl = NULL;
387 const SANE_Option_Descriptor *opt;
388 int len;
389 int padding = 0;
390 int x;
391 int count;
392 int hold_for_group = 0;
394 opt = psane_get_option_descriptor(activeDS.deviceHandle, i);
395 if (!opt)
396 continue;
397 if (opt->type == SANE_TYPE_GROUP && split_tabs)
399 if (control_len > 0)
401 *from_index = i - 1;
402 goto exit;
404 else
406 *from_index = i;
407 return NULL;
410 if (!SANE_OPTION_IS_ACTIVE (opt->cap))
411 continue;
413 len = create_item(hdc, opt, ID_BASE + i, &item_tpl, y, &x, &count);
415 control_count += count;
417 if (!len)
419 continue;
422 hold_for_group = y;
423 y+= item_tpl->cy + 1;
424 max_cx = max(max_cx, x + 2);
425 group_max_cx = max(group_max_cx, x );
427 padding = len % sizeof(DWORD);
429 if (all_controls)
431 LPBYTE newone;
432 newone = HeapReAlloc(GetProcessHeap(),0,all_controls,
433 control_len + len + padding);
434 all_controls = newone;
435 memcpy(all_controls+control_len,item_tpl,len);
436 memset(all_controls+control_len+len,0xca,padding);
437 HeapFree(GetProcessHeap(),0,item_tpl);
439 else
441 if (!padding)
443 all_controls = (LPBYTE)item_tpl;
445 else
447 all_controls = HeapAlloc(GetProcessHeap(),0,len + padding);
448 memcpy(all_controls,item_tpl,len);
449 memset(all_controls+len,0xcb,padding);
450 HeapFree(GetProcessHeap(),0,item_tpl);
454 if (opt->type == SANE_TYPE_GROUP)
456 if (group_offset == -1)
458 group_offset = control_len;
459 group_max_cx = 0;
461 else
463 LPDLGITEMTEMPLATEW group =
464 (LPDLGITEMTEMPLATEW)(all_controls+group_offset);
466 group->cy = hold_for_group - group->y;
467 group->cx = group_max_cx;
469 group = (LPDLGITEMTEMPLATEW)(all_controls+control_len);
470 group->y += 2;
471 y+=2;
472 group_max_cx = 0;
473 group_offset = control_len;
477 control_len += len + padding;
480 if ( group_offset && !split_tabs )
482 LPDLGITEMTEMPLATEW group =
483 (LPDLGITEMTEMPLATEW)(all_controls+group_offset);
484 group->cy = y - group->y;
485 group->cx = group_max_cx;
486 y+=2;
489 *from_index = i-1;
490 exit:
492 tpl = HeapAlloc(GetProcessHeap(),0,sizeof(DLGTEMPLATE) + 3*sizeof(WORD) +
493 control_len);
495 tpl->style = WS_VISIBLE | WS_OVERLAPPEDWINDOW;
496 tpl->dwExtendedStyle = 0;
497 tpl->cdit = control_count;
498 tpl->x = 0;
499 tpl->y = 0;
500 tpl->cx = max_cx + 10;
501 tpl->cy = y + 10;
502 ptr = (LPBYTE)tpl + sizeof(DLGTEMPLATE);
503 *(LPWORD)ptr = 0x0000;
504 ptr+=sizeof(WORD);
505 *(LPWORD)ptr = 0x0000;
506 ptr+=sizeof(WORD);
507 *(LPWORD)ptr = 0x0000;
508 ptr+=sizeof(WORD);
509 memcpy(ptr,all_controls,control_len);
511 HeapFree(GetProcessHeap(),0,all_controls);
513 return tpl;
516 BOOL DoScannerUI(void)
518 HDC hdc;
519 PROPSHEETPAGEW psp[10];
520 int page_count= 0;
521 PROPSHEETHEADERW psh;
522 int index = 1;
523 SANE_Status rc;
524 SANE_Int optcount;
525 UINT psrc;
526 LPWSTR szCaption;
527 DWORD len;
529 hdc = GetDC(0);
531 memset(psp,0,sizeof(psp));
532 rc = psane_control_option(activeDS.deviceHandle, 0, SANE_ACTION_GET_VALUE,
533 &optcount, NULL);
534 if (rc != SANE_STATUS_GOOD)
536 ERR("Unable to read number of options\n");
537 return FALSE;
540 while (index < optcount)
542 const SANE_Option_Descriptor *opt;
543 psp[page_count].u.pResource = create_options_page(hdc, &index,
544 optcount, TRUE);
545 opt = psane_get_option_descriptor(activeDS.deviceHandle, index);
547 if (opt->type == SANE_TYPE_GROUP)
549 LPWSTR title = NULL;
550 INT len;
552 len = MultiByteToWideChar(CP_ACP,0,opt->title,-1,NULL,0);
553 title = HeapAlloc(GetProcessHeap(),0,len * sizeof(WCHAR));
554 MultiByteToWideChar(CP_ACP,0,opt->title,-1,title,len);
556 psp[page_count].pszTitle = title;
559 if (psp[page_count].u.pResource)
561 psp[page_count].dwSize = sizeof(PROPSHEETPAGEW);
562 psp[page_count].dwFlags = PSP_DLGINDIRECT | PSP_USETITLE;
563 psp[page_count].hInstance = SANE_instance;
564 psp[page_count].pfnDlgProc = DialogProc;
565 psp[page_count].lParam = (LPARAM)&activeDS;
566 page_count ++;
569 index ++;
572 len = lstrlenA(activeDS.identity.Manufacturer)
573 + lstrlenA(activeDS.identity.ProductName) + 2;
574 szCaption = HeapAlloc(GetProcessHeap(),0,len *sizeof(WCHAR));
575 MultiByteToWideChar(CP_ACP,0,activeDS.identity.Manufacturer,-1,
576 szCaption,len);
577 szCaption[lstrlenA(activeDS.identity.Manufacturer)] = ' ';
578 MultiByteToWideChar(CP_ACP,0,activeDS.identity.ProductName,-1,
579 &szCaption[lstrlenA(activeDS.identity.Manufacturer)+1],len);
580 psh.dwSize = sizeof(PROPSHEETHEADERW);
581 psh.dwFlags = PSH_PROPSHEETPAGE|PSH_PROPTITLE|PSH_USECALLBACK;
582 psh.hwndParent = activeDS.hwndOwner;
583 psh.hInstance = SANE_instance;
584 psh.u.pszIcon = 0;
585 psh.pszCaption = szCaption;
586 psh.nPages = page_count;
587 psh.u2.nStartPage = 0;
588 psh.u3.ppsp = (LPCPROPSHEETPAGEW)psp;
589 psh.pfnCallback = PropSheetProc;
591 psrc = PropertySheetW(&psh);
593 for(index = 0; index < page_count; index ++)
595 HeapFree(GetProcessHeap(),0,(LPBYTE)psp[index].u.pResource);
596 HeapFree(GetProcessHeap(),0,(LPBYTE)psp[index].pszTitle);
598 HeapFree(GetProcessHeap(),0,szCaption);
600 if (psrc == IDOK)
601 return TRUE;
602 else
603 return FALSE;
606 static void UpdateRelevantEdit(HWND hwnd, const SANE_Option_Descriptor *opt,
607 int index, int position)
609 WCHAR buffer[244];
610 HWND edit_w;
611 int len;
613 if (opt->type == SANE_TYPE_INT)
615 static const WCHAR formatW[] = {'%','i',0};
616 INT si;
618 if (opt->constraint.range->quant)
619 si = position * opt->constraint.range->quant;
620 else
621 si = position;
623 len = sprintfW( buffer, formatW, si );
625 else if (opt->type == SANE_TYPE_FIXED)
627 static const WCHAR formatW[] = {'%','f',0};
628 double s_quant, dd;
630 s_quant = SANE_UNFIX(opt->constraint.range->quant);
632 if (s_quant)
633 dd = position * s_quant;
634 else
635 dd = position * 0.01;
637 len = sprintfW( buffer, formatW, dd );
639 else return;
641 buffer[len++] = ' ';
642 LoadStringW( SANE_instance, opt->unit, buffer + len, sizeof(buffer)/sizeof(WCHAR) - len );
644 edit_w = GetDlgItem(hwnd,index+ID_BASE+ID_EDIT_BASE);
645 if (edit_w) SetWindowTextW(edit_w,buffer);
649 static BOOL UpdateSaneScrollOption(
650 const SANE_Option_Descriptor *opt, int index, DWORD position)
652 SANE_Status rc = SANE_STATUS_GOOD;
653 SANE_Int result = 0;
655 if (opt->type == SANE_TYPE_INT)
657 SANE_Int si;
659 if (opt->constraint.range->quant)
660 si = position * opt->constraint.range->quant;
661 else
662 si = position;
664 rc = psane_control_option (activeDS.deviceHandle,index,
665 SANE_ACTION_SET_VALUE, &si, &result);
668 else if (opt->type == SANE_TYPE_FIXED)
670 double s_quant, dd;
671 SANE_Fixed *sf;
673 s_quant = SANE_UNFIX(opt->constraint.range->quant);
675 if (s_quant)
676 dd = position * s_quant;
677 else
678 dd = position * 0.01;
680 sf = HeapAlloc(GetProcessHeap(),0,opt->size*sizeof(SANE_Word));
682 *sf = SANE_FIX(dd);
684 rc = psane_control_option (activeDS.deviceHandle,index,
685 SANE_ACTION_SET_VALUE, sf, &result);
687 HeapFree(GetProcessHeap(),0,sf);
690 if(rc == SANE_STATUS_GOOD)
692 if (result & SANE_INFO_RELOAD_OPTIONS ||
693 result & SANE_INFO_RELOAD_PARAMS || result & SANE_INFO_INEXACT)
694 return TRUE;
696 return FALSE;
699 static BOOL UpdateSaneBoolOption(int index, BOOL position)
701 SANE_Status rc = SANE_STATUS_GOOD;
702 SANE_Int result = 0;
703 SANE_Bool si;
705 si = position;
707 rc = psane_control_option (activeDS.deviceHandle,index,
708 SANE_ACTION_SET_VALUE, &si, &result);
710 if(rc == SANE_STATUS_GOOD)
712 if (result & SANE_INFO_RELOAD_OPTIONS ||
713 result & SANE_INFO_RELOAD_PARAMS || result & SANE_INFO_INEXACT)
714 return TRUE;
716 return FALSE;
719 static BOOL UpdateSaneStringOption(int index, SANE_String value)
721 SANE_Status rc = SANE_STATUS_GOOD;
722 SANE_Int result = 0;
724 rc = psane_control_option (activeDS.deviceHandle,index,
725 SANE_ACTION_SET_VALUE, value, &result);
727 if(rc == SANE_STATUS_GOOD)
729 if (result & SANE_INFO_RELOAD_OPTIONS ||
730 result & SANE_INFO_RELOAD_PARAMS || result & SANE_INFO_INEXACT)
731 return TRUE;
733 return FALSE;
736 static INT_PTR InitializeDialog(HWND hwnd)
738 SANE_Status rc;
739 SANE_Int optcount;
740 HWND control;
741 int i;
743 rc = psane_control_option(activeDS.deviceHandle, 0, SANE_ACTION_GET_VALUE,
744 &optcount, NULL);
745 if (rc != SANE_STATUS_GOOD)
747 ERR("Unable to read number of options\n");
748 return FALSE;
751 for ( i = 1; i < optcount; i++)
753 const SANE_Option_Descriptor *opt;
755 control = GetDlgItem(hwnd,i+ID_BASE);
757 if (!control)
758 continue;
760 opt = psane_get_option_descriptor(activeDS.deviceHandle, i);
762 TRACE("%i %s %i %i\n",i,opt->title,opt->type,opt->constraint_type);
764 if (!SANE_OPTION_IS_ACTIVE(opt->cap))
765 EnableWindow(control,FALSE);
766 else
767 EnableWindow(control,TRUE);
769 SendMessageA(control,CB_RESETCONTENT,0,0);
770 /* initialize values */
771 if (opt->type == SANE_TYPE_STRING && opt->constraint_type !=
772 SANE_CONSTRAINT_NONE)
774 int j = 0;
775 CHAR buffer[255];
776 while (opt->constraint.string_list[j]!=NULL)
778 SendMessageA(control,CB_ADDSTRING,0,
779 (LPARAM)opt->constraint.string_list[j]);
780 j++;
782 psane_control_option(activeDS.deviceHandle, i, SANE_ACTION_GET_VALUE, buffer,NULL);
783 SendMessageA(control,CB_SELECTSTRING,0,(LPARAM)buffer);
785 else if (opt->type == SANE_TYPE_BOOL)
787 SANE_Bool b;
788 psane_control_option(activeDS.deviceHandle, i,
789 SANE_ACTION_GET_VALUE, &b, NULL);
790 if (b)
791 SendMessageA(control,BM_SETCHECK,BST_CHECKED,0);
794 else if (opt->constraint_type == SANE_CONSTRAINT_RANGE)
796 if (opt->type == SANE_TYPE_INT)
798 SANE_Int si;
799 int min,max;
801 min = (SANE_Int)opt->constraint.range->min /
802 (((SANE_Int)opt->constraint.range->quant)?
803 (SANE_Int)opt->constraint.range->quant:1);
805 max = (SANE_Int)opt->constraint.range->max /
806 (((SANE_Int)opt->constraint.range->quant)
807 ?(SANE_Int)opt->constraint.range->quant:1);
809 SendMessageA(control,SBM_SETRANGE,min,max);
811 psane_control_option(activeDS.deviceHandle, i,
812 SANE_ACTION_GET_VALUE, &si,NULL);
813 if (opt->constraint.range->quant)
814 si = si / opt->constraint.range->quant;
816 SendMessageW(control,SBM_SETPOS, si, TRUE);
817 UpdateRelevantEdit(hwnd, opt, i, si);
819 else if (opt->type == SANE_TYPE_FIXED)
821 SANE_Fixed *sf;
823 double dd;
824 double s_min,s_max,s_quant;
825 INT pos;
826 int min,max;
828 s_min = SANE_UNFIX(opt->constraint.range->min);
829 s_max = SANE_UNFIX(opt->constraint.range->max);
830 s_quant = SANE_UNFIX(opt->constraint.range->quant);
832 if (s_quant)
834 min = (s_min / s_quant);
835 max = (s_max / s_quant);
837 else
839 min = s_min / 0.01;
840 max = s_max / 0.01;
843 SendMessageA(control,SBM_SETRANGE,min,max);
846 sf = HeapAlloc(GetProcessHeap(),0,opt->size*sizeof(SANE_Word));
847 psane_control_option(activeDS.deviceHandle, i,
848 SANE_ACTION_GET_VALUE, sf,NULL);
850 dd = SANE_UNFIX(*sf);
851 HeapFree(GetProcessHeap(),0,sf);
853 /* Note that conversion of float -> SANE_Fixed is lossy;
854 * and when you truncate it into an integer, you can get
855 * unfortunate results. This calculation attempts
856 * to mitigate that harm */
857 if (s_quant)
858 pos = ((dd + (s_quant/2.0)) / s_quant);
859 else
860 pos = (dd + 0.005) / 0.01;
862 SendMessageW(control, SBM_SETPOS, pos, TRUE);
864 UpdateRelevantEdit(hwnd, opt, i, pos);
869 return TRUE;
872 static INT_PTR ProcessScroll(HWND hwnd, WPARAM wParam, LPARAM lParam)
874 int index;
875 const SANE_Option_Descriptor *opt;
876 WORD scroll;
877 DWORD position;
879 index = GetDlgCtrlID((HWND)lParam)- ID_BASE;
880 if (index < 0)
881 return FALSE;
883 opt = psane_get_option_descriptor(activeDS.deviceHandle, index);
885 if (!opt)
886 return FALSE;
888 scroll = LOWORD(wParam);
890 switch (scroll)
892 case SB_THUMBTRACK:
893 case SB_THUMBPOSITION:
895 SCROLLINFO si;
896 si.cbSize = sizeof(SCROLLINFO);
897 si.fMask = SIF_TRACKPOS;
898 GetScrollInfo((HWND)lParam,SB_CTL, &si);
899 position = si.nTrackPos;
901 break;
902 case SB_LEFT:
903 case SB_LINELEFT:
904 case SB_PAGELEFT:
905 position = SendMessageW((HWND)lParam,SBM_GETPOS,0,0);
906 position--;
907 break;
908 case SB_RIGHT:
909 case SB_LINERIGHT:
910 case SB_PAGERIGHT:
911 position = SendMessageW((HWND)lParam,SBM_GETPOS,0,0);
912 position++;
913 break;
914 default:
915 position = SendMessageW((HWND)lParam,SBM_GETPOS,0,0);
918 SendMessageW((HWND)lParam,SBM_SETPOS, position, TRUE);
919 position = SendMessageW((HWND)lParam,SBM_GETPOS,0,0);
921 UpdateRelevantEdit(hwnd, opt, index, position);
922 if (UpdateSaneScrollOption(opt, index, position))
923 InitializeDialog(hwnd);
925 return TRUE;
929 static void ButtonClicked(HWND hwnd, INT id, HWND control)
931 int index;
932 const SANE_Option_Descriptor *opt;
934 index = id - ID_BASE;
935 if (index < 0)
936 return;
938 opt = psane_get_option_descriptor(activeDS.deviceHandle, index);
940 if (!opt)
941 return;
943 if (opt->type == SANE_TYPE_BOOL)
945 BOOL r = SendMessageW(control,BM_GETCHECK,0,0)==BST_CHECKED;
946 if (UpdateSaneBoolOption(index, r))
947 InitializeDialog(hwnd);
951 static void ComboChanged(HWND hwnd, INT id, HWND control)
953 int index;
954 int selection;
955 int len;
956 const SANE_Option_Descriptor *opt;
957 SANE_String value;
959 index = id - ID_BASE;
960 if (index < 0)
961 return;
963 opt = psane_get_option_descriptor(activeDS.deviceHandle, index);
965 if (!opt)
966 return;
968 selection = SendMessageW(control,CB_GETCURSEL,0,0);
969 len = SendMessageW(control,CB_GETLBTEXTLEN,selection,0);
971 len++;
972 value = HeapAlloc(GetProcessHeap(),0,len);
973 SendMessageA(control,CB_GETLBTEXT,selection,(LPARAM)value);
975 if (opt->type == SANE_TYPE_STRING)
977 if (UpdateSaneStringOption(index, value))
978 InitializeDialog(hwnd);
983 static INT_PTR CALLBACK DialogProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
985 switch (msg)
987 case WM_INITDIALOG:
988 return InitializeDialog(hwndDlg);
989 case WM_HSCROLL:
990 return ProcessScroll(hwndDlg, wParam, lParam);
991 case WM_NOTIFY:
993 LPPSHNOTIFY psn = (LPPSHNOTIFY)lParam;
994 switch (((NMHDR*)lParam)->code)
996 case PSN_APPLY:
997 if (psn->lParam)
999 activeDS.currentState = 6;
1000 if (activeDS.windowMessage)
1001 PostMessageA(activeDS.hwndOwner, activeDS.windowMessage, MSG_XFERREADY, 0);
1003 break;
1004 case PSN_QUERYCANCEL:
1005 if (activeDS.windowMessage)
1006 PostMessageA(activeDS.hwndOwner, activeDS.windowMessage, MSG_CLOSEDSREQ, 0);
1007 break;
1008 case PSN_SETACTIVE:
1009 InitializeDialog(hwndDlg);
1010 break;
1012 break;
1014 case WM_COMMAND:
1015 switch (HIWORD(wParam))
1017 case BN_CLICKED:
1018 ButtonClicked(hwndDlg,LOWORD(wParam), (HWND)lParam);
1019 break;
1020 case CBN_SELCHANGE:
1021 ComboChanged(hwndDlg,LOWORD(wParam), (HWND)lParam);
1025 return FALSE;
1028 static int CALLBACK PropSheetProc(HWND hwnd, UINT msg, LPARAM lParam)
1030 if (msg == PSCB_INITIALIZED)
1032 /* rename OK button to Scan */
1033 HWND scan = GetDlgItem(hwnd,IDOK);
1034 SetWindowTextA(scan,"Scan");
1036 return TRUE;
1040 static INT_PTR CALLBACK ScanningProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
1042 return FALSE;
1045 HWND ScanningDialogBox(HWND dialog, LONG progress)
1047 if (!dialog)
1048 dialog = CreateDialogW(SANE_instance,
1049 (LPWSTR)MAKEINTRESOURCE(IDD_DIALOG1), NULL, ScanningProc);
1051 if (progress == -1)
1053 EndDialog(dialog,0);
1054 return NULL;
1057 RedrawWindow(dialog,NULL,NULL,
1058 RDW_INTERNALPAINT|RDW_UPDATENOW|RDW_ALLCHILDREN);
1060 return dialog;
1063 #else /* SONAME_LIBSANE */
1065 BOOL DoScannerUI(void)
1067 return FALSE;
1070 #endif /* SONAME_LIBSANE */