help.c 55.4 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
/* Loads a string from the resource file */
static LPWSTR HH_LoadString(DWORD dwID)
{
    LPWSTR string = NULL;
172
    LPCWSTR stringresource;
173 174
    int iSize;

175
    iSize = LoadStringW(hhctrl_hinstance, dwID, (LPWSTR)&stringresource, 0);
176

177 178 179
    string = heap_alloc((iSize + 2) * sizeof(WCHAR)); /* some strings (tab text) needs double-null termination */
    memcpy(string, stringresource, iSize*sizeof(WCHAR));
    string[iSize] = 0;
180 181 182 183

    return string;
}

184
static HRESULT navigate_url(HHInfo *info, LPCWSTR surl)
185 186 187 188
{
    VARIANT url;
    HRESULT hres;

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

191 192 193 194 195 196 197
    V_VT(&url) = VT_BSTR;
    V_BSTR(&url) = SysAllocString(surl);

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

    VariantClear(&url);

198
    if(FAILED(hres))
199
        TRACE("Navigation failed: %08x\n", hres);
200 201 202 203 204 205 206 207 208 209

    return hres;
}

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

210
    static const WCHAR url_indicator[] = {':', '/', '/', 0};
211 212 213 214 215 216 217 218

    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 */
219 220 221 222

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

223 224
    heap_free(chm_path.chm_file);
    heap_free(chm_path.chm_index);
225 226

    return ret;
227 228
}

229
static BOOL AppendFullPathURL(LPCWSTR file, LPWSTR buf, LPCWSTR index)
230 231
{
    static const WCHAR url_format[] =
232 233 234
        {'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};
235
    WCHAR full_path[MAX_PATH];
236

237
    TRACE("%s %p %s\n", debugstr_w(file), buf, debugstr_w(index));
238

239
    if(!GetFullPathNameW(file, sizeof(full_path)/sizeof(full_path[0]), full_path, NULL)) {
240 241 242 243
        WARN("GetFullPathName failed: %u\n", GetLastError());
        return FALSE;
    }

244
    wsprintfW(buf, url_format, full_path, (!index || index[0] == '/') ? empty : slash, index);
245 246 247 248 249 250 251 252 253 254 255 256
    return TRUE;
}

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

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

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

258 259 260
    /* FIXME: HACK */
    if((ptr = strchrW(buf, '#')))
       *ptr = 0;
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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
static void DoSync(HHInfo *info)
{
    WCHAR buf[INTERNET_MAX_URL_LENGTH];
    HRESULT hres;
    DWORD len;
    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 */
    if ((!AppendFullPathURL(info->pszFile, buf, NULL)) || (len = lstrlenW(buf) > lstrlenW(url)))
    {
        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
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 337
/* 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);
}

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

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

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

    /* update the window sizes */
352
    pHHInfo->WinType.iNavWidth += pt.x;
353 354 355 356 357 358 359 360 361 362 363 364
    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;
}

365
static LRESULT CALLBACK SizeBar_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
James Hawkins's avatar
James Hawkins committed
366 367 368
{
    switch (message)
    {
369 370 371 372 373 374 375 376 377
        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
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
        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;
394
    wcex.lpfnWndProc    = SizeBar_WndProc;
James Hawkins's avatar
James Hawkins committed
395
    wcex.cbClsExtra     = 0;
396
    wcex.cbWndExtra     = sizeof(LONG_PTR);
397
    wcex.hInstance      = hhctrl_hinstance;
James Hawkins's avatar
James Hawkins committed
398 399 400 401 402 403 404 405 406 407
    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);
}

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

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

    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;
425
    HWND hwndParent = pHHInfo->WinType.hwndHelp;
426
    DWORD dwStyles = WS_CHILDWINDOW | WS_OVERLAPPED;
James Hawkins's avatar
James Hawkins committed
427 428 429
    DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
    RECT rc;

430 431 432
    if (!pHHInfo->WinType.fNotExpanded)
        dwStyles |= WS_VISIBLE;

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

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

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

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

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

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

454
static LRESULT Child_OnPaint(HWND hWnd)
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
{
    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));

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

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

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

    return 0;
}

486
static void ResizeTabChild(HHInfo *info, int tab)
487
{
488 489
    HWND hwnd = info->tabs[tab].hwnd;
    INT width, height;
490 491 492 493 494 495 496 497 498 499 500
    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;
501 502
    width = rect.right-rect.left;
    height = rect.bottom-rect.top;
503

504 505 506 507 508 509 510 511 512 513
    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);

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

        break;
    }
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
    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;
    }
