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"
35 /* Window type defaults */
37 #define WINTYPE_DEFAULT_X 280
38 #define WINTYPE_DEFAULT_Y 100
39 #define WINTYPE_DEFAULT_WIDTH 740
40 #define WINTYPE_DEFAULT_HEIGHT 640
41 #define WINTYPE_DEFAULT_NAVWIDTH 250
43 typedef struct tagHHInfo
45 HH_WINTYPEW *pHHWinType;
53 extern HINSTANCE hhctrl_hinstance;
55 static LPWSTR HH_ANSIToUnicode(LPCSTR ansi)
60 count = MultiByteToWideChar(CP_ACP, 0, ansi, -1, NULL, 0);
61 unicode = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR));
62 MultiByteToWideChar(CP_ACP, 0, ansi, -1, unicode, count);
67 /* Loads a string from the resource file */
68 static LPWSTR HH_LoadString(DWORD dwID)
73 iSize = LoadStringW(hhctrl_hinstance, dwID, NULL, 0);
74 iSize += 2; /* some strings (tab text) needs double-null termination */
76 string = HeapAlloc(GetProcessHeap(), 0, iSize * sizeof(WCHAR));
77 LoadStringW(hhctrl_hinstance, dwID, string, iSize);
84 static const WCHAR szChildClass[] = {
85 'H','H',' ','C','h','i','l','d',0
88 static const WCHAR szEmpty[] = {0};
90 LRESULT CALLBACK Child_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
95 return DefWindowProcW(hWnd, message, wParam, lParam);
101 static void HH_RegisterChildWndClass(HHInfo *pHHInfo)
105 wcex.cbSize = sizeof(WNDCLASSEXW);
107 wcex.lpfnWndProc = (WNDPROC)Child_WndProc;
110 wcex.hInstance = pHHInfo->hInstance;
111 wcex.hIcon = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
112 wcex.hCursor = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
113 wcex.hbrBackground = (HBRUSH)(COLOR_3DFACE);
114 wcex.lpszMenuName = NULL;
115 wcex.lpszClassName = szChildClass;
116 wcex.hIconSm = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
118 RegisterClassExW(&wcex);
125 static void TB_AddButton(TBBUTTON *pButtons, DWORD dwIndex, DWORD dwID)
127 /* FIXME: Load the correct button bitmaps */
128 pButtons[dwIndex].iBitmap = STD_PRINT;
129 pButtons[dwIndex].idCommand = dwID;
130 pButtons[dwIndex].fsState = TBSTATE_ENABLED;
131 pButtons[dwIndex].fsStyle = BTNS_BUTTON;
132 pButtons[dwIndex].dwData = 0;
133 pButtons[dwIndex].iString = 0;
136 static void TB_AddButtonsFromFlags(TBBUTTON *pButtons, DWORD dwButtonFlags, LPDWORD pdwNumButtons)
140 if (dwButtonFlags & HHWIN_BUTTON_EXPAND)
141 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_EXPAND);
143 if (dwButtonFlags & HHWIN_BUTTON_BACK)
144 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_BACK);
146 if (dwButtonFlags & HHWIN_BUTTON_FORWARD)
147 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_FORWARD);
149 if (dwButtonFlags & HHWIN_BUTTON_STOP)
150 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_STOP);
152 if (dwButtonFlags & HHWIN_BUTTON_REFRESH)
153 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_REFRESH);
155 if (dwButtonFlags & HHWIN_BUTTON_HOME)
156 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_HOME);
158 if (dwButtonFlags & HHWIN_BUTTON_SYNC)
159 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_SYNC);
161 if (dwButtonFlags & HHWIN_BUTTON_OPTIONS)
162 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_OPTIONS);
164 if (dwButtonFlags & HHWIN_BUTTON_PRINT)
165 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_PRINT);
167 if (dwButtonFlags & HHWIN_BUTTON_JUMP1)
168 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_JUMP1);
170 if (dwButtonFlags & HHWIN_BUTTON_JUMP2)
171 TB_AddButton(pButtons,(*pdwNumButtons)++, IDTB_JUMP2);
173 if (dwButtonFlags & HHWIN_BUTTON_ZOOM)
174 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_ZOOM);
176 if (dwButtonFlags & HHWIN_BUTTON_TOC_NEXT)
177 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_TOC_NEXT);
179 if (dwButtonFlags & HHWIN_BUTTON_TOC_PREV)
180 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_TOC_PREV);
183 static BOOL HH_AddToolbar(HHInfo *pHHInfo)
186 HWND hwndParent = pHHInfo->pHHWinType->hwndHelp;
187 DWORD toolbarFlags = pHHInfo->pHHWinType->fsToolBarFlags;
188 TBBUTTON buttons[IDTB_TOC_PREV - IDTB_EXPAND];
190 DWORD dwStyles, dwExStyles;
191 DWORD dwNumButtons, dwIndex;
193 /* FIXME: Remove the following line once we read the CHM file */
194 toolbarFlags = HHWIN_BUTTON_EXPAND | HHWIN_BUTTON_BACK | HHWIN_BUTTON_STOP |
195 HHWIN_BUTTON_REFRESH | HHWIN_BUTTON_HOME | HHWIN_BUTTON_PRINT;
196 TB_AddButtonsFromFlags(buttons, toolbarFlags, &dwNumButtons);
197 pHHInfo->dwNumTBButtons = dwNumButtons;
199 dwStyles = WS_CHILDWINDOW | WS_VISIBLE | TBSTYLE_FLAT |
200 TBSTYLE_WRAPABLE | TBSTYLE_TOOLTIPS | CCS_NODIVIDER;
201 dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
203 hToolbar = CreateWindowExW(dwExStyles, TOOLBARCLASSNAMEW, NULL, dwStyles,
204 0, 0, 0, 0, hwndParent, NULL,
205 pHHInfo->hInstance, NULL);
209 SendMessageW(hToolbar, TB_SETBITMAPSIZE, 0, MAKELONG(ICON_SIZE, ICON_SIZE));
210 SendMessageW(hToolbar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
211 SendMessageW(hToolbar, WM_SETFONT, (WPARAM)pHHInfo->hFont, TRUE);
213 /* FIXME: Load correct icons for all buttons */
214 tbAB.hInst = HINST_COMMCTRL;
215 tbAB.nID = IDB_STD_LARGE_COLOR;
216 SendMessageW(hToolbar, TB_ADDBITMAP, 0, (LPARAM)&tbAB);
218 for (dwIndex = 0; dwIndex < dwNumButtons; dwIndex++)
220 LPWSTR szBuf = HH_LoadString(buttons[dwIndex].idCommand);
221 DWORD dwLen = strlenW(szBuf);
222 szBuf[dwLen + 2] = 0; /* Double-null terminate */
224 buttons[dwIndex].iString = (DWORD)SendMessageW(hToolbar, TB_ADDSTRINGW, 0, (LPARAM)szBuf);
225 HeapFree(GetProcessHeap(), 0, szBuf);
228 SendMessageW(hToolbar, TB_ADDBUTTONSW, dwNumButtons, (LPARAM)&buttons);
229 SendMessageW(hToolbar, TB_AUTOSIZE, 0, 0);
230 ShowWindow(hToolbar, SW_SHOW);
232 pHHInfo->pHHWinType->hwndToolBar = hToolbar;
236 /* Navigation Pane */
238 static void NP_GetNavigationRect(HHInfo *pHHInfo, RECT *rc)
240 HWND hwndParent = pHHInfo->pHHWinType->hwndHelp;
241 HWND hwndToolbar = pHHInfo->pHHWinType->hwndToolBar;
242 RECT rectWND, rectTB;
244 GetClientRect(hwndParent, &rectWND);
245 GetClientRect(hwndToolbar, &rectTB);
248 rc->top = rectTB.bottom;
249 rc->bottom = rectWND.bottom - rectTB.bottom;
251 if (pHHInfo->pHHWinType->fsValidMembers & HHWIN_PARAM_NAV_WIDTH)
252 rc->right = pHHInfo->pHHWinType->iNavWidth;
254 rc->right = WINTYPE_DEFAULT_NAVWIDTH;
257 static void NP_CreateTab(HINSTANCE hInstance, HWND hwndTabCtrl, DWORD dwStrID, DWORD dwIndex)
260 LPWSTR tabText = HH_LoadString(dwStrID);
262 tie.mask = TCIF_TEXT;
263 tie.pszText = tabText;
265 TabCtrl_InsertItemW(hwndTabCtrl, dwIndex, &tie);
266 HeapFree(GetProcessHeap(), 0, tabText);
269 static BOOL HH_AddNavigationPane(HHInfo *pHHInfo)
271 HWND hWnd, hwndTabCtrl;
272 HWND hwndParent = pHHInfo->pHHWinType->hwndHelp;
273 DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE;
274 DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
278 NP_GetNavigationRect(pHHInfo, &rc);
280 hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
281 rc.left, rc.top, rc.right, rc.bottom,
282 hwndParent, NULL, pHHInfo->hInstance, NULL);
286 hwndTabCtrl = CreateWindowExW(dwExStyles, WC_TABCONTROLW, szEmpty, dwStyles,
287 0, 0, rc.right, rc.bottom, hWnd,
288 NULL, pHHInfo->hInstance, NULL);
292 /* FIXME: Check which tabs to include when we read the CHM file */
293 NP_CreateTab(pHHInfo->hInstance, hwndTabCtrl, IDS_CONTENTS, dwIndex++);
294 NP_CreateTab(pHHInfo->hInstance, hwndTabCtrl, IDS_INDEX, dwIndex++);
295 NP_CreateTab(pHHInfo->hInstance, hwndTabCtrl, IDS_SEARCH, dwIndex++);
296 NP_CreateTab(pHHInfo->hInstance, hwndTabCtrl, IDS_FAVORITES, dwIndex++);
298 SendMessageW(hwndTabCtrl, WM_SETFONT, (WPARAM)pHHInfo->hFont, TRUE);
300 pHHInfo->hwndTabCtrl = hwndTabCtrl;
301 pHHInfo->pHHWinType->hwndNavigation = hWnd;
307 static void HP_GetHTMLRect(HHInfo *pHHInfo, RECT *rc)
309 HWND hwndParent = pHHInfo->pHHWinType->hwndHelp;
310 HWND hwndToolbar = pHHInfo->pHHWinType->hwndToolBar;
311 HWND hwndNavigation = pHHInfo->pHHWinType->hwndNavigation;
312 RECT rectTB, rectWND, rectNP;
314 GetClientRect(hwndParent, &rectWND);
315 GetClientRect(hwndToolbar, &rectTB);
316 GetClientRect(hwndNavigation, &rectNP);
318 rc->left = rectNP.right;
319 rc->top = rectTB.bottom;
320 rc->right = rectWND.right - rectNP.right;
321 rc->bottom = rectWND.bottom - rectTB.bottom;
324 static BOOL HH_AddHTMLPane(HHInfo *pHHInfo)
327 HWND hwndParent = pHHInfo->pHHWinType->hwndHelp;
328 DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN;
329 DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_CLIENTEDGE;
332 HP_GetHTMLRect(pHHInfo, &rc);
334 hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
335 rc.left, rc.top, rc.right, rc.bottom,
336 hwndParent, NULL, pHHInfo->hInstance, NULL);
340 /* store the pointer to the HH info struct */
341 SetWindowLongPtrA(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
343 ShowWindow(hWnd, SW_SHOW);
346 pHHInfo->pHHWinType->hwndHTML = hWnd;
352 LRESULT CALLBACK Help_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
361 hdc = BeginPaint(hWnd, &ps);
369 return DefWindowProcW(hWnd, message, wParam, lParam);
375 static BOOL HH_CreateHelpWindow(HHInfo *pHHInfo)
378 HINSTANCE hInstance = pHHInfo->hInstance;
380 DWORD dwStyles, dwExStyles;
381 DWORD x, y, width, height;
383 static const WCHAR windowClassW[] = {
384 'H','H',' ', 'P','a','r','e','n','t',0
387 static const WCHAR windowTitleW[] = {
388 'H','T','M','L',' ','H','e','l','p',0
391 wcex.cbSize = sizeof(WNDCLASSEXW);
392 wcex.style = CS_HREDRAW | CS_VREDRAW;
393 wcex.lpfnWndProc = (WNDPROC)Help_WndProc;
396 wcex.hInstance = hInstance;
397 wcex.hIcon = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
398 wcex.hCursor = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
399 wcex.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);
400 wcex.lpszMenuName = NULL;
401 wcex.lpszClassName = windowClassW;
402 wcex.hIconSm = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
404 RegisterClassExW(&wcex);
406 dwStyles = WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
407 dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR |
408 WS_EX_WINDOWEDGE | WS_EX_APPWINDOW;
410 /* these will be loaded from the CHM file in the future if they're provided */
411 x = WINTYPE_DEFAULT_X;
412 y = WINTYPE_DEFAULT_Y;
413 width = WINTYPE_DEFAULT_WIDTH;
414 height = WINTYPE_DEFAULT_HEIGHT;
416 hWnd = CreateWindowExW(dwExStyles, windowClassW, windowTitleW, dwStyles,
417 x, y, width, height, NULL, NULL, hInstance, NULL);
421 ShowWindow(hWnd, SW_SHOW);
424 /* store the pointer to the HH info struct */
425 SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
427 pHHInfo->pHHWinType->hwndHelp = hWnd;
431 static void HH_CreateFont(HHInfo *pHHInfo)
435 GetObjectW(GetStockObject(ANSI_VAR_FONT), sizeof(LOGFONTW), &lf);
436 lf.lfWeight = FW_NORMAL;
438 lf.lfUnderline = FALSE;
440 pHHInfo->hFont = CreateFontIndirectW(&lf);
443 static void HH_InitRequiredControls(DWORD dwControls)
445 INITCOMMONCONTROLSEX icex;
447 icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
448 icex.dwICC = dwControls;
449 InitCommonControlsEx(&icex);
452 /* Creates the whole package */
453 static BOOL HH_CreateViewer(HHInfo *pHHInfo)
455 HH_CreateFont(pHHInfo);
457 if (!HH_CreateHelpWindow(pHHInfo))
460 HH_InitRequiredControls(ICC_BAR_CLASSES);
462 if (!HH_AddToolbar(pHHInfo))
465 HH_RegisterChildWndClass(pHHInfo);
467 if (!HH_AddNavigationPane(pHHInfo))
470 if (!HH_AddHTMLPane(pHHInfo))
476 static HHInfo *HH_OpenHH(HINSTANCE hInstance, LPCWSTR szCmdLine)
478 HHInfo *pHHInfo = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HHInfo));
480 pHHInfo->pHHWinType = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HH_WINTYPEW));
481 pHHInfo->hInstance = hInstance;
482 pHHInfo->szCmdLine = szCmdLine;
487 static void HH_Close(HHInfo *pHHInfo)
492 HeapFree(GetProcessHeap(), 0, pHHInfo->pHHWinType);
495 /* FIXME: Check szCmdLine for bad arguments */
496 int WINAPI doWinMain(HINSTANCE hInstance, LPSTR szCmdLine)
501 if (OleInitialize(NULL) != S_OK)
504 pHHInfo = HH_OpenHH(hInstance, HH_ANSIToUnicode(szCmdLine));
505 if (!pHHInfo || !HH_CreateViewer(pHHInfo))
511 while (GetMessageW(&msg, 0, 0, 0))
513 TranslateMessage(&msg);
514 DispatchMessageW(&msg);
518 HeapFree(GetProcessHeap(), 0, pHHInfo);