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