dialog.c 119 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Implementation of the Microsoft Installer (msi.dll)
 *
 * Copyright 2005 Mike McCormack for CodeWeavers
 *
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20
 */

21
#define COBJMACROS
22 23
#define NONAMELESSUNION
#define NONAMELESSSTRUCT
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 43 44 45 46 47

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

WINE_DEFAULT_DEBUG_CHANNEL(msi);

48 49
extern HINSTANCE msi_hInstance;

50 51
struct msi_control_tag;
typedef struct msi_control_tag msi_control;
52
typedef UINT (*msi_handler)( msi_dialog *, msi_control *, WPARAM );
53
typedef void (*msi_update)( msi_dialog *, msi_control * );
54 55 56

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

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

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

102
typedef UINT (*msi_dialog_control_func)( msi_dialog *dialog, MSIRECORD *rec );
103 104 105 106 107 108
struct control_handler 
{
    LPCWSTR control_type;
    msi_dialog_control_func func;
};

109 110 111 112 113
typedef struct
{
    msi_dialog* dialog;
    msi_control *parent;
    DWORD       attributes;
114
    LPWSTR      propval;
115 116
} radio_button_group_descr;

117 118
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 };
119 120 121
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 };
122
static const WCHAR szProgress[] = { 'P','r','o','g','r','e','s','s',0 };
123 124 125 126 127
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 };
128
static const WCHAR szScrollableText[] = { 'S','c','r','o','l','l','a','b','l','e','T','e','x','t',0 };
129 130 131 132
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 };
133 134 135
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 };
136
static const WCHAR szIcon[] = { 'I','c','o','n',0 };
137
static const WCHAR szSelectionTree[] = { 'S','e','l','e','c','t','i','o','n','T','r','e','e',0 };
138
static const WCHAR szGroupBox[] = { 'G','r','o','u','p','B','o','x',0 };
139
static const WCHAR szListBox[] = { 'L','i','s','t','B','o','x',0 };
140
static const WCHAR szDirectoryCombo[] = { 'D','i','r','e','c','t','o','r','y','C','o','m','b','o',0 };
141
static const WCHAR szDirectoryList[] = { 'D','i','r','e','c','t','o','r','y','L','i','s','t',0 };
142
static const WCHAR szVolumeCostList[] = { 'V','o','l','u','m','e','C','o','s','t','L','i','s','t',0 };
143
static const WCHAR szVolumeSelectCombo[] = { 'V','o','l','u','m','e','S','e','l','e','c','t','C','o','m','b','o',0 };
144 145
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};
146
static const WCHAR szProperty[] = {'P','r','o','p','e','r','t','y',0};
147

148 149 150 151 152
/* dialog sequencing */

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

153 154
#define USER_INSTALLSTATE_ALL 0x1000

155 156 157
static DWORD uiThreadId;
static HWND hMsiHiddenWindow;

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
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;
}
176

177
static INT msi_dialog_scale_unit( msi_dialog *dialog, INT val )
178
{
179
    return MulDiv( val, dialog->scale, 12 );
180
}
181

182 183 184 185
static msi_control *msi_dialog_find_control( msi_dialog *dialog, LPCWSTR name )
{
    msi_control *control;

186 187
    if( !name )
        return NULL;
188 189
    if( !dialog->hwnd )
        return NULL;
190
    LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
191
        if( !strcmpW( control->name, name ) ) /* FIXME: case sensitive? */
192 193
            return control;
    return NULL;
194 195
}

196 197 198 199 200 201
static msi_control *msi_dialog_find_control_by_type( msi_dialog *dialog, LPCWSTR type )
{
    msi_control *control;

    if( !type )
        return NULL;
202 203
    if( !dialog->hwnd )
        return NULL;
204 205 206 207 208 209
    LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
        if( !strcmpW( control->type, type ) ) /* FIXME: case sensitive? */
            return control;
    return NULL;
}

210 211 212 213
static msi_control *msi_dialog_find_control_by_hwnd( msi_dialog *dialog, HWND hwnd )
{
    msi_control *control;

214 215
    if( !dialog->hwnd )
        return NULL;
216
    LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
217
        if( hwnd == control->hwnd )
218 219
            return control;
    return NULL;
220 221
}

222 223 224 225 226 227 228 229 230 231
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;
}

232 233
static LPWSTR msi_dialog_dup_property( msi_dialog *dialog, LPCWSTR property, BOOL indirect )
{
234 235
    LPWSTR prop = NULL;

236 237 238 239
    if (!property)
        return NULL;

    if (indirect)
240
        prop = msi_dup_property( dialog->package->db, property );
241 242 243

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

245
    return prop;
246 247
}

248 249 250 251 252 253 254 255 256 257
msi_dialog *msi_dialog_get_parent( msi_dialog *dialog )
{
    return dialog->parent;
}

LPWSTR msi_dialog_get_name( msi_dialog *dialog )
{
    return dialog->name;
}

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
static msi_control *msi_dialog_create_window( msi_dialog *dialog,
407
                MSIRECORD *rec, DWORD exstyle, LPCWSTR szCls, LPCWSTR name, LPCWSTR text,
408
                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 500 501 502 503 504 505 506
        '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 LPWSTR msi_create_tmp_path(void)
{
    WCHAR tmp[MAX_PATH];
    LPWSTR path = NULL;
    DWORD len, r;

    r = GetTempPathW( MAX_PATH, tmp );
    if( !r )
        return path;
    len = lstrlenW( tmp ) + 20;
507
    path = msi_alloc( len * sizeof (WCHAR) );
508 509
    if( path )
    {
510
        r = GetTempFileNameW( tmp, szMsi, 0, path );
511 512
        if (!r)
        {
513
            msi_free( path );
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
            path = NULL;
        }
    }
    return path;
}

static HANDLE msi_load_image( MSIDATABASE *db, LPCWSTR name, UINT type,
                              UINT cx, UINT cy, UINT flags )
{
    MSIRECORD *rec = NULL;
    HANDLE himage = NULL;
    LPWSTR tmp;
    UINT r;

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

    tmp = msi_create_tmp_path();
    if( !tmp )
        return himage;

    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 );
    }
544
    DeleteFileW( tmp );
545

546
    msi_free( tmp );
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
    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 );
}

573 574 575 576 577 578
static void msi_dialog_update_controls( msi_dialog *dialog, LPCWSTR property )
{
    msi_control *control;

    LIST_FOR_EACH_ENTRY( control, &dialog->controls, msi_control, entry )
    {
579
        if ( control->property && !strcmpW( control->property, property ) && control->update )
580 581 582
            control->update( dialog, control );
    }
}
583

584 585
static void msi_dialog_set_property( MSIPACKAGE *package, LPCWSTR property, LPCWSTR value )
{
586
    UINT r = msi_set_property( package->db, property, value, -1 );
587
    if (r == ERROR_SUCCESS && !strcmpW( property, szSourceDir ))
588 589 590
        msi_reset_folders( package, TRUE );
}

591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
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 );
}

617 618 619 620 621
/* called from the Control Event subscription code */
void msi_dialog_handle_event( msi_dialog* dialog, LPCWSTR control, 
                              LPCWSTR attribute, MSIRECORD *rec )
{
    msi_control* ctrl;
622 623
    LPCWSTR font_text, text = NULL;
    LPWSTR font;
624 625 626 627

    ctrl = msi_dialog_find_control( dialog, control );
    if (!ctrl)
        return;
628
    if( !strcmpW( attribute, szText ) )
629
    {
630 631
        font_text = MSI_RecordGetString( rec , 1 );
        font = msi_dialog_get_style( font_text, &text );
632
        if (!text) text = szEmpty;
633 634 635 636
        SetWindowTextW( ctrl->hwnd, text );
        msi_free( font );
        msi_dialog_check_messages( NULL );
    }
637
    else if( !strcmpW( attribute, szProgress ) )
638
    {
639
        DWORD func, val1, val2, units;
640

641 642 643
        func = MSI_RecordGetInteger( rec, 1 );
        val1 = MSI_RecordGetInteger( rec, 2 );
        val2 = MSI_RecordGetInteger( rec, 3 );
644

645
        TRACE("progress: func %u val1 %u val2 %u\n", func, val1, val2);
646

647
        units = val1 / 512;
648 649 650
        switch (func)
        {
        case 0: /* init */
651 652 653
            SendMessageW( ctrl->hwnd, PBM_SETRANGE, 0, MAKELPARAM(0,100) );
            if (val2)
            {
654 655
                ctrl->progress_max = units ? units : 100;
                ctrl->progress_current = units;
656 657 658 659 660
                ctrl->progress_backwards = TRUE;
                SendMessageW( ctrl->hwnd, PBM_SETPOS, 100, 0 );
            }
            else
            {
661
                ctrl->progress_max = units ? units : 100;
662 663 664 665
                ctrl->progress_current = 0;
                ctrl->progress_backwards = FALSE;
                SendMessageW( ctrl->hwnd, PBM_SETPOS, 0, 0 );
            }
666
            break;
667 668 669
        case 1: /* action data increment */
            if (val2) dialog->package->action_progress_increment = val1;
            else dialog->package->action_progress_increment = 0;
670 671
            break;
        case 2: /* move */
672 673
            if (ctrl->progress_backwards)
            {
674
                if (units >= ctrl->progress_current) ctrl->progress_current -= units;
675 676 677 678
                else ctrl->progress_current = 0;
            }
            else
            {
679
                if (ctrl->progress_current + units < ctrl->progress_max) ctrl->progress_current += units;
680 681 682
                else ctrl->progress_current = ctrl->progress_max;
            }
            SendMessageW( ctrl->hwnd, PBM_SETPOS, MulDiv(100, ctrl->progress_current, ctrl->progress_max), 0 );
683
            break;
684 685 686
        case 3: /* add */
            ctrl->progress_max += units;
            break;
687
        default:
688
            FIXME("Unknown progress message %u\n", func);
689 690
            break;
        }
691
    }
692
    else if ( !strcmpW( attribute, szProperty ) )
693 694
    {
        MSIFEATURE *feature = msi_seltree_get_selected_feature( ctrl );
695
        if (feature) msi_dialog_set_property( dialog->package, ctrl->property, feature->Directory );
696
    }
697
    else if ( !strcmpW( attribute, szSelectionPath ) )
698
    {
699 700
        BOOL indirect = ctrl->attributes & msidbControlAttributesIndirect;
        LPWSTR path = msi_dialog_dup_property( dialog, ctrl->property, indirect );
701
        if (!path) return;
702 703 704
        SetWindowTextW( ctrl->hwnd, path );
        msi_free(path);
    }
705 706 707
    else
    {
        FIXME("Attribute %s not being set\n", debugstr_w(attribute));
708
        return;
709
    }
710 711
}

