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
36 #include "wine/debug.h"
37 #include "wine/unicode.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msi);
44 struct msi_control_tag;
45 typedef struct msi_control_tag msi_control;
46 typedef UINT (*msi_handler)( msi_dialog *, msi_control *, WPARAM );
48 struct msi_control_tag
50 struct msi_control_tag *next;
59 typedef struct msi_font_tag
61 struct msi_font_tag *next;
69 msi_dialog_event_handler event_handler;
76 msi_control *control_list;
80 typedef UINT (*msi_dialog_control_func)( msi_dialog *dialog, MSIRECORD *rec );
81 struct control_handler
84 msi_dialog_control_func func;
92 } radio_button_group_descr;
94 const WCHAR szMsiDialogClass[] = {
95 'M','s','i','D','i','a','l','o','g','C','l','o','s','e','C','l','a','s','s',0
97 const WCHAR szMsiHiddenWindow[] = {
98 'M','s','i','H','i','d','d','e','n','W','i','n','d','o','w',0 };
99 const static WCHAR szStatic[] = { 'S','t','a','t','i','c',0 };
100 const static WCHAR szButton[] = { 'B','U','T','T','O','N', 0 };
101 const static WCHAR szButtonData[] = { 'M','S','I','D','A','T','A',0 };
102 static const WCHAR szText[] = { 'T','e','x','t',0 };
103 static const WCHAR szPushButton[] = { 'P','u','s','h','B','u','t','t','o','n',0 };
104 static const WCHAR szLine[] = { 'L','i','n','e',0 };
105 static const WCHAR szBitmap[] = { 'B','i','t','m','a','p',0 };
106 static const WCHAR szCheckBox[] = { 'C','h','e','c','k','B','o','x',0 };
107 static const WCHAR szScrollableText[] = {
108 'S','c','r','o','l','l','a','b','l','e','T','e','x','t',0 };
109 static const WCHAR szComboBox[] = { 'C','o','m','b','o','B','o','x',0 };
110 static const WCHAR szEdit[] = { 'E','d','i','t',0 };
111 static const WCHAR szMaskedEdit[] = { 'M','a','s','k','e','d','E','d','i','t',0 };
112 static const WCHAR szPathEdit[] = { 'P','a','t','h','E','d','i','t',0 };
113 static const WCHAR szRadioButtonGroup[] = {
114 'R','a','d','i','o','B','u','t','t','o','n','G','r','o','u','p',0 };
116 static UINT msi_dialog_checkbox_handler( msi_dialog *, msi_control *, WPARAM );
117 static void msi_dialog_checkbox_sync_state( msi_dialog *, msi_control * );
118 static UINT msi_dialog_button_handler( msi_dialog *, msi_control *, WPARAM );
119 static UINT msi_dialog_edit_handler( msi_dialog *, msi_control *, WPARAM );
120 static UINT msi_dialog_radiogroup_handler( msi_dialog *, msi_control *, WPARAM param );
121 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
124 /* dialog sequencing */
126 #define WM_MSI_DIALOG_CREATE (WM_USER+0x100)
127 #define WM_MSI_DIALOG_DESTROY (WM_USER+0x101)
129 static DWORD uiThreadId;
130 static HWND hMsiHiddenWindow;
132 static INT msi_dialog_scale_unit( msi_dialog *dialog, INT val )
134 return (dialog->scale * val + 5) / 10;
137 static msi_control *msi_dialog_find_control( msi_dialog *dialog, LPCWSTR name )
139 msi_control *control;
141 for( control = dialog->control_list; control; control = control->next )
142 if( !strcmpW( control->name, name ) ) /* FIXME: case sensitive? */
147 static msi_control *msi_dialog_find_control_by_hwnd( msi_dialog *dialog, HWND hwnd )
149 msi_control *control;
151 for( control = dialog->control_list; control; control = control->next )
152 if( hwnd == control->hwnd )
158 * msi_dialog_get_style
160 * Extract the {\style} string from the front of the text to display and
161 * update the pointer.
163 static LPWSTR msi_dialog_get_style( LPCWSTR *text )
166 LPCWSTR p = *text, q;
171 q = strchrW( p, '}' );
179 ret = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
182 memcpy( ret, p, len*sizeof(WCHAR) );
187 static UINT msi_dialog_add_font( MSIRECORD *rec, LPVOID param )
189 msi_dialog *dialog = param;
196 /* create a font and add it to the list */
197 name = MSI_RecordGetString( rec, 1 );
198 font = HeapAlloc( GetProcessHeap(), 0,
199 sizeof *font + strlenW( name )*sizeof (WCHAR) );
200 strcpyW( font->name, name );
201 font->next = dialog->font_list;
202 dialog->font_list = font;
204 memset( &lf, 0, sizeof lf );
205 face = MSI_RecordGetString( rec, 2 );
206 lf.lfHeight = MSI_RecordGetInteger( rec, 3 );
207 style = MSI_RecordGetInteger( rec, 5 );
208 if( style & msidbTextStyleStyleBitsBold )
209 lf.lfWeight = FW_BOLD;
210 if( style & msidbTextStyleStyleBitsItalic )
212 if( style & msidbTextStyleStyleBitsUnderline )
213 lf.lfUnderline = TRUE;
214 if( style & msidbTextStyleStyleBitsStrike )
215 lf.lfStrikeOut = TRUE;
216 lstrcpynW( lf.lfFaceName, face, LF_FACESIZE );
218 /* adjust the height */
219 hdc = GetDC( dialog->hwnd );
222 lf.lfHeight = -MulDiv(lf.lfHeight, GetDeviceCaps(hdc, LOGPIXELSY), 72);
223 ReleaseDC( dialog->hwnd, hdc );
226 font->hfont = CreateFontIndirectW( &lf );
228 TRACE("Adding font style %s\n", debugstr_w(font->name) );
230 return ERROR_SUCCESS;
233 static msi_font *msi_dialog_find_font( msi_dialog *dialog, LPCWSTR name )
237 for( font = dialog->font_list; font; font = font->next )
238 if( !strcmpW( font->name, name ) ) /* FIXME: case sensitive? */
244 static UINT msi_dialog_set_font( msi_dialog *dialog, HWND hwnd, LPCWSTR name )
248 font = msi_dialog_find_font( dialog, name );
250 SendMessageW( hwnd, WM_SETFONT, (WPARAM) font->hfont, TRUE );
252 ERR("No font entry for %s\n", debugstr_w(name));
253 return ERROR_SUCCESS;
256 static UINT msi_dialog_build_font_list( msi_dialog *dialog )
258 static const WCHAR query[] = {
259 'S','E','L','E','C','T',' ','*',' ',
260 'F','R','O','M',' ','`','T','e','x','t','S','t','y','l','e','`',' ',0
263 MSIQUERY *view = NULL;
265 TRACE("dialog %p\n", dialog );
267 r = MSI_OpenQuery( dialog->package->db, &view, query );
268 if( r != ERROR_SUCCESS )
271 r = MSI_IterateRecords( view, NULL, msi_dialog_add_font, dialog );
272 msiobj_release( &view->hdr );
277 static msi_control *msi_dialog_create_window( msi_dialog *dialog,
278 MSIRECORD *rec, LPCWSTR szCls, LPCWSTR name, LPCWSTR text,
279 DWORD style, HWND parent )
281 DWORD x, y, width, height;
282 LPWSTR font = NULL, title = NULL;
283 msi_control *control;
285 style |= WS_CHILD | WS_GROUP;
287 control = HeapAlloc( GetProcessHeap(), 0,
288 sizeof *control + strlenW(name)*sizeof(WCHAR) );
289 strcpyW( control->name, name );
290 control->next = dialog->control_list;
291 dialog->control_list = control;
292 control->handler = NULL;
293 control->property = NULL;
294 control->value = NULL;
297 x = MSI_RecordGetInteger( rec, 4 );
298 y = MSI_RecordGetInteger( rec, 5 );
299 width = MSI_RecordGetInteger( rec, 6 );
300 height = MSI_RecordGetInteger( rec, 7 );
302 x = msi_dialog_scale_unit( dialog, x );
303 y = msi_dialog_scale_unit( dialog, y );
304 width = msi_dialog_scale_unit( dialog, width );
305 height = msi_dialog_scale_unit( dialog, height );
309 font = msi_dialog_get_style( &text );
310 deformat_string( dialog->package, text, &title );
313 control->hwnd = CreateWindowW( szCls, title, style,
314 x, y, width, height, parent, NULL, NULL, NULL );
316 TRACE("Dialog %s control %s hwnd %p\n",
317 debugstr_w(dialog->name), debugstr_w(text), control->hwnd );
319 msi_dialog_set_font( dialog, control->hwnd,
320 font ? font : dialog->default_font );
322 HeapFree( GetProcessHeap(), 0, font );
323 HeapFree( GetProcessHeap(), 0, title );
328 /* called from the Control Event subscription code */
329 void msi_dialog_handle_event( msi_dialog* dialog, LPCWSTR control,
330 LPCWSTR attribute, MSIRECORD *rec )
335 ctrl = msi_dialog_find_control( dialog, control );
338 if( lstrcmpW(attribute, szText) )
340 text = MSI_RecordGetString( rec , 1 );
341 SetWindowTextW( ctrl->hwnd, text );
344 void msi_dialog_map_events(msi_dialog* dialog, LPCWSTR control)
346 static WCHAR Query[] = {
347 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
348 '`','E','v','e','n','t','M','a','p','p','i','n','g','`',' ',
349 'W','H','E','R','E',' ',
350 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
352 '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',0
355 LPCWSTR event, attribute;
357 row = MSI_QueryGetRecord( dialog->package->db, Query, dialog->name, control );
361 event = MSI_RecordGetString( row, 3 );
362 attribute = MSI_RecordGetString( row, 4 );
363 ControlEvent_SubscribeToEvent( dialog->package, event, control, attribute );
364 msiobj_release( &row->hdr );
367 /* everything except radio buttons */
368 static msi_control *msi_dialog_add_control( msi_dialog *dialog,
369 MSIRECORD *rec, LPCWSTR szCls, DWORD style )
374 name = MSI_RecordGetString( rec, 2 );
375 attributes = MSI_RecordGetInteger( rec, 8 );
376 text = MSI_RecordGetString( rec, 10 );
379 if( ~attributes & 2 )
380 style |= WS_DISABLED;
382 msi_dialog_map_events(dialog, name);
384 return msi_dialog_create_window( dialog, rec, szCls, name, text,
385 style, dialog->hwnd );
388 static UINT msi_dialog_text_control( msi_dialog *dialog, MSIRECORD *rec )
390 TRACE("%p %p\n", dialog, rec);
392 msi_dialog_add_control( dialog, rec, szStatic, 0 );
393 return ERROR_SUCCESS;
396 static UINT msi_dialog_button_control( msi_dialog *dialog, MSIRECORD *rec )
398 msi_control *control;
400 TRACE("%p %p\n", dialog, rec);
402 control = msi_dialog_add_control( dialog, rec, szButton, 0 );
403 control->handler = msi_dialog_button_handler;
405 return ERROR_SUCCESS;
408 static LPWSTR msi_get_checkbox_value( msi_dialog *dialog, LPCWSTR prop )
410 const static WCHAR query[] = {
411 'S','E','L','E','C','T',' ','*',' ',
412 'F','R','O','M',' ','`','C','h','e','c','k','B','o','x',' ','`',
413 'W','H','E','R','E',' ',
414 '`','P','r','o','p','e','r','t','y','`',' ','=',' ',
417 MSIRECORD *rec = NULL;
421 /* find if there is a value associated with the checkbox */
422 rec = MSI_QueryGetRecord( dialog->package->db, query, prop );
426 val = MSI_RecordGetString( rec, 2 );
429 deformat_string( dialog->package, val, &ret );
432 HeapFree( GetProcessHeap(), 0, ret );
436 msiobj_release( &rec->hdr );
440 ret = load_dynamic_property(dialog->package, prop, NULL);
443 HeapFree( GetProcessHeap(), 0, ret );
450 static UINT msi_dialog_checkbox_control( msi_dialog *dialog, MSIRECORD *rec )
452 msi_control *control;
455 TRACE("%p %p\n", dialog, rec);
457 control = msi_dialog_add_control( dialog, rec, szButton,
458 BS_CHECKBOX | BS_MULTILINE );
459 control->handler = msi_dialog_checkbox_handler;
460 prop = MSI_RecordGetString( rec, 9 );
463 control->property = strdupW( prop );
464 control->value = msi_get_checkbox_value( dialog, prop );
465 TRACE("control %s value %s\n", debugstr_w(control->property),
466 debugstr_w(control->value));
468 msi_dialog_checkbox_sync_state( dialog, control );
470 return ERROR_SUCCESS;
473 static UINT msi_dialog_line_control( msi_dialog *dialog, MSIRECORD *rec )
475 TRACE("%p %p\n", dialog, rec);
477 msi_dialog_add_control( dialog, rec, szStatic, SS_ETCHEDHORZ | SS_SUNKEN );
478 return ERROR_SUCCESS;
481 static UINT msi_dialog_scrolltext_control( msi_dialog *dialog, MSIRECORD *rec )
483 const static WCHAR szEdit[] = { 'E','D','I','T',0 };
485 FIXME("%p %p\n", dialog, rec);
487 msi_dialog_add_control( dialog, rec, szEdit, WS_BORDER |
488 ES_MULTILINE | WS_VSCROLL | ES_READONLY | ES_AUTOVSCROLL );
490 return ERROR_SUCCESS;
493 static UINT msi_load_bitmap( MSIDATABASE *db, LPCWSTR name, IPicture **pic )
495 const static WCHAR query[] = {
496 's','e','l','e','c','t',' ','*',' ',
497 'f','r','o','m',' ','B','i','n','a','r','y',' ',
498 'w','h','e','r','e',' ',
499 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0
501 MSIRECORD *rec = NULL;
505 rec = MSI_QueryGetRecord( db, query, name );
507 return ERROR_FUNCTION_FAILED;
509 r = MSI_RecordGetIStream( rec, 2, &stm );
510 msiobj_release( &rec->hdr );
511 if( r != ERROR_SUCCESS )
514 r = OleLoadPicture( stm, 0, TRUE, &IID_IPicture, (LPVOID*) pic );
515 IStream_Release( stm );
517 return ERROR_FUNCTION_FAILED;
519 return ERROR_SUCCESS;
522 static UINT msi_dialog_bitmap_control( msi_dialog *dialog, MSIRECORD *rec )
524 IPicture *pic = NULL;
525 msi_control *control;
526 OLE_HANDLE hBitmap = 0;
530 control = msi_dialog_add_control( dialog, rec, szStatic,
531 SS_BITMAP | SS_LEFT | SS_CENTERIMAGE );
532 text = MSI_RecordGetString( rec, 10 );
533 r = msi_load_bitmap( dialog->package->db, text, &pic );
534 if( r == ERROR_SUCCESS )
536 r = IPicture_get_Handle( pic, &hBitmap );
538 SendMessageW( control->hwnd, STM_SETIMAGE, IMAGE_BITMAP, hBitmap );
542 return ERROR_SUCCESS;
545 static UINT msi_dialog_combo_control( msi_dialog *dialog, MSIRECORD *rec )
547 static const WCHAR szCombo[] = { 'C','O','M','B','O','B','O','X',0 };
549 msi_dialog_add_control( dialog, rec, szCombo,
550 SS_BITMAP | SS_LEFT | SS_CENTERIMAGE );
551 return ERROR_SUCCESS;
554 static UINT msi_dialog_edit_control( msi_dialog *dialog, MSIRECORD *rec )
556 const static WCHAR szEdit[] = { 'E','D','I','T',0 };
557 msi_control *control;
561 control = msi_dialog_add_control( dialog, rec, szEdit, WS_BORDER );
562 control->handler = msi_dialog_edit_handler;
563 prop = MSI_RecordGetString( rec, 9 );
565 control->property = strdupW( prop );
566 val = load_dynamic_property( dialog->package, control->property, NULL );
567 SetWindowTextW( control->hwnd, val );
568 HeapFree( GetProcessHeap(), 0, val );
569 return ERROR_SUCCESS;
572 static UINT msi_dialog_pathedit_control( msi_dialog *dialog, MSIRECORD *rec )
574 FIXME("not implemented properly\n");
575 return msi_dialog_edit_control( dialog, rec );
578 /* radio buttons are a bit different from normal controls */
579 static UINT msi_dialog_create_radiobutton( MSIRECORD *rec, LPVOID param )
581 radio_button_group_descr *group = (radio_button_group_descr *)param;
582 msi_dialog *dialog = group->dialog;
583 msi_control *control;
584 LPCWSTR prop, text, name;
586 DWORD attributes = group->attributes;
588 style = WS_CHILD | BS_AUTORADIOBUTTON | BS_MULTILINE;
589 name = MSI_RecordGetString( rec, 3 );
590 text = MSI_RecordGetString( rec, 8 );
593 if( ~attributes & 2 )
594 style |= WS_DISABLED;
596 control = msi_dialog_create_window( dialog, rec, szButton, name, text,
597 style, group->parent->hwnd );
598 control->handler = msi_dialog_radiogroup_handler;
600 prop = MSI_RecordGetString( rec, 1 );
602 control->property = strdupW( prop );
604 return ERROR_SUCCESS;
607 static UINT msi_dialog_radiogroup_control( msi_dialog *dialog, MSIRECORD *rec )
609 static const WCHAR query[] = {
610 'S','E','L','E','C','T',' ','*',' ',
611 'F','R','O','M',' ','R','a','d','i','o','B','u','t','t','o','n',' ',
612 'W','H','E','R','E',' ',
613 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
616 msi_control *control;
617 MSIQUERY *view = NULL;
618 radio_button_group_descr group;
619 MSIPACKAGE *package = dialog->package;
621 prop = MSI_RecordGetString( rec, 9 );
623 TRACE("%p %p %s\n", dialog, rec, debugstr_w( prop ));
625 /* Create parent group box to hold radio buttons */
626 control = msi_dialog_add_control( dialog, rec, szButton, BS_OWNERDRAW );
630 WNDPROC oldproc = (WNDPROC) SetWindowLongPtrW(control->hwnd, GWLP_WNDPROC,
631 (LONG_PTR)MSIRadioGroup_WndProc);
632 SetPropW(control->hwnd, szButtonData, oldproc);
636 control->property = strdupW( prop );
638 /* query the Radio Button table for all control in this group */
639 r = MSI_OpenQuery( package->db, &view, query, prop );
640 if( r != ERROR_SUCCESS )
642 ERR("query failed for dialog %s radio group %s\n",
643 debugstr_w(dialog->name), debugstr_w(prop));
644 return ERROR_INVALID_PARAMETER;
647 group.dialog = dialog;
648 group.parent = control;
649 group.attributes = MSI_RecordGetInteger( rec, 8 );
651 r = MSI_IterateRecords( view, 0, msi_dialog_create_radiobutton, &group );
652 msiobj_release( &view->hdr );
657 struct control_handler msi_dialog_handler[] =
659 { szText, msi_dialog_text_control },
660 { szPushButton, msi_dialog_button_control },
661 { szLine, msi_dialog_line_control },
662 { szBitmap, msi_dialog_bitmap_control },
663 { szCheckBox, msi_dialog_checkbox_control },
664 { szScrollableText, msi_dialog_scrolltext_control },
665 { szComboBox, msi_dialog_combo_control },
666 { szEdit, msi_dialog_edit_control },
667 { szMaskedEdit, msi_dialog_edit_control },
668 { szPathEdit, msi_dialog_pathedit_control },
669 { szRadioButtonGroup, msi_dialog_radiogroup_control },
672 #define NUM_CONTROL_TYPES (sizeof msi_dialog_handler/sizeof msi_dialog_handler[0])
674 static UINT msi_dialog_create_controls( MSIRECORD *rec, LPVOID param )
676 msi_dialog *dialog = param;
677 LPCWSTR control_type;
680 /* find and call the function that can create this type of control */
681 control_type = MSI_RecordGetString( rec, 3 );
682 for( i=0; i<NUM_CONTROL_TYPES; i++ )
683 if (!strcmpiW( msi_dialog_handler[i].control_type, control_type ))
685 if( i != NUM_CONTROL_TYPES )
686 msi_dialog_handler[i].func( dialog, rec );
688 ERR("no handler for element type %s\n", debugstr_w(control_type));
690 return ERROR_SUCCESS;
693 static UINT msi_dialog_fill_controls( msi_dialog *dialog )
695 static const WCHAR query[] = {
696 'S','E','L','E','C','T',' ','*',' ',
697 'F','R','O','M',' ','C','o','n','t','r','o','l',' ',
698 'W','H','E','R','E',' ',
699 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
701 MSIQUERY *view = NULL;
702 MSIPACKAGE *package = dialog->package;
704 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
706 /* query the Control table for all the elements of the control */
707 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
708 if( r != ERROR_SUCCESS )
710 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
711 return ERROR_INVALID_PARAMETER;
714 r = MSI_IterateRecords( view, 0, msi_dialog_create_controls, dialog );
715 msiobj_release( &view->hdr );
720 static UINT msi_dialog_set_control_condition( MSIRECORD *rec, LPVOID param )
722 static const WCHAR szHide[] = { 'H','i','d','e',0 };
723 static const WCHAR szShow[] = { 'S','h','o','w',0 };
724 static const WCHAR szDisable[] = { 'D','i','s','a','b','l','e',0 };
725 static const WCHAR szEnable[] = { 'E','n','a','b','l','e',0 };
726 msi_dialog *dialog = param;
727 msi_control *control;
728 LPCWSTR name, action, condition;
731 name = MSI_RecordGetString( rec, 2 );
732 action = MSI_RecordGetString( rec, 3 );
733 condition = MSI_RecordGetString( rec, 4 );
734 r = MSI_EvaluateConditionW( dialog->package, condition );
735 control = msi_dialog_find_control( dialog, name );
738 TRACE("%s control %s\n", debugstr_w(action), debugstr_w(name));
740 /* FIXME: case sensitive? */
741 if(!strcmpW(action, szHide))
742 ShowWindow(control->hwnd, SW_HIDE);
743 else if(!strcmpW(action, szShow))
744 ShowWindow(control->hwnd, SW_SHOW);
745 else if(!strcmpW(action, szDisable))
746 EnableWindow(control->hwnd, FALSE);
747 else if(!strcmpW(action, szEnable))
748 EnableWindow(control->hwnd, TRUE);
750 FIXME("Unhandled action %s\n", debugstr_w(action));
753 return ERROR_SUCCESS;
756 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog )
758 static const WCHAR query[] = {
759 'S','E','L','E','C','T',' ','*',' ',
761 'C','o','n','t','r','o','l','C','o','n','d','i','t','i','o','n',' ',
762 'W','H','E','R','E',' ',
763 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0
766 MSIQUERY *view = NULL;
767 MSIPACKAGE *package = dialog->package;
769 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
771 /* query the Control table for all the elements of the control */
772 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
773 if( r != ERROR_SUCCESS )
775 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
776 return ERROR_INVALID_PARAMETER;
779 r = MSI_IterateRecords( view, 0, msi_dialog_set_control_condition, dialog );
780 msiobj_release( &view->hdr );
785 /* figure out the height of 10 point MS Sans Serif */
786 static INT msi_dialog_get_sans_serif_height( HWND hwnd )
788 static const WCHAR szSansSerif[] = {
789 'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0 };
794 HFONT hFont, hOldFont;
800 memset( &lf, 0, sizeof lf );
801 lf.lfHeight = MulDiv(10, GetDeviceCaps(hdc, LOGPIXELSY), 72);
802 strcpyW( lf.lfFaceName, szSansSerif );
803 hFont = CreateFontIndirectW(&lf);
806 hOldFont = SelectObject( hdc, hFont );
807 r = GetTextMetricsW( hdc, &tm );
809 height = tm.tmHeight;
810 SelectObject( hdc, hOldFont );
811 DeleteObject( hFont );
813 ReleaseDC( hwnd, hdc );
818 /* fetch the associated record from the Dialog table */
819 static MSIRECORD *msi_get_dialog_record( msi_dialog *dialog )
821 static const WCHAR query[] = {
822 'S','E','L','E','C','T',' ','*',' ',
823 'F','R','O','M',' ','D','i','a','l','o','g',' ',
824 'W','H','E','R','E',' ',
825 '`','D','i','a','l','o','g','`',' ','=',' ','\'','%','s','\'',0};
826 MSIPACKAGE *package = dialog->package;
827 MSIRECORD *rec = NULL;
829 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
831 rec = MSI_QueryGetRecord( package->db, query, dialog->name );
833 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
838 static void msi_dialog_adjust_dialog_size( msi_dialog *dialog, LPSIZE sz )
843 /* turn the client size into the window rectangle */
846 rect.right = msi_dialog_scale_unit( dialog, sz->cx );
847 rect.bottom = msi_dialog_scale_unit( dialog, sz->cy );
848 style = GetWindowLongPtrW( dialog->hwnd, GWL_STYLE );
849 AdjustWindowRect( &rect, style, FALSE );
850 sz->cx = rect.right - rect.left;
851 sz->cy = rect.bottom - rect.top;
854 static LRESULT msi_dialog_oncreate( HWND hwnd, LPCREATESTRUCTW cs )
856 static const WCHAR df[] = {
857 'D','e','f','a','u','l','t','U','I','F','o','n','t',0 };
858 msi_dialog *dialog = (msi_dialog*) cs->lpCreateParams;
859 MSIRECORD *rec = NULL;
864 TRACE("%p %p\n", dialog, dialog->package);
867 SetWindowLongPtrW( hwnd, GWLP_USERDATA, (LONG_PTR) dialog );
869 rec = msi_get_dialog_record( dialog );
872 TRACE("No record found for dialog %s\n", debugstr_w(dialog->name));
876 dialog->scale = msi_dialog_get_sans_serif_height(dialog->hwnd);
878 size.cx = MSI_RecordGetInteger( rec, 4 );
879 size.cy = MSI_RecordGetInteger( rec, 5 );
880 msi_dialog_adjust_dialog_size( dialog, &size );
882 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
883 text = MSI_RecordGetString( rec, 7 );
885 dialog->default_font = load_dynamic_property( dialog->package, df, NULL );
887 deformat_string( dialog->package, text, &title );
888 SetWindowTextW( hwnd, title );
889 SetWindowPos( hwnd, 0, 0, 0, size.cx, size.cy,
890 SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW );
892 HeapFree( GetProcessHeap(), 0, title );
893 msiobj_release( &rec->hdr );
895 msi_dialog_build_font_list( dialog );
896 msi_dialog_fill_controls( dialog );
897 msi_dialog_evaluate_control_conditions( dialog );
902 static UINT msi_dialog_send_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
904 LPWSTR event_fmt = NULL, arg_fmt = NULL;
906 TRACE("Sending control event %s %s\n", debugstr_w(event), debugstr_w(arg));
908 deformat_string( dialog->package, event, &event_fmt );
909 deformat_string( dialog->package, arg, &arg_fmt );
911 dialog->event_handler( dialog->package, event_fmt, arg_fmt, dialog );
913 HeapFree( GetProcessHeap(), 0, event_fmt );
914 HeapFree( GetProcessHeap(), 0, arg_fmt );
916 return ERROR_SUCCESS;
919 static UINT msi_dialog_set_property( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
921 static const WCHAR szNullArg[] = { '{','}',0 };
922 LPWSTR p, prop, arg_fmt = NULL;
925 len = strlenW(event);
926 prop = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR));
927 strcpyW( prop, &event[1] );
928 p = strchrW( prop, ']' );
932 if( strcmpW( szNullArg, arg ) )
933 deformat_string( dialog->package, arg, &arg_fmt );
934 MSI_SetPropertyW( dialog->package, prop, arg_fmt );
937 ERR("Badly formatted property string - what happens?\n");
938 HeapFree( GetProcessHeap(), 0, prop );
939 return ERROR_SUCCESS;
942 static UINT msi_dialog_control_event( MSIRECORD *rec, LPVOID param )
944 msi_dialog *dialog = param;
945 LPCWSTR condition, event, arg;
948 condition = MSI_RecordGetString( rec, 5 );
949 r = MSI_EvaluateConditionW( dialog->package, condition );
952 event = MSI_RecordGetString( rec, 3 );
953 arg = MSI_RecordGetString( rec, 4 );
954 if( event[0] == '[' )
955 msi_dialog_set_property( dialog, event, arg );
957 msi_dialog_send_event( dialog, event, arg );
960 return ERROR_SUCCESS;
963 static UINT msi_dialog_button_handler( msi_dialog *dialog,
964 msi_control *control, WPARAM param )
966 static const WCHAR query[] = {
967 'S','E','L','E','C','T',' ','*',' ',
968 'F','R','O','M',' ','C','o','n','t','r','o','l','E','v','e','n','t',' ',
969 'W','H','E','R','E',' ',
970 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
972 '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',' ',
973 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','i','n','g','`',0
975 MSIQUERY *view = NULL;
978 if( HIWORD(param) != BN_CLICKED )
979 return ERROR_SUCCESS;
981 r = MSI_OpenQuery( dialog->package->db, &view, query,
982 dialog->name, control->name );
983 if( r != ERROR_SUCCESS )
985 ERR("query failed\n");
989 r = MSI_IterateRecords( view, 0, msi_dialog_control_event, dialog );
990 msiobj_release( &view->hdr );
995 static UINT msi_dialog_get_checkbox_state( msi_dialog *dialog,
996 msi_control *control )
998 WCHAR state[2] = { 0 };
1001 MSI_GetPropertyW( dialog->package, control->property, state, &sz );
1002 return state[0] ? 1 : 0;
1005 static void msi_dialog_set_checkbox_state( msi_dialog *dialog,
1006 msi_control *control, UINT state )
1008 static const WCHAR szState[] = { '1', 0 };
1011 /* if uncheck then the property is set to NULL */
1014 MSI_SetPropertyW( dialog->package, control->property, NULL );
1018 /* check for a custom state */
1019 if (control->value && control->value[0])
1020 val = control->value;
1024 MSI_SetPropertyW( dialog->package, control->property, val );
1027 static void msi_dialog_checkbox_sync_state( msi_dialog *dialog,
1028 msi_control *control )
1032 state = msi_dialog_get_checkbox_state( dialog, control );
1033 SendMessageW( control->hwnd, BM_SETCHECK,
1034 state ? BST_CHECKED : BST_UNCHECKED, 0 );
1037 static UINT msi_dialog_checkbox_handler( msi_dialog *dialog,
1038 msi_control *control, WPARAM param )
1042 if( HIWORD(param) != BN_CLICKED )
1043 return ERROR_SUCCESS;
1045 TRACE("clicked checkbox %s, set %s\n", debugstr_w(control->name),
1046 debugstr_w(control->property));
1048 state = msi_dialog_get_checkbox_state( dialog, control );
1049 state = state ? 0 : 1;
1050 msi_dialog_set_checkbox_state( dialog, control, state );
1051 msi_dialog_checkbox_sync_state( dialog, control );
1053 return msi_dialog_button_handler( dialog, control, param );
1056 static UINT msi_dialog_edit_handler( msi_dialog *dialog,
1057 msi_control *control, WPARAM param )
1062 if( HIWORD(param) != EN_CHANGE )
1063 return ERROR_SUCCESS;
1065 TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
1066 debugstr_w(control->property));
1069 buf = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
1072 r = GetWindowTextW( control->hwnd, buf, sz );
1076 buf = HeapReAlloc( GetProcessHeap(), 0, buf, sz*sizeof(WCHAR) );
1079 MSI_SetPropertyW( dialog->package, control->property, buf );
1081 HeapFree( GetProcessHeap(), 0, buf );
1083 return ERROR_SUCCESS;
1086 static UINT msi_dialog_radiogroup_handler( msi_dialog *dialog,
1087 msi_control *control, WPARAM param )
1089 if( HIWORD(param) != BN_CLICKED )
1090 return ERROR_SUCCESS;
1092 TRACE("clicked radio button %s, set %s\n", debugstr_w(control->name),
1093 debugstr_w(control->property));
1095 MSI_SetPropertyW( dialog->package, control->property, control->name );
1097 return msi_dialog_button_handler( dialog, control, param );
1100 static LRESULT msi_dialog_oncommand( msi_dialog *dialog, WPARAM param, HWND hwnd )
1102 msi_control *control;
1104 TRACE("%p %p %08x\n", dialog, hwnd, param);
1106 control = msi_dialog_find_control_by_hwnd( dialog, hwnd );
1109 if( control->handler )
1111 control->handler( dialog, control, param );
1112 msi_dialog_evaluate_control_conditions( dialog );
1116 ERR("button click from nowhere\n");
1120 static LRESULT WINAPI MSIDialog_WndProc( HWND hwnd, UINT msg,
1121 WPARAM wParam, LPARAM lParam )
1123 msi_dialog *dialog = (LPVOID) GetWindowLongPtrW( hwnd, GWLP_USERDATA );
1125 TRACE("0x%04x\n", msg);
1130 return msi_dialog_oncreate( hwnd, (LPCREATESTRUCTW)lParam );
1133 return msi_dialog_oncommand( dialog, wParam, (HWND)lParam );
1136 dialog->hwnd = NULL;
1139 return DefWindowProcW(hwnd, msg, wParam, lParam);
1142 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1144 WNDPROC oldproc = (WNDPROC) GetPropW(hWnd, szButtonData);
1146 TRACE("hWnd %p msg %04x wParam 0x%08x lParam 0x%08lx\n", hWnd, msg, wParam, lParam);
1148 if (msg == WM_COMMAND) /* Forward notifications to dialog */
1149 SendMessageW(GetParent(hWnd), msg, wParam, lParam);
1151 return CallWindowProcW(oldproc, hWnd, msg, wParam, lParam);
1154 static LRESULT WINAPI MSIHiddenWindowProc( HWND hwnd, UINT msg,
1155 WPARAM wParam, LPARAM lParam )
1157 msi_dialog *dialog = (msi_dialog*) lParam;
1159 TRACE("%d %p\n", msg, dialog);
1163 case WM_MSI_DIALOG_CREATE:
1164 return msi_dialog_run_message_loop( dialog );
1165 case WM_MSI_DIALOG_DESTROY:
1166 msi_dialog_destroy( dialog );
1169 return DefWindowProcW( hwnd, msg, wParam, lParam );
1172 /* functions that interface to other modules within MSI */
1174 msi_dialog *msi_dialog_create( MSIPACKAGE* package, LPCWSTR szDialogName,
1175 msi_dialog_event_handler event_handler )
1177 MSIRECORD *rec = NULL;
1180 TRACE("%p %s\n", package, debugstr_w(szDialogName));
1182 /* allocate the structure for the dialog to use */
1183 dialog = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
1184 sizeof *dialog + sizeof(WCHAR)*strlenW(szDialogName) );
1187 strcpyW( dialog->name, szDialogName );
1188 msiobj_addref( &package->hdr );
1189 dialog->package = package;
1190 dialog->event_handler = event_handler;
1191 dialog->finished = 0;
1193 /* verify that the dialog exists */
1194 rec = msi_get_dialog_record( dialog );
1197 HeapFree( GetProcessHeap(), 0, dialog );
1200 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
1201 msiobj_release( &rec->hdr );
1206 static void msi_process_pending_messages(void)
1210 while( PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ) )
1212 TranslateMessage( &msg );
1213 DispatchMessageW( &msg );
1217 void msi_dialog_end_dialog( msi_dialog *dialog )
1219 TRACE("%p\n", dialog);
1220 dialog->finished = 1;
1221 PostMessageW(dialog->hwnd, WM_NULL, 0, 0);
1224 void msi_dialog_check_messages( HANDLE handle )
1228 /* in threads other than the UI thread, block */
1229 if( uiThreadId != GetCurrentThreadId() )
1232 WaitForSingleObject( handle, INFINITE );
1236 /* there's two choices for the UI thread */
1239 msi_process_pending_messages();
1245 * block here until somebody creates a new dialog or
1246 * the handle we're waiting on becomes ready
1248 r = MsgWaitForMultipleObjects( 1, &handle, 0, INFINITE, QS_ALLINPUT );
1249 if( r == WAIT_OBJECT_0 )
1254 UINT msi_dialog_run_message_loop( msi_dialog *dialog )
1258 if( !(dialog->attributes & msidbDialogAttributesVisible) )
1259 return ERROR_SUCCESS;
1261 if( uiThreadId != GetCurrentThreadId() )
1262 return SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_CREATE, 0, (LPARAM) dialog );
1264 /* create the dialog window, don't show it yet */
1265 hwnd = CreateWindowW( szMsiDialogClass, dialog->name, WS_OVERLAPPEDWINDOW,
1266 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
1267 NULL, NULL, NULL, dialog );
1270 ERR("Failed to create dialog %s\n", debugstr_w( dialog->name ));
1271 return ERROR_FUNCTION_FAILED;
1274 ShowWindow( hwnd, SW_SHOW );
1275 UpdateWindow( hwnd );
1277 if( dialog->attributes & msidbDialogAttributesModal )
1279 while( !dialog->finished )
1281 MsgWaitForMultipleObjects( 0, NULL, 0, INFINITE, QS_ALLEVENTS );
1282 msi_process_pending_messages();
1286 return ERROR_IO_PENDING;
1288 return ERROR_SUCCESS;
1291 void msi_dialog_do_preview( msi_dialog *dialog )
1294 dialog->attributes |= msidbDialogAttributesVisible;
1295 dialog->attributes &= ~msidbDialogAttributesModal;
1296 msi_dialog_run_message_loop( dialog );
1299 void msi_dialog_destroy( msi_dialog *dialog )
1301 if( uiThreadId != GetCurrentThreadId() )
1303 SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_DESTROY, 0, (LPARAM) dialog );
1308 ShowWindow( dialog->hwnd, SW_HIDE );
1310 /* destroy the list of controls */
1311 while( dialog->control_list )
1313 msi_control *t = dialog->control_list;
1314 dialog->control_list = t->next;
1315 /* leave dialog->hwnd - destroying parent destroys child windows */
1316 HeapFree( GetProcessHeap(), 0, t->property );
1317 HeapFree( GetProcessHeap(), 0, t->value );
1319 IPicture_Release( t->pic );
1320 HeapFree( GetProcessHeap(), 0, t );
1323 /* destroy the list of fonts */
1324 while( dialog->font_list )
1326 msi_font *t = dialog->font_list;
1327 dialog->font_list = t->next;
1328 DeleteObject( t->hfont );
1329 HeapFree( GetProcessHeap(), 0, t );
1331 HeapFree( GetProcessHeap(), 0, dialog->default_font );
1334 DestroyWindow( dialog->hwnd );
1336 msiobj_release( &dialog->package->hdr );
1337 dialog->package = NULL;
1338 HeapFree( GetProcessHeap(), 0, dialog );
1341 BOOL msi_dialog_register_class( void )
1345 ZeroMemory( &cls, sizeof cls );
1346 cls.lpfnWndProc = MSIDialog_WndProc;
1347 cls.hInstance = NULL;
1348 cls.hIcon = LoadIconW(0, (LPWSTR)IDI_APPLICATION);
1349 cls.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
1350 cls.hbrBackground = (HBRUSH)(COLOR_WINDOW);
1351 cls.lpszMenuName = NULL;
1352 cls.lpszClassName = szMsiDialogClass;
1354 if( !RegisterClassW( &cls ) )
1357 cls.lpfnWndProc = MSIHiddenWindowProc;
1358 cls.lpszClassName = szMsiHiddenWindow;
1360 if( !RegisterClassW( &cls ) )
1363 uiThreadId = GetCurrentThreadId();
1365 hMsiHiddenWindow = CreateWindowW( szMsiHiddenWindow, NULL, WS_OVERLAPPED,
1366 0, 0, 100, 100, NULL, NULL, NULL, NULL );
1367 if( !hMsiHiddenWindow )
1373 void msi_dialog_unregister_class( void )
1375 DestroyWindow( hMsiHiddenWindow );
1376 UnregisterClassW( szMsiDialogClass, NULL );