Add the string constants located in msi.h and make use of them in
[wine/gsoc_dplay.git] / dlls / msi / dialog.c
blob6de713491990d22c9f7919bcd3b05c88c6cfff10
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #define COBJMACROS
23 #include <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "winnls.h"
29 #include "wingdi.h"
30 #include "msi.h"
31 #include "msipriv.h"
32 #include "msidefs.h"
33 #include "ocidl.h"
34 #include "olectl.h"
35 #include "richedit.h"
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
40 #include "action.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(msi);
45 struct msi_control_tag;
46 typedef struct msi_control_tag msi_control;
47 typedef UINT (*msi_handler)( msi_dialog *, msi_control *, WPARAM );
49 struct msi_control_tag
51 struct msi_control_tag *next;
52 HWND hwnd;
53 msi_handler handler;
54 LPWSTR property;
55 LPWSTR value;
56 HBITMAP hBitmap;
57 HICON hIcon;
58 LPWSTR tabnext;
59 WCHAR name[1];
62 typedef struct msi_font_tag
64 struct msi_font_tag *next;
65 HFONT hfont;
66 WCHAR name[1];
67 } msi_font;
69 struct msi_dialog_tag
71 MSIPACKAGE *package;
72 msi_dialog_event_handler event_handler;
73 BOOL finished;
74 INT scale;
75 DWORD attributes;
76 HWND hwnd;
77 LPWSTR default_font;
78 msi_font *font_list;
79 msi_control *control_list;
80 HWND hWndFocus;
81 WCHAR name[1];
84 typedef UINT (*msi_dialog_control_func)( msi_dialog *dialog, MSIRECORD *rec );
85 struct control_handler
87 LPCWSTR control_type;
88 msi_dialog_control_func func;
91 typedef struct
93 msi_dialog* dialog;
94 msi_control *parent;
95 DWORD attributes;
96 } radio_button_group_descr;
98 const WCHAR szMsiDialogClass[] = {
99 'M','s','i','D','i','a','l','o','g','C','l','o','s','e','C','l','a','s','s',0
101 const WCHAR szMsiHiddenWindow[] = {
102 'M','s','i','H','i','d','d','e','n','W','i','n','d','o','w',0 };
103 const static WCHAR szStatic[] = { 'S','t','a','t','i','c',0 };
104 const static WCHAR szButton[] = { 'B','U','T','T','O','N', 0 };
105 const static WCHAR szButtonData[] = { 'M','S','I','D','A','T','A',0 };
106 static const WCHAR szText[] = { 'T','e','x','t',0 };
107 static const WCHAR szPushButton[] = { 'P','u','s','h','B','u','t','t','o','n',0 };
108 static const WCHAR szLine[] = { 'L','i','n','e',0 };
109 static const WCHAR szBitmap[] = { 'B','i','t','m','a','p',0 };
110 static const WCHAR szCheckBox[] = { 'C','h','e','c','k','B','o','x',0 };
111 static const WCHAR szScrollableText[] = {
112 'S','c','r','o','l','l','a','b','l','e','T','e','x','t',0 };
113 static const WCHAR szComboBox[] = { 'C','o','m','b','o','B','o','x',0 };
114 static const WCHAR szEdit[] = { 'E','d','i','t',0 };
115 static const WCHAR szMaskedEdit[] = { 'M','a','s','k','e','d','E','d','i','t',0 };
116 static const WCHAR szPathEdit[] = { 'P','a','t','h','E','d','i','t',0 };
117 static const WCHAR szRadioButtonGroup[] = {
118 'R','a','d','i','o','B','u','t','t','o','n','G','r','o','u','p',0 };
119 static const WCHAR szIcon[] = { 'I','c','o','n',0 };
121 static UINT msi_dialog_checkbox_handler( msi_dialog *, msi_control *, WPARAM );
122 static void msi_dialog_checkbox_sync_state( msi_dialog *, msi_control * );
123 static UINT msi_dialog_button_handler( msi_dialog *, msi_control *, WPARAM );
124 static UINT msi_dialog_edit_handler( msi_dialog *, msi_control *, WPARAM );
125 static UINT msi_dialog_radiogroup_handler( msi_dialog *, msi_control *, WPARAM param );
126 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog );
127 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
130 /* dialog sequencing */
132 #define WM_MSI_DIALOG_CREATE (WM_USER+0x100)
133 #define WM_MSI_DIALOG_DESTROY (WM_USER+0x101)
135 static DWORD uiThreadId;
136 static HWND hMsiHiddenWindow;
137 static HMODULE hRichedit;
139 static INT msi_dialog_scale_unit( msi_dialog *dialog, INT val )
141 return (dialog->scale * val + 5) / 10;
144 static msi_control *msi_dialog_find_control( msi_dialog *dialog, LPCWSTR name )
146 msi_control *control;
148 if( !name )
149 return NULL;
150 for( control = dialog->control_list; control; control = control->next )
151 if( !strcmpW( control->name, name ) ) /* FIXME: case sensitive? */
152 break;
153 return control;
156 static msi_control *msi_dialog_find_control_by_hwnd( msi_dialog *dialog, HWND hwnd )
158 msi_control *control;
160 for( control = dialog->control_list; control; control = control->next )
161 if( hwnd == control->hwnd )
162 break;
163 return control;
167 * msi_dialog_get_style
169 * Extract the {\style} string from the front of the text to display and
170 * update the pointer.
172 static LPWSTR msi_dialog_get_style( LPCWSTR *text )
174 LPWSTR ret = NULL;
175 LPCWSTR p = *text, q;
176 DWORD len;
178 if( *p++ != '{' )
179 return ret;
180 q = strchrW( p, '}' );
181 if( !q )
182 return ret;
183 *text = ++q;
184 if( *p++ != '\\' )
185 return ret;
186 len = q - p;
188 ret = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
189 if( !ret )
190 return ret;
191 memcpy( ret, p, len*sizeof(WCHAR) );
192 ret[len-1] = 0;
193 return ret;
196 static UINT msi_dialog_add_font( MSIRECORD *rec, LPVOID param )
198 msi_dialog *dialog = param;
199 msi_font *font;
200 LPCWSTR face, name;
201 LOGFONTW lf;
202 INT style;
203 HDC hdc;
205 /* create a font and add it to the list */
206 name = MSI_RecordGetString( rec, 1 );
207 font = HeapAlloc( GetProcessHeap(), 0,
208 sizeof *font + strlenW( name )*sizeof (WCHAR) );
209 strcpyW( font->name, name );
210 font->next = dialog->font_list;
211 dialog->font_list = font;
213 memset( &lf, 0, sizeof lf );
214 face = MSI_RecordGetString( rec, 2 );
215 lf.lfHeight = MSI_RecordGetInteger( rec, 3 );
216 style = MSI_RecordGetInteger( rec, 5 );
217 if( style & msidbTextStyleStyleBitsBold )
218 lf.lfWeight = FW_BOLD;
219 if( style & msidbTextStyleStyleBitsItalic )
220 lf.lfItalic = TRUE;
221 if( style & msidbTextStyleStyleBitsUnderline )
222 lf.lfUnderline = TRUE;
223 if( style & msidbTextStyleStyleBitsStrike )
224 lf.lfStrikeOut = TRUE;
225 lstrcpynW( lf.lfFaceName, face, LF_FACESIZE );
227 /* adjust the height */
228 hdc = GetDC( dialog->hwnd );
229 if (hdc)
231 lf.lfHeight = -MulDiv(lf.lfHeight, GetDeviceCaps(hdc, LOGPIXELSY), 72);
232 ReleaseDC( dialog->hwnd, hdc );
235 font->hfont = CreateFontIndirectW( &lf );
237 TRACE("Adding font style %s\n", debugstr_w(font->name) );
239 return ERROR_SUCCESS;
242 static msi_font *msi_dialog_find_font( msi_dialog *dialog, LPCWSTR name )
244 msi_font *font;
246 for( font = dialog->font_list; font; font = font->next )
247 if( !strcmpW( font->name, name ) ) /* FIXME: case sensitive? */
248 break;
250 return font;
253 static UINT msi_dialog_set_font( msi_dialog *dialog, HWND hwnd, LPCWSTR name )
255 msi_font *font;
257 font = msi_dialog_find_font( dialog, name );
258 if( font )
259 SendMessageW( hwnd, WM_SETFONT, (WPARAM) font->hfont, TRUE );
260 else
261 ERR("No font entry for %s\n", debugstr_w(name));
262 return ERROR_SUCCESS;
265 static UINT msi_dialog_build_font_list( msi_dialog *dialog )
267 static const WCHAR query[] = {
268 'S','E','L','E','C','T',' ','*',' ',
269 'F','R','O','M',' ','`','T','e','x','t','S','t','y','l','e','`',' ',0
271 UINT r;
272 MSIQUERY *view = NULL;
274 TRACE("dialog %p\n", dialog );
276 r = MSI_OpenQuery( dialog->package->db, &view, query );
277 if( r != ERROR_SUCCESS )
278 return r;
280 r = MSI_IterateRecords( view, NULL, msi_dialog_add_font, dialog );
281 msiobj_release( &view->hdr );
283 return r;
286 static msi_control *msi_dialog_create_window( msi_dialog *dialog,
287 MSIRECORD *rec, LPCWSTR szCls, LPCWSTR name, LPCWSTR text,
288 DWORD style, HWND parent )
290 DWORD x, y, width, height;
291 LPWSTR font = NULL, title = NULL;
292 msi_control *control;
294 style |= WS_CHILD;
296 control = HeapAlloc( GetProcessHeap(), 0,
297 sizeof *control + strlenW(name)*sizeof(WCHAR) );
298 strcpyW( control->name, name );
299 control->next = dialog->control_list;
300 dialog->control_list = control;
301 control->handler = NULL;
302 control->property = NULL;
303 control->value = NULL;
304 control->hBitmap = NULL;
305 control->hIcon = NULL;
306 control->tabnext = strdupW( MSI_RecordGetString( rec, 11) );
308 x = MSI_RecordGetInteger( rec, 4 );
309 y = MSI_RecordGetInteger( rec, 5 );
310 width = MSI_RecordGetInteger( rec, 6 );
311 height = MSI_RecordGetInteger( rec, 7 );
313 x = msi_dialog_scale_unit( dialog, x );
314 y = msi_dialog_scale_unit( dialog, y );
315 width = msi_dialog_scale_unit( dialog, width );
316 height = msi_dialog_scale_unit( dialog, height );
318 if( text )
320 font = msi_dialog_get_style( &text );
321 deformat_string( dialog->package, text, &title );
324 control->hwnd = CreateWindowW( szCls, title, style,
325 x, y, width, height, parent, NULL, NULL, NULL );
327 TRACE("Dialog %s control %s hwnd %p\n",
328 debugstr_w(dialog->name), debugstr_w(text), control->hwnd );
330 msi_dialog_set_font( dialog, control->hwnd,
331 font ? font : dialog->default_font );
333 HeapFree( GetProcessHeap(), 0, font );
334 HeapFree( GetProcessHeap(), 0, title );
336 return control;
339 /* called from the Control Event subscription code */
340 void msi_dialog_handle_event( msi_dialog* dialog, LPCWSTR control,
341 LPCWSTR attribute, MSIRECORD *rec )
343 msi_control* ctrl;
344 LPCWSTR text;
346 ctrl = msi_dialog_find_control( dialog, control );
347 if (!ctrl)
348 return;
349 if( lstrcmpW(attribute, szText) )
350 return;
351 text = MSI_RecordGetString( rec , 1 );
352 SetWindowTextW( ctrl->hwnd, text );
353 msi_dialog_check_messages( NULL );
356 static void msi_dialog_map_events(msi_dialog* dialog, LPCWSTR control)
358 static WCHAR Query[] = {
359 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
360 '`','E','v','e','n','t','M','a','p','p','i','n','g','`',' ',
361 'W','H','E','R','E',' ',
362 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
363 'A','N','D',' ',
364 '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',0
366 MSIRECORD *row;
367 LPCWSTR event, attribute;
369 row = MSI_QueryGetRecord( dialog->package->db, Query, dialog->name, control );
370 if (!row)
371 return;
373 event = MSI_RecordGetString( row, 3 );
374 attribute = MSI_RecordGetString( row, 4 );
375 ControlEvent_SubscribeToEvent( dialog->package, event, control, attribute );
376 msiobj_release( &row->hdr );
379 /* everything except radio buttons */
380 static msi_control *msi_dialog_add_control( msi_dialog *dialog,
381 MSIRECORD *rec, LPCWSTR szCls, DWORD style )
383 DWORD attributes;
384 LPCWSTR text, name;
386 name = MSI_RecordGetString( rec, 2 );
387 attributes = MSI_RecordGetInteger( rec, 8 );
388 text = MSI_RecordGetString( rec, 10 );
389 if( attributes & msidbControlAttributesVisible )
390 style |= WS_VISIBLE;
391 if( ~attributes & msidbControlAttributesEnabled )
392 style |= WS_DISABLED;
394 msi_dialog_map_events(dialog, name);
396 return msi_dialog_create_window( dialog, rec, szCls, name, text,
397 style, dialog->hwnd );
400 struct msi_text_info
402 WNDPROC oldproc;
403 DWORD attributes;
407 * we don't erase our own background,
408 * so we have to make sure that the parent window redraws first
410 static void msi_text_on_settext( HWND hWnd )
412 HWND hParent;
413 RECT rc;
415 hParent = GetParent( hWnd );
416 GetWindowRect( hWnd, &rc );
417 MapWindowPoints( NULL, hParent, (LPPOINT) &rc, 2 );
418 InvalidateRect( hParent, &rc, TRUE );
421 static LRESULT WINAPI
422 MSIText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
424 struct msi_text_info *info;
425 LRESULT r = 0;
427 TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
429 info = GetPropW(hWnd, szButtonData);
431 if( msg == WM_CTLCOLORSTATIC &&
432 ( info->attributes & msidbControlAttributesTransparent ) )
434 SetBkMode( (HDC)wParam, TRANSPARENT );
435 return (LRESULT) GetStockObject(NULL_BRUSH);
438 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
440 switch( msg )
442 case WM_SETTEXT:
443 msi_text_on_settext( hWnd );
444 break;
445 case WM_NCDESTROY:
446 HeapFree( GetProcessHeap(), 0, info );
447 RemovePropW( hWnd, szButtonData );
448 break;
451 return r;
454 static UINT msi_dialog_text_control( msi_dialog *dialog, MSIRECORD *rec )
456 msi_control *control;
457 struct msi_text_info *info;
459 TRACE("%p %p\n", dialog, rec);
461 control = msi_dialog_add_control( dialog, rec, szStatic, SS_LEFT | WS_GROUP );
462 if( !control )
463 return ERROR_FUNCTION_FAILED;
465 info = HeapAlloc( GetProcessHeap(), 0, sizeof *info );
466 if( !info )
467 return ERROR_SUCCESS;
469 info->attributes = MSI_RecordGetInteger( rec, 8 );
470 if( info->attributes & msidbControlAttributesTransparent )
471 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT );
473 info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
474 (LONG_PTR)MSIText_WndProc );
475 SetPropW( control->hwnd, szButtonData, info );
477 return ERROR_SUCCESS;
480 static UINT msi_dialog_button_control( msi_dialog *dialog, MSIRECORD *rec )
482 msi_control *control;
484 TRACE("%p %p\n", dialog, rec);
486 control = msi_dialog_add_control( dialog, rec, szButton, WS_TABSTOP );
487 control->handler = msi_dialog_button_handler;
489 return ERROR_SUCCESS;
492 static LPWSTR msi_get_checkbox_value( msi_dialog *dialog, LPCWSTR prop )
494 const static WCHAR query[] = {
495 'S','E','L','E','C','T',' ','*',' ',
496 'F','R','O','M',' ','`','C','h','e','c','k','B','o','x',' ','`',
497 'W','H','E','R','E',' ',
498 '`','P','r','o','p','e','r','t','y','`',' ','=',' ',
499 '\'','%','s','\'',0
501 MSIRECORD *rec = NULL;
502 LPCWSTR val = NULL;
503 LPWSTR ret = NULL;
505 /* find if there is a value associated with the checkbox */
506 rec = MSI_QueryGetRecord( dialog->package->db, query, prop );
507 if (!rec)
508 return ret;
510 val = MSI_RecordGetString( rec, 2 );
511 if (val)
513 deformat_string( dialog->package, val, &ret );
514 if( ret && !ret[0] )
516 HeapFree( GetProcessHeap(), 0, ret );
517 ret = NULL;
520 msiobj_release( &rec->hdr );
521 if (ret)
522 return ret;
524 ret = load_dynamic_property(dialog->package, prop, NULL);
525 if( ret && !ret[0] )
527 HeapFree( GetProcessHeap(), 0, ret );
528 ret = NULL;
531 return ret;
534 static UINT msi_dialog_checkbox_control( msi_dialog *dialog, MSIRECORD *rec )
536 msi_control *control;
537 LPCWSTR prop;
539 TRACE("%p %p\n", dialog, rec);
541 control = msi_dialog_add_control( dialog, rec, szButton,
542 BS_CHECKBOX | BS_MULTILINE | WS_TABSTOP );
543 control->handler = msi_dialog_checkbox_handler;
544 prop = MSI_RecordGetString( rec, 9 );
545 if( prop )
547 control->property = strdupW( prop );
548 control->value = msi_get_checkbox_value( dialog, prop );
549 TRACE("control %s value %s\n", debugstr_w(control->property),
550 debugstr_w(control->value));
552 msi_dialog_checkbox_sync_state( dialog, control );
554 return ERROR_SUCCESS;
557 static UINT msi_dialog_line_control( msi_dialog *dialog, MSIRECORD *rec )
559 TRACE("%p %p\n", dialog, rec);
561 msi_dialog_add_control( dialog, rec, szStatic, SS_ETCHEDHORZ | SS_SUNKEN );
562 return ERROR_SUCCESS;
565 struct msi_streamin_info
567 LPSTR string;
568 DWORD offset;
569 DWORD length;
572 static DWORD CALLBACK
573 msi_richedit_stream_in( DWORD_PTR arg, LPBYTE buffer, LONG count, LONG *pcb )
575 struct msi_streamin_info *info = (struct msi_streamin_info*) arg;
577 if( (count + info->offset) > info->length )
578 count = info->length - info->offset;
579 memcpy( buffer, &info->string[ info->offset ], count );
580 *pcb = count;
581 info->offset += count;
583 TRACE("%ld/%ld\n", info->offset, info->length);
585 return 0;
588 static UINT msi_dialog_scrolltext_control( msi_dialog *dialog, MSIRECORD *rec )
590 const static WCHAR szRichEdit20W[] = {
591 'R','i','c','h','E','d','i','t','2','0','W',0
593 struct msi_streamin_info info;
594 msi_control *control;
595 LPCWSTR text;
596 EDITSTREAM es;
597 DWORD style;
599 style = WS_BORDER | ES_MULTILINE | WS_VSCROLL |
600 ES_READONLY | ES_AUTOVSCROLL | WS_TABSTOP;
601 control = msi_dialog_add_control( dialog, rec, szRichEdit20W, style );
603 text = MSI_RecordGetString( rec, 10 );
604 info.string = strdupWtoA( text );
605 info.offset = 0;
606 info.length = lstrlenA( info.string ) + 1;
608 es.dwCookie = (DWORD_PTR) &info;
609 es.dwError = 0;
610 es.pfnCallback = msi_richedit_stream_in;
612 SendMessageW( control->hwnd, EM_STREAMIN, SF_RTF, (LPARAM) &es );
614 HeapFree( GetProcessHeap(), 0, info.string );
616 return ERROR_SUCCESS;
619 static MSIRECORD *msi_get_binary_record( MSIDATABASE *db, LPCWSTR name )
621 const static WCHAR query[] = {
622 's','e','l','e','c','t',' ','*',' ',
623 'f','r','o','m',' ','B','i','n','a','r','y',' ',
624 'w','h','e','r','e',' ',
625 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0
628 return MSI_QueryGetRecord( db, query, name );
631 static LPWSTR msi_create_tmp_path(void)
633 WCHAR tmp[MAX_PATH];
634 LPWSTR path = NULL;
635 static const WCHAR prefix[] = { 'm','s','i',0 };
636 DWORD len, r;
638 r = GetTempPathW( MAX_PATH, tmp );
639 if( !r )
640 return path;
641 len = lstrlenW( tmp ) + 20;
642 path = HeapAlloc( GetProcessHeap(), 0, len * sizeof (WCHAR) );
643 if( path )
645 r = GetTempFileNameW( tmp, prefix, 0, path );
646 if (!r)
648 HeapFree( GetProcessHeap(), 0, path );
649 path = NULL;
652 return path;
655 static HANDLE msi_load_image( MSIDATABASE *db, LPCWSTR name, UINT type,
656 UINT cx, UINT cy, UINT flags )
658 MSIRECORD *rec = NULL;
659 HANDLE himage = NULL;
660 LPWSTR tmp;
661 UINT r;
663 TRACE("%p %s %u %u %08x\n", db, debugstr_w(name), cx, cy, flags);
665 tmp = msi_create_tmp_path();
666 if( !tmp )
667 return himage;
669 rec = msi_get_binary_record( db, name );
670 if( rec )
672 r = MSI_RecordStreamToFile( rec, 2, tmp );
673 if( r == ERROR_SUCCESS )
675 himage = LoadImageW( 0, tmp, type, cx, cy, flags );
676 DeleteFileW( tmp );
678 msiobj_release( &rec->hdr );
681 HeapFree( GetProcessHeap(), 0, tmp );
682 return himage;
685 static UINT msi_dialog_bitmap_control( msi_dialog *dialog, MSIRECORD *rec )
687 UINT cx, cy, flags, style, attributes;
688 msi_control *control;
689 LPCWSTR text;
691 flags = LR_LOADFROMFILE;
692 style = SS_BITMAP | SS_LEFT | WS_GROUP;
694 attributes = MSI_RecordGetInteger( rec, 8 );
695 if( attributes & msidbControlAttributesFixedSize )
697 flags |= LR_DEFAULTSIZE;
699 style |= SS_CENTERIMAGE;
701 control = msi_dialog_add_control( dialog, rec, szStatic, style );
702 text = MSI_RecordGetString( rec, 10 );
703 cx = MSI_RecordGetInteger( rec, 6 );
704 cy = MSI_RecordGetInteger( rec, 7 );
705 cx = msi_dialog_scale_unit( dialog, cx );
706 cy = msi_dialog_scale_unit( dialog, cy );
708 control->hBitmap = msi_load_image( dialog->package->db, text,
709 IMAGE_BITMAP, cx, cy, flags );
710 if( control->hBitmap )
711 SendMessageW( control->hwnd, STM_SETIMAGE,
712 IMAGE_BITMAP, (LPARAM) control->hBitmap );
713 else
714 ERR("Failed to load bitmap %s\n", debugstr_w(text));
716 return ERROR_SUCCESS;
719 static UINT msi_dialog_icon_control( msi_dialog *dialog, MSIRECORD *rec )
721 msi_control *control;
722 DWORD attributes, cx = 0, cy = 0, flags;
723 LPCWSTR text;
725 TRACE("\n");
727 control = msi_dialog_add_control( dialog, rec, szStatic,
728 SS_ICON | SS_CENTERIMAGE | WS_GROUP );
730 flags = LR_LOADFROMFILE | LR_DEFAULTSIZE;
731 attributes = MSI_RecordGetInteger( rec, 8 );
732 if( attributes & msidbControlAttributesFixedSize )
734 flags &= ~LR_DEFAULTSIZE;
735 if( attributes & msidbControlAttributesIconSize16 )
737 cx += 16;
738 cy += 16;
740 if( attributes & msidbControlAttributesIconSize32 )
742 cx += 32;
743 cy += 32;
745 /* msidbControlAttributesIconSize48 handled by above logic */
748 text = MSI_RecordGetString( rec, 10 );
749 control->hIcon = msi_load_image( dialog->package->db, text,
750 IMAGE_ICON, cx, cy, flags );
751 if( control->hIcon )
752 SendMessageW( control->hwnd, STM_SETICON, (WPARAM) control->hIcon, 0 );
753 else
754 ERR("Failed to load bitmap %s\n", debugstr_w(text));
755 return ERROR_SUCCESS;
758 static UINT msi_dialog_combo_control( msi_dialog *dialog, MSIRECORD *rec )
760 static const WCHAR szCombo[] = { 'C','O','M','B','O','B','O','X',0 };
762 msi_dialog_add_control( dialog, rec, szCombo,
763 SS_BITMAP | SS_LEFT | SS_CENTERIMAGE );
764 return ERROR_SUCCESS;
767 static UINT msi_dialog_edit_control( msi_dialog *dialog, MSIRECORD *rec )
769 msi_control *control;
770 LPCWSTR prop;
771 LPWSTR val;
773 control = msi_dialog_add_control( dialog, rec, szEdit,
774 WS_BORDER | WS_TABSTOP );
775 control->handler = msi_dialog_edit_handler;
776 prop = MSI_RecordGetString( rec, 9 );
777 if( prop )
778 control->property = strdupW( prop );
779 val = load_dynamic_property( dialog->package, control->property, NULL );
780 SetWindowTextW( control->hwnd, val );
781 HeapFree( GetProcessHeap(), 0, val );
782 return ERROR_SUCCESS;
785 /******************** Masked Edit ********************************************/
787 #define MASK_MAX_GROUPS 10
789 struct msi_mask_group
791 UINT len;
792 UINT ofs;
793 WCHAR type;
794 HWND hwnd;
797 struct msi_maskedit_info
799 msi_dialog *dialog;
800 WNDPROC oldproc;
801 HWND hwnd;
802 LPWSTR prop;
803 UINT num_chars;
804 UINT num_groups;
805 struct msi_mask_group group[MASK_MAX_GROUPS];
808 static void msi_mask_control_change( struct msi_maskedit_info *info )
810 LPWSTR val;
811 UINT i, n, r;
813 val = HeapAlloc( GetProcessHeap(), 0, (info->num_chars+1)*sizeof(WCHAR) );
814 for( i=0, n=0; i<info->num_groups; i++ )
816 if( (info->group[i].len + n) > info->num_chars )
818 ERR("can't fit control %d text into template\n",i);
819 break;
821 r = GetWindowTextW( info->group[i].hwnd, &val[n], info->group[i].len+1 );
822 if( r != info->group[i].len )
823 break;
824 n += r;
827 TRACE("%d/%d controls were good\n", i, info->num_groups);
829 if( i == info->num_groups )
831 TRACE("Set property %s to %s\n",
832 debugstr_w(info->prop), debugstr_w(val) );
833 CharUpperBuffW( val, info->num_chars );
834 MSI_SetPropertyW( info->dialog->package, info->prop, val );
835 msi_dialog_evaluate_control_conditions( info->dialog );
837 HeapFree( GetProcessHeap(), 0, val );
840 /* now move to the next control if necessary */
841 static VOID msi_mask_next_control( struct msi_maskedit_info *info, HWND hWnd )
843 HWND hWndNext;
844 UINT len, i;
846 for( i=0; i<info->num_groups; i++ )
847 if( info->group[i].hwnd == hWnd )
848 break;
850 /* don't move from the last control */
851 if( i >= (info->num_groups-1) )
852 return;
854 len = SendMessageW( hWnd, WM_GETTEXTLENGTH, 0, 0 );
855 if( len < info->group[i].len )
856 return;
858 hWndNext = GetNextDlgTabItem( GetParent( hWnd ), hWnd, FALSE );
859 SetFocus( hWndNext );
862 static LRESULT WINAPI
863 MSIMaskedEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
865 struct msi_maskedit_info *info;
866 HRESULT r;
868 TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
870 info = GetPropW(hWnd, szButtonData);
872 r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
874 switch( msg )
876 case WM_COMMAND:
877 if (HIWORD(wParam) == EN_CHANGE)
879 msi_mask_control_change( info );
880 msi_mask_next_control( info, (HWND) lParam );
882 break;
883 case WM_NCDESTROY:
884 HeapFree( GetProcessHeap(), 0, info->prop );
885 HeapFree( GetProcessHeap(), 0, info );
886 RemovePropW( hWnd, szButtonData );
887 break;
890 return r;
893 /* fish the various bits of the property out and put them in the control */
894 static void
895 msi_maskedit_set_text( struct msi_maskedit_info *info, LPCWSTR text )
897 LPCWSTR p;
898 UINT i;
900 p = text;
901 for( i = 0; i < info->num_groups; i++ )
903 if( info->group[i].len < lstrlenW( p ) )
905 LPWSTR chunk = strdupW( p );
906 chunk[ info->group[i].len ] = 0;
907 SetWindowTextW( info->group[i].hwnd, chunk );
908 HeapFree( GetProcessHeap(), 0, chunk );
910 else
912 SetWindowTextW( info->group[i].hwnd, p );
913 break;
915 p += info->group[i].len;
919 static struct msi_maskedit_info * msi_dialog_parse_groups( LPCWSTR mask )
921 struct msi_maskedit_info * info = NULL;
922 int i = 0, n = 0, total = 0;
923 LPCWSTR p;
925 TRACE("masked control, template %s\n", debugstr_w(mask));
927 if( !mask )
928 return info;
930 p = strchrW(mask, '<');
931 if( !p )
932 return info;
934 info = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof *info );
935 if( !info )
936 return info;
938 p++;
939 for( i=0; i<MASK_MAX_GROUPS; i++ )
941 /* stop at the end of the string */
942 if( p[0] == 0 || p[0] == '>' )
943 break;
945 /* count the number of the same identifier */
946 for( n=0; p[n] == p[0]; n++ )
948 info->group[i].ofs = total;
949 info->group[i].type = p[0];
950 if( p[n] == '=' )
952 n++;
953 total++; /* an extra not part of the group */
955 info->group[i].len = n;
956 total += n;
957 p += n;
960 TRACE("%d characters in %d groups\n", total, info->num_groups );
961 if( i == MASK_MAX_GROUPS )
962 ERR("too many groups in PIDTemplate %s\n", debugstr_w(mask));
964 info->num_chars = total;
965 info->num_groups = i;
967 return info;
970 static void
971 msi_maskedit_create_children( struct msi_maskedit_info *info )
973 DWORD width, height, style, wx, ww;
974 LPCWSTR text, font = NULL;
975 RECT rect;
976 HWND hwnd;
977 UINT i;
979 style = WS_CHILD | WS_BORDER | WS_VISIBLE | WS_TABSTOP;
981 GetClientRect( info->hwnd, &rect );
983 width = rect.right - rect.left;
984 height = rect.bottom - rect.top;
986 if( text )
987 font = msi_dialog_get_style( &text );
989 for( i = 0; i < info->num_groups; i++ )
991 wx = (info->group[i].ofs * width) / info->num_chars;
992 ww = (info->group[i].len * width) / info->num_chars;
994 hwnd = CreateWindowW( szEdit, NULL, style, wx, 0, ww, height,
995 info->hwnd, NULL, NULL, NULL );
996 if( !hwnd )
998 ERR("failed to create mask edit sub window\n");
999 break;
1002 SendMessageW( hwnd, EM_LIMITTEXT, info->group[i].len, 0 );
1004 msi_dialog_set_font( info->dialog, hwnd,
1005 font ? font : info->dialog->default_font );
1006 info->group[i].hwnd = hwnd;
1010 /* office 2003 uses "73931<````=````=````=````=`````>@@@@@" */
1011 static UINT msi_dialog_maskedit_control( msi_dialog *dialog, MSIRECORD *rec )
1013 const static WCHAR pidt[] = {'P','I','D','T','e','m','p','l','a','t','e',0};
1014 LPWSTR mask = NULL, title = NULL, val = NULL;
1015 struct msi_maskedit_info *info = NULL;
1016 UINT ret = ERROR_SUCCESS;
1017 msi_control *control;
1018 LPCWSTR prop;
1020 mask = load_dynamic_property( dialog->package, pidt, NULL );
1021 if( !mask )
1023 ERR("PIDTemplate is empty\n");
1024 goto end;
1027 info = msi_dialog_parse_groups( mask );
1028 if( !info )
1030 ERR("template %s is invalid\n", debugstr_w(mask));
1031 goto end;
1034 info->dialog = dialog;
1036 control = msi_dialog_add_control( dialog, rec, szStatic,
1037 SS_OWNERDRAW | WS_GROUP | WS_VISIBLE );
1038 if( !control )
1040 ERR("Failed to create maskedit container\n");
1041 ret = ERROR_FUNCTION_FAILED;
1042 goto end;
1044 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
1046 info->hwnd = control->hwnd;
1048 /* subclass the static control */
1049 info->oldproc = (WNDPROC) SetWindowLongPtrW( info->hwnd, GWLP_WNDPROC,
1050 (LONG_PTR)MSIMaskedEdit_WndProc );
1051 SetPropW( control->hwnd, szButtonData, info );
1053 prop = MSI_RecordGetString( rec, 9 );
1054 if( prop )
1055 info->prop = strdupW( prop );
1057 msi_maskedit_create_children( info );
1059 if( prop )
1061 val = load_dynamic_property( dialog->package, prop, NULL );
1062 if( val )
1064 msi_maskedit_set_text( info, val );
1065 HeapFree( GetProcessHeap(), 0, val );
1069 end:
1070 if( ret != ERROR_SUCCESS )
1071 HeapFree( GetProcessHeap(), 0, info );
1072 HeapFree( GetProcessHeap(), 0, title );
1073 HeapFree( GetProcessHeap(), 0, mask );
1074 return ret;
1077 /******************** Path Edit ********************************************/
1079 static UINT msi_dialog_pathedit_control( msi_dialog *dialog, MSIRECORD *rec )
1081 FIXME("not implemented properly\n");
1082 return msi_dialog_edit_control( dialog, rec );
1085 /* radio buttons are a bit different from normal controls */
1086 static UINT msi_dialog_create_radiobutton( MSIRECORD *rec, LPVOID param )
1088 radio_button_group_descr *group = (radio_button_group_descr *)param;
1089 msi_dialog *dialog = group->dialog;
1090 msi_control *control;
1091 LPCWSTR prop, text, name;
1092 DWORD style;
1093 DWORD attributes = group->attributes;
1095 style = WS_CHILD | BS_AUTORADIOBUTTON | BS_MULTILINE | WS_TABSTOP;
1096 name = MSI_RecordGetString( rec, 3 );
1097 text = MSI_RecordGetString( rec, 8 );
1098 if( attributes & 1 )
1099 style |= WS_VISIBLE;
1100 if( ~attributes & 2 )
1101 style |= WS_DISABLED;
1103 control = msi_dialog_create_window( dialog, rec, szButton, name, text,
1104 style, group->parent->hwnd );
1105 control->handler = msi_dialog_radiogroup_handler;
1107 prop = MSI_RecordGetString( rec, 1 );
1108 if( prop )
1109 control->property = strdupW( prop );
1111 return ERROR_SUCCESS;
1114 static UINT msi_dialog_radiogroup_control( msi_dialog *dialog, MSIRECORD *rec )
1116 static const WCHAR query[] = {
1117 'S','E','L','E','C','T',' ','*',' ',
1118 'F','R','O','M',' ','R','a','d','i','o','B','u','t','t','o','n',' ',
1119 'W','H','E','R','E',' ',
1120 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
1121 UINT r;
1122 LPCWSTR prop;
1123 msi_control *control;
1124 MSIQUERY *view = NULL;
1125 radio_button_group_descr group;
1126 MSIPACKAGE *package = dialog->package;
1127 WNDPROC oldproc;
1129 prop = MSI_RecordGetString( rec, 9 );
1131 TRACE("%p %p %s\n", dialog, rec, debugstr_w( prop ));
1133 /* Create parent group box to hold radio buttons */
1134 control = msi_dialog_add_control( dialog, rec, szButton, BS_OWNERDRAW|WS_GROUP );
1135 if( !control )
1136 return ERROR_FUNCTION_FAILED;
1138 oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1139 (LONG_PTR)MSIRadioGroup_WndProc );
1140 SetPropW(control->hwnd, szButtonData, oldproc);
1141 SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
1143 if( prop )
1144 control->property = strdupW( prop );
1146 /* query the Radio Button table for all control in this group */
1147 r = MSI_OpenQuery( package->db, &view, query, prop );
1148 if( r != ERROR_SUCCESS )
1150 ERR("query failed for dialog %s radio group %s\n",
1151 debugstr_w(dialog->name), debugstr_w(prop));
1152 return ERROR_INVALID_PARAMETER;
1155 group.dialog = dialog;
1156 group.parent = control;
1157 group.attributes = MSI_RecordGetInteger( rec, 8 );
1159 r = MSI_IterateRecords( view, 0, msi_dialog_create_radiobutton, &group );
1160 msiobj_release( &view->hdr );
1162 return r;
1165 struct control_handler msi_dialog_handler[] =
1167 { szText, msi_dialog_text_control },
1168 { szPushButton, msi_dialog_button_control },
1169 { szLine, msi_dialog_line_control },
1170 { szBitmap, msi_dialog_bitmap_control },
1171 { szCheckBox, msi_dialog_checkbox_control },
1172 { szScrollableText, msi_dialog_scrolltext_control },
1173 { szComboBox, msi_dialog_combo_control },
1174 { szEdit, msi_dialog_edit_control },
1175 { szMaskedEdit, msi_dialog_maskedit_control },
1176 { szPathEdit, msi_dialog_pathedit_control },
1177 { szRadioButtonGroup, msi_dialog_radiogroup_control },
1178 { szIcon, msi_dialog_icon_control },
1181 #define NUM_CONTROL_TYPES (sizeof msi_dialog_handler/sizeof msi_dialog_handler[0])
1183 static UINT msi_dialog_create_controls( MSIRECORD *rec, LPVOID param )
1185 msi_dialog *dialog = param;
1186 LPCWSTR control_type;
1187 UINT i;
1189 /* find and call the function that can create this type of control */
1190 control_type = MSI_RecordGetString( rec, 3 );
1191 for( i=0; i<NUM_CONTROL_TYPES; i++ )
1192 if (!strcmpiW( msi_dialog_handler[i].control_type, control_type ))
1193 break;
1194 if( i != NUM_CONTROL_TYPES )
1195 msi_dialog_handler[i].func( dialog, rec );
1196 else
1197 ERR("no handler for element type %s\n", debugstr_w(control_type));
1199 return ERROR_SUCCESS;
1202 static UINT msi_dialog_fill_controls( msi_dialog *dialog )
1204 static const WCHAR query[] = {
1205 'S','E','L','E','C','T',' ','*',' ',
1206 'F','R','O','M',' ','C','o','n','t','r','o','l',' ',
1207 'W','H','E','R','E',' ',
1208 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
1209 UINT r;
1210 MSIQUERY *view = NULL;
1211 MSIPACKAGE *package = dialog->package;
1213 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
1215 /* query the Control table for all the elements of the control */
1216 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
1217 if( r != ERROR_SUCCESS )
1219 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
1220 return ERROR_INVALID_PARAMETER;
1223 r = MSI_IterateRecords( view, 0, msi_dialog_create_controls, dialog );
1224 msiobj_release( &view->hdr );
1226 return r;
1229 static UINT msi_dialog_set_control_condition( MSIRECORD *rec, LPVOID param )
1231 static const WCHAR szHide[] = { 'H','i','d','e',0 };
1232 static const WCHAR szShow[] = { 'S','h','o','w',0 };
1233 static const WCHAR szDisable[] = { 'D','i','s','a','b','l','e',0 };
1234 static const WCHAR szEnable[] = { 'E','n','a','b','l','e',0 };
1235 msi_dialog *dialog = param;
1236 msi_control *control;
1237 LPCWSTR name, action, condition;
1238 UINT r;
1240 name = MSI_RecordGetString( rec, 2 );
1241 action = MSI_RecordGetString( rec, 3 );
1242 condition = MSI_RecordGetString( rec, 4 );
1243 r = MSI_EvaluateConditionW( dialog->package, condition );
1244 control = msi_dialog_find_control( dialog, name );
1245 if( r && control )
1247 TRACE("%s control %s\n", debugstr_w(action), debugstr_w(name));
1249 /* FIXME: case sensitive? */
1250 if(!lstrcmpW(action, szHide))
1251 ShowWindow(control->hwnd, SW_HIDE);
1252 else if(!strcmpW(action, szShow))
1253 ShowWindow(control->hwnd, SW_SHOW);
1254 else if(!strcmpW(action, szDisable))
1255 EnableWindow(control->hwnd, FALSE);
1256 else if(!strcmpW(action, szEnable))
1257 EnableWindow(control->hwnd, TRUE);
1258 else
1259 FIXME("Unhandled action %s\n", debugstr_w(action));
1262 return ERROR_SUCCESS;
1265 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog )
1267 static const WCHAR query[] = {
1268 'S','E','L','E','C','T',' ','*',' ',
1269 'F','R','O','M',' ',
1270 'C','o','n','t','r','o','l','C','o','n','d','i','t','i','o','n',' ',
1271 'W','H','E','R','E',' ',
1272 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0
1274 UINT r;
1275 MSIQUERY *view = NULL;
1276 MSIPACKAGE *package = dialog->package;
1278 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
1280 /* query the Control table for all the elements of the control */
1281 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
1282 if( r != ERROR_SUCCESS )
1284 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
1285 return ERROR_INVALID_PARAMETER;
1288 r = MSI_IterateRecords( view, 0, msi_dialog_set_control_condition, dialog );
1289 msiobj_release( &view->hdr );
1291 return r;
1294 /* figure out the height of 10 point MS Sans Serif */
1295 static INT msi_dialog_get_sans_serif_height( HWND hwnd )
1297 static const WCHAR szSansSerif[] = {
1298 'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0 };
1299 LOGFONTW lf;
1300 TEXTMETRICW tm;
1301 BOOL r;
1302 LONG height = 0;
1303 HFONT hFont, hOldFont;
1304 HDC hdc;
1306 hdc = GetDC( hwnd );
1307 if (hdc)
1309 memset( &lf, 0, sizeof lf );
1310 lf.lfHeight = MulDiv(10, GetDeviceCaps(hdc, LOGPIXELSY), 72);
1311 strcpyW( lf.lfFaceName, szSansSerif );
1312 hFont = CreateFontIndirectW(&lf);
1313 if (hFont)
1315 hOldFont = SelectObject( hdc, hFont );
1316 r = GetTextMetricsW( hdc, &tm );
1317 if (r)
1318 height = tm.tmHeight;
1319 SelectObject( hdc, hOldFont );
1320 DeleteObject( hFont );
1322 ReleaseDC( hwnd, hdc );
1324 return height;
1327 /* fetch the associated record from the Dialog table */
1328 static MSIRECORD *msi_get_dialog_record( msi_dialog *dialog )
1330 static const WCHAR query[] = {
1331 'S','E','L','E','C','T',' ','*',' ',
1332 'F','R','O','M',' ','D','i','a','l','o','g',' ',
1333 'W','H','E','R','E',' ',
1334 '`','D','i','a','l','o','g','`',' ','=',' ','\'','%','s','\'',0};
1335 MSIPACKAGE *package = dialog->package;
1336 MSIRECORD *rec = NULL;
1338 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
1340 rec = MSI_QueryGetRecord( package->db, query, dialog->name );
1341 if( !rec )
1342 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
1344 return rec;
1347 static void msi_dialog_adjust_dialog_size( msi_dialog *dialog, LPSIZE sz )
1349 RECT rect;
1350 LONG style;
1352 /* turn the client size into the window rectangle */
1353 rect.left = 0;
1354 rect.top = 0;
1355 rect.right = msi_dialog_scale_unit( dialog, sz->cx );
1356 rect.bottom = msi_dialog_scale_unit( dialog, sz->cy );
1357 style = GetWindowLongPtrW( dialog->hwnd, GWL_STYLE );
1358 AdjustWindowRect( &rect, style, FALSE );
1359 sz->cx = rect.right - rect.left;
1360 sz->cy = rect.bottom - rect.top;
1363 static BOOL msi_control_set_next( msi_control *control, msi_control *next )
1365 return SetWindowPos( next->hwnd, control->hwnd, 0, 0, 0, 0,
1366 SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOREDRAW |
1367 SWP_NOREPOSITION | SWP_NOSENDCHANGING | SWP_NOSIZE );
1370 static UINT msi_dialog_set_tab_order( msi_dialog *dialog )
1372 msi_control *control, *tab_next;
1374 for( control = dialog->control_list; control; control = control->next )
1376 tab_next = msi_dialog_find_control( dialog, control->tabnext );
1377 if( !tab_next )
1378 continue;
1379 msi_control_set_next( control, tab_next );
1382 return ERROR_SUCCESS;
1385 static void msi_dialog_set_first_control( msi_dialog* dialog, LPCWSTR name )
1387 msi_control *control;
1389 control = msi_dialog_find_control( dialog, name );
1390 if( control )
1391 dialog->hWndFocus = control->hwnd;
1392 else
1393 dialog->hWndFocus = NULL;
1396 static LRESULT msi_dialog_oncreate( HWND hwnd, LPCREATESTRUCTW cs )
1398 static const WCHAR df[] = {
1399 'D','e','f','a','u','l','t','U','I','F','o','n','t',0 };
1400 msi_dialog *dialog = (msi_dialog*) cs->lpCreateParams;
1401 MSIRECORD *rec = NULL;
1402 LPCWSTR text;
1403 LPWSTR title = NULL;
1404 SIZE size;
1406 TRACE("%p %p\n", dialog, dialog->package);
1408 dialog->hwnd = hwnd;
1409 SetWindowLongPtrW( hwnd, GWLP_USERDATA, (LONG_PTR) dialog );
1411 rec = msi_get_dialog_record( dialog );
1412 if( !rec )
1414 TRACE("No record found for dialog %s\n", debugstr_w(dialog->name));
1415 return -1;
1418 dialog->scale = msi_dialog_get_sans_serif_height(dialog->hwnd);
1420 size.cx = MSI_RecordGetInteger( rec, 4 );
1421 size.cy = MSI_RecordGetInteger( rec, 5 );
1422 msi_dialog_adjust_dialog_size( dialog, &size );
1424 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
1425 text = MSI_RecordGetString( rec, 7 );
1427 dialog->default_font = load_dynamic_property( dialog->package, df, NULL );
1429 deformat_string( dialog->package, text, &title );
1430 SetWindowTextW( hwnd, title );
1431 SetWindowPos( hwnd, 0, 0, 0, size.cx, size.cy,
1432 SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW );
1434 HeapFree( GetProcessHeap(), 0, title );
1436 msi_dialog_build_font_list( dialog );
1437 msi_dialog_fill_controls( dialog );
1438 msi_dialog_evaluate_control_conditions( dialog );
1439 msi_dialog_set_tab_order( dialog );
1440 msi_dialog_set_first_control( dialog, MSI_RecordGetString( rec, 8 ) );
1441 msiobj_release( &rec->hdr );
1443 return 0;
1446 static UINT msi_dialog_send_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
1448 LPWSTR event_fmt = NULL, arg_fmt = NULL;
1450 TRACE("Sending control event %s %s\n", debugstr_w(event), debugstr_w(arg));
1452 deformat_string( dialog->package, event, &event_fmt );
1453 deformat_string( dialog->package, arg, &arg_fmt );
1455 dialog->event_handler( dialog->package, event_fmt, arg_fmt, dialog );
1457 HeapFree( GetProcessHeap(), 0, event_fmt );
1458 HeapFree( GetProcessHeap(), 0, arg_fmt );
1460 return ERROR_SUCCESS;
1463 static UINT msi_dialog_set_property( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
1465 static const WCHAR szNullArg[] = { '{','}',0 };
1466 LPWSTR p, prop, arg_fmt = NULL;
1467 UINT len;
1469 len = strlenW(event);
1470 prop = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR));
1471 strcpyW( prop, &event[1] );
1472 p = strchrW( prop, ']' );
1473 if( p && p[1] == 0 )
1475 *p = 0;
1476 if( strcmpW( szNullArg, arg ) )
1477 deformat_string( dialog->package, arg, &arg_fmt );
1478 MSI_SetPropertyW( dialog->package, prop, arg_fmt );
1480 else
1481 ERR("Badly formatted property string - what happens?\n");
1482 HeapFree( GetProcessHeap(), 0, prop );
1483 return ERROR_SUCCESS;
1486 static UINT msi_dialog_control_event( MSIRECORD *rec, LPVOID param )
1488 msi_dialog *dialog = param;
1489 LPCWSTR condition, event, arg;
1490 UINT r;
1492 condition = MSI_RecordGetString( rec, 5 );
1493 r = MSI_EvaluateConditionW( dialog->package, condition );
1494 if( r )
1496 event = MSI_RecordGetString( rec, 3 );
1497 arg = MSI_RecordGetString( rec, 4 );
1498 if( event[0] == '[' )
1499 msi_dialog_set_property( dialog, event, arg );
1500 else
1501 msi_dialog_send_event( dialog, event, arg );
1504 return ERROR_SUCCESS;
1507 static UINT msi_dialog_button_handler( msi_dialog *dialog,
1508 msi_control *control, WPARAM param )
1510 static const WCHAR query[] = {
1511 'S','E','L','E','C','T',' ','*',' ',
1512 'F','R','O','M',' ','C','o','n','t','r','o','l','E','v','e','n','t',' ',
1513 'W','H','E','R','E',' ',
1514 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
1515 'A','N','D',' ',
1516 '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',' ',
1517 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','i','n','g','`',0
1519 MSIQUERY *view = NULL;
1520 UINT r;
1522 if( HIWORD(param) != BN_CLICKED )
1523 return ERROR_SUCCESS;
1525 r = MSI_OpenQuery( dialog->package->db, &view, query,
1526 dialog->name, control->name );
1527 if( r != ERROR_SUCCESS )
1529 ERR("query failed\n");
1530 return 0;
1533 r = MSI_IterateRecords( view, 0, msi_dialog_control_event, dialog );
1534 msiobj_release( &view->hdr );
1536 return r;
1539 static UINT msi_dialog_get_checkbox_state( msi_dialog *dialog,
1540 msi_control *control )
1542 WCHAR state[2] = { 0 };
1543 DWORD sz = 2;
1545 MSI_GetPropertyW( dialog->package, control->property, state, &sz );
1546 return state[0] ? 1 : 0;
1549 static void msi_dialog_set_checkbox_state( msi_dialog *dialog,
1550 msi_control *control, UINT state )
1552 static const WCHAR szState[] = { '1', 0 };
1553 LPCWSTR val;
1555 /* if uncheck then the property is set to NULL */
1556 if (!state)
1558 MSI_SetPropertyW( dialog->package, control->property, NULL );
1559 return;
1562 /* check for a custom state */
1563 if (control->value && control->value[0])
1564 val = control->value;
1565 else
1566 val = szState;
1568 MSI_SetPropertyW( dialog->package, control->property, val );
1571 static void msi_dialog_checkbox_sync_state( msi_dialog *dialog,
1572 msi_control *control )
1574 UINT state;
1576 state = msi_dialog_get_checkbox_state( dialog, control );
1577 SendMessageW( control->hwnd, BM_SETCHECK,
1578 state ? BST_CHECKED : BST_UNCHECKED, 0 );
1581 static UINT msi_dialog_checkbox_handler( msi_dialog *dialog,
1582 msi_control *control, WPARAM param )
1584 UINT state;
1586 if( HIWORD(param) != BN_CLICKED )
1587 return ERROR_SUCCESS;
1589 TRACE("clicked checkbox %s, set %s\n", debugstr_w(control->name),
1590 debugstr_w(control->property));
1592 state = msi_dialog_get_checkbox_state( dialog, control );
1593 state = state ? 0 : 1;
1594 msi_dialog_set_checkbox_state( dialog, control, state );
1595 msi_dialog_checkbox_sync_state( dialog, control );
1597 return msi_dialog_button_handler( dialog, control, param );
1600 static UINT msi_dialog_edit_handler( msi_dialog *dialog,
1601 msi_control *control, WPARAM param )
1603 UINT sz, r;
1604 LPWSTR buf;
1606 if( HIWORD(param) != EN_CHANGE )
1607 return ERROR_SUCCESS;
1609 TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
1610 debugstr_w(control->property));
1612 sz = 0x20;
1613 buf = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
1614 while( buf )
1616 r = GetWindowTextW( control->hwnd, buf, sz );
1617 if( r < (sz-1) )
1618 break;
1619 sz *= 2;
1620 buf = HeapReAlloc( GetProcessHeap(), 0, buf, sz*sizeof(WCHAR) );
1623 MSI_SetPropertyW( dialog->package, control->property, buf );
1625 HeapFree( GetProcessHeap(), 0, buf );
1627 return ERROR_SUCCESS;
1630 static UINT msi_dialog_radiogroup_handler( msi_dialog *dialog,
1631 msi_control *control, WPARAM param )
1633 if( HIWORD(param) != BN_CLICKED )
1634 return ERROR_SUCCESS;
1636 TRACE("clicked radio button %s, set %s\n", debugstr_w(control->name),
1637 debugstr_w(control->property));
1639 MSI_SetPropertyW( dialog->package, control->property, control->name );
1641 return msi_dialog_button_handler( dialog, control, param );
1644 static LRESULT msi_dialog_oncommand( msi_dialog *dialog, WPARAM param, HWND hwnd )
1646 msi_control *control;
1648 TRACE("%p %p %08x\n", dialog, hwnd, param);
1650 control = msi_dialog_find_control_by_hwnd( dialog, hwnd );
1651 if( control )
1653 if( control->handler )
1655 control->handler( dialog, control, param );
1656 msi_dialog_evaluate_control_conditions( dialog );
1659 else
1660 ERR("button click from nowhere\n");
1661 return 0;
1664 static void msi_dialog_setfocus( msi_dialog *dialog )
1666 HWND hwnd = dialog->hWndFocus;
1668 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, TRUE);
1669 hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, FALSE);
1670 SetFocus( hwnd );
1671 dialog->hWndFocus = hwnd;
1674 static LRESULT WINAPI MSIDialog_WndProc( HWND hwnd, UINT msg,
1675 WPARAM wParam, LPARAM lParam )
1677 msi_dialog *dialog = (LPVOID) GetWindowLongPtrW( hwnd, GWLP_USERDATA );
1679 TRACE("0x%04x\n", msg);
1681 switch (msg)
1683 case WM_CREATE:
1684 return msi_dialog_oncreate( hwnd, (LPCREATESTRUCTW)lParam );
1686 case WM_COMMAND:
1687 return msi_dialog_oncommand( dialog, wParam, (HWND)lParam );
1689 case WM_ACTIVATE:
1690 if( LOWORD(wParam) == WA_INACTIVE )
1691 dialog->hWndFocus = GetFocus();
1692 else
1693 msi_dialog_setfocus( dialog );
1694 return 0;
1696 case WM_SETFOCUS:
1697 msi_dialog_setfocus( dialog );
1698 return 0;
1700 /* bounce back to our subclassed static control */
1701 case WM_CTLCOLORSTATIC:
1702 return SendMessageW( (HWND) lParam, WM_CTLCOLORSTATIC, wParam, lParam );
1704 case WM_DESTROY:
1705 dialog->hwnd = NULL;
1706 return 0;
1708 return DefWindowProcW(hwnd, msg, wParam, lParam);
1711 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1713 WNDPROC oldproc = (WNDPROC) GetPropW(hWnd, szButtonData);
1715 TRACE("hWnd %p msg %04x wParam 0x%08x lParam 0x%08lx\n", hWnd, msg, wParam, lParam);
1717 if (msg == WM_COMMAND) /* Forward notifications to dialog */
1718 SendMessageW(GetParent(hWnd), msg, wParam, lParam);
1720 return CallWindowProcW(oldproc, hWnd, msg, wParam, lParam);
1723 static LRESULT WINAPI MSIHiddenWindowProc( HWND hwnd, UINT msg,
1724 WPARAM wParam, LPARAM lParam )
1726 msi_dialog *dialog = (msi_dialog*) lParam;
1728 TRACE("%d %p\n", msg, dialog);
1730 switch (msg)
1732 case WM_MSI_DIALOG_CREATE:
1733 return msi_dialog_run_message_loop( dialog );
1734 case WM_MSI_DIALOG_DESTROY:
1735 msi_dialog_destroy( dialog );
1736 return 0;
1738 return DefWindowProcW( hwnd, msg, wParam, lParam );
1741 /* functions that interface to other modules within MSI */
1743 msi_dialog *msi_dialog_create( MSIPACKAGE* package, LPCWSTR szDialogName,
1744 msi_dialog_event_handler event_handler )
1746 MSIRECORD *rec = NULL;
1747 msi_dialog *dialog;
1749 TRACE("%p %s\n", package, debugstr_w(szDialogName));
1751 /* allocate the structure for the dialog to use */
1752 dialog = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
1753 sizeof *dialog + sizeof(WCHAR)*strlenW(szDialogName) );
1754 if( !dialog )
1755 return NULL;
1756 strcpyW( dialog->name, szDialogName );
1757 msiobj_addref( &package->hdr );
1758 dialog->package = package;
1759 dialog->event_handler = event_handler;
1760 dialog->finished = 0;
1762 /* verify that the dialog exists */
1763 rec = msi_get_dialog_record( dialog );
1764 if( !rec )
1766 HeapFree( GetProcessHeap(), 0, dialog );
1767 return NULL;
1769 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
1770 msiobj_release( &rec->hdr );
1772 return dialog;
1775 static void msi_process_pending_messages( HWND hdlg )
1777 MSG msg;
1779 while( PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ) )
1781 if( hdlg && IsDialogMessageW( hdlg, &msg ))
1782 continue;
1783 TranslateMessage( &msg );
1784 DispatchMessageW( &msg );
1788 void msi_dialog_end_dialog( msi_dialog *dialog )
1790 TRACE("%p\n", dialog);
1791 dialog->finished = 1;
1792 PostMessageW(dialog->hwnd, WM_NULL, 0, 0);
1795 void msi_dialog_check_messages( HANDLE handle )
1797 DWORD r;
1799 /* in threads other than the UI thread, block */
1800 if( uiThreadId != GetCurrentThreadId() )
1802 if( handle )
1803 WaitForSingleObject( handle, INFINITE );
1804 return;
1807 /* there's two choices for the UI thread */
1808 while (1)
1810 msi_process_pending_messages( NULL );
1812 if( !handle )
1813 break;
1816 * block here until somebody creates a new dialog or
1817 * the handle we're waiting on becomes ready
1819 r = MsgWaitForMultipleObjects( 1, &handle, 0, INFINITE, QS_ALLINPUT );
1820 if( r == WAIT_OBJECT_0 )
1821 break;
1825 UINT msi_dialog_run_message_loop( msi_dialog *dialog )
1827 HWND hwnd;
1829 if( !(dialog->attributes & msidbDialogAttributesVisible) )
1830 return ERROR_SUCCESS;
1832 if( uiThreadId != GetCurrentThreadId() )
1833 return SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_CREATE, 0, (LPARAM) dialog );
1835 /* create the dialog window, don't show it yet */
1836 hwnd = CreateWindowW( szMsiDialogClass, dialog->name, WS_OVERLAPPEDWINDOW,
1837 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
1838 NULL, NULL, NULL, dialog );
1839 if( !hwnd )
1841 ERR("Failed to create dialog %s\n", debugstr_w( dialog->name ));
1842 return ERROR_FUNCTION_FAILED;
1845 ShowWindow( hwnd, SW_SHOW );
1846 UpdateWindow( hwnd );
1848 if( dialog->attributes & msidbDialogAttributesModal )
1850 while( !dialog->finished )
1852 MsgWaitForMultipleObjects( 0, NULL, 0, INFINITE, QS_ALLEVENTS );
1853 msi_process_pending_messages( dialog->hwnd );
1856 else
1857 return ERROR_IO_PENDING;
1859 return ERROR_SUCCESS;
1862 void msi_dialog_do_preview( msi_dialog *dialog )
1864 TRACE("\n");
1865 dialog->attributes |= msidbDialogAttributesVisible;
1866 dialog->attributes &= ~msidbDialogAttributesModal;
1867 msi_dialog_run_message_loop( dialog );
1870 void msi_dialog_destroy( msi_dialog *dialog )
1872 if( uiThreadId != GetCurrentThreadId() )
1874 SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_DESTROY, 0, (LPARAM) dialog );
1875 return;
1878 if( dialog->hwnd )
1879 ShowWindow( dialog->hwnd, SW_HIDE );
1881 /* destroy the list of controls */
1882 while( dialog->control_list )
1884 msi_control *t = dialog->control_list;
1885 dialog->control_list = t->next;
1886 /* leave dialog->hwnd - destroying parent destroys child windows */
1887 HeapFree( GetProcessHeap(), 0, t->property );
1888 HeapFree( GetProcessHeap(), 0, t->value );
1889 if( t->hBitmap )
1890 DeleteObject( t->hBitmap );
1891 if( t->hIcon )
1892 DestroyIcon( t->hIcon );
1893 HeapFree( GetProcessHeap(), 0, t->tabnext );
1894 HeapFree( GetProcessHeap(), 0, t );
1897 /* destroy the list of fonts */
1898 while( dialog->font_list )
1900 msi_font *t = dialog->font_list;
1901 dialog->font_list = t->next;
1902 DeleteObject( t->hfont );
1903 HeapFree( GetProcessHeap(), 0, t );
1905 HeapFree( GetProcessHeap(), 0, dialog->default_font );
1907 if( dialog->hwnd )
1908 DestroyWindow( dialog->hwnd );
1910 msiobj_release( &dialog->package->hdr );
1911 dialog->package = NULL;
1912 HeapFree( GetProcessHeap(), 0, dialog );
1915 BOOL msi_dialog_register_class( void )
1917 WNDCLASSW cls;
1919 ZeroMemory( &cls, sizeof cls );
1920 cls.lpfnWndProc = MSIDialog_WndProc;
1921 cls.hInstance = NULL;
1922 cls.hIcon = LoadIconW(0, (LPWSTR)IDI_APPLICATION);
1923 cls.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
1924 cls.hbrBackground = (HBRUSH)(COLOR_WINDOW);
1925 cls.lpszMenuName = NULL;
1926 cls.lpszClassName = szMsiDialogClass;
1928 if( !RegisterClassW( &cls ) )
1929 return FALSE;
1931 cls.lpfnWndProc = MSIHiddenWindowProc;
1932 cls.lpszClassName = szMsiHiddenWindow;
1934 if( !RegisterClassW( &cls ) )
1935 return FALSE;
1937 uiThreadId = GetCurrentThreadId();
1939 hMsiHiddenWindow = CreateWindowW( szMsiHiddenWindow, NULL, WS_OVERLAPPED,
1940 0, 0, 100, 100, NULL, NULL, NULL, NULL );
1941 if( !hMsiHiddenWindow )
1942 return FALSE;
1944 hRichedit = LoadLibraryA("riched20");
1946 return TRUE;
1949 void msi_dialog_unregister_class( void )
1951 DestroyWindow( hMsiHiddenWindow );
1952 UnregisterClassW( szMsiDialogClass, NULL );
1953 uiThreadId = 0;
1954 FreeLibrary( hRichedit );