712
static void msi_dialog_map_events(msi_dialog* dialog, LPCWSTR control)
713
{
714
    static const WCHAR Query[] = {
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
        'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
         '`','E','v','e','n','t','M','a','p','p','i','n','g','`',' ',
        'W','H','E','R','E',' ',
         '`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',' ',
        'A','N','D',' ',
         '`','C','o','n','t','r','o','l','_','`',' ','=',' ','\'','%','s','\'',0
    };
    MSIRECORD *row;
    LPCWSTR event, attribute;

    row = MSI_QueryGetRecord( dialog->package->db, Query, dialog->name, control );
    if (!row)
        return;

    event = MSI_RecordGetString( row, 3 );
    attribute = MSI_RecordGetString( row, 4 );
731
    ControlEvent_SubscribeToEvent( dialog->package, dialog, event, control, attribute );
732 733 734
    msiobj_release( &row->hdr );
}

735 736 737 738 739 740
/* everything except radio buttons */
static msi_control *msi_dialog_add_control( msi_dialog *dialog,
                MSIRECORD *rec, LPCWSTR szCls, DWORD style )
{
    DWORD attributes;
    LPCWSTR text, name;
741
    DWORD exstyle = 0;
742 743 744 745

    name = MSI_RecordGetString( rec, 2 );
    attributes = MSI_RecordGetInteger( rec, 8 );
    text = MSI_RecordGetString( rec, 10 );
746 747 748 749

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

750
    if( attributes & msidbControlAttributesVisible )
751
        style |= WS_VISIBLE;
752
    if( ~attributes & msidbControlAttributesEnabled )
753
        style |= WS_DISABLED;
754 755
    if( attributes & msidbControlAttributesSunken )
        exstyle |= WS_EX_CLIENTEDGE;
756 757 758

    msi_dialog_map_events(dialog, name);

759 760
    return msi_dialog_create_window( dialog, rec, exstyle, szCls, name,
                                     text, style, dialog->hwnd );
761 762
}

763 764
struct msi_text_info
{
765
    msi_font *font;
766 767 768 769
    WNDPROC oldproc;
    DWORD attributes;
};

770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
/*
 * 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 );
}

785 786 787 788
static LRESULT WINAPI
MSIText_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    struct msi_text_info *info;
789
    LRESULT r = 0;
790

791
    TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
792 793 794 795 796 797 798 799 800 801 802

    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);
803 804
    if ( info->font )
        SetTextColor( (HDC)wParam, info->font->color );
805 806 807

    switch( msg )
    {
808 809 810
    case WM_SETTEXT:
        msi_text_on_settext( hWnd );
        break;
811
    case WM_NCDESTROY:
812
        msi_free( info );
813 814 815 816 817 818 819
        RemovePropW( hWnd, szButtonData );
        break;
    }

    return r;
}

820 821
static UINT msi_dialog_text_control( msi_dialog *dialog, MSIRECORD *rec )
{
822 823
    msi_control *control;
    struct msi_text_info *info;
824
    LPCWSTR text, ptr, prop, control_name;
825
    LPWSTR font_name;
826

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

829 830 831 832
    control = msi_dialog_add_control( dialog, rec, szStatic, SS_LEFT | WS_GROUP );
    if( !control )
        return ERROR_FUNCTION_FAILED;

833
    info = msi_alloc( sizeof *info );
834 835 836
    if( !info )
        return ERROR_SUCCESS;

837
    control_name = MSI_RecordGetString( rec, 2 );
838 839 840 841
    control->attributes = MSI_RecordGetInteger( rec, 8 );
    prop = MSI_RecordGetString( rec, 9 );
    control->property = msi_dialog_dup_property( dialog, prop, FALSE );

842 843 844 845 846
    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 );

847 848 849 850 851 852 853 854
    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 );

855
    ControlEvent_SubscribeToEvent( dialog->package, dialog,
856
                                   szSelectionPath, control_name, szSelectionPath );
857

858 859 860
    return ERROR_SUCCESS;
}

861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
/* 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;
}

882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 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 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
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 );

    dialog->event_handler( dialog->package, event_fmt, arg_fmt, dialog );

    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[] = {
946 947 948 949 950 951
        '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;
952 953 954 955 956 957 958 959 960
    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");
961
        return ERROR_SUCCESS;
962
    }
Hans Leidekker's avatar
Hans Leidekker committed
963
    r = MSI_IterateRecords( view, 0, msi_dialog_control_event, dialog );
964 965 966 967
    msiobj_release( &view->hdr );
    return r;
}

968
static UINT msi_dialog_button_control( msi_dialog *dialog, MSIRECORD *rec )
969
{
970
    msi_control *control;
971
    UINT attributes, style;
972

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

975 976 977 978 979 980 981 982 983
    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;

984
    control->handler = msi_dialog_button_handler;
985

986 987 988 989 990 991 992 993 994 995 996 997 998
    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 );
    }
999

1000 1001
    return ERROR_SUCCESS;
}
1002

1003 1004
static LPWSTR msi_get_checkbox_value( msi_dialog *dialog, LPCWSTR prop )
{
1005
    static const WCHAR query[] = {
1006
        'S','E','L','E','C','T',' ','*',' ',
1007
        'F','R','O','M',' ','`','C','h','e','c','k','B','o','x','`',' ',
1008 1009 1010 1011 1012 1013 1014 1015
        '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 */
1016
    rec = MSI_QueryGetRecord( dialog->package->db, query, prop );
1017 1018 1019
    if (!rec)
        return ret;

1020 1021
    ret = msi_get_deformatted_field( dialog->package, rec, 2 );
    if( ret && !ret[0] )
1022
    {
1023 1024
        msi_free( ret );
        ret = NULL;
1025 1026 1027 1028 1029
    }
    msiobj_release( &rec->hdr );
    if (ret)
        return ret;

1030
    ret = msi_dup_property( dialog->package->db, prop );
1031 1032
    if( ret && !ret[0] )
    {
1033
        msi_free( ret );
1034 1035 1036 1037 1038 1039
        ret = NULL;
    }

    return ret;
}

1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 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
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 );
}

1093 1094 1095
static UINT msi_dialog_checkbox_control( msi_dialog *dialog, MSIRECORD *rec )
{
    msi_control *control;
1096
    LPCWSTR prop;
1097 1098 1099

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

1100
    control = msi_dialog_add_control( dialog, rec, szButton, BS_CHECKBOX | BS_MULTILINE | WS_TABSTOP );
1101
    control->handler = msi_dialog_checkbox_handler;
1102
    control->update = msi_dialog_checkbox_sync_state;
1103
    prop = MSI_RecordGetString( rec, 9 );
1104
    if (prop)
1105
    {
1106
        control->property = strdupW( prop );
1107
        control->value = msi_get_checkbox_value( dialog, prop );
1108
        TRACE("control %s value %s\n", debugstr_w(control->property), debugstr_w(control->value));
1109
    }
1110
    msi_dialog_checkbox_sync_state( dialog, control );
1111
    return ERROR_SUCCESS;
1112 1113
}

1114
static UINT msi_dialog_line_control( msi_dialog *dialog, MSIRECORD *rec )
1115
{
1116 1117 1118 1119 1120 1121
    DWORD attributes;
    LPCWSTR name;
    DWORD style, exstyle = 0;
    DWORD x, y, width, height;
    msi_control *control;

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

1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
    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;

    msi_dialog_map_events(dialog, name);

1138
    control = msi_alloc( FIELD_OFFSET(msi_control, name[strlenW( name ) + 1] ));
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
    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;
1154
    control->progress_backwards = FALSE;
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169

    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 );
1170

1171 1172 1173
    return ERROR_SUCCESS;
}

1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
/******************** 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;

1189
    TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1190 1191 1192 1193 1194 1195 1196

    info = GetPropW( hWnd, szButtonData );

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

    switch( msg )
    {
1197 1198
    case WM_GETDLGCODE:
        return DLGC_WANTARROWS;
1199 1200 1201 1202
    case WM_NCDESTROY:
        msi_free( info );
        RemovePropW( hWnd, szButtonData );
        break;
1203 1204
    case WM_PAINT:
        /* native MSI sets a wait cursor here */
1205 1206 1207 1208 1209 1210
        msi_dialog_button_handler( info->dialog, info->control, BN_CLICKED );
        break;
    }
    return r;
}

1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
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;

1229
    TRACE("%d/%d\n", info->offset, info->length);
1230 1231 1232 1233

    return 0;
}

1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
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 );
}

