wine.inf: We should not override existing associations.
[wine/hacks.git] / dlls / hhctrl.ocx / help.c
blob2c288b67be3f89350897792699c77f304a78b8a4
1 /*
2 * Help Viewer Implementation
4 * Copyright 2005 James Hawkins
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.
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.
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
21 #include <stdarg.h>
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"
33 #include "resource.h"
34 #include "chm.h"
35 #include "webbrowser.h"
37 static void Help_OnSize(HWND hWnd);
39 /* Window type defaults */
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
47 static const WCHAR szEmpty[] = {0};
49 typedef struct tagHHInfo
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;
61 extern HINSTANCE hhctrl_hinstance;
63 static LPWSTR HH_ANSIToUnicode(LPCSTR ansi)
65 LPWSTR unicode;
66 int count;
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);
72 return unicode;
75 /* Loads a string from the resource file */
76 static LPWSTR HH_LoadString(DWORD dwID)
78 LPWSTR string = NULL;
79 int iSize;
81 iSize = LoadStringW(hhctrl_hinstance, dwID, NULL, 0);
82 iSize += 2; /* some strings (tab text) needs double-null termination */
84 string = HeapAlloc(GetProcessHeap(), 0, iSize * sizeof(WCHAR));
85 LoadStringW(hhctrl_hinstance, dwID, string, iSize);
87 return string;
90 /* Size Bar */
92 #define SIZEBAR_WIDTH 4
94 static const WCHAR szSizeBarClass[] = {
95 'H','H',' ','S','i','z','e','B','a','r',0
98 /* Draw the SizeBar */
99 static void SB_OnPaint(HWND hWnd)
101 PAINTSTRUCT ps;
102 HDC hdc;
103 RECT rc;
105 hdc = BeginPaint(hWnd, &ps);
107 GetClientRect(hWnd, &rc);
109 /* dark frame */
110 rc.right += 1;
111 rc.bottom -= 1;
112 FrameRect(hdc, &rc, GetStockObject(GRAY_BRUSH));
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);
121 MoveToEx(hdc, 0, rc.bottom, NULL);
122 LineTo(hdc, rc.right, rc.bottom);
124 EndPaint(hWnd, &ps);
127 static void SB_OnLButtonDown(HWND hWnd, WPARAM wParam, LPARAM lParam)
129 SetCapture(hWnd);
132 static void SB_OnLButtonUp(HWND hWnd, WPARAM wParam, LPARAM lParam)
134 HHInfo *pHHInfo = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
135 POINTS pt = MAKEPOINTS(lParam);
137 /* update the window sizes */
138 pHHInfo->pHHWinType->iNavWidth += pt.x;
139 Help_OnSize(hWnd);
141 ReleaseCapture();
144 static void SB_OnMouseMove(HWND hWnd, WPARAM wParam, LPARAM lParam)
146 /* ignore WM_MOUSEMOVE if not dragging the SizeBar */
147 if (!(wParam & MK_LBUTTON))
148 return;
151 static LRESULT CALLBACK SizeBar_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
153 switch (message)
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);
171 return 0;
174 static void HH_RegisterSizeBarClass(HHInfo *pHHInfo)
176 WNDCLASSEXW wcex;
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);
191 RegisterClassExW(&wcex);
194 static void SB_GetSizeBarRect(HHInfo *pHHInfo, RECT *rc)
196 RECT rectWND, rectTB, rectNP;
198 GetClientRect(pHHInfo->pHHWinType->hwndHelp, &rectWND);
199 GetClientRect(pHHInfo->pHHWinType->hwndToolBar, &rectTB);
200 GetClientRect(pHHInfo->pHHWinType->hwndNavigation, &rectNP);
202 rc->left = rectNP.right;
203 rc->top = rectTB.bottom;
204 rc->bottom = rectWND.bottom - rectTB.bottom;
205 rc->right = SIZEBAR_WIDTH;
208 static BOOL HH_AddSizeBar(HHInfo *pHHInfo)
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;
216 SB_GetSizeBarRect(pHHInfo, &rc);
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;
224 /* store the pointer to the HH info struct */
225 SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
227 pHHInfo->hwndSizeBar = hWnd;
228 return TRUE;
231 /* Child Window */
233 static const WCHAR szChildClass[] = {
234 'H','H',' ','C','h','i','l','d',0
237 static void Child_OnPaint(HWND hWnd)
239 PAINTSTRUCT ps;
240 HDC hdc;
241 RECT rc;
243 hdc = BeginPaint(hWnd, &ps);
245 /* Only paint the Navigation pane, identified by the fact
246 * that it has a child window
248 if (GetWindow(hWnd, GW_CHILD))
250 GetClientRect(hWnd, &rc);
252 /* set the border color */
253 SelectObject(hdc, GetStockObject(DC_PEN));
254 SetDCPenColor(hdc, GetSysColor(COLOR_BTNSHADOW));
256 /* Draw the top border */
257 LineTo(hdc, rc.right, 0);
259 SelectObject(hdc, GetStockObject(WHITE_PEN));
260 MoveToEx(hdc, 0, 1, NULL);
261 LineTo(hdc, rc.right, 1);
264 EndPaint(hWnd, &ps);
267 static LRESULT CALLBACK Child_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
269 switch (message)
271 case WM_PAINT:
272 Child_OnPaint(hWnd);
273 break;
274 default:
275 return DefWindowProcW(hWnd, message, wParam, lParam);
278 return 0;
281 static void HH_RegisterChildWndClass(HHInfo *pHHInfo)
283 WNDCLASSEXW wcex;
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);
298 RegisterClassExW(&wcex);
301 /* Toolbar */
303 #define ICON_SIZE 20
305 static void TB_OnClick(HWND hWnd, DWORD dwID)
307 HHInfo *pHHInfo = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
309 switch (dwID)
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:
322 WCHAR szUrl[MAX_PATH];
324 CHM_CreateITSUrl(pHHInfo->pCHMInfo, pHHInfo->pHHWinType->pszHome, szUrl);
325 WB_Navigate(pHHInfo->pWBInfo, szUrl);
326 break;
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;
348 static void TB_AddButton(TBBUTTON *pButtons, DWORD dwIndex, DWORD dwID)
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;
359 static void TB_AddButtonsFromFlags(TBBUTTON *pButtons, DWORD dwButtonFlags, LPDWORD pdwNumButtons)
361 *pdwNumButtons = 0;
363 if (dwButtonFlags & HHWIN_BUTTON_EXPAND)
364 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_EXPAND);
366 if (dwButtonFlags & HHWIN_BUTTON_BACK)
367 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_BACK);
369 if (dwButtonFlags & HHWIN_BUTTON_FORWARD)
370 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_FORWARD);
372 if (dwButtonFlags & HHWIN_BUTTON_STOP)
373 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_STOP);
375 if (dwButtonFlags & HHWIN_BUTTON_REFRESH)
376 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_REFRESH);
378 if (dwButtonFlags & HHWIN_BUTTON_HOME)
379 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_HOME);
381 if (dwButtonFlags & HHWIN_BUTTON_SYNC)
382 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_SYNC);
384 if (dwButtonFlags & HHWIN_BUTTON_OPTIONS)
385 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_OPTIONS);
387 if (dwButtonFlags & HHWIN_BUTTON_PRINT)
388 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_PRINT);
390 if (dwButtonFlags & HHWIN_BUTTON_JUMP1)
391 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_JUMP1);
393 if (dwButtonFlags & HHWIN_BUTTON_JUMP2)
394 TB_AddButton(pButtons,(*pdwNumButtons)++, IDTB_JUMP2);
396 if (dwButtonFlags & HHWIN_BUTTON_ZOOM)
397 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_ZOOM);
399 if (dwButtonFlags & HHWIN_BUTTON_TOC_NEXT)
400 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_TOC_NEXT);
402 if (dwButtonFlags & HHWIN_BUTTON_TOC_PREV)
403 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_TOC_PREV);
406 static BOOL HH_AddToolbar(HHInfo *pHHInfo)
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;
416 if (pHHInfo->pHHWinType->fsWinProperties & HHWIN_PARAM_TB_FLAGS)
417 toolbarFlags = pHHInfo->pHHWinType->fsToolBarFlags;
418 else
419 toolbarFlags = HHWIN_DEF_BUTTONS;
421 TB_AddButtonsFromFlags(buttons, toolbarFlags, &dwNumButtons);
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;
427 hToolbar = CreateWindowExW(dwExStyles, TOOLBARCLASSNAMEW, NULL, dwStyles,
428 0, 0, 0, 0, hwndParent, NULL,
429 pHHInfo->hInstance, NULL);
430 if (!hToolbar)
431 return FALSE;
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);
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);
442 for (dwIndex = 0; dwIndex < dwNumButtons; dwIndex++)
444 LPWSTR szBuf = HH_LoadString(buttons[dwIndex].idCommand);
445 DWORD dwLen = strlenW(szBuf);
446 szBuf[dwLen + 2] = 0; /* Double-null terminate */
448 buttons[dwIndex].iString = (DWORD)SendMessageW(hToolbar, TB_ADDSTRINGW, 0, (LPARAM)szBuf);
449 HeapFree(GetProcessHeap(), 0, szBuf);
452 SendMessageW(hToolbar, TB_ADDBUTTONSW, dwNumButtons, (LPARAM)&buttons);
453 SendMessageW(hToolbar, TB_AUTOSIZE, 0, 0);
454 ShowWindow(hToolbar, SW_SHOW);
456 pHHInfo->pHHWinType->hwndToolBar = hToolbar;
457 return TRUE;
460 /* Navigation Pane */
462 #define TAB_TOP_PADDING 8
463 #define TAB_RIGHT_PADDING 4
465 static void NP_GetNavigationRect(HHInfo *pHHInfo, RECT *rc)
467 HWND hwndParent = pHHInfo->pHHWinType->hwndHelp;
468 HWND hwndToolbar = pHHInfo->pHHWinType->hwndToolBar;
469 RECT rectWND, rectTB;
471 GetClientRect(hwndParent, &rectWND);
472 GetClientRect(hwndToolbar, &rectTB);
474 rc->left = 0;
475 rc->top = rectTB.bottom;
476 rc->bottom = rectWND.bottom - rectTB.bottom;
478 if (!(pHHInfo->pHHWinType->fsValidMembers & HHWIN_PARAM_NAV_WIDTH) &&
479 pHHInfo->pHHWinType->iNavWidth == 0)
481 pHHInfo->pHHWinType->iNavWidth = WINTYPE_DEFAULT_NAVWIDTH;
484 rc->right = pHHInfo->pHHWinType->iNavWidth;
487 static void NP_CreateTab(HINSTANCE hInstance, HWND hwndTabCtrl, DWORD dwStrID, DWORD dwIndex)
489 TCITEMW tie;
490 LPWSTR tabText = HH_LoadString(dwStrID);
492 tie.mask = TCIF_TEXT;
493 tie.pszText = tabText;
495 SendMessageW( hwndTabCtrl, TCM_INSERTITEMW, dwIndex, (LPARAM)&tie );
496 HeapFree(GetProcessHeap(), 0, tabText);
499 static BOOL HH_AddNavigationPane(HHInfo *pHHInfo)
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;
508 NP_GetNavigationRect(pHHInfo, &rc);
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;
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;
524 if (*pHHInfo->pHHWinType->pszToc)
525 NP_CreateTab(pHHInfo->hInstance, hwndTabCtrl, IDS_CONTENTS, dwIndex++);
527 if (*pHHInfo->pHHWinType->pszIndex)
528 NP_CreateTab(pHHInfo->hInstance, hwndTabCtrl, IDS_INDEX, dwIndex++);
530 if (pHHInfo->pHHWinType->fsWinProperties & HHWIN_PROP_TAB_SEARCH)
531 NP_CreateTab(pHHInfo->hInstance, hwndTabCtrl, IDS_SEARCH, dwIndex++);
533 if (pHHInfo->pHHWinType->fsWinProperties & HHWIN_PROP_TAB_FAVORITES)
534 NP_CreateTab(pHHInfo->hInstance, hwndTabCtrl, IDS_FAVORITES, dwIndex++);
536 SendMessageW(hwndTabCtrl, WM_SETFONT, (WPARAM)pHHInfo->hFont, TRUE);
538 pHHInfo->hwndTabCtrl = hwndTabCtrl;
539 pHHInfo->pHHWinType->hwndNavigation = hWnd;
540 return TRUE;
543 /* HTML Pane */
545 static void HP_GetHTMLRect(HHInfo *pHHInfo, RECT *rc)
547 RECT rectTB, rectWND, rectNP, rectSB;
549 GetClientRect(pHHInfo->pHHWinType->hwndHelp, &rectWND);
550 GetClientRect(pHHInfo->pHHWinType->hwndToolBar, &rectTB);
551 GetClientRect(pHHInfo->pHHWinType->hwndNavigation, &rectNP);
552 GetClientRect(pHHInfo->hwndSizeBar, &rectSB);
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;
560 static BOOL HH_AddHTMLPane(HHInfo *pHHInfo)
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;
568 HP_GetHTMLRect(pHHInfo, &rc);
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;
576 if (!WB_EmbedBrowser(pHHInfo->pWBInfo, hWnd))
577 return FALSE;
579 /* store the pointer to the HH info struct */
580 SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
582 ShowWindow(hWnd, SW_SHOW);
583 UpdateWindow(hWnd);
585 pHHInfo->pHHWinType->hwndHTML = hWnd;
586 return TRUE;
589 /* Viewer Window */
591 static void Help_OnSize(HWND hWnd)
593 HHInfo *pHHInfo = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
594 DWORD dwSize;
595 RECT rc;
597 if (!pHHInfo)
598 return;
600 NP_GetNavigationRect(pHHInfo, &rc);
601 SetWindowPos(pHHInfo->pHHWinType->hwndNavigation, HWND_TOP, 0, 0,
602 rc.right, rc.bottom, SWP_NOMOVE);
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);
609 SB_GetSizeBarRect(pHHInfo, &rc);
610 SetWindowPos(pHHInfo->hwndSizeBar, HWND_TOP, rc.left, rc.top,
611 rc.right, rc.bottom, SWP_SHOWWINDOW);
613 HP_GetHTMLRect(pHHInfo, &rc);
614 SetWindowPos(pHHInfo->pHHWinType->hwndHTML, HWND_TOP, rc.left, rc.top,
615 rc.right, rc.bottom, SWP_SHOWWINDOW);
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);
622 static LRESULT CALLBACK Help_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
624 PAINTSTRUCT ps;
625 HDC hdc;
627 switch (message)
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;
644 default:
645 return DefWindowProcW(hWnd, message, wParam, lParam);
648 return 0;
651 static BOOL HH_CreateHelpWindow(HHInfo *pHHInfo)
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;
660 static const WCHAR windowClassW[] = {
661 'H','H',' ', 'P','a','r','e','n','t',0
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);
677 RegisterClassExW(&wcex);
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;
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;
692 if (pHHInfo->pHHWinType->fsValidMembers & HHWIN_PARAM_RECT)
694 x = winPos.left;
695 y = winPos.top;
696 width = winPos.right - x;
697 height = winPos.bottom - y;
699 else
701 x = WINTYPE_DEFAULT_X;
702 y = WINTYPE_DEFAULT_Y;
703 width = WINTYPE_DEFAULT_WIDTH;
704 height = WINTYPE_DEFAULT_HEIGHT;
707 hWnd = CreateWindowExW(dwExStyles, windowClassW, pHHInfo->pHHWinType->pszCaption,
708 dwStyles, x, y, width, height, NULL, NULL, hInstance, NULL);
709 if (!hWnd)
710 return FALSE;
712 ShowWindow(hWnd, SW_SHOW);
713 UpdateWindow(hWnd);
715 /* store the pointer to the HH info struct */
716 SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
718 pHHInfo->pHHWinType->hwndHelp = hWnd;
719 return TRUE;
722 static void HH_CreateFont(HHInfo *pHHInfo)
724 LOGFONTW lf;
726 GetObjectW(GetStockObject(ANSI_VAR_FONT), sizeof(LOGFONTW), &lf);
727 lf.lfWeight = FW_NORMAL;
728 lf.lfItalic = FALSE;
729 lf.lfUnderline = FALSE;
731 pHHInfo->hFont = CreateFontIndirectW(&lf);
734 static void HH_InitRequiredControls(DWORD dwControls)
736 INITCOMMONCONTROLSEX icex;
738 icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
739 icex.dwICC = dwControls;
740 InitCommonControlsEx(&icex);
743 /* Creates the whole package */
744 static BOOL HH_CreateViewer(HHInfo *pHHInfo)
746 HH_CreateFont(pHHInfo);
748 if (!HH_CreateHelpWindow(pHHInfo))
749 return FALSE;
751 HH_InitRequiredControls(ICC_BAR_CLASSES);
753 if (!HH_AddToolbar(pHHInfo))
754 return FALSE;
756 HH_RegisterChildWndClass(pHHInfo);
758 if (!HH_AddNavigationPane(pHHInfo))
759 return FALSE;
761 HH_RegisterSizeBarClass(pHHInfo);
763 if (!HH_AddSizeBar(pHHInfo))
764 return FALSE;
766 if (!HH_AddHTMLPane(pHHInfo))
767 return FALSE;
769 return TRUE;
772 static HHInfo *HH_OpenHH(HINSTANCE hInstance, LPWSTR szCmdLine)
774 HHInfo *pHHInfo = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HHInfo));
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;
782 return pHHInfo;
785 static void HH_Close(HHInfo *pHHInfo)
787 if (!pHHInfo)
788 return;
790 /* Free allocated strings */
791 if (pHHInfo->pHHWinType)
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);
806 HeapFree(GetProcessHeap(), 0, pHHInfo->pHHWinType);
807 HeapFree(GetProcessHeap(), 0, pHHInfo->szCmdLine);
809 if (pHHInfo->pCHMInfo)
811 CHM_CloseCHM(pHHInfo->pCHMInfo);
812 HeapFree(GetProcessHeap(), 0, pHHInfo->pCHMInfo);
815 if (pHHInfo->pWBInfo)
817 WB_UnEmbedBrowser(pHHInfo->pWBInfo);
818 HeapFree(GetProcessHeap(), 0, pHHInfo->pWBInfo);
822 static void HH_OpenDefaultTopic(HHInfo *pHHInfo)
824 WCHAR url[MAX_PATH];
825 LPCWSTR defTopic = pHHInfo->pHHWinType->pszFile;
827 CHM_CreateITSUrl(pHHInfo->pCHMInfo, defTopic, url);
828 WB_Navigate(pHHInfo->pWBInfo, url);
831 static BOOL HH_OpenCHM(HHInfo *pHHInfo)
833 if (!CHM_OpenCHM(pHHInfo->pCHMInfo, pHHInfo->szCmdLine))
834 return FALSE;
836 if (!CHM_LoadWinTypeFromCHM(pHHInfo->pCHMInfo, pHHInfo->pHHWinType))
837 return FALSE;
839 return TRUE;
842 /* FIXME: Check szCmdLine for bad arguments */
843 int WINAPI doWinMain(HINSTANCE hInstance, LPSTR szCmdLine)
845 MSG msg;
846 HHInfo *pHHInfo;
848 if (OleInitialize(NULL) != S_OK)
849 return -1;
851 pHHInfo = HH_OpenHH(hInstance, HH_ANSIToUnicode(szCmdLine));
852 if (!pHHInfo || !HH_OpenCHM(pHHInfo) || !HH_CreateViewer(pHHInfo))
854 OleUninitialize();
855 return -1;
858 HH_OpenDefaultTopic(pHHInfo);
860 while (GetMessageW(&msg, 0, 0, 0))
862 TranslateMessage(&msg);
863 DispatchMessageW(&msg);
866 HH_Close(pHHInfo);
867 HeapFree(GetProcessHeap(), 0, pHHInfo);
868 OleUninitialize();
870 return 0;