dialog.c 134 KB
Newer Older
1 2 3 4
/*
 * Implementation of the Microsoft Installer (msi.dll)
 *
 * Copyright 2005 Mike McCormack for CodeWeavers
5
 * Copyright 2005 Aric Stewart for CodeWeavers
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 21
 */

22
#define COBJMACROS
23
#define NONAMELESSUNION
24

25 26 27 28
#include <stdarg.h>

#include "windef.h"
#include "winbase.h"
29
#include "wingdi.h"
30 31 32 33
#include "winuser.h"
#include "winnls.h"
#include "msi.h"
#include "msipriv.h"
34
#include "msidefs.h"
35 36
#include "ocidl.h"
#include "olectl.h"
37
#include "richedit.h"
38
#include "commctrl.h"
39 40
#include "winreg.h"
#include "shlwapi.h"
41
#include "msiserver.h"
42
#include "shellapi.h"
43 44 45 46 47 48

#include "wine/debug.h"
#include "wine/unicode.h"

WINE_DEFAULT_DEBUG_CHANNEL(msi);

49 50
extern HINSTANCE msi_hInstance;

51 52
struct msi_control_tag;
typedef struct msi_control_tag msi_control;
53
typedef UINT (*msi_handler)( msi_dialog *, msi_control *, WPARAM );
54
typedef void (*msi_update)( msi_dialog *, msi_control * );
55
typedef UINT (*control_event_handler)( msi_dialog *, const WCHAR *, const WCHAR * );
56 57 58

struct msi_control_tag
{
59
    struct list entry;
60
    HWND hwnd;
61
    msi_handler handler;
62
    msi_update update;
63
    LPWSTR property;
64
    LPWSTR value;
65
    HBITMAP hBitmap;
66
    HICON hIcon;
67
    LPWSTR tabnext;
68
    LPWSTR type;
69
    HMODULE hDll;
70 71
    float progress_current;
    float progress_max;
72
    BOOL  progress_backwards;
73
    DWORD attributes;
74 75 76 77 78
    WCHAR name[1];
};

typedef struct msi_font_tag
{
79
    struct list entry;
80
    HFONT hfont;
81
    COLORREF color;
82 83
    WCHAR name[1];
} msi_font;
84

85
struct msi_dialog_tag
86 87
{
    MSIPACKAGE *package;
88
    msi_dialog *parent;
89
    control_event_handler event_handler;
90
    BOOL finished;
91
    INT scale;
92
    DWORD attributes;
93
    SIZE size;
94
    HWND hwnd;
Mike McCormack's avatar
Mike McCormack committed
95
    LPWSTR default_font;
96
    struct list fonts;
97
    struct list controls;
98
    HWND hWndFocus;
99 100
    LPWSTR control_default;
    LPWSTR control_cancel;
101 102 103
    WCHAR name[1];
};

104 105 106 107 108 109 110 111 112
struct subscriber
{
    struct list entry;
    msi_dialog *dialog;
    WCHAR      *event;
    WCHAR      *control;
    WCHAR      *attribute;
};

113
typedef UINT (*msi_dialog_control_func)( msi_dialog *dialog, MSIRECORD *rec );
114 115 116 117 118 119
struct control_handler 
{
    LPCWSTR control_type;
    msi_dialog_control_func func;
};

120 121 122 123
typedef struct
{
    msi_dialog* dialog;
    msi_control *parent;
124
    LPWSTR      propval;
125 126
} radio_button_group_descr;

127 128
static const WCHAR szMsiDialogClass[] = { 'M','s','i','D','i','a','l','o','g','C','l','o','s','e','C','l','a','s','s',0 };
static const WCHAR szMsiHiddenWindow[] = { 'M','s','i','H','i','d','d','e','n','W','i','n','d','o','w',0 };
129 130 131
static const WCHAR szStatic[] = { 'S','t','a','t','i','c',0 };
static const WCHAR szButton[] = { 'B','U','T','T','O','N', 0 };
static const WCHAR szButtonData[] = { 'M','S','I','D','A','T','A',0 };
132
static const WCHAR szProgress[] = { 'P','r','o','g','r','e','s','s',0 };
133 134 135 136 137
static const WCHAR szText[] = { 'T','e','x','t',0 };
static const WCHAR szPushButton[] = { 'P','u','s','h','B','u','t','t','o','n',0 };
static const WCHAR szLine[] = { 'L','i','n','e',0 };
static const WCHAR szBitmap[] = { 'B','i','t','m','a','p',0 };
static const WCHAR szCheckBox[] = { 'C','h','e','c','k','B','o','x',0 };
138
static const WCHAR szScrollableText[] = { 'S','c','r','o','l','l','a','b','l','e','T','e','x','t',0 };
139 140 141 142
static const WCHAR szComboBox[] = { 'C','o','m','b','o','B','o','x',0 };
static const WCHAR szEdit[] = { 'E','d','i','t',0 };
static const WCHAR szMaskedEdit[] = { 'M','a','s','k','e','d','E','d','i','t',0 };
static const WCHAR szPathEdit[] = { 'P','a','t','h','E','d','i','t',0 };
143 144 145
static const WCHAR szProgressBar[] = { 'P','r','o','g','r','e','s','s','B','a','r',0 };
static const WCHAR szSetProgress[] = { 'S','e','t','P','r','o','g','r','e','s','s',0 };
static const WCHAR szRadioButtonGroup[] = { 'R','a','d','i','o','B','u','t','t','o','n','G','r','o','u','p',0 };
146
static const WCHAR szIcon[] = { 'I','c','o','n',0 };
147
static const WCHAR szSelectionTree[] = { 'S','e','l','e','c','t','i','o','n','T','r','e','e',0 };
148
static const WCHAR szGroupBox[] = { 'G','r','o','u','p','B','o','x',0 };
149
static const WCHAR szListBox[] = { 'L','i','s','t','B','o','x',0 };
150
static const WCHAR szDirectoryCombo[] = { 'D','i','r','e','c','t','o','r','y','C','o','m','b','o',0 };
151
static const WCHAR szDirectoryList[] = { 'D','i','r','e','c','t','o','r','y','L','i','s','t',0 };
152
static const WCHAR szVolumeCostList[] = { 'V','o','l','u','m','e','C','o','s','t','L','i','s','t',0 };
153
static const WCHAR szVolumeSelectCombo[] = { 'V','o','l','u','m','e','S','e','l','e','c','t','C','o','m','b','o',0 };
154 155
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};
static const WCHAR szSelectionPath[] = {'S','e','l','e','c','t','i','o','n','P','a','t','h',0};
156
static const WCHAR szHyperLink[] = {'H','y','p','e','r','L','i','n','k',0};
157

158 159 160 161 162
/* dialog sequencing */

#define WM_MSI_DIALOG_CREATE  (WM_USER+0x100)
#define WM_MSI_DIALOG_DESTROY (WM_USER+0x101)

163 164
#define USER_INSTALLSTATE_ALL 0x1000

165 166 167
static DWORD uiThreadId;
static HWND hMsiHiddenWindow;

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
static LPWSTR msi_get_window_text( HWND hwnd )
{
    UINT sz, r;
    LPWSTR buf;

    sz = 0x20;
    buf = msi_alloc( sz*sizeof(WCHAR) );
    while ( buf )
    {
        r = GetWindowTextW( hwnd, buf, sz );
        if ( r < (sz - 1) )
            break;
        sz *= 2;
        buf = msi_realloc( buf, sz*sizeof(WCHAR) );
    }

    return buf;
}
186

187
static INT msi_dialog_scale_unit( msi_dialog *dialog, INT val )
188
{
189
    return MulDiv( val, dialog->scale, 12 );
190
}
191

192 193 194 195
static msi_control *msi_dialog_find_control( msi_dialog *dialog, LPCWSTR name )
{
    msi_control *control;

196 197
    if( !name )
        return NULL;
198 199
    if( !dialog->hwnd )
        return NULL;
200
    LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
201
        if( !strcmpW( control->name, name ) ) /* FIXME: case sensitive? */
202 203
            return control;
    return NULL;
204 205
}

206 207 208 209 210 211
static msi_control *msi_dialog_find_control_by_type( msi_dialog *dialog, LPCWSTR type )
{
    msi_control *control;

    if( !type )
        return NULL;
212 213
    if( !dialog->hwnd )
        return NULL;
214 215 216 217 218 219
    LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
        if( !strcmpW( control->type, type ) ) /* FIXME: case sensitive? */
            return control;
    return NULL;
}

220 221 222 223
static msi_control *msi_dialog_find_control_by_hwnd( msi_dialog *dialog, HWND hwnd )
{
    msi_control *control;

224 225
    if( !dialog->hwnd )
        return NULL;
226
    LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
227
        if( hwnd == control->hwnd )
228 229
            return control;
    return NULL;
230 231
}

232 233 234 235 236 237 238 239 240 241
static LPWSTR msi_get_deformatted_field( MSIPACKAGE *package, MSIRECORD *rec, int field )
{
    LPCWSTR str = MSI_RecordGetString( rec, field );
    LPWSTR ret = NULL;

    if (str)
        deformat_string( package, str, &ret );
    return ret;
}

242 243
static LPWSTR msi_dialog_dup_property( msi_dialog *dialog, LPCWSTR property, BOOL indirect )
{
244 245
    LPWSTR prop = NULL;

246 247 248 249
    if (!property)
        return NULL;

    if (indirect)
250
        prop = msi_dup_property( dialog->package->db, property );
251 252 253

    if (!prop)
        prop = strdupW( property );
254

255
    return prop;
256 257
}

258 259 260 261
/*
 * msi_dialog_get_style
 *
 * Extract the {\style} string from the front of the text to display and
262
 * update the pointer.  Only the last style in a list is applied.
263
 */
264
static LPWSTR msi_dialog_get_style( LPCWSTR p, LPCWSTR *rest )
265
{
266 267
    LPWSTR ret;
    LPCWSTR q, i, first;
268 269
    DWORD len;

270
    q = NULL;
271 272
    *rest = p;
    if( !p )
273 274 275 276 277
        return NULL;

    while ((first = strchrW( p, '{' )) && (q = strchrW( first + 1, '}' )))
    {
        p = first + 1;
278 279
        if( *p != '\\' && *p != '&' )
            return NULL;
280 281

        /* little bit of sanity checking to stop us getting confused with RTF */
282
        for( i=++p; i<q; i++ )
283 284 285 286
            if( *i == '}' || *i == '\\' )
                return NULL;
    }

287
    if (!q)
288 289
        return NULL;

290 291 292
    *rest = ++q;
    len = q - p;

293
    ret = msi_alloc( len*sizeof(WCHAR) );
294 295
    if( !ret )
        return ret;
296
    memcpy( ret, p, len*sizeof(WCHAR) );
297 298 299
    ret[len-1] = 0;
    return ret;
}
300

301 302 303 304 305 306 307 308
static UINT msi_dialog_add_font( MSIRECORD *rec, LPVOID param )
{
    msi_dialog *dialog = param;
    msi_font *font;
    LPCWSTR face, name;
    LOGFONTW lf;
    INT style;
    HDC hdc;
309

310 311
    /* create a font and add it to the list */
    name = MSI_RecordGetString( rec, 1 );
312
    font = msi_alloc( FIELD_OFFSET( msi_font, name[strlenW( name ) + 1] ));
313
    strcpyW( font->name, name );
314
    list_add_head( &dialog->fonts, &font->entry );
315

316 317
    font->color = MSI_RecordGetInteger( rec, 4 );

318 319 320 321
    memset( &lf, 0, sizeof lf );
    face = MSI_RecordGetString( rec, 2 );
    lf.lfHeight = MSI_RecordGetInteger( rec, 3 );
    style = MSI_RecordGetInteger( rec, 5 );
322
    if( style & msidbTextStyleStyleBitsBold )
323
        lf.lfWeight = FW_BOLD;
324
    if( style & msidbTextStyleStyleBitsItalic )
325
        lf.lfItalic = TRUE;
326
    if( style & msidbTextStyleStyleBitsUnderline )
327
        lf.lfUnderline = TRUE;
328
    if( style & msidbTextStyleStyleBitsStrike )
329 330 331 332 333 334 335 336 337 338
        lf.lfStrikeOut = TRUE;
    lstrcpynW( lf.lfFaceName, face, LF_FACESIZE );

    /* adjust the height */
    hdc = GetDC( dialog->hwnd );
    if (hdc)
    {
        lf.lfHeight = -MulDiv(lf.lfHeight, GetDeviceCaps(hdc, LOGPIXELSY), 72);
        ReleaseDC( dialog->hwnd, hdc );
    }
339

340 341 342
    font->hfont = CreateFontIndirectW( &lf );

    TRACE("Adding font style %s\n", debugstr_w(font->name) );
343 344 345 346

    return ERROR_SUCCESS;
}

347
static msi_font *msi_dialog_find_font( msi_dialog *dialog, LPCWSTR name )
348
{
349
    msi_font *font = NULL;
350

351
    LIST_FOR_EACH_ENTRY( font, &dialog->fonts, msi_font, entry )
352 353
        if( !strcmpW( font->name, name ) )  /* FIXME: case sensitive? */
            break;
354

355 356
    return font;
}
357

358 359 360
static UINT msi_dialog_set_font( msi_dialog *dialog, HWND hwnd, LPCWSTR name )
{
    msi_font *font;
361

362 363 364 365 366
    font = msi_dialog_find_font( dialog, name );
    if( font )
        SendMessageW( hwnd, WM_SETFONT, (WPARAM) font->hfont, TRUE );
    else
        ERR("No font entry for %s\n", debugstr_w(name));
367 368 369
    return ERROR_SUCCESS;
}

370 371 372
static UINT msi_dialog_build_font_list( msi_dialog *dialog )
{
    static const WCHAR query[] = {
373 374 375
        'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
        '`','T','e','x','t','S','t','y','l','e','`',0};
    MSIQUERY *view;
376 377 378 379 380 381 382 383 384 385 386 387 388
    UINT r;

    TRACE("dialog %p\n", dialog );

    r = MSI_OpenQuery( dialog->package->db, &view, query );
    if( r != ERROR_SUCCESS )
        return r;

    r = MSI_IterateRecords( view, NULL, msi_dialog_add_font, dialog );
    msiobj_release( &view->hdr );
    return r;
}

389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
static void msi_destroy_control( msi_control *t )
{
    list_remove( &t->entry );
    /* leave dialog->hwnd - destroying parent destroys child windows */
    msi_free( t->property );
    msi_free( t->value );
    if( t->hBitmap )
        DeleteObject( t->hBitmap );
    if( t->hIcon )
        DestroyIcon( t->hIcon );
    msi_free( t->tabnext );
    msi_free( t->type );
    if (t->hDll)
        FreeLibrary( t->hDll );
    msi_free( t );
}

406 407 408
static msi_control *dialog_create_window( msi_dialog *dialog, MSIRECORD *rec, DWORD exstyle,
                                          const WCHAR *szCls, const WCHAR *name, const WCHAR *text,
                                          DWORD style, HWND parent )
409
{
410
    DWORD x, y, width, height;
411 412
    LPWSTR font = NULL, title_font = NULL;
    LPCWSTR title = NULL;
413
    msi_control *control;
414

415
    style |= WS_CHILD;
416

417
    control = msi_alloc( FIELD_OFFSET( msi_control, name[strlenW( name ) + 1] ));
418 419 420
    if (!control)
        return NULL;

421
    strcpyW( control->name, name );
422
    list_add_tail( &dialog->controls, &control->entry );
423
    control->handler = NULL;
424
    control->update = NULL;
425
    control->property = NULL;
426
    control->value = NULL;
427
    control->hBitmap = NULL;
428
    control->hIcon = NULL;
429
    control->hDll = NULL;
430
    control->tabnext = strdupW( MSI_RecordGetString( rec, 11) );
431
    control->type = strdupW( MSI_RecordGetString( rec, 3 ) );
432 433
    control->progress_current = 0;
    control->progress_max = 100;
434
    control->progress_backwards = FALSE;
435 436 437 438 439 440

    x = MSI_RecordGetInteger( rec, 4 );
    y = MSI_RecordGetInteger( rec, 5 );
    width = MSI_RecordGetInteger( rec, 6 );
    height = MSI_RecordGetInteger( rec, 7 );

441 442 443 444 445
    x = msi_dialog_scale_unit( dialog, x );
    y = msi_dialog_scale_unit( dialog, y );
    width = msi_dialog_scale_unit( dialog, width );
    height = msi_dialog_scale_unit( dialog, height );

Mike McCormack's avatar
Mike McCormack committed
446
    if( text )
447
    {
448 449
        deformat_string( dialog->package, text, &title_font );
        font = msi_dialog_get_style( title_font, &title );
450
    }
451

452
    control->hwnd = CreateWindowExW( exstyle, szCls, title, style,
453 454 455 456 457
                          x, y, width, height, parent, NULL, NULL, NULL );

    TRACE("Dialog %s control %s hwnd %p\n",
           debugstr_w(dialog->name), debugstr_w(text), control->hwnd );

Mike McCormack's avatar
Mike McCormack committed
458 459
    msi_dialog_set_font( dialog, control->hwnd,
                         font ? font : dialog->default_font );
460

461
    msi_free( title_font );
462
    msi_free( font );
463

464 465
    return control;
}
466

467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
static LPWSTR msi_dialog_get_uitext( msi_dialog *dialog, LPCWSTR key )
{
    MSIRECORD *rec;
    LPWSTR text;

    static const WCHAR query[] = {
        's','e','l','e','c','t',' ','*',' ',
        'f','r','o','m',' ','`','U','I','T','e','x','t','`',' ',
        'w','h','e','r','e',' ','`','K','e','y','`',' ','=',' ','\'','%','s','\'',0
    };

    rec = MSI_QueryGetRecord( dialog->package->db, query, key );
    if (!rec) return NULL;
    text = strdupW( MSI_RecordGetString( rec, 2 ) );
    msiobj_release( &rec->hdr );
    return text;
}

485 486
static MSIRECORD *msi_get_binary_record( MSIDATABASE *db, LPCWSTR name )
{
487
    static const WCHAR query[] = {
488 489 490 491 492 493 494 495 496 497 498 499
        's','e','l','e','c','t',' ','*',' ',
        'f','r','o','m',' ','B','i','n','a','r','y',' ',
        'w','h','e','r','e',' ',
            '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0
    };

    return MSI_QueryGetRecord( db, query, name );
}

static HANDLE msi_load_image( MSIDATABASE *db, LPCWSTR name, UINT type,
                              UINT cx, UINT cy, UINT flags )
{
500
    MSIRECORD *rec;
501 502 503 504 505 506
    HANDLE himage = NULL;
    LPWSTR tmp;
    UINT r;

    TRACE("%p %s %u %u %08x\n", db, debugstr_w(name), cx, cy, flags);

507
    if (!(tmp = msi_create_temp_file( db ))) return NULL;
508 509 510 511 512 513 514 515 516 517 518

    rec = msi_get_binary_record( db, name );
    if( rec )
    {
        r = MSI_RecordStreamToFile( rec, 2, tmp );
        if( r == ERROR_SUCCESS )
        {
            himage = LoadImageW( 0, tmp, type, cx, cy, flags );
        }
        msiobj_release( &rec->hdr );
    }
519
    DeleteFileW( tmp );
520

521
    msi_free( tmp );
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
    return himage;
}

static HICON msi_load_icon( MSIDATABASE *db, LPCWSTR text, UINT attributes )
{
    DWORD cx = 0, cy = 0, flags;

    flags = LR_LOADFROMFILE | LR_DEFAULTSIZE;
    if( attributes & msidbControlAttributesFixedSize )
    {
        flags &= ~LR_DEFAULTSIZE;
        if( attributes & msidbControlAttributesIconSize16 )
        {
            cx += 16;
            cy += 16;
        }
        if( attributes & msidbControlAttributesIconSize32 )
        {
            cx += 32;
            cy += 32;
        }
        /* msidbControlAttributesIconSize48 handled by above logic */
    }
    return msi_load_image( db, text, IMAGE_ICON, cx, cy, flags );
}

548 549 550 551 552 553
static void msi_dialog_update_controls( msi_dialog *dialog, LPCWSTR property )
{
    msi_control *control;

    LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
    {
554
        if ( control->property && !strcmpW( control->property, property ) && control->update )
555 556 557
            control->update( dialog, control );
    }
}
558

559 560
static void msi_dialog_set_property( MSIPACKAGE *package, LPCWSTR property, LPCWSTR value )
{
561
    UINT r = msi_set_property( package->db, property, value, -1 );
562
    if (r == ERROR_SUCCESS && !strcmpW( property, szSourceDir ))
563 564 565
        msi_reset_folders( package, TRUE );
}

566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
static MSIFEATURE *msi_seltree_feature_from_item( HWND hwnd, HTREEITEM hItem )
{
    TVITEMW tvi;

    /* get the feature from the item */
    memset( &tvi, 0, sizeof tvi );
    tvi.hItem = hItem;
    tvi.mask = TVIF_PARAM | TVIF_HANDLE;
    SendMessageW( hwnd, TVM_GETITEMW, 0, (LPARAM)&tvi );
    return (MSIFEATURE *)tvi.lParam;
}

