Implement NtAccessCheck.
[wine/gsoc-2012-control.git] / dlls / msi / dialog.c
blob4977329f471e10c4669b60256dfb7f0ae842f72c
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2005 Mike McCormack for CodeWeavers
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.
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.
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
21 #define COBJMACROS
23 #include <stdarg.h>
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"
36 #include "wine/debug.h"
37 #include "wine/unicode.h"
39 #include "action.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msi);
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
47 const WCHAR szMsiHiddenWindow[] = {
48 'M','s','i','H','i','d','d','e','n','W','i','n','d','o','w',0
50 const static WCHAR szStatic[] = { 'S','t','a','t','i','c',0 };
51 const static WCHAR szButton[] = { 'B','U','T','T','O','N', 0 };
53 const static WCHAR szButtonData[] = { 'M','S','I','D','A','T','A',0 };
55 struct msi_control_tag;
56 typedef struct msi_control_tag msi_control;
57 typedef UINT (*msi_handler)( msi_dialog *, msi_control *, WPARAM );
59 struct msi_control_tag
61 struct msi_control_tag *next;
62 HWND hwnd;
63 msi_handler handler;
64 LPWSTR property;
65 IPicture *pic;
66 WCHAR name[1];
69 typedef struct msi_font_tag
71 struct msi_font_tag *next;
72 HFONT hfont;
73 WCHAR name[1];
74 } msi_font;
76 struct msi_dialog_tag
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];
90 typedef UINT (*msi_dialog_control_func)( msi_dialog *dialog, MSIRECORD *rec );
91 struct control_handler
93 LPCWSTR control_type;
94 msi_dialog_control_func func;
97 typedef struct
99 msi_dialog* dialog;
100 msi_control *parent;
101 DWORD attributes;
102 } radio_button_group_descr;
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);
112 /* dialog sequencing */
114 #define WM_MSI_DIALOG_CREATE (WM_USER+0x100)
115 #define WM_MSI_DIALOG_DESTROY (WM_USER+0x101)
117 static DWORD uiThreadId;
118 static HWND hMsiHiddenWindow;
120 INT msi_dialog_scale_unit( msi_dialog *dialog, INT val )
122 return (dialog->scale * val + 5) / 10;
126 * msi_dialog_get_style
128 * Extract the {\style} string from the front of the text to display and
129 * update the pointer.
131 static LPWSTR msi_dialog_get_style( LPCWSTR *text )
133 LPWSTR ret = NULL;
134 LPCWSTR p = *text, q;
135 DWORD len;
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;
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;
155 static UINT msi_dialog_add_font( MSIRECORD *rec, LPVOID param )
157 msi_dialog *dialog = param;
158 msi_font *font;
159 LPCWSTR face, name;
160 LOGFONTW lf;
161 INT style;
162 HDC hdc;
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;
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 );
186 /* adjust the height */
187 hdc = GetDC( dialog->hwnd );
188 if (hdc)
190 lf.lfHeight = -MulDiv(lf.lfHeight, GetDeviceCaps(hdc, LOGPIXELSY), 72);
191 ReleaseDC( dialog->hwnd, hdc );
194 font->hfont = CreateFontIndirectW( &lf );
196 TRACE("Adding font style %s\n", debugstr_w(font->name) );
198 return ERROR_SUCCESS;
201 static msi_font *msi_dialog_find_font( msi_dialog *dialog, LPCWSTR name )
203 msi_font *font;
205 for( font = dialog->font_list; font; font = font->next )
206 if( !strcmpW( font->name, name ) ) /* FIXME: case sensitive? */
207 break;
209 return font;
212 static UINT msi_dialog_set_font( msi_dialog *dialog, HWND hwnd, LPCWSTR name )
214 msi_font *font;
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;
224 static UINT msi_dialog_build_font_list( msi_dialog *dialog )
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
230 UINT r;
231 MSIQUERY *view = NULL;
233 TRACE("dialog %p\n", dialog );
235 r = MSI_OpenQuery( dialog->package->db, &view, query );
236 if( r != ERROR_SUCCESS )
237 return r;
239 r = MSI_IterateRecords( view, NULL, msi_dialog_add_font, dialog );
240 msiobj_release( &view->hdr );
242 return r;
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 )
249 DWORD x, y, width, height;
250 LPWSTR font = NULL, title = NULL;
251 msi_control *control;
253 style |= WS_CHILD | WS_GROUP;
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;
264 x = MSI_RecordGetInteger( rec, 4 );
265 y = MSI_RecordGetInteger( rec, 5 );
266 width = MSI_RecordGetInteger( rec, 6 );
267 height = MSI_RecordGetInteger( rec, 7 );
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 );
274 if( text )
276 font = msi_dialog_get_style( &text );
277 deformat_string( dialog->package, text, &title );
280 control->hwnd = CreateWindowW( szCls, title, style,
281 x, y, width, height, parent, NULL, NULL, NULL );
283 TRACE("Dialog %s control %s hwnd %p\n",
284 debugstr_w(dialog->name), debugstr_w(text), control->hwnd );
286 msi_dialog_set_font( dialog, control->hwnd,
287 font ? font : dialog->default_font );
289 HeapFree( GetProcessHeap(), 0, font );
290 HeapFree( GetProcessHeap(), 0, title );
292 return control;
295 /* everything except radio buttons */
296 static msi_control *msi_dialog_add_control( msi_dialog *dialog,
297 MSIRECORD *rec, LPCWSTR szCls, DWORD style )
299 DWORD attributes;
300 LPCWSTR text, name;
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 );
313 static UINT msi_dialog_text_control( msi_dialog *dialog, MSIRECORD *rec )
315 TRACE("%p %p\n", dialog, rec);
317 msi_dialog_add_control( dialog, rec, szStatic, 0 );
318 return ERROR_SUCCESS;
321 static UINT msi_dialog_button_control( msi_dialog *dialog, MSIRECORD *rec )
323 msi_control *control;
325 TRACE("%p %p\n", dialog, rec);
327 control = msi_dialog_add_control( dialog, rec, szButton, 0 );
328 control->handler = msi_dialog_button_handler;
330 return ERROR_SUCCESS;
333 static UINT msi_dialog_checkbox_control( msi_dialog *dialog, MSIRECORD *rec )
335 msi_control *control;
336 LPCWSTR prop;
338 TRACE("%p %p\n", dialog, rec);
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 );
348 return ERROR_SUCCESS;
351 static UINT msi_dialog_line_control( msi_dialog *dialog, MSIRECORD *rec )
353 TRACE("%p %p\n", dialog, rec);
355 msi_dialog_add_control( dialog, rec, szStatic, SS_ETCHEDHORZ | SS_SUNKEN );
356 return ERROR_SUCCESS;
359 static UINT msi_dialog_scrolltext_control( msi_dialog *dialog, MSIRECORD *rec )
361 const static WCHAR szEdit[] = { 'E','D','I','T',0 };
363 FIXME("%p %p\n", dialog, rec);
365 msi_dialog_add_control( dialog, rec, szEdit, WS_BORDER |
366 ES_MULTILINE | WS_VSCROLL | ES_READONLY | ES_AUTOVSCROLL );
368 return ERROR_SUCCESS;
371 static UINT msi_load_bitmap( MSIDATABASE *db, LPCWSTR name, IPicture **pic )
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
379 MSIQUERY *view = NULL;
380 MSIRECORD *rec = NULL;
381 IStream *stm = NULL;
382 UINT r;
384 r = MSI_OpenQuery( db, &view, query, name );
385 if( r != ERROR_SUCCESS )
386 return r;
388 MSI_ViewExecute( view, NULL );
389 MSI_ViewFetch( view, &rec );
390 MSI_ViewClose( view );
391 msiobj_release( &view->hdr );
393 if( !rec )
394 return ERROR_FUNCTION_FAILED;
396 r = MSI_RecordGetIStream( rec, 2, &stm );
397 msiobj_release( &rec->hdr );
398 if( r != ERROR_SUCCESS )
399 return r;
401 r = OleLoadPicture( stm, 0, TRUE, &IID_IPicture, (LPVOID*) pic );
402 IStream_Release( stm );
403 if( FAILED( r ) )
404 return ERROR_FUNCTION_FAILED;
406 return ERROR_SUCCESS;
409 static UINT msi_dialog_bitmap_control( msi_dialog *dialog, MSIRECORD *rec )
411 IPicture *pic = NULL;
412 msi_control *control;
413 OLE_HANDLE hBitmap = 0;
414 LPCWSTR text;
415 UINT r;
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 )
423 r = IPicture_get_Handle( pic, &hBitmap );
424 if( SUCCEEDED( r ) )
425 SendMessageW( control->hwnd, STM_SETIMAGE, IMAGE_BITMAP, hBitmap );
426 control->pic = pic;
429 return ERROR_SUCCESS;
432 static UINT msi_dialog_combo_control( msi_dialog *dialog, MSIRECORD *rec )
434 static const WCHAR szCombo[] = { 'C','O','M','B','O','B','O','X',0 };
436 msi_dialog_add_control( dialog, rec, szCombo,
437 SS_BITMAP | SS_LEFT | SS_CENTERIMAGE );
438 return ERROR_SUCCESS;
441 static UINT msi_dialog_edit_control( msi_dialog *dialog, MSIRECORD *rec )
443 const static WCHAR szEdit[] = { 'E','D','I','T',0 };
444 msi_control *control;
445 LPCWSTR prop;
446 LPWSTR val;
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;
459 static UINT msi_dialog_pathedit_control( msi_dialog *dialog, MSIRECORD *rec )
461 FIXME("not implemented properly\n");
462 return msi_dialog_edit_control( dialog, rec );
465 /* radio buttons are a bit different from normal controls */
466 static UINT msi_dialog_create_radiobutton( MSIRECORD *rec, LPVOID param )
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;
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;
483 control = msi_dialog_create_window( dialog, rec, szButton, name, text,
484 style, group->parent->hwnd );
485 control->handler = msi_dialog_radiogroup_handler;
487 prop = MSI_RecordGetString( rec, 1 );
488 if( prop )
489 control->property = strdupW( prop );
491 return ERROR_SUCCESS;
494 static UINT msi_dialog_radiogroup_control( msi_dialog *dialog, MSIRECORD *rec )
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;
508 prop = MSI_RecordGetString( rec, 9 );
510 TRACE("%p %p %s\n", dialog, rec, debugstr_w( prop ));
512 /* Create parent group box to hold radio buttons */
513 control = msi_dialog_add_control( dialog, rec, szButton, BS_OWNERDRAW );
515 if (control->hwnd)
517 WNDPROC oldproc = (WNDPROC) SetWindowLongPtrW(control->hwnd, GWLP_WNDPROC,
518 (LONG_PTR)MSIRadioGroup_WndProc);
519 SetPropW(control->hwnd, szButtonData, oldproc);
522 if( prop )
523 control->property = strdupW( prop );
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 )
529 ERR("query failed for dialog %s radio group %s\n",
530 debugstr_w(dialog->name), debugstr_w(prop));
531 return ERROR_INVALID_PARAMETER;
534 group.dialog = dialog;
535 group.parent = control;
536 group.attributes = MSI_RecordGetInteger( rec, 8 );
538 r = MSI_IterateRecords( view, 0, msi_dialog_create_radiobutton, &group );
539 msiobj_release( &view->hdr );
541 return r;
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 };
558 struct control_handler msi_dialog_handler[] =
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 },
573 #define NUM_CONTROL_TYPES (sizeof msi_dialog_handler/sizeof msi_dialog_handler[0])
575 static UINT msi_dialog_create_controls( MSIRECORD *rec, LPVOID param )
577 msi_dialog *dialog = param;
578 LPCWSTR control_type;
579 UINT i;
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));
591 return ERROR_SUCCESS;
594 static UINT msi_dialog_fill_controls( msi_dialog *dialog )
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;
605 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
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 )
611 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
612 return ERROR_INVALID_PARAMETER;
615 r = MSI_IterateRecords( view, 0, msi_dialog_create_controls, dialog );
616 msiobj_release( &view->hdr );
618 return r;
621 static msi_control *msi_dialog_find_control( msi_dialog *dialog, LPCWSTR name )
623 msi_control *control;
625 for( control = dialog->control_list; control; control = control->next )
626 if( !strcmpW( control->name, name ) ) /* FIXME: case sensitive? */
627 break;
628 return control;
631 static msi_control *msi_dialog_find_control_by_hwnd( msi_dialog *dialog, HWND hwnd )
633 msi_control *control;
635 for( control = dialog->control_list; control; control = control->next )
636 if( hwnd == control->hwnd )
637 break;
638 return control;
641 static UINT msi_dialog_set_control_condition( MSIRECORD *rec, LPVOID param )
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;
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 )
659 TRACE("%s control %s\n", debugstr_w(action), debugstr_w(name));
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));
674 return ERROR_SUCCESS;
677 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog )
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
686 UINT r;
687 MSIQUERY *view = NULL;
688 MSIPACKAGE *package = dialog->package;
690 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
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 )
696 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
697 return ERROR_INVALID_PARAMETER;
700 r = MSI_IterateRecords( view, 0, msi_dialog_set_control_condition, dialog );
701 msiobj_release( &view->hdr );
703 return r;
706 /* figure out the height of 10 point MS Sans Serif */
707 static INT msi_dialog_get_sans_serif_height( HWND hwnd )
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;
718 hdc = GetDC( hwnd );
719 if (hdc)
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)
727 hOldFont = SelectObject( hdc, hFont );
728 r = GetTextMetricsW( hdc, &tm );
729 if (r)
730 height = tm.tmHeight;
731 SelectObject( hdc, hOldFont );
732 DeleteObject( hFont );
734 ReleaseDC( hwnd, hdc );
736 return height;
739 /* fetch the associated record from the Dialog table */
740 static MSIRECORD *msi_get_dialog_record( msi_dialog *dialog )
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;
752 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
754 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
755 if( r != ERROR_SUCCESS )
757 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
758 return NULL;
760 MSI_ViewExecute( view, NULL );
761 MSI_ViewFetch( view, &rec );
762 MSI_ViewClose( view );
763 msiobj_release( &view->hdr );
765 return rec;
768 static LRESULT msi_dialog_oncreate( HWND hwnd, LPCREATESTRUCTW cs )
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;
778 TRACE("%p %p\n", dialog, dialog->package);
780 dialog->hwnd = hwnd;
781 SetWindowLongPtrW( hwnd, GWLP_USERDATA, (LONG_PTR) dialog );
783 rec = msi_get_dialog_record( dialog );
784 if( !rec )
786 TRACE("No record found for dialog %s\n", debugstr_w(dialog->name));
787 return -1;
790 dialog->scale = msi_dialog_get_sans_serif_height(dialog->hwnd);
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 );
797 width = msi_dialog_scale_unit( dialog, width );
798 height = msi_dialog_scale_unit( dialog, height ) + 25; /* FIXME */
800 dialog->default_font = load_dynamic_property( dialog->package, df, NULL );
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 );
807 HeapFree( GetProcessHeap(), 0, title );
808 msiobj_release( &rec->hdr );
810 msi_dialog_build_font_list( dialog );
811 msi_dialog_fill_controls( dialog );
812 msi_dialog_evaluate_control_conditions( dialog );
814 return 0;
817 static UINT msi_dialog_send_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
819 LPWSTR event_fmt = NULL, arg_fmt = NULL;
821 TRACE("Sending control event %s %s\n", debugstr_w(event), debugstr_w(arg));
823 deformat_string( dialog->package, event, &event_fmt );
824 deformat_string( dialog->package, arg, &arg_fmt );
826 dialog->event_handler( dialog->package, event_fmt, arg_fmt, dialog );
828 HeapFree( GetProcessHeap(), 0, event_fmt );
829 HeapFree( GetProcessHeap(), 0, arg_fmt );
831 return ERROR_SUCCESS;
834 static UINT msi_dialog_set_property( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
836 static const WCHAR szNullArg[] = { '{','}',0 };
837 LPWSTR p, prop, arg_fmt = NULL;
838 UINT len;
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 )
846 *p = 0;
847 if( strcmpW( szNullArg, arg ) )
848 deformat_string( dialog->package, arg, &arg_fmt );
849 MSI_SetPropertyW( dialog->package, prop, arg_fmt );
851 else
852 ERR("Badly formatted property string - what happens?\n");
853 HeapFree( GetProcessHeap(), 0, prop );
854 return ERROR_SUCCESS;
857 static UINT msi_dialog_control_event( MSIRECORD *rec, LPVOID param )
859 msi_dialog *dialog = param;
860 LPCWSTR condition, event, arg;
861 UINT r;
863 condition = MSI_RecordGetString( rec, 5 );
864 r = MSI_EvaluateConditionW( dialog->package, condition );
865 if( r )
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 );
875 return ERROR_SUCCESS;
878 static UINT msi_dialog_button_handler( msi_dialog *dialog,
879 msi_control *control, WPARAM param )
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
890 MSIQUERY *view = NULL;
891 UINT r;
893 if( HIWORD(param) != BN_CLICKED )
894 return ERROR_SUCCESS;
896 r = MSI_OpenQuery( dialog->package->db, &view, query,
897 dialog->name, control->name );
898 if( r != ERROR_SUCCESS )
900 ERR("query failed\n");
901 return 0;
904 r = MSI_IterateRecords( view, 0, msi_dialog_control_event, dialog );
905 msiobj_release( &view->hdr );
907 return r;
910 static UINT msi_dialog_get_checkbox_state( msi_dialog *dialog,
911 msi_control *control )
913 WCHAR state[2] = { 0 };
914 DWORD sz = 2;
916 MSI_GetPropertyW( dialog->package, control->property, state, &sz );
917 return atoiW( state ) ? 1 : 0;
920 static void msi_dialog_set_checkbox_state( msi_dialog *dialog,
921 msi_control *control, UINT state )
923 WCHAR szState[2] = { '0', 0 };
925 if( state )
926 szState[0]++;
927 MSI_SetPropertyW( dialog->package, control->property, szState );
930 static void msi_dialog_checkbox_sync_state( msi_dialog *dialog,
931 msi_control *control )
933 UINT state;
935 state = msi_dialog_get_checkbox_state( dialog, control );
936 SendMessageW( control->hwnd, BM_SETCHECK,
937 state ? BST_CHECKED : BST_UNCHECKED, 0 );
940 static UINT msi_dialog_checkbox_handler( msi_dialog *dialog,
941 msi_control *control, WPARAM param )
943 UINT state;
945 if( HIWORD(param) != BN_CLICKED )
946 return ERROR_SUCCESS;
948 TRACE("clicked checkbox %s, set %s\n", debugstr_w(control->name),
949 debugstr_w(control->property));
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 );
956 return msi_dialog_button_handler( dialog, control, param );
959 static UINT msi_dialog_edit_handler( msi_dialog *dialog,
960 msi_control *control, WPARAM param )
962 UINT sz, r;
963 LPWSTR buf;
965 if( HIWORD(param) != EN_CHANGE )
966 return ERROR_SUCCESS;
968 TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
969 debugstr_w(control->property));
971 sz = 0x20;
972 buf = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
973 while( buf )
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) );
982 MSI_SetPropertyW( dialog->package, control->property, buf );
984 HeapFree( GetProcessHeap(), 0, buf );
986 return ERROR_SUCCESS;
989 static UINT msi_dialog_radiogroup_handler( msi_dialog *dialog,
990 msi_control *control, WPARAM param )
992 if( HIWORD(param) != BN_CLICKED )
993 return ERROR_SUCCESS;
995 TRACE("clicked radio button %s, set %s\n", debugstr_w(control->name),
996 debugstr_w(control->property));
998 MSI_SetPropertyW( dialog->package, control->property, control->name );
1000 return msi_dialog_button_handler( dialog, control, param );
1003 static LRESULT msi_dialog_oncommand( msi_dialog *dialog, WPARAM param, HWND hwnd )
1005 msi_control *control;
1007 TRACE("%p %p %08x\n", dialog, hwnd, param);
1009 control = msi_dialog_find_control_by_hwnd( dialog, hwnd );
1010 if( control )
1012 if( control->handler )
1014 control->handler( dialog, control, param );
1015 msi_dialog_evaluate_control_conditions( dialog );
1018 else
1019 ERR("button click from nowhere\n");
1020 return 0;
1023 static LRESULT WINAPI MSIDialog_WndProc( HWND hwnd, UINT msg,
1024 WPARAM wParam, LPARAM lParam )
1026 msi_dialog *dialog = (LPVOID) GetWindowLongPtrW( hwnd, GWLP_USERDATA );
1028 TRACE("0x%04x\n", msg);
1030 switch (msg)
1032 case WM_CREATE:
1033 return msi_dialog_oncreate( hwnd, (LPCREATESTRUCTW)lParam );
1035 case WM_COMMAND:
1036 return msi_dialog_oncommand( dialog, wParam, (HWND)lParam );
1038 case WM_DESTROY:
1039 dialog->hwnd = NULL;
1040 return 0;
1042 return DefWindowProcW(hwnd, msg, wParam, lParam);
1045 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1047 WNDPROC oldproc = (WNDPROC) GetPropW(hWnd, szButtonData);
1049 TRACE("hWnd %p msg %04x wParam 0x%08x lParam 0x%08lx\n", hWnd, msg, wParam, lParam);
1051 if (msg == WM_COMMAND) /* Forward notifications to dialog */
1052 SendMessageW(GetParent(hWnd), msg, wParam, lParam);
1054 return CallWindowProcW(oldproc, hWnd, msg, wParam, lParam);
1057 static LRESULT WINAPI MSIHiddenWindowProc( HWND hwnd, UINT msg,
1058 WPARAM wParam, LPARAM lParam )
1060 msi_dialog *dialog = (msi_dialog*) lParam;
1062 TRACE("%d %p\n", msg, dialog);
1064 switch (msg)
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;
1072 return DefWindowProcW( hwnd, msg, wParam, lParam );
1075 /* functions that interface to other modules within MSI */
1077 msi_dialog *msi_dialog_create( MSIPACKAGE* package, LPCWSTR szDialogName,
1078 msi_dialog_event_handler event_handler )
1080 MSIRECORD *rec = NULL;
1081 msi_dialog *dialog;
1083 TRACE("%p %s\n", package, debugstr_w(szDialogName));
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;
1096 /* verify that the dialog exists */
1097 rec = msi_get_dialog_record( dialog );
1098 if( !rec )
1100 HeapFree( GetProcessHeap(), 0, dialog );
1101 return NULL;
1103 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
1104 msiobj_release( &rec->hdr );
1106 return dialog;
1109 static void msi_process_pending_messages(void)
1111 MSG msg;
1113 while( PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ) )
1115 TranslateMessage( &msg );
1116 DispatchMessageW( &msg );
1120 void msi_dialog_end_dialog( msi_dialog *dialog )
1122 TRACE("%p\n", dialog);
1123 dialog->finished = 1;
1124 PostMessageW(dialog->hwnd, WM_NULL, 0, 0);
1127 void msi_dialog_check_messages( HANDLE handle )
1129 DWORD r;
1131 /* in threads other than the UI thread, block */
1132 if( uiThreadId != GetCurrentThreadId() )
1134 if( handle )
1135 WaitForSingleObject( handle, INFINITE );
1136 return;
1139 /* there's two choices for the UI thread */
1140 while (1)
1142 msi_process_pending_messages();
1144 if( !handle )
1145 break;
1148 * block here until somebody creates a new dialog or
1149 * the handle we're waiting on becomes ready
1151 r = MsgWaitForMultipleObjects( 1, &handle, 0, INFINITE, QS_ALLINPUT );
1152 if( r == WAIT_OBJECT_0 )
1153 break;
1157 UINT msi_dialog_run_message_loop( msi_dialog *dialog )
1159 HWND hwnd;
1161 if( !(dialog->attributes & msidbDialogAttributesVisible) )
1162 return ERROR_SUCCESS;
1164 if( uiThreadId != GetCurrentThreadId() )
1165 return SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_CREATE, 0, (LPARAM) dialog );
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 )
1173 ERR("Failed to create dialog %s\n", debugstr_w( dialog->name ));
1174 return ERROR_FUNCTION_FAILED;
1177 ShowWindow( hwnd, SW_SHOW );
1178 UpdateWindow( hwnd );
1180 if( dialog->attributes & msidbDialogAttributesModal )
1182 while( !dialog->finished )
1184 MsgWaitForMultipleObjects( 0, NULL, 0, INFINITE, QS_ALLEVENTS );
1185 msi_process_pending_messages();
1188 else
1189 return ERROR_IO_PENDING;
1191 return ERROR_SUCCESS;
1194 void msi_dialog_do_preview( msi_dialog *dialog )
1196 TRACE("\n");
1197 dialog->attributes |= msidbDialogAttributesVisible;
1198 dialog->attributes &= ~msidbDialogAttributesModal;
1199 msi_dialog_run_message_loop( dialog );
1202 void msi_dialog_destroy( msi_dialog *dialog )
1204 if( uiThreadId != GetCurrentThreadId() )
1206 SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_DESTROY, 0, (LPARAM) dialog );
1207 return;
1210 if( dialog->hwnd )
1211 ShowWindow( dialog->hwnd, SW_HIDE );
1213 /* destroy the list of controls */
1214 while( dialog->control_list )
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 );
1225 /* destroy the list of fonts */
1226 while( dialog->font_list )
1228 msi_font *t = dialog->font_list;
1229 dialog->font_list = t->next;
1230 DeleteObject( t->hfont );
1231 HeapFree( GetProcessHeap(), 0, t );
1233 HeapFree( GetProcessHeap(), 0, dialog->default_font );
1235 if( dialog->hwnd )
1236 DestroyWindow( dialog->hwnd );
1238 msiobj_release( &dialog->package->hdr );
1239 dialog->package = NULL;
1240 HeapFree( GetProcessHeap(), 0, dialog );
1243 BOOL msi_dialog_register_class( void )
1245 WNDCLASSW cls;
1247 ZeroMemory( &cls, sizeof cls );
1248 cls.lpfnWndProc = MSIDialog_WndProc;
1249 cls.hInstance = NULL;
1250 cls.hIcon = LoadIconW(0, (LPWSTR)IDI_APPLICATION);
1251 cls.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
1252 cls.hbrBackground = (HBRUSH)(COLOR_WINDOW);
1253 cls.lpszMenuName = NULL;
1254 cls.lpszClassName = szMsiDialogClass;
1256 if( !RegisterClassW( &cls ) )
1257 return FALSE;
1259 cls.lpfnWndProc = MSIHiddenWindowProc;
1260 cls.lpszClassName = szMsiHiddenWindow;
1262 if( !RegisterClassW( &cls ) )
1263 return FALSE;
1265 uiThreadId = GetCurrentThreadId();
1267 hMsiHiddenWindow = CreateWindowW( szMsiHiddenWindow, NULL, WS_OVERLAPPED,
1268 0, 0, 100, 100, NULL, NULL, NULL, NULL );
1269 if( !hMsiHiddenWindow )
1270 return FALSE;
1272 return TRUE;
1275 void msi_dialog_unregister_class( void )
1277 DestroyWindow( hMsiHiddenWindow );
1278 UnregisterClassW( szMsiDialogClass, NULL );
1279 uiThreadId = 0;