msi: Cleanup the dialog event subscriptions when destroying the dialog.
[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     DWORD attributes, style;
1128
1129     style = CBS_AUTOHSCROLL | WS_TABSTOP | WS_GROUP | WS_CHILD;
1130     attributes = MSI_RecordGetInteger( rec, 8 );
1131     if ( ~attributes & msidbControlAttributesSorted)
1132         style |= CBS_SORT;
1133     if ( attributes & msidbControlAttributesComboList)
1134         style |= CBS_DROPDOWNLIST;
1135     else
1136         style |= CBS_DROPDOWN;
1137
1138     msi_dialog_add_control( dialog, rec, szCombo, style );
1139     return ERROR_SUCCESS;
1140 }
1141
1142 static UINT msi_dialog_edit_control( msi_dialog *dialog, MSIRECORD *rec )
1143 {
1144     msi_control *control;
1145     LPCWSTR prop;
1146     LPWSTR val;
1147
1148     control = msi_dialog_add_control( dialog, rec, szEdit,
1149                                       WS_BORDER | WS_TABSTOP );
1150     control->handler = msi_dialog_edit_handler;
1151     prop = MSI_RecordGetString( rec, 9 );
1152     if( prop )
1153         control->property = strdupW( prop );
1154     val = msi_dup_property( dialog->package, control->property );
1155     SetWindowTextW( control->hwnd, val );
1156     msi_free( val );
1157     return ERROR_SUCCESS;
1158 }
1159
1160 /******************** Masked Edit ********************************************/
1161
1162 #define MASK_MAX_GROUPS 20
1163
1164 struct msi_mask_group
1165 {
1166     UINT len;
1167     UINT ofs;
1168     WCHAR type;
1169     HWND hwnd;
1170 };
1171
1172 struct msi_maskedit_info
1173 {
1174     msi_dialog *dialog;
1175     WNDPROC oldproc;
1176     HWND hwnd;
1177     LPWSTR prop;
1178     UINT num_chars;
1179     UINT num_groups;
1180     struct msi_mask_group group[MASK_MAX_GROUPS];
1181 };
1182
1183 static BOOL msi_mask_editable( WCHAR type )
1184 {
1185     switch (type)
1186     {
1187     case '%':
1188     case '#':
1189     case '&':
1190     case '`':
1191     case '?':
1192     case '^':
1193         return TRUE;
1194     }
1195     return FALSE;
1196 }
1197
1198 static void msi_mask_control_change( struct msi_maskedit_info *info )
1199 {
1200     LPWSTR val;
1201     UINT i, n, r;
1202
1203     val = msi_alloc( (info->num_chars+1)*sizeof(WCHAR) );
1204     for( i=0, n=0; i<info->num_groups; i++ )
1205     {
1206         if( (info->group[i].len + n) > info->num_chars )
1207         {
1208             ERR("can't fit control %d text into template\n",i);
1209             break;
1210         }
1211         if (!msi_mask_editable(info->group[i].type))
1212         {
1213             for(r=0; r<info->group[i].len; r++)
1214                 val[n+r] = info->group[i].type;
1215             val[n+r] = 0;
1216         }
1217         else
1218         {
1219             r = GetWindowTextW( info->group[i].hwnd, &val[n], info->group[i].len+1 );
1220             if( r != info->group[i].len )
1221                 break;
1222         }
1223         n += r;
1224     }
1225
1226     TRACE("%d/%d controls were good\n", i, info->num_groups);
1227
1228     if( i == info->num_groups )
1229     {
1230         TRACE("Set property %s to %s\n",
1231               debugstr_w(info->prop), debugstr_w(val) );
1232         CharUpperBuffW( val, info->num_chars );
1233         MSI_SetPropertyW( info->dialog->package, info->prop, val );
1234         msi_dialog_evaluate_control_conditions( info->dialog );
1235     }
1236     msi_free( val );
1237 }
1238
1239 /* now move to the next control if necessary */
1240 static VOID msi_mask_next_control( struct msi_maskedit_info *info, HWND hWnd )
1241 {
1242     HWND hWndNext;
1243     UINT len, i;
1244
1245     for( i=0; i<info->num_groups; i++ )
1246         if( info->group[i].hwnd == hWnd )
1247             break;
1248
1249     /* don't move from the last control */
1250     if( i >= (info->num_groups-1) )
1251         return;
1252
1253     len = SendMessageW( hWnd, WM_GETTEXTLENGTH, 0, 0 );
1254     if( len < info->group[i].len )
1255         return;
1256
1257     hWndNext = GetNextDlgTabItem( GetParent( hWnd ), hWnd, FALSE );
1258     SetFocus( hWndNext );
1259 }
1260
1261 static LRESULT WINAPI
1262 MSIMaskedEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1263 {
1264     struct msi_maskedit_info *info;
1265     HRESULT r;
1266
1267     TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
1268
1269     info = GetPropW(hWnd, szButtonData);
1270
1271     r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1272
1273     switch( msg )
1274     {
1275     case WM_COMMAND:
1276         if (HIWORD(wParam) == EN_CHANGE)
1277         {
1278             msi_mask_control_change( info );
1279             msi_mask_next_control( info, (HWND) lParam );
1280         }
1281         break;
1282     case WM_NCDESTROY:
1283         msi_free( info->prop );
1284         msi_free( info );
1285         RemovePropW( hWnd, szButtonData );
1286         break;
1287     }
1288
1289     return r;
1290 }
1291
1292 /* fish the various bits of the property out and put them in the control */
1293 static void
1294 msi_maskedit_set_text( struct msi_maskedit_info *info, LPCWSTR text )
1295 {
1296     LPCWSTR p;
1297     UINT i;
1298
1299     p = text;
1300     for( i = 0; i < info->num_groups; i++ )
1301     {
1302         if( info->group[i].len < lstrlenW( p ) )
1303         {
1304             LPWSTR chunk = strdupW( p );
1305             chunk[ info->group[i].len ] = 0;
1306             SetWindowTextW( info->group[i].hwnd, chunk );
1307             msi_free( chunk );
1308         }
1309         else
1310         {
1311             SetWindowTextW( info->group[i].hwnd, p );
1312             break;
1313         }
1314         p += info->group[i].len;
1315     }
1316 }
1317
1318 static struct msi_maskedit_info * msi_dialog_parse_groups( LPCWSTR mask )
1319 {
1320     struct msi_maskedit_info * info = NULL;
1321     int i = 0, n = 0, total = 0;
1322     LPCWSTR p;
1323
1324     TRACE("masked control, template %s\n", debugstr_w(mask));
1325
1326     if( !mask )
1327         return info;
1328
1329     info = msi_alloc_zero( sizeof *info );
1330     if( !info )
1331         return info;
1332
1333     p = strchrW(mask, '<');
1334     if( p )
1335         p++;
1336     else
1337         p = mask;
1338
1339     for( i=0; i<MASK_MAX_GROUPS; i++ )
1340     {
1341         /* stop at the end of the string */
1342         if( p[0] == 0 || p[0] == '>' )
1343             break;
1344
1345         /* count the number of the same identifier */
1346         for( n=0; p[n] == p[0]; n++ )
1347             ;
1348         info->group[i].ofs = total;
1349         info->group[i].type = p[0];
1350         if( p[n] == '=' )
1351         {
1352             n++;
1353             total++; /* an extra not part of the group */
1354         }
1355         info->group[i].len = n;
1356         total += n;
1357         p += n;
1358     }
1359
1360     TRACE("%d characters in %d groups\n", total, i );
1361     if( i == MASK_MAX_GROUPS )
1362         ERR("too many groups in PIDTemplate %s\n", debugstr_w(mask));
1363
1364     info->num_chars = total;
1365     info->num_groups = i;
1366
1367     return info;
1368 }
1369
1370 static void
1371 msi_maskedit_create_children( struct msi_maskedit_info *info, LPCWSTR font )
1372 {
1373     DWORD width, height, style, wx, ww;
1374     RECT rect;
1375     HWND hwnd;
1376     UINT i;
1377
1378     style = WS_CHILD | WS_BORDER | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL;
1379
1380     GetClientRect( info->hwnd, &rect );
1381
1382     width = rect.right - rect.left;
1383     height = rect.bottom - rect.top;
1384
1385     for( i = 0; i < info->num_groups; i++ )
1386     {
1387         if (!msi_mask_editable( info->group[i].type ))
1388             continue;
1389         wx = (info->group[i].ofs * width) / info->num_chars;
1390         ww = (info->group[i].len * width) / info->num_chars;
1391
1392         hwnd = CreateWindowW( szEdit, NULL, style, wx, 0, ww, height,
1393                               info->hwnd, NULL, NULL, NULL );
1394         if( !hwnd )
1395         {
1396             ERR("failed to create mask edit sub window\n");
1397             break;
1398         }
1399
1400         SendMessageW( hwnd, EM_LIMITTEXT, info->group[i].len, 0 );
1401
1402         msi_dialog_set_font( info->dialog, hwnd,
1403                              font?font:info->dialog->default_font );
1404         info->group[i].hwnd = hwnd;
1405     }
1406 }
1407
1408 /*
1409  * office 2003 uses "73931<````=````=````=````=`````>@@@@@"
1410  * delphi 7 uses "<????-??????-??????-????>" and "<???-???>"
1411  * filemaker pro 7 uses "<^^^^=^^^^=^^^^=^^^^=^^^^=^^^^=^^^^^>"
1412  */
1413 static UINT msi_dialog_maskedit_control( msi_dialog *dialog, MSIRECORD *rec )
1414 {
1415     LPWSTR font_mask, val = NULL, font;
1416     struct msi_maskedit_info *info = NULL;
1417     UINT ret = ERROR_SUCCESS;
1418     msi_control *control;
1419     LPCWSTR prop, mask;
1420
1421     TRACE("\n");
1422
1423     font_mask = msi_get_deformatted_field( dialog->package, rec, 10 );
1424     font = msi_dialog_get_style( font_mask, &mask );
1425     if( !mask )
1426     {
1427         ERR("mask template is empty\n");
1428         goto end;
1429     }
1430
1431     info = msi_dialog_parse_groups( mask );
1432     if( !info )
1433     {
1434         ERR("template %s is invalid\n", debugstr_w(mask));
1435         goto end;
1436     }
1437
1438     info->dialog = dialog;
1439
1440     control = msi_dialog_add_control( dialog, rec, szStatic,
1441                    SS_OWNERDRAW | WS_GROUP | WS_VISIBLE );
1442     if( !control )
1443     {
1444         ERR("Failed to create maskedit container\n");
1445         ret = ERROR_FUNCTION_FAILED;
1446         goto end;
1447     }
1448     SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
1449
1450     info->hwnd = control->hwnd;
1451
1452     /* subclass the static control */
1453     info->oldproc = (WNDPROC) SetWindowLongPtrW( info->hwnd, GWLP_WNDPROC,
1454                                           (LONG_PTR)MSIMaskedEdit_WndProc );
1455     SetPropW( control->hwnd, szButtonData, info );
1456
1457     prop = MSI_RecordGetString( rec, 9 );
1458     if( prop )
1459         info->prop = strdupW( prop );
1460
1461     msi_maskedit_create_children( info, font );
1462
1463     if( prop )
1464     {
1465         val = msi_dup_property( dialog->package, prop );
1466         if( val )
1467         {
1468             msi_maskedit_set_text( info, val );
1469             msi_free( val );
1470         }
1471     }
1472
1473 end:
1474     if( ret != ERROR_SUCCESS )
1475         msi_free( info );
1476     msi_free( font_mask );
1477     msi_free( font );
1478     return ret;
1479 }
1480
1481 /******************** Progress Bar *****************************************/
1482
1483 static UINT msi_dialog_progress_bar( msi_dialog *dialog, MSIRECORD *rec )
1484 {
1485     msi_dialog_add_control( dialog, rec, PROGRESS_CLASSW, WS_VISIBLE );
1486     return ERROR_SUCCESS;
1487 }
1488
1489 /******************** Path Edit ********************************************/
1490
1491 struct msi_pathedit_info
1492 {
1493     msi_dialog *dialog;
1494     msi_control *control;
1495     WNDPROC oldproc;
1496 };
1497
1498 static LPWSTR msi_get_window_text( HWND hwnd )
1499 {
1500     UINT sz, r;
1501     LPWSTR buf;
1502
1503     sz = 0x20;
1504     buf = msi_alloc( sz*sizeof(WCHAR) );
1505     while ( buf )
1506     {
1507         r = GetWindowTextW( hwnd, buf, sz );
1508         if ( r < (sz - 1) )
1509             break;
1510         sz *= 2;
1511         buf = msi_realloc( buf, sz*sizeof(WCHAR) );
1512     }
1513
1514     return buf;
1515 }
1516
1517 static void msi_dialog_update_pathedit( msi_dialog *dialog, msi_control *control )
1518 {
1519     LPWSTR prop, path;
1520     BOOL indirect;
1521
1522     if (!control && !(control = msi_dialog_find_control_by_type( dialog, szPathEdit )))
1523        return;
1524
1525     indirect = control->attributes & msidbControlAttributesIndirect;
1526     prop = msi_dialog_dup_property( dialog, control->property, indirect );
1527     path = msi_dialog_dup_property( dialog, prop, TRUE );
1528
1529     SetWindowTextW( control->hwnd, path );
1530     SendMessageW( control->hwnd, EM_SETSEL, 0, -1 );
1531
1532     msi_free( path );
1533     msi_free( prop );
1534 }
1535
1536 /* FIXME: test when this should fail */
1537 static BOOL msi_dialog_verify_path( LPWSTR path )
1538 {
1539     if ( !lstrlenW( path ) )
1540         return FALSE;
1541
1542     if ( PathIsRelativeW( path ) )
1543         return FALSE;
1544
1545     return TRUE;
1546 }
1547
1548 /* returns TRUE if the path is valid, FALSE otherwise */
1549 static BOOL msi_dialog_onkillfocus( msi_dialog *dialog, msi_control *control )
1550 {
1551     LPWSTR buf, prop;
1552     BOOL indirect;
1553     BOOL valid;
1554
1555     indirect = control->attributes & msidbControlAttributesIndirect;
1556     prop = msi_dialog_dup_property( dialog, control->property, indirect );
1557
1558     buf = msi_get_window_text( control->hwnd );
1559
1560     if ( !msi_dialog_verify_path( buf ) )
1561     {
1562         /* FIXME: display an error message box */
1563         ERR("Invalid path %s\n", debugstr_w( buf ));
1564         valid = FALSE;
1565         SetFocus( control->hwnd );
1566     }
1567     else
1568     {
1569         valid = TRUE;
1570         MSI_SetPropertyW( dialog->package, prop, buf );
1571     }
1572
1573     msi_dialog_update_pathedit( dialog, control );
1574
1575     TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
1576           debugstr_w(prop));
1577
1578     msi_free( buf );
1579     msi_free( prop );
1580
1581     return valid;
1582 }
1583
1584 static LRESULT WINAPI MSIPathEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1585 {
1586     struct msi_pathedit_info *info = GetPropW(hWnd, szButtonData);
1587     LRESULT r = 0;
1588
1589     TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
1590
1591     if ( msg == WM_KILLFOCUS )
1592     {
1593         /* if the path is invalid, don't handle this message */
1594         if ( !msi_dialog_onkillfocus( info->dialog, info->control ) )
1595             return 0;
1596     }
1597
1598     r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1599
1600     if ( msg == WM_NCDESTROY )
1601     {
1602         msi_free( info );
1603         RemovePropW( hWnd, szButtonData );
1604     }
1605
1606     return r;
1607 }
1608
1609 static UINT msi_dialog_pathedit_control( msi_dialog *dialog, MSIRECORD *rec )
1610 {
1611     struct msi_pathedit_info *info;
1612     msi_control *control;
1613     LPCWSTR prop;
1614
1615     info = msi_alloc( sizeof *info );
1616     if (!info)
1617         return ERROR_FUNCTION_FAILED;
1618
1619     control = msi_dialog_add_control( dialog, rec, szEdit,
1620                                       WS_BORDER | WS_TABSTOP );
1621     control->attributes = MSI_RecordGetInteger( rec, 8 );
1622     prop = MSI_RecordGetString( rec, 9 );
1623     control->property = msi_dialog_dup_property( dialog, prop, FALSE );
1624
1625     info->dialog = dialog;
1626     info->control = control;
1627     info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1628                                                  (LONG_PTR)MSIPathEdit_WndProc );
1629     SetPropW( control->hwnd, szButtonData, info );
1630
1631     msi_dialog_update_pathedit( dialog, control );
1632
1633     return ERROR_SUCCESS;
1634 }
1635
1636 /* radio buttons are a bit different from normal controls */
1637 static UINT msi_dialog_create_radiobutton( MSIRECORD *rec, LPVOID param )
1638 {
1639     radio_button_group_descr *group = (radio_button_group_descr *)param;
1640     msi_dialog *dialog = group->dialog;
1641     msi_control *control;
1642     LPCWSTR prop, text, name;
1643     DWORD style, attributes = group->attributes;
1644
1645     style = WS_CHILD | BS_AUTORADIOBUTTON | BS_MULTILINE | WS_TABSTOP;
1646     name = MSI_RecordGetString( rec, 3 );
1647     text = MSI_RecordGetString( rec, 8 );
1648     if( attributes & msidbControlAttributesVisible )
1649         style |= WS_VISIBLE;
1650     if( ~attributes & msidbControlAttributesEnabled )
1651         style |= WS_DISABLED;
1652
1653     control = msi_dialog_create_window( dialog, rec, 0, szButton, name, text,
1654                                         style, group->parent->hwnd );
1655     if (!control)
1656         return ERROR_FUNCTION_FAILED;
1657     control->handler = msi_dialog_radiogroup_handler;
1658
1659     if (!lstrcmpW(control->name, group->propval))
1660         SendMessageW(control->hwnd, BM_SETCHECK, BST_CHECKED, 0);
1661
1662     prop = MSI_RecordGetString( rec, 1 );
1663     if( prop )
1664         control->property = strdupW( prop );
1665
1666     return ERROR_SUCCESS;
1667 }
1668
1669 static UINT msi_dialog_radiogroup_control( msi_dialog *dialog, MSIRECORD *rec )
1670 {
1671     static const WCHAR query[] = {
1672         'S','E','L','E','C','T',' ','*',' ',
1673         'F','R','O','M',' ','R','a','d','i','o','B','u','t','t','o','n',' ',
1674         'W','H','E','R','E',' ',
1675            '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
1676     UINT r;
1677     LPCWSTR prop;
1678     msi_control *control;
1679     MSIQUERY *view = NULL;
1680     radio_button_group_descr group;
1681     MSIPACKAGE *package = dialog->package;
1682     WNDPROC oldproc;
1683
1684     prop = MSI_RecordGetString( rec, 9 );
1685
1686     TRACE("%p %p %s\n", dialog, rec, debugstr_w( prop ));
1687
1688     /* Create parent group box to hold radio buttons */
1689     control = msi_dialog_add_control( dialog, rec, szButton, BS_OWNERDRAW|WS_GROUP );
1690     if( !control )
1691         return ERROR_FUNCTION_FAILED;
1692
1693     oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1694                                            (LONG_PTR)MSIRadioGroup_WndProc );
1695     SetPropW(control->hwnd, szButtonData, oldproc);
1696     SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
1697
1698     if( prop )
1699         control->property = strdupW( prop );
1700
1701     /* query the Radio Button table for all control in this group */
1702     r = MSI_OpenQuery( package->db, &view, query, prop );
1703     if( r != ERROR_SUCCESS )
1704     {
1705         ERR("query failed for dialog %s radio group %s\n", 
1706             debugstr_w(dialog->name), debugstr_w(prop));
1707         return ERROR_INVALID_PARAMETER;
1708     }
1709
1710     group.dialog = dialog;
1711     group.parent = control;
1712     group.attributes = MSI_RecordGetInteger( rec, 8 );
1713     group.propval = msi_dup_property( dialog->package, control->property );
1714
1715     r = MSI_IterateRecords( view, 0, msi_dialog_create_radiobutton, &group );
1716     msiobj_release( &view->hdr );
1717     msi_free( group.propval );
1718
1719     return r;
1720 }
1721
1722 /******************** Selection Tree ***************************************/
1723
1724 struct msi_selection_tree_info
1725 {
1726     msi_dialog *dialog;
1727     HWND hwnd;
1728     WNDPROC oldproc;
1729     HTREEITEM selected;
1730 };
1731
1732 static void
1733 msi_seltree_sync_item_state( HWND hwnd, MSIFEATURE *feature, HTREEITEM hItem )
1734 {
1735     TVITEMW tvi;
1736     DWORD index = feature->Action;
1737
1738     TRACE("Feature %s -> %d %d %d\n", debugstr_w(feature->Title),
1739         feature->Installed, feature->Action, feature->ActionRequest);
1740
1741     if (index == INSTALLSTATE_UNKNOWN)
1742         index = INSTALLSTATE_ABSENT;
1743
1744     tvi.mask = TVIF_STATE;
1745     tvi.hItem = hItem;
1746     tvi.state = INDEXTOSTATEIMAGEMASK( index );
1747     tvi.stateMask = TVIS_STATEIMAGEMASK;
1748
1749     SendMessageW( hwnd, TVM_SETITEMW, 0, (LPARAM) &tvi );
1750 }
1751
1752 static UINT
1753 msi_seltree_popup_menu( HWND hwnd, INT x, INT y )
1754 {
1755     HMENU hMenu;
1756     INT r;
1757
1758     /* create a menu to display */
1759     hMenu = CreatePopupMenu();
1760
1761     /* FIXME: load strings from resources */
1762     AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_LOCAL, "Install feature locally");
1763     AppendMenuA( hMenu, MF_GRAYED, 0x1000, "Install entire feature");
1764     AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ADVERTISED, "Install on demand");
1765     AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ABSENT, "Don't install");
1766     r = TrackPopupMenu( hMenu, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RETURNCMD,
1767                         x, y, 0, hwnd, NULL );
1768     DestroyMenu( hMenu );
1769     return r;
1770 }
1771
1772 static MSIFEATURE *
1773 msi_seltree_feature_from_item( HWND hwnd, HTREEITEM hItem )
1774 {
1775     TVITEMW tvi;
1776
1777     /* get the feature from the item */
1778     memset( &tvi, 0, sizeof tvi );
1779     tvi.hItem = hItem;
1780     tvi.mask = TVIF_PARAM | TVIF_HANDLE;
1781     SendMessageW( hwnd, TVM_GETITEMW, 0, (LPARAM) &tvi );
1782
1783     return (MSIFEATURE*) tvi.lParam;
1784 }
1785
1786 static LRESULT
1787 msi_seltree_menu( HWND hwnd, HTREEITEM hItem )
1788 {
1789     struct msi_selection_tree_info *info;
1790     MSIFEATURE *feature;
1791     MSIPACKAGE *package;
1792     union {
1793         RECT rc;
1794         POINT pt[2];
1795         HTREEITEM hItem;
1796     } u;
1797     UINT r;
1798
1799     info = GetPropW(hwnd, szButtonData);
1800     package = info->dialog->package;
1801
1802     feature = msi_seltree_feature_from_item( hwnd, hItem );
1803     if (!feature)
1804     {
1805         ERR("item %p feature was NULL\n", hItem);
1806         return 0;
1807     }
1808
1809     /* get the item's rectangle to put the menu just below it */
1810     u.hItem = hItem;
1811     SendMessageW( hwnd, TVM_GETITEMRECT, 0, (LPARAM) &u.rc );
1812     MapWindowPoints( hwnd, NULL, u.pt, 2 );
1813
1814     r = msi_seltree_popup_menu( hwnd, u.rc.left, u.rc.top );
1815
1816     switch (r)
1817     {
1818     case INSTALLSTATE_LOCAL:
1819     case INSTALLSTATE_ADVERTISED:
1820     case INSTALLSTATE_ABSENT:
1821         msi_feature_set_state( feature, r );
1822         break;
1823     default:
1824         FIXME("select feature and all children\n");
1825     }
1826
1827     /* update */
1828     msi_seltree_sync_item_state( hwnd, feature, hItem );
1829     ACTION_UpdateComponentStates( package, feature->Feature );
1830
1831     return 0;
1832 }
1833
1834 static MSIFEATURE *msi_seltree_get_selected_feature( msi_control *control )
1835 {
1836     struct msi_selection_tree_info *info = GetPropW(control->hwnd, szButtonData);
1837     return msi_seltree_feature_from_item( control->hwnd, info->selected );
1838 }
1839
1840 static LRESULT WINAPI
1841 MSISelectionTree_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1842 {
1843     struct msi_selection_tree_info *info;
1844     TVHITTESTINFO tvhti;
1845     HRESULT r;
1846
1847     TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
1848
1849     info = GetPropW(hWnd, szButtonData);
1850
1851     switch( msg )
1852     {
1853     case WM_LBUTTONDOWN:
1854         tvhti.pt.x = (short)LOWORD( lParam );
1855         tvhti.pt.y = (short)HIWORD( lParam );
1856         tvhti.flags = 0;
1857         tvhti.hItem = 0;
1858         r = CallWindowProcW(info->oldproc, hWnd, TVM_HITTEST, 0, (LPARAM) &tvhti );
1859         if (tvhti.flags & TVHT_ONITEMSTATEICON)
1860             return msi_seltree_menu( hWnd, tvhti.hItem );
1861         break;
1862     }
1863
1864     r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1865
1866     switch( msg )
1867     {
1868     case WM_NCDESTROY:
1869         msi_free( info );
1870         RemovePropW( hWnd, szButtonData );
1871         break;
1872     }
1873     return r;
1874 }
1875
1876 static void
1877 msi_seltree_add_child_features( MSIPACKAGE *package, HWND hwnd,
1878                                 LPCWSTR parent, HTREEITEM hParent )
1879 {
1880     struct msi_selection_tree_info *info = GetPropW( hwnd, szButtonData );
1881     MSIFEATURE *feature;
1882     TVINSERTSTRUCTW tvis;
1883     HTREEITEM hitem, hfirst = NULL;
1884
1885     LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
1886     {
1887         if ( lstrcmpW( parent, feature->Feature_Parent ) )
1888             continue;
1889
1890         if ( !feature->Title )
1891             continue;
1892
1893         if ( !feature->Display )
1894             continue;
1895
1896         memset( &tvis, 0, sizeof tvis );
1897         tvis.hParent = hParent;
1898         tvis.hInsertAfter = TVI_LAST;
1899         tvis.u.item.mask = TVIF_TEXT | TVIF_PARAM;
1900         tvis.u.item.pszText = feature->Title;
1901         tvis.u.item.lParam = (LPARAM) feature;
1902
1903         hitem = (HTREEITEM) SendMessageW( hwnd, TVM_INSERTITEMW, 0, (LPARAM) &tvis );
1904         if (!hitem)
1905             continue;
1906
1907         if (!hfirst)
1908             hfirst = hitem;
1909
1910         msi_seltree_sync_item_state( hwnd, feature, hitem );
1911         msi_seltree_add_child_features( package, hwnd,
1912                                         feature->Feature, hitem );
1913
1914         /* the node is expanded if Display is odd */
1915         if ( feature->Display % 2 != 0 )
1916             SendMessageW( hwnd, TVM_EXPAND, TVE_EXPAND, (LPARAM) hitem );
1917     }
1918
1919     /* select the first item */
1920     SendMessageW( hwnd, TVM_SELECTITEM, TVGN_CARET | TVGN_DROPHILITE, (LPARAM) hfirst );
1921     info->selected = hfirst;
1922 }
1923
1924 static void msi_seltree_create_imagelist( HWND hwnd )
1925 {
1926     const int bm_width = 32, bm_height = 16, bm_count = 3;
1927     const int bm_resource = 0x1001;
1928     HIMAGELIST himl;
1929     int i;
1930     HBITMAP hbmp;
1931
1932     himl = ImageList_Create( bm_width, bm_height, FALSE, 4, 0 );
1933     if (!himl)
1934     {
1935         ERR("failed to create image list\n");
1936         return;
1937     }
1938
1939     for (i=0; i<bm_count; i++)
1940     {
1941         hbmp = LoadBitmapW( msi_hInstance, MAKEINTRESOURCEW(i+bm_resource) );
1942         if (!hbmp)
1943         {
1944             ERR("failed to load bitmap %d\n", i);
1945             break;
1946         }
1947
1948         /*
1949          * Add a dummy bitmap at offset zero because the treeview
1950          * can't use it as a state mask (zero means no user state).
1951          */
1952         if (!i)
1953             ImageList_Add( himl, hbmp, NULL );
1954
1955         ImageList_Add( himl, hbmp, NULL );
1956     }
1957
1958     SendMessageW( hwnd, TVM_SETIMAGELIST, TVSIL_STATE, (LPARAM)himl );
1959 }
1960
1961 static UINT msi_dialog_seltree_handler( msi_dialog *dialog,
1962                                         msi_control *control, WPARAM param )
1963 {
1964     struct msi_selection_tree_info *info = GetPropW( control->hwnd, szButtonData );
1965     LPNMTREEVIEWW tv = (LPNMTREEVIEWW)param;
1966     MSIRECORD *row, *rec;
1967     MSIFOLDER *folder;
1968     LPCWSTR dir;
1969     UINT r = ERROR_SUCCESS;
1970
1971     static const WCHAR select[] = {
1972         'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1973         '`','F','e','a','t','u','r','e','`',' ','W','H','E','R','E',' ',
1974         '`','T','i','t','l','e','`',' ','=',' ','\'','%','s','\'',0
1975     };
1976
1977     if (tv->hdr.code != TVN_SELCHANGINGW)
1978         return ERROR_SUCCESS;
1979
1980     info->selected = tv->itemNew.hItem;
1981
1982     row = MSI_QueryGetRecord( dialog->package->db, select, tv->itemNew.pszText );
1983     if (!row)
1984         return ERROR_FUNCTION_FAILED;
1985
1986     rec = MSI_CreateRecord( 1 );
1987
1988     MSI_RecordSetStringW( rec, 1, MSI_RecordGetString( row, 4 ) );
1989     ControlEvent_FireSubscribedEvent( dialog->package, szSelectionDescription, rec );
1990
1991     dir = MSI_RecordGetString( row, 7 );
1992     folder = get_loaded_folder( dialog->package, dir );
1993     if (!folder)
1994     {
1995         r = ERROR_FUNCTION_FAILED;
1996         goto done;
1997     }
1998
1999     MSI_RecordSetStringW( rec, 1, folder->ResolvedTarget );
2000     ControlEvent_FireSubscribedEvent( dialog->package, szSelectionPath, rec );
2001
2002 done:
2003     msiobj_release(&row->hdr);
2004     msiobj_release(&rec->hdr);
2005
2006     return r;
2007 }
2008
2009 static UINT msi_dialog_selection_tree( msi_dialog *dialog, MSIRECORD *rec )
2010 {
2011     msi_control *control;
2012     LPCWSTR prop;
2013     MSIPACKAGE *package = dialog->package;
2014     DWORD style;
2015     struct msi_selection_tree_info *info;
2016
2017     info = msi_alloc( sizeof *info );
2018     if (!info)
2019         return ERROR_FUNCTION_FAILED;
2020
2021     /* create the treeview control */
2022     style = TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT;
2023     style |= WS_GROUP | WS_VSCROLL;
2024     control = msi_dialog_add_control( dialog, rec, WC_TREEVIEWW, style );
2025     if (!control)
2026     {
2027         msi_free(info);
2028         return ERROR_FUNCTION_FAILED;
2029     }
2030
2031     control->handler = msi_dialog_seltree_handler;
2032     control->attributes = MSI_RecordGetInteger( rec, 8 );
2033     prop = MSI_RecordGetString( rec, 9 );
2034     control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2035
2036     /* subclass */
2037     info->dialog = dialog;
2038     info->hwnd = control->hwnd;
2039     info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2040                                           (LONG_PTR)MSISelectionTree_WndProc );
2041     SetPropW( control->hwnd, szButtonData, info );
2042
2043     ControlEvent_SubscribeToEvent( dialog->package, dialog,
2044                                    szSelectionPath, control->name, szProperty );
2045
2046     /* initialize it */
2047     msi_seltree_create_imagelist( control->hwnd );
2048     msi_seltree_add_child_features( package, control->hwnd, NULL, NULL );
2049
2050     return ERROR_SUCCESS;
2051 }
2052
2053 /******************** Group Box ***************************************/
2054
2055 static UINT msi_dialog_group_box( msi_dialog *dialog, MSIRECORD *rec )
2056 {
2057     msi_control *control;
2058     DWORD style;
2059
2060     style = BS_GROUPBOX | WS_CHILD | WS_GROUP;
2061     control = msi_dialog_add_control( dialog, rec, WC_BUTTONW, style );
2062     if (!control)
2063         return ERROR_FUNCTION_FAILED;
2064
2065     return ERROR_SUCCESS;
2066 }
2067
2068 /******************** List Box ***************************************/
2069
2070 struct msi_listbox_info
2071 {
2072     msi_dialog *dialog;
2073     HWND hwnd;
2074     WNDPROC oldproc;
2075     DWORD num_items;
2076     DWORD addpos_items;
2077     LPWSTR *items;
2078 };
2079
2080 static LRESULT WINAPI MSIListBox_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2081 {
2082     struct msi_listbox_info *info;
2083     LRESULT r;
2084     DWORD j;
2085
2086     TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
2087
2088     info = GetPropW( hWnd, szButtonData );
2089     if (!info)
2090         return 0;
2091
2092     r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
2093
2094     switch( msg )
2095     {
2096     case WM_NCDESTROY:
2097         for (j = 0; j < info->num_items; j++)
2098             msi_free( info->items[j] );
2099         msi_free( info->items );
2100         msi_free( info );
2101         RemovePropW( hWnd, szButtonData );
2102         break;
2103     }
2104
2105     return r;
2106 }
2107
2108 static UINT msi_listbox_add_item( MSIRECORD *rec, LPVOID param )
2109 {
2110     struct msi_listbox_info *info = param;
2111     LPCWSTR value, text;
2112     int pos;
2113
2114     value = MSI_RecordGetString( rec, 3 );
2115     text = MSI_RecordGetString( rec, 4 );
2116
2117     info->items[info->addpos_items] = strdupW( value );
2118
2119     pos = SendMessageW( info->hwnd, LB_ADDSTRING, 0, (LPARAM)text );
2120     SendMessageW( info->hwnd, LB_SETITEMDATA, pos, (LPARAM)info->items[info->addpos_items] );
2121     info->addpos_items++;
2122     return ERROR_SUCCESS;
2123 }
2124
2125 static UINT msi_listbox_add_items( struct msi_listbox_info *info, LPCWSTR property )
2126 {
2127     UINT r;
2128     MSIQUERY *view = NULL;
2129     DWORD count;
2130
2131     static const WCHAR query[] = {
2132         'S','E','L','E','C','T',' ','*',' ',
2133         'F','R','O','M',' ','`','L','i','s','t','B','o','x','`',' ',
2134         'W','H','E','R','E',' ',
2135         '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',' ',
2136         'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','`',0
2137     };
2138
2139     r = MSI_OpenQuery( info->dialog->package->db, &view, query, property );
2140     if ( r != ERROR_SUCCESS )
2141         return r;
2142
2143     /* just get the number of records */
2144     r = MSI_IterateRecords( view, &count, NULL, NULL );
2145
2146     info->num_items = count;
2147     info->items = msi_alloc( sizeof(*info->items) * count );
2148
2149     r = MSI_IterateRecords( view, NULL, msi_listbox_add_item, info );
2150     msiobj_release( &view->hdr );
2151
2152     return r;
2153 }
2154
2155 static UINT msi_dialog_listbox_handler( msi_dialog *dialog,
2156                                         msi_control *control, WPARAM param )
2157 {
2158     struct msi_listbox_info *info;
2159     int index;
2160     LPCWSTR value;
2161
2162     if( HIWORD(param) != LBN_SELCHANGE )
2163         return ERROR_SUCCESS;
2164
2165     info = GetPropW( control->hwnd, szButtonData );
2166     index = SendMessageW( control->hwnd, LB_GETCURSEL, 0, 0 );
2167     value = (LPCWSTR) SendMessageW( control->hwnd, LB_GETITEMDATA, index, 0 );
2168
2169     MSI_SetPropertyW( info->dialog->package,
2170                       control->property, value );
2171     msi_dialog_evaluate_control_conditions( info->dialog );
2172
2173     return ERROR_SUCCESS;
2174 }
2175
2176 static UINT msi_dialog_list_box( msi_dialog *dialog, MSIRECORD *rec )
2177 {
2178     struct msi_listbox_info *info;
2179     msi_control *control;
2180     DWORD attributes, style;
2181     LPCWSTR prop;
2182
2183     info = msi_alloc( sizeof *info );
2184     if (!info)
2185         return ERROR_FUNCTION_FAILED;
2186
2187     style = WS_TABSTOP | WS_GROUP | WS_CHILD | LBS_NOTIFY | WS_VSCROLL | WS_BORDER;
2188     attributes = MSI_RecordGetInteger( rec, 8 );
2189     if (~attributes & msidbControlAttributesSorted)
2190         style |= LBS_SORT;
2191
2192     control = msi_dialog_add_control( dialog, rec, WC_LISTBOXW, style );
2193     if (!control)
2194         return ERROR_FUNCTION_FAILED;
2195
2196     control->handler = msi_dialog_listbox_handler;
2197
2198     prop = MSI_RecordGetString( rec, 9 );
2199     control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2200
2201     /* subclass */
2202     info->dialog = dialog;
2203     info->hwnd = control->hwnd;
2204     info->items = NULL;
2205     info->addpos_items = 0;
2206     info->oldproc = (WNDPROC)SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2207                                                 (LONG_PTR)MSIListBox_WndProc );
2208     SetPropW( control->hwnd, szButtonData, info );
2209
2210     if ( control->property )
2211         msi_listbox_add_items( info, control->property );
2212
2213     return ERROR_SUCCESS;
2214 }
2215
2216 /******************** Directory Combo ***************************************/
2217
2218 static void msi_dialog_update_directory_combo( msi_dialog *dialog, msi_control *control )
2219 {
2220     LPWSTR prop, path;
2221     BOOL indirect;
2222
2223     if (!control && !(control = msi_dialog_find_control_by_type( dialog, szDirectoryCombo )))
2224         return;
2225
2226     indirect = control->attributes & msidbControlAttributesIndirect;
2227     prop = msi_dialog_dup_property( dialog, control->property, indirect );
2228     path = msi_dialog_dup_property( dialog, prop, TRUE );
2229
2230     PathStripPathW( path );
2231     PathRemoveBackslashW( path );
2232
2233     SendMessageW( control->hwnd, CB_INSERTSTRING, 0, (LPARAM)path );
2234     SendMessageW( control->hwnd, CB_SETCURSEL, 0, 0 );
2235
2236     msi_free( path );
2237     msi_free( prop );
2238 }
2239
2240 static UINT msi_dialog_directory_combo( msi_dialog *dialog, MSIRECORD *rec )
2241 {
2242     msi_control *control;
2243     LPCWSTR prop;
2244     DWORD style;
2245
2246     /* FIXME: use CBS_OWNERDRAWFIXED and add owner draw code */
2247     style = CBS_DROPDOWNLIST | CBS_HASSTRINGS | WS_CHILD |
2248             WS_GROUP | WS_TABSTOP | WS_VSCROLL;
2249     control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
2250     if (!control)
2251         return ERROR_FUNCTION_FAILED;
2252
2253     control->attributes = MSI_RecordGetInteger( rec, 8 );
2254     prop = MSI_RecordGetString( rec, 9 );
2255     control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2256
2257     msi_dialog_update_directory_combo( dialog, control );
2258
2259     return ERROR_SUCCESS;
2260 }
2261
2262 /******************** Directory List ***************************************/
2263
2264 static void msi_dialog_update_directory_list( msi_dialog *dialog, msi_control *control )
2265 {
2266     WCHAR dir_spec[MAX_PATH];
2267     WIN32_FIND_DATAW wfd;
2268     LPWSTR prop, path;
2269     BOOL indirect;
2270     LVITEMW item;
2271     HANDLE file;
2272
2273     static const WCHAR asterisk[] = {'*',0};
2274     static const WCHAR dot[] = {'.',0};
2275     static const WCHAR dotdot[] = {'.','.',0};
2276
2277     if (!control && !(control = msi_dialog_find_control_by_type( dialog, szDirectoryList )))
2278         return;
2279
2280     /* clear the list-view */
2281     SendMessageW( control->hwnd, LVM_DELETEALLITEMS, 0, 0 );
2282
2283     indirect = control->attributes & msidbControlAttributesIndirect;
2284     prop = msi_dialog_dup_property( dialog, control->property, indirect );
2285     path = msi_dialog_dup_property( dialog, prop, TRUE );
2286
2287     lstrcpyW( dir_spec, path );
2288     lstrcatW( dir_spec, asterisk );
2289
2290     file = FindFirstFileW( dir_spec, &wfd );
2291     if ( file == INVALID_HANDLE_VALUE )
2292         return;
2293
2294     do
2295     {
2296         if ( wfd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY )
2297             continue;
2298
2299         if ( !lstrcmpW( wfd.cFileName, dot ) || !lstrcmpW( wfd.cFileName, dotdot ) )
2300             continue;
2301
2302         item.mask = LVIF_TEXT;
2303         item.cchTextMax = MAX_PATH;
2304         item.iItem = 0;
2305         item.iSubItem = 0;
2306         item.pszText = wfd.cFileName;
2307
2308         SendMessageW( control->hwnd, LVM_INSERTITEMW, 0, (LPARAM)&item );
2309     } while ( FindNextFileW( file, &wfd ) );
2310
2311     msi_free( prop );
2312     msi_free( path );
2313     FindClose( file );
2314 }
2315
2316 UINT msi_dialog_directorylist_up( msi_dialog *dialog )
2317 {
2318     msi_control *control;
2319     LPWSTR prop, path, ptr;
2320     BOOL indirect;
2321
2322     control = msi_dialog_find_control_by_type( dialog, szDirectoryList );
2323     indirect = control->attributes & msidbControlAttributesIndirect;
2324     prop = msi_dialog_dup_property( dialog, control->property, indirect );
2325     path = msi_dialog_dup_property( dialog, prop, TRUE );
2326
2327     /* strip off the last directory */
2328     ptr = PathFindFileNameW( path );
2329     if (ptr != path) *(ptr - 1) = '\0';
2330     PathAddBackslashW( path );
2331
2332     MSI_SetPropertyW( dialog->package, prop, path );
2333
2334     msi_dialog_update_directory_list( dialog, NULL );
2335     msi_dialog_update_directory_combo( dialog, NULL );
2336     msi_dialog_update_pathedit( dialog, NULL );
2337
2338     msi_free( path );
2339     msi_free( prop );
2340
2341     return ERROR_SUCCESS;
2342 }
2343
2344 static UINT msi_dialog_dirlist_handler( msi_dialog *dialog,
2345                                         msi_control *control, WPARAM param )
2346 {
2347     LPNMHDR nmhdr = (LPNMHDR)param;
2348     WCHAR new_path[MAX_PATH];
2349     WCHAR text[MAX_PATH];
2350     LPWSTR path, prop;
2351     BOOL indirect;
2352     LVITEMW item;
2353     int index;
2354
2355     static const WCHAR backslash[] = {'\\',0};
2356
2357     if (nmhdr->code != LVN_ITEMACTIVATE)
2358         return ERROR_SUCCESS;
2359
2360     index = SendMessageW( control->hwnd, LVM_GETNEXTITEM, -1, LVNI_SELECTED );
2361     if ( index < 0 )
2362     {
2363         ERR("No list-view item selected!\n");
2364         return ERROR_FUNCTION_FAILED;
2365     }
2366
2367     item.iSubItem = 0;
2368     item.pszText = text;
2369     item.cchTextMax = MAX_PATH;
2370     SendMessageW( control->hwnd, LVM_GETITEMTEXTW, index, (LPARAM)&item );
2371
2372     indirect = control->attributes & msidbControlAttributesIndirect;
2373     prop = msi_dialog_dup_property( dialog, control->property, indirect );
2374     path = msi_dialog_dup_property( dialog, prop, TRUE );
2375
2376     lstrcpyW( new_path, path );
2377     lstrcatW( new_path, text );
2378     lstrcatW( new_path, backslash );
2379
2380     MSI_SetPropertyW( dialog->package, prop, new_path );
2381
2382     msi_dialog_update_directory_list( dialog, NULL );
2383     msi_dialog_update_directory_combo( dialog, NULL );
2384     msi_dialog_update_pathedit( dialog, NULL );
2385
2386     msi_free( prop );
2387     msi_free( path );
2388     return ERROR_SUCCESS;
2389 }
2390
2391 static UINT msi_dialog_directory_list( msi_dialog *dialog, MSIRECORD *rec )
2392 {
2393     msi_control *control;
2394     LPCWSTR prop;
2395     DWORD style;
2396
2397     style = LVS_LIST | WS_VSCROLL | LVS_SHAREIMAGELISTS |
2398             LVS_AUTOARRANGE | LVS_SINGLESEL | WS_BORDER |
2399             LVS_SORTASCENDING | WS_CHILD | WS_GROUP | WS_TABSTOP;
2400     control = msi_dialog_add_control( dialog, rec, WC_LISTVIEWW, style );
2401     if (!control)
2402         return ERROR_FUNCTION_FAILED;
2403
2404     control->attributes = MSI_RecordGetInteger( rec, 8 );
2405     control->handler = msi_dialog_dirlist_handler;
2406     prop = MSI_RecordGetString( rec, 9 );
2407     control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2408
2409     /* double click to activate an item in the list */
2410     SendMessageW( control->hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE,
2411                   0, LVS_EX_TWOCLICKACTIVATE );
2412
2413     msi_dialog_update_directory_list( dialog, control );
2414
2415     return ERROR_SUCCESS;
2416 }
2417
2418 /******************** VolumeCost List ***************************************/
2419
2420 static BOOL str_is_number( LPCWSTR str )
2421 {
2422     int i;
2423
2424     for (i = 0; i < lstrlenW( str ); i++)
2425         if (!isdigitW(str[i]))
2426             return FALSE;
2427
2428     return TRUE;
2429 }
2430
2431 static const WCHAR column_keys[][80] =
2432 {
2433     {'V','o','l','u','m','e','C','o','s','t','V','o','l','u','m','e',0},
2434     {'V','o','l','u','m','e','C','o','s','t','S','i','z','e',0},
2435     {'V','o','l','u','m','e','C','o','s','t','A','v','a','i','l','a','b','l','e',0},
2436     {'V','o','l','u','m','e','C','o','s','t','R','e','q','u','i','r','e','d',0},
2437     {'V','o','l','u','m','e','C','o','s','t','D','i','f','f','e','r','e','n','c','e',0}
2438 };
2439
2440 static void msi_dialog_vcl_add_columns( msi_dialog *dialog, msi_control *control, MSIRECORD *rec )
2441 {
2442     LPCWSTR text = MSI_RecordGetString( rec, 10 );
2443     LPCWSTR begin = text, end;
2444     WCHAR num[10];
2445     LVCOLUMNW lvc;
2446     DWORD count = 0;
2447
2448     static const WCHAR zero[] = {'0',0};
2449     static const WCHAR negative[] = {'-',0};
2450
2451     while ((begin = strchrW( begin, '{' )) && count < 5)
2452     {
2453         if (!(end = strchrW( begin, '}' )))
2454             return;
2455
2456         lstrcpynW( num, begin + 1, end - begin );
2457         begin += end - begin + 1;
2458
2459         /* empty braces or '0' hides the column */ 
2460         if ( !num[0] || !lstrcmpW( num, zero ) )
2461         {
2462             count++;
2463             continue;
2464         }
2465
2466         /* the width must be a positive number
2467          * if a width is invalid, all remaining columns are hidden
2468          */
2469         if ( !strncmpW( num, negative, 1 ) || !str_is_number( num ) )
2470             return;
2471
2472         ZeroMemory( &lvc, sizeof(lvc) );
2473         lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
2474         lvc.cx = atolW( num );
2475         lvc.pszText = msi_dialog_get_uitext( dialog, column_keys[count] );
2476
2477         SendMessageW( control->hwnd,  LVM_INSERTCOLUMNW, count++, (LPARAM)&lvc );
2478         msi_free( lvc.pszText );
2479     }
2480 }
2481
2482 static void msi_dialog_vcl_add_drives( msi_dialog *dialog, msi_control *control )
2483 {
2484     ULARGE_INTEGER total, free;
2485     WCHAR size_text[MAX_PATH];
2486     LPWSTR drives, ptr;
2487     LVITEMW lvitem;
2488     DWORD size;
2489     int i = 0;
2490
2491     size = GetLogicalDriveStringsW( 0, NULL );
2492     if ( !size ) return;
2493
2494     drives = msi_alloc( (size + 1) * sizeof(WCHAR) );
2495     if ( !drives ) return;
2496
2497     GetLogicalDriveStringsW( size, drives );
2498
2499     ptr = drives;
2500     while (*ptr)
2501     {
2502         lvitem.mask = LVIF_TEXT;
2503         lvitem.iItem = i;
2504         lvitem.iSubItem = 0;
2505         lvitem.pszText = ptr;
2506         lvitem.cchTextMax = lstrlenW(ptr) + 1;
2507         SendMessageW( control->hwnd, LVM_INSERTITEMW, 0, (LPARAM)&lvitem );
2508
2509         GetDiskFreeSpaceExW(ptr, &free, &total, NULL);
2510
2511         StrFormatByteSizeW(total.QuadPart, size_text, MAX_PATH);
2512         lvitem.iSubItem = 1;
2513         lvitem.pszText = size_text;
2514         lvitem.cchTextMax = lstrlenW(size_text) + 1;
2515         SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
2516
2517         StrFormatByteSizeW(free.QuadPart, size_text, MAX_PATH);
2518         lvitem.iSubItem = 2;
2519         lvitem.pszText = size_text;
2520         lvitem.cchTextMax = lstrlenW(size_text) + 1;
2521         SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
2522
2523         ptr += lstrlenW(ptr) + 1;
2524         i++;
2525     }
2526
2527     msi_free( drives );
2528 }
2529
2530 static UINT msi_dialog_volumecost_list( msi_dialog *dialog, MSIRECORD *rec )
2531 {
2532     msi_control *control;
2533     DWORD style;
2534
2535     style = LVS_REPORT | WS_VSCROLL | WS_HSCROLL | LVS_SHAREIMAGELISTS |
2536             LVS_AUTOARRANGE | LVS_SINGLESEL | WS_BORDER |
2537             WS_CHILD | WS_TABSTOP | WS_GROUP;
2538     control = msi_dialog_add_control( dialog, rec, WC_LISTVIEWW, style );
2539     if (!control)
2540         return ERROR_FUNCTION_FAILED;
2541
2542     msi_dialog_vcl_add_columns( dialog, control, rec );
2543     msi_dialog_vcl_add_drives( dialog, control );
2544
2545     return ERROR_SUCCESS;
2546 }
2547
2548 static const struct control_handler msi_dialog_handler[] =
2549 {
2550     { szText, msi_dialog_text_control },
2551     { szPushButton, msi_dialog_button_control },
2552     { szLine, msi_dialog_line_control },
2553     { szBitmap, msi_dialog_bitmap_control },
2554     { szCheckBox, msi_dialog_checkbox_control },
2555     { szScrollableText, msi_dialog_scrolltext_control },
2556     { szComboBox, msi_dialog_combo_control },
2557     { szEdit, msi_dialog_edit_control },
2558     { szMaskedEdit, msi_dialog_maskedit_control },
2559     { szPathEdit, msi_dialog_pathedit_control },
2560     { szProgressBar, msi_dialog_progress_bar },
2561     { szRadioButtonGroup, msi_dialog_radiogroup_control },
2562     { szIcon, msi_dialog_icon_control },
2563     { szSelectionTree, msi_dialog_selection_tree },
2564     { szGroupBox, msi_dialog_group_box },
2565     { szListBox, msi_dialog_list_box },
2566     { szDirectoryCombo, msi_dialog_directory_combo },
2567     { szDirectoryList, msi_dialog_directory_list },
2568     { szVolumeCostList, msi_dialog_volumecost_list },
2569 };
2570
2571 #define NUM_CONTROL_TYPES (sizeof msi_dialog_handler/sizeof msi_dialog_handler[0])
2572
2573 static UINT msi_dialog_create_controls( MSIRECORD *rec, LPVOID param )
2574 {
2575     msi_dialog *dialog = param;
2576     LPCWSTR control_type;
2577     UINT i;
2578
2579     /* find and call the function that can create this type of control */
2580     control_type = MSI_RecordGetString( rec, 3 );
2581     for( i=0; i<NUM_CONTROL_TYPES; i++ )
2582         if (!strcmpiW( msi_dialog_handler[i].control_type, control_type ))
2583             break;
2584     if( i != NUM_CONTROL_TYPES )
2585         msi_dialog_handler[i].func( dialog, rec );
2586     else
2587         ERR("no handler for element type %s\n", debugstr_w(control_type));
2588
2589     return ERROR_SUCCESS;
2590 }
2591
2592 static UINT msi_dialog_fill_controls( msi_dialog *dialog )
2593 {
2594     static const WCHAR query[] = {
2595         'S','E','L','E','C','T',' ','*',' ',
2596         'F','R','O','M',' ','C','o','n','t','r','o','l',' ',
2597         'W','H','E','R','E',' ',
2598            '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
2599     UINT r;
2600     MSIQUERY *view = NULL;
2601     MSIPACKAGE *package = dialog->package;
2602
2603     TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
2604
2605     /* query the Control table for all the elements of the control */
2606     r = MSI_OpenQuery( package->db, &view, query, dialog->name );
2607     if( r != ERROR_SUCCESS )
2608     {
2609         ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
2610         return ERROR_INVALID_PARAMETER;
2611     }
2612
2613     r = MSI_IterateRecords( view, 0, msi_dialog_create_controls, dialog );
2614     msiobj_release( &view->hdr );
2615
2616     return r;
2617 }
2618
2619 static UINT msi_dialog_set_control_condition( MSIRECORD *rec, LPVOID param )
2620 {
2621     static const WCHAR szHide[] = { 'H','i','d','e',0 };
2622     static const WCHAR szShow[] = { 'S','h','o','w',0 };
2623     static const WCHAR szDisable[] = { 'D','i','s','a','b','l','e',0 };
2624     static const WCHAR szEnable[] = { 'E','n','a','b','l','e',0 };
2625     static const WCHAR szDefault[] = { 'D','e','f','a','u','l','t',0 };
2626     msi_dialog *dialog = param;
2627     msi_control *control;
2628     LPCWSTR name, action, condition;
2629     UINT r;
2630
2631     name = MSI_RecordGetString( rec, 2 );
2632     action = MSI_RecordGetString( rec, 3 );
2633     condition = MSI_RecordGetString( rec, 4 );
2634     r = MSI_EvaluateConditionW( dialog->package, condition );
2635     control = msi_dialog_find_control( dialog, name );
2636     if( r == MSICONDITION_TRUE && control )
2637     {
2638         TRACE("%s control %s\n", debugstr_w(action), debugstr_w(name));
2639
2640         /* FIXME: case sensitive? */
2641         if(!lstrcmpW(action, szHide))
2642             ShowWindow(control->hwnd, SW_HIDE);
2643         else if(!strcmpW(action, szShow))
2644             ShowWindow(control->hwnd, SW_SHOW);
2645         else if(!strcmpW(action, szDisable))
2646             EnableWindow(control->hwnd, FALSE);
2647         else if(!strcmpW(action, szEnable))
2648             EnableWindow(control->hwnd, TRUE);
2649         else if(!strcmpW(action, szDefault))
2650             SetFocus(control->hwnd);
2651         else
2652             FIXME("Unhandled action %s\n", debugstr_w(action));
2653     }
2654
2655     return ERROR_SUCCESS;
2656 }
2657
2658 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog )
2659 {
2660     static const WCHAR query[] = {
2661       'S','E','L','E','C','T',' ','*',' ',
2662       'F','R','O','M',' ',
2663         'C','o','n','t','r','o','l','C','o','n','d','i','t','i','o','n',' ',
2664       'W','H','E','R','E',' ',
2665         '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0
2666     };
2667     UINT r;
2668     MSIQUERY *view = NULL;
2669     MSIPACKAGE *package = dialog->package;
2670
2671     TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
2672
2673     /* query the Control table for all the elements of the control */
2674     r = MSI_OpenQuery( package->db, &view, query, dialog->name );
2675     if( r != ERROR_SUCCESS )
2676         return ERROR_SUCCESS;
2677
2678     r = MSI_IterateRecords( view, 0, msi_dialog_set_control_condition, dialog );
2679     msiobj_release( &view->hdr );
2680
2681     return r;
2682 }
2683
2684 UINT msi_dialog_reset( msi_dialog *dialog )
2685 {
2686     /* FIXME: should restore the original values of any properties we changed */
2687     return msi_dialog_evaluate_control_conditions( dialog );
2688 }
2689
2690 /* figure out the height of 10 point MS Sans Serif */
2691 static INT msi_dialog_get_sans_serif_height( HWND hwnd )
2692 {
2693     static const WCHAR szSansSerif[] = {
2694         'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0 };
2695     LOGFONTW lf;
2696     TEXTMETRICW tm;
2697     BOOL r;
2698     LONG height = 0;
2699     HFONT hFont, hOldFont;
2700     HDC hdc;
2701
2702     hdc = GetDC( hwnd );
2703     if (hdc)
2704     {
2705         memset( &lf, 0, sizeof lf );
2706         lf.lfHeight = MulDiv(10, GetDeviceCaps(hdc, LOGPIXELSY), 72);
2707         strcpyW( lf.lfFaceName, szSansSerif );
2708         hFont = CreateFontIndirectW(&lf);
2709         if (hFont)
2710         {
2711             hOldFont = SelectObject( hdc, hFont );
2712             r = GetTextMetricsW( hdc, &tm );
2713             if (r)
2714                 height = tm.tmHeight;
2715             SelectObject( hdc, hOldFont );
2716             DeleteObject( hFont );
2717         }
2718         ReleaseDC( hwnd, hdc );
2719     }
2720     return height;
2721 }
2722
2723 /* fetch the associated record from the Dialog table */
2724 static MSIRECORD *msi_get_dialog_record( msi_dialog *dialog )
2725 {
2726     static const WCHAR query[] = {
2727         'S','E','L','E','C','T',' ','*',' ',
2728         'F','R','O','M',' ','D','i','a','l','o','g',' ',
2729         'W','H','E','R','E',' ',
2730            '`','D','i','a','l','o','g','`',' ','=',' ','\'','%','s','\'',0};
2731     MSIPACKAGE *package = dialog->package;
2732     MSIRECORD *rec = NULL;
2733
2734     TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
2735
2736     rec = MSI_QueryGetRecord( package->db, query, dialog->name );
2737     if( !rec )
2738         ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
2739
2740     return rec;
2741 }
2742
2743 static void msi_dialog_adjust_dialog_pos( msi_dialog *dialog, MSIRECORD *rec, LPRECT pos )
2744 {
2745     static const WCHAR szScreenX[] = {'S','c','r','e','e','n','X',0};
2746     static const WCHAR szScreenY[] = {'S','c','r','e','e','n','Y',0};
2747
2748     UINT xres, yres;
2749     POINT center;
2750     SIZE sz;
2751     LONG style;
2752
2753     center.x = MSI_RecordGetInteger( rec, 2 );
2754     center.y = MSI_RecordGetInteger( rec, 3 );
2755
2756     sz.cx = MSI_RecordGetInteger( rec, 4 );
2757     sz.cy = MSI_RecordGetInteger( rec, 5 );
2758
2759     sz.cx = msi_dialog_scale_unit( dialog, sz.cx );
2760     sz.cy = msi_dialog_scale_unit( dialog, sz.cy );
2761
2762     xres = msi_get_property_int( dialog->package, szScreenX, 0 );
2763     yres = msi_get_property_int( dialog->package, szScreenY, 0 );
2764
2765     center.x = MulDiv( center.x, xres, 100 );
2766     center.y = MulDiv( center.y, yres, 100 );
2767
2768     /* turn the client pos into the window rectangle */
2769     if (dialog->package->center_x && dialog->package->center_y)
2770     {
2771         pos->left = dialog->package->center_x - sz.cx / 2.0;
2772         pos->right = pos->left + sz.cx;
2773         pos->top = dialog->package->center_y - sz.cy / 2.0;
2774         pos->bottom = pos->top + sz.cy;
2775     }
2776     else
2777     {
2778         pos->left = center.x - sz.cx/2;
2779         pos->right = pos->left + sz.cx;
2780         pos->top = center.y - sz.cy/2;
2781         pos->bottom = pos->top + sz.cy;
2782
2783         /* save the center */
2784         dialog->package->center_x = center.x;
2785         dialog->package->center_y = center.y;
2786     }
2787
2788     dialog->size.cx = sz.cx;
2789     dialog->size.cy = sz.cy;
2790
2791     TRACE("%u %u %u %u\n", pos->left, pos->top, pos->right, pos->bottom);
2792
2793     style = GetWindowLongPtrW( dialog->hwnd, GWL_STYLE );
2794     AdjustWindowRect( pos, style, FALSE );
2795 }
2796
2797 static BOOL msi_control_set_next( msi_control *control, msi_control *next )
2798 {
2799     return SetWindowPos( next->hwnd, control->hwnd, 0, 0, 0, 0,
2800                          SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOREDRAW |
2801                          SWP_NOREPOSITION | SWP_NOSENDCHANGING | SWP_NOSIZE );
2802 }
2803
2804 static UINT msi_dialog_set_tab_order( msi_dialog *dialog )
2805 {
2806     msi_control *control, *tab_next;
2807
2808     LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
2809     {
2810         tab_next = msi_dialog_find_control( dialog, control->tabnext );
2811         if( !tab_next )
2812             continue;
2813         msi_control_set_next( control, tab_next );
2814     }
2815
2816     return ERROR_SUCCESS;
2817 }
2818
2819 static void msi_dialog_set_first_control( msi_dialog* dialog, LPCWSTR name )
2820 {
2821     msi_control *control;
2822
2823     control = msi_dialog_find_control( dialog, name );
2824     if( control )
2825         dialog->hWndFocus = control->hwnd;
2826     else
2827         dialog->hWndFocus = NULL;
2828 }
2829
2830 static LRESULT msi_dialog_oncreate( HWND hwnd, LPCREATESTRUCTW cs )
2831 {
2832     static const WCHAR df[] = {
2833         'D','e','f','a','u','l','t','U','I','F','o','n','t',0 };
2834     static const WCHAR dfv[] = {
2835         'M','S',' ','S','h','e','l','l',' ','D','l','g',0 };
2836     msi_dialog *dialog = (msi_dialog*) cs->lpCreateParams;
2837     MSIRECORD *rec = NULL;
2838     LPWSTR title = NULL;
2839     RECT pos;
2840
2841     TRACE("%p %p\n", dialog, dialog->package);
2842
2843     dialog->hwnd = hwnd;
2844     SetWindowLongPtrW( hwnd, GWLP_USERDATA, (LONG_PTR) dialog );
2845
2846     rec = msi_get_dialog_record( dialog );
2847     if( !rec )
2848     {
2849         TRACE("No record found for dialog %s\n", debugstr_w(dialog->name));
2850         return -1;
2851     }
2852
2853     dialog->scale = msi_dialog_get_sans_serif_height(dialog->hwnd);
2854
2855     msi_dialog_adjust_dialog_pos( dialog, rec, &pos );
2856
2857     dialog->attributes = MSI_RecordGetInteger( rec, 6 );
2858
2859     dialog->default_font = msi_dup_property( dialog->package, df );
2860     if (!dialog->default_font)
2861     {
2862         dialog->default_font = strdupW(dfv);
2863         if (!dialog->default_font) return -1;
2864     }
2865
2866     title = msi_get_deformatted_field( dialog->package, rec, 7 );
2867     SetWindowTextW( hwnd, title );
2868     msi_free( title );
2869
2870     SetWindowPos( hwnd, 0, pos.left, pos.top,
2871                   pos.right - pos.left, pos.bottom - pos.top,
2872                   SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW );
2873
2874     msi_dialog_build_font_list( dialog );
2875     msi_dialog_fill_controls( dialog );
2876     msi_dialog_evaluate_control_conditions( dialog );
2877     msi_dialog_set_tab_order( dialog );
2878     msi_dialog_set_first_control( dialog, MSI_RecordGetString( rec, 8 ) );
2879     msiobj_release( &rec->hdr );
2880
2881     return 0;
2882 }
2883
2884 static UINT msi_dialog_send_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
2885 {
2886     LPWSTR event_fmt = NULL, arg_fmt = NULL;
2887
2888     TRACE("Sending control event %s %s\n", debugstr_w(event), debugstr_w(arg));
2889
2890     deformat_string( dialog->package, event, &event_fmt );
2891     deformat_string( dialog->package, arg, &arg_fmt );
2892
2893     dialog->event_handler( dialog->package, event_fmt, arg_fmt, dialog );
2894
2895     msi_free( event_fmt );
2896     msi_free( arg_fmt );
2897
2898     return ERROR_SUCCESS;
2899 }
2900
2901 static UINT msi_dialog_set_property( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
2902 {
2903     static const WCHAR szNullArg[] = { '{','}',0 };
2904     LPWSTR p, prop, arg_fmt = NULL;
2905     UINT len;
2906
2907     len = strlenW(event);
2908     prop = msi_alloc( len*sizeof(WCHAR));
2909     strcpyW( prop, &event[1] );
2910     p = strchrW( prop, ']' );
2911     if( p && p[1] == 0 )
2912     {
2913         *p = 0;
2914         if( strcmpW( szNullArg, arg ) )
2915             deformat_string( dialog->package, arg, &arg_fmt );
2916         MSI_SetPropertyW( dialog->package, prop, arg_fmt );
2917         msi_free( arg_fmt );
2918     }
2919     else
2920         ERR("Badly formatted property string - what happens?\n");
2921     msi_free( prop );
2922     return ERROR_SUCCESS;
2923 }
2924
2925 static UINT msi_dialog_control_event( MSIRECORD *rec, LPVOID param )
2926 {
2927     msi_dialog *dialog = param;
2928     LPCWSTR condition, event, arg;
2929     UINT r;
2930
2931     condition = MSI_RecordGetString( rec, 5 );
2932     r = MSI_EvaluateConditionW( dialog->package, condition );
2933     if( r == MSICONDITION_TRUE || r == MSICONDITION_NONE )
2934     {
2935         event = MSI_RecordGetString( rec, 3 );
2936         arg = MSI_RecordGetString( rec, 4 );
2937         if( event[0] == '[' )
2938             msi_dialog_set_property( dialog, event, arg );
2939         else
2940             msi_dialog_send_event( dialog, event, arg );
2941     }
2942
2943     return ERROR_SUCCESS;
2944 }
2945
2946 struct rec_list
2947 {
2948     struct list entry;
2949     MSIRECORD *rec;
2950 };
2951
2952 static UINT add_rec_to_list( MSIRECORD *rec, LPVOID param )
2953 {
2954     struct rec_list *add_rec;
2955     struct list *records = (struct list *)param;
2956
2957     msiobj_addref( &rec->hdr );
2958
2959     add_rec = msi_alloc( sizeof( *add_rec ) );
2960     if (!add_rec)
2961     {
2962         msiobj_release( &rec->hdr );
2963         return ERROR_OUTOFMEMORY;
2964     }
2965
2966     add_rec->rec = rec;
2967     list_add_tail( records, &add_rec->entry );
2968     return ERROR_SUCCESS;
2969 }
2970
2971 static inline void remove_rec_from_list( struct rec_list *rec_entry )
2972 {
2973     msiobj_release( &rec_entry->rec->hdr );
2974     list_remove( &rec_entry->entry );
2975     msi_free( rec_entry );
2976 }
2977
2978 static UINT msi_dialog_button_handler( msi_dialog *dialog,
2979                                        msi_control *control, WPARAM param )
2980 {
2981     static const WCHAR query[] = {
2982       'S','E','L','E','C','T',' ','*',' ',
2983       'F','R','O','M',' ','C','o','n','t','r','o','l','E','v','e','n','t',' ',
2984       'W','H','E','R','E',' ',
2985          '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
2986       'A','N','D',' ',
2987          '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',' ',
2988       'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','i','n','g','`',0
2989     };
2990     MSIQUERY *view = NULL;
2991     struct rec_list *rec_entry, *next;
2992     struct list events;
2993     UINT r;
2994
2995     if( HIWORD(param) != BN_CLICKED )
2996         return ERROR_SUCCESS;
2997
2998     list_init( &events );
2999
3000     r = MSI_OpenQuery( dialog->package->db, &view, query,
3001                        dialog->name, control->name );
3002     if( r != ERROR_SUCCESS )
3003     {
3004         ERR("query failed\n");
3005         return 0;
3006     }
3007
3008     r = MSI_IterateRecords( view, 0, add_rec_to_list, &events );
3009     msiobj_release( &view->hdr );
3010     if (r != ERROR_SUCCESS)
3011         goto done;
3012
3013     /* handle all SetProperty events first */
3014     LIST_FOR_EACH_ENTRY_SAFE( rec_entry, next, &events, struct rec_list, entry )
3015     {
3016         LPCWSTR event = MSI_RecordGetString( rec_entry->rec, 3 );
3017
3018         if ( event[0] != '[' )
3019             continue;
3020
3021         r = msi_dialog_control_event( rec_entry->rec, dialog );
3022         remove_rec_from_list( rec_entry );
3023
3024         if ( r != ERROR_SUCCESS )
3025             goto done;
3026     }    
3027
3028     /* handle all other events */
3029     LIST_FOR_EACH_ENTRY_SAFE( rec_entry, next, &events, struct rec_list, entry )
3030     {
3031         r = msi_dialog_control_event( rec_entry->rec, dialog );
3032         remove_rec_from_list( rec_entry );
3033
3034         if ( r != ERROR_SUCCESS )
3035             goto done;
3036     }
3037
3038 done:
3039     LIST_FOR_EACH_ENTRY_SAFE( rec_entry, next, &events, struct rec_list, entry )
3040     {
3041         remove_rec_from_list( rec_entry );
3042     }
3043
3044     return r;
3045 }
3046
3047 static UINT msi_dialog_get_checkbox_state( msi_dialog *dialog,
3048                 msi_control *control )
3049 {
3050     WCHAR state[2] = { 0 };
3051     DWORD sz = 2;
3052
3053     MSI_GetPropertyW( dialog->package, control->property, state, &sz );
3054     return state[0] ? 1 : 0;
3055 }
3056
3057 static void msi_dialog_set_checkbox_state( msi_dialog *dialog,
3058                 msi_control *control, UINT state )
3059 {
3060     static const WCHAR szState[] = { '1', 0 };
3061     LPCWSTR val;
3062
3063     /* if uncheck then the property is set to NULL */
3064     if (!state)
3065     {
3066         MSI_SetPropertyW( dialog->package, control->property, NULL );
3067         return;
3068     }
3069
3070     /* check for a custom state */
3071     if (control->value && control->value[0])
3072         val = control->value;
3073     else
3074         val = szState;
3075
3076     MSI_SetPropertyW( dialog->package, control->property, val );
3077 }
3078
3079 static void msi_dialog_checkbox_sync_state( msi_dialog *dialog,
3080                 msi_control *control )
3081 {
3082     UINT state;
3083
3084     state = msi_dialog_get_checkbox_state( dialog, control );
3085     SendMessageW( control->hwnd, BM_SETCHECK,
3086                   state ? BST_CHECKED : BST_UNCHECKED, 0 );
3087 }
3088
3089 static UINT msi_dialog_checkbox_handler( msi_dialog *dialog,
3090                 msi_control *control, WPARAM param )
3091 {
3092     UINT state;
3093
3094     if( HIWORD(param) != BN_CLICKED )
3095         return ERROR_SUCCESS;
3096
3097     TRACE("clicked checkbox %s, set %s\n", debugstr_w(control->name),
3098           debugstr_w(control->property));
3099
3100     state = msi_dialog_get_checkbox_state( dialog, control );
3101     state = state ? 0 : 1;
3102     msi_dialog_set_checkbox_state( dialog, control, state );
3103     msi_dialog_checkbox_sync_state( dialog, control );
3104
3105     return msi_dialog_button_handler( dialog, control, param );
3106 }
3107
3108 static UINT msi_dialog_edit_handler( msi_dialog *dialog,
3109                 msi_control *control, WPARAM param )
3110 {
3111     LPWSTR buf;
3112
3113     if( HIWORD(param) != EN_CHANGE )
3114         return ERROR_SUCCESS;
3115
3116     TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
3117           debugstr_w(control->property));
3118
3119     buf = msi_get_window_text( control->hwnd );
3120     MSI_SetPropertyW( dialog->package, control->property, buf );
3121
3122     msi_free( buf );
3123
3124     return ERROR_SUCCESS;
3125 }
3126
3127 static UINT msi_dialog_radiogroup_handler( msi_dialog *dialog,
3128                 msi_control *control, WPARAM param )
3129 {
3130     if( HIWORD(param) != BN_CLICKED )
3131         return ERROR_SUCCESS;
3132
3133     TRACE("clicked radio button %s, set %s\n", debugstr_w(control->name),
3134           debugstr_w(control->property));
3135
3136     MSI_SetPropertyW( dialog->package, control->property, control->name );
3137
3138     return msi_dialog_button_handler( dialog, control, param );
3139 }
3140
3141 static LRESULT msi_dialog_oncommand( msi_dialog *dialog, WPARAM param, HWND hwnd )
3142 {
3143     msi_control *control = NULL;
3144
3145     TRACE("%p %p %08x\n", dialog, hwnd, param);
3146
3147     switch (param)
3148     {
3149     case 1: /* enter */
3150         control = msi_dialog_find_control( dialog, dialog->control_default );
3151         break;
3152     case 2: /* escape */
3153         control = msi_dialog_find_control( dialog, dialog->control_cancel );
3154         break;
3155     default: 
3156         control = msi_dialog_find_control_by_hwnd( dialog, hwnd );
3157     }
3158
3159     if( control )
3160     {
3161         if( control->handler )
3162         {
3163             control->handler( dialog, control, param );
3164             msi_dialog_evaluate_control_conditions( dialog );
3165         }
3166     }
3167     else
3168         ERR("button click from nowhere %p %d %p\n", dialog, param, hwnd);
3169     return 0;
3170 }
3171
3172 static LRESULT msi_dialog_onnotify( msi_dialog *dialog, LPARAM param )
3173 {
3174     LPNMHDR nmhdr = (LPNMHDR) param;
3175     msi_control *control = msi_dialog_find_control_by_hwnd( dialog, nmhdr->hwndFrom );
3176
3177     TRACE("%p %p\n", dialog, nmhdr->hwndFrom);
3178
3179     if ( control && control->handler )
3180         control->handler( dialog, control, param );
3181
3182     return 0;
3183 }
3184
3185 static void msi_dialog_setfocus( msi_dialog *dialog )
3186 {
3187     HWND hwnd = dialog->hWndFocus;
3188
3189     hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, TRUE);
3190     hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, FALSE);
3191     SetFocus( hwnd );
3192     dialog->hWndFocus = hwnd;
3193 }
3194
3195 static LRESULT WINAPI MSIDialog_WndProc( HWND hwnd, UINT msg,
3196                 WPARAM wParam, LPARAM lParam )
3197 {
3198     msi_dialog *dialog = (LPVOID) GetWindowLongPtrW( hwnd, GWLP_USERDATA );
3199
3200     TRACE("0x%04x\n", msg);
3201
3202     switch (msg)
3203     {
3204     case WM_MOVE:
3205         dialog->package->center_x = LOWORD(lParam) + dialog->size.cx / 2.0;
3206         dialog->package->center_y = HIWORD(lParam) + dialog->size.cy / 2.0;
3207         break;
3208
3209     case WM_CREATE:
3210         return msi_dialog_oncreate( hwnd, (LPCREATESTRUCTW)lParam );
3211
3212     case WM_COMMAND:
3213         return msi_dialog_oncommand( dialog, wParam, (HWND)lParam );
3214
3215     case WM_ACTIVATE:
3216         if( LOWORD(wParam) == WA_INACTIVE )
3217             dialog->hWndFocus = GetFocus();
3218         else
3219             msi_dialog_setfocus( dialog );
3220         return 0;
3221
3222     case WM_SETFOCUS:
3223         msi_dialog_setfocus( dialog );
3224         return 0;
3225
3226     /* bounce back to our subclassed static control */
3227     case WM_CTLCOLORSTATIC:
3228         return SendMessageW( (HWND) lParam, WM_CTLCOLORSTATIC, wParam, lParam );
3229
3230     case WM_DESTROY:
3231         dialog->hwnd = NULL;
3232         return 0;
3233     case WM_NOTIFY:
3234         return msi_dialog_onnotify( dialog, lParam );
3235     }
3236     return DefWindowProcW(hwnd, msg, wParam, lParam);
3237 }
3238
3239 static BOOL CALLBACK msi_radioground_child_enum( HWND hWnd, LPARAM lParam )
3240 {
3241     EnableWindow( hWnd, lParam );
3242     return TRUE;
3243 }
3244
3245 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
3246 {
3247     WNDPROC oldproc = (WNDPROC) GetPropW(hWnd, szButtonData);
3248     LRESULT r;
3249
3250     TRACE("hWnd %p msg %04x wParam 0x%08x lParam 0x%08lx\n", hWnd, msg, wParam, lParam);
3251
3252     if (msg == WM_COMMAND) /* Forward notifications to dialog */
3253         SendMessageW(GetParent(hWnd), msg, wParam, lParam);
3254
3255     r = CallWindowProcW(oldproc, hWnd, msg, wParam, lParam);
3256
3257     /* make sure the radio buttons show as disabled if the parent is disabled */
3258     if (msg == WM_ENABLE)
3259         EnumChildWindows( hWnd, msi_radioground_child_enum, wParam );
3260
3261     return r;
3262 }
3263
3264 static LRESULT WINAPI MSIHiddenWindowProc( HWND hwnd, UINT msg,
3265                 WPARAM wParam, LPARAM lParam )
3266 {
3267     msi_dialog *dialog = (msi_dialog*) lParam;
3268
3269     TRACE("%d %p\n", msg, dialog);
3270
3271     switch (msg)
3272     {
3273     case WM_MSI_DIALOG_CREATE:
3274         return msi_dialog_run_message_loop( dialog );
3275     case WM_MSI_DIALOG_DESTROY:
3276         msi_dialog_destroy( dialog );
3277         return 0;
3278     }
3279     return DefWindowProcW( hwnd, msg, wParam, lParam );
3280 }
3281
3282 /* functions that interface to other modules within MSI */
3283
3284 msi_dialog *msi_dialog_create( MSIPACKAGE* package,
3285                                LPCWSTR szDialogName, msi_dialog *parent,
3286                                msi_dialog_event_handler event_handler )
3287 {
3288     MSIRECORD *rec = NULL;
3289     msi_dialog *dialog;
3290
3291     TRACE("%p %s\n", package, debugstr_w(szDialogName));
3292
3293     /* allocate the structure for the dialog to use */
3294     dialog = msi_alloc_zero( sizeof *dialog + sizeof(WCHAR)*strlenW(szDialogName) );
3295     if( !dialog )
3296         return NULL;
3297     strcpyW( dialog->name, szDialogName );
3298     dialog->parent = parent;
3299     msiobj_addref( &package->hdr );
3300     dialog->package = package;
3301     dialog->event_handler = event_handler;
3302     dialog->finished = 0;
3303     list_init( &dialog->controls );
3304
3305     /* verify that the dialog exists */
3306     rec = msi_get_dialog_record( dialog );
3307     if( !rec )
3308     {
3309         msiobj_release( &package->hdr );
3310         msi_free( dialog );
3311         return NULL;
3312     }
3313     dialog->attributes = MSI_RecordGetInteger( rec, 6 );
3314     dialog->control_default = strdupW( MSI_RecordGetString( rec, 9 ) );
3315     dialog->control_cancel = strdupW( MSI_RecordGetString( rec, 10 ) );
3316     msiobj_release( &rec->hdr );
3317
3318     return dialog;
3319 }
3320
3321 static void msi_process_pending_messages( HWND hdlg )
3322 {
3323     MSG msg;
3324
3325     while( PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ) )
3326     {
3327         if( hdlg && IsDialogMessageW( hdlg, &msg ))
3328             continue;
3329         TranslateMessage( &msg );
3330         DispatchMessageW( &msg );
3331     }
3332 }
3333
3334 void msi_dialog_end_dialog( msi_dialog *dialog )
3335 {
3336     TRACE("%p\n", dialog);
3337     dialog->finished = 1;
3338     PostMessageW(dialog->hwnd, WM_NULL, 0, 0);
3339 }
3340
3341 void msi_dialog_check_messages( HANDLE handle )
3342 {
3343     DWORD r;
3344
3345     /* in threads other than the UI thread, block */
3346     if( uiThreadId != GetCurrentThreadId() )
3347     {
3348         if( handle )
3349             WaitForSingleObject( handle, INFINITE );
3350         return;
3351     }
3352
3353     /* there's two choices for the UI thread */
3354     while (1)
3355     {
3356         msi_process_pending_messages( NULL );
3357
3358         if( !handle )
3359             break;
3360
3361         /*
3362          * block here until somebody creates a new dialog or
3363          * the handle we're waiting on becomes ready
3364          */
3365         r = MsgWaitForMultipleObjects( 1, &handle, 0, INFINITE, QS_ALLINPUT );
3366         if( r == WAIT_OBJECT_0 )
3367             break;
3368     }
3369 }
3370
3371 UINT msi_dialog_run_message_loop( msi_dialog *dialog )
3372 {
3373     DWORD style;
3374     HWND hwnd;
3375
3376     if( uiThreadId != GetCurrentThreadId() )
3377         return SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_CREATE, 0, (LPARAM) dialog );
3378
3379     /* create the dialog window, don't show it yet */
3380     style = WS_OVERLAPPED;
3381     if( dialog->attributes & msidbDialogAttributesVisible )
3382         style |= WS_VISIBLE;
3383
3384     hwnd = CreateWindowW( szMsiDialogClass, dialog->name, style,
3385                      CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
3386                      NULL, NULL, NULL, dialog );
3387     if( !hwnd )
3388     {
3389         ERR("Failed to create dialog %s\n", debugstr_w( dialog->name ));
3390         return ERROR_FUNCTION_FAILED;
3391     }
3392
3393     ShowWindow( hwnd, SW_SHOW );
3394     /* UpdateWindow( hwnd ); - and causes the transparent static controls not to paint */
3395
3396     if( dialog->attributes & msidbDialogAttributesModal )
3397     {
3398         while( !dialog->finished )
3399         {
3400             MsgWaitForMultipleObjects( 0, NULL, 0, INFINITE, QS_ALLINPUT );
3401             msi_process_pending_messages( dialog->hwnd );
3402         }
3403     }
3404     else
3405         return ERROR_IO_PENDING;
3406
3407     return ERROR_SUCCESS;
3408 }
3409
3410 void msi_dialog_do_preview( msi_dialog *dialog )
3411 {
3412     TRACE("\n");
3413     dialog->attributes |= msidbDialogAttributesVisible;
3414     dialog->attributes &= ~msidbDialogAttributesModal;
3415     msi_dialog_run_message_loop( dialog );
3416 }
3417
3418 void msi_dialog_destroy( msi_dialog *dialog )
3419 {
3420     if( uiThreadId != GetCurrentThreadId() )
3421     {
3422         SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_DESTROY, 0, (LPARAM) dialog );
3423         return;
3424     }
3425
3426     if( dialog->hwnd )
3427         ShowWindow( dialog->hwnd, SW_HIDE );
3428
3429     if( dialog->hwnd )
3430         DestroyWindow( dialog->hwnd );
3431
3432     /* unsubscribe events */
3433     ControlEvent_CleanupDialogSubscriptions(dialog->package, dialog->name);
3434
3435     /* destroy the list of controls */
3436     while( !list_empty( &dialog->controls ) )
3437     {
3438         msi_control *t;
3439
3440         t = LIST_ENTRY( list_head( &dialog->controls ),
3441                         msi_control, entry );
3442         msi_destroy_control( t );
3443     }
3444
3445     /* destroy the list of fonts */
3446     while( dialog->font_list )
3447     {
3448         msi_font *t = dialog->font_list;
3449         dialog->font_list = t->next;
3450         DeleteObject( t->hfont );
3451         msi_free( t );
3452     }
3453     msi_free( dialog->default_font );
3454
3455     msi_free( dialog->control_default );
3456     msi_free( dialog->control_cancel );
3457     msiobj_release( &dialog->package->hdr );
3458     dialog->package = NULL;
3459     msi_free( dialog );
3460 }
3461
3462 BOOL msi_dialog_register_class( void )
3463 {
3464     WNDCLASSW cls;
3465
3466     ZeroMemory( &cls, sizeof cls );
3467     cls.lpfnWndProc   = MSIDialog_WndProc;
3468     cls.hInstance     = NULL;
3469     cls.hIcon         = LoadIconW(0, (LPWSTR)IDI_APPLICATION);
3470     cls.hCursor       = LoadCursorW(0, (LPWSTR)IDC_ARROW);
3471     cls.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
3472     cls.lpszMenuName  = NULL;
3473     cls.lpszClassName = szMsiDialogClass;
3474
3475     if( !RegisterClassW( &cls ) )
3476         return FALSE;
3477
3478     cls.lpfnWndProc   = MSIHiddenWindowProc;
3479     cls.lpszClassName = szMsiHiddenWindow;
3480
3481     if( !RegisterClassW( &cls ) )
3482         return FALSE;
3483
3484     uiThreadId = GetCurrentThreadId();
3485
3486     hMsiHiddenWindow = CreateWindowW( szMsiHiddenWindow, NULL, WS_OVERLAPPED,
3487                                    0, 0, 100, 100, NULL, NULL, NULL, NULL );
3488     if( !hMsiHiddenWindow )
3489         return FALSE;
3490
3491     return TRUE;
3492 }
3493
3494 void msi_dialog_unregister_class( void )
3495 {
3496     DestroyWindow( hMsiHiddenWindow );
3497     hMsiHiddenWindow = NULL;
3498     UnregisterClassW( szMsiDialogClass, NULL );
3499     UnregisterClassW( szMsiHiddenWindow, NULL );
3500     uiThreadId = 0;
3501 }
3502
3503 static UINT error_dialog_handler(MSIPACKAGE *package, LPCWSTR event,
3504                                  LPCWSTR argument, msi_dialog* dialog)
3505 {
3506     static const WCHAR end_dialog[] = {'E','n','d','D','i','a','l','o','g',0};
3507     static const WCHAR error_abort[] = {'E','r','r','o','r','A','b','o','r','t',0};
3508     static const WCHAR error_cancel[] = {'E','r','r','o','r','C','a','n','c','e','l',0};
3509     static const WCHAR error_no[] = {'E','r','r','o','r','N','o',0};
3510     static const WCHAR result_prop[] = {
3511         'M','S','I','E','r','r','o','r','D','i','a','l','o','g','R','e','s','u','l','t',0
3512     };
3513
3514     if ( lstrcmpW( event, end_dialog ) )
3515         return ERROR_SUCCESS;
3516
3517     if ( !lstrcmpW( argument, error_abort ) || !lstrcmpW( argument, error_cancel ) ||
3518          !lstrcmpW( argument, error_no ) )
3519     {
3520          MSI_SetPropertyW( package, result_prop, error_abort );
3521     }
3522
3523     ControlEvent_CleanupSubscriptions(package);
3524     msi_dialog_end_dialog( dialog );
3525
3526     return ERROR_SUCCESS;
3527 }
3528
3529 static UINT msi_error_dialog_set_error( MSIPACKAGE *package, LPWSTR error_dialog, LPWSTR error )
3530 {
3531     MSIRECORD * row;
3532
3533     static const WCHAR update[] = 
3534         {'U','P','D','A','T','E',' ','`','C','o','n','t','r','o','l','`',' ',
3535          'S','E','T',' ','`','T','e','x','t','`',' ','=',' ','\'','%','s','\'',' ',
3536          'W','H','E','R','E', ' ','`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
3537          'A','N','D',' ','`','C','o','n','t','r','o','l','`',' ','=',' ',
3538          '\'','E','r','r','o','r','T','e','x','t','\'',0};
3539
3540     row = MSI_QueryGetRecord( package->db, update, error, error_dialog );
3541     if (!row)
3542         return ERROR_FUNCTION_FAILED;
3543
3544     msiobj_release(&row->hdr);
3545     return ERROR_SUCCESS;
3546 }
3547
3548 UINT msi_spawn_error_dialog( MSIPACKAGE *package, LPWSTR error_dialog, LPWSTR error )
3549 {
3550     msi_dialog *dialog;
3551     WCHAR result[MAX_PATH];
3552     UINT r = ERROR_SUCCESS;
3553     DWORD size = MAX_PATH;
3554     int res;
3555
3556     static const WCHAR szUILevel[] = {'U','I','L','e','v','e','l',0};
3557     static const WCHAR pn_prop[] = {'P','r','o','d','u','c','t','N','a','m','e',0};
3558     static const WCHAR title_fmt[] = {'%','s',' ','W','a','r','n','i','n','g',0};
3559     static const WCHAR error_abort[] = {'E','r','r','o','r','A','b','o','r','t',0};
3560     static const WCHAR result_prop[] = {
3561         'M','S','I','E','r','r','o','r','D','i','a','l','o','g','R','e','s','u','l','t',0
3562     };
3563
3564     if ( (msi_get_property_int(package, szUILevel, 0) & INSTALLUILEVEL_MASK) == INSTALLUILEVEL_NONE )
3565         return ERROR_SUCCESS;
3566
3567     if ( !error_dialog )
3568     {
3569         LPWSTR product_name = msi_dup_property( package, pn_prop );
3570         WCHAR title[MAX_PATH];
3571
3572         sprintfW( title, title_fmt, product_name );
3573         res = MessageBoxW( NULL, error, title, MB_OKCANCEL | MB_ICONWARNING );
3574
3575         msi_free( product_name );
3576
3577         if ( res == IDOK )
3578             return ERROR_SUCCESS;
3579         else
3580             return ERROR_FUNCTION_FAILED;
3581     }
3582
3583     r = msi_error_dialog_set_error( package, error_dialog, error );
3584     if ( r != ERROR_SUCCESS )
3585         return r;
3586
3587     dialog = msi_dialog_create( package, error_dialog, package->dialog,
3588                                 error_dialog_handler );
3589     if ( !dialog )
3590         return ERROR_FUNCTION_FAILED;
3591
3592     dialog->finished = FALSE;
3593     r = msi_dialog_run_message_loop( dialog );
3594     if ( r != ERROR_SUCCESS )
3595         goto done;
3596
3597     r = MSI_GetPropertyW( package, result_prop, result, &size );
3598     if ( r != ERROR_SUCCESS)
3599         r = ERROR_SUCCESS;
3600
3601     if ( !lstrcmpW( result, error_abort ) )
3602         r = ERROR_FUNCTION_FAILED;
3603
3604 done:
3605     msi_dialog_destroy( dialog );
3606
3607     return r;
3608 }