1252 1253
static UINT msi_dialog_scrolltext_control( msi_dialog *dialog, MSIRECORD *rec )
{
1254
    static const WCHAR szRichEdit20W[] = {'R','i','c','h','E','d','i','t','2','0','W',0};
1255
    struct msi_scrolltext_info *info;
1256
    msi_control *control;
1257
    HMODULE hRichedit;
1258
    LPCWSTR text;
1259
    DWORD style;
1260

1261 1262 1263 1264
    info = msi_alloc( sizeof *info );
    if (!info)
        return ERROR_FUNCTION_FAILED;

1265
    hRichedit = LoadLibraryA("riched20");
1266

1267 1268
    style = WS_BORDER | ES_MULTILINE | WS_VSCROLL |
            ES_READONLY | ES_AUTOVSCROLL | WS_TABSTOP;
1269
    control = msi_dialog_add_control( dialog, rec, szRichEdit20W, style );
1270
    if (!control)
1271
    {
1272
        FreeLibrary( hRichedit );
1273
        msi_free( info );
1274
        return ERROR_FUNCTION_FAILED;
1275
    }
1276

1277 1278
    control->hDll = hRichedit;

1279 1280
    info->dialog = dialog;
    info->control = control;
1281

1282 1283 1284 1285
    /* subclass the static control */
    info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
                                          (LONG_PTR)MSIScrollText_WndProc );
    SetPropW( control->hwnd, szButtonData, info );
1286

1287
    /* add the text into the richedit */
1288 1289 1290
    text = MSI_RecordGetString( rec, 10 );
    if (text)
        msi_scrolltext_add_text( control, text );
1291

1292 1293
    return ERROR_SUCCESS;
}
1294

1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 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
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;
}

1362 1363 1364 1365
static UINT msi_dialog_bitmap_control( msi_dialog *dialog, MSIRECORD *rec )
{
    UINT cx, cy, flags, style, attributes;
    msi_control *control;
1366
    LPWSTR name;
1367 1368 1369 1370 1371 1372 1373 1374 1375

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

    attributes = MSI_RecordGetInteger( rec, 8 );
    if( attributes & msidbControlAttributesFixedSize )
    {
        flags |= LR_DEFAULTSIZE;
        style |= SS_CENTERIMAGE;
1376
    }
1377 1378 1379 1380 1381 1382 1383

    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 );

1384 1385
    name = msi_get_binary_name( dialog->package, rec );
    control->hBitmap = msi_load_picture( dialog->package->db, name, cx, cy, flags );
1386 1387 1388 1389
    if( control->hBitmap )
        SendMessageW( control->hwnd, STM_SETIMAGE,
                      IMAGE_BITMAP, (LPARAM) control->hBitmap );
    else
1390
        ERR("Failed to load bitmap %s\n", debugstr_w(name));
1391

1392
    msi_free( name );
1393 1394
    
    return ERROR_SUCCESS;
1395 1396 1397 1398 1399
}

static UINT msi_dialog_icon_control( msi_dialog *dialog, MSIRECORD *rec )
{
    msi_control *control;
1400
    DWORD attributes;
1401
    LPWSTR name;
1402 1403 1404 1405 1406

    TRACE("\n");

    control = msi_dialog_add_control( dialog, rec, szStatic,
                            SS_ICON | SS_CENTERIMAGE | WS_GROUP );
1407
            
1408
    attributes = MSI_RecordGetInteger( rec, 8 );
1409 1410
    name = msi_get_binary_name( dialog->package, rec );
    control->hIcon = msi_load_icon( dialog->package->db, name, attributes );
1411 1412
    if( control->hIcon )
        SendMessageW( control->hwnd, STM_SETICON, (WPARAM) control->hIcon, 0 );
1413
    else
1414 1415
        ERR("Failed to load bitmap %s\n", debugstr_w(name));
    msi_free( name );
1416 1417 1418
    return ERROR_SUCCESS;
}

1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 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
/******************** 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[] = {
1480 1481
        'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
        '`','C','o','m','b','o','B','o','x','`',' ','W','H','E','R','E',' ',
1482
        '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',' ',
1483 1484 1485 1486
        'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','`',0};
    MSIQUERY *view;
    DWORD count;
    UINT r;
1487 1488 1489 1490 1491 1492 1493 1494

    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 );
1495 1496 1497 1498 1499
    if (r != ERROR_SUCCESS)
    {
        msiobj_release( &view->hdr );
        return r;
    }
1500 1501 1502 1503 1504 1505 1506 1507
    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;
}

1508
static UINT msi_dialog_set_control_condition( MSIRECORD *rec, LPVOID param )
1509
{
1510 1511 1512 1513 1514 1515 1516 1517 1518
    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;
1519

1520 1521 1522 1523 1524 1525 1526 1527
    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));
1528

1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
        /* 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));
    }
1543 1544 1545
    return ERROR_SUCCESS;
}

1546
static UINT msi_dialog_evaluate_control_conditions( msi_dialog *dialog )
1547
{
1548
    static const WCHAR query[] = {
1549
        'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1550
        'C','o','n','t','r','o','l','C','o','n','d','i','t','i','o','n',' ',
1551
        'W','H','E','R','E',' ','`','D','i','a','l','o','g','_','`',' ','=',' ','\'','%','s','\'',0};
1552
    UINT r;
1553
    MSIQUERY *view;
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 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
    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;
1597 1598 1599 1600
    DWORD j;

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

1601
    value = msi_dup_property( dialog->package->db, control->property );
1602 1603 1604 1605 1606 1607 1608 1609 1610
    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 );
1611
        if (!strcmpW( value, tmp ))
1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
            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);
}

1628 1629
static UINT msi_dialog_combo_control( msi_dialog *dialog, MSIRECORD *rec )
{
1630
    struct msi_combobox_info *info;
1631
    msi_control *control;
1632
    DWORD attributes, style;
1633 1634 1635 1636 1637
    LPCWSTR prop;

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

1639 1640 1641 1642 1643 1644 1645 1646 1647
    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;

1648 1649
    control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
    if (!control)
1650 1651
    {
        msi_free( info );
1652
        return ERROR_FUNCTION_FAILED;
1653 1654
    }

1655 1656 1657
    control->handler = msi_dialog_combobox_handler;
    control->update = msi_dialog_combobox_update;

1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
    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 );
1672

1673 1674
    msi_dialog_combobox_update( dialog, control );

1675 1676 1677
    return ERROR_SUCCESS;
}

1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
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;
}

1694 1695 1696
/* length of 2^32 + 1 */
#define MAX_NUM_DIGITS 11

1697 1698 1699
static UINT msi_dialog_edit_control( msi_dialog *dialog, MSIRECORD *rec )
{
    msi_control *control;
1700 1701
    LPCWSTR prop, text;
    LPWSTR val, begin, end;
1702
    WCHAR num[MAX_NUM_DIGITS];
1703
    DWORD limit;
1704

1705
    control = msi_dialog_add_control( dialog, rec, szEdit,
1706
                                      WS_BORDER | WS_TABSTOP | ES_AUTOHSCROLL );
1707
    control->handler = msi_dialog_edit_handler;
1708 1709 1710 1711 1712 1713 1714

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

1715 1716 1717
        if ( begin && end && end > begin &&
             begin[0] >= '0' && begin[0] <= '9' &&
             end - begin < MAX_NUM_DIGITS)
1718 1719 1720 1721 1722 1723 1724 1725
        {
            lstrcpynW( num, begin + 1, end - begin );
            limit = atolW( num );

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

1726 1727
    prop = MSI_RecordGetString( rec, 9 );
    if( prop )
1728
        control->property = strdupW( prop );
1729

1730
    val = msi_dup_property( dialog->package->db, control->property );
1731
    SetWindowTextW( control->hwnd, val );
1732
    msi_free( val );
1733 1734 1735
    return ERROR_SUCCESS;
}

1736 1737
/******************** Masked Edit ********************************************/

1738
#define MASK_MAX_GROUPS 20
1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758

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];
};

1759 1760 1761 1762 1763 1764 1765 1766 1767
static BOOL msi_mask_editable( WCHAR type )
{
    switch (type)
    {
    case '%':
    case '#':
    case '&':
    case '`':
    case '?':
1768
    case '^':
1769 1770 1771 1772 1773
        return TRUE;
    }
    return FALSE;
}

1774 1775 1776 1777 1778
static void msi_mask_control_change( struct msi_maskedit_info *info )
{
    LPWSTR val;
    UINT i, n, r;

1779
    val = msi_alloc( (info->num_chars+1)*sizeof(WCHAR) );
1780 1781 1782 1783 1784 1785 1786
    for( i=0, n=0; i<info->num_groups; i++ )
    {
        if( (info->group[i].len + n) > info->num_chars )
        {
            ERR("can't fit control %d text into template\n",i);
            break;
        }
1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798
        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;
        }
1799 1800 1801 1802 1803 1804 1805
        n += r;
    }

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

    if( i == info->num_groups )
    {
1806 1807
        TRACE("Set property %s to %s\n", debugstr_w(info->prop), debugstr_w(val));
        msi_dialog_set_property( info->dialog->package, info->prop, val );
1808 1809
        msi_dialog_evaluate_control_conditions( info->dialog );
    }
1810
    msi_free( val );
1811 1812
}

