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