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