1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
/* 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 );
}

1835 1836 1837 1838 1839 1840
static LRESULT WINAPI
MSIMaskedEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    struct msi_maskedit_info *info;
    HRESULT r;

1841
    TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
1842 1843 1844 1845 1846 1847 1848 1849 1850

    info = GetPropW(hWnd, szButtonData);

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

    switch( msg )
    {
    case WM_COMMAND:
        if (HIWORD(wParam) == EN_CHANGE)
1851
        {
1852
            msi_mask_control_change( info );
1853 1854
            msi_mask_next_control( info, (HWND) lParam );
        }
1855 1856
        break;
    case WM_NCDESTROY:
1857 1858
        msi_free( info->prop );
        msi_free( info );
1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875
        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++ )
    {
1876
        if( info->group[i].len < strlenW( p ) )
1877 1878 1879 1880
        {
            LPWSTR chunk = strdupW( p );
            chunk[ info->group[i].len ] = 0;
            SetWindowTextW( info->group[i].hwnd, chunk );
1881
            msi_free( chunk );
1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902
        }
        else
        {
            SetWindowTextW( info->group[i].hwnd, p );
            break;
        }
        p += info->group[i].len;
    }
}

static struct msi_maskedit_info * msi_dialog_parse_groups( LPCWSTR mask )
{
    struct msi_maskedit_info * info = NULL;
    int i = 0, n = 0, total = 0;
    LPCWSTR p;

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

    if( !mask )
        return info;

1903
    info = msi_alloc_zero( sizeof *info );
1904 1905 1906
    if( !info )
        return info;

1907 1908 1909 1910 1911 1912
    p = strchrW(mask, '<');
    if( p )
        p++;
    else
        p = mask;

1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933
    for( i=0; i<MASK_MAX_GROUPS; i++ )
    {
        /* stop at the end of the string */
        if( p[0] == 0 || p[0] == '>' )
            break;

        /* 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;
    }

1934
    TRACE("%d characters in %d groups\n", total, i );
1935 1936 1937 1938 1939 1940 1941 1942 1943 1944
    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
1945
msi_maskedit_create_children( struct msi_maskedit_info *info, LPCWSTR font )
1946 1947 1948 1949 1950 1951
{
    DWORD width, height, style, wx, ww;
    RECT rect;
    HWND hwnd;
    UINT i;

1952
    style = WS_CHILD | WS_BORDER | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL;
1953 1954 1955 1956 1957 1958 1959 1960

    GetClientRect( info->hwnd, &rect );

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

    for( i = 0; i < info->num_groups; i++ )
    {
1961 1962
        if (!msi_mask_editable( info->group[i].type ))
            continue;
1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975
        wx = (info->group[i].ofs * width) / info->num_chars;
        ww = (info->group[i].len * width) / info->num_chars;

        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 );

1976 1977
        msi_dialog_set_font( info->dialog, hwnd,
                             font?font:info->dialog->default_font );
1978 1979 1980 1981
        info->group[i].hwnd = hwnd;
    }
}

1982 1983 1984
/*
 * office 2003 uses "73931<````=````=````=````=`````>@@@@@"
 * delphi 7 uses "<????-??????-??????-????>" and "<???-???>"
1985
 * filemaker pro 7 uses "<^^^^=^^^^=^^^^=^^^^=^^^^=^^^^=^^^^^>"
1986
 */
1987 1988
static UINT msi_dialog_maskedit_control( msi_dialog *dialog, MSIRECORD *rec )
{
1989
    LPWSTR font_mask, val = NULL, font;
1990 1991 1992
    struct msi_maskedit_info *info = NULL;
    UINT ret = ERROR_SUCCESS;
    msi_control *control;
1993
    LPCWSTR prop, mask;
1994

1995 1996
    TRACE("\n");

1997 1998
    font_mask = msi_get_deformatted_field( dialog->package, rec, 10 );
    font = msi_dialog_get_style( font_mask, &mask );
1999 2000
    if( !mask )
    {
2001
        WARN("mask template is empty\n");
2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
        goto end;
    }

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

    info->dialog = dialog;

2014 2015
    control = msi_dialog_add_control( dialog, rec, szStatic,
                   SS_OWNERDRAW | WS_GROUP | WS_VISIBLE );
2016 2017 2018 2019 2020 2021
    if( !control )
    {
        ERR("Failed to create maskedit container\n");
        ret = ERROR_FUNCTION_FAILED;
        goto end;
    }
2022
    SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034

    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 );

2035
    msi_maskedit_create_children( info, font );
2036 2037 2038

    if( prop )
    {
2039
        val = msi_dup_property( dialog->package->db, prop );
2040 2041 2042
        if( val )
        {
            msi_maskedit_set_text( info, val );
2043
            msi_free( val );
2044 2045 2046 2047 2048
        }
    }

end:
    if( ret != ERROR_SUCCESS )
2049
        msi_free( info );
2050 2051
    msi_free( font_mask );
    msi_free( font );
2052 2053 2054
    return ret;
}

2055 2056 2057 2058
/******************** Progress Bar *****************************************/

static UINT msi_dialog_progress_bar( msi_dialog *dialog, MSIRECORD *rec )
{
2059
    msi_control *control;
2060 2061 2062 2063 2064 2065
    DWORD attributes, style;

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

2067
    control = msi_dialog_add_control( dialog, rec, PROGRESS_CLASSW, style );
2068 2069 2070 2071 2072
    if( !control )
        return ERROR_FUNCTION_FAILED;

    ControlEvent_SubscribeToEvent( dialog->package, dialog,
                                   szSetProgress, control->name, szProgress );
2073 2074 2075
    return ERROR_SUCCESS;
}

2076 2077
/******************** Path Edit ********************************************/

2078 2079 2080 2081 2082 2083 2084 2085
struct msi_pathedit_info
{
    msi_dialog *dialog;
    msi_control *control;
    WNDPROC oldproc;
};

static void msi_dialog_update_pathedit( msi_dialog *dialog, msi_control *control )
2086
{
2087
    LPWSTR prop, path;
2088
    BOOL indirect;
2089

2090 2091 2092 2093 2094
    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 );
2095
    path = msi_dialog_dup_property( dialog, prop, TRUE );
2096

2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121
    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 )
{
    if ( !lstrlenW( path ) )
        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;
2122

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

    buf = msi_get_window_text( control->hwnd );
2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137

    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;
2138
        msi_dialog_set_property( dialog->package, prop, buf );
2139 2140 2141
    }

    msi_dialog_update_pathedit( dialog, control );
2142 2143 2144 2145 2146

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

    msi_free( buf );
2147
    msi_free( prop );
2148

2149
    return valid;
2150 2151
}

2152
static LRESULT WINAPI MSIPathEdit_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2153
{
2154 2155
    struct msi_pathedit_info *info = GetPropW(hWnd, szButtonData);
    LRESULT r = 0;
2156

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

2159 2160 2161 2162 2163 2164
    if ( msg == WM_KILLFOCUS )
    {
        /* if the path is invalid, don't handle this message */
        if ( !msi_dialog_onkillfocus( info->dialog, info->control ) )
            return 0;
    }
2165

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

2168 2169 2170 2171 2172 2173 2174
    if ( msg == WM_NCDESTROY )
    {
        msi_free( info );
        RemovePropW( hWnd, szButtonData );
    }

    return r;
2175 2176
}

2177 2178
static UINT msi_dialog_pathedit_control( msi_dialog *dialog, MSIRECORD *rec )
{
2179
    struct msi_pathedit_info *info;
2180 2181 2182
    msi_control *control;
    LPCWSTR prop;

2183 2184 2185 2186
    info = msi_alloc( sizeof *info );
    if (!info)
        return ERROR_FUNCTION_FAILED;

2187 2188 2189 2190
    control = msi_dialog_add_control( dialog, rec, szEdit,
                                      WS_BORDER | WS_TABSTOP );
    control->attributes = MSI_RecordGetInteger( rec, 8 );
    prop = MSI_RecordGetString( rec, 9 );
2191
    control->property = msi_dialog_dup_property( dialog, prop, FALSE );
2192

2193 2194 2195 2196 2197 2198
    info->dialog = dialog;
    info->control = control;
    info->oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
                                                 (LONG_PTR)MSIPathEdit_WndProc );
    SetPropW( control->hwnd, szButtonData, info );

2199
    msi_dialog_update_pathedit( dialog, control );
2200 2201

    return ERROR_SUCCESS;
2202 2203
}

2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215
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 );
}

2216
/* radio buttons are a bit different from normal controls */
2217
static UINT msi_dialog_create_radiobutton( MSIRECORD *rec, LPVOID param )
2218
{
2219
    radio_button_group_descr *group = param;
2220 2221
    msi_dialog *dialog = group->dialog;
    msi_control *control;
2222
    LPCWSTR prop, text, name;
2223
    DWORD style, attributes = group->attributes;
2224

2225
    style = WS_CHILD | BS_AUTORADIOBUTTON | BS_MULTILINE | WS_TABSTOP;
2226 2227
    name = MSI_RecordGetString( rec, 3 );
    text = MSI_RecordGetString( rec, 8 );
2228
    if( attributes & msidbControlAttributesVisible )
2229
        style |= WS_VISIBLE;
2230
    if( ~attributes & msidbControlAttributesEnabled )
2231 2232
        style |= WS_DISABLED;

2233
    control = msi_dialog_create_window( dialog, rec, 0, szButton, name, text,
2234
                                        style, group->parent->hwnd );
2235 2236
    if (!control)
        return ERROR_FUNCTION_FAILED;
2237 2238
    control->handler = msi_dialog_radiogroup_handler;

2239
    if (group->propval && !strcmpW( control->name, group->propval ))
2240 2241
        SendMessageW(control->hwnd, BM_SETCHECK, BST_CHECKED, 0);

2242 2243
    prop = MSI_RecordGetString( rec, 1 );
    if( prop )
2244
        control->property = strdupW( prop );
2245 2246 2247 2248

    return ERROR_SUCCESS;
}

2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273
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;
}

