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