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