wined3d: Check BLENDWEIGHT and BLENDINDICES FFP attributes for fixups.
[wine.git] / dlls / msi / dialog.c
blobfc9c24c90a32f09c40861158123b0246e48d07c4
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_update_all_controls( msi_dialog *dialog )
561 msi_control *control;
563 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
565 if ( control->property && control->update )
566 control->update( dialog, control );
570 static void msi_dialog_set_property( MSIPACKAGE *package, LPCWSTR property, LPCWSTR value )
572 UINT r = msi_set_property( package->db, property, value, -1 );
573 if (r == ERROR_SUCCESS && !strcmpW( property, szSourceDir ))
574 msi_reset_folders( package, TRUE );
577 static MSIFEATURE *msi_seltree_feature_from_item( HWND hwnd, HTREEITEM hItem )
579 TVITEMW tvi;
581 /* get the feature from the item */
582 memset( &tvi, 0, sizeof tvi );
583 tvi.hItem = hItem;
584 tvi.mask = TVIF_PARAM | TVIF_HANDLE;
585 SendMessageW( hwnd, TVM_GETITEMW, 0, (LPARAM)&tvi );
586 return (MSIFEATURE *)tvi.lParam;
589 struct msi_selection_tree_info
591 msi_dialog *dialog;
592 HWND hwnd;
593 WNDPROC oldproc;
594 HTREEITEM selected;
597 static MSIFEATURE *msi_seltree_get_selected_feature( msi_control *control )
599 struct msi_selection_tree_info *info = GetPropW( control->hwnd, szButtonData );
600 return msi_seltree_feature_from_item( control->hwnd, info->selected );
603 static void dialog_handle_event( msi_dialog *dialog, const WCHAR *control,
604 const WCHAR *attribute, MSIRECORD *rec )
606 msi_control* ctrl;
608 ctrl = msi_dialog_find_control( dialog, control );
609 if (!ctrl)
610 return;
611 if( !strcmpW( attribute, szText ) )
613 const WCHAR *font_text, *text = NULL;
614 WCHAR *font, *text_fmt = NULL;
616 font_text = MSI_RecordGetString( rec , 1 );
617 font = msi_dialog_get_style( font_text, &text );
618 deformat_string( dialog->package, text, &text_fmt );
619 if (text_fmt) text = text_fmt;
620 else text = szEmpty;
622 SetWindowTextW( ctrl->hwnd, text );
624 msi_free( font );
625 msi_free( text_fmt );
626 msi_dialog_check_messages( NULL );
628 else if( !strcmpW( attribute, szProgress ) )
630 DWORD func, val1, val2, units;
632 func = MSI_RecordGetInteger( rec, 1 );
633 val1 = MSI_RecordGetInteger( rec, 2 );
634 val2 = MSI_RecordGetInteger( rec, 3 );
636 TRACE("progress: func %u val1 %u val2 %u\n", func, val1, val2);
638 units = val1 / 512;
639 switch (func)
641 case 0: /* init */
642 SendMessageW( ctrl->hwnd, PBM_SETRANGE, 0, MAKELPARAM(0,100) );
643 if (val2)
645 ctrl->progress_max = units ? units : 100;
646 ctrl->progress_current = units;
647 ctrl->progress_backwards = TRUE;
648 SendMessageW( ctrl->hwnd, PBM_SETPOS, 100, 0 );
650 else
652 ctrl->progress_max = units ? units : 100;
653 ctrl->progress_current = 0;
654 ctrl->progress_backwards = FALSE;
655 SendMessageW( ctrl->hwnd, PBM_SETPOS, 0, 0 );
657 break;
658 case 1: /* action data increment */
659 if (val2) dialog->package->action_progress_increment = val1;
660 else dialog->package->action_progress_increment = 0;
661 break;
662 case 2: /* move */
663 if (ctrl->progress_backwards)
665 if (units >= ctrl->progress_current) ctrl->progress_current -= units;
666 else ctrl->progress_current = 0;
668 else
670 if (ctrl->progress_current + units < ctrl->progress_max) ctrl->progress_current += units;
671 else ctrl->progress_current = ctrl->progress_max;
673 SendMessageW( ctrl->hwnd, PBM_SETPOS, MulDiv(100, ctrl->progress_current, ctrl->progress_max), 0 );
674 break;
675 case 3: /* add */
676 ctrl->progress_max += units;
677 break;
678 default:
679 FIXME("Unknown progress message %u\n", func);
680 break;
683 else if ( !strcmpW( attribute, szProperty ) )
685 MSIFEATURE *feature = msi_seltree_get_selected_feature( ctrl );
686 if (feature) msi_dialog_set_property( dialog->package, ctrl->property, feature->Directory );
688 else if ( !strcmpW( attribute, szSelectionPath ) )
690 BOOL indirect = ctrl->attributes & msidbControlAttributesIndirect;
691 LPWSTR path = msi_dialog_dup_property( dialog, ctrl->property, indirect );
692 if (!path) return;
693 SetWindowTextW( ctrl->hwnd, path );
694 msi_free(path);
696 else
698 FIXME("Attribute %s not being set\n", debugstr_w(attribute));
699 return;
703 static void event_subscribe( msi_dialog *dialog, const WCHAR *event, const WCHAR *control, const WCHAR *attribute )
705 struct subscriber *sub;
707 TRACE("dialog %s event %s control %s attribute %s\n", debugstr_w(dialog->name), debugstr_w(event),
708 debugstr_w(control), debugstr_w(attribute));
710 LIST_FOR_EACH_ENTRY( sub, &dialog->package->subscriptions, struct subscriber, entry )
712 if (sub->dialog == dialog &&
713 !strcmpiW( sub->event, event ) &&
714 !strcmpiW( sub->control, control ) &&
715 !strcmpiW( sub->attribute, attribute ))
717 TRACE("already subscribed\n");
718 return;
721 if (!(sub = msi_alloc( sizeof(*sub) ))) return;
722 sub->dialog = dialog;
723 sub->event = strdupW( event );
724 sub->control = strdupW( control );
725 sub->attribute = strdupW( attribute );
726 list_add_tail( &dialog->package->subscriptions, &sub->entry );
729 struct dialog_control
731 msi_dialog *dialog;
732 const WCHAR *control;
735 static UINT map_event( MSIRECORD *row, void *param )
737 struct dialog_control *dc = param;
738 const WCHAR *event = MSI_RecordGetString( row, 3 );
739 const WCHAR *attribute = MSI_RecordGetString( row, 4 );
741 event_subscribe( dc->dialog, event, dc->control, attribute );
742 return ERROR_SUCCESS;
745 static void dialog_map_events( msi_dialog *dialog, const WCHAR *control )
747 static const WCHAR queryW[] =
748 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
749 '`','E','v','e','n','t','M','a','p','p','i','n','g','`',' ',
750 'W','H','E','R','E',' ','`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
751 'A','N','D',' ','`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',0};
752 MSIQUERY *view;
753 struct dialog_control dialog_control =
755 dialog,
756 control
759 if (!MSI_OpenQuery( dialog->package->db, &view, queryW, dialog->name, control ))
761 MSI_IterateRecords( view, NULL, map_event, &dialog_control );
762 msiobj_release( &view->hdr );
766 /* everything except radio buttons */
767 static msi_control *msi_dialog_add_control( msi_dialog *dialog,
768 MSIRECORD *rec, LPCWSTR szCls, DWORD style )
770 DWORD attributes;
771 const WCHAR *text = NULL, *name, *control_type;
772 DWORD exstyle = 0;
774 name = MSI_RecordGetString( rec, 2 );
775 control_type = MSI_RecordGetString( rec, 3 );
776 attributes = MSI_RecordGetInteger( rec, 8 );
777 if (strcmpW( control_type, szScrollableText )) text = MSI_RecordGetString( rec, 10 );
779 TRACE("%s, %s, %08x, %s, %08x\n", debugstr_w(szCls), debugstr_w(name),
780 attributes, debugstr_w(text), style);
782 if( attributes & msidbControlAttributesVisible )
783 style |= WS_VISIBLE;
784 if( ~attributes & msidbControlAttributesEnabled )
785 style |= WS_DISABLED;
786 if( attributes & msidbControlAttributesSunken )
787 exstyle |= WS_EX_CLIENTEDGE;
789 dialog_map_events( dialog, name );
791 return dialog_create_window( dialog, rec, exstyle, szCls, name, text, style, dialog->hwnd );
794 struct msi_text_info
796 msi_font *font;
797 WNDPROC oldproc;
798 DWORD attributes;
802 * we don't erase our own background,
803 * so we have to make sure that the parent window redraws first
805 static void msi_text_on_settext( HWND hWnd )
807 HWND hParent;
808 RECT rc;
810 hParent = GetParent( hWnd );
811 GetWindowRect( hWnd, &rc );
812 MapWindowPoints( NULL, hParent, (LPPOINT) &rc, 2 );
813 InvalidateRect( hParent, &rc, TRUE );
816 static LRESULT WINAPI
817 MSIText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
819 struct msi_text_info *info;
820 LRESULT r = 0;
822 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
824 info = GetPropW(hWnd, szButtonData);
826 if( msg == WM_CTLCOLORSTATIC &&
827 ( info->attributes & msidbControlAttributesTransparent ) )
829 SetBkMode( (HDC)wParam, TRANSPARENT );
830 return (LRESULT) GetStockObject(NULL_BRUSH);
833 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
834 if ( info->font )
835 SetTextColor( (HDC)wParam, info->font->color );
837 switch( msg )
839 case WM_SETTEXT:
840 msi_text_on_settext( hWnd );
841 break;
842 case WM_NCDESTROY:
843 msi_free( info );
844 RemovePropW( hWnd, szButtonData );
845 break;
848 return r;
851 static UINT msi_dialog_text_control( msi_dialog *dialog, MSIRECORD *rec )
853 msi_control *control;
854 struct msi_text_info *info;
855 LPCWSTR text, ptr, prop, control_name;
856 LPWSTR font_name;
858 TRACE("%p %p\n", dialog, rec);
860 control = msi_dialog_add_control( dialog, rec, szStatic, SS_LEFT | WS_GROUP );
861 if( !control )
862 return ERROR_FUNCTION_FAILED;
864 info = msi_alloc( sizeof *info );
865 if( !info )
866 return ERROR_SUCCESS;
868 control_name = MSI_RecordGetString( rec, 2 );
869 control->attributes = MSI_RecordGetInteger( rec, 8 );
870 prop = MSI_RecordGetString( rec, 9 );
871 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
873 text = MSI_RecordGetString( rec, 10 );
874 font_name = msi_dialog_get_style( text, &ptr );
875 info->font = ( font_name ) ? msi_dialog_find_font( dialog, font_name ) : NULL;
876 msi_free( font_name );
878 info->attributes = MSI_RecordGetInteger( rec, 8 );
879 if( info->attributes & msidbControlAttributesTransparent )
880 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT );
882 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
883 (LONG_PTR)MSIText_WndProc );
884 SetPropW( control->hwnd, szButtonData, info );
886 event_subscribe( dialog, szSelectionPath, control_name, szSelectionPath );
887 return ERROR_SUCCESS;
890 /* strip any leading text style label from text field */
891 static WCHAR *msi_get_binary_name( MSIPACKAGE *package, MSIRECORD *rec )
893 WCHAR *p, *text;
895 text = msi_get_deformatted_field( package, rec, 10 );
896 if (!text)
897 return NULL;
899 p = text;
900 while (*p && *p != '{') p++;
901 if (!*p++) return text;
903 while (*p && *p != '}') p++;
904 if (!*p++) return text;
906 p = strdupW( p );
907 msi_free( text );
908 return p;
911 static UINT msi_dialog_set_property_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
913 static const WCHAR szNullArg[] = {'{','}',0};
914 LPWSTR p, prop, arg_fmt = NULL;
915 UINT len;
917 len = strlenW( event );
918 prop = msi_alloc( len * sizeof(WCHAR) );
919 strcpyW( prop, &event[1] );
920 p = strchrW( prop, ']' );
921 if (p && (p[1] == 0 || p[1] == ' '))
923 *p = 0;
924 if (strcmpW( szNullArg, arg ))
925 deformat_string( dialog->package, arg, &arg_fmt );
926 msi_dialog_set_property( dialog->package, prop, arg_fmt );
927 msi_dialog_update_controls( dialog, prop );
928 msi_free( arg_fmt );
930 else ERR("Badly formatted property string - what happens?\n");
931 msi_free( prop );
932 return ERROR_SUCCESS;
935 static UINT msi_dialog_send_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
937 LPWSTR event_fmt = NULL, arg_fmt = NULL;
939 TRACE("Sending control event %s %s\n", debugstr_w(event), debugstr_w(arg));
941 deformat_string( dialog->package, event, &event_fmt );
942 deformat_string( dialog->package, arg, &arg_fmt );
944 dialog->event_handler( dialog, event_fmt, arg_fmt );
946 msi_free( event_fmt );
947 msi_free( arg_fmt );
949 return ERROR_SUCCESS;
952 static UINT msi_dialog_control_event( MSIRECORD *rec, LPVOID param )
954 msi_dialog *dialog = param;
955 LPCWSTR condition, event, arg;
956 UINT r;
958 condition = MSI_RecordGetString( rec, 5 );
959 r = MSI_EvaluateConditionW( dialog->package, condition );
960 if (r == MSICONDITION_TRUE || r == MSICONDITION_NONE)
962 event = MSI_RecordGetString( rec, 3 );
963 arg = MSI_RecordGetString( rec, 4 );
964 if (event[0] == '[')
965 msi_dialog_set_property_event( dialog, event, arg );
966 else
967 msi_dialog_send_event( dialog, event, arg );
969 return ERROR_SUCCESS;
972 static UINT msi_dialog_button_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
974 static const WCHAR query[] = {
975 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
976 'C','o','n','t','r','o','l','E','v','e','n','t',' ','W','H','E','R','E',' ',
977 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ','A','N','D',' ',
978 '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',' ',
979 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','i','n','g','`',0};
980 MSIQUERY *view;
981 UINT r;
983 if (HIWORD(param) != BN_CLICKED)
984 return ERROR_SUCCESS;
986 r = MSI_OpenQuery( dialog->package->db, &view, query, dialog->name, control->name );
987 if (r != ERROR_SUCCESS)
989 ERR("query failed\n");
990 return ERROR_SUCCESS;
992 r = MSI_IterateRecords( view, 0, msi_dialog_control_event, dialog );
993 msiobj_release( &view->hdr );
994 return r;
997 static UINT msi_dialog_button_control( msi_dialog *dialog, MSIRECORD *rec )
999 msi_control *control;
1000 UINT attributes, style;
1002 TRACE("%p %p\n", dialog, rec);
1004 style = WS_TABSTOP;
1005 attributes = MSI_RecordGetInteger( rec, 8 );
1006 if( attributes & msidbControlAttributesIcon )
1007 style |= BS_ICON;
1009 control = msi_dialog_add_control( dialog, rec, szButton, style );
1010 if( !control )
1011 return ERROR_FUNCTION_FAILED;
1013 control->handler = msi_dialog_button_handler;
1015 if (attributes & msidbControlAttributesIcon)
1017 /* set the icon */
1018 LPWSTR name = msi_get_binary_name( dialog->package, rec );
1019 control->hIcon = msi_load_icon( dialog->package->db, name, attributes );
1020 if (control->hIcon)
1022 SendMessageW( control->hwnd, BM_SETIMAGE, IMAGE_ICON, (LPARAM) control->hIcon );
1024 else
1025 ERR("Failed to load icon %s\n", debugstr_w(name));
1026 msi_free( name );
1029 return ERROR_SUCCESS;
1032 static LPWSTR msi_get_checkbox_value( msi_dialog *dialog, LPCWSTR prop )
1034 static const WCHAR query[] = {
1035 'S','E','L','E','C','T',' ','*',' ',
1036 'F','R','O','M',' ','`','C','h','e','c','k','B','o','x','`',' ',
1037 'W','H','E','R','E',' ',
1038 '`','P','r','o','p','e','r','t','y','`',' ','=',' ',
1039 '\'','%','s','\'',0
1041 MSIRECORD *rec = NULL;
1042 LPWSTR ret = NULL;
1044 /* find if there is a value associated with the checkbox */
1045 rec = MSI_QueryGetRecord( dialog->package->db, query, prop );
1046 if (!rec)
1047 return ret;
1049 ret = msi_get_deformatted_field( dialog->package, rec, 2 );
1050 if( ret && !ret[0] )
1052 msi_free( ret );
1053 ret = NULL;
1055 msiobj_release( &rec->hdr );
1056 if (ret)
1057 return ret;
1059 ret = msi_dup_property( dialog->package->db, prop );
1060 if( ret && !ret[0] )
1062 msi_free( ret );
1063 ret = NULL;
1066 return ret;
1069 static UINT msi_dialog_get_checkbox_state( msi_dialog *dialog, msi_control *control )
1071 WCHAR state[2] = {0};
1072 DWORD sz = 2;
1074 msi_get_property( dialog->package->db, control->property, state, &sz );
1075 return state[0] ? 1 : 0;
1078 static void msi_dialog_set_checkbox_state( msi_dialog *dialog, msi_control *control, UINT state )
1080 static const WCHAR szState[] = {'1',0};
1081 LPCWSTR val;
1083 /* if uncheck then the property is set to NULL */
1084 if (!state)
1086 msi_dialog_set_property( dialog->package, control->property, NULL );
1087 return;
1090 /* check for a custom state */
1091 if (control->value && control->value[0])
1092 val = control->value;
1093 else
1094 val = szState;
1096 msi_dialog_set_property( dialog->package, control->property, val );
1099 static void msi_dialog_checkbox_sync_state( msi_dialog *dialog, msi_control *control )
1101 UINT state = msi_dialog_get_checkbox_state( dialog, control );
1102 SendMessageW( control->hwnd, BM_SETCHECK, state ? BST_CHECKED : BST_UNCHECKED, 0 );
1105 static UINT msi_dialog_checkbox_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
1107 UINT state;
1109 if (HIWORD(param) != BN_CLICKED)
1110 return ERROR_SUCCESS;
1112 TRACE("clicked checkbox %s, set %s\n", debugstr_w(control->name), debugstr_w(control->property));
1114 state = msi_dialog_get_checkbox_state( dialog, control );
1115 state = state ? 0 : 1;
1116 msi_dialog_set_checkbox_state( dialog, control, state );
1117 msi_dialog_checkbox_sync_state( dialog, control );
1119 return msi_dialog_button_handler( dialog, control, param );
1122 static UINT msi_dialog_checkbox_control( msi_dialog *dialog, MSIRECORD *rec )
1124 msi_control *control;
1125 LPCWSTR prop;
1127 TRACE("%p %p\n", dialog, rec);
1129 control = msi_dialog_add_control( dialog, rec, szButton, BS_CHECKBOX | BS_MULTILINE | WS_TABSTOP );
1130 control->handler = msi_dialog_checkbox_handler;
1131 control->update = msi_dialog_checkbox_sync_state;
1132 prop = MSI_RecordGetString( rec, 9 );
1133 if (prop)
1135 control->property = strdupW( prop );
1136 control->value = msi_get_checkbox_value( dialog, prop );
1137 TRACE("control %s value %s\n", debugstr_w(control->property), debugstr_w(control->value));
1139 msi_dialog_checkbox_sync_state( dialog, control );
1140 return ERROR_SUCCESS;
1143 static UINT msi_dialog_line_control( msi_dialog *dialog, MSIRECORD *rec )
1145 DWORD attributes;
1146 LPCWSTR name;
1147 DWORD style, exstyle = 0;
1148 DWORD x, y, width, height;
1149 msi_control *control;
1151 TRACE("%p %p\n", dialog, rec);
1153 style = WS_CHILD | SS_ETCHEDHORZ | SS_SUNKEN;
1155 name = MSI_RecordGetString( rec, 2 );
1156 attributes = MSI_RecordGetInteger( rec, 8 );
1158 if( attributes & msidbControlAttributesVisible )
1159 style |= WS_VISIBLE;
1160 if( ~attributes & msidbControlAttributesEnabled )
1161 style |= WS_DISABLED;
1162 if( attributes & msidbControlAttributesSunken )
1163 exstyle |= WS_EX_CLIENTEDGE;
1165 dialog_map_events( dialog, name );
1167 control = msi_alloc( FIELD_OFFSET(msi_control, name[strlenW( name ) + 1] ));
1168 if (!control)
1169 return ERROR_OUTOFMEMORY;
1171 strcpyW( control->name, name );
1172 list_add_head( &dialog->controls, &control->entry );
1173 control->handler = NULL;
1174 control->property = NULL;
1175 control->value = NULL;
1176 control->hBitmap = NULL;
1177 control->hIcon = NULL;
1178 control->hDll = NULL;
1179 control->tabnext = strdupW( MSI_RecordGetString( rec, 11) );
1180 control->type = strdupW( MSI_RecordGetString( rec, 3 ) );
1181 control->progress_current = 0;
1182 control->progress_max = 100;
1183 control->progress_backwards = FALSE;
1185 x = MSI_RecordGetInteger( rec, 4 );
1186 y = MSI_RecordGetInteger( rec, 5 );
1187 width = MSI_RecordGetInteger( rec, 6 );
1189 x = msi_dialog_scale_unit( dialog, x );
1190 y = msi_dialog_scale_unit( dialog, y );
1191 width = msi_dialog_scale_unit( dialog, width );
1192 height = 2; /* line is exactly 2 units in height */
1194 control->hwnd = CreateWindowExW( exstyle, szStatic, NULL, style,
1195 x, y, width, height, dialog->hwnd, NULL, NULL, NULL );
1197 TRACE("Dialog %s control %s hwnd %p\n",
1198 debugstr_w(dialog->name), debugstr_w(name), control->hwnd );
1200 return ERROR_SUCCESS;
1203 /******************** Scroll Text ********************************************/
1205 struct msi_scrolltext_info
1207 msi_dialog *dialog;
1208 msi_control *control;
1209 WNDPROC oldproc;
1212 static LRESULT WINAPI
1213 MSIScrollText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1215 struct msi_scrolltext_info *info;
1216 HRESULT r;
1218 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1220 info = GetPropW( hWnd, szButtonData );
1222 r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
1224 switch( msg )
1226 case WM_GETDLGCODE:
1227 return DLGC_WANTARROWS;
1228 case WM_NCDESTROY:
1229 msi_free( info );
1230 RemovePropW( hWnd, szButtonData );
1231 break;
1232 case WM_PAINT:
1233 /* native MSI sets a wait cursor here */
1234 msi_dialog_button_handler( info->dialog, info->control, BN_CLICKED );
1235 break;
1237 return r;
1240 struct msi_streamin_info
1242 LPSTR string;
1243 DWORD offset;
1244 DWORD length;
1247 static DWORD CALLBACK
1248 msi_richedit_stream_in( DWORD_PTR arg, LPBYTE buffer, LONG count, LONG *pcb )
1250 struct msi_streamin_info *info = (struct msi_streamin_info*) arg;
1252 if( (count + info->offset) > info->length )
1253 count = info->length - info->offset;
1254 memcpy( buffer, &info->string[ info->offset ], count );
1255 *pcb = count;
1256 info->offset += count;
1258 TRACE("%d/%d\n", info->offset, info->length);
1260 return 0;
1263 static void msi_scrolltext_add_text( msi_control *control, LPCWSTR text )
1265 struct msi_streamin_info info;
1266 EDITSTREAM es;
1268 info.string = strdupWtoA( text );
1269 info.offset = 0;
1270 info.length = lstrlenA( info.string ) + 1;
1272 es.dwCookie = (DWORD_PTR) &info;
1273 es.dwError = 0;
1274 es.pfnCallback = msi_richedit_stream_in;
1276 SendMessageW( control->hwnd, EM_STREAMIN, SF_RTF, (LPARAM) &es );
1278 msi_free( info.string );
1281 static UINT msi_dialog_scrolltext_control( msi_dialog *dialog, MSIRECORD *rec )
1283 static const WCHAR szRichEdit20W[] = {'R','i','c','h','E','d','i','t','2','0','W',0};
1284 struct msi_scrolltext_info *info;
1285 msi_control *control;
1286 HMODULE hRichedit;
1287 LPCWSTR text;
1288 DWORD style;
1290 info = msi_alloc( sizeof *info );
1291 if (!info)
1292 return ERROR_FUNCTION_FAILED;
1294 hRichedit = LoadLibraryA("riched20");
1296 style = WS_BORDER | ES_MULTILINE | WS_VSCROLL |
1297 ES_READONLY | ES_AUTOVSCROLL | WS_TABSTOP;
1298 control = msi_dialog_add_control( dialog, rec, szRichEdit20W, style );
1299 if (!control)
1301 FreeLibrary( hRichedit );
1302 msi_free( info );
1303 return ERROR_FUNCTION_FAILED;
1306 control->hDll = hRichedit;
1308 info->dialog = dialog;
1309 info->control = control;
1311 /* subclass the static control */
1312 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1313 (LONG_PTR)MSIScrollText_WndProc );
1314 SetPropW( control->hwnd, szButtonData, info );
1316 /* add the text into the richedit */
1317 text = MSI_RecordGetString( rec, 10 );
1318 if (text)
1319 msi_scrolltext_add_text( control, text );
1321 return ERROR_SUCCESS;
1324 static HBITMAP msi_load_picture( MSIDATABASE *db, LPCWSTR name,
1325 INT cx, INT cy, DWORD flags )
1327 HBITMAP hOleBitmap = 0, hBitmap = 0, hOldSrcBitmap, hOldDestBitmap;
1328 MSIRECORD *rec = NULL;
1329 IStream *stm = NULL;
1330 IPicture *pic = NULL;
1331 HDC srcdc, destdc;
1332 BITMAP bm;
1333 UINT r;
1335 rec = msi_get_binary_record( db, name );
1336 if( !rec )
1337 goto end;
1339 r = MSI_RecordGetIStream( rec, 2, &stm );
1340 msiobj_release( &rec->hdr );
1341 if( r != ERROR_SUCCESS )
1342 goto end;
1344 r = OleLoadPicture( stm, 0, TRUE, &IID_IPicture, (LPVOID*) &pic );
1345 IStream_Release( stm );
1346 if( FAILED( r ) )
1348 ERR("failed to load picture\n");
1349 goto end;
1352 r = IPicture_get_Handle( pic, (OLE_HANDLE*) &hOleBitmap );
1353 if( FAILED( r ) )
1355 ERR("failed to get bitmap handle\n");
1356 goto end;
1359 /* make the bitmap the desired size */
1360 r = GetObjectW( hOleBitmap, sizeof bm, &bm );
1361 if (r != sizeof bm )
1363 ERR("failed to get bitmap size\n");
1364 goto end;
1367 if (flags & LR_DEFAULTSIZE)
1369 cx = bm.bmWidth;
1370 cy = bm.bmHeight;
1373 srcdc = CreateCompatibleDC( NULL );
1374 hOldSrcBitmap = SelectObject( srcdc, hOleBitmap );
1375 destdc = CreateCompatibleDC( NULL );
1376 hBitmap = CreateCompatibleBitmap( srcdc, cx, cy );
1377 hOldDestBitmap = SelectObject( destdc, hBitmap );
1378 StretchBlt( destdc, 0, 0, cx, cy,
1379 srcdc, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
1380 SelectObject( srcdc, hOldSrcBitmap );
1381 SelectObject( destdc, hOldDestBitmap );
1382 DeleteDC( srcdc );
1383 DeleteDC( destdc );
1385 end:
1386 if ( pic )
1387 IPicture_Release( pic );
1388 return hBitmap;
1391 static UINT msi_dialog_bitmap_control( msi_dialog *dialog, MSIRECORD *rec )
1393 UINT cx, cy, flags, style, attributes;
1394 msi_control *control;
1395 LPWSTR name;
1397 flags = LR_LOADFROMFILE;
1398 style = SS_BITMAP | SS_LEFT | WS_GROUP;
1400 attributes = MSI_RecordGetInteger( rec, 8 );
1401 if( attributes & msidbControlAttributesFixedSize )
1403 flags |= LR_DEFAULTSIZE;
1404 style |= SS_CENTERIMAGE;
1407 control = msi_dialog_add_control( dialog, rec, szStatic, style );
1408 cx = MSI_RecordGetInteger( rec, 6 );
1409 cy = MSI_RecordGetInteger( rec, 7 );
1410 cx = msi_dialog_scale_unit( dialog, cx );
1411 cy = msi_dialog_scale_unit( dialog, cy );
1413 name = msi_get_binary_name( dialog->package, rec );
1414 control->hBitmap = msi_load_picture( dialog->package->db, name, cx, cy, flags );
1415 if( control->hBitmap )
1416 SendMessageW( control->hwnd, STM_SETIMAGE,
1417 IMAGE_BITMAP, (LPARAM) control->hBitmap );
1418 else
1419 ERR("Failed to load bitmap %s\n", debugstr_w(name));
1421 msi_free( name );
1423 return ERROR_SUCCESS;
1426 static UINT msi_dialog_icon_control( msi_dialog *dialog, MSIRECORD *rec )
1428 msi_control *control;
1429 DWORD attributes;
1430 LPWSTR name;
1432 TRACE("\n");
1434 control = msi_dialog_add_control( dialog, rec, szStatic,
1435 SS_ICON | SS_CENTERIMAGE | WS_GROUP );
1437 attributes = MSI_RecordGetInteger( rec, 8 );
1438 name = msi_get_binary_name( dialog->package, rec );
1439 control->hIcon = msi_load_icon( dialog->package->db, name, attributes );
1440 if( control->hIcon )
1441 SendMessageW( control->hwnd, STM_SETICON, (WPARAM) control->hIcon, 0 );
1442 else
1443 ERR("Failed to load bitmap %s\n", debugstr_w(name));
1444 msi_free( name );
1445 return ERROR_SUCCESS;
1448 /******************** Combo Box ***************************************/
1450 struct msi_combobox_info
1452 msi_dialog *dialog;
1453 HWND hwnd;
1454 WNDPROC oldproc;
1455 DWORD num_items;
1456 DWORD addpos_items;
1457 LPWSTR *items;
1460 static LRESULT WINAPI MSIComboBox_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1462 struct msi_combobox_info *info;
1463 LRESULT r;
1464 DWORD j;
1466 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1468 info = GetPropW( hWnd, szButtonData );
1469 if (!info)
1470 return 0;
1472 r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
1474 switch (msg)
1476 case WM_NCDESTROY:
1477 for (j = 0; j < info->num_items; j++)
1478 msi_free( info->items[j] );
1479 msi_free( info->items );
1480 msi_free( info );
1481 RemovePropW( hWnd, szButtonData );
1482 break;
1485 return r;
1488 static UINT msi_combobox_add_item( MSIRECORD *rec, LPVOID param )
1490 struct msi_combobox_info *info = param;
1491 LPCWSTR value, text;
1492 int pos;
1494 value = MSI_RecordGetString( rec, 3 );
1495 text = MSI_RecordGetString( rec, 4 );
1497 info->items[info->addpos_items] = strdupW( value );
1499 pos = SendMessageW( info->hwnd, CB_ADDSTRING, 0, (LPARAM)text );
1500 SendMessageW( info->hwnd, CB_SETITEMDATA, pos, (LPARAM)info->items[info->addpos_items] );
1501 info->addpos_items++;
1503 return ERROR_SUCCESS;
1506 static UINT msi_combobox_add_items( struct msi_combobox_info *info, LPCWSTR property )
1508 static const WCHAR query[] = {
1509 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1510 '`','C','o','m','b','o','B','o','x','`',' ','W','H','E','R','E',' ',
1511 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',' ',
1512 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','`',0};
1513 MSIQUERY *view;
1514 DWORD count;
1515 UINT r;
1517 r = MSI_OpenQuery( info->dialog->package->db, &view, query, property );
1518 if (r != ERROR_SUCCESS)
1519 return r;
1521 /* just get the number of records */
1522 count = 0;
1523 r = MSI_IterateRecords( view, &count, NULL, NULL );
1524 if (r != ERROR_SUCCESS)
1526 msiobj_release( &view->hdr );
1527 return r;
1529 info->num_items = count;
1530 info->items = msi_alloc( sizeof(*info->items) * count );
1532 r = MSI_IterateRecords( view, NULL, msi_combobox_add_item, info );
1533 msiobj_release( &view->hdr );
1534 return r;
1537 static UINT msi_dialog_set_control_condition( MSIRECORD *rec, LPVOID param )
1539 static const WCHAR szHide[] = {'H','i','d','e',0};
1540 static const WCHAR szShow[] = {'S','h','o','w',0};
1541 static const WCHAR szDisable[] = {'D','i','s','a','b','l','e',0};
1542 static const WCHAR szEnable[] = {'E','n','a','b','l','e',0};
1543 static const WCHAR szDefault[] = {'D','e','f','a','u','l','t',0};
1544 msi_dialog *dialog = param;
1545 msi_control *control;
1546 LPCWSTR name, action, condition;
1547 UINT r;
1549 name = MSI_RecordGetString( rec, 2 );
1550 action = MSI_RecordGetString( rec, 3 );
1551 condition = MSI_RecordGetString( rec, 4 );
1552 r = MSI_EvaluateConditionW( dialog->package, condition );
1553 control = msi_dialog_find_control( dialog, name );
1554 if (r == MSICONDITION_TRUE && control)
1556 TRACE("%s control %s\n", debugstr_w(action), debugstr_w(name));
1558 /* FIXME: case sensitive? */
1559 if (!strcmpW( action, szHide ))
1560 ShowWindow(control->hwnd, SW_HIDE);
1561 else if (!strcmpW( action, szShow ))
1562 ShowWindow(control->hwnd, SW_SHOW);
1563 else if (!strcmpW( action, szDisable ))
1564 EnableWindow(control->hwnd, FALSE);
1565 else if (!strcmpW( action, szEnable ))
1566 EnableWindow(control->hwnd, TRUE);
1567 else if (!strcmpW( action, szDefault ))
1568 SetFocus(control->hwnd);
1569 else
1570 FIXME("Unhandled action %s\n", debugstr_w(action));
1572 return ERROR_SUCCESS;
1575 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog )
1577 static const WCHAR query[] = {
1578 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1579 'C','o','n','t','r','o','l','C','o','n','d','i','t','i','o','n',' ',
1580 'W','H','E','R','E',' ','`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
1581 UINT r;
1582 MSIQUERY *view;
1583 MSIPACKAGE *package = dialog->package;
1585 TRACE("%p %s\n", dialog, debugstr_w(dialog->name));
1587 /* query the Control table for all the elements of the control */
1588 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
1589 if (r != ERROR_SUCCESS)
1590 return ERROR_SUCCESS;
1592 r = MSI_IterateRecords( view, 0, msi_dialog_set_control_condition, dialog );
1593 msiobj_release( &view->hdr );
1594 return r;
1597 static UINT msi_dialog_combobox_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
1599 struct msi_combobox_info *info;
1600 int index;
1601 LPWSTR value;
1603 if (HIWORD(param) != CBN_SELCHANGE && HIWORD(param) != CBN_EDITCHANGE)
1604 return ERROR_SUCCESS;
1606 info = GetPropW( control->hwnd, szButtonData );
1607 index = SendMessageW( control->hwnd, CB_GETCURSEL, 0, 0 );
1608 if (index == CB_ERR)
1609 value = msi_get_window_text( control->hwnd );
1610 else
1611 value = (LPWSTR) SendMessageW( control->hwnd, CB_GETITEMDATA, index, 0 );
1613 msi_dialog_set_property( info->dialog->package, control->property, value );
1614 msi_dialog_evaluate_control_conditions( info->dialog );
1616 if (index == CB_ERR)
1617 msi_free( value );
1619 return ERROR_SUCCESS;
1622 static void msi_dialog_combobox_update( msi_dialog *dialog, msi_control *control )
1624 struct msi_combobox_info *info;
1625 LPWSTR value, tmp;
1626 DWORD j;
1628 info = GetPropW( control->hwnd, szButtonData );
1630 value = msi_dup_property( dialog->package->db, control->property );
1631 if (!value)
1633 SendMessageW( control->hwnd, CB_SETCURSEL, -1, 0 );
1634 return;
1637 for (j = 0; j < info->num_items; j++)
1639 tmp = (LPWSTR) SendMessageW( control->hwnd, CB_GETITEMDATA, j, 0 );
1640 if (!strcmpW( value, tmp ))
1641 break;
1644 if (j < info->num_items)
1646 SendMessageW( control->hwnd, CB_SETCURSEL, j, 0 );
1648 else
1650 SendMessageW( control->hwnd, CB_SETCURSEL, -1, 0 );
1651 SetWindowTextW( control->hwnd, value );
1654 msi_free(value);
1657 static UINT msi_dialog_combo_control( msi_dialog *dialog, MSIRECORD *rec )
1659 struct msi_combobox_info *info;
1660 msi_control *control;
1661 DWORD attributes, style;
1662 LPCWSTR prop;
1664 info = msi_alloc( sizeof *info );
1665 if (!info)
1666 return ERROR_FUNCTION_FAILED;
1668 style = CBS_AUTOHSCROLL | WS_TABSTOP | WS_GROUP | WS_CHILD;
1669 attributes = MSI_RecordGetInteger( rec, 8 );
1670 if ( ~attributes & msidbControlAttributesSorted)
1671 style |= CBS_SORT;
1672 if ( attributes & msidbControlAttributesComboList)
1673 style |= CBS_DROPDOWNLIST;
1674 else
1675 style |= CBS_DROPDOWN;
1677 control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
1678 if (!control)
1680 msi_free( info );
1681 return ERROR_FUNCTION_FAILED;
1684 control->handler = msi_dialog_combobox_handler;
1685 control->update = msi_dialog_combobox_update;
1687 prop = MSI_RecordGetString( rec, 9 );
1688 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
1690 /* subclass */
1691 info->dialog = dialog;
1692 info->hwnd = control->hwnd;
1693 info->items = NULL;
1694 info->addpos_items = 0;
1695 info->oldproc = (WNDPROC)SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1696 (LONG_PTR)MSIComboBox_WndProc );
1697 SetPropW( control->hwnd, szButtonData, info );
1699 if (control->property)
1700 msi_combobox_add_items( info, control->property );
1702 msi_dialog_combobox_update( dialog, control );
1704 return ERROR_SUCCESS;
1707 static UINT msi_dialog_edit_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
1709 LPWSTR buf;
1711 if (HIWORD(param) != EN_CHANGE)
1712 return ERROR_SUCCESS;
1714 TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name), debugstr_w(control->property));
1716 buf = msi_get_window_text( control->hwnd );
1717 msi_dialog_set_property( dialog->package, control->property, buf );
1718 msi_free( buf );
1720 return ERROR_SUCCESS;
1723 /* length of 2^32 + 1 */
1724 #define MAX_NUM_DIGITS 11
1726 static UINT msi_dialog_edit_control( msi_dialog *dialog, MSIRECORD *rec )
1728 msi_control *control;
1729 LPCWSTR prop, text;
1730 LPWSTR val, begin, end;
1731 WCHAR num[MAX_NUM_DIGITS];
1732 DWORD limit;
1734 control = msi_dialog_add_control( dialog, rec, szEdit,
1735 WS_BORDER | WS_TABSTOP | ES_AUTOHSCROLL );
1736 control->handler = msi_dialog_edit_handler;
1738 text = MSI_RecordGetString( rec, 10 );
1739 if ( text )
1741 begin = strchrW( text, '{' );
1742 end = strchrW( text, '}' );
1744 if ( begin && end && end > begin &&
1745 begin[0] >= '0' && begin[0] <= '9' &&
1746 end - begin < MAX_NUM_DIGITS)
1748 lstrcpynW( num, begin + 1, end - begin );
1749 limit = atolW( num );
1751 SendMessageW( control->hwnd, EM_SETLIMITTEXT, limit, 0 );
1755 prop = MSI_RecordGetString( rec, 9 );
1756 if( prop )
1757 control->property = strdupW( prop );
1759 val = msi_dup_property( dialog->package->db, control->property );
1760 SetWindowTextW( control->hwnd, val );
1761 msi_free( val );
1762 return ERROR_SUCCESS;
1765 /******************** Masked Edit ********************************************/
1767 #define MASK_MAX_GROUPS 20
1769 struct msi_mask_group
1771 UINT len;
1772 UINT ofs;
1773 WCHAR type;
1774 HWND hwnd;
1777 struct msi_maskedit_info
1779 msi_dialog *dialog;
1780 WNDPROC oldproc;
1781 HWND hwnd;
1782 LPWSTR prop;
1783 UINT num_chars;
1784 UINT num_groups;
1785 struct msi_mask_group group[MASK_MAX_GROUPS];
1788 static BOOL msi_mask_editable( WCHAR type )
1790 switch (type)
1792 case '%':
1793 case '#':
1794 case '&':
1795 case '`':
1796 case '?':
1797 case '^':
1798 return TRUE;
1800 return FALSE;
1803 static void msi_mask_control_change( struct msi_maskedit_info *info )
1805 LPWSTR val;
1806 UINT i, n, r;
1808 val = msi_alloc( (info->num_chars+1)*sizeof(WCHAR) );
1809 for( i=0, n=0; i<info->num_groups; i++ )
1811 if (info->group[i].len == ~0u)
1813 UINT len = SendMessageW( info->group[i].hwnd, WM_GETTEXTLENGTH, 0, 0 );
1814 val = msi_realloc( val, (len + 1) * sizeof(WCHAR) );
1815 GetWindowTextW( info->group[i].hwnd, val, len + 1 );
1817 else
1819 if (info->group[i].len + n > info->num_chars)
1821 ERR("can't fit control %d text into template\n",i);
1822 break;
1824 if (!msi_mask_editable(info->group[i].type))
1826 for(r=0; r<info->group[i].len; r++)
1827 val[n+r] = info->group[i].type;
1828 val[n+r] = 0;
1830 else
1832 r = GetWindowTextW( info->group[i].hwnd, &val[n], info->group[i].len+1 );
1833 if( r != info->group[i].len )
1834 break;
1836 n += r;
1840 TRACE("%d/%d controls were good\n", i, info->num_groups);
1842 if( i == info->num_groups )
1844 TRACE("Set property %s to %s\n", debugstr_w(info->prop), debugstr_w(val));
1845 msi_dialog_set_property( info->dialog->package, info->prop, val );
1846 msi_dialog_evaluate_control_conditions( info->dialog );
1848 msi_free( val );
1851 /* now move to the next control if necessary */
1852 static VOID msi_mask_next_control( struct msi_maskedit_info *info, HWND hWnd )
1854 HWND hWndNext;
1855 UINT len, i;
1857 for( i=0; i<info->num_groups; i++ )
1858 if( info->group[i].hwnd == hWnd )
1859 break;
1861 /* don't move from the last control */
1862 if( i >= (info->num_groups-1) )
1863 return;
1865 len = SendMessageW( hWnd, WM_GETTEXTLENGTH, 0, 0 );
1866 if( len < info->group[i].len )
1867 return;
1869 hWndNext = GetNextDlgTabItem( GetParent( hWnd ), hWnd, FALSE );
1870 SetFocus( hWndNext );
1873 static LRESULT WINAPI
1874 MSIMaskedEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1876 struct msi_maskedit_info *info;
1877 HRESULT r;
1879 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1881 info = GetPropW(hWnd, szButtonData);
1883 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1885 switch( msg )
1887 case WM_COMMAND:
1888 if (HIWORD(wParam) == EN_CHANGE)
1890 msi_mask_control_change( info );
1891 msi_mask_next_control( info, (HWND) lParam );
1893 break;
1894 case WM_NCDESTROY:
1895 msi_free( info->prop );
1896 msi_free( info );
1897 RemovePropW( hWnd, szButtonData );
1898 break;
1901 return r;
1904 /* fish the various bits of the property out and put them in the control */
1905 static void
1906 msi_maskedit_set_text( struct msi_maskedit_info *info, LPCWSTR text )
1908 LPCWSTR p;
1909 UINT i;
1911 p = text;
1912 for( i = 0; i < info->num_groups; i++ )
1914 if( info->group[i].len < strlenW( p ) )
1916 LPWSTR chunk = strdupW( p );
1917 chunk[ info->group[i].len ] = 0;
1918 SetWindowTextW( info->group[i].hwnd, chunk );
1919 msi_free( chunk );
1921 else
1923 SetWindowTextW( info->group[i].hwnd, p );
1924 break;
1926 p += info->group[i].len;
1930 static struct msi_maskedit_info * msi_dialog_parse_groups( LPCWSTR mask )
1932 struct msi_maskedit_info *info;
1933 int i = 0, n = 0, total = 0;
1934 LPCWSTR p;
1936 TRACE("masked control, template %s\n", debugstr_w(mask));
1938 if( !mask )
1939 return NULL;
1941 info = msi_alloc_zero( sizeof *info );
1942 if( !info )
1943 return info;
1945 p = strchrW(mask, '<');
1946 if( p )
1947 p++;
1948 else
1949 p = mask;
1951 for( i=0; i<MASK_MAX_GROUPS; i++ )
1953 /* stop at the end of the string */
1954 if( p[0] == 0 || p[0] == '>' )
1956 if (!total)
1958 /* create a group for the empty mask */
1959 info->group[0].type = '&';
1960 info->group[0].len = ~0u;
1961 i = 1;
1963 break;
1966 /* count the number of the same identifier */
1967 for( n=0; p[n] == p[0]; n++ )
1969 info->group[i].ofs = total;
1970 info->group[i].type = p[0];
1971 if( p[n] == '=' )
1973 n++;
1974 total++; /* an extra not part of the group */
1976 info->group[i].len = n;
1977 total += n;
1978 p += n;
1981 TRACE("%d characters in %d groups\n", total, i );
1982 if( i == MASK_MAX_GROUPS )
1983 ERR("too many groups in PIDTemplate %s\n", debugstr_w(mask));
1985 info->num_chars = total;
1986 info->num_groups = i;
1988 return info;
1991 static void
1992 msi_maskedit_create_children( struct msi_maskedit_info *info, LPCWSTR font )
1994 DWORD width, height, style, wx, ww;
1995 RECT rect;
1996 HWND hwnd;
1997 UINT i;
1999 style = WS_CHILD | WS_BORDER | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL;
2001 GetClientRect( info->hwnd, &rect );
2003 width = rect.right - rect.left;
2004 height = rect.bottom - rect.top;
2006 for( i = 0; i < info->num_groups; i++ )
2008 if (!msi_mask_editable( info->group[i].type ))
2009 continue;
2010 if (info->num_chars)
2012 wx = (info->group[i].ofs * width) / info->num_chars;
2013 ww = (info->group[i].len * width) / info->num_chars;
2015 else
2017 wx = 0;
2018 ww = width;
2020 hwnd = CreateWindowW( szEdit, NULL, style, wx, 0, ww, height,
2021 info->hwnd, NULL, NULL, NULL );
2022 if( !hwnd )
2024 ERR("failed to create mask edit sub window\n");
2025 break;
2028 SendMessageW( hwnd, EM_LIMITTEXT, info->group[i].len, 0 );
2030 msi_dialog_set_font( info->dialog, hwnd,
2031 font?font:info->dialog->default_font );
2032 info->group[i].hwnd = hwnd;
2037 * office 2003 uses "73931<````=````=````=````=`````>@@@@@"
2038 * delphi 7 uses "<????-??????-??????-????>" and "<???-???>"
2039 * filemaker pro 7 uses "<^^^^=^^^^=^^^^=^^^^=^^^^=^^^^=^^^^^>"
2041 static UINT msi_dialog_maskedit_control( msi_dialog *dialog, MSIRECORD *rec )
2043 LPWSTR font_mask, val = NULL, font;
2044 struct msi_maskedit_info *info = NULL;
2045 UINT ret = ERROR_SUCCESS;
2046 msi_control *control;
2047 LPCWSTR prop, mask;
2049 TRACE("\n");
2051 font_mask = msi_get_deformatted_field( dialog->package, rec, 10 );
2052 font = msi_dialog_get_style( font_mask, &mask );
2053 if( !mask )
2055 WARN("mask template is empty\n");
2056 goto end;
2059 info = msi_dialog_parse_groups( mask );
2060 if( !info )
2062 ERR("template %s is invalid\n", debugstr_w(mask));
2063 goto end;
2066 info->dialog = dialog;
2068 control = msi_dialog_add_control( dialog, rec, szStatic,
2069 SS_OWNERDRAW | WS_GROUP | WS_VISIBLE );
2070 if( !control )
2072 ERR("Failed to create maskedit container\n");
2073 ret = ERROR_FUNCTION_FAILED;
2074 goto end;
2076 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
2078 info->hwnd = control->hwnd;
2080 /* subclass the static control */
2081 info->oldproc = (WNDPROC) SetWindowLongPtrW( info->hwnd, GWLP_WNDPROC,
2082 (LONG_PTR)MSIMaskedEdit_WndProc );
2083 SetPropW( control->hwnd, szButtonData, info );
2085 prop = MSI_RecordGetString( rec, 9 );
2086 if( prop )
2087 info->prop = strdupW( prop );
2089 msi_maskedit_create_children( info, font );
2091 if( prop )
2093 val = msi_dup_property( dialog->package->db, prop );
2094 if( val )
2096 msi_maskedit_set_text( info, val );
2097 msi_free( val );
2101 end:
2102 if( ret != ERROR_SUCCESS )
2103 msi_free( info );
2104 msi_free( font_mask );
2105 msi_free( font );
2106 return ret;
2109 /******************** Progress Bar *****************************************/
2111 static UINT msi_dialog_progress_bar( msi_dialog *dialog, MSIRECORD *rec )
2113 msi_control *control;
2114 DWORD attributes, style;
2116 style = WS_VISIBLE;
2117 attributes = MSI_RecordGetInteger( rec, 8 );
2118 if( !(attributes & msidbControlAttributesProgress95) )
2119 style |= PBS_SMOOTH;
2121 control = msi_dialog_add_control( dialog, rec, PROGRESS_CLASSW, style );
2122 if( !control )
2123 return ERROR_FUNCTION_FAILED;
2125 event_subscribe( dialog, szSetProgress, control->name, szProgress );
2126 return ERROR_SUCCESS;
2129 /******************** Path Edit ********************************************/
2131 struct msi_pathedit_info
2133 msi_dialog *dialog;
2134 msi_control *control;
2135 WNDPROC oldproc;
2138 static void msi_dialog_update_pathedit( msi_dialog *dialog, msi_control *control )
2140 LPWSTR prop, path;
2141 BOOL indirect;
2143 if (!control && !(control = msi_dialog_find_control_by_type( dialog, szPathEdit )))
2144 return;
2146 indirect = control->attributes & msidbControlAttributesIndirect;
2147 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2148 path = msi_dialog_dup_property( dialog, prop, TRUE );
2150 SetWindowTextW( control->hwnd, path );
2151 SendMessageW( control->hwnd, EM_SETSEL, 0, -1 );
2153 msi_free( path );
2154 msi_free( prop );
2157 /* FIXME: test when this should fail */
2158 static BOOL msi_dialog_verify_path( LPWSTR path )
2160 if ( !path[0] )
2161 return FALSE;
2163 if ( PathIsRelativeW( path ) )
2164 return FALSE;
2166 return TRUE;
2169 /* returns TRUE if the path is valid, FALSE otherwise */
2170 static BOOL msi_dialog_onkillfocus( msi_dialog *dialog, msi_control *control )
2172 LPWSTR buf, prop;
2173 BOOL indirect;
2174 BOOL valid;
2176 indirect = control->attributes & msidbControlAttributesIndirect;
2177 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2179 buf = msi_get_window_text( control->hwnd );
2181 if ( !msi_dialog_verify_path( buf ) )
2183 /* FIXME: display an error message box */
2184 ERR("Invalid path %s\n", debugstr_w( buf ));
2185 valid = FALSE;
2186 SetFocus( control->hwnd );
2188 else
2190 valid = TRUE;
2191 msi_dialog_set_property( dialog->package, prop, buf );
2194 msi_dialog_update_pathedit( dialog, control );
2196 TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
2197 debugstr_w(prop));
2199 msi_free( buf );
2200 msi_free( prop );
2202 return valid;
2205 static LRESULT WINAPI MSIPathEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2207 struct msi_pathedit_info *info = GetPropW(hWnd, szButtonData);
2208 LRESULT r = 0;
2210 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
2212 if ( msg == WM_KILLFOCUS )
2214 /* if the path is invalid, don't handle this message */
2215 if ( !msi_dialog_onkillfocus( info->dialog, info->control ) )
2216 return 0;
2219 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
2221 if ( msg == WM_NCDESTROY )
2223 msi_free( info );
2224 RemovePropW( hWnd, szButtonData );
2227 return r;
2230 static UINT msi_dialog_pathedit_control( msi_dialog *dialog, MSIRECORD *rec )
2232 struct msi_pathedit_info *info;
2233 msi_control *control;
2234 LPCWSTR prop;
2236 info = msi_alloc( sizeof *info );
2237 if (!info)
2238 return ERROR_FUNCTION_FAILED;
2240 control = msi_dialog_add_control( dialog, rec, szEdit,
2241 WS_BORDER | WS_TABSTOP );
2242 control->attributes = MSI_RecordGetInteger( rec, 8 );
2243 prop = MSI_RecordGetString( rec, 9 );
2244 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2245 control->update = msi_dialog_update_pathedit;
2247 info->dialog = dialog;
2248 info->control = control;
2249 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2250 (LONG_PTR)MSIPathEdit_WndProc );
2251 SetPropW( control->hwnd, szButtonData, info );
2253 msi_dialog_update_pathedit( dialog, control );
2255 return ERROR_SUCCESS;
2258 static UINT msi_dialog_radiogroup_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
2260 if (HIWORD(param) != BN_CLICKED)
2261 return ERROR_SUCCESS;
2263 TRACE("clicked radio button %s, set %s\n", debugstr_w(control->name), debugstr_w(control->property));
2265 msi_dialog_set_property( dialog->package, control->property, control->name );
2267 return msi_dialog_button_handler( dialog, control, param );
2270 /* radio buttons are a bit different from normal controls */
2271 static UINT msi_dialog_create_radiobutton( MSIRECORD *rec, LPVOID param )
2273 radio_button_group_descr *group = param;
2274 msi_dialog *dialog = group->dialog;
2275 msi_control *control;
2276 LPCWSTR prop, text, name;
2277 DWORD style = WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_AUTORADIOBUTTON | BS_MULTILINE;
2279 name = MSI_RecordGetString( rec, 3 );
2280 text = MSI_RecordGetString( rec, 8 );
2282 control = dialog_create_window( dialog, rec, 0, szButton, name, text, style,
2283 group->parent->hwnd );
2284 if (!control)
2285 return ERROR_FUNCTION_FAILED;
2286 control->handler = msi_dialog_radiogroup_handler;
2288 if (group->propval && !strcmpW( control->name, group->propval ))
2289 SendMessageW(control->hwnd, BM_SETCHECK, BST_CHECKED, 0);
2291 prop = MSI_RecordGetString( rec, 1 );
2292 if( prop )
2293 control->property = strdupW( prop );
2295 return ERROR_SUCCESS;
2298 static BOOL CALLBACK msi_radioground_child_enum( HWND hWnd, LPARAM lParam )
2300 EnableWindow( hWnd, lParam );
2301 return TRUE;
2304 static LRESULT WINAPI MSIRadioGroup_WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
2306 WNDPROC oldproc = (WNDPROC)GetPropW( hWnd, szButtonData );
2307 LRESULT r;
2309 TRACE("hWnd %p msg %04x wParam 0x%08lx lParam 0x%08lx\n", hWnd, msg, wParam, lParam);
2311 if (msg == WM_COMMAND) /* Forward notifications to dialog */
2312 SendMessageW( GetParent( hWnd ), msg, wParam, lParam );
2314 r = CallWindowProcW( oldproc, hWnd, msg, wParam, lParam );
2316 /* make sure the radio buttons show as disabled if the parent is disabled */
2317 if (msg == WM_ENABLE)
2318 EnumChildWindows( hWnd, msi_radioground_child_enum, wParam );
2320 return r;
2323 static UINT msi_dialog_radiogroup_control( msi_dialog *dialog, MSIRECORD *rec )
2325 static const WCHAR query[] = {
2326 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2327 'R','a','d','i','o','B','u','t','t','o','n',' ','W','H','E','R','E',' ',
2328 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
2329 UINT r;
2330 LPCWSTR prop;
2331 msi_control *control;
2332 MSIQUERY *view;
2333 radio_button_group_descr group;
2334 MSIPACKAGE *package = dialog->package;
2335 WNDPROC oldproc;
2336 DWORD attr, style = WS_GROUP;
2338 prop = MSI_RecordGetString( rec, 9 );
2340 TRACE("%p %p %s\n", dialog, rec, debugstr_w( prop ));
2342 attr = MSI_RecordGetInteger( rec, 8 );
2343 if (attr & msidbControlAttributesVisible)
2344 style |= WS_VISIBLE;
2345 if (~attr & msidbControlAttributesEnabled)
2346 style |= WS_DISABLED;
2347 if (attr & msidbControlAttributesHasBorder)
2348 style |= BS_GROUPBOX;
2349 else
2350 style |= BS_OWNERDRAW;
2352 /* Create parent group box to hold radio buttons */
2353 control = msi_dialog_add_control( dialog, rec, szButton, style );
2354 if( !control )
2355 return ERROR_FUNCTION_FAILED;
2357 oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2358 (LONG_PTR)MSIRadioGroup_WndProc );
2359 SetPropW(control->hwnd, szButtonData, oldproc);
2360 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
2362 if( prop )
2363 control->property = strdupW( prop );
2365 /* query the Radio Button table for all control in this group */
2366 r = MSI_OpenQuery( package->db, &view, query, prop );
2367 if( r != ERROR_SUCCESS )
2369 ERR("query failed for dialog %s radio group %s\n",
2370 debugstr_w(dialog->name), debugstr_w(prop));
2371 return ERROR_INVALID_PARAMETER;
2374 group.dialog = dialog;
2375 group.parent = control;
2376 group.propval = msi_dup_property( dialog->package->db, control->property );
2378 r = MSI_IterateRecords( view, 0, msi_dialog_create_radiobutton, &group );
2379 msiobj_release( &view->hdr );
2380 msi_free( group.propval );
2381 return r;
2384 static void
2385 msi_seltree_sync_item_state( HWND hwnd, MSIFEATURE *feature, HTREEITEM hItem )
2387 TVITEMW tvi;
2388 DWORD index = feature->ActionRequest;
2390 TRACE("Feature %s -> %d %d %d\n", debugstr_w(feature->Title),
2391 feature->Installed, feature->Action, feature->ActionRequest);
2393 if (index == INSTALLSTATE_UNKNOWN)
2394 index = INSTALLSTATE_ABSENT;
2396 tvi.mask = TVIF_STATE;
2397 tvi.hItem = hItem;
2398 tvi.state = INDEXTOSTATEIMAGEMASK( index );
2399 tvi.stateMask = TVIS_STATEIMAGEMASK;
2401 SendMessageW( hwnd, TVM_SETITEMW, 0, (LPARAM) &tvi );
2404 static UINT
2405 msi_seltree_popup_menu( HWND hwnd, INT x, INT y )
2407 HMENU hMenu;
2408 INT r;
2410 /* create a menu to display */
2411 hMenu = CreatePopupMenu();
2413 /* FIXME: load strings from resources */
2414 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_LOCAL, "Install feature locally");
2415 AppendMenuA( hMenu, MF_ENABLED, USER_INSTALLSTATE_ALL, "Install entire feature");
2416 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ADVERTISED, "Install on demand");
2417 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ABSENT, "Don't install");
2418 r = TrackPopupMenu( hMenu, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RETURNCMD,
2419 x, y, 0, hwnd, NULL );
2420 DestroyMenu( hMenu );
2421 return r;
2424 static void
2425 msi_seltree_update_feature_installstate( HWND hwnd, HTREEITEM hItem,
2426 MSIPACKAGE *package, MSIFEATURE *feature, INSTALLSTATE state )
2428 feature->ActionRequest = state;
2429 msi_seltree_sync_item_state( hwnd, feature, hItem );
2430 ACTION_UpdateComponentStates( package, feature );
2433 static void
2434 msi_seltree_update_siblings_and_children_installstate( HWND hwnd, HTREEITEM curr,
2435 MSIPACKAGE *package, INSTALLSTATE state)
2437 /* update all siblings */
2440 MSIFEATURE *feature;
2441 HTREEITEM child;
2443 feature = msi_seltree_feature_from_item( hwnd, curr );
2444 msi_seltree_update_feature_installstate( hwnd, curr, package, feature, state );
2446 /* update this sibling's children */
2447 child = (HTREEITEM)SendMessageW( hwnd, TVM_GETNEXTITEM, (WPARAM)TVGN_CHILD, (LPARAM)curr );
2448 if (child)
2449 msi_seltree_update_siblings_and_children_installstate( hwnd, child,
2450 package, state );
2452 while ((curr = (HTREEITEM)SendMessageW( hwnd, TVM_GETNEXTITEM, (WPARAM)TVGN_NEXT, (LPARAM)curr )));
2455 static LRESULT
2456 msi_seltree_menu( HWND hwnd, HTREEITEM hItem )
2458 struct msi_selection_tree_info *info;
2459 MSIFEATURE *feature;
2460 MSIPACKAGE *package;
2461 union {
2462 RECT rc;
2463 POINT pt[2];
2464 HTREEITEM hItem;
2465 } u;
2466 UINT r;
2468 info = GetPropW(hwnd, szButtonData);
2469 package = info->dialog->package;
2471 feature = msi_seltree_feature_from_item( hwnd, hItem );
2472 if (!feature)
2474 ERR("item %p feature was NULL\n", hItem);
2475 return 0;
2478 /* get the item's rectangle to put the menu just below it */
2479 u.hItem = hItem;
2480 SendMessageW( hwnd, TVM_GETITEMRECT, 0, (LPARAM) &u.rc );
2481 MapWindowPoints( hwnd, NULL, u.pt, 2 );
2483 r = msi_seltree_popup_menu( hwnd, u.rc.left, u.rc.top );
2485 switch (r)
2487 case USER_INSTALLSTATE_ALL:
2488 r = INSTALLSTATE_LOCAL;
2489 /* fall-through */
2490 case INSTALLSTATE_ADVERTISED:
2491 case INSTALLSTATE_ABSENT:
2493 HTREEITEM child;
2494 child = (HTREEITEM)SendMessageW( hwnd, TVM_GETNEXTITEM, (WPARAM)TVGN_CHILD, (LPARAM)hItem );
2495 if (child)
2496 msi_seltree_update_siblings_and_children_installstate( hwnd, child, package, r );
2498 /* fall-through */
2499 case INSTALLSTATE_LOCAL:
2500 msi_seltree_update_feature_installstate( hwnd, hItem, package, feature, r );
2501 break;
2504 return 0;
2507 static LRESULT WINAPI
2508 MSISelectionTree_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2510 struct msi_selection_tree_info *info;
2511 TVHITTESTINFO tvhti;
2512 HRESULT r;
2514 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
2516 info = GetPropW(hWnd, szButtonData);
2518 switch( msg )
2520 case WM_LBUTTONDOWN:
2521 tvhti.pt.x = (short)LOWORD( lParam );
2522 tvhti.pt.y = (short)HIWORD( lParam );
2523 tvhti.flags = 0;
2524 tvhti.hItem = 0;
2525 CallWindowProcW(info->oldproc, hWnd, TVM_HITTEST, 0, (LPARAM) &tvhti );
2526 if (tvhti.flags & TVHT_ONITEMSTATEICON)
2527 return msi_seltree_menu( hWnd, tvhti.hItem );
2528 break;
2530 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
2532 switch( msg )
2534 case WM_NCDESTROY:
2535 msi_free( info );
2536 RemovePropW( hWnd, szButtonData );
2537 break;
2539 return r;
2542 static void
2543 msi_seltree_add_child_features( MSIPACKAGE *package, HWND hwnd,
2544 LPCWSTR parent, HTREEITEM hParent )
2546 struct msi_selection_tree_info *info = GetPropW( hwnd, szButtonData );
2547 MSIFEATURE *feature;
2548 TVINSERTSTRUCTW tvis;
2549 HTREEITEM hitem, hfirst = NULL;
2551 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
2553 if ( parent && feature->Feature_Parent && strcmpW( parent, feature->Feature_Parent ))
2554 continue;
2555 else if ( parent && !feature->Feature_Parent )
2556 continue;
2557 else if ( !parent && feature->Feature_Parent )
2558 continue;
2560 if ( !feature->Title )
2561 continue;
2563 if ( !feature->Display )
2564 continue;
2566 memset( &tvis, 0, sizeof tvis );
2567 tvis.hParent = hParent;
2568 tvis.hInsertAfter = TVI_LAST;
2569 tvis.u.item.mask = TVIF_TEXT | TVIF_PARAM;
2570 tvis.u.item.pszText = feature->Title;
2571 tvis.u.item.lParam = (LPARAM) feature;
2573 hitem = (HTREEITEM) SendMessageW( hwnd, TVM_INSERTITEMW, 0, (LPARAM) &tvis );
2574 if (!hitem)
2575 continue;
2577 if (!hfirst)
2578 hfirst = hitem;
2580 msi_seltree_sync_item_state( hwnd, feature, hitem );
2581 msi_seltree_add_child_features( package, hwnd,
2582 feature->Feature, hitem );
2584 /* the node is expanded if Display is odd */
2585 if ( feature->Display % 2 != 0 )
2586 SendMessageW( hwnd, TVM_EXPAND, TVE_EXPAND, (LPARAM) hitem );
2589 /* select the first item */
2590 SendMessageW( hwnd, TVM_SELECTITEM, TVGN_CARET | TVGN_DROPHILITE, (LPARAM) hfirst );
2591 info->selected = hfirst;
2594 static void msi_seltree_create_imagelist( HWND hwnd )
2596 const int bm_width = 32, bm_height = 16, bm_count = 3;
2597 const int bm_resource = 0x1001;
2598 HIMAGELIST himl;
2599 int i;
2600 HBITMAP hbmp;
2602 himl = ImageList_Create( bm_width, bm_height, FALSE, 4, 0 );
2603 if (!himl)
2605 ERR("failed to create image list\n");
2606 return;
2609 for (i=0; i<bm_count; i++)
2611 hbmp = LoadBitmapW( msi_hInstance, MAKEINTRESOURCEW(i+bm_resource) );
2612 if (!hbmp)
2614 ERR("failed to load bitmap %d\n", i);
2615 break;
2619 * Add a dummy bitmap at offset zero because the treeview
2620 * can't use it as a state mask (zero means no user state).
2622 if (!i)
2623 ImageList_Add( himl, hbmp, NULL );
2625 ImageList_Add( himl, hbmp, NULL );
2628 SendMessageW( hwnd, TVM_SETIMAGELIST, TVSIL_STATE, (LPARAM)himl );
2631 static UINT msi_dialog_seltree_handler( msi_dialog *dialog,
2632 msi_control *control, WPARAM param )
2634 struct msi_selection_tree_info *info = GetPropW( control->hwnd, szButtonData );
2635 LPNMTREEVIEWW tv = (LPNMTREEVIEWW)param;
2636 MSIRECORD *row, *rec;
2637 MSIFOLDER *folder;
2638 MSIFEATURE *feature;
2639 LPCWSTR dir, title = NULL;
2640 UINT r = ERROR_SUCCESS;
2642 static const WCHAR select[] = {
2643 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2644 '`','F','e','a','t','u','r','e','`',' ','W','H','E','R','E',' ',
2645 '`','T','i','t','l','e','`',' ','=',' ','\'','%','s','\'',0
2648 if (tv->hdr.code != TVN_SELCHANGINGW)
2649 return ERROR_SUCCESS;
2651 info->selected = tv->itemNew.hItem;
2653 if (!(tv->itemNew.mask & TVIF_TEXT))
2655 feature = msi_seltree_feature_from_item( control->hwnd, tv->itemNew.hItem );
2656 if (feature)
2657 title = feature->Title;
2659 else
2660 title = tv->itemNew.pszText;
2662 row = MSI_QueryGetRecord( dialog->package->db, select, title );
2663 if (!row)
2664 return ERROR_FUNCTION_FAILED;
2666 rec = MSI_CreateRecord( 1 );
2668 MSI_RecordSetStringW( rec, 1, MSI_RecordGetString( row, 4 ) );
2669 msi_event_fire( dialog->package, szSelectionDescription, rec );
2671 dir = MSI_RecordGetString( row, 7 );
2672 if (dir)
2674 folder = msi_get_loaded_folder( dialog->package, dir );
2675 if (!folder)
2677 r = ERROR_FUNCTION_FAILED;
2678 goto done;
2680 MSI_RecordSetStringW( rec, 1, folder->ResolvedTarget );
2682 else
2683 MSI_RecordSetStringW( rec, 1, NULL );
2685 msi_event_fire( dialog->package, szSelectionPath, rec );
2687 done:
2688 msiobj_release(&row->hdr);
2689 msiobj_release(&rec->hdr);
2691 return r;
2694 static UINT msi_dialog_selection_tree( msi_dialog *dialog, MSIRECORD *rec )
2696 msi_control *control;
2697 LPCWSTR prop, control_name;
2698 MSIPACKAGE *package = dialog->package;
2699 DWORD style;
2700 struct msi_selection_tree_info *info;
2702 info = msi_alloc( sizeof *info );
2703 if (!info)
2704 return ERROR_FUNCTION_FAILED;
2706 /* create the treeview control */
2707 style = TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT;
2708 style |= WS_GROUP | WS_VSCROLL | WS_TABSTOP;
2709 control = msi_dialog_add_control( dialog, rec, WC_TREEVIEWW, style );
2710 if (!control)
2712 msi_free(info);
2713 return ERROR_FUNCTION_FAILED;
2716 control->handler = msi_dialog_seltree_handler;
2717 control_name = MSI_RecordGetString( rec, 2 );
2718 control->attributes = MSI_RecordGetInteger( rec, 8 );
2719 prop = MSI_RecordGetString( rec, 9 );
2720 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2722 /* subclass */
2723 info->dialog = dialog;
2724 info->hwnd = control->hwnd;
2725 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2726 (LONG_PTR)MSISelectionTree_WndProc );
2727 SetPropW( control->hwnd, szButtonData, info );
2729 event_subscribe( dialog, szSelectionPath, control_name, szProperty );
2731 /* initialize it */
2732 msi_seltree_create_imagelist( control->hwnd );
2733 msi_seltree_add_child_features( package, control->hwnd, NULL, NULL );
2735 return ERROR_SUCCESS;
2738 /******************** Group Box ***************************************/
2740 static UINT msi_dialog_group_box( msi_dialog *dialog, MSIRECORD *rec )
2742 msi_control *control;
2743 DWORD style;
2745 style = BS_GROUPBOX | WS_CHILD | WS_GROUP;
2746 control = msi_dialog_add_control( dialog, rec, WC_BUTTONW, style );
2747 if (!control)
2748 return ERROR_FUNCTION_FAILED;
2750 return ERROR_SUCCESS;
2753 /******************** List Box ***************************************/
2755 struct msi_listbox_info
2757 msi_dialog *dialog;
2758 HWND hwnd;
2759 WNDPROC oldproc;
2760 DWORD num_items;
2761 DWORD addpos_items;
2762 LPWSTR *items;
2765 static LRESULT WINAPI MSIListBox_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2767 struct msi_listbox_info *info;
2768 LRESULT r;
2769 DWORD j;
2771 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
2773 info = GetPropW( hWnd, szButtonData );
2774 if (!info)
2775 return 0;
2777 r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
2779 switch( msg )
2781 case WM_NCDESTROY:
2782 for (j = 0; j < info->num_items; j++)
2783 msi_free( info->items[j] );
2784 msi_free( info->items );
2785 msi_free( info );
2786 RemovePropW( hWnd, szButtonData );
2787 break;
2790 return r;
2793 static UINT msi_listbox_add_item( MSIRECORD *rec, LPVOID param )
2795 struct msi_listbox_info *info = param;
2796 LPCWSTR value, text;
2797 int pos;
2799 value = MSI_RecordGetString( rec, 3 );
2800 text = MSI_RecordGetString( rec, 4 );
2802 info->items[info->addpos_items] = strdupW( value );
2804 pos = SendMessageW( info->hwnd, LB_ADDSTRING, 0, (LPARAM)text );
2805 SendMessageW( info->hwnd, LB_SETITEMDATA, pos, (LPARAM)info->items[info->addpos_items] );
2806 info->addpos_items++;
2807 return ERROR_SUCCESS;
2810 static UINT msi_listbox_add_items( struct msi_listbox_info *info, LPCWSTR property )
2812 static const WCHAR query[] = {
2813 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2814 '`','L','i','s','t','B','o','x','`',' ','W','H','E','R','E',' ',
2815 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',' ',
2816 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','`',0};
2817 MSIQUERY *view;
2818 DWORD count;
2819 UINT r;
2821 r = MSI_OpenQuery( info->dialog->package->db, &view, query, property );
2822 if ( r != ERROR_SUCCESS )
2823 return r;
2825 /* just get the number of records */
2826 count = 0;
2827 r = MSI_IterateRecords( view, &count, NULL, NULL );
2828 if (r != ERROR_SUCCESS)
2830 msiobj_release( &view->hdr );
2831 return r;
2833 info->num_items = count;
2834 info->items = msi_alloc( sizeof(*info->items) * count );
2836 r = MSI_IterateRecords( view, NULL, msi_listbox_add_item, info );
2837 msiobj_release( &view->hdr );
2838 return r;
2841 static UINT msi_dialog_listbox_handler( msi_dialog *dialog,
2842 msi_control *control, WPARAM param )
2844 struct msi_listbox_info *info;
2845 int index;
2846 LPCWSTR value;
2848 if( HIWORD(param) != LBN_SELCHANGE )
2849 return ERROR_SUCCESS;
2851 info = GetPropW( control->hwnd, szButtonData );
2852 index = SendMessageW( control->hwnd, LB_GETCURSEL, 0, 0 );
2853 value = (LPCWSTR) SendMessageW( control->hwnd, LB_GETITEMDATA, index, 0 );
2855 msi_dialog_set_property( info->dialog->package, control->property, value );
2856 msi_dialog_evaluate_control_conditions( info->dialog );
2858 return ERROR_SUCCESS;
2861 static UINT msi_dialog_list_box( msi_dialog *dialog, MSIRECORD *rec )
2863 struct msi_listbox_info *info;
2864 msi_control *control;
2865 DWORD attributes, style;
2866 LPCWSTR prop;
2868 info = msi_alloc( sizeof *info );
2869 if (!info)
2870 return ERROR_FUNCTION_FAILED;
2872 style = WS_TABSTOP | WS_GROUP | WS_CHILD | LBS_NOTIFY | WS_VSCROLL | WS_BORDER;
2873 attributes = MSI_RecordGetInteger( rec, 8 );
2874 if (~attributes & msidbControlAttributesSorted)
2875 style |= LBS_SORT;
2877 control = msi_dialog_add_control( dialog, rec, WC_LISTBOXW, style );
2878 if (!control)
2880 msi_free(info);
2881 return ERROR_FUNCTION_FAILED;
2884 control->handler = msi_dialog_listbox_handler;
2886 prop = MSI_RecordGetString( rec, 9 );
2887 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2889 /* subclass */
2890 info->dialog = dialog;
2891 info->hwnd = control->hwnd;
2892 info->items = NULL;
2893 info->addpos_items = 0;
2894 info->oldproc = (WNDPROC)SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2895 (LONG_PTR)MSIListBox_WndProc );
2896 SetPropW( control->hwnd, szButtonData, info );
2898 if ( control->property )
2899 msi_listbox_add_items( info, control->property );
2901 return ERROR_SUCCESS;
2904 /******************** Directory Combo ***************************************/
2906 static void msi_dialog_update_directory_combo( msi_dialog *dialog, msi_control *control )
2908 LPWSTR prop, path;
2909 BOOL indirect;
2911 if (!control && !(control = msi_dialog_find_control_by_type( dialog, szDirectoryCombo )))
2912 return;
2914 indirect = control->attributes & msidbControlAttributesIndirect;
2915 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2916 path = msi_dialog_dup_property( dialog, prop, TRUE );
2918 PathStripPathW( path );
2919 PathRemoveBackslashW( path );
2921 SendMessageW( control->hwnd, CB_INSERTSTRING, 0, (LPARAM)path );
2922 SendMessageW( control->hwnd, CB_SETCURSEL, 0, 0 );
2924 msi_free( path );
2925 msi_free( prop );
2928 static UINT msi_dialog_directory_combo( msi_dialog *dialog, MSIRECORD *rec )
2930 msi_control *control;
2931 LPCWSTR prop;
2932 DWORD style;
2934 /* FIXME: use CBS_OWNERDRAWFIXED and add owner draw code */
2935 style = CBS_DROPDOWNLIST | CBS_HASSTRINGS | WS_CHILD |
2936 WS_GROUP | WS_TABSTOP | WS_VSCROLL;
2937 control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
2938 if (!control)
2939 return ERROR_FUNCTION_FAILED;
2941 control->attributes = MSI_RecordGetInteger( rec, 8 );
2942 prop = MSI_RecordGetString( rec, 9 );
2943 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2945 msi_dialog_update_directory_combo( dialog, control );
2947 return ERROR_SUCCESS;
2950 /******************** Directory List ***************************************/
2952 static void msi_dialog_update_directory_list( msi_dialog *dialog, msi_control *control )
2954 WCHAR dir_spec[MAX_PATH];
2955 WIN32_FIND_DATAW wfd;
2956 LPWSTR prop, path;
2957 BOOL indirect;
2958 LVITEMW item;
2959 HANDLE file;
2961 static const WCHAR asterisk[] = {'*',0};
2963 if (!control && !(control = msi_dialog_find_control_by_type( dialog, szDirectoryList )))
2964 return;
2966 /* clear the list-view */
2967 SendMessageW( control->hwnd, LVM_DELETEALLITEMS, 0, 0 );
2969 indirect = control->attributes & msidbControlAttributesIndirect;
2970 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2971 path = msi_dialog_dup_property( dialog, prop, TRUE );
2973 lstrcpyW( dir_spec, path );
2974 lstrcatW( dir_spec, asterisk );
2976 file = FindFirstFileW( dir_spec, &wfd );
2977 if ( file == INVALID_HANDLE_VALUE )
2978 return;
2982 if ( wfd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY )
2983 continue;
2985 if ( !strcmpW( wfd.cFileName, szDot ) || !strcmpW( wfd.cFileName, szDotDot ) )
2986 continue;
2988 item.mask = LVIF_TEXT;
2989 item.cchTextMax = MAX_PATH;
2990 item.iItem = 0;
2991 item.iSubItem = 0;
2992 item.pszText = wfd.cFileName;
2994 SendMessageW( control->hwnd, LVM_INSERTITEMW, 0, (LPARAM)&item );
2995 } while ( FindNextFileW( file, &wfd ) );
2997 msi_free( prop );
2998 msi_free( path );
2999 FindClose( file );
3002 static UINT msi_dialog_directorylist_up( msi_dialog *dialog )
3004 msi_control *control;
3005 LPWSTR prop, path, ptr;
3006 BOOL indirect;
3008 control = msi_dialog_find_control_by_type( dialog, szDirectoryList );
3009 indirect = control->attributes & msidbControlAttributesIndirect;
3010 prop = msi_dialog_dup_property( dialog, control->property, indirect );
3011 path = msi_dialog_dup_property( dialog, prop, TRUE );
3013 /* strip off the last directory */
3014 ptr = PathFindFileNameW( path );
3015 if (ptr != path) *(ptr - 1) = '\0';
3016 PathAddBackslashW( path );
3018 msi_dialog_set_property( dialog->package, prop, path );
3020 msi_dialog_update_directory_list( dialog, NULL );
3021 msi_dialog_update_directory_combo( dialog, NULL );
3022 msi_dialog_update_pathedit( dialog, NULL );
3024 msi_free( path );
3025 msi_free( prop );
3027 return ERROR_SUCCESS;
3030 static UINT msi_dialog_dirlist_handler( msi_dialog *dialog,
3031 msi_control *control, WPARAM param )
3033 LPNMHDR nmhdr = (LPNMHDR)param;
3034 WCHAR new_path[MAX_PATH];
3035 WCHAR text[MAX_PATH];
3036 LPWSTR path, prop;
3037 BOOL indirect;
3038 LVITEMW item;
3039 int index;
3041 if (nmhdr->code != LVN_ITEMACTIVATE)
3042 return ERROR_SUCCESS;
3044 index = SendMessageW( control->hwnd, LVM_GETNEXTITEM, -1, LVNI_SELECTED );
3045 if ( index < 0 )
3047 ERR("No list-view item selected!\n");
3048 return ERROR_FUNCTION_FAILED;
3051 item.iSubItem = 0;
3052 item.pszText = text;
3053 item.cchTextMax = MAX_PATH;
3054 SendMessageW( control->hwnd, LVM_GETITEMTEXTW, index, (LPARAM)&item );
3056 indirect = control->attributes & msidbControlAttributesIndirect;
3057 prop = msi_dialog_dup_property( dialog, control->property, indirect );
3058 path = msi_dialog_dup_property( dialog, prop, TRUE );
3060 lstrcpyW( new_path, path );
3061 lstrcatW( new_path, text );
3062 lstrcatW( new_path, szBackSlash );
3064 msi_dialog_set_property( dialog->package, prop, new_path );
3066 msi_dialog_update_directory_list( dialog, NULL );
3067 msi_dialog_update_directory_combo( dialog, NULL );
3068 msi_dialog_update_pathedit( dialog, NULL );
3070 msi_free( prop );
3071 msi_free( path );
3072 return ERROR_SUCCESS;
3075 static UINT msi_dialog_directory_list( msi_dialog *dialog, MSIRECORD *rec )
3077 msi_control *control;
3078 LPCWSTR prop;
3079 DWORD style;
3081 style = LVS_LIST | WS_VSCROLL | LVS_SHAREIMAGELISTS |
3082 LVS_AUTOARRANGE | LVS_SINGLESEL | WS_BORDER |
3083 LVS_SORTASCENDING | WS_CHILD | WS_GROUP | WS_TABSTOP;
3084 control = msi_dialog_add_control( dialog, rec, WC_LISTVIEWW, style );
3085 if (!control)
3086 return ERROR_FUNCTION_FAILED;
3088 control->attributes = MSI_RecordGetInteger( rec, 8 );
3089 control->handler = msi_dialog_dirlist_handler;
3090 prop = MSI_RecordGetString( rec, 9 );
3091 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
3093 /* double click to activate an item in the list */
3094 SendMessageW( control->hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE,
3095 0, LVS_EX_TWOCLICKACTIVATE );
3097 msi_dialog_update_directory_list( dialog, control );
3099 return ERROR_SUCCESS;
3102 /******************** VolumeCost List ***************************************/
3104 static BOOL str_is_number( LPCWSTR str )
3106 int i;
3108 for (i = 0; i < lstrlenW( str ); i++)
3109 if (!isdigitW(str[i]))
3110 return FALSE;
3112 return TRUE;
3115 static const WCHAR column_keys[][80] =
3117 {'V','o','l','u','m','e','C','o','s','t','V','o','l','u','m','e',0},
3118 {'V','o','l','u','m','e','C','o','s','t','S','i','z','e',0},
3119 {'V','o','l','u','m','e','C','o','s','t','A','v','a','i','l','a','b','l','e',0},
3120 {'V','o','l','u','m','e','C','o','s','t','R','e','q','u','i','r','e','d',0},
3121 {'V','o','l','u','m','e','C','o','s','t','D','i','f','f','e','r','e','n','c','e',0}
3124 static void msi_dialog_vcl_add_columns( msi_dialog *dialog, msi_control *control, MSIRECORD *rec )
3126 LPCWSTR text = MSI_RecordGetString( rec, 10 );
3127 LPCWSTR begin = text, end;
3128 WCHAR *num;
3129 LVCOLUMNW lvc;
3130 DWORD count = 0;
3132 static const WCHAR negative[] = {'-',0};
3134 if (!text) return;
3136 while ((begin = strchrW( begin, '{' )) && count < 5)
3138 if (!(end = strchrW( begin, '}' )))
3139 return;
3141 num = msi_alloc( (end-begin+1)*sizeof(WCHAR) );
3142 if (!num)
3143 return;
3145 lstrcpynW( num, begin + 1, end - begin );
3146 begin += end - begin + 1;
3148 /* empty braces or '0' hides the column */
3149 if ( !num[0] || !strcmpW( num, szZero ) )
3151 count++;
3152 msi_free( num );
3153 continue;
3156 /* the width must be a positive number
3157 * if a width is invalid, all remaining columns are hidden
3159 if ( !strncmpW( num, negative, 1 ) || !str_is_number( num ) ) {
3160 msi_free( num );
3161 return;
3164 ZeroMemory( &lvc, sizeof(lvc) );
3165 lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
3166 lvc.cx = atolW( num );
3167 lvc.pszText = msi_dialog_get_uitext( dialog, column_keys[count] );
3169 SendMessageW( control->hwnd, LVM_INSERTCOLUMNW, count++, (LPARAM)&lvc );
3170 msi_free( lvc.pszText );
3171 msi_free( num );
3175 static LONGLONG msi_vcl_get_cost( msi_dialog *dialog )
3177 MSIFEATURE *feature;
3178 INT each_cost;
3179 LONGLONG total_cost = 0;
3181 LIST_FOR_EACH_ENTRY( feature, &dialog->package->features, MSIFEATURE, entry )
3183 if (ERROR_SUCCESS == (MSI_GetFeatureCost(dialog->package, feature,
3184 MSICOSTTREE_SELFONLY, INSTALLSTATE_LOCAL, &each_cost)))
3186 /* each_cost is in 512-byte units */
3187 total_cost += each_cost * 512;
3189 if (ERROR_SUCCESS == (MSI_GetFeatureCost(dialog->package, feature,
3190 MSICOSTTREE_SELFONLY, INSTALLSTATE_ABSENT, &each_cost)))
3192 /* each_cost is in 512-byte units */
3193 total_cost -= each_cost * 512;
3196 return total_cost;
3199 static void msi_dialog_vcl_add_drives( msi_dialog *dialog, msi_control *control )
3201 ULARGE_INTEGER total, free;
3202 LONGLONG difference, cost;
3203 WCHAR size_text[MAX_PATH];
3204 WCHAR cost_text[MAX_PATH];
3205 LPWSTR drives, ptr;
3206 LVITEMW lvitem;
3207 DWORD size;
3208 int i = 0;
3210 cost = msi_vcl_get_cost(dialog);
3211 StrFormatByteSizeW(cost, cost_text, MAX_PATH);
3213 size = GetLogicalDriveStringsW( 0, NULL );
3214 if ( !size ) return;
3216 drives = msi_alloc( (size + 1) * sizeof(WCHAR) );
3217 if ( !drives ) return;
3219 GetLogicalDriveStringsW( size, drives );
3221 ptr = drives;
3222 while (*ptr)
3224 lvitem.mask = LVIF_TEXT;
3225 lvitem.iItem = i;
3226 lvitem.iSubItem = 0;
3227 lvitem.pszText = ptr;
3228 lvitem.cchTextMax = lstrlenW(ptr) + 1;
3229 SendMessageW( control->hwnd, LVM_INSERTITEMW, 0, (LPARAM)&lvitem );
3231 GetDiskFreeSpaceExW(ptr, &free, &total, NULL);
3232 difference = free.QuadPart - cost;
3234 StrFormatByteSizeW(total.QuadPart, size_text, MAX_PATH);
3235 lvitem.iSubItem = 1;
3236 lvitem.pszText = size_text;
3237 lvitem.cchTextMax = lstrlenW(size_text) + 1;
3238 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
3240 StrFormatByteSizeW(free.QuadPart, size_text, MAX_PATH);
3241 lvitem.iSubItem = 2;
3242 lvitem.pszText = size_text;
3243 lvitem.cchTextMax = lstrlenW(size_text) + 1;
3244 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
3246 lvitem.iSubItem = 3;
3247 lvitem.pszText = cost_text;
3248 lvitem.cchTextMax = lstrlenW(cost_text) + 1;
3249 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
3251 StrFormatByteSizeW(difference, size_text, MAX_PATH);
3252 lvitem.iSubItem = 4;
3253 lvitem.pszText = size_text;
3254 lvitem.cchTextMax = lstrlenW(size_text) + 1;
3255 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
3257 ptr += lstrlenW(ptr) + 1;
3258 i++;
3261 msi_free( drives );
3264 static UINT msi_dialog_volumecost_list( msi_dialog *dialog, MSIRECORD *rec )
3266 msi_control *control;
3267 DWORD style;
3269 style = LVS_REPORT | WS_VSCROLL | WS_HSCROLL | LVS_SHAREIMAGELISTS |
3270 LVS_AUTOARRANGE | LVS_SINGLESEL | WS_BORDER |
3271 WS_CHILD | WS_TABSTOP | WS_GROUP;
3272 control = msi_dialog_add_control( dialog, rec, WC_LISTVIEWW, style );
3273 if (!control)
3274 return ERROR_FUNCTION_FAILED;
3276 msi_dialog_vcl_add_columns( dialog, control, rec );
3277 msi_dialog_vcl_add_drives( dialog, control );
3279 return ERROR_SUCCESS;
3282 /******************** VolumeSelect Combo ***************************************/
3284 static UINT msi_dialog_volsel_handler( msi_dialog *dialog,
3285 msi_control *control, WPARAM param )
3287 WCHAR text[MAX_PATH];
3288 LPWSTR prop;
3289 BOOL indirect;
3290 int index;
3292 if (HIWORD(param) != CBN_SELCHANGE)
3293 return ERROR_SUCCESS;
3295 index = SendMessageW( control->hwnd, CB_GETCURSEL, 0, 0 );
3296 if ( index == CB_ERR )
3298 ERR("No ComboBox item selected!\n");
3299 return ERROR_FUNCTION_FAILED;
3302 SendMessageW( control->hwnd, CB_GETLBTEXT, index, (LPARAM)text );
3304 indirect = control->attributes & msidbControlAttributesIndirect;
3305 prop = msi_dialog_dup_property( dialog, control->property, indirect );
3307 msi_dialog_set_property( dialog->package, prop, text );
3309 msi_free( prop );
3310 return ERROR_SUCCESS;
3313 static void msi_dialog_vsc_add_drives( msi_dialog *dialog, msi_control *control )
3315 LPWSTR drives, ptr;
3316 DWORD size;
3318 size = GetLogicalDriveStringsW( 0, NULL );
3319 if ( !size ) return;
3321 drives = msi_alloc( (size + 1) * sizeof(WCHAR) );
3322 if ( !drives ) return;
3324 GetLogicalDriveStringsW( size, drives );
3326 ptr = drives;
3327 while (*ptr)
3329 SendMessageW( control->hwnd, CB_ADDSTRING, 0, (LPARAM)ptr );
3330 ptr += lstrlenW(ptr) + 1;
3333 msi_free( drives );
3336 static UINT msi_dialog_volumeselect_combo( msi_dialog *dialog, MSIRECORD *rec )
3338 msi_control *control;
3339 LPCWSTR prop;
3340 DWORD style;
3342 /* FIXME: CBS_OWNERDRAWFIXED */
3343 style = WS_CHILD | WS_VISIBLE | WS_GROUP | WS_TABSTOP |
3344 CBS_DROPDOWNLIST | CBS_SORT | CBS_HASSTRINGS |
3345 WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
3346 control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
3347 if (!control)
3348 return ERROR_FUNCTION_FAILED;
3350 control->attributes = MSI_RecordGetInteger( rec, 8 );
3351 control->handler = msi_dialog_volsel_handler;
3352 prop = MSI_RecordGetString( rec, 9 );
3353 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
3355 msi_dialog_vsc_add_drives( dialog, control );
3357 return ERROR_SUCCESS;
3360 static UINT msi_dialog_hyperlink_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
3362 static const WCHAR hrefW[] = {'h','r','e','f'};
3363 static const WCHAR openW[] = {'o','p','e','n',0};
3364 int len, len_href = sizeof(hrefW) / sizeof(hrefW[0]);
3365 const WCHAR *p, *q;
3366 WCHAR quote = 0;
3367 LITEM item;
3369 item.mask = LIF_ITEMINDEX | LIF_URL;
3370 item.iLink = 0;
3371 item.szUrl[0] = 0;
3373 SendMessageW( control->hwnd, LM_GETITEM, 0, (LPARAM)&item );
3375 p = item.szUrl;
3376 while (*p && *p != '<') p++;
3377 if (!*p++) return ERROR_SUCCESS;
3378 if (toupperW( *p++ ) != 'A' || !isspaceW( *p++ )) return ERROR_SUCCESS;
3379 while (*p && isspaceW( *p )) p++;
3381 len = strlenW( p );
3382 if (len > len_href && !memicmpW( p, hrefW, len_href ))
3384 p += len_href;
3385 while (*p && isspaceW( *p )) p++;
3386 if (!*p || *p++ != '=') return ERROR_SUCCESS;
3387 while (*p && isspaceW( *p )) p++;
3389 if (*p == '\"' || *p == '\'') quote = *p++;
3390 q = p;
3391 if (quote)
3393 while (*q && *q != quote) q++;
3394 if (*q != quote) return ERROR_SUCCESS;
3396 else
3398 while (*q && *q != '>' && !isspaceW( *q )) q++;
3399 if (!*q) return ERROR_SUCCESS;
3401 item.szUrl[q - item.szUrl] = 0;
3402 ShellExecuteW( NULL, openW, p, NULL, NULL, SW_SHOWNORMAL );
3404 return ERROR_SUCCESS;
3407 static UINT msi_dialog_hyperlink( msi_dialog *dialog, MSIRECORD *rec )
3409 msi_control *control;
3410 DWORD style = WS_CHILD | WS_TABSTOP | WS_GROUP;
3411 const WCHAR *text = MSI_RecordGetString( rec, 10 );
3412 int len = strlenW( text );
3413 LITEM item;
3415 control = msi_dialog_add_control( dialog, rec, WC_LINK, style );
3416 if (!control)
3417 return ERROR_FUNCTION_FAILED;
3419 control->attributes = MSI_RecordGetInteger( rec, 8 );
3420 control->handler = msi_dialog_hyperlink_handler;
3422 item.mask = LIF_ITEMINDEX | LIF_STATE | LIF_URL;
3423 item.iLink = 0;
3424 item.state = LIS_ENABLED;
3425 item.stateMask = LIS_ENABLED;
3426 if (len < L_MAX_URL_LENGTH) strcpyW( item.szUrl, text );
3427 else item.szUrl[0] = 0;
3429 SendMessageW( control->hwnd, LM_SETITEM, 0, (LPARAM)&item );
3431 return ERROR_SUCCESS;
3434 static const struct control_handler msi_dialog_handler[] =
3436 { szText, msi_dialog_text_control },
3437 { szPushButton, msi_dialog_button_control },
3438 { szLine, msi_dialog_line_control },
3439 { szBitmap, msi_dialog_bitmap_control },
3440 { szCheckBox, msi_dialog_checkbox_control },
3441 { szScrollableText, msi_dialog_scrolltext_control },
3442 { szComboBox, msi_dialog_combo_control },
3443 { szEdit, msi_dialog_edit_control },
3444 { szMaskedEdit, msi_dialog_maskedit_control },
3445 { szPathEdit, msi_dialog_pathedit_control },
3446 { szProgressBar, msi_dialog_progress_bar },
3447 { szRadioButtonGroup, msi_dialog_radiogroup_control },
3448 { szIcon, msi_dialog_icon_control },
3449 { szSelectionTree, msi_dialog_selection_tree },
3450 { szGroupBox, msi_dialog_group_box },
3451 { szListBox, msi_dialog_list_box },
3452 { szDirectoryCombo, msi_dialog_directory_combo },
3453 { szDirectoryList, msi_dialog_directory_list },
3454 { szVolumeCostList, msi_dialog_volumecost_list },
3455 { szVolumeSelectCombo, msi_dialog_volumeselect_combo },
3456 { szHyperLink, msi_dialog_hyperlink }
3459 #define NUM_CONTROL_TYPES (sizeof msi_dialog_handler/sizeof msi_dialog_handler[0])
3461 static UINT msi_dialog_create_controls( MSIRECORD *rec, LPVOID param )
3463 msi_dialog *dialog = param;
3464 LPCWSTR control_type;
3465 UINT i;
3467 /* find and call the function that can create this type of control */
3468 control_type = MSI_RecordGetString( rec, 3 );
3469 for( i=0; i<NUM_CONTROL_TYPES; i++ )
3470 if (!strcmpiW( msi_dialog_handler[i].control_type, control_type ))
3471 break;
3472 if( i != NUM_CONTROL_TYPES )
3473 msi_dialog_handler[i].func( dialog, rec );
3474 else
3475 ERR("no handler for element type %s\n", debugstr_w(control_type));
3477 return ERROR_SUCCESS;
3480 static UINT msi_dialog_fill_controls( msi_dialog *dialog )
3482 static const WCHAR query[] = {
3483 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
3484 'C','o','n','t','r','o','l',' ','W','H','E','R','E',' ',
3485 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
3486 UINT r;
3487 MSIQUERY *view;
3488 MSIPACKAGE *package = dialog->package;
3490 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
3492 /* query the Control table for all the elements of the control */
3493 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
3494 if( r != ERROR_SUCCESS )
3496 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
3497 return ERROR_INVALID_PARAMETER;
3500 r = MSI_IterateRecords( view, 0, msi_dialog_create_controls, dialog );
3501 msiobj_release( &view->hdr );
3502 return r;
3505 static UINT msi_dialog_reset( msi_dialog *dialog )
3507 /* FIXME: should restore the original values of any properties we changed */
3508 return msi_dialog_evaluate_control_conditions( dialog );
3511 /* figure out the height of 10 point MS Sans Serif */
3512 static INT msi_dialog_get_sans_serif_height( HWND hwnd )
3514 static const WCHAR szSansSerif[] = {
3515 'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0 };
3516 LOGFONTW lf;
3517 TEXTMETRICW tm;
3518 BOOL r;
3519 LONG height = 0;
3520 HFONT hFont, hOldFont;
3521 HDC hdc;
3523 hdc = GetDC( hwnd );
3524 if (hdc)
3526 memset( &lf, 0, sizeof lf );
3527 lf.lfHeight = MulDiv(12, GetDeviceCaps(hdc, LOGPIXELSY), 72);
3528 strcpyW( lf.lfFaceName, szSansSerif );
3529 hFont = CreateFontIndirectW(&lf);
3530 if (hFont)
3532 hOldFont = SelectObject( hdc, hFont );
3533 r = GetTextMetricsW( hdc, &tm );
3534 if (r)
3535 height = tm.tmHeight;
3536 SelectObject( hdc, hOldFont );
3537 DeleteObject( hFont );
3539 ReleaseDC( hwnd, hdc );
3541 return height;
3544 /* fetch the associated record from the Dialog table */
3545 static MSIRECORD *msi_get_dialog_record( msi_dialog *dialog )
3547 static const WCHAR query[] = {
3548 'S','E','L','E','C','T',' ','*',' ',
3549 'F','R','O','M',' ','D','i','a','l','o','g',' ',
3550 'W','H','E','R','E',' ',
3551 '`','D','i','a','l','o','g','`',' ','=',' ','\'','%','s','\'',0};
3552 MSIPACKAGE *package = dialog->package;
3553 MSIRECORD *rec = NULL;
3555 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
3557 rec = MSI_QueryGetRecord( package->db, query, dialog->name );
3558 if( !rec )
3559 WARN("query failed for dialog %s\n", debugstr_w(dialog->name));
3561 return rec;
3564 static void msi_dialog_adjust_dialog_pos( msi_dialog *dialog, MSIRECORD *rec, LPRECT pos )
3566 static const WCHAR szScreenX[] = {'S','c','r','e','e','n','X',0};
3567 static const WCHAR szScreenY[] = {'S','c','r','e','e','n','Y',0};
3569 UINT xres, yres;
3570 POINT center;
3571 SIZE sz;
3572 LONG style;
3574 center.x = MSI_RecordGetInteger( rec, 2 );
3575 center.y = MSI_RecordGetInteger( rec, 3 );
3577 sz.cx = MSI_RecordGetInteger( rec, 4 );
3578 sz.cy = MSI_RecordGetInteger( rec, 5 );
3580 sz.cx = msi_dialog_scale_unit( dialog, sz.cx );
3581 sz.cy = msi_dialog_scale_unit( dialog, sz.cy );
3583 xres = msi_get_property_int( dialog->package->db, szScreenX, 0 );
3584 yres = msi_get_property_int( dialog->package->db, szScreenY, 0 );
3586 center.x = MulDiv( center.x, xres, 100 );
3587 center.y = MulDiv( center.y, yres, 100 );
3589 /* turn the client pos into the window rectangle */
3590 if (dialog->package->center_x && dialog->package->center_y)
3592 pos->left = dialog->package->center_x - sz.cx / 2.0;
3593 pos->right = pos->left + sz.cx;
3594 pos->top = dialog->package->center_y - sz.cy / 2.0;
3595 pos->bottom = pos->top + sz.cy;
3597 else
3599 pos->left = center.x - sz.cx/2;
3600 pos->right = pos->left + sz.cx;
3601 pos->top = center.y - sz.cy/2;
3602 pos->bottom = pos->top + sz.cy;
3604 /* save the center */
3605 dialog->package->center_x = center.x;
3606 dialog->package->center_y = center.y;
3609 dialog->size.cx = sz.cx;
3610 dialog->size.cy = sz.cy;
3612 TRACE("%s\n", wine_dbgstr_rect(pos));
3614 style = GetWindowLongPtrW( dialog->hwnd, GWL_STYLE );
3615 AdjustWindowRect( pos, style, FALSE );
3618 static void msi_dialog_set_tab_order( msi_dialog *dialog, LPCWSTR first )
3620 struct list tab_chain;
3621 msi_control *control;
3622 HWND prev = HWND_TOP;
3624 list_init( &tab_chain );
3625 if (!(control = msi_dialog_find_control( dialog, first ))) return;
3627 dialog->hWndFocus = control->hwnd;
3628 while (control)
3630 list_remove( &control->entry );
3631 list_add_tail( &tab_chain, &control->entry );
3632 if (!control->tabnext) break;
3633 control = msi_dialog_find_control( dialog, control->tabnext );
3636 LIST_FOR_EACH_ENTRY( control, &tab_chain, msi_control, entry )
3638 SetWindowPos( control->hwnd, prev, 0, 0, 0, 0,
3639 SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOREDRAW |
3640 SWP_NOREPOSITION | SWP_NOSENDCHANGING | SWP_NOSIZE );
3641 prev = control->hwnd;
3644 /* put them back on the main list */
3645 list_move_head( &dialog->controls, &tab_chain );
3648 static LRESULT msi_dialog_oncreate( HWND hwnd, LPCREATESTRUCTW cs )
3650 static const WCHAR df[] = {
3651 'D','e','f','a','u','l','t','U','I','F','o','n','t',0 };
3652 static const WCHAR dfv[] = {
3653 'M','S',' ','S','h','e','l','l',' ','D','l','g',0 };
3654 msi_dialog *dialog = cs->lpCreateParams;
3655 MSIRECORD *rec = NULL;
3656 LPWSTR title = NULL;
3657 RECT pos;
3659 TRACE("%p %p\n", dialog, dialog->package);
3661 dialog->hwnd = hwnd;
3662 SetWindowLongPtrW( hwnd, GWLP_USERDATA, (LONG_PTR) dialog );
3664 rec = msi_get_dialog_record( dialog );
3665 if( !rec )
3667 TRACE("No record found for dialog %s\n", debugstr_w(dialog->name));
3668 return -1;
3671 dialog->scale = msi_dialog_get_sans_serif_height(dialog->hwnd);
3673 msi_dialog_adjust_dialog_pos( dialog, rec, &pos );
3675 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
3677 dialog->default_font = msi_dup_property( dialog->package->db, df );
3678 if (!dialog->default_font)
3680 dialog->default_font = strdupW(dfv);
3681 if (!dialog->default_font)
3683 msiobj_release( &rec->hdr );
3684 return -1;
3688 title = msi_get_deformatted_field( dialog->package, rec, 7 );
3689 SetWindowTextW( hwnd, title );
3690 msi_free( title );
3692 SetWindowPos( hwnd, 0, pos.left, pos.top,
3693 pos.right - pos.left, pos.bottom - pos.top,
3694 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW );
3696 msi_dialog_build_font_list( dialog );
3697 msi_dialog_fill_controls( dialog );
3698 msi_dialog_evaluate_control_conditions( dialog );
3699 msi_dialog_set_tab_order( dialog, MSI_RecordGetString( rec, 8 ) );
3700 msiobj_release( &rec->hdr );
3702 return 0;
3705 static LRESULT msi_dialog_oncommand( msi_dialog *dialog, WPARAM param, HWND hwnd )
3707 msi_control *control = NULL;
3709 TRACE("%p %p %08lx\n", dialog, hwnd, param);
3711 switch (param)
3713 case 1: /* enter */
3714 control = msi_dialog_find_control( dialog, dialog->control_default );
3715 break;
3716 case 2: /* escape */
3717 control = msi_dialog_find_control( dialog, dialog->control_cancel );
3718 break;
3719 default:
3720 control = msi_dialog_find_control_by_hwnd( dialog, hwnd );
3723 if( control )
3725 if( control->handler )
3727 control->handler( dialog, control, param );
3728 msi_dialog_evaluate_control_conditions( dialog );
3732 return 0;
3735 static LRESULT msi_dialog_onnotify( msi_dialog *dialog, LPARAM param )
3737 LPNMHDR nmhdr = (LPNMHDR) param;
3738 msi_control *control = msi_dialog_find_control_by_hwnd( dialog, nmhdr->hwndFrom );
3740 TRACE("%p %p\n", dialog, nmhdr->hwndFrom);
3742 if ( control && control->handler )
3743 control->handler( dialog, control, param );
3745 return 0;
3748 static void dialog_setfocus( msi_dialog *dialog )
3750 HWND hwnd = dialog->hWndFocus;
3752 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, TRUE);
3753 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, FALSE);
3754 SetFocus( hwnd );
3755 dialog->hWndFocus = hwnd;
3758 static LRESULT WINAPI MSIDialog_WndProc( HWND hwnd, UINT msg,
3759 WPARAM wParam, LPARAM lParam )
3761 msi_dialog *dialog = (LPVOID) GetWindowLongPtrW( hwnd, GWLP_USERDATA );
3763 TRACE("0x%04x\n", msg);
3765 switch (msg)
3767 case WM_MOVE:
3768 dialog->package->center_x = LOWORD(lParam) + dialog->size.cx / 2.0;
3769 dialog->package->center_y = HIWORD(lParam) + dialog->size.cy / 2.0;
3770 break;
3772 case WM_CREATE:
3773 return msi_dialog_oncreate( hwnd, (LPCREATESTRUCTW)lParam );
3775 case WM_COMMAND:
3776 return msi_dialog_oncommand( dialog, wParam, (HWND)lParam );
3778 case WM_ACTIVATE:
3779 if( LOWORD(wParam) == WA_INACTIVE )
3780 dialog->hWndFocus = GetFocus();
3781 else
3782 dialog_setfocus( dialog );
3783 return 0;
3785 case WM_SETFOCUS:
3786 dialog_setfocus( dialog );
3787 return 0;
3789 /* bounce back to our subclassed static control */
3790 case WM_CTLCOLORSTATIC:
3791 return SendMessageW( (HWND) lParam, WM_CTLCOLORSTATIC, wParam, lParam );
3793 case WM_DESTROY:
3794 dialog->hwnd = NULL;
3795 return 0;
3796 case WM_NOTIFY:
3797 return msi_dialog_onnotify( dialog, lParam );
3799 return DefWindowProcW(hwnd, msg, wParam, lParam);
3802 static void process_pending_messages( HWND hdlg )
3804 MSG msg;
3806 while (PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
3808 if (hdlg && IsDialogMessageW( hdlg, &msg )) continue;
3809 TranslateMessage( &msg );
3810 DispatchMessageW( &msg );
3814 static UINT dialog_run_message_loop( msi_dialog *dialog )
3816 DWORD style;
3817 HWND hwnd;
3819 if( uiThreadId != GetCurrentThreadId() )
3820 return SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_CREATE, 0, (LPARAM) dialog );
3822 /* create the dialog window, don't show it yet */
3823 style = WS_OVERLAPPED;
3824 if( dialog->attributes & msidbDialogAttributesVisible )
3825 style |= WS_VISIBLE;
3827 hwnd = CreateWindowW( szMsiDialogClass, dialog->name, style,
3828 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
3829 NULL, NULL, NULL, dialog );
3830 if( !hwnd )
3832 ERR("Failed to create dialog %s\n", debugstr_w( dialog->name ));
3833 return ERROR_FUNCTION_FAILED;
3836 ShowWindow( hwnd, SW_SHOW );
3837 /* UpdateWindow( hwnd ); - and causes the transparent static controls not to paint */
3839 if( dialog->attributes & msidbDialogAttributesModal )
3841 while( !dialog->finished )
3843 MsgWaitForMultipleObjects( 0, NULL, 0, INFINITE, QS_ALLINPUT );
3844 process_pending_messages( dialog->hwnd );
3847 else
3848 return ERROR_IO_PENDING;
3850 return ERROR_SUCCESS;
3853 static LRESULT WINAPI MSIHiddenWindowProc( HWND hwnd, UINT msg,
3854 WPARAM wParam, LPARAM lParam )
3856 msi_dialog *dialog = (msi_dialog*) lParam;
3858 TRACE("%d %p\n", msg, dialog);
3860 switch (msg)
3862 case WM_MSI_DIALOG_CREATE:
3863 return dialog_run_message_loop( dialog );
3864 case WM_MSI_DIALOG_DESTROY:
3865 msi_dialog_destroy( dialog );
3866 return 0;
3868 return DefWindowProcW( hwnd, msg, wParam, lParam );
3871 static BOOL dialog_register_class( void )
3873 WNDCLASSW cls;
3875 ZeroMemory( &cls, sizeof cls );
3876 cls.lpfnWndProc = MSIDialog_WndProc;
3877 cls.hInstance = NULL;
3878 cls.hIcon = LoadIconW(0, (LPWSTR)IDI_APPLICATION);
3879 cls.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
3880 cls.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
3881 cls.lpszMenuName = NULL;
3882 cls.lpszClassName = szMsiDialogClass;
3884 if( !RegisterClassW( &cls ) )
3885 return FALSE;
3887 cls.lpfnWndProc = MSIHiddenWindowProc;
3888 cls.lpszClassName = szMsiHiddenWindow;
3890 if( !RegisterClassW( &cls ) )
3891 return FALSE;
3893 uiThreadId = GetCurrentThreadId();
3895 hMsiHiddenWindow = CreateWindowW( szMsiHiddenWindow, NULL, WS_OVERLAPPED,
3896 0, 0, 100, 100, NULL, NULL, NULL, NULL );
3897 if( !hMsiHiddenWindow )
3898 return FALSE;
3900 return TRUE;
3903 static msi_dialog *dialog_create( MSIPACKAGE *package, const WCHAR *name, msi_dialog *parent,
3904 control_event_handler event_handler )
3906 MSIRECORD *rec = NULL;
3907 msi_dialog *dialog;
3909 TRACE("%s\n", debugstr_w(name));
3911 if (!hMsiHiddenWindow) dialog_register_class();
3913 /* allocate the structure for the dialog to use */
3914 dialog = msi_alloc_zero( FIELD_OFFSET( msi_dialog, name[strlenW( name ) + 1] ));
3915 if( !dialog )
3916 return NULL;
3917 strcpyW( dialog->name, name );
3918 dialog->parent = parent;
3919 msiobj_addref( &package->hdr );
3920 dialog->package = package;
3921 dialog->event_handler = event_handler;
3922 dialog->finished = 0;
3923 list_init( &dialog->controls );
3924 list_init( &dialog->fonts );
3926 /* verify that the dialog exists */
3927 rec = msi_get_dialog_record( dialog );
3928 if( !rec )
3930 msiobj_release( &package->hdr );
3931 msi_free( dialog );
3932 return NULL;
3934 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
3935 dialog->control_default = strdupW( MSI_RecordGetString( rec, 9 ) );
3936 dialog->control_cancel = strdupW( MSI_RecordGetString( rec, 10 ) );
3937 msiobj_release( &rec->hdr );
3939 return dialog;
3942 static void msi_dialog_end_dialog( msi_dialog *dialog )
3944 TRACE("%p\n", dialog);
3945 dialog->finished = 1;
3946 PostMessageW(dialog->hwnd, WM_NULL, 0, 0);
3949 void msi_dialog_check_messages( HANDLE handle )
3951 DWORD r;
3953 /* in threads other than the UI thread, block */
3954 if( uiThreadId != GetCurrentThreadId() )
3956 if (!handle) return;
3957 while (MsgWaitForMultipleObjectsEx( 1, &handle, INFINITE, QS_ALLINPUT, 0 ) == WAIT_OBJECT_0 + 1)
3959 MSG msg;
3960 while (PeekMessageW( &msg, NULL, 0, 0, PM_REMOVE ))
3962 TranslateMessage( &msg );
3963 DispatchMessageW( &msg );
3966 return;
3969 /* there are two choices for the UI thread */
3970 while (1)
3972 process_pending_messages( NULL );
3974 if( !handle )
3975 break;
3978 * block here until somebody creates a new dialog or
3979 * the handle we're waiting on becomes ready
3981 r = MsgWaitForMultipleObjects( 1, &handle, 0, INFINITE, QS_ALLINPUT );
3982 if( r == WAIT_OBJECT_0 )
3983 break;
3987 static void dialog_do_preview( msi_dialog *dialog )
3989 TRACE("\n");
3990 dialog->attributes |= msidbDialogAttributesVisible;
3991 dialog->attributes &= ~msidbDialogAttributesModal;
3992 dialog_run_message_loop( dialog );
3995 static void free_subscriber( struct subscriber *sub )
3997 msi_free( sub->event );
3998 msi_free( sub->control );
3999 msi_free( sub->attribute );
4000 msi_free( sub );
4003 static void event_cleanup_subscriptions( MSIPACKAGE *package, const WCHAR *dialog )
4005 struct list *item, *next;
4007 LIST_FOR_EACH_SAFE( item, next, &package->subscriptions )
4009 struct subscriber *sub = LIST_ENTRY( item, struct subscriber, entry );
4011 if (strcmpW( sub->dialog->name, dialog )) continue;
4012 list_remove( &sub->entry );
4013 free_subscriber( sub );
4017 void msi_dialog_destroy( msi_dialog *dialog )
4019 msi_font *font, *next;
4021 if( uiThreadId != GetCurrentThreadId() )
4023 SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_DESTROY, 0, (LPARAM) dialog );
4024 return;
4027 if( dialog->hwnd )
4028 ShowWindow( dialog->hwnd, SW_HIDE );
4030 if( dialog->hwnd )
4031 DestroyWindow( dialog->hwnd );
4033 /* unsubscribe events */
4034 event_cleanup_subscriptions( dialog->package, dialog->name );
4036 /* destroy the list of controls */
4037 while( !list_empty( &dialog->controls ) )
4039 msi_control *t;
4041 t = LIST_ENTRY( list_head( &dialog->controls ),
4042 msi_control, entry );
4043 msi_destroy_control( t );
4046 /* destroy the list of fonts */
4047 LIST_FOR_EACH_ENTRY_SAFE( font, next, &dialog->fonts, msi_font, entry )
4049 list_remove( &font->entry );
4050 DeleteObject( font->hfont );
4051 msi_free( font );
4053 msi_free( dialog->default_font );
4055 msi_free( dialog->control_default );
4056 msi_free( dialog->control_cancel );
4057 msiobj_release( &dialog->package->hdr );
4058 dialog->package = NULL;
4059 msi_free( dialog );
4062 void msi_dialog_unregister_class( void )
4064 DestroyWindow( hMsiHiddenWindow );
4065 hMsiHiddenWindow = NULL;
4066 UnregisterClassW( szMsiDialogClass, NULL );
4067 UnregisterClassW( szMsiHiddenWindow, NULL );
4068 uiThreadId = 0;
4071 void msi_event_cleanup_all_subscriptions( MSIPACKAGE *package )
4073 struct list *item, *next;
4075 LIST_FOR_EACH_SAFE( item, next, &package->subscriptions )
4077 struct subscriber *sub = LIST_ENTRY( item, struct subscriber, entry );
4078 list_remove( &sub->entry );
4079 free_subscriber( sub );
4083 static UINT error_dialog_handler( msi_dialog *dialog, const WCHAR *event, const WCHAR *argument )
4085 static const WCHAR end_dialog[] = {'E','n','d','D','i','a','l','o','g',0};
4086 static const WCHAR error_abort[] = {'E','r','r','o','r','A','b','o','r','t',0};
4087 static const WCHAR error_cancel[] = {'E','r','r','o','r','C','a','n','c','e','l',0};
4088 static const WCHAR error_no[] = {'E','r','r','o','r','N','o',0};
4089 static const WCHAR result_prop[] = {
4090 'M','S','I','E','r','r','o','r','D','i','a','l','o','g','R','e','s','u','l','t',0
4093 if ( strcmpW( event, end_dialog ) )
4094 return ERROR_SUCCESS;
4096 if ( !strcmpW( argument, error_abort ) || !strcmpW( argument, error_cancel ) ||
4097 !strcmpW( argument, error_no ) )
4099 msi_set_property( dialog->package->db, result_prop, error_abort, -1 );
4102 msi_event_cleanup_all_subscriptions( dialog->package );
4103 msi_dialog_end_dialog( dialog );
4105 return ERROR_SUCCESS;
4108 static UINT msi_error_dialog_set_error( MSIPACKAGE *package, LPWSTR error_dialog, LPWSTR error )
4110 MSIRECORD * row;
4112 static const WCHAR update[] =
4113 {'U','P','D','A','T','E',' ','`','C','o','n','t','r','o','l','`',' ',
4114 'S','E','T',' ','`','T','e','x','t','`',' ','=',' ','\'','%','s','\'',' ',
4115 'W','H','E','R','E', ' ','`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
4116 'A','N','D',' ','`','C','o','n','t','r','o','l','`',' ','=',' ',
4117 '\'','E','r','r','o','r','T','e','x','t','\'',0};
4119 row = MSI_QueryGetRecord( package->db, update, error, error_dialog );
4120 if (!row)
4121 return ERROR_FUNCTION_FAILED;
4123 msiobj_release(&row->hdr);
4124 return ERROR_SUCCESS;
4127 UINT msi_spawn_error_dialog( MSIPACKAGE *package, LPWSTR error_dialog, LPWSTR error )
4129 msi_dialog *dialog;
4130 WCHAR result[MAX_PATH];
4131 UINT r = ERROR_SUCCESS;
4132 DWORD size = MAX_PATH;
4133 int res;
4135 static const WCHAR pn_prop[] = {'P','r','o','d','u','c','t','N','a','m','e',0};
4136 static const WCHAR title_fmt[] = {'%','s',' ','W','a','r','n','i','n','g',0};
4137 static const WCHAR error_abort[] = {'E','r','r','o','r','A','b','o','r','t',0};
4138 static const WCHAR result_prop[] = {
4139 'M','S','I','E','r','r','o','r','D','i','a','l','o','g','R','e','s','u','l','t',0
4142 if ((package->ui_level & INSTALLUILEVEL_MASK) == INSTALLUILEVEL_NONE) return ERROR_SUCCESS;
4144 if ( !error_dialog )
4146 LPWSTR product_name = msi_dup_property( package->db, pn_prop );
4147 WCHAR title[MAX_PATH];
4149 sprintfW( title, title_fmt, product_name );
4150 res = MessageBoxW( NULL, error, title, MB_OKCANCEL | MB_ICONWARNING );
4152 msi_free( product_name );
4154 if ( res == IDOK )
4155 return ERROR_SUCCESS;
4156 else
4157 return ERROR_FUNCTION_FAILED;
4160 r = msi_error_dialog_set_error( package, error_dialog, error );
4161 if ( r != ERROR_SUCCESS )
4162 return r;
4164 dialog = dialog_create( package, error_dialog, package->dialog, error_dialog_handler );
4165 if ( !dialog )
4166 return ERROR_FUNCTION_FAILED;
4168 dialog->finished = FALSE;
4169 r = dialog_run_message_loop( dialog );
4170 if ( r != ERROR_SUCCESS )
4171 goto done;
4173 r = msi_get_property( package->db, result_prop, result, &size );
4174 if ( r != ERROR_SUCCESS)
4175 r = ERROR_SUCCESS;
4177 if ( !strcmpW( result, error_abort ) )
4178 r = ERROR_FUNCTION_FAILED;
4180 done:
4181 msi_dialog_destroy( dialog );
4183 return r;
4186 static void MSI_ClosePreview( MSIOBJECTHDR *arg )
4188 MSIPREVIEW *preview = (MSIPREVIEW *)arg;
4189 msiobj_release( &preview->package->hdr );
4192 static MSIPREVIEW *MSI_EnableUIPreview( MSIDATABASE *db )
4194 MSIPREVIEW *preview = NULL;
4195 MSIPACKAGE *package;
4197 package = MSI_CreatePackage( db, NULL );
4198 if (package)
4200 preview = alloc_msiobject( MSIHANDLETYPE_PREVIEW, sizeof(MSIPREVIEW), MSI_ClosePreview );
4201 if (preview)
4203 preview->package = package;
4204 msiobj_addref( &package->hdr );
4206 msiobj_release( &package->hdr );
4208 return preview;
4211 UINT WINAPI MsiEnableUIPreview( MSIHANDLE hdb, MSIHANDLE *phPreview )
4213 MSIDATABASE *db;
4214 MSIPREVIEW *preview;
4215 UINT r = ERROR_FUNCTION_FAILED;
4217 TRACE("%d %p\n", hdb, phPreview);
4219 db = msihandle2msiinfo( hdb, MSIHANDLETYPE_DATABASE );
4220 if (!db)
4222 IWineMsiRemoteDatabase *remote_database;
4224 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( hdb );
4225 if (!remote_database)
4226 return ERROR_INVALID_HANDLE;
4228 *phPreview = 0;
4230 IWineMsiRemoteDatabase_Release( remote_database );
4231 WARN("MsiEnableUIPreview not allowed during a custom action!\n");
4233 return ERROR_FUNCTION_FAILED;
4235 preview = MSI_EnableUIPreview( db );
4236 if (preview)
4238 *phPreview = alloc_msihandle( &preview->hdr );
4239 msiobj_release( &preview->hdr );
4240 r = ERROR_SUCCESS;
4241 if (!*phPreview)
4242 r = ERROR_NOT_ENOUGH_MEMORY;
4244 msiobj_release( &db->hdr );
4245 return r;
4248 static UINT preview_event_handler( msi_dialog *dialog, const WCHAR *event, const WCHAR *argument )
4250 MESSAGE("Preview dialog event '%s' (arg='%s')\n", debugstr_w(event), debugstr_w(argument));
4251 return ERROR_SUCCESS;
4254 static UINT MSI_PreviewDialogW( MSIPREVIEW *preview, LPCWSTR szDialogName )
4256 msi_dialog *dialog = NULL;
4257 UINT r = ERROR_SUCCESS;
4259 if (preview->dialog)
4260 msi_dialog_destroy( preview->dialog );
4262 /* an empty name means we should just destroy the current preview dialog */
4263 if (szDialogName)
4265 dialog = dialog_create( preview->package, szDialogName, NULL, preview_event_handler );
4266 if (dialog)
4267 dialog_do_preview( dialog );
4268 else
4269 r = ERROR_FUNCTION_FAILED;
4271 preview->dialog = dialog;
4272 return r;
4275 UINT WINAPI MsiPreviewDialogW( MSIHANDLE hPreview, LPCWSTR szDialogName )
4277 MSIPREVIEW *preview;
4278 UINT r;
4280 TRACE("%d %s\n", hPreview, debugstr_w(szDialogName));
4282 preview = msihandle2msiinfo( hPreview, MSIHANDLETYPE_PREVIEW );
4283 if (!preview)
4284 return ERROR_INVALID_HANDLE;
4286 r = MSI_PreviewDialogW( preview, szDialogName );
4287 msiobj_release( &preview->hdr );
4288 return r;
4291 UINT WINAPI MsiPreviewDialogA( MSIHANDLE hPreview, LPCSTR szDialogName )
4293 UINT r;
4294 LPWSTR strW = NULL;
4296 TRACE("%d %s\n", hPreview, debugstr_a(szDialogName));
4298 if (szDialogName)
4300 strW = strdupAtoW( szDialogName );
4301 if (!strW)
4302 return ERROR_OUTOFMEMORY;
4304 r = MsiPreviewDialogW( hPreview, strW );
4305 msi_free( strW );
4306 return r;
4309 UINT WINAPI MsiPreviewBillboardW( MSIHANDLE hPreview, LPCWSTR szControlName, LPCWSTR szBillboard )
4311 FIXME("%d %s %s\n", hPreview, debugstr_w(szControlName), debugstr_w(szBillboard));
4312 return ERROR_CALL_NOT_IMPLEMENTED;
4315 UINT WINAPI MsiPreviewBillboardA( MSIHANDLE hPreview, LPCSTR szControlName, LPCSTR szBillboard )
4317 FIXME("%d %s %s\n", hPreview, debugstr_a(szControlName), debugstr_a(szBillboard));
4318 return ERROR_CALL_NOT_IMPLEMENTED;
4321 typedef UINT (*event_handler)( msi_dialog *, const WCHAR * );
4323 struct control_event
4325 const WCHAR *event;
4326 event_handler handler;
4329 static UINT dialog_event_handler( msi_dialog *, const WCHAR *, const WCHAR * );
4331 /* create a dialog box and run it if it's modal */
4332 static UINT event_do_dialog( MSIPACKAGE *package, const WCHAR *name, msi_dialog *parent, BOOL destroy_modeless )
4334 msi_dialog *dialog;
4335 UINT r;
4337 /* create a new dialog */
4338 dialog = dialog_create( package, name, parent, dialog_event_handler );
4339 if (dialog)
4341 /* kill the current modeless dialog */
4342 if (destroy_modeless && package->dialog)
4344 msi_dialog_destroy( package->dialog );
4345 package->dialog = NULL;
4348 /* modeless dialogs return an error message */
4349 r = dialog_run_message_loop( dialog );
4350 if (r == ERROR_SUCCESS)
4351 msi_dialog_destroy( dialog );
4352 else
4353 package->dialog = dialog;
4355 else r = ERROR_FUNCTION_FAILED;
4356 return r;
4359 /* end a modal dialog box */
4360 static UINT event_end_dialog( msi_dialog *dialog, const WCHAR *argument )
4362 static const WCHAR exitW[] = {'E','x','i','t',0};
4363 static const WCHAR retryW[] = {'R','e','t','r','y',0};
4364 static const WCHAR ignoreW[] = {'I','g','n','o','r','e',0};
4365 static const WCHAR returnW[] = {'R','e','t','u','r','n',0};
4367 if (!strcmpW( argument, exitW ))
4368 dialog->package->CurrentInstallState = ERROR_INSTALL_USEREXIT;
4369 else if (!strcmpW( argument, retryW ))
4370 dialog->package->CurrentInstallState = ERROR_INSTALL_SUSPEND;
4371 else if (!strcmpW( argument, ignoreW ))
4372 dialog->package->CurrentInstallState = ERROR_SUCCESS;
4373 else if (!strcmpW( argument, returnW ))
4375 msi_dialog *parent = dialog->parent;
4376 msi_free( dialog->package->next_dialog );
4377 dialog->package->next_dialog = (parent) ? strdupW( parent->name ) : NULL;
4378 dialog->package->CurrentInstallState = ERROR_SUCCESS;
4380 else
4382 ERR("Unknown argument string %s\n", debugstr_w(argument));
4383 dialog->package->CurrentInstallState = ERROR_FUNCTION_FAILED;
4385 event_cleanup_subscriptions( dialog->package, dialog->name );
4386 msi_dialog_end_dialog( dialog );
4387 return ERROR_SUCCESS;
4390 /* transition from one modal dialog to another modal dialog */
4391 static UINT event_new_dialog( msi_dialog *dialog, const WCHAR *argument )
4393 /* store the name of the next dialog, and signal this one to end */
4394 dialog->package->next_dialog = strdupW( argument );
4395 msi_event_cleanup_all_subscriptions( dialog->package );
4396 msi_dialog_end_dialog( dialog );
4397 return ERROR_SUCCESS;
4400 /* create a new child dialog of an existing modal dialog */
4401 static UINT event_spawn_dialog( msi_dialog *dialog, const WCHAR *argument )
4403 /* don't destroy a modeless dialogs that might be our parent */
4404 event_do_dialog( dialog->package, argument, dialog, FALSE );
4405 if (dialog->package->CurrentInstallState != ERROR_SUCCESS)
4406 msi_dialog_end_dialog( dialog );
4407 else
4408 msi_dialog_update_all_controls(dialog);
4410 return ERROR_SUCCESS;
4413 /* creates a dialog that remains up for a period of time based on a condition */
4414 static UINT event_spawn_wait_dialog( msi_dialog *dialog, const WCHAR *argument )
4416 FIXME("doing nothing\n");
4417 return ERROR_SUCCESS;
4420 static UINT event_do_action( msi_dialog *dialog, const WCHAR *argument )
4422 ACTION_PerformAction( dialog->package, argument, SCRIPT_NONE );
4423 return ERROR_SUCCESS;
4426 static UINT event_add_local( msi_dialog *dialog, const WCHAR *argument )
4428 MSIFEATURE *feature;
4430 LIST_FOR_EACH_ENTRY( feature, &dialog->package->features, MSIFEATURE, entry )
4432 if (!strcmpW( argument, feature->Feature ) || !strcmpW( argument, szAll ))
4434 if (feature->ActionRequest != INSTALLSTATE_LOCAL)
4435 msi_set_property( dialog->package->db, szPreselected, szOne, -1 );
4436 MSI_SetFeatureStateW( dialog->package, feature->Feature, INSTALLSTATE_LOCAL );
4439 return ERROR_SUCCESS;
4442 static UINT event_remove( msi_dialog *dialog, const WCHAR *argument )
4444 MSIFEATURE *feature;
4446 LIST_FOR_EACH_ENTRY( feature, &dialog->package->features, MSIFEATURE, entry )
4448 if (!strcmpW( argument, feature->Feature ) || !strcmpW( argument, szAll ))
4450 if (feature->ActionRequest != INSTALLSTATE_ABSENT)
4451 msi_set_property( dialog->package->db, szPreselected, szOne, -1 );
4452 MSI_SetFeatureStateW( dialog->package, feature->Feature, INSTALLSTATE_ABSENT );
4455 return ERROR_SUCCESS;
4458 static UINT event_add_source( msi_dialog *dialog, const WCHAR *argument )
4460 MSIFEATURE *feature;
4462 LIST_FOR_EACH_ENTRY( feature, &dialog->package->features, MSIFEATURE, entry )
4464 if (!strcmpW( argument, feature->Feature ) || !strcmpW( argument, szAll ))
4466 if (feature->ActionRequest != INSTALLSTATE_SOURCE)
4467 msi_set_property( dialog->package->db, szPreselected, szOne, -1 );
4468 MSI_SetFeatureStateW( dialog->package, feature->Feature, INSTALLSTATE_SOURCE );
4471 return ERROR_SUCCESS;
4474 void msi_event_fire( MSIPACKAGE *package, const WCHAR *event, MSIRECORD *rec )
4476 struct subscriber *sub;
4478 TRACE("firing event %s\n", debugstr_w(event));
4480 LIST_FOR_EACH_ENTRY( sub, &package->subscriptions, struct subscriber, entry )
4482 if (strcmpiW( sub->event, event )) continue;
4483 dialog_handle_event( sub->dialog, sub->control, sub->attribute, rec );
4487 static UINT event_set_target_path( msi_dialog *dialog, const WCHAR *argument )
4489 WCHAR *path = msi_dup_property( dialog->package->db, argument );
4490 MSIRECORD *rec = MSI_CreateRecord( 1 );
4491 UINT r = ERROR_SUCCESS;
4493 MSI_RecordSetStringW( rec, 1, path );
4494 msi_event_fire( dialog->package, szSelectionPath, rec );
4495 if (path)
4497 /* failure to set the path halts the executing of control events */
4498 r = MSI_SetTargetPathW( dialog->package, argument, path );
4499 msi_free( path );
4501 msi_free( &rec->hdr );
4502 return r;
4505 static UINT event_reset( msi_dialog *dialog, const WCHAR *argument )
4507 msi_dialog_reset( dialog );
4508 return ERROR_SUCCESS;
4511 /* Return ERROR_SUCCESS if dialog is process and ERROR_FUNCTION_FAILED
4512 * if the given parameter is not a dialog box
4514 UINT ACTION_DialogBox( MSIPACKAGE *package, const WCHAR *dialog )
4516 UINT r;
4518 if (package->next_dialog) ERR("Already got next dialog... ignoring it\n");
4519 package->next_dialog = NULL;
4521 /* Dialogs are chained by filling in the next_dialog member
4522 * of the package structure, then terminating the current dialog.
4523 * The code below sees the next_dialog member set, and runs the
4524 * next dialog.
4525 * We fall out of the loop below if we come across a modeless
4526 * dialog, as it returns ERROR_IO_PENDING when we try to run
4527 * its message loop.
4529 r = event_do_dialog( package, dialog, NULL, TRUE );
4530 while (r == ERROR_SUCCESS && package->next_dialog)
4532 WCHAR *name = package->next_dialog;
4534 package->next_dialog = NULL;
4535 r = event_do_dialog( package, name, NULL, TRUE );
4536 msi_free( name );
4538 if (r == ERROR_IO_PENDING) r = ERROR_SUCCESS;
4539 return r;
4542 static UINT event_set_install_level( msi_dialog *dialog, const WCHAR *argument )
4544 int level = atolW( argument );
4546 TRACE("setting install level to %d\n", level);
4547 return MSI_SetInstallLevel( dialog->package, level );
4550 static UINT event_directory_list_up( msi_dialog *dialog, const WCHAR *argument )
4552 return msi_dialog_directorylist_up( dialog );
4555 static UINT event_reinstall_mode( msi_dialog *dialog, const WCHAR *argument )
4557 return msi_set_property( dialog->package->db, szReinstallMode, argument, -1 );
4560 static UINT event_reinstall( msi_dialog *dialog, const WCHAR *argument )
4562 return msi_set_property( dialog->package->db, szReinstall, argument, -1 );
4565 static UINT event_validate_product_id( msi_dialog *dialog, const WCHAR *argument )
4567 return msi_validate_product_id( dialog->package );
4570 static const WCHAR end_dialogW[] = {'E','n','d','D','i','a','l','o','g',0};
4571 static const WCHAR new_dialogW[] = {'N','e','w','D','i','a','l','o','g',0};
4572 static const WCHAR spawn_dialogW[] = {'S','p','a','w','n','D','i','a','l','o','g',0};
4573 static const WCHAR spawn_wait_dialogW[] = {'S','p','a','w','n','W','a','i','t','D','i','a','l','o','g',0};
4574 static const WCHAR do_actionW[] = {'D','o','A','c','t','i','o','n',0};
4575 static const WCHAR add_localW[] = {'A','d','d','L','o','c','a','l',0};
4576 static const WCHAR removeW[] = {'R','e','m','o','v','e',0};
4577 static const WCHAR add_sourceW[] = {'A','d','d','S','o','u','r','c','e',0};
4578 static const WCHAR set_target_pathW[] = {'S','e','t','T','a','r','g','e','t','P','a','t','h',0};
4579 static const WCHAR resetW[] = {'R','e','s','e','t',0};
4580 static const WCHAR set_install_levelW[] = {'S','e','t','I','n','s','t','a','l','l','L','e','v','e','l',0};
4581 static const WCHAR directory_list_upW[] = {'D','i','r','e','c','t','o','r','y','L','i','s','t','U','p',0};
4582 static const WCHAR selection_browseW[] = {'S','e','l','e','c','t','i','o','n','B','r','o','w','s','e',0};
4583 static const WCHAR reinstall_modeW[] = {'R','e','i','n','s','t','a','l','l','M','o','d','e',0};
4584 static const WCHAR reinstallW[] = {'R','e','i','n','s','t','a','l','l',0};
4585 static const WCHAR validate_product_idW[] = {'V','a','l','i','d','a','t','e','P','r','o','d','u','c','t','I','D',0};
4587 static const struct control_event control_events[] =
4589 { end_dialogW, event_end_dialog },
4590 { new_dialogW, event_new_dialog },
4591 { spawn_dialogW, event_spawn_dialog },
4592 { spawn_wait_dialogW, event_spawn_wait_dialog },
4593 { do_actionW, event_do_action },
4594 { add_localW, event_add_local },
4595 { removeW, event_remove },
4596 { add_sourceW, event_add_source },
4597 { set_target_pathW, event_set_target_path },
4598 { resetW, event_reset },
4599 { set_install_levelW, event_set_install_level },
4600 { directory_list_upW, event_directory_list_up },
4601 { selection_browseW, event_spawn_dialog },
4602 { reinstall_modeW, event_reinstall_mode },
4603 { reinstallW, event_reinstall },
4604 { validate_product_idW, event_validate_product_id },
4605 { NULL, NULL }
4608 static UINT dialog_event_handler( msi_dialog *dialog, const WCHAR *event, const WCHAR *argument )
4610 unsigned int i;
4612 TRACE("handling event %s\n", debugstr_w(event));
4614 if (!event) return ERROR_SUCCESS;
4616 for (i = 0; control_events[i].event; i++)
4618 if (!strcmpW( control_events[i].event, event ))
4619 return control_events[i].handler( dialog, argument );
4621 FIXME("unhandled event %s arg(%s)\n", debugstr_w(event), debugstr_w(argument));
4622 return ERROR_SUCCESS;