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 251
43 typedef struct tagHHInfo
45 HH_WINTYPEW *pHHWinType;
52 extern HINSTANCE hhctrl_hinstance;
54 static LPWSTR HH_ANSIToUnicode(LPCSTR ansi)
59 count = MultiByteToWideChar(CP_ACP, 0, ansi, -1, NULL, 0);
60 unicode = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR));
61 MultiByteToWideChar(CP_ACP, 0, ansi, -1, unicode, count);
66 /* Loads a string from the resource file */
67 static LPWSTR HH_LoadString(DWORD dwID)
72 iSize = LoadStringW(hhctrl_hinstance, dwID, NULL, 0);
73 iSize += 2; /* some strings (tab text) needs double-null termination */
75 string = HeapAlloc(GetProcessHeap(), 0, iSize * sizeof(WCHAR));
76 LoadStringW(hhctrl_hinstance, dwID, string, iSize);
83 static const WCHAR szChildClass[] = {
84 'H','H',' ','C','h','i','l','d',0
87 static const WCHAR szEmpty[] = {0};
89 static void Child_OnPaint(HWND hWnd)
95 hdc = BeginPaint(hWnd, &ps);
97 /* Only paint the Navigation pane, identified by the fact
98 * that it has a child window
100 if (GetWindow(hWnd, GW_CHILD))
102 GetClientRect(hWnd, &rc);
104 /* set the border color */
105 SelectObject(hdc, GetStockObject(DC_PEN));
106 SetDCPenColor(hdc, GetSysColor(COLOR_BTNSHADOW));
108 /* Draw the top and right borders */
109 MoveToEx(hdc, 0, 0, NULL);
110 LineTo(hdc, rc.right - 1, 0);
111 LineTo(hdc, rc.right - 1, rc.bottom);
113 /* Fill in the background, taking the border lines into account */
116 FillRect(hdc, &rc, GetSysColorBrush(COLOR_3DFACE));
122 LRESULT CALLBACK Child_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
130 return DefWindowProcW(hWnd, message, wParam, lParam);
136 static void HH_RegisterChildWndClass(HHInfo *pHHInfo)
140 wcex.cbSize = sizeof(WNDCLASSEXW);
142 wcex.lpfnWndProc = (WNDPROC)Child_WndProc;
145 wcex.hInstance = pHHInfo->hInstance;
146 wcex.hIcon = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
147 wcex.hCursor = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
148 wcex.hbrBackground = (HBRUSH)(COLOR_3DFACE);
149 wcex.lpszMenuName = NULL;
150 wcex.lpszClassName = szChildClass;
151 wcex.hIconSm = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
153 RegisterClassExW(&wcex);
160 static void TB_AddButton(TBBUTTON *pButtons, DWORD dwIndex, DWORD dwID)
162 /* FIXME: Load the correct button bitmaps */
163 pButtons[dwIndex].iBitmap = STD_PRINT;
164 pButtons[dwIndex].idCommand = dwID;
165 pButtons[dwIndex].fsState = TBSTATE_ENABLED;
166 pButtons[dwIndex].fsStyle = BTNS_BUTTON;
167 pButtons[dwIndex].dwData = 0;
168 pButtons[dwIndex].iString = 0;
171 static void TB_AddButtonsFromFlags(TBBUTTON *pButtons, DWORD dwButtonFlags, LPDWORD pdwNumButtons)
175 if (dwButtonFlags & HHWIN_BUTTON_EXPAND)
176 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_EXPAND);
178 if (dwButtonFlags & HHWIN_BUTTON_BACK)
179 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_BACK);
181 if (dwButtonFlags & HHWIN_BUTTON_FORWARD)
182 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_FORWARD);
184 if (dwButtonFlags & HHWIN_BUTTON_STOP)
185 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_STOP);
187 if (dwButtonFlags & HHWIN_BUTTON_REFRESH)
188 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_REFRESH);
190 if (dwButtonFlags & HHWIN_BUTTON_HOME)
191 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_HOME);
193 if (dwButtonFlags & HHWIN_BUTTON_SYNC)
194 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_SYNC);
196 if (dwButtonFlags & HHWIN_BUTTON_OPTIONS)
197 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_OPTIONS);
199 if (dwButtonFlags & HHWIN_BUTTON_PRINT)
200 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_PRINT);
202 if (dwButtonFlags & HHWIN_BUTTON_JUMP1)
203 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_JUMP1);
205 if (dwButtonFlags & HHWIN_BUTTON_JUMP2)
206 TB_AddButton(pButtons,(*pdwNumButtons)++, IDTB_JUMP2);
208 if (dwButtonFlags & HHWIN_BUTTON_ZOOM)
209 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_ZOOM);
211 if (dwButtonFlags & HHWIN_BUTTON_TOC_NEXT)
212 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_TOC_NEXT);
214 if (dwButtonFlags & HHWIN_BUTTON_TOC_PREV)
215 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_TOC_PREV);
218 static BOOL HH_AddToolbar(HHInfo *pHHInfo)
221 HWND hwndParent = pHHInfo->pHHWinType->hwndHelp;
222 DWORD toolbarFlags = pHHInfo->pHHWinType->fsToolBarFlags;
223 TBBUTTON buttons[IDTB_TOC_PREV - IDTB_EXPAND];
225 DWORD dwStyles, dwExStyles;
226 DWORD dwNumButtons, dwIndex;
228 /* FIXME: Remove the following line once we read the CHM file */
229 toolbarFlags = HHWIN_BUTTON_EXPAND | HHWIN_BUTTON_BACK | HHWIN_BUTTON_STOP |
230 HHWIN_BUTTON_REFRESH | HHWIN_BUTTON_HOME | HHWIN_BUTTON_PRINT;
231 TB_AddButtonsFromFlags(buttons, toolbarFlags, &dwNumButtons);
233 dwStyles = WS_CHILDWINDOW | WS_VISIBLE | TBSTYLE_FLAT |
234 TBSTYLE_WRAPABLE | TBSTYLE_TOOLTIPS | CCS_NODIVIDER;
235 dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
237 hToolbar = CreateWindowExW(dwExStyles, TOOLBARCLASSNAMEW, NULL, dwStyles,
238 0, 0, 0, 0, hwndParent, NULL,
239 pHHInfo->hInstance, NULL);
243 SendMessageW(hToolbar, TB_SETBITMAPSIZE, 0, MAKELONG(ICON_SIZE, ICON_SIZE));
244 SendMessageW(hToolbar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
245 SendMessageW(hToolbar, WM_SETFONT, (WPARAM)pHHInfo->hFont, TRUE);
247 /* FIXME: Load correct icons for all buttons */
248 tbAB.hInst = HINST_COMMCTRL;
249 tbAB.nID = IDB_STD_LARGE_COLOR;
250 SendMessageW(hToolbar, TB_ADDBITMAP, 0, (LPARAM)&tbAB);
252 for (dwIndex = 0; dwIndex < dwNumButtons; dwIndex++)
254 LPWSTR szBuf = HH_LoadString(buttons[dwIndex].idCommand);
255 DWORD dwLen = strlenW(szBuf);
256 szBuf[dwLen + 2] = 0; /* Double-null terminate */
258 buttons[dwIndex].iString = (DWORD)SendMessageW(hToolbar, TB_ADDSTRINGW, 0, (LPARAM)szBuf);
259 HeapFree(GetProcessHeap(), 0, szBuf);
262 SendMessageW(hToolbar, TB_ADDBUTTONSW, dwNumButtons, (LPARAM)&buttons);
263 SendMessageW(hToolbar, TB_AUTOSIZE, 0, 0);
264 ShowWindow(hToolbar, SW_SHOW);
266 pHHInfo->pHHWinType->hwndToolBar = hToolbar;
270 /* Navigation Pane */
272 #define TAB_PADDING 2
273 #define TAB_TOP_PADDING 8
274 #define TAB_RIGHT_PADDING 4
276 static void NP_GetNavigationRect(HHInfo *pHHInfo, RECT *rc)
278 HWND hwndParent = pHHInfo->pHHWinType->hwndHelp;
279 HWND hwndToolbar = pHHInfo->pHHWinType->hwndToolBar;
280 RECT rectWND, rectTB;
282 GetClientRect(hwndParent, &rectWND);
283 GetClientRect(hwndToolbar, &rectTB);
286 rc->top = rectTB.bottom;
287 rc->bottom = rectWND.bottom - rectTB.bottom;
289 if (pHHInfo->pHHWinType->fsValidMembers & HHWIN_PARAM_NAV_WIDTH)
290 rc->right = pHHInfo->pHHWinType->iNavWidth;
292 rc->right = WINTYPE_DEFAULT_NAVWIDTH;
295 static void NP_CreateTab(HINSTANCE hInstance, HWND hwndTabCtrl, DWORD dwStrID, DWORD dwIndex)
298 LPWSTR tabText = HH_LoadString(dwStrID);
300 tie.mask = TCIF_TEXT;
301 tie.pszText = tabText;
303 TabCtrl_InsertItemW(hwndTabCtrl, dwIndex, &tie);
304 HeapFree(GetProcessHeap(), 0, tabText);
307 static BOOL HH_AddNavigationPane(HHInfo *pHHInfo)
309 HWND hWnd, hwndTabCtrl;
310 HWND hwndParent = pHHInfo->pHHWinType->hwndHelp;
311 DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE;
312 DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
316 NP_GetNavigationRect(pHHInfo, &rc);
318 hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
319 rc.left, rc.top, rc.right, rc.bottom,
320 hwndParent, NULL, pHHInfo->hInstance, NULL);
324 hwndTabCtrl = CreateWindowExW(dwExStyles, WC_TABCONTROLW, szEmpty, dwStyles,
325 TAB_PADDING, TAB_TOP_PADDING,
326 rc.right - TAB_PADDING - TAB_RIGHT_PADDING,
327 rc.bottom - TAB_PADDING - TAB_TOP_PADDING,
328 hWnd, NULL, pHHInfo->hInstance, NULL);
332 /* FIXME: Check which tabs to include when we read the CHM file */
333 NP_CreateTab(pHHInfo->hInstance, hwndTabCtrl, IDS_CONTENTS, dwIndex++);
334 NP_CreateTab(pHHInfo->hInstance, hwndTabCtrl, IDS_INDEX, dwIndex++);
335 NP_CreateTab(pHHInfo->hInstance, hwndTabCtrl, IDS_SEARCH, dwIndex++);
336 NP_CreateTab(pHHInfo->hInstance, hwndTabCtrl, IDS_FAVORITES, dwIndex++);
338 SendMessageW(hwndTabCtrl, WM_SETFONT, (WPARAM)pHHInfo->hFont, TRUE);
340 pHHInfo->hwndTabCtrl = hwndTabCtrl;
341 pHHInfo->pHHWinType->hwndNavigation = hWnd;
347 static void HP_GetHTMLRect(HHInfo *pHHInfo, RECT *rc)
349 HWND hwndParent = pHHInfo->pHHWinType->hwndHelp;
350 HWND hwndToolbar = pHHInfo->pHHWinType->hwndToolBar;
351 HWND hwndNavigation = pHHInfo->pHHWinType->hwndNavigation;
352 RECT rectTB, rectWND, rectNP;
354 GetClientRect(hwndParent, &rectWND);
355 GetClientRect(hwndToolbar, &rectTB);
356 GetClientRect(hwndNavigation, &rectNP);
358 rc->left = rectNP.right;
359 rc->top = rectTB.bottom;
360 rc->right = rectWND.right - rectNP.right;
361 rc->bottom = rectWND.bottom - rectTB.bottom;
364 static BOOL HH_AddHTMLPane(HHInfo *pHHInfo)
367 HWND hwndParent = pHHInfo->pHHWinType->hwndHelp;
368 DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN;
369 DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_CLIENTEDGE;
372 HP_GetHTMLRect(pHHInfo, &rc);
374 hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
375 rc.left, rc.top, rc.right, rc.bottom,
376 hwndParent, NULL, pHHInfo->hInstance, NULL);
380 /* store the pointer to the HH info struct */
381 SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
383 ShowWindow(hWnd, SW_SHOW);
386 pHHInfo->pHHWinType->hwndHTML = hWnd;
392 static void Help_OnSize(HWND hWnd, LPARAM lParam)
394 HHInfo *pHHInfo = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
400 /* Only resize the Navigation pane vertically */
403 NP_GetNavigationRect(pHHInfo, &rc);
404 SetWindowPos(pHHInfo->pHHWinType->hwndNavigation, HWND_TOP, 0, 0,
405 rc.right, rc.bottom, SWP_NOMOVE);
407 GetClientRect(pHHInfo->pHHWinType->hwndNavigation, &rc);
408 SetWindowPos(pHHInfo->hwndTabCtrl, HWND_TOP, 0, 0,
409 rc.right - TAB_PADDING - TAB_RIGHT_PADDING,
410 rc.bottom - TAB_PADDING - TAB_TOP_PADDING, SWP_NOMOVE);
413 HP_GetHTMLRect(pHHInfo, &rc);
414 SetWindowPos(pHHInfo->pHHWinType->hwndHTML, HWND_TOP, 0, 0,
415 LOWORD(lParam), HIWORD(lParam), SWP_NOMOVE);
418 LRESULT CALLBACK Help_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
426 Help_OnSize(hWnd, lParam);
429 hdc = BeginPaint(hWnd, &ps);
437 return DefWindowProcW(hWnd, message, wParam, lParam);
443 static BOOL HH_CreateHelpWindow(HHInfo *pHHInfo)
446 HINSTANCE hInstance = pHHInfo->hInstance;
448 DWORD dwStyles, dwExStyles;
449 DWORD x, y, width, height;
451 static const WCHAR windowClassW[] = {
452 'H','H',' ', 'P','a','r','e','n','t',0
455 static const WCHAR windowTitleW[] = {
456 'H','T','M','L',' ','H','e','l','p',0
459 wcex.cbSize = sizeof(WNDCLASSEXW);
460 wcex.style = CS_HREDRAW | CS_VREDRAW;
461 wcex.lpfnWndProc = (WNDPROC)Help_WndProc;
464 wcex.hInstance = hInstance;
465 wcex.hIcon = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
466 wcex.hCursor = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
467 wcex.hbrBackground = (HBRUSH)(COLOR_MENU + 1);
468 wcex.lpszMenuName = NULL;
469 wcex.lpszClassName = windowClassW;
470 wcex.hIconSm = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
472 RegisterClassExW(&wcex);
474 dwStyles = WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
475 dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR |
476 WS_EX_WINDOWEDGE | WS_EX_APPWINDOW;
478 /* these will be loaded from the CHM file in the future if they're provided */
479 x = WINTYPE_DEFAULT_X;
480 y = WINTYPE_DEFAULT_Y;
481 width = WINTYPE_DEFAULT_WIDTH;
482 height = WINTYPE_DEFAULT_HEIGHT;
484 hWnd = CreateWindowExW(dwExStyles, windowClassW, windowTitleW, dwStyles,
485 x, y, width, height, NULL, NULL, hInstance, NULL);
489 ShowWindow(hWnd, SW_SHOW);
492 /* store the pointer to the HH info struct */
493 SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
495 pHHInfo->pHHWinType->hwndHelp = hWnd;
499 static void HH_CreateFont(HHInfo *pHHInfo)
503 GetObjectW(GetStockObject(ANSI_VAR_FONT), sizeof(LOGFONTW), &lf);
504 lf.lfWeight = FW_NORMAL;
506 lf.lfUnderline = FALSE;
508 pHHInfo->hFont = CreateFontIndirectW(&lf);
511 static void HH_InitRequiredControls(DWORD dwControls)
513 INITCOMMONCONTROLSEX icex;
515 icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
516 icex.dwICC = dwControls;
517 InitCommonControlsEx(&icex);
520 /* Creates the whole package */
521 static BOOL HH_CreateViewer(HHInfo *pHHInfo)
523 HH_CreateFont(pHHInfo);
525 if (!HH_CreateHelpWindow(pHHInfo))
528 HH_InitRequiredControls(ICC_BAR_CLASSES);
530 if (!HH_AddToolbar(pHHInfo))
533 HH_RegisterChildWndClass(pHHInfo);
535 if (!HH_AddNavigationPane(pHHInfo))
538 if (!HH_AddHTMLPane(pHHInfo))
544 static HHInfo *HH_OpenHH(HINSTANCE hInstance, LPWSTR szCmdLine)
546 HHInfo *pHHInfo = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HHInfo));
548 pHHInfo->pHHWinType = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HH_WINTYPEW));
549 pHHInfo->hInstance = hInstance;
550 pHHInfo->szCmdLine = szCmdLine;
555 static void HH_Close(HHInfo *pHHInfo)
560 HeapFree(GetProcessHeap(), 0, pHHInfo->pHHWinType);
561 HeapFree(GetProcessHeap(), 0, pHHInfo->szCmdLine);
564 /* FIXME: Check szCmdLine for bad arguments */
565 int WINAPI doWinMain(HINSTANCE hInstance, LPSTR szCmdLine)
570 if (OleInitialize(NULL) != S_OK)
573 pHHInfo = HH_OpenHH(hInstance, HH_ANSIToUnicode(szCmdLine));
574 if (!pHHInfo || !HH_CreateViewer(pHHInfo))
580 while (GetMessageW(&msg, 0, 0, 0))
582 TranslateMessage(&msg);
583 DispatchMessageW(&msg);
587 HeapFree(GetProcessHeap(), 0, pHHInfo);