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