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