help.c 57.5 KB
Newer Older
1 2 3 4
/*
 * Help Viewer Implementation
 *
 * Copyright 2005 James Hawkins
5
 * Copyright 2007 Jacek Caban for CodeWeavers
6
 * Copyright 2011 Owen Rudge for CodeWeavers
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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
20
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 22
 */

23
#include "hhctrl.h"
24 25 26

#include "wingdi.h"
#include "commctrl.h"
27 28 29
#include "wininet.h"

#include "wine/debug.h"
30

31 32
#include "resource.h"

33 34
WINE_DEFAULT_DEBUG_CHANNEL(htmlhelp);

35
static LRESULT Help_OnSize(HWND hWnd);
36
static void ExpandContract(HHInfo *pHHInfo);
37

38 39
/* Window type defaults */

40 41 42 43
#define WINTYPE_DEFAULT_X           280
#define WINTYPE_DEFAULT_Y           100
#define WINTYPE_DEFAULT_WIDTH       740
#define WINTYPE_DEFAULT_HEIGHT      640
44
#define WINTYPE_DEFAULT_NAVWIDTH    250
45

46 47
#define TAB_TOP_PADDING     8
#define TAB_RIGHT_PADDING   4
48
#define TAB_MARGIN  8
49
#define EDIT_HEIGHT         20
50

51 52
struct list window_list = LIST_INIT(window_list);

James Hawkins's avatar
James Hawkins committed
53 54
static const WCHAR szEmpty[] = {0};

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
struct html_encoded_symbol {
    const char *html_code;
    char        ansi_symbol;
};

/*
 * Table mapping the conversion between HTML encoded symbols and their ANSI code page equivalent.
 * Note: Add additional entries in proper alphabetical order (a binary search is used on this table).
 */
struct html_encoded_symbol html_encoded_symbols[] =
{
    {"AElig",  0xC6},
    {"Aacute", 0xC1},
    {"Acirc",  0xC2},
    {"Agrave", 0xC0},
    {"Aring",  0xC5},
    {"Atilde", 0xC3},
    {"Auml",   0xC4},
    {"Ccedil", 0xC7},
    {"ETH",    0xD0},
    {"Eacute", 0xC9},
    {"Ecirc",  0xCA},
    {"Egrave", 0xC8},
    {"Euml",   0xCB},
    {"Iacute", 0xCD},
    {"Icirc",  0xCE},
    {"Igrave", 0xCC},
    {"Iuml",   0xCF},
    {"Ntilde", 0xD1},
    {"Oacute", 0xD3},
    {"Ocirc",  0xD4},
    {"Ograve", 0xD2},
    {"Oslash", 0xD8},
    {"Otilde", 0xD5},
    {"Ouml",   0xD6},
    {"THORN",  0xDE},
    {"Uacute", 0xDA},
    {"Ucirc",  0xDB},
    {"Ugrave", 0xD9},
    {"Uuml",   0xDC},
    {"Yacute", 0xDD},
    {"aacute", 0xE1},
    {"acirc",  0xE2},
    {"acute",  0xB4},
    {"aelig",  0xE6},
    {"agrave", 0xE0},
    {"amp",    '&'},
    {"aring",  0xE5},
    {"atilde", 0xE3},
    {"auml",   0xE4},
    {"brvbar", 0xA6},
    {"ccedil", 0xE7},
    {"cedil",  0xB8},
    {"cent",   0xA2},
    {"copy",   0xA9},
    {"curren", 0xA4},
    {"deg",    0xB0},
    {"divide", 0xF7},
    {"eacute", 0xE9},
    {"ecirc",  0xEA},
    {"egrave", 0xE8},
    {"eth",    0xF0},
    {"euml",   0xEB},
    {"frac12", 0xBD},
    {"frac14", 0xBC},
    {"frac34", 0xBE},
    {"gt",     '>'},
    {"iacute", 0xED},
    {"icirc",  0xEE},
    {"iexcl",  0xA1},
    {"igrave", 0xEC},
    {"iquest", 0xBF},
    {"iuml",   0xEF},
    {"laquo",  0xAB},
    {"lt",     '<'},
    {"macr",   0xAF},
    {"micro",  0xB5},
    {"middot", 0xB7},
    {"nbsp",   ' '},
    {"not",    0xAC},
    {"ntilde", 0xF1},
    {"oacute", 0xF3},
    {"ocirc",  0xF4},
    {"ograve", 0xF2},
    {"ordf",   0xAA},
    {"ordm",   0xBA},
    {"oslash", 0xF8},
    {"otilde", 0xF5},
    {"ouml",   0xF6},
    {"para",   0xB6},
    {"plusmn", 0xB1},
    {"pound",  0xA3},
    {"quot",   '"'},
    {"raquo",  0xBB},
    {"reg",    0xAE},
    {"sect",   0xA7},
    {"shy",    0xAD},
    {"sup1",   0xB9},
    {"sup2",   0xB2},
    {"sup3",   0xB3},
    {"szlig",  0xDF},
    {"thorn",  0xFE},
    {"times",  0xD7},
    {"uacute", 0xFA},
    {"ucirc",  0xFB},
    {"ugrave", 0xF9},
    {"uml",    0xA8},
    {"uuml",   0xFC},
    {"yacute", 0xFD},
    {"yen",    0xA5},
    {"yuml",   0xFF}
};

168 169 170 171 172
static inline BOOL navigation_visible(HHInfo *info)
{
    return ((info->WinType.fsWinProperties & HHWIN_PROP_TRI_PANE) && !info->WinType.fNotExpanded);
}

173 174 175 176
/* Loads a string from the resource file */
static LPWSTR HH_LoadString(DWORD dwID)
{
    LPWSTR string = NULL;
177
    LPCWSTR stringresource;
178 179
    int iSize;

180
    iSize = LoadStringW(hhctrl_hinstance, dwID, (LPWSTR)&stringresource, 0);
181

182 183 184
    string = heap_alloc((iSize + 2) * sizeof(WCHAR)); /* some strings (tab text) needs double-null termination */
    memcpy(string, stringresource, iSize*sizeof(WCHAR));
    string[iSize] = 0;
185 186 187 188

    return string;
}

189
static HRESULT navigate_url(HHInfo *info, LPCWSTR surl)
190 191 192 193
{
    VARIANT url;
    HRESULT hres;

194 195
    TRACE("%s\n", debugstr_w(surl));

196 197 198 199 200 201 202
    V_VT(&url) = VT_BSTR;
    V_BSTR(&url) = SysAllocString(surl);

    hres = IWebBrowser2_Navigate2(info->web_browser, &url, 0, 0, 0, 0);

    VariantClear(&url);

203
    if(FAILED(hres))
204
        TRACE("Navigation failed: %08x\n", hres);
205 206 207 208 209 210 211 212 213 214

    return hres;
}

BOOL NavigateToUrl(HHInfo *info, LPCWSTR surl)
{
    ChmPath chm_path;
    BOOL ret;
    HRESULT hres;

215
    static const WCHAR url_indicator[] = {':', '/', '/', 0};
216 217 218 219 220 221 222 223

    TRACE("%s\n", debugstr_w(surl));

    if (strstrW(surl, url_indicator)) {
        hres = navigate_url(info, surl);
        if(SUCCEEDED(hres))
            return TRUE;
    } /* look up in chm if it doesn't look like a full url */
224 225 226 227

    SetChmPath(&chm_path, info->pCHMInfo->szFile, surl);
    ret = NavigateToChm(info, chm_path.chm_file, chm_path.chm_index);

228 229
    heap_free(chm_path.chm_file);
    heap_free(chm_path.chm_index);
230 231

    return ret;
232 233
}

234
static BOOL AppendFullPathURL(LPCWSTR file, LPWSTR buf, LPCWSTR index)
235 236
{
    static const WCHAR url_format[] =
237 238 239
        {'m','k',':','@','M','S','I','T','S','t','o','r','e',':','%','s',':',':','%','s','%','s',0};
    static const WCHAR slash[] = {'/',0};
    static const WCHAR empty[] = {0};
240
    WCHAR full_path[MAX_PATH];
241

242
    TRACE("%s %p %s\n", debugstr_w(file), buf, debugstr_w(index));
243

244
    if(!GetFullPathNameW(file, sizeof(full_path)/sizeof(full_path[0]), full_path, NULL)) {
245 246 247 248
        WARN("GetFullPathName failed: %u\n", GetLastError());
        return FALSE;
    }

249
    wsprintfW(buf, url_format, full_path, (!index || index[0] == '/') ? empty : slash, index);
250 251 252 253 254 255 256 257 258 259 260
    return TRUE;
}

BOOL NavigateToChm(HHInfo *info, LPCWSTR file, LPCWSTR index)
{
    WCHAR buf[INTERNET_MAX_URL_LENGTH];

    TRACE("%p %s %s\n", info, debugstr_w(file), debugstr_w(index));

    if ((!info->web_browser) || !AppendFullPathURL(file, buf, index))
        return FALSE;
261

262
    return SUCCEEDED(navigate_url(info, buf));
263 264
}

265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
static void DoSync(HHInfo *info)
{
    WCHAR buf[INTERNET_MAX_URL_LENGTH];
    HRESULT hres;
    BSTR url;

    hres = IWebBrowser2_get_LocationURL(info->web_browser, &url);

    if (FAILED(hres))
    {
        WARN("get_LocationURL failed: %08x\n", hres);
        return;
    }

    /* If we're not currently viewing a page in the active .chm file, abort */
280
    if ((!AppendFullPathURL(info->WinType.pszFile, buf, NULL)) || (lstrlenW(buf) > lstrlenW(url)))
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
    {
        SysFreeString(url);
        return;
    }

    if (lstrcmpiW(buf, url) > 0)
    {
        static const WCHAR delimW[] = {':',':','/',0};
        const WCHAR *index;

        index = strstrW(url, delimW);

        if (index)
            ActivateContentTopic(info->tabs[TAB_CONTENTS].hwnd, index + 3, info->content); /* skip over ::/ */
    }

    SysFreeString(url);
}