struct msi_selection_tree_info
{
    msi_dialog *dialog;
    HWND hwnd;
    WNDPROC oldproc;
    HTREEITEM selected;
};

static MSIFEATURE *msi_seltree_get_selected_feature( msi_control *control )
{
    struct msi_selection_tree_info *info = GetPropW( control->hwnd, szButtonData );
    return msi_seltree_feature_from_item( control->hwnd, info->selected );
}

592 593
static void dialog_handle_event( msi_dialog *dialog, const WCHAR *control,
                                 const WCHAR *attribute, MSIRECORD *rec )
594 595 596 597 598 599
{
    msi_control* ctrl;

    ctrl = msi_dialog_find_control( dialog, control );
    if (!ctrl)
        return;
600
    if( !strcmpW( attribute, szText ) )
601
    {
602 603 604
        const WCHAR *font_text, *text = NULL;
        WCHAR *font, *text_fmt = NULL;

605 606
        font_text = MSI_RecordGetString( rec , 1 );
        font = msi_dialog_get_style( font_text, &text );
607 608 609 610
        deformat_string( dialog->package, text, &text_fmt );
        if (text_fmt) text = text_fmt;
        else text = szEmpty;

611
        SetWindowTextW( ctrl->hwnd, text );
612

613
        msi_free( font );
614
        msi_free( text_fmt );
615 616
        msi_dialog_check_messages( NULL );
    }
617
    else if( !strcmpW( attribute, szProgress ) )
618
    {
619
        DWORD func, val1, val2, units;
620

621 622 623
        func = MSI_RecordGetInteger( rec, 1 );
        val1 = MSI_RecordGetInteger( rec, 2 );
        val2 = MSI_RecordGetInteger( rec, 3 );
624

625
        TRACE("progress: func %u val1 %u val2 %u\n", func, val1, val2);
626

627
        units = val1 / 512;
628 629 630
        switch (func)
        {
        case 0: /* init */
631 632 633
            SendMessageW( ctrl->hwnd, PBM_SETRANGE, 0, MAKELPARAM(0,100) );
            if (val2)
            {
634 635
                ctrl->progress_max = units ? units : 100;
                ctrl->progress_current = units;
636 637 638 639 640
                ctrl->progress_backwards = TRUE;
                SendMessageW( ctrl->hwnd, PBM_SETPOS, 100, 0 );
            }
            else
            {
641
                ctrl->progress_max = units ? units : 100;
642 643 644 645
                ctrl->progress_current = 0;
                ctrl->progress_backwards = FALSE;
                SendMessageW( ctrl->hwnd, PBM_SETPOS, 0, 0 );
            }
646
            break;
647 648 649
        case 1: /* action data increment */
            if (val2) dialog->package->action_progress_increment = val1;
            else dialog->package->action_progress_increment = 0;
650 651
            break;
        case 2: /* move */
652 653
            if (ctrl->progress_backwards)
            {
654
                if (units >= ctrl->progress_current) ctrl->progress_current -= units;
655 656 657 658
                else ctrl->progress_current = 0;
            }
            else
            {
659
                if (ctrl->progress_current + units < ctrl->progress_max) ctrl->progress_current += units;
660 661 662
                else ctrl->progress_current = ctrl->progress_max;
            }
            SendMessageW( ctrl->hwnd, PBM_SETPOS, MulDiv(100, ctrl->progress_current, ctrl->progress_max), 0 );
663
            break;
664 665 666
        case 3: /* add */
            ctrl->progress_max += units;
            break;
667
        default:
668
            FIXME("Unknown progress message %u\n", func);
669 670
            break;
        }
671
    }
672
    else if ( !strcmpW( attribute, szProperty ) )
673 674
    {
        MSIFEATURE *feature = msi_seltree_get_selected_feature( ctrl );
675
        if (feature) msi_dialog_set_property( dialog->package, ctrl->property, feature->Directory );
676
    }
677
    else if ( !strcmpW( attribute, szSelectionPath ) )
678
    {
679 680
        BOOL indirect = ctrl->attributes & msidbControlAttributesIndirect;
        LPWSTR path = msi_dialog_dup_property( dialog, ctrl->property, indirect );
681
        if (!path) return;
682 683 684
        SetWindowTextW( ctrl->hwnd, path );
        msi_free(path);
    }
685 686 687
    else
    {
        FIXME("Attribute %s not being set\n", debugstr_w(attribute));
688
        return;
689
    }
690 691
}

692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
static void event_subscribe( msi_dialog *dialog, const WCHAR *event, const WCHAR *control, const WCHAR *attribute )
{
    struct subscriber *sub;

    TRACE("event %s control %s attribute %s\n", debugstr_w(event), debugstr_w(control), debugstr_w(attribute));

    LIST_FOR_EACH_ENTRY( sub, &dialog->package->subscriptions, struct subscriber, entry )
    {
        if (!strcmpiW( sub->event, event ) &&
            !strcmpiW( sub->control, control ) &&
            !strcmpiW( sub->attribute, attribute ))
        {
            TRACE("already subscribed\n");
            return;
        };
    }
    if (!(sub = msi_alloc( sizeof(*sub) ))) return;
    sub->dialog    = dialog;
    sub->event     = strdupW( event );
    sub->control   = strdupW( control );
    sub->attribute = strdupW( attribute );
    list_add_tail( &dialog->package->subscriptions, &sub->entry );
}

716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
struct dialog_control
{
    msi_dialog  *dialog;
    const WCHAR *control;
};

static UINT map_event( MSIRECORD *row, void *param )
{
    struct dialog_control *dc = param;
    const WCHAR *event = MSI_RecordGetString( row, 3 );
    const WCHAR *attribute = MSI_RecordGetString( row, 4 );

    event_subscribe( dc->dialog, event, dc->control, attribute );
    return ERROR_SUCCESS;
}

732
static void dialog_map_events( msi_dialog *dialog, const WCHAR *control )
733
{
734 735
    static const WCHAR queryW[] =
        {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
736
         '`','E','v','e','n','t','M','a','p','p','i','n','g','`',' ',
737 738 739 740 741 742 743
         'W','H','E','R','E',' ','`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
         'A','N','D',' ','`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',0};
    MSIQUERY *view;
    struct dialog_control dialog_control =
    {
        dialog,
        control
744 745
    };

746 747 748 749 750
    if (!MSI_OpenQuery( dialog->package->db, &view, queryW, dialog->name, control ))
    {
        MSI_IterateRecords( view, NULL, map_event, &dialog_control );
        msiobj_release( &view->hdr );
    }
751 752
}

753 754 755 756 757
/* everything except radio buttons */
static msi_control *msi_dialog_add_control( msi_dialog *dialog,
                MSIRECORD *rec, LPCWSTR szCls, DWORD style )
{
    DWORD attributes;
758
    const WCHAR *text = NULL, *name, *control_type;
759
    DWORD exstyle = 0;
760 761

    name = MSI_RecordGetString( rec, 2 );
762
    control_type = MSI_RecordGetString( rec, 3 );
763
    attributes = MSI_RecordGetInteger( rec, 8 );
764
    if (strcmpW( control_type, szScrollableText )) text = MSI_RecordGetString( rec, 10 );
765 766 767 768

    TRACE("%s, %s, %08x, %s, %08x\n", debugstr_w(szCls), debugstr_w(name),
          attributes, debugstr_w(text), style);

769
    if( attributes & msidbControlAttributesVisible )
770
        style |= WS_VISIBLE;
771
    if( ~attributes & msidbControlAttributesEnabled )
772
        style |= WS_DISABLED;
773 774
    if( attributes & msidbControlAttributesSunken )
        exstyle |= WS_EX_CLIENTEDGE;
775

776
    dialog_map_events( dialog, name );
777

778
    return dialog_create_window( dialog, rec, exstyle, szCls, name, text, style, dialog->hwnd );
779 780
}

781 782
struct msi_text_info
{
783
    msi_font *font;
784 785 786 787
    WNDPROC oldproc;
    DWORD attributes;
};

788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
/*
 * we don't erase our own background,
 * so we have to make sure that the parent window redraws first
 */
static void msi_text_on_settext( HWND hWnd )
{
    HWND hParent;
    RECT rc;

    hParent = GetParent( hWnd );
    GetWindowRect( hWnd, &rc );
    MapWindowPoints( NULL, hParent, (LPPOINT) &rc, 2 );
    InvalidateRect( hParent, &rc, TRUE );
}

803 804 805 806
static LRESULT WINAPI
MSIText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    struct msi_text_info *info;
807
    LRESULT r = 0;
808

809
    TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
810 811 812 813 814 815 816 817 818 819 820

    info = GetPropW(hWnd, szButtonData);

    if( msg == WM_CTLCOLORSTATIC &&
       ( info->attributes & msidbControlAttributesTransparent ) )
    {
        SetBkMode( (HDC)wParam, TRANSPARENT );
        return (LRESULT) GetStockObject(NULL_BRUSH);
    }

    r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
821 822
    if ( info->font )
        SetTextColor( (HDC)wParam, info->font->color );
823 824 825

    switch( msg )
    {
826 827 828
    case WM_SETTEXT:
        msi_text_on_settext( hWnd );
        break;
829
    case WM_NCDESTROY:
830
        msi_free( info );
831 832 833 834 835 836 837
        RemovePropW( hWnd, szButtonData );
        break;
    }

    return r;
}

838 839
static UINT msi_dialog_text_control( msi_dialog *dialog, MSIRECORD *rec )
{
840 841
    msi_control *control;
    struct msi_text_info *info;
842
    LPCWSTR text, ptr, prop, control_name;
843
    LPWSTR font_name;
844

845 846
    TRACE("%p %p\n", dialog, rec);

847 848 849 850
    control = msi_dialog_add_control( dialog, rec, szStatic, SS_LEFT | WS_GROUP );
    if( !control )
        return ERROR_FUNCTION_FAILED;

851
    info = msi_alloc( sizeof *info );
852 853 854
    if( !info )
        return ERROR_SUCCESS;

855
    control_name = MSI_RecordGetString( rec, 2 );
856 857 858 859
    control->attributes = MSI_RecordGetInteger( rec, 8 );
    prop = MSI_RecordGetString( rec, 9 );
    control->property = msi_dialog_dup_property( dialog, prop, FALSE );

860 861 862 863 864
    text = MSI_RecordGetString( rec, 10 );
    font_name = msi_dialog_get_style( text, &ptr );
    info->font = ( font_name ) ? msi_dialog_find_font( dialog, font_name ) : NULL;
    msi_free( font_name );

865 866 867 868 869 870 871 872
    info->attributes = MSI_RecordGetInteger( rec, 8 );
    if( info->attributes & msidbControlAttributesTransparent )
        SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT );

    info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
                                          (LONG_PTR)MSIText_WndProc );
    SetPropW( control->hwnd, szButtonData, info );

873
    event_subscribe( dialog, szSelectionPath, control_name, szSelectionPath );
874 875 876
    return ERROR_SUCCESS;
}

877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
/* strip any leading text style label from text field */
static WCHAR *msi_get_binary_name( MSIPACKAGE *package, MSIRECORD *rec )
{
    WCHAR *p, *text;

    text = msi_get_deformatted_field( package, rec, 10 );
    if (!text)
        return NULL;

    p = text;
    while (*p && *p != '{') p++;
    if (!*p++) return text;

    while (*p && *p != '}') p++;
    if (!*p++) return text;

    p = strdupW( p );
    msi_free( text );
    return p;
}

898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
static UINT msi_dialog_set_property_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
{
    static const WCHAR szNullArg[] = {'{','}',0};
    LPWSTR p, prop, arg_fmt = NULL;
    UINT len;

    len = strlenW( event );
    prop = msi_alloc( len * sizeof(WCHAR) );
    strcpyW( prop, &event[1] );
    p = strchrW( prop, ']' );
    if (p && (p[1] == 0 || p[1] == ' '))
    {
        *p = 0;
        if (strcmpW( szNullArg, arg ))
            deformat_string( dialog->package, arg, &arg_fmt );
        msi_dialog_set_property( dialog->package, prop, arg_fmt );
        msi_dialog_update_controls( dialog, prop );
        msi_free( arg_fmt );
    }
    else ERR("Badly formatted property string - what happens?\n");
    msi_free( prop );
    return ERROR_SUCCESS;
}

static UINT msi_dialog_send_event( msi_dialog *dialog, LPCWSTR event, LPCWSTR arg )
{
    LPWSTR event_fmt = NULL, arg_fmt = NULL;

    TRACE("Sending control event %s %s\n", debugstr_w(event), debugstr_w(arg));

    deformat_string( dialog->package, event, &event_fmt );
    deformat_string( dialog->package, arg, &arg_fmt );

931
    dialog->event_handler( dialog, event_fmt, arg_fmt );
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961

    msi_free( event_fmt );
    msi_free( arg_fmt );

    return ERROR_SUCCESS;
}

static UINT msi_dialog_control_event( MSIRECORD *rec, LPVOID param )
{
    msi_dialog *dialog = param;
    LPCWSTR condition, event, arg;
    UINT r;

    condition = MSI_RecordGetString( rec, 5 );
    r = MSI_EvaluateConditionW( dialog->package, condition );
    if (r == MSICONDITION_TRUE || r == MSICONDITION_NONE)
    {
        event = MSI_RecordGetString( rec, 3 );
        arg = MSI_RecordGetString( rec, 4 );
        if (event[0] == '[')
            msi_dialog_set_property_event( dialog, event, arg );
        else
            msi_dialog_send_event( dialog, event, arg );
    }
    return ERROR_SUCCESS;
}

static UINT msi_dialog_button_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
{
    static const WCHAR query[] = {
962 963 964 965 966 967
        'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
        'C','o','n','t','r','o','l','E','v','e','n','t',' ','W','H','E','R','E',' ',
        '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ','A','N','D',' ',
        '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',' ',
        'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','i','n','g','`',0};
    MSIQUERY *view;
968 969 970 971 972 973 974 975 976
    UINT r;

    if (HIWORD(param) != BN_CLICKED)
        return ERROR_SUCCESS;

    r = MSI_OpenQuery( dialog->package->db, &view, query, dialog->name, control->name );
    if (r != ERROR_SUCCESS)
    {
        ERR("query failed\n");
977
        return ERROR_SUCCESS;
978
    }
Hans Leidekker's avatar
Hans Leidekker committed
979
    r = MSI_IterateRecords( view, 0, msi_dialog_control_event, dialog );
980 981 982 983
    msiobj_release( &view->hdr );
    return r;
}

984
static UINT msi_dialog_button_control( msi_dialog *dialog, MSIRECORD *rec )
985
{
986
    msi_control *control;
987
    UINT attributes, style;
988

989
    TRACE("%p %p\n", dialog, rec);
990

991 992 993 994 995 996 997 998 999
    style = WS_TABSTOP;
    attributes = MSI_RecordGetInteger( rec, 8 );
    if( attributes & msidbControlAttributesIcon )
        style |= BS_ICON;

    control = msi_dialog_add_control( dialog, rec, szButton, style );
    if( !control )
        return ERROR_FUNCTION_FAILED;

1000
    control->handler = msi_dialog_button_handler;
1001

1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
    if (attributes & msidbControlAttributesIcon)
    {
        /* set the icon */
        LPWSTR name = msi_get_binary_name( dialog->package, rec );
        control->hIcon = msi_load_icon( dialog->package->db, name, attributes );
        if (control->hIcon)
        {
            SendMessageW( control->hwnd, BM_SETIMAGE, IMAGE_ICON, (LPARAM) control->hIcon );
        }
        else
            ERR("Failed to load icon %s\n", debugstr_w(name));
        msi_free( name );
    }
1015

1016 1017
    return ERROR_SUCCESS;
}
1018

1019 1020
static LPWSTR msi_get_checkbox_value( msi_dialog *dialog, LPCWSTR prop )
{
1021
    static const WCHAR query[] = {
1022
        'S','E','L','E','C','T',' ','*',' ',
1023
        'F','R','O','M',' ','`','C','h','e','c','k','B','o','x','`',' ',
1024 1025 1026 1027 1028 1029 1030 1031
        'W','H','E','R','E',' ',
        '`','P','r','o','p','e','r','t','y','`',' ','=',' ',
        '\'','%','s','\'',0
    };
    MSIRECORD *rec = NULL;
    LPWSTR ret = NULL;

    /* find if there is a value associated with the checkbox */
1032
    rec = MSI_QueryGetRecord( dialog->package->db, query, prop );
1033 1034 1035
    if (!rec)
        return ret;

1036 1037
    ret = msi_get_deformatted_field( dialog->package, rec, 2 );
    if( ret && !ret[0] )
1038
    {
1039 1040
        msi_free( ret );
        ret = NULL;
1041 1042 1043 1044 1045
    }
    msiobj_release( &rec->hdr );
    if (ret)
        return ret;

1046
    ret = msi_dup_property( dialog->package->db, prop );
1047 1048
    if( ret && !ret[0] )
    {
1049
        msi_free( ret );
1050 1051 1052 1053 1054 1055
        ret = NULL;
    }

    return ret;
}

1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
static UINT msi_dialog_get_checkbox_state( msi_dialog *dialog, msi_control *control )
{
    WCHAR state[2] = {0};
    DWORD sz = 2;

    msi_get_property( dialog->package->db, control->property, state, &sz );
    return state[0] ? 1 : 0;
}

static void msi_dialog_set_checkbox_state( msi_dialog *dialog, msi_control *control, UINT state )
{
    static const WCHAR szState[] = {'1',0};
    LPCWSTR val;

    /* if uncheck then the property is set to NULL */
    if (!state)
    {
        msi_dialog_set_property( dialog->package, control->property, NULL );
        return;
    }

    /* check for a custom state */
    if (control->value && control->value[0])
        val = control->value;
    else
        val = szState;

    msi_dialog_set_property( dialog->package, control->property, val );
}

static void msi_dialog_checkbox_sync_state( msi_dialog *dialog, msi_control *control )
{
    UINT state = msi_dialog_get_checkbox_state( dialog, control );
    SendMessageW( control->hwnd, BM_SETCHECK, state ? BST_CHECKED : BST_UNCHECKED, 0 );
}

static UINT msi_dialog_checkbox_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
{
    UINT state;

    if (HIWORD(param) != BN_CLICKED)
        return ERROR_SUCCESS;

    TRACE("clicked checkbox %s, set %s\n", debugstr_w(control->name), debugstr_w(control->property));

    state = msi_dialog_get_checkbox_state( dialog, control );
    state = state ? 0 : 1;
    msi_dialog_set_checkbox_state( dialog, control, state );
    msi_dialog_checkbox_sync_state( dialog, control );

    return msi_dialog_button_handler( dialog, control, param );
}

1109 1110 1111
static UINT msi_dialog_checkbox_control( msi_dialog *dialog, MSIRECORD *rec )
{
    msi_control *control;
1112
    LPCWSTR prop;
1113 1114 1115

    TRACE("%p %p\n", dialog, rec);

1116
    control = msi_dialog_add_control( dialog, rec, szButton, BS_CHECKBOX | BS_MULTILINE | WS_TABSTOP );
1117
    control->handler = msi_dialog_checkbox_handler;
1118
    control->update = msi_dialog_checkbox_sync_state;
1119
    prop = MSI_RecordGetString( rec, 9 );
1120
    if (prop)
1121
    {
1122
        control->property = strdupW( prop );
1123
        control->value = msi_get_checkbox_value( dialog, prop );
1124
        TRACE("control %s value %s\n", debugstr_w(control->property), debugstr_w(control->value));
1125
    }
1126
    msi_dialog_checkbox_sync_state( dialog, control );
1127
    return ERROR_SUCCESS;
1128 1129
}

