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