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