Sync to Wine-0_9_5:
[reactos.git] / reactos / lib / msi / dialog.c
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
3 *
4 * Copyright 2005 Mike McCormack for CodeWeavers
5 *
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.
10 *
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.
15 *
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #define COBJMACROS
22 #define NONAMELESSUNION
23 #define NONAMELESSSTRUCT
24
25 #include <stdarg.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winuser.h"
30 #include "winnls.h"
31 #include "wingdi.h"
32 #include "msi.h"
33 #include "msipriv.h"
34 #include "msidefs.h"
35 #include "ocidl.h"
36 #include "olectl.h"
37 #include "richedit.h"
38 #include "commctrl.h"
39
40 #include "wine/debug.h"
41 #include "wine/unicode.h"
42
43 #include "action.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(msi);
46
47
48 struct msi_control_tag;
49 typedef struct msi_control_tag msi_control;
50 typedef UINT (*msi_handler)( msi_dialog *, msi_control *, WPARAM );
51
52 struct msi_control_tag
53 {
54 struct list entry;
55 HWND hwnd;
56 msi_handler handler;
57 LPWSTR property;
58 LPWSTR value;
59 HBITMAP hBitmap;
60 HICON hIcon;
61 LPWSTR tabnext;
62 WCHAR name[1];
63 };
64
65 typedef struct msi_font_tag
66 {
67 struct msi_font_tag *next;
68 HFONT hfont;
69 WCHAR name[1];
70 } msi_font;
71
72 struct msi_dialog_tag
73 {
74 MSIPACKAGE *package;
75 msi_dialog_event_handler event_handler;
76 BOOL finished;
77 INT scale;
78 DWORD attributes;
79 HWND hwnd;
80 LPWSTR default_font;
81 msi_font *font_list;
82 struct list controls;
83 HWND hWndFocus;
84 LPWSTR control_default;
85 LPWSTR control_cancel;
86 WCHAR name[1];
87 };
88
89 typedef UINT (*msi_dialog_control_func)( msi_dialog *dialog, MSIRECORD *rec );
90 struct control_handler
91 {
92 LPCWSTR control_type;
93 msi_dialog_control_func func;
94 };
95
96 typedef struct
97 {
98 msi_dialog* dialog;
99 msi_control *parent;
100 DWORD attributes;
101 } radio_button_group_descr;
102
103 const WCHAR szMsiDialogClass[] = {
104 'M','s','i','D','i','a','l','o','g','C','l','o','s','e','C','l','a','s','s',0
105 };
106 const WCHAR szMsiHiddenWindow[] = {
107 'M','s','i','H','i','d','d','e','n','W','i','n','d','o','w',0 };
108 static const WCHAR szStatic[] = { 'S','t','a','t','i','c',0 };
109 static const WCHAR szButton[] = { 'B','U','T','T','O','N', 0 };
110 static const WCHAR szButtonData[] = { 'M','S','I','D','A','T','A',0 };
111 static const WCHAR szText[] = { 'T','e','x','t',0 };
112 static const WCHAR szPushButton[] = { 'P','u','s','h','B','u','t','t','o','n',0 };
113 static const WCHAR szLine[] = { 'L','i','n','e',0 };
114 static const WCHAR szBitmap[] = { 'B','i','t','m','a','p',0 };
115 static const WCHAR szCheckBox[] = { 'C','h','e','c','k','B','o','x',0 };
116 static const WCHAR szScrollableText[] = {
117 'S','c','r','o','l','l','a','b','l','e','T','e','x','t',0 };
118 static const WCHAR szComboBox[] = { 'C','o','m','b','o','B','o','x',0 };
119 static const WCHAR szEdit[] = { 'E','d','i','t',0 };
120 static const WCHAR szMaskedEdit[] = { 'M','a','s','k','e','d','E','d','i','t',0 };
121 static const WCHAR szPathEdit[] = { 'P','a','t','h','E','d','i','t',0 };
122 static const WCHAR szProgressBar[] = {
123 'P','r','o','g','r','e','s','s','B','a','r',0 };
124 static const WCHAR szRadioButtonGroup[] = {
125 'R','a','d','i','o','B','u','t','t','o','n','G','r','o','u','p',0 };
126 static const WCHAR szIcon[] = { 'I','c','o','n',0 };
127 static const WCHAR szSelectionTree[] = {
128 'S','e','l','e','c','t','i','o','n','T','r','e','e',0 };
129
130 static UINT msi_dialog_checkbox_handler( msi_dialog *, msi_control *, WPARAM );
131 static void msi_dialog_checkbox_sync_state( msi_dialog *, msi_control * );
132 static UINT msi_dialog_button_handler( msi_dialog *, msi_control *, WPARAM );
133 static UINT msi_dialog_edit_handler( msi_dialog *, msi_control *, WPARAM );
134 static UINT msi_dialog_radiogroup_handler( msi_dialog *, msi_control *, WPARAM param );
135 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog );
136 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
137
138
139 /* dialog sequencing */
140
141 #define WM_MSI_DIALOG_CREATE (WM_USER+0x100)
142 #define WM_MSI_DIALOG_DESTROY (WM_USER+0x101)
143
144 static DWORD uiThreadId;
145 static HWND hMsiHiddenWindow;
146
147 static INT msi_dialog_scale_unit( msi_dialog *dialog, INT val )
148 {
149 return (dialog->scale * val + 5) / 10;
150 }
151
152 static msi_control *msi_dialog_find_control( msi_dialog *dialog, LPCWSTR name )
153 {
154 msi_control *control;
155
156 if( !name )
157 return NULL;
158 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
159 if( !strcmpW( control->name, name ) ) /* FIXME: case sensitive? */
160 return control;
161 return NULL;
162 }
163
164 static msi_control *msi_dialog_find_control_by_hwnd( msi_dialog *dialog, HWND hwnd )
165 {
166 msi_control *control;
167
168 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
169 if( hwnd == control->hwnd )
170 return control;
171 return NULL;
172 }
173
174 static LPWSTR msi_get_deformatted_field( MSIPACKAGE *package, MSIRECORD *rec, int field )
175 {
176 LPCWSTR str = MSI_RecordGetString( rec, field );
177 LPWSTR ret = NULL;
178
179 if (str)
180 deformat_string( package, str, &ret );
181 return ret;
182 }
183
184 /*
185 * msi_dialog_get_style
186 *
187 * Extract the {\style} string from the front of the text to display and
188 * update the pointer.
189 */
190 static LPWSTR msi_dialog_get_style( LPCWSTR p, LPCWSTR *rest )
191 {
192 LPWSTR ret = NULL;
193 LPCWSTR q, i;
194 DWORD len;
195
196 *rest = p;
197 if( !p )
198 return ret;
199 if( *p++ != '{' )
200 return ret;
201 q = strchrW( p, '}' );
202 if( !q )
203 return ret;
204 if( *p == '\\' || *p == '&' )
205 p++;
206
207 /* little bit of sanity checking to stop us getting confused with RTF */
208 for( i=p; i<q; i++ )
209 if( *i == '}' || *i == '\\' )
210 return ret;
211
212 *rest = ++q;
213 len = q - p;
214
215 ret = msi_alloc( len*sizeof(WCHAR) );
216 if( !ret )
217 return ret;
218 memcpy( ret, p, len*sizeof(WCHAR) );
219 ret[len-1] = 0;
220 return ret;
221 }
222
223 static UINT msi_dialog_add_font( MSIRECORD *rec, LPVOID param )
224 {
225 msi_dialog *dialog = param;
226 msi_font *font;
227 LPCWSTR face, name;
228 LOGFONTW lf;
229 INT style;
230 HDC hdc;
231
232 /* create a font and add it to the list */
233 name = MSI_RecordGetString( rec, 1 );
234 font = msi_alloc( sizeof *font + strlenW( name )*sizeof (WCHAR) );
235 strcpyW( font->name, name );
236 font->next = dialog->font_list;
237 dialog->font_list = font;
238
239 memset( &lf, 0, sizeof lf );
240 face = MSI_RecordGetString( rec, 2 );
241 lf.lfHeight = MSI_RecordGetInteger( rec, 3 );
242 style = MSI_RecordGetInteger( rec, 5 );
243 if( style & msidbTextStyleStyleBitsBold )
244 lf.lfWeight = FW_BOLD;
245 if( style & msidbTextStyleStyleBitsItalic )
246 lf.lfItalic = TRUE;
247 if( style & msidbTextStyleStyleBitsUnderline )
248 lf.lfUnderline = TRUE;
249 if( style & msidbTextStyleStyleBitsStrike )
250 lf.lfStrikeOut = TRUE;
251 lstrcpynW( lf.lfFaceName, face, LF_FACESIZE );
252
253 /* adjust the height */
254 hdc = GetDC( dialog->hwnd );
255 if (hdc)
256 {
257 lf.lfHeight = -MulDiv(lf.lfHeight, GetDeviceCaps(hdc, LOGPIXELSY), 72);
258 ReleaseDC( dialog->hwnd, hdc );
259 }
260
261 font->hfont = CreateFontIndirectW( &lf );
262
263 TRACE("Adding font style %s\n", debugstr_w(font->name) );
264
265 return ERROR_SUCCESS;
266 }
267
268 static msi_font *msi_dialog_find_font( msi_dialog *dialog, LPCWSTR name )
269 {
270 msi_font *font;
271
272 for( font = dialog->font_list; font; font = font->next )
273 if( !strcmpW( font->name, name ) ) /* FIXME: case sensitive? */
274 break;
275
276 return font;
277 }
278
279 static UINT msi_dialog_set_font( msi_dialog *dialog, HWND hwnd, LPCWSTR name )
280 {
281 msi_font *font;
282
283 font = msi_dialog_find_font( dialog, name );
284 if( font )
285 SendMessageW( hwnd, WM_SETFONT, (WPARAM) font->hfont, TRUE );
286 else
287 ERR("No font entry for %s\n", debugstr_w(name));
288 return ERROR_SUCCESS;
289 }
290
291 static UINT msi_dialog_build_font_list( msi_dialog *dialog )
292 {
293 static const WCHAR query[] = {
294 'S','E','L','E','C','T',' ','*',' ',
295 'F','R','O','M',' ','`','T','e','x','t','S','t','y','l','e','`',' ',0
296 };
297 UINT r;
298 MSIQUERY *view = NULL;
299
300 TRACE("dialog %p\n", dialog );
301
302 r = MSI_OpenQuery( dialog->package->db, &view, query );
303 if( r != ERROR_SUCCESS )
304 return r;
305
306 r = MSI_IterateRecords( view, NULL, msi_dialog_add_font, dialog );
307 msiobj_release( &view->hdr );
308
309 return r;
310 }
311
312 static msi_control *msi_dialog_create_window( msi_dialog *dialog,
313 MSIRECORD *rec, LPCWSTR szCls, LPCWSTR name, LPCWSTR text,
314 DWORD style, HWND parent )
315 {
316 DWORD x, y, width, height;
317 LPWSTR font = NULL, title_font = NULL;
318 LPCWSTR title = NULL;
319 msi_control *control;
320
321 style |= WS_CHILD;
322
323 control = msi_alloc( sizeof *control + strlenW(name)*sizeof(WCHAR) );
324 strcpyW( control->name, name );
325 list_add_head( &dialog->controls, &control->entry );
326 control->handler = NULL;
327 control->property = NULL;
328 control->value = NULL;
329 control->hBitmap = NULL;
330 control->hIcon = NULL;
331 control->tabnext = strdupW( MSI_RecordGetString( rec, 11) );
332
333 x = MSI_RecordGetInteger( rec, 4 );
334 y = MSI_RecordGetInteger( rec, 5 );
335 width = MSI_RecordGetInteger( rec, 6 );
336 height = MSI_RecordGetInteger( rec, 7 );
337
338 x = msi_dialog_scale_unit( dialog, x );
339 y = msi_dialog_scale_unit( dialog, y );
340 width = msi_dialog_scale_unit( dialog, width );
341 height = msi_dialog_scale_unit( dialog, height );
342
343 if( text )
344 {
345 deformat_string( dialog->package, text, &title_font );
346 font = msi_dialog_get_style( title_font, &title );
347 }
348
349 control->hwnd = CreateWindowW( szCls, title, style,
350 x, y, width, height, parent, NULL, NULL, NULL );
351
352 TRACE("Dialog %s control %s hwnd %p\n",
353 debugstr_w(dialog->name), debugstr_w(text), control->hwnd );
354
355 msi_dialog_set_font( dialog, control->hwnd,
356 font ? font : dialog->default_font );
357
358 msi_free( title_font );
359 msi_free( font );
360
361 return control;
362 }
363
364 static MSIRECORD *msi_get_binary_record( MSIDATABASE *db, LPCWSTR name )
365 {
366 static const WCHAR query[] = {
367 's','e','l','e','c','t',' ','*',' ',
368 'f','r','o','m',' ','B','i','n','a','r','y',' ',
369 'w','h','e','r','e',' ',
370 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0
371 };
372
373 return MSI_QueryGetRecord( db, query, name );
374 }
375
376 static LPWSTR msi_create_tmp_path(void)
377 {
378 WCHAR tmp[MAX_PATH];
379 LPWSTR path = NULL;
380 static const WCHAR prefix[] = { 'm','s','i',0 };
381 DWORD len, r;
382
383 r = GetTempPathW( MAX_PATH, tmp );
384 if( !r )
385 return path;
386 len = lstrlenW( tmp ) + 20;
387 path = msi_alloc( len * sizeof (WCHAR) );
388 if( path )
389 {
390 r = GetTempFileNameW( tmp, prefix, 0, path );
391 if (!r)
392 {
393 msi_free( path );
394 path = NULL;
395 }
396 }
397 return path;
398 }
399
400
401 static HANDLE msi_load_image( MSIDATABASE *db, LPCWSTR name, UINT type,
402 UINT cx, UINT cy, UINT flags )
403 {
404 MSIRECORD *rec = NULL;
405 HANDLE himage = NULL;
406 LPWSTR tmp;
407 UINT r;
408
409 TRACE("%p %s %u %u %08x\n", db, debugstr_w(name), cx, cy, flags);
410
411 tmp = msi_create_tmp_path();
412 if( !tmp )
413 return himage;
414
415 rec = msi_get_binary_record( db, name );
416 if( rec )
417 {
418 r = MSI_RecordStreamToFile( rec, 2, tmp );
419 if( r == ERROR_SUCCESS )
420 {
421 himage = LoadImageW( 0, tmp, type, cx, cy, flags );
422 DeleteFileW( tmp );
423 }
424 msiobj_release( &rec->hdr );
425 }
426
427 msi_free( tmp );
428 return himage;
429 }
430
431 static HICON msi_load_icon( MSIDATABASE *db, LPCWSTR text, UINT attributes )
432 {
433 DWORD cx = 0, cy = 0, flags;
434
435 flags = LR_LOADFROMFILE | LR_DEFAULTSIZE;
436 if( attributes & msidbControlAttributesFixedSize )
437 {
438 flags &= ~LR_DEFAULTSIZE;
439 if( attributes & msidbControlAttributesIconSize16 )
440 {
441 cx += 16;
442 cy += 16;
443 }
444 if( attributes & msidbControlAttributesIconSize32 )
445 {
446 cx += 32;
447 cy += 32;
448 }
449 /* msidbControlAttributesIconSize48 handled by above logic */
450 }
451 return msi_load_image( db, text, IMAGE_ICON, cx, cy, flags );
452 }
453
454
455 /* called from the Control Event subscription code */
456 void msi_dialog_handle_event( msi_dialog* dialog, LPCWSTR control,
457 LPCWSTR attribute, MSIRECORD *rec )
458 {
459 msi_control* ctrl;
460 LPCWSTR text;
461
462 ctrl = msi_dialog_find_control( dialog, control );
463 if (!ctrl)
464 return;
465 if( lstrcmpW(attribute, szText) )
466 {
467 ERR("Attribute %s\n", debugstr_w(attribute));
468 return;
469 }
470 text = MSI_RecordGetString( rec , 1 );
471 SetWindowTextW( ctrl->hwnd, text );
472 msi_dialog_check_messages( NULL );
473 }
474
475 static void msi_dialog_map_events(msi_dialog* dialog, LPCWSTR control)
476 {
477 static WCHAR Query[] = {
478 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
479 '`','E','v','e','n','t','M','a','p','p','i','n','g','`',' ',
480 'W','H','E','R','E',' ',
481 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
482 'A','N','D',' ',
483 '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',0
484 };
485 MSIRECORD *row;
486 LPCWSTR event, attribute;
487
488 row = MSI_QueryGetRecord( dialog->package->db, Query, dialog->name, control );
489 if (!row)
490 return;
491
492 event = MSI_RecordGetString( row, 3 );
493 attribute = MSI_RecordGetString( row, 4 );
494 ControlEvent_SubscribeToEvent( dialog->package, event, control, attribute );
495 msiobj_release( &row->hdr );
496 }
497
498 /* everything except radio buttons */
499 static msi_control *msi_dialog_add_control( msi_dialog *dialog,
500 MSIRECORD *rec, LPCWSTR szCls, DWORD style )
501 {
502 DWORD attributes;
503 LPCWSTR text, name;
504
505 name = MSI_RecordGetString( rec, 2 );
506 attributes = MSI_RecordGetInteger( rec, 8 );
507 text = MSI_RecordGetString( rec, 10 );
508 if( attributes & msidbControlAttributesVisible )
509 style |= WS_VISIBLE;
510 if( ~attributes & msidbControlAttributesEnabled )
511 style |= WS_DISABLED;
512
513 msi_dialog_map_events(dialog, name);
514
515 return msi_dialog_create_window( dialog, rec, szCls, name, text,
516 style, dialog->hwnd );
517 }
518
519 struct msi_text_info
520 {
521 WNDPROC oldproc;
522 DWORD attributes;
523 };
524
525 /*
526 * we don't erase our own background,
527 * so we have to make sure that the parent window redraws first
528 */
529 static void msi_text_on_settext( HWND hWnd )
530 {
531 HWND hParent;
532 RECT rc;
533
534 hParent = GetParent( hWnd );
535 GetWindowRect( hWnd, &rc );
536 MapWindowPoints( NULL, hParent, (LPPOINT) &rc, 2 );
537 InvalidateRect( hParent, &rc, TRUE );
538 }
539
540 static LRESULT WINAPI
541 MSIText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
542 {
543 struct msi_text_info *info;
544 LRESULT r = 0;
545
546 TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
547
548 info = GetPropW(hWnd, szButtonData);
549
550 if( msg == WM_CTLCOLORSTATIC &&
551 ( info->attributes & msidbControlAttributesTransparent ) )
552 {
553 SetBkMode( (HDC)wParam, TRANSPARENT );
554 return (LRESULT) GetStockObject(NULL_BRUSH);
555 }
556
557 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
558
559 switch( msg )
560 {
561 case WM_SETTEXT:
562 msi_text_on_settext( hWnd );
563 break;
564 case WM_NCDESTROY:
565 msi_free( info );
566 RemovePropW( hWnd, szButtonData );
567 break;
568 }
569
570 return r;
571 }
572
573 static UINT msi_dialog_text_control( msi_dialog *dialog, MSIRECORD *rec )
574 {
575 msi_control *control;
576 struct msi_text_info *info;
577
578 TRACE("%p %p\n", dialog, rec);
579
580 control = msi_dialog_add_control( dialog, rec, szStatic, SS_LEFT | WS_GROUP );
581 if( !control )
582 return ERROR_FUNCTION_FAILED;
583
584 info = msi_alloc( sizeof *info );
585 if( !info )
586 return ERROR_SUCCESS;
587
588 info->attributes = MSI_RecordGetInteger( rec, 8 );
589 if( info->attributes & msidbControlAttributesTransparent )
590 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT );
591
592 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
593 (LONG_PTR)MSIText_WndProc );
594 SetPropW( control->hwnd, szButtonData, info );
595
596 return ERROR_SUCCESS;
597 }
598
599 static UINT msi_dialog_button_control( msi_dialog *dialog, MSIRECORD *rec )
600 {
601 msi_control *control;
602 UINT attributes, style;
603 LPWSTR text;
604
605 TRACE("%p %p\n", dialog, rec);
606
607 style = WS_TABSTOP;
608 attributes = MSI_RecordGetInteger( rec, 8 );
609 if( attributes & msidbControlAttributesIcon )
610 style |= BS_ICON;
611
612 control = msi_dialog_add_control( dialog, rec, szButton, style );
613 if( !control )
614 return ERROR_FUNCTION_FAILED;
615
616 control->handler = msi_dialog_button_handler;
617
618 /* set the icon */
619 text = msi_get_deformatted_field( dialog->package, rec, 10 );
620 control->hIcon = msi_load_icon( dialog->package->db, text, attributes );
621 if( attributes & msidbControlAttributesIcon )
622 SendMessageW( control->hwnd, BM_SETIMAGE, IMAGE_ICON, (LPARAM) control->hIcon );
623 msi_free( text );
624
625 return ERROR_SUCCESS;
626 }
627
628 static LPWSTR msi_get_checkbox_value( msi_dialog *dialog, LPCWSTR prop )
629 {
630 static const WCHAR query[] = {
631 'S','E','L','E','C','T',' ','*',' ',
632 'F','R','O','M',' ','`','C','h','e','c','k','B','o','x',' ','`',
633 'W','H','E','R','E',' ',
634 '`','P','r','o','p','e','r','t','y','`',' ','=',' ',
635 '\'','%','s','\'',0
636 };
637 MSIRECORD *rec = NULL;
638 LPWSTR ret = NULL;
639
640 /* find if there is a value associated with the checkbox */
641 rec = MSI_QueryGetRecord( dialog->package->db, query, prop );
642 if (!rec)
643 return ret;
644
645 ret = msi_get_deformatted_field( dialog->package, rec, 2 );
646 if( ret && !ret[0] )
647 {
648 msi_free( ret );
649 ret = NULL;
650 }
651 msiobj_release( &rec->hdr );
652 if (ret)
653 return ret;
654
655 ret = msi_dup_property( dialog->package, prop );
656 if( ret && !ret[0] )
657 {
658 msi_free( ret );
659 ret = NULL;
660 }
661
662 return ret;
663 }
664
665 static UINT msi_dialog_checkbox_control( msi_dialog *dialog, MSIRECORD *rec )
666 {
667 msi_control *control;
668 LPCWSTR prop;
669
670 TRACE("%p %p\n", dialog, rec);
671
672 control = msi_dialog_add_control( dialog, rec, szButton,
673 BS_CHECKBOX | BS_MULTILINE | WS_TABSTOP );
674 control->handler = msi_dialog_checkbox_handler;
675 prop = MSI_RecordGetString( rec, 9 );
676 if( prop )
677 {
678 control->property = strdupW( prop );
679 control->value = msi_get_checkbox_value( dialog, prop );
680 TRACE("control %s value %s\n", debugstr_w(control->property),
681 debugstr_w(control->value));
682 }
683 msi_dialog_checkbox_sync_state( dialog, control );
684
685 return ERROR_SUCCESS;
686 }
687
688 static UINT msi_dialog_line_control( msi_dialog *dialog, MSIRECORD *rec )
689 {
690 TRACE("%p %p\n", dialog, rec);
691
692 msi_dialog_add_control( dialog, rec, szStatic, SS_ETCHEDHORZ | SS_SUNKEN );
693 return ERROR_SUCCESS;
694 }
695
696 /******************** Scroll Text ********************************************/
697
698 struct msi_scrolltext_info
699 {
700 msi_dialog *dialog;
701 msi_control *control;
702 WNDPROC oldproc;
703 HMODULE hRichedit;
704 };
705
706 static LRESULT WINAPI
707 MSIScrollText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
708 {
709 struct msi_scrolltext_info *info;
710 HRESULT r;
711
712 TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
713
714 info = GetPropW( hWnd, szButtonData );
715
716 r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
717
718 switch( msg )
719 {
720 case WM_NCDESTROY:
721 FreeLibrary( info->hRichedit );
722 msi_free( info );
723 RemovePropW( hWnd, szButtonData );
724 break;
725 case WM_VSCROLL:
726 msi_dialog_button_handler( info->dialog, info->control, BN_CLICKED );
727 break;
728 }
729 return r;
730 }
731
732 struct msi_streamin_info
733 {
734 LPSTR string;
735 DWORD offset;
736 DWORD length;
737 };
738
739 static DWORD CALLBACK
740 msi_richedit_stream_in( DWORD_PTR arg, LPBYTE buffer, LONG count, LONG *pcb )
741 {
742 struct msi_streamin_info *info = (struct msi_streamin_info*) arg;
743
744 if( (count + info->offset) > info->length )
745 count = info->length - info->offset;
746 memcpy( buffer, &info->string[ info->offset ], count );
747 *pcb = count;
748 info->offset += count;
749
750 TRACE("%ld/%ld\n", info->offset, info->length);
751
752 return 0;
753 }
754
755 static void msi_scrolltext_add_text( msi_control *control, LPCWSTR text )
756 {
757 struct msi_streamin_info info;
758 EDITSTREAM es;
759
760 info.string = strdupWtoA( text );
761 info.offset = 0;
762 info.length = lstrlenA( info.string ) + 1;
763
764 es.dwCookie = (DWORD_PTR) &info;
765 es.dwError = 0;
766 es.pfnCallback = msi_richedit_stream_in;
767
768 SendMessageW( control->hwnd, EM_STREAMIN, SF_RTF, (LPARAM) &es );
769
770 msi_free( info.string );
771 }
772
773 static UINT msi_dialog_scrolltext_control( msi_dialog *dialog, MSIRECORD *rec )
774 {
775 static const WCHAR szRichEdit20W[] = {
776 'R','i','c','h','E','d','i','t','2','0','W',0
777 };
778 struct msi_scrolltext_info *info;
779 msi_control *control;
780 DWORD style;
781
782 info = msi_alloc( sizeof *info );
783 if (!info)
784 return ERROR_FUNCTION_FAILED;
785
786 info->hRichedit = LoadLibraryA("riched20");
787
788 style = WS_BORDER | ES_MULTILINE | WS_VSCROLL |
789 ES_READONLY | ES_AUTOVSCROLL | WS_TABSTOP;
790 control = msi_dialog_add_control( dialog, rec, szRichEdit20W, style );
791 if (!control)
792 {
793 FreeLibrary( info->hRichedit );
794 msi_free( info );
795 return ERROR_FUNCTION_FAILED;
796 }
797
798 info->dialog = dialog;
799 info->control = control;
800
801 /* subclass the static control */
802 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
803 (LONG_PTR)MSIScrollText_WndProc );
804 SetPropW( control->hwnd, szButtonData, info );
805
806 /* add the text into the richedit */
807 msi_scrolltext_add_text( control, MSI_RecordGetString( rec, 10 ) );
808
809 return ERROR_SUCCESS;
810 }
811
812 static HBITMAP msi_load_picture( MSIDATABASE *db, LPCWSTR name,
813 INT cx, INT cy, DWORD flags )
814 {
815 HBITMAP hOleBitmap = 0, hBitmap = 0, hOldSrcBitmap, hOldDestBitmap;
816 MSIRECORD *rec = NULL;
817 IStream *stm = NULL;
818 IPicture *pic = NULL;
819 HDC srcdc, destdc;
820 BITMAP bm;
821 UINT r;
822
823 rec = msi_get_binary_record( db, name );
824 if( !rec )
825 goto end;
826
827 r = MSI_RecordGetIStream( rec, 2, &stm );
828 msiobj_release( &rec->hdr );
829 if( r != ERROR_SUCCESS )
830 goto end;
831
832 r = OleLoadPicture( stm, 0, TRUE, &IID_IPicture, (LPVOID*) &pic );
833 IStream_Release( stm );
834 if( FAILED( r ) )
835 {
836 ERR("failed to load picture\n");
837 goto end;
838 }
839
840 r = IPicture_get_Handle( pic, (OLE_HANDLE*) &hOleBitmap );
841 if( FAILED( r ) )
842 {
843 ERR("failed to get bitmap handle\n");
844 goto end;
845 }
846
847 /* make the bitmap the desired size */
848 r = GetObjectW( hOleBitmap, sizeof bm, &bm );
849 if (r != sizeof bm )
850 {
851 ERR("failed to get bitmap size\n");
852 goto end;
853 }
854
855 if (flags & LR_DEFAULTSIZE)
856 {
857 cx = bm.bmWidth;
858 cy = bm.bmHeight;
859 }
860
861 srcdc = CreateCompatibleDC( NULL );
862 hOldSrcBitmap = SelectObject( srcdc, hOleBitmap );
863 destdc = CreateCompatibleDC( NULL );
864 hBitmap = CreateCompatibleBitmap( srcdc, cx, cy );
865 hOldDestBitmap = SelectObject( destdc, hBitmap );
866 StretchBlt( destdc, 0, 0, cx, cy,
867 srcdc, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
868 SelectObject( srcdc, hOldSrcBitmap );
869 SelectObject( destdc, hOldDestBitmap );
870 DeleteDC( srcdc );
871 DeleteDC( destdc );
872
873 end:
874 if ( pic )
875 IPicture_Release( pic );
876 return hBitmap;
877 }
878
879 static UINT msi_dialog_bitmap_control( msi_dialog *dialog, MSIRECORD *rec )
880 {
881 UINT cx, cy, flags, style, attributes;
882 msi_control *control;
883 LPWSTR text;
884
885 flags = LR_LOADFROMFILE;
886 style = SS_BITMAP | SS_LEFT | WS_GROUP;
887
888 attributes = MSI_RecordGetInteger( rec, 8 );
889 if( attributes & msidbControlAttributesFixedSize )
890 {
891 flags |= LR_DEFAULTSIZE;
892 style |= SS_CENTERIMAGE;
893 }
894
895 control = msi_dialog_add_control( dialog, rec, szStatic, style );
896 cx = MSI_RecordGetInteger( rec, 6 );
897 cy = MSI_RecordGetInteger( rec, 7 );
898 cx = msi_dialog_scale_unit( dialog, cx );
899 cy = msi_dialog_scale_unit( dialog, cy );
900
901 text = msi_get_deformatted_field( dialog->package, rec, 10 );
902 control->hBitmap = msi_load_picture( dialog->package->db, text, cx, cy, flags );
903 if( control->hBitmap )
904 SendMessageW( control->hwnd, STM_SETIMAGE,
905 IMAGE_BITMAP, (LPARAM) control->hBitmap );
906 else
907 ERR("Failed to load bitmap %s\n", debugstr_w(text));
908
909 msi_free( text );
910
911 return ERROR_SUCCESS;
912 }
913
914 static UINT msi_dialog_icon_control( msi_dialog *dialog, MSIRECORD *rec )
915 {
916 msi_control *control;
917 DWORD attributes;
918 LPWSTR text;
919
920 TRACE("\n");
921
922 control = msi_dialog_add_control( dialog, rec, szStatic,
923 SS_ICON | SS_CENTERIMAGE | WS_GROUP );
924
925 attributes = MSI_RecordGetInteger( rec, 8 );
926 text = msi_get_deformatted_field( dialog->package, rec, 10 );
927 control->hIcon = msi_load_icon( dialog->package->db, text, attributes );
928 if( control->hIcon )
929 SendMessageW( control->hwnd, STM_SETICON, (WPARAM) control->hIcon, 0 );
930 else
931 ERR("Failed to load bitmap %s\n", debugstr_w(text));
932 msi_free( text );
933 return ERROR_SUCCESS;
934 }
935
936 static UINT msi_dialog_combo_control( msi_dialog *dialog, MSIRECORD *rec )
937 {
938 static const WCHAR szCombo[] = { 'C','O','M','B','O','B','O','X',0 };
939
940 msi_dialog_add_control( dialog, rec, szCombo,
941 SS_BITMAP | SS_LEFT | SS_CENTERIMAGE );
942 return ERROR_SUCCESS;
943 }
944
945 static UINT msi_dialog_edit_control( msi_dialog *dialog, MSIRECORD *rec )
946 {
947 msi_control *control;
948 LPCWSTR prop;
949 LPWSTR val;
950
951 control = msi_dialog_add_control( dialog, rec, szEdit,
952 WS_BORDER | WS_TABSTOP );
953 control->handler = msi_dialog_edit_handler;
954 prop = MSI_RecordGetString( rec, 9 );
955 if( prop )
956 control->property = strdupW( prop );
957 val = msi_dup_property( dialog->package, control->property );
958 SetWindowTextW( control->hwnd, val );
959 msi_free( val );
960 return ERROR_SUCCESS;
961 }
962
963 /******************** Masked Edit ********************************************/
964
965 #define MASK_MAX_GROUPS 10
966
967 struct msi_mask_group
968 {
969 UINT len;
970 UINT ofs;
971 WCHAR type;
972 HWND hwnd;
973 };
974
975 struct msi_maskedit_info
976 {
977 msi_dialog *dialog;
978 WNDPROC oldproc;
979 HWND hwnd;
980 LPWSTR prop;
981 UINT num_chars;
982 UINT num_groups;
983 struct msi_mask_group group[MASK_MAX_GROUPS];
984 };
985
986 static BOOL msi_mask_editable( WCHAR type )
987 {
988 switch (type)
989 {
990 case '%':
991 case '#':
992 case '&':
993 case '`':
994 case '?':
995 case '^':
996 return TRUE;
997 }
998 return FALSE;
999 }
1000
1001 static void msi_mask_control_change( struct msi_maskedit_info *info )
1002 {
1003 LPWSTR val;
1004 UINT i, n, r;
1005
1006 val = msi_alloc( (info->num_chars+1)*sizeof(WCHAR) );
1007 for( i=0, n=0; i<info->num_groups; i++ )
1008 {
1009 if( (info->group[i].len + n) > info->num_chars )
1010 {
1011 ERR("can't fit control %d text into template\n",i);
1012 break;
1013 }
1014 if (!msi_mask_editable(info->group[i].type))
1015 {
1016 for(r=0; r<info->group[i].len; r++)
1017 val[n+r] = info->group[i].type;
1018 val[n+r] = 0;
1019 }
1020 else
1021 {
1022 r = GetWindowTextW( info->group[i].hwnd, &val[n], info->group[i].len+1 );
1023 if( r != info->group[i].len )
1024 break;
1025 }
1026 n += r;
1027 }
1028
1029 TRACE("%d/%d controls were good\n", i, info->num_groups);
1030
1031 if( i == info->num_groups )
1032 {
1033 TRACE("Set property %s to %s\n",
1034 debugstr_w(info->prop), debugstr_w(val) );
1035 CharUpperBuffW( val, info->num_chars );
1036 MSI_SetPropertyW( info->dialog->package, info->prop, val );
1037 msi_dialog_evaluate_control_conditions( info->dialog );
1038 }
1039 msi_free( val );
1040 }
1041
1042 /* now move to the next control if necessary */
1043 static VOID msi_mask_next_control( struct msi_maskedit_info *info, HWND hWnd )
1044 {
1045 HWND hWndNext;
1046 UINT len, i;
1047
1048 for( i=0; i<info->num_groups; i++ )
1049 if( info->group[i].hwnd == hWnd )
1050 break;
1051
1052 /* don't move from the last control */
1053 if( i >= (info->num_groups-1) )
1054 return;
1055
1056 len = SendMessageW( hWnd, WM_GETTEXTLENGTH, 0, 0 );
1057 if( len < info->group[i].len )
1058 return;
1059
1060 hWndNext = GetNextDlgTabItem( GetParent( hWnd ), hWnd, FALSE );
1061 SetFocus( hWndNext );
1062 }
1063
1064 static LRESULT WINAPI
1065 MSIMaskedEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1066 {
1067 struct msi_maskedit_info *info;
1068 HRESULT r;
1069
1070 TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
1071
1072 info = GetPropW(hWnd, szButtonData);
1073
1074 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1075
1076 switch( msg )
1077 {
1078 case WM_COMMAND:
1079 if (HIWORD(wParam) == EN_CHANGE)
1080 {
1081 msi_mask_control_change( info );
1082 msi_mask_next_control( info, (HWND) lParam );
1083 }
1084 break;
1085 case WM_NCDESTROY:
1086 msi_free( info->prop );
1087 msi_free( info );
1088 RemovePropW( hWnd, szButtonData );
1089 break;
1090 }
1091
1092 return r;
1093 }
1094
1095 /* fish the various bits of the property out and put them in the control */
1096 static void
1097 msi_maskedit_set_text( struct msi_maskedit_info *info, LPCWSTR text )
1098 {
1099 LPCWSTR p;
1100 UINT i;
1101
1102 p = text;
1103 for( i = 0; i < info->num_groups; i++ )
1104 {
1105 if( info->group[i].len < lstrlenW( p ) )
1106 {
1107 LPWSTR chunk = strdupW( p );
1108 chunk[ info->group[i].len ] = 0;
1109 SetWindowTextW( info->group[i].hwnd, chunk );
1110 msi_free( chunk );
1111 }
1112 else
1113 {
1114 SetWindowTextW( info->group[i].hwnd, p );
1115 break;
1116 }
1117 p += info->group[i].len;
1118 }
1119 }
1120
1121 static struct msi_maskedit_info * msi_dialog_parse_groups( LPCWSTR mask )
1122 {
1123 struct msi_maskedit_info * info = NULL;
1124 int i = 0, n = 0, total = 0;
1125 LPCWSTR p;
1126
1127 TRACE("masked control, template %s\n", debugstr_w(mask));
1128
1129 if( !mask )
1130 return info;
1131
1132 info = msi_alloc_zero( sizeof *info );
1133 if( !info )
1134 return info;
1135
1136 p = strchrW(mask, '<');
1137 if( p )
1138 p++;
1139 else
1140 p = mask;
1141
1142 for( i=0; i<MASK_MAX_GROUPS; i++ )
1143 {
1144 /* stop at the end of the string */
1145 if( p[0] == 0 || p[0] == '>' )
1146 break;
1147
1148 /* count the number of the same identifier */
1149 for( n=0; p[n] == p[0]; n++ )
1150 ;
1151 info->group[i].ofs = total;
1152 info->group[i].type = p[0];
1153 if( p[n] == '=' )
1154 {
1155 n++;
1156 total++; /* an extra not part of the group */
1157 }
1158 info->group[i].len = n;
1159 total += n;
1160 p += n;
1161 }
1162
1163 TRACE("%d characters in %d groups\n", total, i );
1164 if( i == MASK_MAX_GROUPS )
1165 ERR("too many groups in PIDTemplate %s\n", debugstr_w(mask));
1166
1167 info->num_chars = total;
1168 info->num_groups = i;
1169
1170 return info;
1171 }
1172
1173 static void
1174 msi_maskedit_create_children( struct msi_maskedit_info *info, LPCWSTR font )
1175 {
1176 DWORD width, height, style, wx, ww;
1177 RECT rect;
1178 HWND hwnd;
1179 UINT i;
1180
1181 style = WS_CHILD | WS_BORDER | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL;
1182
1183 GetClientRect( info->hwnd, &rect );
1184
1185 width = rect.right - rect.left;
1186 height = rect.bottom - rect.top;
1187
1188 for( i = 0; i < info->num_groups; i++ )
1189 {
1190 if (!msi_mask_editable( info->group[i].type ))
1191 continue;
1192 wx = (info->group[i].ofs * width) / info->num_chars;
1193 ww = (info->group[i].len * width) / info->num_chars;
1194
1195 hwnd = CreateWindowW( szEdit, NULL, style, wx, 0, ww, height,
1196 info->hwnd, NULL, NULL, NULL );
1197 if( !hwnd )
1198 {
1199 ERR("failed to create mask edit sub window\n");
1200 break;
1201 }
1202
1203 SendMessageW( hwnd, EM_LIMITTEXT, info->group[i].len, 0 );
1204
1205 msi_dialog_set_font( info->dialog, hwnd,
1206 font?font:info->dialog->default_font );
1207 info->group[i].hwnd = hwnd;
1208 }
1209 }
1210
1211 /*
1212 * office 2003 uses "73931<````=````=````=````=`````>@@@@@"
1213 * delphi 7 uses "<????-??????-??????-????>" and "<???-???>"
1214 * filemaker pro 7 uses "<^^^^=^^^^=^^^^=^^^^=^^^^=^^^^=^^^^^>"
1215 */
1216 static UINT msi_dialog_maskedit_control( msi_dialog *dialog, MSIRECORD *rec )
1217 {
1218 LPWSTR font_mask, val = NULL, font;
1219 struct msi_maskedit_info *info = NULL;
1220 UINT ret = ERROR_SUCCESS;
1221 msi_control *control;
1222 LPCWSTR prop, mask;
1223
1224 TRACE("\n");
1225
1226 font_mask = msi_get_deformatted_field( dialog->package, rec, 10 );
1227 font = msi_dialog_get_style( font_mask, &mask );
1228 if( !mask )
1229 {
1230 ERR("mask template is empty\n");
1231 goto end;
1232 }
1233
1234 info = msi_dialog_parse_groups( mask );
1235 if( !info )
1236 {
1237 ERR("template %s is invalid\n", debugstr_w(mask));
1238 goto end;
1239 }
1240
1241 info->dialog = dialog;
1242
1243 control = msi_dialog_add_control( dialog, rec, szStatic,
1244 SS_OWNERDRAW | WS_GROUP | WS_VISIBLE );
1245 if( !control )
1246 {
1247 ERR("Failed to create maskedit container\n");
1248 ret = ERROR_FUNCTION_FAILED;
1249 goto end;
1250 }
1251 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
1252
1253 info->hwnd = control->hwnd;
1254
1255 /* subclass the static control */
1256 info->oldproc = (WNDPROC) SetWindowLongPtrW( info->hwnd, GWLP_WNDPROC,
1257 (LONG_PTR)MSIMaskedEdit_WndProc );
1258 SetPropW( control->hwnd, szButtonData, info );
1259
1260 prop = MSI_RecordGetString( rec, 9 );
1261 if( prop )
1262 info->prop = strdupW( prop );
1263
1264 msi_maskedit_create_children( info, font );
1265
1266 if( prop )
1267 {
1268 val = msi_dup_property( dialog->package, prop );
1269 if( val )
1270 {
1271 msi_maskedit_set_text( info, val );
1272 msi_free( val );
1273 }
1274 }
1275
1276 end:
1277 if( ret != ERROR_SUCCESS )
1278 msi_free( info );
1279 msi_free( font_mask );
1280 msi_free( font );
1281 return ret;
1282 }
1283
1284 /******************** Progress Bar *****************************************/
1285
1286 static UINT msi_dialog_progress_bar( msi_dialog *dialog, MSIRECORD *rec )
1287 {
1288 msi_dialog_add_control( dialog, rec, PROGRESS_CLASSW, WS_VISIBLE );
1289 return ERROR_SUCCESS;
1290 }
1291
1292 /******************** Path Edit ********************************************/
1293
1294 static UINT msi_dialog_pathedit_control( msi_dialog *dialog, MSIRECORD *rec )
1295 {
1296 FIXME("not implemented properly\n");
1297 return msi_dialog_edit_control( dialog, rec );
1298 }
1299
1300 /* radio buttons are a bit different from normal controls */
1301 static UINT msi_dialog_create_radiobutton( MSIRECORD *rec, LPVOID param )
1302 {
1303 radio_button_group_descr *group = (radio_button_group_descr *)param;
1304 msi_dialog *dialog = group->dialog;
1305 msi_control *control;
1306 LPCWSTR prop, text, name;
1307 DWORD style, attributes = group->attributes;
1308
1309 style = WS_CHILD | BS_AUTORADIOBUTTON | BS_MULTILINE | WS_TABSTOP;
1310 name = MSI_RecordGetString( rec, 3 );
1311 text = MSI_RecordGetString( rec, 8 );
1312 if( attributes & 1 )
1313 style |= WS_VISIBLE;
1314 if( ~attributes & 2 )
1315 style |= WS_DISABLED;
1316
1317 control = msi_dialog_create_window( dialog, rec, szButton, name, text,
1318 style, group->parent->hwnd );
1319 if (!control)
1320 return ERROR_FUNCTION_FAILED;
1321 control->handler = msi_dialog_radiogroup_handler;
1322
1323 prop = MSI_RecordGetString( rec, 1 );
1324 if( prop )
1325 control->property = strdupW( prop );
1326
1327 return ERROR_SUCCESS;
1328 }
1329
1330 static UINT msi_dialog_radiogroup_control( msi_dialog *dialog, MSIRECORD *rec )
1331 {
1332 static const WCHAR query[] = {
1333 'S','E','L','E','C','T',' ','*',' ',
1334 'F','R','O','M',' ','R','a','d','i','o','B','u','t','t','o','n',' ',
1335 'W','H','E','R','E',' ',
1336 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
1337 UINT r;
1338 LPCWSTR prop;
1339 msi_control *control;
1340 MSIQUERY *view = NULL;
1341 radio_button_group_descr group;
1342 MSIPACKAGE *package = dialog->package;
1343 WNDPROC oldproc;
1344
1345 prop = MSI_RecordGetString( rec, 9 );
1346
1347 TRACE("%p %p %s\n", dialog, rec, debugstr_w( prop ));
1348
1349 /* Create parent group box to hold radio buttons */
1350 control = msi_dialog_add_control( dialog, rec, szButton, BS_OWNERDRAW|WS_GROUP );
1351 if( !control )
1352 return ERROR_FUNCTION_FAILED;
1353
1354 oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1355 (LONG_PTR)MSIRadioGroup_WndProc );
1356 SetPropW(control->hwnd, szButtonData, oldproc);
1357 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
1358
1359 if( prop )
1360 control->property = strdupW( prop );
1361
1362 /* query the Radio Button table for all control in this group */
1363 r = MSI_OpenQuery( package->db, &view, query, prop );
1364 if( r != ERROR_SUCCESS )
1365 {
1366 ERR("query failed for dialog %s radio group %s\n",
1367 debugstr_w(dialog->name), debugstr_w(prop));
1368 return ERROR_INVALID_PARAMETER;
1369 }
1370
1371 group.dialog = dialog;
1372 group.parent = control;
1373 group.attributes = MSI_RecordGetInteger( rec, 8 );
1374
1375 r = MSI_IterateRecords( view, 0, msi_dialog_create_radiobutton, &group );
1376 msiobj_release( &view->hdr );
1377
1378 return r;
1379 }
1380
1381 /******************** Selection Tree ***************************************/
1382
1383 static void
1384 msi_dialog_tv_add_child_features( MSIPACKAGE *package, HWND hwnd,
1385 LPCWSTR parent, HTREEITEM hParent )
1386 {
1387 MSIFEATURE *feature;
1388 TVINSERTSTRUCTW tvis;
1389 HTREEITEM hitem;
1390
1391 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
1392 {
1393 if ( lstrcmpW( parent, feature->Feature_Parent ) )
1394 continue;
1395
1396 if ( !feature->Title )
1397 continue;
1398
1399 memset( &tvis, 0, sizeof tvis );
1400 tvis.hParent = hParent;
1401 tvis.hInsertAfter = TVI_SORT;
1402 if (feature->Title)
1403 {
1404 tvis.u.item.mask = TVIF_TEXT;
1405 tvis.u.item.pszText = feature->Title;
1406 }
1407 tvis.u.item.lParam = (LPARAM) feature;
1408 hitem = (HTREEITEM) SendMessageW( hwnd, TVM_INSERTITEMW, 0, (LPARAM) &tvis );
1409 if (!hitem)
1410 continue;
1411
1412 msi_dialog_tv_add_child_features( package, hwnd,
1413 feature->Feature, hitem );
1414 }
1415 }
1416
1417 static UINT msi_dialog_selection_tree( msi_dialog *dialog, MSIRECORD *rec )
1418 {
1419 msi_control *control;
1420 LPCWSTR prop;
1421 LPWSTR val;
1422 MSIPACKAGE *package = dialog->package;
1423
1424 prop = MSI_RecordGetString( rec, 9 );
1425 val = msi_dup_property( package, prop );
1426 control = msi_dialog_add_control( dialog, rec, WC_TREEVIEWW,
1427 TVS_HASBUTTONS | WS_GROUP | WS_VSCROLL );
1428 if (!control)
1429 return ERROR_FUNCTION_FAILED;
1430
1431 msi_dialog_tv_add_child_features( package, control->hwnd, NULL, NULL );
1432
1433 msi_free( val );
1434
1435 return ERROR_SUCCESS;
1436 }
1437
1438 struct control_handler msi_dialog_handler[] =
1439 {
1440 { szText, msi_dialog_text_control },
1441 { szPushButton, msi_dialog_button_control },
1442 { szLine, msi_dialog_line_control },
1443 { szBitmap, msi_dialog_bitmap_control },
1444 { szCheckBox, msi_dialog_checkbox_control },
1445 { szScrollableText, msi_dialog_scrolltext_control },
1446 { szComboBox, msi_dialog_combo_control },
1447 { szEdit, msi_dialog_edit_control },
1448 { szMaskedEdit, msi_dialog_maskedit_control },
1449 { szPathEdit, msi_dialog_pathedit_control },
1450 { szProgressBar, msi_dialog_progress_bar },
1451 { szRadioButtonGroup, msi_dialog_radiogroup_control },
1452 { szIcon, msi_dialog_icon_control },
1453 { szSelectionTree, msi_dialog_selection_tree },
1454 };
1455
1456 #define NUM_CONTROL_TYPES (sizeof msi_dialog_handler/sizeof msi_dialog_handler[0])
1457
1458 static UINT msi_dialog_create_controls( MSIRECORD *rec, LPVOID param )
1459 {
1460 msi_dialog *dialog = param;
1461 LPCWSTR control_type;
1462 UINT i;
1463
1464 /* find and call the function that can create this type of control */
1465 control_type = MSI_RecordGetString( rec, 3 );
1466 for( i=0; i<NUM_CONTROL_TYPES; i++ )
1467 if (!strcmpiW( msi_dialog_handler[i].control_type, control_type ))
1468 break;
1469 if( i != NUM_CONTROL_TYPES )
1470 msi_dialog_handler[i].func( dialog, rec );
1471 else
1472 ERR("no handler for element type %s\n", debugstr_w(control_type));
1473
1474 return ERROR_SUCCESS;
1475 }
1476
1477 static UINT msi_dialog_fill_controls( msi_dialog *dialog )
1478 {
1479 static const WCHAR query[] = {
1480 'S','E','L','E','C','T',' ','*',' ',
1481 'F','R','O','M',' ','C','o','n','t','r','o','l',' ',
1482 'W','H','E','R','E',' ',
1483 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
1484 UINT r;
1485 MSIQUERY *view = NULL;
1486 MSIPACKAGE *package = dialog->package;
1487
1488 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
1489
1490 /* query the Control table for all the elements of the control */
1491 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
1492 if( r != ERROR_SUCCESS )
1493 {
1494 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
1495 return ERROR_INVALID_PARAMETER;
1496 }
1497
1498 r = MSI_IterateRecords( view, 0, msi_dialog_create_controls, dialog );
1499 msiobj_release( &view->hdr );
1500
1501 return r;
1502 }
1503
1504 static UINT msi_dialog_set_control_condition( MSIRECORD *rec, LPVOID param )
1505 {
1506 static const WCHAR szHide[] = { 'H','i','d','e',0 };
1507 static const WCHAR szShow[] = { 'S','h','o','w',0 };
1508 static const WCHAR szDisable[] = { 'D','i','s','a','b','l','e',0 };
1509 static const WCHAR szEnable[] = { 'E','n','a','b','l','e',0 };
1510 msi_dialog *dialog = param;
1511 msi_control *control;
1512 LPCWSTR name, action, condition;
1513 UINT r;
1514
1515 name = MSI_RecordGetString( rec, 2 );
1516 action = MSI_RecordGetString( rec, 3 );
1517 condition = MSI_RecordGetString( rec, 4 );
1518 r = MSI_EvaluateConditionW( dialog->package, condition );
1519 control = msi_dialog_find_control( dialog, name );
1520 if( r == MSICONDITION_TRUE && control )
1521 {
1522 TRACE("%s control %s\n", debugstr_w(action), debugstr_w(name));
1523
1524 /* FIXME: case sensitive? */
1525 if(!lstrcmpW(action, szHide))
1526 ShowWindow(control->hwnd, SW_HIDE);
1527 else if(!strcmpW(action, szShow))
1528 ShowWindow(control->hwnd, SW_SHOW);
1529 else if(!strcmpW(action, szDisable))
1530 EnableWindow(control->hwnd, FALSE);
1531 else if(!strcmpW(action, szEnable))
1532 EnableWindow(control->hwnd, TRUE);
1533 else
1534 FIXME("Unhandled action %s\n", debugstr_w(action));
1535 }
1536
1537 return ERROR_SUCCESS;
1538 }
1539
1540 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog )
1541 {
1542 static const WCHAR query[] = {
1543 'S','E','L','E','C','T',' ','*',' ',
1544 'F','R','O','M',' ',
1545 'C','o','n','t','r','o','l','C','o','n','d','i','t','i','o','n',' ',
1546 'W','H','E','R','E',' ',
1547 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0
1548 };
1549 UINT r;
1550 MSIQUERY *view = NULL;
1551 MSIPACKAGE *package = dialog->package;
1552
1553 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
1554
1555 /* query the Control table for all the elements of the control */
1556 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
1557 if( r != ERROR_SUCCESS )
1558 {
1559 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
1560 return ERROR_INVALID_PARAMETER;
1561 }
1562
1563 r = MSI_IterateRecords( view, 0, msi_dialog_set_control_condition, dialog );
1564 msiobj_release( &view->hdr );
1565
1566 return r;
1567 }
1568
1569 UINT msi_dialog_reset( msi_dialog *dialog )
1570 {
1571 /* FIXME: should restore the original values of any properties we changed */
1572 return msi_dialog_evaluate_control_conditions( dialog );
1573 }
1574
1575 /* figure out the height of 10 point MS Sans Serif */
1576 static INT msi_dialog_get_sans_serif_height( HWND hwnd )
1577 {
1578 static const WCHAR szSansSerif[] = {
1579 'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0 };
1580 LOGFONTW lf;
1581 TEXTMETRICW tm;
1582 BOOL r;
1583 LONG height = 0;
1584 HFONT hFont, hOldFont;
1585 HDC hdc;
1586
1587 hdc = GetDC( hwnd );
1588 if (hdc)
1589 {
1590 memset( &lf, 0, sizeof lf );
1591 lf.lfHeight = MulDiv(10, GetDeviceCaps(hdc, LOGPIXELSY), 72);
1592 strcpyW( lf.lfFaceName, szSansSerif );
1593 hFont = CreateFontIndirectW(&lf);
1594 if (hFont)
1595 {
1596 hOldFont = SelectObject( hdc, hFont );
1597 r = GetTextMetricsW( hdc, &tm );
1598 if (r)
1599 height = tm.tmHeight;
1600 SelectObject( hdc, hOldFont );
1601 DeleteObject( hFont );
1602 }
1603 ReleaseDC( hwnd, hdc );
1604 }
1605 return height;
1606 }
1607
1608 /* fetch the associated record from the Dialog table */
1609 static MSIRECORD *msi_get_dialog_record( msi_dialog *dialog )
1610 {
1611 static const WCHAR query[] = {
1612 'S','E','L','E','C','T',' ','*',' ',
1613 'F','R','O','M',' ','D','i','a','l','o','g',' ',
1614 'W','H','E','R','E',' ',
1615 '`','D','i','a','l','o','g','`',' ','=',' ','\'','%','s','\'',0};
1616 MSIPACKAGE *package = dialog->package;
1617 MSIRECORD *rec = NULL;
1618
1619 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
1620
1621 rec = MSI_QueryGetRecord( package->db, query, dialog->name );
1622 if( !rec )
1623 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
1624
1625 return rec;
1626 }
1627
1628 static void msi_dialog_adjust_dialog_size( msi_dialog *dialog, LPSIZE sz )
1629 {
1630 RECT rect;
1631 LONG style;
1632
1633 /* turn the client size into the window rectangle */
1634 rect.left = 0;
1635 rect.top = 0;
1636 rect.right = msi_dialog_scale_unit( dialog, sz->cx );
1637 rect.bottom = msi_dialog_scale_unit( dialog, sz->cy );
1638 style = GetWindowLongPtrW( dialog->hwnd, GWL_STYLE );
1639 AdjustWindowRect( &rect, style, FALSE );
1640 sz->cx = rect.right - rect.left;
1641 sz->cy = rect.bottom - rect.top;
1642 }
1643
1644 static BOOL msi_control_set_next( msi_control *control, msi_control *next )
1645 {
1646 return SetWindowPos( next->hwnd, control->hwnd, 0, 0, 0, 0,
1647 SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOREDRAW |
1648 SWP_NOREPOSITION | SWP_NOSENDCHANGING | SWP_NOSIZE );
1649 }
1650
1651 static UINT msi_dialog_set_tab_order( msi_dialog *dialog )
1652 {
1653 msi_control *control, *tab_next;
1654
1655 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
1656 {
1657 tab_next = msi_dialog_find_control( dialog, control->tabnext );
1658 if( !tab_next )
1659 continue;
1660 msi_control_set_next( control, tab_next );
1661 }
1662
1663 return ERROR_SUCCESS;
1664 }
1665
1666 static void msi_dialog_set_first_control( msi_dialog* dialog, LPCWSTR name )
1667 {
1668 msi_control *control;
1669
1670 control = msi_dialog_find_control( dialog, name );
1671 if( control )
1672 dialog->hWndFocus = control->hwnd;
1673 else
1674 dialog->hWndFocus = NULL;
1675 }
1676
1677 static LRESULT msi_dialog_oncreate( HWND hwnd, LPCREATESTRUCTW cs )
1678 {
1679 static const WCHAR df[] = {
1680 'D','e','f','a','u','l','t','U','I','F','o','n','t',0 };
1681 static const WCHAR dfv[] = {
1682 'M','S',' ','S','h','e','l','l',' ','D','l','g',0 };
1683 msi_dialog *dialog = (msi_dialog*) cs->lpCreateParams;
1684 MSIRECORD *rec = NULL;
1685 LPWSTR title = NULL;
1686 SIZE size;
1687
1688 TRACE("%p %p\n", dialog, dialog->package);
1689
1690 dialog->hwnd = hwnd;
1691 SetWindowLongPtrW( hwnd, GWLP_USERDATA, (LONG_PTR) dialog );
1692
1693 rec = msi_get_dialog_record( dialog );
1694 if( !rec )
1695 {
1696 TRACE("No record found for dialog %s\n", debugstr_w(dialog->name));
1697 return -1;
1698 }
1699
1700 dialog->scale = msi_dialog_get_sans_serif_height(dialog->hwnd);
1701
1702 size.cx = MSI_RecordGetInteger( rec, 4 );
1703 size.cy = MSI_RecordGetInteger( rec, 5 );
1704 msi_dialog_adjust_dialog_size( dialog, &size );
1705
1706 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
1707
1708 dialog->default_font = msi_dup_property( dialog->package, df );
1709 if (!dialog->default_font)
1710 {
1711 dialog->default_font = strdupW(dfv);
1712 if (!dialog->default_font) return -1;
1713 }
1714
1715 title = msi_get_deformatted_field( dialog->package, rec, 7 );
1716 SetWindowTextW( hwnd, title );
1717 msi_free( title );
1718
1719 SetWindowPos( hwnd, 0, 0, 0, size.cx, size.cy,
1720 SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW );
1721
1722
1723 msi_dialog_build_font_list( dialog );
1724 msi_dialog_fill_controls( dialog );
1725 msi_dialog_evaluate_control_conditions( dialog );
1726 msi_dialog_set_tab_order( dialog );
1727 msi_dialog_set_first_control( dialog, MSI_RecordGetString( rec, 8 ) );
1728 msiobj_release( &rec->hdr );
1729
1730 return 0;
1731 }
1732
1733 static UINT msi_dialog_send_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
1734 {
1735 LPWSTR event_fmt = NULL, arg_fmt = NULL;
1736
1737 TRACE("Sending control event %s %s\n", debugstr_w(event), debugstr_w(arg));
1738
1739 deformat_string( dialog->package, event, &event_fmt );
1740 deformat_string( dialog->package, arg, &arg_fmt );
1741
1742 dialog->event_handler( dialog->package, event_fmt, arg_fmt, dialog );
1743
1744 msi_free( event_fmt );
1745 msi_free( arg_fmt );
1746
1747 return ERROR_SUCCESS;
1748 }
1749
1750 static UINT msi_dialog_set_property( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
1751 {
1752 static const WCHAR szNullArg[] = { '{','}',0 };
1753 LPWSTR p, prop, arg_fmt = NULL;
1754 UINT len;
1755
1756 len = strlenW(event);
1757 prop = msi_alloc( len*sizeof(WCHAR));
1758 strcpyW( prop, &event[1] );
1759 p = strchrW( prop, ']' );
1760 if( p && p[1] == 0 )
1761 {
1762 *p = 0;
1763 if( strcmpW( szNullArg, arg ) )
1764 deformat_string( dialog->package, arg, &arg_fmt );
1765 MSI_SetPropertyW( dialog->package, prop, arg_fmt );
1766 msi_free( arg_fmt );
1767 }
1768 else
1769 ERR("Badly formatted property string - what happens?\n");
1770 msi_free( prop );
1771 return ERROR_SUCCESS;
1772 }
1773
1774 static UINT msi_dialog_control_event( MSIRECORD *rec, LPVOID param )
1775 {
1776 msi_dialog *dialog = param;
1777 LPCWSTR condition, event, arg;
1778 UINT r;
1779
1780 condition = MSI_RecordGetString( rec, 5 );
1781 r = MSI_EvaluateConditionW( dialog->package, condition );
1782 if( r == MSICONDITION_TRUE )
1783 {
1784 event = MSI_RecordGetString( rec, 3 );
1785 arg = MSI_RecordGetString( rec, 4 );
1786 if( event[0] == '[' )
1787 msi_dialog_set_property( dialog, event, arg );
1788 else
1789 msi_dialog_send_event( dialog, event, arg );
1790 }
1791
1792 return ERROR_SUCCESS;
1793 }
1794
1795 static UINT msi_dialog_button_handler( msi_dialog *dialog,
1796 msi_control *control, WPARAM param )
1797 {
1798 static const WCHAR query[] = {
1799 'S','E','L','E','C','T',' ','*',' ',
1800 'F','R','O','M',' ','C','o','n','t','r','o','l','E','v','e','n','t',' ',
1801 'W','H','E','R','E',' ',
1802 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
1803 'A','N','D',' ',
1804 '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',' ',
1805 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','i','n','g','`',0
1806 };
1807 MSIQUERY *view = NULL;
1808 UINT r;
1809
1810 if( HIWORD(param) != BN_CLICKED )
1811 return ERROR_SUCCESS;
1812
1813 r = MSI_OpenQuery( dialog->package->db, &view, query,
1814 dialog->name, control->name );
1815 if( r != ERROR_SUCCESS )
1816 {
1817 ERR("query failed\n");
1818 return 0;
1819 }
1820
1821 r = MSI_IterateRecords( view, 0, msi_dialog_control_event, dialog );
1822 msiobj_release( &view->hdr );
1823
1824 return r;
1825 }
1826
1827 static UINT msi_dialog_get_checkbox_state( msi_dialog *dialog,
1828 msi_control *control )
1829 {
1830 WCHAR state[2] = { 0 };
1831 DWORD sz = 2;
1832
1833 MSI_GetPropertyW( dialog->package, control->property, state, &sz );
1834 return state[0] ? 1 : 0;
1835 }
1836
1837 static void msi_dialog_set_checkbox_state( msi_dialog *dialog,
1838 msi_control *control, UINT state )
1839 {
1840 static const WCHAR szState[] = { '1', 0 };
1841 LPCWSTR val;
1842
1843 /* if uncheck then the property is set to NULL */
1844 if (!state)
1845 {
1846 MSI_SetPropertyW( dialog->package, control->property, NULL );
1847 return;
1848 }
1849
1850 /* check for a custom state */
1851 if (control->value && control->value[0])
1852 val = control->value;
1853 else
1854 val = szState;
1855
1856 MSI_SetPropertyW( dialog->package, control->property, val );
1857 }
1858
1859 static void msi_dialog_checkbox_sync_state( msi_dialog *dialog,
1860 msi_control *control )
1861 {
1862 UINT state;
1863
1864 state = msi_dialog_get_checkbox_state( dialog, control );
1865 SendMessageW( control->hwnd, BM_SETCHECK,
1866 state ? BST_CHECKED : BST_UNCHECKED, 0 );
1867 }
1868
1869 static UINT msi_dialog_checkbox_handler( msi_dialog *dialog,
1870 msi_control *control, WPARAM param )
1871 {
1872 UINT state;
1873
1874 if( HIWORD(param) != BN_CLICKED )
1875 return ERROR_SUCCESS;
1876
1877 TRACE("clicked checkbox %s, set %s\n", debugstr_w(control->name),
1878 debugstr_w(control->property));
1879
1880 state = msi_dialog_get_checkbox_state( dialog, control );
1881 state = state ? 0 : 1;
1882 msi_dialog_set_checkbox_state( dialog, control, state );
1883 msi_dialog_checkbox_sync_state( dialog, control );
1884
1885 return msi_dialog_button_handler( dialog, control, param );
1886 }
1887
1888 static UINT msi_dialog_edit_handler( msi_dialog *dialog,
1889 msi_control *control, WPARAM param )
1890 {
1891 UINT sz, r;
1892 LPWSTR buf;
1893
1894 if( HIWORD(param) != EN_CHANGE )
1895 return ERROR_SUCCESS;
1896
1897 TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
1898 debugstr_w(control->property));
1899
1900 sz = 0x20;
1901 buf = msi_alloc( sz*sizeof(WCHAR) );
1902 while( buf )
1903 {
1904 r = GetWindowTextW( control->hwnd, buf, sz );
1905 if( r < (sz-1) )
1906 break;
1907 sz *= 2;
1908 buf = msi_realloc( buf, sz*sizeof(WCHAR) );
1909 }
1910
1911 MSI_SetPropertyW( dialog->package, control->property, buf );
1912
1913 msi_free( buf );
1914
1915 return ERROR_SUCCESS;
1916 }
1917
1918 static UINT msi_dialog_radiogroup_handler( msi_dialog *dialog,
1919 msi_control *control, WPARAM param )
1920 {
1921 if( HIWORD(param) != BN_CLICKED )
1922 return ERROR_SUCCESS;
1923
1924 TRACE("clicked radio button %s, set %s\n", debugstr_w(control->name),
1925 debugstr_w(control->property));
1926
1927 MSI_SetPropertyW( dialog->package, control->property, control->name );
1928
1929 return msi_dialog_button_handler( dialog, control, param );
1930 }
1931
1932 static LRESULT msi_dialog_oncommand( msi_dialog *dialog, WPARAM param, HWND hwnd )
1933 {
1934 msi_control *control = NULL;
1935
1936 TRACE("%p %p %08x\n", dialog, hwnd, param);
1937
1938 switch (param)
1939 {
1940 case 1: /* enter */
1941 control = msi_dialog_find_control( dialog, dialog->control_default );
1942 break;
1943 case 2: /* escape */
1944 control = msi_dialog_find_control( dialog, dialog->control_cancel );
1945 break;
1946 default:
1947 control = msi_dialog_find_control_by_hwnd( dialog, hwnd );
1948 }
1949
1950 if( control )
1951 {
1952 if( control->handler )
1953 {
1954 control->handler( dialog, control, param );
1955 msi_dialog_evaluate_control_conditions( dialog );
1956 }
1957 }
1958 else
1959 ERR("button click from nowhere %p %d %p\n", dialog, param, hwnd);
1960 return 0;
1961 }
1962
1963 static void msi_dialog_setfocus( msi_dialog *dialog )
1964 {
1965 HWND hwnd = dialog->hWndFocus;
1966
1967 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, TRUE);
1968 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, FALSE);
1969 SetFocus( hwnd );
1970 dialog->hWndFocus = hwnd;
1971 }
1972
1973 static LRESULT WINAPI MSIDialog_WndProc( HWND hwnd, UINT msg,
1974 WPARAM wParam, LPARAM lParam )
1975 {
1976 msi_dialog *dialog = (LPVOID) GetWindowLongPtrW( hwnd, GWLP_USERDATA );
1977
1978 TRACE("0x%04x\n", msg);
1979
1980 switch (msg)
1981 {
1982 case WM_CREATE:
1983 return msi_dialog_oncreate( hwnd, (LPCREATESTRUCTW)lParam );
1984
1985 case WM_COMMAND:
1986 return msi_dialog_oncommand( dialog, wParam, (HWND)lParam );
1987
1988 case WM_ACTIVATE:
1989 if( LOWORD(wParam) == WA_INACTIVE )
1990 dialog->hWndFocus = GetFocus();
1991 else
1992 msi_dialog_setfocus( dialog );
1993 return 0;
1994
1995 case WM_SETFOCUS:
1996 msi_dialog_setfocus( dialog );
1997 return 0;
1998
1999 /* bounce back to our subclassed static control */
2000 case WM_CTLCOLORSTATIC:
2001 return SendMessageW( (HWND) lParam, WM_CTLCOLORSTATIC, wParam, lParam );
2002
2003 case WM_DESTROY:
2004 dialog->hwnd = NULL;
2005 return 0;
2006 }
2007 return DefWindowProcW(hwnd, msg, wParam, lParam);
2008 }
2009
2010 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2011 {
2012 WNDPROC oldproc = (WNDPROC) GetPropW(hWnd, szButtonData);
2013
2014 TRACE("hWnd %p msg %04x wParam 0x%08x lParam 0x%08lx\n", hWnd, msg, wParam, lParam);
2015
2016 if (msg == WM_COMMAND) /* Forward notifications to dialog */
2017 SendMessageW(GetParent(hWnd), msg, wParam, lParam);
2018
2019 return CallWindowProcW(oldproc, hWnd, msg, wParam, lParam);
2020 }
2021
2022 static LRESULT WINAPI MSIHiddenWindowProc( HWND hwnd, UINT msg,
2023 WPARAM wParam, LPARAM lParam )
2024 {
2025 msi_dialog *dialog = (msi_dialog*) lParam;
2026
2027 TRACE("%d %p\n", msg, dialog);
2028
2029 switch (msg)
2030 {
2031 case WM_MSI_DIALOG_CREATE:
2032 return msi_dialog_run_message_loop( dialog );
2033 case WM_MSI_DIALOG_DESTROY:
2034 msi_dialog_destroy( dialog );
2035 return 0;
2036 }
2037 return DefWindowProcW( hwnd, msg, wParam, lParam );
2038 }
2039
2040 /* functions that interface to other modules within MSI */
2041
2042 msi_dialog *msi_dialog_create( MSIPACKAGE* package, LPCWSTR szDialogName,
2043 msi_dialog_event_handler event_handler )
2044 {
2045 MSIRECORD *rec = NULL;
2046 msi_dialog *dialog;
2047
2048 TRACE("%p %s\n", package, debugstr_w(szDialogName));
2049
2050 /* allocate the structure for the dialog to use */
2051 dialog = msi_alloc_zero( sizeof *dialog + sizeof(WCHAR)*strlenW(szDialogName) );
2052 if( !dialog )
2053 return NULL;
2054 strcpyW( dialog->name, szDialogName );
2055 msiobj_addref( &package->hdr );
2056 dialog->package = package;
2057 dialog->event_handler = event_handler;
2058 dialog->finished = 0;
2059 list_init( &dialog->controls );
2060
2061 /* verify that the dialog exists */
2062 rec = msi_get_dialog_record( dialog );
2063 if( !rec )
2064 {
2065 msiobj_release( &package->hdr );
2066 msi_free( dialog );
2067 return NULL;
2068 }
2069 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
2070 dialog->control_default = strdupW( MSI_RecordGetString( rec, 9 ) );
2071 dialog->control_cancel = strdupW( MSI_RecordGetString( rec, 10 ) );
2072 msiobj_release( &rec->hdr );
2073
2074 return dialog;
2075 }
2076
2077 static void msi_process_pending_messages( HWND hdlg )
2078 {
2079 MSG msg;
2080
2081 while( PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ) )
2082 {
2083 if( hdlg && IsDialogMessageW( hdlg, &msg ))
2084 continue;
2085 TranslateMessage( &msg );
2086 DispatchMessageW( &msg );
2087 }
2088 }
2089
2090 void msi_dialog_end_dialog( msi_dialog *dialog )
2091 {
2092 TRACE("%p\n", dialog);
2093 dialog->finished = 1;
2094 PostMessageW(dialog->hwnd, WM_NULL, 0, 0);
2095 }
2096
2097 void msi_dialog_check_messages( HANDLE handle )
2098 {
2099 DWORD r;
2100
2101 /* in threads other than the UI thread, block */
2102 if( uiThreadId != GetCurrentThreadId() )
2103 {
2104 if( handle )
2105 WaitForSingleObject( handle, INFINITE );
2106 return;
2107 }
2108
2109 /* there's two choices for the UI thread */
2110 while (1)
2111 {
2112 msi_process_pending_messages( NULL );
2113
2114 if( !handle )
2115 break;
2116
2117 /*
2118 * block here until somebody creates a new dialog or
2119 * the handle we're waiting on becomes ready
2120 */
2121 r = MsgWaitForMultipleObjects( 1, &handle, 0, INFINITE, QS_ALLINPUT );
2122 if( r == WAIT_OBJECT_0 )
2123 break;
2124 }
2125 }
2126
2127 UINT msi_dialog_run_message_loop( msi_dialog *dialog )
2128 {
2129 HWND hwnd;
2130
2131 if( !(dialog->attributes & msidbDialogAttributesVisible) )
2132 return ERROR_SUCCESS;
2133
2134 if( uiThreadId != GetCurrentThreadId() )
2135 return SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_CREATE, 0, (LPARAM) dialog );
2136
2137 /* create the dialog window, don't show it yet */
2138 hwnd = CreateWindowW( szMsiDialogClass, dialog->name, WS_OVERLAPPEDWINDOW,
2139 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
2140 NULL, NULL, NULL, dialog );
2141 if( !hwnd )
2142 {
2143 ERR("Failed to create dialog %s\n", debugstr_w( dialog->name ));
2144 return ERROR_FUNCTION_FAILED;
2145 }
2146
2147 ShowWindow( hwnd, SW_SHOW );
2148 /* UpdateWindow( hwnd ); - and causes the transparent static controls not to paint */
2149
2150 if( dialog->attributes & msidbDialogAttributesModal )
2151 {
2152 while( !dialog->finished )
2153 {
2154 MsgWaitForMultipleObjects( 0, NULL, 0, INFINITE, QS_ALLEVENTS );
2155 msi_process_pending_messages( dialog->hwnd );
2156 }
2157 }
2158 else
2159 return ERROR_IO_PENDING;
2160
2161 return ERROR_SUCCESS;
2162 }
2163
2164 void msi_dialog_do_preview( msi_dialog *dialog )
2165 {
2166 TRACE("\n");
2167 dialog->attributes |= msidbDialogAttributesVisible;
2168 dialog->attributes &= ~msidbDialogAttributesModal;
2169 msi_dialog_run_message_loop( dialog );
2170 }
2171
2172 void msi_dialog_destroy( msi_dialog *dialog )
2173 {
2174 if( uiThreadId != GetCurrentThreadId() )
2175 {
2176 SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_DESTROY, 0, (LPARAM) dialog );
2177 return;
2178 }
2179
2180 if( dialog->hwnd )
2181 ShowWindow( dialog->hwnd, SW_HIDE );
2182
2183 if( dialog->hwnd )
2184 DestroyWindow( dialog->hwnd );
2185
2186 /* destroy the list of controls */
2187 while( !list_empty( &dialog->controls ) )
2188 {
2189 msi_control *t = LIST_ENTRY( list_head( &dialog->controls ),
2190 msi_control, entry );
2191 list_remove( &t->entry );
2192 /* leave dialog->hwnd - destroying parent destroys child windows */
2193 msi_free( t->property );
2194 msi_free( t->value );
2195 if( t->hBitmap )
2196 DeleteObject( t->hBitmap );
2197 if( t->hIcon )
2198 DestroyIcon( t->hIcon );
2199 msi_free( t->tabnext );
2200 msi_free( t );
2201 }
2202
2203 /* destroy the list of fonts */
2204 while( dialog->font_list )
2205 {
2206 msi_font *t = dialog->font_list;
2207 dialog->font_list = t->next;
2208 DeleteObject( t->hfont );
2209 msi_free( t );
2210 }
2211 msi_free( dialog->default_font );
2212
2213 msi_free( dialog->control_default );
2214 msi_free( dialog->control_cancel );
2215 msiobj_release( &dialog->package->hdr );
2216 dialog->package = NULL;
2217 msi_free( dialog );
2218 }
2219
2220 BOOL msi_dialog_register_class( void )
2221 {
2222 WNDCLASSW cls;
2223
2224 ZeroMemory( &cls, sizeof cls );
2225 cls.lpfnWndProc = MSIDialog_WndProc;
2226 cls.hInstance = NULL;
2227 cls.hIcon = LoadIconW(0, (LPWSTR)IDI_APPLICATION);
2228 cls.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
2229 cls.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
2230 cls.lpszMenuName = NULL;
2231 cls.lpszClassName = szMsiDialogClass;
2232
2233 if( !RegisterClassW( &cls ) )
2234 return FALSE;
2235
2236 cls.lpfnWndProc = MSIHiddenWindowProc;
2237 cls.lpszClassName = szMsiHiddenWindow;
2238
2239 if( !RegisterClassW( &cls ) )
2240 return FALSE;
2241
2242 uiThreadId = GetCurrentThreadId();
2243
2244 hMsiHiddenWindow = CreateWindowW( szMsiHiddenWindow, NULL, WS_OVERLAPPED,
2245 0, 0, 100, 100, NULL, NULL, NULL, NULL );
2246 if( !hMsiHiddenWindow )
2247 return FALSE;
2248
2249 return TRUE;
2250 }
2251
2252 void msi_dialog_unregister_class( void )
2253 {
2254 DestroyWindow( hMsiHiddenWindow );
2255 hMsiHiddenWindow = NULL;
2256 UnregisterClassW( szMsiDialogClass, NULL );
2257 UnregisterClassW( szMsiHiddenWindow, NULL );
2258 uiThreadId = 0;
2259 }