widl: Get rid of PowerPC support.
[wine.git] / dlls / sane.ds / ui.c
blob8be7ed42b70521fc6681ec7fd8e1b3b4cba60c2f
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 <stdlib.h>
22 #include <stdarg.h>
23 #include <stdio.h>
25 #define NONAMELESSUNION
27 #include "sane_i.h"
28 #include "winuser.h"
29 #include "winnls.h"
30 #include "wingdi.h"
31 #include "prsht.h"
32 #include "wine/debug.h"
33 #include "resource.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(twain);
37 #define ID_BASE 0x100
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;
48 INT len;
49 SIZE size;
50 WORD *ptr;
51 LONG base;
53 *template_out = NULL;
55 if (!text)
56 return 0;
58 base = GetDialogBaseUnits();
60 len = lstrlenW(text) * sizeof(WCHAR);
61 len += sizeof(DLGITEMTEMPLATE);
62 len += 4*sizeof(WORD);
64 tpl = malloc(len);
65 tpl->style=WS_VISIBLE;
66 tpl->dwExtendedStyle = 0;
67 tpl->x = 4;
68 tpl->y = y;
69 tpl->id = ID_BASE;
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);
76 *ptr++ = 0xffff;
77 *ptr++ = 0x0082;
78 lstrcpyW( ptr, text );
79 ptr += lstrlenW(ptr) + 1;
80 *ptr = 0;
82 *template_out = tpl;
83 return len;
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;
90 INT len;
91 WORD *ptr;
92 SIZE size;
93 LONG base;
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);
103 tpl = malloc(len);
104 tpl->style=WS_VISIBLE|ES_READONLY|WS_BORDER;
105 tpl->dwExtendedStyle = 0;
106 tpl->x = 1;
107 tpl->y = y;
108 tpl->id = id;
110 if (is_int)
111 GetTextExtentPoint32A(hdc,int_base,lstrlenA(int_base),&size);
112 else
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);
119 *ptr++ = 0xffff;
120 *ptr++ = 0x0081;
121 lstrcpyW( ptr, text );
122 ptr += lstrlenW(ptr) + 1;
123 *ptr = 0;
125 *template_out = tpl;
126 return len;
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;
134 WORD class = 0xffff;
135 DWORD styles = WS_VISIBLE;
136 WORD *ptr = NULL;
137 LPDLGITEMTEMPLATEW lead_static = NULL;
138 LPDLGITEMTEMPLATEW trail_edit = NULL;
139 DWORD leading_len = 0;
140 DWORD trail_len = 0;
141 DWORD local_len = 0;
142 const WCHAR *title = NULL;
143 WCHAR buffer[255];
144 int padding = 0;
145 int padding2 = 0;
146 int base_x = 0;
147 LONG base;
148 int ctl_cx = 0;
149 SIZE size;
151 GetTextExtentPoint32A(hdc,"X",1,&size);
152 base = GetDialogBaseUnits();
153 base_x = MulDiv(size.cx,4,LOWORD(base));
155 switch (opt->type)
157 case TYPE_BOOL:
158 class = 0x0080; /* Button */
159 styles |= BS_AUTOCHECKBOX;
160 title = opt->title;
161 break;
162 case TYPE_INT:
164 int i;
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*/
172 styles |= ES_NUMBER;
173 title = buffer;
174 break;
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);
180 break;
181 default:
182 class= 0x0085; /* Combo */
183 ctl_cx = 10 * base_x;
184 styles |= CBS_DROPDOWNLIST;
185 break;
187 leading_len += create_leading_static(hdc, opt->title, &lead_static, y, id+ID_STATIC_BASE);
188 break;
190 case TYPE_FIXED:
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);
197 free( i );
199 switch (opt->constraint_type)
201 case CONSTRAINT_NONE:
202 class = 0x0081; /* Edit */
203 title = buffer;
204 break;
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);
209 break;
210 default:
211 class= 0x0085; /* Combo */
212 ctl_cx = 10 * base_x;
213 styles |= CBS_DROPDOWNLIST;
214 break;
216 leading_len += create_leading_static(hdc, opt->title, &lead_static, y, id+ID_STATIC_BASE);
217 break;
219 case TYPE_STRING:
221 char str[256];
222 switch (opt->constraint_type)
224 case CONSTRAINT_NONE:
225 class = 0x0081; /* Edit*/
226 break;
227 default:
228 class= 0x0085; /* Combo */
229 ctl_cx = opt->size * base_x;
230 styles |= CBS_DROPDOWNLIST;
231 break;
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) );
236 title = buffer;
237 break;
239 case TYPE_BUTTON:
240 class = 0x0080; /* Button */
241 title = opt->title;
242 break;
243 case TYPE_GROUP:
244 class = 0x0080; /* Button */
245 styles |= BS_GROUPBOX;
246 title = opt->title;
247 break;
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);
257 tpl->style=styles;
258 tpl->dwExtendedStyle = 0;
259 if (lead_static)
260 tpl->x = lead_static->x + lead_static->cx + 1;
261 else if (opt->type == TYPE_GROUP)
262 tpl->x = 2;
263 else
264 tpl->x = 4;
265 tpl->y = y;
266 tpl->id = id;
268 if (title)
270 GetTextExtentPoint32W(hdc,title,lstrlenW(title),&size);
271 tpl->cx = size.cx;
272 tpl->cy = size.cy;
274 else
276 if (lead_static)
277 tpl->cy = lead_static->cy;
278 else
279 tpl->cy = 15;
281 if (!ctl_cx)
282 ctl_cx = 15;
284 tpl->cx = ctl_cx;
286 ptr = (WORD *)(tpl + 1);
287 *ptr++ = 0xffff;
288 *ptr++ = class;
289 if (title)
291 lstrcpyW( ptr, title );
292 ptr += lstrlenW(ptr);
294 *ptr++ = 0;
295 *ptr = 0;
297 if (trail_edit)
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);
309 else
310 *cx = tpl->cx + tpl->x;
312 *template_out = rc;
313 if (leading_len)
314 *count = 2;
315 else
316 *count = 1;
318 if (trail_edit)
319 *count+=1;
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)
328 int i;
329 INT y = 2;
330 LPDLGTEMPLATEW tpl = NULL;
331 LPBYTE all_controls = NULL;
332 DWORD control_len = 0;
333 int max_cx = 0;
334 int group_max_cx = 0;
335 LPBYTE ptr;
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;
343 int len;
344 int padding = 0;
345 int x;
346 int count;
347 int hold_for_group = 0;
349 opt.optno = i;
350 if (SANE_CALL( option_get_descriptor, &opt )) continue;
351 if (opt.type == TYPE_GROUP && split_tabs)
353 if (control_len > 0)
355 *from_index = i - 1;
356 goto exit;
358 else
360 *from_index = i;
361 return NULL;
364 if (!opt.is_active)
365 continue;
367 len = create_item(hdc, &opt, ID_BASE + i, &item_tpl, y, &x, &count);
369 control_count += count;
371 if (!len)
373 continue;
376 hold_for_group = y;
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);
383 if (all_controls)
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);
388 free(item_tpl);
390 else
392 if (!padding)
394 all_controls = (LPBYTE)item_tpl;
396 else
398 all_controls = malloc(len + padding);
399 memcpy(all_controls,item_tpl,len);
400 memset(all_controls+len,0xcb,padding);
401 free(item_tpl);
405 if (opt.type == TYPE_GROUP)
407 if (group_offset == -1)
409 group_offset = control_len;
410 group_max_cx = 0;
412 else
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);
421 group->y += 2;
422 y+=2;
423 group_max_cx = 0;
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;
437 y+=2;
440 *from_index = i-1;
441 exit:
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;
448 tpl->x = 0;
449 tpl->y = 0;
450 tpl->cx = max_cx + 10;
451 tpl->cy = y + 10;
452 ptr = (LPBYTE)tpl + sizeof(DLGTEMPLATE);
453 *(LPWORD)ptr = 0x0000;
454 ptr+=sizeof(WORD);
455 *(LPWORD)ptr = 0x0000;
456 ptr+=sizeof(WORD);
457 *(LPWORD)ptr = 0x0000;
458 ptr+=sizeof(WORD);
459 memcpy(ptr,all_controls,control_len);
461 free(all_controls);
463 return tpl;
466 BOOL DoScannerUI(void)
468 HDC hdc;
469 PROPSHEETPAGEW psp[10];
470 int page_count= 0;
471 PROPSHEETHEADERW psh;
472 int index = 1;
473 TW_UINT16 rc;
474 int optcount;
475 UINT psrc;
476 LPWSTR szCaption;
477 DWORD len;
479 hdc = GetDC(0);
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");
486 return FALSE;
489 while (index < optcount)
491 struct option_descriptor opt;
493 psp[page_count].u.pResource = create_options_page(hdc, &index,
494 optcount, TRUE);
495 opt.optno = 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;
510 page_count ++;
513 index ++;
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,
520 szCaption,len);
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;
528 psh.u.pszIcon = 0;
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);
542 free(szCaption);
544 if (psrc == IDOK)
545 return TRUE;
546 else
547 return FALSE;
550 static void UpdateRelevantEdit(HWND hwnd, const struct option_descriptor *opt, int position)
552 WCHAR buffer[244];
553 HWND edit_w;
554 int len;
556 switch (opt->type)
558 case TYPE_INT:
560 INT si;
562 if (opt->constraint.range.quant)
563 si = position * opt->constraint.range.quant;
564 else
565 si = position;
567 len = swprintf( buffer, ARRAY_SIZE(buffer), L"%i", si );
568 break;
570 case TYPE_FIXED:
572 double dd;
574 if (opt->constraint.range.quant)
575 dd = position * (opt->constraint.range.quant / 65536.0);
576 else
577 dd = position * 0.01;
579 len = swprintf( buffer, ARRAY_SIZE(buffer), L"%f", dd );
580 break;
582 default:
583 return;
586 buffer[len++] = ' ';
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)
596 BOOL result = FALSE;
597 int si;
599 switch (opt->type)
601 case TYPE_INT:
603 if (opt->constraint.range.quant)
604 si = position * opt->constraint.range.quant;
605 else
606 si = position;
608 sane_option_set_value( opt->optno, &si, &result );
609 break;
611 case TYPE_FIXED:
612 if (opt->constraint.range.quant)
613 si = position * opt->constraint.range.quant;
614 else
615 si = MulDiv( position, 65536, 100 );
617 sane_option_set_value( opt->optno, &si, &result );
618 break;
619 default:
620 break;
623 return result;
626 static INT_PTR InitializeDialog(HWND hwnd)
628 TW_UINT16 rc;
629 int optcount;
630 HWND control;
631 int i;
633 rc = sane_option_get_value( 0, &optcount );
634 if (rc != TWCC_SUCCESS)
636 ERR("Unable to read number of options\n");
637 return FALSE;
640 for ( i = 1; i < optcount; i++)
642 struct option_descriptor opt;
644 control = GetDlgItem(hwnd,i+ID_BASE);
646 if (!control)
647 continue;
649 opt.optno = i;
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)
659 CHAR buffer[255];
660 WCHAR *p;
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)
669 BOOL b;
670 sane_option_get_value( i, &b );
671 if (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];
678 CHAR buffer[16];
679 int val;
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)
693 int si;
694 int min,max;
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;
720 else
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;
738 else
739 pos = MulDiv( *sf, 100, 65536 );
741 free(sf);
742 SendMessageW(control, SBM_SETPOS, pos, TRUE);
743 UpdateRelevantEdit(hwnd, &opt, pos);
748 return TRUE;
751 static INT_PTR ProcessScroll(HWND hwnd, WPARAM wParam, LPARAM lParam)
753 struct option_descriptor opt;
754 WORD scroll;
755 DWORD position;
757 opt.optno = GetDlgCtrlID((HWND)lParam)- ID_BASE;
758 if (opt.optno < 0)
759 return FALSE;
761 if (SANE_CALL( option_get_descriptor, &opt )) return FALSE;
763 scroll = LOWORD(wParam);
765 switch (scroll)
767 case SB_THUMBTRACK:
768 case SB_THUMBPOSITION:
770 SCROLLINFO si;
771 si.cbSize = sizeof(SCROLLINFO);
772 si.fMask = SIF_TRACKPOS;
773 GetScrollInfo((HWND)lParam,SB_CTL, &si);
774 position = si.nTrackPos;
776 break;
777 case SB_LEFT:
778 case SB_LINELEFT:
779 case SB_PAGELEFT:
780 position = SendMessageW((HWND)lParam,SBM_GETPOS,0,0);
781 position--;
782 break;
783 case SB_RIGHT:
784 case SB_LINERIGHT:
785 case SB_PAGERIGHT:
786 position = SendMessageW((HWND)lParam,SBM_GETPOS,0,0);
787 position++;
788 break;
789 default:
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);
800 return TRUE;
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;
810 if (opt.optno < 0)
811 return;
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)
825 int selection;
826 int len;
827 struct option_descriptor opt;
828 char *value;
829 BOOL changed = FALSE;
831 opt.optno = id - ID_BASE;
832 if (opt.optno < 0)
833 return;
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);
840 len++;
841 value = malloc(len);
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);
854 free( value );
858 static INT_PTR CALLBACK DialogProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
860 switch (msg)
862 case WM_INITDIALOG:
863 return InitializeDialog(hwndDlg);
864 case WM_HSCROLL:
865 return ProcessScroll(hwndDlg, wParam, lParam);
866 case WM_NOTIFY:
868 LPPSHNOTIFY psn = (LPPSHNOTIFY)lParam;
869 switch (((NMHDR*)lParam)->code)
871 case PSN_APPLY:
872 if (psn->lParam)
874 activeDS.currentState = 6;
875 SANE_Notify(MSG_XFERREADY);
877 break;
878 case PSN_QUERYCANCEL:
879 SANE_Notify(MSG_CLOSEDSREQ);
880 break;
881 case PSN_SETACTIVE:
882 InitializeDialog(hwndDlg);
883 break;
885 break;
887 case WM_COMMAND:
888 switch (HIWORD(wParam))
890 case BN_CLICKED:
891 ButtonClicked(hwndDlg,LOWORD(wParam), (HWND)lParam);
892 break;
893 case CBN_SELCHANGE:
894 ComboChanged(hwndDlg,LOWORD(wParam), (HWND)lParam);
898 return FALSE;
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");
909 return TRUE;
913 static INT_PTR CALLBACK ScanningProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
915 return FALSE;
918 HWND ScanningDialogBox(HWND dialog, LONG progress)
920 if (!dialog)
921 dialog = CreateDialogW(SANE_instance,
922 (LPWSTR)MAKEINTRESOURCE(IDD_DIALOG1), NULL, ScanningProc);
924 if (progress == -1)
926 EndDialog(dialog,0);
927 return NULL;
930 RedrawWindow(dialog,NULL,NULL,
931 RDW_INTERNALPAINT|RDW_UPDATENOW|RDW_ALLCHILDREN);
933 return dialog;