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