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