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