1130
static UINT msi_dialog_line_control( msi_dialog *dialog, MSIRECORD *rec )
1131
{
1132 1133 1134 1135 1136 1137
    DWORD attributes;
    LPCWSTR name;
    DWORD style, exstyle = 0;
    DWORD x, y, width, height;
    msi_control *control;

1138 1139
    TRACE("%p %p\n", dialog, rec);

1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
    style = WS_CHILD | SS_ETCHEDHORZ | SS_SUNKEN;

    name = MSI_RecordGetString( rec, 2 );
    attributes = MSI_RecordGetInteger( rec, 8 );

    if( attributes & msidbControlAttributesVisible )
        style |= WS_VISIBLE;
    if( ~attributes & msidbControlAttributesEnabled )
        style |= WS_DISABLED;
    if( attributes & msidbControlAttributesSunken )
        exstyle |= WS_EX_CLIENTEDGE;

1152
    dialog_map_events( dialog, name );
1153

1154
    control = msi_alloc( FIELD_OFFSET(msi_control, name[strlenW( name ) + 1] ));
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
    if (!control)
        return ERROR_OUTOFMEMORY;

    strcpyW( control->name, name );
    list_add_head( &dialog->controls, &control->entry );
    control->handler = NULL;
    control->property = NULL;
    control->value = NULL;
    control->hBitmap = NULL;
    control->hIcon = NULL;
    control->hDll = NULL;
    control->tabnext = strdupW( MSI_RecordGetString( rec, 11) );
    control->type = strdupW( MSI_RecordGetString( rec, 3 ) );
    control->progress_current = 0;
    control->progress_max = 100;
1170
    control->progress_backwards = FALSE;
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185

    x = MSI_RecordGetInteger( rec, 4 );
    y = MSI_RecordGetInteger( rec, 5 );
    width = MSI_RecordGetInteger( rec, 6 );

    x = msi_dialog_scale_unit( dialog, x );
    y = msi_dialog_scale_unit( dialog, y );
    width = msi_dialog_scale_unit( dialog, width );
    height = 2; /* line is exactly 2 units in height */

    control->hwnd = CreateWindowExW( exstyle, szStatic, NULL, style,
                          x, y, width, height, dialog->hwnd, NULL, NULL, NULL );

    TRACE("Dialog %s control %s hwnd %p\n",
           debugstr_w(dialog->name), debugstr_w(name), control->hwnd );
1186

1187 1188 1189
    return ERROR_SUCCESS;
}

1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
/******************** Scroll Text ********************************************/

struct msi_scrolltext_info
{
    msi_dialog *dialog;
    msi_control *control;
    WNDPROC oldproc;
};

static LRESULT WINAPI
MSIScrollText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    struct msi_scrolltext_info *info;
    HRESULT r;

1205
    TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1206 1207 1208 1209 1210 1211 1212

    info = GetPropW( hWnd, szButtonData );

    r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );

    switch( msg )
    {
1213 1214
    case WM_GETDLGCODE:
        return DLGC_WANTARROWS;
1215 1216 1217 1218
    case WM_NCDESTROY:
        msi_free( info );
        RemovePropW( hWnd, szButtonData );
        break;
1219 1220
    case WM_PAINT:
        /* native MSI sets a wait cursor here */
1221 1222 1223 1224 1225 1226
        msi_dialog_button_handler( info->dialog, info->control, BN_CLICKED );
        break;
    }
    return r;
}

1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
struct msi_streamin_info
{
    LPSTR string;
    DWORD offset;
    DWORD length;
};

static DWORD CALLBACK
msi_richedit_stream_in( DWORD_PTR arg, LPBYTE buffer, LONG count, LONG *pcb )
{
    struct msi_streamin_info *info = (struct msi_streamin_info*) arg;

    if( (count + info->offset) > info->length )
        count = info->length - info->offset;
    memcpy( buffer, &info->string[ info->offset ], count );
    *pcb = count;
    info->offset += count;

1245
    TRACE("%d/%d\n", info->offset, info->length);
1246 1247 1248 1249

    return 0;
}

1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
static void msi_scrolltext_add_text( msi_control *control, LPCWSTR text )
{
    struct msi_streamin_info info;
    EDITSTREAM es;

    info.string = strdupWtoA( text );
    info.offset = 0;
    info.length = lstrlenA( info.string ) + 1;

    es.dwCookie = (DWORD_PTR) &info;
    es.dwError = 0;
    es.pfnCallback = msi_richedit_stream_in;

    SendMessageW( control->hwnd, EM_STREAMIN, SF_RTF, (LPARAM) &es );

    msi_free( info.string );
}

1268 1269
static UINT msi_dialog_scrolltext_control( msi_dialog *dialog, MSIRECORD *rec )
{
1270
    static const WCHAR szRichEdit20W[] = {'R','i','c','h','E','d','i','t','2','0','W',0};
1271
    struct msi_scrolltext_info *info;
1272
    msi_control *control;
1273
    HMODULE hRichedit;
1274
    LPCWSTR text;
1275
    DWORD style;
1276

1277 1278 1279 1280
    info = msi_alloc( sizeof *info );
    if (!info)
        return ERROR_FUNCTION_FAILED;

1281
    hRichedit = LoadLibraryA("riched20");
1282

1283 1284
    style = WS_BORDER | ES_MULTILINE | WS_VSCROLL |
            ES_READONLY | ES_AUTOVSCROLL | WS_TABSTOP;
1285
    control = msi_dialog_add_control( dialog, rec, szRichEdit20W, style );
1286
    if (!control)
1287
    {
1288
        FreeLibrary( hRichedit );
1289
        msi_free( info );
1290
        return ERROR_FUNCTION_FAILED;
1291
    }
1292

1293 1294
    control->hDll = hRichedit;

1295 1296
    info->dialog = dialog;
    info->control = control;
1297

1298 1299 1300 1301
    /* subclass the static control */
    info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
                                          (LONG_PTR)MSIScrollText_WndProc );
    SetPropW( control->hwnd, szButtonData, info );
1302

1303
    /* add the text into the richedit */
1304 1305 1306
    text = MSI_RecordGetString( rec, 10 );
    if (text)
        msi_scrolltext_add_text( control, text );
1307

1308 1309
    return ERROR_SUCCESS;
}
1310

1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
static HBITMAP msi_load_picture( MSIDATABASE *db, LPCWSTR name,
                                 INT cx, INT cy, DWORD flags )
{
    HBITMAP hOleBitmap = 0, hBitmap = 0, hOldSrcBitmap, hOldDestBitmap;
    MSIRECORD *rec = NULL;
    IStream *stm = NULL;
    IPicture *pic = NULL;
    HDC srcdc, destdc;
    BITMAP bm;
    UINT r;

    rec = msi_get_binary_record( db, name );
    if( !rec )
        goto end;

    r = MSI_RecordGetIStream( rec, 2, &stm );
    msiobj_release( &rec->hdr );
    if( r != ERROR_SUCCESS )
        goto end;

    r = OleLoadPicture( stm, 0, TRUE, &IID_IPicture, (LPVOID*) &pic );
    IStream_Release( stm );
    if( FAILED( r ) )
    {
        ERR("failed to load picture\n");
        goto end;
    }

    r = IPicture_get_Handle( pic, (OLE_HANDLE*) &hOleBitmap );
    if( FAILED( r ) )
    {
        ERR("failed to get bitmap handle\n");
        goto end;
    }
 
    /* make the bitmap the desired size */
    r = GetObjectW( hOleBitmap, sizeof bm, &bm );
    if (r != sizeof bm )
    {
        ERR("failed to get bitmap size\n");
        goto end;
    }

    if (flags & LR_DEFAULTSIZE)
    {
        cx = bm.bmWidth;
        cy = bm.bmHeight;
    }

    srcdc = CreateCompatibleDC( NULL );
    hOldSrcBitmap = SelectObject( srcdc, hOleBitmap );
    destdc = CreateCompatibleDC( NULL );
    hBitmap = CreateCompatibleBitmap( srcdc, cx, cy );
    hOldDestBitmap = SelectObject( destdc, hBitmap );
    StretchBlt( destdc, 0, 0, cx, cy,
                srcdc, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
    SelectObject( srcdc, hOldSrcBitmap );
    SelectObject( destdc, hOldDestBitmap );
    DeleteDC( srcdc );
    DeleteDC( destdc );

end:
    if ( pic )
        IPicture_Release( pic );
    return hBitmap;
}

1378 1379 1380 1381
static UINT msi_dialog_bitmap_control( msi_dialog *dialog, MSIRECORD *rec )
{
    UINT cx, cy, flags, style, attributes;
    msi_control *control;
1382
    LPWSTR name;
1383 1384 1385 1386 1387 1388 1389 1390 1391

    flags = LR_LOADFROMFILE;
    style = SS_BITMAP | SS_LEFT | WS_GROUP;

    attributes = MSI_RecordGetInteger( rec, 8 );
    if( attributes & msidbControlAttributesFixedSize )
    {
        flags |= LR_DEFAULTSIZE;
        style |= SS_CENTERIMAGE;
1392
    }
1393 1394 1395 1396 1397 1398 1399

    control = msi_dialog_add_control( dialog, rec, szStatic, style );
    cx = MSI_RecordGetInteger( rec, 6 );
    cy = MSI_RecordGetInteger( rec, 7 );
    cx = msi_dialog_scale_unit( dialog, cx );
    cy = msi_dialog_scale_unit( dialog, cy );

1400 1401
    name = msi_get_binary_name( dialog->package, rec );
    control->hBitmap = msi_load_picture( dialog->package->db, name, cx, cy, flags );
1402 1403 1404 1405
    if( control->hBitmap )
        SendMessageW( control->hwnd, STM_SETIMAGE,
                      IMAGE_BITMAP, (LPARAM) control->hBitmap );
    else
1406
        ERR("Failed to load bitmap %s\n", debugstr_w(name));
1407

1408
    msi_free( name );
1409 1410
    
    return ERROR_SUCCESS;
1411 1412 1413 1414 1415
}

static UINT msi_dialog_icon_control( msi_dialog *dialog, MSIRECORD *rec )
{
    msi_control *control;
1416
    DWORD attributes;
1417
    LPWSTR name;
1418 1419 1420 1421 1422

    TRACE("\n");

    control = msi_dialog_add_control( dialog, rec, szStatic,
                            SS_ICON | SS_CENTERIMAGE | WS_GROUP );
1423
            
1424
    attributes = MSI_RecordGetInteger( rec, 8 );
1425 1426
    name = msi_get_binary_name( dialog->package, rec );
    control->hIcon = msi_load_icon( dialog->package->db, name, attributes );
1427 1428
    if( control->hIcon )
        SendMessageW( control->hwnd, STM_SETICON, (WPARAM) control->hIcon, 0 );
1429
    else
1430 1431
        ERR("Failed to load bitmap %s\n", debugstr_w(name));
    msi_free( name );
1432 1433 1434
    return ERROR_SUCCESS;
}

1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495
/******************** Combo Box ***************************************/

struct msi_combobox_info
{
    msi_dialog *dialog;
    HWND hwnd;
    WNDPROC oldproc;
    DWORD num_items;
    DWORD addpos_items;
    LPWSTR *items;
};

static LRESULT WINAPI MSIComboBox_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    struct msi_combobox_info *info;
    LRESULT r;
    DWORD j;

    TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);

    info = GetPropW( hWnd, szButtonData );
    if (!info)
        return 0;

    r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );

    switch (msg)
    {
    case WM_NCDESTROY:
        for (j = 0; j < info->num_items; j++)
            msi_free( info->items[j] );
        msi_free( info->items );
        msi_free( info );
        RemovePropW( hWnd, szButtonData );
        break;
    }

    return r;
}

static UINT msi_combobox_add_item( MSIRECORD *rec, LPVOID param )
{
    struct msi_combobox_info *info = param;
    LPCWSTR value, text;
    int pos;

    value = MSI_RecordGetString( rec, 3 );
    text = MSI_RecordGetString( rec, 4 );

    info->items[info->addpos_items] = strdupW( value );

    pos = SendMessageW( info->hwnd, CB_ADDSTRING, 0, (LPARAM)text );
    SendMessageW( info->hwnd, CB_SETITEMDATA, pos, (LPARAM)info->items[info->addpos_items] );
    info->addpos_items++;

    return ERROR_SUCCESS;
}

static UINT msi_combobox_add_items( struct msi_combobox_info *info, LPCWSTR property )
{
    static const WCHAR query[] = {
1496 1497
        'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
        '`','C','o','m','b','o','B','o','x','`',' ','W','H','E','R','E',' ',
1498
        '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',' ',
1499 1500 1501 1502
        'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','`',0};
    MSIQUERY *view;
    DWORD count;
    UINT r;
1503 1504 1505 1506 1507 1508 1509 1510

    r = MSI_OpenQuery( info->dialog->package->db, &view, query, property );
    if (r != ERROR_SUCCESS)
        return r;

    /* just get the number of records */
    count = 0;
    r = MSI_IterateRecords( view, &count, NULL, NULL );
1511 1512 1513 1514 1515
    if (r != ERROR_SUCCESS)
    {
        msiobj_release( &view->hdr );
        return r;
    }
1516 1517 1518 1519 1520 1521 1522 1523
    info->num_items = count;
    info->items = msi_alloc( sizeof(*info->items) * count );

    r = MSI_IterateRecords( view, NULL, msi_combobox_add_item, info );
    msiobj_release( &view->hdr );
    return r;
}

1524
static UINT msi_dialog_set_control_condition( MSIRECORD *rec, LPVOID param )
1525
{
1526 1527 1528 1529 1530 1531 1532 1533 1534
    static const WCHAR szHide[] = {'H','i','d','e',0};
    static const WCHAR szShow[] = {'S','h','o','w',0};
    static const WCHAR szDisable[] = {'D','i','s','a','b','l','e',0};
    static const WCHAR szEnable[] = {'E','n','a','b','l','e',0};
    static const WCHAR szDefault[] = {'D','e','f','a','u','l','t',0};
    msi_dialog *dialog = param;
    msi_control *control;
    LPCWSTR name, action, condition;
    UINT r;
1535

1536 1537 1538 1539 1540 1541 1542 1543
    name = MSI_RecordGetString( rec, 2 );
    action = MSI_RecordGetString( rec, 3 );
    condition = MSI_RecordGetString( rec, 4 );
    r = MSI_EvaluateConditionW( dialog->package, condition );
    control = msi_dialog_find_control( dialog, name );
    if (r == MSICONDITION_TRUE && control)
    {
        TRACE("%s control %s\n", debugstr_w(action), debugstr_w(name));
1544

1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
        /* FIXME: case sensitive? */
        if (!strcmpW( action, szHide ))
            ShowWindow(control->hwnd, SW_HIDE);
        else if (!strcmpW( action, szShow ))
            ShowWindow(control->hwnd, SW_SHOW);
        else if (!strcmpW( action, szDisable ))
            EnableWindow(control->hwnd, FALSE);
        else if (!strcmpW( action, szEnable ))
            EnableWindow(control->hwnd, TRUE);
        else if (!strcmpW( action, szDefault ))
            SetFocus(control->hwnd);
        else
            FIXME("Unhandled action %s\n", debugstr_w(action));
    }
1559 1560 1561
    return ERROR_SUCCESS;
}

1562
static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog )
1563
{
1564
    static const WCHAR query[] = {
1565
        'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1566
        'C','o','n','t','r','o','l','C','o','n','d','i','t','i','o','n',' ',
1567
        'W','H','E','R','E',' ','`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
1568
    UINT r;
1569
    MSIQUERY *view;
1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612
    MSIPACKAGE *package = dialog->package;

    TRACE("%p %s\n", dialog, debugstr_w(dialog->name));

    /* query the Control table for all the elements of the control */
    r = MSI_OpenQuery( package->db, &view, query, dialog->name );
    if (r != ERROR_SUCCESS)
        return ERROR_SUCCESS;

    r = MSI_IterateRecords( view, 0, msi_dialog_set_control_condition, dialog );
    msiobj_release( &view->hdr );
    return r;
}

static UINT msi_dialog_combobox_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
{
    struct msi_combobox_info *info;
    int index;
    LPWSTR value;

    if (HIWORD(param) != CBN_SELCHANGE && HIWORD(param) != CBN_EDITCHANGE)
        return ERROR_SUCCESS;

    info = GetPropW( control->hwnd, szButtonData );
    index = SendMessageW( control->hwnd, CB_GETCURSEL, 0, 0 );
    if (index == CB_ERR)
        value = msi_get_window_text( control->hwnd );
    else
        value = (LPWSTR) SendMessageW( control->hwnd, CB_GETITEMDATA, index, 0 );

    msi_dialog_set_property( info->dialog->package, control->property, value );
    msi_dialog_evaluate_control_conditions( info->dialog );

    if (index == CB_ERR)
        msi_free( value );

    return ERROR_SUCCESS;
}

static void msi_dialog_combobox_update( msi_dialog *dialog, msi_control *control )
{
    struct msi_combobox_info *info;
    LPWSTR value, tmp;
1613 1614 1615 1616
    DWORD j;

    info = GetPropW( control->hwnd, szButtonData );

1617
    value = msi_dup_property( dialog->package->db, control->property );
1618 1619 1620 1621 1622 1623 1624 1625 1626
    if (!value)
    {
        SendMessageW( control->hwnd, CB_SETCURSEL, -1, 0 );
        return;
    }

    for (j = 0; j < info->num_items; j++)
    {
        tmp = (LPWSTR) SendMessageW( control->hwnd, CB_GETITEMDATA, j, 0 );
1627
        if (!strcmpW( value, tmp ))
1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
            break;
    }

    if (j < info->num_items)
    {
        SendMessageW( control->hwnd, CB_SETCURSEL, j, 0 );
    }
    else
    {
        SendMessageW( control->hwnd, CB_SETCURSEL, -1, 0 );
        SetWindowTextW( control->hwnd, value );
    }

    msi_free(value);
}

1644 1645
static UINT msi_dialog_combo_control( msi_dialog *dialog, MSIRECORD *rec )
{
1646
    struct msi_combobox_info *info;
1647
    msi_control *control;
1648
    DWORD attributes, style;
1649 1650 1651 1652 1653
    LPCWSTR prop;

    info = msi_alloc( sizeof *info );
    if (!info)
        return ERROR_FUNCTION_FAILED;
1654

1655 1656 1657 1658 1659 1660 1661 1662 1663
    style = CBS_AUTOHSCROLL | WS_TABSTOP | WS_GROUP | WS_CHILD;
    attributes = MSI_RecordGetInteger( rec, 8 );
    if ( ~attributes & msidbControlAttributesSorted)
        style |= CBS_SORT;
    if ( attributes & msidbControlAttributesComboList)
        style |= CBS_DROPDOWNLIST;
    else
        style |= CBS_DROPDOWN;

1664 1665
    control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
    if (!control)
1666 1667
    {
        msi_free( info );
1668
        return ERROR_FUNCTION_FAILED;
1669 1670
    }

1671 1672 1673
    control->handler = msi_dialog_combobox_handler;
    control->update = msi_dialog_combobox_update;

1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687
    prop = MSI_RecordGetString( rec, 9 );
    control->property = msi_dialog_dup_property( dialog, prop, FALSE );

    /* subclass */
    info->dialog = dialog;
    info->hwnd = control->hwnd;
    info->items = NULL;
    info->addpos_items = 0;
    info->oldproc = (WNDPROC)SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
                                                (LONG_PTR)MSIComboBox_WndProc );
    SetPropW( control->hwnd, szButtonData, info );

    if (control->property)
        msi_combobox_add_items( info, control->property );
1688

1689 1690
    msi_dialog_combobox_update( dialog, control );

1691 1692 1693
    return ERROR_SUCCESS;
}

1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
static UINT msi_dialog_edit_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
{
    LPWSTR buf;

    if (HIWORD(param) != EN_CHANGE)
        return ERROR_SUCCESS;

    TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name), debugstr_w(control->property));

    buf = msi_get_window_text( control->hwnd );
    msi_dialog_set_property( dialog->package, control->property, buf );
    msi_free( buf );

    return ERROR_SUCCESS;
}

1710 1711 1712
/* length of 2^32 + 1 */
#define MAX_NUM_DIGITS 11

1713 1714 1715
static UINT msi_dialog_edit_control( msi_dialog *dialog, MSIRECORD *rec )
{
    msi_control *control;
1716 1717
    LPCWSTR prop, text;
    LPWSTR val, begin, end;
1718
    WCHAR num[MAX_NUM_DIGITS];
1719
    DWORD limit;
1720

1721
    control = msi_dialog_add_control( dialog, rec, szEdit,
1722
                                      WS_BORDER | WS_TABSTOP | ES_AUTOHSCROLL );
1723
    control->handler = msi_dialog_edit_handler;
1724 1725 1726 1727 1728 1729 1730

    text = MSI_RecordGetString( rec, 10 );
    if ( text )
    {
        begin = strchrW( text, '{' );
        end = strchrW( text, '}' );

1731 1732 1733
        if ( begin && end && end > begin &&
             begin[0] >= '0' && begin[0] <= '9' &&
             end - begin < MAX_NUM_DIGITS)
1734 1735 1736 1737 1738 1739 1740 1741
        {
            lstrcpynW( num, begin + 1, end - begin );
            limit = atolW( num );

            SendMessageW( control->hwnd, EM_SETLIMITTEXT, limit, 0 );
        }
    }

1742 1743
    prop = MSI_RecordGetString( rec, 9 );
    if( prop )
1744
        control->property = strdupW( prop );
1745

1746
    val = msi_dup_property( dialog->package->db, control->property );
1747
    SetWindowTextW( control->hwnd, val );
1748
    msi_free( val );
1749 1750 1751
    return ERROR_SUCCESS;
}

1752 1753
/******************** Masked Edit ********************************************/

1754
#define MASK_MAX_GROUPS 20
1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774

struct msi_mask_group
{
    UINT len;
    UINT ofs;
    WCHAR type;
    HWND hwnd;
};

