msi: Make the MSI SelectionTree control do something useful.
[wine/multimedia.git] / dlls / msi / dialog.c
blob76d4d064d60bf9ef0c5532aad14137af9c559eaf
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
22 #define NONAMELESSUNION
23 #define NONAMELESSSTRUCT
25 #include <stdarg.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winuser.h"
30 #include "winnls.h"
31 #include "wingdi.h"
32 #include "msi.h"
33 #include "msipriv.h"
34 #include "msidefs.h"
35 #include "ocidl.h"
36 #include "olectl.h"
37 #include "richedit.h"
38 #include "commctrl.h"
40 #include "wine/debug.h"
41 #include "wine/unicode.h"
43 #include "action.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(msi);
48 extern HINSTANCE msi_hInstance;
50 struct msi_control_tag;
51 typedef struct msi_control_tag msi_control;
52 typedef UINT (*msi_handler)( msi_dialog *, msi_control *, WPARAM );
54 struct msi_control_tag
56 struct list entry;
57 HWND hwnd;
58 msi_handler handler;
59 LPWSTR property;
60 LPWSTR value;
61 HBITMAP hBitmap;
62 HICON hIcon;
63 LPWSTR tabnext;
64 HMODULE hDll;
65 WCHAR name[1];
68 typedef struct msi_font_tag
70 struct msi_font_tag *next;
71 HFONT hfont;
72 WCHAR name[1];
73 } msi_font;
75 struct msi_dialog_tag
77 MSIPACKAGE *package;
78 msi_dialog_event_handler event_handler;
79 BOOL finished;
80 INT scale;
81 DWORD attributes;
82 HWND hwnd;
83 LPWSTR default_font;
84 msi_font *font_list;
85 struct list controls;
86 HWND hWndFocus;
87 LPWSTR control_default;
88 LPWSTR control_cancel;
89 WCHAR name[1];
92 typedef UINT (*msi_dialog_control_func)( msi_dialog *dialog, MSIRECORD *rec );
93 struct control_handler
95 LPCWSTR control_type;
96 msi_dialog_control_func func;
99 typedef struct
101 msi_dialog* dialog;
102 msi_control *parent;
103 DWORD attributes;
104 } radio_button_group_descr;
106 const WCHAR szMsiDialogClass[] = {
107 'M','s','i','D','i','a','l','o','g','C','l','o','s','e','C','l','a','s','s',0
109 const WCHAR szMsiHiddenWindow[] = {
110 'M','s','i','H','i','d','d','e','n','W','i','n','d','o','w',0 };
111 static const WCHAR szStatic[] = { 'S','t','a','t','i','c',0 };
112 static const WCHAR szButton[] = { 'B','U','T','T','O','N', 0 };
113 static const WCHAR szButtonData[] = { 'M','S','I','D','A','T','A',0 };
114 static const WCHAR szText[] = { 'T','e','x','t',0 };
115 static const WCHAR szPushButton[] = { 'P','u','s','h','B','u','t','t','o','n',0 };
116 static const WCHAR szLine[] = { 'L','i','n','e',0 };
117 static const WCHAR szBitmap[] = { 'B','i','t','m','a','p',0 };
118 static const WCHAR szCheckBox[] = { 'C','h','e','c','k','B','o','x',0 };
119 static const WCHAR szScrollableText[] = {
120 'S','c','r','o','l','l','a','b','l','e','T','e','x','t',0 };
121 static const WCHAR szComboBox[] = { 'C','o','m','b','o','B','o','x',0 };
122 static const WCHAR szEdit[] = { 'E','d','i','t',0 };
123 static const WCHAR szMaskedEdit[] = { 'M','a','s','k','e','d','E','d','i','t',0 };
124 static const WCHAR szPathEdit[] = { 'P','a','t','h','E','d','i','t',0 };
125 static const WCHAR szProgressBar[] = {
126 'P','r','o','g','r','e','s','s','B','a','r',0 };
127 static const WCHAR szRadioButtonGroup[] = {
128 'R','a','d','i','o','B','u','t','t','o','n','G','r','o','u','p',0 };
129 static const WCHAR szIcon[] = { 'I','c','o','n',0 };
130 static const WCHAR szSelectionTree[] = {
131 'S','e','l','e','c','t','i','o','n','T','r','e','e',0 };
133 static UINT msi_dialog_checkbox_handler( msi_dialog *, msi_control *, WPARAM );
134 static void msi_dialog_checkbox_sync_state( msi_dialog *, msi_control * );
135 static UINT msi_dialog_button_handler( msi_dialog *, msi_control *, WPARAM );
136 static UINT msi_dialog_edit_handler( msi_dialog *, msi_control *, WPARAM );
137 static UINT msi_dialog_radiogroup_handler( msi_dialog *, msi_control *, WPARAM param );
138 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog );
139 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
142 /* dialog sequencing */
144 #define WM_MSI_DIALOG_CREATE (WM_USER+0x100)
145 #define WM_MSI_DIALOG_DESTROY (WM_USER+0x101)
147 static DWORD uiThreadId;
148 static HWND hMsiHiddenWindow;
150 static INT msi_dialog_scale_unit( msi_dialog *dialog, INT val )
152 return (dialog->scale * val + 5) / 10;
155 static msi_control *msi_dialog_find_control( msi_dialog *dialog, LPCWSTR name )
157 msi_control *control;
159 if( !name )
160 return NULL;
161 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
162 if( !strcmpW( control->name, name ) ) /* FIXME: case sensitive? */
163 return control;
164 return NULL;
167 static msi_control *msi_dialog_find_control_by_hwnd( msi_dialog *dialog, HWND hwnd )
169 msi_control *control;
171 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
172 if( hwnd == control->hwnd )
173 return control;
174 return NULL;
177 static LPWSTR msi_get_deformatted_field( MSIPACKAGE *package, MSIRECORD *rec, int field )
179 LPCWSTR str = MSI_RecordGetString( rec, field );
180 LPWSTR ret = NULL;
182 if (str)
183 deformat_string( package, str, &ret );
184 return ret;
188 * msi_dialog_get_style
190 * Extract the {\style} string from the front of the text to display and
191 * update the pointer.
193 static LPWSTR msi_dialog_get_style( LPCWSTR p, LPCWSTR *rest )
195 LPWSTR ret = NULL;
196 LPCWSTR q, i;
197 DWORD len;
199 *rest = p;
200 if( !p )
201 return ret;
202 if( *p++ != '{' )
203 return ret;
204 q = strchrW( p, '}' );
205 if( !q )
206 return ret;
207 if( *p == '\\' || *p == '&' )
208 p++;
210 /* little bit of sanity checking to stop us getting confused with RTF */
211 for( i=p; i<q; i++ )
212 if( *i == '}' || *i == '\\' )
213 return ret;
215 *rest = ++q;
216 len = q - p;
218 ret = msi_alloc( len*sizeof(WCHAR) );
219 if( !ret )
220 return ret;
221 memcpy( ret, p, len*sizeof(WCHAR) );
222 ret[len-1] = 0;
223 return ret;
226 static UINT msi_dialog_add_font( MSIRECORD *rec, LPVOID param )
228 msi_dialog *dialog = param;
229 msi_font *font;
230 LPCWSTR face, name;
231 LOGFONTW lf;
232 INT style;
233 HDC hdc;
235 /* create a font and add it to the list */
236 name = MSI_RecordGetString( rec, 1 );
237 font = msi_alloc( sizeof *font + strlenW( name )*sizeof (WCHAR) );
238 strcpyW( font->name, name );
239 font->next = dialog->font_list;
240 dialog->font_list = font;
242 memset( &lf, 0, sizeof lf );
243 face = MSI_RecordGetString( rec, 2 );
244 lf.lfHeight = MSI_RecordGetInteger( rec, 3 );
245 style = MSI_RecordGetInteger( rec, 5 );
246 if( style & msidbTextStyleStyleBitsBold )
247 lf.lfWeight = FW_BOLD;
248 if( style & msidbTextStyleStyleBitsItalic )
249 lf.lfItalic = TRUE;
250 if( style & msidbTextStyleStyleBitsUnderline )
251 lf.lfUnderline = TRUE;
252 if( style & msidbTextStyleStyleBitsStrike )
253 lf.lfStrikeOut = TRUE;
254 lstrcpynW( lf.lfFaceName, face, LF_FACESIZE );
256 /* adjust the height */
257 hdc = GetDC( dialog->hwnd );
258 if (hdc)
260 lf.lfHeight = -MulDiv(lf.lfHeight, GetDeviceCaps(hdc, LOGPIXELSY), 72);
261 ReleaseDC( dialog->hwnd, hdc );
264 font->hfont = CreateFontIndirectW( &lf );
266 TRACE("Adding font style %s\n", debugstr_w(font->name) );
268 return ERROR_SUCCESS;
271 static msi_font *msi_dialog_find_font( msi_dialog *dialog, LPCWSTR name )
273 msi_font *font;
275 for( font = dialog->font_list; font; font = font->next )
276 if( !strcmpW( font->name, name ) ) /* FIXME: case sensitive? */
277 break;
279 return font;
282 static UINT msi_dialog_set_font( msi_dialog *dialog, HWND hwnd, LPCWSTR name )
284 msi_font *font;
286 font = msi_dialog_find_font( dialog, name );
287 if( font )
288 SendMessageW( hwnd, WM_SETFONT, (WPARAM) font->hfont, TRUE );
289 else
290 ERR("No font entry for %s\n", debugstr_w(name));
291 return ERROR_SUCCESS;
294 static UINT msi_dialog_build_font_list( msi_dialog *dialog )
296 static const WCHAR query[] = {
297 'S','E','L','E','C','T',' ','*',' ',
298 'F','R','O','M',' ','`','T','e','x','t','S','t','y','l','e','`',' ',0
300 UINT r;
301 MSIQUERY *view = NULL;
303 TRACE("dialog %p\n", dialog );
305 r = MSI_OpenQuery( dialog->package->db, &view, query );
306 if( r != ERROR_SUCCESS )
307 return r;
309 r = MSI_IterateRecords( view, NULL, msi_dialog_add_font, dialog );
310 msiobj_release( &view->hdr );
312 return r;
315 static msi_control *msi_dialog_create_window( msi_dialog *dialog,
316 MSIRECORD *rec, LPCWSTR szCls, LPCWSTR name, LPCWSTR text,
317 DWORD style, HWND parent )
319 DWORD x, y, width, height;
320 LPWSTR font = NULL, title_font = NULL;
321 LPCWSTR title = NULL;
322 msi_control *control;
324 style |= WS_CHILD;
326 control = msi_alloc( sizeof *control + strlenW(name)*sizeof(WCHAR) );
327 strcpyW( control->name, name );
328 list_add_head( &dialog->controls, &control->entry );
329 control->handler = NULL;
330 control->property = NULL;
331 control->value = NULL;
332 control->hBitmap = NULL;
333 control->hIcon = NULL;
334 control->hDll = NULL;
335 control->tabnext = strdupW( MSI_RecordGetString( rec, 11) );
337 x = MSI_RecordGetInteger( rec, 4 );
338 y = MSI_RecordGetInteger( rec, 5 );
339 width = MSI_RecordGetInteger( rec, 6 );
340 height = MSI_RecordGetInteger( rec, 7 );
342 x = msi_dialog_scale_unit( dialog, x );
343 y = msi_dialog_scale_unit( dialog, y );
344 width = msi_dialog_scale_unit( dialog, width );
345 height = msi_dialog_scale_unit( dialog, height );
347 if( text )
349 deformat_string( dialog->package, text, &title_font );
350 font = msi_dialog_get_style( title_font, &title );
353 control->hwnd = CreateWindowW( szCls, title, style,
354 x, y, width, height, parent, NULL, NULL, NULL );
356 TRACE("Dialog %s control %s hwnd %p\n",
357 debugstr_w(dialog->name), debugstr_w(text), control->hwnd );
359 msi_dialog_set_font( dialog, control->hwnd,
360 font ? font : dialog->default_font );
362 msi_free( title_font );
363 msi_free( font );
365 return control;
368 static MSIRECORD *msi_get_binary_record( MSIDATABASE *db, LPCWSTR name )
370 static const WCHAR query[] = {
371 's','e','l','e','c','t',' ','*',' ',
372 'f','r','o','m',' ','B','i','n','a','r','y',' ',
373 'w','h','e','r','e',' ',
374 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0
377 return MSI_QueryGetRecord( db, query, name );
380 static LPWSTR msi_create_tmp_path(void)
382 WCHAR tmp[MAX_PATH];
383 LPWSTR path = NULL;
384 static const WCHAR prefix[] = { 'm','s','i',0 };
385 DWORD len, r;
387 r = GetTempPathW( MAX_PATH, tmp );
388 if( !r )
389 return path;
390 len = lstrlenW( tmp ) + 20;
391 path = msi_alloc( len * sizeof (WCHAR) );
392 if( path )
394 r = GetTempFileNameW( tmp, prefix, 0, path );
395 if (!r)
397 msi_free( path );
398 path = NULL;
401 return path;
405 static HANDLE msi_load_image( MSIDATABASE *db, LPCWSTR name, UINT type,
406 UINT cx, UINT cy, UINT flags )
408 MSIRECORD *rec = NULL;
409 HANDLE himage = NULL;
410 LPWSTR tmp;
411 UINT r;
413 TRACE("%p %s %u %u %08x\n", db, debugstr_w(name), cx, cy, flags);
415 tmp = msi_create_tmp_path();
416 if( !tmp )
417 return himage;
419 rec = msi_get_binary_record( db, name );
420 if( rec )
422 r = MSI_RecordStreamToFile( rec, 2, tmp );
423 if( r == ERROR_SUCCESS )
425 himage = LoadImageW( 0, tmp, type, cx, cy, flags );
426 DeleteFileW( tmp );
428 msiobj_release( &rec->hdr );
431 msi_free( tmp );
432 return himage;
435 static HICON msi_load_icon( MSIDATABASE *db, LPCWSTR text, UINT attributes )
437 DWORD cx = 0, cy = 0, flags;
439 flags = LR_LOADFROMFILE | LR_DEFAULTSIZE;
440 if( attributes & msidbControlAttributesFixedSize )
442 flags &= ~LR_DEFAULTSIZE;
443 if( attributes & msidbControlAttributesIconSize16 )
445 cx += 16;
446 cy += 16;
448 if( attributes & msidbControlAttributesIconSize32 )
450 cx += 32;
451 cy += 32;
453 /* msidbControlAttributesIconSize48 handled by above logic */
455 return msi_load_image( db, text, IMAGE_ICON, cx, cy, flags );
459 /* called from the Control Event subscription code */
460 void msi_dialog_handle_event( msi_dialog* dialog, LPCWSTR control,
461 LPCWSTR attribute, MSIRECORD *rec )
463 msi_control* ctrl;
464 LPCWSTR font_text, text = NULL;
465 LPWSTR font;
467 ctrl = msi_dialog_find_control( dialog, control );
468 if (!ctrl)
469 return;
470 if( lstrcmpW(attribute, szText) )
472 ERR("Attribute %s\n", debugstr_w(attribute));
473 return;
475 font_text = MSI_RecordGetString( rec , 1 );
476 font = msi_dialog_get_style( font_text, &text );
477 SetWindowTextW( ctrl->hwnd, text );
478 msi_free( font );
479 msi_dialog_check_messages( NULL );
482 static void msi_dialog_map_events(msi_dialog* dialog, LPCWSTR control)
484 static WCHAR Query[] = {
485 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
486 '`','E','v','e','n','t','M','a','p','p','i','n','g','`',' ',
487 'W','H','E','R','E',' ',
488 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
489 'A','N','D',' ',
490 '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',0
492 MSIRECORD *row;
493 LPCWSTR event, attribute;
495 row = MSI_QueryGetRecord( dialog->package->db, Query, dialog->name, control );
496 if (!row)
497 return;
499 event = MSI_RecordGetString( row, 3 );
500 attribute = MSI_RecordGetString( row, 4 );
501 ControlEvent_SubscribeToEvent( dialog->package, event, control, attribute );
502 msiobj_release( &row->hdr );
505 /* everything except radio buttons */
506 static msi_control *msi_dialog_add_control( msi_dialog *dialog,
507 MSIRECORD *rec, LPCWSTR szCls, DWORD style )
509 DWORD attributes;
510 LPCWSTR text, name;
512 name = MSI_RecordGetString( rec, 2 );
513 attributes = MSI_RecordGetInteger( rec, 8 );
514 text = MSI_RecordGetString( rec, 10 );
515 if( attributes & msidbControlAttributesVisible )
516 style |= WS_VISIBLE;
517 if( ~attributes & msidbControlAttributesEnabled )
518 style |= WS_DISABLED;
520 msi_dialog_map_events(dialog, name);
522 return msi_dialog_create_window( dialog, rec, szCls, name, text,
523 style, dialog->hwnd );
526 struct msi_text_info
528 WNDPROC oldproc;
529 DWORD attributes;
533 * we don't erase our own background,
534 * so we have to make sure that the parent window redraws first
536 static void msi_text_on_settext( HWND hWnd )
538 HWND hParent;
539 RECT rc;
541 hParent = GetParent( hWnd );
542 GetWindowRect( hWnd, &rc );
543 MapWindowPoints( NULL, hParent, (LPPOINT) &rc, 2 );
544 InvalidateRect( hParent, &rc, TRUE );
547 static LRESULT WINAPI
548 MSIText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
550 struct msi_text_info *info;
551 LRESULT r = 0;
553 TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
555 info = GetPropW(hWnd, szButtonData);
557 if( msg == WM_CTLCOLORSTATIC &&
558 ( info->attributes & msidbControlAttributesTransparent ) )
560 SetBkMode( (HDC)wParam, TRANSPARENT );
561 return (LRESULT) GetStockObject(NULL_BRUSH);
564 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
566 switch( msg )
568 case WM_SETTEXT:
569 msi_text_on_settext( hWnd );
570 break;
571 case WM_NCDESTROY:
572 msi_free( info );
573 RemovePropW( hWnd, szButtonData );
574 break;
577 return r;
580 static UINT msi_dialog_text_control( msi_dialog *dialog, MSIRECORD *rec )
582 msi_control *control;
583 struct msi_text_info *info;
585 TRACE("%p %p\n", dialog, rec);
587 control = msi_dialog_add_control( dialog, rec, szStatic, SS_LEFT | WS_GROUP );
588 if( !control )
589 return ERROR_FUNCTION_FAILED;
591 info = msi_alloc( sizeof *info );
592 if( !info )
593 return ERROR_SUCCESS;
595 info->attributes = MSI_RecordGetInteger( rec, 8 );
596 if( info->attributes & msidbControlAttributesTransparent )
597 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT );
599 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
600 (LONG_PTR)MSIText_WndProc );
601 SetPropW( control->hwnd, szButtonData, info );
603 return ERROR_SUCCESS;
606 static UINT msi_dialog_button_control( msi_dialog *dialog, MSIRECORD *rec )
608 msi_control *control;
609 UINT attributes, style;
610 LPWSTR text;
612 TRACE("%p %p\n", dialog, rec);
614 style = WS_TABSTOP;
615 attributes = MSI_RecordGetInteger( rec, 8 );
616 if( attributes & msidbControlAttributesIcon )
617 style |= BS_ICON;
619 control = msi_dialog_add_control( dialog, rec, szButton, style );
620 if( !control )
621 return ERROR_FUNCTION_FAILED;
623 control->handler = msi_dialog_button_handler;
625 /* set the icon */
626 text = msi_get_deformatted_field( dialog->package, rec, 10 );
627 control->hIcon = msi_load_icon( dialog->package->db, text, attributes );
628 if( attributes & msidbControlAttributesIcon )
629 SendMessageW( control->hwnd, BM_SETIMAGE, IMAGE_ICON, (LPARAM) control->hIcon );
630 msi_free( text );
632 return ERROR_SUCCESS;
635 static LPWSTR msi_get_checkbox_value( msi_dialog *dialog, LPCWSTR prop )
637 static const WCHAR query[] = {
638 'S','E','L','E','C','T',' ','*',' ',
639 'F','R','O','M',' ','`','C','h','e','c','k','B','o','x',' ','`',
640 'W','H','E','R','E',' ',
641 '`','P','r','o','p','e','r','t','y','`',' ','=',' ',
642 '\'','%','s','\'',0
644 MSIRECORD *rec = NULL;
645 LPWSTR ret = NULL;
647 /* find if there is a value associated with the checkbox */
648 rec = MSI_QueryGetRecord( dialog->package->db, query, prop );
649 if (!rec)
650 return ret;
652 ret = msi_get_deformatted_field( dialog->package, rec, 2 );
653 if( ret && !ret[0] )
655 msi_free( ret );
656 ret = NULL;
658 msiobj_release( &rec->hdr );
659 if (ret)
660 return ret;
662 ret = msi_dup_property( dialog->package, prop );
663 if( ret && !ret[0] )
665 msi_free( ret );
666 ret = NULL;
669 return ret;
672 static UINT msi_dialog_checkbox_control( msi_dialog *dialog, MSIRECORD *rec )
674 msi_control *control;
675 LPCWSTR prop;
677 TRACE("%p %p\n", dialog, rec);
679 control = msi_dialog_add_control( dialog, rec, szButton,
680 BS_CHECKBOX | BS_MULTILINE | WS_TABSTOP );
681 control->handler = msi_dialog_checkbox_handler;
682 prop = MSI_RecordGetString( rec, 9 );
683 if( prop )
685 control->property = strdupW( prop );
686 control->value = msi_get_checkbox_value( dialog, prop );
687 TRACE("control %s value %s\n", debugstr_w(control->property),
688 debugstr_w(control->value));
690 msi_dialog_checkbox_sync_state( dialog, control );
692 return ERROR_SUCCESS;
695 static UINT msi_dialog_line_control( msi_dialog *dialog, MSIRECORD *rec )
697 TRACE("%p %p\n", dialog, rec);
699 msi_dialog_add_control( dialog, rec, szStatic, SS_ETCHEDHORZ | SS_SUNKEN );
700 return ERROR_SUCCESS;
703 /******************** Scroll Text ********************************************/
705 struct msi_scrolltext_info
707 msi_dialog *dialog;
708 msi_control *control;
709 WNDPROC oldproc;
712 static LRESULT WINAPI
713 MSIScrollText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
715 struct msi_scrolltext_info *info;
716 HRESULT r;
718 TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
720 info = GetPropW( hWnd, szButtonData );
722 r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
724 switch( msg )
726 case WM_NCDESTROY:
727 msi_free( info );
728 RemovePropW( hWnd, szButtonData );
729 break;
730 case WM_PAINT:
731 /* native MSI sets a wait cursor here */
732 msi_dialog_button_handler( info->dialog, info->control, BN_CLICKED );
733 break;
735 return r;
738 struct msi_streamin_info
740 LPSTR string;
741 DWORD offset;
742 DWORD length;
745 static DWORD CALLBACK
746 msi_richedit_stream_in( DWORD_PTR arg, LPBYTE buffer, LONG count, LONG *pcb )
748 struct msi_streamin_info *info = (struct msi_streamin_info*) arg;
750 if( (count + info->offset) > info->length )
751 count = info->length - info->offset;
752 memcpy( buffer, &info->string[ info->offset ], count );
753 *pcb = count;
754 info->offset += count;
756 TRACE("%ld/%ld\n", info->offset, info->length);
758 return 0;
761 static void msi_scrolltext_add_text( msi_control *control, LPCWSTR text )
763 struct msi_streamin_info info;
764 EDITSTREAM es;
766 info.string = strdupWtoA( text );
767 info.offset = 0;
768 info.length = lstrlenA( info.string ) + 1;
770 es.dwCookie = (DWORD_PTR) &info;
771 es.dwError = 0;
772 es.pfnCallback = msi_richedit_stream_in;
774 SendMessageW( control->hwnd, EM_STREAMIN, SF_RTF, (LPARAM) &es );
776 msi_free( info.string );
779 static UINT msi_dialog_scrolltext_control( msi_dialog *dialog, MSIRECORD *rec )
781 static const WCHAR szRichEdit20W[] = {
782 'R','i','c','h','E','d','i','t','2','0','W',0
784 struct msi_scrolltext_info *info;
785 msi_control *control;
786 HMODULE hRichedit;
787 DWORD style;
789 info = msi_alloc( sizeof *info );
790 if (!info)
791 return ERROR_FUNCTION_FAILED;
793 hRichedit = LoadLibraryA("riched20");
795 style = WS_BORDER | ES_MULTILINE | WS_VSCROLL |
796 ES_READONLY | ES_AUTOVSCROLL | WS_TABSTOP;
797 control = msi_dialog_add_control( dialog, rec, szRichEdit20W, style );
798 if (!control)
800 FreeLibrary( hRichedit );
801 msi_free( info );
802 return ERROR_FUNCTION_FAILED;
805 control->hDll = hRichedit;
807 info->dialog = dialog;
808 info->control = control;
810 /* subclass the static control */
811 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
812 (LONG_PTR)MSIScrollText_WndProc );
813 SetPropW( control->hwnd, szButtonData, info );
815 /* add the text into the richedit */
816 msi_scrolltext_add_text( control, MSI_RecordGetString( rec, 10 ) );
818 return ERROR_SUCCESS;
821 static HBITMAP msi_load_picture( MSIDATABASE *db, LPCWSTR name,
822 INT cx, INT cy, DWORD flags )
824 HBITMAP hOleBitmap = 0, hBitmap = 0, hOldSrcBitmap, hOldDestBitmap;
825 MSIRECORD *rec = NULL;
826 IStream *stm = NULL;
827 IPicture *pic = NULL;
828 HDC srcdc, destdc;
829 BITMAP bm;
830 UINT r;
832 rec = msi_get_binary_record( db, name );
833 if( !rec )
834 goto end;
836 r = MSI_RecordGetIStream( rec, 2, &stm );
837 msiobj_release( &rec->hdr );
838 if( r != ERROR_SUCCESS )
839 goto end;
841 r = OleLoadPicture( stm, 0, TRUE, &IID_IPicture, (LPVOID*) &pic );
842 IStream_Release( stm );
843 if( FAILED( r ) )
845 ERR("failed to load picture\n");
846 goto end;
849 r = IPicture_get_Handle( pic, (OLE_HANDLE*) &hOleBitmap );
850 if( FAILED( r ) )
852 ERR("failed to get bitmap handle\n");
853 goto end;
856 /* make the bitmap the desired size */
857 r = GetObjectW( hOleBitmap, sizeof bm, &bm );
858 if (r != sizeof bm )
860 ERR("failed to get bitmap size\n");
861 goto end;
864 if (flags & LR_DEFAULTSIZE)
866 cx = bm.bmWidth;
867 cy = bm.bmHeight;
870 srcdc = CreateCompatibleDC( NULL );
871 hOldSrcBitmap = SelectObject( srcdc, hOleBitmap );
872 destdc = CreateCompatibleDC( NULL );
873 hBitmap = CreateCompatibleBitmap( srcdc, cx, cy );
874 hOldDestBitmap = SelectObject( destdc, hBitmap );
875 StretchBlt( destdc, 0, 0, cx, cy,
876 srcdc, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
877 SelectObject( srcdc, hOldSrcBitmap );
878 SelectObject( destdc, hOldDestBitmap );
879 DeleteDC( srcdc );
880 DeleteDC( destdc );
882 end:
883 if ( pic )
884 IPicture_Release( pic );
885 return hBitmap;
888 static UINT msi_dialog_bitmap_control( msi_dialog *dialog, MSIRECORD *rec )
890 UINT cx, cy, flags, style, attributes;
891 msi_control *control;
892 LPWSTR text;
894 flags = LR_LOADFROMFILE;
895 style = SS_BITMAP | SS_LEFT | WS_GROUP;
897 attributes = MSI_RecordGetInteger( rec, 8 );
898 if( attributes & msidbControlAttributesFixedSize )
900 flags |= LR_DEFAULTSIZE;
901 style |= SS_CENTERIMAGE;
904 control = msi_dialog_add_control( dialog, rec, szStatic, style );
905 cx = MSI_RecordGetInteger( rec, 6 );
906 cy = MSI_RecordGetInteger( rec, 7 );
907 cx = msi_dialog_scale_unit( dialog, cx );
908 cy = msi_dialog_scale_unit( dialog, cy );
910 text = msi_get_deformatted_field( dialog->package, rec, 10 );
911 control->hBitmap = msi_load_picture( dialog->package->db, text, cx, cy, flags );
912 if( control->hBitmap )
913 SendMessageW( control->hwnd, STM_SETIMAGE,
914 IMAGE_BITMAP, (LPARAM) control->hBitmap );
915 else
916 ERR("Failed to load bitmap %s\n", debugstr_w(text));
918 msi_free( text );
920 return ERROR_SUCCESS;
923 static UINT msi_dialog_icon_control( msi_dialog *dialog, MSIRECORD *rec )
925 msi_control *control;
926 DWORD attributes;
927 LPWSTR text;
929 TRACE("\n");
931 control = msi_dialog_add_control( dialog, rec, szStatic,
932 SS_ICON | SS_CENTERIMAGE | WS_GROUP );
934 attributes = MSI_RecordGetInteger( rec, 8 );
935 text = msi_get_deformatted_field( dialog->package, rec, 10 );
936 control->hIcon = msi_load_icon( dialog->package->db, text, attributes );
937 if( control->hIcon )
938 SendMessageW( control->hwnd, STM_SETICON, (WPARAM) control->hIcon, 0 );
939 else
940 ERR("Failed to load bitmap %s\n", debugstr_w(text));
941 msi_free( text );
942 return ERROR_SUCCESS;
945 static UINT msi_dialog_combo_control( msi_dialog *dialog, MSIRECORD *rec )
947 static const WCHAR szCombo[] = { 'C','O','M','B','O','B','O','X',0 };
949 msi_dialog_add_control( dialog, rec, szCombo,
950 SS_BITMAP | SS_LEFT | SS_CENTERIMAGE );
951 return ERROR_SUCCESS;
954 static UINT msi_dialog_edit_control( msi_dialog *dialog, MSIRECORD *rec )
956 msi_control *control;
957 LPCWSTR prop;
958 LPWSTR val;
960 control = msi_dialog_add_control( dialog, rec, szEdit,
961 WS_BORDER | WS_TABSTOP );
962 control->handler = msi_dialog_edit_handler;
963 prop = MSI_RecordGetString( rec, 9 );
964 if( prop )
965 control->property = strdupW( prop );
966 val = msi_dup_property( dialog->package, control->property );
967 SetWindowTextW( control->hwnd, val );
968 msi_free( val );
969 return ERROR_SUCCESS;
972 /******************** Masked Edit ********************************************/
974 #define MASK_MAX_GROUPS 10
976 struct msi_mask_group
978 UINT len;
979 UINT ofs;
980 WCHAR type;
981 HWND hwnd;
984 struct msi_maskedit_info
986 msi_dialog *dialog;
987 WNDPROC oldproc;
988 HWND hwnd;
989 LPWSTR prop;
990 UINT num_chars;
991 UINT num_groups;
992 struct msi_mask_group group[MASK_MAX_GROUPS];
995 static BOOL msi_mask_editable( WCHAR type )
997 switch (type)
999 case '%':
1000 case '#':
1001 case '&':
1002 case '`':
1003 case '?':
1004 case '^':
1005 return TRUE;
1007 return FALSE;
1010 static void msi_mask_control_change( struct msi_maskedit_info *info )
1012 LPWSTR val;
1013 UINT i, n, r;
1015 val = msi_alloc( (info->num_chars+1)*sizeof(WCHAR) );
1016 for( i=0, n=0; i<info->num_groups; i++ )
1018 if( (info->group[i].len + n) > info->num_chars )
1020 ERR("can't fit control %d text into template\n",i);
1021 break;
1023 if (!msi_mask_editable(info->group[i].type))
1025 for(r=0; r<info->group[i].len; r++)
1026 val[n+r] = info->group[i].type;
1027 val[n+r] = 0;
1029 else
1031 r = GetWindowTextW( info->group[i].hwnd, &val[n], info->group[i].len+1 );
1032 if( r != info->group[i].len )
1033 break;
1035 n += r;
1038 TRACE("%d/%d controls were good\n", i, info->num_groups);
1040 if( i == info->num_groups )
1042 TRACE("Set property %s to %s\n",
1043 debugstr_w(info->prop), debugstr_w(val) );
1044 CharUpperBuffW( val, info->num_chars );
1045 MSI_SetPropertyW( info->dialog->package, info->prop, val );
1046 msi_dialog_evaluate_control_conditions( info->dialog );
1048 msi_free( val );
1051 /* now move to the next control if necessary */
1052 static VOID msi_mask_next_control( struct msi_maskedit_info *info, HWND hWnd )
1054 HWND hWndNext;
1055 UINT len, i;
1057 for( i=0; i<info->num_groups; i++ )
1058 if( info->group[i].hwnd == hWnd )
1059 break;
1061 /* don't move from the last control */
1062 if( i >= (info->num_groups-1) )
1063 return;
1065 len = SendMessageW( hWnd, WM_GETTEXTLENGTH, 0, 0 );
1066 if( len < info->group[i].len )
1067 return;
1069 hWndNext = GetNextDlgTabItem( GetParent( hWnd ), hWnd, FALSE );
1070 SetFocus( hWndNext );
1073 static LRESULT WINAPI
1074 MSIMaskedEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1076 struct msi_maskedit_info *info;
1077 HRESULT r;
1079 TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
1081 info = GetPropW(hWnd, szButtonData);
1083 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1085 switch( msg )
1087 case WM_COMMAND:
1088 if (HIWORD(wParam) == EN_CHANGE)
1090 msi_mask_control_change( info );
1091 msi_mask_next_control( info, (HWND) lParam );
1093 break;
1094 case WM_NCDESTROY:
1095 msi_free( info->prop );
1096 msi_free( info );
1097 RemovePropW( hWnd, szButtonData );
1098 break;
1101 return r;
1104 /* fish the various bits of the property out and put them in the control */
1105 static void
1106 msi_maskedit_set_text( struct msi_maskedit_info *info, LPCWSTR text )
1108 LPCWSTR p;
1109 UINT i;
1111 p = text;
1112 for( i = 0; i < info->num_groups; i++ )
1114 if( info->group[i].len < lstrlenW( p ) )
1116 LPWSTR chunk = strdupW( p );
1117 chunk[ info->group[i].len ] = 0;
1118 SetWindowTextW( info->group[i].hwnd, chunk );
1119 msi_free( chunk );
1121 else
1123 SetWindowTextW( info->group[i].hwnd, p );
1124 break;
1126 p += info->group[i].len;
1130 static struct msi_maskedit_info * msi_dialog_parse_groups( LPCWSTR mask )
1132 struct msi_maskedit_info * info = NULL;
1133 int i = 0, n = 0, total = 0;
1134 LPCWSTR p;
1136 TRACE("masked control, template %s\n", debugstr_w(mask));
1138 if( !mask )
1139 return info;
1141 info = msi_alloc_zero( sizeof *info );
1142 if( !info )
1143 return info;
1145 p = strchrW(mask, '<');
1146 if( p )
1147 p++;
1148 else
1149 p = mask;
1151 for( i=0; i<MASK_MAX_GROUPS; i++ )
1153 /* stop at the end of the string */
1154 if( p[0] == 0 || p[0] == '>' )
1155 break;
1157 /* count the number of the same identifier */
1158 for( n=0; p[n] == p[0]; n++ )
1160 info->group[i].ofs = total;
1161 info->group[i].type = p[0];
1162 if( p[n] == '=' )
1164 n++;
1165 total++; /* an extra not part of the group */
1167 info->group[i].len = n;
1168 total += n;
1169 p += n;
1172 TRACE("%d characters in %d groups\n", total, i );
1173 if( i == MASK_MAX_GROUPS )
1174 ERR("too many groups in PIDTemplate %s\n", debugstr_w(mask));
1176 info->num_chars = total;
1177 info->num_groups = i;
1179 return info;
1182 static void
1183 msi_maskedit_create_children( struct msi_maskedit_info *info, LPCWSTR font )
1185 DWORD width, height, style, wx, ww;
1186 RECT rect;
1187 HWND hwnd;
1188 UINT i;
1190 style = WS_CHILD | WS_BORDER | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL;
1192 GetClientRect( info->hwnd, &rect );
1194 width = rect.right - rect.left;
1195 height = rect.bottom - rect.top;
1197 for( i = 0; i < info->num_groups; i++ )
1199 if (!msi_mask_editable( info->group[i].type ))
1200 continue;
1201 wx = (info->group[i].ofs * width) / info->num_chars;
1202 ww = (info->group[i].len * width) / info->num_chars;
1204 hwnd = CreateWindowW( szEdit, NULL, style, wx, 0, ww, height,
1205 info->hwnd, NULL, NULL, NULL );
1206 if( !hwnd )
1208 ERR("failed to create mask edit sub window\n");
1209 break;
1212 SendMessageW( hwnd, EM_LIMITTEXT, info->group[i].len, 0 );
1214 msi_dialog_set_font( info->dialog, hwnd,
1215 font?font:info->dialog->default_font );
1216 info->group[i].hwnd = hwnd;
1221 * office 2003 uses "73931<````=````=````=````=`````>@@@@@"
1222 * delphi 7 uses "<????-??????-??????-????>" and "<???-???>"
1223 * filemaker pro 7 uses "<^^^^=^^^^=^^^^=^^^^=^^^^=^^^^=^^^^^>"
1225 static UINT msi_dialog_maskedit_control( msi_dialog *dialog, MSIRECORD *rec )
1227 LPWSTR font_mask, val = NULL, font;
1228 struct msi_maskedit_info *info = NULL;
1229 UINT ret = ERROR_SUCCESS;
1230 msi_control *control;
1231 LPCWSTR prop, mask;
1233 TRACE("\n");
1235 font_mask = msi_get_deformatted_field( dialog->package, rec, 10 );
1236 font = msi_dialog_get_style( font_mask, &mask );
1237 if( !mask )
1239 ERR("mask template is empty\n");
1240 goto end;
1243 info = msi_dialog_parse_groups( mask );
1244 if( !info )
1246 ERR("template %s is invalid\n", debugstr_w(mask));
1247 goto end;
1250 info->dialog = dialog;
1252 control = msi_dialog_add_control( dialog, rec, szStatic,
1253 SS_OWNERDRAW | WS_GROUP | WS_VISIBLE );
1254 if( !control )
1256 ERR("Failed to create maskedit container\n");
1257 ret = ERROR_FUNCTION_FAILED;
1258 goto end;
1260 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
1262 info->hwnd = control->hwnd;
1264 /* subclass the static control */
1265 info->oldproc = (WNDPROC) SetWindowLongPtrW( info->hwnd, GWLP_WNDPROC,
1266 (LONG_PTR)MSIMaskedEdit_WndProc );
1267 SetPropW( control->hwnd, szButtonData, info );
1269 prop = MSI_RecordGetString( rec, 9 );
1270 if( prop )
1271 info->prop = strdupW( prop );
1273 msi_maskedit_create_children( info, font );
1275 if( prop )
1277 val = msi_dup_property( dialog->package, prop );
1278 if( val )
1280 msi_maskedit_set_text( info, val );
1281 msi_free( val );
1285 end:
1286 if( ret != ERROR_SUCCESS )
1287 msi_free( info );
1288 msi_free( font_mask );
1289 msi_free( font );
1290 return ret;
1293 /******************** Progress Bar *****************************************/
1295 static UINT msi_dialog_progress_bar( msi_dialog *dialog, MSIRECORD *rec )
1297 msi_dialog_add_control( dialog, rec, PROGRESS_CLASSW, WS_VISIBLE );
1298 return ERROR_SUCCESS;
1301 /******************** Path Edit ********************************************/
1303 static UINT msi_dialog_pathedit_control( msi_dialog *dialog, MSIRECORD *rec )
1305 FIXME("not implemented properly\n");
1306 return msi_dialog_edit_control( dialog, rec );
1309 /* radio buttons are a bit different from normal controls */
1310 static UINT msi_dialog_create_radiobutton( MSIRECORD *rec, LPVOID param )
1312 radio_button_group_descr *group = (radio_button_group_descr *)param;
1313 msi_dialog *dialog = group->dialog;
1314 msi_control *control;
1315 LPCWSTR prop, text, name;
1316 DWORD style, attributes = group->attributes;
1318 style = WS_CHILD | BS_AUTORADIOBUTTON | BS_MULTILINE | WS_TABSTOP;
1319 name = MSI_RecordGetString( rec, 3 );
1320 text = MSI_RecordGetString( rec, 8 );
1321 if( attributes & 1 )
1322 style |= WS_VISIBLE;
1323 if( ~attributes & 2 )
1324 style |= WS_DISABLED;
1326 control = msi_dialog_create_window( dialog, rec, szButton, name, text,
1327 style, group->parent->hwnd );
1328 if (!control)
1329 return ERROR_FUNCTION_FAILED;
1330 control->handler = msi_dialog_radiogroup_handler;
1332 prop = MSI_RecordGetString( rec, 1 );
1333 if( prop )
1334 control->property = strdupW( prop );
1336 return ERROR_SUCCESS;
1339 static UINT msi_dialog_radiogroup_control( msi_dialog *dialog, MSIRECORD *rec )
1341 static const WCHAR query[] = {
1342 'S','E','L','E','C','T',' ','*',' ',
1343 'F','R','O','M',' ','R','a','d','i','o','B','u','t','t','o','n',' ',
1344 'W','H','E','R','E',' ',
1345 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
1346 UINT r;
1347 LPCWSTR prop;
1348 msi_control *control;
1349 MSIQUERY *view = NULL;
1350 radio_button_group_descr group;
1351 MSIPACKAGE *package = dialog->package;
1352 WNDPROC oldproc;
1354 prop = MSI_RecordGetString( rec, 9 );
1356 TRACE("%p %p %s\n", dialog, rec, debugstr_w( prop ));
1358 /* Create parent group box to hold radio buttons */
1359 control = msi_dialog_add_control( dialog, rec, szButton, BS_OWNERDRAW|WS_GROUP );
1360 if( !control )
1361 return ERROR_FUNCTION_FAILED;
1363 oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1364 (LONG_PTR)MSIRadioGroup_WndProc );
1365 SetPropW(control->hwnd, szButtonData, oldproc);
1366 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
1368 if( prop )
1369 control->property = strdupW( prop );
1371 /* query the Radio Button table for all control in this group */
1372 r = MSI_OpenQuery( package->db, &view, query, prop );
1373 if( r != ERROR_SUCCESS )
1375 ERR("query failed for dialog %s radio group %s\n",
1376 debugstr_w(dialog->name), debugstr_w(prop));
1377 return ERROR_INVALID_PARAMETER;
1380 group.dialog = dialog;
1381 group.parent = control;
1382 group.attributes = MSI_RecordGetInteger( rec, 8 );
1384 r = MSI_IterateRecords( view, 0, msi_dialog_create_radiobutton, &group );
1385 msiobj_release( &view->hdr );
1387 return r;
1390 /******************** Selection Tree ***************************************/
1392 struct msi_selection_tree_info
1394 msi_dialog *dialog;
1395 HWND hwnd;
1396 WNDPROC oldproc;
1399 static void
1400 msi_seltree_sync_item_state( HWND hwnd, MSIFEATURE *feature, HTREEITEM hItem )
1402 TVITEMW tvi;
1404 TRACE("Feature %s -> %d %d %d\n", debugstr_w(feature->Title),
1405 feature->Installed, feature->Action, feature->ActionRequest);
1407 tvi.mask = TVIF_STATE;
1408 tvi.hItem = hItem;
1409 tvi.state = INDEXTOSTATEIMAGEMASK( feature->Action );
1410 tvi.stateMask = TVIS_STATEIMAGEMASK;
1412 SendMessageW( hwnd, TVM_SETITEMW, 0, (LPARAM) &tvi );
1415 static UINT
1416 msi_seltree_popup_menu( HWND hwnd, INT x, INT y )
1418 HMENU hMenu;
1419 INT r;
1421 /* create a menu to display */
1422 hMenu = CreatePopupMenu();
1424 /* FIXME: load strings from resources */
1425 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_LOCAL, "Install feature locally");
1426 AppendMenuA( hMenu, MF_GRAYED, 0x1000, "Install entire feature");
1427 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ADVERTISED, "Install on demand");
1428 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ABSENT, "Don't install");
1429 r = TrackPopupMenu( hMenu, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RETURNCMD,
1430 x, y, 0, hwnd, NULL );
1431 DestroyMenu( hMenu );
1432 return r;
1435 static MSIFEATURE *
1436 msi_seltree_feature_from_item( HWND hwnd, HTREEITEM hItem )
1438 TVITEMW tvi;
1440 /* get the feature from the item */
1441 memset( &tvi, 0, sizeof tvi );
1442 tvi.hItem = hItem;
1443 tvi.mask = TVIF_PARAM | TVIF_HANDLE;
1444 SendMessageW( hwnd, TVM_GETITEMW, 0, (LPARAM) &tvi );
1446 return (MSIFEATURE*) tvi.lParam;
1449 static LRESULT
1450 msi_seltree_menu( HWND hwnd, HTREEITEM hItem )
1452 MSIFEATURE *feature;
1453 union {
1454 RECT rc;
1455 POINT pt[2];
1456 HTREEITEM hItem;
1457 } u;
1458 UINT r;
1460 feature = msi_seltree_feature_from_item( hwnd, hItem );
1461 if (!feature)
1463 ERR("item %p feature was NULL\n", hItem);
1464 return 0;
1467 /* get the item's rectangle to put the menu just below it */
1468 u.hItem = hItem;
1469 SendMessageW( hwnd, TVM_GETITEMRECT, 0, (LPARAM) &u.rc );
1470 MapWindowPoints( hwnd, NULL, u.pt, 2 );
1472 r = msi_seltree_popup_menu( hwnd, u.rc.left, u.rc.top );
1474 switch (r)
1476 case INSTALLSTATE_LOCAL:
1477 case INSTALLSTATE_ADVERTISED:
1478 case INSTALLSTATE_ABSENT:
1479 feature->ActionRequest = r;
1480 feature->Action = r;
1481 break;
1482 default:
1483 FIXME("select feature and all children\n");
1486 /* update */
1487 msi_seltree_sync_item_state( hwnd, feature, hItem );
1489 return 0;
1492 static LRESULT WINAPI
1493 MSISelectionTree_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1495 struct msi_selection_tree_info *info;
1496 TVHITTESTINFO tvhti;
1497 HRESULT r;
1499 TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
1501 info = GetPropW(hWnd, szButtonData);
1503 switch( msg )
1505 case WM_LBUTTONDOWN:
1506 tvhti.pt.x = LOWORD( lParam );
1507 tvhti.pt.y = HIWORD( lParam );
1508 tvhti.flags = 0;
1509 tvhti.hItem = 0;
1510 r = CallWindowProcW(info->oldproc, hWnd, TVM_HITTEST, 0, (LPARAM) &tvhti );
1511 if (tvhti.flags & TVHT_ONITEMSTATEICON)
1512 return msi_seltree_menu( hWnd, tvhti.hItem );
1513 break;
1516 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1518 switch( msg )
1520 case WM_NCDESTROY:
1521 msi_free( info );
1522 RemovePropW( hWnd, szButtonData );
1523 break;
1525 return r;
1528 static void
1529 msi_seltree_add_child_features( MSIPACKAGE *package, HWND hwnd,
1530 LPCWSTR parent, HTREEITEM hParent )
1532 MSIFEATURE *feature;
1533 TVINSERTSTRUCTW tvis;
1534 HTREEITEM hitem;
1536 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
1538 if ( lstrcmpW( parent, feature->Feature_Parent ) )
1539 continue;
1541 if ( !feature->Title )
1542 continue;
1544 memset( &tvis, 0, sizeof tvis );
1545 tvis.hParent = hParent;
1546 tvis.hInsertAfter = TVI_SORT;
1547 tvis.u.item.mask = TVIF_TEXT | TVIF_PARAM;
1548 tvis.u.item.pszText = feature->Title;
1549 tvis.u.item.lParam = (LPARAM) feature;
1551 hitem = (HTREEITEM) SendMessageW( hwnd, TVM_INSERTITEMW, 0, (LPARAM) &tvis );
1552 if (!hitem)
1553 continue;
1555 msi_seltree_sync_item_state( hwnd, feature, hitem );
1556 msi_seltree_add_child_features( package, hwnd,
1557 feature->Feature, hitem );
1561 static void msi_seltree_create_imagelist( HWND hwnd )
1563 const int bm_width = 32, bm_height = 16, bm_count = 3;
1564 const int bm_resource = 0x1001;
1565 HIMAGELIST himl;
1566 int i;
1567 HBITMAP hbmp;
1569 himl = ImageList_Create( bm_width, bm_height, FALSE, 4, 0 );
1570 if (!himl)
1572 ERR("failed to create image list\n");
1573 return;
1576 for (i=0; i<bm_count; i++)
1578 hbmp = LoadBitmapW( msi_hInstance, MAKEINTRESOURCEW(i+bm_resource) );
1579 if (!hbmp)
1581 ERR("failed to load bitmap %d\n", i);
1582 break;
1586 * Add a dummy bitmap at offset zero because the treeview
1587 * can't use it as a state mask (zero means no user state).
1589 if (!i)
1590 ImageList_Add( himl, hbmp, NULL );
1592 ImageList_Add( himl, hbmp, NULL );
1595 SendMessageW( hwnd, TVM_SETIMAGELIST, TVSIL_STATE, (LPARAM)himl );
1598 static UINT msi_dialog_selection_tree( msi_dialog *dialog, MSIRECORD *rec )
1600 msi_control *control;
1601 LPCWSTR prop;
1602 MSIPACKAGE *package = dialog->package;
1603 DWORD style;
1604 struct msi_selection_tree_info *info;
1606 info = msi_alloc( sizeof *info );
1607 if (!info)
1608 return ERROR_FUNCTION_FAILED;
1610 /* create the treeview control */
1611 prop = MSI_RecordGetString( rec, 9 );
1612 style = TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT;
1613 style |= WS_GROUP | WS_VSCROLL;
1614 control = msi_dialog_add_control( dialog, rec, WC_TREEVIEWW, style );
1615 if (!control)
1617 msi_free(info);
1618 return ERROR_FUNCTION_FAILED;
1621 /* subclass */
1622 info->dialog = dialog;
1623 info->hwnd = control->hwnd;
1624 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1625 (LONG_PTR)MSISelectionTree_WndProc );
1626 SetPropW( control->hwnd, szButtonData, info );
1628 /* initialize it */
1629 msi_seltree_create_imagelist( control->hwnd );
1630 msi_seltree_add_child_features( package, control->hwnd, NULL, NULL );
1632 return ERROR_SUCCESS;
1635 struct control_handler msi_dialog_handler[] =
1637 { szText, msi_dialog_text_control },
1638 { szPushButton, msi_dialog_button_control },
1639 { szLine, msi_dialog_line_control },
1640 { szBitmap, msi_dialog_bitmap_control },
1641 { szCheckBox, msi_dialog_checkbox_control },
1642 { szScrollableText, msi_dialog_scrolltext_control },
1643 { szComboBox, msi_dialog_combo_control },
1644 { szEdit, msi_dialog_edit_control },
1645 { szMaskedEdit, msi_dialog_maskedit_control },
1646 { szPathEdit, msi_dialog_pathedit_control },
1647 { szProgressBar, msi_dialog_progress_bar },
1648 { szRadioButtonGroup, msi_dialog_radiogroup_control },
1649 { szIcon, msi_dialog_icon_control },
1650 { szSelectionTree, msi_dialog_selection_tree },
1653 #define NUM_CONTROL_TYPES (sizeof msi_dialog_handler/sizeof msi_dialog_handler[0])
1655 static UINT msi_dialog_create_controls( MSIRECORD *rec, LPVOID param )
1657 msi_dialog *dialog = param;
1658 LPCWSTR control_type;
1659 UINT i;
1661 /* find and call the function that can create this type of control */
1662 control_type = MSI_RecordGetString( rec, 3 );
1663 for( i=0; i<NUM_CONTROL_TYPES; i++ )
1664 if (!strcmpiW( msi_dialog_handler[i].control_type, control_type ))
1665 break;
1666 if( i != NUM_CONTROL_TYPES )
1667 msi_dialog_handler[i].func( dialog, rec );
1668 else
1669 ERR("no handler for element type %s\n", debugstr_w(control_type));
1671 return ERROR_SUCCESS;
1674 static UINT msi_dialog_fill_controls( msi_dialog *dialog )
1676 static const WCHAR query[] = {
1677 'S','E','L','E','C','T',' ','*',' ',
1678 'F','R','O','M',' ','C','o','n','t','r','o','l',' ',
1679 'W','H','E','R','E',' ',
1680 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
1681 UINT r;
1682 MSIQUERY *view = NULL;
1683 MSIPACKAGE *package = dialog->package;
1685 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
1687 /* query the Control table for all the elements of the control */
1688 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
1689 if( r != ERROR_SUCCESS )
1691 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
1692 return ERROR_INVALID_PARAMETER;
1695 r = MSI_IterateRecords( view, 0, msi_dialog_create_controls, dialog );
1696 msiobj_release( &view->hdr );
1698 return r;
1701 static UINT msi_dialog_set_control_condition( MSIRECORD *rec, LPVOID param )
1703 static const WCHAR szHide[] = { 'H','i','d','e',0 };
1704 static const WCHAR szShow[] = { 'S','h','o','w',0 };
1705 static const WCHAR szDisable[] = { 'D','i','s','a','b','l','e',0 };
1706 static const WCHAR szEnable[] = { 'E','n','a','b','l','e',0 };
1707 msi_dialog *dialog = param;
1708 msi_control *control;
1709 LPCWSTR name, action, condition;
1710 UINT r;
1712 name = MSI_RecordGetString( rec, 2 );
1713 action = MSI_RecordGetString( rec, 3 );
1714 condition = MSI_RecordGetString( rec, 4 );
1715 r = MSI_EvaluateConditionW( dialog->package, condition );
1716 control = msi_dialog_find_control( dialog, name );
1717 if( r == MSICONDITION_TRUE && control )
1719 TRACE("%s control %s\n", debugstr_w(action), debugstr_w(name));
1721 /* FIXME: case sensitive? */
1722 if(!lstrcmpW(action, szHide))
1723 ShowWindow(control->hwnd, SW_HIDE);
1724 else if(!strcmpW(action, szShow))
1725 ShowWindow(control->hwnd, SW_SHOW);
1726 else if(!strcmpW(action, szDisable))
1727 EnableWindow(control->hwnd, FALSE);
1728 else if(!strcmpW(action, szEnable))
1729 EnableWindow(control->hwnd, TRUE);
1730 else
1731 FIXME("Unhandled action %s\n", debugstr_w(action));
1734 return ERROR_SUCCESS;
1737 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog )
1739 static const WCHAR query[] = {
1740 'S','E','L','E','C','T',' ','*',' ',
1741 'F','R','O','M',' ',
1742 'C','o','n','t','r','o','l','C','o','n','d','i','t','i','o','n',' ',
1743 'W','H','E','R','E',' ',
1744 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0
1746 UINT r;
1747 MSIQUERY *view = NULL;
1748 MSIPACKAGE *package = dialog->package;
1750 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
1752 /* query the Control table for all the elements of the control */
1753 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
1754 if( r != ERROR_SUCCESS )
1756 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
1757 return ERROR_INVALID_PARAMETER;
1760 r = MSI_IterateRecords( view, 0, msi_dialog_set_control_condition, dialog );
1761 msiobj_release( &view->hdr );
1763 return r;
1766 UINT msi_dialog_reset( msi_dialog *dialog )
1768 /* FIXME: should restore the original values of any properties we changed */
1769 return msi_dialog_evaluate_control_conditions( dialog );
1772 /* figure out the height of 10 point MS Sans Serif */
1773 static INT msi_dialog_get_sans_serif_height( HWND hwnd )
1775 static const WCHAR szSansSerif[] = {
1776 'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0 };
1777 LOGFONTW lf;
1778 TEXTMETRICW tm;
1779 BOOL r;
1780 LONG height = 0;
1781 HFONT hFont, hOldFont;
1782 HDC hdc;
1784 hdc = GetDC( hwnd );
1785 if (hdc)
1787 memset( &lf, 0, sizeof lf );
1788 lf.lfHeight = MulDiv(10, GetDeviceCaps(hdc, LOGPIXELSY), 72);
1789 strcpyW( lf.lfFaceName, szSansSerif );
1790 hFont = CreateFontIndirectW(&lf);
1791 if (hFont)
1793 hOldFont = SelectObject( hdc, hFont );
1794 r = GetTextMetricsW( hdc, &tm );
1795 if (r)
1796 height = tm.tmHeight;
1797 SelectObject( hdc, hOldFont );
1798 DeleteObject( hFont );
1800 ReleaseDC( hwnd, hdc );
1802 return height;
1805 /* fetch the associated record from the Dialog table */
1806 static MSIRECORD *msi_get_dialog_record( msi_dialog *dialog )
1808 static const WCHAR query[] = {
1809 'S','E','L','E','C','T',' ','*',' ',
1810 'F','R','O','M',' ','D','i','a','l','o','g',' ',
1811 'W','H','E','R','E',' ',
1812 '`','D','i','a','l','o','g','`',' ','=',' ','\'','%','s','\'',0};
1813 MSIPACKAGE *package = dialog->package;
1814 MSIRECORD *rec = NULL;
1816 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
1818 rec = MSI_QueryGetRecord( package->db, query, dialog->name );
1819 if( !rec )
1820 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
1822 return rec;
1825 static void msi_dialog_adjust_dialog_size( msi_dialog *dialog, LPSIZE sz )
1827 RECT rect;
1828 LONG style;
1830 /* turn the client size into the window rectangle */
1831 rect.left = 0;
1832 rect.top = 0;
1833 rect.right = msi_dialog_scale_unit( dialog, sz->cx );
1834 rect.bottom = msi_dialog_scale_unit( dialog, sz->cy );
1835 style = GetWindowLongPtrW( dialog->hwnd, GWL_STYLE );
1836 AdjustWindowRect( &rect, style, FALSE );
1837 sz->cx = rect.right - rect.left;
1838 sz->cy = rect.bottom - rect.top;
1841 static BOOL msi_control_set_next( msi_control *control, msi_control *next )
1843 return SetWindowPos( next->hwnd, control->hwnd, 0, 0, 0, 0,
1844 SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOREDRAW |
1845 SWP_NOREPOSITION | SWP_NOSENDCHANGING | SWP_NOSIZE );
1848 static UINT msi_dialog_set_tab_order( msi_dialog *dialog )
1850 msi_control *control, *tab_next;
1852 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
1854 tab_next = msi_dialog_find_control( dialog, control->tabnext );
1855 if( !tab_next )
1856 continue;
1857 msi_control_set_next( control, tab_next );
1860 return ERROR_SUCCESS;
1863 static void msi_dialog_set_first_control( msi_dialog* dialog, LPCWSTR name )
1865 msi_control *control;
1867 control = msi_dialog_find_control( dialog, name );
1868 if( control )
1869 dialog->hWndFocus = control->hwnd;
1870 else
1871 dialog->hWndFocus = NULL;
1874 static LRESULT msi_dialog_oncreate( HWND hwnd, LPCREATESTRUCTW cs )
1876 static const WCHAR df[] = {
1877 'D','e','f','a','u','l','t','U','I','F','o','n','t',0 };
1878 static const WCHAR dfv[] = {
1879 'M','S',' ','S','h','e','l','l',' ','D','l','g',0 };
1880 msi_dialog *dialog = (msi_dialog*) cs->lpCreateParams;
1881 MSIRECORD *rec = NULL;
1882 LPWSTR title = NULL;
1883 SIZE size;
1885 TRACE("%p %p\n", dialog, dialog->package);
1887 dialog->hwnd = hwnd;
1888 SetWindowLongPtrW( hwnd, GWLP_USERDATA, (LONG_PTR) dialog );
1890 rec = msi_get_dialog_record( dialog );
1891 if( !rec )
1893 TRACE("No record found for dialog %s\n", debugstr_w(dialog->name));
1894 return -1;
1897 dialog->scale = msi_dialog_get_sans_serif_height(dialog->hwnd);
1899 size.cx = MSI_RecordGetInteger( rec, 4 );
1900 size.cy = MSI_RecordGetInteger( rec, 5 );
1901 msi_dialog_adjust_dialog_size( dialog, &size );
1903 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
1905 dialog->default_font = msi_dup_property( dialog->package, df );
1906 if (!dialog->default_font)
1908 dialog->default_font = strdupW(dfv);
1909 if (!dialog->default_font) return -1;
1912 title = msi_get_deformatted_field( dialog->package, rec, 7 );
1913 SetWindowTextW( hwnd, title );
1914 msi_free( title );
1916 SetWindowPos( hwnd, 0, 0, 0, size.cx, size.cy,
1917 SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW );
1920 msi_dialog_build_font_list( dialog );
1921 msi_dialog_fill_controls( dialog );
1922 msi_dialog_evaluate_control_conditions( dialog );
1923 msi_dialog_set_tab_order( dialog );
1924 msi_dialog_set_first_control( dialog, MSI_RecordGetString( rec, 8 ) );
1925 msiobj_release( &rec->hdr );
1927 return 0;
1930 static UINT msi_dialog_send_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
1932 LPWSTR event_fmt = NULL, arg_fmt = NULL;
1934 TRACE("Sending control event %s %s\n", debugstr_w(event), debugstr_w(arg));
1936 deformat_string( dialog->package, event, &event_fmt );
1937 deformat_string( dialog->package, arg, &arg_fmt );
1939 dialog->event_handler( dialog->package, event_fmt, arg_fmt, dialog );
1941 msi_free( event_fmt );
1942 msi_free( arg_fmt );
1944 return ERROR_SUCCESS;
1947 static UINT msi_dialog_set_property( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
1949 static const WCHAR szNullArg[] = { '{','}',0 };
1950 LPWSTR p, prop, arg_fmt = NULL;
1951 UINT len;
1953 len = strlenW(event);
1954 prop = msi_alloc( len*sizeof(WCHAR));
1955 strcpyW( prop, &event[1] );
1956 p = strchrW( prop, ']' );
1957 if( p && p[1] == 0 )
1959 *p = 0;
1960 if( strcmpW( szNullArg, arg ) )
1961 deformat_string( dialog->package, arg, &arg_fmt );
1962 MSI_SetPropertyW( dialog->package, prop, arg_fmt );
1963 msi_free( arg_fmt );
1965 else
1966 ERR("Badly formatted property string - what happens?\n");
1967 msi_free( prop );
1968 return ERROR_SUCCESS;
1971 static UINT msi_dialog_control_event( MSIRECORD *rec, LPVOID param )
1973 msi_dialog *dialog = param;
1974 LPCWSTR condition, event, arg;
1975 UINT r;
1977 condition = MSI_RecordGetString( rec, 5 );
1978 r = MSI_EvaluateConditionW( dialog->package, condition );
1979 if( r == MSICONDITION_TRUE )
1981 event = MSI_RecordGetString( rec, 3 );
1982 arg = MSI_RecordGetString( rec, 4 );
1983 if( event[0] == '[' )
1984 msi_dialog_set_property( dialog, event, arg );
1985 else
1986 msi_dialog_send_event( dialog, event, arg );
1989 return ERROR_SUCCESS;
1992 static UINT msi_dialog_button_handler( msi_dialog *dialog,
1993 msi_control *control, WPARAM param )
1995 static const WCHAR query[] = {
1996 'S','E','L','E','C','T',' ','*',' ',
1997 'F','R','O','M',' ','C','o','n','t','r','o','l','E','v','e','n','t',' ',
1998 'W','H','E','R','E',' ',
1999 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
2000 'A','N','D',' ',
2001 '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',' ',
2002 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','i','n','g','`',0
2004 MSIQUERY *view = NULL;
2005 UINT r;
2007 if( HIWORD(param) != BN_CLICKED )
2008 return ERROR_SUCCESS;
2010 r = MSI_OpenQuery( dialog->package->db, &view, query,
2011 dialog->name, control->name );
2012 if( r != ERROR_SUCCESS )
2014 ERR("query failed\n");
2015 return 0;
2018 r = MSI_IterateRecords( view, 0, msi_dialog_control_event, dialog );
2019 msiobj_release( &view->hdr );
2021 return r;
2024 static UINT msi_dialog_get_checkbox_state( msi_dialog *dialog,
2025 msi_control *control )
2027 WCHAR state[2] = { 0 };
2028 DWORD sz = 2;
2030 MSI_GetPropertyW( dialog->package, control->property, state, &sz );
2031 return state[0] ? 1 : 0;
2034 static void msi_dialog_set_checkbox_state( msi_dialog *dialog,
2035 msi_control *control, UINT state )
2037 static const WCHAR szState[] = { '1', 0 };
2038 LPCWSTR val;
2040 /* if uncheck then the property is set to NULL */
2041 if (!state)
2043 MSI_SetPropertyW( dialog->package, control->property, NULL );
2044 return;
2047 /* check for a custom state */
2048 if (control->value && control->value[0])
2049 val = control->value;
2050 else
2051 val = szState;
2053 MSI_SetPropertyW( dialog->package, control->property, val );
2056 static void msi_dialog_checkbox_sync_state( msi_dialog *dialog,
2057 msi_control *control )
2059 UINT state;
2061 state = msi_dialog_get_checkbox_state( dialog, control );
2062 SendMessageW( control->hwnd, BM_SETCHECK,
2063 state ? BST_CHECKED : BST_UNCHECKED, 0 );
2066 static UINT msi_dialog_checkbox_handler( msi_dialog *dialog,
2067 msi_control *control, WPARAM param )
2069 UINT state;
2071 if( HIWORD(param) != BN_CLICKED )
2072 return ERROR_SUCCESS;
2074 TRACE("clicked checkbox %s, set %s\n", debugstr_w(control->name),
2075 debugstr_w(control->property));
2077 state = msi_dialog_get_checkbox_state( dialog, control );
2078 state = state ? 0 : 1;
2079 msi_dialog_set_checkbox_state( dialog, control, state );
2080 msi_dialog_checkbox_sync_state( dialog, control );
2082 return msi_dialog_button_handler( dialog, control, param );
2085 static UINT msi_dialog_edit_handler( msi_dialog *dialog,
2086 msi_control *control, WPARAM param )
2088 UINT sz, r;
2089 LPWSTR buf;
2091 if( HIWORD(param) != EN_CHANGE )
2092 return ERROR_SUCCESS;
2094 TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
2095 debugstr_w(control->property));
2097 sz = 0x20;
2098 buf = msi_alloc( sz*sizeof(WCHAR) );
2099 while( buf )
2101 r = GetWindowTextW( control->hwnd, buf, sz );
2102 if( r < (sz-1) )
2103 break;
2104 sz *= 2;
2105 buf = msi_realloc( buf, sz*sizeof(WCHAR) );
2108 MSI_SetPropertyW( dialog->package, control->property, buf );
2110 msi_free( buf );
2112 return ERROR_SUCCESS;
2115 static UINT msi_dialog_radiogroup_handler( msi_dialog *dialog,
2116 msi_control *control, WPARAM param )
2118 if( HIWORD(param) != BN_CLICKED )
2119 return ERROR_SUCCESS;
2121 TRACE("clicked radio button %s, set %s\n", debugstr_w(control->name),
2122 debugstr_w(control->property));
2124 MSI_SetPropertyW( dialog->package, control->property, control->name );
2126 return msi_dialog_button_handler( dialog, control, param );
2129 static LRESULT msi_dialog_oncommand( msi_dialog *dialog, WPARAM param, HWND hwnd )
2131 msi_control *control = NULL;
2133 TRACE("%p %p %08x\n", dialog, hwnd, param);
2135 switch (param)
2137 case 1: /* enter */
2138 control = msi_dialog_find_control( dialog, dialog->control_default );
2139 break;
2140 case 2: /* escape */
2141 control = msi_dialog_find_control( dialog, dialog->control_cancel );
2142 break;
2143 default:
2144 control = msi_dialog_find_control_by_hwnd( dialog, hwnd );
2147 if( control )
2149 if( control->handler )
2151 control->handler( dialog, control, param );
2152 msi_dialog_evaluate_control_conditions( dialog );
2155 else
2156 ERR("button click from nowhere %p %d %p\n", dialog, param, hwnd);
2157 return 0;
2160 static void msi_dialog_setfocus( msi_dialog *dialog )
2162 HWND hwnd = dialog->hWndFocus;
2164 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, TRUE);
2165 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, FALSE);
2166 SetFocus( hwnd );
2167 dialog->hWndFocus = hwnd;
2170 static LRESULT WINAPI MSIDialog_WndProc( HWND hwnd, UINT msg,
2171 WPARAM wParam, LPARAM lParam )
2173 msi_dialog *dialog = (LPVOID) GetWindowLongPtrW( hwnd, GWLP_USERDATA );
2175 TRACE("0x%04x\n", msg);
2177 switch (msg)
2179 case WM_CREATE:
2180 return msi_dialog_oncreate( hwnd, (LPCREATESTRUCTW)lParam );
2182 case WM_COMMAND:
2183 return msi_dialog_oncommand( dialog, wParam, (HWND)lParam );
2185 case WM_ACTIVATE:
2186 if( LOWORD(wParam) == WA_INACTIVE )
2187 dialog->hWndFocus = GetFocus();
2188 else
2189 msi_dialog_setfocus( dialog );
2190 return 0;
2192 case WM_SETFOCUS:
2193 msi_dialog_setfocus( dialog );
2194 return 0;
2196 /* bounce back to our subclassed static control */
2197 case WM_CTLCOLORSTATIC:
2198 return SendMessageW( (HWND) lParam, WM_CTLCOLORSTATIC, wParam, lParam );
2200 case WM_DESTROY:
2201 dialog->hwnd = NULL;
2202 return 0;
2204 return DefWindowProcW(hwnd, msg, wParam, lParam);
2207 static BOOL CALLBACK msi_radioground_child_enum( HWND hWnd, LPARAM lParam )
2209 EnableWindow( hWnd, lParam );
2210 return TRUE;
2213 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2215 WNDPROC oldproc = (WNDPROC) GetPropW(hWnd, szButtonData);
2216 LRESULT r;
2218 TRACE("hWnd %p msg %04x wParam 0x%08x lParam 0x%08lx\n", hWnd, msg, wParam, lParam);
2220 if (msg == WM_COMMAND) /* Forward notifications to dialog */
2221 SendMessageW(GetParent(hWnd), msg, wParam, lParam);
2223 r = CallWindowProcW(oldproc, hWnd, msg, wParam, lParam);
2225 /* make sure the radio buttons show as disabled if the parent is disabled */
2226 if (msg == WM_ENABLE)
2227 EnumChildWindows( hWnd, msi_radioground_child_enum, wParam );
2229 return r;
2232 static LRESULT WINAPI MSIHiddenWindowProc( HWND hwnd, UINT msg,
2233 WPARAM wParam, LPARAM lParam )
2235 msi_dialog *dialog = (msi_dialog*) lParam;
2237 TRACE("%d %p\n", msg, dialog);
2239 switch (msg)
2241 case WM_MSI_DIALOG_CREATE:
2242 return msi_dialog_run_message_loop( dialog );
2243 case WM_MSI_DIALOG_DESTROY:
2244 msi_dialog_destroy( dialog );
2245 return 0;
2247 return DefWindowProcW( hwnd, msg, wParam, lParam );
2250 /* functions that interface to other modules within MSI */
2252 msi_dialog *msi_dialog_create( MSIPACKAGE* package, LPCWSTR szDialogName,
2253 msi_dialog_event_handler event_handler )
2255 MSIRECORD *rec = NULL;
2256 msi_dialog *dialog;
2258 TRACE("%p %s\n", package, debugstr_w(szDialogName));
2260 /* allocate the structure for the dialog to use */
2261 dialog = msi_alloc_zero( sizeof *dialog + sizeof(WCHAR)*strlenW(szDialogName) );
2262 if( !dialog )
2263 return NULL;
2264 strcpyW( dialog->name, szDialogName );
2265 msiobj_addref( &package->hdr );
2266 dialog->package = package;
2267 dialog->event_handler = event_handler;
2268 dialog->finished = 0;
2269 list_init( &dialog->controls );
2271 /* verify that the dialog exists */
2272 rec = msi_get_dialog_record( dialog );
2273 if( !rec )
2275 msiobj_release( &package->hdr );
2276 msi_free( dialog );
2277 return NULL;
2279 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
2280 dialog->control_default = strdupW( MSI_RecordGetString( rec, 9 ) );
2281 dialog->control_cancel = strdupW( MSI_RecordGetString( rec, 10 ) );
2282 msiobj_release( &rec->hdr );
2284 return dialog;
2287 static void msi_process_pending_messages( HWND hdlg )
2289 MSG msg;
2291 while( PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ) )
2293 if( hdlg && IsDialogMessageW( hdlg, &msg ))
2294 continue;
2295 TranslateMessage( &msg );
2296 DispatchMessageW( &msg );
2300 void msi_dialog_end_dialog( msi_dialog *dialog )
2302 TRACE("%p\n", dialog);
2303 dialog->finished = 1;
2304 PostMessageW(dialog->hwnd, WM_NULL, 0, 0);
2307 void msi_dialog_check_messages( HANDLE handle )
2309 DWORD r;
2311 /* in threads other than the UI thread, block */
2312 if( uiThreadId != GetCurrentThreadId() )
2314 if( handle )
2315 WaitForSingleObject( handle, INFINITE );
2316 return;
2319 /* there's two choices for the UI thread */
2320 while (1)
2322 msi_process_pending_messages( NULL );
2324 if( !handle )
2325 break;
2328 * block here until somebody creates a new dialog or
2329 * the handle we're waiting on becomes ready
2331 r = MsgWaitForMultipleObjects( 1, &handle, 0, INFINITE, QS_ALLINPUT );
2332 if( r == WAIT_OBJECT_0 )
2333 break;
2337 UINT msi_dialog_run_message_loop( msi_dialog *dialog )
2339 HWND hwnd;
2341 if( !(dialog->attributes & msidbDialogAttributesVisible) )
2342 return ERROR_SUCCESS;
2344 if( uiThreadId != GetCurrentThreadId() )
2345 return SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_CREATE, 0, (LPARAM) dialog );
2347 /* create the dialog window, don't show it yet */
2348 hwnd = CreateWindowW( szMsiDialogClass, dialog->name, WS_OVERLAPPEDWINDOW,
2349 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
2350 NULL, NULL, NULL, dialog );
2351 if( !hwnd )
2353 ERR("Failed to create dialog %s\n", debugstr_w( dialog->name ));
2354 return ERROR_FUNCTION_FAILED;
2357 ShowWindow( hwnd, SW_SHOW );
2358 /* UpdateWindow( hwnd ); - and causes the transparent static controls not to paint */
2360 if( dialog->attributes & msidbDialogAttributesModal )
2362 while( !dialog->finished )
2364 MsgWaitForMultipleObjects( 0, NULL, 0, INFINITE, QS_ALLEVENTS );
2365 msi_process_pending_messages( dialog->hwnd );
2368 else
2369 return ERROR_IO_PENDING;
2371 return ERROR_SUCCESS;
2374 void msi_dialog_do_preview( msi_dialog *dialog )
2376 TRACE("\n");
2377 dialog->attributes |= msidbDialogAttributesVisible;
2378 dialog->attributes &= ~msidbDialogAttributesModal;
2379 msi_dialog_run_message_loop( dialog );
2382 void msi_dialog_destroy( msi_dialog *dialog )
2384 if( uiThreadId != GetCurrentThreadId() )
2386 SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_DESTROY, 0, (LPARAM) dialog );
2387 return;
2390 if( dialog->hwnd )
2391 ShowWindow( dialog->hwnd, SW_HIDE );
2393 if( dialog->hwnd )
2394 DestroyWindow( dialog->hwnd );
2396 /* destroy the list of controls */
2397 while( !list_empty( &dialog->controls ) )
2399 msi_control *t = LIST_ENTRY( list_head( &dialog->controls ),
2400 msi_control, entry );
2401 list_remove( &t->entry );
2402 /* leave dialog->hwnd - destroying parent destroys child windows */
2403 msi_free( t->property );
2404 msi_free( t->value );
2405 if( t->hBitmap )
2406 DeleteObject( t->hBitmap );
2407 if( t->hIcon )
2408 DestroyIcon( t->hIcon );
2409 msi_free( t->tabnext );
2410 msi_free( t );
2411 if (t->hDll)
2412 FreeLibrary( t->hDll );
2415 /* destroy the list of fonts */
2416 while( dialog->font_list )
2418 msi_font *t = dialog->font_list;
2419 dialog->font_list = t->next;
2420 DeleteObject( t->hfont );
2421 msi_free( t );
2423 msi_free( dialog->default_font );
2425 msi_free( dialog->control_default );
2426 msi_free( dialog->control_cancel );
2427 msiobj_release( &dialog->package->hdr );
2428 dialog->package = NULL;
2429 msi_free( dialog );
2432 BOOL msi_dialog_register_class( void )
2434 WNDCLASSW cls;
2436 ZeroMemory( &cls, sizeof cls );
2437 cls.lpfnWndProc = MSIDialog_WndProc;
2438 cls.hInstance = NULL;
2439 cls.hIcon = LoadIconW(0, (LPWSTR)IDI_APPLICATION);
2440 cls.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
2441 cls.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
2442 cls.lpszMenuName = NULL;
2443 cls.lpszClassName = szMsiDialogClass;
2445 if( !RegisterClassW( &cls ) )
2446 return FALSE;
2448 cls.lpfnWndProc = MSIHiddenWindowProc;
2449 cls.lpszClassName = szMsiHiddenWindow;
2451 if( !RegisterClassW( &cls ) )
2452 return FALSE;
2454 uiThreadId = GetCurrentThreadId();
2456 hMsiHiddenWindow = CreateWindowW( szMsiHiddenWindow, NULL, WS_OVERLAPPED,
2457 0, 0, 100, 100, NULL, NULL, NULL, NULL );
2458 if( !hMsiHiddenWindow )
2459 return FALSE;
2461 return TRUE;
2464 void msi_dialog_unregister_class( void )
2466 DestroyWindow( hMsiHiddenWindow );
2467 hMsiHiddenWindow = NULL;
2468 UnregisterClassW( szMsiDialogClass, NULL );
2469 UnregisterClassW( szMsiHiddenWindow, NULL );
2470 uiThreadId = 0;