d3d10/effect: Use case-insensitive comparison in GetMemberTypeBySemantic().
[wine.git] / dlls / msi / dialog.c
blob703f9d4395735f19dfb446022c412e715d7b76b8
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2005 Mike McCormack for CodeWeavers
5 * Copyright 2005 Aric Stewart for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define COBJMACROS
23 #define NONAMELESSUNION
25 #include <stdarg.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "winnls.h"
32 #include "msi.h"
33 #include "msidefs.h"
34 #include "ocidl.h"
35 #include "olectl.h"
36 #include "richedit.h"
37 #include "commctrl.h"
38 #include "winreg.h"
39 #include "shlwapi.h"
40 #include "shellapi.h"
42 #include "wine/debug.h"
44 #include "msipriv.h"
45 #include "msiserver.h"
46 #include "resource.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(msi);
50 extern HINSTANCE msi_hInstance;
52 struct msi_control_tag;
53 typedef struct msi_control_tag msi_control;
54 typedef UINT (*msi_handler)( msi_dialog *, msi_control *, WPARAM );
55 typedef void (*msi_update)( msi_dialog *, msi_control * );
56 typedef UINT (*control_event_handler)( msi_dialog *, const WCHAR *, const WCHAR * );
57 typedef UINT (*event_handler)( msi_dialog *, const WCHAR * );
59 struct msi_control_tag
61 struct list entry;
62 HWND hwnd;
63 msi_handler handler;
64 msi_update update;
65 LPWSTR property;
66 LPWSTR value;
67 HBITMAP hBitmap;
68 HICON hIcon;
69 HIMAGELIST hImageList;
70 LPWSTR tabnext;
71 LPWSTR type;
72 HMODULE hDll;
73 float progress_current;
74 float progress_max;
75 BOOL progress_backwards;
76 DWORD attributes;
77 WCHAR name[1];
80 typedef struct msi_font_tag
82 struct list entry;
83 HFONT hfont;
84 COLORREF color;
85 WCHAR name[1];
86 } msi_font;
88 struct msi_dialog_tag
90 MSIPACKAGE *package;
91 msi_dialog *parent;
92 control_event_handler event_handler;
93 BOOL finished;
94 INT scale;
95 DWORD attributes;
96 SIZE size;
97 HWND hwnd;
98 LPWSTR default_font;
99 struct list fonts;
100 struct list controls;
101 HWND hWndFocus;
102 LPWSTR control_default;
103 LPWSTR control_cancel;
104 event_handler pending_event;
105 LPWSTR pending_argument;
106 INT retval;
107 WCHAR name[1];
110 struct subscriber
112 struct list entry;
113 msi_dialog *dialog;
114 WCHAR *event;
115 WCHAR *control;
116 WCHAR *attribute;
119 typedef UINT (*msi_dialog_control_func)( msi_dialog *dialog, MSIRECORD *rec );
120 struct control_handler
122 LPCWSTR control_type;
123 msi_dialog_control_func func;
126 typedef struct
128 msi_dialog* dialog;
129 msi_control *parent;
130 LPWSTR propval;
131 } radio_button_group_descr;
133 /* dialog sequencing */
135 #define WM_MSI_DIALOG_CREATE (WM_USER+0x100)
136 #define WM_MSI_DIALOG_DESTROY (WM_USER+0x101)
138 #define USER_INSTALLSTATE_ALL 0x1000
140 static DWORD uiThreadId;
141 static HWND hMsiHiddenWindow;
143 static LPWSTR msi_get_window_text( HWND hwnd )
145 UINT sz, r;
146 LPWSTR buf;
148 sz = 0x20;
149 buf = msi_alloc( sz*sizeof(WCHAR) );
150 while ( buf )
152 r = GetWindowTextW( hwnd, buf, sz );
153 if ( r < (sz - 1) )
154 break;
155 sz *= 2;
156 buf = msi_realloc( buf, sz*sizeof(WCHAR) );
159 return buf;
162 static INT msi_dialog_scale_unit( msi_dialog *dialog, INT val )
164 return MulDiv( val, dialog->scale, 12 );
167 static msi_control *msi_dialog_find_control( msi_dialog *dialog, LPCWSTR name )
169 msi_control *control;
171 if( !name )
172 return NULL;
173 if( !dialog->hwnd )
174 return NULL;
175 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
176 if( !wcscmp( control->name, name ) ) /* FIXME: case sensitive? */
177 return control;
178 return NULL;
181 static msi_control *msi_dialog_find_control_by_type( msi_dialog *dialog, LPCWSTR type )
183 msi_control *control;
185 if( !type )
186 return NULL;
187 if( !dialog->hwnd )
188 return NULL;
189 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
190 if( !wcscmp( control->type, type ) ) /* FIXME: case sensitive? */
191 return control;
192 return NULL;
195 static msi_control *msi_dialog_find_control_by_hwnd( msi_dialog *dialog, HWND hwnd )
197 msi_control *control;
199 if( !dialog->hwnd )
200 return NULL;
201 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
202 if( hwnd == control->hwnd )
203 return control;
204 return NULL;
207 static LPWSTR msi_get_deformatted_field( MSIPACKAGE *package, MSIRECORD *rec, int field )
209 LPCWSTR str = MSI_RecordGetString( rec, field );
210 LPWSTR ret = NULL;
212 if (str)
213 deformat_string( package, str, &ret );
214 return ret;
217 static LPWSTR msi_dialog_dup_property( msi_dialog *dialog, LPCWSTR property, BOOL indirect )
219 LPWSTR prop = NULL;
221 if (!property)
222 return NULL;
224 if (indirect)
225 prop = msi_dup_property( dialog->package->db, property );
227 if (!prop)
228 prop = strdupW( property );
230 return prop;
234 * msi_dialog_get_style
236 * Extract the {\style} string from the front of the text to display and
237 * update the pointer. Only the last style in a list is applied.
239 static LPWSTR msi_dialog_get_style( LPCWSTR p, LPCWSTR *rest )
241 LPWSTR ret;
242 LPCWSTR q, i, first;
243 DWORD len;
245 q = NULL;
246 *rest = p;
247 if( !p )
248 return NULL;
250 while ((first = wcschr( p, '{' )) && (q = wcschr( first + 1, '}' )))
252 p = first + 1;
253 if( *p != '\\' && *p != '&' )
254 return NULL;
256 /* little bit of sanity checking to stop us getting confused with RTF */
257 for( i=++p; i<q; i++ )
258 if( *i == '}' || *i == '\\' )
259 return NULL;
262 if (!q)
263 return NULL;
265 *rest = ++q;
266 len = q - p;
268 ret = msi_alloc( len*sizeof(WCHAR) );
269 if( !ret )
270 return ret;
271 memcpy( ret, p, len*sizeof(WCHAR) );
272 ret[len-1] = 0;
273 return ret;
276 static UINT msi_dialog_add_font( MSIRECORD *rec, LPVOID param )
278 msi_dialog *dialog = param;
279 msi_font *font;
280 LPCWSTR face, name;
281 LOGFONTW lf;
282 INT style;
283 HDC hdc;
285 /* create a font and add it to the list */
286 name = MSI_RecordGetString( rec, 1 );
287 font = msi_alloc( FIELD_OFFSET( msi_font, name[lstrlenW( name ) + 1] ));
288 lstrcpyW( font->name, name );
289 list_add_head( &dialog->fonts, &font->entry );
291 font->color = MSI_RecordGetInteger( rec, 4 );
293 memset( &lf, 0, sizeof lf );
294 face = MSI_RecordGetString( rec, 2 );
295 lf.lfHeight = MSI_RecordGetInteger( rec, 3 );
296 style = MSI_RecordGetInteger( rec, 5 );
297 if( style & msidbTextStyleStyleBitsBold )
298 lf.lfWeight = FW_BOLD;
299 if( style & msidbTextStyleStyleBitsItalic )
300 lf.lfItalic = TRUE;
301 if( style & msidbTextStyleStyleBitsUnderline )
302 lf.lfUnderline = TRUE;
303 if( style & msidbTextStyleStyleBitsStrike )
304 lf.lfStrikeOut = TRUE;
305 lstrcpynW( lf.lfFaceName, face, LF_FACESIZE );
307 /* adjust the height */
308 hdc = GetDC( dialog->hwnd );
309 if (hdc)
311 lf.lfHeight = -MulDiv(lf.lfHeight, GetDeviceCaps(hdc, LOGPIXELSY), 72);
312 ReleaseDC( dialog->hwnd, hdc );
315 font->hfont = CreateFontIndirectW( &lf );
317 TRACE("Adding font style %s\n", debugstr_w(font->name) );
319 return ERROR_SUCCESS;
322 static msi_font *msi_dialog_find_font( msi_dialog *dialog, LPCWSTR name )
324 msi_font *font = NULL;
326 LIST_FOR_EACH_ENTRY( font, &dialog->fonts, msi_font, entry )
327 if( !wcscmp( font->name, name ) ) /* FIXME: case sensitive? */
328 break;
330 return font;
333 static UINT msi_dialog_set_font( msi_dialog *dialog, HWND hwnd, LPCWSTR name )
335 msi_font *font;
337 font = msi_dialog_find_font( dialog, name );
338 if( font )
339 SendMessageW( hwnd, WM_SETFONT, (WPARAM) font->hfont, TRUE );
340 else
341 ERR("No font entry for %s\n", debugstr_w(name));
342 return ERROR_SUCCESS;
345 static UINT msi_dialog_build_font_list( msi_dialog *dialog )
347 MSIQUERY *view;
348 UINT r;
350 TRACE("dialog %p\n", dialog );
352 r = MSI_OpenQuery( dialog->package->db, &view, L"SELECT * FROM `TextStyle`" );
353 if( r != ERROR_SUCCESS )
354 return r;
356 r = MSI_IterateRecords( view, NULL, msi_dialog_add_font, dialog );
357 msiobj_release( &view->hdr );
358 return r;
361 static void msi_destroy_control( msi_control *t )
363 list_remove( &t->entry );
364 /* leave dialog->hwnd - destroying parent destroys child windows */
365 msi_free( t->property );
366 msi_free( t->value );
367 if( t->hBitmap )
368 DeleteObject( t->hBitmap );
369 if( t->hIcon )
370 DestroyIcon( t->hIcon );
371 if ( t->hImageList )
372 ImageList_Destroy( t->hImageList );
373 msi_free( t->tabnext );
374 msi_free( t->type );
375 if (t->hDll)
376 FreeLibrary( t->hDll );
377 msi_free( t );
380 static msi_control *dialog_create_window( msi_dialog *dialog, MSIRECORD *rec, DWORD exstyle,
381 const WCHAR *szCls, const WCHAR *name, const WCHAR *text,
382 DWORD style, HWND parent )
384 DWORD x, y, width, height;
385 LPWSTR font = NULL, title_font = NULL;
386 LPCWSTR title = NULL;
387 msi_control *control;
389 style |= WS_CHILD;
391 control = msi_alloc( FIELD_OFFSET( msi_control, name[lstrlenW( name ) + 1] ));
392 if (!control)
393 return NULL;
395 lstrcpyW( control->name, name );
396 list_add_tail( &dialog->controls, &control->entry );
397 control->handler = NULL;
398 control->update = NULL;
399 control->property = NULL;
400 control->value = NULL;
401 control->hBitmap = NULL;
402 control->hIcon = NULL;
403 control->hImageList = NULL;
404 control->hDll = NULL;
405 control->tabnext = strdupW( MSI_RecordGetString( rec, 11) );
406 control->type = strdupW( MSI_RecordGetString( rec, 3 ) );
407 control->progress_current = 0;
408 control->progress_max = 100;
409 control->progress_backwards = FALSE;
411 x = MSI_RecordGetInteger( rec, 4 );
412 y = MSI_RecordGetInteger( rec, 5 );
413 width = MSI_RecordGetInteger( rec, 6 );
414 height = MSI_RecordGetInteger( rec, 7 );
416 x = msi_dialog_scale_unit( dialog, x );
417 y = msi_dialog_scale_unit( dialog, y );
418 width = msi_dialog_scale_unit( dialog, width );
419 height = msi_dialog_scale_unit( dialog, height );
421 if( text )
423 deformat_string( dialog->package, text, &title_font );
424 font = msi_dialog_get_style( title_font, &title );
427 control->hwnd = CreateWindowExW( exstyle, szCls, title, style,
428 x, y, width, height, parent, NULL, NULL, NULL );
430 TRACE("Dialog %s control %s hwnd %p\n",
431 debugstr_w(dialog->name), debugstr_w(text), control->hwnd );
433 msi_dialog_set_font( dialog, control->hwnd,
434 font ? font : dialog->default_font );
436 msi_free( title_font );
437 msi_free( font );
439 return control;
442 static LPWSTR msi_dialog_get_uitext( msi_dialog *dialog, LPCWSTR key )
444 MSIRECORD *rec;
445 LPWSTR text;
447 rec = MSI_QueryGetRecord( dialog->package->db, L"SELECT * FROM `UIText` WHERE `Key` = '%s'", key );
448 if (!rec) return NULL;
449 text = strdupW( MSI_RecordGetString( rec, 2 ) );
450 msiobj_release( &rec->hdr );
451 return text;
454 static HANDLE msi_load_image( MSIDATABASE *db, LPCWSTR name, UINT type,
455 UINT cx, UINT cy, UINT flags )
457 MSIRECORD *rec;
458 HANDLE himage = NULL;
459 LPWSTR tmp;
460 UINT r;
462 TRACE("%p %s %u %u %08x\n", db, debugstr_w(name), cx, cy, flags);
464 if (!(tmp = msi_create_temp_file( db ))) return NULL;
466 rec = MSI_QueryGetRecord( db, L"SELECT * FROM `Binary` WHERE `Name` = '%s'", name );
467 if( rec )
469 r = MSI_RecordStreamToFile( rec, 2, tmp );
470 if( r == ERROR_SUCCESS )
472 himage = LoadImageW( 0, tmp, type, cx, cy, flags );
474 msiobj_release( &rec->hdr );
476 DeleteFileW( tmp );
478 msi_free( tmp );
479 return himage;
482 static HICON msi_load_icon( MSIDATABASE *db, LPCWSTR text, UINT attributes )
484 DWORD cx = 0, cy = 0, flags;
486 flags = LR_LOADFROMFILE | LR_DEFAULTSIZE;
487 if( attributes & msidbControlAttributesFixedSize )
489 flags &= ~LR_DEFAULTSIZE;
490 if( attributes & msidbControlAttributesIconSize16 )
492 cx += 16;
493 cy += 16;
495 if( attributes & msidbControlAttributesIconSize32 )
497 cx += 32;
498 cy += 32;
500 /* msidbControlAttributesIconSize48 handled by above logic */
502 return msi_load_image( db, text, IMAGE_ICON, cx, cy, flags );
505 static void msi_dialog_update_controls( msi_dialog *dialog, LPCWSTR property )
507 msi_control *control;
509 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
511 if ( control->property && !wcscmp( control->property, property ) && control->update )
512 control->update( dialog, control );
516 static void msi_dialog_update_all_controls( msi_dialog *dialog )
518 msi_control *control;
520 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
522 if ( control->property && control->update )
523 control->update( dialog, control );
527 static void msi_dialog_set_property( MSIPACKAGE *package, LPCWSTR property, LPCWSTR value )
529 UINT r = msi_set_property( package->db, property, value, -1 );
530 if (r == ERROR_SUCCESS && !wcscmp( property, L"SourceDir" ))
531 msi_reset_source_folders( package );
534 static MSIFEATURE *msi_seltree_feature_from_item( HWND hwnd, HTREEITEM hItem )
536 TVITEMW tvi;
538 /* get the feature from the item */
539 memset( &tvi, 0, sizeof tvi );
540 tvi.hItem = hItem;
541 tvi.mask = TVIF_PARAM | TVIF_HANDLE;
542 SendMessageW( hwnd, TVM_GETITEMW, 0, (LPARAM)&tvi );
543 return (MSIFEATURE *)tvi.lParam;
546 struct msi_selection_tree_info
548 msi_dialog *dialog;
549 HWND hwnd;
550 WNDPROC oldproc;
551 HTREEITEM selected;
554 static MSIFEATURE *msi_seltree_get_selected_feature( msi_control *control )
556 struct msi_selection_tree_info *info = GetPropW( control->hwnd, L"MSIDATA" );
557 return msi_seltree_feature_from_item( control->hwnd, info->selected );
560 static void dialog_handle_event( msi_dialog *dialog, const WCHAR *control,
561 const WCHAR *attribute, MSIRECORD *rec )
563 msi_control* ctrl;
565 ctrl = msi_dialog_find_control( dialog, control );
566 if (!ctrl)
567 return;
568 if( !wcscmp( attribute, L"Text" ) )
570 const WCHAR *font_text, *text = NULL;
571 WCHAR *font, *text_fmt = NULL;
573 font_text = MSI_RecordGetString( rec , 1 );
574 font = msi_dialog_get_style( font_text, &text );
575 deformat_string( dialog->package, text, &text_fmt );
576 if (text_fmt) text = text_fmt;
577 else text = L"";
579 SetWindowTextW( ctrl->hwnd, text );
581 msi_free( font );
582 msi_free( text_fmt );
583 msi_dialog_check_messages( NULL );
585 else if( !wcscmp( attribute, L"Progress" ) )
587 DWORD func, val1, val2, units;
589 func = MSI_RecordGetInteger( rec, 1 );
590 val1 = MSI_RecordGetInteger( rec, 2 );
591 val2 = MSI_RecordGetInteger( rec, 3 );
593 TRACE("progress: func %u val1 %u val2 %u\n", func, val1, val2);
595 units = val1 / 512;
596 switch (func)
598 case 0: /* init */
599 SendMessageW( ctrl->hwnd, PBM_SETRANGE, 0, MAKELPARAM(0,100) );
600 if (val2)
602 ctrl->progress_max = units ? units : 100;
603 ctrl->progress_current = units;
604 ctrl->progress_backwards = TRUE;
605 SendMessageW( ctrl->hwnd, PBM_SETPOS, 100, 0 );
607 else
609 ctrl->progress_max = units ? units : 100;
610 ctrl->progress_current = 0;
611 ctrl->progress_backwards = FALSE;
612 SendMessageW( ctrl->hwnd, PBM_SETPOS, 0, 0 );
614 break;
615 case 1: /* action data increment */
616 if (val2) dialog->package->action_progress_increment = val1;
617 else dialog->package->action_progress_increment = 0;
618 break;
619 case 2: /* move */
620 if (ctrl->progress_backwards)
622 if (units >= ctrl->progress_current) ctrl->progress_current -= units;
623 else ctrl->progress_current = 0;
625 else
627 if (ctrl->progress_current + units < ctrl->progress_max) ctrl->progress_current += units;
628 else ctrl->progress_current = ctrl->progress_max;
630 SendMessageW( ctrl->hwnd, PBM_SETPOS, MulDiv(100, ctrl->progress_current, ctrl->progress_max), 0 );
631 break;
632 case 3: /* add */
633 ctrl->progress_max += units;
634 break;
635 default:
636 FIXME("Unknown progress message %u\n", func);
637 break;
640 else if ( !wcscmp( attribute, L"Property" ) )
642 MSIFEATURE *feature = msi_seltree_get_selected_feature( ctrl );
643 if (feature) msi_dialog_set_property( dialog->package, ctrl->property, feature->Directory );
645 else if ( !wcscmp( attribute, L"SelectionPath" ) )
647 BOOL indirect = ctrl->attributes & msidbControlAttributesIndirect;
648 LPWSTR path = msi_dialog_dup_property( dialog, ctrl->property, indirect );
649 if (!path) return;
650 SetWindowTextW( ctrl->hwnd, path );
651 msi_free(path);
653 else
655 FIXME("Attribute %s not being set\n", debugstr_w(attribute));
656 return;
660 static void event_subscribe( msi_dialog *dialog, const WCHAR *event, const WCHAR *control, const WCHAR *attribute )
662 struct subscriber *sub;
664 TRACE("dialog %s event %s control %s attribute %s\n", debugstr_w(dialog->name), debugstr_w(event),
665 debugstr_w(control), debugstr_w(attribute));
667 LIST_FOR_EACH_ENTRY( sub, &dialog->package->subscriptions, struct subscriber, entry )
669 if (sub->dialog == dialog &&
670 !wcsicmp( sub->event, event ) &&
671 !wcsicmp( sub->control, control ) &&
672 !wcsicmp( sub->attribute, attribute ))
674 TRACE("already subscribed\n");
675 return;
678 if (!(sub = msi_alloc( sizeof(*sub) ))) return;
679 sub->dialog = dialog;
680 sub->event = strdupW( event );
681 sub->control = strdupW( control );
682 sub->attribute = strdupW( attribute );
683 list_add_tail( &dialog->package->subscriptions, &sub->entry );
686 struct dialog_control
688 msi_dialog *dialog;
689 const WCHAR *control;
692 static UINT map_event( MSIRECORD *row, void *param )
694 struct dialog_control *dc = param;
695 const WCHAR *event = MSI_RecordGetString( row, 3 );
696 const WCHAR *attribute = MSI_RecordGetString( row, 4 );
698 event_subscribe( dc->dialog, event, dc->control, attribute );
699 return ERROR_SUCCESS;
702 static void dialog_map_events( msi_dialog *dialog, const WCHAR *control )
704 MSIQUERY *view;
705 struct dialog_control dialog_control =
707 dialog,
708 control
711 if (!MSI_OpenQuery( dialog->package->db, &view,
712 L"SELECT * FROM `EventMapping` WHERE `Dialog_` = '%s' AND `Control_` = '%s'",
713 dialog->name, control ))
715 MSI_IterateRecords( view, NULL, map_event, &dialog_control );
716 msiobj_release( &view->hdr );
720 /* everything except radio buttons */
721 static msi_control *msi_dialog_add_control( msi_dialog *dialog,
722 MSIRECORD *rec, LPCWSTR szCls, DWORD style )
724 DWORD attributes;
725 const WCHAR *text = NULL, *name, *control_type;
726 DWORD exstyle = 0;
728 name = MSI_RecordGetString( rec, 2 );
729 control_type = MSI_RecordGetString( rec, 3 );
730 attributes = MSI_RecordGetInteger( rec, 8 );
731 if (wcscmp( control_type, L"ScrollableText" )) text = MSI_RecordGetString( rec, 10 );
733 TRACE("%s, %s, %08x, %s, %08x\n", debugstr_w(szCls), debugstr_w(name),
734 attributes, debugstr_w(text), style);
736 if( attributes & msidbControlAttributesVisible )
737 style |= WS_VISIBLE;
738 if( ~attributes & msidbControlAttributesEnabled )
739 style |= WS_DISABLED;
740 if( attributes & msidbControlAttributesSunken )
741 exstyle |= WS_EX_CLIENTEDGE;
743 dialog_map_events( dialog, name );
745 return dialog_create_window( dialog, rec, exstyle, szCls, name, text, style, dialog->hwnd );
748 struct msi_text_info
750 msi_font *font;
751 WNDPROC oldproc;
752 DWORD attributes;
756 * we don't erase our own background,
757 * so we have to make sure that the parent window redraws first
759 static void msi_text_on_settext( HWND hWnd )
761 HWND hParent;
762 RECT rc;
764 hParent = GetParent( hWnd );
765 GetWindowRect( hWnd, &rc );
766 MapWindowPoints( NULL, hParent, (LPPOINT) &rc, 2 );
767 InvalidateRect( hParent, &rc, TRUE );
770 static LRESULT WINAPI
771 MSIText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
773 struct msi_text_info *info;
774 LRESULT r = 0;
776 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
778 info = GetPropW(hWnd, L"MSIDATA");
780 if( msg == WM_CTLCOLORSTATIC &&
781 ( info->attributes & msidbControlAttributesTransparent ) )
783 SetBkMode( (HDC)wParam, TRANSPARENT );
784 return (LRESULT) GetStockObject(NULL_BRUSH);
787 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
788 if ( info->font )
789 SetTextColor( (HDC)wParam, info->font->color );
791 switch( msg )
793 case WM_SETTEXT:
794 msi_text_on_settext( hWnd );
795 break;
796 case WM_NCDESTROY:
797 msi_free( info );
798 RemovePropW( hWnd, L"MSIDATA" );
799 break;
802 return r;
805 static UINT msi_dialog_text_control( msi_dialog *dialog, MSIRECORD *rec )
807 msi_control *control;
808 struct msi_text_info *info;
809 LPCWSTR text, ptr, prop, control_name;
810 LPWSTR font_name;
812 TRACE("%p %p\n", dialog, rec);
814 control = msi_dialog_add_control( dialog, rec, L"Static", SS_LEFT | WS_GROUP );
815 if( !control )
816 return ERROR_FUNCTION_FAILED;
818 info = msi_alloc( sizeof *info );
819 if( !info )
820 return ERROR_SUCCESS;
822 control_name = MSI_RecordGetString( rec, 2 );
823 control->attributes = MSI_RecordGetInteger( rec, 8 );
824 prop = MSI_RecordGetString( rec, 9 );
825 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
827 text = MSI_RecordGetString( rec, 10 );
828 font_name = msi_dialog_get_style( text, &ptr );
829 info->font = ( font_name ) ? msi_dialog_find_font( dialog, font_name ) : NULL;
830 msi_free( font_name );
832 info->attributes = MSI_RecordGetInteger( rec, 8 );
833 if( info->attributes & msidbControlAttributesTransparent )
834 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT );
836 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
837 (LONG_PTR)MSIText_WndProc );
838 SetPropW( control->hwnd, L"MSIDATA", info );
840 event_subscribe( dialog, L"SelectionPath", control_name, L"SelectionPath" );
841 return ERROR_SUCCESS;
844 /* strip any leading text style label from text field */
845 static WCHAR *msi_get_binary_name( MSIPACKAGE *package, MSIRECORD *rec )
847 WCHAR *p, *text;
849 text = msi_get_deformatted_field( package, rec, 10 );
850 if (!text)
851 return NULL;
853 p = text;
854 while (*p && *p != '{') p++;
855 if (!*p++) return text;
857 while (*p && *p != '}') p++;
858 if (!*p++) return text;
860 p = strdupW( p );
861 msi_free( text );
862 return p;
865 static UINT msi_dialog_set_property_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
867 LPWSTR p, prop, arg_fmt = NULL;
868 UINT len;
870 len = lstrlenW( event );
871 prop = msi_alloc( len * sizeof(WCHAR) );
872 lstrcpyW( prop, &event[1] );
873 p = wcschr( prop, ']' );
874 if (p && (p[1] == 0 || p[1] == ' '))
876 *p = 0;
877 if (wcscmp( L"{}", arg )) deformat_string( dialog->package, arg, &arg_fmt );
878 msi_dialog_set_property( dialog->package, prop, arg_fmt );
879 msi_dialog_update_controls( dialog, prop );
880 msi_free( arg_fmt );
882 else ERR("Badly formatted property string - what happens?\n");
883 msi_free( prop );
884 return ERROR_SUCCESS;
887 static UINT msi_dialog_send_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
889 LPWSTR event_fmt = NULL, arg_fmt = NULL;
891 TRACE("Sending control event %s %s\n", debugstr_w(event), debugstr_w(arg));
893 deformat_string( dialog->package, event, &event_fmt );
894 deformat_string( dialog->package, arg, &arg_fmt );
896 dialog->event_handler( dialog, event_fmt, arg_fmt );
898 msi_free( event_fmt );
899 msi_free( arg_fmt );
901 return ERROR_SUCCESS;
904 static UINT msi_dialog_control_event( MSIRECORD *rec, LPVOID param )
906 msi_dialog *dialog = param;
907 LPCWSTR condition, event, arg;
908 UINT r;
910 condition = MSI_RecordGetString( rec, 5 );
911 r = MSI_EvaluateConditionW( dialog->package, condition );
912 if (r == MSICONDITION_TRUE || r == MSICONDITION_NONE)
914 event = MSI_RecordGetString( rec, 3 );
915 arg = MSI_RecordGetString( rec, 4 );
916 if (event[0] == '[')
917 msi_dialog_set_property_event( dialog, event, arg );
918 else
919 msi_dialog_send_event( dialog, event, arg );
921 return ERROR_SUCCESS;
924 static UINT msi_dialog_button_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
926 MSIQUERY *view;
927 UINT r;
929 if (HIWORD(param) != BN_CLICKED)
930 return ERROR_SUCCESS;
932 r = MSI_OpenQuery( dialog->package->db, &view,
933 L"SELECT * FROM `ControlEvent` WHERE `Dialog_` = '%s' AND `Control_` = '%s' ORDER BY `Ordering`",
934 dialog->name, control->name );
935 if (r != ERROR_SUCCESS)
937 ERR("query failed\n");
938 return ERROR_SUCCESS;
940 r = MSI_IterateRecords( view, 0, msi_dialog_control_event, dialog );
941 msiobj_release( &view->hdr );
943 /* dialog control events must be processed last regardless of ordering */
944 if (dialog->pending_event)
946 r = dialog->pending_event( dialog, dialog->pending_argument );
948 msi_free( dialog->pending_argument );
949 dialog->pending_event = NULL;
950 dialog->pending_argument = NULL;
952 return r;
955 static HBITMAP msi_load_picture( MSIDATABASE *db, const WCHAR *name, INT cx, INT cy, DWORD flags )
957 HBITMAP hOleBitmap = 0, hBitmap = 0, hOldSrcBitmap, hOldDestBitmap;
958 MSIRECORD *rec = NULL;
959 IStream *stm = NULL;
960 IPicture *pic = NULL;
961 HDC srcdc, destdc;
962 BITMAP bm;
963 UINT r;
965 rec = MSI_QueryGetRecord( db, L"SELECT * FROM `Binary` WHERE `Name` = '%s'", name );
966 if (!rec)
967 goto end;
969 r = MSI_RecordGetIStream( rec, 2, &stm );
970 msiobj_release( &rec->hdr );
971 if (r != ERROR_SUCCESS)
972 goto end;
974 r = OleLoadPicture( stm, 0, TRUE, &IID_IPicture, (void **)&pic );
975 IStream_Release( stm );
976 if (FAILED( r ))
978 ERR("failed to load picture\n");
979 goto end;
982 r = IPicture_get_Handle( pic, (OLE_HANDLE *)&hOleBitmap );
983 if (FAILED( r ))
985 ERR("failed to get bitmap handle\n");
986 goto end;
989 /* make the bitmap the desired size */
990 r = GetObjectW( hOleBitmap, sizeof(bm), &bm );
991 if (r != sizeof(bm))
993 ERR("failed to get bitmap size\n");
994 goto end;
997 if (flags & LR_DEFAULTSIZE)
999 cx = bm.bmWidth;
1000 cy = bm.bmHeight;
1003 srcdc = CreateCompatibleDC( NULL );
1004 hOldSrcBitmap = SelectObject( srcdc, hOleBitmap );
1005 destdc = CreateCompatibleDC( NULL );
1006 hBitmap = CreateCompatibleBitmap( srcdc, cx, cy );
1007 hOldDestBitmap = SelectObject( destdc, hBitmap );
1008 StretchBlt( destdc, 0, 0, cx, cy, srcdc, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY );
1009 SelectObject( srcdc, hOldSrcBitmap );
1010 SelectObject( destdc, hOldDestBitmap );
1011 DeleteDC( srcdc );
1012 DeleteDC( destdc );
1014 end:
1015 if (pic) IPicture_Release( pic );
1016 return hBitmap;
1019 static UINT msi_dialog_button_control( msi_dialog *dialog, MSIRECORD *rec )
1021 msi_control *control;
1022 UINT attributes, style, cx = 0, cy = 0, flags = 0;
1023 WCHAR *name = NULL;
1025 TRACE("%p %p\n", dialog, rec);
1027 style = WS_TABSTOP;
1028 attributes = MSI_RecordGetInteger( rec, 8 );
1029 if (attributes & msidbControlAttributesIcon) style |= BS_ICON;
1030 else if (attributes & msidbControlAttributesBitmap)
1032 style |= BS_BITMAP;
1033 if (attributes & msidbControlAttributesFixedSize) flags |= LR_DEFAULTSIZE;
1034 else
1036 cx = msi_dialog_scale_unit( dialog, MSI_RecordGetInteger(rec, 6) );
1037 cy = msi_dialog_scale_unit( dialog, MSI_RecordGetInteger(rec, 7) );
1041 control = msi_dialog_add_control( dialog, rec, L"BUTTON", style );
1042 if (!control)
1043 return ERROR_FUNCTION_FAILED;
1045 control->handler = msi_dialog_button_handler;
1047 if (attributes & msidbControlAttributesIcon)
1049 name = msi_get_binary_name( dialog->package, rec );
1050 control->hIcon = msi_load_icon( dialog->package->db, name, attributes );
1051 if (control->hIcon)
1053 SendMessageW( control->hwnd, BM_SETIMAGE, IMAGE_ICON, (LPARAM) control->hIcon );
1055 else ERR("Failed to load icon %s\n", debugstr_w(name));
1057 else if (attributes & msidbControlAttributesBitmap)
1059 name = msi_get_binary_name( dialog->package, rec );
1060 control->hBitmap = msi_load_picture( dialog->package->db, name, cx, cy, flags );
1061 if (control->hBitmap)
1063 SendMessageW( control->hwnd, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM) control->hBitmap );
1065 else ERR("Failed to load bitmap %s\n", debugstr_w(name));
1068 msi_free( name );
1069 return ERROR_SUCCESS;
1072 static LPWSTR msi_get_checkbox_value( msi_dialog *dialog, LPCWSTR prop )
1074 MSIRECORD *rec = NULL;
1075 LPWSTR ret = NULL;
1077 /* find if there is a value associated with the checkbox */
1078 rec = MSI_QueryGetRecord( dialog->package->db, L"SELECT * FROM `CheckBox` WHERE `Property` = '%s'", prop );
1079 if (!rec)
1080 return ret;
1082 ret = msi_get_deformatted_field( dialog->package, rec, 2 );
1083 if( ret && !ret[0] )
1085 msi_free( ret );
1086 ret = NULL;
1088 msiobj_release( &rec->hdr );
1089 if (ret)
1090 return ret;
1092 ret = msi_dup_property( dialog->package->db, prop );
1093 if( ret && !ret[0] )
1095 msi_free( ret );
1096 ret = NULL;
1099 return ret;
1102 static UINT msi_dialog_get_checkbox_state( msi_dialog *dialog, msi_control *control )
1104 WCHAR state[2] = {0};
1105 DWORD sz = 2;
1107 msi_get_property( dialog->package->db, control->property, state, &sz );
1108 return state[0] ? 1 : 0;
1111 static void msi_dialog_set_checkbox_state( msi_dialog *dialog, msi_control *control, UINT state )
1113 LPCWSTR val;
1115 /* if uncheck then the property is set to NULL */
1116 if (!state)
1118 msi_dialog_set_property( dialog->package, control->property, NULL );
1119 return;
1122 /* check for a custom state */
1123 if (control->value && control->value[0])
1124 val = control->value;
1125 else
1126 val = L"1";
1128 msi_dialog_set_property( dialog->package, control->property, val );
1131 static void msi_dialog_checkbox_sync_state( msi_dialog *dialog, msi_control *control )
1133 UINT state = msi_dialog_get_checkbox_state( dialog, control );
1134 SendMessageW( control->hwnd, BM_SETCHECK, state ? BST_CHECKED : BST_UNCHECKED, 0 );
1137 static UINT msi_dialog_checkbox_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
1139 UINT state;
1141 if (HIWORD(param) != BN_CLICKED)
1142 return ERROR_SUCCESS;
1144 TRACE("clicked checkbox %s, set %s\n", debugstr_w(control->name), debugstr_w(control->property));
1146 state = msi_dialog_get_checkbox_state( dialog, control );
1147 state = state ? 0 : 1;
1148 msi_dialog_set_checkbox_state( dialog, control, state );
1149 msi_dialog_checkbox_sync_state( dialog, control );
1151 return msi_dialog_button_handler( dialog, control, param );
1154 static UINT msi_dialog_checkbox_control( msi_dialog *dialog, MSIRECORD *rec )
1156 msi_control *control;
1157 LPCWSTR prop;
1159 TRACE("%p %p\n", dialog, rec);
1161 control = msi_dialog_add_control( dialog, rec, L"BUTTON", BS_CHECKBOX | BS_MULTILINE | WS_TABSTOP );
1162 control->handler = msi_dialog_checkbox_handler;
1163 control->update = msi_dialog_checkbox_sync_state;
1164 prop = MSI_RecordGetString( rec, 9 );
1165 if (prop)
1167 control->property = strdupW( prop );
1168 control->value = msi_get_checkbox_value( dialog, prop );
1169 TRACE("control %s value %s\n", debugstr_w(control->property), debugstr_w(control->value));
1171 msi_dialog_checkbox_sync_state( dialog, control );
1172 return ERROR_SUCCESS;
1175 static UINT msi_dialog_line_control( msi_dialog *dialog, MSIRECORD *rec )
1177 DWORD attributes;
1178 LPCWSTR name;
1179 DWORD style, exstyle = 0;
1180 DWORD x, y, width, height;
1181 msi_control *control;
1183 TRACE("%p %p\n", dialog, rec);
1185 style = WS_CHILD | SS_ETCHEDHORZ | SS_SUNKEN;
1187 name = MSI_RecordGetString( rec, 2 );
1188 attributes = MSI_RecordGetInteger( rec, 8 );
1190 if( attributes & msidbControlAttributesVisible )
1191 style |= WS_VISIBLE;
1192 if( ~attributes & msidbControlAttributesEnabled )
1193 style |= WS_DISABLED;
1194 if( attributes & msidbControlAttributesSunken )
1195 exstyle |= WS_EX_CLIENTEDGE;
1197 dialog_map_events( dialog, name );
1199 control = msi_alloc( FIELD_OFFSET(msi_control, name[lstrlenW( name ) + 1] ));
1200 if (!control)
1201 return ERROR_OUTOFMEMORY;
1203 lstrcpyW( control->name, name );
1204 list_add_head( &dialog->controls, &control->entry );
1205 control->handler = NULL;
1206 control->property = NULL;
1207 control->value = NULL;
1208 control->hBitmap = NULL;
1209 control->hIcon = NULL;
1210 control->hDll = NULL;
1211 control->tabnext = strdupW( MSI_RecordGetString( rec, 11) );
1212 control->type = strdupW( MSI_RecordGetString( rec, 3 ) );
1213 control->progress_current = 0;
1214 control->progress_max = 100;
1215 control->progress_backwards = FALSE;
1217 x = MSI_RecordGetInteger( rec, 4 );
1218 y = MSI_RecordGetInteger( rec, 5 );
1219 width = MSI_RecordGetInteger( rec, 6 );
1221 x = msi_dialog_scale_unit( dialog, x );
1222 y = msi_dialog_scale_unit( dialog, y );
1223 width = msi_dialog_scale_unit( dialog, width );
1224 height = 2; /* line is exactly 2 units in height */
1226 control->hwnd = CreateWindowExW( exstyle, L"Static", NULL, style,
1227 x, y, width, height, dialog->hwnd, NULL, NULL, NULL );
1229 TRACE("Dialog %s control %s hwnd %p\n",
1230 debugstr_w(dialog->name), debugstr_w(name), control->hwnd );
1232 return ERROR_SUCCESS;
1235 /******************** Scroll Text ********************************************/
1237 struct msi_scrolltext_info
1239 msi_dialog *dialog;
1240 msi_control *control;
1241 WNDPROC oldproc;
1244 static LRESULT WINAPI
1245 MSIScrollText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1247 struct msi_scrolltext_info *info;
1248 HRESULT r;
1250 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1252 info = GetPropW( hWnd, L"MSIDATA" );
1254 r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
1256 switch( msg )
1258 case WM_GETDLGCODE:
1259 return DLGC_WANTARROWS;
1260 case WM_NCDESTROY:
1261 msi_free( info );
1262 RemovePropW( hWnd, L"MSIDATA" );
1263 break;
1264 case WM_PAINT:
1265 /* native MSI sets a wait cursor here */
1266 msi_dialog_button_handler( info->dialog, info->control, BN_CLICKED );
1267 break;
1269 return r;
1272 struct msi_streamin_info
1274 LPSTR string;
1275 DWORD offset;
1276 DWORD length;
1279 static DWORD CALLBACK
1280 msi_richedit_stream_in( DWORD_PTR arg, LPBYTE buffer, LONG count, LONG *pcb )
1282 struct msi_streamin_info *info = (struct msi_streamin_info*) arg;
1284 if( (count + info->offset) > info->length )
1285 count = info->length - info->offset;
1286 memcpy( buffer, &info->string[ info->offset ], count );
1287 *pcb = count;
1288 info->offset += count;
1290 TRACE("%d/%d\n", info->offset, info->length);
1292 return 0;
1295 static void msi_scrolltext_add_text( msi_control *control, LPCWSTR text )
1297 struct msi_streamin_info info;
1298 EDITSTREAM es;
1300 info.string = strdupWtoA( text );
1301 info.offset = 0;
1302 info.length = lstrlenA( info.string ) + 1;
1304 es.dwCookie = (DWORD_PTR) &info;
1305 es.dwError = 0;
1306 es.pfnCallback = msi_richedit_stream_in;
1308 SendMessageW( control->hwnd, EM_STREAMIN, SF_RTF, (LPARAM) &es );
1310 msi_free( info.string );
1313 static UINT msi_dialog_scrolltext_control( msi_dialog *dialog, MSIRECORD *rec )
1315 struct msi_scrolltext_info *info;
1316 msi_control *control;
1317 HMODULE hRichedit;
1318 LPCWSTR text;
1319 DWORD style;
1321 info = msi_alloc( sizeof *info );
1322 if (!info)
1323 return ERROR_FUNCTION_FAILED;
1325 hRichedit = LoadLibraryA("riched20");
1327 style = WS_BORDER | ES_MULTILINE | WS_VSCROLL |
1328 ES_READONLY | ES_AUTOVSCROLL | WS_TABSTOP;
1329 control = msi_dialog_add_control( dialog, rec, L"RichEdit20W", style );
1330 if (!control)
1332 FreeLibrary( hRichedit );
1333 msi_free( info );
1334 return ERROR_FUNCTION_FAILED;
1337 control->hDll = hRichedit;
1339 info->dialog = dialog;
1340 info->control = control;
1342 /* subclass the static control */
1343 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1344 (LONG_PTR)MSIScrollText_WndProc );
1345 SetPropW( control->hwnd, L"MSIDATA", info );
1347 /* add the text into the richedit */
1348 text = MSI_RecordGetString( rec, 10 );
1349 if (text)
1350 msi_scrolltext_add_text( control, text );
1352 return ERROR_SUCCESS;
1356 static UINT msi_dialog_bitmap_control( msi_dialog *dialog, MSIRECORD *rec )
1358 UINT cx, cy, flags, style, attributes;
1359 msi_control *control;
1360 LPWSTR name;
1362 flags = LR_LOADFROMFILE;
1363 style = SS_BITMAP | SS_LEFT | WS_GROUP;
1365 attributes = MSI_RecordGetInteger( rec, 8 );
1366 if( attributes & msidbControlAttributesFixedSize )
1368 flags |= LR_DEFAULTSIZE;
1369 style |= SS_CENTERIMAGE;
1372 control = msi_dialog_add_control( dialog, rec, L"Static", style );
1373 cx = MSI_RecordGetInteger( rec, 6 );
1374 cy = MSI_RecordGetInteger( rec, 7 );
1375 cx = msi_dialog_scale_unit( dialog, cx );
1376 cy = msi_dialog_scale_unit( dialog, cy );
1378 name = msi_get_binary_name( dialog->package, rec );
1379 control->hBitmap = msi_load_picture( dialog->package->db, name, cx, cy, flags );
1380 if( control->hBitmap )
1381 SendMessageW( control->hwnd, STM_SETIMAGE,
1382 IMAGE_BITMAP, (LPARAM) control->hBitmap );
1383 else
1384 ERR("Failed to load bitmap %s\n", debugstr_w(name));
1386 msi_free( name );
1388 return ERROR_SUCCESS;
1391 static UINT msi_dialog_icon_control( msi_dialog *dialog, MSIRECORD *rec )
1393 msi_control *control;
1394 DWORD attributes;
1395 LPWSTR name;
1397 TRACE("\n");
1399 control = msi_dialog_add_control( dialog, rec, L"Static",
1400 SS_ICON | SS_CENTERIMAGE | WS_GROUP );
1402 attributes = MSI_RecordGetInteger( rec, 8 );
1403 name = msi_get_binary_name( dialog->package, rec );
1404 control->hIcon = msi_load_icon( dialog->package->db, name, attributes );
1405 if( control->hIcon )
1406 SendMessageW( control->hwnd, STM_SETICON, (WPARAM) control->hIcon, 0 );
1407 else
1408 ERR("Failed to load bitmap %s\n", debugstr_w(name));
1409 msi_free( name );
1410 return ERROR_SUCCESS;
1413 /******************** Combo Box ***************************************/
1415 struct msi_combobox_info
1417 msi_dialog *dialog;
1418 HWND hwnd;
1419 WNDPROC oldproc;
1420 DWORD num_items;
1421 DWORD addpos_items;
1422 LPWSTR *items;
1425 static LRESULT WINAPI MSIComboBox_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1427 struct msi_combobox_info *info;
1428 LRESULT r;
1429 DWORD j;
1431 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1433 info = GetPropW( hWnd, L"MSIDATA" );
1434 if (!info)
1435 return 0;
1437 r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
1439 switch (msg)
1441 case WM_NCDESTROY:
1442 for (j = 0; j < info->num_items; j++)
1443 msi_free( info->items[j] );
1444 msi_free( info->items );
1445 msi_free( info );
1446 RemovePropW( hWnd, L"MSIDATA" );
1447 break;
1450 return r;
1453 static UINT msi_combobox_add_item( MSIRECORD *rec, LPVOID param )
1455 struct msi_combobox_info *info = param;
1456 LPCWSTR value, text;
1457 int pos;
1459 value = MSI_RecordGetString( rec, 3 );
1460 text = MSI_RecordGetString( rec, 4 );
1462 info->items[info->addpos_items] = strdupW( value );
1464 pos = SendMessageW( info->hwnd, CB_ADDSTRING, 0, (LPARAM)text );
1465 SendMessageW( info->hwnd, CB_SETITEMDATA, pos, (LPARAM)info->items[info->addpos_items] );
1466 info->addpos_items++;
1468 return ERROR_SUCCESS;
1471 static UINT msi_combobox_add_items( struct msi_combobox_info *info, LPCWSTR property )
1473 MSIQUERY *view;
1474 DWORD count;
1475 UINT r;
1477 r = MSI_OpenQuery( info->dialog->package->db, &view,
1478 L"SELECT * FROM `ComboBox` WHERE `Property` = '%s' ORDER BY `Order`", property );
1479 if (r != ERROR_SUCCESS)
1480 return r;
1482 /* just get the number of records */
1483 count = 0;
1484 r = MSI_IterateRecords( view, &count, NULL, NULL );
1485 if (r != ERROR_SUCCESS)
1487 msiobj_release( &view->hdr );
1488 return r;
1490 info->num_items = count;
1491 info->items = msi_alloc( sizeof(*info->items) * count );
1493 r = MSI_IterateRecords( view, NULL, msi_combobox_add_item, info );
1494 msiobj_release( &view->hdr );
1495 return r;
1498 static UINT msi_dialog_set_control_condition( MSIRECORD *rec, LPVOID param )
1500 msi_dialog *dialog = param;
1501 msi_control *control;
1502 LPCWSTR name, action, condition;
1503 UINT r;
1505 name = MSI_RecordGetString( rec, 2 );
1506 action = MSI_RecordGetString( rec, 3 );
1507 condition = MSI_RecordGetString( rec, 4 );
1508 r = MSI_EvaluateConditionW( dialog->package, condition );
1509 control = msi_dialog_find_control( dialog, name );
1510 if (r == MSICONDITION_TRUE && control)
1512 TRACE("%s control %s\n", debugstr_w(action), debugstr_w(name));
1514 /* FIXME: case sensitive? */
1515 if (!wcscmp( action, L"Hide" ))
1516 ShowWindow(control->hwnd, SW_HIDE);
1517 else if (!wcscmp( action, L"Show" ))
1518 ShowWindow(control->hwnd, SW_SHOW);
1519 else if (!wcscmp( action, L"Disable" ))
1520 EnableWindow(control->hwnd, FALSE);
1521 else if (!wcscmp( action, L"Enable" ))
1522 EnableWindow(control->hwnd, TRUE);
1523 else if (!wcscmp( action, L"Default" ))
1524 SetFocus(control->hwnd);
1525 else
1526 FIXME("Unhandled action %s\n", debugstr_w(action));
1528 return ERROR_SUCCESS;
1531 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog )
1533 UINT r;
1534 MSIQUERY *view;
1535 MSIPACKAGE *package = dialog->package;
1537 TRACE("%p %s\n", dialog, debugstr_w(dialog->name));
1539 /* query the Control table for all the elements of the control */
1540 r = MSI_OpenQuery( package->db, &view, L"SELECT * FROM `ControlCondition` WHERE `Dialog_` = '%s'", dialog->name );
1541 if (r != ERROR_SUCCESS)
1542 return ERROR_SUCCESS;
1544 r = MSI_IterateRecords( view, 0, msi_dialog_set_control_condition, dialog );
1545 msiobj_release( &view->hdr );
1546 return r;
1549 static UINT msi_dialog_combobox_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
1551 struct msi_combobox_info *info;
1552 int index;
1553 LPWSTR value;
1555 if (HIWORD(param) != CBN_SELCHANGE && HIWORD(param) != CBN_EDITCHANGE)
1556 return ERROR_SUCCESS;
1558 info = GetPropW( control->hwnd, L"MSIDATA" );
1559 index = SendMessageW( control->hwnd, CB_GETCURSEL, 0, 0 );
1560 if (index == CB_ERR)
1561 value = msi_get_window_text( control->hwnd );
1562 else
1563 value = (LPWSTR) SendMessageW( control->hwnd, CB_GETITEMDATA, index, 0 );
1565 msi_dialog_set_property( info->dialog->package, control->property, value );
1566 msi_dialog_evaluate_control_conditions( info->dialog );
1568 if (index == CB_ERR)
1569 msi_free( value );
1571 return ERROR_SUCCESS;
1574 static void msi_dialog_combobox_update( msi_dialog *dialog, msi_control *control )
1576 struct msi_combobox_info *info;
1577 LPWSTR value, tmp;
1578 DWORD j;
1580 info = GetPropW( control->hwnd, L"MSIDATA" );
1582 value = msi_dup_property( dialog->package->db, control->property );
1583 if (!value)
1585 SendMessageW( control->hwnd, CB_SETCURSEL, -1, 0 );
1586 return;
1589 for (j = 0; j < info->num_items; j++)
1591 tmp = (LPWSTR) SendMessageW( control->hwnd, CB_GETITEMDATA, j, 0 );
1592 if (!wcscmp( value, tmp ))
1593 break;
1596 if (j < info->num_items)
1598 SendMessageW( control->hwnd, CB_SETCURSEL, j, 0 );
1600 else
1602 SendMessageW( control->hwnd, CB_SETCURSEL, -1, 0 );
1603 SetWindowTextW( control->hwnd, value );
1606 msi_free(value);
1609 static UINT msi_dialog_combo_control( msi_dialog *dialog, MSIRECORD *rec )
1611 struct msi_combobox_info *info;
1612 msi_control *control;
1613 DWORD attributes, style;
1614 LPCWSTR prop;
1616 info = msi_alloc( sizeof *info );
1617 if (!info)
1618 return ERROR_FUNCTION_FAILED;
1620 style = CBS_AUTOHSCROLL | WS_TABSTOP | WS_GROUP | WS_CHILD;
1621 attributes = MSI_RecordGetInteger( rec, 8 );
1622 if ( ~attributes & msidbControlAttributesSorted)
1623 style |= CBS_SORT;
1624 if ( attributes & msidbControlAttributesComboList)
1625 style |= CBS_DROPDOWNLIST;
1626 else
1627 style |= CBS_DROPDOWN;
1629 control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
1630 if (!control)
1632 msi_free( info );
1633 return ERROR_FUNCTION_FAILED;
1636 control->handler = msi_dialog_combobox_handler;
1637 control->update = msi_dialog_combobox_update;
1639 prop = MSI_RecordGetString( rec, 9 );
1640 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
1642 /* subclass */
1643 info->dialog = dialog;
1644 info->hwnd = control->hwnd;
1645 info->items = NULL;
1646 info->addpos_items = 0;
1647 info->oldproc = (WNDPROC)SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1648 (LONG_PTR)MSIComboBox_WndProc );
1649 SetPropW( control->hwnd, L"MSIDATA", info );
1651 if (control->property)
1652 msi_combobox_add_items( info, control->property );
1654 msi_dialog_combobox_update( dialog, control );
1656 return ERROR_SUCCESS;
1659 static UINT msi_dialog_edit_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
1661 LPWSTR buf;
1663 if (HIWORD(param) != EN_CHANGE)
1664 return ERROR_SUCCESS;
1666 TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name), debugstr_w(control->property));
1668 buf = msi_get_window_text( control->hwnd );
1669 msi_dialog_set_property( dialog->package, control->property, buf );
1670 msi_free( buf );
1672 return ERROR_SUCCESS;
1675 /* length of 2^32 + 1 */
1676 #define MAX_NUM_DIGITS 11
1678 static UINT msi_dialog_edit_control( msi_dialog *dialog, MSIRECORD *rec )
1680 msi_control *control;
1681 LPCWSTR prop, text;
1682 LPWSTR val, begin, end;
1683 WCHAR num[MAX_NUM_DIGITS];
1684 DWORD limit;
1686 control = msi_dialog_add_control( dialog, rec, L"Edit",
1687 WS_BORDER | WS_TABSTOP | ES_AUTOHSCROLL );
1688 control->handler = msi_dialog_edit_handler;
1690 text = MSI_RecordGetString( rec, 10 );
1691 if ( text )
1693 begin = wcschr( text, '{' );
1694 end = wcschr( text, '}' );
1696 if ( begin && end && end > begin &&
1697 begin[0] >= '0' && begin[0] <= '9' &&
1698 end - begin < MAX_NUM_DIGITS)
1700 lstrcpynW( num, begin + 1, end - begin );
1701 limit = wcstol( num, NULL, 10 );
1703 SendMessageW( control->hwnd, EM_SETLIMITTEXT, limit, 0 );
1707 prop = MSI_RecordGetString( rec, 9 );
1708 if( prop )
1709 control->property = strdupW( prop );
1711 val = msi_dup_property( dialog->package->db, control->property );
1712 SetWindowTextW( control->hwnd, val );
1713 msi_free( val );
1714 return ERROR_SUCCESS;
1717 /******************** Masked Edit ********************************************/
1719 #define MASK_MAX_GROUPS 20
1721 struct msi_mask_group
1723 UINT len;
1724 UINT ofs;
1725 WCHAR type;
1726 HWND hwnd;
1729 struct msi_maskedit_info
1731 msi_dialog *dialog;
1732 WNDPROC oldproc;
1733 HWND hwnd;
1734 LPWSTR prop;
1735 UINT num_chars;
1736 UINT num_groups;
1737 struct msi_mask_group group[MASK_MAX_GROUPS];
1740 static BOOL msi_mask_editable( WCHAR type )
1742 switch (type)
1744 case '%':
1745 case '#':
1746 case '&':
1747 case '`':
1748 case '?':
1749 case '^':
1750 return TRUE;
1752 return FALSE;
1755 static void msi_mask_control_change( struct msi_maskedit_info *info )
1757 LPWSTR val;
1758 UINT i, n, r;
1760 val = msi_alloc( (info->num_chars+1)*sizeof(WCHAR) );
1761 for( i=0, n=0; i<info->num_groups; i++ )
1763 if (info->group[i].len == ~0u)
1765 UINT len = SendMessageW( info->group[i].hwnd, WM_GETTEXTLENGTH, 0, 0 );
1766 val = msi_realloc( val, (len + 1) * sizeof(WCHAR) );
1767 GetWindowTextW( info->group[i].hwnd, val, len + 1 );
1769 else
1771 if (info->group[i].len + n > info->num_chars)
1773 ERR("can't fit control %d text into template\n",i);
1774 break;
1776 if (!msi_mask_editable(info->group[i].type))
1778 for(r=0; r<info->group[i].len; r++)
1779 val[n+r] = info->group[i].type;
1780 val[n+r] = 0;
1782 else
1784 r = GetWindowTextW( info->group[i].hwnd, &val[n], info->group[i].len+1 );
1785 if( r != info->group[i].len )
1786 break;
1788 n += r;
1792 TRACE("%d/%d controls were good\n", i, info->num_groups);
1794 if( i == info->num_groups )
1796 TRACE("Set property %s to %s\n", debugstr_w(info->prop), debugstr_w(val));
1797 msi_dialog_set_property( info->dialog->package, info->prop, val );
1798 msi_dialog_evaluate_control_conditions( info->dialog );
1800 msi_free( val );
1803 /* now move to the next control if necessary */
1804 static VOID msi_mask_next_control( struct msi_maskedit_info *info, HWND hWnd )
1806 HWND hWndNext;
1807 UINT len, i;
1809 for( i=0; i<info->num_groups; i++ )
1810 if( info->group[i].hwnd == hWnd )
1811 break;
1813 /* don't move from the last control */
1814 if( i >= (info->num_groups-1) )
1815 return;
1817 len = SendMessageW( hWnd, WM_GETTEXTLENGTH, 0, 0 );
1818 if( len < info->group[i].len )
1819 return;
1821 hWndNext = GetNextDlgTabItem( GetParent( hWnd ), hWnd, FALSE );
1822 SetFocus( hWndNext );
1825 static LRESULT WINAPI
1826 MSIMaskedEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1828 struct msi_maskedit_info *info;
1829 HRESULT r;
1831 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1833 info = GetPropW(hWnd, L"MSIDATA");
1835 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1837 switch( msg )
1839 case WM_COMMAND:
1840 if (HIWORD(wParam) == EN_CHANGE)
1842 msi_mask_control_change( info );
1843 msi_mask_next_control( info, (HWND) lParam );
1845 break;
1846 case WM_NCDESTROY:
1847 msi_free( info->prop );
1848 msi_free( info );
1849 RemovePropW( hWnd, L"MSIDATA" );
1850 break;
1853 return r;
1856 /* fish the various bits of the property out and put them in the control */
1857 static void
1858 msi_maskedit_set_text( struct msi_maskedit_info *info, LPCWSTR text )
1860 LPCWSTR p;
1861 UINT i;
1863 p = text;
1864 for( i = 0; i < info->num_groups; i++ )
1866 if( info->group[i].len < lstrlenW( p ) )
1868 LPWSTR chunk = strdupW( p );
1869 chunk[ info->group[i].len ] = 0;
1870 SetWindowTextW( info->group[i].hwnd, chunk );
1871 msi_free( chunk );
1873 else
1875 SetWindowTextW( info->group[i].hwnd, p );
1876 break;
1878 p += info->group[i].len;
1882 static struct msi_maskedit_info * msi_dialog_parse_groups( LPCWSTR mask )
1884 struct msi_maskedit_info *info;
1885 int i = 0, n = 0, total = 0;
1886 LPCWSTR p;
1888 TRACE("masked control, template %s\n", debugstr_w(mask));
1890 if( !mask )
1891 return NULL;
1893 info = msi_alloc_zero( sizeof *info );
1894 if( !info )
1895 return info;
1897 p = wcschr(mask, '<');
1898 if( p )
1899 p++;
1900 else
1901 p = mask;
1903 for( i=0; i<MASK_MAX_GROUPS; i++ )
1905 /* stop at the end of the string */
1906 if( p[0] == 0 || p[0] == '>' )
1908 if (!total)
1910 /* create a group for the empty mask */
1911 info->group[0].type = '&';
1912 info->group[0].len = ~0u;
1913 i = 1;
1915 break;
1918 /* count the number of the same identifier */
1919 for( n=0; p[n] == p[0]; n++ )
1921 info->group[i].ofs = total;
1922 info->group[i].type = p[0];
1923 if( p[n] == '=' )
1925 n++;
1926 total++; /* an extra not part of the group */
1928 info->group[i].len = n;
1929 total += n;
1930 p += n;
1933 TRACE("%d characters in %d groups\n", total, i );
1934 if( i == MASK_MAX_GROUPS )
1935 ERR("too many groups in PIDTemplate %s\n", debugstr_w(mask));
1937 info->num_chars = total;
1938 info->num_groups = i;
1940 return info;
1943 static void
1944 msi_maskedit_create_children( struct msi_maskedit_info *info, LPCWSTR font )
1946 DWORD width, height, style, wx, ww;
1947 RECT rect;
1948 HWND hwnd;
1949 UINT i;
1951 style = WS_CHILD | WS_BORDER | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL;
1953 GetClientRect( info->hwnd, &rect );
1955 width = rect.right - rect.left;
1956 height = rect.bottom - rect.top;
1958 for( i = 0; i < info->num_groups; i++ )
1960 if (!msi_mask_editable( info->group[i].type ))
1961 continue;
1962 if (info->num_chars)
1964 wx = (info->group[i].ofs * width) / info->num_chars;
1965 ww = (info->group[i].len * width) / info->num_chars;
1967 else
1969 wx = 0;
1970 ww = width;
1972 hwnd = CreateWindowW( L"Edit", NULL, style, wx, 0, ww, height,
1973 info->hwnd, NULL, NULL, NULL );
1974 if( !hwnd )
1976 ERR("failed to create mask edit sub window\n");
1977 break;
1980 SendMessageW( hwnd, EM_LIMITTEXT, info->group[i].len, 0 );
1982 msi_dialog_set_font( info->dialog, hwnd,
1983 font?font:info->dialog->default_font );
1984 info->group[i].hwnd = hwnd;
1989 * office 2003 uses "73931<````=````=````=````=`````>@@@@@"
1990 * delphi 7 uses "<????-??????-??????-????>" and "<???-???>"
1991 * filemaker pro 7 uses "<^^^^=^^^^=^^^^=^^^^=^^^^=^^^^=^^^^^>"
1993 static UINT msi_dialog_maskedit_control( msi_dialog *dialog, MSIRECORD *rec )
1995 LPWSTR font_mask, val = NULL, font;
1996 struct msi_maskedit_info *info = NULL;
1997 UINT ret = ERROR_SUCCESS;
1998 msi_control *control;
1999 LPCWSTR prop, mask;
2001 TRACE("\n");
2003 font_mask = msi_get_deformatted_field( dialog->package, rec, 10 );
2004 font = msi_dialog_get_style( font_mask, &mask );
2005 if( !mask )
2007 WARN("mask template is empty\n");
2008 goto end;
2011 info = msi_dialog_parse_groups( mask );
2012 if( !info )
2014 ERR("template %s is invalid\n", debugstr_w(mask));
2015 goto end;
2018 info->dialog = dialog;
2020 control = msi_dialog_add_control( dialog, rec, L"Static",
2021 SS_OWNERDRAW | WS_GROUP | WS_VISIBLE );
2022 if( !control )
2024 ERR("Failed to create maskedit container\n");
2025 ret = ERROR_FUNCTION_FAILED;
2026 goto end;
2028 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
2030 info->hwnd = control->hwnd;
2032 /* subclass the static control */
2033 info->oldproc = (WNDPROC) SetWindowLongPtrW( info->hwnd, GWLP_WNDPROC,
2034 (LONG_PTR)MSIMaskedEdit_WndProc );
2035 SetPropW( control->hwnd, L"MSIDATA", info );
2037 prop = MSI_RecordGetString( rec, 9 );
2038 if( prop )
2039 info->prop = strdupW( prop );
2041 msi_maskedit_create_children( info, font );
2043 if( prop )
2045 val = msi_dup_property( dialog->package->db, prop );
2046 if( val )
2048 msi_maskedit_set_text( info, val );
2049 msi_free( val );
2053 end:
2054 if( ret != ERROR_SUCCESS )
2055 msi_free( info );
2056 msi_free( font_mask );
2057 msi_free( font );
2058 return ret;
2061 /******************** Progress Bar *****************************************/
2063 static UINT msi_dialog_progress_bar( msi_dialog *dialog, MSIRECORD *rec )
2065 msi_control *control;
2066 DWORD attributes, style;
2068 style = WS_VISIBLE;
2069 attributes = MSI_RecordGetInteger( rec, 8 );
2070 if( !(attributes & msidbControlAttributesProgress95) )
2071 style |= PBS_SMOOTH;
2073 control = msi_dialog_add_control( dialog, rec, PROGRESS_CLASSW, style );
2074 if( !control )
2075 return ERROR_FUNCTION_FAILED;
2077 event_subscribe( dialog, L"SetProgress", control->name, L"Progress" );
2078 return ERROR_SUCCESS;
2081 /******************** Path Edit ********************************************/
2083 struct msi_pathedit_info
2085 msi_dialog *dialog;
2086 msi_control *control;
2087 WNDPROC oldproc;
2090 static WCHAR *get_path_property( msi_dialog *dialog, msi_control *control )
2092 WCHAR *prop, *path;
2093 BOOL indirect = control->attributes & msidbControlAttributesIndirect;
2094 if (!(prop = msi_dialog_dup_property( dialog, control->property, indirect ))) return NULL;
2095 path = msi_dialog_dup_property( dialog, prop, TRUE );
2096 msi_free( prop );
2097 return path;
2100 static void msi_dialog_update_pathedit( msi_dialog *dialog, msi_control *control )
2102 WCHAR *path;
2104 if (!control && !(control = msi_dialog_find_control_by_type( dialog, L"PathEdit" )))
2105 return;
2107 if (!(path = get_path_property( dialog, control ))) return;
2108 SetWindowTextW( control->hwnd, path );
2109 SendMessageW( control->hwnd, EM_SETSEL, 0, -1 );
2110 msi_free( path );
2113 /* FIXME: test when this should fail */
2114 static BOOL msi_dialog_verify_path( LPWSTR path )
2116 if ( !path[0] )
2117 return FALSE;
2119 if ( PathIsRelativeW( path ) )
2120 return FALSE;
2122 return TRUE;
2125 /* returns TRUE if the path is valid, FALSE otherwise */
2126 static BOOL msi_dialog_onkillfocus( msi_dialog *dialog, msi_control *control )
2128 LPWSTR buf, prop;
2129 BOOL indirect;
2130 BOOL valid;
2132 indirect = control->attributes & msidbControlAttributesIndirect;
2133 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2135 buf = msi_get_window_text( control->hwnd );
2137 if ( !msi_dialog_verify_path( buf ) )
2139 /* FIXME: display an error message box */
2140 ERR("Invalid path %s\n", debugstr_w( buf ));
2141 valid = FALSE;
2142 SetFocus( control->hwnd );
2144 else
2146 valid = TRUE;
2147 msi_dialog_set_property( dialog->package, prop, buf );
2150 msi_dialog_update_pathedit( dialog, control );
2152 TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
2153 debugstr_w(prop));
2155 msi_free( buf );
2156 msi_free( prop );
2158 return valid;
2161 static LRESULT WINAPI MSIPathEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2163 struct msi_pathedit_info *info = GetPropW(hWnd, L"MSIDATA");
2164 LRESULT r = 0;
2166 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
2168 if ( msg == WM_KILLFOCUS )
2170 /* if the path is invalid, don't handle this message */
2171 if ( !msi_dialog_onkillfocus( info->dialog, info->control ) )
2172 return 0;
2175 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
2177 if ( msg == WM_NCDESTROY )
2179 msi_free( info );
2180 RemovePropW( hWnd, L"MSIDATA" );
2183 return r;
2186 static UINT msi_dialog_pathedit_control( msi_dialog *dialog, MSIRECORD *rec )
2188 struct msi_pathedit_info *info;
2189 msi_control *control;
2190 LPCWSTR prop;
2192 info = msi_alloc( sizeof *info );
2193 if (!info)
2194 return ERROR_FUNCTION_FAILED;
2196 control = msi_dialog_add_control( dialog, rec, L"Edit",
2197 WS_BORDER | WS_TABSTOP );
2198 control->attributes = MSI_RecordGetInteger( rec, 8 );
2199 prop = MSI_RecordGetString( rec, 9 );
2200 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2201 control->update = msi_dialog_update_pathedit;
2203 info->dialog = dialog;
2204 info->control = control;
2205 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2206 (LONG_PTR)MSIPathEdit_WndProc );
2207 SetPropW( control->hwnd, L"MSIDATA", info );
2209 msi_dialog_update_pathedit( dialog, control );
2211 return ERROR_SUCCESS;
2214 static UINT msi_dialog_radiogroup_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
2216 if (HIWORD(param) != BN_CLICKED)
2217 return ERROR_SUCCESS;
2219 TRACE("clicked radio button %s, set %s\n", debugstr_w(control->name), debugstr_w(control->property));
2221 msi_dialog_set_property( dialog->package, control->property, control->name );
2223 return msi_dialog_button_handler( dialog, control, param );
2226 /* radio buttons are a bit different from normal controls */
2227 static UINT msi_dialog_create_radiobutton( MSIRECORD *rec, LPVOID param )
2229 radio_button_group_descr *group = param;
2230 msi_dialog *dialog = group->dialog;
2231 msi_control *control;
2232 LPCWSTR prop, text, name;
2233 DWORD style = WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_AUTORADIOBUTTON | BS_MULTILINE;
2235 name = MSI_RecordGetString( rec, 3 );
2236 text = MSI_RecordGetString( rec, 8 );
2238 control = dialog_create_window( dialog, rec, 0, L"BUTTON", name, text, style,
2239 group->parent->hwnd );
2240 if (!control)
2241 return ERROR_FUNCTION_FAILED;
2242 control->handler = msi_dialog_radiogroup_handler;
2244 if (group->propval && !wcscmp( control->name, group->propval ))
2245 SendMessageW(control->hwnd, BM_SETCHECK, BST_CHECKED, 0);
2247 prop = MSI_RecordGetString( rec, 1 );
2248 if( prop )
2249 control->property = strdupW( prop );
2251 return ERROR_SUCCESS;
2254 static BOOL CALLBACK msi_radioground_child_enum( HWND hWnd, LPARAM lParam )
2256 EnableWindow( hWnd, lParam );
2257 return TRUE;
2260 static LRESULT WINAPI MSIRadioGroup_WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
2262 WNDPROC oldproc = (WNDPROC)GetPropW( hWnd, L"MSIDATA" );
2263 LRESULT r;
2265 TRACE("hWnd %p msg %04x wParam 0x%08lx lParam 0x%08lx\n", hWnd, msg, wParam, lParam);
2267 if (msg == WM_COMMAND) /* Forward notifications to dialog */
2268 SendMessageW( GetParent( hWnd ), msg, wParam, lParam );
2270 r = CallWindowProcW( oldproc, hWnd, msg, wParam, lParam );
2272 /* make sure the radio buttons show as disabled if the parent is disabled */
2273 if (msg == WM_ENABLE)
2274 EnumChildWindows( hWnd, msi_radioground_child_enum, wParam );
2276 return r;
2279 static UINT msi_dialog_radiogroup_control( msi_dialog *dialog, MSIRECORD *rec )
2281 UINT r;
2282 LPCWSTR prop;
2283 msi_control *control;
2284 MSIQUERY *view;
2285 radio_button_group_descr group;
2286 MSIPACKAGE *package = dialog->package;
2287 WNDPROC oldproc;
2288 DWORD attr, style = WS_GROUP;
2290 prop = MSI_RecordGetString( rec, 9 );
2292 TRACE("%p %p %s\n", dialog, rec, debugstr_w( prop ));
2294 attr = MSI_RecordGetInteger( rec, 8 );
2295 if (attr & msidbControlAttributesVisible)
2296 style |= WS_VISIBLE;
2297 if (~attr & msidbControlAttributesEnabled)
2298 style |= WS_DISABLED;
2299 if (attr & msidbControlAttributesHasBorder)
2300 style |= BS_GROUPBOX;
2301 else
2302 style |= BS_OWNERDRAW;
2304 /* Create parent group box to hold radio buttons */
2305 control = msi_dialog_add_control( dialog, rec, L"BUTTON", style );
2306 if( !control )
2307 return ERROR_FUNCTION_FAILED;
2309 oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2310 (LONG_PTR)MSIRadioGroup_WndProc );
2311 SetPropW(control->hwnd, L"MSIDATA", oldproc);
2312 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
2314 if( prop )
2315 control->property = strdupW( prop );
2317 /* query the Radio Button table for all control in this group */
2318 r = MSI_OpenQuery( package->db, &view, L"SELECT * FROM `RadioButton` WHERE `Property` = '%s'", prop );
2319 if( r != ERROR_SUCCESS )
2321 ERR("query failed for dialog %s radio group %s\n",
2322 debugstr_w(dialog->name), debugstr_w(prop));
2323 return ERROR_INVALID_PARAMETER;
2326 group.dialog = dialog;
2327 group.parent = control;
2328 group.propval = msi_dup_property( dialog->package->db, control->property );
2330 r = MSI_IterateRecords( view, 0, msi_dialog_create_radiobutton, &group );
2331 msiobj_release( &view->hdr );
2332 msi_free( group.propval );
2333 return r;
2336 static void
2337 msi_seltree_sync_item_state( HWND hwnd, MSIFEATURE *feature, HTREEITEM hItem )
2339 TVITEMW tvi;
2340 DWORD index = feature->ActionRequest;
2342 TRACE("Feature %s -> %d %d %d\n", debugstr_w(feature->Title),
2343 feature->Installed, feature->Action, feature->ActionRequest);
2345 if (index == INSTALLSTATE_UNKNOWN)
2346 index = INSTALLSTATE_ABSENT;
2348 tvi.mask = TVIF_STATE;
2349 tvi.hItem = hItem;
2350 tvi.state = INDEXTOSTATEIMAGEMASK( index );
2351 tvi.stateMask = TVIS_STATEIMAGEMASK;
2353 SendMessageW( hwnd, TVM_SETITEMW, 0, (LPARAM) &tvi );
2356 static UINT
2357 msi_seltree_popup_menu( HWND hwnd, INT x, INT y )
2359 HMENU hMenu;
2360 INT r;
2362 /* create a menu to display */
2363 hMenu = CreatePopupMenu();
2365 /* FIXME: load strings from resources */
2366 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_LOCAL, "Install feature locally");
2367 AppendMenuA( hMenu, MF_ENABLED, USER_INSTALLSTATE_ALL, "Install entire feature");
2368 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ADVERTISED, "Install on demand");
2369 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ABSENT, "Don't install");
2370 r = TrackPopupMenu( hMenu, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RETURNCMD,
2371 x, y, 0, hwnd, NULL );
2372 DestroyMenu( hMenu );
2373 return r;
2376 static void
2377 msi_seltree_update_feature_installstate( HWND hwnd, HTREEITEM hItem,
2378 MSIPACKAGE *package, MSIFEATURE *feature, INSTALLSTATE state )
2380 feature->ActionRequest = state;
2381 msi_seltree_sync_item_state( hwnd, feature, hItem );
2382 ACTION_UpdateComponentStates( package, feature );
2385 static void
2386 msi_seltree_update_siblings_and_children_installstate( HWND hwnd, HTREEITEM curr,
2387 MSIPACKAGE *package, INSTALLSTATE state)
2389 /* update all siblings */
2392 MSIFEATURE *feature;
2393 HTREEITEM child;
2395 feature = msi_seltree_feature_from_item( hwnd, curr );
2396 msi_seltree_update_feature_installstate( hwnd, curr, package, feature, state );
2398 /* update this sibling's children */
2399 child = (HTREEITEM)SendMessageW( hwnd, TVM_GETNEXTITEM, (WPARAM)TVGN_CHILD, (LPARAM)curr );
2400 if (child)
2401 msi_seltree_update_siblings_and_children_installstate( hwnd, child,
2402 package, state );
2404 while ((curr = (HTREEITEM)SendMessageW( hwnd, TVM_GETNEXTITEM, (WPARAM)TVGN_NEXT, (LPARAM)curr )));
2407 static LRESULT
2408 msi_seltree_menu( HWND hwnd, HTREEITEM hItem )
2410 struct msi_selection_tree_info *info;
2411 MSIFEATURE *feature;
2412 MSIPACKAGE *package;
2413 union {
2414 RECT rc;
2415 POINT pt[2];
2416 HTREEITEM hItem;
2417 } u;
2418 UINT r;
2420 info = GetPropW(hwnd, L"MSIDATA");
2421 package = info->dialog->package;
2423 feature = msi_seltree_feature_from_item( hwnd, hItem );
2424 if (!feature)
2426 ERR("item %p feature was NULL\n", hItem);
2427 return 0;
2430 /* get the item's rectangle to put the menu just below it */
2431 u.hItem = hItem;
2432 SendMessageW( hwnd, TVM_GETITEMRECT, 0, (LPARAM) &u.rc );
2433 MapWindowPoints( hwnd, NULL, u.pt, 2 );
2435 r = msi_seltree_popup_menu( hwnd, u.rc.left, u.rc.top );
2437 switch (r)
2439 case USER_INSTALLSTATE_ALL:
2440 r = INSTALLSTATE_LOCAL;
2441 /* fall-through */
2442 case INSTALLSTATE_ADVERTISED:
2443 case INSTALLSTATE_ABSENT:
2445 HTREEITEM child;
2446 child = (HTREEITEM)SendMessageW( hwnd, TVM_GETNEXTITEM, (WPARAM)TVGN_CHILD, (LPARAM)hItem );
2447 if (child)
2448 msi_seltree_update_siblings_and_children_installstate( hwnd, child, package, r );
2450 /* fall-through */
2451 case INSTALLSTATE_LOCAL:
2452 msi_seltree_update_feature_installstate( hwnd, hItem, package, feature, r );
2453 break;
2456 return 0;
2459 static LRESULT WINAPI
2460 MSISelectionTree_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2462 struct msi_selection_tree_info *info;
2463 TVHITTESTINFO tvhti;
2464 HRESULT r;
2466 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
2468 info = GetPropW(hWnd, L"MSIDATA");
2470 switch( msg )
2472 case WM_LBUTTONDOWN:
2473 tvhti.pt.x = (short)LOWORD( lParam );
2474 tvhti.pt.y = (short)HIWORD( lParam );
2475 tvhti.flags = 0;
2476 tvhti.hItem = 0;
2477 CallWindowProcW(info->oldproc, hWnd, TVM_HITTEST, 0, (LPARAM) &tvhti );
2478 if (tvhti.flags & TVHT_ONITEMSTATEICON)
2479 return msi_seltree_menu( hWnd, tvhti.hItem );
2480 break;
2482 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
2484 switch( msg )
2486 case WM_NCDESTROY:
2487 msi_free( info );
2488 RemovePropW( hWnd, L"MSIDATA" );
2489 break;
2491 return r;
2494 static void
2495 msi_seltree_add_child_features( MSIPACKAGE *package, HWND hwnd,
2496 LPCWSTR parent, HTREEITEM hParent )
2498 struct msi_selection_tree_info *info = GetPropW( hwnd, L"MSIDATA" );
2499 MSIFEATURE *feature;
2500 TVINSERTSTRUCTW tvis;
2501 HTREEITEM hitem, hfirst = NULL;
2503 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
2505 if ( parent && feature->Feature_Parent && wcscmp( parent, feature->Feature_Parent ))
2506 continue;
2507 else if ( parent && !feature->Feature_Parent )
2508 continue;
2509 else if ( !parent && feature->Feature_Parent )
2510 continue;
2512 if ( !feature->Title )
2513 continue;
2515 if ( !feature->Display )
2516 continue;
2518 memset( &tvis, 0, sizeof tvis );
2519 tvis.hParent = hParent;
2520 tvis.hInsertAfter = TVI_LAST;
2521 tvis.u.item.mask = TVIF_TEXT | TVIF_PARAM;
2522 tvis.u.item.pszText = feature->Title;
2523 tvis.u.item.lParam = (LPARAM) feature;
2525 hitem = (HTREEITEM) SendMessageW( hwnd, TVM_INSERTITEMW, 0, (LPARAM) &tvis );
2526 if (!hitem)
2527 continue;
2529 if (!hfirst)
2530 hfirst = hitem;
2532 msi_seltree_sync_item_state( hwnd, feature, hitem );
2533 msi_seltree_add_child_features( package, hwnd,
2534 feature->Feature, hitem );
2536 /* the node is expanded if Display is odd */
2537 if ( feature->Display % 2 != 0 )
2538 SendMessageW( hwnd, TVM_EXPAND, TVE_EXPAND, (LPARAM) hitem );
2541 /* select the first item */
2542 SendMessageW( hwnd, TVM_SELECTITEM, TVGN_CARET | TVGN_DROPHILITE, (LPARAM) hfirst );
2543 info->selected = hfirst;
2546 static void msi_seltree_create_imagelist( HWND hwnd )
2548 const int bm_width = 32, bm_height = 16, bm_count = 3;
2549 const int bm_resource = 0x1001;
2550 HIMAGELIST himl;
2551 int i;
2552 HBITMAP hbmp;
2554 himl = ImageList_Create( bm_width, bm_height, FALSE, 4, 0 );
2555 if (!himl)
2557 ERR("failed to create image list\n");
2558 return;
2561 for (i=0; i<bm_count; i++)
2563 hbmp = LoadBitmapW( msi_hInstance, MAKEINTRESOURCEW(i+bm_resource) );
2564 if (!hbmp)
2566 ERR("failed to load bitmap %d\n", i);
2567 break;
2571 * Add a dummy bitmap at offset zero because the treeview
2572 * can't use it as a state mask (zero means no user state).
2574 if (!i)
2575 ImageList_Add( himl, hbmp, NULL );
2577 ImageList_Add( himl, hbmp, NULL );
2580 SendMessageW( hwnd, TVM_SETIMAGELIST, TVSIL_STATE, (LPARAM)himl );
2583 static UINT msi_dialog_seltree_handler( msi_dialog *dialog,
2584 msi_control *control, WPARAM param )
2586 struct msi_selection_tree_info *info = GetPropW( control->hwnd, L"MSIDATA" );
2587 LPNMTREEVIEWW tv = (LPNMTREEVIEWW)param;
2588 MSIRECORD *row, *rec;
2589 MSIFOLDER *folder;
2590 MSIFEATURE *feature;
2591 LPCWSTR dir, title = NULL;
2592 UINT r = ERROR_SUCCESS;
2594 if (tv->hdr.code != TVN_SELCHANGINGW)
2595 return ERROR_SUCCESS;
2597 info->selected = tv->itemNew.hItem;
2599 if (!(tv->itemNew.mask & TVIF_TEXT))
2601 feature = msi_seltree_feature_from_item( control->hwnd, tv->itemNew.hItem );
2602 if (feature)
2603 title = feature->Title;
2605 else
2606 title = tv->itemNew.pszText;
2608 row = MSI_QueryGetRecord( dialog->package->db, L"SELECT * FROM `Feature` WHERE `Title` = '%s'", title );
2609 if (!row)
2610 return ERROR_FUNCTION_FAILED;
2612 rec = MSI_CreateRecord( 1 );
2614 MSI_RecordSetStringW( rec, 1, MSI_RecordGetString( row, 4 ) );
2615 msi_event_fire( dialog->package, L"SelectionDescription", rec );
2617 dir = MSI_RecordGetString( row, 7 );
2618 if (dir)
2620 folder = msi_get_loaded_folder( dialog->package, dir );
2621 if (!folder)
2623 r = ERROR_FUNCTION_FAILED;
2624 goto done;
2626 MSI_RecordSetStringW( rec, 1, folder->ResolvedTarget );
2628 else
2629 MSI_RecordSetStringW( rec, 1, NULL );
2631 msi_event_fire( dialog->package, L"SelectionPath", rec );
2633 done:
2634 msiobj_release(&row->hdr);
2635 msiobj_release(&rec->hdr);
2637 return r;
2640 static UINT msi_dialog_selection_tree( msi_dialog *dialog, MSIRECORD *rec )
2642 msi_control *control;
2643 LPCWSTR prop, control_name;
2644 MSIPACKAGE *package = dialog->package;
2645 DWORD style;
2646 struct msi_selection_tree_info *info;
2648 info = msi_alloc( sizeof *info );
2649 if (!info)
2650 return ERROR_FUNCTION_FAILED;
2652 /* create the treeview control */
2653 style = TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT;
2654 style |= WS_GROUP | WS_VSCROLL | WS_TABSTOP;
2655 control = msi_dialog_add_control( dialog, rec, WC_TREEVIEWW, style );
2656 if (!control)
2658 msi_free(info);
2659 return ERROR_FUNCTION_FAILED;
2662 control->handler = msi_dialog_seltree_handler;
2663 control_name = MSI_RecordGetString( rec, 2 );
2664 control->attributes = MSI_RecordGetInteger( rec, 8 );
2665 prop = MSI_RecordGetString( rec, 9 );
2666 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2668 /* subclass */
2669 info->dialog = dialog;
2670 info->hwnd = control->hwnd;
2671 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2672 (LONG_PTR)MSISelectionTree_WndProc );
2673 SetPropW( control->hwnd, L"MSIDATA", info );
2675 event_subscribe( dialog, L"SelectionPath", control_name, L"Property" );
2677 /* initialize it */
2678 msi_seltree_create_imagelist( control->hwnd );
2679 msi_seltree_add_child_features( package, control->hwnd, NULL, NULL );
2681 return ERROR_SUCCESS;
2684 /******************** Group Box ***************************************/
2686 static UINT msi_dialog_group_box( msi_dialog *dialog, MSIRECORD *rec )
2688 msi_control *control;
2689 DWORD style;
2691 style = BS_GROUPBOX | WS_CHILD | WS_GROUP;
2692 control = msi_dialog_add_control( dialog, rec, WC_BUTTONW, style );
2693 if (!control)
2694 return ERROR_FUNCTION_FAILED;
2696 return ERROR_SUCCESS;
2699 /******************** List Box ***************************************/
2701 struct msi_listbox_info
2703 msi_dialog *dialog;
2704 HWND hwnd;
2705 WNDPROC oldproc;
2706 DWORD num_items;
2707 DWORD addpos_items;
2708 LPWSTR *items;
2711 static LRESULT WINAPI MSIListBox_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2713 struct msi_listbox_info *info;
2714 LRESULT r;
2715 DWORD j;
2717 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
2719 info = GetPropW( hWnd, L"MSIDATA" );
2720 if (!info)
2721 return 0;
2723 r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
2725 switch( msg )
2727 case WM_NCDESTROY:
2728 for (j = 0; j < info->num_items; j++)
2729 msi_free( info->items[j] );
2730 msi_free( info->items );
2731 msi_free( info );
2732 RemovePropW( hWnd, L"MSIDATA" );
2733 break;
2736 return r;
2739 static UINT msi_listbox_add_item( MSIRECORD *rec, LPVOID param )
2741 struct msi_listbox_info *info = param;
2742 LPCWSTR value, text;
2743 int pos;
2745 value = MSI_RecordGetString( rec, 3 );
2746 text = MSI_RecordGetString( rec, 4 );
2748 info->items[info->addpos_items] = strdupW( value );
2750 pos = SendMessageW( info->hwnd, LB_ADDSTRING, 0, (LPARAM)text );
2751 SendMessageW( info->hwnd, LB_SETITEMDATA, pos, (LPARAM)info->items[info->addpos_items] );
2752 info->addpos_items++;
2753 return ERROR_SUCCESS;
2756 static UINT msi_listbox_add_items( struct msi_listbox_info *info, LPCWSTR property )
2758 MSIQUERY *view;
2759 DWORD count;
2760 UINT r;
2762 r = MSI_OpenQuery( info->dialog->package->db, &view,
2763 L"SELECT * FROM `ListBox` WHERE `Property` = '%s' ORDER BY `Order`", property );
2764 if ( r != ERROR_SUCCESS )
2765 return r;
2767 /* just get the number of records */
2768 count = 0;
2769 r = MSI_IterateRecords( view, &count, NULL, NULL );
2770 if (r != ERROR_SUCCESS)
2772 msiobj_release( &view->hdr );
2773 return r;
2775 info->num_items = count;
2776 info->items = msi_alloc( sizeof(*info->items) * count );
2778 r = MSI_IterateRecords( view, NULL, msi_listbox_add_item, info );
2779 msiobj_release( &view->hdr );
2780 return r;
2783 static UINT msi_dialog_listbox_handler( msi_dialog *dialog,
2784 msi_control *control, WPARAM param )
2786 struct msi_listbox_info *info;
2787 int index;
2788 LPCWSTR value;
2790 if( HIWORD(param) != LBN_SELCHANGE )
2791 return ERROR_SUCCESS;
2793 info = GetPropW( control->hwnd, L"MSIDATA" );
2794 index = SendMessageW( control->hwnd, LB_GETCURSEL, 0, 0 );
2795 value = (LPCWSTR) SendMessageW( control->hwnd, LB_GETITEMDATA, index, 0 );
2797 msi_dialog_set_property( info->dialog->package, control->property, value );
2798 msi_dialog_evaluate_control_conditions( info->dialog );
2800 return ERROR_SUCCESS;
2803 static UINT msi_dialog_list_box( msi_dialog *dialog, MSIRECORD *rec )
2805 struct msi_listbox_info *info;
2806 msi_control *control;
2807 DWORD attributes, style;
2808 LPCWSTR prop;
2810 info = msi_alloc( sizeof *info );
2811 if (!info)
2812 return ERROR_FUNCTION_FAILED;
2814 style = WS_TABSTOP | WS_GROUP | WS_CHILD | LBS_NOTIFY | WS_VSCROLL | WS_BORDER;
2815 attributes = MSI_RecordGetInteger( rec, 8 );
2816 if (~attributes & msidbControlAttributesSorted)
2817 style |= LBS_SORT;
2819 control = msi_dialog_add_control( dialog, rec, WC_LISTBOXW, style );
2820 if (!control)
2822 msi_free(info);
2823 return ERROR_FUNCTION_FAILED;
2826 control->handler = msi_dialog_listbox_handler;
2828 prop = MSI_RecordGetString( rec, 9 );
2829 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2831 /* subclass */
2832 info->dialog = dialog;
2833 info->hwnd = control->hwnd;
2834 info->items = NULL;
2835 info->addpos_items = 0;
2836 info->oldproc = (WNDPROC)SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2837 (LONG_PTR)MSIListBox_WndProc );
2838 SetPropW( control->hwnd, L"MSIDATA", info );
2840 if ( control->property )
2841 msi_listbox_add_items( info, control->property );
2843 return ERROR_SUCCESS;
2846 /******************** Directory Combo ***************************************/
2848 static void msi_dialog_update_directory_combo( msi_dialog *dialog, msi_control *control )
2850 WCHAR *path;
2852 if (!control && !(control = msi_dialog_find_control_by_type( dialog, L"DirectoryCombo" )))
2853 return;
2855 if (!(path = get_path_property( dialog, control ))) return;
2856 PathStripPathW( path );
2857 PathRemoveBackslashW( path );
2859 SendMessageW( control->hwnd, CB_INSERTSTRING, 0, (LPARAM)path );
2860 SendMessageW( control->hwnd, CB_SETCURSEL, 0, 0 );
2862 msi_free( path );
2865 static UINT msi_dialog_directory_combo( msi_dialog *dialog, MSIRECORD *rec )
2867 msi_control *control;
2868 LPCWSTR prop;
2869 DWORD style;
2871 /* FIXME: use CBS_OWNERDRAWFIXED and add owner draw code */
2872 style = CBS_DROPDOWNLIST | CBS_HASSTRINGS | WS_CHILD |
2873 WS_GROUP | WS_TABSTOP | WS_VSCROLL;
2874 control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
2875 if (!control)
2876 return ERROR_FUNCTION_FAILED;
2878 control->attributes = MSI_RecordGetInteger( rec, 8 );
2879 prop = MSI_RecordGetString( rec, 9 );
2880 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2882 msi_dialog_update_directory_combo( dialog, control );
2884 return ERROR_SUCCESS;
2887 /******************** Directory List ***************************************/
2889 static void msi_dialog_update_directory_list( msi_dialog *dialog, msi_control *control )
2891 WCHAR dir_spec[MAX_PATH], *path;
2892 WIN32_FIND_DATAW wfd;
2893 LVITEMW item;
2894 HANDLE file;
2896 if (!control && !(control = msi_dialog_find_control_by_type( dialog, L"DirectoryList" )))
2897 return;
2899 /* clear the list-view */
2900 SendMessageW( control->hwnd, LVM_DELETEALLITEMS, 0, 0 );
2902 if (!(path = get_path_property( dialog, control ))) return;
2903 lstrcpyW( dir_spec, path );
2904 lstrcatW( dir_spec, L"*" );
2906 file = FindFirstFileW( dir_spec, &wfd );
2907 if (file == INVALID_HANDLE_VALUE)
2909 msi_free( path );
2910 return;
2915 if ( wfd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY )
2916 continue;
2918 if ( !wcscmp( wfd.cFileName, L"." ) || !wcscmp( wfd.cFileName, L".." ) )
2919 continue;
2921 item.mask = LVIF_TEXT;
2922 item.cchTextMax = MAX_PATH;
2923 item.iItem = 0;
2924 item.iSubItem = 0;
2925 item.pszText = wfd.cFileName;
2927 SendMessageW( control->hwnd, LVM_INSERTITEMW, 0, (LPARAM)&item );
2928 } while ( FindNextFileW( file, &wfd ) );
2930 msi_free( path );
2931 FindClose( file );
2934 static UINT msi_dialog_directorylist_up( msi_dialog *dialog )
2936 msi_control *control;
2937 LPWSTR prop, path, ptr;
2938 BOOL indirect;
2940 control = msi_dialog_find_control_by_type( dialog, L"DirectoryList" );
2941 indirect = control->attributes & msidbControlAttributesIndirect;
2942 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2943 path = msi_dialog_dup_property( dialog, prop, TRUE );
2945 /* strip off the last directory */
2946 ptr = PathFindFileNameW( path );
2947 if (ptr != path) *(ptr - 1) = '\0';
2948 PathAddBackslashW( path );
2950 msi_dialog_set_property( dialog->package, prop, path );
2952 msi_dialog_update_directory_list( dialog, NULL );
2953 msi_dialog_update_directory_combo( dialog, NULL );
2954 msi_dialog_update_pathedit( dialog, NULL );
2956 msi_free( path );
2957 msi_free( prop );
2959 return ERROR_SUCCESS;
2962 static WCHAR *get_unique_folder_name( const WCHAR *root, int *ret_len )
2964 WCHAR newfolder[MAX_PATH], *path, *ptr;
2965 int len, count = 2;
2967 len = LoadStringW( msi_hInstance, IDS_NEWFOLDER, newfolder, ARRAY_SIZE(newfolder) );
2968 len += lstrlenW(root) + 1;
2969 if (!(path = msi_alloc( (len + 4) * sizeof(WCHAR) ))) return NULL;
2970 lstrcpyW( path, root );
2971 lstrcatW( path, newfolder );
2973 for (;;)
2975 if (GetFileAttributesW( path ) == INVALID_FILE_ATTRIBUTES) break;
2976 if (count > 99)
2978 msi_free( path );
2979 return NULL;
2981 swprintf( path, len + 4, L"%s%s %u", root, newfolder, count++ );
2984 ptr = wcsrchr( path, '\\' ) + 1;
2985 *ret_len = lstrlenW(ptr);
2986 memmove( path, ptr, *ret_len * sizeof(WCHAR) );
2987 return path;
2990 static UINT msi_dialog_directorylist_new( msi_dialog *dialog )
2992 msi_control *control;
2993 WCHAR *path;
2994 LVITEMW item;
2995 int index;
2997 control = msi_dialog_find_control_by_type( dialog, L"DirectoryList" );
2999 if (!(path = get_path_property( dialog, control ))) return ERROR_OUTOFMEMORY;
3001 item.mask = LVIF_TEXT;
3002 item.iItem = 0;
3003 item.iSubItem = 0;
3004 item.pszText = get_unique_folder_name( path, &item.cchTextMax );
3006 index = SendMessageW( control->hwnd, LVM_INSERTITEMW, 0, (LPARAM)&item );
3007 SendMessageW( control->hwnd, LVM_ENSUREVISIBLE, index, 0 );
3008 SendMessageW( control->hwnd, LVM_EDITLABELW, index, -1 );
3010 msi_free( path );
3011 msi_free( item.pszText );
3012 return ERROR_SUCCESS;
3015 static UINT msi_dialog_dirlist_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
3017 NMHDR *nmhdr = (NMHDR *)param;
3018 WCHAR text[MAX_PATH], *new_path, *path, *prop;
3019 BOOL indirect;
3021 switch (nmhdr->code)
3023 case LVN_ENDLABELEDITW:
3025 NMLVDISPINFOW *info = (NMLVDISPINFOW *)param;
3026 if (!info->item.pszText) return ERROR_SUCCESS;
3027 lstrcpynW( text, info->item.pszText, ARRAY_SIZE(text) );
3028 text[ARRAY_SIZE(text) - 1] = 0;
3029 break;
3031 case LVN_ITEMACTIVATE:
3033 LVITEMW item;
3034 int index = SendMessageW( control->hwnd, LVM_GETNEXTITEM, -1, LVNI_SELECTED );
3035 if (index < 0)
3037 ERR("no list-view item selected\n");
3038 return ERROR_FUNCTION_FAILED;
3041 item.iSubItem = 0;
3042 item.pszText = text;
3043 item.cchTextMax = MAX_PATH;
3044 SendMessageW( control->hwnd, LVM_GETITEMTEXTW, index, (LPARAM)&item );
3045 text[ARRAY_SIZE(text) - 1] = 0;
3046 break;
3048 default:
3049 return ERROR_SUCCESS;
3052 indirect = control->attributes & msidbControlAttributesIndirect;
3053 prop = msi_dialog_dup_property( dialog, control->property, indirect );
3054 path = msi_dialog_dup_property( dialog, prop, TRUE );
3056 if (!(new_path = msi_alloc( (lstrlenW(path) + lstrlenW(text) + 2) * sizeof(WCHAR) )))
3058 msi_free( prop );
3059 msi_free( path );
3060 return ERROR_OUTOFMEMORY;
3062 lstrcpyW( new_path, path );
3063 lstrcatW( new_path, text );
3064 if (nmhdr->code == LVN_ENDLABELEDITW) CreateDirectoryW( new_path, NULL );
3065 lstrcatW( new_path, L"\\" );
3067 msi_dialog_set_property( dialog->package, prop, new_path );
3069 msi_dialog_update_directory_list( dialog, NULL );
3070 msi_dialog_update_directory_combo( dialog, NULL );
3071 msi_dialog_update_pathedit( dialog, NULL );
3073 msi_free( prop );
3074 msi_free( path );
3075 msi_free( new_path );
3077 return ERROR_SUCCESS;
3080 static UINT msi_dialog_directory_list( msi_dialog *dialog, MSIRECORD *rec )
3082 msi_control *control;
3083 LPCWSTR prop;
3084 DWORD style;
3086 style = LVS_LIST | WS_VSCROLL | LVS_SHAREIMAGELISTS | LVS_EDITLABELS |
3087 LVS_AUTOARRANGE | LVS_SINGLESEL | WS_BORDER |
3088 LVS_SORTASCENDING | WS_CHILD | WS_GROUP | WS_TABSTOP;
3089 control = msi_dialog_add_control( dialog, rec, WC_LISTVIEWW, style );
3090 if (!control)
3091 return ERROR_FUNCTION_FAILED;
3093 control->attributes = MSI_RecordGetInteger( rec, 8 );
3094 control->handler = msi_dialog_dirlist_handler;
3095 prop = MSI_RecordGetString( rec, 9 );
3096 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
3098 /* double click to activate an item in the list */
3099 SendMessageW( control->hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE,
3100 0, LVS_EX_TWOCLICKACTIVATE );
3102 msi_dialog_update_directory_list( dialog, control );
3104 return ERROR_SUCCESS;
3107 /******************** VolumeCost List ***************************************/
3109 static BOOL str_is_number( LPCWSTR str )
3111 int i;
3113 for (i = 0; i < lstrlenW( str ); i++)
3114 if (!iswdigit(str[i]))
3115 return FALSE;
3117 return TRUE;
3120 static const WCHAR column_keys[][80] =
3122 L"VolumeCostVolume",
3123 L"VolumeCostSize",
3124 L"VolumeCostAvailable",
3125 L"VolumeCostRequired",
3126 L"VolumeCostDifference",
3129 static void msi_dialog_vcl_add_columns( msi_dialog *dialog, msi_control *control, MSIRECORD *rec )
3131 LPCWSTR text = MSI_RecordGetString( rec, 10 );
3132 LPCWSTR begin = text, end;
3133 WCHAR *num;
3134 LVCOLUMNW lvc;
3135 DWORD count = 0;
3137 if (!text) return;
3139 while ((begin = wcschr( begin, '{' )) && count < 5)
3141 if (!(end = wcschr( begin, '}' )))
3142 return;
3144 num = msi_alloc( (end-begin+1)*sizeof(WCHAR) );
3145 if (!num)
3146 return;
3148 lstrcpynW( num, begin + 1, end - begin );
3149 begin += end - begin + 1;
3151 /* empty braces or '0' hides the column */
3152 if ( !num[0] || !wcscmp( num, L"0" ) )
3154 count++;
3155 msi_free( num );
3156 continue;
3159 /* the width must be a positive number
3160 * if a width is invalid, all remaining columns are hidden
3162 if ( !wcsncmp( num, L"-", 1 ) || !str_is_number( num ) ) {
3163 msi_free( num );
3164 return;
3167 ZeroMemory( &lvc, sizeof(lvc) );
3168 lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
3169 lvc.cx = wcstol( num, NULL, 10 );
3170 lvc.pszText = msi_dialog_get_uitext( dialog, column_keys[count] );
3172 SendMessageW( control->hwnd, LVM_INSERTCOLUMNW, count++, (LPARAM)&lvc );
3173 msi_free( lvc.pszText );
3174 msi_free( num );
3178 static LONGLONG msi_vcl_get_cost( msi_dialog *dialog )
3180 MSIFEATURE *feature;
3181 INT each_cost;
3182 LONGLONG total_cost = 0;
3184 LIST_FOR_EACH_ENTRY( feature, &dialog->package->features, MSIFEATURE, entry )
3186 if (ERROR_SUCCESS == (MSI_GetFeatureCost(dialog->package, feature,
3187 MSICOSTTREE_SELFONLY, INSTALLSTATE_LOCAL, &each_cost)))
3189 /* each_cost is in 512-byte units */
3190 total_cost += each_cost * 512;
3192 if (ERROR_SUCCESS == (MSI_GetFeatureCost(dialog->package, feature,
3193 MSICOSTTREE_SELFONLY, INSTALLSTATE_ABSENT, &each_cost)))
3195 /* each_cost is in 512-byte units */
3196 total_cost -= each_cost * 512;
3199 return total_cost;
3202 static void msi_dialog_vcl_add_drives( msi_dialog *dialog, msi_control *control )
3204 ULARGE_INTEGER total, free;
3205 LONGLONG difference, cost;
3206 WCHAR size_text[MAX_PATH];
3207 WCHAR cost_text[MAX_PATH];
3208 LPWSTR drives, ptr;
3209 LVITEMW lvitem;
3210 DWORD size, flags;
3211 int i = 0;
3213 cost = msi_vcl_get_cost(dialog);
3214 StrFormatByteSizeW(cost, cost_text, MAX_PATH);
3216 size = GetLogicalDriveStringsW( 0, NULL );
3217 if ( !size ) return;
3219 drives = msi_alloc( (size + 1) * sizeof(WCHAR) );
3220 if ( !drives ) return;
3222 GetLogicalDriveStringsW( size, drives );
3224 ptr = drives;
3225 while (*ptr)
3227 if (GetVolumeInformationW(ptr, NULL, 0, NULL, 0, &flags, NULL, 0) &&
3228 flags & FILE_READ_ONLY_VOLUME)
3230 ptr += lstrlenW(ptr) + 1;
3231 continue;
3234 lvitem.mask = LVIF_TEXT;
3235 lvitem.iItem = i;
3236 lvitem.iSubItem = 0;
3237 lvitem.pszText = ptr;
3238 lvitem.cchTextMax = lstrlenW(ptr) + 1;
3239 SendMessageW( control->hwnd, LVM_INSERTITEMW, 0, (LPARAM)&lvitem );
3241 GetDiskFreeSpaceExW(ptr, &free, &total, NULL);
3242 difference = free.QuadPart - cost;
3244 StrFormatByteSizeW(total.QuadPart, size_text, MAX_PATH);
3245 lvitem.iSubItem = 1;
3246 lvitem.pszText = size_text;
3247 lvitem.cchTextMax = lstrlenW(size_text) + 1;
3248 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
3250 StrFormatByteSizeW(free.QuadPart, size_text, MAX_PATH);
3251 lvitem.iSubItem = 2;
3252 lvitem.pszText = size_text;
3253 lvitem.cchTextMax = lstrlenW(size_text) + 1;
3254 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
3256 lvitem.iSubItem = 3;
3257 lvitem.pszText = cost_text;
3258 lvitem.cchTextMax = lstrlenW(cost_text) + 1;
3259 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
3261 StrFormatByteSizeW(difference, size_text, MAX_PATH);
3262 lvitem.iSubItem = 4;
3263 lvitem.pszText = size_text;
3264 lvitem.cchTextMax = lstrlenW(size_text) + 1;
3265 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
3267 ptr += lstrlenW(ptr) + 1;
3268 i++;
3271 msi_free( drives );
3274 static UINT msi_dialog_volumecost_list( msi_dialog *dialog, MSIRECORD *rec )
3276 msi_control *control;
3277 DWORD style;
3279 style = LVS_REPORT | WS_VSCROLL | WS_HSCROLL | LVS_SHAREIMAGELISTS |
3280 LVS_AUTOARRANGE | LVS_SINGLESEL | WS_BORDER |
3281 WS_CHILD | WS_TABSTOP | WS_GROUP;
3282 control = msi_dialog_add_control( dialog, rec, WC_LISTVIEWW, style );
3283 if (!control)
3284 return ERROR_FUNCTION_FAILED;
3286 msi_dialog_vcl_add_columns( dialog, control, rec );
3287 msi_dialog_vcl_add_drives( dialog, control );
3289 return ERROR_SUCCESS;
3292 /******************** VolumeSelect Combo ***************************************/
3294 static UINT msi_dialog_volsel_handler( msi_dialog *dialog,
3295 msi_control *control, WPARAM param )
3297 WCHAR text[MAX_PATH];
3298 LPWSTR prop;
3299 BOOL indirect;
3300 int index;
3302 if (HIWORD(param) != CBN_SELCHANGE)
3303 return ERROR_SUCCESS;
3305 index = SendMessageW( control->hwnd, CB_GETCURSEL, 0, 0 );
3306 if ( index == CB_ERR )
3308 ERR("No ComboBox item selected!\n");
3309 return ERROR_FUNCTION_FAILED;
3312 SendMessageW( control->hwnd, CB_GETLBTEXT, index, (LPARAM)text );
3314 indirect = control->attributes & msidbControlAttributesIndirect;
3315 prop = msi_dialog_dup_property( dialog, control->property, indirect );
3317 msi_dialog_set_property( dialog->package, prop, text );
3319 msi_free( prop );
3320 return ERROR_SUCCESS;
3323 static void msi_dialog_vsc_add_drives( msi_dialog *dialog, msi_control *control )
3325 LPWSTR drives, ptr;
3326 DWORD size;
3328 size = GetLogicalDriveStringsW( 0, NULL );
3329 if ( !size ) return;
3331 drives = msi_alloc( (size + 1) * sizeof(WCHAR) );
3332 if ( !drives ) return;
3334 GetLogicalDriveStringsW( size, drives );
3336 ptr = drives;
3337 while (*ptr)
3339 SendMessageW( control->hwnd, CB_ADDSTRING, 0, (LPARAM)ptr );
3340 ptr += lstrlenW(ptr) + 1;
3343 msi_free( drives );
3346 static UINT msi_dialog_volumeselect_combo( msi_dialog *dialog, MSIRECORD *rec )
3348 msi_control *control;
3349 LPCWSTR prop;
3350 DWORD style;
3352 /* FIXME: CBS_OWNERDRAWFIXED */
3353 style = WS_CHILD | WS_VISIBLE | WS_GROUP | WS_TABSTOP |
3354 CBS_DROPDOWNLIST | CBS_SORT | CBS_HASSTRINGS |
3355 WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
3356 control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
3357 if (!control)
3358 return ERROR_FUNCTION_FAILED;
3360 control->attributes = MSI_RecordGetInteger( rec, 8 );
3361 control->handler = msi_dialog_volsel_handler;
3362 prop = MSI_RecordGetString( rec, 9 );
3363 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
3365 msi_dialog_vsc_add_drives( dialog, control );
3367 return ERROR_SUCCESS;
3370 static UINT msi_dialog_hyperlink_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
3372 int len, len_href = ARRAY_SIZE( L"href" ) - 1;
3373 const WCHAR *p, *q;
3374 WCHAR quote = 0;
3375 LITEM item;
3377 item.mask = LIF_ITEMINDEX | LIF_URL;
3378 item.iLink = 0;
3379 item.szUrl[0] = 0;
3381 SendMessageW( control->hwnd, LM_GETITEM, 0, (LPARAM)&item );
3383 p = item.szUrl;
3384 while (*p && *p != '<') p++;
3385 if (!*p++) return ERROR_SUCCESS;
3386 if (towupper( *p++ ) != 'A' || !iswspace( *p++ )) return ERROR_SUCCESS;
3387 while (*p && iswspace( *p )) p++;
3389 len = lstrlenW( p );
3390 if (len > len_href && !wcsnicmp( p, L"href", len_href ))
3392 p += len_href;
3393 while (*p && iswspace( *p )) p++;
3394 if (!*p || *p++ != '=') return ERROR_SUCCESS;
3395 while (*p && iswspace( *p )) p++;
3397 if (*p == '\"' || *p == '\'') quote = *p++;
3398 q = p;
3399 if (quote)
3401 while (*q && *q != quote) q++;
3402 if (*q != quote) return ERROR_SUCCESS;
3404 else
3406 while (*q && *q != '>' && !iswspace( *q )) q++;
3407 if (!*q) return ERROR_SUCCESS;
3409 item.szUrl[q - item.szUrl] = 0;
3410 ShellExecuteW( NULL, L"open", p, NULL, NULL, SW_SHOWNORMAL );
3412 return ERROR_SUCCESS;
3415 static UINT msi_dialog_hyperlink( msi_dialog *dialog, MSIRECORD *rec )
3417 msi_control *control;
3418 DWORD style = WS_CHILD | WS_TABSTOP | WS_GROUP;
3419 const WCHAR *text = MSI_RecordGetString( rec, 10 );
3420 int len = lstrlenW( text );
3421 LITEM item;
3423 control = msi_dialog_add_control( dialog, rec, WC_LINK, style );
3424 if (!control)
3425 return ERROR_FUNCTION_FAILED;
3427 control->attributes = MSI_RecordGetInteger( rec, 8 );
3428 control->handler = msi_dialog_hyperlink_handler;
3430 item.mask = LIF_ITEMINDEX | LIF_STATE | LIF_URL;
3431 item.iLink = 0;
3432 item.state = LIS_ENABLED;
3433 item.stateMask = LIS_ENABLED;
3434 if (len < L_MAX_URL_LENGTH) lstrcpyW( item.szUrl, text );
3435 else item.szUrl[0] = 0;
3437 SendMessageW( control->hwnd, LM_SETITEM, 0, (LPARAM)&item );
3439 return ERROR_SUCCESS;
3442 /******************** ListView *****************************************/
3444 struct listview_param
3446 msi_dialog *dialog;
3447 msi_control *control;
3450 static UINT msi_dialog_listview_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
3452 NMHDR *nmhdr = (NMHDR *)param;
3454 FIXME("code %#x (%d)\n", nmhdr->code, nmhdr->code);
3456 return ERROR_SUCCESS;
3459 static UINT msi_listview_add_item( MSIRECORD *rec, LPVOID param )
3461 struct listview_param *lv_param = (struct listview_param *)param;
3462 LPCWSTR text, binary;
3463 LVITEMW item;
3464 HICON hIcon;
3466 text = MSI_RecordGetString( rec, 4 );
3467 binary = MSI_RecordGetString( rec, 5 );
3468 hIcon = msi_load_icon( lv_param->dialog->package->db, binary, 0 );
3470 TRACE("Adding: text %s, binary %s, icon %p\n", debugstr_w(text), debugstr_w(binary), hIcon);
3472 memset( &item, 0, sizeof(item) );
3473 item.mask = LVIF_TEXT | LVIF_IMAGE;
3474 deformat_string( lv_param->dialog->package, text, &item.pszText );
3475 item.iImage = ImageList_AddIcon( lv_param->control->hImageList, hIcon );
3476 item.iItem = item.iImage;
3477 SendMessageW( lv_param->control->hwnd, LVM_INSERTITEMW, 0, (LPARAM)&item );
3479 DestroyIcon( hIcon );
3481 return ERROR_SUCCESS;
3484 static UINT msi_listview_add_items( msi_dialog *dialog, msi_control *control )
3486 MSIQUERY *view;
3487 struct listview_param lv_param = { dialog, control };
3489 if (MSI_OpenQuery( dialog->package->db, &view, L"SELECT * FROM `ListView` WHERE `Property` = '%s' ORDER BY `Order`",
3490 control->property ) == ERROR_SUCCESS)
3492 MSI_IterateRecords( view, NULL, msi_listview_add_item, &lv_param );
3493 msiobj_release( &view->hdr );
3496 return ERROR_SUCCESS;
3499 static UINT msi_dialog_listview( msi_dialog *dialog, MSIRECORD *rec )
3501 msi_control *control;
3502 LPCWSTR prop;
3503 DWORD style, attributes;
3504 LVCOLUMNW col;
3505 RECT rc;
3507 style = LVS_REPORT | LVS_NOCOLUMNHEADER | LVS_SHAREIMAGELISTS | LVS_SINGLESEL |
3508 LVS_SHOWSELALWAYS | WS_VSCROLL | WS_HSCROLL | WS_BORDER | WS_TABSTOP | WS_CHILD;
3509 attributes = MSI_RecordGetInteger( rec, 8 );
3510 if ( ~attributes & msidbControlAttributesSorted )
3511 style |= LVS_SORTASCENDING;
3512 control = msi_dialog_add_control( dialog, rec, WC_LISTVIEWW, style );
3513 if (!control)
3514 return ERROR_FUNCTION_FAILED;
3516 prop = MSI_RecordGetString( rec, 9 );
3517 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
3519 control->hImageList = ImageList_Create( 16, 16, ILC_COLOR32, 0, 1);
3520 SendMessageW( control->hwnd, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM)control->hImageList );
3522 col.mask = LVCF_FMT | LVCF_WIDTH;
3523 col.fmt = LVCFMT_LEFT;
3524 col.cx = 16;
3525 SendMessageW( control->hwnd, LVM_INSERTCOLUMNW, 0, (LPARAM)&col );
3527 GetClientRect( control->hwnd, &rc );
3528 col.cx = rc.right - 16;
3529 SendMessageW( control->hwnd, LVM_INSERTCOLUMNW, 0, (LPARAM)&col );
3531 if (control->property)
3532 msi_listview_add_items( dialog, control );
3534 control->handler = msi_dialog_listview_handler;
3536 return ERROR_SUCCESS;
3539 static const struct control_handler msi_dialog_handler[] =
3541 { L"Text", msi_dialog_text_control },
3542 { L"PushButton", msi_dialog_button_control },
3543 { L"Line", msi_dialog_line_control },
3544 { L"Bitmap", msi_dialog_bitmap_control },
3545 { L"CheckBox", msi_dialog_checkbox_control },
3546 { L"ScrollableText", msi_dialog_scrolltext_control },
3547 { L"ComboBox", msi_dialog_combo_control },
3548 { L"Edit", msi_dialog_edit_control },
3549 { L"MaskedEdit", msi_dialog_maskedit_control },
3550 { L"PathEdit", msi_dialog_pathedit_control },
3551 { L"ProgressBar", msi_dialog_progress_bar },
3552 { L"RadioButtonGroup", msi_dialog_radiogroup_control },
3553 { L"Icon", msi_dialog_icon_control },
3554 { L"SelectionTree", msi_dialog_selection_tree },
3555 { L"GroupBox", msi_dialog_group_box },
3556 { L"ListBox", msi_dialog_list_box },
3557 { L"DirectoryCombo", msi_dialog_directory_combo },
3558 { L"DirectoryList", msi_dialog_directory_list },
3559 { L"VolumeCostList", msi_dialog_volumecost_list },
3560 { L"VolumeSelectCombo", msi_dialog_volumeselect_combo },
3561 { L"HyperLink", msi_dialog_hyperlink },
3562 { L"ListView", msi_dialog_listview }
3565 static UINT msi_dialog_create_controls( MSIRECORD *rec, LPVOID param )
3567 msi_dialog *dialog = param;
3568 LPCWSTR control_type;
3569 UINT i;
3571 /* find and call the function that can create this type of control */
3572 control_type = MSI_RecordGetString( rec, 3 );
3573 for( i = 0; i < ARRAY_SIZE( msi_dialog_handler ); i++ )
3574 if (!wcsicmp( msi_dialog_handler[i].control_type, control_type ))
3575 break;
3576 if( i != ARRAY_SIZE( msi_dialog_handler ))
3577 msi_dialog_handler[i].func( dialog, rec );
3578 else
3579 ERR("no handler for element type %s\n", debugstr_w(control_type));
3581 return ERROR_SUCCESS;
3584 static UINT msi_dialog_fill_controls( msi_dialog *dialog )
3586 UINT r;
3587 MSIQUERY *view;
3588 MSIPACKAGE *package = dialog->package;
3590 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
3592 /* query the Control table for all the elements of the control */
3593 r = MSI_OpenQuery( package->db, &view, L"SELECT * FROM `Control` WHERE `Dialog_` = '%s'", dialog->name );
3594 if( r != ERROR_SUCCESS )
3596 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
3597 return ERROR_INVALID_PARAMETER;
3600 r = MSI_IterateRecords( view, 0, msi_dialog_create_controls, dialog );
3601 msiobj_release( &view->hdr );
3602 return r;
3605 static UINT msi_dialog_reset( msi_dialog *dialog )
3607 /* FIXME: should restore the original values of any properties we changed */
3608 return msi_dialog_evaluate_control_conditions( dialog );
3611 /* figure out the height of 10 point MS Sans Serif */
3612 static INT msi_dialog_get_sans_serif_height( HWND hwnd )
3614 LOGFONTW lf;
3615 TEXTMETRICW tm;
3616 BOOL r;
3617 LONG height = 0;
3618 HFONT hFont, hOldFont;
3619 HDC hdc;
3621 hdc = GetDC( hwnd );
3622 if (hdc)
3624 memset( &lf, 0, sizeof lf );
3625 lf.lfHeight = MulDiv(12, GetDeviceCaps(hdc, LOGPIXELSY), 72);
3626 lstrcpyW( lf.lfFaceName, L"MS Sans Serif" );
3627 hFont = CreateFontIndirectW(&lf);
3628 if (hFont)
3630 hOldFont = SelectObject( hdc, hFont );
3631 r = GetTextMetricsW( hdc, &tm );
3632 if (r)
3633 height = tm.tmHeight;
3634 SelectObject( hdc, hOldFont );
3635 DeleteObject( hFont );
3637 ReleaseDC( hwnd, hdc );
3639 return height;
3642 /* fetch the associated record from the Dialog table */
3643 static MSIRECORD *msi_get_dialog_record( msi_dialog *dialog )
3645 MSIPACKAGE *package = dialog->package;
3646 MSIRECORD *rec = NULL;
3648 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
3650 rec = MSI_QueryGetRecord( package->db, L"SELECT * FROM `Dialog` WHERE `Dialog` = '%s'", dialog->name );
3651 if( !rec )
3652 WARN("query failed for dialog %s\n", debugstr_w(dialog->name));
3654 return rec;
3657 static void msi_dialog_adjust_dialog_pos( msi_dialog *dialog, MSIRECORD *rec, LPRECT pos )
3659 UINT xres, yres;
3660 POINT center;
3661 SIZE sz;
3662 LONG style;
3664 center.x = MSI_RecordGetInteger( rec, 2 );
3665 center.y = MSI_RecordGetInteger( rec, 3 );
3667 sz.cx = MSI_RecordGetInteger( rec, 4 );
3668 sz.cy = MSI_RecordGetInteger( rec, 5 );
3670 sz.cx = msi_dialog_scale_unit( dialog, sz.cx );
3671 sz.cy = msi_dialog_scale_unit( dialog, sz.cy );
3673 xres = msi_get_property_int( dialog->package->db, L"ScreenX", 0 );
3674 yres = msi_get_property_int( dialog->package->db, L"ScreenY", 0 );
3676 center.x = MulDiv( center.x, xres, 100 );
3677 center.y = MulDiv( center.y, yres, 100 );
3679 /* turn the client pos into the window rectangle */
3680 if (dialog->package->center_x && dialog->package->center_y)
3682 pos->left = dialog->package->center_x - sz.cx / 2.0;
3683 pos->right = pos->left + sz.cx;
3684 pos->top = dialog->package->center_y - sz.cy / 2.0;
3685 pos->bottom = pos->top + sz.cy;
3687 else
3689 pos->left = center.x - sz.cx/2;
3690 pos->right = pos->left + sz.cx;
3691 pos->top = center.y - sz.cy/2;
3692 pos->bottom = pos->top + sz.cy;
3694 /* save the center */
3695 dialog->package->center_x = center.x;
3696 dialog->package->center_y = center.y;
3699 dialog->size.cx = sz.cx;
3700 dialog->size.cy = sz.cy;
3702 TRACE("%s\n", wine_dbgstr_rect(pos));
3704 style = GetWindowLongPtrW( dialog->hwnd, GWL_STYLE );
3705 AdjustWindowRect( pos, style, FALSE );
3708 static void msi_dialog_set_tab_order( msi_dialog *dialog, LPCWSTR first )
3710 struct list tab_chain;
3711 msi_control *control;
3712 HWND prev = HWND_TOP;
3714 list_init( &tab_chain );
3715 if (!(control = msi_dialog_find_control( dialog, first ))) return;
3717 dialog->hWndFocus = control->hwnd;
3718 while (control)
3720 list_remove( &control->entry );
3721 list_add_tail( &tab_chain, &control->entry );
3722 if (!control->tabnext) break;
3723 control = msi_dialog_find_control( dialog, control->tabnext );
3726 LIST_FOR_EACH_ENTRY( control, &tab_chain, msi_control, entry )
3728 SetWindowPos( control->hwnd, prev, 0, 0, 0, 0,
3729 SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOREDRAW |
3730 SWP_NOREPOSITION | SWP_NOSENDCHANGING | SWP_NOSIZE );
3731 prev = control->hwnd;
3734 /* put them back on the main list */
3735 list_move_head( &dialog->controls, &tab_chain );
3738 static LRESULT msi_dialog_oncreate( HWND hwnd, LPCREATESTRUCTW cs )
3740 msi_dialog *dialog = cs->lpCreateParams;
3741 MSIRECORD *rec = NULL;
3742 LPWSTR title = NULL;
3743 RECT pos;
3745 TRACE("%p %p\n", dialog, dialog->package);
3747 dialog->hwnd = hwnd;
3748 SetWindowLongPtrW( hwnd, GWLP_USERDATA, (LONG_PTR) dialog );
3750 rec = msi_get_dialog_record( dialog );
3751 if( !rec )
3753 TRACE("No record found for dialog %s\n", debugstr_w(dialog->name));
3754 return -1;
3757 dialog->scale = msi_dialog_get_sans_serif_height(dialog->hwnd);
3759 msi_dialog_adjust_dialog_pos( dialog, rec, &pos );
3761 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
3763 dialog->default_font = msi_dup_property( dialog->package->db, L"DefaultUIFont" );
3764 if (!dialog->default_font)
3766 dialog->default_font = strdupW( L"MS Shell Dlg" );
3767 if (!dialog->default_font)
3769 msiobj_release( &rec->hdr );
3770 return -1;
3774 title = msi_get_deformatted_field( dialog->package, rec, 7 );
3775 SetWindowTextW( hwnd, title );
3776 msi_free( title );
3778 SetWindowPos( hwnd, 0, pos.left, pos.top,
3779 pos.right - pos.left, pos.bottom - pos.top,
3780 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW );
3782 msi_dialog_build_font_list( dialog );
3783 msi_dialog_fill_controls( dialog );
3784 msi_dialog_evaluate_control_conditions( dialog );
3785 msi_dialog_set_tab_order( dialog, MSI_RecordGetString( rec, 8 ) );
3786 msiobj_release( &rec->hdr );
3788 return 0;
3791 static LRESULT msi_dialog_oncommand( msi_dialog *dialog, WPARAM param, HWND hwnd )
3793 msi_control *control = NULL;
3795 TRACE("%p %p %08lx\n", dialog, hwnd, param);
3797 switch (param)
3799 case 1: /* enter */
3800 control = msi_dialog_find_control( dialog, dialog->control_default );
3801 break;
3802 case 2: /* escape */
3803 control = msi_dialog_find_control( dialog, dialog->control_cancel );
3804 break;
3805 default:
3806 control = msi_dialog_find_control_by_hwnd( dialog, hwnd );
3809 if( control )
3811 if( control->handler )
3813 control->handler( dialog, control, param );
3814 msi_dialog_evaluate_control_conditions( dialog );
3818 return 0;
3821 static LRESULT msi_dialog_onnotify( msi_dialog *dialog, LPARAM param )
3823 LPNMHDR nmhdr = (LPNMHDR) param;
3824 msi_control *control = msi_dialog_find_control_by_hwnd( dialog, nmhdr->hwndFrom );
3826 TRACE("%p %p\n", dialog, nmhdr->hwndFrom);
3828 if ( control && control->handler )
3829 control->handler( dialog, control, param );
3831 return 0;
3834 static void dialog_setfocus( msi_dialog *dialog )
3836 HWND hwnd = dialog->hWndFocus;
3838 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, TRUE);
3839 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, FALSE);
3840 SetFocus( hwnd );
3841 dialog->hWndFocus = hwnd;
3844 static LRESULT WINAPI MSIDialog_WndProc( HWND hwnd, UINT msg,
3845 WPARAM wParam, LPARAM lParam )
3847 msi_dialog *dialog = (LPVOID) GetWindowLongPtrW( hwnd, GWLP_USERDATA );
3849 TRACE("0x%04x\n", msg);
3851 switch (msg)
3853 case WM_MOVE:
3854 dialog->package->center_x = LOWORD(lParam) + dialog->size.cx / 2.0;
3855 dialog->package->center_y = HIWORD(lParam) + dialog->size.cy / 2.0;
3856 break;
3858 case WM_CREATE:
3859 return msi_dialog_oncreate( hwnd, (LPCREATESTRUCTW)lParam );
3861 case WM_COMMAND:
3862 return msi_dialog_oncommand( dialog, wParam, (HWND)lParam );
3864 case WM_CLOSE:
3865 /* Simulate escape press */
3866 return msi_dialog_oncommand(dialog, 2, NULL);
3868 case WM_ACTIVATE:
3869 if( LOWORD(wParam) == WA_INACTIVE )
3870 dialog->hWndFocus = GetFocus();
3871 else
3872 dialog_setfocus( dialog );
3873 return 0;
3875 case WM_SETFOCUS:
3876 dialog_setfocus( dialog );
3877 return 0;
3879 /* bounce back to our subclassed static control */
3880 case WM_CTLCOLORSTATIC:
3881 return SendMessageW( (HWND) lParam, WM_CTLCOLORSTATIC, wParam, lParam );
3883 case WM_DESTROY:
3884 dialog->hwnd = NULL;
3885 return 0;
3886 case WM_NOTIFY:
3887 return msi_dialog_onnotify( dialog, lParam );
3889 return DefWindowProcW(hwnd, msg, wParam, lParam);
3892 static void process_pending_messages( HWND hdlg )
3894 MSG msg;
3896 while (PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
3898 if (hdlg && IsDialogMessageW( hdlg, &msg )) continue;
3899 TranslateMessage( &msg );
3900 DispatchMessageW( &msg );
3904 static UINT dialog_run_message_loop( msi_dialog *dialog )
3906 DWORD style;
3907 HWND hwnd;
3909 if( uiThreadId != GetCurrentThreadId() )
3910 return SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_CREATE, 0, (LPARAM) dialog );
3912 /* create the dialog window, don't show it yet */
3913 style = WS_OVERLAPPED | WS_SYSMENU;
3914 if( dialog->attributes & msidbDialogAttributesVisible )
3915 style |= WS_VISIBLE;
3917 if (dialog->parent == NULL && (dialog->attributes & msidbDialogAttributesMinimize))
3918 style |= WS_MINIMIZEBOX;
3920 hwnd = CreateWindowW( L"MsiDialogCloseClass", dialog->name, style,
3921 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
3922 NULL, NULL, NULL, dialog );
3923 if( !hwnd )
3925 ERR("Failed to create dialog %s\n", debugstr_w( dialog->name ));
3926 return ERROR_FUNCTION_FAILED;
3929 ShowWindow( hwnd, SW_SHOW );
3930 /* UpdateWindow( hwnd ); - and causes the transparent static controls not to paint */
3932 if( dialog->attributes & msidbDialogAttributesModal )
3934 while( !dialog->finished )
3936 MsgWaitForMultipleObjects( 0, NULL, 0, INFINITE, QS_ALLINPUT );
3937 process_pending_messages( dialog->hwnd );
3940 else
3941 return ERROR_IO_PENDING;
3943 return ERROR_SUCCESS;
3946 static LRESULT WINAPI MSIHiddenWindowProc( HWND hwnd, UINT msg,
3947 WPARAM wParam, LPARAM lParam )
3949 msi_dialog *dialog = (msi_dialog*) lParam;
3951 TRACE("%d %p\n", msg, dialog);
3953 switch (msg)
3955 case WM_MSI_DIALOG_CREATE:
3956 return dialog_run_message_loop( dialog );
3957 case WM_MSI_DIALOG_DESTROY:
3958 msi_dialog_destroy( dialog );
3959 return 0;
3961 return DefWindowProcW( hwnd, msg, wParam, lParam );
3964 static BOOL dialog_register_class( void )
3966 WNDCLASSW cls;
3968 ZeroMemory( &cls, sizeof cls );
3969 cls.lpfnWndProc = MSIDialog_WndProc;
3970 cls.hInstance = NULL;
3971 cls.hIcon = LoadIconW(0, (LPWSTR)IDI_APPLICATION);
3972 cls.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
3973 cls.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
3974 cls.lpszMenuName = NULL;
3975 cls.lpszClassName = L"MsiDialogCloseClass";
3977 if( !RegisterClassW( &cls ) )
3978 return FALSE;
3980 cls.lpfnWndProc = MSIHiddenWindowProc;
3981 cls.lpszClassName = L"MsiHiddenWindow";
3983 if( !RegisterClassW( &cls ) )
3984 return FALSE;
3986 uiThreadId = GetCurrentThreadId();
3988 hMsiHiddenWindow = CreateWindowW( L"MsiHiddenWindow", NULL, WS_OVERLAPPED,
3989 0, 0, 100, 100, NULL, NULL, NULL, NULL );
3990 if( !hMsiHiddenWindow )
3991 return FALSE;
3993 return TRUE;
3996 static msi_dialog *dialog_create( MSIPACKAGE *package, const WCHAR *name, msi_dialog *parent,
3997 control_event_handler event_handler )
3999 MSIRECORD *rec = NULL;
4000 msi_dialog *dialog;
4002 TRACE("%s\n", debugstr_w(name));
4004 if (!hMsiHiddenWindow) dialog_register_class();
4006 /* allocate the structure for the dialog to use */
4007 dialog = msi_alloc_zero( FIELD_OFFSET( msi_dialog, name[lstrlenW( name ) + 1] ));
4008 if( !dialog )
4009 return NULL;
4010 lstrcpyW( dialog->name, name );
4011 dialog->parent = parent;
4012 dialog->package = package;
4013 dialog->event_handler = event_handler;
4014 dialog->finished = 0;
4015 list_init( &dialog->controls );
4016 list_init( &dialog->fonts );
4018 /* verify that the dialog exists */
4019 rec = msi_get_dialog_record( dialog );
4020 if( !rec )
4022 msi_free( dialog );
4023 return NULL;
4025 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
4026 dialog->control_default = strdupW( MSI_RecordGetString( rec, 9 ) );
4027 dialog->control_cancel = strdupW( MSI_RecordGetString( rec, 10 ) );
4028 msiobj_release( &rec->hdr );
4030 rec = MSI_CreateRecord(2);
4031 if (!rec)
4033 msi_dialog_destroy(dialog);
4034 return NULL;
4036 MSI_RecordSetStringW(rec, 1, name);
4037 MSI_RecordSetStringW(rec, 2, L"Dialog created");
4038 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONSTART, rec);
4039 msiobj_release(&rec->hdr);
4041 return dialog;
4044 static void msi_dialog_end_dialog( msi_dialog *dialog )
4046 TRACE("%p\n", dialog);
4047 dialog->finished = 1;
4048 PostMessageW(dialog->hwnd, WM_NULL, 0, 0);
4051 void msi_dialog_check_messages( HANDLE handle )
4053 DWORD r;
4055 /* in threads other than the UI thread, block */
4056 if( uiThreadId != GetCurrentThreadId() )
4058 if (!handle) return;
4059 while (MsgWaitForMultipleObjectsEx( 1, &handle, INFINITE, QS_ALLINPUT, 0 ) == WAIT_OBJECT_0 + 1)
4061 MSG msg;
4062 while (PeekMessageW( &msg, NULL, 0, 0, PM_REMOVE ))
4064 TranslateMessage( &msg );
4065 DispatchMessageW( &msg );
4068 return;
4071 /* there are two choices for the UI thread */
4072 while (1)
4074 process_pending_messages( NULL );
4076 if( !handle )
4077 break;
4080 * block here until somebody creates a new dialog or
4081 * the handle we're waiting on becomes ready
4083 r = MsgWaitForMultipleObjects( 1, &handle, 0, INFINITE, QS_ALLINPUT );
4084 if( r == WAIT_OBJECT_0 )
4085 break;
4089 static void dialog_do_preview( msi_dialog *dialog )
4091 TRACE("\n");
4092 dialog->attributes |= msidbDialogAttributesVisible;
4093 dialog->attributes &= ~msidbDialogAttributesModal;
4094 dialog_run_message_loop( dialog );
4097 static void free_subscriber( struct subscriber *sub )
4099 msi_free( sub->event );
4100 msi_free( sub->control );
4101 msi_free( sub->attribute );
4102 msi_free( sub );
4105 static void event_cleanup_subscriptions( MSIPACKAGE *package, const WCHAR *dialog )
4107 struct list *item, *next;
4109 LIST_FOR_EACH_SAFE( item, next, &package->subscriptions )
4111 struct subscriber *sub = LIST_ENTRY( item, struct subscriber, entry );
4113 if (wcscmp( sub->dialog->name, dialog )) continue;
4114 list_remove( &sub->entry );
4115 free_subscriber( sub );
4119 void msi_dialog_destroy( msi_dialog *dialog )
4121 msi_font *font, *next;
4123 if( uiThreadId != GetCurrentThreadId() )
4125 SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_DESTROY, 0, (LPARAM) dialog );
4126 return;
4129 if( dialog->hwnd )
4131 ShowWindow( dialog->hwnd, SW_HIDE );
4132 DestroyWindow( dialog->hwnd );
4135 /* unsubscribe events */
4136 event_cleanup_subscriptions( dialog->package, dialog->name );
4138 /* destroy the list of controls */
4139 while( !list_empty( &dialog->controls ) )
4141 msi_control *t;
4143 t = LIST_ENTRY( list_head( &dialog->controls ),
4144 msi_control, entry );
4145 msi_destroy_control( t );
4148 /* destroy the list of fonts */
4149 LIST_FOR_EACH_ENTRY_SAFE( font, next, &dialog->fonts, msi_font, entry )
4151 list_remove( &font->entry );
4152 DeleteObject( font->hfont );
4153 msi_free( font );
4155 msi_free( dialog->default_font );
4157 msi_free( dialog->control_default );
4158 msi_free( dialog->control_cancel );
4159 dialog->package = NULL;
4160 msi_free( dialog );
4163 void msi_dialog_unregister_class( void )
4165 DestroyWindow( hMsiHiddenWindow );
4166 hMsiHiddenWindow = NULL;
4167 UnregisterClassW( L"MsiDialogCloseClass", NULL );
4168 UnregisterClassW( L"MsiHiddenWindow", NULL );
4169 uiThreadId = 0;
4172 void msi_event_cleanup_all_subscriptions( MSIPACKAGE *package )
4174 struct list *item, *next;
4176 LIST_FOR_EACH_SAFE( item, next, &package->subscriptions )
4178 struct subscriber *sub = LIST_ENTRY( item, struct subscriber, entry );
4179 list_remove( &sub->entry );
4180 free_subscriber( sub );
4184 static void MSI_ClosePreview( MSIOBJECTHDR *arg )
4186 MSIPREVIEW *preview = (MSIPREVIEW *)arg;
4187 msiobj_release( &preview->package->hdr );
4190 static MSIPREVIEW *MSI_EnableUIPreview( MSIDATABASE *db )
4192 MSIPREVIEW *preview = NULL;
4193 MSIPACKAGE *package;
4195 package = MSI_CreatePackage( db );
4196 if (package)
4198 preview = alloc_msiobject( MSIHANDLETYPE_PREVIEW, sizeof(MSIPREVIEW), MSI_ClosePreview );
4199 if (preview)
4201 preview->package = package;
4202 msiobj_addref( &package->hdr );
4204 msiobj_release( &package->hdr );
4206 return preview;
4209 UINT WINAPI MsiEnableUIPreview( MSIHANDLE hdb, MSIHANDLE *phPreview )
4211 MSIDATABASE *db;
4212 MSIPREVIEW *preview;
4213 UINT r = ERROR_FUNCTION_FAILED;
4215 TRACE("%d %p\n", hdb, phPreview);
4217 if (!(db = msihandle2msiinfo(hdb, MSIHANDLETYPE_DATABASE)))
4218 return ERROR_INVALID_HANDLE;
4220 preview = MSI_EnableUIPreview( db );
4221 if (preview)
4223 *phPreview = alloc_msihandle( &preview->hdr );
4224 msiobj_release( &preview->hdr );
4225 r = ERROR_SUCCESS;
4226 if (!*phPreview)
4227 r = ERROR_NOT_ENOUGH_MEMORY;
4229 msiobj_release( &db->hdr );
4230 return r;
4233 static UINT preview_event_handler( msi_dialog *dialog, const WCHAR *event, const WCHAR *argument )
4235 MESSAGE("Preview dialog event '%s' (arg='%s')\n", debugstr_w(event), debugstr_w(argument));
4236 return ERROR_SUCCESS;
4239 static UINT MSI_PreviewDialogW( MSIPREVIEW *preview, LPCWSTR szDialogName )
4241 msi_dialog *dialog = NULL;
4242 UINT r = ERROR_SUCCESS;
4244 if (preview->dialog)
4245 msi_dialog_destroy( preview->dialog );
4247 /* an empty name means we should just destroy the current preview dialog */
4248 if (szDialogName)
4250 dialog = dialog_create( preview->package, szDialogName, NULL, preview_event_handler );
4251 if (dialog)
4252 dialog_do_preview( dialog );
4253 else
4254 r = ERROR_FUNCTION_FAILED;
4256 preview->dialog = dialog;
4257 return r;
4260 UINT WINAPI MsiPreviewDialogW( MSIHANDLE hPreview, LPCWSTR szDialogName )
4262 MSIPREVIEW *preview;
4263 UINT r;
4265 TRACE("%d %s\n", hPreview, debugstr_w(szDialogName));
4267 preview = msihandle2msiinfo( hPreview, MSIHANDLETYPE_PREVIEW );
4268 if (!preview)
4269 return ERROR_INVALID_HANDLE;
4271 r = MSI_PreviewDialogW( preview, szDialogName );
4272 msiobj_release( &preview->hdr );
4273 return r;
4276 UINT WINAPI MsiPreviewDialogA( MSIHANDLE hPreview, LPCSTR szDialogName )
4278 UINT r;
4279 LPWSTR strW = NULL;
4281 TRACE("%d %s\n", hPreview, debugstr_a(szDialogName));
4283 if (szDialogName)
4285 strW = strdupAtoW( szDialogName );
4286 if (!strW)
4287 return ERROR_OUTOFMEMORY;
4289 r = MsiPreviewDialogW( hPreview, strW );
4290 msi_free( strW );
4291 return r;
4294 UINT WINAPI MsiPreviewBillboardW( MSIHANDLE hPreview, LPCWSTR szControlName, LPCWSTR szBillboard )
4296 FIXME("%d %s %s\n", hPreview, debugstr_w(szControlName), debugstr_w(szBillboard));
4297 return ERROR_CALL_NOT_IMPLEMENTED;
4300 UINT WINAPI MsiPreviewBillboardA( MSIHANDLE hPreview, LPCSTR szControlName, LPCSTR szBillboard )
4302 FIXME("%d %s %s\n", hPreview, debugstr_a(szControlName), debugstr_a(szBillboard));
4303 return ERROR_CALL_NOT_IMPLEMENTED;
4306 struct control_event
4308 const WCHAR *event;
4309 event_handler handler;
4312 static UINT dialog_event_handler( msi_dialog *, const WCHAR *, const WCHAR * );
4314 /* create a dialog box and run it if it's modal */
4315 static INT event_do_dialog( MSIPACKAGE *package, const WCHAR *name, msi_dialog *parent, BOOL destroy_modeless )
4317 msi_dialog *dialog;
4318 UINT r;
4319 INT retval;
4321 /* create a new dialog */
4322 dialog = dialog_create( package, name, parent, dialog_event_handler );
4323 if (dialog)
4325 /* kill the current modeless dialog */
4326 if (destroy_modeless && package->dialog)
4328 msi_dialog_destroy( package->dialog );
4329 package->dialog = NULL;
4332 /* modeless dialogs return an error message */
4333 r = dialog_run_message_loop( dialog );
4334 if (r == ERROR_SUCCESS)
4336 retval = dialog->retval;
4337 msi_dialog_destroy( dialog );
4338 return retval;
4340 else
4342 package->dialog = dialog;
4343 return IDOK;
4346 else return 0;
4349 /* end a modal dialog box */
4350 static UINT event_end_dialog( msi_dialog *dialog, const WCHAR *argument )
4352 if (!wcscmp( argument, L"Exit" ))
4353 dialog->retval = IDCANCEL;
4354 else if (!wcscmp( argument, L"Retry" ))
4355 dialog->retval = IDRETRY;
4356 else if (!wcscmp( argument, L"Ignore" ))
4357 dialog->retval = IDOK;
4358 else if (!wcscmp( argument, L"Return" ))
4359 dialog->retval = 0;
4360 else
4362 ERR("Unknown argument string %s\n", debugstr_w(argument));
4363 dialog->retval = IDABORT;
4365 event_cleanup_subscriptions( dialog->package, dialog->name );
4366 msi_dialog_end_dialog( dialog );
4367 return ERROR_SUCCESS;
4370 static UINT pending_event_end_dialog( msi_dialog *dialog, const WCHAR *argument )
4372 dialog->pending_event = event_end_dialog;
4373 msi_free( dialog->pending_argument );
4374 dialog->pending_argument = strdupW( argument );
4375 return ERROR_SUCCESS;
4378 /* transition from one modal dialog to another modal dialog */
4379 static UINT event_new_dialog( msi_dialog *dialog, const WCHAR *argument )
4381 /* store the name of the next dialog, and signal this one to end */
4382 dialog->package->next_dialog = strdupW( argument );
4383 msi_event_cleanup_all_subscriptions( dialog->package );
4384 msi_dialog_end_dialog( dialog );
4385 return ERROR_SUCCESS;
4388 static UINT pending_event_new_dialog( msi_dialog *dialog, const WCHAR *argument )
4390 dialog->pending_event = event_new_dialog;
4391 msi_free( dialog->pending_argument );
4392 dialog->pending_argument = strdupW( argument );
4393 return ERROR_SUCCESS;
4396 /* create a new child dialog of an existing modal dialog */
4397 static UINT event_spawn_dialog( msi_dialog *dialog, const WCHAR *argument )
4399 INT r;
4400 /* don't destroy a modeless dialogs that might be our parent */
4401 r = event_do_dialog( dialog->package, argument, dialog, FALSE );
4402 if (r != 0)
4404 dialog->retval = r;
4405 msi_dialog_end_dialog( dialog );
4407 else
4408 msi_dialog_update_all_controls(dialog);
4410 return ERROR_SUCCESS;
4413 static UINT pending_event_spawn_dialog( msi_dialog *dialog, const WCHAR *argument )
4415 dialog->pending_event = event_spawn_dialog;
4416 msi_free( dialog->pending_argument );
4417 dialog->pending_argument = strdupW( argument );
4418 return ERROR_SUCCESS;
4421 /* creates a dialog that remains up for a period of time based on a condition */
4422 static UINT event_spawn_wait_dialog( msi_dialog *dialog, const WCHAR *argument )
4424 FIXME("doing nothing\n");
4425 return ERROR_SUCCESS;
4428 static UINT event_do_action( msi_dialog *dialog, const WCHAR *argument )
4430 ACTION_PerformAction(dialog->package, argument);
4431 return ERROR_SUCCESS;
4434 static UINT event_add_local( msi_dialog *dialog, const WCHAR *argument )
4436 MSIFEATURE *feature;
4438 LIST_FOR_EACH_ENTRY( feature, &dialog->package->features, MSIFEATURE, entry )
4440 if (!wcscmp( argument, feature->Feature ) || !wcscmp( argument, L"ALL" ))
4442 if (feature->ActionRequest != INSTALLSTATE_LOCAL)
4443 msi_set_property( dialog->package->db, L"Preselected", L"1", -1 );
4444 MSI_SetFeatureStateW( dialog->package, feature->Feature, INSTALLSTATE_LOCAL );
4447 return ERROR_SUCCESS;
4450 static UINT event_remove( msi_dialog *dialog, const WCHAR *argument )
4452 MSIFEATURE *feature;
4454 LIST_FOR_EACH_ENTRY( feature, &dialog->package->features, MSIFEATURE, entry )
4456 if (!wcscmp( argument, feature->Feature ) || !wcscmp( argument, L"ALL" ))
4458 if (feature->ActionRequest != INSTALLSTATE_ABSENT)
4459 msi_set_property( dialog->package->db, L"Preselected", L"1", -1 );
4460 MSI_SetFeatureStateW( dialog->package, feature->Feature, INSTALLSTATE_ABSENT );
4463 return ERROR_SUCCESS;
4466 static UINT event_add_source( msi_dialog *dialog, const WCHAR *argument )
4468 MSIFEATURE *feature;
4470 LIST_FOR_EACH_ENTRY( feature, &dialog->package->features, MSIFEATURE, entry )
4472 if (!wcscmp( argument, feature->Feature ) || !wcscmp( argument, L"ALL" ))
4474 if (feature->ActionRequest != INSTALLSTATE_SOURCE)
4475 msi_set_property( dialog->package->db, L"Preselected", L"1", -1 );
4476 MSI_SetFeatureStateW( dialog->package, feature->Feature, INSTALLSTATE_SOURCE );
4479 return ERROR_SUCCESS;
4482 void msi_event_fire( MSIPACKAGE *package, const WCHAR *event, MSIRECORD *rec )
4484 struct subscriber *sub;
4486 TRACE("firing event %s\n", debugstr_w(event));
4488 LIST_FOR_EACH_ENTRY( sub, &package->subscriptions, struct subscriber, entry )
4490 if (wcsicmp( sub->event, event )) continue;
4491 dialog_handle_event( sub->dialog, sub->control, sub->attribute, rec );
4495 static UINT event_set_target_path( msi_dialog *dialog, const WCHAR *argument )
4497 WCHAR *path = msi_dup_property( dialog->package->db, argument );
4498 MSIRECORD *rec = MSI_CreateRecord( 1 );
4499 UINT r = ERROR_SUCCESS;
4501 MSI_RecordSetStringW( rec, 1, path );
4502 msi_event_fire( dialog->package, L"SelectionPath", rec );
4503 if (path)
4505 /* failure to set the path halts the executing of control events */
4506 r = MSI_SetTargetPathW( dialog->package, argument, path );
4507 msi_free( path );
4509 msi_free( &rec->hdr );
4510 return r;
4513 static UINT event_reset( msi_dialog *dialog, const WCHAR *argument )
4515 msi_dialog_reset( dialog );
4516 return ERROR_SUCCESS;
4519 INT ACTION_ShowDialog( MSIPACKAGE *package, const WCHAR *dialog )
4521 MSIRECORD *row;
4522 INT rc;
4524 if (!TABLE_Exists(package->db, L"Dialog")) return 0;
4526 row = MSI_CreateRecord(0);
4527 if (!row) return -1;
4528 MSI_RecordSetStringW(row, 0, dialog);
4529 rc = MSI_ProcessMessage(package, INSTALLMESSAGE_SHOWDIALOG, row);
4530 msiobj_release(&row->hdr);
4532 if (rc == -2) rc = 0;
4534 if (!rc)
4536 MSIRECORD *row = MSI_CreateRecord(2);
4537 if (!row) return -1;
4538 MSI_RecordSetInteger(row, 1, 2726);
4539 MSI_RecordSetStringW(row, 2, dialog);
4540 MSI_ProcessMessage(package, INSTALLMESSAGE_INFO, row);
4542 msiobj_release(&row->hdr);
4544 return rc;
4547 INT ACTION_DialogBox( MSIPACKAGE *package, const WCHAR *dialog )
4549 INT r;
4551 if (package->next_dialog) ERR("Already got next dialog... ignoring it\n");
4552 package->next_dialog = NULL;
4554 /* Dialogs are chained through NewDialog, which sets the next_dialog member.
4555 * We fall out of the loop if we reach a modeless dialog, which immediately
4556 * returns IDOK, or an EndDialog event, which returns the value corresponding
4557 * to its argument.
4559 r = event_do_dialog( package, dialog, NULL, TRUE );
4560 while (package->next_dialog)
4562 WCHAR *name = package->next_dialog;
4564 package->next_dialog = NULL;
4565 r = event_do_dialog( package, name, NULL, TRUE );
4566 msi_free( name );
4568 return r;
4571 static UINT event_set_install_level( msi_dialog *dialog, const WCHAR *argument )
4573 int level = wcstol( argument, NULL, 10 );
4575 TRACE("setting install level to %d\n", level);
4576 return MSI_SetInstallLevel( dialog->package, level );
4579 static UINT event_directory_list_up( msi_dialog *dialog, const WCHAR *argument )
4581 return msi_dialog_directorylist_up( dialog );
4584 static UINT event_directory_list_new( msi_dialog *dialog, const WCHAR *argument )
4586 return msi_dialog_directorylist_new( dialog );
4589 static UINT event_reinstall_mode( msi_dialog *dialog, const WCHAR *argument )
4591 return msi_set_property( dialog->package->db, L"REINSTALLMODE", argument, -1 );
4594 static UINT event_reinstall( msi_dialog *dialog, const WCHAR *argument )
4596 return msi_set_property( dialog->package->db, L"REINSTALL", argument, -1 );
4599 static UINT event_validate_product_id( msi_dialog *dialog, const WCHAR *argument )
4601 return msi_validate_product_id( dialog->package );
4604 static const struct control_event control_events[] =
4606 { L"EndDialog", pending_event_end_dialog },
4607 { L"NewDialog", pending_event_new_dialog },
4608 { L"SpawnDialog", pending_event_spawn_dialog },
4609 { L"SpawnWaitDialog", event_spawn_wait_dialog },
4610 { L"DoAction", event_do_action },
4611 { L"AddLocal", event_add_local },
4612 { L"Remove", event_remove },
4613 { L"AddSource", event_add_source },
4614 { L"SetTargetPath", event_set_target_path },
4615 { L"Reset", event_reset },
4616 { L"SetInstallLevel", event_set_install_level },
4617 { L"DirectoryListUp", event_directory_list_up },
4618 { L"DirectoryListNew", event_directory_list_new },
4619 { L"SelectionBrowse", event_spawn_dialog },
4620 { L"ReinstallMode", event_reinstall_mode },
4621 { L"Reinstall", event_reinstall },
4622 { L"ValidateProductID", event_validate_product_id },
4623 { NULL, NULL }
4626 static UINT dialog_event_handler( msi_dialog *dialog, const WCHAR *event, const WCHAR *argument )
4628 unsigned int i;
4630 TRACE("handling event %s\n", debugstr_w(event));
4632 if (!event) return ERROR_SUCCESS;
4634 for (i = 0; control_events[i].event; i++)
4636 if (!wcscmp( control_events[i].event, event ))
4637 return control_events[i].handler( dialog, argument );
4639 FIXME("unhandled event %s arg(%s)\n", debugstr_w(event), debugstr_w(argument));
4640 return ERROR_SUCCESS;