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