msi: Use the respective update functions when creating the Browse dialog control.
[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 #include "action.h"
46
47 WINE_DEFAULT_DEBUG_CHANNEL(msi);
48
49
50 extern HINSTANCE msi_hInstance;
51
52 struct msi_control_tag;
53 typedef struct msi_control_tag msi_control;
54 typedef UINT (*msi_handler)( msi_dialog *, msi_control *, WPARAM );
55
56 struct msi_control_tag
57 {
58     struct list entry;
59     HWND hwnd;
60     msi_handler handler;
61     LPWSTR property;
62     LPWSTR value;
63     HBITMAP hBitmap;
64     HICON hIcon;
65     LPWSTR tabnext;
66     HMODULE hDll;
67     float progress_current;
68     float progress_max;
69     DWORD attributes;
70     WCHAR name[1];
71 };
72
73 typedef struct msi_font_tag
74 {
75     struct msi_font_tag *next;
76     HFONT hfont;
77     COLORREF color;
78     WCHAR name[1];
79 } msi_font;
80
81 struct msi_dialog_tag
82 {
83     MSIPACKAGE *package;
84     msi_dialog_event_handler event_handler;
85     BOOL finished;
86     INT scale;
87     DWORD attributes;
88     HWND hwnd;
89     LPWSTR default_font;
90     msi_font *font_list;
91     struct list controls;
92     HWND hWndFocus;
93     LPWSTR control_default;
94     LPWSTR control_cancel;
95     WCHAR name[1];
96 };
97
98 typedef UINT (*msi_dialog_control_func)( msi_dialog *dialog, MSIRECORD *rec );
99 struct control_handler 
100 {
101     LPCWSTR control_type;
102     msi_dialog_control_func func;
103 };
104
105 typedef struct
106 {
107     msi_dialog* dialog;
108     msi_control *parent;
109     DWORD       attributes;
110     LPWSTR      propval;
111 } radio_button_group_descr;
112
113 static const WCHAR szMsiDialogClass[] = {
114     'M','s','i','D','i','a','l','o','g','C','l','o','s','e','C','l','a','s','s',0
115 };
116 static const WCHAR szMsiHiddenWindow[] = {
117     'M','s','i','H','i','d','d','e','n','W','i','n','d','o','w',0 };
118 static const WCHAR szStatic[] = { 'S','t','a','t','i','c',0 };
119 static const WCHAR szButton[] = { 'B','U','T','T','O','N', 0 };
120 static const WCHAR szButtonData[] = { 'M','S','I','D','A','T','A',0 };
121 static const WCHAR szProgress[] = { 'P','r','o','g','r','e','s','s',0 };
122 static const WCHAR szText[] = { 'T','e','x','t',0 };
123 static const WCHAR szPushButton[] = { 'P','u','s','h','B','u','t','t','o','n',0 };
124 static const WCHAR szLine[] = { 'L','i','n','e',0 };
125 static const WCHAR szBitmap[] = { 'B','i','t','m','a','p',0 };
126 static const WCHAR szCheckBox[] = { 'C','h','e','c','k','B','o','x',0 };
127 static const WCHAR szScrollableText[] = {
128     'S','c','r','o','l','l','a','b','l','e','T','e','x','t',0 };
129 static const WCHAR szComboBox[] = { 'C','o','m','b','o','B','o','x',0 };
130 static const WCHAR szEdit[] = { 'E','d','i','t',0 };
131 static const WCHAR szMaskedEdit[] = { 'M','a','s','k','e','d','E','d','i','t',0 };
132 static const WCHAR szPathEdit[] = { 'P','a','t','h','E','d','i','t',0 };
133 static const WCHAR szProgressBar[] = {
134      'P','r','o','g','r','e','s','s','B','a','r',0 };
135 static const WCHAR szRadioButtonGroup[] = { 
136     'R','a','d','i','o','B','u','t','t','o','n','G','r','o','u','p',0 };
137 static const WCHAR szIcon[] = { 'I','c','o','n',0 };
138 static const WCHAR szSelectionTree[] = {
139     'S','e','l','e','c','t','i','o','n','T','r','e','e',0 };
140 static const WCHAR szGroupBox[] = { 'G','r','o','u','p','B','o','x',0 };
141 static const WCHAR szListBox[] = { 'L','i','s','t','B','o','x',0 };
142 static const WCHAR szDirectoryCombo[] = { 'D','i','r','e','c','t','o','r','y','C','o','m','b','o',0 };
143 static const WCHAR szDirectoryList[] = { 'D','i','r','e','c','t','o','r','y','L','i','s','t',0 };
144 static const WCHAR szVolumeCostList[] = { 'V','o','l','u','m','e','C','o','s','t','L','i','s','t',0 };
145
146 static UINT msi_dialog_checkbox_handler( msi_dialog *, msi_control *, WPARAM );
147 static void msi_dialog_checkbox_sync_state( msi_dialog *, msi_control * );
148 static UINT msi_dialog_button_handler( msi_dialog *, msi_control *, WPARAM );
149 static UINT msi_dialog_edit_handler( msi_dialog *, msi_control *, WPARAM );
150 static UINT msi_dialog_radiogroup_handler( msi_dialog *, msi_control *, WPARAM param );
151 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog );
152 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
153
154
155 /* dialog sequencing */
156
157 #define WM_MSI_DIALOG_CREATE  (WM_USER+0x100)
158 #define WM_MSI_DIALOG_DESTROY (WM_USER+0x101)
159
160 static DWORD uiThreadId;
161 static HWND hMsiHiddenWindow;
162
163 static INT msi_dialog_scale_unit( msi_dialog *dialog, INT val )
164 {
165     return (dialog->scale * val + 5) / 10;
166 }
167
168 static msi_control *msi_dialog_find_control( msi_dialog *dialog, LPCWSTR name )
169 {
170     msi_control *control;
171
172     if( !name )
173         return NULL;
174     LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
175         if( !strcmpW( control->name, name ) ) /* FIXME: case sensitive? */
176             return control;
177     return NULL;
178 }
179
180 static msi_control *msi_dialog_find_control_by_hwnd( msi_dialog *dialog, HWND hwnd )
181 {
182     msi_control *control;
183
184     LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
185         if( hwnd == control->hwnd )
186             return control;
187     return NULL;
188 }
189
190 static LPWSTR msi_get_deformatted_field( MSIPACKAGE *package, MSIRECORD *rec, int field )
191 {
192     LPCWSTR str = MSI_RecordGetString( rec, field );
193     LPWSTR ret = NULL;
194
195     if (str)
196         deformat_string( package, str, &ret );
197     return ret;
198 }
199
200 static LPWSTR msi_dialog_dup_property( msi_dialog *dialog, LPCWSTR property, BOOL indirect )
201 {
202     if (!property)
203         return NULL;
204
205     if (indirect)
206         return msi_dup_property( dialog->package, property );
207
208     return strdupW( property );
209 }
210
211 /*
212  * msi_dialog_get_style
213  *
214  * Extract the {\style} string from the front of the text to display and
215  *  update the pointer.
216  */
217 static LPWSTR msi_dialog_get_style( LPCWSTR p, LPCWSTR *rest )
218 {
219     LPWSTR ret = NULL;
220     LPCWSTR q, i;
221     DWORD len;
222
223     *rest = p;
224     if( !p )
225         return ret;
226     if( *p++ != '{' )
227         return ret;
228     q = strchrW( p, '}' );
229     if( !q )
230         return ret;
231     if( *p == '\\' || *p == '&' )
232         p++;
233
234     /* little bit of sanity checking to stop us getting confused with RTF */
235     for( i=p; i<q; i++ )
236         if( *i == '}' || *i == '\\' )
237             return ret;
238     
239     *rest = ++q;
240     len = q - p;
241
242     ret = msi_alloc( len*sizeof(WCHAR) );
243     if( !ret )
244         return ret;
245     memcpy( ret, p, len*sizeof(WCHAR) );
246     ret[len-1] = 0;
247     return ret;
248 }
249
250 static UINT msi_dialog_add_font( MSIRECORD *rec, LPVOID param )
251 {
252     msi_dialog *dialog = param;
253     msi_font *font;
254     LPCWSTR face, name;
255     LOGFONTW lf;
256     INT style;
257     HDC hdc;
258
259     /* create a font and add it to the list */
260     name = MSI_RecordGetString( rec, 1 );
261     font = msi_alloc( sizeof *font + strlenW( name )*sizeof (WCHAR) );
262     strcpyW( font->name, name );
263     font->next = dialog->font_list;
264     dialog->font_list = font;
265
266     font->color = MSI_RecordGetInteger( rec, 4 );
267
268     memset( &lf, 0, sizeof lf );
269     face = MSI_RecordGetString( rec, 2 );
270     lf.lfHeight = MSI_RecordGetInteger( rec, 3 );
271     style = MSI_RecordGetInteger( rec, 5 );
272     if( style & msidbTextStyleStyleBitsBold )
273         lf.lfWeight = FW_BOLD;
274     if( style & msidbTextStyleStyleBitsItalic )
275         lf.lfItalic = TRUE;
276     if( style & msidbTextStyleStyleBitsUnderline )
277         lf.lfUnderline = TRUE;
278     if( style & msidbTextStyleStyleBitsStrike )
279         lf.lfStrikeOut = TRUE;
280     lstrcpynW( lf.lfFaceName, face, LF_FACESIZE );
281
282     /* adjust the height */
283     hdc = GetDC( dialog->hwnd );
284     if (hdc)
285     {
286         lf.lfHeight = -MulDiv(lf.lfHeight, GetDeviceCaps(hdc, LOGPIXELSY), 72);
287         ReleaseDC( dialog->hwnd, hdc );
288     }
289
290     font->hfont = CreateFontIndirectW( &lf );
291
292     TRACE("Adding font style %s\n", debugstr_w(font->name) );
293
294     return ERROR_SUCCESS;
295 }
296
297 static msi_font *msi_dialog_find_font( msi_dialog *dialog, LPCWSTR name )
298 {
299     msi_font *font;
300
301     for( font = dialog->font_list; font; font = font->next )
302         if( !strcmpW( font->name, name ) )  /* FIXME: case sensitive? */
303             break;
304
305     return font;
306 }
307
308 static UINT msi_dialog_set_font( msi_dialog *dialog, HWND hwnd, LPCWSTR name )
309 {
310     msi_font *font;
311
312     font = msi_dialog_find_font( dialog, name );
313     if( font )
314         SendMessageW( hwnd, WM_SETFONT, (WPARAM) font->hfont, TRUE );
315     else
316         ERR("No font entry for %s\n", debugstr_w(name));
317     return ERROR_SUCCESS;
318 }
319
320 static UINT msi_dialog_build_font_list( msi_dialog *dialog )
321 {
322     static const WCHAR query[] = {
323       'S','E','L','E','C','T',' ','*',' ',
324       'F','R','O','M',' ','`','T','e','x','t','S','t','y','l','e','`',' ',0
325     };
326     UINT r;
327     MSIQUERY *view = NULL;
328
329     TRACE("dialog %p\n", dialog );
330
331     r = MSI_OpenQuery( dialog->package->db, &view, query );
332     if( r != ERROR_SUCCESS )
333         return r;
334
335     r = MSI_IterateRecords( view, NULL, msi_dialog_add_font, dialog );
336     msiobj_release( &view->hdr );
337
338     return r;
339 }
340
341 static msi_control *msi_dialog_create_window( msi_dialog *dialog,
342                 MSIRECORD *rec, DWORD exstyle, LPCWSTR szCls, LPCWSTR name, LPCWSTR text,
343                 DWORD style, HWND parent )
344 {
345     DWORD x, y, width, height;
346     LPWSTR font = NULL, title_font = NULL;
347     LPCWSTR title = NULL;
348     msi_control *control;
349
350     style |= WS_CHILD;
351
352     control = msi_alloc( sizeof *control + strlenW(name)*sizeof(WCHAR) );
353     strcpyW( control->name, name );
354     list_add_head( &dialog->controls, &control->entry );
355     control->handler = NULL;
356     control->property = NULL;
357     control->value = NULL;
358     control->hBitmap = NULL;
359     control->hIcon = NULL;
360     control->hDll = NULL;
361     control->tabnext = strdupW( MSI_RecordGetString( rec, 11) );
362     control->progress_current = 0;
363     control->progress_max = 100;
364
365     x = MSI_RecordGetInteger( rec, 4 );
366     y = MSI_RecordGetInteger( rec, 5 );
367     width = MSI_RecordGetInteger( rec, 6 );
368     height = MSI_RecordGetInteger( rec, 7 );
369
370     x = msi_dialog_scale_unit( dialog, x );
371     y = msi_dialog_scale_unit( dialog, y );
372     width = msi_dialog_scale_unit( dialog, width );
373     height = msi_dialog_scale_unit( dialog, height );
374
375     if( text )
376     {
377         deformat_string( dialog->package, text, &title_font );
378         font = msi_dialog_get_style( title_font, &title );
379     }
380
381     control->hwnd = CreateWindowExW( exstyle, szCls, title, style,
382                           x, y, width, height, parent, NULL, NULL, NULL );
383
384     TRACE("Dialog %s control %s hwnd %p\n",
385            debugstr_w(dialog->name), debugstr_w(text), control->hwnd );
386
387     msi_dialog_set_font( dialog, control->hwnd,
388                          font ? font : dialog->default_font );
389
390     msi_free( title_font );
391     msi_free( font );
392
393     return control;
394 }
395
396 static MSIRECORD *msi_get_binary_record( MSIDATABASE *db, LPCWSTR name )
397 {
398     static const WCHAR query[] = {
399         's','e','l','e','c','t',' ','*',' ',
400         'f','r','o','m',' ','B','i','n','a','r','y',' ',
401         'w','h','e','r','e',' ',
402             '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0
403     };
404
405     return MSI_QueryGetRecord( db, query, name );
406 }
407
408 static LPWSTR msi_create_tmp_path(void)
409 {
410     WCHAR tmp[MAX_PATH];
411     LPWSTR path = NULL;
412     static const WCHAR prefix[] = { 'm','s','i',0 };
413     DWORD len, r;
414
415     r = GetTempPathW( MAX_PATH, tmp );
416     if( !r )
417         return path;
418     len = lstrlenW( tmp ) + 20;
419     path = msi_alloc( len * sizeof (WCHAR) );
420     if( path )
421     {
422         r = GetTempFileNameW( tmp, prefix, 0, path );
423         if (!r)
424         {
425             msi_free( path );
426             path = NULL;
427         }
428     }
429     return path;
430 }
431
432
433 static HANDLE msi_load_image( MSIDATABASE *db, LPCWSTR name, UINT type,
434                               UINT cx, UINT cy, UINT flags )
435 {
436     MSIRECORD *rec = NULL;
437     HANDLE himage = NULL;
438     LPWSTR tmp;
439     UINT r;
440
441     TRACE("%p %s %u %u %08x\n", db, debugstr_w(name), cx, cy, flags);
442
443     tmp = msi_create_tmp_path();
444     if( !tmp )
445         return himage;
446
447     rec = msi_get_binary_record( db, name );
448     if( rec )
449     {
450         r = MSI_RecordStreamToFile( rec, 2, tmp );
451         if( r == ERROR_SUCCESS )
452         {
453             himage = LoadImageW( 0, tmp, type, cx, cy, flags );
454             DeleteFileW( tmp );
455         }
456         msiobj_release( &rec->hdr );
457     }
458
459     msi_free( tmp );
460     return himage;
461 }
462
463 static HICON msi_load_icon( MSIDATABASE *db, LPCWSTR text, UINT attributes )
464 {
465     DWORD cx = 0, cy = 0, flags;
466
467     flags = LR_LOADFROMFILE | LR_DEFAULTSIZE;
468     if( attributes & msidbControlAttributesFixedSize )
469     {
470         flags &= ~LR_DEFAULTSIZE;
471         if( attributes & msidbControlAttributesIconSize16 )
472         {
473             cx += 16;
474             cy += 16;
475         }
476         if( attributes & msidbControlAttributesIconSize32 )
477         {
478             cx += 32;
479             cy += 32;
480         }
481         /* msidbControlAttributesIconSize48 handled by above logic */
482     }
483     return msi_load_image( db, text, IMAGE_ICON, cx, cy, flags );
484 }
485
486
487 /* called from the Control Event subscription code */
488 void msi_dialog_handle_event( msi_dialog* dialog, LPCWSTR control, 
489                               LPCWSTR attribute, MSIRECORD *rec )
490 {
491     msi_control* ctrl;
492     LPCWSTR font_text, text = NULL;
493     LPWSTR font;
494
495     ctrl = msi_dialog_find_control( dialog, control );
496     if (!ctrl)
497         return;
498     if( !lstrcmpW(attribute, szText) )
499     {
500         font_text = MSI_RecordGetString( rec , 1 );
501         font = msi_dialog_get_style( font_text, &text );
502         SetWindowTextW( ctrl->hwnd, text );
503         msi_free( font );
504         msi_dialog_check_messages( NULL );
505     }
506     else if( !lstrcmpW(attribute, szProgress) )
507     {
508         DWORD func, val;
509
510         func = MSI_RecordGetInteger( rec , 1 );
511         val = MSI_RecordGetInteger( rec , 2 );
512
513         switch (func)
514         {
515         case 0: /* init */
516             ctrl->progress_max = val;
517             ctrl->progress_current = 0;
518             SendMessageW(ctrl->hwnd, PBM_SETRANGE, 0, MAKELPARAM(0,100));
519             SendMessageW(ctrl->hwnd, PBM_SETPOS, 0, 0);
520             break;
521         case 1: /* FIXME: not sure what this is supposed to do */
522             break;
523         case 2: /* move */
524             ctrl->progress_current += val;
525             SendMessageW(ctrl->hwnd, PBM_SETPOS, 100*(ctrl->progress_current/ctrl->progress_max), 0);
526             break;
527         default:
528             ERR("Unknown progress message %ld\n", func);
529             break;
530         }
531     }
532     else
533     {
534         FIXME("Attribute %s not being set\n", debugstr_w(attribute));
535         return;
536     }
537 }
538
539 static void msi_dialog_map_events(msi_dialog* dialog, LPCWSTR control)
540 {
541     static const WCHAR Query[] = {
542         'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
543          '`','E','v','e','n','t','M','a','p','p','i','n','g','`',' ',
544         'W','H','E','R','E',' ',
545          '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
546         'A','N','D',' ',
547          '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',0
548     };
549     MSIRECORD *row;
550     LPCWSTR event, attribute;
551
552     row = MSI_QueryGetRecord( dialog->package->db, Query, dialog->name, control );
553     if (!row)
554         return;
555
556     event = MSI_RecordGetString( row, 3 );
557     attribute = MSI_RecordGetString( row, 4 );
558     ControlEvent_SubscribeToEvent( dialog->package, event, control, attribute );
559     msiobj_release( &row->hdr );
560 }
561
562 /* everything except radio buttons */
563 static msi_control *msi_dialog_add_control( msi_dialog *dialog,
564                 MSIRECORD *rec, LPCWSTR szCls, DWORD style )
565 {
566     DWORD attributes;
567     LPCWSTR text, name;
568     DWORD exstyle = 0;
569
570     name = MSI_RecordGetString( rec, 2 );
571     attributes = MSI_RecordGetInteger( rec, 8 );
572     text = MSI_RecordGetString( rec, 10 );
573     if( attributes & msidbControlAttributesVisible )
574         style |= WS_VISIBLE;
575     if( ~attributes & msidbControlAttributesEnabled )
576         style |= WS_DISABLED;
577     if( attributes & msidbControlAttributesSunken )
578         exstyle |= WS_EX_CLIENTEDGE;
579
580     msi_dialog_map_events(dialog, name);
581
582     return msi_dialog_create_window( dialog, rec, exstyle, szCls, name,
583                                      text, style, dialog->hwnd );
584 }
585
586 struct msi_text_info
587 {
588     msi_font *font;
589     WNDPROC oldproc;
590     DWORD attributes;
591 };
592
593 /*
594  * we don't erase our own background,
595  * so we have to make sure that the parent window redraws first
596  */
597 static void msi_text_on_settext( HWND hWnd )
598 {
599     HWND hParent;
600     RECT rc;
601
602     hParent = GetParent( hWnd );
603     GetWindowRect( hWnd, &rc );
604     MapWindowPoints( NULL, hParent, (LPPOINT) &rc, 2 );
605     InvalidateRect( hParent, &rc, TRUE );
606 }
607
608 static LRESULT WINAPI
609 MSIText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
610 {
611     struct msi_text_info *info;
612     LRESULT r = 0;
613
614     TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
615
616     info = GetPropW(hWnd, szButtonData);
617
618     if ( info->font )
619         SetTextColor( (HDC)wParam, info->font->color );
620
621     if( msg == WM_CTLCOLORSTATIC &&
622        ( info->attributes & msidbControlAttributesTransparent ) )
623     {
624         SetBkMode( (HDC)wParam, TRANSPARENT );
625         return (LRESULT) GetStockObject(NULL_BRUSH);
626     }
627
628     r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
629
630     switch( msg )
631     {
632     case WM_SETTEXT:
633         msi_text_on_settext( hWnd );
634         break;
635     case WM_NCDESTROY:
636         msi_free( info );
637         RemovePropW( hWnd, szButtonData );
638         break;
639     }
640
641     return r;
642 }
643
644 static UINT msi_dialog_text_control( msi_dialog *dialog, MSIRECORD *rec )
645 {
646     msi_control *control;
647     struct msi_text_info *info;
648     LPCWSTR text, ptr;
649     LPWSTR font_name;
650
651     TRACE("%p %p\n", dialog, rec);
652
653     control = msi_dialog_add_control( dialog, rec, szStatic, SS_LEFT | WS_GROUP );
654     if( !control )
655         return ERROR_FUNCTION_FAILED;
656
657     info = msi_alloc( sizeof *info );
658     if( !info )
659         return ERROR_SUCCESS;
660
661     text = MSI_RecordGetString( rec, 10 );
662     font_name = msi_dialog_get_style( text, &ptr );
663     info->font = ( font_name ) ? msi_dialog_find_font( dialog, font_name ) : NULL;
664     msi_free( font_name );
665
666     info->attributes = MSI_RecordGetInteger( rec, 8 );
667     if( info->attributes & msidbControlAttributesTransparent )
668         SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT );
669
670     info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
671                                           (LONG_PTR)MSIText_WndProc );
672     SetPropW( control->hwnd, szButtonData, info );
673
674     return ERROR_SUCCESS;
675 }
676
677 static UINT msi_dialog_button_control( msi_dialog *dialog, MSIRECORD *rec )
678 {
679     msi_control *control;
680     UINT attributes, style;
681     LPWSTR text;
682
683     TRACE("%p %p\n", dialog, rec);
684
685     style = WS_TABSTOP;
686     attributes = MSI_RecordGetInteger( rec, 8 );
687     if( attributes & msidbControlAttributesIcon )
688         style |= BS_ICON;
689
690     control = msi_dialog_add_control( dialog, rec, szButton, style );
691     if( !control )
692         return ERROR_FUNCTION_FAILED;
693
694     control->handler = msi_dialog_button_handler;
695
696     /* set the icon */
697     text = msi_get_deformatted_field( dialog->package, rec, 10 );
698     control->hIcon = msi_load_icon( dialog->package->db, text, attributes );
699     if( attributes & msidbControlAttributesIcon )
700         SendMessageW( control->hwnd, BM_SETIMAGE, IMAGE_ICON, (LPARAM) control->hIcon );
701     msi_free( text );
702
703     return ERROR_SUCCESS;
704 }
705
706 static LPWSTR msi_get_checkbox_value( msi_dialog *dialog, LPCWSTR prop )
707 {
708     static const WCHAR query[] = {
709         'S','E','L','E','C','T',' ','*',' ',
710         'F','R','O','M',' ','`','C','h','e','c','k','B','o','x',' ','`',
711         'W','H','E','R','E',' ',
712         '`','P','r','o','p','e','r','t','y','`',' ','=',' ',
713         '\'','%','s','\'',0
714     };
715     MSIRECORD *rec = NULL;
716     LPWSTR ret = NULL;
717
718     /* find if there is a value associated with the checkbox */
719     rec = MSI_QueryGetRecord( dialog->package->db, query, prop );
720     if (!rec)
721         return ret;
722
723     ret = msi_get_deformatted_field( dialog->package, rec, 2 );
724     if( ret && !ret[0] )
725     {
726         msi_free( ret );
727         ret = NULL;
728     }
729     msiobj_release( &rec->hdr );
730     if (ret)
731         return ret;
732
733     ret = msi_dup_property( dialog->package, prop );
734     if( ret && !ret[0] )
735     {
736         msi_free( ret );
737         ret = NULL;
738     }
739
740     return ret;
741 }
742
743 static UINT msi_dialog_checkbox_control( msi_dialog *dialog, MSIRECORD *rec )
744 {
745     msi_control *control;
746     LPCWSTR prop;
747
748     TRACE("%p %p\n", dialog, rec);
749
750     control = msi_dialog_add_control( dialog, rec, szButton,
751                                 BS_CHECKBOX | BS_MULTILINE | WS_TABSTOP );
752     control->handler = msi_dialog_checkbox_handler;
753     prop = MSI_RecordGetString( rec, 9 );
754     if( prop )
755     {
756         control->property = strdupW( prop );
757         control->value = msi_get_checkbox_value( dialog, prop );
758         TRACE("control %s value %s\n", debugstr_w(control->property),
759               debugstr_w(control->value));
760     }
761     msi_dialog_checkbox_sync_state( dialog, control );
762
763     return ERROR_SUCCESS;
764 }
765
766 static UINT msi_dialog_line_control( msi_dialog *dialog, MSIRECORD *rec )
767 {
768     TRACE("%p %p\n", dialog, rec);
769
770     /* line is exactly 2 units in height */
771     MSI_RecordSetInteger( rec, 7, 2 );
772
773     msi_dialog_add_control( dialog, rec, szStatic, SS_ETCHEDHORZ | SS_SUNKEN );
774     return ERROR_SUCCESS;
775 }
776
777 /******************** Scroll Text ********************************************/
778
779 struct msi_scrolltext_info
780 {
781     msi_dialog *dialog;
782     msi_control *control;
783     WNDPROC oldproc;
784 };
785
786 static LRESULT WINAPI
787 MSIScrollText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
788 {
789     struct msi_scrolltext_info *info;
790     HRESULT r;
791
792     TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
793
794     info = GetPropW( hWnd, szButtonData );
795
796     r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
797
798     switch( msg )
799     {
800     case WM_NCDESTROY:
801         msi_free( info );
802         RemovePropW( hWnd, szButtonData );
803         break;
804     case WM_PAINT:
805         /* native MSI sets a wait cursor here */
806         msi_dialog_button_handler( info->dialog, info->control, BN_CLICKED );
807         break;
808     }
809     return r;
810 }
811
812 struct msi_streamin_info
813 {
814     LPSTR string;
815     DWORD offset;
816     DWORD length;
817 };
818
819 static DWORD CALLBACK
820 msi_richedit_stream_in( DWORD_PTR arg, LPBYTE buffer, LONG count, LONG *pcb )
821 {
822     struct msi_streamin_info *info = (struct msi_streamin_info*) arg;
823
824     if( (count + info->offset) > info->length )
825         count = info->length - info->offset;
826     memcpy( buffer, &info->string[ info->offset ], count );
827     *pcb = count;
828     info->offset += count;
829
830     TRACE("%ld/%ld\n", info->offset, info->length);
831
832     return 0;
833 }
834
835 static void msi_scrolltext_add_text( msi_control *control, LPCWSTR text )
836 {
837     struct msi_streamin_info info;
838     EDITSTREAM es;
839
840     info.string = strdupWtoA( text );
841     info.offset = 0;
842     info.length = lstrlenA( info.string ) + 1;
843
844     es.dwCookie = (DWORD_PTR) &info;
845     es.dwError = 0;
846     es.pfnCallback = msi_richedit_stream_in;
847
848     SendMessageW( control->hwnd, EM_STREAMIN, SF_RTF, (LPARAM) &es );
849
850     msi_free( info.string );
851 }
852
853 static UINT msi_dialog_scrolltext_control( msi_dialog *dialog, MSIRECORD *rec )
854 {
855     static const WCHAR szRichEdit20W[] = {
856         'R','i','c','h','E','d','i','t','2','0','W',0
857     };
858     struct msi_scrolltext_info *info;
859     msi_control *control;
860     HMODULE hRichedit;
861     DWORD style;
862
863     info = msi_alloc( sizeof *info );
864     if (!info)
865         return ERROR_FUNCTION_FAILED;
866
867     hRichedit = LoadLibraryA("riched20");
868
869     style = WS_BORDER | ES_MULTILINE | WS_VSCROLL |
870             ES_READONLY | ES_AUTOVSCROLL | WS_TABSTOP;
871     control = msi_dialog_add_control( dialog, rec, szRichEdit20W, style );
872     if (!control)
873     {
874         FreeLibrary( hRichedit );
875         msi_free( info );
876         return ERROR_FUNCTION_FAILED;
877     }
878
879     control->hDll = hRichedit;
880
881     info->dialog = dialog;
882     info->control = control;
883
884     /* subclass the static control */
885     info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
886                                           (LONG_PTR)MSIScrollText_WndProc );
887     SetPropW( control->hwnd, szButtonData, info );
888
889     /* add the text into the richedit */
890     msi_scrolltext_add_text( control, MSI_RecordGetString( rec, 10 ) );
891
892     return ERROR_SUCCESS;
893 }
894
895 static HBITMAP msi_load_picture( MSIDATABASE *db, LPCWSTR name,
896                                  INT cx, INT cy, DWORD flags )
897 {
898     HBITMAP hOleBitmap = 0, hBitmap = 0, hOldSrcBitmap, hOldDestBitmap;
899     MSIRECORD *rec = NULL;
900     IStream *stm = NULL;
901     IPicture *pic = NULL;
902     HDC srcdc, destdc;
903     BITMAP bm;
904     UINT r;
905
906     rec = msi_get_binary_record( db, name );
907     if( !rec )
908         goto end;
909
910     r = MSI_RecordGetIStream( rec, 2, &stm );
911     msiobj_release( &rec->hdr );
912     if( r != ERROR_SUCCESS )
913         goto end;
914
915     r = OleLoadPicture( stm, 0, TRUE, &IID_IPicture, (LPVOID*) &pic );
916     IStream_Release( stm );
917     if( FAILED( r ) )
918     {
919         ERR("failed to load picture\n");
920         goto end;
921     }
922
923     r = IPicture_get_Handle( pic, (OLE_HANDLE*) &hOleBitmap );
924     if( FAILED( r ) )
925     {
926         ERR("failed to get bitmap handle\n");
927         goto end;
928     }
929  
930     /* make the bitmap the desired size */
931     r = GetObjectW( hOleBitmap, sizeof bm, &bm );
932     if (r != sizeof bm )
933     {
934         ERR("failed to get bitmap size\n");
935         goto end;
936     }
937
938     if (flags & LR_DEFAULTSIZE)
939     {
940         cx = bm.bmWidth;
941         cy = bm.bmHeight;
942     }
943
944     srcdc = CreateCompatibleDC( NULL );
945     hOldSrcBitmap = SelectObject( srcdc, hOleBitmap );
946     destdc = CreateCompatibleDC( NULL );
947     hBitmap = CreateCompatibleBitmap( srcdc, cx, cy );
948     hOldDestBitmap = SelectObject( destdc, hBitmap );
949     StretchBlt( destdc, 0, 0, cx, cy,
950                 srcdc, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
951     SelectObject( srcdc, hOldSrcBitmap );
952     SelectObject( destdc, hOldDestBitmap );
953     DeleteDC( srcdc );
954     DeleteDC( destdc );
955
956 end:
957     if ( pic )
958         IPicture_Release( pic );
959     return hBitmap;
960 }
961
962 static UINT msi_dialog_bitmap_control( msi_dialog *dialog, MSIRECORD *rec )
963 {
964     UINT cx, cy, flags, style, attributes;
965     msi_control *control;
966     LPWSTR text;
967
968     flags = LR_LOADFROMFILE;
969     style = SS_BITMAP | SS_LEFT | WS_GROUP;
970
971     attributes = MSI_RecordGetInteger( rec, 8 );
972     if( attributes & msidbControlAttributesFixedSize )
973     {
974         flags |= LR_DEFAULTSIZE;
975         style |= SS_CENTERIMAGE;
976     }
977
978     control = msi_dialog_add_control( dialog, rec, szStatic, style );
979     cx = MSI_RecordGetInteger( rec, 6 );
980     cy = MSI_RecordGetInteger( rec, 7 );
981     cx = msi_dialog_scale_unit( dialog, cx );
982     cy = msi_dialog_scale_unit( dialog, cy );
983
984     text = msi_get_deformatted_field( dialog->package, rec, 10 );
985     control->hBitmap = msi_load_picture( dialog->package->db, text, cx, cy, flags );
986     if( control->hBitmap )
987         SendMessageW( control->hwnd, STM_SETIMAGE,
988                       IMAGE_BITMAP, (LPARAM) control->hBitmap );
989     else
990         ERR("Failed to load bitmap %s\n", debugstr_w(text));
991
992     msi_free( text );
993     
994     return ERROR_SUCCESS;
995 }
996
997 static UINT msi_dialog_icon_control( msi_dialog *dialog, MSIRECORD *rec )
998 {
999     msi_control *control;
1000     DWORD attributes;
1001     LPWSTR text;
1002
1003     TRACE("\n");
1004
1005     control = msi_dialog_add_control( dialog, rec, szStatic,
1006                             SS_ICON | SS_CENTERIMAGE | WS_GROUP );
1007             
1008     attributes = MSI_RecordGetInteger( rec, 8 );
1009     text = msi_get_deformatted_field( dialog->package, rec, 10 );
1010     control->hIcon = msi_load_icon( dialog->package->db, text, attributes );
1011     if( control->hIcon )
1012         SendMessageW( control->hwnd, STM_SETICON, (WPARAM) control->hIcon, 0 );
1013     else
1014         ERR("Failed to load bitmap %s\n", debugstr_w(text));
1015     msi_free( text );
1016     return ERROR_SUCCESS;
1017 }
1018
1019 static UINT msi_dialog_combo_control( msi_dialog *dialog, MSIRECORD *rec )
1020 {
1021     static const WCHAR szCombo[] = { 'C','O','M','B','O','B','O','X',0 };
1022
1023     msi_dialog_add_control( dialog, rec, szCombo,
1024                             SS_BITMAP | SS_LEFT | SS_CENTERIMAGE );
1025     return ERROR_SUCCESS;
1026 }
1027
1028 static UINT msi_dialog_edit_control( msi_dialog *dialog, MSIRECORD *rec )
1029 {
1030     msi_control *control;
1031     LPCWSTR prop;
1032     LPWSTR val;
1033
1034     control = msi_dialog_add_control( dialog, rec, szEdit,
1035                                       WS_BORDER | WS_TABSTOP );
1036     control->handler = msi_dialog_edit_handler;
1037     prop = MSI_RecordGetString( rec, 9 );
1038     if( prop )
1039         control->property = strdupW( prop );
1040     val = msi_dup_property( dialog->package, control->property );
1041     SetWindowTextW( control->hwnd, val );
1042     msi_free( val );
1043     return ERROR_SUCCESS;
1044 }
1045
1046 /******************** Masked Edit ********************************************/
1047
1048 #define MASK_MAX_GROUPS 10
1049
1050 struct msi_mask_group
1051 {
1052     UINT len;
1053     UINT ofs;
1054     WCHAR type;
1055     HWND hwnd;
1056 };
1057
1058 struct msi_maskedit_info
1059 {
1060     msi_dialog *dialog;
1061     WNDPROC oldproc;
1062     HWND hwnd;
1063     LPWSTR prop;
1064     UINT num_chars;
1065     UINT num_groups;
1066     struct msi_mask_group group[MASK_MAX_GROUPS];
1067 };
1068
1069 static BOOL msi_mask_editable( WCHAR type )
1070 {
1071     switch (type)
1072     {
1073     case '%':
1074     case '#':
1075     case '&':
1076     case '`':
1077     case '?':
1078     case '^':
1079         return TRUE;
1080     }
1081     return FALSE;
1082 }
1083
1084 static void msi_mask_control_change( struct msi_maskedit_info *info )
1085 {
1086     LPWSTR val;
1087     UINT i, n, r;
1088
1089     val = msi_alloc( (info->num_chars+1)*sizeof(WCHAR) );
1090     for( i=0, n=0; i<info->num_groups; i++ )
1091     {
1092         if( (info->group[i].len + n) > info->num_chars )
1093         {
1094             ERR("can't fit control %d text into template\n",i);
1095             break;
1096         }
1097         if (!msi_mask_editable(info->group[i].type))
1098         {
1099             for(r=0; r<info->group[i].len; r++)
1100                 val[n+r] = info->group[i].type;
1101             val[n+r] = 0;
1102         }
1103         else
1104         {
1105             r = GetWindowTextW( info->group[i].hwnd, &val[n], info->group[i].len+1 );
1106             if( r != info->group[i].len )
1107                 break;
1108         }
1109         n += r;
1110     }
1111
1112     TRACE("%d/%d controls were good\n", i, info->num_groups);
1113
1114     if( i == info->num_groups )
1115     {
1116         TRACE("Set property %s to %s\n",
1117               debugstr_w(info->prop), debugstr_w(val) );
1118         CharUpperBuffW( val, info->num_chars );
1119         MSI_SetPropertyW( info->dialog->package, info->prop, val );
1120         msi_dialog_evaluate_control_conditions( info->dialog );
1121     }
1122     msi_free( val );
1123 }
1124
1125 /* now move to the next control if necessary */
1126 static VOID msi_mask_next_control( struct msi_maskedit_info *info, HWND hWnd )
1127 {
1128     HWND hWndNext;
1129     UINT len, i;
1130
1131     for( i=0; i<info->num_groups; i++ )
1132         if( info->group[i].hwnd == hWnd )
1133             break;
1134
1135     /* don't move from the last control */
1136     if( i >= (info->num_groups-1) )
1137         return;
1138
1139     len = SendMessageW( hWnd, WM_GETTEXTLENGTH, 0, 0 );
1140     if( len < info->group[i].len )
1141         return;
1142
1143     hWndNext = GetNextDlgTabItem( GetParent( hWnd ), hWnd, FALSE );
1144     SetFocus( hWndNext );
1145 }
1146
1147 static LRESULT WINAPI
1148 MSIMaskedEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1149 {
1150     struct msi_maskedit_info *info;
1151     HRESULT r;
1152
1153     TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
1154
1155     info = GetPropW(hWnd, szButtonData);
1156
1157     r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1158
1159     switch( msg )
1160     {
1161     case WM_COMMAND:
1162         if (HIWORD(wParam) == EN_CHANGE)
1163         {
1164             msi_mask_control_change( info );
1165             msi_mask_next_control( info, (HWND) lParam );
1166         }
1167         break;
1168     case WM_NCDESTROY:
1169         msi_free( info->prop );
1170         msi_free( info );
1171         RemovePropW( hWnd, szButtonData );
1172         break;
1173     }
1174
1175     return r;
1176 }
1177
1178 /* fish the various bits of the property out and put them in the control */
1179 static void
1180 msi_maskedit_set_text( struct msi_maskedit_info *info, LPCWSTR text )
1181 {
1182     LPCWSTR p;
1183     UINT i;
1184
1185     p = text;
1186     for( i = 0; i < info->num_groups; i++ )
1187     {
1188         if( info->group[i].len < lstrlenW( p ) )
1189         {
1190             LPWSTR chunk = strdupW( p );
1191             chunk[ info->group[i].len ] = 0;
1192             SetWindowTextW( info->group[i].hwnd, chunk );
1193             msi_free( chunk );
1194         }
1195         else
1196         {
1197             SetWindowTextW( info->group[i].hwnd, p );
1198             break;
1199         }
1200         p += info->group[i].len;
1201     }
1202 }
1203
1204 static struct msi_maskedit_info * msi_dialog_parse_groups( LPCWSTR mask )
1205 {
1206     struct msi_maskedit_info * info = NULL;
1207     int i = 0, n = 0, total = 0;
1208     LPCWSTR p;
1209
1210     TRACE("masked control, template %s\n", debugstr_w(mask));
1211
1212     if( !mask )
1213         return info;
1214
1215     info = msi_alloc_zero( sizeof *info );
1216     if( !info )
1217         return info;
1218
1219     p = strchrW(mask, '<');
1220     if( p )
1221         p++;
1222     else
1223         p = mask;
1224
1225     for( i=0; i<MASK_MAX_GROUPS; i++ )
1226     {
1227         /* stop at the end of the string */
1228         if( p[0] == 0 || p[0] == '>' )
1229             break;
1230
1231         /* count the number of the same identifier */
1232         for( n=0; p[n] == p[0]; n++ )
1233             ;
1234         info->group[i].ofs = total;
1235         info->group[i].type = p[0];
1236         if( p[n] == '=' )
1237         {
1238             n++;
1239             total++; /* an extra not part of the group */
1240         }
1241         info->group[i].len = n;
1242         total += n;
1243         p += n;
1244     }
1245
1246     TRACE("%d characters in %d groups\n", total, i );
1247     if( i == MASK_MAX_GROUPS )
1248         ERR("too many groups in PIDTemplate %s\n", debugstr_w(mask));
1249
1250     info->num_chars = total;
1251     info->num_groups = i;
1252
1253     return info;
1254 }
1255
1256 static void
1257 msi_maskedit_create_children( struct msi_maskedit_info *info, LPCWSTR font )
1258 {
1259     DWORD width, height, style, wx, ww;
1260     RECT rect;
1261     HWND hwnd;
1262     UINT i;
1263
1264     style = WS_CHILD | WS_BORDER | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL;
1265
1266     GetClientRect( info->hwnd, &rect );
1267
1268     width = rect.right - rect.left;
1269     height = rect.bottom - rect.top;
1270
1271     for( i = 0; i < info->num_groups; i++ )
1272     {
1273         if (!msi_mask_editable( info->group[i].type ))
1274             continue;
1275         wx = (info->group[i].ofs * width) / info->num_chars;
1276         ww = (info->group[i].len * width) / info->num_chars;
1277
1278         hwnd = CreateWindowW( szEdit, NULL, style, wx, 0, ww, height,
1279                               info->hwnd, NULL, NULL, NULL );
1280         if( !hwnd )
1281         {
1282             ERR("failed to create mask edit sub window\n");
1283             break;
1284         }
1285
1286         SendMessageW( hwnd, EM_LIMITTEXT, info->group[i].len, 0 );
1287
1288         msi_dialog_set_font( info->dialog, hwnd,
1289                              font?font:info->dialog->default_font );
1290         info->group[i].hwnd = hwnd;
1291     }
1292 }
1293
1294 /*
1295  * office 2003 uses "73931<````=````=````=````=`````>@@@@@"
1296  * delphi 7 uses "<????-??????-??????-????>" and "<???-???>"
1297  * filemaker pro 7 uses "<^^^^=^^^^=^^^^=^^^^=^^^^=^^^^=^^^^^>"
1298  */
1299 static UINT msi_dialog_maskedit_control( msi_dialog *dialog, MSIRECORD *rec )
1300 {
1301     LPWSTR font_mask, val = NULL, font;
1302     struct msi_maskedit_info *info = NULL;
1303     UINT ret = ERROR_SUCCESS;
1304     msi_control *control;
1305     LPCWSTR prop, mask;
1306
1307     TRACE("\n");
1308
1309     font_mask = msi_get_deformatted_field( dialog->package, rec, 10 );
1310     font = msi_dialog_get_style( font_mask, &mask );
1311     if( !mask )
1312     {
1313         ERR("mask template is empty\n");
1314         goto end;
1315     }
1316
1317     info = msi_dialog_parse_groups( mask );
1318     if( !info )
1319     {
1320         ERR("template %s is invalid\n", debugstr_w(mask));
1321         goto end;
1322     }
1323
1324     info->dialog = dialog;
1325
1326     control = msi_dialog_add_control( dialog, rec, szStatic,
1327                    SS_OWNERDRAW | WS_GROUP | WS_VISIBLE );
1328     if( !control )
1329     {
1330         ERR("Failed to create maskedit container\n");
1331         ret = ERROR_FUNCTION_FAILED;
1332         goto end;
1333     }
1334     SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
1335
1336     info->hwnd = control->hwnd;
1337
1338     /* subclass the static control */
1339     info->oldproc = (WNDPROC) SetWindowLongPtrW( info->hwnd, GWLP_WNDPROC,
1340                                           (LONG_PTR)MSIMaskedEdit_WndProc );
1341     SetPropW( control->hwnd, szButtonData, info );
1342
1343     prop = MSI_RecordGetString( rec, 9 );
1344     if( prop )
1345         info->prop = strdupW( prop );
1346
1347     msi_maskedit_create_children( info, font );
1348
1349     if( prop )
1350     {
1351         val = msi_dup_property( dialog->package, prop );
1352         if( val )
1353         {
1354             msi_maskedit_set_text( info, val );
1355             msi_free( val );
1356         }
1357     }
1358
1359 end:
1360     if( ret != ERROR_SUCCESS )
1361         msi_free( info );
1362     msi_free( font_mask );
1363     msi_free( font );
1364     return ret;
1365 }
1366
1367 /******************** Progress Bar *****************************************/
1368
1369 static UINT msi_dialog_progress_bar( msi_dialog *dialog, MSIRECORD *rec )
1370 {
1371     msi_dialog_add_control( dialog, rec, PROGRESS_CLASSW, WS_VISIBLE );
1372     return ERROR_SUCCESS;
1373 }
1374
1375 /******************** Path Edit ********************************************/
1376
1377 static LPWSTR msi_get_window_text( HWND hwnd )
1378 {
1379     UINT sz, r;
1380     LPWSTR buf;
1381
1382     sz = 0x20;
1383     buf = msi_alloc( sz*sizeof(WCHAR) );
1384     while ( buf )
1385     {
1386         r = GetWindowTextW( hwnd, buf, sz );
1387         if ( r < (sz - 1) )
1388             break;
1389         sz *= 2;
1390         buf = msi_realloc( buf, sz*sizeof(WCHAR) );
1391     }
1392
1393     return buf;
1394 }
1395
1396 static UINT msi_dialog_pathedit_handler( msi_dialog *dialog,
1397                 msi_control *control, WPARAM param )
1398 {
1399     LPCWSTR prop;
1400     LPWSTR buf, indirect = NULL;
1401
1402     if( HIWORD(param) != EN_KILLFOCUS )
1403         return ERROR_SUCCESS;
1404
1405     prop = control->property;
1406     if ( control->attributes & msidbControlAttributesIndirect )
1407     {
1408         indirect = msi_dup_property( dialog->package, control->property );
1409         prop = indirect;
1410     }
1411
1412     /* FIXME: verify the new path */
1413     buf = msi_get_window_text( control->hwnd );
1414     MSI_SetPropertyW( dialog->package, prop, buf );
1415
1416     TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
1417           debugstr_w(prop));
1418
1419     msi_free( buf );
1420     msi_free( indirect );
1421
1422     return ERROR_SUCCESS;
1423 }
1424
1425 static void msi_dialog_update_pathedit( msi_dialog *dialog )
1426 {
1427     msi_control *control;
1428     LPWSTR prop, path;
1429     BOOL indirect;
1430
1431     control = msi_dialog_find_control( dialog, szPathEdit );
1432     indirect = control->attributes & msidbControlAttributesIndirect;
1433     prop = msi_dialog_dup_property( dialog, control->property, indirect );
1434
1435     path = msi_dup_property( dialog->package, prop );
1436     SetWindowTextW( control->hwnd, path );
1437     SendMessageW( control->hwnd, EM_SETSEL, 0, -1 );
1438
1439     msi_free( path );
1440     msi_free( prop );
1441 }
1442
1443 static UINT msi_dialog_pathedit_control( msi_dialog *dialog, MSIRECORD *rec )
1444 {
1445     msi_control *control;
1446     LPCWSTR prop;
1447
1448     control = msi_dialog_add_control( dialog, rec, szEdit,
1449                                       WS_BORDER | WS_TABSTOP );
1450     control->handler = msi_dialog_pathedit_handler;
1451     control->attributes = MSI_RecordGetInteger( rec, 8 );
1452
1453     prop = MSI_RecordGetString( rec, 9 );
1454     if ( prop )
1455         control->property = strdupW( prop );
1456
1457     msi_dialog_update_pathedit( dialog );
1458
1459     return ERROR_SUCCESS;
1460 }
1461
1462 /* radio buttons are a bit different from normal controls */
1463 static UINT msi_dialog_create_radiobutton( MSIRECORD *rec, LPVOID param )
1464 {
1465     radio_button_group_descr *group = (radio_button_group_descr *)param;
1466     msi_dialog *dialog = group->dialog;
1467     msi_control *control;
1468     LPCWSTR prop, text, name;
1469     DWORD style, attributes = group->attributes;
1470
1471     style = WS_CHILD | BS_AUTORADIOBUTTON | BS_MULTILINE | WS_TABSTOP;
1472     name = MSI_RecordGetString( rec, 3 );
1473     text = MSI_RecordGetString( rec, 8 );
1474     if( attributes & 1 )
1475         style |= WS_VISIBLE;
1476     if( ~attributes & 2 )
1477         style |= WS_DISABLED;
1478
1479     control = msi_dialog_create_window( dialog, rec, 0, szButton, name, text,
1480                                         style, group->parent->hwnd );
1481     if (!control)
1482         return ERROR_FUNCTION_FAILED;
1483     control->handler = msi_dialog_radiogroup_handler;
1484
1485     if (!lstrcmpW(control->name, group->propval))
1486         SendMessageW(control->hwnd, BM_SETCHECK, BST_CHECKED, 0);
1487
1488     prop = MSI_RecordGetString( rec, 1 );
1489     if( prop )
1490         control->property = strdupW( prop );
1491
1492     return ERROR_SUCCESS;
1493 }
1494
1495 static UINT msi_dialog_radiogroup_control( msi_dialog *dialog, MSIRECORD *rec )
1496 {
1497     static const WCHAR query[] = {
1498         'S','E','L','E','C','T',' ','*',' ',
1499         'F','R','O','M',' ','R','a','d','i','o','B','u','t','t','o','n',' ',
1500         'W','H','E','R','E',' ',
1501            '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
1502     UINT r;
1503     LPCWSTR prop;
1504     msi_control *control;
1505     MSIQUERY *view = NULL;
1506     radio_button_group_descr group;
1507     MSIPACKAGE *package = dialog->package;
1508     WNDPROC oldproc;
1509
1510     prop = MSI_RecordGetString( rec, 9 );
1511
1512     TRACE("%p %p %s\n", dialog, rec, debugstr_w( prop ));
1513
1514     /* Create parent group box to hold radio buttons */
1515     control = msi_dialog_add_control( dialog, rec, szButton, BS_OWNERDRAW|WS_GROUP );
1516     if( !control )
1517         return ERROR_FUNCTION_FAILED;
1518
1519     oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1520                                            (LONG_PTR)MSIRadioGroup_WndProc );
1521     SetPropW(control->hwnd, szButtonData, oldproc);
1522     SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
1523
1524     if( prop )
1525         control->property = strdupW( prop );
1526
1527     /* query the Radio Button table for all control in this group */
1528     r = MSI_OpenQuery( package->db, &view, query, prop );
1529     if( r != ERROR_SUCCESS )
1530     {
1531         ERR("query failed for dialog %s radio group %s\n", 
1532             debugstr_w(dialog->name), debugstr_w(prop));
1533         return ERROR_INVALID_PARAMETER;
1534     }
1535
1536     group.dialog = dialog;
1537     group.parent = control;
1538     group.attributes = MSI_RecordGetInteger( rec, 8 );
1539     group.propval = msi_dup_property( dialog->package, control->property );
1540
1541     r = MSI_IterateRecords( view, 0, msi_dialog_create_radiobutton, &group );
1542     msiobj_release( &view->hdr );
1543     msi_free( group.propval );
1544
1545     return r;
1546 }
1547
1548 /******************** Selection Tree ***************************************/
1549
1550 struct msi_selection_tree_info
1551 {
1552     msi_dialog *dialog;
1553     HWND hwnd;
1554     WNDPROC oldproc;
1555 };
1556
1557 static void
1558 msi_seltree_sync_item_state( HWND hwnd, MSIFEATURE *feature, HTREEITEM hItem )
1559 {
1560     TVITEMW tvi;
1561
1562     TRACE("Feature %s -> %d %d %d\n", debugstr_w(feature->Title),
1563         feature->Installed, feature->Action, feature->ActionRequest);
1564
1565     tvi.mask = TVIF_STATE;
1566     tvi.hItem = hItem;
1567     tvi.state = INDEXTOSTATEIMAGEMASK( feature->Action );
1568     tvi.stateMask = TVIS_STATEIMAGEMASK;
1569
1570     SendMessageW( hwnd, TVM_SETITEMW, 0, (LPARAM) &tvi );
1571 }
1572
1573 static UINT
1574 msi_seltree_popup_menu( HWND hwnd, INT x, INT y )
1575 {
1576     HMENU hMenu;
1577     INT r;
1578
1579     /* create a menu to display */
1580     hMenu = CreatePopupMenu();
1581
1582     /* FIXME: load strings from resources */
1583     AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_LOCAL, "Install feature locally");
1584     AppendMenuA( hMenu, MF_GRAYED, 0x1000, "Install entire feature");
1585     AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ADVERTISED, "Install on demand");
1586     AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ABSENT, "Don't install");
1587     r = TrackPopupMenu( hMenu, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RETURNCMD,
1588                         x, y, 0, hwnd, NULL );
1589     DestroyMenu( hMenu );
1590     return r;
1591 }
1592
1593 static MSIFEATURE *
1594 msi_seltree_feature_from_item( HWND hwnd, HTREEITEM hItem )
1595 {
1596     TVITEMW tvi;
1597
1598     /* get the feature from the item */
1599     memset( &tvi, 0, sizeof tvi );
1600     tvi.hItem = hItem;
1601     tvi.mask = TVIF_PARAM | TVIF_HANDLE;
1602     SendMessageW( hwnd, TVM_GETITEMW, 0, (LPARAM) &tvi );
1603
1604     return (MSIFEATURE*) tvi.lParam;
1605 }
1606
1607 static LRESULT
1608 msi_seltree_menu( HWND hwnd, HTREEITEM hItem )
1609 {
1610     MSIFEATURE *feature;
1611     ComponentList *cl;
1612     union {
1613         RECT rc;
1614         POINT pt[2];
1615         HTREEITEM hItem;
1616     } u;
1617     UINT r;
1618
1619     feature = msi_seltree_feature_from_item( hwnd, hItem );
1620     if (!feature)
1621     {
1622         ERR("item %p feature was NULL\n", hItem);
1623         return 0;
1624     }
1625
1626     /* get the item's rectangle to put the menu just below it */
1627     u.hItem = hItem;
1628     SendMessageW( hwnd, TVM_GETITEMRECT, 0, (LPARAM) &u.rc );
1629     MapWindowPoints( hwnd, NULL, u.pt, 2 );
1630
1631     r = msi_seltree_popup_menu( hwnd, u.rc.left, u.rc.top );
1632
1633     switch (r)
1634     {
1635     case INSTALLSTATE_LOCAL:
1636     case INSTALLSTATE_ADVERTISED:
1637     case INSTALLSTATE_ABSENT:
1638         feature->ActionRequest = r;
1639         feature->Action = r;
1640         break;
1641     default:
1642         FIXME("select feature and all children\n");
1643     }
1644
1645     /* update */
1646     msi_seltree_sync_item_state( hwnd, feature, hItem );
1647
1648     /* update the feature's components */
1649     LIST_FOR_EACH_ENTRY( cl, &feature->Components, ComponentList, entry )
1650     {
1651         cl->component->Action = feature->Action;
1652         cl->component->ActionRequest = feature->ActionRequest;
1653     }
1654
1655     return 0;
1656 }
1657
1658 static LRESULT WINAPI
1659 MSISelectionTree_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1660 {
1661     struct msi_selection_tree_info *info;
1662     TVHITTESTINFO tvhti;
1663     HRESULT r;
1664
1665     TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
1666
1667     info = GetPropW(hWnd, szButtonData);
1668
1669     switch( msg )
1670     {
1671     case WM_LBUTTONDOWN:
1672         tvhti.pt.x = LOWORD( lParam );
1673         tvhti.pt.y = HIWORD( lParam );
1674         tvhti.flags = 0;
1675         tvhti.hItem = 0;
1676         r = CallWindowProcW(info->oldproc, hWnd, TVM_HITTEST, 0, (LPARAM) &tvhti );
1677         if (tvhti.flags & TVHT_ONITEMSTATEICON)
1678             return msi_seltree_menu( hWnd, tvhti.hItem );
1679         break;
1680     }
1681
1682     r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1683
1684     switch( msg )
1685     {
1686     case WM_NCDESTROY:
1687         msi_free( info );
1688         RemovePropW( hWnd, szButtonData );
1689         break;
1690     }
1691     return r;
1692 }
1693
1694 static void
1695 msi_seltree_add_child_features( MSIPACKAGE *package, HWND hwnd,
1696                                 LPCWSTR parent, HTREEITEM hParent )
1697 {
1698     MSIFEATURE *feature;
1699     TVINSERTSTRUCTW tvis;
1700     HTREEITEM hitem;
1701
1702     LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
1703     {
1704         if ( lstrcmpW( parent, feature->Feature_Parent ) )
1705             continue;
1706
1707         if ( !feature->Title )
1708             continue;
1709
1710         if ( !feature->Display )
1711             continue;
1712
1713         memset( &tvis, 0, sizeof tvis );
1714         tvis.hParent = hParent;
1715         tvis.hInsertAfter = TVI_LAST;
1716         tvis.u.item.mask = TVIF_TEXT | TVIF_PARAM;
1717         tvis.u.item.pszText = feature->Title;
1718         tvis.u.item.lParam = (LPARAM) feature;
1719
1720         hitem = (HTREEITEM) SendMessageW( hwnd, TVM_INSERTITEMW, 0, (LPARAM) &tvis );
1721         if (!hitem)
1722             continue;
1723
1724         msi_seltree_sync_item_state( hwnd, feature, hitem );
1725         msi_seltree_add_child_features( package, hwnd,
1726                                         feature->Feature, hitem );
1727
1728         /* the node is expanded if Display is odd */
1729         if ( feature->Display % 2 != 0 )
1730             SendMessageW( hwnd, TVM_EXPAND, TVE_EXPAND, (LPARAM) hitem );
1731     }
1732 }
1733
1734 static void msi_seltree_create_imagelist( HWND hwnd )
1735 {
1736     const int bm_width = 32, bm_height = 16, bm_count = 3;
1737     const int bm_resource = 0x1001;
1738     HIMAGELIST himl;
1739     int i;
1740     HBITMAP hbmp;
1741
1742     himl = ImageList_Create( bm_width, bm_height, FALSE, 4, 0 );
1743     if (!himl)
1744     {
1745         ERR("failed to create image list\n");
1746         return;
1747     }
1748
1749     for (i=0; i<bm_count; i++)
1750     {
1751         hbmp = LoadBitmapW( msi_hInstance, MAKEINTRESOURCEW(i+bm_resource) );
1752         if (!hbmp)
1753         {
1754             ERR("failed to load bitmap %d\n", i);
1755             break;
1756         }
1757
1758         /*
1759          * Add a dummy bitmap at offset zero because the treeview
1760          * can't use it as a state mask (zero means no user state).
1761          */
1762         if (!i)
1763             ImageList_Add( himl, hbmp, NULL );
1764
1765         ImageList_Add( himl, hbmp, NULL );
1766     }
1767
1768     SendMessageW( hwnd, TVM_SETIMAGELIST, TVSIL_STATE, (LPARAM)himl );
1769 }
1770
1771 static UINT msi_dialog_selection_tree( msi_dialog *dialog, MSIRECORD *rec )
1772 {
1773     msi_control *control;
1774     LPCWSTR prop;
1775     MSIPACKAGE *package = dialog->package;
1776     DWORD style;
1777     struct msi_selection_tree_info *info;
1778
1779     info = msi_alloc( sizeof *info );
1780     if (!info)
1781         return ERROR_FUNCTION_FAILED;
1782
1783     /* create the treeview control */
1784     prop = MSI_RecordGetString( rec, 9 );
1785     style = TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT;
1786     style |= WS_GROUP | WS_VSCROLL;
1787     control = msi_dialog_add_control( dialog, rec, WC_TREEVIEWW, style );
1788     if (!control)
1789     {
1790         msi_free(info);
1791         return ERROR_FUNCTION_FAILED;
1792     }
1793
1794     /* subclass */
1795     info->dialog = dialog;
1796     info->hwnd = control->hwnd;
1797     info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1798                                           (LONG_PTR)MSISelectionTree_WndProc );
1799     SetPropW( control->hwnd, szButtonData, info );
1800
1801     /* initialize it */
1802     msi_seltree_create_imagelist( control->hwnd );
1803     msi_seltree_add_child_features( package, control->hwnd, NULL, NULL );
1804
1805     return ERROR_SUCCESS;
1806 }
1807
1808 /******************** Group Box ***************************************/
1809
1810 static UINT msi_dialog_group_box( msi_dialog *dialog, MSIRECORD *rec )
1811 {
1812     msi_control *control;
1813     DWORD style;
1814
1815     style = BS_GROUPBOX | WS_CHILD | WS_GROUP;
1816     control = msi_dialog_add_control( dialog, rec, WC_BUTTONW, style );
1817     if (!control)
1818         return ERROR_FUNCTION_FAILED;
1819
1820     return ERROR_SUCCESS;
1821 }
1822
1823 /******************** List Box ***************************************/
1824
1825 struct msi_listbox_item
1826 {
1827     LPWSTR property;
1828     LPWSTR value;
1829 };
1830
1831 struct msi_listbox_info
1832 {
1833     msi_dialog *dialog;
1834     HWND hwnd;
1835     WNDPROC oldproc;
1836     DWORD num_items;
1837     struct msi_listbox_item *items;
1838 };
1839
1840 static LRESULT WINAPI MSIListBox_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1841 {
1842     struct msi_listbox_info *info;
1843     LRESULT r;
1844     DWORD j;
1845
1846     TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
1847
1848     info = GetPropW( hWnd, szButtonData );
1849     if (!info)
1850         return 0;
1851
1852     r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
1853
1854     switch( msg )
1855     {
1856     case WM_NCDESTROY:
1857         for (j = 0; j < info->num_items; j++)
1858         {
1859             msi_free( info->items[j].property );
1860             msi_free( info->items[j].value );
1861         }
1862         msi_free( info->items );
1863         msi_free( info );
1864         RemovePropW( hWnd, szButtonData );
1865         break;
1866     }
1867
1868     return r;
1869 }
1870
1871 static UINT msi_listbox_add_item( MSIRECORD *rec, LPVOID param )
1872 {
1873     struct msi_listbox_info *info = param;
1874     struct msi_listbox_item *item;
1875     LPCWSTR property, value, text;
1876     static int index = 0;
1877
1878     item = &info->items[index++];
1879     property = MSI_RecordGetString( rec, 1 );
1880     value = MSI_RecordGetString( rec, 3 );
1881     text = MSI_RecordGetString( rec, 4 );
1882
1883     item->property = strdupW( property );
1884     item->value = strdupW( value );
1885
1886     SendMessageW( info->hwnd, LB_ADDSTRING, 0, (LPARAM)text );
1887
1888     return ERROR_SUCCESS;
1889 }
1890
1891 static UINT msi_listbox_add_items( struct msi_listbox_info *info )
1892 {
1893     UINT r;
1894     MSIQUERY *view = NULL;
1895     DWORD count;
1896
1897     static const WCHAR query[] = {
1898         'S','E','L','E','C','T',' ','*',' ',
1899         'F','R','O','M',' ','`','L','i','s','t','B','o','x','`',' ',
1900         'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','`',0
1901     };
1902
1903     r = MSI_OpenQuery( info->dialog->package->db, &view, query );
1904     if ( r != ERROR_SUCCESS )
1905         return r;
1906
1907     /* just get the number of records */
1908     r = MSI_IterateRecords( view, &count, NULL, NULL );
1909
1910     info->num_items = count;
1911     info->items = msi_alloc( sizeof(*info->items) * count );
1912
1913     r = MSI_IterateRecords( view, NULL, msi_listbox_add_item, info );
1914     msiobj_release( &view->hdr );
1915
1916     return r;
1917 }
1918
1919 static UINT msi_dialog_listbox_handler( msi_dialog *dialog,
1920                                         msi_control *control, WPARAM param )
1921 {
1922     struct msi_listbox_info *info;
1923     int index;
1924
1925     if( HIWORD(param) != LBN_SELCHANGE )
1926         return ERROR_SUCCESS;
1927
1928     info = GetPropW( control->hwnd, szButtonData );
1929     index = SendMessageW( control->hwnd, LB_GETCURSEL, 0, 0 );
1930
1931     MSI_SetPropertyW( info->dialog->package,
1932                       info->items[index].property, info->items[index].value );
1933     msi_dialog_evaluate_control_conditions( info->dialog );
1934
1935     return ERROR_SUCCESS;
1936 }
1937
1938 static UINT msi_dialog_list_box( msi_dialog *dialog, MSIRECORD *rec )
1939 {
1940     struct msi_listbox_info *info;
1941     msi_control *control;
1942     DWORD style;
1943
1944     info = msi_alloc( sizeof *info );
1945     if (!info)
1946         return ERROR_FUNCTION_FAILED;
1947
1948     style = WS_TABSTOP | WS_GROUP | WS_CHILD | LBS_STANDARD;
1949     control = msi_dialog_add_control( dialog, rec, WC_LISTBOXW, style );
1950     if (!control)
1951         return ERROR_FUNCTION_FAILED;
1952
1953     control->handler = msi_dialog_listbox_handler;
1954
1955     /* subclass */
1956     info->dialog = dialog;
1957     info->hwnd = control->hwnd;
1958     info->items = NULL;
1959     info->oldproc = (WNDPROC)SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1960                                                 (LONG_PTR)MSIListBox_WndProc );
1961     SetPropW( control->hwnd, szButtonData, info );
1962
1963     msi_listbox_add_items( info );
1964
1965     return ERROR_SUCCESS;
1966 }
1967
1968 /******************** Directory Combo ***************************************/
1969
1970 static void msi_dialog_update_directory_combo( msi_dialog *dialog )
1971 {
1972     msi_control *control;
1973     LPWSTR prop, path;
1974     BOOL indirect;
1975
1976     control = msi_dialog_find_control( dialog, szDirectoryCombo );
1977     indirect = control->attributes & msidbControlAttributesIndirect;
1978     prop = msi_dialog_dup_property( dialog, control->property, indirect );
1979     path = msi_dup_property( dialog->package, prop );
1980
1981     PathStripPathW( path );
1982     PathRemoveBackslashW( path );
1983
1984     SendMessageW( control->hwnd, CB_INSERTSTRING, 0, (LPARAM)path );
1985     SendMessageW( control->hwnd, CB_SETCURSEL, 0, 0 );
1986
1987     msi_free( path );
1988     msi_free( prop );
1989 }
1990
1991 static UINT msi_dialog_directory_combo( msi_dialog *dialog, MSIRECORD *rec )
1992 {
1993     msi_control *control;
1994     LPCWSTR prop;
1995     DWORD style;
1996
1997     /* FIXME: use CBS_OWNERDRAWFIXED and add owner draw code */
1998     style = CBS_DROPDOWNLIST | CBS_HASSTRINGS | WS_CHILD |
1999             WS_GROUP | WS_TABSTOP | WS_VSCROLL;
2000     control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
2001     if (!control)
2002         return ERROR_FUNCTION_FAILED;
2003
2004     control->attributes = MSI_RecordGetInteger( rec, 8 );
2005     prop = MSI_RecordGetString( rec, 9 );
2006     control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2007
2008     msi_dialog_update_directory_combo( dialog );
2009
2010     return ERROR_SUCCESS;
2011 }
2012
2013 /******************** Directory List ***************************************/
2014
2015 UINT msi_dialog_directorylist_up( msi_dialog *dialog )
2016 {
2017     msi_control *control;
2018     LPWSTR prop, path, ptr;
2019     BOOL indirect;
2020
2021     control = msi_dialog_find_control( dialog, szDirectoryList );
2022     indirect = control->attributes & msidbControlAttributesIndirect;
2023     prop = msi_dialog_dup_property( dialog, control->property, indirect );
2024
2025     path = msi_dup_property( dialog->package, prop );
2026
2027     /* strip off the last directory */
2028     ptr = PathFindFileNameW( path );
2029     if (ptr != path) *(ptr - 1) = '\0';
2030     PathAddBackslashW( path );
2031
2032     MSI_SetPropertyW( dialog->package, prop, path );
2033
2034     msi_dialog_update_directory_combo( dialog );
2035     msi_dialog_update_pathedit( dialog );
2036
2037     msi_free( path );
2038     msi_free( prop );
2039
2040     return ERROR_SUCCESS;
2041 }
2042
2043 static UINT msi_dialog_directory_list( msi_dialog *dialog, MSIRECORD *rec )
2044 {
2045     msi_control *control;
2046     LPCWSTR prop;
2047     DWORD style;
2048
2049     style = LVS_LIST | LVS_EDITLABELS | WS_VSCROLL | LVS_SHAREIMAGELISTS |
2050             LVS_AUTOARRANGE | LVS_SINGLESEL | WS_BORDER |
2051             LVS_SORTASCENDING | WS_CHILD | WS_GROUP | WS_TABSTOP;
2052     control = msi_dialog_add_control( dialog, rec, WC_LISTVIEWW, style );
2053     if (!control)
2054         return ERROR_FUNCTION_FAILED;
2055
2056     control->attributes = MSI_RecordGetInteger( rec, 8 );
2057     prop = MSI_RecordGetString( rec, 9 );
2058     control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2059
2060     return ERROR_SUCCESS;
2061 }
2062
2063 /******************** VolumeCost List ***************************************/
2064
2065 static UINT msi_dialog_volumecost_list( msi_dialog *dialog, MSIRECORD *rec )
2066 {
2067     msi_control *control;
2068     DWORD style;
2069
2070     style = LVS_REPORT | WS_VSCROLL | WS_HSCROLL | LVS_SHAREIMAGELISTS |
2071             LVS_AUTOARRANGE | LVS_SINGLESEL | WS_BORDER |
2072             WS_CHILD | WS_TABSTOP | WS_GROUP;
2073     control = msi_dialog_add_control( dialog, rec, WC_LISTVIEWW, style );
2074     if (!control)
2075         return ERROR_FUNCTION_FAILED;
2076
2077     return ERROR_SUCCESS;
2078 }
2079
2080 static const struct control_handler msi_dialog_handler[] =
2081 {
2082     { szText, msi_dialog_text_control },
2083     { szPushButton, msi_dialog_button_control },
2084     { szLine, msi_dialog_line_control },
2085     { szBitmap, msi_dialog_bitmap_control },
2086     { szCheckBox, msi_dialog_checkbox_control },
2087     { szScrollableText, msi_dialog_scrolltext_control },
2088     { szComboBox, msi_dialog_combo_control },
2089     { szEdit, msi_dialog_edit_control },
2090     { szMaskedEdit, msi_dialog_maskedit_control },
2091     { szPathEdit, msi_dialog_pathedit_control },
2092     { szProgressBar, msi_dialog_progress_bar },
2093     { szRadioButtonGroup, msi_dialog_radiogroup_control },
2094     { szIcon, msi_dialog_icon_control },
2095     { szSelectionTree, msi_dialog_selection_tree },
2096     { szGroupBox, msi_dialog_group_box },
2097     { szListBox, msi_dialog_list_box },
2098     { szDirectoryCombo, msi_dialog_directory_combo },
2099     { szDirectoryList, msi_dialog_directory_list },
2100     { szVolumeCostList, msi_dialog_volumecost_list },
2101 };
2102
2103 #define NUM_CONTROL_TYPES (sizeof msi_dialog_handler/sizeof msi_dialog_handler[0])
2104
2105 static UINT msi_dialog_create_controls( MSIRECORD *rec, LPVOID param )
2106 {
2107     msi_dialog *dialog = param;
2108     LPCWSTR control_type;
2109     UINT i;
2110
2111     /* find and call the function that can create this type of control */
2112     control_type = MSI_RecordGetString( rec, 3 );
2113     for( i=0; i<NUM_CONTROL_TYPES; i++ )
2114         if (!strcmpiW( msi_dialog_handler[i].control_type, control_type ))
2115             break;
2116     if( i != NUM_CONTROL_TYPES )
2117         msi_dialog_handler[i].func( dialog, rec );
2118     else
2119         ERR("no handler for element type %s\n", debugstr_w(control_type));
2120
2121     return ERROR_SUCCESS;
2122 }
2123
2124 static UINT msi_dialog_fill_controls( msi_dialog *dialog )
2125 {
2126     static const WCHAR query[] = {
2127         'S','E','L','E','C','T',' ','*',' ',
2128         'F','R','O','M',' ','C','o','n','t','r','o','l',' ',
2129         'W','H','E','R','E',' ',
2130            '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
2131     UINT r;
2132     MSIQUERY *view = NULL;
2133     MSIPACKAGE *package = dialog->package;
2134
2135     TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
2136
2137     /* query the Control table for all the elements of the control */
2138     r = MSI_OpenQuery( package->db, &view, query, dialog->name );
2139     if( r != ERROR_SUCCESS )
2140     {
2141         ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
2142         return ERROR_INVALID_PARAMETER;
2143     }
2144
2145     r = MSI_IterateRecords( view, 0, msi_dialog_create_controls, dialog );
2146     msiobj_release( &view->hdr );
2147
2148     return r;
2149 }
2150
2151 static UINT msi_dialog_set_control_condition( MSIRECORD *rec, LPVOID param )
2152 {
2153     static const WCHAR szHide[] = { 'H','i','d','e',0 };
2154     static const WCHAR szShow[] = { 'S','h','o','w',0 };
2155     static const WCHAR szDisable[] = { 'D','i','s','a','b','l','e',0 };
2156     static const WCHAR szEnable[] = { 'E','n','a','b','l','e',0 };
2157     msi_dialog *dialog = param;
2158     msi_control *control;
2159     LPCWSTR name, action, condition;
2160     UINT r;
2161
2162     name = MSI_RecordGetString( rec, 2 );
2163     action = MSI_RecordGetString( rec, 3 );
2164     condition = MSI_RecordGetString( rec, 4 );
2165     r = MSI_EvaluateConditionW( dialog->package, condition );
2166     control = msi_dialog_find_control( dialog, name );
2167     if( r == MSICONDITION_TRUE && control )
2168     {
2169         TRACE("%s control %s\n", debugstr_w(action), debugstr_w(name));
2170
2171         /* FIXME: case sensitive? */
2172         if(!lstrcmpW(action, szHide))
2173             ShowWindow(control->hwnd, SW_HIDE);
2174         else if(!strcmpW(action, szShow))
2175             ShowWindow(control->hwnd, SW_SHOW);
2176         else if(!strcmpW(action, szDisable))
2177             EnableWindow(control->hwnd, FALSE);
2178         else if(!strcmpW(action, szEnable))
2179             EnableWindow(control->hwnd, TRUE);
2180         else
2181             FIXME("Unhandled action %s\n", debugstr_w(action));
2182     }
2183
2184     return ERROR_SUCCESS;
2185 }
2186
2187 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog )
2188 {
2189     static const WCHAR query[] = {
2190       'S','E','L','E','C','T',' ','*',' ',
2191       'F','R','O','M',' ',
2192         'C','o','n','t','r','o','l','C','o','n','d','i','t','i','o','n',' ',
2193       'W','H','E','R','E',' ',
2194         '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0
2195     };
2196     UINT r;
2197     MSIQUERY *view = NULL;
2198     MSIPACKAGE *package = dialog->package;
2199
2200     TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
2201
2202     /* query the Control table for all the elements of the control */
2203     r = MSI_OpenQuery( package->db, &view, query, dialog->name );
2204     if( r != ERROR_SUCCESS )
2205     {
2206         ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
2207         return ERROR_INVALID_PARAMETER;
2208     }
2209
2210     r = MSI_IterateRecords( view, 0, msi_dialog_set_control_condition, dialog );
2211     msiobj_release( &view->hdr );
2212
2213     return r;
2214 }
2215
2216 UINT msi_dialog_reset( msi_dialog *dialog )
2217 {
2218     /* FIXME: should restore the original values of any properties we changed */
2219     return msi_dialog_evaluate_control_conditions( dialog );
2220 }
2221
2222 /* figure out the height of 10 point MS Sans Serif */
2223 static INT msi_dialog_get_sans_serif_height( HWND hwnd )
2224 {
2225     static const WCHAR szSansSerif[] = {
2226         'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0 };
2227     LOGFONTW lf;
2228     TEXTMETRICW tm;
2229     BOOL r;
2230     LONG height = 0;
2231     HFONT hFont, hOldFont;
2232     HDC hdc;
2233
2234     hdc = GetDC( hwnd );
2235     if (hdc)
2236     {
2237         memset( &lf, 0, sizeof lf );
2238         lf.lfHeight = MulDiv(10, GetDeviceCaps(hdc, LOGPIXELSY), 72);
2239         strcpyW( lf.lfFaceName, szSansSerif );
2240         hFont = CreateFontIndirectW(&lf);
2241         if (hFont)
2242         {
2243             hOldFont = SelectObject( hdc, hFont );
2244             r = GetTextMetricsW( hdc, &tm );
2245             if (r)
2246                 height = tm.tmHeight;
2247             SelectObject( hdc, hOldFont );
2248             DeleteObject( hFont );
2249         }
2250         ReleaseDC( hwnd, hdc );
2251     }
2252     return height;
2253 }
2254
2255 /* fetch the associated record from the Dialog table */
2256 static MSIRECORD *msi_get_dialog_record( msi_dialog *dialog )
2257 {
2258     static const WCHAR query[] = {
2259         'S','E','L','E','C','T',' ','*',' ',
2260         'F','R','O','M',' ','D','i','a','l','o','g',' ',
2261         'W','H','E','R','E',' ',
2262            '`','D','i','a','l','o','g','`',' ','=',' ','\'','%','s','\'',0};
2263     MSIPACKAGE *package = dialog->package;
2264     MSIRECORD *rec = NULL;
2265
2266     TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
2267
2268     rec = MSI_QueryGetRecord( package->db, query, dialog->name );
2269     if( !rec )
2270         ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
2271
2272     return rec;
2273 }
2274
2275 static void msi_dialog_adjust_dialog_pos( msi_dialog *dialog, MSIRECORD *rec, LPRECT pos )
2276 {
2277     static const WCHAR szScreenX[] = {'S','c','r','e','e','n','X',0};
2278     static const WCHAR szScreenY[] = {'S','c','r','e','e','n','Y',0};
2279
2280     UINT xres, yres;
2281     POINT center;
2282     SIZE sz;
2283     LONG style;
2284
2285     center.x = MSI_RecordGetInteger( rec, 2 );
2286     center.y = MSI_RecordGetInteger( rec, 3 );
2287
2288     sz.cx = MSI_RecordGetInteger( rec, 4 );
2289     sz.cy = MSI_RecordGetInteger( rec, 5 );
2290
2291     sz.cx = msi_dialog_scale_unit( dialog, sz.cx );
2292     sz.cy = msi_dialog_scale_unit( dialog, sz.cy );
2293
2294     xres = msi_get_property_int( dialog->package, szScreenX, 0 );
2295     yres = msi_get_property_int( dialog->package, szScreenY, 0 );
2296
2297     center.x = MulDiv( center.x, xres, 100 );
2298     center.y = MulDiv( center.y, yres, 100 );
2299
2300     /* turn the client pos into the window rectangle */
2301     pos->left = center.x - sz.cx/2;
2302     pos->right = pos->left + sz.cx;
2303     pos->top = center.y - sz.cy/2;
2304     pos->bottom = pos->top + sz.cy;
2305
2306     TRACE("%lu %lu %lu %lu\n", pos->left, pos->top, pos->right, pos->bottom);
2307
2308     style = GetWindowLongPtrW( dialog->hwnd, GWL_STYLE );
2309     AdjustWindowRect( pos, style, FALSE );
2310 }
2311
2312 static BOOL msi_control_set_next( msi_control *control, msi_control *next )
2313 {
2314     return SetWindowPos( next->hwnd, control->hwnd, 0, 0, 0, 0,
2315                          SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOREDRAW |
2316                          SWP_NOREPOSITION | SWP_NOSENDCHANGING | SWP_NOSIZE );
2317 }
2318
2319 static UINT msi_dialog_set_tab_order( msi_dialog *dialog )
2320 {
2321     msi_control *control, *tab_next;
2322
2323     LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
2324     {
2325         tab_next = msi_dialog_find_control( dialog, control->tabnext );
2326         if( !tab_next )
2327             continue;
2328         msi_control_set_next( control, tab_next );
2329     }
2330
2331     return ERROR_SUCCESS;
2332 }
2333
2334 static void msi_dialog_set_first_control( msi_dialog* dialog, LPCWSTR name )
2335 {
2336     msi_control *control;
2337
2338     control = msi_dialog_find_control( dialog, name );
2339     if( control )
2340         dialog->hWndFocus = control->hwnd;
2341     else
2342         dialog->hWndFocus = NULL;
2343 }
2344
2345 static LRESULT msi_dialog_oncreate( HWND hwnd, LPCREATESTRUCTW cs )
2346 {
2347     static const WCHAR df[] = {
2348         'D','e','f','a','u','l','t','U','I','F','o','n','t',0 };
2349     static const WCHAR dfv[] = {
2350         'M','S',' ','S','h','e','l','l',' ','D','l','g',0 };
2351     msi_dialog *dialog = (msi_dialog*) cs->lpCreateParams;
2352     MSIRECORD *rec = NULL;
2353     LPWSTR title = NULL;
2354     RECT pos;
2355
2356     TRACE("%p %p\n", dialog, dialog->package);
2357
2358     dialog->hwnd = hwnd;
2359     SetWindowLongPtrW( hwnd, GWLP_USERDATA, (LONG_PTR) dialog );
2360
2361     rec = msi_get_dialog_record( dialog );
2362     if( !rec )
2363     {
2364         TRACE("No record found for dialog %s\n", debugstr_w(dialog->name));
2365         return -1;
2366     }
2367
2368     dialog->scale = msi_dialog_get_sans_serif_height(dialog->hwnd);
2369
2370     msi_dialog_adjust_dialog_pos( dialog, rec, &pos );
2371
2372     dialog->attributes = MSI_RecordGetInteger( rec, 6 );
2373
2374     dialog->default_font = msi_dup_property( dialog->package, df );
2375     if (!dialog->default_font)
2376     {
2377         dialog->default_font = strdupW(dfv);
2378         if (!dialog->default_font) return -1;
2379     }
2380
2381     title = msi_get_deformatted_field( dialog->package, rec, 7 );
2382     SetWindowTextW( hwnd, title );
2383     msi_free( title );
2384
2385     SetWindowPos( hwnd, 0, pos.left, pos.top,
2386                   pos.right - pos.left, pos.bottom - pos.top,
2387                   SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW );
2388
2389     msi_dialog_build_font_list( dialog );
2390     msi_dialog_fill_controls( dialog );
2391     msi_dialog_evaluate_control_conditions( dialog );
2392     msi_dialog_set_tab_order( dialog );
2393     msi_dialog_set_first_control( dialog, MSI_RecordGetString( rec, 8 ) );
2394     msiobj_release( &rec->hdr );
2395
2396     return 0;
2397 }
2398
2399 static UINT msi_dialog_send_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
2400 {
2401     LPWSTR event_fmt = NULL, arg_fmt = NULL;
2402
2403     TRACE("Sending control event %s %s\n", debugstr_w(event), debugstr_w(arg));
2404
2405     deformat_string( dialog->package, event, &event_fmt );
2406     deformat_string( dialog->package, arg, &arg_fmt );
2407
2408     dialog->event_handler( dialog->package, event_fmt, arg_fmt, dialog );
2409
2410     msi_free( event_fmt );
2411     msi_free( arg_fmt );
2412
2413     return ERROR_SUCCESS;
2414 }
2415
2416 static UINT msi_dialog_set_property( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
2417 {
2418     static const WCHAR szNullArg[] = { '{','}',0 };
2419     LPWSTR p, prop, arg_fmt = NULL;
2420     UINT len;
2421
2422     len = strlenW(event);
2423     prop = msi_alloc( len*sizeof(WCHAR));
2424     strcpyW( prop, &event[1] );
2425     p = strchrW( prop, ']' );
2426     if( p && p[1] == 0 )
2427     {
2428         *p = 0;
2429         if( strcmpW( szNullArg, arg ) )
2430             deformat_string( dialog->package, arg, &arg_fmt );
2431         MSI_SetPropertyW( dialog->package, prop, arg_fmt );
2432         msi_free( arg_fmt );
2433     }
2434     else
2435         ERR("Badly formatted property string - what happens?\n");
2436     msi_free( prop );
2437     return ERROR_SUCCESS;
2438 }
2439
2440 static UINT msi_dialog_control_event( MSIRECORD *rec, LPVOID param )
2441 {
2442     msi_dialog *dialog = param;
2443     LPCWSTR condition, event, arg;
2444     UINT r;
2445
2446     condition = MSI_RecordGetString( rec, 5 );
2447     r = MSI_EvaluateConditionW( dialog->package, condition );
2448     if( r == MSICONDITION_TRUE || r == MSICONDITION_NONE )
2449     {
2450         event = MSI_RecordGetString( rec, 3 );
2451         arg = MSI_RecordGetString( rec, 4 );
2452         if( event[0] == '[' )
2453             msi_dialog_set_property( dialog, event, arg );
2454         else
2455             msi_dialog_send_event( dialog, event, arg );
2456     }
2457
2458     return ERROR_SUCCESS;
2459 }
2460
2461 static UINT msi_dialog_button_handler( msi_dialog *dialog,
2462                                        msi_control *control, WPARAM param )
2463 {
2464     static const WCHAR query[] = {
2465       'S','E','L','E','C','T',' ','*',' ',
2466       'F','R','O','M',' ','C','o','n','t','r','o','l','E','v','e','n','t',' ',
2467       'W','H','E','R','E',' ',
2468          '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
2469       'A','N','D',' ',
2470          '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',' ',
2471       'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','i','n','g','`',0
2472     };
2473     MSIQUERY *view = NULL;
2474     UINT r;
2475
2476     if( HIWORD(param) != BN_CLICKED )
2477         return ERROR_SUCCESS;
2478
2479     r = MSI_OpenQuery( dialog->package->db, &view, query,
2480                        dialog->name, control->name );
2481     if( r != ERROR_SUCCESS )
2482     {
2483         ERR("query failed\n");
2484         return 0;
2485     }
2486
2487     r = MSI_IterateRecords( view, 0, msi_dialog_control_event, dialog );
2488     msiobj_release( &view->hdr );
2489
2490     return r;
2491 }
2492
2493 static UINT msi_dialog_get_checkbox_state( msi_dialog *dialog,
2494                 msi_control *control )
2495 {
2496     WCHAR state[2] = { 0 };
2497     DWORD sz = 2;
2498
2499     MSI_GetPropertyW( dialog->package, control->property, state, &sz );
2500     return state[0] ? 1 : 0;
2501 }
2502
2503 static void msi_dialog_set_checkbox_state( msi_dialog *dialog,
2504                 msi_control *control, UINT state )
2505 {
2506     static const WCHAR szState[] = { '1', 0 };
2507     LPCWSTR val;
2508
2509     /* if uncheck then the property is set to NULL */
2510     if (!state)
2511     {
2512         MSI_SetPropertyW( dialog->package, control->property, NULL );
2513         return;
2514     }
2515
2516     /* check for a custom state */
2517     if (control->value && control->value[0])
2518         val = control->value;
2519     else
2520         val = szState;
2521
2522     MSI_SetPropertyW( dialog->package, control->property, val );
2523 }
2524
2525 static void msi_dialog_checkbox_sync_state( msi_dialog *dialog,
2526                 msi_control *control )
2527 {
2528     UINT state;
2529
2530     state = msi_dialog_get_checkbox_state( dialog, control );
2531     SendMessageW( control->hwnd, BM_SETCHECK,
2532                   state ? BST_CHECKED : BST_UNCHECKED, 0 );
2533 }
2534
2535 static UINT msi_dialog_checkbox_handler( msi_dialog *dialog,
2536                 msi_control *control, WPARAM param )
2537 {
2538     UINT state;
2539
2540     if( HIWORD(param) != BN_CLICKED )
2541         return ERROR_SUCCESS;
2542
2543     TRACE("clicked checkbox %s, set %s\n", debugstr_w(control->name),
2544           debugstr_w(control->property));
2545
2546     state = msi_dialog_get_checkbox_state( dialog, control );
2547     state = state ? 0 : 1;
2548     msi_dialog_set_checkbox_state( dialog, control, state );
2549     msi_dialog_checkbox_sync_state( dialog, control );
2550
2551     return msi_dialog_button_handler( dialog, control, param );
2552 }
2553
2554 static UINT msi_dialog_edit_handler( msi_dialog *dialog,
2555                 msi_control *control, WPARAM param )
2556 {
2557     LPWSTR buf;
2558
2559     if( HIWORD(param) != EN_CHANGE )
2560         return ERROR_SUCCESS;
2561
2562     TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
2563           debugstr_w(control->property));
2564
2565     buf = msi_get_window_text( control->hwnd );
2566     MSI_SetPropertyW( dialog->package, control->property, buf );
2567
2568     msi_free( buf );
2569
2570     return ERROR_SUCCESS;
2571 }
2572
2573 static UINT msi_dialog_radiogroup_handler( msi_dialog *dialog,
2574                 msi_control *control, WPARAM param )
2575 {
2576     if( HIWORD(param) != BN_CLICKED )
2577         return ERROR_SUCCESS;
2578
2579     TRACE("clicked radio button %s, set %s\n", debugstr_w(control->name),
2580           debugstr_w(control->property));
2581
2582     MSI_SetPropertyW( dialog->package, control->property, control->name );
2583
2584     return msi_dialog_button_handler( dialog, control, param );
2585 }
2586
2587 static LRESULT msi_dialog_oncommand( msi_dialog *dialog, WPARAM param, HWND hwnd )
2588 {
2589     msi_control *control = NULL;
2590
2591     TRACE("%p %p %08x\n", dialog, hwnd, param);
2592
2593     switch (param)
2594     {
2595     case 1: /* enter */
2596         control = msi_dialog_find_control( dialog, dialog->control_default );
2597         break;
2598     case 2: /* escape */
2599         control = msi_dialog_find_control( dialog, dialog->control_cancel );
2600         break;
2601     default: 
2602         control = msi_dialog_find_control_by_hwnd( dialog, hwnd );
2603     }
2604
2605     if( control )
2606     {
2607         if( control->handler )
2608         {
2609             control->handler( dialog, control, param );
2610             msi_dialog_evaluate_control_conditions( dialog );
2611         }
2612     }
2613     else
2614         ERR("button click from nowhere %p %d %p\n", dialog, param, hwnd);
2615     return 0;
2616 }
2617
2618 static void msi_dialog_setfocus( msi_dialog *dialog )
2619 {
2620     HWND hwnd = dialog->hWndFocus;
2621
2622     hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, TRUE);
2623     hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, FALSE);
2624     SetFocus( hwnd );
2625     dialog->hWndFocus = hwnd;
2626 }
2627
2628 static LRESULT WINAPI MSIDialog_WndProc( HWND hwnd, UINT msg,
2629                 WPARAM wParam, LPARAM lParam )
2630 {
2631     msi_dialog *dialog = (LPVOID) GetWindowLongPtrW( hwnd, GWLP_USERDATA );
2632
2633     TRACE("0x%04x\n", msg);
2634
2635     switch (msg)
2636     {
2637     case WM_CREATE:
2638         return msi_dialog_oncreate( hwnd, (LPCREATESTRUCTW)lParam );
2639
2640     case WM_COMMAND:
2641         return msi_dialog_oncommand( dialog, wParam, (HWND)lParam );
2642
2643     case WM_ACTIVATE:
2644         if( LOWORD(wParam) == WA_INACTIVE )
2645             dialog->hWndFocus = GetFocus();
2646         else
2647             msi_dialog_setfocus( dialog );
2648         return 0;
2649
2650     case WM_SETFOCUS:
2651         msi_dialog_setfocus( dialog );
2652         return 0;
2653
2654     /* bounce back to our subclassed static control */
2655     case WM_CTLCOLORSTATIC:
2656         return SendMessageW( (HWND) lParam, WM_CTLCOLORSTATIC, wParam, lParam );
2657
2658     case WM_DESTROY:
2659         dialog->hwnd = NULL;
2660         return 0;
2661     }
2662     return DefWindowProcW(hwnd, msg, wParam, lParam);
2663 }
2664
2665 static BOOL CALLBACK msi_radioground_child_enum( HWND hWnd, LPARAM lParam )
2666 {
2667     EnableWindow( hWnd, lParam );
2668     return TRUE;
2669 }
2670
2671 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2672 {
2673     WNDPROC oldproc = (WNDPROC) GetPropW(hWnd, szButtonData);
2674     LRESULT r;
2675
2676     TRACE("hWnd %p msg %04x wParam 0x%08x lParam 0x%08lx\n", hWnd, msg, wParam, lParam);
2677
2678     if (msg == WM_COMMAND) /* Forward notifications to dialog */
2679         SendMessageW(GetParent(hWnd), msg, wParam, lParam);
2680
2681     r = CallWindowProcW(oldproc, hWnd, msg, wParam, lParam);
2682
2683     /* make sure the radio buttons show as disabled if the parent is disabled */
2684     if (msg == WM_ENABLE)
2685         EnumChildWindows( hWnd, msi_radioground_child_enum, wParam );
2686
2687     return r;
2688 }
2689
2690 static LRESULT WINAPI MSIHiddenWindowProc( HWND hwnd, UINT msg,
2691                 WPARAM wParam, LPARAM lParam )
2692 {
2693     msi_dialog *dialog = (msi_dialog*) lParam;
2694
2695     TRACE("%d %p\n", msg, dialog);
2696
2697     switch (msg)
2698     {
2699     case WM_MSI_DIALOG_CREATE:
2700         return msi_dialog_run_message_loop( dialog );
2701     case WM_MSI_DIALOG_DESTROY:
2702         msi_dialog_destroy( dialog );
2703         return 0;
2704     }
2705     return DefWindowProcW( hwnd, msg, wParam, lParam );
2706 }
2707
2708 /* functions that interface to other modules within MSI */
2709
2710 msi_dialog *msi_dialog_create( MSIPACKAGE* package, LPCWSTR szDialogName,
2711                                 msi_dialog_event_handler event_handler )
2712 {
2713     MSIRECORD *rec = NULL;
2714     msi_dialog *dialog;
2715
2716     TRACE("%p %s\n", package, debugstr_w(szDialogName));
2717
2718     /* allocate the structure for the dialog to use */
2719     dialog = msi_alloc_zero( sizeof *dialog + sizeof(WCHAR)*strlenW(szDialogName) );
2720     if( !dialog )
2721         return NULL;
2722     strcpyW( dialog->name, szDialogName );
2723     msiobj_addref( &package->hdr );
2724     dialog->package = package;
2725     dialog->event_handler = event_handler;
2726     dialog->finished = 0;
2727     list_init( &dialog->controls );
2728
2729     /* verify that the dialog exists */
2730     rec = msi_get_dialog_record( dialog );
2731     if( !rec )
2732     {
2733         msiobj_release( &package->hdr );
2734         msi_free( dialog );
2735         return NULL;
2736     }
2737     dialog->attributes = MSI_RecordGetInteger( rec, 6 );
2738     dialog->control_default = strdupW( MSI_RecordGetString( rec, 9 ) );
2739     dialog->control_cancel = strdupW( MSI_RecordGetString( rec, 10 ) );
2740     msiobj_release( &rec->hdr );
2741
2742     return dialog;
2743 }
2744
2745 static void msi_process_pending_messages( HWND hdlg )
2746 {
2747     MSG msg;
2748
2749     while( PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ) )
2750     {
2751         if( hdlg && IsDialogMessageW( hdlg, &msg ))
2752             continue;
2753         TranslateMessage( &msg );
2754         DispatchMessageW( &msg );
2755     }
2756 }
2757
2758 void msi_dialog_end_dialog( msi_dialog *dialog )
2759 {
2760     TRACE("%p\n", dialog);
2761     dialog->finished = 1;
2762     PostMessageW(dialog->hwnd, WM_NULL, 0, 0);
2763 }
2764
2765 void msi_dialog_check_messages( HANDLE handle )
2766 {
2767     DWORD r;
2768
2769     /* in threads other than the UI thread, block */
2770     if( uiThreadId != GetCurrentThreadId() )
2771     {
2772         if( handle )
2773             WaitForSingleObject( handle, INFINITE );
2774         return;
2775     }
2776
2777     /* there's two choices for the UI thread */
2778     while (1)
2779     {
2780         msi_process_pending_messages( NULL );
2781
2782         if( !handle )
2783             break;
2784
2785         /*
2786          * block here until somebody creates a new dialog or
2787          * the handle we're waiting on becomes ready
2788          */
2789         r = MsgWaitForMultipleObjects( 1, &handle, 0, INFINITE, QS_ALLINPUT );
2790         if( r == WAIT_OBJECT_0 )
2791             break;
2792     }
2793 }
2794
2795 UINT msi_dialog_run_message_loop( msi_dialog *dialog )
2796 {
2797     DWORD style;
2798     HWND hwnd;
2799
2800     if( uiThreadId != GetCurrentThreadId() )
2801         return SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_CREATE, 0, (LPARAM) dialog );
2802
2803     /* create the dialog window, don't show it yet */
2804     style = WS_OVERLAPPED;
2805     if( dialog->attributes & msidbDialogAttributesVisible )
2806         style |= WS_VISIBLE;
2807
2808     hwnd = CreateWindowW( szMsiDialogClass, dialog->name, style,
2809                      CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
2810                      NULL, NULL, NULL, dialog );
2811     if( !hwnd )
2812     {
2813         ERR("Failed to create dialog %s\n", debugstr_w( dialog->name ));
2814         return ERROR_FUNCTION_FAILED;
2815     }
2816
2817     ShowWindow( hwnd, SW_SHOW );
2818     /* UpdateWindow( hwnd ); - and causes the transparent static controls not to paint */
2819
2820     if( dialog->attributes & msidbDialogAttributesModal )
2821     {
2822         while( !dialog->finished )
2823         {
2824             MsgWaitForMultipleObjects( 0, NULL, 0, INFINITE, QS_ALLINPUT );
2825             msi_process_pending_messages( dialog->hwnd );
2826         }
2827     }
2828     else
2829         return ERROR_IO_PENDING;
2830
2831     return ERROR_SUCCESS;
2832 }
2833
2834 void msi_dialog_do_preview( msi_dialog *dialog )
2835 {
2836     TRACE("\n");
2837     dialog->attributes |= msidbDialogAttributesVisible;
2838     dialog->attributes &= ~msidbDialogAttributesModal;
2839     msi_dialog_run_message_loop( dialog );
2840 }
2841
2842 void msi_dialog_destroy( msi_dialog *dialog )
2843 {
2844     if( uiThreadId != GetCurrentThreadId() )
2845     {
2846         SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_DESTROY, 0, (LPARAM) dialog );
2847         return;
2848     }
2849
2850     if( dialog->hwnd )
2851         ShowWindow( dialog->hwnd, SW_HIDE );
2852     
2853     if( dialog->hwnd )
2854         DestroyWindow( dialog->hwnd );
2855
2856     /* destroy the list of controls */
2857     while( !list_empty( &dialog->controls ) )
2858     {
2859         msi_control *t = LIST_ENTRY( list_head( &dialog->controls ),
2860                                      msi_control, entry );
2861         list_remove( &t->entry );
2862         /* leave dialog->hwnd - destroying parent destroys child windows */
2863         msi_free( t->property );
2864         msi_free( t->value );
2865         if( t->hBitmap )
2866             DeleteObject( t->hBitmap );
2867         if( t->hIcon )
2868             DestroyIcon( t->hIcon );
2869         msi_free( t->tabnext );
2870         msi_free( t );
2871         if (t->hDll)
2872             FreeLibrary( t->hDll );
2873     }
2874
2875     /* destroy the list of fonts */
2876     while( dialog->font_list )
2877     {
2878         msi_font *t = dialog->font_list;
2879         dialog->font_list = t->next;
2880         DeleteObject( t->hfont );
2881         msi_free( t );
2882     }
2883     msi_free( dialog->default_font );
2884
2885     msi_free( dialog->control_default );
2886     msi_free( dialog->control_cancel );
2887     msiobj_release( &dialog->package->hdr );
2888     dialog->package = NULL;
2889     msi_free( dialog );
2890 }
2891
2892 BOOL msi_dialog_register_class( void )
2893 {
2894     WNDCLASSW cls;
2895
2896     ZeroMemory( &cls, sizeof cls );
2897     cls.lpfnWndProc   = MSIDialog_WndProc;
2898     cls.hInstance     = NULL;
2899     cls.hIcon         = LoadIconW(0, (LPWSTR)IDI_APPLICATION);
2900     cls.hCursor       = LoadCursorW(0, (LPWSTR)IDC_ARROW);
2901     cls.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
2902     cls.lpszMenuName  = NULL;
2903     cls.lpszClassName = szMsiDialogClass;
2904
2905     if( !RegisterClassW( &cls ) )
2906         return FALSE;
2907
2908     cls.lpfnWndProc   = MSIHiddenWindowProc;
2909     cls.lpszClassName = szMsiHiddenWindow;
2910
2911     if( !RegisterClassW( &cls ) )
2912         return FALSE;
2913
2914     uiThreadId = GetCurrentThreadId();
2915
2916     hMsiHiddenWindow = CreateWindowW( szMsiHiddenWindow, NULL, WS_OVERLAPPED,
2917                                    0, 0, 100, 100, NULL, NULL, NULL, NULL );
2918     if( !hMsiHiddenWindow )
2919         return FALSE;
2920
2921     return TRUE;
2922 }
2923
2924 void msi_dialog_unregister_class( void )
2925 {
2926     DestroyWindow( hMsiHiddenWindow );
2927     hMsiHiddenWindow = NULL;
2928     UnregisterClassW( szMsiDialogClass, NULL );
2929     UnregisterClassW( szMsiHiddenWindow, NULL );
2930     uiThreadId = 0;
2931 }