ole32: Create moniker enumerator with an initial reference count.
[wine/multimedia.git] / dlls / msi / dialog.c
blobffbd6b8403f0e666028174e8bada29056f55a7c8
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 szProgress[] = { 'P','r','o','g','r','e','s','s',0 };
115 static const WCHAR szText[] = { 'T','e','x','t',0 };
116 static const WCHAR szPushButton[] = { 'P','u','s','h','B','u','t','t','o','n',0 };
117 static const WCHAR szLine[] = { 'L','i','n','e',0 };
118 static const WCHAR szBitmap[] = { 'B','i','t','m','a','p',0 };
119 static const WCHAR szCheckBox[] = { 'C','h','e','c','k','B','o','x',0 };
120 static const WCHAR szScrollableText[] = {
121 'S','c','r','o','l','l','a','b','l','e','T','e','x','t',0 };
122 static const WCHAR szComboBox[] = { 'C','o','m','b','o','B','o','x',0 };
123 static const WCHAR szEdit[] = { 'E','d','i','t',0 };
124 static const WCHAR szMaskedEdit[] = { 'M','a','s','k','e','d','E','d','i','t',0 };
125 static const WCHAR szPathEdit[] = { 'P','a','t','h','E','d','i','t',0 };
126 static const WCHAR szProgressBar[] = {
127 'P','r','o','g','r','e','s','s','B','a','r',0 };
128 static const WCHAR szRadioButtonGroup[] = {
129 'R','a','d','i','o','B','u','t','t','o','n','G','r','o','u','p',0 };
130 static const WCHAR szIcon[] = { 'I','c','o','n',0 };
131 static const WCHAR szSelectionTree[] = {
132 'S','e','l','e','c','t','i','o','n','T','r','e','e',0 };
134 static UINT msi_dialog_checkbox_handler( msi_dialog *, msi_control *, WPARAM );
135 static void msi_dialog_checkbox_sync_state( msi_dialog *, msi_control * );
136 static UINT msi_dialog_button_handler( msi_dialog *, msi_control *, WPARAM );
137 static UINT msi_dialog_edit_handler( msi_dialog *, msi_control *, WPARAM );
138 static UINT msi_dialog_radiogroup_handler( msi_dialog *, msi_control *, WPARAM param );
139 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog );
140 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
143 /* dialog sequencing */
145 #define WM_MSI_DIALOG_CREATE (WM_USER+0x100)
146 #define WM_MSI_DIALOG_DESTROY (WM_USER+0x101)
148 static DWORD uiThreadId;
149 static HWND hMsiHiddenWindow;
151 static INT msi_dialog_scale_unit( msi_dialog *dialog, INT val )
153 return (dialog->scale * val + 5) / 10;
156 static msi_control *msi_dialog_find_control( msi_dialog *dialog, LPCWSTR name )
158 msi_control *control;
160 if( !name )
161 return NULL;
162 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
163 if( !strcmpW( control->name, name ) ) /* FIXME: case sensitive? */
164 return control;
165 return NULL;
168 static msi_control *msi_dialog_find_control_by_hwnd( msi_dialog *dialog, HWND hwnd )
170 msi_control *control;
172 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
173 if( hwnd == control->hwnd )
174 return control;
175 return NULL;
178 static LPWSTR msi_get_deformatted_field( MSIPACKAGE *package, MSIRECORD *rec, int field )
180 LPCWSTR str = MSI_RecordGetString( rec, field );
181 LPWSTR ret = NULL;
183 if (str)
184 deformat_string( package, str, &ret );
185 return ret;
189 * msi_dialog_get_style
191 * Extract the {\style} string from the front of the text to display and
192 * update the pointer.
194 static LPWSTR msi_dialog_get_style( LPCWSTR p, LPCWSTR *rest )
196 LPWSTR ret = NULL;
197 LPCWSTR q, i;
198 DWORD len;
200 *rest = p;
201 if( !p )
202 return ret;
203 if( *p++ != '{' )
204 return ret;
205 q = strchrW( p, '}' );
206 if( !q )
207 return ret;
208 if( *p == '\\' || *p == '&' )
209 p++;
211 /* little bit of sanity checking to stop us getting confused with RTF */
212 for( i=p; i<q; i++ )
213 if( *i == '}' || *i == '\\' )
214 return ret;
216 *rest = ++q;
217 len = q - p;
219 ret = msi_alloc( len*sizeof(WCHAR) );
220 if( !ret )
221 return ret;
222 memcpy( ret, p, len*sizeof(WCHAR) );
223 ret[len-1] = 0;
224 return ret;
227 static UINT msi_dialog_add_font( MSIRECORD *rec, LPVOID param )
229 msi_dialog *dialog = param;
230 msi_font *font;
231 LPCWSTR face, name;
232 LOGFONTW lf;
233 INT style;
234 HDC hdc;
236 /* create a font and add it to the list */
237 name = MSI_RecordGetString( rec, 1 );
238 font = msi_alloc( sizeof *font + strlenW( name )*sizeof (WCHAR) );
239 strcpyW( font->name, name );
240 font->next = dialog->font_list;
241 dialog->font_list = font;
243 memset( &lf, 0, sizeof lf );
244 face = MSI_RecordGetString( rec, 2 );
245 lf.lfHeight = MSI_RecordGetInteger( rec, 3 );
246 style = MSI_RecordGetInteger( rec, 5 );
247 if( style & msidbTextStyleStyleBitsBold )
248 lf.lfWeight = FW_BOLD;
249 if( style & msidbTextStyleStyleBitsItalic )
250 lf.lfItalic = TRUE;
251 if( style & msidbTextStyleStyleBitsUnderline )
252 lf.lfUnderline = TRUE;
253 if( style & msidbTextStyleStyleBitsStrike )
254 lf.lfStrikeOut = TRUE;
255 lstrcpynW( lf.lfFaceName, face, LF_FACESIZE );
257 /* adjust the height */
258 hdc = GetDC( dialog->hwnd );
259 if (hdc)
261 lf.lfHeight = -MulDiv(lf.lfHeight, GetDeviceCaps(hdc, LOGPIXELSY), 72);
262 ReleaseDC( dialog->hwnd, hdc );
265 font->hfont = CreateFontIndirectW( &lf );
267 TRACE("Adding font style %s\n", debugstr_w(font->name) );
269 return ERROR_SUCCESS;
272 static msi_font *msi_dialog_find_font( msi_dialog *dialog, LPCWSTR name )
274 msi_font *font;
276 for( font = dialog->font_list; font; font = font->next )
277 if( !strcmpW( font->name, name ) ) /* FIXME: case sensitive? */
278 break;
280 return font;
283 static UINT msi_dialog_set_font( msi_dialog *dialog, HWND hwnd, LPCWSTR name )
285 msi_font *font;
287 font = msi_dialog_find_font( dialog, name );
288 if( font )
289 SendMessageW( hwnd, WM_SETFONT, (WPARAM) font->hfont, TRUE );
290 else
291 ERR("No font entry for %s\n", debugstr_w(name));
292 return ERROR_SUCCESS;
295 static UINT msi_dialog_build_font_list( msi_dialog *dialog )
297 static const WCHAR query[] = {
298 'S','E','L','E','C','T',' ','*',' ',
299 'F','R','O','M',' ','`','T','e','x','t','S','t','y','l','e','`',' ',0
301 UINT r;
302 MSIQUERY *view = NULL;
304 TRACE("dialog %p\n", dialog );
306 r = MSI_OpenQuery( dialog->package->db, &view, query );
307 if( r != ERROR_SUCCESS )
308 return r;
310 r = MSI_IterateRecords( view, NULL, msi_dialog_add_font, dialog );
311 msiobj_release( &view->hdr );
313 return r;
316 static msi_control *msi_dialog_create_window( msi_dialog *dialog,
317 MSIRECORD *rec, DWORD exstyle, LPCWSTR szCls, LPCWSTR name, LPCWSTR text,
318 DWORD style, HWND parent )
320 DWORD x, y, width, height;
321 LPWSTR font = NULL, title_font = NULL;
322 LPCWSTR title = NULL;
323 msi_control *control;
325 style |= WS_CHILD;
327 control = msi_alloc( sizeof *control + strlenW(name)*sizeof(WCHAR) );
328 strcpyW( control->name, name );
329 list_add_head( &dialog->controls, &control->entry );
330 control->handler = NULL;
331 control->property = NULL;
332 control->value = NULL;
333 control->hBitmap = NULL;
334 control->hIcon = NULL;
335 control->hDll = NULL;
336 control->tabnext = strdupW( MSI_RecordGetString( rec, 11) );
338 x = MSI_RecordGetInteger( rec, 4 );
339 y = MSI_RecordGetInteger( rec, 5 );
340 width = MSI_RecordGetInteger( rec, 6 );
341 height = MSI_RecordGetInteger( rec, 7 );
343 x = msi_dialog_scale_unit( dialog, x );
344 y = msi_dialog_scale_unit( dialog, y );
345 width = msi_dialog_scale_unit( dialog, width );
346 height = msi_dialog_scale_unit( dialog, height );
348 if( text )
350 deformat_string( dialog->package, text, &title_font );
351 font = msi_dialog_get_style( title_font, &title );
354 control->hwnd = CreateWindowExW( exstyle, szCls, title, style,
355 x, y, width, height, parent, NULL, NULL, NULL );
357 TRACE("Dialog %s control %s hwnd %p\n",
358 debugstr_w(dialog->name), debugstr_w(text), control->hwnd );
360 msi_dialog_set_font( dialog, control->hwnd,
361 font ? font : dialog->default_font );
363 msi_free( title_font );
364 msi_free( font );
366 return control;
369 static MSIRECORD *msi_get_binary_record( MSIDATABASE *db, LPCWSTR name )
371 static const WCHAR query[] = {
372 's','e','l','e','c','t',' ','*',' ',
373 'f','r','o','m',' ','B','i','n','a','r','y',' ',
374 'w','h','e','r','e',' ',
375 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0
378 return MSI_QueryGetRecord( db, query, name );
381 static LPWSTR msi_create_tmp_path(void)
383 WCHAR tmp[MAX_PATH];
384 LPWSTR path = NULL;
385 static const WCHAR prefix[] = { 'm','s','i',0 };
386 DWORD len, r;
388 r = GetTempPathW( MAX_PATH, tmp );
389 if( !r )
390 return path;
391 len = lstrlenW( tmp ) + 20;
392 path = msi_alloc( len * sizeof (WCHAR) );
393 if( path )
395 r = GetTempFileNameW( tmp, prefix, 0, path );
396 if (!r)
398 msi_free( path );
399 path = NULL;
402 return path;
406 static HANDLE msi_load_image( MSIDATABASE *db, LPCWSTR name, UINT type,
407 UINT cx, UINT cy, UINT flags )
409 MSIRECORD *rec = NULL;
410 HANDLE himage = NULL;
411 LPWSTR tmp;
412 UINT r;
414 TRACE("%p %s %u %u %08x\n", db, debugstr_w(name), cx, cy, flags);
416 tmp = msi_create_tmp_path();
417 if( !tmp )
418 return himage;
420 rec = msi_get_binary_record( db, name );
421 if( rec )
423 r = MSI_RecordStreamToFile( rec, 2, tmp );
424 if( r == ERROR_SUCCESS )
426 himage = LoadImageW( 0, tmp, type, cx, cy, flags );
427 DeleteFileW( tmp );
429 msiobj_release( &rec->hdr );
432 msi_free( tmp );
433 return himage;
436 static HICON msi_load_icon( MSIDATABASE *db, LPCWSTR text, UINT attributes )
438 DWORD cx = 0, cy = 0, flags;
440 flags = LR_LOADFROMFILE | LR_DEFAULTSIZE;
441 if( attributes & msidbControlAttributesFixedSize )
443 flags &= ~LR_DEFAULTSIZE;
444 if( attributes & msidbControlAttributesIconSize16 )
446 cx += 16;
447 cy += 16;
449 if( attributes & msidbControlAttributesIconSize32 )
451 cx += 32;
452 cy += 32;
454 /* msidbControlAttributesIconSize48 handled by above logic */
456 return msi_load_image( db, text, IMAGE_ICON, cx, cy, flags );
460 /* called from the Control Event subscription code */
461 void msi_dialog_handle_event( msi_dialog* dialog, LPCWSTR control,
462 LPCWSTR attribute, MSIRECORD *rec )
464 msi_control* ctrl;
465 LPCWSTR font_text, text = NULL;
466 LPWSTR font;
468 ctrl = msi_dialog_find_control( dialog, control );
469 if (!ctrl)
470 return;
471 if( !lstrcmpW(attribute, szText) )
473 font_text = MSI_RecordGetString( rec , 1 );
474 font = msi_dialog_get_style( font_text, &text );
475 SetWindowTextW( ctrl->hwnd, text );
476 msi_free( font );
477 msi_dialog_check_messages( NULL );
479 else if( !lstrcmpW(attribute, szProgress) )
481 /* FIXME: should forward to progress bar */
482 static int display_fixme = 1;
483 if (display_fixme)
484 FIXME("Attribute %s not being set\n", debugstr_w(attribute));
485 display_fixme = 0;
487 else
489 FIXME("Attribute %s not being set\n", debugstr_w(attribute));
490 return;
494 static void msi_dialog_map_events(msi_dialog* dialog, LPCWSTR control)
496 static const WCHAR Query[] = {
497 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
498 '`','E','v','e','n','t','M','a','p','p','i','n','g','`',' ',
499 'W','H','E','R','E',' ',
500 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
501 'A','N','D',' ',
502 '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',0
504 MSIRECORD *row;
505 LPCWSTR event, attribute;
507 row = MSI_QueryGetRecord( dialog->package->db, Query, dialog->name, control );
508 if (!row)
509 return;
511 event = MSI_RecordGetString( row, 3 );
512 attribute = MSI_RecordGetString( row, 4 );
513 ControlEvent_SubscribeToEvent( dialog->package, event, control, attribute );
514 msiobj_release( &row->hdr );
517 /* everything except radio buttons */
518 static msi_control *msi_dialog_add_control( msi_dialog *dialog,
519 MSIRECORD *rec, LPCWSTR szCls, DWORD style )
521 DWORD attributes;
522 LPCWSTR text, name;
523 DWORD exstyle = 0;
525 name = MSI_RecordGetString( rec, 2 );
526 attributes = MSI_RecordGetInteger( rec, 8 );
527 text = MSI_RecordGetString( rec, 10 );
528 if( attributes & msidbControlAttributesVisible )
529 style |= WS_VISIBLE;
530 if( ~attributes & msidbControlAttributesEnabled )
531 style |= WS_DISABLED;
532 if( attributes & msidbControlAttributesSunken )
533 exstyle |= WS_EX_CLIENTEDGE;
535 msi_dialog_map_events(dialog, name);
537 return msi_dialog_create_window( dialog, rec, exstyle, szCls, name,
538 text, style, dialog->hwnd );
541 struct msi_text_info
543 WNDPROC oldproc;
544 DWORD attributes;
548 * we don't erase our own background,
549 * so we have to make sure that the parent window redraws first
551 static void msi_text_on_settext( HWND hWnd )
553 HWND hParent;
554 RECT rc;
556 hParent = GetParent( hWnd );
557 GetWindowRect( hWnd, &rc );
558 MapWindowPoints( NULL, hParent, (LPPOINT) &rc, 2 );
559 InvalidateRect( hParent, &rc, TRUE );
562 static LRESULT WINAPI
563 MSIText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
565 struct msi_text_info *info;
566 LRESULT r = 0;
568 TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
570 info = GetPropW(hWnd, szButtonData);
572 if( msg == WM_CTLCOLORSTATIC &&
573 ( info->attributes & msidbControlAttributesTransparent ) )
575 SetBkMode( (HDC)wParam, TRANSPARENT );
576 return (LRESULT) GetStockObject(NULL_BRUSH);
579 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
581 switch( msg )
583 case WM_SETTEXT:
584 msi_text_on_settext( hWnd );
585 break;
586 case WM_NCDESTROY:
587 msi_free( info );
588 RemovePropW( hWnd, szButtonData );
589 break;
592 return r;
595 static UINT msi_dialog_text_control( msi_dialog *dialog, MSIRECORD *rec )
597 msi_control *control;
598 struct msi_text_info *info;
600 TRACE("%p %p\n", dialog, rec);
602 control = msi_dialog_add_control( dialog, rec, szStatic, SS_LEFT | WS_GROUP );
603 if( !control )
604 return ERROR_FUNCTION_FAILED;
606 info = msi_alloc( sizeof *info );
607 if( !info )
608 return ERROR_SUCCESS;
610 info->attributes = MSI_RecordGetInteger( rec, 8 );
611 if( info->attributes & msidbControlAttributesTransparent )
612 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT );
614 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
615 (LONG_PTR)MSIText_WndProc );
616 SetPropW( control->hwnd, szButtonData, info );
618 return ERROR_SUCCESS;
621 static UINT msi_dialog_button_control( msi_dialog *dialog, MSIRECORD *rec )
623 msi_control *control;
624 UINT attributes, style;
625 LPWSTR text;
627 TRACE("%p %p\n", dialog, rec);
629 style = WS_TABSTOP;
630 attributes = MSI_RecordGetInteger( rec, 8 );
631 if( attributes & msidbControlAttributesIcon )
632 style |= BS_ICON;
634 control = msi_dialog_add_control( dialog, rec, szButton, style );
635 if( !control )
636 return ERROR_FUNCTION_FAILED;
638 control->handler = msi_dialog_button_handler;
640 /* set the icon */
641 text = msi_get_deformatted_field( dialog->package, rec, 10 );
642 control->hIcon = msi_load_icon( dialog->package->db, text, attributes );
643 if( attributes & msidbControlAttributesIcon )
644 SendMessageW( control->hwnd, BM_SETIMAGE, IMAGE_ICON, (LPARAM) control->hIcon );
645 msi_free( text );
647 return ERROR_SUCCESS;
650 static LPWSTR msi_get_checkbox_value( msi_dialog *dialog, LPCWSTR prop )
652 static const WCHAR query[] = {
653 'S','E','L','E','C','T',' ','*',' ',
654 'F','R','O','M',' ','`','C','h','e','c','k','B','o','x',' ','`',
655 'W','H','E','R','E',' ',
656 '`','P','r','o','p','e','r','t','y','`',' ','=',' ',
657 '\'','%','s','\'',0
659 MSIRECORD *rec = NULL;
660 LPWSTR ret = NULL;
662 /* find if there is a value associated with the checkbox */
663 rec = MSI_QueryGetRecord( dialog->package->db, query, prop );
664 if (!rec)
665 return ret;
667 ret = msi_get_deformatted_field( dialog->package, rec, 2 );
668 if( ret && !ret[0] )
670 msi_free( ret );
671 ret = NULL;
673 msiobj_release( &rec->hdr );
674 if (ret)
675 return ret;
677 ret = msi_dup_property( dialog->package, prop );
678 if( ret && !ret[0] )
680 msi_free( ret );
681 ret = NULL;
684 return ret;
687 static UINT msi_dialog_checkbox_control( msi_dialog *dialog, MSIRECORD *rec )
689 msi_control *control;
690 LPCWSTR prop;
692 TRACE("%p %p\n", dialog, rec);
694 control = msi_dialog_add_control( dialog, rec, szButton,
695 BS_CHECKBOX | BS_MULTILINE | WS_TABSTOP );
696 control->handler = msi_dialog_checkbox_handler;
697 prop = MSI_RecordGetString( rec, 9 );
698 if( prop )
700 control->property = strdupW( prop );
701 control->value = msi_get_checkbox_value( dialog, prop );
702 TRACE("control %s value %s\n", debugstr_w(control->property),
703 debugstr_w(control->value));
705 msi_dialog_checkbox_sync_state( dialog, control );
707 return ERROR_SUCCESS;
710 static UINT msi_dialog_line_control( msi_dialog *dialog, MSIRECORD *rec )
712 TRACE("%p %p\n", dialog, rec);
714 msi_dialog_add_control( dialog, rec, szStatic, SS_ETCHEDHORZ | SS_SUNKEN );
715 return ERROR_SUCCESS;
718 /******************** Scroll Text ********************************************/
720 struct msi_scrolltext_info
722 msi_dialog *dialog;
723 msi_control *control;
724 WNDPROC oldproc;
727 static LRESULT WINAPI
728 MSIScrollText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
730 struct msi_scrolltext_info *info;
731 HRESULT r;
733 TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
735 info = GetPropW( hWnd, szButtonData );
737 r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
739 switch( msg )
741 case WM_NCDESTROY:
742 msi_free( info );
743 RemovePropW( hWnd, szButtonData );
744 break;
745 case WM_PAINT:
746 /* native MSI sets a wait cursor here */
747 msi_dialog_button_handler( info->dialog, info->control, BN_CLICKED );
748 break;
750 return r;
753 struct msi_streamin_info
755 LPSTR string;
756 DWORD offset;
757 DWORD length;
760 static DWORD CALLBACK
761 msi_richedit_stream_in( DWORD_PTR arg, LPBYTE buffer, LONG count, LONG *pcb )
763 struct msi_streamin_info *info = (struct msi_streamin_info*) arg;
765 if( (count + info->offset) > info->length )
766 count = info->length - info->offset;
767 memcpy( buffer, &info->string[ info->offset ], count );
768 *pcb = count;
769 info->offset += count;
771 TRACE("%ld/%ld\n", info->offset, info->length);
773 return 0;
776 static void msi_scrolltext_add_text( msi_control *control, LPCWSTR text )
778 struct msi_streamin_info info;
779 EDITSTREAM es;
781 info.string = strdupWtoA( text );
782 info.offset = 0;
783 info.length = lstrlenA( info.string ) + 1;
785 es.dwCookie = (DWORD_PTR) &info;
786 es.dwError = 0;
787 es.pfnCallback = msi_richedit_stream_in;
789 SendMessageW( control->hwnd, EM_STREAMIN, SF_RTF, (LPARAM) &es );
791 msi_free( info.string );
794 static UINT msi_dialog_scrolltext_control( msi_dialog *dialog, MSIRECORD *rec )
796 static const WCHAR szRichEdit20W[] = {
797 'R','i','c','h','E','d','i','t','2','0','W',0
799 struct msi_scrolltext_info *info;
800 msi_control *control;
801 HMODULE hRichedit;
802 DWORD style;
804 info = msi_alloc( sizeof *info );
805 if (!info)
806 return ERROR_FUNCTION_FAILED;
808 hRichedit = LoadLibraryA("riched20");
810 style = WS_BORDER | ES_MULTILINE | WS_VSCROLL |
811 ES_READONLY | ES_AUTOVSCROLL | WS_TABSTOP;
812 control = msi_dialog_add_control( dialog, rec, szRichEdit20W, style );
813 if (!control)
815 FreeLibrary( hRichedit );
816 msi_free( info );
817 return ERROR_FUNCTION_FAILED;
820 control->hDll = hRichedit;
822 info->dialog = dialog;
823 info->control = control;
825 /* subclass the static control */
826 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
827 (LONG_PTR)MSIScrollText_WndProc );
828 SetPropW( control->hwnd, szButtonData, info );
830 /* add the text into the richedit */
831 msi_scrolltext_add_text( control, MSI_RecordGetString( rec, 10 ) );
833 return ERROR_SUCCESS;
836 static HBITMAP msi_load_picture( MSIDATABASE *db, LPCWSTR name,
837 INT cx, INT cy, DWORD flags )
839 HBITMAP hOleBitmap = 0, hBitmap = 0, hOldSrcBitmap, hOldDestBitmap;
840 MSIRECORD *rec = NULL;
841 IStream *stm = NULL;
842 IPicture *pic = NULL;
843 HDC srcdc, destdc;
844 BITMAP bm;
845 UINT r;
847 rec = msi_get_binary_record( db, name );
848 if( !rec )
849 goto end;
851 r = MSI_RecordGetIStream( rec, 2, &stm );
852 msiobj_release( &rec->hdr );
853 if( r != ERROR_SUCCESS )
854 goto end;
856 r = OleLoadPicture( stm, 0, TRUE, &IID_IPicture, (LPVOID*) &pic );
857 IStream_Release( stm );
858 if( FAILED( r ) )
860 ERR("failed to load picture\n");
861 goto end;
864 r = IPicture_get_Handle( pic, (OLE_HANDLE*) &hOleBitmap );
865 if( FAILED( r ) )
867 ERR("failed to get bitmap handle\n");
868 goto end;
871 /* make the bitmap the desired size */
872 r = GetObjectW( hOleBitmap, sizeof bm, &bm );
873 if (r != sizeof bm )
875 ERR("failed to get bitmap size\n");
876 goto end;
879 if (flags & LR_DEFAULTSIZE)
881 cx = bm.bmWidth;
882 cy = bm.bmHeight;
885 srcdc = CreateCompatibleDC( NULL );
886 hOldSrcBitmap = SelectObject( srcdc, hOleBitmap );
887 destdc = CreateCompatibleDC( NULL );
888 hBitmap = CreateCompatibleBitmap( srcdc, cx, cy );
889 hOldDestBitmap = SelectObject( destdc, hBitmap );
890 StretchBlt( destdc, 0, 0, cx, cy,
891 srcdc, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
892 SelectObject( srcdc, hOldSrcBitmap );
893 SelectObject( destdc, hOldDestBitmap );
894 DeleteDC( srcdc );
895 DeleteDC( destdc );
897 end:
898 if ( pic )
899 IPicture_Release( pic );
900 return hBitmap;
903 static UINT msi_dialog_bitmap_control( msi_dialog *dialog, MSIRECORD *rec )
905 UINT cx, cy, flags, style, attributes;
906 msi_control *control;
907 LPWSTR text;
909 flags = LR_LOADFROMFILE;
910 style = SS_BITMAP | SS_LEFT | WS_GROUP;
912 attributes = MSI_RecordGetInteger( rec, 8 );
913 if( attributes & msidbControlAttributesFixedSize )
915 flags |= LR_DEFAULTSIZE;
916 style |= SS_CENTERIMAGE;
919 control = msi_dialog_add_control( dialog, rec, szStatic, style );
920 cx = MSI_RecordGetInteger( rec, 6 );
921 cy = MSI_RecordGetInteger( rec, 7 );
922 cx = msi_dialog_scale_unit( dialog, cx );
923 cy = msi_dialog_scale_unit( dialog, cy );
925 text = msi_get_deformatted_field( dialog->package, rec, 10 );
926 control->hBitmap = msi_load_picture( dialog->package->db, text, cx, cy, flags );
927 if( control->hBitmap )
928 SendMessageW( control->hwnd, STM_SETIMAGE,
929 IMAGE_BITMAP, (LPARAM) control->hBitmap );
930 else
931 ERR("Failed to load bitmap %s\n", debugstr_w(text));
933 msi_free( text );
935 return ERROR_SUCCESS;
938 static UINT msi_dialog_icon_control( msi_dialog *dialog, MSIRECORD *rec )
940 msi_control *control;
941 DWORD attributes;
942 LPWSTR text;
944 TRACE("\n");
946 control = msi_dialog_add_control( dialog, rec, szStatic,
947 SS_ICON | SS_CENTERIMAGE | WS_GROUP );
949 attributes = MSI_RecordGetInteger( rec, 8 );
950 text = msi_get_deformatted_field( dialog->package, rec, 10 );
951 control->hIcon = msi_load_icon( dialog->package->db, text, attributes );
952 if( control->hIcon )
953 SendMessageW( control->hwnd, STM_SETICON, (WPARAM) control->hIcon, 0 );
954 else
955 ERR("Failed to load bitmap %s\n", debugstr_w(text));
956 msi_free( text );
957 return ERROR_SUCCESS;
960 static UINT msi_dialog_combo_control( msi_dialog *dialog, MSIRECORD *rec )
962 static const WCHAR szCombo[] = { 'C','O','M','B','O','B','O','X',0 };
964 msi_dialog_add_control( dialog, rec, szCombo,
965 SS_BITMAP | SS_LEFT | SS_CENTERIMAGE );
966 return ERROR_SUCCESS;
969 static UINT msi_dialog_edit_control( msi_dialog *dialog, MSIRECORD *rec )
971 msi_control *control;
972 LPCWSTR prop;
973 LPWSTR val;
975 control = msi_dialog_add_control( dialog, rec, szEdit,
976 WS_BORDER | WS_TABSTOP );
977 control->handler = msi_dialog_edit_handler;
978 prop = MSI_RecordGetString( rec, 9 );
979 if( prop )
980 control->property = strdupW( prop );
981 val = msi_dup_property( dialog->package, control->property );
982 SetWindowTextW( control->hwnd, val );
983 msi_free( val );
984 return ERROR_SUCCESS;
987 /******************** Masked Edit ********************************************/
989 #define MASK_MAX_GROUPS 10
991 struct msi_mask_group
993 UINT len;
994 UINT ofs;
995 WCHAR type;
996 HWND hwnd;
999 struct msi_maskedit_info
1001 msi_dialog *dialog;
1002 WNDPROC oldproc;
1003 HWND hwnd;
1004 LPWSTR prop;
1005 UINT num_chars;
1006 UINT num_groups;
1007 struct msi_mask_group group[MASK_MAX_GROUPS];
1010 static BOOL msi_mask_editable( WCHAR type )
1012 switch (type)
1014 case '%':
1015 case '#':
1016 case '&':
1017 case '`':
1018 case '?':
1019 case '^':
1020 return TRUE;
1022 return FALSE;
1025 static void msi_mask_control_change( struct msi_maskedit_info *info )
1027 LPWSTR val;
1028 UINT i, n, r;
1030 val = msi_alloc( (info->num_chars+1)*sizeof(WCHAR) );
1031 for( i=0, n=0; i<info->num_groups; i++ )
1033 if( (info->group[i].len + n) > info->num_chars )
1035 ERR("can't fit control %d text into template\n",i);
1036 break;
1038 if (!msi_mask_editable(info->group[i].type))
1040 for(r=0; r<info->group[i].len; r++)
1041 val[n+r] = info->group[i].type;
1042 val[n+r] = 0;
1044 else
1046 r = GetWindowTextW( info->group[i].hwnd, &val[n], info->group[i].len+1 );
1047 if( r != info->group[i].len )
1048 break;
1050 n += r;
1053 TRACE("%d/%d controls were good\n", i, info->num_groups);
1055 if( i == info->num_groups )
1057 TRACE("Set property %s to %s\n",
1058 debugstr_w(info->prop), debugstr_w(val) );
1059 CharUpperBuffW( val, info->num_chars );
1060 MSI_SetPropertyW( info->dialog->package, info->prop, val );
1061 msi_dialog_evaluate_control_conditions( info->dialog );
1063 msi_free( val );
1066 /* now move to the next control if necessary */
1067 static VOID msi_mask_next_control( struct msi_maskedit_info *info, HWND hWnd )
1069 HWND hWndNext;
1070 UINT len, i;
1072 for( i=0; i<info->num_groups; i++ )
1073 if( info->group[i].hwnd == hWnd )
1074 break;
1076 /* don't move from the last control */
1077 if( i >= (info->num_groups-1) )
1078 return;
1080 len = SendMessageW( hWnd, WM_GETTEXTLENGTH, 0, 0 );
1081 if( len < info->group[i].len )
1082 return;
1084 hWndNext = GetNextDlgTabItem( GetParent( hWnd ), hWnd, FALSE );
1085 SetFocus( hWndNext );
1088 static LRESULT WINAPI
1089 MSIMaskedEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1091 struct msi_maskedit_info *info;
1092 HRESULT r;
1094 TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
1096 info = GetPropW(hWnd, szButtonData);
1098 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1100 switch( msg )
1102 case WM_COMMAND:
1103 if (HIWORD(wParam) == EN_CHANGE)
1105 msi_mask_control_change( info );
1106 msi_mask_next_control( info, (HWND) lParam );
1108 break;
1109 case WM_NCDESTROY:
1110 msi_free( info->prop );
1111 msi_free( info );
1112 RemovePropW( hWnd, szButtonData );
1113 break;
1116 return r;
1119 /* fish the various bits of the property out and put them in the control */
1120 static void
1121 msi_maskedit_set_text( struct msi_maskedit_info *info, LPCWSTR text )
1123 LPCWSTR p;
1124 UINT i;
1126 p = text;
1127 for( i = 0; i < info->num_groups; i++ )
1129 if( info->group[i].len < lstrlenW( p ) )
1131 LPWSTR chunk = strdupW( p );
1132 chunk[ info->group[i].len ] = 0;
1133 SetWindowTextW( info->group[i].hwnd, chunk );
1134 msi_free( chunk );
1136 else
1138 SetWindowTextW( info->group[i].hwnd, p );
1139 break;
1141 p += info->group[i].len;
1145 static struct msi_maskedit_info * msi_dialog_parse_groups( LPCWSTR mask )
1147 struct msi_maskedit_info * info = NULL;
1148 int i = 0, n = 0, total = 0;
1149 LPCWSTR p;
1151 TRACE("masked control, template %s\n", debugstr_w(mask));
1153 if( !mask )
1154 return info;
1156 info = msi_alloc_zero( sizeof *info );
1157 if( !info )
1158 return info;
1160 p = strchrW(mask, '<');
1161 if( p )
1162 p++;
1163 else
1164 p = mask;
1166 for( i=0; i<MASK_MAX_GROUPS; i++ )
1168 /* stop at the end of the string */
1169 if( p[0] == 0 || p[0] == '>' )
1170 break;
1172 /* count the number of the same identifier */
1173 for( n=0; p[n] == p[0]; n++ )
1175 info->group[i].ofs = total;
1176 info->group[i].type = p[0];
1177 if( p[n] == '=' )
1179 n++;
1180 total++; /* an extra not part of the group */
1182 info->group[i].len = n;
1183 total += n;
1184 p += n;
1187 TRACE("%d characters in %d groups\n", total, i );
1188 if( i == MASK_MAX_GROUPS )
1189 ERR("too many groups in PIDTemplate %s\n", debugstr_w(mask));
1191 info->num_chars = total;
1192 info->num_groups = i;
1194 return info;
1197 static void
1198 msi_maskedit_create_children( struct msi_maskedit_info *info, LPCWSTR font )
1200 DWORD width, height, style, wx, ww;
1201 RECT rect;
1202 HWND hwnd;
1203 UINT i;
1205 style = WS_CHILD | WS_BORDER | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL;
1207 GetClientRect( info->hwnd, &rect );
1209 width = rect.right - rect.left;
1210 height = rect.bottom - rect.top;
1212 for( i = 0; i < info->num_groups; i++ )
1214 if (!msi_mask_editable( info->group[i].type ))
1215 continue;
1216 wx = (info->group[i].ofs * width) / info->num_chars;
1217 ww = (info->group[i].len * width) / info->num_chars;
1219 hwnd = CreateWindowW( szEdit, NULL, style, wx, 0, ww, height,
1220 info->hwnd, NULL, NULL, NULL );
1221 if( !hwnd )
1223 ERR("failed to create mask edit sub window\n");
1224 break;
1227 SendMessageW( hwnd, EM_LIMITTEXT, info->group[i].len, 0 );
1229 msi_dialog_set_font( info->dialog, hwnd,
1230 font?font:info->dialog->default_font );
1231 info->group[i].hwnd = hwnd;
1236 * office 2003 uses "73931<````=````=````=````=`````>@@@@@"
1237 * delphi 7 uses "<????-??????-??????-????>" and "<???-???>"
1238 * filemaker pro 7 uses "<^^^^=^^^^=^^^^=^^^^=^^^^=^^^^=^^^^^>"
1240 static UINT msi_dialog_maskedit_control( msi_dialog *dialog, MSIRECORD *rec )
1242 LPWSTR font_mask, val = NULL, font;
1243 struct msi_maskedit_info *info = NULL;
1244 UINT ret = ERROR_SUCCESS;
1245 msi_control *control;
1246 LPCWSTR prop, mask;
1248 TRACE("\n");
1250 font_mask = msi_get_deformatted_field( dialog->package, rec, 10 );
1251 font = msi_dialog_get_style( font_mask, &mask );
1252 if( !mask )
1254 ERR("mask template is empty\n");
1255 goto end;
1258 info = msi_dialog_parse_groups( mask );
1259 if( !info )
1261 ERR("template %s is invalid\n", debugstr_w(mask));
1262 goto end;
1265 info->dialog = dialog;
1267 control = msi_dialog_add_control( dialog, rec, szStatic,
1268 SS_OWNERDRAW | WS_GROUP | WS_VISIBLE );
1269 if( !control )
1271 ERR("Failed to create maskedit container\n");
1272 ret = ERROR_FUNCTION_FAILED;
1273 goto end;
1275 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
1277 info->hwnd = control->hwnd;
1279 /* subclass the static control */
1280 info->oldproc = (WNDPROC) SetWindowLongPtrW( info->hwnd, GWLP_WNDPROC,
1281 (LONG_PTR)MSIMaskedEdit_WndProc );
1282 SetPropW( control->hwnd, szButtonData, info );
1284 prop = MSI_RecordGetString( rec, 9 );
1285 if( prop )
1286 info->prop = strdupW( prop );
1288 msi_maskedit_create_children( info, font );
1290 if( prop )
1292 val = msi_dup_property( dialog->package, prop );
1293 if( val )
1295 msi_maskedit_set_text( info, val );
1296 msi_free( val );
1300 end:
1301 if( ret != ERROR_SUCCESS )
1302 msi_free( info );
1303 msi_free( font_mask );
1304 msi_free( font );
1305 return ret;
1308 /******************** Progress Bar *****************************************/
1310 static UINT msi_dialog_progress_bar( msi_dialog *dialog, MSIRECORD *rec )
1312 msi_dialog_add_control( dialog, rec, PROGRESS_CLASSW, WS_VISIBLE );
1313 return ERROR_SUCCESS;
1316 /******************** Path Edit ********************************************/
1318 static UINT msi_dialog_pathedit_control( msi_dialog *dialog, MSIRECORD *rec )
1320 FIXME("not implemented properly\n");
1321 return msi_dialog_edit_control( dialog, rec );
1324 /* radio buttons are a bit different from normal controls */
1325 static UINT msi_dialog_create_radiobutton( MSIRECORD *rec, LPVOID param )
1327 radio_button_group_descr *group = (radio_button_group_descr *)param;
1328 msi_dialog *dialog = group->dialog;
1329 msi_control *control;
1330 LPCWSTR prop, text, name;
1331 DWORD style, attributes = group->attributes;
1333 style = WS_CHILD | BS_AUTORADIOBUTTON | BS_MULTILINE | WS_TABSTOP;
1334 name = MSI_RecordGetString( rec, 3 );
1335 text = MSI_RecordGetString( rec, 8 );
1336 if( attributes & 1 )
1337 style |= WS_VISIBLE;
1338 if( ~attributes & 2 )
1339 style |= WS_DISABLED;
1341 control = msi_dialog_create_window( dialog, rec, 0, szButton, name, text,
1342 style, group->parent->hwnd );
1343 if (!control)
1344 return ERROR_FUNCTION_FAILED;
1345 control->handler = msi_dialog_radiogroup_handler;
1347 prop = MSI_RecordGetString( rec, 1 );
1348 if( prop )
1349 control->property = strdupW( prop );
1351 return ERROR_SUCCESS;
1354 static UINT msi_dialog_radiogroup_control( msi_dialog *dialog, MSIRECORD *rec )
1356 static const WCHAR query[] = {
1357 'S','E','L','E','C','T',' ','*',' ',
1358 'F','R','O','M',' ','R','a','d','i','o','B','u','t','t','o','n',' ',
1359 'W','H','E','R','E',' ',
1360 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
1361 UINT r;
1362 LPCWSTR prop;
1363 msi_control *control;
1364 MSIQUERY *view = NULL;
1365 radio_button_group_descr group;
1366 MSIPACKAGE *package = dialog->package;
1367 WNDPROC oldproc;
1369 prop = MSI_RecordGetString( rec, 9 );
1371 TRACE("%p %p %s\n", dialog, rec, debugstr_w( prop ));
1373 /* Create parent group box to hold radio buttons */
1374 control = msi_dialog_add_control( dialog, rec, szButton, BS_OWNERDRAW|WS_GROUP );
1375 if( !control )
1376 return ERROR_FUNCTION_FAILED;
1378 oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1379 (LONG_PTR)MSIRadioGroup_WndProc );
1380 SetPropW(control->hwnd, szButtonData, oldproc);
1381 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
1383 if( prop )
1384 control->property = strdupW( prop );
1386 /* query the Radio Button table for all control in this group */
1387 r = MSI_OpenQuery( package->db, &view, query, prop );
1388 if( r != ERROR_SUCCESS )
1390 ERR("query failed for dialog %s radio group %s\n",
1391 debugstr_w(dialog->name), debugstr_w(prop));
1392 return ERROR_INVALID_PARAMETER;
1395 group.dialog = dialog;
1396 group.parent = control;
1397 group.attributes = MSI_RecordGetInteger( rec, 8 );
1399 r = MSI_IterateRecords( view, 0, msi_dialog_create_radiobutton, &group );
1400 msiobj_release( &view->hdr );
1402 return r;
1405 /******************** Selection Tree ***************************************/
1407 struct msi_selection_tree_info
1409 msi_dialog *dialog;
1410 HWND hwnd;
1411 WNDPROC oldproc;
1414 static void
1415 msi_seltree_sync_item_state( HWND hwnd, MSIFEATURE *feature, HTREEITEM hItem )
1417 TVITEMW tvi;
1419 TRACE("Feature %s -> %d %d %d\n", debugstr_w(feature->Title),
1420 feature->Installed, feature->Action, feature->ActionRequest);
1422 tvi.mask = TVIF_STATE;
1423 tvi.hItem = hItem;
1424 tvi.state = INDEXTOSTATEIMAGEMASK( feature->Action );
1425 tvi.stateMask = TVIS_STATEIMAGEMASK;
1427 SendMessageW( hwnd, TVM_SETITEMW, 0, (LPARAM) &tvi );
1430 static UINT
1431 msi_seltree_popup_menu( HWND hwnd, INT x, INT y )
1433 HMENU hMenu;
1434 INT r;
1436 /* create a menu to display */
1437 hMenu = CreatePopupMenu();
1439 /* FIXME: load strings from resources */
1440 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_LOCAL, "Install feature locally");
1441 AppendMenuA( hMenu, MF_GRAYED, 0x1000, "Install entire feature");
1442 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ADVERTISED, "Install on demand");
1443 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ABSENT, "Don't install");
1444 r = TrackPopupMenu( hMenu, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RETURNCMD,
1445 x, y, 0, hwnd, NULL );
1446 DestroyMenu( hMenu );
1447 return r;
1450 static MSIFEATURE *
1451 msi_seltree_feature_from_item( HWND hwnd, HTREEITEM hItem )
1453 TVITEMW tvi;
1455 /* get the feature from the item */
1456 memset( &tvi, 0, sizeof tvi );
1457 tvi.hItem = hItem;
1458 tvi.mask = TVIF_PARAM | TVIF_HANDLE;
1459 SendMessageW( hwnd, TVM_GETITEMW, 0, (LPARAM) &tvi );
1461 return (MSIFEATURE*) tvi.lParam;
1464 static LRESULT
1465 msi_seltree_menu( HWND hwnd, HTREEITEM hItem )
1467 MSIFEATURE *feature;
1468 union {
1469 RECT rc;
1470 POINT pt[2];
1471 HTREEITEM hItem;
1472 } u;
1473 UINT r;
1475 feature = msi_seltree_feature_from_item( hwnd, hItem );
1476 if (!feature)
1478 ERR("item %p feature was NULL\n", hItem);
1479 return 0;
1482 /* get the item's rectangle to put the menu just below it */
1483 u.hItem = hItem;
1484 SendMessageW( hwnd, TVM_GETITEMRECT, 0, (LPARAM) &u.rc );
1485 MapWindowPoints( hwnd, NULL, u.pt, 2 );
1487 r = msi_seltree_popup_menu( hwnd, u.rc.left, u.rc.top );
1489 switch (r)
1491 case INSTALLSTATE_LOCAL:
1492 case INSTALLSTATE_ADVERTISED:
1493 case INSTALLSTATE_ABSENT:
1494 feature->ActionRequest = r;
1495 feature->Action = r;
1496 break;
1497 default:
1498 FIXME("select feature and all children\n");
1501 /* update */
1502 msi_seltree_sync_item_state( hwnd, feature, hItem );
1504 return 0;
1507 static LRESULT WINAPI
1508 MSISelectionTree_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1510 struct msi_selection_tree_info *info;
1511 TVHITTESTINFO tvhti;
1512 HRESULT r;
1514 TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
1516 info = GetPropW(hWnd, szButtonData);
1518 switch( msg )
1520 case WM_LBUTTONDOWN:
1521 tvhti.pt.x = LOWORD( lParam );
1522 tvhti.pt.y = HIWORD( lParam );
1523 tvhti.flags = 0;
1524 tvhti.hItem = 0;
1525 r = CallWindowProcW(info->oldproc, hWnd, TVM_HITTEST, 0, (LPARAM) &tvhti );
1526 if (tvhti.flags & TVHT_ONITEMSTATEICON)
1527 return msi_seltree_menu( hWnd, tvhti.hItem );
1528 break;
1531 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1533 switch( msg )
1535 case WM_NCDESTROY:
1536 msi_free( info );
1537 RemovePropW( hWnd, szButtonData );
1538 break;
1540 return r;
1543 static void
1544 msi_seltree_add_child_features( MSIPACKAGE *package, HWND hwnd,
1545 LPCWSTR parent, HTREEITEM hParent )
1547 MSIFEATURE *feature;
1548 TVINSERTSTRUCTW tvis;
1549 HTREEITEM hitem;
1551 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
1553 if ( lstrcmpW( parent, feature->Feature_Parent ) )
1554 continue;
1556 if ( !feature->Title )
1557 continue;
1559 memset( &tvis, 0, sizeof tvis );
1560 tvis.hParent = hParent;
1561 tvis.hInsertAfter = TVI_SORT;
1562 tvis.u.item.mask = TVIF_TEXT | TVIF_PARAM;
1563 tvis.u.item.pszText = feature->Title;
1564 tvis.u.item.lParam = (LPARAM) feature;
1566 hitem = (HTREEITEM) SendMessageW( hwnd, TVM_INSERTITEMW, 0, (LPARAM) &tvis );
1567 if (!hitem)
1568 continue;
1570 msi_seltree_sync_item_state( hwnd, feature, hitem );
1571 msi_seltree_add_child_features( package, hwnd,
1572 feature->Feature, hitem );
1576 static void msi_seltree_create_imagelist( HWND hwnd )
1578 const int bm_width = 32, bm_height = 16, bm_count = 3;
1579 const int bm_resource = 0x1001;
1580 HIMAGELIST himl;
1581 int i;
1582 HBITMAP hbmp;
1584 himl = ImageList_Create( bm_width, bm_height, FALSE, 4, 0 );
1585 if (!himl)
1587 ERR("failed to create image list\n");
1588 return;
1591 for (i=0; i<bm_count; i++)
1593 hbmp = LoadBitmapW( msi_hInstance, MAKEINTRESOURCEW(i+bm_resource) );
1594 if (!hbmp)
1596 ERR("failed to load bitmap %d\n", i);
1597 break;
1601 * Add a dummy bitmap at offset zero because the treeview
1602 * can't use it as a state mask (zero means no user state).
1604 if (!i)
1605 ImageList_Add( himl, hbmp, NULL );
1607 ImageList_Add( himl, hbmp, NULL );
1610 SendMessageW( hwnd, TVM_SETIMAGELIST, TVSIL_STATE, (LPARAM)himl );
1613 static UINT msi_dialog_selection_tree( msi_dialog *dialog, MSIRECORD *rec )
1615 msi_control *control;
1616 LPCWSTR prop;
1617 MSIPACKAGE *package = dialog->package;
1618 DWORD style;
1619 struct msi_selection_tree_info *info;
1621 info = msi_alloc( sizeof *info );
1622 if (!info)
1623 return ERROR_FUNCTION_FAILED;
1625 /* create the treeview control */
1626 prop = MSI_RecordGetString( rec, 9 );
1627 style = TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT;
1628 style |= WS_GROUP | WS_VSCROLL;
1629 control = msi_dialog_add_control( dialog, rec, WC_TREEVIEWW, style );
1630 if (!control)
1632 msi_free(info);
1633 return ERROR_FUNCTION_FAILED;
1636 /* subclass */
1637 info->dialog = dialog;
1638 info->hwnd = control->hwnd;
1639 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1640 (LONG_PTR)MSISelectionTree_WndProc );
1641 SetPropW( control->hwnd, szButtonData, info );
1643 /* initialize it */
1644 msi_seltree_create_imagelist( control->hwnd );
1645 msi_seltree_add_child_features( package, control->hwnd, NULL, NULL );
1647 return ERROR_SUCCESS;
1650 struct control_handler msi_dialog_handler[] =
1652 { szText, msi_dialog_text_control },
1653 { szPushButton, msi_dialog_button_control },
1654 { szLine, msi_dialog_line_control },
1655 { szBitmap, msi_dialog_bitmap_control },
1656 { szCheckBox, msi_dialog_checkbox_control },
1657 { szScrollableText, msi_dialog_scrolltext_control },
1658 { szComboBox, msi_dialog_combo_control },
1659 { szEdit, msi_dialog_edit_control },
1660 { szMaskedEdit, msi_dialog_maskedit_control },
1661 { szPathEdit, msi_dialog_pathedit_control },
1662 { szProgressBar, msi_dialog_progress_bar },
1663 { szRadioButtonGroup, msi_dialog_radiogroup_control },
1664 { szIcon, msi_dialog_icon_control },
1665 { szSelectionTree, msi_dialog_selection_tree },
1668 #define NUM_CONTROL_TYPES (sizeof msi_dialog_handler/sizeof msi_dialog_handler[0])
1670 static UINT msi_dialog_create_controls( MSIRECORD *rec, LPVOID param )
1672 msi_dialog *dialog = param;
1673 LPCWSTR control_type;
1674 UINT i;
1676 /* find and call the function that can create this type of control */
1677 control_type = MSI_RecordGetString( rec, 3 );
1678 for( i=0; i<NUM_CONTROL_TYPES; i++ )
1679 if (!strcmpiW( msi_dialog_handler[i].control_type, control_type ))
1680 break;
1681 if( i != NUM_CONTROL_TYPES )
1682 msi_dialog_handler[i].func( dialog, rec );
1683 else
1684 ERR("no handler for element type %s\n", debugstr_w(control_type));
1686 return ERROR_SUCCESS;
1689 static UINT msi_dialog_fill_controls( msi_dialog *dialog )
1691 static const WCHAR query[] = {
1692 'S','E','L','E','C','T',' ','*',' ',
1693 'F','R','O','M',' ','C','o','n','t','r','o','l',' ',
1694 'W','H','E','R','E',' ',
1695 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
1696 UINT r;
1697 MSIQUERY *view = NULL;
1698 MSIPACKAGE *package = dialog->package;
1700 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
1702 /* query the Control table for all the elements of the control */
1703 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
1704 if( r != ERROR_SUCCESS )
1706 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
1707 return ERROR_INVALID_PARAMETER;
1710 r = MSI_IterateRecords( view, 0, msi_dialog_create_controls, dialog );
1711 msiobj_release( &view->hdr );
1713 return r;
1716 static UINT msi_dialog_set_control_condition( MSIRECORD *rec, LPVOID param )
1718 static const WCHAR szHide[] = { 'H','i','d','e',0 };
1719 static const WCHAR szShow[] = { 'S','h','o','w',0 };
1720 static const WCHAR szDisable[] = { 'D','i','s','a','b','l','e',0 };
1721 static const WCHAR szEnable[] = { 'E','n','a','b','l','e',0 };
1722 msi_dialog *dialog = param;
1723 msi_control *control;
1724 LPCWSTR name, action, condition;
1725 UINT r;
1727 name = MSI_RecordGetString( rec, 2 );
1728 action = MSI_RecordGetString( rec, 3 );
1729 condition = MSI_RecordGetString( rec, 4 );
1730 r = MSI_EvaluateConditionW( dialog->package, condition );
1731 control = msi_dialog_find_control( dialog, name );
1732 if( r == MSICONDITION_TRUE && control )
1734 TRACE("%s control %s\n", debugstr_w(action), debugstr_w(name));
1736 /* FIXME: case sensitive? */
1737 if(!lstrcmpW(action, szHide))
1738 ShowWindow(control->hwnd, SW_HIDE);
1739 else if(!strcmpW(action, szShow))
1740 ShowWindow(control->hwnd, SW_SHOW);
1741 else if(!strcmpW(action, szDisable))
1742 EnableWindow(control->hwnd, FALSE);
1743 else if(!strcmpW(action, szEnable))
1744 EnableWindow(control->hwnd, TRUE);
1745 else
1746 FIXME("Unhandled action %s\n", debugstr_w(action));
1749 return ERROR_SUCCESS;
1752 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog )
1754 static const WCHAR query[] = {
1755 'S','E','L','E','C','T',' ','*',' ',
1756 'F','R','O','M',' ',
1757 'C','o','n','t','r','o','l','C','o','n','d','i','t','i','o','n',' ',
1758 'W','H','E','R','E',' ',
1759 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0
1761 UINT r;
1762 MSIQUERY *view = NULL;
1763 MSIPACKAGE *package = dialog->package;
1765 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
1767 /* query the Control table for all the elements of the control */
1768 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
1769 if( r != ERROR_SUCCESS )
1771 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
1772 return ERROR_INVALID_PARAMETER;
1775 r = MSI_IterateRecords( view, 0, msi_dialog_set_control_condition, dialog );
1776 msiobj_release( &view->hdr );
1778 return r;
1781 UINT msi_dialog_reset( msi_dialog *dialog )
1783 /* FIXME: should restore the original values of any properties we changed */
1784 return msi_dialog_evaluate_control_conditions( dialog );
1787 /* figure out the height of 10 point MS Sans Serif */
1788 static INT msi_dialog_get_sans_serif_height( HWND hwnd )
1790 static const WCHAR szSansSerif[] = {
1791 'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0 };
1792 LOGFONTW lf;
1793 TEXTMETRICW tm;
1794 BOOL r;
1795 LONG height = 0;
1796 HFONT hFont, hOldFont;
1797 HDC hdc;
1799 hdc = GetDC( hwnd );
1800 if (hdc)
1802 memset( &lf, 0, sizeof lf );
1803 lf.lfHeight = MulDiv(10, GetDeviceCaps(hdc, LOGPIXELSY), 72);
1804 strcpyW( lf.lfFaceName, szSansSerif );
1805 hFont = CreateFontIndirectW(&lf);
1806 if (hFont)
1808 hOldFont = SelectObject( hdc, hFont );
1809 r = GetTextMetricsW( hdc, &tm );
1810 if (r)
1811 height = tm.tmHeight;
1812 SelectObject( hdc, hOldFont );
1813 DeleteObject( hFont );
1815 ReleaseDC( hwnd, hdc );
1817 return height;
1820 /* fetch the associated record from the Dialog table */
1821 static MSIRECORD *msi_get_dialog_record( msi_dialog *dialog )
1823 static const WCHAR query[] = {
1824 'S','E','L','E','C','T',' ','*',' ',
1825 'F','R','O','M',' ','D','i','a','l','o','g',' ',
1826 'W','H','E','R','E',' ',
1827 '`','D','i','a','l','o','g','`',' ','=',' ','\'','%','s','\'',0};
1828 MSIPACKAGE *package = dialog->package;
1829 MSIRECORD *rec = NULL;
1831 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
1833 rec = MSI_QueryGetRecord( package->db, query, dialog->name );
1834 if( !rec )
1835 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
1837 return rec;
1840 static void msi_dialog_adjust_dialog_size( msi_dialog *dialog, LPSIZE sz )
1842 RECT rect;
1843 LONG style;
1845 /* turn the client size into the window rectangle */
1846 rect.left = 0;
1847 rect.top = 0;
1848 rect.right = msi_dialog_scale_unit( dialog, sz->cx );
1849 rect.bottom = msi_dialog_scale_unit( dialog, sz->cy );
1850 style = GetWindowLongPtrW( dialog->hwnd, GWL_STYLE );
1851 AdjustWindowRect( &rect, style, FALSE );
1852 sz->cx = rect.right - rect.left;
1853 sz->cy = rect.bottom - rect.top;
1856 static BOOL msi_control_set_next( msi_control *control, msi_control *next )
1858 return SetWindowPos( next->hwnd, control->hwnd, 0, 0, 0, 0,
1859 SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOREDRAW |
1860 SWP_NOREPOSITION | SWP_NOSENDCHANGING | SWP_NOSIZE );
1863 static UINT msi_dialog_set_tab_order( msi_dialog *dialog )
1865 msi_control *control, *tab_next;
1867 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
1869 tab_next = msi_dialog_find_control( dialog, control->tabnext );
1870 if( !tab_next )
1871 continue;
1872 msi_control_set_next( control, tab_next );
1875 return ERROR_SUCCESS;
1878 static void msi_dialog_set_first_control( msi_dialog* dialog, LPCWSTR name )
1880 msi_control *control;
1882 control = msi_dialog_find_control( dialog, name );
1883 if( control )
1884 dialog->hWndFocus = control->hwnd;
1885 else
1886 dialog->hWndFocus = NULL;
1889 static LRESULT msi_dialog_oncreate( HWND hwnd, LPCREATESTRUCTW cs )
1891 static const WCHAR df[] = {
1892 'D','e','f','a','u','l','t','U','I','F','o','n','t',0 };
1893 static const WCHAR dfv[] = {
1894 'M','S',' ','S','h','e','l','l',' ','D','l','g',0 };
1895 msi_dialog *dialog = (msi_dialog*) cs->lpCreateParams;
1896 MSIRECORD *rec = NULL;
1897 LPWSTR title = NULL;
1898 SIZE size;
1900 TRACE("%p %p\n", dialog, dialog->package);
1902 dialog->hwnd = hwnd;
1903 SetWindowLongPtrW( hwnd, GWLP_USERDATA, (LONG_PTR) dialog );
1905 rec = msi_get_dialog_record( dialog );
1906 if( !rec )
1908 TRACE("No record found for dialog %s\n", debugstr_w(dialog->name));
1909 return -1;
1912 dialog->scale = msi_dialog_get_sans_serif_height(dialog->hwnd);
1914 size.cx = MSI_RecordGetInteger( rec, 4 );
1915 size.cy = MSI_RecordGetInteger( rec, 5 );
1916 msi_dialog_adjust_dialog_size( dialog, &size );
1918 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
1920 dialog->default_font = msi_dup_property( dialog->package, df );
1921 if (!dialog->default_font)
1923 dialog->default_font = strdupW(dfv);
1924 if (!dialog->default_font) return -1;
1927 title = msi_get_deformatted_field( dialog->package, rec, 7 );
1928 SetWindowTextW( hwnd, title );
1929 msi_free( title );
1931 SetWindowPos( hwnd, 0, 0, 0, size.cx, size.cy,
1932 SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW );
1934 msi_dialog_build_font_list( dialog );
1935 msi_dialog_fill_controls( dialog );
1936 msi_dialog_evaluate_control_conditions( dialog );
1937 msi_dialog_set_tab_order( dialog );
1938 msi_dialog_set_first_control( dialog, MSI_RecordGetString( rec, 8 ) );
1939 msiobj_release( &rec->hdr );
1941 return 0;
1944 static UINT msi_dialog_send_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
1946 LPWSTR event_fmt = NULL, arg_fmt = NULL;
1948 TRACE("Sending control event %s %s\n", debugstr_w(event), debugstr_w(arg));
1950 deformat_string( dialog->package, event, &event_fmt );
1951 deformat_string( dialog->package, arg, &arg_fmt );
1953 dialog->event_handler( dialog->package, event_fmt, arg_fmt, dialog );
1955 msi_free( event_fmt );
1956 msi_free( arg_fmt );
1958 return ERROR_SUCCESS;
1961 static UINT msi_dialog_set_property( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
1963 static const WCHAR szNullArg[] = { '{','}',0 };
1964 LPWSTR p, prop, arg_fmt = NULL;
1965 UINT len;
1967 len = strlenW(event);
1968 prop = msi_alloc( len*sizeof(WCHAR));
1969 strcpyW( prop, &event[1] );
1970 p = strchrW( prop, ']' );
1971 if( p && p[1] == 0 )
1973 *p = 0;
1974 if( strcmpW( szNullArg, arg ) )
1975 deformat_string( dialog->package, arg, &arg_fmt );
1976 MSI_SetPropertyW( dialog->package, prop, arg_fmt );
1977 msi_free( arg_fmt );
1979 else
1980 ERR("Badly formatted property string - what happens?\n");
1981 msi_free( prop );
1982 return ERROR_SUCCESS;
1985 static UINT msi_dialog_control_event( MSIRECORD *rec, LPVOID param )
1987 msi_dialog *dialog = param;
1988 LPCWSTR condition, event, arg;
1989 UINT r;
1991 condition = MSI_RecordGetString( rec, 5 );
1992 r = MSI_EvaluateConditionW( dialog->package, condition );
1993 if( r == MSICONDITION_TRUE || r == MSICONDITION_NONE )
1995 event = MSI_RecordGetString( rec, 3 );
1996 arg = MSI_RecordGetString( rec, 4 );
1997 if( event[0] == '[' )
1998 msi_dialog_set_property( dialog, event, arg );
1999 else
2000 msi_dialog_send_event( dialog, event, arg );
2003 return ERROR_SUCCESS;
2006 static UINT msi_dialog_button_handler( msi_dialog *dialog,
2007 msi_control *control, WPARAM param )
2009 static const WCHAR query[] = {
2010 'S','E','L','E','C','T',' ','*',' ',
2011 'F','R','O','M',' ','C','o','n','t','r','o','l','E','v','e','n','t',' ',
2012 'W','H','E','R','E',' ',
2013 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
2014 'A','N','D',' ',
2015 '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',' ',
2016 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','i','n','g','`',0
2018 MSIQUERY *view = NULL;
2019 UINT r;
2021 if( HIWORD(param) != BN_CLICKED )
2022 return ERROR_SUCCESS;
2024 r = MSI_OpenQuery( dialog->package->db, &view, query,
2025 dialog->name, control->name );
2026 if( r != ERROR_SUCCESS )
2028 ERR("query failed\n");
2029 return 0;
2032 r = MSI_IterateRecords( view, 0, msi_dialog_control_event, dialog );
2033 msiobj_release( &view->hdr );
2035 return r;
2038 static UINT msi_dialog_get_checkbox_state( msi_dialog *dialog,
2039 msi_control *control )
2041 WCHAR state[2] = { 0 };
2042 DWORD sz = 2;
2044 MSI_GetPropertyW( dialog->package, control->property, state, &sz );
2045 return state[0] ? 1 : 0;
2048 static void msi_dialog_set_checkbox_state( msi_dialog *dialog,
2049 msi_control *control, UINT state )
2051 static const WCHAR szState[] = { '1', 0 };
2052 LPCWSTR val;
2054 /* if uncheck then the property is set to NULL */
2055 if (!state)
2057 MSI_SetPropertyW( dialog->package, control->property, NULL );
2058 return;
2061 /* check for a custom state */
2062 if (control->value && control->value[0])
2063 val = control->value;
2064 else
2065 val = szState;
2067 MSI_SetPropertyW( dialog->package, control->property, val );
2070 static void msi_dialog_checkbox_sync_state( msi_dialog *dialog,
2071 msi_control *control )
2073 UINT state;
2075 state = msi_dialog_get_checkbox_state( dialog, control );
2076 SendMessageW( control->hwnd, BM_SETCHECK,
2077 state ? BST_CHECKED : BST_UNCHECKED, 0 );
2080 static UINT msi_dialog_checkbox_handler( msi_dialog *dialog,
2081 msi_control *control, WPARAM param )
2083 UINT state;
2085 if( HIWORD(param) != BN_CLICKED )
2086 return ERROR_SUCCESS;
2088 TRACE("clicked checkbox %s, set %s\n", debugstr_w(control->name),
2089 debugstr_w(control->property));
2091 state = msi_dialog_get_checkbox_state( dialog, control );
2092 state = state ? 0 : 1;
2093 msi_dialog_set_checkbox_state( dialog, control, state );
2094 msi_dialog_checkbox_sync_state( dialog, control );
2096 return msi_dialog_button_handler( dialog, control, param );
2099 static UINT msi_dialog_edit_handler( msi_dialog *dialog,
2100 msi_control *control, WPARAM param )
2102 UINT sz, r;
2103 LPWSTR buf;
2105 if( HIWORD(param) != EN_CHANGE )
2106 return ERROR_SUCCESS;
2108 TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
2109 debugstr_w(control->property));
2111 sz = 0x20;
2112 buf = msi_alloc( sz*sizeof(WCHAR) );
2113 while( buf )
2115 r = GetWindowTextW( control->hwnd, buf, sz );
2116 if( r < (sz-1) )
2117 break;
2118 sz *= 2;
2119 buf = msi_realloc( buf, sz*sizeof(WCHAR) );
2122 MSI_SetPropertyW( dialog->package, control->property, buf );
2124 msi_free( buf );
2126 return ERROR_SUCCESS;
2129 static UINT msi_dialog_radiogroup_handler( msi_dialog *dialog,
2130 msi_control *control, WPARAM param )
2132 if( HIWORD(param) != BN_CLICKED )
2133 return ERROR_SUCCESS;
2135 TRACE("clicked radio button %s, set %s\n", debugstr_w(control->name),
2136 debugstr_w(control->property));
2138 MSI_SetPropertyW( dialog->package, control->property, control->name );
2140 return msi_dialog_button_handler( dialog, control, param );
2143 static LRESULT msi_dialog_oncommand( msi_dialog *dialog, WPARAM param, HWND hwnd )
2145 msi_control *control = NULL;
2147 TRACE("%p %p %08x\n", dialog, hwnd, param);
2149 switch (param)
2151 case 1: /* enter */
2152 control = msi_dialog_find_control( dialog, dialog->control_default );
2153 break;
2154 case 2: /* escape */
2155 control = msi_dialog_find_control( dialog, dialog->control_cancel );
2156 break;
2157 default:
2158 control = msi_dialog_find_control_by_hwnd( dialog, hwnd );
2161 if( control )
2163 if( control->handler )
2165 control->handler( dialog, control, param );
2166 msi_dialog_evaluate_control_conditions( dialog );
2169 else
2170 ERR("button click from nowhere %p %d %p\n", dialog, param, hwnd);
2171 return 0;
2174 static void msi_dialog_setfocus( msi_dialog *dialog )
2176 HWND hwnd = dialog->hWndFocus;
2178 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, TRUE);
2179 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, FALSE);
2180 SetFocus( hwnd );
2181 dialog->hWndFocus = hwnd;
2184 static LRESULT WINAPI MSIDialog_WndProc( HWND hwnd, UINT msg,
2185 WPARAM wParam, LPARAM lParam )
2187 msi_dialog *dialog = (LPVOID) GetWindowLongPtrW( hwnd, GWLP_USERDATA );
2189 TRACE("0x%04x\n", msg);
2191 switch (msg)
2193 case WM_CREATE:
2194 return msi_dialog_oncreate( hwnd, (LPCREATESTRUCTW)lParam );
2196 case WM_COMMAND:
2197 return msi_dialog_oncommand( dialog, wParam, (HWND)lParam );
2199 case WM_ACTIVATE:
2200 if( LOWORD(wParam) == WA_INACTIVE )
2201 dialog->hWndFocus = GetFocus();
2202 else
2203 msi_dialog_setfocus( dialog );
2204 return 0;
2206 case WM_SETFOCUS:
2207 msi_dialog_setfocus( dialog );
2208 return 0;
2210 /* bounce back to our subclassed static control */
2211 case WM_CTLCOLORSTATIC:
2212 return SendMessageW( (HWND) lParam, WM_CTLCOLORSTATIC, wParam, lParam );
2214 case WM_DESTROY:
2215 dialog->hwnd = NULL;
2216 return 0;
2218 return DefWindowProcW(hwnd, msg, wParam, lParam);
2221 static BOOL CALLBACK msi_radioground_child_enum( HWND hWnd, LPARAM lParam )
2223 EnableWindow( hWnd, lParam );
2224 return TRUE;
2227 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2229 WNDPROC oldproc = (WNDPROC) GetPropW(hWnd, szButtonData);
2230 LRESULT r;
2232 TRACE("hWnd %p msg %04x wParam 0x%08x lParam 0x%08lx\n", hWnd, msg, wParam, lParam);
2234 if (msg == WM_COMMAND) /* Forward notifications to dialog */
2235 SendMessageW(GetParent(hWnd), msg, wParam, lParam);
2237 r = CallWindowProcW(oldproc, hWnd, msg, wParam, lParam);
2239 /* make sure the radio buttons show as disabled if the parent is disabled */
2240 if (msg == WM_ENABLE)
2241 EnumChildWindows( hWnd, msi_radioground_child_enum, wParam );
2243 return r;
2246 static LRESULT WINAPI MSIHiddenWindowProc( HWND hwnd, UINT msg,
2247 WPARAM wParam, LPARAM lParam )
2249 msi_dialog *dialog = (msi_dialog*) lParam;
2251 TRACE("%d %p\n", msg, dialog);
2253 switch (msg)
2255 case WM_MSI_DIALOG_CREATE:
2256 return msi_dialog_run_message_loop( dialog );
2257 case WM_MSI_DIALOG_DESTROY:
2258 msi_dialog_destroy( dialog );
2259 return 0;
2261 return DefWindowProcW( hwnd, msg, wParam, lParam );
2264 /* functions that interface to other modules within MSI */
2266 msi_dialog *msi_dialog_create( MSIPACKAGE* package, LPCWSTR szDialogName,
2267 msi_dialog_event_handler event_handler )
2269 MSIRECORD *rec = NULL;
2270 msi_dialog *dialog;
2272 TRACE("%p %s\n", package, debugstr_w(szDialogName));
2274 /* allocate the structure for the dialog to use */
2275 dialog = msi_alloc_zero( sizeof *dialog + sizeof(WCHAR)*strlenW(szDialogName) );
2276 if( !dialog )
2277 return NULL;
2278 strcpyW( dialog->name, szDialogName );
2279 msiobj_addref( &package->hdr );
2280 dialog->package = package;
2281 dialog->event_handler = event_handler;
2282 dialog->finished = 0;
2283 list_init( &dialog->controls );
2285 /* verify that the dialog exists */
2286 rec = msi_get_dialog_record( dialog );
2287 if( !rec )
2289 msiobj_release( &package->hdr );
2290 msi_free( dialog );
2291 return NULL;
2293 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
2294 dialog->control_default = strdupW( MSI_RecordGetString( rec, 9 ) );
2295 dialog->control_cancel = strdupW( MSI_RecordGetString( rec, 10 ) );
2296 msiobj_release( &rec->hdr );
2298 return dialog;
2301 static void msi_process_pending_messages( HWND hdlg )
2303 MSG msg;
2305 while( PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ) )
2307 if( hdlg && IsDialogMessageW( hdlg, &msg ))
2308 continue;
2309 TranslateMessage( &msg );
2310 DispatchMessageW( &msg );
2314 void msi_dialog_end_dialog( msi_dialog *dialog )
2316 TRACE("%p\n", dialog);
2317 dialog->finished = 1;
2318 PostMessageW(dialog->hwnd, WM_NULL, 0, 0);
2321 void msi_dialog_check_messages( HANDLE handle )
2323 DWORD r;
2325 /* in threads other than the UI thread, block */
2326 if( uiThreadId != GetCurrentThreadId() )
2328 if( handle )
2329 WaitForSingleObject( handle, INFINITE );
2330 return;
2333 /* there's two choices for the UI thread */
2334 while (1)
2336 msi_process_pending_messages( NULL );
2338 if( !handle )
2339 break;
2342 * block here until somebody creates a new dialog or
2343 * the handle we're waiting on becomes ready
2345 r = MsgWaitForMultipleObjects( 1, &handle, 0, INFINITE, QS_ALLINPUT );
2346 if( r == WAIT_OBJECT_0 )
2347 break;
2351 UINT msi_dialog_run_message_loop( msi_dialog *dialog )
2353 DWORD style;
2354 HWND hwnd;
2356 if( uiThreadId != GetCurrentThreadId() )
2357 return SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_CREATE, 0, (LPARAM) dialog );
2359 /* create the dialog window, don't show it yet */
2360 style = WS_OVERLAPPED;
2361 if( dialog->attributes & msidbDialogAttributesVisible )
2362 style |= WS_VISIBLE;
2364 hwnd = CreateWindowW( szMsiDialogClass, dialog->name, style,
2365 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
2366 NULL, NULL, NULL, dialog );
2367 if( !hwnd )
2369 ERR("Failed to create dialog %s\n", debugstr_w( dialog->name ));
2370 return ERROR_FUNCTION_FAILED;
2373 ShowWindow( hwnd, SW_SHOW );
2374 /* UpdateWindow( hwnd ); - and causes the transparent static controls not to paint */
2376 if( dialog->attributes & msidbDialogAttributesModal )
2378 while( !dialog->finished )
2380 MsgWaitForMultipleObjects( 0, NULL, 0, INFINITE, QS_ALLEVENTS );
2381 msi_process_pending_messages( dialog->hwnd );
2384 else
2385 return ERROR_IO_PENDING;
2387 return ERROR_SUCCESS;
2390 void msi_dialog_do_preview( msi_dialog *dialog )
2392 TRACE("\n");
2393 dialog->attributes |= msidbDialogAttributesVisible;
2394 dialog->attributes &= ~msidbDialogAttributesModal;
2395 msi_dialog_run_message_loop( dialog );
2398 void msi_dialog_destroy( msi_dialog *dialog )
2400 if( uiThreadId != GetCurrentThreadId() )
2402 SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_DESTROY, 0, (LPARAM) dialog );
2403 return;
2406 if( dialog->hwnd )
2407 ShowWindow( dialog->hwnd, SW_HIDE );
2409 if( dialog->hwnd )
2410 DestroyWindow( dialog->hwnd );
2412 /* destroy the list of controls */
2413 while( !list_empty( &dialog->controls ) )
2415 msi_control *t = LIST_ENTRY( list_head( &dialog->controls ),
2416 msi_control, entry );
2417 list_remove( &t->entry );
2418 /* leave dialog->hwnd - destroying parent destroys child windows */
2419 msi_free( t->property );
2420 msi_free( t->value );
2421 if( t->hBitmap )
2422 DeleteObject( t->hBitmap );
2423 if( t->hIcon )
2424 DestroyIcon( t->hIcon );
2425 msi_free( t->tabnext );
2426 msi_free( t );
2427 if (t->hDll)
2428 FreeLibrary( t->hDll );
2431 /* destroy the list of fonts */
2432 while( dialog->font_list )
2434 msi_font *t = dialog->font_list;
2435 dialog->font_list = t->next;
2436 DeleteObject( t->hfont );
2437 msi_free( t );
2439 msi_free( dialog->default_font );
2441 msi_free( dialog->control_default );
2442 msi_free( dialog->control_cancel );
2443 msiobj_release( &dialog->package->hdr );
2444 dialog->package = NULL;
2445 msi_free( dialog );
2448 BOOL msi_dialog_register_class( void )
2450 WNDCLASSW cls;
2452 ZeroMemory( &cls, sizeof cls );
2453 cls.lpfnWndProc = MSIDialog_WndProc;
2454 cls.hInstance = NULL;
2455 cls.hIcon = LoadIconW(0, (LPWSTR)IDI_APPLICATION);
2456 cls.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
2457 cls.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
2458 cls.lpszMenuName = NULL;
2459 cls.lpszClassName = szMsiDialogClass;
2461 if( !RegisterClassW( &cls ) )
2462 return FALSE;
2464 cls.lpfnWndProc = MSIHiddenWindowProc;
2465 cls.lpszClassName = szMsiHiddenWindow;
2467 if( !RegisterClassW( &cls ) )
2468 return FALSE;
2470 uiThreadId = GetCurrentThreadId();
2472 hMsiHiddenWindow = CreateWindowW( szMsiHiddenWindow, NULL, WS_OVERLAPPED,
2473 0, 0, 100, 100, NULL, NULL, NULL, NULL );
2474 if( !hMsiHiddenWindow )
2475 return FALSE;
2477 return TRUE;
2480 void msi_dialog_unregister_class( void )
2482 DestroyWindow( hMsiHiddenWindow );
2483 hMsiHiddenWindow = NULL;
2484 UnregisterClassW( szMsiDialogClass, NULL );
2485 UnregisterClassW( szMsiHiddenWindow, NULL );
2486 uiThreadId = 0;