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
63 typedef struct msi_font_tag
65 struct msi_font_tag *next;
73 msi_dialog_event_handler event_handler;
85 typedef UINT (*msi_dialog_control_func)( msi_dialog *dialog, MSIRECORD *rec );
86 struct control_handler
89 msi_dialog_control_func func;
97 } radio_button_group_descr;
99 const WCHAR szMsiDialogClass[] = {
100 'M','s','i','D','i','a','l','o','g','C','l','o','s','e','C','l','a','s','s',0
102 const WCHAR szMsiHiddenWindow[] = {
103 'M','s','i','H','i','d','d','e','n','W','i','n','d','o','w',0 };
104 const static WCHAR szStatic[] = { 'S','t','a','t','i','c',0 };
105 const static WCHAR szButton[] = { 'B','U','T','T','O','N', 0 };
106 const static WCHAR szButtonData[] = { 'M','S','I','D','A','T','A',0 };
107 static const WCHAR szText[] = { 'T','e','x','t',0 };
108 static const WCHAR szPushButton[] = { 'P','u','s','h','B','u','t','t','o','n',0 };
109 static const WCHAR szLine[] = { 'L','i','n','e',0 };
110 static const WCHAR szBitmap[] = { 'B','i','t','m','a','p',0 };
111 static const WCHAR szCheckBox[] = { 'C','h','e','c','k','B','o','x',0 };
112 static const WCHAR szScrollableText[] = {
113 'S','c','r','o','l','l','a','b','l','e','T','e','x','t',0 };
114 static const WCHAR szComboBox[] = { 'C','o','m','b','o','B','o','x',0 };
115 static const WCHAR szEdit[] = { 'E','d','i','t',0 };
116 static const WCHAR szMaskedEdit[] = { 'M','a','s','k','e','d','E','d','i','t',0 };
117 static const WCHAR szPathEdit[] = { 'P','a','t','h','E','d','i','t',0 };
118 static const WCHAR szRadioButtonGroup[] = {
119 'R','a','d','i','o','B','u','t','t','o','n','G','r','o','u','p',0 };
120 static const WCHAR szIcon[] = { 'I','c','o','n',0 };
122 static UINT msi_dialog_checkbox_handler( msi_dialog *, msi_control *, WPARAM );
123 static void msi_dialog_checkbox_sync_state( msi_dialog *, msi_control * );
124 static UINT msi_dialog_button_handler( msi_dialog *, msi_control *, WPARAM );
125 static UINT msi_dialog_edit_handler( msi_dialog *, msi_control *, WPARAM );
126 static UINT msi_dialog_radiogroup_handler( msi_dialog *, msi_control *, WPARAM param );
127 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog );
128 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
131 /* dialog sequencing */
133 #define WM_MSI_DIALOG_CREATE (WM_USER+0x100)
134 #define WM_MSI_DIALOG_DESTROY (WM_USER+0x101)
136 static DWORD uiThreadId;
137 static HWND hMsiHiddenWindow;
139 static INT msi_dialog_scale_unit( msi_dialog *dialog, INT val )
141 return (dialog->scale * val + 5) / 10;
144 static msi_control *msi_dialog_find_control( msi_dialog *dialog, LPCWSTR name )
146 msi_control *control;
150 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
151 if( !strcmpW( control->name, name ) ) /* FIXME: case sensitive? */
156 static msi_control *msi_dialog_find_control_by_hwnd( msi_dialog *dialog, HWND hwnd )
158 msi_control *control;
160 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
161 if( hwnd == control->hwnd )
166 static LPWSTR msi_get_deformatted_field( MSIPACKAGE *package, MSIRECORD *rec, int field )
168 LPCWSTR str = MSI_RecordGetString( rec, field );
172 deformat_string( package, str, &ret );
177 * msi_dialog_get_style
179 * Extract the {\style} string from the front of the text to display and
180 * update the pointer.
182 static LPWSTR msi_dialog_get_style( LPWSTR *text )
192 q = strchrW( p, '}' );
200 ret = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
203 memcpy( ret, p, len*sizeof(WCHAR) );
208 static UINT msi_dialog_add_font( MSIRECORD *rec, LPVOID param )
210 msi_dialog *dialog = param;
217 /* create a font and add it to the list */
218 name = MSI_RecordGetString( rec, 1 );
219 font = HeapAlloc( GetProcessHeap(), 0,
220 sizeof *font + strlenW( name )*sizeof (WCHAR) );
221 strcpyW( font->name, name );
222 font->next = dialog->font_list;
223 dialog->font_list = font;
225 memset( &lf, 0, sizeof lf );
226 face = MSI_RecordGetString( rec, 2 );
227 lf.lfHeight = MSI_RecordGetInteger( rec, 3 );
228 style = MSI_RecordGetInteger( rec, 5 );
229 if( style & msidbTextStyleStyleBitsBold )
230 lf.lfWeight = FW_BOLD;
231 if( style & msidbTextStyleStyleBitsItalic )
233 if( style & msidbTextStyleStyleBitsUnderline )
234 lf.lfUnderline = TRUE;
235 if( style & msidbTextStyleStyleBitsStrike )
236 lf.lfStrikeOut = TRUE;
237 lstrcpynW( lf.lfFaceName, face, LF_FACESIZE );
239 /* adjust the height */
240 hdc = GetDC( dialog->hwnd );
243 lf.lfHeight = -MulDiv(lf.lfHeight, GetDeviceCaps(hdc, LOGPIXELSY), 72);
244 ReleaseDC( dialog->hwnd, hdc );
247 font->hfont = CreateFontIndirectW( &lf );
249 TRACE("Adding font style %s\n", debugstr_w(font->name) );
251 return ERROR_SUCCESS;
254 static msi_font *msi_dialog_find_font( msi_dialog *dialog, LPCWSTR name )
258 for( font = dialog->font_list; font; font = font->next )
259 if( !strcmpW( font->name, name ) ) /* FIXME: case sensitive? */
265 static UINT msi_dialog_set_font( msi_dialog *dialog, HWND hwnd, LPCWSTR name )
269 font = msi_dialog_find_font( dialog, name );
271 SendMessageW( hwnd, WM_SETFONT, (WPARAM) font->hfont, TRUE );
273 ERR("No font entry for %s\n", debugstr_w(name));
274 return ERROR_SUCCESS;
277 static UINT msi_dialog_build_font_list( msi_dialog *dialog )
279 static const WCHAR query[] = {
280 'S','E','L','E','C','T',' ','*',' ',
281 'F','R','O','M',' ','`','T','e','x','t','S','t','y','l','e','`',' ',0
284 MSIQUERY *view = NULL;
286 TRACE("dialog %p\n", dialog );
288 r = MSI_OpenQuery( dialog->package->db, &view, query );
289 if( r != ERROR_SUCCESS )
292 r = MSI_IterateRecords( view, NULL, msi_dialog_add_font, dialog );
293 msiobj_release( &view->hdr );
298 static msi_control *msi_dialog_create_window( msi_dialog *dialog,
299 MSIRECORD *rec, LPCWSTR szCls, LPCWSTR name, LPCWSTR text,
300 DWORD style, HWND parent )
302 DWORD x, y, width, height;
303 LPWSTR font = NULL, title = NULL;
304 msi_control *control;
308 control = HeapAlloc( GetProcessHeap(), 0,
309 sizeof *control + strlenW(name)*sizeof(WCHAR) );
310 strcpyW( control->name, name );
311 list_add_head( &dialog->controls, &control->entry );
312 control->handler = NULL;
313 control->property = NULL;
314 control->value = NULL;
315 control->hBitmap = NULL;
316 control->hIcon = NULL;
317 control->hDll = NULL;
318 control->tabnext = strdupW( MSI_RecordGetString( rec, 11) );
320 x = MSI_RecordGetInteger( rec, 4 );
321 y = MSI_RecordGetInteger( rec, 5 );
322 width = MSI_RecordGetInteger( rec, 6 );
323 height = MSI_RecordGetInteger( rec, 7 );
325 x = msi_dialog_scale_unit( dialog, x );
326 y = msi_dialog_scale_unit( dialog, y );
327 width = msi_dialog_scale_unit( dialog, width );
328 height = msi_dialog_scale_unit( dialog, height );
332 deformat_string( dialog->package, text, &title );
333 font = msi_dialog_get_style( &title );
336 control->hwnd = CreateWindowW( szCls, title, style,
337 x, y, width, height, parent, NULL, NULL, NULL );
339 TRACE("Dialog %s control %s hwnd %p\n",
340 debugstr_w(dialog->name), debugstr_w(text), control->hwnd );
342 msi_dialog_set_font( dialog, control->hwnd,
343 font ? font : dialog->default_font );
345 HeapFree( GetProcessHeap(), 0, font );
346 HeapFree( GetProcessHeap(), 0, title );
351 static MSIRECORD *msi_get_binary_record( MSIDATABASE *db, LPCWSTR name )
353 const static WCHAR query[] = {
354 's','e','l','e','c','t',' ','*',' ',
355 'f','r','o','m',' ','B','i','n','a','r','y',' ',
356 'w','h','e','r','e',' ',
357 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0
360 return MSI_QueryGetRecord( db, query, name );
363 static LPWSTR msi_create_tmp_path(void)
367 static const WCHAR prefix[] = { 'm','s','i',0 };
370 r = GetTempPathW( MAX_PATH, tmp );
373 len = lstrlenW( tmp ) + 20;
374 path = HeapAlloc( GetProcessHeap(), 0, len * sizeof (WCHAR) );
377 r = GetTempFileNameW( tmp, prefix, 0, path );
380 HeapFree( GetProcessHeap(), 0, path );
388 static HANDLE msi_load_image( MSIDATABASE *db, LPCWSTR name, UINT type,
389 UINT cx, UINT cy, UINT flags )
391 MSIRECORD *rec = NULL;
392 HANDLE himage = NULL;
396 TRACE("%p %s %u %u %08x\n", db, debugstr_w(name), cx, cy, flags);
398 tmp = msi_create_tmp_path();
402 rec = msi_get_binary_record( db, name );
405 r = MSI_RecordStreamToFile( rec, 2, tmp );
406 if( r == ERROR_SUCCESS )
408 himage = LoadImageW( 0, tmp, type, cx, cy, flags );
411 msiobj_release( &rec->hdr );
414 HeapFree( GetProcessHeap(), 0, tmp );
418 static HICON msi_load_icon( MSIDATABASE *db, LPCWSTR text, UINT attributes )
420 DWORD cx = 0, cy = 0, flags;
422 flags = LR_LOADFROMFILE | LR_DEFAULTSIZE;
423 if( attributes & msidbControlAttributesFixedSize )
425 flags &= ~LR_DEFAULTSIZE;
426 if( attributes & msidbControlAttributesIconSize16 )
431 if( attributes & msidbControlAttributesIconSize32 )
436 /* msidbControlAttributesIconSize48 handled by above logic */
438 return msi_load_image( db, text, IMAGE_ICON, cx, cy, flags );
442 /* called from the Control Event subscription code */
443 void msi_dialog_handle_event( msi_dialog* dialog, LPCWSTR control,
444 LPCWSTR attribute, MSIRECORD *rec )
449 ctrl = msi_dialog_find_control( dialog, control );
452 if( lstrcmpW(attribute, szText) )
454 text = MSI_RecordGetString( rec , 1 );
455 SetWindowTextW( ctrl->hwnd, text );
456 msi_dialog_check_messages( NULL );
459 static void msi_dialog_map_events(msi_dialog* dialog, LPCWSTR control)
461 static WCHAR Query[] = {
462 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
463 '`','E','v','e','n','t','M','a','p','p','i','n','g','`',' ',
464 'W','H','E','R','E',' ',
465 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
467 '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',0
470 LPCWSTR event, attribute;
472 row = MSI_QueryGetRecord( dialog->package->db, Query, dialog->name, control );
476 event = MSI_RecordGetString( row, 3 );
477 attribute = MSI_RecordGetString( row, 4 );
478 ControlEvent_SubscribeToEvent( dialog->package, event, control, attribute );
479 msiobj_release( &row->hdr );
482 /* everything except radio buttons */
483 static msi_control *msi_dialog_add_control( msi_dialog *dialog,
484 MSIRECORD *rec, LPCWSTR szCls, DWORD style )
489 name = MSI_RecordGetString( rec, 2 );
490 attributes = MSI_RecordGetInteger( rec, 8 );
491 text = MSI_RecordGetString( rec, 10 );
492 if( attributes & msidbControlAttributesVisible )
494 if( ~attributes & msidbControlAttributesEnabled )
495 style |= WS_DISABLED;
497 msi_dialog_map_events(dialog, name);
499 return msi_dialog_create_window( dialog, rec, szCls, name, text,
500 style, dialog->hwnd );
510 * we don't erase our own background,
511 * so we have to make sure that the parent window redraws first
513 static void msi_text_on_settext( HWND hWnd )
518 hParent = GetParent( hWnd );
519 GetWindowRect( hWnd, &rc );
520 MapWindowPoints( NULL, hParent, (LPPOINT) &rc, 2 );
521 InvalidateRect( hParent, &rc, TRUE );
524 static LRESULT WINAPI
525 MSIText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
527 struct msi_text_info *info;
530 TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
532 info = GetPropW(hWnd, szButtonData);
534 if( msg == WM_CTLCOLORSTATIC &&
535 ( info->attributes & msidbControlAttributesTransparent ) )
537 SetBkMode( (HDC)wParam, TRANSPARENT );
538 return (LRESULT) GetStockObject(NULL_BRUSH);
541 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
546 msi_text_on_settext( hWnd );
549 HeapFree( GetProcessHeap(), 0, info );
550 RemovePropW( hWnd, szButtonData );
557 static UINT msi_dialog_text_control( msi_dialog *dialog, MSIRECORD *rec )
559 msi_control *control;
560 struct msi_text_info *info;
562 TRACE("%p %p\n", dialog, rec);
564 control = msi_dialog_add_control( dialog, rec, szStatic, SS_LEFT | WS_GROUP );
566 return ERROR_FUNCTION_FAILED;
568 info = HeapAlloc( GetProcessHeap(), 0, sizeof *info );
570 return ERROR_SUCCESS;
572 info->attributes = MSI_RecordGetInteger( rec, 8 );
573 if( info->attributes & msidbControlAttributesTransparent )
574 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT );
576 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
577 (LONG_PTR)MSIText_WndProc );
578 SetPropW( control->hwnd, szButtonData, info );
580 return ERROR_SUCCESS;
583 static UINT msi_dialog_button_control( msi_dialog *dialog, MSIRECORD *rec )
585 msi_control *control;
586 UINT attributes, style;
589 TRACE("%p %p\n", dialog, rec);
592 attributes = MSI_RecordGetInteger( rec, 8 );
593 if( attributes & msidbControlAttributesIcon )
596 control = msi_dialog_add_control( dialog, rec, szButton, style );
598 return ERROR_FUNCTION_FAILED;
600 control->handler = msi_dialog_button_handler;
603 text = msi_get_deformatted_field( dialog->package, rec, 10 );
604 control->hIcon = msi_load_icon( dialog->package->db, text, attributes );
605 if( attributes & msidbControlAttributesIcon )
606 SendMessageW( control->hwnd, BM_SETIMAGE, IMAGE_ICON, (LPARAM) control->hIcon );
607 HeapFree( GetProcessHeap(), 0, text );
609 return ERROR_SUCCESS;
612 static LPWSTR msi_get_checkbox_value( msi_dialog *dialog, LPCWSTR prop )
614 const static WCHAR query[] = {
615 'S','E','L','E','C','T',' ','*',' ',
616 'F','R','O','M',' ','`','C','h','e','c','k','B','o','x',' ','`',
617 'W','H','E','R','E',' ',
618 '`','P','r','o','p','e','r','t','y','`',' ','=',' ',
621 MSIRECORD *rec = NULL;
625 /* find if there is a value associated with the checkbox */
626 rec = MSI_QueryGetRecord( dialog->package->db, query, prop );
630 val = MSI_RecordGetString( rec, 2 );
633 deformat_string( dialog->package, val, &ret );
636 HeapFree( GetProcessHeap(), 0, ret );
640 msiobj_release( &rec->hdr );
644 ret = msi_dup_property( dialog->package, prop );
647 HeapFree( GetProcessHeap(), 0, ret );
654 static UINT msi_dialog_checkbox_control( msi_dialog *dialog, MSIRECORD *rec )
656 msi_control *control;
659 TRACE("%p %p\n", dialog, rec);
661 control = msi_dialog_add_control( dialog, rec, szButton,
662 BS_CHECKBOX | BS_MULTILINE | WS_TABSTOP );
663 control->handler = msi_dialog_checkbox_handler;
664 prop = MSI_RecordGetString( rec, 9 );
667 control->property = strdupW( prop );
668 control->value = msi_get_checkbox_value( dialog, prop );
669 TRACE("control %s value %s\n", debugstr_w(control->property),
670 debugstr_w(control->value));
672 msi_dialog_checkbox_sync_state( dialog, control );
674 return ERROR_SUCCESS;
677 static UINT msi_dialog_line_control( msi_dialog *dialog, MSIRECORD *rec )
679 TRACE("%p %p\n", dialog, rec);
681 msi_dialog_add_control( dialog, rec, szStatic, SS_ETCHEDHORZ | SS_SUNKEN );
682 return ERROR_SUCCESS;
685 struct msi_streamin_info
692 static DWORD CALLBACK
693 msi_richedit_stream_in( DWORD_PTR arg, LPBYTE buffer, LONG count, LONG *pcb )
695 struct msi_streamin_info *info = (struct msi_streamin_info*) arg;
697 if( (count + info->offset) > info->length )
698 count = info->length - info->offset;
699 memcpy( buffer, &info->string[ info->offset ], count );
701 info->offset += count;
703 TRACE("%ld/%ld\n", info->offset, info->length);
708 static UINT msi_dialog_scrolltext_control( msi_dialog *dialog, MSIRECORD *rec )
710 const static WCHAR szRichEdit20W[] = {
711 'R','i','c','h','E','d','i','t','2','0','W',0
713 struct msi_streamin_info info;
714 msi_control *control;
720 hRichedit = LoadLibraryA("riched20");
722 style = WS_BORDER | ES_MULTILINE | WS_VSCROLL |
723 ES_READONLY | ES_AUTOVSCROLL | WS_TABSTOP;
724 control = msi_dialog_add_control( dialog, rec, szRichEdit20W, style );
726 return ERROR_FUNCTION_FAILED;
728 control->hDll = hRichedit;
730 text = MSI_RecordGetString( rec, 10 );
731 info.string = strdupWtoA( text );
733 info.length = lstrlenA( info.string ) + 1;
735 es.dwCookie = (DWORD_PTR) &info;
737 es.pfnCallback = msi_richedit_stream_in;
739 SendMessageW( control->hwnd, EM_STREAMIN, SF_RTF, (LPARAM) &es );
741 HeapFree( GetProcessHeap(), 0, info.string );
743 return ERROR_SUCCESS;
746 static UINT msi_dialog_bitmap_control( msi_dialog *dialog, MSIRECORD *rec )
748 UINT cx, cy, flags, style, attributes;
749 msi_control *control;
752 flags = LR_LOADFROMFILE;
753 style = SS_BITMAP | SS_LEFT | WS_GROUP;
755 attributes = MSI_RecordGetInteger( rec, 8 );
756 if( attributes & msidbControlAttributesFixedSize )
758 flags |= LR_DEFAULTSIZE;
759 style |= SS_CENTERIMAGE;
762 control = msi_dialog_add_control( dialog, rec, szStatic, style );
763 cx = MSI_RecordGetInteger( rec, 6 );
764 cy = MSI_RecordGetInteger( rec, 7 );
765 cx = msi_dialog_scale_unit( dialog, cx );
766 cy = msi_dialog_scale_unit( dialog, cy );
768 text = msi_get_deformatted_field( dialog->package, rec, 10 );
769 control->hBitmap = msi_load_image( dialog->package->db, text,
770 IMAGE_BITMAP, cx, cy, flags );
771 if( control->hBitmap )
772 SendMessageW( control->hwnd, STM_SETIMAGE,
773 IMAGE_BITMAP, (LPARAM) control->hBitmap );
775 ERR("Failed to load bitmap %s\n", debugstr_w(text));
777 HeapFree( GetProcessHeap(), 0, text );
779 return ERROR_SUCCESS;
782 static UINT msi_dialog_icon_control( msi_dialog *dialog, MSIRECORD *rec )
784 msi_control *control;
790 control = msi_dialog_add_control( dialog, rec, szStatic,
791 SS_ICON | SS_CENTERIMAGE | WS_GROUP );
793 attributes = MSI_RecordGetInteger( rec, 8 );
794 text = msi_get_deformatted_field( dialog->package, rec, 10 );
795 control->hIcon = msi_load_icon( dialog->package->db, text, attributes );
797 SendMessageW( control->hwnd, STM_SETICON, (WPARAM) control->hIcon, 0 );
799 ERR("Failed to load bitmap %s\n", debugstr_w(text));
800 HeapFree( GetProcessHeap(), 0, text );
801 return ERROR_SUCCESS;
804 static UINT msi_dialog_combo_control( msi_dialog *dialog, MSIRECORD *rec )
806 static const WCHAR szCombo[] = { 'C','O','M','B','O','B','O','X',0 };
808 msi_dialog_add_control( dialog, rec, szCombo,
809 SS_BITMAP | SS_LEFT | SS_CENTERIMAGE );
810 return ERROR_SUCCESS;
813 static UINT msi_dialog_edit_control( msi_dialog *dialog, MSIRECORD *rec )
815 msi_control *control;
819 control = msi_dialog_add_control( dialog, rec, szEdit,
820 WS_BORDER | WS_TABSTOP );
821 control->handler = msi_dialog_edit_handler;
822 prop = MSI_RecordGetString( rec, 9 );
824 control->property = strdupW( prop );
825 val = msi_dup_property( dialog->package, control->property );
826 SetWindowTextW( control->hwnd, val );
827 HeapFree( GetProcessHeap(), 0, val );
828 return ERROR_SUCCESS;
831 /******************** Masked Edit ********************************************/
833 #define MASK_MAX_GROUPS 10
835 struct msi_mask_group
843 struct msi_maskedit_info
851 struct msi_mask_group group[MASK_MAX_GROUPS];
854 static void msi_mask_control_change( struct msi_maskedit_info *info )
859 val = HeapAlloc( GetProcessHeap(), 0, (info->num_chars+1)*sizeof(WCHAR) );
860 for( i=0, n=0; i<info->num_groups; i++ )
862 if( (info->group[i].len + n) > info->num_chars )
864 ERR("can't fit control %d text into template\n",i);
867 r = GetWindowTextW( info->group[i].hwnd, &val[n], info->group[i].len+1 );
868 if( r != info->group[i].len )
873 TRACE("%d/%d controls were good\n", i, info->num_groups);
875 if( i == info->num_groups )
877 TRACE("Set property %s to %s\n",
878 debugstr_w(info->prop), debugstr_w(val) );
879 CharUpperBuffW( val, info->num_chars );
880 MSI_SetPropertyW( info->dialog->package, info->prop, val );
881 msi_dialog_evaluate_control_conditions( info->dialog );
883 HeapFree( GetProcessHeap(), 0, val );
886 /* now move to the next control if necessary */
887 static VOID msi_mask_next_control( struct msi_maskedit_info *info, HWND hWnd )
892 for( i=0; i<info->num_groups; i++ )
893 if( info->group[i].hwnd == hWnd )
896 /* don't move from the last control */
897 if( i >= (info->num_groups-1) )
900 len = SendMessageW( hWnd, WM_GETTEXTLENGTH, 0, 0 );
901 if( len < info->group[i].len )
904 hWndNext = GetNextDlgTabItem( GetParent( hWnd ), hWnd, FALSE );
905 SetFocus( hWndNext );
908 static LRESULT WINAPI
909 MSIMaskedEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
911 struct msi_maskedit_info *info;
914 TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
916 info = GetPropW(hWnd, szButtonData);
918 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
923 if (HIWORD(wParam) == EN_CHANGE)
925 msi_mask_control_change( info );
926 msi_mask_next_control( info, (HWND) lParam );
930 HeapFree( GetProcessHeap(), 0, info->prop );
931 HeapFree( GetProcessHeap(), 0, info );
932 RemovePropW( hWnd, szButtonData );
939 /* fish the various bits of the property out and put them in the control */
941 msi_maskedit_set_text( struct msi_maskedit_info *info, LPCWSTR text )
947 for( i = 0; i < info->num_groups; i++ )
949 if( info->group[i].len < lstrlenW( p ) )
951 LPWSTR chunk = strdupW( p );
952 chunk[ info->group[i].len ] = 0;
953 SetWindowTextW( info->group[i].hwnd, chunk );
954 HeapFree( GetProcessHeap(), 0, chunk );
958 SetWindowTextW( info->group[i].hwnd, p );
961 p += info->group[i].len;
965 static struct msi_maskedit_info * msi_dialog_parse_groups( LPCWSTR mask )
967 struct msi_maskedit_info * info = NULL;
968 int i = 0, n = 0, total = 0;
971 TRACE("masked control, template %s\n", debugstr_w(mask));
976 p = strchrW(mask, '<');
980 info = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof *info );
985 for( i=0; i<MASK_MAX_GROUPS; i++ )
993 /* stop at the end of the string */
994 if( p[0] == 0 || p[0] == '>' )
997 /* count the number of the same identifier */
998 for( n=0; p[n] == p[0]; n++ )
1000 info->group[i].ofs = total;
1001 info->group[i].type = p[0];
1005 total++; /* an extra not part of the group */
1007 info->group[i].len = n;
1012 TRACE("%d characters in %d groups\n", total, i );
1013 if( i == MASK_MAX_GROUPS )
1014 ERR("too many groups in PIDTemplate %s\n", debugstr_w(mask));
1016 info->num_chars = total;
1017 info->num_groups = i;
1023 msi_maskedit_create_children( struct msi_maskedit_info *info, LPCWSTR font )
1025 DWORD width, height, style, wx, ww;
1030 style = WS_CHILD | WS_BORDER | WS_VISIBLE | WS_TABSTOP;
1032 GetClientRect( info->hwnd, &rect );
1034 width = rect.right - rect.left;
1035 height = rect.bottom - rect.top;
1037 for( i = 0; i < info->num_groups; i++ )
1039 wx = (info->group[i].ofs * width) / info->num_chars;
1040 ww = (info->group[i].len * width) / info->num_chars;
1042 hwnd = CreateWindowW( szEdit, NULL, style, wx, 0, ww, height,
1043 info->hwnd, NULL, NULL, NULL );
1046 ERR("failed to create mask edit sub window\n");
1050 SendMessageW( hwnd, EM_LIMITTEXT, info->group[i].len, 0 );
1052 msi_dialog_set_font( info->dialog, hwnd,
1053 font?font:info->dialog->default_font );
1054 info->group[i].hwnd = hwnd;
1058 /* office 2003 uses "73931<````=````=````=````=`````>@@@@@" */
1059 static UINT msi_dialog_maskedit_control( msi_dialog *dialog, MSIRECORD *rec )
1061 LPWSTR mask, title = NULL, val = NULL, font;
1062 struct msi_maskedit_info *info = NULL;
1063 UINT ret = ERROR_SUCCESS;
1064 msi_control *control;
1069 mask = msi_get_deformatted_field( dialog->package, rec, 10 );
1070 font = msi_dialog_get_style( &mask );
1073 ERR("mask template is empty\n");
1077 info = msi_dialog_parse_groups( mask );
1080 ERR("template %s is invalid\n", debugstr_w(mask));
1084 info->dialog = dialog;
1086 control = msi_dialog_add_control( dialog, rec, szStatic,
1087 SS_OWNERDRAW | WS_GROUP | WS_VISIBLE );
1090 ERR("Failed to create maskedit container\n");
1091 ret = ERROR_FUNCTION_FAILED;
1094 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
1096 info->hwnd = control->hwnd;
1098 /* subclass the static control */
1099 info->oldproc = (WNDPROC) SetWindowLongPtrW( info->hwnd, GWLP_WNDPROC,
1100 (LONG_PTR)MSIMaskedEdit_WndProc );
1101 SetPropW( control->hwnd, szButtonData, info );
1103 prop = MSI_RecordGetString( rec, 9 );
1105 info->prop = strdupW( prop );
1107 msi_maskedit_create_children( info, font );
1111 val = msi_dup_property( dialog->package, prop );
1114 msi_maskedit_set_text( info, val );
1115 HeapFree( GetProcessHeap(), 0, val );
1120 if( ret != ERROR_SUCCESS )
1121 HeapFree( GetProcessHeap(), 0, info );
1122 HeapFree( GetProcessHeap(), 0, title );
1123 HeapFree( GetProcessHeap(), 0, mask );
1127 /******************** Path Edit ********************************************/
1129 static UINT msi_dialog_pathedit_control( msi_dialog *dialog, MSIRECORD *rec )
1131 FIXME("not implemented properly\n");
1132 return msi_dialog_edit_control( dialog, rec );
1135 /* radio buttons are a bit different from normal controls */
1136 static UINT msi_dialog_create_radiobutton( MSIRECORD *rec, LPVOID param )
1138 radio_button_group_descr *group = (radio_button_group_descr *)param;
1139 msi_dialog *dialog = group->dialog;
1140 msi_control *control;
1141 LPCWSTR prop, text, name;
1142 DWORD style, attributes = group->attributes;
1144 style = WS_CHILD | BS_AUTORADIOBUTTON | BS_MULTILINE | WS_TABSTOP;
1145 name = MSI_RecordGetString( rec, 3 );
1146 text = MSI_RecordGetString( rec, 8 );
1147 if( attributes & 1 )
1148 style |= WS_VISIBLE;
1149 if( ~attributes & 2 )
1150 style |= WS_DISABLED;
1152 control = msi_dialog_create_window( dialog, rec, szButton, name, text,
1153 style, group->parent->hwnd );
1155 return ERROR_FUNCTION_FAILED;
1156 control->handler = msi_dialog_radiogroup_handler;
1158 prop = MSI_RecordGetString( rec, 1 );
1160 control->property = strdupW( prop );
1162 return ERROR_SUCCESS;
1165 static UINT msi_dialog_radiogroup_control( msi_dialog *dialog, MSIRECORD *rec )
1167 static const WCHAR query[] = {
1168 'S','E','L','E','C','T',' ','*',' ',
1169 'F','R','O','M',' ','R','a','d','i','o','B','u','t','t','o','n',' ',
1170 'W','H','E','R','E',' ',
1171 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
1174 msi_control *control;
1175 MSIQUERY *view = NULL;
1176 radio_button_group_descr group;
1177 MSIPACKAGE *package = dialog->package;
1180 prop = MSI_RecordGetString( rec, 9 );
1182 TRACE("%p %p %s\n", dialog, rec, debugstr_w( prop ));
1184 /* Create parent group box to hold radio buttons */
1185 control = msi_dialog_add_control( dialog, rec, szButton, BS_OWNERDRAW|WS_GROUP );
1187 return ERROR_FUNCTION_FAILED;
1189 oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1190 (LONG_PTR)MSIRadioGroup_WndProc );
1191 SetPropW(control->hwnd, szButtonData, oldproc);
1192 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
1195 control->property = strdupW( prop );
1197 /* query the Radio Button table for all control in this group */
1198 r = MSI_OpenQuery( package->db, &view, query, prop );
1199 if( r != ERROR_SUCCESS )
1201 ERR("query failed for dialog %s radio group %s\n",
1202 debugstr_w(dialog->name), debugstr_w(prop));
1203 return ERROR_INVALID_PARAMETER;
1206 group.dialog = dialog;
1207 group.parent = control;
1208 group.attributes = MSI_RecordGetInteger( rec, 8 );
1210 r = MSI_IterateRecords( view, 0, msi_dialog_create_radiobutton, &group );
1211 msiobj_release( &view->hdr );
1216 struct control_handler msi_dialog_handler[] =
1218 { szText, msi_dialog_text_control },
1219 { szPushButton, msi_dialog_button_control },
1220 { szLine, msi_dialog_line_control },
1221 { szBitmap, msi_dialog_bitmap_control },
1222 { szCheckBox, msi_dialog_checkbox_control },
1223 { szScrollableText, msi_dialog_scrolltext_control },
1224 { szComboBox, msi_dialog_combo_control },
1225 { szEdit, msi_dialog_edit_control },
1226 { szMaskedEdit, msi_dialog_maskedit_control },
1227 { szPathEdit, msi_dialog_pathedit_control },
1228 { szRadioButtonGroup, msi_dialog_radiogroup_control },
1229 { szIcon, msi_dialog_icon_control },
1232 #define NUM_CONTROL_TYPES (sizeof msi_dialog_handler/sizeof msi_dialog_handler[0])
1234 static UINT msi_dialog_create_controls( MSIRECORD *rec, LPVOID param )
1236 msi_dialog *dialog = param;
1237 LPCWSTR control_type;
1240 /* find and call the function that can create this type of control */
1241 control_type = MSI_RecordGetString( rec, 3 );
1242 for( i=0; i<NUM_CONTROL_TYPES; i++ )
1243 if (!strcmpiW( msi_dialog_handler[i].control_type, control_type ))
1245 if( i != NUM_CONTROL_TYPES )
1246 msi_dialog_handler[i].func( dialog, rec );
1248 ERR("no handler for element type %s\n", debugstr_w(control_type));
1250 return ERROR_SUCCESS;
1253 static UINT msi_dialog_fill_controls( msi_dialog *dialog )
1255 static const WCHAR query[] = {
1256 'S','E','L','E','C','T',' ','*',' ',
1257 'F','R','O','M',' ','C','o','n','t','r','o','l',' ',
1258 'W','H','E','R','E',' ',
1259 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
1261 MSIQUERY *view = NULL;
1262 MSIPACKAGE *package = dialog->package;
1264 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
1266 /* query the Control table for all the elements of the control */
1267 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
1268 if( r != ERROR_SUCCESS )
1270 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
1271 return ERROR_INVALID_PARAMETER;
1274 r = MSI_IterateRecords( view, 0, msi_dialog_create_controls, dialog );
1275 msiobj_release( &view->hdr );
1280 static UINT msi_dialog_set_control_condition( MSIRECORD *rec, LPVOID param )
1282 static const WCHAR szHide[] = { 'H','i','d','e',0 };
1283 static const WCHAR szShow[] = { 'S','h','o','w',0 };
1284 static const WCHAR szDisable[] = { 'D','i','s','a','b','l','e',0 };
1285 static const WCHAR szEnable[] = { 'E','n','a','b','l','e',0 };
1286 msi_dialog *dialog = param;
1287 msi_control *control;
1288 LPCWSTR name, action, condition;
1291 name = MSI_RecordGetString( rec, 2 );
1292 action = MSI_RecordGetString( rec, 3 );
1293 condition = MSI_RecordGetString( rec, 4 );
1294 r = MSI_EvaluateConditionW( dialog->package, condition );
1295 control = msi_dialog_find_control( dialog, name );
1298 TRACE("%s control %s\n", debugstr_w(action), debugstr_w(name));
1300 /* FIXME: case sensitive? */
1301 if(!lstrcmpW(action, szHide))
1302 ShowWindow(control->hwnd, SW_HIDE);
1303 else if(!strcmpW(action, szShow))
1304 ShowWindow(control->hwnd, SW_SHOW);
1305 else if(!strcmpW(action, szDisable))
1306 EnableWindow(control->hwnd, FALSE);
1307 else if(!strcmpW(action, szEnable))
1308 EnableWindow(control->hwnd, TRUE);
1310 FIXME("Unhandled action %s\n", debugstr_w(action));
1313 return ERROR_SUCCESS;
1316 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog )
1318 static const WCHAR query[] = {
1319 'S','E','L','E','C','T',' ','*',' ',
1320 'F','R','O','M',' ',
1321 'C','o','n','t','r','o','l','C','o','n','d','i','t','i','o','n',' ',
1322 'W','H','E','R','E',' ',
1323 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0
1326 MSIQUERY *view = NULL;
1327 MSIPACKAGE *package = dialog->package;
1329 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
1331 /* query the Control table for all the elements of the control */
1332 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
1333 if( r != ERROR_SUCCESS )
1335 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
1336 return ERROR_INVALID_PARAMETER;
1339 r = MSI_IterateRecords( view, 0, msi_dialog_set_control_condition, dialog );
1340 msiobj_release( &view->hdr );
1345 /* figure out the height of 10 point MS Sans Serif */
1346 static INT msi_dialog_get_sans_serif_height( HWND hwnd )
1348 static const WCHAR szSansSerif[] = {
1349 'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0 };
1354 HFONT hFont, hOldFont;
1357 hdc = GetDC( hwnd );
1360 memset( &lf, 0, sizeof lf );
1361 lf.lfHeight = MulDiv(10, GetDeviceCaps(hdc, LOGPIXELSY), 72);
1362 strcpyW( lf.lfFaceName, szSansSerif );
1363 hFont = CreateFontIndirectW(&lf);
1366 hOldFont = SelectObject( hdc, hFont );
1367 r = GetTextMetricsW( hdc, &tm );
1369 height = tm.tmHeight;
1370 SelectObject( hdc, hOldFont );
1371 DeleteObject( hFont );
1373 ReleaseDC( hwnd, hdc );
1378 /* fetch the associated record from the Dialog table */
1379 static MSIRECORD *msi_get_dialog_record( msi_dialog *dialog )
1381 static const WCHAR query[] = {
1382 'S','E','L','E','C','T',' ','*',' ',
1383 'F','R','O','M',' ','D','i','a','l','o','g',' ',
1384 'W','H','E','R','E',' ',
1385 '`','D','i','a','l','o','g','`',' ','=',' ','\'','%','s','\'',0};
1386 MSIPACKAGE *package = dialog->package;
1387 MSIRECORD *rec = NULL;
1389 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
1391 rec = MSI_QueryGetRecord( package->db, query, dialog->name );
1393 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
1398 static void msi_dialog_adjust_dialog_size( msi_dialog *dialog, LPSIZE sz )
1403 /* turn the client size into the window rectangle */
1406 rect.right = msi_dialog_scale_unit( dialog, sz->cx );
1407 rect.bottom = msi_dialog_scale_unit( dialog, sz->cy );
1408 style = GetWindowLongPtrW( dialog->hwnd, GWL_STYLE );
1409 AdjustWindowRect( &rect, style, FALSE );
1410 sz->cx = rect.right - rect.left;
1411 sz->cy = rect.bottom - rect.top;
1414 static BOOL msi_control_set_next( msi_control *control, msi_control *next )
1416 return SetWindowPos( next->hwnd, control->hwnd, 0, 0, 0, 0,
1417 SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOREDRAW |
1418 SWP_NOREPOSITION | SWP_NOSENDCHANGING | SWP_NOSIZE );
1421 static UINT msi_dialog_set_tab_order( msi_dialog *dialog )
1423 msi_control *control, *tab_next;
1425 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
1427 tab_next = msi_dialog_find_control( dialog, control->tabnext );
1430 msi_control_set_next( control, tab_next );
1433 return ERROR_SUCCESS;
1436 static void msi_dialog_set_first_control( msi_dialog* dialog, LPCWSTR name )
1438 msi_control *control;
1440 control = msi_dialog_find_control( dialog, name );
1442 dialog->hWndFocus = control->hwnd;
1444 dialog->hWndFocus = NULL;
1447 static LRESULT msi_dialog_oncreate( HWND hwnd, LPCREATESTRUCTW cs )
1449 static const WCHAR df[] = {
1450 'D','e','f','a','u','l','t','U','I','F','o','n','t',0 };
1451 msi_dialog *dialog = (msi_dialog*) cs->lpCreateParams;
1452 MSIRECORD *rec = NULL;
1453 LPWSTR title = NULL;
1456 TRACE("%p %p\n", dialog, dialog->package);
1458 dialog->hwnd = hwnd;
1459 SetWindowLongPtrW( hwnd, GWLP_USERDATA, (LONG_PTR) dialog );
1461 rec = msi_get_dialog_record( dialog );
1464 TRACE("No record found for dialog %s\n", debugstr_w(dialog->name));
1468 dialog->scale = msi_dialog_get_sans_serif_height(dialog->hwnd);
1470 size.cx = MSI_RecordGetInteger( rec, 4 );
1471 size.cy = MSI_RecordGetInteger( rec, 5 );
1472 msi_dialog_adjust_dialog_size( dialog, &size );
1474 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
1476 dialog->default_font = msi_dup_property( dialog->package, df );
1478 title = msi_get_deformatted_field( dialog->package, rec, 7 );
1479 SetWindowTextW( hwnd, title );
1480 HeapFree( GetProcessHeap(), 0, title );
1482 SetWindowPos( hwnd, 0, 0, 0, size.cx, size.cy,
1483 SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW );
1486 msi_dialog_build_font_list( dialog );
1487 msi_dialog_fill_controls( dialog );
1488 msi_dialog_evaluate_control_conditions( dialog );
1489 msi_dialog_set_tab_order( dialog );
1490 msi_dialog_set_first_control( dialog, MSI_RecordGetString( rec, 8 ) );
1491 msiobj_release( &rec->hdr );
1496 static UINT msi_dialog_send_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
1498 LPWSTR event_fmt = NULL, arg_fmt = NULL;
1500 TRACE("Sending control event %s %s\n", debugstr_w(event), debugstr_w(arg));
1502 deformat_string( dialog->package, event, &event_fmt );
1503 deformat_string( dialog->package, arg, &arg_fmt );
1505 dialog->event_handler( dialog->package, event_fmt, arg_fmt, dialog );
1507 HeapFree( GetProcessHeap(), 0, event_fmt );
1508 HeapFree( GetProcessHeap(), 0, arg_fmt );
1510 return ERROR_SUCCESS;
1513 static UINT msi_dialog_set_property( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
1515 static const WCHAR szNullArg[] = { '{','}',0 };
1516 LPWSTR p, prop, arg_fmt = NULL;
1519 len = strlenW(event);
1520 prop = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR));
1521 strcpyW( prop, &event[1] );
1522 p = strchrW( prop, ']' );
1523 if( p && p[1] == 0 )
1526 if( strcmpW( szNullArg, arg ) )
1527 deformat_string( dialog->package, arg, &arg_fmt );
1528 MSI_SetPropertyW( dialog->package, prop, arg_fmt );
1531 ERR("Badly formatted property string - what happens?\n");
1532 HeapFree( GetProcessHeap(), 0, prop );
1533 return ERROR_SUCCESS;
1536 static UINT msi_dialog_control_event( MSIRECORD *rec, LPVOID param )
1538 msi_dialog *dialog = param;
1539 LPCWSTR condition, event, arg;
1542 condition = MSI_RecordGetString( rec, 5 );
1543 r = MSI_EvaluateConditionW( dialog->package, condition );
1546 event = MSI_RecordGetString( rec, 3 );
1547 arg = MSI_RecordGetString( rec, 4 );
1548 if( event[0] == '[' )
1549 msi_dialog_set_property( dialog, event, arg );
1551 msi_dialog_send_event( dialog, event, arg );
1554 return ERROR_SUCCESS;
1557 static UINT msi_dialog_button_handler( msi_dialog *dialog,
1558 msi_control *control, WPARAM param )
1560 static const WCHAR query[] = {
1561 'S','E','L','E','C','T',' ','*',' ',
1562 'F','R','O','M',' ','C','o','n','t','r','o','l','E','v','e','n','t',' ',
1563 'W','H','E','R','E',' ',
1564 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
1566 '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',' ',
1567 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','i','n','g','`',0
1569 MSIQUERY *view = NULL;
1572 if( HIWORD(param) != BN_CLICKED )
1573 return ERROR_SUCCESS;
1575 r = MSI_OpenQuery( dialog->package->db, &view, query,
1576 dialog->name, control->name );
1577 if( r != ERROR_SUCCESS )
1579 ERR("query failed\n");
1583 r = MSI_IterateRecords( view, 0, msi_dialog_control_event, dialog );
1584 msiobj_release( &view->hdr );
1589 static UINT msi_dialog_get_checkbox_state( msi_dialog *dialog,
1590 msi_control *control )
1592 WCHAR state[2] = { 0 };
1595 MSI_GetPropertyW( dialog->package, control->property, state, &sz );
1596 return state[0] ? 1 : 0;
1599 static void msi_dialog_set_checkbox_state( msi_dialog *dialog,
1600 msi_control *control, UINT state )
1602 static const WCHAR szState[] = { '1', 0 };
1605 /* if uncheck then the property is set to NULL */
1608 MSI_SetPropertyW( dialog->package, control->property, NULL );
1612 /* check for a custom state */
1613 if (control->value && control->value[0])
1614 val = control->value;
1618 MSI_SetPropertyW( dialog->package, control->property, val );
1621 static void msi_dialog_checkbox_sync_state( msi_dialog *dialog,
1622 msi_control *control )
1626 state = msi_dialog_get_checkbox_state( dialog, control );
1627 SendMessageW( control->hwnd, BM_SETCHECK,
1628 state ? BST_CHECKED : BST_UNCHECKED, 0 );
1631 static UINT msi_dialog_checkbox_handler( msi_dialog *dialog,
1632 msi_control *control, WPARAM param )
1636 if( HIWORD(param) != BN_CLICKED )
1637 return ERROR_SUCCESS;
1639 TRACE("clicked checkbox %s, set %s\n", debugstr_w(control->name),
1640 debugstr_w(control->property));
1642 state = msi_dialog_get_checkbox_state( dialog, control );
1643 state = state ? 0 : 1;
1644 msi_dialog_set_checkbox_state( dialog, control, state );
1645 msi_dialog_checkbox_sync_state( dialog, control );
1647 return msi_dialog_button_handler( dialog, control, param );
1650 static UINT msi_dialog_edit_handler( msi_dialog *dialog,
1651 msi_control *control, WPARAM param )
1656 if( HIWORD(param) != EN_CHANGE )
1657 return ERROR_SUCCESS;
1659 TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
1660 debugstr_w(control->property));
1663 buf = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
1666 r = GetWindowTextW( control->hwnd, buf, sz );
1670 buf = HeapReAlloc( GetProcessHeap(), 0, buf, sz*sizeof(WCHAR) );
1673 MSI_SetPropertyW( dialog->package, control->property, buf );
1675 HeapFree( GetProcessHeap(), 0, buf );
1677 return ERROR_SUCCESS;
1680 static UINT msi_dialog_radiogroup_handler( msi_dialog *dialog,
1681 msi_control *control, WPARAM param )
1683 if( HIWORD(param) != BN_CLICKED )
1684 return ERROR_SUCCESS;
1686 TRACE("clicked radio button %s, set %s\n", debugstr_w(control->name),
1687 debugstr_w(control->property));
1689 MSI_SetPropertyW( dialog->package, control->property, control->name );
1691 return msi_dialog_button_handler( dialog, control, param );
1694 static LRESULT msi_dialog_oncommand( msi_dialog *dialog, WPARAM param, HWND hwnd )
1696 msi_control *control;
1698 TRACE("%p %p %08x\n", dialog, hwnd, param);
1700 control = msi_dialog_find_control_by_hwnd( dialog, hwnd );
1703 if( control->handler )
1705 control->handler( dialog, control, param );
1706 msi_dialog_evaluate_control_conditions( dialog );
1710 ERR("button click from nowhere\n");
1714 static void msi_dialog_setfocus( msi_dialog *dialog )
1716 HWND hwnd = dialog->hWndFocus;
1718 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, TRUE);
1719 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, FALSE);
1721 dialog->hWndFocus = hwnd;
1724 static LRESULT WINAPI MSIDialog_WndProc( HWND hwnd, UINT msg,
1725 WPARAM wParam, LPARAM lParam )
1727 msi_dialog *dialog = (LPVOID) GetWindowLongPtrW( hwnd, GWLP_USERDATA );
1729 TRACE("0x%04x\n", msg);
1734 return msi_dialog_oncreate( hwnd, (LPCREATESTRUCTW)lParam );
1737 return msi_dialog_oncommand( dialog, wParam, (HWND)lParam );
1740 if( LOWORD(wParam) == WA_INACTIVE )
1741 dialog->hWndFocus = GetFocus();
1743 msi_dialog_setfocus( dialog );
1747 msi_dialog_setfocus( dialog );
1750 /* bounce back to our subclassed static control */
1751 case WM_CTLCOLORSTATIC:
1752 return SendMessageW( (HWND) lParam, WM_CTLCOLORSTATIC, wParam, lParam );
1755 dialog->hwnd = NULL;
1758 return DefWindowProcW(hwnd, msg, wParam, lParam);
1761 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1763 WNDPROC oldproc = (WNDPROC) GetPropW(hWnd, szButtonData);
1765 TRACE("hWnd %p msg %04x wParam 0x%08x lParam 0x%08lx\n", hWnd, msg, wParam, lParam);
1767 if (msg == WM_COMMAND) /* Forward notifications to dialog */
1768 SendMessageW(GetParent(hWnd), msg, wParam, lParam);
1770 return CallWindowProcW(oldproc, hWnd, msg, wParam, lParam);
1773 static LRESULT WINAPI MSIHiddenWindowProc( HWND hwnd, UINT msg,
1774 WPARAM wParam, LPARAM lParam )
1776 msi_dialog *dialog = (msi_dialog*) lParam;
1778 TRACE("%d %p\n", msg, dialog);
1782 case WM_MSI_DIALOG_CREATE:
1783 return msi_dialog_run_message_loop( dialog );
1784 case WM_MSI_DIALOG_DESTROY:
1785 msi_dialog_destroy( dialog );
1788 return DefWindowProcW( hwnd, msg, wParam, lParam );
1791 /* functions that interface to other modules within MSI */
1793 msi_dialog *msi_dialog_create( MSIPACKAGE* package, LPCWSTR szDialogName,
1794 msi_dialog_event_handler event_handler )
1796 MSIRECORD *rec = NULL;
1799 TRACE("%p %s\n", package, debugstr_w(szDialogName));
1801 /* allocate the structure for the dialog to use */
1802 dialog = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
1803 sizeof *dialog + sizeof(WCHAR)*strlenW(szDialogName) );
1806 strcpyW( dialog->name, szDialogName );
1807 msiobj_addref( &package->hdr );
1808 dialog->package = package;
1809 dialog->event_handler = event_handler;
1810 dialog->finished = 0;
1811 list_init( &dialog->controls );
1813 /* verify that the dialog exists */
1814 rec = msi_get_dialog_record( dialog );
1817 msiobj_release( &package->hdr );
1818 HeapFree( GetProcessHeap(), 0, dialog );
1821 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
1822 msiobj_release( &rec->hdr );
1827 static void msi_process_pending_messages( HWND hdlg )
1831 while( PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ) )
1833 if( hdlg && IsDialogMessageW( hdlg, &msg ))
1835 TranslateMessage( &msg );
1836 DispatchMessageW( &msg );
1840 void msi_dialog_end_dialog( msi_dialog *dialog )
1842 TRACE("%p\n", dialog);
1843 dialog->finished = 1;
1844 PostMessageW(dialog->hwnd, WM_NULL, 0, 0);
1847 void msi_dialog_check_messages( HANDLE handle )
1851 /* in threads other than the UI thread, block */
1852 if( uiThreadId != GetCurrentThreadId() )
1855 WaitForSingleObject( handle, INFINITE );
1859 /* there's two choices for the UI thread */
1862 msi_process_pending_messages( NULL );
1868 * block here until somebody creates a new dialog or
1869 * the handle we're waiting on becomes ready
1871 r = MsgWaitForMultipleObjects( 1, &handle, 0, INFINITE, QS_ALLINPUT );
1872 if( r == WAIT_OBJECT_0 )
1877 UINT msi_dialog_run_message_loop( msi_dialog *dialog )
1881 if( !(dialog->attributes & msidbDialogAttributesVisible) )
1882 return ERROR_SUCCESS;
1884 if( uiThreadId != GetCurrentThreadId() )
1885 return SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_CREATE, 0, (LPARAM) dialog );
1887 /* create the dialog window, don't show it yet */
1888 hwnd = CreateWindowW( szMsiDialogClass, dialog->name, WS_OVERLAPPEDWINDOW,
1889 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
1890 NULL, NULL, NULL, dialog );
1893 ERR("Failed to create dialog %s\n", debugstr_w( dialog->name ));
1894 return ERROR_FUNCTION_FAILED;
1897 ShowWindow( hwnd, SW_SHOW );
1898 UpdateWindow( hwnd );
1900 if( dialog->attributes & msidbDialogAttributesModal )
1902 while( !dialog->finished )
1904 MsgWaitForMultipleObjects( 0, NULL, 0, INFINITE, QS_ALLEVENTS );
1905 msi_process_pending_messages( dialog->hwnd );
1909 return ERROR_IO_PENDING;
1911 return ERROR_SUCCESS;
1914 void msi_dialog_do_preview( msi_dialog *dialog )
1917 dialog->attributes |= msidbDialogAttributesVisible;
1918 dialog->attributes &= ~msidbDialogAttributesModal;
1919 msi_dialog_run_message_loop( dialog );
1922 void msi_dialog_destroy( msi_dialog *dialog )
1924 if( uiThreadId != GetCurrentThreadId() )
1926 SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_DESTROY, 0, (LPARAM) dialog );
1931 ShowWindow( dialog->hwnd, SW_HIDE );
1934 DestroyWindow( dialog->hwnd );
1936 /* destroy the list of controls */
1937 while( !list_empty( &dialog->controls ) )
1939 msi_control *t = LIST_ENTRY( list_head( &dialog->controls ),
1940 msi_control, entry );
1941 list_remove( &t->entry );
1942 /* leave dialog->hwnd - destroying parent destroys child windows */
1943 HeapFree( GetProcessHeap(), 0, t->property );
1944 HeapFree( GetProcessHeap(), 0, t->value );
1946 DeleteObject( t->hBitmap );
1948 DestroyIcon( t->hIcon );
1949 HeapFree( GetProcessHeap(), 0, t->tabnext );
1950 HeapFree( GetProcessHeap(), 0, t );
1952 FreeLibrary( t->hDll );
1955 /* destroy the list of fonts */
1956 while( dialog->font_list )
1958 msi_font *t = dialog->font_list;
1959 dialog->font_list = t->next;
1960 DeleteObject( t->hfont );
1961 HeapFree( GetProcessHeap(), 0, t );
1963 HeapFree( GetProcessHeap(), 0, dialog->default_font );
1965 msiobj_release( &dialog->package->hdr );
1966 dialog->package = NULL;
1967 HeapFree( GetProcessHeap(), 0, dialog );
1970 BOOL msi_dialog_register_class( void )
1974 ZeroMemory( &cls, sizeof cls );
1975 cls.lpfnWndProc = MSIDialog_WndProc;
1976 cls.hInstance = NULL;
1977 cls.hIcon = LoadIconW(0, (LPWSTR)IDI_APPLICATION);
1978 cls.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
1979 cls.hbrBackground = (HBRUSH)(COLOR_WINDOW);
1980 cls.lpszMenuName = NULL;
1981 cls.lpszClassName = szMsiDialogClass;
1983 if( !RegisterClassW( &cls ) )
1986 cls.lpfnWndProc = MSIHiddenWindowProc;
1987 cls.lpszClassName = szMsiHiddenWindow;
1989 if( !RegisterClassW( &cls ) )
1992 uiThreadId = GetCurrentThreadId();
1994 hMsiHiddenWindow = CreateWindowW( szMsiHiddenWindow, NULL, WS_OVERLAPPED,
1995 0, 0, 100, 100, NULL, NULL, NULL, NULL );
1996 if( !hMsiHiddenWindow )
2002 void msi_dialog_unregister_class( void )
2004 DestroyWindow( hMsiHiddenWindow );
2005 UnregisterClassW( szMsiDialogClass, NULL );