kernel32/tests: Add tabular UTF-7 encoding tests.
[wine.git] / dlls / msi / dialog.c
blob438087969dbe9ed01b62127aa4c9c0b3d79eac42
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
24 #define NONAMELESSSTRUCT
26 #include <stdarg.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "winuser.h"
32 #include "winnls.h"
33 #include "msi.h"
34 #include "msipriv.h"
35 #include "msidefs.h"
36 #include "ocidl.h"
37 #include "olectl.h"
38 #include "richedit.h"
39 #include "commctrl.h"
40 #include "winreg.h"
41 #include "shlwapi.h"
42 #include "msiserver.h"
43 #include "shellapi.h"
45 #include "wine/debug.h"
46 #include "wine/unicode.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(msi);
50 extern HINSTANCE msi_hInstance;
52 struct msi_control_tag;
53 typedef struct msi_control_tag msi_control;
54 typedef UINT (*msi_handler)( msi_dialog *, msi_control *, WPARAM );
55 typedef void (*msi_update)( msi_dialog *, msi_control * );
56 typedef UINT (*control_event_handler)( msi_dialog *, const WCHAR *, const WCHAR * );
58 struct msi_control_tag
60 struct list entry;
61 HWND hwnd;
62 msi_handler handler;
63 msi_update update;
64 LPWSTR property;
65 LPWSTR value;
66 HBITMAP hBitmap;
67 HICON hIcon;
68 LPWSTR tabnext;
69 LPWSTR type;
70 HMODULE hDll;
71 float progress_current;
72 float progress_max;
73 BOOL progress_backwards;
74 DWORD attributes;
75 WCHAR name[1];
78 typedef struct msi_font_tag
80 struct list entry;
81 HFONT hfont;
82 COLORREF color;
83 WCHAR name[1];
84 } msi_font;
86 struct msi_dialog_tag
88 MSIPACKAGE *package;
89 msi_dialog *parent;
90 control_event_handler event_handler;
91 BOOL finished;
92 INT scale;
93 DWORD attributes;
94 SIZE size;
95 HWND hwnd;
96 LPWSTR default_font;
97 struct list fonts;
98 struct list controls;
99 HWND hWndFocus;
100 LPWSTR control_default;
101 LPWSTR control_cancel;
102 WCHAR name[1];
105 struct subscriber
107 struct list entry;
108 msi_dialog *dialog;
109 WCHAR *event;
110 WCHAR *control;
111 WCHAR *attribute;
114 typedef UINT (*msi_dialog_control_func)( msi_dialog *dialog, MSIRECORD *rec );
115 struct control_handler
117 LPCWSTR control_type;
118 msi_dialog_control_func func;
121 typedef struct
123 msi_dialog* dialog;
124 msi_control *parent;
125 DWORD attributes;
126 LPWSTR propval;
127 } radio_button_group_descr;
129 static const WCHAR szMsiDialogClass[] = { 'M','s','i','D','i','a','l','o','g','C','l','o','s','e','C','l','a','s','s',0 };
130 static const WCHAR szMsiHiddenWindow[] = { 'M','s','i','H','i','d','d','e','n','W','i','n','d','o','w',0 };
131 static const WCHAR szStatic[] = { 'S','t','a','t','i','c',0 };
132 static const WCHAR szButton[] = { 'B','U','T','T','O','N', 0 };
133 static const WCHAR szButtonData[] = { 'M','S','I','D','A','T','A',0 };
134 static const WCHAR szProgress[] = { 'P','r','o','g','r','e','s','s',0 };
135 static const WCHAR szText[] = { 'T','e','x','t',0 };
136 static const WCHAR szPushButton[] = { 'P','u','s','h','B','u','t','t','o','n',0 };
137 static const WCHAR szLine[] = { 'L','i','n','e',0 };
138 static const WCHAR szBitmap[] = { 'B','i','t','m','a','p',0 };
139 static const WCHAR szCheckBox[] = { 'C','h','e','c','k','B','o','x',0 };
140 static const WCHAR szScrollableText[] = { 'S','c','r','o','l','l','a','b','l','e','T','e','x','t',0 };
141 static const WCHAR szComboBox[] = { 'C','o','m','b','o','B','o','x',0 };
142 static const WCHAR szEdit[] = { 'E','d','i','t',0 };
143 static const WCHAR szMaskedEdit[] = { 'M','a','s','k','e','d','E','d','i','t',0 };
144 static const WCHAR szPathEdit[] = { 'P','a','t','h','E','d','i','t',0 };
145 static const WCHAR szProgressBar[] = { 'P','r','o','g','r','e','s','s','B','a','r',0 };
146 static const WCHAR szSetProgress[] = { 'S','e','t','P','r','o','g','r','e','s','s',0 };
147 static const WCHAR szRadioButtonGroup[] = { 'R','a','d','i','o','B','u','t','t','o','n','G','r','o','u','p',0 };
148 static const WCHAR szIcon[] = { 'I','c','o','n',0 };
149 static const WCHAR szSelectionTree[] = { 'S','e','l','e','c','t','i','o','n','T','r','e','e',0 };
150 static const WCHAR szGroupBox[] = { 'G','r','o','u','p','B','o','x',0 };
151 static const WCHAR szListBox[] = { 'L','i','s','t','B','o','x',0 };
152 static const WCHAR szDirectoryCombo[] = { 'D','i','r','e','c','t','o','r','y','C','o','m','b','o',0 };
153 static const WCHAR szDirectoryList[] = { 'D','i','r','e','c','t','o','r','y','L','i','s','t',0 };
154 static const WCHAR szVolumeCostList[] = { 'V','o','l','u','m','e','C','o','s','t','L','i','s','t',0 };
155 static const WCHAR szVolumeSelectCombo[] = { 'V','o','l','u','m','e','S','e','l','e','c','t','C','o','m','b','o',0 };
156 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};
157 static const WCHAR szSelectionPath[] = {'S','e','l','e','c','t','i','o','n','P','a','t','h',0};
158 static const WCHAR szHyperLink[] = {'H','y','p','e','r','L','i','n','k',0};
160 /* dialog sequencing */
162 #define WM_MSI_DIALOG_CREATE (WM_USER+0x100)
163 #define WM_MSI_DIALOG_DESTROY (WM_USER+0x101)
165 #define USER_INSTALLSTATE_ALL 0x1000
167 static DWORD uiThreadId;
168 static HWND hMsiHiddenWindow;
170 static LPWSTR msi_get_window_text( HWND hwnd )
172 UINT sz, r;
173 LPWSTR buf;
175 sz = 0x20;
176 buf = msi_alloc( sz*sizeof(WCHAR) );
177 while ( buf )
179 r = GetWindowTextW( hwnd, buf, sz );
180 if ( r < (sz - 1) )
181 break;
182 sz *= 2;
183 buf = msi_realloc( buf, sz*sizeof(WCHAR) );
186 return buf;
189 static INT msi_dialog_scale_unit( msi_dialog *dialog, INT val )
191 return MulDiv( val, dialog->scale, 12 );
194 static msi_control *msi_dialog_find_control( msi_dialog *dialog, LPCWSTR name )
196 msi_control *control;
198 if( !name )
199 return NULL;
200 if( !dialog->hwnd )
201 return NULL;
202 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
203 if( !strcmpW( control->name, name ) ) /* FIXME: case sensitive? */
204 return control;
205 return NULL;
208 static msi_control *msi_dialog_find_control_by_type( msi_dialog *dialog, LPCWSTR type )
210 msi_control *control;
212 if( !type )
213 return NULL;
214 if( !dialog->hwnd )
215 return NULL;
216 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
217 if( !strcmpW( control->type, type ) ) /* FIXME: case sensitive? */
218 return control;
219 return NULL;
222 static msi_control *msi_dialog_find_control_by_hwnd( msi_dialog *dialog, HWND hwnd )
224 msi_control *control;
226 if( !dialog->hwnd )
227 return NULL;
228 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
229 if( hwnd == control->hwnd )
230 return control;
231 return NULL;
234 static LPWSTR msi_get_deformatted_field( MSIPACKAGE *package, MSIRECORD *rec, int field )
236 LPCWSTR str = MSI_RecordGetString( rec, field );
237 LPWSTR ret = NULL;
239 if (str)
240 deformat_string( package, str, &ret );
241 return ret;
244 static LPWSTR msi_dialog_dup_property( msi_dialog *dialog, LPCWSTR property, BOOL indirect )
246 LPWSTR prop = NULL;
248 if (!property)
249 return NULL;
251 if (indirect)
252 prop = msi_dup_property( dialog->package->db, property );
254 if (!prop)
255 prop = strdupW( property );
257 return prop;
261 * msi_dialog_get_style
263 * Extract the {\style} string from the front of the text to display and
264 * update the pointer. Only the last style in a list is applied.
266 static LPWSTR msi_dialog_get_style( LPCWSTR p, LPCWSTR *rest )
268 LPWSTR ret;
269 LPCWSTR q, i, first;
270 DWORD len;
272 q = NULL;
273 *rest = p;
274 if( !p )
275 return NULL;
277 while ((first = strchrW( p, '{' )) && (q = strchrW( first + 1, '}' )))
279 p = first + 1;
280 if( *p != '\\' && *p != '&' )
281 return NULL;
283 /* little bit of sanity checking to stop us getting confused with RTF */
284 for( i=++p; i<q; i++ )
285 if( *i == '}' || *i == '\\' )
286 return NULL;
289 if (!q)
290 return NULL;
292 *rest = ++q;
293 len = q - p;
295 ret = msi_alloc( len*sizeof(WCHAR) );
296 if( !ret )
297 return ret;
298 memcpy( ret, p, len*sizeof(WCHAR) );
299 ret[len-1] = 0;
300 return ret;
303 static UINT msi_dialog_add_font( MSIRECORD *rec, LPVOID param )
305 msi_dialog *dialog = param;
306 msi_font *font;
307 LPCWSTR face, name;
308 LOGFONTW lf;
309 INT style;
310 HDC hdc;
312 /* create a font and add it to the list */
313 name = MSI_RecordGetString( rec, 1 );
314 font = msi_alloc( FIELD_OFFSET( msi_font, name[strlenW( name ) + 1] ));
315 strcpyW( font->name, name );
316 list_add_head( &dialog->fonts, &font->entry );
318 font->color = MSI_RecordGetInteger( rec, 4 );
320 memset( &lf, 0, sizeof lf );
321 face = MSI_RecordGetString( rec, 2 );
322 lf.lfHeight = MSI_RecordGetInteger( rec, 3 );
323 style = MSI_RecordGetInteger( rec, 5 );
324 if( style & msidbTextStyleStyleBitsBold )
325 lf.lfWeight = FW_BOLD;
326 if( style & msidbTextStyleStyleBitsItalic )
327 lf.lfItalic = TRUE;
328 if( style & msidbTextStyleStyleBitsUnderline )
329 lf.lfUnderline = TRUE;
330 if( style & msidbTextStyleStyleBitsStrike )
331 lf.lfStrikeOut = TRUE;
332 lstrcpynW( lf.lfFaceName, face, LF_FACESIZE );
334 /* adjust the height */
335 hdc = GetDC( dialog->hwnd );
336 if (hdc)
338 lf.lfHeight = -MulDiv(lf.lfHeight, GetDeviceCaps(hdc, LOGPIXELSY), 72);
339 ReleaseDC( dialog->hwnd, hdc );
342 font->hfont = CreateFontIndirectW( &lf );
344 TRACE("Adding font style %s\n", debugstr_w(font->name) );
346 return ERROR_SUCCESS;
349 static msi_font *msi_dialog_find_font( msi_dialog *dialog, LPCWSTR name )
351 msi_font *font = NULL;
353 LIST_FOR_EACH_ENTRY( font, &dialog->fonts, msi_font, entry )
354 if( !strcmpW( font->name, name ) ) /* FIXME: case sensitive? */
355 break;
357 return font;
360 static UINT msi_dialog_set_font( msi_dialog *dialog, HWND hwnd, LPCWSTR name )
362 msi_font *font;
364 font = msi_dialog_find_font( dialog, name );
365 if( font )
366 SendMessageW( hwnd, WM_SETFONT, (WPARAM) font->hfont, TRUE );
367 else
368 ERR("No font entry for %s\n", debugstr_w(name));
369 return ERROR_SUCCESS;
372 static UINT msi_dialog_build_font_list( msi_dialog *dialog )
374 static const WCHAR query[] = {
375 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
376 '`','T','e','x','t','S','t','y','l','e','`',0};
377 MSIQUERY *view;
378 UINT r;
380 TRACE("dialog %p\n", dialog );
382 r = MSI_OpenQuery( dialog->package->db, &view, query );
383 if( r != ERROR_SUCCESS )
384 return r;
386 r = MSI_IterateRecords( view, NULL, msi_dialog_add_font, dialog );
387 msiobj_release( &view->hdr );
388 return r;
391 static void msi_destroy_control( msi_control *t )
393 list_remove( &t->entry );
394 /* leave dialog->hwnd - destroying parent destroys child windows */
395 msi_free( t->property );
396 msi_free( t->value );
397 if( t->hBitmap )
398 DeleteObject( t->hBitmap );
399 if( t->hIcon )
400 DestroyIcon( t->hIcon );
401 msi_free( t->tabnext );
402 msi_free( t->type );
403 if (t->hDll)
404 FreeLibrary( t->hDll );
405 msi_free( t );
408 static msi_control *dialog_create_window( msi_dialog *dialog, MSIRECORD *rec, DWORD exstyle,
409 const WCHAR *szCls, const WCHAR *name, const WCHAR *text,
410 DWORD style, HWND parent )
412 DWORD x, y, width, height;
413 LPWSTR font = NULL, title_font = NULL;
414 LPCWSTR title = NULL;
415 msi_control *control;
417 style |= WS_CHILD;
419 control = msi_alloc( FIELD_OFFSET( msi_control, name[strlenW( name ) + 1] ));
420 if (!control)
421 return NULL;
423 strcpyW( control->name, name );
424 list_add_tail( &dialog->controls, &control->entry );
425 control->handler = NULL;
426 control->update = NULL;
427 control->property = NULL;
428 control->value = NULL;
429 control->hBitmap = NULL;
430 control->hIcon = NULL;
431 control->hDll = NULL;
432 control->tabnext = strdupW( MSI_RecordGetString( rec, 11) );
433 control->type = strdupW( MSI_RecordGetString( rec, 3 ) );
434 control->progress_current = 0;
435 control->progress_max = 100;
436 control->progress_backwards = FALSE;
438 x = MSI_RecordGetInteger( rec, 4 );
439 y = MSI_RecordGetInteger( rec, 5 );
440 width = MSI_RecordGetInteger( rec, 6 );
441 height = MSI_RecordGetInteger( rec, 7 );
443 x = msi_dialog_scale_unit( dialog, x );
444 y = msi_dialog_scale_unit( dialog, y );
445 width = msi_dialog_scale_unit( dialog, width );
446 height = msi_dialog_scale_unit( dialog, height );
448 if( text )
450 deformat_string( dialog->package, text, &title_font );
451 font = msi_dialog_get_style( title_font, &title );
454 control->hwnd = CreateWindowExW( exstyle, szCls, title, style,
455 x, y, width, height, parent, NULL, NULL, NULL );
457 TRACE("Dialog %s control %s hwnd %p\n",
458 debugstr_w(dialog->name), debugstr_w(text), control->hwnd );
460 msi_dialog_set_font( dialog, control->hwnd,
461 font ? font : dialog->default_font );
463 msi_free( title_font );
464 msi_free( font );
466 return control;
469 static LPWSTR msi_dialog_get_uitext( msi_dialog *dialog, LPCWSTR key )
471 MSIRECORD *rec;
472 LPWSTR text;
474 static const WCHAR query[] = {
475 's','e','l','e','c','t',' ','*',' ',
476 'f','r','o','m',' ','`','U','I','T','e','x','t','`',' ',
477 'w','h','e','r','e',' ','`','K','e','y','`',' ','=',' ','\'','%','s','\'',0
480 rec = MSI_QueryGetRecord( dialog->package->db, query, key );
481 if (!rec) return NULL;
482 text = strdupW( MSI_RecordGetString( rec, 2 ) );
483 msiobj_release( &rec->hdr );
484 return text;
487 static MSIRECORD *msi_get_binary_record( MSIDATABASE *db, LPCWSTR name )
489 static const WCHAR query[] = {
490 's','e','l','e','c','t',' ','*',' ',
491 'f','r','o','m',' ','B','i','n','a','r','y',' ',
492 'w','h','e','r','e',' ',
493 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0
496 return MSI_QueryGetRecord( db, query, name );
499 static LPWSTR msi_create_tmp_path(void)
501 WCHAR tmp[MAX_PATH];
502 LPWSTR path = NULL;
503 DWORD len, r;
505 r = GetTempPathW( MAX_PATH, tmp );
506 if( !r )
507 return path;
508 len = lstrlenW( tmp ) + 20;
509 path = msi_alloc( len * sizeof (WCHAR) );
510 if( path )
512 r = GetTempFileNameW( tmp, szMsi, 0, path );
513 if (!r)
515 msi_free( path );
516 path = NULL;
519 return path;
522 static HANDLE msi_load_image( MSIDATABASE *db, LPCWSTR name, UINT type,
523 UINT cx, UINT cy, UINT flags )
525 MSIRECORD *rec = NULL;
526 HANDLE himage = NULL;
527 LPWSTR tmp;
528 UINT r;
530 TRACE("%p %s %u %u %08x\n", db, debugstr_w(name), cx, cy, flags);
532 tmp = msi_create_tmp_path();
533 if( !tmp )
534 return himage;
536 rec = msi_get_binary_record( db, name );
537 if( rec )
539 r = MSI_RecordStreamToFile( rec, 2, tmp );
540 if( r == ERROR_SUCCESS )
542 himage = LoadImageW( 0, tmp, type, cx, cy, flags );
544 msiobj_release( &rec->hdr );
546 DeleteFileW( tmp );
548 msi_free( tmp );
549 return himage;
552 static HICON msi_load_icon( MSIDATABASE *db, LPCWSTR text, UINT attributes )
554 DWORD cx = 0, cy = 0, flags;
556 flags = LR_LOADFROMFILE | LR_DEFAULTSIZE;
557 if( attributes & msidbControlAttributesFixedSize )
559 flags &= ~LR_DEFAULTSIZE;
560 if( attributes & msidbControlAttributesIconSize16 )
562 cx += 16;
563 cy += 16;
565 if( attributes & msidbControlAttributesIconSize32 )
567 cx += 32;
568 cy += 32;
570 /* msidbControlAttributesIconSize48 handled by above logic */
572 return msi_load_image( db, text, IMAGE_ICON, cx, cy, flags );
575 static void msi_dialog_update_controls( msi_dialog *dialog, LPCWSTR property )
577 msi_control *control;
579 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
581 if ( control->property && !strcmpW( control->property, property ) && control->update )
582 control->update( dialog, control );
586 static void msi_dialog_set_property( MSIPACKAGE *package, LPCWSTR property, LPCWSTR value )
588 UINT r = msi_set_property( package->db, property, value, -1 );
589 if (r == ERROR_SUCCESS && !strcmpW( property, szSourceDir ))
590 msi_reset_folders( package, TRUE );
593 static MSIFEATURE *msi_seltree_feature_from_item( HWND hwnd, HTREEITEM hItem )
595 TVITEMW tvi;
597 /* get the feature from the item */
598 memset( &tvi, 0, sizeof tvi );
599 tvi.hItem = hItem;
600 tvi.mask = TVIF_PARAM | TVIF_HANDLE;
601 SendMessageW( hwnd, TVM_GETITEMW, 0, (LPARAM)&tvi );
602 return (MSIFEATURE *)tvi.lParam;
605 struct msi_selection_tree_info
607 msi_dialog *dialog;
608 HWND hwnd;
609 WNDPROC oldproc;
610 HTREEITEM selected;
613 static MSIFEATURE *msi_seltree_get_selected_feature( msi_control *control )
615 struct msi_selection_tree_info *info = GetPropW( control->hwnd, szButtonData );
616 return msi_seltree_feature_from_item( control->hwnd, info->selected );
619 static void dialog_handle_event( msi_dialog *dialog, const WCHAR *control,
620 const WCHAR *attribute, MSIRECORD *rec )
622 msi_control* ctrl;
624 ctrl = msi_dialog_find_control( dialog, control );
625 if (!ctrl)
626 return;
627 if( !strcmpW( attribute, szText ) )
629 const WCHAR *font_text, *text = NULL;
630 WCHAR *font, *text_fmt = NULL;
632 font_text = MSI_RecordGetString( rec , 1 );
633 font = msi_dialog_get_style( font_text, &text );
634 deformat_string( dialog->package, text, &text_fmt );
635 if (text_fmt) text = text_fmt;
636 else text = szEmpty;
638 SetWindowTextW( ctrl->hwnd, text );
640 msi_free( font );
641 msi_free( text_fmt );
642 msi_dialog_check_messages( NULL );
644 else if( !strcmpW( attribute, szProgress ) )
646 DWORD func, val1, val2, units;
648 func = MSI_RecordGetInteger( rec, 1 );
649 val1 = MSI_RecordGetInteger( rec, 2 );
650 val2 = MSI_RecordGetInteger( rec, 3 );
652 TRACE("progress: func %u val1 %u val2 %u\n", func, val1, val2);
654 units = val1 / 512;
655 switch (func)
657 case 0: /* init */
658 SendMessageW( ctrl->hwnd, PBM_SETRANGE, 0, MAKELPARAM(0,100) );
659 if (val2)
661 ctrl->progress_max = units ? units : 100;
662 ctrl->progress_current = units;
663 ctrl->progress_backwards = TRUE;
664 SendMessageW( ctrl->hwnd, PBM_SETPOS, 100, 0 );
666 else
668 ctrl->progress_max = units ? units : 100;
669 ctrl->progress_current = 0;
670 ctrl->progress_backwards = FALSE;
671 SendMessageW( ctrl->hwnd, PBM_SETPOS, 0, 0 );
673 break;
674 case 1: /* action data increment */
675 if (val2) dialog->package->action_progress_increment = val1;
676 else dialog->package->action_progress_increment = 0;
677 break;
678 case 2: /* move */
679 if (ctrl->progress_backwards)
681 if (units >= ctrl->progress_current) ctrl->progress_current -= units;
682 else ctrl->progress_current = 0;
684 else
686 if (ctrl->progress_current + units < ctrl->progress_max) ctrl->progress_current += units;
687 else ctrl->progress_current = ctrl->progress_max;
689 SendMessageW( ctrl->hwnd, PBM_SETPOS, MulDiv(100, ctrl->progress_current, ctrl->progress_max), 0 );
690 break;
691 case 3: /* add */
692 ctrl->progress_max += units;
693 break;
694 default:
695 FIXME("Unknown progress message %u\n", func);
696 break;
699 else if ( !strcmpW( attribute, szProperty ) )
701 MSIFEATURE *feature = msi_seltree_get_selected_feature( ctrl );
702 if (feature) msi_dialog_set_property( dialog->package, ctrl->property, feature->Directory );
704 else if ( !strcmpW( attribute, szSelectionPath ) )
706 BOOL indirect = ctrl->attributes & msidbControlAttributesIndirect;
707 LPWSTR path = msi_dialog_dup_property( dialog, ctrl->property, indirect );
708 if (!path) return;
709 SetWindowTextW( ctrl->hwnd, path );
710 msi_free(path);
712 else
714 FIXME("Attribute %s not being set\n", debugstr_w(attribute));
715 return;
719 static void event_subscribe( msi_dialog *dialog, const WCHAR *event, const WCHAR *control, const WCHAR *attribute )
721 struct subscriber *sub;
723 TRACE("event %s control %s attribute %s\n", debugstr_w(event), debugstr_w(control), debugstr_w(attribute));
725 LIST_FOR_EACH_ENTRY( sub, &dialog->package->subscriptions, struct subscriber, entry )
727 if (!strcmpiW( sub->event, event ) &&
728 !strcmpiW( sub->control, control ) &&
729 !strcmpiW( sub->attribute, attribute ))
731 TRACE("already subscribed\n");
732 return;
735 if (!(sub = msi_alloc( sizeof(*sub) ))) return;
736 sub->dialog = dialog;
737 sub->event = strdupW( event );
738 sub->control = strdupW( control );
739 sub->attribute = strdupW( attribute );
740 list_add_tail( &dialog->package->subscriptions, &sub->entry );
743 struct dialog_control
745 msi_dialog *dialog;
746 const WCHAR *control;
749 static UINT map_event( MSIRECORD *row, void *param )
751 struct dialog_control *dc = param;
752 const WCHAR *event = MSI_RecordGetString( row, 3 );
753 const WCHAR *attribute = MSI_RecordGetString( row, 4 );
755 event_subscribe( dc->dialog, event, dc->control, attribute );
756 return ERROR_SUCCESS;
759 static void dialog_map_events( msi_dialog *dialog, const WCHAR *control )
761 static const WCHAR queryW[] =
762 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
763 '`','E','v','e','n','t','M','a','p','p','i','n','g','`',' ',
764 'W','H','E','R','E',' ','`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
765 'A','N','D',' ','`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',0};
766 MSIQUERY *view;
767 struct dialog_control dialog_control =
769 dialog,
770 control
773 if (!MSI_OpenQuery( dialog->package->db, &view, queryW, dialog->name, control ))
775 MSI_IterateRecords( view, NULL, map_event, &dialog_control );
776 msiobj_release( &view->hdr );
780 /* everything except radio buttons */
781 static msi_control *msi_dialog_add_control( msi_dialog *dialog,
782 MSIRECORD *rec, LPCWSTR szCls, DWORD style )
784 DWORD attributes;
785 const WCHAR *text = NULL, *name, *control_type;
786 DWORD exstyle = 0;
788 name = MSI_RecordGetString( rec, 2 );
789 control_type = MSI_RecordGetString( rec, 3 );
790 attributes = MSI_RecordGetInteger( rec, 8 );
791 if (strcmpW( control_type, szScrollableText )) text = MSI_RecordGetString( rec, 10 );
793 TRACE("%s, %s, %08x, %s, %08x\n", debugstr_w(szCls), debugstr_w(name),
794 attributes, debugstr_w(text), style);
796 if( attributes & msidbControlAttributesVisible )
797 style |= WS_VISIBLE;
798 if( ~attributes & msidbControlAttributesEnabled )
799 style |= WS_DISABLED;
800 if( attributes & msidbControlAttributesSunken )
801 exstyle |= WS_EX_CLIENTEDGE;
803 dialog_map_events( dialog, name );
805 return dialog_create_window( dialog, rec, exstyle, szCls, name, text, style, dialog->hwnd );
808 struct msi_text_info
810 msi_font *font;
811 WNDPROC oldproc;
812 DWORD attributes;
816 * we don't erase our own background,
817 * so we have to make sure that the parent window redraws first
819 static void msi_text_on_settext( HWND hWnd )
821 HWND hParent;
822 RECT rc;
824 hParent = GetParent( hWnd );
825 GetWindowRect( hWnd, &rc );
826 MapWindowPoints( NULL, hParent, (LPPOINT) &rc, 2 );
827 InvalidateRect( hParent, &rc, TRUE );
830 static LRESULT WINAPI
831 MSIText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
833 struct msi_text_info *info;
834 LRESULT r = 0;
836 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
838 info = GetPropW(hWnd, szButtonData);
840 if( msg == WM_CTLCOLORSTATIC &&
841 ( info->attributes & msidbControlAttributesTransparent ) )
843 SetBkMode( (HDC)wParam, TRANSPARENT );
844 return (LRESULT) GetStockObject(NULL_BRUSH);
847 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
848 if ( info->font )
849 SetTextColor( (HDC)wParam, info->font->color );
851 switch( msg )
853 case WM_SETTEXT:
854 msi_text_on_settext( hWnd );
855 break;
856 case WM_NCDESTROY:
857 msi_free( info );
858 RemovePropW( hWnd, szButtonData );
859 break;
862 return r;
865 static UINT msi_dialog_text_control( msi_dialog *dialog, MSIRECORD *rec )
867 msi_control *control;
868 struct msi_text_info *info;
869 LPCWSTR text, ptr, prop, control_name;
870 LPWSTR font_name;
872 TRACE("%p %p\n", dialog, rec);
874 control = msi_dialog_add_control( dialog, rec, szStatic, SS_LEFT | WS_GROUP );
875 if( !control )
876 return ERROR_FUNCTION_FAILED;
878 info = msi_alloc( sizeof *info );
879 if( !info )
880 return ERROR_SUCCESS;
882 control_name = MSI_RecordGetString( rec, 2 );
883 control->attributes = MSI_RecordGetInteger( rec, 8 );
884 prop = MSI_RecordGetString( rec, 9 );
885 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
887 text = MSI_RecordGetString( rec, 10 );
888 font_name = msi_dialog_get_style( text, &ptr );
889 info->font = ( font_name ) ? msi_dialog_find_font( dialog, font_name ) : NULL;
890 msi_free( font_name );
892 info->attributes = MSI_RecordGetInteger( rec, 8 );
893 if( info->attributes & msidbControlAttributesTransparent )
894 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT );
896 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
897 (LONG_PTR)MSIText_WndProc );
898 SetPropW( control->hwnd, szButtonData, info );
900 event_subscribe( dialog, szSelectionPath, control_name, szSelectionPath );
901 return ERROR_SUCCESS;
904 /* strip any leading text style label from text field */
905 static WCHAR *msi_get_binary_name( MSIPACKAGE *package, MSIRECORD *rec )
907 WCHAR *p, *text;
909 text = msi_get_deformatted_field( package, rec, 10 );
910 if (!text)
911 return NULL;
913 p = text;
914 while (*p && *p != '{') p++;
915 if (!*p++) return text;
917 while (*p && *p != '}') p++;
918 if (!*p++) return text;
920 p = strdupW( p );
921 msi_free( text );
922 return p;
925 static UINT msi_dialog_set_property_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
927 static const WCHAR szNullArg[] = {'{','}',0};
928 LPWSTR p, prop, arg_fmt = NULL;
929 UINT len;
931 len = strlenW( event );
932 prop = msi_alloc( len * sizeof(WCHAR) );
933 strcpyW( prop, &event[1] );
934 p = strchrW( prop, ']' );
935 if (p && (p[1] == 0 || p[1] == ' '))
937 *p = 0;
938 if (strcmpW( szNullArg, arg ))
939 deformat_string( dialog->package, arg, &arg_fmt );
940 msi_dialog_set_property( dialog->package, prop, arg_fmt );
941 msi_dialog_update_controls( dialog, prop );
942 msi_free( arg_fmt );
944 else ERR("Badly formatted property string - what happens?\n");
945 msi_free( prop );
946 return ERROR_SUCCESS;
949 static UINT msi_dialog_send_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
951 LPWSTR event_fmt = NULL, arg_fmt = NULL;
953 TRACE("Sending control event %s %s\n", debugstr_w(event), debugstr_w(arg));
955 deformat_string( dialog->package, event, &event_fmt );
956 deformat_string( dialog->package, arg, &arg_fmt );
958 dialog->event_handler( dialog, event_fmt, arg_fmt );
960 msi_free( event_fmt );
961 msi_free( arg_fmt );
963 return ERROR_SUCCESS;
966 static UINT msi_dialog_control_event( MSIRECORD *rec, LPVOID param )
968 msi_dialog *dialog = param;
969 LPCWSTR condition, event, arg;
970 UINT r;
972 condition = MSI_RecordGetString( rec, 5 );
973 r = MSI_EvaluateConditionW( dialog->package, condition );
974 if (r == MSICONDITION_TRUE || r == MSICONDITION_NONE)
976 event = MSI_RecordGetString( rec, 3 );
977 arg = MSI_RecordGetString( rec, 4 );
978 if (event[0] == '[')
979 msi_dialog_set_property_event( dialog, event, arg );
980 else
981 msi_dialog_send_event( dialog, event, arg );
983 return ERROR_SUCCESS;
986 static UINT msi_dialog_button_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
988 static const WCHAR query[] = {
989 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
990 'C','o','n','t','r','o','l','E','v','e','n','t',' ','W','H','E','R','E',' ',
991 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ','A','N','D',' ',
992 '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',' ',
993 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','i','n','g','`',0};
994 MSIQUERY *view;
995 UINT r;
997 if (HIWORD(param) != BN_CLICKED)
998 return ERROR_SUCCESS;
1000 r = MSI_OpenQuery( dialog->package->db, &view, query, dialog->name, control->name );
1001 if (r != ERROR_SUCCESS)
1003 ERR("query failed\n");
1004 return ERROR_SUCCESS;
1006 r = MSI_IterateRecords( view, 0, msi_dialog_control_event, dialog );
1007 msiobj_release( &view->hdr );
1008 return r;
1011 static UINT msi_dialog_button_control( msi_dialog *dialog, MSIRECORD *rec )
1013 msi_control *control;
1014 UINT attributes, style;
1016 TRACE("%p %p\n", dialog, rec);
1018 style = WS_TABSTOP;
1019 attributes = MSI_RecordGetInteger( rec, 8 );
1020 if( attributes & msidbControlAttributesIcon )
1021 style |= BS_ICON;
1023 control = msi_dialog_add_control( dialog, rec, szButton, style );
1024 if( !control )
1025 return ERROR_FUNCTION_FAILED;
1027 control->handler = msi_dialog_button_handler;
1029 if (attributes & msidbControlAttributesIcon)
1031 /* set the icon */
1032 LPWSTR name = msi_get_binary_name( dialog->package, rec );
1033 control->hIcon = msi_load_icon( dialog->package->db, name, attributes );
1034 if (control->hIcon)
1036 SendMessageW( control->hwnd, BM_SETIMAGE, IMAGE_ICON, (LPARAM) control->hIcon );
1038 else
1039 ERR("Failed to load icon %s\n", debugstr_w(name));
1040 msi_free( name );
1043 return ERROR_SUCCESS;
1046 static LPWSTR msi_get_checkbox_value( msi_dialog *dialog, LPCWSTR prop )
1048 static const WCHAR query[] = {
1049 'S','E','L','E','C','T',' ','*',' ',
1050 'F','R','O','M',' ','`','C','h','e','c','k','B','o','x','`',' ',
1051 'W','H','E','R','E',' ',
1052 '`','P','r','o','p','e','r','t','y','`',' ','=',' ',
1053 '\'','%','s','\'',0
1055 MSIRECORD *rec = NULL;
1056 LPWSTR ret = NULL;
1058 /* find if there is a value associated with the checkbox */
1059 rec = MSI_QueryGetRecord( dialog->package->db, query, prop );
1060 if (!rec)
1061 return ret;
1063 ret = msi_get_deformatted_field( dialog->package, rec, 2 );
1064 if( ret && !ret[0] )
1066 msi_free( ret );
1067 ret = NULL;
1069 msiobj_release( &rec->hdr );
1070 if (ret)
1071 return ret;
1073 ret = msi_dup_property( dialog->package->db, prop );
1074 if( ret && !ret[0] )
1076 msi_free( ret );
1077 ret = NULL;
1080 return ret;
1083 static UINT msi_dialog_get_checkbox_state( msi_dialog *dialog, msi_control *control )
1085 WCHAR state[2] = {0};
1086 DWORD sz = 2;
1088 msi_get_property( dialog->package->db, control->property, state, &sz );
1089 return state[0] ? 1 : 0;
1092 static void msi_dialog_set_checkbox_state( msi_dialog *dialog, msi_control *control, UINT state )
1094 static const WCHAR szState[] = {'1',0};
1095 LPCWSTR val;
1097 /* if uncheck then the property is set to NULL */
1098 if (!state)
1100 msi_dialog_set_property( dialog->package, control->property, NULL );
1101 return;
1104 /* check for a custom state */
1105 if (control->value && control->value[0])
1106 val = control->value;
1107 else
1108 val = szState;
1110 msi_dialog_set_property( dialog->package, control->property, val );
1113 static void msi_dialog_checkbox_sync_state( msi_dialog *dialog, msi_control *control )
1115 UINT state = msi_dialog_get_checkbox_state( dialog, control );
1116 SendMessageW( control->hwnd, BM_SETCHECK, state ? BST_CHECKED : BST_UNCHECKED, 0 );
1119 static UINT msi_dialog_checkbox_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
1121 UINT state;
1123 if (HIWORD(param) != BN_CLICKED)
1124 return ERROR_SUCCESS;
1126 TRACE("clicked checkbox %s, set %s\n", debugstr_w(control->name), debugstr_w(control->property));
1128 state = msi_dialog_get_checkbox_state( dialog, control );
1129 state = state ? 0 : 1;
1130 msi_dialog_set_checkbox_state( dialog, control, state );
1131 msi_dialog_checkbox_sync_state( dialog, control );
1133 return msi_dialog_button_handler( dialog, control, param );
1136 static UINT msi_dialog_checkbox_control( msi_dialog *dialog, MSIRECORD *rec )
1138 msi_control *control;
1139 LPCWSTR prop;
1141 TRACE("%p %p\n", dialog, rec);
1143 control = msi_dialog_add_control( dialog, rec, szButton, BS_CHECKBOX | BS_MULTILINE | WS_TABSTOP );
1144 control->handler = msi_dialog_checkbox_handler;
1145 control->update = msi_dialog_checkbox_sync_state;
1146 prop = MSI_RecordGetString( rec, 9 );
1147 if (prop)
1149 control->property = strdupW( prop );
1150 control->value = msi_get_checkbox_value( dialog, prop );
1151 TRACE("control %s value %s\n", debugstr_w(control->property), debugstr_w(control->value));
1153 msi_dialog_checkbox_sync_state( dialog, control );
1154 return ERROR_SUCCESS;
1157 static UINT msi_dialog_line_control( msi_dialog *dialog, MSIRECORD *rec )
1159 DWORD attributes;
1160 LPCWSTR name;
1161 DWORD style, exstyle = 0;
1162 DWORD x, y, width, height;
1163 msi_control *control;
1165 TRACE("%p %p\n", dialog, rec);
1167 style = WS_CHILD | SS_ETCHEDHORZ | SS_SUNKEN;
1169 name = MSI_RecordGetString( rec, 2 );
1170 attributes = MSI_RecordGetInteger( rec, 8 );
1172 if( attributes & msidbControlAttributesVisible )
1173 style |= WS_VISIBLE;
1174 if( ~attributes & msidbControlAttributesEnabled )
1175 style |= WS_DISABLED;
1176 if( attributes & msidbControlAttributesSunken )
1177 exstyle |= WS_EX_CLIENTEDGE;
1179 dialog_map_events( dialog, name );
1181 control = msi_alloc( FIELD_OFFSET(msi_control, name[strlenW( name ) + 1] ));
1182 if (!control)
1183 return ERROR_OUTOFMEMORY;
1185 strcpyW( control->name, name );
1186 list_add_head( &dialog->controls, &control->entry );
1187 control->handler = NULL;
1188 control->property = NULL;
1189 control->value = NULL;
1190 control->hBitmap = NULL;
1191 control->hIcon = NULL;
1192 control->hDll = NULL;
1193 control->tabnext = strdupW( MSI_RecordGetString( rec, 11) );
1194 control->type = strdupW( MSI_RecordGetString( rec, 3 ) );
1195 control->progress_current = 0;
1196 control->progress_max = 100;
1197 control->progress_backwards = FALSE;
1199 x = MSI_RecordGetInteger( rec, 4 );
1200 y = MSI_RecordGetInteger( rec, 5 );
1201 width = MSI_RecordGetInteger( rec, 6 );
1203 x = msi_dialog_scale_unit( dialog, x );
1204 y = msi_dialog_scale_unit( dialog, y );
1205 width = msi_dialog_scale_unit( dialog, width );
1206 height = 2; /* line is exactly 2 units in height */
1208 control->hwnd = CreateWindowExW( exstyle, szStatic, NULL, style,
1209 x, y, width, height, dialog->hwnd, NULL, NULL, NULL );
1211 TRACE("Dialog %s control %s hwnd %p\n",
1212 debugstr_w(dialog->name), debugstr_w(name), control->hwnd );
1214 return ERROR_SUCCESS;
1217 /******************** Scroll Text ********************************************/
1219 struct msi_scrolltext_info
1221 msi_dialog *dialog;
1222 msi_control *control;
1223 WNDPROC oldproc;
1226 static LRESULT WINAPI
1227 MSIScrollText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1229 struct msi_scrolltext_info *info;
1230 HRESULT r;
1232 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1234 info = GetPropW( hWnd, szButtonData );
1236 r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
1238 switch( msg )
1240 case WM_GETDLGCODE:
1241 return DLGC_WANTARROWS;
1242 case WM_NCDESTROY:
1243 msi_free( info );
1244 RemovePropW( hWnd, szButtonData );
1245 break;
1246 case WM_PAINT:
1247 /* native MSI sets a wait cursor here */
1248 msi_dialog_button_handler( info->dialog, info->control, BN_CLICKED );
1249 break;
1251 return r;
1254 struct msi_streamin_info
1256 LPSTR string;
1257 DWORD offset;
1258 DWORD length;
1261 static DWORD CALLBACK
1262 msi_richedit_stream_in( DWORD_PTR arg, LPBYTE buffer, LONG count, LONG *pcb )
1264 struct msi_streamin_info *info = (struct msi_streamin_info*) arg;
1266 if( (count + info->offset) > info->length )
1267 count = info->length - info->offset;
1268 memcpy( buffer, &info->string[ info->offset ], count );
1269 *pcb = count;
1270 info->offset += count;
1272 TRACE("%d/%d\n", info->offset, info->length);
1274 return 0;
1277 static void msi_scrolltext_add_text( msi_control *control, LPCWSTR text )
1279 struct msi_streamin_info info;
1280 EDITSTREAM es;
1282 info.string = strdupWtoA( text );
1283 info.offset = 0;
1284 info.length = lstrlenA( info.string ) + 1;
1286 es.dwCookie = (DWORD_PTR) &info;
1287 es.dwError = 0;
1288 es.pfnCallback = msi_richedit_stream_in;
1290 SendMessageW( control->hwnd, EM_STREAMIN, SF_RTF, (LPARAM) &es );
1292 msi_free( info.string );
1295 static UINT msi_dialog_scrolltext_control( msi_dialog *dialog, MSIRECORD *rec )
1297 static const WCHAR szRichEdit20W[] = {'R','i','c','h','E','d','i','t','2','0','W',0};
1298 struct msi_scrolltext_info *info;
1299 msi_control *control;
1300 HMODULE hRichedit;
1301 LPCWSTR text;
1302 DWORD style;
1304 info = msi_alloc( sizeof *info );
1305 if (!info)
1306 return ERROR_FUNCTION_FAILED;
1308 hRichedit = LoadLibraryA("riched20");
1310 style = WS_BORDER | ES_MULTILINE | WS_VSCROLL |
1311 ES_READONLY | ES_AUTOVSCROLL | WS_TABSTOP;
1312 control = msi_dialog_add_control( dialog, rec, szRichEdit20W, style );
1313 if (!control)
1315 FreeLibrary( hRichedit );
1316 msi_free( info );
1317 return ERROR_FUNCTION_FAILED;
1320 control->hDll = hRichedit;
1322 info->dialog = dialog;
1323 info->control = control;
1325 /* subclass the static control */
1326 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1327 (LONG_PTR)MSIScrollText_WndProc );
1328 SetPropW( control->hwnd, szButtonData, info );
1330 /* add the text into the richedit */
1331 text = MSI_RecordGetString( rec, 10 );
1332 if (text)
1333 msi_scrolltext_add_text( control, text );
1335 return ERROR_SUCCESS;
1338 static HBITMAP msi_load_picture( MSIDATABASE *db, LPCWSTR name,
1339 INT cx, INT cy, DWORD flags )
1341 HBITMAP hOleBitmap = 0, hBitmap = 0, hOldSrcBitmap, hOldDestBitmap;
1342 MSIRECORD *rec = NULL;
1343 IStream *stm = NULL;
1344 IPicture *pic = NULL;
1345 HDC srcdc, destdc;
1346 BITMAP bm;
1347 UINT r;
1349 rec = msi_get_binary_record( db, name );
1350 if( !rec )
1351 goto end;
1353 r = MSI_RecordGetIStream( rec, 2, &stm );
1354 msiobj_release( &rec->hdr );
1355 if( r != ERROR_SUCCESS )
1356 goto end;
1358 r = OleLoadPicture( stm, 0, TRUE, &IID_IPicture, (LPVOID*) &pic );
1359 IStream_Release( stm );
1360 if( FAILED( r ) )
1362 ERR("failed to load picture\n");
1363 goto end;
1366 r = IPicture_get_Handle( pic, (OLE_HANDLE*) &hOleBitmap );
1367 if( FAILED( r ) )
1369 ERR("failed to get bitmap handle\n");
1370 goto end;
1373 /* make the bitmap the desired size */
1374 r = GetObjectW( hOleBitmap, sizeof bm, &bm );
1375 if (r != sizeof bm )
1377 ERR("failed to get bitmap size\n");
1378 goto end;
1381 if (flags & LR_DEFAULTSIZE)
1383 cx = bm.bmWidth;
1384 cy = bm.bmHeight;
1387 srcdc = CreateCompatibleDC( NULL );
1388 hOldSrcBitmap = SelectObject( srcdc, hOleBitmap );
1389 destdc = CreateCompatibleDC( NULL );
1390 hBitmap = CreateCompatibleBitmap( srcdc, cx, cy );
1391 hOldDestBitmap = SelectObject( destdc, hBitmap );
1392 StretchBlt( destdc, 0, 0, cx, cy,
1393 srcdc, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
1394 SelectObject( srcdc, hOldSrcBitmap );
1395 SelectObject( destdc, hOldDestBitmap );
1396 DeleteDC( srcdc );
1397 DeleteDC( destdc );
1399 end:
1400 if ( pic )
1401 IPicture_Release( pic );
1402 return hBitmap;
1405 static UINT msi_dialog_bitmap_control( msi_dialog *dialog, MSIRECORD *rec )
1407 UINT cx, cy, flags, style, attributes;
1408 msi_control *control;
1409 LPWSTR name;
1411 flags = LR_LOADFROMFILE;
1412 style = SS_BITMAP | SS_LEFT | WS_GROUP;
1414 attributes = MSI_RecordGetInteger( rec, 8 );
1415 if( attributes & msidbControlAttributesFixedSize )
1417 flags |= LR_DEFAULTSIZE;
1418 style |= SS_CENTERIMAGE;
1421 control = msi_dialog_add_control( dialog, rec, szStatic, style );
1422 cx = MSI_RecordGetInteger( rec, 6 );
1423 cy = MSI_RecordGetInteger( rec, 7 );
1424 cx = msi_dialog_scale_unit( dialog, cx );
1425 cy = msi_dialog_scale_unit( dialog, cy );
1427 name = msi_get_binary_name( dialog->package, rec );
1428 control->hBitmap = msi_load_picture( dialog->package->db, name, cx, cy, flags );
1429 if( control->hBitmap )
1430 SendMessageW( control->hwnd, STM_SETIMAGE,
1431 IMAGE_BITMAP, (LPARAM) control->hBitmap );
1432 else
1433 ERR("Failed to load bitmap %s\n", debugstr_w(name));
1435 msi_free( name );
1437 return ERROR_SUCCESS;
1440 static UINT msi_dialog_icon_control( msi_dialog *dialog, MSIRECORD *rec )
1442 msi_control *control;
1443 DWORD attributes;
1444 LPWSTR name;
1446 TRACE("\n");
1448 control = msi_dialog_add_control( dialog, rec, szStatic,
1449 SS_ICON | SS_CENTERIMAGE | WS_GROUP );
1451 attributes = MSI_RecordGetInteger( rec, 8 );
1452 name = msi_get_binary_name( dialog->package, rec );
1453 control->hIcon = msi_load_icon( dialog->package->db, name, attributes );
1454 if( control->hIcon )
1455 SendMessageW( control->hwnd, STM_SETICON, (WPARAM) control->hIcon, 0 );
1456 else
1457 ERR("Failed to load bitmap %s\n", debugstr_w(name));
1458 msi_free( name );
1459 return ERROR_SUCCESS;
1462 /******************** Combo Box ***************************************/
1464 struct msi_combobox_info
1466 msi_dialog *dialog;
1467 HWND hwnd;
1468 WNDPROC oldproc;
1469 DWORD num_items;
1470 DWORD addpos_items;
1471 LPWSTR *items;
1474 static LRESULT WINAPI MSIComboBox_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1476 struct msi_combobox_info *info;
1477 LRESULT r;
1478 DWORD j;
1480 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1482 info = GetPropW( hWnd, szButtonData );
1483 if (!info)
1484 return 0;
1486 r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
1488 switch (msg)
1490 case WM_NCDESTROY:
1491 for (j = 0; j < info->num_items; j++)
1492 msi_free( info->items[j] );
1493 msi_free( info->items );
1494 msi_free( info );
1495 RemovePropW( hWnd, szButtonData );
1496 break;
1499 return r;
1502 static UINT msi_combobox_add_item( MSIRECORD *rec, LPVOID param )
1504 struct msi_combobox_info *info = param;
1505 LPCWSTR value, text;
1506 int pos;
1508 value = MSI_RecordGetString( rec, 3 );
1509 text = MSI_RecordGetString( rec, 4 );
1511 info->items[info->addpos_items] = strdupW( value );
1513 pos = SendMessageW( info->hwnd, CB_ADDSTRING, 0, (LPARAM)text );
1514 SendMessageW( info->hwnd, CB_SETITEMDATA, pos, (LPARAM)info->items[info->addpos_items] );
1515 info->addpos_items++;
1517 return ERROR_SUCCESS;
1520 static UINT msi_combobox_add_items( struct msi_combobox_info *info, LPCWSTR property )
1522 static const WCHAR query[] = {
1523 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1524 '`','C','o','m','b','o','B','o','x','`',' ','W','H','E','R','E',' ',
1525 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',' ',
1526 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','`',0};
1527 MSIQUERY *view;
1528 DWORD count;
1529 UINT r;
1531 r = MSI_OpenQuery( info->dialog->package->db, &view, query, property );
1532 if (r != ERROR_SUCCESS)
1533 return r;
1535 /* just get the number of records */
1536 count = 0;
1537 r = MSI_IterateRecords( view, &count, NULL, NULL );
1538 if (r != ERROR_SUCCESS)
1540 msiobj_release( &view->hdr );
1541 return r;
1543 info->num_items = count;
1544 info->items = msi_alloc( sizeof(*info->items) * count );
1546 r = MSI_IterateRecords( view, NULL, msi_combobox_add_item, info );
1547 msiobj_release( &view->hdr );
1548 return r;
1551 static UINT msi_dialog_set_control_condition( MSIRECORD *rec, LPVOID param )
1553 static const WCHAR szHide[] = {'H','i','d','e',0};
1554 static const WCHAR szShow[] = {'S','h','o','w',0};
1555 static const WCHAR szDisable[] = {'D','i','s','a','b','l','e',0};
1556 static const WCHAR szEnable[] = {'E','n','a','b','l','e',0};
1557 static const WCHAR szDefault[] = {'D','e','f','a','u','l','t',0};
1558 msi_dialog *dialog = param;
1559 msi_control *control;
1560 LPCWSTR name, action, condition;
1561 UINT r;
1563 name = MSI_RecordGetString( rec, 2 );
1564 action = MSI_RecordGetString( rec, 3 );
1565 condition = MSI_RecordGetString( rec, 4 );
1566 r = MSI_EvaluateConditionW( dialog->package, condition );
1567 control = msi_dialog_find_control( dialog, name );
1568 if (r == MSICONDITION_TRUE && control)
1570 TRACE("%s control %s\n", debugstr_w(action), debugstr_w(name));
1572 /* FIXME: case sensitive? */
1573 if (!strcmpW( action, szHide ))
1574 ShowWindow(control->hwnd, SW_HIDE);
1575 else if (!strcmpW( action, szShow ))
1576 ShowWindow(control->hwnd, SW_SHOW);
1577 else if (!strcmpW( action, szDisable ))
1578 EnableWindow(control->hwnd, FALSE);
1579 else if (!strcmpW( action, szEnable ))
1580 EnableWindow(control->hwnd, TRUE);
1581 else if (!strcmpW( action, szDefault ))
1582 SetFocus(control->hwnd);
1583 else
1584 FIXME("Unhandled action %s\n", debugstr_w(action));
1586 return ERROR_SUCCESS;
1589 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog )
1591 static const WCHAR query[] = {
1592 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1593 'C','o','n','t','r','o','l','C','o','n','d','i','t','i','o','n',' ',
1594 'W','H','E','R','E',' ','`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
1595 UINT r;
1596 MSIQUERY *view;
1597 MSIPACKAGE *package = dialog->package;
1599 TRACE("%p %s\n", dialog, debugstr_w(dialog->name));
1601 /* query the Control table for all the elements of the control */
1602 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
1603 if (r != ERROR_SUCCESS)
1604 return ERROR_SUCCESS;
1606 r = MSI_IterateRecords( view, 0, msi_dialog_set_control_condition, dialog );
1607 msiobj_release( &view->hdr );
1608 return r;
1611 static UINT msi_dialog_combobox_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
1613 struct msi_combobox_info *info;
1614 int index;
1615 LPWSTR value;
1617 if (HIWORD(param) != CBN_SELCHANGE && HIWORD(param) != CBN_EDITCHANGE)
1618 return ERROR_SUCCESS;
1620 info = GetPropW( control->hwnd, szButtonData );
1621 index = SendMessageW( control->hwnd, CB_GETCURSEL, 0, 0 );
1622 if (index == CB_ERR)
1623 value = msi_get_window_text( control->hwnd );
1624 else
1625 value = (LPWSTR) SendMessageW( control->hwnd, CB_GETITEMDATA, index, 0 );
1627 msi_dialog_set_property( info->dialog->package, control->property, value );
1628 msi_dialog_evaluate_control_conditions( info->dialog );
1630 if (index == CB_ERR)
1631 msi_free( value );
1633 return ERROR_SUCCESS;
1636 static void msi_dialog_combobox_update( msi_dialog *dialog, msi_control *control )
1638 struct msi_combobox_info *info;
1639 LPWSTR value, tmp;
1640 DWORD j;
1642 info = GetPropW( control->hwnd, szButtonData );
1644 value = msi_dup_property( dialog->package->db, control->property );
1645 if (!value)
1647 SendMessageW( control->hwnd, CB_SETCURSEL, -1, 0 );
1648 return;
1651 for (j = 0; j < info->num_items; j++)
1653 tmp = (LPWSTR) SendMessageW( control->hwnd, CB_GETITEMDATA, j, 0 );
1654 if (!strcmpW( value, tmp ))
1655 break;
1658 if (j < info->num_items)
1660 SendMessageW( control->hwnd, CB_SETCURSEL, j, 0 );
1662 else
1664 SendMessageW( control->hwnd, CB_SETCURSEL, -1, 0 );
1665 SetWindowTextW( control->hwnd, value );
1668 msi_free(value);
1671 static UINT msi_dialog_combo_control( msi_dialog *dialog, MSIRECORD *rec )
1673 struct msi_combobox_info *info;
1674 msi_control *control;
1675 DWORD attributes, style;
1676 LPCWSTR prop;
1678 info = msi_alloc( sizeof *info );
1679 if (!info)
1680 return ERROR_FUNCTION_FAILED;
1682 style = CBS_AUTOHSCROLL | WS_TABSTOP | WS_GROUP | WS_CHILD;
1683 attributes = MSI_RecordGetInteger( rec, 8 );
1684 if ( ~attributes & msidbControlAttributesSorted)
1685 style |= CBS_SORT;
1686 if ( attributes & msidbControlAttributesComboList)
1687 style |= CBS_DROPDOWNLIST;
1688 else
1689 style |= CBS_DROPDOWN;
1691 control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
1692 if (!control)
1694 msi_free( info );
1695 return ERROR_FUNCTION_FAILED;
1698 control->handler = msi_dialog_combobox_handler;
1699 control->update = msi_dialog_combobox_update;
1701 prop = MSI_RecordGetString( rec, 9 );
1702 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
1704 /* subclass */
1705 info->dialog = dialog;
1706 info->hwnd = control->hwnd;
1707 info->items = NULL;
1708 info->addpos_items = 0;
1709 info->oldproc = (WNDPROC)SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1710 (LONG_PTR)MSIComboBox_WndProc );
1711 SetPropW( control->hwnd, szButtonData, info );
1713 if (control->property)
1714 msi_combobox_add_items( info, control->property );
1716 msi_dialog_combobox_update( dialog, control );
1718 return ERROR_SUCCESS;
1721 static UINT msi_dialog_edit_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
1723 LPWSTR buf;
1725 if (HIWORD(param) != EN_CHANGE)
1726 return ERROR_SUCCESS;
1728 TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name), debugstr_w(control->property));
1730 buf = msi_get_window_text( control->hwnd );
1731 msi_dialog_set_property( dialog->package, control->property, buf );
1732 msi_free( buf );
1734 return ERROR_SUCCESS;
1737 /* length of 2^32 + 1 */
1738 #define MAX_NUM_DIGITS 11
1740 static UINT msi_dialog_edit_control( msi_dialog *dialog, MSIRECORD *rec )
1742 msi_control *control;
1743 LPCWSTR prop, text;
1744 LPWSTR val, begin, end;
1745 WCHAR num[MAX_NUM_DIGITS];
1746 DWORD limit;
1748 control = msi_dialog_add_control( dialog, rec, szEdit,
1749 WS_BORDER | WS_TABSTOP | ES_AUTOHSCROLL );
1750 control->handler = msi_dialog_edit_handler;
1752 text = MSI_RecordGetString( rec, 10 );
1753 if ( text )
1755 begin = strchrW( text, '{' );
1756 end = strchrW( text, '}' );
1758 if ( begin && end && end > begin &&
1759 begin[0] >= '0' && begin[0] <= '9' &&
1760 end - begin < MAX_NUM_DIGITS)
1762 lstrcpynW( num, begin + 1, end - begin );
1763 limit = atolW( num );
1765 SendMessageW( control->hwnd, EM_SETLIMITTEXT, limit, 0 );
1769 prop = MSI_RecordGetString( rec, 9 );
1770 if( prop )
1771 control->property = strdupW( prop );
1773 val = msi_dup_property( dialog->package->db, control->property );
1774 SetWindowTextW( control->hwnd, val );
1775 msi_free( val );
1776 return ERROR_SUCCESS;
1779 /******************** Masked Edit ********************************************/
1781 #define MASK_MAX_GROUPS 20
1783 struct msi_mask_group
1785 UINT len;
1786 UINT ofs;
1787 WCHAR type;
1788 HWND hwnd;
1791 struct msi_maskedit_info
1793 msi_dialog *dialog;
1794 WNDPROC oldproc;
1795 HWND hwnd;
1796 LPWSTR prop;
1797 UINT num_chars;
1798 UINT num_groups;
1799 struct msi_mask_group group[MASK_MAX_GROUPS];
1802 static BOOL msi_mask_editable( WCHAR type )
1804 switch (type)
1806 case '%':
1807 case '#':
1808 case '&':
1809 case '`':
1810 case '?':
1811 case '^':
1812 return TRUE;
1814 return FALSE;
1817 static void msi_mask_control_change( struct msi_maskedit_info *info )
1819 LPWSTR val;
1820 UINT i, n, r;
1822 val = msi_alloc( (info->num_chars+1)*sizeof(WCHAR) );
1823 for( i=0, n=0; i<info->num_groups; i++ )
1825 if (info->group[i].len == ~0u)
1827 UINT len = SendMessageW( info->group[i].hwnd, WM_GETTEXTLENGTH, 0, 0 );
1828 val = msi_realloc( val, (len + 1) * sizeof(WCHAR) );
1829 GetWindowTextW( info->group[i].hwnd, val, len + 1 );
1831 else
1833 if (info->group[i].len + n > info->num_chars)
1835 ERR("can't fit control %d text into template\n",i);
1836 break;
1838 if (!msi_mask_editable(info->group[i].type))
1840 for(r=0; r<info->group[i].len; r++)
1841 val[n+r] = info->group[i].type;
1842 val[n+r] = 0;
1844 else
1846 r = GetWindowTextW( info->group[i].hwnd, &val[n], info->group[i].len+1 );
1847 if( r != info->group[i].len )
1848 break;
1850 n += r;
1854 TRACE("%d/%d controls were good\n", i, info->num_groups);
1856 if( i == info->num_groups )
1858 TRACE("Set property %s to %s\n", debugstr_w(info->prop), debugstr_w(val));
1859 msi_dialog_set_property( info->dialog->package, info->prop, val );
1860 msi_dialog_evaluate_control_conditions( info->dialog );
1862 msi_free( val );
1865 /* now move to the next control if necessary */
1866 static VOID msi_mask_next_control( struct msi_maskedit_info *info, HWND hWnd )
1868 HWND hWndNext;
1869 UINT len, i;
1871 for( i=0; i<info->num_groups; i++ )
1872 if( info->group[i].hwnd == hWnd )
1873 break;
1875 /* don't move from the last control */
1876 if( i >= (info->num_groups-1) )
1877 return;
1879 len = SendMessageW( hWnd, WM_GETTEXTLENGTH, 0, 0 );
1880 if( len < info->group[i].len )
1881 return;
1883 hWndNext = GetNextDlgTabItem( GetParent( hWnd ), hWnd, FALSE );
1884 SetFocus( hWndNext );
1887 static LRESULT WINAPI
1888 MSIMaskedEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1890 struct msi_maskedit_info *info;
1891 HRESULT r;
1893 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1895 info = GetPropW(hWnd, szButtonData);
1897 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1899 switch( msg )
1901 case WM_COMMAND:
1902 if (HIWORD(wParam) == EN_CHANGE)
1904 msi_mask_control_change( info );
1905 msi_mask_next_control( info, (HWND) lParam );
1907 break;
1908 case WM_NCDESTROY:
1909 msi_free( info->prop );
1910 msi_free( info );
1911 RemovePropW( hWnd, szButtonData );
1912 break;
1915 return r;
1918 /* fish the various bits of the property out and put them in the control */
1919 static void
1920 msi_maskedit_set_text( struct msi_maskedit_info *info, LPCWSTR text )
1922 LPCWSTR p;
1923 UINT i;
1925 p = text;
1926 for( i = 0; i < info->num_groups; i++ )
1928 if( info->group[i].len < strlenW( p ) )
1930 LPWSTR chunk = strdupW( p );
1931 chunk[ info->group[i].len ] = 0;
1932 SetWindowTextW( info->group[i].hwnd, chunk );
1933 msi_free( chunk );
1935 else
1937 SetWindowTextW( info->group[i].hwnd, p );
1938 break;
1940 p += info->group[i].len;
1944 static struct msi_maskedit_info * msi_dialog_parse_groups( LPCWSTR mask )
1946 struct msi_maskedit_info *info;
1947 int i = 0, n = 0, total = 0;
1948 LPCWSTR p;
1950 TRACE("masked control, template %s\n", debugstr_w(mask));
1952 if( !mask )
1953 return NULL;
1955 info = msi_alloc_zero( sizeof *info );
1956 if( !info )
1957 return info;
1959 p = strchrW(mask, '<');
1960 if( p )
1961 p++;
1962 else
1963 p = mask;
1965 for( i=0; i<MASK_MAX_GROUPS; i++ )
1967 /* stop at the end of the string */
1968 if( p[0] == 0 || p[0] == '>' )
1970 if (!total)
1972 /* create a group for the empty mask */
1973 info->group[0].type = '&';
1974 info->group[0].len = ~0u;
1975 i = 1;
1977 break;
1980 /* count the number of the same identifier */
1981 for( n=0; p[n] == p[0]; n++ )
1983 info->group[i].ofs = total;
1984 info->group[i].type = p[0];
1985 if( p[n] == '=' )
1987 n++;
1988 total++; /* an extra not part of the group */
1990 info->group[i].len = n;
1991 total += n;
1992 p += n;
1995 TRACE("%d characters in %d groups\n", total, i );
1996 if( i == MASK_MAX_GROUPS )
1997 ERR("too many groups in PIDTemplate %s\n", debugstr_w(mask));
1999 info->num_chars = total;
2000 info->num_groups = i;
2002 return info;
2005 static void
2006 msi_maskedit_create_children( struct msi_maskedit_info *info, LPCWSTR font )
2008 DWORD width, height, style, wx, ww;
2009 RECT rect;
2010 HWND hwnd;
2011 UINT i;
2013 style = WS_CHILD | WS_BORDER | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL;
2015 GetClientRect( info->hwnd, &rect );
2017 width = rect.right - rect.left;
2018 height = rect.bottom - rect.top;
2020 for( i = 0; i < info->num_groups; i++ )
2022 if (!msi_mask_editable( info->group[i].type ))
2023 continue;
2024 if (info->num_chars)
2026 wx = (info->group[i].ofs * width) / info->num_chars;
2027 ww = (info->group[i].len * width) / info->num_chars;
2029 else
2031 wx = 0;
2032 ww = width;
2034 hwnd = CreateWindowW( szEdit, NULL, style, wx, 0, ww, height,
2035 info->hwnd, NULL, NULL, NULL );
2036 if( !hwnd )
2038 ERR("failed to create mask edit sub window\n");
2039 break;
2042 SendMessageW( hwnd, EM_LIMITTEXT, info->group[i].len, 0 );
2044 msi_dialog_set_font( info->dialog, hwnd,
2045 font?font:info->dialog->default_font );
2046 info->group[i].hwnd = hwnd;
2051 * office 2003 uses "73931<````=````=````=````=`````>@@@@@"
2052 * delphi 7 uses "<????-??????-??????-????>" and "<???-???>"
2053 * filemaker pro 7 uses "<^^^^=^^^^=^^^^=^^^^=^^^^=^^^^=^^^^^>"
2055 static UINT msi_dialog_maskedit_control( msi_dialog *dialog, MSIRECORD *rec )
2057 LPWSTR font_mask, val = NULL, font;
2058 struct msi_maskedit_info *info = NULL;
2059 UINT ret = ERROR_SUCCESS;
2060 msi_control *control;
2061 LPCWSTR prop, mask;
2063 TRACE("\n");
2065 font_mask = msi_get_deformatted_field( dialog->package, rec, 10 );
2066 font = msi_dialog_get_style( font_mask, &mask );
2067 if( !mask )
2069 WARN("mask template is empty\n");
2070 goto end;
2073 info = msi_dialog_parse_groups( mask );
2074 if( !info )
2076 ERR("template %s is invalid\n", debugstr_w(mask));
2077 goto end;
2080 info->dialog = dialog;
2082 control = msi_dialog_add_control( dialog, rec, szStatic,
2083 SS_OWNERDRAW | WS_GROUP | WS_VISIBLE );
2084 if( !control )
2086 ERR("Failed to create maskedit container\n");
2087 ret = ERROR_FUNCTION_FAILED;
2088 goto end;
2090 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
2092 info->hwnd = control->hwnd;
2094 /* subclass the static control */
2095 info->oldproc = (WNDPROC) SetWindowLongPtrW( info->hwnd, GWLP_WNDPROC,
2096 (LONG_PTR)MSIMaskedEdit_WndProc );
2097 SetPropW( control->hwnd, szButtonData, info );
2099 prop = MSI_RecordGetString( rec, 9 );
2100 if( prop )
2101 info->prop = strdupW( prop );
2103 msi_maskedit_create_children( info, font );
2105 if( prop )
2107 val = msi_dup_property( dialog->package->db, prop );
2108 if( val )
2110 msi_maskedit_set_text( info, val );
2111 msi_free( val );
2115 end:
2116 if( ret != ERROR_SUCCESS )
2117 msi_free( info );
2118 msi_free( font_mask );
2119 msi_free( font );
2120 return ret;
2123 /******************** Progress Bar *****************************************/
2125 static UINT msi_dialog_progress_bar( msi_dialog *dialog, MSIRECORD *rec )
2127 msi_control *control;
2128 DWORD attributes, style;
2130 style = WS_VISIBLE;
2131 attributes = MSI_RecordGetInteger( rec, 8 );
2132 if( !(attributes & msidbControlAttributesProgress95) )
2133 style |= PBS_SMOOTH;
2135 control = msi_dialog_add_control( dialog, rec, PROGRESS_CLASSW, style );
2136 if( !control )
2137 return ERROR_FUNCTION_FAILED;
2139 event_subscribe( dialog, szSetProgress, control->name, szProgress );
2140 return ERROR_SUCCESS;
2143 /******************** Path Edit ********************************************/
2145 struct msi_pathedit_info
2147 msi_dialog *dialog;
2148 msi_control *control;
2149 WNDPROC oldproc;
2152 static void msi_dialog_update_pathedit( msi_dialog *dialog, msi_control *control )
2154 LPWSTR prop, path;
2155 BOOL indirect;
2157 if (!control && !(control = msi_dialog_find_control_by_type( dialog, szPathEdit )))
2158 return;
2160 indirect = control->attributes & msidbControlAttributesIndirect;
2161 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2162 path = msi_dialog_dup_property( dialog, prop, TRUE );
2164 SetWindowTextW( control->hwnd, path );
2165 SendMessageW( control->hwnd, EM_SETSEL, 0, -1 );
2167 msi_free( path );
2168 msi_free( prop );
2171 /* FIXME: test when this should fail */
2172 static BOOL msi_dialog_verify_path( LPWSTR path )
2174 if ( !path[0] )
2175 return FALSE;
2177 if ( PathIsRelativeW( path ) )
2178 return FALSE;
2180 return TRUE;
2183 /* returns TRUE if the path is valid, FALSE otherwise */
2184 static BOOL msi_dialog_onkillfocus( msi_dialog *dialog, msi_control *control )
2186 LPWSTR buf, prop;
2187 BOOL indirect;
2188 BOOL valid;
2190 indirect = control->attributes & msidbControlAttributesIndirect;
2191 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2193 buf = msi_get_window_text( control->hwnd );
2195 if ( !msi_dialog_verify_path( buf ) )
2197 /* FIXME: display an error message box */
2198 ERR("Invalid path %s\n", debugstr_w( buf ));
2199 valid = FALSE;
2200 SetFocus( control->hwnd );
2202 else
2204 valid = TRUE;
2205 msi_dialog_set_property( dialog->package, prop, buf );
2208 msi_dialog_update_pathedit( dialog, control );
2210 TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
2211 debugstr_w(prop));
2213 msi_free( buf );
2214 msi_free( prop );
2216 return valid;
2219 static LRESULT WINAPI MSIPathEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2221 struct msi_pathedit_info *info = GetPropW(hWnd, szButtonData);
2222 LRESULT r = 0;
2224 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
2226 if ( msg == WM_KILLFOCUS )
2228 /* if the path is invalid, don't handle this message */
2229 if ( !msi_dialog_onkillfocus( info->dialog, info->control ) )
2230 return 0;
2233 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
2235 if ( msg == WM_NCDESTROY )
2237 msi_free( info );
2238 RemovePropW( hWnd, szButtonData );
2241 return r;
2244 static UINT msi_dialog_pathedit_control( msi_dialog *dialog, MSIRECORD *rec )
2246 struct msi_pathedit_info *info;
2247 msi_control *control;
2248 LPCWSTR prop;
2250 info = msi_alloc( sizeof *info );
2251 if (!info)
2252 return ERROR_FUNCTION_FAILED;
2254 control = msi_dialog_add_control( dialog, rec, szEdit,
2255 WS_BORDER | WS_TABSTOP );
2256 control->attributes = MSI_RecordGetInteger( rec, 8 );
2257 prop = MSI_RecordGetString( rec, 9 );
2258 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2260 info->dialog = dialog;
2261 info->control = control;
2262 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2263 (LONG_PTR)MSIPathEdit_WndProc );
2264 SetPropW( control->hwnd, szButtonData, info );
2266 msi_dialog_update_pathedit( dialog, control );
2268 return ERROR_SUCCESS;
2271 static UINT msi_dialog_radiogroup_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
2273 if (HIWORD(param) != BN_CLICKED)
2274 return ERROR_SUCCESS;
2276 TRACE("clicked radio button %s, set %s\n", debugstr_w(control->name), debugstr_w(control->property));
2278 msi_dialog_set_property( dialog->package, control->property, control->name );
2280 return msi_dialog_button_handler( dialog, control, param );
2283 /* radio buttons are a bit different from normal controls */
2284 static UINT msi_dialog_create_radiobutton( MSIRECORD *rec, LPVOID param )
2286 radio_button_group_descr *group = param;
2287 msi_dialog *dialog = group->dialog;
2288 msi_control *control;
2289 LPCWSTR prop, text, name;
2290 DWORD style, attributes = group->attributes;
2292 style = WS_CHILD | BS_AUTORADIOBUTTON | BS_MULTILINE | WS_TABSTOP;
2293 name = MSI_RecordGetString( rec, 3 );
2294 text = MSI_RecordGetString( rec, 8 );
2295 if( attributes & msidbControlAttributesVisible )
2296 style |= WS_VISIBLE;
2297 if( ~attributes & msidbControlAttributesEnabled )
2298 style |= WS_DISABLED;
2300 control = dialog_create_window( dialog, rec, 0, szButton, name, text, style,
2301 group->parent->hwnd );
2302 if (!control)
2303 return ERROR_FUNCTION_FAILED;
2304 control->handler = msi_dialog_radiogroup_handler;
2306 if (group->propval && !strcmpW( control->name, group->propval ))
2307 SendMessageW(control->hwnd, BM_SETCHECK, BST_CHECKED, 0);
2309 prop = MSI_RecordGetString( rec, 1 );
2310 if( prop )
2311 control->property = strdupW( prop );
2313 return ERROR_SUCCESS;
2316 static BOOL CALLBACK msi_radioground_child_enum( HWND hWnd, LPARAM lParam )
2318 EnableWindow( hWnd, lParam );
2319 return TRUE;
2322 static LRESULT WINAPI MSIRadioGroup_WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
2324 WNDPROC oldproc = (WNDPROC)GetPropW( hWnd, szButtonData );
2325 LRESULT r;
2327 TRACE("hWnd %p msg %04x wParam 0x%08lx lParam 0x%08lx\n", hWnd, msg, wParam, lParam);
2329 if (msg == WM_COMMAND) /* Forward notifications to dialog */
2330 SendMessageW( GetParent( hWnd ), msg, wParam, lParam );
2332 r = CallWindowProcW( oldproc, hWnd, msg, wParam, lParam );
2334 /* make sure the radio buttons show as disabled if the parent is disabled */
2335 if (msg == WM_ENABLE)
2336 EnumChildWindows( hWnd, msi_radioground_child_enum, wParam );
2338 return r;
2341 static UINT msi_dialog_radiogroup_control( msi_dialog *dialog, MSIRECORD *rec )
2343 static const WCHAR query[] = {
2344 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2345 'R','a','d','i','o','B','u','t','t','o','n',' ','W','H','E','R','E',' ',
2346 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
2347 UINT r;
2348 LPCWSTR prop;
2349 msi_control *control;
2350 MSIQUERY *view;
2351 radio_button_group_descr group;
2352 MSIPACKAGE *package = dialog->package;
2353 WNDPROC oldproc;
2354 DWORD attr, style = WS_GROUP;
2356 prop = MSI_RecordGetString( rec, 9 );
2358 TRACE("%p %p %s\n", dialog, rec, debugstr_w( prop ));
2360 attr = MSI_RecordGetInteger( rec, 8 );
2361 if (attr & msidbControlAttributesHasBorder)
2362 style |= BS_GROUPBOX;
2363 else
2364 style |= BS_OWNERDRAW;
2366 /* Create parent group box to hold radio buttons */
2367 control = msi_dialog_add_control( dialog, rec, szButton, style );
2368 if( !control )
2369 return ERROR_FUNCTION_FAILED;
2371 oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2372 (LONG_PTR)MSIRadioGroup_WndProc );
2373 SetPropW(control->hwnd, szButtonData, oldproc);
2374 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
2376 if( prop )
2377 control->property = strdupW( prop );
2379 /* query the Radio Button table for all control in this group */
2380 r = MSI_OpenQuery( package->db, &view, query, prop );
2381 if( r != ERROR_SUCCESS )
2383 ERR("query failed for dialog %s radio group %s\n",
2384 debugstr_w(dialog->name), debugstr_w(prop));
2385 return ERROR_INVALID_PARAMETER;
2388 group.dialog = dialog;
2389 group.parent = control;
2390 group.attributes = MSI_RecordGetInteger( rec, 8 );
2391 group.propval = msi_dup_property( dialog->package->db, control->property );
2393 r = MSI_IterateRecords( view, 0, msi_dialog_create_radiobutton, &group );
2394 msiobj_release( &view->hdr );
2395 msi_free( group.propval );
2396 return r;
2399 static void
2400 msi_seltree_sync_item_state( HWND hwnd, MSIFEATURE *feature, HTREEITEM hItem )
2402 TVITEMW tvi;
2403 DWORD index = feature->ActionRequest;
2405 TRACE("Feature %s -> %d %d %d\n", debugstr_w(feature->Title),
2406 feature->Installed, feature->Action, feature->ActionRequest);
2408 if (index == INSTALLSTATE_UNKNOWN)
2409 index = INSTALLSTATE_ABSENT;
2411 tvi.mask = TVIF_STATE;
2412 tvi.hItem = hItem;
2413 tvi.state = INDEXTOSTATEIMAGEMASK( index );
2414 tvi.stateMask = TVIS_STATEIMAGEMASK;
2416 SendMessageW( hwnd, TVM_SETITEMW, 0, (LPARAM) &tvi );
2419 static UINT
2420 msi_seltree_popup_menu( HWND hwnd, INT x, INT y )
2422 HMENU hMenu;
2423 INT r;
2425 /* create a menu to display */
2426 hMenu = CreatePopupMenu();
2428 /* FIXME: load strings from resources */
2429 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_LOCAL, "Install feature locally");
2430 AppendMenuA( hMenu, MF_ENABLED, USER_INSTALLSTATE_ALL, "Install entire feature");
2431 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ADVERTISED, "Install on demand");
2432 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ABSENT, "Don't install");
2433 r = TrackPopupMenu( hMenu, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RETURNCMD,
2434 x, y, 0, hwnd, NULL );
2435 DestroyMenu( hMenu );
2436 return r;
2439 static void
2440 msi_seltree_update_feature_installstate( HWND hwnd, HTREEITEM hItem,
2441 MSIPACKAGE *package, MSIFEATURE *feature, INSTALLSTATE state )
2443 feature->ActionRequest = state;
2444 msi_seltree_sync_item_state( hwnd, feature, hItem );
2445 ACTION_UpdateComponentStates( package, feature );
2448 static void
2449 msi_seltree_update_siblings_and_children_installstate( HWND hwnd, HTREEITEM curr,
2450 MSIPACKAGE *package, INSTALLSTATE state)
2452 /* update all siblings */
2455 MSIFEATURE *feature;
2456 HTREEITEM child;
2458 feature = msi_seltree_feature_from_item( hwnd, curr );
2459 msi_seltree_update_feature_installstate( hwnd, curr, package, feature, state );
2461 /* update this sibling's children */
2462 child = (HTREEITEM)SendMessageW( hwnd, TVM_GETNEXTITEM, (WPARAM)TVGN_CHILD, (LPARAM)curr );
2463 if (child)
2464 msi_seltree_update_siblings_and_children_installstate( hwnd, child,
2465 package, state );
2467 while ((curr = (HTREEITEM)SendMessageW( hwnd, TVM_GETNEXTITEM, (WPARAM)TVGN_NEXT, (LPARAM)curr )));
2470 static LRESULT
2471 msi_seltree_menu( HWND hwnd, HTREEITEM hItem )
2473 struct msi_selection_tree_info *info;
2474 MSIFEATURE *feature;
2475 MSIPACKAGE *package;
2476 union {
2477 RECT rc;
2478 POINT pt[2];
2479 HTREEITEM hItem;
2480 } u;
2481 UINT r;
2483 info = GetPropW(hwnd, szButtonData);
2484 package = info->dialog->package;
2486 feature = msi_seltree_feature_from_item( hwnd, hItem );
2487 if (!feature)
2489 ERR("item %p feature was NULL\n", hItem);
2490 return 0;
2493 /* get the item's rectangle to put the menu just below it */
2494 u.hItem = hItem;
2495 SendMessageW( hwnd, TVM_GETITEMRECT, 0, (LPARAM) &u.rc );
2496 MapWindowPoints( hwnd, NULL, u.pt, 2 );
2498 r = msi_seltree_popup_menu( hwnd, u.rc.left, u.rc.top );
2500 switch (r)
2502 case USER_INSTALLSTATE_ALL:
2503 r = INSTALLSTATE_LOCAL;
2504 /* fall-through */
2505 case INSTALLSTATE_ADVERTISED:
2506 case INSTALLSTATE_ABSENT:
2508 HTREEITEM child;
2509 child = (HTREEITEM)SendMessageW( hwnd, TVM_GETNEXTITEM, (WPARAM)TVGN_CHILD, (LPARAM)hItem );
2510 if (child)
2511 msi_seltree_update_siblings_and_children_installstate( hwnd, child, package, r );
2513 /* fall-through */
2514 case INSTALLSTATE_LOCAL:
2515 msi_seltree_update_feature_installstate( hwnd, hItem, package, feature, r );
2516 break;
2519 return 0;
2522 static LRESULT WINAPI
2523 MSISelectionTree_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2525 struct msi_selection_tree_info *info;
2526 TVHITTESTINFO tvhti;
2527 HRESULT r;
2529 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
2531 info = GetPropW(hWnd, szButtonData);
2533 switch( msg )
2535 case WM_LBUTTONDOWN:
2536 tvhti.pt.x = (short)LOWORD( lParam );
2537 tvhti.pt.y = (short)HIWORD( lParam );
2538 tvhti.flags = 0;
2539 tvhti.hItem = 0;
2540 CallWindowProcW(info->oldproc, hWnd, TVM_HITTEST, 0, (LPARAM) &tvhti );
2541 if (tvhti.flags & TVHT_ONITEMSTATEICON)
2542 return msi_seltree_menu( hWnd, tvhti.hItem );
2543 break;
2545 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
2547 switch( msg )
2549 case WM_NCDESTROY:
2550 msi_free( info );
2551 RemovePropW( hWnd, szButtonData );
2552 break;
2554 return r;
2557 static void
2558 msi_seltree_add_child_features( MSIPACKAGE *package, HWND hwnd,
2559 LPCWSTR parent, HTREEITEM hParent )
2561 struct msi_selection_tree_info *info = GetPropW( hwnd, szButtonData );
2562 MSIFEATURE *feature;
2563 TVINSERTSTRUCTW tvis;
2564 HTREEITEM hitem, hfirst = NULL;
2566 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
2568 if ( parent && feature->Feature_Parent && strcmpW( parent, feature->Feature_Parent ))
2569 continue;
2570 else if ( parent && !feature->Feature_Parent )
2571 continue;
2572 else if ( !parent && feature->Feature_Parent )
2573 continue;
2575 if ( !feature->Title )
2576 continue;
2578 if ( !feature->Display )
2579 continue;
2581 memset( &tvis, 0, sizeof tvis );
2582 tvis.hParent = hParent;
2583 tvis.hInsertAfter = TVI_LAST;
2584 tvis.u.item.mask = TVIF_TEXT | TVIF_PARAM;
2585 tvis.u.item.pszText = feature->Title;
2586 tvis.u.item.lParam = (LPARAM) feature;
2588 hitem = (HTREEITEM) SendMessageW( hwnd, TVM_INSERTITEMW, 0, (LPARAM) &tvis );
2589 if (!hitem)
2590 continue;
2592 if (!hfirst)
2593 hfirst = hitem;
2595 msi_seltree_sync_item_state( hwnd, feature, hitem );
2596 msi_seltree_add_child_features( package, hwnd,
2597 feature->Feature, hitem );
2599 /* the node is expanded if Display is odd */
2600 if ( feature->Display % 2 != 0 )
2601 SendMessageW( hwnd, TVM_EXPAND, TVE_EXPAND, (LPARAM) hitem );
2604 /* select the first item */
2605 SendMessageW( hwnd, TVM_SELECTITEM, TVGN_CARET | TVGN_DROPHILITE, (LPARAM) hfirst );
2606 info->selected = hfirst;
2609 static void msi_seltree_create_imagelist( HWND hwnd )
2611 const int bm_width = 32, bm_height = 16, bm_count = 3;
2612 const int bm_resource = 0x1001;
2613 HIMAGELIST himl;
2614 int i;
2615 HBITMAP hbmp;
2617 himl = ImageList_Create( bm_width, bm_height, FALSE, 4, 0 );
2618 if (!himl)
2620 ERR("failed to create image list\n");
2621 return;
2624 for (i=0; i<bm_count; i++)
2626 hbmp = LoadBitmapW( msi_hInstance, MAKEINTRESOURCEW(i+bm_resource) );
2627 if (!hbmp)
2629 ERR("failed to load bitmap %d\n", i);
2630 break;
2634 * Add a dummy bitmap at offset zero because the treeview
2635 * can't use it as a state mask (zero means no user state).
2637 if (!i)
2638 ImageList_Add( himl, hbmp, NULL );
2640 ImageList_Add( himl, hbmp, NULL );
2643 SendMessageW( hwnd, TVM_SETIMAGELIST, TVSIL_STATE, (LPARAM)himl );
2646 static UINT msi_dialog_seltree_handler( msi_dialog *dialog,
2647 msi_control *control, WPARAM param )
2649 struct msi_selection_tree_info *info = GetPropW( control->hwnd, szButtonData );
2650 LPNMTREEVIEWW tv = (LPNMTREEVIEWW)param;
2651 MSIRECORD *row, *rec;
2652 MSIFOLDER *folder;
2653 MSIFEATURE *feature;
2654 LPCWSTR dir, title = NULL;
2655 UINT r = ERROR_SUCCESS;
2657 static const WCHAR select[] = {
2658 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2659 '`','F','e','a','t','u','r','e','`',' ','W','H','E','R','E',' ',
2660 '`','T','i','t','l','e','`',' ','=',' ','\'','%','s','\'',0
2663 if (tv->hdr.code != TVN_SELCHANGINGW)
2664 return ERROR_SUCCESS;
2666 info->selected = tv->itemNew.hItem;
2668 if (!(tv->itemNew.mask & TVIF_TEXT))
2670 feature = msi_seltree_feature_from_item( control->hwnd, tv->itemNew.hItem );
2671 if (feature)
2672 title = feature->Title;
2674 else
2675 title = tv->itemNew.pszText;
2677 row = MSI_QueryGetRecord( dialog->package->db, select, title );
2678 if (!row)
2679 return ERROR_FUNCTION_FAILED;
2681 rec = MSI_CreateRecord( 1 );
2683 MSI_RecordSetStringW( rec, 1, MSI_RecordGetString( row, 4 ) );
2684 msi_event_fire( dialog->package, szSelectionDescription, rec );
2686 dir = MSI_RecordGetString( row, 7 );
2687 if (dir)
2689 folder = msi_get_loaded_folder( dialog->package, dir );
2690 if (!folder)
2692 r = ERROR_FUNCTION_FAILED;
2693 goto done;
2695 MSI_RecordSetStringW( rec, 1, folder->ResolvedTarget );
2697 else
2698 MSI_RecordSetStringW( rec, 1, NULL );
2700 msi_event_fire( dialog->package, szSelectionPath, rec );
2702 done:
2703 msiobj_release(&row->hdr);
2704 msiobj_release(&rec->hdr);
2706 return r;
2709 static UINT msi_dialog_selection_tree( msi_dialog *dialog, MSIRECORD *rec )
2711 msi_control *control;
2712 LPCWSTR prop, control_name;
2713 MSIPACKAGE *package = dialog->package;
2714 DWORD style;
2715 struct msi_selection_tree_info *info;
2717 info = msi_alloc( sizeof *info );
2718 if (!info)
2719 return ERROR_FUNCTION_FAILED;
2721 /* create the treeview control */
2722 style = TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT;
2723 style |= WS_GROUP | WS_VSCROLL;
2724 control = msi_dialog_add_control( dialog, rec, WC_TREEVIEWW, style );
2725 if (!control)
2727 msi_free(info);
2728 return ERROR_FUNCTION_FAILED;
2731 control->handler = msi_dialog_seltree_handler;
2732 control_name = MSI_RecordGetString( rec, 2 );
2733 control->attributes = MSI_RecordGetInteger( rec, 8 );
2734 prop = MSI_RecordGetString( rec, 9 );
2735 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2737 /* subclass */
2738 info->dialog = dialog;
2739 info->hwnd = control->hwnd;
2740 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2741 (LONG_PTR)MSISelectionTree_WndProc );
2742 SetPropW( control->hwnd, szButtonData, info );
2744 event_subscribe( dialog, szSelectionPath, control_name, szProperty );
2746 /* initialize it */
2747 msi_seltree_create_imagelist( control->hwnd );
2748 msi_seltree_add_child_features( package, control->hwnd, NULL, NULL );
2750 return ERROR_SUCCESS;
2753 /******************** Group Box ***************************************/
2755 static UINT msi_dialog_group_box( msi_dialog *dialog, MSIRECORD *rec )
2757 msi_control *control;
2758 DWORD style;
2760 style = BS_GROUPBOX | WS_CHILD | WS_GROUP;
2761 control = msi_dialog_add_control( dialog, rec, WC_BUTTONW, style );
2762 if (!control)
2763 return ERROR_FUNCTION_FAILED;
2765 return ERROR_SUCCESS;
2768 /******************** List Box ***************************************/
2770 struct msi_listbox_info
2772 msi_dialog *dialog;
2773 HWND hwnd;
2774 WNDPROC oldproc;
2775 DWORD num_items;
2776 DWORD addpos_items;
2777 LPWSTR *items;
2780 static LRESULT WINAPI MSIListBox_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2782 struct msi_listbox_info *info;
2783 LRESULT r;
2784 DWORD j;
2786 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
2788 info = GetPropW( hWnd, szButtonData );
2789 if (!info)
2790 return 0;
2792 r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
2794 switch( msg )
2796 case WM_NCDESTROY:
2797 for (j = 0; j < info->num_items; j++)
2798 msi_free( info->items[j] );
2799 msi_free( info->items );
2800 msi_free( info );
2801 RemovePropW( hWnd, szButtonData );
2802 break;
2805 return r;
2808 static UINT msi_listbox_add_item( MSIRECORD *rec, LPVOID param )
2810 struct msi_listbox_info *info = param;
2811 LPCWSTR value, text;
2812 int pos;
2814 value = MSI_RecordGetString( rec, 3 );
2815 text = MSI_RecordGetString( rec, 4 );
2817 info->items[info->addpos_items] = strdupW( value );
2819 pos = SendMessageW( info->hwnd, LB_ADDSTRING, 0, (LPARAM)text );
2820 SendMessageW( info->hwnd, LB_SETITEMDATA, pos, (LPARAM)info->items[info->addpos_items] );
2821 info->addpos_items++;
2822 return ERROR_SUCCESS;
2825 static UINT msi_listbox_add_items( struct msi_listbox_info *info, LPCWSTR property )
2827 static const WCHAR query[] = {
2828 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2829 '`','L','i','s','t','B','o','x','`',' ','W','H','E','R','E',' ',
2830 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',' ',
2831 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','`',0};
2832 MSIQUERY *view;
2833 DWORD count;
2834 UINT r;
2836 r = MSI_OpenQuery( info->dialog->package->db, &view, query, property );
2837 if ( r != ERROR_SUCCESS )
2838 return r;
2840 /* just get the number of records */
2841 count = 0;
2842 r = MSI_IterateRecords( view, &count, NULL, NULL );
2843 if (r != ERROR_SUCCESS)
2845 msiobj_release( &view->hdr );
2846 return r;
2848 info->num_items = count;
2849 info->items = msi_alloc( sizeof(*info->items) * count );
2851 r = MSI_IterateRecords( view, NULL, msi_listbox_add_item, info );
2852 msiobj_release( &view->hdr );
2853 return r;
2856 static UINT msi_dialog_listbox_handler( msi_dialog *dialog,
2857 msi_control *control, WPARAM param )
2859 struct msi_listbox_info *info;
2860 int index;
2861 LPCWSTR value;
2863 if( HIWORD(param) != LBN_SELCHANGE )
2864 return ERROR_SUCCESS;
2866 info = GetPropW( control->hwnd, szButtonData );
2867 index = SendMessageW( control->hwnd, LB_GETCURSEL, 0, 0 );
2868 value = (LPCWSTR) SendMessageW( control->hwnd, LB_GETITEMDATA, index, 0 );
2870 msi_dialog_set_property( info->dialog->package, control->property, value );
2871 msi_dialog_evaluate_control_conditions( info->dialog );
2873 return ERROR_SUCCESS;
2876 static UINT msi_dialog_list_box( msi_dialog *dialog, MSIRECORD *rec )
2878 struct msi_listbox_info *info;
2879 msi_control *control;
2880 DWORD attributes, style;
2881 LPCWSTR prop;
2883 info = msi_alloc( sizeof *info );
2884 if (!info)
2885 return ERROR_FUNCTION_FAILED;
2887 style = WS_TABSTOP | WS_GROUP | WS_CHILD | LBS_NOTIFY | WS_VSCROLL | WS_BORDER;
2888 attributes = MSI_RecordGetInteger( rec, 8 );
2889 if (~attributes & msidbControlAttributesSorted)
2890 style |= LBS_SORT;
2892 control = msi_dialog_add_control( dialog, rec, WC_LISTBOXW, style );
2893 if (!control)
2895 msi_free(info);
2896 return ERROR_FUNCTION_FAILED;
2899 control->handler = msi_dialog_listbox_handler;
2901 prop = MSI_RecordGetString( rec, 9 );
2902 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2904 /* subclass */
2905 info->dialog = dialog;
2906 info->hwnd = control->hwnd;
2907 info->items = NULL;
2908 info->addpos_items = 0;
2909 info->oldproc = (WNDPROC)SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2910 (LONG_PTR)MSIListBox_WndProc );
2911 SetPropW( control->hwnd, szButtonData, info );
2913 if ( control->property )
2914 msi_listbox_add_items( info, control->property );
2916 return ERROR_SUCCESS;
2919 /******************** Directory Combo ***************************************/
2921 static void msi_dialog_update_directory_combo( msi_dialog *dialog, msi_control *control )
2923 LPWSTR prop, path;
2924 BOOL indirect;
2926 if (!control && !(control = msi_dialog_find_control_by_type( dialog, szDirectoryCombo )))
2927 return;
2929 indirect = control->attributes & msidbControlAttributesIndirect;
2930 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2931 path = msi_dialog_dup_property( dialog, prop, TRUE );
2933 PathStripPathW( path );
2934 PathRemoveBackslashW( path );
2936 SendMessageW( control->hwnd, CB_INSERTSTRING, 0, (LPARAM)path );
2937 SendMessageW( control->hwnd, CB_SETCURSEL, 0, 0 );
2939 msi_free( path );
2940 msi_free( prop );
2943 static UINT msi_dialog_directory_combo( msi_dialog *dialog, MSIRECORD *rec )
2945 msi_control *control;
2946 LPCWSTR prop;
2947 DWORD style;
2949 /* FIXME: use CBS_OWNERDRAWFIXED and add owner draw code */
2950 style = CBS_DROPDOWNLIST | CBS_HASSTRINGS | WS_CHILD |
2951 WS_GROUP | WS_TABSTOP | WS_VSCROLL;
2952 control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
2953 if (!control)
2954 return ERROR_FUNCTION_FAILED;
2956 control->attributes = MSI_RecordGetInteger( rec, 8 );
2957 prop = MSI_RecordGetString( rec, 9 );
2958 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2960 msi_dialog_update_directory_combo( dialog, control );
2962 return ERROR_SUCCESS;
2965 /******************** Directory List ***************************************/
2967 static void msi_dialog_update_directory_list( msi_dialog *dialog, msi_control *control )
2969 WCHAR dir_spec[MAX_PATH];
2970 WIN32_FIND_DATAW wfd;
2971 LPWSTR prop, path;
2972 BOOL indirect;
2973 LVITEMW item;
2974 HANDLE file;
2976 static const WCHAR asterisk[] = {'*',0};
2978 if (!control && !(control = msi_dialog_find_control_by_type( dialog, szDirectoryList )))
2979 return;
2981 /* clear the list-view */
2982 SendMessageW( control->hwnd, LVM_DELETEALLITEMS, 0, 0 );
2984 indirect = control->attributes & msidbControlAttributesIndirect;
2985 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2986 path = msi_dialog_dup_property( dialog, prop, TRUE );
2988 lstrcpyW( dir_spec, path );
2989 lstrcatW( dir_spec, asterisk );
2991 file = FindFirstFileW( dir_spec, &wfd );
2992 if ( file == INVALID_HANDLE_VALUE )
2993 return;
2997 if ( wfd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY )
2998 continue;
3000 if ( !strcmpW( wfd.cFileName, szDot ) || !strcmpW( wfd.cFileName, szDotDot ) )
3001 continue;
3003 item.mask = LVIF_TEXT;
3004 item.cchTextMax = MAX_PATH;
3005 item.iItem = 0;
3006 item.iSubItem = 0;
3007 item.pszText = wfd.cFileName;
3009 SendMessageW( control->hwnd, LVM_INSERTITEMW, 0, (LPARAM)&item );
3010 } while ( FindNextFileW( file, &wfd ) );
3012 msi_free( prop );
3013 msi_free( path );
3014 FindClose( file );
3017 static UINT msi_dialog_directorylist_up( msi_dialog *dialog )
3019 msi_control *control;
3020 LPWSTR prop, path, ptr;
3021 BOOL indirect;
3023 control = msi_dialog_find_control_by_type( dialog, szDirectoryList );
3024 indirect = control->attributes & msidbControlAttributesIndirect;
3025 prop = msi_dialog_dup_property( dialog, control->property, indirect );
3026 path = msi_dialog_dup_property( dialog, prop, TRUE );
3028 /* strip off the last directory */
3029 ptr = PathFindFileNameW( path );
3030 if (ptr != path) *(ptr - 1) = '\0';
3031 PathAddBackslashW( path );
3033 msi_dialog_set_property( dialog->package, prop, path );
3035 msi_dialog_update_directory_list( dialog, NULL );
3036 msi_dialog_update_directory_combo( dialog, NULL );
3037 msi_dialog_update_pathedit( dialog, NULL );
3039 msi_free( path );
3040 msi_free( prop );
3042 return ERROR_SUCCESS;
3045 static UINT msi_dialog_dirlist_handler( msi_dialog *dialog,
3046 msi_control *control, WPARAM param )
3048 LPNMHDR nmhdr = (LPNMHDR)param;
3049 WCHAR new_path[MAX_PATH];
3050 WCHAR text[MAX_PATH];
3051 LPWSTR path, prop;
3052 BOOL indirect;
3053 LVITEMW item;
3054 int index;
3056 if (nmhdr->code != LVN_ITEMACTIVATE)
3057 return ERROR_SUCCESS;
3059 index = SendMessageW( control->hwnd, LVM_GETNEXTITEM, -1, LVNI_SELECTED );
3060 if ( index < 0 )
3062 ERR("No list-view item selected!\n");
3063 return ERROR_FUNCTION_FAILED;
3066 item.iSubItem = 0;
3067 item.pszText = text;
3068 item.cchTextMax = MAX_PATH;
3069 SendMessageW( control->hwnd, LVM_GETITEMTEXTW, index, (LPARAM)&item );
3071 indirect = control->attributes & msidbControlAttributesIndirect;
3072 prop = msi_dialog_dup_property( dialog, control->property, indirect );
3073 path = msi_dialog_dup_property( dialog, prop, TRUE );
3075 lstrcpyW( new_path, path );
3076 lstrcatW( new_path, text );
3077 lstrcatW( new_path, szBackSlash );
3079 msi_dialog_set_property( dialog->package, prop, new_path );
3081 msi_dialog_update_directory_list( dialog, NULL );
3082 msi_dialog_update_directory_combo( dialog, NULL );
3083 msi_dialog_update_pathedit( dialog, NULL );
3085 msi_free( prop );
3086 msi_free( path );
3087 return ERROR_SUCCESS;
3090 static UINT msi_dialog_directory_list( msi_dialog *dialog, MSIRECORD *rec )
3092 msi_control *control;
3093 LPCWSTR prop;
3094 DWORD style;
3096 style = LVS_LIST | WS_VSCROLL | LVS_SHAREIMAGELISTS |
3097 LVS_AUTOARRANGE | LVS_SINGLESEL | WS_BORDER |
3098 LVS_SORTASCENDING | WS_CHILD | WS_GROUP | WS_TABSTOP;
3099 control = msi_dialog_add_control( dialog, rec, WC_LISTVIEWW, style );
3100 if (!control)
3101 return ERROR_FUNCTION_FAILED;
3103 control->attributes = MSI_RecordGetInteger( rec, 8 );
3104 control->handler = msi_dialog_dirlist_handler;
3105 prop = MSI_RecordGetString( rec, 9 );
3106 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
3108 /* double click to activate an item in the list */
3109 SendMessageW( control->hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE,
3110 0, LVS_EX_TWOCLICKACTIVATE );
3112 msi_dialog_update_directory_list( dialog, control );
3114 return ERROR_SUCCESS;
3117 /******************** VolumeCost List ***************************************/
3119 static BOOL str_is_number( LPCWSTR str )
3121 int i;
3123 for (i = 0; i < lstrlenW( str ); i++)
3124 if (!isdigitW(str[i]))
3125 return FALSE;
3127 return TRUE;
3130 static const WCHAR column_keys[][80] =
3132 {'V','o','l','u','m','e','C','o','s','t','V','o','l','u','m','e',0},
3133 {'V','o','l','u','m','e','C','o','s','t','S','i','z','e',0},
3134 {'V','o','l','u','m','e','C','o','s','t','A','v','a','i','l','a','b','l','e',0},
3135 {'V','o','l','u','m','e','C','o','s','t','R','e','q','u','i','r','e','d',0},
3136 {'V','o','l','u','m','e','C','o','s','t','D','i','f','f','e','r','e','n','c','e',0}
3139 static void msi_dialog_vcl_add_columns( msi_dialog *dialog, msi_control *control, MSIRECORD *rec )
3141 LPCWSTR text = MSI_RecordGetString( rec, 10 );
3142 LPCWSTR begin = text, end;
3143 WCHAR *num;
3144 LVCOLUMNW lvc;
3145 DWORD count = 0;
3147 static const WCHAR negative[] = {'-',0};
3149 if (!text) return;
3151 while ((begin = strchrW( begin, '{' )) && count < 5)
3153 if (!(end = strchrW( begin, '}' )))
3154 return;
3156 num = msi_alloc( (end-begin+1)*sizeof(WCHAR) );
3157 if (!num)
3158 return;
3160 lstrcpynW( num, begin + 1, end - begin );
3161 begin += end - begin + 1;
3163 /* empty braces or '0' hides the column */
3164 if ( !num[0] || !strcmpW( num, szZero ) )
3166 count++;
3167 msi_free( num );
3168 continue;
3171 /* the width must be a positive number
3172 * if a width is invalid, all remaining columns are hidden
3174 if ( !strncmpW( num, negative, 1 ) || !str_is_number( num ) ) {
3175 msi_free( num );
3176 return;
3179 ZeroMemory( &lvc, sizeof(lvc) );
3180 lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
3181 lvc.cx = atolW( num );
3182 lvc.pszText = msi_dialog_get_uitext( dialog, column_keys[count] );
3184 SendMessageW( control->hwnd, LVM_INSERTCOLUMNW, count++, (LPARAM)&lvc );
3185 msi_free( lvc.pszText );
3186 msi_free( num );
3190 static LONGLONG msi_vcl_get_cost( msi_dialog *dialog )
3192 MSIFEATURE *feature;
3193 INT each_cost;
3194 LONGLONG total_cost = 0;
3196 LIST_FOR_EACH_ENTRY( feature, &dialog->package->features, MSIFEATURE, entry )
3198 if (ERROR_SUCCESS == (MSI_GetFeatureCost(dialog->package, feature,
3199 MSICOSTTREE_SELFONLY, INSTALLSTATE_LOCAL, &each_cost)))
3201 /* each_cost is in 512-byte units */
3202 total_cost += each_cost * 512;
3204 if (ERROR_SUCCESS == (MSI_GetFeatureCost(dialog->package, feature,
3205 MSICOSTTREE_SELFONLY, INSTALLSTATE_ABSENT, &each_cost)))
3207 /* each_cost is in 512-byte units */
3208 total_cost -= each_cost * 512;
3211 return total_cost;
3214 static void msi_dialog_vcl_add_drives( msi_dialog *dialog, msi_control *control )
3216 ULARGE_INTEGER total, free;
3217 LONGLONG difference, cost;
3218 WCHAR size_text[MAX_PATH];
3219 WCHAR cost_text[MAX_PATH];
3220 LPWSTR drives, ptr;
3221 LVITEMW lvitem;
3222 DWORD size;
3223 int i = 0;
3225 cost = msi_vcl_get_cost(dialog);
3226 StrFormatByteSizeW(cost, cost_text, MAX_PATH);
3228 size = GetLogicalDriveStringsW( 0, NULL );
3229 if ( !size ) return;
3231 drives = msi_alloc( (size + 1) * sizeof(WCHAR) );
3232 if ( !drives ) return;
3234 GetLogicalDriveStringsW( size, drives );
3236 ptr = drives;
3237 while (*ptr)
3239 lvitem.mask = LVIF_TEXT;
3240 lvitem.iItem = i;
3241 lvitem.iSubItem = 0;
3242 lvitem.pszText = ptr;
3243 lvitem.cchTextMax = lstrlenW(ptr) + 1;
3244 SendMessageW( control->hwnd, LVM_INSERTITEMW, 0, (LPARAM)&lvitem );
3246 GetDiskFreeSpaceExW(ptr, &free, &total, NULL);
3247 difference = free.QuadPart - cost;
3249 StrFormatByteSizeW(total.QuadPart, size_text, MAX_PATH);
3250 lvitem.iSubItem = 1;
3251 lvitem.pszText = size_text;
3252 lvitem.cchTextMax = lstrlenW(size_text) + 1;
3253 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
3255 StrFormatByteSizeW(free.QuadPart, size_text, MAX_PATH);
3256 lvitem.iSubItem = 2;
3257 lvitem.pszText = size_text;
3258 lvitem.cchTextMax = lstrlenW(size_text) + 1;
3259 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
3261 lvitem.iSubItem = 3;
3262 lvitem.pszText = cost_text;
3263 lvitem.cchTextMax = lstrlenW(cost_text) + 1;
3264 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
3266 StrFormatByteSizeW(difference, size_text, MAX_PATH);
3267 lvitem.iSubItem = 4;
3268 lvitem.pszText = size_text;
3269 lvitem.cchTextMax = lstrlenW(size_text) + 1;
3270 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
3272 ptr += lstrlenW(ptr) + 1;
3273 i++;
3276 msi_free( drives );
3279 static UINT msi_dialog_volumecost_list( msi_dialog *dialog, MSIRECORD *rec )
3281 msi_control *control;
3282 DWORD style;
3284 style = LVS_REPORT | WS_VSCROLL | WS_HSCROLL | LVS_SHAREIMAGELISTS |
3285 LVS_AUTOARRANGE | LVS_SINGLESEL | WS_BORDER |
3286 WS_CHILD | WS_TABSTOP | WS_GROUP;
3287 control = msi_dialog_add_control( dialog, rec, WC_LISTVIEWW, style );
3288 if (!control)
3289 return ERROR_FUNCTION_FAILED;
3291 msi_dialog_vcl_add_columns( dialog, control, rec );
3292 msi_dialog_vcl_add_drives( dialog, control );
3294 return ERROR_SUCCESS;
3297 /******************** VolumeSelect Combo ***************************************/
3299 static UINT msi_dialog_volsel_handler( msi_dialog *dialog,
3300 msi_control *control, WPARAM param )
3302 WCHAR text[MAX_PATH];
3303 LPWSTR prop;
3304 BOOL indirect;
3305 int index;
3307 if (HIWORD(param) != CBN_SELCHANGE)
3308 return ERROR_SUCCESS;
3310 index = SendMessageW( control->hwnd, CB_GETCURSEL, 0, 0 );
3311 if ( index == CB_ERR )
3313 ERR("No ComboBox item selected!\n");
3314 return ERROR_FUNCTION_FAILED;
3317 SendMessageW( control->hwnd, CB_GETLBTEXT, index, (LPARAM)text );
3319 indirect = control->attributes & msidbControlAttributesIndirect;
3320 prop = msi_dialog_dup_property( dialog, control->property, indirect );
3322 msi_dialog_set_property( dialog->package, prop, text );
3324 msi_free( prop );
3325 return ERROR_SUCCESS;
3328 static void msi_dialog_vsc_add_drives( msi_dialog *dialog, msi_control *control )
3330 LPWSTR drives, ptr;
3331 DWORD size;
3333 size = GetLogicalDriveStringsW( 0, NULL );
3334 if ( !size ) return;
3336 drives = msi_alloc( (size + 1) * sizeof(WCHAR) );
3337 if ( !drives ) return;
3339 GetLogicalDriveStringsW( size, drives );
3341 ptr = drives;
3342 while (*ptr)
3344 SendMessageW( control->hwnd, CB_ADDSTRING, 0, (LPARAM)ptr );
3345 ptr += lstrlenW(ptr) + 1;
3348 msi_free( drives );
3351 static UINT msi_dialog_volumeselect_combo( msi_dialog *dialog, MSIRECORD *rec )
3353 msi_control *control;
3354 LPCWSTR prop;
3355 DWORD style;
3357 /* FIXME: CBS_OWNERDRAWFIXED */
3358 style = WS_CHILD | WS_VISIBLE | WS_GROUP | WS_TABSTOP |
3359 CBS_DROPDOWNLIST | CBS_SORT | CBS_HASSTRINGS |
3360 WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
3361 control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
3362 if (!control)
3363 return ERROR_FUNCTION_FAILED;
3365 control->attributes = MSI_RecordGetInteger( rec, 8 );
3366 control->handler = msi_dialog_volsel_handler;
3367 prop = MSI_RecordGetString( rec, 9 );
3368 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
3370 msi_dialog_vsc_add_drives( dialog, control );
3372 return ERROR_SUCCESS;
3375 static UINT msi_dialog_hyperlink_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
3377 static const WCHAR hrefW[] = {'h','r','e','f'};
3378 static const WCHAR openW[] = {'o','p','e','n',0};
3379 int len, len_href = sizeof(hrefW) / sizeof(hrefW[0]);
3380 const WCHAR *p, *q;
3381 WCHAR quote = 0;
3382 LITEM item;
3384 item.mask = LIF_ITEMINDEX | LIF_URL;
3385 item.iLink = 0;
3386 item.szUrl[0] = 0;
3388 SendMessageW( control->hwnd, LM_GETITEM, 0, (LPARAM)&item );
3390 p = item.szUrl;
3391 while (*p && *p != '<') p++;
3392 if (!*p++) return ERROR_SUCCESS;
3393 if (toupperW( *p++ ) != 'A' || !isspaceW( *p++ )) return ERROR_SUCCESS;
3394 while (*p && isspaceW( *p )) p++;
3396 len = strlenW( p );
3397 if (len > len_href && !memicmpW( p, hrefW, len_href ))
3399 p += len_href;
3400 while (*p && isspaceW( *p )) p++;
3401 if (!*p || *p++ != '=') return ERROR_SUCCESS;
3402 while (*p && isspaceW( *p )) p++;
3404 if (*p == '\"' || *p == '\'') quote = *p++;
3405 q = p;
3406 if (quote)
3408 while (*q && *q != quote) q++;
3409 if (*q != quote) return ERROR_SUCCESS;
3411 else
3413 while (*q && *q != '>' && !isspaceW( *q )) q++;
3414 if (!*q) return ERROR_SUCCESS;
3416 item.szUrl[q - item.szUrl] = 0;
3417 ShellExecuteW( NULL, openW, p, NULL, NULL, SW_SHOWNORMAL );
3419 return ERROR_SUCCESS;
3422 static UINT msi_dialog_hyperlink( msi_dialog *dialog, MSIRECORD *rec )
3424 msi_control *control;
3425 DWORD style = WS_CHILD | WS_TABSTOP | WS_GROUP;
3426 const WCHAR *text = MSI_RecordGetString( rec, 10 );
3427 int len = strlenW( text );
3428 LITEM item;
3430 control = msi_dialog_add_control( dialog, rec, WC_LINK, style );
3431 if (!control)
3432 return ERROR_FUNCTION_FAILED;
3434 control->attributes = MSI_RecordGetInteger( rec, 8 );
3435 control->handler = msi_dialog_hyperlink_handler;
3437 item.mask = LIF_ITEMINDEX | LIF_STATE | LIF_URL;
3438 item.iLink = 0;
3439 item.state = LIS_ENABLED;
3440 item.stateMask = LIS_ENABLED;
3441 if (len < L_MAX_URL_LENGTH) strcpyW( item.szUrl, text );
3442 else item.szUrl[0] = 0;
3444 SendMessageW( control->hwnd, LM_SETITEM, 0, (LPARAM)&item );
3446 return ERROR_SUCCESS;
3449 static const struct control_handler msi_dialog_handler[] =
3451 { szText, msi_dialog_text_control },
3452 { szPushButton, msi_dialog_button_control },
3453 { szLine, msi_dialog_line_control },
3454 { szBitmap, msi_dialog_bitmap_control },
3455 { szCheckBox, msi_dialog_checkbox_control },
3456 { szScrollableText, msi_dialog_scrolltext_control },
3457 { szComboBox, msi_dialog_combo_control },
3458 { szEdit, msi_dialog_edit_control },
3459 { szMaskedEdit, msi_dialog_maskedit_control },
3460 { szPathEdit, msi_dialog_pathedit_control },
3461 { szProgressBar, msi_dialog_progress_bar },
3462 { szRadioButtonGroup, msi_dialog_radiogroup_control },
3463 { szIcon, msi_dialog_icon_control },
3464 { szSelectionTree, msi_dialog_selection_tree },
3465 { szGroupBox, msi_dialog_group_box },
3466 { szListBox, msi_dialog_list_box },
3467 { szDirectoryCombo, msi_dialog_directory_combo },
3468 { szDirectoryList, msi_dialog_directory_list },
3469 { szVolumeCostList, msi_dialog_volumecost_list },
3470 { szVolumeSelectCombo, msi_dialog_volumeselect_combo },
3471 { szHyperLink, msi_dialog_hyperlink }
3474 #define NUM_CONTROL_TYPES (sizeof msi_dialog_handler/sizeof msi_dialog_handler[0])
3476 static UINT msi_dialog_create_controls( MSIRECORD *rec, LPVOID param )
3478 msi_dialog *dialog = param;
3479 LPCWSTR control_type;
3480 UINT i;
3482 /* find and call the function that can create this type of control */
3483 control_type = MSI_RecordGetString( rec, 3 );
3484 for( i=0; i<NUM_CONTROL_TYPES; i++ )
3485 if (!strcmpiW( msi_dialog_handler[i].control_type, control_type ))
3486 break;
3487 if( i != NUM_CONTROL_TYPES )
3488 msi_dialog_handler[i].func( dialog, rec );
3489 else
3490 ERR("no handler for element type %s\n", debugstr_w(control_type));
3492 return ERROR_SUCCESS;
3495 static UINT msi_dialog_fill_controls( msi_dialog *dialog )
3497 static const WCHAR query[] = {
3498 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
3499 'C','o','n','t','r','o','l',' ','W','H','E','R','E',' ',
3500 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
3501 UINT r;
3502 MSIQUERY *view;
3503 MSIPACKAGE *package = dialog->package;
3505 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
3507 /* query the Control table for all the elements of the control */
3508 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
3509 if( r != ERROR_SUCCESS )
3511 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
3512 return ERROR_INVALID_PARAMETER;
3515 r = MSI_IterateRecords( view, 0, msi_dialog_create_controls, dialog );
3516 msiobj_release( &view->hdr );
3517 return r;
3520 static UINT msi_dialog_reset( msi_dialog *dialog )
3522 /* FIXME: should restore the original values of any properties we changed */
3523 return msi_dialog_evaluate_control_conditions( dialog );
3526 /* figure out the height of 10 point MS Sans Serif */
3527 static INT msi_dialog_get_sans_serif_height( HWND hwnd )
3529 static const WCHAR szSansSerif[] = {
3530 'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0 };
3531 LOGFONTW lf;
3532 TEXTMETRICW tm;
3533 BOOL r;
3534 LONG height = 0;
3535 HFONT hFont, hOldFont;
3536 HDC hdc;
3538 hdc = GetDC( hwnd );
3539 if (hdc)
3541 memset( &lf, 0, sizeof lf );
3542 lf.lfHeight = MulDiv(12, GetDeviceCaps(hdc, LOGPIXELSY), 72);
3543 strcpyW( lf.lfFaceName, szSansSerif );
3544 hFont = CreateFontIndirectW(&lf);
3545 if (hFont)
3547 hOldFont = SelectObject( hdc, hFont );
3548 r = GetTextMetricsW( hdc, &tm );
3549 if (r)
3550 height = tm.tmHeight;
3551 SelectObject( hdc, hOldFont );
3552 DeleteObject( hFont );
3554 ReleaseDC( hwnd, hdc );
3556 return height;
3559 /* fetch the associated record from the Dialog table */
3560 static MSIRECORD *msi_get_dialog_record( msi_dialog *dialog )
3562 static const WCHAR query[] = {
3563 'S','E','L','E','C','T',' ','*',' ',
3564 'F','R','O','M',' ','D','i','a','l','o','g',' ',
3565 'W','H','E','R','E',' ',
3566 '`','D','i','a','l','o','g','`',' ','=',' ','\'','%','s','\'',0};
3567 MSIPACKAGE *package = dialog->package;
3568 MSIRECORD *rec = NULL;
3570 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
3572 rec = MSI_QueryGetRecord( package->db, query, dialog->name );
3573 if( !rec )
3574 WARN("query failed for dialog %s\n", debugstr_w(dialog->name));
3576 return rec;
3579 static void msi_dialog_adjust_dialog_pos( msi_dialog *dialog, MSIRECORD *rec, LPRECT pos )
3581 static const WCHAR szScreenX[] = {'S','c','r','e','e','n','X',0};
3582 static const WCHAR szScreenY[] = {'S','c','r','e','e','n','Y',0};
3584 UINT xres, yres;
3585 POINT center;
3586 SIZE sz;
3587 LONG style;
3589 center.x = MSI_RecordGetInteger( rec, 2 );
3590 center.y = MSI_RecordGetInteger( rec, 3 );
3592 sz.cx = MSI_RecordGetInteger( rec, 4 );
3593 sz.cy = MSI_RecordGetInteger( rec, 5 );
3595 sz.cx = msi_dialog_scale_unit( dialog, sz.cx );
3596 sz.cy = msi_dialog_scale_unit( dialog, sz.cy );
3598 xres = msi_get_property_int( dialog->package->db, szScreenX, 0 );
3599 yres = msi_get_property_int( dialog->package->db, szScreenY, 0 );
3601 center.x = MulDiv( center.x, xres, 100 );
3602 center.y = MulDiv( center.y, yres, 100 );
3604 /* turn the client pos into the window rectangle */
3605 if (dialog->package->center_x && dialog->package->center_y)
3607 pos->left = dialog->package->center_x - sz.cx / 2.0;
3608 pos->right = pos->left + sz.cx;
3609 pos->top = dialog->package->center_y - sz.cy / 2.0;
3610 pos->bottom = pos->top + sz.cy;
3612 else
3614 pos->left = center.x - sz.cx/2;
3615 pos->right = pos->left + sz.cx;
3616 pos->top = center.y - sz.cy/2;
3617 pos->bottom = pos->top + sz.cy;
3619 /* save the center */
3620 dialog->package->center_x = center.x;
3621 dialog->package->center_y = center.y;
3624 dialog->size.cx = sz.cx;
3625 dialog->size.cy = sz.cy;
3627 TRACE("%u %u %u %u\n", pos->left, pos->top, pos->right, pos->bottom);
3629 style = GetWindowLongPtrW( dialog->hwnd, GWL_STYLE );
3630 AdjustWindowRect( pos, style, FALSE );
3633 static void msi_dialog_set_tab_order( msi_dialog *dialog, LPCWSTR first )
3635 struct list tab_chain;
3636 msi_control *control;
3637 HWND prev = HWND_TOP;
3639 list_init( &tab_chain );
3640 if (!(control = msi_dialog_find_control( dialog, first ))) return;
3642 dialog->hWndFocus = control->hwnd;
3643 while (control)
3645 list_remove( &control->entry );
3646 list_add_tail( &tab_chain, &control->entry );
3647 if (!control->tabnext) break;
3648 control = msi_dialog_find_control( dialog, control->tabnext );
3651 LIST_FOR_EACH_ENTRY( control, &tab_chain, msi_control, entry )
3653 SetWindowPos( control->hwnd, prev, 0, 0, 0, 0,
3654 SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOREDRAW |
3655 SWP_NOREPOSITION | SWP_NOSENDCHANGING | SWP_NOSIZE );
3656 prev = control->hwnd;
3659 /* put them back on the main list */
3660 list_move_head( &dialog->controls, &tab_chain );
3663 static LRESULT msi_dialog_oncreate( HWND hwnd, LPCREATESTRUCTW cs )
3665 static const WCHAR df[] = {
3666 'D','e','f','a','u','l','t','U','I','F','o','n','t',0 };
3667 static const WCHAR dfv[] = {
3668 'M','S',' ','S','h','e','l','l',' ','D','l','g',0 };
3669 msi_dialog *dialog = cs->lpCreateParams;
3670 MSIRECORD *rec = NULL;
3671 LPWSTR title = NULL;
3672 RECT pos;
3674 TRACE("%p %p\n", dialog, dialog->package);
3676 dialog->hwnd = hwnd;
3677 SetWindowLongPtrW( hwnd, GWLP_USERDATA, (LONG_PTR) dialog );
3679 rec = msi_get_dialog_record( dialog );
3680 if( !rec )
3682 TRACE("No record found for dialog %s\n", debugstr_w(dialog->name));
3683 return -1;
3686 dialog->scale = msi_dialog_get_sans_serif_height(dialog->hwnd);
3688 msi_dialog_adjust_dialog_pos( dialog, rec, &pos );
3690 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
3692 dialog->default_font = msi_dup_property( dialog->package->db, df );
3693 if (!dialog->default_font)
3695 dialog->default_font = strdupW(dfv);
3696 msiobj_release( &rec->hdr );
3697 if (!dialog->default_font) return -1;
3700 title = msi_get_deformatted_field( dialog->package, rec, 7 );
3701 SetWindowTextW( hwnd, title );
3702 msi_free( title );
3704 SetWindowPos( hwnd, 0, pos.left, pos.top,
3705 pos.right - pos.left, pos.bottom - pos.top,
3706 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW );
3708 msi_dialog_build_font_list( dialog );
3709 msi_dialog_fill_controls( dialog );
3710 msi_dialog_evaluate_control_conditions( dialog );
3711 msi_dialog_set_tab_order( dialog, MSI_RecordGetString( rec, 8 ) );
3712 msiobj_release( &rec->hdr );
3714 return 0;
3717 static LRESULT msi_dialog_oncommand( msi_dialog *dialog, WPARAM param, HWND hwnd )
3719 msi_control *control = NULL;
3721 TRACE("%p %p %08lx\n", dialog, hwnd, param);
3723 switch (param)
3725 case 1: /* enter */
3726 control = msi_dialog_find_control( dialog, dialog->control_default );
3727 break;
3728 case 2: /* escape */
3729 control = msi_dialog_find_control( dialog, dialog->control_cancel );
3730 break;
3731 default:
3732 control = msi_dialog_find_control_by_hwnd( dialog, hwnd );
3735 if( control )
3737 if( control->handler )
3739 control->handler( dialog, control, param );
3740 msi_dialog_evaluate_control_conditions( dialog );
3744 return 0;
3747 static LRESULT msi_dialog_onnotify( msi_dialog *dialog, LPARAM param )
3749 LPNMHDR nmhdr = (LPNMHDR) param;
3750 msi_control *control = msi_dialog_find_control_by_hwnd( dialog, nmhdr->hwndFrom );
3752 TRACE("%p %p\n", dialog, nmhdr->hwndFrom);
3754 if ( control && control->handler )
3755 control->handler( dialog, control, param );
3757 return 0;
3760 static void dialog_setfocus( msi_dialog *dialog )
3762 HWND hwnd = dialog->hWndFocus;
3764 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, TRUE);
3765 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, FALSE);
3766 SetFocus( hwnd );
3767 dialog->hWndFocus = hwnd;
3770 static LRESULT WINAPI MSIDialog_WndProc( HWND hwnd, UINT msg,
3771 WPARAM wParam, LPARAM lParam )
3773 msi_dialog *dialog = (LPVOID) GetWindowLongPtrW( hwnd, GWLP_USERDATA );
3775 TRACE("0x%04x\n", msg);
3777 switch (msg)
3779 case WM_MOVE:
3780 dialog->package->center_x = LOWORD(lParam) + dialog->size.cx / 2.0;
3781 dialog->package->center_y = HIWORD(lParam) + dialog->size.cy / 2.0;
3782 break;
3784 case WM_CREATE:
3785 return msi_dialog_oncreate( hwnd, (LPCREATESTRUCTW)lParam );
3787 case WM_COMMAND:
3788 return msi_dialog_oncommand( dialog, wParam, (HWND)lParam );
3790 case WM_ACTIVATE:
3791 if( LOWORD(wParam) == WA_INACTIVE )
3792 dialog->hWndFocus = GetFocus();
3793 else
3794 dialog_setfocus( dialog );
3795 return 0;
3797 case WM_SETFOCUS:
3798 dialog_setfocus( dialog );
3799 return 0;
3801 /* bounce back to our subclassed static control */
3802 case WM_CTLCOLORSTATIC:
3803 return SendMessageW( (HWND) lParam, WM_CTLCOLORSTATIC, wParam, lParam );
3805 case WM_DESTROY:
3806 dialog->hwnd = NULL;
3807 return 0;
3808 case WM_NOTIFY:
3809 return msi_dialog_onnotify( dialog, lParam );
3811 return DefWindowProcW(hwnd, msg, wParam, lParam);
3814 static void process_pending_messages( HWND hdlg )
3816 MSG msg;
3818 while (PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
3820 if (hdlg && IsDialogMessageW( hdlg, &msg )) continue;
3821 TranslateMessage( &msg );
3822 DispatchMessageW( &msg );
3826 static UINT dialog_run_message_loop( msi_dialog *dialog )
3828 DWORD style;
3829 HWND hwnd;
3831 if( uiThreadId != GetCurrentThreadId() )
3832 return SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_CREATE, 0, (LPARAM) dialog );
3834 /* create the dialog window, don't show it yet */
3835 style = WS_OVERLAPPED;
3836 if( dialog->attributes & msidbDialogAttributesVisible )
3837 style |= WS_VISIBLE;
3839 hwnd = CreateWindowW( szMsiDialogClass, dialog->name, style,
3840 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
3841 NULL, NULL, NULL, dialog );
3842 if( !hwnd )
3844 ERR("Failed to create dialog %s\n", debugstr_w( dialog->name ));
3845 return ERROR_FUNCTION_FAILED;
3848 ShowWindow( hwnd, SW_SHOW );
3849 /* UpdateWindow( hwnd ); - and causes the transparent static controls not to paint */
3851 if( dialog->attributes & msidbDialogAttributesModal )
3853 while( !dialog->finished )
3855 MsgWaitForMultipleObjects( 0, NULL, 0, INFINITE, QS_ALLINPUT );
3856 process_pending_messages( dialog->hwnd );
3859 else
3860 return ERROR_IO_PENDING;
3862 return ERROR_SUCCESS;
3865 static LRESULT WINAPI MSIHiddenWindowProc( HWND hwnd, UINT msg,
3866 WPARAM wParam, LPARAM lParam )
3868 msi_dialog *dialog = (msi_dialog*) lParam;
3870 TRACE("%d %p\n", msg, dialog);
3872 switch (msg)
3874 case WM_MSI_DIALOG_CREATE:
3875 return dialog_run_message_loop( dialog );
3876 case WM_MSI_DIALOG_DESTROY:
3877 msi_dialog_destroy( dialog );
3878 return 0;
3880 return DefWindowProcW( hwnd, msg, wParam, lParam );
3883 static BOOL dialog_register_class( void )
3885 WNDCLASSW cls;
3887 ZeroMemory( &cls, sizeof cls );
3888 cls.lpfnWndProc = MSIDialog_WndProc;
3889 cls.hInstance = NULL;
3890 cls.hIcon = LoadIconW(0, (LPWSTR)IDI_APPLICATION);
3891 cls.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
3892 cls.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
3893 cls.lpszMenuName = NULL;
3894 cls.lpszClassName = szMsiDialogClass;
3896 if( !RegisterClassW( &cls ) )
3897 return FALSE;
3899 cls.lpfnWndProc = MSIHiddenWindowProc;
3900 cls.lpszClassName = szMsiHiddenWindow;
3902 if( !RegisterClassW( &cls ) )
3903 return FALSE;
3905 uiThreadId = GetCurrentThreadId();
3907 hMsiHiddenWindow = CreateWindowW( szMsiHiddenWindow, NULL, WS_OVERLAPPED,
3908 0, 0, 100, 100, NULL, NULL, NULL, NULL );
3909 if( !hMsiHiddenWindow )
3910 return FALSE;
3912 return TRUE;
3915 static msi_dialog *dialog_create( MSIPACKAGE *package, const WCHAR *name, msi_dialog *parent,
3916 control_event_handler event_handler )
3918 MSIRECORD *rec = NULL;
3919 msi_dialog *dialog;
3921 TRACE("%s\n", debugstr_w(name));
3923 if (!hMsiHiddenWindow) dialog_register_class();
3925 /* allocate the structure for the dialog to use */
3926 dialog = msi_alloc_zero( FIELD_OFFSET( msi_dialog, name[strlenW( name ) + 1] ));
3927 if( !dialog )
3928 return NULL;
3929 strcpyW( dialog->name, name );
3930 dialog->parent = parent;
3931 msiobj_addref( &package->hdr );
3932 dialog->package = package;
3933 dialog->event_handler = event_handler;
3934 dialog->finished = 0;
3935 list_init( &dialog->controls );
3936 list_init( &dialog->fonts );
3938 /* verify that the dialog exists */
3939 rec = msi_get_dialog_record( dialog );
3940 if( !rec )
3942 msiobj_release( &package->hdr );
3943 msi_free( dialog );
3944 return NULL;
3946 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
3947 dialog->control_default = strdupW( MSI_RecordGetString( rec, 9 ) );
3948 dialog->control_cancel = strdupW( MSI_RecordGetString( rec, 10 ) );
3949 msiobj_release( &rec->hdr );
3951 return dialog;
3954 static void msi_dialog_end_dialog( msi_dialog *dialog )
3956 TRACE("%p\n", dialog);
3957 dialog->finished = 1;
3958 PostMessageW(dialog->hwnd, WM_NULL, 0, 0);
3961 void msi_dialog_check_messages( HANDLE handle )
3963 DWORD r;
3965 /* in threads other than the UI thread, block */
3966 if( uiThreadId != GetCurrentThreadId() )
3968 if (!handle) return;
3969 while (MsgWaitForMultipleObjectsEx( 1, &handle, INFINITE, QS_ALLINPUT, 0 ) == WAIT_OBJECT_0 + 1)
3971 MSG msg;
3972 while (PeekMessageW( &msg, NULL, 0, 0, PM_REMOVE ))
3974 TranslateMessage( &msg );
3975 DispatchMessageW( &msg );
3978 return;
3981 /* there are two choices for the UI thread */
3982 while (1)
3984 process_pending_messages( NULL );
3986 if( !handle )
3987 break;
3990 * block here until somebody creates a new dialog or
3991 * the handle we're waiting on becomes ready
3993 r = MsgWaitForMultipleObjects( 1, &handle, 0, INFINITE, QS_ALLINPUT );
3994 if( r == WAIT_OBJECT_0 )
3995 break;
3999 static void dialog_do_preview( msi_dialog *dialog )
4001 TRACE("\n");
4002 dialog->attributes |= msidbDialogAttributesVisible;
4003 dialog->attributes &= ~msidbDialogAttributesModal;
4004 dialog_run_message_loop( dialog );
4007 static void free_subscriber( struct subscriber *sub )
4009 msi_free( sub->event );
4010 msi_free( sub->control );
4011 msi_free( sub->attribute );
4012 msi_free( sub );
4015 static void event_cleanup_subscriptions( MSIPACKAGE *package, const WCHAR *dialog )
4017 struct list *item, *next;
4019 LIST_FOR_EACH_SAFE( item, next, &package->subscriptions )
4021 struct subscriber *sub = LIST_ENTRY( item, struct subscriber, entry );
4023 if (strcmpW( sub->dialog->name, dialog )) continue;
4024 list_remove( &sub->entry );
4025 free_subscriber( sub );
4029 void msi_dialog_destroy( msi_dialog *dialog )
4031 msi_font *font, *next;
4033 if( uiThreadId != GetCurrentThreadId() )
4035 SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_DESTROY, 0, (LPARAM) dialog );
4036 return;
4039 if( dialog->hwnd )
4040 ShowWindow( dialog->hwnd, SW_HIDE );
4042 if( dialog->hwnd )
4043 DestroyWindow( dialog->hwnd );
4045 /* unsubscribe events */
4046 event_cleanup_subscriptions( dialog->package, dialog->name );
4048 /* destroy the list of controls */
4049 while( !list_empty( &dialog->controls ) )
4051 msi_control *t;
4053 t = LIST_ENTRY( list_head( &dialog->controls ),
4054 msi_control, entry );
4055 msi_destroy_control( t );
4058 /* destroy the list of fonts */
4059 LIST_FOR_EACH_ENTRY_SAFE( font, next, &dialog->fonts, msi_font, entry )
4061 list_remove( &font->entry );
4062 DeleteObject( font->hfont );
4063 msi_free( font );
4065 msi_free( dialog->default_font );
4067 msi_free( dialog->control_default );
4068 msi_free( dialog->control_cancel );
4069 msiobj_release( &dialog->package->hdr );
4070 dialog->package = NULL;
4071 msi_free( dialog );
4074 void msi_dialog_unregister_class( void )
4076 DestroyWindow( hMsiHiddenWindow );
4077 hMsiHiddenWindow = NULL;
4078 UnregisterClassW( szMsiDialogClass, NULL );
4079 UnregisterClassW( szMsiHiddenWindow, NULL );
4080 uiThreadId = 0;
4083 void msi_event_cleanup_all_subscriptions( MSIPACKAGE *package )
4085 struct list *item, *next;
4087 LIST_FOR_EACH_SAFE( item, next, &package->subscriptions )
4089 struct subscriber *sub = LIST_ENTRY( item, struct subscriber, entry );
4090 list_remove( &sub->entry );
4091 free_subscriber( sub );
4095 static UINT error_dialog_handler( msi_dialog *dialog, const WCHAR *event, const WCHAR *argument )
4097 static const WCHAR end_dialog[] = {'E','n','d','D','i','a','l','o','g',0};
4098 static const WCHAR error_abort[] = {'E','r','r','o','r','A','b','o','r','t',0};
4099 static const WCHAR error_cancel[] = {'E','r','r','o','r','C','a','n','c','e','l',0};
4100 static const WCHAR error_no[] = {'E','r','r','o','r','N','o',0};
4101 static const WCHAR result_prop[] = {
4102 'M','S','I','E','r','r','o','r','D','i','a','l','o','g','R','e','s','u','l','t',0
4105 if ( strcmpW( event, end_dialog ) )
4106 return ERROR_SUCCESS;
4108 if ( !strcmpW( argument, error_abort ) || !strcmpW( argument, error_cancel ) ||
4109 !strcmpW( argument, error_no ) )
4111 msi_set_property( dialog->package->db, result_prop, error_abort, -1 );
4114 msi_event_cleanup_all_subscriptions( dialog->package );
4115 msi_dialog_end_dialog( dialog );
4117 return ERROR_SUCCESS;
4120 static UINT msi_error_dialog_set_error( MSIPACKAGE *package, LPWSTR error_dialog, LPWSTR error )
4122 MSIRECORD * row;
4124 static const WCHAR update[] =
4125 {'U','P','D','A','T','E',' ','`','C','o','n','t','r','o','l','`',' ',
4126 'S','E','T',' ','`','T','e','x','t','`',' ','=',' ','\'','%','s','\'',' ',
4127 'W','H','E','R','E', ' ','`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
4128 'A','N','D',' ','`','C','o','n','t','r','o','l','`',' ','=',' ',
4129 '\'','E','r','r','o','r','T','e','x','t','\'',0};
4131 row = MSI_QueryGetRecord( package->db, update, error, error_dialog );
4132 if (!row)
4133 return ERROR_FUNCTION_FAILED;
4135 msiobj_release(&row->hdr);
4136 return ERROR_SUCCESS;
4139 UINT msi_spawn_error_dialog( MSIPACKAGE *package, LPWSTR error_dialog, LPWSTR error )
4141 msi_dialog *dialog;
4142 WCHAR result[MAX_PATH];
4143 UINT r = ERROR_SUCCESS;
4144 DWORD size = MAX_PATH;
4145 int res;
4147 static const WCHAR pn_prop[] = {'P','r','o','d','u','c','t','N','a','m','e',0};
4148 static const WCHAR title_fmt[] = {'%','s',' ','W','a','r','n','i','n','g',0};
4149 static const WCHAR error_abort[] = {'E','r','r','o','r','A','b','o','r','t',0};
4150 static const WCHAR result_prop[] = {
4151 'M','S','I','E','r','r','o','r','D','i','a','l','o','g','R','e','s','u','l','t',0
4154 if ((package->ui_level & INSTALLUILEVEL_MASK) == INSTALLUILEVEL_NONE) return ERROR_SUCCESS;
4156 if ( !error_dialog )
4158 LPWSTR product_name = msi_dup_property( package->db, pn_prop );
4159 WCHAR title[MAX_PATH];
4161 sprintfW( title, title_fmt, product_name );
4162 res = MessageBoxW( NULL, error, title, MB_OKCANCEL | MB_ICONWARNING );
4164 msi_free( product_name );
4166 if ( res == IDOK )
4167 return ERROR_SUCCESS;
4168 else
4169 return ERROR_FUNCTION_FAILED;
4172 r = msi_error_dialog_set_error( package, error_dialog, error );
4173 if ( r != ERROR_SUCCESS )
4174 return r;
4176 dialog = dialog_create( package, error_dialog, package->dialog, error_dialog_handler );
4177 if ( !dialog )
4178 return ERROR_FUNCTION_FAILED;
4180 dialog->finished = FALSE;
4181 r = dialog_run_message_loop( dialog );
4182 if ( r != ERROR_SUCCESS )
4183 goto done;
4185 r = msi_get_property( package->db, result_prop, result, &size );
4186 if ( r != ERROR_SUCCESS)
4187 r = ERROR_SUCCESS;
4189 if ( !strcmpW( result, error_abort ) )
4190 r = ERROR_FUNCTION_FAILED;
4192 done:
4193 msi_dialog_destroy( dialog );
4195 return r;
4198 static void MSI_ClosePreview( MSIOBJECTHDR *arg )
4200 MSIPREVIEW *preview = (MSIPREVIEW *)arg;
4201 msiobj_release( &preview->package->hdr );
4204 static MSIPREVIEW *MSI_EnableUIPreview( MSIDATABASE *db )
4206 MSIPREVIEW *preview = NULL;
4207 MSIPACKAGE *package;
4209 package = MSI_CreatePackage( db, NULL );
4210 if (package)
4212 preview = alloc_msiobject( MSIHANDLETYPE_PREVIEW, sizeof(MSIPREVIEW), MSI_ClosePreview );
4213 if (preview)
4215 preview->package = package;
4216 msiobj_addref( &package->hdr );
4218 msiobj_release( &package->hdr );
4220 return preview;
4223 UINT WINAPI MsiEnableUIPreview( MSIHANDLE hdb, MSIHANDLE *phPreview )
4225 MSIDATABASE *db;
4226 MSIPREVIEW *preview;
4227 UINT r = ERROR_FUNCTION_FAILED;
4229 TRACE("%d %p\n", hdb, phPreview);
4231 db = msihandle2msiinfo( hdb, MSIHANDLETYPE_DATABASE );
4232 if (!db)
4234 IWineMsiRemoteDatabase *remote_database;
4236 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( hdb );
4237 if (!remote_database)
4238 return ERROR_INVALID_HANDLE;
4240 *phPreview = 0;
4242 IWineMsiRemoteDatabase_Release( remote_database );
4243 WARN("MsiEnableUIPreview not allowed during a custom action!\n");
4245 return ERROR_FUNCTION_FAILED;
4247 preview = MSI_EnableUIPreview( db );
4248 if (preview)
4250 *phPreview = alloc_msihandle( &preview->hdr );
4251 msiobj_release( &preview->hdr );
4252 r = ERROR_SUCCESS;
4253 if (!*phPreview)
4254 r = ERROR_NOT_ENOUGH_MEMORY;
4256 msiobj_release( &db->hdr );
4257 return r;
4260 static UINT preview_event_handler( msi_dialog *dialog, const WCHAR *event, const WCHAR *argument )
4262 MESSAGE("Preview dialog event '%s' (arg='%s')\n", debugstr_w(event), debugstr_w(argument));
4263 return ERROR_SUCCESS;
4266 static UINT MSI_PreviewDialogW( MSIPREVIEW *preview, LPCWSTR szDialogName )
4268 msi_dialog *dialog = NULL;
4269 UINT r = ERROR_SUCCESS;
4271 if (preview->dialog)
4272 msi_dialog_destroy( preview->dialog );
4274 /* an empty name means we should just destroy the current preview dialog */
4275 if (szDialogName)
4277 dialog = dialog_create( preview->package, szDialogName, NULL, preview_event_handler );
4278 if (dialog)
4279 dialog_do_preview( dialog );
4280 else
4281 r = ERROR_FUNCTION_FAILED;
4283 preview->dialog = dialog;
4284 return r;
4287 UINT WINAPI MsiPreviewDialogW( MSIHANDLE hPreview, LPCWSTR szDialogName )
4289 MSIPREVIEW *preview;
4290 UINT r;
4292 TRACE("%d %s\n", hPreview, debugstr_w(szDialogName));
4294 preview = msihandle2msiinfo( hPreview, MSIHANDLETYPE_PREVIEW );
4295 if (!preview)
4296 return ERROR_INVALID_HANDLE;
4298 r = MSI_PreviewDialogW( preview, szDialogName );
4299 msiobj_release( &preview->hdr );
4300 return r;
4303 UINT WINAPI MsiPreviewDialogA( MSIHANDLE hPreview, LPCSTR szDialogName )
4305 UINT r;
4306 LPWSTR strW = NULL;
4308 TRACE("%d %s\n", hPreview, debugstr_a(szDialogName));
4310 if (szDialogName)
4312 strW = strdupAtoW( szDialogName );
4313 if (!strW)
4314 return ERROR_OUTOFMEMORY;
4316 r = MsiPreviewDialogW( hPreview, strW );
4317 msi_free( strW );
4318 return r;
4321 UINT WINAPI MsiPreviewBillboardW( MSIHANDLE hPreview, LPCWSTR szControlName, LPCWSTR szBillboard )
4323 FIXME("%d %s %s\n", hPreview, debugstr_w(szControlName), debugstr_w(szBillboard));
4324 return ERROR_CALL_NOT_IMPLEMENTED;
4327 UINT WINAPI MsiPreviewBillboardA( MSIHANDLE hPreview, LPCSTR szControlName, LPCSTR szBillboard )
4329 FIXME("%d %s %s\n", hPreview, debugstr_a(szControlName), debugstr_a(szBillboard));
4330 return ERROR_CALL_NOT_IMPLEMENTED;
4333 typedef UINT (*event_handler)( msi_dialog *, const WCHAR * );
4335 struct control_event
4337 const WCHAR *event;
4338 event_handler handler;
4341 static UINT dialog_event_handler( msi_dialog *, const WCHAR *, const WCHAR * );
4343 /* create a dialog box and run it if it's modal */
4344 static UINT event_do_dialog( MSIPACKAGE *package, const WCHAR *name, msi_dialog *parent, BOOL destroy_modeless )
4346 msi_dialog *dialog;
4347 UINT r;
4349 /* create a new dialog */
4350 dialog = dialog_create( package, name, parent, dialog_event_handler );
4351 if (dialog)
4353 /* kill the current modeless dialog */
4354 if (destroy_modeless && package->dialog)
4356 msi_dialog_destroy( package->dialog );
4357 package->dialog = NULL;
4360 /* modeless dialogs return an error message */
4361 r = dialog_run_message_loop( dialog );
4362 if (r == ERROR_SUCCESS)
4363 msi_dialog_destroy( dialog );
4364 else
4365 package->dialog = dialog;
4367 else r = ERROR_FUNCTION_FAILED;
4368 return r;
4371 /* end a modal dialog box */
4372 static UINT event_end_dialog( msi_dialog *dialog, const WCHAR *argument )
4374 static const WCHAR exitW[] = {'E','x','i','t',0};
4375 static const WCHAR retryW[] = {'R','e','t','r','y',0};
4376 static const WCHAR ignoreW[] = {'I','g','n','o','r','e',0};
4377 static const WCHAR returnW[] = {'R','e','t','u','r','n',0};
4379 if (!strcmpW( argument, exitW ))
4380 dialog->package->CurrentInstallState = ERROR_INSTALL_USEREXIT;
4381 else if (!strcmpW( argument, retryW ))
4382 dialog->package->CurrentInstallState = ERROR_INSTALL_SUSPEND;
4383 else if (!strcmpW( argument, ignoreW ))
4384 dialog->package->CurrentInstallState = ERROR_SUCCESS;
4385 else if (!strcmpW( argument, returnW ))
4387 msi_dialog *parent = dialog->parent;
4388 msi_free( dialog->package->next_dialog );
4389 dialog->package->next_dialog = (parent) ? strdupW( parent->name ) : NULL;
4390 dialog->package->CurrentInstallState = ERROR_SUCCESS;
4392 else
4394 ERR("Unknown argument string %s\n", debugstr_w(argument));
4395 dialog->package->CurrentInstallState = ERROR_FUNCTION_FAILED;
4397 event_cleanup_subscriptions( dialog->package, dialog->name );
4398 msi_dialog_end_dialog( dialog );
4399 return ERROR_SUCCESS;
4402 /* transition from one modal dialog to another modal dialog */
4403 static UINT event_new_dialog( msi_dialog *dialog, const WCHAR *argument )
4405 /* store the name of the next dialog, and signal this one to end */
4406 dialog->package->next_dialog = strdupW( argument );
4407 msi_event_cleanup_all_subscriptions( dialog->package );
4408 msi_dialog_end_dialog( dialog );
4409 return ERROR_SUCCESS;
4412 /* create a new child dialog of an existing modal dialog */
4413 static UINT event_spawn_dialog( msi_dialog *dialog, const WCHAR *argument )
4415 /* don't destroy a modeless dialogs that might be our parent */
4416 event_do_dialog( dialog->package, argument, dialog, FALSE );
4417 if (dialog->package->CurrentInstallState != ERROR_SUCCESS) msi_dialog_end_dialog( dialog );
4418 return ERROR_SUCCESS;
4421 /* creates a dialog that remains up for a period of time based on a condition */
4422 static UINT event_spawn_wait_dialog( msi_dialog *dialog, const WCHAR *argument )
4424 FIXME("doing nothing\n");
4425 return ERROR_SUCCESS;
4428 static UINT event_do_action( msi_dialog *dialog, const WCHAR *argument )
4430 ACTION_PerformAction( dialog->package, argument, SCRIPT_NONE );
4431 return ERROR_SUCCESS;
4434 static UINT event_add_local( msi_dialog *dialog, const WCHAR *argument )
4436 MSIFEATURE *feature;
4438 LIST_FOR_EACH_ENTRY( feature, &dialog->package->features, MSIFEATURE, entry )
4440 if (!strcmpW( argument, feature->Feature ) || !strcmpW( argument, szAll ))
4442 if (feature->ActionRequest != INSTALLSTATE_LOCAL)
4443 msi_set_property( dialog->package->db, szPreselected, szOne, -1 );
4444 MSI_SetFeatureStateW( dialog->package, feature->Feature, INSTALLSTATE_LOCAL );
4447 return ERROR_SUCCESS;
4450 static UINT event_remove( msi_dialog *dialog, const WCHAR *argument )
4452 MSIFEATURE *feature;
4454 LIST_FOR_EACH_ENTRY( feature, &dialog->package->features, MSIFEATURE, entry )
4456 if (!strcmpW( argument, feature->Feature ) || !strcmpW( argument, szAll ))
4458 if (feature->ActionRequest != INSTALLSTATE_ABSENT)
4459 msi_set_property( dialog->package->db, szPreselected, szOne, -1 );
4460 MSI_SetFeatureStateW( dialog->package, feature->Feature, INSTALLSTATE_ABSENT );
4463 return ERROR_SUCCESS;
4466 static UINT event_add_source( msi_dialog *dialog, const WCHAR *argument )
4468 MSIFEATURE *feature;
4470 LIST_FOR_EACH_ENTRY( feature, &dialog->package->features, MSIFEATURE, entry )
4472 if (!strcmpW( argument, feature->Feature ) || !strcmpW( argument, szAll ))
4474 if (feature->ActionRequest != INSTALLSTATE_SOURCE)
4475 msi_set_property( dialog->package->db, szPreselected, szOne, -1 );
4476 MSI_SetFeatureStateW( dialog->package, feature->Feature, INSTALLSTATE_SOURCE );
4479 return ERROR_SUCCESS;
4482 void msi_event_fire( MSIPACKAGE *package, const WCHAR *event, MSIRECORD *rec )
4484 struct subscriber *sub;
4486 TRACE("firing event %s\n", debugstr_w(event));
4488 LIST_FOR_EACH_ENTRY( sub, &package->subscriptions, struct subscriber, entry )
4490 if (strcmpiW( sub->event, event )) continue;
4491 dialog_handle_event( sub->dialog, sub->control, sub->attribute, rec );
4495 static UINT event_set_target_path( msi_dialog *dialog, const WCHAR *argument )
4497 WCHAR *path = msi_dup_property( dialog->package->db, argument );
4498 MSIRECORD *rec = MSI_CreateRecord( 1 );
4499 UINT r = ERROR_SUCCESS;
4501 MSI_RecordSetStringW( rec, 1, path );
4502 msi_event_fire( dialog->package, szSelectionPath, rec );
4503 if (path)
4505 /* failure to set the path halts the executing of control events */
4506 r = MSI_SetTargetPathW( dialog->package, argument, path );
4507 msi_free( path );
4509 msi_free( &rec->hdr );
4510 return r;
4513 static UINT event_reset( msi_dialog *dialog, const WCHAR *argument )
4515 msi_dialog_reset( dialog );
4516 return ERROR_SUCCESS;
4519 /* Return ERROR_SUCCESS if dialog is process and ERROR_FUNCTION_FAILED
4520 * if the given parameter is not a dialog box
4522 UINT ACTION_DialogBox( MSIPACKAGE *package, const WCHAR *dialog )
4524 UINT r;
4526 if (package->next_dialog) ERR("Already got next dialog... ignoring it\n");
4527 package->next_dialog = NULL;
4529 /* Dialogs are chained by filling in the next_dialog member
4530 * of the package structure, then terminating the current dialog.
4531 * The code below sees the next_dialog member set, and runs the
4532 * next dialog.
4533 * We fall out of the loop below if we come across a modeless
4534 * dialog, as it returns ERROR_IO_PENDING when we try to run
4535 * its message loop.
4537 r = event_do_dialog( package, dialog, NULL, TRUE );
4538 while (r == ERROR_SUCCESS && package->next_dialog)
4540 WCHAR *name = package->next_dialog;
4542 package->next_dialog = NULL;
4543 r = event_do_dialog( package, name, NULL, TRUE );
4544 msi_free( name );
4546 if (r == ERROR_IO_PENDING) r = ERROR_SUCCESS;
4547 return r;
4550 static UINT event_set_install_level( msi_dialog *dialog, const WCHAR *argument )
4552 int level = atolW( argument );
4554 TRACE("setting install level to %d\n", level);
4555 return MSI_SetInstallLevel( dialog->package, level );
4558 static UINT event_directory_list_up( msi_dialog *dialog, const WCHAR *argument )
4560 return msi_dialog_directorylist_up( dialog );
4563 static UINT event_reinstall_mode( msi_dialog *dialog, const WCHAR *argument )
4565 return msi_set_property( dialog->package->db, szReinstallMode, argument, -1 );
4568 static UINT event_reinstall( msi_dialog *dialog, const WCHAR *argument )
4570 return msi_set_property( dialog->package->db, szReinstall, argument, -1 );
4573 static UINT event_validate_product_id( msi_dialog *dialog, const WCHAR *argument )
4575 return msi_validate_product_id( dialog->package );
4578 static const WCHAR end_dialogW[] = {'E','n','d','D','i','a','l','o','g',0};
4579 static const WCHAR new_dialogW[] = {'N','e','w','D','i','a','l','o','g',0};
4580 static const WCHAR spawn_dialogW[] = {'S','p','a','w','n','D','i','a','l','o','g',0};
4581 static const WCHAR spawn_wait_dialogW[] = {'S','p','a','w','n','W','a','i','t','D','i','a','l','o','g',0};
4582 static const WCHAR do_actionW[] = {'D','o','A','c','t','i','o','n',0};
4583 static const WCHAR add_localW[] = {'A','d','d','L','o','c','a','l',0};
4584 static const WCHAR removeW[] = {'R','e','m','o','v','e',0};
4585 static const WCHAR add_sourceW[] = {'A','d','d','S','o','u','r','c','e',0};
4586 static const WCHAR set_target_pathW[] = {'S','e','t','T','a','r','g','e','t','P','a','t','h',0};
4587 static const WCHAR resetW[] = {'R','e','s','e','t',0};
4588 static const WCHAR set_install_levelW[] = {'S','e','t','I','n','s','t','a','l','l','L','e','v','e','l',0};
4589 static const WCHAR directory_list_upW[] = {'D','i','r','e','c','t','o','r','y','L','i','s','t','U','p',0};
4590 static const WCHAR selection_browseW[] = {'S','e','l','e','c','t','i','o','n','B','r','o','w','s','e',0};
4591 static const WCHAR reinstall_modeW[] = {'R','e','i','n','s','t','a','l','l','M','o','d','e',0};
4592 static const WCHAR reinstallW[] = {'R','e','i','n','s','t','a','l','l',0};
4593 static const WCHAR validate_product_idW[] = {'V','a','l','i','d','a','t','e','P','r','o','d','u','c','t','I','D',0};
4595 static const struct control_event control_events[] =
4597 { end_dialogW, event_end_dialog },
4598 { new_dialogW, event_new_dialog },
4599 { spawn_dialogW, event_spawn_dialog },
4600 { spawn_wait_dialogW, event_spawn_wait_dialog },
4601 { do_actionW, event_do_action },
4602 { add_localW, event_add_local },
4603 { removeW, event_remove },
4604 { add_sourceW, event_add_source },
4605 { set_target_pathW, event_set_target_path },
4606 { resetW, event_reset },
4607 { set_install_levelW, event_set_install_level },
4608 { directory_list_upW, event_directory_list_up },
4609 { selection_browseW, event_spawn_dialog },
4610 { reinstall_modeW, event_reinstall_mode },
4611 { reinstallW, event_reinstall },
4612 { validate_product_idW, event_validate_product_id },
4613 { NULL, NULL }
4616 static UINT dialog_event_handler( msi_dialog *dialog, const WCHAR *event, const WCHAR *argument )
4618 unsigned int i;
4620 TRACE("handling event %s\n", debugstr_w(event));
4622 if (!event) return ERROR_SUCCESS;
4624 for (i = 0; control_events[i].event; i++)
4626 if (!strcmpW( control_events[i].event, event ))
4627 return control_events[i].handler( dialog, argument );
4629 FIXME("unhandled event %s arg(%s)\n", debugstr_w(event), debugstr_w(argument));
4630 return ERROR_SUCCESS;