push e5b2529cdc55a2e67d855677d8651b2d2fa8d45f
[wine/hacks.git] / dlls / msi / dialog.c
blobadd78769660cac533671ae933dc84dd654367678
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 static UINT msi_dialog_edit_control( msi_dialog *dialog, MSIRECORD *rec )
1209 msi_control *control;
1210 LPCWSTR prop, text;
1211 LPWSTR val, begin, end;
1212 WCHAR num[10];
1213 DWORD limit;
1215 control = msi_dialog_add_control( dialog, rec, szEdit,
1216 WS_BORDER | WS_TABSTOP | ES_AUTOHSCROLL );
1217 control->handler = msi_dialog_edit_handler;
1219 text = MSI_RecordGetString( rec, 10 );
1220 if ( text )
1222 begin = strchrW( text, '{' );
1223 end = strchrW( text, '}' );
1225 if ( begin && end && end > begin )
1227 lstrcpynW( num, begin + 1, end - begin );
1228 limit = atolW( num );
1230 SendMessageW( control->hwnd, EM_SETLIMITTEXT, limit, 0 );
1234 prop = MSI_RecordGetString( rec, 9 );
1235 if( prop )
1236 control->property = strdupW( prop );
1238 val = msi_dup_property( dialog->package, control->property );
1239 SetWindowTextW( control->hwnd, val );
1240 msi_free( val );
1241 return ERROR_SUCCESS;
1244 /******************** Masked Edit ********************************************/
1246 #define MASK_MAX_GROUPS 20
1248 struct msi_mask_group
1250 UINT len;
1251 UINT ofs;
1252 WCHAR type;
1253 HWND hwnd;
1256 struct msi_maskedit_info
1258 msi_dialog *dialog;
1259 WNDPROC oldproc;
1260 HWND hwnd;
1261 LPWSTR prop;
1262 UINT num_chars;
1263 UINT num_groups;
1264 struct msi_mask_group group[MASK_MAX_GROUPS];
1267 static BOOL msi_mask_editable( WCHAR type )
1269 switch (type)
1271 case '%':
1272 case '#':
1273 case '&':
1274 case '`':
1275 case '?':
1276 case '^':
1277 return TRUE;
1279 return FALSE;
1282 static void msi_mask_control_change( struct msi_maskedit_info *info )
1284 LPWSTR val;
1285 UINT i, n, r;
1287 val = msi_alloc( (info->num_chars+1)*sizeof(WCHAR) );
1288 for( i=0, n=0; i<info->num_groups; i++ )
1290 if( (info->group[i].len + n) > info->num_chars )
1292 ERR("can't fit control %d text into template\n",i);
1293 break;
1295 if (!msi_mask_editable(info->group[i].type))
1297 for(r=0; r<info->group[i].len; r++)
1298 val[n+r] = info->group[i].type;
1299 val[n+r] = 0;
1301 else
1303 r = GetWindowTextW( info->group[i].hwnd, &val[n], info->group[i].len+1 );
1304 if( r != info->group[i].len )
1305 break;
1307 n += r;
1310 TRACE("%d/%d controls were good\n", i, info->num_groups);
1312 if( i == info->num_groups )
1314 TRACE("Set property %s to %s\n",
1315 debugstr_w(info->prop), debugstr_w(val) );
1316 CharUpperBuffW( val, info->num_chars );
1317 MSI_SetPropertyW( info->dialog->package, info->prop, val );
1318 msi_dialog_evaluate_control_conditions( info->dialog );
1320 msi_free( val );
1323 /* now move to the next control if necessary */
1324 static VOID msi_mask_next_control( struct msi_maskedit_info *info, HWND hWnd )
1326 HWND hWndNext;
1327 UINT len, i;
1329 for( i=0; i<info->num_groups; i++ )
1330 if( info->group[i].hwnd == hWnd )
1331 break;
1333 /* don't move from the last control */
1334 if( i >= (info->num_groups-1) )
1335 return;
1337 len = SendMessageW( hWnd, WM_GETTEXTLENGTH, 0, 0 );
1338 if( len < info->group[i].len )
1339 return;
1341 hWndNext = GetNextDlgTabItem( GetParent( hWnd ), hWnd, FALSE );
1342 SetFocus( hWndNext );
1345 static LRESULT WINAPI
1346 MSIMaskedEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1348 struct msi_maskedit_info *info;
1349 HRESULT r;
1351 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1353 info = GetPropW(hWnd, szButtonData);
1355 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1357 switch( msg )
1359 case WM_COMMAND:
1360 if (HIWORD(wParam) == EN_CHANGE)
1362 msi_mask_control_change( info );
1363 msi_mask_next_control( info, (HWND) lParam );
1365 break;
1366 case WM_NCDESTROY:
1367 msi_free( info->prop );
1368 msi_free( info );
1369 RemovePropW( hWnd, szButtonData );
1370 break;
1373 return r;
1376 /* fish the various bits of the property out and put them in the control */
1377 static void
1378 msi_maskedit_set_text( struct msi_maskedit_info *info, LPCWSTR text )
1380 LPCWSTR p;
1381 UINT i;
1383 p = text;
1384 for( i = 0; i < info->num_groups; i++ )
1386 if( info->group[i].len < lstrlenW( p ) )
1388 LPWSTR chunk = strdupW( p );
1389 chunk[ info->group[i].len ] = 0;
1390 SetWindowTextW( info->group[i].hwnd, chunk );
1391 msi_free( chunk );
1393 else
1395 SetWindowTextW( info->group[i].hwnd, p );
1396 break;
1398 p += info->group[i].len;
1402 static struct msi_maskedit_info * msi_dialog_parse_groups( LPCWSTR mask )
1404 struct msi_maskedit_info * info = NULL;
1405 int i = 0, n = 0, total = 0;
1406 LPCWSTR p;
1408 TRACE("masked control, template %s\n", debugstr_w(mask));
1410 if( !mask )
1411 return info;
1413 info = msi_alloc_zero( sizeof *info );
1414 if( !info )
1415 return info;
1417 p = strchrW(mask, '<');
1418 if( p )
1419 p++;
1420 else
1421 p = mask;
1423 for( i=0; i<MASK_MAX_GROUPS; i++ )
1425 /* stop at the end of the string */
1426 if( p[0] == 0 || p[0] == '>' )
1427 break;
1429 /* count the number of the same identifier */
1430 for( n=0; p[n] == p[0]; n++ )
1432 info->group[i].ofs = total;
1433 info->group[i].type = p[0];
1434 if( p[n] == '=' )
1436 n++;
1437 total++; /* an extra not part of the group */
1439 info->group[i].len = n;
1440 total += n;
1441 p += n;
1444 TRACE("%d characters in %d groups\n", total, i );
1445 if( i == MASK_MAX_GROUPS )
1446 ERR("too many groups in PIDTemplate %s\n", debugstr_w(mask));
1448 info->num_chars = total;
1449 info->num_groups = i;
1451 return info;
1454 static void
1455 msi_maskedit_create_children( struct msi_maskedit_info *info, LPCWSTR font )
1457 DWORD width, height, style, wx, ww;
1458 RECT rect;
1459 HWND hwnd;
1460 UINT i;
1462 style = WS_CHILD | WS_BORDER | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL;
1464 GetClientRect( info->hwnd, &rect );
1466 width = rect.right - rect.left;
1467 height = rect.bottom - rect.top;
1469 for( i = 0; i < info->num_groups; i++ )
1471 if (!msi_mask_editable( info->group[i].type ))
1472 continue;
1473 wx = (info->group[i].ofs * width) / info->num_chars;
1474 ww = (info->group[i].len * width) / info->num_chars;
1476 hwnd = CreateWindowW( szEdit, NULL, style, wx, 0, ww, height,
1477 info->hwnd, NULL, NULL, NULL );
1478 if( !hwnd )
1480 ERR("failed to create mask edit sub window\n");
1481 break;
1484 SendMessageW( hwnd, EM_LIMITTEXT, info->group[i].len, 0 );
1486 msi_dialog_set_font( info->dialog, hwnd,
1487 font?font:info->dialog->default_font );
1488 info->group[i].hwnd = hwnd;
1493 * office 2003 uses "73931<````=````=````=````=`````>@@@@@"
1494 * delphi 7 uses "<????-??????-??????-????>" and "<???-???>"
1495 * filemaker pro 7 uses "<^^^^=^^^^=^^^^=^^^^=^^^^=^^^^=^^^^^>"
1497 static UINT msi_dialog_maskedit_control( msi_dialog *dialog, MSIRECORD *rec )
1499 LPWSTR font_mask, val = NULL, font;
1500 struct msi_maskedit_info *info = NULL;
1501 UINT ret = ERROR_SUCCESS;
1502 msi_control *control;
1503 LPCWSTR prop, mask;
1505 TRACE("\n");
1507 font_mask = msi_get_deformatted_field( dialog->package, rec, 10 );
1508 font = msi_dialog_get_style( font_mask, &mask );
1509 if( !mask )
1511 ERR("mask template is empty\n");
1512 goto end;
1515 info = msi_dialog_parse_groups( mask );
1516 if( !info )
1518 ERR("template %s is invalid\n", debugstr_w(mask));
1519 goto end;
1522 info->dialog = dialog;
1524 control = msi_dialog_add_control( dialog, rec, szStatic,
1525 SS_OWNERDRAW | WS_GROUP | WS_VISIBLE );
1526 if( !control )
1528 ERR("Failed to create maskedit container\n");
1529 ret = ERROR_FUNCTION_FAILED;
1530 goto end;
1532 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
1534 info->hwnd = control->hwnd;
1536 /* subclass the static control */
1537 info->oldproc = (WNDPROC) SetWindowLongPtrW( info->hwnd, GWLP_WNDPROC,
1538 (LONG_PTR)MSIMaskedEdit_WndProc );
1539 SetPropW( control->hwnd, szButtonData, info );
1541 prop = MSI_RecordGetString( rec, 9 );
1542 if( prop )
1543 info->prop = strdupW( prop );
1545 msi_maskedit_create_children( info, font );
1547 if( prop )
1549 val = msi_dup_property( dialog->package, prop );
1550 if( val )
1552 msi_maskedit_set_text( info, val );
1553 msi_free( val );
1557 end:
1558 if( ret != ERROR_SUCCESS )
1559 msi_free( info );
1560 msi_free( font_mask );
1561 msi_free( font );
1562 return ret;
1565 /******************** Progress Bar *****************************************/
1567 static UINT msi_dialog_progress_bar( msi_dialog *dialog, MSIRECORD *rec )
1569 msi_dialog_add_control( dialog, rec, PROGRESS_CLASSW, WS_VISIBLE );
1570 return ERROR_SUCCESS;
1573 /******************** Path Edit ********************************************/
1575 struct msi_pathedit_info
1577 msi_dialog *dialog;
1578 msi_control *control;
1579 WNDPROC oldproc;
1582 static LPWSTR msi_get_window_text( HWND hwnd )
1584 UINT sz, r;
1585 LPWSTR buf;
1587 sz = 0x20;
1588 buf = msi_alloc( sz*sizeof(WCHAR) );
1589 while ( buf )
1591 r = GetWindowTextW( hwnd, buf, sz );
1592 if ( r < (sz - 1) )
1593 break;
1594 sz *= 2;
1595 buf = msi_realloc( buf, sz*sizeof(WCHAR) );
1598 return buf;
1601 static void msi_dialog_update_pathedit( msi_dialog *dialog, msi_control *control )
1603 LPWSTR prop, path;
1604 BOOL indirect;
1606 if (!control && !(control = msi_dialog_find_control_by_type( dialog, szPathEdit )))
1607 return;
1609 indirect = control->attributes & msidbControlAttributesIndirect;
1610 prop = msi_dialog_dup_property( dialog, control->property, indirect );
1611 path = msi_dialog_dup_property( dialog, prop, TRUE );
1613 SetWindowTextW( control->hwnd, path );
1614 SendMessageW( control->hwnd, EM_SETSEL, 0, -1 );
1616 msi_free( path );
1617 msi_free( prop );
1620 /* FIXME: test when this should fail */
1621 static BOOL msi_dialog_verify_path( LPWSTR path )
1623 if ( !lstrlenW( path ) )
1624 return FALSE;
1626 if ( PathIsRelativeW( path ) )
1627 return FALSE;
1629 return TRUE;
1632 /* returns TRUE if the path is valid, FALSE otherwise */
1633 static BOOL msi_dialog_onkillfocus( msi_dialog *dialog, msi_control *control )
1635 LPWSTR buf, prop;
1636 BOOL indirect;
1637 BOOL valid;
1639 indirect = control->attributes & msidbControlAttributesIndirect;
1640 prop = msi_dialog_dup_property( dialog, control->property, indirect );
1642 buf = msi_get_window_text( control->hwnd );
1644 if ( !msi_dialog_verify_path( buf ) )
1646 /* FIXME: display an error message box */
1647 ERR("Invalid path %s\n", debugstr_w( buf ));
1648 valid = FALSE;
1649 SetFocus( control->hwnd );
1651 else
1653 valid = TRUE;
1654 MSI_SetPropertyW( dialog->package, prop, buf );
1657 msi_dialog_update_pathedit( dialog, control );
1659 TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
1660 debugstr_w(prop));
1662 msi_free( buf );
1663 msi_free( prop );
1665 return valid;
1668 static LRESULT WINAPI MSIPathEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1670 struct msi_pathedit_info *info = GetPropW(hWnd, szButtonData);
1671 LRESULT r = 0;
1673 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1675 if ( msg == WM_KILLFOCUS )
1677 /* if the path is invalid, don't handle this message */
1678 if ( !msi_dialog_onkillfocus( info->dialog, info->control ) )
1679 return 0;
1682 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1684 if ( msg == WM_NCDESTROY )
1686 msi_free( info );
1687 RemovePropW( hWnd, szButtonData );
1690 return r;
1693 static UINT msi_dialog_pathedit_control( msi_dialog *dialog, MSIRECORD *rec )
1695 struct msi_pathedit_info *info;
1696 msi_control *control;
1697 LPCWSTR prop;
1699 info = msi_alloc( sizeof *info );
1700 if (!info)
1701 return ERROR_FUNCTION_FAILED;
1703 control = msi_dialog_add_control( dialog, rec, szEdit,
1704 WS_BORDER | WS_TABSTOP );
1705 control->attributes = MSI_RecordGetInteger( rec, 8 );
1706 prop = MSI_RecordGetString( rec, 9 );
1707 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
1709 info->dialog = dialog;
1710 info->control = control;
1711 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1712 (LONG_PTR)MSIPathEdit_WndProc );
1713 SetPropW( control->hwnd, szButtonData, info );
1715 msi_dialog_update_pathedit( dialog, control );
1717 return ERROR_SUCCESS;
1720 /* radio buttons are a bit different from normal controls */
1721 static UINT msi_dialog_create_radiobutton( MSIRECORD *rec, LPVOID param )
1723 radio_button_group_descr *group = (radio_button_group_descr *)param;
1724 msi_dialog *dialog = group->dialog;
1725 msi_control *control;
1726 LPCWSTR prop, text, name;
1727 DWORD style, attributes = group->attributes;
1729 style = WS_CHILD | BS_AUTORADIOBUTTON | BS_MULTILINE | WS_TABSTOP;
1730 name = MSI_RecordGetString( rec, 3 );
1731 text = MSI_RecordGetString( rec, 8 );
1732 if( attributes & msidbControlAttributesVisible )
1733 style |= WS_VISIBLE;
1734 if( ~attributes & msidbControlAttributesEnabled )
1735 style |= WS_DISABLED;
1737 control = msi_dialog_create_window( dialog, rec, 0, szButton, name, text,
1738 style, group->parent->hwnd );
1739 if (!control)
1740 return ERROR_FUNCTION_FAILED;
1741 control->handler = msi_dialog_radiogroup_handler;
1743 if (!lstrcmpW(control->name, group->propval))
1744 SendMessageW(control->hwnd, BM_SETCHECK, BST_CHECKED, 0);
1746 prop = MSI_RecordGetString( rec, 1 );
1747 if( prop )
1748 control->property = strdupW( prop );
1750 return ERROR_SUCCESS;
1753 static UINT msi_dialog_radiogroup_control( msi_dialog *dialog, MSIRECORD *rec )
1755 static const WCHAR query[] = {
1756 'S','E','L','E','C','T',' ','*',' ',
1757 'F','R','O','M',' ','R','a','d','i','o','B','u','t','t','o','n',' ',
1758 'W','H','E','R','E',' ',
1759 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
1760 UINT r;
1761 LPCWSTR prop;
1762 msi_control *control;
1763 MSIQUERY *view = NULL;
1764 radio_button_group_descr group;
1765 MSIPACKAGE *package = dialog->package;
1766 WNDPROC oldproc;
1767 DWORD attr, style = WS_GROUP;
1769 prop = MSI_RecordGetString( rec, 9 );
1771 TRACE("%p %p %s\n", dialog, rec, debugstr_w( prop ));
1773 attr = MSI_RecordGetInteger( rec, 8 );
1774 if (attr & msidbControlAttributesHasBorder)
1775 style |= BS_GROUPBOX;
1776 else
1777 style |= BS_OWNERDRAW;
1779 /* Create parent group box to hold radio buttons */
1780 control = msi_dialog_add_control( dialog, rec, szButton, style );
1781 if( !control )
1782 return ERROR_FUNCTION_FAILED;
1784 oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1785 (LONG_PTR)MSIRadioGroup_WndProc );
1786 SetPropW(control->hwnd, szButtonData, oldproc);
1787 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
1789 if( prop )
1790 control->property = strdupW( prop );
1792 /* query the Radio Button table for all control in this group */
1793 r = MSI_OpenQuery( package->db, &view, query, prop );
1794 if( r != ERROR_SUCCESS )
1796 ERR("query failed for dialog %s radio group %s\n",
1797 debugstr_w(dialog->name), debugstr_w(prop));
1798 return ERROR_INVALID_PARAMETER;
1801 group.dialog = dialog;
1802 group.parent = control;
1803 group.attributes = MSI_RecordGetInteger( rec, 8 );
1804 group.propval = msi_dup_property( dialog->package, control->property );
1806 r = MSI_IterateRecords( view, 0, msi_dialog_create_radiobutton, &group );
1807 msiobj_release( &view->hdr );
1808 msi_free( group.propval );
1810 return r;
1813 /******************** Selection Tree ***************************************/
1815 struct msi_selection_tree_info
1817 msi_dialog *dialog;
1818 HWND hwnd;
1819 WNDPROC oldproc;
1820 HTREEITEM selected;
1823 static void
1824 msi_seltree_sync_item_state( HWND hwnd, MSIFEATURE *feature, HTREEITEM hItem )
1826 TVITEMW tvi;
1827 DWORD index = feature->Action;
1829 TRACE("Feature %s -> %d %d %d\n", debugstr_w(feature->Title),
1830 feature->Installed, feature->Action, feature->ActionRequest);
1832 if (index == INSTALLSTATE_UNKNOWN)
1833 index = INSTALLSTATE_ABSENT;
1835 tvi.mask = TVIF_STATE;
1836 tvi.hItem = hItem;
1837 tvi.state = INDEXTOSTATEIMAGEMASK( index );
1838 tvi.stateMask = TVIS_STATEIMAGEMASK;
1840 SendMessageW( hwnd, TVM_SETITEMW, 0, (LPARAM) &tvi );
1843 static UINT
1844 msi_seltree_popup_menu( HWND hwnd, INT x, INT y )
1846 HMENU hMenu;
1847 INT r;
1849 /* create a menu to display */
1850 hMenu = CreatePopupMenu();
1852 /* FIXME: load strings from resources */
1853 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_LOCAL, "Install feature locally");
1854 AppendMenuA( hMenu, MF_GRAYED, 0x1000, "Install entire feature");
1855 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ADVERTISED, "Install on demand");
1856 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ABSENT, "Don't install");
1857 r = TrackPopupMenu( hMenu, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RETURNCMD,
1858 x, y, 0, hwnd, NULL );
1859 DestroyMenu( hMenu );
1860 return r;
1863 static MSIFEATURE *
1864 msi_seltree_feature_from_item( HWND hwnd, HTREEITEM hItem )
1866 TVITEMW tvi;
1868 /* get the feature from the item */
1869 memset( &tvi, 0, sizeof tvi );
1870 tvi.hItem = hItem;
1871 tvi.mask = TVIF_PARAM | TVIF_HANDLE;
1872 SendMessageW( hwnd, TVM_GETITEMW, 0, (LPARAM) &tvi );
1874 return (MSIFEATURE*) tvi.lParam;
1877 static LRESULT
1878 msi_seltree_menu( HWND hwnd, HTREEITEM hItem )
1880 struct msi_selection_tree_info *info;
1881 MSIFEATURE *feature;
1882 MSIPACKAGE *package;
1883 union {
1884 RECT rc;
1885 POINT pt[2];
1886 HTREEITEM hItem;
1887 } u;
1888 UINT r;
1890 info = GetPropW(hwnd, szButtonData);
1891 package = info->dialog->package;
1893 feature = msi_seltree_feature_from_item( hwnd, hItem );
1894 if (!feature)
1896 ERR("item %p feature was NULL\n", hItem);
1897 return 0;
1900 /* get the item's rectangle to put the menu just below it */
1901 u.hItem = hItem;
1902 SendMessageW( hwnd, TVM_GETITEMRECT, 0, (LPARAM) &u.rc );
1903 MapWindowPoints( hwnd, NULL, u.pt, 2 );
1905 r = msi_seltree_popup_menu( hwnd, u.rc.left, u.rc.top );
1907 switch (r)
1909 case INSTALLSTATE_LOCAL:
1910 case INSTALLSTATE_ADVERTISED:
1911 case INSTALLSTATE_ABSENT:
1912 msi_feature_set_state(package, feature, r);
1913 break;
1914 default:
1915 FIXME("select feature and all children\n");
1918 /* update */
1919 msi_seltree_sync_item_state( hwnd, feature, hItem );
1920 ACTION_UpdateComponentStates( package, feature->Feature );
1922 return 0;
1925 static MSIFEATURE *msi_seltree_get_selected_feature( msi_control *control )
1927 struct msi_selection_tree_info *info = GetPropW(control->hwnd, szButtonData);
1928 return msi_seltree_feature_from_item( control->hwnd, info->selected );
1931 static LRESULT WINAPI
1932 MSISelectionTree_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1934 struct msi_selection_tree_info *info;
1935 TVHITTESTINFO tvhti;
1936 HRESULT r;
1938 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1940 info = GetPropW(hWnd, szButtonData);
1942 switch( msg )
1944 case WM_LBUTTONDOWN:
1945 tvhti.pt.x = (short)LOWORD( lParam );
1946 tvhti.pt.y = (short)HIWORD( lParam );
1947 tvhti.flags = 0;
1948 tvhti.hItem = 0;
1949 r = CallWindowProcW(info->oldproc, hWnd, TVM_HITTEST, 0, (LPARAM) &tvhti );
1950 if (tvhti.flags & TVHT_ONITEMSTATEICON)
1951 return msi_seltree_menu( hWnd, tvhti.hItem );
1952 break;
1955 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1957 switch( msg )
1959 case WM_NCDESTROY:
1960 msi_free( info );
1961 RemovePropW( hWnd, szButtonData );
1962 break;
1964 return r;
1967 static void
1968 msi_seltree_add_child_features( MSIPACKAGE *package, HWND hwnd,
1969 LPCWSTR parent, HTREEITEM hParent )
1971 struct msi_selection_tree_info *info = GetPropW( hwnd, szButtonData );
1972 MSIFEATURE *feature;
1973 TVINSERTSTRUCTW tvis;
1974 HTREEITEM hitem, hfirst = NULL;
1976 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
1978 if ( lstrcmpW( parent, feature->Feature_Parent ) )
1979 continue;
1981 if ( !feature->Title )
1982 continue;
1984 if ( !feature->Display )
1985 continue;
1987 memset( &tvis, 0, sizeof tvis );
1988 tvis.hParent = hParent;
1989 tvis.hInsertAfter = TVI_LAST;
1990 tvis.u.item.mask = TVIF_TEXT | TVIF_PARAM;
1991 tvis.u.item.pszText = feature->Title;
1992 tvis.u.item.lParam = (LPARAM) feature;
1994 hitem = (HTREEITEM) SendMessageW( hwnd, TVM_INSERTITEMW, 0, (LPARAM) &tvis );
1995 if (!hitem)
1996 continue;
1998 if (!hfirst)
1999 hfirst = hitem;
2001 msi_seltree_sync_item_state( hwnd, feature, hitem );
2002 msi_seltree_add_child_features( package, hwnd,
2003 feature->Feature, hitem );
2005 /* the node is expanded if Display is odd */
2006 if ( feature->Display % 2 != 0 )
2007 SendMessageW( hwnd, TVM_EXPAND, TVE_EXPAND, (LPARAM) hitem );
2010 /* select the first item */
2011 SendMessageW( hwnd, TVM_SELECTITEM, TVGN_CARET | TVGN_DROPHILITE, (LPARAM) hfirst );
2012 info->selected = hfirst;
2015 static void msi_seltree_create_imagelist( HWND hwnd )
2017 const int bm_width = 32, bm_height = 16, bm_count = 3;
2018 const int bm_resource = 0x1001;
2019 HIMAGELIST himl;
2020 int i;
2021 HBITMAP hbmp;
2023 himl = ImageList_Create( bm_width, bm_height, FALSE, 4, 0 );
2024 if (!himl)
2026 ERR("failed to create image list\n");
2027 return;
2030 for (i=0; i<bm_count; i++)
2032 hbmp = LoadBitmapW( msi_hInstance, MAKEINTRESOURCEW(i+bm_resource) );
2033 if (!hbmp)
2035 ERR("failed to load bitmap %d\n", i);
2036 break;
2040 * Add a dummy bitmap at offset zero because the treeview
2041 * can't use it as a state mask (zero means no user state).
2043 if (!i)
2044 ImageList_Add( himl, hbmp, NULL );
2046 ImageList_Add( himl, hbmp, NULL );
2049 SendMessageW( hwnd, TVM_SETIMAGELIST, TVSIL_STATE, (LPARAM)himl );
2052 static UINT msi_dialog_seltree_handler( msi_dialog *dialog,
2053 msi_control *control, WPARAM param )
2055 struct msi_selection_tree_info *info = GetPropW( control->hwnd, szButtonData );
2056 LPNMTREEVIEWW tv = (LPNMTREEVIEWW)param;
2057 MSIRECORD *row, *rec;
2058 MSIFOLDER *folder;
2059 MSIFEATURE *feature;
2060 LPCWSTR dir, title = NULL;
2061 UINT r = ERROR_SUCCESS;
2063 static const WCHAR select[] = {
2064 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2065 '`','F','e','a','t','u','r','e','`',' ','W','H','E','R','E',' ',
2066 '`','T','i','t','l','e','`',' ','=',' ','\'','%','s','\'',0
2069 if (tv->hdr.code != TVN_SELCHANGINGW)
2070 return ERROR_SUCCESS;
2072 info->selected = tv->itemNew.hItem;
2074 if (!(tv->itemNew.mask & TVIF_TEXT))
2076 feature = msi_seltree_feature_from_item( control->hwnd, tv->itemNew.hItem );
2077 if (feature)
2078 title = feature->Title;
2080 else
2081 title = tv->itemNew.pszText;
2083 row = MSI_QueryGetRecord( dialog->package->db, select, title );
2084 if (!row)
2085 return ERROR_FUNCTION_FAILED;
2087 rec = MSI_CreateRecord( 1 );
2089 MSI_RecordSetStringW( rec, 1, MSI_RecordGetString( row, 4 ) );
2090 ControlEvent_FireSubscribedEvent( dialog->package, szSelectionDescription, rec );
2092 dir = MSI_RecordGetString( row, 7 );
2093 folder = get_loaded_folder( dialog->package, dir );
2094 if (!folder)
2096 r = ERROR_FUNCTION_FAILED;
2097 goto done;
2100 MSI_RecordSetStringW( rec, 1, folder->ResolvedTarget );
2101 ControlEvent_FireSubscribedEvent( dialog->package, szSelectionPath, rec );
2103 done:
2104 msiobj_release(&row->hdr);
2105 msiobj_release(&rec->hdr);
2107 return r;
2110 static UINT msi_dialog_selection_tree( msi_dialog *dialog, MSIRECORD *rec )
2112 msi_control *control;
2113 LPCWSTR prop;
2114 MSIPACKAGE *package = dialog->package;
2115 DWORD style;
2116 struct msi_selection_tree_info *info;
2118 info = msi_alloc( sizeof *info );
2119 if (!info)
2120 return ERROR_FUNCTION_FAILED;
2122 /* create the treeview control */
2123 style = TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT;
2124 style |= WS_GROUP | WS_VSCROLL;
2125 control = msi_dialog_add_control( dialog, rec, WC_TREEVIEWW, style );
2126 if (!control)
2128 msi_free(info);
2129 return ERROR_FUNCTION_FAILED;
2132 control->handler = msi_dialog_seltree_handler;
2133 control->attributes = MSI_RecordGetInteger( rec, 8 );
2134 prop = MSI_RecordGetString( rec, 9 );
2135 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2137 /* subclass */
2138 info->dialog = dialog;
2139 info->hwnd = control->hwnd;
2140 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2141 (LONG_PTR)MSISelectionTree_WndProc );
2142 SetPropW( control->hwnd, szButtonData, info );
2144 ControlEvent_SubscribeToEvent( dialog->package, dialog,
2145 szSelectionPath, control->name, szProperty );
2147 /* initialize it */
2148 msi_seltree_create_imagelist( control->hwnd );
2149 msi_seltree_add_child_features( package, control->hwnd, NULL, NULL );
2151 return ERROR_SUCCESS;
2154 /******************** Group Box ***************************************/
2156 static UINT msi_dialog_group_box( msi_dialog *dialog, MSIRECORD *rec )
2158 msi_control *control;
2159 DWORD style;
2161 style = BS_GROUPBOX | WS_CHILD | WS_GROUP;
2162 control = msi_dialog_add_control( dialog, rec, WC_BUTTONW, style );
2163 if (!control)
2164 return ERROR_FUNCTION_FAILED;
2166 return ERROR_SUCCESS;
2169 /******************** List Box ***************************************/
2171 struct msi_listbox_info
2173 msi_dialog *dialog;
2174 HWND hwnd;
2175 WNDPROC oldproc;
2176 DWORD num_items;
2177 DWORD addpos_items;
2178 LPWSTR *items;
2181 static LRESULT WINAPI MSIListBox_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2183 struct msi_listbox_info *info;
2184 LRESULT r;
2185 DWORD j;
2187 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
2189 info = GetPropW( hWnd, szButtonData );
2190 if (!info)
2191 return 0;
2193 r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
2195 switch( msg )
2197 case WM_NCDESTROY:
2198 for (j = 0; j < info->num_items; j++)
2199 msi_free( info->items[j] );
2200 msi_free( info->items );
2201 msi_free( info );
2202 RemovePropW( hWnd, szButtonData );
2203 break;
2206 return r;
2209 static UINT msi_listbox_add_item( MSIRECORD *rec, LPVOID param )
2211 struct msi_listbox_info *info = param;
2212 LPCWSTR value, text;
2213 int pos;
2215 value = MSI_RecordGetString( rec, 3 );
2216 text = MSI_RecordGetString( rec, 4 );
2218 info->items[info->addpos_items] = strdupW( value );
2220 pos = SendMessageW( info->hwnd, LB_ADDSTRING, 0, (LPARAM)text );
2221 SendMessageW( info->hwnd, LB_SETITEMDATA, pos, (LPARAM)info->items[info->addpos_items] );
2222 info->addpos_items++;
2223 return ERROR_SUCCESS;
2226 static UINT msi_listbox_add_items( struct msi_listbox_info *info, LPCWSTR property )
2228 UINT r;
2229 MSIQUERY *view = NULL;
2230 DWORD count;
2232 static const WCHAR query[] = {
2233 'S','E','L','E','C','T',' ','*',' ',
2234 'F','R','O','M',' ','`','L','i','s','t','B','o','x','`',' ',
2235 'W','H','E','R','E',' ',
2236 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',' ',
2237 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','`',0
2240 r = MSI_OpenQuery( info->dialog->package->db, &view, query, property );
2241 if ( r != ERROR_SUCCESS )
2242 return r;
2244 /* just get the number of records */
2245 r = MSI_IterateRecords( view, &count, NULL, NULL );
2247 info->num_items = count;
2248 info->items = msi_alloc( sizeof(*info->items) * count );
2250 r = MSI_IterateRecords( view, NULL, msi_listbox_add_item, info );
2251 msiobj_release( &view->hdr );
2253 return r;
2256 static UINT msi_dialog_listbox_handler( msi_dialog *dialog,
2257 msi_control *control, WPARAM param )
2259 struct msi_listbox_info *info;
2260 int index;
2261 LPCWSTR value;
2263 if( HIWORD(param) != LBN_SELCHANGE )
2264 return ERROR_SUCCESS;
2266 info = GetPropW( control->hwnd, szButtonData );
2267 index = SendMessageW( control->hwnd, LB_GETCURSEL, 0, 0 );
2268 value = (LPCWSTR) SendMessageW( control->hwnd, LB_GETITEMDATA, index, 0 );
2270 MSI_SetPropertyW( info->dialog->package,
2271 control->property, value );
2272 msi_dialog_evaluate_control_conditions( info->dialog );
2274 return ERROR_SUCCESS;
2277 static UINT msi_dialog_list_box( msi_dialog *dialog, MSIRECORD *rec )
2279 struct msi_listbox_info *info;
2280 msi_control *control;
2281 DWORD attributes, style;
2282 LPCWSTR prop;
2284 info = msi_alloc( sizeof *info );
2285 if (!info)
2286 return ERROR_FUNCTION_FAILED;
2288 style = WS_TABSTOP | WS_GROUP | WS_CHILD | LBS_NOTIFY | WS_VSCROLL | WS_BORDER;
2289 attributes = MSI_RecordGetInteger( rec, 8 );
2290 if (~attributes & msidbControlAttributesSorted)
2291 style |= LBS_SORT;
2293 control = msi_dialog_add_control( dialog, rec, WC_LISTBOXW, style );
2294 if (!control)
2296 msi_free(info);
2297 return ERROR_FUNCTION_FAILED;
2300 control->handler = msi_dialog_listbox_handler;
2302 prop = MSI_RecordGetString( rec, 9 );
2303 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2305 /* subclass */
2306 info->dialog = dialog;
2307 info->hwnd = control->hwnd;
2308 info->items = NULL;
2309 info->addpos_items = 0;
2310 info->oldproc = (WNDPROC)SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2311 (LONG_PTR)MSIListBox_WndProc );
2312 SetPropW( control->hwnd, szButtonData, info );
2314 if ( control->property )
2315 msi_listbox_add_items( info, control->property );
2317 return ERROR_SUCCESS;
2320 /******************** Directory Combo ***************************************/
2322 static void msi_dialog_update_directory_combo( msi_dialog *dialog, msi_control *control )
2324 LPWSTR prop, path;
2325 BOOL indirect;
2327 if (!control && !(control = msi_dialog_find_control_by_type( dialog, szDirectoryCombo )))
2328 return;
2330 indirect = control->attributes & msidbControlAttributesIndirect;
2331 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2332 path = msi_dialog_dup_property( dialog, prop, TRUE );
2334 PathStripPathW( path );
2335 PathRemoveBackslashW( path );
2337 SendMessageW( control->hwnd, CB_INSERTSTRING, 0, (LPARAM)path );
2338 SendMessageW( control->hwnd, CB_SETCURSEL, 0, 0 );
2340 msi_free( path );
2341 msi_free( prop );
2344 static UINT msi_dialog_directory_combo( msi_dialog *dialog, MSIRECORD *rec )
2346 msi_control *control;
2347 LPCWSTR prop;
2348 DWORD style;
2350 /* FIXME: use CBS_OWNERDRAWFIXED and add owner draw code */
2351 style = CBS_DROPDOWNLIST | CBS_HASSTRINGS | WS_CHILD |
2352 WS_GROUP | WS_TABSTOP | WS_VSCROLL;
2353 control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
2354 if (!control)
2355 return ERROR_FUNCTION_FAILED;
2357 control->attributes = MSI_RecordGetInteger( rec, 8 );
2358 prop = MSI_RecordGetString( rec, 9 );
2359 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2361 msi_dialog_update_directory_combo( dialog, control );
2363 return ERROR_SUCCESS;
2366 /******************** Directory List ***************************************/
2368 static void msi_dialog_update_directory_list( msi_dialog *dialog, msi_control *control )
2370 WCHAR dir_spec[MAX_PATH];
2371 WIN32_FIND_DATAW wfd;
2372 LPWSTR prop, path;
2373 BOOL indirect;
2374 LVITEMW item;
2375 HANDLE file;
2377 static const WCHAR asterisk[] = {'*',0};
2378 static const WCHAR dot[] = {'.',0};
2379 static const WCHAR dotdot[] = {'.','.',0};
2381 if (!control && !(control = msi_dialog_find_control_by_type( dialog, szDirectoryList )))
2382 return;
2384 /* clear the list-view */
2385 SendMessageW( control->hwnd, LVM_DELETEALLITEMS, 0, 0 );
2387 indirect = control->attributes & msidbControlAttributesIndirect;
2388 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2389 path = msi_dialog_dup_property( dialog, prop, TRUE );
2391 lstrcpyW( dir_spec, path );
2392 lstrcatW( dir_spec, asterisk );
2394 file = FindFirstFileW( dir_spec, &wfd );
2395 if ( file == INVALID_HANDLE_VALUE )
2396 return;
2400 if ( wfd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY )
2401 continue;
2403 if ( !lstrcmpW( wfd.cFileName, dot ) || !lstrcmpW( wfd.cFileName, dotdot ) )
2404 continue;
2406 item.mask = LVIF_TEXT;
2407 item.cchTextMax = MAX_PATH;
2408 item.iItem = 0;
2409 item.iSubItem = 0;
2410 item.pszText = wfd.cFileName;
2412 SendMessageW( control->hwnd, LVM_INSERTITEMW, 0, (LPARAM)&item );
2413 } while ( FindNextFileW( file, &wfd ) );
2415 msi_free( prop );
2416 msi_free( path );
2417 FindClose( file );
2420 UINT msi_dialog_directorylist_up( msi_dialog *dialog )
2422 msi_control *control;
2423 LPWSTR prop, path, ptr;
2424 BOOL indirect;
2426 control = msi_dialog_find_control_by_type( dialog, szDirectoryList );
2427 indirect = control->attributes & msidbControlAttributesIndirect;
2428 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2429 path = msi_dialog_dup_property( dialog, prop, TRUE );
2431 /* strip off the last directory */
2432 ptr = PathFindFileNameW( path );
2433 if (ptr != path) *(ptr - 1) = '\0';
2434 PathAddBackslashW( path );
2436 MSI_SetPropertyW( dialog->package, prop, path );
2438 msi_dialog_update_directory_list( dialog, NULL );
2439 msi_dialog_update_directory_combo( dialog, NULL );
2440 msi_dialog_update_pathedit( dialog, NULL );
2442 msi_free( path );
2443 msi_free( prop );
2445 return ERROR_SUCCESS;
2448 static UINT msi_dialog_dirlist_handler( msi_dialog *dialog,
2449 msi_control *control, WPARAM param )
2451 LPNMHDR nmhdr = (LPNMHDR)param;
2452 WCHAR new_path[MAX_PATH];
2453 WCHAR text[MAX_PATH];
2454 LPWSTR path, prop;
2455 BOOL indirect;
2456 LVITEMW item;
2457 int index;
2459 static const WCHAR backslash[] = {'\\',0};
2461 if (nmhdr->code != LVN_ITEMACTIVATE)
2462 return ERROR_SUCCESS;
2464 index = SendMessageW( control->hwnd, LVM_GETNEXTITEM, -1, LVNI_SELECTED );
2465 if ( index < 0 )
2467 ERR("No list-view item selected!\n");
2468 return ERROR_FUNCTION_FAILED;
2471 item.iSubItem = 0;
2472 item.pszText = text;
2473 item.cchTextMax = MAX_PATH;
2474 SendMessageW( control->hwnd, LVM_GETITEMTEXTW, index, (LPARAM)&item );
2476 indirect = control->attributes & msidbControlAttributesIndirect;
2477 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2478 path = msi_dialog_dup_property( dialog, prop, TRUE );
2480 lstrcpyW( new_path, path );
2481 lstrcatW( new_path, text );
2482 lstrcatW( new_path, backslash );
2484 MSI_SetPropertyW( dialog->package, prop, new_path );
2486 msi_dialog_update_directory_list( dialog, NULL );
2487 msi_dialog_update_directory_combo( dialog, NULL );
2488 msi_dialog_update_pathedit( dialog, NULL );
2490 msi_free( prop );
2491 msi_free( path );
2492 return ERROR_SUCCESS;
2495 static UINT msi_dialog_directory_list( msi_dialog *dialog, MSIRECORD *rec )
2497 msi_control *control;
2498 LPCWSTR prop;
2499 DWORD style;
2501 style = LVS_LIST | WS_VSCROLL | LVS_SHAREIMAGELISTS |
2502 LVS_AUTOARRANGE | LVS_SINGLESEL | WS_BORDER |
2503 LVS_SORTASCENDING | WS_CHILD | WS_GROUP | WS_TABSTOP;
2504 control = msi_dialog_add_control( dialog, rec, WC_LISTVIEWW, style );
2505 if (!control)
2506 return ERROR_FUNCTION_FAILED;
2508 control->attributes = MSI_RecordGetInteger( rec, 8 );
2509 control->handler = msi_dialog_dirlist_handler;
2510 prop = MSI_RecordGetString( rec, 9 );
2511 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2513 /* double click to activate an item in the list */
2514 SendMessageW( control->hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE,
2515 0, LVS_EX_TWOCLICKACTIVATE );
2517 msi_dialog_update_directory_list( dialog, control );
2519 return ERROR_SUCCESS;
2522 /******************** VolumeCost List ***************************************/
2524 static BOOL str_is_number( LPCWSTR str )
2526 int i;
2528 for (i = 0; i < lstrlenW( str ); i++)
2529 if (!isdigitW(str[i]))
2530 return FALSE;
2532 return TRUE;
2535 static const WCHAR column_keys[][80] =
2537 {'V','o','l','u','m','e','C','o','s','t','V','o','l','u','m','e',0},
2538 {'V','o','l','u','m','e','C','o','s','t','S','i','z','e',0},
2539 {'V','o','l','u','m','e','C','o','s','t','A','v','a','i','l','a','b','l','e',0},
2540 {'V','o','l','u','m','e','C','o','s','t','R','e','q','u','i','r','e','d',0},
2541 {'V','o','l','u','m','e','C','o','s','t','D','i','f','f','e','r','e','n','c','e',0}
2544 static void msi_dialog_vcl_add_columns( msi_dialog *dialog, msi_control *control, MSIRECORD *rec )
2546 LPCWSTR text = MSI_RecordGetString( rec, 10 );
2547 LPCWSTR begin = text, end;
2548 WCHAR *num;
2549 LVCOLUMNW lvc;
2550 DWORD count = 0;
2552 static const WCHAR zero[] = {'0',0};
2553 static const WCHAR negative[] = {'-',0};
2555 if (!text) return;
2557 while ((begin = strchrW( begin, '{' )) && count < 5)
2559 if (!(end = strchrW( begin, '}' )))
2560 return;
2562 num = msi_alloc( (end-begin+1)*sizeof(WCHAR) );
2563 if (!num)
2564 return;
2566 lstrcpynW( num, begin + 1, end - begin );
2567 begin += end - begin + 1;
2569 /* empty braces or '0' hides the column */
2570 if ( !num[0] || !lstrcmpW( num, zero ) )
2572 count++;
2573 msi_free( num );
2574 continue;
2577 /* the width must be a positive number
2578 * if a width is invalid, all remaining columns are hidden
2580 if ( !strncmpW( num, negative, 1 ) || !str_is_number( num ) ) {
2581 msi_free( num );
2582 return;
2585 ZeroMemory( &lvc, sizeof(lvc) );
2586 lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
2587 lvc.cx = atolW( num );
2588 lvc.pszText = msi_dialog_get_uitext( dialog, column_keys[count] );
2590 SendMessageW( control->hwnd, LVM_INSERTCOLUMNW, count++, (LPARAM)&lvc );
2591 msi_free( lvc.pszText );
2592 msi_free( num );
2596 static void msi_dialog_vcl_add_drives( msi_dialog *dialog, msi_control *control )
2598 ULARGE_INTEGER total, free;
2599 WCHAR size_text[MAX_PATH];
2600 LPWSTR drives, ptr;
2601 LVITEMW lvitem;
2602 DWORD size;
2603 int i = 0;
2605 size = GetLogicalDriveStringsW( 0, NULL );
2606 if ( !size ) return;
2608 drives = msi_alloc( (size + 1) * sizeof(WCHAR) );
2609 if ( !drives ) return;
2611 GetLogicalDriveStringsW( size, drives );
2613 ptr = drives;
2614 while (*ptr)
2616 lvitem.mask = LVIF_TEXT;
2617 lvitem.iItem = i;
2618 lvitem.iSubItem = 0;
2619 lvitem.pszText = ptr;
2620 lvitem.cchTextMax = lstrlenW(ptr) + 1;
2621 SendMessageW( control->hwnd, LVM_INSERTITEMW, 0, (LPARAM)&lvitem );
2623 GetDiskFreeSpaceExW(ptr, &free, &total, NULL);
2625 StrFormatByteSizeW(total.QuadPart, size_text, MAX_PATH);
2626 lvitem.iSubItem = 1;
2627 lvitem.pszText = size_text;
2628 lvitem.cchTextMax = lstrlenW(size_text) + 1;
2629 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
2631 StrFormatByteSizeW(free.QuadPart, size_text, MAX_PATH);
2632 lvitem.iSubItem = 2;
2633 lvitem.pszText = size_text;
2634 lvitem.cchTextMax = lstrlenW(size_text) + 1;
2635 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
2637 ptr += lstrlenW(ptr) + 1;
2638 i++;
2641 msi_free( drives );
2644 static UINT msi_dialog_volumecost_list( msi_dialog *dialog, MSIRECORD *rec )
2646 msi_control *control;
2647 DWORD style;
2649 style = LVS_REPORT | WS_VSCROLL | WS_HSCROLL | LVS_SHAREIMAGELISTS |
2650 LVS_AUTOARRANGE | LVS_SINGLESEL | WS_BORDER |
2651 WS_CHILD | WS_TABSTOP | WS_GROUP;
2652 control = msi_dialog_add_control( dialog, rec, WC_LISTVIEWW, style );
2653 if (!control)
2654 return ERROR_FUNCTION_FAILED;
2656 msi_dialog_vcl_add_columns( dialog, control, rec );
2657 msi_dialog_vcl_add_drives( dialog, control );
2659 return ERROR_SUCCESS;
2662 /******************** VolumeSelect Combo ***************************************/
2664 static UINT msi_dialog_volsel_handler( msi_dialog *dialog,
2665 msi_control *control, WPARAM param )
2667 WCHAR text[MAX_PATH];
2668 LPWSTR prop;
2669 BOOL indirect;
2670 int index;
2672 if (HIWORD(param) != CBN_SELCHANGE)
2673 return ERROR_SUCCESS;
2675 index = SendMessageW( control->hwnd, CB_GETCURSEL, 0, 0 );
2676 if ( index == CB_ERR )
2678 ERR("No ComboBox item selected!\n");
2679 return ERROR_FUNCTION_FAILED;
2682 SendMessageW( control->hwnd, CB_GETLBTEXT, index, (LPARAM)text );
2684 indirect = control->attributes & msidbControlAttributesIndirect;
2685 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2687 MSI_SetPropertyW( dialog->package, prop, text );
2689 msi_free( prop );
2690 return ERROR_SUCCESS;
2693 static void msi_dialog_vsc_add_drives( msi_dialog *dialog, msi_control *control )
2695 LPWSTR drives, ptr;
2696 DWORD size;
2698 size = GetLogicalDriveStringsW( 0, NULL );
2699 if ( !size ) return;
2701 drives = msi_alloc( (size + 1) * sizeof(WCHAR) );
2702 if ( !drives ) return;
2704 GetLogicalDriveStringsW( size, drives );
2706 ptr = drives;
2707 while (*ptr)
2709 SendMessageW( control->hwnd, CB_ADDSTRING, 0, (LPARAM)ptr );
2710 ptr += lstrlenW(ptr) + 1;
2713 msi_free( drives );
2716 static UINT msi_dialog_volumeselect_combo( msi_dialog *dialog, MSIRECORD *rec )
2718 msi_control *control;
2719 LPCWSTR prop;
2720 DWORD style;
2722 /* FIXME: CBS_OWNERDRAWFIXED */
2723 style = WS_CHILD | WS_VISIBLE | WS_GROUP | WS_TABSTOP |
2724 CBS_DROPDOWNLIST | CBS_SORT | CBS_HASSTRINGS |
2725 WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
2726 control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
2727 if (!control)
2728 return ERROR_FUNCTION_FAILED;
2730 control->attributes = MSI_RecordGetInteger( rec, 8 );
2731 control->handler = msi_dialog_volsel_handler;
2732 prop = MSI_RecordGetString( rec, 9 );
2733 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2735 msi_dialog_vsc_add_drives( dialog, control );
2737 return ERROR_SUCCESS;
2740 static const struct control_handler msi_dialog_handler[] =
2742 { szText, msi_dialog_text_control },
2743 { szPushButton, msi_dialog_button_control },
2744 { szLine, msi_dialog_line_control },
2745 { szBitmap, msi_dialog_bitmap_control },
2746 { szCheckBox, msi_dialog_checkbox_control },
2747 { szScrollableText, msi_dialog_scrolltext_control },
2748 { szComboBox, msi_dialog_combo_control },
2749 { szEdit, msi_dialog_edit_control },
2750 { szMaskedEdit, msi_dialog_maskedit_control },
2751 { szPathEdit, msi_dialog_pathedit_control },
2752 { szProgressBar, msi_dialog_progress_bar },
2753 { szRadioButtonGroup, msi_dialog_radiogroup_control },
2754 { szIcon, msi_dialog_icon_control },
2755 { szSelectionTree, msi_dialog_selection_tree },
2756 { szGroupBox, msi_dialog_group_box },
2757 { szListBox, msi_dialog_list_box },
2758 { szDirectoryCombo, msi_dialog_directory_combo },
2759 { szDirectoryList, msi_dialog_directory_list },
2760 { szVolumeCostList, msi_dialog_volumecost_list },
2761 { szVolumeSelectCombo, msi_dialog_volumeselect_combo },
2764 #define NUM_CONTROL_TYPES (sizeof msi_dialog_handler/sizeof msi_dialog_handler[0])
2766 static UINT msi_dialog_create_controls( MSIRECORD *rec, LPVOID param )
2768 msi_dialog *dialog = param;
2769 LPCWSTR control_type;
2770 UINT i;
2772 /* find and call the function that can create this type of control */
2773 control_type = MSI_RecordGetString( rec, 3 );
2774 for( i=0; i<NUM_CONTROL_TYPES; i++ )
2775 if (!strcmpiW( msi_dialog_handler[i].control_type, control_type ))
2776 break;
2777 if( i != NUM_CONTROL_TYPES )
2778 msi_dialog_handler[i].func( dialog, rec );
2779 else
2780 ERR("no handler for element type %s\n", debugstr_w(control_type));
2782 return ERROR_SUCCESS;
2785 static UINT msi_dialog_fill_controls( msi_dialog *dialog )
2787 static const WCHAR query[] = {
2788 'S','E','L','E','C','T',' ','*',' ',
2789 'F','R','O','M',' ','C','o','n','t','r','o','l',' ',
2790 'W','H','E','R','E',' ',
2791 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
2792 UINT r;
2793 MSIQUERY *view = NULL;
2794 MSIPACKAGE *package = dialog->package;
2796 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
2798 /* query the Control table for all the elements of the control */
2799 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
2800 if( r != ERROR_SUCCESS )
2802 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
2803 return ERROR_INVALID_PARAMETER;
2806 r = MSI_IterateRecords( view, 0, msi_dialog_create_controls, dialog );
2807 msiobj_release( &view->hdr );
2809 return r;
2812 static UINT msi_dialog_set_control_condition( MSIRECORD *rec, LPVOID param )
2814 static const WCHAR szHide[] = { 'H','i','d','e',0 };
2815 static const WCHAR szShow[] = { 'S','h','o','w',0 };
2816 static const WCHAR szDisable[] = { 'D','i','s','a','b','l','e',0 };
2817 static const WCHAR szEnable[] = { 'E','n','a','b','l','e',0 };
2818 static const WCHAR szDefault[] = { 'D','e','f','a','u','l','t',0 };
2819 msi_dialog *dialog = param;
2820 msi_control *control;
2821 LPCWSTR name, action, condition;
2822 UINT r;
2824 name = MSI_RecordGetString( rec, 2 );
2825 action = MSI_RecordGetString( rec, 3 );
2826 condition = MSI_RecordGetString( rec, 4 );
2827 r = MSI_EvaluateConditionW( dialog->package, condition );
2828 control = msi_dialog_find_control( dialog, name );
2829 if( r == MSICONDITION_TRUE && control )
2831 TRACE("%s control %s\n", debugstr_w(action), debugstr_w(name));
2833 /* FIXME: case sensitive? */
2834 if(!lstrcmpW(action, szHide))
2835 ShowWindow(control->hwnd, SW_HIDE);
2836 else if(!strcmpW(action, szShow))
2837 ShowWindow(control->hwnd, SW_SHOW);
2838 else if(!strcmpW(action, szDisable))
2839 EnableWindow(control->hwnd, FALSE);
2840 else if(!strcmpW(action, szEnable))
2841 EnableWindow(control->hwnd, TRUE);
2842 else if(!strcmpW(action, szDefault))
2843 SetFocus(control->hwnd);
2844 else
2845 FIXME("Unhandled action %s\n", debugstr_w(action));
2848 return ERROR_SUCCESS;
2851 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog )
2853 static const WCHAR query[] = {
2854 'S','E','L','E','C','T',' ','*',' ',
2855 'F','R','O','M',' ',
2856 'C','o','n','t','r','o','l','C','o','n','d','i','t','i','o','n',' ',
2857 'W','H','E','R','E',' ',
2858 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0
2860 UINT r;
2861 MSIQUERY *view = NULL;
2862 MSIPACKAGE *package = dialog->package;
2864 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
2866 /* query the Control table for all the elements of the control */
2867 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
2868 if( r != ERROR_SUCCESS )
2869 return ERROR_SUCCESS;
2871 r = MSI_IterateRecords( view, 0, msi_dialog_set_control_condition, dialog );
2872 msiobj_release( &view->hdr );
2874 return r;
2877 UINT msi_dialog_reset( msi_dialog *dialog )
2879 /* FIXME: should restore the original values of any properties we changed */
2880 return msi_dialog_evaluate_control_conditions( dialog );
2883 /* figure out the height of 10 point MS Sans Serif */
2884 static INT msi_dialog_get_sans_serif_height( HWND hwnd )
2886 static const WCHAR szSansSerif[] = {
2887 'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0 };
2888 LOGFONTW lf;
2889 TEXTMETRICW tm;
2890 BOOL r;
2891 LONG height = 0;
2892 HFONT hFont, hOldFont;
2893 HDC hdc;
2895 hdc = GetDC( hwnd );
2896 if (hdc)
2898 memset( &lf, 0, sizeof lf );
2899 lf.lfHeight = MulDiv(10, GetDeviceCaps(hdc, LOGPIXELSY), 72);
2900 strcpyW( lf.lfFaceName, szSansSerif );
2901 hFont = CreateFontIndirectW(&lf);
2902 if (hFont)
2904 hOldFont = SelectObject( hdc, hFont );
2905 r = GetTextMetricsW( hdc, &tm );
2906 if (r)
2907 height = tm.tmHeight;
2908 SelectObject( hdc, hOldFont );
2909 DeleteObject( hFont );
2911 ReleaseDC( hwnd, hdc );
2913 return height;
2916 /* fetch the associated record from the Dialog table */
2917 static MSIRECORD *msi_get_dialog_record( msi_dialog *dialog )
2919 static const WCHAR query[] = {
2920 'S','E','L','E','C','T',' ','*',' ',
2921 'F','R','O','M',' ','D','i','a','l','o','g',' ',
2922 'W','H','E','R','E',' ',
2923 '`','D','i','a','l','o','g','`',' ','=',' ','\'','%','s','\'',0};
2924 MSIPACKAGE *package = dialog->package;
2925 MSIRECORD *rec = NULL;
2927 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
2929 rec = MSI_QueryGetRecord( package->db, query, dialog->name );
2930 if( !rec )
2931 WARN("query failed for dialog %s\n", debugstr_w(dialog->name));
2933 return rec;
2936 static void msi_dialog_adjust_dialog_pos( msi_dialog *dialog, MSIRECORD *rec, LPRECT pos )
2938 static const WCHAR szScreenX[] = {'S','c','r','e','e','n','X',0};
2939 static const WCHAR szScreenY[] = {'S','c','r','e','e','n','Y',0};
2941 UINT xres, yres;
2942 POINT center;
2943 SIZE sz;
2944 LONG style;
2946 center.x = MSI_RecordGetInteger( rec, 2 );
2947 center.y = MSI_RecordGetInteger( rec, 3 );
2949 sz.cx = MSI_RecordGetInteger( rec, 4 );
2950 sz.cy = MSI_RecordGetInteger( rec, 5 );
2952 sz.cx = msi_dialog_scale_unit( dialog, sz.cx );
2953 sz.cy = msi_dialog_scale_unit( dialog, sz.cy );
2955 xres = msi_get_property_int( dialog->package, szScreenX, 0 );
2956 yres = msi_get_property_int( dialog->package, szScreenY, 0 );
2958 center.x = MulDiv( center.x, xres, 100 );
2959 center.y = MulDiv( center.y, yres, 100 );
2961 /* turn the client pos into the window rectangle */
2962 if (dialog->package->center_x && dialog->package->center_y)
2964 pos->left = dialog->package->center_x - sz.cx / 2.0;
2965 pos->right = pos->left + sz.cx;
2966 pos->top = dialog->package->center_y - sz.cy / 2.0;
2967 pos->bottom = pos->top + sz.cy;
2969 else
2971 pos->left = center.x - sz.cx/2;
2972 pos->right = pos->left + sz.cx;
2973 pos->top = center.y - sz.cy/2;
2974 pos->bottom = pos->top + sz.cy;
2976 /* save the center */
2977 dialog->package->center_x = center.x;
2978 dialog->package->center_y = center.y;
2981 dialog->size.cx = sz.cx;
2982 dialog->size.cy = sz.cy;
2984 TRACE("%u %u %u %u\n", pos->left, pos->top, pos->right, pos->bottom);
2986 style = GetWindowLongPtrW( dialog->hwnd, GWL_STYLE );
2987 AdjustWindowRect( pos, style, FALSE );
2990 static BOOL msi_control_set_next( msi_control *control, msi_control *next )
2992 return SetWindowPos( next->hwnd, control->hwnd, 0, 0, 0, 0,
2993 SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOREDRAW |
2994 SWP_NOREPOSITION | SWP_NOSENDCHANGING | SWP_NOSIZE );
2997 static UINT msi_dialog_set_tab_order( msi_dialog *dialog )
2999 msi_control *control, *tab_next;
3001 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
3003 tab_next = msi_dialog_find_control( dialog, control->tabnext );
3004 if( !tab_next )
3005 continue;
3006 msi_control_set_next( control, tab_next );
3009 return ERROR_SUCCESS;
3012 static void msi_dialog_set_first_control( msi_dialog* dialog, LPCWSTR name )
3014 msi_control *control;
3016 control = msi_dialog_find_control( dialog, name );
3017 if( control )
3018 dialog->hWndFocus = control->hwnd;
3019 else
3020 dialog->hWndFocus = NULL;
3023 static LRESULT msi_dialog_oncreate( HWND hwnd, LPCREATESTRUCTW cs )
3025 static const WCHAR df[] = {
3026 'D','e','f','a','u','l','t','U','I','F','o','n','t',0 };
3027 static const WCHAR dfv[] = {
3028 'M','S',' ','S','h','e','l','l',' ','D','l','g',0 };
3029 msi_dialog *dialog = (msi_dialog*) cs->lpCreateParams;
3030 MSIRECORD *rec = NULL;
3031 LPWSTR title = NULL;
3032 RECT pos;
3034 TRACE("%p %p\n", dialog, dialog->package);
3036 dialog->hwnd = hwnd;
3037 SetWindowLongPtrW( hwnd, GWLP_USERDATA, (LONG_PTR) dialog );
3039 rec = msi_get_dialog_record( dialog );
3040 if( !rec )
3042 TRACE("No record found for dialog %s\n", debugstr_w(dialog->name));
3043 return -1;
3046 dialog->scale = msi_dialog_get_sans_serif_height(dialog->hwnd);
3048 msi_dialog_adjust_dialog_pos( dialog, rec, &pos );
3050 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
3052 dialog->default_font = msi_dup_property( dialog->package, df );
3053 if (!dialog->default_font)
3055 dialog->default_font = strdupW(dfv);
3056 if (!dialog->default_font) return -1;
3059 title = msi_get_deformatted_field( dialog->package, rec, 7 );
3060 SetWindowTextW( hwnd, title );
3061 msi_free( title );
3063 SetWindowPos( hwnd, 0, pos.left, pos.top,
3064 pos.right - pos.left, pos.bottom - pos.top,
3065 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW );
3067 msi_dialog_build_font_list( dialog );
3068 msi_dialog_fill_controls( dialog );
3069 msi_dialog_evaluate_control_conditions( dialog );
3070 msi_dialog_set_tab_order( dialog );
3071 msi_dialog_set_first_control( dialog, MSI_RecordGetString( rec, 8 ) );
3072 msiobj_release( &rec->hdr );
3074 return 0;
3077 static UINT msi_dialog_send_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
3079 LPWSTR event_fmt = NULL, arg_fmt = NULL;
3081 TRACE("Sending control event %s %s\n", debugstr_w(event), debugstr_w(arg));
3083 deformat_string( dialog->package, event, &event_fmt );
3084 deformat_string( dialog->package, arg, &arg_fmt );
3086 dialog->event_handler( dialog->package, event_fmt, arg_fmt, dialog );
3088 msi_free( event_fmt );
3089 msi_free( arg_fmt );
3091 return ERROR_SUCCESS;
3094 static UINT msi_dialog_set_property( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
3096 static const WCHAR szNullArg[] = { '{','}',0 };
3097 LPWSTR p, prop, arg_fmt = NULL;
3098 UINT len;
3100 len = strlenW(event);
3101 prop = msi_alloc( len*sizeof(WCHAR));
3102 strcpyW( prop, &event[1] );
3103 p = strchrW( prop, ']' );
3104 if( p && (p[1] == 0 || p[1] == ' ') )
3106 *p = 0;
3107 if( strcmpW( szNullArg, arg ) )
3108 deformat_string( dialog->package, arg, &arg_fmt );
3109 MSI_SetPropertyW( dialog->package, prop, arg_fmt );
3110 msi_dialog_update_controls( dialog, prop );
3111 msi_free( arg_fmt );
3113 else
3114 ERR("Badly formatted property string - what happens?\n");
3115 msi_free( prop );
3116 return ERROR_SUCCESS;
3119 static UINT msi_dialog_control_event( MSIRECORD *rec, LPVOID param )
3121 msi_dialog *dialog = param;
3122 LPCWSTR condition, event, arg;
3123 UINT r;
3125 condition = MSI_RecordGetString( rec, 5 );
3126 r = MSI_EvaluateConditionW( dialog->package, condition );
3127 if( r == MSICONDITION_TRUE || r == MSICONDITION_NONE )
3129 event = MSI_RecordGetString( rec, 3 );
3130 arg = MSI_RecordGetString( rec, 4 );
3131 if( event[0] == '[' )
3132 msi_dialog_set_property( dialog, event, arg );
3133 else
3134 msi_dialog_send_event( dialog, event, arg );
3137 return ERROR_SUCCESS;
3140 struct rec_list
3142 struct list entry;
3143 MSIRECORD *rec;
3146 static UINT add_rec_to_list( MSIRECORD *rec, LPVOID param )
3148 struct rec_list *add_rec;
3149 struct list *records = (struct list *)param;
3151 msiobj_addref( &rec->hdr );
3153 add_rec = msi_alloc( sizeof( *add_rec ) );
3154 if (!add_rec)
3156 msiobj_release( &rec->hdr );
3157 return ERROR_OUTOFMEMORY;
3160 add_rec->rec = rec;
3161 list_add_tail( records, &add_rec->entry );
3162 return ERROR_SUCCESS;
3165 static inline void remove_rec_from_list( struct rec_list *rec_entry )
3167 msiobj_release( &rec_entry->rec->hdr );
3168 list_remove( &rec_entry->entry );
3169 msi_free( rec_entry );
3172 static UINT msi_dialog_button_handler( msi_dialog *dialog,
3173 msi_control *control, WPARAM param )
3175 static const WCHAR query[] = {
3176 'S','E','L','E','C','T',' ','*',' ',
3177 'F','R','O','M',' ','C','o','n','t','r','o','l','E','v','e','n','t',' ',
3178 'W','H','E','R','E',' ',
3179 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
3180 'A','N','D',' ',
3181 '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',' ',
3182 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','i','n','g','`',0
3184 MSIQUERY *view = NULL;
3185 struct rec_list *rec_entry, *next;
3186 struct list events;
3187 UINT r;
3189 if( HIWORD(param) != BN_CLICKED )
3190 return ERROR_SUCCESS;
3192 list_init( &events );
3194 r = MSI_OpenQuery( dialog->package->db, &view, query,
3195 dialog->name, control->name );
3196 if( r != ERROR_SUCCESS )
3198 ERR("query failed\n");
3199 return 0;
3202 r = MSI_IterateRecords( view, 0, add_rec_to_list, &events );
3203 msiobj_release( &view->hdr );
3204 if (r != ERROR_SUCCESS)
3205 goto done;
3207 /* handle all SetProperty events first */
3208 LIST_FOR_EACH_ENTRY_SAFE( rec_entry, next, &events, struct rec_list, entry )
3210 LPCWSTR event = MSI_RecordGetString( rec_entry->rec, 3 );
3212 if ( event[0] != '[' )
3213 continue;
3215 r = msi_dialog_control_event( rec_entry->rec, dialog );
3216 remove_rec_from_list( rec_entry );
3218 if ( r != ERROR_SUCCESS )
3219 goto done;
3222 /* handle all other events */
3223 LIST_FOR_EACH_ENTRY_SAFE( rec_entry, next, &events, struct rec_list, entry )
3225 r = msi_dialog_control_event( rec_entry->rec, dialog );
3226 remove_rec_from_list( rec_entry );
3228 if ( r != ERROR_SUCCESS )
3229 goto done;
3232 done:
3233 LIST_FOR_EACH_ENTRY_SAFE( rec_entry, next, &events, struct rec_list, entry )
3235 remove_rec_from_list( rec_entry );
3238 return r;
3241 static UINT msi_dialog_get_checkbox_state( msi_dialog *dialog,
3242 msi_control *control )
3244 WCHAR state[2] = { 0 };
3245 DWORD sz = 2;
3247 MSI_GetPropertyW( dialog->package, control->property, state, &sz );
3248 return state[0] ? 1 : 0;
3251 static void msi_dialog_set_checkbox_state( msi_dialog *dialog,
3252 msi_control *control, UINT state )
3254 static const WCHAR szState[] = { '1', 0 };
3255 LPCWSTR val;
3257 /* if uncheck then the property is set to NULL */
3258 if (!state)
3260 MSI_SetPropertyW( dialog->package, control->property, NULL );
3261 return;
3264 /* check for a custom state */
3265 if (control->value && control->value[0])
3266 val = control->value;
3267 else
3268 val = szState;
3270 MSI_SetPropertyW( dialog->package, control->property, val );
3273 static void msi_dialog_checkbox_sync_state( msi_dialog *dialog,
3274 msi_control *control )
3276 UINT state;
3278 state = msi_dialog_get_checkbox_state( dialog, control );
3279 SendMessageW( control->hwnd, BM_SETCHECK,
3280 state ? BST_CHECKED : BST_UNCHECKED, 0 );
3283 static UINT msi_dialog_checkbox_handler( msi_dialog *dialog,
3284 msi_control *control, WPARAM param )
3286 UINT state;
3288 if( HIWORD(param) != BN_CLICKED )
3289 return ERROR_SUCCESS;
3291 TRACE("clicked checkbox %s, set %s\n", debugstr_w(control->name),
3292 debugstr_w(control->property));
3294 state = msi_dialog_get_checkbox_state( dialog, control );
3295 state = state ? 0 : 1;
3296 msi_dialog_set_checkbox_state( dialog, control, state );
3297 msi_dialog_checkbox_sync_state( dialog, control );
3299 return msi_dialog_button_handler( dialog, control, param );
3302 static UINT msi_dialog_edit_handler( msi_dialog *dialog,
3303 msi_control *control, WPARAM param )
3305 LPWSTR buf;
3307 if( HIWORD(param) != EN_CHANGE )
3308 return ERROR_SUCCESS;
3310 TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
3311 debugstr_w(control->property));
3313 buf = msi_get_window_text( control->hwnd );
3314 MSI_SetPropertyW( dialog->package, control->property, buf );
3316 msi_free( buf );
3318 return ERROR_SUCCESS;
3321 static UINT msi_dialog_radiogroup_handler( msi_dialog *dialog,
3322 msi_control *control, WPARAM param )
3324 if( HIWORD(param) != BN_CLICKED )
3325 return ERROR_SUCCESS;
3327 TRACE("clicked radio button %s, set %s\n", debugstr_w(control->name),
3328 debugstr_w(control->property));
3330 MSI_SetPropertyW( dialog->package, control->property, control->name );
3332 return msi_dialog_button_handler( dialog, control, param );
3335 static LRESULT msi_dialog_oncommand( msi_dialog *dialog, WPARAM param, HWND hwnd )
3337 msi_control *control = NULL;
3339 TRACE("%p %p %08lx\n", dialog, hwnd, param);
3341 switch (param)
3343 case 1: /* enter */
3344 control = msi_dialog_find_control( dialog, dialog->control_default );
3345 break;
3346 case 2: /* escape */
3347 control = msi_dialog_find_control( dialog, dialog->control_cancel );
3348 break;
3349 default:
3350 control = msi_dialog_find_control_by_hwnd( dialog, hwnd );
3353 if( control )
3355 if( control->handler )
3357 control->handler( dialog, control, param );
3358 msi_dialog_evaluate_control_conditions( dialog );
3362 return 0;
3365 static LRESULT msi_dialog_onnotify( msi_dialog *dialog, LPARAM param )
3367 LPNMHDR nmhdr = (LPNMHDR) param;
3368 msi_control *control = msi_dialog_find_control_by_hwnd( dialog, nmhdr->hwndFrom );
3370 TRACE("%p %p\n", dialog, nmhdr->hwndFrom);
3372 if ( control && control->handler )
3373 control->handler( dialog, control, param );
3375 return 0;
3378 static void msi_dialog_setfocus( msi_dialog *dialog )
3380 HWND hwnd = dialog->hWndFocus;
3382 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, TRUE);
3383 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, FALSE);
3384 SetFocus( hwnd );
3385 dialog->hWndFocus = hwnd;
3388 static LRESULT WINAPI MSIDialog_WndProc( HWND hwnd, UINT msg,
3389 WPARAM wParam, LPARAM lParam )
3391 msi_dialog *dialog = (LPVOID) GetWindowLongPtrW( hwnd, GWLP_USERDATA );
3393 TRACE("0x%04x\n", msg);
3395 switch (msg)
3397 case WM_MOVE:
3398 dialog->package->center_x = LOWORD(lParam) + dialog->size.cx / 2.0;
3399 dialog->package->center_y = HIWORD(lParam) + dialog->size.cy / 2.0;
3400 break;
3402 case WM_CREATE:
3403 return msi_dialog_oncreate( hwnd, (LPCREATESTRUCTW)lParam );
3405 case WM_COMMAND:
3406 return msi_dialog_oncommand( dialog, wParam, (HWND)lParam );
3408 case WM_ACTIVATE:
3409 if( LOWORD(wParam) == WA_INACTIVE )
3410 dialog->hWndFocus = GetFocus();
3411 else
3412 msi_dialog_setfocus( dialog );
3413 return 0;
3415 case WM_SETFOCUS:
3416 msi_dialog_setfocus( dialog );
3417 return 0;
3419 /* bounce back to our subclassed static control */
3420 case WM_CTLCOLORSTATIC:
3421 return SendMessageW( (HWND) lParam, WM_CTLCOLORSTATIC, wParam, lParam );
3423 case WM_DESTROY:
3424 dialog->hwnd = NULL;
3425 return 0;
3426 case WM_NOTIFY:
3427 return msi_dialog_onnotify( dialog, lParam );
3429 return DefWindowProcW(hwnd, msg, wParam, lParam);
3432 static BOOL CALLBACK msi_radioground_child_enum( HWND hWnd, LPARAM lParam )
3434 EnableWindow( hWnd, lParam );
3435 return TRUE;
3438 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
3440 WNDPROC oldproc = (WNDPROC) GetPropW(hWnd, szButtonData);
3441 LRESULT r;
3443 TRACE("hWnd %p msg %04x wParam 0x%08lx lParam 0x%08lx\n", hWnd, msg, wParam, lParam);
3445 if (msg == WM_COMMAND) /* Forward notifications to dialog */
3446 SendMessageW(GetParent(hWnd), msg, wParam, lParam);
3448 r = CallWindowProcW(oldproc, hWnd, msg, wParam, lParam);
3450 /* make sure the radio buttons show as disabled if the parent is disabled */
3451 if (msg == WM_ENABLE)
3452 EnumChildWindows( hWnd, msi_radioground_child_enum, wParam );
3454 return r;
3457 static LRESULT WINAPI MSIHiddenWindowProc( HWND hwnd, UINT msg,
3458 WPARAM wParam, LPARAM lParam )
3460 msi_dialog *dialog = (msi_dialog*) lParam;
3462 TRACE("%d %p\n", msg, dialog);
3464 switch (msg)
3466 case WM_MSI_DIALOG_CREATE:
3467 return msi_dialog_run_message_loop( dialog );
3468 case WM_MSI_DIALOG_DESTROY:
3469 msi_dialog_destroy( dialog );
3470 return 0;
3472 return DefWindowProcW( hwnd, msg, wParam, lParam );
3475 /* functions that interface to other modules within MSI */
3477 msi_dialog *msi_dialog_create( MSIPACKAGE* package,
3478 LPCWSTR szDialogName, msi_dialog *parent,
3479 msi_dialog_event_handler event_handler )
3481 MSIRECORD *rec = NULL;
3482 msi_dialog *dialog;
3484 TRACE("%p %s\n", package, debugstr_w(szDialogName));
3486 if (!hMsiHiddenWindow)
3487 msi_dialog_register_class();
3489 /* allocate the structure for the dialog to use */
3490 dialog = msi_alloc_zero( sizeof *dialog + sizeof(WCHAR)*strlenW(szDialogName) );
3491 if( !dialog )
3492 return NULL;
3493 strcpyW( dialog->name, szDialogName );
3494 dialog->parent = parent;
3495 msiobj_addref( &package->hdr );
3496 dialog->package = package;
3497 dialog->event_handler = event_handler;
3498 dialog->finished = 0;
3499 list_init( &dialog->controls );
3501 /* verify that the dialog exists */
3502 rec = msi_get_dialog_record( dialog );
3503 if( !rec )
3505 msiobj_release( &package->hdr );
3506 msi_free( dialog );
3507 return NULL;
3509 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
3510 dialog->control_default = strdupW( MSI_RecordGetString( rec, 9 ) );
3511 dialog->control_cancel = strdupW( MSI_RecordGetString( rec, 10 ) );
3512 msiobj_release( &rec->hdr );
3514 return dialog;
3517 static void msi_process_pending_messages( HWND hdlg )
3519 MSG msg;
3521 while( PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ) )
3523 if( hdlg && IsDialogMessageW( hdlg, &msg ))
3524 continue;
3525 TranslateMessage( &msg );
3526 DispatchMessageW( &msg );
3530 void msi_dialog_end_dialog( msi_dialog *dialog )
3532 TRACE("%p\n", dialog);
3533 dialog->finished = 1;
3534 PostMessageW(dialog->hwnd, WM_NULL, 0, 0);
3537 void msi_dialog_check_messages( HANDLE handle )
3539 DWORD r;
3541 /* in threads other than the UI thread, block */
3542 if( uiThreadId != GetCurrentThreadId() )
3544 if( handle )
3545 WaitForSingleObject( handle, INFINITE );
3546 return;
3549 /* there's two choices for the UI thread */
3550 while (1)
3552 msi_process_pending_messages( NULL );
3554 if( !handle )
3555 break;
3558 * block here until somebody creates a new dialog or
3559 * the handle we're waiting on becomes ready
3561 r = MsgWaitForMultipleObjects( 1, &handle, 0, INFINITE, QS_ALLINPUT );
3562 if( r == WAIT_OBJECT_0 )
3563 break;
3567 UINT msi_dialog_run_message_loop( msi_dialog *dialog )
3569 DWORD style;
3570 HWND hwnd;
3572 if( uiThreadId != GetCurrentThreadId() )
3573 return SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_CREATE, 0, (LPARAM) dialog );
3575 /* create the dialog window, don't show it yet */
3576 style = WS_OVERLAPPED;
3577 if( dialog->attributes & msidbDialogAttributesVisible )
3578 style |= WS_VISIBLE;
3580 hwnd = CreateWindowW( szMsiDialogClass, dialog->name, style,
3581 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
3582 NULL, NULL, NULL, dialog );
3583 if( !hwnd )
3585 ERR("Failed to create dialog %s\n", debugstr_w( dialog->name ));
3586 return ERROR_FUNCTION_FAILED;
3589 ShowWindow( hwnd, SW_SHOW );
3590 /* UpdateWindow( hwnd ); - and causes the transparent static controls not to paint */
3592 if( dialog->attributes & msidbDialogAttributesModal )
3594 while( !dialog->finished )
3596 MsgWaitForMultipleObjects( 0, NULL, 0, INFINITE, QS_ALLINPUT );
3597 msi_process_pending_messages( dialog->hwnd );
3600 else
3601 return ERROR_IO_PENDING;
3603 return ERROR_SUCCESS;
3606 void msi_dialog_do_preview( msi_dialog *dialog )
3608 TRACE("\n");
3609 dialog->attributes |= msidbDialogAttributesVisible;
3610 dialog->attributes &= ~msidbDialogAttributesModal;
3611 msi_dialog_run_message_loop( dialog );
3614 void msi_dialog_destroy( msi_dialog *dialog )
3616 if( uiThreadId != GetCurrentThreadId() )
3618 SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_DESTROY, 0, (LPARAM) dialog );
3619 return;
3622 if( dialog->hwnd )
3623 ShowWindow( dialog->hwnd, SW_HIDE );
3625 if( dialog->hwnd )
3626 DestroyWindow( dialog->hwnd );
3628 /* unsubscribe events */
3629 ControlEvent_CleanupDialogSubscriptions(dialog->package, dialog->name);
3631 /* destroy the list of controls */
3632 while( !list_empty( &dialog->controls ) )
3634 msi_control *t;
3636 t = LIST_ENTRY( list_head( &dialog->controls ),
3637 msi_control, entry );
3638 msi_destroy_control( t );
3641 /* destroy the list of fonts */
3642 while( dialog->font_list )
3644 msi_font *t = dialog->font_list;
3645 dialog->font_list = t->next;
3646 DeleteObject( t->hfont );
3647 msi_free( t );
3649 msi_free( dialog->default_font );
3651 msi_free( dialog->control_default );
3652 msi_free( dialog->control_cancel );
3653 msiobj_release( &dialog->package->hdr );
3654 dialog->package = NULL;
3655 msi_free( dialog );
3658 BOOL msi_dialog_register_class( void )
3660 WNDCLASSW cls;
3662 ZeroMemory( &cls, sizeof cls );
3663 cls.lpfnWndProc = MSIDialog_WndProc;
3664 cls.hInstance = NULL;
3665 cls.hIcon = LoadIconW(0, (LPWSTR)IDI_APPLICATION);
3666 cls.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
3667 cls.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
3668 cls.lpszMenuName = NULL;
3669 cls.lpszClassName = szMsiDialogClass;
3671 if( !RegisterClassW( &cls ) )
3672 return FALSE;
3674 cls.lpfnWndProc = MSIHiddenWindowProc;
3675 cls.lpszClassName = szMsiHiddenWindow;
3677 if( !RegisterClassW( &cls ) )
3678 return FALSE;
3680 uiThreadId = GetCurrentThreadId();
3682 hMsiHiddenWindow = CreateWindowW( szMsiHiddenWindow, NULL, WS_OVERLAPPED,
3683 0, 0, 100, 100, NULL, NULL, NULL, NULL );
3684 if( !hMsiHiddenWindow )
3685 return FALSE;
3687 return TRUE;
3690 void msi_dialog_unregister_class( void )
3692 DestroyWindow( hMsiHiddenWindow );
3693 hMsiHiddenWindow = NULL;
3694 UnregisterClassW( szMsiDialogClass, NULL );
3695 UnregisterClassW( szMsiHiddenWindow, NULL );
3696 uiThreadId = 0;
3699 static UINT error_dialog_handler(MSIPACKAGE *package, LPCWSTR event,
3700 LPCWSTR argument, msi_dialog* dialog)
3702 static const WCHAR end_dialog[] = {'E','n','d','D','i','a','l','o','g',0};
3703 static const WCHAR error_abort[] = {'E','r','r','o','r','A','b','o','r','t',0};
3704 static const WCHAR error_cancel[] = {'E','r','r','o','r','C','a','n','c','e','l',0};
3705 static const WCHAR error_no[] = {'E','r','r','o','r','N','o',0};
3706 static const WCHAR result_prop[] = {
3707 'M','S','I','E','r','r','o','r','D','i','a','l','o','g','R','e','s','u','l','t',0
3710 if ( lstrcmpW( event, end_dialog ) )
3711 return ERROR_SUCCESS;
3713 if ( !lstrcmpW( argument, error_abort ) || !lstrcmpW( argument, error_cancel ) ||
3714 !lstrcmpW( argument, error_no ) )
3716 MSI_SetPropertyW( package, result_prop, error_abort );
3719 ControlEvent_CleanupSubscriptions(package);
3720 msi_dialog_end_dialog( dialog );
3722 return ERROR_SUCCESS;
3725 static UINT msi_error_dialog_set_error( MSIPACKAGE *package, LPWSTR error_dialog, LPWSTR error )
3727 MSIRECORD * row;
3729 static const WCHAR update[] =
3730 {'U','P','D','A','T','E',' ','`','C','o','n','t','r','o','l','`',' ',
3731 'S','E','T',' ','`','T','e','x','t','`',' ','=',' ','\'','%','s','\'',' ',
3732 'W','H','E','R','E', ' ','`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
3733 'A','N','D',' ','`','C','o','n','t','r','o','l','`',' ','=',' ',
3734 '\'','E','r','r','o','r','T','e','x','t','\'',0};
3736 row = MSI_QueryGetRecord( package->db, update, error, error_dialog );
3737 if (!row)
3738 return ERROR_FUNCTION_FAILED;
3740 msiobj_release(&row->hdr);
3741 return ERROR_SUCCESS;
3744 UINT msi_spawn_error_dialog( MSIPACKAGE *package, LPWSTR error_dialog, LPWSTR error )
3746 msi_dialog *dialog;
3747 WCHAR result[MAX_PATH];
3748 UINT r = ERROR_SUCCESS;
3749 DWORD size = MAX_PATH;
3750 int res;
3752 static const WCHAR szUILevel[] = {'U','I','L','e','v','e','l',0};
3753 static const WCHAR pn_prop[] = {'P','r','o','d','u','c','t','N','a','m','e',0};
3754 static const WCHAR title_fmt[] = {'%','s',' ','W','a','r','n','i','n','g',0};
3755 static const WCHAR error_abort[] = {'E','r','r','o','r','A','b','o','r','t',0};
3756 static const WCHAR result_prop[] = {
3757 'M','S','I','E','r','r','o','r','D','i','a','l','o','g','R','e','s','u','l','t',0
3760 if ( (msi_get_property_int(package, szUILevel, 0) & INSTALLUILEVEL_MASK) == INSTALLUILEVEL_NONE )
3761 return ERROR_SUCCESS;
3763 if ( !error_dialog )
3765 LPWSTR product_name = msi_dup_property( package, pn_prop );
3766 WCHAR title[MAX_PATH];
3768 sprintfW( title, title_fmt, product_name );
3769 res = MessageBoxW( NULL, error, title, MB_OKCANCEL | MB_ICONWARNING );
3771 msi_free( product_name );
3773 if ( res == IDOK )
3774 return ERROR_SUCCESS;
3775 else
3776 return ERROR_FUNCTION_FAILED;
3779 r = msi_error_dialog_set_error( package, error_dialog, error );
3780 if ( r != ERROR_SUCCESS )
3781 return r;
3783 dialog = msi_dialog_create( package, error_dialog, package->dialog,
3784 error_dialog_handler );
3785 if ( !dialog )
3786 return ERROR_FUNCTION_FAILED;
3788 dialog->finished = FALSE;
3789 r = msi_dialog_run_message_loop( dialog );
3790 if ( r != ERROR_SUCCESS )
3791 goto done;
3793 r = MSI_GetPropertyW( package, result_prop, result, &size );
3794 if ( r != ERROR_SUCCESS)
3795 r = ERROR_SUCCESS;
3797 if ( !lstrcmpW( result, error_abort ) )
3798 r = ERROR_FUNCTION_FAILED;
3800 done:
3801 msi_dialog_destroy( dialog );
3803 return r;