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