541
    }
542 543
}

544 545
static LRESULT Child_OnSize(HWND hwnd)
{
546
    HHInfo *info = (HHInfo*)GetWindowLongPtrW(hwnd, 0);
547 548 549 550 551 552 553 554 555
    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);
556

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

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

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

    if (!info)
        return 0;

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

576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
    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;
593 594 595 596

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

597
    return 0;
598 599
}

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

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

610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
    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;
629
        }
630
        if(iiter->nItems > 1) {
631 632 633 634 635 636 637 638
            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;

639 640
                if(!item->name)
                    item->name = GetDocumentTitle(info->pCHMInfo, item->local);
641 642 643 644 645 646 647 648 649 650 651
                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);
652 653 654 655 656 657
            return 0;
        }
        name = iiter->items[0].name;
        local = iiter->items[0].local;
        chmfile = iiter->merge.chm_file;
        break;
658 659 660 661 662 663
    case TAB_SEARCH:
        siter = (SearchItem *) user_data;
        name = siter->filename;
        local = siter->filename;
        chmfile = info->pCHMInfo->szFile;
        break;
664 665 666
    default:
        FIXME("Unhandled operation for this tab!\n");
        return 0;
667 668
    }

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

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

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

681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
/* 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);
}

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

709 710 711
        switch(nmhdr->code) {
        case TCN_SELCHANGE:
            return OnTabChange(hWnd);
712
        case TVN_SELCHANGEDW:
713
            return OnTopicChange(info, (void*)((NMTREEVIEWW *)lParam)->itemNew.lParam);
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
        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;
        }
732
        case NM_DBLCLK:
733 734 735 736 737
            if(!info)
                return 0;
            switch(info->current_tab)
            {
            case TAB_INDEX:
738
                return OnTopicChange(info, (void*)((NMITEMACTIVATE *)lParam)->lParam);
739 740 741 742
            case TAB_SEARCH:
                return OnTopicChange(info, (void*)((NMITEMACTIVATE *)lParam)->lParam);
            }
            break;
743
        case NM_RETURN:
744 745 746 747
            if(!info)
                return 0;
            switch(info->current_tab) {
            case TAB_INDEX: {
748 749 750 751 752
                HWND hwndList = info->tabs[TAB_INDEX].hwnd;
                LVITEMW lvItem;

                lvItem.iItem = (int) SendMessageW(hwndList, LVM_GETSELECTIONMARK, 0, 0);
                lvItem.mask = TVIF_PARAM;
753
                SendMessageW(hwndList, LVM_GETITEMW, 0, (LPARAM)&lvItem);
754
                OnTopicChange(info, (void*) lvItem.lParam);
755
                return 0;
756
            }
757
            case TAB_SEARCH: {
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
                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;
781
                    SendMessageW(hwndList, LVM_GETITEMW, 0, (LPARAM)&lvItem);
782 783 784 785
                    OnTopicChange(info, (void*) lvItem.lParam);
                    return 0;
                }
                break;
786 787 788
            }
            }
            break;
789 790 791
        }
        break;
    }
792 793
    default:
        return DefWindowProcW(hWnd, message, wParam, lParam);
794 795 796 797 798 799 800 801 802 803 804
    }

    return 0;
}

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

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

    RegisterClassExW(&wcex);
}

819 820
/* Toolbar */

821 822
#define ICON_SIZE   20

823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
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);
841 842 843
    item.fMask = MIIM_FTYPE | MIIM_STATE | MIIM_STRING;
    item.fType = MFT_STRING;
    item.fState = MF_ENABLED;
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 870

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

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

    switch (dwID)
    {
        case IDTB_STOP:
878
            DoPageAction(info, WB_STOP);
879 880
            break;
        case IDTB_REFRESH:
881
            DoPageAction(info, WB_REFRESH);
882 883
            break;
        case IDTB_BACK:
884
            DoPageAction(info, WB_GOBACK);
885 886
            break;
        case IDTB_HOME:
887
            NavigateToChm(info, info->pCHMInfo->szFile, info->WinType.pszHome);
888 889
            break;
        case IDTB_FORWARD:
890
            DoPageAction(info, WB_GOFORWARD);
891
            break;
892 893 894
        case IDTB_PRINT:
            DoPageAction(info, WB_PRINT);
            break;
895 896
        case IDTB_EXPAND:
        case IDTB_CONTRACT:
897 898
            ExpandContract(info);
            break;
899
        case IDTB_SYNC:
900 901
            DoSync(info);
            break;
902
        case IDTB_OPTIONS:
903 904
            DisplayPopupMenu(info);
            break;
905 906 907 908 909 910 911 912
        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;
913 914 915 916 917 918 919 920 921 922 923 924
        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;
    }
}

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

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

