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