2274 2275 2276
static UINT msi_dialog_radiogroup_control( msi_dialog *dialog, MSIRECORD *rec )
{
    static const WCHAR query[] = {
2277 2278 2279
        '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};
2280 2281 2282
    UINT r;
    LPCWSTR prop;
    msi_control *control;
2283
    MSIQUERY *view;
2284 2285
    radio_button_group_descr group;
    MSIPACKAGE *package = dialog->package;
2286
    WNDPROC oldproc;
2287
    DWORD attr, style = WS_GROUP;
2288 2289 2290 2291 2292

    prop = MSI_RecordGetString( rec, 9 );

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

2293 2294 2295 2296 2297 2298
    attr = MSI_RecordGetInteger( rec, 8 );
    if (attr & msidbControlAttributesHasBorder)
        style |= BS_GROUPBOX;
    else
        style |= BS_OWNERDRAW;

2299
    /* Create parent group box to hold radio buttons */
2300
    control = msi_dialog_add_control( dialog, rec, szButton, style );
2301 2302
    if( !control )
        return ERROR_FUNCTION_FAILED;
2303

2304 2305 2306
    oldproc = (WNDPROC) SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
                                           (LONG_PTR)MSIRadioGroup_WndProc );
    SetPropW(control->hwnd, szButtonData, oldproc);
2307
    SetWindowLongPtrW( control->hwnd, GWL_EXSTYLE, WS_EX_CONTROLPARENT );
2308 2309

    if( prop )
2310
        control->property = strdupW( prop );
2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323

    /* 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;
    group.attributes = MSI_RecordGetInteger( rec, 8 );
2324
    group.propval = msi_dup_property( dialog->package->db, control->property );
2325 2326 2327

    r = MSI_IterateRecords( view, 0, msi_dialog_create_radiobutton, &group );
    msiobj_release( &view->hdr );
2328
    msi_free( group.propval );
2329 2330 2331
    return r;
}

2332 2333 2334 2335
static void
msi_seltree_sync_item_state( HWND hwnd, MSIFEATURE *feature, HTREEITEM hItem )
{
    TVITEMW tvi;
2336
    DWORD index = feature->ActionRequest;
2337 2338 2339 2340

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

2341 2342 2343
    if (index == INSTALLSTATE_UNKNOWN)
        index = INSTALLSTATE_ABSENT;

2344 2345
    tvi.mask = TVIF_STATE;
    tvi.hItem = hItem;
2346
    tvi.state = INDEXTOSTATEIMAGEMASK( index );
2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362
    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");
2363
    AppendMenuA( hMenu, MF_ENABLED, USER_INSTALLSTATE_ALL, "Install entire feature");
2364 2365 2366 2367 2368 2369 2370 2371
    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;
}

2372 2373 2374 2375
static void
msi_seltree_update_feature_installstate( HWND hwnd, HTREEITEM hItem,
        MSIPACKAGE *package, MSIFEATURE *feature, INSTALLSTATE state )
{
2376
    feature->ActionRequest = state;
2377
    msi_seltree_sync_item_state( hwnd, feature, hItem );
2378
    ACTION_UpdateComponentStates( package, feature );
2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402
}

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 )));
}

2403 2404 2405
static LRESULT
msi_seltree_menu( HWND hwnd, HTREEITEM hItem )
{
2406
    struct msi_selection_tree_info *info;
2407
    MSIFEATURE *feature;
2408
    MSIPACKAGE *package;
2409 2410 2411 2412 2413 2414 2415
    union {
        RECT rc;
        POINT pt[2];
        HTREEITEM hItem;
    } u;
    UINT r;

2416 2417 2418
    info = GetPropW(hwnd, szButtonData);
    package = info->dialog->package;

2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434
    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)
    {
2435 2436 2437
    case USER_INSTALLSTATE_ALL:
        r = INSTALLSTATE_LOCAL;
        /* fall-through */
2438 2439
    case INSTALLSTATE_ADVERTISED:
    case INSTALLSTATE_ABSENT:
2440 2441 2442 2443 2444 2445 2446 2447 2448
        {
            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 );
2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461
        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;

2462
    TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
2463 2464 2465 2466 2467 2468

    info = GetPropW(hWnd, szButtonData);

    switch( msg )
    {
    case WM_LBUTTONDOWN:
2469 2470
        tvhti.pt.x = (short)LOWORD( lParam );
        tvhti.pt.y = (short)HIWORD( lParam );
2471 2472
        tvhti.flags = 0;
        tvhti.hItem = 0;
2473
        CallWindowProcW(info->oldproc, hWnd, TVM_HITTEST, 0, (LPARAM) &tvhti );
2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489
        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;
}

2490
static void
2491 2492
msi_seltree_add_child_features( MSIPACKAGE *package, HWND hwnd,
                                LPCWSTR parent, HTREEITEM hParent )
2493
{
2494
    struct msi_selection_tree_info *info = GetPropW( hwnd, szButtonData );
2495 2496
    MSIFEATURE *feature;
    TVINSERTSTRUCTW tvis;
2497
    HTREEITEM hitem, hfirst = NULL;
2498 2499 2500

    LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
    {
2501 2502 2503 2504 2505
        if ( parent && feature->Feature_Parent && strcmpW( parent, feature->Feature_Parent ))
            continue;
        else if ( parent && !feature->Feature_Parent )
            continue;
        else if ( !parent && feature->Feature_Parent )
2506 2507 2508 2509 2510
            continue;

        if ( !feature->Title )
            continue;

2511 2512 2513
        if ( !feature->Display )
            continue;

2514 2515
        memset( &tvis, 0, sizeof tvis );
        tvis.hParent = hParent;
2516
        tvis.hInsertAfter = TVI_LAST;
2517 2518
        tvis.u.item.mask = TVIF_TEXT | TVIF_PARAM;
        tvis.u.item.pszText = feature->Title;
2519
        tvis.u.item.lParam = (LPARAM) feature;
2520

2521 2522 2523 2524
        hitem = (HTREEITEM) SendMessageW( hwnd, TVM_INSERTITEMW, 0, (LPARAM) &tvis );
        if (!hitem)
            continue;

2525 2526 2527
        if (!hfirst)
            hfirst = hitem;

2528 2529 2530
        msi_seltree_sync_item_state( hwnd, feature, hitem );
        msi_seltree_add_child_features( package, hwnd,
                                        feature->Feature, hitem );
2531 2532 2533 2534

        /* the node is expanded if Display is odd */
        if ( feature->Display % 2 != 0 )
            SendMessageW( hwnd, TVM_EXPAND, TVE_EXPAND, (LPARAM) hitem );
2535
    }
2536 2537 2538

    /* select the first item */
    SendMessageW( hwnd, TVM_SELECTITEM, TVGN_CARET | TVGN_DROPHILITE, (LPARAM) hfirst );
2539
    info->selected = hfirst;
2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573
}

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 );
2574
    }
2575 2576

    SendMessageW( hwnd, TVM_SETIMAGELIST, TVSIL_STATE, (LPARAM)himl );
2577 2578
}

2579 2580 2581
static UINT msi_dialog_seltree_handler( msi_dialog *dialog,
                                        msi_control *control, WPARAM param )
{
2582
    struct msi_selection_tree_info *info = GetPropW( control->hwnd, szButtonData );
2583 2584 2585
    LPNMTREEVIEWW tv = (LPNMTREEVIEWW)param;
    MSIRECORD *row, *rec;
    MSIFOLDER *folder;
2586 2587
    MSIFEATURE *feature;
    LPCWSTR dir, title = NULL;
2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598
    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;

2599 2600
    info->selected = tv->itemNew.hItem;

2601 2602 2603 2604 2605 2606 2607 2608 2609 2610
    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 );
2611 2612 2613 2614 2615 2616 2617 2618 2619
    if (!row)
        return ERROR_FUNCTION_FAILED;

    rec = MSI_CreateRecord( 1 );

    MSI_RecordSetStringW( rec, 1, MSI_RecordGetString( row, 4 ) );
    ControlEvent_FireSubscribedEvent( dialog->package, szSelectionDescription, rec );

    dir = MSI_RecordGetString( row, 7 );
2620
    if (dir)
2621
    {
2622
        folder = msi_get_loaded_folder( dialog->package, dir );
2623 2624 2625 2626 2627 2628
        if (!folder)
        {
            r = ERROR_FUNCTION_FAILED;
            goto done;
        }
        MSI_RecordSetStringW( rec, 1, folder->ResolvedTarget );
2629
    }
2630 2631
    else
        MSI_RecordSetStringW( rec, 1, NULL );
2632 2633 2634 2635 2636 2637 2638 2639 2640 2641

    ControlEvent_FireSubscribedEvent( dialog->package, szSelectionPath, rec );

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

    return r;
}

2642 2643 2644
static UINT msi_dialog_selection_tree( msi_dialog *dialog, MSIRECORD *rec )
{
    msi_control *control;
2645
    LPCWSTR prop, control_name;
2646
    MSIPACKAGE *package = dialog->package;
2647 2648 2649 2650 2651 2652
    DWORD style;
    struct msi_selection_tree_info *info;

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

2654 2655 2656 2657
    /* 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 );
2658
    if (!control)
2659 2660
    {
        msi_free(info);
2661
        return ERROR_FUNCTION_FAILED;
2662
    }
2663

2664
    control->handler = msi_dialog_seltree_handler;
2665
    control_name = MSI_RecordGetString( rec, 2 );
2666 2667 2668 2669
    control->attributes = MSI_RecordGetInteger( rec, 8 );
    prop = MSI_RecordGetString( rec, 9 );
    control->property = msi_dialog_dup_property( dialog, prop, FALSE );

2670 2671 2672 2673 2674 2675
    /* 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 );
2676

2677
    ControlEvent_SubscribeToEvent( dialog->package, dialog,
2678
                                   szSelectionPath, control_name, szProperty );
2679

2680 2681 2682
    /* initialize it */
    msi_seltree_create_imagelist( control->hwnd );
    msi_seltree_add_child_features( package, control->hwnd, NULL, NULL );
2683 2684 2685 2686

    return ERROR_SUCCESS;
}

