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