942
    /* Common bitmaps */
943 944 945 946 947
    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);
948 949 950
    /* hhctrl.ocx bitmaps */
    tbAB.hInst = hhctrl_hinstance;
    tbAB.nID = IDB_HHTOOLBAR;
951
    nHHBitmaps = SendMessageW(hToolbar, TB_ADDBITMAP, HHTB_NUMBITMAPS, (LPARAM)&tbAB);
952

953 954
    *pdwNumButtons = 0;

955 956 957 958 959 960 961 962
    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);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1028
    TB_AddButtonsFromFlags(pHHInfo, buttons, toolbarFlags, &dwNumButtons);
1029 1030 1031 1032 1033

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

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

1040
    SendMessageW(hToolbar, TB_ADDBUTTONSW, dwNumButtons, (LPARAM)buttons);
1041 1042 1043
    SendMessageW(hToolbar, TB_AUTOSIZE, 0, 0);
    ShowWindow(hToolbar, SW_SHOW);

1044 1045 1046 1047 1048
    return TRUE;
}

/* Navigation Pane */

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

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

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

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

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

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

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

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

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

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

1094 1095 1096
    if (!info->WinType.fNotExpanded)
        dwStyles |= WS_VISIBLE;

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

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

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

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

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

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

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

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

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

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

/* HTML Pane */

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

1140 1141 1142
    GetClientRect(info->WinType.hwndHelp, &rectWND);
    GetClientRect(info->WinType.hwndToolBar, &rectTB);
    GetClientRect(info->hwndSizeBar, &rectSB);
1143

1144 1145 1146 1147 1148 1149 1150 1151
    if (info->WinType.fNotExpanded)
        rc->left = 0;
    else
    {
        GetClientRect(info->WinType.hwndNavigation, &rectNP);
        rc->left = rectNP.right + rectSB.right;
    }

1152
    rc->top = rectTB.bottom;
James Hawkins's avatar
James Hawkins committed
1153
    rc->right = rectWND.right - rc->left;
1154 1155 1156
    rc->bottom = rectWND.bottom - rectTB.bottom;
}

1157 1158
static BOOL HH_AddHTMLPane(HHInfo *pHHInfo)
{
1159
    HWND hWnd;
1160
    HWND hwndParent = pHHInfo->WinType.hwndHelp;
1161 1162 1163 1164 1165 1166 1167 1168
    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,
1169
                           hwndParent, NULL, hhctrl_hinstance, NULL);
1170 1171 1172
    if (!hWnd)
        return FALSE;

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

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

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

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

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

1192 1193
    if(info->tabs[TAB_CONTENTS].id == -1)
        return TRUE; /* No "Contents" tab */
1194 1195 1196 1197
    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) {
1198 1199 1200 1201
        ERR("Could not create treeview control\n");
        return FALSE;
    }

1202 1203 1204 1205 1206 1207 1208
    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;
1209
    ResizeTabChild(info, TAB_CONTENTS);
1210
    ShowWindow(hWnd, SW_SHOW);
1211 1212 1213 1214

    return TRUE;
}

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

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

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

    ResizeTabChild(info, TAB_SEARCH);

    return TRUE;
}

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
/* 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)
{
1340
    HHInfo *info = (HHInfo *)GetWindowLongPtrW(hWnd, 0);
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366

    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;
1367 1368 1369
        switch(nmhdr->code)
        {
        case NM_DBLCLK: {
1370
            HHInfo *info = (HHInfo*)GetWindowLongPtrW(hWnd, 0);
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
            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;
        }
1382
        case NM_RETURN: {
1383
            HHInfo *info = (HHInfo*)GetWindowLongPtrW(hWnd, 0);
1384 1385 1386 1387 1388 1389 1390 1391
            IndexSubItem *iter;
            LVITEMW lvItem;

            if(info == 0)
                return 0;

            lvItem.iItem = (int) SendMessageW(info->popup.hwndList, LVM_GETSELECTIONMARK, 0, 0);
            lvItem.mask = TVIF_PARAM;
1392
            SendMessageW(info->popup.hwndList, LVM_GETITEMW, 0, (LPARAM)&lvItem);
1393 1394 1395 1396 1397 1398
            iter = (IndexSubItem*) lvItem.lParam;
            NavigateToChm(info, info->index->merge.chm_file, iter->local);
            ShowWindow(info->popup.hwndPopup, SW_HIDE);
            return 0;
        }
        }
1399 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
        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;
1425
    wcex.cbWndExtra     = sizeof(LONG_PTR);
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438
    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;
1439
    wcex.cbWndExtra     = sizeof(LONG_PTR);
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
    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;
1487 1488
    SetWindowLongPtrW(hwndPopup, 0, (LONG_PTR)info);
    SetWindowLongPtrW(hwndCallback, 0, (LONG_PTR)info);
1489 1490 1491 1492 1493 1494 1495

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

    return TRUE;
}

1496 1497
/* Viewer Window */

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

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

    if (!pHHInfo)
