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