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

#include "wine/debug.h"
23

24 25 26 27 28 29 30 31 32 33 34 35
#include <stdarg.h>

#define COBJMACROS

#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "winnls.h"
#include "htmlhelp.h"
#include "ole2.h"
#include "rpcproxy.h"

36 37
#define INIT_GUID
#include "hhctrl.h"
38 39 40

WINE_DEFAULT_DEBUG_CHANNEL(htmlhelp);

41
HINSTANCE hhctrl_hinstance;
42
BOOL hh_process = FALSE;
43

44

45 46 47 48 49 50 51 52 53 54 55 56 57 58
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD fdwReason, LPVOID lpvReserved)
{
    TRACE("(%p,%d,%p)\n", hInstance, fdwReason, lpvReserved);

    switch (fdwReason)
    {
    case DLL_PROCESS_ATTACH:
        hhctrl_hinstance = hInstance;
        DisableThreadLibraryCalls(hInstance);
        break;
    }
    return TRUE;
}

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
static const char *command_to_string(UINT command)
{
#define X(x) case x: return #x
    switch (command)
    {
        X( HH_DISPLAY_TOPIC );
        X( HH_DISPLAY_TOC );
        X( HH_DISPLAY_INDEX );
        X( HH_DISPLAY_SEARCH );
        X( HH_SET_WIN_TYPE );
        X( HH_GET_WIN_TYPE );
        X( HH_GET_WIN_HANDLE );
        X( HH_ENUM_INFO_TYPE );
        X( HH_SET_INFO_TYPE );
        X( HH_SYNC );
        X( HH_RESERVED1 );
        X( HH_RESERVED2 );
        X( HH_RESERVED3 );
        X( HH_KEYWORD_LOOKUP );
        X( HH_DISPLAY_TEXT_POPUP );
        X( HH_HELP_CONTEXT );
        X( HH_TP_HELP_CONTEXTMENU );
        X( HH_TP_HELP_WM_HELP );
        X( HH_CLOSE_ALL );
        X( HH_ALINK_LOOKUP );
        X( HH_GET_LAST_ERROR );
        X( HH_ENUM_CATEGORY );
        X( HH_ENUM_CATEGORY_IT );
        X( HH_RESET_IT_FILTER );
        X( HH_SET_INCLUSIVE_FILTER );
        X( HH_SET_EXCLUSIVE_FILTER );
        X( HH_INITIALIZE );
        X( HH_UNINITIALIZE );
92
        X( HH_SAFE_DISPLAY_TOPIC );
93 94 95 96 97 98 99
        X( HH_PRETRANSLATEMESSAGE );
        X( HH_SET_GLOBAL_PROPERTY );
    default: return "???";
    }
#undef X
}

100
static BOOL resolve_filename(const WCHAR *env_filename, WCHAR *fullname, DWORD buflen, WCHAR **index, WCHAR **window)
101 102
{
    static const WCHAR helpW[] = {'\\','h','e','l','p','\\',0};
103 104 105
    static const WCHAR delimW[] = {':',':',0};
    static const WCHAR delim2W[] = {'>',0};

106
    DWORD env_len;
107
    WCHAR *filename, *extra;
108 109

    env_filename = skip_schema(env_filename);
110 111 112 113 114

    /* the format is "helpFile[::/index][>window]" */
    if (index) *index = NULL;
    if (window) *window = NULL;

115 116 117 118 119 120 121 122 123 124
    env_len = ExpandEnvironmentStringsW(env_filename, NULL, 0);
    if (!env_len)
        return 0;

    filename = heap_alloc(env_len * sizeof(WCHAR));
    if (filename == NULL)
        return 0;

    ExpandEnvironmentStringsW(env_filename, filename, env_len);

125
    extra = wcsstr(filename, delim2W);
126 127
    if (extra)
    {
128
        *extra = 0;
129 130 131 132
        if (window)
            *window = strdupW(extra+1);
    }

133
    extra = wcsstr(filename, delimW);
134 135
    if (extra)
    {
136
        *extra = 0;
137 138 139
        if (index)
            *index = strdupW(extra+2);
    }
140 141 142 143 144

    GetFullPathNameW(filename, buflen, fullname, NULL);
    if (GetFileAttributesW(fullname) == INVALID_FILE_ATTRIBUTES)
    {
        GetWindowsDirectoryW(fullname, buflen);
145 146
        lstrcatW(fullname, helpW);
        lstrcatW(fullname, filename);
147
    }
148 149 150

    heap_free(filename);

151 152 153
    return (GetFileAttributesW(fullname) != INVALID_FILE_ATTRIBUTES);
}

