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