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
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(msi);
45 struct msi_control_tag;
46 typedef struct msi_control_tag msi_control;
47 typedef UINT (*msi_handler)( msi_dialog *, msi_control *, WPARAM );
49 struct msi_control_tag
51 struct msi_control_tag *next;
60 typedef struct msi_font_tag
62 struct msi_font_tag *next;
70 msi_dialog_event_handler event_handler;
77 msi_control *control_list;
81 typedef UINT (*msi_dialog_control_func)( msi_dialog *dialog, MSIRECORD *rec );
82 struct control_handler
85 msi_dialog_control_func func;
93 } radio_button_group_descr;
95 const WCHAR szMsiDialogClass[] = {
96 'M','s','i','D','i','a','l','o','g','C','l','o','s','e','C','l','a','s','s',0
98 const WCHAR szMsiHiddenWindow[] = {
99 'M','s','i','H','i','d','d','e','n','W','i','n','d','o','w',0 };
100 const static WCHAR szStatic[] = { 'S','t','a','t','i','c',0 };
101 const static WCHAR szButton[] = { 'B','U','T','T','O','N', 0 };
102 const static WCHAR szButtonData[] = { 'M','S','I','D','A','T','A',0 };
103 static const WCHAR szText[] = { 'T','e','x','t',0 };
104 static const WCHAR szPushButton[] = { 'P','u','s','h','B','u','t','t','o','n',0 };
105 static const WCHAR szLine[] = { 'L','i','n','e',0 };
106 static const WCHAR szBitmap[] = { 'B','i','t','m','a','p',0 };
107 static const WCHAR szCheckBox[] = { 'C','h','e','c','k','B','o','x',0 };
108 static const WCHAR szScrollableText[] = {
109 'S','c','r','o','l','l','a','b','l','e','T','e','x','t',0 };
110 static const WCHAR szComboBox[] = { 'C','o','m','b','o','B','o','x',0 };
111 static const WCHAR szEdit[] = { 'E','d','i','t',0 };
112 static const WCHAR szMaskedEdit[] = { 'M','a','s','k','e','d','E','d','i','t',0 };
113 static const WCHAR szPathEdit[] = { 'P','a','t','h','E','d','i','t',0 };
114 static const WCHAR szRadioButtonGroup[] = {
115 'R','a','d','i','o','B','u','t','t','o','n','G','r','o','u','p',0 };
117 static UINT msi_dialog_checkbox_handler( msi_dialog *, msi_control *, WPARAM );
118 static void msi_dialog_checkbox_sync_state( msi_dialog *, msi_control * );
119 static UINT msi_dialog_button_handler( msi_dialog *, msi_control *, WPARAM );
120 static UINT msi_dialog_edit_handler( msi_dialog *, msi_control *, WPARAM );
121 static UINT msi_dialog_radiogroup_handler( msi_dialog *, msi_control *, WPARAM param );
122 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
125 /* dialog sequencing */
127 #define WM_MSI_DIALOG_CREATE (WM_USER+0x100)
128 #define WM_MSI_DIALOG_DESTROY (WM_USER+0x101)
130 static DWORD uiThreadId;
131 static HWND hMsiHiddenWindow;
132 static HMODULE hRichedit;
134 static INT msi_dialog_scale_unit( msi_dialog *dialog, INT val )
136 return (dialog->scale * val + 5) / 10;
139 static msi_control *msi_dialog_find_control( msi_dialog *dialog, LPCWSTR name )
141 msi_control *control;
143 for( control = dialog->control_list; control; control = control->next )
144 if( !strcmpW( control->name, name ) ) /* FIXME: case sensitive? */
149 static msi_control *msi_dialog_find_control_by_hwnd( msi_dialog *dialog, HWND hwnd )
151 msi_control *control;
153 for( control = dialog->control_list; control; control = control->next )
154 if( hwnd == control->hwnd )
160 * msi_dialog_get_style
162 * Extract the {\style} string from the front of the text to display and
163 * update the pointer.
165 static LPWSTR msi_dialog_get_style( LPCWSTR *text )
168 LPCWSTR p = *text, q;
173 q = strchrW( p, '}' );
181 ret = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
184 memcpy( ret, p, len*sizeof(WCHAR) );
189 static UINT msi_dialog_add_font( MSIRECORD *rec, LPVOID param )
191 msi_dialog *dialog = param;
198 /* create a font and add it to the list */
199 name = MSI_RecordGetString( rec, 1 );
200 font = HeapAlloc( GetProcessHeap(), 0,
201 sizeof *font + strlenW( name )*sizeof (WCHAR) );
202 strcpyW( font->name, name );
203 font->next = dialog->font_list;
204 dialog->font_list = font;
206 memset( &lf, 0, sizeof lf );
207 face = MSI_RecordGetString( rec, 2 );
208 lf.lfHeight = MSI_RecordGetInteger( rec, 3 );
209 style = MSI_RecordGetInteger( rec, 5 );
210 if( style & msidbTextStyleStyleBitsBold )
211 lf.lfWeight = FW_BOLD;
212 if( style & msidbTextStyleStyleBitsItalic )
214 if( style & msidbTextStyleStyleBitsUnderline )
215 lf.lfUnderline = TRUE;
216 if( style & msidbTextStyleStyleBitsStrike )
217 lf.lfStrikeOut = TRUE;
218 lstrcpynW( lf.lfFaceName, face, LF_FACESIZE );
220 /* adjust the height */
221 hdc = GetDC( dialog->hwnd );
224 lf.lfHeight = -MulDiv(lf.lfHeight, GetDeviceCaps(hdc, LOGPIXELSY), 72);
225 ReleaseDC( dialog->hwnd, hdc );
228 font->hfont = CreateFontIndirectW( &lf );
230 TRACE("Adding font style %s\n", debugstr_w(font->name) );
232 return ERROR_SUCCESS;
235 static msi_font *msi_dialog_find_font( msi_dialog *dialog, LPCWSTR name )
239 for( font = dialog->font_list; font; font = font->next )
240 if( !strcmpW( font->name, name ) ) /* FIXME: case sensitive? */
246 static UINT msi_dialog_set_font( msi_dialog *dialog, HWND hwnd, LPCWSTR name )
250 font = msi_dialog_find_font( dialog, name );
252 SendMessageW( hwnd, WM_SETFONT, (WPARAM) font->hfont, TRUE );
254 ERR("No font entry for %s\n", debugstr_w(name));
255 return ERROR_SUCCESS;
258 static UINT msi_dialog_build_font_list( msi_dialog *dialog )
260 static const WCHAR query[] = {
261 'S','E','L','E','C','T',' ','*',' ',
262 'F','R','O','M',' ','`','T','e','x','t','S','t','y','l','e','`',' ',0
265 MSIQUERY *view = NULL;
267 TRACE("dialog %p\n", dialog );
269 r = MSI_OpenQuery( dialog->package->db, &view, query );
270 if( r != ERROR_SUCCESS )
273 r = MSI_IterateRecords( view, NULL, msi_dialog_add_font, dialog );
274 msiobj_release( &view->hdr );
279 static msi_control *msi_dialog_create_window( msi_dialog *dialog,
280 MSIRECORD *rec, LPCWSTR szCls, LPCWSTR name, LPCWSTR text,
281 DWORD style, HWND parent )
283 DWORD x, y, width, height;
284 LPWSTR font = NULL, title = NULL;
285 msi_control *control;
289 control = HeapAlloc( GetProcessHeap(), 0,
290 sizeof *control + strlenW(name)*sizeof(WCHAR) );
291 strcpyW( control->name, name );
292 control->next = dialog->control_list;
293 dialog->control_list = control;
294 control->handler = NULL;
295 control->property = NULL;
296 control->value = NULL;
299 x = MSI_RecordGetInteger( rec, 4 );
300 y = MSI_RecordGetInteger( rec, 5 );
301 width = MSI_RecordGetInteger( rec, 6 );
302 height = MSI_RecordGetInteger( rec, 7 );
304 x = msi_dialog_scale_unit( dialog, x );
305 y = msi_dialog_scale_unit( dialog, y );
306 width = msi_dialog_scale_unit( dialog, width );
307 height = msi_dialog_scale_unit( dialog, height );
311 font = msi_dialog_get_style( &text );
312 deformat_string( dialog->package, text, &title );
315 control->hwnd = CreateWindowW( szCls, title, style,
316 x, y, width, height, parent, NULL, NULL, NULL );
318 TRACE("Dialog %s control %s hwnd %p\n",
319 debugstr_w(dialog->name), debugstr_w(text), control->hwnd );
321 msi_dialog_set_font( dialog, control->hwnd,
322 font ? font : dialog->default_font );
324 HeapFree( GetProcessHeap(), 0, font );
325 HeapFree( GetProcessHeap(), 0, title );
330 /* called from the Control Event subscription code */
331 void msi_dialog_handle_event( msi_dialog* dialog, LPCWSTR control,
332 LPCWSTR attribute, MSIRECORD *rec )
337 ctrl = msi_dialog_find_control( dialog, control );
340 if( lstrcmpW(attribute, szText) )
342 text = MSI_RecordGetString( rec , 1 );
343 SetWindowTextW( ctrl->hwnd, text );
346 static void msi_dialog_map_events(msi_dialog* dialog, LPCWSTR control)
348 static WCHAR Query[] = {
349 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
350 '`','E','v','e','n','t','M','a','p','p','i','n','g','`',' ',
351 'W','H','E','R','E',' ',
352 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
354 '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',0
357 LPCWSTR event, attribute;
359 row = MSI_QueryGetRecord( dialog->package->db, Query, dialog->name, control );
363 event = MSI_RecordGetString( row, 3 );
364 attribute = MSI_RecordGetString( row, 4 );
365 ControlEvent_SubscribeToEvent( dialog->package, event, control, attribute );
366 msiobj_release( &row->hdr );
369 /* everything except radio buttons */
370 static msi_control *msi_dialog_add_control( msi_dialog *dialog,
371 MSIRECORD *rec, LPCWSTR szCls, DWORD style )
376 name = MSI_RecordGetString( rec, 2 );
377 attributes = MSI_RecordGetInteger( rec, 8 );
378 text = MSI_RecordGetString( rec, 10 );
381 if( ~attributes & 2 )
382 style |= WS_DISABLED;
384 msi_dialog_map_events(dialog, name);
386 return msi_dialog_create_window( dialog, rec, szCls, name, text,
387 style, dialog->hwnd );
390 static UINT msi_dialog_text_control( msi_dialog *dialog, MSIRECORD *rec )
392 TRACE("%p %p\n", dialog, rec);
394 msi_dialog_add_control( dialog, rec, szStatic, 0 );
395 return ERROR_SUCCESS;
398 static UINT msi_dialog_button_control( msi_dialog *dialog, MSIRECORD *rec )
400 msi_control *control;
402 TRACE("%p %p\n", dialog, rec);
404 control = msi_dialog_add_control( dialog, rec, szButton, 0 );
405 control->handler = msi_dialog_button_handler;
407 return ERROR_SUCCESS;
410 static LPWSTR msi_get_checkbox_value( msi_dialog *dialog, LPCWSTR prop )
412 const static WCHAR query[] = {
413 'S','E','L','E','C','T',' ','*',' ',
414 'F','R','O','M',' ','`','C','h','e','c','k','B','o','x',' ','`',
415 'W','H','E','R','E',' ',
416 '`','P','r','o','p','e','r','t','y','`',' ','=',' ',
419 MSIRECORD *rec = NULL;
423 /* find if there is a value associated with the checkbox */
424 rec = MSI_QueryGetRecord( dialog->package->db, query, prop );
428 val = MSI_RecordGetString( rec, 2 );
431 deformat_string( dialog->package, val, &ret );
434 HeapFree( GetProcessHeap(), 0, ret );
438 msiobj_release( &rec->hdr );
442 ret = load_dynamic_property(dialog->package, prop, NULL);
445 HeapFree( GetProcessHeap(), 0, ret );
452 static UINT msi_dialog_checkbox_control( msi_dialog *dialog, MSIRECORD *rec )
454 msi_control *control;
457 TRACE("%p %p\n", dialog, rec);
459 control = msi_dialog_add_control( dialog, rec, szButton,
460 BS_CHECKBOX | BS_MULTILINE );
461 control->handler = msi_dialog_checkbox_handler;
462 prop = MSI_RecordGetString( rec, 9 );
465 control->property = strdupW( prop );
466 control->value = msi_get_checkbox_value( dialog, prop );
467 TRACE("control %s value %s\n", debugstr_w(control->property),
468 debugstr_w(control->value));
470 msi_dialog_checkbox_sync_state( dialog, control );
472 return ERROR_SUCCESS;
475 static UINT msi_dialog_line_control( msi_dialog *dialog, MSIRECORD *rec )
477 TRACE("%p %p\n", dialog, rec);
479 msi_dialog_add_control( dialog, rec, szStatic, SS_ETCHEDHORZ | SS_SUNKEN );
480 return ERROR_SUCCESS;
483 struct msi_streamin_info
490 static DWORD CALLBACK
491 msi_richedit_stream_in( DWORD_PTR arg, LPBYTE buffer, LONG count, LONG *pcb )
493 struct msi_streamin_info *info = (struct msi_streamin_info*) arg;
495 if( (count + info->offset) > info->length )
496 count = info->length - info->offset;
497 memcpy( buffer, &info->string[ info->offset ], count );
499 info->offset += count;
501 TRACE("%ld/%ld\n", info->offset, info->length);
506 static UINT msi_dialog_scrolltext_control( msi_dialog *dialog, MSIRECORD *rec )
508 const LPCWSTR szRichEdit = RICHEDIT_CLASS20W;
509 struct msi_streamin_info info;
510 msi_control *control;
515 style = WS_BORDER | ES_MULTILINE | WS_VSCROLL | ES_READONLY | ES_AUTOVSCROLL;
516 control = msi_dialog_add_control( dialog, rec, szRichEdit, style );
518 text = MSI_RecordGetString( rec, 10 );
519 info.string = strdupWtoA( text );
521 info.length = lstrlenA( info.string ) + 1;
523 es.dwCookie = (DWORD_PTR) &info;
525 es.pfnCallback = msi_richedit_stream_in;
527 SendMessageW( control->hwnd, EM_STREAMIN, SF_RTF, (LPARAM) &es );
529 HeapFree( GetProcessHeap(), 0, info.string );
531 return ERROR_SUCCESS;
534 static UINT msi_load_bitmap( MSIDATABASE *db, LPCWSTR name, IPicture **pic )
536 const static WCHAR query[] = {
537 's','e','l','e','c','t',' ','*',' ',
538 'f','r','o','m',' ','B','i','n','a','r','y',' ',
539 'w','h','e','r','e',' ',
540 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0
542 MSIRECORD *rec = NULL;
546 rec = MSI_QueryGetRecord( db, query, name );
548 return ERROR_FUNCTION_FAILED;
550 r = MSI_RecordGetIStream( rec, 2, &stm );
551 msiobj_release( &rec->hdr );
552 if( r != ERROR_SUCCESS )
555 r = OleLoadPicture( stm, 0, TRUE, &IID_IPicture, (LPVOID*) pic );
556 IStream_Release( stm );
558 return ERROR_FUNCTION_FAILED;
560 return ERROR_SUCCESS;
563 static UINT msi_dialog_bitmap_control( msi_dialog *dialog, MSIRECORD *rec )
565 IPicture *pic = NULL;
566 msi_control *control;
567 OLE_HANDLE hBitmap = 0;
571 control = msi_dialog_add_control( dialog, rec, szStatic,
572 SS_BITMAP | SS_LEFT | SS_CENTERIMAGE );
573 text = MSI_RecordGetString( rec, 10 );
574 r = msi_load_bitmap( dialog->package->db, text, &pic );
575 if( r == ERROR_SUCCESS )
577 r = IPicture_get_Handle( pic, &hBitmap );
579 SendMessageW( control->hwnd, STM_SETIMAGE, IMAGE_BITMAP, hBitmap );
583 return ERROR_SUCCESS;
586 static UINT msi_dialog_combo_control( msi_dialog *dialog, MSIRECORD *rec )
588 static const WCHAR szCombo[] = { 'C','O','M','B','O','B','O','X',0 };
590 msi_dialog_add_control( dialog, rec, szCombo,
591 SS_BITMAP | SS_LEFT | SS_CENTERIMAGE );
592 return ERROR_SUCCESS;
595 static UINT msi_dialog_edit_control( msi_dialog *dialog, MSIRECORD *rec )
597 const static WCHAR szEdit[] = { 'E','D','I','T',0 };
598 msi_control *control;
602 control = msi_dialog_add_control( dialog, rec, szEdit, WS_BORDER );
603 control->handler = msi_dialog_edit_handler;
604 prop = MSI_RecordGetString( rec, 9 );
606 control->property = strdupW( prop );
607 val = load_dynamic_property( dialog->package, control->property, NULL );
608 SetWindowTextW( control->hwnd, val );
609 HeapFree( GetProcessHeap(), 0, val );
610 return ERROR_SUCCESS;
613 static UINT msi_dialog_pathedit_control( msi_dialog *dialog, MSIRECORD *rec )
615 FIXME("not implemented properly\n");
616 return msi_dialog_edit_control( dialog, rec );
619 /* radio buttons are a bit different from normal controls */
620 static UINT msi_dialog_create_radiobutton( MSIRECORD *rec, LPVOID param )
622 radio_button_group_descr *group = (radio_button_group_descr *)param;
623 msi_dialog *dialog = group->dialog;
624 msi_control *control;
625 LPCWSTR prop, text, name;
627 DWORD attributes = group->attributes;
629 style = WS_CHILD | BS_AUTORADIOBUTTON | BS_MULTILINE;
630 name = MSI_RecordGetString( rec, 3 );
631 text = MSI_RecordGetString( rec, 8 );
634 if( ~attributes & 2 )
635 style |= WS_DISABLED;
637 control = msi_dialog_create_window( dialog, rec, szButton, name, text,
638 style, group->parent->hwnd );
639 control->handler = msi_dialog_radiogroup_handler;
641 prop = MSI_RecordGetString( rec, 1 );
643 control->property = strdupW( prop );
645 return ERROR_SUCCESS;
648 static UINT msi_dialog_radiogroup_control( msi_dialog *dialog, MSIRECORD *rec )
650 static const WCHAR query[] = {
651 'S','E','L','E','C','T',' ','*',' ',
652 'F','R','O','M',' ','R','a','d','i','o','B','u','t','t','o','n',' ',
653 'W','H','E','R','E',' ',
654 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
657 msi_control *control;
658 MSIQUERY *view = NULL;
659 radio_button_group_descr group;
660 MSIPACKAGE *package = dialog->package;
663 prop = MSI_RecordGetString( rec, 9 );
665 TRACE("%p %p %s\n", dialog, rec, debugstr_w( prop ));
667 /* Create parent group box to hold radio buttons */
668 control = msi_dialog_add_control( dialog, rec, szButton, BS_OWNERDRAW|WS_GROUP );
670 return ERROR_FUNCTION_FAILED;
672 oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
673 (LONG_PTR)MSIRadioGroup_WndProc );
674 SetPropW(control->hwnd, szButtonData, oldproc);
677 control->property = strdupW( prop );
679 /* query the Radio Button table for all control in this group */
680 r = MSI_OpenQuery( package->db, &view, query, prop );
681 if( r != ERROR_SUCCESS )
683 ERR("query failed for dialog %s radio group %s\n",
684 debugstr_w(dialog->name), debugstr_w(prop));
685 return ERROR_INVALID_PARAMETER;
688 group.dialog = dialog;
689 group.parent = control;
690 group.attributes = MSI_RecordGetInteger( rec, 8 );
692 r = MSI_IterateRecords( view, 0, msi_dialog_create_radiobutton, &group );
693 msiobj_release( &view->hdr );
698 struct control_handler msi_dialog_handler[] =
700 { szText, msi_dialog_text_control },
701 { szPushButton, msi_dialog_button_control },
702 { szLine, msi_dialog_line_control },
703 { szBitmap, msi_dialog_bitmap_control },
704 { szCheckBox, msi_dialog_checkbox_control },
705 { szScrollableText, msi_dialog_scrolltext_control },
706 { szComboBox, msi_dialog_combo_control },
707 { szEdit, msi_dialog_edit_control },
708 { szMaskedEdit, msi_dialog_edit_control },
709 { szPathEdit, msi_dialog_pathedit_control },
710 { szRadioButtonGroup, msi_dialog_radiogroup_control },
713 #define NUM_CONTROL_TYPES (sizeof msi_dialog_handler/sizeof msi_dialog_handler[0])
715 static UINT msi_dialog_create_controls( MSIRECORD *rec, LPVOID param )
717 msi_dialog *dialog = param;
718 LPCWSTR control_type;
721 /* find and call the function that can create this type of control */
722 control_type = MSI_RecordGetString( rec, 3 );
723 for( i=0; i<NUM_CONTROL_TYPES; i++ )
724 if (!strcmpiW( msi_dialog_handler[i].control_type, control_type ))
726 if( i != NUM_CONTROL_TYPES )
727 msi_dialog_handler[i].func( dialog, rec );
729 ERR("no handler for element type %s\n", debugstr_w(control_type));
731 return ERROR_SUCCESS;
734 static UINT msi_dialog_fill_controls( msi_dialog *dialog )
736 static const WCHAR query[] = {
737 'S','E','L','E','C','T',' ','*',' ',
738 'F','R','O','M',' ','C','o','n','t','r','o','l',' ',
739 'W','H','E','R','E',' ',
740 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
742 MSIQUERY *view = NULL;
743 MSIPACKAGE *package = dialog->package;
745 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
747 /* query the Control table for all the elements of the control */
748 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
749 if( r != ERROR_SUCCESS )
751 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
752 return ERROR_INVALID_PARAMETER;
755 r = MSI_IterateRecords( view, 0, msi_dialog_create_controls, dialog );
756 msiobj_release( &view->hdr );
761 static UINT msi_dialog_set_control_condition( MSIRECORD *rec, LPVOID param )
763 static const WCHAR szHide[] = { 'H','i','d','e',0 };
764 static const WCHAR szShow[] = { 'S','h','o','w',0 };
765 static const WCHAR szDisable[] = { 'D','i','s','a','b','l','e',0 };
766 static const WCHAR szEnable[] = { 'E','n','a','b','l','e',0 };
767 msi_dialog *dialog = param;
768 msi_control *control;
769 LPCWSTR name, action, condition;
772 name = MSI_RecordGetString( rec, 2 );
773 action = MSI_RecordGetString( rec, 3 );
774 condition = MSI_RecordGetString( rec, 4 );
775 r = MSI_EvaluateConditionW( dialog->package, condition );
776 control = msi_dialog_find_control( dialog, name );
779 TRACE("%s control %s\n", debugstr_w(action), debugstr_w(name));
781 /* FIXME: case sensitive? */
782 if(!strcmpW(action, szHide))
783 ShowWindow(control->hwnd, SW_HIDE);
784 else if(!strcmpW(action, szShow))
785 ShowWindow(control->hwnd, SW_SHOW);
786 else if(!strcmpW(action, szDisable))
787 EnableWindow(control->hwnd, FALSE);
788 else if(!strcmpW(action, szEnable))
789 EnableWindow(control->hwnd, TRUE);
791 FIXME("Unhandled action %s\n", debugstr_w(action));
794 return ERROR_SUCCESS;
797 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog )
799 static const WCHAR query[] = {
800 'S','E','L','E','C','T',' ','*',' ',
802 'C','o','n','t','r','o','l','C','o','n','d','i','t','i','o','n',' ',
803 'W','H','E','R','E',' ',
804 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0
807 MSIQUERY *view = NULL;
808 MSIPACKAGE *package = dialog->package;
810 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
812 /* query the Control table for all the elements of the control */
813 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
814 if( r != ERROR_SUCCESS )
816 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
817 return ERROR_INVALID_PARAMETER;
820 r = MSI_IterateRecords( view, 0, msi_dialog_set_control_condition, dialog );
821 msiobj_release( &view->hdr );
826 /* figure out the height of 10 point MS Sans Serif */
827 static INT msi_dialog_get_sans_serif_height( HWND hwnd )
829 static const WCHAR szSansSerif[] = {
830 'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0 };
835 HFONT hFont, hOldFont;
841 memset( &lf, 0, sizeof lf );
842 lf.lfHeight = MulDiv(10, GetDeviceCaps(hdc, LOGPIXELSY), 72);
843 strcpyW( lf.lfFaceName, szSansSerif );
844 hFont = CreateFontIndirectW(&lf);
847 hOldFont = SelectObject( hdc, hFont );
848 r = GetTextMetricsW( hdc, &tm );
850 height = tm.tmHeight;
851 SelectObject( hdc, hOldFont );
852 DeleteObject( hFont );
854 ReleaseDC( hwnd, hdc );
859 /* fetch the associated record from the Dialog table */
860 static MSIRECORD *msi_get_dialog_record( msi_dialog *dialog )
862 static const WCHAR query[] = {
863 'S','E','L','E','C','T',' ','*',' ',
864 'F','R','O','M',' ','D','i','a','l','o','g',' ',
865 'W','H','E','R','E',' ',
866 '`','D','i','a','l','o','g','`',' ','=',' ','\'','%','s','\'',0};
867 MSIPACKAGE *package = dialog->package;
868 MSIRECORD *rec = NULL;
870 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
872 rec = MSI_QueryGetRecord( package->db, query, dialog->name );
874 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
879 static void msi_dialog_adjust_dialog_size( msi_dialog *dialog, LPSIZE sz )
884 /* turn the client size into the window rectangle */
887 rect.right = msi_dialog_scale_unit( dialog, sz->cx );
888 rect.bottom = msi_dialog_scale_unit( dialog, sz->cy );
889 style = GetWindowLongPtrW( dialog->hwnd, GWL_STYLE );
890 AdjustWindowRect( &rect, style, FALSE );
891 sz->cx = rect.right - rect.left;
892 sz->cy = rect.bottom - rect.top;
895 static LRESULT msi_dialog_oncreate( HWND hwnd, LPCREATESTRUCTW cs )
897 static const WCHAR df[] = {
898 'D','e','f','a','u','l','t','U','I','F','o','n','t',0 };
899 msi_dialog *dialog = (msi_dialog*) cs->lpCreateParams;
900 MSIRECORD *rec = NULL;
905 TRACE("%p %p\n", dialog, dialog->package);
908 SetWindowLongPtrW( hwnd, GWLP_USERDATA, (LONG_PTR) dialog );
910 rec = msi_get_dialog_record( dialog );
913 TRACE("No record found for dialog %s\n", debugstr_w(dialog->name));
917 dialog->scale = msi_dialog_get_sans_serif_height(dialog->hwnd);
919 size.cx = MSI_RecordGetInteger( rec, 4 );
920 size.cy = MSI_RecordGetInteger( rec, 5 );
921 msi_dialog_adjust_dialog_size( dialog, &size );
923 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
924 text = MSI_RecordGetString( rec, 7 );
926 dialog->default_font = load_dynamic_property( dialog->package, df, NULL );
928 deformat_string( dialog->package, text, &title );
929 SetWindowTextW( hwnd, title );
930 SetWindowPos( hwnd, 0, 0, 0, size.cx, size.cy,
931 SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW );
933 HeapFree( GetProcessHeap(), 0, title );
934 msiobj_release( &rec->hdr );
936 msi_dialog_build_font_list( dialog );
937 msi_dialog_fill_controls( dialog );
938 msi_dialog_evaluate_control_conditions( dialog );
943 static UINT msi_dialog_send_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
945 LPWSTR event_fmt = NULL, arg_fmt = NULL;
947 TRACE("Sending control event %s %s\n", debugstr_w(event), debugstr_w(arg));
949 deformat_string( dialog->package, event, &event_fmt );
950 deformat_string( dialog->package, arg, &arg_fmt );
952 dialog->event_handler( dialog->package, event_fmt, arg_fmt, dialog );
954 HeapFree( GetProcessHeap(), 0, event_fmt );
955 HeapFree( GetProcessHeap(), 0, arg_fmt );
957 return ERROR_SUCCESS;
960 static UINT msi_dialog_set_property( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
962 static const WCHAR szNullArg[] = { '{','}',0 };
963 LPWSTR p, prop, arg_fmt = NULL;
966 len = strlenW(event);
967 prop = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR));
968 strcpyW( prop, &event[1] );
969 p = strchrW( prop, ']' );
973 if( strcmpW( szNullArg, arg ) )
974 deformat_string( dialog->package, arg, &arg_fmt );
975 MSI_SetPropertyW( dialog->package, prop, arg_fmt );
978 ERR("Badly formatted property string - what happens?\n");
979 HeapFree( GetProcessHeap(), 0, prop );
980 return ERROR_SUCCESS;
983 static UINT msi_dialog_control_event( MSIRECORD *rec, LPVOID param )
985 msi_dialog *dialog = param;
986 LPCWSTR condition, event, arg;
989 condition = MSI_RecordGetString( rec, 5 );
990 r = MSI_EvaluateConditionW( dialog->package, condition );
993 event = MSI_RecordGetString( rec, 3 );
994 arg = MSI_RecordGetString( rec, 4 );
995 if( event[0] == '[' )
996 msi_dialog_set_property( dialog, event, arg );
998 msi_dialog_send_event( dialog, event, arg );
1001 return ERROR_SUCCESS;
1004 static UINT msi_dialog_button_handler( msi_dialog *dialog,
1005 msi_control *control, WPARAM param )
1007 static const WCHAR query[] = {
1008 'S','E','L','E','C','T',' ','*',' ',
1009 'F','R','O','M',' ','C','o','n','t','r','o','l','E','v','e','n','t',' ',
1010 'W','H','E','R','E',' ',
1011 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
1013 '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',' ',
1014 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','i','n','g','`',0
1016 MSIQUERY *view = NULL;
1019 if( HIWORD(param) != BN_CLICKED )
1020 return ERROR_SUCCESS;
1022 r = MSI_OpenQuery( dialog->package->db, &view, query,
1023 dialog->name, control->name );
1024 if( r != ERROR_SUCCESS )
1026 ERR("query failed\n");
1030 r = MSI_IterateRecords( view, 0, msi_dialog_control_event, dialog );
1031 msiobj_release( &view->hdr );
1036 static UINT msi_dialog_get_checkbox_state( msi_dialog *dialog,
1037 msi_control *control )
1039 WCHAR state[2] = { 0 };
1042 MSI_GetPropertyW( dialog->package, control->property, state, &sz );
1043 return state[0] ? 1 : 0;
1046 static void msi_dialog_set_checkbox_state( msi_dialog *dialog,
1047 msi_control *control, UINT state )
1049 static const WCHAR szState[] = { '1', 0 };
1052 /* if uncheck then the property is set to NULL */
1055 MSI_SetPropertyW( dialog->package, control->property, NULL );
1059 /* check for a custom state */
1060 if (control->value && control->value[0])
1061 val = control->value;
1065 MSI_SetPropertyW( dialog->package, control->property, val );
1068 static void msi_dialog_checkbox_sync_state( msi_dialog *dialog,
1069 msi_control *control )
1073 state = msi_dialog_get_checkbox_state( dialog, control );
1074 SendMessageW( control->hwnd, BM_SETCHECK,
1075 state ? BST_CHECKED : BST_UNCHECKED, 0 );
1078 static UINT msi_dialog_checkbox_handler( msi_dialog *dialog,
1079 msi_control *control, WPARAM param )
1083 if( HIWORD(param) != BN_CLICKED )
1084 return ERROR_SUCCESS;
1086 TRACE("clicked checkbox %s, set %s\n", debugstr_w(control->name),
1087 debugstr_w(control->property));
1089 state = msi_dialog_get_checkbox_state( dialog, control );
1090 state = state ? 0 : 1;
1091 msi_dialog_set_checkbox_state( dialog, control, state );
1092 msi_dialog_checkbox_sync_state( dialog, control );
1094 return msi_dialog_button_handler( dialog, control, param );
1097 static UINT msi_dialog_edit_handler( msi_dialog *dialog,
1098 msi_control *control, WPARAM param )
1103 if( HIWORD(param) != EN_CHANGE )
1104 return ERROR_SUCCESS;
1106 TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
1107 debugstr_w(control->property));
1110 buf = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
1113 r = GetWindowTextW( control->hwnd, buf, sz );
1117 buf = HeapReAlloc( GetProcessHeap(), 0, buf, sz*sizeof(WCHAR) );
1120 MSI_SetPropertyW( dialog->package, control->property, buf );
1122 HeapFree( GetProcessHeap(), 0, buf );
1124 return ERROR_SUCCESS;
1127 static UINT msi_dialog_radiogroup_handler( msi_dialog *dialog,
1128 msi_control *control, WPARAM param )
1130 if( HIWORD(param) != BN_CLICKED )
1131 return ERROR_SUCCESS;
1133 TRACE("clicked radio button %s, set %s\n", debugstr_w(control->name),
1134 debugstr_w(control->property));
1136 MSI_SetPropertyW( dialog->package, control->property, control->name );
1138 return msi_dialog_button_handler( dialog, control, param );
1141 static LRESULT msi_dialog_oncommand( msi_dialog *dialog, WPARAM param, HWND hwnd )
1143 msi_control *control;
1145 TRACE("%p %p %08x\n", dialog, hwnd, param);
1147 control = msi_dialog_find_control_by_hwnd( dialog, hwnd );
1150 if( control->handler )
1152 control->handler( dialog, control, param );
1153 msi_dialog_evaluate_control_conditions( dialog );
1157 ERR("button click from nowhere\n");
1161 static LRESULT WINAPI MSIDialog_WndProc( HWND hwnd, UINT msg,
1162 WPARAM wParam, LPARAM lParam )
1164 msi_dialog *dialog = (LPVOID) GetWindowLongPtrW( hwnd, GWLP_USERDATA );
1166 TRACE("0x%04x\n", msg);
1171 return msi_dialog_oncreate( hwnd, (LPCREATESTRUCTW)lParam );
1174 return msi_dialog_oncommand( dialog, wParam, (HWND)lParam );
1177 dialog->hwnd = NULL;
1180 return DefWindowProcW(hwnd, msg, wParam, lParam);
1183 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1185 WNDPROC oldproc = (WNDPROC) GetPropW(hWnd, szButtonData);
1187 TRACE("hWnd %p msg %04x wParam 0x%08x lParam 0x%08lx\n", hWnd, msg, wParam, lParam);
1189 if (msg == WM_COMMAND) /* Forward notifications to dialog */
1190 SendMessageW(GetParent(hWnd), msg, wParam, lParam);
1192 return CallWindowProcW(oldproc, hWnd, msg, wParam, lParam);
1195 static LRESULT WINAPI MSIHiddenWindowProc( HWND hwnd, UINT msg,
1196 WPARAM wParam, LPARAM lParam )
1198 msi_dialog *dialog = (msi_dialog*) lParam;
1200 TRACE("%d %p\n", msg, dialog);
1204 case WM_MSI_DIALOG_CREATE:
1205 return msi_dialog_run_message_loop( dialog );
1206 case WM_MSI_DIALOG_DESTROY:
1207 msi_dialog_destroy( dialog );
1210 return DefWindowProcW( hwnd, msg, wParam, lParam );
1213 /* functions that interface to other modules within MSI */
1215 msi_dialog *msi_dialog_create( MSIPACKAGE* package, LPCWSTR szDialogName,
1216 msi_dialog_event_handler event_handler )
1218 MSIRECORD *rec = NULL;
1221 TRACE("%p %s\n", package, debugstr_w(szDialogName));
1223 /* allocate the structure for the dialog to use */
1224 dialog = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
1225 sizeof *dialog + sizeof(WCHAR)*strlenW(szDialogName) );
1228 strcpyW( dialog->name, szDialogName );
1229 msiobj_addref( &package->hdr );
1230 dialog->package = package;
1231 dialog->event_handler = event_handler;
1232 dialog->finished = 0;
1234 /* verify that the dialog exists */
1235 rec = msi_get_dialog_record( dialog );
1238 HeapFree( GetProcessHeap(), 0, dialog );
1241 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
1242 msiobj_release( &rec->hdr );
1247 static void msi_process_pending_messages(void)
1251 while( PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ) )
1253 TranslateMessage( &msg );
1254 DispatchMessageW( &msg );
1258 void msi_dialog_end_dialog( msi_dialog *dialog )
1260 TRACE("%p\n", dialog);
1261 dialog->finished = 1;
1262 PostMessageW(dialog->hwnd, WM_NULL, 0, 0);
1265 void msi_dialog_check_messages( HANDLE handle )
1269 /* in threads other than the UI thread, block */
1270 if( uiThreadId != GetCurrentThreadId() )
1273 WaitForSingleObject( handle, INFINITE );
1277 /* there's two choices for the UI thread */
1280 msi_process_pending_messages();
1286 * block here until somebody creates a new dialog or
1287 * the handle we're waiting on becomes ready
1289 r = MsgWaitForMultipleObjects( 1, &handle, 0, INFINITE, QS_ALLINPUT );
1290 if( r == WAIT_OBJECT_0 )
1295 UINT msi_dialog_run_message_loop( msi_dialog *dialog )
1299 if( !(dialog->attributes & msidbDialogAttributesVisible) )
1300 return ERROR_SUCCESS;
1302 if( uiThreadId != GetCurrentThreadId() )
1303 return SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_CREATE, 0, (LPARAM) dialog );
1305 /* create the dialog window, don't show it yet */
1306 hwnd = CreateWindowW( szMsiDialogClass, dialog->name, WS_OVERLAPPEDWINDOW,
1307 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
1308 NULL, NULL, NULL, dialog );
1311 ERR("Failed to create dialog %s\n", debugstr_w( dialog->name ));
1312 return ERROR_FUNCTION_FAILED;
1315 ShowWindow( hwnd, SW_SHOW );
1316 UpdateWindow( hwnd );
1318 if( dialog->attributes & msidbDialogAttributesModal )
1320 while( !dialog->finished )
1322 MsgWaitForMultipleObjects( 0, NULL, 0, INFINITE, QS_ALLEVENTS );
1323 msi_process_pending_messages();
1327 return ERROR_IO_PENDING;
1329 return ERROR_SUCCESS;
1332 void msi_dialog_do_preview( msi_dialog *dialog )
1335 dialog->attributes |= msidbDialogAttributesVisible;
1336 dialog->attributes &= ~msidbDialogAttributesModal;
1337 msi_dialog_run_message_loop( dialog );
1340 void msi_dialog_destroy( msi_dialog *dialog )
1342 if( uiThreadId != GetCurrentThreadId() )
1344 SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_DESTROY, 0, (LPARAM) dialog );
1349 ShowWindow( dialog->hwnd, SW_HIDE );
1351 /* destroy the list of controls */
1352 while( dialog->control_list )
1354 msi_control *t = dialog->control_list;
1355 dialog->control_list = t->next;
1356 /* leave dialog->hwnd - destroying parent destroys child windows */
1357 HeapFree( GetProcessHeap(), 0, t->property );
1358 HeapFree( GetProcessHeap(), 0, t->value );
1360 IPicture_Release( t->pic );
1361 HeapFree( GetProcessHeap(), 0, t );
1364 /* destroy the list of fonts */
1365 while( dialog->font_list )
1367 msi_font *t = dialog->font_list;
1368 dialog->font_list = t->next;
1369 DeleteObject( t->hfont );
1370 HeapFree( GetProcessHeap(), 0, t );
1372 HeapFree( GetProcessHeap(), 0, dialog->default_font );
1375 DestroyWindow( dialog->hwnd );
1377 msiobj_release( &dialog->package->hdr );
1378 dialog->package = NULL;
1379 HeapFree( GetProcessHeap(), 0, dialog );
1382 BOOL msi_dialog_register_class( void )
1386 ZeroMemory( &cls, sizeof cls );
1387 cls.lpfnWndProc = MSIDialog_WndProc;
1388 cls.hInstance = NULL;
1389 cls.hIcon = LoadIconW(0, (LPWSTR)IDI_APPLICATION);
1390 cls.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
1391 cls.hbrBackground = (HBRUSH)(COLOR_WINDOW);
1392 cls.lpszMenuName = NULL;
1393 cls.lpszClassName = szMsiDialogClass;
1395 if( !RegisterClassW( &cls ) )
1398 cls.lpfnWndProc = MSIHiddenWindowProc;
1399 cls.lpszClassName = szMsiHiddenWindow;
1401 if( !RegisterClassW( &cls ) )
1404 uiThreadId = GetCurrentThreadId();
1406 hMsiHiddenWindow = CreateWindowW( szMsiHiddenWindow, NULL, WS_OVERLAPPED,
1407 0, 0, 100, 100, NULL, NULL, NULL, NULL );
1408 if( !hMsiHiddenWindow )
1411 hRichedit = LoadLibraryA("riched20");
1416 void msi_dialog_unregister_class( void )
1418 DestroyWindow( hMsiHiddenWindow );
1419 UnregisterClassW( szMsiDialogClass, NULL );
1421 FreeLibrary( hRichedit );