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