James Hawkins's avatar
James Hawkins committed
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
/* Size Bar */

#define SIZEBAR_WIDTH   4

static const WCHAR szSizeBarClass[] = {
    'H','H',' ','S','i','z','e','B','a','r',0
};

/* Draw the SizeBar */
static void SB_OnPaint(HWND hWnd)
{
    PAINTSTRUCT ps;
    HDC hdc;
    RECT rc;
    
    hdc = BeginPaint(hWnd, &ps);

    GetClientRect(hWnd, &rc);

    /* dark frame */
    rc.right += 1;
    rc.bottom -= 1;
    FrameRect(hdc, &rc, GetStockObject(GRAY_BRUSH));

    /* white highlight */
    SelectObject(hdc, GetStockObject(WHITE_PEN));
    MoveToEx(hdc, rc.right, 1, NULL);
    LineTo(hdc, 1, 1);
    LineTo(hdc, 1, rc.bottom - 1);

    
    MoveToEx(hdc, 0, rc.bottom, NULL);
    LineTo(hdc, rc.right, rc.bottom);

    EndPaint(hWnd, &ps);
}

337 338 339 340 341 342 343
static void SB_OnLButtonDown(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
    SetCapture(hWnd);
}

static void SB_OnLButtonUp(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
344
    HHInfo *pHHInfo = (HHInfo *)GetWindowLongPtrW(hWnd, 0);
345 346 347 348
    POINT pt;

    pt.x = (short)LOWORD(lParam);
    pt.y = (short)HIWORD(lParam);
349 350

    /* update the window sizes */
351
    pHHInfo->WinType.iNavWidth += pt.x;
352 353 354 355 356 357 358 359 360 361 362 363
    Help_OnSize(hWnd);

    ReleaseCapture();
}

static void SB_OnMouseMove(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
    /* ignore WM_MOUSEMOVE if not dragging the SizeBar */
    if (!(wParam & MK_LBUTTON))
        return;
}

364
static LRESULT CALLBACK SizeBar_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
James Hawkins's avatar
James Hawkins committed
365 366 367
{
    switch (message)
    {
368 369 370 371 372 373 374 375 376
        case WM_LBUTTONDOWN:
            SB_OnLButtonDown(hWnd, wParam, lParam);
            break;
        case WM_LBUTTONUP:
            SB_OnLButtonUp(hWnd, wParam, lParam);
            break;
        case WM_MOUSEMOVE:
            SB_OnMouseMove(hWnd, wParam, lParam);
            break;
James Hawkins's avatar
James Hawkins committed
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
        case WM_PAINT:
            SB_OnPaint(hWnd);
            break;
        default:
            return DefWindowProcW(hWnd, message, wParam, lParam);
    }

    return 0;
}

static void HH_RegisterSizeBarClass(HHInfo *pHHInfo)
{
    WNDCLASSEXW wcex;

    wcex.cbSize         = sizeof(WNDCLASSEXW);
    wcex.style          = 0;
393
    wcex.lpfnWndProc    = SizeBar_WndProc;
James Hawkins's avatar
James Hawkins committed
394
    wcex.cbClsExtra     = 0;
395
    wcex.cbWndExtra     = sizeof(LONG_PTR);
396
    wcex.hInstance      = hhctrl_hinstance;
James Hawkins's avatar
James Hawkins committed
397 398 399 400 401 402 403 404 405 406
    wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
    wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_SIZEWE);
    wcex.hbrBackground  = (HBRUSH)(COLOR_MENU + 1);
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = szSizeBarClass;
    wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);

    RegisterClassExW(&wcex);
}

407
static void SB_GetSizeBarRect(HHInfo *info, RECT *rc)
James Hawkins's avatar
James Hawkins committed
408 409 410
{
    RECT rectWND, rectTB, rectNP;

411 412 413
    GetClientRect(info->WinType.hwndHelp, &rectWND);
    GetClientRect(info->WinType.hwndToolBar, &rectTB);
    GetClientRect(info->WinType.hwndNavigation, &rectNP);
James Hawkins's avatar
James Hawkins committed
414 415 416 417 418 419 420 421 422 423

    rc->left = rectNP.right;
    rc->top = rectTB.bottom;
    rc->bottom = rectWND.bottom - rectTB.bottom;
    rc->right = SIZEBAR_WIDTH;
}

static BOOL HH_AddSizeBar(HHInfo *pHHInfo)
{
    HWND hWnd;
424
    HWND hwndParent = pHHInfo->WinType.hwndHelp;
425
    DWORD dwStyles = WS_CHILDWINDOW | WS_OVERLAPPED;
James Hawkins's avatar
James Hawkins committed
426 427 428
    DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
    RECT rc;

429
    if (navigation_visible(pHHInfo))
430 431
        dwStyles |= WS_VISIBLE;

James Hawkins's avatar
James Hawkins committed
432 433 434 435
    SB_GetSizeBarRect(pHHInfo, &rc);

    hWnd = CreateWindowExW(dwExStyles, szSizeBarClass, szEmpty, dwStyles,
                           rc.left, rc.top, rc.right, rc.bottom,
436
                           hwndParent, NULL, hhctrl_hinstance, NULL);
James Hawkins's avatar
James Hawkins committed
437 438 439
    if (!hWnd)
        return FALSE;

440
    /* store the pointer to the HH info struct */
441
    SetWindowLongPtrW(hWnd, 0, (LONG_PTR)pHHInfo);
442

James Hawkins's avatar
James Hawkins committed
443 444 445 446
    pHHInfo->hwndSizeBar = hWnd;
    return TRUE;
}

447 448 449 450 451 452
/* Child Window */

static const WCHAR szChildClass[] = {
    'H','H',' ','C','h','i','l','d',0
};

453
static LRESULT Child_OnPaint(HWND hWnd)
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
{
    PAINTSTRUCT ps;
    HDC hdc;
    RECT rc;

    hdc = BeginPaint(hWnd, &ps);

    /* Only paint the Navigation pane, identified by the fact
     * that it has a child window
     */
    if (GetWindow(hWnd, GW_CHILD))
    {
        GetClientRect(hWnd, &rc);

        /* set the border color */
        SelectObject(hdc, GetStockObject(DC_PEN));
        SetDCPenColor(hdc, GetSysColor(COLOR_BTNSHADOW));

472 473
        /* Draw the top border */
        LineTo(hdc, rc.right, 0);
474

475 476 477
        SelectObject(hdc, GetStockObject(WHITE_PEN));
        MoveToEx(hdc, 0, 1, NULL);
        LineTo(hdc, rc.right, 1);
478 479 480
    }

    EndPaint(hWnd, &ps);
481 482 483 484

    return 0;
}

485
static void ResizeTabChild(HHInfo *info, int tab)
486
{
487 488
    HWND hwnd = info->tabs[tab].hwnd;
    INT width, height;
489 490 491 492 493 494 495 496 497 498 499
    RECT rect, tabrc;
    DWORD cnt;

    GetClientRect(info->WinType.hwndNavigation, &rect);
    SendMessageW(info->hwndTabCtrl, TCM_GETITEMRECT, 0, (LPARAM)&tabrc);
    cnt = SendMessageW(info->hwndTabCtrl, TCM_GETROWCOUNT, 0, 0);

    rect.left = TAB_MARGIN;
    rect.top = TAB_TOP_PADDING + cnt*(tabrc.bottom-tabrc.top) + TAB_MARGIN;
    rect.right -= TAB_RIGHT_PADDING + TAB_MARGIN;
    rect.bottom -= TAB_MARGIN;
500 501
    width = rect.right-rect.left;
    height = rect.bottom-rect.top;
502

503 504 505 506 507 508 509 510 511 512
    SetWindowPos(hwnd, NULL, rect.left, rect.top, width, height,
                 SWP_NOZORDER | SWP_NOACTIVATE);

    switch (tab)
    {
    case TAB_INDEX: {
        int scroll_width = GetSystemMetrics(SM_CXVSCROLL);
        int border_width = GetSystemMetrics(SM_CXBORDER);
        int edge_width = GetSystemMetrics(SM_CXEDGE);

513 514 515
        /* Resize the tab widget column to perfectly fit the tab window and
         * leave sufficient space for the scroll widget.
         */
516 517 518 519 520
        SendMessageW(info->tabs[TAB_INDEX].hwnd, LVM_SETCOLUMNWIDTH, 0,
                     width-scroll_width-2*border_width-2*edge_width);

        break;
    }
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
    case TAB_SEARCH: {
        int scroll_width = GetSystemMetrics(SM_CXVSCROLL);
        int border_width = GetSystemMetrics(SM_CXBORDER);
        int edge_width = GetSystemMetrics(SM_CXEDGE);
        int top_pos = 0;

        SetWindowPos(info->search.hwndEdit, NULL, 0, top_pos, width,
                      EDIT_HEIGHT, SWP_NOZORDER | SWP_NOACTIVATE);
        top_pos += EDIT_HEIGHT + TAB_MARGIN;
        SetWindowPos(info->search.hwndList, NULL, 0, top_pos, width,
                      height-top_pos, SWP_NOZORDER | SWP_NOACTIVATE);
        /* Resize the tab widget column to perfectly fit the tab window and
         * leave sufficient space for the scroll widget.
         */
        SendMessageW(info->search.hwndList, LVM_SETCOLUMNWIDTH, 0,
                     width-scroll_width-2*border_width-2*edge_width);

        break;
    }
540
    }
541 542
}

