wintrust: Return error directly from WINTRUST_VerifySigner.
[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(HWND hwnd, void *user_data)
401 {
402     HHInfo *info = (HHInfo*)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
403     LPCWSTR chmfile = NULL, name = NULL, local = NULL;
404     ContentItem *citer;
405     IndexItem *iiter;
406
407     if(!user_data || !info)
408         return 0;
409
410     switch (info->current_tab)
411     {
412     case TAB_CONTENTS:
413         citer = (ContentItem *) user_data;
414         name = citer->name;
415         local = citer->local;
416         while(citer) {
417             if(citer->merge.chm_file) {
418                 chmfile = citer->merge.chm_file;
419                 break;
420             }
421             citer = citer->parent;
422         }
423         chmfile = citer->merge.chm_file;
424         break;
425     case TAB_INDEX:
426         iiter = (IndexItem *) user_data;
427         if(iiter->nItems == 0) {
428             FIXME("No entries for this item!\n");
429             return 0;
430         }
431         if(iiter->nItems > 1) {
432             int i = 0;
433             LVITEMW lvi;
434
435             SendMessageW(info->popup.hwndList, LVM_DELETEALLITEMS, 0, 0);
436             for(i=0;i<iiter->nItems;i++) {
437                 IndexSubItem *item = &iiter->items[i];
438                 WCHAR *name = iiter->keyword;
439
440                 if(item->name)
441                     name = item->name;
442                 memset(&lvi, 0, sizeof(lvi));
443                 lvi.iItem = i;
444                 lvi.mask = LVIF_TEXT|LVIF_PARAM;
445                 lvi.cchTextMax = strlenW(name)+1;
446                 lvi.pszText = name;
447                 lvi.lParam = (LPARAM) item;
448                 SendMessageW(info->popup.hwndList, LVM_INSERTITEMW, 0, (LPARAM)&lvi);
449             }
450             ShowWindow(info->popup.hwndPopup, SW_SHOW);
451             return 0;
452         }
453         name = iiter->items[0].name;
454         local = iiter->items[0].local;
455         chmfile = iiter->merge.chm_file;
456         break;
457     default:
458         FIXME("Unhandled operation for this tab!\n");
459         return 0;
460     }
461
462     TRACE("name %s loal %s\n", debugstr_w(name), debugstr_w(local));
463
464     NavigateToChm(info, chmfile, local);
465     return 0;
466 }
467
468 static LRESULT CALLBACK Child_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
469 {
470     switch (message)
471     {
472     case WM_PAINT:
473         return Child_OnPaint(hWnd);
474     case WM_SIZE:
475         return Child_OnSize(hWnd);
476     case WM_NOTIFY: {
477         NMHDR *nmhdr = (NMHDR*)lParam;
478         switch(nmhdr->code) {
479         case TCN_SELCHANGE:
480             return OnTabChange(hWnd);
481         case TVN_SELCHANGEDW:
482             return OnTopicChange(hWnd, (void*)((NMTREEVIEWW *)lParam)->itemNew.lParam);
483         case NM_DBLCLK:
484             return OnTopicChange(hWnd, (void*)((NMITEMACTIVATE *)lParam)->lParam);
485         }
486         break;
487     }
488     default:
489         return DefWindowProcW(hWnd, message, wParam, lParam);
490     }
491
492     return 0;
493 }
494
495 static void HH_RegisterChildWndClass(HHInfo *pHHInfo)
496 {
497     WNDCLASSEXW wcex;
498
499     wcex.cbSize         = sizeof(WNDCLASSEXW);
500     wcex.style          = 0;
501     wcex.lpfnWndProc    = Child_WndProc;
502     wcex.cbClsExtra     = 0;
503     wcex.cbWndExtra     = 0;
504     wcex.hInstance      = hhctrl_hinstance;
505     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
506     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
507     wcex.hbrBackground  = (HBRUSH)(COLOR_BTNFACE + 1);
508     wcex.lpszMenuName   = NULL;
509     wcex.lpszClassName  = szChildClass;
510     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
511
512     RegisterClassExW(&wcex);
513 }
514
515 /* Toolbar */
516
517 #define ICON_SIZE   20
518
519 static void TB_OnClick(HWND hWnd, DWORD dwID)
520 {
521     HHInfo *info = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
522
523     switch (dwID)
524     {
525         case IDTB_STOP:
526             DoPageAction(info, WB_STOP);
527             break;
528         case IDTB_REFRESH:
529             DoPageAction(info, WB_REFRESH);
530             break;
531         case IDTB_BACK:
532             DoPageAction(info, WB_GOBACK);
533             break;
534         case IDTB_HOME:
535             NavigateToChm(info, info->pCHMInfo->szFile, info->WinType.pszHome);
536             break;
537         case IDTB_FORWARD:
538             DoPageAction(info, WB_GOFORWARD);
539             break;
540         case IDTB_EXPAND:
541         case IDTB_CONTRACT:
542         case IDTB_SYNC:
543         case IDTB_PRINT:
544         case IDTB_OPTIONS:
545         case IDTB_BROWSE_FWD:
546         case IDTB_BROWSE_BACK:
547         case IDTB_JUMP1:
548         case IDTB_JUMP2:
549         case IDTB_CUSTOMIZE:
550         case IDTB_ZOOM:
551         case IDTB_TOC_NEXT:
552         case IDTB_TOC_PREV:
553             break;
554     }
555 }
556
557 static void TB_AddButton(TBBUTTON *pButtons, DWORD dwIndex, DWORD dwID)
558 {
559     /* FIXME: Load the correct button bitmaps */
560     pButtons[dwIndex].iBitmap = STD_PRINT;
561     pButtons[dwIndex].idCommand = dwID;
562     pButtons[dwIndex].fsState = TBSTATE_ENABLED;
563     pButtons[dwIndex].fsStyle = BTNS_BUTTON;
564     pButtons[dwIndex].dwData = 0;
565     pButtons[dwIndex].iString = 0;
566 }
567
568 static void TB_AddButtonsFromFlags(TBBUTTON *pButtons, DWORD dwButtonFlags, LPDWORD pdwNumButtons)
569 {
570     *pdwNumButtons = 0;
571
572     if (dwButtonFlags & HHWIN_BUTTON_EXPAND)
573         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_EXPAND);
574
575     if (dwButtonFlags & HHWIN_BUTTON_BACK)
576         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_BACK);
577
578     if (dwButtonFlags & HHWIN_BUTTON_FORWARD)
579         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_FORWARD);
580
581     if (dwButtonFlags & HHWIN_BUTTON_STOP)
582         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_STOP);
583
584     if (dwButtonFlags & HHWIN_BUTTON_REFRESH)
585         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_REFRESH);
586
587     if (dwButtonFlags & HHWIN_BUTTON_HOME)
588         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_HOME);
589
590     if (dwButtonFlags & HHWIN_BUTTON_SYNC)
591         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_SYNC);
592
593     if (dwButtonFlags & HHWIN_BUTTON_OPTIONS)
594         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_OPTIONS);
595
596     if (dwButtonFlags & HHWIN_BUTTON_PRINT)
597         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_PRINT);
598
599     if (dwButtonFlags & HHWIN_BUTTON_JUMP1)
600         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_JUMP1);
601
602     if (dwButtonFlags & HHWIN_BUTTON_JUMP2)
603         TB_AddButton(pButtons,(*pdwNumButtons)++, IDTB_JUMP2);
604
605     if (dwButtonFlags & HHWIN_BUTTON_ZOOM)
606         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_ZOOM);
607
608     if (dwButtonFlags & HHWIN_BUTTON_TOC_NEXT)
609         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_TOC_NEXT);
610
611     if (dwButtonFlags & HHWIN_BUTTON_TOC_PREV)
612         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_TOC_PREV);
613 }
614
615 static BOOL HH_AddToolbar(HHInfo *pHHInfo)
616 {
617     HWND hToolbar;
618     HWND hwndParent = pHHInfo->WinType.hwndHelp;
619     DWORD toolbarFlags;
620     TBBUTTON buttons[IDTB_TOC_PREV - IDTB_EXPAND];
621     TBADDBITMAP tbAB;
622     DWORD dwStyles, dwExStyles;
623     DWORD dwNumButtons, dwIndex;
624
625     if (pHHInfo->WinType.fsWinProperties & HHWIN_PARAM_TB_FLAGS)
626         toolbarFlags = pHHInfo->WinType.fsToolBarFlags;
627     else
628         toolbarFlags = HHWIN_DEF_BUTTONS;
629
630     TB_AddButtonsFromFlags(buttons, toolbarFlags, &dwNumButtons);
631
632     dwStyles = WS_CHILDWINDOW | WS_VISIBLE | TBSTYLE_FLAT |
633                TBSTYLE_WRAPABLE | TBSTYLE_TOOLTIPS | CCS_NODIVIDER;
634     dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
635
636     hToolbar = CreateWindowExW(dwExStyles, TOOLBARCLASSNAMEW, NULL, dwStyles,
637                                0, 0, 0, 0, hwndParent, NULL,
638                                hhctrl_hinstance, NULL);
639     if (!hToolbar)
640         return FALSE;
641
642     SendMessageW(hToolbar, TB_SETBITMAPSIZE, 0, MAKELONG(ICON_SIZE, ICON_SIZE));
643     SendMessageW(hToolbar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
644     SendMessageW(hToolbar, WM_SETFONT, (WPARAM)pHHInfo->hFont, TRUE);
645
646     /* FIXME: Load correct icons for all buttons */
647     tbAB.hInst = HINST_COMMCTRL;
648     tbAB.nID = IDB_STD_LARGE_COLOR;
649     SendMessageW(hToolbar, TB_ADDBITMAP, 0, (LPARAM)&tbAB);
650
651     for (dwIndex = 0; dwIndex < dwNumButtons; dwIndex++)
652     {
653         LPWSTR szBuf = HH_LoadString(buttons[dwIndex].idCommand);
654         DWORD dwLen = strlenW(szBuf);
655         szBuf[dwLen + 1] = 0; /* Double-null terminate */
656
657         buttons[dwIndex].iString = (DWORD)SendMessageW(hToolbar, TB_ADDSTRINGW, 0, (LPARAM)szBuf);
658         heap_free(szBuf);
659     }
660
661     SendMessageW(hToolbar, TB_ADDBUTTONSW, dwNumButtons, (LPARAM)buttons);
662     SendMessageW(hToolbar, TB_AUTOSIZE, 0, 0);
663     ShowWindow(hToolbar, SW_SHOW);
664
665     pHHInfo->WinType.hwndToolBar = hToolbar;
666     return TRUE;
667 }
668
669 /* Navigation Pane */
670
671 static void NP_GetNavigationRect(HHInfo *pHHInfo, RECT *rc)
672 {
673     HWND hwndParent = pHHInfo->WinType.hwndHelp;
674     HWND hwndToolbar = pHHInfo->WinType.hwndToolBar;
675     RECT rectWND, rectTB;
676
677     GetClientRect(hwndParent, &rectWND);
678     GetClientRect(hwndToolbar, &rectTB);
679
680     rc->left = 0;
681     rc->top = rectTB.bottom;
682     rc->bottom = rectWND.bottom - rectTB.bottom;
683
684     if (!(pHHInfo->WinType.fsValidMembers & HHWIN_PARAM_NAV_WIDTH) &&
685           pHHInfo->WinType.iNavWidth == 0)
686     {
687         pHHInfo->WinType.iNavWidth = WINTYPE_DEFAULT_NAVWIDTH;
688     }
689
690     rc->right = pHHInfo->WinType.iNavWidth;
691 }
692
693 static DWORD NP_CreateTab(HINSTANCE hInstance, HWND hwndTabCtrl, DWORD index)
694 {
695     TCITEMW tie;
696     LPWSTR tabText = HH_LoadString(index);
697     DWORD ret;
698
699     tie.mask = TCIF_TEXT;
700     tie.pszText = tabText;
701
702     ret = SendMessageW( hwndTabCtrl, TCM_INSERTITEMW, index, (LPARAM)&tie );
703
704     heap_free(tabText);
705     return ret;
706 }
707
708 static BOOL HH_AddNavigationPane(HHInfo *info)
709 {
710     HWND hWnd, hwndTabCtrl;
711     HWND hwndParent = info->WinType.hwndHelp;
712     DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE;
713     DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
714     RECT rc;
715
716     NP_GetNavigationRect(info, &rc);
717
718     hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
719                            rc.left, rc.top, rc.right, rc.bottom,
720                            hwndParent, NULL, hhctrl_hinstance, NULL);
721     if (!hWnd)
722         return FALSE;
723
724     SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)info);
725
726     hwndTabCtrl = CreateWindowExW(dwExStyles, WC_TABCONTROLW, szEmpty, dwStyles,
727                                   0, TAB_TOP_PADDING,
728                                   rc.right - TAB_RIGHT_PADDING,
729                                   rc.bottom - TAB_TOP_PADDING,
730                                   hWnd, NULL, hhctrl_hinstance, NULL);
731     if (!hwndTabCtrl)
732         return FALSE;
733
734     if (*info->WinType.pszToc)
735         info->tabs[TAB_CONTENTS].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_CONTENTS);
736
737     if (*info->WinType.pszIndex)
738         info->tabs[TAB_INDEX].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_INDEX);
739
740     if (info->WinType.fsWinProperties & HHWIN_PROP_TAB_SEARCH)
741         info->tabs[TAB_SEARCH].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_SEARCH);
742
743     if (info->WinType.fsWinProperties & HHWIN_PROP_TAB_FAVORITES)
744         info->tabs[TAB_FAVORITES].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_FAVORITES);
745
746     SendMessageW(hwndTabCtrl, WM_SETFONT, (WPARAM)info->hFont, TRUE);
747
748     info->hwndTabCtrl = hwndTabCtrl;
749     info->WinType.hwndNavigation = hWnd;
750     return TRUE;
751 }
752
753 /* HTML Pane */
754
755 static void HP_GetHTMLRect(HHInfo *info, RECT *rc)
756 {
757     RECT rectTB, rectWND, rectNP, rectSB;
758
759     GetClientRect(info->WinType.hwndHelp, &rectWND);
760     GetClientRect(info->WinType.hwndToolBar, &rectTB);
761     GetClientRect(info->WinType.hwndNavigation, &rectNP);
762     GetClientRect(info->hwndSizeBar, &rectSB);
763
764     rc->left = rectNP.right + rectSB.right;
765     rc->top = rectTB.bottom;
766     rc->right = rectWND.right - rc->left;
767     rc->bottom = rectWND.bottom - rectTB.bottom;
768 }
769
770 static BOOL HH_AddHTMLPane(HHInfo *pHHInfo)
771 {
772     HWND hWnd;
773     HWND hwndParent = pHHInfo->WinType.hwndHelp;
774     DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN;
775     DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_CLIENTEDGE;
776     RECT rc;
777
778     HP_GetHTMLRect(pHHInfo, &rc);
779
780     hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
781                            rc.left, rc.top, rc.right, rc.bottom,
782                            hwndParent, NULL, hhctrl_hinstance, NULL);
783     if (!hWnd)
784         return FALSE;
785
786     if (!InitWebBrowser(pHHInfo, hWnd))
787         return FALSE;
788
789     /* store the pointer to the HH info struct */
790     SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
791
792     ShowWindow(hWnd, SW_SHOW);
793     UpdateWindow(hWnd);
794
795     pHHInfo->WinType.hwndHTML = hWnd;
796     return TRUE;
797 }
798
799 static BOOL AddContentTab(HHInfo *info)
800 {
801     if(info->tabs[TAB_CONTENTS].id == -1)
802         return TRUE; /* No "Contents" tab */
803     info->tabs[TAB_CONTENTS].hwnd = CreateWindowExW(WS_EX_CLIENTEDGE, WC_TREEVIEWW,
804            szEmpty, WS_CHILD | WS_BORDER | 0x25, 50, 50, 100, 100,
805            info->WinType.hwndNavigation, NULL, hhctrl_hinstance, NULL);
806     if(!info->tabs[TAB_CONTENTS].hwnd) {
807         ERR("Could not create treeview control\n");
808         return FALSE;
809     }
810
811     ResizeTabChild(info, TAB_CONTENTS);
812     ShowWindow(info->tabs[TAB_CONTENTS].hwnd, SW_SHOW);
813
814     return TRUE;
815 }
816
817 static BOOL AddIndexTab(HHInfo *info)
818 {
819     char hidden_column[] = "Column";
820     LVCOLUMNA lvc;
821
822     if(info->tabs[TAB_INDEX].id == -1)
823         return TRUE; /* No "Index" tab */
824     info->tabs[TAB_INDEX].hwnd = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW,
825            szEmpty, WS_CHILD | WS_BORDER | LVS_SINGLESEL | LVS_REPORT | LVS_NOCOLUMNHEADER, 50, 50, 100, 100,
826            info->WinType.hwndNavigation, NULL, hhctrl_hinstance, NULL);
827     if(!info->tabs[TAB_INDEX].hwnd) {
828         ERR("Could not create ListView control\n");
829         return FALSE;
830     }
831     memset(&lvc, 0, sizeof(lvc));
832     lvc.mask = LVCF_TEXT;
833     lvc.pszText = hidden_column;
834     if(SendMessageW(info->tabs[TAB_INDEX].hwnd, LVM_INSERTCOLUMNA, 0, (LPARAM) &lvc) == -1)
835     {
836         ERR("Could not create ListView column\n");
837         return FALSE;
838     }
839
840     ResizeTabChild(info, TAB_INDEX);
841     ShowWindow(info->tabs[TAB_INDEX].hwnd, SW_HIDE);
842
843     return TRUE;
844 }
845
846 /* The Index tab's sub-topic popup */
847
848 static void ResizePopupChild(HHInfo *info)
849 {
850     int scroll_width = GetSystemMetrics(SM_CXVSCROLL);
851     int border_width = GetSystemMetrics(SM_CXBORDER);
852     int edge_width = GetSystemMetrics(SM_CXEDGE);
853     INT width, height;
854     RECT rect;
855
856     if(!info)
857         return;
858
859     GetClientRect(info->popup.hwndPopup, &rect);
860     SetWindowPos(info->popup.hwndCallback, HWND_TOP, 0, 0,
861                  rect.right, rect.bottom, SWP_NOMOVE);
862
863     rect.left = TAB_MARGIN;
864     rect.top = TAB_TOP_PADDING + TAB_MARGIN;
865     rect.right -= TAB_RIGHT_PADDING + TAB_MARGIN;
866     rect.bottom -= TAB_MARGIN;
867     width = rect.right-rect.left;
868     height = rect.bottom-rect.top;
869
870     SetWindowPos(info->popup.hwndList, NULL, rect.left, rect.top, width, height,
871                  SWP_NOZORDER | SWP_NOACTIVATE);
872
873     SendMessageW(info->popup.hwndList, LVM_SETCOLUMNWIDTH, 0,
874                  width-scroll_width-2*border_width-2*edge_width);
875 }
876
877 static LRESULT CALLBACK HelpPopup_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
878 {
879     HHInfo *info = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
880
881     switch (message)
882     {
883     case WM_SIZE:
884         ResizePopupChild(info);
885         return 0;
886     case WM_DESTROY:
887         DestroyWindow(hWnd);
888         return 0;
889     case WM_CLOSE:
890         ShowWindow(hWnd, SW_HIDE);
891         return 0;
892
893     default:
894         return DefWindowProcW(hWnd, message, wParam, lParam);
895     }
896
897     return 0;
898 }
899
900 static LRESULT CALLBACK PopupChild_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
901 {
902     switch (message)
903     {
904     case WM_NOTIFY: {
905         NMHDR *nmhdr = (NMHDR*)lParam;
906         if(nmhdr->code == NM_DBLCLK) {
907             HHInfo *info = (HHInfo*)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
908             IndexSubItem *iter;
909
910             if(info == 0 || lParam == 0)
911                 return 0;
912             iter = (IndexSubItem*) ((NMITEMACTIVATE *)lParam)->lParam;
913             if(iter == 0)
914                 return 0;
915             NavigateToChm(info, info->index->merge.chm_file, iter->local);
916             ShowWindow(info->popup.hwndPopup, SW_HIDE);
917             return 0;
918         }
919         break;
920     }
921     default:
922         return DefWindowProcW(hWnd, message, wParam, lParam);
923     }
924
925     return 0;
926 }
927
928 static BOOL AddIndexPopup(HHInfo *info)
929 {
930     static const WCHAR szPopupChildClass[] = {'H','H',' ','P','o','p','u','p',' ','C','h','i','l','d',0};
931     static const WCHAR windowCaptionW[] = {'S','e','l','e','c','t',' ','T','o','p','i','c',':',0};
932     static const WCHAR windowClassW[] = {'H','H',' ','P','o','p','u','p',0};
933     HWND hwndList, hwndPopup, hwndCallback;
934     char hidden_column[] = "Column";
935     WNDCLASSEXW wcex;
936     LVCOLUMNA lvc;
937
938     if(info->tabs[TAB_INDEX].id == -1)
939         return TRUE; /* No "Index" tab */
940
941     wcex.cbSize         = sizeof(WNDCLASSEXW);
942     wcex.style          = CS_HREDRAW | CS_VREDRAW;
943     wcex.lpfnWndProc    = HelpPopup_WndProc;
944     wcex.cbClsExtra     = 0;
945     wcex.cbWndExtra     = 0;
946     wcex.hInstance      = hhctrl_hinstance;
947     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
948     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
949     wcex.hbrBackground  = (HBRUSH)(COLOR_MENU + 1);
950     wcex.lpszMenuName   = NULL;
951     wcex.lpszClassName  = windowClassW;
952     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
953     RegisterClassExW(&wcex);
954
955     wcex.cbSize         = sizeof(WNDCLASSEXW);
956     wcex.style          = 0;
957     wcex.lpfnWndProc    = PopupChild_WndProc;
958     wcex.cbClsExtra     = 0;
959     wcex.cbWndExtra     = 0;
960     wcex.hInstance      = hhctrl_hinstance;
961     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
962     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
963     wcex.hbrBackground  = (HBRUSH)(COLOR_BTNFACE + 1);
964     wcex.lpszMenuName   = NULL;
965     wcex.lpszClassName  = szPopupChildClass;
966     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
967     RegisterClassExW(&wcex);
968
969     hwndPopup = CreateWindowExW(WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_APPWINDOW
970                                  | WS_EX_WINDOWEDGE | WS_EX_RIGHTSCROLLBAR,
971                                 windowClassW, windowCaptionW, WS_POPUPWINDOW
972                                  | WS_OVERLAPPEDWINDOW | WS_VISIBLE
973                                  | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, CW_USEDEFAULT,
974                                 CW_USEDEFAULT, 300, 200, info->WinType.hwndHelp,
975                                 NULL, hhctrl_hinstance, NULL);
976     if (!hwndPopup)
977         return FALSE;
978
979     hwndCallback = CreateWindowExW(WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR,
980                                    szPopupChildClass, szEmpty, WS_CHILDWINDOW | WS_VISIBLE,
981                                    0, 0, 0, 0,
982                                    hwndPopup, NULL, hhctrl_hinstance, NULL);
983     if (!hwndCallback)
984         return FALSE;
985
986     ShowWindow(hwndPopup, SW_HIDE);
987     hwndList = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW, szEmpty,
988                                WS_CHILD | WS_BORDER | LVS_SINGLESEL | LVS_REPORT
989                                 | LVS_NOCOLUMNHEADER, 50, 50, 100, 100,
990                                hwndCallback, NULL, hhctrl_hinstance, NULL);
991     if(!hwndList) {
992         ERR("Could not create popup ListView control\n");
993         return FALSE;
994     }
995     memset(&lvc, 0, sizeof(lvc));
996     lvc.mask = LVCF_TEXT;
997     lvc.pszText = hidden_column;
998     if(SendMessageW(hwndList, LVM_INSERTCOLUMNA, 0, (LPARAM) &lvc) == -1)
999     {
1000         ERR("Could not create popup ListView column\n");
1001         return FALSE;
1002     }
1003
1004     info->popup.hwndCallback = hwndCallback;
1005     info->popup.hwndPopup = hwndPopup;
1006     info->popup.hwndList = hwndList;
1007     SetWindowLongPtrW(hwndPopup, GWLP_USERDATA, (LONG_PTR)info);
1008     SetWindowLongPtrW(hwndCallback, GWLP_USERDATA, (LONG_PTR)info);
1009
1010     ResizePopupChild(info);
1011     ShowWindow(hwndList, SW_SHOW);
1012
1013     return TRUE;
1014 }
1015
1016 /* Viewer Window */
1017
1018 static LRESULT Help_OnSize(HWND hWnd)
1019 {
1020     HHInfo *pHHInfo = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
1021     DWORD dwSize;
1022     RECT rc;
1023
1024     if (!pHHInfo)
1025         return 0;
1026
1027     NP_GetNavigationRect(pHHInfo, &rc);
1028     SetWindowPos(pHHInfo->WinType.hwndNavigation, HWND_TOP, 0, 0,
1029                  rc.right, rc.bottom, SWP_NOMOVE);
1030
1031     SB_GetSizeBarRect(pHHInfo, &rc);
1032     SetWindowPos(pHHInfo->hwndSizeBar, HWND_TOP, rc.left, rc.top,
1033                  rc.right, rc.bottom, SWP_SHOWWINDOW);
1034
1035     HP_GetHTMLRect(pHHInfo, &rc);
1036     SetWindowPos(pHHInfo->WinType.hwndHTML, HWND_TOP, rc.left, rc.top,
1037                  rc.right, rc.bottom, SWP_SHOWWINDOW);
1038
1039     /* Resize browser window taking the frame size into account */
1040     dwSize = GetSystemMetrics(SM_CXFRAME);
1041     ResizeWebBrowser(pHHInfo, rc.right - dwSize, rc.bottom - dwSize);
1042
1043     return 0;
1044 }
1045
1046 static LRESULT CALLBACK Help_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1047 {
1048     switch (message)
1049     {
1050     case WM_COMMAND:
1051         if (HIWORD(wParam) == BN_CLICKED)
1052             TB_OnClick(hWnd, LOWORD(wParam));
1053         break;
1054     case WM_SIZE:
1055         return Help_OnSize(hWnd);
1056     case WM_CLOSE:
1057         ReleaseHelpViewer((HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA));
1058         return 0;
1059     case WM_DESTROY:
1060         if(hh_process)
1061             PostQuitMessage(0);
1062         break;
1063
1064     default:
1065         return DefWindowProcW(hWnd, message, wParam, lParam);
1066     }
1067
1068     return 0;
1069 }
1070
1071 static BOOL HH_CreateHelpWindow(HHInfo *info)
1072 {
1073     HWND hWnd;
1074     RECT winPos = info->WinType.rcWindowPos;
1075     WNDCLASSEXW wcex;
1076     DWORD dwStyles, dwExStyles;
1077     DWORD x, y, width, height;
1078
1079     static const WCHAR windowClassW[] = {
1080         'H','H',' ', 'P','a','r','e','n','t',0
1081     };
1082
1083     wcex.cbSize         = sizeof(WNDCLASSEXW);
1084     wcex.style          = CS_HREDRAW | CS_VREDRAW;
1085     wcex.lpfnWndProc    = Help_WndProc;
1086     wcex.cbClsExtra     = 0;
1087     wcex.cbWndExtra     = 0;
1088     wcex.hInstance      = hhctrl_hinstance;
1089     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1090     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
1091     wcex.hbrBackground  = (HBRUSH)(COLOR_MENU + 1);
1092     wcex.lpszMenuName   = NULL;
1093     wcex.lpszClassName  = windowClassW;
1094     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1095
1096     RegisterClassExW(&wcex);
1097
1098     /* Read in window parameters if available */
1099     if (info->WinType.fsValidMembers & HHWIN_PARAM_STYLES)
1100         dwStyles = info->WinType.dwStyles | WS_OVERLAPPEDWINDOW;
1101     else
1102         dwStyles = WS_OVERLAPPEDWINDOW | WS_VISIBLE |
1103                    WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
1104
1105     if (info->WinType.fsValidMembers & HHWIN_PARAM_EXSTYLES)
1106         dwExStyles = info->WinType.dwExStyles;
1107     else
1108         dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_APPWINDOW |
1109                      WS_EX_WINDOWEDGE | WS_EX_RIGHTSCROLLBAR;
1110
1111     if (info->WinType.fsValidMembers & HHWIN_PARAM_RECT)
1112     {
1113         x = winPos.left;
1114         y = winPos.top;
1115         width = winPos.right - x;
1116         height = winPos.bottom - y;
1117     }
1118     else
1119     {
1120         x = WINTYPE_DEFAULT_X;
1121         y = WINTYPE_DEFAULT_Y;
1122         width = WINTYPE_DEFAULT_WIDTH;
1123         height = WINTYPE_DEFAULT_HEIGHT;
1124     }
1125
1126     hWnd = CreateWindowExW(dwExStyles, windowClassW, info->WinType.pszCaption,
1127                            dwStyles, x, y, width, height, NULL, NULL, hhctrl_hinstance, NULL);
1128     if (!hWnd)
1129         return FALSE;
1130
1131     ShowWindow(hWnd, SW_SHOW);
1132     UpdateWindow(hWnd);
1133
1134     /* store the pointer to the HH info struct */
1135     SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)info);
1136
1137     info->WinType.hwndHelp = hWnd;
1138     return TRUE;
1139 }
1140
1141 static void HH_CreateFont(HHInfo *pHHInfo)
1142 {
1143     LOGFONTW lf;
1144
1145     GetObjectW(GetStockObject(ANSI_VAR_FONT), sizeof(LOGFONTW), &lf);
1146     lf.lfWeight = FW_NORMAL;
1147     lf.lfItalic = FALSE;
1148     lf.lfUnderline = FALSE;
1149
1150     pHHInfo->hFont = CreateFontIndirectW(&lf);
1151 }
1152
1153 static void HH_InitRequiredControls(DWORD dwControls)
1154 {
1155     INITCOMMONCONTROLSEX icex;
1156
1157     icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
1158     icex.dwICC = dwControls;
1159     InitCommonControlsEx(&icex);
1160 }
1161
1162 /* Creates the whole package */
1163 static BOOL CreateViewer(HHInfo *pHHInfo)
1164 {
1165     HH_CreateFont(pHHInfo);
1166
1167     if (!HH_CreateHelpWindow(pHHInfo))
1168         return FALSE;
1169
1170     HH_InitRequiredControls(ICC_BAR_CLASSES);
1171
1172     if (!HH_AddToolbar(pHHInfo))
1173         return FALSE;
1174
1175     HH_RegisterChildWndClass(pHHInfo);
1176
1177     if (!HH_AddNavigationPane(pHHInfo))
1178         return FALSE;
1179
1180     HH_RegisterSizeBarClass(pHHInfo);
1181
1182     if (!HH_AddSizeBar(pHHInfo))
1183         return FALSE;
1184
1185     if (!HH_AddHTMLPane(pHHInfo))
1186         return FALSE;
1187
1188     if (!AddContentTab(pHHInfo))
1189         return FALSE;
1190
1191     if (!AddIndexTab(pHHInfo))
1192         return FALSE;
1193
1194     if (!AddIndexPopup(pHHInfo))
1195         return FALSE;
1196
1197     InitContent(pHHInfo);
1198     InitIndex(pHHInfo);
1199
1200     return TRUE;
1201 }
1202
1203 void ReleaseHelpViewer(HHInfo *info)
1204 {
1205     TRACE("(%p)\n", info);
1206
1207     if (!info)
1208         return;
1209
1210     /* Free allocated strings */
1211     heap_free(info->pszType);
1212     heap_free(info->pszCaption);
1213     heap_free(info->pszToc);
1214     heap_free(info->pszIndex);
1215     heap_free(info->pszFile);
1216     heap_free(info->pszHome);
1217     heap_free(info->pszJump1);
1218     heap_free(info->pszJump2);
1219     heap_free(info->pszUrlJump1);
1220     heap_free(info->pszUrlJump2);
1221
1222     if (info->pCHMInfo)
1223         CloseCHM(info->pCHMInfo);
1224
1225     ReleaseWebBrowser(info);
1226     ReleaseContent(info);
1227     ReleaseIndex(info);
1228
1229     if(info->WinType.hwndHelp)
1230         DestroyWindow(info->WinType.hwndHelp);
1231
1232     heap_free(info);
1233     OleUninitialize();
1234 }
1235
1236 HHInfo *CreateHelpViewer(LPCWSTR filename)
1237 {
1238     HHInfo *info = heap_alloc_zero(sizeof(HHInfo));
1239     int i;
1240
1241     /* Set the invalid tab ID (-1) as the default value for all
1242      * of the tabs, this matches a failed TCM_INSERTITEM call.
1243      */
1244     for(i=0;i<sizeof(info->tabs)/sizeof(HHTab);i++)
1245         info->tabs[i].id = -1;
1246
1247     OleInitialize(NULL);
1248
1249     info->pCHMInfo = OpenCHM(filename);
1250     if(!info->pCHMInfo) {
1251         ReleaseHelpViewer(info);
1252         return NULL;
1253     }
1254
1255     if (!LoadWinTypeFromCHM(info)) {
1256         ReleaseHelpViewer(info);
1257         return NULL;
1258     }
1259
1260     if(!CreateViewer(info)) {
1261         ReleaseHelpViewer(info);
1262         return NULL;
1263     }
1264
1265     return info;
1266 }