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