Added parser template and made AVISplitter use it.
[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
45 struct msi_control_tag;
46 typedef struct msi_control_tag msi_control;
47 typedef UINT (*msi_click_handler)( msi_dialog *, msi_control * );
48
49 struct msi_control_tag
50 {
51     struct msi_control_tag *next;
52     HWND hwnd;
53     msi_click_handler click_handler;
54     LPWSTR property;
55     WCHAR name[1];
56 };
57
58 typedef struct msi_font_tag
59 {
60     struct msi_font_tag *next;
61     HFONT hfont;
62     WCHAR name[1];
63 } msi_font;
64
65 struct msi_dialog_tag
66 {
67     MSIPACKAGE *package;
68     msi_dialog_event_handler event_handler;
69     BOOL finished;
70     INT scale;
71     DWORD attributes;
72     HWND hwnd;
73     LPWSTR default_font;
74     msi_font *font_list;
75     msi_control *control_list;
76     WCHAR name[1];
77 };
78
79 typedef UINT (*msi_dialog_control_func)( msi_dialog *dialog, MSIRECORD *rec );
80 struct control_handler 
81 {
82     LPCWSTR control_type;
83     msi_dialog_control_func func;
84 };
85
86 static UINT msi_dialog_checkbox_click( msi_dialog *, msi_control * );
87 static void msi_dialog_checkbox_sync_state( msi_dialog *, msi_control * );
88 static UINT msi_dialog_button_click( msi_dialog *, msi_control * );
89
90
91 INT msi_dialog_scale_unit( msi_dialog *dialog, INT val )
92 {
93     return (dialog->scale * val + 5) / 10;
94 }
95
96 /*
97  * msi_dialog_get_style
98  *
99  * Extract the {\style} string from the front of the text to display and
100  *  update the pointer.
101  */
102 static LPWSTR msi_dialog_get_style( LPCWSTR *text )
103 {
104     LPWSTR ret = NULL;
105     LPCWSTR p = *text, q;
106     DWORD len;
107
108     if( *p++ != '{' )
109         return ret;
110     q = strchrW( p, '}' );
111     if( !q )
112         return ret;
113     *text = ++q;
114     if( *p++ != '\\' )
115         return ret;
116     len = q - p;
117     
118     ret = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
119     if( !ret )
120         return ret;
121     strncpyW( ret, p, len );
122     ret[len-1] = 0;
123     return ret;
124 }
125
126 static UINT msi_dialog_add_font( MSIRECORD *rec, LPVOID param )
127 {
128     msi_dialog *dialog = param;
129     msi_font *font;
130     LPCWSTR face, name;
131     LOGFONTW lf;
132     INT style;
133     HDC hdc;
134
135     /* create a font and add it to the list */
136     name = MSI_RecordGetString( rec, 1 );
137     font = HeapAlloc( GetProcessHeap(), 0,
138                       sizeof *font + strlenW( name )*sizeof (WCHAR) );
139     strcpyW( font->name, name );
140     font->next = dialog->font_list;
141     dialog->font_list = font;
142
143     memset( &lf, 0, sizeof lf );
144     face = MSI_RecordGetString( rec, 2 );
145     lf.lfHeight = MSI_RecordGetInteger( rec, 3 );
146     style = MSI_RecordGetInteger( rec, 5 );
147     if( style & msidbTextStyleStyleBitsBold )
148         lf.lfWeight = FW_BOLD;
149     if( style & msidbTextStyleStyleBitsItalic )
150         lf.lfItalic = TRUE;
151     if( style & msidbTextStyleStyleBitsUnderline )
152         lf.lfUnderline = TRUE;
153     if( style & msidbTextStyleStyleBitsStrike )
154         lf.lfStrikeOut = TRUE;
155     lstrcpynW( lf.lfFaceName, face, LF_FACESIZE );
156
157     /* adjust the height */
158     hdc = GetDC( dialog->hwnd );
159     if (hdc)
160     {
161         lf.lfHeight = -MulDiv(lf.lfHeight, GetDeviceCaps(hdc, LOGPIXELSY), 72);
162         ReleaseDC( dialog->hwnd, hdc );
163     }
164
165     font->hfont = CreateFontIndirectW( &lf );
166
167     TRACE("Adding font style %s\n", debugstr_w(font->name) );
168
169     return ERROR_SUCCESS;
170 }
171
172 static msi_font *msi_dialog_find_font( msi_dialog *dialog, LPCWSTR name )
173 {
174     msi_font *font;
175
176     for( font = dialog->font_list; font; font = font->next )
177         if( !strcmpW( font->name, name ) )  /* FIXME: case sensitive? */
178             break;
179
180     return font;
181 }
182
183 static UINT msi_dialog_set_font( msi_dialog *dialog, HWND hwnd, LPCWSTR name )
184 {
185     msi_font *font;
186
187     font = msi_dialog_find_font( dialog, name );
188     if( font )
189         SendMessageW( hwnd, WM_SETFONT, (WPARAM) font->hfont, TRUE );
190     else
191         ERR("No font entry for %s\n", debugstr_w(name));
192     return ERROR_SUCCESS;
193 }
194
195 static UINT msi_dialog_build_font_list( msi_dialog *dialog )
196 {
197     static const WCHAR query[] = {
198       'S','E','L','E','C','T',' ','*',' ',
199       'F','R','O','M',' ','`','T','e','x','t','S','t','y','l','e','`',' ',0
200     };
201     UINT r;
202     MSIQUERY *view = NULL;
203
204     TRACE("dialog %p\n", dialog );
205
206     r = MSI_OpenQuery( dialog->package->db, &view, query );
207     if( r != ERROR_SUCCESS )
208         return r;
209
210     r = MSI_IterateRecords( view, NULL, msi_dialog_add_font, dialog );
211     msiobj_release( &view->hdr );
212
213     return r;
214 }
215
216 static msi_control *msi_dialog_add_control( msi_dialog *dialog,
217                 MSIRECORD *rec, LPCWSTR szCls, DWORD style )
218 {
219     DWORD x, y, width, height;
220     LPCWSTR text, name;
221     LPWSTR font = NULL, title = NULL;
222     msi_control *control = NULL;
223
224     style |= WS_CHILD | WS_VISIBLE | WS_GROUP;
225
226     name = MSI_RecordGetString( rec, 2 );
227     control = HeapAlloc( GetProcessHeap(), 0,
228                          sizeof *control + strlenW(name)*sizeof(WCHAR) );
229     strcpyW( control->name, name );
230     control->next = dialog->control_list;
231     dialog->control_list = control;
232     control->click_handler = NULL;
233     control->property = NULL;
234
235     x = MSI_RecordGetInteger( rec, 4 );
236     y = MSI_RecordGetInteger( rec, 5 );
237     width = MSI_RecordGetInteger( rec, 6 );
238     height = MSI_RecordGetInteger( rec, 7 );
239     text = MSI_RecordGetString( rec, 10 );
240
241     TRACE("Dialog %s control %s\n", debugstr_w(dialog->name), debugstr_w(text));
242
243     x = msi_dialog_scale_unit( dialog, x );
244     y = msi_dialog_scale_unit( dialog, y );
245     width = msi_dialog_scale_unit( dialog, width );
246     height = msi_dialog_scale_unit( dialog, height );
247
248     if( text )
249     {
250         font = msi_dialog_get_style( &text );
251         deformat_string( dialog->package, text, &title );
252     }
253     control->hwnd = CreateWindowW( szCls, title, style,
254                           x, y, width, height, dialog->hwnd, NULL, NULL, NULL );
255     msi_dialog_set_font( dialog, control->hwnd,
256                          font ? font : dialog->default_font );
257     HeapFree( GetProcessHeap(), 0, font );
258     HeapFree( GetProcessHeap(), 0, title );
259     return control;
260 }
261
262 static UINT msi_dialog_text_control( msi_dialog *dialog, MSIRECORD *rec )
263 {
264     TRACE("%p %p\n", dialog, rec);
265
266     msi_dialog_add_control( dialog, rec, szStatic, 0 );
267     return ERROR_SUCCESS;
268 }
269
270 static UINT msi_dialog_button_control( msi_dialog *dialog, MSIRECORD *rec )
271 {
272     const static WCHAR szButton[] = { 'B','U','T','T','O','N', 0 };
273     msi_control *control;
274
275     TRACE("%p %p\n", dialog, rec);
276
277     control = msi_dialog_add_control( dialog, rec, szButton, 0 );
278     control->click_handler = msi_dialog_button_click;
279
280     return ERROR_SUCCESS;
281 }
282
283 static UINT msi_dialog_checkbox_control( msi_dialog *dialog, MSIRECORD *rec )
284 {
285     const static WCHAR szButton[] = { 'B','U','T','T','O','N', 0 };
286     msi_control *control;
287     LPCWSTR prop;
288
289     TRACE("%p %p\n", dialog, rec);
290
291     control = msi_dialog_add_control( dialog, rec, szButton,
292                                       BS_CHECKBOX | BS_MULTILINE );
293     control->click_handler = msi_dialog_checkbox_click;
294     prop = MSI_RecordGetString( rec, 9 );
295     if( prop )
296         control->property = dupstrW( prop );
297     msi_dialog_checkbox_sync_state( dialog, control );
298
299     return ERROR_SUCCESS;
300 }
301
302 static UINT msi_dialog_line_control( msi_dialog *dialog, MSIRECORD *rec )
303 {
304     TRACE("%p %p\n", dialog, rec);
305
306     msi_dialog_add_control( dialog, rec, szStatic, SS_ETCHEDHORZ | SS_SUNKEN );
307     return ERROR_SUCCESS;
308 }
309
310 static UINT msi_dialog_scrolltext_control( msi_dialog *dialog, MSIRECORD *rec )
311 {
312     const static WCHAR szEdit[] = { 'E','D','I','T',0 };
313
314     TRACE("%p %p\n", dialog, rec);
315
316     msi_dialog_add_control( dialog, rec, szEdit,
317                  ES_MULTILINE | WS_VSCROLL | ES_READONLY | ES_AUTOVSCROLL );
318
319     return ERROR_SUCCESS;
320 }
321
322 static UINT msi_dialog_bitmap_control( msi_dialog *dialog, MSIRECORD *rec )
323 {
324     TRACE("%p %p\n", dialog, rec);
325
326     msi_dialog_add_control( dialog, rec, szStatic,
327                             SS_BITMAP | SS_LEFT | SS_CENTERIMAGE );
328     return ERROR_SUCCESS;
329 }
330
331 static const WCHAR szText[] = { 'T','e','x','t',0 };
332 static const WCHAR szButton[] = { 'P','u','s','h','B','u','t','t','o','n',0 };
333 static const WCHAR szLine[] = { 'L','i','n','e',0 };
334 static const WCHAR szBitmap[] = { 'B','i','t','m','a','p',0 };
335 static const WCHAR szCheckBox[] = { 'C','h','e','c','k','B','o','x',0 };
336 static const WCHAR szScrollableText[] = {
337     'S','c','r','o','l','l','a','b','l','e','T','e','x','t',0 };
338
339 struct control_handler msi_dialog_handler[] =
340 {
341     { szText, msi_dialog_text_control },
342     { szButton, msi_dialog_button_control },
343     { szLine, msi_dialog_line_control },
344     { szBitmap, msi_dialog_bitmap_control },
345     { szCheckBox, msi_dialog_checkbox_control },
346     { szScrollableText, msi_dialog_scrolltext_control },
347 };
348
349 #define NUM_CONTROL_TYPES (sizeof msi_dialog_handler/sizeof msi_dialog_handler[0])
350
351 static UINT msi_dialog_create_controls( MSIRECORD *rec, LPVOID param )
352 {
353     msi_dialog *dialog = param;
354     LPCWSTR control_type;
355     UINT i;
356
357     /* find and call the function that can create this type of control */
358     control_type = MSI_RecordGetString( rec, 3 );
359     for( i=0; i<NUM_CONTROL_TYPES; i++ )
360         if (!strcmpiW( msi_dialog_handler[i].control_type, control_type ))
361             break;
362     if( i != NUM_CONTROL_TYPES )
363         msi_dialog_handler[i].func( dialog, rec );
364     else
365         ERR("no handler for element type %s\n", debugstr_w(control_type));
366
367     return ERROR_SUCCESS;
368 }
369
370 static UINT msi_dialog_fill_controls( msi_dialog *dialog )
371 {
372     static const WCHAR query[] = {
373         'S','E','L','E','C','T',' ','*',' ',
374         'F','R','O','M',' ','C','o','n','t','r','o','l',' ',
375         'W','H','E','R','E',' ',
376            '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
377     UINT r;
378     MSIQUERY *view = NULL;
379     MSIPACKAGE *package = dialog->package;
380
381     TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
382
383     /* query the Control table for all the elements of the control */
384     r = MSI_OpenQuery( package->db, &view, query, dialog->name );
385     if( r != ERROR_SUCCESS )
386     {
387         ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
388         return ERROR_INVALID_PARAMETER;
389     }
390
391     r = MSI_IterateRecords( view, 0, msi_dialog_create_controls, dialog );
392     msiobj_release( &view->hdr );
393
394     return r;
395 }
396
397 static msi_control *msi_dialog_find_control( msi_dialog *dialog, LPCWSTR name )
398 {
399     msi_control *control;
400
401     for( control = dialog->control_list; control; control = control->next )
402         if( !strcmpW( control->name, name ) ) /* FIXME: case sensitive? */
403             break;
404     return control;
405 }
406
407 static msi_control *msi_dialog_find_control_by_hwnd( msi_dialog *dialog, HWND hwnd )
408 {
409     msi_control *control;
410
411     for( control = dialog->control_list; control; control = control->next )
412         if( hwnd == control->hwnd )
413             break;
414     return control;
415 }
416
417 static UINT msi_dialog_set_control_condition( MSIRECORD *rec, LPVOID param )
418 {
419     static const WCHAR szHide[] = { 'H','i','d','e',0 };
420     static const WCHAR szShow[] = { 'S','h','o','w',0 };
421     static const WCHAR szDisable[] = { 'D','i','s','a','b','l','e',0 };
422     static const WCHAR szEnable[] = { 'E','n','a','b','l','e',0 };
423     msi_dialog *dialog = param;
424     msi_control *control;
425     LPCWSTR name, action, condition;
426     UINT r;
427
428     name = MSI_RecordGetString( rec, 2 );
429     action = MSI_RecordGetString( rec, 3 );
430     condition = MSI_RecordGetString( rec, 4 );
431     r = MSI_EvaluateConditionW( dialog->package, condition );
432     control = msi_dialog_find_control( dialog, name );
433     if( r && control )
434     {
435         TRACE("%s control %s\n", debugstr_w(action), debugstr_w(name));
436
437         /* FIXME: case sensitive? */
438         if(!strcmpW(action, szHide))
439             ShowWindow(control->hwnd, SW_HIDE);
440         else if(!strcmpW(action, szShow))
441             ShowWindow(control->hwnd, SW_SHOW);
442         else if(!strcmpW(action, szDisable))
443             EnableWindow(control->hwnd, FALSE);
444         else if(!strcmpW(action, szEnable))
445             EnableWindow(control->hwnd, TRUE);
446         else
447             FIXME("Unhandled action %s\n", debugstr_w(action));
448     }
449
450     return ERROR_SUCCESS;
451 }
452
453 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog )
454 {
455     static const WCHAR query[] = {
456       'S','E','L','E','C','T',' ','*',' ',
457       'F','R','O','M',' ',
458         'C','o','n','t','r','o','l','C','o','n','d','i','t','i','o','n',' ',
459       'W','H','E','R','E',' ',
460         '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0
461     };
462     UINT r;
463     MSIQUERY *view = NULL;
464     MSIPACKAGE *package = dialog->package;
465
466     TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
467
468     /* query the Control table for all the elements of the control */
469     r = MSI_OpenQuery( package->db, &view, query, dialog->name );
470     if( r != ERROR_SUCCESS )
471     {
472         ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
473         return ERROR_INVALID_PARAMETER;
474     }
475
476     r = MSI_IterateRecords( view, 0, msi_dialog_set_control_condition, dialog );
477     msiobj_release( &view->hdr );
478
479     return r;
480 }
481
482 /* figure out the height of 10 point MS Sans Serif */
483 static INT msi_dialog_get_sans_serif_height( HWND hwnd )
484 {
485     static const WCHAR szSansSerif[] = {
486         'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0 };
487     LOGFONTW lf;
488     TEXTMETRICW tm;
489     BOOL r;
490     LONG height = 0;
491     HFONT hFont, hOldFont;
492     HDC hdc;
493
494     hdc = GetDC( hwnd );
495     if (hdc)
496     {
497         memset( &lf, 0, sizeof lf );
498         lf.lfHeight = MulDiv(10, GetDeviceCaps(hdc, LOGPIXELSY), 72);
499         strcpyW( lf.lfFaceName, szSansSerif );
500         hFont = CreateFontIndirectW(&lf);
501         if (hFont)
502         {
503             hOldFont = SelectObject( hdc, hFont );
504             r = GetTextMetricsW( hdc, &tm );
505             if (r)
506                 height = tm.tmHeight;
507             SelectObject( hdc, hOldFont );
508             DeleteObject( hFont );
509         }
510         ReleaseDC( hwnd, hdc );
511     }
512     return height;
513 }
514
515 static LRESULT msi_dialog_oncreate( HWND hwnd, LPCREATESTRUCTW cs )
516 {
517     static const WCHAR query[] = {
518         'S','E','L','E','C','T',' ','*',' ',
519         'F','R','O','M',' ','D','i','a','l','o','g',' ',
520         'W','H','E','R','E',' ',
521            '`','D','i','a','l','o','g','`',' ','=',' ','\'','%','s','\'',0};
522     static const WCHAR df[] = {
523         'D','e','f','a','u','l','t','U','I','F','o','n','t',0 };
524     msi_dialog *dialog = (msi_dialog*) cs->lpCreateParams;
525     MSIPACKAGE *package = dialog->package;
526     MSIQUERY *view = NULL;
527     MSIRECORD *rec = NULL;
528     DWORD width, height;
529     LPCWSTR text;
530     LPWSTR title = NULL;
531     UINT r;
532
533     TRACE("%p %p\n", dialog, package);
534
535     dialog->hwnd = hwnd;
536     SetWindowLongPtrW( hwnd, GWLP_USERDATA, (LONG_PTR) dialog );
537
538     /* fetch the associated record from the Dialog table */
539     r = MSI_OpenQuery( package->db, &view, query, dialog->name );
540     if( r != ERROR_SUCCESS )
541     {
542         ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
543         return -1;
544     }
545     MSI_ViewExecute( view, NULL );
546     MSI_ViewFetch( view, &rec );
547     MSI_ViewClose( view );
548     msiobj_release( &view->hdr );
549
550     if( !rec )
551     {
552         TRACE("No record found for dialog %s\n", debugstr_w(dialog->name));
553         return -1;
554     }
555
556     dialog->scale = msi_dialog_get_sans_serif_height(dialog->hwnd);
557
558     width = MSI_RecordGetInteger( rec, 4 );
559     height = MSI_RecordGetInteger( rec, 5 );
560     dialog->attributes = MSI_RecordGetInteger( rec, 6 );
561     text = MSI_RecordGetString( rec, 7 );
562
563     width = msi_dialog_scale_unit( dialog, width );
564     height = msi_dialog_scale_unit( dialog, height ) + 25; /* FIXME */
565
566     dialog->default_font = load_dynamic_property( dialog->package, df, NULL );
567
568     deformat_string( dialog->package, text, &title );
569     SetWindowTextW( hwnd, title );
570     SetWindowPos( hwnd, 0, 0, 0, width, height,
571                   SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW );
572
573     HeapFree( GetProcessHeap(), 0, title );
574     msiobj_release( &rec->hdr );
575
576     msi_dialog_build_font_list( dialog );
577     msi_dialog_fill_controls( dialog );
578     msi_dialog_evaluate_control_conditions( dialog );
579
580     return 0;
581 }
582
583 static UINT msi_dialog_send_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
584 {
585     LPWSTR event_fmt = NULL, arg_fmt = NULL;
586
587     TRACE("Sending control event %s %s\n", debugstr_w(event), debugstr_w(arg));
588
589     deformat_string( dialog->package, event, &event_fmt );
590     deformat_string( dialog->package, arg, &arg_fmt );
591
592     dialog->event_handler( dialog->package, event_fmt, arg_fmt, dialog );
593
594     HeapFree( GetProcessHeap(), 0, event_fmt );
595     HeapFree( GetProcessHeap(), 0, arg_fmt );
596
597     return ERROR_SUCCESS;
598 }
599
600 static UINT msi_dialog_set_property( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
601 {
602     static const WCHAR szNullArg[] = { '{','}',0 };
603     LPWSTR p, prop, arg_fmt = NULL;
604     UINT len;
605
606     len = strlenW(event);
607     prop = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR));
608     strcpyW( prop, &event[1] );
609     p = strchrW( prop, ']' );
610     if( p && p[1] == 0 )
611     {
612         *p = 0;
613         if( strcmpW( szNullArg, arg ) )
614             deformat_string( dialog->package, arg, &arg_fmt );
615         MSI_SetPropertyW( dialog->package, prop, arg_fmt );
616     }
617     else
618         ERR("Badly formatted property string - what happens?\n");
619     HeapFree( GetProcessHeap(), 0, prop );
620     return ERROR_SUCCESS;
621 }
622
623 static UINT msi_dialog_control_event( MSIRECORD *rec, LPVOID param )
624 {
625     msi_dialog *dialog = param;
626     LPCWSTR condition, event, arg;
627     UINT r;
628
629     condition = MSI_RecordGetString( rec, 5 );
630     r = MSI_EvaluateConditionW( dialog->package, condition );
631     if( r )
632     {
633         event = MSI_RecordGetString( rec, 3 );
634         arg = MSI_RecordGetString( rec, 4 );
635         if( event[0] == '[' )
636             msi_dialog_set_property( dialog, event, arg );
637         else
638             msi_dialog_send_event( dialog, event, arg );
639     }
640
641     return ERROR_SUCCESS;
642 }
643
644 static UINT msi_dialog_button_click( msi_dialog *dialog, msi_control *control )
645 {
646     static const WCHAR query[] = {
647       'S','E','L','E','C','T',' ','*',' ',
648       'F','R','O','M',' ','C','o','n','t','r','o','l','E','v','e','n','t',' ',
649       'W','H','E','R','E',' ',
650          '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
651       'A','N','D',' ',
652          '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',' ',
653       'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','i','n','g','`',0
654     };
655     MSIQUERY *view = NULL;
656     UINT r;
657
658     r = MSI_OpenQuery( dialog->package->db, &view, query,
659                        dialog->name, control->name );
660     if( r != ERROR_SUCCESS )
661     {
662         ERR("query failed\n");
663         return 0;
664     }
665
666     r = MSI_IterateRecords( view, 0, msi_dialog_control_event, dialog );
667     msiobj_release( &view->hdr );
668
669     return r;
670 }
671
672 static UINT msi_dialog_get_checkbox_state( msi_dialog *dialog,
673                 msi_control *control )
674 {
675     WCHAR state[2] = { 0 };
676     DWORD sz = 2;
677
678     MSI_GetPropertyW( dialog->package, control->property, state, &sz );
679     return atoiW( state ) ? 1 : 0;
680 }
681
682 static void msi_dialog_set_checkbox_state( msi_dialog *dialog,
683                 msi_control *control, UINT state )
684 {
685     WCHAR szState[2] = { '0', 0 };
686
687     if( state )
688         szState[0]++;
689     MSI_SetPropertyW( dialog->package, control->property, szState );
690 }
691
692 static void msi_dialog_checkbox_sync_state( msi_dialog *dialog,
693                 msi_control *control )
694 {
695     UINT state;
696
697     state = msi_dialog_get_checkbox_state( dialog, control );
698     SendMessageW( control->hwnd, BM_SETCHECK,
699                   state ? BST_CHECKED : BST_UNCHECKED, 0 );
700 }
701
702 static UINT msi_dialog_checkbox_click( msi_dialog *dialog,
703                 msi_control *control )
704 {
705     UINT state;
706
707     TRACE("clicked checkbox %s, set %s\n", debugstr_w(control->name),
708           debugstr_w(control->property));
709
710     state = msi_dialog_get_checkbox_state( dialog, control );
711     state = state ? 0 : 1;
712     msi_dialog_set_checkbox_state( dialog, control, state );
713     msi_dialog_checkbox_sync_state( dialog, control );
714
715     return msi_dialog_button_click( dialog, control );
716 }
717
718 static LRESULT msi_dialog_handle_click( msi_dialog *dialog, HWND hwnd )
719 {
720     msi_control *control;
721
722     TRACE("BN_CLICKED %p %p\n", dialog, hwnd);
723
724     control = msi_dialog_find_control_by_hwnd( dialog, hwnd );
725     if( control )
726     {
727         if( control->click_handler )
728         {
729             control->click_handler( dialog, control );
730             msi_dialog_evaluate_control_conditions( dialog );
731         }
732     }
733     else
734         ERR("button click from nowhere\n");
735     return 0;
736 }
737
738 static LRESULT WINAPI MSIDialog_WndProc( HWND hwnd, UINT msg,
739                 WPARAM wParam, LPARAM lParam )
740 {
741     msi_dialog *dialog = (LPVOID) GetWindowLongPtrW( hwnd, GWLP_USERDATA );
742
743     switch (msg)
744     {
745     case WM_CREATE:
746         return msi_dialog_oncreate( hwnd, (LPCREATESTRUCTW)lParam );
747
748     case WM_COMMAND:
749         if( HIWORD(wParam) == BN_CLICKED )
750             return msi_dialog_handle_click( dialog, (HWND)lParam );
751         break;
752
753     case WM_DESTROY:
754         dialog->hwnd = NULL;
755         return 0;
756     }
757     return DefWindowProcW(hwnd, msg, wParam, lParam);
758 }
759
760 /* functions that interface to other modules within MSI */
761
762 msi_dialog *msi_dialog_create( MSIPACKAGE* package, LPCWSTR szDialogName,
763                                 msi_dialog_event_handler event_handler )
764 {
765     msi_dialog *dialog;
766     HWND hwnd;
767
768     TRACE("%p %s\n", package, debugstr_w(szDialogName));
769
770     /* allocate the structure for the dialog to use */
771     dialog = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
772                         sizeof *dialog + sizeof(WCHAR)*strlenW(szDialogName) );
773     if( !dialog )
774         return NULL;
775     strcpyW( dialog->name, szDialogName );
776     dialog->package = package;
777     dialog->event_handler = event_handler;
778
779     /* create the dialog window, don't show it yet */
780     hwnd = CreateWindowW( szMsiDialogClass, szDialogName, WS_OVERLAPPEDWINDOW,
781                      CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
782                      NULL, NULL, NULL, dialog );
783     if( !hwnd )
784     {
785         ERR("Failed to create dialog %s\n", debugstr_w( szDialogName ));
786         msi_dialog_destroy( dialog );
787         return NULL;
788     }
789
790     return dialog;
791 }
792
793 void msi_dialog_end_dialog( msi_dialog *dialog )
794 {
795     dialog->finished = 1;
796 }
797
798 UINT msi_dialog_run_message_loop( msi_dialog *dialog )
799 {
800     MSG msg;
801
802     if( dialog->attributes & msidbDialogAttributesVisible )
803     {
804         ShowWindow( dialog->hwnd, SW_SHOW );
805         UpdateWindow( dialog->hwnd );
806     }
807
808     if( dialog->attributes & msidbDialogAttributesModal )
809     {
810         while( !dialog->finished && GetMessageW( &msg, 0, 0, 0 ) )
811         {
812             TranslateMessage( &msg );
813             DispatchMessageW( &msg );
814         }
815     }
816     else
817         return ERROR_IO_PENDING;
818
819     return ERROR_SUCCESS;
820 }
821
822 void msi_dialog_check_messages( msi_dialog *dialog, HANDLE handle )
823 {
824     MSG msg;
825     DWORD r;
826
827     do
828     {
829         while( PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ) )
830         {
831             TranslateMessage( &msg );
832             DispatchMessageW( &msg );
833         }
834         if( !handle )
835             break;
836         r = MsgWaitForMultipleObjects( 1, &handle, 0, INFINITE, QS_ALLEVENTS );
837     }
838     while( WAIT_OBJECT_0 != r );
839 }
840
841 void msi_dialog_do_preview( msi_dialog *dialog )
842 {
843     dialog->attributes |= msidbDialogAttributesVisible;
844     dialog->attributes &= ~msidbDialogAttributesModal;
845     msi_dialog_run_message_loop( dialog );
846 }
847
848 void msi_dialog_destroy( msi_dialog *dialog )
849 {
850     if( dialog->hwnd )
851         ShowWindow( dialog->hwnd, SW_HIDE );
852     
853     /* destroy the list of controls */
854     while( dialog->control_list )
855     {
856         msi_control *t = dialog->control_list;
857         dialog->control_list = t->next;
858         /* leave dialog->hwnd - destroying parent destroys child windows */
859         HeapFree( GetProcessHeap(), 0, t->property );
860         HeapFree( GetProcessHeap(), 0, t );
861     }
862
863     /* destroy the list of fonts */
864     while( dialog->font_list )
865     {
866         msi_font *t = dialog->font_list;
867         dialog->font_list = t->next;
868         DeleteObject( t->hfont );
869         HeapFree( GetProcessHeap(), 0, t );
870     }
871     HeapFree( GetProcessHeap(), 0, dialog->default_font );
872
873     if( dialog->hwnd )
874         DestroyWindow( dialog->hwnd );
875
876     dialog->package = NULL;
877     HeapFree( GetProcessHeap(), 0, dialog );
878 }
879
880 void msi_dialog_register_class( void )
881 {
882     WNDCLASSW cls;
883
884     ZeroMemory( &cls, sizeof cls );
885     cls.lpfnWndProc   = MSIDialog_WndProc;
886     cls.hInstance     = NULL;
887     cls.hIcon         = LoadIconW(0, (LPWSTR)IDI_APPLICATION);
888     cls.hCursor       = LoadCursorW(0, (LPWSTR)IDC_ARROW);
889     cls.hbrBackground = (HBRUSH)(COLOR_WINDOW);
890     cls.lpszMenuName  = NULL;
891     cls.lpszClassName = szMsiDialogClass;
892
893     RegisterClassW( &cls );
894 }
895
896 void msi_dialog_unregister_class( void )
897 {
898     UnregisterClassW( szMsiDialogClass, NULL );
899 }