2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701
/******************** 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;
}

2702 2703 2704 2705 2706 2707 2708 2709
/******************** List Box ***************************************/

struct msi_listbox_info
{
    msi_dialog *dialog;
    HWND hwnd;
    WNDPROC oldproc;
    DWORD num_items;
2710
    DWORD addpos_items;
2711
    LPWSTR *items;
2712 2713 2714 2715 2716 2717 2718 2719
};

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

2720
    TRACE("%p %04x %08lx %08lx\n", hWnd, msg, wParam, lParam);
2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731

    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++)
2732
            msi_free( info->items[j] );
2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744
        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;
2745
    LPCWSTR value, text;
2746
    int pos;
2747 2748 2749 2750

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

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

2753
    pos = SendMessageW( info->hwnd, LB_ADDSTRING, 0, (LPARAM)text );
2754 2755
    SendMessageW( info->hwnd, LB_SETITEMDATA, pos, (LPARAM)info->items[info->addpos_items] );
    info->addpos_items++;
2756 2757 2758
    return ERROR_SUCCESS;
}

2759
static UINT msi_listbox_add_items( struct msi_listbox_info *info, LPCWSTR property )
2760 2761
{
    static const WCHAR query[] = {
2762 2763
        'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
        '`','L','i','s','t','B','o','x','`',' ','W','H','E','R','E',' ',
2764
        '`','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',' ',
2765 2766 2767 2768
        'O','R','D','E','R',' ','B','Y',' ','`','O','r','d','e','r','`',0};
    MSIQUERY *view;
    DWORD count;
    UINT r;
2769

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

    /* just get the number of records */
2775
    count = 0;
2776
    r = MSI_IterateRecords( view, &count, NULL, NULL );
2777 2778 2779 2780 2781
    if (r != ERROR_SUCCESS)
    {
        msiobj_release( &view->hdr );
        return r;
    }
2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794
    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;
2795
    LPCWSTR value;
2796 2797 2798 2799 2800 2801

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

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

2804
    msi_dialog_set_property( info->dialog->package, control->property, value );
2805 2806 2807 2808 2809 2810 2811 2812 2813
    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;
2814
    DWORD attributes, style;
2815
    LPCWSTR prop;
2816 2817 2818 2819 2820

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

2821 2822 2823 2824 2825
    style = WS_TABSTOP | WS_GROUP | WS_CHILD | LBS_NOTIFY | WS_VSCROLL | WS_BORDER;
    attributes = MSI_RecordGetInteger( rec, 8 );
    if (~attributes & msidbControlAttributesSorted)
        style |= LBS_SORT;

2826 2827
    control = msi_dialog_add_control( dialog, rec, WC_LISTBOXW, style );
    if (!control)
2828 2829
    {
        msi_free(info);
2830
        return ERROR_FUNCTION_FAILED;
2831
    }
2832 2833 2834

    control->handler = msi_dialog_listbox_handler;

2835 2836 2837
    prop = MSI_RecordGetString( rec, 9 );
    control->property = msi_dialog_dup_property( dialog, prop, FALSE );

2838 2839 2840 2841
    /* subclass */
    info->dialog = dialog;
    info->hwnd = control->hwnd;
    info->items = NULL;
2842
    info->addpos_items = 0;
2843 2844 2845 2846
    info->oldproc = (WNDPROC)SetWindowLongPtrW( control->hwnd, GWLP_WNDPROC,
                                                (LONG_PTR)MSIListBox_WndProc );
    SetPropW( control->hwnd, szButtonData, info );

2847 2848
    if ( control->property )
        msi_listbox_add_items( info, control->property );
2849 2850 2851 2852

    return ERROR_SUCCESS;
}

2853 2854
/******************** Directory Combo ***************************************/

2855
static void msi_dialog_update_directory_combo( msi_dialog *dialog, msi_control *control )
2856 2857 2858 2859
{
    LPWSTR prop, path;
    BOOL indirect;

2860
    if (!control && !(control = msi_dialog_find_control_by_type( dialog, szDirectoryCombo )))
2861 2862
        return;

2863 2864
    indirect = control->attributes & msidbControlAttributesIndirect;
    prop = msi_dialog_dup_property( dialog, control->property, indirect );
2865
    path = msi_dialog_dup_property( dialog, prop, TRUE );
2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876

    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 );
}

2877 2878 2879
static UINT msi_dialog_directory_combo( msi_dialog *dialog, MSIRECORD *rec )
{
    msi_control *control;
2880
    LPCWSTR prop;
2881 2882
    DWORD style;

2883 2884 2885
    /* FIXME: use CBS_OWNERDRAWFIXED and add owner draw code */
    style = CBS_DROPDOWNLIST | CBS_HASSTRINGS | WS_CHILD |
            WS_GROUP | WS_TABSTOP | WS_VSCROLL;
2886 2887 2888 2889
    control = msi_dialog_add_control( dialog, rec, WC_COMBOBOXW, style );
    if (!control)
        return ERROR_FUNCTION_FAILED;

2890 2891 2892 2893
    control->attributes = MSI_RecordGetInteger( rec, 8 );
    prop = MSI_RecordGetString( rec, 9 );
    control->property = msi_dialog_dup_property( dialog, prop, FALSE );

2894
    msi_dialog_update_directory_combo( dialog, control );
2895

2896 2897 2898
    return ERROR_SUCCESS;
}

2899 2900
/******************** Directory List ***************************************/

2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919
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 );
2920
    path = msi_dialog_dup_property( dialog, prop, TRUE );
2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933

    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;

2934
        if ( !strcmpW( wfd.cFileName, szDot ) || !strcmpW( wfd.cFileName, szDotDot ) )
2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950
            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 );
}

2951 2952 2953 2954 2955 2956
UINT msi_dialog_directorylist_up( msi_dialog *dialog )
{
    msi_control *control;
    LPWSTR prop, path, ptr;
    BOOL indirect;

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

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

2967
    msi_dialog_set_property( dialog->package, prop, path );
2968

2969
    msi_dialog_update_directory_list( dialog, NULL );
2970 2971
    msi_dialog_update_directory_combo( dialog, NULL );
    msi_dialog_update_pathedit( dialog, NULL );
2972

2973 2974 2975 2976 2977 2978
    msi_free( path );
    msi_free( prop );

    return ERROR_SUCCESS;
}

2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006
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 );
3007
    path = msi_dialog_dup_property( dialog, prop, TRUE );
3008 3009 3010

    lstrcpyW( new_path, path );
    lstrcatW( new_path, text );
3011
    lstrcatW( new_path, szBackSlash );
3012

3013
    msi_dialog_set_property( dialog->package, prop, new_path );
3014 3015 3016 3017 3018 3019 3020 3021 3022 3023

    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;
}

3024 3025 3026
static UINT msi_dialog_directory_list( msi_dialog *dialog, MSIRECORD *rec )
{
    msi_control *control;
3027
    LPCWSTR prop;
3028 3029
    DWORD style;

3030
    style = LVS_LIST | WS_VSCROLL | LVS_SHAREIMAGELISTS |
3031 3032 3033 3034 3035 3036
            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;

3037
    control->attributes = MSI_RecordGetInteger( rec, 8 );
3038
    control->handler = msi_dialog_dirlist_handler;
3039 3040 3041
    prop = MSI_RecordGetString( rec, 9 );
    control->property = msi_dialog_dup_property( dialog, prop, FALSE );

3042 3043 3044 3045
    /* double click to activate an item in the list */
    SendMessageW( control->hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE,
                  0, LVS_EX_TWOCLICKACTIVATE );

3046 3047
    msi_dialog_update_directory_list( dialog, control );

3048 3049 3050
    return ERROR_SUCCESS;
}

3051 3052
/******************** VolumeCost List ***************************************/

3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063
static BOOL str_is_number( LPCWSTR str )
{
    int i;

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

    return TRUE;
}

3064
static const WCHAR column_keys[][80] =
3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076
{
    {'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;
3077
    WCHAR *num;
3078 3079 3080 3081 3082
    LVCOLUMNW lvc;
    DWORD count = 0;

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

3083 3084
    if (!text) return;

3085 3086 3087 3088 3089
    while ((begin = strchrW( begin, '{' )) && count < 5)
    {
        if (!(end = strchrW( begin, '}' )))
            return;

3090 3091 3092 3093
        num = msi_alloc( (end-begin+1)*sizeof(WCHAR) );
        if (!num)
            return;

3094 3095 3096 3097
        lstrcpynW( num, begin + 1, end - begin );
        begin += end - begin + 1;

        /* empty braces or '0' hides the column */ 
3098
        if ( !num[0] || !strcmpW( num, szZero ) )
3099 3100
        {
            count++;
3101
            msi_free( num );
3102 3103 3104 3105 3106 3107
            continue;
        }

        /* the width must be a positive number
         * if a width is invalid, all remaining columns are hidden
         */
3108 3109
        if ( !strncmpW( num, negative, 1 ) || !str_is_number( num ) ) {
            msi_free( num );
3110
            return;
3111
        }
3112

3113 3114
        ZeroMemory( &lvc, sizeof(lvc) );
        lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
3115
        lvc.cx = atolW( num );
3116
        lvc.pszText = msi_dialog_get_uitext( dialog, column_keys[count] );
3117

3118
        SendMessageW( control->hwnd,  LVM_INSERTCOLUMNW, count++, (LPARAM)&lvc );
3119
        msi_free( lvc.pszText );
3120
        msi_free( num );
3121 3122 3123
    }
}

3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147
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;
}

