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"
33 /* Window type defaults */
35 #define WINTYPE_DEFAULT_X 280
36 #define WINTYPE_DEFAULT_Y 100
37 #define WINTYPE_DEFAULT_WIDTH 740
38 #define WINTYPE_DEFAULT_HEIGHT 640
39 #define WINTYPE_DEFAULT_NAVWIDTH 250
41 typedef struct tagHHInfo
43 HH_WINTYPEW *pHHWinType;
50 extern HINSTANCE hhctrl_hinstance;
52 static LPWSTR HH_ANSIToUnicode(LPCSTR ansi)
57 count = MultiByteToWideChar(CP_ACP, 0, ansi, -1, NULL, 0);
58 unicode = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR));
59 MultiByteToWideChar(CP_ACP, 0, ansi, -1, unicode, count);
64 /* Loads a string from the resource file */
65 static LPWSTR HH_LoadString(DWORD dwID)
70 iSize = LoadStringW(hhctrl_hinstance, dwID, NULL, 0);
71 iSize += 2; /* some strings (tab text) needs double-null termination */
73 string = HeapAlloc(GetProcessHeap(), 0, iSize * sizeof(WCHAR));
74 LoadStringW(hhctrl_hinstance, dwID, string, iSize);
81 static const WCHAR szChildClass[] = {
82 'H','H',' ','C','h','i','l','d',0
85 static const WCHAR szEmpty[] = {0};
87 LRESULT CALLBACK Child_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
92 return DefWindowProcW(hWnd, message, wParam, lParam);
98 static void HH_RegisterChildWndClass(HHInfo *pHHInfo)
102 wcex.cbSize = sizeof(WNDCLASSEXW);
104 wcex.lpfnWndProc = (WNDPROC)Child_WndProc;
107 wcex.hInstance = pHHInfo->hInstance;
108 wcex.hIcon = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
109 wcex.hCursor = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
110 wcex.hbrBackground = (HBRUSH)(COLOR_3DFACE);
111 wcex.lpszMenuName = NULL;
112 wcex.lpszClassName = szChildClass;
113 wcex.hIconSm = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
115 RegisterClassExW(&wcex);
122 static void TB_AddButton(TBBUTTON *pButtons, DWORD dwIndex, DWORD dwID)
124 /* FIXME: Load the correct button bitmaps */
125 pButtons[dwIndex].iBitmap = STD_PRINT;
126 pButtons[dwIndex].idCommand = dwID;
127 pButtons[dwIndex].fsState = TBSTATE_ENABLED;
128 pButtons[dwIndex].fsStyle = BTNS_BUTTON;
129 pButtons[dwIndex].dwData = 0;
130 pButtons[dwIndex].iString = 0;
133 static void TB_AddButtonsFromFlags(TBBUTTON *pButtons, DWORD dwButtonFlags, LPDWORD pdwNumButtons)
137 if (dwButtonFlags & HHWIN_BUTTON_EXPAND)
138 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_EXPAND);
140 if (dwButtonFlags & HHWIN_BUTTON_BACK)
141 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_BACK);
143 if (dwButtonFlags & HHWIN_BUTTON_FORWARD)
144 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_FORWARD);
146 if (dwButtonFlags & HHWIN_BUTTON_STOP)
147 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_STOP);
149 if (dwButtonFlags & HHWIN_BUTTON_REFRESH)
150 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_REFRESH);
152 if (dwButtonFlags & HHWIN_BUTTON_HOME)
153 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_HOME);
155 if (dwButtonFlags & HHWIN_BUTTON_SYNC)
156 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_SYNC);
158 if (dwButtonFlags & HHWIN_BUTTON_OPTIONS)
159 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_OPTIONS);
161 if (dwButtonFlags & HHWIN_BUTTON_PRINT)
162 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_PRINT);
164 if (dwButtonFlags & HHWIN_BUTTON_JUMP1)
165 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_JUMP1);
167 if (dwButtonFlags & HHWIN_BUTTON_JUMP2)
168 TB_AddButton(pButtons,(*pdwNumButtons)++, IDTB_JUMP2);
170 if (dwButtonFlags & HHWIN_BUTTON_ZOOM)
171 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_ZOOM);
173 if (dwButtonFlags & HHWIN_BUTTON_TOC_NEXT)
174 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_TOC_NEXT);
176 if (dwButtonFlags & HHWIN_BUTTON_TOC_PREV)
177 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_TOC_PREV);
180 static BOOL HH_AddToolbar(HHInfo *pHHInfo)
183 HWND hwndParent = pHHInfo->pHHWinType->hwndHelp;
184 DWORD toolbarFlags = pHHInfo->pHHWinType->fsToolBarFlags;
185 TBBUTTON buttons[IDTB_TOC_PREV - IDTB_EXPAND];
187 DWORD dwStyles, dwExStyles;
188 DWORD dwNumButtons, dwIndex;
190 /* FIXME: Remove the following line once we read the CHM file */
191 toolbarFlags = HHWIN_BUTTON_EXPAND | HHWIN_BUTTON_BACK | HHWIN_BUTTON_STOP |
192 HHWIN_BUTTON_REFRESH | HHWIN_BUTTON_HOME | HHWIN_BUTTON_PRINT;
193 TB_AddButtonsFromFlags(buttons, toolbarFlags, &dwNumButtons);
194 pHHInfo->dwNumTBButtons = dwNumButtons;
196 dwStyles = WS_CHILDWINDOW | WS_VISIBLE | TBSTYLE_FLAT |
197 TBSTYLE_WRAPABLE | TBSTYLE_TOOLTIPS | CCS_NODIVIDER;
198 dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
200 hToolbar = CreateWindowExW(dwExStyles, TOOLBARCLASSNAMEW, NULL, dwStyles,
201 0, 0, 0, 0, hwndParent, NULL,
202 pHHInfo->hInstance, NULL);
206 SendMessageW(hToolbar, TB_SETBITMAPSIZE, 0, MAKELONG(ICON_SIZE, ICON_SIZE));
207 SendMessageW(hToolbar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
208 SendMessageW(hToolbar, WM_SETFONT, (WPARAM)pHHInfo->hFont, TRUE);
210 /* FIXME: Load correct icons for all buttons */
211 tbAB.hInst = HINST_COMMCTRL;
212 tbAB.nID = IDB_STD_LARGE_COLOR;
213 SendMessageW(hToolbar, TB_ADDBITMAP, 0, (LPARAM)&tbAB);
215 for (dwIndex = 0; dwIndex < dwNumButtons; dwIndex++)
217 LPWSTR szBuf = HH_LoadString(buttons[dwIndex].idCommand);
218 DWORD dwLen = strlenW(szBuf);
219 szBuf[dwLen + 2] = 0; /* Double-null terminate */
221 buttons[dwIndex].iString = (DWORD)SendMessageW(hToolbar, TB_ADDSTRINGW, 0, (LPARAM)szBuf);
222 HeapFree(GetProcessHeap(), 0, szBuf);
225 SendMessageW(hToolbar, TB_ADDBUTTONSW, dwNumButtons, (LPARAM)&buttons);
226 SendMessageW(hToolbar, TB_AUTOSIZE, 0, 0);
227 ShowWindow(hToolbar, SW_SHOW);
229 pHHInfo->pHHWinType->hwndToolBar = hToolbar;
233 /* Navigation Pane */
235 static void NP_GetNavigationRect(HHInfo *pHHInfo, RECT *rc)
237 HWND hwndParent = pHHInfo->pHHWinType->hwndHelp;
238 HWND hwndToolbar = pHHInfo->pHHWinType->hwndToolBar;
239 RECT rectWND, rectTB;
241 GetClientRect(hwndParent, &rectWND);
242 GetClientRect(hwndToolbar, &rectTB);
245 rc->top = rectTB.bottom;
246 rc->bottom = rectWND.bottom - rectTB.bottom;
248 if (pHHInfo->pHHWinType->fsValidMembers & HHWIN_PARAM_NAV_WIDTH)
249 rc->right = pHHInfo->pHHWinType->iNavWidth;
251 rc->right = WINTYPE_DEFAULT_NAVWIDTH;
254 static BOOL HH_AddNavigationPane(HHInfo *pHHInfo)
257 HWND hwndParent = pHHInfo->pHHWinType->hwndHelp;
258 DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE;
259 DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
262 NP_GetNavigationRect(pHHInfo, &rc);
264 hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
265 rc.left, rc.top, rc.right, rc.bottom,
266 hwndParent, NULL, pHHInfo->hInstance, NULL);
270 pHHInfo->pHHWinType->hwndNavigation = hWnd;
276 static void HP_GetHTMLRect(HHInfo *pHHInfo, RECT *rc)
278 HWND hwndParent = pHHInfo->pHHWinType->hwndHelp;
279 HWND hwndToolbar = pHHInfo->pHHWinType->hwndToolBar;
280 HWND hwndNavigation = pHHInfo->pHHWinType->hwndNavigation;
281 RECT rectTB, rectWND, rectNP;
283 GetClientRect(hwndParent, &rectWND);
284 GetClientRect(hwndToolbar, &rectTB);
285 GetClientRect(hwndNavigation, &rectNP);
287 rc->left = rectNP.right;
288 rc->top = rectTB.bottom;
289 rc->right = rectWND.right - rectNP.right;
290 rc->bottom = rectWND.bottom - rectTB.bottom;
293 static BOOL HH_AddHTMLPane(HHInfo *pHHInfo)
296 HWND hwndParent = pHHInfo->pHHWinType->hwndHelp;
297 DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN;
298 DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_CLIENTEDGE;
301 HP_GetHTMLRect(pHHInfo, &rc);
303 hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
304 rc.left, rc.top, rc.right, rc.bottom,
305 hwndParent, NULL, pHHInfo->hInstance, NULL);
309 /* store the pointer to the HH info struct */
310 SetWindowLongPtrA(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
312 ShowWindow(hWnd, SW_SHOW);
315 pHHInfo->pHHWinType->hwndHTML = hWnd;
321 LRESULT CALLBACK Help_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
330 hdc = BeginPaint(hWnd, &ps);
338 return DefWindowProcW(hWnd, message, wParam, lParam);
344 static BOOL HH_CreateHelpWindow(HHInfo *pHHInfo)
347 HINSTANCE hInstance = pHHInfo->hInstance;
349 DWORD dwStyles, dwExStyles;
350 DWORD x, y, width, height;
352 static const WCHAR windowClassW[] = {
353 'H','H',' ', 'P','a','r','e','n','t',0
356 static const WCHAR windowTitleW[] = {
357 'H','T','M','L',' ','H','e','l','p',0
360 wcex.cbSize = sizeof(WNDCLASSEXW);
361 wcex.style = CS_HREDRAW | CS_VREDRAW;
362 wcex.lpfnWndProc = (WNDPROC)Help_WndProc;
365 wcex.hInstance = hInstance;
366 wcex.hIcon = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
367 wcex.hCursor = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
368 wcex.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);
369 wcex.lpszMenuName = NULL;
370 wcex.lpszClassName = windowClassW;
371 wcex.hIconSm = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
373 RegisterClassExW(&wcex);
375 dwStyles = WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
376 dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR |
377 WS_EX_WINDOWEDGE | WS_EX_APPWINDOW;
379 /* these will be loaded from the CHM file in the future if they're provided */
380 x = WINTYPE_DEFAULT_X;
381 y = WINTYPE_DEFAULT_Y;
382 width = WINTYPE_DEFAULT_WIDTH;
383 height = WINTYPE_DEFAULT_HEIGHT;
385 hWnd = CreateWindowExW(dwExStyles, windowClassW, windowTitleW, dwStyles,
386 x, y, width, height, NULL, NULL, hInstance, NULL);
390 ShowWindow(hWnd, SW_SHOW);
393 /* store the pointer to the HH info struct */
394 SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
396 pHHInfo->pHHWinType->hwndHelp = hWnd;
400 static void HH_CreateFont(HHInfo *pHHInfo)
404 GetObjectW(GetStockObject(ANSI_VAR_FONT), sizeof(LOGFONTW), &lf);
405 lf.lfWeight = FW_NORMAL;
407 lf.lfUnderline = FALSE;
409 pHHInfo->hFont = CreateFontIndirectW(&lf);
412 static void HH_InitRequiredControls(DWORD dwControls)
414 INITCOMMONCONTROLSEX icex;
416 icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
417 icex.dwICC = dwControls;
418 InitCommonControlsEx(&icex);
421 /* Creates the whole package */
422 static BOOL HH_CreateViewer(HHInfo *pHHInfo)
424 HH_CreateFont(pHHInfo);
426 if (!HH_CreateHelpWindow(pHHInfo))
429 HH_InitRequiredControls(ICC_BAR_CLASSES);
431 if (!HH_AddToolbar(pHHInfo))
434 HH_RegisterChildWndClass(pHHInfo);
436 if (!HH_AddNavigationPane(pHHInfo))
439 if (!HH_AddHTMLPane(pHHInfo))
445 static HHInfo *HH_OpenHH(HINSTANCE hInstance, LPCWSTR szCmdLine)
447 HHInfo *pHHInfo = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HHInfo));
449 pHHInfo->pHHWinType = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HH_WINTYPEW));
450 pHHInfo->hInstance = hInstance;
451 pHHInfo->szCmdLine = szCmdLine;
456 static void HH_Close(HHInfo *pHHInfo)
461 HeapFree(GetProcessHeap(), 0, pHHInfo->pHHWinType);
464 /* FIXME: Check szCmdLine for bad arguments */
465 int WINAPI doWinMain(HINSTANCE hInstance, LPSTR szCmdLine)
470 if (OleInitialize(NULL) != S_OK)
473 pHHInfo = HH_OpenHH(hInstance, HH_ANSIToUnicode(szCmdLine));
474 if (!pHHInfo || !HH_CreateViewer(pHHInfo))
480 while (GetMessageW(&msg, 0, 0, 0))
482 TranslateMessage(&msg);
483 DispatchMessageW(&msg);
487 HeapFree(GetProcessHeap(), 0, pHHInfo);