154
/******************************************************************
155
 *		HtmlHelpW (HHCTRL.OCX.15)
156
 */
157
HWND WINAPI HtmlHelpW(HWND caller, LPCWSTR filename, UINT command, DWORD_PTR data)
158
{
159 160
    WCHAR fullname[MAX_PATH];

161
    TRACE("(%p, %s, command=%s, data=%lx)\n",
162 163
          caller, debugstr_w( filename ),
          command_to_string( command ), data);
164

165
    switch (command)
166
    {
167 168
    case HH_DISPLAY_TOPIC:
    case HH_DISPLAY_TOC:
169
    case HH_DISPLAY_INDEX:
170
    case HH_DISPLAY_SEARCH:{
171
        BOOL res;
172
        NMHDR nmhdr;
173
        HHInfo *info = NULL;
174
        WCHAR *window = NULL;
175
        const WCHAR *index = NULL;
176
        WCHAR *default_index = NULL;
177
        int tab_index = TAB_CONTENTS;
178

179 180 181
        if (!filename)
            return NULL;

182
        if (!resolve_filename(filename, fullname, MAX_PATH, &default_index, &window))
183 184 185 186
        {
            WARN("can't find %s\n", debugstr_w(filename));
            return 0;
        }
187
        index = default_index;
188

189 190 191 192
        if (window)
            info = find_window(window);

        info = CreateHelpViewer(info, fullname, caller);
193
        if(!info)
194 195 196
        {
            heap_free(default_index);
            heap_free(window);
197
            return NULL;
198
        }
199 200 201

        if(!index)
            index = info->WinType.pszFile;
202 203
        if(!info->WinType.pszType)
            info->WinType.pszType = info->stringsW.pszType = window;
204 205
        else
            heap_free(window);
206

207 208 209 210 211 212
        /* called to load a specified topic */
        switch(command)
        {
        case HH_DISPLAY_TOPIC:
        case HH_DISPLAY_TOC:
            if (data)
213 214 215 216
            {
                static const WCHAR delimW[] = {':',':',0};
                const WCHAR *i = (const WCHAR *)data;

217
                index = wcsstr(i, delimW);
218 219 220 221
                if(index)
                {
                    if(memcmp(info->pCHMInfo->szFile, i, index-i))
                        FIXME("Opening a CHM file in the context of another is not supported.\n");
222
                    index += lstrlenW(delimW);
223 224 225 226
                }
                else
                    index = i;
            }
227 228 229
            break;
        }

230
        res = NavigateToChm(info, info->pCHMInfo->szFile, index);
231
        heap_free(default_index);
232

233
        if(!res)
234
        {
235 236
            ReleaseHelpViewer(info);
            return NULL;
237
        }
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261

        switch(command)
        {
        case HH_DISPLAY_TOPIC:
        case HH_DISPLAY_TOC:
            tab_index = TAB_CONTENTS;
            break;
        case HH_DISPLAY_INDEX:
            tab_index = TAB_INDEX;
            if (data)
                FIXME("Should select keyword '%s'.\n", debugstr_w((WCHAR *)data));
            break;
        case HH_DISPLAY_SEARCH:
            tab_index = TAB_SEARCH;
            if (data)
                FIXME("Should display search specified by HH_FTS_QUERY structure.\n");
            break;
        }
        /* open the requested tab */
        memset(&nmhdr, 0, sizeof(nmhdr));
        nmhdr.code = TCN_SELCHANGE;
        SendMessageW(info->hwndTabCtrl, TCM_SETCURSEL, (WPARAM)info->tabs[tab_index].id, 0);
        SendMessageW(info->WinType.hwndNavigation, WM_NOTIFY, 0, (LPARAM)&nmhdr);

262
        return info->WinType.hwndHelp;
263
    }
264
    case HH_HELP_CONTEXT: {
265 266
        WCHAR *window = NULL;
        HHInfo *info = NULL;
267 268
        LPWSTR url;

269 270 271
        if (!filename)
            return NULL;

272
        if (!resolve_filename(filename, fullname, MAX_PATH, NULL, &window))
273 274 275 276 277
        {
            WARN("can't find %s\n", debugstr_w(filename));
            return 0;
        }

278 279 280 281
        if (window)
            info = find_window(window);

        info = CreateHelpViewer(info, fullname, caller);
282
        if(!info)
283 284
        {
            heap_free(window);
285
            return NULL;
286 287 288 289 290 291
        }

        if(!info->WinType.pszType)
            info->WinType.pszType = info->stringsW.pszType = window;
        else
            heap_free(window);
292 293 294

        url = FindContextAlias(info->pCHMInfo, data);
        if(!url)
295
        {
296 297
            if(!data) /* there may legitimately be no context alias for id 0 */
                return info->WinType.hwndHelp;
298
            ReleaseHelpViewer(info);
299
            return NULL;
300
        }
301 302

        NavigateToUrl(info, url);
303
        heap_free(url);
304
        return info->WinType.hwndHelp;
305
    }
306 307 308 309 310 311 312 313 314 315
    case HH_PRETRANSLATEMESSAGE: {
        static BOOL warned = FALSE;

        if (!warned)
        {
            FIXME("HH_PRETRANSLATEMESSAGE unimplemented\n");
            warned = TRUE;
        }
        return 0;
    }
316 317 318 319 320 321 322 323 324 325
    case HH_CLOSE_ALL: {
        HHInfo *info, *next;

        LIST_FOR_EACH_ENTRY_SAFE(info, next, &window_list, HHInfo, entry)
        {
            TRACE("Destroying window %s.\n", debugstr_w(info->WinType.pszType));
            ReleaseHelpViewer(info);
        }
        return 0;
    }
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
    case HH_SET_WIN_TYPE: {
        HH_WINTYPEW *wintype = (HH_WINTYPEW *)data;
        WCHAR *window = NULL;
        HHInfo *info = NULL;

        if (!filename && wintype->pszType)
            window = strdupW(wintype->pszType);
        else if (!filename || !resolve_filename(filename, fullname, MAX_PATH, NULL, &window) || !window)
        {
            WARN("can't find window name: %s\n", debugstr_w(filename));
            return 0;
        }
        info = find_window(window);
        if (!info)
        {
            info = heap_alloc_zero(sizeof(HHInfo));
            info->WinType.pszType = info->stringsW.pszType = window;
            list_add_tail(&window_list, &info->entry);
        }
        else
            heap_free(window);

        TRACE("Changing WINTYPE, fsValidMembers=0x%x\n", wintype->fsValidMembers);

350 351
        MergeChmProperties(wintype, info, TRUE);
        UpdateHelpWindow(info);
352 353
        return 0;
    }
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
    case HH_GET_WIN_TYPE: {
        HH_WINTYPEW *wintype = (HH_WINTYPEW *)data;
        WCHAR *window = NULL;
        HHInfo *info = NULL;

        if (!filename || !resolve_filename(filename, fullname, MAX_PATH, NULL, &window) || !window)
        {
            WARN("can't find window name: %s\n", debugstr_w(filename));
            return 0;
        }
        info = find_window(window);
        if (!info)
        {
            WARN("Could not find window named %s.\n", debugstr_w(window));
            heap_free(window);
            return (HWND)~0;
        }

        TRACE("Retrieving WINTYPE for %s.\n", debugstr_w(window));
        *wintype = info->WinType;
        heap_free(window);
        return 0;
    }
377 378 379 380
    default:
        FIXME("HH case %s not handled.\n", command_to_string( command ));
    }

James Hawkins's avatar
James Hawkins committed
381
    return 0;
382 383
}