543 544
static LRESULT Child_OnSize(HWND hwnd)
{
545
    HHInfo *info = (HHInfo*)GetWindowLongPtrW(hwnd, 0);
546 547 548 549 550 551 552 553 554
    RECT rect;

    if(!info || hwnd != info->WinType.hwndNavigation)
        return 0;

    GetClientRect(hwnd, &rect);
    SetWindowPos(info->hwndTabCtrl, HWND_TOP, 0, 0,
                 rect.right - TAB_RIGHT_PADDING,
                 rect.bottom - TAB_TOP_PADDING, SWP_NOMOVE);
555

556 557
    ResizeTabChild(info, TAB_CONTENTS);
    ResizeTabChild(info, TAB_INDEX);
558
    ResizeTabChild(info, TAB_SEARCH);
559 560 561 562 563
    return 0;
}

static LRESULT OnTabChange(HWND hwnd)
{
564
    HHInfo *info = (HHInfo*)GetWindowLongPtrW(hwnd, 0);
565
    int tab_id, tab_index, i;
566 567 568 569 570 571 572 573 574

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

    if (!info)
        return 0;

    if(info->tabs[info->current_tab].hwnd)
        ShowWindow(info->tabs[info->current_tab].hwnd, SW_HIDE);

575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
    tab_id = (int) SendMessageW(info->hwndTabCtrl, TCM_GETCURSEL, 0, 0);
    /* convert the ID of the tab to an index in our tab list */
    tab_index = -1;
    for (i=0; i<TAB_NUMTABS; i++)
    {
        if (info->tabs[i].id == tab_id)
        {
            tab_index = i;
            break;
        }
    }
    if (tab_index == -1)
    {
        FIXME("Tab ID %d does not correspond to a valid index in the tab list.\n", tab_id);
        return 0;
    }
    info->current_tab = tab_index;
592 593 594 595

    if(info->tabs[info->current_tab].hwnd)
        ShowWindow(info->tabs[info->current_tab].hwnd, SW_SHOW);

596
    return 0;
597 598
}

599
static LRESULT OnTopicChange(HHInfo *info, void *user_data)
600
{
601 602
    LPCWSTR chmfile = NULL, name = NULL, local = NULL;
    ContentItem *citer;
603
    SearchItem *siter;
604
    IndexItem *iiter;
605

606
    if(!user_data || !info)
607 608
        return 0;

609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
    switch (info->current_tab)
    {
    case TAB_CONTENTS:
        citer = (ContentItem *) user_data;
        name = citer->name;
        local = citer->local;
        while(citer) {
            if(citer->merge.chm_file) {
                chmfile = citer->merge.chm_file;
                break;
            }
            citer = citer->parent;
        }
        break;
    case TAB_INDEX:
        iiter = (IndexItem *) user_data;
        if(iiter->nItems == 0) {
            FIXME("No entries for this item!\n");
            return 0;
628
        }
629
        if(iiter->nItems > 1) {
630 631 632 633 634 635 636 637
            int i = 0;
            LVITEMW lvi;

            SendMessageW(info->popup.hwndList, LVM_DELETEALLITEMS, 0, 0);
            for(i=0;i<iiter->nItems;i++) {
                IndexSubItem *item = &iiter->items[i];
                WCHAR *name = iiter->keyword;

638 639
                if(!item->name)
                    item->name = GetDocumentTitle(info->pCHMInfo, item->local);
640 641 642 643 644 645 646 647 648 649 650
                if(item->name)
                    name = item->name;
                memset(&lvi, 0, sizeof(lvi));
                lvi.iItem = i;
                lvi.mask = LVIF_TEXT|LVIF_PARAM;
                lvi.cchTextMax = strlenW(name)+1;
                lvi.pszText = name;
                lvi.lParam = (LPARAM) item;
                SendMessageW(info->popup.hwndList, LVM_INSERTITEMW, 0, (LPARAM)&lvi);
            }
            ShowWindow(info->popup.hwndPopup, SW_SHOW);
651 652 653 654 655 656
            return 0;
        }
        name = iiter->items[0].name;
        local = iiter->items[0].local;
        chmfile = iiter->merge.chm_file;
        break;
657 658 659 660 661 662
    case TAB_SEARCH:
        siter = (SearchItem *) user_data;
        name = siter->filename;
        local = siter->filename;
        chmfile = info->pCHMInfo->szFile;
        break;
663 664 665
    default:
        FIXME("Unhandled operation for this tab!\n");
        return 0;
666 667
    }

668 669 670 671 672 673
    if(!chmfile)
    {
        FIXME("No help file found for this item!\n");
        return 0;
    }

674 675 676
    TRACE("name %s loal %s\n", debugstr_w(name), debugstr_w(local));

    NavigateToChm(info, chmfile, local);
677 678 679
    return 0;
}

680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
/* Capture the Enter/Return key and send it up to Child_WndProc as an NM_RETURN message */
static LRESULT CALLBACK EditChild_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    WNDPROC editWndProc = (WNDPROC)GetWindowLongPtrW(hWnd, GWLP_USERDATA);

    if(message == WM_KEYUP && wParam == VK_RETURN)
    {
        NMHDR nmhdr;

        nmhdr.hwndFrom = hWnd;
        nmhdr.code = NM_RETURN;
        SendMessageW(GetParent(GetParent(hWnd)), WM_NOTIFY, wParam, (LPARAM)&nmhdr);
    }
    return editWndProc(hWnd, message, wParam, lParam);
}

696
static LRESULT CALLBACK Child_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
697 698 699
{
    switch (message)
    {
700 701 702 703
    case WM_PAINT:
        return Child_OnPaint(hWnd);
    case WM_SIZE:
        return Child_OnSize(hWnd);
704
    case WM_NOTIFY: {
705
        HHInfo *info = (HHInfo*)GetWindowLongPtrW(hWnd, 0);
706
        NMHDR *nmhdr = (NMHDR*)lParam;
707

708 709 710
        switch(nmhdr->code) {
        case TCN_SELCHANGE:
            return OnTabChange(hWnd);
711
        case TVN_SELCHANGEDW:
712
            return OnTopicChange(info, (void*)((NMTREEVIEWW *)lParam)->itemNew.lParam);
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
        case TVN_ITEMEXPANDINGW: {
            TVITEMW *item = &((NMTREEVIEWW *)lParam)->itemNew;
            HWND hwndTreeView = info->tabs[TAB_CONTENTS].hwnd;

            item->mask = TVIF_IMAGE|TVIF_SELECTEDIMAGE;
            if (item->state & TVIS_EXPANDED)
            {
                item->iImage = HHTV_FOLDER;
                item->iSelectedImage = HHTV_FOLDER;
            }
            else
            {
                item->iImage = HHTV_OPENFOLDER;
                item->iSelectedImage = HHTV_OPENFOLDER;
            }
            SendMessageW(hwndTreeView, TVM_SETITEMW, 0, (LPARAM)item);
            return 0;
        }
731
        case NM_DBLCLK:
732 733 734 735 736
            if(!info)
                return 0;
            switch(info->current_tab)
            {
            case TAB_INDEX:
737
                return OnTopicChange(info, (void*)((NMITEMACTIVATE *)lParam)->lParam);
738 739 740 741
            case TAB_SEARCH:
                return OnTopicChange(info, (void*)((NMITEMACTIVATE *)lParam)->lParam);
            }
            break;
742
        case NM_RETURN:
743 744 745 746
            if(!info)
                return 0;
            switch(info->current_tab) {
            case TAB_INDEX: {
747 748 749 750 751
                HWND hwndList = info->tabs[TAB_INDEX].hwnd;
                LVITEMW lvItem;

                lvItem.iItem = (int) SendMessageW(hwndList, LVM_GETSELECTIONMARK, 0, 0);
                lvItem.mask = TVIF_PARAM;
752
                SendMessageW(hwndList, LVM_GETITEMW, 0, (LPARAM)&lvItem);
753
                OnTopicChange(info, (void*) lvItem.lParam);
754
                return 0;
755
            }
756
            case TAB_SEARCH: {
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
                if(nmhdr->hwndFrom == info->search.hwndEdit) {
                    char needle[100];
                    DWORD i, len;

                    len = GetWindowTextA(info->search.hwndEdit, needle, sizeof(needle));
                    if(!len)
                    {
                        FIXME("Unable to get search text.\n");
                        return 0;
                    }
                    /* Convert the requested text for comparison later against the
                     * lower case version of HTML file contents.
                     */
                    for(i=0;i<len;i++)
                        needle[i] = tolower(needle[i]);
                    InitSearch(info, needle);
                    return 0;
                }else if(nmhdr->hwndFrom == info->search.hwndList) {
                    HWND hwndList = info->search.hwndList;
                    LVITEMW lvItem;

                    lvItem.iItem = (int) SendMessageW(hwndList, LVM_GETSELECTIONMARK, 0, 0);
                    lvItem.mask = TVIF_PARAM;
780
                    SendMessageW(hwndList, LVM_GETITEMW, 0, (LPARAM)&lvItem);
781 782 783 784
                    OnTopicChange(info, (void*) lvItem.lParam);
                    return 0;
                }
                break;
785 786 787
            }
            }
            break;
788 789 790
        }
        break;
    }
791 792
    default:
        return DefWindowProcW(hWnd, message, wParam, lParam);
793 794 795 796 797 798 799 800 801 802 803
    }

    return 0;
}