struct msi_maskedit_info
{
    msi_dialog *dialog;
    WNDPROC oldproc;
    HWND hwnd;
    LPWSTR prop;
    UINT num_chars;
    UINT num_groups;
    struct msi_mask_group group[MASK_MAX_GROUPS];
};

1775 1776 1777 1778 1779 1780 1781 1782 1783
static BOOL msi_mask_editable( WCHAR type )
{
    switch (type)
    {
    case '%':
    case '#':
    case '&':
    case '`':
    case '?':
1784
    case '^':
1785 1786 1787 1788 1789
        return TRUE;
    }
    return FALSE;
}

1790 1791 1792 1793 1794
static void msi_mask_control_change( struct msi_maskedit_info *info )
{
    LPWSTR val;
    UINT i, n, r;

1795
    val = msi_alloc( (info->num_chars+1)*sizeof(WCHAR) );
1796 1797
    for( i=0, n=0; i<info->num_groups; i++ )
    {
1798
        if (info->group[i].len == ~0u)
1799
        {
1800 1801 1802
            UINT len = SendMessageW( info->group[i].hwnd, WM_GETTEXTLENGTH, 0, 0 );
            val = msi_realloc( val, (len + 1) * sizeof(WCHAR) );
            GetWindowTextW( info->group[i].hwnd, val, len + 1 );
1803 1804 1805
        }
        else
        {
1806 1807 1808
            if (info->group[i].len + n > info->num_chars)
            {
                ERR("can't fit control %d text into template\n",i);
1809
                break;
1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823
            }
            if (!msi_mask_editable(info->group[i].type))
            {
                for(r=0; r<info->group[i].len; r++)
                    val[n+r] = info->group[i].type;
                val[n+r] = 0;
            }
            else
            {
                r = GetWindowTextW( info->group[i].hwnd, &val[n], info->group[i].len+1 );
                if( r != info->group[i].len )
                    break;
            }
            n += r;
1824
        }
1825 1826 1827 1828 1829 1830
    }

    TRACE("%d/%d controls were good\n", i, info->num_groups);

    if( i == info->num_groups )
    {
1831 1832
        TRACE("Set property %s to %s\n", debugstr_w(info->prop), debugstr_w(val));
        msi_dialog_set_property( info->dialog->package, info->prop, val );
1833 1834
        msi_dialog_evaluate_control_conditions( info->dialog );
    }
1835
    msi_free( val );
1836 1837
}

1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859
/* now move to the next control if necessary */
static VOID msi_mask_next_control( struct msi_maskedit_info *info, HWND hWnd )
{
    HWND hWndNext;
    UINT len, i;

    for( i=0; i<info->num_groups; i++ )
        if( info->group[i].hwnd == hWnd )
            break;

    /* don't move from the last control */
    if( i >= (info->num_groups-1) )
        return;

    len = SendMessageW( hWnd, WM_GETTEXTLENGTH, 0, 0 );
    if( len < info->group[i].len )
        return;

    hWndNext = GetNextDlgTabItem( GetParent( hWnd ), hWnd, FALSE );
    SetFocus( hWndNext );
}

1860 1861 1862 1863 1864 1865
static LRESULT WINAPI
MSIMaskedEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    struct msi_maskedit_info *info;
    HRESULT r;

1866
    TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1867 1868 1869 1870 1871 1872 1873 1874 1875

    info = GetPropW(hWnd, szButtonData);

    r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);

    switch( msg )
    {
    case WM_COMMAND:
        if (HIWORD(wParam) == EN_CHANGE)
1876
        {
1877
            msi_mask_control_change( info );
1878 1879
            msi_mask_next_control( info, (HWND) lParam );
        }
1880 1881
        break;
    case WM_NCDESTROY:
1882 1883
        msi_free( info->prop );
        msi_free( info );
1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900
        RemovePropW( hWnd, szButtonData );
        break;
    }

    return r;
}

/* fish the various bits of the property out and put them in the control */
static void
msi_maskedit_set_text( struct msi_maskedit_info *info, LPCWSTR text )
{
    LPCWSTR p;
    UINT i;

    p = text;
    for( i = 0; i < info->num_groups; i++ )
    {
1901
        if( info->group[i].len < strlenW( p ) )
1902 1903 1904 1905
        {
            LPWSTR chunk = strdupW( p );
            chunk[ info->group[i].len ] = 0;
            SetWindowTextW( info->group[i].hwnd, chunk );
1906
            msi_free( chunk );
1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918
        }
        else
        {
            SetWindowTextW( info->group[i].hwnd, p );
            break;
        }
        p += info->group[i].len;
    }
}

static struct msi_maskedit_info * msi_dialog_parse_groups( LPCWSTR mask )
{
1919
    struct msi_maskedit_info *info;
1920 1921 1922 1923 1924 1925
    int i = 0, n = 0, total = 0;
    LPCWSTR p;

    TRACE("masked control, template %s\n", debugstr_w(mask));

    if( !mask )
1926
        return NULL;
1927

1928
    info = msi_alloc_zero( sizeof *info );
1929 1930 1931
    if( !info )
        return info;

1932 1933 1934 1935 1936 1937
    p = strchrW(mask, '<');
    if( p )
        p++;
    else
        p = mask;

1938 1939 1940 1941
    for( i=0; i<MASK_MAX_GROUPS; i++ )
    {
        /* stop at the end of the string */
        if( p[0] == 0 || p[0] == '>' )
1942 1943 1944 1945 1946 1947 1948 1949
        {
            if (!total)
            {
                /* create a group for the empty mask */
                info->group[0].type = '&';
                info->group[0].len = ~0u;
                i = 1;
            }
1950
            break;
1951
        }
1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967

        /* count the number of the same identifier */
        for( n=0; p[n] == p[0]; n++ )
            ;
        info->group[i].ofs = total;
        info->group[i].type = p[0];
        if( p[n] == '=' )
        {
            n++;
            total++; /* an extra not part of the group */
        }
        info->group[i].len = n;
        total += n;
        p += n;
    }

1968
    TRACE("%d characters in %d groups\n", total, i );
1969 1970 1971 1972 1973 1974 1975 1976 1977 1978
    if( i == MASK_MAX_GROUPS )
        ERR("too many groups in PIDTemplate %s\n", debugstr_w(mask));

    info->num_chars = total;
    info->num_groups = i;

    return info;
}

static void
1979
msi_maskedit_create_children( struct msi_maskedit_info *info, LPCWSTR font )
1980 1981 1982 1983 1984 1985
{
    DWORD width, height, style, wx, ww;
    RECT rect;
    HWND hwnd;
    UINT i;

1986
    style = WS_CHILD | WS_BORDER | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL;
1987 1988 1989 1990 1991 1992 1993 1994

    GetClientRect( info->hwnd, &rect );

    width = rect.right - rect.left;
    height = rect.bottom - rect.top;

    for( i = 0; i < info->num_groups; i++ )
    {
1995 1996
        if (!msi_mask_editable( info->group[i].type ))
            continue;
1997 1998 1999 2000 2001 2002 2003 2004 2005 2006
        if (info->num_chars)
        {
            wx = (info->group[i].ofs * width) / info->num_chars;
            ww = (info->group[i].len * width) / info->num_chars;
        }
        else
        {
            wx = 0;
            ww = width;
        }
2007 2008 2009 2010 2011 2012 2013 2014 2015 2016
        hwnd = CreateWindowW( szEdit, NULL, style, wx, 0, ww, height,
                              info->hwnd, NULL, NULL, NULL );
        if( !hwnd )
        {
            ERR("failed to create mask edit sub window\n");
            break;
        }

        SendMessageW( hwnd, EM_LIMITTEXT, info->group[i].len, 0 );

2017 2018
        msi_dialog_set_font( info->dialog, hwnd,
                             font?font:info->dialog->default_font );
2019 2020 2021 2022
        info->group[i].hwnd = hwnd;
    }
}

2023 2024 2025
/*
 * office 2003 uses "73931<````=````=````=````=`````>@@@@@"
 * delphi 7 uses "<????-??????-??????-????>" and "<???-???>"
2026
 * filemaker pro 7 uses "<^^^^=^^^^=^^^^=^^^^=^^^^=^^^^=^^^^^>"
2027
 */
2028 2029
static UINT msi_dialog_maskedit_control( msi_dialog *dialog, MSIRECORD *rec )
{
2030
    LPWSTR font_mask, val = NULL, font;
2031 2032 2033
    struct msi_maskedit_info *info = NULL;
    UINT ret = ERROR_SUCCESS;
    msi_control *control;
2034
    LPCWSTR prop, mask;
2035

2036 2037
    TRACE("\n");

2038 2039
    font_mask = msi_get_deformatted_field( dialog->package, rec, 10 );
    font = msi_dialog_get_style( font_mask, &mask );
2040 2041
    if( !mask )
    {
2042
        WARN("mask template is empty\n");
2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054
        goto end;
    }

    info = msi_dialog_parse_groups( mask );
    if( !info )
    {
        ERR("template %s is invalid\n", debugstr_w(mask));
        goto end;
    }

    info->dialog = dialog;

2055 2056
    control = msi_dialog_add_control( dialog, rec, szStatic,
                   SS_OWNERDRAW | WS_GROUP | WS_VISIBLE );
2057 2058 2059 2060 2061 2062
    if( !control )
    {
        ERR("Failed to create maskedit container\n");
        ret = ERROR_FUNCTION_FAILED;
        goto end;
    }
2063
    SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075

    info->hwnd = control->hwnd;

    /* subclass the static control */
    info->oldproc = (WNDPROC) SetWindowLongPtrW( info->hwnd, GWLP_WNDPROC,
                                          (LONG_PTR)MSIMaskedEdit_WndProc );
    SetPropW( control->hwnd, szButtonData, info );

    prop = MSI_RecordGetString( rec, 9 );
    if( prop )
        info->prop = strdupW( prop );

2076
    msi_maskedit_create_children( info, font );
2077 2078 2079

    if( prop )
    {
2080
        val = msi_dup_property( dialog->package->db, prop );
2081 2082 2083
        if( val )
        {
            msi_maskedit_set_text( info, val );
2084
            msi_free( val );
2085 2086 2087 2088 2089
        }
    }

end:
    if( ret != ERROR_SUCCESS )
2090
        msi_free( info );
2091 2092
    msi_free( font_mask );
    msi_free( font );
2093 2094 2095
    return ret;
}

2096 2097 2098 2099
/******************** Progress Bar *****************************************/

static UINT msi_dialog_progress_bar( msi_dialog *dialog, MSIRECORD *rec )
{
2100
    msi_control *control;
2101 2102 2103 2104 2105 2106
    DWORD attributes, style;

    style = WS_VISIBLE;
    attributes = MSI_RecordGetInteger( rec, 8 );
    if( !(attributes & msidbControlAttributesProgress95) )
        style |= PBS_SMOOTH;
2107

2108
    control = msi_dialog_add_control( dialog, rec, PROGRESS_CLASSW, style );
2109 2110 2111
    if( !control )
        return ERROR_FUNCTION_FAILED;

2112
    event_subscribe( dialog, szSetProgress, control->name, szProgress );
2113 2114 2115
    return ERROR_SUCCESS;
}

2116 2117
/******************** Path Edit ********************************************/

2118 2119 2120 2121 2122 2123 2124 2125
struct msi_pathedit_info
{
    msi_dialog *dialog;
    msi_control *control;
    WNDPROC oldproc;
};

static void msi_dialog_update_pathedit( msi_dialog *dialog, msi_control *control )
2126
{
2127
    LPWSTR prop, path;
2128
    BOOL indirect;
2129

2130 2131 2132 2133 2134
    if (!control && !(control = msi_dialog_find_control_by_type( dialog, szPathEdit )))
       return;

    indirect = control->attributes & msidbControlAttributesIndirect;
    prop = msi_dialog_dup_property( dialog, control->property, indirect );
2135
    path = msi_dialog_dup_property( dialog, prop, TRUE );
2136

2137 2138 2139 2140 2141 2142 2143 2144 2145 2146
    SetWindowTextW( control->hwnd, path );
    SendMessageW( control->hwnd, EM_SETSEL, 0, -1 );

    msi_free( path );
    msi_free( prop );
}

/* FIXME: test when this should fail */
static BOOL msi_dialog_verify_path( LPWSTR path )
{
2147
    if ( !path[0] )
2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161
        return FALSE;

    if ( PathIsRelativeW( path ) )
        return FALSE;

    return TRUE;
}

/* returns TRUE if the path is valid, FALSE otherwise */
static BOOL msi_dialog_onkillfocus( msi_dialog *dialog, msi_control *control )
{
    LPWSTR buf, prop;
    BOOL indirect;
    BOOL valid;
2162

2163 2164
    indirect = control->attributes & msidbControlAttributesIndirect;
    prop = msi_dialog_dup_property( dialog, control->property, indirect );
2165 2166

    buf = msi_get_window_text( control->hwnd );
2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177

    if ( !msi_dialog_verify_path( buf ) )
    {
        /* FIXME: display an error message box */
        ERR("Invalid path %s\n", debugstr_w( buf ));
        valid = FALSE;
        SetFocus( control->hwnd );
    }
    else
    {
        valid = TRUE;
2178
        msi_dialog_set_property( dialog->package, prop, buf );
2179 2180 2181
    }

    msi_dialog_update_pathedit( dialog, control );
2182 2183 2184 2185 2186

    TRACE("edit %s contents changed, set %s\n", debugstr_w(control->name),
          debugstr_w(prop));

    msi_free( buf );
2187
    msi_free( prop );
2188

2189
    return valid;
2190 2191
}

2192
static LRESULT WINAPI MSIPathEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2193
{
2194 2195
    struct msi_pathedit_info *info = GetPropW(hWnd, szButtonData);
    LRESULT r = 0;
2196

2197
    TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
2198

2199 2200 2201 2202 2203 2204
    if ( msg == WM_KILLFOCUS )
    {
        /* if the path is invalid, don't handle this message */
        if ( !msi_dialog_onkillfocus( info->dialog, info->control ) )
            return 0;
    }
2205

2206
    r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);
2207

2208 2209 2210 2211 2212 2213 2214
    if ( msg == WM_NCDESTROY )
    {
        msi_free( info );
        RemovePropW( hWnd, szButtonData );
    }

    return r;
2215 2216
}

2217 2218
static UINT msi_dialog_pathedit_control( msi_dialog *dialog, MSIRECORD *rec )
{
2219
    struct msi_pathedit_info *info;
2220 2221 2222
    msi_control *control;
    LPCWSTR prop;

2223 2224 2225 2226
    info = msi_alloc( sizeof *info );
    if (!info)
        return ERROR_FUNCTION_FAILED;

2227 2228 2229 2230
    control = msi_dialog_add_control( dialog, rec, szEdit,
                                      WS_BORDER | WS_TABSTOP );
    control->attributes = MSI_RecordGetInteger( rec, 8 );
    prop = MSI_RecordGetString( rec, 9 );
2231
    control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2232

2233 2234 2235 2236 2237 2238
    info->dialog = dialog;
    info->control = control;
    info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
                                                 (LONG_PTR)MSIPathEdit_WndProc );
    SetPropW( control->hwnd, szButtonData, info );

2239
    msi_dialog_update_pathedit( dialog, control );
2240 2241

    return ERROR_SUCCESS;
2242 2243
}

2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255
static UINT msi_dialog_radiogroup_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
{
    if (HIWORD(param) != BN_CLICKED)
        return ERROR_SUCCESS;

    TRACE("clicked radio button %s, set %s\n", debugstr_w(control->name), debugstr_w(control->property));

    msi_dialog_set_property( dialog->package, control->property, control->name );

    return msi_dialog_button_handler( dialog, control, param );
}

2256
/* radio buttons are a bit different from normal controls */
2257
static UINT msi_dialog_create_radiobutton( MSIRECORD *rec, LPVOID param )
2258
{
2259
    radio_button_group_descr *group = param;
2260 2261
    msi_dialog *dialog = group->dialog;
    msi_control *control;
2262
    LPCWSTR prop, text, name;
2263
    DWORD style = WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_AUTORADIOBUTTON | BS_MULTILINE;
2264 2265 2266 2267

    name = MSI_RecordGetString( rec, 3 );
    text = MSI_RecordGetString( rec, 8 );

2268 2269
    control = dialog_create_window( dialog, rec, 0, szButton, name, text, style,
                                    group->parent->hwnd );
2270 2271
    if (!control)
        return ERROR_FUNCTION_FAILED;
2272 2273
    control->handler = msi_dialog_radiogroup_handler;

2274
    if (group->propval && !strcmpW( control->name, group->propval ))
2275 2276
        SendMessageW(control->hwnd, BM_SETCHECK, BST_CHECKED, 0);

2277 2278
    prop = MSI_RecordGetString( rec, 1 );
    if( prop )
2279
        control->property = strdupW( prop );
2280 2281 2282 2283

    return ERROR_SUCCESS;
}

2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308
static BOOL CALLBACK msi_radioground_child_enum( HWND hWnd, LPARAM lParam )
{
    EnableWindow( hWnd, lParam );
    return TRUE;
}

static LRESULT WINAPI MSIRadioGroup_WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    WNDPROC oldproc = (WNDPROC)GetPropW( hWnd, szButtonData );
    LRESULT r;

    TRACE("hWnd %p msg %04x wParam 0x%08lx lParam 0x%08lx\n", hWnd, msg, wParam, lParam);

    if (msg == WM_COMMAND) /* Forward notifications to dialog */
        SendMessageW( GetParent( hWnd ), msg, wParam, lParam );

    r = CallWindowProcW( oldproc, hWnd, msg, wParam, lParam );

    /* make sure the radio buttons show as disabled if the parent is disabled */
    if (msg == WM_ENABLE)
        EnumChildWindows( hWnd, msi_radioground_child_enum, wParam );

    return r;
}

2309 2310 2311
static UINT msi_dialog_radiogroup_control( msi_dialog *dialog, MSIRECORD *rec )
{
    static const WCHAR query[] = {
2312 2313 2314
        'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
        'R','a','d','i','o','B','u','t','t','o','n',' ','W','H','E','R','E',' ',
        '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
2315 2316 2317
    UINT r;
    LPCWSTR prop;
    msi_control *control;
2318
    MSIQUERY *view;
2319 2320
    radio_button_group_descr group;
    MSIPACKAGE *package = dialog->package;
2321
    WNDPROC oldproc;
2322
    DWORD attr, style = WS_GROUP;
2323 2324 2325 2326 2327

    prop = MSI_RecordGetString( rec, 9 );

    TRACE("%p %p %s\n", dialog, rec, debugstr_w( prop ));

2328
    attr = MSI_RecordGetInteger( rec, 8 );
2329 2330 2331 2332
    if (attr & msidbControlAttributesVisible)
        style |= WS_VISIBLE;
    if (~attr & msidbControlAttributesEnabled)
        style |= WS_DISABLED;
2333 2334 2335 2336 2337
    if (attr & msidbControlAttributesHasBorder)
        style |= BS_GROUPBOX;
    else
        style |= BS_OWNERDRAW;

2338
    /* Create parent group box to hold radio buttons */
2339
    control = msi_dialog_add_control( dialog, rec, szButton, style );
2340 2341
    if( !control )
        return ERROR_FUNCTION_FAILED;
2342

2343 2344 2345
    oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
                                           (LONG_PTR)MSIRadioGroup_WndProc );
    SetPropW(control->hwnd, szButtonData, oldproc);
2346
    SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
2347 2348

    if( prop )
2349
        control->property = strdupW( prop );
2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361

    /* query the Radio Button table for all control in this group */
    r = MSI_OpenQuery( package->db, &view, query, prop );
    if( r != ERROR_SUCCESS )
    {
        ERR("query failed for dialog %s radio group %s\n", 
            debugstr_w(dialog->name), debugstr_w(prop));
        return ERROR_INVALID_PARAMETER;
    }

    group.dialog = dialog;
    group.parent = control;
2362
    group.propval = msi_dup_property( dialog->package->db, control->property );
2363 2364 2365

    r = MSI_IterateRecords( view, 0, msi_dialog_create_radiobutton, &group );
    msiobj_release( &view->hdr );
2366
    msi_free( group.propval );
2367 2368 2369
    return r;
}

2370 2371 2372 2373
static void
msi_seltree_sync_item_state( HWND hwnd, MSIFEATURE *feature, HTREEITEM hItem )
{
    TVITEMW tvi;
2374
    DWORD index = feature->ActionRequest;
2375 2376 2377 2378

    TRACE("Feature %s -> %d %d %d\n", debugstr_w(feature->Title),
        feature->Installed, feature->Action, feature->ActionRequest);

2379 2380 2381
    if (index == INSTALLSTATE_UNKNOWN)
        index = INSTALLSTATE_ABSENT;

2382 2383
    tvi.mask = TVIF_STATE;
    tvi.hItem = hItem;
2384
    tvi.state = INDEXTOSTATEIMAGEMASK( index );
2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400
    tvi.stateMask = TVIS_STATEIMAGEMASK;

    SendMessageW( hwnd, TVM_SETITEMW, 0, (LPARAM) &tvi );
}

