d3d8: TRACE fixes.
[wine.git] / dlls / msi / dialog.c
blob64cbbbc6eee570b0b784808af73cde18ec78144f
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2005 Mike McCormack for CodeWeavers
5 * Copyright 2005 Aric Stewart for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define COBJMACROS
23 #define NONAMELESSUNION
25 #include <stdarg.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "winnls.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"
39 #include "winreg.h"
40 #include "shlwapi.h"
41 #include "msiserver.h"
42 #include "shellapi.h"
44 #include "wine/debug.h"
45 #include "wine/unicode.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(msi);
49 extern HINSTANCE msi_hInstance;
51 struct msi_control_tag;
52 typedef struct msi_control_tag msi_control;
53 typedef UINT (*msi_handler)( msi_dialog *, msi_control *, WPARAM );
54 typedef void (*msi_update)( msi_dialog *, msi_control * );
55 typedef UINT (*control_event_handler)( msi_dialog *, const WCHAR *, const WCHAR * );
57 struct msi_control_tag
59 struct list entry;
60 HWND hwnd;
61 msi_handler handler;
62 msi_update update;
63 LPWSTR property;
64 LPWSTR value;
65 HBITMAP hBitmap;
66 HICON hIcon;
67 LPWSTR tabnext;
68 LPWSTR type;
69 HMODULE hDll;
70 float progress_current;
71 float progress_max;
72 BOOL progress_backwards;
73 DWORD attributes;
74 WCHAR name[1];
77 typedef struct msi_font_tag
79 struct list entry;
80 HFONT hfont;
81 COLORREF color;
82 WCHAR name[1];
83 } msi_font;
85 struct msi_dialog_tag
87 MSIPACKAGE *package;
88 msi_dialog *parent;
89 control_event_handler event_handler;
90 BOOL finished;
91 INT scale;
92 DWORD attributes;
93 SIZE size;
94 HWND hwnd;
95 LPWSTR default_font;
96 struct list fonts;
97 struct list controls;
98 HWND hWndFocus;
99 LPWSTR control_default;
100 LPWSTR control_cancel;
101 WCHAR name[1];
104 struct subscriber
106 struct list entry;
107 msi_dialog *dialog;
108 WCHAR *event;
109 WCHAR *control;
110 WCHAR *attribute;
113 typedef UINT (*msi_dialog_control_func)( msi_dialog *dialog, MSIRECORD *rec );
114 struct control_handler
116 LPCWSTR control_type;
117 msi_dialog_control_func func;
120 typedef struct
122 msi_dialog* dialog;
123 msi_control *parent;
124 LPWSTR propval;
125 } radio_button_group_descr;
127 static const WCHAR szMsiDialogClass[] = { 'M','s','i','D','i','a','l','o','g','C','l','o','s','e','C','l','a','s','s',0 };
128 static const WCHAR szMsiHiddenWindow[] = { 'M','s','i','H','i','d','d','e','n','W','i','n','d','o','w',0 };
129 static const WCHAR szStatic[] = { 'S','t','a','t','i','c',0 };
130 static const WCHAR szButton[] = { 'B','U','T','T','O','N', 0 };
131 static const WCHAR szButtonData[] = { 'M','S','I','D','A','T','A',0 };
132 static const WCHAR szProgress[] = { 'P','r','o','g','r','e','s','s',0 };
133 static const WCHAR szText[] = { 'T','e','x','t',0 };
134 static const WCHAR szPushButton[] = { 'P','u','s','h','B','u','t','t','o','n',0 };
135 static const WCHAR szLine[] = { 'L','i','n','e',0 };
136 static const WCHAR szBitmap[] = { 'B','i','t','m','a','p',0 };
137 static const WCHAR szCheckBox[] = { 'C','h','e','c','k','B','o','x',0 };
138 static const WCHAR szScrollableText[] = { 'S','c','r','o','l','l','a','b','l','e','T','e','x','t',0 };
139 static const WCHAR szComboBox[] = { 'C','o','m','b','o','B','o','x',0 };
140 static const WCHAR szEdit[] = { 'E','d','i','t',0 };
141 static const WCHAR szMaskedEdit[] = { 'M','a','s','k','e','d','E','d','i','t',0 };
142 static const WCHAR szPathEdit[] = { 'P','a','t','h','E','d','i','t',0 };
143 static const WCHAR szProgressBar[] = { 'P','r','o','g','r','e','s','s','B','a','r',0 };
144 static const WCHAR szSetProgress[] = { 'S','e','t','P','r','o','g','r','e','s','s',0 };
145 static const WCHAR szRadioButtonGroup[] = { 'R','a','d','i','o','B','u','t','t','o','n','G','r','o','u','p',0 };
146 static const WCHAR szIcon[] = { 'I','c','o','n',0 };
147 static const WCHAR szSelectionTree[] = { 'S','e','l','e','c','t','i','o','n','T','r','e','e',0 };
148 static const WCHAR szGroupBox[] = { 'G','r','o','u','p','B','o','x',0 };
149 static const WCHAR szListBox[] = { 'L','i','s','t','B','o','x',0 };
150 static const WCHAR szDirectoryCombo[] = { 'D','i','r','e','c','t','o','r','y','C','o','m','b','o',0 };
151 static const WCHAR szDirectoryList[] = { 'D','i','r','e','c','t','o','r','y','L','i','s','t',0 };
152 static const WCHAR szVolumeCostList[] = { 'V','o','l','u','m','e','C','o','s','t','L','i','s','t',0 };
153 static const WCHAR szVolumeSelectCombo[] = { 'V','o','l','u','m','e','S','e','l','e','c','t','C','o','m','b','o',0 };
154 static const WCHAR szSelectionDescription[] = {'S','e','l','e','c','t','i','o','n','D','e','s','c','r','i','p','t','i','o','n',0};
155 static const WCHAR szSelectionPath[] = {'S','e','l','e','c','t','i','o','n','P','a','t','h',0};
156 static const WCHAR szHyperLink[] = {'H','y','p','e','r','L','i','n','k',0};
158 /* dialog sequencing */
160 #define WM_MSI_DIALOG_CREATE (WM_USER+0x100)
161 #define WM_MSI_DIALOG_DESTROY (WM_USER+0x101)
163 #define USER_INSTALLSTATE_ALL 0x1000
165 static DWORD uiThreadId;
166 static HWND hMsiHiddenWindow;
168 static LPWSTR msi_get_window_text( HWND hwnd )
170 UINT sz, r;
171 LPWSTR buf;
173 sz = 0x20;
174 buf = msi_alloc( sz*sizeof(WCHAR) );
175 while ( buf )
177 r = GetWindowTextW( hwnd, buf, sz );
178 if ( r < (sz - 1) )
179 break;
180 sz *= 2;
181 buf = msi_realloc( buf, sz*sizeof(WCHAR) );
184 return buf;
187 static INT msi_dialog_scale_unit( msi_dialog *dialog, INT val )
189 return MulDiv( val, dialog->scale, 12 );
192 static msi_control *msi_dialog_find_control( msi_dialog *dialog, LPCWSTR name )
194 msi_control *control;
196 if( !name )
197 return NULL;
198 if( !dialog->hwnd )
199 return NULL;
200 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
201 if( !strcmpW( control->name, name ) ) /* FIXME: case sensitive? */
202 return control;
203 return NULL;
206 static msi_control *msi_dialog_find_control_by_type( msi_dialog *dialog, LPCWSTR type )
208 msi_control *control;
210 if( !type )
211 return NULL;
212 if( !dialog->hwnd )
213 return NULL;
214 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
215 if( !strcmpW( control->type, type ) ) /* FIXME: case sensitive? */
216 return control;
217 return NULL;
220 static msi_control *msi_dialog_find_control_by_hwnd( msi_dialog *dialog, HWND hwnd )
222 msi_control *control;
224 if( !dialog->hwnd )
225 return NULL;
226 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
227 if( hwnd == control->hwnd )
228 return control;
229 return NULL;
232 static LPWSTR msi_get_deformatted_field( MSIPACKAGE *package, MSIRECORD *rec, int field )
234 LPCWSTR str = MSI_RecordGetString( rec, field );
235 LPWSTR ret = NULL;
237 if (str)
238 deformat_string( package, str, &ret );
239 return ret;
242 static LPWSTR msi_dialog_dup_property( msi_dialog *dialog, LPCWSTR property, BOOL indirect )
244 LPWSTR prop = NULL;
246 if (!property)
247 return NULL;
249 if (indirect)
250 prop = msi_dup_property( dialog->package->db, property );
252 if (!prop)
253 prop = strdupW( property );
255 return prop;
259 * msi_dialog_get_style
261 * Extract the {\style} string from the front of the text to display and
262 * update the pointer. Only the last style in a list is applied.
264 static LPWSTR msi_dialog_get_style( LPCWSTR p, LPCWSTR *rest )
266 LPWSTR ret;
267 LPCWSTR q, i, first;
268 DWORD len;
270 q = NULL;
271 *rest = p;
272 if( !p )
273 return NULL;
275 while ((first = strchrW( p, '{' )) && (q = strchrW( first + 1, '}' )))
277 p = first + 1;
278 if( *p != '\\' && *p != '&' )
279 return NULL;
281 /* little bit of sanity checking to stop us getting confused with RTF */
282 for( i=++p; i<q; i++ )
283 if( *i == '}' || *i == '\\' )
284 return NULL;
287 if (!q)
288 return NULL;
290 *rest = ++q;
291 len = q - p;
293 ret = msi_alloc( len*sizeof(WCHAR) );
294 if( !ret )
295 return ret;
296 memcpy( ret, p, len*sizeof(WCHAR) );
297 ret[len-1] = 0;
298 return ret;
301 static UINT msi_dialog_add_font( MSIRECORD *rec, LPVOID param )
303 msi_dialog *dialog = param;
304 msi_font *font;
305 LPCWSTR face, name;
306 LOGFONTW lf;
307 INT style;
308 HDC hdc;
310 /* create a font and add it to the list */
311 name = MSI_RecordGetString( rec, 1 );
312 font = msi_alloc( FIELD_OFFSET( msi_font, name[strlenW( name ) + 1] ));
313 strcpyW( font->name, name );
314 list_add_head( &dialog->fonts, &font->entry );
316 font->color = MSI_RecordGetInteger( rec, 4 );
318 memset( &lf, 0, sizeof lf );
319 face = MSI_RecordGetString( rec, 2 );
320 lf.lfHeight = MSI_RecordGetInteger( rec, 3 );
321 style = MSI_RecordGetInteger( rec, 5 );
322 if( style & msidbTextStyleStyleBitsBold )
323 lf.lfWeight = FW_BOLD;
324 if( style & msidbTextStyleStyleBitsItalic )
325 lf.lfItalic = TRUE;
326 if( style & msidbTextStyleStyleBitsUnderline )
327 lf.lfUnderline = TRUE;
328 if( style & msidbTextStyleStyleBitsStrike )
329 lf.lfStrikeOut = TRUE;
330 lstrcpynW( lf.lfFaceName, face, LF_FACESIZE );
332 /* adjust the height */
333 hdc = GetDC( dialog->hwnd );
334 if (hdc)
336 lf.lfHeight = -MulDiv(lf.lfHeight, GetDeviceCaps(hdc, LOGPIXELSY), 72);
337 ReleaseDC( dialog->hwnd, hdc );
340 font->hfont = CreateFontIndirectW( &lf );
342 TRACE("Adding font style %s\n", debugstr_w(font->name) );
344 return ERROR_SUCCESS;
347 static msi_font *msi_dialog_find_font( msi_dialog *dialog, LPCWSTR name )
349 msi_font *font = NULL;
351 LIST_FOR_EACH_ENTRY( font, &dialog->fonts, msi_font, entry )
352 if( !strcmpW( font->name, name ) ) /* FIXME: case sensitive? */
353 break;
355 return font;
358 static UINT msi_dialog_set_font( msi_dialog *dialog, HWND hwnd, LPCWSTR name )
360 msi_font *font;
362 font = msi_dialog_find_font( dialog, name );
363 if( font )
364 SendMessageW( hwnd, WM_SETFONT, (WPARAM) font->hfont, TRUE );
365 else
366 ERR("No font entry for %s\n", debugstr_w(name));
367 return ERROR_SUCCESS;
370 static UINT msi_dialog_build_font_list( msi_dialog *dialog )
372 static const WCHAR query[] = {
373 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
374 '`','T','e','x','t','S','t','y','l','e','`',0};
375 MSIQUERY *view;
376 UINT r;
378 TRACE("dialog %p\n", dialog );
380 r = MSI_OpenQuery( dialog->package->db, &view, query );
381 if( r != ERROR_SUCCESS )
382 return r;
384 r = MSI_IterateRecords( view, NULL, msi_dialog_add_font, dialog );
385 msiobj_release( &view->hdr );
386 return r;
389 static void msi_destroy_control( msi_control *t )
391 list_remove( &t->entry );
392 /* leave dialog->hwnd - destroying parent destroys child windows */
393 msi_free( t->property );
394 msi_free( t->value );
395 if( t->hBitmap )
396 DeleteObject( t->hBitmap );
397 if( t->hIcon )
398 DestroyIcon( t->hIcon );
399 msi_free( t->tabnext );
400 msi_free( t->type );
401 if (t->hDll)
402 FreeLibrary( t->hDll );
403 msi_free( t );
406 static msi_control *dialog_create_window( msi_dialog *dialog, MSIRECORD *rec, DWORD exstyle,
407 const WCHAR *szCls, const WCHAR *name, const WCHAR *text,
408 DWORD style, HWND parent )
410 DWORD x, y, width, height;
411 LPWSTR font = NULL, title_font = NULL;
412 LPCWSTR title = NULL;
413 msi_control *control;
415 style |= WS_CHILD;
417 control = msi_alloc( FIELD_OFFSET( msi_control, name[strlenW( name ) + 1] ));
418 if (!control)
419 return NULL;
421 strcpyW( control->name, name );
422 list_add_tail( &dialog->controls, &control->entry );
423 control->handler = NULL;
424 control->update = NULL;
425 control->property = NULL;
426 control->value = NULL;
427 control->hBitmap = NULL;
428 control->hIcon = NULL;
429 control->hDll = NULL;
430 control->tabnext = strdupW( MSI_RecordGetString( rec, 11) );
431 control->type = strdupW( MSI_RecordGetString( rec, 3 ) );
432 control->progress_current = 0;
433 control->progress_max = 100;
434 control->progress_backwards = FALSE;
436 x = MSI_RecordGetInteger( rec, 4 );
437 y = MSI_RecordGetInteger( rec, 5 );
438 width = MSI_RecordGetInteger( rec, 6 );
439 height = MSI_RecordGetInteger( rec, 7 );
441 x = msi_dialog_scale_unit( dialog, x );
442 y = msi_dialog_scale_unit( dialog, y );
443 width = msi_dialog_scale_unit( dialog, width );
444 height = msi_dialog_scale_unit( dialog, height );
446 if( text )
448 deformat_string( dialog->package, text, &title_font );
449 font = msi_dialog_get_style( title_font, &title );
452 control->hwnd = CreateWindowExW( exstyle, szCls, title, style,
453 x, y, width, height, parent, NULL, NULL, NULL );
455 TRACE("Dialog %s control %s hwnd %p\n",
456 debugstr_w(dialog->name), debugstr_w(text), control->hwnd );
458 msi_dialog_set_font( dialog, control->hwnd,
459 font ? font : dialog->default_font );
461 msi_free( title_font );
462 msi_free( font );
464 return control;
467 static LPWSTR msi_dialog_get_uitext( msi_dialog *dialog, LPCWSTR key )
469 MSIRECORD *rec;
470 LPWSTR text;
472 static const WCHAR query[] = {
473 's','e','l','e','c','t',' ','*',' ',
474 'f','r','o','m',' ','`','U','I','T','e','x','t','`',' ',
475 'w','h','e','r','e',' ','`','K','e','y','`',' ','=',' ','\'','%','s','\'',0
478 rec = MSI_QueryGetRecord( dialog->package->db, query, key );
479 if (!rec) return NULL;
480 text = strdupW( MSI_RecordGetString( rec, 2 ) );
481 msiobj_release( &rec->hdr );
482 return text;
485 static MSIRECORD *msi_get_binary_record( MSIDATABASE *db, LPCWSTR name )
487 static const WCHAR query[] = {
488 's','e','l','e','c','t',' ','*',' ',
489 'f','r','o','m',' ','B','i','n','a','r','y',' ',
490 'w','h','e','r','e',' ',
491 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0
494 return MSI_QueryGetRecord( db, query, name );
497 static HANDLE msi_load_image( MSIDATABASE *db, LPCWSTR name, UINT type,
498 UINT cx, UINT cy, UINT flags )
500 MSIRECORD *rec;
501 HANDLE himage = NULL;
502 LPWSTR tmp;
503 UINT r;
505 TRACE("%p %s %u %u %08x\n", db, debugstr_w(name), cx, cy, flags);
507 if (!(tmp = msi_create_temp_file( db ))) return NULL;
509 rec = msi_get_binary_record( db, name );
510 if( rec )
512 r = MSI_RecordStreamToFile( rec, 2, tmp );
513 if( r == ERROR_SUCCESS )
515 himage = LoadImageW( 0, tmp, type, cx, cy, flags );
517 msiobj_release( &rec->hdr );
519 DeleteFileW( tmp );
521 msi_free( tmp );
522 return himage;
525 static HICON msi_load_icon( MSIDATABASE *db, LPCWSTR text, UINT attributes )
527 DWORD cx = 0, cy = 0, flags;
529 flags = LR_LOADFROMFILE | LR_DEFAULTSIZE;
530 if( attributes & msidbControlAttributesFixedSize )
532 flags &= ~LR_DEFAULTSIZE;
533 if( attributes & msidbControlAttributesIconSize16 )
535 cx += 16;
536 cy += 16;
538 if( attributes & msidbControlAttributesIconSize32 )
540 cx += 32;
541 cy += 32;
543 /* msidbControlAttributesIconSize48 handled by above logic */
545 return msi_load_image( db, text, IMAGE_ICON, cx, cy, flags );
548 static void msi_dialog_update_controls( msi_dialog *dialog, LPCWSTR property )
550 msi_control *control;
552 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
554 if ( control->property && !strcmpW( control->property, property ) && control->update )
555 control->update( dialog, control );
559 static void msi_dialog_set_property( MSIPACKAGE *package, LPCWSTR property, LPCWSTR value )
561 UINT r = msi_set_property( package->db, property, value, -1 );
562 if (r == ERROR_SUCCESS && !strcmpW( property, szSourceDir ))
563 msi_reset_folders( package, TRUE );
566 static MSIFEATURE *msi_seltree_feature_from_item( HWND hwnd, HTREEITEM hItem )
568 TVITEMW tvi;
570 /* get the feature from the item */
571 memset( &tvi, 0, sizeof tvi );
572 tvi.hItem = hItem;
573 tvi.mask = TVIF_PARAM | TVIF_HANDLE;
574 SendMessageW( hwnd, TVM_GETITEMW, 0, (LPARAM)&tvi );
575 return (MSIFEATURE *)tvi.lParam;
578 struct msi_selection_tree_info
580 msi_dialog *dialog;
581 HWND hwnd;
582 WNDPROC oldproc;
583 HTREEITEM selected;
586 static MSIFEATURE *msi_seltree_get_selected_feature( msi_control *control )
588 struct msi_selection_tree_info *info = GetPropW( control->hwnd, szButtonData );
589 return msi_seltree_feature_from_item( control->hwnd, info->selected );
592 static void dialog_handle_event( msi_dialog *dialog, const WCHAR *control,
593 const WCHAR *attribute, MSIRECORD *rec )
595 msi_control* ctrl;
597 ctrl = msi_dialog_find_control( dialog, control );
598 if (!ctrl)
599 return;
600 if( !strcmpW( attribute, szText ) )
602 const WCHAR *font_text, *text = NULL;
603 WCHAR *font, *text_fmt = NULL;
605 font_text = MSI_RecordGetString( rec , 1 );
606 font = msi_dialog_get_style( font_text, &text );
607 deformat_string( dialog->package, text, &text_fmt );
608 if (text_fmt) text = text_fmt;
609 else text = szEmpty;
611 SetWindowTextW( ctrl->hwnd, text );
613 msi_free( font );
614 msi_free( text_fmt );
615 msi_dialog_check_messages( NULL );
617 else if( !strcmpW( attribute, szProgress ) )
619 DWORD func, val1, val2, units;
621 func = MSI_RecordGetInteger( rec, 1 );
622 val1 = MSI_RecordGetInteger( rec, 2 );
623 val2 = MSI_RecordGetInteger( rec, 3 );
625 TRACE("progress: func %u val1 %u val2 %u\n", func, val1, val2);
627 units = val1 / 512;
628 switch (func)
630 case 0: /* init */
631 SendMessageW( ctrl->hwnd, PBM_SETRANGE, 0, MAKELPARAM(0,100) );
632 if (val2)
634 ctrl->progress_max = units ? units : 100;
635 ctrl->progress_current = units;
636 ctrl->progress_backwards = TRUE;
637 SendMessageW( ctrl->hwnd, PBM_SETPOS, 100, 0 );
639 else
641 ctrl->progress_max = units ? units : 100;
642 ctrl->progress_current = 0;
643 ctrl->progress_backwards = FALSE;
644 SendMessageW( ctrl->hwnd, PBM_SETPOS, 0, 0 );
646 break;
647 case 1: /* action data increment */
648 if (val2) dialog->package->action_progress_increment = val1;
649 else dialog->package->action_progress_increment = 0;
650 break;
651 case 2: /* move */
652 if (ctrl->progress_backwards)
654 if (units >= ctrl->progress_current) ctrl->progress_current -= units;
655 else ctrl->progress_current = 0;
657 else
659 if (ctrl->progress_current + units < ctrl->progress_max) ctrl->progress_current += units;
660 else ctrl->progress_current = ctrl->progress_max;
662 SendMessageW( ctrl->hwnd, PBM_SETPOS, MulDiv(100, ctrl->progress_current, ctrl->progress_max), 0 );
663 break;
664 case 3: /* add */
665 ctrl->progress_max += units;
666 break;
667 default:
668 FIXME("Unknown progress message %u\n", func);
669 break;
672 else if ( !strcmpW( attribute, szProperty ) )
674 MSIFEATURE *feature = msi_seltree_get_selected_feature( ctrl );
675 if (feature) msi_dialog_set_property( dialog->package, ctrl->property, feature->Directory );
677 else if ( !strcmpW( attribute, szSelectionPath ) )
679 BOOL indirect = ctrl->attributes & msidbControlAttributesIndirect;
680 LPWSTR path = msi_dialog_dup_property( dialog, ctrl->property, indirect );
681 if (!path) return;
682 SetWindowTextW( ctrl->hwnd, path );
683 msi_free(path);
685 else
687 FIXME("Attribute %s not being set\n", debugstr_w(attribute));
688 return;
692 static void event_subscribe( msi_dialog *dialog, const WCHAR *event, const WCHAR *control, const WCHAR *attribute )
694 struct subscriber *sub;
696 TRACE("event %s control %s attribute %s\n", debugstr_w(event), debugstr_w(control), debugstr_w(attribute));
698 LIST_FOR_EACH_ENTRY( sub, &dialog->package->subscriptions, struct subscriber, entry )
700 if (!strcmpiW( sub->event, event ) &&
701 !strcmpiW( sub->control, control ) &&
702 !strcmpiW( sub->attribute, attribute ))
704 TRACE("already subscribed\n");
705 return;
708 if (!(sub = msi_alloc( sizeof(*sub) ))) return;
709 sub->dialog = dialog;
710 sub->event = strdupW( event );
711 sub->control = strdupW( control );
712 sub->attribute = strdupW( attribute );
713 list_add_tail( &dialog->package->subscriptions, &sub->entry );
716 struct dialog_control
718 msi_dialog *dialog;
719 const WCHAR *control;
722 static UINT map_event( MSIRECORD *row, void *param )
724 struct dialog_control *dc = param;
725 const WCHAR *event = MSI_RecordGetString( row, 3 );
726 const WCHAR *attribute = MSI_RecordGetString( row, 4 );
728 event_subscribe( dc->dialog, event, dc->control, attribute );
729 return ERROR_SUCCESS;
732 static void dialog_map_events( msi_dialog *dialog, const WCHAR *control )
734 static const WCHAR queryW[] =
735 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
736 '`','E','v','e','n','t','M','a','p','p','i','n','g','`',' ',
737 'W','H','E','R','E',' ','`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
738 'A','N','D',' ','`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',0};
739 MSIQUERY *view;
740 struct dialog_control dialog_control =
742 dialog,
743 control
746 if (!MSI_OpenQuery( dialog->package->db, &view, queryW, dialog->name, control ))
748 MSI_IterateRecords( view, NULL, map_event, &dialog_control );
749 msiobj_release( &view->hdr );
753 /* everything except radio buttons */
754 static msi_control *msi_dialog_add_control( msi_dialog *dialog,
755 MSIRECORD *rec, LPCWSTR szCls, DWORD style )
757 DWORD attributes;
758 const WCHAR *text = NULL, *name, *control_type;
759 DWORD exstyle = 0;
761 name = MSI_RecordGetString( rec, 2 );
762 control_type = MSI_RecordGetString( rec, 3 );
763 attributes = MSI_RecordGetInteger( rec, 8 );
764 if (strcmpW( control_type, szScrollableText )) text = MSI_RecordGetString( rec, 10 );
766 TRACE("%s, %s, %08x, %s, %08x\n", debugstr_w(szCls), debugstr_w(name),
767 attributes, debugstr_w(text), style);
769 if( attributes & msidbControlAttributesVisible )
770 style |= WS_VISIBLE;
771 if( ~attributes & msidbControlAttributesEnabled )
772 style |= WS_DISABLED;
773 if( attributes & msidbControlAttributesSunken )
774 exstyle |= WS_EX_CLIENTEDGE;
776 dialog_map_events( dialog, name );
778 return dialog_create_window( dialog, rec, exstyle, szCls, name, text, style, dialog->hwnd );
781 struct msi_text_info
783 msi_font *font;
784 WNDPROC oldproc;
785 DWORD attributes;
789 * we don't erase our own background,
790 * so we have to make sure that the parent window redraws first
792 static void msi_text_on_settext( HWND hWnd )
794 HWND hParent;
795 RECT rc;
797 hParent = GetParent( hWnd );
798 GetWindowRect( hWnd, &rc );
799 MapWindowPoints( NULL, hParent, (LPPOINT) &rc, 2 );
800 InvalidateRect( hParent, &rc, TRUE );
803 static LRESULT WINAPI
804 MSIText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
806 struct msi_text_info *info;
807 LRESULT r = 0;
809 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
811 info = GetPropW(hWnd, szButtonData);
813 if( msg == WM_CTLCOLORSTATIC &&
814 ( info->attributes & msidbControlAttributesTransparent ) )
816 SetBkMode( (HDC)wParam, TRANSPARENT );
817 return (LRESULT) GetStockObject(NULL_BRUSH);
820 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
821 if ( info->font )
822 SetTextColor( (HDC)wParam, info->font->color );
824 switch( msg )
826 case WM_SETTEXT:
827 msi_text_on_settext( hWnd );
828 break;
829 case WM_NCDESTROY:
830 msi_free( info );
831 RemovePropW( hWnd, szButtonData );
832 break;
835 return r;
838 static UINT msi_dialog_text_control( msi_dialog *dialog, MSIRECORD *rec )
840 msi_control *control;
841 struct msi_text_info *info;
842 LPCWSTR text, ptr, prop, control_name;
843 LPWSTR font_name;
845 TRACE("%p %p\n", dialog, rec);
847 control = msi_dialog_add_control( dialog, rec, szStatic, SS_LEFT | WS_GROUP );
848 if( !control )
849 return ERROR_FUNCTION_FAILED;
851 info = msi_alloc( sizeof *info );
852 if( !info )
853 return ERROR_SUCCESS;
855 control_name = MSI_RecordGetString( rec, 2 );
856 control->attributes = MSI_RecordGetInteger( rec, 8 );
857 prop = MSI_RecordGetString( rec, 9 );
858 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
860 text = MSI_RecordGetString( rec, 10 );
861 font_name = msi_dialog_get_style( text, &ptr );
862 info->font = ( font_name ) ? msi_dialog_find_font( dialog, font_name ) : NULL;
863 msi_free( font_name );
865 info->attributes = MSI_RecordGetInteger( rec, 8 );
866 if( info->attributes & msidbControlAttributesTransparent )
867 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT );
869 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
870 (LONG_PTR)MSIText_WndProc );
871 SetPropW( control->hwnd, szButtonData, info );
873 event_subscribe( dialog, szSelectionPath, control_name, szSelectionPath );
874 return ERROR_SUCCESS;
877 /* strip any leading text style label from text field */
878 static WCHAR *msi_get_binary_name( MSIPACKAGE *package, MSIRECORD *rec )
880 WCHAR *p, *text;
882 text = msi_get_deformatted_field( package, rec, 10 );
883 if (!text)
884 return NULL;
886 p = text;
887 while (*p && *p != '{') p++;
888 if (!*p++) return text;
890 while (*p && *p != '}') p++;
891 if (!*p++) return text;
893 p = strdupW( p );
894 msi_free( text );
895 return p;
898 static UINT msi_dialog_set_property_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
900 static const WCHAR szNullArg[] = {'{','}',0};
901 LPWSTR p, prop, arg_fmt = NULL;
902 UINT len;
904 len = strlenW( event );
905 prop = msi_alloc( len * sizeof(WCHAR) );
906 strcpyW( prop, &event[1] );
907 p = strchrW( prop, ']' );
908 if (p && (p[1] == 0 || p[1] == ' '))
910 *p = 0;
911 if (strcmpW( szNullArg, arg ))
912 deformat_string( dialog->package, arg, &arg_fmt );
913 msi_dialog_set_property( dialog->package, prop, arg_fmt );
914 msi_dialog_update_controls( dialog, prop );
915 msi_free( arg_fmt );
917 else ERR("Badly formatted property string - what happens?\n");
918 msi_free( prop );
919 return ERROR_SUCCESS;
922 static UINT msi_dialog_send_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
924 LPWSTR event_fmt = NULL, arg_fmt = NULL;
926 TRACE("Sending control event %s %s\n", debugstr_w(event), debugstr_w(arg));
928 deformat_string( dialog->package, event, &event_fmt );
929 deformat_string( dialog->package, arg, &arg_fmt );
931 dialog->event_handler( dialog, event_fmt, arg_fmt );
933 msi_free( event_fmt );
934 msi_free( arg_fmt );
936 return ERROR_SUCCESS;
939 static UINT msi_dialog_control_event( MSIRECORD *rec, LPVOID param )
941 msi_dialog *dialog = param;
942 LPCWSTR condition, event, arg;
943 UINT r;
945 condition = MSI_RecordGetString( rec, 5 );
946 r = MSI_EvaluateConditionW( dialog->package, condition );
947 if (r == MSICONDITION_TRUE || r == MSICONDITION_NONE)
949 event = MSI_RecordGetString( rec, 3 );
950 arg = MSI_RecordGetString( rec, 4 );
951 if (event[0] == '[')
952 msi_dialog_set_property_event( dialog, event, arg );
953 else
954 msi_dialog_send_event( dialog, event, arg );
956 return ERROR_SUCCESS;
959 static UINT msi_dialog_button_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
961 static const WCHAR query[] = {
962 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
963 'C','o','n','t','r','o','l','E','v','e','n','t',' ','W','H','E','R','E',' ',
964 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ','A','N','D',' ',
965 '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',' ',
966 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','i','n','g','`',0};
967 MSIQUERY *view;
968 UINT r;
970 if (HIWORD(param) != BN_CLICKED)
971 return ERROR_SUCCESS;
973 r = MSI_OpenQuery( dialog->package->db, &view, query, dialog->name, control->name );
974 if (r != ERROR_SUCCESS)
976 ERR("query failed\n");
977 return ERROR_SUCCESS;
979 r = MSI_IterateRecords( view, 0, msi_dialog_control_event, dialog );
980 msiobj_release( &view->hdr );
981 return r;
984 static UINT msi_dialog_button_control( msi_dialog *dialog, MSIRECORD *rec )
986 msi_control *control;
987 UINT attributes, style;
989 TRACE("%p %p\n", dialog, rec);
991 style = WS_TABSTOP;
992 attributes = MSI_RecordGetInteger( rec, 8 );
993 if( attributes & msidbControlAttributesIcon )
994 style |= BS_ICON;
996 control = msi_dialog_add_control( dialog, rec, szButton, style );
997 if( !control )
998 return ERROR_FUNCTION_FAILED;
1000 control->handler = msi_dialog_button_handler;
1002 if (attributes & msidbControlAttributesIcon)
1004 /* set the icon */
1005 LPWSTR name = msi_get_binary_name( dialog->package, rec );
1006 control->hIcon = msi_load_icon( dialog->package->db, name, attributes );
1007 if (control->hIcon)
1009 SendMessageW( control->hwnd, BM_SETIMAGE, IMAGE_ICON, (LPARAM) control->hIcon );
1011 else
1012 ERR("Failed to load icon %s\n", debugstr_w(name));
1013 msi_free( name );
1016 return ERROR_SUCCESS;
1019 static LPWSTR msi_get_checkbox_value( msi_dialog *dialog, LPCWSTR prop )
1021 static const WCHAR query[] = {
1022 'S','E','L','E','C','T',' ','*',' ',
1023 'F','R','O','M',' ','`','C','h','e','c','k','B','o','x','`',' ',
1024 'W','H','E','R','E',' ',
1025 '`','P','r','o','p','e','r','t','y','`',' ','=',' ',
1026 '\'','%','s','\'',0
1028 MSIRECORD *rec = NULL;
1029 LPWSTR ret = NULL;
1031 /* find if there is a value associated with the checkbox */
1032 rec = MSI_QueryGetRecord( dialog->package->db, query, prop );
1033 if (!rec)
1034 return ret;
1036 ret = msi_get_deformatted_field( dialog->package, rec, 2 );
1037 if( ret && !ret[0] )
1039 msi_free( ret );
1040 ret = NULL;
1042 msiobj_release( &rec->hdr );
1043 if (ret)
1044 return ret;
1046 ret = msi_dup_property( dialog->package->db, prop );
1047 if( ret && !ret[0] )
1049 msi_free( ret );
1050 ret = NULL;
1053 return ret;
1056 static UINT msi_dialog_get_checkbox_state( msi_dialog *dialog, msi_control *control )
1058 WCHAR state[2] = {0};
1059 DWORD sz = 2;
1061 msi_get_property( dialog->package->db, control->property, state, &sz );
1062 return state[0] ? 1 : 0;
1065 static void msi_dialog_set_checkbox_state( msi_dialog *dialog, msi_control *control, UINT state )
1067 static const WCHAR szState[] = {'1',0};
1068 LPCWSTR val;
1070 /* if uncheck then the property is set to NULL */
1071 if (!state)
1073 msi_dialog_set_property( dialog->package, control->property, NULL );
1074 return;
1077 /* check for a custom state */
1078 if (control->value && control->value[0])
1079 val = control->value;
1080 else
1081 val = szState;
1083 msi_dialog_set_property( dialog->package, control->property, val );
1086 static void msi_dialog_checkbox_sync_state( msi_dialog *dialog, msi_control *control )
1088 UINT state = msi_dialog_get_checkbox_state( dialog, control );
1089 SendMessageW( control->hwnd, BM_SETCHECK, state ? BST_CHECKED : BST_UNCHECKED, 0 );
1092 static UINT msi_dialog_checkbox_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
1094 UINT state;
1096 if (HIWORD(param) != BN_CLICKED)
1097 return ERROR_SUCCESS;
1099 TRACE("clicked checkbox %s, set %s\n", debugstr_w(control->name), debugstr_w(control->property));
1101 state = msi_dialog_get_checkbox_state( dialog, control );
1102 state = state ? 0 : 1;
1103 msi_dialog_set_checkbox_state( dialog, control, state );
1104 msi_dialog_checkbox_sync_state( dialog, control );
1106 return msi_dialog_button_handler( dialog, control, param );
1109 static UINT msi_dialog_checkbox_control( msi_dialog *dialog, MSIRECORD *rec )
1111 msi_control *control;
1112 LPCWSTR prop;
1114 TRACE("%p %p\n", dialog, rec);
1116 control = msi_dialog_add_control( dialog, rec, szButton, BS_CHECKBOX | BS_MULTILINE | WS_TABSTOP );
1117 control->handler = msi_dialog_checkbox_handler;
1118 control->update = msi_dialog_checkbox_sync_state;
1119 prop = MSI_RecordGetString( rec, 9 );
1120 if (prop)
1122 control->property = strdupW( prop );
1123 control->value = msi_get_checkbox_value( dialog, prop );
1124 TRACE("control %s value %s\n", debugstr_w(control->property), debugstr_w(control->value));
1126 msi_dialog_checkbox_sync_state( dialog, control );
1127 return ERROR_SUCCESS;
1130 static UINT msi_dialog_line_control( msi_dialog *dialog, MSIRECORD *rec )
1132 DWORD attributes;
1133 LPCWSTR name;
1134 DWORD style, exstyle = 0;
1135 DWORD x, y, width, height;
1136 msi_control *control;
1138 TRACE("%p %p\n", dialog, rec);
1140 style = WS_CHILD | SS_ETCHEDHORZ | SS_SUNKEN;
1142 name = MSI_RecordGetString( rec, 2 );
1143 attributes = MSI_RecordGetInteger( rec, 8 );
1145 if( attributes & msidbControlAttributesVisible )
1146 style |= WS_VISIBLE;
1147 if( ~attributes & msidbControlAttributesEnabled )
1148 style |= WS_DISABLED;
1149 if( attributes & msidbControlAttributesSunken )
1150 exstyle |= WS_EX_CLIENTEDGE;
1152 dialog_map_events( dialog, name );
1154 control = msi_alloc( FIELD_OFFSET(msi_control, name[strlenW( name ) + 1] ));
1155 if (!control)
1156 return ERROR_OUTOFMEMORY;
1158 strcpyW( control->name, name );
1159 list_add_head( &dialog->controls, &control->entry );
1160 control->handler = NULL;
1161 control->property = NULL;
1162 control->value = NULL;
1163 control->hBitmap = NULL;
1164 control->hIcon = NULL;
1165 control->hDll = NULL;
1166 control->tabnext = strdupW( MSI_RecordGetString( rec, 11) );
1167 control->type = strdupW( MSI_RecordGetString( rec, 3 ) );
1168 control->progress_current = 0;
1169 control->progress_max = 100;
1170 control->progress_backwards = FALSE;
1172 x = MSI_RecordGetInteger( rec, 4 );
1173 y = MSI_RecordGetInteger( rec, 5 );
1174 width = MSI_RecordGetInteger( rec, 6 );
1176 x = msi_dialog_scale_unit( dialog, x );
1177 y = msi_dialog_scale_unit( dialog, y );
1178 width = msi_dialog_scale_unit( dialog, width );
1179 height = 2; /* line is exactly 2 units in height */
1181 control->hwnd = CreateWindowExW( exstyle, szStatic, NULL, style,
1182 x, y, width, height, dialog->hwnd, NULL, NULL, NULL );
1184 TRACE("Dialog %s control %s hwnd %p\n",
1185 debugstr_w(dialog->name), debugstr_w(name), control->hwnd );
1187 return ERROR_SUCCESS;
1190 /******************** Scroll Text ********************************************/
1192 struct msi_scrolltext_info
1194 msi_dialog *dialog;
1195 msi_control *control;
1196 WNDPROC oldproc;
1199 static LRESULT WINAPI
1200 MSIScrollText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1202 struct msi_scrolltext_info *info;
1203 HRESULT r;
1205 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1207 info = GetPropW( hWnd, szButtonData );
1209 r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
1211 switch( msg )
1213 case WM_GETDLGCODE:
1214 return DLGC_WANTARROWS;
1215 case WM_NCDESTROY:
1216 msi_free( info );
1217 RemovePropW( hWnd, szButtonData );
1218 break;
1219 case WM_PAINT:
1220 /* native MSI sets a wait cursor here */
1221 msi_dialog_button_handler( info->dialog, info->control, BN_CLICKED );
1222 break;
1224 return r;
1227 struct msi_streamin_info
1229 LPSTR string;
1230 DWORD offset;
1231 DWORD length;
1234 static DWORD CALLBACK
1235 msi_richedit_stream_in( DWORD_PTR arg, LPBYTE buffer, LONG count, LONG *pcb )
1237 struct msi_streamin_info *info = (struct msi_streamin_info*) arg;
1239 if( (count + info->offset) > info->length )
1240 count = info->length - info->offset;
1241 memcpy( buffer, &info->string[ info->offset ], count );
1242 *pcb = count;
1243 info->offset += count;
1245 TRACE("%d/%d\n", info->offset, info->length);
1247 return 0;
1250 static void msi_scrolltext_add_text( msi_control *control, LPCWSTR text )
1252 struct msi_streamin_info info;
1253 EDITSTREAM es;
1255 info.string = strdupWtoA( text );
1256 info.offset = 0;
1257 info.length = lstrlenA( info.string ) + 1;
1259 es.dwCookie = (DWORD_PTR) &info;
1260 es.dwError = 0;
1261 es.pfnCallback = msi_richedit_stream_in;
1263 SendMessageW( control->hwnd, EM_STREAMIN, SF_RTF, (LPARAM) &es );
1265 msi_free( info.string );
1268 static UINT msi_dialog_scrolltext_control( msi_dialog *dialog, MSIRECORD *rec )
1270 static const WCHAR szRichEdit20W[] = {'R','i','c','h','E','d','i','t','2','0','W',0};
1271 struct msi_scrolltext_info *info;
1272 msi_control *control;
1273 HMODULE hRichedit;
1274 LPCWSTR text;
1275 DWORD style;
1277 info = msi_alloc( sizeof *info );
1278 if (!info)
1279 return ERROR_FUNCTION_FAILED;
1281 hRichedit = LoadLibraryA("riched20");
1283 style = WS_BORDER | ES_MULTILINE | WS_VSCROLL |
1284 ES_READONLY | ES_AUTOVSCROLL | WS_TABSTOP;
1285 control = msi_dialog_add_control( dialog, rec, szRichEdit20W, style );
1286 if (!control)
1288 FreeLibrary( hRichedit );
1289 msi_free( info );
1290 return ERROR_FUNCTION_FAILED;
1293 control->hDll = hRichedit;
1295 info->dialog = dialog;
1296 info->control = control;
1298 /* subclass the static control */
1299 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1300 (LONG_PTR)MSIScrollText_WndProc );
1301 SetPropW( control->hwnd, szButtonData, info );
1303 /* add the text into the richedit */
1304 text = MSI_RecordGetString( rec, 10 );
1305 if (text)
1306 msi_scrolltext_add_text( control, text );
1308 return ERROR_SUCCESS;
1311 static HBITMAP msi_load_picture( MSIDATABASE *db, LPCWSTR name,
1312 INT cx, INT cy, DWORD flags )
1314 HBITMAP hOleBitmap = 0, hBitmap = 0, hOldSrcBitmap, hOldDestBitmap;
1315 MSIRECORD *rec = NULL;
1316 IStream *stm = NULL;
1317 IPicture *pic = NULL;
1318 HDC srcdc, destdc;
1319 BITMAP bm;
1320 UINT r;
1322 rec = msi_get_binary_record( db, name );
1323 if( !rec )
1324 goto end;
1326 r = MSI_RecordGetIStream( rec, 2, &stm );
1327 msiobj_release( &rec->hdr );
1328 if( r != ERROR_SUCCESS )
1329 goto end;
1331 r = OleLoadPicture( stm, 0, TRUE, &IID_IPicture, (LPVOID*) &pic );
1332 IStream_Release( stm );
1333 if( FAILED( r ) )
1335 ERR("failed to load picture\n");
1336 goto end;
1339 r = IPicture_get_Handle( pic, (OLE_HANDLE*) &hOleBitmap );
1340 if( FAILED( r ) )
1342 ERR("failed to get bitmap handle\n");
1343 goto end;
1346 /* make the bitmap the desired size */
1347 r = GetObjectW( hOleBitmap, sizeof bm, &bm );
1348 if (r != sizeof bm )
1350 ERR("failed to get bitmap size\n");
1351 goto end;
1354 if (flags & LR_DEFAULTSIZE)
1356 cx = bm.bmWidth;
1357 cy = bm.bmHeight;
1360 srcdc = CreateCompatibleDC( NULL );
1361 hOldSrcBitmap = SelectObject( srcdc, hOleBitmap );
1362 destdc = CreateCompatibleDC( NULL );
1363 hBitmap = CreateCompatibleBitmap( srcdc, cx, cy );
1364 hOldDestBitmap = SelectObject( destdc, hBitmap );
1365 StretchBlt( destdc, 0, 0, cx, cy,
1366 srcdc, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
1367 SelectObject( srcdc, hOldSrcBitmap );
1368 SelectObject( destdc, hOldDestBitmap );
1369 DeleteDC( srcdc );
1370 DeleteDC( destdc );
1372 end:
1373 if ( pic )
1374 IPicture_Release( pic );
1375 return hBitmap;
1378 static UINT msi_dialog_bitmap_control( msi_dialog *dialog, MSIRECORD *rec )
1380 UINT cx, cy, flags, style, attributes;
1381 msi_control *control;
1382 LPWSTR name;
1384 flags = LR_LOADFROMFILE;
1385 style = SS_BITMAP | SS_LEFT | WS_GROUP;
1387 attributes = MSI_RecordGetInteger( rec, 8 );
1388 if( attributes & msidbControlAttributesFixedSize )
1390 flags |= LR_DEFAULTSIZE;
1391 style |= SS_CENTERIMAGE;
1394 control = msi_dialog_add_control( dialog, rec, szStatic, style );
1395 cx = MSI_RecordGetInteger( rec, 6 );
1396 cy = MSI_RecordGetInteger( rec, 7 );
1397 cx = msi_dialog_scale_unit( dialog, cx );
1398 cy = msi_dialog_scale_unit( dialog, cy );
1400 name = msi_get_binary_name( dialog->package, rec );
1401 control->hBitmap = msi_load_picture( dialog->package->db, name, cx, cy, flags );
1402 if( control->hBitmap )
1403 SendMessageW( control->hwnd, STM_SETIMAGE,
1404 IMAGE_BITMAP, (LPARAM) control->hBitmap );
1405 else
1406 ERR("Failed to load bitmap %s\n", debugstr_w(name));
1408 msi_free( name );
1410 return ERROR_SUCCESS;
1413 static UINT msi_dialog_icon_control( msi_dialog *dialog, MSIRECORD *rec )
1415 msi_control *control;
1416 DWORD attributes;
1417 LPWSTR name;
1419 TRACE("\n");
1421 control = msi_dialog_add_control( dialog, rec, szStatic,
1422 SS_ICON | SS_CENTERIMAGE | WS_GROUP );
1424 attributes = MSI_RecordGetInteger( rec, 8 );
1425 name = msi_get_binary_name( dialog->package, rec );
1426 control->hIcon = msi_load_icon( dialog->package->db, name, attributes );
1427 if( control->hIcon )
1428 SendMessageW( control->hwnd, STM_SETICON, (WPARAM) control->hIcon, 0 );
1429 else
1430 ERR("Failed to load bitmap %s\n", debugstr_w(name));
1431 msi_free( name );
1432 return ERROR_SUCCESS;
1435 /******************** Combo Box ***************************************/
1437 struct msi_combobox_info
1439 msi_dialog *dialog;
1440 HWND hwnd;
1441 WNDPROC oldproc;
1442 DWORD num_items;
1443 DWORD addpos_items;
1444 LPWSTR *items;
1447 static LRESULT WINAPI MSIComboBox_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1449 struct msi_combobox_info *info;
1450 LRESULT r;
1451 DWORD j;
1453 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1455 info = GetPropW( hWnd, szButtonData );
1456 if (!info)
1457 return 0;
1459 r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
1461 switch (msg)
1463 case WM_NCDESTROY:
1464 for (j = 0; j < info->num_items; j++)
1465 msi_free( info->items[j] );
1466 msi_free( info->items );
1467 msi_free( info );
1468 RemovePropW( hWnd, szButtonData );
1469 break;
1472 return r;
1475 static UINT msi_combobox_add_item( MSIRECORD *rec, LPVOID param )
1477 struct msi_combobox_info *info = param;
1478 LPCWSTR value, text;
1479 int pos;
1481 value = MSI_RecordGetString( rec, 3 );
1482 text = MSI_RecordGetString( rec, 4 );
1484 info->items[info->addpos_items] = strdupW( value );
1486 pos = SendMessageW( info->hwnd, CB_ADDSTRING, 0, (LPARAM)text );
1487 SendMessageW( info->hwnd, CB_SETITEMDATA, pos, (LPARAM)info->items[info->addpos_items] );
1488 info->addpos_items++;
1490 return ERROR_SUCCESS;
1493 static UINT msi_combobox_add_items( struct msi_combobox_info *info, LPCWSTR property )
1495 static const WCHAR query[] = {
1496 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1497 '`','C','o','m','b','o','B','o','x','`',' ','W','H','E','R','E',' ',
1498 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',' ',
1499 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','`',0};
1500 MSIQUERY *view;
1501 DWORD count;
1502 UINT r;
1504 r = MSI_OpenQuery( info->dialog->package->db, &view, query, property );
1505 if (r != ERROR_SUCCESS)
1506 return r;
1508 /* just get the number of records */
1509 count = 0;
1510 r = MSI_IterateRecords( view, &count, NULL, NULL );
1511 if (r != ERROR_SUCCESS)
1513 msiobj_release( &view->hdr );
1514 return r;
1516 info->num_items = count;
1517 info->items = msi_alloc( sizeof(*info->items) * count );
1519 r = MSI_IterateRecords( view, NULL, msi_combobox_add_item, info );
1520 msiobj_release( &view->hdr );
1521 return r;
1524 static UINT msi_dialog_set_control_condition( MSIRECORD *rec, LPVOID param )
1526 static const WCHAR szHide[] = {'H','i','d','e',0};
1527 static const WCHAR szShow[] = {'S','h','o','w',0};
1528 static const WCHAR szDisable[] = {'D','i','s','a','b','l','e',0};
1529 static const WCHAR szEnable[] = {'E','n','a','b','l','e',0};
1530 static const WCHAR szDefault[] = {'D','e','f','a','u','l','t',0};
1531 msi_dialog *dialog = param;
1532 msi_control *control;
1533 LPCWSTR name, action, condition;
1534 UINT r;
1536 name = MSI_RecordGetString( rec, 2 );
1537 action = MSI_RecordGetString( rec, 3 );
1538 condition = MSI_RecordGetString( rec, 4 );
1539 r = MSI_EvaluateConditionW( dialog->package, condition );
1540 control = msi_dialog_find_control( dialog, name );
1541 if (r == MSICONDITION_TRUE && control)
1543 TRACE("%s control %s\n", debugstr_w(action), debugstr_w(name));
1545 /* FIXME: case sensitive? */
1546 if (!strcmpW( action, szHide ))
1547 ShowWindow(control->hwnd, SW_HIDE);
1548 else if (!strcmpW( action, szShow ))
1549 ShowWindow(control->hwnd, SW_SHOW);
1550 else if (!strcmpW( action, szDisable ))
1551 EnableWindow(control->hwnd, FALSE);
1552 else if (!strcmpW( action, szEnable ))
1553 EnableWindow(control->hwnd, TRUE);
1554 else if (!strcmpW( action, szDefault ))
1555 SetFocus(control->hwnd);
1556 else
1557 FIXME("Unhandled action %s\n", debugstr_w(action));
1559 return ERROR_SUCCESS;
1562 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog )
1564 static const WCHAR query[] = {
1565 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1566 'C','o','n','t','r','o','l','C','o','n','d','i','t','i','o','n',' ',
1567 'W','H','E','R','E',' ','`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
1568 UINT r;
1569 MSIQUERY *view;
1570 MSIPACKAGE *package = dialog->package;
1572 TRACE("%p %s\n", dialog, debugstr_w(dialog->name));
1574 /* query the Control table for all the elements of the control */
1575 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
1576 if (r != ERROR_SUCCESS)
1577 return ERROR_SUCCESS;
1579 r = MSI_IterateRecords( view, 0, msi_dialog_set_control_condition, dialog );
1580 msiobj_release( &view->hdr );
1581 return r;
1584 static UINT msi_dialog_combobox_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
1586 struct msi_combobox_info *info;
1587 int index;
1588 LPWSTR value;
1590 if (HIWORD(param) != CBN_SELCHANGE && HIWORD(param) != CBN_EDITCHANGE)
1591 return ERROR_SUCCESS;
1593 info = GetPropW( control->hwnd, szButtonData );
1594 index = SendMessageW( control->hwnd, CB_GETCURSEL, 0, 0 );
1595 if (index == CB_ERR)
1596 value = msi_get_window_text( control->hwnd );
1597 else
1598 value = (LPWSTR) SendMessageW( control->hwnd, CB_GETITEMDATA, index, 0 );
1600 msi_dialog_set_property( info->dialog->package, control->property, value );
1601 msi_dialog_evaluate_control_conditions( info->dialog );
1603 if (index == CB_ERR)
1604 msi_free( value );
1606 return ERROR_SUCCESS;
1609 static void msi_dialog_combobox_update( msi_dialog *dialog, msi_control *control )
1611 struct msi_combobox_info *info;
1612 LPWSTR value, tmp;
1613 DWORD j;
1615 info = GetPropW( control->hwnd, szButtonData );
1617 value = msi_dup_property( dialog->package->db, control->property );
1618 if (!value)
1620 SendMessageW( control->hwnd, CB_SETCURSEL, -1, 0 );
1621 return;
1624 for (j = 0; j < info->num_items; j++)
1626 tmp = (LPWSTR) SendMessageW( control->hwnd, CB_GETITEMDATA, j, 0 );
1627 if (!strcmpW( value, tmp ))
1628 break;
1631 if (j < info->num_items)
1633 SendMessageW( control->hwnd, CB_SETCURSEL, j, 0 );
1635 else
1637 SendMessageW( control->hwnd, CB_SETCURSEL, -1, 0 );
1638 SetWindowTextW( control->hwnd, value );
1641 msi_free(value);
1644 static UINT msi_dialog_combo_control( msi_dialog *dialog, MSIRECORD *rec )
1646 struct msi_combobox_info *info;
1647 msi_control *control;
1648 DWORD attributes, style;
1649 LPCWSTR prop;
1651 info = msi_alloc( sizeof *info );
1652 if (!info)
1653 return ERROR_FUNCTION_FAILED;
1655 style = CBS_AUTOHSCROLL | WS_TABSTOP | WS_GROUP | WS_CHILD;
1656 attributes = MSI_RecordGetInteger( rec, 8 );
1657 if ( ~attributes & msidbControlAttributesSorted)
1658 style |= CBS_SORT;
1659 if ( attributes & msidbControlAttributesComboList)
1660 style |= CBS_DROPDOWNLIST;
1661 else
1662 style |= CBS_DROPDOWN;
1664 control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
1665 if (!control)
1667 msi_free( info );
1668 return ERROR_FUNCTION_FAILED;
1671 control->handler = msi_dialog_combobox_handler;
1672 control->update = msi_dialog_combobox_update;
1674 prop = MSI_RecordGetString( rec, 9 );
1675 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
1677 /* subclass */
1678 info->dialog = dialog;
1679 info->hwnd = control->hwnd;
1680 info->items = NULL;
1681 info->addpos_items = 0;
1682 info->oldproc = (WNDPROC)SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1683 (LONG_PTR)MSIComboBox_WndProc );
1684 SetPropW( control->hwnd, szButtonData, info );
1686 if (control->property)
1687 msi_combobox_add_items( info, control->property );
1689 msi_dialog_combobox_update( dialog, control );
1691 return ERROR_SUCCESS;
1694 static UINT msi_dialog_edit_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
1696 LPWSTR buf;
1698 if (HIWORD(param) != EN_CHANGE)
1699 return ERROR_SUCCESS;
1701 TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name), debugstr_w(control->property));
1703 buf = msi_get_window_text( control->hwnd );
1704 msi_dialog_set_property( dialog->package, control->property, buf );
1705 msi_free( buf );
1707 return ERROR_SUCCESS;
1710 /* length of 2^32 + 1 */
1711 #define MAX_NUM_DIGITS 11
1713 static UINT msi_dialog_edit_control( msi_dialog *dialog, MSIRECORD *rec )
1715 msi_control *control;
1716 LPCWSTR prop, text;
1717 LPWSTR val, begin, end;
1718 WCHAR num[MAX_NUM_DIGITS];
1719 DWORD limit;
1721 control = msi_dialog_add_control( dialog, rec, szEdit,
1722 WS_BORDER | WS_TABSTOP | ES_AUTOHSCROLL );
1723 control->handler = msi_dialog_edit_handler;
1725 text = MSI_RecordGetString( rec, 10 );
1726 if ( text )
1728 begin = strchrW( text, '{' );
1729 end = strchrW( text, '}' );
1731 if ( begin && end && end > begin &&
1732 begin[0] >= '0' && begin[0] <= '9' &&
1733 end - begin < MAX_NUM_DIGITS)
1735 lstrcpynW( num, begin + 1, end - begin );
1736 limit = atolW( num );
1738 SendMessageW( control->hwnd, EM_SETLIMITTEXT, limit, 0 );
1742 prop = MSI_RecordGetString( rec, 9 );
1743 if( prop )
1744 control->property = strdupW( prop );
1746 val = msi_dup_property( dialog->package->db, control->property );
1747 SetWindowTextW( control->hwnd, val );
1748 msi_free( val );
1749 return ERROR_SUCCESS;
1752 /******************** Masked Edit ********************************************/
1754 #define MASK_MAX_GROUPS 20
1756 struct msi_mask_group
1758 UINT len;
1759 UINT ofs;
1760 WCHAR type;
1761 HWND hwnd;
1764 struct msi_maskedit_info
1766 msi_dialog *dialog;
1767 WNDPROC oldproc;
1768 HWND hwnd;
1769 LPWSTR prop;
1770 UINT num_chars;
1771 UINT num_groups;
1772 struct msi_mask_group group[MASK_MAX_GROUPS];
1775 static BOOL msi_mask_editable( WCHAR type )
1777 switch (type)
1779 case '%':
1780 case '#':
1781 case '&':
1782 case '`':
1783 case '?':
1784 case '^':
1785 return TRUE;
1787 return FALSE;
1790 static void msi_mask_control_change( struct msi_maskedit_info *info )
1792 LPWSTR val;
1793 UINT i, n, r;
1795 val = msi_alloc( (info->num_chars+1)*sizeof(WCHAR) );
1796 for( i=0, n=0; i<info->num_groups; i++ )
1798 if (info->group[i].len == ~0u)
1800 UINT len = SendMessageW( info->group[i].hwnd, WM_GETTEXTLENGTH, 0, 0 );
1801 val = msi_realloc( val, (len + 1) * sizeof(WCHAR) );
1802 GetWindowTextW( info->group[i].hwnd, val, len + 1 );
1804 else
1806 if (info->group[i].len + n > info->num_chars)
1808 ERR("can't fit control %d text into template\n",i);
1809 break;
1811 if (!msi_mask_editable(info->group[i].type))
1813 for(r=0; r<info->group[i].len; r++)
1814 val[n+r] = info->group[i].type;
1815 val[n+r] = 0;
1817 else
1819 r = GetWindowTextW( info->group[i].hwnd, &val[n], info->group[i].len+1 );
1820 if( r != info->group[i].len )
1821 break;
1823 n += r;
1827 TRACE("%d/%d controls were good\n", i, info->num_groups);
1829 if( i == info->num_groups )
1831 TRACE("Set property %s to %s\n", debugstr_w(info->prop), debugstr_w(val));
1832 msi_dialog_set_property( info->dialog->package, info->prop, val );
1833 msi_dialog_evaluate_control_conditions( info->dialog );
1835 msi_free( val );
1838 /* now move to the next control if necessary */
1839 static VOID msi_mask_next_control( struct msi_maskedit_info *info, HWND hWnd )
1841 HWND hWndNext;
1842 UINT len, i;
1844 for( i=0; i<info->num_groups; i++ )
1845 if( info->group[i].hwnd == hWnd )
1846 break;
1848 /* don't move from the last control */
1849 if( i >= (info->num_groups-1) )
1850 return;
1852 len = SendMessageW( hWnd, WM_GETTEXTLENGTH, 0, 0 );
1853 if( len < info->group[i].len )
1854 return;
1856 hWndNext = GetNextDlgTabItem( GetParent( hWnd ), hWnd, FALSE );
1857 SetFocus( hWndNext );
1860 static LRESULT WINAPI
1861 MSIMaskedEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1863 struct msi_maskedit_info *info;
1864 HRESULT r;
1866 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1868 info = GetPropW(hWnd, szButtonData);
1870 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1872 switch( msg )
1874 case WM_COMMAND:
1875 if (HIWORD(wParam) == EN_CHANGE)
1877 msi_mask_control_change( info );
1878 msi_mask_next_control( info, (HWND) lParam );
1880 break;
1881 case WM_NCDESTROY:
1882 msi_free( info->prop );
1883 msi_free( info );
1884 RemovePropW( hWnd, szButtonData );
1885 break;
1888 return r;
1891 /* fish the various bits of the property out and put them in the control */
1892 static void
1893 msi_maskedit_set_text( struct msi_maskedit_info *info, LPCWSTR text )
1895 LPCWSTR p;
1896 UINT i;
1898 p = text;
1899 for( i = 0; i < info->num_groups; i++ )
1901 if( info->group[i].len < strlenW( p ) )
1903 LPWSTR chunk = strdupW( p );
1904 chunk[ info->group[i].len ] = 0;
1905 SetWindowTextW( info->group[i].hwnd, chunk );
1906 msi_free( chunk );
1908 else
1910 SetWindowTextW( info->group[i].hwnd, p );
1911 break;
1913 p += info->group[i].len;
1917 static struct msi_maskedit_info * msi_dialog_parse_groups( LPCWSTR mask )
1919 struct msi_maskedit_info *info;
1920 int i = 0, n = 0, total = 0;
1921 LPCWSTR p;
1923 TRACE("masked control, template %s\n", debugstr_w(mask));
1925 if( !mask )
1926 return NULL;
1928 info = msi_alloc_zero( sizeof *info );
1929 if( !info )
1930 return info;
1932 p = strchrW(mask, '<');
1933 if( p )
1934 p++;
1935 else
1936 p = mask;
1938 for( i=0; i<MASK_MAX_GROUPS; i++ )
1940 /* stop at the end of the string */
1941 if( p[0] == 0 || p[0] == '>' )
1943 if (!total)
1945 /* create a group for the empty mask */
1946 info->group[0].type = '&';
1947 info->group[0].len = ~0u;
1948 i = 1;
1950 break;
1953 /* count the number of the same identifier */
1954 for( n=0; p[n] == p[0]; n++ )
1956 info->group[i].ofs = total;
1957 info->group[i].type = p[0];
1958 if( p[n] == '=' )
1960 n++;
1961 total++; /* an extra not part of the group */
1963 info->group[i].len = n;
1964 total += n;
1965 p += n;
1968 TRACE("%d characters in %d groups\n", total, i );
1969 if( i == MASK_MAX_GROUPS )
1970 ERR("too many groups in PIDTemplate %s\n", debugstr_w(mask));
1972 info->num_chars = total;
1973 info->num_groups = i;
1975 return info;
1978 static void
1979 msi_maskedit_create_children( struct msi_maskedit_info *info, LPCWSTR font )
1981 DWORD width, height, style, wx, ww;
1982 RECT rect;
1983 HWND hwnd;
1984 UINT i;
1986 style = WS_CHILD | WS_BORDER | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL;
1988 GetClientRect( info->hwnd, &rect );
1990 width = rect.right - rect.left;
1991 height = rect.bottom - rect.top;
1993 for( i = 0; i < info->num_groups; i++ )
1995 if (!msi_mask_editable( info->group[i].type ))
1996 continue;
1997 if (info->num_chars)
1999 wx = (info->group[i].ofs * width) / info->num_chars;
2000 ww = (info->group[i].len * width) / info->num_chars;
2002 else
2004 wx = 0;
2005 ww = width;
2007 hwnd = CreateWindowW( szEdit, NULL, style, wx, 0, ww, height,
2008 info->hwnd, NULL, NULL, NULL );
2009 if( !hwnd )
2011 ERR("failed to create mask edit sub window\n");
2012 break;
2015 SendMessageW( hwnd, EM_LIMITTEXT, info->group[i].len, 0 );
2017 msi_dialog_set_font( info->dialog, hwnd,
2018 font?font:info->dialog->default_font );
2019 info->group[i].hwnd = hwnd;
2024 * office 2003 uses "73931<````=````=````=````=`````>@@@@@"
2025 * delphi 7 uses "<????-??????-??????-????>" and "<???-???>"
2026 * filemaker pro 7 uses "<^^^^=^^^^=^^^^=^^^^=^^^^=^^^^=^^^^^>"
2028 static UINT msi_dialog_maskedit_control( msi_dialog *dialog, MSIRECORD *rec )
2030 LPWSTR font_mask, val = NULL, font;
2031 struct msi_maskedit_info *info = NULL;
2032 UINT ret = ERROR_SUCCESS;
2033 msi_control *control;
2034 LPCWSTR prop, mask;
2036 TRACE("\n");
2038 font_mask = msi_get_deformatted_field( dialog->package, rec, 10 );
2039 font = msi_dialog_get_style( font_mask, &mask );
2040 if( !mask )
2042 WARN("mask template is empty\n");
2043 goto end;
2046 info = msi_dialog_parse_groups( mask );
2047 if( !info )
2049 ERR("template %s is invalid\n", debugstr_w(mask));
2050 goto end;
2053 info->dialog = dialog;
2055 control = msi_dialog_add_control( dialog, rec, szStatic,
2056 SS_OWNERDRAW | WS_GROUP | WS_VISIBLE );
2057 if( !control )
2059 ERR("Failed to create maskedit container\n");
2060 ret = ERROR_FUNCTION_FAILED;
2061 goto end;
2063 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
2065 info->hwnd = control->hwnd;
2067 /* subclass the static control */
2068 info->oldproc = (WNDPROC) SetWindowLongPtrW( info->hwnd, GWLP_WNDPROC,
2069 (LONG_PTR)MSIMaskedEdit_WndProc );
2070 SetPropW( control->hwnd, szButtonData, info );
2072 prop = MSI_RecordGetString( rec, 9 );
2073 if( prop )
2074 info->prop = strdupW( prop );
2076 msi_maskedit_create_children( info, font );
2078 if( prop )
2080 val = msi_dup_property( dialog->package->db, prop );
2081 if( val )
2083 msi_maskedit_set_text( info, val );
2084 msi_free( val );
2088 end:
2089 if( ret != ERROR_SUCCESS )
2090 msi_free( info );
2091 msi_free( font_mask );
2092 msi_free( font );
2093 return ret;
2096 /******************** Progress Bar *****************************************/
2098 static UINT msi_dialog_progress_bar( msi_dialog *dialog, MSIRECORD *rec )
2100 msi_control *control;
2101 DWORD attributes, style;
2103 style = WS_VISIBLE;
2104 attributes = MSI_RecordGetInteger( rec, 8 );
2105 if( !(attributes & msidbControlAttributesProgress95) )
2106 style |= PBS_SMOOTH;
2108 control = msi_dialog_add_control( dialog, rec, PROGRESS_CLASSW, style );
2109 if( !control )
2110 return ERROR_FUNCTION_FAILED;
2112 event_subscribe( dialog, szSetProgress, control->name, szProgress );
2113 return ERROR_SUCCESS;
2116 /******************** Path Edit ********************************************/
2118 struct msi_pathedit_info
2120 msi_dialog *dialog;
2121 msi_control *control;
2122 WNDPROC oldproc;
2125 static void msi_dialog_update_pathedit( msi_dialog *dialog, msi_control *control )
2127 LPWSTR prop, path;
2128 BOOL indirect;
2130 if (!control && !(control = msi_dialog_find_control_by_type( dialog, szPathEdit )))
2131 return;
2133 indirect = control->attributes & msidbControlAttributesIndirect;
2134 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2135 path = msi_dialog_dup_property( dialog, prop, TRUE );
2137 SetWindowTextW( control->hwnd, path );
2138 SendMessageW( control->hwnd, EM_SETSEL, 0, -1 );
2140 msi_free( path );
2141 msi_free( prop );
2144 /* FIXME: test when this should fail */
2145 static BOOL msi_dialog_verify_path( LPWSTR path )
2147 if ( !path[0] )
2148 return FALSE;
2150 if ( PathIsRelativeW( path ) )
2151 return FALSE;
2153 return TRUE;
2156 /* returns TRUE if the path is valid, FALSE otherwise */
2157 static BOOL msi_dialog_onkillfocus( msi_dialog *dialog, msi_control *control )
2159 LPWSTR buf, prop;
2160 BOOL indirect;
2161 BOOL valid;
2163 indirect = control->attributes & msidbControlAttributesIndirect;
2164 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2166 buf = msi_get_window_text( control->hwnd );
2168 if ( !msi_dialog_verify_path( buf ) )
2170 /* FIXME: display an error message box */
2171 ERR("Invalid path %s\n", debugstr_w( buf ));
2172 valid = FALSE;
2173 SetFocus( control->hwnd );
2175 else
2177 valid = TRUE;
2178 msi_dialog_set_property( dialog->package, prop, buf );
2181 msi_dialog_update_pathedit( dialog, control );
2183 TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
2184 debugstr_w(prop));
2186 msi_free( buf );
2187 msi_free( prop );
2189 return valid;
2192 static LRESULT WINAPI MSIPathEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2194 struct msi_pathedit_info *info = GetPropW(hWnd, szButtonData);
2195 LRESULT r = 0;
2197 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
2199 if ( msg == WM_KILLFOCUS )
2201 /* if the path is invalid, don't handle this message */
2202 if ( !msi_dialog_onkillfocus( info->dialog, info->control ) )
2203 return 0;
2206 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
2208 if ( msg == WM_NCDESTROY )
2210 msi_free( info );
2211 RemovePropW( hWnd, szButtonData );
2214 return r;
2217 static UINT msi_dialog_pathedit_control( msi_dialog *dialog, MSIRECORD *rec )
2219 struct msi_pathedit_info *info;
2220 msi_control *control;
2221 LPCWSTR prop;
2223 info = msi_alloc( sizeof *info );
2224 if (!info)
2225 return ERROR_FUNCTION_FAILED;
2227 control = msi_dialog_add_control( dialog, rec, szEdit,
2228 WS_BORDER | WS_TABSTOP );
2229 control->attributes = MSI_RecordGetInteger( rec, 8 );
2230 prop = MSI_RecordGetString( rec, 9 );
2231 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2233 info->dialog = dialog;
2234 info->control = control;
2235 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2236 (LONG_PTR)MSIPathEdit_WndProc );
2237 SetPropW( control->hwnd, szButtonData, info );
2239 msi_dialog_update_pathedit( dialog, control );
2241 return ERROR_SUCCESS;
2244 static UINT msi_dialog_radiogroup_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
2246 if (HIWORD(param) != BN_CLICKED)
2247 return ERROR_SUCCESS;
2249 TRACE("clicked radio button %s, set %s\n", debugstr_w(control->name), debugstr_w(control->property));
2251 msi_dialog_set_property( dialog->package, control->property, control->name );
2253 return msi_dialog_button_handler( dialog, control, param );
2256 /* radio buttons are a bit different from normal controls */
2257 static UINT msi_dialog_create_radiobutton( MSIRECORD *rec, LPVOID param )
2259 radio_button_group_descr *group = param;
2260 msi_dialog *dialog = group->dialog;
2261 msi_control *control;
2262 LPCWSTR prop, text, name;
2263 DWORD style = WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_AUTORADIOBUTTON | BS_MULTILINE;
2265 name = MSI_RecordGetString( rec, 3 );
2266 text = MSI_RecordGetString( rec, 8 );
2268 control = dialog_create_window( dialog, rec, 0, szButton, name, text, style,
2269 group->parent->hwnd );
2270 if (!control)
2271 return ERROR_FUNCTION_FAILED;
2272 control->handler = msi_dialog_radiogroup_handler;
2274 if (group->propval && !strcmpW( control->name, group->propval ))
2275 SendMessageW(control->hwnd, BM_SETCHECK, BST_CHECKED, 0);
2277 prop = MSI_RecordGetString( rec, 1 );
2278 if( prop )
2279 control->property = strdupW( prop );
2281 return ERROR_SUCCESS;
2284 static BOOL CALLBACK msi_radioground_child_enum( HWND hWnd, LPARAM lParam )
2286 EnableWindow( hWnd, lParam );
2287 return TRUE;
2290 static LRESULT WINAPI MSIRadioGroup_WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
2292 WNDPROC oldproc = (WNDPROC)GetPropW( hWnd, szButtonData );
2293 LRESULT r;
2295 TRACE("hWnd %p msg %04x wParam 0x%08lx lParam 0x%08lx\n", hWnd, msg, wParam, lParam);
2297 if (msg == WM_COMMAND) /* Forward notifications to dialog */
2298 SendMessageW( GetParent( hWnd ), msg, wParam, lParam );
2300 r = CallWindowProcW( oldproc, hWnd, msg, wParam, lParam );
2302 /* make sure the radio buttons show as disabled if the parent is disabled */
2303 if (msg == WM_ENABLE)
2304 EnumChildWindows( hWnd, msi_radioground_child_enum, wParam );
2306 return r;
2309 static UINT msi_dialog_radiogroup_control( msi_dialog *dialog, MSIRECORD *rec )
2311 static const WCHAR query[] = {
2312 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2313 'R','a','d','i','o','B','u','t','t','o','n',' ','W','H','E','R','E',' ',
2314 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
2315 UINT r;
2316 LPCWSTR prop;
2317 msi_control *control;
2318 MSIQUERY *view;
2319 radio_button_group_descr group;
2320 MSIPACKAGE *package = dialog->package;
2321 WNDPROC oldproc;
2322 DWORD attr, style = WS_GROUP;
2324 prop = MSI_RecordGetString( rec, 9 );
2326 TRACE("%p %p %s\n", dialog, rec, debugstr_w( prop ));
2328 attr = MSI_RecordGetInteger( rec, 8 );
2329 if (attr & msidbControlAttributesVisible)
2330 style |= WS_VISIBLE;
2331 if (~attr & msidbControlAttributesEnabled)
2332 style |= WS_DISABLED;
2333 if (attr & msidbControlAttributesHasBorder)
2334 style |= BS_GROUPBOX;
2335 else
2336 style |= BS_OWNERDRAW;
2338 /* Create parent group box to hold radio buttons */
2339 control = msi_dialog_add_control( dialog, rec, szButton, style );
2340 if( !control )
2341 return ERROR_FUNCTION_FAILED;
2343 oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2344 (LONG_PTR)MSIRadioGroup_WndProc );
2345 SetPropW(control->hwnd, szButtonData, oldproc);
2346 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
2348 if( prop )
2349 control->property = strdupW( prop );
2351 /* query the Radio Button table for all control in this group */
2352 r = MSI_OpenQuery( package->db, &view, query, prop );
2353 if( r != ERROR_SUCCESS )
2355 ERR("query failed for dialog %s radio group %s\n",
2356 debugstr_w(dialog->name), debugstr_w(prop));
2357 return ERROR_INVALID_PARAMETER;
2360 group.dialog = dialog;
2361 group.parent = control;
2362 group.propval = msi_dup_property( dialog->package->db, control->property );
2364 r = MSI_IterateRecords( view, 0, msi_dialog_create_radiobutton, &group );
2365 msiobj_release( &view->hdr );
2366 msi_free( group.propval );
2367 return r;
2370 static void
2371 msi_seltree_sync_item_state( HWND hwnd, MSIFEATURE *feature, HTREEITEM hItem )
2373 TVITEMW tvi;
2374 DWORD index = feature->ActionRequest;
2376 TRACE("Feature %s -> %d %d %d\n", debugstr_w(feature->Title),
2377 feature->Installed, feature->Action, feature->ActionRequest);
2379 if (index == INSTALLSTATE_UNKNOWN)
2380 index = INSTALLSTATE_ABSENT;
2382 tvi.mask = TVIF_STATE;
2383 tvi.hItem = hItem;
2384 tvi.state = INDEXTOSTATEIMAGEMASK( index );
2385 tvi.stateMask = TVIS_STATEIMAGEMASK;
2387 SendMessageW( hwnd, TVM_SETITEMW, 0, (LPARAM) &tvi );
2390 static UINT
2391 msi_seltree_popup_menu( HWND hwnd, INT x, INT y )
2393 HMENU hMenu;
2394 INT r;
2396 /* create a menu to display */
2397 hMenu = CreatePopupMenu();
2399 /* FIXME: load strings from resources */
2400 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_LOCAL, "Install feature locally");
2401 AppendMenuA( hMenu, MF_ENABLED, USER_INSTALLSTATE_ALL, "Install entire feature");
2402 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ADVERTISED, "Install on demand");
2403 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ABSENT, "Don't install");
2404 r = TrackPopupMenu( hMenu, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RETURNCMD,
2405 x, y, 0, hwnd, NULL );
2406 DestroyMenu( hMenu );
2407 return r;
2410 static void
2411 msi_seltree_update_feature_installstate( HWND hwnd, HTREEITEM hItem,
2412 MSIPACKAGE *package, MSIFEATURE *feature, INSTALLSTATE state )
2414 feature->ActionRequest = state;
2415 msi_seltree_sync_item_state( hwnd, feature, hItem );
2416 ACTION_UpdateComponentStates( package, feature );
2419 static void
2420 msi_seltree_update_siblings_and_children_installstate( HWND hwnd, HTREEITEM curr,
2421 MSIPACKAGE *package, INSTALLSTATE state)
2423 /* update all siblings */
2426 MSIFEATURE *feature;
2427 HTREEITEM child;
2429 feature = msi_seltree_feature_from_item( hwnd, curr );
2430 msi_seltree_update_feature_installstate( hwnd, curr, package, feature, state );
2432 /* update this sibling's children */
2433 child = (HTREEITEM)SendMessageW( hwnd, TVM_GETNEXTITEM, (WPARAM)TVGN_CHILD, (LPARAM)curr );
2434 if (child)
2435 msi_seltree_update_siblings_and_children_installstate( hwnd, child,
2436 package, state );
2438 while ((curr = (HTREEITEM)SendMessageW( hwnd, TVM_GETNEXTITEM, (WPARAM)TVGN_NEXT, (LPARAM)curr )));
2441 static LRESULT
2442 msi_seltree_menu( HWND hwnd, HTREEITEM hItem )
2444 struct msi_selection_tree_info *info;
2445 MSIFEATURE *feature;
2446 MSIPACKAGE *package;
2447 union {
2448 RECT rc;
2449 POINT pt[2];
2450 HTREEITEM hItem;
2451 } u;
2452 UINT r;
2454 info = GetPropW(hwnd, szButtonData);
2455 package = info->dialog->package;
2457 feature = msi_seltree_feature_from_item( hwnd, hItem );
2458 if (!feature)
2460 ERR("item %p feature was NULL\n", hItem);
2461 return 0;
2464 /* get the item's rectangle to put the menu just below it */
2465 u.hItem = hItem;
2466 SendMessageW( hwnd, TVM_GETITEMRECT, 0, (LPARAM) &u.rc );
2467 MapWindowPoints( hwnd, NULL, u.pt, 2 );
2469 r = msi_seltree_popup_menu( hwnd, u.rc.left, u.rc.top );
2471 switch (r)
2473 case USER_INSTALLSTATE_ALL:
2474 r = INSTALLSTATE_LOCAL;
2475 /* fall-through */
2476 case INSTALLSTATE_ADVERTISED:
2477 case INSTALLSTATE_ABSENT:
2479 HTREEITEM child;
2480 child = (HTREEITEM)SendMessageW( hwnd, TVM_GETNEXTITEM, (WPARAM)TVGN_CHILD, (LPARAM)hItem );
2481 if (child)
2482 msi_seltree_update_siblings_and_children_installstate( hwnd, child, package, r );
2484 /* fall-through */
2485 case INSTALLSTATE_LOCAL:
2486 msi_seltree_update_feature_installstate( hwnd, hItem, package, feature, r );
2487 break;
2490 return 0;
2493 static LRESULT WINAPI
2494 MSISelectionTree_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2496 struct msi_selection_tree_info *info;
2497 TVHITTESTINFO tvhti;
2498 HRESULT r;
2500 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
2502 info = GetPropW(hWnd, szButtonData);
2504 switch( msg )
2506 case WM_LBUTTONDOWN:
2507 tvhti.pt.x = (short)LOWORD( lParam );
2508 tvhti.pt.y = (short)HIWORD( lParam );
2509 tvhti.flags = 0;
2510 tvhti.hItem = 0;
2511 CallWindowProcW(info->oldproc, hWnd, TVM_HITTEST, 0, (LPARAM) &tvhti );
2512 if (tvhti.flags & TVHT_ONITEMSTATEICON)
2513 return msi_seltree_menu( hWnd, tvhti.hItem );
2514 break;
2516 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
2518 switch( msg )
2520 case WM_NCDESTROY:
2521 msi_free( info );
2522 RemovePropW( hWnd, szButtonData );
2523 break;
2525 return r;
2528 static void
2529 msi_seltree_add_child_features( MSIPACKAGE *package, HWND hwnd,
2530 LPCWSTR parent, HTREEITEM hParent )
2532 struct msi_selection_tree_info *info = GetPropW( hwnd, szButtonData );
2533 MSIFEATURE *feature;
2534 TVINSERTSTRUCTW tvis;
2535 HTREEITEM hitem, hfirst = NULL;
2537 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
2539 if ( parent && feature->Feature_Parent && strcmpW( parent, feature->Feature_Parent ))
2540 continue;
2541 else if ( parent && !feature->Feature_Parent )
2542 continue;
2543 else if ( !parent && feature->Feature_Parent )
2544 continue;
2546 if ( !feature->Title )
2547 continue;
2549 if ( !feature->Display )
2550 continue;
2552 memset( &tvis, 0, sizeof tvis );
2553 tvis.hParent = hParent;
2554 tvis.hInsertAfter = TVI_LAST;
2555 tvis.u.item.mask = TVIF_TEXT | TVIF_PARAM;
2556 tvis.u.item.pszText = feature->Title;
2557 tvis.u.item.lParam = (LPARAM) feature;
2559 hitem = (HTREEITEM) SendMessageW( hwnd, TVM_INSERTITEMW, 0, (LPARAM) &tvis );
2560 if (!hitem)
2561 continue;
2563 if (!hfirst)
2564 hfirst = hitem;
2566 msi_seltree_sync_item_state( hwnd, feature, hitem );
2567 msi_seltree_add_child_features( package, hwnd,
2568 feature->Feature, hitem );
2570 /* the node is expanded if Display is odd */
2571 if ( feature->Display % 2 != 0 )
2572 SendMessageW( hwnd, TVM_EXPAND, TVE_EXPAND, (LPARAM) hitem );
2575 /* select the first item */
2576 SendMessageW( hwnd, TVM_SELECTITEM, TVGN_CARET | TVGN_DROPHILITE, (LPARAM) hfirst );
2577 info->selected = hfirst;
2580 static void msi_seltree_create_imagelist( HWND hwnd )
2582 const int bm_width = 32, bm_height = 16, bm_count = 3;
2583 const int bm_resource = 0x1001;
2584 HIMAGELIST himl;
2585 int i;
2586 HBITMAP hbmp;
2588 himl = ImageList_Create( bm_width, bm_height, FALSE, 4, 0 );
2589 if (!himl)
2591 ERR("failed to create image list\n");
2592 return;
2595 for (i=0; i<bm_count; i++)
2597 hbmp = LoadBitmapW( msi_hInstance, MAKEINTRESOURCEW(i+bm_resource) );
2598 if (!hbmp)
2600 ERR("failed to load bitmap %d\n", i);
2601 break;
2605 * Add a dummy bitmap at offset zero because the treeview
2606 * can't use it as a state mask (zero means no user state).
2608 if (!i)
2609 ImageList_Add( himl, hbmp, NULL );
2611 ImageList_Add( himl, hbmp, NULL );
2614 SendMessageW( hwnd, TVM_SETIMAGELIST, TVSIL_STATE, (LPARAM)himl );
2617 static UINT msi_dialog_seltree_handler( msi_dialog *dialog,
2618 msi_control *control, WPARAM param )
2620 struct msi_selection_tree_info *info = GetPropW( control->hwnd, szButtonData );
2621 LPNMTREEVIEWW tv = (LPNMTREEVIEWW)param;
2622 MSIRECORD *row, *rec;
2623 MSIFOLDER *folder;
2624 MSIFEATURE *feature;
2625 LPCWSTR dir, title = NULL;
2626 UINT r = ERROR_SUCCESS;
2628 static const WCHAR select[] = {
2629 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2630 '`','F','e','a','t','u','r','e','`',' ','W','H','E','R','E',' ',
2631 '`','T','i','t','l','e','`',' ','=',' ','\'','%','s','\'',0
2634 if (tv->hdr.code != TVN_SELCHANGINGW)
2635 return ERROR_SUCCESS;
2637 info->selected = tv->itemNew.hItem;
2639 if (!(tv->itemNew.mask & TVIF_TEXT))
2641 feature = msi_seltree_feature_from_item( control->hwnd, tv->itemNew.hItem );
2642 if (feature)
2643 title = feature->Title;
2645 else
2646 title = tv->itemNew.pszText;
2648 row = MSI_QueryGetRecord( dialog->package->db, select, title );
2649 if (!row)
2650 return ERROR_FUNCTION_FAILED;
2652 rec = MSI_CreateRecord( 1 );
2654 MSI_RecordSetStringW( rec, 1, MSI_RecordGetString( row, 4 ) );
2655 msi_event_fire( dialog->package, szSelectionDescription, rec );
2657 dir = MSI_RecordGetString( row, 7 );
2658 if (dir)
2660 folder = msi_get_loaded_folder( dialog->package, dir );
2661 if (!folder)
2663 r = ERROR_FUNCTION_FAILED;
2664 goto done;
2666 MSI_RecordSetStringW( rec, 1, folder->ResolvedTarget );
2668 else
2669 MSI_RecordSetStringW( rec, 1, NULL );
2671 msi_event_fire( dialog->package, szSelectionPath, rec );
2673 done:
2674 msiobj_release(&row->hdr);
2675 msiobj_release(&rec->hdr);
2677 return r;
2680 static UINT msi_dialog_selection_tree( msi_dialog *dialog, MSIRECORD *rec )
2682 msi_control *control;
2683 LPCWSTR prop, control_name;
2684 MSIPACKAGE *package = dialog->package;
2685 DWORD style;
2686 struct msi_selection_tree_info *info;
2688 info = msi_alloc( sizeof *info );
2689 if (!info)
2690 return ERROR_FUNCTION_FAILED;
2692 /* create the treeview control */
2693 style = TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT;
2694 style |= WS_GROUP | WS_VSCROLL | WS_TABSTOP;
2695 control = msi_dialog_add_control( dialog, rec, WC_TREEVIEWW, style );
2696 if (!control)
2698 msi_free(info);
2699 return ERROR_FUNCTION_FAILED;
2702 control->handler = msi_dialog_seltree_handler;
2703 control_name = MSI_RecordGetString( rec, 2 );
2704 control->attributes = MSI_RecordGetInteger( rec, 8 );
2705 prop = MSI_RecordGetString( rec, 9 );
2706 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2708 /* subclass */
2709 info->dialog = dialog;
2710 info->hwnd = control->hwnd;
2711 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2712 (LONG_PTR)MSISelectionTree_WndProc );
2713 SetPropW( control->hwnd, szButtonData, info );
2715 event_subscribe( dialog, szSelectionPath, control_name, szProperty );
2717 /* initialize it */
2718 msi_seltree_create_imagelist( control->hwnd );
2719 msi_seltree_add_child_features( package, control->hwnd, NULL, NULL );
2721 return ERROR_SUCCESS;
2724 /******************** Group Box ***************************************/
2726 static UINT msi_dialog_group_box( msi_dialog *dialog, MSIRECORD *rec )
2728 msi_control *control;
2729 DWORD style;
2731 style = BS_GROUPBOX | WS_CHILD | WS_GROUP;
2732 control = msi_dialog_add_control( dialog, rec, WC_BUTTONW, style );
2733 if (!control)
2734 return ERROR_FUNCTION_FAILED;
2736 return ERROR_SUCCESS;
2739 /******************** List Box ***************************************/
2741 struct msi_listbox_info
2743 msi_dialog *dialog;
2744 HWND hwnd;
2745 WNDPROC oldproc;
2746 DWORD num_items;
2747 DWORD addpos_items;
2748 LPWSTR *items;
2751 static LRESULT WINAPI MSIListBox_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2753 struct msi_listbox_info *info;
2754 LRESULT r;
2755 DWORD j;
2757 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
2759 info = GetPropW( hWnd, szButtonData );
2760 if (!info)
2761 return 0;
2763 r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
2765 switch( msg )
2767 case WM_NCDESTROY:
2768 for (j = 0; j < info->num_items; j++)
2769 msi_free( info->items[j] );
2770 msi_free( info->items );
2771 msi_free( info );
2772 RemovePropW( hWnd, szButtonData );
2773 break;
2776 return r;
2779 static UINT msi_listbox_add_item( MSIRECORD *rec, LPVOID param )
2781 struct msi_listbox_info *info = param;
2782 LPCWSTR value, text;
2783 int pos;
2785 value = MSI_RecordGetString( rec, 3 );
2786 text = MSI_RecordGetString( rec, 4 );
2788 info->items[info->addpos_items] = strdupW( value );
2790 pos = SendMessageW( info->hwnd, LB_ADDSTRING, 0, (LPARAM)text );
2791 SendMessageW( info->hwnd, LB_SETITEMDATA, pos, (LPARAM)info->items[info->addpos_items] );
2792 info->addpos_items++;
2793 return ERROR_SUCCESS;
2796 static UINT msi_listbox_add_items( struct msi_listbox_info *info, LPCWSTR property )
2798 static const WCHAR query[] = {
2799 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2800 '`','L','i','s','t','B','o','x','`',' ','W','H','E','R','E',' ',
2801 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',' ',
2802 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','`',0};
2803 MSIQUERY *view;
2804 DWORD count;
2805 UINT r;
2807 r = MSI_OpenQuery( info->dialog->package->db, &view, query, property );
2808 if ( r != ERROR_SUCCESS )
2809 return r;
2811 /* just get the number of records */
2812 count = 0;
2813 r = MSI_IterateRecords( view, &count, NULL, NULL );
2814 if (r != ERROR_SUCCESS)
2816 msiobj_release( &view->hdr );
2817 return r;
2819 info->num_items = count;
2820 info->items = msi_alloc( sizeof(*info->items) * count );
2822 r = MSI_IterateRecords( view, NULL, msi_listbox_add_item, info );
2823 msiobj_release( &view->hdr );
2824 return r;
2827 static UINT msi_dialog_listbox_handler( msi_dialog *dialog,
2828 msi_control *control, WPARAM param )
2830 struct msi_listbox_info *info;
2831 int index;
2832 LPCWSTR value;
2834 if( HIWORD(param) != LBN_SELCHANGE )
2835 return ERROR_SUCCESS;
2837 info = GetPropW( control->hwnd, szButtonData );
2838 index = SendMessageW( control->hwnd, LB_GETCURSEL, 0, 0 );
2839 value = (LPCWSTR) SendMessageW( control->hwnd, LB_GETITEMDATA, index, 0 );
2841 msi_dialog_set_property( info->dialog->package, control->property, value );
2842 msi_dialog_evaluate_control_conditions( info->dialog );
2844 return ERROR_SUCCESS;
2847 static UINT msi_dialog_list_box( msi_dialog *dialog, MSIRECORD *rec )
2849 struct msi_listbox_info *info;
2850 msi_control *control;
2851 DWORD attributes, style;
2852 LPCWSTR prop;
2854 info = msi_alloc( sizeof *info );
2855 if (!info)
2856 return ERROR_FUNCTION_FAILED;
2858 style = WS_TABSTOP | WS_GROUP | WS_CHILD | LBS_NOTIFY | WS_VSCROLL | WS_BORDER;
2859 attributes = MSI_RecordGetInteger( rec, 8 );
2860 if (~attributes & msidbControlAttributesSorted)
2861 style |= LBS_SORT;
2863 control = msi_dialog_add_control( dialog, rec, WC_LISTBOXW, style );
2864 if (!control)
2866 msi_free(info);
2867 return ERROR_FUNCTION_FAILED;
2870 control->handler = msi_dialog_listbox_handler;
2872 prop = MSI_RecordGetString( rec, 9 );
2873 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2875 /* subclass */
2876 info->dialog = dialog;
2877 info->hwnd = control->hwnd;
2878 info->items = NULL;
2879 info->addpos_items = 0;
2880 info->oldproc = (WNDPROC)SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2881 (LONG_PTR)MSIListBox_WndProc );
2882 SetPropW( control->hwnd, szButtonData, info );
2884 if ( control->property )
2885 msi_listbox_add_items( info, control->property );
2887 return ERROR_SUCCESS;
2890 /******************** Directory Combo ***************************************/
2892 static void msi_dialog_update_directory_combo( msi_dialog *dialog, msi_control *control )
2894 LPWSTR prop, path;
2895 BOOL indirect;
2897 if (!control && !(control = msi_dialog_find_control_by_type( dialog, szDirectoryCombo )))
2898 return;
2900 indirect = control->attributes & msidbControlAttributesIndirect;
2901 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2902 path = msi_dialog_dup_property( dialog, prop, TRUE );
2904 PathStripPathW( path );
2905 PathRemoveBackslashW( path );
2907 SendMessageW( control->hwnd, CB_INSERTSTRING, 0, (LPARAM)path );
2908 SendMessageW( control->hwnd, CB_SETCURSEL, 0, 0 );
2910 msi_free( path );
2911 msi_free( prop );
2914 static UINT msi_dialog_directory_combo( msi_dialog *dialog, MSIRECORD *rec )
2916 msi_control *control;
2917 LPCWSTR prop;
2918 DWORD style;
2920 /* FIXME: use CBS_OWNERDRAWFIXED and add owner draw code */
2921 style = CBS_DROPDOWNLIST | CBS_HASSTRINGS | WS_CHILD |
2922 WS_GROUP | WS_TABSTOP | WS_VSCROLL;
2923 control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
2924 if (!control)
2925 return ERROR_FUNCTION_FAILED;
2927 control->attributes = MSI_RecordGetInteger( rec, 8 );
2928 prop = MSI_RecordGetString( rec, 9 );
2929 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2931 msi_dialog_update_directory_combo( dialog, control );
2933 return ERROR_SUCCESS;
2936 /******************** Directory List ***************************************/
2938 static void msi_dialog_update_directory_list( msi_dialog *dialog, msi_control *control )
2940 WCHAR dir_spec[MAX_PATH];
2941 WIN32_FIND_DATAW wfd;
2942 LPWSTR prop, path;
2943 BOOL indirect;
2944 LVITEMW item;
2945 HANDLE file;
2947 static const WCHAR asterisk[] = {'*',0};
2949 if (!control && !(control = msi_dialog_find_control_by_type( dialog, szDirectoryList )))
2950 return;
2952 /* clear the list-view */
2953 SendMessageW( control->hwnd, LVM_DELETEALLITEMS, 0, 0 );
2955 indirect = control->attributes & msidbControlAttributesIndirect;
2956 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2957 path = msi_dialog_dup_property( dialog, prop, TRUE );
2959 lstrcpyW( dir_spec, path );
2960 lstrcatW( dir_spec, asterisk );
2962 file = FindFirstFileW( dir_spec, &wfd );
2963 if ( file == INVALID_HANDLE_VALUE )
2964 return;
2968 if ( wfd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY )
2969 continue;
2971 if ( !strcmpW( wfd.cFileName, szDot ) || !strcmpW( wfd.cFileName, szDotDot ) )
2972 continue;
2974 item.mask = LVIF_TEXT;
2975 item.cchTextMax = MAX_PATH;
2976 item.iItem = 0;
2977 item.iSubItem = 0;
2978 item.pszText = wfd.cFileName;
2980 SendMessageW( control->hwnd, LVM_INSERTITEMW, 0, (LPARAM)&item );
2981 } while ( FindNextFileW( file, &wfd ) );
2983 msi_free( prop );
2984 msi_free( path );
2985 FindClose( file );
2988 static UINT msi_dialog_directorylist_up( msi_dialog *dialog )
2990 msi_control *control;
2991 LPWSTR prop, path, ptr;
2992 BOOL indirect;
2994 control = msi_dialog_find_control_by_type( dialog, szDirectoryList );
2995 indirect = control->attributes & msidbControlAttributesIndirect;
2996 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2997 path = msi_dialog_dup_property( dialog, prop, TRUE );
2999 /* strip off the last directory */
3000 ptr = PathFindFileNameW( path );
3001 if (ptr != path) *(ptr - 1) = '\0';
3002 PathAddBackslashW( path );
3004 msi_dialog_set_property( dialog->package, prop, path );
3006 msi_dialog_update_directory_list( dialog, NULL );
3007 msi_dialog_update_directory_combo( dialog, NULL );
3008 msi_dialog_update_pathedit( dialog, NULL );
3010 msi_free( path );
3011 msi_free( prop );
3013 return ERROR_SUCCESS;
3016 static UINT msi_dialog_dirlist_handler( msi_dialog *dialog,
3017 msi_control *control, WPARAM param )
3019 LPNMHDR nmhdr = (LPNMHDR)param;
3020 WCHAR new_path[MAX_PATH];
3021 WCHAR text[MAX_PATH];
3022 LPWSTR path, prop;
3023 BOOL indirect;
3024 LVITEMW item;
3025 int index;
3027 if (nmhdr->code != LVN_ITEMACTIVATE)
3028 return ERROR_SUCCESS;
3030 index = SendMessageW( control->hwnd, LVM_GETNEXTITEM, -1, LVNI_SELECTED );
3031 if ( index < 0 )
3033 ERR("No list-view item selected!\n");
3034 return ERROR_FUNCTION_FAILED;
3037 item.iSubItem = 0;
3038 item.pszText = text;
3039 item.cchTextMax = MAX_PATH;
3040 SendMessageW( control->hwnd, LVM_GETITEMTEXTW, index, (LPARAM)&item );
3042 indirect = control->attributes & msidbControlAttributesIndirect;
3043 prop = msi_dialog_dup_property( dialog, control->property, indirect );
3044 path = msi_dialog_dup_property( dialog, prop, TRUE );
3046 lstrcpyW( new_path, path );
3047 lstrcatW( new_path, text );
3048 lstrcatW( new_path, szBackSlash );
3050 msi_dialog_set_property( dialog->package, prop, new_path );
3052 msi_dialog_update_directory_list( dialog, NULL );
3053 msi_dialog_update_directory_combo( dialog, NULL );
3054 msi_dialog_update_pathedit( dialog, NULL );
3056 msi_free( prop );
3057 msi_free( path );
3058 return ERROR_SUCCESS;
3061 static UINT msi_dialog_directory_list( msi_dialog *dialog, MSIRECORD *rec )
3063 msi_control *control;
3064 LPCWSTR prop;
3065 DWORD style;
3067 style = LVS_LIST | WS_VSCROLL | LVS_SHAREIMAGELISTS |
3068 LVS_AUTOARRANGE | LVS_SINGLESEL | WS_BORDER |
3069 LVS_SORTASCENDING | WS_CHILD | WS_GROUP | WS_TABSTOP;
3070 control = msi_dialog_add_control( dialog, rec, WC_LISTVIEWW, style );
3071 if (!control)
3072 return ERROR_FUNCTION_FAILED;
3074 control->attributes = MSI_RecordGetInteger( rec, 8 );
3075 control->handler = msi_dialog_dirlist_handler;
3076 prop = MSI_RecordGetString( rec, 9 );
3077 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
3079 /* double click to activate an item in the list */
3080 SendMessageW( control->hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE,
3081 0, LVS_EX_TWOCLICKACTIVATE );
3083 msi_dialog_update_directory_list( dialog, control );
3085 return ERROR_SUCCESS;
3088 /******************** VolumeCost List ***************************************/
3090 static BOOL str_is_number( LPCWSTR str )
3092 int i;
3094 for (i = 0; i < lstrlenW( str ); i++)
3095 if (!isdigitW(str[i]))
3096 return FALSE;
3098 return TRUE;
3101 static const WCHAR column_keys[][80] =
3103 {'V','o','l','u','m','e','C','o','s','t','V','o','l','u','m','e',0},
3104 {'V','o','l','u','m','e','C','o','s','t','S','i','z','e',0},
3105 {'V','o','l','u','m','e','C','o','s','t','A','v','a','i','l','a','b','l','e',0},
3106 {'V','o','l','u','m','e','C','o','s','t','R','e','q','u','i','r','e','d',0},
3107 {'V','o','l','u','m','e','C','o','s','t','D','i','f','f','e','r','e','n','c','e',0}
3110 static void msi_dialog_vcl_add_columns( msi_dialog *dialog, msi_control *control, MSIRECORD *rec )
3112 LPCWSTR text = MSI_RecordGetString( rec, 10 );
3113 LPCWSTR begin = text, end;
3114 WCHAR *num;
3115 LVCOLUMNW lvc;
3116 DWORD count = 0;
3118 static const WCHAR negative[] = {'-',0};
3120 if (!text) return;
3122 while ((begin = strchrW( begin, '{' )) && count < 5)
3124 if (!(end = strchrW( begin, '}' )))
3125 return;
3127 num = msi_alloc( (end-begin+1)*sizeof(WCHAR) );
3128 if (!num)
3129 return;
3131 lstrcpynW( num, begin + 1, end - begin );
3132 begin += end - begin + 1;
3134 /* empty braces or '0' hides the column */
3135 if ( !num[0] || !strcmpW( num, szZero ) )
3137 count++;
3138 msi_free( num );
3139 continue;
3142 /* the width must be a positive number
3143 * if a width is invalid, all remaining columns are hidden
3145 if ( !strncmpW( num, negative, 1 ) || !str_is_number( num ) ) {
3146 msi_free( num );
3147 return;
3150 ZeroMemory( &lvc, sizeof(lvc) );
3151 lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
3152 lvc.cx = atolW( num );
3153 lvc.pszText = msi_dialog_get_uitext( dialog, column_keys[count] );
3155 SendMessageW( control->hwnd, LVM_INSERTCOLUMNW, count++, (LPARAM)&lvc );
3156 msi_free( lvc.pszText );
3157 msi_free( num );
3161 static LONGLONG msi_vcl_get_cost( msi_dialog *dialog )
3163 MSIFEATURE *feature;
3164 INT each_cost;
3165 LONGLONG total_cost = 0;
3167 LIST_FOR_EACH_ENTRY( feature, &dialog->package->features, MSIFEATURE, entry )
3169 if (ERROR_SUCCESS == (MSI_GetFeatureCost(dialog->package, feature,
3170 MSICOSTTREE_SELFONLY, INSTALLSTATE_LOCAL, &each_cost)))
3172 /* each_cost is in 512-byte units */
3173 total_cost += each_cost * 512;
3175 if (ERROR_SUCCESS == (MSI_GetFeatureCost(dialog->package, feature,
3176 MSICOSTTREE_SELFONLY, INSTALLSTATE_ABSENT, &each_cost)))
3178 /* each_cost is in 512-byte units */
3179 total_cost -= each_cost * 512;
3182 return total_cost;
3185 static void msi_dialog_vcl_add_drives( msi_dialog *dialog, msi_control *control )
3187 ULARGE_INTEGER total, free;
3188 LONGLONG difference, cost;
3189 WCHAR size_text[MAX_PATH];
3190 WCHAR cost_text[MAX_PATH];
3191 LPWSTR drives, ptr;
3192 LVITEMW lvitem;
3193 DWORD size;
3194 int i = 0;
3196 cost = msi_vcl_get_cost(dialog);
3197 StrFormatByteSizeW(cost, cost_text, MAX_PATH);
3199 size = GetLogicalDriveStringsW( 0, NULL );
3200 if ( !size ) return;
3202 drives = msi_alloc( (size + 1) * sizeof(WCHAR) );
3203 if ( !drives ) return;
3205 GetLogicalDriveStringsW( size, drives );
3207 ptr = drives;
3208 while (*ptr)
3210 lvitem.mask = LVIF_TEXT;
3211 lvitem.iItem = i;
3212 lvitem.iSubItem = 0;
3213 lvitem.pszText = ptr;
3214 lvitem.cchTextMax = lstrlenW(ptr) + 1;
3215 SendMessageW( control->hwnd, LVM_INSERTITEMW, 0, (LPARAM)&lvitem );
3217 GetDiskFreeSpaceExW(ptr, &free, &total, NULL);
3218 difference = free.QuadPart - cost;
3220 StrFormatByteSizeW(total.QuadPart, size_text, MAX_PATH);
3221 lvitem.iSubItem = 1;
3222 lvitem.pszText = size_text;
3223 lvitem.cchTextMax = lstrlenW(size_text) + 1;
3224 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
3226 StrFormatByteSizeW(free.QuadPart, size_text, MAX_PATH);
3227 lvitem.iSubItem = 2;
3228 lvitem.pszText = size_text;
3229 lvitem.cchTextMax = lstrlenW(size_text) + 1;
3230 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
3232 lvitem.iSubItem = 3;
3233 lvitem.pszText = cost_text;
3234 lvitem.cchTextMax = lstrlenW(cost_text) + 1;
3235 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
3237 StrFormatByteSizeW(difference, size_text, MAX_PATH);
3238 lvitem.iSubItem = 4;
3239 lvitem.pszText = size_text;
3240 lvitem.cchTextMax = lstrlenW(size_text) + 1;
3241 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
3243 ptr += lstrlenW(ptr) + 1;
3244 i++;
3247 msi_free( drives );
3250 static UINT msi_dialog_volumecost_list( msi_dialog *dialog, MSIRECORD *rec )
3252 msi_control *control;
3253 DWORD style;
3255 style = LVS_REPORT | WS_VSCROLL | WS_HSCROLL | LVS_SHAREIMAGELISTS |
3256 LVS_AUTOARRANGE | LVS_SINGLESEL | WS_BORDER |
3257 WS_CHILD | WS_TABSTOP | WS_GROUP;
3258 control = msi_dialog_add_control( dialog, rec, WC_LISTVIEWW, style );
3259 if (!control)
3260 return ERROR_FUNCTION_FAILED;
3262 msi_dialog_vcl_add_columns( dialog, control, rec );
3263 msi_dialog_vcl_add_drives( dialog, control );
3265 return ERROR_SUCCESS;
3268 /******************** VolumeSelect Combo ***************************************/
3270 static UINT msi_dialog_volsel_handler( msi_dialog *dialog,
3271 msi_control *control, WPARAM param )
3273 WCHAR text[MAX_PATH];
3274 LPWSTR prop;
3275 BOOL indirect;
3276 int index;
3278 if (HIWORD(param) != CBN_SELCHANGE)
3279 return ERROR_SUCCESS;
3281 index = SendMessageW( control->hwnd, CB_GETCURSEL, 0, 0 );
3282 if ( index == CB_ERR )
3284 ERR("No ComboBox item selected!\n");
3285 return ERROR_FUNCTION_FAILED;
3288 SendMessageW( control->hwnd, CB_GETLBTEXT, index, (LPARAM)text );
3290 indirect = control->attributes & msidbControlAttributesIndirect;
3291 prop = msi_dialog_dup_property( dialog, control->property, indirect );
3293 msi_dialog_set_property( dialog->package, prop, text );
3295 msi_free( prop );
3296 return ERROR_SUCCESS;
3299 static void msi_dialog_vsc_add_drives( msi_dialog *dialog, msi_control *control )
3301 LPWSTR drives, ptr;
3302 DWORD size;
3304 size = GetLogicalDriveStringsW( 0, NULL );
3305 if ( !size ) return;
3307 drives = msi_alloc( (size + 1) * sizeof(WCHAR) );
3308 if ( !drives ) return;
3310 GetLogicalDriveStringsW( size, drives );
3312 ptr = drives;
3313 while (*ptr)
3315 SendMessageW( control->hwnd, CB_ADDSTRING, 0, (LPARAM)ptr );
3316 ptr += lstrlenW(ptr) + 1;
3319 msi_free( drives );
3322 static UINT msi_dialog_volumeselect_combo( msi_dialog *dialog, MSIRECORD *rec )
3324 msi_control *control;
3325 LPCWSTR prop;
3326 DWORD style;
3328 /* FIXME: CBS_OWNERDRAWFIXED */
3329 style = WS_CHILD | WS_VISIBLE | WS_GROUP | WS_TABSTOP |
3330 CBS_DROPDOWNLIST | CBS_SORT | CBS_HASSTRINGS |
3331 WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
3332 control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
3333 if (!control)
3334 return ERROR_FUNCTION_FAILED;
3336 control->attributes = MSI_RecordGetInteger( rec, 8 );
3337 control->handler = msi_dialog_volsel_handler;
3338 prop = MSI_RecordGetString( rec, 9 );
3339 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
3341 msi_dialog_vsc_add_drives( dialog, control );
3343 return ERROR_SUCCESS;
3346 static UINT msi_dialog_hyperlink_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
3348 static const WCHAR hrefW[] = {'h','r','e','f'};
3349 static const WCHAR openW[] = {'o','p','e','n',0};
3350 int len, len_href = sizeof(hrefW) / sizeof(hrefW[0]);
3351 const WCHAR *p, *q;
3352 WCHAR quote = 0;
3353 LITEM item;
3355 item.mask = LIF_ITEMINDEX | LIF_URL;
3356 item.iLink = 0;
3357 item.szUrl[0] = 0;
3359 SendMessageW( control->hwnd, LM_GETITEM, 0, (LPARAM)&item );
3361 p = item.szUrl;
3362 while (*p && *p != '<') p++;
3363 if (!*p++) return ERROR_SUCCESS;
3364 if (toupperW( *p++ ) != 'A' || !isspaceW( *p++ )) return ERROR_SUCCESS;
3365 while (*p && isspaceW( *p )) p++;
3367 len = strlenW( p );
3368 if (len > len_href && !memicmpW( p, hrefW, len_href ))
3370 p += len_href;
3371 while (*p && isspaceW( *p )) p++;
3372 if (!*p || *p++ != '=') return ERROR_SUCCESS;
3373 while (*p && isspaceW( *p )) p++;
3375 if (*p == '\"' || *p == '\'') quote = *p++;
3376 q = p;
3377 if (quote)
3379 while (*q && *q != quote) q++;
3380 if (*q != quote) return ERROR_SUCCESS;
3382 else
3384 while (*q && *q != '>' && !isspaceW( *q )) q++;
3385 if (!*q) return ERROR_SUCCESS;
3387 item.szUrl[q - item.szUrl] = 0;
3388 ShellExecuteW( NULL, openW, p, NULL, NULL, SW_SHOWNORMAL );
3390 return ERROR_SUCCESS;
3393 static UINT msi_dialog_hyperlink( msi_dialog *dialog, MSIRECORD *rec )
3395 msi_control *control;
3396 DWORD style = WS_CHILD | WS_TABSTOP | WS_GROUP;
3397 const WCHAR *text = MSI_RecordGetString( rec, 10 );
3398 int len = strlenW( text );
3399 LITEM item;
3401 control = msi_dialog_add_control( dialog, rec, WC_LINK, style );
3402 if (!control)
3403 return ERROR_FUNCTION_FAILED;
3405 control->attributes = MSI_RecordGetInteger( rec, 8 );
3406 control->handler = msi_dialog_hyperlink_handler;
3408 item.mask = LIF_ITEMINDEX | LIF_STATE | LIF_URL;
3409 item.iLink = 0;
3410 item.state = LIS_ENABLED;
3411 item.stateMask = LIS_ENABLED;
3412 if (len < L_MAX_URL_LENGTH) strcpyW( item.szUrl, text );
3413 else item.szUrl[0] = 0;
3415 SendMessageW( control->hwnd, LM_SETITEM, 0, (LPARAM)&item );
3417 return ERROR_SUCCESS;
3420 static const struct control_handler msi_dialog_handler[] =
3422 { szText, msi_dialog_text_control },
3423 { szPushButton, msi_dialog_button_control },
3424 { szLine, msi_dialog_line_control },
3425 { szBitmap, msi_dialog_bitmap_control },
3426 { szCheckBox, msi_dialog_checkbox_control },
3427 { szScrollableText, msi_dialog_scrolltext_control },
3428 { szComboBox, msi_dialog_combo_control },
3429 { szEdit, msi_dialog_edit_control },
3430 { szMaskedEdit, msi_dialog_maskedit_control },
3431 { szPathEdit, msi_dialog_pathedit_control },
3432 { szProgressBar, msi_dialog_progress_bar },
3433 { szRadioButtonGroup, msi_dialog_radiogroup_control },
3434 { szIcon, msi_dialog_icon_control },
3435 { szSelectionTree, msi_dialog_selection_tree },
3436 { szGroupBox, msi_dialog_group_box },
3437 { szListBox, msi_dialog_list_box },
3438 { szDirectoryCombo, msi_dialog_directory_combo },
3439 { szDirectoryList, msi_dialog_directory_list },
3440 { szVolumeCostList, msi_dialog_volumecost_list },
3441 { szVolumeSelectCombo, msi_dialog_volumeselect_combo },
3442 { szHyperLink, msi_dialog_hyperlink }
3445 #define NUM_CONTROL_TYPES (sizeof msi_dialog_handler/sizeof msi_dialog_handler[0])
3447 static UINT msi_dialog_create_controls( MSIRECORD *rec, LPVOID param )
3449 msi_dialog *dialog = param;
3450 LPCWSTR control_type;
3451 UINT i;
3453 /* find and call the function that can create this type of control */
3454 control_type = MSI_RecordGetString( rec, 3 );
3455 for( i=0; i<NUM_CONTROL_TYPES; i++ )
3456 if (!strcmpiW( msi_dialog_handler[i].control_type, control_type ))
3457 break;
3458 if( i != NUM_CONTROL_TYPES )
3459 msi_dialog_handler[i].func( dialog, rec );
3460 else
3461 ERR("no handler for element type %s\n", debugstr_w(control_type));
3463 return ERROR_SUCCESS;
3466 static UINT msi_dialog_fill_controls( msi_dialog *dialog )
3468 static const WCHAR query[] = {
3469 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
3470 'C','o','n','t','r','o','l',' ','W','H','E','R','E',' ',
3471 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
3472 UINT r;
3473 MSIQUERY *view;
3474 MSIPACKAGE *package = dialog->package;
3476 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
3478 /* query the Control table for all the elements of the control */
3479 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
3480 if( r != ERROR_SUCCESS )
3482 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
3483 return ERROR_INVALID_PARAMETER;
3486 r = MSI_IterateRecords( view, 0, msi_dialog_create_controls, dialog );
3487 msiobj_release( &view->hdr );
3488 return r;
3491 static UINT msi_dialog_reset( msi_dialog *dialog )
3493 /* FIXME: should restore the original values of any properties we changed */
3494 return msi_dialog_evaluate_control_conditions( dialog );
3497 /* figure out the height of 10 point MS Sans Serif */
3498 static INT msi_dialog_get_sans_serif_height( HWND hwnd )
3500 static const WCHAR szSansSerif[] = {
3501 'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0 };
3502 LOGFONTW lf;
3503 TEXTMETRICW tm;
3504 BOOL r;
3505 LONG height = 0;
3506 HFONT hFont, hOldFont;
3507 HDC hdc;
3509 hdc = GetDC( hwnd );
3510 if (hdc)
3512 memset( &lf, 0, sizeof lf );
3513 lf.lfHeight = MulDiv(12, GetDeviceCaps(hdc, LOGPIXELSY), 72);
3514 strcpyW( lf.lfFaceName, szSansSerif );
3515 hFont = CreateFontIndirectW(&lf);
3516 if (hFont)
3518 hOldFont = SelectObject( hdc, hFont );
3519 r = GetTextMetricsW( hdc, &tm );
3520 if (r)
3521 height = tm.tmHeight;
3522 SelectObject( hdc, hOldFont );
3523 DeleteObject( hFont );
3525 ReleaseDC( hwnd, hdc );
3527 return height;
3530 /* fetch the associated record from the Dialog table */
3531 static MSIRECORD *msi_get_dialog_record( msi_dialog *dialog )
3533 static const WCHAR query[] = {
3534 'S','E','L','E','C','T',' ','*',' ',
3535 'F','R','O','M',' ','D','i','a','l','o','g',' ',
3536 'W','H','E','R','E',' ',
3537 '`','D','i','a','l','o','g','`',' ','=',' ','\'','%','s','\'',0};
3538 MSIPACKAGE *package = dialog->package;
3539 MSIRECORD *rec = NULL;
3541 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
3543 rec = MSI_QueryGetRecord( package->db, query, dialog->name );
3544 if( !rec )
3545 WARN("query failed for dialog %s\n", debugstr_w(dialog->name));
3547 return rec;
3550 static void msi_dialog_adjust_dialog_pos( msi_dialog *dialog, MSIRECORD *rec, LPRECT pos )
3552 static const WCHAR szScreenX[] = {'S','c','r','e','e','n','X',0};
3553 static const WCHAR szScreenY[] = {'S','c','r','e','e','n','Y',0};
3555 UINT xres, yres;
3556 POINT center;
3557 SIZE sz;
3558 LONG style;
3560 center.x = MSI_RecordGetInteger( rec, 2 );
3561 center.y = MSI_RecordGetInteger( rec, 3 );
3563 sz.cx = MSI_RecordGetInteger( rec, 4 );
3564 sz.cy = MSI_RecordGetInteger( rec, 5 );
3566 sz.cx = msi_dialog_scale_unit( dialog, sz.cx );
3567 sz.cy = msi_dialog_scale_unit( dialog, sz.cy );
3569 xres = msi_get_property_int( dialog->package->db, szScreenX, 0 );
3570 yres = msi_get_property_int( dialog->package->db, szScreenY, 0 );
3572 center.x = MulDiv( center.x, xres, 100 );
3573 center.y = MulDiv( center.y, yres, 100 );
3575 /* turn the client pos into the window rectangle */
3576 if (dialog->package->center_x && dialog->package->center_y)
3578 pos->left = dialog->package->center_x - sz.cx / 2.0;
3579 pos->right = pos->left + sz.cx;
3580 pos->top = dialog->package->center_y - sz.cy / 2.0;
3581 pos->bottom = pos->top + sz.cy;
3583 else
3585 pos->left = center.x - sz.cx/2;
3586 pos->right = pos->left + sz.cx;
3587 pos->top = center.y - sz.cy/2;
3588 pos->bottom = pos->top + sz.cy;
3590 /* save the center */
3591 dialog->package->center_x = center.x;
3592 dialog->package->center_y = center.y;
3595 dialog->size.cx = sz.cx;
3596 dialog->size.cy = sz.cy;
3598 TRACE("%u %u %u %u\n", pos->left, pos->top, pos->right, pos->bottom);
3600 style = GetWindowLongPtrW( dialog->hwnd, GWL_STYLE );
3601 AdjustWindowRect( pos, style, FALSE );
3604 static void msi_dialog_set_tab_order( msi_dialog *dialog, LPCWSTR first )
3606 struct list tab_chain;
3607 msi_control *control;
3608 HWND prev = HWND_TOP;
3610 list_init( &tab_chain );
3611 if (!(control = msi_dialog_find_control( dialog, first ))) return;
3613 dialog->hWndFocus = control->hwnd;
3614 while (control)
3616 list_remove( &control->entry );
3617 list_add_tail( &tab_chain, &control->entry );
3618 if (!control->tabnext) break;
3619 control = msi_dialog_find_control( dialog, control->tabnext );
3622 LIST_FOR_EACH_ENTRY( control, &tab_chain, msi_control, entry )
3624 SetWindowPos( control->hwnd, prev, 0, 0, 0, 0,
3625 SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOREDRAW |
3626 SWP_NOREPOSITION | SWP_NOSENDCHANGING | SWP_NOSIZE );
3627 prev = control->hwnd;
3630 /* put them back on the main list */
3631 list_move_head( &dialog->controls, &tab_chain );
3634 static LRESULT msi_dialog_oncreate( HWND hwnd, LPCREATESTRUCTW cs )
3636 static const WCHAR df[] = {
3637 'D','e','f','a','u','l','t','U','I','F','o','n','t',0 };
3638 static const WCHAR dfv[] = {
3639 'M','S',' ','S','h','e','l','l',' ','D','l','g',0 };
3640 msi_dialog *dialog = cs->lpCreateParams;
3641 MSIRECORD *rec = NULL;
3642 LPWSTR title = NULL;
3643 RECT pos;
3645 TRACE("%p %p\n", dialog, dialog->package);
3647 dialog->hwnd = hwnd;
3648 SetWindowLongPtrW( hwnd, GWLP_USERDATA, (LONG_PTR) dialog );
3650 rec = msi_get_dialog_record( dialog );
3651 if( !rec )
3653 TRACE("No record found for dialog %s\n", debugstr_w(dialog->name));
3654 return -1;
3657 dialog->scale = msi_dialog_get_sans_serif_height(dialog->hwnd);
3659 msi_dialog_adjust_dialog_pos( dialog, rec, &pos );
3661 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
3663 dialog->default_font = msi_dup_property( dialog->package->db, df );
3664 if (!dialog->default_font)
3666 dialog->default_font = strdupW(dfv);
3667 if (!dialog->default_font)
3669 msiobj_release( &rec->hdr );
3670 return -1;
3674 title = msi_get_deformatted_field( dialog->package, rec, 7 );
3675 SetWindowTextW( hwnd, title );
3676 msi_free( title );
3678 SetWindowPos( hwnd, 0, pos.left, pos.top,
3679 pos.right - pos.left, pos.bottom - pos.top,
3680 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW );
3682 msi_dialog_build_font_list( dialog );
3683 msi_dialog_fill_controls( dialog );
3684 msi_dialog_evaluate_control_conditions( dialog );
3685 msi_dialog_set_tab_order( dialog, MSI_RecordGetString( rec, 8 ) );
3686 msiobj_release( &rec->hdr );
3688 return 0;
3691 static LRESULT msi_dialog_oncommand( msi_dialog *dialog, WPARAM param, HWND hwnd )
3693 msi_control *control = NULL;
3695 TRACE("%p %p %08lx\n", dialog, hwnd, param);
3697 switch (param)
3699 case 1: /* enter */
3700 control = msi_dialog_find_control( dialog, dialog->control_default );
3701 break;
3702 case 2: /* escape */
3703 control = msi_dialog_find_control( dialog, dialog->control_cancel );
3704 break;
3705 default:
3706 control = msi_dialog_find_control_by_hwnd( dialog, hwnd );
3709 if( control )
3711 if( control->handler )
3713 control->handler( dialog, control, param );
3714 msi_dialog_evaluate_control_conditions( dialog );
3718 return 0;
3721 static LRESULT msi_dialog_onnotify( msi_dialog *dialog, LPARAM param )
3723 LPNMHDR nmhdr = (LPNMHDR) param;
3724 msi_control *control = msi_dialog_find_control_by_hwnd( dialog, nmhdr->hwndFrom );
3726 TRACE("%p %p\n", dialog, nmhdr->hwndFrom);
3728 if ( control && control->handler )
3729 control->handler( dialog, control, param );
3731 return 0;
3734 static void dialog_setfocus( msi_dialog *dialog )
3736 HWND hwnd = dialog->hWndFocus;
3738 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, TRUE);
3739 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, FALSE);
3740 SetFocus( hwnd );
3741 dialog->hWndFocus = hwnd;
3744 static LRESULT WINAPI MSIDialog_WndProc( HWND hwnd, UINT msg,
3745 WPARAM wParam, LPARAM lParam )
3747 msi_dialog *dialog = (LPVOID) GetWindowLongPtrW( hwnd, GWLP_USERDATA );
3749 TRACE("0x%04x\n", msg);
3751 switch (msg)
3753 case WM_MOVE:
3754 dialog->package->center_x = LOWORD(lParam) + dialog->size.cx / 2.0;
3755 dialog->package->center_y = HIWORD(lParam) + dialog->size.cy / 2.0;
3756 break;
3758 case WM_CREATE:
3759 return msi_dialog_oncreate( hwnd, (LPCREATESTRUCTW)lParam );
3761 case WM_COMMAND:
3762 return msi_dialog_oncommand( dialog, wParam, (HWND)lParam );
3764 case WM_ACTIVATE:
3765 if( LOWORD(wParam) == WA_INACTIVE )
3766 dialog->hWndFocus = GetFocus();
3767 else
3768 dialog_setfocus( dialog );
3769 return 0;
3771 case WM_SETFOCUS:
3772 dialog_setfocus( dialog );
3773 return 0;
3775 /* bounce back to our subclassed static control */
3776 case WM_CTLCOLORSTATIC:
3777 return SendMessageW( (HWND) lParam, WM_CTLCOLORSTATIC, wParam, lParam );
3779 case WM_DESTROY:
3780 dialog->hwnd = NULL;
3781 return 0;
3782 case WM_NOTIFY:
3783 return msi_dialog_onnotify( dialog, lParam );
3785 return DefWindowProcW(hwnd, msg, wParam, lParam);
3788 static void process_pending_messages( HWND hdlg )
3790 MSG msg;
3792 while (PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
3794 if (hdlg && IsDialogMessageW( hdlg, &msg )) continue;
3795 TranslateMessage( &msg );
3796 DispatchMessageW( &msg );
3800 static UINT dialog_run_message_loop( msi_dialog *dialog )
3802 DWORD style;
3803 HWND hwnd;
3805 if( uiThreadId != GetCurrentThreadId() )
3806 return SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_CREATE, 0, (LPARAM) dialog );
3808 /* create the dialog window, don't show it yet */
3809 style = WS_OVERLAPPED;
3810 if( dialog->attributes & msidbDialogAttributesVisible )
3811 style |= WS_VISIBLE;
3813 hwnd = CreateWindowW( szMsiDialogClass, dialog->name, style,
3814 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
3815 NULL, NULL, NULL, dialog );
3816 if( !hwnd )
3818 ERR("Failed to create dialog %s\n", debugstr_w( dialog->name ));
3819 return ERROR_FUNCTION_FAILED;
3822 ShowWindow( hwnd, SW_SHOW );
3823 /* UpdateWindow( hwnd ); - and causes the transparent static controls not to paint */
3825 if( dialog->attributes & msidbDialogAttributesModal )
3827 while( !dialog->finished )
3829 MsgWaitForMultipleObjects( 0, NULL, 0, INFINITE, QS_ALLINPUT );
3830 process_pending_messages( dialog->hwnd );
3833 else
3834 return ERROR_IO_PENDING;
3836 return ERROR_SUCCESS;
3839 static LRESULT WINAPI MSIHiddenWindowProc( HWND hwnd, UINT msg,
3840 WPARAM wParam, LPARAM lParam )
3842 msi_dialog *dialog = (msi_dialog*) lParam;
3844 TRACE("%d %p\n", msg, dialog);
3846 switch (msg)
3848 case WM_MSI_DIALOG_CREATE:
3849 return dialog_run_message_loop( dialog );
3850 case WM_MSI_DIALOG_DESTROY:
3851 msi_dialog_destroy( dialog );
3852 return 0;
3854 return DefWindowProcW( hwnd, msg, wParam, lParam );
3857 static BOOL dialog_register_class( void )
3859 WNDCLASSW cls;
3861 ZeroMemory( &cls, sizeof cls );
3862 cls.lpfnWndProc = MSIDialog_WndProc;
3863 cls.hInstance = NULL;
3864 cls.hIcon = LoadIconW(0, (LPWSTR)IDI_APPLICATION);
3865 cls.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
3866 cls.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
3867 cls.lpszMenuName = NULL;
3868 cls.lpszClassName = szMsiDialogClass;
3870 if( !RegisterClassW( &cls ) )
3871 return FALSE;
3873 cls.lpfnWndProc = MSIHiddenWindowProc;
3874 cls.lpszClassName = szMsiHiddenWindow;
3876 if( !RegisterClassW( &cls ) )
3877 return FALSE;
3879 uiThreadId = GetCurrentThreadId();
3881 hMsiHiddenWindow = CreateWindowW( szMsiHiddenWindow, NULL, WS_OVERLAPPED,
3882 0, 0, 100, 100, NULL, NULL, NULL, NULL );
3883 if( !hMsiHiddenWindow )
3884 return FALSE;
3886 return TRUE;
3889 static msi_dialog *dialog_create( MSIPACKAGE *package, const WCHAR *name, msi_dialog *parent,
3890 control_event_handler event_handler )
3892 MSIRECORD *rec = NULL;
3893 msi_dialog *dialog;
3895 TRACE("%s\n", debugstr_w(name));
3897 if (!hMsiHiddenWindow) dialog_register_class();
3899 /* allocate the structure for the dialog to use */
3900 dialog = msi_alloc_zero( FIELD_OFFSET( msi_dialog, name[strlenW( name ) + 1] ));
3901 if( !dialog )
3902 return NULL;
3903 strcpyW( dialog->name, name );
3904 dialog->parent = parent;
3905 msiobj_addref( &package->hdr );
3906 dialog->package = package;
3907 dialog->event_handler = event_handler;
3908 dialog->finished = 0;
3909 list_init( &dialog->controls );
3910 list_init( &dialog->fonts );
3912 /* verify that the dialog exists */
3913 rec = msi_get_dialog_record( dialog );
3914 if( !rec )
3916 msiobj_release( &package->hdr );
3917 msi_free( dialog );
3918 return NULL;
3920 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
3921 dialog->control_default = strdupW( MSI_RecordGetString( rec, 9 ) );
3922 dialog->control_cancel = strdupW( MSI_RecordGetString( rec, 10 ) );
3923 msiobj_release( &rec->hdr );
3925 return dialog;
3928 static void msi_dialog_end_dialog( msi_dialog *dialog )
3930 TRACE("%p\n", dialog);
3931 dialog->finished = 1;
3932 PostMessageW(dialog->hwnd, WM_NULL, 0, 0);
3935 void msi_dialog_check_messages( HANDLE handle )
3937 DWORD r;
3939 /* in threads other than the UI thread, block */
3940 if( uiThreadId != GetCurrentThreadId() )
3942 if (!handle) return;
3943 while (MsgWaitForMultipleObjectsEx( 1, &handle, INFINITE, QS_ALLINPUT, 0 ) == WAIT_OBJECT_0 + 1)
3945 MSG msg;
3946 while (PeekMessageW( &msg, NULL, 0, 0, PM_REMOVE ))
3948 TranslateMessage( &msg );
3949 DispatchMessageW( &msg );
3952 return;
3955 /* there are two choices for the UI thread */
3956 while (1)
3958 process_pending_messages( NULL );
3960 if( !handle )
3961 break;
3964 * block here until somebody creates a new dialog or
3965 * the handle we're waiting on becomes ready
3967 r = MsgWaitForMultipleObjects( 1, &handle, 0, INFINITE, QS_ALLINPUT );
3968 if( r == WAIT_OBJECT_0 )
3969 break;
3973 static void dialog_do_preview( msi_dialog *dialog )
3975 TRACE("\n");
3976 dialog->attributes |= msidbDialogAttributesVisible;
3977 dialog->attributes &= ~msidbDialogAttributesModal;
3978 dialog_run_message_loop( dialog );
3981 static void free_subscriber( struct subscriber *sub )
3983 msi_free( sub->event );
3984 msi_free( sub->control );
3985 msi_free( sub->attribute );
3986 msi_free( sub );
3989 static void event_cleanup_subscriptions( MSIPACKAGE *package, const WCHAR *dialog )
3991 struct list *item, *next;
3993 LIST_FOR_EACH_SAFE( item, next, &package->subscriptions )
3995 struct subscriber *sub = LIST_ENTRY( item, struct subscriber, entry );
3997 if (strcmpW( sub->dialog->name, dialog )) continue;
3998 list_remove( &sub->entry );
3999 free_subscriber( sub );
4003 void msi_dialog_destroy( msi_dialog *dialog )
4005 msi_font *font, *next;
4007 if( uiThreadId != GetCurrentThreadId() )
4009 SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_DESTROY, 0, (LPARAM) dialog );
4010 return;
4013 if( dialog->hwnd )
4014 ShowWindow( dialog->hwnd, SW_HIDE );
4016 if( dialog->hwnd )
4017 DestroyWindow( dialog->hwnd );
4019 /* unsubscribe events */
4020 event_cleanup_subscriptions( dialog->package, dialog->name );
4022 /* destroy the list of controls */
4023 while( !list_empty( &dialog->controls ) )
4025 msi_control *t;
4027 t = LIST_ENTRY( list_head( &dialog->controls ),
4028 msi_control, entry );
4029 msi_destroy_control( t );
4032 /* destroy the list of fonts */
4033 LIST_FOR_EACH_ENTRY_SAFE( font, next, &dialog->fonts, msi_font, entry )
4035 list_remove( &font->entry );
4036 DeleteObject( font->hfont );
4037 msi_free( font );
4039 msi_free( dialog->default_font );
4041 msi_free( dialog->control_default );
4042 msi_free( dialog->control_cancel );
4043 msiobj_release( &dialog->package->hdr );
4044 dialog->package = NULL;
4045 msi_free( dialog );
4048 void msi_dialog_unregister_class( void )
4050 DestroyWindow( hMsiHiddenWindow );
4051 hMsiHiddenWindow = NULL;
4052 UnregisterClassW( szMsiDialogClass, NULL );
4053 UnregisterClassW( szMsiHiddenWindow, NULL );
4054 uiThreadId = 0;
4057 void msi_event_cleanup_all_subscriptions( MSIPACKAGE *package )
4059 struct list *item, *next;
4061 LIST_FOR_EACH_SAFE( item, next, &package->subscriptions )
4063 struct subscriber *sub = LIST_ENTRY( item, struct subscriber, entry );
4064 list_remove( &sub->entry );
4065 free_subscriber( sub );
4069 static UINT error_dialog_handler( msi_dialog *dialog, const WCHAR *event, const WCHAR *argument )
4071 static const WCHAR end_dialog[] = {'E','n','d','D','i','a','l','o','g',0};
4072 static const WCHAR error_abort[] = {'E','r','r','o','r','A','b','o','r','t',0};
4073 static const WCHAR error_cancel[] = {'E','r','r','o','r','C','a','n','c','e','l',0};
4074 static const WCHAR error_no[] = {'E','r','r','o','r','N','o',0};
4075 static const WCHAR result_prop[] = {
4076 'M','S','I','E','r','r','o','r','D','i','a','l','o','g','R','e','s','u','l','t',0
4079 if ( strcmpW( event, end_dialog ) )
4080 return ERROR_SUCCESS;
4082 if ( !strcmpW( argument, error_abort ) || !strcmpW( argument, error_cancel ) ||
4083 !strcmpW( argument, error_no ) )
4085 msi_set_property( dialog->package->db, result_prop, error_abort, -1 );
4088 msi_event_cleanup_all_subscriptions( dialog->package );
4089 msi_dialog_end_dialog( dialog );
4091 return ERROR_SUCCESS;
4094 static UINT msi_error_dialog_set_error( MSIPACKAGE *package, LPWSTR error_dialog, LPWSTR error )
4096 MSIRECORD * row;
4098 static const WCHAR update[] =
4099 {'U','P','D','A','T','E',' ','`','C','o','n','t','r','o','l','`',' ',
4100 'S','E','T',' ','`','T','e','x','t','`',' ','=',' ','\'','%','s','\'',' ',
4101 'W','H','E','R','E', ' ','`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
4102 'A','N','D',' ','`','C','o','n','t','r','o','l','`',' ','=',' ',
4103 '\'','E','r','r','o','r','T','e','x','t','\'',0};
4105 row = MSI_QueryGetRecord( package->db, update, error, error_dialog );
4106 if (!row)
4107 return ERROR_FUNCTION_FAILED;
4109 msiobj_release(&row->hdr);
4110 return ERROR_SUCCESS;
4113 UINT msi_spawn_error_dialog( MSIPACKAGE *package, LPWSTR error_dialog, LPWSTR error )
4115 msi_dialog *dialog;
4116 WCHAR result[MAX_PATH];
4117 UINT r = ERROR_SUCCESS;
4118 DWORD size = MAX_PATH;
4119 int res;
4121 static const WCHAR pn_prop[] = {'P','r','o','d','u','c','t','N','a','m','e',0};
4122 static const WCHAR title_fmt[] = {'%','s',' ','W','a','r','n','i','n','g',0};
4123 static const WCHAR error_abort[] = {'E','r','r','o','r','A','b','o','r','t',0};
4124 static const WCHAR result_prop[] = {
4125 'M','S','I','E','r','r','o','r','D','i','a','l','o','g','R','e','s','u','l','t',0
4128 if ((package->ui_level & INSTALLUILEVEL_MASK) == INSTALLUILEVEL_NONE) return ERROR_SUCCESS;
4130 if ( !error_dialog )
4132 LPWSTR product_name = msi_dup_property( package->db, pn_prop );
4133 WCHAR title[MAX_PATH];
4135 sprintfW( title, title_fmt, product_name );
4136 res = MessageBoxW( NULL, error, title, MB_OKCANCEL | MB_ICONWARNING );
4138 msi_free( product_name );
4140 if ( res == IDOK )
4141 return ERROR_SUCCESS;
4142 else
4143 return ERROR_FUNCTION_FAILED;
4146 r = msi_error_dialog_set_error( package, error_dialog, error );
4147 if ( r != ERROR_SUCCESS )
4148 return r;
4150 dialog = dialog_create( package, error_dialog, package->dialog, error_dialog_handler );
4151 if ( !dialog )
4152 return ERROR_FUNCTION_FAILED;
4154 dialog->finished = FALSE;
4155 r = dialog_run_message_loop( dialog );
4156 if ( r != ERROR_SUCCESS )
4157 goto done;
4159 r = msi_get_property( package->db, result_prop, result, &size );
4160 if ( r != ERROR_SUCCESS)
4161 r = ERROR_SUCCESS;
4163 if ( !strcmpW( result, error_abort ) )
4164 r = ERROR_FUNCTION_FAILED;
4166 done:
4167 msi_dialog_destroy( dialog );
4169 return r;
4172 static void MSI_ClosePreview( MSIOBJECTHDR *arg )
4174 MSIPREVIEW *preview = (MSIPREVIEW *)arg;
4175 msiobj_release( &preview->package->hdr );
4178 static MSIPREVIEW *MSI_EnableUIPreview( MSIDATABASE *db )
4180 MSIPREVIEW *preview = NULL;
4181 MSIPACKAGE *package;
4183 package = MSI_CreatePackage( db, NULL );
4184 if (package)
4186 preview = alloc_msiobject( MSIHANDLETYPE_PREVIEW, sizeof(MSIPREVIEW), MSI_ClosePreview );
4187 if (preview)
4189 preview->package = package;
4190 msiobj_addref( &package->hdr );
4192 msiobj_release( &package->hdr );
4194 return preview;
4197 UINT WINAPI MsiEnableUIPreview( MSIHANDLE hdb, MSIHANDLE *phPreview )
4199 MSIDATABASE *db;
4200 MSIPREVIEW *preview;
4201 UINT r = ERROR_FUNCTION_FAILED;
4203 TRACE("%d %p\n", hdb, phPreview);
4205 db = msihandle2msiinfo( hdb, MSIHANDLETYPE_DATABASE );
4206 if (!db)
4208 IWineMsiRemoteDatabase *remote_database;
4210 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( hdb );
4211 if (!remote_database)
4212 return ERROR_INVALID_HANDLE;
4214 *phPreview = 0;
4216 IWineMsiRemoteDatabase_Release( remote_database );
4217 WARN("MsiEnableUIPreview not allowed during a custom action!\n");
4219 return ERROR_FUNCTION_FAILED;
4221 preview = MSI_EnableUIPreview( db );
4222 if (preview)
4224 *phPreview = alloc_msihandle( &preview->hdr );
4225 msiobj_release( &preview->hdr );
4226 r = ERROR_SUCCESS;
4227 if (!*phPreview)
4228 r = ERROR_NOT_ENOUGH_MEMORY;
4230 msiobj_release( &db->hdr );
4231 return r;
4234 static UINT preview_event_handler( msi_dialog *dialog, const WCHAR *event, const WCHAR *argument )
4236 MESSAGE("Preview dialog event '%s' (arg='%s')\n", debugstr_w(event), debugstr_w(argument));
4237 return ERROR_SUCCESS;
4240 static UINT MSI_PreviewDialogW( MSIPREVIEW *preview, LPCWSTR szDialogName )
4242 msi_dialog *dialog = NULL;
4243 UINT r = ERROR_SUCCESS;
4245 if (preview->dialog)
4246 msi_dialog_destroy( preview->dialog );
4248 /* an empty name means we should just destroy the current preview dialog */
4249 if (szDialogName)
4251 dialog = dialog_create( preview->package, szDialogName, NULL, preview_event_handler );
4252 if (dialog)
4253 dialog_do_preview( dialog );
4254 else
4255 r = ERROR_FUNCTION_FAILED;
4257 preview->dialog = dialog;
4258 return r;
4261 UINT WINAPI MsiPreviewDialogW( MSIHANDLE hPreview, LPCWSTR szDialogName )
4263 MSIPREVIEW *preview;
4264 UINT r;
4266 TRACE("%d %s\n", hPreview, debugstr_w(szDialogName));
4268 preview = msihandle2msiinfo( hPreview, MSIHANDLETYPE_PREVIEW );
4269 if (!preview)
4270 return ERROR_INVALID_HANDLE;
4272 r = MSI_PreviewDialogW( preview, szDialogName );
4273 msiobj_release( &preview->hdr );
4274 return r;
4277 UINT WINAPI MsiPreviewDialogA( MSIHANDLE hPreview, LPCSTR szDialogName )
4279 UINT r;
4280 LPWSTR strW = NULL;
4282 TRACE("%d %s\n", hPreview, debugstr_a(szDialogName));
4284 if (szDialogName)
4286 strW = strdupAtoW( szDialogName );
4287 if (!strW)
4288 return ERROR_OUTOFMEMORY;
4290 r = MsiPreviewDialogW( hPreview, strW );
4291 msi_free( strW );
4292 return r;
4295 UINT WINAPI MsiPreviewBillboardW( MSIHANDLE hPreview, LPCWSTR szControlName, LPCWSTR szBillboard )
4297 FIXME("%d %s %s\n", hPreview, debugstr_w(szControlName), debugstr_w(szBillboard));
4298 return ERROR_CALL_NOT_IMPLEMENTED;
4301 UINT WINAPI MsiPreviewBillboardA( MSIHANDLE hPreview, LPCSTR szControlName, LPCSTR szBillboard )
4303 FIXME("%d %s %s\n", hPreview, debugstr_a(szControlName), debugstr_a(szBillboard));
4304 return ERROR_CALL_NOT_IMPLEMENTED;
4307 typedef UINT (*event_handler)( msi_dialog *, const WCHAR * );
4309 struct control_event
4311 const WCHAR *event;
4312 event_handler handler;
4315 static UINT dialog_event_handler( msi_dialog *, const WCHAR *, const WCHAR * );
4317 /* create a dialog box and run it if it's modal */
4318 static UINT event_do_dialog( MSIPACKAGE *package, const WCHAR *name, msi_dialog *parent, BOOL destroy_modeless )
4320 msi_dialog *dialog;
4321 UINT r;
4323 /* create a new dialog */
4324 dialog = dialog_create( package, name, parent, dialog_event_handler );
4325 if (dialog)
4327 /* kill the current modeless dialog */
4328 if (destroy_modeless && package->dialog)
4330 msi_dialog_destroy( package->dialog );
4331 package->dialog = NULL;
4334 /* modeless dialogs return an error message */
4335 r = dialog_run_message_loop( dialog );
4336 if (r == ERROR_SUCCESS)
4337 msi_dialog_destroy( dialog );
4338 else
4339 package->dialog = dialog;
4341 else r = ERROR_FUNCTION_FAILED;
4342 return r;
4345 /* end a modal dialog box */
4346 static UINT event_end_dialog( msi_dialog *dialog, const WCHAR *argument )
4348 static const WCHAR exitW[] = {'E','x','i','t',0};
4349 static const WCHAR retryW[] = {'R','e','t','r','y',0};
4350 static const WCHAR ignoreW[] = {'I','g','n','o','r','e',0};
4351 static const WCHAR returnW[] = {'R','e','t','u','r','n',0};
4353 if (!strcmpW( argument, exitW ))
4354 dialog->package->CurrentInstallState = ERROR_INSTALL_USEREXIT;
4355 else if (!strcmpW( argument, retryW ))
4356 dialog->package->CurrentInstallState = ERROR_INSTALL_SUSPEND;
4357 else if (!strcmpW( argument, ignoreW ))
4358 dialog->package->CurrentInstallState = ERROR_SUCCESS;
4359 else if (!strcmpW( argument, returnW ))
4361 msi_dialog *parent = dialog->parent;
4362 msi_free( dialog->package->next_dialog );
4363 dialog->package->next_dialog = (parent) ? strdupW( parent->name ) : NULL;
4364 dialog->package->CurrentInstallState = ERROR_SUCCESS;
4366 else
4368 ERR("Unknown argument string %s\n", debugstr_w(argument));
4369 dialog->package->CurrentInstallState = ERROR_FUNCTION_FAILED;
4371 event_cleanup_subscriptions( dialog->package, dialog->name );
4372 msi_dialog_end_dialog( dialog );
4373 return ERROR_SUCCESS;
4376 /* transition from one modal dialog to another modal dialog */
4377 static UINT event_new_dialog( msi_dialog *dialog, const WCHAR *argument )
4379 /* store the name of the next dialog, and signal this one to end */
4380 dialog->package->next_dialog = strdupW( argument );
4381 msi_event_cleanup_all_subscriptions( dialog->package );
4382 msi_dialog_end_dialog( dialog );
4383 return ERROR_SUCCESS;
4386 /* create a new child dialog of an existing modal dialog */
4387 static UINT event_spawn_dialog( msi_dialog *dialog, const WCHAR *argument )
4389 /* don't destroy a modeless dialogs that might be our parent */
4390 event_do_dialog( dialog->package, argument, dialog, FALSE );
4391 if (dialog->package->CurrentInstallState != ERROR_SUCCESS) msi_dialog_end_dialog( dialog );
4392 return ERROR_SUCCESS;
4395 /* creates a dialog that remains up for a period of time based on a condition */
4396 static UINT event_spawn_wait_dialog( msi_dialog *dialog, const WCHAR *argument )
4398 FIXME("doing nothing\n");
4399 return ERROR_SUCCESS;
4402 static UINT event_do_action( msi_dialog *dialog, const WCHAR *argument )
4404 ACTION_PerformAction( dialog->package, argument, SCRIPT_NONE );
4405 return ERROR_SUCCESS;
4408 static UINT event_add_local( msi_dialog *dialog, const WCHAR *argument )
4410 MSIFEATURE *feature;
4412 LIST_FOR_EACH_ENTRY( feature, &dialog->package->features, MSIFEATURE, entry )
4414 if (!strcmpW( argument, feature->Feature ) || !strcmpW( argument, szAll ))
4416 if (feature->ActionRequest != INSTALLSTATE_LOCAL)
4417 msi_set_property( dialog->package->db, szPreselected, szOne, -1 );
4418 MSI_SetFeatureStateW( dialog->package, feature->Feature, INSTALLSTATE_LOCAL );
4421 return ERROR_SUCCESS;
4424 static UINT event_remove( msi_dialog *dialog, const WCHAR *argument )
4426 MSIFEATURE *feature;
4428 LIST_FOR_EACH_ENTRY( feature, &dialog->package->features, MSIFEATURE, entry )
4430 if (!strcmpW( argument, feature->Feature ) || !strcmpW( argument, szAll ))
4432 if (feature->ActionRequest != INSTALLSTATE_ABSENT)
4433 msi_set_property( dialog->package->db, szPreselected, szOne, -1 );
4434 MSI_SetFeatureStateW( dialog->package, feature->Feature, INSTALLSTATE_ABSENT );
4437 return ERROR_SUCCESS;
4440 static UINT event_add_source( msi_dialog *dialog, const WCHAR *argument )
4442 MSIFEATURE *feature;
4444 LIST_FOR_EACH_ENTRY( feature, &dialog->package->features, MSIFEATURE, entry )
4446 if (!strcmpW( argument, feature->Feature ) || !strcmpW( argument, szAll ))
4448 if (feature->ActionRequest != INSTALLSTATE_SOURCE)
4449 msi_set_property( dialog->package->db, szPreselected, szOne, -1 );
4450 MSI_SetFeatureStateW( dialog->package, feature->Feature, INSTALLSTATE_SOURCE );
4453 return ERROR_SUCCESS;
4456 void msi_event_fire( MSIPACKAGE *package, const WCHAR *event, MSIRECORD *rec )
4458 struct subscriber *sub;
4460 TRACE("firing event %s\n", debugstr_w(event));
4462 LIST_FOR_EACH_ENTRY( sub, &package->subscriptions, struct subscriber, entry )
4464 if (strcmpiW( sub->event, event )) continue;
4465 dialog_handle_event( sub->dialog, sub->control, sub->attribute, rec );
4469 static UINT event_set_target_path( msi_dialog *dialog, const WCHAR *argument )
4471 WCHAR *path = msi_dup_property( dialog->package->db, argument );
4472 MSIRECORD *rec = MSI_CreateRecord( 1 );
4473 UINT r = ERROR_SUCCESS;
4475 MSI_RecordSetStringW( rec, 1, path );
4476 msi_event_fire( dialog->package, szSelectionPath, rec );
4477 if (path)
4479 /* failure to set the path halts the executing of control events */
4480 r = MSI_SetTargetPathW( dialog->package, argument, path );
4481 msi_free( path );
4483 msi_free( &rec->hdr );
4484 return r;
4487 static UINT event_reset( msi_dialog *dialog, const WCHAR *argument )
4489 msi_dialog_reset( dialog );
4490 return ERROR_SUCCESS;
4493 /* Return ERROR_SUCCESS if dialog is process and ERROR_FUNCTION_FAILED
4494 * if the given parameter is not a dialog box
4496 UINT ACTION_DialogBox( MSIPACKAGE *package, const WCHAR *dialog )
4498 UINT r;
4500 if (package->next_dialog) ERR("Already got next dialog... ignoring it\n");
4501 package->next_dialog = NULL;
4503 /* Dialogs are chained by filling in the next_dialog member
4504 * of the package structure, then terminating the current dialog.
4505 * The code below sees the next_dialog member set, and runs the
4506 * next dialog.
4507 * We fall out of the loop below if we come across a modeless
4508 * dialog, as it returns ERROR_IO_PENDING when we try to run
4509 * its message loop.
4511 r = event_do_dialog( package, dialog, NULL, TRUE );
4512 while (r == ERROR_SUCCESS && package->next_dialog)
4514 WCHAR *name = package->next_dialog;
4516 package->next_dialog = NULL;
4517 r = event_do_dialog( package, name, NULL, TRUE );
4518 msi_free( name );
4520 if (r == ERROR_IO_PENDING) r = ERROR_SUCCESS;
4521 return r;
4524 static UINT event_set_install_level( msi_dialog *dialog, const WCHAR *argument )
4526 int level = atolW( argument );
4528 TRACE("setting install level to %d\n", level);
4529 return MSI_SetInstallLevel( dialog->package, level );
4532 static UINT event_directory_list_up( msi_dialog *dialog, const WCHAR *argument )
4534 return msi_dialog_directorylist_up( dialog );
4537 static UINT event_reinstall_mode( msi_dialog *dialog, const WCHAR *argument )
4539 return msi_set_property( dialog->package->db, szReinstallMode, argument, -1 );
4542 static UINT event_reinstall( msi_dialog *dialog, const WCHAR *argument )
4544 return msi_set_property( dialog->package->db, szReinstall, argument, -1 );
4547 static UINT event_validate_product_id( msi_dialog *dialog, const WCHAR *argument )
4549 return msi_validate_product_id( dialog->package );
4552 static const WCHAR end_dialogW[] = {'E','n','d','D','i','a','l','o','g',0};
4553 static const WCHAR new_dialogW[] = {'N','e','w','D','i','a','l','o','g',0};
4554 static const WCHAR spawn_dialogW[] = {'S','p','a','w','n','D','i','a','l','o','g',0};
4555 static const WCHAR spawn_wait_dialogW[] = {'S','p','a','w','n','W','a','i','t','D','i','a','l','o','g',0};
4556 static const WCHAR do_actionW[] = {'D','o','A','c','t','i','o','n',0};
4557 static const WCHAR add_localW[] = {'A','d','d','L','o','c','a','l',0};
4558 static const WCHAR removeW[] = {'R','e','m','o','v','e',0};
4559 static const WCHAR add_sourceW[] = {'A','d','d','S','o','u','r','c','e',0};
4560 static const WCHAR set_target_pathW[] = {'S','e','t','T','a','r','g','e','t','P','a','t','h',0};
4561 static const WCHAR resetW[] = {'R','e','s','e','t',0};
4562 static const WCHAR set_install_levelW[] = {'S','e','t','I','n','s','t','a','l','l','L','e','v','e','l',0};
4563 static const WCHAR directory_list_upW[] = {'D','i','r','e','c','t','o','r','y','L','i','s','t','U','p',0};
4564 static const WCHAR selection_browseW[] = {'S','e','l','e','c','t','i','o','n','B','r','o','w','s','e',0};
4565 static const WCHAR reinstall_modeW[] = {'R','e','i','n','s','t','a','l','l','M','o','d','e',0};
4566 static const WCHAR reinstallW[] = {'R','e','i','n','s','t','a','l','l',0};
4567 static const WCHAR validate_product_idW[] = {'V','a','l','i','d','a','t','e','P','r','o','d','u','c','t','I','D',0};
4569 static const struct control_event control_events[] =
4571 { end_dialogW, event_end_dialog },
4572 { new_dialogW, event_new_dialog },
4573 { spawn_dialogW, event_spawn_dialog },
4574 { spawn_wait_dialogW, event_spawn_wait_dialog },
4575 { do_actionW, event_do_action },
4576 { add_localW, event_add_local },
4577 { removeW, event_remove },
4578 { add_sourceW, event_add_source },
4579 { set_target_pathW, event_set_target_path },
4580 { resetW, event_reset },
4581 { set_install_levelW, event_set_install_level },
4582 { directory_list_upW, event_directory_list_up },
4583 { selection_browseW, event_spawn_dialog },
4584 { reinstall_modeW, event_reinstall_mode },
4585 { reinstallW, event_reinstall },
4586 { validate_product_idW, event_validate_product_id },
4587 { NULL, NULL }
4590 static UINT dialog_event_handler( msi_dialog *dialog, const WCHAR *event, const WCHAR *argument )
4592 unsigned int i;
4594 TRACE("handling event %s\n", debugstr_w(event));
4596 if (!event) return ERROR_SUCCESS;
4598 for (i = 0; control_events[i].event; i++)
4600 if (!strcmpW( control_events[i].event, event ))
4601 return control_events[i].handler( dialog, argument );
4603 FIXME("unhandled event %s arg(%s)\n", debugstr_w(event), debugstr_w(argument));
4604 return ERROR_SUCCESS;