msi: Add handling for the GroupBox dialog control.
[wine/wine64.git] / dlls / msi / dialog.c
bloba4b4becc8d1eee0b7e5828d712ae55f51eb97b06
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 float progress_current;
66 float progress_max;
67 WCHAR name[1];
70 typedef struct msi_font_tag
72 struct msi_font_tag *next;
73 HFONT hfont;
74 COLORREF color;
75 WCHAR name[1];
76 } msi_font;
78 struct msi_dialog_tag
80 MSIPACKAGE *package;
81 msi_dialog_event_handler event_handler;
82 BOOL finished;
83 INT scale;
84 DWORD attributes;
85 HWND hwnd;
86 LPWSTR default_font;
87 msi_font *font_list;
88 struct list controls;
89 HWND hWndFocus;
90 LPWSTR control_default;
91 LPWSTR control_cancel;
92 WCHAR name[1];
95 typedef UINT (*msi_dialog_control_func)( msi_dialog *dialog, MSIRECORD *rec );
96 struct control_handler
98 LPCWSTR control_type;
99 msi_dialog_control_func func;
102 typedef struct
104 msi_dialog* dialog;
105 msi_control *parent;
106 DWORD attributes;
107 LPWSTR propval;
108 } radio_button_group_descr;
110 static const WCHAR szMsiDialogClass[] = {
111 'M','s','i','D','i','a','l','o','g','C','l','o','s','e','C','l','a','s','s',0
113 static const WCHAR szMsiHiddenWindow[] = {
114 'M','s','i','H','i','d','d','e','n','W','i','n','d','o','w',0 };
115 static const WCHAR szStatic[] = { 'S','t','a','t','i','c',0 };
116 static const WCHAR szButton[] = { 'B','U','T','T','O','N', 0 };
117 static const WCHAR szButtonData[] = { 'M','S','I','D','A','T','A',0 };
118 static const WCHAR szProgress[] = { 'P','r','o','g','r','e','s','s',0 };
119 static const WCHAR szText[] = { 'T','e','x','t',0 };
120 static const WCHAR szPushButton[] = { 'P','u','s','h','B','u','t','t','o','n',0 };
121 static const WCHAR szLine[] = { 'L','i','n','e',0 };
122 static const WCHAR szBitmap[] = { 'B','i','t','m','a','p',0 };
123 static const WCHAR szCheckBox[] = { 'C','h','e','c','k','B','o','x',0 };
124 static const WCHAR szScrollableText[] = {
125 'S','c','r','o','l','l','a','b','l','e','T','e','x','t',0 };
126 static const WCHAR szComboBox[] = { 'C','o','m','b','o','B','o','x',0 };
127 static const WCHAR szEdit[] = { 'E','d','i','t',0 };
128 static const WCHAR szMaskedEdit[] = { 'M','a','s','k','e','d','E','d','i','t',0 };
129 static const WCHAR szPathEdit[] = { 'P','a','t','h','E','d','i','t',0 };
130 static const WCHAR szProgressBar[] = {
131 'P','r','o','g','r','e','s','s','B','a','r',0 };
132 static const WCHAR szRadioButtonGroup[] = {
133 'R','a','d','i','o','B','u','t','t','o','n','G','r','o','u','p',0 };
134 static const WCHAR szIcon[] = { 'I','c','o','n',0 };
135 static const WCHAR szSelectionTree[] = {
136 'S','e','l','e','c','t','i','o','n','T','r','e','e',0 };
137 static const WCHAR szGroupBox[] = { 'G','r','o','u','p','B','o','x',0 };
139 static UINT msi_dialog_checkbox_handler( msi_dialog *, msi_control *, WPARAM );
140 static void msi_dialog_checkbox_sync_state( msi_dialog *, msi_control * );
141 static UINT msi_dialog_button_handler( msi_dialog *, msi_control *, WPARAM );
142 static UINT msi_dialog_edit_handler( msi_dialog *, msi_control *, WPARAM );
143 static UINT msi_dialog_radiogroup_handler( msi_dialog *, msi_control *, WPARAM param );
144 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog );
145 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
148 /* dialog sequencing */
150 #define WM_MSI_DIALOG_CREATE (WM_USER+0x100)
151 #define WM_MSI_DIALOG_DESTROY (WM_USER+0x101)
153 static DWORD uiThreadId;
154 static HWND hMsiHiddenWindow;
156 static INT msi_dialog_scale_unit( msi_dialog *dialog, INT val )
158 return (dialog->scale * val + 5) / 10;
161 static msi_control *msi_dialog_find_control( msi_dialog *dialog, LPCWSTR name )
163 msi_control *control;
165 if( !name )
166 return NULL;
167 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
168 if( !strcmpW( control->name, name ) ) /* FIXME: case sensitive? */
169 return control;
170 return NULL;
173 static msi_control *msi_dialog_find_control_by_hwnd( msi_dialog *dialog, HWND hwnd )
175 msi_control *control;
177 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
178 if( hwnd == control->hwnd )
179 return control;
180 return NULL;
183 static LPWSTR msi_get_deformatted_field( MSIPACKAGE *package, MSIRECORD *rec, int field )
185 LPCWSTR str = MSI_RecordGetString( rec, field );
186 LPWSTR ret = NULL;
188 if (str)
189 deformat_string( package, str, &ret );
190 return ret;
194 * msi_dialog_get_style
196 * Extract the {\style} string from the front of the text to display and
197 * update the pointer.
199 static LPWSTR msi_dialog_get_style( LPCWSTR p, LPCWSTR *rest )
201 LPWSTR ret = NULL;
202 LPCWSTR q, i;
203 DWORD len;
205 *rest = p;
206 if( !p )
207 return ret;
208 if( *p++ != '{' )
209 return ret;
210 q = strchrW( p, '}' );
211 if( !q )
212 return ret;
213 if( *p == '\\' || *p == '&' )
214 p++;
216 /* little bit of sanity checking to stop us getting confused with RTF */
217 for( i=p; i<q; i++ )
218 if( *i == '}' || *i == '\\' )
219 return ret;
221 *rest = ++q;
222 len = q - p;
224 ret = msi_alloc( len*sizeof(WCHAR) );
225 if( !ret )
226 return ret;
227 memcpy( ret, p, len*sizeof(WCHAR) );
228 ret[len-1] = 0;
229 return ret;
232 static UINT msi_dialog_add_font( MSIRECORD *rec, LPVOID param )
234 msi_dialog *dialog = param;
235 msi_font *font;
236 LPCWSTR face, name;
237 LOGFONTW lf;
238 INT style;
239 HDC hdc;
241 /* create a font and add it to the list */
242 name = MSI_RecordGetString( rec, 1 );
243 font = msi_alloc( sizeof *font + strlenW( name )*sizeof (WCHAR) );
244 strcpyW( font->name, name );
245 font->next = dialog->font_list;
246 dialog->font_list = font;
248 font->color = MSI_RecordGetInteger( rec, 4 );
250 memset( &lf, 0, sizeof lf );
251 face = MSI_RecordGetString( rec, 2 );
252 lf.lfHeight = MSI_RecordGetInteger( rec, 3 );
253 style = MSI_RecordGetInteger( rec, 5 );
254 if( style & msidbTextStyleStyleBitsBold )
255 lf.lfWeight = FW_BOLD;
256 if( style & msidbTextStyleStyleBitsItalic )
257 lf.lfItalic = TRUE;
258 if( style & msidbTextStyleStyleBitsUnderline )
259 lf.lfUnderline = TRUE;
260 if( style & msidbTextStyleStyleBitsStrike )
261 lf.lfStrikeOut = TRUE;
262 lstrcpynW( lf.lfFaceName, face, LF_FACESIZE );
264 /* adjust the height */
265 hdc = GetDC( dialog->hwnd );
266 if (hdc)
268 lf.lfHeight = -MulDiv(lf.lfHeight, GetDeviceCaps(hdc, LOGPIXELSY), 72);
269 ReleaseDC( dialog->hwnd, hdc );
272 font->hfont = CreateFontIndirectW( &lf );
274 TRACE("Adding font style %s\n", debugstr_w(font->name) );
276 return ERROR_SUCCESS;
279 static msi_font *msi_dialog_find_font( msi_dialog *dialog, LPCWSTR name )
281 msi_font *font;
283 for( font = dialog->font_list; font; font = font->next )
284 if( !strcmpW( font->name, name ) ) /* FIXME: case sensitive? */
285 break;
287 return font;
290 static UINT msi_dialog_set_font( msi_dialog *dialog, HWND hwnd, LPCWSTR name )
292 msi_font *font;
294 font = msi_dialog_find_font( dialog, name );
295 if( font )
296 SendMessageW( hwnd, WM_SETFONT, (WPARAM) font->hfont, TRUE );
297 else
298 ERR("No font entry for %s\n", debugstr_w(name));
299 return ERROR_SUCCESS;
302 static UINT msi_dialog_build_font_list( msi_dialog *dialog )
304 static const WCHAR query[] = {
305 'S','E','L','E','C','T',' ','*',' ',
306 'F','R','O','M',' ','`','T','e','x','t','S','t','y','l','e','`',' ',0
308 UINT r;
309 MSIQUERY *view = NULL;
311 TRACE("dialog %p\n", dialog );
313 r = MSI_OpenQuery( dialog->package->db, &view, query );
314 if( r != ERROR_SUCCESS )
315 return r;
317 r = MSI_IterateRecords( view, NULL, msi_dialog_add_font, dialog );
318 msiobj_release( &view->hdr );
320 return r;
323 static msi_control *msi_dialog_create_window( msi_dialog *dialog,
324 MSIRECORD *rec, DWORD exstyle, LPCWSTR szCls, LPCWSTR name, LPCWSTR text,
325 DWORD style, HWND parent )
327 DWORD x, y, width, height;
328 LPWSTR font = NULL, title_font = NULL;
329 LPCWSTR title = NULL;
330 msi_control *control;
332 style |= WS_CHILD;
334 control = msi_alloc( sizeof *control + strlenW(name)*sizeof(WCHAR) );
335 strcpyW( control->name, name );
336 list_add_head( &dialog->controls, &control->entry );
337 control->handler = NULL;
338 control->property = NULL;
339 control->value = NULL;
340 control->hBitmap = NULL;
341 control->hIcon = NULL;
342 control->hDll = NULL;
343 control->tabnext = strdupW( MSI_RecordGetString( rec, 11) );
344 control->progress_current = 0;
345 control->progress_max = 100;
347 x = MSI_RecordGetInteger( rec, 4 );
348 y = MSI_RecordGetInteger( rec, 5 );
349 width = MSI_RecordGetInteger( rec, 6 );
350 height = MSI_RecordGetInteger( rec, 7 );
352 x = msi_dialog_scale_unit( dialog, x );
353 y = msi_dialog_scale_unit( dialog, y );
354 width = msi_dialog_scale_unit( dialog, width );
355 height = msi_dialog_scale_unit( dialog, height );
357 if( text )
359 deformat_string( dialog->package, text, &title_font );
360 font = msi_dialog_get_style( title_font, &title );
363 control->hwnd = CreateWindowExW( exstyle, szCls, title, style,
364 x, y, width, height, parent, NULL, NULL, NULL );
366 TRACE("Dialog %s control %s hwnd %p\n",
367 debugstr_w(dialog->name), debugstr_w(text), control->hwnd );
369 msi_dialog_set_font( dialog, control->hwnd,
370 font ? font : dialog->default_font );
372 msi_free( title_font );
373 msi_free( font );
375 return control;
378 static MSIRECORD *msi_get_binary_record( MSIDATABASE *db, LPCWSTR name )
380 static const WCHAR query[] = {
381 's','e','l','e','c','t',' ','*',' ',
382 'f','r','o','m',' ','B','i','n','a','r','y',' ',
383 'w','h','e','r','e',' ',
384 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0
387 return MSI_QueryGetRecord( db, query, name );
390 static LPWSTR msi_create_tmp_path(void)
392 WCHAR tmp[MAX_PATH];
393 LPWSTR path = NULL;
394 static const WCHAR prefix[] = { 'm','s','i',0 };
395 DWORD len, r;
397 r = GetTempPathW( MAX_PATH, tmp );
398 if( !r )
399 return path;
400 len = lstrlenW( tmp ) + 20;
401 path = msi_alloc( len * sizeof (WCHAR) );
402 if( path )
404 r = GetTempFileNameW( tmp, prefix, 0, path );
405 if (!r)
407 msi_free( path );
408 path = NULL;
411 return path;
415 static HANDLE msi_load_image( MSIDATABASE *db, LPCWSTR name, UINT type,
416 UINT cx, UINT cy, UINT flags )
418 MSIRECORD *rec = NULL;
419 HANDLE himage = NULL;
420 LPWSTR tmp;
421 UINT r;
423 TRACE("%p %s %u %u %08x\n", db, debugstr_w(name), cx, cy, flags);
425 tmp = msi_create_tmp_path();
426 if( !tmp )
427 return himage;
429 rec = msi_get_binary_record( db, name );
430 if( rec )
432 r = MSI_RecordStreamToFile( rec, 2, tmp );
433 if( r == ERROR_SUCCESS )
435 himage = LoadImageW( 0, tmp, type, cx, cy, flags );
436 DeleteFileW( tmp );
438 msiobj_release( &rec->hdr );
441 msi_free( tmp );
442 return himage;
445 static HICON msi_load_icon( MSIDATABASE *db, LPCWSTR text, UINT attributes )
447 DWORD cx = 0, cy = 0, flags;
449 flags = LR_LOADFROMFILE | LR_DEFAULTSIZE;
450 if( attributes & msidbControlAttributesFixedSize )
452 flags &= ~LR_DEFAULTSIZE;
453 if( attributes & msidbControlAttributesIconSize16 )
455 cx += 16;
456 cy += 16;
458 if( attributes & msidbControlAttributesIconSize32 )
460 cx += 32;
461 cy += 32;
463 /* msidbControlAttributesIconSize48 handled by above logic */
465 return msi_load_image( db, text, IMAGE_ICON, cx, cy, flags );
469 /* called from the Control Event subscription code */
470 void msi_dialog_handle_event( msi_dialog* dialog, LPCWSTR control,
471 LPCWSTR attribute, MSIRECORD *rec )
473 msi_control* ctrl;
474 LPCWSTR font_text, text = NULL;
475 LPWSTR font;
477 ctrl = msi_dialog_find_control( dialog, control );
478 if (!ctrl)
479 return;
480 if( !lstrcmpW(attribute, szText) )
482 font_text = MSI_RecordGetString( rec , 1 );
483 font = msi_dialog_get_style( font_text, &text );
484 SetWindowTextW( ctrl->hwnd, text );
485 msi_free( font );
486 msi_dialog_check_messages( NULL );
488 else if( !lstrcmpW(attribute, szProgress) )
490 DWORD func, val;
492 func = MSI_RecordGetInteger( rec , 1 );
493 val = MSI_RecordGetInteger( rec , 2 );
495 switch (func)
497 case 0: /* init */
498 ctrl->progress_max = val;
499 ctrl->progress_current = 0;
500 SendMessageW(ctrl->hwnd, PBM_SETRANGE, 0, MAKELPARAM(0,100));
501 SendMessageW(ctrl->hwnd, PBM_SETPOS, 0, 0);
502 break;
503 case 1: /* FIXME: not sure what this is supposed to do */
504 break;
505 case 2: /* move */
506 ctrl->progress_current += val;
507 SendMessageW(ctrl->hwnd, PBM_SETPOS, 100*(ctrl->progress_current/ctrl->progress_max), 0);
508 break;
509 default:
510 ERR("Unknown progress message %ld\n", func);
511 break;
514 else
516 FIXME("Attribute %s not being set\n", debugstr_w(attribute));
517 return;
521 static void msi_dialog_map_events(msi_dialog* dialog, LPCWSTR control)
523 static const WCHAR Query[] = {
524 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
525 '`','E','v','e','n','t','M','a','p','p','i','n','g','`',' ',
526 'W','H','E','R','E',' ',
527 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
528 'A','N','D',' ',
529 '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',0
531 MSIRECORD *row;
532 LPCWSTR event, attribute;
534 row = MSI_QueryGetRecord( dialog->package->db, Query, dialog->name, control );
535 if (!row)
536 return;
538 event = MSI_RecordGetString( row, 3 );
539 attribute = MSI_RecordGetString( row, 4 );
540 ControlEvent_SubscribeToEvent( dialog->package, event, control, attribute );
541 msiobj_release( &row->hdr );
544 /* everything except radio buttons */
545 static msi_control *msi_dialog_add_control( msi_dialog *dialog,
546 MSIRECORD *rec, LPCWSTR szCls, DWORD style )
548 DWORD attributes;
549 LPCWSTR text, name;
550 DWORD exstyle = 0;
552 name = MSI_RecordGetString( rec, 2 );
553 attributes = MSI_RecordGetInteger( rec, 8 );
554 text = MSI_RecordGetString( rec, 10 );
555 if( attributes & msidbControlAttributesVisible )
556 style |= WS_VISIBLE;
557 if( ~attributes & msidbControlAttributesEnabled )
558 style |= WS_DISABLED;
559 if( attributes & msidbControlAttributesSunken )
560 exstyle |= WS_EX_CLIENTEDGE;
562 msi_dialog_map_events(dialog, name);
564 return msi_dialog_create_window( dialog, rec, exstyle, szCls, name,
565 text, style, dialog->hwnd );
568 struct msi_text_info
570 msi_font *font;
571 WNDPROC oldproc;
572 DWORD attributes;
576 * we don't erase our own background,
577 * so we have to make sure that the parent window redraws first
579 static void msi_text_on_settext( HWND hWnd )
581 HWND hParent;
582 RECT rc;
584 hParent = GetParent( hWnd );
585 GetWindowRect( hWnd, &rc );
586 MapWindowPoints( NULL, hParent, (LPPOINT) &rc, 2 );
587 InvalidateRect( hParent, &rc, TRUE );
590 static LRESULT WINAPI
591 MSIText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
593 struct msi_text_info *info;
594 LRESULT r = 0;
596 TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
598 info = GetPropW(hWnd, szButtonData);
600 if ( info->font )
601 SetTextColor( (HDC)wParam, info->font->color );
603 if( msg == WM_CTLCOLORSTATIC &&
604 ( info->attributes & msidbControlAttributesTransparent ) )
606 SetBkMode( (HDC)wParam, TRANSPARENT );
607 return (LRESULT) GetStockObject(NULL_BRUSH);
610 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
612 switch( msg )
614 case WM_SETTEXT:
615 msi_text_on_settext( hWnd );
616 break;
617 case WM_NCDESTROY:
618 msi_free( info );
619 RemovePropW( hWnd, szButtonData );
620 break;
623 return r;
626 static UINT msi_dialog_text_control( msi_dialog *dialog, MSIRECORD *rec )
628 msi_control *control;
629 struct msi_text_info *info;
630 LPCWSTR text, ptr;
631 LPWSTR font_name;
633 TRACE("%p %p\n", dialog, rec);
635 control = msi_dialog_add_control( dialog, rec, szStatic, SS_LEFT | WS_GROUP );
636 if( !control )
637 return ERROR_FUNCTION_FAILED;
639 info = msi_alloc( sizeof *info );
640 if( !info )
641 return ERROR_SUCCESS;
643 text = MSI_RecordGetString( rec, 10 );
644 font_name = msi_dialog_get_style( text, &ptr );
645 info->font = ( font_name ) ? msi_dialog_find_font( dialog, font_name ) : NULL;
646 msi_free( font_name );
648 info->attributes = MSI_RecordGetInteger( rec, 8 );
649 if( info->attributes & msidbControlAttributesTransparent )
650 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT );
652 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
653 (LONG_PTR)MSIText_WndProc );
654 SetPropW( control->hwnd, szButtonData, info );
656 return ERROR_SUCCESS;
659 static UINT msi_dialog_button_control( msi_dialog *dialog, MSIRECORD *rec )
661 msi_control *control;
662 UINT attributes, style;
663 LPWSTR text;
665 TRACE("%p %p\n", dialog, rec);
667 style = WS_TABSTOP;
668 attributes = MSI_RecordGetInteger( rec, 8 );
669 if( attributes & msidbControlAttributesIcon )
670 style |= BS_ICON;
672 control = msi_dialog_add_control( dialog, rec, szButton, style );
673 if( !control )
674 return ERROR_FUNCTION_FAILED;
676 control->handler = msi_dialog_button_handler;
678 /* set the icon */
679 text = msi_get_deformatted_field( dialog->package, rec, 10 );
680 control->hIcon = msi_load_icon( dialog->package->db, text, attributes );
681 if( attributes & msidbControlAttributesIcon )
682 SendMessageW( control->hwnd, BM_SETIMAGE, IMAGE_ICON, (LPARAM) control->hIcon );
683 msi_free( text );
685 return ERROR_SUCCESS;
688 static LPWSTR msi_get_checkbox_value( msi_dialog *dialog, LPCWSTR prop )
690 static const WCHAR query[] = {
691 'S','E','L','E','C','T',' ','*',' ',
692 'F','R','O','M',' ','`','C','h','e','c','k','B','o','x',' ','`',
693 'W','H','E','R','E',' ',
694 '`','P','r','o','p','e','r','t','y','`',' ','=',' ',
695 '\'','%','s','\'',0
697 MSIRECORD *rec = NULL;
698 LPWSTR ret = NULL;
700 /* find if there is a value associated with the checkbox */
701 rec = MSI_QueryGetRecord( dialog->package->db, query, prop );
702 if (!rec)
703 return ret;
705 ret = msi_get_deformatted_field( dialog->package, rec, 2 );
706 if( ret && !ret[0] )
708 msi_free( ret );
709 ret = NULL;
711 msiobj_release( &rec->hdr );
712 if (ret)
713 return ret;
715 ret = msi_dup_property( dialog->package, prop );
716 if( ret && !ret[0] )
718 msi_free( ret );
719 ret = NULL;
722 return ret;
725 static UINT msi_dialog_checkbox_control( msi_dialog *dialog, MSIRECORD *rec )
727 msi_control *control;
728 LPCWSTR prop;
730 TRACE("%p %p\n", dialog, rec);
732 control = msi_dialog_add_control( dialog, rec, szButton,
733 BS_CHECKBOX | BS_MULTILINE | WS_TABSTOP );
734 control->handler = msi_dialog_checkbox_handler;
735 prop = MSI_RecordGetString( rec, 9 );
736 if( prop )
738 control->property = strdupW( prop );
739 control->value = msi_get_checkbox_value( dialog, prop );
740 TRACE("control %s value %s\n", debugstr_w(control->property),
741 debugstr_w(control->value));
743 msi_dialog_checkbox_sync_state( dialog, control );
745 return ERROR_SUCCESS;
748 static UINT msi_dialog_line_control( msi_dialog *dialog, MSIRECORD *rec )
750 TRACE("%p %p\n", dialog, rec);
752 msi_dialog_add_control( dialog, rec, szStatic, SS_ETCHEDHORZ | SS_SUNKEN );
753 return ERROR_SUCCESS;
756 /******************** Scroll Text ********************************************/
758 struct msi_scrolltext_info
760 msi_dialog *dialog;
761 msi_control *control;
762 WNDPROC oldproc;
765 static LRESULT WINAPI
766 MSIScrollText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
768 struct msi_scrolltext_info *info;
769 HRESULT r;
771 TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
773 info = GetPropW( hWnd, szButtonData );
775 r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
777 switch( msg )
779 case WM_NCDESTROY:
780 msi_free( info );
781 RemovePropW( hWnd, szButtonData );
782 break;
783 case WM_PAINT:
784 /* native MSI sets a wait cursor here */
785 msi_dialog_button_handler( info->dialog, info->control, BN_CLICKED );
786 break;
788 return r;
791 struct msi_streamin_info
793 LPSTR string;
794 DWORD offset;
795 DWORD length;
798 static DWORD CALLBACK
799 msi_richedit_stream_in( DWORD_PTR arg, LPBYTE buffer, LONG count, LONG *pcb )
801 struct msi_streamin_info *info = (struct msi_streamin_info*) arg;
803 if( (count + info->offset) > info->length )
804 count = info->length - info->offset;
805 memcpy( buffer, &info->string[ info->offset ], count );
806 *pcb = count;
807 info->offset += count;
809 TRACE("%ld/%ld\n", info->offset, info->length);
811 return 0;
814 static void msi_scrolltext_add_text( msi_control *control, LPCWSTR text )
816 struct msi_streamin_info info;
817 EDITSTREAM es;
819 info.string = strdupWtoA( text );
820 info.offset = 0;
821 info.length = lstrlenA( info.string ) + 1;
823 es.dwCookie = (DWORD_PTR) &info;
824 es.dwError = 0;
825 es.pfnCallback = msi_richedit_stream_in;
827 SendMessageW( control->hwnd, EM_STREAMIN, SF_RTF, (LPARAM) &es );
829 msi_free( info.string );
832 static UINT msi_dialog_scrolltext_control( msi_dialog *dialog, MSIRECORD *rec )
834 static const WCHAR szRichEdit20W[] = {
835 'R','i','c','h','E','d','i','t','2','0','W',0
837 struct msi_scrolltext_info *info;
838 msi_control *control;
839 HMODULE hRichedit;
840 DWORD style;
842 info = msi_alloc( sizeof *info );
843 if (!info)
844 return ERROR_FUNCTION_FAILED;
846 hRichedit = LoadLibraryA("riched20");
848 style = WS_BORDER | ES_MULTILINE | WS_VSCROLL |
849 ES_READONLY | ES_AUTOVSCROLL | WS_TABSTOP;
850 control = msi_dialog_add_control( dialog, rec, szRichEdit20W, style );
851 if (!control)
853 FreeLibrary( hRichedit );
854 msi_free( info );
855 return ERROR_FUNCTION_FAILED;
858 control->hDll = hRichedit;
860 info->dialog = dialog;
861 info->control = control;
863 /* subclass the static control */
864 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
865 (LONG_PTR)MSIScrollText_WndProc );
866 SetPropW( control->hwnd, szButtonData, info );
868 /* add the text into the richedit */
869 msi_scrolltext_add_text( control, MSI_RecordGetString( rec, 10 ) );
871 return ERROR_SUCCESS;
874 static HBITMAP msi_load_picture( MSIDATABASE *db, LPCWSTR name,
875 INT cx, INT cy, DWORD flags )
877 HBITMAP hOleBitmap = 0, hBitmap = 0, hOldSrcBitmap, hOldDestBitmap;
878 MSIRECORD *rec = NULL;
879 IStream *stm = NULL;
880 IPicture *pic = NULL;
881 HDC srcdc, destdc;
882 BITMAP bm;
883 UINT r;
885 rec = msi_get_binary_record( db, name );
886 if( !rec )
887 goto end;
889 r = MSI_RecordGetIStream( rec, 2, &stm );
890 msiobj_release( &rec->hdr );
891 if( r != ERROR_SUCCESS )
892 goto end;
894 r = OleLoadPicture( stm, 0, TRUE, &IID_IPicture, (LPVOID*) &pic );
895 IStream_Release( stm );
896 if( FAILED( r ) )
898 ERR("failed to load picture\n");
899 goto end;
902 r = IPicture_get_Handle( pic, (OLE_HANDLE*) &hOleBitmap );
903 if( FAILED( r ) )
905 ERR("failed to get bitmap handle\n");
906 goto end;
909 /* make the bitmap the desired size */
910 r = GetObjectW( hOleBitmap, sizeof bm, &bm );
911 if (r != sizeof bm )
913 ERR("failed to get bitmap size\n");
914 goto end;
917 if (flags & LR_DEFAULTSIZE)
919 cx = bm.bmWidth;
920 cy = bm.bmHeight;
923 srcdc = CreateCompatibleDC( NULL );
924 hOldSrcBitmap = SelectObject( srcdc, hOleBitmap );
925 destdc = CreateCompatibleDC( NULL );
926 hBitmap = CreateCompatibleBitmap( srcdc, cx, cy );
927 hOldDestBitmap = SelectObject( destdc, hBitmap );
928 StretchBlt( destdc, 0, 0, cx, cy,
929 srcdc, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
930 SelectObject( srcdc, hOldSrcBitmap );
931 SelectObject( destdc, hOldDestBitmap );
932 DeleteDC( srcdc );
933 DeleteDC( destdc );
935 end:
936 if ( pic )
937 IPicture_Release( pic );
938 return hBitmap;
941 static UINT msi_dialog_bitmap_control( msi_dialog *dialog, MSIRECORD *rec )
943 UINT cx, cy, flags, style, attributes;
944 msi_control *control;
945 LPWSTR text;
947 flags = LR_LOADFROMFILE;
948 style = SS_BITMAP | SS_LEFT | WS_GROUP;
950 attributes = MSI_RecordGetInteger( rec, 8 );
951 if( attributes & msidbControlAttributesFixedSize )
953 flags |= LR_DEFAULTSIZE;
954 style |= SS_CENTERIMAGE;
957 control = msi_dialog_add_control( dialog, rec, szStatic, style );
958 cx = MSI_RecordGetInteger( rec, 6 );
959 cy = MSI_RecordGetInteger( rec, 7 );
960 cx = msi_dialog_scale_unit( dialog, cx );
961 cy = msi_dialog_scale_unit( dialog, cy );
963 text = msi_get_deformatted_field( dialog->package, rec, 10 );
964 control->hBitmap = msi_load_picture( dialog->package->db, text, cx, cy, flags );
965 if( control->hBitmap )
966 SendMessageW( control->hwnd, STM_SETIMAGE,
967 IMAGE_BITMAP, (LPARAM) control->hBitmap );
968 else
969 ERR("Failed to load bitmap %s\n", debugstr_w(text));
971 msi_free( text );
973 return ERROR_SUCCESS;
976 static UINT msi_dialog_icon_control( msi_dialog *dialog, MSIRECORD *rec )
978 msi_control *control;
979 DWORD attributes;
980 LPWSTR text;
982 TRACE("\n");
984 control = msi_dialog_add_control( dialog, rec, szStatic,
985 SS_ICON | SS_CENTERIMAGE | WS_GROUP );
987 attributes = MSI_RecordGetInteger( rec, 8 );
988 text = msi_get_deformatted_field( dialog->package, rec, 10 );
989 control->hIcon = msi_load_icon( dialog->package->db, text, attributes );
990 if( control->hIcon )
991 SendMessageW( control->hwnd, STM_SETICON, (WPARAM) control->hIcon, 0 );
992 else
993 ERR("Failed to load bitmap %s\n", debugstr_w(text));
994 msi_free( text );
995 return ERROR_SUCCESS;
998 static UINT msi_dialog_combo_control( msi_dialog *dialog, MSIRECORD *rec )
1000 static const WCHAR szCombo[] = { 'C','O','M','B','O','B','O','X',0 };
1002 msi_dialog_add_control( dialog, rec, szCombo,
1003 SS_BITMAP | SS_LEFT | SS_CENTERIMAGE );
1004 return ERROR_SUCCESS;
1007 static UINT msi_dialog_edit_control( msi_dialog *dialog, MSIRECORD *rec )
1009 msi_control *control;
1010 LPCWSTR prop;
1011 LPWSTR val;
1013 control = msi_dialog_add_control( dialog, rec, szEdit,
1014 WS_BORDER | WS_TABSTOP );
1015 control->handler = msi_dialog_edit_handler;
1016 prop = MSI_RecordGetString( rec, 9 );
1017 if( prop )
1018 control->property = strdupW( prop );
1019 val = msi_dup_property( dialog->package, control->property );
1020 SetWindowTextW( control->hwnd, val );
1021 msi_free( val );
1022 return ERROR_SUCCESS;
1025 /******************** Masked Edit ********************************************/
1027 #define MASK_MAX_GROUPS 10
1029 struct msi_mask_group
1031 UINT len;
1032 UINT ofs;
1033 WCHAR type;
1034 HWND hwnd;
1037 struct msi_maskedit_info
1039 msi_dialog *dialog;
1040 WNDPROC oldproc;
1041 HWND hwnd;
1042 LPWSTR prop;
1043 UINT num_chars;
1044 UINT num_groups;
1045 struct msi_mask_group group[MASK_MAX_GROUPS];
1048 static BOOL msi_mask_editable( WCHAR type )
1050 switch (type)
1052 case '%':
1053 case '#':
1054 case '&':
1055 case '`':
1056 case '?':
1057 case '^':
1058 return TRUE;
1060 return FALSE;
1063 static void msi_mask_control_change( struct msi_maskedit_info *info )
1065 LPWSTR val;
1066 UINT i, n, r;
1068 val = msi_alloc( (info->num_chars+1)*sizeof(WCHAR) );
1069 for( i=0, n=0; i<info->num_groups; i++ )
1071 if( (info->group[i].len + n) > info->num_chars )
1073 ERR("can't fit control %d text into template\n",i);
1074 break;
1076 if (!msi_mask_editable(info->group[i].type))
1078 for(r=0; r<info->group[i].len; r++)
1079 val[n+r] = info->group[i].type;
1080 val[n+r] = 0;
1082 else
1084 r = GetWindowTextW( info->group[i].hwnd, &val[n], info->group[i].len+1 );
1085 if( r != info->group[i].len )
1086 break;
1088 n += r;
1091 TRACE("%d/%d controls were good\n", i, info->num_groups);
1093 if( i == info->num_groups )
1095 TRACE("Set property %s to %s\n",
1096 debugstr_w(info->prop), debugstr_w(val) );
1097 CharUpperBuffW( val, info->num_chars );
1098 MSI_SetPropertyW( info->dialog->package, info->prop, val );
1099 msi_dialog_evaluate_control_conditions( info->dialog );
1101 msi_free( val );
1104 /* now move to the next control if necessary */
1105 static VOID msi_mask_next_control( struct msi_maskedit_info *info, HWND hWnd )
1107 HWND hWndNext;
1108 UINT len, i;
1110 for( i=0; i<info->num_groups; i++ )
1111 if( info->group[i].hwnd == hWnd )
1112 break;
1114 /* don't move from the last control */
1115 if( i >= (info->num_groups-1) )
1116 return;
1118 len = SendMessageW( hWnd, WM_GETTEXTLENGTH, 0, 0 );
1119 if( len < info->group[i].len )
1120 return;
1122 hWndNext = GetNextDlgTabItem( GetParent( hWnd ), hWnd, FALSE );
1123 SetFocus( hWndNext );
1126 static LRESULT WINAPI
1127 MSIMaskedEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1129 struct msi_maskedit_info *info;
1130 HRESULT r;
1132 TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
1134 info = GetPropW(hWnd, szButtonData);
1136 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1138 switch( msg )
1140 case WM_COMMAND:
1141 if (HIWORD(wParam) == EN_CHANGE)
1143 msi_mask_control_change( info );
1144 msi_mask_next_control( info, (HWND) lParam );
1146 break;
1147 case WM_NCDESTROY:
1148 msi_free( info->prop );
1149 msi_free( info );
1150 RemovePropW( hWnd, szButtonData );
1151 break;
1154 return r;
1157 /* fish the various bits of the property out and put them in the control */
1158 static void
1159 msi_maskedit_set_text( struct msi_maskedit_info *info, LPCWSTR text )
1161 LPCWSTR p;
1162 UINT i;
1164 p = text;
1165 for( i = 0; i < info->num_groups; i++ )
1167 if( info->group[i].len < lstrlenW( p ) )
1169 LPWSTR chunk = strdupW( p );
1170 chunk[ info->group[i].len ] = 0;
1171 SetWindowTextW( info->group[i].hwnd, chunk );
1172 msi_free( chunk );
1174 else
1176 SetWindowTextW( info->group[i].hwnd, p );
1177 break;
1179 p += info->group[i].len;
1183 static struct msi_maskedit_info * msi_dialog_parse_groups( LPCWSTR mask )
1185 struct msi_maskedit_info * info = NULL;
1186 int i = 0, n = 0, total = 0;
1187 LPCWSTR p;
1189 TRACE("masked control, template %s\n", debugstr_w(mask));
1191 if( !mask )
1192 return info;
1194 info = msi_alloc_zero( sizeof *info );
1195 if( !info )
1196 return info;
1198 p = strchrW(mask, '<');
1199 if( p )
1200 p++;
1201 else
1202 p = mask;
1204 for( i=0; i<MASK_MAX_GROUPS; i++ )
1206 /* stop at the end of the string */
1207 if( p[0] == 0 || p[0] == '>' )
1208 break;
1210 /* count the number of the same identifier */
1211 for( n=0; p[n] == p[0]; n++ )
1213 info->group[i].ofs = total;
1214 info->group[i].type = p[0];
1215 if( p[n] == '=' )
1217 n++;
1218 total++; /* an extra not part of the group */
1220 info->group[i].len = n;
1221 total += n;
1222 p += n;
1225 TRACE("%d characters in %d groups\n", total, i );
1226 if( i == MASK_MAX_GROUPS )
1227 ERR("too many groups in PIDTemplate %s\n", debugstr_w(mask));
1229 info->num_chars = total;
1230 info->num_groups = i;
1232 return info;
1235 static void
1236 msi_maskedit_create_children( struct msi_maskedit_info *info, LPCWSTR font )
1238 DWORD width, height, style, wx, ww;
1239 RECT rect;
1240 HWND hwnd;
1241 UINT i;
1243 style = WS_CHILD | WS_BORDER | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL;
1245 GetClientRect( info->hwnd, &rect );
1247 width = rect.right - rect.left;
1248 height = rect.bottom - rect.top;
1250 for( i = 0; i < info->num_groups; i++ )
1252 if (!msi_mask_editable( info->group[i].type ))
1253 continue;
1254 wx = (info->group[i].ofs * width) / info->num_chars;
1255 ww = (info->group[i].len * width) / info->num_chars;
1257 hwnd = CreateWindowW( szEdit, NULL, style, wx, 0, ww, height,
1258 info->hwnd, NULL, NULL, NULL );
1259 if( !hwnd )
1261 ERR("failed to create mask edit sub window\n");
1262 break;
1265 SendMessageW( hwnd, EM_LIMITTEXT, info->group[i].len, 0 );
1267 msi_dialog_set_font( info->dialog, hwnd,
1268 font?font:info->dialog->default_font );
1269 info->group[i].hwnd = hwnd;
1274 * office 2003 uses "73931<````=````=````=````=`````>@@@@@"
1275 * delphi 7 uses "<????-??????-??????-????>" and "<???-???>"
1276 * filemaker pro 7 uses "<^^^^=^^^^=^^^^=^^^^=^^^^=^^^^=^^^^^>"
1278 static UINT msi_dialog_maskedit_control( msi_dialog *dialog, MSIRECORD *rec )
1280 LPWSTR font_mask, val = NULL, font;
1281 struct msi_maskedit_info *info = NULL;
1282 UINT ret = ERROR_SUCCESS;
1283 msi_control *control;
1284 LPCWSTR prop, mask;
1286 TRACE("\n");
1288 font_mask = msi_get_deformatted_field( dialog->package, rec, 10 );
1289 font = msi_dialog_get_style( font_mask, &mask );
1290 if( !mask )
1292 ERR("mask template is empty\n");
1293 goto end;
1296 info = msi_dialog_parse_groups( mask );
1297 if( !info )
1299 ERR("template %s is invalid\n", debugstr_w(mask));
1300 goto end;
1303 info->dialog = dialog;
1305 control = msi_dialog_add_control( dialog, rec, szStatic,
1306 SS_OWNERDRAW | WS_GROUP | WS_VISIBLE );
1307 if( !control )
1309 ERR("Failed to create maskedit container\n");
1310 ret = ERROR_FUNCTION_FAILED;
1311 goto end;
1313 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
1315 info->hwnd = control->hwnd;
1317 /* subclass the static control */
1318 info->oldproc = (WNDPROC) SetWindowLongPtrW( info->hwnd, GWLP_WNDPROC,
1319 (LONG_PTR)MSIMaskedEdit_WndProc );
1320 SetPropW( control->hwnd, szButtonData, info );
1322 prop = MSI_RecordGetString( rec, 9 );
1323 if( prop )
1324 info->prop = strdupW( prop );
1326 msi_maskedit_create_children( info, font );
1328 if( prop )
1330 val = msi_dup_property( dialog->package, prop );
1331 if( val )
1333 msi_maskedit_set_text( info, val );
1334 msi_free( val );
1338 end:
1339 if( ret != ERROR_SUCCESS )
1340 msi_free( info );
1341 msi_free( font_mask );
1342 msi_free( font );
1343 return ret;
1346 /******************** Progress Bar *****************************************/
1348 static UINT msi_dialog_progress_bar( msi_dialog *dialog, MSIRECORD *rec )
1350 msi_dialog_add_control( dialog, rec, PROGRESS_CLASSW, WS_VISIBLE );
1351 return ERROR_SUCCESS;
1354 /******************** Path Edit ********************************************/
1356 static UINT msi_dialog_pathedit_control( msi_dialog *dialog, MSIRECORD *rec )
1358 FIXME("not implemented properly\n");
1359 return msi_dialog_edit_control( dialog, rec );
1362 /* radio buttons are a bit different from normal controls */
1363 static UINT msi_dialog_create_radiobutton( MSIRECORD *rec, LPVOID param )
1365 radio_button_group_descr *group = (radio_button_group_descr *)param;
1366 msi_dialog *dialog = group->dialog;
1367 msi_control *control;
1368 LPCWSTR prop, text, name;
1369 DWORD style, attributes = group->attributes;
1371 style = WS_CHILD | BS_AUTORADIOBUTTON | BS_MULTILINE | WS_TABSTOP;
1372 name = MSI_RecordGetString( rec, 3 );
1373 text = MSI_RecordGetString( rec, 8 );
1374 if( attributes & 1 )
1375 style |= WS_VISIBLE;
1376 if( ~attributes & 2 )
1377 style |= WS_DISABLED;
1379 control = msi_dialog_create_window( dialog, rec, 0, szButton, name, text,
1380 style, group->parent->hwnd );
1381 if (!control)
1382 return ERROR_FUNCTION_FAILED;
1383 control->handler = msi_dialog_radiogroup_handler;
1385 if (!lstrcmpW(control->name, group->propval))
1386 SendMessageW(control->hwnd, BM_SETCHECK, BST_CHECKED, 0);
1388 prop = MSI_RecordGetString( rec, 1 );
1389 if( prop )
1390 control->property = strdupW( prop );
1392 return ERROR_SUCCESS;
1395 static UINT msi_dialog_radiogroup_control( msi_dialog *dialog, MSIRECORD *rec )
1397 static const WCHAR query[] = {
1398 'S','E','L','E','C','T',' ','*',' ',
1399 'F','R','O','M',' ','R','a','d','i','o','B','u','t','t','o','n',' ',
1400 'W','H','E','R','E',' ',
1401 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
1402 UINT r;
1403 LPCWSTR prop;
1404 msi_control *control;
1405 MSIQUERY *view = NULL;
1406 radio_button_group_descr group;
1407 MSIPACKAGE *package = dialog->package;
1408 WNDPROC oldproc;
1410 prop = MSI_RecordGetString( rec, 9 );
1412 TRACE("%p %p %s\n", dialog, rec, debugstr_w( prop ));
1414 /* Create parent group box to hold radio buttons */
1415 control = msi_dialog_add_control( dialog, rec, szButton, BS_OWNERDRAW|WS_GROUP );
1416 if( !control )
1417 return ERROR_FUNCTION_FAILED;
1419 oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1420 (LONG_PTR)MSIRadioGroup_WndProc );
1421 SetPropW(control->hwnd, szButtonData, oldproc);
1422 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
1424 if( prop )
1425 control->property = strdupW( prop );
1427 /* query the Radio Button table for all control in this group */
1428 r = MSI_OpenQuery( package->db, &view, query, prop );
1429 if( r != ERROR_SUCCESS )
1431 ERR("query failed for dialog %s radio group %s\n",
1432 debugstr_w(dialog->name), debugstr_w(prop));
1433 return ERROR_INVALID_PARAMETER;
1436 group.dialog = dialog;
1437 group.parent = control;
1438 group.attributes = MSI_RecordGetInteger( rec, 8 );
1439 group.propval = msi_dup_property( dialog->package, control->property );
1441 r = MSI_IterateRecords( view, 0, msi_dialog_create_radiobutton, &group );
1442 msiobj_release( &view->hdr );
1443 msi_free( group.propval );
1445 return r;
1448 /******************** Selection Tree ***************************************/
1450 struct msi_selection_tree_info
1452 msi_dialog *dialog;
1453 HWND hwnd;
1454 WNDPROC oldproc;
1457 static void
1458 msi_seltree_sync_item_state( HWND hwnd, MSIFEATURE *feature, HTREEITEM hItem )
1460 TVITEMW tvi;
1462 TRACE("Feature %s -> %d %d %d\n", debugstr_w(feature->Title),
1463 feature->Installed, feature->Action, feature->ActionRequest);
1465 tvi.mask = TVIF_STATE;
1466 tvi.hItem = hItem;
1467 tvi.state = INDEXTOSTATEIMAGEMASK( feature->Action );
1468 tvi.stateMask = TVIS_STATEIMAGEMASK;
1470 SendMessageW( hwnd, TVM_SETITEMW, 0, (LPARAM) &tvi );
1473 static UINT
1474 msi_seltree_popup_menu( HWND hwnd, INT x, INT y )
1476 HMENU hMenu;
1477 INT r;
1479 /* create a menu to display */
1480 hMenu = CreatePopupMenu();
1482 /* FIXME: load strings from resources */
1483 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_LOCAL, "Install feature locally");
1484 AppendMenuA( hMenu, MF_GRAYED, 0x1000, "Install entire feature");
1485 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ADVERTISED, "Install on demand");
1486 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ABSENT, "Don't install");
1487 r = TrackPopupMenu( hMenu, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RETURNCMD,
1488 x, y, 0, hwnd, NULL );
1489 DestroyMenu( hMenu );
1490 return r;
1493 static MSIFEATURE *
1494 msi_seltree_feature_from_item( HWND hwnd, HTREEITEM hItem )
1496 TVITEMW tvi;
1498 /* get the feature from the item */
1499 memset( &tvi, 0, sizeof tvi );
1500 tvi.hItem = hItem;
1501 tvi.mask = TVIF_PARAM | TVIF_HANDLE;
1502 SendMessageW( hwnd, TVM_GETITEMW, 0, (LPARAM) &tvi );
1504 return (MSIFEATURE*) tvi.lParam;
1507 static LRESULT
1508 msi_seltree_menu( HWND hwnd, HTREEITEM hItem )
1510 MSIFEATURE *feature;
1511 union {
1512 RECT rc;
1513 POINT pt[2];
1514 HTREEITEM hItem;
1515 } u;
1516 UINT r;
1518 feature = msi_seltree_feature_from_item( hwnd, hItem );
1519 if (!feature)
1521 ERR("item %p feature was NULL\n", hItem);
1522 return 0;
1525 /* get the item's rectangle to put the menu just below it */
1526 u.hItem = hItem;
1527 SendMessageW( hwnd, TVM_GETITEMRECT, 0, (LPARAM) &u.rc );
1528 MapWindowPoints( hwnd, NULL, u.pt, 2 );
1530 r = msi_seltree_popup_menu( hwnd, u.rc.left, u.rc.top );
1532 switch (r)
1534 case INSTALLSTATE_LOCAL:
1535 case INSTALLSTATE_ADVERTISED:
1536 case INSTALLSTATE_ABSENT:
1537 feature->ActionRequest = r;
1538 feature->Action = r;
1539 break;
1540 default:
1541 FIXME("select feature and all children\n");
1544 /* update */
1545 msi_seltree_sync_item_state( hwnd, feature, hItem );
1547 return 0;
1550 static LRESULT WINAPI
1551 MSISelectionTree_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1553 struct msi_selection_tree_info *info;
1554 TVHITTESTINFO tvhti;
1555 HRESULT r;
1557 TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
1559 info = GetPropW(hWnd, szButtonData);
1561 switch( msg )
1563 case WM_LBUTTONDOWN:
1564 tvhti.pt.x = LOWORD( lParam );
1565 tvhti.pt.y = HIWORD( lParam );
1566 tvhti.flags = 0;
1567 tvhti.hItem = 0;
1568 r = CallWindowProcW(info->oldproc, hWnd, TVM_HITTEST, 0, (LPARAM) &tvhti );
1569 if (tvhti.flags & TVHT_ONITEMSTATEICON)
1570 return msi_seltree_menu( hWnd, tvhti.hItem );
1571 break;
1574 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1576 switch( msg )
1578 case WM_NCDESTROY:
1579 msi_free( info );
1580 RemovePropW( hWnd, szButtonData );
1581 break;
1583 return r;
1586 static void
1587 msi_seltree_add_child_features( MSIPACKAGE *package, HWND hwnd,
1588 LPCWSTR parent, HTREEITEM hParent )
1590 MSIFEATURE *feature;
1591 TVINSERTSTRUCTW tvis;
1592 HTREEITEM hitem;
1594 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
1596 if ( lstrcmpW( parent, feature->Feature_Parent ) )
1597 continue;
1599 if ( !feature->Title )
1600 continue;
1602 memset( &tvis, 0, sizeof tvis );
1603 tvis.hParent = hParent;
1604 tvis.hInsertAfter = TVI_SORT;
1605 tvis.u.item.mask = TVIF_TEXT | TVIF_PARAM;
1606 tvis.u.item.pszText = feature->Title;
1607 tvis.u.item.lParam = (LPARAM) feature;
1609 hitem = (HTREEITEM) SendMessageW( hwnd, TVM_INSERTITEMW, 0, (LPARAM) &tvis );
1610 if (!hitem)
1611 continue;
1613 msi_seltree_sync_item_state( hwnd, feature, hitem );
1614 msi_seltree_add_child_features( package, hwnd,
1615 feature->Feature, hitem );
1619 static void msi_seltree_create_imagelist( HWND hwnd )
1621 const int bm_width = 32, bm_height = 16, bm_count = 3;
1622 const int bm_resource = 0x1001;
1623 HIMAGELIST himl;
1624 int i;
1625 HBITMAP hbmp;
1627 himl = ImageList_Create( bm_width, bm_height, FALSE, 4, 0 );
1628 if (!himl)
1630 ERR("failed to create image list\n");
1631 return;
1634 for (i=0; i<bm_count; i++)
1636 hbmp = LoadBitmapW( msi_hInstance, MAKEINTRESOURCEW(i+bm_resource) );
1637 if (!hbmp)
1639 ERR("failed to load bitmap %d\n", i);
1640 break;
1644 * Add a dummy bitmap at offset zero because the treeview
1645 * can't use it as a state mask (zero means no user state).
1647 if (!i)
1648 ImageList_Add( himl, hbmp, NULL );
1650 ImageList_Add( himl, hbmp, NULL );
1653 SendMessageW( hwnd, TVM_SETIMAGELIST, TVSIL_STATE, (LPARAM)himl );
1656 static UINT msi_dialog_selection_tree( msi_dialog *dialog, MSIRECORD *rec )
1658 msi_control *control;
1659 LPCWSTR prop;
1660 MSIPACKAGE *package = dialog->package;
1661 DWORD style;
1662 struct msi_selection_tree_info *info;
1664 info = msi_alloc( sizeof *info );
1665 if (!info)
1666 return ERROR_FUNCTION_FAILED;
1668 /* create the treeview control */
1669 prop = MSI_RecordGetString( rec, 9 );
1670 style = TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT;
1671 style |= WS_GROUP | WS_VSCROLL;
1672 control = msi_dialog_add_control( dialog, rec, WC_TREEVIEWW, style );
1673 if (!control)
1675 msi_free(info);
1676 return ERROR_FUNCTION_FAILED;
1679 /* subclass */
1680 info->dialog = dialog;
1681 info->hwnd = control->hwnd;
1682 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1683 (LONG_PTR)MSISelectionTree_WndProc );
1684 SetPropW( control->hwnd, szButtonData, info );
1686 /* initialize it */
1687 msi_seltree_create_imagelist( control->hwnd );
1688 msi_seltree_add_child_features( package, control->hwnd, NULL, NULL );
1690 return ERROR_SUCCESS;
1693 /******************** Group Box ***************************************/
1695 static UINT msi_dialog_group_box( msi_dialog *dialog, MSIRECORD *rec )
1697 msi_control *control;
1698 DWORD style;
1700 style = BS_GROUPBOX | WS_CHILD | WS_GROUP;
1701 control = msi_dialog_add_control( dialog, rec, WC_BUTTONW, style );
1702 if (!control)
1703 return ERROR_FUNCTION_FAILED;
1705 return ERROR_SUCCESS;
1708 static const struct control_handler msi_dialog_handler[] =
1710 { szText, msi_dialog_text_control },
1711 { szPushButton, msi_dialog_button_control },
1712 { szLine, msi_dialog_line_control },
1713 { szBitmap, msi_dialog_bitmap_control },
1714 { szCheckBox, msi_dialog_checkbox_control },
1715 { szScrollableText, msi_dialog_scrolltext_control },
1716 { szComboBox, msi_dialog_combo_control },
1717 { szEdit, msi_dialog_edit_control },
1718 { szMaskedEdit, msi_dialog_maskedit_control },
1719 { szPathEdit, msi_dialog_pathedit_control },
1720 { szProgressBar, msi_dialog_progress_bar },
1721 { szRadioButtonGroup, msi_dialog_radiogroup_control },
1722 { szIcon, msi_dialog_icon_control },
1723 { szSelectionTree, msi_dialog_selection_tree },
1724 { szGroupBox, msi_dialog_group_box },
1727 #define NUM_CONTROL_TYPES (sizeof msi_dialog_handler/sizeof msi_dialog_handler[0])
1729 static UINT msi_dialog_create_controls( MSIRECORD *rec, LPVOID param )
1731 msi_dialog *dialog = param;
1732 LPCWSTR control_type;
1733 UINT i;
1735 /* find and call the function that can create this type of control */
1736 control_type = MSI_RecordGetString( rec, 3 );
1737 for( i=0; i<NUM_CONTROL_TYPES; i++ )
1738 if (!strcmpiW( msi_dialog_handler[i].control_type, control_type ))
1739 break;
1740 if( i != NUM_CONTROL_TYPES )
1741 msi_dialog_handler[i].func( dialog, rec );
1742 else
1743 ERR("no handler for element type %s\n", debugstr_w(control_type));
1745 return ERROR_SUCCESS;
1748 static UINT msi_dialog_fill_controls( msi_dialog *dialog )
1750 static const WCHAR query[] = {
1751 'S','E','L','E','C','T',' ','*',' ',
1752 'F','R','O','M',' ','C','o','n','t','r','o','l',' ',
1753 'W','H','E','R','E',' ',
1754 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
1755 UINT r;
1756 MSIQUERY *view = NULL;
1757 MSIPACKAGE *package = dialog->package;
1759 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
1761 /* query the Control table for all the elements of the control */
1762 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
1763 if( r != ERROR_SUCCESS )
1765 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
1766 return ERROR_INVALID_PARAMETER;
1769 r = MSI_IterateRecords( view, 0, msi_dialog_create_controls, dialog );
1770 msiobj_release( &view->hdr );
1772 return r;
1775 static UINT msi_dialog_set_control_condition( MSIRECORD *rec, LPVOID param )
1777 static const WCHAR szHide[] = { 'H','i','d','e',0 };
1778 static const WCHAR szShow[] = { 'S','h','o','w',0 };
1779 static const WCHAR szDisable[] = { 'D','i','s','a','b','l','e',0 };
1780 static const WCHAR szEnable[] = { 'E','n','a','b','l','e',0 };
1781 msi_dialog *dialog = param;
1782 msi_control *control;
1783 LPCWSTR name, action, condition;
1784 UINT r;
1786 name = MSI_RecordGetString( rec, 2 );
1787 action = MSI_RecordGetString( rec, 3 );
1788 condition = MSI_RecordGetString( rec, 4 );
1789 r = MSI_EvaluateConditionW( dialog->package, condition );
1790 control = msi_dialog_find_control( dialog, name );
1791 if( r == MSICONDITION_TRUE && control )
1793 TRACE("%s control %s\n", debugstr_w(action), debugstr_w(name));
1795 /* FIXME: case sensitive? */
1796 if(!lstrcmpW(action, szHide))
1797 ShowWindow(control->hwnd, SW_HIDE);
1798 else if(!strcmpW(action, szShow))
1799 ShowWindow(control->hwnd, SW_SHOW);
1800 else if(!strcmpW(action, szDisable))
1801 EnableWindow(control->hwnd, FALSE);
1802 else if(!strcmpW(action, szEnable))
1803 EnableWindow(control->hwnd, TRUE);
1804 else
1805 FIXME("Unhandled action %s\n", debugstr_w(action));
1808 return ERROR_SUCCESS;
1811 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog )
1813 static const WCHAR query[] = {
1814 'S','E','L','E','C','T',' ','*',' ',
1815 'F','R','O','M',' ',
1816 'C','o','n','t','r','o','l','C','o','n','d','i','t','i','o','n',' ',
1817 'W','H','E','R','E',' ',
1818 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0
1820 UINT r;
1821 MSIQUERY *view = NULL;
1822 MSIPACKAGE *package = dialog->package;
1824 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
1826 /* query the Control table for all the elements of the control */
1827 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
1828 if( r != ERROR_SUCCESS )
1830 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
1831 return ERROR_INVALID_PARAMETER;
1834 r = MSI_IterateRecords( view, 0, msi_dialog_set_control_condition, dialog );
1835 msiobj_release( &view->hdr );
1837 return r;
1840 UINT msi_dialog_reset( msi_dialog *dialog )
1842 /* FIXME: should restore the original values of any properties we changed */
1843 return msi_dialog_evaluate_control_conditions( dialog );
1846 /* figure out the height of 10 point MS Sans Serif */
1847 static INT msi_dialog_get_sans_serif_height( HWND hwnd )
1849 static const WCHAR szSansSerif[] = {
1850 'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0 };
1851 LOGFONTW lf;
1852 TEXTMETRICW tm;
1853 BOOL r;
1854 LONG height = 0;
1855 HFONT hFont, hOldFont;
1856 HDC hdc;
1858 hdc = GetDC( hwnd );
1859 if (hdc)
1861 memset( &lf, 0, sizeof lf );
1862 lf.lfHeight = MulDiv(10, GetDeviceCaps(hdc, LOGPIXELSY), 72);
1863 strcpyW( lf.lfFaceName, szSansSerif );
1864 hFont = CreateFontIndirectW(&lf);
1865 if (hFont)
1867 hOldFont = SelectObject( hdc, hFont );
1868 r = GetTextMetricsW( hdc, &tm );
1869 if (r)
1870 height = tm.tmHeight;
1871 SelectObject( hdc, hOldFont );
1872 DeleteObject( hFont );
1874 ReleaseDC( hwnd, hdc );
1876 return height;
1879 /* fetch the associated record from the Dialog table */
1880 static MSIRECORD *msi_get_dialog_record( msi_dialog *dialog )
1882 static const WCHAR query[] = {
1883 'S','E','L','E','C','T',' ','*',' ',
1884 'F','R','O','M',' ','D','i','a','l','o','g',' ',
1885 'W','H','E','R','E',' ',
1886 '`','D','i','a','l','o','g','`',' ','=',' ','\'','%','s','\'',0};
1887 MSIPACKAGE *package = dialog->package;
1888 MSIRECORD *rec = NULL;
1890 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
1892 rec = MSI_QueryGetRecord( package->db, query, dialog->name );
1893 if( !rec )
1894 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
1896 return rec;
1899 static void msi_dialog_adjust_dialog_size( msi_dialog *dialog, LPSIZE sz )
1901 RECT rect;
1902 LONG style;
1904 /* turn the client size into the window rectangle */
1905 rect.left = 0;
1906 rect.top = 0;
1907 rect.right = msi_dialog_scale_unit( dialog, sz->cx );
1908 rect.bottom = msi_dialog_scale_unit( dialog, sz->cy );
1909 style = GetWindowLongPtrW( dialog->hwnd, GWL_STYLE );
1910 AdjustWindowRect( &rect, style, FALSE );
1911 sz->cx = rect.right - rect.left;
1912 sz->cy = rect.bottom - rect.top;
1915 static BOOL msi_control_set_next( msi_control *control, msi_control *next )
1917 return SetWindowPos( next->hwnd, control->hwnd, 0, 0, 0, 0,
1918 SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOREDRAW |
1919 SWP_NOREPOSITION | SWP_NOSENDCHANGING | SWP_NOSIZE );
1922 static UINT msi_dialog_set_tab_order( msi_dialog *dialog )
1924 msi_control *control, *tab_next;
1926 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
1928 tab_next = msi_dialog_find_control( dialog, control->tabnext );
1929 if( !tab_next )
1930 continue;
1931 msi_control_set_next( control, tab_next );
1934 return ERROR_SUCCESS;
1937 static void msi_dialog_set_first_control( msi_dialog* dialog, LPCWSTR name )
1939 msi_control *control;
1941 control = msi_dialog_find_control( dialog, name );
1942 if( control )
1943 dialog->hWndFocus = control->hwnd;
1944 else
1945 dialog->hWndFocus = NULL;
1948 static LRESULT msi_dialog_oncreate( HWND hwnd, LPCREATESTRUCTW cs )
1950 static const WCHAR df[] = {
1951 'D','e','f','a','u','l','t','U','I','F','o','n','t',0 };
1952 static const WCHAR dfv[] = {
1953 'M','S',' ','S','h','e','l','l',' ','D','l','g',0 };
1954 msi_dialog *dialog = (msi_dialog*) cs->lpCreateParams;
1955 MSIRECORD *rec = NULL;
1956 LPWSTR title = NULL;
1957 SIZE size;
1959 TRACE("%p %p\n", dialog, dialog->package);
1961 dialog->hwnd = hwnd;
1962 SetWindowLongPtrW( hwnd, GWLP_USERDATA, (LONG_PTR) dialog );
1964 rec = msi_get_dialog_record( dialog );
1965 if( !rec )
1967 TRACE("No record found for dialog %s\n", debugstr_w(dialog->name));
1968 return -1;
1971 dialog->scale = msi_dialog_get_sans_serif_height(dialog->hwnd);
1973 size.cx = MSI_RecordGetInteger( rec, 4 );
1974 size.cy = MSI_RecordGetInteger( rec, 5 );
1975 msi_dialog_adjust_dialog_size( dialog, &size );
1977 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
1979 dialog->default_font = msi_dup_property( dialog->package, df );
1980 if (!dialog->default_font)
1982 dialog->default_font = strdupW(dfv);
1983 if (!dialog->default_font) return -1;
1986 title = msi_get_deformatted_field( dialog->package, rec, 7 );
1987 SetWindowTextW( hwnd, title );
1988 msi_free( title );
1990 SetWindowPos( hwnd, 0, 0, 0, size.cx, size.cy,
1991 SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW );
1993 msi_dialog_build_font_list( dialog );
1994 msi_dialog_fill_controls( dialog );
1995 msi_dialog_evaluate_control_conditions( dialog );
1996 msi_dialog_set_tab_order( dialog );
1997 msi_dialog_set_first_control( dialog, MSI_RecordGetString( rec, 8 ) );
1998 msiobj_release( &rec->hdr );
2000 return 0;
2003 static UINT msi_dialog_send_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
2005 LPWSTR event_fmt = NULL, arg_fmt = NULL;
2007 TRACE("Sending control event %s %s\n", debugstr_w(event), debugstr_w(arg));
2009 deformat_string( dialog->package, event, &event_fmt );
2010 deformat_string( dialog->package, arg, &arg_fmt );
2012 dialog->event_handler( dialog->package, event_fmt, arg_fmt, dialog );
2014 msi_free( event_fmt );
2015 msi_free( arg_fmt );
2017 return ERROR_SUCCESS;
2020 static UINT msi_dialog_set_property( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
2022 static const WCHAR szNullArg[] = { '{','}',0 };
2023 LPWSTR p, prop, arg_fmt = NULL;
2024 UINT len;
2026 len = strlenW(event);
2027 prop = msi_alloc( len*sizeof(WCHAR));
2028 strcpyW( prop, &event[1] );
2029 p = strchrW( prop, ']' );
2030 if( p && p[1] == 0 )
2032 *p = 0;
2033 if( strcmpW( szNullArg, arg ) )
2034 deformat_string( dialog->package, arg, &arg_fmt );
2035 MSI_SetPropertyW( dialog->package, prop, arg_fmt );
2036 msi_free( arg_fmt );
2038 else
2039 ERR("Badly formatted property string - what happens?\n");
2040 msi_free( prop );
2041 return ERROR_SUCCESS;
2044 static UINT msi_dialog_control_event( MSIRECORD *rec, LPVOID param )
2046 msi_dialog *dialog = param;
2047 LPCWSTR condition, event, arg;
2048 UINT r;
2050 condition = MSI_RecordGetString( rec, 5 );
2051 r = MSI_EvaluateConditionW( dialog->package, condition );
2052 if( r == MSICONDITION_TRUE || r == MSICONDITION_NONE )
2054 event = MSI_RecordGetString( rec, 3 );
2055 arg = MSI_RecordGetString( rec, 4 );
2056 if( event[0] == '[' )
2057 msi_dialog_set_property( dialog, event, arg );
2058 else
2059 msi_dialog_send_event( dialog, event, arg );
2062 return ERROR_SUCCESS;
2065 static UINT msi_dialog_button_handler( msi_dialog *dialog,
2066 msi_control *control, WPARAM param )
2068 static const WCHAR query[] = {
2069 'S','E','L','E','C','T',' ','*',' ',
2070 'F','R','O','M',' ','C','o','n','t','r','o','l','E','v','e','n','t',' ',
2071 'W','H','E','R','E',' ',
2072 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
2073 'A','N','D',' ',
2074 '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',' ',
2075 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','i','n','g','`',0
2077 MSIQUERY *view = NULL;
2078 UINT r;
2080 if( HIWORD(param) != BN_CLICKED )
2081 return ERROR_SUCCESS;
2083 r = MSI_OpenQuery( dialog->package->db, &view, query,
2084 dialog->name, control->name );
2085 if( r != ERROR_SUCCESS )
2087 ERR("query failed\n");
2088 return 0;
2091 r = MSI_IterateRecords( view, 0, msi_dialog_control_event, dialog );
2092 msiobj_release( &view->hdr );
2094 return r;
2097 static UINT msi_dialog_get_checkbox_state( msi_dialog *dialog,
2098 msi_control *control )
2100 WCHAR state[2] = { 0 };
2101 DWORD sz = 2;
2103 MSI_GetPropertyW( dialog->package, control->property, state, &sz );
2104 return state[0] ? 1 : 0;
2107 static void msi_dialog_set_checkbox_state( msi_dialog *dialog,
2108 msi_control *control, UINT state )
2110 static const WCHAR szState[] = { '1', 0 };
2111 LPCWSTR val;
2113 /* if uncheck then the property is set to NULL */
2114 if (!state)
2116 MSI_SetPropertyW( dialog->package, control->property, NULL );
2117 return;
2120 /* check for a custom state */
2121 if (control->value && control->value[0])
2122 val = control->value;
2123 else
2124 val = szState;
2126 MSI_SetPropertyW( dialog->package, control->property, val );
2129 static void msi_dialog_checkbox_sync_state( msi_dialog *dialog,
2130 msi_control *control )
2132 UINT state;
2134 state = msi_dialog_get_checkbox_state( dialog, control );
2135 SendMessageW( control->hwnd, BM_SETCHECK,
2136 state ? BST_CHECKED : BST_UNCHECKED, 0 );
2139 static UINT msi_dialog_checkbox_handler( msi_dialog *dialog,
2140 msi_control *control, WPARAM param )
2142 UINT state;
2144 if( HIWORD(param) != BN_CLICKED )
2145 return ERROR_SUCCESS;
2147 TRACE("clicked checkbox %s, set %s\n", debugstr_w(control->name),
2148 debugstr_w(control->property));
2150 state = msi_dialog_get_checkbox_state( dialog, control );
2151 state = state ? 0 : 1;
2152 msi_dialog_set_checkbox_state( dialog, control, state );
2153 msi_dialog_checkbox_sync_state( dialog, control );
2155 return msi_dialog_button_handler( dialog, control, param );
2158 static UINT msi_dialog_edit_handler( msi_dialog *dialog,
2159 msi_control *control, WPARAM param )
2161 UINT sz, r;
2162 LPWSTR buf;
2164 if( HIWORD(param) != EN_CHANGE )
2165 return ERROR_SUCCESS;
2167 TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
2168 debugstr_w(control->property));
2170 sz = 0x20;
2171 buf = msi_alloc( sz*sizeof(WCHAR) );
2172 while( buf )
2174 r = GetWindowTextW( control->hwnd, buf, sz );
2175 if( r < (sz-1) )
2176 break;
2177 sz *= 2;
2178 buf = msi_realloc( buf, sz*sizeof(WCHAR) );
2181 MSI_SetPropertyW( dialog->package, control->property, buf );
2183 msi_free( buf );
2185 return ERROR_SUCCESS;
2188 static UINT msi_dialog_radiogroup_handler( msi_dialog *dialog,
2189 msi_control *control, WPARAM param )
2191 if( HIWORD(param) != BN_CLICKED )
2192 return ERROR_SUCCESS;
2194 TRACE("clicked radio button %s, set %s\n", debugstr_w(control->name),
2195 debugstr_w(control->property));
2197 MSI_SetPropertyW( dialog->package, control->property, control->name );
2199 return msi_dialog_button_handler( dialog, control, param );
2202 static LRESULT msi_dialog_oncommand( msi_dialog *dialog, WPARAM param, HWND hwnd )
2204 msi_control *control = NULL;
2206 TRACE("%p %p %08x\n", dialog, hwnd, param);
2208 switch (param)
2210 case 1: /* enter */
2211 control = msi_dialog_find_control( dialog, dialog->control_default );
2212 break;
2213 case 2: /* escape */
2214 control = msi_dialog_find_control( dialog, dialog->control_cancel );
2215 break;
2216 default:
2217 control = msi_dialog_find_control_by_hwnd( dialog, hwnd );
2220 if( control )
2222 if( control->handler )
2224 control->handler( dialog, control, param );
2225 msi_dialog_evaluate_control_conditions( dialog );
2228 else
2229 ERR("button click from nowhere %p %d %p\n", dialog, param, hwnd);
2230 return 0;
2233 static void msi_dialog_setfocus( msi_dialog *dialog )
2235 HWND hwnd = dialog->hWndFocus;
2237 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, TRUE);
2238 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, FALSE);
2239 SetFocus( hwnd );
2240 dialog->hWndFocus = hwnd;
2243 static LRESULT WINAPI MSIDialog_WndProc( HWND hwnd, UINT msg,
2244 WPARAM wParam, LPARAM lParam )
2246 msi_dialog *dialog = (LPVOID) GetWindowLongPtrW( hwnd, GWLP_USERDATA );
2248 TRACE("0x%04x\n", msg);
2250 switch (msg)
2252 case WM_CREATE:
2253 return msi_dialog_oncreate( hwnd, (LPCREATESTRUCTW)lParam );
2255 case WM_COMMAND:
2256 return msi_dialog_oncommand( dialog, wParam, (HWND)lParam );
2258 case WM_ACTIVATE:
2259 if( LOWORD(wParam) == WA_INACTIVE )
2260 dialog->hWndFocus = GetFocus();
2261 else
2262 msi_dialog_setfocus( dialog );
2263 return 0;
2265 case WM_SETFOCUS:
2266 msi_dialog_setfocus( dialog );
2267 return 0;
2269 /* bounce back to our subclassed static control */
2270 case WM_CTLCOLORSTATIC:
2271 return SendMessageW( (HWND) lParam, WM_CTLCOLORSTATIC, wParam, lParam );
2273 case WM_DESTROY:
2274 dialog->hwnd = NULL;
2275 return 0;
2277 return DefWindowProcW(hwnd, msg, wParam, lParam);
2280 static BOOL CALLBACK msi_radioground_child_enum( HWND hWnd, LPARAM lParam )
2282 EnableWindow( hWnd, lParam );
2283 return TRUE;
2286 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2288 WNDPROC oldproc = (WNDPROC) GetPropW(hWnd, szButtonData);
2289 LRESULT r;
2291 TRACE("hWnd %p msg %04x wParam 0x%08x lParam 0x%08lx\n", hWnd, msg, wParam, lParam);
2293 if (msg == WM_COMMAND) /* Forward notifications to dialog */
2294 SendMessageW(GetParent(hWnd), msg, wParam, lParam);
2296 r = CallWindowProcW(oldproc, hWnd, msg, wParam, lParam);
2298 /* make sure the radio buttons show as disabled if the parent is disabled */
2299 if (msg == WM_ENABLE)
2300 EnumChildWindows( hWnd, msi_radioground_child_enum, wParam );
2302 return r;
2305 static LRESULT WINAPI MSIHiddenWindowProc( HWND hwnd, UINT msg,
2306 WPARAM wParam, LPARAM lParam )
2308 msi_dialog *dialog = (msi_dialog*) lParam;
2310 TRACE("%d %p\n", msg, dialog);
2312 switch (msg)
2314 case WM_MSI_DIALOG_CREATE:
2315 return msi_dialog_run_message_loop( dialog );
2316 case WM_MSI_DIALOG_DESTROY:
2317 msi_dialog_destroy( dialog );
2318 return 0;
2320 return DefWindowProcW( hwnd, msg, wParam, lParam );
2323 /* functions that interface to other modules within MSI */
2325 msi_dialog *msi_dialog_create( MSIPACKAGE* package, LPCWSTR szDialogName,
2326 msi_dialog_event_handler event_handler )
2328 MSIRECORD *rec = NULL;
2329 msi_dialog *dialog;
2331 TRACE("%p %s\n", package, debugstr_w(szDialogName));
2333 /* allocate the structure for the dialog to use */
2334 dialog = msi_alloc_zero( sizeof *dialog + sizeof(WCHAR)*strlenW(szDialogName) );
2335 if( !dialog )
2336 return NULL;
2337 strcpyW( dialog->name, szDialogName );
2338 msiobj_addref( &package->hdr );
2339 dialog->package = package;
2340 dialog->event_handler = event_handler;
2341 dialog->finished = 0;
2342 list_init( &dialog->controls );
2344 /* verify that the dialog exists */
2345 rec = msi_get_dialog_record( dialog );
2346 if( !rec )
2348 msiobj_release( &package->hdr );
2349 msi_free( dialog );
2350 return NULL;
2352 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
2353 dialog->control_default = strdupW( MSI_RecordGetString( rec, 9 ) );
2354 dialog->control_cancel = strdupW( MSI_RecordGetString( rec, 10 ) );
2355 msiobj_release( &rec->hdr );
2357 return dialog;
2360 static void msi_process_pending_messages( HWND hdlg )
2362 MSG msg;
2364 while( PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ) )
2366 if( hdlg && IsDialogMessageW( hdlg, &msg ))
2367 continue;
2368 TranslateMessage( &msg );
2369 DispatchMessageW( &msg );
2373 void msi_dialog_end_dialog( msi_dialog *dialog )
2375 TRACE("%p\n", dialog);
2376 dialog->finished = 1;
2377 PostMessageW(dialog->hwnd, WM_NULL, 0, 0);
2380 void msi_dialog_check_messages( HANDLE handle )
2382 DWORD r;
2384 /* in threads other than the UI thread, block */
2385 if( uiThreadId != GetCurrentThreadId() )
2387 if( handle )
2388 WaitForSingleObject( handle, INFINITE );
2389 return;
2392 /* there's two choices for the UI thread */
2393 while (1)
2395 msi_process_pending_messages( NULL );
2397 if( !handle )
2398 break;
2401 * block here until somebody creates a new dialog or
2402 * the handle we're waiting on becomes ready
2404 r = MsgWaitForMultipleObjects( 1, &handle, 0, INFINITE, QS_ALLINPUT );
2405 if( r == WAIT_OBJECT_0 )
2406 break;
2410 UINT msi_dialog_run_message_loop( msi_dialog *dialog )
2412 DWORD style;
2413 HWND hwnd;
2415 if( uiThreadId != GetCurrentThreadId() )
2416 return SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_CREATE, 0, (LPARAM) dialog );
2418 /* create the dialog window, don't show it yet */
2419 style = WS_OVERLAPPED;
2420 if( dialog->attributes & msidbDialogAttributesVisible )
2421 style |= WS_VISIBLE;
2423 hwnd = CreateWindowW( szMsiDialogClass, dialog->name, style,
2424 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
2425 NULL, NULL, NULL, dialog );
2426 if( !hwnd )
2428 ERR("Failed to create dialog %s\n", debugstr_w( dialog->name ));
2429 return ERROR_FUNCTION_FAILED;
2432 ShowWindow( hwnd, SW_SHOW );
2433 /* UpdateWindow( hwnd ); - and causes the transparent static controls not to paint */
2435 if( dialog->attributes & msidbDialogAttributesModal )
2437 while( !dialog->finished )
2439 MsgWaitForMultipleObjects( 0, NULL, 0, INFINITE, QS_ALLEVENTS );
2440 msi_process_pending_messages( dialog->hwnd );
2443 else
2444 return ERROR_IO_PENDING;
2446 return ERROR_SUCCESS;
2449 void msi_dialog_do_preview( msi_dialog *dialog )
2451 TRACE("\n");
2452 dialog->attributes |= msidbDialogAttributesVisible;
2453 dialog->attributes &= ~msidbDialogAttributesModal;
2454 msi_dialog_run_message_loop( dialog );
2457 void msi_dialog_destroy( msi_dialog *dialog )
2459 if( uiThreadId != GetCurrentThreadId() )
2461 SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_DESTROY, 0, (LPARAM) dialog );
2462 return;
2465 if( dialog->hwnd )
2466 ShowWindow( dialog->hwnd, SW_HIDE );
2468 if( dialog->hwnd )
2469 DestroyWindow( dialog->hwnd );
2471 /* destroy the list of controls */
2472 while( !list_empty( &dialog->controls ) )
2474 msi_control *t = LIST_ENTRY( list_head( &dialog->controls ),
2475 msi_control, entry );
2476 list_remove( &t->entry );
2477 /* leave dialog->hwnd - destroying parent destroys child windows */
2478 msi_free( t->property );
2479 msi_free( t->value );
2480 if( t->hBitmap )
2481 DeleteObject( t->hBitmap );
2482 if( t->hIcon )
2483 DestroyIcon( t->hIcon );
2484 msi_free( t->tabnext );
2485 msi_free( t );
2486 if (t->hDll)
2487 FreeLibrary( t->hDll );
2490 /* destroy the list of fonts */
2491 while( dialog->font_list )
2493 msi_font *t = dialog->font_list;
2494 dialog->font_list = t->next;
2495 DeleteObject( t->hfont );
2496 msi_free( t );
2498 msi_free( dialog->default_font );
2500 msi_free( dialog->control_default );
2501 msi_free( dialog->control_cancel );
2502 msiobj_release( &dialog->package->hdr );
2503 dialog->package = NULL;
2504 msi_free( dialog );
2507 BOOL msi_dialog_register_class( void )
2509 WNDCLASSW cls;
2511 ZeroMemory( &cls, sizeof cls );
2512 cls.lpfnWndProc = MSIDialog_WndProc;
2513 cls.hInstance = NULL;
2514 cls.hIcon = LoadIconW(0, (LPWSTR)IDI_APPLICATION);
2515 cls.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
2516 cls.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
2517 cls.lpszMenuName = NULL;
2518 cls.lpszClassName = szMsiDialogClass;
2520 if( !RegisterClassW( &cls ) )
2521 return FALSE;
2523 cls.lpfnWndProc = MSIHiddenWindowProc;
2524 cls.lpszClassName = szMsiHiddenWindow;
2526 if( !RegisterClassW( &cls ) )
2527 return FALSE;
2529 uiThreadId = GetCurrentThreadId();
2531 hMsiHiddenWindow = CreateWindowW( szMsiHiddenWindow, NULL, WS_OVERLAPPED,
2532 0, 0, 100, 100, NULL, NULL, NULL, NULL );
2533 if( !hMsiHiddenWindow )
2534 return FALSE;
2536 return TRUE;
2539 void msi_dialog_unregister_class( void )
2541 DestroyWindow( hMsiHiddenWindow );
2542 hMsiHiddenWindow = NULL;
2543 UnregisterClassW( szMsiDialogClass, NULL );
2544 UnregisterClassW( szMsiHiddenWindow, NULL );
2545 uiThreadId = 0;