mshtml: Share nsobject reference with nsnode.
[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                     name = item->name;
639                 memset(&lvi, 0, sizeof(lvi));
640                 lvi.iItem = i;
641                 lvi.mask = LVIF_TEXT|LVIF_PARAM;
642                 lvi.cchTextMax = strlenW(name)+1;
643                 lvi.pszText = name;
644                 lvi.lParam = (LPARAM) item;
645                 SendMessageW(info->popup.hwndList, LVM_INSERTITEMW, 0, (LPARAM)&lvi);
646             }
647             ShowWindow(info->popup.hwndPopup, SW_SHOW);
648             return 0;
649         }
650         name = iiter->items[0].name;
651         local = iiter->items[0].local;
652         chmfile = iiter->merge.chm_file;
653         break;
654     case TAB_SEARCH:
655         siter = (SearchItem *) user_data;
656         name = siter->filename;
657         local = siter->filename;
658         chmfile = info->pCHMInfo->szFile;
659         break;
660     default:
661         FIXME("Unhandled operation for this tab!\n");
662         return 0;
663     }
664
665     if(!chmfile)
666     {
667         FIXME("No help file found for this item!\n");
668         return 0;
669     }
670
671     TRACE("name %s loal %s\n", debugstr_w(name), debugstr_w(local));
672
673     NavigateToChm(info, chmfile, local);
674     return 0;
675 }
676
677 /* Capture the Enter/Return key and send it up to Child_WndProc as an NM_RETURN message */
678 static LRESULT CALLBACK EditChild_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
679 {
680     WNDPROC editWndProc = (WNDPROC)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
681
682     if(message == WM_KEYUP && wParam == VK_RETURN)
683     {
684         NMHDR nmhdr;
685
686         nmhdr.hwndFrom = hWnd;
687         nmhdr.code = NM_RETURN;
688         SendMessageW(GetParent(GetParent(hWnd)), WM_NOTIFY, wParam, (LPARAM)&nmhdr);
689     }
690     return editWndProc(hWnd, message, wParam, lParam);
691 }
692
693 static LRESULT CALLBACK Child_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
694 {
695     switch (message)
696     {
697     case WM_PAINT:
698         return Child_OnPaint(hWnd);
699     case WM_SIZE:
700         return Child_OnSize(hWnd);
701     case WM_NOTIFY: {
702         HHInfo *info = (HHInfo*)GetWindowLongPtrW(hWnd, 0);
703         NMHDR *nmhdr = (NMHDR*)lParam;
704
705         switch(nmhdr->code) {
706         case TCN_SELCHANGE:
707             return OnTabChange(hWnd);
708         case TVN_SELCHANGEDW:
709             return OnTopicChange(info, (void*)((NMTREEVIEWW *)lParam)->itemNew.lParam);
710         case TVN_ITEMEXPANDINGW: {
711             TVITEMW *item = &((NMTREEVIEWW *)lParam)->itemNew;
712             HWND hwndTreeView = info->tabs[TAB_CONTENTS].hwnd;
713
714             item->mask = TVIF_IMAGE|TVIF_SELECTEDIMAGE;
715             if (item->state & TVIS_EXPANDED)
716             {
717                 item->iImage = HHTV_FOLDER;
718                 item->iSelectedImage = HHTV_FOLDER;
719             }
720             else
721             {
722                 item->iImage = HHTV_OPENFOLDER;
723                 item->iSelectedImage = HHTV_OPENFOLDER;
724             }
725             SendMessageW(hwndTreeView, TVM_SETITEMW, 0, (LPARAM)item);
726             return 0;
727         }
728         case NM_DBLCLK:
729             if(!info)
730                 return 0;
731             switch(info->current_tab)
732             {
733             case TAB_INDEX:
734                 return OnTopicChange(info, (void*)((NMITEMACTIVATE *)lParam)->lParam);
735             case TAB_SEARCH:
736                 return OnTopicChange(info, (void*)((NMITEMACTIVATE *)lParam)->lParam);
737             }
738             break;
739         case NM_RETURN:
740             if(!info)
741                 return 0;
742             switch(info->current_tab) {
743             case TAB_INDEX: {
744                 HWND hwndList = info->tabs[TAB_INDEX].hwnd;
745                 LVITEMW lvItem;
746
747                 lvItem.iItem = (int) SendMessageW(hwndList, LVM_GETSELECTIONMARK, 0, 0);
748                 lvItem.mask = TVIF_PARAM;
749                 SendMessageW(hwndList, LVM_GETITEMW, 0, (LPARAM)&lvItem);
750                 OnTopicChange(info, (void*) lvItem.lParam);
751                 return 0;
752             }
753             case TAB_SEARCH: {
754                 if(nmhdr->hwndFrom == info->search.hwndEdit) {
755                     char needle[100];
756                     DWORD i, len;
757
758                     len = GetWindowTextA(info->search.hwndEdit, needle, sizeof(needle));
759                     if(!len)
760                     {
761                         FIXME("Unable to get search text.\n");
762                         return 0;
763                     }
764                     /* Convert the requested text for comparison later against the
765                      * lower case version of HTML file contents.
766                      */
767                     for(i=0;i<len;i++)
768                         needle[i] = tolower(needle[i]);
769                     InitSearch(info, needle);
770                     return 0;
771                 }else if(nmhdr->hwndFrom == info->search.hwndList) {
772                     HWND hwndList = info->search.hwndList;
773                     LVITEMW lvItem;
774
775                     lvItem.iItem = (int) SendMessageW(hwndList, LVM_GETSELECTIONMARK, 0, 0);
776                     lvItem.mask = TVIF_PARAM;
777                     SendMessageW(hwndList, LVM_GETITEMW, 0, (LPARAM)&lvItem);
778                     OnTopicChange(info, (void*) lvItem.lParam);
779                     return 0;
780                 }
781                 break;
782             }
783             }
784             break;
785         }
786         break;
787     }
788     default:
789         return DefWindowProcW(hWnd, message, wParam, lParam);
790     }
791
792     return 0;
793 }
794
795 static void HH_RegisterChildWndClass(HHInfo *pHHInfo)
796 {
797     WNDCLASSEXW wcex;
798
799     wcex.cbSize         = sizeof(WNDCLASSEXW);
800     wcex.style          = 0;
801     wcex.lpfnWndProc    = Child_WndProc;
802     wcex.cbClsExtra     = 0;
803     wcex.cbWndExtra     = sizeof(LONG_PTR);
804     wcex.hInstance      = hhctrl_hinstance;
805     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
806     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
807     wcex.hbrBackground  = (HBRUSH)(COLOR_BTNFACE + 1);
808     wcex.lpszMenuName   = NULL;
809     wcex.lpszClassName  = szChildClass;
810     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
811
812     RegisterClassExW(&wcex);
813 }
814
815 /* Toolbar */
816
817 #define ICON_SIZE   20
818
819 static void DisplayPopupMenu(HHInfo *info)
820 {
821     HMENU menu, submenu;
822     TBBUTTONINFOW button;
823     MENUITEMINFOW item;
824     POINT coords;
825     RECT rect;
826     DWORD index;
827
828     menu = LoadMenuW(hhctrl_hinstance, MAKEINTRESOURCEW(MENU_POPUP));
829
830     if (!menu)
831         return;
832
833     submenu = GetSubMenu(menu, 0);
834
835     /* Update the Show/Hide menu item */
836     item.cbSize = sizeof(MENUITEMINFOW);
837     item.fMask = MIIM_FTYPE | MIIM_STATE | MIIM_STRING;
838     item.fType = MFT_STRING;
839     item.fState = MF_ENABLED;
840
841     if (info->WinType.fNotExpanded)
842         item.dwTypeData = HH_LoadString(IDS_SHOWTABS);
843     else
844         item.dwTypeData = HH_LoadString(IDS_HIDETABS);
845
846     SetMenuItemInfoW(submenu, IDTB_EXPAND, FALSE, &item);
847     heap_free(item.dwTypeData);
848
849     /* Find the index toolbar button */
850     button.cbSize = sizeof(TBBUTTONINFOW);
851     button.dwMask = TBIF_COMMAND;
852     index = SendMessageW(info->WinType.hwndToolBar, TB_GETBUTTONINFOW, IDTB_OPTIONS, (LPARAM) &button);
853
854     if (index == -1)
855        return;
856
857     /* Get position */
858     SendMessageW(info->WinType.hwndToolBar, TB_GETITEMRECT, index, (LPARAM) &rect);
859
860     coords.x = rect.left;
861     coords.y = rect.bottom;
862
863     ClientToScreen(info->WinType.hwndToolBar, &coords);
864     TrackPopupMenu(submenu, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_LEFTBUTTON | TPM_NOANIMATION, coords.x, coords.y, 0, info->WinType.hwndHelp, NULL);
865 }
866
867 static void TB_OnClick(HWND hWnd, DWORD dwID)
868 {
869     HHInfo *info = (HHInfo *)GetWindowLongPtrW(hWnd, 0);
870
871     switch (dwID)
872     {
873         case IDTB_STOP:
874             DoPageAction(info, WB_STOP);
875             break;
876         case IDTB_REFRESH:
877             DoPageAction(info, WB_REFRESH);
878             break;
879         case IDTB_BACK:
880             DoPageAction(info, WB_GOBACK);
881             break;
882         case IDTB_HOME:
883             NavigateToChm(info, info->pCHMInfo->szFile, info->WinType.pszHome);
884             break;
885         case IDTB_FORWARD:
886             DoPageAction(info, WB_GOFORWARD);
887             break;
888         case IDTB_PRINT:
889             DoPageAction(info, WB_PRINT);
890             break;
891         case IDTB_EXPAND:
892         case IDTB_CONTRACT:
893             ExpandContract(info);
894             break;
895         case IDTB_SYNC:
896             DoSync(info);
897             break;
898         case IDTB_OPTIONS:
899             DisplayPopupMenu(info);
900             break;
901         case IDTB_NOTES:
902         case IDTB_CONTENTS:
903         case IDTB_INDEX:
904         case IDTB_SEARCH:
905         case IDTB_HISTORY:
906         case IDTB_FAVORITES:
907             /* These are officially unimplemented as of the Windows 7 SDK */
908             break;
909         case IDTB_BROWSE_FWD:
910         case IDTB_BROWSE_BACK:
911         case IDTB_JUMP1:
912         case IDTB_JUMP2:
913         case IDTB_CUSTOMIZE:
914         case IDTB_ZOOM:
915         case IDTB_TOC_NEXT:
916         case IDTB_TOC_PREV:
917             break;
918     }
919 }
920
921 static void TB_AddButton(TBBUTTON *pButtons, DWORD dwIndex, DWORD dwID, DWORD dwBitmap)
922 {
923     pButtons[dwIndex].iBitmap = dwBitmap;
924     pButtons[dwIndex].idCommand = dwID;
925     pButtons[dwIndex].fsState = TBSTATE_ENABLED;
926     pButtons[dwIndex].fsStyle = BTNS_BUTTON;
927     pButtons[dwIndex].dwData = 0;
928     pButtons[dwIndex].iString = 0;
929 }
930
931 static void TB_AddButtonsFromFlags(HHInfo *pHHInfo, TBBUTTON *pButtons, DWORD dwButtonFlags, LPDWORD pdwNumButtons)
932 {
933     int nHistBitmaps = 0, nStdBitmaps = 0, nHHBitmaps = 0;
934     HWND hToolbar = pHHInfo->WinType.hwndToolBar;
935     TBADDBITMAP tbAB;
936     DWORD unsupported;
937
938     /* Common bitmaps */
939     tbAB.hInst = HINST_COMMCTRL;
940     tbAB.nID = IDB_HIST_LARGE_COLOR;
941     nHistBitmaps = SendMessageW(hToolbar, TB_ADDBITMAP, 0, (LPARAM)&tbAB);
942     tbAB.nID = IDB_STD_LARGE_COLOR;
943     nStdBitmaps = SendMessageW(hToolbar, TB_ADDBITMAP, 0, (LPARAM)&tbAB);
944     /* hhctrl.ocx bitmaps */
945     tbAB.hInst = hhctrl_hinstance;
946     tbAB.nID = IDB_HHTOOLBAR;
947     nHHBitmaps = SendMessageW(hToolbar, TB_ADDBITMAP, HHTB_NUMBITMAPS, (LPARAM)&tbAB);
948
949     *pdwNumButtons = 0;
950
951     unsupported = dwButtonFlags & (HHWIN_BUTTON_BROWSE_FWD |
952         HHWIN_BUTTON_BROWSE_BCK | HHWIN_BUTTON_NOTES | HHWIN_BUTTON_CONTENTS |
953         HHWIN_BUTTON_INDEX | HHWIN_BUTTON_SEARCH | HHWIN_BUTTON_HISTORY |
954         HHWIN_BUTTON_FAVORITES | HHWIN_BUTTON_JUMP1 | HHWIN_BUTTON_JUMP2 |
955         HHWIN_BUTTON_ZOOM | HHWIN_BUTTON_TOC_NEXT | HHWIN_BUTTON_TOC_PREV);
956     if (unsupported)
957         FIXME("got asked for unsupported buttons: %06x\n", unsupported);
958
959     if (dwButtonFlags & HHWIN_BUTTON_EXPAND)
960     {
961         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_EXPAND, nHHBitmaps + HHTB_EXPAND);
962         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_CONTRACT, nHHBitmaps + HHTB_CONTRACT);
963
964         if (pHHInfo->WinType.fNotExpanded)
965             pButtons[1].fsState |= TBSTATE_HIDDEN;
966         else
967             pButtons[0].fsState |= TBSTATE_HIDDEN;
968     }
969
970     if (dwButtonFlags & HHWIN_BUTTON_BACK)
971         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_BACK, nHistBitmaps + HIST_BACK);
972
973     if (dwButtonFlags & HHWIN_BUTTON_FORWARD)
974         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_FORWARD, nHistBitmaps + HIST_FORWARD);
975
976     if (dwButtonFlags & HHWIN_BUTTON_STOP)
977         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_STOP, nHHBitmaps + HHTB_STOP);
978
979     if (dwButtonFlags & HHWIN_BUTTON_REFRESH)
980         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_REFRESH, nHHBitmaps + HHTB_REFRESH);
981
982     if (dwButtonFlags & HHWIN_BUTTON_HOME)
983         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_HOME, nHHBitmaps + HHTB_HOME);
984
985     if (dwButtonFlags & HHWIN_BUTTON_SYNC)
986         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_SYNC, nHHBitmaps + HHTB_SYNC);
987
988     if (dwButtonFlags & HHWIN_BUTTON_OPTIONS)
989         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_OPTIONS, nStdBitmaps + STD_PROPERTIES);
990
991     if (dwButtonFlags & HHWIN_BUTTON_PRINT)
992         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_PRINT, nStdBitmaps + STD_PRINT);
993 }
994
995 static BOOL HH_AddToolbar(HHInfo *pHHInfo)
996 {
997     HWND hToolbar;
998     HWND hwndParent = pHHInfo->WinType.hwndHelp;
999     DWORD toolbarFlags;
1000     TBBUTTON buttons[IDTB_TOC_PREV - IDTB_EXPAND];
1001     DWORD dwStyles, dwExStyles;
1002     DWORD dwNumButtons, dwIndex;
1003
1004     if (pHHInfo->WinType.fsWinProperties & HHWIN_PARAM_TB_FLAGS)
1005         toolbarFlags = pHHInfo->WinType.fsToolBarFlags;
1006     else
1007         toolbarFlags = HHWIN_DEF_BUTTONS;
1008
1009     dwStyles = WS_CHILDWINDOW | WS_VISIBLE | TBSTYLE_FLAT |
1010                TBSTYLE_WRAPABLE | TBSTYLE_TOOLTIPS | CCS_NODIVIDER;
1011     dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
1012
1013     hToolbar = CreateWindowExW(dwExStyles, TOOLBARCLASSNAMEW, NULL, dwStyles,
1014                                0, 0, 0, 0, hwndParent, NULL,
1015                                hhctrl_hinstance, NULL);
1016     if (!hToolbar)
1017         return FALSE;
1018     pHHInfo->WinType.hwndToolBar = hToolbar;
1019
1020     SendMessageW(hToolbar, TB_SETBITMAPSIZE, 0, MAKELONG(ICON_SIZE, ICON_SIZE));
1021     SendMessageW(hToolbar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
1022     SendMessageW(hToolbar, WM_SETFONT, (WPARAM)pHHInfo->hFont, TRUE);
1023
1024     TB_AddButtonsFromFlags(pHHInfo, buttons, toolbarFlags, &dwNumButtons);
1025
1026     for (dwIndex = 0; dwIndex < dwNumButtons; dwIndex++)
1027     {
1028         LPWSTR szBuf = HH_LoadString(buttons[dwIndex].idCommand);
1029         DWORD dwLen = strlenW(szBuf);
1030         szBuf[dwLen + 1] = 0; /* Double-null terminate */
1031
1032         buttons[dwIndex].iString = (DWORD)SendMessageW(hToolbar, TB_ADDSTRINGW, 0, (LPARAM)szBuf);
1033         heap_free(szBuf);
1034     }
1035
1036     SendMessageW(hToolbar, TB_ADDBUTTONSW, dwNumButtons, (LPARAM)buttons);
1037     SendMessageW(hToolbar, TB_AUTOSIZE, 0, 0);
1038     ShowWindow(hToolbar, SW_SHOW);
1039
1040     return TRUE;
1041 }
1042
1043 /* Navigation Pane */
1044
1045 static void NP_GetNavigationRect(HHInfo *pHHInfo, RECT *rc)
1046 {
1047     HWND hwndParent = pHHInfo->WinType.hwndHelp;
1048     HWND hwndToolbar = pHHInfo->WinType.hwndToolBar;
1049     RECT rectWND, rectTB;
1050
1051     GetClientRect(hwndParent, &rectWND);
1052     GetClientRect(hwndToolbar, &rectTB);
1053
1054     rc->left = 0;
1055     rc->top = rectTB.bottom;
1056     rc->bottom = rectWND.bottom - rectTB.bottom;
1057
1058     if (!(pHHInfo->WinType.fsValidMembers & HHWIN_PARAM_NAV_WIDTH) &&
1059           pHHInfo->WinType.iNavWidth == 0)
1060     {
1061         pHHInfo->WinType.iNavWidth = WINTYPE_DEFAULT_NAVWIDTH;
1062     }
1063
1064     rc->right = pHHInfo->WinType.iNavWidth;
1065 }
1066
1067 static DWORD NP_CreateTab(HINSTANCE hInstance, HWND hwndTabCtrl, DWORD index)
1068 {
1069     TCITEMW tie;
1070     LPWSTR tabText = HH_LoadString(index);
1071     DWORD ret;
1072
1073     tie.mask = TCIF_TEXT;
1074     tie.pszText = tabText;
1075
1076     ret = SendMessageW( hwndTabCtrl, TCM_INSERTITEMW, index, (LPARAM)&tie );
1077
1078     heap_free(tabText);
1079     return ret;
1080 }
1081
1082 static BOOL HH_AddNavigationPane(HHInfo *info)
1083 {
1084     HWND hWnd, hwndTabCtrl;
1085     HWND hwndParent = info->WinType.hwndHelp;
1086     DWORD dwStyles = WS_CHILDWINDOW;
1087     DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
1088     RECT rc;
1089
1090     if (!info->WinType.fNotExpanded)
1091         dwStyles |= WS_VISIBLE;
1092
1093     NP_GetNavigationRect(info, &rc);
1094
1095     hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
1096                            rc.left, rc.top, rc.right, rc.bottom,
1097                            hwndParent, NULL, hhctrl_hinstance, NULL);
1098     if (!hWnd)
1099         return FALSE;
1100
1101     SetWindowLongPtrW(hWnd, 0, (LONG_PTR)info);
1102
1103     hwndTabCtrl = CreateWindowExW(dwExStyles, WC_TABCONTROLW, szEmpty, dwStyles | WS_VISIBLE,
1104                                   0, TAB_TOP_PADDING,
1105                                   rc.right - TAB_RIGHT_PADDING,
1106                                   rc.bottom - TAB_TOP_PADDING,
1107                                   hWnd, NULL, hhctrl_hinstance, NULL);
1108     if (!hwndTabCtrl)
1109         return FALSE;
1110
1111     if (*info->WinType.pszToc)
1112         info->tabs[TAB_CONTENTS].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_CONTENTS);
1113
1114     if (*info->WinType.pszIndex)
1115         info->tabs[TAB_INDEX].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_INDEX);
1116
1117     if (info->WinType.fsWinProperties & HHWIN_PROP_TAB_SEARCH)
1118         info->tabs[TAB_SEARCH].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_SEARCH);
1119
1120     if (info->WinType.fsWinProperties & HHWIN_PROP_TAB_FAVORITES)
1121         info->tabs[TAB_FAVORITES].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_FAVORITES);
1122
1123     SendMessageW(hwndTabCtrl, WM_SETFONT, (WPARAM)info->hFont, TRUE);
1124
1125     info->hwndTabCtrl = hwndTabCtrl;
1126     info->WinType.hwndNavigation = hWnd;
1127     return TRUE;
1128 }
1129
1130 /* HTML Pane */
1131
1132 static void HP_GetHTMLRect(HHInfo *info, RECT *rc)
1133 {
1134     RECT rectTB, rectWND, rectNP, rectSB;
1135
1136     GetClientRect(info->WinType.hwndHelp, &rectWND);
1137     GetClientRect(info->WinType.hwndToolBar, &rectTB);
1138     GetClientRect(info->hwndSizeBar, &rectSB);
1139
1140     if (info->WinType.fNotExpanded)
1141         rc->left = 0;
1142     else
1143     {
1144         GetClientRect(info->WinType.hwndNavigation, &rectNP);
1145         rc->left = rectNP.right + rectSB.right;
1146     }
1147
1148     rc->top = rectTB.bottom;
1149     rc->right = rectWND.right - rc->left;
1150     rc->bottom = rectWND.bottom - rectTB.bottom;
1151 }
1152
1153 static BOOL HH_AddHTMLPane(HHInfo *pHHInfo)
1154 {
1155     HWND hWnd;
1156     HWND hwndParent = pHHInfo->WinType.hwndHelp;
1157     DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN;
1158     DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_CLIENTEDGE;
1159     RECT rc;
1160
1161     HP_GetHTMLRect(pHHInfo, &rc);
1162
1163     hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
1164                            rc.left, rc.top, rc.right, rc.bottom,
1165                            hwndParent, NULL, hhctrl_hinstance, NULL);
1166     if (!hWnd)
1167         return FALSE;
1168
1169     if (!InitWebBrowser(pHHInfo, hWnd))
1170         return FALSE;
1171
1172     /* store the pointer to the HH info struct */
1173     SetWindowLongPtrW(hWnd, 0, (LONG_PTR)pHHInfo);
1174
1175     ShowWindow(hWnd, SW_SHOW);
1176     UpdateWindow(hWnd);
1177
1178     pHHInfo->WinType.hwndHTML = hWnd;
1179     return TRUE;
1180 }
1181
1182 static BOOL AddContentTab(HHInfo *info)
1183 {
1184     HIMAGELIST hImageList;
1185     HBITMAP hBitmap;
1186     HWND hWnd;
1187
1188     if(info->tabs[TAB_CONTENTS].id == -1)
1189         return TRUE; /* No "Contents" tab */
1190     hWnd = CreateWindowExW(WS_EX_CLIENTEDGE, WC_TREEVIEWW, szEmpty, WS_CHILD | WS_BORDER | TVS_LINESATROOT
1191                            | TVS_SHOWSELALWAYS | TVS_HASBUTTONS, 50, 50, 100, 100,
1192                            info->WinType.hwndNavigation, NULL, hhctrl_hinstance, NULL);
1193     if(!hWnd) {
1194         ERR("Could not create treeview control\n");
1195         return FALSE;
1196     }
1197
1198     hImageList = ImageList_Create(16, 16, ILC_COLOR32, 0, HHTV_NUMBITMAPS);
1199     hBitmap = LoadBitmapW(hhctrl_hinstance, MAKEINTRESOURCEW(IDB_HHTREEVIEW));
1200     ImageList_Add(hImageList, hBitmap, NULL);
1201     SendMessageW(hWnd, TVM_SETIMAGELIST, TVSIL_NORMAL, (LPARAM)hImageList);
1202
1203     info->contents.hImageList = hImageList;
1204     info->tabs[TAB_CONTENTS].hwnd = hWnd;
1205     ResizeTabChild(info, TAB_CONTENTS);
1206     ShowWindow(hWnd, SW_SHOW);
1207
1208     return TRUE;
1209 }
1210
1211 static BOOL AddIndexTab(HHInfo *info)
1212 {
1213     char hidden_column[] = "Column";
1214     LVCOLUMNA lvc;
1215
1216     if(info->tabs[TAB_INDEX].id == -1)
1217         return TRUE; /* No "Index" tab */
1218     info->tabs[TAB_INDEX].hwnd = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW,
1219            szEmpty, WS_CHILD | WS_BORDER | LVS_SINGLESEL | LVS_REPORT | LVS_NOCOLUMNHEADER, 50, 50, 100, 100,
1220            info->WinType.hwndNavigation, NULL, hhctrl_hinstance, NULL);
1221     if(!info->tabs[TAB_INDEX].hwnd) {
1222         ERR("Could not create ListView control\n");
1223         return FALSE;
1224     }
1225     memset(&lvc, 0, sizeof(lvc));
1226     lvc.mask = LVCF_TEXT;
1227     lvc.pszText = hidden_column;
1228     if(SendMessageW(info->tabs[TAB_INDEX].hwnd, LVM_INSERTCOLUMNA, 0, (LPARAM) &lvc) == -1)
1229     {
1230         ERR("Could not create ListView column\n");
1231         return FALSE;
1232     }
1233
1234     ResizeTabChild(info, TAB_INDEX);
1235     ShowWindow(info->tabs[TAB_INDEX].hwnd, SW_HIDE);
1236
1237     return TRUE;
1238 }
1239
1240 static BOOL AddSearchTab(HHInfo *info)
1241 {
1242     HWND hwndList, hwndEdit, hwndContainer;
1243     char hidden_column[] = "Column";
1244     WNDPROC editWndProc;
1245     LVCOLUMNA lvc;
1246
1247     if(info->tabs[TAB_SEARCH].id == -1)
1248         return TRUE; /* No "Search" tab */
1249     hwndContainer = CreateWindowExW(WS_EX_CONTROLPARENT, szChildClass, szEmpty,
1250                                     WS_CHILD, 0, 0, 0, 0, info->WinType.hwndNavigation,
1251                                     NULL, hhctrl_hinstance, NULL);
1252     if(!hwndContainer) {
1253         ERR("Could not create search window container control.\n");
1254         return FALSE;
1255     }
1256     hwndEdit = CreateWindowExW(WS_EX_CLIENTEDGE, WC_EDITW, szEmpty, WS_CHILD
1257                                 | WS_VISIBLE | ES_LEFT | SS_NOTIFY, 0, 0, 0, 0,
1258                                hwndContainer, NULL, hhctrl_hinstance, NULL);
1259     if(!hwndEdit) {
1260         ERR("Could not create search ListView control.\n");
1261         return FALSE;
1262     }
1263     if(SendMessageW(hwndEdit, WM_SETFONT, (WPARAM) info->hFont, (LPARAM) FALSE) == -1)
1264     {
1265         ERR("Could not set font for edit control.\n");
1266         return FALSE;
1267     }
1268     editWndProc = (WNDPROC) SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC, (LONG_PTR)EditChild_WndProc);
1269     if(!editWndProc) {
1270         ERR("Could not redirect messages for edit control.\n");
1271         return FALSE;
1272     }
1273     SetWindowLongPtrW(hwndEdit, GWLP_USERDATA, (LONG_PTR)editWndProc);
1274     hwndList = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW, szEmpty,
1275                                WS_CHILD | WS_VISIBLE | WS_BORDER | LVS_SINGLESEL
1276                                 | LVS_REPORT | LVS_NOCOLUMNHEADER, 0, 0, 0, 0,
1277                                hwndContainer, NULL, hhctrl_hinstance, NULL);
1278     if(!hwndList) {
1279         ERR("Could not create search ListView control.\n");
1280         return FALSE;
1281     }
1282     memset(&lvc, 0, sizeof(lvc));
1283     lvc.mask = LVCF_TEXT;
1284     lvc.pszText = hidden_column;
1285     if(SendMessageW(hwndList, LVM_INSERTCOLUMNA, 0, (LPARAM) &lvc) == -1)
1286     {
1287         ERR("Could not create ListView column\n");
1288         return FALSE;
1289     }
1290
1291     info->search.hwndEdit = hwndEdit;
1292     info->search.hwndList = hwndList;
1293     info->search.hwndContainer = hwndContainer;
1294     info->tabs[TAB_SEARCH].hwnd = hwndContainer;
1295
1296     SetWindowLongPtrW(hwndContainer, 0, (LONG_PTR)info);
1297
1298     ResizeTabChild(info, TAB_SEARCH);
1299
1300     return TRUE;
1301 }
1302
1303 /* The Index tab's sub-topic popup */
1304
1305 static void ResizePopupChild(HHInfo *info)
1306 {
1307     int scroll_width = GetSystemMetrics(SM_CXVSCROLL);
1308     int border_width = GetSystemMetrics(SM_CXBORDER);
1309     int edge_width = GetSystemMetrics(SM_CXEDGE);
1310     INT width, height;
1311     RECT rect;
1312
1313     if(!info)
1314         return;
1315
1316     GetClientRect(info->popup.hwndPopup, &rect);
1317     SetWindowPos(info->popup.hwndCallback, HWND_TOP, 0, 0,
1318                  rect.right, rect.bottom, SWP_NOMOVE);
1319
1320     rect.left = TAB_MARGIN;
1321     rect.top = TAB_TOP_PADDING + TAB_MARGIN;
1322     rect.right -= TAB_RIGHT_PADDING + TAB_MARGIN;
1323     rect.bottom -= TAB_MARGIN;
1324     width = rect.right-rect.left;
1325     height = rect.bottom-rect.top;
1326
1327     SetWindowPos(info->popup.hwndList, NULL, rect.left, rect.top, width, height,
1328                  SWP_NOZORDER | SWP_NOACTIVATE);
1329
1330     SendMessageW(info->popup.hwndList, LVM_SETCOLUMNWIDTH, 0,
1331                  width-scroll_width-2*border_width-2*edge_width);
1332 }
1333
1334 static LRESULT CALLBACK HelpPopup_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1335 {
1336     HHInfo *info = (HHInfo *)GetWindowLongPtrW(hWnd, 0);
1337
1338     switch (message)
1339     {
1340     case WM_SIZE:
1341         ResizePopupChild(info);
1342         return 0;
1343     case WM_DESTROY:
1344         DestroyWindow(hWnd);
1345         return 0;
1346     case WM_CLOSE:
1347         ShowWindow(hWnd, SW_HIDE);
1348         return 0;
1349
1350     default:
1351         return DefWindowProcW(hWnd, message, wParam, lParam);
1352     }
1353
1354     return 0;
1355 }
1356
1357 static LRESULT CALLBACK PopupChild_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1358 {
1359     switch (message)
1360     {
1361     case WM_NOTIFY: {
1362         NMHDR *nmhdr = (NMHDR*)lParam;
1363         switch(nmhdr->code)
1364         {
1365         case NM_DBLCLK: {
1366             HHInfo *info = (HHInfo*)GetWindowLongPtrW(hWnd, 0);
1367             IndexSubItem *iter;
1368
1369             if(info == 0 || lParam == 0)
1370                 return 0;
1371             iter = (IndexSubItem*) ((NMITEMACTIVATE *)lParam)->lParam;
1372             if(iter == 0)
1373                 return 0;
1374             NavigateToChm(info, info->index->merge.chm_file, iter->local);
1375             ShowWindow(info->popup.hwndPopup, SW_HIDE);
1376             return 0;
1377         }
1378         case NM_RETURN: {
1379             HHInfo *info = (HHInfo*)GetWindowLongPtrW(hWnd, 0);
1380             IndexSubItem *iter;
1381             LVITEMW lvItem;
1382
1383             if(info == 0)
1384                 return 0;
1385
1386             lvItem.iItem = (int) SendMessageW(info->popup.hwndList, LVM_GETSELECTIONMARK, 0, 0);
1387             lvItem.mask = TVIF_PARAM;
1388             SendMessageW(info->popup.hwndList, LVM_GETITEMW, 0, (LPARAM)&lvItem);
1389             iter = (IndexSubItem*) lvItem.lParam;
1390             NavigateToChm(info, info->index->merge.chm_file, iter->local);
1391             ShowWindow(info->popup.hwndPopup, SW_HIDE);
1392             return 0;
1393         }
1394         }
1395         break;
1396     }
1397     default:
1398         return DefWindowProcW(hWnd, message, wParam, lParam);
1399     }
1400
1401     return 0;
1402 }
1403
1404 static BOOL AddIndexPopup(HHInfo *info)
1405 {
1406     static const WCHAR szPopupChildClass[] = {'H','H',' ','P','o','p','u','p',' ','C','h','i','l','d',0};
1407     static const WCHAR windowCaptionW[] = {'S','e','l','e','c','t',' ','T','o','p','i','c',':',0};
1408     static const WCHAR windowClassW[] = {'H','H',' ','P','o','p','u','p',0};
1409     HWND hwndList, hwndPopup, hwndCallback;
1410     char hidden_column[] = "Column";
1411     WNDCLASSEXW wcex;
1412     LVCOLUMNA lvc;
1413
1414     if(info->tabs[TAB_INDEX].id == -1)
1415         return TRUE; /* No "Index" tab */
1416
1417     wcex.cbSize         = sizeof(WNDCLASSEXW);
1418     wcex.style          = CS_HREDRAW | CS_VREDRAW;
1419     wcex.lpfnWndProc    = HelpPopup_WndProc;
1420     wcex.cbClsExtra     = 0;
1421     wcex.cbWndExtra     = sizeof(LONG_PTR);
1422     wcex.hInstance      = hhctrl_hinstance;
1423     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1424     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
1425     wcex.hbrBackground  = (HBRUSH)(COLOR_MENU + 1);
1426     wcex.lpszMenuName   = NULL;
1427     wcex.lpszClassName  = windowClassW;
1428     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1429     RegisterClassExW(&wcex);
1430
1431     wcex.cbSize         = sizeof(WNDCLASSEXW);
1432     wcex.style          = 0;
1433     wcex.lpfnWndProc    = PopupChild_WndProc;
1434     wcex.cbClsExtra     = 0;
1435     wcex.cbWndExtra     = sizeof(LONG_PTR);
1436     wcex.hInstance      = hhctrl_hinstance;
1437     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1438     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
1439     wcex.hbrBackground  = (HBRUSH)(COLOR_BTNFACE + 1);
1440     wcex.lpszMenuName   = NULL;
1441     wcex.lpszClassName  = szPopupChildClass;
1442     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1443     RegisterClassExW(&wcex);
1444
1445     hwndPopup = CreateWindowExW(WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_APPWINDOW
1446                                  | WS_EX_WINDOWEDGE | WS_EX_RIGHTSCROLLBAR,
1447                                 windowClassW, windowCaptionW, WS_POPUPWINDOW
1448                                  | WS_OVERLAPPEDWINDOW | WS_VISIBLE
1449                                  | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, CW_USEDEFAULT,
1450                                 CW_USEDEFAULT, 300, 200, info->WinType.hwndHelp,
1451                                 NULL, hhctrl_hinstance, NULL);
1452     if (!hwndPopup)
1453         return FALSE;
1454
1455     hwndCallback = CreateWindowExW(WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR,
1456                                    szPopupChildClass, szEmpty, WS_CHILDWINDOW | WS_VISIBLE,
1457                                    0, 0, 0, 0,
1458                                    hwndPopup, NULL, hhctrl_hinstance, NULL);
1459     if (!hwndCallback)
1460         return FALSE;
1461
1462     ShowWindow(hwndPopup, SW_HIDE);
1463     hwndList = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW, szEmpty,
1464                                WS_CHILD | WS_BORDER | LVS_SINGLESEL | LVS_REPORT
1465                                 | LVS_NOCOLUMNHEADER, 50, 50, 100, 100,
1466                                hwndCallback, NULL, hhctrl_hinstance, NULL);
1467     if(!hwndList) {
1468         ERR("Could not create popup ListView control\n");
1469         return FALSE;
1470     }
1471     memset(&lvc, 0, sizeof(lvc));
1472     lvc.mask = LVCF_TEXT;
1473     lvc.pszText = hidden_column;
1474     if(SendMessageW(hwndList, LVM_INSERTCOLUMNA, 0, (LPARAM) &lvc) == -1)
1475     {
1476         ERR("Could not create popup ListView column\n");
1477         return FALSE;
1478     }
1479
1480     info->popup.hwndCallback = hwndCallback;
1481     info->popup.hwndPopup = hwndPopup;
1482     info->popup.hwndList = hwndList;
1483     SetWindowLongPtrW(hwndPopup, 0, (LONG_PTR)info);
1484     SetWindowLongPtrW(hwndCallback, 0, (LONG_PTR)info);
1485
1486     ResizePopupChild(info);
1487     ShowWindow(hwndList, SW_SHOW);
1488
1489     return TRUE;
1490 }
1491
1492 /* Viewer Window */
1493
1494 static void ExpandContract(HHInfo *pHHInfo)
1495 {
1496     RECT r, nav;
1497
1498     pHHInfo->WinType.fNotExpanded = !pHHInfo->WinType.fNotExpanded;
1499     GetWindowRect(pHHInfo->WinType.hwndHelp, &r);
1500     NP_GetNavigationRect(pHHInfo, &nav);
1501
1502     /* hide/show both the nav bar and the size bar */
1503     if (pHHInfo->WinType.fNotExpanded)
1504     {
1505         ShowWindow(pHHInfo->WinType.hwndNavigation, SW_HIDE);
1506         ShowWindow(pHHInfo->hwndSizeBar, SW_HIDE);
1507         r.left = r.left + nav.right;
1508
1509         SendMessageW(pHHInfo->WinType.hwndToolBar, TB_HIDEBUTTON, IDTB_EXPAND, MAKELPARAM(FALSE, 0));
1510         SendMessageW(pHHInfo->WinType.hwndToolBar, TB_HIDEBUTTON, IDTB_CONTRACT, MAKELPARAM(TRUE, 0));
1511     }
1512     else
1513     {
1514         ShowWindow(pHHInfo->WinType.hwndNavigation, SW_SHOW);
1515         ShowWindow(pHHInfo->hwndSizeBar, SW_SHOW);
1516         r.left = r.left - nav.right;
1517
1518         SendMessageW(pHHInfo->WinType.hwndToolBar, TB_HIDEBUTTON, IDTB_EXPAND, MAKELPARAM(TRUE, 0));
1519         SendMessageW(pHHInfo->WinType.hwndToolBar, TB_HIDEBUTTON, IDTB_CONTRACT, MAKELPARAM(FALSE, 0));
1520     }
1521
1522     MoveWindow(pHHInfo->WinType.hwndHelp, r.left, r.top, r.right-r.left, r.bottom-r.top, TRUE);
1523 }
1524
1525 static LRESULT Help_OnSize(HWND hWnd)
1526 {
1527     HHInfo *pHHInfo = (HHInfo *)GetWindowLongPtrW(hWnd, 0);
1528     DWORD dwSize;
1529     RECT rc;
1530
1531     if (!pHHInfo)
1532         return 0;
1533
1534     if (!pHHInfo->WinType.fNotExpanded)
1535     {
1536         NP_GetNavigationRect(pHHInfo, &rc);
1537         SetWindowPos(pHHInfo->WinType.hwndNavigation, HWND_TOP, 0, 0,
1538                      rc.right, rc.bottom, SWP_NOMOVE);
1539
1540         SB_GetSizeBarRect(pHHInfo, &rc);
1541         SetWindowPos(pHHInfo->hwndSizeBar, HWND_TOP, rc.left, rc.top,
1542                      rc.right, rc.bottom, SWP_SHOWWINDOW);
1543
1544     }
1545
1546     HP_GetHTMLRect(pHHInfo, &rc);
1547     SetWindowPos(pHHInfo->WinType.hwndHTML, HWND_TOP, rc.left, rc.top,
1548                  rc.right, rc.bottom, SWP_SHOWWINDOW);
1549
1550     /* Resize browser window taking the frame size into account */
1551     dwSize = GetSystemMetrics(SM_CXFRAME);
1552     ResizeWebBrowser(pHHInfo, rc.right - dwSize, rc.bottom - dwSize);
1553
1554     return 0;
1555 }
1556
1557 static LRESULT CALLBACK Help_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1558 {
1559     switch (message)
1560     {
1561     case WM_COMMAND:
1562         if (HIWORD(wParam) == BN_CLICKED)
1563             TB_OnClick(hWnd, LOWORD(wParam));
1564         break;
1565     case WM_SIZE:
1566         return Help_OnSize(hWnd);
1567     case WM_CLOSE:
1568         ReleaseHelpViewer((HHInfo *)GetWindowLongPtrW(hWnd, 0));
1569         return 0;
1570     case WM_DESTROY:
1571         if(hh_process)
1572             PostQuitMessage(0);
1573         break;
1574
1575     default:
1576         return DefWindowProcW(hWnd, message, wParam, lParam);
1577     }
1578
1579     return 0;
1580 }
1581
1582 static BOOL HH_CreateHelpWindow(HHInfo *info)
1583 {
1584     HWND hWnd;
1585     RECT winPos = info->WinType.rcWindowPos;
1586     WNDCLASSEXW wcex;
1587     DWORD dwStyles, dwExStyles;
1588     DWORD x, y, width = 0, height = 0;
1589     LPCWSTR caption;
1590
1591     static const WCHAR windowClassW[] = {
1592         'H','H',' ', 'P','a','r','e','n','t',0
1593     };
1594
1595     wcex.cbSize         = sizeof(WNDCLASSEXW);
1596     wcex.style          = CS_HREDRAW | CS_VREDRAW;
1597     wcex.lpfnWndProc    = Help_WndProc;
1598     wcex.cbClsExtra     = 0;
1599     wcex.cbWndExtra     = sizeof(LONG_PTR);
1600     wcex.hInstance      = hhctrl_hinstance;
1601     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1602     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
1603     wcex.hbrBackground  = (HBRUSH)(COLOR_MENU + 1);
1604     wcex.lpszMenuName   = NULL;
1605     wcex.lpszClassName  = windowClassW;
1606     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1607
1608     RegisterClassExW(&wcex);
1609
1610     /* Read in window parameters if available */
1611     if (info->WinType.fsValidMembers & HHWIN_PARAM_STYLES)
1612         dwStyles = info->WinType.dwStyles | WS_OVERLAPPEDWINDOW;
1613     else
1614         dwStyles = WS_OVERLAPPEDWINDOW | WS_VISIBLE |
1615                    WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
1616
1617     if (info->WinType.fsValidMembers & HHWIN_PARAM_EXSTYLES)
1618         dwExStyles = info->WinType.dwExStyles;
1619     else
1620         dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_APPWINDOW |
1621                      WS_EX_WINDOWEDGE | WS_EX_RIGHTSCROLLBAR;
1622
1623     if (info->WinType.fsValidMembers & HHWIN_PARAM_RECT)
1624     {
1625         x = winPos.left;
1626         y = winPos.top;
1627         width = winPos.right - x;
1628         height = winPos.bottom - y;
1629     }
1630     if (!width || !height)
1631     {
1632         x = WINTYPE_DEFAULT_X;
1633         y = WINTYPE_DEFAULT_Y;
1634         width = WINTYPE_DEFAULT_WIDTH;
1635         height = WINTYPE_DEFAULT_HEIGHT;
1636     }
1637
1638     if (info->WinType.fNotExpanded)
1639     {
1640         if (!(info->WinType.fsValidMembers & HHWIN_PARAM_NAV_WIDTH) &&
1641               info->WinType.iNavWidth == 0)
1642         {
1643             info->WinType.iNavWidth = WINTYPE_DEFAULT_NAVWIDTH;
1644         }
1645
1646         x += info->WinType.iNavWidth;
1647         width -= info->WinType.iNavWidth;
1648     }
1649
1650
1651     caption = info->WinType.pszCaption;
1652     if (!*caption) caption = info->pCHMInfo->defTitle;
1653
1654     hWnd = CreateWindowExW(dwExStyles, windowClassW, caption,
1655                            dwStyles, x, y, width, height, NULL, NULL, hhctrl_hinstance, NULL);
1656     if (!hWnd)
1657         return FALSE;
1658
1659     ShowWindow(hWnd, SW_SHOW);
1660     UpdateWindow(hWnd);
1661
1662     /* store the pointer to the HH info struct */
1663     SetWindowLongPtrW(hWnd, 0, (LONG_PTR)info);
1664
1665     info->WinType.hwndHelp = hWnd;
1666     return TRUE;
1667 }
1668
1669 static void HH_CreateFont(HHInfo *pHHInfo)
1670 {
1671     LOGFONTW lf;
1672
1673     GetObjectW(GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONTW), &lf);
1674     lf.lfWeight = FW_NORMAL;
1675     lf.lfItalic = FALSE;
1676     lf.lfUnderline = FALSE;
1677
1678     pHHInfo->hFont = CreateFontIndirectW(&lf);
1679 }
1680
1681 static void HH_InitRequiredControls(DWORD dwControls)
1682 {
1683     INITCOMMONCONTROLSEX icex;
1684
1685     icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
1686     icex.dwICC = dwControls;
1687     InitCommonControlsEx(&icex);
1688 }
1689
1690 /* Creates the whole package */
1691 static BOOL CreateViewer(HHInfo *pHHInfo)
1692 {
1693     HH_CreateFont(pHHInfo);
1694
1695     if (!HH_CreateHelpWindow(pHHInfo))
1696         return FALSE;
1697
1698     HH_InitRequiredControls(ICC_BAR_CLASSES);
1699
1700     if (!HH_AddToolbar(pHHInfo))
1701         return FALSE;
1702
1703     HH_RegisterChildWndClass(pHHInfo);
1704
1705     if (!HH_AddNavigationPane(pHHInfo))
1706         return FALSE;
1707
1708     HH_RegisterSizeBarClass(pHHInfo);
1709
1710     if (!HH_AddSizeBar(pHHInfo))
1711         return FALSE;
1712
1713     if (!HH_AddHTMLPane(pHHInfo))
1714         return FALSE;
1715
1716     if (!AddContentTab(pHHInfo))
1717         return FALSE;
1718
1719     if (!AddIndexTab(pHHInfo))
1720         return FALSE;
1721
1722     if (!AddIndexPopup(pHHInfo))
1723         return FALSE;
1724
1725     if (!AddSearchTab(pHHInfo))
1726         return FALSE;
1727
1728     InitContent(pHHInfo);
1729     InitIndex(pHHInfo);
1730
1731     return TRUE;
1732 }
1733
1734 void ReleaseHelpViewer(HHInfo *info)
1735 {
1736     TRACE("(%p)\n", info);
1737
1738     if (!info)
1739         return;
1740
1741     /* Free allocated strings */
1742     heap_free(info->pszType);
1743     heap_free(info->pszCaption);
1744     heap_free(info->pszToc);
1745     heap_free(info->pszIndex);
1746     heap_free(info->pszFile);
1747     heap_free(info->pszHome);
1748     heap_free(info->pszJump1);
1749     heap_free(info->pszJump2);
1750     heap_free(info->pszUrlJump1);
1751     heap_free(info->pszUrlJump2);
1752
1753     if (info->pCHMInfo)
1754         CloseCHM(info->pCHMInfo);
1755
1756     ReleaseWebBrowser(info);
1757     ReleaseContent(info);
1758     ReleaseIndex(info);
1759     ReleaseSearch(info);
1760
1761     if(info->contents.hImageList)
1762         ImageList_Destroy(info->contents.hImageList);
1763     if(info->WinType.hwndHelp)
1764         DestroyWindow(info->WinType.hwndHelp);
1765
1766     heap_free(info);
1767     OleUninitialize();
1768 }
1769
1770 HHInfo *CreateHelpViewer(LPCWSTR filename)
1771 {
1772     HHInfo *info = heap_alloc_zero(sizeof(HHInfo));
1773     int i;
1774
1775     /* Set the invalid tab ID (-1) as the default value for all
1776      * of the tabs, this matches a failed TCM_INSERTITEM call.
1777      */
1778     for(i=0;i<sizeof(info->tabs)/sizeof(HHTab);i++)
1779         info->tabs[i].id = -1;
1780
1781     OleInitialize(NULL);
1782
1783     info->pCHMInfo = OpenCHM(filename);
1784     if(!info->pCHMInfo) {
1785         ReleaseHelpViewer(info);
1786         return NULL;
1787     }
1788
1789     if (!LoadWinTypeFromCHM(info)) {
1790         ReleaseHelpViewer(info);
1791         return NULL;
1792     }
1793
1794     if(!CreateViewer(info)) {
1795         ReleaseHelpViewer(info);
1796         return NULL;
1797     }
1798
1799     return info;
1800 }
1801
1802 /*
1803  * Search the table of HTML entities and return the corresponding ANSI symbol.
1804  */
1805 static char find_html_symbol(const char *entity, int entity_len)
1806 {
1807     int max = sizeof(html_encoded_symbols)/sizeof(html_encoded_symbols[0])-1;
1808     int min = 0, dir;
1809
1810     while(min <= max)
1811     {
1812         int pos = (min+max)/2;
1813         const char *encoded_symbol = html_encoded_symbols[pos].html_code;
1814         dir = strncmp(encoded_symbol, entity, entity_len);
1815         if(dir == 0 && !encoded_symbol[entity_len]) return html_encoded_symbols[pos].ansi_symbol;
1816         if(dir < 0)
1817             min = pos+1;
1818         else
1819             max = pos-1;
1820     }
1821     return 0;
1822 }
1823
1824 /*
1825  * Decode a string containing HTML encoded characters into a unicode string.
1826  */
1827 WCHAR *decode_html(const char *html_fragment, int html_fragment_len, UINT code_page)
1828 {
1829     const char *h = html_fragment;
1830     char *amp, *sem, symbol, *tmp;
1831     int len, tmp_len = 0;
1832     WCHAR *unicode_text;
1833
1834     tmp = heap_alloc(html_fragment_len+1);
1835     while(1)
1836     {
1837         symbol = 0;
1838         amp = strchr(h, '&');
1839         if(!amp) break;
1840         len = amp-h;
1841         /* Copy the characters prior to the HTML encoded character */
1842         memcpy(&tmp[tmp_len], h, len);
1843         tmp_len += len;
1844         amp++; /* skip ampersand */
1845         sem = strchr(amp, ';');
1846         /* Require a semicolon after the ampersand */
1847         if(!sem)
1848         {
1849             h = amp;
1850             tmp[tmp_len++] = '&';
1851             continue;
1852         }
1853         /* Find the symbol either by using the ANSI character number (prefixed by the pound symbol)
1854          * or by searching the HTML entity table */
1855         len = sem-amp;
1856         if(amp[0] == '#')
1857         {
1858             char *endnum = NULL;
1859             int tmp;
1860
1861             tmp = (char) strtol(amp, &endnum, 10);
1862             if(endnum == sem)
1863                 symbol = tmp;
1864         }
1865         else
1866             symbol = find_html_symbol(amp, len);
1867         if(!symbol)
1868         {
1869             FIXME("Failed to translate HTML encoded character '&%.*s;'.\n", len, amp);
1870             h = amp;
1871             tmp[tmp_len++] = '&';
1872             continue;
1873         }
1874         /* Insert the new symbol */
1875         h = sem+1;
1876         tmp[tmp_len++] = symbol;
1877     }
1878     /* Convert any remaining characters */
1879     len = html_fragment_len-(h-html_fragment);
1880     memcpy(&tmp[tmp_len], h, len);
1881     tmp_len += len;
1882     tmp[tmp_len++] = 0; /* NULL-terminate the string */
1883
1884     len = MultiByteToWideChar(code_page, 0, tmp, tmp_len, NULL, 0);
1885     unicode_text = heap_alloc(len*sizeof(WCHAR));
1886     MultiByteToWideChar(code_page, 0, tmp, tmp_len, unicode_text, len);
1887     heap_free(tmp);
1888     return unicode_text;
1889 }