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