Make sure not to free a a pointer that has been modified.
[wine/multimedia.git] / dlls / msi / dialog.c
blob204d524670a511a2a331392266ccf6e73a981b57
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2005 Mike McCormack for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #define COBJMACROS
23 #include <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "winnls.h"
29 #include "wingdi.h"
30 #include "msi.h"
31 #include "msipriv.h"
32 #include "msidefs.h"
33 #include "ocidl.h"
34 #include "olectl.h"
35 #include "richedit.h"
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
40 #include "action.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 list entry;
52 HWND hwnd;
53 msi_handler handler;
54 LPWSTR property;
55 LPWSTR value;
56 HBITMAP hBitmap;
57 HICON hIcon;
58 LPWSTR tabnext;
59 HMODULE hDll;
60 WCHAR name[1];
63 typedef struct msi_font_tag
65 struct msi_font_tag *next;
66 HFONT hfont;
67 WCHAR name[1];
68 } msi_font;
70 struct msi_dialog_tag
72 MSIPACKAGE *package;
73 msi_dialog_event_handler event_handler;
74 BOOL finished;
75 INT scale;
76 DWORD attributes;
77 HWND hwnd;
78 LPWSTR default_font;
79 msi_font *font_list;
80 struct list controls;
81 HWND hWndFocus;
82 WCHAR name[1];
85 typedef UINT (*msi_dialog_control_func)( msi_dialog *dialog, MSIRECORD *rec );
86 struct control_handler
88 LPCWSTR control_type;
89 msi_dialog_control_func func;
92 typedef struct
94 msi_dialog* dialog;
95 msi_control *parent;
96 DWORD attributes;
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;
148 if( !name )
149 return NULL;
150 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
151 if( !strcmpW( control->name, name ) ) /* FIXME: case sensitive? */
152 break;
153 return control;
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 )
162 break;
163 return control;
166 static LPWSTR msi_get_deformatted_field( MSIPACKAGE *package, MSIRECORD *rec, int field )
168 LPCWSTR str = MSI_RecordGetString( rec, field );
169 LPWSTR ret = NULL;
171 if (str)
172 deformat_string( package, str, &ret );
173 return 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( LPCWSTR p, LPCWSTR *rest )
184 LPWSTR ret = NULL;
185 LPCWSTR q, i;
186 DWORD len;
188 *rest = p;
189 if( !p )
190 return ret;
191 if( *p++ != '{' )
192 return ret;
193 q = strchrW( p, '}' );
194 if( !q )
195 return ret;
196 if( *p++ != '\\' )
197 return ret;
199 /* little bit of sanity checking to stop us getting confused with RTF */
200 for( i=p; i<q; i++ )
201 if( *i == '}' || *i == '\\' )
202 return ret;
204 *rest = ++q;
205 len = q - p;
207 ret = msi_alloc( len*sizeof(WCHAR) );
208 if( !ret )
209 return ret;
210 memcpy( ret, p, len*sizeof(WCHAR) );
211 ret[len-1] = 0;
212 return ret;
215 static UINT msi_dialog_add_font( MSIRECORD *rec, LPVOID param )
217 msi_dialog *dialog = param;
218 msi_font *font;
219 LPCWSTR face, name;
220 LOGFONTW lf;
221 INT style;
222 HDC hdc;
224 /* create a font and add it to the list */
225 name = MSI_RecordGetString( rec, 1 );
226 font = msi_alloc( sizeof *font + strlenW( name )*sizeof (WCHAR) );
227 strcpyW( font->name, name );
228 font->next = dialog->font_list;
229 dialog->font_list = font;
231 memset( &lf, 0, sizeof lf );
232 face = MSI_RecordGetString( rec, 2 );
233 lf.lfHeight = MSI_RecordGetInteger( rec, 3 );
234 style = MSI_RecordGetInteger( rec, 5 );
235 if( style & msidbTextStyleStyleBitsBold )
236 lf.lfWeight = FW_BOLD;
237 if( style & msidbTextStyleStyleBitsItalic )
238 lf.lfItalic = TRUE;
239 if( style & msidbTextStyleStyleBitsUnderline )
240 lf.lfUnderline = TRUE;
241 if( style & msidbTextStyleStyleBitsStrike )
242 lf.lfStrikeOut = TRUE;
243 lstrcpynW( lf.lfFaceName, face, LF_FACESIZE );
245 /* adjust the height */
246 hdc = GetDC( dialog->hwnd );
247 if (hdc)
249 lf.lfHeight = -MulDiv(lf.lfHeight, GetDeviceCaps(hdc, LOGPIXELSY), 72);
250 ReleaseDC( dialog->hwnd, hdc );
253 font->hfont = CreateFontIndirectW( &lf );
255 TRACE("Adding font style %s\n", debugstr_w(font->name) );
257 return ERROR_SUCCESS;
260 static msi_font *msi_dialog_find_font( msi_dialog *dialog, LPCWSTR name )
262 msi_font *font;
264 for( font = dialog->font_list; font; font = font->next )
265 if( !strcmpW( font->name, name ) ) /* FIXME: case sensitive? */
266 break;
268 return font;
271 static UINT msi_dialog_set_font( msi_dialog *dialog, HWND hwnd, LPCWSTR name )
273 msi_font *font;
275 font = msi_dialog_find_font( dialog, name );
276 if( font )
277 SendMessageW( hwnd, WM_SETFONT, (WPARAM) font->hfont, TRUE );
278 else
279 ERR("No font entry for %s\n", debugstr_w(name));
280 return ERROR_SUCCESS;
283 static UINT msi_dialog_build_font_list( msi_dialog *dialog )
285 static const WCHAR query[] = {
286 'S','E','L','E','C','T',' ','*',' ',
287 'F','R','O','M',' ','`','T','e','x','t','S','t','y','l','e','`',' ',0
289 UINT r;
290 MSIQUERY *view = NULL;
292 TRACE("dialog %p\n", dialog );
294 r = MSI_OpenQuery( dialog->package->db, &view, query );
295 if( r != ERROR_SUCCESS )
296 return r;
298 r = MSI_IterateRecords( view, NULL, msi_dialog_add_font, dialog );
299 msiobj_release( &view->hdr );
301 return r;
304 static msi_control *msi_dialog_create_window( msi_dialog *dialog,
305 MSIRECORD *rec, LPCWSTR szCls, LPCWSTR name, LPCWSTR text,
306 DWORD style, HWND parent )
308 DWORD x, y, width, height;
309 LPWSTR font = NULL, title_font = NULL;
310 LPCWSTR title = NULL;
311 msi_control *control;
313 style |= WS_CHILD;
315 control = msi_alloc( sizeof *control + strlenW(name)*sizeof(WCHAR) );
316 strcpyW( control->name, name );
317 list_add_head( &dialog->controls, &control->entry );
318 control->handler = NULL;
319 control->property = NULL;
320 control->value = NULL;
321 control->hBitmap = NULL;
322 control->hIcon = NULL;
323 control->hDll = NULL;
324 control->tabnext = strdupW( MSI_RecordGetString( rec, 11) );
326 x = MSI_RecordGetInteger( rec, 4 );
327 y = MSI_RecordGetInteger( rec, 5 );
328 width = MSI_RecordGetInteger( rec, 6 );
329 height = MSI_RecordGetInteger( rec, 7 );
331 x = msi_dialog_scale_unit( dialog, x );
332 y = msi_dialog_scale_unit( dialog, y );
333 width = msi_dialog_scale_unit( dialog, width );
334 height = msi_dialog_scale_unit( dialog, height );
336 if( text )
338 deformat_string( dialog->package, text, &title_font );
339 font = msi_dialog_get_style( title_font, &title );
342 control->hwnd = CreateWindowW( szCls, title, style,
343 x, y, width, height, parent, NULL, NULL, NULL );
345 TRACE("Dialog %s control %s hwnd %p\n",
346 debugstr_w(dialog->name), debugstr_w(text), control->hwnd );
348 msi_dialog_set_font( dialog, control->hwnd,
349 font ? font : dialog->default_font );
351 msi_free( title_font );
352 msi_free( font );
354 return control;
357 static MSIRECORD *msi_get_binary_record( MSIDATABASE *db, LPCWSTR name )
359 const static WCHAR query[] = {
360 's','e','l','e','c','t',' ','*',' ',
361 'f','r','o','m',' ','B','i','n','a','r','y',' ',
362 'w','h','e','r','e',' ',
363 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0
366 return MSI_QueryGetRecord( db, query, name );
369 static LPWSTR msi_create_tmp_path(void)
371 WCHAR tmp[MAX_PATH];
372 LPWSTR path = NULL;
373 static const WCHAR prefix[] = { 'm','s','i',0 };
374 DWORD len, r;
376 r = GetTempPathW( MAX_PATH, tmp );
377 if( !r )
378 return path;
379 len = lstrlenW( tmp ) + 20;
380 path = msi_alloc( len * sizeof (WCHAR) );
381 if( path )
383 r = GetTempFileNameW( tmp, prefix, 0, path );
384 if (!r)
386 msi_free( path );
387 path = NULL;
390 return path;
394 static HANDLE msi_load_image( MSIDATABASE *db, LPCWSTR name, UINT type,
395 UINT cx, UINT cy, UINT flags )
397 MSIRECORD *rec = NULL;
398 HANDLE himage = NULL;
399 LPWSTR tmp;
400 UINT r;
402 TRACE("%p %s %u %u %08x\n", db, debugstr_w(name), cx, cy, flags);
404 tmp = msi_create_tmp_path();
405 if( !tmp )
406 return himage;
408 rec = msi_get_binary_record( db, name );
409 if( rec )
411 r = MSI_RecordStreamToFile( rec, 2, tmp );
412 if( r == ERROR_SUCCESS )
414 himage = LoadImageW( 0, tmp, type, cx, cy, flags );
415 DeleteFileW( tmp );
417 msiobj_release( &rec->hdr );
420 msi_free( tmp );
421 return himage;
424 static HICON msi_load_icon( MSIDATABASE *db, LPCWSTR text, UINT attributes )
426 DWORD cx = 0, cy = 0, flags;
428 flags = LR_LOADFROMFILE | LR_DEFAULTSIZE;
429 if( attributes & msidbControlAttributesFixedSize )
431 flags &= ~LR_DEFAULTSIZE;
432 if( attributes & msidbControlAttributesIconSize16 )
434 cx += 16;
435 cy += 16;
437 if( attributes & msidbControlAttributesIconSize32 )
439 cx += 32;
440 cy += 32;
442 /* msidbControlAttributesIconSize48 handled by above logic */
444 return msi_load_image( db, text, IMAGE_ICON, cx, cy, flags );
448 /* called from the Control Event subscription code */
449 void msi_dialog_handle_event( msi_dialog* dialog, LPCWSTR control,
450 LPCWSTR attribute, MSIRECORD *rec )
452 msi_control* ctrl;
453 LPCWSTR text;
455 ctrl = msi_dialog_find_control( dialog, control );
456 if (!ctrl)
457 return;
458 if( lstrcmpW(attribute, szText) )
459 return;
460 text = MSI_RecordGetString( rec , 1 );
461 SetWindowTextW( ctrl->hwnd, text );
462 msi_dialog_check_messages( NULL );
465 static void msi_dialog_map_events(msi_dialog* dialog, LPCWSTR control)
467 static WCHAR Query[] = {
468 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
469 '`','E','v','e','n','t','M','a','p','p','i','n','g','`',' ',
470 'W','H','E','R','E',' ',
471 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
472 'A','N','D',' ',
473 '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',0
475 MSIRECORD *row;
476 LPCWSTR event, attribute;
478 row = MSI_QueryGetRecord( dialog->package->db, Query, dialog->name, control );
479 if (!row)
480 return;
482 event = MSI_RecordGetString( row, 3 );
483 attribute = MSI_RecordGetString( row, 4 );
484 ControlEvent_SubscribeToEvent( dialog->package, event, control, attribute );
485 msiobj_release( &row->hdr );
488 /* everything except radio buttons */
489 static msi_control *msi_dialog_add_control( msi_dialog *dialog,
490 MSIRECORD *rec, LPCWSTR szCls, DWORD style )
492 DWORD attributes;
493 LPCWSTR text, name;
495 name = MSI_RecordGetString( rec, 2 );
496 attributes = MSI_RecordGetInteger( rec, 8 );
497 text = MSI_RecordGetString( rec, 10 );
498 if( attributes & msidbControlAttributesVisible )
499 style |= WS_VISIBLE;
500 if( ~attributes & msidbControlAttributesEnabled )
501 style |= WS_DISABLED;
503 msi_dialog_map_events(dialog, name);
505 return msi_dialog_create_window( dialog, rec, szCls, name, text,
506 style, dialog->hwnd );
509 struct msi_text_info
511 WNDPROC oldproc;
512 DWORD attributes;
516 * we don't erase our own background,
517 * so we have to make sure that the parent window redraws first
519 static void msi_text_on_settext( HWND hWnd )
521 HWND hParent;
522 RECT rc;
524 hParent = GetParent( hWnd );
525 GetWindowRect( hWnd, &rc );
526 MapWindowPoints( NULL, hParent, (LPPOINT) &rc, 2 );
527 InvalidateRect( hParent, &rc, TRUE );
530 static LRESULT WINAPI
531 MSIText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
533 struct msi_text_info *info;
534 LRESULT r = 0;
536 TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
538 info = GetPropW(hWnd, szButtonData);
540 if( msg == WM_CTLCOLORSTATIC &&
541 ( info->attributes & msidbControlAttributesTransparent ) )
543 SetBkMode( (HDC)wParam, TRANSPARENT );
544 return (LRESULT) GetStockObject(NULL_BRUSH);
547 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
549 switch( msg )
551 case WM_SETTEXT:
552 msi_text_on_settext( hWnd );
553 break;
554 case WM_NCDESTROY:
555 msi_free( info );
556 RemovePropW( hWnd, szButtonData );
557 break;
560 return r;
563 static UINT msi_dialog_text_control( msi_dialog *dialog, MSIRECORD *rec )
565 msi_control *control;
566 struct msi_text_info *info;
568 TRACE("%p %p\n", dialog, rec);
570 control = msi_dialog_add_control( dialog, rec, szStatic, SS_LEFT | WS_GROUP );
571 if( !control )
572 return ERROR_FUNCTION_FAILED;
574 info = msi_alloc( sizeof *info );
575 if( !info )
576 return ERROR_SUCCESS;
578 info->attributes = MSI_RecordGetInteger( rec, 8 );
579 if( info->attributes & msidbControlAttributesTransparent )
580 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT );
582 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
583 (LONG_PTR)MSIText_WndProc );
584 SetPropW( control->hwnd, szButtonData, info );
586 return ERROR_SUCCESS;
589 static UINT msi_dialog_button_control( msi_dialog *dialog, MSIRECORD *rec )
591 msi_control *control;
592 UINT attributes, style;
593 LPWSTR text;
595 TRACE("%p %p\n", dialog, rec);
597 style = WS_TABSTOP;
598 attributes = MSI_RecordGetInteger( rec, 8 );
599 if( attributes & msidbControlAttributesIcon )
600 style |= BS_ICON;
602 control = msi_dialog_add_control( dialog, rec, szButton, style );
603 if( !control )
604 return ERROR_FUNCTION_FAILED;
606 control->handler = msi_dialog_button_handler;
608 /* set the icon */
609 text = msi_get_deformatted_field( dialog->package, rec, 10 );
610 control->hIcon = msi_load_icon( dialog->package->db, text, attributes );
611 if( attributes & msidbControlAttributesIcon )
612 SendMessageW( control->hwnd, BM_SETIMAGE, IMAGE_ICON, (LPARAM) control->hIcon );
613 msi_free( text );
615 return ERROR_SUCCESS;
618 static LPWSTR msi_get_checkbox_value( msi_dialog *dialog, LPCWSTR prop )
620 const static WCHAR query[] = {
621 'S','E','L','E','C','T',' ','*',' ',
622 'F','R','O','M',' ','`','C','h','e','c','k','B','o','x',' ','`',
623 'W','H','E','R','E',' ',
624 '`','P','r','o','p','e','r','t','y','`',' ','=',' ',
625 '\'','%','s','\'',0
627 MSIRECORD *rec = NULL;
628 LPCWSTR val = NULL;
629 LPWSTR ret = NULL;
631 /* find if there is a value associated with the checkbox */
632 rec = MSI_QueryGetRecord( dialog->package->db, query, prop );
633 if (!rec)
634 return ret;
636 val = MSI_RecordGetString( rec, 2 );
637 if (val)
639 deformat_string( dialog->package, val, &ret );
640 if( ret && !ret[0] )
642 msi_free( ret );
643 ret = NULL;
646 msiobj_release( &rec->hdr );
647 if (ret)
648 return ret;
650 ret = msi_dup_property( dialog->package, prop );
651 if( ret && !ret[0] )
653 msi_free( ret );
654 ret = NULL;
657 return ret;
660 static UINT msi_dialog_checkbox_control( msi_dialog *dialog, MSIRECORD *rec )
662 msi_control *control;
663 LPCWSTR prop;
665 TRACE("%p %p\n", dialog, rec);
667 control = msi_dialog_add_control( dialog, rec, szButton,
668 BS_CHECKBOX | BS_MULTILINE | WS_TABSTOP );
669 control->handler = msi_dialog_checkbox_handler;
670 prop = MSI_RecordGetString( rec, 9 );
671 if( prop )
673 control->property = strdupW( prop );
674 control->value = msi_get_checkbox_value( dialog, prop );
675 TRACE("control %s value %s\n", debugstr_w(control->property),
676 debugstr_w(control->value));
678 msi_dialog_checkbox_sync_state( dialog, control );
680 return ERROR_SUCCESS;
683 static UINT msi_dialog_line_control( msi_dialog *dialog, MSIRECORD *rec )
685 TRACE("%p %p\n", dialog, rec);
687 msi_dialog_add_control( dialog, rec, szStatic, SS_ETCHEDHORZ | SS_SUNKEN );
688 return ERROR_SUCCESS;
691 struct msi_streamin_info
693 LPSTR string;
694 DWORD offset;
695 DWORD length;
698 static DWORD CALLBACK
699 msi_richedit_stream_in( DWORD_PTR arg, LPBYTE buffer, LONG count, LONG *pcb )
701 struct msi_streamin_info *info = (struct msi_streamin_info*) arg;
703 if( (count + info->offset) > info->length )
704 count = info->length - info->offset;
705 memcpy( buffer, &info->string[ info->offset ], count );
706 *pcb = count;
707 info->offset += count;
709 TRACE("%ld/%ld\n", info->offset, info->length);
711 return 0;
714 static UINT msi_dialog_scrolltext_control( msi_dialog *dialog, MSIRECORD *rec )
716 const static WCHAR szRichEdit20W[] = {
717 'R','i','c','h','E','d','i','t','2','0','W',0
719 struct msi_streamin_info info;
720 msi_control *control;
721 LPCWSTR text;
722 EDITSTREAM es;
723 DWORD style;
724 HMODULE hRichedit;
726 hRichedit = LoadLibraryA("riched20");
728 style = WS_BORDER | ES_MULTILINE | WS_VSCROLL |
729 ES_READONLY | ES_AUTOVSCROLL | WS_TABSTOP;
730 control = msi_dialog_add_control( dialog, rec, szRichEdit20W, style );
731 if (!control)
732 return ERROR_FUNCTION_FAILED;
734 control->hDll = hRichedit;
736 text = MSI_RecordGetString( rec, 10 );
737 info.string = strdupWtoA( text );
738 info.offset = 0;
739 info.length = lstrlenA( info.string ) + 1;
741 es.dwCookie = (DWORD_PTR) &info;
742 es.dwError = 0;
743 es.pfnCallback = msi_richedit_stream_in;
745 SendMessageW( control->hwnd, EM_STREAMIN, SF_RTF, (LPARAM) &es );
747 msi_free( info.string );
749 return ERROR_SUCCESS;
752 static UINT msi_dialog_bitmap_control( msi_dialog *dialog, MSIRECORD *rec )
754 UINT cx, cy, flags, style, attributes;
755 msi_control *control;
756 LPWSTR text;
758 flags = LR_LOADFROMFILE;
759 style = SS_BITMAP | SS_LEFT | WS_GROUP;
761 attributes = MSI_RecordGetInteger( rec, 8 );
762 if( attributes & msidbControlAttributesFixedSize )
764 flags |= LR_DEFAULTSIZE;
765 style |= SS_CENTERIMAGE;
768 control = msi_dialog_add_control( dialog, rec, szStatic, style );
769 cx = MSI_RecordGetInteger( rec, 6 );
770 cy = MSI_RecordGetInteger( rec, 7 );
771 cx = msi_dialog_scale_unit( dialog, cx );
772 cy = msi_dialog_scale_unit( dialog, cy );
774 text = msi_get_deformatted_field( dialog->package, rec, 10 );
775 control->hBitmap = msi_load_image( dialog->package->db, text,
776 IMAGE_BITMAP, cx, cy, flags );
777 if( control->hBitmap )
778 SendMessageW( control->hwnd, STM_SETIMAGE,
779 IMAGE_BITMAP, (LPARAM) control->hBitmap );
780 else
781 ERR("Failed to load bitmap %s\n", debugstr_w(text));
783 msi_free( text );
785 return ERROR_SUCCESS;
788 static UINT msi_dialog_icon_control( msi_dialog *dialog, MSIRECORD *rec )
790 msi_control *control;
791 DWORD attributes;
792 LPWSTR text;
794 TRACE("\n");
796 control = msi_dialog_add_control( dialog, rec, szStatic,
797 SS_ICON | SS_CENTERIMAGE | WS_GROUP );
799 attributes = MSI_RecordGetInteger( rec, 8 );
800 text = msi_get_deformatted_field( dialog->package, rec, 10 );
801 control->hIcon = msi_load_icon( dialog->package->db, text, attributes );
802 if( control->hIcon )
803 SendMessageW( control->hwnd, STM_SETICON, (WPARAM) control->hIcon, 0 );
804 else
805 ERR("Failed to load bitmap %s\n", debugstr_w(text));
806 msi_free( text );
807 return ERROR_SUCCESS;
810 static UINT msi_dialog_combo_control( msi_dialog *dialog, MSIRECORD *rec )
812 static const WCHAR szCombo[] = { 'C','O','M','B','O','B','O','X',0 };
814 msi_dialog_add_control( dialog, rec, szCombo,
815 SS_BITMAP | SS_LEFT | SS_CENTERIMAGE );
816 return ERROR_SUCCESS;
819 static UINT msi_dialog_edit_control( msi_dialog *dialog, MSIRECORD *rec )
821 msi_control *control;
822 LPCWSTR prop;
823 LPWSTR val;
825 control = msi_dialog_add_control( dialog, rec, szEdit,
826 WS_BORDER | WS_TABSTOP );
827 control->handler = msi_dialog_edit_handler;
828 prop = MSI_RecordGetString( rec, 9 );
829 if( prop )
830 control->property = strdupW( prop );
831 val = msi_dup_property( dialog->package, control->property );
832 SetWindowTextW( control->hwnd, val );
833 msi_free( val );
834 return ERROR_SUCCESS;
837 /******************** Masked Edit ********************************************/
839 #define MASK_MAX_GROUPS 10
841 struct msi_mask_group
843 UINT len;
844 UINT ofs;
845 WCHAR type;
846 HWND hwnd;
849 struct msi_maskedit_info
851 msi_dialog *dialog;
852 WNDPROC oldproc;
853 HWND hwnd;
854 LPWSTR prop;
855 UINT num_chars;
856 UINT num_groups;
857 struct msi_mask_group group[MASK_MAX_GROUPS];
860 static void msi_mask_control_change( struct msi_maskedit_info *info )
862 LPWSTR val;
863 UINT i, n, r;
865 val = msi_alloc( (info->num_chars+1)*sizeof(WCHAR) );
866 for( i=0, n=0; i<info->num_groups; i++ )
868 if( (info->group[i].len + n) > info->num_chars )
870 ERR("can't fit control %d text into template\n",i);
871 break;
873 r = GetWindowTextW( info->group[i].hwnd, &val[n], info->group[i].len+1 );
874 if( r != info->group[i].len )
875 break;
876 n += r;
879 TRACE("%d/%d controls were good\n", i, info->num_groups);
881 if( i == info->num_groups )
883 TRACE("Set property %s to %s\n",
884 debugstr_w(info->prop), debugstr_w(val) );
885 CharUpperBuffW( val, info->num_chars );
886 MSI_SetPropertyW( info->dialog->package, info->prop, val );
887 msi_dialog_evaluate_control_conditions( info->dialog );
889 msi_free( val );
892 /* now move to the next control if necessary */
893 static VOID msi_mask_next_control( struct msi_maskedit_info *info, HWND hWnd )
895 HWND hWndNext;
896 UINT len, i;
898 for( i=0; i<info->num_groups; i++ )
899 if( info->group[i].hwnd == hWnd )
900 break;
902 /* don't move from the last control */
903 if( i >= (info->num_groups-1) )
904 return;
906 len = SendMessageW( hWnd, WM_GETTEXTLENGTH, 0, 0 );
907 if( len < info->group[i].len )
908 return;
910 hWndNext = GetNextDlgTabItem( GetParent( hWnd ), hWnd, FALSE );
911 SetFocus( hWndNext );
914 static LRESULT WINAPI
915 MSIMaskedEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
917 struct msi_maskedit_info *info;
918 HRESULT r;
920 TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
922 info = GetPropW(hWnd, szButtonData);
924 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
926 switch( msg )
928 case WM_COMMAND:
929 if (HIWORD(wParam) == EN_CHANGE)
931 msi_mask_control_change( info );
932 msi_mask_next_control( info, (HWND) lParam );
934 break;
935 case WM_NCDESTROY:
936 msi_free( info->prop );
937 msi_free( info );
938 RemovePropW( hWnd, szButtonData );
939 break;
942 return r;
945 /* fish the various bits of the property out and put them in the control */
946 static void
947 msi_maskedit_set_text( struct msi_maskedit_info *info, LPCWSTR text )
949 LPCWSTR p;
950 UINT i;
952 p = text;
953 for( i = 0; i < info->num_groups; i++ )
955 if( info->group[i].len < lstrlenW( p ) )
957 LPWSTR chunk = strdupW( p );
958 chunk[ info->group[i].len ] = 0;
959 SetWindowTextW( info->group[i].hwnd, chunk );
960 msi_free( chunk );
962 else
964 SetWindowTextW( info->group[i].hwnd, p );
965 break;
967 p += info->group[i].len;
971 static struct msi_maskedit_info * msi_dialog_parse_groups( LPCWSTR mask )
973 struct msi_maskedit_info * info = NULL;
974 int i = 0, n = 0, total = 0;
975 LPCWSTR p;
977 TRACE("masked control, template %s\n", debugstr_w(mask));
979 if( !mask )
980 return info;
982 p = strchrW(mask, '<');
983 if( !p )
984 return info;
986 info = msi_alloc_zero( sizeof *info );
987 if( !info )
988 return info;
990 p++;
991 for( i=0; i<MASK_MAX_GROUPS; i++ )
993 while (*p=='-')
995 total++;
996 p++;
999 /* stop at the end of the string */
1000 if( p[0] == 0 || p[0] == '>' )
1001 break;
1003 /* count the number of the same identifier */
1004 for( n=0; p[n] == p[0]; n++ )
1006 info->group[i].ofs = total;
1007 info->group[i].type = p[0];
1008 if( p[n] == '=' )
1010 n++;
1011 total++; /* an extra not part of the group */
1013 info->group[i].len = n;
1014 total += n;
1015 p += n;
1018 TRACE("%d characters in %d groups\n", total, i );
1019 if( i == MASK_MAX_GROUPS )
1020 ERR("too many groups in PIDTemplate %s\n", debugstr_w(mask));
1022 info->num_chars = total;
1023 info->num_groups = i;
1025 return info;
1028 static void
1029 msi_maskedit_create_children( struct msi_maskedit_info *info, LPCWSTR font )
1031 DWORD width, height, style, wx, ww;
1032 RECT rect;
1033 HWND hwnd;
1034 UINT i;
1036 style = WS_CHILD | WS_BORDER | WS_VISIBLE | WS_TABSTOP;
1038 GetClientRect( info->hwnd, &rect );
1040 width = rect.right - rect.left;
1041 height = rect.bottom - rect.top;
1043 for( i = 0; i < info->num_groups; i++ )
1045 wx = (info->group[i].ofs * width) / info->num_chars;
1046 ww = (info->group[i].len * width) / info->num_chars;
1048 hwnd = CreateWindowW( szEdit, NULL, style, wx, 0, ww, height,
1049 info->hwnd, NULL, NULL, NULL );
1050 if( !hwnd )
1052 ERR("failed to create mask edit sub window\n");
1053 break;
1056 SendMessageW( hwnd, EM_LIMITTEXT, info->group[i].len, 0 );
1058 msi_dialog_set_font( info->dialog, hwnd,
1059 font?font:info->dialog->default_font );
1060 info->group[i].hwnd = hwnd;
1064 /* office 2003 uses "73931<````=````=````=````=`````>@@@@@" */
1065 static UINT msi_dialog_maskedit_control( msi_dialog *dialog, MSIRECORD *rec )
1067 LPWSTR font_mask, val = NULL, font;
1068 struct msi_maskedit_info *info = NULL;
1069 UINT ret = ERROR_SUCCESS;
1070 msi_control *control;
1071 LPCWSTR prop, mask;
1073 TRACE("\n");
1075 font_mask = msi_get_deformatted_field( dialog->package, rec, 10 );
1076 font = msi_dialog_get_style( font_mask, &mask );
1077 if( !mask )
1079 ERR("mask template is empty\n");
1080 goto end;
1083 info = msi_dialog_parse_groups( mask );
1084 if( !info )
1086 ERR("template %s is invalid\n", debugstr_w(mask));
1087 goto end;
1090 info->dialog = dialog;
1092 control = msi_dialog_add_control( dialog, rec, szStatic,
1093 SS_OWNERDRAW | WS_GROUP | WS_VISIBLE );
1094 if( !control )
1096 ERR("Failed to create maskedit container\n");
1097 ret = ERROR_FUNCTION_FAILED;
1098 goto end;
1100 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
1102 info->hwnd = control->hwnd;
1104 /* subclass the static control */
1105 info->oldproc = (WNDPROC) SetWindowLongPtrW( info->hwnd, GWLP_WNDPROC,
1106 (LONG_PTR)MSIMaskedEdit_WndProc );
1107 SetPropW( control->hwnd, szButtonData, info );
1109 prop = MSI_RecordGetString( rec, 9 );
1110 if( prop )
1111 info->prop = strdupW( prop );
1113 msi_maskedit_create_children( info, font );
1115 if( prop )
1117 val = msi_dup_property( dialog->package, prop );
1118 if( val )
1120 msi_maskedit_set_text( info, val );
1121 msi_free( val );
1125 end:
1126 if( ret != ERROR_SUCCESS )
1127 msi_free( info );
1128 msi_free( font_mask );
1129 msi_free( font );
1130 return ret;
1133 /******************** Path Edit ********************************************/
1135 static UINT msi_dialog_pathedit_control( msi_dialog *dialog, MSIRECORD *rec )
1137 FIXME("not implemented properly\n");
1138 return msi_dialog_edit_control( dialog, rec );
1141 /* radio buttons are a bit different from normal controls */
1142 static UINT msi_dialog_create_radiobutton( MSIRECORD *rec, LPVOID param )
1144 radio_button_group_descr *group = (radio_button_group_descr *)param;
1145 msi_dialog *dialog = group->dialog;
1146 msi_control *control;
1147 LPCWSTR prop, text, name;
1148 DWORD style, attributes = group->attributes;
1150 style = WS_CHILD | BS_AUTORADIOBUTTON | BS_MULTILINE | WS_TABSTOP;
1151 name = MSI_RecordGetString( rec, 3 );
1152 text = MSI_RecordGetString( rec, 8 );
1153 if( attributes & 1 )
1154 style |= WS_VISIBLE;
1155 if( ~attributes & 2 )
1156 style |= WS_DISABLED;
1158 control = msi_dialog_create_window( dialog, rec, szButton, name, text,
1159 style, group->parent->hwnd );
1160 if (!control)
1161 return ERROR_FUNCTION_FAILED;
1162 control->handler = msi_dialog_radiogroup_handler;
1164 prop = MSI_RecordGetString( rec, 1 );
1165 if( prop )
1166 control->property = strdupW( prop );
1168 return ERROR_SUCCESS;
1171 static UINT msi_dialog_radiogroup_control( msi_dialog *dialog, MSIRECORD *rec )
1173 static const WCHAR query[] = {
1174 'S','E','L','E','C','T',' ','*',' ',
1175 'F','R','O','M',' ','R','a','d','i','o','B','u','t','t','o','n',' ',
1176 'W','H','E','R','E',' ',
1177 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
1178 UINT r;
1179 LPCWSTR prop;
1180 msi_control *control;
1181 MSIQUERY *view = NULL;
1182 radio_button_group_descr group;
1183 MSIPACKAGE *package = dialog->package;
1184 WNDPROC oldproc;
1186 prop = MSI_RecordGetString( rec, 9 );
1188 TRACE("%p %p %s\n", dialog, rec, debugstr_w( prop ));
1190 /* Create parent group box to hold radio buttons */
1191 control = msi_dialog_add_control( dialog, rec, szButton, BS_OWNERDRAW|WS_GROUP );
1192 if( !control )
1193 return ERROR_FUNCTION_FAILED;
1195 oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1196 (LONG_PTR)MSIRadioGroup_WndProc );
1197 SetPropW(control->hwnd, szButtonData, oldproc);
1198 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
1200 if( prop )
1201 control->property = strdupW( prop );
1203 /* query the Radio Button table for all control in this group */
1204 r = MSI_OpenQuery( package->db, &view, query, prop );
1205 if( r != ERROR_SUCCESS )
1207 ERR("query failed for dialog %s radio group %s\n",
1208 debugstr_w(dialog->name), debugstr_w(prop));
1209 return ERROR_INVALID_PARAMETER;
1212 group.dialog = dialog;
1213 group.parent = control;
1214 group.attributes = MSI_RecordGetInteger( rec, 8 );
1216 r = MSI_IterateRecords( view, 0, msi_dialog_create_radiobutton, &group );
1217 msiobj_release( &view->hdr );
1219 return r;
1222 struct control_handler msi_dialog_handler[] =
1224 { szText, msi_dialog_text_control },
1225 { szPushButton, msi_dialog_button_control },
1226 { szLine, msi_dialog_line_control },
1227 { szBitmap, msi_dialog_bitmap_control },
1228 { szCheckBox, msi_dialog_checkbox_control },
1229 { szScrollableText, msi_dialog_scrolltext_control },
1230 { szComboBox, msi_dialog_combo_control },
1231 { szEdit, msi_dialog_edit_control },
1232 { szMaskedEdit, msi_dialog_maskedit_control },
1233 { szPathEdit, msi_dialog_pathedit_control },
1234 { szRadioButtonGroup, msi_dialog_radiogroup_control },
1235 { szIcon, msi_dialog_icon_control },
1238 #define NUM_CONTROL_TYPES (sizeof msi_dialog_handler/sizeof msi_dialog_handler[0])
1240 static UINT msi_dialog_create_controls( MSIRECORD *rec, LPVOID param )
1242 msi_dialog *dialog = param;
1243 LPCWSTR control_type;
1244 UINT i;
1246 /* find and call the function that can create this type of control */
1247 control_type = MSI_RecordGetString( rec, 3 );
1248 for( i=0; i<NUM_CONTROL_TYPES; i++ )
1249 if (!strcmpiW( msi_dialog_handler[i].control_type, control_type ))
1250 break;
1251 if( i != NUM_CONTROL_TYPES )
1252 msi_dialog_handler[i].func( dialog, rec );
1253 else
1254 ERR("no handler for element type %s\n", debugstr_w(control_type));
1256 return ERROR_SUCCESS;
1259 static UINT msi_dialog_fill_controls( msi_dialog *dialog )
1261 static const WCHAR query[] = {
1262 'S','E','L','E','C','T',' ','*',' ',
1263 'F','R','O','M',' ','C','o','n','t','r','o','l',' ',
1264 'W','H','E','R','E',' ',
1265 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
1266 UINT r;
1267 MSIQUERY *view = NULL;
1268 MSIPACKAGE *package = dialog->package;
1270 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
1272 /* query the Control table for all the elements of the control */
1273 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
1274 if( r != ERROR_SUCCESS )
1276 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
1277 return ERROR_INVALID_PARAMETER;
1280 r = MSI_IterateRecords( view, 0, msi_dialog_create_controls, dialog );
1281 msiobj_release( &view->hdr );
1283 return r;
1286 static UINT msi_dialog_set_control_condition( MSIRECORD *rec, LPVOID param )
1288 static const WCHAR szHide[] = { 'H','i','d','e',0 };
1289 static const WCHAR szShow[] = { 'S','h','o','w',0 };
1290 static const WCHAR szDisable[] = { 'D','i','s','a','b','l','e',0 };
1291 static const WCHAR szEnable[] = { 'E','n','a','b','l','e',0 };
1292 msi_dialog *dialog = param;
1293 msi_control *control;
1294 LPCWSTR name, action, condition;
1295 UINT r;
1297 name = MSI_RecordGetString( rec, 2 );
1298 action = MSI_RecordGetString( rec, 3 );
1299 condition = MSI_RecordGetString( rec, 4 );
1300 r = MSI_EvaluateConditionW( dialog->package, condition );
1301 control = msi_dialog_find_control( dialog, name );
1302 if( r && control )
1304 TRACE("%s control %s\n", debugstr_w(action), debugstr_w(name));
1306 /* FIXME: case sensitive? */
1307 if(!lstrcmpW(action, szHide))
1308 ShowWindow(control->hwnd, SW_HIDE);
1309 else if(!strcmpW(action, szShow))
1310 ShowWindow(control->hwnd, SW_SHOW);
1311 else if(!strcmpW(action, szDisable))
1312 EnableWindow(control->hwnd, FALSE);
1313 else if(!strcmpW(action, szEnable))
1314 EnableWindow(control->hwnd, TRUE);
1315 else
1316 FIXME("Unhandled action %s\n", debugstr_w(action));
1319 return ERROR_SUCCESS;
1322 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog )
1324 static const WCHAR query[] = {
1325 'S','E','L','E','C','T',' ','*',' ',
1326 'F','R','O','M',' ',
1327 'C','o','n','t','r','o','l','C','o','n','d','i','t','i','o','n',' ',
1328 'W','H','E','R','E',' ',
1329 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0
1331 UINT r;
1332 MSIQUERY *view = NULL;
1333 MSIPACKAGE *package = dialog->package;
1335 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
1337 /* query the Control table for all the elements of the control */
1338 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
1339 if( r != ERROR_SUCCESS )
1341 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
1342 return ERROR_INVALID_PARAMETER;
1345 r = MSI_IterateRecords( view, 0, msi_dialog_set_control_condition, dialog );
1346 msiobj_release( &view->hdr );
1348 return r;
1351 /* figure out the height of 10 point MS Sans Serif */
1352 static INT msi_dialog_get_sans_serif_height( HWND hwnd )
1354 static const WCHAR szSansSerif[] = {
1355 'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0 };
1356 LOGFONTW lf;
1357 TEXTMETRICW tm;
1358 BOOL r;
1359 LONG height = 0;
1360 HFONT hFont, hOldFont;
1361 HDC hdc;
1363 hdc = GetDC( hwnd );
1364 if (hdc)
1366 memset( &lf, 0, sizeof lf );
1367 lf.lfHeight = MulDiv(10, GetDeviceCaps(hdc, LOGPIXELSY), 72);
1368 strcpyW( lf.lfFaceName, szSansSerif );
1369 hFont = CreateFontIndirectW(&lf);
1370 if (hFont)
1372 hOldFont = SelectObject( hdc, hFont );
1373 r = GetTextMetricsW( hdc, &tm );
1374 if (r)
1375 height = tm.tmHeight;
1376 SelectObject( hdc, hOldFont );
1377 DeleteObject( hFont );
1379 ReleaseDC( hwnd, hdc );
1381 return height;
1384 /* fetch the associated record from the Dialog table */
1385 static MSIRECORD *msi_get_dialog_record( msi_dialog *dialog )
1387 static const WCHAR query[] = {
1388 'S','E','L','E','C','T',' ','*',' ',
1389 'F','R','O','M',' ','D','i','a','l','o','g',' ',
1390 'W','H','E','R','E',' ',
1391 '`','D','i','a','l','o','g','`',' ','=',' ','\'','%','s','\'',0};
1392 MSIPACKAGE *package = dialog->package;
1393 MSIRECORD *rec = NULL;
1395 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
1397 rec = MSI_QueryGetRecord( package->db, query, dialog->name );
1398 if( !rec )
1399 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
1401 return rec;
1404 static void msi_dialog_adjust_dialog_size( msi_dialog *dialog, LPSIZE sz )
1406 RECT rect;
1407 LONG style;
1409 /* turn the client size into the window rectangle */
1410 rect.left = 0;
1411 rect.top = 0;
1412 rect.right = msi_dialog_scale_unit( dialog, sz->cx );
1413 rect.bottom = msi_dialog_scale_unit( dialog, sz->cy );
1414 style = GetWindowLongPtrW( dialog->hwnd, GWL_STYLE );
1415 AdjustWindowRect( &rect, style, FALSE );
1416 sz->cx = rect.right - rect.left;
1417 sz->cy = rect.bottom - rect.top;
1420 static BOOL msi_control_set_next( msi_control *control, msi_control *next )
1422 return SetWindowPos( next->hwnd, control->hwnd, 0, 0, 0, 0,
1423 SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOREDRAW |
1424 SWP_NOREPOSITION | SWP_NOSENDCHANGING | SWP_NOSIZE );
1427 static UINT msi_dialog_set_tab_order( msi_dialog *dialog )
1429 msi_control *control, *tab_next;
1431 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
1433 tab_next = msi_dialog_find_control( dialog, control->tabnext );
1434 if( !tab_next )
1435 continue;
1436 msi_control_set_next( control, tab_next );
1439 return ERROR_SUCCESS;
1442 static void msi_dialog_set_first_control( msi_dialog* dialog, LPCWSTR name )
1444 msi_control *control;
1446 control = msi_dialog_find_control( dialog, name );
1447 if( control )
1448 dialog->hWndFocus = control->hwnd;
1449 else
1450 dialog->hWndFocus = NULL;
1453 static LRESULT msi_dialog_oncreate( HWND hwnd, LPCREATESTRUCTW cs )
1455 static const WCHAR df[] = {
1456 'D','e','f','a','u','l','t','U','I','F','o','n','t',0 };
1457 msi_dialog *dialog = (msi_dialog*) cs->lpCreateParams;
1458 MSIRECORD *rec = NULL;
1459 LPWSTR title = NULL;
1460 SIZE size;
1462 TRACE("%p %p\n", dialog, dialog->package);
1464 dialog->hwnd = hwnd;
1465 SetWindowLongPtrW( hwnd, GWLP_USERDATA, (LONG_PTR) dialog );
1467 rec = msi_get_dialog_record( dialog );
1468 if( !rec )
1470 TRACE("No record found for dialog %s\n", debugstr_w(dialog->name));
1471 return -1;
1474 dialog->scale = msi_dialog_get_sans_serif_height(dialog->hwnd);
1476 size.cx = MSI_RecordGetInteger( rec, 4 );
1477 size.cy = MSI_RecordGetInteger( rec, 5 );
1478 msi_dialog_adjust_dialog_size( dialog, &size );
1480 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
1482 dialog->default_font = msi_dup_property( dialog->package, df );
1484 title = msi_get_deformatted_field( dialog->package, rec, 7 );
1485 SetWindowTextW( hwnd, title );
1486 msi_free( title );
1488 SetWindowPos( hwnd, 0, 0, 0, size.cx, size.cy,
1489 SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW );
1492 msi_dialog_build_font_list( dialog );
1493 msi_dialog_fill_controls( dialog );
1494 msi_dialog_evaluate_control_conditions( dialog );
1495 msi_dialog_set_tab_order( dialog );
1496 msi_dialog_set_first_control( dialog, MSI_RecordGetString( rec, 8 ) );
1497 msiobj_release( &rec->hdr );
1499 return 0;
1502 static UINT msi_dialog_send_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
1504 LPWSTR event_fmt = NULL, arg_fmt = NULL;
1506 TRACE("Sending control event %s %s\n", debugstr_w(event), debugstr_w(arg));
1508 deformat_string( dialog->package, event, &event_fmt );
1509 deformat_string( dialog->package, arg, &arg_fmt );
1511 dialog->event_handler( dialog->package, event_fmt, arg_fmt, dialog );
1513 msi_free( event_fmt );
1514 msi_free( arg_fmt );
1516 return ERROR_SUCCESS;
1519 static UINT msi_dialog_set_property( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
1521 static const WCHAR szNullArg[] = { '{','}',0 };
1522 LPWSTR p, prop, arg_fmt = NULL;
1523 UINT len;
1525 len = strlenW(event);
1526 prop = msi_alloc( len*sizeof(WCHAR));
1527 strcpyW( prop, &event[1] );
1528 p = strchrW( prop, ']' );
1529 if( p && p[1] == 0 )
1531 *p = 0;
1532 if( strcmpW( szNullArg, arg ) )
1533 deformat_string( dialog->package, arg, &arg_fmt );
1534 MSI_SetPropertyW( dialog->package, prop, arg_fmt );
1535 msi_free( arg_fmt );
1537 else
1538 ERR("Badly formatted property string - what happens?\n");
1539 msi_free( prop );
1540 return ERROR_SUCCESS;
1543 static UINT msi_dialog_control_event( MSIRECORD *rec, LPVOID param )
1545 msi_dialog *dialog = param;
1546 LPCWSTR condition, event, arg;
1547 UINT r;
1549 condition = MSI_RecordGetString( rec, 5 );
1550 r = MSI_EvaluateConditionW( dialog->package, condition );
1551 if( r )
1553 event = MSI_RecordGetString( rec, 3 );
1554 arg = MSI_RecordGetString( rec, 4 );
1555 if( event[0] == '[' )
1556 msi_dialog_set_property( dialog, event, arg );
1557 else
1558 msi_dialog_send_event( dialog, event, arg );
1561 return ERROR_SUCCESS;
1564 static UINT msi_dialog_button_handler( msi_dialog *dialog,
1565 msi_control *control, WPARAM param )
1567 static const WCHAR query[] = {
1568 'S','E','L','E','C','T',' ','*',' ',
1569 'F','R','O','M',' ','C','o','n','t','r','o','l','E','v','e','n','t',' ',
1570 'W','H','E','R','E',' ',
1571 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
1572 'A','N','D',' ',
1573 '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',' ',
1574 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','i','n','g','`',0
1576 MSIQUERY *view = NULL;
1577 UINT r;
1579 if( HIWORD(param) != BN_CLICKED )
1580 return ERROR_SUCCESS;
1582 r = MSI_OpenQuery( dialog->package->db, &view, query,
1583 dialog->name, control->name );
1584 if( r != ERROR_SUCCESS )
1586 ERR("query failed\n");
1587 return 0;
1590 r = MSI_IterateRecords( view, 0, msi_dialog_control_event, dialog );
1591 msiobj_release( &view->hdr );
1593 return r;
1596 static UINT msi_dialog_get_checkbox_state( msi_dialog *dialog,
1597 msi_control *control )
1599 WCHAR state[2] = { 0 };
1600 DWORD sz = 2;
1602 MSI_GetPropertyW( dialog->package, control->property, state, &sz );
1603 return state[0] ? 1 : 0;
1606 static void msi_dialog_set_checkbox_state( msi_dialog *dialog,
1607 msi_control *control, UINT state )
1609 static const WCHAR szState[] = { '1', 0 };
1610 LPCWSTR val;
1612 /* if uncheck then the property is set to NULL */
1613 if (!state)
1615 MSI_SetPropertyW( dialog->package, control->property, NULL );
1616 return;
1619 /* check for a custom state */
1620 if (control->value && control->value[0])
1621 val = control->value;
1622 else
1623 val = szState;
1625 MSI_SetPropertyW( dialog->package, control->property, val );
1628 static void msi_dialog_checkbox_sync_state( msi_dialog *dialog,
1629 msi_control *control )
1631 UINT state;
1633 state = msi_dialog_get_checkbox_state( dialog, control );
1634 SendMessageW( control->hwnd, BM_SETCHECK,
1635 state ? BST_CHECKED : BST_UNCHECKED, 0 );
1638 static UINT msi_dialog_checkbox_handler( msi_dialog *dialog,
1639 msi_control *control, WPARAM param )
1641 UINT state;
1643 if( HIWORD(param) != BN_CLICKED )
1644 return ERROR_SUCCESS;
1646 TRACE("clicked checkbox %s, set %s\n", debugstr_w(control->name),
1647 debugstr_w(control->property));
1649 state = msi_dialog_get_checkbox_state( dialog, control );
1650 state = state ? 0 : 1;
1651 msi_dialog_set_checkbox_state( dialog, control, state );
1652 msi_dialog_checkbox_sync_state( dialog, control );
1654 return msi_dialog_button_handler( dialog, control, param );
1657 static UINT msi_dialog_edit_handler( msi_dialog *dialog,
1658 msi_control *control, WPARAM param )
1660 UINT sz, r;
1661 LPWSTR buf;
1663 if( HIWORD(param) != EN_CHANGE )
1664 return ERROR_SUCCESS;
1666 TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
1667 debugstr_w(control->property));
1669 sz = 0x20;
1670 buf = msi_alloc( sz*sizeof(WCHAR) );
1671 while( buf )
1673 r = GetWindowTextW( control->hwnd, buf, sz );
1674 if( r < (sz-1) )
1675 break;
1676 sz *= 2;
1677 buf = msi_realloc( buf, sz*sizeof(WCHAR) );
1680 MSI_SetPropertyW( dialog->package, control->property, buf );
1682 msi_free( buf );
1684 return ERROR_SUCCESS;
1687 static UINT msi_dialog_radiogroup_handler( msi_dialog *dialog,
1688 msi_control *control, WPARAM param )
1690 if( HIWORD(param) != BN_CLICKED )
1691 return ERROR_SUCCESS;
1693 TRACE("clicked radio button %s, set %s\n", debugstr_w(control->name),
1694 debugstr_w(control->property));
1696 MSI_SetPropertyW( dialog->package, control->property, control->name );
1698 return msi_dialog_button_handler( dialog, control, param );
1701 static LRESULT msi_dialog_oncommand( msi_dialog *dialog, WPARAM param, HWND hwnd )
1703 msi_control *control;
1705 TRACE("%p %p %08x\n", dialog, hwnd, param);
1707 control = msi_dialog_find_control_by_hwnd( dialog, hwnd );
1708 if( control )
1710 if( control->handler )
1712 control->handler( dialog, control, param );
1713 msi_dialog_evaluate_control_conditions( dialog );
1716 else
1717 ERR("button click from nowhere\n");
1718 return 0;
1721 static void msi_dialog_setfocus( msi_dialog *dialog )
1723 HWND hwnd = dialog->hWndFocus;
1725 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, TRUE);
1726 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, FALSE);
1727 SetFocus( hwnd );
1728 dialog->hWndFocus = hwnd;
1731 static LRESULT WINAPI MSIDialog_WndProc( HWND hwnd, UINT msg,
1732 WPARAM wParam, LPARAM lParam )
1734 msi_dialog *dialog = (LPVOID) GetWindowLongPtrW( hwnd, GWLP_USERDATA );
1736 TRACE("0x%04x\n", msg);
1738 switch (msg)
1740 case WM_CREATE:
1741 return msi_dialog_oncreate( hwnd, (LPCREATESTRUCTW)lParam );
1743 case WM_COMMAND:
1744 return msi_dialog_oncommand( dialog, wParam, (HWND)lParam );
1746 case WM_ACTIVATE:
1747 if( LOWORD(wParam) == WA_INACTIVE )
1748 dialog->hWndFocus = GetFocus();
1749 else
1750 msi_dialog_setfocus( dialog );
1751 return 0;
1753 case WM_SETFOCUS:
1754 msi_dialog_setfocus( dialog );
1755 return 0;
1757 /* bounce back to our subclassed static control */
1758 case WM_CTLCOLORSTATIC:
1759 return SendMessageW( (HWND) lParam, WM_CTLCOLORSTATIC, wParam, lParam );
1761 case WM_DESTROY:
1762 dialog->hwnd = NULL;
1763 return 0;
1765 return DefWindowProcW(hwnd, msg, wParam, lParam);
1768 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1770 WNDPROC oldproc = (WNDPROC) GetPropW(hWnd, szButtonData);
1772 TRACE("hWnd %p msg %04x wParam 0x%08x lParam 0x%08lx\n", hWnd, msg, wParam, lParam);
1774 if (msg == WM_COMMAND) /* Forward notifications to dialog */
1775 SendMessageW(GetParent(hWnd), msg, wParam, lParam);
1777 return CallWindowProcW(oldproc, hWnd, msg, wParam, lParam);
1780 static LRESULT WINAPI MSIHiddenWindowProc( HWND hwnd, UINT msg,
1781 WPARAM wParam, LPARAM lParam )
1783 msi_dialog *dialog = (msi_dialog*) lParam;
1785 TRACE("%d %p\n", msg, dialog);
1787 switch (msg)
1789 case WM_MSI_DIALOG_CREATE:
1790 return msi_dialog_run_message_loop( dialog );
1791 case WM_MSI_DIALOG_DESTROY:
1792 msi_dialog_destroy( dialog );
1793 return 0;
1795 return DefWindowProcW( hwnd, msg, wParam, lParam );
1798 /* functions that interface to other modules within MSI */
1800 msi_dialog *msi_dialog_create( MSIPACKAGE* package, LPCWSTR szDialogName,
1801 msi_dialog_event_handler event_handler )
1803 MSIRECORD *rec = NULL;
1804 msi_dialog *dialog;
1806 TRACE("%p %s\n", package, debugstr_w(szDialogName));
1808 /* allocate the structure for the dialog to use */
1809 dialog = msi_alloc_zero( sizeof *dialog + sizeof(WCHAR)*strlenW(szDialogName) );
1810 if( !dialog )
1811 return NULL;
1812 strcpyW( dialog->name, szDialogName );
1813 msiobj_addref( &package->hdr );
1814 dialog->package = package;
1815 dialog->event_handler = event_handler;
1816 dialog->finished = 0;
1817 list_init( &dialog->controls );
1819 /* verify that the dialog exists */
1820 rec = msi_get_dialog_record( dialog );
1821 if( !rec )
1823 msiobj_release( &package->hdr );
1824 msi_free( dialog );
1825 return NULL;
1827 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
1828 msiobj_release( &rec->hdr );
1830 return dialog;
1833 static void msi_process_pending_messages( HWND hdlg )
1835 MSG msg;
1837 while( PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ) )
1839 if( hdlg && IsDialogMessageW( hdlg, &msg ))
1840 continue;
1841 TranslateMessage( &msg );
1842 DispatchMessageW( &msg );
1846 void msi_dialog_end_dialog( msi_dialog *dialog )
1848 TRACE("%p\n", dialog);
1849 dialog->finished = 1;
1850 PostMessageW(dialog->hwnd, WM_NULL, 0, 0);
1853 void msi_dialog_check_messages( HANDLE handle )
1855 DWORD r;
1857 /* in threads other than the UI thread, block */
1858 if( uiThreadId != GetCurrentThreadId() )
1860 if( handle )
1861 WaitForSingleObject( handle, INFINITE );
1862 return;
1865 /* there's two choices for the UI thread */
1866 while (1)
1868 msi_process_pending_messages( NULL );
1870 if( !handle )
1871 break;
1874 * block here until somebody creates a new dialog or
1875 * the handle we're waiting on becomes ready
1877 r = MsgWaitForMultipleObjects( 1, &handle, 0, INFINITE, QS_ALLINPUT );
1878 if( r == WAIT_OBJECT_0 )
1879 break;
1883 UINT msi_dialog_run_message_loop( msi_dialog *dialog )
1885 HWND hwnd;
1887 if( !(dialog->attributes & msidbDialogAttributesVisible) )
1888 return ERROR_SUCCESS;
1890 if( uiThreadId != GetCurrentThreadId() )
1891 return SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_CREATE, 0, (LPARAM) dialog );
1893 /* create the dialog window, don't show it yet */
1894 hwnd = CreateWindowW( szMsiDialogClass, dialog->name, WS_OVERLAPPEDWINDOW,
1895 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
1896 NULL, NULL, NULL, dialog );
1897 if( !hwnd )
1899 ERR("Failed to create dialog %s\n", debugstr_w( dialog->name ));
1900 return ERROR_FUNCTION_FAILED;
1903 ShowWindow( hwnd, SW_SHOW );
1904 UpdateWindow( hwnd );
1906 if( dialog->attributes & msidbDialogAttributesModal )
1908 while( !dialog->finished )
1910 MsgWaitForMultipleObjects( 0, NULL, 0, INFINITE, QS_ALLEVENTS );
1911 msi_process_pending_messages( dialog->hwnd );
1914 else
1915 return ERROR_IO_PENDING;
1917 return ERROR_SUCCESS;
1920 void msi_dialog_do_preview( msi_dialog *dialog )
1922 TRACE("\n");
1923 dialog->attributes |= msidbDialogAttributesVisible;
1924 dialog->attributes &= ~msidbDialogAttributesModal;
1925 msi_dialog_run_message_loop( dialog );
1928 void msi_dialog_destroy( msi_dialog *dialog )
1930 if( uiThreadId != GetCurrentThreadId() )
1932 SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_DESTROY, 0, (LPARAM) dialog );
1933 return;
1936 if( dialog->hwnd )
1937 ShowWindow( dialog->hwnd, SW_HIDE );
1939 if( dialog->hwnd )
1940 DestroyWindow( dialog->hwnd );
1942 /* destroy the list of controls */
1943 while( !list_empty( &dialog->controls ) )
1945 msi_control *t = LIST_ENTRY( list_head( &dialog->controls ),
1946 msi_control, entry );
1947 list_remove( &t->entry );
1948 /* leave dialog->hwnd - destroying parent destroys child windows */
1949 msi_free( t->property );
1950 msi_free( t->value );
1951 if( t->hBitmap )
1952 DeleteObject( t->hBitmap );
1953 if( t->hIcon )
1954 DestroyIcon( t->hIcon );
1955 msi_free( t->tabnext );
1956 msi_free( t );
1957 if (t->hDll)
1958 FreeLibrary( t->hDll );
1961 /* destroy the list of fonts */
1962 while( dialog->font_list )
1964 msi_font *t = dialog->font_list;
1965 dialog->font_list = t->next;
1966 DeleteObject( t->hfont );
1967 msi_free( t );
1969 msi_free( dialog->default_font );
1971 msiobj_release( &dialog->package->hdr );
1972 dialog->package = NULL;
1973 msi_free( dialog );
1976 BOOL msi_dialog_register_class( void )
1978 WNDCLASSW cls;
1980 ZeroMemory( &cls, sizeof cls );
1981 cls.lpfnWndProc = MSIDialog_WndProc;
1982 cls.hInstance = NULL;
1983 cls.hIcon = LoadIconW(0, (LPWSTR)IDI_APPLICATION);
1984 cls.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
1985 cls.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
1986 cls.lpszMenuName = NULL;
1987 cls.lpszClassName = szMsiDialogClass;
1989 if( !RegisterClassW( &cls ) )
1990 return FALSE;
1992 cls.lpfnWndProc = MSIHiddenWindowProc;
1993 cls.lpszClassName = szMsiHiddenWindow;
1995 if( !RegisterClassW( &cls ) )
1996 return FALSE;
1998 uiThreadId = GetCurrentThreadId();
2000 hMsiHiddenWindow = CreateWindowW( szMsiHiddenWindow, NULL, WS_OVERLAPPED,
2001 0, 0, 100, 100, NULL, NULL, NULL, NULL );
2002 if( !hMsiHiddenWindow )
2003 return FALSE;
2005 return TRUE;
2008 void msi_dialog_unregister_class( void )
2010 DestroyWindow( hMsiHiddenWindow );
2011 UnregisterClassW( szMsiDialogClass, NULL );
2012 uiThreadId = 0;