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