Release 1.7.39.
[wine.git] / dlls / msi / dialog.c
blob6baf08ddd5e069ebab6c198cb15db062bbb3f71e
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2005 Mike McCormack for CodeWeavers
5 * Copyright 2005 Aric Stewart for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define COBJMACROS
23 #define NONAMELESSUNION
25 #include <stdarg.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "winnls.h"
32 #include "msi.h"
33 #include "msipriv.h"
34 #include "msidefs.h"
35 #include "ocidl.h"
36 #include "olectl.h"
37 #include "richedit.h"
38 #include "commctrl.h"
39 #include "winreg.h"
40 #include "shlwapi.h"
41 #include "msiserver.h"
42 #include "shellapi.h"
44 #include "wine/debug.h"
45 #include "wine/unicode.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(msi);
49 extern HINSTANCE msi_hInstance;
51 struct msi_control_tag;
52 typedef struct msi_control_tag msi_control;
53 typedef UINT (*msi_handler)( msi_dialog *, msi_control *, WPARAM );
54 typedef void (*msi_update)( msi_dialog *, msi_control * );
55 typedef UINT (*control_event_handler)( msi_dialog *, const WCHAR *, const WCHAR * );
57 struct msi_control_tag
59 struct list entry;
60 HWND hwnd;
61 msi_handler handler;
62 msi_update update;
63 LPWSTR property;
64 LPWSTR value;
65 HBITMAP hBitmap;
66 HICON hIcon;
67 LPWSTR tabnext;
68 LPWSTR type;
69 HMODULE hDll;
70 float progress_current;
71 float progress_max;
72 BOOL progress_backwards;
73 DWORD attributes;
74 WCHAR name[1];
77 typedef struct msi_font_tag
79 struct list entry;
80 HFONT hfont;
81 COLORREF color;
82 WCHAR name[1];
83 } msi_font;
85 struct msi_dialog_tag
87 MSIPACKAGE *package;
88 msi_dialog *parent;
89 control_event_handler event_handler;
90 BOOL finished;
91 INT scale;
92 DWORD attributes;
93 SIZE size;
94 HWND hwnd;
95 LPWSTR default_font;
96 struct list fonts;
97 struct list controls;
98 HWND hWndFocus;
99 LPWSTR control_default;
100 LPWSTR control_cancel;
101 WCHAR name[1];
104 struct subscriber
106 struct list entry;
107 msi_dialog *dialog;
108 WCHAR *event;
109 WCHAR *control;
110 WCHAR *attribute;
113 typedef UINT (*msi_dialog_control_func)( msi_dialog *dialog, MSIRECORD *rec );
114 struct control_handler
116 LPCWSTR control_type;
117 msi_dialog_control_func func;
120 typedef struct
122 msi_dialog* dialog;
123 msi_control *parent;
124 DWORD attributes;
125 LPWSTR propval;
126 } radio_button_group_descr;
128 static const WCHAR szMsiDialogClass[] = { 'M','s','i','D','i','a','l','o','g','C','l','o','s','e','C','l','a','s','s',0 };
129 static const WCHAR szMsiHiddenWindow[] = { 'M','s','i','H','i','d','d','e','n','W','i','n','d','o','w',0 };
130 static const WCHAR szStatic[] = { 'S','t','a','t','i','c',0 };
131 static const WCHAR szButton[] = { 'B','U','T','T','O','N', 0 };
132 static const WCHAR szButtonData[] = { 'M','S','I','D','A','T','A',0 };
133 static const WCHAR szProgress[] = { 'P','r','o','g','r','e','s','s',0 };
134 static const WCHAR szText[] = { 'T','e','x','t',0 };
135 static const WCHAR szPushButton[] = { 'P','u','s','h','B','u','t','t','o','n',0 };
136 static const WCHAR szLine[] = { 'L','i','n','e',0 };
137 static const WCHAR szBitmap[] = { 'B','i','t','m','a','p',0 };
138 static const WCHAR szCheckBox[] = { 'C','h','e','c','k','B','o','x',0 };
139 static const WCHAR szScrollableText[] = { 'S','c','r','o','l','l','a','b','l','e','T','e','x','t',0 };
140 static const WCHAR szComboBox[] = { 'C','o','m','b','o','B','o','x',0 };
141 static const WCHAR szEdit[] = { 'E','d','i','t',0 };
142 static const WCHAR szMaskedEdit[] = { 'M','a','s','k','e','d','E','d','i','t',0 };
143 static const WCHAR szPathEdit[] = { 'P','a','t','h','E','d','i','t',0 };
144 static const WCHAR szProgressBar[] = { 'P','r','o','g','r','e','s','s','B','a','r',0 };
145 static const WCHAR szSetProgress[] = { 'S','e','t','P','r','o','g','r','e','s','s',0 };
146 static const WCHAR szRadioButtonGroup[] = { 'R','a','d','i','o','B','u','t','t','o','n','G','r','o','u','p',0 };
147 static const WCHAR szIcon[] = { 'I','c','o','n',0 };
148 static const WCHAR szSelectionTree[] = { 'S','e','l','e','c','t','i','o','n','T','r','e','e',0 };
149 static const WCHAR szGroupBox[] = { 'G','r','o','u','p','B','o','x',0 };
150 static const WCHAR szListBox[] = { 'L','i','s','t','B','o','x',0 };
151 static const WCHAR szDirectoryCombo[] = { 'D','i','r','e','c','t','o','r','y','C','o','m','b','o',0 };
152 static const WCHAR szDirectoryList[] = { 'D','i','r','e','c','t','o','r','y','L','i','s','t',0 };
153 static const WCHAR szVolumeCostList[] = { 'V','o','l','u','m','e','C','o','s','t','L','i','s','t',0 };
154 static const WCHAR szVolumeSelectCombo[] = { 'V','o','l','u','m','e','S','e','l','e','c','t','C','o','m','b','o',0 };
155 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};
156 static const WCHAR szSelectionPath[] = {'S','e','l','e','c','t','i','o','n','P','a','t','h',0};
157 static const WCHAR szHyperLink[] = {'H','y','p','e','r','L','i','n','k',0};
159 /* dialog sequencing */
161 #define WM_MSI_DIALOG_CREATE (WM_USER+0x100)
162 #define WM_MSI_DIALOG_DESTROY (WM_USER+0x101)
164 #define USER_INSTALLSTATE_ALL 0x1000
166 static DWORD uiThreadId;
167 static HWND hMsiHiddenWindow;
169 static LPWSTR msi_get_window_text( HWND hwnd )
171 UINT sz, r;
172 LPWSTR buf;
174 sz = 0x20;
175 buf = msi_alloc( sz*sizeof(WCHAR) );
176 while ( buf )
178 r = GetWindowTextW( hwnd, buf, sz );
179 if ( r < (sz - 1) )
180 break;
181 sz *= 2;
182 buf = msi_realloc( buf, sz*sizeof(WCHAR) );
185 return buf;
188 static INT msi_dialog_scale_unit( msi_dialog *dialog, INT val )
190 return MulDiv( val, dialog->scale, 12 );
193 static msi_control *msi_dialog_find_control( msi_dialog *dialog, LPCWSTR name )
195 msi_control *control;
197 if( !name )
198 return NULL;
199 if( !dialog->hwnd )
200 return NULL;
201 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
202 if( !strcmpW( control->name, name ) ) /* FIXME: case sensitive? */
203 return control;
204 return NULL;
207 static msi_control *msi_dialog_find_control_by_type( msi_dialog *dialog, LPCWSTR type )
209 msi_control *control;
211 if( !type )
212 return NULL;
213 if( !dialog->hwnd )
214 return NULL;
215 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
216 if( !strcmpW( control->type, type ) ) /* FIXME: case sensitive? */
217 return control;
218 return NULL;
221 static msi_control *msi_dialog_find_control_by_hwnd( msi_dialog *dialog, HWND hwnd )
223 msi_control *control;
225 if( !dialog->hwnd )
226 return NULL;
227 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
228 if( hwnd == control->hwnd )
229 return control;
230 return NULL;
233 static LPWSTR msi_get_deformatted_field( MSIPACKAGE *package, MSIRECORD *rec, int field )
235 LPCWSTR str = MSI_RecordGetString( rec, field );
236 LPWSTR ret = NULL;
238 if (str)
239 deformat_string( package, str, &ret );
240 return ret;
243 static LPWSTR msi_dialog_dup_property( msi_dialog *dialog, LPCWSTR property, BOOL indirect )
245 LPWSTR prop = NULL;
247 if (!property)
248 return NULL;
250 if (indirect)
251 prop = msi_dup_property( dialog->package->db, property );
253 if (!prop)
254 prop = strdupW( property );
256 return prop;
260 * msi_dialog_get_style
262 * Extract the {\style} string from the front of the text to display and
263 * update the pointer. Only the last style in a list is applied.
265 static LPWSTR msi_dialog_get_style( LPCWSTR p, LPCWSTR *rest )
267 LPWSTR ret;
268 LPCWSTR q, i, first;
269 DWORD len;
271 q = NULL;
272 *rest = p;
273 if( !p )
274 return NULL;
276 while ((first = strchrW( p, '{' )) && (q = strchrW( first + 1, '}' )))
278 p = first + 1;
279 if( *p != '\\' && *p != '&' )
280 return NULL;
282 /* little bit of sanity checking to stop us getting confused with RTF */
283 for( i=++p; i<q; i++ )
284 if( *i == '}' || *i == '\\' )
285 return NULL;
288 if (!q)
289 return NULL;
291 *rest = ++q;
292 len = q - p;
294 ret = msi_alloc( len*sizeof(WCHAR) );
295 if( !ret )
296 return ret;
297 memcpy( ret, p, len*sizeof(WCHAR) );
298 ret[len-1] = 0;
299 return ret;
302 static UINT msi_dialog_add_font( MSIRECORD *rec, LPVOID param )
304 msi_dialog *dialog = param;
305 msi_font *font;
306 LPCWSTR face, name;
307 LOGFONTW lf;
308 INT style;
309 HDC hdc;
311 /* create a font and add it to the list */
312 name = MSI_RecordGetString( rec, 1 );
313 font = msi_alloc( FIELD_OFFSET( msi_font, name[strlenW( name ) + 1] ));
314 strcpyW( font->name, name );
315 list_add_head( &dialog->fonts, &font->entry );
317 font->color = MSI_RecordGetInteger( rec, 4 );
319 memset( &lf, 0, sizeof lf );
320 face = MSI_RecordGetString( rec, 2 );
321 lf.lfHeight = MSI_RecordGetInteger( rec, 3 );
322 style = MSI_RecordGetInteger( rec, 5 );
323 if( style & msidbTextStyleStyleBitsBold )
324 lf.lfWeight = FW_BOLD;
325 if( style & msidbTextStyleStyleBitsItalic )
326 lf.lfItalic = TRUE;
327 if( style & msidbTextStyleStyleBitsUnderline )
328 lf.lfUnderline = TRUE;
329 if( style & msidbTextStyleStyleBitsStrike )
330 lf.lfStrikeOut = TRUE;
331 lstrcpynW( lf.lfFaceName, face, LF_FACESIZE );
333 /* adjust the height */
334 hdc = GetDC( dialog->hwnd );
335 if (hdc)
337 lf.lfHeight = -MulDiv(lf.lfHeight, GetDeviceCaps(hdc, LOGPIXELSY), 72);
338 ReleaseDC( dialog->hwnd, hdc );
341 font->hfont = CreateFontIndirectW( &lf );
343 TRACE("Adding font style %s\n", debugstr_w(font->name) );
345 return ERROR_SUCCESS;
348 static msi_font *msi_dialog_find_font( msi_dialog *dialog, LPCWSTR name )
350 msi_font *font = NULL;
352 LIST_FOR_EACH_ENTRY( font, &dialog->fonts, msi_font, entry )
353 if( !strcmpW( font->name, name ) ) /* FIXME: case sensitive? */
354 break;
356 return font;
359 static UINT msi_dialog_set_font( msi_dialog *dialog, HWND hwnd, LPCWSTR name )
361 msi_font *font;
363 font = msi_dialog_find_font( dialog, name );
364 if( font )
365 SendMessageW( hwnd, WM_SETFONT, (WPARAM) font->hfont, TRUE );
366 else
367 ERR("No font entry for %s\n", debugstr_w(name));
368 return ERROR_SUCCESS;
371 static UINT msi_dialog_build_font_list( msi_dialog *dialog )
373 static const WCHAR query[] = {
374 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
375 '`','T','e','x','t','S','t','y','l','e','`',0};
376 MSIQUERY *view;
377 UINT r;
379 TRACE("dialog %p\n", dialog );
381 r = MSI_OpenQuery( dialog->package->db, &view, query );
382 if( r != ERROR_SUCCESS )
383 return r;
385 r = MSI_IterateRecords( view, NULL, msi_dialog_add_font, dialog );
386 msiobj_release( &view->hdr );
387 return r;
390 static void msi_destroy_control( msi_control *t )
392 list_remove( &t->entry );
393 /* leave dialog->hwnd - destroying parent destroys child windows */
394 msi_free( t->property );
395 msi_free( t->value );
396 if( t->hBitmap )
397 DeleteObject( t->hBitmap );
398 if( t->hIcon )
399 DestroyIcon( t->hIcon );
400 msi_free( t->tabnext );
401 msi_free( t->type );
402 if (t->hDll)
403 FreeLibrary( t->hDll );
404 msi_free( t );
407 static msi_control *dialog_create_window( msi_dialog *dialog, MSIRECORD *rec, DWORD exstyle,
408 const WCHAR *szCls, const WCHAR *name, const WCHAR *text,
409 DWORD style, HWND parent )
411 DWORD x, y, width, height;
412 LPWSTR font = NULL, title_font = NULL;
413 LPCWSTR title = NULL;
414 msi_control *control;
416 style |= WS_CHILD;
418 control = msi_alloc( FIELD_OFFSET( msi_control, name[strlenW( name ) + 1] ));
419 if (!control)
420 return NULL;
422 strcpyW( control->name, name );
423 list_add_tail( &dialog->controls, &control->entry );
424 control->handler = NULL;
425 control->update = NULL;
426 control->property = NULL;
427 control->value = NULL;
428 control->hBitmap = NULL;
429 control->hIcon = NULL;
430 control->hDll = NULL;
431 control->tabnext = strdupW( MSI_RecordGetString( rec, 11) );
432 control->type = strdupW( MSI_RecordGetString( rec, 3 ) );
433 control->progress_current = 0;
434 control->progress_max = 100;
435 control->progress_backwards = FALSE;
437 x = MSI_RecordGetInteger( rec, 4 );
438 y = MSI_RecordGetInteger( rec, 5 );
439 width = MSI_RecordGetInteger( rec, 6 );
440 height = MSI_RecordGetInteger( rec, 7 );
442 x = msi_dialog_scale_unit( dialog, x );
443 y = msi_dialog_scale_unit( dialog, y );
444 width = msi_dialog_scale_unit( dialog, width );
445 height = msi_dialog_scale_unit( dialog, height );
447 if( text )
449 deformat_string( dialog->package, text, &title_font );
450 font = msi_dialog_get_style( title_font, &title );
453 control->hwnd = CreateWindowExW( exstyle, szCls, title, style,
454 x, y, width, height, parent, NULL, NULL, NULL );
456 TRACE("Dialog %s control %s hwnd %p\n",
457 debugstr_w(dialog->name), debugstr_w(text), control->hwnd );
459 msi_dialog_set_font( dialog, control->hwnd,
460 font ? font : dialog->default_font );
462 msi_free( title_font );
463 msi_free( font );
465 return control;
468 static LPWSTR msi_dialog_get_uitext( msi_dialog *dialog, LPCWSTR key )
470 MSIRECORD *rec;
471 LPWSTR text;
473 static const WCHAR query[] = {
474 's','e','l','e','c','t',' ','*',' ',
475 'f','r','o','m',' ','`','U','I','T','e','x','t','`',' ',
476 'w','h','e','r','e',' ','`','K','e','y','`',' ','=',' ','\'','%','s','\'',0
479 rec = MSI_QueryGetRecord( dialog->package->db, query, key );
480 if (!rec) return NULL;
481 text = strdupW( MSI_RecordGetString( rec, 2 ) );
482 msiobj_release( &rec->hdr );
483 return text;
486 static MSIRECORD *msi_get_binary_record( MSIDATABASE *db, LPCWSTR name )
488 static const WCHAR query[] = {
489 's','e','l','e','c','t',' ','*',' ',
490 'f','r','o','m',' ','B','i','n','a','r','y',' ',
491 'w','h','e','r','e',' ',
492 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0
495 return MSI_QueryGetRecord( db, query, name );
498 static LPWSTR msi_create_tmp_path(void)
500 WCHAR tmp[MAX_PATH];
501 LPWSTR path = NULL;
502 DWORD len, r;
504 r = GetTempPathW( MAX_PATH, tmp );
505 if( !r )
506 return path;
507 len = lstrlenW( tmp ) + 20;
508 path = msi_alloc( len * sizeof (WCHAR) );
509 if( path )
511 r = GetTempFileNameW( tmp, szMsi, 0, path );
512 if (!r)
514 msi_free( path );
515 path = NULL;
518 return path;
521 static HANDLE msi_load_image( MSIDATABASE *db, LPCWSTR name, UINT type,
522 UINT cx, UINT cy, UINT flags )
524 MSIRECORD *rec = NULL;
525 HANDLE himage = NULL;
526 LPWSTR tmp;
527 UINT r;
529 TRACE("%p %s %u %u %08x\n", db, debugstr_w(name), cx, cy, flags);
531 tmp = msi_create_tmp_path();
532 if( !tmp )
533 return himage;
535 rec = msi_get_binary_record( db, name );
536 if( rec )
538 r = MSI_RecordStreamToFile( rec, 2, tmp );
539 if( r == ERROR_SUCCESS )
541 himage = LoadImageW( 0, tmp, type, cx, cy, flags );
543 msiobj_release( &rec->hdr );
545 DeleteFileW( tmp );
547 msi_free( tmp );
548 return himage;
551 static HICON msi_load_icon( MSIDATABASE *db, LPCWSTR text, UINT attributes )
553 DWORD cx = 0, cy = 0, flags;
555 flags = LR_LOADFROMFILE | LR_DEFAULTSIZE;
556 if( attributes & msidbControlAttributesFixedSize )
558 flags &= ~LR_DEFAULTSIZE;
559 if( attributes & msidbControlAttributesIconSize16 )
561 cx += 16;
562 cy += 16;
564 if( attributes & msidbControlAttributesIconSize32 )
566 cx += 32;
567 cy += 32;
569 /* msidbControlAttributesIconSize48 handled by above logic */
571 return msi_load_image( db, text, IMAGE_ICON, cx, cy, flags );
574 static void msi_dialog_update_controls( msi_dialog *dialog, LPCWSTR property )
576 msi_control *control;
578 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
580 if ( control->property && !strcmpW( control->property, property ) && control->update )
581 control->update( dialog, control );
585 static void msi_dialog_set_property( MSIPACKAGE *package, LPCWSTR property, LPCWSTR value )
587 UINT r = msi_set_property( package->db, property, value, -1 );
588 if (r == ERROR_SUCCESS && !strcmpW( property, szSourceDir ))
589 msi_reset_folders( package, TRUE );
592 static MSIFEATURE *msi_seltree_feature_from_item( HWND hwnd, HTREEITEM hItem )
594 TVITEMW tvi;
596 /* get the feature from the item */
597 memset( &tvi, 0, sizeof tvi );
598 tvi.hItem = hItem;
599 tvi.mask = TVIF_PARAM | TVIF_HANDLE;
600 SendMessageW( hwnd, TVM_GETITEMW, 0, (LPARAM)&tvi );
601 return (MSIFEATURE *)tvi.lParam;
604 struct msi_selection_tree_info
606 msi_dialog *dialog;
607 HWND hwnd;
608 WNDPROC oldproc;
609 HTREEITEM selected;
612 static MSIFEATURE *msi_seltree_get_selected_feature( msi_control *control )
614 struct msi_selection_tree_info *info = GetPropW( control->hwnd, szButtonData );
615 return msi_seltree_feature_from_item( control->hwnd, info->selected );
618 static void dialog_handle_event( msi_dialog *dialog, const WCHAR *control,
619 const WCHAR *attribute, MSIRECORD *rec )
621 msi_control* ctrl;
623 ctrl = msi_dialog_find_control( dialog, control );
624 if (!ctrl)
625 return;
626 if( !strcmpW( attribute, szText ) )
628 const WCHAR *font_text, *text = NULL;
629 WCHAR *font, *text_fmt = NULL;
631 font_text = MSI_RecordGetString( rec , 1 );
632 font = msi_dialog_get_style( font_text, &text );
633 deformat_string( dialog->package, text, &text_fmt );
634 if (text_fmt) text = text_fmt;
635 else text = szEmpty;
637 SetWindowTextW( ctrl->hwnd, text );
639 msi_free( font );
640 msi_free( text_fmt );
641 msi_dialog_check_messages( NULL );
643 else if( !strcmpW( attribute, szProgress ) )
645 DWORD func, val1, val2, units;
647 func = MSI_RecordGetInteger( rec, 1 );
648 val1 = MSI_RecordGetInteger( rec, 2 );
649 val2 = MSI_RecordGetInteger( rec, 3 );
651 TRACE("progress: func %u val1 %u val2 %u\n", func, val1, val2);
653 units = val1 / 512;
654 switch (func)
656 case 0: /* init */
657 SendMessageW( ctrl->hwnd, PBM_SETRANGE, 0, MAKELPARAM(0,100) );
658 if (val2)
660 ctrl->progress_max = units ? units : 100;
661 ctrl->progress_current = units;
662 ctrl->progress_backwards = TRUE;
663 SendMessageW( ctrl->hwnd, PBM_SETPOS, 100, 0 );
665 else
667 ctrl->progress_max = units ? units : 100;
668 ctrl->progress_current = 0;
669 ctrl->progress_backwards = FALSE;
670 SendMessageW( ctrl->hwnd, PBM_SETPOS, 0, 0 );
672 break;
673 case 1: /* action data increment */
674 if (val2) dialog->package->action_progress_increment = val1;
675 else dialog->package->action_progress_increment = 0;
676 break;
677 case 2: /* move */
678 if (ctrl->progress_backwards)
680 if (units >= ctrl->progress_current) ctrl->progress_current -= units;
681 else ctrl->progress_current = 0;
683 else
685 if (ctrl->progress_current + units < ctrl->progress_max) ctrl->progress_current += units;
686 else ctrl->progress_current = ctrl->progress_max;
688 SendMessageW( ctrl->hwnd, PBM_SETPOS, MulDiv(100, ctrl->progress_current, ctrl->progress_max), 0 );
689 break;
690 case 3: /* add */
691 ctrl->progress_max += units;
692 break;
693 default:
694 FIXME("Unknown progress message %u\n", func);
695 break;
698 else if ( !strcmpW( attribute, szProperty ) )
700 MSIFEATURE *feature = msi_seltree_get_selected_feature( ctrl );
701 if (feature) msi_dialog_set_property( dialog->package, ctrl->property, feature->Directory );
703 else if ( !strcmpW( attribute, szSelectionPath ) )
705 BOOL indirect = ctrl->attributes & msidbControlAttributesIndirect;
706 LPWSTR path = msi_dialog_dup_property( dialog, ctrl->property, indirect );
707 if (!path) return;
708 SetWindowTextW( ctrl->hwnd, path );
709 msi_free(path);
711 else
713 FIXME("Attribute %s not being set\n", debugstr_w(attribute));
714 return;
718 static void event_subscribe( msi_dialog *dialog, const WCHAR *event, const WCHAR *control, const WCHAR *attribute )
720 struct subscriber *sub;
722 TRACE("event %s control %s attribute %s\n", debugstr_w(event), debugstr_w(control), debugstr_w(attribute));
724 LIST_FOR_EACH_ENTRY( sub, &dialog->package->subscriptions, struct subscriber, entry )
726 if (!strcmpiW( sub->event, event ) &&
727 !strcmpiW( sub->control, control ) &&
728 !strcmpiW( sub->attribute, attribute ))
730 TRACE("already subscribed\n");
731 return;
734 if (!(sub = msi_alloc( sizeof(*sub) ))) return;
735 sub->dialog = dialog;
736 sub->event = strdupW( event );
737 sub->control = strdupW( control );
738 sub->attribute = strdupW( attribute );
739 list_add_tail( &dialog->package->subscriptions, &sub->entry );
742 struct dialog_control
744 msi_dialog *dialog;
745 const WCHAR *control;
748 static UINT map_event( MSIRECORD *row, void *param )
750 struct dialog_control *dc = param;
751 const WCHAR *event = MSI_RecordGetString( row, 3 );
752 const WCHAR *attribute = MSI_RecordGetString( row, 4 );
754 event_subscribe( dc->dialog, event, dc->control, attribute );
755 return ERROR_SUCCESS;
758 static void dialog_map_events( msi_dialog *dialog, const WCHAR *control )
760 static const WCHAR queryW[] =
761 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
762 '`','E','v','e','n','t','M','a','p','p','i','n','g','`',' ',
763 'W','H','E','R','E',' ','`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
764 'A','N','D',' ','`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',0};
765 MSIQUERY *view;
766 struct dialog_control dialog_control =
768 dialog,
769 control
772 if (!MSI_OpenQuery( dialog->package->db, &view, queryW, dialog->name, control ))
774 MSI_IterateRecords( view, NULL, map_event, &dialog_control );
775 msiobj_release( &view->hdr );
779 /* everything except radio buttons */
780 static msi_control *msi_dialog_add_control( msi_dialog *dialog,
781 MSIRECORD *rec, LPCWSTR szCls, DWORD style )
783 DWORD attributes;
784 const WCHAR *text = NULL, *name, *control_type;
785 DWORD exstyle = 0;
787 name = MSI_RecordGetString( rec, 2 );
788 control_type = MSI_RecordGetString( rec, 3 );
789 attributes = MSI_RecordGetInteger( rec, 8 );
790 if (strcmpW( control_type, szScrollableText )) text = MSI_RecordGetString( rec, 10 );
792 TRACE("%s, %s, %08x, %s, %08x\n", debugstr_w(szCls), debugstr_w(name),
793 attributes, debugstr_w(text), style);
795 if( attributes & msidbControlAttributesVisible )
796 style |= WS_VISIBLE;
797 if( ~attributes & msidbControlAttributesEnabled )
798 style |= WS_DISABLED;
799 if( attributes & msidbControlAttributesSunken )
800 exstyle |= WS_EX_CLIENTEDGE;
802 dialog_map_events( dialog, name );
804 return dialog_create_window( dialog, rec, exstyle, szCls, name, text, style, dialog->hwnd );
807 struct msi_text_info
809 msi_font *font;
810 WNDPROC oldproc;
811 DWORD attributes;
815 * we don't erase our own background,
816 * so we have to make sure that the parent window redraws first
818 static void msi_text_on_settext( HWND hWnd )
820 HWND hParent;
821 RECT rc;
823 hParent = GetParent( hWnd );
824 GetWindowRect( hWnd, &rc );
825 MapWindowPoints( NULL, hParent, (LPPOINT) &rc, 2 );
826 InvalidateRect( hParent, &rc, TRUE );
829 static LRESULT WINAPI
830 MSIText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
832 struct msi_text_info *info;
833 LRESULT r = 0;
835 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
837 info = GetPropW(hWnd, szButtonData);
839 if( msg == WM_CTLCOLORSTATIC &&
840 ( info->attributes & msidbControlAttributesTransparent ) )
842 SetBkMode( (HDC)wParam, TRANSPARENT );
843 return (LRESULT) GetStockObject(NULL_BRUSH);
846 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
847 if ( info->font )
848 SetTextColor( (HDC)wParam, info->font->color );
850 switch( msg )
852 case WM_SETTEXT:
853 msi_text_on_settext( hWnd );
854 break;
855 case WM_NCDESTROY:
856 msi_free( info );
857 RemovePropW( hWnd, szButtonData );
858 break;
861 return r;
864 static UINT msi_dialog_text_control( msi_dialog *dialog, MSIRECORD *rec )
866 msi_control *control;
867 struct msi_text_info *info;
868 LPCWSTR text, ptr, prop, control_name;
869 LPWSTR font_name;
871 TRACE("%p %p\n", dialog, rec);
873 control = msi_dialog_add_control( dialog, rec, szStatic, SS_LEFT | WS_GROUP );
874 if( !control )
875 return ERROR_FUNCTION_FAILED;
877 info = msi_alloc( sizeof *info );
878 if( !info )
879 return ERROR_SUCCESS;
881 control_name = MSI_RecordGetString( rec, 2 );
882 control->attributes = MSI_RecordGetInteger( rec, 8 );
883 prop = MSI_RecordGetString( rec, 9 );
884 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
886 text = MSI_RecordGetString( rec, 10 );
887 font_name = msi_dialog_get_style( text, &ptr );
888 info->font = ( font_name ) ? msi_dialog_find_font( dialog, font_name ) : NULL;
889 msi_free( font_name );
891 info->attributes = MSI_RecordGetInteger( rec, 8 );
892 if( info->attributes & msidbControlAttributesTransparent )
893 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT );
895 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
896 (LONG_PTR)MSIText_WndProc );
897 SetPropW( control->hwnd, szButtonData, info );
899 event_subscribe( dialog, szSelectionPath, control_name, szSelectionPath );
900 return ERROR_SUCCESS;
903 /* strip any leading text style label from text field */
904 static WCHAR *msi_get_binary_name( MSIPACKAGE *package, MSIRECORD *rec )
906 WCHAR *p, *text;
908 text = msi_get_deformatted_field( package, rec, 10 );
909 if (!text)
910 return NULL;
912 p = text;
913 while (*p && *p != '{') p++;
914 if (!*p++) return text;
916 while (*p && *p != '}') p++;
917 if (!*p++) return text;
919 p = strdupW( p );
920 msi_free( text );
921 return p;
924 static UINT msi_dialog_set_property_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
926 static const WCHAR szNullArg[] = {'{','}',0};
927 LPWSTR p, prop, arg_fmt = NULL;
928 UINT len;
930 len = strlenW( event );
931 prop = msi_alloc( len * sizeof(WCHAR) );
932 strcpyW( prop, &event[1] );
933 p = strchrW( prop, ']' );
934 if (p && (p[1] == 0 || p[1] == ' '))
936 *p = 0;
937 if (strcmpW( szNullArg, arg ))
938 deformat_string( dialog->package, arg, &arg_fmt );
939 msi_dialog_set_property( dialog->package, prop, arg_fmt );
940 msi_dialog_update_controls( dialog, prop );
941 msi_free( arg_fmt );
943 else ERR("Badly formatted property string - what happens?\n");
944 msi_free( prop );
945 return ERROR_SUCCESS;
948 static UINT msi_dialog_send_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
950 LPWSTR event_fmt = NULL, arg_fmt = NULL;
952 TRACE("Sending control event %s %s\n", debugstr_w(event), debugstr_w(arg));
954 deformat_string( dialog->package, event, &event_fmt );
955 deformat_string( dialog->package, arg, &arg_fmt );
957 dialog->event_handler( dialog, event_fmt, arg_fmt );
959 msi_free( event_fmt );
960 msi_free( arg_fmt );
962 return ERROR_SUCCESS;
965 static UINT msi_dialog_control_event( MSIRECORD *rec, LPVOID param )
967 msi_dialog *dialog = param;
968 LPCWSTR condition, event, arg;
969 UINT r;
971 condition = MSI_RecordGetString( rec, 5 );
972 r = MSI_EvaluateConditionW( dialog->package, condition );
973 if (r == MSICONDITION_TRUE || r == MSICONDITION_NONE)
975 event = MSI_RecordGetString( rec, 3 );
976 arg = MSI_RecordGetString( rec, 4 );
977 if (event[0] == '[')
978 msi_dialog_set_property_event( dialog, event, arg );
979 else
980 msi_dialog_send_event( dialog, event, arg );
982 return ERROR_SUCCESS;
985 static UINT msi_dialog_button_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
987 static const WCHAR query[] = {
988 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
989 'C','o','n','t','r','o','l','E','v','e','n','t',' ','W','H','E','R','E',' ',
990 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ','A','N','D',' ',
991 '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',' ',
992 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','i','n','g','`',0};
993 MSIQUERY *view;
994 UINT r;
996 if (HIWORD(param) != BN_CLICKED)
997 return ERROR_SUCCESS;
999 r = MSI_OpenQuery( dialog->package->db, &view, query, dialog->name, control->name );
1000 if (r != ERROR_SUCCESS)
1002 ERR("query failed\n");
1003 return ERROR_SUCCESS;
1005 r = MSI_IterateRecords( view, 0, msi_dialog_control_event, dialog );
1006 msiobj_release( &view->hdr );
1007 return r;
1010 static UINT msi_dialog_button_control( msi_dialog *dialog, MSIRECORD *rec )
1012 msi_control *control;
1013 UINT attributes, style;
1015 TRACE("%p %p\n", dialog, rec);
1017 style = WS_TABSTOP;
1018 attributes = MSI_RecordGetInteger( rec, 8 );
1019 if( attributes & msidbControlAttributesIcon )
1020 style |= BS_ICON;
1022 control = msi_dialog_add_control( dialog, rec, szButton, style );
1023 if( !control )
1024 return ERROR_FUNCTION_FAILED;
1026 control->handler = msi_dialog_button_handler;
1028 if (attributes & msidbControlAttributesIcon)
1030 /* set the icon */
1031 LPWSTR name = msi_get_binary_name( dialog->package, rec );
1032 control->hIcon = msi_load_icon( dialog->package->db, name, attributes );
1033 if (control->hIcon)
1035 SendMessageW( control->hwnd, BM_SETIMAGE, IMAGE_ICON, (LPARAM) control->hIcon );
1037 else
1038 ERR("Failed to load icon %s\n", debugstr_w(name));
1039 msi_free( name );
1042 return ERROR_SUCCESS;
1045 static LPWSTR msi_get_checkbox_value( msi_dialog *dialog, LPCWSTR prop )
1047 static const WCHAR query[] = {
1048 'S','E','L','E','C','T',' ','*',' ',
1049 'F','R','O','M',' ','`','C','h','e','c','k','B','o','x','`',' ',
1050 'W','H','E','R','E',' ',
1051 '`','P','r','o','p','e','r','t','y','`',' ','=',' ',
1052 '\'','%','s','\'',0
1054 MSIRECORD *rec = NULL;
1055 LPWSTR ret = NULL;
1057 /* find if there is a value associated with the checkbox */
1058 rec = MSI_QueryGetRecord( dialog->package->db, query, prop );
1059 if (!rec)
1060 return ret;
1062 ret = msi_get_deformatted_field( dialog->package, rec, 2 );
1063 if( ret && !ret[0] )
1065 msi_free( ret );
1066 ret = NULL;
1068 msiobj_release( &rec->hdr );
1069 if (ret)
1070 return ret;
1072 ret = msi_dup_property( dialog->package->db, prop );
1073 if( ret && !ret[0] )
1075 msi_free( ret );
1076 ret = NULL;
1079 return ret;
1082 static UINT msi_dialog_get_checkbox_state( msi_dialog *dialog, msi_control *control )
1084 WCHAR state[2] = {0};
1085 DWORD sz = 2;
1087 msi_get_property( dialog->package->db, control->property, state, &sz );
1088 return state[0] ? 1 : 0;
1091 static void msi_dialog_set_checkbox_state( msi_dialog *dialog, msi_control *control, UINT state )
1093 static const WCHAR szState[] = {'1',0};
1094 LPCWSTR val;
1096 /* if uncheck then the property is set to NULL */
1097 if (!state)
1099 msi_dialog_set_property( dialog->package, control->property, NULL );
1100 return;
1103 /* check for a custom state */
1104 if (control->value && control->value[0])
1105 val = control->value;
1106 else
1107 val = szState;
1109 msi_dialog_set_property( dialog->package, control->property, val );
1112 static void msi_dialog_checkbox_sync_state( msi_dialog *dialog, msi_control *control )
1114 UINT state = msi_dialog_get_checkbox_state( dialog, control );
1115 SendMessageW( control->hwnd, BM_SETCHECK, state ? BST_CHECKED : BST_UNCHECKED, 0 );
1118 static UINT msi_dialog_checkbox_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
1120 UINT state;
1122 if (HIWORD(param) != BN_CLICKED)
1123 return ERROR_SUCCESS;
1125 TRACE("clicked checkbox %s, set %s\n", debugstr_w(control->name), debugstr_w(control->property));
1127 state = msi_dialog_get_checkbox_state( dialog, control );
1128 state = state ? 0 : 1;
1129 msi_dialog_set_checkbox_state( dialog, control, state );
1130 msi_dialog_checkbox_sync_state( dialog, control );
1132 return msi_dialog_button_handler( dialog, control, param );
1135 static UINT msi_dialog_checkbox_control( msi_dialog *dialog, MSIRECORD *rec )
1137 msi_control *control;
1138 LPCWSTR prop;
1140 TRACE("%p %p\n", dialog, rec);
1142 control = msi_dialog_add_control( dialog, rec, szButton, BS_CHECKBOX | BS_MULTILINE | WS_TABSTOP );
1143 control->handler = msi_dialog_checkbox_handler;
1144 control->update = msi_dialog_checkbox_sync_state;
1145 prop = MSI_RecordGetString( rec, 9 );
1146 if (prop)
1148 control->property = strdupW( prop );
1149 control->value = msi_get_checkbox_value( dialog, prop );
1150 TRACE("control %s value %s\n", debugstr_w(control->property), debugstr_w(control->value));
1152 msi_dialog_checkbox_sync_state( dialog, control );
1153 return ERROR_SUCCESS;
1156 static UINT msi_dialog_line_control( msi_dialog *dialog, MSIRECORD *rec )
1158 DWORD attributes;
1159 LPCWSTR name;
1160 DWORD style, exstyle = 0;
1161 DWORD x, y, width, height;
1162 msi_control *control;
1164 TRACE("%p %p\n", dialog, rec);
1166 style = WS_CHILD | SS_ETCHEDHORZ | SS_SUNKEN;
1168 name = MSI_RecordGetString( rec, 2 );
1169 attributes = MSI_RecordGetInteger( rec, 8 );
1171 if( attributes & msidbControlAttributesVisible )
1172 style |= WS_VISIBLE;
1173 if( ~attributes & msidbControlAttributesEnabled )
1174 style |= WS_DISABLED;
1175 if( attributes & msidbControlAttributesSunken )
1176 exstyle |= WS_EX_CLIENTEDGE;
1178 dialog_map_events( dialog, name );
1180 control = msi_alloc( FIELD_OFFSET(msi_control, name[strlenW( name ) + 1] ));
1181 if (!control)
1182 return ERROR_OUTOFMEMORY;
1184 strcpyW( control->name, name );
1185 list_add_head( &dialog->controls, &control->entry );
1186 control->handler = NULL;
1187 control->property = NULL;
1188 control->value = NULL;
1189 control->hBitmap = NULL;
1190 control->hIcon = NULL;
1191 control->hDll = NULL;
1192 control->tabnext = strdupW( MSI_RecordGetString( rec, 11) );
1193 control->type = strdupW( MSI_RecordGetString( rec, 3 ) );
1194 control->progress_current = 0;
1195 control->progress_max = 100;
1196 control->progress_backwards = FALSE;
1198 x = MSI_RecordGetInteger( rec, 4 );
1199 y = MSI_RecordGetInteger( rec, 5 );
1200 width = MSI_RecordGetInteger( rec, 6 );
1202 x = msi_dialog_scale_unit( dialog, x );
1203 y = msi_dialog_scale_unit( dialog, y );
1204 width = msi_dialog_scale_unit( dialog, width );
1205 height = 2; /* line is exactly 2 units in height */
1207 control->hwnd = CreateWindowExW( exstyle, szStatic, NULL, style,
1208 x, y, width, height, dialog->hwnd, NULL, NULL, NULL );
1210 TRACE("Dialog %s control %s hwnd %p\n",
1211 debugstr_w(dialog->name), debugstr_w(name), control->hwnd );
1213 return ERROR_SUCCESS;
1216 /******************** Scroll Text ********************************************/
1218 struct msi_scrolltext_info
1220 msi_dialog *dialog;
1221 msi_control *control;
1222 WNDPROC oldproc;
1225 static LRESULT WINAPI
1226 MSIScrollText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1228 struct msi_scrolltext_info *info;
1229 HRESULT r;
1231 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1233 info = GetPropW( hWnd, szButtonData );
1235 r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
1237 switch( msg )
1239 case WM_GETDLGCODE:
1240 return DLGC_WANTARROWS;
1241 case WM_NCDESTROY:
1242 msi_free( info );
1243 RemovePropW( hWnd, szButtonData );
1244 break;
1245 case WM_PAINT:
1246 /* native MSI sets a wait cursor here */
1247 msi_dialog_button_handler( info->dialog, info->control, BN_CLICKED );
1248 break;
1250 return r;
1253 struct msi_streamin_info
1255 LPSTR string;
1256 DWORD offset;
1257 DWORD length;
1260 static DWORD CALLBACK
1261 msi_richedit_stream_in( DWORD_PTR arg, LPBYTE buffer, LONG count, LONG *pcb )
1263 struct msi_streamin_info *info = (struct msi_streamin_info*) arg;
1265 if( (count + info->offset) > info->length )
1266 count = info->length - info->offset;
1267 memcpy( buffer, &info->string[ info->offset ], count );
1268 *pcb = count;
1269 info->offset += count;
1271 TRACE("%d/%d\n", info->offset, info->length);
1273 return 0;
1276 static void msi_scrolltext_add_text( msi_control *control, LPCWSTR text )
1278 struct msi_streamin_info info;
1279 EDITSTREAM es;
1281 info.string = strdupWtoA( text );
1282 info.offset = 0;
1283 info.length = lstrlenA( info.string ) + 1;
1285 es.dwCookie = (DWORD_PTR) &info;
1286 es.dwError = 0;
1287 es.pfnCallback = msi_richedit_stream_in;
1289 SendMessageW( control->hwnd, EM_STREAMIN, SF_RTF, (LPARAM) &es );
1291 msi_free( info.string );
1294 static UINT msi_dialog_scrolltext_control( msi_dialog *dialog, MSIRECORD *rec )
1296 static const WCHAR szRichEdit20W[] = {'R','i','c','h','E','d','i','t','2','0','W',0};
1297 struct msi_scrolltext_info *info;
1298 msi_control *control;
1299 HMODULE hRichedit;
1300 LPCWSTR text;
1301 DWORD style;
1303 info = msi_alloc( sizeof *info );
1304 if (!info)
1305 return ERROR_FUNCTION_FAILED;
1307 hRichedit = LoadLibraryA("riched20");
1309 style = WS_BORDER | ES_MULTILINE | WS_VSCROLL |
1310 ES_READONLY | ES_AUTOVSCROLL | WS_TABSTOP;
1311 control = msi_dialog_add_control( dialog, rec, szRichEdit20W, style );
1312 if (!control)
1314 FreeLibrary( hRichedit );
1315 msi_free( info );
1316 return ERROR_FUNCTION_FAILED;
1319 control->hDll = hRichedit;
1321 info->dialog = dialog;
1322 info->control = control;
1324 /* subclass the static control */
1325 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1326 (LONG_PTR)MSIScrollText_WndProc );
1327 SetPropW( control->hwnd, szButtonData, info );
1329 /* add the text into the richedit */
1330 text = MSI_RecordGetString( rec, 10 );
1331 if (text)
1332 msi_scrolltext_add_text( control, text );
1334 return ERROR_SUCCESS;
1337 static HBITMAP msi_load_picture( MSIDATABASE *db, LPCWSTR name,
1338 INT cx, INT cy, DWORD flags )
1340 HBITMAP hOleBitmap = 0, hBitmap = 0, hOldSrcBitmap, hOldDestBitmap;
1341 MSIRECORD *rec = NULL;
1342 IStream *stm = NULL;
1343 IPicture *pic = NULL;
1344 HDC srcdc, destdc;
1345 BITMAP bm;
1346 UINT r;
1348 rec = msi_get_binary_record( db, name );
1349 if( !rec )
1350 goto end;
1352 r = MSI_RecordGetIStream( rec, 2, &stm );
1353 msiobj_release( &rec->hdr );
1354 if( r != ERROR_SUCCESS )
1355 goto end;
1357 r = OleLoadPicture( stm, 0, TRUE, &IID_IPicture, (LPVOID*) &pic );
1358 IStream_Release( stm );
1359 if( FAILED( r ) )
1361 ERR("failed to load picture\n");
1362 goto end;
1365 r = IPicture_get_Handle( pic, (OLE_HANDLE*) &hOleBitmap );
1366 if( FAILED( r ) )
1368 ERR("failed to get bitmap handle\n");
1369 goto end;
1372 /* make the bitmap the desired size */
1373 r = GetObjectW( hOleBitmap, sizeof bm, &bm );
1374 if (r != sizeof bm )
1376 ERR("failed to get bitmap size\n");
1377 goto end;
1380 if (flags & LR_DEFAULTSIZE)
1382 cx = bm.bmWidth;
1383 cy = bm.bmHeight;
1386 srcdc = CreateCompatibleDC( NULL );
1387 hOldSrcBitmap = SelectObject( srcdc, hOleBitmap );
1388 destdc = CreateCompatibleDC( NULL );
1389 hBitmap = CreateCompatibleBitmap( srcdc, cx, cy );
1390 hOldDestBitmap = SelectObject( destdc, hBitmap );
1391 StretchBlt( destdc, 0, 0, cx, cy,
1392 srcdc, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
1393 SelectObject( srcdc, hOldSrcBitmap );
1394 SelectObject( destdc, hOldDestBitmap );
1395 DeleteDC( srcdc );
1396 DeleteDC( destdc );
1398 end:
1399 if ( pic )
1400 IPicture_Release( pic );
1401 return hBitmap;
1404 static UINT msi_dialog_bitmap_control( msi_dialog *dialog, MSIRECORD *rec )
1406 UINT cx, cy, flags, style, attributes;
1407 msi_control *control;
1408 LPWSTR name;
1410 flags = LR_LOADFROMFILE;
1411 style = SS_BITMAP | SS_LEFT | WS_GROUP;
1413 attributes = MSI_RecordGetInteger( rec, 8 );
1414 if( attributes & msidbControlAttributesFixedSize )
1416 flags |= LR_DEFAULTSIZE;
1417 style |= SS_CENTERIMAGE;
1420 control = msi_dialog_add_control( dialog, rec, szStatic, style );
1421 cx = MSI_RecordGetInteger( rec, 6 );
1422 cy = MSI_RecordGetInteger( rec, 7 );
1423 cx = msi_dialog_scale_unit( dialog, cx );
1424 cy = msi_dialog_scale_unit( dialog, cy );
1426 name = msi_get_binary_name( dialog->package, rec );
1427 control->hBitmap = msi_load_picture( dialog->package->db, name, cx, cy, flags );
1428 if( control->hBitmap )
1429 SendMessageW( control->hwnd, STM_SETIMAGE,
1430 IMAGE_BITMAP, (LPARAM) control->hBitmap );
1431 else
1432 ERR("Failed to load bitmap %s\n", debugstr_w(name));
1434 msi_free( name );
1436 return ERROR_SUCCESS;
1439 static UINT msi_dialog_icon_control( msi_dialog *dialog, MSIRECORD *rec )
1441 msi_control *control;
1442 DWORD attributes;
1443 LPWSTR name;
1445 TRACE("\n");
1447 control = msi_dialog_add_control( dialog, rec, szStatic,
1448 SS_ICON | SS_CENTERIMAGE | WS_GROUP );
1450 attributes = MSI_RecordGetInteger( rec, 8 );
1451 name = msi_get_binary_name( dialog->package, rec );
1452 control->hIcon = msi_load_icon( dialog->package->db, name, attributes );
1453 if( control->hIcon )
1454 SendMessageW( control->hwnd, STM_SETICON, (WPARAM) control->hIcon, 0 );
1455 else
1456 ERR("Failed to load bitmap %s\n", debugstr_w(name));
1457 msi_free( name );
1458 return ERROR_SUCCESS;
1461 /******************** Combo Box ***************************************/
1463 struct msi_combobox_info
1465 msi_dialog *dialog;
1466 HWND hwnd;
1467 WNDPROC oldproc;
1468 DWORD num_items;
1469 DWORD addpos_items;
1470 LPWSTR *items;
1473 static LRESULT WINAPI MSIComboBox_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1475 struct msi_combobox_info *info;
1476 LRESULT r;
1477 DWORD j;
1479 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1481 info = GetPropW( hWnd, szButtonData );
1482 if (!info)
1483 return 0;
1485 r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
1487 switch (msg)
1489 case WM_NCDESTROY:
1490 for (j = 0; j < info->num_items; j++)
1491 msi_free( info->items[j] );
1492 msi_free( info->items );
1493 msi_free( info );
1494 RemovePropW( hWnd, szButtonData );
1495 break;
1498 return r;
1501 static UINT msi_combobox_add_item( MSIRECORD *rec, LPVOID param )
1503 struct msi_combobox_info *info = param;
1504 LPCWSTR value, text;
1505 int pos;
1507 value = MSI_RecordGetString( rec, 3 );
1508 text = MSI_RecordGetString( rec, 4 );
1510 info->items[info->addpos_items] = strdupW( value );
1512 pos = SendMessageW( info->hwnd, CB_ADDSTRING, 0, (LPARAM)text );
1513 SendMessageW( info->hwnd, CB_SETITEMDATA, pos, (LPARAM)info->items[info->addpos_items] );
1514 info->addpos_items++;
1516 return ERROR_SUCCESS;
1519 static UINT msi_combobox_add_items( struct msi_combobox_info *info, LPCWSTR property )
1521 static const WCHAR query[] = {
1522 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1523 '`','C','o','m','b','o','B','o','x','`',' ','W','H','E','R','E',' ',
1524 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',' ',
1525 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','`',0};
1526 MSIQUERY *view;
1527 DWORD count;
1528 UINT r;
1530 r = MSI_OpenQuery( info->dialog->package->db, &view, query, property );
1531 if (r != ERROR_SUCCESS)
1532 return r;
1534 /* just get the number of records */
1535 count = 0;
1536 r = MSI_IterateRecords( view, &count, NULL, NULL );
1537 if (r != ERROR_SUCCESS)
1539 msiobj_release( &view->hdr );
1540 return r;
1542 info->num_items = count;
1543 info->items = msi_alloc( sizeof(*info->items) * count );
1545 r = MSI_IterateRecords( view, NULL, msi_combobox_add_item, info );
1546 msiobj_release( &view->hdr );
1547 return r;
1550 static UINT msi_dialog_set_control_condition( MSIRECORD *rec, LPVOID param )
1552 static const WCHAR szHide[] = {'H','i','d','e',0};
1553 static const WCHAR szShow[] = {'S','h','o','w',0};
1554 static const WCHAR szDisable[] = {'D','i','s','a','b','l','e',0};
1555 static const WCHAR szEnable[] = {'E','n','a','b','l','e',0};
1556 static const WCHAR szDefault[] = {'D','e','f','a','u','l','t',0};
1557 msi_dialog *dialog = param;
1558 msi_control *control;
1559 LPCWSTR name, action, condition;
1560 UINT r;
1562 name = MSI_RecordGetString( rec, 2 );
1563 action = MSI_RecordGetString( rec, 3 );
1564 condition = MSI_RecordGetString( rec, 4 );
1565 r = MSI_EvaluateConditionW( dialog->package, condition );
1566 control = msi_dialog_find_control( dialog, name );
1567 if (r == MSICONDITION_TRUE && control)
1569 TRACE("%s control %s\n", debugstr_w(action), debugstr_w(name));
1571 /* FIXME: case sensitive? */
1572 if (!strcmpW( action, szHide ))
1573 ShowWindow(control->hwnd, SW_HIDE);
1574 else if (!strcmpW( action, szShow ))
1575 ShowWindow(control->hwnd, SW_SHOW);
1576 else if (!strcmpW( action, szDisable ))
1577 EnableWindow(control->hwnd, FALSE);
1578 else if (!strcmpW( action, szEnable ))
1579 EnableWindow(control->hwnd, TRUE);
1580 else if (!strcmpW( action, szDefault ))
1581 SetFocus(control->hwnd);
1582 else
1583 FIXME("Unhandled action %s\n", debugstr_w(action));
1585 return ERROR_SUCCESS;
1588 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog )
1590 static const WCHAR query[] = {
1591 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1592 'C','o','n','t','r','o','l','C','o','n','d','i','t','i','o','n',' ',
1593 'W','H','E','R','E',' ','`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
1594 UINT r;
1595 MSIQUERY *view;
1596 MSIPACKAGE *package = dialog->package;
1598 TRACE("%p %s\n", dialog, debugstr_w(dialog->name));
1600 /* query the Control table for all the elements of the control */
1601 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
1602 if (r != ERROR_SUCCESS)
1603 return ERROR_SUCCESS;
1605 r = MSI_IterateRecords( view, 0, msi_dialog_set_control_condition, dialog );
1606 msiobj_release( &view->hdr );
1607 return r;
1610 static UINT msi_dialog_combobox_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
1612 struct msi_combobox_info *info;
1613 int index;
1614 LPWSTR value;
1616 if (HIWORD(param) != CBN_SELCHANGE && HIWORD(param) != CBN_EDITCHANGE)
1617 return ERROR_SUCCESS;
1619 info = GetPropW( control->hwnd, szButtonData );
1620 index = SendMessageW( control->hwnd, CB_GETCURSEL, 0, 0 );
1621 if (index == CB_ERR)
1622 value = msi_get_window_text( control->hwnd );
1623 else
1624 value = (LPWSTR) SendMessageW( control->hwnd, CB_GETITEMDATA, index, 0 );
1626 msi_dialog_set_property( info->dialog->package, control->property, value );
1627 msi_dialog_evaluate_control_conditions( info->dialog );
1629 if (index == CB_ERR)
1630 msi_free( value );
1632 return ERROR_SUCCESS;
1635 static void msi_dialog_combobox_update( msi_dialog *dialog, msi_control *control )
1637 struct msi_combobox_info *info;
1638 LPWSTR value, tmp;
1639 DWORD j;
1641 info = GetPropW( control->hwnd, szButtonData );
1643 value = msi_dup_property( dialog->package->db, control->property );
1644 if (!value)
1646 SendMessageW( control->hwnd, CB_SETCURSEL, -1, 0 );
1647 return;
1650 for (j = 0; j < info->num_items; j++)
1652 tmp = (LPWSTR) SendMessageW( control->hwnd, CB_GETITEMDATA, j, 0 );
1653 if (!strcmpW( value, tmp ))
1654 break;
1657 if (j < info->num_items)
1659 SendMessageW( control->hwnd, CB_SETCURSEL, j, 0 );
1661 else
1663 SendMessageW( control->hwnd, CB_SETCURSEL, -1, 0 );
1664 SetWindowTextW( control->hwnd, value );
1667 msi_free(value);
1670 static UINT msi_dialog_combo_control( msi_dialog *dialog, MSIRECORD *rec )
1672 struct msi_combobox_info *info;
1673 msi_control *control;
1674 DWORD attributes, style;
1675 LPCWSTR prop;
1677 info = msi_alloc( sizeof *info );
1678 if (!info)
1679 return ERROR_FUNCTION_FAILED;
1681 style = CBS_AUTOHSCROLL | WS_TABSTOP | WS_GROUP | WS_CHILD;
1682 attributes = MSI_RecordGetInteger( rec, 8 );
1683 if ( ~attributes & msidbControlAttributesSorted)
1684 style |= CBS_SORT;
1685 if ( attributes & msidbControlAttributesComboList)
1686 style |= CBS_DROPDOWNLIST;
1687 else
1688 style |= CBS_DROPDOWN;
1690 control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
1691 if (!control)
1693 msi_free( info );
1694 return ERROR_FUNCTION_FAILED;
1697 control->handler = msi_dialog_combobox_handler;
1698 control->update = msi_dialog_combobox_update;
1700 prop = MSI_RecordGetString( rec, 9 );
1701 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
1703 /* subclass */
1704 info->dialog = dialog;
1705 info->hwnd = control->hwnd;
1706 info->items = NULL;
1707 info->addpos_items = 0;
1708 info->oldproc = (WNDPROC)SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1709 (LONG_PTR)MSIComboBox_WndProc );
1710 SetPropW( control->hwnd, szButtonData, info );
1712 if (control->property)
1713 msi_combobox_add_items( info, control->property );
1715 msi_dialog_combobox_update( dialog, control );
1717 return ERROR_SUCCESS;
1720 static UINT msi_dialog_edit_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
1722 LPWSTR buf;
1724 if (HIWORD(param) != EN_CHANGE)
1725 return ERROR_SUCCESS;
1727 TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name), debugstr_w(control->property));
1729 buf = msi_get_window_text( control->hwnd );
1730 msi_dialog_set_property( dialog->package, control->property, buf );
1731 msi_free( buf );
1733 return ERROR_SUCCESS;
1736 /* length of 2^32 + 1 */
1737 #define MAX_NUM_DIGITS 11
1739 static UINT msi_dialog_edit_control( msi_dialog *dialog, MSIRECORD *rec )
1741 msi_control *control;
1742 LPCWSTR prop, text;
1743 LPWSTR val, begin, end;
1744 WCHAR num[MAX_NUM_DIGITS];
1745 DWORD limit;
1747 control = msi_dialog_add_control( dialog, rec, szEdit,
1748 WS_BORDER | WS_TABSTOP | ES_AUTOHSCROLL );
1749 control->handler = msi_dialog_edit_handler;
1751 text = MSI_RecordGetString( rec, 10 );
1752 if ( text )
1754 begin = strchrW( text, '{' );
1755 end = strchrW( text, '}' );
1757 if ( begin && end && end > begin &&
1758 begin[0] >= '0' && begin[0] <= '9' &&
1759 end - begin < MAX_NUM_DIGITS)
1761 lstrcpynW( num, begin + 1, end - begin );
1762 limit = atolW( num );
1764 SendMessageW( control->hwnd, EM_SETLIMITTEXT, limit, 0 );
1768 prop = MSI_RecordGetString( rec, 9 );
1769 if( prop )
1770 control->property = strdupW( prop );
1772 val = msi_dup_property( dialog->package->db, control->property );
1773 SetWindowTextW( control->hwnd, val );
1774 msi_free( val );
1775 return ERROR_SUCCESS;
1778 /******************** Masked Edit ********************************************/
1780 #define MASK_MAX_GROUPS 20
1782 struct msi_mask_group
1784 UINT len;
1785 UINT ofs;
1786 WCHAR type;
1787 HWND hwnd;
1790 struct msi_maskedit_info
1792 msi_dialog *dialog;
1793 WNDPROC oldproc;
1794 HWND hwnd;
1795 LPWSTR prop;
1796 UINT num_chars;
1797 UINT num_groups;
1798 struct msi_mask_group group[MASK_MAX_GROUPS];
1801 static BOOL msi_mask_editable( WCHAR type )
1803 switch (type)
1805 case '%':
1806 case '#':
1807 case '&':
1808 case '`':
1809 case '?':
1810 case '^':
1811 return TRUE;
1813 return FALSE;
1816 static void msi_mask_control_change( struct msi_maskedit_info *info )
1818 LPWSTR val;
1819 UINT i, n, r;
1821 val = msi_alloc( (info->num_chars+1)*sizeof(WCHAR) );
1822 for( i=0, n=0; i<info->num_groups; i++ )
1824 if (info->group[i].len == ~0u)
1826 UINT len = SendMessageW( info->group[i].hwnd, WM_GETTEXTLENGTH, 0, 0 );
1827 val = msi_realloc( val, (len + 1) * sizeof(WCHAR) );
1828 GetWindowTextW( info->group[i].hwnd, val, len + 1 );
1830 else
1832 if (info->group[i].len + n > info->num_chars)
1834 ERR("can't fit control %d text into template\n",i);
1835 break;
1837 if (!msi_mask_editable(info->group[i].type))
1839 for(r=0; r<info->group[i].len; r++)
1840 val[n+r] = info->group[i].type;
1841 val[n+r] = 0;
1843 else
1845 r = GetWindowTextW( info->group[i].hwnd, &val[n], info->group[i].len+1 );
1846 if( r != info->group[i].len )
1847 break;
1849 n += r;
1853 TRACE("%d/%d controls were good\n", i, info->num_groups);
1855 if( i == info->num_groups )
1857 TRACE("Set property %s to %s\n", debugstr_w(info->prop), debugstr_w(val));
1858 msi_dialog_set_property( info->dialog->package, info->prop, val );
1859 msi_dialog_evaluate_control_conditions( info->dialog );
1861 msi_free( val );
1864 /* now move to the next control if necessary */
1865 static VOID msi_mask_next_control( struct msi_maskedit_info *info, HWND hWnd )
1867 HWND hWndNext;
1868 UINT len, i;
1870 for( i=0; i<info->num_groups; i++ )
1871 if( info->group[i].hwnd == hWnd )
1872 break;
1874 /* don't move from the last control */
1875 if( i >= (info->num_groups-1) )
1876 return;
1878 len = SendMessageW( hWnd, WM_GETTEXTLENGTH, 0, 0 );
1879 if( len < info->group[i].len )
1880 return;
1882 hWndNext = GetNextDlgTabItem( GetParent( hWnd ), hWnd, FALSE );
1883 SetFocus( hWndNext );
1886 static LRESULT WINAPI
1887 MSIMaskedEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1889 struct msi_maskedit_info *info;
1890 HRESULT r;
1892 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1894 info = GetPropW(hWnd, szButtonData);
1896 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1898 switch( msg )
1900 case WM_COMMAND:
1901 if (HIWORD(wParam) == EN_CHANGE)
1903 msi_mask_control_change( info );
1904 msi_mask_next_control( info, (HWND) lParam );
1906 break;
1907 case WM_NCDESTROY:
1908 msi_free( info->prop );
1909 msi_free( info );
1910 RemovePropW( hWnd, szButtonData );
1911 break;
1914 return r;
1917 /* fish the various bits of the property out and put them in the control */
1918 static void
1919 msi_maskedit_set_text( struct msi_maskedit_info *info, LPCWSTR text )
1921 LPCWSTR p;
1922 UINT i;
1924 p = text;
1925 for( i = 0; i < info->num_groups; i++ )
1927 if( info->group[i].len < strlenW( p ) )
1929 LPWSTR chunk = strdupW( p );
1930 chunk[ info->group[i].len ] = 0;
1931 SetWindowTextW( info->group[i].hwnd, chunk );
1932 msi_free( chunk );
1934 else
1936 SetWindowTextW( info->group[i].hwnd, p );
1937 break;
1939 p += info->group[i].len;
1943 static struct msi_maskedit_info * msi_dialog_parse_groups( LPCWSTR mask )
1945 struct msi_maskedit_info *info;
1946 int i = 0, n = 0, total = 0;
1947 LPCWSTR p;
1949 TRACE("masked control, template %s\n", debugstr_w(mask));
1951 if( !mask )
1952 return NULL;
1954 info = msi_alloc_zero( sizeof *info );
1955 if( !info )
1956 return info;
1958 p = strchrW(mask, '<');
1959 if( p )
1960 p++;
1961 else
1962 p = mask;
1964 for( i=0; i<MASK_MAX_GROUPS; i++ )
1966 /* stop at the end of the string */
1967 if( p[0] == 0 || p[0] == '>' )
1969 if (!total)
1971 /* create a group for the empty mask */
1972 info->group[0].type = '&';
1973 info->group[0].len = ~0u;
1974 i = 1;
1976 break;
1979 /* count the number of the same identifier */
1980 for( n=0; p[n] == p[0]; n++ )
1982 info->group[i].ofs = total;
1983 info->group[i].type = p[0];
1984 if( p[n] == '=' )
1986 n++;
1987 total++; /* an extra not part of the group */
1989 info->group[i].len = n;
1990 total += n;
1991 p += n;
1994 TRACE("%d characters in %d groups\n", total, i );
1995 if( i == MASK_MAX_GROUPS )
1996 ERR("too many groups in PIDTemplate %s\n", debugstr_w(mask));
1998 info->num_chars = total;
1999 info->num_groups = i;
2001 return info;
2004 static void
2005 msi_maskedit_create_children( struct msi_maskedit_info *info, LPCWSTR font )
2007 DWORD width, height, style, wx, ww;
2008 RECT rect;
2009 HWND hwnd;
2010 UINT i;
2012 style = WS_CHILD | WS_BORDER | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL;
2014 GetClientRect( info->hwnd, &rect );
2016 width = rect.right - rect.left;
2017 height = rect.bottom - rect.top;
2019 for( i = 0; i < info->num_groups; i++ )
2021 if (!msi_mask_editable( info->group[i].type ))
2022 continue;
2023 if (info->num_chars)
2025 wx = (info->group[i].ofs * width) / info->num_chars;
2026 ww = (info->group[i].len * width) / info->num_chars;
2028 else
2030 wx = 0;
2031 ww = width;
2033 hwnd = CreateWindowW( szEdit, NULL, style, wx, 0, ww, height,
2034 info->hwnd, NULL, NULL, NULL );
2035 if( !hwnd )
2037 ERR("failed to create mask edit sub window\n");
2038 break;
2041 SendMessageW( hwnd, EM_LIMITTEXT, info->group[i].len, 0 );
2043 msi_dialog_set_font( info->dialog, hwnd,
2044 font?font:info->dialog->default_font );
2045 info->group[i].hwnd = hwnd;
2050 * office 2003 uses "73931<````=````=````=````=`````>@@@@@"
2051 * delphi 7 uses "<????-??????-??????-????>" and "<???-???>"
2052 * filemaker pro 7 uses "<^^^^=^^^^=^^^^=^^^^=^^^^=^^^^=^^^^^>"
2054 static UINT msi_dialog_maskedit_control( msi_dialog *dialog, MSIRECORD *rec )
2056 LPWSTR font_mask, val = NULL, font;
2057 struct msi_maskedit_info *info = NULL;
2058 UINT ret = ERROR_SUCCESS;
2059 msi_control *control;
2060 LPCWSTR prop, mask;
2062 TRACE("\n");
2064 font_mask = msi_get_deformatted_field( dialog->package, rec, 10 );
2065 font = msi_dialog_get_style( font_mask, &mask );
2066 if( !mask )
2068 WARN("mask template is empty\n");
2069 goto end;
2072 info = msi_dialog_parse_groups( mask );
2073 if( !info )
2075 ERR("template %s is invalid\n", debugstr_w(mask));
2076 goto end;
2079 info->dialog = dialog;
2081 control = msi_dialog_add_control( dialog, rec, szStatic,
2082 SS_OWNERDRAW | WS_GROUP | WS_VISIBLE );
2083 if( !control )
2085 ERR("Failed to create maskedit container\n");
2086 ret = ERROR_FUNCTION_FAILED;
2087 goto end;
2089 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
2091 info->hwnd = control->hwnd;
2093 /* subclass the static control */
2094 info->oldproc = (WNDPROC) SetWindowLongPtrW( info->hwnd, GWLP_WNDPROC,
2095 (LONG_PTR)MSIMaskedEdit_WndProc );
2096 SetPropW( control->hwnd, szButtonData, info );
2098 prop = MSI_RecordGetString( rec, 9 );
2099 if( prop )
2100 info->prop = strdupW( prop );
2102 msi_maskedit_create_children( info, font );
2104 if( prop )
2106 val = msi_dup_property( dialog->package->db, prop );
2107 if( val )
2109 msi_maskedit_set_text( info, val );
2110 msi_free( val );
2114 end:
2115 if( ret != ERROR_SUCCESS )
2116 msi_free( info );
2117 msi_free( font_mask );
2118 msi_free( font );
2119 return ret;
2122 /******************** Progress Bar *****************************************/
2124 static UINT msi_dialog_progress_bar( msi_dialog *dialog, MSIRECORD *rec )
2126 msi_control *control;
2127 DWORD attributes, style;
2129 style = WS_VISIBLE;
2130 attributes = MSI_RecordGetInteger( rec, 8 );
2131 if( !(attributes & msidbControlAttributesProgress95) )
2132 style |= PBS_SMOOTH;
2134 control = msi_dialog_add_control( dialog, rec, PROGRESS_CLASSW, style );
2135 if( !control )
2136 return ERROR_FUNCTION_FAILED;
2138 event_subscribe( dialog, szSetProgress, control->name, szProgress );
2139 return ERROR_SUCCESS;
2142 /******************** Path Edit ********************************************/
2144 struct msi_pathedit_info
2146 msi_dialog *dialog;
2147 msi_control *control;
2148 WNDPROC oldproc;
2151 static void msi_dialog_update_pathedit( msi_dialog *dialog, msi_control *control )
2153 LPWSTR prop, path;
2154 BOOL indirect;
2156 if (!control && !(control = msi_dialog_find_control_by_type( dialog, szPathEdit )))
2157 return;
2159 indirect = control->attributes & msidbControlAttributesIndirect;
2160 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2161 path = msi_dialog_dup_property( dialog, prop, TRUE );
2163 SetWindowTextW( control->hwnd, path );
2164 SendMessageW( control->hwnd, EM_SETSEL, 0, -1 );
2166 msi_free( path );
2167 msi_free( prop );
2170 /* FIXME: test when this should fail */
2171 static BOOL msi_dialog_verify_path( LPWSTR path )
2173 if ( !path[0] )
2174 return FALSE;
2176 if ( PathIsRelativeW( path ) )
2177 return FALSE;
2179 return TRUE;
2182 /* returns TRUE if the path is valid, FALSE otherwise */
2183 static BOOL msi_dialog_onkillfocus( msi_dialog *dialog, msi_control *control )
2185 LPWSTR buf, prop;
2186 BOOL indirect;
2187 BOOL valid;
2189 indirect = control->attributes & msidbControlAttributesIndirect;
2190 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2192 buf = msi_get_window_text( control->hwnd );
2194 if ( !msi_dialog_verify_path( buf ) )
2196 /* FIXME: display an error message box */
2197 ERR("Invalid path %s\n", debugstr_w( buf ));
2198 valid = FALSE;
2199 SetFocus( control->hwnd );
2201 else
2203 valid = TRUE;
2204 msi_dialog_set_property( dialog->package, prop, buf );
2207 msi_dialog_update_pathedit( dialog, control );
2209 TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
2210 debugstr_w(prop));
2212 msi_free( buf );
2213 msi_free( prop );
2215 return valid;
2218 static LRESULT WINAPI MSIPathEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2220 struct msi_pathedit_info *info = GetPropW(hWnd, szButtonData);
2221 LRESULT r = 0;
2223 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
2225 if ( msg == WM_KILLFOCUS )
2227 /* if the path is invalid, don't handle this message */
2228 if ( !msi_dialog_onkillfocus( info->dialog, info->control ) )
2229 return 0;
2232 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
2234 if ( msg == WM_NCDESTROY )
2236 msi_free( info );
2237 RemovePropW( hWnd, szButtonData );
2240 return r;
2243 static UINT msi_dialog_pathedit_control( msi_dialog *dialog, MSIRECORD *rec )
2245 struct msi_pathedit_info *info;
2246 msi_control *control;
2247 LPCWSTR prop;
2249 info = msi_alloc( sizeof *info );
2250 if (!info)
2251 return ERROR_FUNCTION_FAILED;
2253 control = msi_dialog_add_control( dialog, rec, szEdit,
2254 WS_BORDER | WS_TABSTOP );
2255 control->attributes = MSI_RecordGetInteger( rec, 8 );
2256 prop = MSI_RecordGetString( rec, 9 );
2257 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2259 info->dialog = dialog;
2260 info->control = control;
2261 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2262 (LONG_PTR)MSIPathEdit_WndProc );
2263 SetPropW( control->hwnd, szButtonData, info );
2265 msi_dialog_update_pathedit( dialog, control );
2267 return ERROR_SUCCESS;
2270 static UINT msi_dialog_radiogroup_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
2272 if (HIWORD(param) != BN_CLICKED)
2273 return ERROR_SUCCESS;
2275 TRACE("clicked radio button %s, set %s\n", debugstr_w(control->name), debugstr_w(control->property));
2277 msi_dialog_set_property( dialog->package, control->property, control->name );
2279 return msi_dialog_button_handler( dialog, control, param );
2282 /* radio buttons are a bit different from normal controls */
2283 static UINT msi_dialog_create_radiobutton( MSIRECORD *rec, LPVOID param )
2285 radio_button_group_descr *group = param;
2286 msi_dialog *dialog = group->dialog;
2287 msi_control *control;
2288 LPCWSTR prop, text, name;
2289 DWORD style, attributes = group->attributes;
2291 style = WS_CHILD | BS_AUTORADIOBUTTON | BS_MULTILINE | WS_TABSTOP;
2292 name = MSI_RecordGetString( rec, 3 );
2293 text = MSI_RecordGetString( rec, 8 );
2294 if( attributes & msidbControlAttributesVisible )
2295 style |= WS_VISIBLE;
2296 if( ~attributes & msidbControlAttributesEnabled )
2297 style |= WS_DISABLED;
2299 control = dialog_create_window( dialog, rec, 0, szButton, name, text, style,
2300 group->parent->hwnd );
2301 if (!control)
2302 return ERROR_FUNCTION_FAILED;
2303 control->handler = msi_dialog_radiogroup_handler;
2305 if (group->propval && !strcmpW( control->name, group->propval ))
2306 SendMessageW(control->hwnd, BM_SETCHECK, BST_CHECKED, 0);
2308 prop = MSI_RecordGetString( rec, 1 );
2309 if( prop )
2310 control->property = strdupW( prop );
2312 return ERROR_SUCCESS;
2315 static BOOL CALLBACK msi_radioground_child_enum( HWND hWnd, LPARAM lParam )
2317 EnableWindow( hWnd, lParam );
2318 return TRUE;
2321 static LRESULT WINAPI MSIRadioGroup_WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
2323 WNDPROC oldproc = (WNDPROC)GetPropW( hWnd, szButtonData );
2324 LRESULT r;
2326 TRACE("hWnd %p msg %04x wParam 0x%08lx lParam 0x%08lx\n", hWnd, msg, wParam, lParam);
2328 if (msg == WM_COMMAND) /* Forward notifications to dialog */
2329 SendMessageW( GetParent( hWnd ), msg, wParam, lParam );
2331 r = CallWindowProcW( oldproc, hWnd, msg, wParam, lParam );
2333 /* make sure the radio buttons show as disabled if the parent is disabled */
2334 if (msg == WM_ENABLE)
2335 EnumChildWindows( hWnd, msi_radioground_child_enum, wParam );
2337 return r;
2340 static UINT msi_dialog_radiogroup_control( msi_dialog *dialog, MSIRECORD *rec )
2342 static const WCHAR query[] = {
2343 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2344 'R','a','d','i','o','B','u','t','t','o','n',' ','W','H','E','R','E',' ',
2345 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
2346 UINT r;
2347 LPCWSTR prop;
2348 msi_control *control;
2349 MSIQUERY *view;
2350 radio_button_group_descr group;
2351 MSIPACKAGE *package = dialog->package;
2352 WNDPROC oldproc;
2353 DWORD attr, style = WS_GROUP;
2355 prop = MSI_RecordGetString( rec, 9 );
2357 TRACE("%p %p %s\n", dialog, rec, debugstr_w( prop ));
2359 attr = MSI_RecordGetInteger( rec, 8 );
2360 if (attr & msidbControlAttributesHasBorder)
2361 style |= BS_GROUPBOX;
2362 else
2363 style |= BS_OWNERDRAW;
2365 /* Create parent group box to hold radio buttons */
2366 control = msi_dialog_add_control( dialog, rec, szButton, style );
2367 if( !control )
2368 return ERROR_FUNCTION_FAILED;
2370 oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2371 (LONG_PTR)MSIRadioGroup_WndProc );
2372 SetPropW(control->hwnd, szButtonData, oldproc);
2373 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
2375 if( prop )
2376 control->property = strdupW( prop );
2378 /* query the Radio Button table for all control in this group */
2379 r = MSI_OpenQuery( package->db, &view, query, prop );
2380 if( r != ERROR_SUCCESS )
2382 ERR("query failed for dialog %s radio group %s\n",
2383 debugstr_w(dialog->name), debugstr_w(prop));
2384 return ERROR_INVALID_PARAMETER;
2387 group.dialog = dialog;
2388 group.parent = control;
2389 group.attributes = MSI_RecordGetInteger( rec, 8 );
2390 group.propval = msi_dup_property( dialog->package->db, control->property );
2392 r = MSI_IterateRecords( view, 0, msi_dialog_create_radiobutton, &group );
2393 msiobj_release( &view->hdr );
2394 msi_free( group.propval );
2395 return r;
2398 static void
2399 msi_seltree_sync_item_state( HWND hwnd, MSIFEATURE *feature, HTREEITEM hItem )
2401 TVITEMW tvi;
2402 DWORD index = feature->ActionRequest;
2404 TRACE("Feature %s -> %d %d %d\n", debugstr_w(feature->Title),
2405 feature->Installed, feature->Action, feature->ActionRequest);
2407 if (index == INSTALLSTATE_UNKNOWN)
2408 index = INSTALLSTATE_ABSENT;
2410 tvi.mask = TVIF_STATE;
2411 tvi.hItem = hItem;
2412 tvi.state = INDEXTOSTATEIMAGEMASK( index );
2413 tvi.stateMask = TVIS_STATEIMAGEMASK;
2415 SendMessageW( hwnd, TVM_SETITEMW, 0, (LPARAM) &tvi );
2418 static UINT
2419 msi_seltree_popup_menu( HWND hwnd, INT x, INT y )
2421 HMENU hMenu;
2422 INT r;
2424 /* create a menu to display */
2425 hMenu = CreatePopupMenu();
2427 /* FIXME: load strings from resources */
2428 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_LOCAL, "Install feature locally");
2429 AppendMenuA( hMenu, MF_ENABLED, USER_INSTALLSTATE_ALL, "Install entire feature");
2430 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ADVERTISED, "Install on demand");
2431 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ABSENT, "Don't install");
2432 r = TrackPopupMenu( hMenu, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RETURNCMD,
2433 x, y, 0, hwnd, NULL );
2434 DestroyMenu( hMenu );
2435 return r;
2438 static void
2439 msi_seltree_update_feature_installstate( HWND hwnd, HTREEITEM hItem,
2440 MSIPACKAGE *package, MSIFEATURE *feature, INSTALLSTATE state )
2442 feature->ActionRequest = state;
2443 msi_seltree_sync_item_state( hwnd, feature, hItem );
2444 ACTION_UpdateComponentStates( package, feature );
2447 static void
2448 msi_seltree_update_siblings_and_children_installstate( HWND hwnd, HTREEITEM curr,
2449 MSIPACKAGE *package, INSTALLSTATE state)
2451 /* update all siblings */
2454 MSIFEATURE *feature;
2455 HTREEITEM child;
2457 feature = msi_seltree_feature_from_item( hwnd, curr );
2458 msi_seltree_update_feature_installstate( hwnd, curr, package, feature, state );
2460 /* update this sibling's children */
2461 child = (HTREEITEM)SendMessageW( hwnd, TVM_GETNEXTITEM, (WPARAM)TVGN_CHILD, (LPARAM)curr );
2462 if (child)
2463 msi_seltree_update_siblings_and_children_installstate( hwnd, child,
2464 package, state );
2466 while ((curr = (HTREEITEM)SendMessageW( hwnd, TVM_GETNEXTITEM, (WPARAM)TVGN_NEXT, (LPARAM)curr )));
2469 static LRESULT
2470 msi_seltree_menu( HWND hwnd, HTREEITEM hItem )
2472 struct msi_selection_tree_info *info;
2473 MSIFEATURE *feature;
2474 MSIPACKAGE *package;
2475 union {
2476 RECT rc;
2477 POINT pt[2];
2478 HTREEITEM hItem;
2479 } u;
2480 UINT r;
2482 info = GetPropW(hwnd, szButtonData);
2483 package = info->dialog->package;
2485 feature = msi_seltree_feature_from_item( hwnd, hItem );
2486 if (!feature)
2488 ERR("item %p feature was NULL\n", hItem);
2489 return 0;
2492 /* get the item's rectangle to put the menu just below it */
2493 u.hItem = hItem;
2494 SendMessageW( hwnd, TVM_GETITEMRECT, 0, (LPARAM) &u.rc );
2495 MapWindowPoints( hwnd, NULL, u.pt, 2 );
2497 r = msi_seltree_popup_menu( hwnd, u.rc.left, u.rc.top );
2499 switch (r)
2501 case USER_INSTALLSTATE_ALL:
2502 r = INSTALLSTATE_LOCAL;
2503 /* fall-through */
2504 case INSTALLSTATE_ADVERTISED:
2505 case INSTALLSTATE_ABSENT:
2507 HTREEITEM child;
2508 child = (HTREEITEM)SendMessageW( hwnd, TVM_GETNEXTITEM, (WPARAM)TVGN_CHILD, (LPARAM)hItem );
2509 if (child)
2510 msi_seltree_update_siblings_and_children_installstate( hwnd, child, package, r );
2512 /* fall-through */
2513 case INSTALLSTATE_LOCAL:
2514 msi_seltree_update_feature_installstate( hwnd, hItem, package, feature, r );
2515 break;
2518 return 0;
2521 static LRESULT WINAPI
2522 MSISelectionTree_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2524 struct msi_selection_tree_info *info;
2525 TVHITTESTINFO tvhti;
2526 HRESULT r;
2528 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
2530 info = GetPropW(hWnd, szButtonData);
2532 switch( msg )
2534 case WM_LBUTTONDOWN:
2535 tvhti.pt.x = (short)LOWORD( lParam );
2536 tvhti.pt.y = (short)HIWORD( lParam );
2537 tvhti.flags = 0;
2538 tvhti.hItem = 0;
2539 CallWindowProcW(info->oldproc, hWnd, TVM_HITTEST, 0, (LPARAM) &tvhti );
2540 if (tvhti.flags & TVHT_ONITEMSTATEICON)
2541 return msi_seltree_menu( hWnd, tvhti.hItem );
2542 break;
2544 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
2546 switch( msg )
2548 case WM_NCDESTROY:
2549 msi_free( info );
2550 RemovePropW( hWnd, szButtonData );
2551 break;
2553 return r;
2556 static void
2557 msi_seltree_add_child_features( MSIPACKAGE *package, HWND hwnd,
2558 LPCWSTR parent, HTREEITEM hParent )
2560 struct msi_selection_tree_info *info = GetPropW( hwnd, szButtonData );
2561 MSIFEATURE *feature;
2562 TVINSERTSTRUCTW tvis;
2563 HTREEITEM hitem, hfirst = NULL;
2565 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
2567 if ( parent && feature->Feature_Parent && strcmpW( parent, feature->Feature_Parent ))
2568 continue;
2569 else if ( parent && !feature->Feature_Parent )
2570 continue;
2571 else if ( !parent && feature->Feature_Parent )
2572 continue;
2574 if ( !feature->Title )
2575 continue;
2577 if ( !feature->Display )
2578 continue;
2580 memset( &tvis, 0, sizeof tvis );
2581 tvis.hParent = hParent;
2582 tvis.hInsertAfter = TVI_LAST;
2583 tvis.u.item.mask = TVIF_TEXT | TVIF_PARAM;
2584 tvis.u.item.pszText = feature->Title;
2585 tvis.u.item.lParam = (LPARAM) feature;
2587 hitem = (HTREEITEM) SendMessageW( hwnd, TVM_INSERTITEMW, 0, (LPARAM) &tvis );
2588 if (!hitem)
2589 continue;
2591 if (!hfirst)
2592 hfirst = hitem;
2594 msi_seltree_sync_item_state( hwnd, feature, hitem );
2595 msi_seltree_add_child_features( package, hwnd,
2596 feature->Feature, hitem );
2598 /* the node is expanded if Display is odd */
2599 if ( feature->Display % 2 != 0 )
2600 SendMessageW( hwnd, TVM_EXPAND, TVE_EXPAND, (LPARAM) hitem );
2603 /* select the first item */
2604 SendMessageW( hwnd, TVM_SELECTITEM, TVGN_CARET | TVGN_DROPHILITE, (LPARAM) hfirst );
2605 info->selected = hfirst;
2608 static void msi_seltree_create_imagelist( HWND hwnd )
2610 const int bm_width = 32, bm_height = 16, bm_count = 3;
2611 const int bm_resource = 0x1001;
2612 HIMAGELIST himl;
2613 int i;
2614 HBITMAP hbmp;
2616 himl = ImageList_Create( bm_width, bm_height, FALSE, 4, 0 );
2617 if (!himl)
2619 ERR("failed to create image list\n");
2620 return;
2623 for (i=0; i<bm_count; i++)
2625 hbmp = LoadBitmapW( msi_hInstance, MAKEINTRESOURCEW(i+bm_resource) );
2626 if (!hbmp)
2628 ERR("failed to load bitmap %d\n", i);
2629 break;
2633 * Add a dummy bitmap at offset zero because the treeview
2634 * can't use it as a state mask (zero means no user state).
2636 if (!i)
2637 ImageList_Add( himl, hbmp, NULL );
2639 ImageList_Add( himl, hbmp, NULL );
2642 SendMessageW( hwnd, TVM_SETIMAGELIST, TVSIL_STATE, (LPARAM)himl );
2645 static UINT msi_dialog_seltree_handler( msi_dialog *dialog,
2646 msi_control *control, WPARAM param )
2648 struct msi_selection_tree_info *info = GetPropW( control->hwnd, szButtonData );
2649 LPNMTREEVIEWW tv = (LPNMTREEVIEWW)param;
2650 MSIRECORD *row, *rec;
2651 MSIFOLDER *folder;
2652 MSIFEATURE *feature;
2653 LPCWSTR dir, title = NULL;
2654 UINT r = ERROR_SUCCESS;
2656 static const WCHAR select[] = {
2657 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2658 '`','F','e','a','t','u','r','e','`',' ','W','H','E','R','E',' ',
2659 '`','T','i','t','l','e','`',' ','=',' ','\'','%','s','\'',0
2662 if (tv->hdr.code != TVN_SELCHANGINGW)
2663 return ERROR_SUCCESS;
2665 info->selected = tv->itemNew.hItem;
2667 if (!(tv->itemNew.mask & TVIF_TEXT))
2669 feature = msi_seltree_feature_from_item( control->hwnd, tv->itemNew.hItem );
2670 if (feature)
2671 title = feature->Title;
2673 else
2674 title = tv->itemNew.pszText;
2676 row = MSI_QueryGetRecord( dialog->package->db, select, title );
2677 if (!row)
2678 return ERROR_FUNCTION_FAILED;
2680 rec = MSI_CreateRecord( 1 );
2682 MSI_RecordSetStringW( rec, 1, MSI_RecordGetString( row, 4 ) );
2683 msi_event_fire( dialog->package, szSelectionDescription, rec );
2685 dir = MSI_RecordGetString( row, 7 );
2686 if (dir)
2688 folder = msi_get_loaded_folder( dialog->package, dir );
2689 if (!folder)
2691 r = ERROR_FUNCTION_FAILED;
2692 goto done;
2694 MSI_RecordSetStringW( rec, 1, folder->ResolvedTarget );
2696 else
2697 MSI_RecordSetStringW( rec, 1, NULL );
2699 msi_event_fire( dialog->package, szSelectionPath, rec );
2701 done:
2702 msiobj_release(&row->hdr);
2703 msiobj_release(&rec->hdr);
2705 return r;
2708 static UINT msi_dialog_selection_tree( msi_dialog *dialog, MSIRECORD *rec )
2710 msi_control *control;
2711 LPCWSTR prop, control_name;
2712 MSIPACKAGE *package = dialog->package;
2713 DWORD style;
2714 struct msi_selection_tree_info *info;
2716 info = msi_alloc( sizeof *info );
2717 if (!info)
2718 return ERROR_FUNCTION_FAILED;
2720 /* create the treeview control */
2721 style = TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT;
2722 style |= WS_GROUP | WS_VSCROLL;
2723 control = msi_dialog_add_control( dialog, rec, WC_TREEVIEWW, style );
2724 if (!control)
2726 msi_free(info);
2727 return ERROR_FUNCTION_FAILED;
2730 control->handler = msi_dialog_seltree_handler;
2731 control_name = MSI_RecordGetString( rec, 2 );
2732 control->attributes = MSI_RecordGetInteger( rec, 8 );
2733 prop = MSI_RecordGetString( rec, 9 );
2734 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2736 /* subclass */
2737 info->dialog = dialog;
2738 info->hwnd = control->hwnd;
2739 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2740 (LONG_PTR)MSISelectionTree_WndProc );
2741 SetPropW( control->hwnd, szButtonData, info );
2743 event_subscribe( dialog, szSelectionPath, control_name, szProperty );
2745 /* initialize it */
2746 msi_seltree_create_imagelist( control->hwnd );
2747 msi_seltree_add_child_features( package, control->hwnd, NULL, NULL );
2749 return ERROR_SUCCESS;
2752 /******************** Group Box ***************************************/
2754 static UINT msi_dialog_group_box( msi_dialog *dialog, MSIRECORD *rec )
2756 msi_control *control;
2757 DWORD style;
2759 style = BS_GROUPBOX | WS_CHILD | WS_GROUP;
2760 control = msi_dialog_add_control( dialog, rec, WC_BUTTONW, style );
2761 if (!control)
2762 return ERROR_FUNCTION_FAILED;
2764 return ERROR_SUCCESS;
2767 /******************** List Box ***************************************/
2769 struct msi_listbox_info
2771 msi_dialog *dialog;
2772 HWND hwnd;
2773 WNDPROC oldproc;
2774 DWORD num_items;
2775 DWORD addpos_items;
2776 LPWSTR *items;
2779 static LRESULT WINAPI MSIListBox_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2781 struct msi_listbox_info *info;
2782 LRESULT r;
2783 DWORD j;
2785 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
2787 info = GetPropW( hWnd, szButtonData );
2788 if (!info)
2789 return 0;
2791 r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
2793 switch( msg )
2795 case WM_NCDESTROY:
2796 for (j = 0; j < info->num_items; j++)
2797 msi_free( info->items[j] );
2798 msi_free( info->items );
2799 msi_free( info );
2800 RemovePropW( hWnd, szButtonData );
2801 break;
2804 return r;
2807 static UINT msi_listbox_add_item( MSIRECORD *rec, LPVOID param )
2809 struct msi_listbox_info *info = param;
2810 LPCWSTR value, text;
2811 int pos;
2813 value = MSI_RecordGetString( rec, 3 );
2814 text = MSI_RecordGetString( rec, 4 );
2816 info->items[info->addpos_items] = strdupW( value );
2818 pos = SendMessageW( info->hwnd, LB_ADDSTRING, 0, (LPARAM)text );
2819 SendMessageW( info->hwnd, LB_SETITEMDATA, pos, (LPARAM)info->items[info->addpos_items] );
2820 info->addpos_items++;
2821 return ERROR_SUCCESS;
2824 static UINT msi_listbox_add_items( struct msi_listbox_info *info, LPCWSTR property )
2826 static const WCHAR query[] = {
2827 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2828 '`','L','i','s','t','B','o','x','`',' ','W','H','E','R','E',' ',
2829 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',' ',
2830 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','`',0};
2831 MSIQUERY *view;
2832 DWORD count;
2833 UINT r;
2835 r = MSI_OpenQuery( info->dialog->package->db, &view, query, property );
2836 if ( r != ERROR_SUCCESS )
2837 return r;
2839 /* just get the number of records */
2840 count = 0;
2841 r = MSI_IterateRecords( view, &count, NULL, NULL );
2842 if (r != ERROR_SUCCESS)
2844 msiobj_release( &view->hdr );
2845 return r;
2847 info->num_items = count;
2848 info->items = msi_alloc( sizeof(*info->items) * count );
2850 r = MSI_IterateRecords( view, NULL, msi_listbox_add_item, info );
2851 msiobj_release( &view->hdr );
2852 return r;
2855 static UINT msi_dialog_listbox_handler( msi_dialog *dialog,
2856 msi_control *control, WPARAM param )
2858 struct msi_listbox_info *info;
2859 int index;
2860 LPCWSTR value;
2862 if( HIWORD(param) != LBN_SELCHANGE )
2863 return ERROR_SUCCESS;
2865 info = GetPropW( control->hwnd, szButtonData );
2866 index = SendMessageW( control->hwnd, LB_GETCURSEL, 0, 0 );
2867 value = (LPCWSTR) SendMessageW( control->hwnd, LB_GETITEMDATA, index, 0 );
2869 msi_dialog_set_property( info->dialog->package, control->property, value );
2870 msi_dialog_evaluate_control_conditions( info->dialog );
2872 return ERROR_SUCCESS;
2875 static UINT msi_dialog_list_box( msi_dialog *dialog, MSIRECORD *rec )
2877 struct msi_listbox_info *info;
2878 msi_control *control;
2879 DWORD attributes, style;
2880 LPCWSTR prop;
2882 info = msi_alloc( sizeof *info );
2883 if (!info)
2884 return ERROR_FUNCTION_FAILED;
2886 style = WS_TABSTOP | WS_GROUP | WS_CHILD | LBS_NOTIFY | WS_VSCROLL | WS_BORDER;
2887 attributes = MSI_RecordGetInteger( rec, 8 );
2888 if (~attributes & msidbControlAttributesSorted)
2889 style |= LBS_SORT;
2891 control = msi_dialog_add_control( dialog, rec, WC_LISTBOXW, style );
2892 if (!control)
2894 msi_free(info);
2895 return ERROR_FUNCTION_FAILED;
2898 control->handler = msi_dialog_listbox_handler;
2900 prop = MSI_RecordGetString( rec, 9 );
2901 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2903 /* subclass */
2904 info->dialog = dialog;
2905 info->hwnd = control->hwnd;
2906 info->items = NULL;
2907 info->addpos_items = 0;
2908 info->oldproc = (WNDPROC)SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2909 (LONG_PTR)MSIListBox_WndProc );
2910 SetPropW( control->hwnd, szButtonData, info );
2912 if ( control->property )
2913 msi_listbox_add_items( info, control->property );
2915 return ERROR_SUCCESS;
2918 /******************** Directory Combo ***************************************/
2920 static void msi_dialog_update_directory_combo( msi_dialog *dialog, msi_control *control )
2922 LPWSTR prop, path;
2923 BOOL indirect;
2925 if (!control && !(control = msi_dialog_find_control_by_type( dialog, szDirectoryCombo )))
2926 return;
2928 indirect = control->attributes & msidbControlAttributesIndirect;
2929 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2930 path = msi_dialog_dup_property( dialog, prop, TRUE );
2932 PathStripPathW( path );
2933 PathRemoveBackslashW( path );
2935 SendMessageW( control->hwnd, CB_INSERTSTRING, 0, (LPARAM)path );
2936 SendMessageW( control->hwnd, CB_SETCURSEL, 0, 0 );
2938 msi_free( path );
2939 msi_free( prop );
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 WCHAR dir_spec[MAX_PATH];
2969 WIN32_FIND_DATAW wfd;
2970 LPWSTR prop, path;
2971 BOOL indirect;
2972 LVITEMW item;
2973 HANDLE file;
2975 static const WCHAR asterisk[] = {'*',0};
2977 if (!control && !(control = msi_dialog_find_control_by_type( dialog, szDirectoryList )))
2978 return;
2980 /* clear the list-view */
2981 SendMessageW( control->hwnd, LVM_DELETEALLITEMS, 0, 0 );
2983 indirect = control->attributes & msidbControlAttributesIndirect;
2984 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2985 path = msi_dialog_dup_property( dialog, prop, TRUE );
2987 lstrcpyW( dir_spec, path );
2988 lstrcatW( dir_spec, asterisk );
2990 file = FindFirstFileW( dir_spec, &wfd );
2991 if ( file == INVALID_HANDLE_VALUE )
2992 return;
2996 if ( wfd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY )
2997 continue;
2999 if ( !strcmpW( wfd.cFileName, szDot ) || !strcmpW( wfd.cFileName, szDotDot ) )
3000 continue;
3002 item.mask = LVIF_TEXT;
3003 item.cchTextMax = MAX_PATH;
3004 item.iItem = 0;
3005 item.iSubItem = 0;
3006 item.pszText = wfd.cFileName;
3008 SendMessageW( control->hwnd, LVM_INSERTITEMW, 0, (LPARAM)&item );
3009 } while ( FindNextFileW( file, &wfd ) );
3011 msi_free( prop );
3012 msi_free( path );
3013 FindClose( file );
3016 static UINT msi_dialog_directorylist_up( msi_dialog *dialog )
3018 msi_control *control;
3019 LPWSTR prop, path, ptr;
3020 BOOL indirect;
3022 control = msi_dialog_find_control_by_type( dialog, szDirectoryList );
3023 indirect = control->attributes & msidbControlAttributesIndirect;
3024 prop = msi_dialog_dup_property( dialog, control->property, indirect );
3025 path = msi_dialog_dup_property( dialog, prop, TRUE );
3027 /* strip off the last directory */
3028 ptr = PathFindFileNameW( path );
3029 if (ptr != path) *(ptr - 1) = '\0';
3030 PathAddBackslashW( path );
3032 msi_dialog_set_property( dialog->package, prop, path );
3034 msi_dialog_update_directory_list( dialog, NULL );
3035 msi_dialog_update_directory_combo( dialog, NULL );
3036 msi_dialog_update_pathedit( dialog, NULL );
3038 msi_free( path );
3039 msi_free( prop );
3041 return ERROR_SUCCESS;
3044 static UINT msi_dialog_dirlist_handler( msi_dialog *dialog,
3045 msi_control *control, WPARAM param )
3047 LPNMHDR nmhdr = (LPNMHDR)param;
3048 WCHAR new_path[MAX_PATH];
3049 WCHAR text[MAX_PATH];
3050 LPWSTR path, prop;
3051 BOOL indirect;
3052 LVITEMW item;
3053 int index;
3055 if (nmhdr->code != LVN_ITEMACTIVATE)
3056 return ERROR_SUCCESS;
3058 index = SendMessageW( control->hwnd, LVM_GETNEXTITEM, -1, LVNI_SELECTED );
3059 if ( index < 0 )
3061 ERR("No list-view item selected!\n");
3062 return ERROR_FUNCTION_FAILED;
3065 item.iSubItem = 0;
3066 item.pszText = text;
3067 item.cchTextMax = MAX_PATH;
3068 SendMessageW( control->hwnd, LVM_GETITEMTEXTW, index, (LPARAM)&item );
3070 indirect = control->attributes & msidbControlAttributesIndirect;
3071 prop = msi_dialog_dup_property( dialog, control->property, indirect );
3072 path = msi_dialog_dup_property( dialog, prop, TRUE );
3074 lstrcpyW( new_path, path );
3075 lstrcatW( new_path, text );
3076 lstrcatW( new_path, szBackSlash );
3078 msi_dialog_set_property( dialog->package, prop, new_path );
3080 msi_dialog_update_directory_list( dialog, NULL );
3081 msi_dialog_update_directory_combo( dialog, NULL );
3082 msi_dialog_update_pathedit( dialog, NULL );
3084 msi_free( prop );
3085 msi_free( path );
3086 return ERROR_SUCCESS;
3089 static UINT msi_dialog_directory_list( msi_dialog *dialog, MSIRECORD *rec )
3091 msi_control *control;
3092 LPCWSTR prop;
3093 DWORD style;
3095 style = LVS_LIST | WS_VSCROLL | LVS_SHAREIMAGELISTS |
3096 LVS_AUTOARRANGE | LVS_SINGLESEL | WS_BORDER |
3097 LVS_SORTASCENDING | WS_CHILD | WS_GROUP | WS_TABSTOP;
3098 control = msi_dialog_add_control( dialog, rec, WC_LISTVIEWW, style );
3099 if (!control)
3100 return ERROR_FUNCTION_FAILED;
3102 control->attributes = MSI_RecordGetInteger( rec, 8 );
3103 control->handler = msi_dialog_dirlist_handler;
3104 prop = MSI_RecordGetString( rec, 9 );
3105 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
3107 /* double click to activate an item in the list */
3108 SendMessageW( control->hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE,
3109 0, LVS_EX_TWOCLICKACTIVATE );
3111 msi_dialog_update_directory_list( dialog, control );
3113 return ERROR_SUCCESS;
3116 /******************** VolumeCost List ***************************************/
3118 static BOOL str_is_number( LPCWSTR str )
3120 int i;
3122 for (i = 0; i < lstrlenW( str ); i++)
3123 if (!isdigitW(str[i]))
3124 return FALSE;
3126 return TRUE;
3129 static const WCHAR column_keys[][80] =
3131 {'V','o','l','u','m','e','C','o','s','t','V','o','l','u','m','e',0},
3132 {'V','o','l','u','m','e','C','o','s','t','S','i','z','e',0},
3133 {'V','o','l','u','m','e','C','o','s','t','A','v','a','i','l','a','b','l','e',0},
3134 {'V','o','l','u','m','e','C','o','s','t','R','e','q','u','i','r','e','d',0},
3135 {'V','o','l','u','m','e','C','o','s','t','D','i','f','f','e','r','e','n','c','e',0}
3138 static void msi_dialog_vcl_add_columns( msi_dialog *dialog, msi_control *control, MSIRECORD *rec )
3140 LPCWSTR text = MSI_RecordGetString( rec, 10 );
3141 LPCWSTR begin = text, end;
3142 WCHAR *num;
3143 LVCOLUMNW lvc;
3144 DWORD count = 0;
3146 static const WCHAR negative[] = {'-',0};
3148 if (!text) return;
3150 while ((begin = strchrW( begin, '{' )) && count < 5)
3152 if (!(end = strchrW( begin, '}' )))
3153 return;
3155 num = msi_alloc( (end-begin+1)*sizeof(WCHAR) );
3156 if (!num)
3157 return;
3159 lstrcpynW( num, begin + 1, end - begin );
3160 begin += end - begin + 1;
3162 /* empty braces or '0' hides the column */
3163 if ( !num[0] || !strcmpW( num, szZero ) )
3165 count++;
3166 msi_free( num );
3167 continue;
3170 /* the width must be a positive number
3171 * if a width is invalid, all remaining columns are hidden
3173 if ( !strncmpW( num, negative, 1 ) || !str_is_number( num ) ) {
3174 msi_free( num );
3175 return;
3178 ZeroMemory( &lvc, sizeof(lvc) );
3179 lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
3180 lvc.cx = atolW( num );
3181 lvc.pszText = msi_dialog_get_uitext( dialog, column_keys[count] );
3183 SendMessageW( control->hwnd, LVM_INSERTCOLUMNW, count++, (LPARAM)&lvc );
3184 msi_free( lvc.pszText );
3185 msi_free( num );
3189 static LONGLONG msi_vcl_get_cost( msi_dialog *dialog )
3191 MSIFEATURE *feature;
3192 INT each_cost;
3193 LONGLONG total_cost = 0;
3195 LIST_FOR_EACH_ENTRY( feature, &dialog->package->features, MSIFEATURE, entry )
3197 if (ERROR_SUCCESS == (MSI_GetFeatureCost(dialog->package, feature,
3198 MSICOSTTREE_SELFONLY, INSTALLSTATE_LOCAL, &each_cost)))
3200 /* each_cost is in 512-byte units */
3201 total_cost += each_cost * 512;
3203 if (ERROR_SUCCESS == (MSI_GetFeatureCost(dialog->package, feature,
3204 MSICOSTTREE_SELFONLY, INSTALLSTATE_ABSENT, &each_cost)))
3206 /* each_cost is in 512-byte units */
3207 total_cost -= each_cost * 512;
3210 return total_cost;
3213 static void msi_dialog_vcl_add_drives( msi_dialog *dialog, msi_control *control )
3215 ULARGE_INTEGER total, free;
3216 LONGLONG difference, cost;
3217 WCHAR size_text[MAX_PATH];
3218 WCHAR cost_text[MAX_PATH];
3219 LPWSTR drives, ptr;
3220 LVITEMW lvitem;
3221 DWORD size;
3222 int i = 0;
3224 cost = msi_vcl_get_cost(dialog);
3225 StrFormatByteSizeW(cost, cost_text, MAX_PATH);
3227 size = GetLogicalDriveStringsW( 0, NULL );
3228 if ( !size ) return;
3230 drives = msi_alloc( (size + 1) * sizeof(WCHAR) );
3231 if ( !drives ) return;
3233 GetLogicalDriveStringsW( size, drives );
3235 ptr = drives;
3236 while (*ptr)
3238 lvitem.mask = LVIF_TEXT;
3239 lvitem.iItem = i;
3240 lvitem.iSubItem = 0;
3241 lvitem.pszText = ptr;
3242 lvitem.cchTextMax = lstrlenW(ptr) + 1;
3243 SendMessageW( control->hwnd, LVM_INSERTITEMW, 0, (LPARAM)&lvitem );
3245 GetDiskFreeSpaceExW(ptr, &free, &total, NULL);
3246 difference = free.QuadPart - cost;
3248 StrFormatByteSizeW(total.QuadPart, size_text, MAX_PATH);
3249 lvitem.iSubItem = 1;
3250 lvitem.pszText = size_text;
3251 lvitem.cchTextMax = lstrlenW(size_text) + 1;
3252 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
3254 StrFormatByteSizeW(free.QuadPart, size_text, MAX_PATH);
3255 lvitem.iSubItem = 2;
3256 lvitem.pszText = size_text;
3257 lvitem.cchTextMax = lstrlenW(size_text) + 1;
3258 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
3260 lvitem.iSubItem = 3;
3261 lvitem.pszText = cost_text;
3262 lvitem.cchTextMax = lstrlenW(cost_text) + 1;
3263 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
3265 StrFormatByteSizeW(difference, size_text, MAX_PATH);
3266 lvitem.iSubItem = 4;
3267 lvitem.pszText = size_text;
3268 lvitem.cchTextMax = lstrlenW(size_text) + 1;
3269 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
3271 ptr += lstrlenW(ptr) + 1;
3272 i++;
3275 msi_free( drives );
3278 static UINT msi_dialog_volumecost_list( msi_dialog *dialog, MSIRECORD *rec )
3280 msi_control *control;
3281 DWORD style;
3283 style = LVS_REPORT | WS_VSCROLL | WS_HSCROLL | LVS_SHAREIMAGELISTS |
3284 LVS_AUTOARRANGE | LVS_SINGLESEL | WS_BORDER |
3285 WS_CHILD | WS_TABSTOP | WS_GROUP;
3286 control = msi_dialog_add_control( dialog, rec, WC_LISTVIEWW, style );
3287 if (!control)
3288 return ERROR_FUNCTION_FAILED;
3290 msi_dialog_vcl_add_columns( dialog, control, rec );
3291 msi_dialog_vcl_add_drives( dialog, control );
3293 return ERROR_SUCCESS;
3296 /******************** VolumeSelect Combo ***************************************/
3298 static UINT msi_dialog_volsel_handler( msi_dialog *dialog,
3299 msi_control *control, WPARAM param )
3301 WCHAR text[MAX_PATH];
3302 LPWSTR prop;
3303 BOOL indirect;
3304 int index;
3306 if (HIWORD(param) != CBN_SELCHANGE)
3307 return ERROR_SUCCESS;
3309 index = SendMessageW( control->hwnd, CB_GETCURSEL, 0, 0 );
3310 if ( index == CB_ERR )
3312 ERR("No ComboBox item selected!\n");
3313 return ERROR_FUNCTION_FAILED;
3316 SendMessageW( control->hwnd, CB_GETLBTEXT, index, (LPARAM)text );
3318 indirect = control->attributes & msidbControlAttributesIndirect;
3319 prop = msi_dialog_dup_property( dialog, control->property, indirect );
3321 msi_dialog_set_property( dialog->package, prop, text );
3323 msi_free( prop );
3324 return ERROR_SUCCESS;
3327 static void msi_dialog_vsc_add_drives( msi_dialog *dialog, msi_control *control )
3329 LPWSTR drives, ptr;
3330 DWORD size;
3332 size = GetLogicalDriveStringsW( 0, NULL );
3333 if ( !size ) return;
3335 drives = msi_alloc( (size + 1) * sizeof(WCHAR) );
3336 if ( !drives ) return;
3338 GetLogicalDriveStringsW( size, drives );
3340 ptr = drives;
3341 while (*ptr)
3343 SendMessageW( control->hwnd, CB_ADDSTRING, 0, (LPARAM)ptr );
3344 ptr += lstrlenW(ptr) + 1;
3347 msi_free( drives );
3350 static UINT msi_dialog_volumeselect_combo( msi_dialog *dialog, MSIRECORD *rec )
3352 msi_control *control;
3353 LPCWSTR prop;
3354 DWORD style;
3356 /* FIXME: CBS_OWNERDRAWFIXED */
3357 style = WS_CHILD | WS_VISIBLE | WS_GROUP | WS_TABSTOP |
3358 CBS_DROPDOWNLIST | CBS_SORT | CBS_HASSTRINGS |
3359 WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
3360 control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
3361 if (!control)
3362 return ERROR_FUNCTION_FAILED;
3364 control->attributes = MSI_RecordGetInteger( rec, 8 );
3365 control->handler = msi_dialog_volsel_handler;
3366 prop = MSI_RecordGetString( rec, 9 );
3367 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
3369 msi_dialog_vsc_add_drives( dialog, control );
3371 return ERROR_SUCCESS;
3374 static UINT msi_dialog_hyperlink_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
3376 static const WCHAR hrefW[] = {'h','r','e','f'};
3377 static const WCHAR openW[] = {'o','p','e','n',0};
3378 int len, len_href = sizeof(hrefW) / sizeof(hrefW[0]);
3379 const WCHAR *p, *q;
3380 WCHAR quote = 0;
3381 LITEM item;
3383 item.mask = LIF_ITEMINDEX | LIF_URL;
3384 item.iLink = 0;
3385 item.szUrl[0] = 0;
3387 SendMessageW( control->hwnd, LM_GETITEM, 0, (LPARAM)&item );
3389 p = item.szUrl;
3390 while (*p && *p != '<') p++;
3391 if (!*p++) return ERROR_SUCCESS;
3392 if (toupperW( *p++ ) != 'A' || !isspaceW( *p++ )) return ERROR_SUCCESS;
3393 while (*p && isspaceW( *p )) p++;
3395 len = strlenW( p );
3396 if (len > len_href && !memicmpW( p, hrefW, len_href ))
3398 p += len_href;
3399 while (*p && isspaceW( *p )) p++;
3400 if (!*p || *p++ != '=') return ERROR_SUCCESS;
3401 while (*p && isspaceW( *p )) p++;
3403 if (*p == '\"' || *p == '\'') quote = *p++;
3404 q = p;
3405 if (quote)
3407 while (*q && *q != quote) q++;
3408 if (*q != quote) return ERROR_SUCCESS;
3410 else
3412 while (*q && *q != '>' && !isspaceW( *q )) q++;
3413 if (!*q) return ERROR_SUCCESS;
3415 item.szUrl[q - item.szUrl] = 0;
3416 ShellExecuteW( NULL, openW, p, NULL, NULL, SW_SHOWNORMAL );
3418 return ERROR_SUCCESS;
3421 static UINT msi_dialog_hyperlink( msi_dialog *dialog, MSIRECORD *rec )
3423 msi_control *control;
3424 DWORD style = WS_CHILD | WS_TABSTOP | WS_GROUP;
3425 const WCHAR *text = MSI_RecordGetString( rec, 10 );
3426 int len = strlenW( text );
3427 LITEM item;
3429 control = msi_dialog_add_control( dialog, rec, WC_LINK, style );
3430 if (!control)
3431 return ERROR_FUNCTION_FAILED;
3433 control->attributes = MSI_RecordGetInteger( rec, 8 );
3434 control->handler = msi_dialog_hyperlink_handler;
3436 item.mask = LIF_ITEMINDEX | LIF_STATE | LIF_URL;
3437 item.iLink = 0;
3438 item.state = LIS_ENABLED;
3439 item.stateMask = LIS_ENABLED;
3440 if (len < L_MAX_URL_LENGTH) strcpyW( item.szUrl, text );
3441 else item.szUrl[0] = 0;
3443 SendMessageW( control->hwnd, LM_SETITEM, 0, (LPARAM)&item );
3445 return ERROR_SUCCESS;
3448 static const struct control_handler msi_dialog_handler[] =
3450 { szText, msi_dialog_text_control },
3451 { szPushButton, msi_dialog_button_control },
3452 { szLine, msi_dialog_line_control },
3453 { szBitmap, msi_dialog_bitmap_control },
3454 { szCheckBox, msi_dialog_checkbox_control },
3455 { szScrollableText, msi_dialog_scrolltext_control },
3456 { szComboBox, msi_dialog_combo_control },
3457 { szEdit, msi_dialog_edit_control },
3458 { szMaskedEdit, msi_dialog_maskedit_control },
3459 { szPathEdit, msi_dialog_pathedit_control },
3460 { szProgressBar, msi_dialog_progress_bar },
3461 { szRadioButtonGroup, msi_dialog_radiogroup_control },
3462 { szIcon, msi_dialog_icon_control },
3463 { szSelectionTree, msi_dialog_selection_tree },
3464 { szGroupBox, msi_dialog_group_box },
3465 { szListBox, msi_dialog_list_box },
3466 { szDirectoryCombo, msi_dialog_directory_combo },
3467 { szDirectoryList, msi_dialog_directory_list },
3468 { szVolumeCostList, msi_dialog_volumecost_list },
3469 { szVolumeSelectCombo, msi_dialog_volumeselect_combo },
3470 { szHyperLink, msi_dialog_hyperlink }
3473 #define NUM_CONTROL_TYPES (sizeof msi_dialog_handler/sizeof msi_dialog_handler[0])
3475 static UINT msi_dialog_create_controls( MSIRECORD *rec, LPVOID param )
3477 msi_dialog *dialog = param;
3478 LPCWSTR control_type;
3479 UINT i;
3481 /* find and call the function that can create this type of control */
3482 control_type = MSI_RecordGetString( rec, 3 );
3483 for( i=0; i<NUM_CONTROL_TYPES; i++ )
3484 if (!strcmpiW( msi_dialog_handler[i].control_type, control_type ))
3485 break;
3486 if( i != NUM_CONTROL_TYPES )
3487 msi_dialog_handler[i].func( dialog, rec );
3488 else
3489 ERR("no handler for element type %s\n", debugstr_w(control_type));
3491 return ERROR_SUCCESS;
3494 static UINT msi_dialog_fill_controls( msi_dialog *dialog )
3496 static const WCHAR query[] = {
3497 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
3498 'C','o','n','t','r','o','l',' ','W','H','E','R','E',' ',
3499 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
3500 UINT r;
3501 MSIQUERY *view;
3502 MSIPACKAGE *package = dialog->package;
3504 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
3506 /* query the Control table for all the elements of the control */
3507 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
3508 if( r != ERROR_SUCCESS )
3510 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
3511 return ERROR_INVALID_PARAMETER;
3514 r = MSI_IterateRecords( view, 0, msi_dialog_create_controls, dialog );
3515 msiobj_release( &view->hdr );
3516 return r;
3519 static UINT msi_dialog_reset( msi_dialog *dialog )
3521 /* FIXME: should restore the original values of any properties we changed */
3522 return msi_dialog_evaluate_control_conditions( dialog );
3525 /* figure out the height of 10 point MS Sans Serif */
3526 static INT msi_dialog_get_sans_serif_height( HWND hwnd )
3528 static const WCHAR szSansSerif[] = {
3529 'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0 };
3530 LOGFONTW lf;
3531 TEXTMETRICW tm;
3532 BOOL r;
3533 LONG height = 0;
3534 HFONT hFont, hOldFont;
3535 HDC hdc;
3537 hdc = GetDC( hwnd );
3538 if (hdc)
3540 memset( &lf, 0, sizeof lf );
3541 lf.lfHeight = MulDiv(12, GetDeviceCaps(hdc, LOGPIXELSY), 72);
3542 strcpyW( lf.lfFaceName, szSansSerif );
3543 hFont = CreateFontIndirectW(&lf);
3544 if (hFont)
3546 hOldFont = SelectObject( hdc, hFont );
3547 r = GetTextMetricsW( hdc, &tm );
3548 if (r)
3549 height = tm.tmHeight;
3550 SelectObject( hdc, hOldFont );
3551 DeleteObject( hFont );
3553 ReleaseDC( hwnd, hdc );
3555 return height;
3558 /* fetch the associated record from the Dialog table */
3559 static MSIRECORD *msi_get_dialog_record( msi_dialog *dialog )
3561 static const WCHAR query[] = {
3562 'S','E','L','E','C','T',' ','*',' ',
3563 'F','R','O','M',' ','D','i','a','l','o','g',' ',
3564 'W','H','E','R','E',' ',
3565 '`','D','i','a','l','o','g','`',' ','=',' ','\'','%','s','\'',0};
3566 MSIPACKAGE *package = dialog->package;
3567 MSIRECORD *rec = NULL;
3569 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
3571 rec = MSI_QueryGetRecord( package->db, query, dialog->name );
3572 if( !rec )
3573 WARN("query failed for dialog %s\n", debugstr_w(dialog->name));
3575 return rec;
3578 static void msi_dialog_adjust_dialog_pos( msi_dialog *dialog, MSIRECORD *rec, LPRECT pos )
3580 static const WCHAR szScreenX[] = {'S','c','r','e','e','n','X',0};
3581 static const WCHAR szScreenY[] = {'S','c','r','e','e','n','Y',0};
3583 UINT xres, yres;
3584 POINT center;
3585 SIZE sz;
3586 LONG style;
3588 center.x = MSI_RecordGetInteger( rec, 2 );
3589 center.y = MSI_RecordGetInteger( rec, 3 );
3591 sz.cx = MSI_RecordGetInteger( rec, 4 );
3592 sz.cy = MSI_RecordGetInteger( rec, 5 );
3594 sz.cx = msi_dialog_scale_unit( dialog, sz.cx );
3595 sz.cy = msi_dialog_scale_unit( dialog, sz.cy );
3597 xres = msi_get_property_int( dialog->package->db, szScreenX, 0 );
3598 yres = msi_get_property_int( dialog->package->db, szScreenY, 0 );
3600 center.x = MulDiv( center.x, xres, 100 );
3601 center.y = MulDiv( center.y, yres, 100 );
3603 /* turn the client pos into the window rectangle */
3604 if (dialog->package->center_x && dialog->package->center_y)
3606 pos->left = dialog->package->center_x - sz.cx / 2.0;
3607 pos->right = pos->left + sz.cx;
3608 pos->top = dialog->package->center_y - sz.cy / 2.0;
3609 pos->bottom = pos->top + sz.cy;
3611 else
3613 pos->left = center.x - sz.cx/2;
3614 pos->right = pos->left + sz.cx;
3615 pos->top = center.y - sz.cy/2;
3616 pos->bottom = pos->top + sz.cy;
3618 /* save the center */
3619 dialog->package->center_x = center.x;
3620 dialog->package->center_y = center.y;
3623 dialog->size.cx = sz.cx;
3624 dialog->size.cy = sz.cy;
3626 TRACE("%u %u %u %u\n", pos->left, pos->top, pos->right, pos->bottom);
3628 style = GetWindowLongPtrW( dialog->hwnd, GWL_STYLE );
3629 AdjustWindowRect( pos, style, FALSE );
3632 static void msi_dialog_set_tab_order( msi_dialog *dialog, LPCWSTR first )
3634 struct list tab_chain;
3635 msi_control *control;
3636 HWND prev = HWND_TOP;
3638 list_init( &tab_chain );
3639 if (!(control = msi_dialog_find_control( dialog, first ))) return;
3641 dialog->hWndFocus = control->hwnd;
3642 while (control)
3644 list_remove( &control->entry );
3645 list_add_tail( &tab_chain, &control->entry );
3646 if (!control->tabnext) break;
3647 control = msi_dialog_find_control( dialog, control->tabnext );
3650 LIST_FOR_EACH_ENTRY( control, &tab_chain, msi_control, entry )
3652 SetWindowPos( control->hwnd, prev, 0, 0, 0, 0,
3653 SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOREDRAW |
3654 SWP_NOREPOSITION | SWP_NOSENDCHANGING | SWP_NOSIZE );
3655 prev = control->hwnd;
3658 /* put them back on the main list */
3659 list_move_head( &dialog->controls, &tab_chain );
3662 static LRESULT msi_dialog_oncreate( HWND hwnd, LPCREATESTRUCTW cs )
3664 static const WCHAR df[] = {
3665 'D','e','f','a','u','l','t','U','I','F','o','n','t',0 };
3666 static const WCHAR dfv[] = {
3667 'M','S',' ','S','h','e','l','l',' ','D','l','g',0 };
3668 msi_dialog *dialog = cs->lpCreateParams;
3669 MSIRECORD *rec = NULL;
3670 LPWSTR title = NULL;
3671 RECT pos;
3673 TRACE("%p %p\n", dialog, dialog->package);
3675 dialog->hwnd = hwnd;
3676 SetWindowLongPtrW( hwnd, GWLP_USERDATA, (LONG_PTR) dialog );
3678 rec = msi_get_dialog_record( dialog );
3679 if( !rec )
3681 TRACE("No record found for dialog %s\n", debugstr_w(dialog->name));
3682 return -1;
3685 dialog->scale = msi_dialog_get_sans_serif_height(dialog->hwnd);
3687 msi_dialog_adjust_dialog_pos( dialog, rec, &pos );
3689 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
3691 dialog->default_font = msi_dup_property( dialog->package->db, df );
3692 if (!dialog->default_font)
3694 dialog->default_font = strdupW(dfv);
3695 msiobj_release( &rec->hdr );
3696 if (!dialog->default_font) return -1;
3699 title = msi_get_deformatted_field( dialog->package, rec, 7 );
3700 SetWindowTextW( hwnd, title );
3701 msi_free( title );
3703 SetWindowPos( hwnd, 0, pos.left, pos.top,
3704 pos.right - pos.left, pos.bottom - pos.top,
3705 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW );
3707 msi_dialog_build_font_list( dialog );
3708 msi_dialog_fill_controls( dialog );
3709 msi_dialog_evaluate_control_conditions( dialog );
3710 msi_dialog_set_tab_order( dialog, MSI_RecordGetString( rec, 8 ) );
3711 msiobj_release( &rec->hdr );
3713 return 0;
3716 static LRESULT msi_dialog_oncommand( msi_dialog *dialog, WPARAM param, HWND hwnd )
3718 msi_control *control = NULL;
3720 TRACE("%p %p %08lx\n", dialog, hwnd, param);
3722 switch (param)
3724 case 1: /* enter */
3725 control = msi_dialog_find_control( dialog, dialog->control_default );
3726 break;
3727 case 2: /* escape */
3728 control = msi_dialog_find_control( dialog, dialog->control_cancel );
3729 break;
3730 default:
3731 control = msi_dialog_find_control_by_hwnd( dialog, hwnd );
3734 if( control )
3736 if( control->handler )
3738 control->handler( dialog, control, param );
3739 msi_dialog_evaluate_control_conditions( dialog );
3743 return 0;
3746 static LRESULT msi_dialog_onnotify( msi_dialog *dialog, LPARAM param )
3748 LPNMHDR nmhdr = (LPNMHDR) param;
3749 msi_control *control = msi_dialog_find_control_by_hwnd( dialog, nmhdr->hwndFrom );
3751 TRACE("%p %p\n", dialog, nmhdr->hwndFrom);
3753 if ( control && control->handler )
3754 control->handler( dialog, control, param );
3756 return 0;
3759 static void dialog_setfocus( msi_dialog *dialog )
3761 HWND hwnd = dialog->hWndFocus;
3763 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, TRUE);
3764 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, FALSE);
3765 SetFocus( hwnd );
3766 dialog->hWndFocus = hwnd;
3769 static LRESULT WINAPI MSIDialog_WndProc( HWND hwnd, UINT msg,
3770 WPARAM wParam, LPARAM lParam )
3772 msi_dialog *dialog = (LPVOID) GetWindowLongPtrW( hwnd, GWLP_USERDATA );
3774 TRACE("0x%04x\n", msg);
3776 switch (msg)
3778 case WM_MOVE:
3779 dialog->package->center_x = LOWORD(lParam) + dialog->size.cx / 2.0;
3780 dialog->package->center_y = HIWORD(lParam) + dialog->size.cy / 2.0;
3781 break;
3783 case WM_CREATE:
3784 return msi_dialog_oncreate( hwnd, (LPCREATESTRUCTW)lParam );
3786 case WM_COMMAND:
3787 return msi_dialog_oncommand( dialog, wParam, (HWND)lParam );
3789 case WM_ACTIVATE:
3790 if( LOWORD(wParam) == WA_INACTIVE )
3791 dialog->hWndFocus = GetFocus();
3792 else
3793 dialog_setfocus( dialog );
3794 return 0;
3796 case WM_SETFOCUS:
3797 dialog_setfocus( dialog );
3798 return 0;
3800 /* bounce back to our subclassed static control */
3801 case WM_CTLCOLORSTATIC:
3802 return SendMessageW( (HWND) lParam, WM_CTLCOLORSTATIC, wParam, lParam );
3804 case WM_DESTROY:
3805 dialog->hwnd = NULL;
3806 return 0;
3807 case WM_NOTIFY:
3808 return msi_dialog_onnotify( dialog, lParam );
3810 return DefWindowProcW(hwnd, msg, wParam, lParam);
3813 static void process_pending_messages( HWND hdlg )
3815 MSG msg;
3817 while (PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
3819 if (hdlg && IsDialogMessageW( hdlg, &msg )) continue;
3820 TranslateMessage( &msg );
3821 DispatchMessageW( &msg );
3825 static UINT dialog_run_message_loop( msi_dialog *dialog )
3827 DWORD style;
3828 HWND hwnd;
3830 if( uiThreadId != GetCurrentThreadId() )
3831 return SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_CREATE, 0, (LPARAM) dialog );
3833 /* create the dialog window, don't show it yet */
3834 style = WS_OVERLAPPED;
3835 if( dialog->attributes & msidbDialogAttributesVisible )
3836 style |= WS_VISIBLE;
3838 hwnd = CreateWindowW( szMsiDialogClass, dialog->name, style,
3839 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
3840 NULL, NULL, NULL, dialog );
3841 if( !hwnd )
3843 ERR("Failed to create dialog %s\n", debugstr_w( dialog->name ));
3844 return ERROR_FUNCTION_FAILED;
3847 ShowWindow( hwnd, SW_SHOW );
3848 /* UpdateWindow( hwnd ); - and causes the transparent static controls not to paint */
3850 if( dialog->attributes & msidbDialogAttributesModal )
3852 while( !dialog->finished )
3854 MsgWaitForMultipleObjects( 0, NULL, 0, INFINITE, QS_ALLINPUT );
3855 process_pending_messages( dialog->hwnd );
3858 else
3859 return ERROR_IO_PENDING;
3861 return ERROR_SUCCESS;
3864 static LRESULT WINAPI MSIHiddenWindowProc( HWND hwnd, UINT msg,
3865 WPARAM wParam, LPARAM lParam )
3867 msi_dialog *dialog = (msi_dialog*) lParam;
3869 TRACE("%d %p\n", msg, dialog);
3871 switch (msg)
3873 case WM_MSI_DIALOG_CREATE:
3874 return dialog_run_message_loop( dialog );
3875 case WM_MSI_DIALOG_DESTROY:
3876 msi_dialog_destroy( dialog );
3877 return 0;
3879 return DefWindowProcW( hwnd, msg, wParam, lParam );
3882 static BOOL dialog_register_class( void )
3884 WNDCLASSW cls;
3886 ZeroMemory( &cls, sizeof cls );
3887 cls.lpfnWndProc = MSIDialog_WndProc;
3888 cls.hInstance = NULL;
3889 cls.hIcon = LoadIconW(0, (LPWSTR)IDI_APPLICATION);
3890 cls.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
3891 cls.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
3892 cls.lpszMenuName = NULL;
3893 cls.lpszClassName = szMsiDialogClass;
3895 if( !RegisterClassW( &cls ) )
3896 return FALSE;
3898 cls.lpfnWndProc = MSIHiddenWindowProc;
3899 cls.lpszClassName = szMsiHiddenWindow;
3901 if( !RegisterClassW( &cls ) )
3902 return FALSE;
3904 uiThreadId = GetCurrentThreadId();
3906 hMsiHiddenWindow = CreateWindowW( szMsiHiddenWindow, NULL, WS_OVERLAPPED,
3907 0, 0, 100, 100, NULL, NULL, NULL, NULL );
3908 if( !hMsiHiddenWindow )
3909 return FALSE;
3911 return TRUE;
3914 static msi_dialog *dialog_create( MSIPACKAGE *package, const WCHAR *name, msi_dialog *parent,
3915 control_event_handler event_handler )
3917 MSIRECORD *rec = NULL;
3918 msi_dialog *dialog;
3920 TRACE("%s\n", debugstr_w(name));
3922 if (!hMsiHiddenWindow) dialog_register_class();
3924 /* allocate the structure for the dialog to use */
3925 dialog = msi_alloc_zero( FIELD_OFFSET( msi_dialog, name[strlenW( name ) + 1] ));
3926 if( !dialog )
3927 return NULL;
3928 strcpyW( dialog->name, name );
3929 dialog->parent = parent;
3930 msiobj_addref( &package->hdr );
3931 dialog->package = package;
3932 dialog->event_handler = event_handler;
3933 dialog->finished = 0;
3934 list_init( &dialog->controls );
3935 list_init( &dialog->fonts );
3937 /* verify that the dialog exists */
3938 rec = msi_get_dialog_record( dialog );
3939 if( !rec )
3941 msiobj_release( &package->hdr );
3942 msi_free( dialog );
3943 return NULL;
3945 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
3946 dialog->control_default = strdupW( MSI_RecordGetString( rec, 9 ) );
3947 dialog->control_cancel = strdupW( MSI_RecordGetString( rec, 10 ) );
3948 msiobj_release( &rec->hdr );
3950 return dialog;
3953 static void msi_dialog_end_dialog( msi_dialog *dialog )
3955 TRACE("%p\n", dialog);
3956 dialog->finished = 1;
3957 PostMessageW(dialog->hwnd, WM_NULL, 0, 0);
3960 void msi_dialog_check_messages( HANDLE handle )
3962 DWORD r;
3964 /* in threads other than the UI thread, block */
3965 if( uiThreadId != GetCurrentThreadId() )
3967 if (!handle) return;
3968 while (MsgWaitForMultipleObjectsEx( 1, &handle, INFINITE, QS_ALLINPUT, 0 ) == WAIT_OBJECT_0 + 1)
3970 MSG msg;
3971 while (PeekMessageW( &msg, NULL, 0, 0, PM_REMOVE ))
3973 TranslateMessage( &msg );
3974 DispatchMessageW( &msg );
3977 return;
3980 /* there are two choices for the UI thread */
3981 while (1)
3983 process_pending_messages( NULL );
3985 if( !handle )
3986 break;
3989 * block here until somebody creates a new dialog or
3990 * the handle we're waiting on becomes ready
3992 r = MsgWaitForMultipleObjects( 1, &handle, 0, INFINITE, QS_ALLINPUT );
3993 if( r == WAIT_OBJECT_0 )
3994 break;
3998 static void dialog_do_preview( msi_dialog *dialog )
4000 TRACE("\n");
4001 dialog->attributes |= msidbDialogAttributesVisible;
4002 dialog->attributes &= ~msidbDialogAttributesModal;
4003 dialog_run_message_loop( dialog );
4006 static void free_subscriber( struct subscriber *sub )
4008 msi_free( sub->event );
4009 msi_free( sub->control );
4010 msi_free( sub->attribute );
4011 msi_free( sub );
4014 static void event_cleanup_subscriptions( MSIPACKAGE *package, const WCHAR *dialog )
4016 struct list *item, *next;
4018 LIST_FOR_EACH_SAFE( item, next, &package->subscriptions )
4020 struct subscriber *sub = LIST_ENTRY( item, struct subscriber, entry );
4022 if (strcmpW( sub->dialog->name, dialog )) continue;
4023 list_remove( &sub->entry );
4024 free_subscriber( sub );
4028 void msi_dialog_destroy( msi_dialog *dialog )
4030 msi_font *font, *next;
4032 if( uiThreadId != GetCurrentThreadId() )
4034 SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_DESTROY, 0, (LPARAM) dialog );
4035 return;
4038 if( dialog->hwnd )
4039 ShowWindow( dialog->hwnd, SW_HIDE );
4041 if( dialog->hwnd )
4042 DestroyWindow( dialog->hwnd );
4044 /* unsubscribe events */
4045 event_cleanup_subscriptions( dialog->package, dialog->name );
4047 /* destroy the list of controls */
4048 while( !list_empty( &dialog->controls ) )
4050 msi_control *t;
4052 t = LIST_ENTRY( list_head( &dialog->controls ),
4053 msi_control, entry );
4054 msi_destroy_control( t );
4057 /* destroy the list of fonts */
4058 LIST_FOR_EACH_ENTRY_SAFE( font, next, &dialog->fonts, msi_font, entry )
4060 list_remove( &font->entry );
4061 DeleteObject( font->hfont );
4062 msi_free( font );
4064 msi_free( dialog->default_font );
4066 msi_free( dialog->control_default );
4067 msi_free( dialog->control_cancel );
4068 msiobj_release( &dialog->package->hdr );
4069 dialog->package = NULL;
4070 msi_free( dialog );
4073 void msi_dialog_unregister_class( void )
4075 DestroyWindow( hMsiHiddenWindow );
4076 hMsiHiddenWindow = NULL;
4077 UnregisterClassW( szMsiDialogClass, NULL );
4078 UnregisterClassW( szMsiHiddenWindow, NULL );
4079 uiThreadId = 0;
4082 void msi_event_cleanup_all_subscriptions( MSIPACKAGE *package )
4084 struct list *item, *next;
4086 LIST_FOR_EACH_SAFE( item, next, &package->subscriptions )
4088 struct subscriber *sub = LIST_ENTRY( item, struct subscriber, entry );
4089 list_remove( &sub->entry );
4090 free_subscriber( sub );
4094 static UINT error_dialog_handler( msi_dialog *dialog, const WCHAR *event, const WCHAR *argument )
4096 static const WCHAR end_dialog[] = {'E','n','d','D','i','a','l','o','g',0};
4097 static const WCHAR error_abort[] = {'E','r','r','o','r','A','b','o','r','t',0};
4098 static const WCHAR error_cancel[] = {'E','r','r','o','r','C','a','n','c','e','l',0};
4099 static const WCHAR error_no[] = {'E','r','r','o','r','N','o',0};
4100 static const WCHAR result_prop[] = {
4101 'M','S','I','E','r','r','o','r','D','i','a','l','o','g','R','e','s','u','l','t',0
4104 if ( strcmpW( event, end_dialog ) )
4105 return ERROR_SUCCESS;
4107 if ( !strcmpW( argument, error_abort ) || !strcmpW( argument, error_cancel ) ||
4108 !strcmpW( argument, error_no ) )
4110 msi_set_property( dialog->package->db, result_prop, error_abort, -1 );
4113 msi_event_cleanup_all_subscriptions( dialog->package );
4114 msi_dialog_end_dialog( dialog );
4116 return ERROR_SUCCESS;
4119 static UINT msi_error_dialog_set_error( MSIPACKAGE *package, LPWSTR error_dialog, LPWSTR error )
4121 MSIRECORD * row;
4123 static const WCHAR update[] =
4124 {'U','P','D','A','T','E',' ','`','C','o','n','t','r','o','l','`',' ',
4125 'S','E','T',' ','`','T','e','x','t','`',' ','=',' ','\'','%','s','\'',' ',
4126 'W','H','E','R','E', ' ','`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
4127 'A','N','D',' ','`','C','o','n','t','r','o','l','`',' ','=',' ',
4128 '\'','E','r','r','o','r','T','e','x','t','\'',0};
4130 row = MSI_QueryGetRecord( package->db, update, error, error_dialog );
4131 if (!row)
4132 return ERROR_FUNCTION_FAILED;
4134 msiobj_release(&row->hdr);
4135 return ERROR_SUCCESS;
4138 UINT msi_spawn_error_dialog( MSIPACKAGE *package, LPWSTR error_dialog, LPWSTR error )
4140 msi_dialog *dialog;
4141 WCHAR result[MAX_PATH];
4142 UINT r = ERROR_SUCCESS;
4143 DWORD size = MAX_PATH;
4144 int res;
4146 static const WCHAR pn_prop[] = {'P','r','o','d','u','c','t','N','a','m','e',0};
4147 static const WCHAR title_fmt[] = {'%','s',' ','W','a','r','n','i','n','g',0};
4148 static const WCHAR error_abort[] = {'E','r','r','o','r','A','b','o','r','t',0};
4149 static const WCHAR result_prop[] = {
4150 'M','S','I','E','r','r','o','r','D','i','a','l','o','g','R','e','s','u','l','t',0
4153 if ((package->ui_level & INSTALLUILEVEL_MASK) == INSTALLUILEVEL_NONE) return ERROR_SUCCESS;
4155 if ( !error_dialog )
4157 LPWSTR product_name = msi_dup_property( package->db, pn_prop );
4158 WCHAR title[MAX_PATH];
4160 sprintfW( title, title_fmt, product_name );
4161 res = MessageBoxW( NULL, error, title, MB_OKCANCEL | MB_ICONWARNING );
4163 msi_free( product_name );
4165 if ( res == IDOK )
4166 return ERROR_SUCCESS;
4167 else
4168 return ERROR_FUNCTION_FAILED;
4171 r = msi_error_dialog_set_error( package, error_dialog, error );
4172 if ( r != ERROR_SUCCESS )
4173 return r;
4175 dialog = dialog_create( package, error_dialog, package->dialog, error_dialog_handler );
4176 if ( !dialog )
4177 return ERROR_FUNCTION_FAILED;
4179 dialog->finished = FALSE;
4180 r = dialog_run_message_loop( dialog );
4181 if ( r != ERROR_SUCCESS )
4182 goto done;
4184 r = msi_get_property( package->db, result_prop, result, &size );
4185 if ( r != ERROR_SUCCESS)
4186 r = ERROR_SUCCESS;
4188 if ( !strcmpW( result, error_abort ) )
4189 r = ERROR_FUNCTION_FAILED;
4191 done:
4192 msi_dialog_destroy( dialog );
4194 return r;
4197 static void MSI_ClosePreview( MSIOBJECTHDR *arg )
4199 MSIPREVIEW *preview = (MSIPREVIEW *)arg;
4200 msiobj_release( &preview->package->hdr );
4203 static MSIPREVIEW *MSI_EnableUIPreview( MSIDATABASE *db )
4205 MSIPREVIEW *preview = NULL;
4206 MSIPACKAGE *package;
4208 package = MSI_CreatePackage( db, NULL );
4209 if (package)
4211 preview = alloc_msiobject( MSIHANDLETYPE_PREVIEW, sizeof(MSIPREVIEW), MSI_ClosePreview );
4212 if (preview)
4214 preview->package = package;
4215 msiobj_addref( &package->hdr );
4217 msiobj_release( &package->hdr );
4219 return preview;
4222 UINT WINAPI MsiEnableUIPreview( MSIHANDLE hdb, MSIHANDLE *phPreview )
4224 MSIDATABASE *db;
4225 MSIPREVIEW *preview;
4226 UINT r = ERROR_FUNCTION_FAILED;
4228 TRACE("%d %p\n", hdb, phPreview);
4230 db = msihandle2msiinfo( hdb, MSIHANDLETYPE_DATABASE );
4231 if (!db)
4233 IWineMsiRemoteDatabase *remote_database;
4235 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( hdb );
4236 if (!remote_database)
4237 return ERROR_INVALID_HANDLE;
4239 *phPreview = 0;
4241 IWineMsiRemoteDatabase_Release( remote_database );
4242 WARN("MsiEnableUIPreview not allowed during a custom action!\n");
4244 return ERROR_FUNCTION_FAILED;
4246 preview = MSI_EnableUIPreview( db );
4247 if (preview)
4249 *phPreview = alloc_msihandle( &preview->hdr );
4250 msiobj_release( &preview->hdr );
4251 r = ERROR_SUCCESS;
4252 if (!*phPreview)
4253 r = ERROR_NOT_ENOUGH_MEMORY;
4255 msiobj_release( &db->hdr );
4256 return r;
4259 static UINT preview_event_handler( msi_dialog *dialog, const WCHAR *event, const WCHAR *argument )
4261 MESSAGE("Preview dialog event '%s' (arg='%s')\n", debugstr_w(event), debugstr_w(argument));
4262 return ERROR_SUCCESS;
4265 static UINT MSI_PreviewDialogW( MSIPREVIEW *preview, LPCWSTR szDialogName )
4267 msi_dialog *dialog = NULL;
4268 UINT r = ERROR_SUCCESS;
4270 if (preview->dialog)
4271 msi_dialog_destroy( preview->dialog );
4273 /* an empty name means we should just destroy the current preview dialog */
4274 if (szDialogName)
4276 dialog = dialog_create( preview->package, szDialogName, NULL, preview_event_handler );
4277 if (dialog)
4278 dialog_do_preview( dialog );
4279 else
4280 r = ERROR_FUNCTION_FAILED;
4282 preview->dialog = dialog;
4283 return r;
4286 UINT WINAPI MsiPreviewDialogW( MSIHANDLE hPreview, LPCWSTR szDialogName )
4288 MSIPREVIEW *preview;
4289 UINT r;
4291 TRACE("%d %s\n", hPreview, debugstr_w(szDialogName));
4293 preview = msihandle2msiinfo( hPreview, MSIHANDLETYPE_PREVIEW );
4294 if (!preview)
4295 return ERROR_INVALID_HANDLE;
4297 r = MSI_PreviewDialogW( preview, szDialogName );
4298 msiobj_release( &preview->hdr );
4299 return r;
4302 UINT WINAPI MsiPreviewDialogA( MSIHANDLE hPreview, LPCSTR szDialogName )
4304 UINT r;
4305 LPWSTR strW = NULL;
4307 TRACE("%d %s\n", hPreview, debugstr_a(szDialogName));
4309 if (szDialogName)
4311 strW = strdupAtoW( szDialogName );
4312 if (!strW)
4313 return ERROR_OUTOFMEMORY;
4315 r = MsiPreviewDialogW( hPreview, strW );
4316 msi_free( strW );
4317 return r;
4320 UINT WINAPI MsiPreviewBillboardW( MSIHANDLE hPreview, LPCWSTR szControlName, LPCWSTR szBillboard )
4322 FIXME("%d %s %s\n", hPreview, debugstr_w(szControlName), debugstr_w(szBillboard));
4323 return ERROR_CALL_NOT_IMPLEMENTED;
4326 UINT WINAPI MsiPreviewBillboardA( MSIHANDLE hPreview, LPCSTR szControlName, LPCSTR szBillboard )
4328 FIXME("%d %s %s\n", hPreview, debugstr_a(szControlName), debugstr_a(szBillboard));
4329 return ERROR_CALL_NOT_IMPLEMENTED;
4332 typedef UINT (*event_handler)( msi_dialog *, const WCHAR * );
4334 struct control_event
4336 const WCHAR *event;
4337 event_handler handler;
4340 static UINT dialog_event_handler( msi_dialog *, const WCHAR *, const WCHAR * );
4342 /* create a dialog box and run it if it's modal */
4343 static UINT event_do_dialog( MSIPACKAGE *package, const WCHAR *name, msi_dialog *parent, BOOL destroy_modeless )
4345 msi_dialog *dialog;
4346 UINT r;
4348 /* create a new dialog */
4349 dialog = dialog_create( package, name, parent, dialog_event_handler );
4350 if (dialog)
4352 /* kill the current modeless dialog */
4353 if (destroy_modeless && package->dialog)
4355 msi_dialog_destroy( package->dialog );
4356 package->dialog = NULL;
4359 /* modeless dialogs return an error message */
4360 r = dialog_run_message_loop( dialog );
4361 if (r == ERROR_SUCCESS)
4362 msi_dialog_destroy( dialog );
4363 else
4364 package->dialog = dialog;
4366 else r = ERROR_FUNCTION_FAILED;
4367 return r;
4370 /* end a modal dialog box */
4371 static UINT event_end_dialog( msi_dialog *dialog, const WCHAR *argument )
4373 static const WCHAR exitW[] = {'E','x','i','t',0};
4374 static const WCHAR retryW[] = {'R','e','t','r','y',0};
4375 static const WCHAR ignoreW[] = {'I','g','n','o','r','e',0};
4376 static const WCHAR returnW[] = {'R','e','t','u','r','n',0};
4378 if (!strcmpW( argument, exitW ))
4379 dialog->package->CurrentInstallState = ERROR_INSTALL_USEREXIT;
4380 else if (!strcmpW( argument, retryW ))
4381 dialog->package->CurrentInstallState = ERROR_INSTALL_SUSPEND;
4382 else if (!strcmpW( argument, ignoreW ))
4383 dialog->package->CurrentInstallState = ERROR_SUCCESS;
4384 else if (!strcmpW( argument, returnW ))
4386 msi_dialog *parent = dialog->parent;
4387 msi_free( dialog->package->next_dialog );
4388 dialog->package->next_dialog = (parent) ? strdupW( parent->name ) : NULL;
4389 dialog->package->CurrentInstallState = ERROR_SUCCESS;
4391 else
4393 ERR("Unknown argument string %s\n", debugstr_w(argument));
4394 dialog->package->CurrentInstallState = ERROR_FUNCTION_FAILED;
4396 event_cleanup_subscriptions( dialog->package, dialog->name );
4397 msi_dialog_end_dialog( dialog );
4398 return ERROR_SUCCESS;
4401 /* transition from one modal dialog to another modal dialog */
4402 static UINT event_new_dialog( msi_dialog *dialog, const WCHAR *argument )
4404 /* store the name of the next dialog, and signal this one to end */
4405 dialog->package->next_dialog = strdupW( argument );
4406 msi_event_cleanup_all_subscriptions( dialog->package );
4407 msi_dialog_end_dialog( dialog );
4408 return ERROR_SUCCESS;
4411 /* create a new child dialog of an existing modal dialog */
4412 static UINT event_spawn_dialog( msi_dialog *dialog, const WCHAR *argument )
4414 /* don't destroy a modeless dialogs that might be our parent */
4415 event_do_dialog( dialog->package, argument, dialog, FALSE );
4416 if (dialog->package->CurrentInstallState != ERROR_SUCCESS) msi_dialog_end_dialog( dialog );
4417 return ERROR_SUCCESS;
4420 /* creates a dialog that remains up for a period of time based on a condition */
4421 static UINT event_spawn_wait_dialog( msi_dialog *dialog, const WCHAR *argument )
4423 FIXME("doing nothing\n");
4424 return ERROR_SUCCESS;
4427 static UINT event_do_action( msi_dialog *dialog, const WCHAR *argument )
4429 ACTION_PerformAction( dialog->package, argument, SCRIPT_NONE );
4430 return ERROR_SUCCESS;
4433 static UINT event_add_local( msi_dialog *dialog, const WCHAR *argument )
4435 MSIFEATURE *feature;
4437 LIST_FOR_EACH_ENTRY( feature, &dialog->package->features, MSIFEATURE, entry )
4439 if (!strcmpW( argument, feature->Feature ) || !strcmpW( argument, szAll ))
4441 if (feature->ActionRequest != INSTALLSTATE_LOCAL)
4442 msi_set_property( dialog->package->db, szPreselected, szOne, -1 );
4443 MSI_SetFeatureStateW( dialog->package, feature->Feature, INSTALLSTATE_LOCAL );
4446 return ERROR_SUCCESS;
4449 static UINT event_remove( msi_dialog *dialog, const WCHAR *argument )
4451 MSIFEATURE *feature;
4453 LIST_FOR_EACH_ENTRY( feature, &dialog->package->features, MSIFEATURE, entry )
4455 if (!strcmpW( argument, feature->Feature ) || !strcmpW( argument, szAll ))
4457 if (feature->ActionRequest != INSTALLSTATE_ABSENT)
4458 msi_set_property( dialog->package->db, szPreselected, szOne, -1 );
4459 MSI_SetFeatureStateW( dialog->package, feature->Feature, INSTALLSTATE_ABSENT );
4462 return ERROR_SUCCESS;
4465 static UINT event_add_source( msi_dialog *dialog, const WCHAR *argument )
4467 MSIFEATURE *feature;
4469 LIST_FOR_EACH_ENTRY( feature, &dialog->package->features, MSIFEATURE, entry )
4471 if (!strcmpW( argument, feature->Feature ) || !strcmpW( argument, szAll ))
4473 if (feature->ActionRequest != INSTALLSTATE_SOURCE)
4474 msi_set_property( dialog->package->db, szPreselected, szOne, -1 );
4475 MSI_SetFeatureStateW( dialog->package, feature->Feature, INSTALLSTATE_SOURCE );
4478 return ERROR_SUCCESS;
4481 void msi_event_fire( MSIPACKAGE *package, const WCHAR *event, MSIRECORD *rec )
4483 struct subscriber *sub;
4485 TRACE("firing event %s\n", debugstr_w(event));
4487 LIST_FOR_EACH_ENTRY( sub, &package->subscriptions, struct subscriber, entry )
4489 if (strcmpiW( sub->event, event )) continue;
4490 dialog_handle_event( sub->dialog, sub->control, sub->attribute, rec );
4494 static UINT event_set_target_path( msi_dialog *dialog, const WCHAR *argument )
4496 WCHAR *path = msi_dup_property( dialog->package->db, argument );
4497 MSIRECORD *rec = MSI_CreateRecord( 1 );
4498 UINT r = ERROR_SUCCESS;
4500 MSI_RecordSetStringW( rec, 1, path );
4501 msi_event_fire( dialog->package, szSelectionPath, rec );
4502 if (path)
4504 /* failure to set the path halts the executing of control events */
4505 r = MSI_SetTargetPathW( dialog->package, argument, path );
4506 msi_free( path );
4508 msi_free( &rec->hdr );
4509 return r;
4512 static UINT event_reset( msi_dialog *dialog, const WCHAR *argument )
4514 msi_dialog_reset( dialog );
4515 return ERROR_SUCCESS;
4518 /* Return ERROR_SUCCESS if dialog is process and ERROR_FUNCTION_FAILED
4519 * if the given parameter is not a dialog box
4521 UINT ACTION_DialogBox( MSIPACKAGE *package, const WCHAR *dialog )
4523 UINT r;
4525 if (package->next_dialog) ERR("Already got next dialog... ignoring it\n");
4526 package->next_dialog = NULL;
4528 /* Dialogs are chained by filling in the next_dialog member
4529 * of the package structure, then terminating the current dialog.
4530 * The code below sees the next_dialog member set, and runs the
4531 * next dialog.
4532 * We fall out of the loop below if we come across a modeless
4533 * dialog, as it returns ERROR_IO_PENDING when we try to run
4534 * its message loop.
4536 r = event_do_dialog( package, dialog, NULL, TRUE );
4537 while (r == ERROR_SUCCESS && package->next_dialog)
4539 WCHAR *name = package->next_dialog;
4541 package->next_dialog = NULL;
4542 r = event_do_dialog( package, name, NULL, TRUE );
4543 msi_free( name );
4545 if (r == ERROR_IO_PENDING) r = ERROR_SUCCESS;
4546 return r;
4549 static UINT event_set_install_level( msi_dialog *dialog, const WCHAR *argument )
4551 int level = atolW( argument );
4553 TRACE("setting install level to %d\n", level);
4554 return MSI_SetInstallLevel( dialog->package, level );
4557 static UINT event_directory_list_up( msi_dialog *dialog, const WCHAR *argument )
4559 return msi_dialog_directorylist_up( dialog );
4562 static UINT event_reinstall_mode( msi_dialog *dialog, const WCHAR *argument )
4564 return msi_set_property( dialog->package->db, szReinstallMode, argument, -1 );
4567 static UINT event_reinstall( msi_dialog *dialog, const WCHAR *argument )
4569 return msi_set_property( dialog->package->db, szReinstall, argument, -1 );
4572 static UINT event_validate_product_id( msi_dialog *dialog, const WCHAR *argument )
4574 return msi_validate_product_id( dialog->package );
4577 static const WCHAR end_dialogW[] = {'E','n','d','D','i','a','l','o','g',0};
4578 static const WCHAR new_dialogW[] = {'N','e','w','D','i','a','l','o','g',0};
4579 static const WCHAR spawn_dialogW[] = {'S','p','a','w','n','D','i','a','l','o','g',0};
4580 static const WCHAR spawn_wait_dialogW[] = {'S','p','a','w','n','W','a','i','t','D','i','a','l','o','g',0};
4581 static const WCHAR do_actionW[] = {'D','o','A','c','t','i','o','n',0};
4582 static const WCHAR add_localW[] = {'A','d','d','L','o','c','a','l',0};
4583 static const WCHAR removeW[] = {'R','e','m','o','v','e',0};
4584 static const WCHAR add_sourceW[] = {'A','d','d','S','o','u','r','c','e',0};
4585 static const WCHAR set_target_pathW[] = {'S','e','t','T','a','r','g','e','t','P','a','t','h',0};
4586 static const WCHAR resetW[] = {'R','e','s','e','t',0};
4587 static const WCHAR set_install_levelW[] = {'S','e','t','I','n','s','t','a','l','l','L','e','v','e','l',0};
4588 static const WCHAR directory_list_upW[] = {'D','i','r','e','c','t','o','r','y','L','i','s','t','U','p',0};
4589 static const WCHAR selection_browseW[] = {'S','e','l','e','c','t','i','o','n','B','r','o','w','s','e',0};
4590 static const WCHAR reinstall_modeW[] = {'R','e','i','n','s','t','a','l','l','M','o','d','e',0};
4591 static const WCHAR reinstallW[] = {'R','e','i','n','s','t','a','l','l',0};
4592 static const WCHAR validate_product_idW[] = {'V','a','l','i','d','a','t','e','P','r','o','d','u','c','t','I','D',0};
4594 static const struct control_event control_events[] =
4596 { end_dialogW, event_end_dialog },
4597 { new_dialogW, event_new_dialog },
4598 { spawn_dialogW, event_spawn_dialog },
4599 { spawn_wait_dialogW, event_spawn_wait_dialog },
4600 { do_actionW, event_do_action },
4601 { add_localW, event_add_local },
4602 { removeW, event_remove },
4603 { add_sourceW, event_add_source },
4604 { set_target_pathW, event_set_target_path },
4605 { resetW, event_reset },
4606 { set_install_levelW, event_set_install_level },
4607 { directory_list_upW, event_directory_list_up },
4608 { selection_browseW, event_spawn_dialog },
4609 { reinstall_modeW, event_reinstall_mode },
4610 { reinstallW, event_reinstall },
4611 { validate_product_idW, event_validate_product_id },
4612 { NULL, NULL }
4615 static UINT dialog_event_handler( msi_dialog *dialog, const WCHAR *event, const WCHAR *argument )
4617 unsigned int i;
4619 TRACE("handling event %s\n", debugstr_w(event));
4621 if (!event) return ERROR_SUCCESS;
4623 for (i = 0; control_events[i].event; i++)
4625 if (!strcmpW( control_events[i].event, event ))
4626 return control_events[i].handler( dialog, argument );
4628 FIXME("unhandled event %s arg(%s)\n", debugstr_w(event), debugstr_w(argument));
4629 return ERROR_SUCCESS;