static UINT
msi_seltree_popup_menu( HWND hwnd, INT x, INT y )
{
    HMENU hMenu;
    INT r;

    /* create a menu to display */
    hMenu = CreatePopupMenu();

    /* FIXME: load strings from resources */
    AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_LOCAL, "Install feature locally");
2401
    AppendMenuA( hMenu, MF_ENABLED, USER_INSTALLSTATE_ALL, "Install entire feature");
2402 2403 2404 2405 2406 2407 2408 2409
    AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ADVERTISED, "Install on demand");
    AppendMenuA( hMenu, MF_ENABLED, INSTALLSTATE_ABSENT, "Don't install");
    r = TrackPopupMenu( hMenu, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RETURNCMD,
                        x, y, 0, hwnd, NULL );
    DestroyMenu( hMenu );
    return r;
}

2410 2411 2412 2413
static void
msi_seltree_update_feature_installstate( HWND hwnd, HTREEITEM hItem,
        MSIPACKAGE *package, MSIFEATURE *feature, INSTALLSTATE state )
{
2414
    feature->ActionRequest = state;
2415
    msi_seltree_sync_item_state( hwnd, feature, hItem );
2416
    ACTION_UpdateComponentStates( package, feature );
2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440
}

static void
msi_seltree_update_siblings_and_children_installstate( HWND hwnd, HTREEITEM curr,
        MSIPACKAGE *package, INSTALLSTATE state)
{
    /* update all siblings */
    do
    {
        MSIFEATURE *feature;
        HTREEITEM child;

        feature = msi_seltree_feature_from_item( hwnd, curr );
        msi_seltree_update_feature_installstate( hwnd, curr, package, feature, state );

        /* update this sibling's children */
        child = (HTREEITEM)SendMessageW( hwnd, TVM_GETNEXTITEM, (WPARAM)TVGN_CHILD, (LPARAM)curr );
        if (child)
            msi_seltree_update_siblings_and_children_installstate( hwnd, child,
                    package, state );
    }
    while ((curr = (HTREEITEM)SendMessageW( hwnd, TVM_GETNEXTITEM, (WPARAM)TVGN_NEXT, (LPARAM)curr )));
}

2441 2442 2443
static LRESULT
msi_seltree_menu( HWND hwnd, HTREEITEM hItem )
{
2444
    struct msi_selection_tree_info *info;
2445
    MSIFEATURE *feature;
2446
    MSIPACKAGE *package;
2447 2448 2449 2450 2451 2452 2453
    union {
        RECT rc;
        POINT pt[2];
        HTREEITEM hItem;
    } u;
    UINT r;

2454 2455 2456
    info = GetPropW(hwnd, szButtonData);
    package = info->dialog->package;

2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472
    feature = msi_seltree_feature_from_item( hwnd, hItem );
    if (!feature)
    {
        ERR("item %p feature was NULL\n", hItem);
        return 0;
    }

    /* get the item's rectangle to put the menu just below it */
    u.hItem = hItem;
    SendMessageW( hwnd, TVM_GETITEMRECT, 0, (LPARAM) &u.rc );
    MapWindowPoints( hwnd, NULL, u.pt, 2 );

    r = msi_seltree_popup_menu( hwnd, u.rc.left, u.rc.top );

    switch (r)
    {
2473 2474 2475
    case USER_INSTALLSTATE_ALL:
        r = INSTALLSTATE_LOCAL;
        /* fall-through */
2476 2477
    case INSTALLSTATE_ADVERTISED:
    case INSTALLSTATE_ABSENT:
2478 2479 2480 2481 2482 2483 2484 2485 2486
        {
            HTREEITEM child;
            child = (HTREEITEM)SendMessageW( hwnd, TVM_GETNEXTITEM, (WPARAM)TVGN_CHILD, (LPARAM)hItem );
            if (child)
                msi_seltree_update_siblings_and_children_installstate( hwnd, child, package, r );
        }
        /* fall-through */
    case INSTALLSTATE_LOCAL:
        msi_seltree_update_feature_installstate( hwnd, hItem, package, feature, r );
2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499
        break;
    }

    return 0;
}

static LRESULT WINAPI
MSISelectionTree_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    struct msi_selection_tree_info *info;
    TVHITTESTINFO tvhti;
    HRESULT r;

2500
    TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
2501 2502 2503 2504 2505 2506

    info = GetPropW(hWnd, szButtonData);

    switch( msg )
    {
    case WM_LBUTTONDOWN:
2507 2508
        tvhti.pt.x = (short)LOWORD( lParam );
        tvhti.pt.y = (short)HIWORD( lParam );
2509 2510
        tvhti.flags = 0;
        tvhti.hItem = 0;
2511
        CallWindowProcW(info->oldproc, hWnd, TVM_HITTEST, 0, (LPARAM) &tvhti );
2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527
        if (tvhti.flags & TVHT_ONITEMSTATEICON)
            return msi_seltree_menu( hWnd, tvhti.hItem );
        break;
    }
    r = CallWindowProcW(info->oldproc, hWnd, msg, wParam, lParam);

    switch( msg )
    {
    case WM_NCDESTROY:
        msi_free( info );
        RemovePropW( hWnd, szButtonData );
        break;
    }
    return r;
}

2528
static void
2529 2530
msi_seltree_add_child_features( MSIPACKAGE *package, HWND hwnd,
                                LPCWSTR parent, HTREEITEM hParent )
2531
{
2532
    struct msi_selection_tree_info *info = GetPropW( hwnd, szButtonData );
2533 2534
    MSIFEATURE *feature;
    TVINSERTSTRUCTW tvis;
2535
    HTREEITEM hitem, hfirst = NULL;
2536 2537 2538

    LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
    {
2539 2540 2541 2542 2543
        if ( parent && feature->Feature_Parent && strcmpW( parent, feature->Feature_Parent ))
            continue;
        else if ( parent && !feature->Feature_Parent )
            continue;
        else if ( !parent && feature->Feature_Parent )
2544 2545 2546 2547 2548
            continue;

        if ( !feature->Title )
            continue;

2549 2550 2551
        if ( !feature->Display )
            continue;

2552 2553
        memset( &tvis, 0, sizeof tvis );
        tvis.hParent = hParent;
2554
        tvis.hInsertAfter = TVI_LAST;
2555 2556
        tvis.u.item.mask = TVIF_TEXT | TVIF_PARAM;
        tvis.u.item.pszText = feature->Title;
2557
        tvis.u.item.lParam = (LPARAM) feature;
2558

2559 2560 2561 2562
        hitem = (HTREEITEM) SendMessageW( hwnd, TVM_INSERTITEMW, 0, (LPARAM) &tvis );
        if (!hitem)
            continue;

2563 2564 2565
        if (!hfirst)
            hfirst = hitem;

2566 2567 2568
        msi_seltree_sync_item_state( hwnd, feature, hitem );
        msi_seltree_add_child_features( package, hwnd,
                                        feature->Feature, hitem );
2569 2570 2571 2572

        /* the node is expanded if Display is odd */
        if ( feature->Display % 2 != 0 )
            SendMessageW( hwnd, TVM_EXPAND, TVE_EXPAND, (LPARAM) hitem );
2573
    }
2574 2575 2576

    /* select the first item */
    SendMessageW( hwnd, TVM_SELECTITEM, TVGN_CARET | TVGN_DROPHILITE, (LPARAM) hfirst );
2577
    info->selected = hfirst;
2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611
}

static void msi_seltree_create_imagelist( HWND hwnd )
{
    const int bm_width = 32, bm_height = 16, bm_count = 3;
    const int bm_resource = 0x1001;
    HIMAGELIST himl;
    int i;
    HBITMAP hbmp;

    himl = ImageList_Create( bm_width, bm_height, FALSE, 4, 0 );
    if (!himl)
    {
        ERR("failed to create image list\n");
        return;
    }

    for (i=0; i<bm_count; i++)
    {
        hbmp = LoadBitmapW( msi_hInstance, MAKEINTRESOURCEW(i+bm_resource) );
        if (!hbmp)
        {
            ERR("failed to load bitmap %d\n", i);
            break;
        }

        /*
         * Add a dummy bitmap at offset zero because the treeview
         * can't use it as a state mask (zero means no user state).
         */
        if (!i)
            ImageList_Add( himl, hbmp, NULL );

        ImageList_Add( himl, hbmp, NULL );
2612
    }
2613 2614

    SendMessageW( hwnd, TVM_SETIMAGELIST, TVSIL_STATE, (LPARAM)himl );
2615 2616
}

2617 2618 2619
static UINT msi_dialog_seltree_handler( msi_dialog *dialog,
                                        msi_control *control, WPARAM param )
{
2620
    struct msi_selection_tree_info *info = GetPropW( control->hwnd, szButtonData );
2621 2622 2623
    LPNMTREEVIEWW tv = (LPNMTREEVIEWW)param;
    MSIRECORD *row, *rec;
    MSIFOLDER *folder;
2624 2625
    MSIFEATURE *feature;
    LPCWSTR dir, title = NULL;
2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636
    UINT r = ERROR_SUCCESS;

    static const WCHAR select[] = {
        'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
        '`','F','e','a','t','u','r','e','`',' ','W','H','E','R','E',' ',
        '`','T','i','t','l','e','`',' ','=',' ','\'','%','s','\'',0
    };

    if (tv->hdr.code != TVN_SELCHANGINGW)
        return ERROR_SUCCESS;

2637 2638
    info->selected = tv->itemNew.hItem;

2639 2640 2641 2642 2643 2644 2645 2646 2647 2648
    if (!(tv->itemNew.mask & TVIF_TEXT))
    {
        feature = msi_seltree_feature_from_item( control->hwnd, tv->itemNew.hItem );
        if (feature)
            title = feature->Title;
    }
    else
        title = tv->itemNew.pszText;

    row = MSI_QueryGetRecord( dialog->package->db, select, title );
2649 2650 2651 2652 2653 2654
    if (!row)
        return ERROR_FUNCTION_FAILED;

    rec = MSI_CreateRecord( 1 );

    MSI_RecordSetStringW( rec, 1, MSI_RecordGetString( row, 4 ) );
2655
    msi_event_fire( dialog->package, szSelectionDescription, rec );
2656 2657

    dir = MSI_RecordGetString( row, 7 );
2658
    if (dir)
2659
    {
2660
        folder = msi_get_loaded_folder( dialog->package, dir );
2661 2662 2663 2664 2665 2666
        if (!folder)
        {
            r = ERROR_FUNCTION_FAILED;
            goto done;
        }
        MSI_RecordSetStringW( rec, 1, folder->ResolvedTarget );
2667
    }
2668 2669
    else
        MSI_RecordSetStringW( rec, 1, NULL );
2670

2671
    msi_event_fire( dialog->package, szSelectionPath, rec );
2672 2673 2674 2675 2676 2677 2678 2679

done:
    msiobj_release(&row->hdr);
    msiobj_release(&rec->hdr);

    return r;
}

2680 2681 2682
static UINT msi_dialog_selection_tree( msi_dialog *dialog, MSIRECORD *rec )
{
    msi_control *control;
2683
    LPCWSTR prop, control_name;
2684
    MSIPACKAGE *package = dialog->package;
2685 2686 2687 2688 2689 2690
    DWORD style;
    struct msi_selection_tree_info *info;

    info = msi_alloc( sizeof *info );
    if (!info)
        return ERROR_FUNCTION_FAILED;
2691

2692 2693 2694 2695
    /* create the treeview control */
    style = TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT;
    style |= WS_GROUP | WS_VSCROLL;
    control = msi_dialog_add_control( dialog, rec, WC_TREEVIEWW, style );
2696
    if (!control)
2697 2698
    {
        msi_free(info);
2699
        return ERROR_FUNCTION_FAILED;
2700
    }
2701

2702
    control->handler = msi_dialog_seltree_handler;
2703
    control_name = MSI_RecordGetString( rec, 2 );
2704 2705 2706 2707
    control->attributes = MSI_RecordGetInteger( rec, 8 );
    prop = MSI_RecordGetString( rec, 9 );
    control->property = msi_dialog_dup_property( dialog, prop, FALSE );

2708 2709 2710 2711 2712 2713
    /* subclass */
    info->dialog = dialog;
    info->hwnd = control->hwnd;
    info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
                                          (LONG_PTR)MSISelectionTree_WndProc );
    SetPropW( control->hwnd, szButtonData, info );
2714

2715
    event_subscribe( dialog, szSelectionPath, control_name, szProperty );
2716

2717 2718 2719
    /* initialize it */
    msi_seltree_create_imagelist( control->hwnd );
    msi_seltree_add_child_features( package, control->hwnd, NULL, NULL );
2720 2721 2722 2723

    return ERROR_SUCCESS;
}

2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738
/******************** Group Box ***************************************/

static UINT msi_dialog_group_box( msi_dialog *dialog, MSIRECORD *rec )
{
    msi_control *control;
    DWORD style;

    style = BS_GROUPBOX | WS_CHILD | WS_GROUP;
    control = msi_dialog_add_control( dialog, rec, WC_BUTTONW, style );
    if (!control)
        return ERROR_FUNCTION_FAILED;

    return ERROR_SUCCESS;
}

2739 2740 2741 2742 2743 2744 2745 2746
/******************** List Box ***************************************/

struct msi_listbox_info
{
    msi_dialog *dialog;
    HWND hwnd;
    WNDPROC oldproc;
    DWORD num_items;
2747
    DWORD addpos_items;
2748
    LPWSTR *items;
2749 2750 2751 2752 2753 2754 2755 2756
};

static LRESULT WINAPI MSIListBox_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    struct msi_listbox_info *info;
    LRESULT r;
    DWORD j;

2757
    TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768

    info = GetPropW( hWnd, szButtonData );
    if (!info)
        return 0;

    r = CallWindowProcW( info->oldproc, hWnd, msg, wParam, lParam );

    switch( msg )
    {
    case WM_NCDESTROY:
        for (j = 0; j < info->num_items; j++)
2769
            msi_free( info->items[j] );
2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781
        msi_free( info->items );
        msi_free( info );
        RemovePropW( hWnd, szButtonData );
        break;
    }

    return r;
}

static UINT msi_listbox_add_item( MSIRECORD *rec, LPVOID param )
{
    struct msi_listbox_info *info = param;
2782
    LPCWSTR value, text;
2783
    int pos;
2784 2785 2786 2787

    value = MSI_RecordGetString( rec, 3 );
    text = MSI_RecordGetString( rec, 4 );

2788
    info->items[info->addpos_items] = strdupW( value );
2789

2790
    pos = SendMessageW( info->hwnd, LB_ADDSTRING, 0, (LPARAM)text );
2791 2792
    SendMessageW( info->hwnd, LB_SETITEMDATA, pos, (LPARAM)info->items[info->addpos_items] );
    info->addpos_items++;
2793 2794 2795
    return ERROR_SUCCESS;
}

2796
static UINT msi_listbox_add_items( struct msi_listbox_info *info, LPCWSTR property )
2797 2798
{
    static const WCHAR query[] = {
2799 2800
        'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
        '`','L','i','s','t','B','o','x','`',' ','W','H','E','R','E',' ',
2801
        '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',' ',
2802 2803 2804 2805
        'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','`',0};
    MSIQUERY *view;
    DWORD count;
    UINT r;
2806

2807
    r = MSI_OpenQuery( info->dialog->package->db, &view, query, property );
2808 2809 2810 2811
    if ( r != ERROR_SUCCESS )
        return r;

    /* just get the number of records */
2812
    count = 0;
2813
    r = MSI_IterateRecords( view, &count, NULL, NULL );
2814 2815 2816 2817 2818
    if (r != ERROR_SUCCESS)
    {
        msiobj_release( &view->hdr );
        return r;
    }
2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831
    info->num_items = count;
    info->items = msi_alloc( sizeof(*info->items) * count );

    r = MSI_IterateRecords( view, NULL, msi_listbox_add_item, info );
    msiobj_release( &view->hdr );
    return r;
}

static UINT msi_dialog_listbox_handler( msi_dialog *dialog,
                                        msi_control *control, WPARAM param )
{
    struct msi_listbox_info *info;
    int index;
2832
    LPCWSTR value;
2833 2834 2835 2836 2837 2838

    if( HIWORD(param) != LBN_SELCHANGE )
        return ERROR_SUCCESS;

    info = GetPropW( control->hwnd, szButtonData );
    index = SendMessageW( control->hwnd, LB_GETCURSEL, 0, 0 );
2839
    value = (LPCWSTR) SendMessageW( control->hwnd, LB_GETITEMDATA, index, 0 );
2840

2841
    msi_dialog_set_property( info->dialog->package, control->property, value );
2842 2843 2844 2845 2846 2847 2848 2849 2850
    msi_dialog_evaluate_control_conditions( info->dialog );

    return ERROR_SUCCESS;
}

static UINT msi_dialog_list_box( msi_dialog *dialog, MSIRECORD *rec )
{
    struct msi_listbox_info *info;
    msi_control *control;
2851
    DWORD attributes, style;
2852
    LPCWSTR prop;
2853 2854 2855 2856 2857

    info = msi_alloc( sizeof *info );
    if (!info)
        return ERROR_FUNCTION_FAILED;

2858 2859 2860 2861 2862
    style = WS_TABSTOP | WS_GROUP | WS_CHILD | LBS_NOTIFY | WS_VSCROLL | WS_BORDER;
    attributes = MSI_RecordGetInteger( rec, 8 );
    if (~attributes & msidbControlAttributesSorted)
        style |= LBS_SORT;

2863 2864
    control = msi_dialog_add_control( dialog, rec, WC_LISTBOXW, style );
    if (!control)
2865 2866
    {
        msi_free(info);
2867
        return ERROR_FUNCTION_FAILED;
2868
    }
2869 2870 2871

    control->handler = msi_dialog_listbox_handler;

2872 2873 2874
    prop = MSI_RecordGetString( rec, 9 );
    control->property = msi_dialog_dup_property( dialog, prop, FALSE );

2875 2876 2877 2878
    /* subclass */
    info->dialog = dialog;
    info->hwnd = control->hwnd;
    info->items = NULL;
2879
    info->addpos_items = 0;
2880 2881 2882 2883
    info->oldproc = (WNDPROC)SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
                                                (LONG_PTR)MSIListBox_WndProc );
    SetPropW( control->hwnd, szButtonData, info );

2884 2885
    if ( control->property )
        msi_listbox_add_items( info, control->property );
2886 2887 2888 2889

    return ERROR_SUCCESS;
}

2890 2891
/******************** Directory Combo ***************************************/

2892
static void msi_dialog_update_directory_combo( msi_dialog *dialog, msi_control *control )
2893 2894 2895 2896
{
    LPWSTR prop, path;
    BOOL indirect;

2897
    if (!control && !(control = msi_dialog_find_control_by_type( dialog, szDirectoryCombo )))
2898 2899
        return;

2900 2901
    indirect = control->attributes & msidbControlAttributesIndirect;
    prop = msi_dialog_dup_property( dialog, control->property, indirect );
2902
    path = msi_dialog_dup_property( dialog, prop, TRUE );
2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913

    PathStripPathW( path );
    PathRemoveBackslashW( path );

    SendMessageW( control->hwnd, CB_INSERTSTRING, 0, (LPARAM)path );
    SendMessageW( control->hwnd, CB_SETCURSEL, 0, 0 );

    msi_free( path );
    msi_free( prop );
}

2914 2915 2916
static UINT msi_dialog_directory_combo( msi_dialog *dialog, MSIRECORD *rec )
{
    msi_control *control;
2917
    LPCWSTR prop;
2918 2919
    DWORD style;

2920 2921 2922
    /* FIXME: use CBS_OWNERDRAWFIXED and add owner draw code */
    style = CBS_DROPDOWNLIST | CBS_HASSTRINGS | WS_CHILD |
            WS_GROUP | WS_TABSTOP | WS_VSCROLL;
2923 2924 2925 2926
    control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
    if (!control)
        return ERROR_FUNCTION_FAILED;

2927 2928 2929 2930
    control->attributes = MSI_RecordGetInteger( rec, 8 );
    prop = MSI_RecordGetString( rec, 9 );
    control->property = msi_dialog_dup_property( dialog, prop, FALSE );

2931
    msi_dialog_update_directory_combo( dialog, control );
2932

2933 2934 2935
    return ERROR_SUCCESS;
}

2936 2937
/******************** Directory List ***************************************/

