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