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