server: Clone file_get_sd() and file_set_fd() for directories.
[wine/multimedia.git] / dlls / msi / dialog.c
blob8be1cc70f61198a5c5319cc022ffbe2f61e2e0a5
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2005 Mike McCormack for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #define COBJMACROS
22 #define NONAMELESSUNION
23 #define NONAMELESSSTRUCT
25 #include <stdarg.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "winnls.h"
32 #include "msi.h"
33 #include "msipriv.h"
34 #include "msidefs.h"
35 #include "ocidl.h"
36 #include "olectl.h"
37 #include "richedit.h"
38 #include "commctrl.h"
39 #include "winreg.h"
40 #include "shlwapi.h"
42 #include "wine/debug.h"
43 #include "wine/unicode.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(msi);
48 extern HINSTANCE msi_hInstance;
50 struct msi_control_tag;
51 typedef struct msi_control_tag msi_control;
52 typedef UINT (*msi_handler)( msi_dialog *, msi_control *, WPARAM );
53 typedef void (*msi_update)( msi_dialog *, msi_control * );
55 struct msi_control_tag
57 struct list entry;
58 HWND hwnd;
59 msi_handler handler;
60 msi_update update;
61 LPWSTR property;
62 LPWSTR value;
63 HBITMAP hBitmap;
64 HICON hIcon;
65 LPWSTR tabnext;
66 LPWSTR type;
67 HMODULE hDll;
68 float progress_current;
69 float progress_max;
70 DWORD attributes;
71 WCHAR name[1];
74 typedef struct msi_font_tag
76 struct msi_font_tag *next;
77 HFONT hfont;
78 COLORREF color;
79 WCHAR name[1];
80 } msi_font;
82 struct msi_dialog_tag
84 MSIPACKAGE *package;
85 msi_dialog *parent;
86 msi_dialog_event_handler event_handler;
87 BOOL finished;
88 INT scale;
89 DWORD attributes;
90 SIZE size;
91 HWND hwnd;
92 LPWSTR default_font;
93 msi_font *font_list;
94 struct list controls;
95 HWND hWndFocus;
96 LPWSTR control_default;
97 LPWSTR control_cancel;
98 WCHAR name[1];
101 typedef UINT (*msi_dialog_control_func)( msi_dialog *dialog, MSIRECORD *rec );
102 struct control_handler
104 LPCWSTR control_type;
105 msi_dialog_control_func func;
108 typedef struct
110 msi_dialog* dialog;
111 msi_control *parent;
112 DWORD attributes;
113 LPWSTR propval;
114 } radio_button_group_descr;
116 static const WCHAR szMsiDialogClass[] = {
117 'M','s','i','D','i','a','l','o','g','C','l','o','s','e','C','l','a','s','s',0
119 static const WCHAR szMsiHiddenWindow[] = {
120 'M','s','i','H','i','d','d','e','n','W','i','n','d','o','w',0 };
121 static const WCHAR szStatic[] = { 'S','t','a','t','i','c',0 };
122 static const WCHAR szButton[] = { 'B','U','T','T','O','N', 0 };
123 static const WCHAR szButtonData[] = { 'M','S','I','D','A','T','A',0 };
124 static const WCHAR szProgress[] = { 'P','r','o','g','r','e','s','s',0 };
125 static const WCHAR szText[] = { 'T','e','x','t',0 };
126 static const WCHAR szPushButton[] = { 'P','u','s','h','B','u','t','t','o','n',0 };
127 static const WCHAR szLine[] = { 'L','i','n','e',0 };
128 static const WCHAR szBitmap[] = { 'B','i','t','m','a','p',0 };
129 static const WCHAR szCheckBox[] = { 'C','h','e','c','k','B','o','x',0 };
130 static const WCHAR szScrollableText[] = {
131 'S','c','r','o','l','l','a','b','l','e','T','e','x','t',0 };
132 static const WCHAR szComboBox[] = { 'C','o','m','b','o','B','o','x',0 };
133 static const WCHAR szEdit[] = { 'E','d','i','t',0 };
134 static const WCHAR szMaskedEdit[] = { 'M','a','s','k','e','d','E','d','i','t',0 };
135 static const WCHAR szPathEdit[] = { 'P','a','t','h','E','d','i','t',0 };
136 static const WCHAR szProgressBar[] = {
137 'P','r','o','g','r','e','s','s','B','a','r',0 };
138 static const WCHAR szRadioButtonGroup[] = {
139 'R','a','d','i','o','B','u','t','t','o','n','G','r','o','u','p',0 };
140 static const WCHAR szIcon[] = { 'I','c','o','n',0 };
141 static const WCHAR szSelectionTree[] = {
142 'S','e','l','e','c','t','i','o','n','T','r','e','e',0 };
143 static const WCHAR szGroupBox[] = { 'G','r','o','u','p','B','o','x',0 };
144 static const WCHAR szListBox[] = { 'L','i','s','t','B','o','x',0 };
145 static const WCHAR szDirectoryCombo[] = { 'D','i','r','e','c','t','o','r','y','C','o','m','b','o',0 };
146 static const WCHAR szDirectoryList[] = { 'D','i','r','e','c','t','o','r','y','L','i','s','t',0 };
147 static const WCHAR szVolumeCostList[] = { 'V','o','l','u','m','e','C','o','s','t','L','i','s','t',0 };
148 static const WCHAR szVolumeSelectCombo[] = { 'V','o','l','u','m','e','S','e','l','e','c','t','C','o','m','b','o',0 };
149 static const WCHAR szSelectionDescription[] = {'S','e','l','e','c','t','i','o','n','D','e','s','c','r','i','p','t','i','o','n',0};
150 static const WCHAR szSelectionPath[] = {'S','e','l','e','c','t','i','o','n','P','a','t','h',0};
151 static const WCHAR szProperty[] = {'P','r','o','p','e','r','t','y',0};
153 static UINT msi_dialog_checkbox_handler( msi_dialog *, msi_control *, WPARAM );
154 static void msi_dialog_checkbox_sync_state( msi_dialog *, msi_control * );
155 static UINT msi_dialog_button_handler( msi_dialog *, msi_control *, WPARAM );
156 static UINT msi_dialog_edit_handler( msi_dialog *, msi_control *, WPARAM );
157 static UINT msi_dialog_radiogroup_handler( msi_dialog *, msi_control *, WPARAM );
158 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog );
159 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
160 static MSIFEATURE *msi_seltree_get_selected_feature( msi_control *control );
162 /* dialog sequencing */
164 #define WM_MSI_DIALOG_CREATE (WM_USER+0x100)
165 #define WM_MSI_DIALOG_DESTROY (WM_USER+0x101)
167 static DWORD uiThreadId;
168 static HWND hMsiHiddenWindow;
170 static INT msi_dialog_scale_unit( msi_dialog *dialog, INT val )
172 return (dialog->scale * val + 5) / 10;
175 static msi_control *msi_dialog_find_control( msi_dialog *dialog, LPCWSTR name )
177 msi_control *control;
179 if( !name )
180 return NULL;
181 if( !dialog->hwnd )
182 return NULL;
183 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
184 if( !strcmpW( control->name, name ) ) /* FIXME: case sensitive? */
185 return control;
186 return NULL;
189 static msi_control *msi_dialog_find_control_by_type( msi_dialog *dialog, LPCWSTR type )
191 msi_control *control;
193 if( !type )
194 return NULL;
195 if( !dialog->hwnd )
196 return NULL;
197 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
198 if( !strcmpW( control->type, type ) ) /* FIXME: case sensitive? */
199 return control;
200 return NULL;
203 static msi_control *msi_dialog_find_control_by_hwnd( msi_dialog *dialog, HWND hwnd )
205 msi_control *control;
207 if( !dialog->hwnd )
208 return NULL;
209 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
210 if( hwnd == control->hwnd )
211 return control;
212 return NULL;
215 static LPWSTR msi_get_deformatted_field( MSIPACKAGE *package, MSIRECORD *rec, int field )
217 LPCWSTR str = MSI_RecordGetString( rec, field );
218 LPWSTR ret = NULL;
220 if (str)
221 deformat_string( package, str, &ret );
222 return ret;
225 static LPWSTR msi_dialog_dup_property( msi_dialog *dialog, LPCWSTR property, BOOL indirect )
227 LPWSTR prop = NULL;
229 if (!property)
230 return NULL;
232 if (indirect)
233 prop = msi_dup_property( dialog->package, property );
235 if (!prop)
236 prop = strdupW( property );
238 return prop;
241 msi_dialog *msi_dialog_get_parent( msi_dialog *dialog )
243 return dialog->parent;
246 LPWSTR msi_dialog_get_name( msi_dialog *dialog )
248 return dialog->name;
252 * msi_dialog_get_style
254 * Extract the {\style} string from the front of the text to display and
255 * update the pointer. Only the last style in a list is applied.
257 static LPWSTR msi_dialog_get_style( LPCWSTR p, LPCWSTR *rest )
259 LPWSTR ret;
260 LPCWSTR q, i, first;
261 DWORD len;
263 q = NULL;
264 *rest = p;
265 if( !p )
266 return NULL;
268 while ((first = strchrW( p, '{' )) && (q = strchrW( first + 1, '}' )))
270 p = first + 1;
271 if( *p != '\\' && *p != '&' )
272 return NULL;
274 /* little bit of sanity checking to stop us getting confused with RTF */
275 for( i=++p; i<q; i++ )
276 if( *i == '}' || *i == '\\' )
277 return NULL;
280 if (!p || !q)
281 return NULL;
283 *rest = ++q;
284 len = q - p;
286 ret = msi_alloc( len*sizeof(WCHAR) );
287 if( !ret )
288 return ret;
289 memcpy( ret, p, len*sizeof(WCHAR) );
290 ret[len-1] = 0;
291 return ret;
294 static UINT msi_dialog_add_font( MSIRECORD *rec, LPVOID param )
296 msi_dialog *dialog = param;
297 msi_font *font;
298 LPCWSTR face, name;
299 LOGFONTW lf;
300 INT style;
301 HDC hdc;
303 /* create a font and add it to the list */
304 name = MSI_RecordGetString( rec, 1 );
305 font = msi_alloc( sizeof *font + strlenW( name )*sizeof (WCHAR) );
306 strcpyW( font->name, name );
307 font->next = dialog->font_list;
308 dialog->font_list = font;
310 font->color = MSI_RecordGetInteger( rec, 4 );
312 memset( &lf, 0, sizeof lf );
313 face = MSI_RecordGetString( rec, 2 );
314 lf.lfHeight = MSI_RecordGetInteger( rec, 3 );
315 style = MSI_RecordGetInteger( rec, 5 );
316 if( style & msidbTextStyleStyleBitsBold )
317 lf.lfWeight = FW_BOLD;
318 if( style & msidbTextStyleStyleBitsItalic )
319 lf.lfItalic = TRUE;
320 if( style & msidbTextStyleStyleBitsUnderline )
321 lf.lfUnderline = TRUE;
322 if( style & msidbTextStyleStyleBitsStrike )
323 lf.lfStrikeOut = TRUE;
324 lstrcpynW( lf.lfFaceName, face, LF_FACESIZE );
326 /* adjust the height */
327 hdc = GetDC( dialog->hwnd );
328 if (hdc)
330 lf.lfHeight = -MulDiv(lf.lfHeight, GetDeviceCaps(hdc, LOGPIXELSY), 72);
331 ReleaseDC( dialog->hwnd, hdc );
334 font->hfont = CreateFontIndirectW( &lf );
336 TRACE("Adding font style %s\n", debugstr_w(font->name) );
338 return ERROR_SUCCESS;
341 static msi_font *msi_dialog_find_font( msi_dialog *dialog, LPCWSTR name )
343 msi_font *font;
345 for( font = dialog->font_list; font; font = font->next )
346 if( !strcmpW( font->name, name ) ) /* FIXME: case sensitive? */
347 break;
349 return font;
352 static UINT msi_dialog_set_font( msi_dialog *dialog, HWND hwnd, LPCWSTR name )
354 msi_font *font;
356 font = msi_dialog_find_font( dialog, name );
357 if( font )
358 SendMessageW( hwnd, WM_SETFONT, (WPARAM) font->hfont, TRUE );
359 else
360 ERR("No font entry for %s\n", debugstr_w(name));
361 return ERROR_SUCCESS;
364 static UINT msi_dialog_build_font_list( msi_dialog *dialog )
366 static const WCHAR query[] = {
367 'S','E','L','E','C','T',' ','*',' ',
368 'F','R','O','M',' ','`','T','e','x','t','S','t','y','l','e','`',' ',0
370 UINT r;
371 MSIQUERY *view = NULL;
373 TRACE("dialog %p\n", dialog );
375 r = MSI_OpenQuery( dialog->package->db, &view, query );
376 if( r != ERROR_SUCCESS )
377 return r;
379 r = MSI_IterateRecords( view, NULL, msi_dialog_add_font, dialog );
380 msiobj_release( &view->hdr );
382 return r;
385 static void msi_destroy_control( msi_control *t )
387 list_remove( &t->entry );
388 /* leave dialog->hwnd - destroying parent destroys child windows */
389 msi_free( t->property );
390 msi_free( t->value );
391 if( t->hBitmap )
392 DeleteObject( t->hBitmap );
393 if( t->hIcon )
394 DestroyIcon( t->hIcon );
395 msi_free( t->tabnext );
396 msi_free( t->type );
397 if (t->hDll)
398 FreeLibrary( t->hDll );
399 msi_free( t );
402 static msi_control *msi_dialog_create_window( msi_dialog *dialog,
403 MSIRECORD *rec, DWORD exstyle, LPCWSTR szCls, LPCWSTR name, LPCWSTR text,
404 DWORD style, HWND parent )
406 DWORD x, y, width, height;
407 LPWSTR font = NULL, title_font = NULL;
408 LPCWSTR title = NULL;
409 msi_control *control;
411 style |= WS_CHILD;
413 control = msi_alloc( sizeof *control + strlenW(name)*sizeof(WCHAR) );
414 if (!control)
415 return NULL;
417 strcpyW( control->name, name );
418 list_add_head( &dialog->controls, &control->entry );
419 control->handler = NULL;
420 control->update = NULL;
421 control->property = NULL;
422 control->value = NULL;
423 control->hBitmap = NULL;
424 control->hIcon = NULL;
425 control->hDll = NULL;
426 control->tabnext = strdupW( MSI_RecordGetString( rec, 11) );
427 control->type = strdupW( MSI_RecordGetString( rec, 3 ) );
428 control->progress_current = 0;
429 control->progress_max = 100;
431 x = MSI_RecordGetInteger( rec, 4 );
432 y = MSI_RecordGetInteger( rec, 5 );
433 width = MSI_RecordGetInteger( rec, 6 );
434 height = MSI_RecordGetInteger( rec, 7 );
436 x = msi_dialog_scale_unit( dialog, x );
437 y = msi_dialog_scale_unit( dialog, y );
438 width = msi_dialog_scale_unit( dialog, width );
439 height = msi_dialog_scale_unit( dialog, height );
441 if( text )
443 deformat_string( dialog->package, text, &title_font );
444 font = msi_dialog_get_style( title_font, &title );
447 control->hwnd = CreateWindowExW( exstyle, szCls, title, style,
448 x, y, width, height, parent, NULL, NULL, NULL );
450 TRACE("Dialog %s control %s hwnd %p\n",
451 debugstr_w(dialog->name), debugstr_w(text), control->hwnd );
453 msi_dialog_set_font( dialog, control->hwnd,
454 font ? font : dialog->default_font );
456 msi_free( title_font );
457 msi_free( font );
459 return control;
462 static LPWSTR msi_dialog_get_uitext( msi_dialog *dialog, LPCWSTR key )
464 MSIRECORD *rec;
465 LPWSTR text;
467 static const WCHAR query[] = {
468 's','e','l','e','c','t',' ','*',' ',
469 'f','r','o','m',' ','`','U','I','T','e','x','t','`',' ',
470 'w','h','e','r','e',' ','`','K','e','y','`',' ','=',' ','\'','%','s','\'',0
473 rec = MSI_QueryGetRecord( dialog->package->db, query, key );
474 if (!rec) return NULL;
475 text = strdupW( MSI_RecordGetString( rec, 2 ) );
476 msiobj_release( &rec->hdr );
477 return text;
480 static MSIRECORD *msi_get_binary_record( MSIDATABASE *db, LPCWSTR name )
482 static const WCHAR query[] = {
483 's','e','l','e','c','t',' ','*',' ',
484 'f','r','o','m',' ','B','i','n','a','r','y',' ',
485 'w','h','e','r','e',' ',
486 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0
489 return MSI_QueryGetRecord( db, query, name );
492 static LPWSTR msi_create_tmp_path(void)
494 WCHAR tmp[MAX_PATH];
495 LPWSTR path = NULL;
496 static const WCHAR prefix[] = { 'm','s','i',0 };
497 DWORD len, r;
499 r = GetTempPathW( MAX_PATH, tmp );
500 if( !r )
501 return path;
502 len = lstrlenW( tmp ) + 20;
503 path = msi_alloc( len * sizeof (WCHAR) );
504 if( path )
506 r = GetTempFileNameW( tmp, prefix, 0, path );
507 if (!r)
509 msi_free( path );
510 path = NULL;
513 return path;
517 static HANDLE msi_load_image( MSIDATABASE *db, LPCWSTR name, UINT type,
518 UINT cx, UINT cy, UINT flags )
520 MSIRECORD *rec = NULL;
521 HANDLE himage = NULL;
522 LPWSTR tmp;
523 UINT r;
525 TRACE("%p %s %u %u %08x\n", db, debugstr_w(name), cx, cy, flags);
527 tmp = msi_create_tmp_path();
528 if( !tmp )
529 return himage;
531 rec = msi_get_binary_record( db, name );
532 if( rec )
534 r = MSI_RecordStreamToFile( rec, 2, tmp );
535 if( r == ERROR_SUCCESS )
537 himage = LoadImageW( 0, tmp, type, cx, cy, flags );
539 msiobj_release( &rec->hdr );
541 DeleteFileW( tmp );
543 msi_free( tmp );
544 return himage;
547 static HICON msi_load_icon( MSIDATABASE *db, LPCWSTR text, UINT attributes )
549 DWORD cx = 0, cy = 0, flags;
551 flags = LR_LOADFROMFILE | LR_DEFAULTSIZE;
552 if( attributes & msidbControlAttributesFixedSize )
554 flags &= ~LR_DEFAULTSIZE;
555 if( attributes & msidbControlAttributesIconSize16 )
557 cx += 16;
558 cy += 16;
560 if( attributes & msidbControlAttributesIconSize32 )
562 cx += 32;
563 cy += 32;
565 /* msidbControlAttributesIconSize48 handled by above logic */
567 return msi_load_image( db, text, IMAGE_ICON, cx, cy, flags );
570 static void msi_dialog_update_controls( msi_dialog *dialog, LPCWSTR property )
572 msi_control *control;
574 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
576 if ( !lstrcmpW( control->property, property ) && control->update )
577 control->update( dialog, control );
581 /* called from the Control Event subscription code */
582 void msi_dialog_handle_event( msi_dialog* dialog, LPCWSTR control,
583 LPCWSTR attribute, MSIRECORD *rec )
585 msi_control* ctrl;
586 LPCWSTR font_text, text = NULL;
587 LPWSTR font;
589 static const WCHAR empty[] = {0};
591 ctrl = msi_dialog_find_control( dialog, control );
592 if (!ctrl)
593 return;
594 if( !lstrcmpW(attribute, szText) )
596 font_text = MSI_RecordGetString( rec , 1 );
597 font = msi_dialog_get_style( font_text, &text );
598 if (!text) text = empty;
599 SetWindowTextW( ctrl->hwnd, text );
600 msi_free( font );
601 msi_dialog_check_messages( NULL );
603 else if( !lstrcmpW(attribute, szProgress) )
605 DWORD func, val;
607 func = MSI_RecordGetInteger( rec , 1 );
608 val = MSI_RecordGetInteger( rec , 2 );
610 switch (func)
612 case 0: /* init */
613 ctrl->progress_max = val;
614 ctrl->progress_current = 0;
615 SendMessageW(ctrl->hwnd, PBM_SETRANGE, 0, MAKELPARAM(0,100));
616 SendMessageW(ctrl->hwnd, PBM_SETPOS, 0, 0);
617 break;
618 case 1: /* FIXME: not sure what this is supposed to do */
619 break;
620 case 2: /* move */
621 ctrl->progress_current += val;
622 SendMessageW(ctrl->hwnd, PBM_SETPOS, 100*(ctrl->progress_current/ctrl->progress_max), 0);
623 break;
624 default:
625 ERR("Unknown progress message %d\n", func);
626 break;
629 else if ( !lstrcmpW(attribute, szProperty) )
631 MSIFEATURE *feature = msi_seltree_get_selected_feature( ctrl );
632 MSI_SetPropertyW( dialog->package, ctrl->property, feature->Directory );
634 else if ( !lstrcmpW(attribute, szSelectionPath) )
636 LPWSTR prop = msi_dialog_dup_property( dialog, ctrl->property, TRUE );
637 LPWSTR path;
638 if (!prop) return;
639 path = msi_dup_property( dialog->package, prop );
640 SetWindowTextW( ctrl->hwnd, path );
641 msi_free(prop);
642 msi_free(path);
644 else
646 FIXME("Attribute %s not being set\n", debugstr_w(attribute));
647 return;
651 static void msi_dialog_map_events(msi_dialog* dialog, LPCWSTR control)
653 static const WCHAR Query[] = {
654 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
655 '`','E','v','e','n','t','M','a','p','p','i','n','g','`',' ',
656 'W','H','E','R','E',' ',
657 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
658 'A','N','D',' ',
659 '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',0
661 MSIRECORD *row;
662 LPCWSTR event, attribute;
664 row = MSI_QueryGetRecord( dialog->package->db, Query, dialog->name, control );
665 if (!row)
666 return;
668 event = MSI_RecordGetString( row, 3 );
669 attribute = MSI_RecordGetString( row, 4 );
670 ControlEvent_SubscribeToEvent( dialog->package, dialog, event, control, attribute );
671 msiobj_release( &row->hdr );
674 /* everything except radio buttons */
675 static msi_control *msi_dialog_add_control( msi_dialog *dialog,
676 MSIRECORD *rec, LPCWSTR szCls, DWORD style )
678 DWORD attributes;
679 LPCWSTR text, name;
680 DWORD exstyle = 0;
682 name = MSI_RecordGetString( rec, 2 );
683 attributes = MSI_RecordGetInteger( rec, 8 );
684 text = MSI_RecordGetString( rec, 10 );
685 if( attributes & msidbControlAttributesVisible )
686 style |= WS_VISIBLE;
687 if( ~attributes & msidbControlAttributesEnabled )
688 style |= WS_DISABLED;
689 if( attributes & msidbControlAttributesSunken )
690 exstyle |= WS_EX_CLIENTEDGE;
692 msi_dialog_map_events(dialog, name);
694 return msi_dialog_create_window( dialog, rec, exstyle, szCls, name,
695 text, style, dialog->hwnd );
698 struct msi_text_info
700 msi_font *font;
701 WNDPROC oldproc;
702 DWORD attributes;
706 * we don't erase our own background,
707 * so we have to make sure that the parent window redraws first
709 static void msi_text_on_settext( HWND hWnd )
711 HWND hParent;
712 RECT rc;
714 hParent = GetParent( hWnd );
715 GetWindowRect( hWnd, &rc );
716 MapWindowPoints( NULL, hParent, (LPPOINT) &rc, 2 );
717 InvalidateRect( hParent, &rc, TRUE );
720 static LRESULT WINAPI
721 MSIText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
723 struct msi_text_info *info;
724 LRESULT r = 0;
726 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
728 info = GetPropW(hWnd, szButtonData);
730 if( msg == WM_CTLCOLORSTATIC &&
731 ( info->attributes & msidbControlAttributesTransparent ) )
733 SetBkMode( (HDC)wParam, TRANSPARENT );
734 return (LRESULT) GetStockObject(NULL_BRUSH);
737 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
738 if ( info->font )
739 SetTextColor( (HDC)wParam, info->font->color );
741 switch( msg )
743 case WM_SETTEXT:
744 msi_text_on_settext( hWnd );
745 break;
746 case WM_NCDESTROY:
747 msi_free( info );
748 RemovePropW( hWnd, szButtonData );
749 break;
752 return r;
755 static UINT msi_dialog_text_control( msi_dialog *dialog, MSIRECORD *rec )
757 msi_control *control;
758 struct msi_text_info *info;
759 LPCWSTR text, ptr, prop;
760 LPWSTR font_name;
762 TRACE("%p %p\n", dialog, rec);
764 control = msi_dialog_add_control( dialog, rec, szStatic, SS_LEFT | WS_GROUP );
765 if( !control )
766 return ERROR_FUNCTION_FAILED;
768 info = msi_alloc( sizeof *info );
769 if( !info )
770 return ERROR_SUCCESS;
772 control->attributes = MSI_RecordGetInteger( rec, 8 );
773 prop = MSI_RecordGetString( rec, 9 );
774 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
776 text = MSI_RecordGetString( rec, 10 );
777 font_name = msi_dialog_get_style( text, &ptr );
778 info->font = ( font_name ) ? msi_dialog_find_font( dialog, font_name ) : NULL;
779 msi_free( font_name );
781 info->attributes = MSI_RecordGetInteger( rec, 8 );
782 if( info->attributes & msidbControlAttributesTransparent )
783 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT );
785 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
786 (LONG_PTR)MSIText_WndProc );
787 SetPropW( control->hwnd, szButtonData, info );
789 ControlEvent_SubscribeToEvent( dialog->package, dialog,
790 szSelectionPath, control->name, szSelectionPath );
792 return ERROR_SUCCESS;
795 static UINT msi_dialog_button_control( msi_dialog *dialog, MSIRECORD *rec )
797 msi_control *control;
798 UINT attributes, style;
799 LPWSTR text;
801 TRACE("%p %p\n", dialog, rec);
803 style = WS_TABSTOP;
804 attributes = MSI_RecordGetInteger( rec, 8 );
805 if( attributes & msidbControlAttributesIcon )
806 style |= BS_ICON;
808 control = msi_dialog_add_control( dialog, rec, szButton, style );
809 if( !control )
810 return ERROR_FUNCTION_FAILED;
812 control->handler = msi_dialog_button_handler;
814 /* set the icon */
815 text = msi_get_deformatted_field( dialog->package, rec, 10 );
816 control->hIcon = msi_load_icon( dialog->package->db, text, attributes );
817 if( attributes & msidbControlAttributesIcon )
818 SendMessageW( control->hwnd, BM_SETIMAGE, IMAGE_ICON, (LPARAM) control->hIcon );
819 msi_free( text );
821 return ERROR_SUCCESS;
824 static LPWSTR msi_get_checkbox_value( msi_dialog *dialog, LPCWSTR prop )
826 static const WCHAR query[] = {
827 'S','E','L','E','C','T',' ','*',' ',
828 'F','R','O','M',' ','`','C','h','e','c','k','B','o','x','`',' ',
829 'W','H','E','R','E',' ',
830 '`','P','r','o','p','e','r','t','y','`',' ','=',' ',
831 '\'','%','s','\'',0
833 MSIRECORD *rec = NULL;
834 LPWSTR ret = NULL;
836 /* find if there is a value associated with the checkbox */
837 rec = MSI_QueryGetRecord( dialog->package->db, query, prop );
838 if (!rec)
839 return ret;
841 ret = msi_get_deformatted_field( dialog->package, rec, 2 );
842 if( ret && !ret[0] )
844 msi_free( ret );
845 ret = NULL;
847 msiobj_release( &rec->hdr );
848 if (ret)
849 return ret;
851 ret = msi_dup_property( dialog->package, prop );
852 if( ret && !ret[0] )
854 msi_free( ret );
855 ret = NULL;
858 return ret;
861 static UINT msi_dialog_checkbox_control( msi_dialog *dialog, MSIRECORD *rec )
863 msi_control *control;
864 LPCWSTR prop;
866 TRACE("%p %p\n", dialog, rec);
868 control = msi_dialog_add_control( dialog, rec, szButton,
869 BS_CHECKBOX | BS_MULTILINE | WS_TABSTOP );
870 control->handler = msi_dialog_checkbox_handler;
871 control->update = msi_dialog_checkbox_sync_state;
872 prop = MSI_RecordGetString( rec, 9 );
873 if( prop )
875 control->property = strdupW( prop );
876 control->value = msi_get_checkbox_value( dialog, prop );
877 TRACE("control %s value %s\n", debugstr_w(control->property),
878 debugstr_w(control->value));
880 msi_dialog_checkbox_sync_state( dialog, control );
882 return ERROR_SUCCESS;
885 static UINT msi_dialog_line_control( msi_dialog *dialog, MSIRECORD *rec )
887 DWORD attributes;
888 LPCWSTR name;
889 DWORD style, exstyle = 0;
890 DWORD x, y, width, height;
891 msi_control *control;
893 TRACE("%p %p\n", dialog, rec);
895 style = WS_CHILD | SS_ETCHEDHORZ | SS_SUNKEN;
897 name = MSI_RecordGetString( rec, 2 );
898 attributes = MSI_RecordGetInteger( rec, 8 );
900 if( attributes & msidbControlAttributesVisible )
901 style |= WS_VISIBLE;
902 if( ~attributes & msidbControlAttributesEnabled )
903 style |= WS_DISABLED;
904 if( attributes & msidbControlAttributesSunken )
905 exstyle |= WS_EX_CLIENTEDGE;
907 msi_dialog_map_events(dialog, name);
909 control = msi_alloc( sizeof(*control) + strlenW(name) * sizeof(WCHAR) );
910 if (!control)
911 return ERROR_OUTOFMEMORY;
913 strcpyW( control->name, name );
914 list_add_head( &dialog->controls, &control->entry );
915 control->handler = NULL;
916 control->property = NULL;
917 control->value = NULL;
918 control->hBitmap = NULL;
919 control->hIcon = NULL;
920 control->hDll = NULL;
921 control->tabnext = strdupW( MSI_RecordGetString( rec, 11) );
922 control->type = strdupW( MSI_RecordGetString( rec, 3 ) );
923 control->progress_current = 0;
924 control->progress_max = 100;
926 x = MSI_RecordGetInteger( rec, 4 );
927 y = MSI_RecordGetInteger( rec, 5 );
928 width = MSI_RecordGetInteger( rec, 6 );
930 x = msi_dialog_scale_unit( dialog, x );
931 y = msi_dialog_scale_unit( dialog, y );
932 width = msi_dialog_scale_unit( dialog, width );
933 height = 2; /* line is exactly 2 units in height */
935 control->hwnd = CreateWindowExW( exstyle, szStatic, NULL, style,
936 x, y, width, height, dialog->hwnd, NULL, NULL, NULL );
938 TRACE("Dialog %s control %s hwnd %p\n",
939 debugstr_w(dialog->name), debugstr_w(name), control->hwnd );
941 return ERROR_SUCCESS;
944 /******************** Scroll Text ********************************************/
946 struct msi_scrolltext_info
948 msi_dialog *dialog;
949 msi_control *control;
950 WNDPROC oldproc;
953 static LRESULT WINAPI
954 MSIScrollText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
956 struct msi_scrolltext_info *info;
957 HRESULT r;
959 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
961 info = GetPropW( hWnd, szButtonData );
963 r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
965 switch( msg )
967 case WM_NCDESTROY:
968 msi_free( info );
969 RemovePropW( hWnd, szButtonData );
970 break;
971 case WM_PAINT:
972 /* native MSI sets a wait cursor here */
973 msi_dialog_button_handler( info->dialog, info->control, BN_CLICKED );
974 break;
976 return r;
979 struct msi_streamin_info
981 LPSTR string;
982 DWORD offset;
983 DWORD length;
986 static DWORD CALLBACK
987 msi_richedit_stream_in( DWORD_PTR arg, LPBYTE buffer, LONG count, LONG *pcb )
989 struct msi_streamin_info *info = (struct msi_streamin_info*) arg;
991 if( (count + info->offset) > info->length )
992 count = info->length - info->offset;
993 memcpy( buffer, &info->string[ info->offset ], count );
994 *pcb = count;
995 info->offset += count;
997 TRACE("%d/%d\n", info->offset, info->length);
999 return 0;
1002 static void msi_scrolltext_add_text( msi_control *control, LPCWSTR text )
1004 struct msi_streamin_info info;
1005 EDITSTREAM es;
1007 info.string = strdupWtoA( text );
1008 info.offset = 0;
1009 info.length = lstrlenA( info.string ) + 1;
1011 es.dwCookie = (DWORD_PTR) &info;
1012 es.dwError = 0;
1013 es.pfnCallback = msi_richedit_stream_in;
1015 SendMessageW( control->hwnd, EM_STREAMIN, SF_RTF, (LPARAM) &es );
1017 msi_free( info.string );
1020 static UINT msi_dialog_scrolltext_control( msi_dialog *dialog, MSIRECORD *rec )
1022 static const WCHAR szRichEdit20W[] = {
1023 'R','i','c','h','E','d','i','t','2','0','W',0
1025 struct msi_scrolltext_info *info;
1026 msi_control *control;
1027 HMODULE hRichedit;
1028 LPCWSTR text;
1029 DWORD style;
1031 info = msi_alloc( sizeof *info );
1032 if (!info)
1033 return ERROR_FUNCTION_FAILED;
1035 hRichedit = LoadLibraryA("riched20");
1037 style = WS_BORDER | ES_MULTILINE | WS_VSCROLL |
1038 ES_READONLY | ES_AUTOVSCROLL | WS_TABSTOP;
1039 control = msi_dialog_add_control( dialog, rec, szRichEdit20W, style );
1040 if (!control)
1042 FreeLibrary( hRichedit );
1043 msi_free( info );
1044 return ERROR_FUNCTION_FAILED;
1047 control->hDll = hRichedit;
1049 info->dialog = dialog;
1050 info->control = control;
1052 /* subclass the static control */
1053 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1054 (LONG_PTR)MSIScrollText_WndProc );
1055 SetPropW( control->hwnd, szButtonData, info );
1057 /* add the text into the richedit */
1058 text = MSI_RecordGetString( rec, 10 );
1059 if (text)
1060 msi_scrolltext_add_text( control, text );
1062 return ERROR_SUCCESS;
1065 static HBITMAP msi_load_picture( MSIDATABASE *db, LPCWSTR name,
1066 INT cx, INT cy, DWORD flags )
1068 HBITMAP hOleBitmap = 0, hBitmap = 0, hOldSrcBitmap, hOldDestBitmap;
1069 MSIRECORD *rec = NULL;
1070 IStream *stm = NULL;
1071 IPicture *pic = NULL;
1072 HDC srcdc, destdc;
1073 BITMAP bm;
1074 UINT r;
1076 rec = msi_get_binary_record( db, name );
1077 if( !rec )
1078 goto end;
1080 r = MSI_RecordGetIStream( rec, 2, &stm );
1081 msiobj_release( &rec->hdr );
1082 if( r != ERROR_SUCCESS )
1083 goto end;
1085 r = OleLoadPicture( stm, 0, TRUE, &IID_IPicture, (LPVOID*) &pic );
1086 IStream_Release( stm );
1087 if( FAILED( r ) )
1089 ERR("failed to load picture\n");
1090 goto end;
1093 r = IPicture_get_Handle( pic, (OLE_HANDLE*) &hOleBitmap );
1094 if( FAILED( r ) )
1096 ERR("failed to get bitmap handle\n");
1097 goto end;
1100 /* make the bitmap the desired size */
1101 r = GetObjectW( hOleBitmap, sizeof bm, &bm );
1102 if (r != sizeof bm )
1104 ERR("failed to get bitmap size\n");
1105 goto end;
1108 if (flags & LR_DEFAULTSIZE)
1110 cx = bm.bmWidth;
1111 cy = bm.bmHeight;
1114 srcdc = CreateCompatibleDC( NULL );
1115 hOldSrcBitmap = SelectObject( srcdc, hOleBitmap );
1116 destdc = CreateCompatibleDC( NULL );
1117 hBitmap = CreateCompatibleBitmap( srcdc, cx, cy );
1118 hOldDestBitmap = SelectObject( destdc, hBitmap );
1119 StretchBlt( destdc, 0, 0, cx, cy,
1120 srcdc, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
1121 SelectObject( srcdc, hOldSrcBitmap );
1122 SelectObject( destdc, hOldDestBitmap );
1123 DeleteDC( srcdc );
1124 DeleteDC( destdc );
1126 end:
1127 if ( pic )
1128 IPicture_Release( pic );
1129 return hBitmap;
1132 static UINT msi_dialog_bitmap_control( msi_dialog *dialog, MSIRECORD *rec )
1134 UINT cx, cy, flags, style, attributes;
1135 msi_control *control;
1136 LPWSTR text;
1138 flags = LR_LOADFROMFILE;
1139 style = SS_BITMAP | SS_LEFT | WS_GROUP;
1141 attributes = MSI_RecordGetInteger( rec, 8 );
1142 if( attributes & msidbControlAttributesFixedSize )
1144 flags |= LR_DEFAULTSIZE;
1145 style |= SS_CENTERIMAGE;
1148 control = msi_dialog_add_control( dialog, rec, szStatic, style );
1149 cx = MSI_RecordGetInteger( rec, 6 );
1150 cy = MSI_RecordGetInteger( rec, 7 );
1151 cx = msi_dialog_scale_unit( dialog, cx );
1152 cy = msi_dialog_scale_unit( dialog, cy );
1154 text = msi_get_deformatted_field( dialog->package, rec, 10 );
1155 control->hBitmap = msi_load_picture( dialog->package->db, text, cx, cy, flags );
1156 if( control->hBitmap )
1157 SendMessageW( control->hwnd, STM_SETIMAGE,
1158 IMAGE_BITMAP, (LPARAM) control->hBitmap );
1159 else
1160 ERR("Failed to load bitmap %s\n", debugstr_w(text));
1162 msi_free( text );
1164 return ERROR_SUCCESS;
1167 static UINT msi_dialog_icon_control( msi_dialog *dialog, MSIRECORD *rec )
1169 msi_control *control;
1170 DWORD attributes;
1171 LPWSTR text;
1173 TRACE("\n");
1175 control = msi_dialog_add_control( dialog, rec, szStatic,
1176 SS_ICON | SS_CENTERIMAGE | WS_GROUP );
1178 attributes = MSI_RecordGetInteger( rec, 8 );
1179 text = msi_get_deformatted_field( dialog->package, rec, 10 );
1180 control->hIcon = msi_load_icon( dialog->package->db, text, attributes );
1181 if( control->hIcon )
1182 SendMessageW( control->hwnd, STM_SETICON, (WPARAM) control->hIcon, 0 );
1183 else
1184 ERR("Failed to load bitmap %s\n", debugstr_w(text));
1185 msi_free( text );
1186 return ERROR_SUCCESS;
1189 static UINT msi_dialog_combo_control( msi_dialog *dialog, MSIRECORD *rec )
1191 static const WCHAR szCombo[] = { 'C','O','M','B','O','B','O','X',0 };
1192 DWORD attributes, style;
1194 style = CBS_AUTOHSCROLL | WS_TABSTOP | WS_GROUP | WS_CHILD;
1195 attributes = MSI_RecordGetInteger( rec, 8 );
1196 if ( ~attributes & msidbControlAttributesSorted)
1197 style |= CBS_SORT;
1198 if ( attributes & msidbControlAttributesComboList)
1199 style |= CBS_DROPDOWNLIST;
1200 else
1201 style |= CBS_DROPDOWN;
1203 msi_dialog_add_control( dialog, rec, szCombo, style );
1204 return ERROR_SUCCESS;
1207 /* length of 2^32 + 1 */
1208 #define MAX_NUM_DIGITS 11
1210 static UINT msi_dialog_edit_control( msi_dialog *dialog, MSIRECORD *rec )
1212 msi_control *control;
1213 LPCWSTR prop, text;
1214 LPWSTR val, begin, end;
1215 WCHAR num[MAX_NUM_DIGITS];
1216 DWORD limit;
1218 control = msi_dialog_add_control( dialog, rec, szEdit,
1219 WS_BORDER | WS_TABSTOP | ES_AUTOHSCROLL );
1220 control->handler = msi_dialog_edit_handler;
1222 text = MSI_RecordGetString( rec, 10 );
1223 if ( text )
1225 begin = strchrW( text, '{' );
1226 end = strchrW( text, '}' );
1228 if ( begin && end && end > begin &&
1229 begin[0] >= '0' && begin[0] <= '9' &&
1230 end - begin < MAX_NUM_DIGITS)
1232 lstrcpynW( num, begin + 1, end - begin );
1233 limit = atolW( num );
1235 SendMessageW( control->hwnd, EM_SETLIMITTEXT, limit, 0 );
1239 prop = MSI_RecordGetString( rec, 9 );
1240 if( prop )
1241 control->property = strdupW( prop );
1243 val = msi_dup_property( dialog->package, control->property );
1244 SetWindowTextW( control->hwnd, val );
1245 msi_free( val );
1246 return ERROR_SUCCESS;
1249 /******************** Masked Edit ********************************************/
1251 #define MASK_MAX_GROUPS 20
1253 struct msi_mask_group
1255 UINT len;
1256 UINT ofs;
1257 WCHAR type;
1258 HWND hwnd;
1261 struct msi_maskedit_info
1263 msi_dialog *dialog;
1264 WNDPROC oldproc;
1265 HWND hwnd;
1266 LPWSTR prop;
1267 UINT num_chars;
1268 UINT num_groups;
1269 struct msi_mask_group group[MASK_MAX_GROUPS];
1272 static BOOL msi_mask_editable( WCHAR type )
1274 switch (type)
1276 case '%':
1277 case '#':
1278 case '&':
1279 case '`':
1280 case '?':
1281 case '^':
1282 return TRUE;
1284 return FALSE;
1287 static void msi_mask_control_change( struct msi_maskedit_info *info )
1289 LPWSTR val;
1290 UINT i, n, r;
1292 val = msi_alloc( (info->num_chars+1)*sizeof(WCHAR) );
1293 for( i=0, n=0; i<info->num_groups; i++ )
1295 if( (info->group[i].len + n) > info->num_chars )
1297 ERR("can't fit control %d text into template\n",i);
1298 break;
1300 if (!msi_mask_editable(info->group[i].type))
1302 for(r=0; r<info->group[i].len; r++)
1303 val[n+r] = info->group[i].type;
1304 val[n+r] = 0;
1306 else
1308 r = GetWindowTextW( info->group[i].hwnd, &val[n], info->group[i].len+1 );
1309 if( r != info->group[i].len )
1310 break;
1312 n += r;
1315 TRACE("%d/%d controls were good\n", i, info->num_groups);
1317 if( i == info->num_groups )
1319 TRACE("Set property %s to %s\n",
1320 debugstr_w(info->prop), debugstr_w(val) );
1321 CharUpperBuffW( val, info->num_chars );
1322 MSI_SetPropertyW( info->dialog->package, info->prop, val );
1323 msi_dialog_evaluate_control_conditions( info->dialog );
1325 msi_free( val );
1328 /* now move to the next control if necessary */
1329 static VOID msi_mask_next_control( struct msi_maskedit_info *info, HWND hWnd )
1331 HWND hWndNext;
1332 UINT len, i;
1334 for( i=0; i<info->num_groups; i++ )
1335 if( info->group[i].hwnd == hWnd )
1336 break;
1338 /* don't move from the last control */
1339 if( i >= (info->num_groups-1) )
1340 return;
1342 len = SendMessageW( hWnd, WM_GETTEXTLENGTH, 0, 0 );
1343 if( len < info->group[i].len )
1344 return;
1346 hWndNext = GetNextDlgTabItem( GetParent( hWnd ), hWnd, FALSE );
1347 SetFocus( hWndNext );
1350 static LRESULT WINAPI
1351 MSIMaskedEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1353 struct msi_maskedit_info *info;
1354 HRESULT r;
1356 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1358 info = GetPropW(hWnd, szButtonData);
1360 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1362 switch( msg )
1364 case WM_COMMAND:
1365 if (HIWORD(wParam) == EN_CHANGE)
1367 msi_mask_control_change( info );
1368 msi_mask_next_control( info, (HWND) lParam );
1370 break;
1371 case WM_NCDESTROY:
1372 msi_free( info->prop );
1373 msi_free( info );
1374 RemovePropW( hWnd, szButtonData );
1375 break;
1378 return r;
1381 /* fish the various bits of the property out and put them in the control */
1382 static void
1383 msi_maskedit_set_text( struct msi_maskedit_info *info, LPCWSTR text )
1385 LPCWSTR p;
1386 UINT i;
1388 p = text;
1389 for( i = 0; i < info->num_groups; i++ )
1391 if( info->group[i].len < strlenW( p ) )
1393 LPWSTR chunk = strdupW( p );
1394 chunk[ info->group[i].len ] = 0;
1395 SetWindowTextW( info->group[i].hwnd, chunk );
1396 msi_free( chunk );
1398 else
1400 SetWindowTextW( info->group[i].hwnd, p );
1401 break;
1403 p += info->group[i].len;
1407 static struct msi_maskedit_info * msi_dialog_parse_groups( LPCWSTR mask )
1409 struct msi_maskedit_info * info = NULL;
1410 int i = 0, n = 0, total = 0;
1411 LPCWSTR p;
1413 TRACE("masked control, template %s\n", debugstr_w(mask));
1415 if( !mask )
1416 return info;
1418 info = msi_alloc_zero( sizeof *info );
1419 if( !info )
1420 return info;
1422 p = strchrW(mask, '<');
1423 if( p )
1424 p++;
1425 else
1426 p = mask;
1428 for( i=0; i<MASK_MAX_GROUPS; i++ )
1430 /* stop at the end of the string */
1431 if( p[0] == 0 || p[0] == '>' )
1432 break;
1434 /* count the number of the same identifier */
1435 for( n=0; p[n] == p[0]; n++ )
1437 info->group[i].ofs = total;
1438 info->group[i].type = p[0];
1439 if( p[n] == '=' )
1441 n++;
1442 total++; /* an extra not part of the group */
1444 info->group[i].len = n;
1445 total += n;
1446 p += n;
1449 TRACE("%d characters in %d groups\n", total, i );
1450 if( i == MASK_MAX_GROUPS )
1451 ERR("too many groups in PIDTemplate %s\n", debugstr_w(mask));
1453 info->num_chars = total;
1454 info->num_groups = i;
1456 return info;
1459 static void
1460 msi_maskedit_create_children( struct msi_maskedit_info *info, LPCWSTR font )
1462 DWORD width, height, style, wx, ww;
1463 RECT rect;
1464 HWND hwnd;
1465 UINT i;
1467 style = WS_CHILD | WS_BORDER | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL;
1469 GetClientRect( info->hwnd, &rect );
1471 width = rect.right - rect.left;
1472 height = rect.bottom - rect.top;
1474 for( i = 0; i < info->num_groups; i++ )
1476 if (!msi_mask_editable( info->group[i].type ))
1477 continue;
1478 wx = (info->group[i].ofs * width) / info->num_chars;
1479 ww = (info->group[i].len * width) / info->num_chars;
1481 hwnd = CreateWindowW( szEdit, NULL, style, wx, 0, ww, height,
1482 info->hwnd, NULL, NULL, NULL );
1483 if( !hwnd )
1485 ERR("failed to create mask edit sub window\n");
1486 break;
1489 SendMessageW( hwnd, EM_LIMITTEXT, info->group[i].len, 0 );
1491 msi_dialog_set_font( info->dialog, hwnd,
1492 font?font:info->dialog->default_font );
1493 info->group[i].hwnd = hwnd;
1498 * office 2003 uses "73931<````=````=````=````=`````>@@@@@"
1499 * delphi 7 uses "<????-??????-??????-????>" and "<???-???>"
1500 * filemaker pro 7 uses "<^^^^=^^^^=^^^^=^^^^=^^^^=^^^^=^^^^^>"
1502 static UINT msi_dialog_maskedit_control( msi_dialog *dialog, MSIRECORD *rec )
1504 LPWSTR font_mask, val = NULL, font;
1505 struct msi_maskedit_info *info = NULL;
1506 UINT ret = ERROR_SUCCESS;
1507 msi_control *control;
1508 LPCWSTR prop, mask;
1510 TRACE("\n");
1512 font_mask = msi_get_deformatted_field( dialog->package, rec, 10 );
1513 font = msi_dialog_get_style( font_mask, &mask );
1514 if( !mask )
1516 ERR("mask template is empty\n");
1517 goto end;
1520 info = msi_dialog_parse_groups( mask );
1521 if( !info )
1523 ERR("template %s is invalid\n", debugstr_w(mask));
1524 goto end;
1527 info->dialog = dialog;
1529 control = msi_dialog_add_control( dialog, rec, szStatic,
1530 SS_OWNERDRAW | WS_GROUP | WS_VISIBLE );
1531 if( !control )
1533 ERR("Failed to create maskedit container\n");
1534 ret = ERROR_FUNCTION_FAILED;
1535 goto end;
1537 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
1539 info->hwnd = control->hwnd;
1541 /* subclass the static control */
1542 info->oldproc = (WNDPROC) SetWindowLongPtrW( info->hwnd, GWLP_WNDPROC,
1543 (LONG_PTR)MSIMaskedEdit_WndProc );
1544 SetPropW( control->hwnd, szButtonData, info );
1546 prop = MSI_RecordGetString( rec, 9 );
1547 if( prop )
1548 info->prop = strdupW( prop );
1550 msi_maskedit_create_children( info, font );
1552 if( prop )
1554 val = msi_dup_property( dialog->package, prop );
1555 if( val )
1557 msi_maskedit_set_text( info, val );
1558 msi_free( val );
1562 end:
1563 if( ret != ERROR_SUCCESS )
1564 msi_free( info );
1565 msi_free( font_mask );
1566 msi_free( font );
1567 return ret;
1570 /******************** Progress Bar *****************************************/
1572 static UINT msi_dialog_progress_bar( msi_dialog *dialog, MSIRECORD *rec )
1574 msi_dialog_add_control( dialog, rec, PROGRESS_CLASSW, WS_VISIBLE );
1575 return ERROR_SUCCESS;
1578 /******************** Path Edit ********************************************/
1580 struct msi_pathedit_info
1582 msi_dialog *dialog;
1583 msi_control *control;
1584 WNDPROC oldproc;
1587 static LPWSTR msi_get_window_text( HWND hwnd )
1589 UINT sz, r;
1590 LPWSTR buf;
1592 sz = 0x20;
1593 buf = msi_alloc( sz*sizeof(WCHAR) );
1594 while ( buf )
1596 r = GetWindowTextW( hwnd, buf, sz );
1597 if ( r < (sz - 1) )
1598 break;
1599 sz *= 2;
1600 buf = msi_realloc( buf, sz*sizeof(WCHAR) );
1603 return buf;
1606 static void msi_dialog_update_pathedit( msi_dialog *dialog, msi_control *control )
1608 LPWSTR prop, path;
1609 BOOL indirect;
1611 if (!control && !(control = msi_dialog_find_control_by_type( dialog, szPathEdit )))
1612 return;
1614 indirect = control->attributes & msidbControlAttributesIndirect;
1615 prop = msi_dialog_dup_property( dialog, control->property, indirect );
1616 path = msi_dialog_dup_property( dialog, prop, TRUE );
1618 SetWindowTextW( control->hwnd, path );
1619 SendMessageW( control->hwnd, EM_SETSEL, 0, -1 );
1621 msi_free( path );
1622 msi_free( prop );
1625 /* FIXME: test when this should fail */
1626 static BOOL msi_dialog_verify_path( LPWSTR path )
1628 if ( !lstrlenW( path ) )
1629 return FALSE;
1631 if ( PathIsRelativeW( path ) )
1632 return FALSE;
1634 return TRUE;
1637 /* returns TRUE if the path is valid, FALSE otherwise */
1638 static BOOL msi_dialog_onkillfocus( msi_dialog *dialog, msi_control *control )
1640 LPWSTR buf, prop;
1641 BOOL indirect;
1642 BOOL valid;
1644 indirect = control->attributes & msidbControlAttributesIndirect;
1645 prop = msi_dialog_dup_property( dialog, control->property, indirect );
1647 buf = msi_get_window_text( control->hwnd );
1649 if ( !msi_dialog_verify_path( buf ) )
1651 /* FIXME: display an error message box */
1652 ERR("Invalid path %s\n", debugstr_w( buf ));
1653 valid = FALSE;
1654 SetFocus( control->hwnd );
1656 else
1658 valid = TRUE;
1659 MSI_SetPropertyW( dialog->package, prop, buf );
1662 msi_dialog_update_pathedit( dialog, control );
1664 TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
1665 debugstr_w(prop));
1667 msi_free( buf );
1668 msi_free( prop );
1670 return valid;
1673 static LRESULT WINAPI MSIPathEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1675 struct msi_pathedit_info *info = GetPropW(hWnd, szButtonData);
1676 LRESULT r = 0;
1678 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1680 if ( msg == WM_KILLFOCUS )
1682 /* if the path is invalid, don't handle this message */
1683 if ( !msi_dialog_onkillfocus( info->dialog, info->control ) )
1684 return 0;
1687 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1689 if ( msg == WM_NCDESTROY )
1691 msi_free( info );
1692 RemovePropW( hWnd, szButtonData );
1695 return r;
1698 static UINT msi_dialog_pathedit_control( msi_dialog *dialog, MSIRECORD *rec )
1700 struct msi_pathedit_info *info;
1701 msi_control *control;
1702 LPCWSTR prop;
1704 info = msi_alloc( sizeof *info );
1705 if (!info)
1706 return ERROR_FUNCTION_FAILED;
1708 control = msi_dialog_add_control( dialog, rec, szEdit,
1709 WS_BORDER | WS_TABSTOP );
1710 control->attributes = MSI_RecordGetInteger( rec, 8 );
1711 prop = MSI_RecordGetString( rec, 9 );
1712 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
1714 info->dialog = dialog;
1715 info->control = control;
1716 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1717 (LONG_PTR)MSIPathEdit_WndProc );
1718 SetPropW( control->hwnd, szButtonData, info );
1720 msi_dialog_update_pathedit( dialog, control );
1722 return ERROR_SUCCESS;
1725 /* radio buttons are a bit different from normal controls */
1726 static UINT msi_dialog_create_radiobutton( MSIRECORD *rec, LPVOID param )
1728 radio_button_group_descr *group = (radio_button_group_descr *)param;
1729 msi_dialog *dialog = group->dialog;
1730 msi_control *control;
1731 LPCWSTR prop, text, name;
1732 DWORD style, attributes = group->attributes;
1734 style = WS_CHILD | BS_AUTORADIOBUTTON | BS_MULTILINE | WS_TABSTOP;
1735 name = MSI_RecordGetString( rec, 3 );
1736 text = MSI_RecordGetString( rec, 8 );
1737 if( attributes & msidbControlAttributesVisible )
1738 style |= WS_VISIBLE;
1739 if( ~attributes & msidbControlAttributesEnabled )
1740 style |= WS_DISABLED;
1742 control = msi_dialog_create_window( dialog, rec, 0, szButton, name, text,
1743 style, group->parent->hwnd );
1744 if (!control)
1745 return ERROR_FUNCTION_FAILED;
1746 control->handler = msi_dialog_radiogroup_handler;
1748 if (!lstrcmpW(control->name, group->propval))
1749 SendMessageW(control->hwnd, BM_SETCHECK, BST_CHECKED, 0);
1751 prop = MSI_RecordGetString( rec, 1 );
1752 if( prop )
1753 control->property = strdupW( prop );
1755 return ERROR_SUCCESS;
1758 static UINT msi_dialog_radiogroup_control( msi_dialog *dialog, MSIRECORD *rec )
1760 static const WCHAR query[] = {
1761 'S','E','L','E','C','T',' ','*',' ',
1762 'F','R','O','M',' ','R','a','d','i','o','B','u','t','t','o','n',' ',
1763 'W','H','E','R','E',' ',
1764 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
1765 UINT r;
1766 LPCWSTR prop;
1767 msi_control *control;
1768 MSIQUERY *view = NULL;
1769 radio_button_group_descr group;
1770 MSIPACKAGE *package = dialog->package;
1771 WNDPROC oldproc;
1772 DWORD attr, style = WS_GROUP;
1774 prop = MSI_RecordGetString( rec, 9 );
1776 TRACE("%p %p %s\n", dialog, rec, debugstr_w( prop ));
1778 attr = MSI_RecordGetInteger( rec, 8 );
1779 if (attr & msidbControlAttributesHasBorder)
1780 style |= BS_GROUPBOX;
1781 else
1782 style |= BS_OWNERDRAW;
1784 /* Create parent group box to hold radio buttons */
1785 control = msi_dialog_add_control( dialog, rec, szButton, style );
1786 if( !control )
1787 return ERROR_FUNCTION_FAILED;
1789 oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1790 (LONG_PTR)MSIRadioGroup_WndProc );
1791 SetPropW(control->hwnd, szButtonData, oldproc);
1792 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
1794 if( prop )
1795 control->property = strdupW( prop );
1797 /* query the Radio Button table for all control in this group */
1798 r = MSI_OpenQuery( package->db, &view, query, prop );
1799 if( r != ERROR_SUCCESS )
1801 ERR("query failed for dialog %s radio group %s\n",
1802 debugstr_w(dialog->name), debugstr_w(prop));
1803 return ERROR_INVALID_PARAMETER;
1806 group.dialog = dialog;
1807 group.parent = control;
1808 group.attributes = MSI_RecordGetInteger( rec, 8 );
1809 group.propval = msi_dup_property( dialog->package, control->property );
1811 r = MSI_IterateRecords( view, 0, msi_dialog_create_radiobutton, &group );
1812 msiobj_release( &view->hdr );
1813 msi_free( group.propval );
1815 return r;
1818 /******************** Selection Tree ***************************************/
1820 struct msi_selection_tree_info
1822 msi_dialog *dialog;
1823 HWND hwnd;
1824 WNDPROC oldproc;
1825 HTREEITEM selected;
1828 static void
1829 msi_seltree_sync_item_state( HWND hwnd, MSIFEATURE *feature, HTREEITEM hItem )
1831 TVITEMW tvi;
1832 DWORD index = feature->Action;
1834 TRACE("Feature %s -> %d %d %d\n", debugstr_w(feature->Title),
1835 feature->Installed, feature->Action, feature->ActionRequest);
1837 if (index == INSTALLSTATE_UNKNOWN)
1838 index = INSTALLSTATE_ABSENT;
1840 tvi.mask = TVIF_STATE;
1841 tvi.hItem = hItem;
1842 tvi.state = INDEXTOSTATEIMAGEMASK( index );
1843 tvi.stateMask = TVIS_STATEIMAGEMASK;
1845 SendMessageW( hwnd, TVM_SETITEMW, 0, (LPARAM) &tvi );
1848 static UINT
1849 msi_seltree_popup_menu( HWND hwnd, INT x, INT y )
1851 HMENU hMenu;
1852 INT r;
1854 /* create a menu to display */
1855 hMenu = CreatePopupMenu();
1857 /* FIXME: load strings from resources */
1858 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_LOCAL, "Install feature locally");
1859 AppendMenuA( hMenu, MF_GRAYED, 0x1000, "Install entire feature");
1860 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ADVERTISED, "Install on demand");
1861 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ABSENT, "Don't install");
1862 r = TrackPopupMenu( hMenu, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RETURNCMD,
1863 x, y, 0, hwnd, NULL );
1864 DestroyMenu( hMenu );
1865 return r;
1868 static MSIFEATURE *
1869 msi_seltree_feature_from_item( HWND hwnd, HTREEITEM hItem )
1871 TVITEMW tvi;
1873 /* get the feature from the item */
1874 memset( &tvi, 0, sizeof tvi );
1875 tvi.hItem = hItem;
1876 tvi.mask = TVIF_PARAM | TVIF_HANDLE;
1877 SendMessageW( hwnd, TVM_GETITEMW, 0, (LPARAM) &tvi );
1879 return (MSIFEATURE*) tvi.lParam;
1882 static LRESULT
1883 msi_seltree_menu( HWND hwnd, HTREEITEM hItem )
1885 struct msi_selection_tree_info *info;
1886 MSIFEATURE *feature;
1887 MSIPACKAGE *package;
1888 union {
1889 RECT rc;
1890 POINT pt[2];
1891 HTREEITEM hItem;
1892 } u;
1893 UINT r;
1895 info = GetPropW(hwnd, szButtonData);
1896 package = info->dialog->package;
1898 feature = msi_seltree_feature_from_item( hwnd, hItem );
1899 if (!feature)
1901 ERR("item %p feature was NULL\n", hItem);
1902 return 0;
1905 /* get the item's rectangle to put the menu just below it */
1906 u.hItem = hItem;
1907 SendMessageW( hwnd, TVM_GETITEMRECT, 0, (LPARAM) &u.rc );
1908 MapWindowPoints( hwnd, NULL, u.pt, 2 );
1910 r = msi_seltree_popup_menu( hwnd, u.rc.left, u.rc.top );
1912 switch (r)
1914 case INSTALLSTATE_LOCAL:
1915 case INSTALLSTATE_ADVERTISED:
1916 case INSTALLSTATE_ABSENT:
1917 msi_feature_set_state(package, feature, r);
1918 break;
1919 default:
1920 FIXME("select feature and all children\n");
1923 /* update */
1924 msi_seltree_sync_item_state( hwnd, feature, hItem );
1925 ACTION_UpdateComponentStates( package, feature->Feature );
1927 return 0;
1930 static MSIFEATURE *msi_seltree_get_selected_feature( msi_control *control )
1932 struct msi_selection_tree_info *info = GetPropW(control->hwnd, szButtonData);
1933 return msi_seltree_feature_from_item( control->hwnd, info->selected );
1936 static LRESULT WINAPI
1937 MSISelectionTree_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1939 struct msi_selection_tree_info *info;
1940 TVHITTESTINFO tvhti;
1941 HRESULT r;
1943 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1945 info = GetPropW(hWnd, szButtonData);
1947 switch( msg )
1949 case WM_LBUTTONDOWN:
1950 tvhti.pt.x = (short)LOWORD( lParam );
1951 tvhti.pt.y = (short)HIWORD( lParam );
1952 tvhti.flags = 0;
1953 tvhti.hItem = 0;
1954 r = CallWindowProcW(info->oldproc, hWnd, TVM_HITTEST, 0, (LPARAM) &tvhti );
1955 if (tvhti.flags & TVHT_ONITEMSTATEICON)
1956 return msi_seltree_menu( hWnd, tvhti.hItem );
1957 break;
1960 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1962 switch( msg )
1964 case WM_NCDESTROY:
1965 msi_free( info );
1966 RemovePropW( hWnd, szButtonData );
1967 break;
1969 return r;
1972 static void
1973 msi_seltree_add_child_features( MSIPACKAGE *package, HWND hwnd,
1974 LPCWSTR parent, HTREEITEM hParent )
1976 struct msi_selection_tree_info *info = GetPropW( hwnd, szButtonData );
1977 MSIFEATURE *feature;
1978 TVINSERTSTRUCTW tvis;
1979 HTREEITEM hitem, hfirst = NULL;
1981 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
1983 if ( lstrcmpW( parent, feature->Feature_Parent ) )
1984 continue;
1986 if ( !feature->Title )
1987 continue;
1989 if ( !feature->Display )
1990 continue;
1992 memset( &tvis, 0, sizeof tvis );
1993 tvis.hParent = hParent;
1994 tvis.hInsertAfter = TVI_LAST;
1995 tvis.u.item.mask = TVIF_TEXT | TVIF_PARAM;
1996 tvis.u.item.pszText = feature->Title;
1997 tvis.u.item.lParam = (LPARAM) feature;
1999 hitem = (HTREEITEM) SendMessageW( hwnd, TVM_INSERTITEMW, 0, (LPARAM) &tvis );
2000 if (!hitem)
2001 continue;
2003 if (!hfirst)
2004 hfirst = hitem;
2006 msi_seltree_sync_item_state( hwnd, feature, hitem );
2007 msi_seltree_add_child_features( package, hwnd,
2008 feature->Feature, hitem );
2010 /* the node is expanded if Display is odd */
2011 if ( feature->Display % 2 != 0 )
2012 SendMessageW( hwnd, TVM_EXPAND, TVE_EXPAND, (LPARAM) hitem );
2015 /* select the first item */
2016 SendMessageW( hwnd, TVM_SELECTITEM, TVGN_CARET | TVGN_DROPHILITE, (LPARAM) hfirst );
2017 info->selected = hfirst;
2020 static void msi_seltree_create_imagelist( HWND hwnd )
2022 const int bm_width = 32, bm_height = 16, bm_count = 3;
2023 const int bm_resource = 0x1001;
2024 HIMAGELIST himl;
2025 int i;
2026 HBITMAP hbmp;
2028 himl = ImageList_Create( bm_width, bm_height, FALSE, 4, 0 );
2029 if (!himl)
2031 ERR("failed to create image list\n");
2032 return;
2035 for (i=0; i<bm_count; i++)
2037 hbmp = LoadBitmapW( msi_hInstance, MAKEINTRESOURCEW(i+bm_resource) );
2038 if (!hbmp)
2040 ERR("failed to load bitmap %d\n", i);
2041 break;
2045 * Add a dummy bitmap at offset zero because the treeview
2046 * can't use it as a state mask (zero means no user state).
2048 if (!i)
2049 ImageList_Add( himl, hbmp, NULL );
2051 ImageList_Add( himl, hbmp, NULL );
2054 SendMessageW( hwnd, TVM_SETIMAGELIST, TVSIL_STATE, (LPARAM)himl );
2057 static UINT msi_dialog_seltree_handler( msi_dialog *dialog,
2058 msi_control *control, WPARAM param )
2060 struct msi_selection_tree_info *info = GetPropW( control->hwnd, szButtonData );
2061 LPNMTREEVIEWW tv = (LPNMTREEVIEWW)param;
2062 MSIRECORD *row, *rec;
2063 MSIFOLDER *folder;
2064 MSIFEATURE *feature;
2065 LPCWSTR dir, title = NULL;
2066 UINT r = ERROR_SUCCESS;
2068 static const WCHAR select[] = {
2069 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2070 '`','F','e','a','t','u','r','e','`',' ','W','H','E','R','E',' ',
2071 '`','T','i','t','l','e','`',' ','=',' ','\'','%','s','\'',0
2074 if (tv->hdr.code != TVN_SELCHANGINGW)
2075 return ERROR_SUCCESS;
2077 info->selected = tv->itemNew.hItem;
2079 if (!(tv->itemNew.mask & TVIF_TEXT))
2081 feature = msi_seltree_feature_from_item( control->hwnd, tv->itemNew.hItem );
2082 if (feature)
2083 title = feature->Title;
2085 else
2086 title = tv->itemNew.pszText;
2088 row = MSI_QueryGetRecord( dialog->package->db, select, title );
2089 if (!row)
2090 return ERROR_FUNCTION_FAILED;
2092 rec = MSI_CreateRecord( 1 );
2094 MSI_RecordSetStringW( rec, 1, MSI_RecordGetString( row, 4 ) );
2095 ControlEvent_FireSubscribedEvent( dialog->package, szSelectionDescription, rec );
2097 dir = MSI_RecordGetString( row, 7 );
2098 folder = get_loaded_folder( dialog->package, dir );
2099 if (!folder)
2101 r = ERROR_FUNCTION_FAILED;
2102 goto done;
2105 MSI_RecordSetStringW( rec, 1, folder->ResolvedTarget );
2106 ControlEvent_FireSubscribedEvent( dialog->package, szSelectionPath, rec );
2108 done:
2109 msiobj_release(&row->hdr);
2110 msiobj_release(&rec->hdr);
2112 return r;
2115 static UINT msi_dialog_selection_tree( msi_dialog *dialog, MSIRECORD *rec )
2117 msi_control *control;
2118 LPCWSTR prop;
2119 MSIPACKAGE *package = dialog->package;
2120 DWORD style;
2121 struct msi_selection_tree_info *info;
2123 info = msi_alloc( sizeof *info );
2124 if (!info)
2125 return ERROR_FUNCTION_FAILED;
2127 /* create the treeview control */
2128 style = TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT;
2129 style |= WS_GROUP | WS_VSCROLL;
2130 control = msi_dialog_add_control( dialog, rec, WC_TREEVIEWW, style );
2131 if (!control)
2133 msi_free(info);
2134 return ERROR_FUNCTION_FAILED;
2137 control->handler = msi_dialog_seltree_handler;
2138 control->attributes = MSI_RecordGetInteger( rec, 8 );
2139 prop = MSI_RecordGetString( rec, 9 );
2140 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2142 /* subclass */
2143 info->dialog = dialog;
2144 info->hwnd = control->hwnd;
2145 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2146 (LONG_PTR)MSISelectionTree_WndProc );
2147 SetPropW( control->hwnd, szButtonData, info );
2149 ControlEvent_SubscribeToEvent( dialog->package, dialog,
2150 szSelectionPath, control->name, szProperty );
2152 /* initialize it */
2153 msi_seltree_create_imagelist( control->hwnd );
2154 msi_seltree_add_child_features( package, control->hwnd, NULL, NULL );
2156 return ERROR_SUCCESS;
2159 /******************** Group Box ***************************************/
2161 static UINT msi_dialog_group_box( msi_dialog *dialog, MSIRECORD *rec )
2163 msi_control *control;
2164 DWORD style;
2166 style = BS_GROUPBOX | WS_CHILD | WS_GROUP;
2167 control = msi_dialog_add_control( dialog, rec, WC_BUTTONW, style );
2168 if (!control)
2169 return ERROR_FUNCTION_FAILED;
2171 return ERROR_SUCCESS;
2174 /******************** List Box ***************************************/
2176 struct msi_listbox_info
2178 msi_dialog *dialog;
2179 HWND hwnd;
2180 WNDPROC oldproc;
2181 DWORD num_items;
2182 DWORD addpos_items;
2183 LPWSTR *items;
2186 static LRESULT WINAPI MSIListBox_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2188 struct msi_listbox_info *info;
2189 LRESULT r;
2190 DWORD j;
2192 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
2194 info = GetPropW( hWnd, szButtonData );
2195 if (!info)
2196 return 0;
2198 r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
2200 switch( msg )
2202 case WM_NCDESTROY:
2203 for (j = 0; j < info->num_items; j++)
2204 msi_free( info->items[j] );
2205 msi_free( info->items );
2206 msi_free( info );
2207 RemovePropW( hWnd, szButtonData );
2208 break;
2211 return r;
2214 static UINT msi_listbox_add_item( MSIRECORD *rec, LPVOID param )
2216 struct msi_listbox_info *info = param;
2217 LPCWSTR value, text;
2218 int pos;
2220 value = MSI_RecordGetString( rec, 3 );
2221 text = MSI_RecordGetString( rec, 4 );
2223 info->items[info->addpos_items] = strdupW( value );
2225 pos = SendMessageW( info->hwnd, LB_ADDSTRING, 0, (LPARAM)text );
2226 SendMessageW( info->hwnd, LB_SETITEMDATA, pos, (LPARAM)info->items[info->addpos_items] );
2227 info->addpos_items++;
2228 return ERROR_SUCCESS;
2231 static UINT msi_listbox_add_items( struct msi_listbox_info *info, LPCWSTR property )
2233 UINT r;
2234 MSIQUERY *view = NULL;
2235 DWORD count;
2237 static const WCHAR query[] = {
2238 'S','E','L','E','C','T',' ','*',' ',
2239 'F','R','O','M',' ','`','L','i','s','t','B','o','x','`',' ',
2240 'W','H','E','R','E',' ',
2241 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',' ',
2242 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','`',0
2245 r = MSI_OpenQuery( info->dialog->package->db, &view, query, property );
2246 if ( r != ERROR_SUCCESS )
2247 return r;
2249 /* just get the number of records */
2250 count = 0;
2251 r = MSI_IterateRecords( view, &count, NULL, NULL );
2253 info->num_items = count;
2254 info->items = msi_alloc( sizeof(*info->items) * count );
2256 r = MSI_IterateRecords( view, NULL, msi_listbox_add_item, info );
2257 msiobj_release( &view->hdr );
2259 return r;
2262 static UINT msi_dialog_listbox_handler( msi_dialog *dialog,
2263 msi_control *control, WPARAM param )
2265 struct msi_listbox_info *info;
2266 int index;
2267 LPCWSTR value;
2269 if( HIWORD(param) != LBN_SELCHANGE )
2270 return ERROR_SUCCESS;
2272 info = GetPropW( control->hwnd, szButtonData );
2273 index = SendMessageW( control->hwnd, LB_GETCURSEL, 0, 0 );
2274 value = (LPCWSTR) SendMessageW( control->hwnd, LB_GETITEMDATA, index, 0 );
2276 MSI_SetPropertyW( info->dialog->package,
2277 control->property, value );
2278 msi_dialog_evaluate_control_conditions( info->dialog );
2280 return ERROR_SUCCESS;
2283 static UINT msi_dialog_list_box( msi_dialog *dialog, MSIRECORD *rec )
2285 struct msi_listbox_info *info;
2286 msi_control *control;
2287 DWORD attributes, style;
2288 LPCWSTR prop;
2290 info = msi_alloc( sizeof *info );
2291 if (!info)
2292 return ERROR_FUNCTION_FAILED;
2294 style = WS_TABSTOP | WS_GROUP | WS_CHILD | LBS_NOTIFY | WS_VSCROLL | WS_BORDER;
2295 attributes = MSI_RecordGetInteger( rec, 8 );
2296 if (~attributes & msidbControlAttributesSorted)
2297 style |= LBS_SORT;
2299 control = msi_dialog_add_control( dialog, rec, WC_LISTBOXW, style );
2300 if (!control)
2302 msi_free(info);
2303 return ERROR_FUNCTION_FAILED;
2306 control->handler = msi_dialog_listbox_handler;
2308 prop = MSI_RecordGetString( rec, 9 );
2309 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2311 /* subclass */
2312 info->dialog = dialog;
2313 info->hwnd = control->hwnd;
2314 info->items = NULL;
2315 info->addpos_items = 0;
2316 info->oldproc = (WNDPROC)SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2317 (LONG_PTR)MSIListBox_WndProc );
2318 SetPropW( control->hwnd, szButtonData, info );
2320 if ( control->property )
2321 msi_listbox_add_items( info, control->property );
2323 return ERROR_SUCCESS;
2326 /******************** Directory Combo ***************************************/
2328 static void msi_dialog_update_directory_combo( msi_dialog *dialog, msi_control *control )
2330 LPWSTR prop, path;
2331 BOOL indirect;
2333 if (!control && !(control = msi_dialog_find_control_by_type( dialog, szDirectoryCombo )))
2334 return;
2336 indirect = control->attributes & msidbControlAttributesIndirect;
2337 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2338 path = msi_dialog_dup_property( dialog, prop, TRUE );
2340 PathStripPathW( path );
2341 PathRemoveBackslashW( path );
2343 SendMessageW( control->hwnd, CB_INSERTSTRING, 0, (LPARAM)path );
2344 SendMessageW( control->hwnd, CB_SETCURSEL, 0, 0 );
2346 msi_free( path );
2347 msi_free( prop );
2350 static UINT msi_dialog_directory_combo( msi_dialog *dialog, MSIRECORD *rec )
2352 msi_control *control;
2353 LPCWSTR prop;
2354 DWORD style;
2356 /* FIXME: use CBS_OWNERDRAWFIXED and add owner draw code */
2357 style = CBS_DROPDOWNLIST | CBS_HASSTRINGS | WS_CHILD |
2358 WS_GROUP | WS_TABSTOP | WS_VSCROLL;
2359 control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
2360 if (!control)
2361 return ERROR_FUNCTION_FAILED;
2363 control->attributes = MSI_RecordGetInteger( rec, 8 );
2364 prop = MSI_RecordGetString( rec, 9 );
2365 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2367 msi_dialog_update_directory_combo( dialog, control );
2369 return ERROR_SUCCESS;
2372 /******************** Directory List ***************************************/
2374 static void msi_dialog_update_directory_list( msi_dialog *dialog, msi_control *control )
2376 WCHAR dir_spec[MAX_PATH];
2377 WIN32_FIND_DATAW wfd;
2378 LPWSTR prop, path;
2379 BOOL indirect;
2380 LVITEMW item;
2381 HANDLE file;
2383 static const WCHAR asterisk[] = {'*',0};
2384 static const WCHAR dot[] = {'.',0};
2385 static const WCHAR dotdot[] = {'.','.',0};
2387 if (!control && !(control = msi_dialog_find_control_by_type( dialog, szDirectoryList )))
2388 return;
2390 /* clear the list-view */
2391 SendMessageW( control->hwnd, LVM_DELETEALLITEMS, 0, 0 );
2393 indirect = control->attributes & msidbControlAttributesIndirect;
2394 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2395 path = msi_dialog_dup_property( dialog, prop, TRUE );
2397 lstrcpyW( dir_spec, path );
2398 lstrcatW( dir_spec, asterisk );
2400 file = FindFirstFileW( dir_spec, &wfd );
2401 if ( file == INVALID_HANDLE_VALUE )
2402 return;
2406 if ( wfd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY )
2407 continue;
2409 if ( !lstrcmpW( wfd.cFileName, dot ) || !lstrcmpW( wfd.cFileName, dotdot ) )
2410 continue;
2412 item.mask = LVIF_TEXT;
2413 item.cchTextMax = MAX_PATH;
2414 item.iItem = 0;
2415 item.iSubItem = 0;
2416 item.pszText = wfd.cFileName;
2418 SendMessageW( control->hwnd, LVM_INSERTITEMW, 0, (LPARAM)&item );
2419 } while ( FindNextFileW( file, &wfd ) );
2421 msi_free( prop );
2422 msi_free( path );
2423 FindClose( file );
2426 UINT msi_dialog_directorylist_up( msi_dialog *dialog )
2428 msi_control *control;
2429 LPWSTR prop, path, ptr;
2430 BOOL indirect;
2432 control = msi_dialog_find_control_by_type( dialog, szDirectoryList );
2433 indirect = control->attributes & msidbControlAttributesIndirect;
2434 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2435 path = msi_dialog_dup_property( dialog, prop, TRUE );
2437 /* strip off the last directory */
2438 ptr = PathFindFileNameW( path );
2439 if (ptr != path) *(ptr - 1) = '\0';
2440 PathAddBackslashW( path );
2442 MSI_SetPropertyW( dialog->package, prop, path );
2444 msi_dialog_update_directory_list( dialog, NULL );
2445 msi_dialog_update_directory_combo( dialog, NULL );
2446 msi_dialog_update_pathedit( dialog, NULL );
2448 msi_free( path );
2449 msi_free( prop );
2451 return ERROR_SUCCESS;
2454 static UINT msi_dialog_dirlist_handler( msi_dialog *dialog,
2455 msi_control *control, WPARAM param )
2457 LPNMHDR nmhdr = (LPNMHDR)param;
2458 WCHAR new_path[MAX_PATH];
2459 WCHAR text[MAX_PATH];
2460 LPWSTR path, prop;
2461 BOOL indirect;
2462 LVITEMW item;
2463 int index;
2465 static const WCHAR backslash[] = {'\\',0};
2467 if (nmhdr->code != LVN_ITEMACTIVATE)
2468 return ERROR_SUCCESS;
2470 index = SendMessageW( control->hwnd, LVM_GETNEXTITEM, -1, LVNI_SELECTED );
2471 if ( index < 0 )
2473 ERR("No list-view item selected!\n");
2474 return ERROR_FUNCTION_FAILED;
2477 item.iSubItem = 0;
2478 item.pszText = text;
2479 item.cchTextMax = MAX_PATH;
2480 SendMessageW( control->hwnd, LVM_GETITEMTEXTW, index, (LPARAM)&item );
2482 indirect = control->attributes & msidbControlAttributesIndirect;
2483 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2484 path = msi_dialog_dup_property( dialog, prop, TRUE );
2486 lstrcpyW( new_path, path );
2487 lstrcatW( new_path, text );
2488 lstrcatW( new_path, backslash );
2490 MSI_SetPropertyW( dialog->package, prop, new_path );
2492 msi_dialog_update_directory_list( dialog, NULL );
2493 msi_dialog_update_directory_combo( dialog, NULL );
2494 msi_dialog_update_pathedit( dialog, NULL );
2496 msi_free( prop );
2497 msi_free( path );
2498 return ERROR_SUCCESS;
2501 static UINT msi_dialog_directory_list( msi_dialog *dialog, MSIRECORD *rec )
2503 msi_control *control;
2504 LPCWSTR prop;
2505 DWORD style;
2507 style = LVS_LIST | WS_VSCROLL | LVS_SHAREIMAGELISTS |
2508 LVS_AUTOARRANGE | LVS_SINGLESEL | WS_BORDER |
2509 LVS_SORTASCENDING | WS_CHILD | WS_GROUP | WS_TABSTOP;
2510 control = msi_dialog_add_control( dialog, rec, WC_LISTVIEWW, style );
2511 if (!control)
2512 return ERROR_FUNCTION_FAILED;
2514 control->attributes = MSI_RecordGetInteger( rec, 8 );
2515 control->handler = msi_dialog_dirlist_handler;
2516 prop = MSI_RecordGetString( rec, 9 );
2517 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2519 /* double click to activate an item in the list */
2520 SendMessageW( control->hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE,
2521 0, LVS_EX_TWOCLICKACTIVATE );
2523 msi_dialog_update_directory_list( dialog, control );
2525 return ERROR_SUCCESS;
2528 /******************** VolumeCost List ***************************************/
2530 static BOOL str_is_number( LPCWSTR str )
2532 int i;
2534 for (i = 0; i < lstrlenW( str ); i++)
2535 if (!isdigitW(str[i]))
2536 return FALSE;
2538 return TRUE;
2541 static const WCHAR column_keys[][80] =
2543 {'V','o','l','u','m','e','C','o','s','t','V','o','l','u','m','e',0},
2544 {'V','o','l','u','m','e','C','o','s','t','S','i','z','e',0},
2545 {'V','o','l','u','m','e','C','o','s','t','A','v','a','i','l','a','b','l','e',0},
2546 {'V','o','l','u','m','e','C','o','s','t','R','e','q','u','i','r','e','d',0},
2547 {'V','o','l','u','m','e','C','o','s','t','D','i','f','f','e','r','e','n','c','e',0}
2550 static void msi_dialog_vcl_add_columns( msi_dialog *dialog, msi_control *control, MSIRECORD *rec )
2552 LPCWSTR text = MSI_RecordGetString( rec, 10 );
2553 LPCWSTR begin = text, end;
2554 WCHAR *num;
2555 LVCOLUMNW lvc;
2556 DWORD count = 0;
2558 static const WCHAR zero[] = {'0',0};
2559 static const WCHAR negative[] = {'-',0};
2561 if (!text) return;
2563 while ((begin = strchrW( begin, '{' )) && count < 5)
2565 if (!(end = strchrW( begin, '}' )))
2566 return;
2568 num = msi_alloc( (end-begin+1)*sizeof(WCHAR) );
2569 if (!num)
2570 return;
2572 lstrcpynW( num, begin + 1, end - begin );
2573 begin += end - begin + 1;
2575 /* empty braces or '0' hides the column */
2576 if ( !num[0] || !lstrcmpW( num, zero ) )
2578 count++;
2579 msi_free( num );
2580 continue;
2583 /* the width must be a positive number
2584 * if a width is invalid, all remaining columns are hidden
2586 if ( !strncmpW( num, negative, 1 ) || !str_is_number( num ) ) {
2587 msi_free( num );
2588 return;
2591 ZeroMemory( &lvc, sizeof(lvc) );
2592 lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
2593 lvc.cx = atolW( num );
2594 lvc.pszText = msi_dialog_get_uitext( dialog, column_keys[count] );
2596 SendMessageW( control->hwnd, LVM_INSERTCOLUMNW, count++, (LPARAM)&lvc );
2597 msi_free( lvc.pszText );
2598 msi_free( num );
2602 static void msi_dialog_vcl_add_drives( msi_dialog *dialog, msi_control *control )
2604 ULARGE_INTEGER total, free;
2605 WCHAR size_text[MAX_PATH];
2606 LPWSTR drives, ptr;
2607 LVITEMW lvitem;
2608 DWORD size;
2609 int i = 0;
2611 size = GetLogicalDriveStringsW( 0, NULL );
2612 if ( !size ) return;
2614 drives = msi_alloc( (size + 1) * sizeof(WCHAR) );
2615 if ( !drives ) return;
2617 GetLogicalDriveStringsW( size, drives );
2619 ptr = drives;
2620 while (*ptr)
2622 lvitem.mask = LVIF_TEXT;
2623 lvitem.iItem = i;
2624 lvitem.iSubItem = 0;
2625 lvitem.pszText = ptr;
2626 lvitem.cchTextMax = lstrlenW(ptr) + 1;
2627 SendMessageW( control->hwnd, LVM_INSERTITEMW, 0, (LPARAM)&lvitem );
2629 GetDiskFreeSpaceExW(ptr, &free, &total, NULL);
2631 StrFormatByteSizeW(total.QuadPart, size_text, MAX_PATH);
2632 lvitem.iSubItem = 1;
2633 lvitem.pszText = size_text;
2634 lvitem.cchTextMax = lstrlenW(size_text) + 1;
2635 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
2637 StrFormatByteSizeW(free.QuadPart, size_text, MAX_PATH);
2638 lvitem.iSubItem = 2;
2639 lvitem.pszText = size_text;
2640 lvitem.cchTextMax = lstrlenW(size_text) + 1;
2641 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
2643 ptr += lstrlenW(ptr) + 1;
2644 i++;
2647 msi_free( drives );
2650 static UINT msi_dialog_volumecost_list( msi_dialog *dialog, MSIRECORD *rec )
2652 msi_control *control;
2653 DWORD style;
2655 style = LVS_REPORT | WS_VSCROLL | WS_HSCROLL | LVS_SHAREIMAGELISTS |
2656 LVS_AUTOARRANGE | LVS_SINGLESEL | WS_BORDER |
2657 WS_CHILD | WS_TABSTOP | WS_GROUP;
2658 control = msi_dialog_add_control( dialog, rec, WC_LISTVIEWW, style );
2659 if (!control)
2660 return ERROR_FUNCTION_FAILED;
2662 msi_dialog_vcl_add_columns( dialog, control, rec );
2663 msi_dialog_vcl_add_drives( dialog, control );
2665 return ERROR_SUCCESS;
2668 /******************** VolumeSelect Combo ***************************************/
2670 static UINT msi_dialog_volsel_handler( msi_dialog *dialog,
2671 msi_control *control, WPARAM param )
2673 WCHAR text[MAX_PATH];
2674 LPWSTR prop;
2675 BOOL indirect;
2676 int index;
2678 if (HIWORD(param) != CBN_SELCHANGE)
2679 return ERROR_SUCCESS;
2681 index = SendMessageW( control->hwnd, CB_GETCURSEL, 0, 0 );
2682 if ( index == CB_ERR )
2684 ERR("No ComboBox item selected!\n");
2685 return ERROR_FUNCTION_FAILED;
2688 SendMessageW( control->hwnd, CB_GETLBTEXT, index, (LPARAM)text );
2690 indirect = control->attributes & msidbControlAttributesIndirect;
2691 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2693 MSI_SetPropertyW( dialog->package, prop, text );
2695 msi_free( prop );
2696 return ERROR_SUCCESS;
2699 static void msi_dialog_vsc_add_drives( msi_dialog *dialog, msi_control *control )
2701 LPWSTR drives, ptr;
2702 DWORD size;
2704 size = GetLogicalDriveStringsW( 0, NULL );
2705 if ( !size ) return;
2707 drives = msi_alloc( (size + 1) * sizeof(WCHAR) );
2708 if ( !drives ) return;
2710 GetLogicalDriveStringsW( size, drives );
2712 ptr = drives;
2713 while (*ptr)
2715 SendMessageW( control->hwnd, CB_ADDSTRING, 0, (LPARAM)ptr );
2716 ptr += lstrlenW(ptr) + 1;
2719 msi_free( drives );
2722 static UINT msi_dialog_volumeselect_combo( msi_dialog *dialog, MSIRECORD *rec )
2724 msi_control *control;
2725 LPCWSTR prop;
2726 DWORD style;
2728 /* FIXME: CBS_OWNERDRAWFIXED */
2729 style = WS_CHILD | WS_VISIBLE | WS_GROUP | WS_TABSTOP |
2730 CBS_DROPDOWNLIST | CBS_SORT | CBS_HASSTRINGS |
2731 WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
2732 control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
2733 if (!control)
2734 return ERROR_FUNCTION_FAILED;
2736 control->attributes = MSI_RecordGetInteger( rec, 8 );
2737 control->handler = msi_dialog_volsel_handler;
2738 prop = MSI_RecordGetString( rec, 9 );
2739 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2741 msi_dialog_vsc_add_drives( dialog, control );
2743 return ERROR_SUCCESS;
2746 static const struct control_handler msi_dialog_handler[] =
2748 { szText, msi_dialog_text_control },
2749 { szPushButton, msi_dialog_button_control },
2750 { szLine, msi_dialog_line_control },
2751 { szBitmap, msi_dialog_bitmap_control },
2752 { szCheckBox, msi_dialog_checkbox_control },
2753 { szScrollableText, msi_dialog_scrolltext_control },
2754 { szComboBox, msi_dialog_combo_control },
2755 { szEdit, msi_dialog_edit_control },
2756 { szMaskedEdit, msi_dialog_maskedit_control },
2757 { szPathEdit, msi_dialog_pathedit_control },
2758 { szProgressBar, msi_dialog_progress_bar },
2759 { szRadioButtonGroup, msi_dialog_radiogroup_control },
2760 { szIcon, msi_dialog_icon_control },
2761 { szSelectionTree, msi_dialog_selection_tree },
2762 { szGroupBox, msi_dialog_group_box },
2763 { szListBox, msi_dialog_list_box },
2764 { szDirectoryCombo, msi_dialog_directory_combo },
2765 { szDirectoryList, msi_dialog_directory_list },
2766 { szVolumeCostList, msi_dialog_volumecost_list },
2767 { szVolumeSelectCombo, msi_dialog_volumeselect_combo },
2770 #define NUM_CONTROL_TYPES (sizeof msi_dialog_handler/sizeof msi_dialog_handler[0])
2772 static UINT msi_dialog_create_controls( MSIRECORD *rec, LPVOID param )
2774 msi_dialog *dialog = param;
2775 LPCWSTR control_type;
2776 UINT i;
2778 /* find and call the function that can create this type of control */
2779 control_type = MSI_RecordGetString( rec, 3 );
2780 for( i=0; i<NUM_CONTROL_TYPES; i++ )
2781 if (!strcmpiW( msi_dialog_handler[i].control_type, control_type ))
2782 break;
2783 if( i != NUM_CONTROL_TYPES )
2784 msi_dialog_handler[i].func( dialog, rec );
2785 else
2786 ERR("no handler for element type %s\n", debugstr_w(control_type));
2788 return ERROR_SUCCESS;
2791 static UINT msi_dialog_fill_controls( msi_dialog *dialog )
2793 static const WCHAR query[] = {
2794 'S','E','L','E','C','T',' ','*',' ',
2795 'F','R','O','M',' ','C','o','n','t','r','o','l',' ',
2796 'W','H','E','R','E',' ',
2797 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
2798 UINT r;
2799 MSIQUERY *view = NULL;
2800 MSIPACKAGE *package = dialog->package;
2802 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
2804 /* query the Control table for all the elements of the control */
2805 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
2806 if( r != ERROR_SUCCESS )
2808 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
2809 return ERROR_INVALID_PARAMETER;
2812 r = MSI_IterateRecords( view, 0, msi_dialog_create_controls, dialog );
2813 msiobj_release( &view->hdr );
2815 return r;
2818 static UINT msi_dialog_set_control_condition( MSIRECORD *rec, LPVOID param )
2820 static const WCHAR szHide[] = { 'H','i','d','e',0 };
2821 static const WCHAR szShow[] = { 'S','h','o','w',0 };
2822 static const WCHAR szDisable[] = { 'D','i','s','a','b','l','e',0 };
2823 static const WCHAR szEnable[] = { 'E','n','a','b','l','e',0 };
2824 static const WCHAR szDefault[] = { 'D','e','f','a','u','l','t',0 };
2825 msi_dialog *dialog = param;
2826 msi_control *control;
2827 LPCWSTR name, action, condition;
2828 UINT r;
2830 name = MSI_RecordGetString( rec, 2 );
2831 action = MSI_RecordGetString( rec, 3 );
2832 condition = MSI_RecordGetString( rec, 4 );
2833 r = MSI_EvaluateConditionW( dialog->package, condition );
2834 control = msi_dialog_find_control( dialog, name );
2835 if( r == MSICONDITION_TRUE && control )
2837 TRACE("%s control %s\n", debugstr_w(action), debugstr_w(name));
2839 /* FIXME: case sensitive? */
2840 if(!lstrcmpW(action, szHide))
2841 ShowWindow(control->hwnd, SW_HIDE);
2842 else if(!strcmpW(action, szShow))
2843 ShowWindow(control->hwnd, SW_SHOW);
2844 else if(!strcmpW(action, szDisable))
2845 EnableWindow(control->hwnd, FALSE);
2846 else if(!strcmpW(action, szEnable))
2847 EnableWindow(control->hwnd, TRUE);
2848 else if(!strcmpW(action, szDefault))
2849 SetFocus(control->hwnd);
2850 else
2851 FIXME("Unhandled action %s\n", debugstr_w(action));
2854 return ERROR_SUCCESS;
2857 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog )
2859 static const WCHAR query[] = {
2860 'S','E','L','E','C','T',' ','*',' ',
2861 'F','R','O','M',' ',
2862 'C','o','n','t','r','o','l','C','o','n','d','i','t','i','o','n',' ',
2863 'W','H','E','R','E',' ',
2864 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0
2866 UINT r;
2867 MSIQUERY *view = NULL;
2868 MSIPACKAGE *package = dialog->package;
2870 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
2872 /* query the Control table for all the elements of the control */
2873 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
2874 if( r != ERROR_SUCCESS )
2875 return ERROR_SUCCESS;
2877 r = MSI_IterateRecords( view, 0, msi_dialog_set_control_condition, dialog );
2878 msiobj_release( &view->hdr );
2880 return r;
2883 UINT msi_dialog_reset( msi_dialog *dialog )
2885 /* FIXME: should restore the original values of any properties we changed */
2886 return msi_dialog_evaluate_control_conditions( dialog );
2889 /* figure out the height of 10 point MS Sans Serif */
2890 static INT msi_dialog_get_sans_serif_height( HWND hwnd )
2892 static const WCHAR szSansSerif[] = {
2893 'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0 };
2894 LOGFONTW lf;
2895 TEXTMETRICW tm;
2896 BOOL r;
2897 LONG height = 0;
2898 HFONT hFont, hOldFont;
2899 HDC hdc;
2901 hdc = GetDC( hwnd );
2902 if (hdc)
2904 memset( &lf, 0, sizeof lf );
2905 lf.lfHeight = MulDiv(10, GetDeviceCaps(hdc, LOGPIXELSY), 72);
2906 strcpyW( lf.lfFaceName, szSansSerif );
2907 hFont = CreateFontIndirectW(&lf);
2908 if (hFont)
2910 hOldFont = SelectObject( hdc, hFont );
2911 r = GetTextMetricsW( hdc, &tm );
2912 if (r)
2913 height = tm.tmHeight;
2914 SelectObject( hdc, hOldFont );
2915 DeleteObject( hFont );
2917 ReleaseDC( hwnd, hdc );
2919 return height;
2922 /* fetch the associated record from the Dialog table */
2923 static MSIRECORD *msi_get_dialog_record( msi_dialog *dialog )
2925 static const WCHAR query[] = {
2926 'S','E','L','E','C','T',' ','*',' ',
2927 'F','R','O','M',' ','D','i','a','l','o','g',' ',
2928 'W','H','E','R','E',' ',
2929 '`','D','i','a','l','o','g','`',' ','=',' ','\'','%','s','\'',0};
2930 MSIPACKAGE *package = dialog->package;
2931 MSIRECORD *rec = NULL;
2933 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
2935 rec = MSI_QueryGetRecord( package->db, query, dialog->name );
2936 if( !rec )
2937 WARN("query failed for dialog %s\n", debugstr_w(dialog->name));
2939 return rec;
2942 static void msi_dialog_adjust_dialog_pos( msi_dialog *dialog, MSIRECORD *rec, LPRECT pos )
2944 static const WCHAR szScreenX[] = {'S','c','r','e','e','n','X',0};
2945 static const WCHAR szScreenY[] = {'S','c','r','e','e','n','Y',0};
2947 UINT xres, yres;
2948 POINT center;
2949 SIZE sz;
2950 LONG style;
2952 center.x = MSI_RecordGetInteger( rec, 2 );
2953 center.y = MSI_RecordGetInteger( rec, 3 );
2955 sz.cx = MSI_RecordGetInteger( rec, 4 );
2956 sz.cy = MSI_RecordGetInteger( rec, 5 );
2958 sz.cx = msi_dialog_scale_unit( dialog, sz.cx );
2959 sz.cy = msi_dialog_scale_unit( dialog, sz.cy );
2961 xres = msi_get_property_int( dialog->package, szScreenX, 0 );
2962 yres = msi_get_property_int( dialog->package, szScreenY, 0 );
2964 center.x = MulDiv( center.x, xres, 100 );
2965 center.y = MulDiv( center.y, yres, 100 );
2967 /* turn the client pos into the window rectangle */
2968 if (dialog->package->center_x && dialog->package->center_y)
2970 pos->left = dialog->package->center_x - sz.cx / 2.0;
2971 pos->right = pos->left + sz.cx;
2972 pos->top = dialog->package->center_y - sz.cy / 2.0;
2973 pos->bottom = pos->top + sz.cy;
2975 else
2977 pos->left = center.x - sz.cx/2;
2978 pos->right = pos->left + sz.cx;
2979 pos->top = center.y - sz.cy/2;
2980 pos->bottom = pos->top + sz.cy;
2982 /* save the center */
2983 dialog->package->center_x = center.x;
2984 dialog->package->center_y = center.y;
2987 dialog->size.cx = sz.cx;
2988 dialog->size.cy = sz.cy;
2990 TRACE("%u %u %u %u\n", pos->left, pos->top, pos->right, pos->bottom);
2992 style = GetWindowLongPtrW( dialog->hwnd, GWL_STYLE );
2993 AdjustWindowRect( pos, style, FALSE );
2996 static BOOL msi_control_set_next( msi_control *control, msi_control *next )
2998 return SetWindowPos( next->hwnd, control->hwnd, 0, 0, 0, 0,
2999 SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOREDRAW |
3000 SWP_NOREPOSITION | SWP_NOSENDCHANGING | SWP_NOSIZE );
3003 static UINT msi_dialog_set_tab_order( msi_dialog *dialog )
3005 msi_control *control, *tab_next;
3007 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
3009 tab_next = msi_dialog_find_control( dialog, control->tabnext );
3010 if( !tab_next )
3011 continue;
3012 msi_control_set_next( control, tab_next );
3015 return ERROR_SUCCESS;
3018 static void msi_dialog_set_first_control( msi_dialog* dialog, LPCWSTR name )
3020 msi_control *control;
3022 control = msi_dialog_find_control( dialog, name );
3023 if( control )
3024 dialog->hWndFocus = control->hwnd;
3025 else
3026 dialog->hWndFocus = NULL;
3029 static LRESULT msi_dialog_oncreate( HWND hwnd, LPCREATESTRUCTW cs )
3031 static const WCHAR df[] = {
3032 'D','e','f','a','u','l','t','U','I','F','o','n','t',0 };
3033 static const WCHAR dfv[] = {
3034 'M','S',' ','S','h','e','l','l',' ','D','l','g',0 };
3035 msi_dialog *dialog = (msi_dialog*) cs->lpCreateParams;
3036 MSIRECORD *rec = NULL;
3037 LPWSTR title = NULL;
3038 RECT pos;
3040 TRACE("%p %p\n", dialog, dialog->package);
3042 dialog->hwnd = hwnd;
3043 SetWindowLongPtrW( hwnd, GWLP_USERDATA, (LONG_PTR) dialog );
3045 rec = msi_get_dialog_record( dialog );
3046 if( !rec )
3048 TRACE("No record found for dialog %s\n", debugstr_w(dialog->name));
3049 return -1;
3052 dialog->scale = msi_dialog_get_sans_serif_height(dialog->hwnd);
3054 msi_dialog_adjust_dialog_pos( dialog, rec, &pos );
3056 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
3058 dialog->default_font = msi_dup_property( dialog->package, df );
3059 if (!dialog->default_font)
3061 dialog->default_font = strdupW(dfv);
3062 if (!dialog->default_font) return -1;
3065 title = msi_get_deformatted_field( dialog->package, rec, 7 );
3066 SetWindowTextW( hwnd, title );
3067 msi_free( title );
3069 SetWindowPos( hwnd, 0, pos.left, pos.top,
3070 pos.right - pos.left, pos.bottom - pos.top,
3071 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW );
3073 msi_dialog_build_font_list( dialog );
3074 msi_dialog_fill_controls( dialog );
3075 msi_dialog_evaluate_control_conditions( dialog );
3076 msi_dialog_set_tab_order( dialog );
3077 msi_dialog_set_first_control( dialog, MSI_RecordGetString( rec, 8 ) );
3078 msiobj_release( &rec->hdr );
3080 return 0;
3083 static UINT msi_dialog_send_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
3085 LPWSTR event_fmt = NULL, arg_fmt = NULL;
3087 TRACE("Sending control event %s %s\n", debugstr_w(event), debugstr_w(arg));
3089 deformat_string( dialog->package, event, &event_fmt );
3090 deformat_string( dialog->package, arg, &arg_fmt );
3092 dialog->event_handler( dialog->package, event_fmt, arg_fmt, dialog );
3094 msi_free( event_fmt );
3095 msi_free( arg_fmt );
3097 return ERROR_SUCCESS;
3100 static UINT msi_dialog_set_property( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
3102 static const WCHAR szNullArg[] = { '{','}',0 };
3103 LPWSTR p, prop, arg_fmt = NULL;
3104 UINT len;
3106 len = strlenW(event);
3107 prop = msi_alloc( len*sizeof(WCHAR));
3108 strcpyW( prop, &event[1] );
3109 p = strchrW( prop, ']' );
3110 if( p && (p[1] == 0 || p[1] == ' ') )
3112 *p = 0;
3113 if( strcmpW( szNullArg, arg ) )
3114 deformat_string( dialog->package, arg, &arg_fmt );
3115 MSI_SetPropertyW( dialog->package, prop, arg_fmt );
3116 msi_dialog_update_controls( dialog, prop );
3117 msi_free( arg_fmt );
3119 else
3120 ERR("Badly formatted property string - what happens?\n");
3121 msi_free( prop );
3122 return ERROR_SUCCESS;
3125 static UINT msi_dialog_control_event( MSIRECORD *rec, LPVOID param )
3127 msi_dialog *dialog = param;
3128 LPCWSTR condition, event, arg;
3129 UINT r;
3131 condition = MSI_RecordGetString( rec, 5 );
3132 r = MSI_EvaluateConditionW( dialog->package, condition );
3133 if( r == MSICONDITION_TRUE || r == MSICONDITION_NONE )
3135 event = MSI_RecordGetString( rec, 3 );
3136 arg = MSI_RecordGetString( rec, 4 );
3137 if( event[0] == '[' )
3138 msi_dialog_set_property( dialog, event, arg );
3139 else
3140 msi_dialog_send_event( dialog, event, arg );
3143 return ERROR_SUCCESS;
3146 struct rec_list
3148 struct list entry;
3149 MSIRECORD *rec;
3152 static UINT add_rec_to_list( MSIRECORD *rec, LPVOID param )
3154 struct rec_list *add_rec;
3155 struct list *records = (struct list *)param;
3157 msiobj_addref( &rec->hdr );
3159 add_rec = msi_alloc( sizeof( *add_rec ) );
3160 if (!add_rec)
3162 msiobj_release( &rec->hdr );
3163 return ERROR_OUTOFMEMORY;
3166 add_rec->rec = rec;
3167 list_add_tail( records, &add_rec->entry );
3168 return ERROR_SUCCESS;
3171 static inline void remove_rec_from_list( struct rec_list *rec_entry )
3173 msiobj_release( &rec_entry->rec->hdr );
3174 list_remove( &rec_entry->entry );
3175 msi_free( rec_entry );
3178 static UINT msi_dialog_button_handler( msi_dialog *dialog,
3179 msi_control *control, WPARAM param )
3181 static const WCHAR query[] = {
3182 'S','E','L','E','C','T',' ','*',' ',
3183 'F','R','O','M',' ','C','o','n','t','r','o','l','E','v','e','n','t',' ',
3184 'W','H','E','R','E',' ',
3185 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
3186 'A','N','D',' ',
3187 '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',' ',
3188 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','i','n','g','`',0
3190 MSIQUERY *view = NULL;
3191 struct rec_list *rec_entry, *next;
3192 struct list events;
3193 UINT r;
3195 if( HIWORD(param) != BN_CLICKED )
3196 return ERROR_SUCCESS;
3198 list_init( &events );
3200 r = MSI_OpenQuery( dialog->package->db, &view, query,
3201 dialog->name, control->name );
3202 if( r != ERROR_SUCCESS )
3204 ERR("query failed\n");
3205 return 0;
3208 r = MSI_IterateRecords( view, 0, add_rec_to_list, &events );
3209 msiobj_release( &view->hdr );
3210 if (r != ERROR_SUCCESS)
3211 goto done;
3213 /* handle all SetProperty events first */
3214 LIST_FOR_EACH_ENTRY_SAFE( rec_entry, next, &events, struct rec_list, entry )
3216 LPCWSTR event = MSI_RecordGetString( rec_entry->rec, 3 );
3218 if ( event[0] != '[' )
3219 continue;
3221 r = msi_dialog_control_event( rec_entry->rec, dialog );
3222 remove_rec_from_list( rec_entry );
3224 if ( r != ERROR_SUCCESS )
3225 goto done;
3228 /* handle all other events */
3229 LIST_FOR_EACH_ENTRY_SAFE( rec_entry, next, &events, struct rec_list, entry )
3231 r = msi_dialog_control_event( rec_entry->rec, dialog );
3232 remove_rec_from_list( rec_entry );
3234 if ( r != ERROR_SUCCESS )
3235 goto done;
3238 done:
3239 LIST_FOR_EACH_ENTRY_SAFE( rec_entry, next, &events, struct rec_list, entry )
3241 remove_rec_from_list( rec_entry );
3244 return r;
3247 static UINT msi_dialog_get_checkbox_state( msi_dialog *dialog,
3248 msi_control *control )
3250 WCHAR state[2] = { 0 };
3251 DWORD sz = 2;
3253 MSI_GetPropertyW( dialog->package, control->property, state, &sz );
3254 return state[0] ? 1 : 0;
3257 static void msi_dialog_set_checkbox_state( msi_dialog *dialog,
3258 msi_control *control, UINT state )
3260 static const WCHAR szState[] = { '1', 0 };
3261 LPCWSTR val;
3263 /* if uncheck then the property is set to NULL */
3264 if (!state)
3266 MSI_SetPropertyW( dialog->package, control->property, NULL );
3267 return;
3270 /* check for a custom state */
3271 if (control->value && control->value[0])
3272 val = control->value;
3273 else
3274 val = szState;
3276 MSI_SetPropertyW( dialog->package, control->property, val );
3279 static void msi_dialog_checkbox_sync_state( msi_dialog *dialog,
3280 msi_control *control )
3282 UINT state;
3284 state = msi_dialog_get_checkbox_state( dialog, control );
3285 SendMessageW( control->hwnd, BM_SETCHECK,
3286 state ? BST_CHECKED : BST_UNCHECKED, 0 );
3289 static UINT msi_dialog_checkbox_handler( msi_dialog *dialog,
3290 msi_control *control, WPARAM param )
3292 UINT state;
3294 if( HIWORD(param) != BN_CLICKED )
3295 return ERROR_SUCCESS;
3297 TRACE("clicked checkbox %s, set %s\n", debugstr_w(control->name),
3298 debugstr_w(control->property));
3300 state = msi_dialog_get_checkbox_state( dialog, control );
3301 state = state ? 0 : 1;
3302 msi_dialog_set_checkbox_state( dialog, control, state );
3303 msi_dialog_checkbox_sync_state( dialog, control );
3305 return msi_dialog_button_handler( dialog, control, param );
3308 static UINT msi_dialog_edit_handler( msi_dialog *dialog,
3309 msi_control *control, WPARAM param )
3311 LPWSTR buf;
3313 if( HIWORD(param) != EN_CHANGE )
3314 return ERROR_SUCCESS;
3316 TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
3317 debugstr_w(control->property));
3319 buf = msi_get_window_text( control->hwnd );
3320 MSI_SetPropertyW( dialog->package, control->property, buf );
3322 msi_free( buf );
3324 return ERROR_SUCCESS;
3327 static UINT msi_dialog_radiogroup_handler( msi_dialog *dialog,
3328 msi_control *control, WPARAM param )
3330 if( HIWORD(param) != BN_CLICKED )
3331 return ERROR_SUCCESS;
3333 TRACE("clicked radio button %s, set %s\n", debugstr_w(control->name),
3334 debugstr_w(control->property));
3336 MSI_SetPropertyW( dialog->package, control->property, control->name );
3338 return msi_dialog_button_handler( dialog, control, param );
3341 static LRESULT msi_dialog_oncommand( msi_dialog *dialog, WPARAM param, HWND hwnd )
3343 msi_control *control = NULL;
3345 TRACE("%p %p %08lx\n", dialog, hwnd, param);
3347 switch (param)
3349 case 1: /* enter */
3350 control = msi_dialog_find_control( dialog, dialog->control_default );
3351 break;
3352 case 2: /* escape */
3353 control = msi_dialog_find_control( dialog, dialog->control_cancel );
3354 break;
3355 default:
3356 control = msi_dialog_find_control_by_hwnd( dialog, hwnd );
3359 if( control )
3361 if( control->handler )
3363 control->handler( dialog, control, param );
3364 msi_dialog_evaluate_control_conditions( dialog );
3368 return 0;
3371 static LRESULT msi_dialog_onnotify( msi_dialog *dialog, LPARAM param )
3373 LPNMHDR nmhdr = (LPNMHDR) param;
3374 msi_control *control = msi_dialog_find_control_by_hwnd( dialog, nmhdr->hwndFrom );
3376 TRACE("%p %p\n", dialog, nmhdr->hwndFrom);
3378 if ( control && control->handler )
3379 control->handler( dialog, control, param );
3381 return 0;
3384 static void msi_dialog_setfocus( msi_dialog *dialog )
3386 HWND hwnd = dialog->hWndFocus;
3388 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, TRUE);
3389 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, FALSE);
3390 SetFocus( hwnd );
3391 dialog->hWndFocus = hwnd;
3394 static LRESULT WINAPI MSIDialog_WndProc( HWND hwnd, UINT msg,
3395 WPARAM wParam, LPARAM lParam )
3397 msi_dialog *dialog = (LPVOID) GetWindowLongPtrW( hwnd, GWLP_USERDATA );
3399 TRACE("0x%04x\n", msg);
3401 switch (msg)
3403 case WM_MOVE:
3404 dialog->package->center_x = LOWORD(lParam) + dialog->size.cx / 2.0;
3405 dialog->package->center_y = HIWORD(lParam) + dialog->size.cy / 2.0;
3406 break;
3408 case WM_CREATE:
3409 return msi_dialog_oncreate( hwnd, (LPCREATESTRUCTW)lParam );
3411 case WM_COMMAND:
3412 return msi_dialog_oncommand( dialog, wParam, (HWND)lParam );
3414 case WM_ACTIVATE:
3415 if( LOWORD(wParam) == WA_INACTIVE )
3416 dialog->hWndFocus = GetFocus();
3417 else
3418 msi_dialog_setfocus( dialog );
3419 return 0;
3421 case WM_SETFOCUS:
3422 msi_dialog_setfocus( dialog );
3423 return 0;
3425 /* bounce back to our subclassed static control */
3426 case WM_CTLCOLORSTATIC:
3427 return SendMessageW( (HWND) lParam, WM_CTLCOLORSTATIC, wParam, lParam );
3429 case WM_DESTROY:
3430 dialog->hwnd = NULL;
3431 return 0;
3432 case WM_NOTIFY:
3433 return msi_dialog_onnotify( dialog, lParam );
3435 return DefWindowProcW(hwnd, msg, wParam, lParam);
3438 static BOOL CALLBACK msi_radioground_child_enum( HWND hWnd, LPARAM lParam )
3440 EnableWindow( hWnd, lParam );
3441 return TRUE;
3444 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
3446 WNDPROC oldproc = (WNDPROC) GetPropW(hWnd, szButtonData);
3447 LRESULT r;
3449 TRACE("hWnd %p msg %04x wParam 0x%08lx lParam 0x%08lx\n", hWnd, msg, wParam, lParam);
3451 if (msg == WM_COMMAND) /* Forward notifications to dialog */
3452 SendMessageW(GetParent(hWnd), msg, wParam, lParam);
3454 r = CallWindowProcW(oldproc, hWnd, msg, wParam, lParam);
3456 /* make sure the radio buttons show as disabled if the parent is disabled */
3457 if (msg == WM_ENABLE)
3458 EnumChildWindows( hWnd, msi_radioground_child_enum, wParam );
3460 return r;
3463 static LRESULT WINAPI MSIHiddenWindowProc( HWND hwnd, UINT msg,
3464 WPARAM wParam, LPARAM lParam )
3466 msi_dialog *dialog = (msi_dialog*) lParam;
3468 TRACE("%d %p\n", msg, dialog);
3470 switch (msg)
3472 case WM_MSI_DIALOG_CREATE:
3473 return msi_dialog_run_message_loop( dialog );
3474 case WM_MSI_DIALOG_DESTROY:
3475 msi_dialog_destroy( dialog );
3476 return 0;
3478 return DefWindowProcW( hwnd, msg, wParam, lParam );
3481 /* functions that interface to other modules within MSI */
3483 msi_dialog *msi_dialog_create( MSIPACKAGE* package,
3484 LPCWSTR szDialogName, msi_dialog *parent,
3485 msi_dialog_event_handler event_handler )
3487 MSIRECORD *rec = NULL;
3488 msi_dialog *dialog;
3490 TRACE("%p %s\n", package, debugstr_w(szDialogName));
3492 if (!hMsiHiddenWindow)
3493 msi_dialog_register_class();
3495 /* allocate the structure for the dialog to use */
3496 dialog = msi_alloc_zero( sizeof *dialog + sizeof(WCHAR)*strlenW(szDialogName) );
3497 if( !dialog )
3498 return NULL;
3499 strcpyW( dialog->name, szDialogName );
3500 dialog->parent = parent;
3501 msiobj_addref( &package->hdr );
3502 dialog->package = package;
3503 dialog->event_handler = event_handler;
3504 dialog->finished = 0;
3505 list_init( &dialog->controls );
3507 /* verify that the dialog exists */
3508 rec = msi_get_dialog_record( dialog );
3509 if( !rec )
3511 msiobj_release( &package->hdr );
3512 msi_free( dialog );
3513 return NULL;
3515 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
3516 dialog->control_default = strdupW( MSI_RecordGetString( rec, 9 ) );
3517 dialog->control_cancel = strdupW( MSI_RecordGetString( rec, 10 ) );
3518 msiobj_release( &rec->hdr );
3520 return dialog;
3523 static void msi_process_pending_messages( HWND hdlg )
3525 MSG msg;
3527 while( PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ) )
3529 if( hdlg && IsDialogMessageW( hdlg, &msg ))
3530 continue;
3531 TranslateMessage( &msg );
3532 DispatchMessageW( &msg );
3536 void msi_dialog_end_dialog( msi_dialog *dialog )
3538 TRACE("%p\n", dialog);
3539 dialog->finished = 1;
3540 PostMessageW(dialog->hwnd, WM_NULL, 0, 0);
3543 void msi_dialog_check_messages( HANDLE handle )
3545 DWORD r;
3547 /* in threads other than the UI thread, block */
3548 if( uiThreadId != GetCurrentThreadId() )
3550 if( handle )
3551 WaitForSingleObject( handle, INFINITE );
3552 return;
3555 /* there's two choices for the UI thread */
3556 while (1)
3558 msi_process_pending_messages( NULL );
3560 if( !handle )
3561 break;
3564 * block here until somebody creates a new dialog or
3565 * the handle we're waiting on becomes ready
3567 r = MsgWaitForMultipleObjects( 1, &handle, 0, INFINITE, QS_ALLINPUT );
3568 if( r == WAIT_OBJECT_0 )
3569 break;
3573 UINT msi_dialog_run_message_loop( msi_dialog *dialog )
3575 DWORD style;
3576 HWND hwnd;
3578 if( uiThreadId != GetCurrentThreadId() )
3579 return SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_CREATE, 0, (LPARAM) dialog );
3581 /* create the dialog window, don't show it yet */
3582 style = WS_OVERLAPPED;
3583 if( dialog->attributes & msidbDialogAttributesVisible )
3584 style |= WS_VISIBLE;
3586 hwnd = CreateWindowW( szMsiDialogClass, dialog->name, style,
3587 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
3588 NULL, NULL, NULL, dialog );
3589 if( !hwnd )
3591 ERR("Failed to create dialog %s\n", debugstr_w( dialog->name ));
3592 return ERROR_FUNCTION_FAILED;
3595 ShowWindow( hwnd, SW_SHOW );
3596 /* UpdateWindow( hwnd ); - and causes the transparent static controls not to paint */
3598 if( dialog->attributes & msidbDialogAttributesModal )
3600 while( !dialog->finished )
3602 MsgWaitForMultipleObjects( 0, NULL, 0, INFINITE, QS_ALLINPUT );
3603 msi_process_pending_messages( dialog->hwnd );
3606 else
3607 return ERROR_IO_PENDING;
3609 return ERROR_SUCCESS;
3612 void msi_dialog_do_preview( msi_dialog *dialog )
3614 TRACE("\n");
3615 dialog->attributes |= msidbDialogAttributesVisible;
3616 dialog->attributes &= ~msidbDialogAttributesModal;
3617 msi_dialog_run_message_loop( dialog );
3620 void msi_dialog_destroy( msi_dialog *dialog )
3622 if( uiThreadId != GetCurrentThreadId() )
3624 SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_DESTROY, 0, (LPARAM) dialog );
3625 return;
3628 if( dialog->hwnd )
3629 ShowWindow( dialog->hwnd, SW_HIDE );
3631 if( dialog->hwnd )
3632 DestroyWindow( dialog->hwnd );
3634 /* unsubscribe events */
3635 ControlEvent_CleanupDialogSubscriptions(dialog->package, dialog->name);
3637 /* destroy the list of controls */
3638 while( !list_empty( &dialog->controls ) )
3640 msi_control *t;
3642 t = LIST_ENTRY( list_head( &dialog->controls ),
3643 msi_control, entry );
3644 msi_destroy_control( t );
3647 /* destroy the list of fonts */
3648 while( dialog->font_list )
3650 msi_font *t = dialog->font_list;
3651 dialog->font_list = t->next;
3652 DeleteObject( t->hfont );
3653 msi_free( t );
3655 msi_free( dialog->default_font );
3657 msi_free( dialog->control_default );
3658 msi_free( dialog->control_cancel );
3659 msiobj_release( &dialog->package->hdr );
3660 dialog->package = NULL;
3661 msi_free( dialog );
3664 BOOL msi_dialog_register_class( void )
3666 WNDCLASSW cls;
3668 ZeroMemory( &cls, sizeof cls );
3669 cls.lpfnWndProc = MSIDialog_WndProc;
3670 cls.hInstance = NULL;
3671 cls.hIcon = LoadIconW(0, (LPWSTR)IDI_APPLICATION);
3672 cls.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
3673 cls.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
3674 cls.lpszMenuName = NULL;
3675 cls.lpszClassName = szMsiDialogClass;
3677 if( !RegisterClassW( &cls ) )
3678 return FALSE;
3680 cls.lpfnWndProc = MSIHiddenWindowProc;
3681 cls.lpszClassName = szMsiHiddenWindow;
3683 if( !RegisterClassW( &cls ) )
3684 return FALSE;
3686 uiThreadId = GetCurrentThreadId();
3688 hMsiHiddenWindow = CreateWindowW( szMsiHiddenWindow, NULL, WS_OVERLAPPED,
3689 0, 0, 100, 100, NULL, NULL, NULL, NULL );
3690 if( !hMsiHiddenWindow )
3691 return FALSE;
3693 return TRUE;
3696 void msi_dialog_unregister_class( void )
3698 DestroyWindow( hMsiHiddenWindow );
3699 hMsiHiddenWindow = NULL;
3700 UnregisterClassW( szMsiDialogClass, NULL );
3701 UnregisterClassW( szMsiHiddenWindow, NULL );
3702 uiThreadId = 0;
3705 static UINT error_dialog_handler(MSIPACKAGE *package, LPCWSTR event,
3706 LPCWSTR argument, msi_dialog* dialog)
3708 static const WCHAR end_dialog[] = {'E','n','d','D','i','a','l','o','g',0};
3709 static const WCHAR error_abort[] = {'E','r','r','o','r','A','b','o','r','t',0};
3710 static const WCHAR error_cancel[] = {'E','r','r','o','r','C','a','n','c','e','l',0};
3711 static const WCHAR error_no[] = {'E','r','r','o','r','N','o',0};
3712 static const WCHAR result_prop[] = {
3713 'M','S','I','E','r','r','o','r','D','i','a','l','o','g','R','e','s','u','l','t',0
3716 if ( lstrcmpW( event, end_dialog ) )
3717 return ERROR_SUCCESS;
3719 if ( !lstrcmpW( argument, error_abort ) || !lstrcmpW( argument, error_cancel ) ||
3720 !lstrcmpW( argument, error_no ) )
3722 MSI_SetPropertyW( package, result_prop, error_abort );
3725 ControlEvent_CleanupSubscriptions(package);
3726 msi_dialog_end_dialog( dialog );
3728 return ERROR_SUCCESS;
3731 static UINT msi_error_dialog_set_error( MSIPACKAGE *package, LPWSTR error_dialog, LPWSTR error )
3733 MSIRECORD * row;
3735 static const WCHAR update[] =
3736 {'U','P','D','A','T','E',' ','`','C','o','n','t','r','o','l','`',' ',
3737 'S','E','T',' ','`','T','e','x','t','`',' ','=',' ','\'','%','s','\'',' ',
3738 'W','H','E','R','E', ' ','`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
3739 'A','N','D',' ','`','C','o','n','t','r','o','l','`',' ','=',' ',
3740 '\'','E','r','r','o','r','T','e','x','t','\'',0};
3742 row = MSI_QueryGetRecord( package->db, update, error, error_dialog );
3743 if (!row)
3744 return ERROR_FUNCTION_FAILED;
3746 msiobj_release(&row->hdr);
3747 return ERROR_SUCCESS;
3750 UINT msi_spawn_error_dialog( MSIPACKAGE *package, LPWSTR error_dialog, LPWSTR error )
3752 msi_dialog *dialog;
3753 WCHAR result[MAX_PATH];
3754 UINT r = ERROR_SUCCESS;
3755 DWORD size = MAX_PATH;
3756 int res;
3758 static const WCHAR szUILevel[] = {'U','I','L','e','v','e','l',0};
3759 static const WCHAR pn_prop[] = {'P','r','o','d','u','c','t','N','a','m','e',0};
3760 static const WCHAR title_fmt[] = {'%','s',' ','W','a','r','n','i','n','g',0};
3761 static const WCHAR error_abort[] = {'E','r','r','o','r','A','b','o','r','t',0};
3762 static const WCHAR result_prop[] = {
3763 'M','S','I','E','r','r','o','r','D','i','a','l','o','g','R','e','s','u','l','t',0
3766 if ( (msi_get_property_int(package, szUILevel, 0) & INSTALLUILEVEL_MASK) == INSTALLUILEVEL_NONE )
3767 return ERROR_SUCCESS;
3769 if ( !error_dialog )
3771 LPWSTR product_name = msi_dup_property( package, pn_prop );
3772 WCHAR title[MAX_PATH];
3774 sprintfW( title, title_fmt, product_name );
3775 res = MessageBoxW( NULL, error, title, MB_OKCANCEL | MB_ICONWARNING );
3777 msi_free( product_name );
3779 if ( res == IDOK )
3780 return ERROR_SUCCESS;
3781 else
3782 return ERROR_FUNCTION_FAILED;
3785 r = msi_error_dialog_set_error( package, error_dialog, error );
3786 if ( r != ERROR_SUCCESS )
3787 return r;
3789 dialog = msi_dialog_create( package, error_dialog, package->dialog,
3790 error_dialog_handler );
3791 if ( !dialog )
3792 return ERROR_FUNCTION_FAILED;
3794 dialog->finished = FALSE;
3795 r = msi_dialog_run_message_loop( dialog );
3796 if ( r != ERROR_SUCCESS )
3797 goto done;
3799 r = MSI_GetPropertyW( package, result_prop, result, &size );
3800 if ( r != ERROR_SUCCESS)
3801 r = ERROR_SUCCESS;
3803 if ( !lstrcmpW( result, error_abort ) )
3804 r = ERROR_FUNCTION_FAILED;
3806 done:
3807 msi_dialog_destroy( dialog );
3809 return r;