static void HH_RegisterChildWndClass(HHInfo *pHHInfo)
{
    WNDCLASSEXW wcex;

    wcex.cbSize         = sizeof(WNDCLASSEXW);
    wcex.style          = 0;
804
    wcex.lpfnWndProc    = Child_WndProc;
805
    wcex.cbClsExtra     = 0;
806
    wcex.cbWndExtra     = sizeof(LONG_PTR);
807
    wcex.hInstance      = hhctrl_hinstance;
808 809
    wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
    wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
810
    wcex.hbrBackground  = (HBRUSH)(COLOR_BTNFACE + 1);
811 812 813 814 815 816 817
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = szChildClass;
    wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);

    RegisterClassExW(&wcex);
}

818 819
/* Toolbar */

820 821
#define ICON_SIZE   20

822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
static void DisplayPopupMenu(HHInfo *info)
{
    HMENU menu, submenu;
    TBBUTTONINFOW button;
    MENUITEMINFOW item;
    POINT coords;
    RECT rect;
    DWORD index;

    menu = LoadMenuW(hhctrl_hinstance, MAKEINTRESOURCEW(MENU_POPUP));

    if (!menu)
        return;

    submenu = GetSubMenu(menu, 0);

    /* Update the Show/Hide menu item */
    item.cbSize = sizeof(MENUITEMINFOW);
840 841 842
    item.fMask = MIIM_FTYPE | MIIM_STATE | MIIM_STRING;
    item.fType = MFT_STRING;
    item.fState = MF_ENABLED;
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869

    if (info->WinType.fNotExpanded)
        item.dwTypeData = HH_LoadString(IDS_SHOWTABS);
    else
        item.dwTypeData = HH_LoadString(IDS_HIDETABS);

    SetMenuItemInfoW(submenu, IDTB_EXPAND, FALSE, &item);
    heap_free(item.dwTypeData);

    /* Find the index toolbar button */
    button.cbSize = sizeof(TBBUTTONINFOW);
    button.dwMask = TBIF_COMMAND;
    index = SendMessageW(info->WinType.hwndToolBar, TB_GETBUTTONINFOW, IDTB_OPTIONS, (LPARAM) &button);

    if (index == -1)
       return;

    /* Get position */
    SendMessageW(info->WinType.hwndToolBar, TB_GETITEMRECT, index, (LPARAM) &rect);

    coords.x = rect.left;
    coords.y = rect.bottom;

    ClientToScreen(info->WinType.hwndToolBar, &coords);
    TrackPopupMenu(submenu, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_LEFTBUTTON | TPM_NOANIMATION, coords.x, coords.y, 0, info->WinType.hwndHelp, NULL);
}

870 871
static void TB_OnClick(HWND hWnd, DWORD dwID)
{
872
    HHInfo *info = (HHInfo *)GetWindowLongPtrW(hWnd, 0);
873 874 875 876

    switch (dwID)
    {
        case IDTB_STOP:
877
            DoPageAction(info, WB_STOP);
878 879
            break;
        case IDTB_REFRESH:
880
            DoPageAction(info, WB_REFRESH);
881 882
            break;
        case IDTB_BACK:
883
            DoPageAction(info, WB_GOBACK);
884 885
            break;
        case IDTB_HOME:
886
            NavigateToChm(info, info->pCHMInfo->szFile, info->WinType.pszHome);
887 888
            break;
        case IDTB_FORWARD:
889
            DoPageAction(info, WB_GOFORWARD);
890
            break;
891 892 893
        case IDTB_PRINT:
            DoPageAction(info, WB_PRINT);
            break;
894 895
        case IDTB_EXPAND:
        case IDTB_CONTRACT:
896 897
            ExpandContract(info);
            break;
898
        case IDTB_SYNC:
899 900
            DoSync(info);
            break;
901
        case IDTB_OPTIONS:
902 903
            DisplayPopupMenu(info);
            break;
904 905 906 907 908 909 910 911
        case IDTB_NOTES:
        case IDTB_CONTENTS:
        case IDTB_INDEX:
        case IDTB_SEARCH:
        case IDTB_HISTORY:
        case IDTB_FAVORITES:
            /* These are officially unimplemented as of the Windows 7 SDK */
            break;
912 913 914 915 916 917 918 919 920 921 922 923
        case IDTB_BROWSE_FWD:
        case IDTB_BROWSE_BACK:
        case IDTB_JUMP1:
        case IDTB_JUMP2:
        case IDTB_CUSTOMIZE:
        case IDTB_ZOOM:
        case IDTB_TOC_NEXT:
        case IDTB_TOC_PREV:
            break;
    }
}

924
static void TB_AddButton(TBBUTTON *pButtons, DWORD dwIndex, DWORD dwID, DWORD dwBitmap)
925
{
926
    pButtons[dwIndex].iBitmap = dwBitmap;
927 928 929 930 931 932 933
    pButtons[dwIndex].idCommand = dwID;
    pButtons[dwIndex].fsState = TBSTATE_ENABLED;
    pButtons[dwIndex].fsStyle = BTNS_BUTTON;
    pButtons[dwIndex].dwData = 0;
    pButtons[dwIndex].iString = 0;
}

934
static void TB_AddButtonsFromFlags(HHInfo *pHHInfo, TBBUTTON *pButtons, DWORD dwButtonFlags, LPDWORD pdwNumButtons)
935
{
936
    int nHistBitmaps = 0, nStdBitmaps = 0, nHHBitmaps = 0;
937 938
    HWND hToolbar = pHHInfo->WinType.hwndToolBar;
    TBADDBITMAP tbAB;
939
    DWORD unsupported;
940

941
    /* Common bitmaps */
942 943 944 945 946
    tbAB.hInst = HINST_COMMCTRL;
    tbAB.nID = IDB_HIST_LARGE_COLOR;
    nHistBitmaps = SendMessageW(hToolbar, TB_ADDBITMAP, 0, (LPARAM)&tbAB);
    tbAB.nID = IDB_STD_LARGE_COLOR;
    nStdBitmaps = SendMessageW(hToolbar, TB_ADDBITMAP, 0, (LPARAM)&tbAB);
947 948 949
    /* hhctrl.ocx bitmaps */
    tbAB.hInst = hhctrl_hinstance;
    tbAB.nID = IDB_HHTOOLBAR;
950
    nHHBitmaps = SendMessageW(hToolbar, TB_ADDBITMAP, HHTB_NUMBITMAPS, (LPARAM)&tbAB);
951

952 953
    *pdwNumButtons = 0;

954 955 956 957 958 959 960 961
    unsupported = dwButtonFlags & (HHWIN_BUTTON_BROWSE_FWD |
        HHWIN_BUTTON_BROWSE_BCK | HHWIN_BUTTON_NOTES | HHWIN_BUTTON_CONTENTS |
        HHWIN_BUTTON_INDEX | HHWIN_BUTTON_SEARCH | HHWIN_BUTTON_HISTORY |
        HHWIN_BUTTON_FAVORITES | HHWIN_BUTTON_JUMP1 | HHWIN_BUTTON_JUMP2 |
        HHWIN_BUTTON_ZOOM | HHWIN_BUTTON_TOC_NEXT | HHWIN_BUTTON_TOC_PREV);
    if (unsupported)
        FIXME("got asked for unsupported buttons: %06x\n", unsupported);

962
    if (dwButtonFlags & HHWIN_BUTTON_EXPAND)
963
    {
964 965
        TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_EXPAND, nHHBitmaps + HHTB_EXPAND);
        TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_CONTRACT, nHHBitmaps + HHTB_CONTRACT);
966 967 968 969 970 971

        if (pHHInfo->WinType.fNotExpanded)
            pButtons[1].fsState |= TBSTATE_HIDDEN;
        else
            pButtons[0].fsState |= TBSTATE_HIDDEN;
    }
972 973

    if (dwButtonFlags & HHWIN_BUTTON_BACK)
974
        TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_BACK, nHistBitmaps + HIST_BACK);
975 976

    if (dwButtonFlags & HHWIN_BUTTON_FORWARD)
977
        TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_FORWARD, nHistBitmaps + HIST_FORWARD);
978 979

    if (dwButtonFlags & HHWIN_BUTTON_STOP)
980
        TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_STOP, nHHBitmaps + HHTB_STOP);
981 982

    if (dwButtonFlags & HHWIN_BUTTON_REFRESH)
983
        TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_REFRESH, nHHBitmaps + HHTB_REFRESH);
984 985

    if (dwButtonFlags & HHWIN_BUTTON_HOME)
986
        TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_HOME, nHHBitmaps + HHTB_HOME);
987 988

    if (dwButtonFlags & HHWIN_BUTTON_SYNC)
989
        TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_SYNC, nHHBitmaps + HHTB_SYNC);
990 991

    if (dwButtonFlags & HHWIN_BUTTON_OPTIONS)
992
        TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_OPTIONS, nStdBitmaps + STD_PROPERTIES);
993 994

    if (dwButtonFlags & HHWIN_BUTTON_PRINT)
995
        TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_PRINT, nStdBitmaps + STD_PRINT);
996 997
}

