mshtml/tests: Fix a typo in ok() message.
[wine.git] / dlls / msi / dialog.c
blob8a28e4d4af0779323a69e0cb3b41a1cae47e04f1
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2005 Mike McCormack for CodeWeavers
5 * Copyright 2005 Aric Stewart for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define COBJMACROS
23 #define NONAMELESSUNION
25 #include <stdarg.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "winnls.h"
32 #include "msi.h"
33 #include "msidefs.h"
34 #include "ocidl.h"
35 #include "olectl.h"
36 #include "richedit.h"
37 #include "commctrl.h"
38 #include "winreg.h"
39 #include "shlwapi.h"
40 #include "shellapi.h"
42 #include "wine/debug.h"
44 #include "msipriv.h"
45 #include "msiserver.h"
46 #include "resource.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 * );
57 typedef UINT (*event_handler)( msi_dialog *, const WCHAR * );
59 struct msi_control_tag
61 struct list entry;
62 HWND hwnd;
63 msi_handler handler;
64 msi_update update;
65 LPWSTR property;
66 LPWSTR value;
67 HBITMAP hBitmap;
68 HICON hIcon;
69 LPWSTR tabnext;
70 LPWSTR type;
71 HMODULE hDll;
72 float progress_current;
73 float progress_max;
74 BOOL progress_backwards;
75 DWORD attributes;
76 WCHAR name[1];
79 typedef struct msi_font_tag
81 struct list entry;
82 HFONT hfont;
83 COLORREF color;
84 WCHAR name[1];
85 } msi_font;
87 struct msi_dialog_tag
89 MSIPACKAGE *package;
90 msi_dialog *parent;
91 control_event_handler event_handler;
92 BOOL finished;
93 INT scale;
94 DWORD attributes;
95 SIZE size;
96 HWND hwnd;
97 LPWSTR default_font;
98 struct list fonts;
99 struct list controls;
100 HWND hWndFocus;
101 LPWSTR control_default;
102 LPWSTR control_cancel;
103 event_handler pending_event;
104 LPWSTR pending_argument;
105 INT retval;
106 WCHAR name[1];
109 struct subscriber
111 struct list entry;
112 msi_dialog *dialog;
113 WCHAR *event;
114 WCHAR *control;
115 WCHAR *attribute;
118 typedef UINT (*msi_dialog_control_func)( msi_dialog *dialog, MSIRECORD *rec );
119 struct control_handler
121 LPCWSTR control_type;
122 msi_dialog_control_func func;
125 typedef struct
127 msi_dialog* dialog;
128 msi_control *parent;
129 LPWSTR propval;
130 } radio_button_group_descr;
132 static const WCHAR szMsiDialogClass[] = { 'M','s','i','D','i','a','l','o','g','C','l','o','s','e','C','l','a','s','s',0 };
133 static const WCHAR szMsiHiddenWindow[] = { 'M','s','i','H','i','d','d','e','n','W','i','n','d','o','w',0 };
134 static const WCHAR szStatic[] = { 'S','t','a','t','i','c',0 };
135 static const WCHAR szButton[] = { 'B','U','T','T','O','N', 0 };
136 static const WCHAR szButtonData[] = { 'M','S','I','D','A','T','A',0 };
137 static const WCHAR szProgress[] = { 'P','r','o','g','r','e','s','s',0 };
138 static const WCHAR szText[] = { 'T','e','x','t',0 };
139 static const WCHAR szPushButton[] = { 'P','u','s','h','B','u','t','t','o','n',0 };
140 static const WCHAR szLine[] = { 'L','i','n','e',0 };
141 static const WCHAR szBitmap[] = { 'B','i','t','m','a','p',0 };
142 static const WCHAR szCheckBox[] = { 'C','h','e','c','k','B','o','x',0 };
143 static const WCHAR szScrollableText[] = { 'S','c','r','o','l','l','a','b','l','e','T','e','x','t',0 };
144 static const WCHAR szComboBox[] = { 'C','o','m','b','o','B','o','x',0 };
145 static const WCHAR szEdit[] = { 'E','d','i','t',0 };
146 static const WCHAR szMaskedEdit[] = { 'M','a','s','k','e','d','E','d','i','t',0 };
147 static const WCHAR szPathEdit[] = { 'P','a','t','h','E','d','i','t',0 };
148 static const WCHAR szProgressBar[] = { 'P','r','o','g','r','e','s','s','B','a','r',0 };
149 static const WCHAR szSetProgress[] = { 'S','e','t','P','r','o','g','r','e','s','s',0 };
150 static const WCHAR szRadioButtonGroup[] = { 'R','a','d','i','o','B','u','t','t','o','n','G','r','o','u','p',0 };
151 static const WCHAR szIcon[] = { 'I','c','o','n',0 };
152 static const WCHAR szSelectionTree[] = { 'S','e','l','e','c','t','i','o','n','T','r','e','e',0 };
153 static const WCHAR szGroupBox[] = { 'G','r','o','u','p','B','o','x',0 };
154 static const WCHAR szListBox[] = { 'L','i','s','t','B','o','x',0 };
155 static const WCHAR szDirectoryCombo[] = { 'D','i','r','e','c','t','o','r','y','C','o','m','b','o',0 };
156 static const WCHAR szDirectoryList[] = { 'D','i','r','e','c','t','o','r','y','L','i','s','t',0 };
157 static const WCHAR szVolumeCostList[] = { 'V','o','l','u','m','e','C','o','s','t','L','i','s','t',0 };
158 static const WCHAR szVolumeSelectCombo[] = { 'V','o','l','u','m','e','S','e','l','e','c','t','C','o','m','b','o',0 };
159 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};
160 static const WCHAR szSelectionPath[] = {'S','e','l','e','c','t','i','o','n','P','a','t','h',0};
161 static const WCHAR szHyperLink[] = {'H','y','p','e','r','L','i','n','k',0};
163 /* dialog sequencing */
165 #define WM_MSI_DIALOG_CREATE (WM_USER+0x100)
166 #define WM_MSI_DIALOG_DESTROY (WM_USER+0x101)
168 #define USER_INSTALLSTATE_ALL 0x1000
170 static DWORD uiThreadId;
171 static HWND hMsiHiddenWindow;
173 static LPWSTR msi_get_window_text( HWND hwnd )
175 UINT sz, r;
176 LPWSTR buf;
178 sz = 0x20;
179 buf = msi_alloc( sz*sizeof(WCHAR) );
180 while ( buf )
182 r = GetWindowTextW( hwnd, buf, sz );
183 if ( r < (sz - 1) )
184 break;
185 sz *= 2;
186 buf = msi_realloc( buf, sz*sizeof(WCHAR) );
189 return buf;
192 static INT msi_dialog_scale_unit( msi_dialog *dialog, INT val )
194 return MulDiv( val, dialog->scale, 12 );
197 static msi_control *msi_dialog_find_control( msi_dialog *dialog, LPCWSTR name )
199 msi_control *control;
201 if( !name )
202 return NULL;
203 if( !dialog->hwnd )
204 return NULL;
205 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
206 if( !wcscmp( control->name, name ) ) /* FIXME: case sensitive? */
207 return control;
208 return NULL;
211 static msi_control *msi_dialog_find_control_by_type( msi_dialog *dialog, LPCWSTR type )
213 msi_control *control;
215 if( !type )
216 return NULL;
217 if( !dialog->hwnd )
218 return NULL;
219 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
220 if( !wcscmp( control->type, type ) ) /* FIXME: case sensitive? */
221 return control;
222 return NULL;
225 static msi_control *msi_dialog_find_control_by_hwnd( msi_dialog *dialog, HWND hwnd )
227 msi_control *control;
229 if( !dialog->hwnd )
230 return NULL;
231 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
232 if( hwnd == control->hwnd )
233 return control;
234 return NULL;
237 static LPWSTR msi_get_deformatted_field( MSIPACKAGE *package, MSIRECORD *rec, int field )
239 LPCWSTR str = MSI_RecordGetString( rec, field );
240 LPWSTR ret = NULL;
242 if (str)
243 deformat_string( package, str, &ret );
244 return ret;
247 static LPWSTR msi_dialog_dup_property( msi_dialog *dialog, LPCWSTR property, BOOL indirect )
249 LPWSTR prop = NULL;
251 if (!property)
252 return NULL;
254 if (indirect)
255 prop = msi_dup_property( dialog->package->db, property );
257 if (!prop)
258 prop = strdupW( property );
260 return prop;
264 * msi_dialog_get_style
266 * Extract the {\style} string from the front of the text to display and
267 * update the pointer. Only the last style in a list is applied.
269 static LPWSTR msi_dialog_get_style( LPCWSTR p, LPCWSTR *rest )
271 LPWSTR ret;
272 LPCWSTR q, i, first;
273 DWORD len;
275 q = NULL;
276 *rest = p;
277 if( !p )
278 return NULL;
280 while ((first = wcschr( p, '{' )) && (q = wcschr( first + 1, '}' )))
282 p = first + 1;
283 if( *p != '\\' && *p != '&' )
284 return NULL;
286 /* little bit of sanity checking to stop us getting confused with RTF */
287 for( i=++p; i<q; i++ )
288 if( *i == '}' || *i == '\\' )
289 return NULL;
292 if (!q)
293 return NULL;
295 *rest = ++q;
296 len = q - p;
298 ret = msi_alloc( len*sizeof(WCHAR) );
299 if( !ret )
300 return ret;
301 memcpy( ret, p, len*sizeof(WCHAR) );
302 ret[len-1] = 0;
303 return ret;
306 static UINT msi_dialog_add_font( MSIRECORD *rec, LPVOID param )
308 msi_dialog *dialog = param;
309 msi_font *font;
310 LPCWSTR face, name;
311 LOGFONTW lf;
312 INT style;
313 HDC hdc;
315 /* create a font and add it to the list */
316 name = MSI_RecordGetString( rec, 1 );
317 font = msi_alloc( FIELD_OFFSET( msi_font, name[lstrlenW( name ) + 1] ));
318 lstrcpyW( font->name, name );
319 list_add_head( &dialog->fonts, &font->entry );
321 font->color = MSI_RecordGetInteger( rec, 4 );
323 memset( &lf, 0, sizeof lf );
324 face = MSI_RecordGetString( rec, 2 );
325 lf.lfHeight = MSI_RecordGetInteger( rec, 3 );
326 style = MSI_RecordGetInteger( rec, 5 );
327 if( style & msidbTextStyleStyleBitsBold )
328 lf.lfWeight = FW_BOLD;
329 if( style & msidbTextStyleStyleBitsItalic )
330 lf.lfItalic = TRUE;
331 if( style & msidbTextStyleStyleBitsUnderline )
332 lf.lfUnderline = TRUE;
333 if( style & msidbTextStyleStyleBitsStrike )
334 lf.lfStrikeOut = TRUE;
335 lstrcpynW( lf.lfFaceName, face, LF_FACESIZE );
337 /* adjust the height */
338 hdc = GetDC( dialog->hwnd );
339 if (hdc)
341 lf.lfHeight = -MulDiv(lf.lfHeight, GetDeviceCaps(hdc, LOGPIXELSY), 72);
342 ReleaseDC( dialog->hwnd, hdc );
345 font->hfont = CreateFontIndirectW( &lf );
347 TRACE("Adding font style %s\n", debugstr_w(font->name) );
349 return ERROR_SUCCESS;
352 static msi_font *msi_dialog_find_font( msi_dialog *dialog, LPCWSTR name )
354 msi_font *font = NULL;
356 LIST_FOR_EACH_ENTRY( font, &dialog->fonts, msi_font, entry )
357 if( !wcscmp( font->name, name ) ) /* FIXME: case sensitive? */
358 break;
360 return font;
363 static UINT msi_dialog_set_font( msi_dialog *dialog, HWND hwnd, LPCWSTR name )
365 msi_font *font;
367 font = msi_dialog_find_font( dialog, name );
368 if( font )
369 SendMessageW( hwnd, WM_SETFONT, (WPARAM) font->hfont, TRUE );
370 else
371 ERR("No font entry for %s\n", debugstr_w(name));
372 return ERROR_SUCCESS;
375 static UINT msi_dialog_build_font_list( msi_dialog *dialog )
377 static const WCHAR query[] = {
378 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
379 '`','T','e','x','t','S','t','y','l','e','`',0};
380 MSIQUERY *view;
381 UINT r;
383 TRACE("dialog %p\n", dialog );
385 r = MSI_OpenQuery( dialog->package->db, &view, query );
386 if( r != ERROR_SUCCESS )
387 return r;
389 r = MSI_IterateRecords( view, NULL, msi_dialog_add_font, dialog );
390 msiobj_release( &view->hdr );
391 return r;
394 static void msi_destroy_control( msi_control *t )
396 list_remove( &t->entry );
397 /* leave dialog->hwnd - destroying parent destroys child windows */
398 msi_free( t->property );
399 msi_free( t->value );
400 if( t->hBitmap )
401 DeleteObject( t->hBitmap );
402 if( t->hIcon )
403 DestroyIcon( t->hIcon );
404 msi_free( t->tabnext );
405 msi_free( t->type );
406 if (t->hDll)
407 FreeLibrary( t->hDll );
408 msi_free( t );
411 static msi_control *dialog_create_window( msi_dialog *dialog, MSIRECORD *rec, DWORD exstyle,
412 const WCHAR *szCls, const WCHAR *name, const WCHAR *text,
413 DWORD style, HWND parent )
415 DWORD x, y, width, height;
416 LPWSTR font = NULL, title_font = NULL;
417 LPCWSTR title = NULL;
418 msi_control *control;
420 style |= WS_CHILD;
422 control = msi_alloc( FIELD_OFFSET( msi_control, name[lstrlenW( name ) + 1] ));
423 if (!control)
424 return NULL;
426 lstrcpyW( control->name, name );
427 list_add_tail( &dialog->controls, &control->entry );
428 control->handler = NULL;
429 control->update = NULL;
430 control->property = NULL;
431 control->value = NULL;
432 control->hBitmap = NULL;
433 control->hIcon = NULL;
434 control->hDll = NULL;
435 control->tabnext = strdupW( MSI_RecordGetString( rec, 11) );
436 control->type = strdupW( MSI_RecordGetString( rec, 3 ) );
437 control->progress_current = 0;
438 control->progress_max = 100;
439 control->progress_backwards = FALSE;
441 x = MSI_RecordGetInteger( rec, 4 );
442 y = MSI_RecordGetInteger( rec, 5 );
443 width = MSI_RecordGetInteger( rec, 6 );
444 height = MSI_RecordGetInteger( rec, 7 );
446 x = msi_dialog_scale_unit( dialog, x );
447 y = msi_dialog_scale_unit( dialog, y );
448 width = msi_dialog_scale_unit( dialog, width );
449 height = msi_dialog_scale_unit( dialog, height );
451 if( text )
453 deformat_string( dialog->package, text, &title_font );
454 font = msi_dialog_get_style( title_font, &title );
457 control->hwnd = CreateWindowExW( exstyle, szCls, title, style,
458 x, y, width, height, parent, NULL, NULL, NULL );
460 TRACE("Dialog %s control %s hwnd %p\n",
461 debugstr_w(dialog->name), debugstr_w(text), control->hwnd );
463 msi_dialog_set_font( dialog, control->hwnd,
464 font ? font : dialog->default_font );
466 msi_free( title_font );
467 msi_free( font );
469 return control;
472 static LPWSTR msi_dialog_get_uitext( msi_dialog *dialog, LPCWSTR key )
474 MSIRECORD *rec;
475 LPWSTR text;
477 static const WCHAR query[] = {
478 's','e','l','e','c','t',' ','*',' ',
479 'f','r','o','m',' ','`','U','I','T','e','x','t','`',' ',
480 'w','h','e','r','e',' ','`','K','e','y','`',' ','=',' ','\'','%','s','\'',0
483 rec = MSI_QueryGetRecord( dialog->package->db, query, key );
484 if (!rec) return NULL;
485 text = strdupW( MSI_RecordGetString( rec, 2 ) );
486 msiobj_release( &rec->hdr );
487 return text;
490 static MSIRECORD *msi_get_binary_record( MSIDATABASE *db, LPCWSTR name )
492 static const WCHAR query[] = {
493 's','e','l','e','c','t',' ','*',' ',
494 'f','r','o','m',' ','B','i','n','a','r','y',' ',
495 'w','h','e','r','e',' ',
496 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0
499 return MSI_QueryGetRecord( db, query, name );
502 static HANDLE msi_load_image( MSIDATABASE *db, LPCWSTR name, UINT type,
503 UINT cx, UINT cy, UINT flags )
505 MSIRECORD *rec;
506 HANDLE himage = NULL;
507 LPWSTR tmp;
508 UINT r;
510 TRACE("%p %s %u %u %08x\n", db, debugstr_w(name), cx, cy, flags);
512 if (!(tmp = msi_create_temp_file( db ))) return NULL;
514 rec = msi_get_binary_record( db, name );
515 if( rec )
517 r = MSI_RecordStreamToFile( rec, 2, tmp );
518 if( r == ERROR_SUCCESS )
520 himage = LoadImageW( 0, tmp, type, cx, cy, flags );
522 msiobj_release( &rec->hdr );
524 DeleteFileW( tmp );
526 msi_free( tmp );
527 return himage;
530 static HICON msi_load_icon( MSIDATABASE *db, LPCWSTR text, UINT attributes )
532 DWORD cx = 0, cy = 0, flags;
534 flags = LR_LOADFROMFILE | LR_DEFAULTSIZE;
535 if( attributes & msidbControlAttributesFixedSize )
537 flags &= ~LR_DEFAULTSIZE;
538 if( attributes & msidbControlAttributesIconSize16 )
540 cx += 16;
541 cy += 16;
543 if( attributes & msidbControlAttributesIconSize32 )
545 cx += 32;
546 cy += 32;
548 /* msidbControlAttributesIconSize48 handled by above logic */
550 return msi_load_image( db, text, IMAGE_ICON, cx, cy, flags );
553 static void msi_dialog_update_controls( msi_dialog *dialog, LPCWSTR property )
555 msi_control *control;
557 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
559 if ( control->property && !wcscmp( control->property, property ) && control->update )
560 control->update( dialog, control );
564 static void msi_dialog_update_all_controls( msi_dialog *dialog )
566 msi_control *control;
568 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
570 if ( control->property && control->update )
571 control->update( dialog, control );
575 static void msi_dialog_set_property( MSIPACKAGE *package, LPCWSTR property, LPCWSTR value )
577 UINT r = msi_set_property( package->db, property, value, -1 );
578 if (r == ERROR_SUCCESS && !wcscmp( property, szSourceDir ))
579 msi_reset_source_folders( package );
582 static MSIFEATURE *msi_seltree_feature_from_item( HWND hwnd, HTREEITEM hItem )
584 TVITEMW tvi;
586 /* get the feature from the item */
587 memset( &tvi, 0, sizeof tvi );
588 tvi.hItem = hItem;
589 tvi.mask = TVIF_PARAM | TVIF_HANDLE;
590 SendMessageW( hwnd, TVM_GETITEMW, 0, (LPARAM)&tvi );
591 return (MSIFEATURE *)tvi.lParam;
594 struct msi_selection_tree_info
596 msi_dialog *dialog;
597 HWND hwnd;
598 WNDPROC oldproc;
599 HTREEITEM selected;
602 static MSIFEATURE *msi_seltree_get_selected_feature( msi_control *control )
604 struct msi_selection_tree_info *info = GetPropW( control->hwnd, szButtonData );
605 return msi_seltree_feature_from_item( control->hwnd, info->selected );
608 static void dialog_handle_event( msi_dialog *dialog, const WCHAR *control,
609 const WCHAR *attribute, MSIRECORD *rec )
611 msi_control* ctrl;
613 ctrl = msi_dialog_find_control( dialog, control );
614 if (!ctrl)
615 return;
616 if( !wcscmp( attribute, szText ) )
618 const WCHAR *font_text, *text = NULL;
619 WCHAR *font, *text_fmt = NULL;
621 font_text = MSI_RecordGetString( rec , 1 );
622 font = msi_dialog_get_style( font_text, &text );
623 deformat_string( dialog->package, text, &text_fmt );
624 if (text_fmt) text = text_fmt;
625 else text = szEmpty;
627 SetWindowTextW( ctrl->hwnd, text );
629 msi_free( font );
630 msi_free( text_fmt );
631 msi_dialog_check_messages( NULL );
633 else if( !wcscmp( attribute, szProgress ) )
635 DWORD func, val1, val2, units;
637 func = MSI_RecordGetInteger( rec, 1 );
638 val1 = MSI_RecordGetInteger( rec, 2 );
639 val2 = MSI_RecordGetInteger( rec, 3 );
641 TRACE("progress: func %u val1 %u val2 %u\n", func, val1, val2);
643 units = val1 / 512;
644 switch (func)
646 case 0: /* init */
647 SendMessageW( ctrl->hwnd, PBM_SETRANGE, 0, MAKELPARAM(0,100) );
648 if (val2)
650 ctrl->progress_max = units ? units : 100;
651 ctrl->progress_current = units;
652 ctrl->progress_backwards = TRUE;
653 SendMessageW( ctrl->hwnd, PBM_SETPOS, 100, 0 );
655 else
657 ctrl->progress_max = units ? units : 100;
658 ctrl->progress_current = 0;
659 ctrl->progress_backwards = FALSE;
660 SendMessageW( ctrl->hwnd, PBM_SETPOS, 0, 0 );
662 break;
663 case 1: /* action data increment */
664 if (val2) dialog->package->action_progress_increment = val1;
665 else dialog->package->action_progress_increment = 0;
666 break;
667 case 2: /* move */
668 if (ctrl->progress_backwards)
670 if (units >= ctrl->progress_current) ctrl->progress_current -= units;
671 else ctrl->progress_current = 0;
673 else
675 if (ctrl->progress_current + units < ctrl->progress_max) ctrl->progress_current += units;
676 else ctrl->progress_current = ctrl->progress_max;
678 SendMessageW( ctrl->hwnd, PBM_SETPOS, MulDiv(100, ctrl->progress_current, ctrl->progress_max), 0 );
679 break;
680 case 3: /* add */
681 ctrl->progress_max += units;
682 break;
683 default:
684 FIXME("Unknown progress message %u\n", func);
685 break;
688 else if ( !wcscmp( attribute, szProperty ) )
690 MSIFEATURE *feature = msi_seltree_get_selected_feature( ctrl );
691 if (feature) msi_dialog_set_property( dialog->package, ctrl->property, feature->Directory );
693 else if ( !wcscmp( attribute, szSelectionPath ) )
695 BOOL indirect = ctrl->attributes & msidbControlAttributesIndirect;
696 LPWSTR path = msi_dialog_dup_property( dialog, ctrl->property, indirect );
697 if (!path) return;
698 SetWindowTextW( ctrl->hwnd, path );
699 msi_free(path);
701 else
703 FIXME("Attribute %s not being set\n", debugstr_w(attribute));
704 return;
708 static void event_subscribe( msi_dialog *dialog, const WCHAR *event, const WCHAR *control, const WCHAR *attribute )
710 struct subscriber *sub;
712 TRACE("dialog %s event %s control %s attribute %s\n", debugstr_w(dialog->name), debugstr_w(event),
713 debugstr_w(control), debugstr_w(attribute));
715 LIST_FOR_EACH_ENTRY( sub, &dialog->package->subscriptions, struct subscriber, entry )
717 if (sub->dialog == dialog &&
718 !wcsicmp( sub->event, event ) &&
719 !wcsicmp( sub->control, control ) &&
720 !wcsicmp( sub->attribute, attribute ))
722 TRACE("already subscribed\n");
723 return;
726 if (!(sub = msi_alloc( sizeof(*sub) ))) return;
727 sub->dialog = dialog;
728 sub->event = strdupW( event );
729 sub->control = strdupW( control );
730 sub->attribute = strdupW( attribute );
731 list_add_tail( &dialog->package->subscriptions, &sub->entry );
734 struct dialog_control
736 msi_dialog *dialog;
737 const WCHAR *control;
740 static UINT map_event( MSIRECORD *row, void *param )
742 struct dialog_control *dc = param;
743 const WCHAR *event = MSI_RecordGetString( row, 3 );
744 const WCHAR *attribute = MSI_RecordGetString( row, 4 );
746 event_subscribe( dc->dialog, event, dc->control, attribute );
747 return ERROR_SUCCESS;
750 static void dialog_map_events( msi_dialog *dialog, const WCHAR *control )
752 static const WCHAR queryW[] =
753 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
754 '`','E','v','e','n','t','M','a','p','p','i','n','g','`',' ',
755 'W','H','E','R','E',' ','`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
756 'A','N','D',' ','`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',0};
757 MSIQUERY *view;
758 struct dialog_control dialog_control =
760 dialog,
761 control
764 if (!MSI_OpenQuery( dialog->package->db, &view, queryW, dialog->name, control ))
766 MSI_IterateRecords( view, NULL, map_event, &dialog_control );
767 msiobj_release( &view->hdr );
771 /* everything except radio buttons */
772 static msi_control *msi_dialog_add_control( msi_dialog *dialog,
773 MSIRECORD *rec, LPCWSTR szCls, DWORD style )
775 DWORD attributes;
776 const WCHAR *text = NULL, *name, *control_type;
777 DWORD exstyle = 0;
779 name = MSI_RecordGetString( rec, 2 );
780 control_type = MSI_RecordGetString( rec, 3 );
781 attributes = MSI_RecordGetInteger( rec, 8 );
782 if (wcscmp( control_type, szScrollableText )) text = MSI_RecordGetString( rec, 10 );
784 TRACE("%s, %s, %08x, %s, %08x\n", debugstr_w(szCls), debugstr_w(name),
785 attributes, debugstr_w(text), style);
787 if( attributes & msidbControlAttributesVisible )
788 style |= WS_VISIBLE;
789 if( ~attributes & msidbControlAttributesEnabled )
790 style |= WS_DISABLED;
791 if( attributes & msidbControlAttributesSunken )
792 exstyle |= WS_EX_CLIENTEDGE;
794 dialog_map_events( dialog, name );
796 return dialog_create_window( dialog, rec, exstyle, szCls, name, text, style, dialog->hwnd );
799 struct msi_text_info
801 msi_font *font;
802 WNDPROC oldproc;
803 DWORD attributes;
807 * we don't erase our own background,
808 * so we have to make sure that the parent window redraws first
810 static void msi_text_on_settext( HWND hWnd )
812 HWND hParent;
813 RECT rc;
815 hParent = GetParent( hWnd );
816 GetWindowRect( hWnd, &rc );
817 MapWindowPoints( NULL, hParent, (LPPOINT) &rc, 2 );
818 InvalidateRect( hParent, &rc, TRUE );
821 static LRESULT WINAPI
822 MSIText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
824 struct msi_text_info *info;
825 LRESULT r = 0;
827 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
829 info = GetPropW(hWnd, szButtonData);
831 if( msg == WM_CTLCOLORSTATIC &&
832 ( info->attributes & msidbControlAttributesTransparent ) )
834 SetBkMode( (HDC)wParam, TRANSPARENT );
835 return (LRESULT) GetStockObject(NULL_BRUSH);
838 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
839 if ( info->font )
840 SetTextColor( (HDC)wParam, info->font->color );
842 switch( msg )
844 case WM_SETTEXT:
845 msi_text_on_settext( hWnd );
846 break;
847 case WM_NCDESTROY:
848 msi_free( info );
849 RemovePropW( hWnd, szButtonData );
850 break;
853 return r;
856 static UINT msi_dialog_text_control( msi_dialog *dialog, MSIRECORD *rec )
858 msi_control *control;
859 struct msi_text_info *info;
860 LPCWSTR text, ptr, prop, control_name;
861 LPWSTR font_name;
863 TRACE("%p %p\n", dialog, rec);
865 control = msi_dialog_add_control( dialog, rec, szStatic, SS_LEFT | WS_GROUP );
866 if( !control )
867 return ERROR_FUNCTION_FAILED;
869 info = msi_alloc( sizeof *info );
870 if( !info )
871 return ERROR_SUCCESS;
873 control_name = MSI_RecordGetString( rec, 2 );
874 control->attributes = MSI_RecordGetInteger( rec, 8 );
875 prop = MSI_RecordGetString( rec, 9 );
876 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
878 text = MSI_RecordGetString( rec, 10 );
879 font_name = msi_dialog_get_style( text, &ptr );
880 info->font = ( font_name ) ? msi_dialog_find_font( dialog, font_name ) : NULL;
881 msi_free( font_name );
883 info->attributes = MSI_RecordGetInteger( rec, 8 );
884 if( info->attributes & msidbControlAttributesTransparent )
885 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT );
887 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
888 (LONG_PTR)MSIText_WndProc );
889 SetPropW( control->hwnd, szButtonData, info );
891 event_subscribe( dialog, szSelectionPath, control_name, szSelectionPath );
892 return ERROR_SUCCESS;
895 /* strip any leading text style label from text field */
896 static WCHAR *msi_get_binary_name( MSIPACKAGE *package, MSIRECORD *rec )
898 WCHAR *p, *text;
900 text = msi_get_deformatted_field( package, rec, 10 );
901 if (!text)
902 return NULL;
904 p = text;
905 while (*p && *p != '{') p++;
906 if (!*p++) return text;
908 while (*p && *p != '}') p++;
909 if (!*p++) return text;
911 p = strdupW( p );
912 msi_free( text );
913 return p;
916 static UINT msi_dialog_set_property_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
918 static const WCHAR szNullArg[] = {'{','}',0};
919 LPWSTR p, prop, arg_fmt = NULL;
920 UINT len;
922 len = lstrlenW( event );
923 prop = msi_alloc( len * sizeof(WCHAR) );
924 lstrcpyW( prop, &event[1] );
925 p = wcschr( prop, ']' );
926 if (p && (p[1] == 0 || p[1] == ' '))
928 *p = 0;
929 if (wcscmp( szNullArg, arg ))
930 deformat_string( dialog->package, arg, &arg_fmt );
931 msi_dialog_set_property( dialog->package, prop, arg_fmt );
932 msi_dialog_update_controls( dialog, prop );
933 msi_free( arg_fmt );
935 else ERR("Badly formatted property string - what happens?\n");
936 msi_free( prop );
937 return ERROR_SUCCESS;
940 static UINT msi_dialog_send_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
942 LPWSTR event_fmt = NULL, arg_fmt = NULL;
944 TRACE("Sending control event %s %s\n", debugstr_w(event), debugstr_w(arg));
946 deformat_string( dialog->package, event, &event_fmt );
947 deformat_string( dialog->package, arg, &arg_fmt );
949 dialog->event_handler( dialog, event_fmt, arg_fmt );
951 msi_free( event_fmt );
952 msi_free( arg_fmt );
954 return ERROR_SUCCESS;
957 static UINT msi_dialog_control_event( MSIRECORD *rec, LPVOID param )
959 msi_dialog *dialog = param;
960 LPCWSTR condition, event, arg;
961 UINT r;
963 condition = MSI_RecordGetString( rec, 5 );
964 r = MSI_EvaluateConditionW( dialog->package, condition );
965 if (r == MSICONDITION_TRUE || r == MSICONDITION_NONE)
967 event = MSI_RecordGetString( rec, 3 );
968 arg = MSI_RecordGetString( rec, 4 );
969 if (event[0] == '[')
970 msi_dialog_set_property_event( dialog, event, arg );
971 else
972 msi_dialog_send_event( dialog, event, arg );
974 return ERROR_SUCCESS;
977 static UINT msi_dialog_button_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
979 static const WCHAR query[] = {
980 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
981 'C','o','n','t','r','o','l','E','v','e','n','t',' ','W','H','E','R','E',' ',
982 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ','A','N','D',' ',
983 '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',' ',
984 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','i','n','g','`',0};
985 MSIQUERY *view;
986 UINT r;
988 if (HIWORD(param) != BN_CLICKED)
989 return ERROR_SUCCESS;
991 r = MSI_OpenQuery( dialog->package->db, &view, query, dialog->name, control->name );
992 if (r != ERROR_SUCCESS)
994 ERR("query failed\n");
995 return ERROR_SUCCESS;
997 r = MSI_IterateRecords( view, 0, msi_dialog_control_event, dialog );
998 msiobj_release( &view->hdr );
1000 /* dialog control events must be processed last regardless of ordering */
1001 if (dialog->pending_event)
1003 r = dialog->pending_event( dialog, dialog->pending_argument );
1005 msi_free( dialog->pending_argument );
1006 dialog->pending_event = NULL;
1007 dialog->pending_argument = NULL;
1009 return r;
1012 static UINT msi_dialog_button_control( msi_dialog *dialog, MSIRECORD *rec )
1014 msi_control *control;
1015 UINT attributes, style;
1017 TRACE("%p %p\n", dialog, rec);
1019 style = WS_TABSTOP;
1020 attributes = MSI_RecordGetInteger( rec, 8 );
1021 if( attributes & msidbControlAttributesIcon )
1022 style |= BS_ICON;
1024 control = msi_dialog_add_control( dialog, rec, szButton, style );
1025 if( !control )
1026 return ERROR_FUNCTION_FAILED;
1028 control->handler = msi_dialog_button_handler;
1030 if (attributes & msidbControlAttributesIcon)
1032 /* set the icon */
1033 LPWSTR name = msi_get_binary_name( dialog->package, rec );
1034 control->hIcon = msi_load_icon( dialog->package->db, name, attributes );
1035 if (control->hIcon)
1037 SendMessageW( control->hwnd, BM_SETIMAGE, IMAGE_ICON, (LPARAM) control->hIcon );
1039 else
1040 ERR("Failed to load icon %s\n", debugstr_w(name));
1041 msi_free( name );
1044 return ERROR_SUCCESS;
1047 static LPWSTR msi_get_checkbox_value( msi_dialog *dialog, LPCWSTR prop )
1049 static const WCHAR query[] = {
1050 'S','E','L','E','C','T',' ','*',' ',
1051 'F','R','O','M',' ','`','C','h','e','c','k','B','o','x','`',' ',
1052 'W','H','E','R','E',' ',
1053 '`','P','r','o','p','e','r','t','y','`',' ','=',' ',
1054 '\'','%','s','\'',0
1056 MSIRECORD *rec = NULL;
1057 LPWSTR ret = NULL;
1059 /* find if there is a value associated with the checkbox */
1060 rec = MSI_QueryGetRecord( dialog->package->db, query, prop );
1061 if (!rec)
1062 return ret;
1064 ret = msi_get_deformatted_field( dialog->package, rec, 2 );
1065 if( ret && !ret[0] )
1067 msi_free( ret );
1068 ret = NULL;
1070 msiobj_release( &rec->hdr );
1071 if (ret)
1072 return ret;
1074 ret = msi_dup_property( dialog->package->db, prop );
1075 if( ret && !ret[0] )
1077 msi_free( ret );
1078 ret = NULL;
1081 return ret;
1084 static UINT msi_dialog_get_checkbox_state( msi_dialog *dialog, msi_control *control )
1086 WCHAR state[2] = {0};
1087 DWORD sz = 2;
1089 msi_get_property( dialog->package->db, control->property, state, &sz );
1090 return state[0] ? 1 : 0;
1093 static void msi_dialog_set_checkbox_state( msi_dialog *dialog, msi_control *control, UINT state )
1095 static const WCHAR szState[] = {'1',0};
1096 LPCWSTR val;
1098 /* if uncheck then the property is set to NULL */
1099 if (!state)
1101 msi_dialog_set_property( dialog->package, control->property, NULL );
1102 return;
1105 /* check for a custom state */
1106 if (control->value && control->value[0])
1107 val = control->value;
1108 else
1109 val = szState;
1111 msi_dialog_set_property( dialog->package, control->property, val );
1114 static void msi_dialog_checkbox_sync_state( msi_dialog *dialog, msi_control *control )
1116 UINT state = msi_dialog_get_checkbox_state( dialog, control );
1117 SendMessageW( control->hwnd, BM_SETCHECK, state ? BST_CHECKED : BST_UNCHECKED, 0 );
1120 static UINT msi_dialog_checkbox_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
1122 UINT state;
1124 if (HIWORD(param) != BN_CLICKED)
1125 return ERROR_SUCCESS;
1127 TRACE("clicked checkbox %s, set %s\n", debugstr_w(control->name), debugstr_w(control->property));
1129 state = msi_dialog_get_checkbox_state( dialog, control );
1130 state = state ? 0 : 1;
1131 msi_dialog_set_checkbox_state( dialog, control, state );
1132 msi_dialog_checkbox_sync_state( dialog, control );
1134 return msi_dialog_button_handler( dialog, control, param );
1137 static UINT msi_dialog_checkbox_control( msi_dialog *dialog, MSIRECORD *rec )
1139 msi_control *control;
1140 LPCWSTR prop;
1142 TRACE("%p %p\n", dialog, rec);
1144 control = msi_dialog_add_control( dialog, rec, szButton, BS_CHECKBOX | BS_MULTILINE | WS_TABSTOP );
1145 control->handler = msi_dialog_checkbox_handler;
1146 control->update = msi_dialog_checkbox_sync_state;
1147 prop = MSI_RecordGetString( rec, 9 );
1148 if (prop)
1150 control->property = strdupW( prop );
1151 control->value = msi_get_checkbox_value( dialog, prop );
1152 TRACE("control %s value %s\n", debugstr_w(control->property), debugstr_w(control->value));
1154 msi_dialog_checkbox_sync_state( dialog, control );
1155 return ERROR_SUCCESS;
1158 static UINT msi_dialog_line_control( msi_dialog *dialog, MSIRECORD *rec )
1160 DWORD attributes;
1161 LPCWSTR name;
1162 DWORD style, exstyle = 0;
1163 DWORD x, y, width, height;
1164 msi_control *control;
1166 TRACE("%p %p\n", dialog, rec);
1168 style = WS_CHILD | SS_ETCHEDHORZ | SS_SUNKEN;
1170 name = MSI_RecordGetString( rec, 2 );
1171 attributes = MSI_RecordGetInteger( rec, 8 );
1173 if( attributes & msidbControlAttributesVisible )
1174 style |= WS_VISIBLE;
1175 if( ~attributes & msidbControlAttributesEnabled )
1176 style |= WS_DISABLED;
1177 if( attributes & msidbControlAttributesSunken )
1178 exstyle |= WS_EX_CLIENTEDGE;
1180 dialog_map_events( dialog, name );
1182 control = msi_alloc( FIELD_OFFSET(msi_control, name[lstrlenW( name ) + 1] ));
1183 if (!control)
1184 return ERROR_OUTOFMEMORY;
1186 lstrcpyW( control->name, name );
1187 list_add_head( &dialog->controls, &control->entry );
1188 control->handler = NULL;
1189 control->property = NULL;
1190 control->value = NULL;
1191 control->hBitmap = NULL;
1192 control->hIcon = NULL;
1193 control->hDll = NULL;
1194 control->tabnext = strdupW( MSI_RecordGetString( rec, 11) );
1195 control->type = strdupW( MSI_RecordGetString( rec, 3 ) );
1196 control->progress_current = 0;
1197 control->progress_max = 100;
1198 control->progress_backwards = FALSE;
1200 x = MSI_RecordGetInteger( rec, 4 );
1201 y = MSI_RecordGetInteger( rec, 5 );
1202 width = MSI_RecordGetInteger( rec, 6 );
1204 x = msi_dialog_scale_unit( dialog, x );
1205 y = msi_dialog_scale_unit( dialog, y );
1206 width = msi_dialog_scale_unit( dialog, width );
1207 height = 2; /* line is exactly 2 units in height */
1209 control->hwnd = CreateWindowExW( exstyle, szStatic, NULL, style,
1210 x, y, width, height, dialog->hwnd, NULL, NULL, NULL );
1212 TRACE("Dialog %s control %s hwnd %p\n",
1213 debugstr_w(dialog->name), debugstr_w(name), control->hwnd );
1215 return ERROR_SUCCESS;
1218 /******************** Scroll Text ********************************************/
1220 struct msi_scrolltext_info
1222 msi_dialog *dialog;
1223 msi_control *control;
1224 WNDPROC oldproc;
1227 static LRESULT WINAPI
1228 MSIScrollText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1230 struct msi_scrolltext_info *info;
1231 HRESULT r;
1233 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1235 info = GetPropW( hWnd, szButtonData );
1237 r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
1239 switch( msg )
1241 case WM_GETDLGCODE:
1242 return DLGC_WANTARROWS;
1243 case WM_NCDESTROY:
1244 msi_free( info );
1245 RemovePropW( hWnd, szButtonData );
1246 break;
1247 case WM_PAINT:
1248 /* native MSI sets a wait cursor here */
1249 msi_dialog_button_handler( info->dialog, info->control, BN_CLICKED );
1250 break;
1252 return r;
1255 struct msi_streamin_info
1257 LPSTR string;
1258 DWORD offset;
1259 DWORD length;
1262 static DWORD CALLBACK
1263 msi_richedit_stream_in( DWORD_PTR arg, LPBYTE buffer, LONG count, LONG *pcb )
1265 struct msi_streamin_info *info = (struct msi_streamin_info*) arg;
1267 if( (count + info->offset) > info->length )
1268 count = info->length - info->offset;
1269 memcpy( buffer, &info->string[ info->offset ], count );
1270 *pcb = count;
1271 info->offset += count;
1273 TRACE("%d/%d\n", info->offset, info->length);
1275 return 0;
1278 static void msi_scrolltext_add_text( msi_control *control, LPCWSTR text )
1280 struct msi_streamin_info info;
1281 EDITSTREAM es;
1283 info.string = strdupWtoA( text );
1284 info.offset = 0;
1285 info.length = lstrlenA( info.string ) + 1;
1287 es.dwCookie = (DWORD_PTR) &info;
1288 es.dwError = 0;
1289 es.pfnCallback = msi_richedit_stream_in;
1291 SendMessageW( control->hwnd, EM_STREAMIN, SF_RTF, (LPARAM) &es );
1293 msi_free( info.string );
1296 static UINT msi_dialog_scrolltext_control( msi_dialog *dialog, MSIRECORD *rec )
1298 static const WCHAR szRichEdit20W[] = {'R','i','c','h','E','d','i','t','2','0','W',0};
1299 struct msi_scrolltext_info *info;
1300 msi_control *control;
1301 HMODULE hRichedit;
1302 LPCWSTR text;
1303 DWORD style;
1305 info = msi_alloc( sizeof *info );
1306 if (!info)
1307 return ERROR_FUNCTION_FAILED;
1309 hRichedit = LoadLibraryA("riched20");
1311 style = WS_BORDER | ES_MULTILINE | WS_VSCROLL |
1312 ES_READONLY | ES_AUTOVSCROLL | WS_TABSTOP;
1313 control = msi_dialog_add_control( dialog, rec, szRichEdit20W, style );
1314 if (!control)
1316 FreeLibrary( hRichedit );
1317 msi_free( info );
1318 return ERROR_FUNCTION_FAILED;
1321 control->hDll = hRichedit;
1323 info->dialog = dialog;
1324 info->control = control;
1326 /* subclass the static control */
1327 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1328 (LONG_PTR)MSIScrollText_WndProc );
1329 SetPropW( control->hwnd, szButtonData, info );
1331 /* add the text into the richedit */
1332 text = MSI_RecordGetString( rec, 10 );
1333 if (text)
1334 msi_scrolltext_add_text( control, text );
1336 return ERROR_SUCCESS;
1339 static HBITMAP msi_load_picture( MSIDATABASE *db, LPCWSTR name,
1340 INT cx, INT cy, DWORD flags )
1342 HBITMAP hOleBitmap = 0, hBitmap = 0, hOldSrcBitmap, hOldDestBitmap;
1343 MSIRECORD *rec = NULL;
1344 IStream *stm = NULL;
1345 IPicture *pic = NULL;
1346 HDC srcdc, destdc;
1347 BITMAP bm;
1348 UINT r;
1350 rec = msi_get_binary_record( db, name );
1351 if( !rec )
1352 goto end;
1354 r = MSI_RecordGetIStream( rec, 2, &stm );
1355 msiobj_release( &rec->hdr );
1356 if( r != ERROR_SUCCESS )
1357 goto end;
1359 r = OleLoadPicture( stm, 0, TRUE, &IID_IPicture, (LPVOID*) &pic );
1360 IStream_Release( stm );
1361 if( FAILED( r ) )
1363 ERR("failed to load picture\n");
1364 goto end;
1367 r = IPicture_get_Handle( pic, (OLE_HANDLE*) &hOleBitmap );
1368 if( FAILED( r ) )
1370 ERR("failed to get bitmap handle\n");
1371 goto end;
1374 /* make the bitmap the desired size */
1375 r = GetObjectW( hOleBitmap, sizeof bm, &bm );
1376 if (r != sizeof bm )
1378 ERR("failed to get bitmap size\n");
1379 goto end;
1382 if (flags & LR_DEFAULTSIZE)
1384 cx = bm.bmWidth;
1385 cy = bm.bmHeight;
1388 srcdc = CreateCompatibleDC( NULL );
1389 hOldSrcBitmap = SelectObject( srcdc, hOleBitmap );
1390 destdc = CreateCompatibleDC( NULL );
1391 hBitmap = CreateCompatibleBitmap( srcdc, cx, cy );
1392 hOldDestBitmap = SelectObject( destdc, hBitmap );
1393 StretchBlt( destdc, 0, 0, cx, cy,
1394 srcdc, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
1395 SelectObject( srcdc, hOldSrcBitmap );
1396 SelectObject( destdc, hOldDestBitmap );
1397 DeleteDC( srcdc );
1398 DeleteDC( destdc );
1400 end:
1401 if ( pic )
1402 IPicture_Release( pic );
1403 return hBitmap;
1406 static UINT msi_dialog_bitmap_control( msi_dialog *dialog, MSIRECORD *rec )
1408 UINT cx, cy, flags, style, attributes;
1409 msi_control *control;
1410 LPWSTR name;
1412 flags = LR_LOADFROMFILE;
1413 style = SS_BITMAP | SS_LEFT | WS_GROUP;
1415 attributes = MSI_RecordGetInteger( rec, 8 );
1416 if( attributes & msidbControlAttributesFixedSize )
1418 flags |= LR_DEFAULTSIZE;
1419 style |= SS_CENTERIMAGE;
1422 control = msi_dialog_add_control( dialog, rec, szStatic, style );
1423 cx = MSI_RecordGetInteger( rec, 6 );
1424 cy = MSI_RecordGetInteger( rec, 7 );
1425 cx = msi_dialog_scale_unit( dialog, cx );
1426 cy = msi_dialog_scale_unit( dialog, cy );
1428 name = msi_get_binary_name( dialog->package, rec );
1429 control->hBitmap = msi_load_picture( dialog->package->db, name, cx, cy, flags );
1430 if( control->hBitmap )
1431 SendMessageW( control->hwnd, STM_SETIMAGE,
1432 IMAGE_BITMAP, (LPARAM) control->hBitmap );
1433 else
1434 ERR("Failed to load bitmap %s\n", debugstr_w(name));
1436 msi_free( name );
1438 return ERROR_SUCCESS;
1441 static UINT msi_dialog_icon_control( msi_dialog *dialog, MSIRECORD *rec )
1443 msi_control *control;
1444 DWORD attributes;
1445 LPWSTR name;
1447 TRACE("\n");
1449 control = msi_dialog_add_control( dialog, rec, szStatic,
1450 SS_ICON | SS_CENTERIMAGE | WS_GROUP );
1452 attributes = MSI_RecordGetInteger( rec, 8 );
1453 name = msi_get_binary_name( dialog->package, rec );
1454 control->hIcon = msi_load_icon( dialog->package->db, name, attributes );
1455 if( control->hIcon )
1456 SendMessageW( control->hwnd, STM_SETICON, (WPARAM) control->hIcon, 0 );
1457 else
1458 ERR("Failed to load bitmap %s\n", debugstr_w(name));
1459 msi_free( name );
1460 return ERROR_SUCCESS;
1463 /******************** Combo Box ***************************************/
1465 struct msi_combobox_info
1467 msi_dialog *dialog;
1468 HWND hwnd;
1469 WNDPROC oldproc;
1470 DWORD num_items;
1471 DWORD addpos_items;
1472 LPWSTR *items;
1475 static LRESULT WINAPI MSIComboBox_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1477 struct msi_combobox_info *info;
1478 LRESULT r;
1479 DWORD j;
1481 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1483 info = GetPropW( hWnd, szButtonData );
1484 if (!info)
1485 return 0;
1487 r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
1489 switch (msg)
1491 case WM_NCDESTROY:
1492 for (j = 0; j < info->num_items; j++)
1493 msi_free( info->items[j] );
1494 msi_free( info->items );
1495 msi_free( info );
1496 RemovePropW( hWnd, szButtonData );
1497 break;
1500 return r;
1503 static UINT msi_combobox_add_item( MSIRECORD *rec, LPVOID param )
1505 struct msi_combobox_info *info = param;
1506 LPCWSTR value, text;
1507 int pos;
1509 value = MSI_RecordGetString( rec, 3 );
1510 text = MSI_RecordGetString( rec, 4 );
1512 info->items[info->addpos_items] = strdupW( value );
1514 pos = SendMessageW( info->hwnd, CB_ADDSTRING, 0, (LPARAM)text );
1515 SendMessageW( info->hwnd, CB_SETITEMDATA, pos, (LPARAM)info->items[info->addpos_items] );
1516 info->addpos_items++;
1518 return ERROR_SUCCESS;
1521 static UINT msi_combobox_add_items( struct msi_combobox_info *info, LPCWSTR property )
1523 static const WCHAR query[] = {
1524 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1525 '`','C','o','m','b','o','B','o','x','`',' ','W','H','E','R','E',' ',
1526 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',' ',
1527 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','`',0};
1528 MSIQUERY *view;
1529 DWORD count;
1530 UINT r;
1532 r = MSI_OpenQuery( info->dialog->package->db, &view, query, property );
1533 if (r != ERROR_SUCCESS)
1534 return r;
1536 /* just get the number of records */
1537 count = 0;
1538 r = MSI_IterateRecords( view, &count, NULL, NULL );
1539 if (r != ERROR_SUCCESS)
1541 msiobj_release( &view->hdr );
1542 return r;
1544 info->num_items = count;
1545 info->items = msi_alloc( sizeof(*info->items) * count );
1547 r = MSI_IterateRecords( view, NULL, msi_combobox_add_item, info );
1548 msiobj_release( &view->hdr );
1549 return r;
1552 static UINT msi_dialog_set_control_condition( MSIRECORD *rec, LPVOID param )
1554 static const WCHAR szHide[] = {'H','i','d','e',0};
1555 static const WCHAR szShow[] = {'S','h','o','w',0};
1556 static const WCHAR szDisable[] = {'D','i','s','a','b','l','e',0};
1557 static const WCHAR szEnable[] = {'E','n','a','b','l','e',0};
1558 static const WCHAR szDefault[] = {'D','e','f','a','u','l','t',0};
1559 msi_dialog *dialog = param;
1560 msi_control *control;
1561 LPCWSTR name, action, condition;
1562 UINT r;
1564 name = MSI_RecordGetString( rec, 2 );
1565 action = MSI_RecordGetString( rec, 3 );
1566 condition = MSI_RecordGetString( rec, 4 );
1567 r = MSI_EvaluateConditionW( dialog->package, condition );
1568 control = msi_dialog_find_control( dialog, name );
1569 if (r == MSICONDITION_TRUE && control)
1571 TRACE("%s control %s\n", debugstr_w(action), debugstr_w(name));
1573 /* FIXME: case sensitive? */
1574 if (!wcscmp( action, szHide ))
1575 ShowWindow(control->hwnd, SW_HIDE);
1576 else if (!wcscmp( action, szShow ))
1577 ShowWindow(control->hwnd, SW_SHOW);
1578 else if (!wcscmp( action, szDisable ))
1579 EnableWindow(control->hwnd, FALSE);
1580 else if (!wcscmp( action, szEnable ))
1581 EnableWindow(control->hwnd, TRUE);
1582 else if (!wcscmp( action, szDefault ))
1583 SetFocus(control->hwnd);
1584 else
1585 FIXME("Unhandled action %s\n", debugstr_w(action));
1587 return ERROR_SUCCESS;
1590 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog )
1592 static const WCHAR query[] = {
1593 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1594 'C','o','n','t','r','o','l','C','o','n','d','i','t','i','o','n',' ',
1595 'W','H','E','R','E',' ','`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
1596 UINT r;
1597 MSIQUERY *view;
1598 MSIPACKAGE *package = dialog->package;
1600 TRACE("%p %s\n", dialog, debugstr_w(dialog->name));
1602 /* query the Control table for all the elements of the control */
1603 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
1604 if (r != ERROR_SUCCESS)
1605 return ERROR_SUCCESS;
1607 r = MSI_IterateRecords( view, 0, msi_dialog_set_control_condition, dialog );
1608 msiobj_release( &view->hdr );
1609 return r;
1612 static UINT msi_dialog_combobox_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
1614 struct msi_combobox_info *info;
1615 int index;
1616 LPWSTR value;
1618 if (HIWORD(param) != CBN_SELCHANGE && HIWORD(param) != CBN_EDITCHANGE)
1619 return ERROR_SUCCESS;
1621 info = GetPropW( control->hwnd, szButtonData );
1622 index = SendMessageW( control->hwnd, CB_GETCURSEL, 0, 0 );
1623 if (index == CB_ERR)
1624 value = msi_get_window_text( control->hwnd );
1625 else
1626 value = (LPWSTR) SendMessageW( control->hwnd, CB_GETITEMDATA, index, 0 );
1628 msi_dialog_set_property( info->dialog->package, control->property, value );
1629 msi_dialog_evaluate_control_conditions( info->dialog );
1631 if (index == CB_ERR)
1632 msi_free( value );
1634 return ERROR_SUCCESS;
1637 static void msi_dialog_combobox_update( msi_dialog *dialog, msi_control *control )
1639 struct msi_combobox_info *info;
1640 LPWSTR value, tmp;
1641 DWORD j;
1643 info = GetPropW( control->hwnd, szButtonData );
1645 value = msi_dup_property( dialog->package->db, control->property );
1646 if (!value)
1648 SendMessageW( control->hwnd, CB_SETCURSEL, -1, 0 );
1649 return;
1652 for (j = 0; j < info->num_items; j++)
1654 tmp = (LPWSTR) SendMessageW( control->hwnd, CB_GETITEMDATA, j, 0 );
1655 if (!wcscmp( value, tmp ))
1656 break;
1659 if (j < info->num_items)
1661 SendMessageW( control->hwnd, CB_SETCURSEL, j, 0 );
1663 else
1665 SendMessageW( control->hwnd, CB_SETCURSEL, -1, 0 );
1666 SetWindowTextW( control->hwnd, value );
1669 msi_free(value);
1672 static UINT msi_dialog_combo_control( msi_dialog *dialog, MSIRECORD *rec )
1674 struct msi_combobox_info *info;
1675 msi_control *control;
1676 DWORD attributes, style;
1677 LPCWSTR prop;
1679 info = msi_alloc( sizeof *info );
1680 if (!info)
1681 return ERROR_FUNCTION_FAILED;
1683 style = CBS_AUTOHSCROLL | WS_TABSTOP | WS_GROUP | WS_CHILD;
1684 attributes = MSI_RecordGetInteger( rec, 8 );
1685 if ( ~attributes & msidbControlAttributesSorted)
1686 style |= CBS_SORT;
1687 if ( attributes & msidbControlAttributesComboList)
1688 style |= CBS_DROPDOWNLIST;
1689 else
1690 style |= CBS_DROPDOWN;
1692 control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
1693 if (!control)
1695 msi_free( info );
1696 return ERROR_FUNCTION_FAILED;
1699 control->handler = msi_dialog_combobox_handler;
1700 control->update = msi_dialog_combobox_update;
1702 prop = MSI_RecordGetString( rec, 9 );
1703 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
1705 /* subclass */
1706 info->dialog = dialog;
1707 info->hwnd = control->hwnd;
1708 info->items = NULL;
1709 info->addpos_items = 0;
1710 info->oldproc = (WNDPROC)SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1711 (LONG_PTR)MSIComboBox_WndProc );
1712 SetPropW( control->hwnd, szButtonData, info );
1714 if (control->property)
1715 msi_combobox_add_items( info, control->property );
1717 msi_dialog_combobox_update( dialog, control );
1719 return ERROR_SUCCESS;
1722 static UINT msi_dialog_edit_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
1724 LPWSTR buf;
1726 if (HIWORD(param) != EN_CHANGE)
1727 return ERROR_SUCCESS;
1729 TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name), debugstr_w(control->property));
1731 buf = msi_get_window_text( control->hwnd );
1732 msi_dialog_set_property( dialog->package, control->property, buf );
1733 msi_free( buf );
1735 return ERROR_SUCCESS;
1738 /* length of 2^32 + 1 */
1739 #define MAX_NUM_DIGITS 11
1741 static UINT msi_dialog_edit_control( msi_dialog *dialog, MSIRECORD *rec )
1743 msi_control *control;
1744 LPCWSTR prop, text;
1745 LPWSTR val, begin, end;
1746 WCHAR num[MAX_NUM_DIGITS];
1747 DWORD limit;
1749 control = msi_dialog_add_control( dialog, rec, szEdit,
1750 WS_BORDER | WS_TABSTOP | ES_AUTOHSCROLL );
1751 control->handler = msi_dialog_edit_handler;
1753 text = MSI_RecordGetString( rec, 10 );
1754 if ( text )
1756 begin = wcschr( text, '{' );
1757 end = wcschr( text, '}' );
1759 if ( begin && end && end > begin &&
1760 begin[0] >= '0' && begin[0] <= '9' &&
1761 end - begin < MAX_NUM_DIGITS)
1763 lstrcpynW( num, begin + 1, end - begin );
1764 limit = wcstol( num, NULL, 10 );
1766 SendMessageW( control->hwnd, EM_SETLIMITTEXT, limit, 0 );
1770 prop = MSI_RecordGetString( rec, 9 );
1771 if( prop )
1772 control->property = strdupW( prop );
1774 val = msi_dup_property( dialog->package->db, control->property );
1775 SetWindowTextW( control->hwnd, val );
1776 msi_free( val );
1777 return ERROR_SUCCESS;
1780 /******************** Masked Edit ********************************************/
1782 #define MASK_MAX_GROUPS 20
1784 struct msi_mask_group
1786 UINT len;
1787 UINT ofs;
1788 WCHAR type;
1789 HWND hwnd;
1792 struct msi_maskedit_info
1794 msi_dialog *dialog;
1795 WNDPROC oldproc;
1796 HWND hwnd;
1797 LPWSTR prop;
1798 UINT num_chars;
1799 UINT num_groups;
1800 struct msi_mask_group group[MASK_MAX_GROUPS];
1803 static BOOL msi_mask_editable( WCHAR type )
1805 switch (type)
1807 case '%':
1808 case '#':
1809 case '&':
1810 case '`':
1811 case '?':
1812 case '^':
1813 return TRUE;
1815 return FALSE;
1818 static void msi_mask_control_change( struct msi_maskedit_info *info )
1820 LPWSTR val;
1821 UINT i, n, r;
1823 val = msi_alloc( (info->num_chars+1)*sizeof(WCHAR) );
1824 for( i=0, n=0; i<info->num_groups; i++ )
1826 if (info->group[i].len == ~0u)
1828 UINT len = SendMessageW( info->group[i].hwnd, WM_GETTEXTLENGTH, 0, 0 );
1829 val = msi_realloc( val, (len + 1) * sizeof(WCHAR) );
1830 GetWindowTextW( info->group[i].hwnd, val, len + 1 );
1832 else
1834 if (info->group[i].len + n > info->num_chars)
1836 ERR("can't fit control %d text into template\n",i);
1837 break;
1839 if (!msi_mask_editable(info->group[i].type))
1841 for(r=0; r<info->group[i].len; r++)
1842 val[n+r] = info->group[i].type;
1843 val[n+r] = 0;
1845 else
1847 r = GetWindowTextW( info->group[i].hwnd, &val[n], info->group[i].len+1 );
1848 if( r != info->group[i].len )
1849 break;
1851 n += r;
1855 TRACE("%d/%d controls were good\n", i, info->num_groups);
1857 if( i == info->num_groups )
1859 TRACE("Set property %s to %s\n", debugstr_w(info->prop), debugstr_w(val));
1860 msi_dialog_set_property( info->dialog->package, info->prop, val );
1861 msi_dialog_evaluate_control_conditions( info->dialog );
1863 msi_free( val );
1866 /* now move to the next control if necessary */
1867 static VOID msi_mask_next_control( struct msi_maskedit_info *info, HWND hWnd )
1869 HWND hWndNext;
1870 UINT len, i;
1872 for( i=0; i<info->num_groups; i++ )
1873 if( info->group[i].hwnd == hWnd )
1874 break;
1876 /* don't move from the last control */
1877 if( i >= (info->num_groups-1) )
1878 return;
1880 len = SendMessageW( hWnd, WM_GETTEXTLENGTH, 0, 0 );
1881 if( len < info->group[i].len )
1882 return;
1884 hWndNext = GetNextDlgTabItem( GetParent( hWnd ), hWnd, FALSE );
1885 SetFocus( hWndNext );
1888 static LRESULT WINAPI
1889 MSIMaskedEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1891 struct msi_maskedit_info *info;
1892 HRESULT r;
1894 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1896 info = GetPropW(hWnd, szButtonData);
1898 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1900 switch( msg )
1902 case WM_COMMAND:
1903 if (HIWORD(wParam) == EN_CHANGE)
1905 msi_mask_control_change( info );
1906 msi_mask_next_control( info, (HWND) lParam );
1908 break;
1909 case WM_NCDESTROY:
1910 msi_free( info->prop );
1911 msi_free( info );
1912 RemovePropW( hWnd, szButtonData );
1913 break;
1916 return r;
1919 /* fish the various bits of the property out and put them in the control */
1920 static void
1921 msi_maskedit_set_text( struct msi_maskedit_info *info, LPCWSTR text )
1923 LPCWSTR p;
1924 UINT i;
1926 p = text;
1927 for( i = 0; i < info->num_groups; i++ )
1929 if( info->group[i].len < lstrlenW( p ) )
1931 LPWSTR chunk = strdupW( p );
1932 chunk[ info->group[i].len ] = 0;
1933 SetWindowTextW( info->group[i].hwnd, chunk );
1934 msi_free( chunk );
1936 else
1938 SetWindowTextW( info->group[i].hwnd, p );
1939 break;
1941 p += info->group[i].len;
1945 static struct msi_maskedit_info * msi_dialog_parse_groups( LPCWSTR mask )
1947 struct msi_maskedit_info *info;
1948 int i = 0, n = 0, total = 0;
1949 LPCWSTR p;
1951 TRACE("masked control, template %s\n", debugstr_w(mask));
1953 if( !mask )
1954 return NULL;
1956 info = msi_alloc_zero( sizeof *info );
1957 if( !info )
1958 return info;
1960 p = wcschr(mask, '<');
1961 if( p )
1962 p++;
1963 else
1964 p = mask;
1966 for( i=0; i<MASK_MAX_GROUPS; i++ )
1968 /* stop at the end of the string */
1969 if( p[0] == 0 || p[0] == '>' )
1971 if (!total)
1973 /* create a group for the empty mask */
1974 info->group[0].type = '&';
1975 info->group[0].len = ~0u;
1976 i = 1;
1978 break;
1981 /* count the number of the same identifier */
1982 for( n=0; p[n] == p[0]; n++ )
1984 info->group[i].ofs = total;
1985 info->group[i].type = p[0];
1986 if( p[n] == '=' )
1988 n++;
1989 total++; /* an extra not part of the group */
1991 info->group[i].len = n;
1992 total += n;
1993 p += n;
1996 TRACE("%d characters in %d groups\n", total, i );
1997 if( i == MASK_MAX_GROUPS )
1998 ERR("too many groups in PIDTemplate %s\n", debugstr_w(mask));
2000 info->num_chars = total;
2001 info->num_groups = i;
2003 return info;
2006 static void
2007 msi_maskedit_create_children( struct msi_maskedit_info *info, LPCWSTR font )
2009 DWORD width, height, style, wx, ww;
2010 RECT rect;
2011 HWND hwnd;
2012 UINT i;
2014 style = WS_CHILD | WS_BORDER | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL;
2016 GetClientRect( info->hwnd, &rect );
2018 width = rect.right - rect.left;
2019 height = rect.bottom - rect.top;
2021 for( i = 0; i < info->num_groups; i++ )
2023 if (!msi_mask_editable( info->group[i].type ))
2024 continue;
2025 if (info->num_chars)
2027 wx = (info->group[i].ofs * width) / info->num_chars;
2028 ww = (info->group[i].len * width) / info->num_chars;
2030 else
2032 wx = 0;
2033 ww = width;
2035 hwnd = CreateWindowW( szEdit, NULL, style, wx, 0, ww, height,
2036 info->hwnd, NULL, NULL, NULL );
2037 if( !hwnd )
2039 ERR("failed to create mask edit sub window\n");
2040 break;
2043 SendMessageW( hwnd, EM_LIMITTEXT, info->group[i].len, 0 );
2045 msi_dialog_set_font( info->dialog, hwnd,
2046 font?font:info->dialog->default_font );
2047 info->group[i].hwnd = hwnd;
2052 * office 2003 uses "73931<````=````=````=````=`````>@@@@@"
2053 * delphi 7 uses "<????-??????-??????-????>" and "<???-???>"
2054 * filemaker pro 7 uses "<^^^^=^^^^=^^^^=^^^^=^^^^=^^^^=^^^^^>"
2056 static UINT msi_dialog_maskedit_control( msi_dialog *dialog, MSIRECORD *rec )
2058 LPWSTR font_mask, val = NULL, font;
2059 struct msi_maskedit_info *info = NULL;
2060 UINT ret = ERROR_SUCCESS;
2061 msi_control *control;
2062 LPCWSTR prop, mask;
2064 TRACE("\n");
2066 font_mask = msi_get_deformatted_field( dialog->package, rec, 10 );
2067 font = msi_dialog_get_style( font_mask, &mask );
2068 if( !mask )
2070 WARN("mask template is empty\n");
2071 goto end;
2074 info = msi_dialog_parse_groups( mask );
2075 if( !info )
2077 ERR("template %s is invalid\n", debugstr_w(mask));
2078 goto end;
2081 info->dialog = dialog;
2083 control = msi_dialog_add_control( dialog, rec, szStatic,
2084 SS_OWNERDRAW | WS_GROUP | WS_VISIBLE );
2085 if( !control )
2087 ERR("Failed to create maskedit container\n");
2088 ret = ERROR_FUNCTION_FAILED;
2089 goto end;
2091 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
2093 info->hwnd = control->hwnd;
2095 /* subclass the static control */
2096 info->oldproc = (WNDPROC) SetWindowLongPtrW( info->hwnd, GWLP_WNDPROC,
2097 (LONG_PTR)MSIMaskedEdit_WndProc );
2098 SetPropW( control->hwnd, szButtonData, info );
2100 prop = MSI_RecordGetString( rec, 9 );
2101 if( prop )
2102 info->prop = strdupW( prop );
2104 msi_maskedit_create_children( info, font );
2106 if( prop )
2108 val = msi_dup_property( dialog->package->db, prop );
2109 if( val )
2111 msi_maskedit_set_text( info, val );
2112 msi_free( val );
2116 end:
2117 if( ret != ERROR_SUCCESS )
2118 msi_free( info );
2119 msi_free( font_mask );
2120 msi_free( font );
2121 return ret;
2124 /******************** Progress Bar *****************************************/
2126 static UINT msi_dialog_progress_bar( msi_dialog *dialog, MSIRECORD *rec )
2128 msi_control *control;
2129 DWORD attributes, style;
2131 style = WS_VISIBLE;
2132 attributes = MSI_RecordGetInteger( rec, 8 );
2133 if( !(attributes & msidbControlAttributesProgress95) )
2134 style |= PBS_SMOOTH;
2136 control = msi_dialog_add_control( dialog, rec, PROGRESS_CLASSW, style );
2137 if( !control )
2138 return ERROR_FUNCTION_FAILED;
2140 event_subscribe( dialog, szSetProgress, control->name, szProgress );
2141 return ERROR_SUCCESS;
2144 /******************** Path Edit ********************************************/
2146 struct msi_pathedit_info
2148 msi_dialog *dialog;
2149 msi_control *control;
2150 WNDPROC oldproc;
2153 static WCHAR *get_path_property( msi_dialog *dialog, msi_control *control )
2155 WCHAR *prop, *path;
2156 BOOL indirect = control->attributes & msidbControlAttributesIndirect;
2157 if (!(prop = msi_dialog_dup_property( dialog, control->property, indirect ))) return NULL;
2158 path = msi_dialog_dup_property( dialog, prop, TRUE );
2159 msi_free( prop );
2160 return path;
2163 static void msi_dialog_update_pathedit( msi_dialog *dialog, msi_control *control )
2165 WCHAR *path;
2167 if (!control && !(control = msi_dialog_find_control_by_type( dialog, szPathEdit )))
2168 return;
2170 if (!(path = get_path_property( dialog, control ))) return;
2171 SetWindowTextW( control->hwnd, path );
2172 SendMessageW( control->hwnd, EM_SETSEL, 0, -1 );
2173 msi_free( path );
2176 /* FIXME: test when this should fail */
2177 static BOOL msi_dialog_verify_path( LPWSTR path )
2179 if ( !path[0] )
2180 return FALSE;
2182 if ( PathIsRelativeW( path ) )
2183 return FALSE;
2185 return TRUE;
2188 /* returns TRUE if the path is valid, FALSE otherwise */
2189 static BOOL msi_dialog_onkillfocus( msi_dialog *dialog, msi_control *control )
2191 LPWSTR buf, prop;
2192 BOOL indirect;
2193 BOOL valid;
2195 indirect = control->attributes & msidbControlAttributesIndirect;
2196 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2198 buf = msi_get_window_text( control->hwnd );
2200 if ( !msi_dialog_verify_path( buf ) )
2202 /* FIXME: display an error message box */
2203 ERR("Invalid path %s\n", debugstr_w( buf ));
2204 valid = FALSE;
2205 SetFocus( control->hwnd );
2207 else
2209 valid = TRUE;
2210 msi_dialog_set_property( dialog->package, prop, buf );
2213 msi_dialog_update_pathedit( dialog, control );
2215 TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
2216 debugstr_w(prop));
2218 msi_free( buf );
2219 msi_free( prop );
2221 return valid;
2224 static LRESULT WINAPI MSIPathEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2226 struct msi_pathedit_info *info = GetPropW(hWnd, szButtonData);
2227 LRESULT r = 0;
2229 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
2231 if ( msg == WM_KILLFOCUS )
2233 /* if the path is invalid, don't handle this message */
2234 if ( !msi_dialog_onkillfocus( info->dialog, info->control ) )
2235 return 0;
2238 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
2240 if ( msg == WM_NCDESTROY )
2242 msi_free( info );
2243 RemovePropW( hWnd, szButtonData );
2246 return r;
2249 static UINT msi_dialog_pathedit_control( msi_dialog *dialog, MSIRECORD *rec )
2251 struct msi_pathedit_info *info;
2252 msi_control *control;
2253 LPCWSTR prop;
2255 info = msi_alloc( sizeof *info );
2256 if (!info)
2257 return ERROR_FUNCTION_FAILED;
2259 control = msi_dialog_add_control( dialog, rec, szEdit,
2260 WS_BORDER | WS_TABSTOP );
2261 control->attributes = MSI_RecordGetInteger( rec, 8 );
2262 prop = MSI_RecordGetString( rec, 9 );
2263 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2264 control->update = msi_dialog_update_pathedit;
2266 info->dialog = dialog;
2267 info->control = control;
2268 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2269 (LONG_PTR)MSIPathEdit_WndProc );
2270 SetPropW( control->hwnd, szButtonData, info );
2272 msi_dialog_update_pathedit( dialog, control );
2274 return ERROR_SUCCESS;
2277 static UINT msi_dialog_radiogroup_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
2279 if (HIWORD(param) != BN_CLICKED)
2280 return ERROR_SUCCESS;
2282 TRACE("clicked radio button %s, set %s\n", debugstr_w(control->name), debugstr_w(control->property));
2284 msi_dialog_set_property( dialog->package, control->property, control->name );
2286 return msi_dialog_button_handler( dialog, control, param );
2289 /* radio buttons are a bit different from normal controls */
2290 static UINT msi_dialog_create_radiobutton( MSIRECORD *rec, LPVOID param )
2292 radio_button_group_descr *group = param;
2293 msi_dialog *dialog = group->dialog;
2294 msi_control *control;
2295 LPCWSTR prop, text, name;
2296 DWORD style = WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_AUTORADIOBUTTON | BS_MULTILINE;
2298 name = MSI_RecordGetString( rec, 3 );
2299 text = MSI_RecordGetString( rec, 8 );
2301 control = dialog_create_window( dialog, rec, 0, szButton, name, text, style,
2302 group->parent->hwnd );
2303 if (!control)
2304 return ERROR_FUNCTION_FAILED;
2305 control->handler = msi_dialog_radiogroup_handler;
2307 if (group->propval && !wcscmp( control->name, group->propval ))
2308 SendMessageW(control->hwnd, BM_SETCHECK, BST_CHECKED, 0);
2310 prop = MSI_RecordGetString( rec, 1 );
2311 if( prop )
2312 control->property = strdupW( prop );
2314 return ERROR_SUCCESS;
2317 static BOOL CALLBACK msi_radioground_child_enum( HWND hWnd, LPARAM lParam )
2319 EnableWindow( hWnd, lParam );
2320 return TRUE;
2323 static LRESULT WINAPI MSIRadioGroup_WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
2325 WNDPROC oldproc = (WNDPROC)GetPropW( hWnd, szButtonData );
2326 LRESULT r;
2328 TRACE("hWnd %p msg %04x wParam 0x%08lx lParam 0x%08lx\n", hWnd, msg, wParam, lParam);
2330 if (msg == WM_COMMAND) /* Forward notifications to dialog */
2331 SendMessageW( GetParent( hWnd ), msg, wParam, lParam );
2333 r = CallWindowProcW( oldproc, hWnd, msg, wParam, lParam );
2335 /* make sure the radio buttons show as disabled if the parent is disabled */
2336 if (msg == WM_ENABLE)
2337 EnumChildWindows( hWnd, msi_radioground_child_enum, wParam );
2339 return r;
2342 static UINT msi_dialog_radiogroup_control( msi_dialog *dialog, MSIRECORD *rec )
2344 static const WCHAR query[] = {
2345 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2346 'R','a','d','i','o','B','u','t','t','o','n',' ','W','H','E','R','E',' ',
2347 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
2348 UINT r;
2349 LPCWSTR prop;
2350 msi_control *control;
2351 MSIQUERY *view;
2352 radio_button_group_descr group;
2353 MSIPACKAGE *package = dialog->package;
2354 WNDPROC oldproc;
2355 DWORD attr, style = WS_GROUP;
2357 prop = MSI_RecordGetString( rec, 9 );
2359 TRACE("%p %p %s\n", dialog, rec, debugstr_w( prop ));
2361 attr = MSI_RecordGetInteger( rec, 8 );
2362 if (attr & msidbControlAttributesVisible)
2363 style |= WS_VISIBLE;
2364 if (~attr & msidbControlAttributesEnabled)
2365 style |= WS_DISABLED;
2366 if (attr & msidbControlAttributesHasBorder)
2367 style |= BS_GROUPBOX;
2368 else
2369 style |= BS_OWNERDRAW;
2371 /* Create parent group box to hold radio buttons */
2372 control = msi_dialog_add_control( dialog, rec, szButton, style );
2373 if( !control )
2374 return ERROR_FUNCTION_FAILED;
2376 oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2377 (LONG_PTR)MSIRadioGroup_WndProc );
2378 SetPropW(control->hwnd, szButtonData, oldproc);
2379 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
2381 if( prop )
2382 control->property = strdupW( prop );
2384 /* query the Radio Button table for all control in this group */
2385 r = MSI_OpenQuery( package->db, &view, query, prop );
2386 if( r != ERROR_SUCCESS )
2388 ERR("query failed for dialog %s radio group %s\n",
2389 debugstr_w(dialog->name), debugstr_w(prop));
2390 return ERROR_INVALID_PARAMETER;
2393 group.dialog = dialog;
2394 group.parent = control;
2395 group.propval = msi_dup_property( dialog->package->db, control->property );
2397 r = MSI_IterateRecords( view, 0, msi_dialog_create_radiobutton, &group );
2398 msiobj_release( &view->hdr );
2399 msi_free( group.propval );
2400 return r;
2403 static void
2404 msi_seltree_sync_item_state( HWND hwnd, MSIFEATURE *feature, HTREEITEM hItem )
2406 TVITEMW tvi;
2407 DWORD index = feature->ActionRequest;
2409 TRACE("Feature %s -> %d %d %d\n", debugstr_w(feature->Title),
2410 feature->Installed, feature->Action, feature->ActionRequest);
2412 if (index == INSTALLSTATE_UNKNOWN)
2413 index = INSTALLSTATE_ABSENT;
2415 tvi.mask = TVIF_STATE;
2416 tvi.hItem = hItem;
2417 tvi.state = INDEXTOSTATEIMAGEMASK( index );
2418 tvi.stateMask = TVIS_STATEIMAGEMASK;
2420 SendMessageW( hwnd, TVM_SETITEMW, 0, (LPARAM) &tvi );
2423 static UINT
2424 msi_seltree_popup_menu( HWND hwnd, INT x, INT y )
2426 HMENU hMenu;
2427 INT r;
2429 /* create a menu to display */
2430 hMenu = CreatePopupMenu();
2432 /* FIXME: load strings from resources */
2433 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_LOCAL, "Install feature locally");
2434 AppendMenuA( hMenu, MF_ENABLED, USER_INSTALLSTATE_ALL, "Install entire feature");
2435 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ADVERTISED, "Install on demand");
2436 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ABSENT, "Don't install");
2437 r = TrackPopupMenu( hMenu, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RETURNCMD,
2438 x, y, 0, hwnd, NULL );
2439 DestroyMenu( hMenu );
2440 return r;
2443 static void
2444 msi_seltree_update_feature_installstate( HWND hwnd, HTREEITEM hItem,
2445 MSIPACKAGE *package, MSIFEATURE *feature, INSTALLSTATE state )
2447 feature->ActionRequest = state;
2448 msi_seltree_sync_item_state( hwnd, feature, hItem );
2449 ACTION_UpdateComponentStates( package, feature );
2452 static void
2453 msi_seltree_update_siblings_and_children_installstate( HWND hwnd, HTREEITEM curr,
2454 MSIPACKAGE *package, INSTALLSTATE state)
2456 /* update all siblings */
2459 MSIFEATURE *feature;
2460 HTREEITEM child;
2462 feature = msi_seltree_feature_from_item( hwnd, curr );
2463 msi_seltree_update_feature_installstate( hwnd, curr, package, feature, state );
2465 /* update this sibling's children */
2466 child = (HTREEITEM)SendMessageW( hwnd, TVM_GETNEXTITEM, (WPARAM)TVGN_CHILD, (LPARAM)curr );
2467 if (child)
2468 msi_seltree_update_siblings_and_children_installstate( hwnd, child,
2469 package, state );
2471 while ((curr = (HTREEITEM)SendMessageW( hwnd, TVM_GETNEXTITEM, (WPARAM)TVGN_NEXT, (LPARAM)curr )));
2474 static LRESULT
2475 msi_seltree_menu( HWND hwnd, HTREEITEM hItem )
2477 struct msi_selection_tree_info *info;
2478 MSIFEATURE *feature;
2479 MSIPACKAGE *package;
2480 union {
2481 RECT rc;
2482 POINT pt[2];
2483 HTREEITEM hItem;
2484 } u;
2485 UINT r;
2487 info = GetPropW(hwnd, szButtonData);
2488 package = info->dialog->package;
2490 feature = msi_seltree_feature_from_item( hwnd, hItem );
2491 if (!feature)
2493 ERR("item %p feature was NULL\n", hItem);
2494 return 0;
2497 /* get the item's rectangle to put the menu just below it */
2498 u.hItem = hItem;
2499 SendMessageW( hwnd, TVM_GETITEMRECT, 0, (LPARAM) &u.rc );
2500 MapWindowPoints( hwnd, NULL, u.pt, 2 );
2502 r = msi_seltree_popup_menu( hwnd, u.rc.left, u.rc.top );
2504 switch (r)
2506 case USER_INSTALLSTATE_ALL:
2507 r = INSTALLSTATE_LOCAL;
2508 /* fall-through */
2509 case INSTALLSTATE_ADVERTISED:
2510 case INSTALLSTATE_ABSENT:
2512 HTREEITEM child;
2513 child = (HTREEITEM)SendMessageW( hwnd, TVM_GETNEXTITEM, (WPARAM)TVGN_CHILD, (LPARAM)hItem );
2514 if (child)
2515 msi_seltree_update_siblings_and_children_installstate( hwnd, child, package, r );
2517 /* fall-through */
2518 case INSTALLSTATE_LOCAL:
2519 msi_seltree_update_feature_installstate( hwnd, hItem, package, feature, r );
2520 break;
2523 return 0;
2526 static LRESULT WINAPI
2527 MSISelectionTree_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2529 struct msi_selection_tree_info *info;
2530 TVHITTESTINFO tvhti;
2531 HRESULT r;
2533 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
2535 info = GetPropW(hWnd, szButtonData);
2537 switch( msg )
2539 case WM_LBUTTONDOWN:
2540 tvhti.pt.x = (short)LOWORD( lParam );
2541 tvhti.pt.y = (short)HIWORD( lParam );
2542 tvhti.flags = 0;
2543 tvhti.hItem = 0;
2544 CallWindowProcW(info->oldproc, hWnd, TVM_HITTEST, 0, (LPARAM) &tvhti );
2545 if (tvhti.flags & TVHT_ONITEMSTATEICON)
2546 return msi_seltree_menu( hWnd, tvhti.hItem );
2547 break;
2549 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
2551 switch( msg )
2553 case WM_NCDESTROY:
2554 msi_free( info );
2555 RemovePropW( hWnd, szButtonData );
2556 break;
2558 return r;
2561 static void
2562 msi_seltree_add_child_features( MSIPACKAGE *package, HWND hwnd,
2563 LPCWSTR parent, HTREEITEM hParent )
2565 struct msi_selection_tree_info *info = GetPropW( hwnd, szButtonData );
2566 MSIFEATURE *feature;
2567 TVINSERTSTRUCTW tvis;
2568 HTREEITEM hitem, hfirst = NULL;
2570 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
2572 if ( parent && feature->Feature_Parent && wcscmp( parent, feature->Feature_Parent ))
2573 continue;
2574 else if ( parent && !feature->Feature_Parent )
2575 continue;
2576 else if ( !parent && feature->Feature_Parent )
2577 continue;
2579 if ( !feature->Title )
2580 continue;
2582 if ( !feature->Display )
2583 continue;
2585 memset( &tvis, 0, sizeof tvis );
2586 tvis.hParent = hParent;
2587 tvis.hInsertAfter = TVI_LAST;
2588 tvis.u.item.mask = TVIF_TEXT | TVIF_PARAM;
2589 tvis.u.item.pszText = feature->Title;
2590 tvis.u.item.lParam = (LPARAM) feature;
2592 hitem = (HTREEITEM) SendMessageW( hwnd, TVM_INSERTITEMW, 0, (LPARAM) &tvis );
2593 if (!hitem)
2594 continue;
2596 if (!hfirst)
2597 hfirst = hitem;
2599 msi_seltree_sync_item_state( hwnd, feature, hitem );
2600 msi_seltree_add_child_features( package, hwnd,
2601 feature->Feature, hitem );
2603 /* the node is expanded if Display is odd */
2604 if ( feature->Display % 2 != 0 )
2605 SendMessageW( hwnd, TVM_EXPAND, TVE_EXPAND, (LPARAM) hitem );
2608 /* select the first item */
2609 SendMessageW( hwnd, TVM_SELECTITEM, TVGN_CARET | TVGN_DROPHILITE, (LPARAM) hfirst );
2610 info->selected = hfirst;
2613 static void msi_seltree_create_imagelist( HWND hwnd )
2615 const int bm_width = 32, bm_height = 16, bm_count = 3;
2616 const int bm_resource = 0x1001;
2617 HIMAGELIST himl;
2618 int i;
2619 HBITMAP hbmp;
2621 himl = ImageList_Create( bm_width, bm_height, FALSE, 4, 0 );
2622 if (!himl)
2624 ERR("failed to create image list\n");
2625 return;
2628 for (i=0; i<bm_count; i++)
2630 hbmp = LoadBitmapW( msi_hInstance, MAKEINTRESOURCEW(i+bm_resource) );
2631 if (!hbmp)
2633 ERR("failed to load bitmap %d\n", i);
2634 break;
2638 * Add a dummy bitmap at offset zero because the treeview
2639 * can't use it as a state mask (zero means no user state).
2641 if (!i)
2642 ImageList_Add( himl, hbmp, NULL );
2644 ImageList_Add( himl, hbmp, NULL );
2647 SendMessageW( hwnd, TVM_SETIMAGELIST, TVSIL_STATE, (LPARAM)himl );
2650 static UINT msi_dialog_seltree_handler( msi_dialog *dialog,
2651 msi_control *control, WPARAM param )
2653 struct msi_selection_tree_info *info = GetPropW( control->hwnd, szButtonData );
2654 LPNMTREEVIEWW tv = (LPNMTREEVIEWW)param;
2655 MSIRECORD *row, *rec;
2656 MSIFOLDER *folder;
2657 MSIFEATURE *feature;
2658 LPCWSTR dir, title = NULL;
2659 UINT r = ERROR_SUCCESS;
2661 static const WCHAR select[] = {
2662 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2663 '`','F','e','a','t','u','r','e','`',' ','W','H','E','R','E',' ',
2664 '`','T','i','t','l','e','`',' ','=',' ','\'','%','s','\'',0
2667 if (tv->hdr.code != TVN_SELCHANGINGW)
2668 return ERROR_SUCCESS;
2670 info->selected = tv->itemNew.hItem;
2672 if (!(tv->itemNew.mask & TVIF_TEXT))
2674 feature = msi_seltree_feature_from_item( control->hwnd, tv->itemNew.hItem );
2675 if (feature)
2676 title = feature->Title;
2678 else
2679 title = tv->itemNew.pszText;
2681 row = MSI_QueryGetRecord( dialog->package->db, select, title );
2682 if (!row)
2683 return ERROR_FUNCTION_FAILED;
2685 rec = MSI_CreateRecord( 1 );
2687 MSI_RecordSetStringW( rec, 1, MSI_RecordGetString( row, 4 ) );
2688 msi_event_fire( dialog->package, szSelectionDescription, rec );
2690 dir = MSI_RecordGetString( row, 7 );
2691 if (dir)
2693 folder = msi_get_loaded_folder( dialog->package, dir );
2694 if (!folder)
2696 r = ERROR_FUNCTION_FAILED;
2697 goto done;
2699 MSI_RecordSetStringW( rec, 1, folder->ResolvedTarget );
2701 else
2702 MSI_RecordSetStringW( rec, 1, NULL );
2704 msi_event_fire( dialog->package, szSelectionPath, rec );
2706 done:
2707 msiobj_release(&row->hdr);
2708 msiobj_release(&rec->hdr);
2710 return r;
2713 static UINT msi_dialog_selection_tree( msi_dialog *dialog, MSIRECORD *rec )
2715 msi_control *control;
2716 LPCWSTR prop, control_name;
2717 MSIPACKAGE *package = dialog->package;
2718 DWORD style;
2719 struct msi_selection_tree_info *info;
2721 info = msi_alloc( sizeof *info );
2722 if (!info)
2723 return ERROR_FUNCTION_FAILED;
2725 /* create the treeview control */
2726 style = TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT;
2727 style |= WS_GROUP | WS_VSCROLL | WS_TABSTOP;
2728 control = msi_dialog_add_control( dialog, rec, WC_TREEVIEWW, style );
2729 if (!control)
2731 msi_free(info);
2732 return ERROR_FUNCTION_FAILED;
2735 control->handler = msi_dialog_seltree_handler;
2736 control_name = MSI_RecordGetString( rec, 2 );
2737 control->attributes = MSI_RecordGetInteger( rec, 8 );
2738 prop = MSI_RecordGetString( rec, 9 );
2739 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2741 /* subclass */
2742 info->dialog = dialog;
2743 info->hwnd = control->hwnd;
2744 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2745 (LONG_PTR)MSISelectionTree_WndProc );
2746 SetPropW( control->hwnd, szButtonData, info );
2748 event_subscribe( dialog, szSelectionPath, control_name, szProperty );
2750 /* initialize it */
2751 msi_seltree_create_imagelist( control->hwnd );
2752 msi_seltree_add_child_features( package, control->hwnd, NULL, NULL );
2754 return ERROR_SUCCESS;
2757 /******************** Group Box ***************************************/
2759 static UINT msi_dialog_group_box( msi_dialog *dialog, MSIRECORD *rec )
2761 msi_control *control;
2762 DWORD style;
2764 style = BS_GROUPBOX | WS_CHILD | WS_GROUP;
2765 control = msi_dialog_add_control( dialog, rec, WC_BUTTONW, style );
2766 if (!control)
2767 return ERROR_FUNCTION_FAILED;
2769 return ERROR_SUCCESS;
2772 /******************** List Box ***************************************/
2774 struct msi_listbox_info
2776 msi_dialog *dialog;
2777 HWND hwnd;
2778 WNDPROC oldproc;
2779 DWORD num_items;
2780 DWORD addpos_items;
2781 LPWSTR *items;
2784 static LRESULT WINAPI MSIListBox_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2786 struct msi_listbox_info *info;
2787 LRESULT r;
2788 DWORD j;
2790 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
2792 info = GetPropW( hWnd, szButtonData );
2793 if (!info)
2794 return 0;
2796 r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
2798 switch( msg )
2800 case WM_NCDESTROY:
2801 for (j = 0; j < info->num_items; j++)
2802 msi_free( info->items[j] );
2803 msi_free( info->items );
2804 msi_free( info );
2805 RemovePropW( hWnd, szButtonData );
2806 break;
2809 return r;
2812 static UINT msi_listbox_add_item( MSIRECORD *rec, LPVOID param )
2814 struct msi_listbox_info *info = param;
2815 LPCWSTR value, text;
2816 int pos;
2818 value = MSI_RecordGetString( rec, 3 );
2819 text = MSI_RecordGetString( rec, 4 );
2821 info->items[info->addpos_items] = strdupW( value );
2823 pos = SendMessageW( info->hwnd, LB_ADDSTRING, 0, (LPARAM)text );
2824 SendMessageW( info->hwnd, LB_SETITEMDATA, pos, (LPARAM)info->items[info->addpos_items] );
2825 info->addpos_items++;
2826 return ERROR_SUCCESS;
2829 static UINT msi_listbox_add_items( struct msi_listbox_info *info, LPCWSTR property )
2831 static const WCHAR query[] = {
2832 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2833 '`','L','i','s','t','B','o','x','`',' ','W','H','E','R','E',' ',
2834 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',' ',
2835 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','`',0};
2836 MSIQUERY *view;
2837 DWORD count;
2838 UINT r;
2840 r = MSI_OpenQuery( info->dialog->package->db, &view, query, property );
2841 if ( r != ERROR_SUCCESS )
2842 return r;
2844 /* just get the number of records */
2845 count = 0;
2846 r = MSI_IterateRecords( view, &count, NULL, NULL );
2847 if (r != ERROR_SUCCESS)
2849 msiobj_release( &view->hdr );
2850 return r;
2852 info->num_items = count;
2853 info->items = msi_alloc( sizeof(*info->items) * count );
2855 r = MSI_IterateRecords( view, NULL, msi_listbox_add_item, info );
2856 msiobj_release( &view->hdr );
2857 return r;
2860 static UINT msi_dialog_listbox_handler( msi_dialog *dialog,
2861 msi_control *control, WPARAM param )
2863 struct msi_listbox_info *info;
2864 int index;
2865 LPCWSTR value;
2867 if( HIWORD(param) != LBN_SELCHANGE )
2868 return ERROR_SUCCESS;
2870 info = GetPropW( control->hwnd, szButtonData );
2871 index = SendMessageW( control->hwnd, LB_GETCURSEL, 0, 0 );
2872 value = (LPCWSTR) SendMessageW( control->hwnd, LB_GETITEMDATA, index, 0 );
2874 msi_dialog_set_property( info->dialog->package, control->property, value );
2875 msi_dialog_evaluate_control_conditions( info->dialog );
2877 return ERROR_SUCCESS;
2880 static UINT msi_dialog_list_box( msi_dialog *dialog, MSIRECORD *rec )
2882 struct msi_listbox_info *info;
2883 msi_control *control;
2884 DWORD attributes, style;
2885 LPCWSTR prop;
2887 info = msi_alloc( sizeof *info );
2888 if (!info)
2889 return ERROR_FUNCTION_FAILED;
2891 style = WS_TABSTOP | WS_GROUP | WS_CHILD | LBS_NOTIFY | WS_VSCROLL | WS_BORDER;
2892 attributes = MSI_RecordGetInteger( rec, 8 );
2893 if (~attributes & msidbControlAttributesSorted)
2894 style |= LBS_SORT;
2896 control = msi_dialog_add_control( dialog, rec, WC_LISTBOXW, style );
2897 if (!control)
2899 msi_free(info);
2900 return ERROR_FUNCTION_FAILED;
2903 control->handler = msi_dialog_listbox_handler;
2905 prop = MSI_RecordGetString( rec, 9 );
2906 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2908 /* subclass */
2909 info->dialog = dialog;
2910 info->hwnd = control->hwnd;
2911 info->items = NULL;
2912 info->addpos_items = 0;
2913 info->oldproc = (WNDPROC)SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2914 (LONG_PTR)MSIListBox_WndProc );
2915 SetPropW( control->hwnd, szButtonData, info );
2917 if ( control->property )
2918 msi_listbox_add_items( info, control->property );
2920 return ERROR_SUCCESS;
2923 /******************** Directory Combo ***************************************/
2925 static void msi_dialog_update_directory_combo( msi_dialog *dialog, msi_control *control )
2927 WCHAR *path;
2929 if (!control && !(control = msi_dialog_find_control_by_type( dialog, szDirectoryCombo )))
2930 return;
2932 if (!(path = get_path_property( dialog, control ))) return;
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 );
2942 static UINT msi_dialog_directory_combo( msi_dialog *dialog, MSIRECORD *rec )
2944 msi_control *control;
2945 LPCWSTR prop;
2946 DWORD style;
2948 /* FIXME: use CBS_OWNERDRAWFIXED and add owner draw code */
2949 style = CBS_DROPDOWNLIST | CBS_HASSTRINGS | WS_CHILD |
2950 WS_GROUP | WS_TABSTOP | WS_VSCROLL;
2951 control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
2952 if (!control)
2953 return ERROR_FUNCTION_FAILED;
2955 control->attributes = MSI_RecordGetInteger( rec, 8 );
2956 prop = MSI_RecordGetString( rec, 9 );
2957 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2959 msi_dialog_update_directory_combo( dialog, control );
2961 return ERROR_SUCCESS;
2964 /******************** Directory List ***************************************/
2966 static void msi_dialog_update_directory_list( msi_dialog *dialog, msi_control *control )
2968 static const WCHAR asterisk[] = {'*',0};
2969 WCHAR dir_spec[MAX_PATH], *path;
2970 WIN32_FIND_DATAW wfd;
2971 LVITEMW item;
2972 HANDLE file;
2974 if (!control && !(control = msi_dialog_find_control_by_type( dialog, szDirectoryList )))
2975 return;
2977 /* clear the list-view */
2978 SendMessageW( control->hwnd, LVM_DELETEALLITEMS, 0, 0 );
2980 if (!(path = get_path_property( dialog, control ))) return;
2981 lstrcpyW( dir_spec, path );
2982 lstrcatW( dir_spec, asterisk );
2984 file = FindFirstFileW( dir_spec, &wfd );
2985 if (file == INVALID_HANDLE_VALUE)
2987 msi_free( path );
2988 return;
2993 if ( wfd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY )
2994 continue;
2996 if ( !wcscmp( wfd.cFileName, szDot ) || !wcscmp( wfd.cFileName, szDotDot ) )
2997 continue;
2999 item.mask = LVIF_TEXT;
3000 item.cchTextMax = MAX_PATH;
3001 item.iItem = 0;
3002 item.iSubItem = 0;
3003 item.pszText = wfd.cFileName;
3005 SendMessageW( control->hwnd, LVM_INSERTITEMW, 0, (LPARAM)&item );
3006 } while ( FindNextFileW( file, &wfd ) );
3008 msi_free( path );
3009 FindClose( file );
3012 static UINT msi_dialog_directorylist_up( msi_dialog *dialog )
3014 msi_control *control;
3015 LPWSTR prop, path, ptr;
3016 BOOL indirect;
3018 control = msi_dialog_find_control_by_type( dialog, szDirectoryList );
3019 indirect = control->attributes & msidbControlAttributesIndirect;
3020 prop = msi_dialog_dup_property( dialog, control->property, indirect );
3021 path = msi_dialog_dup_property( dialog, prop, TRUE );
3023 /* strip off the last directory */
3024 ptr = PathFindFileNameW( path );
3025 if (ptr != path) *(ptr - 1) = '\0';
3026 PathAddBackslashW( path );
3028 msi_dialog_set_property( dialog->package, prop, path );
3030 msi_dialog_update_directory_list( dialog, NULL );
3031 msi_dialog_update_directory_combo( dialog, NULL );
3032 msi_dialog_update_pathedit( dialog, NULL );
3034 msi_free( path );
3035 msi_free( prop );
3037 return ERROR_SUCCESS;
3040 static WCHAR *get_unique_folder_name( const WCHAR *root, int *ret_len )
3042 static const WCHAR fmtW[] = {'%','s','%','s',' ','%','u',0};
3043 WCHAR newfolder[MAX_PATH], *path, *ptr;
3044 int len, count = 2;
3046 len = LoadStringW( msi_hInstance, IDS_NEWFOLDER, newfolder, ARRAY_SIZE(newfolder) );
3047 len += lstrlenW(root) + 1;
3048 if (!(path = msi_alloc( (len + 4) * sizeof(WCHAR) ))) return NULL;
3049 lstrcpyW( path, root );
3050 lstrcatW( path, newfolder );
3052 for (;;)
3054 if (GetFileAttributesW( path ) == INVALID_FILE_ATTRIBUTES) break;
3055 if (count > 99)
3057 msi_free( path );
3058 return NULL;
3060 swprintf( path, len + 4, fmtW, root, newfolder, count++ );
3063 ptr = wcsrchr( path, '\\' ) + 1;
3064 *ret_len = lstrlenW(ptr);
3065 memmove( path, ptr, *ret_len * sizeof(WCHAR) );
3066 return path;
3069 static UINT msi_dialog_directorylist_new( msi_dialog *dialog )
3071 msi_control *control;
3072 WCHAR *path;
3073 LVITEMW item;
3074 int index;
3076 control = msi_dialog_find_control_by_type( dialog, szDirectoryList );
3078 if (!(path = get_path_property( dialog, control ))) return ERROR_OUTOFMEMORY;
3080 item.mask = LVIF_TEXT;
3081 item.iItem = 0;
3082 item.iSubItem = 0;
3083 item.pszText = get_unique_folder_name( path, &item.cchTextMax );
3085 index = SendMessageW( control->hwnd, LVM_INSERTITEMW, 0, (LPARAM)&item );
3086 SendMessageW( control->hwnd, LVM_ENSUREVISIBLE, index, 0 );
3087 SendMessageW( control->hwnd, LVM_EDITLABELW, index, -1 );
3089 msi_free( path );
3090 msi_free( item.pszText );
3091 return ERROR_SUCCESS;
3094 static UINT msi_dialog_dirlist_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
3096 NMHDR *nmhdr = (NMHDR *)param;
3097 WCHAR text[MAX_PATH], *new_path, *path, *prop;
3098 BOOL indirect;
3100 switch (nmhdr->code)
3102 case LVN_ENDLABELEDITW:
3104 NMLVDISPINFOW *info = (NMLVDISPINFOW *)param;
3105 if (!info->item.pszText) return ERROR_SUCCESS;
3106 lstrcpynW( text, info->item.pszText, ARRAY_SIZE(text) );
3107 text[ARRAY_SIZE(text) - 1] = 0;
3108 break;
3110 case LVN_ITEMACTIVATE:
3112 LVITEMW item;
3113 int index = SendMessageW( control->hwnd, LVM_GETNEXTITEM, -1, LVNI_SELECTED );
3114 if (index < 0)
3116 ERR("no list-view item selected\n");
3117 return ERROR_FUNCTION_FAILED;
3120 item.iSubItem = 0;
3121 item.pszText = text;
3122 item.cchTextMax = MAX_PATH;
3123 SendMessageW( control->hwnd, LVM_GETITEMTEXTW, index, (LPARAM)&item );
3124 text[ARRAY_SIZE(text) - 1] = 0;
3125 break;
3127 default:
3128 return ERROR_SUCCESS;
3131 indirect = control->attributes & msidbControlAttributesIndirect;
3132 prop = msi_dialog_dup_property( dialog, control->property, indirect );
3133 path = msi_dialog_dup_property( dialog, prop, TRUE );
3135 if (!(new_path = msi_alloc( (lstrlenW(path) + lstrlenW(text) + 2) * sizeof(WCHAR) )))
3137 msi_free( prop );
3138 msi_free( path );
3139 return ERROR_OUTOFMEMORY;
3141 lstrcpyW( new_path, path );
3142 lstrcatW( new_path, text );
3143 if (nmhdr->code == LVN_ENDLABELEDITW) CreateDirectoryW( new_path, NULL );
3144 lstrcatW( new_path, szBackSlash );
3146 msi_dialog_set_property( dialog->package, prop, new_path );
3148 msi_dialog_update_directory_list( dialog, NULL );
3149 msi_dialog_update_directory_combo( dialog, NULL );
3150 msi_dialog_update_pathedit( dialog, NULL );
3152 msi_free( prop );
3153 msi_free( path );
3154 msi_free( new_path );
3156 return ERROR_SUCCESS;
3159 static UINT msi_dialog_directory_list( msi_dialog *dialog, MSIRECORD *rec )
3161 msi_control *control;
3162 LPCWSTR prop;
3163 DWORD style;
3165 style = LVS_LIST | WS_VSCROLL | LVS_SHAREIMAGELISTS | LVS_EDITLABELS |
3166 LVS_AUTOARRANGE | LVS_SINGLESEL | WS_BORDER |
3167 LVS_SORTASCENDING | WS_CHILD | WS_GROUP | WS_TABSTOP;
3168 control = msi_dialog_add_control( dialog, rec, WC_LISTVIEWW, style );
3169 if (!control)
3170 return ERROR_FUNCTION_FAILED;
3172 control->attributes = MSI_RecordGetInteger( rec, 8 );
3173 control->handler = msi_dialog_dirlist_handler;
3174 prop = MSI_RecordGetString( rec, 9 );
3175 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
3177 /* double click to activate an item in the list */
3178 SendMessageW( control->hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE,
3179 0, LVS_EX_TWOCLICKACTIVATE );
3181 msi_dialog_update_directory_list( dialog, control );
3183 return ERROR_SUCCESS;
3186 /******************** VolumeCost List ***************************************/
3188 static BOOL str_is_number( LPCWSTR str )
3190 int i;
3192 for (i = 0; i < lstrlenW( str ); i++)
3193 if (!iswdigit(str[i]))
3194 return FALSE;
3196 return TRUE;
3199 static const WCHAR column_keys[][80] =
3201 {'V','o','l','u','m','e','C','o','s','t','V','o','l','u','m','e',0},
3202 {'V','o','l','u','m','e','C','o','s','t','S','i','z','e',0},
3203 {'V','o','l','u','m','e','C','o','s','t','A','v','a','i','l','a','b','l','e',0},
3204 {'V','o','l','u','m','e','C','o','s','t','R','e','q','u','i','r','e','d',0},
3205 {'V','o','l','u','m','e','C','o','s','t','D','i','f','f','e','r','e','n','c','e',0}
3208 static void msi_dialog_vcl_add_columns( msi_dialog *dialog, msi_control *control, MSIRECORD *rec )
3210 LPCWSTR text = MSI_RecordGetString( rec, 10 );
3211 LPCWSTR begin = text, end;
3212 WCHAR *num;
3213 LVCOLUMNW lvc;
3214 DWORD count = 0;
3216 static const WCHAR negative[] = {'-',0};
3218 if (!text) return;
3220 while ((begin = wcschr( begin, '{' )) && count < 5)
3222 if (!(end = wcschr( begin, '}' )))
3223 return;
3225 num = msi_alloc( (end-begin+1)*sizeof(WCHAR) );
3226 if (!num)
3227 return;
3229 lstrcpynW( num, begin + 1, end - begin );
3230 begin += end - begin + 1;
3232 /* empty braces or '0' hides the column */
3233 if ( !num[0] || !wcscmp( num, szZero ) )
3235 count++;
3236 msi_free( num );
3237 continue;
3240 /* the width must be a positive number
3241 * if a width is invalid, all remaining columns are hidden
3243 if ( !wcsncmp( num, negative, 1 ) || !str_is_number( num ) ) {
3244 msi_free( num );
3245 return;
3248 ZeroMemory( &lvc, sizeof(lvc) );
3249 lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
3250 lvc.cx = wcstol( num, NULL, 10 );
3251 lvc.pszText = msi_dialog_get_uitext( dialog, column_keys[count] );
3253 SendMessageW( control->hwnd, LVM_INSERTCOLUMNW, count++, (LPARAM)&lvc );
3254 msi_free( lvc.pszText );
3255 msi_free( num );
3259 static LONGLONG msi_vcl_get_cost( msi_dialog *dialog )
3261 MSIFEATURE *feature;
3262 INT each_cost;
3263 LONGLONG total_cost = 0;
3265 LIST_FOR_EACH_ENTRY( feature, &dialog->package->features, MSIFEATURE, entry )
3267 if (ERROR_SUCCESS == (MSI_GetFeatureCost(dialog->package, feature,
3268 MSICOSTTREE_SELFONLY, INSTALLSTATE_LOCAL, &each_cost)))
3270 /* each_cost is in 512-byte units */
3271 total_cost += each_cost * 512;
3273 if (ERROR_SUCCESS == (MSI_GetFeatureCost(dialog->package, feature,
3274 MSICOSTTREE_SELFONLY, INSTALLSTATE_ABSENT, &each_cost)))
3276 /* each_cost is in 512-byte units */
3277 total_cost -= each_cost * 512;
3280 return total_cost;
3283 static void msi_dialog_vcl_add_drives( msi_dialog *dialog, msi_control *control )
3285 ULARGE_INTEGER total, free;
3286 LONGLONG difference, cost;
3287 WCHAR size_text[MAX_PATH];
3288 WCHAR cost_text[MAX_PATH];
3289 LPWSTR drives, ptr;
3290 LVITEMW lvitem;
3291 DWORD size, flags;
3292 int i = 0;
3294 cost = msi_vcl_get_cost(dialog);
3295 StrFormatByteSizeW(cost, cost_text, MAX_PATH);
3297 size = GetLogicalDriveStringsW( 0, NULL );
3298 if ( !size ) return;
3300 drives = msi_alloc( (size + 1) * sizeof(WCHAR) );
3301 if ( !drives ) return;
3303 GetLogicalDriveStringsW( size, drives );
3305 ptr = drives;
3306 while (*ptr)
3308 if (GetVolumeInformationW(ptr, NULL, 0, NULL, 0, &flags, NULL, 0) &&
3309 flags & FILE_READ_ONLY_VOLUME)
3311 ptr += lstrlenW(ptr) + 1;
3312 continue;
3315 lvitem.mask = LVIF_TEXT;
3316 lvitem.iItem = i;
3317 lvitem.iSubItem = 0;
3318 lvitem.pszText = ptr;
3319 lvitem.cchTextMax = lstrlenW(ptr) + 1;
3320 SendMessageW( control->hwnd, LVM_INSERTITEMW, 0, (LPARAM)&lvitem );
3322 GetDiskFreeSpaceExW(ptr, &free, &total, NULL);
3323 difference = free.QuadPart - cost;
3325 StrFormatByteSizeW(total.QuadPart, size_text, MAX_PATH);
3326 lvitem.iSubItem = 1;
3327 lvitem.pszText = size_text;
3328 lvitem.cchTextMax = lstrlenW(size_text) + 1;
3329 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
3331 StrFormatByteSizeW(free.QuadPart, size_text, MAX_PATH);
3332 lvitem.iSubItem = 2;
3333 lvitem.pszText = size_text;
3334 lvitem.cchTextMax = lstrlenW(size_text) + 1;
3335 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
3337 lvitem.iSubItem = 3;
3338 lvitem.pszText = cost_text;
3339 lvitem.cchTextMax = lstrlenW(cost_text) + 1;
3340 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
3342 StrFormatByteSizeW(difference, size_text, MAX_PATH);
3343 lvitem.iSubItem = 4;
3344 lvitem.pszText = size_text;
3345 lvitem.cchTextMax = lstrlenW(size_text) + 1;
3346 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
3348 ptr += lstrlenW(ptr) + 1;
3349 i++;
3352 msi_free( drives );
3355 static UINT msi_dialog_volumecost_list( msi_dialog *dialog, MSIRECORD *rec )
3357 msi_control *control;
3358 DWORD style;
3360 style = LVS_REPORT | WS_VSCROLL | WS_HSCROLL | LVS_SHAREIMAGELISTS |
3361 LVS_AUTOARRANGE | LVS_SINGLESEL | WS_BORDER |
3362 WS_CHILD | WS_TABSTOP | WS_GROUP;
3363 control = msi_dialog_add_control( dialog, rec, WC_LISTVIEWW, style );
3364 if (!control)
3365 return ERROR_FUNCTION_FAILED;
3367 msi_dialog_vcl_add_columns( dialog, control, rec );
3368 msi_dialog_vcl_add_drives( dialog, control );
3370 return ERROR_SUCCESS;
3373 /******************** VolumeSelect Combo ***************************************/
3375 static UINT msi_dialog_volsel_handler( msi_dialog *dialog,
3376 msi_control *control, WPARAM param )
3378 WCHAR text[MAX_PATH];
3379 LPWSTR prop;
3380 BOOL indirect;
3381 int index;
3383 if (HIWORD(param) != CBN_SELCHANGE)
3384 return ERROR_SUCCESS;
3386 index = SendMessageW( control->hwnd, CB_GETCURSEL, 0, 0 );
3387 if ( index == CB_ERR )
3389 ERR("No ComboBox item selected!\n");
3390 return ERROR_FUNCTION_FAILED;
3393 SendMessageW( control->hwnd, CB_GETLBTEXT, index, (LPARAM)text );
3395 indirect = control->attributes & msidbControlAttributesIndirect;
3396 prop = msi_dialog_dup_property( dialog, control->property, indirect );
3398 msi_dialog_set_property( dialog->package, prop, text );
3400 msi_free( prop );
3401 return ERROR_SUCCESS;
3404 static void msi_dialog_vsc_add_drives( msi_dialog *dialog, msi_control *control )
3406 LPWSTR drives, ptr;
3407 DWORD size;
3409 size = GetLogicalDriveStringsW( 0, NULL );
3410 if ( !size ) return;
3412 drives = msi_alloc( (size + 1) * sizeof(WCHAR) );
3413 if ( !drives ) return;
3415 GetLogicalDriveStringsW( size, drives );
3417 ptr = drives;
3418 while (*ptr)
3420 SendMessageW( control->hwnd, CB_ADDSTRING, 0, (LPARAM)ptr );
3421 ptr += lstrlenW(ptr) + 1;
3424 msi_free( drives );
3427 static UINT msi_dialog_volumeselect_combo( msi_dialog *dialog, MSIRECORD *rec )
3429 msi_control *control;
3430 LPCWSTR prop;
3431 DWORD style;
3433 /* FIXME: CBS_OWNERDRAWFIXED */
3434 style = WS_CHILD | WS_VISIBLE | WS_GROUP | WS_TABSTOP |
3435 CBS_DROPDOWNLIST | CBS_SORT | CBS_HASSTRINGS |
3436 WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
3437 control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
3438 if (!control)
3439 return ERROR_FUNCTION_FAILED;
3441 control->attributes = MSI_RecordGetInteger( rec, 8 );
3442 control->handler = msi_dialog_volsel_handler;
3443 prop = MSI_RecordGetString( rec, 9 );
3444 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
3446 msi_dialog_vsc_add_drives( dialog, control );
3448 return ERROR_SUCCESS;
3451 static UINT msi_dialog_hyperlink_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
3453 static const WCHAR hrefW[] = {'h','r','e','f'};
3454 static const WCHAR openW[] = {'o','p','e','n',0};
3455 int len, len_href = ARRAY_SIZE( hrefW );
3456 const WCHAR *p, *q;
3457 WCHAR quote = 0;
3458 LITEM item;
3460 item.mask = LIF_ITEMINDEX | LIF_URL;
3461 item.iLink = 0;
3462 item.szUrl[0] = 0;
3464 SendMessageW( control->hwnd, LM_GETITEM, 0, (LPARAM)&item );
3466 p = item.szUrl;
3467 while (*p && *p != '<') p++;
3468 if (!*p++) return ERROR_SUCCESS;
3469 if (towupper( *p++ ) != 'A' || !iswspace( *p++ )) return ERROR_SUCCESS;
3470 while (*p && iswspace( *p )) p++;
3472 len = lstrlenW( p );
3473 if (len > len_href && !wcsnicmp( p, hrefW, len_href ))
3475 p += len_href;
3476 while (*p && iswspace( *p )) p++;
3477 if (!*p || *p++ != '=') return ERROR_SUCCESS;
3478 while (*p && iswspace( *p )) p++;
3480 if (*p == '\"' || *p == '\'') quote = *p++;
3481 q = p;
3482 if (quote)
3484 while (*q && *q != quote) q++;
3485 if (*q != quote) return ERROR_SUCCESS;
3487 else
3489 while (*q && *q != '>' && !iswspace( *q )) q++;
3490 if (!*q) return ERROR_SUCCESS;
3492 item.szUrl[q - item.szUrl] = 0;
3493 ShellExecuteW( NULL, openW, p, NULL, NULL, SW_SHOWNORMAL );
3495 return ERROR_SUCCESS;
3498 static UINT msi_dialog_hyperlink( msi_dialog *dialog, MSIRECORD *rec )
3500 msi_control *control;
3501 DWORD style = WS_CHILD | WS_TABSTOP | WS_GROUP;
3502 const WCHAR *text = MSI_RecordGetString( rec, 10 );
3503 int len = lstrlenW( text );
3504 LITEM item;
3506 control = msi_dialog_add_control( dialog, rec, WC_LINK, style );
3507 if (!control)
3508 return ERROR_FUNCTION_FAILED;
3510 control->attributes = MSI_RecordGetInteger( rec, 8 );
3511 control->handler = msi_dialog_hyperlink_handler;
3513 item.mask = LIF_ITEMINDEX | LIF_STATE | LIF_URL;
3514 item.iLink = 0;
3515 item.state = LIS_ENABLED;
3516 item.stateMask = LIS_ENABLED;
3517 if (len < L_MAX_URL_LENGTH) lstrcpyW( item.szUrl, text );
3518 else item.szUrl[0] = 0;
3520 SendMessageW( control->hwnd, LM_SETITEM, 0, (LPARAM)&item );
3522 return ERROR_SUCCESS;
3525 static const struct control_handler msi_dialog_handler[] =
3527 { szText, msi_dialog_text_control },
3528 { szPushButton, msi_dialog_button_control },
3529 { szLine, msi_dialog_line_control },
3530 { szBitmap, msi_dialog_bitmap_control },
3531 { szCheckBox, msi_dialog_checkbox_control },
3532 { szScrollableText, msi_dialog_scrolltext_control },
3533 { szComboBox, msi_dialog_combo_control },
3534 { szEdit, msi_dialog_edit_control },
3535 { szMaskedEdit, msi_dialog_maskedit_control },
3536 { szPathEdit, msi_dialog_pathedit_control },
3537 { szProgressBar, msi_dialog_progress_bar },
3538 { szRadioButtonGroup, msi_dialog_radiogroup_control },
3539 { szIcon, msi_dialog_icon_control },
3540 { szSelectionTree, msi_dialog_selection_tree },
3541 { szGroupBox, msi_dialog_group_box },
3542 { szListBox, msi_dialog_list_box },
3543 { szDirectoryCombo, msi_dialog_directory_combo },
3544 { szDirectoryList, msi_dialog_directory_list },
3545 { szVolumeCostList, msi_dialog_volumecost_list },
3546 { szVolumeSelectCombo, msi_dialog_volumeselect_combo },
3547 { szHyperLink, msi_dialog_hyperlink }
3550 static UINT msi_dialog_create_controls( MSIRECORD *rec, LPVOID param )
3552 msi_dialog *dialog = param;
3553 LPCWSTR control_type;
3554 UINT i;
3556 /* find and call the function that can create this type of control */
3557 control_type = MSI_RecordGetString( rec, 3 );
3558 for( i = 0; i < ARRAY_SIZE( msi_dialog_handler ); i++ )
3559 if (!wcsicmp( msi_dialog_handler[i].control_type, control_type ))
3560 break;
3561 if( i != ARRAY_SIZE( msi_dialog_handler ))
3562 msi_dialog_handler[i].func( dialog, rec );
3563 else
3564 ERR("no handler for element type %s\n", debugstr_w(control_type));
3566 return ERROR_SUCCESS;
3569 static UINT msi_dialog_fill_controls( msi_dialog *dialog )
3571 static const WCHAR query[] = {
3572 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
3573 'C','o','n','t','r','o','l',' ','W','H','E','R','E',' ',
3574 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
3575 UINT r;
3576 MSIQUERY *view;
3577 MSIPACKAGE *package = dialog->package;
3579 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
3581 /* query the Control table for all the elements of the control */
3582 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
3583 if( r != ERROR_SUCCESS )
3585 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
3586 return ERROR_INVALID_PARAMETER;
3589 r = MSI_IterateRecords( view, 0, msi_dialog_create_controls, dialog );
3590 msiobj_release( &view->hdr );
3591 return r;
3594 static UINT msi_dialog_reset( msi_dialog *dialog )
3596 /* FIXME: should restore the original values of any properties we changed */
3597 return msi_dialog_evaluate_control_conditions( dialog );
3600 /* figure out the height of 10 point MS Sans Serif */
3601 static INT msi_dialog_get_sans_serif_height( HWND hwnd )
3603 static const WCHAR szSansSerif[] = {
3604 'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0 };
3605 LOGFONTW lf;
3606 TEXTMETRICW tm;
3607 BOOL r;
3608 LONG height = 0;
3609 HFONT hFont, hOldFont;
3610 HDC hdc;
3612 hdc = GetDC( hwnd );
3613 if (hdc)
3615 memset( &lf, 0, sizeof lf );
3616 lf.lfHeight = MulDiv(12, GetDeviceCaps(hdc, LOGPIXELSY), 72);
3617 lstrcpyW( lf.lfFaceName, szSansSerif );
3618 hFont = CreateFontIndirectW(&lf);
3619 if (hFont)
3621 hOldFont = SelectObject( hdc, hFont );
3622 r = GetTextMetricsW( hdc, &tm );
3623 if (r)
3624 height = tm.tmHeight;
3625 SelectObject( hdc, hOldFont );
3626 DeleteObject( hFont );
3628 ReleaseDC( hwnd, hdc );
3630 return height;
3633 /* fetch the associated record from the Dialog table */
3634 static MSIRECORD *msi_get_dialog_record( msi_dialog *dialog )
3636 static const WCHAR query[] = {
3637 'S','E','L','E','C','T',' ','*',' ',
3638 'F','R','O','M',' ','D','i','a','l','o','g',' ',
3639 'W','H','E','R','E',' ',
3640 '`','D','i','a','l','o','g','`',' ','=',' ','\'','%','s','\'',0};
3641 MSIPACKAGE *package = dialog->package;
3642 MSIRECORD *rec = NULL;
3644 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
3646 rec = MSI_QueryGetRecord( package->db, query, dialog->name );
3647 if( !rec )
3648 WARN("query failed for dialog %s\n", debugstr_w(dialog->name));
3650 return rec;
3653 static void msi_dialog_adjust_dialog_pos( msi_dialog *dialog, MSIRECORD *rec, LPRECT pos )
3655 static const WCHAR szScreenX[] = {'S','c','r','e','e','n','X',0};
3656 static const WCHAR szScreenY[] = {'S','c','r','e','e','n','Y',0};
3658 UINT xres, yres;
3659 POINT center;
3660 SIZE sz;
3661 LONG style;
3663 center.x = MSI_RecordGetInteger( rec, 2 );
3664 center.y = MSI_RecordGetInteger( rec, 3 );
3666 sz.cx = MSI_RecordGetInteger( rec, 4 );
3667 sz.cy = MSI_RecordGetInteger( rec, 5 );
3669 sz.cx = msi_dialog_scale_unit( dialog, sz.cx );
3670 sz.cy = msi_dialog_scale_unit( dialog, sz.cy );
3672 xres = msi_get_property_int( dialog->package->db, szScreenX, 0 );
3673 yres = msi_get_property_int( dialog->package->db, szScreenY, 0 );
3675 center.x = MulDiv( center.x, xres, 100 );
3676 center.y = MulDiv( center.y, yres, 100 );
3678 /* turn the client pos into the window rectangle */
3679 if (dialog->package->center_x && dialog->package->center_y)
3681 pos->left = dialog->package->center_x - sz.cx / 2.0;
3682 pos->right = pos->left + sz.cx;
3683 pos->top = dialog->package->center_y - sz.cy / 2.0;
3684 pos->bottom = pos->top + sz.cy;
3686 else
3688 pos->left = center.x - sz.cx/2;
3689 pos->right = pos->left + sz.cx;
3690 pos->top = center.y - sz.cy/2;
3691 pos->bottom = pos->top + sz.cy;
3693 /* save the center */
3694 dialog->package->center_x = center.x;
3695 dialog->package->center_y = center.y;
3698 dialog->size.cx = sz.cx;
3699 dialog->size.cy = sz.cy;
3701 TRACE("%s\n", wine_dbgstr_rect(pos));
3703 style = GetWindowLongPtrW( dialog->hwnd, GWL_STYLE );
3704 AdjustWindowRect( pos, style, FALSE );
3707 static void msi_dialog_set_tab_order( msi_dialog *dialog, LPCWSTR first )
3709 struct list tab_chain;
3710 msi_control *control;
3711 HWND prev = HWND_TOP;
3713 list_init( &tab_chain );
3714 if (!(control = msi_dialog_find_control( dialog, first ))) return;
3716 dialog->hWndFocus = control->hwnd;
3717 while (control)
3719 list_remove( &control->entry );
3720 list_add_tail( &tab_chain, &control->entry );
3721 if (!control->tabnext) break;
3722 control = msi_dialog_find_control( dialog, control->tabnext );
3725 LIST_FOR_EACH_ENTRY( control, &tab_chain, msi_control, entry )
3727 SetWindowPos( control->hwnd, prev, 0, 0, 0, 0,
3728 SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOREDRAW |
3729 SWP_NOREPOSITION | SWP_NOSENDCHANGING | SWP_NOSIZE );
3730 prev = control->hwnd;
3733 /* put them back on the main list */
3734 list_move_head( &dialog->controls, &tab_chain );
3737 static LRESULT msi_dialog_oncreate( HWND hwnd, LPCREATESTRUCTW cs )
3739 static const WCHAR df[] = {
3740 'D','e','f','a','u','l','t','U','I','F','o','n','t',0 };
3741 static const WCHAR dfv[] = {
3742 'M','S',' ','S','h','e','l','l',' ','D','l','g',0 };
3743 msi_dialog *dialog = cs->lpCreateParams;
3744 MSIRECORD *rec = NULL;
3745 LPWSTR title = NULL;
3746 RECT pos;
3748 TRACE("%p %p\n", dialog, dialog->package);
3750 dialog->hwnd = hwnd;
3751 SetWindowLongPtrW( hwnd, GWLP_USERDATA, (LONG_PTR) dialog );
3753 rec = msi_get_dialog_record( dialog );
3754 if( !rec )
3756 TRACE("No record found for dialog %s\n", debugstr_w(dialog->name));
3757 return -1;
3760 dialog->scale = msi_dialog_get_sans_serif_height(dialog->hwnd);
3762 msi_dialog_adjust_dialog_pos( dialog, rec, &pos );
3764 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
3766 dialog->default_font = msi_dup_property( dialog->package->db, df );
3767 if (!dialog->default_font)
3769 dialog->default_font = strdupW(dfv);
3770 if (!dialog->default_font)
3772 msiobj_release( &rec->hdr );
3773 return -1;
3777 title = msi_get_deformatted_field( dialog->package, rec, 7 );
3778 SetWindowTextW( hwnd, title );
3779 msi_free( title );
3781 SetWindowPos( hwnd, 0, pos.left, pos.top,
3782 pos.right - pos.left, pos.bottom - pos.top,
3783 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW );
3785 msi_dialog_build_font_list( dialog );
3786 msi_dialog_fill_controls( dialog );
3787 msi_dialog_evaluate_control_conditions( dialog );
3788 msi_dialog_set_tab_order( dialog, MSI_RecordGetString( rec, 8 ) );
3789 msiobj_release( &rec->hdr );
3791 return 0;
3794 static LRESULT msi_dialog_oncommand( msi_dialog *dialog, WPARAM param, HWND hwnd )
3796 msi_control *control = NULL;
3798 TRACE("%p %p %08lx\n", dialog, hwnd, param);
3800 switch (param)
3802 case 1: /* enter */
3803 control = msi_dialog_find_control( dialog, dialog->control_default );
3804 break;
3805 case 2: /* escape */
3806 control = msi_dialog_find_control( dialog, dialog->control_cancel );
3807 break;
3808 default:
3809 control = msi_dialog_find_control_by_hwnd( dialog, hwnd );
3812 if( control )
3814 if( control->handler )
3816 control->handler( dialog, control, param );
3817 msi_dialog_evaluate_control_conditions( dialog );
3821 return 0;
3824 static LRESULT msi_dialog_onnotify( msi_dialog *dialog, LPARAM param )
3826 LPNMHDR nmhdr = (LPNMHDR) param;
3827 msi_control *control = msi_dialog_find_control_by_hwnd( dialog, nmhdr->hwndFrom );
3829 TRACE("%p %p\n", dialog, nmhdr->hwndFrom);
3831 if ( control && control->handler )
3832 control->handler( dialog, control, param );
3834 return 0;
3837 static void dialog_setfocus( msi_dialog *dialog )
3839 HWND hwnd = dialog->hWndFocus;
3841 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, TRUE);
3842 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, FALSE);
3843 SetFocus( hwnd );
3844 dialog->hWndFocus = hwnd;
3847 static LRESULT WINAPI MSIDialog_WndProc( HWND hwnd, UINT msg,
3848 WPARAM wParam, LPARAM lParam )
3850 msi_dialog *dialog = (LPVOID) GetWindowLongPtrW( hwnd, GWLP_USERDATA );
3852 TRACE("0x%04x\n", msg);
3854 switch (msg)
3856 case WM_MOVE:
3857 dialog->package->center_x = LOWORD(lParam) + dialog->size.cx / 2.0;
3858 dialog->package->center_y = HIWORD(lParam) + dialog->size.cy / 2.0;
3859 break;
3861 case WM_CREATE:
3862 return msi_dialog_oncreate( hwnd, (LPCREATESTRUCTW)lParam );
3864 case WM_COMMAND:
3865 return msi_dialog_oncommand( dialog, wParam, (HWND)lParam );
3867 case WM_CLOSE:
3868 /* Simulate escape press */
3869 return msi_dialog_oncommand(dialog, 2, NULL);
3871 case WM_ACTIVATE:
3872 if( LOWORD(wParam) == WA_INACTIVE )
3873 dialog->hWndFocus = GetFocus();
3874 else
3875 dialog_setfocus( dialog );
3876 return 0;
3878 case WM_SETFOCUS:
3879 dialog_setfocus( dialog );
3880 return 0;
3882 /* bounce back to our subclassed static control */
3883 case WM_CTLCOLORSTATIC:
3884 return SendMessageW( (HWND) lParam, WM_CTLCOLORSTATIC, wParam, lParam );
3886 case WM_DESTROY:
3887 dialog->hwnd = NULL;
3888 return 0;
3889 case WM_NOTIFY:
3890 return msi_dialog_onnotify( dialog, lParam );
3892 return DefWindowProcW(hwnd, msg, wParam, lParam);
3895 static void process_pending_messages( HWND hdlg )
3897 MSG msg;
3899 while (PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
3901 if (hdlg && IsDialogMessageW( hdlg, &msg )) continue;
3902 TranslateMessage( &msg );
3903 DispatchMessageW( &msg );
3907 static UINT dialog_run_message_loop( msi_dialog *dialog )
3909 DWORD style;
3910 HWND hwnd;
3912 if( uiThreadId != GetCurrentThreadId() )
3913 return SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_CREATE, 0, (LPARAM) dialog );
3915 /* create the dialog window, don't show it yet */
3916 style = WS_OVERLAPPED | WS_SYSMENU;
3917 if( dialog->attributes & msidbDialogAttributesVisible )
3918 style |= WS_VISIBLE;
3920 if (dialog->parent == NULL && (dialog->attributes & msidbDialogAttributesMinimize))
3921 style |= WS_MINIMIZEBOX;
3923 hwnd = CreateWindowW( szMsiDialogClass, dialog->name, style,
3924 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
3925 NULL, NULL, NULL, dialog );
3926 if( !hwnd )
3928 ERR("Failed to create dialog %s\n", debugstr_w( dialog->name ));
3929 return ERROR_FUNCTION_FAILED;
3932 ShowWindow( hwnd, SW_SHOW );
3933 /* UpdateWindow( hwnd ); - and causes the transparent static controls not to paint */
3935 if( dialog->attributes & msidbDialogAttributesModal )
3937 while( !dialog->finished )
3939 MsgWaitForMultipleObjects( 0, NULL, 0, INFINITE, QS_ALLINPUT );
3940 process_pending_messages( dialog->hwnd );
3943 else
3944 return ERROR_IO_PENDING;
3946 return ERROR_SUCCESS;
3949 static LRESULT WINAPI MSIHiddenWindowProc( HWND hwnd, UINT msg,
3950 WPARAM wParam, LPARAM lParam )
3952 msi_dialog *dialog = (msi_dialog*) lParam;
3954 TRACE("%d %p\n", msg, dialog);
3956 switch (msg)
3958 case WM_MSI_DIALOG_CREATE:
3959 return dialog_run_message_loop( dialog );
3960 case WM_MSI_DIALOG_DESTROY:
3961 msi_dialog_destroy( dialog );
3962 return 0;
3964 return DefWindowProcW( hwnd, msg, wParam, lParam );
3967 static BOOL dialog_register_class( void )
3969 WNDCLASSW cls;
3971 ZeroMemory( &cls, sizeof cls );
3972 cls.lpfnWndProc = MSIDialog_WndProc;
3973 cls.hInstance = NULL;
3974 cls.hIcon = LoadIconW(0, (LPWSTR)IDI_APPLICATION);
3975 cls.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
3976 cls.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
3977 cls.lpszMenuName = NULL;
3978 cls.lpszClassName = szMsiDialogClass;
3980 if( !RegisterClassW( &cls ) )
3981 return FALSE;
3983 cls.lpfnWndProc = MSIHiddenWindowProc;
3984 cls.lpszClassName = szMsiHiddenWindow;
3986 if( !RegisterClassW( &cls ) )
3987 return FALSE;
3989 uiThreadId = GetCurrentThreadId();
3991 hMsiHiddenWindow = CreateWindowW( szMsiHiddenWindow, NULL, WS_OVERLAPPED,
3992 0, 0, 100, 100, NULL, NULL, NULL, NULL );
3993 if( !hMsiHiddenWindow )
3994 return FALSE;
3996 return TRUE;
3999 static msi_dialog *dialog_create( MSIPACKAGE *package, const WCHAR *name, msi_dialog *parent,
4000 control_event_handler event_handler )
4002 static const WCHAR szDialogCreated[] =
4003 {'D','i','a','l','o','g',' ','c','r','e','a','t','e','d',0};
4004 MSIRECORD *rec = NULL;
4005 msi_dialog *dialog;
4007 TRACE("%s\n", debugstr_w(name));
4009 if (!hMsiHiddenWindow) dialog_register_class();
4011 /* allocate the structure for the dialog to use */
4012 dialog = msi_alloc_zero( FIELD_OFFSET( msi_dialog, name[lstrlenW( name ) + 1] ));
4013 if( !dialog )
4014 return NULL;
4015 lstrcpyW( dialog->name, name );
4016 dialog->parent = parent;
4017 dialog->package = package;
4018 dialog->event_handler = event_handler;
4019 dialog->finished = 0;
4020 list_init( &dialog->controls );
4021 list_init( &dialog->fonts );
4023 /* verify that the dialog exists */
4024 rec = msi_get_dialog_record( dialog );
4025 if( !rec )
4027 msi_free( dialog );
4028 return NULL;
4030 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
4031 dialog->control_default = strdupW( MSI_RecordGetString( rec, 9 ) );
4032 dialog->control_cancel = strdupW( MSI_RecordGetString( rec, 10 ) );
4033 msiobj_release( &rec->hdr );
4035 rec = MSI_CreateRecord(2);
4036 if (!rec)
4038 msi_dialog_destroy(dialog);
4039 return NULL;
4041 MSI_RecordSetStringW(rec, 1, name);
4042 MSI_RecordSetStringW(rec, 2, szDialogCreated);
4043 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONSTART, rec);
4044 msiobj_release(&rec->hdr);
4046 return dialog;
4049 static void msi_dialog_end_dialog( msi_dialog *dialog )
4051 TRACE("%p\n", dialog);
4052 dialog->finished = 1;
4053 PostMessageW(dialog->hwnd, WM_NULL, 0, 0);
4056 void msi_dialog_check_messages( HANDLE handle )
4058 DWORD r;
4060 /* in threads other than the UI thread, block */
4061 if( uiThreadId != GetCurrentThreadId() )
4063 if (!handle) return;
4064 while (MsgWaitForMultipleObjectsEx( 1, &handle, INFINITE, QS_ALLINPUT, 0 ) == WAIT_OBJECT_0 + 1)
4066 MSG msg;
4067 while (PeekMessageW( &msg, NULL, 0, 0, PM_REMOVE ))
4069 TranslateMessage( &msg );
4070 DispatchMessageW( &msg );
4073 return;
4076 /* there are two choices for the UI thread */
4077 while (1)
4079 process_pending_messages( NULL );
4081 if( !handle )
4082 break;
4085 * block here until somebody creates a new dialog or
4086 * the handle we're waiting on becomes ready
4088 r = MsgWaitForMultipleObjects( 1, &handle, 0, INFINITE, QS_ALLINPUT );
4089 if( r == WAIT_OBJECT_0 )
4090 break;
4094 static void dialog_do_preview( msi_dialog *dialog )
4096 TRACE("\n");
4097 dialog->attributes |= msidbDialogAttributesVisible;
4098 dialog->attributes &= ~msidbDialogAttributesModal;
4099 dialog_run_message_loop( dialog );
4102 static void free_subscriber( struct subscriber *sub )
4104 msi_free( sub->event );
4105 msi_free( sub->control );
4106 msi_free( sub->attribute );
4107 msi_free( sub );
4110 static void event_cleanup_subscriptions( MSIPACKAGE *package, const WCHAR *dialog )
4112 struct list *item, *next;
4114 LIST_FOR_EACH_SAFE( item, next, &package->subscriptions )
4116 struct subscriber *sub = LIST_ENTRY( item, struct subscriber, entry );
4118 if (wcscmp( sub->dialog->name, dialog )) continue;
4119 list_remove( &sub->entry );
4120 free_subscriber( sub );
4124 void msi_dialog_destroy( msi_dialog *dialog )
4126 msi_font *font, *next;
4128 if( uiThreadId != GetCurrentThreadId() )
4130 SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_DESTROY, 0, (LPARAM) dialog );
4131 return;
4134 if( dialog->hwnd )
4136 ShowWindow( dialog->hwnd, SW_HIDE );
4137 DestroyWindow( dialog->hwnd );
4140 /* unsubscribe events */
4141 event_cleanup_subscriptions( dialog->package, dialog->name );
4143 /* destroy the list of controls */
4144 while( !list_empty( &dialog->controls ) )
4146 msi_control *t;
4148 t = LIST_ENTRY( list_head( &dialog->controls ),
4149 msi_control, entry );
4150 msi_destroy_control( t );
4153 /* destroy the list of fonts */
4154 LIST_FOR_EACH_ENTRY_SAFE( font, next, &dialog->fonts, msi_font, entry )
4156 list_remove( &font->entry );
4157 DeleteObject( font->hfont );
4158 msi_free( font );
4160 msi_free( dialog->default_font );
4162 msi_free( dialog->control_default );
4163 msi_free( dialog->control_cancel );
4164 dialog->package = NULL;
4165 msi_free( dialog );
4168 void msi_dialog_unregister_class( void )
4170 DestroyWindow( hMsiHiddenWindow );
4171 hMsiHiddenWindow = NULL;
4172 UnregisterClassW( szMsiDialogClass, NULL );
4173 UnregisterClassW( szMsiHiddenWindow, NULL );
4174 uiThreadId = 0;
4177 void msi_event_cleanup_all_subscriptions( MSIPACKAGE *package )
4179 struct list *item, *next;
4181 LIST_FOR_EACH_SAFE( item, next, &package->subscriptions )
4183 struct subscriber *sub = LIST_ENTRY( item, struct subscriber, entry );
4184 list_remove( &sub->entry );
4185 free_subscriber( sub );
4189 static void MSI_ClosePreview( MSIOBJECTHDR *arg )
4191 MSIPREVIEW *preview = (MSIPREVIEW *)arg;
4192 msiobj_release( &preview->package->hdr );
4195 static MSIPREVIEW *MSI_EnableUIPreview( MSIDATABASE *db )
4197 MSIPREVIEW *preview = NULL;
4198 MSIPACKAGE *package;
4200 package = MSI_CreatePackage( db );
4201 if (package)
4203 preview = alloc_msiobject( MSIHANDLETYPE_PREVIEW, sizeof(MSIPREVIEW), MSI_ClosePreview );
4204 if (preview)
4206 preview->package = package;
4207 msiobj_addref( &package->hdr );
4209 msiobj_release( &package->hdr );
4211 return preview;
4214 UINT WINAPI MsiEnableUIPreview( MSIHANDLE hdb, MSIHANDLE *phPreview )
4216 MSIDATABASE *db;
4217 MSIPREVIEW *preview;
4218 UINT r = ERROR_FUNCTION_FAILED;
4220 TRACE("%d %p\n", hdb, phPreview);
4222 if (!(db = msihandle2msiinfo(hdb, MSIHANDLETYPE_DATABASE)))
4223 return ERROR_INVALID_HANDLE;
4225 preview = MSI_EnableUIPreview( db );
4226 if (preview)
4228 *phPreview = alloc_msihandle( &preview->hdr );
4229 msiobj_release( &preview->hdr );
4230 r = ERROR_SUCCESS;
4231 if (!*phPreview)
4232 r = ERROR_NOT_ENOUGH_MEMORY;
4234 msiobj_release( &db->hdr );
4235 return r;
4238 static UINT preview_event_handler( msi_dialog *dialog, const WCHAR *event, const WCHAR *argument )
4240 MESSAGE("Preview dialog event '%s' (arg='%s')\n", debugstr_w(event), debugstr_w(argument));
4241 return ERROR_SUCCESS;
4244 static UINT MSI_PreviewDialogW( MSIPREVIEW *preview, LPCWSTR szDialogName )
4246 msi_dialog *dialog = NULL;
4247 UINT r = ERROR_SUCCESS;
4249 if (preview->dialog)
4250 msi_dialog_destroy( preview->dialog );
4252 /* an empty name means we should just destroy the current preview dialog */
4253 if (szDialogName)
4255 dialog = dialog_create( preview->package, szDialogName, NULL, preview_event_handler );
4256 if (dialog)
4257 dialog_do_preview( dialog );
4258 else
4259 r = ERROR_FUNCTION_FAILED;
4261 preview->dialog = dialog;
4262 return r;
4265 UINT WINAPI MsiPreviewDialogW( MSIHANDLE hPreview, LPCWSTR szDialogName )
4267 MSIPREVIEW *preview;
4268 UINT r;
4270 TRACE("%d %s\n", hPreview, debugstr_w(szDialogName));
4272 preview = msihandle2msiinfo( hPreview, MSIHANDLETYPE_PREVIEW );
4273 if (!preview)
4274 return ERROR_INVALID_HANDLE;
4276 r = MSI_PreviewDialogW( preview, szDialogName );
4277 msiobj_release( &preview->hdr );
4278 return r;
4281 UINT WINAPI MsiPreviewDialogA( MSIHANDLE hPreview, LPCSTR szDialogName )
4283 UINT r;
4284 LPWSTR strW = NULL;
4286 TRACE("%d %s\n", hPreview, debugstr_a(szDialogName));
4288 if (szDialogName)
4290 strW = strdupAtoW( szDialogName );
4291 if (!strW)
4292 return ERROR_OUTOFMEMORY;
4294 r = MsiPreviewDialogW( hPreview, strW );
4295 msi_free( strW );
4296 return r;
4299 UINT WINAPI MsiPreviewBillboardW( MSIHANDLE hPreview, LPCWSTR szControlName, LPCWSTR szBillboard )
4301 FIXME("%d %s %s\n", hPreview, debugstr_w(szControlName), debugstr_w(szBillboard));
4302 return ERROR_CALL_NOT_IMPLEMENTED;
4305 UINT WINAPI MsiPreviewBillboardA( MSIHANDLE hPreview, LPCSTR szControlName, LPCSTR szBillboard )
4307 FIXME("%d %s %s\n", hPreview, debugstr_a(szControlName), debugstr_a(szBillboard));
4308 return ERROR_CALL_NOT_IMPLEMENTED;
4311 struct control_event
4313 const WCHAR *event;
4314 event_handler handler;
4317 static UINT dialog_event_handler( msi_dialog *, const WCHAR *, const WCHAR * );
4319 /* create a dialog box and run it if it's modal */
4320 static INT event_do_dialog( MSIPACKAGE *package, const WCHAR *name, msi_dialog *parent, BOOL destroy_modeless )
4322 msi_dialog *dialog;
4323 UINT r;
4324 INT retval;
4326 /* create a new dialog */
4327 dialog = dialog_create( package, name, parent, dialog_event_handler );
4328 if (dialog)
4330 /* kill the current modeless dialog */
4331 if (destroy_modeless && package->dialog)
4333 msi_dialog_destroy( package->dialog );
4334 package->dialog = NULL;
4337 /* modeless dialogs return an error message */
4338 r = dialog_run_message_loop( dialog );
4339 if (r == ERROR_SUCCESS)
4341 retval = dialog->retval;
4342 msi_dialog_destroy( dialog );
4343 return retval;
4345 else
4347 package->dialog = dialog;
4348 return IDOK;
4351 else return 0;
4354 /* end a modal dialog box */
4355 static UINT event_end_dialog( msi_dialog *dialog, const WCHAR *argument )
4357 static const WCHAR exitW[] = {'E','x','i','t',0};
4358 static const WCHAR retryW[] = {'R','e','t','r','y',0};
4359 static const WCHAR ignoreW[] = {'I','g','n','o','r','e',0};
4360 static const WCHAR returnW[] = {'R','e','t','u','r','n',0};
4362 if (!wcscmp( argument, exitW ))
4363 dialog->retval = IDCANCEL;
4364 else if (!wcscmp( argument, retryW ))
4365 dialog->retval = IDRETRY;
4366 else if (!wcscmp( argument, ignoreW ))
4367 dialog->retval = IDOK;
4368 else if (!wcscmp( argument, returnW ))
4369 dialog->retval = 0;
4370 else
4372 ERR("Unknown argument string %s\n", debugstr_w(argument));
4373 dialog->retval = IDABORT;
4375 event_cleanup_subscriptions( dialog->package, dialog->name );
4376 msi_dialog_end_dialog( dialog );
4377 return ERROR_SUCCESS;
4380 static UINT pending_event_end_dialog( msi_dialog *dialog, const WCHAR *argument )
4382 dialog->pending_event = event_end_dialog;
4383 msi_free( dialog->pending_argument );
4384 dialog->pending_argument = strdupW( argument );
4385 return ERROR_SUCCESS;
4388 /* transition from one modal dialog to another modal dialog */
4389 static UINT event_new_dialog( msi_dialog *dialog, const WCHAR *argument )
4391 /* store the name of the next dialog, and signal this one to end */
4392 dialog->package->next_dialog = strdupW( argument );
4393 msi_event_cleanup_all_subscriptions( dialog->package );
4394 msi_dialog_end_dialog( dialog );
4395 return ERROR_SUCCESS;
4398 static UINT pending_event_new_dialog( msi_dialog *dialog, const WCHAR *argument )
4400 dialog->pending_event = event_new_dialog;
4401 msi_free( dialog->pending_argument );
4402 dialog->pending_argument = strdupW( argument );
4403 return ERROR_SUCCESS;
4406 /* create a new child dialog of an existing modal dialog */
4407 static UINT event_spawn_dialog( msi_dialog *dialog, const WCHAR *argument )
4409 INT r;
4410 /* don't destroy a modeless dialogs that might be our parent */
4411 r = event_do_dialog( dialog->package, argument, dialog, FALSE );
4412 if (r != 0)
4414 dialog->retval = r;
4415 msi_dialog_end_dialog( dialog );
4417 else
4418 msi_dialog_update_all_controls(dialog);
4420 return ERROR_SUCCESS;
4423 static UINT pending_event_spawn_dialog( msi_dialog *dialog, const WCHAR *argument )
4425 dialog->pending_event = event_spawn_dialog;
4426 msi_free( dialog->pending_argument );
4427 dialog->pending_argument = strdupW( argument );
4428 return ERROR_SUCCESS;
4431 /* creates a dialog that remains up for a period of time based on a condition */
4432 static UINT event_spawn_wait_dialog( msi_dialog *dialog, const WCHAR *argument )
4434 FIXME("doing nothing\n");
4435 return ERROR_SUCCESS;
4438 static UINT event_do_action( msi_dialog *dialog, const WCHAR *argument )
4440 ACTION_PerformAction(dialog->package, argument);
4441 return ERROR_SUCCESS;
4444 static UINT event_add_local( msi_dialog *dialog, const WCHAR *argument )
4446 MSIFEATURE *feature;
4448 LIST_FOR_EACH_ENTRY( feature, &dialog->package->features, MSIFEATURE, entry )
4450 if (!wcscmp( argument, feature->Feature ) || !wcscmp( argument, szAll ))
4452 if (feature->ActionRequest != INSTALLSTATE_LOCAL)
4453 msi_set_property( dialog->package->db, szPreselected, szOne, -1 );
4454 MSI_SetFeatureStateW( dialog->package, feature->Feature, INSTALLSTATE_LOCAL );
4457 return ERROR_SUCCESS;
4460 static UINT event_remove( msi_dialog *dialog, const WCHAR *argument )
4462 MSIFEATURE *feature;
4464 LIST_FOR_EACH_ENTRY( feature, &dialog->package->features, MSIFEATURE, entry )
4466 if (!wcscmp( argument, feature->Feature ) || !wcscmp( argument, szAll ))
4468 if (feature->ActionRequest != INSTALLSTATE_ABSENT)
4469 msi_set_property( dialog->package->db, szPreselected, szOne, -1 );
4470 MSI_SetFeatureStateW( dialog->package, feature->Feature, INSTALLSTATE_ABSENT );
4473 return ERROR_SUCCESS;
4476 static UINT event_add_source( msi_dialog *dialog, const WCHAR *argument )
4478 MSIFEATURE *feature;
4480 LIST_FOR_EACH_ENTRY( feature, &dialog->package->features, MSIFEATURE, entry )
4482 if (!wcscmp( argument, feature->Feature ) || !wcscmp( argument, szAll ))
4484 if (feature->ActionRequest != INSTALLSTATE_SOURCE)
4485 msi_set_property( dialog->package->db, szPreselected, szOne, -1 );
4486 MSI_SetFeatureStateW( dialog->package, feature->Feature, INSTALLSTATE_SOURCE );
4489 return ERROR_SUCCESS;
4492 void msi_event_fire( MSIPACKAGE *package, const WCHAR *event, MSIRECORD *rec )
4494 struct subscriber *sub;
4496 TRACE("firing event %s\n", debugstr_w(event));
4498 LIST_FOR_EACH_ENTRY( sub, &package->subscriptions, struct subscriber, entry )
4500 if (wcsicmp( sub->event, event )) continue;
4501 dialog_handle_event( sub->dialog, sub->control, sub->attribute, rec );
4505 static UINT event_set_target_path( msi_dialog *dialog, const WCHAR *argument )
4507 WCHAR *path = msi_dup_property( dialog->package->db, argument );
4508 MSIRECORD *rec = MSI_CreateRecord( 1 );
4509 UINT r = ERROR_SUCCESS;
4511 MSI_RecordSetStringW( rec, 1, path );
4512 msi_event_fire( dialog->package, szSelectionPath, rec );
4513 if (path)
4515 /* failure to set the path halts the executing of control events */
4516 r = MSI_SetTargetPathW( dialog->package, argument, path );
4517 msi_free( path );
4519 msi_free( &rec->hdr );
4520 return r;
4523 static UINT event_reset( msi_dialog *dialog, const WCHAR *argument )
4525 msi_dialog_reset( dialog );
4526 return ERROR_SUCCESS;
4529 INT ACTION_ShowDialog( MSIPACKAGE *package, const WCHAR *dialog )
4531 static const WCHAR szDialog[] = {'D','i','a','l','o','g',0};
4532 MSIRECORD *row;
4533 INT rc;
4535 if (!TABLE_Exists(package->db, szDialog)) return 0;
4537 row = MSI_CreateRecord(0);
4538 if (!row) return -1;
4539 MSI_RecordSetStringW(row, 0, dialog);
4540 rc = MSI_ProcessMessage(package, INSTALLMESSAGE_SHOWDIALOG, row);
4541 msiobj_release(&row->hdr);
4543 if (rc == -2) rc = 0;
4545 if (!rc)
4547 MSIRECORD *row = MSI_CreateRecord(2);
4548 if (!row) return -1;
4549 MSI_RecordSetInteger(row, 1, 2726);
4550 MSI_RecordSetStringW(row, 2, dialog);
4551 MSI_ProcessMessage(package, INSTALLMESSAGE_INFO, row);
4553 msiobj_release(&row->hdr);
4555 return rc;
4558 INT ACTION_DialogBox( MSIPACKAGE *package, const WCHAR *dialog )
4560 INT r;
4562 if (package->next_dialog) ERR("Already got next dialog... ignoring it\n");
4563 package->next_dialog = NULL;
4565 /* Dialogs are chained through NewDialog, which sets the next_dialog member.
4566 * We fall out of the loop if we reach a modeless dialog, which immediately
4567 * returns IDOK, or an EndDialog event, which returns the value corresponding
4568 * to its argument.
4570 r = event_do_dialog( package, dialog, NULL, TRUE );
4571 while (package->next_dialog)
4573 WCHAR *name = package->next_dialog;
4575 package->next_dialog = NULL;
4576 r = event_do_dialog( package, name, NULL, TRUE );
4577 msi_free( name );
4579 return r;
4582 static UINT event_set_install_level( msi_dialog *dialog, const WCHAR *argument )
4584 int level = wcstol( argument, NULL, 10 );
4586 TRACE("setting install level to %d\n", level);
4587 return MSI_SetInstallLevel( dialog->package, level );
4590 static UINT event_directory_list_up( msi_dialog *dialog, const WCHAR *argument )
4592 return msi_dialog_directorylist_up( dialog );
4595 static UINT event_directory_list_new( msi_dialog *dialog, const WCHAR *argument )
4597 return msi_dialog_directorylist_new( dialog );
4600 static UINT event_reinstall_mode( msi_dialog *dialog, const WCHAR *argument )
4602 return msi_set_property( dialog->package->db, szReinstallMode, argument, -1 );
4605 static UINT event_reinstall( msi_dialog *dialog, const WCHAR *argument )
4607 return msi_set_property( dialog->package->db, szReinstall, argument, -1 );
4610 static UINT event_validate_product_id( msi_dialog *dialog, const WCHAR *argument )
4612 return msi_validate_product_id( dialog->package );
4615 static const WCHAR end_dialogW[] = {'E','n','d','D','i','a','l','o','g',0};
4616 static const WCHAR new_dialogW[] = {'N','e','w','D','i','a','l','o','g',0};
4617 static const WCHAR spawn_dialogW[] = {'S','p','a','w','n','D','i','a','l','o','g',0};
4618 static const WCHAR spawn_wait_dialogW[] = {'S','p','a','w','n','W','a','i','t','D','i','a','l','o','g',0};
4619 static const WCHAR do_actionW[] = {'D','o','A','c','t','i','o','n',0};
4620 static const WCHAR add_localW[] = {'A','d','d','L','o','c','a','l',0};
4621 static const WCHAR removeW[] = {'R','e','m','o','v','e',0};
4622 static const WCHAR add_sourceW[] = {'A','d','d','S','o','u','r','c','e',0};
4623 static const WCHAR set_target_pathW[] = {'S','e','t','T','a','r','g','e','t','P','a','t','h',0};
4624 static const WCHAR resetW[] = {'R','e','s','e','t',0};
4625 static const WCHAR set_install_levelW[] = {'S','e','t','I','n','s','t','a','l','l','L','e','v','e','l',0};
4626 static const WCHAR directory_list_upW[] = {'D','i','r','e','c','t','o','r','y','L','i','s','t','U','p',0};
4627 static const WCHAR directory_list_newW[] = {'D','i','r','e','c','t','o','r','y','L','i','s','t','N','e','w',0};
4628 static const WCHAR selection_browseW[] = {'S','e','l','e','c','t','i','o','n','B','r','o','w','s','e',0};
4629 static const WCHAR reinstall_modeW[] = {'R','e','i','n','s','t','a','l','l','M','o','d','e',0};
4630 static const WCHAR reinstallW[] = {'R','e','i','n','s','t','a','l','l',0};
4631 static const WCHAR validate_product_idW[] = {'V','a','l','i','d','a','t','e','P','r','o','d','u','c','t','I','D',0};
4633 static const struct control_event control_events[] =
4635 { end_dialogW, pending_event_end_dialog },
4636 { new_dialogW, pending_event_new_dialog },
4637 { spawn_dialogW, pending_event_spawn_dialog },
4638 { spawn_wait_dialogW, event_spawn_wait_dialog },
4639 { do_actionW, event_do_action },
4640 { add_localW, event_add_local },
4641 { removeW, event_remove },
4642 { add_sourceW, event_add_source },
4643 { set_target_pathW, event_set_target_path },
4644 { resetW, event_reset },
4645 { set_install_levelW, event_set_install_level },
4646 { directory_list_upW, event_directory_list_up },
4647 { directory_list_newW, event_directory_list_new },
4648 { selection_browseW, event_spawn_dialog },
4649 { reinstall_modeW, event_reinstall_mode },
4650 { reinstallW, event_reinstall },
4651 { validate_product_idW, event_validate_product_id },
4652 { NULL, NULL }
4655 static UINT dialog_event_handler( msi_dialog *dialog, const WCHAR *event, const WCHAR *argument )
4657 unsigned int i;
4659 TRACE("handling event %s\n", debugstr_w(event));
4661 if (!event) return ERROR_SUCCESS;
4663 for (i = 0; control_events[i].event; i++)
4665 if (!wcscmp( control_events[i].event, event ))
4666 return control_events[i].handler( dialog, argument );
4668 FIXME("unhandled event %s arg(%s)\n", debugstr_w(event), debugstr_w(argument));
4669 return ERROR_SUCCESS;