2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956
static void msi_dialog_update_directory_list( msi_dialog *dialog, msi_control *control )
{
    WCHAR dir_spec[MAX_PATH];
    WIN32_FIND_DATAW wfd;
    LPWSTR prop, path;
    BOOL indirect;
    LVITEMW item;
    HANDLE file;

    static const WCHAR asterisk[] = {'*',0};

    if (!control && !(control = msi_dialog_find_control_by_type( dialog, szDirectoryList )))
        return;

    /* clear the list-view */
    SendMessageW( control->hwnd, LVM_DELETEALLITEMS, 0, 0 );

    indirect = control->attributes & msidbControlAttributesIndirect;
    prop = msi_dialog_dup_property( dialog, control->property, indirect );
2957
    path = msi_dialog_dup_property( dialog, prop, TRUE );
2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970

    lstrcpyW( dir_spec, path );
    lstrcatW( dir_spec, asterisk );

    file = FindFirstFileW( dir_spec, &wfd );
    if ( file == INVALID_HANDLE_VALUE )
        return;

    do
    {
        if ( wfd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY )
            continue;

2971
        if ( !strcmpW( wfd.cFileName, szDot ) || !strcmpW( wfd.cFileName, szDotDot ) )
2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987
            continue;

        item.mask = LVIF_TEXT;
        item.cchTextMax = MAX_PATH;
        item.iItem = 0;
        item.iSubItem = 0;
        item.pszText = wfd.cFileName;

        SendMessageW( control->hwnd, LVM_INSERTITEMW, 0, (LPARAM)&item );
    } while ( FindNextFileW( file, &wfd ) );

    msi_free( prop );
    msi_free( path );
    FindClose( file );
}

2988
static UINT msi_dialog_directorylist_up( msi_dialog *dialog )
2989 2990 2991 2992 2993
{
    msi_control *control;
    LPWSTR prop, path, ptr;
    BOOL indirect;

2994
    control = msi_dialog_find_control_by_type( dialog, szDirectoryList );
2995 2996
    indirect = control->attributes & msidbControlAttributesIndirect;
    prop = msi_dialog_dup_property( dialog, control->property, indirect );
2997
    path = msi_dialog_dup_property( dialog, prop, TRUE );
2998 2999 3000 3001 3002 3003

    /* strip off the last directory */
    ptr = PathFindFileNameW( path );
    if (ptr != path) *(ptr - 1) = '\0';
    PathAddBackslashW( path );

3004
    msi_dialog_set_property( dialog->package, prop, path );
3005

3006
    msi_dialog_update_directory_list( dialog, NULL );
3007 3008
    msi_dialog_update_directory_combo( dialog, NULL );
    msi_dialog_update_pathedit( dialog, NULL );
3009

3010 3011 3012 3013 3014 3015
    msi_free( path );
    msi_free( prop );

    return ERROR_SUCCESS;
}

3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043
static UINT msi_dialog_dirlist_handler( msi_dialog *dialog,
                                        msi_control *control, WPARAM param )
{
    LPNMHDR nmhdr = (LPNMHDR)param;
    WCHAR new_path[MAX_PATH];
    WCHAR text[MAX_PATH];
    LPWSTR path, prop;
    BOOL indirect;
    LVITEMW item;
    int index;

    if (nmhdr->code != LVN_ITEMACTIVATE)
        return ERROR_SUCCESS;

    index = SendMessageW( control->hwnd, LVM_GETNEXTITEM, -1, LVNI_SELECTED );
    if ( index < 0 )
    {
        ERR("No list-view item selected!\n");
        return ERROR_FUNCTION_FAILED;
    }

    item.iSubItem = 0;
    item.pszText = text;
    item.cchTextMax = MAX_PATH;
    SendMessageW( control->hwnd, LVM_GETITEMTEXTW, index, (LPARAM)&item );

    indirect = control->attributes & msidbControlAttributesIndirect;
    prop = msi_dialog_dup_property( dialog, control->property, indirect );
3044
    path = msi_dialog_dup_property( dialog, prop, TRUE );
3045 3046 3047

    lstrcpyW( new_path, path );
    lstrcatW( new_path, text );
3048
    lstrcatW( new_path, szBackSlash );
3049

3050
    msi_dialog_set_property( dialog->package, prop, new_path );
3051 3052 3053 3054 3055 3056 3057 3058 3059 3060

    msi_dialog_update_directory_list( dialog, NULL );
    msi_dialog_update_directory_combo( dialog, NULL );
    msi_dialog_update_pathedit( dialog, NULL );

    msi_free( prop );
    msi_free( path );
    return ERROR_SUCCESS;
}

3061 3062 3063
static UINT msi_dialog_directory_list( msi_dialog *dialog, MSIRECORD *rec )
{
    msi_control *control;
3064
    LPCWSTR prop;
3065 3066
    DWORD style;

3067
    style = LVS_LIST | WS_VSCROLL | LVS_SHAREIMAGELISTS |
3068 3069 3070 3071 3072 3073
            LVS_AUTOARRANGE | LVS_SINGLESEL | WS_BORDER |
            LVS_SORTASCENDING | WS_CHILD | WS_GROUP | WS_TABSTOP;
    control = msi_dialog_add_control( dialog, rec, WC_LISTVIEWW, style );
    if (!control)
        return ERROR_FUNCTION_FAILED;

3074
    control->attributes = MSI_RecordGetInteger( rec, 8 );
3075
    control->handler = msi_dialog_dirlist_handler;
3076 3077 3078
    prop = MSI_RecordGetString( rec, 9 );
    control->property = msi_dialog_dup_property( dialog, prop, FALSE );

3079 3080 3081 3082
    /* double click to activate an item in the list */
    SendMessageW( control->hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE,
                  0, LVS_EX_TWOCLICKACTIVATE );

3083 3084
    msi_dialog_update_directory_list( dialog, control );

3085 3086 3087
    return ERROR_SUCCESS;
}

3088 3089
/******************** VolumeCost List ***************************************/

3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100
static BOOL str_is_number( LPCWSTR str )
{
    int i;

    for (i = 0; i < lstrlenW( str ); i++)
        if (!isdigitW(str[i]))
            return FALSE;

    return TRUE;
}

3101
static const WCHAR column_keys[][80] =
3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113
{
    {'V','o','l','u','m','e','C','o','s','t','V','o','l','u','m','e',0},
    {'V','o','l','u','m','e','C','o','s','t','S','i','z','e',0},
    {'V','o','l','u','m','e','C','o','s','t','A','v','a','i','l','a','b','l','e',0},
    {'V','o','l','u','m','e','C','o','s','t','R','e','q','u','i','r','e','d',0},
    {'V','o','l','u','m','e','C','o','s','t','D','i','f','f','e','r','e','n','c','e',0}
};

static void msi_dialog_vcl_add_columns( msi_dialog *dialog, msi_control *control, MSIRECORD *rec )
{
    LPCWSTR text = MSI_RecordGetString( rec, 10 );
    LPCWSTR begin = text, end;
3114
    WCHAR *num;
3115 3116 3117 3118 3119
    LVCOLUMNW lvc;
    DWORD count = 0;

    static const WCHAR negative[] = {'-',0};

3120 3121
    if (!text) return;

3122 3123 3124 3125 3126
    while ((begin = strchrW( begin, '{' )) && count < 5)
    {
        if (!(end = strchrW( begin, '}' )))
            return;

3127 3128 3129 3130
        num = msi_alloc( (end-begin+1)*sizeof(WCHAR) );
        if (!num)
            return;

3131 3132 3133 3134
        lstrcpynW( num, begin + 1, end - begin );
        begin += end - begin + 1;

        /* empty braces or '0' hides the column */ 
3135
        if ( !num[0] || !strcmpW( num, szZero ) )
3136 3137
        {
            count++;
3138
            msi_free( num );
3139 3140 3141 3142 3143 3144
            continue;
        }

        /* the width must be a positive number
         * if a width is invalid, all remaining columns are hidden
         */
3145 3146
        if ( !strncmpW( num, negative, 1 ) || !str_is_number( num ) ) {
            msi_free( num );
3147
            return;
3148
        }
3149

3150 3151
        ZeroMemory( &lvc, sizeof(lvc) );
        lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
3152
        lvc.cx = atolW( num );
3153
        lvc.pszText = msi_dialog_get_uitext( dialog, column_keys[count] );
3154

3155
        SendMessageW( control->hwnd,  LVM_INSERTCOLUMNW, count++, (LPARAM)&lvc );
3156
        msi_free( lvc.pszText );
3157
        msi_free( num );
3158 3159 3160
    }
}

3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184
static LONGLONG msi_vcl_get_cost( msi_dialog *dialog )
{
    MSIFEATURE *feature;
    INT each_cost;
    LONGLONG total_cost = 0;

    LIST_FOR_EACH_ENTRY( feature, &dialog->package->features, MSIFEATURE, entry )
    {
        if (ERROR_SUCCESS == (MSI_GetFeatureCost(dialog->package, feature,
                MSICOSTTREE_SELFONLY, INSTALLSTATE_LOCAL, &each_cost)))
        {
            /* each_cost is in 512-byte units */
            total_cost += each_cost * 512;
        }
        if (ERROR_SUCCESS == (MSI_GetFeatureCost(dialog->package, feature,
                MSICOSTTREE_SELFONLY, INSTALLSTATE_ABSENT, &each_cost)))
        {
            /* each_cost is in 512-byte units */
            total_cost -= each_cost * 512;
        }
    }
    return total_cost;
}

3185 3186
static void msi_dialog_vcl_add_drives( msi_dialog *dialog, msi_control *control )
{
3187
    ULARGE_INTEGER total, free;
3188
    LONGLONG difference, cost;
3189
    WCHAR size_text[MAX_PATH];
3190
    WCHAR cost_text[MAX_PATH];
3191 3192 3193 3194 3195
    LPWSTR drives, ptr;
    LVITEMW lvitem;
    DWORD size;
    int i = 0;

3196 3197 3198
    cost = msi_vcl_get_cost(dialog);
    StrFormatByteSizeW(cost, cost_text, MAX_PATH);

3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216
    size = GetLogicalDriveStringsW( 0, NULL );
    if ( !size ) return;

    drives = msi_alloc( (size + 1) * sizeof(WCHAR) );
    if ( !drives ) return;

    GetLogicalDriveStringsW( size, drives );

    ptr = drives;
    while (*ptr)
    {
        lvitem.mask = LVIF_TEXT;
        lvitem.iItem = i;
        lvitem.iSubItem = 0;
        lvitem.pszText = ptr;
        lvitem.cchTextMax = lstrlenW(ptr) + 1;
        SendMessageW( control->hwnd, LVM_INSERTITEMW, 0, (LPARAM)&lvitem );

3217
        GetDiskFreeSpaceExW(ptr, &free, &total, NULL);
3218
        difference = free.QuadPart - cost;
3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231

        StrFormatByteSizeW(total.QuadPart, size_text, MAX_PATH);
        lvitem.iSubItem = 1;
        lvitem.pszText = size_text;
        lvitem.cchTextMax = lstrlenW(size_text) + 1;
        SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );

        StrFormatByteSizeW(free.QuadPart, size_text, MAX_PATH);
        lvitem.iSubItem = 2;
        lvitem.pszText = size_text;
        lvitem.cchTextMax = lstrlenW(size_text) + 1;
        SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );

3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242
        lvitem.iSubItem = 3;
        lvitem.pszText = cost_text;
        lvitem.cchTextMax = lstrlenW(cost_text) + 1;
        SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );

        StrFormatByteSizeW(difference, size_text, MAX_PATH);
        lvitem.iSubItem = 4;
        lvitem.pszText = size_text;
        lvitem.cchTextMax = lstrlenW(size_text) + 1;
        SendMessageW( control->hwnd, LVM_SETITEMW, 0, (LPARAM)&lvitem );

3243 3244 3245 3246 3247 3248 3249
        ptr += lstrlenW(ptr) + 1;
        i++;
    }

    msi_free( drives );
}

3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261
static UINT msi_dialog_volumecost_list( msi_dialog *dialog, MSIRECORD *rec )
{
    msi_control *control;
    DWORD style;

    style = LVS_REPORT | WS_VSCROLL | WS_HSCROLL | LVS_SHAREIMAGELISTS |
            LVS_AUTOARRANGE | LVS_SINGLESEL | WS_BORDER |
            WS_CHILD | WS_TABSTOP | WS_GROUP;
    control = msi_dialog_add_control( dialog, rec, WC_LISTVIEWW, style );
    if (!control)
        return ERROR_FUNCTION_FAILED;

3262
    msi_dialog_vcl_add_columns( dialog, control, rec );
3263
    msi_dialog_vcl_add_drives( dialog, control );
3264

3265 3266 3267
    return ERROR_SUCCESS;
}

3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292
/******************** VolumeSelect Combo ***************************************/

static UINT msi_dialog_volsel_handler( msi_dialog *dialog,
                                       msi_control *control, WPARAM param )
{
    WCHAR text[MAX_PATH];
    LPWSTR prop;
    BOOL indirect;
    int index;

    if (HIWORD(param) != CBN_SELCHANGE)
        return ERROR_SUCCESS;

    index = SendMessageW( control->hwnd, CB_GETCURSEL, 0, 0 );
    if ( index == CB_ERR )
    {
        ERR("No ComboBox item selected!\n");
        return ERROR_FUNCTION_FAILED;
    }

    SendMessageW( control->hwnd, CB_GETLBTEXT, index, (LPARAM)text );

    indirect = control->attributes & msidbControlAttributesIndirect;
    prop = msi_dialog_dup_property( dialog, control->property, indirect );

3293
    msi_dialog_set_property( dialog->package, prop, text );
3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345

    msi_free( prop );
    return ERROR_SUCCESS;
}

static void msi_dialog_vsc_add_drives( msi_dialog *dialog, msi_control *control )
{
    LPWSTR drives, ptr;
    DWORD size;

    size = GetLogicalDriveStringsW( 0, NULL );
    if ( !size ) return;

    drives = msi_alloc( (size + 1) * sizeof(WCHAR) );
    if ( !drives ) return;

    GetLogicalDriveStringsW( size, drives );

    ptr = drives;
    while (*ptr)
    {
        SendMessageW( control->hwnd, CB_ADDSTRING, 0, (LPARAM)ptr );
        ptr += lstrlenW(ptr) + 1;
    }

    msi_free( drives );
}

static UINT msi_dialog_volumeselect_combo( msi_dialog *dialog, MSIRECORD *rec )
{
    msi_control *control;
    LPCWSTR prop;
    DWORD style;

    /* FIXME: CBS_OWNERDRAWFIXED */
    style = WS_CHILD | WS_VISIBLE | WS_GROUP | WS_TABSTOP |
            CBS_DROPDOWNLIST | CBS_SORT | CBS_HASSTRINGS |
            WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
    control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
    if (!control)
        return ERROR_FUNCTION_FAILED;

    control->attributes = MSI_RecordGetInteger( rec, 8 );
    control->handler = msi_dialog_volsel_handler;
    prop = MSI_RecordGetString( rec, 9 );
    control->property = msi_dialog_dup_property( dialog, prop, FALSE );

    msi_dialog_vsc_add_drives( dialog, control );

    return ERROR_SUCCESS;
}

3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419
static UINT msi_dialog_hyperlink_handler( msi_dialog *dialog, msi_control *control, WPARAM param )
{
    static const WCHAR hrefW[] = {'h','r','e','f'};
    static const WCHAR openW[] = {'o','p','e','n',0};
    int len, len_href = sizeof(hrefW) / sizeof(hrefW[0]);
    const WCHAR *p, *q;
    WCHAR quote = 0;
    LITEM item;

    item.mask     = LIF_ITEMINDEX | LIF_URL;
    item.iLink    = 0;
    item.szUrl[0] = 0;

    SendMessageW( control->hwnd, LM_GETITEM, 0, (LPARAM)&item );

    p = item.szUrl;
    while (*p && *p != '<') p++;
    if (!*p++) return ERROR_SUCCESS;
    if (toupperW( *p++ ) != 'A' || !isspaceW( *p++ )) return ERROR_SUCCESS;
    while (*p && isspaceW( *p )) p++;

    len = strlenW( p );
    if (len > len_href && !memicmpW( p, hrefW, len_href ))
    {
        p += len_href;
        while (*p && isspaceW( *p )) p++;
        if (!*p || *p++ != '=') return ERROR_SUCCESS;
        while (*p && isspaceW( *p )) p++;

        if (*p == '\"' || *p == '\'') quote = *p++;
        q = p;
        if (quote)
        {
            while (*q && *q != quote) q++;
            if (*q != quote) return ERROR_SUCCESS;
        }
        else
        {
            while (*q && *q != '>' && !isspaceW( *q )) q++;
            if (!*q) return ERROR_SUCCESS;
        }
        item.szUrl[q - item.szUrl] = 0;
        ShellExecuteW( NULL, openW, p, NULL, NULL, SW_SHOWNORMAL );
    }
    return ERROR_SUCCESS;
}

static UINT msi_dialog_hyperlink( msi_dialog *dialog, MSIRECORD *rec )
{
    msi_control *control;
    DWORD style = WS_CHILD | WS_TABSTOP | WS_GROUP;
    const WCHAR *text = MSI_RecordGetString( rec, 10 );
    int len = strlenW( text );
    LITEM item;

    control = msi_dialog_add_control( dialog, rec, WC_LINK, style );
    if (!control)
        return ERROR_FUNCTION_FAILED;

    control->attributes = MSI_RecordGetInteger( rec, 8 );
    control->handler    = msi_dialog_hyperlink_handler;

    item.mask      = LIF_ITEMINDEX | LIF_STATE | LIF_URL;
    item.iLink     = 0;
    item.state     = LIS_ENABLED;
    item.stateMask = LIS_ENABLED;
    if (len < L_MAX_URL_LENGTH) strcpyW( item.szUrl, text );
    else item.szUrl[0] = 0;

    SendMessageW( control->hwnd, LM_SETITEM, 0, (LPARAM)&item );

    return ERROR_SUCCESS;
}

3420
static const struct control_handler msi_dialog_handler[] =
3421 3422
{
    { szText, msi_dialog_text_control },
3423
    { szPushButton, msi_dialog_button_control },
3424 3425
    { szLine, msi_dialog_line_control },
    { szBitmap, msi_dialog_bitmap_control },
3426 3427
    { szCheckBox, msi_dialog_checkbox_control },
    { szScrollableText, msi_dialog_scrolltext_control },
3428 3429
    { szComboBox, msi_dialog_combo_control },
    { szEdit, msi_dialog_edit_control },
3430
    { szMaskedEdit, msi_dialog_maskedit_control },
3431
    { szPathEdit, msi_dialog_pathedit_control },
3432
    { szProgressBar, msi_dialog_progress_bar },
3433
    { szRadioButtonGroup, msi_dialog_radiogroup_control },
3434
    { szIcon, msi_dialog_icon_control },
3435
    { szSelectionTree, msi_dialog_selection_tree },
3436
    { szGroupBox, msi_dialog_group_box },
3437
    { szListBox, msi_dialog_list_box },
3438
    { szDirectoryCombo, msi_dialog_directory_combo },
3439
    { szDirectoryList, msi_dialog_directory_list },
3440
    { szVolumeCostList, msi_dialog_volumecost_list },
3441
    { szVolumeSelectCombo, msi_dialog_volumeselect_combo },
3442
    { szHyperLink, msi_dialog_hyperlink }
3443 3444 3445 3446 3447 3448
};

#define NUM_CONTROL_TYPES (sizeof msi_dialog_handler/sizeof msi_dialog_handler[0])

static UINT msi_dialog_create_controls( MSIRECORD *rec, LPVOID param )
{
3449
    msi_dialog *dialog = param;
3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465
    LPCWSTR control_type;
    UINT i;

    /* find and call the function that can create this type of control */
    control_type = MSI_RecordGetString( rec, 3 );
    for( i=0; i<NUM_CONTROL_TYPES; i++ )
        if (!strcmpiW( msi_dialog_handler[i].control_type, control_type ))
            break;
    if( i != NUM_CONTROL_TYPES )
        msi_dialog_handler[i].func( dialog, rec );
    else
        ERR("no handler for element type %s\n", debugstr_w(control_type));

    return ERROR_SUCCESS;
}

