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