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