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