Add initial toolbar button handling.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "winuser.h"
27 #include "winnls.h"
28 #include "commctrl.h"
29 #include "htmlhelp.h"
30 #include "ole2.h"
31 #include "wine/unicode.h"
32
33 #include "resource.h"
34 #include "chm.h"
35 #include "webbrowser.h"
36
37 /* Window type defaults */
38
39 #define WINTYPE_DEFAULT_X           280
40 #define WINTYPE_DEFAULT_Y           100
41 #define WINTYPE_DEFAULT_WIDTH       740
42 #define WINTYPE_DEFAULT_HEIGHT      640
43 #define WINTYPE_DEFAULT_NAVWIDTH    251
44
45 typedef struct tagHHInfo
46 {
47     HH_WINTYPEW *pHHWinType;
48     CHMInfo *pCHMInfo;
49     WBInfo *pWBInfo;
50     HINSTANCE hInstance;
51     LPWSTR szCmdLine;
52     HWND hwndTabCtrl;
53     HFONT hFont;
54 } HHInfo;
55
56 extern HINSTANCE hhctrl_hinstance;
57
58 static LPWSTR HH_ANSIToUnicode(LPCSTR ansi)
59 {
60     LPWSTR unicode;
61     int count;
62
63     count = MultiByteToWideChar(CP_ACP, 0, ansi, -1, NULL, 0);
64     unicode = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR));
65     MultiByteToWideChar(CP_ACP, 0, ansi, -1, unicode, count);
66
67     return unicode;
68 }
69
70 /* Loads a string from the resource file */
71 static LPWSTR HH_LoadString(DWORD dwID)
72 {
73     LPWSTR string = NULL;
74     int iSize;
75
76     iSize = LoadStringW(hhctrl_hinstance, dwID, NULL, 0);
77     iSize += 2; /* some strings (tab text) needs double-null termination */
78
79     string = HeapAlloc(GetProcessHeap(), 0, iSize * sizeof(WCHAR));
80     LoadStringW(hhctrl_hinstance, dwID, string, iSize);
81
82     return string;
83 }
84
85 /* Child Window */
86
87 static const WCHAR szChildClass[] = {
88     'H','H',' ','C','h','i','l','d',0
89 };
90
91 static const WCHAR szEmpty[] = {0};
92
93 static void Child_OnPaint(HWND hWnd)
94 {
95     PAINTSTRUCT ps;
96     HDC hdc;
97     RECT rc;
98
99     hdc = BeginPaint(hWnd, &ps);
100
101     /* Only paint the Navigation pane, identified by the fact
102      * that it has a child window
103      */
104     if (GetWindow(hWnd, GW_CHILD))
105     {
106         GetClientRect(hWnd, &rc);
107
108         /* set the border color */
109         SelectObject(hdc, GetStockObject(DC_PEN));
110         SetDCPenColor(hdc, GetSysColor(COLOR_BTNSHADOW));
111
112         /* Draw the top and right borders */
113         MoveToEx(hdc, 0, 0, NULL);
114         LineTo(hdc, rc.right - 1, 0);
115         LineTo(hdc, rc.right - 1, rc.bottom);
116
117         /* Fill in the background, taking the border lines into account */
118         rc.top += 2;
119         rc.right -= 1;
120         FillRect(hdc, &rc, GetSysColorBrush(COLOR_3DFACE));
121     }
122
123     EndPaint(hWnd, &ps);
124 }
125
126 LRESULT CALLBACK Child_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
127 {
128     switch (message)
129     {
130         case WM_PAINT:
131             Child_OnPaint(hWnd);
132             break;
133         default:
134             return DefWindowProcW(hWnd, message, wParam, lParam);
135     }
136
137     return 0;
138 }
139
140 static void HH_RegisterChildWndClass(HHInfo *pHHInfo)
141 {
142     WNDCLASSEXW wcex;
143
144     wcex.cbSize         = sizeof(WNDCLASSEXW);
145     wcex.style          = 0;
146     wcex.lpfnWndProc    = (WNDPROC)Child_WndProc;
147     wcex.cbClsExtra     = 0;
148     wcex.cbWndExtra     = 0;
149     wcex.hInstance      = pHHInfo->hInstance;
150     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
151     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
152     wcex.hbrBackground  = (HBRUSH)(COLOR_3DFACE);
153     wcex.lpszMenuName   = NULL;
154     wcex.lpszClassName  = szChildClass;
155     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
156
157     RegisterClassExW(&wcex);
158 }
159
160 /* Toolbar */
161
162 #define ICON_SIZE   20
163
164 static void TB_OnClick(HWND hWnd, DWORD dwID)
165 {
166     HHInfo *pHHInfo = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
167
168     switch (dwID)
169     {
170         case IDTB_STOP:
171             WB_DoPageAction(pHHInfo->pWBInfo, WB_STOP);
172             break;
173         case IDTB_REFRESH:
174             WB_DoPageAction(pHHInfo->pWBInfo, WB_REFRESH);
175             break;
176         case IDTB_BACK:
177             WB_DoPageAction(pHHInfo->pWBInfo, WB_GOBACK);
178             break;
179         case IDTB_HOME:
180         {
181             WCHAR szUrl[MAX_PATH];
182
183             CHM_CreateITSUrl(pHHInfo->pCHMInfo, pHHInfo->pHHWinType->pszHome, szUrl);
184             WB_Navigate(pHHInfo->pWBInfo, szUrl);
185             break;
186         }
187         case IDTB_FORWARD:
188             WB_DoPageAction(pHHInfo->pWBInfo, WB_GOFORWARD);
189             break;
190         case IDTB_EXPAND:
191         case IDTB_CONTRACT:
192         case IDTB_SYNC:
193         case IDTB_PRINT:
194         case IDTB_OPTIONS:
195         case IDTB_BROWSE_FWD:
196         case IDTB_BROWSE_BACK:
197         case IDTB_JUMP1:
198         case IDTB_JUMP2:
199         case IDTB_CUSTOMIZE:
200         case IDTB_ZOOM:
201         case IDTB_TOC_NEXT:
202         case IDTB_TOC_PREV:
203             break;
204     }
205 }
206
207 static void TB_AddButton(TBBUTTON *pButtons, DWORD dwIndex, DWORD dwID)
208 {
209     /* FIXME: Load the correct button bitmaps */
210     pButtons[dwIndex].iBitmap = STD_PRINT;
211     pButtons[dwIndex].idCommand = dwID;
212     pButtons[dwIndex].fsState = TBSTATE_ENABLED;
213     pButtons[dwIndex].fsStyle = BTNS_BUTTON;
214     pButtons[dwIndex].dwData = 0;
215     pButtons[dwIndex].iString = 0;
216 }
217
218 static void TB_AddButtonsFromFlags(TBBUTTON *pButtons, DWORD dwButtonFlags, LPDWORD pdwNumButtons)
219 {
220     *pdwNumButtons = 0;
221
222     if (dwButtonFlags & HHWIN_BUTTON_EXPAND)
223         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_EXPAND);
224
225     if (dwButtonFlags & HHWIN_BUTTON_BACK)
226         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_BACK);
227
228     if (dwButtonFlags & HHWIN_BUTTON_FORWARD)
229         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_FORWARD);
230
231     if (dwButtonFlags & HHWIN_BUTTON_STOP)
232         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_STOP);
233
234     if (dwButtonFlags & HHWIN_BUTTON_REFRESH)
235         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_REFRESH);
236
237     if (dwButtonFlags & HHWIN_BUTTON_HOME)
238         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_HOME);
239
240     if (dwButtonFlags & HHWIN_BUTTON_SYNC)
241         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_SYNC);
242
243     if (dwButtonFlags & HHWIN_BUTTON_OPTIONS)
244         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_OPTIONS);
245
246     if (dwButtonFlags & HHWIN_BUTTON_PRINT)
247         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_PRINT);
248
249     if (dwButtonFlags & HHWIN_BUTTON_JUMP1)
250         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_JUMP1);
251
252     if (dwButtonFlags & HHWIN_BUTTON_JUMP2)
253         TB_AddButton(pButtons,(*pdwNumButtons)++, IDTB_JUMP2);
254
255     if (dwButtonFlags & HHWIN_BUTTON_ZOOM)
256         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_ZOOM);
257
258     if (dwButtonFlags & HHWIN_BUTTON_TOC_NEXT)
259         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_TOC_NEXT);
260
261     if (dwButtonFlags & HHWIN_BUTTON_TOC_PREV)
262         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_TOC_PREV);
263 }
264
265 static BOOL HH_AddToolbar(HHInfo *pHHInfo)
266 {
267     HWND hToolbar;
268     HWND hwndParent = pHHInfo->pHHWinType->hwndHelp;
269     DWORD toolbarFlags;
270     TBBUTTON buttons[IDTB_TOC_PREV - IDTB_EXPAND];
271     TBADDBITMAP tbAB;
272     DWORD dwStyles, dwExStyles;
273     DWORD dwNumButtons, dwIndex;
274
275     if (pHHInfo->pHHWinType->fsWinProperties & HHWIN_PARAM_TB_FLAGS)
276         toolbarFlags = pHHInfo->pHHWinType->fsToolBarFlags;
277     else
278         toolbarFlags = HHWIN_DEF_BUTTONS;
279
280     TB_AddButtonsFromFlags(buttons, toolbarFlags, &dwNumButtons);
281
282     dwStyles = WS_CHILDWINDOW | WS_VISIBLE | TBSTYLE_FLAT |
283                TBSTYLE_WRAPABLE | TBSTYLE_TOOLTIPS | CCS_NODIVIDER;
284     dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
285
286     hToolbar = CreateWindowExW(dwExStyles, TOOLBARCLASSNAMEW, NULL, dwStyles,
287                                0, 0, 0, 0, hwndParent, NULL,
288                                pHHInfo->hInstance, NULL);
289     if (!hToolbar)
290         return FALSE;
291
292     SendMessageW(hToolbar, TB_SETBITMAPSIZE, 0, MAKELONG(ICON_SIZE, ICON_SIZE));
293     SendMessageW(hToolbar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
294     SendMessageW(hToolbar, WM_SETFONT, (WPARAM)pHHInfo->hFont, TRUE);
295
296     /* FIXME: Load correct icons for all buttons */
297     tbAB.hInst = HINST_COMMCTRL;
298     tbAB.nID = IDB_STD_LARGE_COLOR;
299     SendMessageW(hToolbar, TB_ADDBITMAP, 0, (LPARAM)&tbAB);
300
301     for (dwIndex = 0; dwIndex < dwNumButtons; dwIndex++)
302     {
303         LPWSTR szBuf = HH_LoadString(buttons[dwIndex].idCommand);
304         DWORD dwLen = strlenW(szBuf);
305         szBuf[dwLen + 2] = 0; /* Double-null terminate */
306
307         buttons[dwIndex].iString = (DWORD)SendMessageW(hToolbar, TB_ADDSTRINGW, 0, (LPARAM)szBuf);
308         HeapFree(GetProcessHeap(), 0, szBuf);
309     }
310
311     SendMessageW(hToolbar, TB_ADDBUTTONSW, dwNumButtons, (LPARAM)&buttons);
312     SendMessageW(hToolbar, TB_AUTOSIZE, 0, 0);
313     ShowWindow(hToolbar, SW_SHOW);
314
315     pHHInfo->pHHWinType->hwndToolBar = hToolbar;
316     return TRUE;
317 }
318
319 /* Navigation Pane */
320
321 #define TAB_PADDING         2
322 #define TAB_TOP_PADDING     8
323 #define TAB_RIGHT_PADDING   4
324
325 static void NP_GetNavigationRect(HHInfo *pHHInfo, RECT *rc)
326 {
327     HWND hwndParent = pHHInfo->pHHWinType->hwndHelp;
328     HWND hwndToolbar = pHHInfo->pHHWinType->hwndToolBar;
329     RECT rectWND, rectTB;
330
331     GetClientRect(hwndParent, &rectWND);
332     GetClientRect(hwndToolbar, &rectTB);
333
334     rc->left = 0;
335     rc->top = rectTB.bottom;
336     rc->bottom = rectWND.bottom - rectTB.bottom;
337
338     if (pHHInfo->pHHWinType->fsValidMembers & HHWIN_PARAM_NAV_WIDTH)
339         rc->right = pHHInfo->pHHWinType->iNavWidth;
340     else
341         rc->right = WINTYPE_DEFAULT_NAVWIDTH;
342 }
343
344 static void NP_CreateTab(HINSTANCE hInstance, HWND hwndTabCtrl, DWORD dwStrID, DWORD dwIndex)
345 {
346     TCITEMW tie;
347     LPWSTR tabText = HH_LoadString(dwStrID);
348
349     tie.mask = TCIF_TEXT;
350     tie.pszText = tabText;
351
352     TabCtrl_InsertItemW(hwndTabCtrl, dwIndex, &tie);
353     HeapFree(GetProcessHeap(), 0, tabText);
354 }
355
356 static BOOL HH_AddNavigationPane(HHInfo *pHHInfo)
357 {
358     HWND hWnd, hwndTabCtrl;
359     HWND hwndParent = pHHInfo->pHHWinType->hwndHelp;
360     DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE;
361     DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
362     DWORD dwIndex = 0;
363     RECT rc;
364
365     NP_GetNavigationRect(pHHInfo, &rc);
366
367     hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
368                            rc.left, rc.top, rc.right, rc.bottom,
369                            hwndParent, NULL, pHHInfo->hInstance, NULL);
370     if (!hWnd)
371         return FALSE;
372
373     hwndTabCtrl = CreateWindowExW(dwExStyles, WC_TABCONTROLW, szEmpty, dwStyles,
374                                   TAB_PADDING, TAB_TOP_PADDING,
375                                   rc.right - TAB_PADDING - TAB_RIGHT_PADDING,
376                                   rc.bottom - TAB_PADDING - TAB_TOP_PADDING,
377                                   hWnd, NULL, pHHInfo->hInstance, NULL);
378     if (!hwndTabCtrl)
379         return FALSE;
380
381     if (*pHHInfo->pHHWinType->pszToc)
382         NP_CreateTab(pHHInfo->hInstance, hwndTabCtrl, IDS_CONTENTS, dwIndex++);
383
384     if (*pHHInfo->pHHWinType->pszIndex)
385         NP_CreateTab(pHHInfo->hInstance, hwndTabCtrl, IDS_INDEX, dwIndex++);
386
387     if (pHHInfo->pHHWinType->fsWinProperties & HHWIN_PROP_TAB_SEARCH)
388         NP_CreateTab(pHHInfo->hInstance, hwndTabCtrl, IDS_SEARCH, dwIndex++);
389
390     if (pHHInfo->pHHWinType->fsWinProperties & HHWIN_PROP_TAB_FAVORITES)
391         NP_CreateTab(pHHInfo->hInstance, hwndTabCtrl, IDS_FAVORITES, dwIndex++);
392
393     SendMessageW(hwndTabCtrl, WM_SETFONT, (WPARAM)pHHInfo->hFont, TRUE);
394
395     pHHInfo->hwndTabCtrl = hwndTabCtrl;
396     pHHInfo->pHHWinType->hwndNavigation = hWnd;
397     return TRUE;
398 }
399
400 /* HTML Pane */
401
402 static void HP_GetHTMLRect(HHInfo *pHHInfo, RECT *rc)
403 {
404     HWND hwndParent = pHHInfo->pHHWinType->hwndHelp;
405     HWND hwndToolbar = pHHInfo->pHHWinType->hwndToolBar;
406     HWND hwndNavigation = pHHInfo->pHHWinType->hwndNavigation;
407     RECT rectTB, rectWND, rectNP;
408
409     GetClientRect(hwndParent, &rectWND);
410     GetClientRect(hwndToolbar, &rectTB);
411     GetClientRect(hwndNavigation, &rectNP);
412
413     rc->left = rectNP.right;
414     rc->top = rectTB.bottom;
415     rc->right = rectWND.right - rectNP.right;
416     rc->bottom = rectWND.bottom - rectTB.bottom;
417 }
418
419 static BOOL HH_AddHTMLPane(HHInfo *pHHInfo)
420 {
421     HWND hWnd;
422     HWND hwndParent = pHHInfo->pHHWinType->hwndHelp;
423     DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN;
424     DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_CLIENTEDGE;
425     RECT rc;
426
427     HP_GetHTMLRect(pHHInfo, &rc);
428
429     hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
430                            rc.left, rc.top, rc.right, rc.bottom,
431                            hwndParent, NULL, pHHInfo->hInstance, NULL);
432     if (!hWnd)
433         return FALSE;
434
435     if (!WB_EmbedBrowser(pHHInfo->pWBInfo, hWnd))
436         return FALSE;
437
438     /* store the pointer to the HH info struct */
439     SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
440
441     ShowWindow(hWnd, SW_SHOW);
442     UpdateWindow(hWnd);
443
444     pHHInfo->pHHWinType->hwndHTML = hWnd;
445     return TRUE;
446 }
447
448 /* Viewer Window */
449
450 static void Help_OnSize(HWND hWnd, LPARAM lParam)
451 {
452     HHInfo *pHHInfo = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
453     RECT rc;
454
455     if (!pHHInfo)
456         return;
457
458     /* Only resize the Navigation pane vertically */
459     if (HIWORD(lParam))
460     {
461         NP_GetNavigationRect(pHHInfo, &rc);
462         SetWindowPos(pHHInfo->pHHWinType->hwndNavigation, HWND_TOP, 0, 0,
463                      rc.right, rc.bottom, SWP_NOMOVE);
464
465         GetClientRect(pHHInfo->pHHWinType->hwndNavigation, &rc);
466         SetWindowPos(pHHInfo->hwndTabCtrl, HWND_TOP, 0, 0,
467                      rc.right - TAB_PADDING - TAB_RIGHT_PADDING,
468                      rc.bottom - TAB_PADDING - TAB_TOP_PADDING, SWP_NOMOVE);
469     }
470
471     HP_GetHTMLRect(pHHInfo, &rc);
472     SetWindowPos(pHHInfo->pHHWinType->hwndHTML, HWND_TOP, 0, 0,
473                  LOWORD(lParam), HIWORD(lParam), SWP_NOMOVE);
474 }
475
476 LRESULT CALLBACK Help_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
477 {
478     PAINTSTRUCT ps;
479     HDC hdc;
480
481     switch (message)
482     {
483         case WM_COMMAND:
484             if (HIWORD(wParam) == BN_CLICKED)
485                 TB_OnClick(hWnd, LOWORD(wParam));
486             break;
487         case WM_SIZE:
488             Help_OnSize(hWnd, lParam);
489             break;
490         case WM_PAINT:
491             hdc = BeginPaint(hWnd, &ps);
492             EndPaint(hWnd, &ps);
493             break;
494         case WM_DESTROY:
495             PostQuitMessage(0);
496             break;
497
498         default:
499             return DefWindowProcW(hWnd, message, wParam, lParam);
500     }
501
502     return 0;
503 }
504
505 static BOOL HH_CreateHelpWindow(HHInfo *pHHInfo)
506 {
507     HWND hWnd;
508     HINSTANCE hInstance = pHHInfo->hInstance;
509     RECT winPos = pHHInfo->pHHWinType->rcWindowPos;
510     WNDCLASSEXW wcex;
511     DWORD dwStyles, dwExStyles;
512     DWORD x, y, width, height;
513
514     static const WCHAR windowClassW[] = {
515         'H','H',' ', 'P','a','r','e','n','t',0
516     };
517
518     wcex.cbSize         = sizeof(WNDCLASSEXW);
519     wcex.style          = CS_HREDRAW | CS_VREDRAW;
520     wcex.lpfnWndProc    = (WNDPROC)Help_WndProc;
521     wcex.cbClsExtra     = 0;
522     wcex.cbWndExtra     = 0;
523     wcex.hInstance      = hInstance;
524     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
525     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
526     wcex.hbrBackground  = (HBRUSH)(COLOR_MENU + 1);
527     wcex.lpszMenuName   = NULL;
528     wcex.lpszClassName  = windowClassW;
529     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
530
531     RegisterClassExW(&wcex);
532
533     /* Read in window parameters if available */
534     if (pHHInfo->pHHWinType->fsValidMembers & HHWIN_PARAM_STYLES)
535         dwStyles = pHHInfo->pHHWinType->dwStyles;
536     else
537         dwStyles = WS_OVERLAPPEDWINDOW | WS_VISIBLE |
538                    WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
539
540     if (pHHInfo->pHHWinType->fsValidMembers & HHWIN_PARAM_EXSTYLES)
541         dwExStyles = pHHInfo->pHHWinType->dwExStyles;
542     else
543         dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_APPWINDOW |
544                      WS_EX_WINDOWEDGE | WS_EX_RIGHTSCROLLBAR;
545
546     if (pHHInfo->pHHWinType->fsValidMembers & HHWIN_PARAM_RECT)
547     {
548         x = winPos.left;
549         y = winPos.top;
550         width = winPos.right - x;
551         height = winPos.bottom - y;
552     }
553     else
554     {
555         x = WINTYPE_DEFAULT_X;
556         y = WINTYPE_DEFAULT_Y;
557         width = WINTYPE_DEFAULT_WIDTH;
558         height = WINTYPE_DEFAULT_HEIGHT;
559     }
560
561     hWnd = CreateWindowExW(dwExStyles, windowClassW, pHHInfo->pHHWinType->pszCaption,
562                            dwStyles, x, y, width, height, NULL, NULL, hInstance, NULL);
563     if (!hWnd)
564         return FALSE;
565
566     ShowWindow(hWnd, SW_SHOW);
567     UpdateWindow(hWnd);
568
569     /* store the pointer to the HH info struct */
570     SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
571
572     pHHInfo->pHHWinType->hwndHelp = hWnd;
573     return TRUE;
574 }
575
576 static void HH_CreateFont(HHInfo *pHHInfo)
577 {
578     LOGFONTW lf;
579
580     GetObjectW(GetStockObject(ANSI_VAR_FONT), sizeof(LOGFONTW), &lf);
581     lf.lfWeight = FW_NORMAL;
582     lf.lfItalic = FALSE;
583     lf.lfUnderline = FALSE;
584
585     pHHInfo->hFont = CreateFontIndirectW(&lf);
586 }
587
588 static void HH_InitRequiredControls(DWORD dwControls)
589 {
590     INITCOMMONCONTROLSEX icex;
591
592     icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
593     icex.dwICC = dwControls;
594     InitCommonControlsEx(&icex);
595 }
596
597 /* Creates the whole package */
598 static BOOL HH_CreateViewer(HHInfo *pHHInfo)
599 {
600     HH_CreateFont(pHHInfo);
601
602     if (!HH_CreateHelpWindow(pHHInfo))
603         return FALSE;
604
605     HH_InitRequiredControls(ICC_BAR_CLASSES);
606
607     if (!HH_AddToolbar(pHHInfo))
608         return FALSE;
609
610     HH_RegisterChildWndClass(pHHInfo);
611
612     if (!HH_AddNavigationPane(pHHInfo))
613         return FALSE;
614
615     if (!HH_AddHTMLPane(pHHInfo))
616         return FALSE;
617
618     return TRUE;
619 }
620
621 static HHInfo *HH_OpenHH(HINSTANCE hInstance, LPWSTR szCmdLine)
622 {
623     HHInfo *pHHInfo = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HHInfo));
624
625     pHHInfo->pHHWinType = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HH_WINTYPEW));
626     pHHInfo->pCHMInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(CHMInfo));
627     pHHInfo->pWBInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(WBInfo));
628     pHHInfo->hInstance = hInstance;
629     pHHInfo->szCmdLine = szCmdLine;
630
631     return pHHInfo;
632 }
633
634 static void HH_Close(HHInfo *pHHInfo)
635 {
636     if (!pHHInfo)
637         return;
638
639     /* Free allocated strings */
640     if (pHHInfo->pHHWinType)
641     {
642         HeapFree(GetProcessHeap(), 0, (LPWSTR)pHHInfo->pHHWinType->pszType);
643         HeapFree(GetProcessHeap(), 0, (LPWSTR)pHHInfo->pHHWinType->pszCaption);
644         HeapFree(GetProcessHeap(), 0, (LPWSTR)pHHInfo->pHHWinType->pszToc);
645         HeapFree(GetProcessHeap(), 0, (LPWSTR)pHHInfo->pHHWinType->pszType);
646         HeapFree(GetProcessHeap(), 0, (LPWSTR)pHHInfo->pHHWinType->pszIndex);
647         HeapFree(GetProcessHeap(), 0, (LPWSTR)pHHInfo->pHHWinType->pszFile);
648         HeapFree(GetProcessHeap(), 0, (LPWSTR)pHHInfo->pHHWinType->pszHome);
649         HeapFree(GetProcessHeap(), 0, (LPWSTR)pHHInfo->pHHWinType->pszJump1);
650         HeapFree(GetProcessHeap(), 0, (LPWSTR)pHHInfo->pHHWinType->pszJump2);
651         HeapFree(GetProcessHeap(), 0, (LPWSTR)pHHInfo->pHHWinType->pszUrlJump1);
652         HeapFree(GetProcessHeap(), 0, (LPWSTR)pHHInfo->pHHWinType->pszUrlJump2);
653     }
654
655     HeapFree(GetProcessHeap(), 0, pHHInfo->pHHWinType);
656     HeapFree(GetProcessHeap(), 0, pHHInfo->szCmdLine);
657
658     if (pHHInfo->pCHMInfo)
659     {
660         CHM_CloseCHM(pHHInfo->pCHMInfo);
661         HeapFree(GetProcessHeap(), 0, pHHInfo->pCHMInfo);
662     }
663
664     if (pHHInfo->pWBInfo)
665     {
666         WB_UnEmbedBrowser(pHHInfo->pWBInfo);
667         HeapFree(GetProcessHeap(), 0, pHHInfo->pWBInfo);
668     }
669 }
670
671 static void HH_OpenDefaultTopic(HHInfo *pHHInfo)
672 {
673     WCHAR url[MAX_PATH];
674     LPCWSTR defTopic = pHHInfo->pHHWinType->pszFile;
675
676     CHM_CreateITSUrl(pHHInfo->pCHMInfo, defTopic, url);
677     WB_Navigate(pHHInfo->pWBInfo, url);
678 }
679
680 static BOOL HH_OpenCHM(HHInfo *pHHInfo)
681 {
682     if (!CHM_OpenCHM(pHHInfo->pCHMInfo, pHHInfo->szCmdLine))
683         return FALSE;
684
685     if (!CHM_LoadWinTypeFromCHM(pHHInfo->pCHMInfo, pHHInfo->pHHWinType))
686         return FALSE;
687
688     return TRUE;
689 }
690
691 /* FIXME: Check szCmdLine for bad arguments */
692 int WINAPI doWinMain(HINSTANCE hInstance, LPSTR szCmdLine)
693 {
694     MSG msg;
695     HHInfo *pHHInfo;
696
697     if (OleInitialize(NULL) != S_OK)
698         return -1;
699
700     pHHInfo = HH_OpenHH(hInstance, HH_ANSIToUnicode(szCmdLine));
701     if (!pHHInfo || !HH_OpenCHM(pHHInfo) || !HH_CreateViewer(pHHInfo))
702     {
703         OleUninitialize();
704         return -1;
705     }
706
707     HH_OpenDefaultTopic(pHHInfo);
708     
709     while (GetMessageW(&msg, 0, 0, 0))
710     {
711         TranslateMessage(&msg);
712         DispatchMessageW(&msg);
713     }
714
715     HH_Close(pHHInfo);
716     HeapFree(GetProcessHeap(), 0, pHHInfo);
717     OleUninitialize();
718
719     return 0;
720 }