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