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