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