3148 3149
static void msi_dialog_vcl_add_drives( msi_dialog *dialog, msi_control *control )
{
3150
    ULARGE_INTEGER total, free;
3151
    LONGLONG difference, cost;
3152
    WCHAR size_text[MAX_PATH];
3153
    WCHAR cost_text[MAX_PATH];
3154 3155 3156 3157 3158
    LPWSTR drives, ptr;
    LVITEMW lvitem;
    DWORD size;
    int i = 0;

3159 3160 3161
    cost = msi_vcl_get_cost(dialog);
    StrFormatByteSizeW(cost, cost_text, MAX_PATH);

3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179
    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 );

3180
        GetDiskFreeSpaceExW(ptr, &free, &total, NULL);
3181
        difference = free.QuadPart - cost;
3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194

        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 );

3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205
        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 );

3206 3207 3208 3209 3210 3211 3212
        ptr += lstrlenW(ptr) + 1;
        i++;
    }

    msi_free( drives );
}

3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224
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;

3225
    msi_dialog_vcl_add_columns( dialog, control, rec );
3226
    msi_dialog_vcl_add_drives( dialog, control );
3227

3228 3229 3230
    return ERROR_SUCCESS;
}

3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255
/******************** 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 );

3256
    msi_dialog_set_property( dialog->package, prop, text );
3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 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 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308

    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;
}

3309
static const struct control_handler msi_dialog_handler[] =
3310 3311
{
    { szText, msi_dialog_text_control },
3312
    { szPushButton, msi_dialog_button_control },
3313 3314
    { szLine, msi_dialog_line_control },
    { szBitmap, msi_dialog_bitmap_control },
3315 3316
    { szCheckBox, msi_dialog_checkbox_control },
    { szScrollableText, msi_dialog_scrolltext_control },
3317 3318
    { szComboBox, msi_dialog_combo_control },
    { szEdit, msi_dialog_edit_control },
3319
    { szMaskedEdit, msi_dialog_maskedit_control },
3320
    { szPathEdit, msi_dialog_pathedit_control },
3321
    { szProgressBar, msi_dialog_progress_bar },
3322
    { szRadioButtonGroup, msi_dialog_radiogroup_control },
3323
    { szIcon, msi_dialog_icon_control },
3324
    { szSelectionTree, msi_dialog_selection_tree },
3325
    { szGroupBox, msi_dialog_group_box },
3326
    { szListBox, msi_dialog_list_box },
3327
    { szDirectoryCombo, msi_dialog_directory_combo },
3328
    { szDirectoryList, msi_dialog_directory_list },
3329
    { szVolumeCostList, msi_dialog_volumecost_list },
3330
    { szVolumeSelectCombo, msi_dialog_volumeselect_combo },
3331 3332 3333 3334 3335 3336
};

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

static UINT msi_dialog_create_controls( MSIRECORD *rec, LPVOID param )
{
3337
    msi_dialog *dialog = param;
3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353
    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;
}

3354
static UINT msi_dialog_fill_controls( msi_dialog *dialog )
3355 3356
{
    static const WCHAR query[] = {
3357 3358 3359
        '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};
3360
    UINT r;
3361
    MSIQUERY *view;
3362 3363
    MSIPACKAGE *package = dialog->package;

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

    /* query the Control table for all the elements of the control */
3367
    r = MSI_OpenQuery( package->db, &view, query, dialog->name );
3368 3369
    if( r != ERROR_SUCCESS )
    {
3370
        ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
3371 3372 3373
        return ERROR_INVALID_PARAMETER;
    }

3374 3375 3376 3377 3378
    r = MSI_IterateRecords( view, 0, msi_dialog_create_controls, dialog );
    msiobj_release( &view->hdr );
    return r;
}

3379 3380 3381 3382 3383 3384
UINT msi_dialog_reset( msi_dialog *dialog )
{
    /* FIXME: should restore the original values of any properties we changed */
    return msi_dialog_evaluate_control_conditions( dialog );
}

3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400
/* 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 );
3401
        lf.lfHeight = MulDiv(12, GetDeviceCaps(hdc, LOGPIXELSY), 72);
3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417
        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;
}

3418 3419
/* fetch the associated record from the Dialog table */
static MSIRECORD *msi_get_dialog_record( msi_dialog *dialog )
3420 3421 3422 3423 3424 3425 3426 3427 3428
{
    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;

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

3431 3432
    rec = MSI_QueryGetRecord( package->db, query, dialog->name );
    if( !rec )
3433
        WARN("query failed for dialog %s\n", debugstr_w(dialog->name));
3434

3435 3436 3437
    return rec;
}

3438
static void msi_dialog_adjust_dialog_pos( msi_dialog *dialog, MSIRECORD *rec, LPRECT pos )
3439
{
3440 3441 3442 3443
    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;
3444 3445
    POINT center;
    SIZE sz;
3446
    LONG style;
3447 3448 3449 3450 3451 3452 3453 3454 3455

    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 );
3456

3457 3458
    xres = msi_get_property_int( dialog->package->db, szScreenX, 0 );
    yres = msi_get_property_int( dialog->package->db, szScreenY, 0 );
3459

3460 3461
    center.x = MulDiv( center.x, xres, 100 );
    center.y = MulDiv( center.y, yres, 100 );
3462

3463
    /* turn the client pos into the window rectangle */
3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484
    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;
3485

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

3488
    style = GetWindowLongPtrW( dialog->hwnd, GWL_STYLE );
3489
    AdjustWindowRect( pos, style, FALSE );
3490 3491
}

3492
static void msi_dialog_set_tab_order( msi_dialog *dialog, LPCWSTR first )
3493
{
3494 3495 3496
    struct list tab_chain;
    msi_control *control;
    HWND prev = HWND_TOP;
3497

3498 3499
    list_init( &tab_chain );
    if (!(control = msi_dialog_find_control( dialog, first ))) return;
3500

3501 3502
    dialog->hWndFocus = control->hwnd;
    while (control)
3503
    {
3504 3505 3506 3507
        list_remove( &control->entry );
        list_add_tail( &tab_chain, &control->entry );
        if (!control->tabnext) break;
        control = msi_dialog_find_control( dialog, control->tabnext );
3508 3509
    }

3510 3511 3512 3513 3514 3515 3516
    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;
    }
3517

3518 3519
    /* put them back on the main list */
    list_move_head( &dialog->controls, &tab_chain );
3520 3521
}

3522 3523 3524 3525
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 };
3526 3527
    static const WCHAR dfv[] = {
        'M','S',' ','S','h','e','l','l',' ','D','l','g',0 };
3528
    msi_dialog *dialog = cs->lpCreateParams;
3529 3530
    MSIRECORD *rec = NULL;
    LPWSTR title = NULL;
3531
    RECT pos;
3532 3533 3534 3535 3536 3537 3538

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

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

    rec = msi_get_dialog_record( dialog );
3539 3540
    if( !rec )
    {
3541 3542
        TRACE("No record found for dialog %s\n", debugstr_w(dialog->name));
        return -1;
3543 3544 3545 3546
    }

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

3547
    msi_dialog_adjust_dialog_pos( dialog, rec, &pos );
3548

3549
    dialog->attributes = MSI_RecordGetInteger( rec, 6 );
3550

3551
    dialog->default_font = msi_dup_property( dialog->package->db, df );
3552 3553 3554
    if (!dialog->default_font)
    {
        dialog->default_font = strdupW(dfv);
3555
        msiobj_release( &rec->hdr );
3556 3557
        if (!dialog->default_font) return -1;
    }
Mike McCormack's avatar
Mike McCormack committed
3558

3559
    title = msi_get_deformatted_field( dialog->package, rec, 7 );
3560
    SetWindowTextW( hwnd, title );
3561
    msi_free( title );
3562

3563 3564
    SetWindowPos( hwnd, 0, pos.left, pos.top,
                  pos.right - pos.left, pos.bottom - pos.top,
3565
                  SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW );
3566

3567 3568 3569
    msi_dialog_build_font_list( dialog );
    msi_dialog_fill_controls( dialog );
    msi_dialog_evaluate_control_conditions( dialog );
3570
    msi_dialog_set_tab_order( dialog, MSI_RecordGetString( rec, 8 ) );
3571
    msiobj_release( &rec->hdr );
3572 3573 3574 3575

    return 0;
}

3576
static LRESULT msi_dialog_oncommand( msi_dialog *dialog, WPARAM param, HWND hwnd )
3577
{
3578
    msi_control *control = NULL;
3579

3580
    TRACE("%p %p %08lx\n", dialog, hwnd, param);
3581

3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593
    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 );
    }

3594 3595
    if( control )
    {
3596
        if( control->handler )
3597
        {
3598
            control->handler( dialog, control, param );
3599 3600
            msi_dialog_evaluate_control_conditions( dialog );
        }
3601
    }
3602

3603 3604 3605
    return 0;
}

3606 3607 3608 3609 3610
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 );

3611
    TRACE("%p %p\n", dialog, nmhdr->hwndFrom);
3612 3613 3614 3615 3616 3617 3618

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

    return 0;
}

