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