hhctrl.ocx: Add a ListView for the Index tab.
[wine/multimedia.git] / dlls / hhctrl.ocx / help.c
blob20305f141f9684c3fea14f8b7cdbe09aca5a35e9
1 /*
2 * Help Viewer Implementation
4 * Copyright 2005 James Hawkins
5 * Copyright 2007 Jacek Caban for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "hhctrl.h"
24 #include "wingdi.h"
25 #include "commctrl.h"
26 #include "wininet.h"
28 #include "wine/debug.h"
30 #include "resource.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(htmlhelp);
34 static LRESULT Help_OnSize(HWND hWnd);
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 250
44 #define TAB_TOP_PADDING 8
45 #define TAB_RIGHT_PADDING 4
46 #define TAB_MARGIN 8
48 static const WCHAR szEmpty[] = {0};
50 /* Loads a string from the resource file */
51 static LPWSTR HH_LoadString(DWORD dwID)
53 LPWSTR string = NULL;
54 LPCWSTR stringresource;
55 int iSize;
57 iSize = LoadStringW(hhctrl_hinstance, dwID, (LPWSTR)&stringresource, 0);
59 string = heap_alloc((iSize + 2) * sizeof(WCHAR)); /* some strings (tab text) needs double-null termination */
60 memcpy(string, stringresource, iSize*sizeof(WCHAR));
61 string[iSize] = 0;
63 return string;
66 static HRESULT navigate_url(HHInfo *info, LPCWSTR surl)
68 VARIANT url;
69 HRESULT hres;
71 TRACE("%s\n", debugstr_w(surl));
73 V_VT(&url) = VT_BSTR;
74 V_BSTR(&url) = SysAllocString(surl);
76 hres = IWebBrowser2_Navigate2(info->web_browser, &url, 0, 0, 0, 0);
78 VariantClear(&url);
80 if(FAILED(hres))
81 TRACE("Navigation failed: %08x\n", hres);
83 return hres;
86 BOOL NavigateToUrl(HHInfo *info, LPCWSTR surl)
88 ChmPath chm_path;
89 BOOL ret;
90 HRESULT hres;
92 static const WCHAR url_indicator[] = {':', '/', '/', 0};
94 TRACE("%s\n", debugstr_w(surl));
96 if (strstrW(surl, url_indicator)) {
97 hres = navigate_url(info, surl);
98 if(SUCCEEDED(hres))
99 return TRUE;
100 } /* look up in chm if it doesn't look like a full url */
102 SetChmPath(&chm_path, info->pCHMInfo->szFile, surl);
103 ret = NavigateToChm(info, chm_path.chm_file, chm_path.chm_index);
105 heap_free(chm_path.chm_file);
106 heap_free(chm_path.chm_index);
108 return ret;
111 BOOL NavigateToChm(HHInfo *info, LPCWSTR file, LPCWSTR index)
113 WCHAR buf[INTERNET_MAX_URL_LENGTH];
114 WCHAR full_path[MAX_PATH];
115 LPWSTR ptr;
117 static const WCHAR url_format[] =
118 {'m','k',':','@','M','S','I','T','S','t','o','r','e',':','%','s',':',':','%','s','%','s',0};
119 static const WCHAR slash[] = {'/',0};
120 static const WCHAR empty[] = {0};
122 TRACE("%p %s %s\n", info, debugstr_w(file), debugstr_w(index));
124 if (!info->web_browser)
125 return FALSE;
127 if(!GetFullPathNameW(file, sizeof(full_path)/sizeof(full_path[0]), full_path, NULL)) {
128 WARN("GetFullPathName failed: %u\n", GetLastError());
129 return FALSE;
132 wsprintfW(buf, url_format, full_path, (!index || index[0] == '/') ? empty : slash, index);
134 /* FIXME: HACK */
135 if((ptr = strchrW(buf, '#')))
136 *ptr = 0;
138 return SUCCEEDED(navigate_url(info, buf));
141 /* Size Bar */
143 #define SIZEBAR_WIDTH 4
145 static const WCHAR szSizeBarClass[] = {
146 'H','H',' ','S','i','z','e','B','a','r',0
149 /* Draw the SizeBar */
150 static void SB_OnPaint(HWND hWnd)
152 PAINTSTRUCT ps;
153 HDC hdc;
154 RECT rc;
156 hdc = BeginPaint(hWnd, &ps);
158 GetClientRect(hWnd, &rc);
160 /* dark frame */
161 rc.right += 1;
162 rc.bottom -= 1;
163 FrameRect(hdc, &rc, GetStockObject(GRAY_BRUSH));
165 /* white highlight */
166 SelectObject(hdc, GetStockObject(WHITE_PEN));
167 MoveToEx(hdc, rc.right, 1, NULL);
168 LineTo(hdc, 1, 1);
169 LineTo(hdc, 1, rc.bottom - 1);
172 MoveToEx(hdc, 0, rc.bottom, NULL);
173 LineTo(hdc, rc.right, rc.bottom);
175 EndPaint(hWnd, &ps);
178 static void SB_OnLButtonDown(HWND hWnd, WPARAM wParam, LPARAM lParam)
180 SetCapture(hWnd);
183 static void SB_OnLButtonUp(HWND hWnd, WPARAM wParam, LPARAM lParam)
185 HHInfo *pHHInfo = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
186 POINT pt;
188 pt.x = (short)LOWORD(lParam);
189 pt.y = (short)HIWORD(lParam);
191 /* update the window sizes */
192 pHHInfo->WinType.iNavWidth += pt.x;
193 Help_OnSize(hWnd);
195 ReleaseCapture();
198 static void SB_OnMouseMove(HWND hWnd, WPARAM wParam, LPARAM lParam)
200 /* ignore WM_MOUSEMOVE if not dragging the SizeBar */
201 if (!(wParam & MK_LBUTTON))
202 return;
205 static LRESULT CALLBACK SizeBar_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
207 switch (message)
209 case WM_LBUTTONDOWN:
210 SB_OnLButtonDown(hWnd, wParam, lParam);
211 break;
212 case WM_LBUTTONUP:
213 SB_OnLButtonUp(hWnd, wParam, lParam);
214 break;
215 case WM_MOUSEMOVE:
216 SB_OnMouseMove(hWnd, wParam, lParam);
217 break;
218 case WM_PAINT:
219 SB_OnPaint(hWnd);
220 break;
221 default:
222 return DefWindowProcW(hWnd, message, wParam, lParam);
225 return 0;
228 static void HH_RegisterSizeBarClass(HHInfo *pHHInfo)
230 WNDCLASSEXW wcex;
232 wcex.cbSize = sizeof(WNDCLASSEXW);
233 wcex.style = 0;
234 wcex.lpfnWndProc = SizeBar_WndProc;
235 wcex.cbClsExtra = 0;
236 wcex.cbWndExtra = 0;
237 wcex.hInstance = hhctrl_hinstance;
238 wcex.hIcon = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
239 wcex.hCursor = LoadCursorW(NULL, (LPCWSTR)IDC_SIZEWE);
240 wcex.hbrBackground = (HBRUSH)(COLOR_MENU + 1);
241 wcex.lpszMenuName = NULL;
242 wcex.lpszClassName = szSizeBarClass;
243 wcex.hIconSm = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
245 RegisterClassExW(&wcex);
248 static void SB_GetSizeBarRect(HHInfo *info, RECT *rc)
250 RECT rectWND, rectTB, rectNP;
252 GetClientRect(info->WinType.hwndHelp, &rectWND);
253 GetClientRect(info->WinType.hwndToolBar, &rectTB);
254 GetClientRect(info->WinType.hwndNavigation, &rectNP);
256 rc->left = rectNP.right;
257 rc->top = rectTB.bottom;
258 rc->bottom = rectWND.bottom - rectTB.bottom;
259 rc->right = SIZEBAR_WIDTH;
262 static BOOL HH_AddSizeBar(HHInfo *pHHInfo)
264 HWND hWnd;
265 HWND hwndParent = pHHInfo->WinType.hwndHelp;
266 DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE | WS_OVERLAPPED;
267 DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
268 RECT rc;
270 SB_GetSizeBarRect(pHHInfo, &rc);
272 hWnd = CreateWindowExW(dwExStyles, szSizeBarClass, szEmpty, dwStyles,
273 rc.left, rc.top, rc.right, rc.bottom,
274 hwndParent, NULL, hhctrl_hinstance, NULL);
275 if (!hWnd)
276 return FALSE;
278 /* store the pointer to the HH info struct */
279 SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
281 pHHInfo->hwndSizeBar = hWnd;
282 return TRUE;
285 /* Child Window */
287 static const WCHAR szChildClass[] = {
288 'H','H',' ','C','h','i','l','d',0
291 static LRESULT Child_OnPaint(HWND hWnd)
293 PAINTSTRUCT ps;
294 HDC hdc;
295 RECT rc;
297 hdc = BeginPaint(hWnd, &ps);
299 /* Only paint the Navigation pane, identified by the fact
300 * that it has a child window
302 if (GetWindow(hWnd, GW_CHILD))
304 GetClientRect(hWnd, &rc);
306 /* set the border color */
307 SelectObject(hdc, GetStockObject(DC_PEN));
308 SetDCPenColor(hdc, GetSysColor(COLOR_BTNSHADOW));
310 /* Draw the top border */
311 LineTo(hdc, rc.right, 0);
313 SelectObject(hdc, GetStockObject(WHITE_PEN));
314 MoveToEx(hdc, 0, 1, NULL);
315 LineTo(hdc, rc.right, 1);
318 EndPaint(hWnd, &ps);
320 return 0;
323 static void ResizeTabChild(HHInfo *info, int tab)
325 HWND hwnd = info->tabs[tab].hwnd;
326 INT width, height;
327 RECT rect, tabrc;
328 DWORD cnt;
330 GetClientRect(info->WinType.hwndNavigation, &rect);
331 SendMessageW(info->hwndTabCtrl, TCM_GETITEMRECT, 0, (LPARAM)&tabrc);
332 cnt = SendMessageW(info->hwndTabCtrl, TCM_GETROWCOUNT, 0, 0);
334 rect.left = TAB_MARGIN;
335 rect.top = TAB_TOP_PADDING + cnt*(tabrc.bottom-tabrc.top) + TAB_MARGIN;
336 rect.right -= TAB_RIGHT_PADDING + TAB_MARGIN;
337 rect.bottom -= TAB_MARGIN;
338 width = rect.right-rect.left;
339 height = rect.bottom-rect.top;
341 SetWindowPos(hwnd, NULL, rect.left, rect.top, width, height,
342 SWP_NOZORDER | SWP_NOACTIVATE);
344 /* Resize the tab widget column to perfectly fit the tab window and
345 * leave sufficient space for the scroll widget.
347 switch (tab)
349 case TAB_INDEX: {
350 int scroll_width = GetSystemMetrics(SM_CXVSCROLL);
351 int border_width = GetSystemMetrics(SM_CXBORDER);
352 int edge_width = GetSystemMetrics(SM_CXEDGE);
354 SendMessageW(info->tabs[TAB_INDEX].hwnd, LVM_SETCOLUMNWIDTH, 0,
355 width-scroll_width-2*border_width-2*edge_width);
357 break;
362 static LRESULT Child_OnSize(HWND hwnd)
364 HHInfo *info = (HHInfo*)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
365 RECT rect;
367 if(!info || hwnd != info->WinType.hwndNavigation)
368 return 0;
370 GetClientRect(hwnd, &rect);
371 SetWindowPos(info->hwndTabCtrl, HWND_TOP, 0, 0,
372 rect.right - TAB_RIGHT_PADDING,
373 rect.bottom - TAB_TOP_PADDING, SWP_NOMOVE);
375 ResizeTabChild(info, TAB_CONTENTS);
376 ResizeTabChild(info, TAB_INDEX);
377 return 0;
380 static LRESULT OnTabChange(HWND hwnd)
382 HHInfo *info = (HHInfo*)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
384 TRACE("%p\n", hwnd);
386 if (!info)
387 return 0;
389 if(info->tabs[info->current_tab].hwnd)
390 ShowWindow(info->tabs[info->current_tab].hwnd, SW_HIDE);
392 info->current_tab = SendMessageW(info->hwndTabCtrl, TCM_GETCURSEL, 0, 0);
394 if(info->tabs[info->current_tab].hwnd)
395 ShowWindow(info->tabs[info->current_tab].hwnd, SW_SHOW);
397 return 0;
400 static LRESULT OnTopicChange(HWND hwnd, ContentItem *item)
402 HHInfo *info = (HHInfo*)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
403 LPCWSTR chmfile = NULL;
404 ContentItem *iter = item;
406 if(!item || !info)
407 return 0;
409 TRACE("name %s loal %s\n", debugstr_w(item->name), debugstr_w(item->local));
411 while(iter) {
412 if(iter->merge.chm_file) {
413 chmfile = iter->merge.chm_file;
414 break;
416 iter = iter->parent;
419 NavigateToChm(info, chmfile, item->local);
420 return 0;
423 static LRESULT CALLBACK Child_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
425 switch (message)
427 case WM_PAINT:
428 return Child_OnPaint(hWnd);
429 case WM_SIZE:
430 return Child_OnSize(hWnd);
431 case WM_NOTIFY: {
432 NMHDR *nmhdr = (NMHDR*)lParam;
433 switch(nmhdr->code) {
434 case TCN_SELCHANGE:
435 return OnTabChange(hWnd);
436 case TVN_SELCHANGEDW:
437 return OnTopicChange(hWnd, (ContentItem*)((NMTREEVIEWW *)lParam)->itemNew.lParam);
439 break;
441 default:
442 return DefWindowProcW(hWnd, message, wParam, lParam);
445 return 0;
448 static void HH_RegisterChildWndClass(HHInfo *pHHInfo)
450 WNDCLASSEXW wcex;
452 wcex.cbSize = sizeof(WNDCLASSEXW);
453 wcex.style = 0;
454 wcex.lpfnWndProc = Child_WndProc;
455 wcex.cbClsExtra = 0;
456 wcex.cbWndExtra = 0;
457 wcex.hInstance = hhctrl_hinstance;
458 wcex.hIcon = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
459 wcex.hCursor = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
460 wcex.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
461 wcex.lpszMenuName = NULL;
462 wcex.lpszClassName = szChildClass;
463 wcex.hIconSm = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
465 RegisterClassExW(&wcex);
468 /* Toolbar */
470 #define ICON_SIZE 20
472 static void TB_OnClick(HWND hWnd, DWORD dwID)
474 HHInfo *info = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
476 switch (dwID)
478 case IDTB_STOP:
479 DoPageAction(info, WB_STOP);
480 break;
481 case IDTB_REFRESH:
482 DoPageAction(info, WB_REFRESH);
483 break;
484 case IDTB_BACK:
485 DoPageAction(info, WB_GOBACK);
486 break;
487 case IDTB_HOME:
488 NavigateToChm(info, info->pCHMInfo->szFile, info->WinType.pszHome);
489 break;
490 case IDTB_FORWARD:
491 DoPageAction(info, WB_GOFORWARD);
492 break;
493 case IDTB_EXPAND:
494 case IDTB_CONTRACT:
495 case IDTB_SYNC:
496 case IDTB_PRINT:
497 case IDTB_OPTIONS:
498 case IDTB_BROWSE_FWD:
499 case IDTB_BROWSE_BACK:
500 case IDTB_JUMP1:
501 case IDTB_JUMP2:
502 case IDTB_CUSTOMIZE:
503 case IDTB_ZOOM:
504 case IDTB_TOC_NEXT:
505 case IDTB_TOC_PREV:
506 break;
510 static void TB_AddButton(TBBUTTON *pButtons, DWORD dwIndex, DWORD dwID)
512 /* FIXME: Load the correct button bitmaps */
513 pButtons[dwIndex].iBitmap = STD_PRINT;
514 pButtons[dwIndex].idCommand = dwID;
515 pButtons[dwIndex].fsState = TBSTATE_ENABLED;
516 pButtons[dwIndex].fsStyle = BTNS_BUTTON;
517 pButtons[dwIndex].dwData = 0;
518 pButtons[dwIndex].iString = 0;
521 static void TB_AddButtonsFromFlags(TBBUTTON *pButtons, DWORD dwButtonFlags, LPDWORD pdwNumButtons)
523 *pdwNumButtons = 0;
525 if (dwButtonFlags & HHWIN_BUTTON_EXPAND)
526 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_EXPAND);
528 if (dwButtonFlags & HHWIN_BUTTON_BACK)
529 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_BACK);
531 if (dwButtonFlags & HHWIN_BUTTON_FORWARD)
532 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_FORWARD);
534 if (dwButtonFlags & HHWIN_BUTTON_STOP)
535 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_STOP);
537 if (dwButtonFlags & HHWIN_BUTTON_REFRESH)
538 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_REFRESH);
540 if (dwButtonFlags & HHWIN_BUTTON_HOME)
541 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_HOME);
543 if (dwButtonFlags & HHWIN_BUTTON_SYNC)
544 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_SYNC);
546 if (dwButtonFlags & HHWIN_BUTTON_OPTIONS)
547 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_OPTIONS);
549 if (dwButtonFlags & HHWIN_BUTTON_PRINT)
550 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_PRINT);
552 if (dwButtonFlags & HHWIN_BUTTON_JUMP1)
553 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_JUMP1);
555 if (dwButtonFlags & HHWIN_BUTTON_JUMP2)
556 TB_AddButton(pButtons,(*pdwNumButtons)++, IDTB_JUMP2);
558 if (dwButtonFlags & HHWIN_BUTTON_ZOOM)
559 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_ZOOM);
561 if (dwButtonFlags & HHWIN_BUTTON_TOC_NEXT)
562 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_TOC_NEXT);
564 if (dwButtonFlags & HHWIN_BUTTON_TOC_PREV)
565 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_TOC_PREV);
568 static BOOL HH_AddToolbar(HHInfo *pHHInfo)
570 HWND hToolbar;
571 HWND hwndParent = pHHInfo->WinType.hwndHelp;
572 DWORD toolbarFlags;
573 TBBUTTON buttons[IDTB_TOC_PREV - IDTB_EXPAND];
574 TBADDBITMAP tbAB;
575 DWORD dwStyles, dwExStyles;
576 DWORD dwNumButtons, dwIndex;
578 if (pHHInfo->WinType.fsWinProperties & HHWIN_PARAM_TB_FLAGS)
579 toolbarFlags = pHHInfo->WinType.fsToolBarFlags;
580 else
581 toolbarFlags = HHWIN_DEF_BUTTONS;
583 TB_AddButtonsFromFlags(buttons, toolbarFlags, &dwNumButtons);
585 dwStyles = WS_CHILDWINDOW | WS_VISIBLE | TBSTYLE_FLAT |
586 TBSTYLE_WRAPABLE | TBSTYLE_TOOLTIPS | CCS_NODIVIDER;
587 dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
589 hToolbar = CreateWindowExW(dwExStyles, TOOLBARCLASSNAMEW, NULL, dwStyles,
590 0, 0, 0, 0, hwndParent, NULL,
591 hhctrl_hinstance, NULL);
592 if (!hToolbar)
593 return FALSE;
595 SendMessageW(hToolbar, TB_SETBITMAPSIZE, 0, MAKELONG(ICON_SIZE, ICON_SIZE));
596 SendMessageW(hToolbar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
597 SendMessageW(hToolbar, WM_SETFONT, (WPARAM)pHHInfo->hFont, TRUE);
599 /* FIXME: Load correct icons for all buttons */
600 tbAB.hInst = HINST_COMMCTRL;
601 tbAB.nID = IDB_STD_LARGE_COLOR;
602 SendMessageW(hToolbar, TB_ADDBITMAP, 0, (LPARAM)&tbAB);
604 for (dwIndex = 0; dwIndex < dwNumButtons; dwIndex++)
606 LPWSTR szBuf = HH_LoadString(buttons[dwIndex].idCommand);
607 DWORD dwLen = strlenW(szBuf);
608 szBuf[dwLen + 1] = 0; /* Double-null terminate */
610 buttons[dwIndex].iString = (DWORD)SendMessageW(hToolbar, TB_ADDSTRINGW, 0, (LPARAM)szBuf);
611 heap_free(szBuf);
614 SendMessageW(hToolbar, TB_ADDBUTTONSW, dwNumButtons, (LPARAM)buttons);
615 SendMessageW(hToolbar, TB_AUTOSIZE, 0, 0);
616 ShowWindow(hToolbar, SW_SHOW);
618 pHHInfo->WinType.hwndToolBar = hToolbar;
619 return TRUE;
622 /* Navigation Pane */
624 static void NP_GetNavigationRect(HHInfo *pHHInfo, RECT *rc)
626 HWND hwndParent = pHHInfo->WinType.hwndHelp;
627 HWND hwndToolbar = pHHInfo->WinType.hwndToolBar;
628 RECT rectWND, rectTB;
630 GetClientRect(hwndParent, &rectWND);
631 GetClientRect(hwndToolbar, &rectTB);
633 rc->left = 0;
634 rc->top = rectTB.bottom;
635 rc->bottom = rectWND.bottom - rectTB.bottom;
637 if (!(pHHInfo->WinType.fsValidMembers & HHWIN_PARAM_NAV_WIDTH) &&
638 pHHInfo->WinType.iNavWidth == 0)
640 pHHInfo->WinType.iNavWidth = WINTYPE_DEFAULT_NAVWIDTH;
643 rc->right = pHHInfo->WinType.iNavWidth;
646 static DWORD NP_CreateTab(HINSTANCE hInstance, HWND hwndTabCtrl, DWORD index)
648 TCITEMW tie;
649 LPWSTR tabText = HH_LoadString(index);
650 DWORD ret;
652 tie.mask = TCIF_TEXT;
653 tie.pszText = tabText;
655 ret = SendMessageW( hwndTabCtrl, TCM_INSERTITEMW, index, (LPARAM)&tie );
657 heap_free(tabText);
658 return ret;
661 static BOOL HH_AddNavigationPane(HHInfo *info)
663 HWND hWnd, hwndTabCtrl;
664 HWND hwndParent = info->WinType.hwndHelp;
665 DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE;
666 DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
667 RECT rc;
669 NP_GetNavigationRect(info, &rc);
671 hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
672 rc.left, rc.top, rc.right, rc.bottom,
673 hwndParent, NULL, hhctrl_hinstance, NULL);
674 if (!hWnd)
675 return FALSE;
677 SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)info);
679 hwndTabCtrl = CreateWindowExW(dwExStyles, WC_TABCONTROLW, szEmpty, dwStyles,
680 0, TAB_TOP_PADDING,
681 rc.right - TAB_RIGHT_PADDING,
682 rc.bottom - TAB_TOP_PADDING,
683 hWnd, NULL, hhctrl_hinstance, NULL);
684 if (!hwndTabCtrl)
685 return FALSE;
687 if (*info->WinType.pszToc)
688 info->tabs[TAB_CONTENTS].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_CONTENTS);
690 if (*info->WinType.pszIndex)
691 info->tabs[TAB_INDEX].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_INDEX);
693 if (info->WinType.fsWinProperties & HHWIN_PROP_TAB_SEARCH)
694 info->tabs[TAB_SEARCH].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_SEARCH);
696 if (info->WinType.fsWinProperties & HHWIN_PROP_TAB_FAVORITES)
697 info->tabs[TAB_FAVORITES].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_FAVORITES);
699 SendMessageW(hwndTabCtrl, WM_SETFONT, (WPARAM)info->hFont, TRUE);
701 info->hwndTabCtrl = hwndTabCtrl;
702 info->WinType.hwndNavigation = hWnd;
703 return TRUE;
706 /* HTML Pane */
708 static void HP_GetHTMLRect(HHInfo *info, RECT *rc)
710 RECT rectTB, rectWND, rectNP, rectSB;
712 GetClientRect(info->WinType.hwndHelp, &rectWND);
713 GetClientRect(info->WinType.hwndToolBar, &rectTB);
714 GetClientRect(info->WinType.hwndNavigation, &rectNP);
715 GetClientRect(info->hwndSizeBar, &rectSB);
717 rc->left = rectNP.right + rectSB.right;
718 rc->top = rectTB.bottom;
719 rc->right = rectWND.right - rc->left;
720 rc->bottom = rectWND.bottom - rectTB.bottom;
723 static BOOL HH_AddHTMLPane(HHInfo *pHHInfo)
725 HWND hWnd;
726 HWND hwndParent = pHHInfo->WinType.hwndHelp;
727 DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN;
728 DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_CLIENTEDGE;
729 RECT rc;
731 HP_GetHTMLRect(pHHInfo, &rc);
733 hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
734 rc.left, rc.top, rc.right, rc.bottom,
735 hwndParent, NULL, hhctrl_hinstance, NULL);
736 if (!hWnd)
737 return FALSE;
739 if (!InitWebBrowser(pHHInfo, hWnd))
740 return FALSE;
742 /* store the pointer to the HH info struct */
743 SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
745 ShowWindow(hWnd, SW_SHOW);
746 UpdateWindow(hWnd);
748 pHHInfo->WinType.hwndHTML = hWnd;
749 return TRUE;
752 static BOOL AddContentTab(HHInfo *info)
754 if(info->tabs[TAB_CONTENTS].id == -1)
755 return TRUE; /* No "Contents" tab */
756 info->tabs[TAB_CONTENTS].hwnd = CreateWindowExW(WS_EX_CLIENTEDGE, WC_TREEVIEWW,
757 szEmpty, WS_CHILD | WS_BORDER | 0x25, 50, 50, 100, 100,
758 info->WinType.hwndNavigation, NULL, hhctrl_hinstance, NULL);
759 if(!info->tabs[TAB_CONTENTS].hwnd) {
760 ERR("Could not create treeview control\n");
761 return FALSE;
764 ResizeTabChild(info, TAB_CONTENTS);
765 ShowWindow(info->tabs[TAB_CONTENTS].hwnd, SW_SHOW);
767 return TRUE;
770 static BOOL AddIndexTab(HHInfo *info)
772 char hidden_column[] = "Column";
773 LVCOLUMNA lvc;
775 if(info->tabs[TAB_INDEX].id == -1)
776 return TRUE; /* No "Index" tab */
777 info->tabs[TAB_INDEX].hwnd = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW,
778 szEmpty, WS_CHILD | WS_BORDER | LVS_SINGLESEL | LVS_REPORT | LVS_NOCOLUMNHEADER, 50, 50, 100, 100,
779 info->WinType.hwndNavigation, NULL, hhctrl_hinstance, NULL);
780 if(!info->tabs[TAB_INDEX].hwnd) {
781 ERR("Could not create ListView control\n");
782 return FALSE;
784 memset(&lvc, 0, sizeof(lvc));
785 lvc.mask = LVCF_TEXT;
786 lvc.pszText = hidden_column;
787 if(SendMessageW(info->tabs[TAB_INDEX].hwnd, LVM_INSERTCOLUMNA, 0, (LPARAM) &lvc) == -1)
789 ERR("Could not create ListView column\n");
790 return FALSE;
793 ResizeTabChild(info, TAB_INDEX);
794 ShowWindow(info->tabs[TAB_INDEX].hwnd, SW_HIDE);
796 return TRUE;
799 /* Viewer Window */
801 static LRESULT Help_OnSize(HWND hWnd)
803 HHInfo *pHHInfo = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
804 DWORD dwSize;
805 RECT rc;
807 if (!pHHInfo)
808 return 0;
810 NP_GetNavigationRect(pHHInfo, &rc);
811 SetWindowPos(pHHInfo->WinType.hwndNavigation, HWND_TOP, 0, 0,
812 rc.right, rc.bottom, SWP_NOMOVE);
814 SB_GetSizeBarRect(pHHInfo, &rc);
815 SetWindowPos(pHHInfo->hwndSizeBar, HWND_TOP, rc.left, rc.top,
816 rc.right, rc.bottom, SWP_SHOWWINDOW);
818 HP_GetHTMLRect(pHHInfo, &rc);
819 SetWindowPos(pHHInfo->WinType.hwndHTML, HWND_TOP, rc.left, rc.top,
820 rc.right, rc.bottom, SWP_SHOWWINDOW);
822 /* Resize browser window taking the frame size into account */
823 dwSize = GetSystemMetrics(SM_CXFRAME);
824 ResizeWebBrowser(pHHInfo, rc.right - dwSize, rc.bottom - dwSize);
826 return 0;
829 static LRESULT CALLBACK Help_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
831 switch (message)
833 case WM_COMMAND:
834 if (HIWORD(wParam) == BN_CLICKED)
835 TB_OnClick(hWnd, LOWORD(wParam));
836 break;
837 case WM_SIZE:
838 return Help_OnSize(hWnd);
839 case WM_CLOSE:
840 ReleaseHelpViewer((HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA));
841 return 0;
842 case WM_DESTROY:
843 if(hh_process)
844 PostQuitMessage(0);
845 break;
847 default:
848 return DefWindowProcW(hWnd, message, wParam, lParam);
851 return 0;
854 static BOOL HH_CreateHelpWindow(HHInfo *info)
856 HWND hWnd;
857 RECT winPos = info->WinType.rcWindowPos;
858 WNDCLASSEXW wcex;
859 DWORD dwStyles, dwExStyles;
860 DWORD x, y, width, height;
862 static const WCHAR windowClassW[] = {
863 'H','H',' ', 'P','a','r','e','n','t',0
866 wcex.cbSize = sizeof(WNDCLASSEXW);
867 wcex.style = CS_HREDRAW | CS_VREDRAW;
868 wcex.lpfnWndProc = Help_WndProc;
869 wcex.cbClsExtra = 0;
870 wcex.cbWndExtra = 0;
871 wcex.hInstance = hhctrl_hinstance;
872 wcex.hIcon = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
873 wcex.hCursor = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
874 wcex.hbrBackground = (HBRUSH)(COLOR_MENU + 1);
875 wcex.lpszMenuName = NULL;
876 wcex.lpszClassName = windowClassW;
877 wcex.hIconSm = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
879 RegisterClassExW(&wcex);
881 /* Read in window parameters if available */
882 if (info->WinType.fsValidMembers & HHWIN_PARAM_STYLES)
883 dwStyles = info->WinType.dwStyles | WS_OVERLAPPEDWINDOW;
884 else
885 dwStyles = WS_OVERLAPPEDWINDOW | WS_VISIBLE |
886 WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
888 if (info->WinType.fsValidMembers & HHWIN_PARAM_EXSTYLES)
889 dwExStyles = info->WinType.dwExStyles;
890 else
891 dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_APPWINDOW |
892 WS_EX_WINDOWEDGE | WS_EX_RIGHTSCROLLBAR;
894 if (info->WinType.fsValidMembers & HHWIN_PARAM_RECT)
896 x = winPos.left;
897 y = winPos.top;
898 width = winPos.right - x;
899 height = winPos.bottom - y;
901 else
903 x = WINTYPE_DEFAULT_X;
904 y = WINTYPE_DEFAULT_Y;
905 width = WINTYPE_DEFAULT_WIDTH;
906 height = WINTYPE_DEFAULT_HEIGHT;
909 hWnd = CreateWindowExW(dwExStyles, windowClassW, info->WinType.pszCaption,
910 dwStyles, x, y, width, height, NULL, NULL, hhctrl_hinstance, NULL);
911 if (!hWnd)
912 return FALSE;
914 ShowWindow(hWnd, SW_SHOW);
915 UpdateWindow(hWnd);
917 /* store the pointer to the HH info struct */
918 SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)info);
920 info->WinType.hwndHelp = hWnd;
921 return TRUE;
924 static void HH_CreateFont(HHInfo *pHHInfo)
926 LOGFONTW lf;
928 GetObjectW(GetStockObject(ANSI_VAR_FONT), sizeof(LOGFONTW), &lf);
929 lf.lfWeight = FW_NORMAL;
930 lf.lfItalic = FALSE;
931 lf.lfUnderline = FALSE;
933 pHHInfo->hFont = CreateFontIndirectW(&lf);
936 static void HH_InitRequiredControls(DWORD dwControls)
938 INITCOMMONCONTROLSEX icex;
940 icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
941 icex.dwICC = dwControls;
942 InitCommonControlsEx(&icex);
945 /* Creates the whole package */
946 static BOOL CreateViewer(HHInfo *pHHInfo)
948 HH_CreateFont(pHHInfo);
950 if (!HH_CreateHelpWindow(pHHInfo))
951 return FALSE;
953 HH_InitRequiredControls(ICC_BAR_CLASSES);
955 if (!HH_AddToolbar(pHHInfo))
956 return FALSE;
958 HH_RegisterChildWndClass(pHHInfo);
960 if (!HH_AddNavigationPane(pHHInfo))
961 return FALSE;
963 HH_RegisterSizeBarClass(pHHInfo);
965 if (!HH_AddSizeBar(pHHInfo))
966 return FALSE;
968 if (!HH_AddHTMLPane(pHHInfo))
969 return FALSE;
971 if (!AddContentTab(pHHInfo))
972 return FALSE;
974 if (!AddIndexTab(pHHInfo))
975 return FALSE;
977 InitContent(pHHInfo);
979 return TRUE;
982 void ReleaseHelpViewer(HHInfo *info)
984 TRACE("(%p)\n", info);
986 if (!info)
987 return;
989 /* Free allocated strings */
990 heap_free(info->pszType);
991 heap_free(info->pszCaption);
992 heap_free(info->pszToc);
993 heap_free(info->pszIndex);
994 heap_free(info->pszFile);
995 heap_free(info->pszHome);
996 heap_free(info->pszJump1);
997 heap_free(info->pszJump2);
998 heap_free(info->pszUrlJump1);
999 heap_free(info->pszUrlJump2);
1001 if (info->pCHMInfo)
1002 CloseCHM(info->pCHMInfo);
1004 ReleaseWebBrowser(info);
1005 ReleaseContent(info);
1007 if(info->WinType.hwndHelp)
1008 DestroyWindow(info->WinType.hwndHelp);
1010 heap_free(info);
1011 OleUninitialize();
1014 HHInfo *CreateHelpViewer(LPCWSTR filename)
1016 HHInfo *info = heap_alloc_zero(sizeof(HHInfo));
1017 int i;
1019 /* Set the invalid tab ID (-1) as the default value for all
1020 * of the tabs, this matches a failed TCM_INSERTITEM call.
1022 for(i=0;i<sizeof(info->tabs)/sizeof(HHTab);i++)
1023 info->tabs[i].id = -1;
1025 OleInitialize(NULL);
1027 info->pCHMInfo = OpenCHM(filename);
1028 if(!info->pCHMInfo) {
1029 ReleaseHelpViewer(info);
1030 return NULL;
1033 if (!LoadWinTypeFromCHM(info)) {
1034 ReleaseHelpViewer(info);
1035 return NULL;
1038 if(!CreateViewer(info)) {
1039 ReleaseHelpViewer(info);
1040 return NULL;
1043 return info;