998 999
static BOOL HH_AddToolbar(HHInfo *pHHInfo)
{
1000
    HWND hToolbar;
1001
    HWND hwndParent = pHHInfo->WinType.hwndHelp;
1002
    DWORD toolbarFlags;
1003 1004 1005 1006
    TBBUTTON buttons[IDTB_TOC_PREV - IDTB_EXPAND];
    DWORD dwStyles, dwExStyles;
    DWORD dwNumButtons, dwIndex;

1007 1008
    if (pHHInfo->WinType.fsWinProperties & HHWIN_PARAM_TB_FLAGS)
        toolbarFlags = pHHInfo->WinType.fsToolBarFlags;
1009 1010 1011
    else
        toolbarFlags = HHWIN_DEF_BUTTONS;

1012
    dwStyles = WS_CHILDWINDOW | TBSTYLE_FLAT | TBSTYLE_WRAPABLE | TBSTYLE_TOOLTIPS | CCS_NODIVIDER;
1013 1014 1015 1016
    dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;

    hToolbar = CreateWindowExW(dwExStyles, TOOLBARCLASSNAMEW, NULL, dwStyles,
                               0, 0, 0, 0, hwndParent, NULL,
1017
                               hhctrl_hinstance, NULL);
1018 1019
    if (!hToolbar)
        return FALSE;
1020
    pHHInfo->WinType.hwndToolBar = hToolbar;
1021 1022 1023 1024 1025

    SendMessageW(hToolbar, TB_SETBITMAPSIZE, 0, MAKELONG(ICON_SIZE, ICON_SIZE));
    SendMessageW(hToolbar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
    SendMessageW(hToolbar, WM_SETFONT, (WPARAM)pHHInfo->hFont, TRUE);

1026
    TB_AddButtonsFromFlags(pHHInfo, buttons, toolbarFlags, &dwNumButtons);
1027 1028 1029 1030 1031

    for (dwIndex = 0; dwIndex < dwNumButtons; dwIndex++)
    {
        LPWSTR szBuf = HH_LoadString(buttons[dwIndex].idCommand);
        DWORD dwLen = strlenW(szBuf);
1032
        szBuf[dwLen + 1] = 0; /* Double-null terminate */
1033 1034

        buttons[dwIndex].iString = (DWORD)SendMessageW(hToolbar, TB_ADDSTRINGW, 0, (LPARAM)szBuf);
1035
        heap_free(szBuf);
1036 1037
    }

1038
    SendMessageW(hToolbar, TB_ADDBUTTONSW, dwNumButtons, (LPARAM)buttons);
1039
    SendMessageW(hToolbar, TB_AUTOSIZE, 0, 0);
1040 1041
    if (pHHInfo->WinType.fsWinProperties & HHWIN_PROP_TRI_PANE)
        ShowWindow(hToolbar, SW_SHOW);
1042

1043 1044 1045 1046 1047
    return TRUE;
}

/* Navigation Pane */

1048 1049
static void NP_GetNavigationRect(HHInfo *pHHInfo, RECT *rc)
{
1050 1051
    HWND hwndParent = pHHInfo->WinType.hwndHelp;
    HWND hwndToolbar = pHHInfo->WinType.hwndToolBar;
1052 1053 1054 1055 1056 1057 1058 1059 1060
    RECT rectWND, rectTB;

    GetClientRect(hwndParent, &rectWND);
    GetClientRect(hwndToolbar, &rectTB);

    rc->left = 0;
    rc->top = rectTB.bottom;
    rc->bottom = rectWND.bottom - rectTB.bottom;

1061 1062
    if (!(pHHInfo->WinType.fsValidMembers & HHWIN_PARAM_NAV_WIDTH) &&
          pHHInfo->WinType.iNavWidth == 0)
1063
    {
1064
        pHHInfo->WinType.iNavWidth = WINTYPE_DEFAULT_NAVWIDTH;
1065 1066
    }

1067
    rc->right = pHHInfo->WinType.iNavWidth;
1068 1069
}

1070
static DWORD NP_CreateTab(HINSTANCE hInstance, HWND hwndTabCtrl, DWORD index)
1071 1072
{
    TCITEMW tie;
1073 1074
    LPWSTR tabText = HH_LoadString(index);
    DWORD ret;
1075 1076 1077 1078

    tie.mask = TCIF_TEXT;
    tie.pszText = tabText;

1079 1080
    ret = SendMessageW( hwndTabCtrl, TCM_INSERTITEMW, index, (LPARAM)&tie );

1081
    heap_free(tabText);
1082
    return ret;
1083 1084
}

1085
static BOOL HH_AddNavigationPane(HHInfo *info)
1086
{
1087
    HWND hWnd, hwndTabCtrl;
1088
    HWND hwndParent = info->WinType.hwndHelp;
1089
    DWORD dwStyles = WS_CHILDWINDOW;
1090 1091 1092
    DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
    RECT rc;

1093
    if (navigation_visible(info))
1094 1095
        dwStyles |= WS_VISIBLE;

1096
    NP_GetNavigationRect(info, &rc);
1097 1098 1099

    hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
                           rc.left, rc.top, rc.right, rc.bottom,
1100
                           hwndParent, NULL, hhctrl_hinstance, NULL);
1101 1102 1103
    if (!hWnd)
        return FALSE;

1104
    SetWindowLongPtrW(hWnd, 0, (LONG_PTR)info);
1105

1106
    hwndTabCtrl = CreateWindowExW(dwExStyles, WC_TABCONTROLW, szEmpty, dwStyles | WS_VISIBLE,
1107 1108 1109
                                  0, TAB_TOP_PADDING,
                                  rc.right - TAB_RIGHT_PADDING,
                                  rc.bottom - TAB_TOP_PADDING,
1110
                                  hWnd, NULL, hhctrl_hinstance, NULL);
1111 1112 1113
    if (!hwndTabCtrl)
        return FALSE;

1114
    if (*info->WinType.pszToc)
1115
        info->tabs[TAB_CONTENTS].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_CONTENTS);
1116

1117
    if (*info->WinType.pszIndex)
1118
        info->tabs[TAB_INDEX].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_INDEX);
1119

1120
    if (info->WinType.fsWinProperties & HHWIN_PROP_TAB_SEARCH)
1121
        info->tabs[TAB_SEARCH].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_SEARCH);
1122

1123
    if (info->WinType.fsWinProperties & HHWIN_PROP_TAB_FAVORITES)
1124
        info->tabs[TAB_FAVORITES].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_FAVORITES);
1125

1126
    SendMessageW(hwndTabCtrl, WM_SETFONT, (WPARAM)info->hFont, TRUE);
1127

1128 1129
    info->hwndTabCtrl = hwndTabCtrl;
    info->WinType.hwndNavigation = hWnd;
1130 1131 1132 1133 1134
    return TRUE;
}

/* HTML Pane */

1135
static void HP_GetHTMLRect(HHInfo *info, RECT *rc)
1136
{
James Hawkins's avatar
James Hawkins committed
1137
    RECT rectTB, rectWND, rectNP, rectSB;
1138

1139 1140
    GetClientRect(info->WinType.hwndHelp, &rectWND);
    GetClientRect(info->hwndSizeBar, &rectSB);
1141

1142 1143 1144
    rc->left = 0;
    rc->top = 0;
    if (navigation_visible(info))
1145 1146
    {
        GetClientRect(info->WinType.hwndNavigation, &rectNP);
1147 1148 1149 1150 1151 1152
        rc->left += rectNP.right + rectSB.right;
    }
    if (info->WinType.fsWinProperties & HHWIN_PROP_TRI_PANE)
    {
        GetClientRect(info->WinType.hwndToolBar, &rectTB);
        rc->top += rectTB.bottom;
1153
    }
James Hawkins's avatar
James Hawkins committed
1154
    rc->right = rectWND.right - rc->left;
1155
    rc->bottom = rectWND.bottom - rc->top;
1156 1157
}

1158 1159
static BOOL HH_AddHTMLPane(HHInfo *pHHInfo)
{
1160
    HWND hWnd;
1161
    HWND hwndParent = pHHInfo->WinType.hwndHelp;
1162 1163 1164 1165 1166 1167 1168 1169
    DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN;
    DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_CLIENTEDGE;
    RECT rc;

    HP_GetHTMLRect(pHHInfo, &rc);

    hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
                           rc.left, rc.top, rc.right, rc.bottom,
1170
                           hwndParent, NULL, hhctrl_hinstance, NULL);
1171 1172 1173
    if (!hWnd)
        return FALSE;

1174
    if (!InitWebBrowser(pHHInfo, hWnd))
1175 1176
        return FALSE;

1177
    /* store the pointer to the HH info struct */
1178
    SetWindowLongPtrW(hWnd, 0, (LONG_PTR)pHHInfo);
1179 1180 1181 1182

    ShowWindow(hWnd, SW_SHOW);
    UpdateWindow(hWnd);

1183
    pHHInfo->WinType.hwndHTML = hWnd;
1184 1185 1186
    return TRUE;
}

1187 1188
static BOOL AddContentTab(HHInfo *info)
{
1189 1190 1191 1192
    HIMAGELIST hImageList;
    HBITMAP hBitmap;
    HWND hWnd;

1193 1194
    if(info->tabs[TAB_CONTENTS].id == -1)
        return TRUE; /* No "Contents" tab */
1195 1196 1197 1198
    hWnd = CreateWindowExW(WS_EX_CLIENTEDGE, WC_TREEVIEWW, szEmpty, WS_CHILD | WS_BORDER | TVS_LINESATROOT
                           | TVS_SHOWSELALWAYS | TVS_HASBUTTONS, 50, 50, 100, 100,
                           info->WinType.hwndNavigation, NULL, hhctrl_hinstance, NULL);
    if(!hWnd) {
1199 1200 1201 1202
        ERR("Could not create treeview control\n");
        return FALSE;
    }

1203 1204 1205 1206 1207 1208 1209
    hImageList = ImageList_Create(16, 16, ILC_COLOR32, 0, HHTV_NUMBITMAPS);
    hBitmap = LoadBitmapW(hhctrl_hinstance, MAKEINTRESOURCEW(IDB_HHTREEVIEW));
    ImageList_Add(hImageList, hBitmap, NULL);
    SendMessageW(hWnd, TVM_SETIMAGELIST, TVSIL_NORMAL, (LPARAM)hImageList);

    info->contents.hImageList = hImageList;
    info->tabs[TAB_CONTENTS].hwnd = hWnd;
1210
    ResizeTabChild(info, TAB_CONTENTS);
1211
    ShowWindow(hWnd, SW_SHOW);
1212 1213 1214 1215

    return TRUE;
}

