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