3466
static UINT msi_dialog_fill_controls( msi_dialog *dialog )
3467 3468
{
    static const WCHAR query[] = {
3469 3470 3471
        'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
        'C','o','n','t','r','o','l',' ','W','H','E','R','E',' ',
        '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
3472
    UINT r;
3473
    MSIQUERY *view;
3474 3475
    MSIPACKAGE *package = dialog->package;

3476
    TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
3477 3478

    /* query the Control table for all the elements of the control */
3479
    r = MSI_OpenQuery( package->db, &view, query, dialog->name );
3480 3481
    if( r != ERROR_SUCCESS )
    {
3482
        ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
3483 3484 3485
        return ERROR_INVALID_PARAMETER;
    }

3486 3487 3488 3489 3490
    r = MSI_IterateRecords( view, 0, msi_dialog_create_controls, dialog );
    msiobj_release( &view->hdr );
    return r;
}

3491
static UINT msi_dialog_reset( msi_dialog *dialog )
3492 3493 3494 3495 3496
{
    /* FIXME: should restore the original values of any properties we changed */
    return msi_dialog_evaluate_control_conditions( dialog );
}

3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512
/* figure out the height of 10 point MS Sans Serif */
static INT msi_dialog_get_sans_serif_height( HWND hwnd )
{
    static const WCHAR szSansSerif[] = {
        'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0 };
    LOGFONTW lf;
    TEXTMETRICW tm;
    BOOL r;
    LONG height = 0;
    HFONT hFont, hOldFont;
    HDC hdc;

    hdc = GetDC( hwnd );
    if (hdc)
    {
        memset( &lf, 0, sizeof lf );
3513
        lf.lfHeight = MulDiv(12, GetDeviceCaps(hdc, LOGPIXELSY), 72);
3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529
        strcpyW( lf.lfFaceName, szSansSerif );
        hFont = CreateFontIndirectW(&lf);
        if (hFont)
        {
            hOldFont = SelectObject( hdc, hFont );
            r = GetTextMetricsW( hdc, &tm );
            if (r)
                height = tm.tmHeight;
            SelectObject( hdc, hOldFont );
            DeleteObject( hFont );
        }
        ReleaseDC( hwnd, hdc );
    }
    return height;
}

3530 3531
/* fetch the associated record from the Dialog table */
static MSIRECORD *msi_get_dialog_record( msi_dialog *dialog )
3532 3533 3534 3535 3536 3537 3538 3539 3540
{
    static const WCHAR query[] = {
        'S','E','L','E','C','T',' ','*',' ',
        'F','R','O','M',' ','D','i','a','l','o','g',' ',
        'W','H','E','R','E',' ',
           '`','D','i','a','l','o','g','`',' ','=',' ','\'','%','s','\'',0};
    MSIPACKAGE *package = dialog->package;
    MSIRECORD *rec = NULL;

3541
    TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
3542

3543 3544
    rec = MSI_QueryGetRecord( package->db, query, dialog->name );
    if( !rec )
3545
        WARN("query failed for dialog %s\n", debugstr_w(dialog->name));
3546

3547 3548 3549
    return rec;
}

3550
static void msi_dialog_adjust_dialog_pos( msi_dialog *dialog, MSIRECORD *rec, LPRECT pos )
3551
{
3552 3553 3554 3555
    static const WCHAR szScreenX[] = {'S','c','r','e','e','n','X',0};
    static const WCHAR szScreenY[] = {'S','c','r','e','e','n','Y',0};

    UINT xres, yres;
3556 3557
    POINT center;
    SIZE sz;
3558
    LONG style;
3559 3560 3561 3562 3563 3564 3565 3566 3567

    center.x = MSI_RecordGetInteger( rec, 2 );
    center.y = MSI_RecordGetInteger( rec, 3 );

    sz.cx = MSI_RecordGetInteger( rec, 4 );
    sz.cy = MSI_RecordGetInteger( rec, 5 );

    sz.cx = msi_dialog_scale_unit( dialog, sz.cx );
    sz.cy = msi_dialog_scale_unit( dialog, sz.cy );
3568

3569 3570
    xres = msi_get_property_int( dialog->package->db, szScreenX, 0 );
    yres = msi_get_property_int( dialog->package->db, szScreenY, 0 );
3571

3572 3573
    center.x = MulDiv( center.x, xres, 100 );
    center.y = MulDiv( center.y, yres, 100 );
3574

3575
    /* turn the client pos into the window rectangle */
3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596
    if (dialog->package->center_x && dialog->package->center_y)
    {
        pos->left = dialog->package->center_x - sz.cx / 2.0;
        pos->right = pos->left + sz.cx;
        pos->top = dialog->package->center_y - sz.cy / 2.0;
        pos->bottom = pos->top + sz.cy;
    }
    else
    {
        pos->left = center.x - sz.cx/2;
        pos->right = pos->left + sz.cx;
        pos->top = center.y - sz.cy/2;
        pos->bottom = pos->top + sz.cy;

        /* save the center */
        dialog->package->center_x = center.x;
        dialog->package->center_y = center.y;
    }

    dialog->size.cx = sz.cx;
    dialog->size.cy = sz.cy;
3597

3598
    TRACE("%u %u %u %u\n", pos->left, pos->top, pos->right, pos->bottom);
3599

3600
    style = GetWindowLongPtrW( dialog->hwnd, GWL_STYLE );
3601
    AdjustWindowRect( pos, style, FALSE );
3602 3603
}

3604
static void msi_dialog_set_tab_order( msi_dialog *dialog, LPCWSTR first )
3605
{
3606 3607 3608
    struct list tab_chain;
    msi_control *control;
    HWND prev = HWND_TOP;
3609

3610 3611
    list_init( &tab_chain );
    if (!(control = msi_dialog_find_control( dialog, first ))) return;
3612

3613 3614
    dialog->hWndFocus = control->hwnd;
    while (control)
3615
    {
3616 3617 3618 3619
        list_remove( &control->entry );
        list_add_tail( &tab_chain, &control->entry );
        if (!control->tabnext) break;
        control = msi_dialog_find_control( dialog, control->tabnext );
3620 3621
    }

3622 3623 3624 3625 3626 3627 3628
    LIST_FOR_EACH_ENTRY( control, &tab_chain, msi_control, entry )
    {
        SetWindowPos( control->hwnd, prev, 0, 0, 0, 0,
                      SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOREDRAW |
                      SWP_NOREPOSITION | SWP_NOSENDCHANGING | SWP_NOSIZE );
        prev = control->hwnd;
    }
3629

3630 3631
    /* put them back on the main list */
    list_move_head( &dialog->controls, &tab_chain );
3632 3633
}

3634 3635 3636 3637
static LRESULT msi_dialog_oncreate( HWND hwnd, LPCREATESTRUCTW cs )
{
    static const WCHAR df[] = {
        'D','e','f','a','u','l','t','U','I','F','o','n','t',0 };
3638 3639
    static const WCHAR dfv[] = {
        'M','S',' ','S','h','e','l','l',' ','D','l','g',0 };
3640
    msi_dialog *dialog = cs->lpCreateParams;
3641 3642
    MSIRECORD *rec = NULL;
    LPWSTR title = NULL;
3643
    RECT pos;
3644 3645 3646 3647 3648 3649 3650

    TRACE("%p %p\n", dialog, dialog->package);

    dialog->hwnd = hwnd;
    SetWindowLongPtrW( hwnd, GWLP_USERDATA, (LONG_PTR) dialog );

    rec = msi_get_dialog_record( dialog );
3651 3652
    if( !rec )
    {
3653 3654
        TRACE("No record found for dialog %s\n", debugstr_w(dialog->name));
        return -1;
3655 3656 3657 3658
    }

    dialog->scale = msi_dialog_get_sans_serif_height(dialog->hwnd);

3659
    msi_dialog_adjust_dialog_pos( dialog, rec, &pos );
3660

3661
    dialog->attributes = MSI_RecordGetInteger( rec, 6 );
3662

3663
    dialog->default_font = msi_dup_property( dialog->package->db, df );
3664 3665 3666
    if (!dialog->default_font)
    {
        dialog->default_font = strdupW(dfv);
3667
        msiobj_release( &rec->hdr );
3668 3669
        if (!dialog->default_font) return -1;
    }
Mike McCormack's avatar
Mike McCormack committed
3670

3671
    title = msi_get_deformatted_field( dialog->package, rec, 7 );
3672
    SetWindowTextW( hwnd, title );
3673
    msi_free( title );
3674

3675 3676
    SetWindowPos( hwnd, 0, pos.left, pos.top,
                  pos.right - pos.left, pos.bottom - pos.top,
3677
                  SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW );
3678

3679 3680 3681
    msi_dialog_build_font_list( dialog );
    msi_dialog_fill_controls( dialog );
    msi_dialog_evaluate_control_conditions( dialog );
3682
    msi_dialog_set_tab_order( dialog, MSI_RecordGetString( rec, 8 ) );
3683
    msiobj_release( &rec->hdr );
3684 3685 3686 3687

    return 0;
}

3688
static LRESULT msi_dialog_oncommand( msi_dialog *dialog, WPARAM param, HWND hwnd )
3689
{
3690
    msi_control *control = NULL;
3691

3692
    TRACE("%p %p %08lx\n", dialog, hwnd, param);
3693

3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705
    switch (param)
    {
    case 1: /* enter */
        control = msi_dialog_find_control( dialog, dialog->control_default );
        break;
    case 2: /* escape */
        control = msi_dialog_find_control( dialog, dialog->control_cancel );
        break;
    default: 
        control = msi_dialog_find_control_by_hwnd( dialog, hwnd );
    }

3706 3707
    if( control )
    {
3708
        if( control->handler )
3709
        {
3710
            control->handler( dialog, control, param );
3711 3712
            msi_dialog_evaluate_control_conditions( dialog );
        }
3713
    }
3714

3715 3716 3717
    return 0;
}

3718 3719 3720 3721 3722
static LRESULT msi_dialog_onnotify( msi_dialog *dialog, LPARAM param )
{
    LPNMHDR nmhdr = (LPNMHDR) param;
    msi_control *control = msi_dialog_find_control_by_hwnd( dialog, nmhdr->hwndFrom );

3723
    TRACE("%p %p\n", dialog, nmhdr->hwndFrom);
3724 3725 3726 3727 3728 3729 3730

    if ( control && control->handler )
        control->handler( dialog, control, param );

    return 0;
}

3731
static void dialog_setfocus( msi_dialog *dialog )
3732 3733 3734 3735 3736 3737 3738 3739 3740
{
    HWND hwnd = dialog->hWndFocus;

    hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, TRUE);
    hwnd = GetNextDlgTabItem( dialog->hwnd, hwnd, FALSE);
    SetFocus( hwnd );
    dialog->hWndFocus = hwnd;
}

3741 3742 3743
static LRESULT WINAPI MSIDialog_WndProc( HWND hwnd, UINT msg,
                WPARAM wParam, LPARAM lParam )
{
3744
    msi_dialog *dialog = (LPVOID) GetWindowLongPtrW( hwnd, GWLP_USERDATA );
3745

3746 3747
    TRACE("0x%04x\n", msg);

3748 3749
    switch (msg)
    {
3750 3751 3752 3753
    case WM_MOVE:
        dialog->package->center_x = LOWORD(lParam) + dialog->size.cx / 2.0;
        dialog->package->center_y = HIWORD(lParam) + dialog->size.cy / 2.0;
        break;
3754

3755 3756 3757 3758
    case WM_CREATE:
        return msi_dialog_oncreate( hwnd, (LPCREATESTRUCTW)lParam );

    case WM_COMMAND:
3759
        return msi_dialog_oncommand( dialog, wParam, (HWND)lParam );
3760

3761 3762 3763 3764
    case WM_ACTIVATE:
        if( LOWORD(wParam) == WA_INACTIVE )
            dialog->hWndFocus = GetFocus();
        else
3765
            dialog_setfocus( dialog );
3766 3767 3768
        return 0;

    case WM_SETFOCUS:
3769
        dialog_setfocus( dialog );
3770 3771
        return 0;

3772 3773 3774 3775
    /* bounce back to our subclassed static control */
    case WM_CTLCOLORSTATIC:
        return SendMessageW( (HWND) lParam, WM_CTLCOLORSTATIC, wParam, lParam );

3776 3777 3778
    case WM_DESTROY:
        dialog->hwnd = NULL;
        return 0;
3779 3780
    case WM_NOTIFY:
        return msi_dialog_onnotify( dialog, lParam );
3781 3782 3783 3784
    }
    return DefWindowProcW(hwnd, msg, wParam, lParam);
}

3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835
static void process_pending_messages( HWND hdlg )
{
    MSG msg;

    while (PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
    {
        if (hdlg && IsDialogMessageW( hdlg, &msg )) continue;
        TranslateMessage( &msg );
        DispatchMessageW( &msg );
    }
}

static UINT dialog_run_message_loop( msi_dialog *dialog )
{
    DWORD style;
    HWND hwnd;

    if( uiThreadId != GetCurrentThreadId() )
        return SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_CREATE, 0, (LPARAM) dialog );

    /* create the dialog window, don't show it yet */
    style = WS_OVERLAPPED;
    if( dialog->attributes & msidbDialogAttributesVisible )
        style |= WS_VISIBLE;

    hwnd = CreateWindowW( szMsiDialogClass, dialog->name, style,
                     CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
                     NULL, NULL, NULL, dialog );
    if( !hwnd )
    {
        ERR("Failed to create dialog %s\n", debugstr_w( dialog->name ));
        return ERROR_FUNCTION_FAILED;
    }

    ShowWindow( hwnd, SW_SHOW );
    /* UpdateWindow( hwnd ); - and causes the transparent static controls not to paint */

    if( dialog->attributes & msidbDialogAttributesModal )
    {
        while( !dialog->finished )
        {
            MsgWaitForMultipleObjects( 0, NULL, 0, INFINITE, QS_ALLINPUT );
            process_pending_messages( dialog->hwnd );
        }
    }
    else
        return ERROR_IO_PENDING;

    return ERROR_SUCCESS;
}

3836 3837 3838 3839 3840 3841 3842 3843 3844 3845
static LRESULT WINAPI MSIHiddenWindowProc( HWND hwnd, UINT msg,
                WPARAM wParam, LPARAM lParam )
{
    msi_dialog *dialog = (msi_dialog*) lParam;

    TRACE("%d %p\n", msg, dialog);

    switch (msg)
    {
    case WM_MSI_DIALOG_CREATE:
3846
        return dialog_run_message_loop( dialog );
3847 3848 3849 3850 3851 3852 3853
    case WM_MSI_DIALOG_DESTROY:
        msi_dialog_destroy( dialog );
        return 0;
    }
    return DefWindowProcW( hwnd, msg, wParam, lParam );
}

3854
static BOOL dialog_register_class( void )
3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885
{
    WNDCLASSW cls;

    ZeroMemory( &cls, sizeof cls );
    cls.lpfnWndProc   = MSIDialog_WndProc;
    cls.hInstance     = NULL;
    cls.hIcon         = LoadIconW(0, (LPWSTR)IDI_APPLICATION);
    cls.hCursor       = LoadCursorW(0, (LPWSTR)IDC_ARROW);
    cls.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
    cls.lpszMenuName  = NULL;
    cls.lpszClassName = szMsiDialogClass;

    if( !RegisterClassW( &cls ) )
        return FALSE;

    cls.lpfnWndProc   = MSIHiddenWindowProc;
    cls.lpszClassName = szMsiHiddenWindow;

    if( !RegisterClassW( &cls ) )
        return FALSE;

    uiThreadId = GetCurrentThreadId();

    hMsiHiddenWindow = CreateWindowW( szMsiHiddenWindow, NULL, WS_OVERLAPPED,
                                   0, 0, 100, 100, NULL, NULL, NULL, NULL );
    if( !hMsiHiddenWindow )
        return FALSE;

    return TRUE;
}

3886 3887
static msi_dialog *dialog_create( MSIPACKAGE *package, const WCHAR *name, msi_dialog *parent,
                                  control_event_handler event_handler )
3888
{
3889
    MSIRECORD *rec = NULL;
3890
    msi_dialog *dialog;
3891

3892
    TRACE("%s\n", debugstr_w(name));
3893

3894
    if (!hMsiHiddenWindow) dialog_register_class();
3895

3896
    /* allocate the structure for the dialog to use */
3897
    dialog = msi_alloc_zero( FIELD_OFFSET( msi_dialog, name[strlenW( name ) + 1] ));
3898 3899
    if( !dialog )
        return NULL;
3900
    strcpyW( dialog->name, name );
3901
    dialog->parent = parent;
3902
    msiobj_addref( &package->hdr );
3903 3904
    dialog->package = package;
    dialog->event_handler = event_handler;
3905
    dialog->finished = 0;
3906
    list_init( &dialog->controls );
3907
    list_init( &dialog->fonts );
3908

3909 3910 3911
    /* verify that the dialog exists */
    rec = msi_get_dialog_record( dialog );
    if( !rec )
3912
    {
3913
        msiobj_release( &package->hdr );
3914
        msi_free( dialog );
3915 3916
        return NULL;
    }
3917
    dialog->attributes = MSI_RecordGetInteger( rec, 6 );
3918 3919
    dialog->control_default = strdupW( MSI_RecordGetString( rec, 9 ) );
    dialog->control_cancel = strdupW( MSI_RecordGetString( rec, 10 ) );
3920
    msiobj_release( &rec->hdr );
3921 3922 3923 3924

    return dialog;
}

3925
static void msi_dialog_end_dialog( msi_dialog *dialog )
3926
{
3927
    TRACE("%p\n", dialog);
3928
    dialog->finished = 1;
3929
    PostMessageW(dialog->hwnd, WM_NULL, 0, 0);
3930 3931
}

3932
void msi_dialog_check_messages( HANDLE handle )
3933
{
3934
    DWORD r;
3935

3936 3937
    /* in threads other than the UI thread, block */
    if( uiThreadId != GetCurrentThreadId() )
3938
    {
3939 3940 3941 3942 3943 3944 3945 3946 3947 3948
        if (!handle) return;
        while (MsgWaitForMultipleObjectsEx( 1, &handle, INFINITE, QS_ALLINPUT, 0 ) == WAIT_OBJECT_0 + 1)
        {
            MSG msg;
            while (PeekMessageW( &msg, NULL, 0, 0, PM_REMOVE ))
            {
                TranslateMessage( &msg );
                DispatchMessageW( &msg );
            }
        }
3949
        return;
3950 3951
    }

3952
    /* there are two choices for the UI thread */
3953
    while (1)
3954
    {
3955
        process_pending_messages( NULL );
3956

3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967
        if( !handle )
            break;

        /*
         * block here until somebody creates a new dialog or
         * the handle we're waiting on becomes ready
         */
        r = MsgWaitForMultipleObjects( 1, &handle, 0, INFINITE, QS_ALLINPUT );
        if( r == WAIT_OBJECT_0 )
            break;
    }
3968 3969
}

3970
static void dialog_do_preview( msi_dialog *dialog )
3971
{
3972 3973 3974 3975 3976
    TRACE("\n");
    dialog->attributes |= msidbDialogAttributesVisible;
    dialog->attributes &= ~msidbDialogAttributesModal;
    dialog_run_message_loop( dialog );
}
3977

3978 3979 3980 3981 3982 3983 3984
static void free_subscriber( struct subscriber *sub )
{
    msi_free( sub->event );
    msi_free( sub->control );
    msi_free( sub->attribute );
    msi_free( sub );
}
3985

3986 3987 3988
static void event_cleanup_subscriptions( MSIPACKAGE *package, const WCHAR *dialog )
{
    struct list *item, *next;
3989

3990
    LIST_FOR_EACH_SAFE( item, next, &package->subscriptions )
3991
    {
3992
        struct subscriber *sub = LIST_ENTRY( item, struct subscriber, entry );
3993

3994 3995 3996
        if (strcmpW( sub->dialog->name, dialog )) continue;
        list_remove( &sub->entry );
        free_subscriber( sub );
3997 3998 3999 4000
    }
}

void msi_dialog_destroy( msi_dialog *dialog )
4001
{
4002 4003
    msi_font *font, *next;

4004 4005 4006 4007 4008 4009
    if( uiThreadId != GetCurrentThreadId() )
    {
        SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_DESTROY, 0, (LPARAM) dialog );
        return;
    }

4010 4011
    if( dialog->hwnd )
        ShowWindow( dialog->hwnd, SW_HIDE );
4012

4013 4014 4015
    if( dialog->hwnd )
        DestroyWindow( dialog->hwnd );

4016
    /* unsubscribe events */
4017
    event_cleanup_subscriptions( dialog->package, dialog->name );
4018

4019
    /* destroy the list of controls */
4020
    while( !list_empty( &dialog->controls ) )
4021
    {
4022 4023 4024 4025 4026
        msi_control *t;

        t = LIST_ENTRY( list_head( &dialog->controls ),
                        msi_control, entry );
        msi_destroy_control( t );
4027 4028 4029
    }

    /* destroy the list of fonts */
4030
    LIST_FOR_EACH_ENTRY_SAFE( font, next, &dialog->fonts, msi_font, entry )
4031
    {
4032 4033 4034
        list_remove( &font->entry );
        DeleteObject( font->hfont );
        msi_free( font );
4035
    }
4036
    msi_free( dialog->default_font );
4037

4038 4039
    msi_free( dialog->control_default );
    msi_free( dialog->control_cancel );
4040
    msiobj_release( &dialog->package->hdr );
4041
    dialog->package = NULL;
4042
    msi_free( dialog );
4043 4044
}

4045 4046 4047
void msi_dialog_unregister_class( void )
{
    DestroyWindow( hMsiHiddenWindow );
4048
    hMsiHiddenWindow = NULL;
4049
    UnregisterClassW( szMsiDialogClass, NULL );
4050
    UnregisterClassW( szMsiHiddenWindow, NULL );
4051
    uiThreadId = 0;
4052
}
4053

4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066
void msi_event_cleanup_all_subscriptions( MSIPACKAGE *package )
{
    struct list *item, *next;

    LIST_FOR_EACH_SAFE( item, next, &package->subscriptions )
    {
        struct subscriber *sub = LIST_ENTRY( item, struct subscriber, entry );
        list_remove( &sub->entry );
        free_subscriber( sub );
    }
}

