d3dx9: Implement volume texture filtering in D3DXFilterTexture.
[wine] / dlls / hhctrl.ocx / help.c
1 /*
2  * Help Viewer Implementation
3  *
4  * Copyright 2005 James Hawkins
5  * Copyright 2007 Jacek Caban for CodeWeavers
6  * Copyright 2011 Owen Rudge for CodeWeavers
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * 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 #include "wingdi.h"
26 #include "commctrl.h"
27 #include "wininet.h"
28
29 #include "wine/debug.h"
30
31 #include "resource.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(htmlhelp);
34
35 static LRESULT Help_OnSize(HWND hWnd);
36 static void ExpandContract(HHInfo *pHHInfo);
37
38 /* Window type defaults */
39
40 #define WINTYPE_DEFAULT_X           280
41 #define WINTYPE_DEFAULT_Y           100
42 #define WINTYPE_DEFAULT_WIDTH       740
43 #define WINTYPE_DEFAULT_HEIGHT      640
44 #define WINTYPE_DEFAULT_NAVWIDTH    250
45
46 #define TAB_TOP_PADDING     8
47 #define TAB_RIGHT_PADDING   4
48 #define TAB_MARGIN  8
49 #define EDIT_HEIGHT         20
50
51 static const WCHAR szEmpty[] = {0};
52
53 struct html_encoded_symbol {
54     const char *html_code;
55     char        ansi_symbol;
56 };
57
58 /*
59  * Table mapping the conversion between HTML encoded symbols and their ANSI code page equivalent.
60  * Note: Add additional entries in proper alphabetical order (a binary search is used on this table).
61  */
62 struct html_encoded_symbol html_encoded_symbols[] =
63 {
64     {"AElig",  0xC6},
65     {"Aacute", 0xC1},
66     {"Acirc",  0xC2},
67     {"Agrave", 0xC0},
68     {"Aring",  0xC5},
69     {"Atilde", 0xC3},
70     {"Auml",   0xC4},
71     {"Ccedil", 0xC7},
72     {"ETH",    0xD0},
73     {"Eacute", 0xC9},
74     {"Ecirc",  0xCA},
75     {"Egrave", 0xC8},
76     {"Euml",   0xCB},
77     {"Iacute", 0xCD},
78     {"Icirc",  0xCE},
79     {"Igrave", 0xCC},
80     {"Iuml",   0xCF},
81     {"Ntilde", 0xD1},
82     {"Oacute", 0xD3},
83     {"Ocirc",  0xD4},
84     {"Ograve", 0xD2},
85     {"Oslash", 0xD8},
86     {"Otilde", 0xD5},
87     {"Ouml",   0xD6},
88     {"THORN",  0xDE},
89     {"Uacute", 0xDA},
90     {"Ucirc",  0xDB},
91     {"Ugrave", 0xD9},
92     {"Uuml",   0xDC},
93     {"Yacute", 0xDD},
94     {"aacute", 0xE1},
95     {"acirc",  0xE2},
96     {"acute",  0xB4},
97     {"aelig",  0xE6},
98     {"agrave", 0xE0},
99     {"amp",    '&'},
100     {"aring",  0xE5},
101     {"atilde", 0xE3},
102     {"auml",   0xE4},
103     {"brvbar", 0xA6},
104     {"ccedil", 0xE7},
105     {"cedil",  0xB8},
106     {"cent",   0xA2},
107     {"copy",   0xA9},
108     {"curren", 0xA4},
109     {"deg",    0xB0},
110     {"divide", 0xF7},
111     {"eacute", 0xE9},
112     {"ecirc",  0xEA},
113     {"egrave", 0xE8},
114     {"eth",    0xF0},
115     {"euml",   0xEB},
116     {"frac12", 0xBD},
117     {"frac14", 0xBC},
118     {"frac34", 0xBE},
119     {"gt",     '>'},
120     {"iacute", 0xED},
121     {"icirc",  0xEE},
122     {"iexcl",  0xA1},
123     {"igrave", 0xEC},
124     {"iquest", 0xBF},
125     {"iuml",   0xEF},
126     {"laquo",  0xAB},
127     {"lt",     '<'},
128     {"macr",   0xAF},
129     {"micro",  0xB5},
130     {"middot", 0xB7},
131     {"nbsp",   ' '},
132     {"not",    0xAC},
133     {"ntilde", 0xF1},
134     {"oacute", 0xF3},
135     {"ocirc",  0xF4},
136     {"ograve", 0xF2},
137     {"ordf",   0xAA},
138     {"ordm",   0xBA},
139     {"oslash", 0xF8},
140     {"otilde", 0xF5},
141     {"ouml",   0xF6},
142     {"para",   0xB6},
143     {"plusmn", 0xB1},
144     {"pound",  0xA3},
145     {"quot",   '"'},
146     {"raquo",  0xBB},
147     {"reg",    0xAE},
148     {"sect",   0xA7},
149     {"shy",    0xAD},
150     {"sup1",   0xB9},
151     {"sup2",   0xB2},
152     {"sup3",   0xB3},
153     {"szlig",  0xDF},
154     {"thorn",  0xFE},
155     {"times",  0xD7},
156     {"uacute", 0xFA},
157     {"ucirc",  0xFB},
158     {"ugrave", 0xF9},
159     {"uml",    0xA8},
160     {"uuml",   0xFC},
161     {"yacute", 0xFD},
162     {"yen",    0xA5},
163     {"yuml",   0xFF}
164 };
165
166 /* Loads a string from the resource file */
167 static LPWSTR HH_LoadString(DWORD dwID)
168 {
169     LPWSTR string = NULL;
170     LPCWSTR stringresource;
171     int iSize;
172
173     iSize = LoadStringW(hhctrl_hinstance, dwID, (LPWSTR)&stringresource, 0);
174
175     string = heap_alloc((iSize + 2) * sizeof(WCHAR)); /* some strings (tab text) needs double-null termination */
176     memcpy(string, stringresource, iSize*sizeof(WCHAR));
177     string[iSize] = 0;
178
179     return string;
180 }
181
182 static HRESULT navigate_url(HHInfo *info, LPCWSTR surl)
183 {
184     VARIANT url;
185     HRESULT hres;
186
187     TRACE("%s\n", debugstr_w(surl));
188
189     V_VT(&url) = VT_BSTR;
190     V_BSTR(&url) = SysAllocString(surl);
191
192     hres = IWebBrowser2_Navigate2(info->web_browser, &url, 0, 0, 0, 0);
193
194     VariantClear(&url);
195
196     if(FAILED(hres))
197         TRACE("Navigation failed: %08x\n", hres);
198
199     return hres;
200 }
201
202 BOOL NavigateToUrl(HHInfo *info, LPCWSTR surl)
203 {
204     ChmPath chm_path;
205     BOOL ret;
206     HRESULT hres;
207
208     static const WCHAR url_indicator[] = {':', '/', '/', 0};
209
210     TRACE("%s\n", debugstr_w(surl));
211
212     if (strstrW(surl, url_indicator)) {
213         hres = navigate_url(info, surl);
214         if(SUCCEEDED(hres))
215             return TRUE;
216     } /* look up in chm if it doesn't look like a full url */
217
218     SetChmPath(&chm_path, info->pCHMInfo->szFile, surl);
219     ret = NavigateToChm(info, chm_path.chm_file, chm_path.chm_index);
220
221     heap_free(chm_path.chm_file);
222     heap_free(chm_path.chm_index);
223
224     return ret;
225 }
226
227 static BOOL AppendFullPathURL(LPCWSTR file, LPWSTR buf, LPCWSTR index)
228 {
229     static const WCHAR url_format[] =
230         {'m','k',':','@','M','S','I','T','S','t','o','r','e',':','%','s',':',':','%','s','%','s',0};
231     static const WCHAR slash[] = {'/',0};
232     static const WCHAR empty[] = {0};
233     WCHAR full_path[MAX_PATH];
234
235     TRACE("%s %p %s\n", debugstr_w(file), buf, debugstr_w(index));
236
237     if(!GetFullPathNameW(file, sizeof(full_path)/sizeof(full_path[0]), full_path, NULL)) {
238         WARN("GetFullPathName failed: %u\n", GetLastError());
239         return FALSE;
240     }
241
242     wsprintfW(buf, url_format, full_path, (!index || index[0] == '/') ? empty : slash, index);
243     return TRUE;
244 }
245
246 BOOL NavigateToChm(HHInfo *info, LPCWSTR file, LPCWSTR index)
247 {
248     WCHAR buf[INTERNET_MAX_URL_LENGTH];
249     LPWSTR ptr;
250
251     TRACE("%p %s %s\n", info, debugstr_w(file), debugstr_w(index));
252
253     if ((!info->web_browser) || !AppendFullPathURL(file, buf, index))
254         return FALSE;
255
256     /* FIXME: HACK */
257     if((ptr = strchrW(buf, '#')))
258        *ptr = 0;
259
260     return SUCCEEDED(navigate_url(info, buf));
261 }
262
263 static void DoSync(HHInfo *info)
264 {
265     WCHAR buf[INTERNET_MAX_URL_LENGTH];
266     HRESULT hres;
267     DWORD len;
268     BSTR url;
269
270     hres = IWebBrowser2_get_LocationURL(info->web_browser, &url);
271
272     if (FAILED(hres))
273     {
274         WARN("get_LocationURL failed: %08x\n", hres);
275         return;
276     }
277
278     /* If we're not currently viewing a page in the active .chm file, abort */
279     if ((!AppendFullPathURL(info->pszFile, buf, NULL)) || (len = lstrlenW(buf) > lstrlenW(url)))
280     {
281         SysFreeString(url);
282         return;
283     }
284
285     if (lstrcmpiW(buf, url) > 0)
286     {
287         static const WCHAR delimW[] = {':',':','/',0};
288         const WCHAR *index;
289
290         index = strstrW(url, delimW);
291
292         if (index)
293             ActivateContentTopic(info->tabs[TAB_CONTENTS].hwnd, index + 3, info->content); /* skip over ::/ */
294     }
295
296     SysFreeString(url);
297 }
298
299 /* Size Bar */
300
301 #define SIZEBAR_WIDTH   4
302
303 static const WCHAR szSizeBarClass[] = {
304     'H','H',' ','S','i','z','e','B','a','r',0
305 };
306
307 /* Draw the SizeBar */
308 static void SB_OnPaint(HWND hWnd)
309 {
310     PAINTSTRUCT ps;
311     HDC hdc;
312     RECT rc;
313     
314     hdc = BeginPaint(hWnd, &ps);
315
316     GetClientRect(hWnd, &rc);
317
318     /* dark frame */
319     rc.right += 1;
320     rc.bottom -= 1;
321     FrameRect(hdc, &rc, GetStockObject(GRAY_BRUSH));
322
323     /* white highlight */
324     SelectObject(hdc, GetStockObject(WHITE_PEN));
325     MoveToEx(hdc, rc.right, 1, NULL);
326     LineTo(hdc, 1, 1);
327     LineTo(hdc, 1, rc.bottom - 1);
328
329     
330     MoveToEx(hdc, 0, rc.bottom, NULL);
331     LineTo(hdc, rc.right, rc.bottom);
332
333     EndPaint(hWnd, &ps);
334 }
335
336 static void SB_OnLButtonDown(HWND hWnd, WPARAM wParam, LPARAM lParam)
337 {
338     SetCapture(hWnd);
339 }
340
341 static void SB_OnLButtonUp(HWND hWnd, WPARAM wParam, LPARAM lParam)
342 {
343     HHInfo *pHHInfo = (HHInfo *)GetWindowLongPtrW(hWnd, 0);
344     POINT pt;
345
346     pt.x = (short)LOWORD(lParam);
347     pt.y = (short)HIWORD(lParam);
348
349     /* update the window sizes */
350     pHHInfo->WinType.iNavWidth += pt.x;
351     Help_OnSize(hWnd);
352
353     ReleaseCapture();
354 }
355
356 static void SB_OnMouseMove(HWND hWnd, WPARAM wParam, LPARAM lParam)
357 {
358     /* ignore WM_MOUSEMOVE if not dragging the SizeBar */
359     if (!(wParam & MK_LBUTTON))
360         return;
361 }
362
363 static LRESULT CALLBACK SizeBar_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
364 {
365     switch (message)
366     {
367         case WM_LBUTTONDOWN:
368             SB_OnLButtonDown(hWnd, wParam, lParam);
369             break;
370         case WM_LBUTTONUP:
371             SB_OnLButtonUp(hWnd, wParam, lParam);
372             break;
373         case WM_MOUSEMOVE:
374             SB_OnMouseMove(hWnd, wParam, lParam);
375             break;
376         case WM_PAINT:
377             SB_OnPaint(hWnd);
378             break;
379         default:
380             return DefWindowProcW(hWnd, message, wParam, lParam);
381     }
382
383     return 0;
384 }
385
386 static void HH_RegisterSizeBarClass(HHInfo *pHHInfo)
387 {
388     WNDCLASSEXW wcex;
389
390     wcex.cbSize         = sizeof(WNDCLASSEXW);
391     wcex.style          = 0;
392     wcex.lpfnWndProc    = SizeBar_WndProc;
393     wcex.cbClsExtra     = 0;
394     wcex.cbWndExtra     = sizeof(LONG_PTR);
395     wcex.hInstance      = hhctrl_hinstance;
396     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
397     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_SIZEWE);
398     wcex.hbrBackground  = (HBRUSH)(COLOR_MENU + 1);
399     wcex.lpszMenuName   = NULL;
400     wcex.lpszClassName  = szSizeBarClass;
401     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
402
403     RegisterClassExW(&wcex);
404 }
405
406 static void SB_GetSizeBarRect(HHInfo *info, RECT *rc)
407 {
408     RECT rectWND, rectTB, rectNP;
409
410     GetClientRect(info->WinType.hwndHelp, &rectWND);
411     GetClientRect(info->WinType.hwndToolBar, &rectTB);
412     GetClientRect(info->WinType.hwndNavigation, &rectNP);
413
414     rc->left = rectNP.right;
415     rc->top = rectTB.bottom;
416     rc->bottom = rectWND.bottom - rectTB.bottom;
417     rc->right = SIZEBAR_WIDTH;
418 }
419
420 static BOOL HH_AddSizeBar(HHInfo *pHHInfo)
421 {
422     HWND hWnd;
423     HWND hwndParent = pHHInfo->WinType.hwndHelp;
424     DWORD dwStyles = WS_CHILDWINDOW | WS_OVERLAPPED;
425     DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
426     RECT rc;
427
428     if (!pHHInfo->WinType.fNotExpanded)
429         dwStyles |= WS_VISIBLE;
430
431     SB_GetSizeBarRect(pHHInfo, &rc);
432
433     hWnd = CreateWindowExW(dwExStyles, szSizeBarClass, szEmpty, dwStyles,
434                            rc.left, rc.top, rc.right, rc.bottom,
435                            hwndParent, NULL, hhctrl_hinstance, NULL);
436     if (!hWnd)
437         return FALSE;
438
439     /* store the pointer to the HH info struct */
440     SetWindowLongPtrW(hWnd, 0, (LONG_PTR)pHHInfo);
441
442     pHHInfo->hwndSizeBar = hWnd;
443     return TRUE;
444 }
445
446 /* Child Window */
447
448 static const WCHAR szChildClass[] = {
449     'H','H',' ','C','h','i','l','d',0
450 };
451
452 static LRESULT Child_OnPaint(HWND hWnd)
453 {
454     PAINTSTRUCT ps;
455     HDC hdc;
456     RECT rc;
457
458     hdc = BeginPaint(hWnd, &ps);
459
460     /* Only paint the Navigation pane, identified by the fact
461      * that it has a child window
462      */
463     if (GetWindow(hWnd, GW_CHILD))
464     {
465         GetClientRect(hWnd, &rc);
466
467         /* set the border color */
468         SelectObject(hdc, GetStockObject(DC_PEN));
469         SetDCPenColor(hdc, GetSysColor(COLOR_BTNSHADOW));
470
471         /* Draw the top border */
472         LineTo(hdc, rc.right, 0);
473
474         SelectObject(hdc, GetStockObject(WHITE_PEN));
475         MoveToEx(hdc, 0, 1, NULL);
476         LineTo(hdc, rc.right, 1);
477     }
478
479     EndPaint(hWnd, &ps);
480
481     return 0;
482 }
483
484 static void ResizeTabChild(HHInfo *info, int tab)
485 {
486     HWND hwnd = info->tabs[tab].hwnd;
487     INT width, height;
488     RECT rect, tabrc;
489     DWORD cnt;
490
491     GetClientRect(info->WinType.hwndNavigation, &rect);
492     SendMessageW(info->hwndTabCtrl, TCM_GETITEMRECT, 0, (LPARAM)&tabrc);
493     cnt = SendMessageW(info->hwndTabCtrl, TCM_GETROWCOUNT, 0, 0);
494
495     rect.left = TAB_MARGIN;
496     rect.top = TAB_TOP_PADDING + cnt*(tabrc.bottom-tabrc.top) + TAB_MARGIN;
497     rect.right -= TAB_RIGHT_PADDING + TAB_MARGIN;
498     rect.bottom -= TAB_MARGIN;
499     width = rect.right-rect.left;
500     height = rect.bottom-rect.top;
501
502     SetWindowPos(hwnd, NULL, rect.left, rect.top, width, height,
503                  SWP_NOZORDER | SWP_NOACTIVATE);
504
505     switch (tab)
506     {
507     case TAB_INDEX: {
508         int scroll_width = GetSystemMetrics(SM_CXVSCROLL);
509         int border_width = GetSystemMetrics(SM_CXBORDER);
510         int edge_width = GetSystemMetrics(SM_CXEDGE);
511
512         /* Resize the tab widget column to perfectly fit the tab window and
513          * leave sufficient space for the scroll widget.
514          */
515         SendMessageW(info->tabs[TAB_INDEX].hwnd, LVM_SETCOLUMNWIDTH, 0,
516                      width-scroll_width-2*border_width-2*edge_width);
517
518         break;
519     }
520     case TAB_SEARCH: {
521         int scroll_width = GetSystemMetrics(SM_CXVSCROLL);
522         int border_width = GetSystemMetrics(SM_CXBORDER);
523         int edge_width = GetSystemMetrics(SM_CXEDGE);
524         int top_pos = 0;
525
526         SetWindowPos(info->search.hwndEdit, NULL, 0, top_pos, width,
527                       EDIT_HEIGHT, SWP_NOZORDER | SWP_NOACTIVATE);
528         top_pos += EDIT_HEIGHT + TAB_MARGIN;
529         SetWindowPos(info->search.hwndList, NULL, 0, top_pos, width,
530                       height-top_pos, SWP_NOZORDER | SWP_NOACTIVATE);
531         /* Resize the tab widget column to perfectly fit the tab window and
532          * leave sufficient space for the scroll widget.
533          */
534         SendMessageW(info->search.hwndList, LVM_SETCOLUMNWIDTH, 0,
535                      width-scroll_width-2*border_width-2*edge_width);
536
537         break;
538     }
539     }
540 }
541
542 static LRESULT Child_OnSize(HWND hwnd)
543 {
544     HHInfo *info = (HHInfo*)GetWindowLongPtrW(hwnd, 0);
545     RECT rect;
546
547     if(!info || hwnd != info->WinType.hwndNavigation)
548         return 0;
549
550     GetClientRect(hwnd, &rect);
551     SetWindowPos(info->hwndTabCtrl, HWND_TOP, 0, 0,
552                  rect.right - TAB_RIGHT_PADDING,
553                  rect.bottom - TAB_TOP_PADDING, SWP_NOMOVE);
554
555     ResizeTabChild(info, TAB_CONTENTS);
556     ResizeTabChild(info, TAB_INDEX);
557     ResizeTabChild(info, TAB_SEARCH);
558     return 0;
559 }
560
561 static LRESULT OnTabChange(HWND hwnd)
562 {
563     HHInfo *info = (HHInfo*)GetWindowLongPtrW(hwnd, 0);
564     int tab_id, tab_index, i;
565
566     TRACE("%p\n", hwnd);
567
568     if (!info)
569         return 0;
570
571     if(info->tabs[info->current_tab].hwnd)
572         ShowWindow(info->tabs[info->current_tab].hwnd, SW_HIDE);
573
574     tab_id = (int) SendMessageW(info->hwndTabCtrl, TCM_GETCURSEL, 0, 0);
575     /* convert the ID of the tab to an index in our tab list */
576     tab_index = -1;
577     for (i=0; i<TAB_NUMTABS; i++)
578     {
579         if (info->tabs[i].id == tab_id)
580         {
581             tab_index = i;
582             break;
583         }
584     }
585     if (tab_index == -1)
586     {
587         FIXME("Tab ID %d does not correspond to a valid index in the tab list.\n", tab_id);
588         return 0;
589     }
590     info->current_tab = tab_index;
591
592     if(info->tabs[info->current_tab].hwnd)
593         ShowWindow(info->tabs[info->current_tab].hwnd, SW_SHOW);
594
595     return 0;
596 }
597
598 static LRESULT OnTopicChange(HHInfo *info, void *user_data)
599 {
600     LPCWSTR chmfile = NULL, name = NULL, local = NULL;
601     ContentItem *citer;
602     SearchItem *siter;
603     IndexItem *iiter;
604
605     if(!user_data || !info)
606         return 0;
607
608     switch (info->current_tab)
609     {
610     case TAB_CONTENTS:
611         citer = (ContentItem *) user_data;
612         name = citer->name;
613         local = citer->local;
614         while(citer) {
615             if(citer->merge.chm_file) {
616                 chmfile = citer->merge.chm_file;
617                 break;
618             }
619             citer = citer->parent;
620         }
621         break;
622     case TAB_INDEX:
623         iiter = (IndexItem *) user_data;
624         if(iiter->nItems == 0) {
625             FIXME("No entries for this item!\n");
626             return 0;
627         }
628         if(iiter->nItems > 1) {
629             int i = 0;
630             LVITEMW lvi;
631
632             SendMessageW(info->popup.hwndList, LVM_DELETEALLITEMS, 0, 0);
633             for(i=0;i<iiter->nItems;i++) {
634                 IndexSubItem *item = &iiter->items[i];
635                 WCHAR *name = iiter->keyword;
636
637                 if(!item->name)
638                     item->name = GetDocumentTitle(info->pCHMInfo, item->local);
639                 if(item->name)
640                     name = item->name;
641                 memset(&lvi, 0, sizeof(lvi));
642                 lvi.iItem = i;
643                 lvi.mask = LVIF_TEXT|LVIF_PARAM;
644                 lvi.cchTextMax = strlenW(name)+1;
645                 lvi.pszText = name;
646                 lvi.lParam = (LPARAM) item;
647                 SendMessageW(info->popup.hwndList, LVM_INSERTITEMW, 0, (LPARAM)&lvi);
648             }
649             ShowWindow(info->popup.hwndPopup, SW_SHOW);
650             return 0;
651         }
652         name = iiter->items[0].name;
653         local = iiter->items[0].local;
654         chmfile = iiter->merge.chm_file;
655         break;
656     case TAB_SEARCH:
657         siter = (SearchItem *) user_data;
658         name = siter->filename;
659         local = siter->filename;
660         chmfile = info->pCHMInfo->szFile;
661         break;
662     default:
663         FIXME("Unhandled operation for this tab!\n");
664         return 0;
665     }
666
667     if(!chmfile)
668     {
669         FIXME("No help file found for this item!\n");
670         return 0;
671     }
672
673     TRACE("name %s loal %s\n", debugstr_w(name), debugstr_w(local));
674
675     NavigateToChm(info, chmfile, local);
676     return 0;
677 }
678
679 /* Capture the Enter/Return key and send it up to Child_WndProc as an NM_RETURN message */
680 static LRESULT CALLBACK EditChild_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
681 {
682     WNDPROC editWndProc = (WNDPROC)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
683
684     if(message == WM_KEYUP && wParam == VK_RETURN)
685     {
686         NMHDR nmhdr;
687
688         nmhdr.hwndFrom = hWnd;
689         nmhdr.code = NM_RETURN;
690         SendMessageW(GetParent(GetParent(hWnd)), WM_NOTIFY, wParam, (LPARAM)&nmhdr);
691     }
692     return editWndProc(hWnd, message, wParam, lParam);
693 }
694
695 static LRESULT CALLBACK Child_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
696 {
697     switch (message)
698     {
699     case WM_PAINT:
700         return Child_OnPaint(hWnd);
701     case WM_SIZE:
702         return Child_OnSize(hWnd);
703     case WM_NOTIFY: {
704         HHInfo *info = (HHInfo*)GetWindowLongPtrW(hWnd, 0);
705         NMHDR *nmhdr = (NMHDR*)lParam;
706
707         switch(nmhdr->code) {
708         case TCN_SELCHANGE:
709             return OnTabChange(hWnd);
710         case TVN_SELCHANGEDW:
711             return OnTopicChange(info, (void*)((NMTREEVIEWW *)lParam)->itemNew.lParam);
712         case TVN_ITEMEXPANDINGW: {
713             TVITEMW *item = &((NMTREEVIEWW *)lParam)->itemNew;
714             HWND hwndTreeView = info->tabs[TAB_CONTENTS].hwnd;
715
716             item->mask = TVIF_IMAGE|TVIF_SELECTEDIMAGE;
717             if (item->state & TVIS_EXPANDED)
718             {
719                 item->iImage = HHTV_FOLDER;
720                 item->iSelectedImage = HHTV_FOLDER;
721             }
722             else
723             {
724                 item->iImage = HHTV_OPENFOLDER;
725                 item->iSelectedImage = HHTV_OPENFOLDER;
726             }
727             SendMessageW(hwndTreeView, TVM_SETITEMW, 0, (LPARAM)item);
728             return 0;
729         }
730         case NM_DBLCLK:
731             if(!info)
732                 return 0;
733             switch(info->current_tab)
734             {
735             case TAB_INDEX:
736                 return OnTopicChange(info, (void*)((NMITEMACTIVATE *)lParam)->lParam);
737             case TAB_SEARCH:
738                 return OnTopicChange(info, (void*)((NMITEMACTIVATE *)lParam)->lParam);
739             }
740             break;
741         case NM_RETURN:
742             if(!info)
743                 return 0;
744             switch(info->current_tab) {
745             case TAB_INDEX: {
746                 HWND hwndList = info->tabs[TAB_INDEX].hwnd;
747                 LVITEMW lvItem;
748
749                 lvItem.iItem = (int) SendMessageW(hwndList, LVM_GETSELECTIONMARK, 0, 0);
750                 lvItem.mask = TVIF_PARAM;
751                 SendMessageW(hwndList, LVM_GETITEMW, 0, (LPARAM)&lvItem);
752                 OnTopicChange(info, (void*) lvItem.lParam);
753                 return 0;
754             }
755             case TAB_SEARCH: {
756                 if(nmhdr->hwndFrom == info->search.hwndEdit) {
757                     char needle[100];
758                     DWORD i, len;
759
760                     len = GetWindowTextA(info->search.hwndEdit, needle, sizeof(needle));
761                     if(!len)
762                     {
763                         FIXME("Unable to get search text.\n");
764                         return 0;
765                     }
766                     /* Convert the requested text for comparison later against the
767                      * lower case version of HTML file contents.
768                      */
769                     for(i=0;i<len;i++)
770                         needle[i] = tolower(needle[i]);
771                     InitSearch(info, needle);
772                     return 0;
773                 }else if(nmhdr->hwndFrom == info->search.hwndList) {
774                     HWND hwndList = info->search.hwndList;
775                     LVITEMW lvItem;
776
777                     lvItem.iItem = (int) SendMessageW(hwndList, LVM_GETSELECTIONMARK, 0, 0);
778                     lvItem.mask = TVIF_PARAM;
779                     SendMessageW(hwndList, LVM_GETITEMW, 0, (LPARAM)&lvItem);
780                     OnTopicChange(info, (void*) lvItem.lParam);
781                     return 0;
782                 }
783                 break;
784             }
785             }
786             break;
787         }
788         break;
789     }
790     default:
791         return DefWindowProcW(hWnd, message, wParam, lParam);
792     }
793
794     return 0;
795 }
796
797 static void HH_RegisterChildWndClass(HHInfo *pHHInfo)
798 {
799     WNDCLASSEXW wcex;
800
801     wcex.cbSize         = sizeof(WNDCLASSEXW);
802     wcex.style          = 0;
803     wcex.lpfnWndProc    = Child_WndProc;
804     wcex.cbClsExtra     = 0;
805     wcex.cbWndExtra     = sizeof(LONG_PTR);
806     wcex.hInstance      = hhctrl_hinstance;
807     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
808     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
809     wcex.hbrBackground  = (HBRUSH)(COLOR_BTNFACE + 1);
810     wcex.lpszMenuName   = NULL;
811     wcex.lpszClassName  = szChildClass;
812     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
813
814     RegisterClassExW(&wcex);
815 }
816
817 /* Toolbar */
818
819 #define ICON_SIZE   20
820
821 static void DisplayPopupMenu(HHInfo *info)
822 {
823     HMENU menu, submenu;
824     TBBUTTONINFOW button;
825     MENUITEMINFOW item;
826     POINT coords;
827     RECT rect;
828     DWORD index;
829
830     menu = LoadMenuW(hhctrl_hinstance, MAKEINTRESOURCEW(MENU_POPUP));
831
832     if (!menu)
833         return;
834
835     submenu = GetSubMenu(menu, 0);
836
837     /* Update the Show/Hide menu item */
838     item.cbSize = sizeof(MENUITEMINFOW);
839     item.fMask = MIIM_FTYPE | MIIM_STATE | MIIM_STRING;
840     item.fType = MFT_STRING;
841     item.fState = MF_ENABLED;
842
843     if (info->WinType.fNotExpanded)
844         item.dwTypeData = HH_LoadString(IDS_SHOWTABS);
845     else
846         item.dwTypeData = HH_LoadString(IDS_HIDETABS);
847
848     SetMenuItemInfoW(submenu, IDTB_EXPAND, FALSE, &item);
849     heap_free(item.dwTypeData);
850
851     /* Find the index toolbar button */
852     button.cbSize = sizeof(TBBUTTONINFOW);
853     button.dwMask = TBIF_COMMAND;
854     index = SendMessageW(info->WinType.hwndToolBar, TB_GETBUTTONINFOW, IDTB_OPTIONS, (LPARAM) &button);
855
856     if (index == -1)
857        return;
858
859     /* Get position */
860     SendMessageW(info->WinType.hwndToolBar, TB_GETITEMRECT, index, (LPARAM) &rect);
861
862     coords.x = rect.left;
863     coords.y = rect.bottom;
864
865     ClientToScreen(info->WinType.hwndToolBar, &coords);
866     TrackPopupMenu(submenu, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_LEFTBUTTON | TPM_NOANIMATION, coords.x, coords.y, 0, info->WinType.hwndHelp, NULL);
867 }
868
869 static void TB_OnClick(HWND hWnd, DWORD dwID)
870 {
871     HHInfo *info = (HHInfo *)GetWindowLongPtrW(hWnd, 0);
872
873     switch (dwID)
874     {
875         case IDTB_STOP:
876             DoPageAction(info, WB_STOP);
877             break;
878         case IDTB_REFRESH:
879             DoPageAction(info, WB_REFRESH);
880             break;
881         case IDTB_BACK:
882             DoPageAction(info, WB_GOBACK);
883             break;
884         case IDTB_HOME:
885             NavigateToChm(info, info->pCHMInfo->szFile, info->WinType.pszHome);
886             break;
887         case IDTB_FORWARD:
888             DoPageAction(info, WB_GOFORWARD);
889             break;
890         case IDTB_PRINT:
891             DoPageAction(info, WB_PRINT);
892             break;
893         case IDTB_EXPAND:
894         case IDTB_CONTRACT:
895             ExpandContract(info);
896             break;
897         case IDTB_SYNC:
898             DoSync(info);
899             break;
900         case IDTB_OPTIONS:
901             DisplayPopupMenu(info);
902             break;
903         case IDTB_NOTES:
904         case IDTB_CONTENTS:
905         case IDTB_INDEX:
906         case IDTB_SEARCH:
907         case IDTB_HISTORY:
908         case IDTB_FAVORITES:
909             /* These are officially unimplemented as of the Windows 7 SDK */
910             break;
911         case IDTB_BROWSE_FWD:
912         case IDTB_BROWSE_BACK:
913         case IDTB_JUMP1:
914         case IDTB_JUMP2:
915         case IDTB_CUSTOMIZE:
916         case IDTB_ZOOM:
917         case IDTB_TOC_NEXT:
918         case IDTB_TOC_PREV:
919             break;
920     }
921 }
922
923 static void TB_AddButton(TBBUTTON *pButtons, DWORD dwIndex, DWORD dwID, DWORD dwBitmap)
924 {
925     pButtons[dwIndex].iBitmap = dwBitmap;
926     pButtons[dwIndex].idCommand = dwID;
927     pButtons[dwIndex].fsState = TBSTATE_ENABLED;
928     pButtons[dwIndex].fsStyle = BTNS_BUTTON;
929     pButtons[dwIndex].dwData = 0;
930     pButtons[dwIndex].iString = 0;
931 }
932
933 static void TB_AddButtonsFromFlags(HHInfo *pHHInfo, TBBUTTON *pButtons, DWORD dwButtonFlags, LPDWORD pdwNumButtons)
934 {
935     int nHistBitmaps = 0, nStdBitmaps = 0, nHHBitmaps = 0;
936     HWND hToolbar = pHHInfo->WinType.hwndToolBar;
937     TBADDBITMAP tbAB;
938     DWORD unsupported;
939
940     /* Common bitmaps */
941     tbAB.hInst = HINST_COMMCTRL;
942     tbAB.nID = IDB_HIST_LARGE_COLOR;
943     nHistBitmaps = SendMessageW(hToolbar, TB_ADDBITMAP, 0, (LPARAM)&tbAB);
944     tbAB.nID = IDB_STD_LARGE_COLOR;
945     nStdBitmaps = SendMessageW(hToolbar, TB_ADDBITMAP, 0, (LPARAM)&tbAB);
946     /* hhctrl.ocx bitmaps */
947     tbAB.hInst = hhctrl_hinstance;
948     tbAB.nID = IDB_HHTOOLBAR;
949     nHHBitmaps = SendMessageW(hToolbar, TB_ADDBITMAP, HHTB_NUMBITMAPS, (LPARAM)&tbAB);
950
951     *pdwNumButtons = 0;
952
953     unsupported = dwButtonFlags & (HHWIN_BUTTON_BROWSE_FWD |
954         HHWIN_BUTTON_BROWSE_BCK | HHWIN_BUTTON_NOTES | HHWIN_BUTTON_CONTENTS |
955         HHWIN_BUTTON_INDEX | HHWIN_BUTTON_SEARCH | HHWIN_BUTTON_HISTORY |
956         HHWIN_BUTTON_FAVORITES | HHWIN_BUTTON_JUMP1 | HHWIN_BUTTON_JUMP2 |
957         HHWIN_BUTTON_ZOOM | HHWIN_BUTTON_TOC_NEXT | HHWIN_BUTTON_TOC_PREV);
958     if (unsupported)
959         FIXME("got asked for unsupported buttons: %06x\n", unsupported);
960
961     if (dwButtonFlags & HHWIN_BUTTON_EXPAND)
962     {
963         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_EXPAND, nHHBitmaps + HHTB_EXPAND);
964         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_CONTRACT, nHHBitmaps + HHTB_CONTRACT);
965
966         if (pHHInfo->WinType.fNotExpanded)
967             pButtons[1].fsState |= TBSTATE_HIDDEN;
968         else
969             pButtons[0].fsState |= TBSTATE_HIDDEN;
970     }
971
972     if (dwButtonFlags & HHWIN_BUTTON_BACK)
973         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_BACK, nHistBitmaps + HIST_BACK);
974
975     if (dwButtonFlags & HHWIN_BUTTON_FORWARD)
976         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_FORWARD, nHistBitmaps + HIST_FORWARD);
977
978     if (dwButtonFlags & HHWIN_BUTTON_STOP)
979         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_STOP, nHHBitmaps + HHTB_STOP);
980
981     if (dwButtonFlags & HHWIN_BUTTON_REFRESH)
982         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_REFRESH, nHHBitmaps + HHTB_REFRESH);
983
984     if (dwButtonFlags & HHWIN_BUTTON_HOME)
985         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_HOME, nHHBitmaps + HHTB_HOME);
986
987     if (dwButtonFlags & HHWIN_BUTTON_SYNC)
988         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_SYNC, nHHBitmaps + HHTB_SYNC);
989
990     if (dwButtonFlags & HHWIN_BUTTON_OPTIONS)
991         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_OPTIONS, nStdBitmaps + STD_PROPERTIES);
992
993     if (dwButtonFlags & HHWIN_BUTTON_PRINT)
994         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_PRINT, nStdBitmaps + STD_PRINT);
995 }
996
997 static BOOL HH_AddToolbar(HHInfo *pHHInfo)
998 {
999     HWND hToolbar;
1000     HWND hwndParent = pHHInfo->WinType.hwndHelp;
1001     DWORD toolbarFlags;
1002     TBBUTTON buttons[IDTB_TOC_PREV - IDTB_EXPAND];
1003     DWORD dwStyles, dwExStyles;
1004     DWORD dwNumButtons, dwIndex;
1005
1006     if (pHHInfo->WinType.fsWinProperties & HHWIN_PARAM_TB_FLAGS)
1007         toolbarFlags = pHHInfo->WinType.fsToolBarFlags;
1008     else
1009         toolbarFlags = HHWIN_DEF_BUTTONS;
1010
1011     dwStyles = WS_CHILDWINDOW | WS_VISIBLE | TBSTYLE_FLAT |
1012                TBSTYLE_WRAPABLE | TBSTYLE_TOOLTIPS | CCS_NODIVIDER;
1013     dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
1014
1015     hToolbar = CreateWindowExW(dwExStyles, TOOLBARCLASSNAMEW, NULL, dwStyles,
1016                                0, 0, 0, 0, hwndParent, NULL,
1017                                hhctrl_hinstance, NULL);
1018     if (!hToolbar)
1019         return FALSE;
1020     pHHInfo->WinType.hwndToolBar = hToolbar;
1021
1022     SendMessageW(hToolbar, TB_SETBITMAPSIZE, 0, MAKELONG(ICON_SIZE, ICON_SIZE));
1023     SendMessageW(hToolbar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
1024     SendMessageW(hToolbar, WM_SETFONT, (WPARAM)pHHInfo->hFont, TRUE);
1025
1026     TB_AddButtonsFromFlags(pHHInfo, buttons, toolbarFlags, &dwNumButtons);
1027
1028     for (dwIndex = 0; dwIndex < dwNumButtons; dwIndex++)
1029     {
1030         LPWSTR szBuf = HH_LoadString(buttons[dwIndex].idCommand);
1031         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     ShowWindow(hToolbar, SW_SHOW);
1041
1042     return TRUE;
1043 }
1044
1045 /* Navigation Pane */
1046
1047 static void NP_GetNavigationRect(HHInfo *pHHInfo, RECT *rc)
1048 {
1049     HWND hwndParent = pHHInfo->WinType.hwndHelp;
1050     HWND hwndToolbar = pHHInfo->WinType.hwndToolBar;
1051     RECT rectWND, rectTB;
1052
1053     GetClientRect(hwndParent, &rectWND);
1054     GetClientRect(hwndToolbar, &rectTB);
1055
1056     rc->left = 0;
1057     rc->top = rectTB.bottom;
1058     rc->bottom = rectWND.bottom - rectTB.bottom;
1059
1060     if (!(pHHInfo->WinType.fsValidMembers & HHWIN_PARAM_NAV_WIDTH) &&
1061           pHHInfo->WinType.iNavWidth == 0)
1062     {
1063         pHHInfo->WinType.iNavWidth = WINTYPE_DEFAULT_NAVWIDTH;
1064     }
1065
1066     rc->right = pHHInfo->WinType.iNavWidth;
1067 }
1068
1069 static DWORD NP_CreateTab(HINSTANCE hInstance, HWND hwndTabCtrl, DWORD index)
1070 {
1071     TCITEMW tie;
1072     LPWSTR tabText = HH_LoadString(index);
1073     DWORD ret;
1074
1075     tie.mask = TCIF_TEXT;
1076     tie.pszText = tabText;
1077
1078     ret = SendMessageW( hwndTabCtrl, TCM_INSERTITEMW, index, (LPARAM)&tie );
1079
1080     heap_free(tabText);
1081     return ret;
1082 }
1083
1084 static BOOL HH_AddNavigationPane(HHInfo *info)
1085 {
1086     HWND hWnd, hwndTabCtrl;
1087     HWND hwndParent = info->WinType.hwndHelp;
1088     DWORD dwStyles = WS_CHILDWINDOW;
1089     DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
1090     RECT rc;
1091
1092     if (!info->WinType.fNotExpanded)
1093         dwStyles |= WS_VISIBLE;
1094
1095     NP_GetNavigationRect(info, &rc);
1096
1097     hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
1098                            rc.left, rc.top, rc.right, rc.bottom,
1099                            hwndParent, NULL, hhctrl_hinstance, NULL);
1100     if (!hWnd)
1101         return FALSE;
1102
1103     SetWindowLongPtrW(hWnd, 0, (LONG_PTR)info);
1104
1105     hwndTabCtrl = CreateWindowExW(dwExStyles, WC_TABCONTROLW, szEmpty, dwStyles | WS_VISIBLE,
1106                                   0, TAB_TOP_PADDING,
1107                                   rc.right - TAB_RIGHT_PADDING,
1108                                   rc.bottom - TAB_TOP_PADDING,
1109                                   hWnd, NULL, hhctrl_hinstance, NULL);
1110     if (!hwndTabCtrl)
1111         return FALSE;
1112
1113     if (*info->WinType.pszToc)
1114         info->tabs[TAB_CONTENTS].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_CONTENTS);
1115
1116     if (*info->WinType.pszIndex)
1117         info->tabs[TAB_INDEX].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_INDEX);
1118
1119     if (info->WinType.fsWinProperties & HHWIN_PROP_TAB_SEARCH)
1120         info->tabs[TAB_SEARCH].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_SEARCH);
1121
1122     if (info->WinType.fsWinProperties & HHWIN_PROP_TAB_FAVORITES)
1123         info->tabs[TAB_FAVORITES].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_FAVORITES);
1124
1125     SendMessageW(hwndTabCtrl, WM_SETFONT, (WPARAM)info->hFont, TRUE);
1126
1127     info->hwndTabCtrl = hwndTabCtrl;
1128     info->WinType.hwndNavigation = hWnd;
1129     return TRUE;
1130 }
1131
1132 /* HTML Pane */
1133
1134 static void HP_GetHTMLRect(HHInfo *info, RECT *rc)
1135 {
1136     RECT rectTB, rectWND, rectNP, rectSB;
1137
1138     GetClientRect(info->WinType.hwndHelp, &rectWND);
1139     GetClientRect(info->WinType.hwndToolBar, &rectTB);
1140     GetClientRect(info->hwndSizeBar, &rectSB);
1141
1142     if (info->WinType.fNotExpanded)
1143         rc->left = 0;
1144     else
1145     {
1146         GetClientRect(info->WinType.hwndNavigation, &rectNP);
1147         rc->left = rectNP.right + rectSB.right;
1148     }
1149
1150     rc->top = rectTB.bottom;
1151     rc->right = rectWND.right - rc->left;
1152     rc->bottom = rectWND.bottom - rectTB.bottom;
1153 }
1154
1155 static BOOL HH_AddHTMLPane(HHInfo *pHHInfo)
1156 {
1157     HWND hWnd;
1158     HWND hwndParent = pHHInfo->WinType.hwndHelp;
1159     DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN;
1160     DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_CLIENTEDGE;
1161     RECT rc;
1162
1163     HP_GetHTMLRect(pHHInfo, &rc);
1164
1165     hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
1166                            rc.left, rc.top, rc.right, rc.bottom,
1167                            hwndParent, NULL, hhctrl_hinstance, NULL);
1168     if (!hWnd)
1169         return FALSE;
1170
1171     if (!InitWebBrowser(pHHInfo, hWnd))
1172         return FALSE;
1173
1174     /* store the pointer to the HH info struct */
1175     SetWindowLongPtrW(hWnd, 0, (LONG_PTR)pHHInfo);
1176
1177     ShowWindow(hWnd, SW_SHOW);
1178     UpdateWindow(hWnd);
1179
1180     pHHInfo->WinType.hwndHTML = hWnd;
1181     return TRUE;
1182 }
1183
1184 static BOOL AddContentTab(HHInfo *info)
1185 {
1186     HIMAGELIST hImageList;
1187     HBITMAP hBitmap;
1188     HWND hWnd;
1189
1190     if(info->tabs[TAB_CONTENTS].id == -1)
1191         return TRUE; /* No "Contents" tab */
1192     hWnd = CreateWindowExW(WS_EX_CLIENTEDGE, WC_TREEVIEWW, szEmpty, WS_CHILD | WS_BORDER | TVS_LINESATROOT
1193                            | TVS_SHOWSELALWAYS | TVS_HASBUTTONS, 50, 50, 100, 100,
1194                            info->WinType.hwndNavigation, NULL, hhctrl_hinstance, NULL);
1195     if(!hWnd) {
1196         ERR("Could not create treeview control\n");
1197         return FALSE;
1198     }
1199
1200     hImageList = ImageList_Create(16, 16, ILC_COLOR32, 0, HHTV_NUMBITMAPS);
1201     hBitmap = LoadBitmapW(hhctrl_hinstance, MAKEINTRESOURCEW(IDB_HHTREEVIEW));
1202     ImageList_Add(hImageList, hBitmap, NULL);
1203     SendMessageW(hWnd, TVM_SETIMAGELIST, TVSIL_NORMAL, (LPARAM)hImageList);
1204
1205     info->contents.hImageList = hImageList;
1206     info->tabs[TAB_CONTENTS].hwnd = hWnd;
1207     ResizeTabChild(info, TAB_CONTENTS);
1208     ShowWindow(hWnd, SW_SHOW);
1209
1210     return TRUE;
1211 }
1212
1213 static BOOL AddIndexTab(HHInfo *info)
1214 {
1215     char hidden_column[] = "Column";
1216     LVCOLUMNA lvc;
1217
1218     if(info->tabs[TAB_INDEX].id == -1)
1219         return TRUE; /* No "Index" tab */
1220     info->tabs[TAB_INDEX].hwnd = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW,
1221            szEmpty, WS_CHILD | WS_BORDER | LVS_SINGLESEL | LVS_REPORT | LVS_NOCOLUMNHEADER, 50, 50, 100, 100,
1222            info->WinType.hwndNavigation, NULL, hhctrl_hinstance, NULL);
1223     if(!info->tabs[TAB_INDEX].hwnd) {
1224         ERR("Could not create ListView control\n");
1225         return FALSE;
1226     }
1227     memset(&lvc, 0, sizeof(lvc));
1228     lvc.mask = LVCF_TEXT;
1229     lvc.pszText = hidden_column;
1230     if(SendMessageW(info->tabs[TAB_INDEX].hwnd, LVM_INSERTCOLUMNA, 0, (LPARAM) &lvc) == -1)
1231     {
1232         ERR("Could not create ListView column\n");
1233         return FALSE;
1234     }
1235
1236     ResizeTabChild(info, TAB_INDEX);
1237     ShowWindow(info->tabs[TAB_INDEX].hwnd, SW_HIDE);
1238
1239     return TRUE;
1240 }
1241
1242 static BOOL AddSearchTab(HHInfo *info)
1243 {
1244     HWND hwndList, hwndEdit, hwndContainer;
1245     char hidden_column[] = "Column";
1246     WNDPROC editWndProc;
1247     LVCOLUMNA lvc;
1248
1249     if(info->tabs[TAB_SEARCH].id == -1)
1250         return TRUE; /* No "Search" tab */
1251     hwndContainer = CreateWindowExW(WS_EX_CONTROLPARENT, szChildClass, szEmpty,
1252                                     WS_CHILD, 0, 0, 0, 0, info->WinType.hwndNavigation,
1253                                     NULL, hhctrl_hinstance, NULL);
1254     if(!hwndContainer) {
1255         ERR("Could not create search window container control.\n");
1256         return FALSE;
1257     }
1258     hwndEdit = CreateWindowExW(WS_EX_CLIENTEDGE, WC_EDITW, szEmpty, WS_CHILD
1259                                 | WS_VISIBLE | ES_LEFT | SS_NOTIFY, 0, 0, 0, 0,
1260                                hwndContainer, NULL, hhctrl_hinstance, NULL);
1261     if(!hwndEdit) {
1262         ERR("Could not create search ListView control.\n");
1263         return FALSE;
1264     }
1265     if(SendMessageW(hwndEdit, WM_SETFONT, (WPARAM) info->hFont, (LPARAM) FALSE) == -1)
1266     {
1267         ERR("Could not set font for edit control.\n");
1268         return FALSE;
1269     }
1270     editWndProc = (WNDPROC) SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC, (LONG_PTR)EditChild_WndProc);
1271     if(!editWndProc) {
1272         ERR("Could not redirect messages for edit control.\n");
1273         return FALSE;
1274     }
1275     SetWindowLongPtrW(hwndEdit, GWLP_USERDATA, (LONG_PTR)editWndProc);
1276     hwndList = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW, szEmpty,
1277                                WS_CHILD | WS_VISIBLE | WS_BORDER | LVS_SINGLESEL
1278                                 | LVS_REPORT | LVS_NOCOLUMNHEADER, 0, 0, 0, 0,
1279                                hwndContainer, NULL, hhctrl_hinstance, NULL);
1280     if(!hwndList) {
1281         ERR("Could not create search ListView control.\n");
1282         return FALSE;
1283     }
1284     memset(&lvc, 0, sizeof(lvc));
1285     lvc.mask = LVCF_TEXT;
1286     lvc.pszText = hidden_column;
1287     if(SendMessageW(hwndList, LVM_INSERTCOLUMNA, 0, (LPARAM) &lvc) == -1)
1288     {
1289         ERR("Could not create ListView column\n");
1290         return FALSE;
1291     }
1292
1293     info->search.hwndEdit = hwndEdit;
1294     info->search.hwndList = hwndList;
1295     info->search.hwndContainer = hwndContainer;
1296     info->tabs[TAB_SEARCH].hwnd = hwndContainer;
1297
1298     SetWindowLongPtrW(hwndContainer, 0, (LONG_PTR)info);
1299
1300     ResizeTabChild(info, TAB_SEARCH);
1301
1302     return TRUE;
1303 }
1304
1305 /* The Index tab's sub-topic popup */
1306
1307 static void ResizePopupChild(HHInfo *info)
1308 {
1309     int scroll_width = GetSystemMetrics(SM_CXVSCROLL);
1310     int border_width = GetSystemMetrics(SM_CXBORDER);
1311     int edge_width = GetSystemMetrics(SM_CXEDGE);
1312     INT width, height;
1313     RECT rect;
1314
1315     if(!info)
1316         return;
1317
1318     GetClientRect(info->popup.hwndPopup, &rect);
1319     SetWindowPos(info->popup.hwndCallback, HWND_TOP, 0, 0,
1320                  rect.right, rect.bottom, SWP_NOMOVE);
1321
1322     rect.left = TAB_MARGIN;
1323     rect.top = TAB_TOP_PADDING + TAB_MARGIN;
1324     rect.right -= TAB_RIGHT_PADDING + TAB_MARGIN;
1325     rect.bottom -= TAB_MARGIN;
1326     width = rect.right-rect.left;
1327     height = rect.bottom-rect.top;
1328
1329     SetWindowPos(info->popup.hwndList, NULL, rect.left, rect.top, width, height,
1330                  SWP_NOZORDER | SWP_NOACTIVATE);
1331
1332     SendMessageW(info->popup.hwndList, LVM_SETCOLUMNWIDTH, 0,
1333                  width-scroll_width-2*border_width-2*edge_width);
1334 }
1335
1336 static LRESULT CALLBACK HelpPopup_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1337 {
1338     HHInfo *info = (HHInfo *)GetWindowLongPtrW(hWnd, 0);
1339
1340     switch (message)
1341     {
1342     case WM_SIZE:
1343         ResizePopupChild(info);
1344         return 0;
1345     case WM_DESTROY:
1346         DestroyWindow(hWnd);
1347         return 0;
1348     case WM_CLOSE:
1349         ShowWindow(hWnd, SW_HIDE);
1350         return 0;
1351
1352     default:
1353         return DefWindowProcW(hWnd, message, wParam, lParam);
1354     }
1355
1356     return 0;
1357 }
1358
1359 static LRESULT CALLBACK PopupChild_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1360 {
1361     switch (message)
1362     {
1363     case WM_NOTIFY: {
1364         NMHDR *nmhdr = (NMHDR*)lParam;
1365         switch(nmhdr->code)
1366         {
1367         case NM_DBLCLK: {
1368             HHInfo *info = (HHInfo*)GetWindowLongPtrW(hWnd, 0);
1369             IndexSubItem *iter;
1370
1371             if(info == 0 || lParam == 0)
1372                 return 0;
1373             iter = (IndexSubItem*) ((NMITEMACTIVATE *)lParam)->lParam;
1374             if(iter == 0)
1375                 return 0;
1376             NavigateToChm(info, info->index->merge.chm_file, iter->local);
1377             ShowWindow(info->popup.hwndPopup, SW_HIDE);
1378             return 0;
1379         }
1380         case NM_RETURN: {
1381             HHInfo *info = (HHInfo*)GetWindowLongPtrW(hWnd, 0);
1382             IndexSubItem *iter;
1383             LVITEMW lvItem;
1384
1385             if(info == 0)
1386                 return 0;
1387
1388             lvItem.iItem = (int) SendMessageW(info->popup.hwndList, LVM_GETSELECTIONMARK, 0, 0);
1389             lvItem.mask = TVIF_PARAM;
1390             SendMessageW(info->popup.hwndList, LVM_GETITEMW, 0, (LPARAM)&lvItem);
1391             iter = (IndexSubItem*) lvItem.lParam;
1392             NavigateToChm(info, info->index->merge.chm_file, iter->local);
1393             ShowWindow(info->popup.hwndPopup, SW_HIDE);
1394             return 0;
1395         }
1396         }
1397         break;
1398     }
1399     default:
1400         return DefWindowProcW(hWnd, message, wParam, lParam);
1401     }
1402
1403     return 0;
1404 }
1405
1406 static BOOL AddIndexPopup(HHInfo *info)
1407 {
1408     static const WCHAR szPopupChildClass[] = {'H','H',' ','P','o','p','u','p',' ','C','h','i','l','d',0};
1409     static const WCHAR windowCaptionW[] = {'S','e','l','e','c','t',' ','T','o','p','i','c',':',0};
1410     static const WCHAR windowClassW[] = {'H','H',' ','P','o','p','u','p',0};
1411     HWND hwndList, hwndPopup, hwndCallback;
1412     char hidden_column[] = "Column";
1413     WNDCLASSEXW wcex;
1414     LVCOLUMNA lvc;
1415
1416     if(info->tabs[TAB_INDEX].id == -1)
1417         return TRUE; /* No "Index" tab */
1418
1419     wcex.cbSize         = sizeof(WNDCLASSEXW);
1420     wcex.style          = CS_HREDRAW | CS_VREDRAW;
1421     wcex.lpfnWndProc    = HelpPopup_WndProc;
1422     wcex.cbClsExtra     = 0;
1423     wcex.cbWndExtra     = sizeof(LONG_PTR);
1424     wcex.hInstance      = hhctrl_hinstance;
1425     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1426     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
1427     wcex.hbrBackground  = (HBRUSH)(COLOR_MENU + 1);
1428     wcex.lpszMenuName   = NULL;
1429     wcex.lpszClassName  = windowClassW;
1430     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1431     RegisterClassExW(&wcex);
1432
1433     wcex.cbSize         = sizeof(WNDCLASSEXW);
1434     wcex.style          = 0;
1435     wcex.lpfnWndProc    = PopupChild_WndProc;
1436     wcex.cbClsExtra     = 0;
1437     wcex.cbWndExtra     = sizeof(LONG_PTR);
1438     wcex.hInstance      = hhctrl_hinstance;
1439     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1440     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
1441     wcex.hbrBackground  = (HBRUSH)(COLOR_BTNFACE + 1);
1442     wcex.lpszMenuName   = NULL;
1443     wcex.lpszClassName  = szPopupChildClass;
1444     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1445     RegisterClassExW(&wcex);
1446
1447     hwndPopup = CreateWindowExW(WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_APPWINDOW
1448                                  | WS_EX_WINDOWEDGE | WS_EX_RIGHTSCROLLBAR,
1449                                 windowClassW, windowCaptionW, WS_POPUPWINDOW
1450                                  | WS_OVERLAPPEDWINDOW | WS_VISIBLE
1451                                  | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, CW_USEDEFAULT,
1452                                 CW_USEDEFAULT, 300, 200, info->WinType.hwndHelp,
1453                                 NULL, hhctrl_hinstance, NULL);
1454     if (!hwndPopup)
1455         return FALSE;
1456
1457     hwndCallback = CreateWindowExW(WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR,
1458                                    szPopupChildClass, szEmpty, WS_CHILDWINDOW | WS_VISIBLE,
1459                                    0, 0, 0, 0,
1460                                    hwndPopup, NULL, hhctrl_hinstance, NULL);
1461     if (!hwndCallback)
1462         return FALSE;
1463
1464     ShowWindow(hwndPopup, SW_HIDE);
1465     hwndList = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW, szEmpty,
1466                                WS_CHILD | WS_BORDER | LVS_SINGLESEL | LVS_REPORT
1467                                 | LVS_NOCOLUMNHEADER, 50, 50, 100, 100,
1468                                hwndCallback, NULL, hhctrl_hinstance, NULL);
1469     if(!hwndList) {
1470         ERR("Could not create popup ListView control\n");
1471         return FALSE;
1472     }
1473     memset(&lvc, 0, sizeof(lvc));
1474     lvc.mask = LVCF_TEXT;
1475     lvc.pszText = hidden_column;
1476     if(SendMessageW(hwndList, LVM_INSERTCOLUMNA, 0, (LPARAM) &lvc) == -1)
1477     {
1478         ERR("Could not create popup ListView column\n");
1479         return FALSE;
1480     }
1481
1482     info->popup.hwndCallback = hwndCallback;
1483     info->popup.hwndPopup = hwndPopup;
1484     info->popup.hwndList = hwndList;
1485     SetWindowLongPtrW(hwndPopup, 0, (LONG_PTR)info);
1486     SetWindowLongPtrW(hwndCallback, 0, (LONG_PTR)info);
1487
1488     ResizePopupChild(info);
1489     ShowWindow(hwndList, SW_SHOW);
1490
1491     return TRUE;
1492 }
1493
1494 /* Viewer Window */
1495
1496 static void ExpandContract(HHInfo *pHHInfo)
1497 {
1498     RECT r, nav;
1499
1500     pHHInfo->WinType.fNotExpanded = !pHHInfo->WinType.fNotExpanded;
1501     GetWindowRect(pHHInfo->WinType.hwndHelp, &r);
1502     NP_GetNavigationRect(pHHInfo, &nav);
1503
1504     /* hide/show both the nav bar and the size bar */
1505     if (pHHInfo->WinType.fNotExpanded)
1506     {
1507         ShowWindow(pHHInfo->WinType.hwndNavigation, SW_HIDE);
1508         ShowWindow(pHHInfo->hwndSizeBar, SW_HIDE);
1509         r.left = r.left + nav.right;
1510
1511         SendMessageW(pHHInfo->WinType.hwndToolBar, TB_HIDEBUTTON, IDTB_EXPAND, MAKELPARAM(FALSE, 0));
1512         SendMessageW(pHHInfo->WinType.hwndToolBar, TB_HIDEBUTTON, IDTB_CONTRACT, MAKELPARAM(TRUE, 0));
1513     }
1514     else
1515     {
1516         ShowWindow(pHHInfo->WinType.hwndNavigation, SW_SHOW);
1517         ShowWindow(pHHInfo->hwndSizeBar, SW_SHOW);
1518         r.left = r.left - nav.right;
1519
1520         SendMessageW(pHHInfo->WinType.hwndToolBar, TB_HIDEBUTTON, IDTB_EXPAND, MAKELPARAM(TRUE, 0));
1521         SendMessageW(pHHInfo->WinType.hwndToolBar, TB_HIDEBUTTON, IDTB_CONTRACT, MAKELPARAM(FALSE, 0));
1522     }
1523
1524     MoveWindow(pHHInfo->WinType.hwndHelp, r.left, r.top, r.right-r.left, r.bottom-r.top, TRUE);
1525 }
1526
1527 static LRESULT Help_OnSize(HWND hWnd)
1528 {
1529     HHInfo *pHHInfo = (HHInfo *)GetWindowLongPtrW(hWnd, 0);
1530     DWORD dwSize;
1531     RECT rc;
1532
1533     if (!pHHInfo)
1534         return 0;
1535
1536     if (!pHHInfo->WinType.fNotExpanded)
1537     {
1538         NP_GetNavigationRect(pHHInfo, &rc);
1539         SetWindowPos(pHHInfo->WinType.hwndNavigation, HWND_TOP, 0, 0,
1540                      rc.right, rc.bottom, SWP_NOMOVE);
1541
1542         SB_GetSizeBarRect(pHHInfo, &rc);
1543         SetWindowPos(pHHInfo->hwndSizeBar, HWND_TOP, rc.left, rc.top,
1544                      rc.right, rc.bottom, SWP_SHOWWINDOW);
1545
1546     }
1547
1548     HP_GetHTMLRect(pHHInfo, &rc);
1549     SetWindowPos(pHHInfo->WinType.hwndHTML, HWND_TOP, rc.left, rc.top,
1550                  rc.right, rc.bottom, SWP_SHOWWINDOW);
1551
1552     /* Resize browser window taking the frame size into account */
1553     dwSize = GetSystemMetrics(SM_CXFRAME);
1554     ResizeWebBrowser(pHHInfo, rc.right - dwSize, rc.bottom - dwSize);
1555
1556     return 0;
1557 }
1558
1559 static LRESULT CALLBACK Help_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1560 {
1561     switch (message)
1562     {
1563     case WM_COMMAND:
1564         if (HIWORD(wParam) == BN_CLICKED)
1565             TB_OnClick(hWnd, LOWORD(wParam));
1566         break;
1567     case WM_SIZE:
1568         return Help_OnSize(hWnd);
1569     case WM_CLOSE:
1570         ReleaseHelpViewer((HHInfo *)GetWindowLongPtrW(hWnd, 0));
1571         return 0;
1572     case WM_DESTROY:
1573         if(hh_process)
1574             PostQuitMessage(0);
1575         break;
1576
1577     default:
1578         return DefWindowProcW(hWnd, message, wParam, lParam);
1579     }
1580
1581     return 0;
1582 }
1583
1584 static BOOL HH_CreateHelpWindow(HHInfo *info)
1585 {
1586     HWND hWnd;
1587     RECT winPos = info->WinType.rcWindowPos;
1588     WNDCLASSEXW wcex;
1589     DWORD dwStyles, dwExStyles;
1590     DWORD x, y, width = 0, height = 0;
1591     LPCWSTR caption;
1592
1593     static const WCHAR windowClassW[] = {
1594         'H','H',' ', 'P','a','r','e','n','t',0
1595     };
1596
1597     wcex.cbSize         = sizeof(WNDCLASSEXW);
1598     wcex.style          = CS_HREDRAW | CS_VREDRAW;
1599     wcex.lpfnWndProc    = Help_WndProc;
1600     wcex.cbClsExtra     = 0;
1601     wcex.cbWndExtra     = sizeof(LONG_PTR);
1602     wcex.hInstance      = hhctrl_hinstance;
1603     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1604     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
1605     wcex.hbrBackground  = (HBRUSH)(COLOR_MENU + 1);
1606     wcex.lpszMenuName   = NULL;
1607     wcex.lpszClassName  = windowClassW;
1608     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1609
1610     RegisterClassExW(&wcex);
1611
1612     /* Read in window parameters if available */
1613     if (info->WinType.fsValidMembers & HHWIN_PARAM_STYLES)
1614         dwStyles = info->WinType.dwStyles | WS_OVERLAPPEDWINDOW;
1615     else
1616         dwStyles = WS_OVERLAPPEDWINDOW | WS_VISIBLE |
1617                    WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
1618
1619     if (info->WinType.fsValidMembers & HHWIN_PARAM_EXSTYLES)
1620         dwExStyles = info->WinType.dwExStyles;
1621     else
1622         dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_APPWINDOW |
1623                      WS_EX_WINDOWEDGE | WS_EX_RIGHTSCROLLBAR;
1624
1625     if (info->WinType.fsValidMembers & HHWIN_PARAM_RECT)
1626     {
1627         x = winPos.left;
1628         y = winPos.top;
1629         width = winPos.right - x;
1630         height = winPos.bottom - y;
1631     }
1632     if (!width || !height)
1633     {
1634         x = WINTYPE_DEFAULT_X;
1635         y = WINTYPE_DEFAULT_Y;
1636         width = WINTYPE_DEFAULT_WIDTH;
1637         height = WINTYPE_DEFAULT_HEIGHT;
1638     }
1639
1640     if (info->WinType.fNotExpanded)
1641     {
1642         if (!(info->WinType.fsValidMembers & HHWIN_PARAM_NAV_WIDTH) &&
1643               info->WinType.iNavWidth == 0)
1644         {
1645             info->WinType.iNavWidth = WINTYPE_DEFAULT_NAVWIDTH;
1646         }
1647
1648         x += info->WinType.iNavWidth;
1649         width -= info->WinType.iNavWidth;
1650     }
1651
1652
1653     caption = info->WinType.pszCaption;
1654     if (!*caption) caption = info->pCHMInfo->defTitle;
1655
1656     hWnd = CreateWindowExW(dwExStyles, windowClassW, caption,
1657                            dwStyles, x, y, width, height, NULL, NULL, hhctrl_hinstance, NULL);
1658     if (!hWnd)
1659         return FALSE;
1660
1661     ShowWindow(hWnd, SW_SHOW);
1662     UpdateWindow(hWnd);
1663
1664     /* store the pointer to the HH info struct */
1665     SetWindowLongPtrW(hWnd, 0, (LONG_PTR)info);
1666
1667     info->WinType.hwndHelp = hWnd;
1668     return TRUE;
1669 }
1670
1671 static void HH_CreateFont(HHInfo *pHHInfo)
1672 {
1673     LOGFONTW lf;
1674
1675     GetObjectW(GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONTW), &lf);
1676     lf.lfWeight = FW_NORMAL;
1677     lf.lfItalic = FALSE;
1678     lf.lfUnderline = FALSE;
1679
1680     pHHInfo->hFont = CreateFontIndirectW(&lf);
1681 }
1682
1683 static void HH_InitRequiredControls(DWORD dwControls)
1684 {
1685     INITCOMMONCONTROLSEX icex;
1686
1687     icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
1688     icex.dwICC = dwControls;
1689     InitCommonControlsEx(&icex);
1690 }
1691
1692 /* Creates the whole package */
1693 static BOOL CreateViewer(HHInfo *pHHInfo)
1694 {
1695     HH_CreateFont(pHHInfo);
1696
1697     if (!HH_CreateHelpWindow(pHHInfo))
1698         return FALSE;
1699
1700     HH_InitRequiredControls(ICC_BAR_CLASSES);
1701
1702     if (!HH_AddToolbar(pHHInfo))
1703         return FALSE;
1704
1705     HH_RegisterChildWndClass(pHHInfo);
1706
1707     if (!HH_AddNavigationPane(pHHInfo))
1708         return FALSE;
1709
1710     HH_RegisterSizeBarClass(pHHInfo);
1711
1712     if (!HH_AddSizeBar(pHHInfo))
1713         return FALSE;
1714
1715     if (!HH_AddHTMLPane(pHHInfo))
1716         return FALSE;
1717
1718     if (!AddContentTab(pHHInfo))
1719         return FALSE;
1720
1721     if (!AddIndexTab(pHHInfo))
1722         return FALSE;
1723
1724     if (!AddIndexPopup(pHHInfo))
1725         return FALSE;
1726
1727     if (!AddSearchTab(pHHInfo))
1728         return FALSE;
1729
1730     InitContent(pHHInfo);
1731     InitIndex(pHHInfo);
1732
1733     return TRUE;
1734 }
1735
1736 void ReleaseHelpViewer(HHInfo *info)
1737 {
1738     TRACE("(%p)\n", info);
1739
1740     if (!info)
1741         return;
1742
1743     /* Free allocated strings */
1744     heap_free(info->pszType);
1745     heap_free(info->pszCaption);
1746     heap_free(info->pszToc);
1747     heap_free(info->pszIndex);
1748     heap_free(info->pszFile);
1749     heap_free(info->pszHome);
1750     heap_free(info->pszJump1);
1751     heap_free(info->pszJump2);
1752     heap_free(info->pszUrlJump1);
1753     heap_free(info->pszUrlJump2);
1754
1755     if (info->pCHMInfo)
1756         CloseCHM(info->pCHMInfo);
1757
1758     ReleaseWebBrowser(info);
1759     ReleaseContent(info);
1760     ReleaseIndex(info);
1761     ReleaseSearch(info);
1762
1763     if(info->contents.hImageList)
1764         ImageList_Destroy(info->contents.hImageList);
1765     if(info->WinType.hwndHelp)
1766         DestroyWindow(info->WinType.hwndHelp);
1767
1768     heap_free(info);
1769     OleUninitialize();
1770 }
1771
1772 HHInfo *CreateHelpViewer(LPCWSTR filename)
1773 {
1774     HHInfo *info = heap_alloc_zero(sizeof(HHInfo));
1775     int i;
1776
1777     /* Set the invalid tab ID (-1) as the default value for all
1778      * of the tabs, this matches a failed TCM_INSERTITEM call.
1779      */
1780     for(i=0;i<sizeof(info->tabs)/sizeof(HHTab);i++)
1781         info->tabs[i].id = -1;
1782
1783     OleInitialize(NULL);
1784
1785     info->pCHMInfo = OpenCHM(filename);
1786     if(!info->pCHMInfo) {
1787         ReleaseHelpViewer(info);
1788         return NULL;
1789     }
1790
1791     if (!LoadWinTypeFromCHM(info)) {
1792         ReleaseHelpViewer(info);
1793         return NULL;
1794     }
1795
1796     if(!CreateViewer(info)) {
1797         ReleaseHelpViewer(info);
1798         return NULL;
1799     }
1800
1801     return info;
1802 }
1803
1804 /*
1805  * Search the table of HTML entities and return the corresponding ANSI symbol.
1806  */
1807 static char find_html_symbol(const char *entity, int entity_len)
1808 {
1809     int max = sizeof(html_encoded_symbols)/sizeof(html_encoded_symbols[0])-1;
1810     int min = 0, dir;
1811
1812     while(min <= max)
1813     {
1814         int pos = (min+max)/2;
1815         const char *encoded_symbol = html_encoded_symbols[pos].html_code;
1816         dir = strncmp(encoded_symbol, entity, entity_len);
1817         if(dir == 0 && !encoded_symbol[entity_len]) return html_encoded_symbols[pos].ansi_symbol;
1818         if(dir < 0)
1819             min = pos+1;
1820         else
1821             max = pos-1;
1822     }
1823     return 0;
1824 }
1825
1826 /*
1827  * Decode a string containing HTML encoded characters into a unicode string.
1828  */
1829 WCHAR *decode_html(const char *html_fragment, int html_fragment_len, UINT code_page)
1830 {
1831     const char *h = html_fragment;
1832     char *amp, *sem, symbol, *tmp;
1833     int len, tmp_len = 0;
1834     WCHAR *unicode_text;
1835
1836     tmp = heap_alloc(html_fragment_len+1);
1837     while(1)
1838     {
1839         symbol = 0;
1840         amp = strchr(h, '&');
1841         if(!amp) break;
1842         len = amp-h;
1843         /* Copy the characters prior to the HTML encoded character */
1844         memcpy(&tmp[tmp_len], h, len);
1845         tmp_len += len;
1846         amp++; /* skip ampersand */
1847         sem = strchr(amp, ';');
1848         /* Require a semicolon after the ampersand */
1849         if(!sem)
1850         {
1851             h = amp;
1852             tmp[tmp_len++] = '&';
1853             continue;
1854         }
1855         /* Find the symbol either by using the ANSI character number (prefixed by the pound symbol)
1856          * or by searching the HTML entity table */
1857         len = sem-amp;
1858         if(amp[0] == '#')
1859         {
1860             char *endnum = NULL;
1861             int tmp;
1862
1863             tmp = (char) strtol(amp, &endnum, 10);
1864             if(endnum == sem)
1865                 symbol = tmp;
1866         }
1867         else
1868             symbol = find_html_symbol(amp, len);
1869         if(!symbol)
1870         {
1871             FIXME("Failed to translate HTML encoded character '&%.*s;'.\n", len, amp);
1872             h = amp;
1873             tmp[tmp_len++] = '&';
1874             continue;
1875         }
1876         /* Insert the new symbol */
1877         h = sem+1;
1878         tmp[tmp_len++] = symbol;
1879     }
1880     /* Convert any remaining characters */
1881     len = html_fragment_len-(h-html_fragment);
1882     memcpy(&tmp[tmp_len], h, len);
1883     tmp_len += len;
1884     tmp[tmp_len++] = 0; /* NULL-terminate the string */
1885
1886     len = MultiByteToWideChar(code_page, 0, tmp, tmp_len, NULL, 0);
1887     unicode_text = heap_alloc(len*sizeof(WCHAR));
1888     MultiByteToWideChar(code_page, 0, tmp, tmp_len, unicode_text, len);
1889     heap_free(tmp);
1890     return unicode_text;
1891 }