wined3d: Do not perform a NULL check on riid (Coverity).
[wine/multimedia.git] / dlls / hhctrl.ocx / help.c
blob426996d48ea9150ed597e191077d18d04b0e3315
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 POINT pt;
137 pt.x = (short)LOWORD(lParam);
138 pt.y = (short)HIWORD(lParam);
140 /* update the window sizes */
141 pHHInfo->pHHWinType->iNavWidth += pt.x;
142 Help_OnSize(hWnd);
144 ReleaseCapture();
147 static void SB_OnMouseMove(HWND hWnd, WPARAM wParam, LPARAM lParam)
149 /* ignore WM_MOUSEMOVE if not dragging the SizeBar */
150 if (!(wParam & MK_LBUTTON))
151 return;
154 static LRESULT CALLBACK SizeBar_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
156 switch (message)
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);
174 return 0;
177 static void HH_RegisterSizeBarClass(HHInfo *pHHInfo)
179 WNDCLASSEXW wcex;
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);
194 RegisterClassExW(&wcex);
197 static void SB_GetSizeBarRect(HHInfo *pHHInfo, RECT *rc)
199 RECT rectWND, rectTB, rectNP;
201 GetClientRect(pHHInfo->pHHWinType->hwndHelp, &rectWND);
202 GetClientRect(pHHInfo->pHHWinType->hwndToolBar, &rectTB);
203 GetClientRect(pHHInfo->pHHWinType->hwndNavigation, &rectNP);
205 rc->left = rectNP.right;
206 rc->top = rectTB.bottom;
207 rc->bottom = rectWND.bottom - rectTB.bottom;
208 rc->right = SIZEBAR_WIDTH;
211 static BOOL HH_AddSizeBar(HHInfo *pHHInfo)
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;
219 SB_GetSizeBarRect(pHHInfo, &rc);
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;
227 /* store the pointer to the HH info struct */
228 SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
230 pHHInfo->hwndSizeBar = hWnd;
231 return TRUE;
234 /* Child Window */
236 static const WCHAR szChildClass[] = {
237 'H','H',' ','C','h','i','l','d',0
240 static void Child_OnPaint(HWND hWnd)
242 PAINTSTRUCT ps;
243 HDC hdc;
244 RECT rc;
246 hdc = BeginPaint(hWnd, &ps);
248 /* Only paint the Navigation pane, identified by the fact
249 * that it has a child window
251 if (GetWindow(hWnd, GW_CHILD))
253 GetClientRect(hWnd, &rc);
255 /* set the border color */
256 SelectObject(hdc, GetStockObject(DC_PEN));
257 SetDCPenColor(hdc, GetSysColor(COLOR_BTNSHADOW));
259 /* Draw the top border */
260 LineTo(hdc, rc.right, 0);
262 SelectObject(hdc, GetStockObject(WHITE_PEN));
263 MoveToEx(hdc, 0, 1, NULL);
264 LineTo(hdc, rc.right, 1);
267 EndPaint(hWnd, &ps);
270 static LRESULT CALLBACK Child_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
272 switch (message)
274 case WM_PAINT:
275 Child_OnPaint(hWnd);
276 break;
277 default:
278 return DefWindowProcW(hWnd, message, wParam, lParam);
281 return 0;
284 static void HH_RegisterChildWndClass(HHInfo *pHHInfo)
286 WNDCLASSEXW wcex;
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);
301 RegisterClassExW(&wcex);
304 /* Toolbar */
306 #define ICON_SIZE 20
308 static void TB_OnClick(HWND hWnd, DWORD dwID)
310 HHInfo *pHHInfo = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
312 switch (dwID)
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:
325 WCHAR szUrl[MAX_PATH];
327 CHM_CreateITSUrl(pHHInfo->pCHMInfo, pHHInfo->pHHWinType->pszHome, szUrl);
328 WB_Navigate(pHHInfo->pWBInfo, szUrl);
329 break;
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;
351 static void TB_AddButton(TBBUTTON *pButtons, DWORD dwIndex, DWORD dwID)
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;
362 static void TB_AddButtonsFromFlags(TBBUTTON *pButtons, DWORD dwButtonFlags, LPDWORD pdwNumButtons)
364 *pdwNumButtons = 0;
366 if (dwButtonFlags & HHWIN_BUTTON_EXPAND)
367 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_EXPAND);
369 if (dwButtonFlags & HHWIN_BUTTON_BACK)
370 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_BACK);
372 if (dwButtonFlags & HHWIN_BUTTON_FORWARD)
373 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_FORWARD);
375 if (dwButtonFlags & HHWIN_BUTTON_STOP)
376 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_STOP);
378 if (dwButtonFlags & HHWIN_BUTTON_REFRESH)
379 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_REFRESH);
381 if (dwButtonFlags & HHWIN_BUTTON_HOME)
382 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_HOME);
384 if (dwButtonFlags & HHWIN_BUTTON_SYNC)
385 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_SYNC);
387 if (dwButtonFlags & HHWIN_BUTTON_OPTIONS)
388 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_OPTIONS);
390 if (dwButtonFlags & HHWIN_BUTTON_PRINT)
391 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_PRINT);
393 if (dwButtonFlags & HHWIN_BUTTON_JUMP1)
394 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_JUMP1);
396 if (dwButtonFlags & HHWIN_BUTTON_JUMP2)
397 TB_AddButton(pButtons,(*pdwNumButtons)++, IDTB_JUMP2);
399 if (dwButtonFlags & HHWIN_BUTTON_ZOOM)
400 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_ZOOM);
402 if (dwButtonFlags & HHWIN_BUTTON_TOC_NEXT)
403 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_TOC_NEXT);
405 if (dwButtonFlags & HHWIN_BUTTON_TOC_PREV)
406 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_TOC_PREV);
409 static BOOL HH_AddToolbar(HHInfo *pHHInfo)
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;
419 if (pHHInfo->pHHWinType->fsWinProperties & HHWIN_PARAM_TB_FLAGS)
420 toolbarFlags = pHHInfo->pHHWinType->fsToolBarFlags;
421 else
422 toolbarFlags = HHWIN_DEF_BUTTONS;
424 TB_AddButtonsFromFlags(buttons, toolbarFlags, &dwNumButtons);
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;
430 hToolbar = CreateWindowExW(dwExStyles, TOOLBARCLASSNAMEW, NULL, dwStyles,
431 0, 0, 0, 0, hwndParent, NULL,
432 pHHInfo->hInstance, NULL);
433 if (!hToolbar)
434 return FALSE;
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);
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);
445 for (dwIndex = 0; dwIndex < dwNumButtons; dwIndex++)
447 LPWSTR szBuf = HH_LoadString(buttons[dwIndex].idCommand);
448 DWORD dwLen = strlenW(szBuf);
449 szBuf[dwLen + 2] = 0; /* Double-null terminate */
451 buttons[dwIndex].iString = (DWORD)SendMessageW(hToolbar, TB_ADDSTRINGW, 0, (LPARAM)szBuf);
452 HeapFree(GetProcessHeap(), 0, szBuf);
455 SendMessageW(hToolbar, TB_ADDBUTTONSW, dwNumButtons, (LPARAM)&buttons);
456 SendMessageW(hToolbar, TB_AUTOSIZE, 0, 0);
457 ShowWindow(hToolbar, SW_SHOW);
459 pHHInfo->pHHWinType->hwndToolBar = hToolbar;
460 return TRUE;
463 /* Navigation Pane */
465 #define TAB_TOP_PADDING 8
466 #define TAB_RIGHT_PADDING 4
468 static void NP_GetNavigationRect(HHInfo *pHHInfo, RECT *rc)
470 HWND hwndParent = pHHInfo->pHHWinType->hwndHelp;
471 HWND hwndToolbar = pHHInfo->pHHWinType->hwndToolBar;
472 RECT rectWND, rectTB;
474 GetClientRect(hwndParent, &rectWND);
475 GetClientRect(hwndToolbar, &rectTB);
477 rc->left = 0;
478 rc->top = rectTB.bottom;
479 rc->bottom = rectWND.bottom - rectTB.bottom;
481 if (!(pHHInfo->pHHWinType->fsValidMembers & HHWIN_PARAM_NAV_WIDTH) &&
482 pHHInfo->pHHWinType->iNavWidth == 0)
484 pHHInfo->pHHWinType->iNavWidth = WINTYPE_DEFAULT_NAVWIDTH;
487 rc->right = pHHInfo->pHHWinType->iNavWidth;
490 static void NP_CreateTab(HINSTANCE hInstance, HWND hwndTabCtrl, DWORD dwStrID, DWORD dwIndex)
492 TCITEMW tie;
493 LPWSTR tabText = HH_LoadString(dwStrID);
495 tie.mask = TCIF_TEXT;
496 tie.pszText = tabText;
498 SendMessageW( hwndTabCtrl, TCM_INSERTITEMW, dwIndex, (LPARAM)&tie );
499 HeapFree(GetProcessHeap(), 0, tabText);
502 static BOOL HH_AddNavigationPane(HHInfo *pHHInfo)
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;
511 NP_GetNavigationRect(pHHInfo, &rc);
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;
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;
527 if (*pHHInfo->pHHWinType->pszToc)
528 NP_CreateTab(pHHInfo->hInstance, hwndTabCtrl, IDS_CONTENTS, dwIndex++);
530 if (*pHHInfo->pHHWinType->pszIndex)
531 NP_CreateTab(pHHInfo->hInstance, hwndTabCtrl, IDS_INDEX, dwIndex++);
533 if (pHHInfo->pHHWinType->fsWinProperties & HHWIN_PROP_TAB_SEARCH)
534 NP_CreateTab(pHHInfo->hInstance, hwndTabCtrl, IDS_SEARCH, dwIndex++);
536 if (pHHInfo->pHHWinType->fsWinProperties & HHWIN_PROP_TAB_FAVORITES)
537 NP_CreateTab(pHHInfo->hInstance, hwndTabCtrl, IDS_FAVORITES, dwIndex++);
539 SendMessageW(hwndTabCtrl, WM_SETFONT, (WPARAM)pHHInfo->hFont, TRUE);
541 pHHInfo->hwndTabCtrl = hwndTabCtrl;
542 pHHInfo->pHHWinType->hwndNavigation = hWnd;
543 return TRUE;
546 /* HTML Pane */
548 static void HP_GetHTMLRect(HHInfo *pHHInfo, RECT *rc)
550 RECT rectTB, rectWND, rectNP, rectSB;
552 GetClientRect(pHHInfo->pHHWinType->hwndHelp, &rectWND);
553 GetClientRect(pHHInfo->pHHWinType->hwndToolBar, &rectTB);
554 GetClientRect(pHHInfo->pHHWinType->hwndNavigation, &rectNP);
555 GetClientRect(pHHInfo->hwndSizeBar, &rectSB);
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;
563 static BOOL HH_AddHTMLPane(HHInfo *pHHInfo)
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;
571 HP_GetHTMLRect(pHHInfo, &rc);
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;
579 if (!WB_EmbedBrowser(pHHInfo->pWBInfo, hWnd))
580 return FALSE;
582 /* store the pointer to the HH info struct */
583 SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
585 ShowWindow(hWnd, SW_SHOW);
586 UpdateWindow(hWnd);
588 pHHInfo->pHHWinType->hwndHTML = hWnd;
589 return TRUE;
592 /* Viewer Window */
594 static void Help_OnSize(HWND hWnd)
596 HHInfo *pHHInfo = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
597 DWORD dwSize;
598 RECT rc;
600 if (!pHHInfo)
601 return;
603 NP_GetNavigationRect(pHHInfo, &rc);
604 SetWindowPos(pHHInfo->pHHWinType->hwndNavigation, HWND_TOP, 0, 0,
605 rc.right, rc.bottom, SWP_NOMOVE);
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);
612 SB_GetSizeBarRect(pHHInfo, &rc);
613 SetWindowPos(pHHInfo->hwndSizeBar, HWND_TOP, rc.left, rc.top,
614 rc.right, rc.bottom, SWP_SHOWWINDOW);
616 HP_GetHTMLRect(pHHInfo, &rc);
617 SetWindowPos(pHHInfo->pHHWinType->hwndHTML, HWND_TOP, rc.left, rc.top,
618 rc.right, rc.bottom, SWP_SHOWWINDOW);
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);
625 static LRESULT CALLBACK Help_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
627 PAINTSTRUCT ps;
628 HDC hdc;
630 switch (message)
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;
647 default:
648 return DefWindowProcW(hWnd, message, wParam, lParam);
651 return 0;
654 static BOOL HH_CreateHelpWindow(HHInfo *pHHInfo)
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;
663 static const WCHAR windowClassW[] = {
664 'H','H',' ', 'P','a','r','e','n','t',0
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);
680 RegisterClassExW(&wcex);
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;
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;
695 if (pHHInfo->pHHWinType->fsValidMembers & HHWIN_PARAM_RECT)
697 x = winPos.left;
698 y = winPos.top;
699 width = winPos.right - x;
700 height = winPos.bottom - y;
702 else
704 x = WINTYPE_DEFAULT_X;
705 y = WINTYPE_DEFAULT_Y;
706 width = WINTYPE_DEFAULT_WIDTH;
707 height = WINTYPE_DEFAULT_HEIGHT;
710 hWnd = CreateWindowExW(dwExStyles, windowClassW, pHHInfo->pHHWinType->pszCaption,
711 dwStyles, x, y, width, height, NULL, NULL, hInstance, NULL);
712 if (!hWnd)
713 return FALSE;
715 ShowWindow(hWnd, SW_SHOW);
716 UpdateWindow(hWnd);
718 /* store the pointer to the HH info struct */
719 SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
721 pHHInfo->pHHWinType->hwndHelp = hWnd;
722 return TRUE;
725 static void HH_CreateFont(HHInfo *pHHInfo)
727 LOGFONTW lf;
729 GetObjectW(GetStockObject(ANSI_VAR_FONT), sizeof(LOGFONTW), &lf);
730 lf.lfWeight = FW_NORMAL;
731 lf.lfItalic = FALSE;
732 lf.lfUnderline = FALSE;
734 pHHInfo->hFont = CreateFontIndirectW(&lf);
737 static void HH_InitRequiredControls(DWORD dwControls)
739 INITCOMMONCONTROLSEX icex;
741 icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
742 icex.dwICC = dwControls;
743 InitCommonControlsEx(&icex);
746 /* Creates the whole package */
747 static BOOL HH_CreateViewer(HHInfo *pHHInfo)
749 HH_CreateFont(pHHInfo);
751 if (!HH_CreateHelpWindow(pHHInfo))
752 return FALSE;
754 HH_InitRequiredControls(ICC_BAR_CLASSES);
756 if (!HH_AddToolbar(pHHInfo))
757 return FALSE;
759 HH_RegisterChildWndClass(pHHInfo);
761 if (!HH_AddNavigationPane(pHHInfo))
762 return FALSE;
764 HH_RegisterSizeBarClass(pHHInfo);
766 if (!HH_AddSizeBar(pHHInfo))
767 return FALSE;
769 if (!HH_AddHTMLPane(pHHInfo))
770 return FALSE;
772 return TRUE;
775 static HHInfo *HH_OpenHH(HINSTANCE hInstance, LPWSTR szCmdLine)
777 HHInfo *pHHInfo = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HHInfo));
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;
785 return pHHInfo;
788 static void HH_Close(HHInfo *pHHInfo)
790 if (!pHHInfo)
791 return;
793 /* Free allocated strings */
794 if (pHHInfo->pHHWinType)
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);
808 HeapFree(GetProcessHeap(), 0, pHHInfo->pHHWinType);
809 HeapFree(GetProcessHeap(), 0, pHHInfo->szCmdLine);
811 if (pHHInfo->pCHMInfo)
813 CHM_CloseCHM(pHHInfo->pCHMInfo);
814 HeapFree(GetProcessHeap(), 0, pHHInfo->pCHMInfo);
817 if (pHHInfo->pWBInfo)
819 WB_UnEmbedBrowser(pHHInfo->pWBInfo);
820 HeapFree(GetProcessHeap(), 0, pHHInfo->pWBInfo);
824 static void HH_OpenDefaultTopic(HHInfo *pHHInfo)
826 WCHAR url[MAX_PATH];
827 LPCWSTR defTopic = pHHInfo->pHHWinType->pszFile;
829 CHM_CreateITSUrl(pHHInfo->pCHMInfo, defTopic, url);
830 WB_Navigate(pHHInfo->pWBInfo, url);
833 static BOOL HH_OpenCHM(HHInfo *pHHInfo)
835 if (!CHM_OpenCHM(pHHInfo->pCHMInfo, pHHInfo->szCmdLine))
836 return FALSE;
838 if (!CHM_LoadWinTypeFromCHM(pHHInfo->pCHMInfo, pHHInfo->pHHWinType))
839 return FALSE;
841 return TRUE;
844 /* FIXME: Check szCmdLine for bad arguments */
845 int WINAPI doWinMain(HINSTANCE hInstance, LPSTR szCmdLine)
847 MSG msg;
848 HHInfo *pHHInfo;
850 if (FAILED(OleInitialize(NULL)))
851 return -1;
853 pHHInfo = HH_OpenHH(hInstance, HH_ANSIToUnicode(szCmdLine));
854 if (!pHHInfo || !HH_OpenCHM(pHHInfo) || !HH_CreateViewer(pHHInfo))
856 OleUninitialize();
857 return -1;
860 HH_OpenDefaultTopic(pHHInfo);
862 while (GetMessageW(&msg, 0, 0, 0))
864 TranslateMessage(&msg);
865 DispatchMessageW(&msg);
868 HH_Close(pHHInfo);
869 HeapFree(GetProcessHeap(), 0, pHHInfo);
870 OleUninitialize();
872 return 0;