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