user32/tests: Fix a memory leak (valgrind).
[wine.git] / dlls / msi / dialog.c
blob6708f1348b26b6089d66103e5f600044ca94f65f
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2005 Mike McCormack for CodeWeavers
5 * Copyright 2005 Aric Stewart for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define COBJMACROS
23 #define NONAMELESSUNION
24 #define NONAMELESSSTRUCT
26 #include <stdarg.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "winuser.h"
32 #include "winnls.h"
33 #include "msi.h"
34 #include "msipriv.h"
35 #include "msidefs.h"
36 #include "ocidl.h"
37 #include "olectl.h"
38 #include "richedit.h"
39 #include "commctrl.h"
40 #include "winreg.h"
41 #include "shlwapi.h"
42 #include "msiserver.h"
43 #include "shellapi.h"
45 #include "wine/debug.h"
46 #include "wine/unicode.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(msi);
50 extern HINSTANCE msi_hInstance;
52 struct msi_control_tag;
53 typedef struct msi_control_tag msi_control;
54 typedef UINT (*msi_handler)( msi_dialog *, msi_control *, WPARAM );
55 typedef void (*msi_update)( msi_dialog *, msi_control * );
56 typedef UINT (*control_event_handler)( msi_dialog *, const WCHAR *, const WCHAR * );
58 struct msi_control_tag
60 struct list entry;
61 HWND hwnd;
62 msi_handler handler;
63 msi_update update;
64 LPWSTR property;
65 LPWSTR value;
66 HBITMAP hBitmap;
67 HICON hIcon;
68 LPWSTR tabnext;
69 LPWSTR type;
70 HMODULE hDll;
71 float progress_current;
72 float progress_max;
73 BOOL progress_backwards;
74 DWORD attributes;
75 WCHAR name[1];
78 typedef struct msi_font_tag
80 struct list entry;
81 HFONT hfont;
82 COLORREF color;
83 WCHAR name[1];
84 } msi_font;
86 struct msi_dialog_tag
88 MSIPACKAGE *package;
89 msi_dialog *parent;
90 control_event_handler event_handler;
91 BOOL finished;
92 INT scale;
93 DWORD attributes;
94 SIZE size;
95 HWND hwnd;
96 LPWSTR default_font;
97 struct list fonts;
98 struct list controls;
99 HWND hWndFocus;
100 LPWSTR control_default;
101 LPWSTR control_cancel;
102 WCHAR name[1];
105 struct subscriber
107 struct list entry;
108 msi_dialog *dialog;
109 WCHAR *event;
110 WCHAR *control;
111 WCHAR *attribute;
114 typedef UINT (*msi_dialog_control_func)( msi_dialog *dialog, MSIRECORD *rec );
115 struct control_handler
117 LPCWSTR control_type;
118 msi_dialog_control_func func;
121 typedef struct
123 msi_dialog* dialog;
124 msi_control *parent;
125 DWORD attributes;
126 LPWSTR propval;
127 } radio_button_group_descr;
129 static const WCHAR szMsiDialogClass[] = { 'M','s','i','D','i','a','l','o','g','C','l','o','s','e','C','l','a','s','s',0 };
130 static const WCHAR szMsiHiddenWindow[] = { 'M','s','i','H','i','d','d','e','n','W','i','n','d','o','w',0 };
131 static const WCHAR szStatic[] = { 'S','t','a','t','i','c',0 };
132 static const WCHAR szButton[] = { 'B','U','T','T','O','N', 0 };
133 static const WCHAR szButtonData[] = { 'M','S','I','D','A','T','A',0 };
134 static const WCHAR szProgress[] = { 'P','r','o','g','r','e','s','s',0 };
135 static const WCHAR szText[] = { 'T','e','x','t',0 };
136 static const WCHAR szPushButton[] = { 'P','u','s','h','B','u','t','t','o','n',0 };
137 static const WCHAR szLine[] = { 'L','i','n','e',0 };
138 static const WCHAR szBitmap[] = { 'B','i','t','m','a','p',0 };
139 static const WCHAR szCheckBox[] = { 'C','h','e','c','k','B','o','x',0 };
140 static const WCHAR szScrollableText[] = { 'S','c','r','o','l','l','a','b','l','e','T','e','x','t',0 };
141 static const WCHAR szComboBox[] = { 'C','o','m','b','o','B','o','x',0 };
142 static const WCHAR szEdit[] = { 'E','d','i','t',0 };
143 static const WCHAR szMaskedEdit[] = { 'M','a','s','k','e','d','E','d','i','t',0 };
144 static const WCHAR szPathEdit[] = { 'P','a','t','h','E','d','i','t',0 };
145 static const WCHAR szProgressBar[] = { 'P','r','o','g','r','e','s','s','B','a','r',0 };
146 static const WCHAR szSetProgress[] = { 'S','e','t','P','r','o','g','r','e','s','s',0 };
147 static const WCHAR szRadioButtonGroup[] = { 'R','a','d','i','o','B','u','t','t','o','n','G','r','o','u','p',0 };
148 static const WCHAR szIcon[] = { 'I','c','o','n',0 };
149 static const WCHAR szSelectionTree[] = { 'S','e','l','e','c','t','i','o','n','T','r','e','e',0 };
150 static const WCHAR szGroupBox[] = { 'G','r','o','u','p','B','o','x',0 };
151 static const WCHAR szListBox[] = { 'L','i','s','t','B','o','x',0 };
152 static const WCHAR szDirectoryCombo[] = { 'D','i','r','e','c','t','o','r','y','C','o','m','b','o',0 };
153 static const WCHAR szDirectoryList[] = { 'D','i','r','e','c','t','o','r','y','L','i','s','t',0 };
154 static const WCHAR szVolumeCostList[] = { 'V','o','l','u','m','e','C','o','s','t','L','i','s','t',0 };
155 static const WCHAR szVolumeSelectCombo[] = { 'V','o','l','u','m','e','S','e','l','e','c','t','C','o','m','b','o',0 };
156 static const WCHAR szSelectionDescription[] = {'S','e','l','e','c','t','i','o','n','D','e','s','c','r','i','p','t','i','o','n',0};
157 static const WCHAR szSelectionPath[] = {'S','e','l','e','c','t','i','o','n','P','a','t','h',0};
158 static const WCHAR szProperty[] = {'P','r','o','p','e','r','t','y',0};
159 static const WCHAR szHyperLink[] = {'H','y','p','e','r','L','i','n','k',0};
161 /* dialog sequencing */
163 #define WM_MSI_DIALOG_CREATE (WM_USER+0x100)
164 #define WM_MSI_DIALOG_DESTROY (WM_USER+0x101)
166 #define USER_INSTALLSTATE_ALL 0x1000
168 static DWORD uiThreadId;
169 static HWND hMsiHiddenWindow;
171 static LPWSTR msi_get_window_text( HWND hwnd )
173 UINT sz, r;
174 LPWSTR buf;
176 sz = 0x20;
177 buf = msi_alloc( sz*sizeof(WCHAR) );
178 while ( buf )
180 r = GetWindowTextW( hwnd, buf, sz );
181 if ( r < (sz - 1) )
182 break;
183 sz *= 2;
184 buf = msi_realloc( buf, sz*sizeof(WCHAR) );
187 return buf;
190 static INT msi_dialog_scale_unit( msi_dialog *dialog, INT val )
192 return MulDiv( val, dialog->scale, 12 );
195 static msi_control *msi_dialog_find_control( msi_dialog *dialog, LPCWSTR name )
197 msi_control *control;
199 if( !name )
200 return NULL;
201 if( !dialog->hwnd )
202 return NULL;
203 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
204 if( !strcmpW( control->name, name ) ) /* FIXME: case sensitive? */
205 return control;
206 return NULL;
209 static msi_control *msi_dialog_find_control_by_type( msi_dialog *dialog, LPCWSTR type )
211 msi_control *control;
213 if( !type )
214 return NULL;
215 if( !dialog->hwnd )
216 return NULL;
217 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
218 if( !strcmpW( control->type, type ) ) /* FIXME: case sensitive? */
219 return control;
220 return NULL;
223 static msi_control *msi_dialog_find_control_by_hwnd( msi_dialog *dialog, HWND hwnd )
225 msi_control *control;
227 if( !dialog->hwnd )
228 return NULL;
229 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
230 if( hwnd == control->hwnd )
231 return control;
232 return NULL;
235 static LPWSTR msi_get_deformatted_field( MSIPACKAGE *package, MSIRECORD *rec, int field )
237 LPCWSTR str = MSI_RecordGetString( rec, field );
238 LPWSTR ret = NULL;
240 if (str)
241 deformat_string( package, str, &ret );
242 return ret;
245 static LPWSTR msi_dialog_dup_property( msi_dialog *dialog, LPCWSTR property, BOOL indirect )
247 LPWSTR prop = NULL;
249 if (!property)
250 return NULL;
252 if (indirect)
253 prop = msi_dup_property( dialog->package->db, property );
255 if (!prop)
256 prop = strdupW( property );
258 return prop;
262 * msi_dialog_get_style
264 * Extract the {\style} string from the front of the text to display and
265 * update the pointer. Only the last style in a list is applied.
267 static LPWSTR msi_dialog_get_style( LPCWSTR p, LPCWSTR *rest )
269 LPWSTR ret;
270 LPCWSTR q, i, first;
271 DWORD len;
273 q = NULL;
274 *rest = p;
275 if( !p )
276 return NULL;
278 while ((first = strchrW( p, '{' )) && (q = strchrW( first + 1, '}' )))
280 p = first + 1;
281 if( *p != '\\' && *p != '&' )
282 return NULL;
284 /* little bit of sanity checking to stop us getting confused with RTF */
285 for( i=++p; i<q; i++ )
286 if( *i == '}' || *i == '\\' )
287 return NULL;
290 if (!q)
291 return NULL;
293 *rest = ++q;
294 len = q - p;
296 ret = msi_alloc( len*sizeof(WCHAR) );
297 if( !ret )
298 return ret;
299 memcpy( ret, p, len*sizeof(WCHAR) );
300 ret[len-1] = 0;
301 return ret;
304 static UINT msi_dialog_add_font( MSIRECORD *rec, LPVOID param )
306 msi_dialog *dialog = param;
307 msi_font *font;
308 LPCWSTR face, name;
309 LOGFONTW lf;
310 INT style;
311 HDC hdc;
313 /* create a font and add it to the list */
314 name = MSI_RecordGetString( rec, 1 );
315 font = msi_alloc( FIELD_OFFSET( msi_font, name[strlenW( name ) + 1] ));
316 strcpyW( font->name, name );
317 list_add_head( &dialog->fonts, &font->entry );
319 font->color = MSI_RecordGetInteger( rec, 4 );
321 memset( &lf, 0, sizeof lf );
322 face = MSI_RecordGetString( rec, 2 );
323 lf.lfHeight = MSI_RecordGetInteger( rec, 3 );
324 style = MSI_RecordGetInteger( rec, 5 );
325 if( style & msidbTextStyleStyleBitsBold )
326 lf.lfWeight = FW_BOLD;
327 if( style & msidbTextStyleStyleBitsItalic )
328 lf.lfItalic = TRUE;
329 if( style & msidbTextStyleStyleBitsUnderline )
330 lf.lfUnderline = TRUE;
331 if( style & msidbTextStyleStyleBitsStrike )
332 lf.lfStrikeOut = TRUE;
333 lstrcpynW( lf.lfFaceName, face, LF_FACESIZE );
335 /* adjust the height */
336 hdc = GetDC( dialog->hwnd );
337 if (hdc)
339 lf.lfHeight = -MulDiv(lf.lfHeight, GetDeviceCaps(hdc, LOGPIXELSY), 72);
340 ReleaseDC( dialog->hwnd, hdc );
343 font->hfont = CreateFontIndirectW( &lf );
345 TRACE("Adding font style %s\n", debugstr_w(font->name) );
347 return ERROR_SUCCESS;
350 static msi_font *msi_dialog_find_font( msi_dialog *dialog, LPCWSTR name )
352 msi_font *font = NULL;
354 LIST_FOR_EACH_ENTRY( font, &dialog->fonts, msi_font, entry )
355 if( !strcmpW( font->name, name ) ) /* FIXME: case sensitive? */
356 break;
358 return font;
361 static UINT msi_dialog_set_font( msi_dialog *dialog, HWND hwnd, LPCWSTR name )
363 msi_font *font;
365 font = msi_dialog_find_font( dialog, name );
366 if( font )
367 SendMessageW( hwnd, WM_SETFONT, (WPARAM) font->hfont, TRUE );
368 else
369 ERR("No font entry for %s\n", debugstr_w(name));
370 return ERROR_SUCCESS;
373 static UINT msi_dialog_build_font_list( msi_dialog *dialog )
375 static const WCHAR query[] = {
376 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
377 '`','T','e','x','t','S','t','y','l','e','`',0};
378 MSIQUERY *view;
379 UINT r;
381 TRACE("dialog %p\n", dialog );
383 r = MSI_OpenQuery( dialog->package->db, &view, query );
384 if( r != ERROR_SUCCESS )
385 return r;
387 r = MSI_IterateRecords( view, NULL, msi_dialog_add_font, dialog );
388 msiobj_release( &view->hdr );
389 return r;
392 static void msi_destroy_control( msi_control *t )
394 list_remove( &t->entry );
395 /* leave dialog->hwnd - destroying parent destroys child windows */
396 msi_free( t->property );
397 msi_free( t->value );
398 if( t->hBitmap )
399 DeleteObject( t->hBitmap );
400 if( t->hIcon )
401 DestroyIcon( t->hIcon );
402 msi_free( t->tabnext );
403 msi_free( t->type );
404 if (t->hDll)
405 FreeLibrary( t->hDll );
406 msi_free( t );
409 static msi_control *dialog_create_window( msi_dialog *dialog, MSIRECORD *rec, DWORD exstyle,
410 const WCHAR *szCls, const WCHAR *name, const WCHAR *text,
411 DWORD style, HWND parent )
413 DWORD x, y, width, height;
414 LPWSTR font = NULL, title_font = NULL;
415 LPCWSTR title = NULL;
416 msi_control *control;
418 style |= WS_CHILD;
420 control = msi_alloc( FIELD_OFFSET( msi_control, name[strlenW( name ) + 1] ));
421 if (!control)
422 return NULL;
424 strcpyW( control->name, name );
425 list_add_tail( &dialog->controls, &control->entry );
426 control->handler = NULL;
427 control->update = NULL;
428 control->property = NULL;
429 control->value = NULL;
430 control->hBitmap = NULL;
431 control->hIcon = NULL;
432 control->hDll = NULL;
433 control->tabnext = strdupW( MSI_RecordGetString( rec, 11) );
434 control->type = strdupW( MSI_RecordGetString( rec, 3 ) );
435 control->progress_current = 0;
436 control->progress_max = 100;
437 control->progress_backwards = FALSE;
439 x = MSI_RecordGetInteger( rec, 4 );
440 y = MSI_RecordGetInteger( rec, 5 );
441 width = MSI_RecordGetInteger( rec, 6 );
442 height = MSI_RecordGetInteger( rec, 7 );
444 x = msi_dialog_scale_unit( dialog, x );
445 y = msi_dialog_scale_unit( dialog, y );
446 width = msi_dialog_scale_unit( dialog, width );
447 height = msi_dialog_scale_unit( dialog, height );
449 if( text )
451 deformat_string( dialog->package, text, &title_font );
452 font = msi_dialog_get_style( title_font, &title );
455 control->hwnd = CreateWindowExW( exstyle, szCls, title, style,
456 x, y, width, height, parent, NULL, NULL, NULL );
458 TRACE("Dialog %s control %s hwnd %p\n",
459 debugstr_w(dialog->name), debugstr_w(text), control->hwnd );
461 msi_dialog_set_font( dialog, control->hwnd,
462 font ? font : dialog->default_font );
464 msi_free( title_font );
465 msi_free( font );
467 return control;
470 static LPWSTR msi_dialog_get_uitext( msi_dialog *dialog, LPCWSTR key )
472 MSIRECORD *rec;
473 LPWSTR text;
475 static const WCHAR query[] = {
476 's','e','l','e','c','t',' ','*',' ',
477 'f','r','o','m',' ','`','U','I','T','e','x','t','`',' ',
478 'w','h','e','r','e',' ','`','K','e','y','`',' ','=',' ','\'','%','s','\'',0
481 rec = MSI_QueryGetRecord( dialog->package->db, query, key );
482 if (!rec) return NULL;
483 text = strdupW( MSI_RecordGetString( rec, 2 ) );
484 msiobj_release( &rec->hdr );
485 return text;
488 static MSIRECORD *msi_get_binary_record( MSIDATABASE *db, LPCWSTR name )
490 static const WCHAR query[] = {
491 's','e','l','e','c','t',' ','*',' ',
492 'f','r','o','m',' ','B','i','n','a','r','y',' ',
493 'w','h','e','r','e',' ',
494 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0
497 return MSI_QueryGetRecord( db, query, name );
500 static LPWSTR msi_create_tmp_path(void)
502 WCHAR tmp[MAX_PATH];
503 LPWSTR path = NULL;
504 DWORD len, r;
506 r = GetTempPathW( MAX_PATH, tmp );
507 if( !r )
508 return path;
509 len = lstrlenW( tmp ) + 20;
510 path = msi_alloc( len * sizeof (WCHAR) );
511 if( path )
513 r = GetTempFileNameW( tmp, szMsi, 0, path );
514 if (!r)
516 msi_free( path );
517 path = NULL;
520 return path;
523 static HANDLE msi_load_image( MSIDATABASE *db, LPCWSTR name, UINT type,
524 UINT cx, UINT cy, UINT flags )
526 MSIRECORD *rec = NULL;
527 HANDLE himage = NULL;
528 LPWSTR tmp;
529 UINT r;
531 TRACE("%p %s %u %u %08x\n", db, debugstr_w(name), cx, cy, flags);
533 tmp = msi_create_tmp_path();
534 if( !tmp )
535 return himage;
537 rec = msi_get_binary_record( db, name );
538 if( rec )
540 r = MSI_RecordStreamToFile( rec, 2, tmp );
541 if( r == ERROR_SUCCESS )
543 himage = LoadImageW( 0, tmp, type, cx, cy, flags );
545 msiobj_release( &rec->hdr );
547 DeleteFileW( tmp );
549 msi_free( tmp );
550 return himage;
553 static HICON msi_load_icon( MSIDATABASE *db, LPCWSTR text, UINT attributes )
555 DWORD cx = 0, cy = 0, flags;
557 flags = LR_LOADFROMFILE | LR_DEFAULTSIZE;
558 if( attributes & msidbControlAttributesFixedSize )
560 flags &= ~LR_DEFAULTSIZE;
561 if( attributes & msidbControlAttributesIconSize16 )
563 cx += 16;
564 cy += 16;
566 if( attributes & msidbControlAttributesIconSize32 )
568 cx += 32;
569 cy += 32;
571 /* msidbControlAttributesIconSize48 handled by above logic */
573 return msi_load_image( db, text, IMAGE_ICON, cx, cy, flags );
576 static void msi_dialog_update_controls( msi_dialog *dialog, LPCWSTR property )
578 msi_control *control;
580 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
582 if ( control->property && !strcmpW( control->property, property ) && control->update )
583 control->update( dialog, control );
587 static void msi_dialog_set_property( MSIPACKAGE *package, LPCWSTR property, LPCWSTR value )
589 UINT r = msi_set_property( package->db, property, value, -1 );
590 if (r == ERROR_SUCCESS && !strcmpW( property, szSourceDir ))
591 msi_reset_folders( package, TRUE );
594 static MSIFEATURE *msi_seltree_feature_from_item( HWND hwnd, HTREEITEM hItem )
596 TVITEMW tvi;
598 /* get the feature from the item */
599 memset( &tvi, 0, sizeof tvi );
600 tvi.hItem = hItem;
601 tvi.mask = TVIF_PARAM | TVIF_HANDLE;
602 SendMessageW( hwnd, TVM_GETITEMW, 0, (LPARAM)&tvi );
603 return (MSIFEATURE *)tvi.lParam;
606 struct msi_selection_tree_info
608 msi_dialog *dialog;
609 HWND hwnd;
610 WNDPROC oldproc;
611 HTREEITEM selected;
614 static MSIFEATURE *msi_seltree_get_selected_feature( msi_control *control )
616 struct msi_selection_tree_info *info = GetPropW( control->hwnd, szButtonData );
617 return msi_seltree_feature_from_item( control->hwnd, info->selected );
620 static void dialog_handle_event( msi_dialog *dialog, const WCHAR *control,
621 const WCHAR *attribute, MSIRECORD *rec )
623 msi_control* ctrl;
625 ctrl = msi_dialog_find_control( dialog, control );
626 if (!ctrl)
627 return;
628 if( !strcmpW( attribute, szText ) )
630 const WCHAR *font_text, *text = NULL;
631 WCHAR *font, *text_fmt = NULL;
633 font_text = MSI_RecordGetString( rec , 1 );
634 font = msi_dialog_get_style( font_text, &text );
635 deformat_string( dialog->package, text, &text_fmt );
636 if (text_fmt) text = text_fmt;
637 else text = szEmpty;
639 SetWindowTextW( ctrl->hwnd, text );
641 msi_free( font );
642 msi_free( text_fmt );
643 msi_dialog_check_messages( NULL );
645 else if( !strcmpW( attribute, szProgress ) )
647 DWORD func, val1, val2, units;
649 func = MSI_RecordGetInteger( rec, 1 );
650 val1 = MSI_RecordGetInteger( rec, 2 );
651 val2 = MSI_RecordGetInteger( rec, 3 );
653 TRACE("progress: func %u val1 %u val2 %u\n", func, val1, val2);
655 units = val1 / 512;
656 switch (func)
658 case 0: /* init */
659 SendMessageW( ctrl->hwnd, PBM_SETRANGE, 0, MAKELPARAM(0,100) );
660 if (val2)
662 ctrl->progress_max = units ? units : 100;
663 ctrl->progress_current = units;
664 ctrl->progress_backwards = TRUE;
665 SendMessageW( ctrl->hwnd, PBM_SETPOS, 100, 0 );
667 else
669 ctrl->progress_max = units ? units : 100;
670 ctrl->progress_current = 0;
671 ctrl->progress_backwards = FALSE;
672 SendMessageW( ctrl->hwnd, PBM_SETPOS, 0, 0 );
674 break;
675 case 1: /* action data increment */
676 if (val2) dialog->package->action_progress_increment = val1;
677 else dialog->package->action_progress_increment = 0;
678 break;
679 case 2: /* move */
680 if (ctrl->progress_backwards)
682 if (units >= ctrl->progress_current) ctrl->progress_current -= units;
683 else ctrl->progress_current = 0;
685 else
687 if (ctrl->progress_current + units < ctrl->progress_max) ctrl->progress_current += units;
688 else ctrl->progress_current = ctrl->progress_max;
690 SendMessageW( ctrl->hwnd, PBM_SETPOS, MulDiv(100, ctrl->progress_current, ctrl->progress_max), 0 );
691 break;
692 case 3: /* add */
693 ctrl->progress_max += units;
694 break;
695 default:
696 FIXME("Unknown progress message %u\n", func);
697 break;
700 else if ( !strcmpW( attribute, szProperty ) )
702 MSIFEATURE *feature = msi_seltree_get_selected_feature( ctrl );
703 if (feature) msi_dialog_set_property( dialog->package, ctrl->property, feature->Directory );
705 else if ( !strcmpW( attribute, szSelectionPath ) )
707 BOOL indirect = ctrl->attributes & msidbControlAttributesIndirect;
708 LPWSTR path = msi_dialog_dup_property( dialog, ctrl->property, indirect );
709 if (!path) return;
710 SetWindowTextW( ctrl->hwnd, path );
711 msi_free(path);
713 else
715 FIXME("Attribute %s not being set\n", debugstr_w(attribute));
716 return;
720 static void event_subscribe( msi_dialog *dialog, const WCHAR *event, const WCHAR *control, const WCHAR *attribute )
722 struct subscriber *sub;
724 TRACE("event %s control %s attribute %s\n", debugstr_w(event), debugstr_w(control), debugstr_w(attribute));
726 LIST_FOR_EACH_ENTRY( sub, &dialog->package->subscriptions, struct subscriber, entry )
728 if (!strcmpiW( sub->event, event ) &&
729 !strcmpiW( sub->control, control ) &&
730 !strcmpiW( sub->attribute, attribute ))
732 TRACE("already subscribed\n");
733 return;
736 if (!(sub = msi_alloc( sizeof(*sub) ))) return;
737 sub->dialog = dialog;
738 sub->event = strdupW( event );
739 sub->control = strdupW( control );
740 sub->attribute = strdupW( attribute );
741 list_add_tail( &dialog->package->subscriptions, &sub->entry );
744 struct dialog_control
746 msi_dialog *dialog;
747 const WCHAR *control;
750 static UINT map_event( MSIRECORD *row, void *param )
752 struct dialog_control *dc = param;
753 const WCHAR *event = MSI_RecordGetString( row, 3 );
754 const WCHAR *attribute = MSI_RecordGetString( row, 4 );
756 event_subscribe( dc->dialog, event, dc->control, attribute );
757 return ERROR_SUCCESS;
760 static void dialog_map_events( msi_dialog *dialog, const WCHAR *control )
762 static const WCHAR queryW[] =
763 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
764 '`','E','v','e','n','t','M','a','p','p','i','n','g','`',' ',
765 'W','H','E','R','E',' ','`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
766 'A','N','D',' ','`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',0};
767 MSIQUERY *view;
768 struct dialog_control dialog_control =
770 dialog,
771 control
774 if (!MSI_OpenQuery( dialog->package->db, &view, queryW, dialog->name, control ))
776 MSI_IterateRecords( view, NULL, map_event, &dialog_control );
777 msiobj_release( &view->hdr );
781 /* everything except radio buttons */
782 static msi_control *msi_dialog_add_control( msi_dialog *dialog,
783 MSIRECORD *rec, LPCWSTR szCls, DWORD style )
785 DWORD attributes;
786 const WCHAR *text = NULL, *name, *control_type;
787 DWORD exstyle = 0;
789 name = MSI_RecordGetString( rec, 2 );
790 control_type = MSI_RecordGetString( rec, 3 );
791 attributes = MSI_RecordGetInteger( rec, 8 );
792 if (strcmpW( control_type, szScrollableText )) text = MSI_RecordGetString( rec, 10 );
794 TRACE("%s, %s, %08x, %s, %08x\n", debugstr_w(szCls), debugstr_w(name),
795 attributes, debugstr_w(text), style);
797 if( attributes & msidbControlAttributesVisible )
798 style |= WS_VISIBLE;
799 if( ~attributes & msidbControlAttributesEnabled )
800 style |= WS_DISABLED;
801 if( attributes & msidbControlAttributesSunken )
802 exstyle |= WS_EX_CLIENTEDGE;
804 dialog_map_events( dialog, name );
806 return dialog_create_window( dialog, rec, exstyle, szCls, name, text, style, dialog->hwnd );
809 struct msi_text_info
811 msi_font *font;
812 WNDPROC oldproc;
813 DWORD attributes;
817 * we don't erase our own background,
818 * so we have to make sure that the parent window redraws first
820 static void msi_text_on_settext( HWND hWnd )
822 HWND hParent;
823 RECT rc;
825 hParent = GetParent( hWnd );
826 GetWindowRect( hWnd, &rc );
827 MapWindowPoints( NULL, hParent, (LPPOINT) &rc, 2 );
828 InvalidateRect( hParent, &rc, TRUE );
831 static LRESULT WINAPI
832 MSIText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
834 struct msi_text_info *info;
835 LRESULT r = 0;
837 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
839 info = GetPropW(hWnd, szButtonData);
841 if( msg == WM_CTLCOLORSTATIC &&
842 ( info->attributes & msidbControlAttributesTransparent ) )
844 SetBkMode( (HDC)wParam, TRANSPARENT );
845 return (LRESULT) GetStockObject(NULL_BRUSH);
848 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
849 if ( info->font )
850 SetTextColor( (HDC)wParam, info->font->color );
852 switch( msg )
854 case WM_SETTEXT:
855 msi_text_on_settext( hWnd );
856 break;
857 case WM_NCDESTROY:
858 msi_free( info );
859 RemovePropW( hWnd, szButtonData );
860 break;
863 return r;
866 static UINT msi_dialog_text_control( msi_dialog *dialog, MSIRECORD *rec )
868 msi_control *control;
869 struct msi_text_info *info;
870 LPCWSTR text, ptr, prop, control_name;
871 LPWSTR font_name;
873 TRACE("%p %p\n", dialog, rec);
875 control = msi_dialog_add_control( dialog, rec, szStatic, SS_LEFT | WS_GROUP );
876 if( !control )
877 return ERROR_FUNCTION_FAILED;
879 info = msi_alloc( sizeof *info );
880 if( !info )
881 return ERROR_SUCCESS;
883 control_name = MSI_RecordGetString( rec, 2 );
884 control->attributes = MSI_RecordGetInteger( rec, 8 );
885 prop = MSI_RecordGetString( rec, 9 );
886 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
888 text = MSI_RecordGetString( rec, 10 );
889 font_name = msi_dialog_get_style( text, &ptr );
890 info->font = ( font_name ) ? msi_dialog_find_font( dialog, font_name ) : NULL;
891 msi_free( font_name );
893 info->attributes = MSI_RecordGetInteger( rec, 8 );
894 if( info->attributes & msidbControlAttributesTransparent )
895 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT );
897 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
898 (LONG_PTR)MSIText_WndProc );
899 SetPropW( control->hwnd, szButtonData, info );
901 event_subscribe( dialog, szSelectionPath, control_name, szSelectionPath );
902 return ERROR_SUCCESS;
905 /* strip any leading text style label from text field */
906 static WCHAR *msi_get_binary_name( MSIPACKAGE *package, MSIRECORD *rec )
908 WCHAR *p, *text;
910 text = msi_get_deformatted_field( package, rec, 10 );
911 if (!text)
912 return NULL;
914 p = text;
915 while (*p && *p != '{') p++;
916 if (!*p++) return text;
918 while (*p && *p != '}') p++;
919 if (!*p++) return text;
921 p = strdupW( p );
922 msi_free( text );
923 return p;
926 static UINT msi_dialog_set_property_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
928 static const WCHAR szNullArg[] = {'{','}',0};
929 LPWSTR p, prop, arg_fmt = NULL;
930 UINT len;
932 len = strlenW( event );
933 prop = msi_alloc( len * sizeof(WCHAR) );
934 strcpyW( prop, &event[1] );
935 p = strchrW( prop, ']' );
936 if (p && (p[1] == 0 || p[1] == ' '))
938 *p = 0;
939 if (strcmpW( szNullArg, arg ))
940 deformat_string( dialog->package, arg, &arg_fmt );
941 msi_dialog_set_property( dialog->package, prop, arg_fmt );
942 msi_dialog_update_controls( dialog, prop );
943 msi_free( arg_fmt );
945 else ERR("Badly formatted property string - what happens?\n");
946 msi_free( prop );
947 return ERROR_SUCCESS;
950 static UINT msi_dialog_send_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
952 LPWSTR event_fmt = NULL, arg_fmt = NULL;
954 TRACE("Sending control event %s %s\n", debugstr_w(event), debugstr_w(arg));
956 deformat_string( dialog->package, event, &event_fmt );
957 deformat_string( dialog->package, arg, &arg_fmt );
959 dialog->event_handler( dialog, event_fmt, arg_fmt );
961 msi_free( event_fmt );
962 msi_free( arg_fmt );
964 return ERROR_SUCCESS;
967 static UINT msi_dialog_control_event( MSIRECORD *rec, LPVOID param )
969 msi_dialog *dialog = param;
970 LPCWSTR condition, event, arg;
971 UINT r;
973 condition = MSI_RecordGetString( rec, 5 );
974 r = MSI_EvaluateConditionW( dialog->package, condition );
975 if (r == MSICONDITION_TRUE || r == MSICONDITION_NONE)
977 event = MSI_RecordGetString( rec, 3 );
978 arg = MSI_RecordGetString( rec, 4 );
979 if (event[0] == '[')
980 msi_dialog_set_property_event( dialog, event, arg );
981 else
982 msi_dialog_send_event( dialog, event, arg );
984 return ERROR_SUCCESS;
987 static UINT msi_dialog_button_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
989 static const WCHAR query[] = {
990 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
991 'C','o','n','t','r','o','l','E','v','e','n','t',' ','W','H','E','R','E',' ',
992 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ','A','N','D',' ',
993 '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',' ',
994 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','i','n','g','`',0};
995 MSIQUERY *view;
996 UINT r;
998 if (HIWORD(param) != BN_CLICKED)
999 return ERROR_SUCCESS;
1001 r = MSI_OpenQuery( dialog->package->db, &view, query, dialog->name, control->name );
1002 if (r != ERROR_SUCCESS)
1004 ERR("query failed\n");
1005 return ERROR_SUCCESS;
1007 r = MSI_IterateRecords( view, 0, msi_dialog_control_event, dialog );
1008 msiobj_release( &view->hdr );
1009 return r;
1012 static UINT msi_dialog_button_control( msi_dialog *dialog, MSIRECORD *rec )
1014 msi_control *control;
1015 UINT attributes, style;
1017 TRACE("%p %p\n", dialog, rec);
1019 style = WS_TABSTOP;
1020 attributes = MSI_RecordGetInteger( rec, 8 );
1021 if( attributes & msidbControlAttributesIcon )
1022 style |= BS_ICON;
1024 control = msi_dialog_add_control( dialog, rec, szButton, style );
1025 if( !control )
1026 return ERROR_FUNCTION_FAILED;
1028 control->handler = msi_dialog_button_handler;
1030 if (attributes & msidbControlAttributesIcon)
1032 /* set the icon */
1033 LPWSTR name = msi_get_binary_name( dialog->package, rec );
1034 control->hIcon = msi_load_icon( dialog->package->db, name, attributes );
1035 if (control->hIcon)
1037 SendMessageW( control->hwnd, BM_SETIMAGE, IMAGE_ICON, (LPARAM) control->hIcon );
1039 else
1040 ERR("Failed to load icon %s\n", debugstr_w(name));
1041 msi_free( name );
1044 return ERROR_SUCCESS;
1047 static LPWSTR msi_get_checkbox_value( msi_dialog *dialog, LPCWSTR prop )
1049 static const WCHAR query[] = {
1050 'S','E','L','E','C','T',' ','*',' ',
1051 'F','R','O','M',' ','`','C','h','e','c','k','B','o','x','`',' ',
1052 'W','H','E','R','E',' ',
1053 '`','P','r','o','p','e','r','t','y','`',' ','=',' ',
1054 '\'','%','s','\'',0
1056 MSIRECORD *rec = NULL;
1057 LPWSTR ret = NULL;
1059 /* find if there is a value associated with the checkbox */
1060 rec = MSI_QueryGetRecord( dialog->package->db, query, prop );
1061 if (!rec)
1062 return ret;
1064 ret = msi_get_deformatted_field( dialog->package, rec, 2 );
1065 if( ret && !ret[0] )
1067 msi_free( ret );
1068 ret = NULL;
1070 msiobj_release( &rec->hdr );
1071 if (ret)
1072 return ret;
1074 ret = msi_dup_property( dialog->package->db, prop );
1075 if( ret && !ret[0] )
1077 msi_free( ret );
1078 ret = NULL;
1081 return ret;
1084 static UINT msi_dialog_get_checkbox_state( msi_dialog *dialog, msi_control *control )
1086 WCHAR state[2] = {0};
1087 DWORD sz = 2;
1089 msi_get_property( dialog->package->db, control->property, state, &sz );
1090 return state[0] ? 1 : 0;
1093 static void msi_dialog_set_checkbox_state( msi_dialog *dialog, msi_control *control, UINT state )
1095 static const WCHAR szState[] = {'1',0};
1096 LPCWSTR val;
1098 /* if uncheck then the property is set to NULL */
1099 if (!state)
1101 msi_dialog_set_property( dialog->package, control->property, NULL );
1102 return;
1105 /* check for a custom state */
1106 if (control->value && control->value[0])
1107 val = control->value;
1108 else
1109 val = szState;
1111 msi_dialog_set_property( dialog->package, control->property, val );
1114 static void msi_dialog_checkbox_sync_state( msi_dialog *dialog, msi_control *control )
1116 UINT state = msi_dialog_get_checkbox_state( dialog, control );
1117 SendMessageW( control->hwnd, BM_SETCHECK, state ? BST_CHECKED : BST_UNCHECKED, 0 );
1120 static UINT msi_dialog_checkbox_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
1122 UINT state;
1124 if (HIWORD(param) != BN_CLICKED)
1125 return ERROR_SUCCESS;
1127 TRACE("clicked checkbox %s, set %s\n", debugstr_w(control->name), debugstr_w(control->property));
1129 state = msi_dialog_get_checkbox_state( dialog, control );
1130 state = state ? 0 : 1;
1131 msi_dialog_set_checkbox_state( dialog, control, state );
1132 msi_dialog_checkbox_sync_state( dialog, control );
1134 return msi_dialog_button_handler( dialog, control, param );
1137 static UINT msi_dialog_checkbox_control( msi_dialog *dialog, MSIRECORD *rec )
1139 msi_control *control;
1140 LPCWSTR prop;
1142 TRACE("%p %p\n", dialog, rec);
1144 control = msi_dialog_add_control( dialog, rec, szButton, BS_CHECKBOX | BS_MULTILINE | WS_TABSTOP );
1145 control->handler = msi_dialog_checkbox_handler;
1146 control->update = msi_dialog_checkbox_sync_state;
1147 prop = MSI_RecordGetString( rec, 9 );
1148 if (prop)
1150 control->property = strdupW( prop );
1151 control->value = msi_get_checkbox_value( dialog, prop );
1152 TRACE("control %s value %s\n", debugstr_w(control->property), debugstr_w(control->value));
1154 msi_dialog_checkbox_sync_state( dialog, control );
1155 return ERROR_SUCCESS;
1158 static UINT msi_dialog_line_control( msi_dialog *dialog, MSIRECORD *rec )
1160 DWORD attributes;
1161 LPCWSTR name;
1162 DWORD style, exstyle = 0;
1163 DWORD x, y, width, height;
1164 msi_control *control;
1166 TRACE("%p %p\n", dialog, rec);
1168 style = WS_CHILD | SS_ETCHEDHORZ | SS_SUNKEN;
1170 name = MSI_RecordGetString( rec, 2 );
1171 attributes = MSI_RecordGetInteger( rec, 8 );
1173 if( attributes & msidbControlAttributesVisible )
1174 style |= WS_VISIBLE;
1175 if( ~attributes & msidbControlAttributesEnabled )
1176 style |= WS_DISABLED;
1177 if( attributes & msidbControlAttributesSunken )
1178 exstyle |= WS_EX_CLIENTEDGE;
1180 dialog_map_events( dialog, name );
1182 control = msi_alloc( FIELD_OFFSET(msi_control, name[strlenW( name ) + 1] ));
1183 if (!control)
1184 return ERROR_OUTOFMEMORY;
1186 strcpyW( control->name, name );
1187 list_add_head( &dialog->controls, &control->entry );
1188 control->handler = NULL;
1189 control->property = NULL;
1190 control->value = NULL;
1191 control->hBitmap = NULL;
1192 control->hIcon = NULL;
1193 control->hDll = NULL;
1194 control->tabnext = strdupW( MSI_RecordGetString( rec, 11) );
1195 control->type = strdupW( MSI_RecordGetString( rec, 3 ) );
1196 control->progress_current = 0;
1197 control->progress_max = 100;
1198 control->progress_backwards = FALSE;
1200 x = MSI_RecordGetInteger( rec, 4 );
1201 y = MSI_RecordGetInteger( rec, 5 );
1202 width = MSI_RecordGetInteger( rec, 6 );
1204 x = msi_dialog_scale_unit( dialog, x );
1205 y = msi_dialog_scale_unit( dialog, y );
1206 width = msi_dialog_scale_unit( dialog, width );
1207 height = 2; /* line is exactly 2 units in height */
1209 control->hwnd = CreateWindowExW( exstyle, szStatic, NULL, style,
1210 x, y, width, height, dialog->hwnd, NULL, NULL, NULL );
1212 TRACE("Dialog %s control %s hwnd %p\n",
1213 debugstr_w(dialog->name), debugstr_w(name), control->hwnd );
1215 return ERROR_SUCCESS;
1218 /******************** Scroll Text ********************************************/
1220 struct msi_scrolltext_info
1222 msi_dialog *dialog;
1223 msi_control *control;
1224 WNDPROC oldproc;
1227 static LRESULT WINAPI
1228 MSIScrollText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1230 struct msi_scrolltext_info *info;
1231 HRESULT r;
1233 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1235 info = GetPropW( hWnd, szButtonData );
1237 r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
1239 switch( msg )
1241 case WM_GETDLGCODE:
1242 return DLGC_WANTARROWS;
1243 case WM_NCDESTROY:
1244 msi_free( info );
1245 RemovePropW( hWnd, szButtonData );
1246 break;
1247 case WM_PAINT:
1248 /* native MSI sets a wait cursor here */
1249 msi_dialog_button_handler( info->dialog, info->control, BN_CLICKED );
1250 break;
1252 return r;
1255 struct msi_streamin_info
1257 LPSTR string;
1258 DWORD offset;
1259 DWORD length;
1262 static DWORD CALLBACK
1263 msi_richedit_stream_in( DWORD_PTR arg, LPBYTE buffer, LONG count, LONG *pcb )
1265 struct msi_streamin_info *info = (struct msi_streamin_info*) arg;
1267 if( (count + info->offset) > info->length )
1268 count = info->length - info->offset;
1269 memcpy( buffer, &info->string[ info->offset ], count );
1270 *pcb = count;
1271 info->offset += count;
1273 TRACE("%d/%d\n", info->offset, info->length);
1275 return 0;
1278 static void msi_scrolltext_add_text( msi_control *control, LPCWSTR text )
1280 struct msi_streamin_info info;
1281 EDITSTREAM es;
1283 info.string = strdupWtoA( text );
1284 info.offset = 0;
1285 info.length = lstrlenA( info.string ) + 1;
1287 es.dwCookie = (DWORD_PTR) &info;
1288 es.dwError = 0;
1289 es.pfnCallback = msi_richedit_stream_in;
1291 SendMessageW( control->hwnd, EM_STREAMIN, SF_RTF, (LPARAM) &es );
1293 msi_free( info.string );
1296 static UINT msi_dialog_scrolltext_control( msi_dialog *dialog, MSIRECORD *rec )
1298 static const WCHAR szRichEdit20W[] = {'R','i','c','h','E','d','i','t','2','0','W',0};
1299 struct msi_scrolltext_info *info;
1300 msi_control *control;
1301 HMODULE hRichedit;
1302 LPCWSTR text;
1303 DWORD style;
1305 info = msi_alloc( sizeof *info );
1306 if (!info)
1307 return ERROR_FUNCTION_FAILED;
1309 hRichedit = LoadLibraryA("riched20");
1311 style = WS_BORDER | ES_MULTILINE | WS_VSCROLL |
1312 ES_READONLY | ES_AUTOVSCROLL | WS_TABSTOP;
1313 control = msi_dialog_add_control( dialog, rec, szRichEdit20W, style );
1314 if (!control)
1316 FreeLibrary( hRichedit );
1317 msi_free( info );
1318 return ERROR_FUNCTION_FAILED;
1321 control->hDll = hRichedit;
1323 info->dialog = dialog;
1324 info->control = control;
1326 /* subclass the static control */
1327 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1328 (LONG_PTR)MSIScrollText_WndProc );
1329 SetPropW( control->hwnd, szButtonData, info );
1331 /* add the text into the richedit */
1332 text = MSI_RecordGetString( rec, 10 );
1333 if (text)
1334 msi_scrolltext_add_text( control, text );
1336 return ERROR_SUCCESS;
1339 static HBITMAP msi_load_picture( MSIDATABASE *db, LPCWSTR name,
1340 INT cx, INT cy, DWORD flags )
1342 HBITMAP hOleBitmap = 0, hBitmap = 0, hOldSrcBitmap, hOldDestBitmap;
1343 MSIRECORD *rec = NULL;
1344 IStream *stm = NULL;
1345 IPicture *pic = NULL;
1346 HDC srcdc, destdc;
1347 BITMAP bm;
1348 UINT r;
1350 rec = msi_get_binary_record( db, name );
1351 if( !rec )
1352 goto end;
1354 r = MSI_RecordGetIStream( rec, 2, &stm );
1355 msiobj_release( &rec->hdr );
1356 if( r != ERROR_SUCCESS )
1357 goto end;
1359 r = OleLoadPicture( stm, 0, TRUE, &IID_IPicture, (LPVOID*) &pic );
1360 IStream_Release( stm );
1361 if( FAILED( r ) )
1363 ERR("failed to load picture\n");
1364 goto end;
1367 r = IPicture_get_Handle( pic, (OLE_HANDLE*) &hOleBitmap );
1368 if( FAILED( r ) )
1370 ERR("failed to get bitmap handle\n");
1371 goto end;
1374 /* make the bitmap the desired size */
1375 r = GetObjectW( hOleBitmap, sizeof bm, &bm );
1376 if (r != sizeof bm )
1378 ERR("failed to get bitmap size\n");
1379 goto end;
1382 if (flags & LR_DEFAULTSIZE)
1384 cx = bm.bmWidth;
1385 cy = bm.bmHeight;
1388 srcdc = CreateCompatibleDC( NULL );
1389 hOldSrcBitmap = SelectObject( srcdc, hOleBitmap );
1390 destdc = CreateCompatibleDC( NULL );
1391 hBitmap = CreateCompatibleBitmap( srcdc, cx, cy );
1392 hOldDestBitmap = SelectObject( destdc, hBitmap );
1393 StretchBlt( destdc, 0, 0, cx, cy,
1394 srcdc, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
1395 SelectObject( srcdc, hOldSrcBitmap );
1396 SelectObject( destdc, hOldDestBitmap );
1397 DeleteDC( srcdc );
1398 DeleteDC( destdc );
1400 end:
1401 if ( pic )
1402 IPicture_Release( pic );
1403 return hBitmap;
1406 static UINT msi_dialog_bitmap_control( msi_dialog *dialog, MSIRECORD *rec )
1408 UINT cx, cy, flags, style, attributes;
1409 msi_control *control;
1410 LPWSTR name;
1412 flags = LR_LOADFROMFILE;
1413 style = SS_BITMAP | SS_LEFT | WS_GROUP;
1415 attributes = MSI_RecordGetInteger( rec, 8 );
1416 if( attributes & msidbControlAttributesFixedSize )
1418 flags |= LR_DEFAULTSIZE;
1419 style |= SS_CENTERIMAGE;
1422 control = msi_dialog_add_control( dialog, rec, szStatic, style );
1423 cx = MSI_RecordGetInteger( rec, 6 );
1424 cy = MSI_RecordGetInteger( rec, 7 );
1425 cx = msi_dialog_scale_unit( dialog, cx );
1426 cy = msi_dialog_scale_unit( dialog, cy );
1428 name = msi_get_binary_name( dialog->package, rec );
1429 control->hBitmap = msi_load_picture( dialog->package->db, name, cx, cy, flags );
1430 if( control->hBitmap )
1431 SendMessageW( control->hwnd, STM_SETIMAGE,
1432 IMAGE_BITMAP, (LPARAM) control->hBitmap );
1433 else
1434 ERR("Failed to load bitmap %s\n", debugstr_w(name));
1436 msi_free( name );
1438 return ERROR_SUCCESS;
1441 static UINT msi_dialog_icon_control( msi_dialog *dialog, MSIRECORD *rec )
1443 msi_control *control;
1444 DWORD attributes;
1445 LPWSTR name;
1447 TRACE("\n");
1449 control = msi_dialog_add_control( dialog, rec, szStatic,
1450 SS_ICON | SS_CENTERIMAGE | WS_GROUP );
1452 attributes = MSI_RecordGetInteger( rec, 8 );
1453 name = msi_get_binary_name( dialog->package, rec );
1454 control->hIcon = msi_load_icon( dialog->package->db, name, attributes );
1455 if( control->hIcon )
1456 SendMessageW( control->hwnd, STM_SETICON, (WPARAM) control->hIcon, 0 );
1457 else
1458 ERR("Failed to load bitmap %s\n", debugstr_w(name));
1459 msi_free( name );
1460 return ERROR_SUCCESS;
1463 /******************** Combo Box ***************************************/
1465 struct msi_combobox_info
1467 msi_dialog *dialog;
1468 HWND hwnd;
1469 WNDPROC oldproc;
1470 DWORD num_items;
1471 DWORD addpos_items;
1472 LPWSTR *items;
1475 static LRESULT WINAPI MSIComboBox_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1477 struct msi_combobox_info *info;
1478 LRESULT r;
1479 DWORD j;
1481 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1483 info = GetPropW( hWnd, szButtonData );
1484 if (!info)
1485 return 0;
1487 r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
1489 switch (msg)
1491 case WM_NCDESTROY:
1492 for (j = 0; j < info->num_items; j++)
1493 msi_free( info->items[j] );
1494 msi_free( info->items );
1495 msi_free( info );
1496 RemovePropW( hWnd, szButtonData );
1497 break;
1500 return r;
1503 static UINT msi_combobox_add_item( MSIRECORD *rec, LPVOID param )
1505 struct msi_combobox_info *info = param;
1506 LPCWSTR value, text;
1507 int pos;
1509 value = MSI_RecordGetString( rec, 3 );
1510 text = MSI_RecordGetString( rec, 4 );
1512 info->items[info->addpos_items] = strdupW( value );
1514 pos = SendMessageW( info->hwnd, CB_ADDSTRING, 0, (LPARAM)text );
1515 SendMessageW( info->hwnd, CB_SETITEMDATA, pos, (LPARAM)info->items[info->addpos_items] );
1516 info->addpos_items++;
1518 return ERROR_SUCCESS;
1521 static UINT msi_combobox_add_items( struct msi_combobox_info *info, LPCWSTR property )
1523 static const WCHAR query[] = {
1524 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1525 '`','C','o','m','b','o','B','o','x','`',' ','W','H','E','R','E',' ',
1526 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',' ',
1527 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','`',0};
1528 MSIQUERY *view;
1529 DWORD count;
1530 UINT r;
1532 r = MSI_OpenQuery( info->dialog->package->db, &view, query, property );
1533 if (r != ERROR_SUCCESS)
1534 return r;
1536 /* just get the number of records */
1537 count = 0;
1538 r = MSI_IterateRecords( view, &count, NULL, NULL );
1539 if (r != ERROR_SUCCESS)
1541 msiobj_release( &view->hdr );
1542 return r;
1544 info->num_items = count;
1545 info->items = msi_alloc( sizeof(*info->items) * count );
1547 r = MSI_IterateRecords( view, NULL, msi_combobox_add_item, info );
1548 msiobj_release( &view->hdr );
1549 return r;
1552 static UINT msi_dialog_set_control_condition( MSIRECORD *rec, LPVOID param )
1554 static const WCHAR szHide[] = {'H','i','d','e',0};
1555 static const WCHAR szShow[] = {'S','h','o','w',0};
1556 static const WCHAR szDisable[] = {'D','i','s','a','b','l','e',0};
1557 static const WCHAR szEnable[] = {'E','n','a','b','l','e',0};
1558 static const WCHAR szDefault[] = {'D','e','f','a','u','l','t',0};
1559 msi_dialog *dialog = param;
1560 msi_control *control;
1561 LPCWSTR name, action, condition;
1562 UINT r;
1564 name = MSI_RecordGetString( rec, 2 );
1565 action = MSI_RecordGetString( rec, 3 );
1566 condition = MSI_RecordGetString( rec, 4 );
1567 r = MSI_EvaluateConditionW( dialog->package, condition );
1568 control = msi_dialog_find_control( dialog, name );
1569 if (r == MSICONDITION_TRUE && control)
1571 TRACE("%s control %s\n", debugstr_w(action), debugstr_w(name));
1573 /* FIXME: case sensitive? */
1574 if (!strcmpW( action, szHide ))
1575 ShowWindow(control->hwnd, SW_HIDE);
1576 else if (!strcmpW( action, szShow ))
1577 ShowWindow(control->hwnd, SW_SHOW);
1578 else if (!strcmpW( action, szDisable ))
1579 EnableWindow(control->hwnd, FALSE);
1580 else if (!strcmpW( action, szEnable ))
1581 EnableWindow(control->hwnd, TRUE);
1582 else if (!strcmpW( action, szDefault ))
1583 SetFocus(control->hwnd);
1584 else
1585 FIXME("Unhandled action %s\n", debugstr_w(action));
1587 return ERROR_SUCCESS;
1590 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog )
1592 static const WCHAR query[] = {
1593 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1594 'C','o','n','t','r','o','l','C','o','n','d','i','t','i','o','n',' ',
1595 'W','H','E','R','E',' ','`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
1596 UINT r;
1597 MSIQUERY *view;
1598 MSIPACKAGE *package = dialog->package;
1600 TRACE("%p %s\n", dialog, debugstr_w(dialog->name));
1602 /* query the Control table for all the elements of the control */
1603 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
1604 if (r != ERROR_SUCCESS)
1605 return ERROR_SUCCESS;
1607 r = MSI_IterateRecords( view, 0, msi_dialog_set_control_condition, dialog );
1608 msiobj_release( &view->hdr );
1609 return r;
1612 static UINT msi_dialog_combobox_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
1614 struct msi_combobox_info *info;
1615 int index;
1616 LPWSTR value;
1618 if (HIWORD(param) != CBN_SELCHANGE && HIWORD(param) != CBN_EDITCHANGE)
1619 return ERROR_SUCCESS;
1621 info = GetPropW( control->hwnd, szButtonData );
1622 index = SendMessageW( control->hwnd, CB_GETCURSEL, 0, 0 );
1623 if (index == CB_ERR)
1624 value = msi_get_window_text( control->hwnd );
1625 else
1626 value = (LPWSTR) SendMessageW( control->hwnd, CB_GETITEMDATA, index, 0 );
1628 msi_dialog_set_property( info->dialog->package, control->property, value );
1629 msi_dialog_evaluate_control_conditions( info->dialog );
1631 if (index == CB_ERR)
1632 msi_free( value );
1634 return ERROR_SUCCESS;
1637 static void msi_dialog_combobox_update( msi_dialog *dialog, msi_control *control )
1639 struct msi_combobox_info *info;
1640 LPWSTR value, tmp;
1641 DWORD j;
1643 info = GetPropW( control->hwnd, szButtonData );
1645 value = msi_dup_property( dialog->package->db, control->property );
1646 if (!value)
1648 SendMessageW( control->hwnd, CB_SETCURSEL, -1, 0 );
1649 return;
1652 for (j = 0; j < info->num_items; j++)
1654 tmp = (LPWSTR) SendMessageW( control->hwnd, CB_GETITEMDATA, j, 0 );
1655 if (!strcmpW( value, tmp ))
1656 break;
1659 if (j < info->num_items)
1661 SendMessageW( control->hwnd, CB_SETCURSEL, j, 0 );
1663 else
1665 SendMessageW( control->hwnd, CB_SETCURSEL, -1, 0 );
1666 SetWindowTextW( control->hwnd, value );
1669 msi_free(value);
1672 static UINT msi_dialog_combo_control( msi_dialog *dialog, MSIRECORD *rec )
1674 struct msi_combobox_info *info;
1675 msi_control *control;
1676 DWORD attributes, style;
1677 LPCWSTR prop;
1679 info = msi_alloc( sizeof *info );
1680 if (!info)
1681 return ERROR_FUNCTION_FAILED;
1683 style = CBS_AUTOHSCROLL | WS_TABSTOP | WS_GROUP | WS_CHILD;
1684 attributes = MSI_RecordGetInteger( rec, 8 );
1685 if ( ~attributes & msidbControlAttributesSorted)
1686 style |= CBS_SORT;
1687 if ( attributes & msidbControlAttributesComboList)
1688 style |= CBS_DROPDOWNLIST;
1689 else
1690 style |= CBS_DROPDOWN;
1692 control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
1693 if (!control)
1695 msi_free( info );
1696 return ERROR_FUNCTION_FAILED;
1699 control->handler = msi_dialog_combobox_handler;
1700 control->update = msi_dialog_combobox_update;
1702 prop = MSI_RecordGetString( rec, 9 );
1703 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
1705 /* subclass */
1706 info->dialog = dialog;
1707 info->hwnd = control->hwnd;
1708 info->items = NULL;
1709 info->addpos_items = 0;
1710 info->oldproc = (WNDPROC)SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1711 (LONG_PTR)MSIComboBox_WndProc );
1712 SetPropW( control->hwnd, szButtonData, info );
1714 if (control->property)
1715 msi_combobox_add_items( info, control->property );
1717 msi_dialog_combobox_update( dialog, control );
1719 return ERROR_SUCCESS;
1722 static UINT msi_dialog_edit_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
1724 LPWSTR buf;
1726 if (HIWORD(param) != EN_CHANGE)
1727 return ERROR_SUCCESS;
1729 TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name), debugstr_w(control->property));
1731 buf = msi_get_window_text( control->hwnd );
1732 msi_dialog_set_property( dialog->package, control->property, buf );
1733 msi_free( buf );
1735 return ERROR_SUCCESS;
1738 /* length of 2^32 + 1 */
1739 #define MAX_NUM_DIGITS 11
1741 static UINT msi_dialog_edit_control( msi_dialog *dialog, MSIRECORD *rec )
1743 msi_control *control;
1744 LPCWSTR prop, text;
1745 LPWSTR val, begin, end;
1746 WCHAR num[MAX_NUM_DIGITS];
1747 DWORD limit;
1749 control = msi_dialog_add_control( dialog, rec, szEdit,
1750 WS_BORDER | WS_TABSTOP | ES_AUTOHSCROLL );
1751 control->handler = msi_dialog_edit_handler;
1753 text = MSI_RecordGetString( rec, 10 );
1754 if ( text )
1756 begin = strchrW( text, '{' );
1757 end = strchrW( text, '}' );
1759 if ( begin && end && end > begin &&
1760 begin[0] >= '0' && begin[0] <= '9' &&
1761 end - begin < MAX_NUM_DIGITS)
1763 lstrcpynW( num, begin + 1, end - begin );
1764 limit = atolW( num );
1766 SendMessageW( control->hwnd, EM_SETLIMITTEXT, limit, 0 );
1770 prop = MSI_RecordGetString( rec, 9 );
1771 if( prop )
1772 control->property = strdupW( prop );
1774 val = msi_dup_property( dialog->package->db, control->property );
1775 SetWindowTextW( control->hwnd, val );
1776 msi_free( val );
1777 return ERROR_SUCCESS;
1780 /******************** Masked Edit ********************************************/
1782 #define MASK_MAX_GROUPS 20
1784 struct msi_mask_group
1786 UINT len;
1787 UINT ofs;
1788 WCHAR type;
1789 HWND hwnd;
1792 struct msi_maskedit_info
1794 msi_dialog *dialog;
1795 WNDPROC oldproc;
1796 HWND hwnd;
1797 LPWSTR prop;
1798 UINT num_chars;
1799 UINT num_groups;
1800 struct msi_mask_group group[MASK_MAX_GROUPS];
1803 static BOOL msi_mask_editable( WCHAR type )
1805 switch (type)
1807 case '%':
1808 case '#':
1809 case '&':
1810 case '`':
1811 case '?':
1812 case '^':
1813 return TRUE;
1815 return FALSE;
1818 static void msi_mask_control_change( struct msi_maskedit_info *info )
1820 LPWSTR val;
1821 UINT i, n, r;
1823 val = msi_alloc( (info->num_chars+1)*sizeof(WCHAR) );
1824 for( i=0, n=0; i<info->num_groups; i++ )
1826 if (info->group[i].len == ~0u)
1828 UINT len = SendMessageW( info->group[i].hwnd, WM_GETTEXTLENGTH, 0, 0 );
1829 val = msi_realloc( val, (len + 1) * sizeof(WCHAR) );
1830 GetWindowTextW( info->group[i].hwnd, val, len + 1 );
1832 else
1834 if (info->group[i].len + n > info->num_chars)
1836 ERR("can't fit control %d text into template\n",i);
1837 break;
1839 if (!msi_mask_editable(info->group[i].type))
1841 for(r=0; r<info->group[i].len; r++)
1842 val[n+r] = info->group[i].type;
1843 val[n+r] = 0;
1845 else
1847 r = GetWindowTextW( info->group[i].hwnd, &val[n], info->group[i].len+1 );
1848 if( r != info->group[i].len )
1849 break;
1851 n += r;
1855 TRACE("%d/%d controls were good\n", i, info->num_groups);
1857 if( i == info->num_groups )
1859 TRACE("Set property %s to %s\n", debugstr_w(info->prop), debugstr_w(val));
1860 msi_dialog_set_property( info->dialog->package, info->prop, val );
1861 msi_dialog_evaluate_control_conditions( info->dialog );
1863 msi_free( val );
1866 /* now move to the next control if necessary */
1867 static VOID msi_mask_next_control( struct msi_maskedit_info *info, HWND hWnd )
1869 HWND hWndNext;
1870 UINT len, i;
1872 for( i=0; i<info->num_groups; i++ )
1873 if( info->group[i].hwnd == hWnd )
1874 break;
1876 /* don't move from the last control */
1877 if( i >= (info->num_groups-1) )
1878 return;
1880 len = SendMessageW( hWnd, WM_GETTEXTLENGTH, 0, 0 );
1881 if( len < info->group[i].len )
1882 return;
1884 hWndNext = GetNextDlgTabItem( GetParent( hWnd ), hWnd, FALSE );
1885 SetFocus( hWndNext );
1888 static LRESULT WINAPI
1889 MSIMaskedEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1891 struct msi_maskedit_info *info;
1892 HRESULT r;
1894 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1896 info = GetPropW(hWnd, szButtonData);
1898 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1900 switch( msg )
1902 case WM_COMMAND:
1903 if (HIWORD(wParam) == EN_CHANGE)
1905 msi_mask_control_change( info );
1906 msi_mask_next_control( info, (HWND) lParam );
1908 break;
1909 case WM_NCDESTROY:
1910 msi_free( info->prop );
1911 msi_free( info );
1912 RemovePropW( hWnd, szButtonData );
1913 break;
1916 return r;
1919 /* fish the various bits of the property out and put them in the control */
1920 static void
1921 msi_maskedit_set_text( struct msi_maskedit_info *info, LPCWSTR text )
1923 LPCWSTR p;
1924 UINT i;
1926 p = text;
1927 for( i = 0; i < info->num_groups; i++ )
1929 if( info->group[i].len < strlenW( p ) )
1931 LPWSTR chunk = strdupW( p );
1932 chunk[ info->group[i].len ] = 0;
1933 SetWindowTextW( info->group[i].hwnd, chunk );
1934 msi_free( chunk );
1936 else
1938 SetWindowTextW( info->group[i].hwnd, p );
1939 break;
1941 p += info->group[i].len;
1945 static struct msi_maskedit_info * msi_dialog_parse_groups( LPCWSTR mask )
1947 struct msi_maskedit_info *info;
1948 int i = 0, n = 0, total = 0;
1949 LPCWSTR p;
1951 TRACE("masked control, template %s\n", debugstr_w(mask));
1953 if( !mask )
1954 return NULL;
1956 info = msi_alloc_zero( sizeof *info );
1957 if( !info )
1958 return info;
1960 p = strchrW(mask, '<');
1961 if( p )
1962 p++;
1963 else
1964 p = mask;
1966 for( i=0; i<MASK_MAX_GROUPS; i++ )
1968 /* stop at the end of the string */
1969 if( p[0] == 0 || p[0] == '>' )
1971 if (!total)
1973 /* create a group for the empty mask */
1974 info->group[0].type = '&';
1975 info->group[0].len = ~0u;
1976 i = 1;
1978 break;
1981 /* count the number of the same identifier */
1982 for( n=0; p[n] == p[0]; n++ )
1984 info->group[i].ofs = total;
1985 info->group[i].type = p[0];
1986 if( p[n] == '=' )
1988 n++;
1989 total++; /* an extra not part of the group */
1991 info->group[i].len = n;
1992 total += n;
1993 p += n;
1996 TRACE("%d characters in %d groups\n", total, i );
1997 if( i == MASK_MAX_GROUPS )
1998 ERR("too many groups in PIDTemplate %s\n", debugstr_w(mask));
2000 info->num_chars = total;
2001 info->num_groups = i;
2003 return info;
2006 static void
2007 msi_maskedit_create_children( struct msi_maskedit_info *info, LPCWSTR font )
2009 DWORD width, height, style, wx, ww;
2010 RECT rect;
2011 HWND hwnd;
2012 UINT i;
2014 style = WS_CHILD | WS_BORDER | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL;
2016 GetClientRect( info->hwnd, &rect );
2018 width = rect.right - rect.left;
2019 height = rect.bottom - rect.top;
2021 for( i = 0; i < info->num_groups; i++ )
2023 if (!msi_mask_editable( info->group[i].type ))
2024 continue;
2025 if (info->num_chars)
2027 wx = (info->group[i].ofs * width) / info->num_chars;
2028 ww = (info->group[i].len * width) / info->num_chars;
2030 else
2032 wx = 0;
2033 ww = width;
2035 hwnd = CreateWindowW( szEdit, NULL, style, wx, 0, ww, height,
2036 info->hwnd, NULL, NULL, NULL );
2037 if( !hwnd )
2039 ERR("failed to create mask edit sub window\n");
2040 break;
2043 SendMessageW( hwnd, EM_LIMITTEXT, info->group[i].len, 0 );
2045 msi_dialog_set_font( info->dialog, hwnd,
2046 font?font:info->dialog->default_font );
2047 info->group[i].hwnd = hwnd;
2052 * office 2003 uses "73931<````=````=````=````=`````>@@@@@"
2053 * delphi 7 uses "<????-??????-??????-????>" and "<???-???>"
2054 * filemaker pro 7 uses "<^^^^=^^^^=^^^^=^^^^=^^^^=^^^^=^^^^^>"
2056 static UINT msi_dialog_maskedit_control( msi_dialog *dialog, MSIRECORD *rec )
2058 LPWSTR font_mask, val = NULL, font;
2059 struct msi_maskedit_info *info = NULL;
2060 UINT ret = ERROR_SUCCESS;
2061 msi_control *control;
2062 LPCWSTR prop, mask;
2064 TRACE("\n");
2066 font_mask = msi_get_deformatted_field( dialog->package, rec, 10 );
2067 font = msi_dialog_get_style( font_mask, &mask );
2068 if( !mask )
2070 WARN("mask template is empty\n");
2071 goto end;
2074 info = msi_dialog_parse_groups( mask );
2075 if( !info )
2077 ERR("template %s is invalid\n", debugstr_w(mask));
2078 goto end;
2081 info->dialog = dialog;
2083 control = msi_dialog_add_control( dialog, rec, szStatic,
2084 SS_OWNERDRAW | WS_GROUP | WS_VISIBLE );
2085 if( !control )
2087 ERR("Failed to create maskedit container\n");
2088 ret = ERROR_FUNCTION_FAILED;
2089 goto end;
2091 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
2093 info->hwnd = control->hwnd;
2095 /* subclass the static control */
2096 info->oldproc = (WNDPROC) SetWindowLongPtrW( info->hwnd, GWLP_WNDPROC,
2097 (LONG_PTR)MSIMaskedEdit_WndProc );
2098 SetPropW( control->hwnd, szButtonData, info );
2100 prop = MSI_RecordGetString( rec, 9 );
2101 if( prop )
2102 info->prop = strdupW( prop );
2104 msi_maskedit_create_children( info, font );
2106 if( prop )
2108 val = msi_dup_property( dialog->package->db, prop );
2109 if( val )
2111 msi_maskedit_set_text( info, val );
2112 msi_free( val );
2116 end:
2117 if( ret != ERROR_SUCCESS )
2118 msi_free( info );
2119 msi_free( font_mask );
2120 msi_free( font );
2121 return ret;
2124 /******************** Progress Bar *****************************************/
2126 static UINT msi_dialog_progress_bar( msi_dialog *dialog, MSIRECORD *rec )
2128 msi_control *control;
2129 DWORD attributes, style;
2131 style = WS_VISIBLE;
2132 attributes = MSI_RecordGetInteger( rec, 8 );
2133 if( !(attributes & msidbControlAttributesProgress95) )
2134 style |= PBS_SMOOTH;
2136 control = msi_dialog_add_control( dialog, rec, PROGRESS_CLASSW, style );
2137 if( !control )
2138 return ERROR_FUNCTION_FAILED;
2140 event_subscribe( dialog, szSetProgress, control->name, szProgress );
2141 return ERROR_SUCCESS;
2144 /******************** Path Edit ********************************************/
2146 struct msi_pathedit_info
2148 msi_dialog *dialog;
2149 msi_control *control;
2150 WNDPROC oldproc;
2153 static void msi_dialog_update_pathedit( msi_dialog *dialog, msi_control *control )
2155 LPWSTR prop, path;
2156 BOOL indirect;
2158 if (!control && !(control = msi_dialog_find_control_by_type( dialog, szPathEdit )))
2159 return;
2161 indirect = control->attributes & msidbControlAttributesIndirect;
2162 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2163 path = msi_dialog_dup_property( dialog, prop, TRUE );
2165 SetWindowTextW( control->hwnd, path );
2166 SendMessageW( control->hwnd, EM_SETSEL, 0, -1 );
2168 msi_free( path );
2169 msi_free( prop );
2172 /* FIXME: test when this should fail */
2173 static BOOL msi_dialog_verify_path( LPWSTR path )
2175 if ( !lstrlenW( path ) )
2176 return FALSE;
2178 if ( PathIsRelativeW( path ) )
2179 return FALSE;
2181 return TRUE;
2184 /* returns TRUE if the path is valid, FALSE otherwise */
2185 static BOOL msi_dialog_onkillfocus( msi_dialog *dialog, msi_control *control )
2187 LPWSTR buf, prop;
2188 BOOL indirect;
2189 BOOL valid;
2191 indirect = control->attributes & msidbControlAttributesIndirect;
2192 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2194 buf = msi_get_window_text( control->hwnd );
2196 if ( !msi_dialog_verify_path( buf ) )
2198 /* FIXME: display an error message box */
2199 ERR("Invalid path %s\n", debugstr_w( buf ));
2200 valid = FALSE;
2201 SetFocus( control->hwnd );
2203 else
2205 valid = TRUE;
2206 msi_dialog_set_property( dialog->package, prop, buf );
2209 msi_dialog_update_pathedit( dialog, control );
2211 TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
2212 debugstr_w(prop));
2214 msi_free( buf );
2215 msi_free( prop );
2217 return valid;
2220 static LRESULT WINAPI MSIPathEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2222 struct msi_pathedit_info *info = GetPropW(hWnd, szButtonData);
2223 LRESULT r = 0;
2225 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
2227 if ( msg == WM_KILLFOCUS )
2229 /* if the path is invalid, don't handle this message */
2230 if ( !msi_dialog_onkillfocus( info->dialog, info->control ) )
2231 return 0;
2234 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
2236 if ( msg == WM_NCDESTROY )
2238 msi_free( info );
2239 RemovePropW( hWnd, szButtonData );
2242 return r;
2245 static UINT msi_dialog_pathedit_control( msi_dialog *dialog, MSIRECORD *rec )
2247 struct msi_pathedit_info *info;
2248 msi_control *control;
2249 LPCWSTR prop;
2251 info = msi_alloc( sizeof *info );
2252 if (!info)
2253 return ERROR_FUNCTION_FAILED;
2255 control = msi_dialog_add_control( dialog, rec, szEdit,
2256 WS_BORDER | WS_TABSTOP );
2257 control->attributes = MSI_RecordGetInteger( rec, 8 );
2258 prop = MSI_RecordGetString( rec, 9 );
2259 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2261 info->dialog = dialog;
2262 info->control = control;
2263 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2264 (LONG_PTR)MSIPathEdit_WndProc );
2265 SetPropW( control->hwnd, szButtonData, info );
2267 msi_dialog_update_pathedit( dialog, control );
2269 return ERROR_SUCCESS;
2272 static UINT msi_dialog_radiogroup_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
2274 if (HIWORD(param) != BN_CLICKED)
2275 return ERROR_SUCCESS;
2277 TRACE("clicked radio button %s, set %s\n", debugstr_w(control->name), debugstr_w(control->property));
2279 msi_dialog_set_property( dialog->package, control->property, control->name );
2281 return msi_dialog_button_handler( dialog, control, param );
2284 /* radio buttons are a bit different from normal controls */
2285 static UINT msi_dialog_create_radiobutton( MSIRECORD *rec, LPVOID param )
2287 radio_button_group_descr *group = param;
2288 msi_dialog *dialog = group->dialog;
2289 msi_control *control;
2290 LPCWSTR prop, text, name;
2291 DWORD style, attributes = group->attributes;
2293 style = WS_CHILD | BS_AUTORADIOBUTTON | BS_MULTILINE | WS_TABSTOP;
2294 name = MSI_RecordGetString( rec, 3 );
2295 text = MSI_RecordGetString( rec, 8 );
2296 if( attributes & msidbControlAttributesVisible )
2297 style |= WS_VISIBLE;
2298 if( ~attributes & msidbControlAttributesEnabled )
2299 style |= WS_DISABLED;
2301 control = dialog_create_window( dialog, rec, 0, szButton, name, text, style,
2302 group->parent->hwnd );
2303 if (!control)
2304 return ERROR_FUNCTION_FAILED;
2305 control->handler = msi_dialog_radiogroup_handler;
2307 if (group->propval && !strcmpW( control->name, group->propval ))
2308 SendMessageW(control->hwnd, BM_SETCHECK, BST_CHECKED, 0);
2310 prop = MSI_RecordGetString( rec, 1 );
2311 if( prop )
2312 control->property = strdupW( prop );
2314 return ERROR_SUCCESS;
2317 static BOOL CALLBACK msi_radioground_child_enum( HWND hWnd, LPARAM lParam )
2319 EnableWindow( hWnd, lParam );
2320 return TRUE;
2323 static LRESULT WINAPI MSIRadioGroup_WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
2325 WNDPROC oldproc = (WNDPROC)GetPropW( hWnd, szButtonData );
2326 LRESULT r;
2328 TRACE("hWnd %p msg %04x wParam 0x%08lx lParam 0x%08lx\n", hWnd, msg, wParam, lParam);
2330 if (msg == WM_COMMAND) /* Forward notifications to dialog */
2331 SendMessageW( GetParent( hWnd ), msg, wParam, lParam );
2333 r = CallWindowProcW( oldproc, hWnd, msg, wParam, lParam );
2335 /* make sure the radio buttons show as disabled if the parent is disabled */
2336 if (msg == WM_ENABLE)
2337 EnumChildWindows( hWnd, msi_radioground_child_enum, wParam );
2339 return r;
2342 static UINT msi_dialog_radiogroup_control( msi_dialog *dialog, MSIRECORD *rec )
2344 static const WCHAR query[] = {
2345 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2346 'R','a','d','i','o','B','u','t','t','o','n',' ','W','H','E','R','E',' ',
2347 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
2348 UINT r;
2349 LPCWSTR prop;
2350 msi_control *control;
2351 MSIQUERY *view;
2352 radio_button_group_descr group;
2353 MSIPACKAGE *package = dialog->package;
2354 WNDPROC oldproc;
2355 DWORD attr, style = WS_GROUP;
2357 prop = MSI_RecordGetString( rec, 9 );
2359 TRACE("%p %p %s\n", dialog, rec, debugstr_w( prop ));
2361 attr = MSI_RecordGetInteger( rec, 8 );
2362 if (attr & msidbControlAttributesHasBorder)
2363 style |= BS_GROUPBOX;
2364 else
2365 style |= BS_OWNERDRAW;
2367 /* Create parent group box to hold radio buttons */
2368 control = msi_dialog_add_control( dialog, rec, szButton, style );
2369 if( !control )
2370 return ERROR_FUNCTION_FAILED;
2372 oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2373 (LONG_PTR)MSIRadioGroup_WndProc );
2374 SetPropW(control->hwnd, szButtonData, oldproc);
2375 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
2377 if( prop )
2378 control->property = strdupW( prop );
2380 /* query the Radio Button table for all control in this group */
2381 r = MSI_OpenQuery( package->db, &view, query, prop );
2382 if( r != ERROR_SUCCESS )
2384 ERR("query failed for dialog %s radio group %s\n",
2385 debugstr_w(dialog->name), debugstr_w(prop));
2386 return ERROR_INVALID_PARAMETER;
2389 group.dialog = dialog;
2390 group.parent = control;
2391 group.attributes = MSI_RecordGetInteger( rec, 8 );
2392 group.propval = msi_dup_property( dialog->package->db, control->property );
2394 r = MSI_IterateRecords( view, 0, msi_dialog_create_radiobutton, &group );
2395 msiobj_release( &view->hdr );
2396 msi_free( group.propval );
2397 return r;
2400 static void
2401 msi_seltree_sync_item_state( HWND hwnd, MSIFEATURE *feature, HTREEITEM hItem )
2403 TVITEMW tvi;
2404 DWORD index = feature->ActionRequest;
2406 TRACE("Feature %s -> %d %d %d\n", debugstr_w(feature->Title),
2407 feature->Installed, feature->Action, feature->ActionRequest);
2409 if (index == INSTALLSTATE_UNKNOWN)
2410 index = INSTALLSTATE_ABSENT;
2412 tvi.mask = TVIF_STATE;
2413 tvi.hItem = hItem;
2414 tvi.state = INDEXTOSTATEIMAGEMASK( index );
2415 tvi.stateMask = TVIS_STATEIMAGEMASK;
2417 SendMessageW( hwnd, TVM_SETITEMW, 0, (LPARAM) &tvi );
2420 static UINT
2421 msi_seltree_popup_menu( HWND hwnd, INT x, INT y )
2423 HMENU hMenu;
2424 INT r;
2426 /* create a menu to display */
2427 hMenu = CreatePopupMenu();
2429 /* FIXME: load strings from resources */
2430 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_LOCAL, "Install feature locally");
2431 AppendMenuA( hMenu, MF_ENABLED, USER_INSTALLSTATE_ALL, "Install entire feature");
2432 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ADVERTISED, "Install on demand");
2433 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ABSENT, "Don't install");
2434 r = TrackPopupMenu( hMenu, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RETURNCMD,
2435 x, y, 0, hwnd, NULL );
2436 DestroyMenu( hMenu );
2437 return r;
2440 static void
2441 msi_seltree_update_feature_installstate( HWND hwnd, HTREEITEM hItem,
2442 MSIPACKAGE *package, MSIFEATURE *feature, INSTALLSTATE state )
2444 feature->ActionRequest = state;
2445 msi_seltree_sync_item_state( hwnd, feature, hItem );
2446 ACTION_UpdateComponentStates( package, feature );
2449 static void
2450 msi_seltree_update_siblings_and_children_installstate( HWND hwnd, HTREEITEM curr,
2451 MSIPACKAGE *package, INSTALLSTATE state)
2453 /* update all siblings */
2456 MSIFEATURE *feature;
2457 HTREEITEM child;
2459 feature = msi_seltree_feature_from_item( hwnd, curr );
2460 msi_seltree_update_feature_installstate( hwnd, curr, package, feature, state );
2462 /* update this sibling's children */
2463 child = (HTREEITEM)SendMessageW( hwnd, TVM_GETNEXTITEM, (WPARAM)TVGN_CHILD, (LPARAM)curr );
2464 if (child)
2465 msi_seltree_update_siblings_and_children_installstate( hwnd, child,
2466 package, state );
2468 while ((curr = (HTREEITEM)SendMessageW( hwnd, TVM_GETNEXTITEM, (WPARAM)TVGN_NEXT, (LPARAM)curr )));
2471 static LRESULT
2472 msi_seltree_menu( HWND hwnd, HTREEITEM hItem )
2474 struct msi_selection_tree_info *info;
2475 MSIFEATURE *feature;
2476 MSIPACKAGE *package;
2477 union {
2478 RECT rc;
2479 POINT pt[2];
2480 HTREEITEM hItem;
2481 } u;
2482 UINT r;
2484 info = GetPropW(hwnd, szButtonData);
2485 package = info->dialog->package;
2487 feature = msi_seltree_feature_from_item( hwnd, hItem );
2488 if (!feature)
2490 ERR("item %p feature was NULL\n", hItem);
2491 return 0;
2494 /* get the item's rectangle to put the menu just below it */
2495 u.hItem = hItem;
2496 SendMessageW( hwnd, TVM_GETITEMRECT, 0, (LPARAM) &u.rc );
2497 MapWindowPoints( hwnd, NULL, u.pt, 2 );
2499 r = msi_seltree_popup_menu( hwnd, u.rc.left, u.rc.top );
2501 switch (r)
2503 case USER_INSTALLSTATE_ALL:
2504 r = INSTALLSTATE_LOCAL;
2505 /* fall-through */
2506 case INSTALLSTATE_ADVERTISED:
2507 case INSTALLSTATE_ABSENT:
2509 HTREEITEM child;
2510 child = (HTREEITEM)SendMessageW( hwnd, TVM_GETNEXTITEM, (WPARAM)TVGN_CHILD, (LPARAM)hItem );
2511 if (child)
2512 msi_seltree_update_siblings_and_children_installstate( hwnd, child, package, r );
2514 /* fall-through */
2515 case INSTALLSTATE_LOCAL:
2516 msi_seltree_update_feature_installstate( hwnd, hItem, package, feature, r );
2517 break;
2520 return 0;
2523 static LRESULT WINAPI
2524 MSISelectionTree_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2526 struct msi_selection_tree_info *info;
2527 TVHITTESTINFO tvhti;
2528 HRESULT r;
2530 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
2532 info = GetPropW(hWnd, szButtonData);
2534 switch( msg )
2536 case WM_LBUTTONDOWN:
2537 tvhti.pt.x = (short)LOWORD( lParam );
2538 tvhti.pt.y = (short)HIWORD( lParam );
2539 tvhti.flags = 0;
2540 tvhti.hItem = 0;
2541 CallWindowProcW(info->oldproc, hWnd, TVM_HITTEST, 0, (LPARAM) &tvhti );
2542 if (tvhti.flags & TVHT_ONITEMSTATEICON)
2543 return msi_seltree_menu( hWnd, tvhti.hItem );
2544 break;
2546 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
2548 switch( msg )
2550 case WM_NCDESTROY:
2551 msi_free( info );
2552 RemovePropW( hWnd, szButtonData );
2553 break;
2555 return r;
2558 static void
2559 msi_seltree_add_child_features( MSIPACKAGE *package, HWND hwnd,
2560 LPCWSTR parent, HTREEITEM hParent )
2562 struct msi_selection_tree_info *info = GetPropW( hwnd, szButtonData );
2563 MSIFEATURE *feature;
2564 TVINSERTSTRUCTW tvis;
2565 HTREEITEM hitem, hfirst = NULL;
2567 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
2569 if ( parent && feature->Feature_Parent && strcmpW( parent, feature->Feature_Parent ))
2570 continue;
2571 else if ( parent && !feature->Feature_Parent )
2572 continue;
2573 else if ( !parent && feature->Feature_Parent )
2574 continue;
2576 if ( !feature->Title )
2577 continue;
2579 if ( !feature->Display )
2580 continue;
2582 memset( &tvis, 0, sizeof tvis );
2583 tvis.hParent = hParent;
2584 tvis.hInsertAfter = TVI_LAST;
2585 tvis.u.item.mask = TVIF_TEXT | TVIF_PARAM;
2586 tvis.u.item.pszText = feature->Title;
2587 tvis.u.item.lParam = (LPARAM) feature;
2589 hitem = (HTREEITEM) SendMessageW( hwnd, TVM_INSERTITEMW, 0, (LPARAM) &tvis );
2590 if (!hitem)
2591 continue;
2593 if (!hfirst)
2594 hfirst = hitem;
2596 msi_seltree_sync_item_state( hwnd, feature, hitem );
2597 msi_seltree_add_child_features( package, hwnd,
2598 feature->Feature, hitem );
2600 /* the node is expanded if Display is odd */
2601 if ( feature->Display % 2 != 0 )
2602 SendMessageW( hwnd, TVM_EXPAND, TVE_EXPAND, (LPARAM) hitem );
2605 /* select the first item */
2606 SendMessageW( hwnd, TVM_SELECTITEM, TVGN_CARET | TVGN_DROPHILITE, (LPARAM) hfirst );
2607 info->selected = hfirst;
2610 static void msi_seltree_create_imagelist( HWND hwnd )
2612 const int bm_width = 32, bm_height = 16, bm_count = 3;
2613 const int bm_resource = 0x1001;
2614 HIMAGELIST himl;
2615 int i;
2616 HBITMAP hbmp;
2618 himl = ImageList_Create( bm_width, bm_height, FALSE, 4, 0 );
2619 if (!himl)
2621 ERR("failed to create image list\n");
2622 return;
2625 for (i=0; i<bm_count; i++)
2627 hbmp = LoadBitmapW( msi_hInstance, MAKEINTRESOURCEW(i+bm_resource) );
2628 if (!hbmp)
2630 ERR("failed to load bitmap %d\n", i);
2631 break;
2635 * Add a dummy bitmap at offset zero because the treeview
2636 * can't use it as a state mask (zero means no user state).
2638 if (!i)
2639 ImageList_Add( himl, hbmp, NULL );
2641 ImageList_Add( himl, hbmp, NULL );
2644 SendMessageW( hwnd, TVM_SETIMAGELIST, TVSIL_STATE, (LPARAM)himl );
2647 static UINT msi_dialog_seltree_handler( msi_dialog *dialog,
2648 msi_control *control, WPARAM param )
2650 struct msi_selection_tree_info *info = GetPropW( control->hwnd, szButtonData );
2651 LPNMTREEVIEWW tv = (LPNMTREEVIEWW)param;
2652 MSIRECORD *row, *rec;
2653 MSIFOLDER *folder;
2654 MSIFEATURE *feature;
2655 LPCWSTR dir, title = NULL;
2656 UINT r = ERROR_SUCCESS;
2658 static const WCHAR select[] = {
2659 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2660 '`','F','e','a','t','u','r','e','`',' ','W','H','E','R','E',' ',
2661 '`','T','i','t','l','e','`',' ','=',' ','\'','%','s','\'',0
2664 if (tv->hdr.code != TVN_SELCHANGINGW)
2665 return ERROR_SUCCESS;
2667 info->selected = tv->itemNew.hItem;
2669 if (!(tv->itemNew.mask & TVIF_TEXT))
2671 feature = msi_seltree_feature_from_item( control->hwnd, tv->itemNew.hItem );
2672 if (feature)
2673 title = feature->Title;
2675 else
2676 title = tv->itemNew.pszText;
2678 row = MSI_QueryGetRecord( dialog->package->db, select, title );
2679 if (!row)
2680 return ERROR_FUNCTION_FAILED;
2682 rec = MSI_CreateRecord( 1 );
2684 MSI_RecordSetStringW( rec, 1, MSI_RecordGetString( row, 4 ) );
2685 msi_event_fire( dialog->package, szSelectionDescription, rec );
2687 dir = MSI_RecordGetString( row, 7 );
2688 if (dir)
2690 folder = msi_get_loaded_folder( dialog->package, dir );
2691 if (!folder)
2693 r = ERROR_FUNCTION_FAILED;
2694 goto done;
2696 MSI_RecordSetStringW( rec, 1, folder->ResolvedTarget );
2698 else
2699 MSI_RecordSetStringW( rec, 1, NULL );
2701 msi_event_fire( dialog->package, szSelectionPath, rec );
2703 done:
2704 msiobj_release(&row->hdr);
2705 msiobj_release(&rec->hdr);
2707 return r;
2710 static UINT msi_dialog_selection_tree( msi_dialog *dialog, MSIRECORD *rec )
2712 msi_control *control;
2713 LPCWSTR prop, control_name;
2714 MSIPACKAGE *package = dialog->package;
2715 DWORD style;
2716 struct msi_selection_tree_info *info;
2718 info = msi_alloc( sizeof *info );
2719 if (!info)
2720 return ERROR_FUNCTION_FAILED;
2722 /* create the treeview control */
2723 style = TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT;
2724 style |= WS_GROUP | WS_VSCROLL;
2725 control = msi_dialog_add_control( dialog, rec, WC_TREEVIEWW, style );
2726 if (!control)
2728 msi_free(info);
2729 return ERROR_FUNCTION_FAILED;
2732 control->handler = msi_dialog_seltree_handler;
2733 control_name = MSI_RecordGetString( rec, 2 );
2734 control->attributes = MSI_RecordGetInteger( rec, 8 );
2735 prop = MSI_RecordGetString( rec, 9 );
2736 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2738 /* subclass */
2739 info->dialog = dialog;
2740 info->hwnd = control->hwnd;
2741 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2742 (LONG_PTR)MSISelectionTree_WndProc );
2743 SetPropW( control->hwnd, szButtonData, info );
2745 event_subscribe( dialog, szSelectionPath, control_name, szProperty );
2747 /* initialize it */
2748 msi_seltree_create_imagelist( control->hwnd );
2749 msi_seltree_add_child_features( package, control->hwnd, NULL, NULL );
2751 return ERROR_SUCCESS;
2754 /******************** Group Box ***************************************/
2756 static UINT msi_dialog_group_box( msi_dialog *dialog, MSIRECORD *rec )
2758 msi_control *control;
2759 DWORD style;
2761 style = BS_GROUPBOX | WS_CHILD | WS_GROUP;
2762 control = msi_dialog_add_control( dialog, rec, WC_BUTTONW, style );
2763 if (!control)
2764 return ERROR_FUNCTION_FAILED;
2766 return ERROR_SUCCESS;
2769 /******************** List Box ***************************************/
2771 struct msi_listbox_info
2773 msi_dialog *dialog;
2774 HWND hwnd;
2775 WNDPROC oldproc;
2776 DWORD num_items;
2777 DWORD addpos_items;
2778 LPWSTR *items;
2781 static LRESULT WINAPI MSIListBox_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2783 struct msi_listbox_info *info;
2784 LRESULT r;
2785 DWORD j;
2787 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
2789 info = GetPropW( hWnd, szButtonData );
2790 if (!info)
2791 return 0;
2793 r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
2795 switch( msg )
2797 case WM_NCDESTROY:
2798 for (j = 0; j < info->num_items; j++)
2799 msi_free( info->items[j] );
2800 msi_free( info->items );
2801 msi_free( info );
2802 RemovePropW( hWnd, szButtonData );
2803 break;
2806 return r;
2809 static UINT msi_listbox_add_item( MSIRECORD *rec, LPVOID param )
2811 struct msi_listbox_info *info = param;
2812 LPCWSTR value, text;
2813 int pos;
2815 value = MSI_RecordGetString( rec, 3 );
2816 text = MSI_RecordGetString( rec, 4 );
2818 info->items[info->addpos_items] = strdupW( value );
2820 pos = SendMessageW( info->hwnd, LB_ADDSTRING, 0, (LPARAM)text );
2821 SendMessageW( info->hwnd, LB_SETITEMDATA, pos, (LPARAM)info->items[info->addpos_items] );
2822 info->addpos_items++;
2823 return ERROR_SUCCESS;
2826 static UINT msi_listbox_add_items( struct msi_listbox_info *info, LPCWSTR property )
2828 static const WCHAR query[] = {
2829 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2830 '`','L','i','s','t','B','o','x','`',' ','W','H','E','R','E',' ',
2831 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',' ',
2832 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','`',0};
2833 MSIQUERY *view;
2834 DWORD count;
2835 UINT r;
2837 r = MSI_OpenQuery( info->dialog->package->db, &view, query, property );
2838 if ( r != ERROR_SUCCESS )
2839 return r;
2841 /* just get the number of records */
2842 count = 0;
2843 r = MSI_IterateRecords( view, &count, NULL, NULL );
2844 if (r != ERROR_SUCCESS)
2846 msiobj_release( &view->hdr );
2847 return r;
2849 info->num_items = count;
2850 info->items = msi_alloc( sizeof(*info->items) * count );
2852 r = MSI_IterateRecords( view, NULL, msi_listbox_add_item, info );
2853 msiobj_release( &view->hdr );
2854 return r;
2857 static UINT msi_dialog_listbox_handler( msi_dialog *dialog,
2858 msi_control *control, WPARAM param )
2860 struct msi_listbox_info *info;
2861 int index;
2862 LPCWSTR value;
2864 if( HIWORD(param) != LBN_SELCHANGE )
2865 return ERROR_SUCCESS;
2867 info = GetPropW( control->hwnd, szButtonData );
2868 index = SendMessageW( control->hwnd, LB_GETCURSEL, 0, 0 );
2869 value = (LPCWSTR) SendMessageW( control->hwnd, LB_GETITEMDATA, index, 0 );
2871 msi_dialog_set_property( info->dialog->package, control->property, value );
2872 msi_dialog_evaluate_control_conditions( info->dialog );
2874 return ERROR_SUCCESS;
2877 static UINT msi_dialog_list_box( msi_dialog *dialog, MSIRECORD *rec )
2879 struct msi_listbox_info *info;
2880 msi_control *control;
2881 DWORD attributes, style;
2882 LPCWSTR prop;
2884 info = msi_alloc( sizeof *info );
2885 if (!info)
2886 return ERROR_FUNCTION_FAILED;
2888 style = WS_TABSTOP | WS_GROUP | WS_CHILD | LBS_NOTIFY | WS_VSCROLL | WS_BORDER;
2889 attributes = MSI_RecordGetInteger( rec, 8 );
2890 if (~attributes & msidbControlAttributesSorted)
2891 style |= LBS_SORT;
2893 control = msi_dialog_add_control( dialog, rec, WC_LISTBOXW, style );
2894 if (!control)
2896 msi_free(info);
2897 return ERROR_FUNCTION_FAILED;
2900 control->handler = msi_dialog_listbox_handler;
2902 prop = MSI_RecordGetString( rec, 9 );
2903 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2905 /* subclass */
2906 info->dialog = dialog;
2907 info->hwnd = control->hwnd;
2908 info->items = NULL;
2909 info->addpos_items = 0;
2910 info->oldproc = (WNDPROC)SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2911 (LONG_PTR)MSIListBox_WndProc );
2912 SetPropW( control->hwnd, szButtonData, info );
2914 if ( control->property )
2915 msi_listbox_add_items( info, control->property );
2917 return ERROR_SUCCESS;
2920 /******************** Directory Combo ***************************************/
2922 static void msi_dialog_update_directory_combo( msi_dialog *dialog, msi_control *control )
2924 LPWSTR prop, path;
2925 BOOL indirect;
2927 if (!control && !(control = msi_dialog_find_control_by_type( dialog, szDirectoryCombo )))
2928 return;
2930 indirect = control->attributes & msidbControlAttributesIndirect;
2931 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2932 path = msi_dialog_dup_property( dialog, prop, TRUE );
2934 PathStripPathW( path );
2935 PathRemoveBackslashW( path );
2937 SendMessageW( control->hwnd, CB_INSERTSTRING, 0, (LPARAM)path );
2938 SendMessageW( control->hwnd, CB_SETCURSEL, 0, 0 );
2940 msi_free( path );
2941 msi_free( prop );
2944 static UINT msi_dialog_directory_combo( msi_dialog *dialog, MSIRECORD *rec )
2946 msi_control *control;
2947 LPCWSTR prop;
2948 DWORD style;
2950 /* FIXME: use CBS_OWNERDRAWFIXED and add owner draw code */
2951 style = CBS_DROPDOWNLIST | CBS_HASSTRINGS | WS_CHILD |
2952 WS_GROUP | WS_TABSTOP | WS_VSCROLL;
2953 control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
2954 if (!control)
2955 return ERROR_FUNCTION_FAILED;
2957 control->attributes = MSI_RecordGetInteger( rec, 8 );
2958 prop = MSI_RecordGetString( rec, 9 );
2959 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2961 msi_dialog_update_directory_combo( dialog, control );
2963 return ERROR_SUCCESS;
2966 /******************** Directory List ***************************************/
2968 static void msi_dialog_update_directory_list( msi_dialog *dialog, msi_control *control )
2970 WCHAR dir_spec[MAX_PATH];
2971 WIN32_FIND_DATAW wfd;
2972 LPWSTR prop, path;
2973 BOOL indirect;
2974 LVITEMW item;
2975 HANDLE file;
2977 static const WCHAR asterisk[] = {'*',0};
2979 if (!control && !(control = msi_dialog_find_control_by_type( dialog, szDirectoryList )))
2980 return;
2982 /* clear the list-view */
2983 SendMessageW( control->hwnd, LVM_DELETEALLITEMS, 0, 0 );
2985 indirect = control->attributes & msidbControlAttributesIndirect;
2986 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2987 path = msi_dialog_dup_property( dialog, prop, TRUE );
2989 lstrcpyW( dir_spec, path );
2990 lstrcatW( dir_spec, asterisk );
2992 file = FindFirstFileW( dir_spec, &wfd );
2993 if ( file == INVALID_HANDLE_VALUE )
2994 return;
2998 if ( wfd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY )
2999 continue;
3001 if ( !strcmpW( wfd.cFileName, szDot ) || !strcmpW( wfd.cFileName, szDotDot ) )
3002 continue;
3004 item.mask = LVIF_TEXT;
3005 item.cchTextMax = MAX_PATH;
3006 item.iItem = 0;
3007 item.iSubItem = 0;
3008 item.pszText = wfd.cFileName;
3010 SendMessageW( control->hwnd, LVM_INSERTITEMW, 0, (LPARAM)&item );
3011 } while ( FindNextFileW( file, &wfd ) );
3013 msi_free( prop );
3014 msi_free( path );
3015 FindClose( file );
3018 static UINT msi_dialog_directorylist_up( msi_dialog *dialog )
3020 msi_control *control;
3021 LPWSTR prop, path, ptr;
3022 BOOL indirect;
3024 control = msi_dialog_find_control_by_type( dialog, szDirectoryList );
3025 indirect = control->attributes & msidbControlAttributesIndirect;
3026 prop = msi_dialog_dup_property( dialog, control->property, indirect );
3027 path = msi_dialog_dup_property( dialog, prop, TRUE );
3029 /* strip off the last directory */
3030 ptr = PathFindFileNameW( path );
3031 if (ptr != path) *(ptr - 1) = '\0';
3032 PathAddBackslashW( path );
3034 msi_dialog_set_property( dialog->package, prop, path );
3036 msi_dialog_update_directory_list( dialog, NULL );
3037 msi_dialog_update_directory_combo( dialog, NULL );
3038 msi_dialog_update_pathedit( dialog, NULL );
3040 msi_free( path );
3041 msi_free( prop );
3043 return ERROR_SUCCESS;
3046 static UINT msi_dialog_dirlist_handler( msi_dialog *dialog,
3047 msi_control *control, WPARAM param )
3049 LPNMHDR nmhdr = (LPNMHDR)param;
3050 WCHAR new_path[MAX_PATH];
3051 WCHAR text[MAX_PATH];
3052 LPWSTR path, prop;
3053 BOOL indirect;
3054 LVITEMW item;
3055 int index;
3057 if (nmhdr->code != LVN_ITEMACTIVATE)
3058 return ERROR_SUCCESS;
3060 index = SendMessageW( control->hwnd, LVM_GETNEXTITEM, -1, LVNI_SELECTED );
3061 if ( index < 0 )
3063 ERR("No list-view item selected!\n");
3064 return ERROR_FUNCTION_FAILED;
3067 item.iSubItem = 0;
3068 item.pszText = text;
3069 item.cchTextMax = MAX_PATH;
3070 SendMessageW( control->hwnd, LVM_GETITEMTEXTW, index, (LPARAM)&item );
3072 indirect = control->attributes & msidbControlAttributesIndirect;
3073 prop = msi_dialog_dup_property( dialog, control->property, indirect );
3074 path = msi_dialog_dup_property( dialog, prop, TRUE );
3076 lstrcpyW( new_path, path );
3077 lstrcatW( new_path, text );
3078 lstrcatW( new_path, szBackSlash );
3080 msi_dialog_set_property( dialog->package, prop, new_path );
3082 msi_dialog_update_directory_list( dialog, NULL );
3083 msi_dialog_update_directory_combo( dialog, NULL );
3084 msi_dialog_update_pathedit( dialog, NULL );
3086 msi_free( prop );
3087 msi_free( path );
3088 return ERROR_SUCCESS;
3091 static UINT msi_dialog_directory_list( msi_dialog *dialog, MSIRECORD *rec )
3093 msi_control *control;
3094 LPCWSTR prop;
3095 DWORD style;
3097 style = LVS_LIST | WS_VSCROLL | LVS_SHAREIMAGELISTS |
3098 LVS_AUTOARRANGE | LVS_SINGLESEL | WS_BORDER |
3099 LVS_SORTASCENDING | WS_CHILD | WS_GROUP | WS_TABSTOP;
3100 control = msi_dialog_add_control( dialog, rec, WC_LISTVIEWW, style );
3101 if (!control)
3102 return ERROR_FUNCTION_FAILED;
3104 control->attributes = MSI_RecordGetInteger( rec, 8 );
3105 control->handler = msi_dialog_dirlist_handler;
3106 prop = MSI_RecordGetString( rec, 9 );
3107 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
3109 /* double click to activate an item in the list */
3110 SendMessageW( control->hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE,
3111 0, LVS_EX_TWOCLICKACTIVATE );
3113 msi_dialog_update_directory_list( dialog, control );
3115 return ERROR_SUCCESS;
3118 /******************** VolumeCost List ***************************************/
3120 static BOOL str_is_number( LPCWSTR str )
3122 int i;
3124 for (i = 0; i < lstrlenW( str ); i++)
3125 if (!isdigitW(str[i]))
3126 return FALSE;
3128 return TRUE;
3131 static const WCHAR column_keys[][80] =
3133 {'V','o','l','u','m','e','C','o','s','t','V','o','l','u','m','e',0},
3134 {'V','o','l','u','m','e','C','o','s','t','S','i','z','e',0},
3135 {'V','o','l','u','m','e','C','o','s','t','A','v','a','i','l','a','b','l','e',0},
3136 {'V','o','l','u','m','e','C','o','s','t','R','e','q','u','i','r','e','d',0},
3137 {'V','o','l','u','m','e','C','o','s','t','D','i','f','f','e','r','e','n','c','e',0}
3140 static void msi_dialog_vcl_add_columns( msi_dialog *dialog, msi_control *control, MSIRECORD *rec )
3142 LPCWSTR text = MSI_RecordGetString( rec, 10 );
3143 LPCWSTR begin = text, end;
3144 WCHAR *num;
3145 LVCOLUMNW lvc;
3146 DWORD count = 0;
3148 static const WCHAR negative[] = {'-',0};
3150 if (!text) return;
3152 while ((begin = strchrW( begin, '{' )) && count < 5)
3154 if (!(end = strchrW( begin, '}' )))
3155 return;
3157 num = msi_alloc( (end-begin+1)*sizeof(WCHAR) );
3158 if (!num)
3159 return;
3161 lstrcpynW( num, begin + 1, end - begin );
3162 begin += end - begin + 1;
3164 /* empty braces or '0' hides the column */
3165 if ( !num[0] || !strcmpW( num, szZero ) )
3167 count++;
3168 msi_free( num );
3169 continue;
3172 /* the width must be a positive number
3173 * if a width is invalid, all remaining columns are hidden
3175 if ( !strncmpW( num, negative, 1 ) || !str_is_number( num ) ) {
3176 msi_free( num );
3177 return;
3180 ZeroMemory( &lvc, sizeof(lvc) );
3181 lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
3182 lvc.cx = atolW( num );
3183 lvc.pszText = msi_dialog_get_uitext( dialog, column_keys[count] );
3185 SendMessageW( control->hwnd, LVM_INSERTCOLUMNW, count++, (LPARAM)&lvc );
3186 msi_free( lvc.pszText );
3187 msi_free( num );
3191 static LONGLONG msi_vcl_get_cost( msi_dialog *dialog )
3193 MSIFEATURE *feature;
3194 INT each_cost;
3195 LONGLONG total_cost = 0;
3197 LIST_FOR_EACH_ENTRY( feature, &dialog->package->features, MSIFEATURE, entry )
3199 if (ERROR_SUCCESS == (MSI_GetFeatureCost(dialog->package, feature,
3200 MSICOSTTREE_SELFONLY, INSTALLSTATE_LOCAL, &each_cost)))
3202 /* each_cost is in 512-byte units */
3203 total_cost += each_cost * 512;
3205 if (ERROR_SUCCESS == (MSI_GetFeatureCost(dialog->package, feature,
3206 MSICOSTTREE_SELFONLY, INSTALLSTATE_ABSENT, &each_cost)))
3208 /* each_cost is in 512-byte units */
3209 total_cost -= each_cost * 512;
3212 return total_cost;
3215 static void msi_dialog_vcl_add_drives( msi_dialog *dialog, msi_control *control )
3217 ULARGE_INTEGER total, free;
3218 LONGLONG difference, cost;
3219 WCHAR size_text[MAX_PATH];
3220 WCHAR cost_text[MAX_PATH];
3221 LPWSTR drives, ptr;
3222 LVITEMW lvitem;
3223 DWORD size;
3224 int i = 0;
3226 cost = msi_vcl_get_cost(dialog);
3227 StrFormatByteSizeW(cost, cost_text, MAX_PATH);
3229 size = GetLogicalDriveStringsW( 0, NULL );
3230 if ( !size ) return;
3232 drives = msi_alloc( (size + 1) * sizeof(WCHAR) );
3233 if ( !drives ) return;
3235 GetLogicalDriveStringsW( size, drives );
3237 ptr = drives;
3238 while (*ptr)
3240 lvitem.mask = LVIF_TEXT;
3241 lvitem.iItem = i;
3242 lvitem.iSubItem = 0;
3243 lvitem.pszText = ptr;
3244 lvitem.cchTextMax = lstrlenW(ptr) + 1;
3245 SendMessageW( control->hwnd, LVM_INSERTITEMW, 0, (LPARAM)&lvitem );
3247 GetDiskFreeSpaceExW(ptr, &free, &total, NULL);
3248 difference = free.QuadPart - cost;
3250 StrFormatByteSizeW(total.QuadPart, size_text, MAX_PATH);
3251 lvitem.iSubItem = 1;
3252 lvitem.pszText = size_text;
3253 lvitem.cchTextMax = lstrlenW(size_text) + 1;
3254 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
3256 StrFormatByteSizeW(free.QuadPart, size_text, MAX_PATH);
3257 lvitem.iSubItem = 2;
3258 lvitem.pszText = size_text;
3259 lvitem.cchTextMax = lstrlenW(size_text) + 1;
3260 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
3262 lvitem.iSubItem = 3;
3263 lvitem.pszText = cost_text;
3264 lvitem.cchTextMax = lstrlenW(cost_text) + 1;
3265 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
3267 StrFormatByteSizeW(difference, size_text, MAX_PATH);
3268 lvitem.iSubItem = 4;
3269 lvitem.pszText = size_text;
3270 lvitem.cchTextMax = lstrlenW(size_text) + 1;
3271 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
3273 ptr += lstrlenW(ptr) + 1;
3274 i++;
3277 msi_free( drives );
3280 static UINT msi_dialog_volumecost_list( msi_dialog *dialog, MSIRECORD *rec )
3282 msi_control *control;
3283 DWORD style;
3285 style = LVS_REPORT | WS_VSCROLL | WS_HSCROLL | LVS_SHAREIMAGELISTS |
3286 LVS_AUTOARRANGE | LVS_SINGLESEL | WS_BORDER |
3287 WS_CHILD | WS_TABSTOP | WS_GROUP;
3288 control = msi_dialog_add_control( dialog, rec, WC_LISTVIEWW, style );
3289 if (!control)
3290 return ERROR_FUNCTION_FAILED;
3292 msi_dialog_vcl_add_columns( dialog, control, rec );
3293 msi_dialog_vcl_add_drives( dialog, control );
3295 return ERROR_SUCCESS;
3298 /******************** VolumeSelect Combo ***************************************/
3300 static UINT msi_dialog_volsel_handler( msi_dialog *dialog,
3301 msi_control *control, WPARAM param )
3303 WCHAR text[MAX_PATH];
3304 LPWSTR prop;
3305 BOOL indirect;
3306 int index;
3308 if (HIWORD(param) != CBN_SELCHANGE)
3309 return ERROR_SUCCESS;
3311 index = SendMessageW( control->hwnd, CB_GETCURSEL, 0, 0 );
3312 if ( index == CB_ERR )
3314 ERR("No ComboBox item selected!\n");
3315 return ERROR_FUNCTION_FAILED;
3318 SendMessageW( control->hwnd, CB_GETLBTEXT, index, (LPARAM)text );
3320 indirect = control->attributes & msidbControlAttributesIndirect;
3321 prop = msi_dialog_dup_property( dialog, control->property, indirect );
3323 msi_dialog_set_property( dialog->package, prop, text );
3325 msi_free( prop );
3326 return ERROR_SUCCESS;
3329 static void msi_dialog_vsc_add_drives( msi_dialog *dialog, msi_control *control )
3331 LPWSTR drives, ptr;
3332 DWORD size;
3334 size = GetLogicalDriveStringsW( 0, NULL );
3335 if ( !size ) return;
3337 drives = msi_alloc( (size + 1) * sizeof(WCHAR) );
3338 if ( !drives ) return;
3340 GetLogicalDriveStringsW( size, drives );
3342 ptr = drives;
3343 while (*ptr)
3345 SendMessageW( control->hwnd, CB_ADDSTRING, 0, (LPARAM)ptr );
3346 ptr += lstrlenW(ptr) + 1;
3349 msi_free( drives );
3352 static UINT msi_dialog_volumeselect_combo( msi_dialog *dialog, MSIRECORD *rec )
3354 msi_control *control;
3355 LPCWSTR prop;
3356 DWORD style;
3358 /* FIXME: CBS_OWNERDRAWFIXED */
3359 style = WS_CHILD | WS_VISIBLE | WS_GROUP | WS_TABSTOP |
3360 CBS_DROPDOWNLIST | CBS_SORT | CBS_HASSTRINGS |
3361 WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
3362 control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
3363 if (!control)
3364 return ERROR_FUNCTION_FAILED;
3366 control->attributes = MSI_RecordGetInteger( rec, 8 );
3367 control->handler = msi_dialog_volsel_handler;
3368 prop = MSI_RecordGetString( rec, 9 );
3369 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
3371 msi_dialog_vsc_add_drives( dialog, control );
3373 return ERROR_SUCCESS;
3376 static UINT msi_dialog_hyperlink_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
3378 static const WCHAR hrefW[] = {'h','r','e','f'};
3379 static const WCHAR openW[] = {'o','p','e','n',0};
3380 int len, len_href = sizeof(hrefW) / sizeof(hrefW[0]);
3381 const WCHAR *p, *q;
3382 WCHAR quote = 0;
3383 LITEM item;
3385 item.mask = LIF_ITEMINDEX | LIF_URL;
3386 item.iLink = 0;
3387 item.szUrl[0] = 0;
3389 SendMessageW( control->hwnd, LM_GETITEM, 0, (LPARAM)&item );
3391 p = item.szUrl;
3392 while (*p && *p != '<') p++;
3393 if (!*p++) return ERROR_SUCCESS;
3394 if (toupperW( *p++ ) != 'A' || !isspaceW( *p++ )) return ERROR_SUCCESS;
3395 while (*p && isspaceW( *p )) p++;
3397 len = strlenW( p );
3398 if (len > len_href && !memicmpW( p, hrefW, len_href ))
3400 p += len_href;
3401 while (*p && isspaceW( *p )) p++;
3402 if (!*p || *p++ != '=') return ERROR_SUCCESS;
3403 while (*p && isspaceW( *p )) p++;
3405 if (*p == '\"' || *p == '\'') quote = *p++;
3406 q = p;
3407 if (quote)
3409 while (*q && *q != quote) q++;
3410 if (*q != quote) return ERROR_SUCCESS;
3412 else
3414 while (*q && *q != '>' && !isspaceW( *q )) q++;
3415 if (!*q) return ERROR_SUCCESS;
3417 item.szUrl[q - item.szUrl] = 0;
3418 ShellExecuteW( NULL, openW, p, NULL, NULL, SW_SHOWNORMAL );
3420 return ERROR_SUCCESS;
3423 static UINT msi_dialog_hyperlink( msi_dialog *dialog, MSIRECORD *rec )
3425 msi_control *control;
3426 DWORD style = WS_CHILD | WS_TABSTOP | WS_GROUP;
3427 const WCHAR *text = MSI_RecordGetString( rec, 10 );
3428 int len = strlenW( text );
3429 LITEM item;
3431 control = msi_dialog_add_control( dialog, rec, WC_LINK, style );
3432 if (!control)
3433 return ERROR_FUNCTION_FAILED;
3435 control->attributes = MSI_RecordGetInteger( rec, 8 );
3436 control->handler = msi_dialog_hyperlink_handler;
3438 item.mask = LIF_ITEMINDEX | LIF_STATE | LIF_URL;
3439 item.iLink = 0;
3440 item.state = LIS_ENABLED;
3441 item.stateMask = LIS_ENABLED;
3442 if (len < L_MAX_URL_LENGTH) strcpyW( item.szUrl, text );
3443 else item.szUrl[0] = 0;
3445 SendMessageW( control->hwnd, LM_SETITEM, 0, (LPARAM)&item );
3447 return ERROR_SUCCESS;
3450 static const struct control_handler msi_dialog_handler[] =
3452 { szText, msi_dialog_text_control },
3453 { szPushButton, msi_dialog_button_control },
3454 { szLine, msi_dialog_line_control },
3455 { szBitmap, msi_dialog_bitmap_control },
3456 { szCheckBox, msi_dialog_checkbox_control },
3457 { szScrollableText, msi_dialog_scrolltext_control },
3458 { szComboBox, msi_dialog_combo_control },
3459 { szEdit, msi_dialog_edit_control },
3460 { szMaskedEdit, msi_dialog_maskedit_control },
3461 { szPathEdit, msi_dialog_pathedit_control },
3462 { szProgressBar, msi_dialog_progress_bar },
3463 { szRadioButtonGroup, msi_dialog_radiogroup_control },
3464 { szIcon, msi_dialog_icon_control },
3465 { szSelectionTree, msi_dialog_selection_tree },
3466 { szGroupBox, msi_dialog_group_box },
3467 { szListBox, msi_dialog_list_box },
3468 { szDirectoryCombo, msi_dialog_directory_combo },
3469 { szDirectoryList, msi_dialog_directory_list },
3470 { szVolumeCostList, msi_dialog_volumecost_list },
3471 { szVolumeSelectCombo, msi_dialog_volumeselect_combo },
3472 { szHyperLink, msi_dialog_hyperlink }
3475 #define NUM_CONTROL_TYPES (sizeof msi_dialog_handler/sizeof msi_dialog_handler[0])
3477 static UINT msi_dialog_create_controls( MSIRECORD *rec, LPVOID param )
3479 msi_dialog *dialog = param;
3480 LPCWSTR control_type;
3481 UINT i;
3483 /* find and call the function that can create this type of control */
3484 control_type = MSI_RecordGetString( rec, 3 );
3485 for( i=0; i<NUM_CONTROL_TYPES; i++ )
3486 if (!strcmpiW( msi_dialog_handler[i].control_type, control_type ))
3487 break;
3488 if( i != NUM_CONTROL_TYPES )
3489 msi_dialog_handler[i].func( dialog, rec );
3490 else
3491 ERR("no handler for element type %s\n", debugstr_w(control_type));
3493 return ERROR_SUCCESS;
3496 static UINT msi_dialog_fill_controls( msi_dialog *dialog )
3498 static const WCHAR query[] = {
3499 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
3500 'C','o','n','t','r','o','l',' ','W','H','E','R','E',' ',
3501 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
3502 UINT r;
3503 MSIQUERY *view;
3504 MSIPACKAGE *package = dialog->package;
3506 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
3508 /* query the Control table for all the elements of the control */
3509 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
3510 if( r != ERROR_SUCCESS )
3512 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
3513 return ERROR_INVALID_PARAMETER;
3516 r = MSI_IterateRecords( view, 0, msi_dialog_create_controls, dialog );
3517 msiobj_release( &view->hdr );
3518 return r;
3521 static UINT msi_dialog_reset( msi_dialog *dialog )
3523 /* FIXME: should restore the original values of any properties we changed */
3524 return msi_dialog_evaluate_control_conditions( dialog );
3527 /* figure out the height of 10 point MS Sans Serif */
3528 static INT msi_dialog_get_sans_serif_height( HWND hwnd )
3530 static const WCHAR szSansSerif[] = {
3531 'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0 };
3532 LOGFONTW lf;
3533 TEXTMETRICW tm;
3534 BOOL r;
3535 LONG height = 0;
3536 HFONT hFont, hOldFont;
3537 HDC hdc;
3539 hdc = GetDC( hwnd );
3540 if (hdc)
3542 memset( &lf, 0, sizeof lf );
3543 lf.lfHeight = MulDiv(12, GetDeviceCaps(hdc, LOGPIXELSY), 72);
3544 strcpyW( lf.lfFaceName, szSansSerif );
3545 hFont = CreateFontIndirectW(&lf);
3546 if (hFont)
3548 hOldFont = SelectObject( hdc, hFont );
3549 r = GetTextMetricsW( hdc, &tm );
3550 if (r)
3551 height = tm.tmHeight;
3552 SelectObject( hdc, hOldFont );
3553 DeleteObject( hFont );
3555 ReleaseDC( hwnd, hdc );
3557 return height;
3560 /* fetch the associated record from the Dialog table */
3561 static MSIRECORD *msi_get_dialog_record( msi_dialog *dialog )
3563 static const WCHAR query[] = {
3564 'S','E','L','E','C','T',' ','*',' ',
3565 'F','R','O','M',' ','D','i','a','l','o','g',' ',
3566 'W','H','E','R','E',' ',
3567 '`','D','i','a','l','o','g','`',' ','=',' ','\'','%','s','\'',0};
3568 MSIPACKAGE *package = dialog->package;
3569 MSIRECORD *rec = NULL;
3571 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
3573 rec = MSI_QueryGetRecord( package->db, query, dialog->name );
3574 if( !rec )
3575 WARN("query failed for dialog %s\n", debugstr_w(dialog->name));
3577 return rec;
3580 static void msi_dialog_adjust_dialog_pos( msi_dialog *dialog, MSIRECORD *rec, LPRECT pos )
3582 static const WCHAR szScreenX[] = {'S','c','r','e','e','n','X',0};
3583 static const WCHAR szScreenY[] = {'S','c','r','e','e','n','Y',0};
3585 UINT xres, yres;
3586 POINT center;
3587 SIZE sz;
3588 LONG style;
3590 center.x = MSI_RecordGetInteger( rec, 2 );
3591 center.y = MSI_RecordGetInteger( rec, 3 );
3593 sz.cx = MSI_RecordGetInteger( rec, 4 );
3594 sz.cy = MSI_RecordGetInteger( rec, 5 );
3596 sz.cx = msi_dialog_scale_unit( dialog, sz.cx );
3597 sz.cy = msi_dialog_scale_unit( dialog, sz.cy );
3599 xres = msi_get_property_int( dialog->package->db, szScreenX, 0 );
3600 yres = msi_get_property_int( dialog->package->db, szScreenY, 0 );
3602 center.x = MulDiv( center.x, xres, 100 );
3603 center.y = MulDiv( center.y, yres, 100 );
3605 /* turn the client pos into the window rectangle */
3606 if (dialog->package->center_x && dialog->package->center_y)
3608 pos->left = dialog->package->center_x - sz.cx / 2.0;
3609 pos->right = pos->left + sz.cx;
3610 pos->top = dialog->package->center_y - sz.cy / 2.0;
3611 pos->bottom = pos->top + sz.cy;
3613 else
3615 pos->left = center.x - sz.cx/2;
3616 pos->right = pos->left + sz.cx;
3617 pos->top = center.y - sz.cy/2;
3618 pos->bottom = pos->top + sz.cy;
3620 /* save the center */
3621 dialog->package->center_x = center.x;
3622 dialog->package->center_y = center.y;
3625 dialog->size.cx = sz.cx;
3626 dialog->size.cy = sz.cy;
3628 TRACE("%u %u %u %u\n", pos->left, pos->top, pos->right, pos->bottom);
3630 style = GetWindowLongPtrW( dialog->hwnd, GWL_STYLE );
3631 AdjustWindowRect( pos, style, FALSE );
3634 static void msi_dialog_set_tab_order( msi_dialog *dialog, LPCWSTR first )
3636 struct list tab_chain;
3637 msi_control *control;
3638 HWND prev = HWND_TOP;
3640 list_init( &tab_chain );
3641 if (!(control = msi_dialog_find_control( dialog, first ))) return;
3643 dialog->hWndFocus = control->hwnd;
3644 while (control)
3646 list_remove( &control->entry );
3647 list_add_tail( &tab_chain, &control->entry );
3648 if (!control->tabnext) break;
3649 control = msi_dialog_find_control( dialog, control->tabnext );
3652 LIST_FOR_EACH_ENTRY( control, &tab_chain, msi_control, entry )
3654 SetWindowPos( control->hwnd, prev, 0, 0, 0, 0,
3655 SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOREDRAW |
3656 SWP_NOREPOSITION | SWP_NOSENDCHANGING | SWP_NOSIZE );
3657 prev = control->hwnd;
3660 /* put them back on the main list */
3661 list_move_head( &dialog->controls, &tab_chain );
3664 static LRESULT msi_dialog_oncreate( HWND hwnd, LPCREATESTRUCTW cs )
3666 static const WCHAR df[] = {
3667 'D','e','f','a','u','l','t','U','I','F','o','n','t',0 };
3668 static const WCHAR dfv[] = {
3669 'M','S',' ','S','h','e','l','l',' ','D','l','g',0 };
3670 msi_dialog *dialog = cs->lpCreateParams;
3671 MSIRECORD *rec = NULL;
3672 LPWSTR title = NULL;
3673 RECT pos;
3675 TRACE("%p %p\n", dialog, dialog->package);
3677 dialog->hwnd = hwnd;
3678 SetWindowLongPtrW( hwnd, GWLP_USERDATA, (LONG_PTR) dialog );
3680 rec = msi_get_dialog_record( dialog );
3681 if( !rec )
3683 TRACE("No record found for dialog %s\n", debugstr_w(dialog->name));
3684 return -1;
3687 dialog->scale = msi_dialog_get_sans_serif_height(dialog->hwnd);
3689 msi_dialog_adjust_dialog_pos( dialog, rec, &pos );
3691 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
3693 dialog->default_font = msi_dup_property( dialog->package->db, df );
3694 if (!dialog->default_font)
3696 dialog->default_font = strdupW(dfv);
3697 msiobj_release( &rec->hdr );
3698 if (!dialog->default_font) return -1;
3701 title = msi_get_deformatted_field( dialog->package, rec, 7 );
3702 SetWindowTextW( hwnd, title );
3703 msi_free( title );
3705 SetWindowPos( hwnd, 0, pos.left, pos.top,
3706 pos.right - pos.left, pos.bottom - pos.top,
3707 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW );
3709 msi_dialog_build_font_list( dialog );
3710 msi_dialog_fill_controls( dialog );
3711 msi_dialog_evaluate_control_conditions( dialog );
3712 msi_dialog_set_tab_order( dialog, MSI_RecordGetString( rec, 8 ) );
3713 msiobj_release( &rec->hdr );
3715 return 0;
3718 static LRESULT msi_dialog_oncommand( msi_dialog *dialog, WPARAM param, HWND hwnd )
3720 msi_control *control = NULL;
3722 TRACE("%p %p %08lx\n", dialog, hwnd, param);
3724 switch (param)
3726 case 1: /* enter */
3727 control = msi_dialog_find_control( dialog, dialog->control_default );
3728 break;
3729 case 2: /* escape */
3730 control = msi_dialog_find_control( dialog, dialog->control_cancel );
3731 break;
3732 default:
3733 control = msi_dialog_find_control_by_hwnd( dialog, hwnd );
3736 if( control )
3738 if( control->handler )
3740 control->handler( dialog, control, param );
3741 msi_dialog_evaluate_control_conditions( dialog );
3745 return 0;
3748 static LRESULT msi_dialog_onnotify( msi_dialog *dialog, LPARAM param )
3750 LPNMHDR nmhdr = (LPNMHDR) param;
3751 msi_control *control = msi_dialog_find_control_by_hwnd( dialog, nmhdr->hwndFrom );
3753 TRACE("%p %p\n", dialog, nmhdr->hwndFrom);
3755 if ( control && control->handler )
3756 control->handler( dialog, control, param );
3758 return 0;
3761 static void dialog_setfocus( msi_dialog *dialog )
3763 HWND hwnd = dialog->hWndFocus;
3765 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, TRUE);
3766 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, FALSE);
3767 SetFocus( hwnd );
3768 dialog->hWndFocus = hwnd;
3771 static LRESULT WINAPI MSIDialog_WndProc( HWND hwnd, UINT msg,
3772 WPARAM wParam, LPARAM lParam )
3774 msi_dialog *dialog = (LPVOID) GetWindowLongPtrW( hwnd, GWLP_USERDATA );
3776 TRACE("0x%04x\n", msg);
3778 switch (msg)
3780 case WM_MOVE:
3781 dialog->package->center_x = LOWORD(lParam) + dialog->size.cx / 2.0;
3782 dialog->package->center_y = HIWORD(lParam) + dialog->size.cy / 2.0;
3783 break;
3785 case WM_CREATE:
3786 return msi_dialog_oncreate( hwnd, (LPCREATESTRUCTW)lParam );
3788 case WM_COMMAND:
3789 return msi_dialog_oncommand( dialog, wParam, (HWND)lParam );
3791 case WM_ACTIVATE:
3792 if( LOWORD(wParam) == WA_INACTIVE )
3793 dialog->hWndFocus = GetFocus();
3794 else
3795 dialog_setfocus( dialog );
3796 return 0;
3798 case WM_SETFOCUS:
3799 dialog_setfocus( dialog );
3800 return 0;
3802 /* bounce back to our subclassed static control */
3803 case WM_CTLCOLORSTATIC:
3804 return SendMessageW( (HWND) lParam, WM_CTLCOLORSTATIC, wParam, lParam );
3806 case WM_DESTROY:
3807 dialog->hwnd = NULL;
3808 return 0;
3809 case WM_NOTIFY:
3810 return msi_dialog_onnotify( dialog, lParam );
3812 return DefWindowProcW(hwnd, msg, wParam, lParam);
3815 static void process_pending_messages( HWND hdlg )
3817 MSG msg;
3819 while (PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
3821 if (hdlg && IsDialogMessageW( hdlg, &msg )) continue;
3822 TranslateMessage( &msg );
3823 DispatchMessageW( &msg );
3827 static UINT dialog_run_message_loop( msi_dialog *dialog )
3829 DWORD style;
3830 HWND hwnd;
3832 if( uiThreadId != GetCurrentThreadId() )
3833 return SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_CREATE, 0, (LPARAM) dialog );
3835 /* create the dialog window, don't show it yet */
3836 style = WS_OVERLAPPED;
3837 if( dialog->attributes & msidbDialogAttributesVisible )
3838 style |= WS_VISIBLE;
3840 hwnd = CreateWindowW( szMsiDialogClass, dialog->name, style,
3841 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
3842 NULL, NULL, NULL, dialog );
3843 if( !hwnd )
3845 ERR("Failed to create dialog %s\n", debugstr_w( dialog->name ));
3846 return ERROR_FUNCTION_FAILED;
3849 ShowWindow( hwnd, SW_SHOW );
3850 /* UpdateWindow( hwnd ); - and causes the transparent static controls not to paint */
3852 if( dialog->attributes & msidbDialogAttributesModal )
3854 while( !dialog->finished )
3856 MsgWaitForMultipleObjects( 0, NULL, 0, INFINITE, QS_ALLINPUT );
3857 process_pending_messages( dialog->hwnd );
3860 else
3861 return ERROR_IO_PENDING;
3863 return ERROR_SUCCESS;
3866 static LRESULT WINAPI MSIHiddenWindowProc( HWND hwnd, UINT msg,
3867 WPARAM wParam, LPARAM lParam )
3869 msi_dialog *dialog = (msi_dialog*) lParam;
3871 TRACE("%d %p\n", msg, dialog);
3873 switch (msg)
3875 case WM_MSI_DIALOG_CREATE:
3876 return dialog_run_message_loop( dialog );
3877 case WM_MSI_DIALOG_DESTROY:
3878 msi_dialog_destroy( dialog );
3879 return 0;
3881 return DefWindowProcW( hwnd, msg, wParam, lParam );
3884 static BOOL dialog_register_class( void )
3886 WNDCLASSW cls;
3888 ZeroMemory( &cls, sizeof cls );
3889 cls.lpfnWndProc = MSIDialog_WndProc;
3890 cls.hInstance = NULL;
3891 cls.hIcon = LoadIconW(0, (LPWSTR)IDI_APPLICATION);
3892 cls.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
3893 cls.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
3894 cls.lpszMenuName = NULL;
3895 cls.lpszClassName = szMsiDialogClass;
3897 if( !RegisterClassW( &cls ) )
3898 return FALSE;
3900 cls.lpfnWndProc = MSIHiddenWindowProc;
3901 cls.lpszClassName = szMsiHiddenWindow;
3903 if( !RegisterClassW( &cls ) )
3904 return FALSE;
3906 uiThreadId = GetCurrentThreadId();
3908 hMsiHiddenWindow = CreateWindowW( szMsiHiddenWindow, NULL, WS_OVERLAPPED,
3909 0, 0, 100, 100, NULL, NULL, NULL, NULL );
3910 if( !hMsiHiddenWindow )
3911 return FALSE;
3913 return TRUE;
3916 static msi_dialog *dialog_create( MSIPACKAGE *package, const WCHAR *name, msi_dialog *parent,
3917 control_event_handler event_handler )
3919 MSIRECORD *rec = NULL;
3920 msi_dialog *dialog;
3922 TRACE("%s\n", debugstr_w(name));
3924 if (!hMsiHiddenWindow) dialog_register_class();
3926 /* allocate the structure for the dialog to use */
3927 dialog = msi_alloc_zero( FIELD_OFFSET( msi_dialog, name[strlenW( name ) + 1] ));
3928 if( !dialog )
3929 return NULL;
3930 strcpyW( dialog->name, name );
3931 dialog->parent = parent;
3932 msiobj_addref( &package->hdr );
3933 dialog->package = package;
3934 dialog->event_handler = event_handler;
3935 dialog->finished = 0;
3936 list_init( &dialog->controls );
3937 list_init( &dialog->fonts );
3939 /* verify that the dialog exists */
3940 rec = msi_get_dialog_record( dialog );
3941 if( !rec )
3943 msiobj_release( &package->hdr );
3944 msi_free( dialog );
3945 return NULL;
3947 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
3948 dialog->control_default = strdupW( MSI_RecordGetString( rec, 9 ) );
3949 dialog->control_cancel = strdupW( MSI_RecordGetString( rec, 10 ) );
3950 msiobj_release( &rec->hdr );
3952 return dialog;
3955 static void msi_dialog_end_dialog( msi_dialog *dialog )
3957 TRACE("%p\n", dialog);
3958 dialog->finished = 1;
3959 PostMessageW(dialog->hwnd, WM_NULL, 0, 0);
3962 void msi_dialog_check_messages( HANDLE handle )
3964 DWORD r;
3966 /* in threads other than the UI thread, block */
3967 if( uiThreadId != GetCurrentThreadId() )
3969 if (!handle) return;
3970 while (MsgWaitForMultipleObjectsEx( 1, &handle, INFINITE, QS_ALLINPUT, 0 ) == WAIT_OBJECT_0 + 1)
3972 MSG msg;
3973 while (PeekMessageW( &msg, NULL, 0, 0, PM_REMOVE ))
3975 TranslateMessage( &msg );
3976 DispatchMessageW( &msg );
3979 return;
3982 /* there are two choices for the UI thread */
3983 while (1)
3985 process_pending_messages( NULL );
3987 if( !handle )
3988 break;
3991 * block here until somebody creates a new dialog or
3992 * the handle we're waiting on becomes ready
3994 r = MsgWaitForMultipleObjects( 1, &handle, 0, INFINITE, QS_ALLINPUT );
3995 if( r == WAIT_OBJECT_0 )
3996 break;
4000 static void dialog_do_preview( msi_dialog *dialog )
4002 TRACE("\n");
4003 dialog->attributes |= msidbDialogAttributesVisible;
4004 dialog->attributes &= ~msidbDialogAttributesModal;
4005 dialog_run_message_loop( dialog );
4008 static void free_subscriber( struct subscriber *sub )
4010 msi_free( sub->event );
4011 msi_free( sub->control );
4012 msi_free( sub->attribute );
4013 msi_free( sub );
4016 static void event_cleanup_subscriptions( MSIPACKAGE *package, const WCHAR *dialog )
4018 struct list *item, *next;
4020 LIST_FOR_EACH_SAFE( item, next, &package->subscriptions )
4022 struct subscriber *sub = LIST_ENTRY( item, struct subscriber, entry );
4024 if (strcmpW( sub->dialog->name, dialog )) continue;
4025 list_remove( &sub->entry );
4026 free_subscriber( sub );
4030 void msi_dialog_destroy( msi_dialog *dialog )
4032 msi_font *font, *next;
4034 if( uiThreadId != GetCurrentThreadId() )
4036 SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_DESTROY, 0, (LPARAM) dialog );
4037 return;
4040 if( dialog->hwnd )
4041 ShowWindow( dialog->hwnd, SW_HIDE );
4043 if( dialog->hwnd )
4044 DestroyWindow( dialog->hwnd );
4046 /* unsubscribe events */
4047 event_cleanup_subscriptions( dialog->package, dialog->name );
4049 /* destroy the list of controls */
4050 while( !list_empty( &dialog->controls ) )
4052 msi_control *t;
4054 t = LIST_ENTRY( list_head( &dialog->controls ),
4055 msi_control, entry );
4056 msi_destroy_control( t );
4059 /* destroy the list of fonts */
4060 LIST_FOR_EACH_ENTRY_SAFE( font, next, &dialog->fonts, msi_font, entry )
4062 list_remove( &font->entry );
4063 DeleteObject( font->hfont );
4064 msi_free( font );
4066 msi_free( dialog->default_font );
4068 msi_free( dialog->control_default );
4069 msi_free( dialog->control_cancel );
4070 msiobj_release( &dialog->package->hdr );
4071 dialog->package = NULL;
4072 msi_free( dialog );
4075 void msi_dialog_unregister_class( void )
4077 DestroyWindow( hMsiHiddenWindow );
4078 hMsiHiddenWindow = NULL;
4079 UnregisterClassW( szMsiDialogClass, NULL );
4080 UnregisterClassW( szMsiHiddenWindow, NULL );
4081 uiThreadId = 0;
4084 void msi_event_cleanup_all_subscriptions( MSIPACKAGE *package )
4086 struct list *item, *next;
4088 LIST_FOR_EACH_SAFE( item, next, &package->subscriptions )
4090 struct subscriber *sub = LIST_ENTRY( item, struct subscriber, entry );
4091 list_remove( &sub->entry );
4092 free_subscriber( sub );
4096 static UINT error_dialog_handler( msi_dialog *dialog, const WCHAR *event, const WCHAR *argument )
4098 static const WCHAR end_dialog[] = {'E','n','d','D','i','a','l','o','g',0};
4099 static const WCHAR error_abort[] = {'E','r','r','o','r','A','b','o','r','t',0};
4100 static const WCHAR error_cancel[] = {'E','r','r','o','r','C','a','n','c','e','l',0};
4101 static const WCHAR error_no[] = {'E','r','r','o','r','N','o',0};
4102 static const WCHAR result_prop[] = {
4103 'M','S','I','E','r','r','o','r','D','i','a','l','o','g','R','e','s','u','l','t',0
4106 if ( strcmpW( event, end_dialog ) )
4107 return ERROR_SUCCESS;
4109 if ( !strcmpW( argument, error_abort ) || !strcmpW( argument, error_cancel ) ||
4110 !strcmpW( argument, error_no ) )
4112 msi_set_property( dialog->package->db, result_prop, error_abort, -1 );
4115 msi_event_cleanup_all_subscriptions( dialog->package );
4116 msi_dialog_end_dialog( dialog );
4118 return ERROR_SUCCESS;
4121 static UINT msi_error_dialog_set_error( MSIPACKAGE *package, LPWSTR error_dialog, LPWSTR error )
4123 MSIRECORD * row;
4125 static const WCHAR update[] =
4126 {'U','P','D','A','T','E',' ','`','C','o','n','t','r','o','l','`',' ',
4127 'S','E','T',' ','`','T','e','x','t','`',' ','=',' ','\'','%','s','\'',' ',
4128 'W','H','E','R','E', ' ','`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
4129 'A','N','D',' ','`','C','o','n','t','r','o','l','`',' ','=',' ',
4130 '\'','E','r','r','o','r','T','e','x','t','\'',0};
4132 row = MSI_QueryGetRecord( package->db, update, error, error_dialog );
4133 if (!row)
4134 return ERROR_FUNCTION_FAILED;
4136 msiobj_release(&row->hdr);
4137 return ERROR_SUCCESS;
4140 UINT msi_spawn_error_dialog( MSIPACKAGE *package, LPWSTR error_dialog, LPWSTR error )
4142 msi_dialog *dialog;
4143 WCHAR result[MAX_PATH];
4144 UINT r = ERROR_SUCCESS;
4145 DWORD size = MAX_PATH;
4146 int res;
4148 static const WCHAR pn_prop[] = {'P','r','o','d','u','c','t','N','a','m','e',0};
4149 static const WCHAR title_fmt[] = {'%','s',' ','W','a','r','n','i','n','g',0};
4150 static const WCHAR error_abort[] = {'E','r','r','o','r','A','b','o','r','t',0};
4151 static const WCHAR result_prop[] = {
4152 'M','S','I','E','r','r','o','r','D','i','a','l','o','g','R','e','s','u','l','t',0
4155 if ((package->ui_level & INSTALLUILEVEL_MASK) == INSTALLUILEVEL_NONE) return ERROR_SUCCESS;
4157 if ( !error_dialog )
4159 LPWSTR product_name = msi_dup_property( package->db, pn_prop );
4160 WCHAR title[MAX_PATH];
4162 sprintfW( title, title_fmt, product_name );
4163 res = MessageBoxW( NULL, error, title, MB_OKCANCEL | MB_ICONWARNING );
4165 msi_free( product_name );
4167 if ( res == IDOK )
4168 return ERROR_SUCCESS;
4169 else
4170 return ERROR_FUNCTION_FAILED;
4173 r = msi_error_dialog_set_error( package, error_dialog, error );
4174 if ( r != ERROR_SUCCESS )
4175 return r;
4177 dialog = dialog_create( package, error_dialog, package->dialog, error_dialog_handler );
4178 if ( !dialog )
4179 return ERROR_FUNCTION_FAILED;
4181 dialog->finished = FALSE;
4182 r = dialog_run_message_loop( dialog );
4183 if ( r != ERROR_SUCCESS )
4184 goto done;
4186 r = msi_get_property( package->db, result_prop, result, &size );
4187 if ( r != ERROR_SUCCESS)
4188 r = ERROR_SUCCESS;
4190 if ( !strcmpW( result, error_abort ) )
4191 r = ERROR_FUNCTION_FAILED;
4193 done:
4194 msi_dialog_destroy( dialog );
4196 return r;
4199 static void MSI_ClosePreview( MSIOBJECTHDR *arg )
4201 MSIPREVIEW *preview = (MSIPREVIEW *)arg;
4202 msiobj_release( &preview->package->hdr );
4205 static MSIPREVIEW *MSI_EnableUIPreview( MSIDATABASE *db )
4207 MSIPREVIEW *preview = NULL;
4208 MSIPACKAGE *package;
4210 package = MSI_CreatePackage( db, NULL );
4211 if (package)
4213 preview = alloc_msiobject( MSIHANDLETYPE_PREVIEW, sizeof(MSIPREVIEW), MSI_ClosePreview );
4214 if (preview)
4216 preview->package = package;
4217 msiobj_addref( &package->hdr );
4219 msiobj_release( &package->hdr );
4221 return preview;
4224 UINT WINAPI MsiEnableUIPreview( MSIHANDLE hdb, MSIHANDLE *phPreview )
4226 MSIDATABASE *db;
4227 MSIPREVIEW *preview;
4228 UINT r = ERROR_FUNCTION_FAILED;
4230 TRACE("%d %p\n", hdb, phPreview);
4232 db = msihandle2msiinfo( hdb, MSIHANDLETYPE_DATABASE );
4233 if (!db)
4235 IWineMsiRemoteDatabase *remote_database;
4237 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( hdb );
4238 if (!remote_database)
4239 return ERROR_INVALID_HANDLE;
4241 *phPreview = 0;
4243 IWineMsiRemoteDatabase_Release( remote_database );
4244 WARN("MsiEnableUIPreview not allowed during a custom action!\n");
4246 return ERROR_FUNCTION_FAILED;
4248 preview = MSI_EnableUIPreview( db );
4249 if (preview)
4251 *phPreview = alloc_msihandle( &preview->hdr );
4252 msiobj_release( &preview->hdr );
4253 r = ERROR_SUCCESS;
4254 if (!*phPreview)
4255 r = ERROR_NOT_ENOUGH_MEMORY;
4257 msiobj_release( &db->hdr );
4258 return r;
4261 static UINT preview_event_handler( msi_dialog *dialog, const WCHAR *event, const WCHAR *argument )
4263 MESSAGE("Preview dialog event '%s' (arg='%s')\n", debugstr_w(event), debugstr_w(argument));
4264 return ERROR_SUCCESS;
4267 static UINT MSI_PreviewDialogW( MSIPREVIEW *preview, LPCWSTR szDialogName )
4269 msi_dialog *dialog = NULL;
4270 UINT r = ERROR_SUCCESS;
4272 if (preview->dialog)
4273 msi_dialog_destroy( preview->dialog );
4275 /* an empty name means we should just destroy the current preview dialog */
4276 if (szDialogName)
4278 dialog = dialog_create( preview->package, szDialogName, NULL, preview_event_handler );
4279 if (dialog)
4280 dialog_do_preview( dialog );
4281 else
4282 r = ERROR_FUNCTION_FAILED;
4284 preview->dialog = dialog;
4285 return r;
4288 UINT WINAPI MsiPreviewDialogW( MSIHANDLE hPreview, LPCWSTR szDialogName )
4290 MSIPREVIEW *preview;
4291 UINT r;
4293 TRACE("%d %s\n", hPreview, debugstr_w(szDialogName));
4295 preview = msihandle2msiinfo( hPreview, MSIHANDLETYPE_PREVIEW );
4296 if (!preview)
4297 return ERROR_INVALID_HANDLE;
4299 r = MSI_PreviewDialogW( preview, szDialogName );
4300 msiobj_release( &preview->hdr );
4301 return r;
4304 UINT WINAPI MsiPreviewDialogA( MSIHANDLE hPreview, LPCSTR szDialogName )
4306 UINT r;
4307 LPWSTR strW = NULL;
4309 TRACE("%d %s\n", hPreview, debugstr_a(szDialogName));
4311 if (szDialogName)
4313 strW = strdupAtoW( szDialogName );
4314 if (!strW)
4315 return ERROR_OUTOFMEMORY;
4317 r = MsiPreviewDialogW( hPreview, strW );
4318 msi_free( strW );
4319 return r;
4322 UINT WINAPI MsiPreviewBillboardW( MSIHANDLE hPreview, LPCWSTR szControlName, LPCWSTR szBillboard )
4324 FIXME("%d %s %s\n", hPreview, debugstr_w(szControlName), debugstr_w(szBillboard));
4325 return ERROR_CALL_NOT_IMPLEMENTED;
4328 UINT WINAPI MsiPreviewBillboardA( MSIHANDLE hPreview, LPCSTR szControlName, LPCSTR szBillboard )
4330 FIXME("%d %s %s\n", hPreview, debugstr_a(szControlName), debugstr_a(szBillboard));
4331 return ERROR_CALL_NOT_IMPLEMENTED;
4334 typedef UINT (*event_handler)( msi_dialog *, const WCHAR * );
4336 struct control_event
4338 const WCHAR *event;
4339 event_handler handler;
4342 static UINT dialog_event_handler( msi_dialog *, const WCHAR *, const WCHAR * );
4344 /* create a dialog box and run it if it's modal */
4345 static UINT event_do_dialog( MSIPACKAGE *package, const WCHAR *name, msi_dialog *parent, BOOL destroy_modeless )
4347 msi_dialog *dialog;
4348 UINT r;
4350 /* create a new dialog */
4351 dialog = dialog_create( package, name, parent, dialog_event_handler );
4352 if (dialog)
4354 /* kill the current modeless dialog */
4355 if (destroy_modeless && package->dialog)
4357 msi_dialog_destroy( package->dialog );
4358 package->dialog = NULL;
4361 /* modeless dialogs return an error message */
4362 r = dialog_run_message_loop( dialog );
4363 if (r == ERROR_SUCCESS)
4364 msi_dialog_destroy( dialog );
4365 else
4366 package->dialog = dialog;
4368 else r = ERROR_FUNCTION_FAILED;
4369 return r;
4372 /* end a modal dialog box */
4373 static UINT event_end_dialog( msi_dialog *dialog, const WCHAR *argument )
4375 static const WCHAR exitW[] = {'E','x','i','t',0};
4376 static const WCHAR retryW[] = {'R','e','t','r','y',0};
4377 static const WCHAR ignoreW[] = {'I','g','n','o','r','e',0};
4378 static const WCHAR returnW[] = {'R','e','t','u','r','n',0};
4380 if (!strcmpW( argument, exitW ))
4381 dialog->package->CurrentInstallState = ERROR_INSTALL_USEREXIT;
4382 else if (!strcmpW( argument, retryW ))
4383 dialog->package->CurrentInstallState = ERROR_INSTALL_SUSPEND;
4384 else if (!strcmpW( argument, ignoreW ))
4385 dialog->package->CurrentInstallState = ERROR_SUCCESS;
4386 else if (!strcmpW( argument, returnW ))
4388 msi_dialog *parent = dialog->parent;
4389 msi_free( dialog->package->next_dialog );
4390 dialog->package->next_dialog = (parent) ? strdupW( parent->name ) : NULL;
4391 dialog->package->CurrentInstallState = ERROR_SUCCESS;
4393 else
4395 ERR("Unknown argument string %s\n", debugstr_w(argument));
4396 dialog->package->CurrentInstallState = ERROR_FUNCTION_FAILED;
4398 event_cleanup_subscriptions( dialog->package, dialog->name );
4399 msi_dialog_end_dialog( dialog );
4400 return ERROR_SUCCESS;
4403 /* transition from one modal dialog to another modal dialog */
4404 static UINT event_new_dialog( msi_dialog *dialog, const WCHAR *argument )
4406 /* store the name of the next dialog, and signal this one to end */
4407 dialog->package->next_dialog = strdupW( argument );
4408 msi_event_cleanup_all_subscriptions( dialog->package );
4409 msi_dialog_end_dialog( dialog );
4410 return ERROR_SUCCESS;
4413 /* create a new child dialog of an existing modal dialog */
4414 static UINT event_spawn_dialog( msi_dialog *dialog, const WCHAR *argument )
4416 /* don't destroy a modeless dialogs that might be our parent */
4417 event_do_dialog( dialog->package, argument, dialog, FALSE );
4418 if (dialog->package->CurrentInstallState != ERROR_SUCCESS) msi_dialog_end_dialog( dialog );
4419 return ERROR_SUCCESS;
4422 /* creates a dialog that remains up for a period of time based on a condition */
4423 static UINT event_spawn_wait_dialog( msi_dialog *dialog, const WCHAR *argument )
4425 FIXME("doing nothing\n");
4426 return ERROR_SUCCESS;
4429 static UINT event_do_action( msi_dialog *dialog, const WCHAR *argument )
4431 ACTION_PerformAction( dialog->package, argument, SCRIPT_NONE );
4432 return ERROR_SUCCESS;
4435 static UINT event_add_local( msi_dialog *dialog, const WCHAR *argument )
4437 MSIFEATURE *feature;
4439 LIST_FOR_EACH_ENTRY( feature, &dialog->package->features, MSIFEATURE, entry )
4441 if (!strcmpW( argument, feature->Feature ) || !strcmpW( argument, szAll ))
4443 if (feature->ActionRequest != INSTALLSTATE_LOCAL)
4444 msi_set_property( dialog->package->db, szPreselected, szOne, -1 );
4445 MSI_SetFeatureStateW( dialog->package, feature->Feature, INSTALLSTATE_LOCAL );
4448 return ERROR_SUCCESS;
4451 static UINT event_remove( msi_dialog *dialog, const WCHAR *argument )
4453 MSIFEATURE *feature;
4455 LIST_FOR_EACH_ENTRY( feature, &dialog->package->features, MSIFEATURE, entry )
4457 if (!strcmpW( argument, feature->Feature ) || !strcmpW( argument, szAll ))
4459 if (feature->ActionRequest != INSTALLSTATE_ABSENT)
4460 msi_set_property( dialog->package->db, szPreselected, szOne, -1 );
4461 MSI_SetFeatureStateW( dialog->package, feature->Feature, INSTALLSTATE_ABSENT );
4464 return ERROR_SUCCESS;
4467 static UINT event_add_source( msi_dialog *dialog, const WCHAR *argument )
4469 MSIFEATURE *feature;
4471 LIST_FOR_EACH_ENTRY( feature, &dialog->package->features, MSIFEATURE, entry )
4473 if (!strcmpW( argument, feature->Feature ) || !strcmpW( argument, szAll ))
4475 if (feature->ActionRequest != INSTALLSTATE_SOURCE)
4476 msi_set_property( dialog->package->db, szPreselected, szOne, -1 );
4477 MSI_SetFeatureStateW( dialog->package, feature->Feature, INSTALLSTATE_SOURCE );
4480 return ERROR_SUCCESS;
4483 void msi_event_fire( MSIPACKAGE *package, const WCHAR *event, MSIRECORD *rec )
4485 struct subscriber *sub;
4487 TRACE("firing event %s\n", debugstr_w(event));
4489 LIST_FOR_EACH_ENTRY( sub, &package->subscriptions, struct subscriber, entry )
4491 if (strcmpiW( sub->event, event )) continue;
4492 dialog_handle_event( sub->dialog, sub->control, sub->attribute, rec );
4496 static UINT event_set_target_path( msi_dialog *dialog, const WCHAR *argument )
4498 WCHAR *path = msi_dup_property( dialog->package->db, argument );
4499 MSIRECORD *rec = MSI_CreateRecord( 1 );
4500 UINT r = ERROR_SUCCESS;
4502 MSI_RecordSetStringW( rec, 1, path );
4503 msi_event_fire( dialog->package, szSelectionPath, rec );
4504 if (path)
4506 /* failure to set the path halts the executing of control events */
4507 r = MSI_SetTargetPathW( dialog->package, argument, path );
4508 msi_free( path );
4510 msi_free( &rec->hdr );
4511 return r;
4514 static UINT event_reset( msi_dialog *dialog, const WCHAR *argument )
4516 msi_dialog_reset( dialog );
4517 return ERROR_SUCCESS;
4520 /* Return ERROR_SUCCESS if dialog is process and ERROR_FUNCTION_FAILED
4521 * if the given parameter is not a dialog box
4523 UINT ACTION_DialogBox( MSIPACKAGE *package, const WCHAR *dialog )
4525 UINT r;
4527 if (package->next_dialog) ERR("Already got next dialog... ignoring it\n");
4528 package->next_dialog = NULL;
4530 /* Dialogs are chained by filling in the next_dialog member
4531 * of the package structure, then terminating the current dialog.
4532 * The code below sees the next_dialog member set, and runs the
4533 * next dialog.
4534 * We fall out of the loop below if we come across a modeless
4535 * dialog, as it returns ERROR_IO_PENDING when we try to run
4536 * its message loop.
4538 r = event_do_dialog( package, dialog, NULL, TRUE );
4539 while (r == ERROR_SUCCESS && package->next_dialog)
4541 WCHAR *name = package->next_dialog;
4543 package->next_dialog = NULL;
4544 r = event_do_dialog( package, name, NULL, TRUE );
4545 msi_free( name );
4547 if (r == ERROR_IO_PENDING) r = ERROR_SUCCESS;
4548 return r;
4551 static UINT event_set_install_level( msi_dialog *dialog, const WCHAR *argument )
4553 int level = atolW( argument );
4555 TRACE("setting install level to %d\n", level);
4556 return MSI_SetInstallLevel( dialog->package, level );
4559 static UINT event_directory_list_up( msi_dialog *dialog, const WCHAR *argument )
4561 return msi_dialog_directorylist_up( dialog );
4564 static UINT event_reinstall_mode( msi_dialog *dialog, const WCHAR *argument )
4566 return msi_set_property( dialog->package->db, szReinstallMode, argument, -1 );
4569 static UINT event_reinstall( msi_dialog *dialog, const WCHAR *argument )
4571 return msi_set_property( dialog->package->db, szReinstall, argument, -1 );
4574 static UINT event_validate_product_id( msi_dialog *dialog, const WCHAR *argument )
4576 return msi_validate_product_id( dialog->package );
4579 static const WCHAR end_dialogW[] = {'E','n','d','D','i','a','l','o','g',0};
4580 static const WCHAR new_dialogW[] = {'N','e','w','D','i','a','l','o','g',0};
4581 static const WCHAR spawn_dialogW[] = {'S','p','a','w','n','D','i','a','l','o','g',0};
4582 static const WCHAR spawn_wait_dialogW[] = {'S','p','a','w','n','W','a','i','t','D','i','a','l','o','g',0};
4583 static const WCHAR do_actionW[] = {'D','o','A','c','t','i','o','n',0};
4584 static const WCHAR add_localW[] = {'A','d','d','L','o','c','a','l',0};
4585 static const WCHAR removeW[] = {'R','e','m','o','v','e',0};
4586 static const WCHAR add_sourceW[] = {'A','d','d','S','o','u','r','c','e',0};
4587 static const WCHAR set_target_pathW[] = {'S','e','t','T','a','r','g','e','t','P','a','t','h',0};
4588 static const WCHAR resetW[] = {'R','e','s','e','t',0};
4589 static const WCHAR set_install_levelW[] = {'S','e','t','I','n','s','t','a','l','l','L','e','v','e','l',0};
4590 static const WCHAR directory_list_upW[] = {'D','i','r','e','c','t','o','r','y','L','i','s','t','U','p',0};
4591 static const WCHAR selection_browseW[] = {'S','e','l','e','c','t','i','o','n','B','r','o','w','s','e',0};
4592 static const WCHAR reinstall_modeW[] = {'R','e','i','n','s','t','a','l','l','M','o','d','e',0};
4593 static const WCHAR reinstallW[] = {'R','e','i','n','s','t','a','l','l',0};
4594 static const WCHAR validate_product_idW[] = {'V','a','l','i','d','a','t','e','P','r','o','d','u','c','t','I','D',0};
4596 static const struct control_event control_events[] =
4598 { end_dialogW, event_end_dialog },
4599 { new_dialogW, event_new_dialog },
4600 { spawn_dialogW, event_spawn_dialog },
4601 { spawn_wait_dialogW, event_spawn_wait_dialog },
4602 { do_actionW, event_do_action },
4603 { add_localW, event_add_local },
4604 { removeW, event_remove },
4605 { add_sourceW, event_add_source },
4606 { set_target_pathW, event_set_target_path },
4607 { resetW, event_reset },
4608 { set_install_levelW, event_set_install_level },
4609 { directory_list_upW, event_directory_list_up },
4610 { selection_browseW, event_spawn_dialog },
4611 { reinstall_modeW, event_reinstall_mode },
4612 { reinstallW, event_reinstall },
4613 { validate_product_idW, event_validate_product_id },
4614 { NULL, NULL }
4617 static UINT dialog_event_handler( msi_dialog *dialog, const WCHAR *event, const WCHAR *argument )
4619 unsigned int i;
4621 TRACE("handling event %s\n", debugstr_w(event));
4623 if (!event) return ERROR_SUCCESS;
4625 for (i = 0; control_events[i].event; i++)
4627 if (!strcmpW( control_events[i].event, event ))
4628 return control_events[i].handler( dialog, argument );
4630 FIXME("unhandled event %s arg(%s)\n", debugstr_w(event), debugstr_w(argument));
4631 return ERROR_SUCCESS;