push a6cedc1a067cfdfb1ba44a8f4b98ea78ae3fe78f
[wine/hacks.git] / dlls / msi / dialog.c
blob495e033e9130cc0625f39b2b2f916de91cf1acc6
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 szSetProgress[] = {
139 'S','e','t','P','r','o','g','r','e','s','s',0 };
140 static const WCHAR szRadioButtonGroup[] = {
141 'R','a','d','i','o','B','u','t','t','o','n','G','r','o','u','p',0 };
142 static const WCHAR szIcon[] = { 'I','c','o','n',0 };
143 static const WCHAR szSelectionTree[] = {
144 'S','e','l','e','c','t','i','o','n','T','r','e','e',0 };
145 static const WCHAR szGroupBox[] = { 'G','r','o','u','p','B','o','x',0 };
146 static const WCHAR szListBox[] = { 'L','i','s','t','B','o','x',0 };
147 static const WCHAR szDirectoryCombo[] = { 'D','i','r','e','c','t','o','r','y','C','o','m','b','o',0 };
148 static const WCHAR szDirectoryList[] = { 'D','i','r','e','c','t','o','r','y','L','i','s','t',0 };
149 static const WCHAR szVolumeCostList[] = { 'V','o','l','u','m','e','C','o','s','t','L','i','s','t',0 };
150 static const WCHAR szVolumeSelectCombo[] = { 'V','o','l','u','m','e','S','e','l','e','c','t','C','o','m','b','o',0 };
151 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};
152 static const WCHAR szSelectionPath[] = {'S','e','l','e','c','t','i','o','n','P','a','t','h',0};
153 static const WCHAR szProperty[] = {'P','r','o','p','e','r','t','y',0};
155 static UINT msi_dialog_checkbox_handler( msi_dialog *, msi_control *, WPARAM );
156 static void msi_dialog_checkbox_sync_state( msi_dialog *, msi_control * );
157 static UINT msi_dialog_button_handler( msi_dialog *, msi_control *, WPARAM );
158 static UINT msi_dialog_edit_handler( msi_dialog *, msi_control *, WPARAM );
159 static UINT msi_dialog_radiogroup_handler( msi_dialog *, msi_control *, WPARAM );
160 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog );
161 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
162 static MSIFEATURE *msi_seltree_get_selected_feature( msi_control *control );
164 /* dialog sequencing */
166 #define WM_MSI_DIALOG_CREATE (WM_USER+0x100)
167 #define WM_MSI_DIALOG_DESTROY (WM_USER+0x101)
169 static DWORD uiThreadId;
170 static HWND hMsiHiddenWindow;
172 static INT msi_dialog_scale_unit( msi_dialog *dialog, INT val )
174 return (dialog->scale * val + 5) / 10;
177 static msi_control *msi_dialog_find_control( msi_dialog *dialog, LPCWSTR name )
179 msi_control *control;
181 if( !name )
182 return NULL;
183 if( !dialog->hwnd )
184 return NULL;
185 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
186 if( !strcmpW( control->name, name ) ) /* FIXME: case sensitive? */
187 return control;
188 return NULL;
191 static msi_control *msi_dialog_find_control_by_type( msi_dialog *dialog, LPCWSTR type )
193 msi_control *control;
195 if( !type )
196 return NULL;
197 if( !dialog->hwnd )
198 return NULL;
199 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
200 if( !strcmpW( control->type, type ) ) /* FIXME: case sensitive? */
201 return control;
202 return NULL;
205 static msi_control *msi_dialog_find_control_by_hwnd( msi_dialog *dialog, HWND hwnd )
207 msi_control *control;
209 if( !dialog->hwnd )
210 return NULL;
211 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
212 if( hwnd == control->hwnd )
213 return control;
214 return NULL;
217 static LPWSTR msi_get_deformatted_field( MSIPACKAGE *package, MSIRECORD *rec, int field )
219 LPCWSTR str = MSI_RecordGetString( rec, field );
220 LPWSTR ret = NULL;
222 if (str)
223 deformat_string( package, str, &ret );
224 return ret;
227 static LPWSTR msi_dialog_dup_property( msi_dialog *dialog, LPCWSTR property, BOOL indirect )
229 LPWSTR prop = NULL;
231 if (!property)
232 return NULL;
234 if (indirect)
235 prop = msi_dup_property( dialog->package, property );
237 if (!prop)
238 prop = strdupW( property );
240 return prop;
243 msi_dialog *msi_dialog_get_parent( msi_dialog *dialog )
245 return dialog->parent;
248 LPWSTR msi_dialog_get_name( msi_dialog *dialog )
250 return dialog->name;
254 * msi_dialog_get_style
256 * Extract the {\style} string from the front of the text to display and
257 * update the pointer. Only the last style in a list is applied.
259 static LPWSTR msi_dialog_get_style( LPCWSTR p, LPCWSTR *rest )
261 LPWSTR ret;
262 LPCWSTR q, i, first;
263 DWORD len;
265 q = NULL;
266 *rest = p;
267 if( !p )
268 return NULL;
270 while ((first = strchrW( p, '{' )) && (q = strchrW( first + 1, '}' )))
272 p = first + 1;
273 if( *p != '\\' && *p != '&' )
274 return NULL;
276 /* little bit of sanity checking to stop us getting confused with RTF */
277 for( i=++p; i<q; i++ )
278 if( *i == '}' || *i == '\\' )
279 return NULL;
282 if (!p || !q)
283 return NULL;
285 *rest = ++q;
286 len = q - p;
288 ret = msi_alloc( len*sizeof(WCHAR) );
289 if( !ret )
290 return ret;
291 memcpy( ret, p, len*sizeof(WCHAR) );
292 ret[len-1] = 0;
293 return ret;
296 static UINT msi_dialog_add_font( MSIRECORD *rec, LPVOID param )
298 msi_dialog *dialog = param;
299 msi_font *font;
300 LPCWSTR face, name;
301 LOGFONTW lf;
302 INT style;
303 HDC hdc;
305 /* create a font and add it to the list */
306 name = MSI_RecordGetString( rec, 1 );
307 font = msi_alloc( sizeof *font + strlenW( name )*sizeof (WCHAR) );
308 strcpyW( font->name, name );
309 font->next = dialog->font_list;
310 dialog->font_list = font;
312 font->color = MSI_RecordGetInteger( rec, 4 );
314 memset( &lf, 0, sizeof lf );
315 face = MSI_RecordGetString( rec, 2 );
316 lf.lfHeight = MSI_RecordGetInteger( rec, 3 );
317 style = MSI_RecordGetInteger( rec, 5 );
318 if( style & msidbTextStyleStyleBitsBold )
319 lf.lfWeight = FW_BOLD;
320 if( style & msidbTextStyleStyleBitsItalic )
321 lf.lfItalic = TRUE;
322 if( style & msidbTextStyleStyleBitsUnderline )
323 lf.lfUnderline = TRUE;
324 if( style & msidbTextStyleStyleBitsStrike )
325 lf.lfStrikeOut = TRUE;
326 lstrcpynW( lf.lfFaceName, face, LF_FACESIZE );
328 /* adjust the height */
329 hdc = GetDC( dialog->hwnd );
330 if (hdc)
332 lf.lfHeight = -MulDiv(lf.lfHeight, GetDeviceCaps(hdc, LOGPIXELSY), 72);
333 ReleaseDC( dialog->hwnd, hdc );
336 font->hfont = CreateFontIndirectW( &lf );
338 TRACE("Adding font style %s\n", debugstr_w(font->name) );
340 return ERROR_SUCCESS;
343 static msi_font *msi_dialog_find_font( msi_dialog *dialog, LPCWSTR name )
345 msi_font *font;
347 for( font = dialog->font_list; font; font = font->next )
348 if( !strcmpW( font->name, name ) ) /* FIXME: case sensitive? */
349 break;
351 return font;
354 static UINT msi_dialog_set_font( msi_dialog *dialog, HWND hwnd, LPCWSTR name )
356 msi_font *font;
358 font = msi_dialog_find_font( dialog, name );
359 if( font )
360 SendMessageW( hwnd, WM_SETFONT, (WPARAM) font->hfont, TRUE );
361 else
362 ERR("No font entry for %s\n", debugstr_w(name));
363 return ERROR_SUCCESS;
366 static UINT msi_dialog_build_font_list( msi_dialog *dialog )
368 static const WCHAR query[] = {
369 'S','E','L','E','C','T',' ','*',' ',
370 'F','R','O','M',' ','`','T','e','x','t','S','t','y','l','e','`',' ',0
372 UINT r;
373 MSIQUERY *view = NULL;
375 TRACE("dialog %p\n", dialog );
377 r = MSI_OpenQuery( dialog->package->db, &view, query );
378 if( r != ERROR_SUCCESS )
379 return r;
381 r = MSI_IterateRecords( view, NULL, msi_dialog_add_font, dialog );
382 msiobj_release( &view->hdr );
384 return r;
387 static void msi_destroy_control( msi_control *t )
389 list_remove( &t->entry );
390 /* leave dialog->hwnd - destroying parent destroys child windows */
391 msi_free( t->property );
392 msi_free( t->value );
393 if( t->hBitmap )
394 DeleteObject( t->hBitmap );
395 if( t->hIcon )
396 DestroyIcon( t->hIcon );
397 msi_free( t->tabnext );
398 msi_free( t->type );
399 if (t->hDll)
400 FreeLibrary( t->hDll );
401 msi_free( t );
404 static msi_control *msi_dialog_create_window( msi_dialog *dialog,
405 MSIRECORD *rec, DWORD exstyle, LPCWSTR szCls, LPCWSTR name, LPCWSTR text,
406 DWORD style, HWND parent )
408 DWORD x, y, width, height;
409 LPWSTR font = NULL, title_font = NULL;
410 LPCWSTR title = NULL;
411 msi_control *control;
413 style |= WS_CHILD;
415 control = msi_alloc( sizeof *control + strlenW(name)*sizeof(WCHAR) );
416 if (!control)
417 return NULL;
419 strcpyW( control->name, name );
420 list_add_head( &dialog->controls, &control->entry );
421 control->handler = NULL;
422 control->update = NULL;
423 control->property = NULL;
424 control->value = NULL;
425 control->hBitmap = NULL;
426 control->hIcon = NULL;
427 control->hDll = NULL;
428 control->tabnext = strdupW( MSI_RecordGetString( rec, 11) );
429 control->type = strdupW( MSI_RecordGetString( rec, 3 ) );
430 control->progress_current = 0;
431 control->progress_max = 100;
433 x = MSI_RecordGetInteger( rec, 4 );
434 y = MSI_RecordGetInteger( rec, 5 );
435 width = MSI_RecordGetInteger( rec, 6 );
436 height = MSI_RecordGetInteger( rec, 7 );
438 x = msi_dialog_scale_unit( dialog, x );
439 y = msi_dialog_scale_unit( dialog, y );
440 width = msi_dialog_scale_unit( dialog, width );
441 height = msi_dialog_scale_unit( dialog, height );
443 if( text )
445 deformat_string( dialog->package, text, &title_font );
446 font = msi_dialog_get_style( title_font, &title );
449 control->hwnd = CreateWindowExW( exstyle, szCls, title, style,
450 x, y, width, height, parent, NULL, NULL, NULL );
452 TRACE("Dialog %s control %s hwnd %p\n",
453 debugstr_w(dialog->name), debugstr_w(text), control->hwnd );
455 msi_dialog_set_font( dialog, control->hwnd,
456 font ? font : dialog->default_font );
458 msi_free( title_font );
459 msi_free( font );
461 return control;
464 static LPWSTR msi_dialog_get_uitext( msi_dialog *dialog, LPCWSTR key )
466 MSIRECORD *rec;
467 LPWSTR text;
469 static const WCHAR query[] = {
470 's','e','l','e','c','t',' ','*',' ',
471 'f','r','o','m',' ','`','U','I','T','e','x','t','`',' ',
472 'w','h','e','r','e',' ','`','K','e','y','`',' ','=',' ','\'','%','s','\'',0
475 rec = MSI_QueryGetRecord( dialog->package->db, query, key );
476 if (!rec) return NULL;
477 text = strdupW( MSI_RecordGetString( rec, 2 ) );
478 msiobj_release( &rec->hdr );
479 return text;
482 static MSIRECORD *msi_get_binary_record( MSIDATABASE *db, LPCWSTR name )
484 static const WCHAR query[] = {
485 's','e','l','e','c','t',' ','*',' ',
486 'f','r','o','m',' ','B','i','n','a','r','y',' ',
487 'w','h','e','r','e',' ',
488 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0
491 return MSI_QueryGetRecord( db, query, name );
494 static LPWSTR msi_create_tmp_path(void)
496 WCHAR tmp[MAX_PATH];
497 LPWSTR path = NULL;
498 static const WCHAR prefix[] = { 'm','s','i',0 };
499 DWORD len, r;
501 r = GetTempPathW( MAX_PATH, tmp );
502 if( !r )
503 return path;
504 len = lstrlenW( tmp ) + 20;
505 path = msi_alloc( len * sizeof (WCHAR) );
506 if( path )
508 r = GetTempFileNameW( tmp, prefix, 0, path );
509 if (!r)
511 msi_free( path );
512 path = NULL;
515 return path;
519 static HANDLE msi_load_image( MSIDATABASE *db, LPCWSTR name, UINT type,
520 UINT cx, UINT cy, UINT flags )
522 MSIRECORD *rec = NULL;
523 HANDLE himage = NULL;
524 LPWSTR tmp;
525 UINT r;
527 TRACE("%p %s %u %u %08x\n", db, debugstr_w(name), cx, cy, flags);
529 tmp = msi_create_tmp_path();
530 if( !tmp )
531 return himage;
533 rec = msi_get_binary_record( db, name );
534 if( rec )
536 r = MSI_RecordStreamToFile( rec, 2, tmp );
537 if( r == ERROR_SUCCESS )
539 himage = LoadImageW( 0, tmp, type, cx, cy, flags );
541 msiobj_release( &rec->hdr );
543 DeleteFileW( tmp );
545 msi_free( tmp );
546 return himage;
549 static HICON msi_load_icon( MSIDATABASE *db, LPCWSTR text, UINT attributes )
551 DWORD cx = 0, cy = 0, flags;
553 flags = LR_LOADFROMFILE | LR_DEFAULTSIZE;
554 if( attributes & msidbControlAttributesFixedSize )
556 flags &= ~LR_DEFAULTSIZE;
557 if( attributes & msidbControlAttributesIconSize16 )
559 cx += 16;
560 cy += 16;
562 if( attributes & msidbControlAttributesIconSize32 )
564 cx += 32;
565 cy += 32;
567 /* msidbControlAttributesIconSize48 handled by above logic */
569 return msi_load_image( db, text, IMAGE_ICON, cx, cy, flags );
572 static void msi_dialog_update_controls( msi_dialog *dialog, LPCWSTR property )
574 msi_control *control;
576 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
578 if ( !lstrcmpW( control->property, property ) && control->update )
579 control->update( dialog, control );
583 /* called from the Control Event subscription code */
584 void msi_dialog_handle_event( msi_dialog* dialog, LPCWSTR control,
585 LPCWSTR attribute, MSIRECORD *rec )
587 msi_control* ctrl;
588 LPCWSTR font_text, text = NULL;
589 LPWSTR font;
591 static const WCHAR empty[] = {0};
593 ctrl = msi_dialog_find_control( dialog, control );
594 if (!ctrl)
595 return;
596 if( !lstrcmpW(attribute, szText) )
598 font_text = MSI_RecordGetString( rec , 1 );
599 font = msi_dialog_get_style( font_text, &text );
600 if (!text) text = empty;
601 SetWindowTextW( ctrl->hwnd, text );
602 msi_free( font );
603 msi_dialog_check_messages( NULL );
605 else if( !lstrcmpW(attribute, szProgress) )
607 DWORD func, val;
609 func = MSI_RecordGetInteger( rec , 1 );
610 val = MSI_RecordGetInteger( rec , 2 );
612 switch (func)
614 case 0: /* init */
615 ctrl->progress_max = val;
616 ctrl->progress_current = 0;
617 SendMessageW(ctrl->hwnd, PBM_SETRANGE, 0, MAKELPARAM(0,100));
618 SendMessageW(ctrl->hwnd, PBM_SETPOS, 0, 0);
619 break;
620 case 1: /* FIXME: not sure what this is supposed to do */
621 break;
622 case 2: /* move */
623 ctrl->progress_current += val;
624 SendMessageW(ctrl->hwnd, PBM_SETPOS, 100*(ctrl->progress_current/ctrl->progress_max), 0);
625 break;
626 default:
627 ERR("Unknown progress message %d\n", func);
628 break;
631 else if ( !lstrcmpW(attribute, szProperty) )
633 MSIFEATURE *feature = msi_seltree_get_selected_feature( ctrl );
634 MSI_SetPropertyW( dialog->package, ctrl->property, feature->Directory );
636 else if ( !lstrcmpW(attribute, szSelectionPath) )
638 LPWSTR prop = msi_dialog_dup_property( dialog, ctrl->property, TRUE );
639 LPWSTR path;
640 if (!prop) return;
641 path = msi_dup_property( dialog->package, prop );
642 SetWindowTextW( ctrl->hwnd, path );
643 msi_free(prop);
644 msi_free(path);
646 else
648 FIXME("Attribute %s not being set\n", debugstr_w(attribute));
649 return;
653 static void msi_dialog_map_events(msi_dialog* dialog, LPCWSTR control)
655 static const WCHAR Query[] = {
656 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
657 '`','E','v','e','n','t','M','a','p','p','i','n','g','`',' ',
658 'W','H','E','R','E',' ',
659 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
660 'A','N','D',' ',
661 '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',0
663 MSIRECORD *row;
664 LPCWSTR event, attribute;
666 row = MSI_QueryGetRecord( dialog->package->db, Query, dialog->name, control );
667 if (!row)
668 return;
670 event = MSI_RecordGetString( row, 3 );
671 attribute = MSI_RecordGetString( row, 4 );
672 ControlEvent_SubscribeToEvent( dialog->package, dialog, event, control, attribute );
673 msiobj_release( &row->hdr );
676 /* everything except radio buttons */
677 static msi_control *msi_dialog_add_control( msi_dialog *dialog,
678 MSIRECORD *rec, LPCWSTR szCls, DWORD style )
680 DWORD attributes;
681 LPCWSTR text, name;
682 DWORD exstyle = 0;
684 name = MSI_RecordGetString( rec, 2 );
685 attributes = MSI_RecordGetInteger( rec, 8 );
686 text = MSI_RecordGetString( rec, 10 );
687 if( attributes & msidbControlAttributesVisible )
688 style |= WS_VISIBLE;
689 if( ~attributes & msidbControlAttributesEnabled )
690 style |= WS_DISABLED;
691 if( attributes & msidbControlAttributesSunken )
692 exstyle |= WS_EX_CLIENTEDGE;
694 msi_dialog_map_events(dialog, name);
696 return msi_dialog_create_window( dialog, rec, exstyle, szCls, name,
697 text, style, dialog->hwnd );
700 struct msi_text_info
702 msi_font *font;
703 WNDPROC oldproc;
704 DWORD attributes;
708 * we don't erase our own background,
709 * so we have to make sure that the parent window redraws first
711 static void msi_text_on_settext( HWND hWnd )
713 HWND hParent;
714 RECT rc;
716 hParent = GetParent( hWnd );
717 GetWindowRect( hWnd, &rc );
718 MapWindowPoints( NULL, hParent, (LPPOINT) &rc, 2 );
719 InvalidateRect( hParent, &rc, TRUE );
722 static LRESULT WINAPI
723 MSIText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
725 struct msi_text_info *info;
726 LRESULT r = 0;
728 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
730 info = GetPropW(hWnd, szButtonData);
732 if( msg == WM_CTLCOLORSTATIC &&
733 ( info->attributes & msidbControlAttributesTransparent ) )
735 SetBkMode( (HDC)wParam, TRANSPARENT );
736 return (LRESULT) GetStockObject(NULL_BRUSH);
739 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
740 if ( info->font )
741 SetTextColor( (HDC)wParam, info->font->color );
743 switch( msg )
745 case WM_SETTEXT:
746 msi_text_on_settext( hWnd );
747 break;
748 case WM_NCDESTROY:
749 msi_free( info );
750 RemovePropW( hWnd, szButtonData );
751 break;
754 return r;
757 static UINT msi_dialog_text_control( msi_dialog *dialog, MSIRECORD *rec )
759 msi_control *control;
760 struct msi_text_info *info;
761 LPCWSTR text, ptr, prop;
762 LPWSTR font_name;
764 TRACE("%p %p\n", dialog, rec);
766 control = msi_dialog_add_control( dialog, rec, szStatic, SS_LEFT | WS_GROUP );
767 if( !control )
768 return ERROR_FUNCTION_FAILED;
770 info = msi_alloc( sizeof *info );
771 if( !info )
772 return ERROR_SUCCESS;
774 control->attributes = MSI_RecordGetInteger( rec, 8 );
775 prop = MSI_RecordGetString( rec, 9 );
776 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
778 text = MSI_RecordGetString( rec, 10 );
779 font_name = msi_dialog_get_style( text, &ptr );
780 info->font = ( font_name ) ? msi_dialog_find_font( dialog, font_name ) : NULL;
781 msi_free( font_name );
783 info->attributes = MSI_RecordGetInteger( rec, 8 );
784 if( info->attributes & msidbControlAttributesTransparent )
785 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT );
787 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
788 (LONG_PTR)MSIText_WndProc );
789 SetPropW( control->hwnd, szButtonData, info );
791 ControlEvent_SubscribeToEvent( dialog->package, dialog,
792 szSelectionPath, control->name, szSelectionPath );
794 return ERROR_SUCCESS;
797 static UINT msi_dialog_button_control( msi_dialog *dialog, MSIRECORD *rec )
799 msi_control *control;
800 UINT attributes, style;
801 LPWSTR text;
803 TRACE("%p %p\n", dialog, rec);
805 style = WS_TABSTOP;
806 attributes = MSI_RecordGetInteger( rec, 8 );
807 if( attributes & msidbControlAttributesIcon )
808 style |= BS_ICON;
810 control = msi_dialog_add_control( dialog, rec, szButton, style );
811 if( !control )
812 return ERROR_FUNCTION_FAILED;
814 control->handler = msi_dialog_button_handler;
816 /* set the icon */
817 text = msi_get_deformatted_field( dialog->package, rec, 10 );
818 control->hIcon = msi_load_icon( dialog->package->db, text, attributes );
819 if( attributes & msidbControlAttributesIcon )
820 SendMessageW( control->hwnd, BM_SETIMAGE, IMAGE_ICON, (LPARAM) control->hIcon );
821 msi_free( text );
823 return ERROR_SUCCESS;
826 static LPWSTR msi_get_checkbox_value( msi_dialog *dialog, LPCWSTR prop )
828 static const WCHAR query[] = {
829 'S','E','L','E','C','T',' ','*',' ',
830 'F','R','O','M',' ','`','C','h','e','c','k','B','o','x','`',' ',
831 'W','H','E','R','E',' ',
832 '`','P','r','o','p','e','r','t','y','`',' ','=',' ',
833 '\'','%','s','\'',0
835 MSIRECORD *rec = NULL;
836 LPWSTR ret = NULL;
838 /* find if there is a value associated with the checkbox */
839 rec = MSI_QueryGetRecord( dialog->package->db, query, prop );
840 if (!rec)
841 return ret;
843 ret = msi_get_deformatted_field( dialog->package, rec, 2 );
844 if( ret && !ret[0] )
846 msi_free( ret );
847 ret = NULL;
849 msiobj_release( &rec->hdr );
850 if (ret)
851 return ret;
853 ret = msi_dup_property( dialog->package, prop );
854 if( ret && !ret[0] )
856 msi_free( ret );
857 ret = NULL;
860 return ret;
863 static UINT msi_dialog_checkbox_control( msi_dialog *dialog, MSIRECORD *rec )
865 msi_control *control;
866 LPCWSTR prop;
868 TRACE("%p %p\n", dialog, rec);
870 control = msi_dialog_add_control( dialog, rec, szButton,
871 BS_CHECKBOX | BS_MULTILINE | WS_TABSTOP );
872 control->handler = msi_dialog_checkbox_handler;
873 control->update = msi_dialog_checkbox_sync_state;
874 prop = MSI_RecordGetString( rec, 9 );
875 if( prop )
877 control->property = strdupW( prop );
878 control->value = msi_get_checkbox_value( dialog, prop );
879 TRACE("control %s value %s\n", debugstr_w(control->property),
880 debugstr_w(control->value));
882 msi_dialog_checkbox_sync_state( dialog, control );
884 return ERROR_SUCCESS;
887 static UINT msi_dialog_line_control( msi_dialog *dialog, MSIRECORD *rec )
889 DWORD attributes;
890 LPCWSTR name;
891 DWORD style, exstyle = 0;
892 DWORD x, y, width, height;
893 msi_control *control;
895 TRACE("%p %p\n", dialog, rec);
897 style = WS_CHILD | SS_ETCHEDHORZ | SS_SUNKEN;
899 name = MSI_RecordGetString( rec, 2 );
900 attributes = MSI_RecordGetInteger( rec, 8 );
902 if( attributes & msidbControlAttributesVisible )
903 style |= WS_VISIBLE;
904 if( ~attributes & msidbControlAttributesEnabled )
905 style |= WS_DISABLED;
906 if( attributes & msidbControlAttributesSunken )
907 exstyle |= WS_EX_CLIENTEDGE;
909 msi_dialog_map_events(dialog, name);
911 control = msi_alloc( sizeof(*control) + strlenW(name) * sizeof(WCHAR) );
912 if (!control)
913 return ERROR_OUTOFMEMORY;
915 strcpyW( control->name, name );
916 list_add_head( &dialog->controls, &control->entry );
917 control->handler = NULL;
918 control->property = NULL;
919 control->value = NULL;
920 control->hBitmap = NULL;
921 control->hIcon = NULL;
922 control->hDll = NULL;
923 control->tabnext = strdupW( MSI_RecordGetString( rec, 11) );
924 control->type = strdupW( MSI_RecordGetString( rec, 3 ) );
925 control->progress_current = 0;
926 control->progress_max = 100;
928 x = MSI_RecordGetInteger( rec, 4 );
929 y = MSI_RecordGetInteger( rec, 5 );
930 width = MSI_RecordGetInteger( rec, 6 );
932 x = msi_dialog_scale_unit( dialog, x );
933 y = msi_dialog_scale_unit( dialog, y );
934 width = msi_dialog_scale_unit( dialog, width );
935 height = 2; /* line is exactly 2 units in height */
937 control->hwnd = CreateWindowExW( exstyle, szStatic, NULL, style,
938 x, y, width, height, dialog->hwnd, NULL, NULL, NULL );
940 TRACE("Dialog %s control %s hwnd %p\n",
941 debugstr_w(dialog->name), debugstr_w(name), control->hwnd );
943 return ERROR_SUCCESS;
946 /******************** Scroll Text ********************************************/
948 struct msi_scrolltext_info
950 msi_dialog *dialog;
951 msi_control *control;
952 WNDPROC oldproc;
955 static LRESULT WINAPI
956 MSIScrollText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
958 struct msi_scrolltext_info *info;
959 HRESULT r;
961 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
963 info = GetPropW( hWnd, szButtonData );
965 r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
967 switch( msg )
969 case WM_NCDESTROY:
970 msi_free( info );
971 RemovePropW( hWnd, szButtonData );
972 break;
973 case WM_PAINT:
974 /* native MSI sets a wait cursor here */
975 msi_dialog_button_handler( info->dialog, info->control, BN_CLICKED );
976 break;
978 return r;
981 struct msi_streamin_info
983 LPSTR string;
984 DWORD offset;
985 DWORD length;
988 static DWORD CALLBACK
989 msi_richedit_stream_in( DWORD_PTR arg, LPBYTE buffer, LONG count, LONG *pcb )
991 struct msi_streamin_info *info = (struct msi_streamin_info*) arg;
993 if( (count + info->offset) > info->length )
994 count = info->length - info->offset;
995 memcpy( buffer, &info->string[ info->offset ], count );
996 *pcb = count;
997 info->offset += count;
999 TRACE("%d/%d\n", info->offset, info->length);
1001 return 0;
1004 static void msi_scrolltext_add_text( msi_control *control, LPCWSTR text )
1006 struct msi_streamin_info info;
1007 EDITSTREAM es;
1009 info.string = strdupWtoA( text );
1010 info.offset = 0;
1011 info.length = lstrlenA( info.string ) + 1;
1013 es.dwCookie = (DWORD_PTR) &info;
1014 es.dwError = 0;
1015 es.pfnCallback = msi_richedit_stream_in;
1017 SendMessageW( control->hwnd, EM_STREAMIN, SF_RTF, (LPARAM) &es );
1019 msi_free( info.string );
1022 static UINT msi_dialog_scrolltext_control( msi_dialog *dialog, MSIRECORD *rec )
1024 static const WCHAR szRichEdit20W[] = {
1025 'R','i','c','h','E','d','i','t','2','0','W',0
1027 struct msi_scrolltext_info *info;
1028 msi_control *control;
1029 HMODULE hRichedit;
1030 LPCWSTR text;
1031 DWORD style;
1033 info = msi_alloc( sizeof *info );
1034 if (!info)
1035 return ERROR_FUNCTION_FAILED;
1037 hRichedit = LoadLibraryA("riched20");
1039 style = WS_BORDER | ES_MULTILINE | WS_VSCROLL |
1040 ES_READONLY | ES_AUTOVSCROLL | WS_TABSTOP;
1041 control = msi_dialog_add_control( dialog, rec, szRichEdit20W, style );
1042 if (!control)
1044 FreeLibrary( hRichedit );
1045 msi_free( info );
1046 return ERROR_FUNCTION_FAILED;
1049 control->hDll = hRichedit;
1051 info->dialog = dialog;
1052 info->control = control;
1054 /* subclass the static control */
1055 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1056 (LONG_PTR)MSIScrollText_WndProc );
1057 SetPropW( control->hwnd, szButtonData, info );
1059 /* add the text into the richedit */
1060 text = MSI_RecordGetString( rec, 10 );
1061 if (text)
1062 msi_scrolltext_add_text( control, text );
1064 return ERROR_SUCCESS;
1067 static HBITMAP msi_load_picture( MSIDATABASE *db, LPCWSTR name,
1068 INT cx, INT cy, DWORD flags )
1070 HBITMAP hOleBitmap = 0, hBitmap = 0, hOldSrcBitmap, hOldDestBitmap;
1071 MSIRECORD *rec = NULL;
1072 IStream *stm = NULL;
1073 IPicture *pic = NULL;
1074 HDC srcdc, destdc;
1075 BITMAP bm;
1076 UINT r;
1078 rec = msi_get_binary_record( db, name );
1079 if( !rec )
1080 goto end;
1082 r = MSI_RecordGetIStream( rec, 2, &stm );
1083 msiobj_release( &rec->hdr );
1084 if( r != ERROR_SUCCESS )
1085 goto end;
1087 r = OleLoadPicture( stm, 0, TRUE, &IID_IPicture, (LPVOID*) &pic );
1088 IStream_Release( stm );
1089 if( FAILED( r ) )
1091 ERR("failed to load picture\n");
1092 goto end;
1095 r = IPicture_get_Handle( pic, (OLE_HANDLE*) &hOleBitmap );
1096 if( FAILED( r ) )
1098 ERR("failed to get bitmap handle\n");
1099 goto end;
1102 /* make the bitmap the desired size */
1103 r = GetObjectW( hOleBitmap, sizeof bm, &bm );
1104 if (r != sizeof bm )
1106 ERR("failed to get bitmap size\n");
1107 goto end;
1110 if (flags & LR_DEFAULTSIZE)
1112 cx = bm.bmWidth;
1113 cy = bm.bmHeight;
1116 srcdc = CreateCompatibleDC( NULL );
1117 hOldSrcBitmap = SelectObject( srcdc, hOleBitmap );
1118 destdc = CreateCompatibleDC( NULL );
1119 hBitmap = CreateCompatibleBitmap( srcdc, cx, cy );
1120 hOldDestBitmap = SelectObject( destdc, hBitmap );
1121 StretchBlt( destdc, 0, 0, cx, cy,
1122 srcdc, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
1123 SelectObject( srcdc, hOldSrcBitmap );
1124 SelectObject( destdc, hOldDestBitmap );
1125 DeleteDC( srcdc );
1126 DeleteDC( destdc );
1128 end:
1129 if ( pic )
1130 IPicture_Release( pic );
1131 return hBitmap;
1134 static UINT msi_dialog_bitmap_control( msi_dialog *dialog, MSIRECORD *rec )
1136 UINT cx, cy, flags, style, attributes;
1137 msi_control *control;
1138 LPWSTR text;
1140 flags = LR_LOADFROMFILE;
1141 style = SS_BITMAP | SS_LEFT | WS_GROUP;
1143 attributes = MSI_RecordGetInteger( rec, 8 );
1144 if( attributes & msidbControlAttributesFixedSize )
1146 flags |= LR_DEFAULTSIZE;
1147 style |= SS_CENTERIMAGE;
1150 control = msi_dialog_add_control( dialog, rec, szStatic, style );
1151 cx = MSI_RecordGetInteger( rec, 6 );
1152 cy = MSI_RecordGetInteger( rec, 7 );
1153 cx = msi_dialog_scale_unit( dialog, cx );
1154 cy = msi_dialog_scale_unit( dialog, cy );
1156 text = msi_get_deformatted_field( dialog->package, rec, 10 );
1157 control->hBitmap = msi_load_picture( dialog->package->db, text, cx, cy, flags );
1158 if( control->hBitmap )
1159 SendMessageW( control->hwnd, STM_SETIMAGE,
1160 IMAGE_BITMAP, (LPARAM) control->hBitmap );
1161 else
1162 ERR("Failed to load bitmap %s\n", debugstr_w(text));
1164 msi_free( text );
1166 return ERROR_SUCCESS;
1169 static UINT msi_dialog_icon_control( msi_dialog *dialog, MSIRECORD *rec )
1171 msi_control *control;
1172 DWORD attributes;
1173 LPWSTR text;
1175 TRACE("\n");
1177 control = msi_dialog_add_control( dialog, rec, szStatic,
1178 SS_ICON | SS_CENTERIMAGE | WS_GROUP );
1180 attributes = MSI_RecordGetInteger( rec, 8 );
1181 text = msi_get_deformatted_field( dialog->package, rec, 10 );
1182 control->hIcon = msi_load_icon( dialog->package->db, text, attributes );
1183 if( control->hIcon )
1184 SendMessageW( control->hwnd, STM_SETICON, (WPARAM) control->hIcon, 0 );
1185 else
1186 ERR("Failed to load bitmap %s\n", debugstr_w(text));
1187 msi_free( text );
1188 return ERROR_SUCCESS;
1191 static UINT msi_dialog_combo_control( msi_dialog *dialog, MSIRECORD *rec )
1193 static const WCHAR szCombo[] = { 'C','O','M','B','O','B','O','X',0 };
1194 DWORD attributes, style;
1196 style = CBS_AUTOHSCROLL | WS_TABSTOP | WS_GROUP | WS_CHILD;
1197 attributes = MSI_RecordGetInteger( rec, 8 );
1198 if ( ~attributes & msidbControlAttributesSorted)
1199 style |= CBS_SORT;
1200 if ( attributes & msidbControlAttributesComboList)
1201 style |= CBS_DROPDOWNLIST;
1202 else
1203 style |= CBS_DROPDOWN;
1205 msi_dialog_add_control( dialog, rec, szCombo, style );
1206 return ERROR_SUCCESS;
1209 /* length of 2^32 + 1 */
1210 #define MAX_NUM_DIGITS 11
1212 static UINT msi_dialog_edit_control( msi_dialog *dialog, MSIRECORD *rec )
1214 msi_control *control;
1215 LPCWSTR prop, text;
1216 LPWSTR val, begin, end;
1217 WCHAR num[MAX_NUM_DIGITS];
1218 DWORD limit;
1220 control = msi_dialog_add_control( dialog, rec, szEdit,
1221 WS_BORDER | WS_TABSTOP | ES_AUTOHSCROLL );
1222 control->handler = msi_dialog_edit_handler;
1224 text = MSI_RecordGetString( rec, 10 );
1225 if ( text )
1227 begin = strchrW( text, '{' );
1228 end = strchrW( text, '}' );
1230 if ( begin && end && end > begin &&
1231 begin[0] >= '0' && begin[0] <= '9' &&
1232 end - begin < MAX_NUM_DIGITS)
1234 lstrcpynW( num, begin + 1, end - begin );
1235 limit = atolW( num );
1237 SendMessageW( control->hwnd, EM_SETLIMITTEXT, limit, 0 );
1241 prop = MSI_RecordGetString( rec, 9 );
1242 if( prop )
1243 control->property = strdupW( prop );
1245 val = msi_dup_property( dialog->package, control->property );
1246 SetWindowTextW( control->hwnd, val );
1247 msi_free( val );
1248 return ERROR_SUCCESS;
1251 /******************** Masked Edit ********************************************/
1253 #define MASK_MAX_GROUPS 20
1255 struct msi_mask_group
1257 UINT len;
1258 UINT ofs;
1259 WCHAR type;
1260 HWND hwnd;
1263 struct msi_maskedit_info
1265 msi_dialog *dialog;
1266 WNDPROC oldproc;
1267 HWND hwnd;
1268 LPWSTR prop;
1269 UINT num_chars;
1270 UINT num_groups;
1271 struct msi_mask_group group[MASK_MAX_GROUPS];
1274 static BOOL msi_mask_editable( WCHAR type )
1276 switch (type)
1278 case '%':
1279 case '#':
1280 case '&':
1281 case '`':
1282 case '?':
1283 case '^':
1284 return TRUE;
1286 return FALSE;
1289 static void msi_mask_control_change( struct msi_maskedit_info *info )
1291 LPWSTR val;
1292 UINT i, n, r;
1294 val = msi_alloc( (info->num_chars+1)*sizeof(WCHAR) );
1295 for( i=0, n=0; i<info->num_groups; i++ )
1297 if( (info->group[i].len + n) > info->num_chars )
1299 ERR("can't fit control %d text into template\n",i);
1300 break;
1302 if (!msi_mask_editable(info->group[i].type))
1304 for(r=0; r<info->group[i].len; r++)
1305 val[n+r] = info->group[i].type;
1306 val[n+r] = 0;
1308 else
1310 r = GetWindowTextW( info->group[i].hwnd, &val[n], info->group[i].len+1 );
1311 if( r != info->group[i].len )
1312 break;
1314 n += r;
1317 TRACE("%d/%d controls were good\n", i, info->num_groups);
1319 if( i == info->num_groups )
1321 TRACE("Set property %s to %s\n",
1322 debugstr_w(info->prop), debugstr_w(val) );
1323 CharUpperBuffW( val, info->num_chars );
1324 MSI_SetPropertyW( info->dialog->package, info->prop, val );
1325 msi_dialog_evaluate_control_conditions( info->dialog );
1327 msi_free( val );
1330 /* now move to the next control if necessary */
1331 static VOID msi_mask_next_control( struct msi_maskedit_info *info, HWND hWnd )
1333 HWND hWndNext;
1334 UINT len, i;
1336 for( i=0; i<info->num_groups; i++ )
1337 if( info->group[i].hwnd == hWnd )
1338 break;
1340 /* don't move from the last control */
1341 if( i >= (info->num_groups-1) )
1342 return;
1344 len = SendMessageW( hWnd, WM_GETTEXTLENGTH, 0, 0 );
1345 if( len < info->group[i].len )
1346 return;
1348 hWndNext = GetNextDlgTabItem( GetParent( hWnd ), hWnd, FALSE );
1349 SetFocus( hWndNext );
1352 static LRESULT WINAPI
1353 MSIMaskedEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1355 struct msi_maskedit_info *info;
1356 HRESULT r;
1358 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1360 info = GetPropW(hWnd, szButtonData);
1362 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1364 switch( msg )
1366 case WM_COMMAND:
1367 if (HIWORD(wParam) == EN_CHANGE)
1369 msi_mask_control_change( info );
1370 msi_mask_next_control( info, (HWND) lParam );
1372 break;
1373 case WM_NCDESTROY:
1374 msi_free( info->prop );
1375 msi_free( info );
1376 RemovePropW( hWnd, szButtonData );
1377 break;
1380 return r;
1383 /* fish the various bits of the property out and put them in the control */
1384 static void
1385 msi_maskedit_set_text( struct msi_maskedit_info *info, LPCWSTR text )
1387 LPCWSTR p;
1388 UINT i;
1390 p = text;
1391 for( i = 0; i < info->num_groups; i++ )
1393 if( info->group[i].len < strlenW( p ) )
1395 LPWSTR chunk = strdupW( p );
1396 chunk[ info->group[i].len ] = 0;
1397 SetWindowTextW( info->group[i].hwnd, chunk );
1398 msi_free( chunk );
1400 else
1402 SetWindowTextW( info->group[i].hwnd, p );
1403 break;
1405 p += info->group[i].len;
1409 static struct msi_maskedit_info * msi_dialog_parse_groups( LPCWSTR mask )
1411 struct msi_maskedit_info * info = NULL;
1412 int i = 0, n = 0, total = 0;
1413 LPCWSTR p;
1415 TRACE("masked control, template %s\n", debugstr_w(mask));
1417 if( !mask )
1418 return info;
1420 info = msi_alloc_zero( sizeof *info );
1421 if( !info )
1422 return info;
1424 p = strchrW(mask, '<');
1425 if( p )
1426 p++;
1427 else
1428 p = mask;
1430 for( i=0; i<MASK_MAX_GROUPS; i++ )
1432 /* stop at the end of the string */
1433 if( p[0] == 0 || p[0] == '>' )
1434 break;
1436 /* count the number of the same identifier */
1437 for( n=0; p[n] == p[0]; n++ )
1439 info->group[i].ofs = total;
1440 info->group[i].type = p[0];
1441 if( p[n] == '=' )
1443 n++;
1444 total++; /* an extra not part of the group */
1446 info->group[i].len = n;
1447 total += n;
1448 p += n;
1451 TRACE("%d characters in %d groups\n", total, i );
1452 if( i == MASK_MAX_GROUPS )
1453 ERR("too many groups in PIDTemplate %s\n", debugstr_w(mask));
1455 info->num_chars = total;
1456 info->num_groups = i;
1458 return info;
1461 static void
1462 msi_maskedit_create_children( struct msi_maskedit_info *info, LPCWSTR font )
1464 DWORD width, height, style, wx, ww;
1465 RECT rect;
1466 HWND hwnd;
1467 UINT i;
1469 style = WS_CHILD | WS_BORDER | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL;
1471 GetClientRect( info->hwnd, &rect );
1473 width = rect.right - rect.left;
1474 height = rect.bottom - rect.top;
1476 for( i = 0; i < info->num_groups; i++ )
1478 if (!msi_mask_editable( info->group[i].type ))
1479 continue;
1480 wx = (info->group[i].ofs * width) / info->num_chars;
1481 ww = (info->group[i].len * width) / info->num_chars;
1483 hwnd = CreateWindowW( szEdit, NULL, style, wx, 0, ww, height,
1484 info->hwnd, NULL, NULL, NULL );
1485 if( !hwnd )
1487 ERR("failed to create mask edit sub window\n");
1488 break;
1491 SendMessageW( hwnd, EM_LIMITTEXT, info->group[i].len, 0 );
1493 msi_dialog_set_font( info->dialog, hwnd,
1494 font?font:info->dialog->default_font );
1495 info->group[i].hwnd = hwnd;
1500 * office 2003 uses "73931<````=````=````=````=`````>@@@@@"
1501 * delphi 7 uses "<????-??????-??????-????>" and "<???-???>"
1502 * filemaker pro 7 uses "<^^^^=^^^^=^^^^=^^^^=^^^^=^^^^=^^^^^>"
1504 static UINT msi_dialog_maskedit_control( msi_dialog *dialog, MSIRECORD *rec )
1506 LPWSTR font_mask, val = NULL, font;
1507 struct msi_maskedit_info *info = NULL;
1508 UINT ret = ERROR_SUCCESS;
1509 msi_control *control;
1510 LPCWSTR prop, mask;
1512 TRACE("\n");
1514 font_mask = msi_get_deformatted_field( dialog->package, rec, 10 );
1515 font = msi_dialog_get_style( font_mask, &mask );
1516 if( !mask )
1518 ERR("mask template is empty\n");
1519 goto end;
1522 info = msi_dialog_parse_groups( mask );
1523 if( !info )
1525 ERR("template %s is invalid\n", debugstr_w(mask));
1526 goto end;
1529 info->dialog = dialog;
1531 control = msi_dialog_add_control( dialog, rec, szStatic,
1532 SS_OWNERDRAW | WS_GROUP | WS_VISIBLE );
1533 if( !control )
1535 ERR("Failed to create maskedit container\n");
1536 ret = ERROR_FUNCTION_FAILED;
1537 goto end;
1539 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
1541 info->hwnd = control->hwnd;
1543 /* subclass the static control */
1544 info->oldproc = (WNDPROC) SetWindowLongPtrW( info->hwnd, GWLP_WNDPROC,
1545 (LONG_PTR)MSIMaskedEdit_WndProc );
1546 SetPropW( control->hwnd, szButtonData, info );
1548 prop = MSI_RecordGetString( rec, 9 );
1549 if( prop )
1550 info->prop = strdupW( prop );
1552 msi_maskedit_create_children( info, font );
1554 if( prop )
1556 val = msi_dup_property( dialog->package, prop );
1557 if( val )
1559 msi_maskedit_set_text( info, val );
1560 msi_free( val );
1564 end:
1565 if( ret != ERROR_SUCCESS )
1566 msi_free( info );
1567 msi_free( font_mask );
1568 msi_free( font );
1569 return ret;
1572 /******************** Progress Bar *****************************************/
1574 static UINT msi_dialog_progress_bar( msi_dialog *dialog, MSIRECORD *rec )
1576 msi_control *control;
1578 control = msi_dialog_add_control( dialog, rec, PROGRESS_CLASSW, WS_VISIBLE );
1579 if( !control )
1580 return ERROR_FUNCTION_FAILED;
1582 ControlEvent_SubscribeToEvent( dialog->package, dialog,
1583 szSetProgress, control->name, szProgress );
1584 return ERROR_SUCCESS;
1587 /******************** Path Edit ********************************************/
1589 struct msi_pathedit_info
1591 msi_dialog *dialog;
1592 msi_control *control;
1593 WNDPROC oldproc;
1596 static LPWSTR msi_get_window_text( HWND hwnd )
1598 UINT sz, r;
1599 LPWSTR buf;
1601 sz = 0x20;
1602 buf = msi_alloc( sz*sizeof(WCHAR) );
1603 while ( buf )
1605 r = GetWindowTextW( hwnd, buf, sz );
1606 if ( r < (sz - 1) )
1607 break;
1608 sz *= 2;
1609 buf = msi_realloc( buf, sz*sizeof(WCHAR) );
1612 return buf;
1615 static void msi_dialog_update_pathedit( msi_dialog *dialog, msi_control *control )
1617 LPWSTR prop, path;
1618 BOOL indirect;
1620 if (!control && !(control = msi_dialog_find_control_by_type( dialog, szPathEdit )))
1621 return;
1623 indirect = control->attributes & msidbControlAttributesIndirect;
1624 prop = msi_dialog_dup_property( dialog, control->property, indirect );
1625 path = msi_dialog_dup_property( dialog, prop, TRUE );
1627 SetWindowTextW( control->hwnd, path );
1628 SendMessageW( control->hwnd, EM_SETSEL, 0, -1 );
1630 msi_free( path );
1631 msi_free( prop );
1634 /* FIXME: test when this should fail */
1635 static BOOL msi_dialog_verify_path( LPWSTR path )
1637 if ( !lstrlenW( path ) )
1638 return FALSE;
1640 if ( PathIsRelativeW( path ) )
1641 return FALSE;
1643 return TRUE;
1646 /* returns TRUE if the path is valid, FALSE otherwise */
1647 static BOOL msi_dialog_onkillfocus( msi_dialog *dialog, msi_control *control )
1649 LPWSTR buf, prop;
1650 BOOL indirect;
1651 BOOL valid;
1653 indirect = control->attributes & msidbControlAttributesIndirect;
1654 prop = msi_dialog_dup_property( dialog, control->property, indirect );
1656 buf = msi_get_window_text( control->hwnd );
1658 if ( !msi_dialog_verify_path( buf ) )
1660 /* FIXME: display an error message box */
1661 ERR("Invalid path %s\n", debugstr_w( buf ));
1662 valid = FALSE;
1663 SetFocus( control->hwnd );
1665 else
1667 valid = TRUE;
1668 MSI_SetPropertyW( dialog->package, prop, buf );
1671 msi_dialog_update_pathedit( dialog, control );
1673 TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
1674 debugstr_w(prop));
1676 msi_free( buf );
1677 msi_free( prop );
1679 return valid;
1682 static LRESULT WINAPI MSIPathEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1684 struct msi_pathedit_info *info = GetPropW(hWnd, szButtonData);
1685 LRESULT r = 0;
1687 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1689 if ( msg == WM_KILLFOCUS )
1691 /* if the path is invalid, don't handle this message */
1692 if ( !msi_dialog_onkillfocus( info->dialog, info->control ) )
1693 return 0;
1696 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1698 if ( msg == WM_NCDESTROY )
1700 msi_free( info );
1701 RemovePropW( hWnd, szButtonData );
1704 return r;
1707 static UINT msi_dialog_pathedit_control( msi_dialog *dialog, MSIRECORD *rec )
1709 struct msi_pathedit_info *info;
1710 msi_control *control;
1711 LPCWSTR prop;
1713 info = msi_alloc( sizeof *info );
1714 if (!info)
1715 return ERROR_FUNCTION_FAILED;
1717 control = msi_dialog_add_control( dialog, rec, szEdit,
1718 WS_BORDER | WS_TABSTOP );
1719 control->attributes = MSI_RecordGetInteger( rec, 8 );
1720 prop = MSI_RecordGetString( rec, 9 );
1721 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
1723 info->dialog = dialog;
1724 info->control = control;
1725 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1726 (LONG_PTR)MSIPathEdit_WndProc );
1727 SetPropW( control->hwnd, szButtonData, info );
1729 msi_dialog_update_pathedit( dialog, control );
1731 return ERROR_SUCCESS;
1734 /* radio buttons are a bit different from normal controls */
1735 static UINT msi_dialog_create_radiobutton( MSIRECORD *rec, LPVOID param )
1737 radio_button_group_descr *group = param;
1738 msi_dialog *dialog = group->dialog;
1739 msi_control *control;
1740 LPCWSTR prop, text, name;
1741 DWORD style, attributes = group->attributes;
1743 style = WS_CHILD | BS_AUTORADIOBUTTON | BS_MULTILINE | WS_TABSTOP;
1744 name = MSI_RecordGetString( rec, 3 );
1745 text = MSI_RecordGetString( rec, 8 );
1746 if( attributes & msidbControlAttributesVisible )
1747 style |= WS_VISIBLE;
1748 if( ~attributes & msidbControlAttributesEnabled )
1749 style |= WS_DISABLED;
1751 control = msi_dialog_create_window( dialog, rec, 0, szButton, name, text,
1752 style, group->parent->hwnd );
1753 if (!control)
1754 return ERROR_FUNCTION_FAILED;
1755 control->handler = msi_dialog_radiogroup_handler;
1757 if (!lstrcmpW(control->name, group->propval))
1758 SendMessageW(control->hwnd, BM_SETCHECK, BST_CHECKED, 0);
1760 prop = MSI_RecordGetString( rec, 1 );
1761 if( prop )
1762 control->property = strdupW( prop );
1764 return ERROR_SUCCESS;
1767 static UINT msi_dialog_radiogroup_control( msi_dialog *dialog, MSIRECORD *rec )
1769 static const WCHAR query[] = {
1770 'S','E','L','E','C','T',' ','*',' ',
1771 'F','R','O','M',' ','R','a','d','i','o','B','u','t','t','o','n',' ',
1772 'W','H','E','R','E',' ',
1773 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
1774 UINT r;
1775 LPCWSTR prop;
1776 msi_control *control;
1777 MSIQUERY *view = NULL;
1778 radio_button_group_descr group;
1779 MSIPACKAGE *package = dialog->package;
1780 WNDPROC oldproc;
1781 DWORD attr, style = WS_GROUP;
1783 prop = MSI_RecordGetString( rec, 9 );
1785 TRACE("%p %p %s\n", dialog, rec, debugstr_w( prop ));
1787 attr = MSI_RecordGetInteger( rec, 8 );
1788 if (attr & msidbControlAttributesHasBorder)
1789 style |= BS_GROUPBOX;
1790 else
1791 style |= BS_OWNERDRAW;
1793 /* Create parent group box to hold radio buttons */
1794 control = msi_dialog_add_control( dialog, rec, szButton, style );
1795 if( !control )
1796 return ERROR_FUNCTION_FAILED;
1798 oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1799 (LONG_PTR)MSIRadioGroup_WndProc );
1800 SetPropW(control->hwnd, szButtonData, oldproc);
1801 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
1803 if( prop )
1804 control->property = strdupW( prop );
1806 /* query the Radio Button table for all control in this group */
1807 r = MSI_OpenQuery( package->db, &view, query, prop );
1808 if( r != ERROR_SUCCESS )
1810 ERR("query failed for dialog %s radio group %s\n",
1811 debugstr_w(dialog->name), debugstr_w(prop));
1812 return ERROR_INVALID_PARAMETER;
1815 group.dialog = dialog;
1816 group.parent = control;
1817 group.attributes = MSI_RecordGetInteger( rec, 8 );
1818 group.propval = msi_dup_property( dialog->package, control->property );
1820 r = MSI_IterateRecords( view, 0, msi_dialog_create_radiobutton, &group );
1821 msiobj_release( &view->hdr );
1822 msi_free( group.propval );
1824 return r;
1827 /******************** Selection Tree ***************************************/
1829 struct msi_selection_tree_info
1831 msi_dialog *dialog;
1832 HWND hwnd;
1833 WNDPROC oldproc;
1834 HTREEITEM selected;
1837 static void
1838 msi_seltree_sync_item_state( HWND hwnd, MSIFEATURE *feature, HTREEITEM hItem )
1840 TVITEMW tvi;
1841 DWORD index = feature->Action;
1843 TRACE("Feature %s -> %d %d %d\n", debugstr_w(feature->Title),
1844 feature->Installed, feature->Action, feature->ActionRequest);
1846 if (index == INSTALLSTATE_UNKNOWN)
1847 index = INSTALLSTATE_ABSENT;
1849 tvi.mask = TVIF_STATE;
1850 tvi.hItem = hItem;
1851 tvi.state = INDEXTOSTATEIMAGEMASK( index );
1852 tvi.stateMask = TVIS_STATEIMAGEMASK;
1854 SendMessageW( hwnd, TVM_SETITEMW, 0, (LPARAM) &tvi );
1857 static UINT
1858 msi_seltree_popup_menu( HWND hwnd, INT x, INT y )
1860 HMENU hMenu;
1861 INT r;
1863 /* create a menu to display */
1864 hMenu = CreatePopupMenu();
1866 /* FIXME: load strings from resources */
1867 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_LOCAL, "Install feature locally");
1868 AppendMenuA( hMenu, MF_GRAYED, 0x1000, "Install entire feature");
1869 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ADVERTISED, "Install on demand");
1870 AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ABSENT, "Don't install");
1871 r = TrackPopupMenu( hMenu, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RETURNCMD,
1872 x, y, 0, hwnd, NULL );
1873 DestroyMenu( hMenu );
1874 return r;
1877 static MSIFEATURE *
1878 msi_seltree_feature_from_item( HWND hwnd, HTREEITEM hItem )
1880 TVITEMW tvi;
1882 /* get the feature from the item */
1883 memset( &tvi, 0, sizeof tvi );
1884 tvi.hItem = hItem;
1885 tvi.mask = TVIF_PARAM | TVIF_HANDLE;
1886 SendMessageW( hwnd, TVM_GETITEMW, 0, (LPARAM) &tvi );
1888 return (MSIFEATURE*) tvi.lParam;
1891 static LRESULT
1892 msi_seltree_menu( HWND hwnd, HTREEITEM hItem )
1894 struct msi_selection_tree_info *info;
1895 MSIFEATURE *feature;
1896 MSIPACKAGE *package;
1897 union {
1898 RECT rc;
1899 POINT pt[2];
1900 HTREEITEM hItem;
1901 } u;
1902 UINT r;
1904 info = GetPropW(hwnd, szButtonData);
1905 package = info->dialog->package;
1907 feature = msi_seltree_feature_from_item( hwnd, hItem );
1908 if (!feature)
1910 ERR("item %p feature was NULL\n", hItem);
1911 return 0;
1914 /* get the item's rectangle to put the menu just below it */
1915 u.hItem = hItem;
1916 SendMessageW( hwnd, TVM_GETITEMRECT, 0, (LPARAM) &u.rc );
1917 MapWindowPoints( hwnd, NULL, u.pt, 2 );
1919 r = msi_seltree_popup_menu( hwnd, u.rc.left, u.rc.top );
1921 switch (r)
1923 case INSTALLSTATE_LOCAL:
1924 case INSTALLSTATE_ADVERTISED:
1925 case INSTALLSTATE_ABSENT:
1926 msi_feature_set_state(package, feature, r);
1927 break;
1928 default:
1929 FIXME("select feature and all children\n");
1932 /* update */
1933 msi_seltree_sync_item_state( hwnd, feature, hItem );
1934 ACTION_UpdateComponentStates( package, feature->Feature );
1936 return 0;
1939 static MSIFEATURE *msi_seltree_get_selected_feature( msi_control *control )
1941 struct msi_selection_tree_info *info = GetPropW(control->hwnd, szButtonData);
1942 return msi_seltree_feature_from_item( control->hwnd, info->selected );
1945 static LRESULT WINAPI
1946 MSISelectionTree_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1948 struct msi_selection_tree_info *info;
1949 TVHITTESTINFO tvhti;
1950 HRESULT r;
1952 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1954 info = GetPropW(hWnd, szButtonData);
1956 switch( msg )
1958 case WM_LBUTTONDOWN:
1959 tvhti.pt.x = (short)LOWORD( lParam );
1960 tvhti.pt.y = (short)HIWORD( lParam );
1961 tvhti.flags = 0;
1962 tvhti.hItem = 0;
1963 r = CallWindowProcW(info->oldproc, hWnd, TVM_HITTEST, 0, (LPARAM) &tvhti );
1964 if (tvhti.flags & TVHT_ONITEMSTATEICON)
1965 return msi_seltree_menu( hWnd, tvhti.hItem );
1966 break;
1969 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1971 switch( msg )
1973 case WM_NCDESTROY:
1974 msi_free( info );
1975 RemovePropW( hWnd, szButtonData );
1976 break;
1978 return r;
1981 static void
1982 msi_seltree_add_child_features( MSIPACKAGE *package, HWND hwnd,
1983 LPCWSTR parent, HTREEITEM hParent )
1985 struct msi_selection_tree_info *info = GetPropW( hwnd, szButtonData );
1986 MSIFEATURE *feature;
1987 TVINSERTSTRUCTW tvis;
1988 HTREEITEM hitem, hfirst = NULL;
1990 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
1992 if ( lstrcmpW( parent, feature->Feature_Parent ) )
1993 continue;
1995 if ( !feature->Title )
1996 continue;
1998 if ( !feature->Display )
1999 continue;
2001 memset( &tvis, 0, sizeof tvis );
2002 tvis.hParent = hParent;
2003 tvis.hInsertAfter = TVI_LAST;
2004 tvis.u.item.mask = TVIF_TEXT | TVIF_PARAM;
2005 tvis.u.item.pszText = feature->Title;
2006 tvis.u.item.lParam = (LPARAM) feature;
2008 hitem = (HTREEITEM) SendMessageW( hwnd, TVM_INSERTITEMW, 0, (LPARAM) &tvis );
2009 if (!hitem)
2010 continue;
2012 if (!hfirst)
2013 hfirst = hitem;
2015 msi_seltree_sync_item_state( hwnd, feature, hitem );
2016 msi_seltree_add_child_features( package, hwnd,
2017 feature->Feature, hitem );
2019 /* the node is expanded if Display is odd */
2020 if ( feature->Display % 2 != 0 )
2021 SendMessageW( hwnd, TVM_EXPAND, TVE_EXPAND, (LPARAM) hitem );
2024 /* select the first item */
2025 SendMessageW( hwnd, TVM_SELECTITEM, TVGN_CARET | TVGN_DROPHILITE, (LPARAM) hfirst );
2026 info->selected = hfirst;
2029 static void msi_seltree_create_imagelist( HWND hwnd )
2031 const int bm_width = 32, bm_height = 16, bm_count = 3;
2032 const int bm_resource = 0x1001;
2033 HIMAGELIST himl;
2034 int i;
2035 HBITMAP hbmp;
2037 himl = ImageList_Create( bm_width, bm_height, FALSE, 4, 0 );
2038 if (!himl)
2040 ERR("failed to create image list\n");
2041 return;
2044 for (i=0; i<bm_count; i++)
2046 hbmp = LoadBitmapW( msi_hInstance, MAKEINTRESOURCEW(i+bm_resource) );
2047 if (!hbmp)
2049 ERR("failed to load bitmap %d\n", i);
2050 break;
2054 * Add a dummy bitmap at offset zero because the treeview
2055 * can't use it as a state mask (zero means no user state).
2057 if (!i)
2058 ImageList_Add( himl, hbmp, NULL );
2060 ImageList_Add( himl, hbmp, NULL );
2063 SendMessageW( hwnd, TVM_SETIMAGELIST, TVSIL_STATE, (LPARAM)himl );
2066 static UINT msi_dialog_seltree_handler( msi_dialog *dialog,
2067 msi_control *control, WPARAM param )
2069 struct msi_selection_tree_info *info = GetPropW( control->hwnd, szButtonData );
2070 LPNMTREEVIEWW tv = (LPNMTREEVIEWW)param;
2071 MSIRECORD *row, *rec;
2072 MSIFOLDER *folder;
2073 MSIFEATURE *feature;
2074 LPCWSTR dir, title = NULL;
2075 UINT r = ERROR_SUCCESS;
2077 static const WCHAR select[] = {
2078 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2079 '`','F','e','a','t','u','r','e','`',' ','W','H','E','R','E',' ',
2080 '`','T','i','t','l','e','`',' ','=',' ','\'','%','s','\'',0
2083 if (tv->hdr.code != TVN_SELCHANGINGW)
2084 return ERROR_SUCCESS;
2086 info->selected = tv->itemNew.hItem;
2088 if (!(tv->itemNew.mask & TVIF_TEXT))
2090 feature = msi_seltree_feature_from_item( control->hwnd, tv->itemNew.hItem );
2091 if (feature)
2092 title = feature->Title;
2094 else
2095 title = tv->itemNew.pszText;
2097 row = MSI_QueryGetRecord( dialog->package->db, select, title );
2098 if (!row)
2099 return ERROR_FUNCTION_FAILED;
2101 rec = MSI_CreateRecord( 1 );
2103 MSI_RecordSetStringW( rec, 1, MSI_RecordGetString( row, 4 ) );
2104 ControlEvent_FireSubscribedEvent( dialog->package, szSelectionDescription, rec );
2106 dir = MSI_RecordGetString( row, 7 );
2107 folder = get_loaded_folder( dialog->package, dir );
2108 if (!folder)
2110 r = ERROR_FUNCTION_FAILED;
2111 goto done;
2114 MSI_RecordSetStringW( rec, 1, folder->ResolvedTarget );
2115 ControlEvent_FireSubscribedEvent( dialog->package, szSelectionPath, rec );
2117 done:
2118 msiobj_release(&row->hdr);
2119 msiobj_release(&rec->hdr);
2121 return r;
2124 static UINT msi_dialog_selection_tree( msi_dialog *dialog, MSIRECORD *rec )
2126 msi_control *control;
2127 LPCWSTR prop;
2128 MSIPACKAGE *package = dialog->package;
2129 DWORD style;
2130 struct msi_selection_tree_info *info;
2132 info = msi_alloc( sizeof *info );
2133 if (!info)
2134 return ERROR_FUNCTION_FAILED;
2136 /* create the treeview control */
2137 style = TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT;
2138 style |= WS_GROUP | WS_VSCROLL;
2139 control = msi_dialog_add_control( dialog, rec, WC_TREEVIEWW, style );
2140 if (!control)
2142 msi_free(info);
2143 return ERROR_FUNCTION_FAILED;
2146 control->handler = msi_dialog_seltree_handler;
2147 control->attributes = MSI_RecordGetInteger( rec, 8 );
2148 prop = MSI_RecordGetString( rec, 9 );
2149 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2151 /* subclass */
2152 info->dialog = dialog;
2153 info->hwnd = control->hwnd;
2154 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2155 (LONG_PTR)MSISelectionTree_WndProc );
2156 SetPropW( control->hwnd, szButtonData, info );
2158 ControlEvent_SubscribeToEvent( dialog->package, dialog,
2159 szSelectionPath, control->name, szProperty );
2161 /* initialize it */
2162 msi_seltree_create_imagelist( control->hwnd );
2163 msi_seltree_add_child_features( package, control->hwnd, NULL, NULL );
2165 return ERROR_SUCCESS;
2168 /******************** Group Box ***************************************/
2170 static UINT msi_dialog_group_box( msi_dialog *dialog, MSIRECORD *rec )
2172 msi_control *control;
2173 DWORD style;
2175 style = BS_GROUPBOX | WS_CHILD | WS_GROUP;
2176 control = msi_dialog_add_control( dialog, rec, WC_BUTTONW, style );
2177 if (!control)
2178 return ERROR_FUNCTION_FAILED;
2180 return ERROR_SUCCESS;
2183 /******************** List Box ***************************************/
2185 struct msi_listbox_info
2187 msi_dialog *dialog;
2188 HWND hwnd;
2189 WNDPROC oldproc;
2190 DWORD num_items;
2191 DWORD addpos_items;
2192 LPWSTR *items;
2195 static LRESULT WINAPI MSIListBox_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2197 struct msi_listbox_info *info;
2198 LRESULT r;
2199 DWORD j;
2201 TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
2203 info = GetPropW( hWnd, szButtonData );
2204 if (!info)
2205 return 0;
2207 r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
2209 switch( msg )
2211 case WM_NCDESTROY:
2212 for (j = 0; j < info->num_items; j++)
2213 msi_free( info->items[j] );
2214 msi_free( info->items );
2215 msi_free( info );
2216 RemovePropW( hWnd, szButtonData );
2217 break;
2220 return r;
2223 static UINT msi_listbox_add_item( MSIRECORD *rec, LPVOID param )
2225 struct msi_listbox_info *info = param;
2226 LPCWSTR value, text;
2227 int pos;
2229 value = MSI_RecordGetString( rec, 3 );
2230 text = MSI_RecordGetString( rec, 4 );
2232 info->items[info->addpos_items] = strdupW( value );
2234 pos = SendMessageW( info->hwnd, LB_ADDSTRING, 0, (LPARAM)text );
2235 SendMessageW( info->hwnd, LB_SETITEMDATA, pos, (LPARAM)info->items[info->addpos_items] );
2236 info->addpos_items++;
2237 return ERROR_SUCCESS;
2240 static UINT msi_listbox_add_items( struct msi_listbox_info *info, LPCWSTR property )
2242 UINT r;
2243 MSIQUERY *view = NULL;
2244 DWORD count;
2246 static const WCHAR query[] = {
2247 'S','E','L','E','C','T',' ','*',' ',
2248 'F','R','O','M',' ','`','L','i','s','t','B','o','x','`',' ',
2249 'W','H','E','R','E',' ',
2250 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',' ',
2251 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','`',0
2254 r = MSI_OpenQuery( info->dialog->package->db, &view, query, property );
2255 if ( r != ERROR_SUCCESS )
2256 return r;
2258 /* just get the number of records */
2259 count = 0;
2260 r = MSI_IterateRecords( view, &count, NULL, NULL );
2262 info->num_items = count;
2263 info->items = msi_alloc( sizeof(*info->items) * count );
2265 r = MSI_IterateRecords( view, NULL, msi_listbox_add_item, info );
2266 msiobj_release( &view->hdr );
2268 return r;
2271 static UINT msi_dialog_listbox_handler( msi_dialog *dialog,
2272 msi_control *control, WPARAM param )
2274 struct msi_listbox_info *info;
2275 int index;
2276 LPCWSTR value;
2278 if( HIWORD(param) != LBN_SELCHANGE )
2279 return ERROR_SUCCESS;
2281 info = GetPropW( control->hwnd, szButtonData );
2282 index = SendMessageW( control->hwnd, LB_GETCURSEL, 0, 0 );
2283 value = (LPCWSTR) SendMessageW( control->hwnd, LB_GETITEMDATA, index, 0 );
2285 MSI_SetPropertyW( info->dialog->package,
2286 control->property, value );
2287 msi_dialog_evaluate_control_conditions( info->dialog );
2289 return ERROR_SUCCESS;
2292 static UINT msi_dialog_list_box( msi_dialog *dialog, MSIRECORD *rec )
2294 struct msi_listbox_info *info;
2295 msi_control *control;
2296 DWORD attributes, style;
2297 LPCWSTR prop;
2299 info = msi_alloc( sizeof *info );
2300 if (!info)
2301 return ERROR_FUNCTION_FAILED;
2303 style = WS_TABSTOP | WS_GROUP | WS_CHILD | LBS_NOTIFY | WS_VSCROLL | WS_BORDER;
2304 attributes = MSI_RecordGetInteger( rec, 8 );
2305 if (~attributes & msidbControlAttributesSorted)
2306 style |= LBS_SORT;
2308 control = msi_dialog_add_control( dialog, rec, WC_LISTBOXW, style );
2309 if (!control)
2311 msi_free(info);
2312 return ERROR_FUNCTION_FAILED;
2315 control->handler = msi_dialog_listbox_handler;
2317 prop = MSI_RecordGetString( rec, 9 );
2318 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2320 /* subclass */
2321 info->dialog = dialog;
2322 info->hwnd = control->hwnd;
2323 info->items = NULL;
2324 info->addpos_items = 0;
2325 info->oldproc = (WNDPROC)SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
2326 (LONG_PTR)MSIListBox_WndProc );
2327 SetPropW( control->hwnd, szButtonData, info );
2329 if ( control->property )
2330 msi_listbox_add_items( info, control->property );
2332 return ERROR_SUCCESS;
2335 /******************** Directory Combo ***************************************/
2337 static void msi_dialog_update_directory_combo( msi_dialog *dialog, msi_control *control )
2339 LPWSTR prop, path;
2340 BOOL indirect;
2342 if (!control && !(control = msi_dialog_find_control_by_type( dialog, szDirectoryCombo )))
2343 return;
2345 indirect = control->attributes & msidbControlAttributesIndirect;
2346 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2347 path = msi_dialog_dup_property( dialog, prop, TRUE );
2349 PathStripPathW( path );
2350 PathRemoveBackslashW( path );
2352 SendMessageW( control->hwnd, CB_INSERTSTRING, 0, (LPARAM)path );
2353 SendMessageW( control->hwnd, CB_SETCURSEL, 0, 0 );
2355 msi_free( path );
2356 msi_free( prop );
2359 static UINT msi_dialog_directory_combo( msi_dialog *dialog, MSIRECORD *rec )
2361 msi_control *control;
2362 LPCWSTR prop;
2363 DWORD style;
2365 /* FIXME: use CBS_OWNERDRAWFIXED and add owner draw code */
2366 style = CBS_DROPDOWNLIST | CBS_HASSTRINGS | WS_CHILD |
2367 WS_GROUP | WS_TABSTOP | WS_VSCROLL;
2368 control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
2369 if (!control)
2370 return ERROR_FUNCTION_FAILED;
2372 control->attributes = MSI_RecordGetInteger( rec, 8 );
2373 prop = MSI_RecordGetString( rec, 9 );
2374 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2376 msi_dialog_update_directory_combo( dialog, control );
2378 return ERROR_SUCCESS;
2381 /******************** Directory List ***************************************/
2383 static void msi_dialog_update_directory_list( msi_dialog *dialog, msi_control *control )
2385 WCHAR dir_spec[MAX_PATH];
2386 WIN32_FIND_DATAW wfd;
2387 LPWSTR prop, path;
2388 BOOL indirect;
2389 LVITEMW item;
2390 HANDLE file;
2392 static const WCHAR asterisk[] = {'*',0};
2393 static const WCHAR dot[] = {'.',0};
2394 static const WCHAR dotdot[] = {'.','.',0};
2396 if (!control && !(control = msi_dialog_find_control_by_type( dialog, szDirectoryList )))
2397 return;
2399 /* clear the list-view */
2400 SendMessageW( control->hwnd, LVM_DELETEALLITEMS, 0, 0 );
2402 indirect = control->attributes & msidbControlAttributesIndirect;
2403 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2404 path = msi_dialog_dup_property( dialog, prop, TRUE );
2406 lstrcpyW( dir_spec, path );
2407 lstrcatW( dir_spec, asterisk );
2409 file = FindFirstFileW( dir_spec, &wfd );
2410 if ( file == INVALID_HANDLE_VALUE )
2411 return;
2415 if ( wfd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY )
2416 continue;
2418 if ( !lstrcmpW( wfd.cFileName, dot ) || !lstrcmpW( wfd.cFileName, dotdot ) )
2419 continue;
2421 item.mask = LVIF_TEXT;
2422 item.cchTextMax = MAX_PATH;
2423 item.iItem = 0;
2424 item.iSubItem = 0;
2425 item.pszText = wfd.cFileName;
2427 SendMessageW( control->hwnd, LVM_INSERTITEMW, 0, (LPARAM)&item );
2428 } while ( FindNextFileW( file, &wfd ) );
2430 msi_free( prop );
2431 msi_free( path );
2432 FindClose( file );
2435 UINT msi_dialog_directorylist_up( msi_dialog *dialog )
2437 msi_control *control;
2438 LPWSTR prop, path, ptr;
2439 BOOL indirect;
2441 control = msi_dialog_find_control_by_type( dialog, szDirectoryList );
2442 indirect = control->attributes & msidbControlAttributesIndirect;
2443 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2444 path = msi_dialog_dup_property( dialog, prop, TRUE );
2446 /* strip off the last directory */
2447 ptr = PathFindFileNameW( path );
2448 if (ptr != path) *(ptr - 1) = '\0';
2449 PathAddBackslashW( path );
2451 MSI_SetPropertyW( dialog->package, prop, path );
2453 msi_dialog_update_directory_list( dialog, NULL );
2454 msi_dialog_update_directory_combo( dialog, NULL );
2455 msi_dialog_update_pathedit( dialog, NULL );
2457 msi_free( path );
2458 msi_free( prop );
2460 return ERROR_SUCCESS;
2463 static UINT msi_dialog_dirlist_handler( msi_dialog *dialog,
2464 msi_control *control, WPARAM param )
2466 LPNMHDR nmhdr = (LPNMHDR)param;
2467 WCHAR new_path[MAX_PATH];
2468 WCHAR text[MAX_PATH];
2469 LPWSTR path, prop;
2470 BOOL indirect;
2471 LVITEMW item;
2472 int index;
2474 static const WCHAR backslash[] = {'\\',0};
2476 if (nmhdr->code != LVN_ITEMACTIVATE)
2477 return ERROR_SUCCESS;
2479 index = SendMessageW( control->hwnd, LVM_GETNEXTITEM, -1, LVNI_SELECTED );
2480 if ( index < 0 )
2482 ERR("No list-view item selected!\n");
2483 return ERROR_FUNCTION_FAILED;
2486 item.iSubItem = 0;
2487 item.pszText = text;
2488 item.cchTextMax = MAX_PATH;
2489 SendMessageW( control->hwnd, LVM_GETITEMTEXTW, index, (LPARAM)&item );
2491 indirect = control->attributes & msidbControlAttributesIndirect;
2492 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2493 path = msi_dialog_dup_property( dialog, prop, TRUE );
2495 lstrcpyW( new_path, path );
2496 lstrcatW( new_path, text );
2497 lstrcatW( new_path, backslash );
2499 MSI_SetPropertyW( dialog->package, prop, new_path );
2501 msi_dialog_update_directory_list( dialog, NULL );
2502 msi_dialog_update_directory_combo( dialog, NULL );
2503 msi_dialog_update_pathedit( dialog, NULL );
2505 msi_free( prop );
2506 msi_free( path );
2507 return ERROR_SUCCESS;
2510 static UINT msi_dialog_directory_list( msi_dialog *dialog, MSIRECORD *rec )
2512 msi_control *control;
2513 LPCWSTR prop;
2514 DWORD style;
2516 style = LVS_LIST | WS_VSCROLL | LVS_SHAREIMAGELISTS |
2517 LVS_AUTOARRANGE | LVS_SINGLESEL | WS_BORDER |
2518 LVS_SORTASCENDING | WS_CHILD | WS_GROUP | WS_TABSTOP;
2519 control = msi_dialog_add_control( dialog, rec, WC_LISTVIEWW, style );
2520 if (!control)
2521 return ERROR_FUNCTION_FAILED;
2523 control->attributes = MSI_RecordGetInteger( rec, 8 );
2524 control->handler = msi_dialog_dirlist_handler;
2525 prop = MSI_RecordGetString( rec, 9 );
2526 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2528 /* double click to activate an item in the list */
2529 SendMessageW( control->hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE,
2530 0, LVS_EX_TWOCLICKACTIVATE );
2532 msi_dialog_update_directory_list( dialog, control );
2534 return ERROR_SUCCESS;
2537 /******************** VolumeCost List ***************************************/
2539 static BOOL str_is_number( LPCWSTR str )
2541 int i;
2543 for (i = 0; i < lstrlenW( str ); i++)
2544 if (!isdigitW(str[i]))
2545 return FALSE;
2547 return TRUE;
2550 static const WCHAR column_keys[][80] =
2552 {'V','o','l','u','m','e','C','o','s','t','V','o','l','u','m','e',0},
2553 {'V','o','l','u','m','e','C','o','s','t','S','i','z','e',0},
2554 {'V','o','l','u','m','e','C','o','s','t','A','v','a','i','l','a','b','l','e',0},
2555 {'V','o','l','u','m','e','C','o','s','t','R','e','q','u','i','r','e','d',0},
2556 {'V','o','l','u','m','e','C','o','s','t','D','i','f','f','e','r','e','n','c','e',0}
2559 static void msi_dialog_vcl_add_columns( msi_dialog *dialog, msi_control *control, MSIRECORD *rec )
2561 LPCWSTR text = MSI_RecordGetString( rec, 10 );
2562 LPCWSTR begin = text, end;
2563 WCHAR *num;
2564 LVCOLUMNW lvc;
2565 DWORD count = 0;
2567 static const WCHAR zero[] = {'0',0};
2568 static const WCHAR negative[] = {'-',0};
2570 if (!text) return;
2572 while ((begin = strchrW( begin, '{' )) && count < 5)
2574 if (!(end = strchrW( begin, '}' )))
2575 return;
2577 num = msi_alloc( (end-begin+1)*sizeof(WCHAR) );
2578 if (!num)
2579 return;
2581 lstrcpynW( num, begin + 1, end - begin );
2582 begin += end - begin + 1;
2584 /* empty braces or '0' hides the column */
2585 if ( !num[0] || !lstrcmpW( num, zero ) )
2587 count++;
2588 msi_free( num );
2589 continue;
2592 /* the width must be a positive number
2593 * if a width is invalid, all remaining columns are hidden
2595 if ( !strncmpW( num, negative, 1 ) || !str_is_number( num ) ) {
2596 msi_free( num );
2597 return;
2600 ZeroMemory( &lvc, sizeof(lvc) );
2601 lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
2602 lvc.cx = atolW( num );
2603 lvc.pszText = msi_dialog_get_uitext( dialog, column_keys[count] );
2605 SendMessageW( control->hwnd, LVM_INSERTCOLUMNW, count++, (LPARAM)&lvc );
2606 msi_free( lvc.pszText );
2607 msi_free( num );
2611 static void msi_dialog_vcl_add_drives( msi_dialog *dialog, msi_control *control )
2613 ULARGE_INTEGER total, free;
2614 WCHAR size_text[MAX_PATH];
2615 LPWSTR drives, ptr;
2616 LVITEMW lvitem;
2617 DWORD size;
2618 int i = 0;
2620 size = GetLogicalDriveStringsW( 0, NULL );
2621 if ( !size ) return;
2623 drives = msi_alloc( (size + 1) * sizeof(WCHAR) );
2624 if ( !drives ) return;
2626 GetLogicalDriveStringsW( size, drives );
2628 ptr = drives;
2629 while (*ptr)
2631 lvitem.mask = LVIF_TEXT;
2632 lvitem.iItem = i;
2633 lvitem.iSubItem = 0;
2634 lvitem.pszText = ptr;
2635 lvitem.cchTextMax = lstrlenW(ptr) + 1;
2636 SendMessageW( control->hwnd, LVM_INSERTITEMW, 0, (LPARAM)&lvitem );
2638 GetDiskFreeSpaceExW(ptr, &free, &total, NULL);
2640 StrFormatByteSizeW(total.QuadPart, size_text, MAX_PATH);
2641 lvitem.iSubItem = 1;
2642 lvitem.pszText = size_text;
2643 lvitem.cchTextMax = lstrlenW(size_text) + 1;
2644 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
2646 StrFormatByteSizeW(free.QuadPart, size_text, MAX_PATH);
2647 lvitem.iSubItem = 2;
2648 lvitem.pszText = size_text;
2649 lvitem.cchTextMax = lstrlenW(size_text) + 1;
2650 SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );
2652 ptr += lstrlenW(ptr) + 1;
2653 i++;
2656 msi_free( drives );
2659 static UINT msi_dialog_volumecost_list( msi_dialog *dialog, MSIRECORD *rec )
2661 msi_control *control;
2662 DWORD style;
2664 style = LVS_REPORT | WS_VSCROLL | WS_HSCROLL | LVS_SHAREIMAGELISTS |
2665 LVS_AUTOARRANGE | LVS_SINGLESEL | WS_BORDER |
2666 WS_CHILD | WS_TABSTOP | WS_GROUP;
2667 control = msi_dialog_add_control( dialog, rec, WC_LISTVIEWW, style );
2668 if (!control)
2669 return ERROR_FUNCTION_FAILED;
2671 msi_dialog_vcl_add_columns( dialog, control, rec );
2672 msi_dialog_vcl_add_drives( dialog, control );
2674 return ERROR_SUCCESS;
2677 /******************** VolumeSelect Combo ***************************************/
2679 static UINT msi_dialog_volsel_handler( msi_dialog *dialog,
2680 msi_control *control, WPARAM param )
2682 WCHAR text[MAX_PATH];
2683 LPWSTR prop;
2684 BOOL indirect;
2685 int index;
2687 if (HIWORD(param) != CBN_SELCHANGE)
2688 return ERROR_SUCCESS;
2690 index = SendMessageW( control->hwnd, CB_GETCURSEL, 0, 0 );
2691 if ( index == CB_ERR )
2693 ERR("No ComboBox item selected!\n");
2694 return ERROR_FUNCTION_FAILED;
2697 SendMessageW( control->hwnd, CB_GETLBTEXT, index, (LPARAM)text );
2699 indirect = control->attributes & msidbControlAttributesIndirect;
2700 prop = msi_dialog_dup_property( dialog, control->property, indirect );
2702 MSI_SetPropertyW( dialog->package, prop, text );
2704 msi_free( prop );
2705 return ERROR_SUCCESS;
2708 static void msi_dialog_vsc_add_drives( msi_dialog *dialog, msi_control *control )
2710 LPWSTR drives, ptr;
2711 DWORD size;
2713 size = GetLogicalDriveStringsW( 0, NULL );
2714 if ( !size ) return;
2716 drives = msi_alloc( (size + 1) * sizeof(WCHAR) );
2717 if ( !drives ) return;
2719 GetLogicalDriveStringsW( size, drives );
2721 ptr = drives;
2722 while (*ptr)
2724 SendMessageW( control->hwnd, CB_ADDSTRING, 0, (LPARAM)ptr );
2725 ptr += lstrlenW(ptr) + 1;
2728 msi_free( drives );
2731 static UINT msi_dialog_volumeselect_combo( msi_dialog *dialog, MSIRECORD *rec )
2733 msi_control *control;
2734 LPCWSTR prop;
2735 DWORD style;
2737 /* FIXME: CBS_OWNERDRAWFIXED */
2738 style = WS_CHILD | WS_VISIBLE | WS_GROUP | WS_TABSTOP |
2739 CBS_DROPDOWNLIST | CBS_SORT | CBS_HASSTRINGS |
2740 WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
2741 control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
2742 if (!control)
2743 return ERROR_FUNCTION_FAILED;
2745 control->attributes = MSI_RecordGetInteger( rec, 8 );
2746 control->handler = msi_dialog_volsel_handler;
2747 prop = MSI_RecordGetString( rec, 9 );
2748 control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2750 msi_dialog_vsc_add_drives( dialog, control );
2752 return ERROR_SUCCESS;
2755 static const struct control_handler msi_dialog_handler[] =
2757 { szText, msi_dialog_text_control },
2758 { szPushButton, msi_dialog_button_control },
2759 { szLine, msi_dialog_line_control },
2760 { szBitmap, msi_dialog_bitmap_control },
2761 { szCheckBox, msi_dialog_checkbox_control },
2762 { szScrollableText, msi_dialog_scrolltext_control },
2763 { szComboBox, msi_dialog_combo_control },
2764 { szEdit, msi_dialog_edit_control },
2765 { szMaskedEdit, msi_dialog_maskedit_control },
2766 { szPathEdit, msi_dialog_pathedit_control },
2767 { szProgressBar, msi_dialog_progress_bar },
2768 { szRadioButtonGroup, msi_dialog_radiogroup_control },
2769 { szIcon, msi_dialog_icon_control },
2770 { szSelectionTree, msi_dialog_selection_tree },
2771 { szGroupBox, msi_dialog_group_box },
2772 { szListBox, msi_dialog_list_box },
2773 { szDirectoryCombo, msi_dialog_directory_combo },
2774 { szDirectoryList, msi_dialog_directory_list },
2775 { szVolumeCostList, msi_dialog_volumecost_list },
2776 { szVolumeSelectCombo, msi_dialog_volumeselect_combo },
2779 #define NUM_CONTROL_TYPES (sizeof msi_dialog_handler/sizeof msi_dialog_handler[0])
2781 static UINT msi_dialog_create_controls( MSIRECORD *rec, LPVOID param )
2783 msi_dialog *dialog = param;
2784 LPCWSTR control_type;
2785 UINT i;
2787 /* find and call the function that can create this type of control */
2788 control_type = MSI_RecordGetString( rec, 3 );
2789 for( i=0; i<NUM_CONTROL_TYPES; i++ )
2790 if (!strcmpiW( msi_dialog_handler[i].control_type, control_type ))
2791 break;
2792 if( i != NUM_CONTROL_TYPES )
2793 msi_dialog_handler[i].func( dialog, rec );
2794 else
2795 ERR("no handler for element type %s\n", debugstr_w(control_type));
2797 return ERROR_SUCCESS;
2800 static UINT msi_dialog_fill_controls( msi_dialog *dialog )
2802 static const WCHAR query[] = {
2803 'S','E','L','E','C','T',' ','*',' ',
2804 'F','R','O','M',' ','C','o','n','t','r','o','l',' ',
2805 'W','H','E','R','E',' ',
2806 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
2807 UINT r;
2808 MSIQUERY *view = NULL;
2809 MSIPACKAGE *package = dialog->package;
2811 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
2813 /* query the Control table for all the elements of the control */
2814 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
2815 if( r != ERROR_SUCCESS )
2817 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
2818 return ERROR_INVALID_PARAMETER;
2821 r = MSI_IterateRecords( view, 0, msi_dialog_create_controls, dialog );
2822 msiobj_release( &view->hdr );
2824 return r;
2827 static UINT msi_dialog_set_control_condition( MSIRECORD *rec, LPVOID param )
2829 static const WCHAR szHide[] = { 'H','i','d','e',0 };
2830 static const WCHAR szShow[] = { 'S','h','o','w',0 };
2831 static const WCHAR szDisable[] = { 'D','i','s','a','b','l','e',0 };
2832 static const WCHAR szEnable[] = { 'E','n','a','b','l','e',0 };
2833 static const WCHAR szDefault[] = { 'D','e','f','a','u','l','t',0 };
2834 msi_dialog *dialog = param;
2835 msi_control *control;
2836 LPCWSTR name, action, condition;
2837 UINT r;
2839 name = MSI_RecordGetString( rec, 2 );
2840 action = MSI_RecordGetString( rec, 3 );
2841 condition = MSI_RecordGetString( rec, 4 );
2842 r = MSI_EvaluateConditionW( dialog->package, condition );
2843 control = msi_dialog_find_control( dialog, name );
2844 if( r == MSICONDITION_TRUE && control )
2846 TRACE("%s control %s\n", debugstr_w(action), debugstr_w(name));
2848 /* FIXME: case sensitive? */
2849 if(!lstrcmpW(action, szHide))
2850 ShowWindow(control->hwnd, SW_HIDE);
2851 else if(!strcmpW(action, szShow))
2852 ShowWindow(control->hwnd, SW_SHOW);
2853 else if(!strcmpW(action, szDisable))
2854 EnableWindow(control->hwnd, FALSE);
2855 else if(!strcmpW(action, szEnable))
2856 EnableWindow(control->hwnd, TRUE);
2857 else if(!strcmpW(action, szDefault))
2858 SetFocus(control->hwnd);
2859 else
2860 FIXME("Unhandled action %s\n", debugstr_w(action));
2863 return ERROR_SUCCESS;
2866 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog )
2868 static const WCHAR query[] = {
2869 'S','E','L','E','C','T',' ','*',' ',
2870 'F','R','O','M',' ',
2871 'C','o','n','t','r','o','l','C','o','n','d','i','t','i','o','n',' ',
2872 'W','H','E','R','E',' ',
2873 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0
2875 UINT r;
2876 MSIQUERY *view = NULL;
2877 MSIPACKAGE *package = dialog->package;
2879 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
2881 /* query the Control table for all the elements of the control */
2882 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
2883 if( r != ERROR_SUCCESS )
2884 return ERROR_SUCCESS;
2886 r = MSI_IterateRecords( view, 0, msi_dialog_set_control_condition, dialog );
2887 msiobj_release( &view->hdr );
2889 return r;
2892 UINT msi_dialog_reset( msi_dialog *dialog )
2894 /* FIXME: should restore the original values of any properties we changed */
2895 return msi_dialog_evaluate_control_conditions( dialog );
2898 /* figure out the height of 10 point MS Sans Serif */
2899 static INT msi_dialog_get_sans_serif_height( HWND hwnd )
2901 static const WCHAR szSansSerif[] = {
2902 'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0 };
2903 LOGFONTW lf;
2904 TEXTMETRICW tm;
2905 BOOL r;
2906 LONG height = 0;
2907 HFONT hFont, hOldFont;
2908 HDC hdc;
2910 hdc = GetDC( hwnd );
2911 if (hdc)
2913 memset( &lf, 0, sizeof lf );
2914 lf.lfHeight = MulDiv(10, GetDeviceCaps(hdc, LOGPIXELSY), 72);
2915 strcpyW( lf.lfFaceName, szSansSerif );
2916 hFont = CreateFontIndirectW(&lf);
2917 if (hFont)
2919 hOldFont = SelectObject( hdc, hFont );
2920 r = GetTextMetricsW( hdc, &tm );
2921 if (r)
2922 height = tm.tmHeight;
2923 SelectObject( hdc, hOldFont );
2924 DeleteObject( hFont );
2926 ReleaseDC( hwnd, hdc );
2928 return height;
2931 /* fetch the associated record from the Dialog table */
2932 static MSIRECORD *msi_get_dialog_record( msi_dialog *dialog )
2934 static const WCHAR query[] = {
2935 'S','E','L','E','C','T',' ','*',' ',
2936 'F','R','O','M',' ','D','i','a','l','o','g',' ',
2937 'W','H','E','R','E',' ',
2938 '`','D','i','a','l','o','g','`',' ','=',' ','\'','%','s','\'',0};
2939 MSIPACKAGE *package = dialog->package;
2940 MSIRECORD *rec = NULL;
2942 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
2944 rec = MSI_QueryGetRecord( package->db, query, dialog->name );
2945 if( !rec )
2946 WARN("query failed for dialog %s\n", debugstr_w(dialog->name));
2948 return rec;
2951 static void msi_dialog_adjust_dialog_pos( msi_dialog *dialog, MSIRECORD *rec, LPRECT pos )
2953 static const WCHAR szScreenX[] = {'S','c','r','e','e','n','X',0};
2954 static const WCHAR szScreenY[] = {'S','c','r','e','e','n','Y',0};
2956 UINT xres, yres;
2957 POINT center;
2958 SIZE sz;
2959 LONG style;
2961 center.x = MSI_RecordGetInteger( rec, 2 );
2962 center.y = MSI_RecordGetInteger( rec, 3 );
2964 sz.cx = MSI_RecordGetInteger( rec, 4 );
2965 sz.cy = MSI_RecordGetInteger( rec, 5 );
2967 sz.cx = msi_dialog_scale_unit( dialog, sz.cx );
2968 sz.cy = msi_dialog_scale_unit( dialog, sz.cy );
2970 xres = msi_get_property_int( dialog->package, szScreenX, 0 );
2971 yres = msi_get_property_int( dialog->package, szScreenY, 0 );
2973 center.x = MulDiv( center.x, xres, 100 );
2974 center.y = MulDiv( center.y, yres, 100 );
2976 /* turn the client pos into the window rectangle */
2977 if (dialog->package->center_x && dialog->package->center_y)
2979 pos->left = dialog->package->center_x - sz.cx / 2.0;
2980 pos->right = pos->left + sz.cx;
2981 pos->top = dialog->package->center_y - sz.cy / 2.0;
2982 pos->bottom = pos->top + sz.cy;
2984 else
2986 pos->left = center.x - sz.cx/2;
2987 pos->right = pos->left + sz.cx;
2988 pos->top = center.y - sz.cy/2;
2989 pos->bottom = pos->top + sz.cy;
2991 /* save the center */
2992 dialog->package->center_x = center.x;
2993 dialog->package->center_y = center.y;
2996 dialog->size.cx = sz.cx;
2997 dialog->size.cy = sz.cy;
2999 TRACE("%u %u %u %u\n", pos->left, pos->top, pos->right, pos->bottom);
3001 style = GetWindowLongPtrW( dialog->hwnd, GWL_STYLE );
3002 AdjustWindowRect( pos, style, FALSE );
3005 static BOOL msi_control_set_next( msi_control *control, msi_control *next )
3007 return SetWindowPos( next->hwnd, control->hwnd, 0, 0, 0, 0,
3008 SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOREDRAW |
3009 SWP_NOREPOSITION | SWP_NOSENDCHANGING | SWP_NOSIZE );
3012 static UINT msi_dialog_set_tab_order( msi_dialog *dialog )
3014 msi_control *control, *tab_next;
3016 LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
3018 tab_next = msi_dialog_find_control( dialog, control->tabnext );
3019 if( !tab_next )
3020 continue;
3021 msi_control_set_next( control, tab_next );
3024 return ERROR_SUCCESS;
3027 static void msi_dialog_set_first_control( msi_dialog* dialog, LPCWSTR name )
3029 msi_control *control;
3031 control = msi_dialog_find_control( dialog, name );
3032 if( control )
3033 dialog->hWndFocus = control->hwnd;
3034 else
3035 dialog->hWndFocus = NULL;
3038 static LRESULT msi_dialog_oncreate( HWND hwnd, LPCREATESTRUCTW cs )
3040 static const WCHAR df[] = {
3041 'D','e','f','a','u','l','t','U','I','F','o','n','t',0 };
3042 static const WCHAR dfv[] = {
3043 'M','S',' ','S','h','e','l','l',' ','D','l','g',0 };
3044 msi_dialog *dialog = cs->lpCreateParams;
3045 MSIRECORD *rec = NULL;
3046 LPWSTR title = NULL;
3047 RECT pos;
3049 TRACE("%p %p\n", dialog, dialog->package);
3051 dialog->hwnd = hwnd;
3052 SetWindowLongPtrW( hwnd, GWLP_USERDATA, (LONG_PTR) dialog );
3054 rec = msi_get_dialog_record( dialog );
3055 if( !rec )
3057 TRACE("No record found for dialog %s\n", debugstr_w(dialog->name));
3058 return -1;
3061 dialog->scale = msi_dialog_get_sans_serif_height(dialog->hwnd);
3063 msi_dialog_adjust_dialog_pos( dialog, rec, &pos );
3065 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
3067 dialog->default_font = msi_dup_property( dialog->package, df );
3068 if (!dialog->default_font)
3070 dialog->default_font = strdupW(dfv);
3071 if (!dialog->default_font) return -1;
3074 title = msi_get_deformatted_field( dialog->package, rec, 7 );
3075 SetWindowTextW( hwnd, title );
3076 msi_free( title );
3078 SetWindowPos( hwnd, 0, pos.left, pos.top,
3079 pos.right - pos.left, pos.bottom - pos.top,
3080 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW );
3082 msi_dialog_build_font_list( dialog );
3083 msi_dialog_fill_controls( dialog );
3084 msi_dialog_evaluate_control_conditions( dialog );
3085 msi_dialog_set_tab_order( dialog );
3086 msi_dialog_set_first_control( dialog, MSI_RecordGetString( rec, 8 ) );
3087 msiobj_release( &rec->hdr );
3089 return 0;
3092 static UINT msi_dialog_send_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
3094 LPWSTR event_fmt = NULL, arg_fmt = NULL;
3096 TRACE("Sending control event %s %s\n", debugstr_w(event), debugstr_w(arg));
3098 deformat_string( dialog->package, event, &event_fmt );
3099 deformat_string( dialog->package, arg, &arg_fmt );
3101 dialog->event_handler( dialog->package, event_fmt, arg_fmt, dialog );
3103 msi_free( event_fmt );
3104 msi_free( arg_fmt );
3106 return ERROR_SUCCESS;
3109 static UINT msi_dialog_set_property( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
3111 static const WCHAR szNullArg[] = { '{','}',0 };
3112 LPWSTR p, prop, arg_fmt = NULL;
3113 UINT len;
3115 len = strlenW(event);
3116 prop = msi_alloc( len*sizeof(WCHAR));
3117 strcpyW( prop, &event[1] );
3118 p = strchrW( prop, ']' );
3119 if( p && (p[1] == 0 || p[1] == ' ') )
3121 *p = 0;
3122 if( strcmpW( szNullArg, arg ) )
3123 deformat_string( dialog->package, arg, &arg_fmt );
3124 MSI_SetPropertyW( dialog->package, prop, arg_fmt );
3125 msi_dialog_update_controls( dialog, prop );
3126 msi_free( arg_fmt );
3128 else
3129 ERR("Badly formatted property string - what happens?\n");
3130 msi_free( prop );
3131 return ERROR_SUCCESS;
3134 static UINT msi_dialog_control_event( MSIRECORD *rec, LPVOID param )
3136 msi_dialog *dialog = param;
3137 LPCWSTR condition, event, arg;
3138 UINT r;
3140 condition = MSI_RecordGetString( rec, 5 );
3141 r = MSI_EvaluateConditionW( dialog->package, condition );
3142 if( r == MSICONDITION_TRUE || r == MSICONDITION_NONE )
3144 event = MSI_RecordGetString( rec, 3 );
3145 arg = MSI_RecordGetString( rec, 4 );
3146 if( event[0] == '[' )
3147 msi_dialog_set_property( dialog, event, arg );
3148 else
3149 msi_dialog_send_event( dialog, event, arg );
3152 return ERROR_SUCCESS;
3155 struct rec_list
3157 struct list entry;
3158 MSIRECORD *rec;
3161 static UINT add_rec_to_list( MSIRECORD *rec, LPVOID param )
3163 struct rec_list *add_rec;
3164 struct list *records = param;
3166 msiobj_addref( &rec->hdr );
3168 add_rec = msi_alloc( sizeof( *add_rec ) );
3169 if (!add_rec)
3171 msiobj_release( &rec->hdr );
3172 return ERROR_OUTOFMEMORY;
3175 add_rec->rec = rec;
3176 list_add_tail( records, &add_rec->entry );
3177 return ERROR_SUCCESS;
3180 static inline void remove_rec_from_list( struct rec_list *rec_entry )
3182 msiobj_release( &rec_entry->rec->hdr );
3183 list_remove( &rec_entry->entry );
3184 msi_free( rec_entry );
3187 static UINT msi_dialog_button_handler( msi_dialog *dialog,
3188 msi_control *control, WPARAM param )
3190 static const WCHAR query[] = {
3191 'S','E','L','E','C','T',' ','*',' ',
3192 'F','R','O','M',' ','C','o','n','t','r','o','l','E','v','e','n','t',' ',
3193 'W','H','E','R','E',' ',
3194 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
3195 'A','N','D',' ',
3196 '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',' ',
3197 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','i','n','g','`',0
3199 MSIQUERY *view = NULL;
3200 struct rec_list *rec_entry, *next;
3201 struct list events;
3202 UINT r;
3204 if( HIWORD(param) != BN_CLICKED )
3205 return ERROR_SUCCESS;
3207 list_init( &events );
3209 r = MSI_OpenQuery( dialog->package->db, &view, query,
3210 dialog->name, control->name );
3211 if( r != ERROR_SUCCESS )
3213 ERR("query failed\n");
3214 return 0;
3217 r = MSI_IterateRecords( view, 0, add_rec_to_list, &events );
3218 msiobj_release( &view->hdr );
3219 if (r != ERROR_SUCCESS)
3220 goto done;
3222 /* handle all SetProperty events first */
3223 LIST_FOR_EACH_ENTRY_SAFE( rec_entry, next, &events, struct rec_list, entry )
3225 LPCWSTR event = MSI_RecordGetString( rec_entry->rec, 3 );
3227 if ( event[0] != '[' )
3228 continue;
3230 r = msi_dialog_control_event( rec_entry->rec, dialog );
3231 remove_rec_from_list( rec_entry );
3233 if ( r != ERROR_SUCCESS )
3234 goto done;
3237 /* handle all other events */
3238 LIST_FOR_EACH_ENTRY_SAFE( rec_entry, next, &events, struct rec_list, entry )
3240 r = msi_dialog_control_event( rec_entry->rec, dialog );
3241 remove_rec_from_list( rec_entry );
3243 if ( r != ERROR_SUCCESS )
3244 goto done;
3247 done:
3248 LIST_FOR_EACH_ENTRY_SAFE( rec_entry, next, &events, struct rec_list, entry )
3250 remove_rec_from_list( rec_entry );
3253 return r;
3256 static UINT msi_dialog_get_checkbox_state( msi_dialog *dialog,
3257 msi_control *control )
3259 WCHAR state[2] = { 0 };
3260 DWORD sz = 2;
3262 MSI_GetPropertyW( dialog->package, control->property, state, &sz );
3263 return state[0] ? 1 : 0;
3266 static void msi_dialog_set_checkbox_state( msi_dialog *dialog,
3267 msi_control *control, UINT state )
3269 static const WCHAR szState[] = { '1', 0 };
3270 LPCWSTR val;
3272 /* if uncheck then the property is set to NULL */
3273 if (!state)
3275 MSI_SetPropertyW( dialog->package, control->property, NULL );
3276 return;
3279 /* check for a custom state */
3280 if (control->value && control->value[0])
3281 val = control->value;
3282 else
3283 val = szState;
3285 MSI_SetPropertyW( dialog->package, control->property, val );
3288 static void msi_dialog_checkbox_sync_state( msi_dialog *dialog,
3289 msi_control *control )
3291 UINT state;
3293 state = msi_dialog_get_checkbox_state( dialog, control );
3294 SendMessageW( control->hwnd, BM_SETCHECK,
3295 state ? BST_CHECKED : BST_UNCHECKED, 0 );
3298 static UINT msi_dialog_checkbox_handler( msi_dialog *dialog,
3299 msi_control *control, WPARAM param )
3301 UINT state;
3303 if( HIWORD(param) != BN_CLICKED )
3304 return ERROR_SUCCESS;
3306 TRACE("clicked checkbox %s, set %s\n", debugstr_w(control->name),
3307 debugstr_w(control->property));
3309 state = msi_dialog_get_checkbox_state( dialog, control );
3310 state = state ? 0 : 1;
3311 msi_dialog_set_checkbox_state( dialog, control, state );
3312 msi_dialog_checkbox_sync_state( dialog, control );
3314 return msi_dialog_button_handler( dialog, control, param );
3317 static UINT msi_dialog_edit_handler( msi_dialog *dialog,
3318 msi_control *control, WPARAM param )
3320 LPWSTR buf;
3322 if( HIWORD(param) != EN_CHANGE )
3323 return ERROR_SUCCESS;
3325 TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
3326 debugstr_w(control->property));
3328 buf = msi_get_window_text( control->hwnd );
3329 MSI_SetPropertyW( dialog->package, control->property, buf );
3331 msi_free( buf );
3333 return ERROR_SUCCESS;
3336 static UINT msi_dialog_radiogroup_handler( msi_dialog *dialog,
3337 msi_control *control, WPARAM param )
3339 if( HIWORD(param) != BN_CLICKED )
3340 return ERROR_SUCCESS;
3342 TRACE("clicked radio button %s, set %s\n", debugstr_w(control->name),
3343 debugstr_w(control->property));
3345 MSI_SetPropertyW( dialog->package, control->property, control->name );
3347 return msi_dialog_button_handler( dialog, control, param );
3350 static LRESULT msi_dialog_oncommand( msi_dialog *dialog, WPARAM param, HWND hwnd )
3352 msi_control *control = NULL;
3354 TRACE("%p %p %08lx\n", dialog, hwnd, param);
3356 switch (param)
3358 case 1: /* enter */
3359 control = msi_dialog_find_control( dialog, dialog->control_default );
3360 break;
3361 case 2: /* escape */
3362 control = msi_dialog_find_control( dialog, dialog->control_cancel );
3363 break;
3364 default:
3365 control = msi_dialog_find_control_by_hwnd( dialog, hwnd );
3368 if( control )
3370 if( control->handler )
3372 control->handler( dialog, control, param );
3373 msi_dialog_evaluate_control_conditions( dialog );
3377 return 0;
3380 static LRESULT msi_dialog_onnotify( msi_dialog *dialog, LPARAM param )
3382 LPNMHDR nmhdr = (LPNMHDR) param;
3383 msi_control *control = msi_dialog_find_control_by_hwnd( dialog, nmhdr->hwndFrom );
3385 TRACE("%p %p\n", dialog, nmhdr->hwndFrom);
3387 if ( control && control->handler )
3388 control->handler( dialog, control, param );
3390 return 0;
3393 static void msi_dialog_setfocus( msi_dialog *dialog )
3395 HWND hwnd = dialog->hWndFocus;
3397 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, TRUE);
3398 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, FALSE);
3399 SetFocus( hwnd );
3400 dialog->hWndFocus = hwnd;
3403 static LRESULT WINAPI MSIDialog_WndProc( HWND hwnd, UINT msg,
3404 WPARAM wParam, LPARAM lParam )
3406 msi_dialog *dialog = (LPVOID) GetWindowLongPtrW( hwnd, GWLP_USERDATA );
3408 TRACE("0x%04x\n", msg);
3410 switch (msg)
3412 case WM_MOVE:
3413 dialog->package->center_x = LOWORD(lParam) + dialog->size.cx / 2.0;
3414 dialog->package->center_y = HIWORD(lParam) + dialog->size.cy / 2.0;
3415 break;
3417 case WM_CREATE:
3418 return msi_dialog_oncreate( hwnd, (LPCREATESTRUCTW)lParam );
3420 case WM_COMMAND:
3421 return msi_dialog_oncommand( dialog, wParam, (HWND)lParam );
3423 case WM_ACTIVATE:
3424 if( LOWORD(wParam) == WA_INACTIVE )
3425 dialog->hWndFocus = GetFocus();
3426 else
3427 msi_dialog_setfocus( dialog );
3428 return 0;
3430 case WM_SETFOCUS:
3431 msi_dialog_setfocus( dialog );
3432 return 0;
3434 /* bounce back to our subclassed static control */
3435 case WM_CTLCOLORSTATIC:
3436 return SendMessageW( (HWND) lParam, WM_CTLCOLORSTATIC, wParam, lParam );
3438 case WM_DESTROY:
3439 dialog->hwnd = NULL;
3440 return 0;
3441 case WM_NOTIFY:
3442 return msi_dialog_onnotify( dialog, lParam );
3444 return DefWindowProcW(hwnd, msg, wParam, lParam);
3447 static BOOL CALLBACK msi_radioground_child_enum( HWND hWnd, LPARAM lParam )
3449 EnableWindow( hWnd, lParam );
3450 return TRUE;
3453 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
3455 WNDPROC oldproc = (WNDPROC) GetPropW(hWnd, szButtonData);
3456 LRESULT r;
3458 TRACE("hWnd %p msg %04x wParam 0x%08lx lParam 0x%08lx\n", hWnd, msg, wParam, lParam);
3460 if (msg == WM_COMMAND) /* Forward notifications to dialog */
3461 SendMessageW(GetParent(hWnd), msg, wParam, lParam);
3463 r = CallWindowProcW(oldproc, hWnd, msg, wParam, lParam);
3465 /* make sure the radio buttons show as disabled if the parent is disabled */
3466 if (msg == WM_ENABLE)
3467 EnumChildWindows( hWnd, msi_radioground_child_enum, wParam );
3469 return r;
3472 static LRESULT WINAPI MSIHiddenWindowProc( HWND hwnd, UINT msg,
3473 WPARAM wParam, LPARAM lParam )
3475 msi_dialog *dialog = (msi_dialog*) lParam;
3477 TRACE("%d %p\n", msg, dialog);
3479 switch (msg)
3481 case WM_MSI_DIALOG_CREATE:
3482 return msi_dialog_run_message_loop( dialog );
3483 case WM_MSI_DIALOG_DESTROY:
3484 msi_dialog_destroy( dialog );
3485 return 0;
3487 return DefWindowProcW( hwnd, msg, wParam, lParam );
3490 static BOOL msi_dialog_register_class( void )
3492 WNDCLASSW cls;
3494 ZeroMemory( &cls, sizeof cls );
3495 cls.lpfnWndProc = MSIDialog_WndProc;
3496 cls.hInstance = NULL;
3497 cls.hIcon = LoadIconW(0, (LPWSTR)IDI_APPLICATION);
3498 cls.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
3499 cls.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
3500 cls.lpszMenuName = NULL;
3501 cls.lpszClassName = szMsiDialogClass;
3503 if( !RegisterClassW( &cls ) )
3504 return FALSE;
3506 cls.lpfnWndProc = MSIHiddenWindowProc;
3507 cls.lpszClassName = szMsiHiddenWindow;
3509 if( !RegisterClassW( &cls ) )
3510 return FALSE;
3512 uiThreadId = GetCurrentThreadId();
3514 hMsiHiddenWindow = CreateWindowW( szMsiHiddenWindow, NULL, WS_OVERLAPPED,
3515 0, 0, 100, 100, NULL, NULL, NULL, NULL );
3516 if( !hMsiHiddenWindow )
3517 return FALSE;
3519 return TRUE;
3522 /* functions that interface to other modules within MSI */
3524 msi_dialog *msi_dialog_create( MSIPACKAGE* package,
3525 LPCWSTR szDialogName, msi_dialog *parent,
3526 msi_dialog_event_handler event_handler )
3528 MSIRECORD *rec = NULL;
3529 msi_dialog *dialog;
3531 TRACE("%p %s\n", package, debugstr_w(szDialogName));
3533 if (!hMsiHiddenWindow)
3534 msi_dialog_register_class();
3536 /* allocate the structure for the dialog to use */
3537 dialog = msi_alloc_zero( sizeof *dialog + sizeof(WCHAR)*strlenW(szDialogName) );
3538 if( !dialog )
3539 return NULL;
3540 strcpyW( dialog->name, szDialogName );
3541 dialog->parent = parent;
3542 msiobj_addref( &package->hdr );
3543 dialog->package = package;
3544 dialog->event_handler = event_handler;
3545 dialog->finished = 0;
3546 list_init( &dialog->controls );
3548 /* verify that the dialog exists */
3549 rec = msi_get_dialog_record( dialog );
3550 if( !rec )
3552 msiobj_release( &package->hdr );
3553 msi_free( dialog );
3554 return NULL;
3556 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
3557 dialog->control_default = strdupW( MSI_RecordGetString( rec, 9 ) );
3558 dialog->control_cancel = strdupW( MSI_RecordGetString( rec, 10 ) );
3559 msiobj_release( &rec->hdr );
3561 return dialog;
3564 static void msi_process_pending_messages( HWND hdlg )
3566 MSG msg;
3568 while( PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ) )
3570 if( hdlg && IsDialogMessageW( hdlg, &msg ))
3571 continue;
3572 TranslateMessage( &msg );
3573 DispatchMessageW( &msg );
3577 void msi_dialog_end_dialog( msi_dialog *dialog )
3579 TRACE("%p\n", dialog);
3580 dialog->finished = 1;
3581 PostMessageW(dialog->hwnd, WM_NULL, 0, 0);
3584 void msi_dialog_check_messages( HANDLE handle )
3586 DWORD r;
3588 /* in threads other than the UI thread, block */
3589 if( uiThreadId != GetCurrentThreadId() )
3591 if( handle )
3592 WaitForSingleObject( handle, INFINITE );
3593 return;
3596 /* there's two choices for the UI thread */
3597 while (1)
3599 msi_process_pending_messages( NULL );
3601 if( !handle )
3602 break;
3605 * block here until somebody creates a new dialog or
3606 * the handle we're waiting on becomes ready
3608 r = MsgWaitForMultipleObjects( 1, &handle, 0, INFINITE, QS_ALLINPUT );
3609 if( r == WAIT_OBJECT_0 )
3610 break;
3614 UINT msi_dialog_run_message_loop( msi_dialog *dialog )
3616 DWORD style;
3617 HWND hwnd;
3619 if( uiThreadId != GetCurrentThreadId() )
3620 return SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_CREATE, 0, (LPARAM) dialog );
3622 /* create the dialog window, don't show it yet */
3623 style = WS_OVERLAPPED;
3624 if( dialog->attributes & msidbDialogAttributesVisible )
3625 style |= WS_VISIBLE;
3627 hwnd = CreateWindowW( szMsiDialogClass, dialog->name, style,
3628 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
3629 NULL, NULL, NULL, dialog );
3630 if( !hwnd )
3632 ERR("Failed to create dialog %s\n", debugstr_w( dialog->name ));
3633 return ERROR_FUNCTION_FAILED;
3636 ShowWindow( hwnd, SW_SHOW );
3637 /* UpdateWindow( hwnd ); - and causes the transparent static controls not to paint */
3639 if( dialog->attributes & msidbDialogAttributesModal )
3641 while( !dialog->finished )
3643 MsgWaitForMultipleObjects( 0, NULL, 0, INFINITE, QS_ALLINPUT );
3644 msi_process_pending_messages( dialog->hwnd );
3647 else
3648 return ERROR_IO_PENDING;
3650 return ERROR_SUCCESS;
3653 void msi_dialog_do_preview( msi_dialog *dialog )
3655 TRACE("\n");
3656 dialog->attributes |= msidbDialogAttributesVisible;
3657 dialog->attributes &= ~msidbDialogAttributesModal;
3658 msi_dialog_run_message_loop( dialog );
3661 void msi_dialog_destroy( msi_dialog *dialog )
3663 if( uiThreadId != GetCurrentThreadId() )
3665 SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_DESTROY, 0, (LPARAM) dialog );
3666 return;
3669 if( dialog->hwnd )
3670 ShowWindow( dialog->hwnd, SW_HIDE );
3672 if( dialog->hwnd )
3673 DestroyWindow( dialog->hwnd );
3675 /* unsubscribe events */
3676 ControlEvent_CleanupDialogSubscriptions(dialog->package, dialog->name);
3678 /* destroy the list of controls */
3679 while( !list_empty( &dialog->controls ) )
3681 msi_control *t;
3683 t = LIST_ENTRY( list_head( &dialog->controls ),
3684 msi_control, entry );
3685 msi_destroy_control( t );
3688 /* destroy the list of fonts */
3689 while( dialog->font_list )
3691 msi_font *t = dialog->font_list;
3692 dialog->font_list = t->next;
3693 DeleteObject( t->hfont );
3694 msi_free( t );
3696 msi_free( dialog->default_font );
3698 msi_free( dialog->control_default );
3699 msi_free( dialog->control_cancel );
3700 msiobj_release( &dialog->package->hdr );
3701 dialog->package = NULL;
3702 msi_free( dialog );
3705 void msi_dialog_unregister_class( void )
3707 DestroyWindow( hMsiHiddenWindow );
3708 hMsiHiddenWindow = NULL;
3709 UnregisterClassW( szMsiDialogClass, NULL );
3710 UnregisterClassW( szMsiHiddenWindow, NULL );
3711 uiThreadId = 0;
3714 static UINT error_dialog_handler(MSIPACKAGE *package, LPCWSTR event,
3715 LPCWSTR argument, msi_dialog* dialog)
3717 static const WCHAR end_dialog[] = {'E','n','d','D','i','a','l','o','g',0};
3718 static const WCHAR error_abort[] = {'E','r','r','o','r','A','b','o','r','t',0};
3719 static const WCHAR error_cancel[] = {'E','r','r','o','r','C','a','n','c','e','l',0};
3720 static const WCHAR error_no[] = {'E','r','r','o','r','N','o',0};
3721 static const WCHAR result_prop[] = {
3722 'M','S','I','E','r','r','o','r','D','i','a','l','o','g','R','e','s','u','l','t',0
3725 if ( lstrcmpW( event, end_dialog ) )
3726 return ERROR_SUCCESS;
3728 if ( !lstrcmpW( argument, error_abort ) || !lstrcmpW( argument, error_cancel ) ||
3729 !lstrcmpW( argument, error_no ) )
3731 MSI_SetPropertyW( package, result_prop, error_abort );
3734 ControlEvent_CleanupSubscriptions(package);
3735 msi_dialog_end_dialog( dialog );
3737 return ERROR_SUCCESS;
3740 static UINT msi_error_dialog_set_error( MSIPACKAGE *package, LPWSTR error_dialog, LPWSTR error )
3742 MSIRECORD * row;
3744 static const WCHAR update[] =
3745 {'U','P','D','A','T','E',' ','`','C','o','n','t','r','o','l','`',' ',
3746 'S','E','T',' ','`','T','e','x','t','`',' ','=',' ','\'','%','s','\'',' ',
3747 'W','H','E','R','E', ' ','`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
3748 'A','N','D',' ','`','C','o','n','t','r','o','l','`',' ','=',' ',
3749 '\'','E','r','r','o','r','T','e','x','t','\'',0};
3751 row = MSI_QueryGetRecord( package->db, update, error, error_dialog );
3752 if (!row)
3753 return ERROR_FUNCTION_FAILED;
3755 msiobj_release(&row->hdr);
3756 return ERROR_SUCCESS;
3759 UINT msi_spawn_error_dialog( MSIPACKAGE *package, LPWSTR error_dialog, LPWSTR error )
3761 msi_dialog *dialog;
3762 WCHAR result[MAX_PATH];
3763 UINT r = ERROR_SUCCESS;
3764 DWORD size = MAX_PATH;
3765 int res;
3767 static const WCHAR szUILevel[] = {'U','I','L','e','v','e','l',0};
3768 static const WCHAR pn_prop[] = {'P','r','o','d','u','c','t','N','a','m','e',0};
3769 static const WCHAR title_fmt[] = {'%','s',' ','W','a','r','n','i','n','g',0};
3770 static const WCHAR error_abort[] = {'E','r','r','o','r','A','b','o','r','t',0};
3771 static const WCHAR result_prop[] = {
3772 'M','S','I','E','r','r','o','r','D','i','a','l','o','g','R','e','s','u','l','t',0
3775 if ( (msi_get_property_int(package, szUILevel, 0) & INSTALLUILEVEL_MASK) == INSTALLUILEVEL_NONE )
3776 return ERROR_SUCCESS;
3778 if ( !error_dialog )
3780 LPWSTR product_name = msi_dup_property( package, pn_prop );
3781 WCHAR title[MAX_PATH];
3783 sprintfW( title, title_fmt, product_name );
3784 res = MessageBoxW( NULL, error, title, MB_OKCANCEL | MB_ICONWARNING );
3786 msi_free( product_name );
3788 if ( res == IDOK )
3789 return ERROR_SUCCESS;
3790 else
3791 return ERROR_FUNCTION_FAILED;
3794 r = msi_error_dialog_set_error( package, error_dialog, error );
3795 if ( r != ERROR_SUCCESS )
3796 return r;
3798 dialog = msi_dialog_create( package, error_dialog, package->dialog,
3799 error_dialog_handler );
3800 if ( !dialog )
3801 return ERROR_FUNCTION_FAILED;
3803 dialog->finished = FALSE;
3804 r = msi_dialog_run_message_loop( dialog );
3805 if ( r != ERROR_SUCCESS )
3806 goto done;
3808 r = MSI_GetPropertyW( package, result_prop, result, &size );
3809 if ( r != ERROR_SUCCESS)
3810 r = ERROR_SUCCESS;
3812 if ( !lstrcmpW( result, error_abort ) )
3813 r = ERROR_FUNCTION_FAILED;
3815 done:
3816 msi_dialog_destroy( dialog );
3818 return r;