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