msi: Empty the window text if no text is provided.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #define COBJMACROS
22 #define NONAMELESSUNION
23 #define NONAMELESSSTRUCT
24
25 #include <stdarg.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "winnls.h"
32 #include "msi.h"
33 #include "msipriv.h"
34 #include "msidefs.h"
35 #include "ocidl.h"
36 #include "olectl.h"
37 #include "richedit.h"
38 #include "commctrl.h"
39 #include "winreg.h"
40 #include "shlwapi.h"
41
42 #include "wine/debug.h"
43 #include "wine/unicode.h"
44
45 #include "action.h"
46
47 WINE_DEFAULT_DEBUG_CHANNEL(msi);
48
49
50 extern HINSTANCE msi_hInstance;
51
52 struct msi_control_tag;
53 typedef struct msi_control_tag msi_control;
54 typedef UINT (*msi_handler)( msi_dialog *, msi_control *, WPARAM );
55
56 struct msi_control_tag
57 {
58     struct list entry;
59     HWND hwnd;
60     msi_handler handler;
61     LPWSTR property;
62     LPWSTR value;
63     HBITMAP hBitmap;
64     HICON hIcon;
65     LPWSTR tabnext;
66     LPWSTR type;
67     HMODULE hDll;
68     float progress_current;
69     float progress_max;
70     DWORD attributes;
71     WCHAR name[1];
72 };
73
74 typedef struct msi_font_tag
75 {
76     struct msi_font_tag *next;
77     HFONT hfont;
78     COLORREF color;
79     WCHAR name[1];
80 } msi_font;
81
82 struct msi_dialog_tag
83 {
84     MSIPACKAGE *package;
85     msi_dialog *parent;
86     msi_dialog_event_handler event_handler;
87     BOOL finished;
88     INT scale;
89     DWORD attributes;
90     SIZE size;
91     HWND hwnd;
92     LPWSTR default_font;
93     msi_font *font_list;
94     struct list controls;
95     HWND hWndFocus;
96     LPWSTR control_default;
97     LPWSTR control_cancel;
98     WCHAR name[1];
99 };
100
101 typedef UINT (*msi_dialog_control_func)( msi_dialog *dialog, MSIRECORD *rec );
102 struct control_handler 
103 {
104     LPCWSTR control_type;
105     msi_dialog_control_func func;
106 };
107
108 typedef struct
109 {
110     msi_dialog* dialog;
111     msi_control *parent;
112     DWORD       attributes;
113     LPWSTR      propval;
114 } radio_button_group_descr;
115
116 static const WCHAR szMsiDialogClass[] = {
117     'M','s','i','D','i','a','l','o','g','C','l','o','s','e','C','l','a','s','s',0
118 };
119 static const WCHAR szMsiHiddenWindow[] = {
120     'M','s','i','H','i','d','d','e','n','W','i','n','d','o','w',0 };
121 static const WCHAR szStatic[] = { 'S','t','a','t','i','c',0 };
122 static const WCHAR szButton[] = { 'B','U','T','T','O','N', 0 };
123 static const WCHAR szButtonData[] = { 'M','S','I','D','A','T','A',0 };
124 static const WCHAR szProgress[] = { 'P','r','o','g','r','e','s','s',0 };
125 static const WCHAR szText[] = { 'T','e','x','t',0 };
126 static const WCHAR szPushButton[] = { 'P','u','s','h','B','u','t','t','o','n',0 };
127 static const WCHAR szLine[] = { 'L','i','n','e',0 };
128 static const WCHAR szBitmap[] = { 'B','i','t','m','a','p',0 };
129 static const WCHAR szCheckBox[] = { 'C','h','e','c','k','B','o','x',0 };
130 static const WCHAR szScrollableText[] = {
131     'S','c','r','o','l','l','a','b','l','e','T','e','x','t',0 };
132 static const WCHAR szComboBox[] = { 'C','o','m','b','o','B','o','x',0 };
133 static const WCHAR szEdit[] = { 'E','d','i','t',0 };
134 static const WCHAR szMaskedEdit[] = { 'M','a','s','k','e','d','E','d','i','t',0 };
135 static const WCHAR szPathEdit[] = { 'P','a','t','h','E','d','i','t',0 };
136 static const WCHAR szProgressBar[] = {
137      'P','r','o','g','r','e','s','s','B','a','r',0 };
138 static const WCHAR szRadioButtonGroup[] = { 
139     'R','a','d','i','o','B','u','t','t','o','n','G','r','o','u','p',0 };
140 static const WCHAR szIcon[] = { 'I','c','o','n',0 };
141 static const WCHAR szSelectionTree[] = {
142     'S','e','l','e','c','t','i','o','n','T','r','e','e',0 };
143 static const WCHAR szGroupBox[] = { 'G','r','o','u','p','B','o','x',0 };
144 static const WCHAR szListBox[] = { 'L','i','s','t','B','o','x',0 };
145 static const WCHAR szDirectoryCombo[] = { 'D','i','r','e','c','t','o','r','y','C','o','m','b','o',0 };
146 static const WCHAR szDirectoryList[] = { 'D','i','r','e','c','t','o','r','y','L','i','s','t',0 };
147 static const WCHAR szVolumeCostList[] = { 'V','o','l','u','m','e','C','o','s','t','L','i','s','t',0 };
148
149 static UINT msi_dialog_checkbox_handler( msi_dialog *, msi_control *, WPARAM );
150 static void msi_dialog_checkbox_sync_state( msi_dialog *, msi_control * );
151 static UINT msi_dialog_button_handler( msi_dialog *, msi_control *, WPARAM );
152 static UINT msi_dialog_edit_handler( msi_dialog *, msi_control *, WPARAM );
153 static UINT msi_dialog_radiogroup_handler( msi_dialog *, msi_control *, WPARAM param );
154 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog );
155 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
156
157
158 /* dialog sequencing */
159
160 #define WM_MSI_DIALOG_CREATE  (WM_USER+0x100)
161 #define WM_MSI_DIALOG_DESTROY (WM_USER+0x101)
162
163 static DWORD uiThreadId;
164 static HWND hMsiHiddenWindow;
165
166 static INT msi_dialog_scale_unit( msi_dialog *dialog, INT val )
167 {
168     return (dialog->scale * val + 5) / 10;
169 }
170
171 static msi_control *msi_dialog_find_control( msi_dialog *dialog, LPCWSTR name )
172 {
173     msi_control *control;
174
175     if( !name )
176         return NULL;
177     LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
178         if( !strcmpW( control->name, name ) ) /* FIXME: case sensitive? */
179             return control;
180     return NULL;
181 }
182
183 static msi_control *msi_dialog_find_control_by_type( msi_dialog *dialog, LPCWSTR type )
184 {
185     msi_control *control;
186
187     if( !type )
188         return NULL;
189     LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
190         if( !strcmpW( control->type, type ) ) /* FIXME: case sensitive? */
191             return control;
192     return NULL;
193 }
194
195 static msi_control *msi_dialog_find_control_by_hwnd( msi_dialog *dialog, HWND hwnd )
196 {
197     msi_control *control;
198
199     LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
200         if( hwnd == control->hwnd )
201             return control;
202     return NULL;
203 }
204
205 static LPWSTR msi_get_deformatted_field( MSIPACKAGE *package, MSIRECORD *rec, int field )
206 {
207     LPCWSTR str = MSI_RecordGetString( rec, field );
208     LPWSTR ret = NULL;
209
210     if (str)
211         deformat_string( package, str, &ret );
212     return ret;
213 }
214
215 static LPWSTR msi_dialog_dup_property( msi_dialog *dialog, LPCWSTR property, BOOL indirect )
216 {
217     if (!property)
218         return NULL;
219
220     if (indirect)
221         return msi_dup_property( dialog->package, property );
222
223     return strdupW( property );
224 }
225
226 msi_dialog *msi_dialog_get_parent( msi_dialog *dialog )
227 {
228     return dialog->parent;
229 }
230
231 LPWSTR msi_dialog_get_name( msi_dialog *dialog )
232 {
233     return dialog->name;
234 }
235
236 /*
237  * msi_dialog_get_style
238  *
239  * Extract the {\style} string from the front of the text to display and
240  *  update the pointer.
241  */
242 static LPWSTR msi_dialog_get_style( LPCWSTR p, LPCWSTR *rest )
243 {
244     LPWSTR ret = NULL;
245     LPCWSTR q, i;
246     DWORD len;
247
248     *rest = p;
249     if( !p )
250         return ret;
251     if( *p++ != '{' )
252         return ret;
253     q = strchrW( p, '}' );
254     if( !q )
255         return ret;
256     if( *p == '\\' || *p == '&' )
257         p++;
258
259     /* little bit of sanity checking to stop us getting confused with RTF */
260     for( i=p; i<q; i++ )
261         if( *i == '}' || *i == '\\' )
262             return ret;
263     
264     *rest = ++q;
265     len = q - p;
266
267     ret = msi_alloc( len*sizeof(WCHAR) );
268     if( !ret )
269         return ret;
270     memcpy( ret, p, len*sizeof(WCHAR) );
271     ret[len-1] = 0;
272     return ret;
273 }
274
275 static UINT msi_dialog_add_font( MSIRECORD *rec, LPVOID param )
276 {
277     msi_dialog *dialog = param;
278     msi_font *font;
279     LPCWSTR face, name;
280     LOGFONTW lf;
281     INT style;
282     HDC hdc;
283
284     /* create a font and add it to the list */
285     name = MSI_RecordGetString( rec, 1 );
286     font = msi_alloc( sizeof *font + strlenW( name )*sizeof (WCHAR) );
287     strcpyW( font->name, name );
288     font->next = dialog->font_list;
289     dialog->font_list = font;
290
291     font->color = MSI_RecordGetInteger( rec, 4 );
292
293     memset( &lf, 0, sizeof lf );
294     face = MSI_RecordGetString( rec, 2 );
295     lf.lfHeight = MSI_RecordGetInteger( rec, 3 );
296     style = MSI_RecordGetInteger( rec, 5 );
297     if( style & msidbTextStyleStyleBitsBold )
298         lf.lfWeight = FW_BOLD;
299     if( style & msidbTextStyleStyleBitsItalic )
300         lf.lfItalic = TRUE;
301     if( style & msidbTextStyleStyleBitsUnderline )
302         lf.lfUnderline = TRUE;
303     if( style & msidbTextStyleStyleBitsStrike )
304         lf.lfStrikeOut = TRUE;
305     lstrcpynW( lf.lfFaceName, face, LF_FACESIZE );
306
307     /* adjust the height */
308     hdc = GetDC( dialog->hwnd );
309     if (hdc)
310     {
311         lf.lfHeight = -MulDiv(lf.lfHeight, GetDeviceCaps(hdc, LOGPIXELSY), 72);
312         ReleaseDC( dialog->hwnd, hdc );
313     }
314
315     font->hfont = CreateFontIndirectW( &lf );
316
317     TRACE("Adding font style %s\n", debugstr_w(font->name) );
318
319     return ERROR_SUCCESS;
320 }
321
322 static msi_font *msi_dialog_find_font( msi_dialog *dialog, LPCWSTR name )
323 {
324     msi_font *font;
325
326     for( font = dialog->font_list; font; font = font->next )
327         if( !strcmpW( font->name, name ) )  /* FIXME: case sensitive? */
328             break;
329
330     return font;
331 }
332
333 static UINT msi_dialog_set_font( msi_dialog *dialog, HWND hwnd, LPCWSTR name )
334 {
335     msi_font *font;
336
337     font = msi_dialog_find_font( dialog, name );
338     if( font )
339         SendMessageW( hwnd, WM_SETFONT, (WPARAM) font->hfont, TRUE );
340     else
341         ERR("No font entry for %s\n", debugstr_w(name));
342     return ERROR_SUCCESS;
343 }
344
345 static UINT msi_dialog_build_font_list( msi_dialog *dialog )
346 {
347     static const WCHAR query[] = {
348       'S','E','L','E','C','T',' ','*',' ',
349       'F','R','O','M',' ','`','T','e','x','t','S','t','y','l','e','`',' ',0
350     };
351     UINT r;
352     MSIQUERY *view = NULL;
353
354     TRACE("dialog %p\n", dialog );
355
356     r = MSI_OpenQuery( dialog->package->db, &view, query );
357     if( r != ERROR_SUCCESS )
358         return r;
359
360     r = MSI_IterateRecords( view, NULL, msi_dialog_add_font, dialog );
361     msiobj_release( &view->hdr );
362
363     return r;
364 }
365
366 static msi_control *msi_dialog_create_window( msi_dialog *dialog,
367                 MSIRECORD *rec, DWORD exstyle, LPCWSTR szCls, LPCWSTR name, LPCWSTR text,
368                 DWORD style, HWND parent )
369 {
370     DWORD x, y, width, height;
371     LPWSTR font = NULL, title_font = NULL;
372     LPCWSTR title = NULL;
373     msi_control *control;
374
375     style |= WS_CHILD;
376
377     control = msi_alloc( sizeof *control + strlenW(name)*sizeof(WCHAR) );
378     strcpyW( control->name, name );
379     list_add_head( &dialog->controls, &control->entry );
380     control->handler = NULL;
381     control->property = NULL;
382     control->value = NULL;
383     control->hBitmap = NULL;
384     control->hIcon = NULL;
385     control->hDll = NULL;
386     control->tabnext = strdupW( MSI_RecordGetString( rec, 11) );
387     control->type = strdupW( MSI_RecordGetString( rec, 3 ) );
388     control->progress_current = 0;
389     control->progress_max = 100;
390
391     x = MSI_RecordGetInteger( rec, 4 );
392     y = MSI_RecordGetInteger( rec, 5 );
393     width = MSI_RecordGetInteger( rec, 6 );
394     height = MSI_RecordGetInteger( rec, 7 );
395
396     x = msi_dialog_scale_unit( dialog, x );
397     y = msi_dialog_scale_unit( dialog, y );
398     width = msi_dialog_scale_unit( dialog, width );
399     height = msi_dialog_scale_unit( dialog, height );
400
401     if( text )
402     {
403         deformat_string( dialog->package, text, &title_font );
404         font = msi_dialog_get_style( title_font, &title );
405     }
406
407     control->hwnd = CreateWindowExW( exstyle, szCls, title, style,
408                           x, y, width, height, parent, NULL, NULL, NULL );
409
410     TRACE("Dialog %s control %s hwnd %p\n",
411            debugstr_w(dialog->name), debugstr_w(text), control->hwnd );
412
413     msi_dialog_set_font( dialog, control->hwnd,
414                          font ? font : dialog->default_font );
415
416     msi_free( title_font );
417     msi_free( font );
418
419     return control;
420 }
421
422 static LPWSTR msi_dialog_get_uitext( msi_dialog *dialog, LPCWSTR key )
423 {
424     MSIRECORD *rec;
425     LPWSTR text;
426
427     static const WCHAR query[] = {
428         's','e','l','e','c','t',' ','*',' ',
429         'f','r','o','m',' ','`','U','I','T','e','x','t','`',' ',
430         'w','h','e','r','e',' ','`','K','e','y','`',' ','=',' ','\'','%','s','\'',0
431     };
432
433     rec = MSI_QueryGetRecord( dialog->package->db, query, key );
434     if (!rec) return NULL;
435     text = strdupW( MSI_RecordGetString( rec, 2 ) );
436     msiobj_release( &rec->hdr );
437     return text;
438 }
439
440 static MSIRECORD *msi_get_binary_record( MSIDATABASE *db, LPCWSTR name )
441 {
442     static const WCHAR query[] = {
443         's','e','l','e','c','t',' ','*',' ',
444         'f','r','o','m',' ','B','i','n','a','r','y',' ',
445         'w','h','e','r','e',' ',
446             '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0
447     };
448
449     return MSI_QueryGetRecord( db, query, name );
450 }
451
452 static LPWSTR msi_create_tmp_path(void)
453 {
454     WCHAR tmp[MAX_PATH];
455     LPWSTR path = NULL;
456     static const WCHAR prefix[] = { 'm','s','i',0 };
457     DWORD len, r;
458
459     r = GetTempPathW( MAX_PATH, tmp );
460     if( !r )
461         return path;
462     len = lstrlenW( tmp ) + 20;
463     path = msi_alloc( len * sizeof (WCHAR) );
464     if( path )
465     {
466         r = GetTempFileNameW( tmp, prefix, 0, path );
467         if (!r)
468         {
469             msi_free( path );
470             path = NULL;
471         }
472     }
473     return path;
474 }
475
476
477 static HANDLE msi_load_image( MSIDATABASE *db, LPCWSTR name, UINT type,
478                               UINT cx, UINT cy, UINT flags )
479 {
480     MSIRECORD *rec = NULL;
481     HANDLE himage = NULL;
482     LPWSTR tmp;
483     UINT r;
484
485     TRACE("%p %s %u %u %08x\n", db, debugstr_w(name), cx, cy, flags);
486
487     tmp = msi_create_tmp_path();
488     if( !tmp )
489         return himage;
490
491     rec = msi_get_binary_record( db, name );
492     if( rec )
493     {
494         r = MSI_RecordStreamToFile( rec, 2, tmp );
495         if( r == ERROR_SUCCESS )
496         {
497             himage = LoadImageW( 0, tmp, type, cx, cy, flags );
498             DeleteFileW( tmp );
499         }
500         msiobj_release( &rec->hdr );
501     }
502
503     msi_free( tmp );
504     return himage;
505 }
506
507 static HICON msi_load_icon( MSIDATABASE *db, LPCWSTR text, UINT attributes )
508 {
509     DWORD cx = 0, cy = 0, flags;
510
511     flags = LR_LOADFROMFILE | LR_DEFAULTSIZE;
512     if( attributes & msidbControlAttributesFixedSize )
513     {
514         flags &= ~LR_DEFAULTSIZE;
515         if( attributes & msidbControlAttributesIconSize16 )
516         {
517             cx += 16;
518             cy += 16;
519         }
520         if( attributes & msidbControlAttributesIconSize32 )
521         {
522             cx += 32;
523             cy += 32;
524         }
525         /* msidbControlAttributesIconSize48 handled by above logic */
526     }
527     return msi_load_image( db, text, IMAGE_ICON, cx, cy, flags );
528 }
529
530
531 /* called from the Control Event subscription code */
532 void msi_dialog_handle_event( msi_dialog* dialog, LPCWSTR control, 
533                               LPCWSTR attribute, MSIRECORD *rec )
534 {
535     msi_control* ctrl;
536     LPCWSTR font_text, text = NULL;
537     LPWSTR font;
538
539     static const WCHAR empty[] = {0};
540
541     ctrl = msi_dialog_find_control( dialog, control );
542     if (!ctrl)
543         return;
544     if( !lstrcmpW(attribute, szText) )
545     {
546         font_text = MSI_RecordGetString( rec , 1 );
547         font = msi_dialog_get_style( font_text, &text );
548         if (!text) text = empty;
549         SetWindowTextW( ctrl->hwnd, text );
550         msi_free( font );
551         msi_dialog_check_messages( NULL );
552     }
553     else if( !lstrcmpW(attribute, szProgress) )
554     {
555         DWORD func, val;
556
557         func = MSI_RecordGetInteger( rec , 1 );
558         val = MSI_RecordGetInteger( rec , 2 );
559
560         switch (func)
561         {
562         case 0: /* init */
563             ctrl->progress_max = val;
564             ctrl->progress_current = 0;
565             SendMessageW(ctrl->hwnd, PBM_SETRANGE, 0, MAKELPARAM(0,100));
566             SendMessageW(ctrl->hwnd, PBM_SETPOS, 0, 0);
567             break;
568         case 1: /* FIXME: not sure what this is supposed to do */
569             break;
570         case 2: /* move */
571             ctrl->progress_current += val;
572             SendMessageW(ctrl->hwnd, PBM_SETPOS, 100*(ctrl->progress_current/ctrl->progress_max), 0);
573             break;
574         default:
575             ERR("Unknown progress message %ld\n", func);
576             break;
577         }
578     }
579     else
580     {
581         FIXME("Attribute %s not being set\n", debugstr_w(attribute));
582         return;
583     }
584 }
585
586 static void msi_dialog_map_events(msi_dialog* dialog, LPCWSTR control)
587 {
588     static const WCHAR Query[] = {
589         'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
590          '`','E','v','e','n','t','M','a','p','p','i','n','g','`',' ',
591         'W','H','E','R','E',' ',
592          '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
593         'A','N','D',' ',
594          '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',0
595     };
596     MSIRECORD *row;
597     LPCWSTR event, attribute;
598
599     row = MSI_QueryGetRecord( dialog->package->db, Query, dialog->name, control );
600     if (!row)
601         return;
602
603     event = MSI_RecordGetString( row, 3 );
604     attribute = MSI_RecordGetString( row, 4 );
605     ControlEvent_SubscribeToEvent( dialog->package, event, control, attribute );
606     msiobj_release( &row->hdr );
607 }
608
609 /* everything except radio buttons */
610 static msi_control *msi_dialog_add_control( msi_dialog *dialog,
611                 MSIRECORD *rec, LPCWSTR szCls, DWORD style )
612 {
613     DWORD attributes;
614     LPCWSTR text, name;
615     DWORD exstyle = 0;
616
617     name = MSI_RecordGetString( rec, 2 );
618     attributes = MSI_RecordGetInteger( rec, 8 );
619     text = MSI_RecordGetString( rec, 10 );
620     if( attributes & msidbControlAttributesVisible )
621         style |= WS_VISIBLE;
622     if( ~attributes & msidbControlAttributesEnabled )
623         style |= WS_DISABLED;
624     if( attributes & msidbControlAttributesSunken )
625         exstyle |= WS_EX_CLIENTEDGE;
626
627     msi_dialog_map_events(dialog, name);
628
629     return msi_dialog_create_window( dialog, rec, exstyle, szCls, name,
630                                      text, style, dialog->hwnd );
631 }
632
633 struct msi_text_info
634 {
635     msi_font *font;
636     WNDPROC oldproc;
637     DWORD attributes;
638 };
639
640 /*
641  * we don't erase our own background,
642  * so we have to make sure that the parent window redraws first
643  */
644 static void msi_text_on_settext( HWND hWnd )
645 {
646     HWND hParent;
647     RECT rc;
648
649     hParent = GetParent( hWnd );
650     GetWindowRect( hWnd, &rc );
651     MapWindowPoints( NULL, hParent, (LPPOINT) &rc, 2 );
652     InvalidateRect( hParent, &rc, TRUE );
653 }
654
655 static LRESULT WINAPI
656 MSIText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
657 {
658     struct msi_text_info *info;
659     LRESULT r = 0;
660
661     TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
662
663     info = GetPropW(hWnd, szButtonData);
664
665     if ( info->font )
666         SetTextColor( (HDC)wParam, info->font->color );
667
668     if( msg == WM_CTLCOLORSTATIC &&
669        ( info->attributes & msidbControlAttributesTransparent ) )
670     {
671         SetBkMode( (HDC)wParam, TRANSPARENT );
672         return (LRESULT) GetStockObject(NULL_BRUSH);
673     }
674
675     r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
676
677     switch( msg )
678     {
679     case WM_SETTEXT:
680         msi_text_on_settext( hWnd );
681         break;
682     case WM_NCDESTROY:
683         msi_free( info );
684         RemovePropW( hWnd, szButtonData );
685         break;
686     }
687
688     return r;
689 }
690
691 static UINT msi_dialog_text_control( msi_dialog *dialog, MSIRECORD *rec )
692 {
693     msi_control *control;
694     struct msi_text_info *info;
695     LPCWSTR text, ptr;
696     LPWSTR font_name;
697
698     TRACE("%p %p\n", dialog, rec);
699
700     control = msi_dialog_add_control( dialog, rec, szStatic, SS_LEFT | WS_GROUP );
701     if( !control )
702         return ERROR_FUNCTION_FAILED;
703
704     info = msi_alloc( sizeof *info );
705     if( !info )
706         return ERROR_SUCCESS;
707
708     text = MSI_RecordGetString( rec, 10 );
709     font_name = msi_dialog_get_style( text, &ptr );
710     info->font = ( font_name ) ? msi_dialog_find_font( dialog, font_name ) : NULL;
711     msi_free( font_name );
712
713     info->attributes = MSI_RecordGetInteger( rec, 8 );
714     if( info->attributes & msidbControlAttributesTransparent )
715         SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT );
716
717     info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
718                                           (LONG_PTR)MSIText_WndProc );
719     SetPropW( control->hwnd, szButtonData, info );
720
721     return ERROR_SUCCESS;
722 }
723
724 static UINT msi_dialog_button_control( msi_dialog *dialog, MSIRECORD *rec )
725 {
726     msi_control *control;
727     UINT attributes, style;
728     LPWSTR text;
729
730     TRACE("%p %p\n", dialog, rec);
731
732     style = WS_TABSTOP;
733     attributes = MSI_RecordGetInteger( rec, 8 );
734     if( attributes & msidbControlAttributesIcon )
735         style |= BS_ICON;
736
737     control = msi_dialog_add_control( dialog, rec, szButton, style );
738     if( !control )
739         return ERROR_FUNCTION_FAILED;
740
741     control->handler = msi_dialog_button_handler;
742
743     /* set the icon */
744     text = msi_get_deformatted_field( dialog->package, rec, 10 );
745     control->hIcon = msi_load_icon( dialog->package->db, text, attributes );
746     if( attributes & msidbControlAttributesIcon )
747         SendMessageW( control->hwnd, BM_SETIMAGE, IMAGE_ICON, (LPARAM) control->hIcon );
748     msi_free( text );
749
750     return ERROR_SUCCESS;
751 }
752
753 static LPWSTR msi_get_checkbox_value( msi_dialog *dialog, LPCWSTR prop )
754 {
755     static const WCHAR query[] = {
756         'S','E','L','E','C','T',' ','*',' ',
757         'F','R','O','M',' ','`','C','h','e','c','k','B','o','x',' ','`',
758         'W','H','E','R','E',' ',
759         '`','P','r','o','p','e','r','t','y','`',' ','=',' ',
760         '\'','%','s','\'',0
761     };
762     MSIRECORD *rec = NULL;
763     LPWSTR ret = NULL;
764
765     /* find if there is a value associated with the checkbox */
766     rec = MSI_QueryGetRecord( dialog->package->db, query, prop );
767     if (!rec)
768         return ret;
769
770     ret = msi_get_deformatted_field( dialog->package, rec, 2 );
771     if( ret && !ret[0] )
772     {
773         msi_free( ret );
774         ret = NULL;
775     }
776     msiobj_release( &rec->hdr );
777     if (ret)
778         return ret;
779
780     ret = msi_dup_property( dialog->package, prop );
781     if( ret && !ret[0] )
782     {
783         msi_free( ret );
784         ret = NULL;
785     }
786
787     return ret;
788 }
789
790 static UINT msi_dialog_checkbox_control( msi_dialog *dialog, MSIRECORD *rec )
791 {
792     msi_control *control;
793     LPCWSTR prop;
794
795     TRACE("%p %p\n", dialog, rec);
796
797     control = msi_dialog_add_control( dialog, rec, szButton,
798                                 BS_CHECKBOX | BS_MULTILINE | WS_TABSTOP );
799     control->handler = msi_dialog_checkbox_handler;
800     prop = MSI_RecordGetString( rec, 9 );
801     if( prop )
802     {
803         control->property = strdupW( prop );
804         control->value = msi_get_checkbox_value( dialog, prop );
805         TRACE("control %s value %s\n", debugstr_w(control->property),
806               debugstr_w(control->value));
807     }
808     msi_dialog_checkbox_sync_state( dialog, control );
809
810     return ERROR_SUCCESS;
811 }
812
813 static UINT msi_dialog_line_control( msi_dialog *dialog, MSIRECORD *rec )
814 {
815     TRACE("%p %p\n", dialog, rec);
816
817     /* line is exactly 2 units in height */
818     MSI_RecordSetInteger( rec, 7, 2 );
819
820     msi_dialog_add_control( dialog, rec, szStatic, SS_ETCHEDHORZ | SS_SUNKEN );
821     return ERROR_SUCCESS;
822 }
823
824 /******************** Scroll Text ********************************************/
825
826 struct msi_scrolltext_info
827 {
828     msi_dialog *dialog;
829     msi_control *control;
830     WNDPROC oldproc;
831 };
832
833 static LRESULT WINAPI
834 MSIScrollText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
835 {
836     struct msi_scrolltext_info *info;
837     HRESULT r;
838
839     TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
840
841     info = GetPropW( hWnd, szButtonData );
842
843     r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
844
845     switch( msg )
846     {
847     case WM_NCDESTROY:
848         msi_free( info );
849         RemovePropW( hWnd, szButtonData );
850         break;
851     case WM_PAINT:
852         /* native MSI sets a wait cursor here */
853         msi_dialog_button_handler( info->dialog, info->control, BN_CLICKED );
854         break;
855     }
856     return r;
857 }
858
859 struct msi_streamin_info
860 {
861     LPSTR string;
862     DWORD offset;
863     DWORD length;
864 };
865
866 static DWORD CALLBACK
867 msi_richedit_stream_in( DWORD_PTR arg, LPBYTE buffer, LONG count, LONG *pcb )
868 {
869     struct msi_streamin_info *info = (struct msi_streamin_info*) arg;
870
871     if( (count + info->offset) > info->length )
872         count = info->length - info->offset;
873     memcpy( buffer, &info->string[ info->offset ], count );
874     *pcb = count;
875     info->offset += count;
876
877     TRACE("%ld/%ld\n", info->offset, info->length);
878
879     return 0;
880 }
881
882 static void msi_scrolltext_add_text( msi_control *control, LPCWSTR text )
883 {
884     struct msi_streamin_info info;
885     EDITSTREAM es;
886
887     info.string = strdupWtoA( text );
888     info.offset = 0;
889     info.length = lstrlenA( info.string ) + 1;
890
891     es.dwCookie = (DWORD_PTR) &info;
892     es.dwError = 0;
893     es.pfnCallback = msi_richedit_stream_in;
894
895     SendMessageW( control->hwnd, EM_STREAMIN, SF_RTF, (LPARAM) &es );
896
897     msi_free( info.string );
898 }
899
900 static UINT msi_dialog_scrolltext_control( msi_dialog *dialog, MSIRECORD *rec )
901 {
902     static const WCHAR szRichEdit20W[] = {
903         'R','i','c','h','E','d','i','t','2','0','W',0
904     };
905     struct msi_scrolltext_info *info;
906     msi_control *control;
907     HMODULE hRichedit;
908     DWORD style;
909
910     info = msi_alloc( sizeof *info );
911     if (!info)
912         return ERROR_FUNCTION_FAILED;
913
914     hRichedit = LoadLibraryA("riched20");
915
916     style = WS_BORDER | ES_MULTILINE | WS_VSCROLL |
917             ES_READONLY | ES_AUTOVSCROLL | WS_TABSTOP;
918     control = msi_dialog_add_control( dialog, rec, szRichEdit20W, style );
919     if (!control)
920     {
921         FreeLibrary( hRichedit );
922         msi_free( info );
923         return ERROR_FUNCTION_FAILED;
924     }
925
926     control->hDll = hRichedit;
927
928     info->dialog = dialog;
929     info->control = control;
930
931     /* subclass the static control */
932     info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
933                                           (LONG_PTR)MSIScrollText_WndProc );
934     SetPropW( control->hwnd, szButtonData, info );
935
936     /* add the text into the richedit */
937     msi_scrolltext_add_text( control, MSI_RecordGetString( rec, 10 ) );
938
939     return ERROR_SUCCESS;
940 }
941
942 static HBITMAP msi_load_picture( MSIDATABASE *db, LPCWSTR name,
943                                  INT cx, INT cy, DWORD flags )
944 {
945     HBITMAP hOleBitmap = 0, hBitmap = 0, hOldSrcBitmap, hOldDestBitmap;
946     MSIRECORD *rec = NULL;
947     IStream *stm = NULL;
948     IPicture *pic = NULL;
949     HDC srcdc, destdc;
950     BITMAP bm;
951     UINT r;
952
953     rec = msi_get_binary_record( db, name );
954     if( !rec )
955         goto end;
956
957     r = MSI_RecordGetIStream( rec, 2, &stm );
958     msiobj_release( &rec->hdr );
959     if( r != ERROR_SUCCESS )
960         goto end;
961
962     r = OleLoadPicture( stm, 0, TRUE, &IID_IPicture, (LPVOID*) &pic );
963     IStream_Release( stm );
964     if( FAILED( r ) )
965     {
966         ERR("failed to load picture\n");
967         goto end;
968     }
969
970     r = IPicture_get_Handle( pic, (OLE_HANDLE*) &hOleBitmap );
971     if( FAILED( r ) )
972     {
973         ERR("failed to get bitmap handle\n");
974         goto end;
975     }
976  
977     /* make the bitmap the desired size */
978     r = GetObjectW( hOleBitmap, sizeof bm, &bm );
979     if (r != sizeof bm )
980     {
981         ERR("failed to get bitmap size\n");
982         goto end;
983     }
984
985     if (flags & LR_DEFAULTSIZE)
986     {
987         cx = bm.bmWidth;
988         cy = bm.bmHeight;
989     }
990
991     srcdc = CreateCompatibleDC( NULL );
992     hOldSrcBitmap = SelectObject( srcdc, hOleBitmap );
993     destdc = CreateCompatibleDC( NULL );
994     hBitmap = CreateCompatibleBitmap( srcdc, cx, cy );
995     hOldDestBitmap = SelectObject( destdc, hBitmap );
996     StretchBlt( destdc, 0, 0, cx, cy,
997                 srcdc, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
998     SelectObject( srcdc, hOldSrcBitmap );
999     SelectObject( destdc, hOldDestBitmap );
1000     DeleteDC( srcdc );
1001     DeleteDC( destdc );
1002
1003 end:
1004     if ( pic )
1005         IPicture_Release( pic );
1006     return hBitmap;
1007 }
1008
1009 static UINT msi_dialog_bitmap_control( msi_dialog *dialog, MSIRECORD *rec )
1010 {
1011     UINT cx, cy, flags, style, attributes;
1012     msi_control *control;
1013     LPWSTR text;
1014
1015     flags = LR_LOADFROMFILE;
1016     style = SS_BITMAP | SS_LEFT | WS_GROUP;
1017
1018     attributes = MSI_RecordGetInteger( rec, 8 );
1019     if( attributes & msidbControlAttributesFixedSize )
1020     {
1021         flags |= LR_DEFAULTSIZE;
1022         style |= SS_CENTERIMAGE;
1023     }
1024
1025     control = msi_dialog_add_control( dialog, rec, szStatic, style );
1026     cx = MSI_RecordGetInteger( rec, 6 );
1027     cy = MSI_RecordGetInteger( rec, 7 );
1028     cx = msi_dialog_scale_unit( dialog, cx );
1029     cy = msi_dialog_scale_unit( dialog, cy );
1030
1031     text = msi_get_deformatted_field( dialog->package, rec, 10 );
1032     control->hBitmap = msi_load_picture( dialog->package->db, text, cx, cy, flags );
1033     if( control->hBitmap )
1034         SendMessageW( control->hwnd, STM_SETIMAGE,
1035                       IMAGE_BITMAP, (LPARAM) control->hBitmap );
1036     else
1037         ERR("Failed to load bitmap %s\n", debugstr_w(text));
1038
1039     msi_free( text );
1040     
1041     return ERROR_SUCCESS;
1042 }
1043
1044 static UINT msi_dialog_icon_control( msi_dialog *dialog, MSIRECORD *rec )
1045 {
1046     msi_control *control;
1047     DWORD attributes;
1048     LPWSTR text;
1049
1050     TRACE("\n");
1051
1052     control = msi_dialog_add_control( dialog, rec, szStatic,
1053                             SS_ICON | SS_CENTERIMAGE | WS_GROUP );
1054             
1055     attributes = MSI_RecordGetInteger( rec, 8 );
1056     text = msi_get_deformatted_field( dialog->package, rec, 10 );
1057     control->hIcon = msi_load_icon( dialog->package->db, text, attributes );
1058     if( control->hIcon )
1059         SendMessageW( control->hwnd, STM_SETICON, (WPARAM) control->hIcon, 0 );
1060     else
1061         ERR("Failed to load bitmap %s\n", debugstr_w(text));
1062     msi_free( text );
1063     return ERROR_SUCCESS;
1064 }
1065
1066 static UINT msi_dialog_combo_control( msi_dialog *dialog, MSIRECORD *rec )
1067 {
1068     static const WCHAR szCombo[] = { 'C','O','M','B','O','B','O','X',0 };
1069
1070     msi_dialog_add_control( dialog, rec, szCombo,
1071                             SS_BITMAP | SS_LEFT | SS_CENTERIMAGE );
1072     return ERROR_SUCCESS;
1073 }
1074
1075 static UINT msi_dialog_edit_control( msi_dialog *dialog, MSIRECORD *rec )
1076 {
1077     msi_control *control;
1078     LPCWSTR prop;
1079     LPWSTR val;
1080
1081     control = msi_dialog_add_control( dialog, rec, szEdit,
1082                                       WS_BORDER | WS_TABSTOP );
1083     control->handler = msi_dialog_edit_handler;
1084     prop = MSI_RecordGetString( rec, 9 );
1085     if( prop )
1086         control->property = strdupW( prop );
1087     val = msi_dup_property( dialog->package, control->property );
1088     SetWindowTextW( control->hwnd, val );
1089     msi_free( val );
1090     return ERROR_SUCCESS;
1091 }
1092
1093 /******************** Masked Edit ********************************************/
1094
1095 #define MASK_MAX_GROUPS 20
1096
1097 struct msi_mask_group
1098 {
1099     UINT len;
1100     UINT ofs;
1101     WCHAR type;
1102     HWND hwnd;
1103 };
1104
1105 struct msi_maskedit_info
1106 {
1107     msi_dialog *dialog;
1108     WNDPROC oldproc;
1109     HWND hwnd;
1110     LPWSTR prop;
1111     UINT num_chars;
1112     UINT num_groups;
1113     struct msi_mask_group group[MASK_MAX_GROUPS];
1114 };
1115
1116 static BOOL msi_mask_editable( WCHAR type )
1117 {
1118     switch (type)
1119     {
1120     case '%':
1121     case '#':
1122     case '&':
1123     case '`':
1124     case '?':
1125     case '^':
1126         return TRUE;
1127     }
1128     return FALSE;
1129 }
1130
1131 static void msi_mask_control_change( struct msi_maskedit_info *info )
1132 {
1133     LPWSTR val;
1134     UINT i, n, r;
1135
1136     val = msi_alloc( (info->num_chars+1)*sizeof(WCHAR) );
1137     for( i=0, n=0; i<info->num_groups; i++ )
1138     {
1139         if( (info->group[i].len + n) > info->num_chars )
1140         {
1141             ERR("can't fit control %d text into template\n",i);
1142             break;
1143         }
1144         if (!msi_mask_editable(info->group[i].type))
1145         {
1146             for(r=0; r<info->group[i].len; r++)
1147                 val[n+r] = info->group[i].type;
1148             val[n+r] = 0;
1149         }
1150         else
1151         {
1152             r = GetWindowTextW( info->group[i].hwnd, &val[n], info->group[i].len+1 );
1153             if( r != info->group[i].len )
1154                 break;
1155         }
1156         n += r;
1157     }
1158
1159     TRACE("%d/%d controls were good\n", i, info->num_groups);
1160
1161     if( i == info->num_groups )
1162     {
1163         TRACE("Set property %s to %s\n",
1164               debugstr_w(info->prop), debugstr_w(val) );
1165         CharUpperBuffW( val, info->num_chars );
1166         MSI_SetPropertyW( info->dialog->package, info->prop, val );
1167         msi_dialog_evaluate_control_conditions( info->dialog );
1168     }
1169     msi_free( val );
1170 }
1171
1172 /* now move to the next control if necessary */
1173 static VOID msi_mask_next_control( struct msi_maskedit_info *info, HWND hWnd )
1174 {
1175     HWND hWndNext;
1176     UINT len, i;
1177
1178     for( i=0; i<info->num_groups; i++ )
1179         if( info->group[i].hwnd == hWnd )
1180             break;
1181
1182     /* don't move from the last control */
1183     if( i >= (info->num_groups-1) )
1184         return;
1185
1186     len = SendMessageW( hWnd, WM_GETTEXTLENGTH, 0, 0 );
1187     if( len < info->group[i].len )
1188         return;
1189
1190     hWndNext = GetNextDlgTabItem( GetParent( hWnd ), hWnd, FALSE );
1191     SetFocus( hWndNext );
1192 }
1193
1194 static LRESULT WINAPI
1195 MSIMaskedEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1196 {
1197     struct msi_maskedit_info *info;
1198     HRESULT r;
1199
1200     TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
1201
1202     info = GetPropW(hWnd, szButtonData);
1203
1204     r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1205
1206     switch( msg )
1207     {
1208     case WM_COMMAND:
1209         if (HIWORD(wParam) == EN_CHANGE)
1210         {
1211             msi_mask_control_change( info );
1212             msi_mask_next_control( info, (HWND) lParam );
1213         }
1214         break;
1215     case WM_NCDESTROY:
1216         msi_free( info->prop );
1217         msi_free( info );
1218         RemovePropW( hWnd, szButtonData );
1219         break;
1220     }
1221
1222     return r;
1223 }
1224
1225 /* fish the various bits of the property out and put them in the control */
1226 static void
1227 msi_maskedit_set_text( struct msi_maskedit_info *info, LPCWSTR text )
1228 {
1229     LPCWSTR p;
1230     UINT i;
1231
1232     p = text;
1233     for( i = 0; i < info->num_groups; i++ )
1234     {
1235         if( info->group[i].len < lstrlenW( p ) )
1236         {
1237             LPWSTR chunk = strdupW( p );
1238             chunk[ info->group[i].len ] = 0;
1239             SetWindowTextW( info->group[i].hwnd, chunk );
1240             msi_free( chunk );
1241         }
1242         else
1243         {
1244             SetWindowTextW( info->group[i].hwnd, p );
1245             break;
1246         }
1247         p += info->group[i].len;
1248     }
1249 }
1250
1251 static struct msi_maskedit_info * msi_dialog_parse_groups( LPCWSTR mask )
1252 {
1253     struct msi_maskedit_info * info = NULL;
1254     int i = 0, n = 0, total = 0;
1255     LPCWSTR p;
1256
1257     TRACE("masked control, template %s\n", debugstr_w(mask));
1258
1259     if( !mask )
1260         return info;
1261
1262     info = msi_alloc_zero( sizeof *info );
1263     if( !info )
1264         return info;
1265
1266     p = strchrW(mask, '<');
1267     if( p )
1268         p++;
1269     else
1270         p = mask;
1271
1272     for( i=0; i<MASK_MAX_GROUPS; i++ )
1273     {
1274         /* stop at the end of the string */
1275         if( p[0] == 0 || p[0] == '>' )
1276             break;
1277
1278         /* count the number of the same identifier */
1279         for( n=0; p[n] == p[0]; n++ )
1280             ;
1281         info->group[i].ofs = total;
1282         info->group[i].type = p[0];
1283         if( p[n] == '=' )
1284         {
1285             n++;
1286             total++; /* an extra not part of the group */
1287         }
1288         info->group[i].len = n;
1289         total += n;
1290         p += n;
1291     }
1292
1293     TRACE("%d characters in %d groups\n", total, i );
1294     if( i == MASK_MAX_GROUPS )
1295         ERR("too many groups in PIDTemplate %s\n", debugstr_w(mask));
1296
1297     info->num_chars = total;
1298     info->num_groups = i;
1299
1300     return info;
1301 }
1302
1303 static void
1304 msi_maskedit_create_children( struct msi_maskedit_info *info, LPCWSTR font )
1305 {
1306     DWORD width, height, style, wx, ww;
1307     RECT rect;
1308     HWND hwnd;
1309     UINT i;
1310
1311     style = WS_CHILD | WS_BORDER | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL;
1312
1313     GetClientRect( info->hwnd, &rect );
1314
1315     width = rect.right - rect.left;
1316     height = rect.bottom - rect.top;
1317
1318     for( i = 0; i < info->num_groups; i++ )
1319     {
1320         if (!msi_mask_editable( info->group[i].type ))
1321             continue;
1322         wx = (info->group[i].ofs * width) / info->num_chars;
1323         ww = (info->group[i].len * width) / info->num_chars;
1324
1325         hwnd = CreateWindowW( szEdit, NULL, style, wx, 0, ww, height,
1326                               info->hwnd, NULL, NULL, NULL );
1327         if( !hwnd )
1328         {
1329             ERR("failed to create mask edit sub window\n");
1330             break;
1331         }
1332
1333         SendMessageW( hwnd, EM_LIMITTEXT, info->group[i].len, 0 );
1334
1335         msi_dialog_set_font( info->dialog, hwnd,
1336                              font?font:info->dialog->default_font );
1337         info->group[i].hwnd = hwnd;
1338     }
1339 }
1340
1341 /*
1342  * office 2003 uses "73931<````=````=````=````=`````>@@@@@"
1343  * delphi 7 uses "<????-??????-??????-????>" and "<???-???>"
1344  * filemaker pro 7 uses "<^^^^=^^^^=^^^^=^^^^=^^^^=^^^^=^^^^^>"
1345  */
1346 static UINT msi_dialog_maskedit_control( msi_dialog *dialog, MSIRECORD *rec )
1347 {
1348     LPWSTR font_mask, val = NULL, font;
1349     struct msi_maskedit_info *info = NULL;
1350     UINT ret = ERROR_SUCCESS;
1351     msi_control *control;
1352     LPCWSTR prop, mask;
1353
1354     TRACE("\n");
1355
1356     font_mask = msi_get_deformatted_field( dialog->package, rec, 10 );
1357     font = msi_dialog_get_style( font_mask, &mask );
1358     if( !mask )
1359     {
1360         ERR("mask template is empty\n");
1361         goto end;
1362     }
1363
1364     info = msi_dialog_parse_groups( mask );
1365     if( !info )
1366     {
1367         ERR("template %s is invalid\n", debugstr_w(mask));
1368         goto end;
1369     }
1370
1371     info->dialog = dialog;
1372
1373     control = msi_dialog_add_control( dialog, rec, szStatic,
1374                    SS_OWNERDRAW | WS_GROUP | WS_VISIBLE );
1375     if( !control )
1376     {
1377         ERR("Failed to create maskedit container\n");
1378         ret = ERROR_FUNCTION_FAILED;
1379         goto end;
1380     }
1381     SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
1382
1383     info->hwnd = control->hwnd;
1384
1385     /* subclass the static control */
1386     info->oldproc = (WNDPROC) SetWindowLongPtrW( info->hwnd, GWLP_WNDPROC,
1387                                           (LONG_PTR)MSIMaskedEdit_WndProc );
1388     SetPropW( control->hwnd, szButtonData, info );
1389
1390     prop = MSI_RecordGetString( rec, 9 );
1391     if( prop )
1392         info->prop = strdupW( prop );
1393
1394     msi_maskedit_create_children( info, font );
1395
1396     if( prop )
1397     {
1398         val = msi_dup_property( dialog->package, prop );
1399         if( val )
1400         {
1401             msi_maskedit_set_text( info, val );
1402             msi_free( val );
1403         }
1404     }
1405
1406 end:
1407     if( ret != ERROR_SUCCESS )
1408         msi_free( info );
1409     msi_free( font_mask );
1410     msi_free( font );
1411     return ret;
1412 }
1413
1414 /******************** Progress Bar *****************************************/
1415
1416 static UINT msi_dialog_progress_bar( msi_dialog *dialog, MSIRECORD *rec )
1417 {
1418     msi_dialog_add_control( dialog, rec, PROGRESS_CLASSW, WS_VISIBLE );
1419     return ERROR_SUCCESS;
1420 }
1421
1422 /******************** Path Edit ********************************************/
1423
1424 struct msi_pathedit_info
1425 {
1426     msi_dialog *dialog;
1427     msi_control *control;
1428     WNDPROC oldproc;
1429 };
1430
1431 static LPWSTR msi_get_window_text( HWND hwnd )
1432 {
1433     UINT sz, r;
1434     LPWSTR buf;
1435
1436     sz = 0x20;
1437     buf = msi_alloc( sz*sizeof(WCHAR) );
1438     while ( buf )
1439     {
1440         r = GetWindowTextW( hwnd, buf, sz );
1441         if ( r < (sz - 1) )
1442             break;
1443         sz *= 2;
1444         buf = msi_realloc( buf, sz*sizeof(WCHAR) );
1445     }
1446
1447     return buf;
1448 }
1449
1450 static void msi_dialog_update_pathedit( msi_dialog *dialog, msi_control *control )
1451 {
1452     LPWSTR prop, path;
1453     BOOL indirect;
1454
1455     if (!control && !(control = msi_dialog_find_control_by_type( dialog, szPathEdit )))
1456        return;
1457
1458     indirect = control->attributes & msidbControlAttributesIndirect;
1459     prop = msi_dialog_dup_property( dialog, control->property, indirect );
1460
1461     path = msi_dup_property( dialog->package, prop );
1462     if (!path)
1463         path = prop;
1464
1465     SetWindowTextW( control->hwnd, path );
1466     SendMessageW( control->hwnd, EM_SETSEL, 0, -1 );
1467
1468     msi_free( path );
1469     msi_free( prop );
1470 }
1471
1472 /* FIXME: test when this should fail */
1473 static BOOL msi_dialog_verify_path( LPWSTR path )
1474 {
1475     if ( !lstrlenW( path ) )
1476         return FALSE;
1477
1478     if ( PathIsRelativeW( path ) )
1479         return FALSE;
1480
1481     return TRUE;
1482 }
1483
1484 /* returns TRUE if the path is valid, FALSE otherwise */
1485 static BOOL msi_dialog_onkillfocus( msi_dialog *dialog, msi_control *control )
1486 {
1487     LPWSTR buf, prop;
1488     BOOL indirect;
1489     BOOL valid;
1490
1491     indirect = control->attributes & msidbControlAttributesIndirect;
1492     prop = msi_dialog_dup_property( dialog, control->property, indirect );
1493
1494     buf = msi_get_window_text( control->hwnd );
1495
1496     if ( !msi_dialog_verify_path( buf ) )
1497     {
1498         /* FIXME: display an error message box */
1499         ERR("Invalid path %s\n", debugstr_w( buf ));
1500         valid = FALSE;
1501         SetFocus( control->hwnd );
1502     }
1503     else
1504     {
1505         valid = TRUE;
1506         MSI_SetPropertyW( dialog->package, prop, buf );
1507     }
1508
1509     msi_dialog_update_pathedit( dialog, control );
1510
1511     TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
1512           debugstr_w(prop));
1513
1514     msi_free( buf );
1515     msi_free( prop );
1516
1517     return valid;
1518 }
1519
1520 static LRESULT WINAPI MSIPathEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1521 {
1522     struct msi_pathedit_info *info = GetPropW(hWnd, szButtonData);
1523     LRESULT r = 0;
1524
1525     TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
1526
1527     if ( msg == WM_KILLFOCUS )
1528     {
1529         /* if the path is invalid, don't handle this message */
1530         if ( !msi_dialog_onkillfocus( info->dialog, info->control ) )
1531             return 0;
1532     }
1533
1534     r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1535
1536     if ( msg == WM_NCDESTROY )
1537     {
1538         msi_free( info );
1539         RemovePropW( hWnd, szButtonData );
1540     }
1541
1542     return r;
1543 }
1544
1545 static UINT msi_dialog_pathedit_control( msi_dialog *dialog, MSIRECORD *rec )
1546 {
1547     struct msi_pathedit_info *info;
1548     msi_control *control;
1549     LPCWSTR prop;
1550
1551     info = msi_alloc( sizeof *info );
1552     if (!info)
1553         return ERROR_FUNCTION_FAILED;
1554
1555     control = msi_dialog_add_control( dialog, rec, szEdit,
1556                                       WS_BORDER | WS_TABSTOP );
1557     control->attributes = MSI_RecordGetInteger( rec, 8 );
1558     prop = MSI_RecordGetString( rec, 9 );
1559     control->property = msi_dialog_dup_property( dialog, prop, FALSE );
1560
1561     info->dialog = dialog;
1562     info->control = control;
1563     info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1564                                                  (LONG_PTR)MSIPathEdit_WndProc );
1565     SetPropW( control->hwnd, szButtonData, info );
1566
1567     msi_dialog_update_pathedit( dialog, control );
1568
1569     return ERROR_SUCCESS;
1570 }
1571
1572 /* radio buttons are a bit different from normal controls */
1573 static UINT msi_dialog_create_radiobutton( MSIRECORD *rec, LPVOID param )
1574 {
1575     radio_button_group_descr *group = (radio_button_group_descr *)param;
1576     msi_dialog *dialog = group->dialog;
1577     msi_control *control;
1578     LPCWSTR prop, text, name;
1579     DWORD style, attributes = group->attributes;
1580
1581     style = WS_CHILD | BS_AUTORADIOBUTTON | BS_MULTILINE | WS_TABSTOP;
1582     name = MSI_RecordGetString( rec, 3 );
1583     text = MSI_RecordGetString( rec, 8 );
1584     if( attributes & 1 )
1585         style |= WS_VISIBLE;
1586     if( ~attributes & 2 )
1587         style |= WS_DISABLED;
1588
1589     control = msi_dialog_create_window( dialog, rec, 0, szButton, name, text,
1590                                         style, group->parent->hwnd );
1591     if (!control)
1592         return ERROR_FUNCTION_FAILED;
1593     control->handler = msi_dialog_radiogroup_handler;
1594
1595     if (!lstrcmpW(control->name, group->propval))
1596         SendMessageW(control->hwnd, BM_SETCHECK, BST_CHECKED, 0);
1597
1598     prop = MSI_RecordGetString( rec, 1 );
1599     if( prop )
1600         control->property = strdupW( prop );
1601
1602     return ERROR_SUCCESS;
1603 }
1604
1605 static UINT msi_dialog_radiogroup_control( msi_dialog *dialog, MSIRECORD *rec )
1606 {
1607     static const WCHAR query[] = {
1608         'S','E','L','E','C','T',' ','*',' ',
1609         'F','R','O','M',' ','R','a','d','i','o','B','u','t','t','o','n',' ',
1610         'W','H','E','R','E',' ',
1611            '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
1612     UINT r;
1613     LPCWSTR prop;
1614     msi_control *control;
1615     MSIQUERY *view = NULL;
1616     radio_button_group_descr group;
1617     MSIPACKAGE *package = dialog->package;
1618     WNDPROC oldproc;
1619
1620     prop = MSI_RecordGetString( rec, 9 );
1621
1622     TRACE("%p %p %s\n", dialog, rec, debugstr_w( prop ));
1623
1624     /* Create parent group box to hold radio buttons */
1625     control = msi_dialog_add_control( dialog, rec, szButton, BS_OWNERDRAW|WS_GROUP );
1626     if( !control )
1627         return ERROR_FUNCTION_FAILED;
1628
1629     oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1630                                            (LONG_PTR)MSIRadioGroup_WndProc );
1631     SetPropW(control->hwnd, szButtonData, oldproc);
1632     SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
1633
1634     if( prop )
1635         control->property = strdupW( prop );
1636
1637     /* query the Radio Button table for all control in this group */
1638     r = MSI_OpenQuery( package->db, &view, query, prop );
1639     if( r != ERROR_SUCCESS )
1640     {
1641         ERR("query failed for dialog %s radio group %s\n", 
1642             debugstr_w(dialog->name), debugstr_w(prop));
1643         return ERROR_INVALID_PARAMETER;
1644     }
1645
1646     group.dialog = dialog;
1647     group.parent = control;
1648     group.attributes = MSI_RecordGetInteger( rec, 8 );
1649     group.propval = msi_dup_property( dialog->package, control->property );
1650
1651     r = MSI_IterateRecords( view, 0, msi_dialog_create_radiobutton, &group );
1652     msiobj_release( &view->hdr );
1653     msi_free( group.propval );
1654
1655     return r;
1656 }
1657
1658 /******************** Selection Tree ***************************************/
1659
1660 struct msi_selection_tree_info
1661 {
1662     msi_dialog *dialog;
1663     HWND hwnd;
1664     WNDPROC oldproc;
1665 };
1666
1667 static void
1668 msi_seltree_sync_item_state( HWND hwnd, MSIFEATURE *feature, HTREEITEM hItem )
1669 {
1670     TVITEMW tvi;
1671     DWORD index = feature->Action;
1672
1673     TRACE("Feature %s -> %d %d %d\n", debugstr_w(feature->Title),
1674         feature->Installed, feature->Action, feature->ActionRequest);
1675
1676     if (index == INSTALLSTATE_UNKNOWN)
1677         index = INSTALLSTATE_ABSENT;
1678
1679     tvi.mask = TVIF_STATE;
1680     tvi.hItem = hItem;
1681     tvi.state = INDEXTOSTATEIMAGEMASK( index );
1682     tvi.stateMask = TVIS_STATEIMAGEMASK;
1683
1684     SendMessageW( hwnd, TVM_SETITEMW, 0, (LPARAM) &tvi );
1685 }
1686
1687 static UINT
1688 msi_seltree_popup_menu( HWND hwnd, INT x, INT y )
1689 {
1690     HMENU hMenu;
1691     INT r;
1692
1693     /* create a menu to display */
1694     hMenu = CreatePopupMenu();
1695
1696     /* FIXME: load strings from resources */
1697     AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_LOCAL, "Install feature locally");
1698     AppendMenuA( hMenu, MF_GRAYED, 0x1000, "Install entire feature");
1699     AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ADVERTISED, "Install on demand");
1700     AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ABSENT, "Don't install");
1701     r = TrackPopupMenu( hMenu, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RETURNCMD,
1702                         x, y, 0, hwnd, NULL );
1703     DestroyMenu( hMenu );
1704     return r;
1705 }
1706
1707 static MSIFEATURE *
1708 msi_seltree_feature_from_item( HWND hwnd, HTREEITEM hItem )
1709 {
1710     TVITEMW tvi;
1711
1712     /* get the feature from the item */
1713     memset( &tvi, 0, sizeof tvi );
1714     tvi.hItem = hItem;
1715     tvi.mask = TVIF_PARAM | TVIF_HANDLE;
1716     SendMessageW( hwnd, TVM_GETITEMW, 0, (LPARAM) &tvi );
1717
1718     return (MSIFEATURE*) tvi.lParam;
1719 }
1720
1721 static LRESULT
1722 msi_seltree_menu( HWND hwnd, HTREEITEM hItem )
1723 {
1724     struct msi_selection_tree_info *info;
1725     MSIFEATURE *feature;
1726     MSIPACKAGE *package;
1727     union {
1728         RECT rc;
1729         POINT pt[2];
1730         HTREEITEM hItem;
1731     } u;
1732     UINT r;
1733
1734     info = GetPropW(hwnd, szButtonData);
1735     package = info->dialog->package;
1736
1737     feature = msi_seltree_feature_from_item( hwnd, hItem );
1738     if (!feature)
1739     {
1740         ERR("item %p feature was NULL\n", hItem);
1741         return 0;
1742     }
1743
1744     /* get the item's rectangle to put the menu just below it */
1745     u.hItem = hItem;
1746     SendMessageW( hwnd, TVM_GETITEMRECT, 0, (LPARAM) &u.rc );
1747     MapWindowPoints( hwnd, NULL, u.pt, 2 );
1748
1749     r = msi_seltree_popup_menu( hwnd, u.rc.left, u.rc.top );
1750
1751     switch (r)
1752     {
1753     case INSTALLSTATE_LOCAL:
1754     case INSTALLSTATE_ADVERTISED:
1755     case INSTALLSTATE_ABSENT:
1756         feature->ActionRequest = r;
1757         feature->Action = r;
1758         break;
1759     default:
1760         FIXME("select feature and all children\n");
1761     }
1762
1763     /* update */
1764     msi_seltree_sync_item_state( hwnd, feature, hItem );
1765     ACTION_UpdateComponentStates( package, feature->Feature );
1766
1767     return 0;
1768 }
1769
1770 static LRESULT WINAPI
1771 MSISelectionTree_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1772 {
1773     struct msi_selection_tree_info *info;
1774     TVHITTESTINFO tvhti;
1775     HRESULT r;
1776
1777     TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
1778
1779     info = GetPropW(hWnd, szButtonData);
1780
1781     switch( msg )
1782     {
1783     case WM_LBUTTONDOWN:
1784         tvhti.pt.x = LOWORD( lParam );
1785         tvhti.pt.y = HIWORD( lParam );
1786         tvhti.flags = 0;
1787         tvhti.hItem = 0;
1788         r = CallWindowProcW(info->oldproc, hWnd, TVM_HITTEST, 0, (LPARAM) &tvhti );
1789         if (tvhti.flags & TVHT_ONITEMSTATEICON)
1790             return msi_seltree_menu( hWnd, tvhti.hItem );
1791         break;
1792     }
1793
1794     r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1795
1796     switch( msg )
1797     {
1798     case WM_NCDESTROY:
1799         msi_free( info );
1800         RemovePropW( hWnd, szButtonData );
1801         break;
1802     }
1803     return r;
1804 }
1805
1806 static void
1807 msi_seltree_add_child_features( MSIPACKAGE *package, HWND hwnd,
1808                                 LPCWSTR parent, HTREEITEM hParent )
1809 {
1810     MSIFEATURE *feature;
1811     TVINSERTSTRUCTW tvis;
1812     HTREEITEM hitem;
1813
1814     LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
1815     {
1816         if ( lstrcmpW( parent, feature->Feature_Parent ) )
1817             continue;
1818
1819         if ( !feature->Title )
1820             continue;
1821
1822         if ( !feature->Display )
1823             continue;
1824
1825         memset( &tvis, 0, sizeof tvis );
1826         tvis.hParent = hParent;
1827         tvis.hInsertAfter = TVI_LAST;
1828         tvis.u.item.mask = TVIF_TEXT | TVIF_PARAM;
1829         tvis.u.item.pszText = feature->Title;
1830         tvis.u.item.lParam = (LPARAM) feature;
1831
1832         hitem = (HTREEITEM) SendMessageW( hwnd, TVM_INSERTITEMW, 0, (LPARAM) &tvis );
1833         if (!hitem)
1834             continue;
1835
1836         msi_seltree_sync_item_state( hwnd, feature, hitem );
1837         msi_seltree_add_child_features( package, hwnd,
1838                                         feature->Feature, hitem );
1839
1840         /* the node is expanded if Display is odd */
1841         if ( feature->Display % 2 != 0 )
1842             SendMessageW( hwnd, TVM_EXPAND, TVE_EXPAND, (LPARAM) hitem );
1843     }
1844 }
1845
1846 static void msi_seltree_create_imagelist( HWND hwnd )
1847 {
1848     const int bm_width = 32, bm_height = 16, bm_count = 3;
1849     const int bm_resource = 0x1001;
1850     HIMAGELIST himl;
1851     int i;
1852     HBITMAP hbmp;
1853
1854     himl = ImageList_Create( bm_width, bm_height, FALSE, 4, 0 );
1855     if (!himl)
1856     {
1857         ERR("failed to create image list\n");
1858         return;
1859     }
1860
1861     for (i=0; i<bm_count; i++)
1862     {
1863         hbmp = LoadBitmapW( msi_hInstance, MAKEINTRESOURCEW(i+bm_resource) );
1864         if (!hbmp)
1865         {
1866             ERR("failed to load bitmap %d\n", i);
1867             break;
1868         }
1869
1870         /*
1871          * Add a dummy bitmap at offset zero because the treeview
1872          * can't use it as a state mask (zero means no user state).
1873          */
1874         if (!i)
1875             ImageList_Add( himl, hbmp, NULL );
1876
1877         ImageList_Add( himl, hbmp, NULL );
1878     }
1879
1880     SendMessageW( hwnd, TVM_SETIMAGELIST, TVSIL_STATE, (LPARAM)himl );
1881 }
1882
1883 static UINT msi_dialog_selection_tree( msi_dialog *dialog, MSIRECORD *rec )
1884 {
1885     msi_control *control;
1886     LPCWSTR prop;
1887     MSIPACKAGE *package = dialog->package;
1888     DWORD style;
1889     struct msi_selection_tree_info *info;
1890
1891     info = msi_alloc( sizeof *info );
1892     if (!info)
1893         return ERROR_FUNCTION_FAILED;
1894
1895     /* create the treeview control */
1896     prop = MSI_RecordGetString( rec, 9 );
1897     style = TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT;
1898     style |= WS_GROUP | WS_VSCROLL;
1899     control = msi_dialog_add_control( dialog, rec, WC_TREEVIEWW, style );
1900     if (!control)
1901     {
1902         msi_free(info);
1903         return ERROR_FUNCTION_FAILED;
1904     }
1905
1906     /* subclass */
1907     info->dialog = dialog;
1908     info->hwnd = control->hwnd;
1909     info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1910                                           (LONG_PTR)MSISelectionTree_WndProc );
1911     SetPropW( control->hwnd, szButtonData, info );
1912
1913     /* initialize it */
1914     msi_seltree_create_imagelist( control->hwnd );
1915     msi_seltree_add_child_features( package, control->hwnd, NULL, NULL );
1916
1917     return ERROR_SUCCESS;
1918 }
1919
1920 /******************** Group Box ***************************************/
1921
1922 static UINT msi_dialog_group_box( msi_dialog *dialog, MSIRECORD *rec )
1923 {
1924     msi_control *control;
1925     DWORD style;
1926
1927     style = BS_GROUPBOX | WS_CHILD | WS_GROUP;
1928     control = msi_dialog_add_control( dialog, rec, WC_BUTTONW, style );
1929     if (!control)
1930         return ERROR_FUNCTION_FAILED;
1931
1932     return ERROR_SUCCESS;
1933 }
1934
1935 /******************** List Box ***************************************/
1936
1937 struct msi_listbox_item
1938 {
1939     LPWSTR property;
1940     LPWSTR value;
1941 };
1942
1943 struct msi_listbox_info
1944 {
1945     msi_dialog *dialog;
1946     HWND hwnd;
1947     WNDPROC oldproc;
1948     DWORD num_items;
1949     struct msi_listbox_item *items;
1950 };
1951
1952 static LRESULT WINAPI MSIListBox_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1953 {
1954     struct msi_listbox_info *info;
1955     LRESULT r;
1956     DWORD j;
1957
1958     TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
1959
1960     info = GetPropW( hWnd, szButtonData );
1961     if (!info)
1962         return 0;
1963
1964     r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
1965
1966     switch( msg )
1967     {
1968     case WM_NCDESTROY:
1969         for (j = 0; j < info->num_items; j++)
1970         {
1971             msi_free( info->items[j].property );
1972             msi_free( info->items[j].value );
1973         }
1974         msi_free( info->items );
1975         msi_free( info );
1976         RemovePropW( hWnd, szButtonData );
1977         break;
1978     }
1979
1980     return r;
1981 }
1982
1983 static UINT msi_listbox_add_item( MSIRECORD *rec, LPVOID param )
1984 {
1985     struct msi_listbox_info *info = param;
1986     struct msi_listbox_item *item;
1987     LPCWSTR property, value, text;
1988     static int index = 0;
1989
1990     item = &info->items[index++];
1991     property = MSI_RecordGetString( rec, 1 );
1992     value = MSI_RecordGetString( rec, 3 );
1993     text = MSI_RecordGetString( rec, 4 );
1994
1995     item->property = strdupW( property );
1996     item->value = strdupW( value );
1997
1998     SendMessageW( info->hwnd, LB_ADDSTRING, 0, (LPARAM)text );
1999
2000     return ERROR_SUCCESS;
2001 }
2002
2003 static UINT msi_listbox_add_items( struct msi_listbox_info *info )
2004 {
2005     UINT r;
2006     MSIQUERY *view = NULL;
2007     DWORD count;
2008
2009     static const WCHAR query[] = {
2010         'S','E','L','E','C','T',' ','*',' ',
2011         'F','R','O','M',' ','`','L','i','s','t','B','o','x','`',' ',
2012         'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','`',0
2013     };
2014
2015     r = MSI_OpenQuery( info->dialog->package->db, &view, query );
2016     if ( r != ERROR_SUCCESS )
2017         return r;
2018
2019     /* just get the number of records */
2020     r = MSI_IterateRecords( view, &count, NULL, NULL );
2021
2022     info->num_items = count;
2023     info->items = msi_alloc( sizeof(*info->items) * count );
2024
2025     r = MSI_IterateRecords( view, NULL, msi_listbox_add_item, info );
2026     msiobj_release( &view->hdr );
2027
2028     return r;
2029 }
2030
2031 static UINT msi_dialog_listbox_handler( msi_dialog *dialog,
2032                                         msi_control *control, WPARAM param )
2033 {
2034     struct msi_listbox_info *info;
2035     int index;
2036
2037     if( HIWORD(param) != LBN_SELCHANGE )
2038         return ERROR_SUCCESS;
2039
2040     info = GetPropW( control->hwnd, szButtonData );
2041     index = SendMessageW( control->hwnd, LB_GETCURSEL, 0, 0 );
2042
2043     MSI_SetPropertyW( info->dialog->package,
2044                       info->items[index].property, info->items[index].value );
2045     msi_dialog_evaluate_control_conditions( info->dialog );
2046
2047     return ERROR_SUCCESS;
2048 }
2049
2050 static UINT msi_dialog_list_box( msi_dialog *dialog, MSIRECORD *rec )
2051 {
2052     struct msi_listbox_info *info;
2053     msi_control *control;
2054     DWORD style;
2055
2056     info = msi_alloc( sizeof *info );
2057     if (!info)
2058         return ERROR_FUNCTION_FAILED;
2059
2060     style = WS_TABSTOP | WS_GROUP | WS_CHILD | LBS_STANDARD;
2061     control = msi_dialog_add_control( dialog, rec, WC_LISTBOXW, style );
2062     if (!control)
2063         return ERROR_FUNCTION_FAILED;
2064
2065     control->handler = msi_dialog_listbox_handler;
2066
2067     /* subclass */
2068     info->dialog = dialog;
2069     info->hwnd = control->hwnd;
2070     info->items = NULL;
2071     info->oldproc = (WNDPROC)SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2072                                                 (LONG_PTR)MSIListBox_WndProc );
2073     SetPropW( control->hwnd, szButtonData, info );
2074
2075     msi_listbox_add_items( info );
2076
2077     return ERROR_SUCCESS;
2078 }
2079
2080 /******************** Directory Combo ***************************************/
2081
2082 static void msi_dialog_update_directory_combo( msi_dialog *dialog, msi_control *control )
2083 {
2084     LPWSTR prop, path;
2085     BOOL indirect;
2086
2087     if (!control && !(control = msi_dialog_find_control_by_type( dialog, szDirectoryCombo )))
2088         return;
2089
2090     indirect = control->attributes & msidbControlAttributesIndirect;
2091     prop = msi_dialog_dup_property( dialog, control->property, indirect );
2092
2093     path = msi_dup_property( dialog->package, prop );
2094     if (!path)
2095         path = prop;
2096
2097     PathStripPathW( path );
2098     PathRemoveBackslashW( path );
2099
2100     SendMessageW( control->hwnd, CB_INSERTSTRING, 0, (LPARAM)path );
2101     SendMessageW( control->hwnd, CB_SETCURSEL, 0, 0 );
2102
2103     msi_free( path );
2104     msi_free( prop );
2105 }
2106
2107 static UINT msi_dialog_directory_combo( msi_dialog *dialog, MSIRECORD *rec )
2108 {
2109     msi_control *control;
2110     LPCWSTR prop;
2111     DWORD style;
2112
2113     /* FIXME: use CBS_OWNERDRAWFIXED and add owner draw code */
2114     style = CBS_DROPDOWNLIST | CBS_HASSTRINGS | WS_CHILD |
2115             WS_GROUP | WS_TABSTOP | WS_VSCROLL;
2116     control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
2117     if (!control)
2118         return ERROR_FUNCTION_FAILED;
2119
2120     control->attributes = MSI_RecordGetInteger( rec, 8 );
2121     prop = MSI_RecordGetString( rec, 9 );
2122     control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2123
2124     msi_dialog_update_directory_combo( dialog, control );
2125
2126     return ERROR_SUCCESS;
2127 }
2128
2129 /******************** Directory List ***************************************/
2130
2131 static void msi_dialog_update_directory_list( msi_dialog *dialog, msi_control *control )
2132 {
2133     WCHAR dir_spec[MAX_PATH];
2134     WIN32_FIND_DATAW wfd;
2135     LPWSTR prop, path;
2136     BOOL indirect;
2137     LVITEMW item;
2138     HANDLE file;
2139
2140     static const WCHAR asterisk[] = {'*',0};
2141     static const WCHAR dot[] = {'.',0};
2142     static const WCHAR dotdot[] = {'.','.',0};
2143
2144     if (!control && !(control = msi_dialog_find_control_by_type( dialog, szDirectoryList )))
2145         return;
2146
2147     /* clear the list-view */
2148     SendMessageW( control->hwnd, LVM_DELETEALLITEMS, 0, 0 );
2149
2150     indirect = control->attributes & msidbControlAttributesIndirect;
2151     prop = msi_dialog_dup_property( dialog, control->property, indirect );
2152
2153     path = msi_dup_property( dialog->package, prop );
2154     if (!path)
2155         path = prop;
2156
2157     lstrcpyW( dir_spec, path );
2158     lstrcatW( dir_spec, asterisk );
2159
2160     file = FindFirstFileW( dir_spec, &wfd );
2161     if ( file == INVALID_HANDLE_VALUE )
2162         return;
2163
2164     do
2165     {
2166         if ( wfd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY )
2167             continue;
2168
2169         if ( !lstrcmpW( wfd.cFileName, dot ) || !lstrcmpW( wfd.cFileName, dotdot ) )
2170             continue;
2171
2172         item.mask = LVIF_TEXT;
2173         item.cchTextMax = MAX_PATH;
2174         item.iItem = 0;
2175         item.iSubItem = 0;
2176         item.pszText = wfd.cFileName;
2177
2178         SendMessageW( control->hwnd, LVM_INSERTITEMW, 0, (LPARAM)&item );
2179     } while ( FindNextFileW( file, &wfd ) );
2180
2181     msi_free( prop );
2182     msi_free( path );
2183     FindClose( file );
2184 }
2185
2186 UINT msi_dialog_directorylist_up( msi_dialog *dialog )
2187 {
2188     msi_control *control;
2189     LPWSTR prop, path, ptr;
2190     BOOL indirect;
2191
2192     control = msi_dialog_find_control_by_type( dialog, szDirectoryList );
2193     indirect = control->attributes & msidbControlAttributesIndirect;
2194     prop = msi_dialog_dup_property( dialog, control->property, indirect );
2195
2196     path = msi_dup_property( dialog->package, prop );
2197     if (!path)
2198         path = prop;
2199
2200     /* strip off the last directory */
2201     ptr = PathFindFileNameW( path );
2202     if (ptr != path) *(ptr - 1) = '\0';
2203     PathAddBackslashW( path );
2204
2205     MSI_SetPropertyW( dialog->package, prop, path );
2206
2207     msi_dialog_update_directory_list( dialog, NULL );
2208     msi_dialog_update_directory_combo( dialog, NULL );
2209     msi_dialog_update_pathedit( dialog, NULL );
2210
2211     msi_free( path );
2212     msi_free( prop );
2213
2214     return ERROR_SUCCESS;
2215 }
2216
2217 static UINT msi_dialog_dirlist_handler( msi_dialog *dialog,
2218                                         msi_control *control, WPARAM param )
2219 {
2220     LPNMHDR nmhdr = (LPNMHDR)param;
2221     WCHAR new_path[MAX_PATH];
2222     WCHAR text[MAX_PATH];
2223     LPWSTR path, prop;
2224     BOOL indirect;
2225     LVITEMW item;
2226     int index;
2227
2228     static const WCHAR backslash[] = {'\\',0};
2229
2230     if (nmhdr->code != LVN_ITEMACTIVATE)
2231         return ERROR_SUCCESS;
2232
2233     index = SendMessageW( control->hwnd, LVM_GETNEXTITEM, -1, LVNI_SELECTED );
2234     if ( index < 0 )
2235     {
2236         ERR("No list-view item selected!\n");
2237         return ERROR_FUNCTION_FAILED;
2238     }
2239
2240     item.iSubItem = 0;
2241     item.pszText = text;
2242     item.cchTextMax = MAX_PATH;
2243     SendMessageW( control->hwnd, LVM_GETITEMTEXTW, index, (LPARAM)&item );
2244
2245     indirect = control->attributes & msidbControlAttributesIndirect;
2246     prop = msi_dialog_dup_property( dialog, control->property, indirect );
2247
2248     path = msi_dup_property( dialog->package, prop );
2249     if (!path)
2250         path = prop;
2251
2252     lstrcpyW( new_path, path );
2253     lstrcatW( new_path, text );
2254     lstrcatW( new_path, backslash );
2255
2256     MSI_SetPropertyW( dialog->package, prop, new_path );
2257
2258     msi_dialog_update_directory_list( dialog, NULL );
2259     msi_dialog_update_directory_combo( dialog, NULL );
2260     msi_dialog_update_pathedit( dialog, NULL );
2261
2262     msi_free( prop );
2263     msi_free( path );
2264     return ERROR_SUCCESS;
2265 }
2266
2267 static UINT msi_dialog_directory_list( msi_dialog *dialog, MSIRECORD *rec )
2268 {
2269     msi_control *control;
2270     LPCWSTR prop;
2271     DWORD style;
2272
2273     style = LVS_LIST | WS_VSCROLL | LVS_SHAREIMAGELISTS |
2274             LVS_AUTOARRANGE | LVS_SINGLESEL | WS_BORDER |
2275             LVS_SORTASCENDING | WS_CHILD | WS_GROUP | WS_TABSTOP;
2276     control = msi_dialog_add_control( dialog, rec, WC_LISTVIEWW, style );
2277     if (!control)
2278         return ERROR_FUNCTION_FAILED;
2279
2280     control->attributes = MSI_RecordGetInteger( rec, 8 );
2281     control->handler = msi_dialog_dirlist_handler;
2282     prop = MSI_RecordGetString( rec, 9 );
2283     control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2284
2285     /* double click to activate an item in the list */
2286     SendMessageW( control->hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE,
2287                   0, LVS_EX_TWOCLICKACTIVATE );
2288
2289     msi_dialog_update_directory_list( dialog, control );
2290
2291     return ERROR_SUCCESS;
2292 }
2293
2294 /******************** VolumeCost List ***************************************/
2295
2296 static BOOL str_is_number( LPCWSTR str )
2297 {
2298     int i;
2299
2300     for (i = 0; i < lstrlenW( str ); i++)
2301         if (!isdigitW(str[i]))
2302             return FALSE;
2303
2304     return TRUE;
2305 }
2306
2307 WCHAR column_keys[][80] =
2308 {
2309     {'V','o','l','u','m','e','C','o','s','t','V','o','l','u','m','e',0},
2310     {'V','o','l','u','m','e','C','o','s','t','S','i','z','e',0},
2311     {'V','o','l','u','m','e','C','o','s','t','A','v','a','i','l','a','b','l','e',0},
2312     {'V','o','l','u','m','e','C','o','s','t','R','e','q','u','i','r','e','d',0},
2313     {'V','o','l','u','m','e','C','o','s','t','D','i','f','f','e','r','e','n','c','e',0}
2314 };
2315
2316 static void msi_dialog_vcl_add_columns( msi_dialog *dialog, msi_control *control, MSIRECORD *rec )
2317 {
2318     LPCWSTR text = MSI_RecordGetString( rec, 10 );
2319     LPCWSTR begin = text, end;
2320     WCHAR num[10];
2321     LVCOLUMNW lvc;
2322     DWORD count = 0;
2323
2324     static const WCHAR zero[] = {'0',0};
2325     static const WCHAR negative[] = {'-',0};
2326
2327     while ((begin = strchrW( begin, '{' )) && count < 5)
2328     {
2329         if (!(end = strchrW( begin, '}' )))
2330             return;
2331
2332         lstrcpynW( num, begin + 1, end - begin );
2333         begin += end - begin + 1;
2334
2335         /* empty braces or '0' hides the column */ 
2336         if ( !num[0] || !lstrcmpW( num, zero ) )
2337         {
2338             count++;
2339             continue;
2340         }
2341
2342         /* the width must be a positive number
2343          * if a width is invalid, all remaining columns are hidden
2344          */
2345         if ( !strncmpW( num, negative, 1 ) || !str_is_number( num ) )
2346             return;
2347
2348         ZeroMemory( &lvc, sizeof(lvc) );
2349         lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
2350         lvc.cx = atolW( num );
2351         lvc.pszText = msi_dialog_get_uitext( dialog, column_keys[count] );
2352
2353         SendMessageW( control->hwnd,  LVM_INSERTCOLUMNW, count++, (LPARAM)&lvc );
2354         msi_free( lvc.pszText );
2355     }
2356 }
2357
2358 static void msi_dialog_vcl_add_drives( msi_dialog *dialog, msi_control *control )
2359 {
2360     ULARGE_INTEGER total, free;
2361     WCHAR size_text[MAX_PATH];
2362     LPWSTR drives, ptr;
2363     LVITEMW lvitem;
2364     DWORD size;
2365     int i = 0;
2366
2367     size = GetLogicalDriveStringsW( 0, NULL );
2368     if ( !size ) return;
2369
2370     drives = msi_alloc( (size + 1) * sizeof(WCHAR) );
2371     if ( !drives ) return;
2372
2373     GetLogicalDriveStringsW( size, drives );
2374
2375     ptr = drives;
2376     while (*ptr)
2377     {
2378         lvitem.mask = LVIF_TEXT;
2379         lvitem.iItem = i;
2380         lvitem.iSubItem = 0;
2381         lvitem.pszText = ptr;
2382         lvitem.cchTextMax = lstrlenW(ptr) + 1;
2383         SendMessageW( control->hwnd, LVM_INSERTITEMW, 0, (LPARAM)&lvitem );
2384
2385         GetDiskFreeSpaceExW(ptr, &free, &total, NULL);
2386
2387         StrFormatByteSizeW(total.QuadPart, size_text, MAX_PATH);
2388         lvitem.iSubItem = 1;
2389         lvitem.pszText = size_text;
2390         lvitem.cchTextMax = lstrlenW(size_text) + 1;
2391         SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
2392
2393         StrFormatByteSizeW(free.QuadPart, size_text, MAX_PATH);
2394         lvitem.iSubItem = 2;
2395         lvitem.pszText = size_text;
2396         lvitem.cchTextMax = lstrlenW(size_text) + 1;
2397         SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
2398
2399         ptr += lstrlenW(ptr) + 1;
2400         i++;
2401     }
2402
2403     msi_free( drives );
2404 }
2405
2406 static UINT msi_dialog_volumecost_list( msi_dialog *dialog, MSIRECORD *rec )
2407 {
2408     msi_control *control;
2409     DWORD style;
2410
2411     style = LVS_REPORT | WS_VSCROLL | WS_HSCROLL | LVS_SHAREIMAGELISTS |
2412             LVS_AUTOARRANGE | LVS_SINGLESEL | WS_BORDER |
2413             WS_CHILD | WS_TABSTOP | WS_GROUP;
2414     control = msi_dialog_add_control( dialog, rec, WC_LISTVIEWW, style );
2415     if (!control)
2416         return ERROR_FUNCTION_FAILED;
2417
2418     msi_dialog_vcl_add_columns( dialog, control, rec );
2419     msi_dialog_vcl_add_drives( dialog, control );
2420
2421     return ERROR_SUCCESS;
2422 }
2423
2424 static const struct control_handler msi_dialog_handler[] =
2425 {
2426     { szText, msi_dialog_text_control },
2427     { szPushButton, msi_dialog_button_control },
2428     { szLine, msi_dialog_line_control },
2429     { szBitmap, msi_dialog_bitmap_control },
2430     { szCheckBox, msi_dialog_checkbox_control },
2431     { szScrollableText, msi_dialog_scrolltext_control },
2432     { szComboBox, msi_dialog_combo_control },
2433     { szEdit, msi_dialog_edit_control },
2434     { szMaskedEdit, msi_dialog_maskedit_control },
2435     { szPathEdit, msi_dialog_pathedit_control },
2436     { szProgressBar, msi_dialog_progress_bar },
2437     { szRadioButtonGroup, msi_dialog_radiogroup_control },
2438     { szIcon, msi_dialog_icon_control },
2439     { szSelectionTree, msi_dialog_selection_tree },
2440     { szGroupBox, msi_dialog_group_box },
2441     { szListBox, msi_dialog_list_box },
2442     { szDirectoryCombo, msi_dialog_directory_combo },
2443     { szDirectoryList, msi_dialog_directory_list },
2444     { szVolumeCostList, msi_dialog_volumecost_list },
2445 };
2446
2447 #define NUM_CONTROL_TYPES (sizeof msi_dialog_handler/sizeof msi_dialog_handler[0])
2448
2449 static UINT msi_dialog_create_controls( MSIRECORD *rec, LPVOID param )
2450 {
2451     msi_dialog *dialog = param;
2452     LPCWSTR control_type;
2453     UINT i;
2454
2455     /* find and call the function that can create this type of control */
2456     control_type = MSI_RecordGetString( rec, 3 );
2457     for( i=0; i<NUM_CONTROL_TYPES; i++ )
2458         if (!strcmpiW( msi_dialog_handler[i].control_type, control_type ))
2459             break;
2460     if( i != NUM_CONTROL_TYPES )
2461         msi_dialog_handler[i].func( dialog, rec );
2462     else
2463         ERR("no handler for element type %s\n", debugstr_w(control_type));
2464
2465     return ERROR_SUCCESS;
2466 }
2467
2468 static UINT msi_dialog_fill_controls( msi_dialog *dialog )
2469 {
2470     static const WCHAR query[] = {
2471         'S','E','L','E','C','T',' ','*',' ',
2472         'F','R','O','M',' ','C','o','n','t','r','o','l',' ',
2473         'W','H','E','R','E',' ',
2474            '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
2475     UINT r;
2476     MSIQUERY *view = NULL;
2477     MSIPACKAGE *package = dialog->package;
2478
2479     TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
2480
2481     /* query the Control table for all the elements of the control */
2482     r = MSI_OpenQuery( package->db, &view, query, dialog->name );
2483     if( r != ERROR_SUCCESS )
2484     {
2485         ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
2486         return ERROR_INVALID_PARAMETER;
2487     }
2488
2489     r = MSI_IterateRecords( view, 0, msi_dialog_create_controls, dialog );
2490     msiobj_release( &view->hdr );
2491
2492     return r;
2493 }
2494
2495 static UINT msi_dialog_set_control_condition( MSIRECORD *rec, LPVOID param )
2496 {
2497     static const WCHAR szHide[] = { 'H','i','d','e',0 };
2498     static const WCHAR szShow[] = { 'S','h','o','w',0 };
2499     static const WCHAR szDisable[] = { 'D','i','s','a','b','l','e',0 };
2500     static const WCHAR szEnable[] = { 'E','n','a','b','l','e',0 };
2501     msi_dialog *dialog = param;
2502     msi_control *control;
2503     LPCWSTR name, action, condition;
2504     UINT r;
2505
2506     name = MSI_RecordGetString( rec, 2 );
2507     action = MSI_RecordGetString( rec, 3 );
2508     condition = MSI_RecordGetString( rec, 4 );
2509     r = MSI_EvaluateConditionW( dialog->package, condition );
2510     control = msi_dialog_find_control( dialog, name );
2511     if( r == MSICONDITION_TRUE && control )
2512     {
2513         TRACE("%s control %s\n", debugstr_w(action), debugstr_w(name));
2514
2515         /* FIXME: case sensitive? */
2516         if(!lstrcmpW(action, szHide))
2517             ShowWindow(control->hwnd, SW_HIDE);
2518         else if(!strcmpW(action, szShow))
2519             ShowWindow(control->hwnd, SW_SHOW);
2520         else if(!strcmpW(action, szDisable))
2521             EnableWindow(control->hwnd, FALSE);
2522         else if(!strcmpW(action, szEnable))
2523             EnableWindow(control->hwnd, TRUE);
2524         else
2525             FIXME("Unhandled action %s\n", debugstr_w(action));
2526     }
2527
2528     return ERROR_SUCCESS;
2529 }
2530
2531 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog )
2532 {
2533     static const WCHAR query[] = {
2534       'S','E','L','E','C','T',' ','*',' ',
2535       'F','R','O','M',' ',
2536         'C','o','n','t','r','o','l','C','o','n','d','i','t','i','o','n',' ',
2537       'W','H','E','R','E',' ',
2538         '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0
2539     };
2540     UINT r;
2541     MSIQUERY *view = NULL;
2542     MSIPACKAGE *package = dialog->package;
2543
2544     TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
2545
2546     /* query the Control table for all the elements of the control */
2547     r = MSI_OpenQuery( package->db, &view, query, dialog->name );
2548     if( r != ERROR_SUCCESS )
2549         return ERROR_SUCCESS;
2550
2551     r = MSI_IterateRecords( view, 0, msi_dialog_set_control_condition, dialog );
2552     msiobj_release( &view->hdr );
2553
2554     return r;
2555 }
2556
2557 UINT msi_dialog_reset( msi_dialog *dialog )
2558 {
2559     /* FIXME: should restore the original values of any properties we changed */
2560     return msi_dialog_evaluate_control_conditions( dialog );
2561 }
2562
2563 /* figure out the height of 10 point MS Sans Serif */
2564 static INT msi_dialog_get_sans_serif_height( HWND hwnd )
2565 {
2566     static const WCHAR szSansSerif[] = {
2567         'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0 };
2568     LOGFONTW lf;
2569     TEXTMETRICW tm;
2570     BOOL r;
2571     LONG height = 0;
2572     HFONT hFont, hOldFont;
2573     HDC hdc;
2574
2575     hdc = GetDC( hwnd );
2576     if (hdc)
2577     {
2578         memset( &lf, 0, sizeof lf );
2579         lf.lfHeight = MulDiv(10, GetDeviceCaps(hdc, LOGPIXELSY), 72);
2580         strcpyW( lf.lfFaceName, szSansSerif );
2581         hFont = CreateFontIndirectW(&lf);
2582         if (hFont)
2583         {
2584             hOldFont = SelectObject( hdc, hFont );
2585             r = GetTextMetricsW( hdc, &tm );
2586             if (r)
2587                 height = tm.tmHeight;
2588             SelectObject( hdc, hOldFont );
2589             DeleteObject( hFont );
2590         }
2591         ReleaseDC( hwnd, hdc );
2592     }
2593     return height;
2594 }
2595
2596 /* fetch the associated record from the Dialog table */
2597 static MSIRECORD *msi_get_dialog_record( msi_dialog *dialog )
2598 {
2599     static const WCHAR query[] = {
2600         'S','E','L','E','C','T',' ','*',' ',
2601         'F','R','O','M',' ','D','i','a','l','o','g',' ',
2602         'W','H','E','R','E',' ',
2603            '`','D','i','a','l','o','g','`',' ','=',' ','\'','%','s','\'',0};
2604     MSIPACKAGE *package = dialog->package;
2605     MSIRECORD *rec = NULL;
2606
2607     TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
2608
2609     rec = MSI_QueryGetRecord( package->db, query, dialog->name );
2610     if( !rec )
2611         ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
2612
2613     return rec;
2614 }
2615
2616 static void msi_dialog_adjust_dialog_pos( msi_dialog *dialog, MSIRECORD *rec, LPRECT pos )
2617 {
2618     static const WCHAR szScreenX[] = {'S','c','r','e','e','n','X',0};
2619     static const WCHAR szScreenY[] = {'S','c','r','e','e','n','Y',0};
2620
2621     UINT xres, yres;
2622     POINT center;
2623     SIZE sz;
2624     LONG style;
2625
2626     center.x = MSI_RecordGetInteger( rec, 2 );
2627     center.y = MSI_RecordGetInteger( rec, 3 );
2628
2629     sz.cx = MSI_RecordGetInteger( rec, 4 );
2630     sz.cy = MSI_RecordGetInteger( rec, 5 );
2631
2632     sz.cx = msi_dialog_scale_unit( dialog, sz.cx );
2633     sz.cy = msi_dialog_scale_unit( dialog, sz.cy );
2634
2635     xres = msi_get_property_int( dialog->package, szScreenX, 0 );
2636     yres = msi_get_property_int( dialog->package, szScreenY, 0 );
2637
2638     center.x = MulDiv( center.x, xres, 100 );
2639     center.y = MulDiv( center.y, yres, 100 );
2640
2641     /* turn the client pos into the window rectangle */
2642     if (dialog->package->center_x && dialog->package->center_y)
2643     {
2644         pos->left = dialog->package->center_x - sz.cx / 2.0;
2645         pos->right = pos->left + sz.cx;
2646         pos->top = dialog->package->center_y - sz.cy / 2.0;
2647         pos->bottom = pos->top + sz.cy;
2648     }
2649     else
2650     {
2651         pos->left = center.x - sz.cx/2;
2652         pos->right = pos->left + sz.cx;
2653         pos->top = center.y - sz.cy/2;
2654         pos->bottom = pos->top + sz.cy;
2655
2656         /* save the center */
2657         dialog->package->center_x = center.x;
2658         dialog->package->center_y = center.y;
2659     }
2660
2661     dialog->size.cx = sz.cx;
2662     dialog->size.cy = sz.cy;
2663
2664     TRACE("%lu %lu %lu %lu\n", pos->left, pos->top, pos->right, pos->bottom);
2665
2666     style = GetWindowLongPtrW( dialog->hwnd, GWL_STYLE );
2667     AdjustWindowRect( pos, style, FALSE );
2668 }
2669
2670 static BOOL msi_control_set_next( msi_control *control, msi_control *next )
2671 {
2672     return SetWindowPos( next->hwnd, control->hwnd, 0, 0, 0, 0,
2673                          SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOREDRAW |
2674                          SWP_NOREPOSITION | SWP_NOSENDCHANGING | SWP_NOSIZE );
2675 }
2676
2677 static UINT msi_dialog_set_tab_order( msi_dialog *dialog )
2678 {
2679     msi_control *control, *tab_next;
2680
2681     LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
2682     {
2683         tab_next = msi_dialog_find_control( dialog, control->tabnext );
2684         if( !tab_next )
2685             continue;
2686         msi_control_set_next( control, tab_next );
2687     }
2688
2689     return ERROR_SUCCESS;
2690 }
2691
2692 static void msi_dialog_set_first_control( msi_dialog* dialog, LPCWSTR name )
2693 {
2694     msi_control *control;
2695
2696     control = msi_dialog_find_control( dialog, name );
2697     if( control )
2698         dialog->hWndFocus = control->hwnd;
2699     else
2700         dialog->hWndFocus = NULL;
2701 }
2702
2703 static LRESULT msi_dialog_oncreate( HWND hwnd, LPCREATESTRUCTW cs )
2704 {
2705     static const WCHAR df[] = {
2706         'D','e','f','a','u','l','t','U','I','F','o','n','t',0 };
2707     static const WCHAR dfv[] = {
2708         'M','S',' ','S','h','e','l','l',' ','D','l','g',0 };
2709     msi_dialog *dialog = (msi_dialog*) cs->lpCreateParams;
2710     MSIRECORD *rec = NULL;
2711     LPWSTR title = NULL;
2712     RECT pos;
2713
2714     TRACE("%p %p\n", dialog, dialog->package);
2715
2716     dialog->hwnd = hwnd;
2717     SetWindowLongPtrW( hwnd, GWLP_USERDATA, (LONG_PTR) dialog );
2718
2719     rec = msi_get_dialog_record( dialog );
2720     if( !rec )
2721     {
2722         TRACE("No record found for dialog %s\n", debugstr_w(dialog->name));
2723         return -1;
2724     }
2725
2726     dialog->scale = msi_dialog_get_sans_serif_height(dialog->hwnd);
2727
2728     msi_dialog_adjust_dialog_pos( dialog, rec, &pos );
2729
2730     dialog->attributes = MSI_RecordGetInteger( rec, 6 );
2731
2732     dialog->default_font = msi_dup_property( dialog->package, df );
2733     if (!dialog->default_font)
2734     {
2735         dialog->default_font = strdupW(dfv);
2736         if (!dialog->default_font) return -1;
2737     }
2738
2739     title = msi_get_deformatted_field( dialog->package, rec, 7 );
2740     SetWindowTextW( hwnd, title );
2741     msi_free( title );
2742
2743     SetWindowPos( hwnd, 0, pos.left, pos.top,
2744                   pos.right - pos.left, pos.bottom - pos.top,
2745                   SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW );
2746
2747     msi_dialog_build_font_list( dialog );
2748     msi_dialog_fill_controls( dialog );
2749     msi_dialog_evaluate_control_conditions( dialog );
2750     msi_dialog_set_tab_order( dialog );
2751     msi_dialog_set_first_control( dialog, MSI_RecordGetString( rec, 8 ) );
2752     msiobj_release( &rec->hdr );
2753
2754     return 0;
2755 }
2756
2757 static UINT msi_dialog_send_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
2758 {
2759     LPWSTR event_fmt = NULL, arg_fmt = NULL;
2760
2761     TRACE("Sending control event %s %s\n", debugstr_w(event), debugstr_w(arg));
2762
2763     deformat_string( dialog->package, event, &event_fmt );
2764     deformat_string( dialog->package, arg, &arg_fmt );
2765
2766     dialog->event_handler( dialog->package, event_fmt, arg_fmt, dialog );
2767
2768     msi_free( event_fmt );
2769     msi_free( arg_fmt );
2770
2771     return ERROR_SUCCESS;
2772 }
2773
2774 static UINT msi_dialog_set_property( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
2775 {
2776     static const WCHAR szNullArg[] = { '{','}',0 };
2777     LPWSTR p, prop, arg_fmt = NULL;
2778     UINT len;
2779
2780     len = strlenW(event);
2781     prop = msi_alloc( len*sizeof(WCHAR));
2782     strcpyW( prop, &event[1] );
2783     p = strchrW( prop, ']' );
2784     if( p && p[1] == 0 )
2785     {
2786         *p = 0;
2787         if( strcmpW( szNullArg, arg ) )
2788             deformat_string( dialog->package, arg, &arg_fmt );
2789         MSI_SetPropertyW( dialog->package, prop, arg_fmt );
2790         msi_free( arg_fmt );
2791     }
2792     else
2793         ERR("Badly formatted property string - what happens?\n");
2794     msi_free( prop );
2795     return ERROR_SUCCESS;
2796 }
2797
2798 static UINT msi_dialog_control_event( MSIRECORD *rec, LPVOID param )
2799 {
2800     msi_dialog *dialog = param;
2801     LPCWSTR condition, event, arg;
2802     UINT r;
2803
2804     condition = MSI_RecordGetString( rec, 5 );
2805     r = MSI_EvaluateConditionW( dialog->package, condition );
2806     if( r == MSICONDITION_TRUE || r == MSICONDITION_NONE )
2807     {
2808         event = MSI_RecordGetString( rec, 3 );
2809         arg = MSI_RecordGetString( rec, 4 );
2810         if( event[0] == '[' )
2811             msi_dialog_set_property( dialog, event, arg );
2812         else
2813             msi_dialog_send_event( dialog, event, arg );
2814     }
2815
2816     return ERROR_SUCCESS;
2817 }
2818
2819 static UINT msi_dialog_button_handler( msi_dialog *dialog,
2820                                        msi_control *control, WPARAM param )
2821 {
2822     static const WCHAR query[] = {
2823       'S','E','L','E','C','T',' ','*',' ',
2824       'F','R','O','M',' ','C','o','n','t','r','o','l','E','v','e','n','t',' ',
2825       'W','H','E','R','E',' ',
2826          '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
2827       'A','N','D',' ',
2828          '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',' ',
2829       'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','i','n','g','`',0
2830     };
2831     MSIQUERY *view = NULL;
2832     UINT r;
2833
2834     if( HIWORD(param) != BN_CLICKED )
2835         return ERROR_SUCCESS;
2836
2837     r = MSI_OpenQuery( dialog->package->db, &view, query,
2838                        dialog->name, control->name );
2839     if( r != ERROR_SUCCESS )
2840     {
2841         ERR("query failed\n");
2842         return 0;
2843     }
2844
2845     r = MSI_IterateRecords( view, 0, msi_dialog_control_event, dialog );
2846     msiobj_release( &view->hdr );
2847
2848     return r;
2849 }
2850
2851 static UINT msi_dialog_get_checkbox_state( msi_dialog *dialog,
2852                 msi_control *control )
2853 {
2854     WCHAR state[2] = { 0 };
2855     DWORD sz = 2;
2856
2857     MSI_GetPropertyW( dialog->package, control->property, state, &sz );
2858     return state[0] ? 1 : 0;
2859 }
2860
2861 static void msi_dialog_set_checkbox_state( msi_dialog *dialog,
2862                 msi_control *control, UINT state )
2863 {
2864     static const WCHAR szState[] = { '1', 0 };
2865     LPCWSTR val;
2866
2867     /* if uncheck then the property is set to NULL */
2868     if (!state)
2869     {
2870         MSI_SetPropertyW( dialog->package, control->property, NULL );
2871         return;
2872     }
2873
2874     /* check for a custom state */
2875     if (control->value && control->value[0])
2876         val = control->value;
2877     else
2878         val = szState;
2879
2880     MSI_SetPropertyW( dialog->package, control->property, val );
2881 }
2882
2883 static void msi_dialog_checkbox_sync_state( msi_dialog *dialog,
2884                 msi_control *control )
2885 {
2886     UINT state;
2887
2888     state = msi_dialog_get_checkbox_state( dialog, control );
2889     SendMessageW( control->hwnd, BM_SETCHECK,
2890                   state ? BST_CHECKED : BST_UNCHECKED, 0 );
2891 }
2892
2893 static UINT msi_dialog_checkbox_handler( msi_dialog *dialog,
2894                 msi_control *control, WPARAM param )
2895 {
2896     UINT state;
2897
2898     if( HIWORD(param) != BN_CLICKED )
2899         return ERROR_SUCCESS;
2900
2901     TRACE("clicked checkbox %s, set %s\n", debugstr_w(control->name),
2902           debugstr_w(control->property));
2903
2904     state = msi_dialog_get_checkbox_state( dialog, control );
2905     state = state ? 0 : 1;
2906     msi_dialog_set_checkbox_state( dialog, control, state );
2907     msi_dialog_checkbox_sync_state( dialog, control );
2908
2909     return msi_dialog_button_handler( dialog, control, param );
2910 }
2911
2912 static UINT msi_dialog_edit_handler( msi_dialog *dialog,
2913                 msi_control *control, WPARAM param )
2914 {
2915     LPWSTR buf;
2916
2917     if( HIWORD(param) != EN_CHANGE )
2918         return ERROR_SUCCESS;
2919
2920     TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
2921           debugstr_w(control->property));
2922
2923     buf = msi_get_window_text( control->hwnd );
2924     MSI_SetPropertyW( dialog->package, control->property, buf );
2925
2926     msi_free( buf );
2927
2928     return ERROR_SUCCESS;
2929 }
2930
2931 static UINT msi_dialog_radiogroup_handler( msi_dialog *dialog,
2932                 msi_control *control, WPARAM param )
2933 {
2934     if( HIWORD(param) != BN_CLICKED )
2935         return ERROR_SUCCESS;
2936
2937     TRACE("clicked radio button %s, set %s\n", debugstr_w(control->name),
2938           debugstr_w(control->property));
2939
2940     MSI_SetPropertyW( dialog->package, control->property, control->name );
2941
2942     return msi_dialog_button_handler( dialog, control, param );
2943 }
2944
2945 static LRESULT msi_dialog_oncommand( msi_dialog *dialog, WPARAM param, HWND hwnd )
2946 {
2947     msi_control *control = NULL;
2948
2949     TRACE("%p %p %08x\n", dialog, hwnd, param);
2950
2951     switch (param)
2952     {
2953     case 1: /* enter */
2954         control = msi_dialog_find_control( dialog, dialog->control_default );
2955         break;
2956     case 2: /* escape */
2957         control = msi_dialog_find_control( dialog, dialog->control_cancel );
2958         break;
2959     default: 
2960         control = msi_dialog_find_control_by_hwnd( dialog, hwnd );
2961     }
2962
2963     if( control )
2964     {
2965         if( control->handler )
2966         {
2967             control->handler( dialog, control, param );
2968             msi_dialog_evaluate_control_conditions( dialog );
2969         }
2970     }
2971     else
2972         ERR("button click from nowhere %p %d %p\n", dialog, param, hwnd);
2973     return 0;
2974 }
2975
2976 static LRESULT msi_dialog_onnotify( msi_dialog *dialog, LPARAM param )
2977 {
2978     LPNMHDR nmhdr = (LPNMHDR) param;
2979     msi_control *control = msi_dialog_find_control_by_hwnd( dialog, nmhdr->hwndFrom );
2980
2981     TRACE("%p %p\n", dialog, nmhdr->hwndFrom);
2982
2983     if ( control && control->handler )
2984         control->handler( dialog, control, param );
2985
2986     return 0;
2987 }
2988
2989 static void msi_dialog_setfocus( msi_dialog *dialog )
2990 {
2991     HWND hwnd = dialog->hWndFocus;
2992
2993     hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, TRUE);
2994     hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, FALSE);
2995     SetFocus( hwnd );
2996     dialog->hWndFocus = hwnd;
2997 }
2998
2999 static LRESULT WINAPI MSIDialog_WndProc( HWND hwnd, UINT msg,
3000                 WPARAM wParam, LPARAM lParam )
3001 {
3002     msi_dialog *dialog = (LPVOID) GetWindowLongPtrW( hwnd, GWLP_USERDATA );
3003
3004     TRACE("0x%04x\n", msg);
3005
3006     switch (msg)
3007     {
3008     case WM_MOVE:
3009         dialog->package->center_x = LOWORD(lParam) + dialog->size.cx / 2.0;
3010         dialog->package->center_y = HIWORD(lParam) + dialog->size.cy / 2.0;
3011         break;
3012         
3013     case WM_CREATE:
3014         return msi_dialog_oncreate( hwnd, (LPCREATESTRUCTW)lParam );
3015
3016     case WM_COMMAND:
3017         return msi_dialog_oncommand( dialog, wParam, (HWND)lParam );
3018
3019     case WM_ACTIVATE:
3020         if( LOWORD(wParam) == WA_INACTIVE )
3021             dialog->hWndFocus = GetFocus();
3022         else
3023             msi_dialog_setfocus( dialog );
3024         return 0;
3025
3026     case WM_SETFOCUS:
3027         msi_dialog_setfocus( dialog );
3028         return 0;
3029
3030     /* bounce back to our subclassed static control */
3031     case WM_CTLCOLORSTATIC:
3032         return SendMessageW( (HWND) lParam, WM_CTLCOLORSTATIC, wParam, lParam );
3033
3034     case WM_DESTROY:
3035         dialog->hwnd = NULL;
3036         return 0;
3037     case WM_NOTIFY:
3038         return msi_dialog_onnotify( dialog, lParam );
3039     }
3040     return DefWindowProcW(hwnd, msg, wParam, lParam);
3041 }
3042
3043 static BOOL CALLBACK msi_radioground_child_enum( HWND hWnd, LPARAM lParam )
3044 {
3045     EnableWindow( hWnd, lParam );
3046     return TRUE;
3047 }
3048
3049 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
3050 {
3051     WNDPROC oldproc = (WNDPROC) GetPropW(hWnd, szButtonData);
3052     LRESULT r;
3053
3054     TRACE("hWnd %p msg %04x wParam 0x%08x lParam 0x%08lx\n", hWnd, msg, wParam, lParam);
3055
3056     if (msg == WM_COMMAND) /* Forward notifications to dialog */
3057         SendMessageW(GetParent(hWnd), msg, wParam, lParam);
3058
3059     r = CallWindowProcW(oldproc, hWnd, msg, wParam, lParam);
3060
3061     /* make sure the radio buttons show as disabled if the parent is disabled */
3062     if (msg == WM_ENABLE)
3063         EnumChildWindows( hWnd, msi_radioground_child_enum, wParam );
3064
3065     return r;
3066 }
3067
3068 static LRESULT WINAPI MSIHiddenWindowProc( HWND hwnd, UINT msg,
3069                 WPARAM wParam, LPARAM lParam )
3070 {
3071     msi_dialog *dialog = (msi_dialog*) lParam;
3072
3073     TRACE("%d %p\n", msg, dialog);
3074
3075     switch (msg)
3076     {
3077     case WM_MSI_DIALOG_CREATE:
3078         return msi_dialog_run_message_loop( dialog );
3079     case WM_MSI_DIALOG_DESTROY:
3080         msi_dialog_destroy( dialog );
3081         return 0;
3082     }
3083     return DefWindowProcW( hwnd, msg, wParam, lParam );
3084 }
3085
3086 /* functions that interface to other modules within MSI */
3087
3088 msi_dialog *msi_dialog_create( MSIPACKAGE* package,
3089                                LPCWSTR szDialogName, msi_dialog *parent,
3090                                msi_dialog_event_handler event_handler )
3091 {
3092     MSIRECORD *rec = NULL;
3093     msi_dialog *dialog;
3094
3095     TRACE("%p %s\n", package, debugstr_w(szDialogName));
3096
3097     /* allocate the structure for the dialog to use */
3098     dialog = msi_alloc_zero( sizeof *dialog + sizeof(WCHAR)*strlenW(szDialogName) );
3099     if( !dialog )
3100         return NULL;
3101     strcpyW( dialog->name, szDialogName );
3102     dialog->parent = parent;
3103     msiobj_addref( &package->hdr );
3104     dialog->package = package;
3105     dialog->event_handler = event_handler;
3106     dialog->finished = 0;
3107     list_init( &dialog->controls );
3108
3109     /* verify that the dialog exists */
3110     rec = msi_get_dialog_record( dialog );
3111     if( !rec )
3112     {
3113         msiobj_release( &package->hdr );
3114         msi_free( dialog );
3115         return NULL;
3116     }
3117     dialog->attributes = MSI_RecordGetInteger( rec, 6 );
3118     dialog->control_default = strdupW( MSI_RecordGetString( rec, 9 ) );
3119     dialog->control_cancel = strdupW( MSI_RecordGetString( rec, 10 ) );
3120     msiobj_release( &rec->hdr );
3121
3122     return dialog;
3123 }
3124
3125 static void msi_process_pending_messages( HWND hdlg )
3126 {
3127     MSG msg;
3128
3129     while( PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ) )
3130     {
3131         if( hdlg && IsDialogMessageW( hdlg, &msg ))
3132             continue;
3133         TranslateMessage( &msg );
3134         DispatchMessageW( &msg );
3135     }
3136 }
3137
3138 void msi_dialog_end_dialog( msi_dialog *dialog )
3139 {
3140     TRACE("%p\n", dialog);
3141     dialog->finished = 1;
3142     PostMessageW(dialog->hwnd, WM_NULL, 0, 0);
3143 }
3144
3145 void msi_dialog_check_messages( HANDLE handle )
3146 {
3147     DWORD r;
3148
3149     /* in threads other than the UI thread, block */
3150     if( uiThreadId != GetCurrentThreadId() )
3151     {
3152         if( handle )
3153             WaitForSingleObject( handle, INFINITE );
3154         return;
3155     }
3156
3157     /* there's two choices for the UI thread */
3158     while (1)
3159     {
3160         msi_process_pending_messages( NULL );
3161
3162         if( !handle )
3163             break;
3164
3165         /*
3166          * block here until somebody creates a new dialog or
3167          * the handle we're waiting on becomes ready
3168          */
3169         r = MsgWaitForMultipleObjects( 1, &handle, 0, INFINITE, QS_ALLINPUT );
3170         if( r == WAIT_OBJECT_0 )
3171             break;
3172     }
3173 }
3174
3175 UINT msi_dialog_run_message_loop( msi_dialog *dialog )
3176 {
3177     DWORD style;
3178     HWND hwnd;
3179
3180     if( uiThreadId != GetCurrentThreadId() )
3181         return SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_CREATE, 0, (LPARAM) dialog );
3182
3183     /* create the dialog window, don't show it yet */
3184     style = WS_OVERLAPPED;
3185     if( dialog->attributes & msidbDialogAttributesVisible )
3186         style |= WS_VISIBLE;
3187
3188     hwnd = CreateWindowW( szMsiDialogClass, dialog->name, style,
3189                      CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
3190                      NULL, NULL, NULL, dialog );
3191     if( !hwnd )
3192     {
3193         ERR("Failed to create dialog %s\n", debugstr_w( dialog->name ));
3194         return ERROR_FUNCTION_FAILED;
3195     }
3196
3197     ShowWindow( hwnd, SW_SHOW );
3198     /* UpdateWindow( hwnd ); - and causes the transparent static controls not to paint */
3199
3200     if( dialog->attributes & msidbDialogAttributesModal )
3201     {
3202         while( !dialog->finished )
3203         {
3204             MsgWaitForMultipleObjects( 0, NULL, 0, INFINITE, QS_ALLINPUT );
3205             msi_process_pending_messages( dialog->hwnd );
3206         }
3207     }
3208     else
3209         return ERROR_IO_PENDING;
3210
3211     return ERROR_SUCCESS;
3212 }
3213
3214 void msi_dialog_do_preview( msi_dialog *dialog )
3215 {
3216     TRACE("\n");
3217     dialog->attributes |= msidbDialogAttributesVisible;
3218     dialog->attributes &= ~msidbDialogAttributesModal;
3219     msi_dialog_run_message_loop( dialog );
3220 }
3221
3222 void msi_dialog_destroy( msi_dialog *dialog )
3223 {
3224     if( uiThreadId != GetCurrentThreadId() )
3225     {
3226         SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_DESTROY, 0, (LPARAM) dialog );
3227         return;
3228     }
3229
3230     if( dialog->hwnd )
3231         ShowWindow( dialog->hwnd, SW_HIDE );
3232     
3233     if( dialog->hwnd )
3234         DestroyWindow( dialog->hwnd );
3235
3236     /* destroy the list of controls */
3237     while( !list_empty( &dialog->controls ) )
3238     {
3239         msi_control *t = LIST_ENTRY( list_head( &dialog->controls ),
3240                                      msi_control, entry );
3241         list_remove( &t->entry );
3242         /* leave dialog->hwnd - destroying parent destroys child windows */
3243         msi_free( t->property );
3244         msi_free( t->value );
3245         if( t->hBitmap )
3246             DeleteObject( t->hBitmap );
3247         if( t->hIcon )
3248             DestroyIcon( t->hIcon );
3249         msi_free( t->tabnext );
3250         msi_free( t->type );
3251         msi_free( t );
3252         if (t->hDll)
3253             FreeLibrary( t->hDll );
3254     }
3255
3256     /* destroy the list of fonts */
3257     while( dialog->font_list )
3258     {
3259         msi_font *t = dialog->font_list;
3260         dialog->font_list = t->next;
3261         DeleteObject( t->hfont );
3262         msi_free( t );
3263     }
3264     msi_free( dialog->default_font );
3265
3266     msi_free( dialog->control_default );
3267     msi_free( dialog->control_cancel );
3268     msiobj_release( &dialog->package->hdr );
3269     dialog->package = NULL;
3270     msi_free( dialog );
3271 }
3272
3273 BOOL msi_dialog_register_class( void )
3274 {
3275     WNDCLASSW cls;
3276
3277     ZeroMemory( &cls, sizeof cls );
3278     cls.lpfnWndProc   = MSIDialog_WndProc;
3279     cls.hInstance     = NULL;
3280     cls.hIcon         = LoadIconW(0, (LPWSTR)IDI_APPLICATION);
3281     cls.hCursor       = LoadCursorW(0, (LPWSTR)IDC_ARROW);
3282     cls.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
3283     cls.lpszMenuName  = NULL;
3284     cls.lpszClassName = szMsiDialogClass;
3285
3286     if( !RegisterClassW( &cls ) )
3287         return FALSE;
3288
3289     cls.lpfnWndProc   = MSIHiddenWindowProc;
3290     cls.lpszClassName = szMsiHiddenWindow;
3291
3292     if( !RegisterClassW( &cls ) )
3293         return FALSE;
3294
3295     uiThreadId = GetCurrentThreadId();
3296
3297     hMsiHiddenWindow = CreateWindowW( szMsiHiddenWindow, NULL, WS_OVERLAPPED,
3298                                    0, 0, 100, 100, NULL, NULL, NULL, NULL );
3299     if( !hMsiHiddenWindow )
3300         return FALSE;
3301
3302     return TRUE;
3303 }
3304
3305 void msi_dialog_unregister_class( void )
3306 {
3307     DestroyWindow( hMsiHiddenWindow );
3308     hMsiHiddenWindow = NULL;
3309     UnregisterClassW( szMsiDialogClass, NULL );
3310     UnregisterClassW( szMsiHiddenWindow, NULL );
3311     uiThreadId = 0;
3312 }