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