1536
        return 0;
1537

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

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

    }
1549 1550

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

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

    return 0;
1559 1560
}

1561
static LRESULT CALLBACK Help_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1562 1563 1564
{
    switch (message)
    {
1565 1566 1567 1568 1569 1570
    case WM_COMMAND:
        if (HIWORD(wParam) == BN_CLICKED)
            TB_OnClick(hWnd, LOWORD(wParam));
        break;
    case WM_SIZE:
        return Help_OnSize(hWnd);
1571
    case WM_CLOSE:
1572
        ReleaseHelpViewer((HHInfo *)GetWindowLongPtrW(hWnd, 0));
1573
        return 0;
1574
    case WM_DESTROY:
1575 1576
        if(hh_process)
            PostQuitMessage(0);
1577 1578 1579 1580
        break;

    default:
        return DefWindowProcW(hWnd, message, wParam, lParam);
1581 1582 1583 1584 1585
    }

    return 0;
}

1586
static BOOL HH_CreateHelpWindow(HHInfo *info)
1587
{
1588
    HWND hWnd, parent = 0;
1589
    RECT winPos = info->WinType.rcWindowPos;
1590 1591
    WNDCLASSEXW wcex;
    DWORD dwStyles, dwExStyles;
1592 1593
    DWORD x, y, width = 0, height = 0;
    LPCWSTR caption;
1594 1595 1596 1597 1598 1599 1600

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

    wcex.cbSize         = sizeof(WNDCLASSEXW);
    wcex.style          = CS_HREDRAW | CS_VREDRAW;
1601
    wcex.lpfnWndProc    = Help_WndProc;
1602
    wcex.cbClsExtra     = 0;
1603
    wcex.cbWndExtra     = sizeof(LONG_PTR);
1604
    wcex.hInstance      = hhctrl_hinstance;
1605 1606
    wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
    wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
1607
    wcex.hbrBackground  = (HBRUSH)(COLOR_MENU + 1);
1608 1609 1610 1611 1612 1613
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = windowClassW;
    wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);

    RegisterClassExW(&wcex);

1614
    /* Read in window parameters if available */
1615
    if (info->WinType.fsValidMembers & HHWIN_PARAM_STYLES)
1616 1617 1618 1619 1620
    {
        dwStyles = info->WinType.dwStyles;
        if (!(info->WinType.dwStyles & WS_CHILD))
            dwStyles |= WS_OVERLAPPEDWINDOW;
    }
1621 1622 1623 1624
    else
        dwStyles = WS_OVERLAPPEDWINDOW | WS_VISIBLE |
                   WS_CLIPSIBLINGS | WS_CLIPCHILDREN;

1625 1626
    if (info->WinType.fsValidMembers & HHWIN_PARAM_EXSTYLES)
        dwExStyles = info->WinType.dwExStyles;
1627 1628 1629
    else
        dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_APPWINDOW |
                     WS_EX_WINDOWEDGE | WS_EX_RIGHTSCROLLBAR;
1630

1631
    if (info->WinType.fsValidMembers & HHWIN_PARAM_RECT)
1632 1633 1634 1635 1636 1637
    {
        x = winPos.left;
        y = winPos.top;
        width = winPos.right - x;
        height = winPos.bottom - y;
    }
1638
    if (!width || !height)
1639 1640 1641 1642 1643 1644
    {
        x = WINTYPE_DEFAULT_X;
        y = WINTYPE_DEFAULT_Y;
        width = WINTYPE_DEFAULT_WIDTH;
        height = WINTYPE_DEFAULT_HEIGHT;
    }
1645

1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658
    if (info->WinType.fNotExpanded)
    {
        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;
    }


1659 1660 1661
    caption = info->WinType.pszCaption;
    if (!*caption) caption = info->pCHMInfo->defTitle;

1662 1663 1664
    if (info->WinType.dwStyles & WS_CHILD)
        parent = info->WinType.hwndCaller;