384
static void wintypeAtoW(const HH_WINTYPEA *data, HH_WINTYPEW *wdata, struct wintype_stringsW *stringsW)
385 386 387 388 389 390 391 392 393 394 395 396 397 398
{
    memcpy(wdata, data, sizeof(*data));
    /* convert all of the ANSI strings to Unicode */
    wdata->pszType       = stringsW->pszType       = strdupAtoW(data->pszType);
    wdata->pszCaption    = stringsW->pszCaption    = strdupAtoW(data->pszCaption);
    wdata->pszToc        = stringsW->pszToc        = strdupAtoW(data->pszToc);
    wdata->pszIndex      = stringsW->pszIndex      = strdupAtoW(data->pszIndex);
    wdata->pszFile       = stringsW->pszFile       = strdupAtoW(data->pszFile);
    wdata->pszHome       = stringsW->pszHome       = strdupAtoW(data->pszHome);
    wdata->pszJump1      = stringsW->pszJump1      = strdupAtoW(data->pszJump1);
    wdata->pszJump2      = stringsW->pszJump2      = strdupAtoW(data->pszJump2);
    wdata->pszUrlJump1   = stringsW->pszUrlJump1   = strdupAtoW(data->pszUrlJump1);
    wdata->pszUrlJump2   = stringsW->pszUrlJump2   = strdupAtoW(data->pszUrlJump2);
    wdata->pszCustomTabs = stringsW->pszCustomTabs = strdupAtoW(data->pszCustomTabs);
399
}
400