3619 3620 3621 3622 3623 3624 3625 3626 3627 3628
static void msi_dialog_setfocus( msi_dialog *dialog )
{
    HWND hwnd = dialog->hWndFocus;

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

3629 3630 3631
static LRESULT WINAPI MSIDialog_WndProc( HWND hwnd, UINT msg,
                WPARAM wParam, LPARAM lParam )
{
3632
    msi_dialog *dialog = (LPVOID) GetWindowLongPtrW( hwnd, GWLP_USERDATA );
3633

3634 3635
    TRACE("0x%04x\n", msg);

3636 3637
    switch (msg)
    {
3638 3639 3640 3641
    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;
3642

3643 3644 3645 3646
    case WM_CREATE:
        return msi_dialog_oncreate( hwnd, (LPCREATESTRUCTW)lParam );

    case WM_COMMAND:
3647
        return msi_dialog_oncommand( dialog, wParam, (HWND)lParam );
3648

3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659
    case WM_ACTIVATE:
        if( LOWORD(wParam) == WA_INACTIVE )
            dialog->hWndFocus = GetFocus();
        else
            msi_dialog_setfocus( dialog );
        return 0;

    case WM_SETFOCUS:
        msi_dialog_setfocus( dialog );
        return 0;

3660 3661 3662 3663
    /* bounce back to our subclassed static control */
    case WM_CTLCOLORSTATIC:
        return SendMessageW( (HWND) lParam, WM_CTLCOLORSTATIC, wParam, lParam );

3664 3665 3666
    case WM_DESTROY:
        dialog->hwnd = NULL;
        return 0;
3667 3668
    case WM_NOTIFY:
        return msi_dialog_onnotify( dialog, lParam );
3669 3670 3671 3672
    }
    return DefWindowProcW(hwnd, msg, wParam, lParam);
}

3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690
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:
        return msi_dialog_run_message_loop( dialog );
    case WM_MSI_DIALOG_DESTROY:
        msi_dialog_destroy( dialog );
        return 0;
    }
    return DefWindowProcW( hwnd, msg, wParam, lParam );
}

3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722
static BOOL msi_dialog_register_class( void )
{
    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;
}

3723 3724
/* functions that interface to other modules within MSI */

3725 3726 3727
msi_dialog *msi_dialog_create( MSIPACKAGE* package,
                               LPCWSTR szDialogName, msi_dialog *parent,
                               msi_dialog_event_handler event_handler )
3728
{
3729
    MSIRECORD *rec = NULL;
3730
    msi_dialog *dialog;
3731 3732 3733

    TRACE("%p %s\n", package, debugstr_w(szDialogName));

3734 3735 3736
    if (!hMsiHiddenWindow)
        msi_dialog_register_class();

3737
    /* allocate the structure for the dialog to use */
3738
    dialog = msi_alloc_zero( FIELD_OFFSET( msi_dialog, name[strlenW( szDialogName ) + 1] ));
3739 3740 3741
    if( !dialog )
        return NULL;
    strcpyW( dialog->name, szDialogName );
3742
    dialog->parent = parent;
3743
    msiobj_addref( &package->hdr );
3744 3745
    dialog->package = package;
    dialog->event_handler = event_handler;
3746
    dialog->finished = 0;
3747
    list_init( &dialog->controls );
3748
    list_init( &dialog->fonts );
3749

3750 3751 3752
    /* verify that the dialog exists */
    rec = msi_get_dialog_record( dialog );
    if( !rec )
3753
    {
3754
        msiobj_release( &package->hdr );
3755
        msi_free( dialog );
3756 3757
        return NULL;
    }
3758
    dialog->attributes = MSI_RecordGetInteger( rec, 6 );
3759 3760
    dialog->control_default = strdupW( MSI_RecordGetString( rec, 9 ) );
    dialog->control_cancel = strdupW( MSI_RecordGetString( rec, 10 ) );
3761
    msiobj_release( &rec->hdr );
3762 3763 3764 3765

    return dialog;
}

3766
static void msi_process_pending_messages( HWND hdlg )
3767 3768 3769 3770 3771
{
    MSG msg;

    while( PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ) )
    {
3772 3773
        if( hdlg && IsDialogMessageW( hdlg, &msg ))
            continue;
3774 3775 3776 3777 3778
        TranslateMessage( &msg );
        DispatchMessageW( &msg );
    }
}

3779 3780
void msi_dialog_end_dialog( msi_dialog *dialog )
{
3781
    TRACE("%p\n", dialog);
3782
    dialog->finished = 1;
3783
    PostMessageW(dialog->hwnd, WM_NULL, 0, 0);
3784 3785
}

3786
void msi_dialog_check_messages( HANDLE handle )
3787
{
3788
    DWORD r;
3789

3790 3791
    /* in threads other than the UI thread, block */
    if( uiThreadId != GetCurrentThreadId() )
3792
    {
3793 3794 3795 3796 3797 3798 3799 3800 3801 3802
        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 );
            }
        }
3803
        return;
3804 3805
    }

3806 3807
    /* there's two choices for the UI thread */
    while (1)
3808
    {
3809
        msi_process_pending_messages( NULL );
3810

3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821
        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;
    }
3822 3823
}

3824
UINT msi_dialog_run_message_loop( msi_dialog *dialog )
3825
{
3826
    DWORD style;
3827 3828 3829 3830 3831 3832
    HWND hwnd;

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

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

    hwnd = CreateWindowW( szMsiDialogClass, dialog->name, style,
3838 3839 3840 3841 3842 3843 3844
                     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;
    }
3845

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

    if( dialog->attributes & msidbDialogAttributesModal )
3850
    {
3851
        while( !dialog->finished )
Mike McCormack's avatar
Mike McCormack committed
3852
        {
3853
            MsgWaitForMultipleObjects( 0, NULL, 0, INFINITE, QS_ALLINPUT );
3854
            msi_process_pending_messages( dialog->hwnd );
Mike McCormack's avatar
Mike McCormack committed
3855
        }
3856
    }
3857 3858 3859 3860
    else
        return ERROR_IO_PENDING;

    return ERROR_SUCCESS;
3861 3862
}

3863
static void msi_dialog_do_preview( msi_dialog *dialog )
3864
{
3865
    TRACE("\n");
3866 3867 3868 3869 3870 3871
    dialog->attributes |= msidbDialogAttributesVisible;
    dialog->attributes &= ~msidbDialogAttributesModal;
    msi_dialog_run_message_loop( dialog );
}

void msi_dialog_destroy( msi_dialog *dialog )
3872
{
3873 3874
    msi_font *font, *next;

3875 3876 3877 3878 3879 3880
    if( uiThreadId != GetCurrentThreadId() )
    {
        SendMessageW( hMsiHiddenWindow, WM_MSI_DIALOG_DESTROY, 0, (LPARAM) dialog );
        return;
    }

3881 3882
    if( dialog->hwnd )
        ShowWindow( dialog->hwnd, SW_HIDE );
3883

3884 3885 3886
    if( dialog->hwnd )
        DestroyWindow( dialog->hwnd );

3887 3888 3889
    /* unsubscribe events */
    ControlEvent_CleanupDialogSubscriptions(dialog->package, dialog->name);

3890
    /* destroy the list of controls */
3891
    while( !list_empty( &dialog->controls ) )
3892
    {
3893 3894 3895 3896 3897
        msi_control *t;

        t = LIST_ENTRY( list_head( &dialog->controls ),
                        msi_control, entry );
        msi_destroy_control( t );
3898 3899 3900
    }

    /* destroy the list of fonts */
3901
    LIST_FOR_EACH_ENTRY_SAFE( font, next, &dialog->fonts, msi_font, entry )
3902
    {
3903 3904 3905
        list_remove( &font->entry );
        DeleteObject( font->hfont );
        msi_free( font );
3906
    }
3907
    msi_free( dialog->default_font );
3908

3909 3910
    msi_free( dialog->control_default );
    msi_free( dialog->control_cancel );
3911
    msiobj_release( &dialog->package->hdr );
3912
    dialog->package = NULL;
3913
    msi_free( dialog );
3914 3915
}

3916 3917 3918
void msi_dialog_unregister_class( void )
{
    DestroyWindow( hMsiHiddenWindow );
3919
    hMsiHiddenWindow = NULL;
3920
    UnregisterClassW( szMsiDialogClass, NULL );
3921
    UnregisterClassW( szMsiHiddenWindow, NULL );
3922
    uiThreadId = 0;
3923
}
3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935

static UINT error_dialog_handler(MSIPACKAGE *package, LPCWSTR event,
                                 LPCWSTR argument, msi_dialog* dialog)
{
    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
    };

3936
    if ( strcmpW( event, end_dialog ) )
3937 3938
        return ERROR_SUCCESS;

3939 3940
    if ( !strcmpW( argument, error_abort ) || !strcmpW( argument, error_cancel ) ||
         !strcmpW( argument, error_no ) )
3941
    {
3942
         msi_set_property( package->db, result_prop, error_abort, -1 );
3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984
    }

    ControlEvent_CleanupSubscriptions(package);
    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
    };

3985
    if ((package->ui_level & INSTALLUILEVEL_MASK) == INSTALLUILEVEL_NONE) return ERROR_SUCCESS;
3986

3987 3988
    if ( !error_dialog )
    {
3989
        LPWSTR product_name = msi_dup_property( package->db, pn_prop );
3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016
        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;

    dialog = msi_dialog_create( package, error_dialog, package->dialog,
                                error_dialog_handler );
    if ( !dialog )
        return ERROR_FUNCTION_FAILED;

    dialog->finished = FALSE;
    r = msi_dialog_run_message_loop( dialog );
    if ( r != ERROR_SUCCESS )
        goto done;

4017
    r = msi_get_property( package->db, result_prop, result, &size );
4018 4019 4020
    if ( r != ERROR_SUCCESS)
        r = ERROR_SUCCESS;

4021
    if ( !strcmpW( result, error_abort ) )
4022 4023 4024 4025 4026 4027 4028
        r = ERROR_FUNCTION_FAILED;

done:
    msi_dialog_destroy( dialog );

    return r;
}
4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 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 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164

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;
}

static UINT preview_event_handler( MSIPACKAGE *package, LPCWSTR event,
                                   LPCWSTR argument, msi_dialog *dialog )
{
    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)
    {
        dialog = msi_dialog_create( preview->package, szDialogName, NULL, preview_event_handler );
        if (dialog)
            msi_dialog_do_preview( dialog );
        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;
}