Implement summary information loading and saving.
[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 #include <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "winnls.h"
27 #include "wingdi.h"
28 #include "msi.h"
29 #include "msipriv.h"
30 #include "msidefs.h"
31
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
34
35 #include "action.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(msi);
38
39
40 const WCHAR szMsiDialogClass[] = {
41     'M','s','i','D','i','a','l','o','g','C','l','o','s','e','C','l','a','s','s',0
42 };
43 const static WCHAR szStatic[] = { 'S','t','a','t','i','c',0 };
44 const static WCHAR szButton[] = { 'B','U','T','T','O','N', 0 };
45
46 const static WCHAR szButtonData[] = { 'M','S','I','D','A','T','A',0 };
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 msi_control_tag *next;
55     HWND hwnd;
56     msi_handler handler;
57     LPWSTR property;
58     WCHAR name[1];
59 };
60
61 typedef struct msi_font_tag
62 {
63     struct msi_font_tag *next;
64     HFONT hfont;
65     WCHAR name[1];
66 } msi_font;
67
68 struct msi_dialog_tag
69 {
70     MSIPACKAGE *package;
71     msi_dialog_event_handler event_handler;
72     BOOL finished;
73     INT scale;
74     DWORD attributes;
75     HWND hwnd;
76     LPWSTR default_font;
77     msi_font *font_list;
78     msi_control *control_list;
79     WCHAR name[1];
80 };
81
82 typedef UINT (*msi_dialog_control_func)( msi_dialog *dialog, MSIRECORD *rec );
83 struct control_handler 
84 {
85     LPCWSTR control_type;
86     msi_dialog_control_func func;
87 };
88
89 typedef struct
90 {
91     msi_dialog* dialog;
92     msi_control *parent;
93     DWORD       attributes;
94 } radio_button_group_descr;
95
96 static UINT msi_dialog_checkbox_handler( msi_dialog *, msi_control *, WPARAM );
97 static void msi_dialog_checkbox_sync_state( msi_dialog *, msi_control * );
98 static UINT msi_dialog_button_handler( msi_dialog *, msi_control *, WPARAM );
99 static UINT msi_dialog_edit_handler( msi_dialog *, msi_control *, WPARAM );
100 static UINT msi_dialog_radiogroup_handler( msi_dialog *, msi_control *, WPARAM param );
101 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
102
103
104 INT msi_dialog_scale_unit( msi_dialog *dialog, INT val )
105 {
106     return (dialog->scale * val + 5) / 10;
107 }
108
109 /*
110  * msi_dialog_get_style
111  *
112  * Extract the {\style} string from the front of the text to display and
113  *  update the pointer.
114  */
115 static LPWSTR msi_dialog_get_style( LPCWSTR *text )
116 {
117     LPWSTR ret = NULL;
118     LPCWSTR p = *text, q;
119     DWORD len;
120
121     if( *p++ != '{' )
122         return ret;
123     q = strchrW( p, '}' );
124     if( !q )
125         return ret;
126     *text = ++q;
127     if( *p++ != '\\' )
128         return ret;
129     len = q - p;
130     
131     ret = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
132     if( !ret )
133         return ret;
134     strncpyW( ret, p, len );
135     ret[len-1] = 0;
136     return ret;
137 }
138
139 static UINT msi_dialog_add_font( MSIRECORD *rec, LPVOID param )
140 {
141     msi_dialog *dialog = param;
142     msi_font *font;
143     LPCWSTR face, name;
144     LOGFONTW lf;
145     INT style;
146     HDC hdc;
147
148     /* create a font and add it to the list */
149     name = MSI_RecordGetString( rec, 1 );
150     font = HeapAlloc( GetProcessHeap(), 0,
151                       sizeof *font + strlenW( name )*sizeof (WCHAR) );
152     strcpyW( font->name, name );
153     font->next = dialog->font_list;
154     dialog->font_list = font;
155
156     memset( &lf, 0, sizeof lf );
157     face = MSI_RecordGetString( rec, 2 );
158     lf.lfHeight = MSI_RecordGetInteger( rec, 3 );
159     style = MSI_RecordGetInteger( rec, 5 );
160     if( style & msidbTextStyleStyleBitsBold )
161         lf.lfWeight = FW_BOLD;
162     if( style & msidbTextStyleStyleBitsItalic )
163         lf.lfItalic = TRUE;
164     if( style & msidbTextStyleStyleBitsUnderline )
165         lf.lfUnderline = TRUE;
166     if( style & msidbTextStyleStyleBitsStrike )
167         lf.lfStrikeOut = TRUE;
168     lstrcpynW( lf.lfFaceName, face, LF_FACESIZE );
169
170     /* adjust the height */
171     hdc = GetDC( dialog->hwnd );
172     if (hdc)
173     {
174         lf.lfHeight = -MulDiv(lf.lfHeight, GetDeviceCaps(hdc, LOGPIXELSY), 72);
175         ReleaseDC( dialog->hwnd, hdc );
176     }
177
178     font->hfont = CreateFontIndirectW( &lf );
179
180     TRACE("Adding font style %s\n", debugstr_w(font->name) );
181
182     return ERROR_SUCCESS;
183 }
184
185 static msi_font *msi_dialog_find_font( msi_dialog *dialog, LPCWSTR name )
186 {
187     msi_font *font;
188
189     for( font = dialog->font_list; font; font = font->next )
190         if( !strcmpW( font->name, name ) )  /* FIXME: case sensitive? */
191             break;
192
193     return font;
194 }
195
196 static UINT msi_dialog_set_font( msi_dialog *dialog, HWND hwnd, LPCWSTR name )
197 {
198     msi_font *font;
199
200     font = msi_dialog_find_font( dialog, name );
201     if( font )
202         SendMessageW( hwnd, WM_SETFONT, (WPARAM) font->hfont, TRUE );
203     else
204         ERR("No font entry for %s\n", debugstr_w(name));
205     return ERROR_SUCCESS;
206 }
207
208 static UINT msi_dialog_build_font_list( msi_dialog *dialog )
209 {
210     static const WCHAR query[] = {
211       'S','E','L','E','C','T',' ','*',' ',
212       'F','R','O','M',' ','`','T','e','x','t','S','t','y','l','e','`',' ',0
213     };
214     UINT r;
215     MSIQUERY *view = NULL;
216
217     TRACE("dialog %p\n", dialog );
218
219     r = MSI_OpenQuery( dialog->package->db, &view, query );
220     if( r != ERROR_SUCCESS )
221         return r;
222
223     r = MSI_IterateRecords( view, NULL, msi_dialog_add_font, dialog );
224     msiobj_release( &view->hdr );
225
226     return r;
227 }
228
229 static msi_control *msi_dialog_add_control( msi_dialog *dialog,
230                 MSIRECORD *rec, LPCWSTR szCls, DWORD style )
231 {
232     DWORD x, y, width, height, attributes;
233     LPCWSTR text, name;
234     LPWSTR font = NULL, title = NULL;
235     msi_control *control = NULL;
236
237     style |= WS_CHILD | WS_GROUP;
238
239     name = MSI_RecordGetString( rec, 2 );
240     control = HeapAlloc( GetProcessHeap(), 0,
241                          sizeof *control + strlenW(name)*sizeof(WCHAR) );
242     strcpyW( control->name, name );
243     control->next = dialog->control_list;
244     dialog->control_list = control;
245     control->handler = NULL;
246     control->property = NULL;
247
248     x = MSI_RecordGetInteger( rec, 4 );
249     y = MSI_RecordGetInteger( rec, 5 );
250     width = MSI_RecordGetInteger( rec, 6 );
251     height = MSI_RecordGetInteger( rec, 7 );
252     attributes = MSI_RecordGetInteger( rec, 8 );
253     text = MSI_RecordGetString( rec, 10 );
254
255     TRACE("Dialog %s control %s\n", debugstr_w(dialog->name), debugstr_w(text));
256
257     x = msi_dialog_scale_unit( dialog, x );
258     y = msi_dialog_scale_unit( dialog, y );
259     width = msi_dialog_scale_unit( dialog, width );
260     height = msi_dialog_scale_unit( dialog, height );
261
262     if( attributes & 1 )
263         style |= WS_VISIBLE;
264     if( ~attributes & 2 )
265         style |= WS_DISABLED;
266     if( text )
267     {
268         font = msi_dialog_get_style( &text );
269         deformat_string( dialog->package, text, &title );
270     }
271     control->hwnd = CreateWindowW( szCls, title, style,
272                           x, y, width, height, dialog->hwnd, NULL, NULL, NULL );
273     msi_dialog_set_font( dialog, control->hwnd,
274                          font ? font : dialog->default_font );
275     HeapFree( GetProcessHeap(), 0, font );
276     HeapFree( GetProcessHeap(), 0, title );
277     return control;
278 }
279
280 static UINT msi_dialog_text_control( msi_dialog *dialog, MSIRECORD *rec )
281 {
282     TRACE("%p %p\n", dialog, rec);
283
284     msi_dialog_add_control( dialog, rec, szStatic, 0 );
285     return ERROR_SUCCESS;
286 }
287
288 static UINT msi_dialog_button_control( msi_dialog *dialog, MSIRECORD *rec )
289 {
290     msi_control *control;
291
292     TRACE("%p %p\n", dialog, rec);
293
294     control = msi_dialog_add_control( dialog, rec, szButton, 0 );
295     control->handler = msi_dialog_button_handler;
296
297     return ERROR_SUCCESS;
298 }
299
300 static UINT msi_dialog_checkbox_control( msi_dialog *dialog, MSIRECORD *rec )
301 {
302     msi_control *control;
303     LPCWSTR prop;
304
305     TRACE("%p %p\n", dialog, rec);
306
307     control = msi_dialog_add_control( dialog, rec, szButton,
308                                       BS_CHECKBOX | BS_MULTILINE );
309     control->handler = msi_dialog_checkbox_handler;
310     prop = MSI_RecordGetString( rec, 9 );
311     if( prop )
312         control->property = strdupW( prop );
313     msi_dialog_checkbox_sync_state( dialog, control );
314
315     return ERROR_SUCCESS;
316 }
317
318 static UINT msi_dialog_line_control( msi_dialog *dialog, MSIRECORD *rec )
319 {
320     TRACE("%p %p\n", dialog, rec);
321
322     msi_dialog_add_control( dialog, rec, szStatic, SS_ETCHEDHORZ | SS_SUNKEN );
323     return ERROR_SUCCESS;
324 }
325
326 static UINT msi_dialog_scrolltext_control( msi_dialog *dialog, MSIRECORD *rec )
327 {
328     const static WCHAR szEdit[] = { 'E','D','I','T',0 };
329
330     TRACE("%p %p\n", dialog, rec);
331
332     msi_dialog_add_control( dialog, rec, szEdit, WS_BORDER |
333                  ES_MULTILINE | WS_VSCROLL | ES_READONLY | ES_AUTOVSCROLL );
334
335     return ERROR_SUCCESS;
336 }
337
338 static UINT msi_dialog_bitmap_control( msi_dialog *dialog, MSIRECORD *rec )
339 {
340     TRACE("%p %p\n", dialog, rec);
341
342     msi_dialog_add_control( dialog, rec, szStatic,
343                             SS_BITMAP | SS_LEFT | SS_CENTERIMAGE );
344     return ERROR_SUCCESS;
345 }
346
347 static UINT msi_dialog_combo_control( msi_dialog *dialog, MSIRECORD *rec )
348 {
349     static const WCHAR szCombo[] = { 'C','O','M','B','O','B','O','X',0 };
350
351     msi_dialog_add_control( dialog, rec, szCombo,
352                             SS_BITMAP | SS_LEFT | SS_CENTERIMAGE );
353     return ERROR_SUCCESS;
354 }
355
356 static UINT msi_dialog_edit_control( msi_dialog *dialog, MSIRECORD *rec )
357 {
358     const static WCHAR szEdit[] = { 'E','D','I','T',0 };
359     msi_control *control;
360     LPCWSTR prop;
361     LPWSTR val;
362
363     control = msi_dialog_add_control( dialog, rec, szEdit, WS_BORDER );
364     control->handler = msi_dialog_edit_handler;
365     prop = MSI_RecordGetString( rec, 9 );
366     if( prop )
367         control->property = strdupW( prop );
368     val = load_dynamic_property( dialog->package, control->property, NULL );
369     SetWindowTextW( control->hwnd, val );
370     HeapFree( GetProcessHeap(), 0, val );
371     return ERROR_SUCCESS;
372 }
373
374 static UINT msi_dialog_pathedit_control( msi_dialog *dialog, MSIRECORD *rec )
375 {
376     FIXME("not implemented properly\n");
377     return msi_dialog_edit_control( dialog, rec );
378 }
379
380 static UINT msi_dialog_create_radiobutton( MSIRECORD *rec, LPVOID param )
381 {
382     radio_button_group_descr *group = (radio_button_group_descr *)param;
383     msi_dialog *dialog = group->dialog;
384     msi_control *control;
385     LPCWSTR prop;
386     DWORD x, y, width, height, style;
387     DWORD attributes = group->attributes;
388     LPCWSTR text, name;
389     LPWSTR font = NULL, title = NULL;
390
391     style = WS_CHILD | BS_AUTORADIOBUTTON | BS_MULTILINE;
392     name = MSI_RecordGetString( rec, 3 );
393     control = HeapAlloc( GetProcessHeap(), 0,
394                          sizeof *control + strlenW(name)*sizeof(WCHAR) );
395     strcpyW( control->name, name );
396     control->next = dialog->control_list;
397     dialog->control_list = control;
398
399     x = MSI_RecordGetInteger( rec, 4 );
400     y = MSI_RecordGetInteger( rec, 5 );
401     width = MSI_RecordGetInteger( rec, 6 );
402     height = MSI_RecordGetInteger( rec, 7 );
403     text = MSI_RecordGetString( rec, 8 );
404
405     x = msi_dialog_scale_unit( dialog, x );
406     y = msi_dialog_scale_unit( dialog, y );
407     width = msi_dialog_scale_unit( dialog, width );
408     height = msi_dialog_scale_unit( dialog, height );
409
410     if( attributes & 1 )
411         style |= WS_VISIBLE;
412     if( ~attributes & 2 )
413         style |= WS_DISABLED;
414
415     if( text )
416     {
417         font = msi_dialog_get_style( &text );
418         deformat_string( dialog->package, text, &title );
419     }
420
421     control->hwnd = CreateWindowW( szButton, title, style, x, y, width, height,
422         group->parent->hwnd, NULL, NULL, NULL );
423
424     TRACE("Dialog %s control %s hwnd %p\n", debugstr_w(dialog->name), debugstr_w(text), control->hwnd);
425
426     msi_dialog_set_font( dialog, control->hwnd,
427             font ? font : dialog->default_font );
428
429     HeapFree( GetProcessHeap(), 0, font );
430     HeapFree( GetProcessHeap(), 0, title );
431
432     control->handler = msi_dialog_radiogroup_handler;
433
434     prop = MSI_RecordGetString( rec, 1 );
435     if( prop )
436         control->property = strdupW( prop );
437
438     return ERROR_SUCCESS;
439 }
440
441 static UINT msi_dialog_radiogroup_control( msi_dialog *dialog, MSIRECORD *rec )
442 {
443     static const WCHAR query[] = {
444         'S','E','L','E','C','T',' ','*',' ',
445         'F','R','O','M',' ','R','a','d','i','o','B','u','t','t','o','n',' ',
446         'W','H','E','R','E',' ',
447            '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
448     UINT r;
449     LPCWSTR prop;
450     msi_control *control;
451     MSIQUERY *view = NULL;
452     radio_button_group_descr group;
453     MSIPACKAGE *package = dialog->package;
454
455     prop = MSI_RecordGetString( rec, 9 );
456
457     TRACE("%p %p %s\n", dialog, rec, debugstr_w( prop ));
458
459     /* Create parent group box to hold radio buttons */
460     control = msi_dialog_add_control( dialog, rec, szButton, BS_OWNERDRAW );
461
462     if (control->hwnd)
463     {
464         WNDPROC oldproc = (WNDPROC) SetWindowLongPtrW(control->hwnd, GWLP_WNDPROC,
465             (LONG_PTR)MSIRadioGroup_WndProc);
466         SetPropW(control->hwnd, szButtonData, oldproc);
467     }
468
469     if( prop )
470         control->property = strdupW( prop );
471
472     /* query the Radio Button table for all control in this group */
473     r = MSI_OpenQuery( package->db, &view, query, prop );
474     if( r != ERROR_SUCCESS )
475     {
476         ERR("query failed for dialog %s radio group %s\n", 
477             debugstr_w(dialog->name), debugstr_w(prop));
478         return ERROR_INVALID_PARAMETER;
479     }
480
481     group.dialog = dialog;
482     group.parent = control;
483     group.attributes = MSI_RecordGetInteger( rec, 8 );
484
485     r = MSI_IterateRecords( view, 0, msi_dialog_create_radiobutton, &group );
486     msiobj_release( &view->hdr );
487
488     return r;
489 }
490
491 static const WCHAR szText[] = { 'T','e','x','t',0 };
492 static const WCHAR szPushButton[] = { 'P','u','s','h','B','u','t','t','o','n',0 };
493 static const WCHAR szLine[] = { 'L','i','n','e',0 };
494 static const WCHAR szBitmap[] = { 'B','i','t','m','a','p',0 };
495 static const WCHAR szCheckBox[] = { 'C','h','e','c','k','B','o','x',0 };
496 static const WCHAR szScrollableText[] = {
497     'S','c','r','o','l','l','a','b','l','e','T','e','x','t',0 };
498 static const WCHAR szComboBox[] = { 'C','o','m','b','o','B','o','x',0 };
499 static const WCHAR szEdit[] = { 'E','d','i','t',0 };
500 static const WCHAR szMaskedEdit[] = { 'M','a','s','k','e','d','E','d','i','t',0 };
501 static const WCHAR szPathEdit[] = { 'P','a','t','h','E','d','i','t',0 };
502 static const WCHAR szRadioButtonGroup[] = { 
503     'R','a','d','i','o','B','u','t','t','o','n','G','r','o','u','p',0 };
504
505 struct control_handler msi_dialog_handler[] =
506 {
507     { szText, msi_dialog_text_control },
508     { szPushButton, msi_dialog_button_control },
509     { szLine, msi_dialog_line_control },
510     { szBitmap, msi_dialog_bitmap_control },
511     { szCheckBox, msi_dialog_checkbox_control },
512     { szScrollableText, msi_dialog_scrolltext_control },
513     { szComboBox, msi_dialog_combo_control },
514     { szEdit, msi_dialog_edit_control },
515     { szMaskedEdit, msi_dialog_edit_control },
516     { szPathEdit, msi_dialog_pathedit_control },
517     { szRadioButtonGroup, msi_dialog_radiogroup_control },
518 };
519
520 #define NUM_CONTROL_TYPES (sizeof msi_dialog_handler/sizeof msi_dialog_handler[0])
521
522 static UINT msi_dialog_create_controls( MSIRECORD *rec, LPVOID param )
523 {
524     msi_dialog *dialog = param;
525     LPCWSTR control_type;
526     UINT i;
527
528     /* find and call the function that can create this type of control */
529     control_type = MSI_RecordGetString( rec, 3 );
530     for( i=0; i<NUM_CONTROL_TYPES; i++ )
531         if (!strcmpiW( msi_dialog_handler[i].control_type, control_type ))
532             break;
533     if( i != NUM_CONTROL_TYPES )
534         msi_dialog_handler[i].func( dialog, rec );
535     else
536         ERR("no handler for element type %s\n", debugstr_w(control_type));
537
538     return ERROR_SUCCESS;
539 }
540
541 static UINT msi_dialog_fill_controls( msi_dialog *dialog )
542 {
543     static const WCHAR query[] = {
544         'S','E','L','E','C','T',' ','*',' ',
545         'F','R','O','M',' ','C','o','n','t','r','o','l',' ',
546         'W','H','E','R','E',' ',
547            '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
548     UINT r;
549     MSIQUERY *view = NULL;
550     MSIPACKAGE *package = dialog->package;
551
552     TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
553
554     /* query the Control table for all the elements of the control */
555     r = MSI_OpenQuery( package->db, &view, query, dialog->name );
556     if( r != ERROR_SUCCESS )
557     {
558         ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
559         return ERROR_INVALID_PARAMETER;
560     }
561
562     r = MSI_IterateRecords( view, 0, msi_dialog_create_controls, dialog );
563     msiobj_release( &view->hdr );
564
565     return r;
566 }
567
568 static msi_control *msi_dialog_find_control( msi_dialog *dialog, LPCWSTR name )
569 {
570     msi_control *control;
571
572     for( control = dialog->control_list; control; control = control->next )
573         if( !strcmpW( control->name, name ) ) /* FIXME: case sensitive? */
574             break;
575     return control;
576 }
577
578 static msi_control *msi_dialog_find_control_by_hwnd( msi_dialog *dialog, HWND hwnd )
579 {
580     msi_control *control;
581
582     for( control = dialog->control_list; control; control = control->next )
583         if( hwnd == control->hwnd )
584             break;
585     return control;
586 }
587
588 static UINT msi_dialog_set_control_condition( MSIRECORD *rec, LPVOID param )
589 {
590     static const WCHAR szHide[] = { 'H','i','d','e',0 };
591     static const WCHAR szShow[] = { 'S','h','o','w',0 };
592     static const WCHAR szDisable[] = { 'D','i','s','a','b','l','e',0 };
593     static const WCHAR szEnable[] = { 'E','n','a','b','l','e',0 };
594     msi_dialog *dialog = param;
595     msi_control *control;
596     LPCWSTR name, action, condition;
597     UINT r;
598
599     name = MSI_RecordGetString( rec, 2 );
600     action = MSI_RecordGetString( rec, 3 );
601     condition = MSI_RecordGetString( rec, 4 );
602     r = MSI_EvaluateConditionW( dialog->package, condition );
603     control = msi_dialog_find_control( dialog, name );
604     if( r && control )
605     {
606         TRACE("%s control %s\n", debugstr_w(action), debugstr_w(name));
607
608         /* FIXME: case sensitive? */
609         if(!strcmpW(action, szHide))
610             ShowWindow(control->hwnd, SW_HIDE);
611         else if(!strcmpW(action, szShow))
612             ShowWindow(control->hwnd, SW_SHOW);
613         else if(!strcmpW(action, szDisable))
614             EnableWindow(control->hwnd, FALSE);
615         else if(!strcmpW(action, szEnable))
616             EnableWindow(control->hwnd, TRUE);
617         else
618             FIXME("Unhandled action %s\n", debugstr_w(action));
619     }
620
621     return ERROR_SUCCESS;
622 }
623
624 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog )
625 {
626     static const WCHAR query[] = {
627       'S','E','L','E','C','T',' ','*',' ',
628       'F','R','O','M',' ',
629         'C','o','n','t','r','o','l','C','o','n','d','i','t','i','o','n',' ',
630       'W','H','E','R','E',' ',
631         '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0
632     };
633     UINT r;
634     MSIQUERY *view = NULL;
635     MSIPACKAGE *package = dialog->package;
636
637     TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
638
639     /* query the Control table for all the elements of the control */
640     r = MSI_OpenQuery( package->db, &view, query, dialog->name );
641     if( r != ERROR_SUCCESS )
642     {
643         ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
644         return ERROR_INVALID_PARAMETER;
645     }
646
647     r = MSI_IterateRecords( view, 0, msi_dialog_set_control_condition, dialog );
648     msiobj_release( &view->hdr );
649
650     return r;
651 }
652
653 /* figure out the height of 10 point MS Sans Serif */
654 static INT msi_dialog_get_sans_serif_height( HWND hwnd )
655 {
656     static const WCHAR szSansSerif[] = {
657         'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0 };
658     LOGFONTW lf;
659     TEXTMETRICW tm;
660     BOOL r;
661     LONG height = 0;
662     HFONT hFont, hOldFont;
663     HDC hdc;
664
665     hdc = GetDC( hwnd );
666     if (hdc)
667     {
668         memset( &lf, 0, sizeof lf );
669         lf.lfHeight = MulDiv(10, GetDeviceCaps(hdc, LOGPIXELSY), 72);
670         strcpyW( lf.lfFaceName, szSansSerif );
671         hFont = CreateFontIndirectW(&lf);
672         if (hFont)
673         {
674             hOldFont = SelectObject( hdc, hFont );
675             r = GetTextMetricsW( hdc, &tm );
676             if (r)
677                 height = tm.tmHeight;
678             SelectObject( hdc, hOldFont );
679             DeleteObject( hFont );
680         }
681         ReleaseDC( hwnd, hdc );
682     }
683     return height;
684 }
685
686 static LRESULT msi_dialog_oncreate( HWND hwnd, LPCREATESTRUCTW cs )
687 {
688     static const WCHAR query[] = {
689         'S','E','L','E','C','T',' ','*',' ',
690         'F','R','O','M',' ','D','i','a','l','o','g',' ',
691         'W','H','E','R','E',' ',
692            '`','D','i','a','l','o','g','`',' ','=',' ','\'','%','s','\'',0};
693     static const WCHAR df[] = {
694         'D','e','f','a','u','l','t','U','I','F','o','n','t',0 };
695     msi_dialog *dialog = (msi_dialog*) cs->lpCreateParams;
696     MSIPACKAGE *package = dialog->package;
697     MSIQUERY *view = NULL;
698     MSIRECORD *rec = NULL;
699     DWORD width, height;
700     LPCWSTR text;
701     LPWSTR title = NULL;
702     UINT r;
703
704     TRACE("%p %p\n", dialog, package);
705
706     dialog->hwnd = hwnd;
707     SetWindowLongPtrW( hwnd, GWLP_USERDATA, (LONG_PTR) dialog );
708
709     /* fetch the associated record from the Dialog table */
710     r = MSI_OpenQuery( package->db, &view, query, dialog->name );
711     if( r != ERROR_SUCCESS )
712     {
713         ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
714         return -1;
715     }
716     MSI_ViewExecute( view, NULL );
717     MSI_ViewFetch( view, &rec );
718     MSI_ViewClose( view );
719     msiobj_release( &view->hdr );
720
721     if( !rec )
722     {
723         TRACE("No record found for dialog %s\n", debugstr_w(dialog->name));
724         return -1;
725     }
726
727     dialog->scale = msi_dialog_get_sans_serif_height(dialog->hwnd);
728
729     width = MSI_RecordGetInteger( rec, 4 );
730     height = MSI_RecordGetInteger( rec, 5 );
731     dialog->attributes = MSI_RecordGetInteger( rec, 6 );
732     text = MSI_RecordGetString( rec, 7 );
733
734     width = msi_dialog_scale_unit( dialog, width );
735     height = msi_dialog_scale_unit( dialog, height ) + 25; /* FIXME */
736
737     dialog->default_font = load_dynamic_property( dialog->package, df, NULL );
738
739     deformat_string( dialog->package, text, &title );
740     SetWindowTextW( hwnd, title );
741     SetWindowPos( hwnd, 0, 0, 0, width, height,
742                   SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW );
743
744     HeapFree( GetProcessHeap(), 0, title );
745     msiobj_release( &rec->hdr );
746
747     msi_dialog_build_font_list( dialog );
748     msi_dialog_fill_controls( dialog );
749     msi_dialog_evaluate_control_conditions( dialog );
750
751     return 0;
752 }
753
754 static UINT msi_dialog_send_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
755 {
756     LPWSTR event_fmt = NULL, arg_fmt = NULL;
757
758     TRACE("Sending control event %s %s\n", debugstr_w(event), debugstr_w(arg));
759
760     deformat_string( dialog->package, event, &event_fmt );
761     deformat_string( dialog->package, arg, &arg_fmt );
762
763     dialog->event_handler( dialog->package, event_fmt, arg_fmt, dialog );
764
765     HeapFree( GetProcessHeap(), 0, event_fmt );
766     HeapFree( GetProcessHeap(), 0, arg_fmt );
767
768     return ERROR_SUCCESS;
769 }
770
771 static UINT msi_dialog_set_property( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
772 {
773     static const WCHAR szNullArg[] = { '{','}',0 };
774     LPWSTR p, prop, arg_fmt = NULL;
775     UINT len;
776
777     len = strlenW(event);
778     prop = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR));
779     strcpyW( prop, &event[1] );
780     p = strchrW( prop, ']' );
781     if( p && p[1] == 0 )
782     {
783         *p = 0;
784         if( strcmpW( szNullArg, arg ) )
785             deformat_string( dialog->package, arg, &arg_fmt );
786         MSI_SetPropertyW( dialog->package, prop, arg_fmt );
787     }
788     else
789         ERR("Badly formatted property string - what happens?\n");
790     HeapFree( GetProcessHeap(), 0, prop );
791     return ERROR_SUCCESS;
792 }
793
794 static UINT msi_dialog_control_event( MSIRECORD *rec, LPVOID param )
795 {
796     msi_dialog *dialog = param;
797     LPCWSTR condition, event, arg;
798     UINT r;
799
800     condition = MSI_RecordGetString( rec, 5 );
801     r = MSI_EvaluateConditionW( dialog->package, condition );
802     if( r )
803     {
804         event = MSI_RecordGetString( rec, 3 );
805         arg = MSI_RecordGetString( rec, 4 );
806         if( event[0] == '[' )
807             msi_dialog_set_property( dialog, event, arg );
808         else
809             msi_dialog_send_event( dialog, event, arg );
810     }
811
812     return ERROR_SUCCESS;
813 }
814
815 static UINT msi_dialog_button_handler( msi_dialog *dialog,
816                                        msi_control *control, WPARAM param )
817 {
818     static const WCHAR query[] = {
819       'S','E','L','E','C','T',' ','*',' ',
820       'F','R','O','M',' ','C','o','n','t','r','o','l','E','v','e','n','t',' ',
821       'W','H','E','R','E',' ',
822          '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
823       'A','N','D',' ',
824          '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',' ',
825       'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','i','n','g','`',0
826     };
827     MSIQUERY *view = NULL;
828     UINT r;
829
830     if( HIWORD(param) != BN_CLICKED )
831         return ERROR_SUCCESS;
832
833     r = MSI_OpenQuery( dialog->package->db, &view, query,
834                        dialog->name, control->name );
835     if( r != ERROR_SUCCESS )
836     {
837         ERR("query failed\n");
838         return 0;
839     }
840
841     r = MSI_IterateRecords( view, 0, msi_dialog_control_event, dialog );
842     msiobj_release( &view->hdr );
843
844     return r;
845 }
846
847 static UINT msi_dialog_get_checkbox_state( msi_dialog *dialog,
848                 msi_control *control )
849 {
850     WCHAR state[2] = { 0 };
851     DWORD sz = 2;
852
853     MSI_GetPropertyW( dialog->package, control->property, state, &sz );
854     return atoiW( state ) ? 1 : 0;
855 }
856
857 static void msi_dialog_set_checkbox_state( msi_dialog *dialog,
858                 msi_control *control, UINT state )
859 {
860     WCHAR szState[2] = { '0', 0 };
861
862     if( state )
863         szState[0]++;
864     MSI_SetPropertyW( dialog->package, control->property, szState );
865 }
866
867 static void msi_dialog_checkbox_sync_state( msi_dialog *dialog,
868                 msi_control *control )
869 {
870     UINT state;
871
872     state = msi_dialog_get_checkbox_state( dialog, control );
873     SendMessageW( control->hwnd, BM_SETCHECK,
874                   state ? BST_CHECKED : BST_UNCHECKED, 0 );
875 }
876
877 static UINT msi_dialog_checkbox_handler( msi_dialog *dialog,
878                 msi_control *control, WPARAM param )
879 {
880     UINT state;
881
882     if( HIWORD(param) != BN_CLICKED )
883         return ERROR_SUCCESS;
884
885     TRACE("clicked checkbox %s, set %s\n", debugstr_w(control->name),
886           debugstr_w(control->property));
887
888     state = msi_dialog_get_checkbox_state( dialog, control );
889     state = state ? 0 : 1;
890     msi_dialog_set_checkbox_state( dialog, control, state );
891     msi_dialog_checkbox_sync_state( dialog, control );
892
893     return msi_dialog_button_handler( dialog, control, param );
894 }
895
896 static UINT msi_dialog_edit_handler( msi_dialog *dialog,
897                 msi_control *control, WPARAM param )
898 {
899     UINT sz, r;
900     LPWSTR buf;
901
902     if( HIWORD(param) != EN_CHANGE )
903         return ERROR_SUCCESS;
904
905     TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
906           debugstr_w(control->property));
907
908     sz = 0x20;
909     buf = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
910     while( buf )
911     {
912         r = GetWindowTextW( control->hwnd, buf, sz );
913         if( r < (sz-1) )
914             break;
915             sz *= 2;
916         buf = HeapReAlloc( GetProcessHeap(), 0, buf, sz*sizeof(WCHAR) );
917     }
918
919     MSI_SetPropertyW( dialog->package, control->property, buf );
920
921     HeapFree( GetProcessHeap(), 0, buf );
922
923     return ERROR_SUCCESS;
924 }
925
926 static UINT msi_dialog_radiogroup_handler( msi_dialog *dialog,
927                 msi_control *control, WPARAM param )
928 {
929     if( HIWORD(param) != BN_CLICKED )
930         return ERROR_SUCCESS;
931
932     TRACE("clicked radio button %s, set %s\n", debugstr_w(control->name),
933           debugstr_w(control->property));
934
935     MSI_SetPropertyW( dialog->package, control->property, control->name );
936
937     return msi_dialog_button_handler( dialog, control, param );
938 }
939
940 static LRESULT msi_dialog_oncommand( msi_dialog *dialog, WPARAM param, HWND hwnd )
941 {
942     msi_control *control;
943
944     TRACE("%p %p %08x\n", dialog, hwnd, param);
945
946     control = msi_dialog_find_control_by_hwnd( dialog, hwnd );
947     if( control )
948     {
949         if( control->handler )
950         {
951             control->handler( dialog, control, param );
952             msi_dialog_evaluate_control_conditions( dialog );
953         }
954     }
955     else
956         ERR("button click from nowhere\n");
957     return 0;
958 }
959
960 static LRESULT WINAPI MSIDialog_WndProc( HWND hwnd, UINT msg,
961                 WPARAM wParam, LPARAM lParam )
962 {
963     msi_dialog *dialog = (LPVOID) GetWindowLongPtrW( hwnd, GWLP_USERDATA );
964
965     TRACE(" 0x%04x\n", msg);
966     switch (msg)
967     {
968     case WM_CREATE:
969         return msi_dialog_oncreate( hwnd, (LPCREATESTRUCTW)lParam );
970
971     case WM_COMMAND:
972         return msi_dialog_oncommand( dialog, wParam, (HWND)lParam );
973
974     case WM_DESTROY:
975         dialog->hwnd = NULL;
976         return 0;
977     }
978     return DefWindowProcW(hwnd, msg, wParam, lParam);
979 }
980
981 /* functions that interface to other modules within MSI */
982
983 msi_dialog *msi_dialog_create( MSIPACKAGE* package, LPCWSTR szDialogName,
984                                 msi_dialog_event_handler event_handler )
985 {
986     msi_dialog *dialog;
987     HWND hwnd;
988
989     TRACE("%p %s\n", package, debugstr_w(szDialogName));
990
991     /* allocate the structure for the dialog to use */
992     dialog = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
993                         sizeof *dialog + sizeof(WCHAR)*strlenW(szDialogName) );
994     if( !dialog )
995         return NULL;
996     strcpyW( dialog->name, szDialogName );
997     dialog->package = package;
998     dialog->event_handler = event_handler;
999
1000     /* create the dialog window, don't show it yet */
1001     hwnd = CreateWindowW( szMsiDialogClass, szDialogName, WS_OVERLAPPEDWINDOW,
1002                      CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
1003                      NULL, NULL, NULL, dialog );
1004     if( !hwnd )
1005     {
1006         ERR("Failed to create dialog %s\n", debugstr_w( szDialogName ));
1007         msi_dialog_destroy( dialog );
1008         return NULL;
1009     }
1010
1011     return dialog;
1012 }
1013
1014 void msi_dialog_end_dialog( msi_dialog *dialog )
1015 {
1016     dialog->finished = 1;
1017 }
1018
1019 UINT msi_dialog_run_message_loop( msi_dialog *dialog )
1020 {
1021     MSG msg;
1022
1023     if( dialog->attributes & msidbDialogAttributesVisible )
1024     {
1025         ShowWindow( dialog->hwnd, SW_SHOW );
1026         UpdateWindow( dialog->hwnd );
1027     }
1028
1029     if( dialog->attributes & msidbDialogAttributesModal )
1030     {
1031         while( !dialog->finished && GetMessageW( &msg, 0, 0, 0 ) )
1032         {
1033             TranslateMessage( &msg );
1034             DispatchMessageW( &msg );
1035         }
1036     }
1037     else
1038         return ERROR_IO_PENDING;
1039
1040     return ERROR_SUCCESS;
1041 }
1042
1043 void msi_dialog_check_messages( msi_dialog *dialog, HANDLE handle )
1044 {
1045     MSG msg;
1046     DWORD r;
1047
1048     do
1049     {
1050         while( PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ) )
1051         {
1052             TranslateMessage( &msg );
1053             DispatchMessageW( &msg );
1054         }
1055         if( !handle )
1056             break;
1057         r = MsgWaitForMultipleObjects( 1, &handle, 0, INFINITE, QS_ALLEVENTS );
1058     }
1059     while( WAIT_OBJECT_0 != r );
1060 }
1061
1062 void msi_dialog_do_preview( msi_dialog *dialog )
1063 {
1064     dialog->attributes |= msidbDialogAttributesVisible;
1065     dialog->attributes &= ~msidbDialogAttributesModal;
1066     msi_dialog_run_message_loop( dialog );
1067 }
1068
1069 void msi_dialog_destroy( msi_dialog *dialog )
1070 {
1071     if( dialog->hwnd )
1072         ShowWindow( dialog->hwnd, SW_HIDE );
1073     
1074     /* destroy the list of controls */
1075     while( dialog->control_list )
1076     {
1077         msi_control *t = dialog->control_list;
1078         dialog->control_list = t->next;
1079         /* leave dialog->hwnd - destroying parent destroys child windows */
1080         HeapFree( GetProcessHeap(), 0, t->property );
1081         HeapFree( GetProcessHeap(), 0, t );
1082     }
1083
1084     /* destroy the list of fonts */
1085     while( dialog->font_list )
1086     {
1087         msi_font *t = dialog->font_list;
1088         dialog->font_list = t->next;
1089         DeleteObject( t->hfont );
1090         HeapFree( GetProcessHeap(), 0, t );
1091     }
1092     HeapFree( GetProcessHeap(), 0, dialog->default_font );
1093
1094     if( dialog->hwnd )
1095         DestroyWindow( dialog->hwnd );
1096
1097     dialog->package = NULL;
1098     HeapFree( GetProcessHeap(), 0, dialog );
1099 }
1100
1101 void msi_dialog_register_class( void )
1102 {
1103     WNDCLASSW cls;
1104
1105     ZeroMemory( &cls, sizeof cls );
1106     cls.lpfnWndProc   = MSIDialog_WndProc;
1107     cls.hInstance     = NULL;
1108     cls.hIcon         = LoadIconW(0, (LPWSTR)IDI_APPLICATION);
1109     cls.hCursor       = LoadCursorW(0, (LPWSTR)IDC_ARROW);
1110     cls.hbrBackground = (HBRUSH)(COLOR_WINDOW);
1111     cls.lpszMenuName  = NULL;
1112     cls.lpszClassName = szMsiDialogClass;
1113
1114     RegisterClassW( &cls );
1115 }
1116
1117 void msi_dialog_unregister_class( void )
1118 {
1119     UnregisterClassW( szMsiDialogClass, NULL );
1120 }
1121
1122 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1123 {
1124     WNDPROC oldproc = (WNDPROC) GetPropW(hWnd, szButtonData);
1125
1126     TRACE("hWnd %p msg %04x wParam 0x%08x lParam 0x%08lx\n", hWnd, msg, wParam, lParam);
1127
1128     if (msg == WM_COMMAND) /* Forward notifications to dialog */
1129         SendMessageW(GetParent(hWnd), msg, wParam, lParam);
1130
1131     return CallWindowProcW(oldproc, hWnd, msg, wParam, lParam);
1132 }