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