msi: Add a stub implementation of the VolumeCostList control.
[wine] / dlls / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #define COBJMACROS
22 #define NONAMELESSUNION
23 #define NONAMELESSSTRUCT
24
25 #include <stdarg.h>
26
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"
41
42 #include "wine/debug.h"
43 #include "wine/unicode.h"
44
45 #include "action.h"
46
47 WINE_DEFAULT_DEBUG_CHANNEL(msi);
48
49
50 extern HINSTANCE msi_hInstance;
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 list entry;
59     HWND hwnd;
60     msi_handler handler;
61     LPWSTR property;
62     LPWSTR value;
63     HBITMAP hBitmap;
64     HICON hIcon;
65     LPWSTR tabnext;
66     HMODULE hDll;
67     float progress_current;
68     float progress_max;
69     DWORD attributes;
70     WCHAR name[1];
71 };
72
73 typedef struct msi_font_tag
74 {
75     struct msi_font_tag *next;
76     HFONT hfont;
77     COLORREF color;
78     WCHAR name[1];
79 } msi_font;
80
81 struct msi_dialog_tag
82 {
83     MSIPACKAGE *package;
84     msi_dialog_event_handler event_handler;
85     BOOL finished;
86     INT scale;
87     DWORD attributes;
88     HWND hwnd;
89     LPWSTR default_font;
90     msi_font *font_list;
91     struct list controls;
92     HWND hWndFocus;
93     LPWSTR control_default;
94     LPWSTR control_cancel;
95     WCHAR name[1];
96 };
97
98 typedef UINT (*msi_dialog_control_func)( msi_dialog *dialog, MSIRECORD *rec );
99 struct control_handler 
100 {
101     LPCWSTR control_type;
102     msi_dialog_control_func func;
103 };
104
105 typedef struct
106 {
107     msi_dialog* dialog;
108     msi_control *parent;
109     DWORD       attributes;
110     LPWSTR      propval;
111 } radio_button_group_descr;
112
113 static const WCHAR szMsiDialogClass[] = {
114     'M','s','i','D','i','a','l','o','g','C','l','o','s','e','C','l','a','s','s',0
115 };
116 static const WCHAR szMsiHiddenWindow[] = {
117     'M','s','i','H','i','d','d','e','n','W','i','n','d','o','w',0 };
118 static const WCHAR szStatic[] = { 'S','t','a','t','i','c',0 };
119 static const WCHAR szButton[] = { 'B','U','T','T','O','N', 0 };
120 static const WCHAR szButtonData[] = { 'M','S','I','D','A','T','A',0 };
121 static const WCHAR szProgress[] = { 'P','r','o','g','r','e','s','s',0 };
122 static const WCHAR szText[] = { 'T','e','x','t',0 };
123 static const WCHAR szPushButton[] = { 'P','u','s','h','B','u','t','t','o','n',0 };
124 static const WCHAR szLine[] = { 'L','i','n','e',0 };
125 static const WCHAR szBitmap[] = { 'B','i','t','m','a','p',0 };
126 static const WCHAR szCheckBox[] = { 'C','h','e','c','k','B','o','x',0 };
127 static const WCHAR szScrollableText[] = {
128     'S','c','r','o','l','l','a','b','l','e','T','e','x','t',0 };
129 static const WCHAR szComboBox[] = { 'C','o','m','b','o','B','o','x',0 };
130 static const WCHAR szEdit[] = { 'E','d','i','t',0 };
131 static const WCHAR szMaskedEdit[] = { 'M','a','s','k','e','d','E','d','i','t',0 };
132 static const WCHAR szPathEdit[] = { 'P','a','t','h','E','d','i','t',0 };
133 static const WCHAR szProgressBar[] = {
134      'P','r','o','g','r','e','s','s','B','a','r',0 };
135 static const WCHAR szRadioButtonGroup[] = { 
136     'R','a','d','i','o','B','u','t','t','o','n','G','r','o','u','p',0 };
137 static const WCHAR szIcon[] = { 'I','c','o','n',0 };
138 static const WCHAR szSelectionTree[] = {
139     'S','e','l','e','c','t','i','o','n','T','r','e','e',0 };
140 static const WCHAR szGroupBox[] = { 'G','r','o','u','p','B','o','x',0 };
141 static const WCHAR szListBox[] = { 'L','i','s','t','B','o','x',0 };
142 static const WCHAR szDirectoryCombo[] = { 'D','i','r','e','c','t','o','r','y','C','o','m','b','o',0 };
143 static const WCHAR szDirectoryList[] = { 'D','i','r','e','c','t','o','r','y','L','i','s','t',0 };
144 static const WCHAR szVolumeCostList[] = { 'V','o','l','u','m','e','C','o','s','t','L','i','s','t',0 };
145
146 static UINT msi_dialog_checkbox_handler( msi_dialog *, msi_control *, WPARAM );
147 static void msi_dialog_checkbox_sync_state( msi_dialog *, msi_control * );
148 static UINT msi_dialog_button_handler( msi_dialog *, msi_control *, WPARAM );
149 static UINT msi_dialog_edit_handler( msi_dialog *, msi_control *, WPARAM );
150 static UINT msi_dialog_radiogroup_handler( msi_dialog *, msi_control *, WPARAM param );
151 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog );
152 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
153
154
155 /* dialog sequencing */
156
157 #define WM_MSI_DIALOG_CREATE  (WM_USER+0x100)
158 #define WM_MSI_DIALOG_DESTROY (WM_USER+0x101)
159
160 static DWORD uiThreadId;
161 static HWND hMsiHiddenWindow;
162
163 static INT msi_dialog_scale_unit( msi_dialog *dialog, INT val )
164 {
165     return (dialog->scale * val + 5) / 10;
166 }
167
168 static msi_control *msi_dialog_find_control( msi_dialog *dialog, LPCWSTR name )
169 {
170     msi_control *control;
171
172     if( !name )
173         return NULL;
174     LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
175         if( !strcmpW( control->name, name ) ) /* FIXME: case sensitive? */
176             return control;
177     return NULL;
178 }
179
180 static msi_control *msi_dialog_find_control_by_hwnd( msi_dialog *dialog, HWND hwnd )
181 {
182     msi_control *control;
183
184     LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
185         if( hwnd == control->hwnd )
186             return control;
187     return NULL;
188 }
189
190 static LPWSTR msi_get_deformatted_field( MSIPACKAGE *package, MSIRECORD *rec, int field )
191 {
192     LPCWSTR str = MSI_RecordGetString( rec, field );
193     LPWSTR ret = NULL;
194
195     if (str)
196         deformat_string( package, str, &ret );
197     return ret;
198 }
199
200 /*
201  * msi_dialog_get_style
202  *
203  * Extract the {\style} string from the front of the text to display and
204  *  update the pointer.
205  */
206 static LPWSTR msi_dialog_get_style( LPCWSTR p, LPCWSTR *rest )
207 {
208     LPWSTR ret = NULL;
209     LPCWSTR q, i;
210     DWORD len;
211
212     *rest = p;
213     if( !p )
214         return ret;
215     if( *p++ != '{' )
216         return ret;
217     q = strchrW( p, '}' );
218     if( !q )
219         return ret;
220     if( *p == '\\' || *p == '&' )
221         p++;
222
223     /* little bit of sanity checking to stop us getting confused with RTF */
224     for( i=p; i<q; i++ )
225         if( *i == '}' || *i == '\\' )
226             return ret;
227     
228     *rest = ++q;
229     len = q - p;
230
231     ret = msi_alloc( len*sizeof(WCHAR) );
232     if( !ret )
233         return ret;
234     memcpy( ret, p, len*sizeof(WCHAR) );
235     ret[len-1] = 0;
236     return ret;
237 }
238
239 static UINT msi_dialog_add_font( MSIRECORD *rec, LPVOID param )
240 {
241     msi_dialog *dialog = param;
242     msi_font *font;
243     LPCWSTR face, name;
244     LOGFONTW lf;
245     INT style;
246     HDC hdc;
247
248     /* create a font and add it to the list */
249     name = MSI_RecordGetString( rec, 1 );
250     font = msi_alloc( sizeof *font + strlenW( name )*sizeof (WCHAR) );
251     strcpyW( font->name, name );
252     font->next = dialog->font_list;
253     dialog->font_list = font;
254
255     font->color = MSI_RecordGetInteger( rec, 4 );
256
257     memset( &lf, 0, sizeof lf );
258     face = MSI_RecordGetString( rec, 2 );
259     lf.lfHeight = MSI_RecordGetInteger( rec, 3 );
260     style = MSI_RecordGetInteger( rec, 5 );
261     if( style & msidbTextStyleStyleBitsBold )
262         lf.lfWeight = FW_BOLD;
263     if( style & msidbTextStyleStyleBitsItalic )
264         lf.lfItalic = TRUE;
265     if( style & msidbTextStyleStyleBitsUnderline )
266         lf.lfUnderline = TRUE;
267     if( style & msidbTextStyleStyleBitsStrike )
268         lf.lfStrikeOut = TRUE;
269     lstrcpynW( lf.lfFaceName, face, LF_FACESIZE );
270
271     /* adjust the height */
272     hdc = GetDC( dialog->hwnd );
273     if (hdc)
274     {
275         lf.lfHeight = -MulDiv(lf.lfHeight, GetDeviceCaps(hdc, LOGPIXELSY), 72);
276         ReleaseDC( dialog->hwnd, hdc );
277     }
278
279     font->hfont = CreateFontIndirectW( &lf );
280
281     TRACE("Adding font style %s\n", debugstr_w(font->name) );
282
283     return ERROR_SUCCESS;
284 }
285
286 static msi_font *msi_dialog_find_font( msi_dialog *dialog, LPCWSTR name )
287 {
288     msi_font *font;
289
290     for( font = dialog->font_list; font; font = font->next )
291         if( !strcmpW( font->name, name ) )  /* FIXME: case sensitive? */
292             break;
293
294     return font;
295 }
296
297 static UINT msi_dialog_set_font( msi_dialog *dialog, HWND hwnd, LPCWSTR name )
298 {
299     msi_font *font;
300
301     font = msi_dialog_find_font( dialog, name );
302     if( font )
303         SendMessageW( hwnd, WM_SETFONT, (WPARAM) font->hfont, TRUE );
304     else
305         ERR("No font entry for %s\n", debugstr_w(name));
306     return ERROR_SUCCESS;
307 }
308
309 static UINT msi_dialog_build_font_list( msi_dialog *dialog )
310 {
311     static const WCHAR query[] = {
312       'S','E','L','E','C','T',' ','*',' ',
313       'F','R','O','M',' ','`','T','e','x','t','S','t','y','l','e','`',' ',0
314     };
315     UINT r;
316     MSIQUERY *view = NULL;
317
318     TRACE("dialog %p\n", dialog );
319
320     r = MSI_OpenQuery( dialog->package->db, &view, query );
321     if( r != ERROR_SUCCESS )
322         return r;
323
324     r = MSI_IterateRecords( view, NULL, msi_dialog_add_font, dialog );
325     msiobj_release( &view->hdr );
326
327     return r;
328 }
329
330 static msi_control *msi_dialog_create_window( msi_dialog *dialog,
331                 MSIRECORD *rec, DWORD exstyle, LPCWSTR szCls, LPCWSTR name, LPCWSTR text,
332                 DWORD style, HWND parent )
333 {
334     DWORD x, y, width, height;
335     LPWSTR font = NULL, title_font = NULL;
336     LPCWSTR title = NULL;
337     msi_control *control;
338
339     style |= WS_CHILD;
340
341     control = msi_alloc( sizeof *control + strlenW(name)*sizeof(WCHAR) );
342     strcpyW( control->name, name );
343     list_add_head( &dialog->controls, &control->entry );
344     control->handler = NULL;
345     control->property = NULL;
346     control->value = NULL;
347     control->hBitmap = NULL;
348     control->hIcon = NULL;
349     control->hDll = NULL;
350     control->tabnext = strdupW( MSI_RecordGetString( rec, 11) );
351     control->progress_current = 0;
352     control->progress_max = 100;
353
354     x = MSI_RecordGetInteger( rec, 4 );
355     y = MSI_RecordGetInteger( rec, 5 );
356     width = MSI_RecordGetInteger( rec, 6 );
357     height = MSI_RecordGetInteger( rec, 7 );
358
359     x = msi_dialog_scale_unit( dialog, x );
360     y = msi_dialog_scale_unit( dialog, y );
361     width = msi_dialog_scale_unit( dialog, width );
362     height = msi_dialog_scale_unit( dialog, height );
363
364     if( text )
365     {
366         deformat_string( dialog->package, text, &title_font );
367         font = msi_dialog_get_style( title_font, &title );
368     }
369
370     control->hwnd = CreateWindowExW( exstyle, szCls, title, style,
371                           x, y, width, height, parent, NULL, NULL, NULL );
372
373     TRACE("Dialog %s control %s hwnd %p\n",
374            debugstr_w(dialog->name), debugstr_w(text), control->hwnd );
375
376     msi_dialog_set_font( dialog, control->hwnd,
377                          font ? font : dialog->default_font );
378
379     msi_free( title_font );
380     msi_free( font );
381
382     return control;
383 }
384
385 static MSIRECORD *msi_get_binary_record( MSIDATABASE *db, LPCWSTR name )
386 {
387     static const WCHAR query[] = {
388         's','e','l','e','c','t',' ','*',' ',
389         'f','r','o','m',' ','B','i','n','a','r','y',' ',
390         'w','h','e','r','e',' ',
391             '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0
392     };
393
394     return MSI_QueryGetRecord( db, query, name );
395 }
396
397 static LPWSTR msi_create_tmp_path(void)
398 {
399     WCHAR tmp[MAX_PATH];
400     LPWSTR path = NULL;
401     static const WCHAR prefix[] = { 'm','s','i',0 };
402     DWORD len, r;
403
404     r = GetTempPathW( MAX_PATH, tmp );
405     if( !r )
406         return path;
407     len = lstrlenW( tmp ) + 20;
408     path = msi_alloc( len * sizeof (WCHAR) );
409     if( path )
410     {
411         r = GetTempFileNameW( tmp, prefix, 0, path );
412         if (!r)
413         {
414             msi_free( path );
415             path = NULL;
416         }
417     }
418     return path;
419 }
420
421
422 static HANDLE msi_load_image( MSIDATABASE *db, LPCWSTR name, UINT type,
423                               UINT cx, UINT cy, UINT flags )
424 {
425     MSIRECORD *rec = NULL;
426     HANDLE himage = NULL;
427     LPWSTR tmp;
428     UINT r;
429
430     TRACE("%p %s %u %u %08x\n", db, debugstr_w(name), cx, cy, flags);
431
432     tmp = msi_create_tmp_path();
433     if( !tmp )
434         return himage;
435
436     rec = msi_get_binary_record( db, name );
437     if( rec )
438     {
439         r = MSI_RecordStreamToFile( rec, 2, tmp );
440         if( r == ERROR_SUCCESS )
441         {
442             himage = LoadImageW( 0, tmp, type, cx, cy, flags );
443             DeleteFileW( tmp );
444         }
445         msiobj_release( &rec->hdr );
446     }
447
448     msi_free( tmp );
449     return himage;
450 }
451
452 static HICON msi_load_icon( MSIDATABASE *db, LPCWSTR text, UINT attributes )
453 {
454     DWORD cx = 0, cy = 0, flags;
455
456     flags = LR_LOADFROMFILE | LR_DEFAULTSIZE;
457     if( attributes & msidbControlAttributesFixedSize )
458     {
459         flags &= ~LR_DEFAULTSIZE;
460         if( attributes & msidbControlAttributesIconSize16 )
461         {
462             cx += 16;
463             cy += 16;
464         }
465         if( attributes & msidbControlAttributesIconSize32 )
466         {
467             cx += 32;
468             cy += 32;
469         }
470         /* msidbControlAttributesIconSize48 handled by above logic */
471     }
472     return msi_load_image( db, text, IMAGE_ICON, cx, cy, flags );
473 }
474
475
476 /* called from the Control Event subscription code */
477 void msi_dialog_handle_event( msi_dialog* dialog, LPCWSTR control, 
478                               LPCWSTR attribute, MSIRECORD *rec )
479 {
480     msi_control* ctrl;
481     LPCWSTR font_text, text = NULL;
482     LPWSTR font;
483
484     ctrl = msi_dialog_find_control( dialog, control );
485     if (!ctrl)
486         return;
487     if( !lstrcmpW(attribute, szText) )
488     {
489         font_text = MSI_RecordGetString( rec , 1 );
490         font = msi_dialog_get_style( font_text, &text );
491         SetWindowTextW( ctrl->hwnd, text );
492         msi_free( font );
493         msi_dialog_check_messages( NULL );
494     }
495     else if( !lstrcmpW(attribute, szProgress) )
496     {
497         DWORD func, val;
498
499         func = MSI_RecordGetInteger( rec , 1 );
500         val = MSI_RecordGetInteger( rec , 2 );
501
502         switch (func)
503         {
504         case 0: /* init */
505             ctrl->progress_max = val;
506             ctrl->progress_current = 0;
507             SendMessageW(ctrl->hwnd, PBM_SETRANGE, 0, MAKELPARAM(0,100));
508             SendMessageW(ctrl->hwnd, PBM_SETPOS, 0, 0);
509             break;
510         case 1: /* FIXME: not sure what this is supposed to do */
511             break;
512         case 2: /* move */
513             ctrl->progress_current += val;
514             SendMessageW(ctrl->hwnd, PBM_SETPOS, 100*(ctrl->progress_current/ctrl->progress_max), 0);
515             break;
516         default:
517             ERR("Unknown progress message %ld\n", func);
518             break;
519         }
520     }
521     else
522     {
523         FIXME("Attribute %s not being set\n", debugstr_w(attribute));
524         return;
525     }
526 }
527
528 static void msi_dialog_map_events(msi_dialog* dialog, LPCWSTR control)
529 {
530     static const WCHAR Query[] = {
531         'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
532          '`','E','v','e','n','t','M','a','p','p','i','n','g','`',' ',
533         'W','H','E','R','E',' ',
534          '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
535         'A','N','D',' ',
536          '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',0
537     };
538     MSIRECORD *row;
539     LPCWSTR event, attribute;
540
541     row = MSI_QueryGetRecord( dialog->package->db, Query, dialog->name, control );
542     if (!row)
543         return;
544
545     event = MSI_RecordGetString( row, 3 );
546     attribute = MSI_RecordGetString( row, 4 );
547     ControlEvent_SubscribeToEvent( dialog->package, event, control, attribute );
548     msiobj_release( &row->hdr );
549 }
550
551 /* everything except radio buttons */
552 static msi_control *msi_dialog_add_control( msi_dialog *dialog,
553                 MSIRECORD *rec, LPCWSTR szCls, DWORD style )
554 {
555     DWORD attributes;
556     LPCWSTR text, name;
557     DWORD exstyle = 0;
558
559     name = MSI_RecordGetString( rec, 2 );
560     attributes = MSI_RecordGetInteger( rec, 8 );
561     text = MSI_RecordGetString( rec, 10 );
562     if( attributes & msidbControlAttributesVisible )
563         style |= WS_VISIBLE;
564     if( ~attributes & msidbControlAttributesEnabled )
565         style |= WS_DISABLED;
566     if( attributes & msidbControlAttributesSunken )
567         exstyle |= WS_EX_CLIENTEDGE;
568
569     msi_dialog_map_events(dialog, name);
570
571     return msi_dialog_create_window( dialog, rec, exstyle, szCls, name,
572                                      text, style, dialog->hwnd );
573 }
574
575 struct msi_text_info
576 {
577     msi_font *font;
578     WNDPROC oldproc;
579     DWORD attributes;
580 };
581
582 /*
583  * we don't erase our own background,
584  * so we have to make sure that the parent window redraws first
585  */
586 static void msi_text_on_settext( HWND hWnd )
587 {
588     HWND hParent;
589     RECT rc;
590
591     hParent = GetParent( hWnd );
592     GetWindowRect( hWnd, &rc );
593     MapWindowPoints( NULL, hParent, (LPPOINT) &rc, 2 );
594     InvalidateRect( hParent, &rc, TRUE );
595 }
596
597 static LRESULT WINAPI
598 MSIText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
599 {
600     struct msi_text_info *info;
601     LRESULT r = 0;
602
603     TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
604
605     info = GetPropW(hWnd, szButtonData);
606
607     if ( info->font )
608         SetTextColor( (HDC)wParam, info->font->color );
609
610     if( msg == WM_CTLCOLORSTATIC &&
611        ( info->attributes & msidbControlAttributesTransparent ) )
612     {
613         SetBkMode( (HDC)wParam, TRANSPARENT );
614         return (LRESULT) GetStockObject(NULL_BRUSH);
615     }
616
617     r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
618
619     switch( msg )
620     {
621     case WM_SETTEXT:
622         msi_text_on_settext( hWnd );
623         break;
624     case WM_NCDESTROY:
625         msi_free( info );
626         RemovePropW( hWnd, szButtonData );
627         break;
628     }
629
630     return r;
631 }
632
633 static UINT msi_dialog_text_control( msi_dialog *dialog, MSIRECORD *rec )
634 {
635     msi_control *control;
636     struct msi_text_info *info;
637     LPCWSTR text, ptr;
638     LPWSTR font_name;
639
640     TRACE("%p %p\n", dialog, rec);
641
642     control = msi_dialog_add_control( dialog, rec, szStatic, SS_LEFT | WS_GROUP );
643     if( !control )
644         return ERROR_FUNCTION_FAILED;
645
646     info = msi_alloc( sizeof *info );
647     if( !info )
648         return ERROR_SUCCESS;
649
650     text = MSI_RecordGetString( rec, 10 );
651     font_name = msi_dialog_get_style( text, &ptr );
652     info->font = ( font_name ) ? msi_dialog_find_font( dialog, font_name ) : NULL;
653     msi_free( font_name );
654
655     info->attributes = MSI_RecordGetInteger( rec, 8 );
656     if( info->attributes & msidbControlAttributesTransparent )
657         SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT );
658
659     info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
660                                           (LONG_PTR)MSIText_WndProc );
661     SetPropW( control->hwnd, szButtonData, info );
662
663     return ERROR_SUCCESS;
664 }
665
666 static UINT msi_dialog_button_control( msi_dialog *dialog, MSIRECORD *rec )
667 {
668     msi_control *control;
669     UINT attributes, style;
670     LPWSTR text;
671
672     TRACE("%p %p\n", dialog, rec);
673
674     style = WS_TABSTOP;
675     attributes = MSI_RecordGetInteger( rec, 8 );
676     if( attributes & msidbControlAttributesIcon )
677         style |= BS_ICON;
678
679     control = msi_dialog_add_control( dialog, rec, szButton, style );
680     if( !control )
681         return ERROR_FUNCTION_FAILED;
682
683     control->handler = msi_dialog_button_handler;
684
685     /* set the icon */
686     text = msi_get_deformatted_field( dialog->package, rec, 10 );
687     control->hIcon = msi_load_icon( dialog->package->db, text, attributes );
688     if( attributes & msidbControlAttributesIcon )
689         SendMessageW( control->hwnd, BM_SETIMAGE, IMAGE_ICON, (LPARAM) control->hIcon );
690     msi_free( text );
691
692     return ERROR_SUCCESS;
693 }
694
695 static LPWSTR msi_get_checkbox_value( msi_dialog *dialog, LPCWSTR prop )
696 {
697     static const WCHAR query[] = {
698         'S','E','L','E','C','T',' ','*',' ',
699         'F','R','O','M',' ','`','C','h','e','c','k','B','o','x',' ','`',
700         'W','H','E','R','E',' ',
701         '`','P','r','o','p','e','r','t','y','`',' ','=',' ',
702         '\'','%','s','\'',0
703     };
704     MSIRECORD *rec = NULL;
705     LPWSTR ret = NULL;
706
707     /* find if there is a value associated with the checkbox */
708     rec = MSI_QueryGetRecord( dialog->package->db, query, prop );
709     if (!rec)
710         return ret;
711
712     ret = msi_get_deformatted_field( dialog->package, rec, 2 );
713     if( ret && !ret[0] )
714     {
715         msi_free( ret );
716         ret = NULL;
717     }
718     msiobj_release( &rec->hdr );
719     if (ret)
720         return ret;
721
722     ret = msi_dup_property( dialog->package, prop );
723     if( ret && !ret[0] )
724     {
725         msi_free( ret );
726         ret = NULL;
727     }
728
729     return ret;
730 }
731
732 static UINT msi_dialog_checkbox_control( msi_dialog *dialog, MSIRECORD *rec )
733 {
734     msi_control *control;
735     LPCWSTR prop;
736
737     TRACE("%p %p\n", dialog, rec);
738
739     control = msi_dialog_add_control( dialog, rec, szButton,
740                                 BS_CHECKBOX | BS_MULTILINE | WS_TABSTOP );
741     control->handler = msi_dialog_checkbox_handler;
742     prop = MSI_RecordGetString( rec, 9 );
743     if( prop )
744     {
745         control->property = strdupW( prop );
746         control->value = msi_get_checkbox_value( dialog, prop );
747         TRACE("control %s value %s\n", debugstr_w(control->property),
748               debugstr_w(control->value));
749     }
750     msi_dialog_checkbox_sync_state( dialog, control );
751
752     return ERROR_SUCCESS;
753 }
754
755 static UINT msi_dialog_line_control( msi_dialog *dialog, MSIRECORD *rec )
756 {
757     TRACE("%p %p\n", dialog, rec);
758
759     /* line is exactly 2 units in height */
760     MSI_RecordSetInteger( rec, 7, 2 );
761
762     msi_dialog_add_control( dialog, rec, szStatic, SS_ETCHEDHORZ | SS_SUNKEN );
763     return ERROR_SUCCESS;
764 }
765
766 /******************** Scroll Text ********************************************/
767
768 struct msi_scrolltext_info
769 {
770     msi_dialog *dialog;
771     msi_control *control;
772     WNDPROC oldproc;
773 };
774
775 static LRESULT WINAPI
776 MSIScrollText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
777 {
778     struct msi_scrolltext_info *info;
779     HRESULT r;
780
781     TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
782
783     info = GetPropW( hWnd, szButtonData );
784
785     r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
786
787     switch( msg )
788     {
789     case WM_NCDESTROY:
790         msi_free( info );
791         RemovePropW( hWnd, szButtonData );
792         break;
793     case WM_PAINT:
794         /* native MSI sets a wait cursor here */
795         msi_dialog_button_handler( info->dialog, info->control, BN_CLICKED );
796         break;
797     }
798     return r;
799 }
800
801 struct msi_streamin_info
802 {
803     LPSTR string;
804     DWORD offset;
805     DWORD length;
806 };
807
808 static DWORD CALLBACK
809 msi_richedit_stream_in( DWORD_PTR arg, LPBYTE buffer, LONG count, LONG *pcb )
810 {
811     struct msi_streamin_info *info = (struct msi_streamin_info*) arg;
812
813     if( (count + info->offset) > info->length )
814         count = info->length - info->offset;
815     memcpy( buffer, &info->string[ info->offset ], count );
816     *pcb = count;
817     info->offset += count;
818
819     TRACE("%ld/%ld\n", info->offset, info->length);
820
821     return 0;
822 }
823
824 static void msi_scrolltext_add_text( msi_control *control, LPCWSTR text )
825 {
826     struct msi_streamin_info info;
827     EDITSTREAM es;
828
829     info.string = strdupWtoA( text );
830     info.offset = 0;
831     info.length = lstrlenA( info.string ) + 1;
832
833     es.dwCookie = (DWORD_PTR) &info;
834     es.dwError = 0;
835     es.pfnCallback = msi_richedit_stream_in;
836
837     SendMessageW( control->hwnd, EM_STREAMIN, SF_RTF, (LPARAM) &es );
838
839     msi_free( info.string );
840 }
841
842 static UINT msi_dialog_scrolltext_control( msi_dialog *dialog, MSIRECORD *rec )
843 {
844     static const WCHAR szRichEdit20W[] = {
845         'R','i','c','h','E','d','i','t','2','0','W',0
846     };
847     struct msi_scrolltext_info *info;
848     msi_control *control;
849     HMODULE hRichedit;
850     DWORD style;
851
852     info = msi_alloc( sizeof *info );
853     if (!info)
854         return ERROR_FUNCTION_FAILED;
855
856     hRichedit = LoadLibraryA("riched20");
857
858     style = WS_BORDER | ES_MULTILINE | WS_VSCROLL |
859             ES_READONLY | ES_AUTOVSCROLL | WS_TABSTOP;
860     control = msi_dialog_add_control( dialog, rec, szRichEdit20W, style );
861     if (!control)
862     {
863         FreeLibrary( hRichedit );
864         msi_free( info );
865         return ERROR_FUNCTION_FAILED;
866     }
867
868     control->hDll = hRichedit;
869
870     info->dialog = dialog;
871     info->control = control;
872
873     /* subclass the static control */
874     info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
875                                           (LONG_PTR)MSIScrollText_WndProc );
876     SetPropW( control->hwnd, szButtonData, info );
877
878     /* add the text into the richedit */
879     msi_scrolltext_add_text( control, MSI_RecordGetString( rec, 10 ) );
880
881     return ERROR_SUCCESS;
882 }
883
884 static HBITMAP msi_load_picture( MSIDATABASE *db, LPCWSTR name,
885                                  INT cx, INT cy, DWORD flags )
886 {
887     HBITMAP hOleBitmap = 0, hBitmap = 0, hOldSrcBitmap, hOldDestBitmap;
888     MSIRECORD *rec = NULL;
889     IStream *stm = NULL;
890     IPicture *pic = NULL;
891     HDC srcdc, destdc;
892     BITMAP bm;
893     UINT r;
894
895     rec = msi_get_binary_record( db, name );
896     if( !rec )
897         goto end;
898
899     r = MSI_RecordGetIStream( rec, 2, &stm );
900     msiobj_release( &rec->hdr );
901     if( r != ERROR_SUCCESS )
902         goto end;
903
904     r = OleLoadPicture( stm, 0, TRUE, &IID_IPicture, (LPVOID*) &pic );
905     IStream_Release( stm );
906     if( FAILED( r ) )
907     {
908         ERR("failed to load picture\n");
909         goto end;
910     }
911
912     r = IPicture_get_Handle( pic, (OLE_HANDLE*) &hOleBitmap );
913     if( FAILED( r ) )
914     {
915         ERR("failed to get bitmap handle\n");
916         goto end;
917     }
918  
919     /* make the bitmap the desired size */
920     r = GetObjectW( hOleBitmap, sizeof bm, &bm );
921     if (r != sizeof bm )
922     {
923         ERR("failed to get bitmap size\n");
924         goto end;
925     }
926
927     if (flags & LR_DEFAULTSIZE)
928     {
929         cx = bm.bmWidth;
930         cy = bm.bmHeight;
931     }
932
933     srcdc = CreateCompatibleDC( NULL );
934     hOldSrcBitmap = SelectObject( srcdc, hOleBitmap );
935     destdc = CreateCompatibleDC( NULL );
936     hBitmap = CreateCompatibleBitmap( srcdc, cx, cy );
937     hOldDestBitmap = SelectObject( destdc, hBitmap );
938     StretchBlt( destdc, 0, 0, cx, cy,
939                 srcdc, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
940     SelectObject( srcdc, hOldSrcBitmap );
941     SelectObject( destdc, hOldDestBitmap );
942     DeleteDC( srcdc );
943     DeleteDC( destdc );
944
945 end:
946     if ( pic )
947         IPicture_Release( pic );
948     return hBitmap;
949 }
950
951 static UINT msi_dialog_bitmap_control( msi_dialog *dialog, MSIRECORD *rec )
952 {
953     UINT cx, cy, flags, style, attributes;
954     msi_control *control;
955     LPWSTR text;
956
957     flags = LR_LOADFROMFILE;
958     style = SS_BITMAP | SS_LEFT | WS_GROUP;
959
960     attributes = MSI_RecordGetInteger( rec, 8 );
961     if( attributes & msidbControlAttributesFixedSize )
962     {
963         flags |= LR_DEFAULTSIZE;
964         style |= SS_CENTERIMAGE;
965     }
966
967     control = msi_dialog_add_control( dialog, rec, szStatic, style );
968     cx = MSI_RecordGetInteger( rec, 6 );
969     cy = MSI_RecordGetInteger( rec, 7 );
970     cx = msi_dialog_scale_unit( dialog, cx );
971     cy = msi_dialog_scale_unit( dialog, cy );
972
973     text = msi_get_deformatted_field( dialog->package, rec, 10 );
974     control->hBitmap = msi_load_picture( dialog->package->db, text, cx, cy, flags );
975     if( control->hBitmap )
976         SendMessageW( control->hwnd, STM_SETIMAGE,
977                       IMAGE_BITMAP, (LPARAM) control->hBitmap );
978     else
979         ERR("Failed to load bitmap %s\n", debugstr_w(text));
980
981     msi_free( text );
982     
983     return ERROR_SUCCESS;
984 }
985
986 static UINT msi_dialog_icon_control( msi_dialog *dialog, MSIRECORD *rec )
987 {
988     msi_control *control;
989     DWORD attributes;
990     LPWSTR text;
991
992     TRACE("\n");
993
994     control = msi_dialog_add_control( dialog, rec, szStatic,
995                             SS_ICON | SS_CENTERIMAGE | WS_GROUP );
996             
997     attributes = MSI_RecordGetInteger( rec, 8 );
998     text = msi_get_deformatted_field( dialog->package, rec, 10 );
999     control->hIcon = msi_load_icon( dialog->package->db, text, attributes );
1000     if( control->hIcon )
1001         SendMessageW( control->hwnd, STM_SETICON, (WPARAM) control->hIcon, 0 );
1002     else
1003         ERR("Failed to load bitmap %s\n", debugstr_w(text));
1004     msi_free( text );
1005     return ERROR_SUCCESS;
1006 }
1007
1008 static UINT msi_dialog_combo_control( msi_dialog *dialog, MSIRECORD *rec )
1009 {
1010     static const WCHAR szCombo[] = { 'C','O','M','B','O','B','O','X',0 };
1011
1012     msi_dialog_add_control( dialog, rec, szCombo,
1013                             SS_BITMAP | SS_LEFT | SS_CENTERIMAGE );
1014     return ERROR_SUCCESS;
1015 }
1016
1017 static UINT msi_dialog_edit_control( msi_dialog *dialog, MSIRECORD *rec )
1018 {
1019     msi_control *control;
1020     LPCWSTR prop;
1021     LPWSTR val;
1022
1023     control = msi_dialog_add_control( dialog, rec, szEdit,
1024                                       WS_BORDER | WS_TABSTOP );
1025     control->handler = msi_dialog_edit_handler;
1026     prop = MSI_RecordGetString( rec, 9 );
1027     if( prop )
1028         control->property = strdupW( prop );
1029     val = msi_dup_property( dialog->package, control->property );
1030     SetWindowTextW( control->hwnd, val );
1031     msi_free( val );
1032     return ERROR_SUCCESS;
1033 }
1034
1035 /******************** Masked Edit ********************************************/
1036
1037 #define MASK_MAX_GROUPS 10
1038
1039 struct msi_mask_group
1040 {
1041     UINT len;
1042     UINT ofs;
1043     WCHAR type;
1044     HWND hwnd;
1045 };
1046
1047 struct msi_maskedit_info
1048 {
1049     msi_dialog *dialog;
1050     WNDPROC oldproc;
1051     HWND hwnd;
1052     LPWSTR prop;
1053     UINT num_chars;
1054     UINT num_groups;
1055     struct msi_mask_group group[MASK_MAX_GROUPS];
1056 };
1057
1058 static BOOL msi_mask_editable( WCHAR type )
1059 {
1060     switch (type)
1061     {
1062     case '%':
1063     case '#':
1064     case '&':
1065     case '`':
1066     case '?':
1067     case '^':
1068         return TRUE;
1069     }
1070     return FALSE;
1071 }
1072
1073 static void msi_mask_control_change( struct msi_maskedit_info *info )
1074 {
1075     LPWSTR val;
1076     UINT i, n, r;
1077
1078     val = msi_alloc( (info->num_chars+1)*sizeof(WCHAR) );
1079     for( i=0, n=0; i<info->num_groups; i++ )
1080     {
1081         if( (info->group[i].len + n) > info->num_chars )
1082         {
1083             ERR("can't fit control %d text into template\n",i);
1084             break;
1085         }
1086         if (!msi_mask_editable(info->group[i].type))
1087         {
1088             for(r=0; r<info->group[i].len; r++)
1089                 val[n+r] = info->group[i].type;
1090             val[n+r] = 0;
1091         }
1092         else
1093         {
1094             r = GetWindowTextW( info->group[i].hwnd, &val[n], info->group[i].len+1 );
1095             if( r != info->group[i].len )
1096                 break;
1097         }
1098         n += r;
1099     }
1100
1101     TRACE("%d/%d controls were good\n", i, info->num_groups);
1102
1103     if( i == info->num_groups )
1104     {
1105         TRACE("Set property %s to %s\n",
1106               debugstr_w(info->prop), debugstr_w(val) );
1107         CharUpperBuffW( val, info->num_chars );
1108         MSI_SetPropertyW( info->dialog->package, info->prop, val );
1109         msi_dialog_evaluate_control_conditions( info->dialog );
1110     }
1111     msi_free( val );
1112 }
1113
1114 /* now move to the next control if necessary */
1115 static VOID msi_mask_next_control( struct msi_maskedit_info *info, HWND hWnd )
1116 {
1117     HWND hWndNext;
1118     UINT len, i;
1119
1120     for( i=0; i<info->num_groups; i++ )
1121         if( info->group[i].hwnd == hWnd )
1122             break;
1123
1124     /* don't move from the last control */
1125     if( i >= (info->num_groups-1) )
1126         return;
1127
1128     len = SendMessageW( hWnd, WM_GETTEXTLENGTH, 0, 0 );
1129     if( len < info->group[i].len )
1130         return;
1131
1132     hWndNext = GetNextDlgTabItem( GetParent( hWnd ), hWnd, FALSE );
1133     SetFocus( hWndNext );
1134 }
1135
1136 static LRESULT WINAPI
1137 MSIMaskedEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1138 {
1139     struct msi_maskedit_info *info;
1140     HRESULT r;
1141
1142     TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
1143
1144     info = GetPropW(hWnd, szButtonData);
1145
1146     r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1147
1148     switch( msg )
1149     {
1150     case WM_COMMAND:
1151         if (HIWORD(wParam) == EN_CHANGE)
1152         {
1153             msi_mask_control_change( info );
1154             msi_mask_next_control( info, (HWND) lParam );
1155         }
1156         break;
1157     case WM_NCDESTROY:
1158         msi_free( info->prop );
1159         msi_free( info );
1160         RemovePropW( hWnd, szButtonData );
1161         break;
1162     }
1163
1164     return r;
1165 }
1166
1167 /* fish the various bits of the property out and put them in the control */
1168 static void
1169 msi_maskedit_set_text( struct msi_maskedit_info *info, LPCWSTR text )
1170 {
1171     LPCWSTR p;
1172     UINT i;
1173
1174     p = text;
1175     for( i = 0; i < info->num_groups; i++ )
1176     {
1177         if( info->group[i].len < lstrlenW( p ) )
1178         {
1179             LPWSTR chunk = strdupW( p );
1180             chunk[ info->group[i].len ] = 0;
1181             SetWindowTextW( info->group[i].hwnd, chunk );
1182             msi_free( chunk );
1183         }
1184         else
1185         {
1186             SetWindowTextW( info->group[i].hwnd, p );
1187             break;
1188         }
1189         p += info->group[i].len;
1190     }
1191 }
1192
1193 static struct msi_maskedit_info * msi_dialog_parse_groups( LPCWSTR mask )
1194 {
1195     struct msi_maskedit_info * info = NULL;
1196     int i = 0, n = 0, total = 0;
1197     LPCWSTR p;
1198
1199     TRACE("masked control, template %s\n", debugstr_w(mask));
1200
1201     if( !mask )
1202         return info;
1203
1204     info = msi_alloc_zero( sizeof *info );
1205     if( !info )
1206         return info;
1207
1208     p = strchrW(mask, '<');
1209     if( p )
1210         p++;
1211     else
1212         p = mask;
1213
1214     for( i=0; i<MASK_MAX_GROUPS; i++ )
1215     {
1216         /* stop at the end of the string */
1217         if( p[0] == 0 || p[0] == '>' )
1218             break;
1219
1220         /* count the number of the same identifier */
1221         for( n=0; p[n] == p[0]; n++ )
1222             ;
1223         info->group[i].ofs = total;
1224         info->group[i].type = p[0];
1225         if( p[n] == '=' )
1226         {
1227             n++;
1228             total++; /* an extra not part of the group */
1229         }
1230         info->group[i].len = n;
1231         total += n;
1232         p += n;
1233     }
1234
1235     TRACE("%d characters in %d groups\n", total, i );
1236     if( i == MASK_MAX_GROUPS )
1237         ERR("too many groups in PIDTemplate %s\n", debugstr_w(mask));
1238
1239     info->num_chars = total;
1240     info->num_groups = i;
1241
1242     return info;
1243 }
1244
1245 static void
1246 msi_maskedit_create_children( struct msi_maskedit_info *info, LPCWSTR font )
1247 {
1248     DWORD width, height, style, wx, ww;
1249     RECT rect;
1250     HWND hwnd;
1251     UINT i;
1252
1253     style = WS_CHILD | WS_BORDER | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL;
1254
1255     GetClientRect( info->hwnd, &rect );
1256
1257     width = rect.right - rect.left;
1258     height = rect.bottom - rect.top;
1259
1260     for( i = 0; i < info->num_groups; i++ )
1261     {
1262         if (!msi_mask_editable( info->group[i].type ))
1263             continue;
1264         wx = (info->group[i].ofs * width) / info->num_chars;
1265         ww = (info->group[i].len * width) / info->num_chars;
1266
1267         hwnd = CreateWindowW( szEdit, NULL, style, wx, 0, ww, height,
1268                               info->hwnd, NULL, NULL, NULL );
1269         if( !hwnd )
1270         {
1271             ERR("failed to create mask edit sub window\n");
1272             break;
1273         }
1274
1275         SendMessageW( hwnd, EM_LIMITTEXT, info->group[i].len, 0 );
1276
1277         msi_dialog_set_font( info->dialog, hwnd,
1278                              font?font:info->dialog->default_font );
1279         info->group[i].hwnd = hwnd;
1280     }
1281 }
1282
1283 /*
1284  * office 2003 uses "73931<````=````=````=````=`````>@@@@@"
1285  * delphi 7 uses "<????-??????-??????-????>" and "<???-???>"
1286  * filemaker pro 7 uses "<^^^^=^^^^=^^^^=^^^^=^^^^=^^^^=^^^^^>"
1287  */
1288 static UINT msi_dialog_maskedit_control( msi_dialog *dialog, MSIRECORD *rec )
1289 {
1290     LPWSTR font_mask, val = NULL, font;
1291     struct msi_maskedit_info *info = NULL;
1292     UINT ret = ERROR_SUCCESS;
1293     msi_control *control;
1294     LPCWSTR prop, mask;
1295
1296     TRACE("\n");
1297
1298     font_mask = msi_get_deformatted_field( dialog->package, rec, 10 );
1299     font = msi_dialog_get_style( font_mask, &mask );
1300     if( !mask )
1301     {
1302         ERR("mask template is empty\n");
1303         goto end;
1304     }
1305
1306     info = msi_dialog_parse_groups( mask );
1307     if( !info )
1308     {
1309         ERR("template %s is invalid\n", debugstr_w(mask));
1310         goto end;
1311     }
1312
1313     info->dialog = dialog;
1314
1315     control = msi_dialog_add_control( dialog, rec, szStatic,
1316                    SS_OWNERDRAW | WS_GROUP | WS_VISIBLE );
1317     if( !control )
1318     {
1319         ERR("Failed to create maskedit container\n");
1320         ret = ERROR_FUNCTION_FAILED;
1321         goto end;
1322     }
1323     SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
1324
1325     info->hwnd = control->hwnd;
1326
1327     /* subclass the static control */
1328     info->oldproc = (WNDPROC) SetWindowLongPtrW( info->hwnd, GWLP_WNDPROC,
1329                                           (LONG_PTR)MSIMaskedEdit_WndProc );
1330     SetPropW( control->hwnd, szButtonData, info );
1331
1332     prop = MSI_RecordGetString( rec, 9 );
1333     if( prop )
1334         info->prop = strdupW( prop );
1335
1336     msi_maskedit_create_children( info, font );
1337
1338     if( prop )
1339     {
1340         val = msi_dup_property( dialog->package, prop );
1341         if( val )
1342         {
1343             msi_maskedit_set_text( info, val );
1344             msi_free( val );
1345         }
1346     }
1347
1348 end:
1349     if( ret != ERROR_SUCCESS )
1350         msi_free( info );
1351     msi_free( font_mask );
1352     msi_free( font );
1353     return ret;
1354 }
1355
1356 /******************** Progress Bar *****************************************/
1357
1358 static UINT msi_dialog_progress_bar( msi_dialog *dialog, MSIRECORD *rec )
1359 {
1360     msi_dialog_add_control( dialog, rec, PROGRESS_CLASSW, WS_VISIBLE );
1361     return ERROR_SUCCESS;
1362 }
1363
1364 /******************** Path Edit ********************************************/
1365
1366 static LPWSTR msi_get_window_text( HWND hwnd )
1367 {
1368     UINT sz, r;
1369     LPWSTR buf;
1370
1371     sz = 0x20;
1372     buf = msi_alloc( sz*sizeof(WCHAR) );
1373     while ( buf )
1374     {
1375         r = GetWindowTextW( hwnd, buf, sz );
1376         if ( r < (sz - 1) )
1377             break;
1378         sz *= 2;
1379         buf = msi_realloc( buf, sz*sizeof(WCHAR) );
1380     }
1381
1382     return buf;
1383 }
1384
1385 static UINT msi_dialog_pathedit_handler( msi_dialog *dialog,
1386                 msi_control *control, WPARAM param )
1387 {
1388     LPCWSTR prop;
1389     LPWSTR buf, indirect = NULL;
1390
1391     if( HIWORD(param) != EN_KILLFOCUS )
1392         return ERROR_SUCCESS;
1393
1394     prop = control->property;
1395     if ( control->attributes & msidbControlAttributesIndirect )
1396     {
1397         indirect = msi_dup_property( dialog->package, control->property );
1398         prop = indirect;
1399     }
1400
1401     /* FIXME: verify the new path */
1402     buf = msi_get_window_text( control->hwnd );
1403     MSI_SetPropertyW( dialog->package, prop, buf );
1404
1405     TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
1406           debugstr_w(prop));
1407
1408     msi_free( buf );
1409     msi_free( indirect );
1410
1411     return ERROR_SUCCESS;
1412 }
1413
1414 static UINT msi_dialog_pathedit_control( msi_dialog *dialog, MSIRECORD *rec )
1415 {
1416     msi_control *control;
1417     LPCWSTR prop;
1418     LPWSTR val, indirect = NULL;
1419
1420     control = msi_dialog_add_control( dialog, rec, szEdit,
1421                                       WS_BORDER | WS_TABSTOP );
1422     control->handler = msi_dialog_pathedit_handler;
1423     control->attributes = MSI_RecordGetInteger( rec, 8 );
1424
1425     prop = MSI_RecordGetString( rec, 9 );
1426     if ( prop )
1427         control->property = strdupW( prop );
1428
1429     if ( control->attributes & msidbControlAttributesIndirect )
1430     {
1431         indirect = msi_dup_property( dialog->package, control->property );
1432         prop = indirect;
1433     }
1434
1435     val = msi_dup_property( dialog->package, prop );
1436     SetWindowTextW( control->hwnd, val );
1437     msi_free( val );
1438     msi_free( indirect );
1439
1440     return ERROR_SUCCESS;
1441 }
1442
1443 /* radio buttons are a bit different from normal controls */
1444 static UINT msi_dialog_create_radiobutton( MSIRECORD *rec, LPVOID param )
1445 {
1446     radio_button_group_descr *group = (radio_button_group_descr *)param;
1447     msi_dialog *dialog = group->dialog;
1448     msi_control *control;
1449     LPCWSTR prop, text, name;
1450     DWORD style, attributes = group->attributes;
1451
1452     style = WS_CHILD | BS_AUTORADIOBUTTON | BS_MULTILINE | WS_TABSTOP;
1453     name = MSI_RecordGetString( rec, 3 );
1454     text = MSI_RecordGetString( rec, 8 );
1455     if( attributes & 1 )
1456         style |= WS_VISIBLE;
1457     if( ~attributes & 2 )
1458         style |= WS_DISABLED;
1459
1460     control = msi_dialog_create_window( dialog, rec, 0, szButton, name, text,
1461                                         style, group->parent->hwnd );
1462     if (!control)
1463         return ERROR_FUNCTION_FAILED;
1464     control->handler = msi_dialog_radiogroup_handler;
1465
1466     if (!lstrcmpW(control->name, group->propval))
1467         SendMessageW(control->hwnd, BM_SETCHECK, BST_CHECKED, 0);
1468
1469     prop = MSI_RecordGetString( rec, 1 );
1470     if( prop )
1471         control->property = strdupW( prop );
1472
1473     return ERROR_SUCCESS;
1474 }
1475
1476 static UINT msi_dialog_radiogroup_control( msi_dialog *dialog, MSIRECORD *rec )
1477 {
1478     static const WCHAR query[] = {
1479         'S','E','L','E','C','T',' ','*',' ',
1480         'F','R','O','M',' ','R','a','d','i','o','B','u','t','t','o','n',' ',
1481         'W','H','E','R','E',' ',
1482            '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
1483     UINT r;
1484     LPCWSTR prop;
1485     msi_control *control;
1486     MSIQUERY *view = NULL;
1487     radio_button_group_descr group;
1488     MSIPACKAGE *package = dialog->package;
1489     WNDPROC oldproc;
1490
1491     prop = MSI_RecordGetString( rec, 9 );
1492
1493     TRACE("%p %p %s\n", dialog, rec, debugstr_w( prop ));
1494
1495     /* Create parent group box to hold radio buttons */
1496     control = msi_dialog_add_control( dialog, rec, szButton, BS_OWNERDRAW|WS_GROUP );
1497     if( !control )
1498         return ERROR_FUNCTION_FAILED;
1499
1500     oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1501                                            (LONG_PTR)MSIRadioGroup_WndProc );
1502     SetPropW(control->hwnd, szButtonData, oldproc);
1503     SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
1504
1505     if( prop )
1506         control->property = strdupW( prop );
1507
1508     /* query the Radio Button table for all control in this group */
1509     r = MSI_OpenQuery( package->db, &view, query, prop );
1510     if( r != ERROR_SUCCESS )
1511     {
1512         ERR("query failed for dialog %s radio group %s\n", 
1513             debugstr_w(dialog->name), debugstr_w(prop));
1514         return ERROR_INVALID_PARAMETER;
1515     }
1516
1517     group.dialog = dialog;
1518     group.parent = control;
1519     group.attributes = MSI_RecordGetInteger( rec, 8 );
1520     group.propval = msi_dup_property( dialog->package, control->property );
1521
1522     r = MSI_IterateRecords( view, 0, msi_dialog_create_radiobutton, &group );
1523     msiobj_release( &view->hdr );
1524     msi_free( group.propval );
1525
1526     return r;
1527 }
1528
1529 /******************** Selection Tree ***************************************/
1530
1531 struct msi_selection_tree_info
1532 {
1533     msi_dialog *dialog;
1534     HWND hwnd;
1535     WNDPROC oldproc;
1536 };
1537
1538 static void
1539 msi_seltree_sync_item_state( HWND hwnd, MSIFEATURE *feature, HTREEITEM hItem )
1540 {
1541     TVITEMW tvi;
1542
1543     TRACE("Feature %s -> %d %d %d\n", debugstr_w(feature->Title),
1544         feature->Installed, feature->Action, feature->ActionRequest);
1545
1546     tvi.mask = TVIF_STATE;
1547     tvi.hItem = hItem;
1548     tvi.state = INDEXTOSTATEIMAGEMASK( feature->Action );
1549     tvi.stateMask = TVIS_STATEIMAGEMASK;
1550
1551     SendMessageW( hwnd, TVM_SETITEMW, 0, (LPARAM) &tvi );
1552 }
1553
1554 static UINT
1555 msi_seltree_popup_menu( HWND hwnd, INT x, INT y )
1556 {
1557     HMENU hMenu;
1558     INT r;
1559
1560     /* create a menu to display */
1561     hMenu = CreatePopupMenu();
1562
1563     /* FIXME: load strings from resources */
1564     AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_LOCAL, "Install feature locally");
1565     AppendMenuA( hMenu, MF_GRAYED, 0x1000, "Install entire feature");
1566     AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ADVERTISED, "Install on demand");
1567     AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ABSENT, "Don't install");
1568     r = TrackPopupMenu( hMenu, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RETURNCMD,
1569                         x, y, 0, hwnd, NULL );
1570     DestroyMenu( hMenu );
1571     return r;
1572 }
1573
1574 static MSIFEATURE *
1575 msi_seltree_feature_from_item( HWND hwnd, HTREEITEM hItem )
1576 {
1577     TVITEMW tvi;
1578
1579     /* get the feature from the item */
1580     memset( &tvi, 0, sizeof tvi );
1581     tvi.hItem = hItem;
1582     tvi.mask = TVIF_PARAM | TVIF_HANDLE;
1583     SendMessageW( hwnd, TVM_GETITEMW, 0, (LPARAM) &tvi );
1584
1585     return (MSIFEATURE*) tvi.lParam;
1586 }
1587
1588 static LRESULT
1589 msi_seltree_menu( HWND hwnd, HTREEITEM hItem )
1590 {
1591     MSIFEATURE *feature;
1592     ComponentList *cl;
1593     union {
1594         RECT rc;
1595         POINT pt[2];
1596         HTREEITEM hItem;
1597     } u;
1598     UINT r;
1599
1600     feature = msi_seltree_feature_from_item( hwnd, hItem );
1601     if (!feature)
1602     {
1603         ERR("item %p feature was NULL\n", hItem);
1604         return 0;
1605     }
1606
1607     /* get the item's rectangle to put the menu just below it */
1608     u.hItem = hItem;
1609     SendMessageW( hwnd, TVM_GETITEMRECT, 0, (LPARAM) &u.rc );
1610     MapWindowPoints( hwnd, NULL, u.pt, 2 );
1611
1612     r = msi_seltree_popup_menu( hwnd, u.rc.left, u.rc.top );
1613
1614     switch (r)
1615     {
1616     case INSTALLSTATE_LOCAL:
1617     case INSTALLSTATE_ADVERTISED:
1618     case INSTALLSTATE_ABSENT:
1619         feature->ActionRequest = r;
1620         feature->Action = r;
1621         break;
1622     default:
1623         FIXME("select feature and all children\n");
1624     }
1625
1626     /* update */
1627     msi_seltree_sync_item_state( hwnd, feature, hItem );
1628
1629     /* update the feature's components */
1630     LIST_FOR_EACH_ENTRY( cl, &feature->Components, ComponentList, entry )
1631     {
1632         cl->component->Action = feature->Action;
1633         cl->component->ActionRequest = feature->ActionRequest;
1634     }
1635
1636     return 0;
1637 }
1638
1639 static LRESULT WINAPI
1640 MSISelectionTree_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1641 {
1642     struct msi_selection_tree_info *info;
1643     TVHITTESTINFO tvhti;
1644     HRESULT r;
1645
1646     TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
1647
1648     info = GetPropW(hWnd, szButtonData);
1649
1650     switch( msg )
1651     {
1652     case WM_LBUTTONDOWN:
1653         tvhti.pt.x = LOWORD( lParam );
1654         tvhti.pt.y = HIWORD( lParam );
1655         tvhti.flags = 0;
1656         tvhti.hItem = 0;
1657         r = CallWindowProcW(info->oldproc, hWnd, TVM_HITTEST, 0, (LPARAM) &tvhti );
1658         if (tvhti.flags & TVHT_ONITEMSTATEICON)
1659             return msi_seltree_menu( hWnd, tvhti.hItem );
1660         break;
1661     }
1662
1663     r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
1664
1665     switch( msg )
1666     {
1667     case WM_NCDESTROY:
1668         msi_free( info );
1669         RemovePropW( hWnd, szButtonData );
1670         break;
1671     }
1672     return r;
1673 }
1674
1675 static void
1676 msi_seltree_add_child_features( MSIPACKAGE *package, HWND hwnd,
1677                                 LPCWSTR parent, HTREEITEM hParent )
1678 {
1679     MSIFEATURE *feature;
1680     TVINSERTSTRUCTW tvis;
1681     HTREEITEM hitem;
1682
1683     LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
1684     {
1685         if ( lstrcmpW( parent, feature->Feature_Parent ) )
1686             continue;
1687
1688         if ( !feature->Title )
1689             continue;
1690
1691         if ( !feature->Display )
1692             continue;
1693
1694         memset( &tvis, 0, sizeof tvis );
1695         tvis.hParent = hParent;
1696         tvis.hInsertAfter = TVI_LAST;
1697         tvis.u.item.mask = TVIF_TEXT | TVIF_PARAM;
1698         tvis.u.item.pszText = feature->Title;
1699         tvis.u.item.lParam = (LPARAM) feature;
1700
1701         hitem = (HTREEITEM) SendMessageW( hwnd, TVM_INSERTITEMW, 0, (LPARAM) &tvis );
1702         if (!hitem)
1703             continue;
1704
1705         msi_seltree_sync_item_state( hwnd, feature, hitem );
1706         msi_seltree_add_child_features( package, hwnd,
1707                                         feature->Feature, hitem );
1708
1709         /* the node is expanded if Display is odd */
1710         if ( feature->Display % 2 != 0 )
1711             SendMessageW( hwnd, TVM_EXPAND, TVE_EXPAND, (LPARAM) hitem );
1712     }
1713 }
1714
1715 static void msi_seltree_create_imagelist( HWND hwnd )
1716 {
1717     const int bm_width = 32, bm_height = 16, bm_count = 3;
1718     const int bm_resource = 0x1001;
1719     HIMAGELIST himl;
1720     int i;
1721     HBITMAP hbmp;
1722
1723     himl = ImageList_Create( bm_width, bm_height, FALSE, 4, 0 );
1724     if (!himl)
1725     {
1726         ERR("failed to create image list\n");
1727         return;
1728     }
1729
1730     for (i=0; i<bm_count; i++)
1731     {
1732         hbmp = LoadBitmapW( msi_hInstance, MAKEINTRESOURCEW(i+bm_resource) );
1733         if (!hbmp)
1734         {
1735             ERR("failed to load bitmap %d\n", i);
1736             break;
1737         }
1738
1739         /*
1740          * Add a dummy bitmap at offset zero because the treeview
1741          * can't use it as a state mask (zero means no user state).
1742          */
1743         if (!i)
1744             ImageList_Add( himl, hbmp, NULL );
1745
1746         ImageList_Add( himl, hbmp, NULL );
1747     }
1748
1749     SendMessageW( hwnd, TVM_SETIMAGELIST, TVSIL_STATE, (LPARAM)himl );
1750 }
1751
1752 static UINT msi_dialog_selection_tree( msi_dialog *dialog, MSIRECORD *rec )
1753 {
1754     msi_control *control;
1755     LPCWSTR prop;
1756     MSIPACKAGE *package = dialog->package;
1757     DWORD style;
1758     struct msi_selection_tree_info *info;
1759
1760     info = msi_alloc( sizeof *info );
1761     if (!info)
1762         return ERROR_FUNCTION_FAILED;
1763
1764     /* create the treeview control */
1765     prop = MSI_RecordGetString( rec, 9 );
1766     style = TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT;
1767     style |= WS_GROUP | WS_VSCROLL;
1768     control = msi_dialog_add_control( dialog, rec, WC_TREEVIEWW, style );
1769     if (!control)
1770     {
1771         msi_free(info);
1772         return ERROR_FUNCTION_FAILED;
1773     }
1774
1775     /* subclass */
1776     info->dialog = dialog;
1777     info->hwnd = control->hwnd;
1778     info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1779                                           (LONG_PTR)MSISelectionTree_WndProc );
1780     SetPropW( control->hwnd, szButtonData, info );
1781
1782     /* initialize it */
1783     msi_seltree_create_imagelist( control->hwnd );
1784     msi_seltree_add_child_features( package, control->hwnd, NULL, NULL );
1785
1786     return ERROR_SUCCESS;
1787 }
1788
1789 /******************** Group Box ***************************************/
1790
1791 static UINT msi_dialog_group_box( msi_dialog *dialog, MSIRECORD *rec )
1792 {
1793     msi_control *control;
1794     DWORD style;
1795
1796     style = BS_GROUPBOX | WS_CHILD | WS_GROUP;
1797     control = msi_dialog_add_control( dialog, rec, WC_BUTTONW, style );
1798     if (!control)
1799         return ERROR_FUNCTION_FAILED;
1800
1801     return ERROR_SUCCESS;
1802 }
1803
1804 /******************** List Box ***************************************/
1805
1806 struct msi_listbox_item
1807 {
1808     LPWSTR property;
1809     LPWSTR value;
1810 };
1811
1812 struct msi_listbox_info
1813 {
1814     msi_dialog *dialog;
1815     HWND hwnd;
1816     WNDPROC oldproc;
1817     DWORD num_items;
1818     struct msi_listbox_item *items;
1819 };
1820
1821 static LRESULT WINAPI MSIListBox_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1822 {
1823     struct msi_listbox_info *info;
1824     LRESULT r;
1825     DWORD j;
1826
1827     TRACE("%p %04x %08x %08lx\n", hWnd, msg, wParam, lParam);
1828
1829     info = GetPropW( hWnd, szButtonData );
1830     if (!info)
1831         return 0;
1832
1833     r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );
1834
1835     switch( msg )
1836     {
1837     case WM_NCDESTROY:
1838         for (j = 0; j < info->num_items; j++)
1839         {
1840             msi_free( info->items[j].property );
1841             msi_free( info->items[j].value );
1842         }
1843         msi_free( info->items );
1844         msi_free( info );
1845         RemovePropW( hWnd, szButtonData );
1846         break;
1847     }
1848
1849     return r;
1850 }
1851
1852 static UINT msi_listbox_add_item( MSIRECORD *rec, LPVOID param )
1853 {
1854     struct msi_listbox_info *info = param;
1855     struct msi_listbox_item *item;
1856     LPCWSTR property, value, text;
1857     static int index = 0;
1858
1859     item = &info->items[index++];
1860     property = MSI_RecordGetString( rec, 1 );
1861     value = MSI_RecordGetString( rec, 3 );
1862     text = MSI_RecordGetString( rec, 4 );
1863
1864     item->property = strdupW( property );
1865     item->value = strdupW( value );
1866
1867     SendMessageW( info->hwnd, LB_ADDSTRING, 0, (LPARAM)text );
1868
1869     return ERROR_SUCCESS;
1870 }
1871
1872 static UINT msi_listbox_add_items( struct msi_listbox_info *info )
1873 {
1874     UINT r;
1875     MSIQUERY *view = NULL;
1876     DWORD count;
1877
1878     static const WCHAR query[] = {
1879         'S','E','L','E','C','T',' ','*',' ',
1880         'F','R','O','M',' ','`','L','i','s','t','B','o','x','`',' ',
1881         'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','`',0
1882     };
1883
1884     r = MSI_OpenQuery( info->dialog->package->db, &view, query );
1885     if ( r != ERROR_SUCCESS )
1886         return r;
1887
1888     /* just get the number of records */
1889     r = MSI_IterateRecords( view, &count, NULL, NULL );
1890
1891     info->num_items = count;
1892     info->items = msi_alloc( sizeof(*info->items) * count );
1893
1894     r = MSI_IterateRecords( view, NULL, msi_listbox_add_item, info );
1895     msiobj_release( &view->hdr );
1896
1897     return r;
1898 }
1899
1900 static UINT msi_dialog_listbox_handler( msi_dialog *dialog,
1901                                         msi_control *control, WPARAM param )
1902 {
1903     struct msi_listbox_info *info;
1904     int index;
1905
1906     if( HIWORD(param) != LBN_SELCHANGE )
1907         return ERROR_SUCCESS;
1908
1909     info = GetPropW( control->hwnd, szButtonData );
1910     index = SendMessageW( control->hwnd, LB_GETCURSEL, 0, 0 );
1911
1912     MSI_SetPropertyW( info->dialog->package,
1913                       info->items[index].property, info->items[index].value );
1914     msi_dialog_evaluate_control_conditions( info->dialog );
1915
1916     return ERROR_SUCCESS;
1917 }
1918
1919 static UINT msi_dialog_list_box( msi_dialog *dialog, MSIRECORD *rec )
1920 {
1921     struct msi_listbox_info *info;
1922     msi_control *control;
1923     DWORD style;
1924
1925     info = msi_alloc( sizeof *info );
1926     if (!info)
1927         return ERROR_FUNCTION_FAILED;
1928
1929     style = WS_TABSTOP | WS_GROUP | WS_CHILD | LBS_STANDARD;
1930     control = msi_dialog_add_control( dialog, rec, WC_LISTBOXW, style );
1931     if (!control)
1932         return ERROR_FUNCTION_FAILED;
1933
1934     control->handler = msi_dialog_listbox_handler;
1935
1936     /* subclass */
1937     info->dialog = dialog;
1938     info->hwnd = control->hwnd;
1939     info->items = NULL;
1940     info->oldproc = (WNDPROC)SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
1941                                                 (LONG_PTR)MSIListBox_WndProc );
1942     SetPropW( control->hwnd, szButtonData, info );
1943
1944     msi_listbox_add_items( info );
1945
1946     return ERROR_SUCCESS;
1947 }
1948
1949 /******************** Directory Combo ***************************************/
1950
1951 static LPWSTR msi_dialog_dup_property( msi_dialog *dialog, LPCWSTR property, BOOL indirect )
1952 {
1953     if (!property)
1954         return NULL;
1955
1956     if (indirect)
1957         return msi_dup_property( dialog->package, property );
1958
1959     return strdupW( property );
1960 }
1961
1962 static UINT msi_dialog_directory_combo( msi_dialog *dialog, MSIRECORD *rec )
1963 {
1964     msi_control *control;
1965     LPWSTR path, propval;
1966     BOOL indirect;
1967     LPCWSTR prop;
1968     DWORD style;
1969
1970     /* FIXME: use CBS_OWNERDRAWFIXED and add owner draw code */
1971     style = CBS_DROPDOWNLIST | CBS_HASSTRINGS | WS_CHILD |
1972             WS_GROUP | WS_TABSTOP | WS_VSCROLL;
1973     control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
1974     if (!control)
1975         return ERROR_FUNCTION_FAILED;
1976
1977     control->attributes = MSI_RecordGetInteger( rec, 8 );
1978     prop = MSI_RecordGetString( rec, 9 );
1979     control->property = msi_dialog_dup_property( dialog, prop, FALSE );
1980
1981     indirect = control->attributes & msidbControlAttributesIndirect;
1982     propval = msi_dialog_dup_property( dialog, prop, indirect );
1983     path = msi_dup_property( dialog->package, propval );
1984
1985     PathStripPathW( path );
1986     PathRemoveBackslashW( path );
1987
1988     /* FIXME: Add whole directory structure to combo box */
1989     SendMessageW( control->hwnd, CB_ADDSTRING, 0, (LPARAM)path );
1990     SendMessageW( control->hwnd, CB_SETCURSEL, 0, 0 );
1991
1992     msi_free( path );
1993     msi_free( propval );
1994     return ERROR_SUCCESS;
1995 }
1996
1997 /******************** Directory List ***************************************/
1998
1999 UINT msi_dialog_directorylist_up( msi_dialog *dialog )
2000 {
2001     msi_control *control;
2002     LPWSTR prop, path, ptr;
2003     BOOL indirect;
2004
2005     control = msi_dialog_find_control( dialog, szDirectoryList );
2006     indirect = control->attributes & msidbControlAttributesIndirect;
2007     prop = msi_dialog_dup_property( dialog, control->property, indirect );
2008
2009     path = msi_dup_property( dialog->package, prop );
2010
2011     /* strip off the last directory */
2012     ptr = PathFindFileNameW( path );
2013     if (ptr != path) *(ptr - 1) = '\0';
2014     PathAddBackslashW( path );
2015
2016     MSI_SetPropertyW( dialog->package, prop, path );
2017
2018     msi_free( path );
2019     msi_free( prop );
2020
2021     return ERROR_SUCCESS;
2022 }
2023
2024 static UINT msi_dialog_directory_list( msi_dialog *dialog, MSIRECORD *rec )
2025 {
2026     msi_control *control;
2027     LPCWSTR prop;
2028     DWORD style;
2029
2030     style = LVS_LIST | LVS_EDITLABELS | WS_VSCROLL | LVS_SHAREIMAGELISTS |
2031             LVS_AUTOARRANGE | LVS_SINGLESEL | WS_BORDER |
2032             LVS_SORTASCENDING | WS_CHILD | WS_GROUP | WS_TABSTOP;
2033     control = msi_dialog_add_control( dialog, rec, WC_LISTVIEWW, style );
2034     if (!control)
2035         return ERROR_FUNCTION_FAILED;
2036
2037     control->attributes = MSI_RecordGetInteger( rec, 8 );
2038     prop = MSI_RecordGetString( rec, 9 );
2039     control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2040
2041     return ERROR_SUCCESS;
2042 }
2043
2044 /******************** VolumeCost List ***************************************/
2045
2046 static UINT msi_dialog_volumecost_list( msi_dialog *dialog, MSIRECORD *rec )
2047 {
2048     msi_control *control;
2049     DWORD style;
2050
2051     style = LVS_REPORT | WS_VSCROLL | WS_HSCROLL | LVS_SHAREIMAGELISTS |
2052             LVS_AUTOARRANGE | LVS_SINGLESEL | WS_BORDER |
2053             WS_CHILD | WS_TABSTOP | WS_GROUP;
2054     control = msi_dialog_add_control( dialog, rec, WC_LISTVIEWW, style );
2055     if (!control)
2056         return ERROR_FUNCTION_FAILED;
2057
2058     return ERROR_SUCCESS;
2059 }
2060
2061 static const struct control_handler msi_dialog_handler[] =
2062 {
2063     { szText, msi_dialog_text_control },
2064     { szPushButton, msi_dialog_button_control },
2065     { szLine, msi_dialog_line_control },
2066     { szBitmap, msi_dialog_bitmap_control },
2067     { szCheckBox, msi_dialog_checkbox_control },
2068     { szScrollableText, msi_dialog_scrolltext_control },
2069     { szComboBox, msi_dialog_combo_control },
2070     { szEdit, msi_dialog_edit_control },
2071     { szMaskedEdit, msi_dialog_maskedit_control },
2072     { szPathEdit, msi_dialog_pathedit_control },
2073     { szProgressBar, msi_dialog_progress_bar },
2074     { szRadioButtonGroup, msi_dialog_radiogroup_control },
2075     { szIcon, msi_dialog_icon_control },
2076     { szSelectionTree, msi_dialog_selection_tree },
2077     { szGroupBox, msi_dialog_group_box },
2078     { szListBox, msi_dialog_list_box },
2079     { szDirectoryCombo, msi_dialog_directory_combo },
2080     { szDirectoryList, msi_dialog_directory_list },
2081     { szVolumeCostList, msi_dialog_volumecost_list },
2082 };
2083
2084 #define NUM_CONTROL_TYPES (sizeof msi_dialog_handler/sizeof msi_dialog_handler[0])
2085
2086 static UINT msi_dialog_create_controls( MSIRECORD *rec, LPVOID param )
2087 {
2088     msi_dialog *dialog = param;
2089     LPCWSTR control_type;
2090     UINT i;
2091
2092     /* find and call the function that can create this type of control */
2093     control_type = MSI_RecordGetString( rec, 3 );
2094     for( i=0; i<NUM_CONTROL_TYPES; i++ )
2095         if (!strcmpiW( msi_dialog_handler[i].control_type, control_type ))
2096             break;
2097     if( i != NUM_CONTROL_TYPES )
2098         msi_dialog_handler[i].func( dialog, rec );
2099     else
2100         ERR("no handler for element type %s\n", debugstr_w(control_type));
2101
2102     return ERROR_SUCCESS;
2103 }
2104
2105 static UINT msi_dialog_fill_controls( msi_dialog *dialog )
2106 {
2107     static const WCHAR query[] = {
2108         'S','E','L','E','C','T',' ','*',' ',
2109         'F','R','O','M',' ','C','o','n','t','r','o','l',' ',
2110         'W','H','E','R','E',' ',
2111            '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
2112     UINT r;
2113     MSIQUERY *view = NULL;
2114     MSIPACKAGE *package = dialog->package;
2115
2116     TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
2117
2118     /* query the Control table for all the elements of the control */
2119     r = MSI_OpenQuery( package->db, &view, query, dialog->name );
2120     if( r != ERROR_SUCCESS )
2121     {
2122         ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
2123         return ERROR_INVALID_PARAMETER;
2124     }
2125
2126     r = MSI_IterateRecords( view, 0, msi_dialog_create_controls, dialog );
2127     msiobj_release( &view->hdr );
2128
2129     return r;
2130 }
2131
2132 static UINT msi_dialog_set_control_condition( MSIRECORD *rec, LPVOID param )
2133 {
2134     static const WCHAR szHide[] = { 'H','i','d','e',0 };
2135     static const WCHAR szShow[] = { 'S','h','o','w',0 };
2136     static const WCHAR szDisable[] = { 'D','i','s','a','b','l','e',0 };
2137     static const WCHAR szEnable[] = { 'E','n','a','b','l','e',0 };
2138     msi_dialog *dialog = param;
2139     msi_control *control;
2140     LPCWSTR name, action, condition;
2141     UINT r;
2142
2143     name = MSI_RecordGetString( rec, 2 );
2144     action = MSI_RecordGetString( rec, 3 );
2145     condition = MSI_RecordGetString( rec, 4 );
2146     r = MSI_EvaluateConditionW( dialog->package, condition );
2147     control = msi_dialog_find_control( dialog, name );
2148     if( r == MSICONDITION_TRUE && control )
2149     {
2150         TRACE("%s control %s\n", debugstr_w(action), debugstr_w(name));
2151
2152         /* FIXME: case sensitive? */
2153         if(!lstrcmpW(action, szHide))
2154             ShowWindow(control->hwnd, SW_HIDE);
2155         else if(!strcmpW(action, szShow))
2156             ShowWindow(control->hwnd, SW_SHOW);
2157         else if(!strcmpW(action, szDisable))
2158             EnableWindow(control->hwnd, FALSE);
2159         else if(!strcmpW(action, szEnable))
2160             EnableWindow(control->hwnd, TRUE);
2161         else
2162             FIXME("Unhandled action %s\n", debugstr_w(action));
2163     }
2164
2165     return ERROR_SUCCESS;
2166 }
2167
2168 static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog )
2169 {
2170     static const WCHAR query[] = {
2171       'S','E','L','E','C','T',' ','*',' ',
2172       'F','R','O','M',' ',
2173         'C','o','n','t','r','o','l','C','o','n','d','i','t','i','o','n',' ',
2174       'W','H','E','R','E',' ',
2175         '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0
2176     };
2177     UINT r;
2178     MSIQUERY *view = NULL;
2179     MSIPACKAGE *package = dialog->package;
2180
2181     TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
2182
2183     /* query the Control table for all the elements of the control */
2184     r = MSI_OpenQuery( package->db, &view, query, dialog->name );
2185     if( r != ERROR_SUCCESS )
2186     {
2187         ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
2188         return ERROR_INVALID_PARAMETER;
2189     }
2190
2191     r = MSI_IterateRecords( view, 0, msi_dialog_set_control_condition, dialog );
2192     msiobj_release( &view->hdr );
2193
2194     return r;
2195 }
2196
2197 UINT msi_dialog_reset( msi_dialog *dialog )
2198 {
2199     /* FIXME: should restore the original values of any properties we changed */
2200     return msi_dialog_evaluate_control_conditions( dialog );
2201 }
2202
2203 /* figure out the height of 10 point MS Sans Serif */
2204 static INT msi_dialog_get_sans_serif_height( HWND hwnd )
2205 {
2206     static const WCHAR szSansSerif[] = {
2207         'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0 };
2208     LOGFONTW lf;
2209     TEXTMETRICW tm;
2210     BOOL r;
2211     LONG height = 0;
2212     HFONT hFont, hOldFont;
2213     HDC hdc;
2214
2215     hdc = GetDC( hwnd );
2216     if (hdc)
2217     {
2218         memset( &lf, 0, sizeof lf );
2219         lf.lfHeight = MulDiv(10, GetDeviceCaps(hdc, LOGPIXELSY), 72);
2220         strcpyW( lf.lfFaceName, szSansSerif );
2221         hFont = CreateFontIndirectW(&lf);
2222         if (hFont)
2223         {
2224             hOldFont = SelectObject( hdc, hFont );
2225             r = GetTextMetricsW( hdc, &tm );
2226             if (r)
2227                 height = tm.tmHeight;
2228             SelectObject( hdc, hOldFont );
2229             DeleteObject( hFont );
2230         }
2231         ReleaseDC( hwnd, hdc );
2232     }
2233     return height;
2234 }
2235
2236 /* fetch the associated record from the Dialog table */
2237 static MSIRECORD *msi_get_dialog_record( msi_dialog *dialog )
2238 {
2239     static const WCHAR query[] = {
2240         'S','E','L','E','C','T',' ','*',' ',
2241         'F','R','O','M',' ','D','i','a','l','o','g',' ',
2242         'W','H','E','R','E',' ',
2243            '`','D','i','a','l','o','g','`',' ','=',' ','\'','%','s','\'',0};
2244     MSIPACKAGE *package = dialog->package;
2245     MSIRECORD *rec = NULL;
2246
2247     TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
2248
2249     rec = MSI_QueryGetRecord( package->db, query, dialog->name );
2250     if( !rec )
2251         ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
2252
2253     return rec;
2254 }
2255
2256 static void msi_dialog_adjust_dialog_pos( msi_dialog *dialog, MSIRECORD *rec, LPRECT pos )
2257 {
2258     static const WCHAR szScreenX[] = {'S','c','r','e','e','n','X',0};
2259     static const WCHAR szScreenY[] = {'S','c','r','e','e','n','Y',0};
2260
2261     UINT xres, yres;
2262     POINT center;
2263     SIZE sz;
2264     LONG style;
2265
2266     center.x = MSI_RecordGetInteger( rec, 2 );
2267     center.y = MSI_RecordGetInteger( rec, 3 );
2268
2269     sz.cx = MSI_RecordGetInteger( rec, 4 );
2270     sz.cy = MSI_RecordGetInteger( rec, 5 );
2271
2272     sz.cx = msi_dialog_scale_unit( dialog, sz.cx );
2273     sz.cy = msi_dialog_scale_unit( dialog, sz.cy );
2274
2275     xres = msi_get_property_int( dialog->package, szScreenX, 0 );
2276     yres = msi_get_property_int( dialog->package, szScreenY, 0 );
2277
2278     center.x = MulDiv( center.x, xres, 100 );
2279     center.y = MulDiv( center.y, yres, 100 );
2280
2281     /* turn the client pos into the window rectangle */
2282     pos->left = center.x - sz.cx/2;
2283     pos->right = pos->left + sz.cx;
2284     pos->top = center.y - sz.cy/2;
2285     pos->bottom = pos->top + sz.cy;
2286
2287     TRACE("%lu %lu %lu %lu\n", pos->left, pos->top, pos->right, pos->bottom);
2288
2289     style = GetWindowLongPtrW( dialog->hwnd, GWL_STYLE );
2290     AdjustWindowRect( pos, style, FALSE );
2291 }
2292
2293 static BOOL msi_control_set_next( msi_control *control, msi_control *next )
2294 {
2295     return SetWindowPos( next->hwnd, control->hwnd, 0, 0, 0, 0,
2296                          SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOREDRAW |
2297                          SWP_NOREPOSITION | SWP_NOSENDCHANGING | SWP_NOSIZE );
2298 }
2299
2300 static UINT msi_dialog_set_tab_order( msi_dialog *dialog )
2301 {
2302     msi_control *control, *tab_next;
2303
2304     LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
2305     {
2306         tab_next = msi_dialog_find_control( dialog, control->tabnext );
2307         if( !tab_next )
2308             continue;
2309         msi_control_set_next( control, tab_next );
2310     }
2311
2312     return ERROR_SUCCESS;
2313 }
2314
2315 static void msi_dialog_set_first_control( msi_dialog* dialog, LPCWSTR name )
2316 {
2317     msi_control *control;
2318
2319     control = msi_dialog_find_control( dialog, name );
2320     if( control )
2321         dialog->hWndFocus = control->hwnd;
2322     else
2323         dialog->hWndFocus = NULL;
2324 }
2325
2326 static LRESULT msi_dialog_oncreate( HWND hwnd, LPCREATESTRUCTW cs )
2327 {
2328     static const WCHAR df[] = {
2329         'D','e','f','a','u','l','t','U','I','F','o','n','t',0 };
2330     static const WCHAR dfv[] = {
2331         'M','S',' ','S','h','e','l','l',' ','D','l','g',0 };
2332     msi_dialog *dialog = (msi_dialog*) cs->lpCreateParams;
2333     MSIRECORD *rec = NULL;
2334     LPWSTR title = NULL;
2335     RECT pos;
2336
2337     TRACE("%p %p\n", dialog, dialog->package);
2338
2339     dialog->hwnd = hwnd;
2340     SetWindowLongPtrW( hwnd, GWLP_USERDATA, (LONG_PTR) dialog );
2341
2342     rec = msi_get_dialog_record( dialog );
2343     if( !rec )
2344     {
2345         TRACE("No record found for dialog %s\n", debugstr_w(dialog->name));
2346         return -1;
2347     }
2348
2349     dialog->scale = msi_dialog_get_sans_serif_height(dialog->hwnd);
2350
2351     msi_dialog_adjust_dialog_pos( dialog, rec, &pos );
2352
2353     dialog->attributes = MSI_RecordGetInteger( rec, 6 );
2354
2355     dialog->default_font = msi_dup_property( dialog->package, df );
2356     if (!dialog->default_font)
2357     {
2358         dialog->default_font = strdupW(dfv);
2359         if (!dialog->default_font) return -1;
2360     }
2361
2362     title = msi_get_deformatted_field( dialog->package, rec, 7 );
2363     SetWindowTextW( hwnd, title );
2364     msi_free( title );
2365
2366     SetWindowPos( hwnd, 0, pos.left, pos.top,
2367                   pos.right - pos.left, pos.bottom - pos.top,
2368                   SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW );
2369
2370     msi_dialog_build_font_list( dialog );
2371     msi_dialog_fill_controls( dialog );
2372     msi_dialog_evaluate_control_conditions( dialog );
2373     msi_dialog_set_tab_order( dialog );
2374     msi_dialog_set_first_control( dialog, MSI_RecordGetString( rec, 8 ) );
2375     msiobj_release( &rec->hdr );
2376
2377     return 0;
2378 }
2379
2380 static UINT msi_dialog_send_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
2381 {
2382     LPWSTR event_fmt = NULL, arg_fmt = NULL;
2383
2384     TRACE("Sending control event %s %s\n", debugstr_w(event), debugstr_w(arg));
2385
2386     deformat_string( dialog->package, event, &event_fmt );
2387     deformat_string( dialog->package, arg, &arg_fmt );
2388
2389     dialog->event_handler( dialog->package, event_fmt, arg_fmt, dialog );
2390
2391     msi_free( event_fmt );
2392     msi_free( arg_fmt );
2393
2394     return ERROR_SUCCESS;
2395 }
2396
2397 static UINT msi_dialog_set_property( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
2398 {
2399     static const WCHAR szNullArg[] = { '{','}',0 };
2400     LPWSTR p, prop, arg_fmt = NULL;
2401     UINT len;
2402
2403     len = strlenW(event);
2404     prop = msi_alloc( len*sizeof(WCHAR));
2405     strcpyW( prop, &event[1] );
2406     p = strchrW( prop, ']' );
2407     if( p && p[1] == 0 )
2408     {
2409         *p = 0;
2410         if( strcmpW( szNullArg, arg ) )
2411             deformat_string( dialog->package, arg, &arg_fmt );
2412         MSI_SetPropertyW( dialog->package, prop, arg_fmt );
2413         msi_free( arg_fmt );
2414     }
2415     else
2416         ERR("Badly formatted property string - what happens?\n");
2417     msi_free( prop );
2418     return ERROR_SUCCESS;
2419 }
2420
2421 static UINT msi_dialog_control_event( MSIRECORD *rec, LPVOID param )
2422 {
2423     msi_dialog *dialog = param;
2424     LPCWSTR condition, event, arg;
2425     UINT r;
2426
2427     condition = MSI_RecordGetString( rec, 5 );
2428     r = MSI_EvaluateConditionW( dialog->package, condition );
2429     if( r == MSICONDITION_TRUE || r == MSICONDITION_NONE )
2430     {
2431         event = MSI_RecordGetString( rec, 3 );
2432         arg = MSI_RecordGetString( rec, 4 );
2433         if( event[0] == '[' )
2434             msi_dialog_set_property( dialog, event, arg );
2435         else
2436             msi_dialog_send_event( dialog, event, arg );
2437     }
2438
2439     return ERROR_SUCCESS;
2440 }
2441
2442 static UINT msi_dialog_button_handler( msi_dialog *dialog,
2443                                        msi_control *control, WPARAM param )
2444 {
2445     static const WCHAR query[] = {
2446       'S','E','L','E','C','T',' ','*',' ',
2447       'F','R','O','M',' ','C','o','n','t','r','o','l','E','v','e','n','t',' ',
2448       'W','H','E','R','E',' ',
2449          '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
2450       'A','N','D',' ',
2451          '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',' ',
2452       'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','i','n','g','`',0
2453     };
2454     MSIQUERY *view = NULL;
2455     UINT r;
2456
2457     if( HIWORD(param) != BN_CLICKED )
2458         return ERROR_SUCCESS;
2459
2460     r = MSI_OpenQuery( dialog->package->db, &view, query,
2461                        dialog->name, control->name );
2462     if( r != ERROR_SUCCESS )
2463     {
2464         ERR("query failed\n");
2465         return 0;
2466     }
2467
2468     r = MSI_IterateRecords( view, 0, msi_dialog_control_event, dialog );
2469     msiobj_release( &view->hdr );
2470
2471     return r;
2472 }
2473
2474 static UINT msi_dialog_get_checkbox_state( msi_dialog *dialog,
2475                 msi_control *control )
2476 {
2477     WCHAR state[2] = { 0 };
2478     DWORD sz = 2;
2479
2480     MSI_GetPropertyW( dialog->package, control->property, state, &sz );
2481     return state[0] ? 1 : 0;
2482 }
2483
2484 static void msi_dialog_set_checkbox_state( msi_dialog *dialog,
2485                 msi_control *control, UINT state )
2486 {
2487     static const WCHAR szState[] = { '1', 0 };
2488     LPCWSTR val;
2489
2490     /* if uncheck then the property is set to NULL */
2491     if (!state)
2492     {
2493         MSI_SetPropertyW( dialog->package, control->property, NULL );
2494         return;
2495     }
2496
2497     /* check for a custom state */
2498     if (control->value && control->value[0])
2499         val = control->value;
2500     else
2501         val = szState;
2502
2503     MSI_SetPropertyW( dialog->package, control->property, val );
2504 }
2505
2506 static void msi_dialog_checkbox_sync_state( msi_dialog *dialog,
2507                 msi_control *control )
2508 {
2509     UINT state;
2510
2511     state = msi_dialog_get_checkbox_state( dialog, control );
2512     SendMessageW( control->hwnd, BM_SETCHECK,
2513                   state ? BST_CHECKED : BST_UNCHECKED, 0 );
2514 }
2515
2516 static UINT msi_dialog_checkbox_handler( msi_dialog *dialog,
2517                 msi_control *control, WPARAM param )
2518 {
2519     UINT state;
2520
2521     if( HIWORD(param) != BN_CLICKED )
2522         return ERROR_SUCCESS;
2523
2524     TRACE("clicked checkbox %s, set %s\n", debugstr_w(control->name),
2525           debugstr_w(control->property));
2526
2527     state = msi_dialog_get_checkbox_state( dialog, control );
2528     state = state ? 0 : 1;
2529     msi_dialog_set_checkbox_state( dialog, control, state );
2530     msi_dialog_checkbox_sync_state( dialog, control );
2531
2532     return msi_dialog_button_handler( dialog, control, param );
2533 }
2534
2535 static UINT msi_dialog_edit_handler( msi_dialog *dialog,
2536                 msi_control *control, WPARAM param )
2537 {
2538     LPWSTR buf;
2539
2540     if( HIWORD(param) != EN_CHANGE )
2541         return ERROR_SUCCESS;
2542
2543     TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
2544           debugstr_w(control->property));
2545
2546     buf = msi_get_window_text( control->hwnd );
2547     MSI_SetPropertyW( dialog->package, control->property, buf );
2548
2549     msi_free( buf );
2550
2551     return ERROR_SUCCESS;
2552 }
2553
2554 static UINT msi_dialog_radiogroup_handler( msi_dialog *dialog,
2555                 msi_control *control, WPARAM param )
2556 {
2557     if( HIWORD(param) != BN_CLICKED )
2558         return ERROR_SUCCESS;
2559
2560     TRACE("clicked radio button %s, set %s\n", debugstr_w(control->name),
2561           debugstr_w(control->property));
2562
2563     MSI_SetPropertyW( dialog->package, control->property, control->name );
2564
2565     return msi_dialog_button_handler( dialog, control, param );
2566 }
2567
2568 static LRESULT msi_dialog_oncommand( msi_dialog *dialog, WPARAM param, HWND hwnd )
2569 {
2570     msi_control *control = NULL;
2571
2572     TRACE("%p %p %08x\n", dialog, hwnd, param);
2573
2574     switch (param)
2575     {
2576     case 1: /* enter */
2577         control = msi_dialog_find_control( dialog, dialog->control_default );
2578         break;
2579     case 2: /* escape */
2580         control = msi_dialog_find_control( dialog, dialog->control_cancel );
2581         break;
2582     default: 
2583         control = msi_dialog_find_control_by_hwnd( dialog, hwnd );
2584     }
2585
2586     if( control )
2587     {
2588         if( control->handler )
2589         {
2590             control->handler( dialog, control, param );
2591             msi_dialog_evaluate_control_conditions( dialog );
2592         }
2593     }
2594     else
2595         ERR("button click from nowhere %p %d %p\n", dialog, param, hwnd);
2596     return 0;
2597 }
2598
2599 static void msi_dialog_setfocus( msi_dialog *dialog )
2600 {
2601     HWND hwnd = dialog->hWndFocus;
2602
2603     hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, TRUE);
2604     hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, FALSE);
2605     SetFocus( hwnd );
2606     dialog->hWndFocus = hwnd;
2607 }
2608
2609 static LRESULT WINAPI MSIDialog_WndProc( HWND hwnd, UINT msg,
2610                 WPARAM wParam, LPARAM lParam )
2611 {
2612     msi_dialog *dialog = (LPVOID) GetWindowLongPtrW( hwnd, GWLP_USERDATA );
2613
2614     TRACE("0x%04x\n", msg);
2615
2616     switch (msg)
2617     {
2618     case WM_CREATE:
2619         return msi_dialog_oncreate( hwnd, (LPCREATESTRUCTW)lParam );
2620
2621     case WM_COMMAND:
2622         return msi_dialog_oncommand( dialog, wParam, (HWND)lParam );
2623
2624     case WM_ACTIVATE:
2625         if( LOWORD(wParam) == WA_INACTIVE )
2626             dialog->hWndFocus = GetFocus();
2627         else
2628             msi_dialog_setfocus( dialog );
2629         return 0;
2630
2631     case WM_SETFOCUS:
2632         msi_dialog_setfocus( dialog );
2633         return 0;
2634
2635     /* bounce back to our subclassed static control */
2636     case WM_CTLCOLORSTATIC:
2637         return SendMessageW( (HWND) lParam, WM_CTLCOLORSTATIC, wParam, lParam );
2638
2639     case WM_DESTROY:
2640         dialog->hwnd = NULL;
2641         return 0;
2642     }
2643     return DefWindowProcW(hwnd, msg, wParam, lParam);
2644 }
2645
2646 static BOOL CALLBACK msi_radioground_child_enum( HWND hWnd, LPARAM lParam )
2647 {
2648     EnableWindow( hWnd, lParam );
2649     return TRUE;
2650 }
2651
2652 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2653 {
2654     WNDPROC oldproc = (WNDPROC) GetPropW(hWnd, szButtonData);
2655     LRESULT r;
2656
2657     TRACE("hWnd %p msg %04x wParam 0x%08x lParam 0x%08lx\n", hWnd, msg, wParam, lParam);
2658
2659     if (msg == WM_COMMAND) /* Forward notifications to dialog */
2660         SendMessageW(GetParent(hWnd), msg, wParam, lParam);
2661
2662     r = CallWindowProcW(oldproc, hWnd, msg, wParam, lParam);
2663
2664     /* make sure the radio buttons show as disabled if the parent is disabled */
2665     if (msg == WM_ENABLE)
2666         EnumChildWindows( hWnd, msi_radioground_child_enum, wParam );
2667
2668     return r;
2669 }
2670
2671 static LRESULT WINAPI MSIHiddenWindowProc( HWND hwnd, UINT msg,
2672                 WPARAM wParam, LPARAM lParam )
2673 {
2674     msi_dialog *dialog = (msi_dialog*) lParam;
2675
2676     TRACE("%d %p\n", msg, dialog);
2677
2678     switch (msg)
2679     {
2680     case WM_MSI_DIALOG_CREATE:
2681         return msi_dialog_run_message_loop( dialog );
2682     case WM_MSI_DIALOG_DESTROY:
2683         msi_dialog_destroy( dialog );
2684         return 0;
2685     }
2686     return DefWindowProcW( hwnd, msg, wParam, lParam );
2687 }
2688
2689 /* functions that interface to other modules within MSI */
2690
2691 msi_dialog *msi_dialog_create( MSIPACKAGE* package, LPCWSTR szDialogName,
2692                                 msi_dialog_event_handler event_handler )
2693 {
2694     MSIRECORD *rec = NULL;
2695     msi_dialog *dialog;
2696
2697     TRACE("%p %s\n", package, debugstr_w(szDialogName));
2698
2699     /* allocate the structure for the dialog to use */
2700     dialog = msi_alloc_zero( sizeof *dialog + sizeof(WCHAR)*strlenW(szDialogName) );
2701     if( !dialog )
2702         return NULL;
2703     strcpyW( dialog->name, szDialogName );
2704     msiobj_addref( &package->hdr );
2705     dialog->package = package;
2706     dialog->event_handler = event_handler;
2707     dialog->finished = 0;
2708     list_init( &dialog->controls );
2709
2710     /* verify that the dialog exists */
2711     rec = msi_get_dialog_record( dialog );
2712     if( !rec )
2713     {
2714         msiobj_release( &package->hdr );
2715         msi_free( dialog );
2716         return NULL;
2717     }
2718     dialog->attributes = MSI_RecordGetInteger( rec, 6 );
2719     dialog->control_default = strdupW( MSI_RecordGetString( rec, 9 ) );
2720     dialog->control_cancel = strdupW( MSI_RecordGetString( rec, 10 ) );
2721     msiobj_release( &rec->hdr );
2722
2723     return dialog;
2724 }
2725
2726 static void msi_process_pending_messages( HWND hdlg )
2727 {
2728     MSG msg;
2729
2730     while( PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ) )
2731     {
2732         if( hdlg && IsDialogMessageW( hdlg, &msg ))
2733             continue;
2734         TranslateMessage( &msg );
2735         DispatchMessageW( &msg );
2736     }
2737 }
2738
2739 void msi_dialog_end_dialog( msi_dialog *dialog )
2740 {
2741     TRACE("%p\n", dialog);
2742     dialog->finished = 1;
2743     PostMessageW(dialog->hwnd, WM_NULL, 0, 0);
2744 }
2745
2746 void msi_dialog_check_messages( HANDLE handle )
2747 {
2748     DWORD r;
2749
2750     /* in threads other than the UI thread, block */
2751     if( uiThreadId != GetCurrentThreadId() )
2752     {
2753         if( handle )
2754             WaitForSingleObject( handle, INFINITE );
2755         return;
2756     }
2757
2758     /* there's two choices for the UI thread */
2759     while (1)
2760     {
2761         msi_process_pending_messages( NULL );
2762
2763         if( !handle )
2764             break;
2765
2766         /*
2767          * block here until somebody creates a new dialog or
2768          * the handle we're waiting on becomes ready
2769          */
2770         r = MsgWaitForMultipleObjects( 1, &handle, 0, INFINITE, QS_ALLINPUT );
2771         if( r == WAIT_OBJECT_0 )
2772             break;
2773     }
2774 }
2775
2776 UINT msi_dialog_run_message_loop( msi_dialog *dialog )
2777 {
2778     DWORD style;
2779     HWND hwnd;
2780
2781     if( uiThreadId != GetCurrentThreadId() )
2782         return SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_CREATE, 0, (LPARAM) dialog );
2783
2784     /* create the dialog window, don't show it yet */
2785     style = WS_OVERLAPPED;
2786     if( dialog->attributes & msidbDialogAttributesVisible )
2787         style |= WS_VISIBLE;
2788
2789     hwnd = CreateWindowW( szMsiDialogClass, dialog->name, style,
2790                      CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
2791                      NULL, NULL, NULL, dialog );
2792     if( !hwnd )
2793     {
2794         ERR("Failed to create dialog %s\n", debugstr_w( dialog->name ));
2795         return ERROR_FUNCTION_FAILED;
2796     }
2797
2798     ShowWindow( hwnd, SW_SHOW );
2799     /* UpdateWindow( hwnd ); - and causes the transparent static controls not to paint */
2800
2801     if( dialog->attributes & msidbDialogAttributesModal )
2802     {
2803         while( !dialog->finished )
2804         {
2805             MsgWaitForMultipleObjects( 0, NULL, 0, INFINITE, QS_ALLINPUT );
2806             msi_process_pending_messages( dialog->hwnd );
2807         }
2808     }
2809     else
2810         return ERROR_IO_PENDING;
2811
2812     return ERROR_SUCCESS;
2813 }
2814
2815 void msi_dialog_do_preview( msi_dialog *dialog )
2816 {
2817     TRACE("\n");
2818     dialog->attributes |= msidbDialogAttributesVisible;
2819     dialog->attributes &= ~msidbDialogAttributesModal;
2820     msi_dialog_run_message_loop( dialog );
2821 }
2822
2823 void msi_dialog_destroy( msi_dialog *dialog )
2824 {
2825     if( uiThreadId != GetCurrentThreadId() )
2826     {
2827         SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_DESTROY, 0, (LPARAM) dialog );
2828         return;
2829     }
2830
2831     if( dialog->hwnd )
2832         ShowWindow( dialog->hwnd, SW_HIDE );
2833     
2834     if( dialog->hwnd )
2835         DestroyWindow( dialog->hwnd );
2836
2837     /* destroy the list of controls */
2838     while( !list_empty( &dialog->controls ) )
2839     {
2840         msi_control *t = LIST_ENTRY( list_head( &dialog->controls ),
2841                                      msi_control, entry );
2842         list_remove( &t->entry );
2843         /* leave dialog->hwnd - destroying parent destroys child windows */
2844         msi_free( t->property );
2845         msi_free( t->value );
2846         if( t->hBitmap )
2847             DeleteObject( t->hBitmap );
2848         if( t->hIcon )
2849             DestroyIcon( t->hIcon );
2850         msi_free( t->tabnext );
2851         msi_free( t );
2852         if (t->hDll)
2853             FreeLibrary( t->hDll );
2854     }
2855
2856     /* destroy the list of fonts */
2857     while( dialog->font_list )
2858     {
2859         msi_font *t = dialog->font_list;
2860         dialog->font_list = t->next;
2861         DeleteObject( t->hfont );
2862         msi_free( t );
2863     }
2864     msi_free( dialog->default_font );
2865
2866     msi_free( dialog->control_default );
2867     msi_free( dialog->control_cancel );
2868     msiobj_release( &dialog->package->hdr );
2869     dialog->package = NULL;
2870     msi_free( dialog );
2871 }
2872
2873 BOOL msi_dialog_register_class( void )
2874 {
2875     WNDCLASSW cls;
2876
2877     ZeroMemory( &cls, sizeof cls );
2878     cls.lpfnWndProc   = MSIDialog_WndProc;
2879     cls.hInstance     = NULL;
2880     cls.hIcon         = LoadIconW(0, (LPWSTR)IDI_APPLICATION);
2881     cls.hCursor       = LoadCursorW(0, (LPWSTR)IDC_ARROW);
2882     cls.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
2883     cls.lpszMenuName  = NULL;
2884     cls.lpszClassName = szMsiDialogClass;
2885
2886     if( !RegisterClassW( &cls ) )
2887         return FALSE;
2888
2889     cls.lpfnWndProc   = MSIHiddenWindowProc;
2890     cls.lpszClassName = szMsiHiddenWindow;
2891
2892     if( !RegisterClassW( &cls ) )
2893         return FALSE;
2894
2895     uiThreadId = GetCurrentThreadId();
2896
2897     hMsiHiddenWindow = CreateWindowW( szMsiHiddenWindow, NULL, WS_OVERLAPPED,
2898                                    0, 0, 100, 100, NULL, NULL, NULL, NULL );
2899     if( !hMsiHiddenWindow )
2900         return FALSE;
2901
2902     return TRUE;
2903 }
2904
2905 void msi_dialog_unregister_class( void )
2906 {
2907     DestroyWindow( hMsiHiddenWindow );
2908     hMsiHiddenWindow = NULL;
2909     UnregisterClassW( szMsiDialogClass, NULL );
2910     UnregisterClassW( szMsiHiddenWindow, NULL );
2911     uiThreadId = 0;
2912 }