1665
    hWnd = CreateWindowExW(dwExStyles, windowClassW, caption,
1666
                           dwStyles, x, y, width, height, parent, NULL, hhctrl_hinstance, NULL);
1667 1668 1669 1670 1671 1672 1673
    if (!hWnd)
        return FALSE;

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

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

1676
    info->WinType.hwndHelp = hWnd;
1677 1678 1679 1680 1681 1682 1683
    return TRUE;
}

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

1684
    GetObjectW(GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONTW), &lf);
1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
    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 */
1702
static BOOL CreateViewer(HHInfo *pHHInfo)
1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713
{
    HH_CreateFont(pHHInfo);

    if (!HH_CreateHelpWindow(pHHInfo))
        return FALSE;

    HH_InitRequiredControls(ICC_BAR_CLASSES);

    if (!HH_AddToolbar(pHHInfo))
        return FALSE;

1714 1715
    HH_RegisterChildWndClass(pHHInfo);

1716 1717 1718
    if (!HH_AddNavigationPane(pHHInfo))
        return FALSE;

James Hawkins's avatar
James Hawkins committed
1719 1720 1721 1722 1723
    HH_RegisterSizeBarClass(pHHInfo);

    if (!HH_AddSizeBar(pHHInfo))
        return FALSE;

1724 1725 1726
    if (!HH_AddHTMLPane(pHHInfo))
        return FALSE;

1727 1728 1729
    if (!AddContentTab(pHHInfo))
        return FALSE;

1730 1731 1732
    if (!AddIndexTab(pHHInfo))
        return FALSE;

1733 1734 1735
    if (!AddIndexPopup(pHHInfo))
        return FALSE;

1736 1737 1738
    if (!AddSearchTab(pHHInfo))
        return FALSE;

1739
    InitContent(pHHInfo);
1740
    InitIndex(pHHInfo);
1741

1742 1743 1744
    return TRUE;
}

1745
void ReleaseHelpViewer(HHInfo *info)
1746
{
1747 1748
    TRACE("(%p)\n", info);

1749
    if (!info)
1750 1751
        return;

1752 1753
    list_remove(&info->entry);

1754
    /* Free allocated strings */
1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
    heap_free(info->pszType);
    heap_free(info->pszCaption);
    heap_free(info->pszToc);
    heap_free(info->pszIndex);
    heap_free(info->pszFile);
    heap_free(info->pszHome);
    heap_free(info->pszJump1);
    heap_free(info->pszJump2);
    heap_free(info->pszUrlJump1);
    heap_free(info->pszUrlJump2);
1765 1766 1767 1768

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

1769
    ReleaseWebBrowser(info);
1770
    ReleaseContent(info);
1771
    ReleaseIndex(info);
1772
    ReleaseSearch(info);
1773

1774 1775
    if(info->contents.hImageList)
        ImageList_Destroy(info->contents.hImageList);
1776 1777 1778
    if(info->WinType.hwndHelp)
        DestroyWindow(info->WinType.hwndHelp);

1779
    heap_free(info);
1780
    OleUninitialize();
1781 1782
}

1783
HHInfo *CreateHelpViewer(LPCWSTR filename, HWND caller)
1784
{
1785
    HHInfo *info = heap_alloc_zero(sizeof(HHInfo));
1786 1787 1788 1789 1790 1791 1792
    int i;

    /* 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;
1793 1794 1795 1796 1797 1798 1799 1800

    OleInitialize(NULL);

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

1802
    if (!LoadWinTypeFromCHM(info)) {
1803
        ReleaseHelpViewer(info);
1804 1805
        return NULL;
    }
1806
    info->WinType.hwndCaller = caller;
1807

1808 1809
    if(!CreateViewer(info)) {
        ReleaseHelpViewer(info);
1810 1811 1812
        return NULL;
    }

1813
    list_add_tail(&window_list, &info->entry);
1814
    return info;
1815
}
1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841

/*
 * 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.
 */
1842
WCHAR *decode_html(const char *html_fragment, int html_fragment_len, UINT code_page)
1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 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 1896 1897 1898
{
    const char *h = html_fragment;
    char *amp, *sem, symbol, *tmp;
    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 */

1899
    len = MultiByteToWideChar(code_page, 0, tmp, tmp_len, NULL, 0);
1900
    unicode_text = heap_alloc(len*sizeof(WCHAR));
1901
    MultiByteToWideChar(code_page, 0, tmp, tmp_len, unicode_text, len);
1902 1903 1904
    heap_free(tmp);
    return unicode_text;
}