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