1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
static BOOL AddIndexTab(HHInfo *info)
{
    char hidden_column[] = "Column";
    LVCOLUMNA lvc;

    if(info->tabs[TAB_INDEX].id == -1)
        return TRUE; /* No "Index" tab */
    info->tabs[TAB_INDEX].hwnd = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW,
           szEmpty, WS_CHILD | WS_BORDER | LVS_SINGLESEL | LVS_REPORT | LVS_NOCOLUMNHEADER, 50, 50, 100, 100,
           info->WinType.hwndNavigation, NULL, hhctrl_hinstance, NULL);
    if(!info->tabs[TAB_INDEX].hwnd) {
        ERR("Could not create ListView control\n");
        return FALSE;
    }
    memset(&lvc, 0, sizeof(lvc));
    lvc.mask = LVCF_TEXT;
    lvc.pszText = hidden_column;
    if(SendMessageW(info->tabs[TAB_INDEX].hwnd, LVM_INSERTCOLUMNA, 0, (LPARAM) &lvc) == -1)
    {
        ERR("Could not create ListView column\n");
        return FALSE;
    }

    ResizeTabChild(info, TAB_INDEX);
    ShowWindow(info->tabs[TAB_INDEX].hwnd, SW_HIDE);

    return TRUE;
}

1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
static BOOL AddSearchTab(HHInfo *info)
{
    HWND hwndList, hwndEdit, hwndContainer;
    char hidden_column[] = "Column";
    WNDPROC editWndProc;
    LVCOLUMNA lvc;

    if(info->tabs[TAB_SEARCH].id == -1)
        return TRUE; /* No "Search" tab */
    hwndContainer = CreateWindowExW(WS_EX_CONTROLPARENT, szChildClass, szEmpty,
                                    WS_CHILD, 0, 0, 0, 0, info->WinType.hwndNavigation,
                                    NULL, hhctrl_hinstance, NULL);
    if(!hwndContainer) {
        ERR("Could not create search window container control.\n");
        return FALSE;
    }
    hwndEdit = CreateWindowExW(WS_EX_CLIENTEDGE, WC_EDITW, szEmpty, WS_CHILD
                                | WS_VISIBLE | ES_LEFT | SS_NOTIFY, 0, 0, 0, 0,
                               hwndContainer, NULL, hhctrl_hinstance, NULL);
    if(!hwndEdit) {
        ERR("Could not create search ListView control.\n");
        return FALSE;
    }
    if(SendMessageW(hwndEdit, WM_SETFONT, (WPARAM) info->hFont, (LPARAM) FALSE) == -1)
    {
        ERR("Could not set font for edit control.\n");
        return FALSE;
    }
    editWndProc = (WNDPROC) SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC, (LONG_PTR)EditChild_WndProc);
    if(!editWndProc) {
        ERR("Could not redirect messages for edit control.\n");
        return FALSE;
    }
    SetWindowLongPtrW(hwndEdit, GWLP_USERDATA, (LONG_PTR)editWndProc);
    hwndList = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW, szEmpty,
                               WS_CHILD | WS_VISIBLE | WS_BORDER | LVS_SINGLESEL
                                | LVS_REPORT | LVS_NOCOLUMNHEADER, 0, 0, 0, 0,
                               hwndContainer, NULL, hhctrl_hinstance, NULL);
    if(!hwndList) {
        ERR("Could not create search ListView control.\n");
        return FALSE;
    }
    memset(&lvc, 0, sizeof(lvc));
    lvc.mask = LVCF_TEXT;
    lvc.pszText = hidden_column;
    if(SendMessageW(hwndList, LVM_INSERTCOLUMNA, 0, (LPARAM) &lvc) == -1)
    {
        ERR("Could not create ListView column\n");
        return FALSE;
    }

    info->search.hwndEdit = hwndEdit;
    info->search.hwndList = hwndList;
    info->search.hwndContainer = hwndContainer;
    info->tabs[TAB_SEARCH].hwnd = hwndContainer;

1301
    SetWindowLongPtrW(hwndContainer, 0, (LONG_PTR)info);
1302 1303 1304 1305 1306 1307

    ResizeTabChild(info, TAB_SEARCH);

    return TRUE;
}

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
/* The Index tab's sub-topic popup */

static void ResizePopupChild(HHInfo *info)
{
    int scroll_width = GetSystemMetrics(SM_CXVSCROLL);
    int border_width = GetSystemMetrics(SM_CXBORDER);
    int edge_width = GetSystemMetrics(SM_CXEDGE);
    INT width, height;
    RECT rect;

    if(!info)
        return;

    GetClientRect(info->popup.hwndPopup, &rect);
    SetWindowPos(info->popup.hwndCallback, HWND_TOP, 0, 0,
                 rect.right, rect.bottom, SWP_NOMOVE);

    rect.left = TAB_MARGIN;
    rect.top = TAB_TOP_PADDING + TAB_MARGIN;
    rect.right -= TAB_RIGHT_PADDING + TAB_MARGIN;
    rect.bottom -= TAB_MARGIN;
    width = rect.right-rect.left;
    height = rect.bottom-rect.top;

    SetWindowPos(info->popup.hwndList, NULL, rect.left, rect.top, width, height,
                 SWP_NOZORDER | SWP_NOACTIVATE);

    SendMessageW(info->popup.hwndList, LVM_SETCOLUMNWIDTH, 0,
                 width-scroll_width-2*border_width-2*edge_width);
}

static LRESULT CALLBACK HelpPopup_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
1341
    HHInfo *info = (HHInfo *)GetWindowLongPtrW(hWnd, 0);
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367

    switch (message)
    {
    case WM_SIZE:
        ResizePopupChild(info);
        return 0;
    case WM_DESTROY:
        DestroyWindow(hWnd);
        return 0;
    case WM_CLOSE:
        ShowWindow(hWnd, SW_HIDE);
        return 0;

    default:
        return DefWindowProcW(hWnd, message, wParam, lParam);
    }

    return 0;
}

static LRESULT CALLBACK PopupChild_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_NOTIFY: {
        NMHDR *nmhdr = (NMHDR*)lParam;
1368 1369 1370
        switch(nmhdr->code)
        {
        case NM_DBLCLK: {
1371
            HHInfo *info = (HHInfo*)GetWindowLongPtrW(hWnd, 0);
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
            IndexSubItem *iter;

            if(info == 0 || lParam == 0)
                return 0;
            iter = (IndexSubItem*) ((NMITEMACTIVATE *)lParam)->lParam;
            if(iter == 0)
                return 0;
            NavigateToChm(info, info->index->merge.chm_file, iter->local);
            ShowWindow(info->popup.hwndPopup, SW_HIDE);
            return 0;
        }
1383
        case NM_RETURN: {
1384
            HHInfo *info = (HHInfo*)GetWindowLongPtrW(hWnd, 0);
1385 1386 1387 1388 1389 1390 1391 1392
            IndexSubItem *iter;
            LVITEMW lvItem;

            if(info == 0)
                return 0;

            lvItem.iItem = (int) SendMessageW(info->popup.hwndList, LVM_GETSELECTIONMARK, 0, 0);
            lvItem.mask = TVIF_PARAM;
1393
            SendMessageW(info->popup.hwndList, LVM_GETITEMW, 0, (LPARAM)&lvItem);
1394 1395 1396 1397 1398 1399
            iter = (IndexSubItem*) lvItem.lParam;
            NavigateToChm(info, info->index->merge.chm_file, iter->local);
            ShowWindow(info->popup.hwndPopup, SW_HIDE);
            return 0;
        }
        }
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
        break;
    }
    default:
        return DefWindowProcW(hWnd, message, wParam, lParam);
    }

    return 0;
}

static BOOL AddIndexPopup(HHInfo *info)
{
    static const WCHAR szPopupChildClass[] = {'H','H',' ','P','o','p','u','p',' ','C','h','i','l','d',0};
    static const WCHAR windowCaptionW[] = {'S','e','l','e','c','t',' ','T','o','p','i','c',':',0};
    static const WCHAR windowClassW[] = {'H','H',' ','P','o','p','u','p',0};
    HWND hwndList, hwndPopup, hwndCallback;
    char hidden_column[] = "Column";
    WNDCLASSEXW wcex;
    LVCOLUMNA lvc;

    if(info->tabs[TAB_INDEX].id == -1)
        return TRUE; /* No "Index" tab */

    wcex.cbSize         = sizeof(WNDCLASSEXW);
    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = HelpPopup_WndProc;
    wcex.cbClsExtra     = 0;
1426
    wcex.cbWndExtra     = sizeof(LONG_PTR);
1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
    wcex.hInstance      = hhctrl_hinstance;
    wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
    wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_MENU + 1);
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = windowClassW;
    wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
    RegisterClassExW(&wcex);

    wcex.cbSize         = sizeof(WNDCLASSEXW);
    wcex.style          = 0;
    wcex.lpfnWndProc    = PopupChild_WndProc;
    wcex.cbClsExtra     = 0;
1440
    wcex.cbWndExtra     = sizeof(LONG_PTR);
