hhctrl.ocx: Fix a double-click bug in the Contents tab introduced by the Index tab...
[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  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "hhctrl.h"
23
24 #include "wingdi.h"
25 #include "commctrl.h"
26 #include "wininet.h"
27
28 #include "wine/debug.h"
29
30 #include "resource.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(htmlhelp);
33
34 static LRESULT Help_OnSize(HWND hWnd);
35
36 /* Window type defaults */
37
38 #define WINTYPE_DEFAULT_X           280
39 #define WINTYPE_DEFAULT_Y           100
40 #define WINTYPE_DEFAULT_WIDTH       740
41 #define WINTYPE_DEFAULT_HEIGHT      640
42 #define WINTYPE_DEFAULT_NAVWIDTH    250
43
44 #define TAB_TOP_PADDING     8
45 #define TAB_RIGHT_PADDING   4
46 #define TAB_MARGIN  8
47
48 static const WCHAR szEmpty[] = {0};
49
50 /* Loads a string from the resource file */
51 static LPWSTR HH_LoadString(DWORD dwID)
52 {
53     LPWSTR string = NULL;
54     LPCWSTR stringresource;
55     int iSize;
56
57     iSize = LoadStringW(hhctrl_hinstance, dwID, (LPWSTR)&stringresource, 0);
58
59     string = heap_alloc((iSize + 2) * sizeof(WCHAR)); /* some strings (tab text) needs double-null termination */
60     memcpy(string, stringresource, iSize*sizeof(WCHAR));
61     string[iSize] = 0;
62
63     return string;
64 }
65
66 static HRESULT navigate_url(HHInfo *info, LPCWSTR surl)
67 {
68     VARIANT url;
69     HRESULT hres;
70
71     TRACE("%s\n", debugstr_w(surl));
72
73     V_VT(&url) = VT_BSTR;
74     V_BSTR(&url) = SysAllocString(surl);
75
76     hres = IWebBrowser2_Navigate2(info->web_browser, &url, 0, 0, 0, 0);
77
78     VariantClear(&url);
79
80     if(FAILED(hres))
81         TRACE("Navigation failed: %08x\n", hres);
82
83     return hres;
84 }
85
86 BOOL NavigateToUrl(HHInfo *info, LPCWSTR surl)
87 {
88     ChmPath chm_path;
89     BOOL ret;
90     HRESULT hres;
91
92     static const WCHAR url_indicator[] = {':', '/', '/', 0};
93
94     TRACE("%s\n", debugstr_w(surl));
95
96     if (strstrW(surl, url_indicator)) {
97         hres = navigate_url(info, surl);
98         if(SUCCEEDED(hres))
99             return TRUE;
100     } /* look up in chm if it doesn't look like a full url */
101
102     SetChmPath(&chm_path, info->pCHMInfo->szFile, surl);
103     ret = NavigateToChm(info, chm_path.chm_file, chm_path.chm_index);
104
105     heap_free(chm_path.chm_file);
106     heap_free(chm_path.chm_index);
107
108     return ret;
109 }
110
111 BOOL NavigateToChm(HHInfo *info, LPCWSTR file, LPCWSTR index)
112 {
113     WCHAR buf[INTERNET_MAX_URL_LENGTH];
114     WCHAR full_path[MAX_PATH];
115     LPWSTR ptr;
116
117     static const WCHAR url_format[] =
118         {'m','k',':','@','M','S','I','T','S','t','o','r','e',':','%','s',':',':','%','s','%','s',0};
119     static const WCHAR slash[] = {'/',0};
120     static const WCHAR empty[] = {0};
121
122     TRACE("%p %s %s\n", info, debugstr_w(file), debugstr_w(index));
123
124     if (!info->web_browser)
125         return FALSE;
126
127     if(!GetFullPathNameW(file, sizeof(full_path)/sizeof(full_path[0]), full_path, NULL)) {
128         WARN("GetFullPathName failed: %u\n", GetLastError());
129         return FALSE;
130     }
131
132     wsprintfW(buf, url_format, full_path, (!index || index[0] == '/') ? empty : slash, index);
133
134     /* FIXME: HACK */
135     if((ptr = strchrW(buf, '#')))
136        *ptr = 0;
137
138     return SUCCEEDED(navigate_url(info, buf));
139 }
140
141 /* Size Bar */
142
143 #define SIZEBAR_WIDTH   4
144
145 static const WCHAR szSizeBarClass[] = {
146     'H','H',' ','S','i','z','e','B','a','r',0
147 };
148
149 /* Draw the SizeBar */
150 static void SB_OnPaint(HWND hWnd)
151 {
152     PAINTSTRUCT ps;
153     HDC hdc;
154     RECT rc;
155     
156     hdc = BeginPaint(hWnd, &ps);
157
158     GetClientRect(hWnd, &rc);
159
160     /* dark frame */
161     rc.right += 1;
162     rc.bottom -= 1;
163     FrameRect(hdc, &rc, GetStockObject(GRAY_BRUSH));
164
165     /* white highlight */
166     SelectObject(hdc, GetStockObject(WHITE_PEN));
167     MoveToEx(hdc, rc.right, 1, NULL);
168     LineTo(hdc, 1, 1);
169     LineTo(hdc, 1, rc.bottom - 1);
170
171     
172     MoveToEx(hdc, 0, rc.bottom, NULL);
173     LineTo(hdc, rc.right, rc.bottom);
174
175     EndPaint(hWnd, &ps);
176 }
177
178 static void SB_OnLButtonDown(HWND hWnd, WPARAM wParam, LPARAM lParam)
179 {
180     SetCapture(hWnd);
181 }
182
183 static void SB_OnLButtonUp(HWND hWnd, WPARAM wParam, LPARAM lParam)
184 {
185     HHInfo *pHHInfo = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
186     POINT pt;
187
188     pt.x = (short)LOWORD(lParam);
189     pt.y = (short)HIWORD(lParam);
190
191     /* update the window sizes */
192     pHHInfo->WinType.iNavWidth += pt.x;
193     Help_OnSize(hWnd);
194
195     ReleaseCapture();
196 }
197
198 static void SB_OnMouseMove(HWND hWnd, WPARAM wParam, LPARAM lParam)
199 {
200     /* ignore WM_MOUSEMOVE if not dragging the SizeBar */
201     if (!(wParam & MK_LBUTTON))
202         return;
203 }
204
205 static LRESULT CALLBACK SizeBar_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
206 {
207     switch (message)
208     {
209         case WM_LBUTTONDOWN:
210             SB_OnLButtonDown(hWnd, wParam, lParam);
211             break;
212         case WM_LBUTTONUP:
213             SB_OnLButtonUp(hWnd, wParam, lParam);
214             break;
215         case WM_MOUSEMOVE:
216             SB_OnMouseMove(hWnd, wParam, lParam);
217             break;
218         case WM_PAINT:
219             SB_OnPaint(hWnd);
220             break;
221         default:
222             return DefWindowProcW(hWnd, message, wParam, lParam);
223     }
224
225     return 0;
226 }
227
228 static void HH_RegisterSizeBarClass(HHInfo *pHHInfo)
229 {
230     WNDCLASSEXW wcex;
231
232     wcex.cbSize         = sizeof(WNDCLASSEXW);
233     wcex.style          = 0;
234     wcex.lpfnWndProc    = SizeBar_WndProc;
235     wcex.cbClsExtra     = 0;
236     wcex.cbWndExtra     = 0;
237     wcex.hInstance      = hhctrl_hinstance;
238     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
239     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_SIZEWE);
240     wcex.hbrBackground  = (HBRUSH)(COLOR_MENU + 1);
241     wcex.lpszMenuName   = NULL;
242     wcex.lpszClassName  = szSizeBarClass;
243     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
244
245     RegisterClassExW(&wcex);
246 }
247
248 static void SB_GetSizeBarRect(HHInfo *info, RECT *rc)
249 {
250     RECT rectWND, rectTB, rectNP;
251
252     GetClientRect(info->WinType.hwndHelp, &rectWND);
253     GetClientRect(info->WinType.hwndToolBar, &rectTB);
254     GetClientRect(info->WinType.hwndNavigation, &rectNP);
255
256     rc->left = rectNP.right;
257     rc->top = rectTB.bottom;
258     rc->bottom = rectWND.bottom - rectTB.bottom;
259     rc->right = SIZEBAR_WIDTH;
260 }
261
262 static BOOL HH_AddSizeBar(HHInfo *pHHInfo)
263 {
264     HWND hWnd;
265     HWND hwndParent = pHHInfo->WinType.hwndHelp;
266     DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE | WS_OVERLAPPED;
267     DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
268     RECT rc;
269
270     SB_GetSizeBarRect(pHHInfo, &rc);
271
272     hWnd = CreateWindowExW(dwExStyles, szSizeBarClass, szEmpty, dwStyles,
273                            rc.left, rc.top, rc.right, rc.bottom,
274                            hwndParent, NULL, hhctrl_hinstance, NULL);
275     if (!hWnd)
276         return FALSE;
277
278     /* store the pointer to the HH info struct */
279     SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
280
281     pHHInfo->hwndSizeBar = hWnd;
282     return TRUE;
283 }
284
285 /* Child Window */
286
287 static const WCHAR szChildClass[] = {
288     'H','H',' ','C','h','i','l','d',0
289 };
290
291 static LRESULT Child_OnPaint(HWND hWnd)
292 {
293     PAINTSTRUCT ps;
294     HDC hdc;
295     RECT rc;
296
297     hdc = BeginPaint(hWnd, &ps);
298
299     /* Only paint the Navigation pane, identified by the fact
300      * that it has a child window
301      */
302     if (GetWindow(hWnd, GW_CHILD))
303     {
304         GetClientRect(hWnd, &rc);
305
306         /* set the border color */
307         SelectObject(hdc, GetStockObject(DC_PEN));
308         SetDCPenColor(hdc, GetSysColor(COLOR_BTNSHADOW));
309
310         /* Draw the top border */
311         LineTo(hdc, rc.right, 0);
312
313         SelectObject(hdc, GetStockObject(WHITE_PEN));
314         MoveToEx(hdc, 0, 1, NULL);
315         LineTo(hdc, rc.right, 1);
316     }
317
318     EndPaint(hWnd, &ps);
319
320     return 0;
321 }
322
323 static void ResizeTabChild(HHInfo *info, int tab)
324 {
325     HWND hwnd = info->tabs[tab].hwnd;
326     INT width, height;
327     RECT rect, tabrc;
328     DWORD cnt;
329
330     GetClientRect(info->WinType.hwndNavigation, &rect);
331     SendMessageW(info->hwndTabCtrl, TCM_GETITEMRECT, 0, (LPARAM)&tabrc);
332     cnt = SendMessageW(info->hwndTabCtrl, TCM_GETROWCOUNT, 0, 0);
333
334     rect.left = TAB_MARGIN;
335     rect.top = TAB_TOP_PADDING + cnt*(tabrc.bottom-tabrc.top) + TAB_MARGIN;
336     rect.right -= TAB_RIGHT_PADDING + TAB_MARGIN;
337     rect.bottom -= TAB_MARGIN;
338     width = rect.right-rect.left;
339     height = rect.bottom-rect.top;
340
341     SetWindowPos(hwnd, NULL, rect.left, rect.top, width, height,
342                  SWP_NOZORDER | SWP_NOACTIVATE);
343
344     /* Resize the tab widget column to perfectly fit the tab window and
345      * leave sufficient space for the scroll widget.
346      */
347     switch (tab)
348     {
349     case TAB_INDEX: {
350         int scroll_width = GetSystemMetrics(SM_CXVSCROLL);
351         int border_width = GetSystemMetrics(SM_CXBORDER);
352         int edge_width = GetSystemMetrics(SM_CXEDGE);
353
354         SendMessageW(info->tabs[TAB_INDEX].hwnd, LVM_SETCOLUMNWIDTH, 0,
355                      width-scroll_width-2*border_width-2*edge_width);
356
357         break;
358     }
359     }
360 }
361
362 static LRESULT Child_OnSize(HWND hwnd)
363 {
364     HHInfo *info = (HHInfo*)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
365     RECT rect;
366
367     if(!info || hwnd != info->WinType.hwndNavigation)
368         return 0;
369
370     GetClientRect(hwnd, &rect);
371     SetWindowPos(info->hwndTabCtrl, HWND_TOP, 0, 0,
372                  rect.right - TAB_RIGHT_PADDING,
373                  rect.bottom - TAB_TOP_PADDING, SWP_NOMOVE);
374
375     ResizeTabChild(info, TAB_CONTENTS);
376     ResizeTabChild(info, TAB_INDEX);
377     return 0;
378 }
379
380 static LRESULT OnTabChange(HWND hwnd)
381 {
382     HHInfo *info = (HHInfo*)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
383
384     TRACE("%p\n", hwnd);
385
386     if (!info)
387         return 0;
388
389     if(info->tabs[info->current_tab].hwnd)
390         ShowWindow(info->tabs[info->current_tab].hwnd, SW_HIDE);
391
392     info->current_tab = SendMessageW(info->hwndTabCtrl, TCM_GETCURSEL, 0, 0);
393
394     if(info->tabs[info->current_tab].hwnd)
395         ShowWindow(info->tabs[info->current_tab].hwnd, SW_SHOW);
396
397     return 0;
398 }
399
400 static LRESULT OnTopicChange(HHInfo *info, void *user_data)
401 {
402     LPCWSTR chmfile = NULL, name = NULL, local = NULL;
403     ContentItem *citer;
404     IndexItem *iiter;
405
406     if(!user_data || !info)
407         return 0;
408
409     switch (info->current_tab)
410     {
411     case TAB_CONTENTS:
412         citer = (ContentItem *) user_data;
413         name = citer->name;
414         local = citer->local;
415         while(citer) {
416             if(citer->merge.chm_file) {
417                 chmfile = citer->merge.chm_file;
418                 break;
419             }
420             citer = citer->parent;
421         }
422         chmfile = citer->merge.chm_file;
423         break;
424     case TAB_INDEX:
425         iiter = (IndexItem *) user_data;
426         if(iiter->nItems == 0) {
427             FIXME("No entries for this item!\n");
428             return 0;
429         }
430         if(iiter->nItems > 1) {
431             int i = 0;
432             LVITEMW lvi;
433
434             SendMessageW(info->popup.hwndList, LVM_DELETEALLITEMS, 0, 0);
435             for(i=0;i<iiter->nItems;i++) {
436                 IndexSubItem *item = &iiter->items[i];
437                 WCHAR *name = iiter->keyword;
438
439                 if(item->name)
440                     name = item->name;
441                 memset(&lvi, 0, sizeof(lvi));
442                 lvi.iItem = i;
443                 lvi.mask = LVIF_TEXT|LVIF_PARAM;
444                 lvi.cchTextMax = strlenW(name)+1;
445                 lvi.pszText = name;
446                 lvi.lParam = (LPARAM) item;
447                 SendMessageW(info->popup.hwndList, LVM_INSERTITEMW, 0, (LPARAM)&lvi);
448             }
449             ShowWindow(info->popup.hwndPopup, SW_SHOW);
450             return 0;
451         }
452         name = iiter->items[0].name;
453         local = iiter->items[0].local;
454         chmfile = iiter->merge.chm_file;
455         break;
456     default:
457         FIXME("Unhandled operation for this tab!\n");
458         return 0;
459     }
460
461     TRACE("name %s loal %s\n", debugstr_w(name), debugstr_w(local));
462
463     NavigateToChm(info, chmfile, local);
464     return 0;
465 }
466
467 static LRESULT CALLBACK Child_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
468 {
469     switch (message)
470     {
471     case WM_PAINT:
472         return Child_OnPaint(hWnd);
473     case WM_SIZE:
474         return Child_OnSize(hWnd);
475     case WM_NOTIFY: {
476         HHInfo *info = (HHInfo*)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
477         NMHDR *nmhdr = (NMHDR*)lParam;
478
479         switch(nmhdr->code) {
480         case TCN_SELCHANGE:
481             return OnTabChange(hWnd);
482         case TVN_SELCHANGEDW:
483             return OnTopicChange(info, (void*)((NMTREEVIEWW *)lParam)->itemNew.lParam);
484         case NM_DBLCLK:
485             if(info->current_tab == TAB_INDEX)
486                 return OnTopicChange(info, (void*)((NMITEMACTIVATE *)lParam)->lParam);
487         }
488         break;
489     }
490     default:
491         return DefWindowProcW(hWnd, message, wParam, lParam);
492     }
493
494     return 0;
495 }
496
497 static void HH_RegisterChildWndClass(HHInfo *pHHInfo)
498 {
499     WNDCLASSEXW wcex;
500
501     wcex.cbSize         = sizeof(WNDCLASSEXW);
502     wcex.style          = 0;
503     wcex.lpfnWndProc    = Child_WndProc;
504     wcex.cbClsExtra     = 0;
505     wcex.cbWndExtra     = 0;
506     wcex.hInstance      = hhctrl_hinstance;
507     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
508     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
509     wcex.hbrBackground  = (HBRUSH)(COLOR_BTNFACE + 1);
510     wcex.lpszMenuName   = NULL;
511     wcex.lpszClassName  = szChildClass;
512     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
513
514     RegisterClassExW(&wcex);
515 }
516
517 /* Toolbar */
518
519 #define ICON_SIZE   20
520
521 static void TB_OnClick(HWND hWnd, DWORD dwID)
522 {
523     HHInfo *info = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
524
525     switch (dwID)
526     {
527         case IDTB_STOP:
528             DoPageAction(info, WB_STOP);
529             break;
530         case IDTB_REFRESH:
531             DoPageAction(info, WB_REFRESH);
532             break;
533         case IDTB_BACK:
534             DoPageAction(info, WB_GOBACK);
535             break;
536         case IDTB_HOME:
537             NavigateToChm(info, info->pCHMInfo->szFile, info->WinType.pszHome);
538             break;
539         case IDTB_FORWARD:
540             DoPageAction(info, WB_GOFORWARD);
541             break;
542         case IDTB_EXPAND:
543         case IDTB_CONTRACT:
544         case IDTB_SYNC:
545         case IDTB_PRINT:
546         case IDTB_OPTIONS:
547         case IDTB_BROWSE_FWD:
548         case IDTB_BROWSE_BACK:
549         case IDTB_JUMP1:
550         case IDTB_JUMP2:
551         case IDTB_CUSTOMIZE:
552         case IDTB_ZOOM:
553         case IDTB_TOC_NEXT:
554         case IDTB_TOC_PREV:
555             break;
556     }
557 }
558
559 static void TB_AddButton(TBBUTTON *pButtons, DWORD dwIndex, DWORD dwID)
560 {
561     /* FIXME: Load the correct button bitmaps */
562     pButtons[dwIndex].iBitmap = STD_PRINT;
563     pButtons[dwIndex].idCommand = dwID;
564     pButtons[dwIndex].fsState = TBSTATE_ENABLED;
565     pButtons[dwIndex].fsStyle = BTNS_BUTTON;
566     pButtons[dwIndex].dwData = 0;
567     pButtons[dwIndex].iString = 0;
568 }
569
570 static void TB_AddButtonsFromFlags(TBBUTTON *pButtons, DWORD dwButtonFlags, LPDWORD pdwNumButtons)
571 {
572     *pdwNumButtons = 0;
573
574     if (dwButtonFlags & HHWIN_BUTTON_EXPAND)
575         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_EXPAND);
576
577     if (dwButtonFlags & HHWIN_BUTTON_BACK)
578         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_BACK);
579
580     if (dwButtonFlags & HHWIN_BUTTON_FORWARD)
581         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_FORWARD);
582
583     if (dwButtonFlags & HHWIN_BUTTON_STOP)
584         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_STOP);
585
586     if (dwButtonFlags & HHWIN_BUTTON_REFRESH)
587         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_REFRESH);
588
589     if (dwButtonFlags & HHWIN_BUTTON_HOME)
590         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_HOME);
591
592     if (dwButtonFlags & HHWIN_BUTTON_SYNC)
593         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_SYNC);
594
595     if (dwButtonFlags & HHWIN_BUTTON_OPTIONS)
596         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_OPTIONS);
597
598     if (dwButtonFlags & HHWIN_BUTTON_PRINT)
599         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_PRINT);
600
601     if (dwButtonFlags & HHWIN_BUTTON_JUMP1)
602         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_JUMP1);
603
604     if (dwButtonFlags & HHWIN_BUTTON_JUMP2)
605         TB_AddButton(pButtons,(*pdwNumButtons)++, IDTB_JUMP2);
606
607     if (dwButtonFlags & HHWIN_BUTTON_ZOOM)
608         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_ZOOM);
609
610     if (dwButtonFlags & HHWIN_BUTTON_TOC_NEXT)
611         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_TOC_NEXT);
612
613     if (dwButtonFlags & HHWIN_BUTTON_TOC_PREV)
614         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_TOC_PREV);
615 }
616
617 static BOOL HH_AddToolbar(HHInfo *pHHInfo)
618 {
619     HWND hToolbar;
620     HWND hwndParent = pHHInfo->WinType.hwndHelp;
621     DWORD toolbarFlags;
622     TBBUTTON buttons[IDTB_TOC_PREV - IDTB_EXPAND];
623     TBADDBITMAP tbAB;
624     DWORD dwStyles, dwExStyles;
625     DWORD dwNumButtons, dwIndex;
626
627     if (pHHInfo->WinType.fsWinProperties & HHWIN_PARAM_TB_FLAGS)
628         toolbarFlags = pHHInfo->WinType.fsToolBarFlags;
629     else
630         toolbarFlags = HHWIN_DEF_BUTTONS;
631
632     TB_AddButtonsFromFlags(buttons, toolbarFlags, &dwNumButtons);
633
634     dwStyles = WS_CHILDWINDOW | WS_VISIBLE | TBSTYLE_FLAT |
635                TBSTYLE_WRAPABLE | TBSTYLE_TOOLTIPS | CCS_NODIVIDER;
636     dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
637
638     hToolbar = CreateWindowExW(dwExStyles, TOOLBARCLASSNAMEW, NULL, dwStyles,
639                                0, 0, 0, 0, hwndParent, NULL,
640                                hhctrl_hinstance, NULL);
641     if (!hToolbar)
642         return FALSE;
643
644     SendMessageW(hToolbar, TB_SETBITMAPSIZE, 0, MAKELONG(ICON_SIZE, ICON_SIZE));
645     SendMessageW(hToolbar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
646     SendMessageW(hToolbar, WM_SETFONT, (WPARAM)pHHInfo->hFont, TRUE);
647
648     /* FIXME: Load correct icons for all buttons */
649     tbAB.hInst = HINST_COMMCTRL;
650     tbAB.nID = IDB_STD_LARGE_COLOR;
651     SendMessageW(hToolbar, TB_ADDBITMAP, 0, (LPARAM)&tbAB);
652
653     for (dwIndex = 0; dwIndex < dwNumButtons; dwIndex++)
654     {
655         LPWSTR szBuf = HH_LoadString(buttons[dwIndex].idCommand);
656         DWORD dwLen = strlenW(szBuf);
657         szBuf[dwLen + 1] = 0; /* Double-null terminate */
658
659         buttons[dwIndex].iString = (DWORD)SendMessageW(hToolbar, TB_ADDSTRINGW, 0, (LPARAM)szBuf);
660         heap_free(szBuf);
661     }
662
663     SendMessageW(hToolbar, TB_ADDBUTTONSW, dwNumButtons, (LPARAM)buttons);
664     SendMessageW(hToolbar, TB_AUTOSIZE, 0, 0);
665     ShowWindow(hToolbar, SW_SHOW);
666
667     pHHInfo->WinType.hwndToolBar = hToolbar;
668     return TRUE;
669 }
670
671 /* Navigation Pane */
672
673 static void NP_GetNavigationRect(HHInfo *pHHInfo, RECT *rc)
674 {
675     HWND hwndParent = pHHInfo->WinType.hwndHelp;
676     HWND hwndToolbar = pHHInfo->WinType.hwndToolBar;
677     RECT rectWND, rectTB;
678
679     GetClientRect(hwndParent, &rectWND);
680     GetClientRect(hwndToolbar, &rectTB);
681
682     rc->left = 0;
683     rc->top = rectTB.bottom;
684     rc->bottom = rectWND.bottom - rectTB.bottom;
685
686     if (!(pHHInfo->WinType.fsValidMembers & HHWIN_PARAM_NAV_WIDTH) &&
687           pHHInfo->WinType.iNavWidth == 0)
688     {
689         pHHInfo->WinType.iNavWidth = WINTYPE_DEFAULT_NAVWIDTH;
690     }
691
692     rc->right = pHHInfo->WinType.iNavWidth;
693 }
694
695 static DWORD NP_CreateTab(HINSTANCE hInstance, HWND hwndTabCtrl, DWORD index)
696 {
697     TCITEMW tie;
698     LPWSTR tabText = HH_LoadString(index);
699     DWORD ret;
700
701     tie.mask = TCIF_TEXT;
702     tie.pszText = tabText;
703
704     ret = SendMessageW( hwndTabCtrl, TCM_INSERTITEMW, index, (LPARAM)&tie );
705
706     heap_free(tabText);
707     return ret;
708 }
709
710 static BOOL HH_AddNavigationPane(HHInfo *info)
711 {
712     HWND hWnd, hwndTabCtrl;
713     HWND hwndParent = info->WinType.hwndHelp;
714     DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE;
715     DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
716     RECT rc;
717
718     NP_GetNavigationRect(info, &rc);
719
720     hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
721                            rc.left, rc.top, rc.right, rc.bottom,
722                            hwndParent, NULL, hhctrl_hinstance, NULL);
723     if (!hWnd)
724         return FALSE;
725
726     SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)info);
727
728     hwndTabCtrl = CreateWindowExW(dwExStyles, WC_TABCONTROLW, szEmpty, dwStyles,
729                                   0, TAB_TOP_PADDING,
730                                   rc.right - TAB_RIGHT_PADDING,
731                                   rc.bottom - TAB_TOP_PADDING,
732                                   hWnd, NULL, hhctrl_hinstance, NULL);
733     if (!hwndTabCtrl)
734         return FALSE;
735
736     if (*info->WinType.pszToc)
737         info->tabs[TAB_CONTENTS].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_CONTENTS);
738
739     if (*info->WinType.pszIndex)
740         info->tabs[TAB_INDEX].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_INDEX);
741
742     if (info->WinType.fsWinProperties & HHWIN_PROP_TAB_SEARCH)
743         info->tabs[TAB_SEARCH].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_SEARCH);
744
745     if (info->WinType.fsWinProperties & HHWIN_PROP_TAB_FAVORITES)
746         info->tabs[TAB_FAVORITES].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_FAVORITES);
747
748     SendMessageW(hwndTabCtrl, WM_SETFONT, (WPARAM)info->hFont, TRUE);
749
750     info->hwndTabCtrl = hwndTabCtrl;
751     info->WinType.hwndNavigation = hWnd;
752     return TRUE;
753 }
754
755 /* HTML Pane */
756
757 static void HP_GetHTMLRect(HHInfo *info, RECT *rc)
758 {
759     RECT rectTB, rectWND, rectNP, rectSB;
760
761     GetClientRect(info->WinType.hwndHelp, &rectWND);
762     GetClientRect(info->WinType.hwndToolBar, &rectTB);
763     GetClientRect(info->WinType.hwndNavigation, &rectNP);
764     GetClientRect(info->hwndSizeBar, &rectSB);
765
766     rc->left = rectNP.right + rectSB.right;
767     rc->top = rectTB.bottom;
768     rc->right = rectWND.right - rc->left;
769     rc->bottom = rectWND.bottom - rectTB.bottom;
770 }
771
772 static BOOL HH_AddHTMLPane(HHInfo *pHHInfo)
773 {
774     HWND hWnd;
775     HWND hwndParent = pHHInfo->WinType.hwndHelp;
776     DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN;
777     DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_CLIENTEDGE;
778     RECT rc;
779
780     HP_GetHTMLRect(pHHInfo, &rc);
781
782     hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
783                            rc.left, rc.top, rc.right, rc.bottom,
784                            hwndParent, NULL, hhctrl_hinstance, NULL);
785     if (!hWnd)
786         return FALSE;
787
788     if (!InitWebBrowser(pHHInfo, hWnd))
789         return FALSE;
790
791     /* store the pointer to the HH info struct */
792     SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
793
794     ShowWindow(hWnd, SW_SHOW);
795     UpdateWindow(hWnd);
796
797     pHHInfo->WinType.hwndHTML = hWnd;
798     return TRUE;
799 }
800
801 static BOOL AddContentTab(HHInfo *info)
802 {
803     if(info->tabs[TAB_CONTENTS].id == -1)
804         return TRUE; /* No "Contents" tab */
805     info->tabs[TAB_CONTENTS].hwnd = CreateWindowExW(WS_EX_CLIENTEDGE, WC_TREEVIEWW,
806            szEmpty, WS_CHILD | WS_BORDER | 0x25, 50, 50, 100, 100,
807            info->WinType.hwndNavigation, NULL, hhctrl_hinstance, NULL);
808     if(!info->tabs[TAB_CONTENTS].hwnd) {
809         ERR("Could not create treeview control\n");
810         return FALSE;
811     }
812
813     ResizeTabChild(info, TAB_CONTENTS);
814     ShowWindow(info->tabs[TAB_CONTENTS].hwnd, SW_SHOW);
815
816     return TRUE;
817 }
818
819 static BOOL AddIndexTab(HHInfo *info)
820 {
821     char hidden_column[] = "Column";
822     LVCOLUMNA lvc;
823
824     if(info->tabs[TAB_INDEX].id == -1)
825         return TRUE; /* No "Index" tab */
826     info->tabs[TAB_INDEX].hwnd = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW,
827            szEmpty, WS_CHILD | WS_BORDER | LVS_SINGLESEL | LVS_REPORT | LVS_NOCOLUMNHEADER, 50, 50, 100, 100,
828            info->WinType.hwndNavigation, NULL, hhctrl_hinstance, NULL);
829     if(!info->tabs[TAB_INDEX].hwnd) {
830         ERR("Could not create ListView control\n");
831         return FALSE;
832     }
833     memset(&lvc, 0, sizeof(lvc));
834     lvc.mask = LVCF_TEXT;
835     lvc.pszText = hidden_column;
836     if(SendMessageW(info->tabs[TAB_INDEX].hwnd, LVM_INSERTCOLUMNA, 0, (LPARAM) &lvc) == -1)
837     {
838         ERR("Could not create ListView column\n");
839         return FALSE;
840     }
841
842     ResizeTabChild(info, TAB_INDEX);
843     ShowWindow(info->tabs[TAB_INDEX].hwnd, SW_HIDE);
844
845     return TRUE;
846 }
847
848 /* The Index tab's sub-topic popup */
849
850 static void ResizePopupChild(HHInfo *info)
851 {
852     int scroll_width = GetSystemMetrics(SM_CXVSCROLL);
853     int border_width = GetSystemMetrics(SM_CXBORDER);
854     int edge_width = GetSystemMetrics(SM_CXEDGE);
855     INT width, height;
856     RECT rect;
857
858     if(!info)
859         return;
860
861     GetClientRect(info->popup.hwndPopup, &rect);
862     SetWindowPos(info->popup.hwndCallback, HWND_TOP, 0, 0,
863                  rect.right, rect.bottom, SWP_NOMOVE);
864
865     rect.left = TAB_MARGIN;
866     rect.top = TAB_TOP_PADDING + TAB_MARGIN;
867     rect.right -= TAB_RIGHT_PADDING + TAB_MARGIN;
868     rect.bottom -= TAB_MARGIN;
869     width = rect.right-rect.left;
870     height = rect.bottom-rect.top;
871
872     SetWindowPos(info->popup.hwndList, NULL, rect.left, rect.top, width, height,
873                  SWP_NOZORDER | SWP_NOACTIVATE);
874
875     SendMessageW(info->popup.hwndList, LVM_SETCOLUMNWIDTH, 0,
876                  width-scroll_width-2*border_width-2*edge_width);
877 }
878
879 static LRESULT CALLBACK HelpPopup_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
880 {
881     HHInfo *info = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
882
883     switch (message)
884     {
885     case WM_SIZE:
886         ResizePopupChild(info);
887         return 0;
888     case WM_DESTROY:
889         DestroyWindow(hWnd);
890         return 0;
891     case WM_CLOSE:
892         ShowWindow(hWnd, SW_HIDE);
893         return 0;
894
895     default:
896         return DefWindowProcW(hWnd, message, wParam, lParam);
897     }
898
899     return 0;
900 }
901
902 static LRESULT CALLBACK PopupChild_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
903 {
904     switch (message)
905     {
906     case WM_NOTIFY: {
907         NMHDR *nmhdr = (NMHDR*)lParam;
908         if(nmhdr->code == NM_DBLCLK) {
909             HHInfo *info = (HHInfo*)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
910             IndexSubItem *iter;
911
912             if(info == 0 || lParam == 0)
913                 return 0;
914             iter = (IndexSubItem*) ((NMITEMACTIVATE *)lParam)->lParam;
915             if(iter == 0)
916                 return 0;
917             NavigateToChm(info, info->index->merge.chm_file, iter->local);
918             ShowWindow(info->popup.hwndPopup, SW_HIDE);
919             return 0;
920         }
921         break;
922     }
923     default:
924         return DefWindowProcW(hWnd, message, wParam, lParam);
925     }
926
927     return 0;
928 }
929
930 static BOOL AddIndexPopup(HHInfo *info)
931 {
932     static const WCHAR szPopupChildClass[] = {'H','H',' ','P','o','p','u','p',' ','C','h','i','l','d',0};
933     static const WCHAR windowCaptionW[] = {'S','e','l','e','c','t',' ','T','o','p','i','c',':',0};
934     static const WCHAR windowClassW[] = {'H','H',' ','P','o','p','u','p',0};
935     HWND hwndList, hwndPopup, hwndCallback;
936     char hidden_column[] = "Column";
937     WNDCLASSEXW wcex;
938     LVCOLUMNA lvc;
939
940     if(info->tabs[TAB_INDEX].id == -1)
941         return TRUE; /* No "Index" tab */
942
943     wcex.cbSize         = sizeof(WNDCLASSEXW);
944     wcex.style          = CS_HREDRAW | CS_VREDRAW;
945     wcex.lpfnWndProc    = HelpPopup_WndProc;
946     wcex.cbClsExtra     = 0;
947     wcex.cbWndExtra     = 0;
948     wcex.hInstance      = hhctrl_hinstance;
949     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
950     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
951     wcex.hbrBackground  = (HBRUSH)(COLOR_MENU + 1);
952     wcex.lpszMenuName   = NULL;
953     wcex.lpszClassName  = windowClassW;
954     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
955     RegisterClassExW(&wcex);
956
957     wcex.cbSize         = sizeof(WNDCLASSEXW);
958     wcex.style          = 0;
959     wcex.lpfnWndProc    = PopupChild_WndProc;
960     wcex.cbClsExtra     = 0;
961     wcex.cbWndExtra     = 0;
962     wcex.hInstance      = hhctrl_hinstance;
963     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
964     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
965     wcex.hbrBackground  = (HBRUSH)(COLOR_BTNFACE + 1);
966     wcex.lpszMenuName   = NULL;
967     wcex.lpszClassName  = szPopupChildClass;
968     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
969     RegisterClassExW(&wcex);
970
971     hwndPopup = CreateWindowExW(WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_APPWINDOW
972                                  | WS_EX_WINDOWEDGE | WS_EX_RIGHTSCROLLBAR,
973                                 windowClassW, windowCaptionW, WS_POPUPWINDOW
974                                  | WS_OVERLAPPEDWINDOW | WS_VISIBLE
975                                  | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, CW_USEDEFAULT,
976                                 CW_USEDEFAULT, 300, 200, info->WinType.hwndHelp,
977                                 NULL, hhctrl_hinstance, NULL);
978     if (!hwndPopup)
979         return FALSE;
980
981     hwndCallback = CreateWindowExW(WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR,
982                                    szPopupChildClass, szEmpty, WS_CHILDWINDOW | WS_VISIBLE,
983                                    0, 0, 0, 0,
984                                    hwndPopup, NULL, hhctrl_hinstance, NULL);
985     if (!hwndCallback)
986         return FALSE;
987
988     ShowWindow(hwndPopup, SW_HIDE);
989     hwndList = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW, szEmpty,
990                                WS_CHILD | WS_BORDER | LVS_SINGLESEL | LVS_REPORT
991                                 | LVS_NOCOLUMNHEADER, 50, 50, 100, 100,
992                                hwndCallback, NULL, hhctrl_hinstance, NULL);
993     if(!hwndList) {
994         ERR("Could not create popup ListView control\n");
995         return FALSE;
996     }
997     memset(&lvc, 0, sizeof(lvc));
998     lvc.mask = LVCF_TEXT;
999     lvc.pszText = hidden_column;
1000     if(SendMessageW(hwndList, LVM_INSERTCOLUMNA, 0, (LPARAM) &lvc) == -1)
1001     {
1002         ERR("Could not create popup ListView column\n");
1003         return FALSE;
1004     }
1005
1006     info->popup.hwndCallback = hwndCallback;
1007     info->popup.hwndPopup = hwndPopup;
1008     info->popup.hwndList = hwndList;
1009     SetWindowLongPtrW(hwndPopup, GWLP_USERDATA, (LONG_PTR)info);
1010     SetWindowLongPtrW(hwndCallback, GWLP_USERDATA, (LONG_PTR)info);
1011
1012     ResizePopupChild(info);
1013     ShowWindow(hwndList, SW_SHOW);
1014
1015     return TRUE;
1016 }
1017
1018 /* Viewer Window */
1019
1020 static LRESULT Help_OnSize(HWND hWnd)
1021 {
1022     HHInfo *pHHInfo = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
1023     DWORD dwSize;
1024     RECT rc;
1025
1026     if (!pHHInfo)
1027         return 0;
1028
1029     NP_GetNavigationRect(pHHInfo, &rc);
1030     SetWindowPos(pHHInfo->WinType.hwndNavigation, HWND_TOP, 0, 0,
1031                  rc.right, rc.bottom, SWP_NOMOVE);
1032
1033     SB_GetSizeBarRect(pHHInfo, &rc);
1034     SetWindowPos(pHHInfo->hwndSizeBar, HWND_TOP, rc.left, rc.top,
1035                  rc.right, rc.bottom, SWP_SHOWWINDOW);
1036
1037     HP_GetHTMLRect(pHHInfo, &rc);
1038     SetWindowPos(pHHInfo->WinType.hwndHTML, HWND_TOP, rc.left, rc.top,
1039                  rc.right, rc.bottom, SWP_SHOWWINDOW);
1040
1041     /* Resize browser window taking the frame size into account */
1042     dwSize = GetSystemMetrics(SM_CXFRAME);
1043     ResizeWebBrowser(pHHInfo, rc.right - dwSize, rc.bottom - dwSize);
1044
1045     return 0;
1046 }
1047
1048 static LRESULT CALLBACK Help_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1049 {
1050     switch (message)
1051     {
1052     case WM_COMMAND:
1053         if (HIWORD(wParam) == BN_CLICKED)
1054             TB_OnClick(hWnd, LOWORD(wParam));
1055         break;
1056     case WM_SIZE:
1057         return Help_OnSize(hWnd);
1058     case WM_CLOSE:
1059         ReleaseHelpViewer((HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA));
1060         return 0;
1061     case WM_DESTROY:
1062         if(hh_process)
1063             PostQuitMessage(0);
1064         break;
1065
1066     default:
1067         return DefWindowProcW(hWnd, message, wParam, lParam);
1068     }
1069
1070     return 0;
1071 }
1072
1073 static BOOL HH_CreateHelpWindow(HHInfo *info)
1074 {
1075     HWND hWnd;
1076     RECT winPos = info->WinType.rcWindowPos;
1077     WNDCLASSEXW wcex;
1078     DWORD dwStyles, dwExStyles;
1079     DWORD x, y, width, height;
1080
1081     static const WCHAR windowClassW[] = {
1082         'H','H',' ', 'P','a','r','e','n','t',0
1083     };
1084
1085     wcex.cbSize         = sizeof(WNDCLASSEXW);
1086     wcex.style          = CS_HREDRAW | CS_VREDRAW;
1087     wcex.lpfnWndProc    = Help_WndProc;
1088     wcex.cbClsExtra     = 0;
1089     wcex.cbWndExtra     = 0;
1090     wcex.hInstance      = hhctrl_hinstance;
1091     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1092     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
1093     wcex.hbrBackground  = (HBRUSH)(COLOR_MENU + 1);
1094     wcex.lpszMenuName   = NULL;
1095     wcex.lpszClassName  = windowClassW;
1096     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1097
1098     RegisterClassExW(&wcex);
1099
1100     /* Read in window parameters if available */
1101     if (info->WinType.fsValidMembers & HHWIN_PARAM_STYLES)
1102         dwStyles = info->WinType.dwStyles | WS_OVERLAPPEDWINDOW;
1103     else
1104         dwStyles = WS_OVERLAPPEDWINDOW | WS_VISIBLE |
1105                    WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
1106
1107     if (info->WinType.fsValidMembers & HHWIN_PARAM_EXSTYLES)
1108         dwExStyles = info->WinType.dwExStyles;
1109     else
1110         dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_APPWINDOW |
1111                      WS_EX_WINDOWEDGE | WS_EX_RIGHTSCROLLBAR;
1112
1113     if (info->WinType.fsValidMembers & HHWIN_PARAM_RECT)
1114     {
1115         x = winPos.left;
1116         y = winPos.top;
1117         width = winPos.right - x;
1118         height = winPos.bottom - y;
1119     }
1120     else
1121     {
1122         x = WINTYPE_DEFAULT_X;
1123         y = WINTYPE_DEFAULT_Y;
1124         width = WINTYPE_DEFAULT_WIDTH;
1125         height = WINTYPE_DEFAULT_HEIGHT;
1126     }
1127
1128     hWnd = CreateWindowExW(dwExStyles, windowClassW, info->WinType.pszCaption,
1129                            dwStyles, x, y, width, height, NULL, NULL, hhctrl_hinstance, NULL);
1130     if (!hWnd)
1131         return FALSE;
1132
1133     ShowWindow(hWnd, SW_SHOW);
1134     UpdateWindow(hWnd);
1135
1136     /* store the pointer to the HH info struct */
1137     SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)info);
1138
1139     info->WinType.hwndHelp = hWnd;
1140     return TRUE;
1141 }
1142
1143 static void HH_CreateFont(HHInfo *pHHInfo)
1144 {
1145     LOGFONTW lf;
1146
1147     GetObjectW(GetStockObject(ANSI_VAR_FONT), sizeof(LOGFONTW), &lf);
1148     lf.lfWeight = FW_NORMAL;
1149     lf.lfItalic = FALSE;
1150     lf.lfUnderline = FALSE;
1151
1152     pHHInfo->hFont = CreateFontIndirectW(&lf);
1153 }
1154
1155 static void HH_InitRequiredControls(DWORD dwControls)
1156 {
1157     INITCOMMONCONTROLSEX icex;
1158
1159     icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
1160     icex.dwICC = dwControls;
1161     InitCommonControlsEx(&icex);
1162 }
1163
1164 /* Creates the whole package */
1165 static BOOL CreateViewer(HHInfo *pHHInfo)
1166 {
1167     HH_CreateFont(pHHInfo);
1168
1169     if (!HH_CreateHelpWindow(pHHInfo))
1170         return FALSE;
1171
1172     HH_InitRequiredControls(ICC_BAR_CLASSES);
1173
1174     if (!HH_AddToolbar(pHHInfo))
1175         return FALSE;
1176
1177     HH_RegisterChildWndClass(pHHInfo);
1178
1179     if (!HH_AddNavigationPane(pHHInfo))
1180         return FALSE;
1181
1182     HH_RegisterSizeBarClass(pHHInfo);
1183
1184     if (!HH_AddSizeBar(pHHInfo))
1185         return FALSE;
1186
1187     if (!HH_AddHTMLPane(pHHInfo))
1188         return FALSE;
1189
1190     if (!AddContentTab(pHHInfo))
1191         return FALSE;
1192
1193     if (!AddIndexTab(pHHInfo))
1194         return FALSE;
1195
1196     if (!AddIndexPopup(pHHInfo))
1197         return FALSE;
1198
1199     InitContent(pHHInfo);
1200     InitIndex(pHHInfo);
1201
1202     return TRUE;
1203 }
1204
1205 void ReleaseHelpViewer(HHInfo *info)
1206 {
1207     TRACE("(%p)\n", info);
1208
1209     if (!info)
1210         return;
1211
1212     /* Free allocated strings */
1213     heap_free(info->pszType);
1214     heap_free(info->pszCaption);
1215     heap_free(info->pszToc);
1216     heap_free(info->pszIndex);
1217     heap_free(info->pszFile);
1218     heap_free(info->pszHome);
1219     heap_free(info->pszJump1);
1220     heap_free(info->pszJump2);
1221     heap_free(info->pszUrlJump1);
1222     heap_free(info->pszUrlJump2);
1223
1224     if (info->pCHMInfo)
1225         CloseCHM(info->pCHMInfo);
1226
1227     ReleaseWebBrowser(info);
1228     ReleaseContent(info);
1229     ReleaseIndex(info);
1230
1231     if(info->WinType.hwndHelp)
1232         DestroyWindow(info->WinType.hwndHelp);
1233
1234     heap_free(info);
1235     OleUninitialize();
1236 }
1237
1238 HHInfo *CreateHelpViewer(LPCWSTR filename)
1239 {
1240     HHInfo *info = heap_alloc_zero(sizeof(HHInfo));
1241     int i;
1242
1243     /* Set the invalid tab ID (-1) as the default value for all
1244      * of the tabs, this matches a failed TCM_INSERTITEM call.
1245      */
1246     for(i=0;i<sizeof(info->tabs)/sizeof(HHTab);i++)
1247         info->tabs[i].id = -1;
1248
1249     OleInitialize(NULL);
1250
1251     info->pCHMInfo = OpenCHM(filename);
1252     if(!info->pCHMInfo) {
1253         ReleaseHelpViewer(info);
1254         return NULL;
1255     }
1256
1257     if (!LoadWinTypeFromCHM(info)) {
1258         ReleaseHelpViewer(info);
1259         return NULL;
1260     }
1261
1262     if(!CreateViewer(info)) {
1263         ReleaseHelpViewer(info);
1264         return NULL;
1265     }
1266
1267     return info;
1268 }