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