hhctrl.ocx: Added handling of topic selection.
[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     int iSize;
55
56     iSize = LoadStringW(hhctrl_hinstance, dwID, NULL, 0);
57     iSize += 2; /* some strings (tab text) needs double-null termination */
58
59     string = hhctrl_alloc(iSize * sizeof(WCHAR));
60     LoadStringW(hhctrl_hinstance, dwID, string, iSize);
61
62     return string;
63 }
64
65 BOOL NavigateToUrl(HHInfo *info, LPCWSTR surl)
66 {
67     VARIANT url;
68     HRESULT hres;
69
70     V_VT(&url) = VT_BSTR;
71     V_BSTR(&url) = SysAllocString(surl);
72
73     hres = IWebBrowser2_Navigate2(info->web_browser, &url, 0, 0, 0, 0);
74
75     VariantClear(&url);
76
77     return SUCCEEDED(hres);
78 }
79
80 BOOL NavigateToChm(HHInfo *info, LPCWSTR file, LPCWSTR index)
81 {
82     WCHAR buf[INTERNET_MAX_URL_LENGTH];
83     WCHAR full_path[MAX_PATH];
84     LPWSTR ptr;
85
86     static const WCHAR url_format[] =
87         {'m','k',':','@','M','S','I','T','S','t','o','r','e',':','%','s',':',':','/','%','s',0};
88
89     TRACE("%p %s %s\n", info, debugstr_w(file), debugstr_w(index));
90
91     if (!info->web_browser)
92         return FALSE;
93
94     if(!GetFullPathNameW(file, sizeof(full_path), full_path, NULL)) {
95         WARN("GetFullPathName failed: %u\n", GetLastError());
96         return FALSE;
97     }
98
99     wsprintfW(buf, url_format, full_path, index);
100
101     /* FIXME: HACK */
102     if((ptr = strchrW(buf, '#')))
103        *ptr = 0;
104
105     return NavigateToUrl(info, buf);
106 }
107
108 /* Size Bar */
109
110 #define SIZEBAR_WIDTH   4
111
112 static const WCHAR szSizeBarClass[] = {
113     'H','H',' ','S','i','z','e','B','a','r',0
114 };
115
116 /* Draw the SizeBar */
117 static void SB_OnPaint(HWND hWnd)
118 {
119     PAINTSTRUCT ps;
120     HDC hdc;
121     RECT rc;
122     
123     hdc = BeginPaint(hWnd, &ps);
124
125     GetClientRect(hWnd, &rc);
126
127     /* dark frame */
128     rc.right += 1;
129     rc.bottom -= 1;
130     FrameRect(hdc, &rc, GetStockObject(GRAY_BRUSH));
131
132     /* white highlight */
133     SelectObject(hdc, GetStockObject(WHITE_PEN));
134     MoveToEx(hdc, rc.right, 1, NULL);
135     LineTo(hdc, 1, 1);
136     LineTo(hdc, 1, rc.bottom - 1);
137
138     
139     MoveToEx(hdc, 0, rc.bottom, NULL);
140     LineTo(hdc, rc.right, rc.bottom);
141
142     EndPaint(hWnd, &ps);
143 }
144
145 static void SB_OnLButtonDown(HWND hWnd, WPARAM wParam, LPARAM lParam)
146 {
147     SetCapture(hWnd);
148 }
149
150 static void SB_OnLButtonUp(HWND hWnd, WPARAM wParam, LPARAM lParam)
151 {
152     HHInfo *pHHInfo = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
153     POINT pt;
154
155     pt.x = (short)LOWORD(lParam);
156     pt.y = (short)HIWORD(lParam);
157
158     /* update the window sizes */
159     pHHInfo->WinType.iNavWidth += pt.x;
160     Help_OnSize(hWnd);
161
162     ReleaseCapture();
163 }
164
165 static void SB_OnMouseMove(HWND hWnd, WPARAM wParam, LPARAM lParam)
166 {
167     /* ignore WM_MOUSEMOVE if not dragging the SizeBar */
168     if (!(wParam & MK_LBUTTON))
169         return;
170 }
171
172 static LRESULT CALLBACK SizeBar_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
173 {
174     switch (message)
175     {
176         case WM_LBUTTONDOWN:
177             SB_OnLButtonDown(hWnd, wParam, lParam);
178             break;
179         case WM_LBUTTONUP:
180             SB_OnLButtonUp(hWnd, wParam, lParam);
181             break;
182         case WM_MOUSEMOVE:
183             SB_OnMouseMove(hWnd, wParam, lParam);
184             break;
185         case WM_PAINT:
186             SB_OnPaint(hWnd);
187             break;
188         default:
189             return DefWindowProcW(hWnd, message, wParam, lParam);
190     }
191
192     return 0;
193 }
194
195 static void HH_RegisterSizeBarClass(HHInfo *pHHInfo)
196 {
197     WNDCLASSEXW wcex;
198
199     wcex.cbSize         = sizeof(WNDCLASSEXW);
200     wcex.style          = 0;
201     wcex.lpfnWndProc    = SizeBar_WndProc;
202     wcex.cbClsExtra     = 0;
203     wcex.cbWndExtra     = 0;
204     wcex.hInstance      = hhctrl_hinstance;
205     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
206     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_SIZEWE);
207     wcex.hbrBackground  = (HBRUSH)(COLOR_MENU + 1);
208     wcex.lpszMenuName   = NULL;
209     wcex.lpszClassName  = szSizeBarClass;
210     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
211
212     RegisterClassExW(&wcex);
213 }
214
215 static void SB_GetSizeBarRect(HHInfo *info, RECT *rc)
216 {
217     RECT rectWND, rectTB, rectNP;
218
219     GetClientRect(info->WinType.hwndHelp, &rectWND);
220     GetClientRect(info->WinType.hwndToolBar, &rectTB);
221     GetClientRect(info->WinType.hwndNavigation, &rectNP);
222
223     rc->left = rectNP.right;
224     rc->top = rectTB.bottom;
225     rc->bottom = rectWND.bottom - rectTB.bottom;
226     rc->right = SIZEBAR_WIDTH;
227 }
228
229 static BOOL HH_AddSizeBar(HHInfo *pHHInfo)
230 {
231     HWND hWnd;
232     HWND hwndParent = pHHInfo->WinType.hwndHelp;
233     DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE | WS_OVERLAPPED;
234     DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
235     RECT rc;
236
237     SB_GetSizeBarRect(pHHInfo, &rc);
238
239     hWnd = CreateWindowExW(dwExStyles, szSizeBarClass, szEmpty, dwStyles,
240                            rc.left, rc.top, rc.right, rc.bottom,
241                            hwndParent, NULL, hhctrl_hinstance, NULL);
242     if (!hWnd)
243         return FALSE;
244
245     /* store the pointer to the HH info struct */
246     SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
247
248     pHHInfo->hwndSizeBar = hWnd;
249     return TRUE;
250 }
251
252 /* Child Window */
253
254 static const WCHAR szChildClass[] = {
255     'H','H',' ','C','h','i','l','d',0
256 };
257
258 static LRESULT Child_OnPaint(HWND hWnd)
259 {
260     PAINTSTRUCT ps;
261     HDC hdc;
262     RECT rc;
263
264     hdc = BeginPaint(hWnd, &ps);
265
266     /* Only paint the Navigation pane, identified by the fact
267      * that it has a child window
268      */
269     if (GetWindow(hWnd, GW_CHILD))
270     {
271         GetClientRect(hWnd, &rc);
272
273         /* set the border color */
274         SelectObject(hdc, GetStockObject(DC_PEN));
275         SetDCPenColor(hdc, GetSysColor(COLOR_BTNSHADOW));
276
277         /* Draw the top border */
278         LineTo(hdc, rc.right, 0);
279
280         SelectObject(hdc, GetStockObject(WHITE_PEN));
281         MoveToEx(hdc, 0, 1, NULL);
282         LineTo(hdc, rc.right, 1);
283     }
284
285     EndPaint(hWnd, &ps);
286
287     return 0;
288 }
289
290 static void ResizeTabChild(HHInfo *info, HWND hwnd)
291 {
292     RECT rect, tabrc;
293     DWORD cnt;
294
295     GetClientRect(info->WinType.hwndNavigation, &rect);
296     SendMessageW(info->hwndTabCtrl, TCM_GETITEMRECT, 0, (LPARAM)&tabrc);
297     cnt = SendMessageW(info->hwndTabCtrl, TCM_GETROWCOUNT, 0, 0);
298
299     rect.left = TAB_MARGIN;
300     rect.top = TAB_TOP_PADDING + cnt*(tabrc.bottom-tabrc.top) + TAB_MARGIN;
301     rect.right -= TAB_RIGHT_PADDING + TAB_MARGIN;
302     rect.bottom -= TAB_MARGIN;
303
304     SetWindowPos(hwnd, NULL, rect.left, rect.top, rect.right-rect.left,
305                  rect.bottom-rect.top, SWP_NOZORDER | SWP_NOACTIVATE);
306 }
307
308 static LRESULT Child_OnSize(HWND hwnd)
309 {
310     HHInfo *info = (HHInfo*)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
311     RECT rect;
312
313     if(!info || hwnd != info->WinType.hwndNavigation)
314         return 0;
315
316     GetClientRect(hwnd, &rect);
317     SetWindowPos(info->hwndTabCtrl, HWND_TOP, 0, 0,
318                  rect.right - TAB_RIGHT_PADDING,
319                  rect.bottom - TAB_TOP_PADDING, SWP_NOMOVE);
320
321     ResizeTabChild(info, info->tabs[TAB_CONTENTS].hwnd);
322     return 0;
323 }
324
325 static LRESULT OnTabChange(HWND hwnd)
326 {
327     HHInfo *info = (HHInfo*)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
328
329     TRACE("%p\n", hwnd);
330
331     if (!info)
332         return 0;
333
334     if(info->tabs[info->current_tab].hwnd)
335         ShowWindow(info->tabs[info->current_tab].hwnd, SW_HIDE);
336
337     info->current_tab = SendMessageW(info->hwndTabCtrl, TCM_GETCURSEL, 0, 0);
338
339     if(info->tabs[info->current_tab].hwnd)
340         ShowWindow(info->tabs[info->current_tab].hwnd, SW_SHOW);
341
342     return 0;
343 }
344
345 static LRESULT OnTopicChange(HWND hwnd, ContentItem *item)
346 {
347     HHInfo *info = (HHInfo*)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
348     LPCWSTR chmfile = NULL;
349     ContentItem *iter = item;
350
351     if(!item || !info)
352         return 0;
353
354     TRACE("name %s loal %s\n", debugstr_w(item->name), debugstr_w(item->local));
355
356     while(iter) {
357         if(iter->merge.chm_file) {
358             chmfile = iter->merge.chm_file;
359             break;
360         }
361         iter = iter->parent;
362     }
363
364     NavigateToChm(info, chmfile, item->local);
365     return 0;
366 }
367
368 static LRESULT CALLBACK Child_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
369 {
370     switch (message)
371     {
372     case WM_PAINT:
373         return Child_OnPaint(hWnd);
374     case WM_SIZE:
375         return Child_OnSize(hWnd);
376     case WM_NOTIFY: {
377         NMHDR *nmhdr = (NMHDR*)lParam;
378         switch(nmhdr->code) {
379         case TCN_SELCHANGE:
380             return OnTabChange(hWnd);
381         case TVN_SELCHANGEDW:
382             return OnTopicChange(hWnd, (ContentItem*)((NMTREEVIEWW *)lParam)->itemNew.lParam);
383         }
384         break;
385     }
386     default:
387         return DefWindowProcW(hWnd, message, wParam, lParam);
388     }
389
390     return 0;
391 }
392
393 static void HH_RegisterChildWndClass(HHInfo *pHHInfo)
394 {
395     WNDCLASSEXW wcex;
396
397     wcex.cbSize         = sizeof(WNDCLASSEXW);
398     wcex.style          = 0;
399     wcex.lpfnWndProc    = Child_WndProc;
400     wcex.cbClsExtra     = 0;
401     wcex.cbWndExtra     = 0;
402     wcex.hInstance      = hhctrl_hinstance;
403     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
404     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
405     wcex.hbrBackground  = (HBRUSH)(COLOR_BTNFACE + 1);
406     wcex.lpszMenuName   = NULL;
407     wcex.lpszClassName  = szChildClass;
408     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
409
410     RegisterClassExW(&wcex);
411 }
412
413 /* Toolbar */
414
415 #define ICON_SIZE   20
416
417 static void TB_OnClick(HWND hWnd, DWORD dwID)
418 {
419     HHInfo *info = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
420
421     switch (dwID)
422     {
423         case IDTB_STOP:
424             DoPageAction(info, WB_STOP);
425             break;
426         case IDTB_REFRESH:
427             DoPageAction(info, WB_REFRESH);
428             break;
429         case IDTB_BACK:
430             DoPageAction(info, WB_GOBACK);
431             break;
432         case IDTB_HOME:
433             NavigateToChm(info, info->pCHMInfo->szFile, info->WinType.pszHome);
434             break;
435         case IDTB_FORWARD:
436             DoPageAction(info, WB_GOFORWARD);
437             break;
438         case IDTB_EXPAND:
439         case IDTB_CONTRACT:
440         case IDTB_SYNC:
441         case IDTB_PRINT:
442         case IDTB_OPTIONS:
443         case IDTB_BROWSE_FWD:
444         case IDTB_BROWSE_BACK:
445         case IDTB_JUMP1:
446         case IDTB_JUMP2:
447         case IDTB_CUSTOMIZE:
448         case IDTB_ZOOM:
449         case IDTB_TOC_NEXT:
450         case IDTB_TOC_PREV:
451             break;
452     }
453 }
454
455 static void TB_AddButton(TBBUTTON *pButtons, DWORD dwIndex, DWORD dwID)
456 {
457     /* FIXME: Load the correct button bitmaps */
458     pButtons[dwIndex].iBitmap = STD_PRINT;
459     pButtons[dwIndex].idCommand = dwID;
460     pButtons[dwIndex].fsState = TBSTATE_ENABLED;
461     pButtons[dwIndex].fsStyle = BTNS_BUTTON;
462     pButtons[dwIndex].dwData = 0;
463     pButtons[dwIndex].iString = 0;
464 }
465
466 static void TB_AddButtonsFromFlags(TBBUTTON *pButtons, DWORD dwButtonFlags, LPDWORD pdwNumButtons)
467 {
468     *pdwNumButtons = 0;
469
470     if (dwButtonFlags & HHWIN_BUTTON_EXPAND)
471         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_EXPAND);
472
473     if (dwButtonFlags & HHWIN_BUTTON_BACK)
474         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_BACK);
475
476     if (dwButtonFlags & HHWIN_BUTTON_FORWARD)
477         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_FORWARD);
478
479     if (dwButtonFlags & HHWIN_BUTTON_STOP)
480         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_STOP);
481
482     if (dwButtonFlags & HHWIN_BUTTON_REFRESH)
483         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_REFRESH);
484
485     if (dwButtonFlags & HHWIN_BUTTON_HOME)
486         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_HOME);
487
488     if (dwButtonFlags & HHWIN_BUTTON_SYNC)
489         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_SYNC);
490
491     if (dwButtonFlags & HHWIN_BUTTON_OPTIONS)
492         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_OPTIONS);
493
494     if (dwButtonFlags & HHWIN_BUTTON_PRINT)
495         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_PRINT);
496
497     if (dwButtonFlags & HHWIN_BUTTON_JUMP1)
498         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_JUMP1);
499
500     if (dwButtonFlags & HHWIN_BUTTON_JUMP2)
501         TB_AddButton(pButtons,(*pdwNumButtons)++, IDTB_JUMP2);
502
503     if (dwButtonFlags & HHWIN_BUTTON_ZOOM)
504         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_ZOOM);
505
506     if (dwButtonFlags & HHWIN_BUTTON_TOC_NEXT)
507         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_TOC_NEXT);
508
509     if (dwButtonFlags & HHWIN_BUTTON_TOC_PREV)
510         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_TOC_PREV);
511 }
512
513 static BOOL HH_AddToolbar(HHInfo *pHHInfo)
514 {
515     HWND hToolbar;
516     HWND hwndParent = pHHInfo->WinType.hwndHelp;
517     DWORD toolbarFlags;
518     TBBUTTON buttons[IDTB_TOC_PREV - IDTB_EXPAND];
519     TBADDBITMAP tbAB;
520     DWORD dwStyles, dwExStyles;
521     DWORD dwNumButtons, dwIndex;
522
523     if (pHHInfo->WinType.fsWinProperties & HHWIN_PARAM_TB_FLAGS)
524         toolbarFlags = pHHInfo->WinType.fsToolBarFlags;
525     else
526         toolbarFlags = HHWIN_DEF_BUTTONS;
527
528     TB_AddButtonsFromFlags(buttons, toolbarFlags, &dwNumButtons);
529
530     dwStyles = WS_CHILDWINDOW | WS_VISIBLE | TBSTYLE_FLAT |
531                TBSTYLE_WRAPABLE | TBSTYLE_TOOLTIPS | CCS_NODIVIDER;
532     dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
533
534     hToolbar = CreateWindowExW(dwExStyles, TOOLBARCLASSNAMEW, NULL, dwStyles,
535                                0, 0, 0, 0, hwndParent, NULL,
536                                hhctrl_hinstance, NULL);
537     if (!hToolbar)
538         return FALSE;
539
540     SendMessageW(hToolbar, TB_SETBITMAPSIZE, 0, MAKELONG(ICON_SIZE, ICON_SIZE));
541     SendMessageW(hToolbar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
542     SendMessageW(hToolbar, WM_SETFONT, (WPARAM)pHHInfo->hFont, TRUE);
543
544     /* FIXME: Load correct icons for all buttons */
545     tbAB.hInst = HINST_COMMCTRL;
546     tbAB.nID = IDB_STD_LARGE_COLOR;
547     SendMessageW(hToolbar, TB_ADDBITMAP, 0, (LPARAM)&tbAB);
548
549     for (dwIndex = 0; dwIndex < dwNumButtons; dwIndex++)
550     {
551         LPWSTR szBuf = HH_LoadString(buttons[dwIndex].idCommand);
552         DWORD dwLen = strlenW(szBuf);
553         szBuf[dwLen + 2] = 0; /* Double-null terminate */
554
555         buttons[dwIndex].iString = (DWORD)SendMessageW(hToolbar, TB_ADDSTRINGW, 0, (LPARAM)szBuf);
556         hhctrl_free(szBuf);
557     }
558
559     SendMessageW(hToolbar, TB_ADDBUTTONSW, dwNumButtons, (LPARAM)&buttons);
560     SendMessageW(hToolbar, TB_AUTOSIZE, 0, 0);
561     ShowWindow(hToolbar, SW_SHOW);
562
563     pHHInfo->WinType.hwndToolBar = hToolbar;
564     return TRUE;
565 }
566
567 /* Navigation Pane */
568
569 static void NP_GetNavigationRect(HHInfo *pHHInfo, RECT *rc)
570 {
571     HWND hwndParent = pHHInfo->WinType.hwndHelp;
572     HWND hwndToolbar = pHHInfo->WinType.hwndToolBar;
573     RECT rectWND, rectTB;
574
575     GetClientRect(hwndParent, &rectWND);
576     GetClientRect(hwndToolbar, &rectTB);
577
578     rc->left = 0;
579     rc->top = rectTB.bottom;
580     rc->bottom = rectWND.bottom - rectTB.bottom;
581
582     if (!(pHHInfo->WinType.fsValidMembers & HHWIN_PARAM_NAV_WIDTH) &&
583           pHHInfo->WinType.iNavWidth == 0)
584     {
585         pHHInfo->WinType.iNavWidth = WINTYPE_DEFAULT_NAVWIDTH;
586     }
587
588     rc->right = pHHInfo->WinType.iNavWidth;
589 }
590
591 static DWORD NP_CreateTab(HINSTANCE hInstance, HWND hwndTabCtrl, DWORD index)
592 {
593     TCITEMW tie;
594     LPWSTR tabText = HH_LoadString(index);
595     DWORD ret;
596
597     tie.mask = TCIF_TEXT;
598     tie.pszText = tabText;
599
600     ret = SendMessageW( hwndTabCtrl, TCM_INSERTITEMW, index, (LPARAM)&tie );
601
602     hhctrl_free(tabText);
603     return ret;
604 }
605
606 static BOOL HH_AddNavigationPane(HHInfo *info)
607 {
608     HWND hWnd, hwndTabCtrl;
609     HWND hwndParent = info->WinType.hwndHelp;
610     DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE;
611     DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
612     RECT rc;
613
614     NP_GetNavigationRect(info, &rc);
615
616     hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
617                            rc.left, rc.top, rc.right, rc.bottom,
618                            hwndParent, NULL, hhctrl_hinstance, NULL);
619     if (!hWnd)
620         return FALSE;
621
622     SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)info);
623
624     hwndTabCtrl = CreateWindowExW(dwExStyles, WC_TABCONTROLW, szEmpty, dwStyles,
625                                   0, TAB_TOP_PADDING,
626                                   rc.right - TAB_RIGHT_PADDING,
627                                   rc.bottom - TAB_TOP_PADDING,
628                                   hWnd, NULL, hhctrl_hinstance, NULL);
629     if (!hwndTabCtrl)
630         return FALSE;
631
632     if (*info->WinType.pszToc)
633         info->tabs[TAB_CONTENTS].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_CONTENTS);
634
635     if (*info->WinType.pszIndex)
636         info->tabs[TAB_INDEX].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_INDEX);
637
638     if (info->WinType.fsWinProperties & HHWIN_PROP_TAB_SEARCH)
639         info->tabs[TAB_SEARCH].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_SEARCH);
640
641     if (info->WinType.fsWinProperties & HHWIN_PROP_TAB_FAVORITES)
642         info->tabs[TAB_FAVORITES].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_FAVORITES);
643
644     SendMessageW(hwndTabCtrl, WM_SETFONT, (WPARAM)info->hFont, TRUE);
645
646     info->hwndTabCtrl = hwndTabCtrl;
647     info->WinType.hwndNavigation = hWnd;
648     return TRUE;
649 }
650
651 /* HTML Pane */
652
653 static void HP_GetHTMLRect(HHInfo *info, RECT *rc)
654 {
655     RECT rectTB, rectWND, rectNP, rectSB;
656
657     GetClientRect(info->WinType.hwndHelp, &rectWND);
658     GetClientRect(info->WinType.hwndToolBar, &rectTB);
659     GetClientRect(info->WinType.hwndNavigation, &rectNP);
660     GetClientRect(info->hwndSizeBar, &rectSB);
661
662     rc->left = rectNP.right + rectSB.right;
663     rc->top = rectTB.bottom;
664     rc->right = rectWND.right - rc->left;
665     rc->bottom = rectWND.bottom - rectTB.bottom;
666 }
667
668 static BOOL HH_AddHTMLPane(HHInfo *pHHInfo)
669 {
670     HWND hWnd;
671     HWND hwndParent = pHHInfo->WinType.hwndHelp;
672     DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN;
673     DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_CLIENTEDGE;
674     RECT rc;
675
676     HP_GetHTMLRect(pHHInfo, &rc);
677
678     hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
679                            rc.left, rc.top, rc.right, rc.bottom,
680                            hwndParent, NULL, hhctrl_hinstance, NULL);
681     if (!hWnd)
682         return FALSE;
683
684     if (!InitWebBrowser(pHHInfo, hWnd))
685         return FALSE;
686
687     /* store the pointer to the HH info struct */
688     SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
689
690     ShowWindow(hWnd, SW_SHOW);
691     UpdateWindow(hWnd);
692
693     pHHInfo->WinType.hwndHTML = hWnd;
694     return TRUE;
695 }
696
697 static BOOL AddContentTab(HHInfo *info)
698 {
699     info->tabs[TAB_CONTENTS].hwnd = CreateWindowExW(WS_EX_CLIENTEDGE, WC_TREEVIEWW,
700            szEmpty, WS_CHILD | WS_BORDER | 0x25, 50, 50, 100, 100,
701            info->WinType.hwndNavigation, NULL, hhctrl_hinstance, NULL);
702     if(!info->tabs[TAB_CONTENTS].hwnd) {
703         ERR("Could not create treeview control\n");
704         return FALSE;
705     }
706
707     ResizeTabChild(info, info->tabs[TAB_CONTENTS].hwnd);
708     ShowWindow(info->tabs[TAB_CONTENTS].hwnd, SW_SHOW);
709
710     return TRUE;
711 }
712
713 /* Viewer Window */
714
715 static LRESULT Help_OnSize(HWND hWnd)
716 {
717     HHInfo *pHHInfo = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
718     DWORD dwSize;
719     RECT rc;
720
721     if (!pHHInfo)
722         return 0;
723
724     NP_GetNavigationRect(pHHInfo, &rc);
725     SetWindowPos(pHHInfo->WinType.hwndNavigation, HWND_TOP, 0, 0,
726                  rc.right, rc.bottom, SWP_NOMOVE);
727
728     SB_GetSizeBarRect(pHHInfo, &rc);
729     SetWindowPos(pHHInfo->hwndSizeBar, HWND_TOP, rc.left, rc.top,
730                  rc.right, rc.bottom, SWP_SHOWWINDOW);
731
732     HP_GetHTMLRect(pHHInfo, &rc);
733     SetWindowPos(pHHInfo->WinType.hwndHTML, HWND_TOP, rc.left, rc.top,
734                  rc.right, rc.bottom, SWP_SHOWWINDOW);
735
736     /* Resize browser window taking the frame size into account */
737     dwSize = GetSystemMetrics(SM_CXFRAME);
738     ResizeWebBrowser(pHHInfo, rc.right - dwSize, rc.bottom - dwSize);
739
740     return 0;
741 }
742
743 static LRESULT CALLBACK Help_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
744 {
745     switch (message)
746     {
747     case WM_COMMAND:
748         if (HIWORD(wParam) == BN_CLICKED)
749             TB_OnClick(hWnd, LOWORD(wParam));
750         break;
751     case WM_SIZE:
752         return Help_OnSize(hWnd);
753     case WM_CLOSE:
754         ReleaseHelpViewer((HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA));
755         return 0;
756     case WM_DESTROY:
757         if(hh_process)
758             PostQuitMessage(0);
759         break;
760
761     default:
762         return DefWindowProcW(hWnd, message, wParam, lParam);
763     }
764
765     return 0;
766 }
767
768 static BOOL HH_CreateHelpWindow(HHInfo *info)
769 {
770     HWND hWnd;
771     RECT winPos = info->WinType.rcWindowPos;
772     WNDCLASSEXW wcex;
773     DWORD dwStyles, dwExStyles;
774     DWORD x, y, width, height;
775
776     static const WCHAR windowClassW[] = {
777         'H','H',' ', 'P','a','r','e','n','t',0
778     };
779
780     wcex.cbSize         = sizeof(WNDCLASSEXW);
781     wcex.style          = CS_HREDRAW | CS_VREDRAW;
782     wcex.lpfnWndProc    = Help_WndProc;
783     wcex.cbClsExtra     = 0;
784     wcex.cbWndExtra     = 0;
785     wcex.hInstance      = hhctrl_hinstance;
786     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
787     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
788     wcex.hbrBackground  = (HBRUSH)(COLOR_MENU + 1);
789     wcex.lpszMenuName   = NULL;
790     wcex.lpszClassName  = windowClassW;
791     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
792
793     RegisterClassExW(&wcex);
794
795     /* Read in window parameters if available */
796     if (info->WinType.fsValidMembers & HHWIN_PARAM_STYLES)
797         dwStyles = info->WinType.dwStyles;
798     else
799         dwStyles = WS_OVERLAPPEDWINDOW | WS_VISIBLE |
800                    WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
801
802     if (info->WinType.fsValidMembers & HHWIN_PARAM_EXSTYLES)
803         dwExStyles = info->WinType.dwExStyles;
804     else
805         dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_APPWINDOW |
806                      WS_EX_WINDOWEDGE | WS_EX_RIGHTSCROLLBAR;
807
808     if (info->WinType.fsValidMembers & HHWIN_PARAM_RECT)
809     {
810         x = winPos.left;
811         y = winPos.top;
812         width = winPos.right - x;
813         height = winPos.bottom - y;
814     }
815     else
816     {
817         x = WINTYPE_DEFAULT_X;
818         y = WINTYPE_DEFAULT_Y;
819         width = WINTYPE_DEFAULT_WIDTH;
820         height = WINTYPE_DEFAULT_HEIGHT;
821     }
822
823     hWnd = CreateWindowExW(dwExStyles, windowClassW, info->WinType.pszCaption,
824                            dwStyles, x, y, width, height, NULL, NULL, hhctrl_hinstance, NULL);
825     if (!hWnd)
826         return FALSE;
827
828     ShowWindow(hWnd, SW_SHOW);
829     UpdateWindow(hWnd);
830
831     /* store the pointer to the HH info struct */
832     SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)info);
833
834     info->WinType.hwndHelp = hWnd;
835     return TRUE;
836 }
837
838 static void HH_CreateFont(HHInfo *pHHInfo)
839 {
840     LOGFONTW lf;
841
842     GetObjectW(GetStockObject(ANSI_VAR_FONT), sizeof(LOGFONTW), &lf);
843     lf.lfWeight = FW_NORMAL;
844     lf.lfItalic = FALSE;
845     lf.lfUnderline = FALSE;
846
847     pHHInfo->hFont = CreateFontIndirectW(&lf);
848 }
849
850 static void HH_InitRequiredControls(DWORD dwControls)
851 {
852     INITCOMMONCONTROLSEX icex;
853
854     icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
855     icex.dwICC = dwControls;
856     InitCommonControlsEx(&icex);
857 }
858
859 /* Creates the whole package */
860 static BOOL CreateViewer(HHInfo *pHHInfo)
861 {
862     HH_CreateFont(pHHInfo);
863
864     if (!HH_CreateHelpWindow(pHHInfo))
865         return FALSE;
866
867     HH_InitRequiredControls(ICC_BAR_CLASSES);
868
869     if (!HH_AddToolbar(pHHInfo))
870         return FALSE;
871
872     HH_RegisterChildWndClass(pHHInfo);
873
874     if (!HH_AddNavigationPane(pHHInfo))
875         return FALSE;
876
877     HH_RegisterSizeBarClass(pHHInfo);
878
879     if (!HH_AddSizeBar(pHHInfo))
880         return FALSE;
881
882     if (!HH_AddHTMLPane(pHHInfo))
883         return FALSE;
884
885     if (!AddContentTab(pHHInfo))
886         return FALSE;
887
888     InitContent(pHHInfo);
889
890     return TRUE;
891 }
892
893 void ReleaseHelpViewer(HHInfo *info)
894 {
895     if (!info)
896         return;
897
898     /* Free allocated strings */
899     hhctrl_free((LPWSTR)info->WinType.pszType);
900     hhctrl_free((LPWSTR)info->WinType.pszCaption);
901     hhctrl_free((LPWSTR)info->WinType.pszToc);
902     hhctrl_free((LPWSTR)info->WinType.pszIndex);
903     hhctrl_free((LPWSTR)info->WinType.pszFile);
904     hhctrl_free((LPWSTR)info->WinType.pszHome);
905     hhctrl_free((LPWSTR)info->WinType.pszJump1);
906     hhctrl_free((LPWSTR)info->WinType.pszJump2);
907     hhctrl_free((LPWSTR)info->WinType.pszUrlJump1);
908     hhctrl_free((LPWSTR)info->WinType.pszUrlJump2);
909
910     if (info->pCHMInfo)
911         CloseCHM(info->pCHMInfo);
912
913     ReleaseWebBrowser(info);
914     ReleaseContent(info);
915
916     if(info->WinType.hwndHelp)
917         DestroyWindow(info->WinType.hwndHelp);
918
919     hhctrl_free(info);
920     OleUninitialize();
921 }
922
923 HHInfo *CreateHelpViewer(LPCWSTR filename)
924 {
925     HHInfo *info = hhctrl_alloc_zero(sizeof(HHInfo));
926
927     OleInitialize(NULL);
928
929     info->pCHMInfo = OpenCHM(filename);
930     if(!info->pCHMInfo) {
931         ReleaseHelpViewer(info);
932         return NULL;
933     }
934
935     if (!LoadWinTypeFromCHM(info->pCHMInfo, &info->WinType)) {
936         ReleaseHelpViewer(info);
937         return NULL;
938     }
939
940     if(!CreateViewer(info)) {
941         ReleaseHelpViewer(info);
942         return NULL;
943     }
944
945     return info;
946 }