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