static UINT error_dialog_handler( msi_dialog *dialog, const WCHAR *event, const WCHAR *argument )
4067 4068 4069 4070 4071 4072 4073 4074 4075
{
    static const WCHAR end_dialog[] = {'E','n','d','D','i','a','l','o','g',0};
    static const WCHAR error_abort[] = {'E','r','r','o','r','A','b','o','r','t',0};
    static const WCHAR error_cancel[] = {'E','r','r','o','r','C','a','n','c','e','l',0};
    static const WCHAR error_no[] = {'E','r','r','o','r','N','o',0};
    static const WCHAR result_prop[] = {
        'M','S','I','E','r','r','o','r','D','i','a','l','o','g','R','e','s','u','l','t',0
    };

4076
    if ( strcmpW( event, end_dialog ) )
4077 4078
        return ERROR_SUCCESS;

4079 4080
    if ( !strcmpW( argument, error_abort ) || !strcmpW( argument, error_cancel ) ||
         !strcmpW( argument, error_no ) )
4081
    {
4082
         msi_set_property( dialog->package->db, result_prop, error_abort, -1 );
4083 4084
    }

4085
    msi_event_cleanup_all_subscriptions( dialog->package );
4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124
    msi_dialog_end_dialog( dialog );

    return ERROR_SUCCESS;
}

static UINT msi_error_dialog_set_error( MSIPACKAGE *package, LPWSTR error_dialog, LPWSTR error )
{
    MSIRECORD * row;

    static const WCHAR update[] = 
        {'U','P','D','A','T','E',' ','`','C','o','n','t','r','o','l','`',' ',
         'S','E','T',' ','`','T','e','x','t','`',' ','=',' ','\'','%','s','\'',' ',
         'W','H','E','R','E', ' ','`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
         'A','N','D',' ','`','C','o','n','t','r','o','l','`',' ','=',' ',
         '\'','E','r','r','o','r','T','e','x','t','\'',0};

    row = MSI_QueryGetRecord( package->db, update, error, error_dialog );
    if (!row)
        return ERROR_FUNCTION_FAILED;

    msiobj_release(&row->hdr);
    return ERROR_SUCCESS;
}

UINT msi_spawn_error_dialog( MSIPACKAGE *package, LPWSTR error_dialog, LPWSTR error )
{
    msi_dialog *dialog;
    WCHAR result[MAX_PATH];
    UINT r = ERROR_SUCCESS;
    DWORD size = MAX_PATH;
    int res;

    static const WCHAR pn_prop[] = {'P','r','o','d','u','c','t','N','a','m','e',0};
    static const WCHAR title_fmt[] = {'%','s',' ','W','a','r','n','i','n','g',0};
    static const WCHAR error_abort[] = {'E','r','r','o','r','A','b','o','r','t',0};
    static const WCHAR result_prop[] = {
        'M','S','I','E','r','r','o','r','D','i','a','l','o','g','R','e','s','u','l','t',0
    };

4125
    if ((package->ui_level & INSTALLUILEVEL_MASK) == INSTALLUILEVEL_NONE) return ERROR_SUCCESS;
4126

4127 4128
    if ( !error_dialog )
    {
4129
        LPWSTR product_name = msi_dup_property( package->db, pn_prop );
4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146
        WCHAR title[MAX_PATH];

        sprintfW( title, title_fmt, product_name );
        res = MessageBoxW( NULL, error, title, MB_OKCANCEL | MB_ICONWARNING );

        msi_free( product_name );

        if ( res == IDOK )
            return ERROR_SUCCESS;
        else
            return ERROR_FUNCTION_FAILED;
    }

    r = msi_error_dialog_set_error( package, error_dialog, error );
    if ( r != ERROR_SUCCESS )
        return r;

4147
    dialog = dialog_create( package, error_dialog, package->dialog, error_dialog_handler );
4148 4149 4150 4151
    if ( !dialog )
        return ERROR_FUNCTION_FAILED;

    dialog->finished = FALSE;
4152
    r = dialog_run_message_loop( dialog );
4153 4154 4155
    if ( r != ERROR_SUCCESS )
        goto done;

4156
    r = msi_get_property( package->db, result_prop, result, &size );
4157 4158 4159
    if ( r != ERROR_SUCCESS)
        r = ERROR_SUCCESS;

4160
    if ( !strcmpW( result, error_abort ) )
4161 4162 4163 4164 4165 4166 4167
        r = ERROR_FUNCTION_FAILED;

done:
    msi_dialog_destroy( dialog );

    return r;
}
4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230

static void MSI_ClosePreview( MSIOBJECTHDR *arg )
{
    MSIPREVIEW *preview = (MSIPREVIEW *)arg;
    msiobj_release( &preview->package->hdr );
}

static MSIPREVIEW *MSI_EnableUIPreview( MSIDATABASE *db )
{
    MSIPREVIEW *preview = NULL;
    MSIPACKAGE *package;

    package = MSI_CreatePackage( db, NULL );
    if (package)
    {
        preview = alloc_msiobject( MSIHANDLETYPE_PREVIEW, sizeof(MSIPREVIEW), MSI_ClosePreview );
        if (preview)
        {
            preview->package = package;
            msiobj_addref( &package->hdr );
        }
        msiobj_release( &package->hdr );
    }
    return preview;
}

UINT WINAPI MsiEnableUIPreview( MSIHANDLE hdb, MSIHANDLE *phPreview )
{
    MSIDATABASE *db;
    MSIPREVIEW *preview;
    UINT r = ERROR_FUNCTION_FAILED;

    TRACE("%d %p\n", hdb, phPreview);

    db = msihandle2msiinfo( hdb, MSIHANDLETYPE_DATABASE );
    if (!db)
    {
        IWineMsiRemoteDatabase *remote_database;

        remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( hdb );
        if (!remote_database)
            return ERROR_INVALID_HANDLE;

        *phPreview = 0;

        IWineMsiRemoteDatabase_Release( remote_database );
        WARN("MsiEnableUIPreview not allowed during a custom action!\n");

        return ERROR_FUNCTION_FAILED;
    }
    preview = MSI_EnableUIPreview( db );
    if (preview)
    {
        *phPreview = alloc_msihandle( &preview->hdr );
        msiobj_release( &preview->hdr );
        r = ERROR_SUCCESS;
        if (!*phPreview)
            r = ERROR_NOT_ENOUGH_MEMORY;
    }
    msiobj_release( &db->hdr );
    return r;
}

4231
static UINT preview_event_handler( msi_dialog *dialog, const WCHAR *event, const WCHAR *argument )
4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247
{
    MESSAGE("Preview dialog event '%s' (arg='%s')\n", debugstr_w(event), debugstr_w(argument));
    return ERROR_SUCCESS;
}

static UINT MSI_PreviewDialogW( MSIPREVIEW *preview, LPCWSTR szDialogName )
{
    msi_dialog *dialog = NULL;
    UINT r = ERROR_SUCCESS;

    if (preview->dialog)
        msi_dialog_destroy( preview->dialog );

    /* an empty name means we should just destroy the current preview dialog */
    if (szDialogName)
    {
4248
        dialog = dialog_create( preview->package, szDialogName, NULL, preview_event_handler );
4249
        if (dialog)
4250
            dialog_do_preview( dialog );
4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302
        else
            r = ERROR_FUNCTION_FAILED;
    }
    preview->dialog = dialog;
    return r;
}

UINT WINAPI MsiPreviewDialogW( MSIHANDLE hPreview, LPCWSTR szDialogName )
{
    MSIPREVIEW *preview;
    UINT r;

    TRACE("%d %s\n", hPreview, debugstr_w(szDialogName));

    preview = msihandle2msiinfo( hPreview, MSIHANDLETYPE_PREVIEW );
    if (!preview)
        return ERROR_INVALID_HANDLE;

    r = MSI_PreviewDialogW( preview, szDialogName );
    msiobj_release( &preview->hdr );
    return r;
}

UINT WINAPI MsiPreviewDialogA( MSIHANDLE hPreview, LPCSTR szDialogName )
{
    UINT r;
    LPWSTR strW = NULL;

    TRACE("%d %s\n", hPreview, debugstr_a(szDialogName));

    if (szDialogName)
    {
        strW = strdupAtoW( szDialogName );
        if (!strW)
            return ERROR_OUTOFMEMORY;
    }
    r = MsiPreviewDialogW( hPreview, strW );
    msi_free( strW );
    return r;
}

UINT WINAPI MsiPreviewBillboardW( MSIHANDLE hPreview, LPCWSTR szControlName, LPCWSTR szBillboard )
{
    FIXME("%d %s %s\n", hPreview, debugstr_w(szControlName), debugstr_w(szBillboard));
    return ERROR_CALL_NOT_IMPLEMENTED;
}

UINT WINAPI MsiPreviewBillboardA( MSIHANDLE hPreview, LPCSTR szControlName, LPCSTR szBillboard )
{
    FIXME("%d %s %s\n", hPreview, debugstr_a(szControlName), debugstr_a(szBillboard));
    return ERROR_CALL_NOT_IMPLEMENTED;
}
4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602

typedef UINT (*event_handler)( msi_dialog *, const WCHAR * );

struct control_event
{
    const WCHAR  *event;
    event_handler handler;
};

static UINT dialog_event_handler( msi_dialog *, const WCHAR *, const WCHAR * );

/* create a dialog box and run it if it's modal */
static UINT event_do_dialog( MSIPACKAGE *package, const WCHAR *name, msi_dialog *parent, BOOL destroy_modeless )
{
    msi_dialog *dialog;
    UINT r;

    /* create a new dialog */
    dialog = dialog_create( package, name, parent, dialog_event_handler );
    if (dialog)
    {
        /* kill the current modeless dialog */
        if (destroy_modeless && package->dialog)
        {
            msi_dialog_destroy( package->dialog );
            package->dialog = NULL;
        }

        /* modeless dialogs return an error message */
        r = dialog_run_message_loop( dialog );
        if (r == ERROR_SUCCESS)
            msi_dialog_destroy( dialog );
        else
            package->dialog = dialog;
    }
    else r = ERROR_FUNCTION_FAILED;
    return r;
}

/* end a modal dialog box */
static UINT event_end_dialog( msi_dialog *dialog, const WCHAR *argument )
{
    static const WCHAR exitW[] = {'E','x','i','t',0};
    static const WCHAR retryW[] = {'R','e','t','r','y',0};
    static const WCHAR ignoreW[] = {'I','g','n','o','r','e',0};
    static const WCHAR returnW[] = {'R','e','t','u','r','n',0};

    if (!strcmpW( argument, exitW ))
        dialog->package->CurrentInstallState = ERROR_INSTALL_USEREXIT;
    else if (!strcmpW( argument, retryW ))
        dialog->package->CurrentInstallState = ERROR_INSTALL_SUSPEND;
    else if (!strcmpW( argument, ignoreW ))
        dialog->package->CurrentInstallState = ERROR_SUCCESS;
    else if (!strcmpW( argument, returnW ))
    {
        msi_dialog *parent = dialog->parent;
        msi_free( dialog->package->next_dialog );
        dialog->package->next_dialog = (parent) ? strdupW( parent->name ) : NULL;
        dialog->package->CurrentInstallState = ERROR_SUCCESS;
    }
    else
    {
        ERR("Unknown argument string %s\n", debugstr_w(argument));
        dialog->package->CurrentInstallState = ERROR_FUNCTION_FAILED;
    }
    event_cleanup_subscriptions( dialog->package, dialog->name );
    msi_dialog_end_dialog( dialog );
    return ERROR_SUCCESS;
}

/* transition from one modal dialog to another modal dialog */
static UINT event_new_dialog( msi_dialog *dialog, const WCHAR *argument )
{
    /* store the name of the next dialog, and signal this one to end */
    dialog->package->next_dialog = strdupW( argument );
    msi_event_cleanup_all_subscriptions( dialog->package );
    msi_dialog_end_dialog( dialog );
    return ERROR_SUCCESS;
}

/* create a new child dialog of an existing modal dialog */
static UINT event_spawn_dialog( msi_dialog *dialog, const WCHAR *argument )
{
    /* don't destroy a modeless dialogs that might be our parent */
    event_do_dialog( dialog->package, argument, dialog, FALSE );
    if (dialog->package->CurrentInstallState != ERROR_SUCCESS) msi_dialog_end_dialog( dialog );
    return ERROR_SUCCESS;
}

/* creates a dialog that remains up for a period of time based on a condition */
static UINT event_spawn_wait_dialog( msi_dialog *dialog, const WCHAR *argument )
{
    FIXME("doing nothing\n");
    return ERROR_SUCCESS;
}

static UINT event_do_action( msi_dialog *dialog, const WCHAR *argument )
{
    ACTION_PerformAction( dialog->package, argument, SCRIPT_NONE );
    return ERROR_SUCCESS;
}

static UINT event_add_local( msi_dialog *dialog, const WCHAR *argument )
{
    MSIFEATURE *feature;

    LIST_FOR_EACH_ENTRY( feature, &dialog->package->features, MSIFEATURE, entry )
    {
        if (!strcmpW( argument, feature->Feature ) || !strcmpW( argument, szAll ))
        {
            if (feature->ActionRequest != INSTALLSTATE_LOCAL)
                msi_set_property( dialog->package->db, szPreselected, szOne, -1 );
            MSI_SetFeatureStateW( dialog->package, feature->Feature, INSTALLSTATE_LOCAL );
        }
    }
    return ERROR_SUCCESS;
}

static UINT event_remove( msi_dialog *dialog, const WCHAR *argument )
{
    MSIFEATURE *feature;

    LIST_FOR_EACH_ENTRY( feature, &dialog->package->features, MSIFEATURE, entry )
    {
        if (!strcmpW( argument, feature->Feature ) || !strcmpW( argument, szAll ))
        {
            if (feature->ActionRequest != INSTALLSTATE_ABSENT)
                msi_set_property( dialog->package->db, szPreselected, szOne, -1 );
            MSI_SetFeatureStateW( dialog->package, feature->Feature, INSTALLSTATE_ABSENT );
        }
    }
    return ERROR_SUCCESS;
}

static UINT event_add_source( msi_dialog *dialog, const WCHAR *argument )
{
    MSIFEATURE *feature;

    LIST_FOR_EACH_ENTRY( feature, &dialog->package->features, MSIFEATURE, entry )
    {
        if (!strcmpW( argument, feature->Feature ) || !strcmpW( argument, szAll ))
        {
            if (feature->ActionRequest != INSTALLSTATE_SOURCE)
                msi_set_property( dialog->package->db, szPreselected, szOne, -1 );
            MSI_SetFeatureStateW( dialog->package, feature->Feature, INSTALLSTATE_SOURCE );
        }
    }
    return ERROR_SUCCESS;
}

void msi_event_fire( MSIPACKAGE *package, const WCHAR *event, MSIRECORD *rec )
{
    struct subscriber *sub;

    TRACE("firing event %s\n", debugstr_w(event));

    LIST_FOR_EACH_ENTRY( sub, &package->subscriptions, struct subscriber, entry )
    {
        if (strcmpiW( sub->event, event )) continue;
        dialog_handle_event( sub->dialog, sub->control, sub->attribute, rec );
    }
}

static UINT event_set_target_path( msi_dialog *dialog, const WCHAR *argument )
{
    WCHAR *path = msi_dup_property( dialog->package->db, argument );
    MSIRECORD *rec = MSI_CreateRecord( 1 );
    UINT r = ERROR_SUCCESS;

    MSI_RecordSetStringW( rec, 1, path );
    msi_event_fire( dialog->package, szSelectionPath, rec );
    if (path)
    {
        /* failure to set the path halts the executing of control events */
        r = MSI_SetTargetPathW( dialog->package, argument, path );
        msi_free( path );
    }
    msi_free( &rec->hdr );
    return r;
}

static UINT event_reset( msi_dialog *dialog, const WCHAR *argument )
{
    msi_dialog_reset( dialog );
    return ERROR_SUCCESS;
}

/* Return ERROR_SUCCESS if dialog is process and ERROR_FUNCTION_FAILED
 * if the given parameter is not a dialog box
 */
UINT ACTION_DialogBox( MSIPACKAGE *package, const WCHAR *dialog )
{
    UINT r;

    if (package->next_dialog) ERR("Already got next dialog... ignoring it\n");
    package->next_dialog = NULL;

    /* Dialogs are chained by filling in the next_dialog member
     * of the package structure, then terminating the current dialog.
     * The code below sees the next_dialog member set, and runs the
     * next dialog.
     * We fall out of the loop below if we come across a modeless
     * dialog, as it returns ERROR_IO_PENDING when we try to run
     * its message loop.
     */
    r = event_do_dialog( package, dialog, NULL, TRUE );
    while (r == ERROR_SUCCESS && package->next_dialog)
    {
        WCHAR *name = package->next_dialog;

        package->next_dialog = NULL;
        r = event_do_dialog( package, name, NULL, TRUE );
        msi_free( name );
    }
    if (r == ERROR_IO_PENDING) r = ERROR_SUCCESS;
    return r;
}

static UINT event_set_install_level( msi_dialog *dialog, const WCHAR *argument )
{
    int level = atolW( argument );

    TRACE("setting install level to %d\n", level);
    return MSI_SetInstallLevel( dialog->package, level );
}

static UINT event_directory_list_up( msi_dialog *dialog, const WCHAR *argument )
{
    return msi_dialog_directorylist_up( dialog );
}

static UINT event_reinstall_mode( msi_dialog *dialog, const WCHAR *argument )
{
    return msi_set_property( dialog->package->db, szReinstallMode, argument, -1 );
}

static UINT event_reinstall( msi_dialog *dialog, const WCHAR *argument )
{
    return msi_set_property( dialog->package->db, szReinstall, argument, -1 );
}

static UINT event_validate_product_id( msi_dialog *dialog, const WCHAR *argument )
{
    return msi_validate_product_id( dialog->package );
}

static const WCHAR end_dialogW[] = {'E','n','d','D','i','a','l','o','g',0};
static const WCHAR new_dialogW[] = {'N','e','w','D','i','a','l','o','g',0};
static const WCHAR spawn_dialogW[] = {'S','p','a','w','n','D','i','a','l','o','g',0};
static const WCHAR spawn_wait_dialogW[] = {'S','p','a','w','n','W','a','i','t','D','i','a','l','o','g',0};
static const WCHAR do_actionW[] = {'D','o','A','c','t','i','o','n',0};
static const WCHAR add_localW[] = {'A','d','d','L','o','c','a','l',0};
static const WCHAR removeW[] = {'R','e','m','o','v','e',0};
static const WCHAR add_sourceW[] = {'A','d','d','S','o','u','r','c','e',0};
static const WCHAR set_target_pathW[] = {'S','e','t','T','a','r','g','e','t','P','a','t','h',0};
static const WCHAR resetW[] = {'R','e','s','e','t',0};
static const WCHAR set_install_levelW[] = {'S','e','t','I','n','s','t','a','l','l','L','e','v','e','l',0};
static const WCHAR directory_list_upW[] = {'D','i','r','e','c','t','o','r','y','L','i','s','t','U','p',0};
static const WCHAR selection_browseW[] = {'S','e','l','e','c','t','i','o','n','B','r','o','w','s','e',0};
static const WCHAR reinstall_modeW[] = {'R','e','i','n','s','t','a','l','l','M','o','d','e',0};
static const WCHAR reinstallW[] = {'R','e','i','n','s','t','a','l','l',0};
static const WCHAR validate_product_idW[] = {'V','a','l','i','d','a','t','e','P','r','o','d','u','c','t','I','D',0};

static const struct control_event control_events[] =
{
    { end_dialogW, event_end_dialog },
    { new_dialogW, event_new_dialog },
    { spawn_dialogW, event_spawn_dialog },
    { spawn_wait_dialogW, event_spawn_wait_dialog },
    { do_actionW, event_do_action },
    { add_localW, event_add_local },
    { removeW, event_remove },
    { add_sourceW, event_add_source },
    { set_target_pathW, event_set_target_path },
    { resetW, event_reset },
    { set_install_levelW, event_set_install_level },
    { directory_list_upW, event_directory_list_up },
    { selection_browseW, event_spawn_dialog },
    { reinstall_modeW, event_reinstall_mode },
    { reinstallW, event_reinstall },
    { validate_product_idW, event_validate_product_id },
    { NULL, NULL }
};

static UINT dialog_event_handler( msi_dialog *dialog, const WCHAR *event, const WCHAR *argument )
{
    unsigned int i;

    TRACE("handling event %s\n", debugstr_w(event));

    if (!event) return ERROR_SUCCESS;

    for (i = 0; control_events[i].event; i++)
    {
        if (!strcmpW( control_events[i].event, event ))
            return control_events[i].handler( dialog, argument );
    }
    FIXME("unhandled event %s arg(%s)\n", debugstr_w(event), debugstr_w(argument));
    return ERROR_SUCCESS;
}