Sync to Wine-20050419:
[reactos.git] / reactos / lib / msi / dialog.c
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
3 *
4 * Copyright 2005 Mike McCormack for CodeWeavers
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #define COBJMACROS
22
23 #include <stdarg.h>
24
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
36 #include "wine/debug.h"
37 #include "wine/unicode.h"
38
39 #include "action.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(msi);
42
43
44 const WCHAR szMsiDialogClass[] = {
45 'M','s','i','D','i','a','l','o','g','C','l','o','s','e','C','l','a','s','s',0
46 };
47 const static WCHAR szStatic[] = { 'S','t','a','t','i','c',0 };
48 const static WCHAR szButton[] = { 'B','U','T','T','O','N', 0 };
49
50 const static WCHAR szButtonData[] = { 'M','S','I','D','A','T','A',0 };
51
52 struct msi_control_tag;
53 typedef struct msi_control_tag msi_control;
54 typedef UINT (*msi_handler)( msi_dialog *, msi_control *, WPARAM );
55
56 struct msi_control_tag
57 {
58 struct msi_control_tag *next;
59 HWND hwnd;
60 msi_handler handler;
61 LPWSTR property;
62 IPicture *pic;
63 WCHAR name[1];
64 };
65
66 typedef struct msi_font_tag
67 {
68 struct msi_font_tag *next;
69 HFONT hfont;
70 WCHAR name[1];
71 } msi_font;
72
73 struct msi_dialog_tag
74 {
75 MSIPACKAGE *package;
76 msi_dialog_event_handler event_handler;
77 BOOL finished;
78 INT scale;
79 DWORD attributes;
80 HWND hwnd;
81 LPWSTR default_font;
82 msi_font *font_list;
83 msi_control *control_list;
84 WCHAR name[1];
85 };
86
87 typedef UINT (*msi_dialog_control_func)( msi_dialog *dialog, MSIRECORD *rec );
88 struct control_handler
89 {
90 LPCWSTR control_type;
91 msi_dialog_control_func func;
92 };
93
94 typedef struct
95 {
96 msi_dialog* dialog;
97 msi_control *parent;
98 DWORD attributes;
99 } radio_button_group_descr;
100
101 static UINT msi_dialog_checkbox_handler( msi_dialog *, msi_control *, WPARAM );
102 static void msi_dialog_checkbox_sync_state( msi_dialog *, msi_control * );
103 static UINT msi_dialog_button_handler( msi_dialog *, msi_control *, WPARAM );
104 static UINT msi_dialog_edit_handler( msi_dialog *, msi_control *, WPARAM );
105 static UINT msi_dialog_radiogroup_handler( msi_dialog *, msi_control *, WPARAM param );
106 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
107
108
109 INT msi_dialog_scale_unit( msi_dialog *dialog, INT val )
110 {
111 return (dialog->scale * val + 5) / 10;
112 }
113
114 /*
115 * msi_dialog_get_style
116 *
117 * Extract the {\style} string from the front of the text to display and
118 * update the pointer.
119 */
120 static LPWSTR msi_dialog_get_style( LPCWSTR *text )
121 {
122 LPWSTR ret = NULL;
123 LPCWSTR p = *text, q;
124 DWORD len;
125
126 if( *p++ != '{' )
127 return ret;
128 q = strchrW( p, '}' );
129 if( !q )
130 return ret;
131 *text = ++q;
132 if( *p++ != '\\' )
133 return ret;
134 len = q - p;
135
136 ret = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
137 if( !ret )
138 return ret;
139 memcpy( ret, p, len*sizeof(WCHAR) );
140 ret[len-1] = 0;
141 return ret;
142 }
143
144 static UINT msi_dialog_add_font( MSIRECORD *rec, LPVOID param )
145 {
146 msi_dialog *dialog = param;
147 msi_font *font;
148 LPCWSTR face, name;
149 LOGFONTW lf;
150 INT style;
151 HDC hdc;
152
153 /* create a font and add it to the list */
154 name = MSI_RecordGetString( rec, 1 );
155 font = HeapAlloc( GetProcessHeap(), 0,
156 sizeof *font + strlenW( name )*sizeof (WCHAR) );
157 strcpyW( font->name, name );
158 font->next = dialog->font_list;
159 dialog->font_list = font;
160
161 memset( &lf, 0, sizeof lf );
162 face = MSI_RecordGetString( rec, 2 );
163 lf.lfHeight = MSI_RecordGetInteger( rec, 3 );
164 style = MSI_RecordGetInteger( rec, 5 );
165 if( style & msidbTextStyleStyleBitsBold )
166 lf.lfWeight = FW_BOLD;
167 if( style & msidbTextStyleStyleBitsItalic )
168 lf.lfItalic = TRUE;
169 if( style & msidbTextStyleStyleBitsUnderline )
170 lf.lfUnderline = TRUE;
171 if( style & msidbTextStyleStyleBitsStrike )
172 lf.lfStrikeOut = TRUE;
173 lstrcpynW( lf.lfFaceName, face, LF_FACESIZE );
174
175 /* adjust the height */
176 hdc = GetDC( dialog->hwnd );
177 if (hdc)
178 {
179 lf.lfHeight = -MulDiv(lf.lfHeight, GetDeviceCaps(hdc, LOGPIXELSY), 72);
180 ReleaseDC( dialog->hwnd, hdc );
181 }
182
183 font->hfont = CreateFontIndirectW( &lf );
184
185 TRACE("Adding font style %s\n", debugstr_w(font->name) );
186
187 return ERROR_SUCCESS;
188 }
189
190 static msi_font *msi_dialog_find_font( msi_dialog *dialog, LPCWSTR name )
191 {
192 msi_font *font;
193
194 for( font = dialog->font_list; font; font = font->next )
195 if( !strcmpW( font->name, name ) ) /* FIXME: case sensitive? */
196 break;
197
198 return font;
199 }
200
201 static UINT msi_dialog_set_font( msi_dialog *dialog, HWND hwnd, LPCWSTR name )
202 {
203 msi_font *font;
204
205 font = msi_dialog_find_font( dialog, name );
206 if( font )
207 SendMessageW( hwnd, WM_SETFONT, (WPARAM) font->hfont, TRUE );
208 else
209 ERR("No font entry for %s\n", debugstr_w(name));
210 return ERROR_SUCCESS;
211 }
212
213 static UINT msi_dialog_build_font_list( msi_dialog *dialog )
214 {
215 static const WCHAR query[] = {
216 'S','E','L','E','C','T',' ','*',' ',
217 'F','R','O','M',' ','`','T','e','x','t','S','t','y','l','e','`',' ',0
218 };
219 UINT r;
220 MSIQUERY *view = NULL;
221
222 TRACE("dialog %p\n", dialog );
223
224 r = MSI_OpenQuery( dialog->package->db, &view, query );
225 if( r != ERROR_SUCCESS )
226 return r;
227
228 r = MSI_IterateRecords( view, NULL, msi_dialog_add_font, dialog );
229 msiobj_release( &view->hdr );
230
231 return r;
232 }
233
234 static msi_control *msi_dialog_create_window( msi_dialog *dialog,
235 MSIRECORD *rec, LPCWSTR szCls, LPCWSTR name, LPCWSTR text,
236 DWORD style, HWND parent )
237 {
238 DWORD x, y, width, height;
239 LPWSTR font = NULL, title = NULL;
240 msi_control *control;
241
242 style |= WS_CHILD | WS_GROUP;
243
244 control = HeapAlloc( GetProcessHeap(), 0,
245 sizeof *control + strlenW(name)*sizeof(WCHAR) );
246 strcpyW( control->name, name );
247 control->next = dialog->control_list;
248 dialog->control_list = control;
249 control->handler = NULL;
250 control->property = NULL;
251 control->pic = NULL;
252
253 x = MSI_RecordGetInteger( rec, 4 );
254 y = MSI_RecordGetInteger( rec, 5 );
255 width = MSI_RecordGetInteger( rec, 6 );
256 height = MSI_RecordGetInteger( rec, 7 );
257
258 x = msi_dialog_scale_unit( dialog, x );
259 y = msi_dialog_scale_unit( dialog, y );
260 width = msi_dialog_scale_unit( dialog, width );
261 height = msi_dialog_scale_unit( dialog, height );
262
263 if( text )
264 {
265 font = msi_dialog_get_style( &text );
266 deformat_string( dialog->package, text, &title );
267 }
268
269 control->hwnd = CreateWindowW( szCls, title, style,
270 x, y, width, height, parent, NULL, NULL, NULL );
271
272 TRACE("Dialog %s control %s hwnd %p\n",
273 debugstr_w(dialog->name), debugstr_w(text), control->hwnd );
274
275 msi_dialog_set_font( dialog, control->hwnd,
276 font ? font : dialog->default_font );
277
278 HeapFree( GetProcessHeap(), 0, font );
279 HeapFree( GetProcessHeap(), 0, title );
280
281 return control;
282 }
283
284 /* everything except radio buttons */
285 static msi_control *msi_dialog_add_control( msi_dialog *dialog,
286 MSIRECORD *rec, LPCWSTR szCls, DWORD style )
287 {
288 DWORD attributes;
289 LPCWSTR text, name;
290
291 name = MSI_RecordGetString( rec, 2 );
292 attributes = MSI_RecordGetInteger( rec, 8 );
293 text = MSI_RecordGetString( rec, 10 );
294 if( attributes & 1 )
295 style |= WS_VISIBLE;
296 if( ~attributes & 2 )
297 style |= WS_DISABLED;
298 return msi_dialog_create_window( dialog, rec, szCls, name, text,
299 style, dialog->hwnd );
300 }
301
302 static UINT msi_dialog_text_control( msi_dialog *dialog, MSIRECORD *rec )
303 {
304 TRACE("%p %p\n", dialog, rec);
305
306 msi_dialog_add_control( dialog, rec, szStatic, 0 );
307 return ERROR_SUCCESS;
308 }
309
310 static UINT msi_dialog_button_control( msi_dialog *dialog, MSIRECORD *rec )
311 {
312 msi_control *control;
313
314 TRACE("%p %p\n", dialog, rec);
315
316 control = msi_dialog_add_control( dialog, rec, szButton, 0 );
317 control->handler = msi_dialog_button_handler;
318
319 return ERROR_SUCCESS;
320 }
321
322 static UINT msi_dialog_checkbox_control( msi_dialog *dialog, MSIRECORD *rec )
323 {
324 msi_control *control;
325 LPCWSTR prop;
326
327 TRACE("%p %p\n", dialog, rec);
328
329 control = msi_dialog_add_control( dialog, rec, szButton,
330 BS_CHECKBOX | BS_MULTILINE );
331 control->handler = msi_dialog_checkbox_handler;
332 prop = MSI_RecordGetString( rec, 9 );
333 if( prop )
334 control->property = strdupW( prop );
335 msi_dialog_checkbox_sync_state( dialog, control );
336
337 return ERROR_SUCCESS;
338 }
339
340 static UINT msi_dialog_line_control( msi_dialog *dialog, MSIRECORD *rec )
341 {
342 TRACE("%p %p\n", dialog, rec);
343
344 msi_dialog_add_control( dialog, rec, szStatic, SS_ETCHEDHORZ | SS_SUNKEN );
345 return ERROR_SUCCESS;
346 }
347
348 static UINT msi_dialog_scrolltext_control( msi_dialog *dialog, MSIRECORD *rec )
349 {
350 const static WCHAR szEdit[] = { 'E','D','I','T',0 };
351
352 FIXME("%p %p\n", dialog, rec);
353
354 msi_dialog_add_control( dialog, rec, szEdit, WS_BORDER |
355 ES_MULTILINE | WS_VSCROLL | ES_READONLY | ES_AUTOVSCROLL );
356
357 return ERROR_SUCCESS;
358 }
359
360 static UINT msi_load_bitmap( MSIDATABASE *db, LPCWSTR name, IPicture **pic )
361 {
362 const static WCHAR query[] = {
363 's','e','l','e','c','t',' ','*',' ',
364 'f','r','o','m',' ','B','i','n','a','r','y',' ',
365 'w','h','e','r','e',' ',
366 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0
367 };
368 MSIQUERY *view = NULL;
369 MSIRECORD *rec = NULL;
370 IStream *stm = NULL;
371 UINT r;
372
373 r = MSI_OpenQuery( db, &view, query, name );
374 if( r != ERROR_SUCCESS )
375 return r;
376
377 MSI_ViewExecute( view, NULL );
378 MSI_ViewFetch( view, &rec );
379 MSI_ViewClose( view );
380 msiobj_release( &view->hdr );
381
382 if( !rec )
383 return ERROR_FUNCTION_FAILED;
384
385 r = MSI_RecordGetIStream( rec, 2, &stm );
386 msiobj_release( &rec->hdr );
387 if( r != ERROR_SUCCESS )
388 return r;
389
390 r = OleLoadPicture( stm, 0, TRUE, &IID_IPicture, (LPVOID*) pic );
391 IStream_Release( stm );
392 if( FAILED( r ) )
393 return ERROR_FUNCTION_FAILED;
394
395 return ERROR_SUCCESS;
396 }
397
398 static UINT msi_dialog_bitmap_control( msi_dialog *dialog, MSIRECORD *rec )
399 {
400 IPicture *pic = NULL;
401 msi_control *control;
402 OLE_HANDLE hBitmap = 0;
403 LPCWSTR text;
404 UINT r;
405
406 control = msi_dialog_add_control( dialog, rec, szStatic,
407 SS_BITMAP | SS_LEFT | SS_CENTERIMAGE );
408 text = MSI_RecordGetString( rec, 10 );
409 r = msi_load_bitmap( dialog->package->db, text, &pic );
410 if( r == ERROR_SUCCESS )
411 {
412 r = IPicture_get_Handle( pic, &hBitmap );
413 if( SUCCEEDED( r ) )
414 SendMessageW( control->hwnd, STM_SETIMAGE, IMAGE_BITMAP, hBitmap );
415 control->pic = pic;
416 }
417
418 return ERROR_SUCCESS;
419 }
420
421 static UINT msi_dialog_combo_control( msi_dialog *dialog, MSIRECORD *rec )
422 {
423 static const WCHAR szCombo[] = { 'C','O','M','B','O','B','O','X',0 };
424
425 msi_dialog_add_control( dialog, rec, szCombo,
426 SS_BITMAP | SS_LEFT | SS_CENTERIMAGE );
427 return ERROR_SUCCESS;
428 }
429
430 static UINT msi_dialog_edit_control( msi_dialog *dialog, MSIRECORD *rec )
431 {
432 const static WCHAR szEdit[] = { 'E','D','I','T',0 };
433 msi_control *control;
434 LPCWSTR prop;
435 LPWSTR val;
436
437 control = msi_dialog_add_control( dialog, rec, szEdit, WS_BORDER );
438 control->handler = msi_dialog_edit_handler;
439 prop = MSI_RecordGetString( rec, 9 );
440 if( prop )
441 control->property = strdupW( prop );
442 val = load_dynamic_property( dialog->package, control->property, NULL );
443 SetWindowTextW( control->hwnd, val );
444 HeapFree( GetProcessHeap(), 0, val );
445 return ERROR_SUCCESS;
446 }
447
448 static UINT msi_dialog_pathedit_control( msi_dialog *dialog, MSIRECORD *rec )
449 {
450 FIXME("not implemented properly\n");
451 return msi_dialog_edit_control( dialog, rec );
452 }
453
454 /* radio buttons are a bit different from normal controls */
455 static UINT msi_dialog_create_radiobutton( MSIRECORD *rec, LPVOID param )
456 {
457 radio_button_group_descr *group = (radio_button_group_descr *)param;
458 msi_dialog *dialog = group->dialog;
459 msi_control *control;
460 LPCWSTR prop, text, name;
461 DWORD style;
462 DWORD attributes = group->attributes;
463
464 style = WS_CHILD | BS_AUTORADIOBUTTON | BS_MULTILINE;
465 name = MSI_RecordGetString( rec, 3 );
466 text = MSI_RecordGetString( rec, 8 );
467 if( attributes & 1 )
468 style |= WS_VISIBLE;
469 if( ~attributes & 2 )
470 style |= WS_DISABLED;
471
472 control = msi_dialog_create_window( dialog, rec, szButton, name, text,
473 style, group->parent->hwnd );
474 control->handler = msi_dialog_radiogroup_handler;
475
476 prop = MSI_RecordGetString( rec, 1 );
477 if( prop )
478 control->property = strdupW( prop );
479
480 return ERROR_SUCCESS;
481 }
482
483 static UINT msi_dialog_radiogroup_control( msi_dialog *dialog, MSIRECORD *rec )
484 {
485 static const WCHAR query[] = {
486 'S','E','L','E','C','T',' ','*',' ',
487 'F','R','O','M',' ','R','a','d','i','o','B','u','t','t','o','n',' ',
488 'W','H','E','R','E',' ',
489 '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
490 UINT r;
491 LPCWSTR prop;
492 msi_control *control;
493 MSIQUERY *view = NULL;
494 radio_button_group_descr group;
495 MSIPACKAGE *package = dialog->package;
496
497 prop = MSI_RecordGetString( rec, 9 );
498
499 TRACE("%p %p %s\n", dialog, rec, debugstr_w( prop ));
500
501 /* Create parent group box to hold radio buttons */
502 control = msi_dialog_add_control( dialog, rec, szButton, BS_OWNERDRAW );
503
504 if (control->hwnd)
505 {
506 WNDPROC oldproc = (WNDPROC) SetWindowLongPtrW(control->hwnd, GWLP_WNDPROC,
507 (LONG_PTR)MSIRadioGroup_WndProc);
508 SetPropW(control->hwnd, szButtonData, oldproc);
509 }
510
511 if( prop )
512 control->property = strdupW( prop );
513
514 /* query the Radio Button table for all control in this group */
515 r = MSI_OpenQuery( package->db, &view, query, prop );
516 if( r != ERROR_SUCCESS )
517 {
518 ERR("query failed for dialog %s radio group %s\n",
519 debugstr_w(dialog->name), debugstr_w(prop));
520 return ERROR_INVALID_PARAMETER;
521 }
522
523 group.dialog = dialog;
524 group.parent = control;
525 group.attributes = MSI_RecordGetInteger( rec, 8 );
526
527 r = MSI_IterateRecords( view, 0, msi_dialog_create_radiobutton, &group );
528 msiobj_release( &view->hdr );
529
530 return r;
531 }
532
533 static const WCHAR szText[] = { 'T','e','x','t',0 };
534 static const WCHAR szPushButton[] = { 'P','u','s','h','B','u','t','t','o','n',0 };
535 static const WCHAR szLine[] = { 'L','i','n','e',0 };
536 static const WCHAR szBitmap[] = { 'B','i','t','m','a','p',0 };
537 static const WCHAR szCheckBox[] = { 'C','h','e','c','k','B','o','x',0 };
538 static const WCHAR szScrollableText[] = {
539 'S','c','r','o','l','l','a','b','l','e','T','e','x','t',0 };
540 static const WCHAR szComboBox[] = { 'C','o','m','b','o','B','o','x',0 };
541 static const WCHAR szEdit[] = { 'E','d','i','t',0 };
542 static const WCHAR szMaskedEdit[] = { 'M','a','s','k','e','d','E','d','i','t',0 };
543 static const WCHAR szPathEdit[] = { 'P','a','t','h','E','d','i','t',0 };
544 static const WCHAR szRadioButtonGroup[] = {
545 'R','a','d','i','o','B','u','t','t','o','n','G','r','o','u','p',0 };
546
547 struct control_handler msi_dialog_handler[] =
548 {
549 { szText, msi_dialog_text_control },
550 { szPushButton, msi_dialog_button_control },
551 { szLine, msi_dialog_line_control },
552 { szBitmap, msi_dialog_bitmap_control },
553 { szCheckBox, msi_dialog_checkbox_control },
554 { szScrollableText, msi_dialog_scrolltext_control },
555 { szComboBox, msi_dialog_combo_control },
556 { szEdit, msi_dialog_edit_control },
557 { szMaskedEdit, msi_dialog_edit_control },
558 { szPathEdit, msi_dialog_pathedit_control },
559 { szRadioButtonGroup, msi_dialog_radiogroup_control },
560 };
561
562 #define NUM_CONTROL_TYPES (sizeof msi_dialog_handler/sizeof msi_dialog_handler[0])
563
564 static UINT msi_dialog_create_controls( MSIRECORD *rec, LPVOID param )
565 {
566 msi_dialog *dialog = param;
567 LPCWSTR control_type;
568 UINT i;
569
570 /* find and call the function that can create this type of control */
571 control_type = MSI_RecordGetString( rec, 3 );
572 for( i=0; i<NUM_CONTROL_TYPES; i++ )
573 if (!strcmpiW( msi_dialog_handler[i].control_type, control_type ))
574 break;
575 if( i != NUM_CONTROL_TYPES )
576 msi_dialog_handler[i].func( dialog, rec );
577 else
578 ERR("no handler for element type %s\n", debugstr_w(control_type));
579
580 return ERROR_SUCCESS;
581 }
582
583 static UINT msi_dialog_fill_controls( msi_dialog *dialog )
584 {
585 static const WCHAR query[] = {
586 'S','E','L','E','C','T',' ','*',' ',
587 'F','R','O','M',' ','C','o','n','t','r','o','l',' ',
588 'W','H','E','R','E',' ',
589 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
590 UINT r;
591 MSIQUERY *view = NULL;
592 MSIPACKAGE *package = dialog->package;
593
594 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
595
596 /* query the Control table for all the elements of the control */
597 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
598 if( r != ERROR_SUCCESS )
599 {
600 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
601 return ERROR_INVALID_PARAMETER;
602 }
603
604 r = MSI_IterateRecords( view, 0, msi_dialog_create_controls, dialog );
605 msiobj_release( &view->hdr );
606
607 return r;
608 }
609
610 static msi_control *msi_dialog_find_control( msi_dialog *dialog, LPCWSTR name )
611 {
612 msi_control *control;
613
614 for( control = dialog->control_list; control; control = control->next )
615 if( !strcmpW( control->name, name ) ) /* FIXME: case sensitive? */
616 break;
617 return control;
618 }
619
620 static msi_control *msi_dialog_find_control_by_hwnd( msi_dialog *dialog, HWND hwnd )
621 {
622 msi_control *control;
623
624 for( control = dialog->control_list; control; control = control->next )
625 if( hwnd == control->hwnd )
626 break;
627 return control;
628 }
629
630 static UINT msi_dialog_set_control_condition( MSIRECORD *rec, LPVOID param )
631 {
632 static const WCHAR szHide[] = { 'H','i','d','e',0 };
633 static const WCHAR szShow[] = { 'S','h','o','w',0 };
634 static const WCHAR szDisable[] = { 'D','i','s','a','b','l','e',0 };
635 static const WCHAR szEnable[] = { 'E','n','a','b','l','e',0 };
636 msi_dialog *dialog = param;
637 msi_control *control;
638 LPCWSTR name, action, condition;
639 UINT r;
640
641 name = MSI_RecordGetString( rec, 2 );
642 action = MSI_RecordGetString( rec, 3 );
643 condition = MSI_RecordGetString( rec, 4 );
644 r = MSI_EvaluateConditionW( dialog->package, condition );
645 control = msi_dialog_find_control( dialog, name );
646 if( r && control )
647 {
648 TRACE("%s control %s\n", debugstr_w(action), debugstr_w(name));
649
650 /* FIXME: case sensitive? */
651 if(!strcmpW(action, szHide))
652 ShowWindow(control->hwnd, SW_HIDE);
653 else if(!strcmpW(action, szShow))
654 ShowWindow(control->hwnd, SW_SHOW);
655 else if(!strcmpW(action, szDisable))
656 EnableWindow(control->hwnd, FALSE);
657 else if(!strcmpW(action, szEnable))
658 EnableWindow(control->hwnd, TRUE);
659 else
660 FIXME("Unhandled action %s\n", debugstr_w(action));
661 }
662
663 return ERROR_SUCCESS;
664 }
665
666 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog )
667 {
668 static const WCHAR query[] = {
669 'S','E','L','E','C','T',' ','*',' ',
670 'F','R','O','M',' ',
671 'C','o','n','t','r','o','l','C','o','n','d','i','t','i','o','n',' ',
672 'W','H','E','R','E',' ',
673 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0
674 };
675 UINT r;
676 MSIQUERY *view = NULL;
677 MSIPACKAGE *package = dialog->package;
678
679 TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
680
681 /* query the Control table for all the elements of the control */
682 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
683 if( r != ERROR_SUCCESS )
684 {
685 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
686 return ERROR_INVALID_PARAMETER;
687 }
688
689 r = MSI_IterateRecords( view, 0, msi_dialog_set_control_condition, dialog );
690 msiobj_release( &view->hdr );
691
692 return r;
693 }
694
695 /* figure out the height of 10 point MS Sans Serif */
696 static INT msi_dialog_get_sans_serif_height( HWND hwnd )
697 {
698 static const WCHAR szSansSerif[] = {
699 'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0 };
700 LOGFONTW lf;
701 TEXTMETRICW tm;
702 BOOL r;
703 LONG height = 0;
704 HFONT hFont, hOldFont;
705 HDC hdc;
706
707 hdc = GetDC( hwnd );
708 if (hdc)
709 {
710 memset( &lf, 0, sizeof lf );
711 lf.lfHeight = MulDiv(10, GetDeviceCaps(hdc, LOGPIXELSY), 72);
712 strcpyW( lf.lfFaceName, szSansSerif );
713 hFont = CreateFontIndirectW(&lf);
714 if (hFont)
715 {
716 hOldFont = SelectObject( hdc, hFont );
717 r = GetTextMetricsW( hdc, &tm );
718 if (r)
719 height = tm.tmHeight;
720 SelectObject( hdc, hOldFont );
721 DeleteObject( hFont );
722 }
723 ReleaseDC( hwnd, hdc );
724 }
725 return height;
726 }
727
728 static LRESULT msi_dialog_oncreate( HWND hwnd, LPCREATESTRUCTW cs )
729 {
730 static const WCHAR query[] = {
731 'S','E','L','E','C','T',' ','*',' ',
732 'F','R','O','M',' ','D','i','a','l','o','g',' ',
733 'W','H','E','R','E',' ',
734 '`','D','i','a','l','o','g','`',' ','=',' ','\'','%','s','\'',0};
735 static const WCHAR df[] = {
736 'D','e','f','a','u','l','t','U','I','F','o','n','t',0 };
737 msi_dialog *dialog = (msi_dialog*) cs->lpCreateParams;
738 MSIPACKAGE *package = dialog->package;
739 MSIQUERY *view = NULL;
740 MSIRECORD *rec = NULL;
741 DWORD width, height;
742 LPCWSTR text;
743 LPWSTR title = NULL;
744 UINT r;
745
746 TRACE("%p %p\n", dialog, package);
747
748 dialog->hwnd = hwnd;
749 SetWindowLongPtrW( hwnd, GWLP_USERDATA, (LONG_PTR) dialog );
750
751 /* fetch the associated record from the Dialog table */
752 r = MSI_OpenQuery( package->db, &view, query, dialog->name );
753 if( r != ERROR_SUCCESS )
754 {
755 ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
756 return -1;
757 }
758 MSI_ViewExecute( view, NULL );
759 MSI_ViewFetch( view, &rec );
760 MSI_ViewClose( view );
761 msiobj_release( &view->hdr );
762
763 if( !rec )
764 {
765 TRACE("No record found for dialog %s\n", debugstr_w(dialog->name));
766 return -1;
767 }
768
769 dialog->scale = msi_dialog_get_sans_serif_height(dialog->hwnd);
770
771 width = MSI_RecordGetInteger( rec, 4 );
772 height = MSI_RecordGetInteger( rec, 5 );
773 dialog->attributes = MSI_RecordGetInteger( rec, 6 );
774 text = MSI_RecordGetString( rec, 7 );
775
776 width = msi_dialog_scale_unit( dialog, width );
777 height = msi_dialog_scale_unit( dialog, height ) + 25; /* FIXME */
778
779 dialog->default_font = load_dynamic_property( dialog->package, df, NULL );
780
781 deformat_string( dialog->package, text, &title );
782 SetWindowTextW( hwnd, title );
783 SetWindowPos( hwnd, 0, 0, 0, width, height,
784 SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW );
785
786 HeapFree( GetProcessHeap(), 0, title );
787 msiobj_release( &rec->hdr );
788
789 msi_dialog_build_font_list( dialog );
790 msi_dialog_fill_controls( dialog );
791 msi_dialog_evaluate_control_conditions( dialog );
792
793 return 0;
794 }
795
796 static UINT msi_dialog_send_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
797 {
798 LPWSTR event_fmt = NULL, arg_fmt = NULL;
799
800 TRACE("Sending control event %s %s\n", debugstr_w(event), debugstr_w(arg));
801
802 deformat_string( dialog->package, event, &event_fmt );
803 deformat_string( dialog->package, arg, &arg_fmt );
804
805 dialog->event_handler( dialog->package, event_fmt, arg_fmt, dialog );
806
807 HeapFree( GetProcessHeap(), 0, event_fmt );
808 HeapFree( GetProcessHeap(), 0, arg_fmt );
809
810 return ERROR_SUCCESS;
811 }
812
813 static UINT msi_dialog_set_property( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
814 {
815 static const WCHAR szNullArg[] = { '{','}',0 };
816 LPWSTR p, prop, arg_fmt = NULL;
817 UINT len;
818
819 len = strlenW(event);
820 prop = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR));
821 strcpyW( prop, &event[1] );
822 p = strchrW( prop, ']' );
823 if( p && p[1] == 0 )
824 {
825 *p = 0;
826 if( strcmpW( szNullArg, arg ) )
827 deformat_string( dialog->package, arg, &arg_fmt );
828 MSI_SetPropertyW( dialog->package, prop, arg_fmt );
829 }
830 else
831 ERR("Badly formatted property string - what happens?\n");
832 HeapFree( GetProcessHeap(), 0, prop );
833 return ERROR_SUCCESS;
834 }
835
836 static UINT msi_dialog_control_event( MSIRECORD *rec, LPVOID param )
837 {
838 msi_dialog *dialog = param;
839 LPCWSTR condition, event, arg;
840 UINT r;
841
842 condition = MSI_RecordGetString( rec, 5 );
843 r = MSI_EvaluateConditionW( dialog->package, condition );
844 if( r )
845 {
846 event = MSI_RecordGetString( rec, 3 );
847 arg = MSI_RecordGetString( rec, 4 );
848 if( event[0] == '[' )
849 msi_dialog_set_property( dialog, event, arg );
850 else
851 msi_dialog_send_event( dialog, event, arg );
852 }
853
854 return ERROR_SUCCESS;
855 }
856
857 static UINT msi_dialog_button_handler( msi_dialog *dialog,
858 msi_control *control, WPARAM param )
859 {
860 static const WCHAR query[] = {
861 'S','E','L','E','C','T',' ','*',' ',
862 'F','R','O','M',' ','C','o','n','t','r','o','l','E','v','e','n','t',' ',
863 'W','H','E','R','E',' ',
864 '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
865 'A','N','D',' ',
866 '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',' ',
867 'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','i','n','g','`',0
868 };
869 MSIQUERY *view = NULL;
870 UINT r;
871
872 if( HIWORD(param) != BN_CLICKED )
873 return ERROR_SUCCESS;
874
875 r = MSI_OpenQuery( dialog->package->db, &view, query,
876 dialog->name, control->name );
877 if( r != ERROR_SUCCESS )
878 {
879 ERR("query failed\n");
880 return 0;
881 }
882
883 r = MSI_IterateRecords( view, 0, msi_dialog_control_event, dialog );
884 msiobj_release( &view->hdr );
885
886 return r;
887 }
888
889 static UINT msi_dialog_get_checkbox_state( msi_dialog *dialog,
890 msi_control *control )
891 {
892 WCHAR state[2] = { 0 };
893 DWORD sz = 2;
894
895 MSI_GetPropertyW( dialog->package, control->property, state, &sz );
896 return atoiW( state ) ? 1 : 0;
897 }
898
899 static void msi_dialog_set_checkbox_state( msi_dialog *dialog,
900 msi_control *control, UINT state )
901 {
902 WCHAR szState[2] = { '0', 0 };
903
904 if( state )
905 szState[0]++;
906 MSI_SetPropertyW( dialog->package, control->property, szState );
907 }
908
909 static void msi_dialog_checkbox_sync_state( msi_dialog *dialog,
910 msi_control *control )
911 {
912 UINT state;
913
914 state = msi_dialog_get_checkbox_state( dialog, control );
915 SendMessageW( control->hwnd, BM_SETCHECK,
916 state ? BST_CHECKED : BST_UNCHECKED, 0 );
917 }
918
919 static UINT msi_dialog_checkbox_handler( msi_dialog *dialog,
920 msi_control *control, WPARAM param )
921 {
922 UINT state;
923
924 if( HIWORD(param) != BN_CLICKED )
925 return ERROR_SUCCESS;
926
927 TRACE("clicked checkbox %s, set %s\n", debugstr_w(control->name),
928 debugstr_w(control->property));
929
930 state = msi_dialog_get_checkbox_state( dialog, control );
931 state = state ? 0 : 1;
932 msi_dialog_set_checkbox_state( dialog, control, state );
933 msi_dialog_checkbox_sync_state( dialog, control );
934
935 return msi_dialog_button_handler( dialog, control, param );
936 }
937
938 static UINT msi_dialog_edit_handler( msi_dialog *dialog,
939 msi_control *control, WPARAM param )
940 {
941 UINT sz, r;
942 LPWSTR buf;
943
944 if( HIWORD(param) != EN_CHANGE )
945 return ERROR_SUCCESS;
946
947 TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
948 debugstr_w(control->property));
949
950 sz = 0x20;
951 buf = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
952 while( buf )
953 {
954 r = GetWindowTextW( control->hwnd, buf, sz );
955 if( r < (sz-1) )
956 break;
957 sz *= 2;
958 buf = HeapReAlloc( GetProcessHeap(), 0, buf, sz*sizeof(WCHAR) );
959 }
960
961 MSI_SetPropertyW( dialog->package, control->property, buf );
962
963 HeapFree( GetProcessHeap(), 0, buf );
964
965 return ERROR_SUCCESS;
966 }
967
968 static UINT msi_dialog_radiogroup_handler( msi_dialog *dialog,
969 msi_control *control, WPARAM param )
970 {
971 if( HIWORD(param) != BN_CLICKED )
972 return ERROR_SUCCESS;
973
974 TRACE("clicked radio button %s, set %s\n", debugstr_w(control->name),
975 debugstr_w(control->property));
976
977 MSI_SetPropertyW( dialog->package, control->property, control->name );
978
979 return msi_dialog_button_handler( dialog, control, param );
980 }
981
982 static LRESULT msi_dialog_oncommand( msi_dialog *dialog, WPARAM param, HWND hwnd )
983 {
984 msi_control *control;
985
986 TRACE("%p %p %08x\n", dialog, hwnd, param);
987
988 control = msi_dialog_find_control_by_hwnd( dialog, hwnd );
989 if( control )
990 {
991 if( control->handler )
992 {
993 control->handler( dialog, control, param );
994 msi_dialog_evaluate_control_conditions( dialog );
995 }
996 }
997 else
998 ERR("button click from nowhere\n");
999 return 0;
1000 }
1001
1002 static LRESULT WINAPI MSIDialog_WndProc( HWND hwnd, UINT msg,
1003 WPARAM wParam, LPARAM lParam )
1004 {
1005 msi_dialog *dialog = (LPVOID) GetWindowLongPtrW( hwnd, GWLP_USERDATA );
1006
1007 TRACE(" 0x%04x\n", msg);
1008 switch (msg)
1009 {
1010 case WM_CREATE:
1011 return msi_dialog_oncreate( hwnd, (LPCREATESTRUCTW)lParam );
1012
1013 case WM_COMMAND:
1014 return msi_dialog_oncommand( dialog, wParam, (HWND)lParam );
1015
1016 case WM_DESTROY:
1017 dialog->hwnd = NULL;
1018 return 0;
1019 }
1020 return DefWindowProcW(hwnd, msg, wParam, lParam);
1021 }
1022
1023 /* functions that interface to other modules within MSI */
1024
1025 msi_dialog *msi_dialog_create( MSIPACKAGE* package, LPCWSTR szDialogName,
1026 msi_dialog_event_handler event_handler )
1027 {
1028 msi_dialog *dialog;
1029 HWND hwnd;
1030
1031 TRACE("%p %s\n", package, debugstr_w(szDialogName));
1032
1033 /* allocate the structure for the dialog to use */
1034 dialog = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
1035 sizeof *dialog + sizeof(WCHAR)*strlenW(szDialogName) );
1036 if( !dialog )
1037 return NULL;
1038 strcpyW( dialog->name, szDialogName );
1039 dialog->package = package;
1040 dialog->event_handler = event_handler;
1041
1042 /* create the dialog window, don't show it yet */
1043 hwnd = CreateWindowW( szMsiDialogClass, szDialogName, WS_OVERLAPPEDWINDOW,
1044 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
1045 NULL, NULL, NULL, dialog );
1046 if( !hwnd )
1047 {
1048 ERR("Failed to create dialog %s\n", debugstr_w( szDialogName ));
1049 msi_dialog_destroy( dialog );
1050 return NULL;
1051 }
1052
1053 return dialog;
1054 }
1055
1056 void msi_dialog_end_dialog( msi_dialog *dialog )
1057 {
1058 dialog->finished = 1;
1059 }
1060
1061 UINT msi_dialog_run_message_loop( msi_dialog *dialog )
1062 {
1063 MSG msg;
1064
1065 if( dialog->attributes & msidbDialogAttributesVisible )
1066 {
1067 ShowWindow( dialog->hwnd, SW_SHOW );
1068 UpdateWindow( dialog->hwnd );
1069 }
1070
1071 if( dialog->attributes & msidbDialogAttributesModal )
1072 {
1073 while( !dialog->finished && GetMessageW( &msg, 0, 0, 0 ) )
1074 {
1075 TranslateMessage( &msg );
1076 DispatchMessageW( &msg );
1077 }
1078 }
1079 else
1080 return ERROR_IO_PENDING;
1081
1082 return ERROR_SUCCESS;
1083 }
1084
1085 void msi_dialog_check_messages( msi_dialog *dialog, HANDLE handle )
1086 {
1087 MSG msg;
1088 DWORD r;
1089
1090 do
1091 {
1092 while( PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ) )
1093 {
1094 TranslateMessage( &msg );
1095 DispatchMessageW( &msg );
1096 }
1097 if( !handle )
1098 break;
1099 r = MsgWaitForMultipleObjects( 1, &handle, 0, INFINITE, QS_ALLEVENTS );
1100 }
1101 while( WAIT_OBJECT_0 != r );
1102 }
1103
1104 void msi_dialog_do_preview( msi_dialog *dialog )
1105 {
1106 dialog->attributes |= msidbDialogAttributesVisible;
1107 dialog->attributes &= ~msidbDialogAttributesModal;
1108 msi_dialog_run_message_loop( dialog );
1109 }
1110
1111 void msi_dialog_destroy( msi_dialog *dialog )
1112 {
1113 if( dialog->hwnd )
1114 ShowWindow( dialog->hwnd, SW_HIDE );
1115
1116 /* destroy the list of controls */
1117 while( dialog->control_list )
1118 {
1119 msi_control *t = dialog->control_list;
1120 dialog->control_list = t->next;
1121 /* leave dialog->hwnd - destroying parent destroys child windows */
1122 HeapFree( GetProcessHeap(), 0, t->property );
1123 if( t->pic )
1124 IPicture_Release( t->pic );
1125 HeapFree( GetProcessHeap(), 0, t );
1126 }
1127
1128 /* destroy the list of fonts */
1129 while( dialog->font_list )
1130 {
1131 msi_font *t = dialog->font_list;
1132 dialog->font_list = t->next;
1133 DeleteObject( t->hfont );
1134 HeapFree( GetProcessHeap(), 0, t );
1135 }
1136 HeapFree( GetProcessHeap(), 0, dialog->default_font );
1137
1138 if( dialog->hwnd )
1139 DestroyWindow( dialog->hwnd );
1140
1141 dialog->package = NULL;
1142 HeapFree( GetProcessHeap(), 0, dialog );
1143 }
1144
1145 void msi_dialog_register_class( void )
1146 {
1147 WNDCLASSW cls;
1148
1149 ZeroMemory( &cls, sizeof cls );
1150 cls.lpfnWndProc = MSIDialog_WndProc;
1151 cls.hInstance = NULL;
1152 cls.hIcon = LoadIconW(0, (LPWSTR)IDI_APPLICATION);
1153 cls.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
1154 cls.hbrBackground = (HBRUSH)(COLOR_WINDOW);
1155 cls.lpszMenuName = NULL;
1156 cls.lpszClassName = szMsiDialogClass;
1157
1158 RegisterClassW( &cls );
1159 }
1160
1161 void msi_dialog_unregister_class( void )
1162 {
1163 UnregisterClassW( szMsiDialogClass, NULL );
1164 }
1165
1166 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1167 {
1168 WNDPROC oldproc = (WNDPROC) GetPropW(hWnd, szButtonData);
1169
1170 TRACE("hWnd %p msg %04x wParam 0x%08x lParam 0x%08lx\n", hWnd, msg, wParam, lParam);
1171
1172 if (msg == WM_COMMAND) /* Forward notifications to dialog */
1173 SendMessageW(GetParent(hWnd), msg, wParam, lParam);
1174
1175 return CallWindowProcW(oldproc, hWnd, msg, wParam, lParam);
1176 }