1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
    wcex.hInstance      = hhctrl_hinstance;
    wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
    wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_BTNFACE + 1);
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = szPopupChildClass;
    wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
    RegisterClassExW(&wcex);

    hwndPopup = CreateWindowExW(WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_APPWINDOW
                                 | WS_EX_WINDOWEDGE | WS_EX_RIGHTSCROLLBAR,
                                windowClassW, windowCaptionW, WS_POPUPWINDOW
                                 | WS_OVERLAPPEDWINDOW | WS_VISIBLE
                                 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, CW_USEDEFAULT,
                                CW_USEDEFAULT, 300, 200, info->WinType.hwndHelp,
                                NULL, hhctrl_hinstance, NULL);
    if (!hwndPopup)
        return FALSE;

    hwndCallback = CreateWindowExW(WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR,
                                   szPopupChildClass, szEmpty, WS_CHILDWINDOW | WS_VISIBLE,
                                   0, 0, 0, 0,
                                   hwndPopup, NULL, hhctrl_hinstance, NULL);
    if (!hwndCallback)
        return FALSE;

    ShowWindow(hwndPopup, SW_HIDE);
    hwndList = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW, szEmpty,
                               WS_CHILD | WS_BORDER | LVS_SINGLESEL | LVS_REPORT
                                | LVS_NOCOLUMNHEADER, 50, 50, 100, 100,
                               hwndCallback, NULL, hhctrl_hinstance, NULL);
    if(!hwndList) {
        ERR("Could not create popup ListView control\n");
        return FALSE;
    }
    memset(&lvc, 0, sizeof(lvc));
    lvc.mask = LVCF_TEXT;
    lvc.pszText = hidden_column;
    if(SendMessageW(hwndList, LVM_INSERTCOLUMNA, 0, (LPARAM) &lvc) == -1)
    {
        ERR("Could not create popup ListView column\n");
        return FALSE;
    }

    info->popup.hwndCallback = hwndCallback;
    info->popup.hwndPopup = hwndPopup;
    info->popup.hwndList = hwndList;
1488 1489
    SetWindowLongPtrW(hwndPopup, 0, (LONG_PTR)info);
    SetWindowLongPtrW(hwndCallback, 0, (LONG_PTR)info);
1490 1491 1492 1493 1494 1495 1496

    ResizePopupChild(info);
    ShowWindow(hwndList, SW_SHOW);

    return TRUE;
}

1497 1498
/* Viewer Window */

1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
static void ExpandContract(HHInfo *pHHInfo)
{
    RECT r, nav;

    pHHInfo->WinType.fNotExpanded = !pHHInfo->WinType.fNotExpanded;
    GetWindowRect(pHHInfo->WinType.hwndHelp, &r);
    NP_GetNavigationRect(pHHInfo, &nav);

    /* hide/show both the nav bar and the size bar */
    if (pHHInfo->WinType.fNotExpanded)
    {
        ShowWindow(pHHInfo->WinType.hwndNavigation, SW_HIDE);
        ShowWindow(pHHInfo->hwndSizeBar, SW_HIDE);
        r.left = r.left + nav.right;

        SendMessageW(pHHInfo->WinType.hwndToolBar, TB_HIDEBUTTON, IDTB_EXPAND, MAKELPARAM(FALSE, 0));
        SendMessageW(pHHInfo->WinType.hwndToolBar, TB_HIDEBUTTON, IDTB_CONTRACT, MAKELPARAM(TRUE, 0));
    }
    else
    {
        ShowWindow(pHHInfo->WinType.hwndNavigation, SW_SHOW);
        ShowWindow(pHHInfo->hwndSizeBar, SW_SHOW);
        r.left = r.left - nav.right;

        SendMessageW(pHHInfo->WinType.hwndToolBar, TB_HIDEBUTTON, IDTB_EXPAND, MAKELPARAM(TRUE, 0));
        SendMessageW(pHHInfo->WinType.hwndToolBar, TB_HIDEBUTTON, IDTB_CONTRACT, MAKELPARAM(FALSE, 0));
    }

    MoveWindow(pHHInfo->WinType.hwndHelp, r.left, r.top, r.right-r.left, r.bottom-r.top, TRUE);
}

1530
static LRESULT Help_OnSize(HWND hWnd)
1531
{
1532
    HHInfo *pHHInfo = (HHInfo *)GetWindowLongPtrW(hWnd, 0);
1533
    DWORD dwSize;
1534 1535 1536
    RECT rc;

    if (!pHHInfo)
1537
        return 0;
1538

1539
    if (navigation_visible(pHHInfo))
1540 1541 1542 1543
    {
        NP_GetNavigationRect(pHHInfo, &rc);
        SetWindowPos(pHHInfo->WinType.hwndNavigation, HWND_TOP, 0, 0,
                     rc.right, rc.bottom, SWP_NOMOVE);
1544

1545 1546 1547 1548 1549
        SB_GetSizeBarRect(pHHInfo, &rc);
        SetWindowPos(pHHInfo->hwndSizeBar, HWND_TOP, rc.left, rc.top,
                     rc.right, rc.bottom, SWP_SHOWWINDOW);

    }
1550 1551

    HP_GetHTMLRect(pHHInfo, &rc);
1552
    SetWindowPos(pHHInfo->WinType.hwndHTML, HWND_TOP, rc.left, rc.top,
1553 1554 1555 1556
                 rc.right, rc.bottom, SWP_SHOWWINDOW);

    /* Resize browser window taking the frame size into account */
    dwSize = GetSystemMetrics(SM_CXFRAME);
1557
    ResizeWebBrowser(pHHInfo, rc.right - dwSize, rc.bottom - dwSize);
1558 1559

    return 0;
1560 1561
}

1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581
void UpdateHelpWindow(HHInfo *info)
{
    if (!info->WinType.hwndHelp)
        return;

    WARN("Only the size of the window is currently updated.\n");
    if (info->WinType.fsValidMembers & HHWIN_PARAM_RECT)
    {
        RECT *rect = &info->WinType.rcWindowPos;
        INT x, y, width, height;

        x = rect->left;
        y = rect->top;
        width = rect->right - x;
        height = rect->bottom - y;
        SetWindowPos(info->WinType.hwndHelp, NULL, rect->left, rect->top, width, height,
                     SWP_NOZORDER | SWP_NOACTIVATE);
    }
}

1582
static LRESULT CALLBACK Help_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1583 1584 1585
{
    switch (message)
    {
1586 1587 1588 1589 1590 1591
    case WM_COMMAND:
        if (HIWORD(wParam) == BN_CLICKED)
            TB_OnClick(hWnd, LOWORD(wParam));
        break;
    case WM_SIZE:
        return Help_OnSize(hWnd);
1592
    case WM_CLOSE:
1593
        ReleaseHelpViewer((HHInfo *)GetWindowLongPtrW(hWnd, 0));
1594
        return 0;
1595
    case WM_DESTROY:
1596 1597
        if(hh_process)
            PostQuitMessage(0);
1598 1599 1600 1601
        break;

    default:
        return DefWindowProcW(hWnd, message, wParam, lParam);
1602 1603 1604 1605 1606
    }

    return 0;
}

1607
static BOOL HH_CreateHelpWindow(HHInfo *info)
1608
{
1609
    HWND hWnd, parent = 0;
1610
    RECT winPos = info->WinType.rcWindowPos;
1611 1612
    WNDCLASSEXW wcex;
    DWORD dwStyles, dwExStyles;
1613 1614
    DWORD x, y, width = 0, height = 0;
    LPCWSTR caption;
1615 1616 1617 1618 1619 1620 1621

    static const WCHAR windowClassW[] = {
        'H','H',' ', 'P','a','r','e','n','t',0
    };

    wcex.cbSize         = sizeof(WNDCLASSEXW);
    wcex.style          = CS_HREDRAW | CS_VREDRAW;
1622
    wcex.lpfnWndProc    = Help_WndProc;
1623
    wcex.cbClsExtra     = 0;
1624
    wcex.cbWndExtra     = sizeof(LONG_PTR);
1625
    wcex.hInstance      = hhctrl_hinstance;
1626 1627
    wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
    wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
1628
    wcex.hbrBackground  = (HBRUSH)(COLOR_MENU + 1);
1629 1630 1631 1632 1633 1634
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = windowClassW;
    wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);

    RegisterClassExW(&wcex);

1635
    /* Read in window parameters if available */
1636
    if (info->WinType.fsValidMembers & HHWIN_PARAM_STYLES)
1637 1638 1639 1640 1641
    {
        dwStyles = info->WinType.dwStyles;
        if (!(info->WinType.dwStyles & WS_CHILD))
            dwStyles |= WS_OVERLAPPEDWINDOW;
    }
1642 1643 1644 1645
    else
        dwStyles = WS_OVERLAPPEDWINDOW | WS_VISIBLE |
                   WS_CLIPSIBLINGS | WS_CLIPCHILDREN;

1646 1647
    if (info->WinType.fsValidMembers & HHWIN_PARAM_EXSTYLES)
        dwExStyles = info->WinType.dwExStyles;
1648 1649 1650
    else
        dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_APPWINDOW |
                     WS_EX_WINDOWEDGE | WS_EX_RIGHTSCROLLBAR;
1651

1652
    if (info->WinType.fsValidMembers & HHWIN_PARAM_RECT)
1653 1654 1655 1656 1657 1658
    {
        x = winPos.left;
        y = winPos.top;
        width = winPos.right - x;
        height = winPos.bottom - y;
    }
1659
    if (!width || !height)
1660 1661 1662 1663 1664 1665
    {
        x = WINTYPE_DEFAULT_X;
        y = WINTYPE_DEFAULT_Y;
        width = WINTYPE_DEFAULT_WIDTH;
        height = WINTYPE_DEFAULT_HEIGHT;
    }
1666

1667
    if (!(info->WinType.fsWinProperties & HHWIN_PROP_TRI_PANE) && info->WinType.fNotExpanded)
1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679
    {
        if (!(info->WinType.fsValidMembers & HHWIN_PARAM_NAV_WIDTH) &&
              info->WinType.iNavWidth == 0)
        {
            info->WinType.iNavWidth = WINTYPE_DEFAULT_NAVWIDTH;
        }

        x += info->WinType.iNavWidth;
        width -= info->WinType.iNavWidth;
    }


1680 1681 1682
    caption = info->WinType.pszCaption;
    if (!*caption) caption = info->pCHMInfo->defTitle;