401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
static void wintypeWtoA(const HH_WINTYPEW *wdata, HH_WINTYPEA *data, struct wintype_stringsA *stringsA)
{
    memcpy(data, wdata, sizeof(*wdata));
    /* convert all of the Unicode strings to ANSI */
    data->pszType       = stringsA->pszType       = strdupWtoA(wdata->pszType);
    data->pszCaption    = stringsA->pszCaption    = strdupWtoA(wdata->pszCaption);
    data->pszToc        = stringsA->pszToc        = strdupWtoA(wdata->pszToc);
    data->pszIndex      = stringsA->pszFile       = strdupWtoA(wdata->pszIndex);
    data->pszFile       = stringsA->pszFile       = strdupWtoA(wdata->pszFile);
    data->pszHome       = stringsA->pszHome       = strdupWtoA(wdata->pszHome);
    data->pszJump1      = stringsA->pszJump1      = strdupWtoA(wdata->pszJump1);
    data->pszJump2      = stringsA->pszJump2      = strdupWtoA(wdata->pszJump2);
    data->pszUrlJump1   = stringsA->pszUrlJump1   = strdupWtoA(wdata->pszUrlJump1);
    data->pszUrlJump2   = stringsA->pszUrlJump2   = strdupWtoA(wdata->pszUrlJump2);
    data->pszCustomTabs = stringsA->pszCustomTabs = strdupWtoA(wdata->pszCustomTabs);
416 417
}

418
/******************************************************************
419
 *		HtmlHelpA (HHCTRL.OCX.14)
420
 */
421
HWND WINAPI HtmlHelpA(HWND caller, LPCSTR filename, UINT command, DWORD_PTR data)
422
{
423 424
    WCHAR *wfile = strdupAtoW( filename );
    HWND result = 0;
425

426 427 428 429 430 431 432 433 434 435 436 437 438
    if (data)
    {
        switch(command)
        {
        case HH_ALINK_LOOKUP:
        case HH_DISPLAY_SEARCH:
        case HH_DISPLAY_TEXT_POPUP:
        case HH_GET_LAST_ERROR:
        case HH_KEYWORD_LOOKUP:
        case HH_SYNC:
            FIXME("structures not handled yet\n");
            break;

439 440 441
        case HH_SET_WIN_TYPE:
        {
            struct wintype_stringsW stringsW;
442 443 444 445
            HH_WINTYPEW wdata;

            wintypeAtoW((HH_WINTYPEA *)data, &wdata, &stringsW);
            result = HtmlHelpW( caller, wfile, command, (DWORD_PTR)&wdata );
446
            wintype_stringsW_free(&stringsW);
447 448 449 450 451 452 453 454 455 456 457 458 459
            goto done;
        }
        case HH_GET_WIN_TYPE:
        {
            HH_WINTYPEW wdata;
            HHInfo *info;

            result = HtmlHelpW( caller, wfile, command, (DWORD_PTR)&wdata );
            if (!wdata.pszType) break;
            info = find_window(wdata.pszType);
            if (!info) break;
            wintype_stringsA_free(&info->stringsA);
            wintypeWtoA(&wdata, (HH_WINTYPEA *)data, &info->stringsA);
460 461 462
            goto done;
        }

463 464 465 466 467
        case HH_DISPLAY_INDEX:
        case HH_DISPLAY_TOPIC:
        case HH_DISPLAY_TOC:
        case HH_GET_WIN_HANDLE:
        case HH_SAFE_DISPLAY_TOPIC:
468 469 470 471 472 473
        {
            WCHAR *wdata = strdupAtoW( (const char *)data );
            result = HtmlHelpW( caller, wfile, command, (DWORD_PTR)wdata );
            heap_free(wdata);
            goto done;
        }
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490

        case HH_CLOSE_ALL:
        case HH_HELP_CONTEXT:
        case HH_INITIALIZE:
        case HH_PRETRANSLATEMESSAGE:
        case HH_TP_HELP_CONTEXTMENU:
        case HH_TP_HELP_WM_HELP:
        case HH_UNINITIALIZE:
            /* either scalar or pointer to scalar - do nothing */
            break;

        default:
            FIXME("Unknown command: %s (%d)\n", command_to_string(command), command);
            break;
        }
    }

491 492
    result = HtmlHelpW( caller, wfile, command, data );
done:
493
    heap_free(wfile);
494
    return result;
495
}
496 497

