Read the MaskEdit template from the correct db field.
[wine] / dlls / msi / dialog.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2005 Mike McCormack for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #define COBJMACROS
22
23 #include <stdarg.h>
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "winnls.h"
29 #include "wingdi.h"
30 #include "msi.h"
31 #include "msipriv.h"
32 #include "msidefs.h"
33 #include "ocidl.h"
34 #include "olectl.h"
35 #include "richedit.h"
36
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
39
40 #include "action.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(msi);
43
44
45 struct msi_control_tag;
46 typedef struct msi_control_tag msi_control;
47 typedef UINT (*msi_handler)( msi_dialog *, msi_control *, WPARAM );
48
49 struct msi_control_tag
50 {
51     struct list entry;
52     HWND hwnd;
53     msi_handler handler;
54     LPWSTR property;
55     LPWSTR value;
56     HBITMAP hBitmap;
57     HICON hIcon;
58     LPWSTR tabnext;
59     HMODULE hDll;
60     WCHAR name[1];
61 };
62
63 typedef struct msi_font_tag
64 {
65     struct msi_font_tag *next;
66     HFONT hfont;
67     WCHAR name[1];
68 } msi_font;
69
70 struct msi_dialog_tag
71 {
72     MSIPACKAGE *package;
73     msi_dialog_event_handler event_handler;
74     BOOL finished;
75     INT scale;
76     DWORD attributes;
77     HWND hwnd;
78     LPWSTR default_font;
79     msi_font *font_list;
80     struct list controls;
81     HWND hWndFocus;
82     WCHAR name[1];
83 };
84
85 typedef UINT (*msi_dialog_control_func)( msi_dialog *dialog, MSIRECORD *rec );
86 struct control_handler 
87 {
88     LPCWSTR control_type;
89     msi_dialog_control_func func;
90 };
91
92 typedef struct
93 {
94     msi_dialog* dialog;
95     msi_control *parent;
96     DWORD       attributes;
97 } radio_button_group_descr;
98
99 const WCHAR szMsiDialogClass[] = {
100     'M','s','i','D','i','a','l','o','g','C','l','o','s','e','C','l','a','s','s',0
101 };
102 const WCHAR szMsiHiddenWindow[] = {
103     'M','s','i','H','i','d','d','e','n','W','i','n','d','o','w',0 };
104 const static WCHAR szStatic[] = { 'S','t','a','t','i','c',0 };
105 const static WCHAR szButton[] = { 'B','U','T','T','O','N', 0 };
106 const static WCHAR szButtonData[] = { 'M','S','I','D','A','T','A',0 };
107 static const WCHAR szText[] = { 'T','e','x','t',0 };
108 static const WCHAR szPushButton[] = { 'P','u','s','h','B','u','t','t','o','n',0 };
109 static const WCHAR szLine[] = { 'L','i','n','e',0 };
110 static const WCHAR szBitmap[] = { 'B','i','t','m','a','p',0 };
111 static const WCHAR szCheckBox[] = { 'C','h','e','c','k','B','o','x',0 };
112 static const WCHAR szScrollableText[] = {
113     'S','c','r','o','l','l','a','b','l','e','T','e','x','t',0 };
114 static const WCHAR szComboBox[] = { 'C','o','m','b','o','B','o','x',0 };
115 static const WCHAR szEdit[] = { 'E','d','i','t',0 };
116 static const WCHAR szMaskedEdit[] = { 'M','a','s','k','e','d','E','d','i','t',0 };
117 static const WCHAR szPathEdit[] = { 'P','a','t','h','E','d','i','t',0 };
118 static const WCHAR szRadioButtonGroup[] = { 
119     'R','a','d','i','o','B','u','t','t','o','n','G','r','o','u','p',0 };
120 static const WCHAR szIcon[] = { 'I','c','o','n',0 };
121
122 static UINT msi_dialog_checkbox_handler( msi_dialog *, msi_control *, WPARAM );
123 static void msi_dialog_checkbox_sync_state( msi_dialog *, msi_control * );
124 static UINT msi_dialog_button_handler( msi_dialog *, msi_control *, WPARAM );
125 static UINT msi_dialog_edit_handler( msi_dialog *, msi_control *, WPARAM );
126 static UINT msi_dialog_radiogroup_handler( msi_dialog *, msi_control *, WPARAM param );
127 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog );
128 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
129
130
131 /* dialog sequencing */
132
133 #define WM_MSI_DIALOG_CREATE  (WM_USER+0x100)
134 #define WM_MSI_DIALOG_DESTROY (WM_USER+0x101)
135
136 static DWORD uiThreadId;
137 static HWND hMsiHiddenWindow;
138
139 static INT msi_dialog_scale_unit( msi_dialog *dialog, INT val )
140 {
141     return (dialog->scale * val + 5) / 10;
142 }
143
144 static msi_control *msi_dialog_find_control( msi_dialog *dialog, LPCWSTR name )
145 {
146     msi_control *control;
147
148     if( !name )
149         return NULL;
150     LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
151         if( !strcmpW( control->name, name ) ) /* FIXME: case sensitive? */
152             break;
153     return control;
154 }
155
156 static msi_control *msi_dialog_find_control_by_hwnd( msi_dialog *dialog, HWND hwnd )
157 {
158     msi_control *control;
159
160     LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
161         if( hwnd == control->hwnd )
162             break;
163     return control;
164 }
165
166 static LPWSTR msi_get_deformatted_field( MSIPACKAGE *package, MSIRECORD *rec, int field )
167 {
168     LPCWSTR str = MSI_RecordGetString( rec, field );
169     LPWSTR ret = NULL;
170
171     if (str)
172         deformat_string( package, str, &ret );
173     return ret;
174 }
175
176 /*
177  * msi_dialog_get_style
178  *
179  * Extract the {\style} string from the front of the text to display and
180  *  update the pointer.
181  */
182 static LPWSTR msi_dialog_get_style( LPWSTR *text )
183 {
184     LPWSTR ret = NULL;
185     LPWSTR p = *text, q;
186     DWORD len;
187
188     if( !*text )
189         return ret;
190     if( *p++ != '{' )
191         return ret;
192     q = strchrW( p, '}' );
193     if( !q )
194         return ret;
195     *text = ++q;
196     if( *p++ != '\\' )
197         return ret;
198     len = q - p;
199     
200     ret = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
201     if( !ret )
202         return ret;
203     memcpy( ret, p, len*sizeof(WCHAR) );
204     ret[len-1] = 0;
205     return ret;
206 }
207
208 static UINT msi_dialog_add_font( MSIRECORD *rec, LPVOID param )
209 {
210     msi_dialog *dialog = param;
211     msi_font *font;
212     LPCWSTR face, name;
213     LOGFONTW lf;
214     INT style;
215     HDC hdc;
216
217     /* create a font and add it to the list */
218     name = MSI_RecordGetString( rec, 1 );
219     font = HeapAlloc( GetProcessHeap(), 0,
220                       sizeof *font + strlenW( name )*sizeof (WCHAR) );
221     strcpyW( font->name, name );
222     font->next = dialog->font_list;
223     dialog->font_list = font;
224
225     memset( &lf, 0, sizeof lf );
226     face = MSI_RecordGetString( rec, 2 );
227     lf.lfHeight = MSI_RecordGetInteger( rec, 3 );
228     style = MSI_RecordGetInteger( rec, 5 );
229     if( style & msidbTextStyleStyleBitsBold )
230         lf.lfWeight = FW_BOLD;
231     if( style & msidbTextStyleStyleBitsItalic )
232         lf.lfItalic = TRUE;
233     if( style & msidbTextStyleStyleBitsUnderline )
234         lf.lfUnderline = TRUE;
235     if( style & msidbTextStyleStyleBitsStrike )
236         lf.lfStrikeOut = TRUE;
237     lstrcpynW( lf.lfFaceName, face, LF_FACESIZE );
238
239     /* adjust the height */
240     hdc = GetDC( dialog->hwnd );
241     if (hdc)
242     {
243         lf.lfHeight = -MulDiv(lf.lfHeight, GetDeviceCaps(hdc, LOGPIXELSY), 72);
244         ReleaseDC( dialog->hwnd, hdc );
245     }
246
247     font->hfont = CreateFontIndirectW( &lf );
248
249     TRACE("Adding font style %s\n", debugstr_w(font->name) );
250
251     return ERROR_SUCCESS;
252 }
253
254 static msi_font *msi_dialog_find_font( msi_dialog *dialog, LPCWSTR name )
255 {
256     msi_font *font;
257
258     for( font = dialog->font_list; font; font = font->next )
259         if( !strcmpW( font->name, name ) )  /* FIXME: case sensitive? */
260             break;
261
262     return font;
263 }
264
265 static UINT msi_dialog_set_font( msi_dialog *dialog, HWND hwnd, LPCWSTR name )
266 {
267     msi_font *font;
268
269     font = msi_dialog_find_font( dialog, name );
270     if( font )
271         SendMessageW( hwnd, WM_SETFONT, (WPARAM) font->hfont, TRUE );
272     else
273         ERR("No font entry for %s\n", debugstr_w(name));
274     return ERROR_SUCCESS;
275 }
276
277 static UINT msi_dialog_build_font_list( msi_dialog *dialog )
278 {
279     static const WCHAR query[] = {
280       'S','E','L','E','C','T',' ','*',' ',
281       'F','R','O','M',' ','`','T','e','x','t','S','t','y','l','e','`',' ',0
282     };
283     UINT r;
284     MSIQUERY *view = NULL;
285
286     TRACE("dialog %p\n", dialog );
287
288     r = MSI_OpenQuery( dialog->package->db, &view, query );
289     if( r != ERROR_SUCCESS )
290         return r;
291
292     r = MSI_IterateRecords( view, NULL, msi_dialog_add_font, dialog );
293     msiobj_release( &view->hdr );
294
295     return r;
296 }
297
298 static msi_control *msi_dialog_create_window( msi_dialog *dialog,
299                 MSIRECORD *rec, LPCWSTR szCls, LPCWSTR name, LPCWSTR text,
300                 DWORD style, HWND parent )
301 {
302     DWORD x, y, width, height;
303     LPWSTR font = NULL, title = NULL;
304     msi_control *control;
305
306     style |= WS_CHILD;
307
308     control = HeapAlloc( GetProcessHeap(), 0,
309                          sizeof *control + strlenW(name)*sizeof(WCHAR) );
310     strcpyW( control->name, name );
311     list_add_head( &dialog->controls, &control->entry );
312     control->handler = NULL;
313     control->property = NULL;
314     control->value = NULL;
315     control->hBitmap = NULL;
316     control->hIcon = NULL;
317     control->hDll = NULL;
318     control->tabnext = strdupW( MSI_RecordGetString( rec, 11) );
319
320     x = MSI_RecordGetInteger( rec, 4 );
321     y = MSI_RecordGetInteger( rec, 5 );
322     width = MSI_RecordGetInteger( rec, 6 );
323     height = MSI_RecordGetInteger( rec, 7 );
324
325     x = msi_dialog_scale_unit( dialog, x );
326     y = msi_dialog_scale_unit( dialog, y );
327     width = msi_dialog_scale_unit( dialog, width );
328     height = msi_dialog_scale_unit( dialog, height );
329
330     if( text )
331     {
332         deformat_string( dialog->package, text, &title );
333         font = msi_dialog_get_style( &title );
334     }
335
336     control->hwnd = CreateWindowW( szCls, title, style,
337                           x, y, width, height, parent, NULL, NULL, NULL );
338
339     TRACE("Dialog %s control %s hwnd %p\n",
340            debugstr_w(dialog->name), debugstr_w(text), control->hwnd );
341
342     msi_dialog_set_font( dialog, control->hwnd,
343                          font ? font : dialog->default_font );
344
345     HeapFree( GetProcessHeap(), 0, font );
346     HeapFree( GetProcessHeap(), 0, title );
347
348     return control;
349 }
350
351 static MSIRECORD *msi_get_binary_record( MSIDATABASE *db, LPCWSTR name )
352 {
353     const static WCHAR query[] = {
354         's','e','l','e','c','t',' ','*',' ',
355         'f','r','o','m',' ','B','i','n','a','r','y',' ',
356         'w','h','e','r','e',' ',
357             '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0
358     };
359
360     return MSI_QueryGetRecord( db, query, name );
361 }
362
363 static LPWSTR msi_create_tmp_path(void)
364 {
365     WCHAR tmp[MAX_PATH];
366     LPWSTR path = NULL;
367     static const WCHAR prefix[] = { 'm','s','i',0 };
368     DWORD len, r;
369
370     r = GetTempPathW( MAX_PATH, tmp );
371     if( !r )
372         return path;
373     len = lstrlenW( tmp ) + 20;
374     path = HeapAlloc( GetProcessHeap(), 0, len * sizeof (WCHAR) );
375     if( path )
376     {
377         r = GetTempFileNameW( tmp, prefix, 0, path );
378         if (!r)
379         {
380             HeapFree( GetProcessHeap(), 0, path );
381             path = NULL;
382         }
383     }
384     return path;
385 }
386
387
388 static HANDLE msi_load_image( MSIDATABASE *db, LPCWSTR name, UINT type,
389                               UINT cx, UINT cy, UINT flags )
390 {
391     MSIRECORD *rec = NULL;
392     HANDLE himage = NULL;
393     LPWSTR tmp;
394     UINT r;
395
396     TRACE("%p %s %u %u %08x\n", db, debugstr_w(name), cx, cy, flags);
397
398     tmp = msi_create_tmp_path();
399     if( !tmp )
400         return himage;
401
402     rec = msi_get_binary_record( db, name );
403     if( rec )
404     {
405         r = MSI_RecordStreamToFile( rec, 2, tmp );
406         if( r == ERROR_SUCCESS )
407         {
408             himage = LoadImageW( 0, tmp, type, cx, cy, flags );
409             DeleteFileW( tmp );
410         }
411         msiobj_release( &rec->hdr );
412     }
413
414     HeapFree( GetProcessHeap(), 0, tmp );
415     return himage;
416 }
417
418 static HICON msi_load_icon( MSIDATABASE *db, LPCWSTR text, UINT attributes )
419 {
420     DWORD cx = 0, cy = 0, flags;
421
422     flags = LR_LOADFROMFILE | LR_DEFAULTSIZE;
423     if( attributes & msidbControlAttributesFixedSize )
424     {
425         flags &= ~LR_DEFAULTSIZE;
426         if( attributes & msidbControlAttributesIconSize16 )
427         {
428             cx += 16;
429             cy += 16;
430         }
431         if( attributes & msidbControlAttributesIconSize32 )
432         {
433             cx += 32;
434             cy += 32;
435         }
436         /* msidbControlAttributesIconSize48 handled by above logic */
437     }
438     return msi_load_image( db, text, IMAGE_ICON, cx, cy, flags );
439 }
440
441
442 /* called from the Control Event subscription code */
443 void msi_dialog_handle_event( msi_dialog* dialog, LPCWSTR control, 
444                               LPCWSTR attribute, MSIRECORD *rec )
445 {
446     msi_control* ctrl;
447     LPCWSTR text;
448
449     ctrl = msi_dialog_find_control( dialog, control );
450     if (!ctrl)
451         return;
452     if( lstrcmpW(attribute, szText) )
453         return;
454     text = MSI_RecordGetString( rec , 1 );
455     SetWindowTextW( ctrl->hwnd, text );
456     msi_dialog_check_messages( NULL );
457 }
458
459 static void msi_dialog_map_events(msi_dialog* dialog, LPCWSTR control)
460 {
461     static WCHAR Query[] = {
462         'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
463          '`','E','v','e','n','t','M','a','p','p','i','n','g','`',' ',
464         'W','H','E','R','E',' ',
465          '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
466         'A','N','D',' ',
467          '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',0
468     };
469     MSIRECORD *row;
470     LPCWSTR event, attribute;
471
472     row = MSI_QueryGetRecord( dialog->package->db, Query, dialog->name, control );
473     if (!row)
474         return;
475
476     event = MSI_RecordGetString( row, 3 );
477     attribute = MSI_RecordGetString( row, 4 );
478     ControlEvent_SubscribeToEvent( dialog->package, event, control, attribute );
479     msiobj_release( &row->hdr );
480 }
481
482 /* everything except radio buttons */
483 static msi_control *msi_dialog_add_control( msi_dialog *dialog,
484                 MSIRECORD *rec, LPCWSTR szCls, DWORD style )
485 {
486     DWORD attributes;
487     LPCWSTR text, name;
488
489     name = MSI_RecordGetString( rec, 2 );
490     attributes = MSI_RecordGetInteger( rec, 8 );
491     text = MSI_RecordGetString( rec, 10 );
492     if( attributes & msidbControlAttributesVisible )
493         style |= WS_VISIBLE;
494     if( ~attributes & msidbControlAttributesEnabled )
495         style |= WS_DISABLED;
496
497     msi_dialog_map_events(dialog, name);
498
499     return msi_dialog_create_window( dialog, rec, szCls, name, text,
500                                      style, dialog->hwnd );
501 }
502
503 struct msi_text_info
504 {
505     WNDPROC oldproc;
506     DWORD attributes;
507 };
508
509 /*
510  * we don't erase our own background,
511  * so we have to make sure that the parent window redraws first
512  */
513 static void msi_text_on_settext( HWND hWnd )
514 {
515     HWND hParent;
516     RECT rc;
517
518     hParent = GetParent( hWnd );
519     GetWindowRect( hWnd, &rc );
520     MapWindowPoints( NULL, hParent, (LPPOINT) &rc, 2 );
521     InvalidateRect( hParent, &rc, TRUE );
522 }
523
524 static LRESULT WINAPI
525 MSIText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
526 {
527     struct msi_text_info *info;
528     LRESULT r = 0;
529
530     TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
531
532     info = GetPropW(hWnd, szButtonData);
533
534     if( msg == WM_CTLCOLORSTATIC &&
535        ( info->attributes & msidbControlAttributesTransparent ) )
536     {
537         SetBkMode( (HDC)wParam, TRANSPARENT );
538         return (LRESULT) GetStockObject(NULL_BRUSH);
539     }
540
541     r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
542
543     switch( msg )
544     {
545     case WM_SETTEXT:
546         msi_text_on_settext( hWnd );
547         break;
548     case WM_NCDESTROY:
549         HeapFree( GetProcessHeap(), 0, info );
550         RemovePropW( hWnd, szButtonData );
551         break;
552     }
553
554     return r;
555 }
556
557 static UINT msi_dialog_text_control( msi_dialog *dialog, MSIRECORD *rec )
558 {
559     msi_control *control;
560     struct msi_text_info *info;
561
562     TRACE("%p %p\n", dialog, rec);
563
564     control = msi_dialog_add_control( dialog, rec, szStatic, SS_LEFT | WS_GROUP );
565     if( !control )
566         return ERROR_FUNCTION_FAILED;
567
568     info = HeapAlloc( GetProcessHeap(), 0, sizeof *info );
569     if( !info )
570         return ERROR_SUCCESS;
571
572     info->attributes = MSI_RecordGetInteger( rec, 8 );
573     if( info->attributes & msidbControlAttributesTransparent )
574         SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT );
575
576     info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
577                                           (LONG_PTR)MSIText_WndProc );
578     SetPropW( control->hwnd, szButtonData, info );
579
580     return ERROR_SUCCESS;
581 }
582
583 static UINT msi_dialog_button_control( msi_dialog *dialog, MSIRECORD *rec )
584 {
585     msi_control *control;
586     UINT attributes, style;
587     LPWSTR text;
588
589     TRACE("%p %p\n", dialog, rec);
590
591     style = WS_TABSTOP;
592     attributes = MSI_RecordGetInteger( rec, 8 );
593     if( attributes & msidbControlAttributesIcon )
594         style |= BS_ICON;
595
596     control = msi_dialog_add_control( dialog, rec, szButton, style );
597     if( !control )
598         return ERROR_FUNCTION_FAILED;
599
600     control->handler = msi_dialog_button_handler;
601
602     /* set the icon */
603     text = msi_get_deformatted_field( dialog->package, rec, 10 );
604     control->hIcon = msi_load_icon( dialog->package->db, text, attributes );
605     if( attributes & msidbControlAttributesIcon )
606         SendMessageW( control->hwnd, BM_SETIMAGE, IMAGE_ICON, (LPARAM) control->hIcon );
607     HeapFree( GetProcessHeap(), 0, text );
608
609     return ERROR_SUCCESS;
610 }
611
612 static LPWSTR msi_get_checkbox_value( msi_dialog *dialog, LPCWSTR prop )
613 {
614     const static WCHAR query[] = {
615         'S','E','L','E','C','T',' ','*',' ',
616         'F','R','O','M',' ','`','C','h','e','c','k','B','o','x',' ','`',
617         'W','H','E','R','E',' ',
618         '`','P','r','o','p','e','r','t','y','`',' ','=',' ',
619         '\'','%','s','\'',0
620     };
621     MSIRECORD *rec = NULL;
622     LPCWSTR val = NULL;
623     LPWSTR ret = NULL;
624
625     /* find if there is a value associated with the checkbox */
626     rec = MSI_QueryGetRecord( dialog->package->db, query, prop );
627     if (!rec)
628         return ret;
629
630     val = MSI_RecordGetString( rec, 2 );
631     if (val)
632     {
633         deformat_string( dialog->package, val, &ret );
634         if( ret && !ret[0] )
635         {
636             HeapFree( GetProcessHeap(), 0, ret );
637             ret = NULL;
638         }
639     }
640     msiobj_release( &rec->hdr );
641     if (ret)
642         return ret;
643
644     ret = msi_dup_property( dialog->package, prop );
645     if( ret && !ret[0] )
646     {
647         HeapFree( GetProcessHeap(), 0, ret );
648         ret = NULL;
649     }
650
651     return ret;
652 }
653
654 static UINT msi_dialog_checkbox_control( msi_dialog *dialog, MSIRECORD *rec )
655 {
656     msi_control *control;
657     LPCWSTR prop;
658
659     TRACE("%p %p\n", dialog, rec);
660
661     control = msi_dialog_add_control( dialog, rec, szButton,
662                                 BS_CHECKBOX | BS_MULTILINE | WS_TABSTOP );
663     control->handler = msi_dialog_checkbox_handler;
664     prop = MSI_RecordGetString( rec, 9 );
665     if( prop )
666     {
667         control->property = strdupW( prop );
668         control->value = msi_get_checkbox_value( dialog, prop );
669         TRACE("control %s value %s\n", debugstr_w(control->property),
670               debugstr_w(control->value));
671     }
672     msi_dialog_checkbox_sync_state( dialog, control );
673
674     return ERROR_SUCCESS;
675 }
676
677 static UINT msi_dialog_line_control( msi_dialog *dialog, MSIRECORD *rec )
678 {
679     TRACE("%p %p\n", dialog, rec);
680
681     msi_dialog_add_control( dialog, rec, szStatic, SS_ETCHEDHORZ | SS_SUNKEN );
682     return ERROR_SUCCESS;
683 }
684
685 struct msi_streamin_info
686 {
687     LPSTR string;
688     DWORD offset;
689     DWORD length;
690 };
691
692 static DWORD CALLBACK
693 msi_richedit_stream_in( DWORD_PTR arg, LPBYTE buffer, LONG count, LONG *pcb )
694 {
695     struct msi_streamin_info *info = (struct msi_streamin_info*) arg;
696
697     if( (count + info->offset) > info->length )
698         count = info->length - info->offset;
699     memcpy( buffer, &info->string[ info->offset ], count );
700     *pcb = count;
701     info->offset += count;
702
703     TRACE("%ld/%ld\n", info->offset, info->length);
704
705     return 0;
706 }
707
708 static UINT msi_dialog_scrolltext_control( msi_dialog *dialog, MSIRECORD *rec )
709 {
710     const static WCHAR szRichEdit20W[] = {
711         'R','i','c','h','E','d','i','t','2','0','W',0
712     };
713     struct msi_streamin_info info;
714     msi_control *control;
715     LPCWSTR text;
716     EDITSTREAM es;
717     DWORD style;
718     HMODULE hRichedit;
719
720     hRichedit = LoadLibraryA("riched20");
721
722     style = WS_BORDER | ES_MULTILINE | WS_VSCROLL |
723             ES_READONLY | ES_AUTOVSCROLL | WS_TABSTOP;
724     control = msi_dialog_add_control( dialog, rec, szRichEdit20W, style );
725     if (!control)
726         return ERROR_FUNCTION_FAILED;
727
728     control->hDll = hRichedit;
729
730     text = MSI_RecordGetString( rec, 10 );
731     info.string = strdupWtoA( text );
732     info.offset = 0;
733     info.length = lstrlenA( info.string ) + 1;
734
735     es.dwCookie = (DWORD_PTR) &info;
736     es.dwError = 0;
737     es.pfnCallback = msi_richedit_stream_in;
738
739     SendMessageW( control->hwnd, EM_STREAMIN, SF_RTF, (LPARAM) &es );
740
741     HeapFree( GetProcessHeap(), 0, info.string );
742
743     return ERROR_SUCCESS;
744 }
745
746 static UINT msi_dialog_bitmap_control( msi_dialog *dialog, MSIRECORD *rec )
747 {
748     UINT cx, cy, flags, style, attributes;
749     msi_control *control;
750     LPWSTR text;
751
752     flags = LR_LOADFROMFILE;
753     style = SS_BITMAP | SS_LEFT | WS_GROUP;
754
755     attributes = MSI_RecordGetInteger( rec, 8 );
756     if( attributes & msidbControlAttributesFixedSize )
757     {
758         flags |= LR_DEFAULTSIZE;
759         style |= SS_CENTERIMAGE;
760     }
761
762     control = msi_dialog_add_control( dialog, rec, szStatic, style );
763     cx = MSI_RecordGetInteger( rec, 6 );
764     cy = MSI_RecordGetInteger( rec, 7 );
765     cx = msi_dialog_scale_unit( dialog, cx );
766     cy = msi_dialog_scale_unit( dialog, cy );
767
768     text = msi_get_deformatted_field( dialog->package, rec, 10 );
769     control->hBitmap = msi_load_image( dialog->package->db, text,
770                                        IMAGE_BITMAP, cx, cy, flags );
771     if( control->hBitmap )
772         SendMessageW( control->hwnd, STM_SETIMAGE,
773                       IMAGE_BITMAP, (LPARAM) control->hBitmap );
774     else
775         ERR("Failed to load bitmap %s\n", debugstr_w(text));
776
777     HeapFree( GetProcessHeap(), 0, text );
778     
779     return ERROR_SUCCESS;
780 }
781
782 static UINT msi_dialog_icon_control( msi_dialog *dialog, MSIRECORD *rec )
783 {
784     msi_control *control;
785     DWORD attributes;
786     LPWSTR text;
787
788     TRACE("\n");
789
790     control = msi_dialog_add_control( dialog, rec, szStatic,
791                             SS_ICON | SS_CENTERIMAGE | WS_GROUP );
792             
793     attributes = MSI_RecordGetInteger( rec, 8 );
794     text = msi_get_deformatted_field( dialog->package, rec, 10 );
795     control->hIcon = msi_load_icon( dialog->package->db, text, attributes );
796     if( control->hIcon )
797         SendMessageW( control->hwnd, STM_SETICON, (WPARAM) control->hIcon, 0 );
798     else
799         ERR("Failed to load bitmap %s\n", debugstr_w(text));
800     HeapFree( GetProcessHeap(), 0, text );
801     return ERROR_SUCCESS;
802 }
803
804 static UINT msi_dialog_combo_control( msi_dialog *dialog, MSIRECORD *rec )
805 {
806     static const WCHAR szCombo[] = { 'C','O','M','B','O','B','O','X',0 };
807
808     msi_dialog_add_control( dialog, rec, szCombo,
809                             SS_BITMAP | SS_LEFT | SS_CENTERIMAGE );
810     return ERROR_SUCCESS;
811 }
812
813 static UINT msi_dialog_edit_control( msi_dialog *dialog, MSIRECORD *rec )
814 {
815     msi_control *control;
816     LPCWSTR prop;
817     LPWSTR val;
818
819     control = msi_dialog_add_control( dialog, rec, szEdit,
820                                       WS_BORDER | WS_TABSTOP );
821     control->handler = msi_dialog_edit_handler;
822     prop = MSI_RecordGetString( rec, 9 );
823     if( prop )
824         control->property = strdupW( prop );
825     val = msi_dup_property( dialog->package, control->property );
826     SetWindowTextW( control->hwnd, val );
827     HeapFree( GetProcessHeap(), 0, val );
828     return ERROR_SUCCESS;
829 }
830
831 /******************** Masked Edit ********************************************/
832
833 #define MASK_MAX_GROUPS 10
834
835 struct msi_mask_group
836 {
837     UINT len;
838     UINT ofs;
839     WCHAR type;
840     HWND hwnd;
841 };
842
843 struct msi_maskedit_info
844 {
845     msi_dialog *dialog;
846     WNDPROC oldproc;
847     HWND hwnd;
848     LPWSTR prop;
849     UINT num_chars;
850     UINT num_groups;
851     struct msi_mask_group group[MASK_MAX_GROUPS];
852 };
853
854 static void msi_mask_control_change( struct msi_maskedit_info *info )
855 {
856     LPWSTR val;
857     UINT i, n, r;
858
859     val = HeapAlloc( GetProcessHeap(), 0, (info->num_chars+1)*sizeof(WCHAR) );
860     for( i=0, n=0; i<info->num_groups; i++ )
861     {
862         if( (info->group[i].len + n) > info->num_chars )
863         {
864             ERR("can't fit control %d text into template\n",i);
865             break;
866         }
867         r = GetWindowTextW( info->group[i].hwnd, &val[n], info->group[i].len+1 );
868         if( r != info->group[i].len )
869             break;
870         n += r;
871     }
872
873     TRACE("%d/%d controls were good\n", i, info->num_groups);
874
875     if( i == info->num_groups )
876     {
877         TRACE("Set property %s to %s\n",
878               debugstr_w(info->prop), debugstr_w(val) );
879         CharUpperBuffW( val, info->num_chars );
880         MSI_SetPropertyW( info->dialog->package, info->prop, val );
881         msi_dialog_evaluate_control_conditions( info->dialog );
882     }
883     HeapFree( GetProcessHeap(), 0, val );
884 }
885
886 /* now move to the next control if necessary */
887 static VOID msi_mask_next_control( struct msi_maskedit_info *info, HWND hWnd )
888 {
889     HWND hWndNext;
890     UINT len, i;
891
892     for( i=0; i<info->num_groups; i++ )
893         if( info->group[i].hwnd == hWnd )
894             break;
895
896     /* don't move from the last control */
897     if( i >= (info->num_groups-1) )
898         return;
899
900     len = SendMessageW( hWnd, WM_GETTEXTLENGTH, 0, 0 );
901     if( len < info->group[i].len )
902         return;
903
904     hWndNext = GetNextDlgTabItem( GetParent( hWnd ), hWnd, FALSE );
905     SetFocus( hWndNext );
906 }
907
908 static LRESULT WINAPI
909 MSIMaskedEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
910 {
911     struct msi_maskedit_info *info;
912     HRESULT r;
913
914     TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
915
916     info = GetPropW(hWnd, szButtonData);
917
918     r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
919
920     switch( msg )
921     {
922     case WM_COMMAND:
923         if (HIWORD(wParam) == EN_CHANGE)
924         {
925             msi_mask_control_change( info );
926             msi_mask_next_control( info, (HWND) lParam );
927         }
928         break;
929     case WM_NCDESTROY:
930         HeapFree( GetProcessHeap(), 0, info->prop );
931         HeapFree( GetProcessHeap(), 0, info );
932         RemovePropW( hWnd, szButtonData );
933         break;
934     }
935
936     return r;
937 }
938
939 /* fish the various bits of the property out and put them in the control */
940 static void
941 msi_maskedit_set_text( struct msi_maskedit_info *info, LPCWSTR text )
942 {
943     LPCWSTR p;
944     UINT i;
945
946     p = text;
947     for( i = 0; i < info->num_groups; i++ )
948     {
949         if( info->group[i].len < lstrlenW( p ) )
950         {
951             LPWSTR chunk = strdupW( p );
952             chunk[ info->group[i].len ] = 0;
953             SetWindowTextW( info->group[i].hwnd, chunk );
954             HeapFree( GetProcessHeap(), 0, chunk );
955         }
956         else
957         {
958             SetWindowTextW( info->group[i].hwnd, p );
959             break;
960         }
961         p += info->group[i].len;
962     }
963 }
964
965 static struct msi_maskedit_info * msi_dialog_parse_groups( LPCWSTR mask )
966 {
967     struct msi_maskedit_info * info = NULL;
968     int i = 0, n = 0, total = 0;
969     LPCWSTR p;
970
971     TRACE("masked control, template %s\n", debugstr_w(mask));
972
973     if( !mask )
974         return info;
975
976     p = strchrW(mask, '<');
977     if( !p )
978         return info;
979
980     info = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof *info );
981     if( !info )
982         return info;
983
984     p++;
985     for( i=0; i<MASK_MAX_GROUPS; i++ )
986     {
987         while (*p=='-')
988         {
989             total++;
990             p++;
991         }
992
993         /* stop at the end of the string */
994         if( p[0] == 0 || p[0] == '>' )
995             break;
996
997         /* count the number of the same identifier */
998         for( n=0; p[n] == p[0]; n++ )
999             ;
1000         info->group[i].ofs = total;
1001         info->group[i].type = p[0];
1002         if( p[n] == '=' )
1003         {
1004             n++;
1005             total++; /* an extra not part of the group */
1006         }
1007         info->group[i].len = n;
1008         total += n;
1009         p += n;
1010     }
1011
1012     TRACE("%d characters in %d groups\n", total, i );
1013     if( i == MASK_MAX_GROUPS )
1014         ERR("too many groups in PIDTemplate %s\n", debugstr_w(mask));
1015
1016     info->num_chars = total;
1017     info->num_groups = i;
1018
1019     return info;
1020 }
1021
1022 static void
1023 msi_maskedit_create_children( struct msi_maskedit_info *info, LPCWSTR font )
1024 {
1025     DWORD width, height, style, wx, ww;
1026     RECT rect;
1027     HWND hwnd;
1028     UINT i;
1029
1030     style = WS_CHILD | WS_BORDER | WS_VISIBLE | WS_TABSTOP;
1031
1032     GetClientRect( info->hwnd, &rect );
1033
1034     width = rect.right - rect.left;
1035     height = rect.bottom - rect.top;
1036
1037     for( i = 0; i < info->num_groups; i++ )
1038     {
1039         wx = (info->group[i].ofs * width) / info->num_chars;
1040         ww = (info->group[i].len * width) / info->num_chars;
1041
1042         hwnd = CreateWindowW( szEdit, NULL, style, wx, 0, ww, height,
1043                               info->hwnd, NULL, NULL, NULL );
1044         if( !hwnd )
1045         {
1046             ERR("failed to create mask edit sub window\n");
1047             break;
1048         }
1049
1050         SendMessageW( hwnd, EM_LIMITTEXT, info->group[i].len, 0 );
1051
1052         msi_dialog_set_font( info->dialog, hwnd,
1053                              font?font:info->dialog->default_font );
1054         info->group[i].hwnd = hwnd;
1055     }
1056 }
1057
1058 /* office 2003 uses "73931<````=````=````=````=`````>@@@@@" */
1059 static UINT msi_dialog_maskedit_control( msi_dialog *dialog, MSIRECORD *rec )
1060 {
1061     LPWSTR mask, title = NULL, val = NULL, font;
1062     struct msi_maskedit_info *info = NULL;
1063     UINT ret = ERROR_SUCCESS;
1064     msi_control *control;
1065     LPCWSTR prop;
1066
1067     TRACE("\n");
1068
1069     mask = msi_get_deformatted_field( dialog->package, rec, 10 );
1070     font = msi_dialog_get_style( &mask );
1071     if( !mask )
1072     {
1073         ERR("mask template is empty\n");
1074         goto end;
1075     }
1076
1077     info = msi_dialog_parse_groups( mask );
1078     if( !info )
1079     {
1080         ERR("template %s is invalid\n", debugstr_w(mask));
1081         goto end;
1082     }
1083
1084     info->dialog = dialog;
1085
1086     control = msi_dialog_add_control( dialog, rec, szStatic,
1087                    SS_OWNERDRAW | WS_GROUP | WS_VISIBLE );
1088     if( !control )
1089     {
1090         ERR("Failed to create maskedit container\n");
1091         ret = ERROR_FUNCTION_FAILED;
1092         goto end;
1093     }
1094     SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
1095
1096     info->hwnd = control->hwnd;
1097
1098     /* subclass the static control */
1099     info->oldproc = (WNDPROC) SetWindowLongPtrW( info->hwnd, GWLP_WNDPROC,
1100                                           (LONG_PTR)MSIMaskedEdit_WndProc );
1101     SetPropW( control->hwnd, szButtonData, info );
1102
1103     prop = MSI_RecordGetString( rec, 9 );
1104     if( prop )
1105         info->prop = strdupW( prop );
1106
1107     msi_maskedit_create_children( info, font );
1108
1109     if( prop )
1110     {
1111         val = msi_dup_property( dialog->package, prop );
1112         if( val )
1113         {
1114             msi_maskedit_set_text( info, val );
1115             HeapFree( GetProcessHeap(), 0, val );
1116         }
1117     }
1118
1119 end:
1120     if( ret != ERROR_SUCCESS )
1121         HeapFree( GetProcessHeap(), 0, info );
1122     HeapFree( GetProcessHeap(), 0, title );
1123     HeapFree( GetProcessHeap(), 0, mask );
1124     return ret;
1125 }
1126
1127 /******************** Path Edit ********************************************/
1128
1129 static UINT msi_dialog_pathedit_control( msi_dialog *dialog, MSIRECORD *rec )
1130 {
1131     FIXME("not implemented properly\n");
1132     return msi_dialog_edit_control( dialog, rec );
1133 }
1134
1135 /* radio buttons are a bit different from normal controls */
1136 static UINT msi_dialog_create_radiobutton( MSIRECORD *rec, LPVOID param )
1137 {
1138     radio_button_group_descr *group = (radio_button_group_descr *)param;
1139     msi_dialog *dialog = group->dialog;
1140     msi_control *control;
1141     LPCWSTR prop, text, name;
1142     DWORD style, attributes = group->attributes;
1143
1144     style = WS_CHILD | BS_AUTORADIOBUTTON | BS_MULTILINE | WS_TABSTOP;
1145     name = MSI_RecordGetString( rec, 3 );
1146     text = MSI_RecordGetString( rec, 8 );
1147     if( attributes & 1 )
1148         style |= WS_VISIBLE;
1149     if( ~attributes & 2 )
1150         style |= WS_DISABLED;
1151
1152     control = msi_dialog_create_window( dialog, rec, szButton, name, text,
1153                                         style, group->parent->hwnd );
1154     if (!control)
1155         return ERROR_FUNCTION_FAILED;
1156     control->handler = msi_dialog_radiogroup_handler;
1157
1158     prop = MSI_RecordGetString( rec, 1 );
1159     if( prop )
1160         control->property = strdupW( prop );
1161
1162     return ERROR_SUCCESS;
1163 }
1164
1165 static UINT msi_dialog_radiogroup_control( msi_dialog *dialog, MSIRECORD *rec )
1166 {
1167     static const WCHAR query[] = {
1168         'S','E','L','E','C','T',' ','*',' ',
1169         'F','R','O','M',' ','R','a','d','i','o','B','u','t','t','o','n',' ',
1170         'W','H','E','R','E',' ',
1171            '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
1172     UINT r;
1173     LPCWSTR prop;
1174     msi_control *control;
1175     MSIQUERY *view = NULL;
1176     radio_button_group_descr group;
1177     MSIPACKAGE *package = dialog->package;
1178     WNDPROC oldproc;
1179
1180     prop = MSI_RecordGetString( rec, 9 );
1181
1182     TRACE("%p %p %s\n", dialog, rec, debugstr_w( prop ));
1183
1184     /* Create parent group box to hold radio buttons */
1185     control = msi_dialog_add_control( dialog, rec, szButton, BS_OWNERDRAW|WS_GROUP );
1186     if( !control )
1187         return ERROR_FUNCTION_FAILED;
1188
1189     oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1190                                            (LONG_PTR)MSIRadioGroup_WndProc );
1191     SetPropW(control->hwnd, szButtonData, oldproc);
1192     SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
1193
1194     if( prop )
1195         control->property = strdupW( prop );
1196
1197     /* query the Radio Button table for all control in this group */
1198     r = MSI_OpenQuery( package->db, &view, query, prop );
1199     if( r != ERROR_SUCCESS )
1200     {
1201         ERR("query failed for dialog %s radio group %s\n", 
1202             debugstr_w(dialog->name), debugstr_w(prop));
1203         return ERROR_INVALID_PARAMETER;
1204     }
1205
1206     group.dialog = dialog;
1207     group.parent = control;
1208     group.attributes = MSI_RecordGetInteger( rec, 8 );
1209
1210     r = MSI_IterateRecords( view, 0, msi_dialog_create_radiobutton, &group );
1211     msiobj_release( &view->hdr );
1212
1213     return r;
1214 }
1215
1216 struct control_handler msi_dialog_handler[] =
1217 {
1218     { szText, msi_dialog_text_control },
1219     { szPushButton, msi_dialog_button_control },
1220     { szLine, msi_dialog_line_control },
1221     { szBitmap, msi_dialog_bitmap_control },
1222     { szCheckBox, msi_dialog_checkbox_control },
1223     { szScrollableText, msi_dialog_scrolltext_control },
1224     { szComboBox, msi_dialog_combo_control },
1225     { szEdit, msi_dialog_edit_control },
1226     { szMaskedEdit, msi_dialog_maskedit_control },
1227     { szPathEdit, msi_dialog_pathedit_control },
1228     { szRadioButtonGroup, msi_dialog_radiogroup_control },
1229     { szIcon, msi_dialog_icon_control },
1230 };
1231
1232 #define NUM_CONTROL_TYPES (sizeof msi_dialog_handler/sizeof msi_dialog_handler[0])
1233
1234 static UINT msi_dialog_create_controls( MSIRECORD *rec, LPVOID param )
1235 {
1236     msi_dialog *dialog = param;
1237     LPCWSTR control_type;
1238     UINT i;
1239
1240     /* find and call the function that can create this type of control */
1241     control_type = MSI_RecordGetString( rec, 3 );
1242     for( i=0; i<NUM_CONTROL_TYPES; i++ )
1243         if (!strcmpiW( msi_dialog_handler[i].control_type, control_type ))
1244             break;
1245     if( i != NUM_CONTROL_TYPES )
1246         msi_dialog_handler[i].func( dialog, rec );
1247     else
1248         ERR("no handler for element type %s\n", debugstr_w(control_type));
1249
1250     return ERROR_SUCCESS;
1251 }
1252
1253 static UINT msi_dialog_fill_controls( msi_dialog *dialog )
1254 {
1255     static const WCHAR query[] = {
1256         'S','E','L','E','C','T',' ','*',' ',
1257         'F','R','O','M',' ','C','o','n','t','r','o','l',' ',
1258         'W','H','E','R','E',' ',
1259            '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
1260     UINT r;
1261     MSIQUERY *view = NULL;
1262     MSIPACKAGE *package = dialog->package;
1263
1264     TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
1265
1266     /* query the Control table for all the elements of the control */
1267     r = MSI_OpenQuery( package->db, &view, query, dialog->name );
1268     if( r != ERROR_SUCCESS )
1269     {
1270         ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
1271         return ERROR_INVALID_PARAMETER;
1272     }
1273
1274     r = MSI_IterateRecords( view, 0, msi_dialog_create_controls, dialog );
1275     msiobj_release( &view->hdr );
1276
1277     return r;
1278 }
1279
1280 static UINT msi_dialog_set_control_condition( MSIRECORD *rec, LPVOID param )
1281 {
1282     static const WCHAR szHide[] = { 'H','i','d','e',0 };
1283     static const WCHAR szShow[] = { 'S','h','o','w',0 };
1284     static const WCHAR szDisable[] = { 'D','i','s','a','b','l','e',0 };
1285     static const WCHAR szEnable[] = { 'E','n','a','b','l','e',0 };
1286     msi_dialog *dialog = param;
1287     msi_control *control;
1288     LPCWSTR name, action, condition;
1289     UINT r;
1290
1291     name = MSI_RecordGetString( rec, 2 );
1292     action = MSI_RecordGetString( rec, 3 );
1293     condition = MSI_RecordGetString( rec, 4 );
1294     r = MSI_EvaluateConditionW( dialog->package, condition );
1295     control = msi_dialog_find_control( dialog, name );
1296     if( r && control )
1297     {
1298         TRACE("%s control %s\n", debugstr_w(action), debugstr_w(name));
1299
1300         /* FIXME: case sensitive? */
1301         if(!lstrcmpW(action, szHide))
1302             ShowWindow(control->hwnd, SW_HIDE);
1303         else if(!strcmpW(action, szShow))
1304             ShowWindow(control->hwnd, SW_SHOW);
1305         else if(!strcmpW(action, szDisable))
1306             EnableWindow(control->hwnd, FALSE);
1307         else if(!strcmpW(action, szEnable))
1308             EnableWindow(control->hwnd, TRUE);
1309         else
1310             FIXME("Unhandled action %s\n", debugstr_w(action));
1311     }
1312
1313     return ERROR_SUCCESS;
1314 }
1315
1316 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog )
1317 {
1318     static const WCHAR query[] = {
1319       'S','E','L','E','C','T',' ','*',' ',
1320       'F','R','O','M',' ',
1321         'C','o','n','t','r','o','l','C','o','n','d','i','t','i','o','n',' ',
1322       'W','H','E','R','E',' ',
1323         '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0
1324     };
1325     UINT r;
1326     MSIQUERY *view = NULL;
1327     MSIPACKAGE *package = dialog->package;
1328
1329     TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
1330
1331     /* query the Control table for all the elements of the control */
1332     r = MSI_OpenQuery( package->db, &view, query, dialog->name );
1333     if( r != ERROR_SUCCESS )
1334     {
1335         ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
1336         return ERROR_INVALID_PARAMETER;
1337     }
1338
1339     r = MSI_IterateRecords( view, 0, msi_dialog_set_control_condition, dialog );
1340     msiobj_release( &view->hdr );
1341
1342     return r;
1343 }
1344
1345 /* figure out the height of 10 point MS Sans Serif */
1346 static INT msi_dialog_get_sans_serif_height( HWND hwnd )
1347 {
1348     static const WCHAR szSansSerif[] = {
1349         'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0 };
1350     LOGFONTW lf;
1351     TEXTMETRICW tm;
1352     BOOL r;
1353     LONG height = 0;
1354     HFONT hFont, hOldFont;
1355     HDC hdc;
1356
1357     hdc = GetDC( hwnd );
1358     if (hdc)
1359     {
1360         memset( &lf, 0, sizeof lf );
1361         lf.lfHeight = MulDiv(10, GetDeviceCaps(hdc, LOGPIXELSY), 72);
1362         strcpyW( lf.lfFaceName, szSansSerif );
1363         hFont = CreateFontIndirectW(&lf);
1364         if (hFont)
1365         {
1366             hOldFont = SelectObject( hdc, hFont );
1367             r = GetTextMetricsW( hdc, &tm );
1368             if (r)
1369                 height = tm.tmHeight;
1370             SelectObject( hdc, hOldFont );
1371             DeleteObject( hFont );
1372         }
1373         ReleaseDC( hwnd, hdc );
1374     }
1375     return height;
1376 }
1377
1378 /* fetch the associated record from the Dialog table */
1379 static MSIRECORD *msi_get_dialog_record( msi_dialog *dialog )
1380 {
1381     static const WCHAR query[] = {
1382         'S','E','L','E','C','T',' ','*',' ',
1383         'F','R','O','M',' ','D','i','a','l','o','g',' ',
1384         'W','H','E','R','E',' ',
1385            '`','D','i','a','l','o','g','`',' ','=',' ','\'','%','s','\'',0};
1386     MSIPACKAGE *package = dialog->package;
1387     MSIRECORD *rec = NULL;
1388
1389     TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
1390
1391     rec = MSI_QueryGetRecord( package->db, query, dialog->name );
1392     if( !rec )
1393         ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
1394
1395     return rec;
1396 }
1397
1398 static void msi_dialog_adjust_dialog_size( msi_dialog *dialog, LPSIZE sz )
1399 {
1400     RECT rect;
1401     LONG style;
1402
1403     /* turn the client size into the window rectangle */
1404     rect.left = 0;
1405     rect.top = 0;
1406     rect.right = msi_dialog_scale_unit( dialog, sz->cx );
1407     rect.bottom = msi_dialog_scale_unit( dialog, sz->cy );
1408     style = GetWindowLongPtrW( dialog->hwnd, GWL_STYLE );
1409     AdjustWindowRect( &rect, style, FALSE );
1410     sz->cx = rect.right - rect.left;
1411     sz->cy = rect.bottom - rect.top;
1412 }
1413
1414 static BOOL msi_control_set_next( msi_control *control, msi_control *next )
1415 {
1416     return SetWindowPos( next->hwnd, control->hwnd, 0, 0, 0, 0,
1417                          SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOREDRAW |
1418                          SWP_NOREPOSITION | SWP_NOSENDCHANGING | SWP_NOSIZE );
1419 }
1420
1421 static UINT msi_dialog_set_tab_order( msi_dialog *dialog )
1422 {
1423     msi_control *control, *tab_next;
1424
1425     LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
1426     {
1427         tab_next = msi_dialog_find_control( dialog, control->tabnext );
1428         if( !tab_next )
1429             continue;
1430         msi_control_set_next( control, tab_next );
1431     }
1432
1433     return ERROR_SUCCESS;
1434 }
1435
1436 static void msi_dialog_set_first_control( msi_dialog* dialog, LPCWSTR name )
1437 {
1438     msi_control *control;
1439
1440     control = msi_dialog_find_control( dialog, name );
1441     if( control )
1442         dialog->hWndFocus = control->hwnd;
1443     else
1444         dialog->hWndFocus = NULL;
1445 }
1446
1447 static LRESULT msi_dialog_oncreate( HWND hwnd, LPCREATESTRUCTW cs )
1448 {
1449     static const WCHAR df[] = {
1450         'D','e','f','a','u','l','t','U','I','F','o','n','t',0 };
1451     msi_dialog *dialog = (msi_dialog*) cs->lpCreateParams;
1452     MSIRECORD *rec = NULL;
1453     LPWSTR title = NULL;
1454     SIZE size;
1455
1456     TRACE("%p %p\n", dialog, dialog->package);
1457
1458     dialog->hwnd = hwnd;
1459     SetWindowLongPtrW( hwnd, GWLP_USERDATA, (LONG_PTR) dialog );
1460
1461     rec = msi_get_dialog_record( dialog );
1462     if( !rec )
1463     {
1464         TRACE("No record found for dialog %s\n", debugstr_w(dialog->name));
1465         return -1;
1466     }
1467
1468     dialog->scale = msi_dialog_get_sans_serif_height(dialog->hwnd);
1469
1470     size.cx = MSI_RecordGetInteger( rec, 4 );
1471     size.cy = MSI_RecordGetInteger( rec, 5 );
1472     msi_dialog_adjust_dialog_size( dialog, &size );
1473
1474     dialog->attributes = MSI_RecordGetInteger( rec, 6 );
1475
1476     dialog->default_font = msi_dup_property( dialog->package, df );
1477
1478     title = msi_get_deformatted_field( dialog->package, rec, 7 );
1479     SetWindowTextW( hwnd, title );
1480     HeapFree( GetProcessHeap(), 0, title );
1481
1482     SetWindowPos( hwnd, 0, 0, 0, size.cx, size.cy,
1483                   SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW );
1484
1485
1486     msi_dialog_build_font_list( dialog );
1487     msi_dialog_fill_controls( dialog );
1488     msi_dialog_evaluate_control_conditions( dialog );
1489     msi_dialog_set_tab_order( dialog );
1490     msi_dialog_set_first_control( dialog, MSI_RecordGetString( rec, 8 ) );
1491     msiobj_release( &rec->hdr );
1492
1493     return 0;
1494 }
1495
1496 static UINT msi_dialog_send_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
1497 {
1498     LPWSTR event_fmt = NULL, arg_fmt = NULL;
1499
1500     TRACE("Sending control event %s %s\n", debugstr_w(event), debugstr_w(arg));
1501
1502     deformat_string( dialog->package, event, &event_fmt );
1503     deformat_string( dialog->package, arg, &arg_fmt );
1504
1505     dialog->event_handler( dialog->package, event_fmt, arg_fmt, dialog );
1506
1507     HeapFree( GetProcessHeap(), 0, event_fmt );
1508     HeapFree( GetProcessHeap(), 0, arg_fmt );
1509
1510     return ERROR_SUCCESS;
1511 }
1512
1513 static UINT msi_dialog_set_property( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
1514 {
1515     static const WCHAR szNullArg[] = { '{','}',0 };
1516     LPWSTR p, prop, arg_fmt = NULL;
1517     UINT len;
1518
1519     len = strlenW(event);
1520     prop = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR));
1521     strcpyW( prop, &event[1] );
1522     p = strchrW( prop, ']' );
1523     if( p && p[1] == 0 )
1524     {
1525         *p = 0;
1526         if( strcmpW( szNullArg, arg ) )
1527             deformat_string( dialog->package, arg, &arg_fmt );
1528         MSI_SetPropertyW( dialog->package, prop, arg_fmt );
1529     }
1530     else
1531         ERR("Badly formatted property string - what happens?\n");
1532     HeapFree( GetProcessHeap(), 0, prop );
1533     return ERROR_SUCCESS;
1534 }
1535
1536 static UINT msi_dialog_control_event( MSIRECORD *rec, LPVOID param )
1537 {
1538     msi_dialog *dialog = param;
1539     LPCWSTR condition, event, arg;
1540     UINT r;
1541
1542     condition = MSI_RecordGetString( rec, 5 );
1543     r = MSI_EvaluateConditionW( dialog->package, condition );
1544     if( r )
1545     {
1546         event = MSI_RecordGetString( rec, 3 );
1547         arg = MSI_RecordGetString( rec, 4 );
1548         if( event[0] == '[' )
1549             msi_dialog_set_property( dialog, event, arg );
1550         else
1551             msi_dialog_send_event( dialog, event, arg );
1552     }
1553
1554     return ERROR_SUCCESS;
1555 }
1556
1557 static UINT msi_dialog_button_handler( msi_dialog *dialog,
1558                                        msi_control *control, WPARAM param )
1559 {
1560     static const WCHAR query[] = {
1561       'S','E','L','E','C','T',' ','*',' ',
1562       'F','R','O','M',' ','C','o','n','t','r','o','l','E','v','e','n','t',' ',
1563       'W','H','E','R','E',' ',
1564          '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
1565       'A','N','D',' ',
1566          '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',' ',
1567       'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','i','n','g','`',0
1568     };
1569     MSIQUERY *view = NULL;
1570     UINT r;
1571
1572     if( HIWORD(param) != BN_CLICKED )
1573         return ERROR_SUCCESS;
1574
1575     r = MSI_OpenQuery( dialog->package->db, &view, query,
1576                        dialog->name, control->name );
1577     if( r != ERROR_SUCCESS )
1578     {
1579         ERR("query failed\n");
1580         return 0;
1581     }
1582
1583     r = MSI_IterateRecords( view, 0, msi_dialog_control_event, dialog );
1584     msiobj_release( &view->hdr );
1585
1586     return r;
1587 }
1588
1589 static UINT msi_dialog_get_checkbox_state( msi_dialog *dialog,
1590                 msi_control *control )
1591 {
1592     WCHAR state[2] = { 0 };
1593     DWORD sz = 2;
1594
1595     MSI_GetPropertyW( dialog->package, control->property, state, &sz );
1596     return state[0] ? 1 : 0;
1597 }
1598
1599 static void msi_dialog_set_checkbox_state( msi_dialog *dialog,
1600                 msi_control *control, UINT state )
1601 {
1602     static const WCHAR szState[] = { '1', 0 };
1603     LPCWSTR val;
1604
1605     /* if uncheck then the property is set to NULL */
1606     if (!state)
1607     {
1608         MSI_SetPropertyW( dialog->package, control->property, NULL );
1609         return;
1610     }
1611
1612     /* check for a custom state */
1613     if (control->value && control->value[0])
1614         val = control->value;
1615     else
1616         val = szState;
1617
1618     MSI_SetPropertyW( dialog->package, control->property, val );
1619 }
1620
1621 static void msi_dialog_checkbox_sync_state( msi_dialog *dialog,
1622                 msi_control *control )
1623 {
1624     UINT state;
1625
1626     state = msi_dialog_get_checkbox_state( dialog, control );
1627     SendMessageW( control->hwnd, BM_SETCHECK,
1628                   state ? BST_CHECKED : BST_UNCHECKED, 0 );
1629 }
1630
1631 static UINT msi_dialog_checkbox_handler( msi_dialog *dialog,
1632                 msi_control *control, WPARAM param )
1633 {
1634     UINT state;
1635
1636     if( HIWORD(param) != BN_CLICKED )
1637         return ERROR_SUCCESS;
1638
1639     TRACE("clicked checkbox %s, set %s\n", debugstr_w(control->name),
1640           debugstr_w(control->property));
1641
1642     state = msi_dialog_get_checkbox_state( dialog, control );
1643     state = state ? 0 : 1;
1644     msi_dialog_set_checkbox_state( dialog, control, state );
1645     msi_dialog_checkbox_sync_state( dialog, control );
1646
1647     return msi_dialog_button_handler( dialog, control, param );
1648 }
1649
1650 static UINT msi_dialog_edit_handler( msi_dialog *dialog,
1651                 msi_control *control, WPARAM param )
1652 {
1653     UINT sz, r;
1654     LPWSTR buf;
1655
1656     if( HIWORD(param) != EN_CHANGE )
1657         return ERROR_SUCCESS;
1658
1659     TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
1660           debugstr_w(control->property));
1661
1662     sz = 0x20;
1663     buf = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
1664     while( buf )
1665     {
1666         r = GetWindowTextW( control->hwnd, buf, sz );
1667         if( r < (sz-1) )
1668             break;
1669             sz *= 2;
1670         buf = HeapReAlloc( GetProcessHeap(), 0, buf, sz*sizeof(WCHAR) );
1671     }
1672
1673     MSI_SetPropertyW( dialog->package, control->property, buf );
1674
1675     HeapFree( GetProcessHeap(), 0, buf );
1676
1677     return ERROR_SUCCESS;
1678 }
1679
1680 static UINT msi_dialog_radiogroup_handler( msi_dialog *dialog,
1681                 msi_control *control, WPARAM param )
1682 {
1683     if( HIWORD(param) != BN_CLICKED )
1684         return ERROR_SUCCESS;
1685
1686     TRACE("clicked radio button %s, set %s\n", debugstr_w(control->name),
1687           debugstr_w(control->property));
1688
1689     MSI_SetPropertyW( dialog->package, control->property, control->name );
1690
1691     return msi_dialog_button_handler( dialog, control, param );
1692 }
1693
1694 static LRESULT msi_dialog_oncommand( msi_dialog *dialog, WPARAM param, HWND hwnd )
1695 {
1696     msi_control *control;
1697
1698     TRACE("%p %p %08x\n", dialog, hwnd, param);
1699
1700     control = msi_dialog_find_control_by_hwnd( dialog, hwnd );
1701     if( control )
1702     {
1703         if( control->handler )
1704         {
1705             control->handler( dialog, control, param );
1706             msi_dialog_evaluate_control_conditions( dialog );
1707         }
1708     }
1709     else
1710         ERR("button click from nowhere\n");
1711     return 0;
1712 }
1713
1714 static void msi_dialog_setfocus( msi_dialog *dialog )
1715 {
1716     HWND hwnd = dialog->hWndFocus;
1717
1718     hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, TRUE);
1719     hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, FALSE);
1720     SetFocus( hwnd );
1721     dialog->hWndFocus = hwnd;
1722 }
1723
1724 static LRESULT WINAPI MSIDialog_WndProc( HWND hwnd, UINT msg,
1725                 WPARAM wParam, LPARAM lParam )
1726 {
1727     msi_dialog *dialog = (LPVOID) GetWindowLongPtrW( hwnd, GWLP_USERDATA );
1728
1729     TRACE("0x%04x\n", msg);
1730
1731     switch (msg)
1732     {
1733     case WM_CREATE:
1734         return msi_dialog_oncreate( hwnd, (LPCREATESTRUCTW)lParam );
1735
1736     case WM_COMMAND:
1737         return msi_dialog_oncommand( dialog, wParam, (HWND)lParam );
1738
1739     case WM_ACTIVATE:
1740         if( LOWORD(wParam) == WA_INACTIVE )
1741             dialog->hWndFocus = GetFocus();
1742         else
1743             msi_dialog_setfocus( dialog );
1744         return 0;
1745
1746     case WM_SETFOCUS:
1747         msi_dialog_setfocus( dialog );
1748         return 0;
1749
1750     /* bounce back to our subclassed static control */
1751     case WM_CTLCOLORSTATIC:
1752         return SendMessageW( (HWND) lParam, WM_CTLCOLORSTATIC, wParam, lParam );
1753
1754     case WM_DESTROY:
1755         dialog->hwnd = NULL;
1756         return 0;
1757     }
1758     return DefWindowProcW(hwnd, msg, wParam, lParam);
1759 }
1760
1761 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1762 {
1763     WNDPROC oldproc = (WNDPROC) GetPropW(hWnd, szButtonData);
1764
1765     TRACE("hWnd %p msg %04x wParam 0x%08x lParam 0x%08lx\n", hWnd, msg, wParam, lParam);
1766
1767     if (msg == WM_COMMAND) /* Forward notifications to dialog */
1768         SendMessageW(GetParent(hWnd), msg, wParam, lParam);
1769
1770     return CallWindowProcW(oldproc, hWnd, msg, wParam, lParam);
1771 }
1772
1773 static LRESULT WINAPI MSIHiddenWindowProc( HWND hwnd, UINT msg,
1774                 WPARAM wParam, LPARAM lParam )
1775 {
1776     msi_dialog *dialog = (msi_dialog*) lParam;
1777
1778     TRACE("%d %p\n", msg, dialog);
1779
1780     switch (msg)
1781     {
1782     case WM_MSI_DIALOG_CREATE:
1783         return msi_dialog_run_message_loop( dialog );
1784     case WM_MSI_DIALOG_DESTROY:
1785         msi_dialog_destroy( dialog );
1786         return 0;
1787     }
1788     return DefWindowProcW( hwnd, msg, wParam, lParam );
1789 }
1790
1791 /* functions that interface to other modules within MSI */
1792
1793 msi_dialog *msi_dialog_create( MSIPACKAGE* package, LPCWSTR szDialogName,
1794                                 msi_dialog_event_handler event_handler )
1795 {
1796     MSIRECORD *rec = NULL;
1797     msi_dialog *dialog;
1798
1799     TRACE("%p %s\n", package, debugstr_w(szDialogName));
1800
1801     /* allocate the structure for the dialog to use */
1802     dialog = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
1803                         sizeof *dialog + sizeof(WCHAR)*strlenW(szDialogName) );
1804     if( !dialog )
1805         return NULL;
1806     strcpyW( dialog->name, szDialogName );
1807     msiobj_addref( &package->hdr );
1808     dialog->package = package;
1809     dialog->event_handler = event_handler;
1810     dialog->finished = 0;
1811     list_init( &dialog->controls );
1812
1813     /* verify that the dialog exists */
1814     rec = msi_get_dialog_record( dialog );
1815     if( !rec )
1816     {
1817         msiobj_release( &package->hdr );
1818         HeapFree( GetProcessHeap(), 0, dialog );
1819         return NULL;
1820     }
1821     dialog->attributes = MSI_RecordGetInteger( rec, 6 );
1822     msiobj_release( &rec->hdr );
1823
1824     return dialog;
1825 }
1826
1827 static void msi_process_pending_messages( HWND hdlg )
1828 {
1829     MSG msg;
1830
1831     while( PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ) )
1832     {
1833         if( hdlg && IsDialogMessageW( hdlg, &msg ))
1834             continue;
1835         TranslateMessage( &msg );
1836         DispatchMessageW( &msg );
1837     }
1838 }
1839
1840 void msi_dialog_end_dialog( msi_dialog *dialog )
1841 {
1842     TRACE("%p\n", dialog);
1843     dialog->finished = 1;
1844     PostMessageW(dialog->hwnd, WM_NULL, 0, 0);
1845 }
1846
1847 void msi_dialog_check_messages( HANDLE handle )
1848 {
1849     DWORD r;
1850
1851     /* in threads other than the UI thread, block */
1852     if( uiThreadId != GetCurrentThreadId() )
1853     {
1854         if( handle )
1855             WaitForSingleObject( handle, INFINITE );
1856         return;
1857     }
1858
1859     /* there's two choices for the UI thread */
1860     while (1)
1861     {
1862         msi_process_pending_messages( NULL );
1863
1864         if( !handle )
1865             break;
1866
1867         /*
1868          * block here until somebody creates a new dialog or
1869          * the handle we're waiting on becomes ready
1870          */
1871         r = MsgWaitForMultipleObjects( 1, &handle, 0, INFINITE, QS_ALLINPUT );
1872         if( r == WAIT_OBJECT_0 )
1873             break;
1874     }
1875 }
1876
1877 UINT msi_dialog_run_message_loop( msi_dialog *dialog )
1878 {
1879     HWND hwnd;
1880
1881     if( !(dialog->attributes & msidbDialogAttributesVisible) )
1882         return ERROR_SUCCESS;
1883
1884     if( uiThreadId != GetCurrentThreadId() )
1885         return SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_CREATE, 0, (LPARAM) dialog );
1886
1887     /* create the dialog window, don't show it yet */
1888     hwnd = CreateWindowW( szMsiDialogClass, dialog->name, WS_OVERLAPPEDWINDOW,
1889                      CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
1890                      NULL, NULL, NULL, dialog );
1891     if( !hwnd )
1892     {
1893         ERR("Failed to create dialog %s\n", debugstr_w( dialog->name ));
1894         return ERROR_FUNCTION_FAILED;
1895     }
1896
1897     ShowWindow( hwnd, SW_SHOW );
1898     UpdateWindow( hwnd );
1899
1900     if( dialog->attributes & msidbDialogAttributesModal )
1901     {
1902         while( !dialog->finished )
1903         {
1904             MsgWaitForMultipleObjects( 0, NULL, 0, INFINITE, QS_ALLEVENTS );
1905             msi_process_pending_messages( dialog->hwnd );
1906         }
1907     }
1908     else
1909         return ERROR_IO_PENDING;
1910
1911     return ERROR_SUCCESS;
1912 }
1913
1914 void msi_dialog_do_preview( msi_dialog *dialog )
1915 {
1916     TRACE("\n");
1917     dialog->attributes |= msidbDialogAttributesVisible;
1918     dialog->attributes &= ~msidbDialogAttributesModal;
1919     msi_dialog_run_message_loop( dialog );
1920 }
1921
1922 void msi_dialog_destroy( msi_dialog *dialog )
1923 {
1924     if( uiThreadId != GetCurrentThreadId() )
1925     {
1926         SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_DESTROY, 0, (LPARAM) dialog );
1927         return;
1928     }
1929
1930     if( dialog->hwnd )
1931         ShowWindow( dialog->hwnd, SW_HIDE );
1932     
1933     if( dialog->hwnd )
1934         DestroyWindow( dialog->hwnd );
1935
1936     /* destroy the list of controls */
1937     while( !list_empty( &dialog->controls ) )
1938     {
1939         msi_control *t = LIST_ENTRY( list_head( &dialog->controls ),
1940                                      msi_control, entry );
1941         list_remove( &t->entry );
1942         /* leave dialog->hwnd - destroying parent destroys child windows */
1943         HeapFree( GetProcessHeap(), 0, t->property );
1944         HeapFree( GetProcessHeap(), 0, t->value );
1945         if( t->hBitmap )
1946             DeleteObject( t->hBitmap );
1947         if( t->hIcon )
1948             DestroyIcon( t->hIcon );
1949         HeapFree( GetProcessHeap(), 0, t->tabnext );
1950         HeapFree( GetProcessHeap(), 0, t );
1951         if (t->hDll)
1952             FreeLibrary( t->hDll );
1953     }
1954
1955     /* destroy the list of fonts */
1956     while( dialog->font_list )
1957     {
1958         msi_font *t = dialog->font_list;
1959         dialog->font_list = t->next;
1960         DeleteObject( t->hfont );
1961         HeapFree( GetProcessHeap(), 0, t );
1962     }
1963     HeapFree( GetProcessHeap(), 0, dialog->default_font );
1964
1965     msiobj_release( &dialog->package->hdr );
1966     dialog->package = NULL;
1967     HeapFree( GetProcessHeap(), 0, dialog );
1968 }
1969
1970 BOOL msi_dialog_register_class( void )
1971 {
1972     WNDCLASSW cls;
1973
1974     ZeroMemory( &cls, sizeof cls );
1975     cls.lpfnWndProc   = MSIDialog_WndProc;
1976     cls.hInstance     = NULL;
1977     cls.hIcon         = LoadIconW(0, (LPWSTR)IDI_APPLICATION);
1978     cls.hCursor       = LoadCursorW(0, (LPWSTR)IDC_ARROW);
1979     cls.hbrBackground = (HBRUSH)(COLOR_WINDOW);
1980     cls.lpszMenuName  = NULL;
1981     cls.lpszClassName = szMsiDialogClass;
1982
1983     if( !RegisterClassW( &cls ) )
1984         return FALSE;
1985
1986     cls.lpfnWndProc   = MSIHiddenWindowProc;
1987     cls.lpszClassName = szMsiHiddenWindow;
1988
1989     if( !RegisterClassW( &cls ) )
1990         return FALSE;
1991
1992     uiThreadId = GetCurrentThreadId();
1993
1994     hMsiHiddenWindow = CreateWindowW( szMsiHiddenWindow, NULL, WS_OVERLAPPED,
1995                                    0, 0, 100, 100, NULL, NULL, NULL, NULL );
1996     if( !hMsiHiddenWindow )
1997         return FALSE;
1998
1999     return TRUE;
2000 }
2001
2002 void msi_dialog_unregister_class( void )
2003 {
2004     DestroyWindow( hMsiHiddenWindow );
2005     UnregisterClassW( szMsiDialogClass, NULL );
2006     uiThreadId = 0;
2007 }