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