Don't open device if already open.
[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
36 #include "wine/debug.h"
37 #include "wine/unicode.h"
38
39 #include "action.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(msi);
42
43
44 struct msi_control_tag;
45 typedef struct msi_control_tag msi_control;
46 typedef UINT (*msi_handler)( msi_dialog *, msi_control *, WPARAM );
47
48 struct msi_control_tag
49 {
50     struct msi_control_tag *next;
51     HWND hwnd;
52     msi_handler handler;
53     LPWSTR property;
54     LPWSTR value;
55     IPicture *pic;
56     WCHAR name[1];
57 };
58
59 typedef struct msi_font_tag
60 {
61     struct msi_font_tag *next;
62     HFONT hfont;
63     WCHAR name[1];
64 } msi_font;
65
66 struct msi_dialog_tag
67 {
68     MSIPACKAGE *package;
69     msi_dialog_event_handler event_handler;
70     BOOL finished;
71     INT scale;
72     DWORD attributes;
73     HWND hwnd;
74     LPWSTR default_font;
75     msi_font *font_list;
76     msi_control *control_list;
77     WCHAR name[1];
78 };
79
80 typedef UINT (*msi_dialog_control_func)( msi_dialog *dialog, MSIRECORD *rec );
81 struct control_handler 
82 {
83     LPCWSTR control_type;
84     msi_dialog_control_func func;
85 };
86
87 typedef struct
88 {
89     msi_dialog* dialog;
90     msi_control *parent;
91     DWORD       attributes;
92 } radio_button_group_descr;
93
94 const WCHAR szMsiDialogClass[] = {
95     'M','s','i','D','i','a','l','o','g','C','l','o','s','e','C','l','a','s','s',0
96 };
97 const WCHAR szMsiHiddenWindow[] = {
98     'M','s','i','H','i','d','d','e','n','W','i','n','d','o','w',0 };
99 const static WCHAR szStatic[] = { 'S','t','a','t','i','c',0 };
100 const static WCHAR szButton[] = { 'B','U','T','T','O','N', 0 };
101 const static WCHAR szButtonData[] = { 'M','S','I','D','A','T','A',0 };
102 static const WCHAR szText[] = { 'T','e','x','t',0 };
103 static const WCHAR szPushButton[] = { 'P','u','s','h','B','u','t','t','o','n',0 };
104 static const WCHAR szLine[] = { 'L','i','n','e',0 };
105 static const WCHAR szBitmap[] = { 'B','i','t','m','a','p',0 };
106 static const WCHAR szCheckBox[] = { 'C','h','e','c','k','B','o','x',0 };
107 static const WCHAR szScrollableText[] = {
108     'S','c','r','o','l','l','a','b','l','e','T','e','x','t',0 };
109 static const WCHAR szComboBox[] = { 'C','o','m','b','o','B','o','x',0 };
110 static const WCHAR szEdit[] = { 'E','d','i','t',0 };
111 static const WCHAR szMaskedEdit[] = { 'M','a','s','k','e','d','E','d','i','t',0 };
112 static const WCHAR szPathEdit[] = { 'P','a','t','h','E','d','i','t',0 };
113 static const WCHAR szRadioButtonGroup[] = { 
114     'R','a','d','i','o','B','u','t','t','o','n','G','r','o','u','p',0 };
115
116 static UINT msi_dialog_checkbox_handler( msi_dialog *, msi_control *, WPARAM );
117 static void msi_dialog_checkbox_sync_state( msi_dialog *, msi_control * );
118 static UINT msi_dialog_button_handler( msi_dialog *, msi_control *, WPARAM );
119 static UINT msi_dialog_edit_handler( msi_dialog *, msi_control *, WPARAM );
120 static UINT msi_dialog_radiogroup_handler( msi_dialog *, msi_control *, WPARAM param );
121 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
122
123
124 /* dialog sequencing */
125
126 #define WM_MSI_DIALOG_CREATE  (WM_USER+0x100)
127 #define WM_MSI_DIALOG_DESTROY (WM_USER+0x101)
128
129 static DWORD uiThreadId;
130 static HWND hMsiHiddenWindow;
131
132 static INT msi_dialog_scale_unit( msi_dialog *dialog, INT val )
133 {
134     return (dialog->scale * val + 5) / 10;
135 }
136
137 static msi_control *msi_dialog_find_control( msi_dialog *dialog, LPCWSTR name )
138 {
139     msi_control *control;
140
141     for( control = dialog->control_list; control; control = control->next )
142         if( !strcmpW( control->name, name ) ) /* FIXME: case sensitive? */
143             break;
144     return control;
145 }
146
147 static msi_control *msi_dialog_find_control_by_hwnd( msi_dialog *dialog, HWND hwnd )
148 {
149     msi_control *control;
150
151     for( control = dialog->control_list; control; control = control->next )
152         if( hwnd == control->hwnd )
153             break;
154     return control;
155 }
156
157 /*
158  * msi_dialog_get_style
159  *
160  * Extract the {\style} string from the front of the text to display and
161  *  update the pointer.
162  */
163 static LPWSTR msi_dialog_get_style( LPCWSTR *text )
164 {
165     LPWSTR ret = NULL;
166     LPCWSTR p = *text, q;
167     DWORD len;
168
169     if( *p++ != '{' )
170         return ret;
171     q = strchrW( p, '}' );
172     if( !q )
173         return ret;
174     *text = ++q;
175     if( *p++ != '\\' )
176         return ret;
177     len = q - p;
178     
179     ret = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
180     if( !ret )
181         return ret;
182     memcpy( ret, p, len*sizeof(WCHAR) );
183     ret[len-1] = 0;
184     return ret;
185 }
186
187 static UINT msi_dialog_add_font( MSIRECORD *rec, LPVOID param )
188 {
189     msi_dialog *dialog = param;
190     msi_font *font;
191     LPCWSTR face, name;
192     LOGFONTW lf;
193     INT style;
194     HDC hdc;
195
196     /* create a font and add it to the list */
197     name = MSI_RecordGetString( rec, 1 );
198     font = HeapAlloc( GetProcessHeap(), 0,
199                       sizeof *font + strlenW( name )*sizeof (WCHAR) );
200     strcpyW( font->name, name );
201     font->next = dialog->font_list;
202     dialog->font_list = font;
203
204     memset( &lf, 0, sizeof lf );
205     face = MSI_RecordGetString( rec, 2 );
206     lf.lfHeight = MSI_RecordGetInteger( rec, 3 );
207     style = MSI_RecordGetInteger( rec, 5 );
208     if( style & msidbTextStyleStyleBitsBold )
209         lf.lfWeight = FW_BOLD;
210     if( style & msidbTextStyleStyleBitsItalic )
211         lf.lfItalic = TRUE;
212     if( style & msidbTextStyleStyleBitsUnderline )
213         lf.lfUnderline = TRUE;
214     if( style & msidbTextStyleStyleBitsStrike )
215         lf.lfStrikeOut = TRUE;
216     lstrcpynW( lf.lfFaceName, face, LF_FACESIZE );
217
218     /* adjust the height */
219     hdc = GetDC( dialog->hwnd );
220     if (hdc)
221     {
222         lf.lfHeight = -MulDiv(lf.lfHeight, GetDeviceCaps(hdc, LOGPIXELSY), 72);
223         ReleaseDC( dialog->hwnd, hdc );
224     }
225
226     font->hfont = CreateFontIndirectW( &lf );
227
228     TRACE("Adding font style %s\n", debugstr_w(font->name) );
229
230     return ERROR_SUCCESS;
231 }
232
233 static msi_font *msi_dialog_find_font( msi_dialog *dialog, LPCWSTR name )
234 {
235     msi_font *font;
236
237     for( font = dialog->font_list; font; font = font->next )
238         if( !strcmpW( font->name, name ) )  /* FIXME: case sensitive? */
239             break;
240
241     return font;
242 }
243
244 static UINT msi_dialog_set_font( msi_dialog *dialog, HWND hwnd, LPCWSTR name )
245 {
246     msi_font *font;
247
248     font = msi_dialog_find_font( dialog, name );
249     if( font )
250         SendMessageW( hwnd, WM_SETFONT, (WPARAM) font->hfont, TRUE );
251     else
252         ERR("No font entry for %s\n", debugstr_w(name));
253     return ERROR_SUCCESS;
254 }
255
256 static UINT msi_dialog_build_font_list( msi_dialog *dialog )
257 {
258     static const WCHAR query[] = {
259       'S','E','L','E','C','T',' ','*',' ',
260       'F','R','O','M',' ','`','T','e','x','t','S','t','y','l','e','`',' ',0
261     };
262     UINT r;
263     MSIQUERY *view = NULL;
264
265     TRACE("dialog %p\n", dialog );
266
267     r = MSI_OpenQuery( dialog->package->db, &view, query );
268     if( r != ERROR_SUCCESS )
269         return r;
270
271     r = MSI_IterateRecords( view, NULL, msi_dialog_add_font, dialog );
272     msiobj_release( &view->hdr );
273
274     return r;
275 }
276
277 static msi_control *msi_dialog_create_window( msi_dialog *dialog,
278                 MSIRECORD *rec, LPCWSTR szCls, LPCWSTR name, LPCWSTR text,
279                 DWORD style, HWND parent )
280 {
281     DWORD x, y, width, height;
282     LPWSTR font = NULL, title = NULL;
283     msi_control *control;
284
285     style |= WS_CHILD | WS_GROUP;
286
287     control = HeapAlloc( GetProcessHeap(), 0,
288                          sizeof *control + strlenW(name)*sizeof(WCHAR) );
289     strcpyW( control->name, name );
290     control->next = dialog->control_list;
291     dialog->control_list = control;
292     control->handler = NULL;
293     control->property = NULL;
294     control->value = NULL;
295     control->pic = NULL;
296
297     x = MSI_RecordGetInteger( rec, 4 );
298     y = MSI_RecordGetInteger( rec, 5 );
299     width = MSI_RecordGetInteger( rec, 6 );
300     height = MSI_RecordGetInteger( rec, 7 );
301
302     x = msi_dialog_scale_unit( dialog, x );
303     y = msi_dialog_scale_unit( dialog, y );
304     width = msi_dialog_scale_unit( dialog, width );
305     height = msi_dialog_scale_unit( dialog, height );
306
307     if( text )
308     {
309         font = msi_dialog_get_style( &text );
310         deformat_string( dialog->package, text, &title );
311     }
312
313     control->hwnd = CreateWindowW( szCls, title, style,
314                           x, y, width, height, parent, NULL, NULL, NULL );
315
316     TRACE("Dialog %s control %s hwnd %p\n",
317            debugstr_w(dialog->name), debugstr_w(text), control->hwnd );
318
319     msi_dialog_set_font( dialog, control->hwnd,
320                          font ? font : dialog->default_font );
321
322     HeapFree( GetProcessHeap(), 0, font );
323     HeapFree( GetProcessHeap(), 0, title );
324
325     return control;
326 }
327
328 /* called from the Control Event subscription code */
329 void msi_dialog_handle_event( msi_dialog* dialog, LPCWSTR control, 
330                               LPCWSTR attribute, MSIRECORD *rec )
331 {
332     msi_control* ctrl;
333     LPCWSTR text;
334
335     ctrl = msi_dialog_find_control( dialog, control );
336     if (!ctrl)
337         return;
338     if( lstrcmpW(attribute, szText) )
339         return;
340     text = MSI_RecordGetString( rec , 1 );
341     SetWindowTextW( ctrl->hwnd, text );
342 }
343
344 void msi_dialog_map_events(msi_dialog* dialog, LPCWSTR control)
345 {
346     static WCHAR Query[] = {
347         'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
348          '`','E','v','e','n','t','M','a','p','p','i','n','g','`',' ',
349         'W','H','E','R','E',' ',
350          '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
351         'A','N','D',' ',
352          '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',0
353     };
354     MSIRECORD *row;
355     LPCWSTR event, attribute;
356
357     row = MSI_QueryGetRecord( dialog->package->db, Query, dialog->name, control );
358     if (!row)
359         return;
360
361     event = MSI_RecordGetString( row, 3 );
362     attribute = MSI_RecordGetString( row, 4 );
363     ControlEvent_SubscribeToEvent( dialog->package, event, control, attribute );
364     msiobj_release( &row->hdr );
365 }
366
367 /* everything except radio buttons */
368 static msi_control *msi_dialog_add_control( msi_dialog *dialog,
369                 MSIRECORD *rec, LPCWSTR szCls, DWORD style )
370 {
371     DWORD attributes;
372     LPCWSTR text, name;
373
374     name = MSI_RecordGetString( rec, 2 );
375     attributes = MSI_RecordGetInteger( rec, 8 );
376     text = MSI_RecordGetString( rec, 10 );
377     if( attributes & 1 )
378         style |= WS_VISIBLE;
379     if( ~attributes & 2 )
380         style |= WS_DISABLED;
381
382     msi_dialog_map_events(dialog, name);
383
384     return msi_dialog_create_window( dialog, rec, szCls, name, text,
385                                      style, dialog->hwnd );
386 }
387
388 static UINT msi_dialog_text_control( msi_dialog *dialog, MSIRECORD *rec )
389 {
390     TRACE("%p %p\n", dialog, rec);
391
392     msi_dialog_add_control( dialog, rec, szStatic, 0 );
393     return ERROR_SUCCESS;
394 }
395
396 static UINT msi_dialog_button_control( msi_dialog *dialog, MSIRECORD *rec )
397 {
398     msi_control *control;
399
400     TRACE("%p %p\n", dialog, rec);
401
402     control = msi_dialog_add_control( dialog, rec, szButton, 0 );
403     control->handler = msi_dialog_button_handler;
404
405     return ERROR_SUCCESS;
406 }
407
408 static LPWSTR msi_get_checkbox_value( msi_dialog *dialog, LPCWSTR prop )
409 {
410     const static WCHAR query[] = {
411         'S','E','L','E','C','T',' ','*',' ',
412         'F','R','O','M',' ','`','C','h','e','c','k','B','o','x',' ','`',
413         'W','H','E','R','E',' ',
414         '`','P','r','o','p','e','r','t','y','`',' ','=',' ',
415         '\'','%','s','\'',0
416     };
417     MSIRECORD *rec = NULL;
418     LPCWSTR val = NULL;
419     LPWSTR ret = NULL;
420
421     /* find if there is a value associated with the checkbox */
422     rec = MSI_QueryGetRecord( dialog->package->db, query, prop );
423     if (!rec)
424         return ret;
425
426     val = MSI_RecordGetString( rec, 2 );
427     if (val)
428     {
429         deformat_string( dialog->package, val, &ret );
430         if( ret && !ret[0] )
431         {
432             HeapFree( GetProcessHeap(), 0, ret );
433             ret = NULL;
434         }
435     }
436     msiobj_release( &rec->hdr );
437     if (ret)
438         return ret;
439
440     ret = load_dynamic_property(dialog->package, prop, NULL);
441     if( ret && !ret[0] )
442     {
443         HeapFree( GetProcessHeap(), 0, ret );
444         ret = NULL;
445     }
446
447     return ret;
448 }
449
450 static UINT msi_dialog_checkbox_control( msi_dialog *dialog, MSIRECORD *rec )
451 {
452     msi_control *control;
453     LPCWSTR prop;
454
455     TRACE("%p %p\n", dialog, rec);
456
457     control = msi_dialog_add_control( dialog, rec, szButton,
458                                       BS_CHECKBOX | BS_MULTILINE );
459     control->handler = msi_dialog_checkbox_handler;
460     prop = MSI_RecordGetString( rec, 9 );
461     if( prop )
462     {
463         control->property = strdupW( prop );
464         control->value = msi_get_checkbox_value( dialog, prop );
465         TRACE("control %s value %s\n", debugstr_w(control->property),
466               debugstr_w(control->value));
467     }
468     msi_dialog_checkbox_sync_state( dialog, control );
469
470     return ERROR_SUCCESS;
471 }
472
473 static UINT msi_dialog_line_control( msi_dialog *dialog, MSIRECORD *rec )
474 {
475     TRACE("%p %p\n", dialog, rec);
476
477     msi_dialog_add_control( dialog, rec, szStatic, SS_ETCHEDHORZ | SS_SUNKEN );
478     return ERROR_SUCCESS;
479 }
480
481 static UINT msi_dialog_scrolltext_control( msi_dialog *dialog, MSIRECORD *rec )
482 {
483     const static WCHAR szEdit[] = { 'E','D','I','T',0 };
484
485     FIXME("%p %p\n", dialog, rec);
486
487     msi_dialog_add_control( dialog, rec, szEdit, WS_BORDER |
488                  ES_MULTILINE | WS_VSCROLL | ES_READONLY | ES_AUTOVSCROLL );
489
490     return ERROR_SUCCESS;
491 }
492
493 static UINT msi_load_bitmap( MSIDATABASE *db, LPCWSTR name, IPicture **pic )
494 {
495     const static WCHAR query[] = {
496         's','e','l','e','c','t',' ','*',' ',
497         'f','r','o','m',' ','B','i','n','a','r','y',' ',
498         'w','h','e','r','e',' ',
499             '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0
500     };
501     MSIRECORD *rec = NULL;
502     IStream *stm = NULL;
503     UINT r;
504
505     rec = MSI_QueryGetRecord( db, query, name );
506     if( !rec )
507         return ERROR_FUNCTION_FAILED;
508
509     r = MSI_RecordGetIStream( rec, 2, &stm );
510     msiobj_release( &rec->hdr );
511     if( r != ERROR_SUCCESS )
512         return r;
513
514     r = OleLoadPicture( stm, 0, TRUE, &IID_IPicture, (LPVOID*) pic );
515     IStream_Release( stm );
516     if( FAILED( r ) )
517         return ERROR_FUNCTION_FAILED;
518
519     return ERROR_SUCCESS;
520 }
521
522 static UINT msi_dialog_bitmap_control( msi_dialog *dialog, MSIRECORD *rec )
523 {
524     IPicture *pic = NULL;
525     msi_control *control;
526     OLE_HANDLE hBitmap = 0;
527     LPCWSTR text;
528     UINT r;
529
530     control = msi_dialog_add_control( dialog, rec, szStatic,
531                             SS_BITMAP | SS_LEFT | SS_CENTERIMAGE );
532     text = MSI_RecordGetString( rec, 10 );
533     r = msi_load_bitmap( dialog->package->db, text, &pic );
534     if( r == ERROR_SUCCESS )
535     {
536         r = IPicture_get_Handle( pic, &hBitmap );
537         if( SUCCEEDED( r ) )
538             SendMessageW( control->hwnd, STM_SETIMAGE, IMAGE_BITMAP, hBitmap );
539         control->pic = pic;
540     }
541     
542     return ERROR_SUCCESS;
543 }
544
545 static UINT msi_dialog_combo_control( msi_dialog *dialog, MSIRECORD *rec )
546 {
547     static const WCHAR szCombo[] = { 'C','O','M','B','O','B','O','X',0 };
548
549     msi_dialog_add_control( dialog, rec, szCombo,
550                             SS_BITMAP | SS_LEFT | SS_CENTERIMAGE );
551     return ERROR_SUCCESS;
552 }
553
554 static UINT msi_dialog_edit_control( msi_dialog *dialog, MSIRECORD *rec )
555 {
556     const static WCHAR szEdit[] = { 'E','D','I','T',0 };
557     msi_control *control;
558     LPCWSTR prop;
559     LPWSTR val;
560
561     control = msi_dialog_add_control( dialog, rec, szEdit, WS_BORDER );
562     control->handler = msi_dialog_edit_handler;
563     prop = MSI_RecordGetString( rec, 9 );
564     if( prop )
565         control->property = strdupW( prop );
566     val = load_dynamic_property( dialog->package, control->property, NULL );
567     SetWindowTextW( control->hwnd, val );
568     HeapFree( GetProcessHeap(), 0, val );
569     return ERROR_SUCCESS;
570 }
571
572 static UINT msi_dialog_pathedit_control( msi_dialog *dialog, MSIRECORD *rec )
573 {
574     FIXME("not implemented properly\n");
575     return msi_dialog_edit_control( dialog, rec );
576 }
577
578 /* radio buttons are a bit different from normal controls */
579 static UINT msi_dialog_create_radiobutton( MSIRECORD *rec, LPVOID param )
580 {
581     radio_button_group_descr *group = (radio_button_group_descr *)param;
582     msi_dialog *dialog = group->dialog;
583     msi_control *control;
584     LPCWSTR prop, text, name;
585     DWORD style;
586     DWORD attributes = group->attributes;
587
588     style = WS_CHILD | BS_AUTORADIOBUTTON | BS_MULTILINE;
589     name = MSI_RecordGetString( rec, 3 );
590     text = MSI_RecordGetString( rec, 8 );
591     if( attributes & 1 )
592         style |= WS_VISIBLE;
593     if( ~attributes & 2 )
594         style |= WS_DISABLED;
595
596     control = msi_dialog_create_window( dialog, rec, szButton, name, text,
597                                         style, group->parent->hwnd );
598     control->handler = msi_dialog_radiogroup_handler;
599
600     prop = MSI_RecordGetString( rec, 1 );
601     if( prop )
602         control->property = strdupW( prop );
603
604     return ERROR_SUCCESS;
605 }
606
607 static UINT msi_dialog_radiogroup_control( msi_dialog *dialog, MSIRECORD *rec )
608 {
609     static const WCHAR query[] = {
610         'S','E','L','E','C','T',' ','*',' ',
611         'F','R','O','M',' ','R','a','d','i','o','B','u','t','t','o','n',' ',
612         'W','H','E','R','E',' ',
613            '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
614     UINT r;
615     LPCWSTR prop;
616     msi_control *control;
617     MSIQUERY *view = NULL;
618     radio_button_group_descr group;
619     MSIPACKAGE *package = dialog->package;
620
621     prop = MSI_RecordGetString( rec, 9 );
622
623     TRACE("%p %p %s\n", dialog, rec, debugstr_w( prop ));
624
625     /* Create parent group box to hold radio buttons */
626     control = msi_dialog_add_control( dialog, rec, szButton, BS_OWNERDRAW );
627
628     if (control->hwnd)
629     {
630         WNDPROC oldproc = (WNDPROC) SetWindowLongPtrW(control->hwnd, GWLP_WNDPROC,
631             (LONG_PTR)MSIRadioGroup_WndProc);
632         SetPropW(control->hwnd, szButtonData, oldproc);
633     }
634
635     if( prop )
636         control->property = strdupW( prop );
637
638     /* query the Radio Button table for all control in this group */
639     r = MSI_OpenQuery( package->db, &view, query, prop );
640     if( r != ERROR_SUCCESS )
641     {
642         ERR("query failed for dialog %s radio group %s\n", 
643             debugstr_w(dialog->name), debugstr_w(prop));
644         return ERROR_INVALID_PARAMETER;
645     }
646
647     group.dialog = dialog;
648     group.parent = control;
649     group.attributes = MSI_RecordGetInteger( rec, 8 );
650
651     r = MSI_IterateRecords( view, 0, msi_dialog_create_radiobutton, &group );
652     msiobj_release( &view->hdr );
653
654     return r;
655 }
656
657 struct control_handler msi_dialog_handler[] =
658 {
659     { szText, msi_dialog_text_control },
660     { szPushButton, msi_dialog_button_control },
661     { szLine, msi_dialog_line_control },
662     { szBitmap, msi_dialog_bitmap_control },
663     { szCheckBox, msi_dialog_checkbox_control },
664     { szScrollableText, msi_dialog_scrolltext_control },
665     { szComboBox, msi_dialog_combo_control },
666     { szEdit, msi_dialog_edit_control },
667     { szMaskedEdit, msi_dialog_edit_control },
668     { szPathEdit, msi_dialog_pathedit_control },
669     { szRadioButtonGroup, msi_dialog_radiogroup_control },
670 };
671
672 #define NUM_CONTROL_TYPES (sizeof msi_dialog_handler/sizeof msi_dialog_handler[0])
673
674 static UINT msi_dialog_create_controls( MSIRECORD *rec, LPVOID param )
675 {
676     msi_dialog *dialog = param;
677     LPCWSTR control_type;
678     UINT i;
679
680     /* find and call the function that can create this type of control */
681     control_type = MSI_RecordGetString( rec, 3 );
682     for( i=0; i<NUM_CONTROL_TYPES; i++ )
683         if (!strcmpiW( msi_dialog_handler[i].control_type, control_type ))
684             break;
685     if( i != NUM_CONTROL_TYPES )
686         msi_dialog_handler[i].func( dialog, rec );
687     else
688         ERR("no handler for element type %s\n", debugstr_w(control_type));
689
690     return ERROR_SUCCESS;
691 }
692
693 static UINT msi_dialog_fill_controls( msi_dialog *dialog )
694 {
695     static const WCHAR query[] = {
696         'S','E','L','E','C','T',' ','*',' ',
697         'F','R','O','M',' ','C','o','n','t','r','o','l',' ',
698         'W','H','E','R','E',' ',
699            '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
700     UINT r;
701     MSIQUERY *view = NULL;
702     MSIPACKAGE *package = dialog->package;
703
704     TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
705
706     /* query the Control table for all the elements of the control */
707     r = MSI_OpenQuery( package->db, &view, query, dialog->name );
708     if( r != ERROR_SUCCESS )
709     {
710         ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
711         return ERROR_INVALID_PARAMETER;
712     }
713
714     r = MSI_IterateRecords( view, 0, msi_dialog_create_controls, dialog );
715     msiobj_release( &view->hdr );
716
717     return r;
718 }
719
720 static UINT msi_dialog_set_control_condition( MSIRECORD *rec, LPVOID param )
721 {
722     static const WCHAR szHide[] = { 'H','i','d','e',0 };
723     static const WCHAR szShow[] = { 'S','h','o','w',0 };
724     static const WCHAR szDisable[] = { 'D','i','s','a','b','l','e',0 };
725     static const WCHAR szEnable[] = { 'E','n','a','b','l','e',0 };
726     msi_dialog *dialog = param;
727     msi_control *control;
728     LPCWSTR name, action, condition;
729     UINT r;
730
731     name = MSI_RecordGetString( rec, 2 );
732     action = MSI_RecordGetString( rec, 3 );
733     condition = MSI_RecordGetString( rec, 4 );
734     r = MSI_EvaluateConditionW( dialog->package, condition );
735     control = msi_dialog_find_control( dialog, name );
736     if( r && control )
737     {
738         TRACE("%s control %s\n", debugstr_w(action), debugstr_w(name));
739
740         /* FIXME: case sensitive? */
741         if(!strcmpW(action, szHide))
742             ShowWindow(control->hwnd, SW_HIDE);
743         else if(!strcmpW(action, szShow))
744             ShowWindow(control->hwnd, SW_SHOW);
745         else if(!strcmpW(action, szDisable))
746             EnableWindow(control->hwnd, FALSE);
747         else if(!strcmpW(action, szEnable))
748             EnableWindow(control->hwnd, TRUE);
749         else
750             FIXME("Unhandled action %s\n", debugstr_w(action));
751     }
752
753     return ERROR_SUCCESS;
754 }
755
756 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog )
757 {
758     static const WCHAR query[] = {
759       'S','E','L','E','C','T',' ','*',' ',
760       'F','R','O','M',' ',
761         'C','o','n','t','r','o','l','C','o','n','d','i','t','i','o','n',' ',
762       'W','H','E','R','E',' ',
763         '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0
764     };
765     UINT r;
766     MSIQUERY *view = NULL;
767     MSIPACKAGE *package = dialog->package;
768
769     TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
770
771     /* query the Control table for all the elements of the control */
772     r = MSI_OpenQuery( package->db, &view, query, dialog->name );
773     if( r != ERROR_SUCCESS )
774     {
775         ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
776         return ERROR_INVALID_PARAMETER;
777     }
778
779     r = MSI_IterateRecords( view, 0, msi_dialog_set_control_condition, dialog );
780     msiobj_release( &view->hdr );
781
782     return r;
783 }
784
785 /* figure out the height of 10 point MS Sans Serif */
786 static INT msi_dialog_get_sans_serif_height( HWND hwnd )
787 {
788     static const WCHAR szSansSerif[] = {
789         'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0 };
790     LOGFONTW lf;
791     TEXTMETRICW tm;
792     BOOL r;
793     LONG height = 0;
794     HFONT hFont, hOldFont;
795     HDC hdc;
796
797     hdc = GetDC( hwnd );
798     if (hdc)
799     {
800         memset( &lf, 0, sizeof lf );
801         lf.lfHeight = MulDiv(10, GetDeviceCaps(hdc, LOGPIXELSY), 72);
802         strcpyW( lf.lfFaceName, szSansSerif );
803         hFont = CreateFontIndirectW(&lf);
804         if (hFont)
805         {
806             hOldFont = SelectObject( hdc, hFont );
807             r = GetTextMetricsW( hdc, &tm );
808             if (r)
809                 height = tm.tmHeight;
810             SelectObject( hdc, hOldFont );
811             DeleteObject( hFont );
812         }
813         ReleaseDC( hwnd, hdc );
814     }
815     return height;
816 }
817
818 /* fetch the associated record from the Dialog table */
819 static MSIRECORD *msi_get_dialog_record( msi_dialog *dialog )
820 {
821     static const WCHAR query[] = {
822         'S','E','L','E','C','T',' ','*',' ',
823         'F','R','O','M',' ','D','i','a','l','o','g',' ',
824         'W','H','E','R','E',' ',
825            '`','D','i','a','l','o','g','`',' ','=',' ','\'','%','s','\'',0};
826     MSIPACKAGE *package = dialog->package;
827     MSIRECORD *rec = NULL;
828
829     TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
830
831     rec = MSI_QueryGetRecord( package->db, query, dialog->name );
832     if( !rec )
833         ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
834
835     return rec;
836 }
837
838 static void msi_dialog_adjust_dialog_size( msi_dialog *dialog, LPSIZE sz )
839 {
840     RECT rect;
841     LONG style;
842
843     /* turn the client size into the window rectangle */
844     rect.left = 0;
845     rect.top = 0;
846     rect.right = msi_dialog_scale_unit( dialog, sz->cx );
847     rect.bottom = msi_dialog_scale_unit( dialog, sz->cy );
848     style = GetWindowLongPtrW( dialog->hwnd, GWL_STYLE );
849     AdjustWindowRect( &rect, style, FALSE );
850     sz->cx = rect.right - rect.left;
851     sz->cy = rect.bottom - rect.top;
852 }
853
854 static LRESULT msi_dialog_oncreate( HWND hwnd, LPCREATESTRUCTW cs )
855 {
856     static const WCHAR df[] = {
857         'D','e','f','a','u','l','t','U','I','F','o','n','t',0 };
858     msi_dialog *dialog = (msi_dialog*) cs->lpCreateParams;
859     MSIRECORD *rec = NULL;
860     LPCWSTR text;
861     LPWSTR title = NULL;
862     SIZE size;
863
864     TRACE("%p %p\n", dialog, dialog->package);
865
866     dialog->hwnd = hwnd;
867     SetWindowLongPtrW( hwnd, GWLP_USERDATA, (LONG_PTR) dialog );
868
869     rec = msi_get_dialog_record( dialog );
870     if( !rec )
871     {
872         TRACE("No record found for dialog %s\n", debugstr_w(dialog->name));
873         return -1;
874     }
875
876     dialog->scale = msi_dialog_get_sans_serif_height(dialog->hwnd);
877
878     size.cx = MSI_RecordGetInteger( rec, 4 );
879     size.cy = MSI_RecordGetInteger( rec, 5 );
880     msi_dialog_adjust_dialog_size( dialog, &size );
881
882     dialog->attributes = MSI_RecordGetInteger( rec, 6 );
883     text = MSI_RecordGetString( rec, 7 );
884
885     dialog->default_font = load_dynamic_property( dialog->package, df, NULL );
886
887     deformat_string( dialog->package, text, &title );
888     SetWindowTextW( hwnd, title );
889     SetWindowPos( hwnd, 0, 0, 0, size.cx, size.cy,
890                   SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW );
891
892     HeapFree( GetProcessHeap(), 0, title );
893     msiobj_release( &rec->hdr );
894
895     msi_dialog_build_font_list( dialog );
896     msi_dialog_fill_controls( dialog );
897     msi_dialog_evaluate_control_conditions( dialog );
898
899     return 0;
900 }
901
902 static UINT msi_dialog_send_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
903 {
904     LPWSTR event_fmt = NULL, arg_fmt = NULL;
905
906     TRACE("Sending control event %s %s\n", debugstr_w(event), debugstr_w(arg));
907
908     deformat_string( dialog->package, event, &event_fmt );
909     deformat_string( dialog->package, arg, &arg_fmt );
910
911     dialog->event_handler( dialog->package, event_fmt, arg_fmt, dialog );
912
913     HeapFree( GetProcessHeap(), 0, event_fmt );
914     HeapFree( GetProcessHeap(), 0, arg_fmt );
915
916     return ERROR_SUCCESS;
917 }
918
919 static UINT msi_dialog_set_property( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
920 {
921     static const WCHAR szNullArg[] = { '{','}',0 };
922     LPWSTR p, prop, arg_fmt = NULL;
923     UINT len;
924
925     len = strlenW(event);
926     prop = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR));
927     strcpyW( prop, &event[1] );
928     p = strchrW( prop, ']' );
929     if( p && p[1] == 0 )
930     {
931         *p = 0;
932         if( strcmpW( szNullArg, arg ) )
933             deformat_string( dialog->package, arg, &arg_fmt );
934         MSI_SetPropertyW( dialog->package, prop, arg_fmt );
935     }
936     else
937         ERR("Badly formatted property string - what happens?\n");
938     HeapFree( GetProcessHeap(), 0, prop );
939     return ERROR_SUCCESS;
940 }
941
942 static UINT msi_dialog_control_event( MSIRECORD *rec, LPVOID param )
943 {
944     msi_dialog *dialog = param;
945     LPCWSTR condition, event, arg;
946     UINT r;
947
948     condition = MSI_RecordGetString( rec, 5 );
949     r = MSI_EvaluateConditionW( dialog->package, condition );
950     if( r )
951     {
952         event = MSI_RecordGetString( rec, 3 );
953         arg = MSI_RecordGetString( rec, 4 );
954         if( event[0] == '[' )
955             msi_dialog_set_property( dialog, event, arg );
956         else
957             msi_dialog_send_event( dialog, event, arg );
958     }
959
960     return ERROR_SUCCESS;
961 }
962
963 static UINT msi_dialog_button_handler( msi_dialog *dialog,
964                                        msi_control *control, WPARAM param )
965 {
966     static const WCHAR query[] = {
967       'S','E','L','E','C','T',' ','*',' ',
968       'F','R','O','M',' ','C','o','n','t','r','o','l','E','v','e','n','t',' ',
969       'W','H','E','R','E',' ',
970          '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
971       'A','N','D',' ',
972          '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',' ',
973       'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','i','n','g','`',0
974     };
975     MSIQUERY *view = NULL;
976     UINT r;
977
978     if( HIWORD(param) != BN_CLICKED )
979         return ERROR_SUCCESS;
980
981     r = MSI_OpenQuery( dialog->package->db, &view, query,
982                        dialog->name, control->name );
983     if( r != ERROR_SUCCESS )
984     {
985         ERR("query failed\n");
986         return 0;
987     }
988
989     r = MSI_IterateRecords( view, 0, msi_dialog_control_event, dialog );
990     msiobj_release( &view->hdr );
991
992     return r;
993 }
994
995 static UINT msi_dialog_get_checkbox_state( msi_dialog *dialog,
996                 msi_control *control )
997 {
998     WCHAR state[2] = { 0 };
999     DWORD sz = 2;
1000
1001     MSI_GetPropertyW( dialog->package, control->property, state, &sz );
1002     return state[0] ? 1 : 0;
1003 }
1004
1005 static void msi_dialog_set_checkbox_state( msi_dialog *dialog,
1006                 msi_control *control, UINT state )
1007 {
1008     static const WCHAR szState[] = { '1', 0 };
1009     LPCWSTR val;
1010
1011     /* if uncheck then the property is set to NULL */
1012     if (!state)
1013     {
1014         MSI_SetPropertyW( dialog->package, control->property, NULL );
1015         return;
1016     }
1017
1018     /* check for a custom state */
1019     if (control->value && control->value[0])
1020         val = control->value;
1021     else
1022         val = szState;
1023
1024     MSI_SetPropertyW( dialog->package, control->property, val );
1025 }
1026
1027 static void msi_dialog_checkbox_sync_state( msi_dialog *dialog,
1028                 msi_control *control )
1029 {
1030     UINT state;
1031
1032     state = msi_dialog_get_checkbox_state( dialog, control );
1033     SendMessageW( control->hwnd, BM_SETCHECK,
1034                   state ? BST_CHECKED : BST_UNCHECKED, 0 );
1035 }
1036
1037 static UINT msi_dialog_checkbox_handler( msi_dialog *dialog,
1038                 msi_control *control, WPARAM param )
1039 {
1040     UINT state;
1041
1042     if( HIWORD(param) != BN_CLICKED )
1043         return ERROR_SUCCESS;
1044
1045     TRACE("clicked checkbox %s, set %s\n", debugstr_w(control->name),
1046           debugstr_w(control->property));
1047
1048     state = msi_dialog_get_checkbox_state( dialog, control );
1049     state = state ? 0 : 1;
1050     msi_dialog_set_checkbox_state( dialog, control, state );
1051     msi_dialog_checkbox_sync_state( dialog, control );
1052
1053     return msi_dialog_button_handler( dialog, control, param );
1054 }
1055
1056 static UINT msi_dialog_edit_handler( msi_dialog *dialog,
1057                 msi_control *control, WPARAM param )
1058 {
1059     UINT sz, r;
1060     LPWSTR buf;
1061
1062     if( HIWORD(param) != EN_CHANGE )
1063         return ERROR_SUCCESS;
1064
1065     TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
1066           debugstr_w(control->property));
1067
1068     sz = 0x20;
1069     buf = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
1070     while( buf )
1071     {
1072         r = GetWindowTextW( control->hwnd, buf, sz );
1073         if( r < (sz-1) )
1074             break;
1075             sz *= 2;
1076         buf = HeapReAlloc( GetProcessHeap(), 0, buf, sz*sizeof(WCHAR) );
1077     }
1078
1079     MSI_SetPropertyW( dialog->package, control->property, buf );
1080
1081     HeapFree( GetProcessHeap(), 0, buf );
1082
1083     return ERROR_SUCCESS;
1084 }
1085
1086 static UINT msi_dialog_radiogroup_handler( msi_dialog *dialog,
1087                 msi_control *control, WPARAM param )
1088 {
1089     if( HIWORD(param) != BN_CLICKED )
1090         return ERROR_SUCCESS;
1091
1092     TRACE("clicked radio button %s, set %s\n", debugstr_w(control->name),
1093           debugstr_w(control->property));
1094
1095     MSI_SetPropertyW( dialog->package, control->property, control->name );
1096
1097     return msi_dialog_button_handler( dialog, control, param );
1098 }
1099
1100 static LRESULT msi_dialog_oncommand( msi_dialog *dialog, WPARAM param, HWND hwnd )
1101 {
1102     msi_control *control;
1103
1104     TRACE("%p %p %08x\n", dialog, hwnd, param);
1105
1106     control = msi_dialog_find_control_by_hwnd( dialog, hwnd );
1107     if( control )
1108     {
1109         if( control->handler )
1110         {
1111             control->handler( dialog, control, param );
1112             msi_dialog_evaluate_control_conditions( dialog );
1113         }
1114     }
1115     else
1116         ERR("button click from nowhere\n");
1117     return 0;
1118 }
1119
1120 static LRESULT WINAPI MSIDialog_WndProc( HWND hwnd, UINT msg,
1121                 WPARAM wParam, LPARAM lParam )
1122 {
1123     msi_dialog *dialog = (LPVOID) GetWindowLongPtrW( hwnd, GWLP_USERDATA );
1124
1125     TRACE("0x%04x\n", msg);
1126
1127     switch (msg)
1128     {
1129     case WM_CREATE:
1130         return msi_dialog_oncreate( hwnd, (LPCREATESTRUCTW)lParam );
1131
1132     case WM_COMMAND:
1133         return msi_dialog_oncommand( dialog, wParam, (HWND)lParam );
1134
1135     case WM_DESTROY:
1136         dialog->hwnd = NULL;
1137         return 0;
1138     }
1139     return DefWindowProcW(hwnd, msg, wParam, lParam);
1140 }
1141
1142 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1143 {
1144     WNDPROC oldproc = (WNDPROC) GetPropW(hWnd, szButtonData);
1145
1146     TRACE("hWnd %p msg %04x wParam 0x%08x lParam 0x%08lx\n", hWnd, msg, wParam, lParam);
1147
1148     if (msg == WM_COMMAND) /* Forward notifications to dialog */
1149         SendMessageW(GetParent(hWnd), msg, wParam, lParam);
1150
1151     return CallWindowProcW(oldproc, hWnd, msg, wParam, lParam);
1152 }
1153
1154 static LRESULT WINAPI MSIHiddenWindowProc( HWND hwnd, UINT msg,
1155                 WPARAM wParam, LPARAM lParam )
1156 {
1157     msi_dialog *dialog = (msi_dialog*) lParam;
1158
1159     TRACE("%d %p\n", msg, dialog);
1160
1161     switch (msg)
1162     {
1163     case WM_MSI_DIALOG_CREATE:
1164         return msi_dialog_run_message_loop( dialog );
1165     case WM_MSI_DIALOG_DESTROY:
1166         msi_dialog_destroy( dialog );
1167         return 0;
1168     }
1169     return DefWindowProcW( hwnd, msg, wParam, lParam );
1170 }
1171
1172 /* functions that interface to other modules within MSI */
1173
1174 msi_dialog *msi_dialog_create( MSIPACKAGE* package, LPCWSTR szDialogName,
1175                                 msi_dialog_event_handler event_handler )
1176 {
1177     MSIRECORD *rec = NULL;
1178     msi_dialog *dialog;
1179
1180     TRACE("%p %s\n", package, debugstr_w(szDialogName));
1181
1182     /* allocate the structure for the dialog to use */
1183     dialog = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
1184                         sizeof *dialog + sizeof(WCHAR)*strlenW(szDialogName) );
1185     if( !dialog )
1186         return NULL;
1187     strcpyW( dialog->name, szDialogName );
1188     msiobj_addref( &package->hdr );
1189     dialog->package = package;
1190     dialog->event_handler = event_handler;
1191     dialog->finished = 0;
1192
1193     /* verify that the dialog exists */
1194     rec = msi_get_dialog_record( dialog );
1195     if( !rec )
1196     {
1197         HeapFree( GetProcessHeap(), 0, dialog );
1198         return NULL;
1199     }
1200     dialog->attributes = MSI_RecordGetInteger( rec, 6 );
1201     msiobj_release( &rec->hdr );
1202
1203     return dialog;
1204 }
1205
1206 static void msi_process_pending_messages(void)
1207 {
1208     MSG msg;
1209
1210     while( PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ) )
1211     {
1212         TranslateMessage( &msg );
1213         DispatchMessageW( &msg );
1214     }
1215 }
1216
1217 void msi_dialog_end_dialog( msi_dialog *dialog )
1218 {
1219     TRACE("%p\n", dialog);
1220     dialog->finished = 1;
1221     PostMessageW(dialog->hwnd, WM_NULL, 0, 0);
1222 }
1223
1224 void msi_dialog_check_messages( HANDLE handle )
1225 {
1226     DWORD r;
1227
1228     /* in threads other than the UI thread, block */
1229     if( uiThreadId != GetCurrentThreadId() )
1230     {
1231         if( handle )
1232             WaitForSingleObject( handle, INFINITE );
1233         return;
1234     }
1235
1236     /* there's two choices for the UI thread */
1237     while (1)
1238     {
1239         msi_process_pending_messages();
1240
1241         if( !handle )
1242             break;
1243
1244         /*
1245          * block here until somebody creates a new dialog or
1246          * the handle we're waiting on becomes ready
1247          */
1248         r = MsgWaitForMultipleObjects( 1, &handle, 0, INFINITE, QS_ALLINPUT );
1249         if( r == WAIT_OBJECT_0 )
1250             break;
1251     }
1252 }
1253
1254 UINT msi_dialog_run_message_loop( msi_dialog *dialog )
1255 {
1256     HWND hwnd;
1257
1258     if( !(dialog->attributes & msidbDialogAttributesVisible) )
1259         return ERROR_SUCCESS;
1260
1261     if( uiThreadId != GetCurrentThreadId() )
1262         return SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_CREATE, 0, (LPARAM) dialog );
1263
1264     /* create the dialog window, don't show it yet */
1265     hwnd = CreateWindowW( szMsiDialogClass, dialog->name, WS_OVERLAPPEDWINDOW,
1266                      CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
1267                      NULL, NULL, NULL, dialog );
1268     if( !hwnd )
1269     {
1270         ERR("Failed to create dialog %s\n", debugstr_w( dialog->name ));
1271         return ERROR_FUNCTION_FAILED;
1272     }
1273
1274     ShowWindow( hwnd, SW_SHOW );
1275     UpdateWindow( hwnd );
1276
1277     if( dialog->attributes & msidbDialogAttributesModal )
1278     {
1279         while( !dialog->finished )
1280         {
1281             MsgWaitForMultipleObjects( 0, NULL, 0, INFINITE, QS_ALLEVENTS );
1282             msi_process_pending_messages();
1283         }
1284     }
1285     else
1286         return ERROR_IO_PENDING;
1287
1288     return ERROR_SUCCESS;
1289 }
1290
1291 void msi_dialog_do_preview( msi_dialog *dialog )
1292 {
1293     TRACE("\n");
1294     dialog->attributes |= msidbDialogAttributesVisible;
1295     dialog->attributes &= ~msidbDialogAttributesModal;
1296     msi_dialog_run_message_loop( dialog );
1297 }
1298
1299 void msi_dialog_destroy( msi_dialog *dialog )
1300 {
1301     if( uiThreadId != GetCurrentThreadId() )
1302     {
1303         SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_DESTROY, 0, (LPARAM) dialog );
1304         return;
1305     }
1306
1307     if( dialog->hwnd )
1308         ShowWindow( dialog->hwnd, SW_HIDE );
1309     
1310     /* destroy the list of controls */
1311     while( dialog->control_list )
1312     {
1313         msi_control *t = dialog->control_list;
1314         dialog->control_list = t->next;
1315         /* leave dialog->hwnd - destroying parent destroys child windows */
1316         HeapFree( GetProcessHeap(), 0, t->property );
1317         HeapFree( GetProcessHeap(), 0, t->value );
1318         if( t->pic )
1319             IPicture_Release( t->pic );
1320         HeapFree( GetProcessHeap(), 0, t );
1321     }
1322
1323     /* destroy the list of fonts */
1324     while( dialog->font_list )
1325     {
1326         msi_font *t = dialog->font_list;
1327         dialog->font_list = t->next;
1328         DeleteObject( t->hfont );
1329         HeapFree( GetProcessHeap(), 0, t );
1330     }
1331     HeapFree( GetProcessHeap(), 0, dialog->default_font );
1332
1333     if( dialog->hwnd )
1334         DestroyWindow( dialog->hwnd );
1335
1336     msiobj_release( &dialog->package->hdr );
1337     dialog->package = NULL;
1338     HeapFree( GetProcessHeap(), 0, dialog );
1339 }
1340
1341 BOOL msi_dialog_register_class( void )
1342 {
1343     WNDCLASSW cls;
1344
1345     ZeroMemory( &cls, sizeof cls );
1346     cls.lpfnWndProc   = MSIDialog_WndProc;
1347     cls.hInstance     = NULL;
1348     cls.hIcon         = LoadIconW(0, (LPWSTR)IDI_APPLICATION);
1349     cls.hCursor       = LoadCursorW(0, (LPWSTR)IDC_ARROW);
1350     cls.hbrBackground = (HBRUSH)(COLOR_WINDOW);
1351     cls.lpszMenuName  = NULL;
1352     cls.lpszClassName = szMsiDialogClass;
1353
1354     if( !RegisterClassW( &cls ) )
1355         return FALSE;
1356
1357     cls.lpfnWndProc   = MSIHiddenWindowProc;
1358     cls.lpszClassName = szMsiHiddenWindow;
1359
1360     if( !RegisterClassW( &cls ) )
1361         return FALSE;
1362
1363     uiThreadId = GetCurrentThreadId();
1364
1365     hMsiHiddenWindow = CreateWindowW( szMsiHiddenWindow, NULL, WS_OVERLAPPED,
1366                                    0, 0, 100, 100, NULL, NULL, NULL, NULL );
1367     if( !hMsiHiddenWindow )
1368         return FALSE;
1369
1370     return TRUE;
1371 }
1372
1373 void msi_dialog_unregister_class( void )
1374 {
1375     DestroyWindow( hMsiHiddenWindow );
1376     UnregisterClassW( szMsiDialogClass, NULL );
1377     uiThreadId = 0;
1378 }