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