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