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