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