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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include "wine/unicode.h"
36 /* Window type defaults */
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 251
44 typedef struct tagHHInfo
46 HH_WINTYPEW *pHHWinType;
54 extern HINSTANCE hhctrl_hinstance;
56 static LPWSTR HH_ANSIToUnicode(LPCSTR ansi)
61 count = MultiByteToWideChar(CP_ACP, 0, ansi, -1, NULL, 0);
62 unicode = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR));
63 MultiByteToWideChar(CP_ACP, 0, ansi, -1, unicode, count);
68 /* Loads a string from the resource file */
69 static LPWSTR HH_LoadString(DWORD dwID)
74 iSize = LoadStringW(hhctrl_hinstance, dwID, NULL, 0);
75 iSize += 2; /* some strings (tab text) needs double-null termination */
77 string = HeapAlloc(GetProcessHeap(), 0, iSize * sizeof(WCHAR));
78 LoadStringW(hhctrl_hinstance, dwID, string, iSize);
85 static const WCHAR szChildClass[] = {
86 'H','H',' ','C','h','i','l','d',0
89 static const WCHAR szEmpty[] = {0};
91 static void Child_OnPaint(HWND hWnd)
97 hdc = BeginPaint(hWnd, &ps);
99 /* Only paint the Navigation pane, identified by the fact
100 * that it has a child window
102 if (GetWindow(hWnd, GW_CHILD))
104 GetClientRect(hWnd, &rc);
106 /* set the border color */
107 SelectObject(hdc, GetStockObject(DC_PEN));
108 SetDCPenColor(hdc, GetSysColor(COLOR_BTNSHADOW));
110 /* Draw the top and right borders */
111 MoveToEx(hdc, 0, 0, NULL);
112 LineTo(hdc, rc.right - 1, 0);
113 LineTo(hdc, rc.right - 1, rc.bottom);
115 /* Fill in the background, taking the border lines into account */
118 FillRect(hdc, &rc, GetSysColorBrush(COLOR_3DFACE));
124 LRESULT CALLBACK Child_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
132 return DefWindowProcW(hWnd, message, wParam, lParam);
138 static void HH_RegisterChildWndClass(HHInfo *pHHInfo)
142 wcex.cbSize = sizeof(WNDCLASSEXW);
144 wcex.lpfnWndProc = (WNDPROC)Child_WndProc;
147 wcex.hInstance = pHHInfo->hInstance;
148 wcex.hIcon = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
149 wcex.hCursor = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
150 wcex.hbrBackground = (HBRUSH)(COLOR_3DFACE);
151 wcex.lpszMenuName = NULL;
152 wcex.lpszClassName = szChildClass;
153 wcex.hIconSm = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
155 RegisterClassExW(&wcex);
162 static void TB_AddButton(TBBUTTON *pButtons, DWORD dwIndex, DWORD dwID)
164 /* FIXME: Load the correct button bitmaps */
165 pButtons[dwIndex].iBitmap = STD_PRINT;
166 pButtons[dwIndex].idCommand = dwID;
167 pButtons[dwIndex].fsState = TBSTATE_ENABLED;
168 pButtons[dwIndex].fsStyle = BTNS_BUTTON;
169 pButtons[dwIndex].dwData = 0;
170 pButtons[dwIndex].iString = 0;
173 static void TB_AddButtonsFromFlags(TBBUTTON *pButtons, DWORD dwButtonFlags, LPDWORD pdwNumButtons)
177 if (dwButtonFlags & HHWIN_BUTTON_EXPAND)
178 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_EXPAND);
180 if (dwButtonFlags & HHWIN_BUTTON_BACK)
181 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_BACK);
183 if (dwButtonFlags & HHWIN_BUTTON_FORWARD)
184 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_FORWARD);
186 if (dwButtonFlags & HHWIN_BUTTON_STOP)
187 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_STOP);
189 if (dwButtonFlags & HHWIN_BUTTON_REFRESH)
190 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_REFRESH);
192 if (dwButtonFlags & HHWIN_BUTTON_HOME)
193 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_HOME);
195 if (dwButtonFlags & HHWIN_BUTTON_SYNC)
196 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_SYNC);
198 if (dwButtonFlags & HHWIN_BUTTON_OPTIONS)
199 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_OPTIONS);
201 if (dwButtonFlags & HHWIN_BUTTON_PRINT)
202 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_PRINT);
204 if (dwButtonFlags & HHWIN_BUTTON_JUMP1)
205 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_JUMP1);
207 if (dwButtonFlags & HHWIN_BUTTON_JUMP2)
208 TB_AddButton(pButtons,(*pdwNumButtons)++, IDTB_JUMP2);
210 if (dwButtonFlags & HHWIN_BUTTON_ZOOM)
211 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_ZOOM);
213 if (dwButtonFlags & HHWIN_BUTTON_TOC_NEXT)
214 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_TOC_NEXT);
216 if (dwButtonFlags & HHWIN_BUTTON_TOC_PREV)
217 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_TOC_PREV);
220 static BOOL HH_AddToolbar(HHInfo *pHHInfo)
223 HWND hwndParent = pHHInfo->pHHWinType->hwndHelp;
225 TBBUTTON buttons[IDTB_TOC_PREV - IDTB_EXPAND];
227 DWORD dwStyles, dwExStyles;
228 DWORD dwNumButtons, dwIndex;
230 if (pHHInfo->pHHWinType->fsWinProperties & HHWIN_PARAM_TB_FLAGS)
231 toolbarFlags = pHHInfo->pHHWinType->fsToolBarFlags;
233 toolbarFlags = HHWIN_DEF_BUTTONS;
235 TB_AddButtonsFromFlags(buttons, toolbarFlags, &dwNumButtons);
237 dwStyles = WS_CHILDWINDOW | WS_VISIBLE | TBSTYLE_FLAT |
238 TBSTYLE_WRAPABLE | TBSTYLE_TOOLTIPS | CCS_NODIVIDER;
239 dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
241 hToolbar = CreateWindowExW(dwExStyles, TOOLBARCLASSNAMEW, NULL, dwStyles,
242 0, 0, 0, 0, hwndParent, NULL,
243 pHHInfo->hInstance, NULL);
247 SendMessageW(hToolbar, TB_SETBITMAPSIZE, 0, MAKELONG(ICON_SIZE, ICON_SIZE));
248 SendMessageW(hToolbar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
249 SendMessageW(hToolbar, WM_SETFONT, (WPARAM)pHHInfo->hFont, TRUE);
251 /* FIXME: Load correct icons for all buttons */
252 tbAB.hInst = HINST_COMMCTRL;
253 tbAB.nID = IDB_STD_LARGE_COLOR;
254 SendMessageW(hToolbar, TB_ADDBITMAP, 0, (LPARAM)&tbAB);
256 for (dwIndex = 0; dwIndex < dwNumButtons; dwIndex++)
258 LPWSTR szBuf = HH_LoadString(buttons[dwIndex].idCommand);
259 DWORD dwLen = strlenW(szBuf);
260 szBuf[dwLen + 2] = 0; /* Double-null terminate */
262 buttons[dwIndex].iString = (DWORD)SendMessageW(hToolbar, TB_ADDSTRINGW, 0, (LPARAM)szBuf);
263 HeapFree(GetProcessHeap(), 0, szBuf);
266 SendMessageW(hToolbar, TB_ADDBUTTONSW, dwNumButtons, (LPARAM)&buttons);
267 SendMessageW(hToolbar, TB_AUTOSIZE, 0, 0);
268 ShowWindow(hToolbar, SW_SHOW);
270 pHHInfo->pHHWinType->hwndToolBar = hToolbar;
274 /* Navigation Pane */
276 #define TAB_PADDING 2
277 #define TAB_TOP_PADDING 8
278 #define TAB_RIGHT_PADDING 4
280 static void NP_GetNavigationRect(HHInfo *pHHInfo, RECT *rc)
282 HWND hwndParent = pHHInfo->pHHWinType->hwndHelp;
283 HWND hwndToolbar = pHHInfo->pHHWinType->hwndToolBar;
284 RECT rectWND, rectTB;
286 GetClientRect(hwndParent, &rectWND);
287 GetClientRect(hwndToolbar, &rectTB);
290 rc->top = rectTB.bottom;
291 rc->bottom = rectWND.bottom - rectTB.bottom;
293 if (pHHInfo->pHHWinType->fsValidMembers & HHWIN_PARAM_NAV_WIDTH)
294 rc->right = pHHInfo->pHHWinType->iNavWidth;
296 rc->right = WINTYPE_DEFAULT_NAVWIDTH;
299 static void NP_CreateTab(HINSTANCE hInstance, HWND hwndTabCtrl, DWORD dwStrID, DWORD dwIndex)
302 LPWSTR tabText = HH_LoadString(dwStrID);
304 tie.mask = TCIF_TEXT;
305 tie.pszText = tabText;
307 TabCtrl_InsertItemW(hwndTabCtrl, dwIndex, &tie);
308 HeapFree(GetProcessHeap(), 0, tabText);
311 static BOOL HH_AddNavigationPane(HHInfo *pHHInfo)
313 HWND hWnd, hwndTabCtrl;
314 HWND hwndParent = pHHInfo->pHHWinType->hwndHelp;
315 DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE;
316 DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
320 NP_GetNavigationRect(pHHInfo, &rc);
322 hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
323 rc.left, rc.top, rc.right, rc.bottom,
324 hwndParent, NULL, pHHInfo->hInstance, NULL);
328 hwndTabCtrl = CreateWindowExW(dwExStyles, WC_TABCONTROLW, szEmpty, dwStyles,
329 TAB_PADDING, TAB_TOP_PADDING,
330 rc.right - TAB_PADDING - TAB_RIGHT_PADDING,
331 rc.bottom - TAB_PADDING - TAB_TOP_PADDING,
332 hWnd, NULL, pHHInfo->hInstance, NULL);
336 if (pHHInfo->pHHWinType->pszToc)
337 NP_CreateTab(pHHInfo->hInstance, hwndTabCtrl, IDS_CONTENTS, dwIndex++);
339 if (pHHInfo->pHHWinType->pszIndex)
340 NP_CreateTab(pHHInfo->hInstance, hwndTabCtrl, IDS_INDEX, dwIndex++);
342 if (pHHInfo->pHHWinType->fsWinProperties & HHWIN_PROP_TAB_SEARCH)
343 NP_CreateTab(pHHInfo->hInstance, hwndTabCtrl, IDS_SEARCH, dwIndex++);
345 if (pHHInfo->pHHWinType->fsWinProperties & HHWIN_PROP_TAB_FAVORITES)
346 NP_CreateTab(pHHInfo->hInstance, hwndTabCtrl, IDS_FAVORITES, dwIndex++);
348 SendMessageW(hwndTabCtrl, WM_SETFONT, (WPARAM)pHHInfo->hFont, TRUE);
350 pHHInfo->hwndTabCtrl = hwndTabCtrl;
351 pHHInfo->pHHWinType->hwndNavigation = hWnd;
357 static void HP_GetHTMLRect(HHInfo *pHHInfo, RECT *rc)
359 HWND hwndParent = pHHInfo->pHHWinType->hwndHelp;
360 HWND hwndToolbar = pHHInfo->pHHWinType->hwndToolBar;
361 HWND hwndNavigation = pHHInfo->pHHWinType->hwndNavigation;
362 RECT rectTB, rectWND, rectNP;
364 GetClientRect(hwndParent, &rectWND);
365 GetClientRect(hwndToolbar, &rectTB);
366 GetClientRect(hwndNavigation, &rectNP);
368 rc->left = rectNP.right;
369 rc->top = rectTB.bottom;
370 rc->right = rectWND.right - rectNP.right;
371 rc->bottom = rectWND.bottom - rectTB.bottom;
374 static BOOL HH_AddHTMLPane(HHInfo *pHHInfo)
377 HWND hwndParent = pHHInfo->pHHWinType->hwndHelp;
378 DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN;
379 DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_CLIENTEDGE;
382 HP_GetHTMLRect(pHHInfo, &rc);
384 hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
385 rc.left, rc.top, rc.right, rc.bottom,
386 hwndParent, NULL, pHHInfo->hInstance, NULL);
390 /* store the pointer to the HH info struct */
391 SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
393 ShowWindow(hWnd, SW_SHOW);
396 pHHInfo->pHHWinType->hwndHTML = hWnd;
402 static void Help_OnSize(HWND hWnd, LPARAM lParam)
404 HHInfo *pHHInfo = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
410 /* Only resize the Navigation pane vertically */
413 NP_GetNavigationRect(pHHInfo, &rc);
414 SetWindowPos(pHHInfo->pHHWinType->hwndNavigation, HWND_TOP, 0, 0,
415 rc.right, rc.bottom, SWP_NOMOVE);
417 GetClientRect(pHHInfo->pHHWinType->hwndNavigation, &rc);
418 SetWindowPos(pHHInfo->hwndTabCtrl, HWND_TOP, 0, 0,
419 rc.right - TAB_PADDING - TAB_RIGHT_PADDING,
420 rc.bottom - TAB_PADDING - TAB_TOP_PADDING, SWP_NOMOVE);
423 HP_GetHTMLRect(pHHInfo, &rc);
424 SetWindowPos(pHHInfo->pHHWinType->hwndHTML, HWND_TOP, 0, 0,
425 LOWORD(lParam), HIWORD(lParam), SWP_NOMOVE);
428 LRESULT CALLBACK Help_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
436 Help_OnSize(hWnd, lParam);
439 hdc = BeginPaint(hWnd, &ps);
447 return DefWindowProcW(hWnd, message, wParam, lParam);
453 static BOOL HH_CreateHelpWindow(HHInfo *pHHInfo)
456 HINSTANCE hInstance = pHHInfo->hInstance;
457 RECT winPos = pHHInfo->pHHWinType->rcWindowPos;
459 DWORD dwStyles, dwExStyles;
460 DWORD x, y, width, height;
462 static const WCHAR windowClassW[] = {
463 'H','H',' ', 'P','a','r','e','n','t',0
466 wcex.cbSize = sizeof(WNDCLASSEXW);
467 wcex.style = CS_HREDRAW | CS_VREDRAW;
468 wcex.lpfnWndProc = (WNDPROC)Help_WndProc;
471 wcex.hInstance = hInstance;
472 wcex.hIcon = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
473 wcex.hCursor = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
474 wcex.hbrBackground = (HBRUSH)(COLOR_MENU + 1);
475 wcex.lpszMenuName = NULL;
476 wcex.lpszClassName = windowClassW;
477 wcex.hIconSm = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
479 RegisterClassExW(&wcex);
481 /* Read in window parameters if available */
482 if (pHHInfo->pHHWinType->fsValidMembers & HHWIN_PARAM_STYLES)
483 dwStyles = pHHInfo->pHHWinType->dwStyles;
485 dwStyles = WS_OVERLAPPEDWINDOW | WS_VISIBLE |
486 WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
488 if (pHHInfo->pHHWinType->fsValidMembers & HHWIN_PARAM_EXSTYLES)
489 dwExStyles = pHHInfo->pHHWinType->dwExStyles;
491 dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_APPWINDOW |
492 WS_EX_WINDOWEDGE | WS_EX_RIGHTSCROLLBAR;
494 if (pHHInfo->pHHWinType->fsValidMembers & HHWIN_PARAM_RECT)
498 width = winPos.right - x;
499 height = winPos.bottom - y;
503 x = WINTYPE_DEFAULT_X;
504 y = WINTYPE_DEFAULT_Y;
505 width = WINTYPE_DEFAULT_WIDTH;
506 height = WINTYPE_DEFAULT_HEIGHT;
509 hWnd = CreateWindowExW(dwExStyles, windowClassW, pHHInfo->pHHWinType->pszCaption,
510 dwStyles, x, y, width, height, NULL, NULL, hInstance, NULL);
514 ShowWindow(hWnd, SW_SHOW);
517 /* store the pointer to the HH info struct */
518 SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
520 pHHInfo->pHHWinType->hwndHelp = hWnd;
524 static void HH_CreateFont(HHInfo *pHHInfo)
528 GetObjectW(GetStockObject(ANSI_VAR_FONT), sizeof(LOGFONTW), &lf);
529 lf.lfWeight = FW_NORMAL;
531 lf.lfUnderline = FALSE;
533 pHHInfo->hFont = CreateFontIndirectW(&lf);
536 static void HH_InitRequiredControls(DWORD dwControls)
538 INITCOMMONCONTROLSEX icex;
540 icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
541 icex.dwICC = dwControls;
542 InitCommonControlsEx(&icex);
545 /* Creates the whole package */
546 static BOOL HH_CreateViewer(HHInfo *pHHInfo)
548 HH_CreateFont(pHHInfo);
550 if (!HH_CreateHelpWindow(pHHInfo))
553 HH_InitRequiredControls(ICC_BAR_CLASSES);
555 if (!HH_AddToolbar(pHHInfo))
558 HH_RegisterChildWndClass(pHHInfo);
560 if (!HH_AddNavigationPane(pHHInfo))
563 if (!HH_AddHTMLPane(pHHInfo))
569 static HHInfo *HH_OpenHH(HINSTANCE hInstance, LPWSTR szCmdLine)
571 HHInfo *pHHInfo = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HHInfo));
573 pHHInfo->pHHWinType = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HH_WINTYPEW));
574 pHHInfo->pCHMInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(CHMInfo));
575 pHHInfo->hInstance = hInstance;
576 pHHInfo->szCmdLine = szCmdLine;
581 static void HH_Close(HHInfo *pHHInfo)
586 /* Free allocated strings */
587 if (pHHInfo->pHHWinType)
589 HeapFree(GetProcessHeap(), 0, (LPWSTR)pHHInfo->pHHWinType->pszType);
590 HeapFree(GetProcessHeap(), 0, (LPWSTR)pHHInfo->pHHWinType->pszCaption);
591 HeapFree(GetProcessHeap(), 0, (LPWSTR)pHHInfo->pHHWinType->pszToc);
592 HeapFree(GetProcessHeap(), 0, (LPWSTR)pHHInfo->pHHWinType->pszType);
593 HeapFree(GetProcessHeap(), 0, (LPWSTR)pHHInfo->pHHWinType->pszIndex);
594 HeapFree(GetProcessHeap(), 0, (LPWSTR)pHHInfo->pHHWinType->pszFile);
595 HeapFree(GetProcessHeap(), 0, (LPWSTR)pHHInfo->pHHWinType->pszHome);
596 HeapFree(GetProcessHeap(), 0, (LPWSTR)pHHInfo->pHHWinType->pszJump1);
597 HeapFree(GetProcessHeap(), 0, (LPWSTR)pHHInfo->pHHWinType->pszJump2);
598 HeapFree(GetProcessHeap(), 0, (LPWSTR)pHHInfo->pHHWinType->pszUrlJump1);
599 HeapFree(GetProcessHeap(), 0, (LPWSTR)pHHInfo->pHHWinType->pszUrlJump2);
602 HeapFree(GetProcessHeap(), 0, pHHInfo->pHHWinType);
603 HeapFree(GetProcessHeap(), 0, pHHInfo->szCmdLine);
605 if (pHHInfo->pCHMInfo)
607 CHM_CloseCHM(pHHInfo->pCHMInfo);
608 HeapFree(GetProcessHeap(), 0, pHHInfo->pCHMInfo);
612 static BOOL HH_OpenCHM(HHInfo *pHHInfo)
614 if (!CHM_OpenCHM(pHHInfo->pCHMInfo, pHHInfo->szCmdLine))
617 if (!CHM_LoadWinTypeFromCHM(pHHInfo->pCHMInfo, pHHInfo->pHHWinType))
623 /* FIXME: Check szCmdLine for bad arguments */
624 int WINAPI doWinMain(HINSTANCE hInstance, LPSTR szCmdLine)
629 if (OleInitialize(NULL) != S_OK)
632 pHHInfo = HH_OpenHH(hInstance, HH_ANSIToUnicode(szCmdLine));
633 if (!pHHInfo || !HH_OpenCHM(pHHInfo) || !HH_CreateViewer(pHHInfo))
639 while (GetMessageW(&msg, 0, 0, 0))
641 TranslateMessage(&msg);
642 DispatchMessageW(&msg);
646 HeapFree(GetProcessHeap(), 0, pHHInfo);