/******************************************************************
498
 *		doWinMain (HHCTRL.OCX.13)
499 500 501 502
 */
int WINAPI doWinMain(HINSTANCE hInstance, LPSTR szCmdLine)
{
    MSG msg;
503
    int len, buflen, mapid = -1;
504 505
    WCHAR *filename;
    char *endq = NULL;
506
    HWND hwnd;
507

508 509
    hh_process = TRUE;

510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
    /* Parse command line option of the HTML Help command.
     *
     * Note: The only currently handled action is "mapid",
     *  which corresponds to opening a specific page.
     */
    while(*szCmdLine == '-')
    {
        LPSTR space, ptr;

        ptr = szCmdLine + 1;
        space = strchr(ptr, ' ');
        if(!strncmp(ptr, "mapid", space-ptr))
        {
            char idtxt[10];

            ptr += strlen("mapid")+1;
            space = strchr(ptr, ' ');
527 528 529
            /* command line ends without number */
            if (!space)
                return 0;
530 531 532 533 534 535 536
            memcpy(idtxt, ptr, space-ptr);
            idtxt[space-ptr] = '\0';
            mapid = atoi(idtxt);
            szCmdLine = space+1;
        }
        else
        {
537
            FIXME("Unhandled HTML Help command line parameter! (%.*s)\n", (int)(space-szCmdLine), szCmdLine);
538 539 540 541
            return 0;
        }
    }

542
    /* FIXME: Check szCmdLine for bad arguments */
543 544 545 546 547 548 549
    if (*szCmdLine == '\"')
        endq = strchr(++szCmdLine, '\"');

    if (endq)
        len = endq - szCmdLine;
    else
        len = strlen(szCmdLine);
550 551 552 553 554

    /* no filename given */
    if (!len)
        return 0;

555 556 557 558 559
    buflen = MultiByteToWideChar(CP_ACP, 0, szCmdLine, len, NULL, 0) + 1;
    filename = heap_alloc(buflen * sizeof(WCHAR));
    MultiByteToWideChar(CP_ACP, 0, szCmdLine, len, filename, buflen);
    filename[buflen-1] = 0;

560 561
    /* Open a specific help topic */
    if(mapid != -1)
562
        hwnd = HtmlHelpW(GetDesktopWindow(), filename, HH_HELP_CONTEXT, mapid);
563
    else
564
        hwnd = HtmlHelpW(GetDesktopWindow(), filename, HH_DISPLAY_TOPIC, 0);
565 566

    heap_free(filename);
567

568 569 570 571 572 573
    if (!hwnd)
    {
        ERR("Failed to open HTML Help file '%s'.\n", szCmdLine);
        return 0;
    }

574 575 576 577 578 579 580 581
    while (GetMessageW(&msg, 0, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessageW(&msg);
    }

    return 0;
}
582 583

/******************************************************************
584
 *		DllGetClassObject (HHCTRL.OCX.@)
585 586 587 588 589 590
 */
HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
{
    FIXME("(%s %s %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
    return CLASS_E_CLASSNOTAVAILABLE;
}
591 592 593 594 595 596

/***********************************************************************
 *		DllRegisterServer (HHCTRL.OCX.@)
 */
HRESULT WINAPI DllRegisterServer(void)
{
597
    return __wine_register_resources( hhctrl_hinstance );
598 599 600 601 602 603 604
}

/***********************************************************************
 *		DllUnregisterServer (HHCTRL.OCX.@)
 */
HRESULT WINAPI DllUnregisterServer(void)
{
605
    return __wine_unregister_resources( hhctrl_hinstance );
606
}