1683 1684 1685
    if (info->WinType.dwStyles & WS_CHILD)
        parent = info->WinType.hwndCaller;

1686
    hWnd = CreateWindowExW(dwExStyles, windowClassW, caption,
1687
                           dwStyles, x, y, width, height, parent, NULL, hhctrl_hinstance, NULL);
1688 1689 1690 1691 1692 1693 1694
    if (!hWnd)
        return FALSE;

    ShowWindow(hWnd, SW_SHOW);
    UpdateWindow(hWnd);

    /* store the pointer to the HH info struct */
1695
    SetWindowLongPtrW(hWnd, 0, (LONG_PTR)info);
1696

1697
    info->WinType.hwndHelp = hWnd;
1698 1699 1700 1701 1702 1703 1704
    return TRUE;
}

static void HH_CreateFont(HHInfo *pHHInfo)
{
    LOGFONTW lf;

1705
    GetObjectW(GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONTW), &lf);
1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722
    lf.lfWeight = FW_NORMAL;
    lf.lfItalic = FALSE;
    lf.lfUnderline = FALSE;

    pHHInfo->hFont = CreateFontIndirectW(&lf);
}

static void HH_InitRequiredControls(DWORD dwControls)
{
    INITCOMMONCONTROLSEX icex;

    icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    icex.dwICC = dwControls;
    InitCommonControlsEx(&icex);
}

/* Creates the whole package */
1723
static BOOL CreateViewer(HHInfo *pHHInfo)
1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734
{
    HH_CreateFont(pHHInfo);

    if (!HH_CreateHelpWindow(pHHInfo))
        return FALSE;

    HH_InitRequiredControls(ICC_BAR_CLASSES);

    if (!HH_AddToolbar(pHHInfo))
        return FALSE;

1735 1736
    HH_RegisterChildWndClass(pHHInfo);

1737 1738 1739
    if (!HH_AddNavigationPane(pHHInfo))
        return FALSE;

James Hawkins's avatar
James Hawkins committed
1740 1741 1742 1743 1744
    HH_RegisterSizeBarClass(pHHInfo);

    if (!HH_AddSizeBar(pHHInfo))
        return FALSE;

1745 1746 1747
    if (!HH_AddHTMLPane(pHHInfo))
        return FALSE;

1748 1749 1750
    if (!AddContentTab(pHHInfo))
        return FALSE;

1751 1752 1753
    if (!AddIndexTab(pHHInfo))
        return FALSE;

1754 1755 1756
    if (!AddIndexPopup(pHHInfo))
        return FALSE;

1757 1758 1759
    if (!AddSearchTab(pHHInfo))
        return FALSE;

1760
    InitContent(pHHInfo);
1761
    InitIndex(pHHInfo);
1762

1763
    pHHInfo->viewer_initialized = TRUE;
1764 1765 1766
    return TRUE;
}

1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780
void wintype_stringsW_free(struct wintype_stringsW *stringsW)
{
    heap_free(stringsW->pszType);
    heap_free(stringsW->pszCaption);
    heap_free(stringsW->pszToc);
    heap_free(stringsW->pszIndex);
    heap_free(stringsW->pszFile);
    heap_free(stringsW->pszHome);
    heap_free(stringsW->pszJump1);
    heap_free(stringsW->pszJump2);
    heap_free(stringsW->pszUrlJump1);
    heap_free(stringsW->pszUrlJump2);
}

1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
void wintype_stringsA_free(struct wintype_stringsA *stringsA)
{
    heap_free(stringsA->pszType);
    heap_free(stringsA->pszCaption);
    heap_free(stringsA->pszToc);
    heap_free(stringsA->pszIndex);
    heap_free(stringsA->pszFile);
    heap_free(stringsA->pszHome);
    heap_free(stringsA->pszJump1);
    heap_free(stringsA->pszJump2);
    heap_free(stringsA->pszUrlJump1);
    heap_free(stringsA->pszUrlJump2);
    heap_free(stringsA->pszCustomTabs);
}

1796
void ReleaseHelpViewer(HHInfo *info)
1797
{
1798 1799
    TRACE("(%p)\n", info);

1800
    if (!info)
1801 1802
        return;

1803 1804
    list_remove(&info->entry);

1805
    wintype_stringsA_free(&info->stringsA);
1806
    wintype_stringsW_free(&info->stringsW);
1807 1808 1809 1810

    if (info->pCHMInfo)
        CloseCHM(info->pCHMInfo);

1811
    ReleaseWebBrowser(info);
1812
    ReleaseContent(info);
1813
    ReleaseIndex(info);
1814
    ReleaseSearch(info);
1815

1816 1817
    if(info->contents.hImageList)
        ImageList_Destroy(info->contents.hImageList);
1818 1819 1820
    if(info->WinType.hwndHelp)
        DestroyWindow(info->WinType.hwndHelp);

1821
    heap_free(info);
1822
    OleUninitialize();
1823 1824
}

1825
HHInfo *CreateHelpViewer(HHInfo *info, LPCWSTR filename, HWND caller)
1826
{
1827
    HHInfo *tmp_info;
1828 1829
    int i;

1830 1831 1832
    if(!info)
    {
        info = heap_alloc_zero(sizeof(HHInfo));
1833
        list_add_tail(&window_list, &info->entry);
1834 1835
    }

1836 1837 1838 1839 1840
    /* Set the invalid tab ID (-1) as the default value for all
     * of the tabs, this matches a failed TCM_INSERTITEM call.
     */
    for(i=0;i<sizeof(info->tabs)/sizeof(HHTab);i++)
        info->tabs[i].id = -1;
1841 1842 1843 1844 1845 1846 1847 1848

    OleInitialize(NULL);

    info->pCHMInfo = OpenCHM(filename);
    if(!info->pCHMInfo) {
        ReleaseHelpViewer(info);
        return NULL;
    }
1849

1850
    if (!LoadWinTypeFromCHM(info)) {
1851
        ReleaseHelpViewer(info);
1852 1853
        return NULL;
    }
1854
    info->WinType.hwndCaller = caller;
1855

1856 1857 1858 1859 1860 1861 1862
    /* If the window is already open then load the file in that existing window */
    if ((tmp_info = find_window(info->WinType.pszType)) && tmp_info != info)
    {
        ReleaseHelpViewer(info);
        return CreateHelpViewer(tmp_info, filename, caller);
    }

1863
    if(!info->viewer_initialized && !CreateViewer(info)) {
1864
        ReleaseHelpViewer(info);
1865 1866 1867
        return NULL;
    }

1868
    return info;
1869
}
1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895

/*
 * Search the table of HTML entities and return the corresponding ANSI symbol.
 */
static char find_html_symbol(const char *entity, int entity_len)
{
    int max = sizeof(html_encoded_symbols)/sizeof(html_encoded_symbols[0])-1;
    int min = 0, dir;

    while(min <= max)
    {
        int pos = (min+max)/2;
        const char *encoded_symbol = html_encoded_symbols[pos].html_code;
        dir = strncmp(encoded_symbol, entity, entity_len);
        if(dir == 0 && !encoded_symbol[entity_len]) return html_encoded_symbols[pos].ansi_symbol;
        if(dir < 0)
            min = pos+1;
        else
            max = pos-1;
    }
    return 0;
}

/*
 * Decode a string containing HTML encoded characters into a unicode string.
 */
1896
WCHAR *decode_html(const char *html_fragment, int html_fragment_len, UINT code_page)
1897
{
1898 1899
    const char *h = html_fragment, *amp, *sem;
    char symbol, *tmp;
1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952
    int len, tmp_len = 0;
    WCHAR *unicode_text;

    tmp = heap_alloc(html_fragment_len+1);
    while(1)
    {
        symbol = 0;
        amp = strchr(h, '&');
        if(!amp) break;
        len = amp-h;
        /* Copy the characters prior to the HTML encoded character */
        memcpy(&tmp[tmp_len], h, len);
        tmp_len += len;
        amp++; /* skip ampersand */
        sem = strchr(amp, ';');
        /* Require a semicolon after the ampersand */
        if(!sem)
        {
            h = amp;
            tmp[tmp_len++] = '&';
            continue;
        }
        /* Find the symbol either by using the ANSI character number (prefixed by the pound symbol)
         * or by searching the HTML entity table */
        len = sem-amp;
        if(amp[0] == '#')
        {
            char *endnum = NULL;
            int tmp;

            tmp = (char) strtol(amp, &endnum, 10);
            if(endnum == sem)
                symbol = tmp;
        }
        else
            symbol = find_html_symbol(amp, len);
        if(!symbol)
        {
            FIXME("Failed to translate HTML encoded character '&%.*s;'.\n", len, amp);
            h = amp;
            tmp[tmp_len++] = '&';
            continue;
        }
        /* Insert the new symbol */
        h = sem+1;
        tmp[tmp_len++] = symbol;
    }
    /* Convert any remaining characters */
    len = html_fragment_len-(h-html_fragment);
    memcpy(&tmp[tmp_len], h, len);
    tmp_len += len;
    tmp[tmp_len++] = 0; /* NULL-terminate the string */

1953
    len = MultiByteToWideChar(code_page, 0, tmp, tmp_len, NULL, 0);
1954
    unicode_text = heap_alloc(len*sizeof(WCHAR));
1955
    MultiByteToWideChar(code_page, 0, tmp, tmp_len, unicode_text, len);
1956 1957 1958
    heap_free(tmp);
    return unicode_text;
}
1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971

/* Find the HTMLHelp structure for an existing window title */
HHInfo *find_window(const WCHAR *window)
{
    HHInfo *info;

    LIST_FOR_EACH_ENTRY(info, &window_list, HHInfo, entry)
    {
        if (strcmpW(info->WinType.pszType, window) == 0)
            return info;
    }
    return NULL;
}