gdi32: Make clip_line external.
[wine/multimedia.git] / dlls / hhctrl.ocx / help.c
blobd710d3b98013be008115d34b8b6490fcc9288567
1 /*
2 * Help Viewer Implementation
4 * Copyright 2005 James Hawkins
5 * Copyright 2007 Jacek Caban for CodeWeavers
6 * Copyright 2011 Owen Rudge for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "hhctrl.h"
25 #include "wingdi.h"
26 #include "commctrl.h"
27 #include "wininet.h"
29 #include "wine/debug.h"
31 #include "resource.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(htmlhelp);
35 static LRESULT Help_OnSize(HWND hWnd);
36 static void ExpandContract(HHInfo *pHHInfo);
38 /* Window type defaults */
40 #define WINTYPE_DEFAULT_X 280
41 #define WINTYPE_DEFAULT_Y 100
42 #define WINTYPE_DEFAULT_WIDTH 740
43 #define WINTYPE_DEFAULT_HEIGHT 640
44 #define WINTYPE_DEFAULT_NAVWIDTH 250
46 #define TAB_TOP_PADDING 8
47 #define TAB_RIGHT_PADDING 4
48 #define TAB_MARGIN 8
49 #define EDIT_HEIGHT 20
51 static const WCHAR szEmpty[] = {0};
53 /* Loads a string from the resource file */
54 static LPWSTR HH_LoadString(DWORD dwID)
56 LPWSTR string = NULL;
57 LPCWSTR stringresource;
58 int iSize;
60 iSize = LoadStringW(hhctrl_hinstance, dwID, (LPWSTR)&stringresource, 0);
62 string = heap_alloc((iSize + 2) * sizeof(WCHAR)); /* some strings (tab text) needs double-null termination */
63 memcpy(string, stringresource, iSize*sizeof(WCHAR));
64 string[iSize] = 0;
66 return string;
69 static HRESULT navigate_url(HHInfo *info, LPCWSTR surl)
71 VARIANT url;
72 HRESULT hres;
74 TRACE("%s\n", debugstr_w(surl));
76 V_VT(&url) = VT_BSTR;
77 V_BSTR(&url) = SysAllocString(surl);
79 hres = IWebBrowser2_Navigate2(info->web_browser, &url, 0, 0, 0, 0);
81 VariantClear(&url);
83 if(FAILED(hres))
84 TRACE("Navigation failed: %08x\n", hres);
86 return hres;
89 BOOL NavigateToUrl(HHInfo *info, LPCWSTR surl)
91 ChmPath chm_path;
92 BOOL ret;
93 HRESULT hres;
95 static const WCHAR url_indicator[] = {':', '/', '/', 0};
97 TRACE("%s\n", debugstr_w(surl));
99 if (strstrW(surl, url_indicator)) {
100 hres = navigate_url(info, surl);
101 if(SUCCEEDED(hres))
102 return TRUE;
103 } /* look up in chm if it doesn't look like a full url */
105 SetChmPath(&chm_path, info->pCHMInfo->szFile, surl);
106 ret = NavigateToChm(info, chm_path.chm_file, chm_path.chm_index);
108 heap_free(chm_path.chm_file);
109 heap_free(chm_path.chm_index);
111 return ret;
114 static BOOL AppendFullPathURL(LPCWSTR file, LPWSTR buf, LPCWSTR index)
116 static const WCHAR url_format[] =
117 {'m','k',':','@','M','S','I','T','S','t','o','r','e',':','%','s',':',':','%','s','%','s',0};
118 static const WCHAR slash[] = {'/',0};
119 static const WCHAR empty[] = {0};
120 WCHAR full_path[MAX_PATH];
122 TRACE("%s %p %s\n", debugstr_w(file), buf, debugstr_w(index));
124 if(!GetFullPathNameW(file, sizeof(full_path)/sizeof(full_path[0]), full_path, NULL)) {
125 WARN("GetFullPathName failed: %u\n", GetLastError());
126 return FALSE;
129 wsprintfW(buf, url_format, full_path, (!index || index[0] == '/') ? empty : slash, index);
130 return TRUE;
133 BOOL NavigateToChm(HHInfo *info, LPCWSTR file, LPCWSTR index)
135 WCHAR buf[INTERNET_MAX_URL_LENGTH];
136 LPWSTR ptr;
138 TRACE("%p %s %s\n", info, debugstr_w(file), debugstr_w(index));
140 if ((!info->web_browser) || !AppendFullPathURL(file, buf, index))
141 return FALSE;
143 /* FIXME: HACK */
144 if((ptr = strchrW(buf, '#')))
145 *ptr = 0;
147 return SUCCEEDED(navigate_url(info, buf));
150 static void DoSync(HHInfo *info)
152 WCHAR buf[INTERNET_MAX_URL_LENGTH];
153 HRESULT hres;
154 DWORD len;
155 BSTR url;
157 hres = IWebBrowser2_get_LocationURL(info->web_browser, &url);
159 if (FAILED(hres))
161 WARN("get_LocationURL failed: %08x\n", hres);
162 return;
165 /* If we're not currently viewing a page in the active .chm file, abort */
166 if ((!AppendFullPathURL(info->pszFile, buf, NULL)) || (len = lstrlenW(buf) > lstrlenW(url)))
168 SysFreeString(url);
169 return;
172 if (lstrcmpiW(buf, url) > 0)
174 static const WCHAR delimW[] = {':',':','/',0};
175 const WCHAR *index;
177 index = strstrW(url, delimW);
179 if (index)
180 ActivateContentTopic(info->tabs[TAB_CONTENTS].hwnd, index + 3, info->content); /* skip over ::/ */
183 SysFreeString(url);
186 /* Size Bar */
188 #define SIZEBAR_WIDTH 4
190 static const WCHAR szSizeBarClass[] = {
191 'H','H',' ','S','i','z','e','B','a','r',0
194 /* Draw the SizeBar */
195 static void SB_OnPaint(HWND hWnd)
197 PAINTSTRUCT ps;
198 HDC hdc;
199 RECT rc;
201 hdc = BeginPaint(hWnd, &ps);
203 GetClientRect(hWnd, &rc);
205 /* dark frame */
206 rc.right += 1;
207 rc.bottom -= 1;
208 FrameRect(hdc, &rc, GetStockObject(GRAY_BRUSH));
210 /* white highlight */
211 SelectObject(hdc, GetStockObject(WHITE_PEN));
212 MoveToEx(hdc, rc.right, 1, NULL);
213 LineTo(hdc, 1, 1);
214 LineTo(hdc, 1, rc.bottom - 1);
217 MoveToEx(hdc, 0, rc.bottom, NULL);
218 LineTo(hdc, rc.right, rc.bottom);
220 EndPaint(hWnd, &ps);
223 static void SB_OnLButtonDown(HWND hWnd, WPARAM wParam, LPARAM lParam)
225 SetCapture(hWnd);
228 static void SB_OnLButtonUp(HWND hWnd, WPARAM wParam, LPARAM lParam)
230 HHInfo *pHHInfo = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
231 POINT pt;
233 pt.x = (short)LOWORD(lParam);
234 pt.y = (short)HIWORD(lParam);
236 /* update the window sizes */
237 pHHInfo->WinType.iNavWidth += pt.x;
238 Help_OnSize(hWnd);
240 ReleaseCapture();
243 static void SB_OnMouseMove(HWND hWnd, WPARAM wParam, LPARAM lParam)
245 /* ignore WM_MOUSEMOVE if not dragging the SizeBar */
246 if (!(wParam & MK_LBUTTON))
247 return;
250 static LRESULT CALLBACK SizeBar_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
252 switch (message)
254 case WM_LBUTTONDOWN:
255 SB_OnLButtonDown(hWnd, wParam, lParam);
256 break;
257 case WM_LBUTTONUP:
258 SB_OnLButtonUp(hWnd, wParam, lParam);
259 break;
260 case WM_MOUSEMOVE:
261 SB_OnMouseMove(hWnd, wParam, lParam);
262 break;
263 case WM_PAINT:
264 SB_OnPaint(hWnd);
265 break;
266 default:
267 return DefWindowProcW(hWnd, message, wParam, lParam);
270 return 0;
273 static void HH_RegisterSizeBarClass(HHInfo *pHHInfo)
275 WNDCLASSEXW wcex;
277 wcex.cbSize = sizeof(WNDCLASSEXW);
278 wcex.style = 0;
279 wcex.lpfnWndProc = SizeBar_WndProc;
280 wcex.cbClsExtra = 0;
281 wcex.cbWndExtra = 0;
282 wcex.hInstance = hhctrl_hinstance;
283 wcex.hIcon = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
284 wcex.hCursor = LoadCursorW(NULL, (LPCWSTR)IDC_SIZEWE);
285 wcex.hbrBackground = (HBRUSH)(COLOR_MENU + 1);
286 wcex.lpszMenuName = NULL;
287 wcex.lpszClassName = szSizeBarClass;
288 wcex.hIconSm = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
290 RegisterClassExW(&wcex);
293 static void SB_GetSizeBarRect(HHInfo *info, RECT *rc)
295 RECT rectWND, rectTB, rectNP;
297 GetClientRect(info->WinType.hwndHelp, &rectWND);
298 GetClientRect(info->WinType.hwndToolBar, &rectTB);
299 GetClientRect(info->WinType.hwndNavigation, &rectNP);
301 rc->left = rectNP.right;
302 rc->top = rectTB.bottom;
303 rc->bottom = rectWND.bottom - rectTB.bottom;
304 rc->right = SIZEBAR_WIDTH;
307 static BOOL HH_AddSizeBar(HHInfo *pHHInfo)
309 HWND hWnd;
310 HWND hwndParent = pHHInfo->WinType.hwndHelp;
311 DWORD dwStyles = WS_CHILDWINDOW | WS_OVERLAPPED;
312 DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
313 RECT rc;
315 if (!pHHInfo->WinType.fNotExpanded)
316 dwStyles |= WS_VISIBLE;
318 SB_GetSizeBarRect(pHHInfo, &rc);
320 hWnd = CreateWindowExW(dwExStyles, szSizeBarClass, szEmpty, dwStyles,
321 rc.left, rc.top, rc.right, rc.bottom,
322 hwndParent, NULL, hhctrl_hinstance, NULL);
323 if (!hWnd)
324 return FALSE;
326 /* store the pointer to the HH info struct */
327 SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
329 pHHInfo->hwndSizeBar = hWnd;
330 return TRUE;
333 /* Child Window */
335 static const WCHAR szChildClass[] = {
336 'H','H',' ','C','h','i','l','d',0
339 static LRESULT Child_OnPaint(HWND hWnd)
341 PAINTSTRUCT ps;
342 HDC hdc;
343 RECT rc;
345 hdc = BeginPaint(hWnd, &ps);
347 /* Only paint the Navigation pane, identified by the fact
348 * that it has a child window
350 if (GetWindow(hWnd, GW_CHILD))
352 GetClientRect(hWnd, &rc);
354 /* set the border color */
355 SelectObject(hdc, GetStockObject(DC_PEN));
356 SetDCPenColor(hdc, GetSysColor(COLOR_BTNSHADOW));
358 /* Draw the top border */
359 LineTo(hdc, rc.right, 0);
361 SelectObject(hdc, GetStockObject(WHITE_PEN));
362 MoveToEx(hdc, 0, 1, NULL);
363 LineTo(hdc, rc.right, 1);
366 EndPaint(hWnd, &ps);
368 return 0;
371 static void ResizeTabChild(HHInfo *info, int tab)
373 HWND hwnd = info->tabs[tab].hwnd;
374 INT width, height;
375 RECT rect, tabrc;
376 DWORD cnt;
378 GetClientRect(info->WinType.hwndNavigation, &rect);
379 SendMessageW(info->hwndTabCtrl, TCM_GETITEMRECT, 0, (LPARAM)&tabrc);
380 cnt = SendMessageW(info->hwndTabCtrl, TCM_GETROWCOUNT, 0, 0);
382 rect.left = TAB_MARGIN;
383 rect.top = TAB_TOP_PADDING + cnt*(tabrc.bottom-tabrc.top) + TAB_MARGIN;
384 rect.right -= TAB_RIGHT_PADDING + TAB_MARGIN;
385 rect.bottom -= TAB_MARGIN;
386 width = rect.right-rect.left;
387 height = rect.bottom-rect.top;
389 SetWindowPos(hwnd, NULL, rect.left, rect.top, width, height,
390 SWP_NOZORDER | SWP_NOACTIVATE);
392 switch (tab)
394 case TAB_INDEX: {
395 int scroll_width = GetSystemMetrics(SM_CXVSCROLL);
396 int border_width = GetSystemMetrics(SM_CXBORDER);
397 int edge_width = GetSystemMetrics(SM_CXEDGE);
399 /* Resize the tab widget column to perfectly fit the tab window and
400 * leave sufficient space for the scroll widget.
402 SendMessageW(info->tabs[TAB_INDEX].hwnd, LVM_SETCOLUMNWIDTH, 0,
403 width-scroll_width-2*border_width-2*edge_width);
405 break;
407 case TAB_SEARCH: {
408 int scroll_width = GetSystemMetrics(SM_CXVSCROLL);
409 int border_width = GetSystemMetrics(SM_CXBORDER);
410 int edge_width = GetSystemMetrics(SM_CXEDGE);
411 int top_pos = 0;
413 SetWindowPos(info->search.hwndEdit, NULL, 0, top_pos, width,
414 EDIT_HEIGHT, SWP_NOZORDER | SWP_NOACTIVATE);
415 top_pos += EDIT_HEIGHT + TAB_MARGIN;
416 SetWindowPos(info->search.hwndList, NULL, 0, top_pos, width,
417 height-top_pos, SWP_NOZORDER | SWP_NOACTIVATE);
418 /* Resize the tab widget column to perfectly fit the tab window and
419 * leave sufficient space for the scroll widget.
421 SendMessageW(info->search.hwndList, LVM_SETCOLUMNWIDTH, 0,
422 width-scroll_width-2*border_width-2*edge_width);
424 break;
429 static LRESULT Child_OnSize(HWND hwnd)
431 HHInfo *info = (HHInfo*)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
432 RECT rect;
434 if(!info || hwnd != info->WinType.hwndNavigation)
435 return 0;
437 GetClientRect(hwnd, &rect);
438 SetWindowPos(info->hwndTabCtrl, HWND_TOP, 0, 0,
439 rect.right - TAB_RIGHT_PADDING,
440 rect.bottom - TAB_TOP_PADDING, SWP_NOMOVE);
442 ResizeTabChild(info, TAB_CONTENTS);
443 ResizeTabChild(info, TAB_INDEX);
444 ResizeTabChild(info, TAB_SEARCH);
445 return 0;
448 static LRESULT OnTabChange(HWND hwnd)
450 HHInfo *info = (HHInfo*)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
452 TRACE("%p\n", hwnd);
454 if (!info)
455 return 0;
457 if(info->tabs[info->current_tab].hwnd)
458 ShowWindow(info->tabs[info->current_tab].hwnd, SW_HIDE);
460 info->current_tab = SendMessageW(info->hwndTabCtrl, TCM_GETCURSEL, 0, 0);
462 if(info->tabs[info->current_tab].hwnd)
463 ShowWindow(info->tabs[info->current_tab].hwnd, SW_SHOW);
465 return 0;
468 static LRESULT OnTopicChange(HHInfo *info, void *user_data)
470 LPCWSTR chmfile = NULL, name = NULL, local = NULL;
471 ContentItem *citer;
472 SearchItem *siter;
473 IndexItem *iiter;
475 if(!user_data || !info)
476 return 0;
478 switch (info->current_tab)
480 case TAB_CONTENTS:
481 citer = (ContentItem *) user_data;
482 name = citer->name;
483 local = citer->local;
484 while(citer) {
485 if(citer->merge.chm_file) {
486 chmfile = citer->merge.chm_file;
487 break;
489 citer = citer->parent;
491 break;
492 case TAB_INDEX:
493 iiter = (IndexItem *) user_data;
494 if(iiter->nItems == 0) {
495 FIXME("No entries for this item!\n");
496 return 0;
498 if(iiter->nItems > 1) {
499 int i = 0;
500 LVITEMW lvi;
502 SendMessageW(info->popup.hwndList, LVM_DELETEALLITEMS, 0, 0);
503 for(i=0;i<iiter->nItems;i++) {
504 IndexSubItem *item = &iiter->items[i];
505 WCHAR *name = iiter->keyword;
507 if(item->name)
508 name = item->name;
509 memset(&lvi, 0, sizeof(lvi));
510 lvi.iItem = i;
511 lvi.mask = LVIF_TEXT|LVIF_PARAM;
512 lvi.cchTextMax = strlenW(name)+1;
513 lvi.pszText = name;
514 lvi.lParam = (LPARAM) item;
515 SendMessageW(info->popup.hwndList, LVM_INSERTITEMW, 0, (LPARAM)&lvi);
517 ShowWindow(info->popup.hwndPopup, SW_SHOW);
518 return 0;
520 name = iiter->items[0].name;
521 local = iiter->items[0].local;
522 chmfile = iiter->merge.chm_file;
523 break;
524 case TAB_SEARCH:
525 siter = (SearchItem *) user_data;
526 name = siter->filename;
527 local = siter->filename;
528 chmfile = info->pCHMInfo->szFile;
529 break;
530 default:
531 FIXME("Unhandled operation for this tab!\n");
532 return 0;
535 if(!chmfile)
537 FIXME("No help file found for this item!\n");
538 return 0;
541 TRACE("name %s loal %s\n", debugstr_w(name), debugstr_w(local));
543 NavigateToChm(info, chmfile, local);
544 return 0;
547 /* Capture the Enter/Return key and send it up to Child_WndProc as an NM_RETURN message */
548 static LRESULT CALLBACK EditChild_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
550 WNDPROC editWndProc = (WNDPROC)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
552 if(message == WM_KEYUP && wParam == VK_RETURN)
554 NMHDR nmhdr;
556 nmhdr.hwndFrom = hWnd;
557 nmhdr.code = NM_RETURN;
558 SendMessageW(GetParent(GetParent(hWnd)), WM_NOTIFY, wParam, (LPARAM)&nmhdr);
560 return editWndProc(hWnd, message, wParam, lParam);
563 static LRESULT CALLBACK Child_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
565 switch (message)
567 case WM_PAINT:
568 return Child_OnPaint(hWnd);
569 case WM_SIZE:
570 return Child_OnSize(hWnd);
571 case WM_NOTIFY: {
572 HHInfo *info = (HHInfo*)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
573 NMHDR *nmhdr = (NMHDR*)lParam;
575 switch(nmhdr->code) {
576 case TCN_SELCHANGE:
577 return OnTabChange(hWnd);
578 case TVN_SELCHANGEDW:
579 return OnTopicChange(info, (void*)((NMTREEVIEWW *)lParam)->itemNew.lParam);
580 case NM_DBLCLK:
581 if(!info)
582 return 0;
583 switch(info->current_tab)
585 case TAB_INDEX:
586 return OnTopicChange(info, (void*)((NMITEMACTIVATE *)lParam)->lParam);
587 case TAB_SEARCH:
588 return OnTopicChange(info, (void*)((NMITEMACTIVATE *)lParam)->lParam);
590 break;
591 case NM_RETURN:
592 if(!info)
593 return 0;
594 switch(info->current_tab) {
595 case TAB_INDEX: {
596 HWND hwndList = info->tabs[TAB_INDEX].hwnd;
597 LVITEMW lvItem;
599 lvItem.iItem = (int) SendMessageW(hwndList, LVM_GETSELECTIONMARK, 0, 0);
600 lvItem.mask = TVIF_PARAM;
601 SendMessageW(hwndList, LVM_GETITEMW, 0, (LPARAM)&lvItem);
602 OnTopicChange(info, (void*) lvItem.lParam);
603 return 0;
605 case TAB_SEARCH: {
606 if(nmhdr->hwndFrom == info->search.hwndEdit) {
607 char needle[100];
608 DWORD i, len;
610 len = GetWindowTextA(info->search.hwndEdit, needle, sizeof(needle));
611 if(!len)
613 FIXME("Unable to get search text.\n");
614 return 0;
616 /* Convert the requested text for comparison later against the
617 * lower case version of HTML file contents.
619 for(i=0;i<len;i++)
620 needle[i] = tolower(needle[i]);
621 InitSearch(info, needle);
622 return 0;
623 }else if(nmhdr->hwndFrom == info->search.hwndList) {
624 HWND hwndList = info->search.hwndList;
625 LVITEMW lvItem;
627 lvItem.iItem = (int) SendMessageW(hwndList, LVM_GETSELECTIONMARK, 0, 0);
628 lvItem.mask = TVIF_PARAM;
629 SendMessageW(hwndList, LVM_GETITEMW, 0, (LPARAM)&lvItem);
630 OnTopicChange(info, (void*) lvItem.lParam);
631 return 0;
633 break;
636 break;
638 break;
640 default:
641 return DefWindowProcW(hWnd, message, wParam, lParam);
644 return 0;
647 static void HH_RegisterChildWndClass(HHInfo *pHHInfo)
649 WNDCLASSEXW wcex;
651 wcex.cbSize = sizeof(WNDCLASSEXW);
652 wcex.style = 0;
653 wcex.lpfnWndProc = Child_WndProc;
654 wcex.cbClsExtra = 0;
655 wcex.cbWndExtra = 0;
656 wcex.hInstance = hhctrl_hinstance;
657 wcex.hIcon = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
658 wcex.hCursor = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
659 wcex.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
660 wcex.lpszMenuName = NULL;
661 wcex.lpszClassName = szChildClass;
662 wcex.hIconSm = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
664 RegisterClassExW(&wcex);
667 /* Toolbar */
669 #define ICON_SIZE 20
671 static void DisplayPopupMenu(HHInfo *info)
673 HMENU menu, submenu;
674 TBBUTTONINFOW button;
675 MENUITEMINFOW item;
676 POINT coords;
677 RECT rect;
678 DWORD index;
680 menu = LoadMenuW(hhctrl_hinstance, MAKEINTRESOURCEW(MENU_POPUP));
682 if (!menu)
683 return;
685 submenu = GetSubMenu(menu, 0);
687 /* Update the Show/Hide menu item */
688 item.cbSize = sizeof(MENUITEMINFOW);
689 item.fMask = MIIM_FTYPE | MIIM_STATE | MIIM_STRING;
690 item.fType = MFT_STRING;
691 item.fState = MF_ENABLED;
693 if (info->WinType.fNotExpanded)
694 item.dwTypeData = HH_LoadString(IDS_SHOWTABS);
695 else
696 item.dwTypeData = HH_LoadString(IDS_HIDETABS);
698 SetMenuItemInfoW(submenu, IDTB_EXPAND, FALSE, &item);
699 heap_free(item.dwTypeData);
701 /* Find the index toolbar button */
702 button.cbSize = sizeof(TBBUTTONINFOW);
703 button.dwMask = TBIF_COMMAND;
704 index = SendMessageW(info->WinType.hwndToolBar, TB_GETBUTTONINFOW, IDTB_OPTIONS, (LPARAM) &button);
706 if (index == -1)
707 return;
709 /* Get position */
710 SendMessageW(info->WinType.hwndToolBar, TB_GETITEMRECT, index, (LPARAM) &rect);
712 coords.x = rect.left;
713 coords.y = rect.bottom;
715 ClientToScreen(info->WinType.hwndToolBar, &coords);
716 TrackPopupMenu(submenu, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_LEFTBUTTON | TPM_NOANIMATION, coords.x, coords.y, 0, info->WinType.hwndHelp, NULL);
719 static void TB_OnClick(HWND hWnd, DWORD dwID)
721 HHInfo *info = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
723 switch (dwID)
725 case IDTB_STOP:
726 DoPageAction(info, WB_STOP);
727 break;
728 case IDTB_REFRESH:
729 DoPageAction(info, WB_REFRESH);
730 break;
731 case IDTB_BACK:
732 DoPageAction(info, WB_GOBACK);
733 break;
734 case IDTB_HOME:
735 NavigateToChm(info, info->pCHMInfo->szFile, info->WinType.pszHome);
736 break;
737 case IDTB_FORWARD:
738 DoPageAction(info, WB_GOFORWARD);
739 break;
740 case IDTB_PRINT:
741 DoPageAction(info, WB_PRINT);
742 break;
743 case IDTB_EXPAND:
744 case IDTB_CONTRACT:
745 ExpandContract(info);
746 break;
747 case IDTB_SYNC:
748 DoSync(info);
749 break;
750 case IDTB_OPTIONS:
751 DisplayPopupMenu(info);
752 break;
753 case IDTB_NOTES:
754 case IDTB_CONTENTS:
755 case IDTB_INDEX:
756 case IDTB_SEARCH:
757 case IDTB_HISTORY:
758 case IDTB_FAVORITES:
759 /* These are officially unimplemented as of the Windows 7 SDK */
760 break;
761 case IDTB_BROWSE_FWD:
762 case IDTB_BROWSE_BACK:
763 case IDTB_JUMP1:
764 case IDTB_JUMP2:
765 case IDTB_CUSTOMIZE:
766 case IDTB_ZOOM:
767 case IDTB_TOC_NEXT:
768 case IDTB_TOC_PREV:
769 break;
773 static void TB_AddButton(TBBUTTON *pButtons, DWORD dwIndex, DWORD dwID, DWORD dwBitmap)
775 pButtons[dwIndex].iBitmap = dwBitmap;
776 pButtons[dwIndex].idCommand = dwID;
777 pButtons[dwIndex].fsState = TBSTATE_ENABLED;
778 pButtons[dwIndex].fsStyle = BTNS_BUTTON;
779 pButtons[dwIndex].dwData = 0;
780 pButtons[dwIndex].iString = 0;
783 static void TB_AddButtonsFromFlags(HHInfo *pHHInfo, TBBUTTON *pButtons, DWORD dwButtonFlags, LPDWORD pdwNumButtons)
785 int nHistBitmaps = 0, nStdBitmaps = 0, nHHBitmaps = 0;
786 HWND hToolbar = pHHInfo->WinType.hwndToolBar;
787 TBADDBITMAP tbAB;
788 DWORD unsupported;
790 /* Common bitmaps */
791 tbAB.hInst = HINST_COMMCTRL;
792 tbAB.nID = IDB_HIST_LARGE_COLOR;
793 nHistBitmaps = SendMessageW(hToolbar, TB_ADDBITMAP, 0, (LPARAM)&tbAB);
794 tbAB.nID = IDB_STD_LARGE_COLOR;
795 nStdBitmaps = SendMessageW(hToolbar, TB_ADDBITMAP, 0, (LPARAM)&tbAB);
796 /* hhctrl.ocx bitmaps */
797 tbAB.hInst = hhctrl_hinstance;
798 tbAB.nID = IDB_HHTOOLBAR;
799 nHHBitmaps = SendMessageW(hToolbar, TB_ADDBITMAP, HHTB_NUMBITMAPS, (LPARAM)&tbAB);
801 *pdwNumButtons = 0;
803 unsupported = dwButtonFlags & (HHWIN_BUTTON_BROWSE_FWD |
804 HHWIN_BUTTON_BROWSE_BCK | HHWIN_BUTTON_NOTES | HHWIN_BUTTON_CONTENTS |
805 HHWIN_BUTTON_INDEX | HHWIN_BUTTON_SEARCH | HHWIN_BUTTON_HISTORY |
806 HHWIN_BUTTON_FAVORITES | HHWIN_BUTTON_JUMP1 | HHWIN_BUTTON_JUMP2 |
807 HHWIN_BUTTON_ZOOM | HHWIN_BUTTON_TOC_NEXT | HHWIN_BUTTON_TOC_PREV);
808 if (unsupported)
809 FIXME("got asked for unsupported buttons: %06x\n", unsupported);
811 if (dwButtonFlags & HHWIN_BUTTON_EXPAND)
813 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_EXPAND, nHHBitmaps + HHTB_EXPAND);
814 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_CONTRACT, nHHBitmaps + HHTB_CONTRACT);
816 if (pHHInfo->WinType.fNotExpanded)
817 pButtons[1].fsState |= TBSTATE_HIDDEN;
818 else
819 pButtons[0].fsState |= TBSTATE_HIDDEN;
822 if (dwButtonFlags & HHWIN_BUTTON_BACK)
823 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_BACK, nHistBitmaps + HIST_BACK);
825 if (dwButtonFlags & HHWIN_BUTTON_FORWARD)
826 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_FORWARD, nHistBitmaps + HIST_FORWARD);
828 if (dwButtonFlags & HHWIN_BUTTON_STOP)
829 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_STOP, nHHBitmaps + HHTB_STOP);
831 if (dwButtonFlags & HHWIN_BUTTON_REFRESH)
832 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_REFRESH, nHHBitmaps + HHTB_REFRESH);
834 if (dwButtonFlags & HHWIN_BUTTON_HOME)
835 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_HOME, nHHBitmaps + HHTB_HOME);
837 if (dwButtonFlags & HHWIN_BUTTON_SYNC)
838 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_SYNC, nHHBitmaps + HHTB_SYNC);
840 if (dwButtonFlags & HHWIN_BUTTON_OPTIONS)
841 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_OPTIONS, nStdBitmaps + STD_PROPERTIES);
843 if (dwButtonFlags & HHWIN_BUTTON_PRINT)
844 TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_PRINT, nStdBitmaps + STD_PRINT);
847 static BOOL HH_AddToolbar(HHInfo *pHHInfo)
849 HWND hToolbar;
850 HWND hwndParent = pHHInfo->WinType.hwndHelp;
851 DWORD toolbarFlags;
852 TBBUTTON buttons[IDTB_TOC_PREV - IDTB_EXPAND];
853 DWORD dwStyles, dwExStyles;
854 DWORD dwNumButtons, dwIndex;
856 if (pHHInfo->WinType.fsWinProperties & HHWIN_PARAM_TB_FLAGS)
857 toolbarFlags = pHHInfo->WinType.fsToolBarFlags;
858 else
859 toolbarFlags = HHWIN_DEF_BUTTONS;
861 dwStyles = WS_CHILDWINDOW | WS_VISIBLE | TBSTYLE_FLAT |
862 TBSTYLE_WRAPABLE | TBSTYLE_TOOLTIPS | CCS_NODIVIDER;
863 dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
865 hToolbar = CreateWindowExW(dwExStyles, TOOLBARCLASSNAMEW, NULL, dwStyles,
866 0, 0, 0, 0, hwndParent, NULL,
867 hhctrl_hinstance, NULL);
868 if (!hToolbar)
869 return FALSE;
870 pHHInfo->WinType.hwndToolBar = hToolbar;
872 SendMessageW(hToolbar, TB_SETBITMAPSIZE, 0, MAKELONG(ICON_SIZE, ICON_SIZE));
873 SendMessageW(hToolbar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
874 SendMessageW(hToolbar, WM_SETFONT, (WPARAM)pHHInfo->hFont, TRUE);
876 TB_AddButtonsFromFlags(pHHInfo, buttons, toolbarFlags, &dwNumButtons);
878 for (dwIndex = 0; dwIndex < dwNumButtons; dwIndex++)
880 LPWSTR szBuf = HH_LoadString(buttons[dwIndex].idCommand);
881 DWORD dwLen = strlenW(szBuf);
882 szBuf[dwLen + 1] = 0; /* Double-null terminate */
884 buttons[dwIndex].iString = (DWORD)SendMessageW(hToolbar, TB_ADDSTRINGW, 0, (LPARAM)szBuf);
885 heap_free(szBuf);
888 SendMessageW(hToolbar, TB_ADDBUTTONSW, dwNumButtons, (LPARAM)buttons);
889 SendMessageW(hToolbar, TB_AUTOSIZE, 0, 0);
890 ShowWindow(hToolbar, SW_SHOW);
892 return TRUE;
895 /* Navigation Pane */
897 static void NP_GetNavigationRect(HHInfo *pHHInfo, RECT *rc)
899 HWND hwndParent = pHHInfo->WinType.hwndHelp;
900 HWND hwndToolbar = pHHInfo->WinType.hwndToolBar;
901 RECT rectWND, rectTB;
903 GetClientRect(hwndParent, &rectWND);
904 GetClientRect(hwndToolbar, &rectTB);
906 rc->left = 0;
907 rc->top = rectTB.bottom;
908 rc->bottom = rectWND.bottom - rectTB.bottom;
910 if (!(pHHInfo->WinType.fsValidMembers & HHWIN_PARAM_NAV_WIDTH) &&
911 pHHInfo->WinType.iNavWidth == 0)
913 pHHInfo->WinType.iNavWidth = WINTYPE_DEFAULT_NAVWIDTH;
916 rc->right = pHHInfo->WinType.iNavWidth;
919 static DWORD NP_CreateTab(HINSTANCE hInstance, HWND hwndTabCtrl, DWORD index)
921 TCITEMW tie;
922 LPWSTR tabText = HH_LoadString(index);
923 DWORD ret;
925 tie.mask = TCIF_TEXT;
926 tie.pszText = tabText;
928 ret = SendMessageW( hwndTabCtrl, TCM_INSERTITEMW, index, (LPARAM)&tie );
930 heap_free(tabText);
931 return ret;
934 static BOOL HH_AddNavigationPane(HHInfo *info)
936 HWND hWnd, hwndTabCtrl;
937 HWND hwndParent = info->WinType.hwndHelp;
938 DWORD dwStyles = WS_CHILDWINDOW;
939 DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
940 RECT rc;
942 if (!info->WinType.fNotExpanded)
943 dwStyles |= WS_VISIBLE;
945 NP_GetNavigationRect(info, &rc);
947 hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
948 rc.left, rc.top, rc.right, rc.bottom,
949 hwndParent, NULL, hhctrl_hinstance, NULL);
950 if (!hWnd)
951 return FALSE;
953 SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)info);
955 hwndTabCtrl = CreateWindowExW(dwExStyles, WC_TABCONTROLW, szEmpty, dwStyles | WS_VISIBLE,
956 0, TAB_TOP_PADDING,
957 rc.right - TAB_RIGHT_PADDING,
958 rc.bottom - TAB_TOP_PADDING,
959 hWnd, NULL, hhctrl_hinstance, NULL);
960 if (!hwndTabCtrl)
961 return FALSE;
963 if (*info->WinType.pszToc)
964 info->tabs[TAB_CONTENTS].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_CONTENTS);
966 if (*info->WinType.pszIndex)
967 info->tabs[TAB_INDEX].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_INDEX);
969 if (info->WinType.fsWinProperties & HHWIN_PROP_TAB_SEARCH)
970 info->tabs[TAB_SEARCH].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_SEARCH);
972 if (info->WinType.fsWinProperties & HHWIN_PROP_TAB_FAVORITES)
973 info->tabs[TAB_FAVORITES].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_FAVORITES);
975 SendMessageW(hwndTabCtrl, WM_SETFONT, (WPARAM)info->hFont, TRUE);
977 info->hwndTabCtrl = hwndTabCtrl;
978 info->WinType.hwndNavigation = hWnd;
979 return TRUE;
982 /* HTML Pane */
984 static void HP_GetHTMLRect(HHInfo *info, RECT *rc)
986 RECT rectTB, rectWND, rectNP, rectSB;
988 GetClientRect(info->WinType.hwndHelp, &rectWND);
989 GetClientRect(info->WinType.hwndToolBar, &rectTB);
990 GetClientRect(info->hwndSizeBar, &rectSB);
992 if (info->WinType.fNotExpanded)
993 rc->left = 0;
994 else
996 GetClientRect(info->WinType.hwndNavigation, &rectNP);
997 rc->left = rectNP.right + rectSB.right;
1000 rc->top = rectTB.bottom;
1001 rc->right = rectWND.right - rc->left;
1002 rc->bottom = rectWND.bottom - rectTB.bottom;
1005 static BOOL HH_AddHTMLPane(HHInfo *pHHInfo)
1007 HWND hWnd;
1008 HWND hwndParent = pHHInfo->WinType.hwndHelp;
1009 DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN;
1010 DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_CLIENTEDGE;
1011 RECT rc;
1013 HP_GetHTMLRect(pHHInfo, &rc);
1015 hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
1016 rc.left, rc.top, rc.right, rc.bottom,
1017 hwndParent, NULL, hhctrl_hinstance, NULL);
1018 if (!hWnd)
1019 return FALSE;
1021 if (!InitWebBrowser(pHHInfo, hWnd))
1022 return FALSE;
1024 /* store the pointer to the HH info struct */
1025 SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
1027 ShowWindow(hWnd, SW_SHOW);
1028 UpdateWindow(hWnd);
1030 pHHInfo->WinType.hwndHTML = hWnd;
1031 return TRUE;
1034 static BOOL AddContentTab(HHInfo *info)
1036 if(info->tabs[TAB_CONTENTS].id == -1)
1037 return TRUE; /* No "Contents" tab */
1038 info->tabs[TAB_CONTENTS].hwnd = CreateWindowExW(WS_EX_CLIENTEDGE, WC_TREEVIEWW,
1039 szEmpty, WS_CHILD | WS_BORDER | 0x25, 50, 50, 100, 100,
1040 info->WinType.hwndNavigation, NULL, hhctrl_hinstance, NULL);
1041 if(!info->tabs[TAB_CONTENTS].hwnd) {
1042 ERR("Could not create treeview control\n");
1043 return FALSE;
1046 ResizeTabChild(info, TAB_CONTENTS);
1047 ShowWindow(info->tabs[TAB_CONTENTS].hwnd, SW_SHOW);
1049 return TRUE;
1052 static BOOL AddIndexTab(HHInfo *info)
1054 char hidden_column[] = "Column";
1055 LVCOLUMNA lvc;
1057 if(info->tabs[TAB_INDEX].id == -1)
1058 return TRUE; /* No "Index" tab */
1059 info->tabs[TAB_INDEX].hwnd = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW,
1060 szEmpty, WS_CHILD | WS_BORDER | LVS_SINGLESEL | LVS_REPORT | LVS_NOCOLUMNHEADER, 50, 50, 100, 100,
1061 info->WinType.hwndNavigation, NULL, hhctrl_hinstance, NULL);
1062 if(!info->tabs[TAB_INDEX].hwnd) {
1063 ERR("Could not create ListView control\n");
1064 return FALSE;
1066 memset(&lvc, 0, sizeof(lvc));
1067 lvc.mask = LVCF_TEXT;
1068 lvc.pszText = hidden_column;
1069 if(SendMessageW(info->tabs[TAB_INDEX].hwnd, LVM_INSERTCOLUMNA, 0, (LPARAM) &lvc) == -1)
1071 ERR("Could not create ListView column\n");
1072 return FALSE;
1075 ResizeTabChild(info, TAB_INDEX);
1076 ShowWindow(info->tabs[TAB_INDEX].hwnd, SW_HIDE);
1078 return TRUE;
1081 static BOOL AddSearchTab(HHInfo *info)
1083 HWND hwndList, hwndEdit, hwndContainer;
1084 char hidden_column[] = "Column";
1085 WNDPROC editWndProc;
1086 LVCOLUMNA lvc;
1088 if(info->tabs[TAB_SEARCH].id == -1)
1089 return TRUE; /* No "Search" tab */
1090 hwndContainer = CreateWindowExW(WS_EX_CONTROLPARENT, szChildClass, szEmpty,
1091 WS_CHILD, 0, 0, 0, 0, info->WinType.hwndNavigation,
1092 NULL, hhctrl_hinstance, NULL);
1093 if(!hwndContainer) {
1094 ERR("Could not create search window container control.\n");
1095 return FALSE;
1097 hwndEdit = CreateWindowExW(WS_EX_CLIENTEDGE, WC_EDITW, szEmpty, WS_CHILD
1098 | WS_VISIBLE | ES_LEFT | SS_NOTIFY, 0, 0, 0, 0,
1099 hwndContainer, NULL, hhctrl_hinstance, NULL);
1100 if(!hwndEdit) {
1101 ERR("Could not create search ListView control.\n");
1102 return FALSE;
1104 if(SendMessageW(hwndEdit, WM_SETFONT, (WPARAM) info->hFont, (LPARAM) FALSE) == -1)
1106 ERR("Could not set font for edit control.\n");
1107 return FALSE;
1109 editWndProc = (WNDPROC) SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC, (LONG_PTR)EditChild_WndProc);
1110 if(!editWndProc) {
1111 ERR("Could not redirect messages for edit control.\n");
1112 return FALSE;
1114 SetWindowLongPtrW(hwndEdit, GWLP_USERDATA, (LONG_PTR)editWndProc);
1115 hwndList = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW, szEmpty,
1116 WS_CHILD | WS_VISIBLE | WS_BORDER | LVS_SINGLESEL
1117 | LVS_REPORT | LVS_NOCOLUMNHEADER, 0, 0, 0, 0,
1118 hwndContainer, NULL, hhctrl_hinstance, NULL);
1119 if(!hwndList) {
1120 ERR("Could not create search ListView control.\n");
1121 return FALSE;
1123 memset(&lvc, 0, sizeof(lvc));
1124 lvc.mask = LVCF_TEXT;
1125 lvc.pszText = hidden_column;
1126 if(SendMessageW(hwndList, LVM_INSERTCOLUMNA, 0, (LPARAM) &lvc) == -1)
1128 ERR("Could not create ListView column\n");
1129 return FALSE;
1132 info->search.hwndEdit = hwndEdit;
1133 info->search.hwndList = hwndList;
1134 info->search.hwndContainer = hwndContainer;
1135 info->tabs[TAB_SEARCH].hwnd = hwndContainer;
1137 SetWindowLongPtrW(hwndContainer, GWLP_USERDATA, (LONG_PTR)info);
1139 ResizeTabChild(info, TAB_SEARCH);
1141 return TRUE;
1144 /* The Index tab's sub-topic popup */
1146 static void ResizePopupChild(HHInfo *info)
1148 int scroll_width = GetSystemMetrics(SM_CXVSCROLL);
1149 int border_width = GetSystemMetrics(SM_CXBORDER);
1150 int edge_width = GetSystemMetrics(SM_CXEDGE);
1151 INT width, height;
1152 RECT rect;
1154 if(!info)
1155 return;
1157 GetClientRect(info->popup.hwndPopup, &rect);
1158 SetWindowPos(info->popup.hwndCallback, HWND_TOP, 0, 0,
1159 rect.right, rect.bottom, SWP_NOMOVE);
1161 rect.left = TAB_MARGIN;
1162 rect.top = TAB_TOP_PADDING + TAB_MARGIN;
1163 rect.right -= TAB_RIGHT_PADDING + TAB_MARGIN;
1164 rect.bottom -= TAB_MARGIN;
1165 width = rect.right-rect.left;
1166 height = rect.bottom-rect.top;
1168 SetWindowPos(info->popup.hwndList, NULL, rect.left, rect.top, width, height,
1169 SWP_NOZORDER | SWP_NOACTIVATE);
1171 SendMessageW(info->popup.hwndList, LVM_SETCOLUMNWIDTH, 0,
1172 width-scroll_width-2*border_width-2*edge_width);
1175 static LRESULT CALLBACK HelpPopup_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1177 HHInfo *info = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
1179 switch (message)
1181 case WM_SIZE:
1182 ResizePopupChild(info);
1183 return 0;
1184 case WM_DESTROY:
1185 DestroyWindow(hWnd);
1186 return 0;
1187 case WM_CLOSE:
1188 ShowWindow(hWnd, SW_HIDE);
1189 return 0;
1191 default:
1192 return DefWindowProcW(hWnd, message, wParam, lParam);
1195 return 0;
1198 static LRESULT CALLBACK PopupChild_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1200 switch (message)
1202 case WM_NOTIFY: {
1203 NMHDR *nmhdr = (NMHDR*)lParam;
1204 switch(nmhdr->code)
1206 case NM_DBLCLK: {
1207 HHInfo *info = (HHInfo*)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
1208 IndexSubItem *iter;
1210 if(info == 0 || lParam == 0)
1211 return 0;
1212 iter = (IndexSubItem*) ((NMITEMACTIVATE *)lParam)->lParam;
1213 if(iter == 0)
1214 return 0;
1215 NavigateToChm(info, info->index->merge.chm_file, iter->local);
1216 ShowWindow(info->popup.hwndPopup, SW_HIDE);
1217 return 0;
1219 case NM_RETURN: {
1220 HHInfo *info = (HHInfo*)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
1221 IndexSubItem *iter;
1222 LVITEMW lvItem;
1224 if(info == 0)
1225 return 0;
1227 lvItem.iItem = (int) SendMessageW(info->popup.hwndList, LVM_GETSELECTIONMARK, 0, 0);
1228 lvItem.mask = TVIF_PARAM;
1229 SendMessageW(info->popup.hwndList, LVM_GETITEMW, 0, (LPARAM)&lvItem);
1230 iter = (IndexSubItem*) lvItem.lParam;
1231 NavigateToChm(info, info->index->merge.chm_file, iter->local);
1232 ShowWindow(info->popup.hwndPopup, SW_HIDE);
1233 return 0;
1236 break;
1238 default:
1239 return DefWindowProcW(hWnd, message, wParam, lParam);
1242 return 0;
1245 static BOOL AddIndexPopup(HHInfo *info)
1247 static const WCHAR szPopupChildClass[] = {'H','H',' ','P','o','p','u','p',' ','C','h','i','l','d',0};
1248 static const WCHAR windowCaptionW[] = {'S','e','l','e','c','t',' ','T','o','p','i','c',':',0};
1249 static const WCHAR windowClassW[] = {'H','H',' ','P','o','p','u','p',0};
1250 HWND hwndList, hwndPopup, hwndCallback;
1251 char hidden_column[] = "Column";
1252 WNDCLASSEXW wcex;
1253 LVCOLUMNA lvc;
1255 if(info->tabs[TAB_INDEX].id == -1)
1256 return TRUE; /* No "Index" tab */
1258 wcex.cbSize = sizeof(WNDCLASSEXW);
1259 wcex.style = CS_HREDRAW | CS_VREDRAW;
1260 wcex.lpfnWndProc = HelpPopup_WndProc;
1261 wcex.cbClsExtra = 0;
1262 wcex.cbWndExtra = 0;
1263 wcex.hInstance = hhctrl_hinstance;
1264 wcex.hIcon = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1265 wcex.hCursor = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
1266 wcex.hbrBackground = (HBRUSH)(COLOR_MENU + 1);
1267 wcex.lpszMenuName = NULL;
1268 wcex.lpszClassName = windowClassW;
1269 wcex.hIconSm = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1270 RegisterClassExW(&wcex);
1272 wcex.cbSize = sizeof(WNDCLASSEXW);
1273 wcex.style = 0;
1274 wcex.lpfnWndProc = PopupChild_WndProc;
1275 wcex.cbClsExtra = 0;
1276 wcex.cbWndExtra = 0;
1277 wcex.hInstance = hhctrl_hinstance;
1278 wcex.hIcon = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1279 wcex.hCursor = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
1280 wcex.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
1281 wcex.lpszMenuName = NULL;
1282 wcex.lpszClassName = szPopupChildClass;
1283 wcex.hIconSm = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1284 RegisterClassExW(&wcex);
1286 hwndPopup = CreateWindowExW(WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_APPWINDOW
1287 | WS_EX_WINDOWEDGE | WS_EX_RIGHTSCROLLBAR,
1288 windowClassW, windowCaptionW, WS_POPUPWINDOW
1289 | WS_OVERLAPPEDWINDOW | WS_VISIBLE
1290 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, CW_USEDEFAULT,
1291 CW_USEDEFAULT, 300, 200, info->WinType.hwndHelp,
1292 NULL, hhctrl_hinstance, NULL);
1293 if (!hwndPopup)
1294 return FALSE;
1296 hwndCallback = CreateWindowExW(WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR,
1297 szPopupChildClass, szEmpty, WS_CHILDWINDOW | WS_VISIBLE,
1298 0, 0, 0, 0,
1299 hwndPopup, NULL, hhctrl_hinstance, NULL);
1300 if (!hwndCallback)
1301 return FALSE;
1303 ShowWindow(hwndPopup, SW_HIDE);
1304 hwndList = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW, szEmpty,
1305 WS_CHILD | WS_BORDER | LVS_SINGLESEL | LVS_REPORT
1306 | LVS_NOCOLUMNHEADER, 50, 50, 100, 100,
1307 hwndCallback, NULL, hhctrl_hinstance, NULL);
1308 if(!hwndList) {
1309 ERR("Could not create popup ListView control\n");
1310 return FALSE;
1312 memset(&lvc, 0, sizeof(lvc));
1313 lvc.mask = LVCF_TEXT;
1314 lvc.pszText = hidden_column;
1315 if(SendMessageW(hwndList, LVM_INSERTCOLUMNA, 0, (LPARAM) &lvc) == -1)
1317 ERR("Could not create popup ListView column\n");
1318 return FALSE;
1321 info->popup.hwndCallback = hwndCallback;
1322 info->popup.hwndPopup = hwndPopup;
1323 info->popup.hwndList = hwndList;
1324 SetWindowLongPtrW(hwndPopup, GWLP_USERDATA, (LONG_PTR)info);
1325 SetWindowLongPtrW(hwndCallback, GWLP_USERDATA, (LONG_PTR)info);
1327 ResizePopupChild(info);
1328 ShowWindow(hwndList, SW_SHOW);
1330 return TRUE;
1333 /* Viewer Window */
1335 static void ExpandContract(HHInfo *pHHInfo)
1337 RECT r, nav;
1339 pHHInfo->WinType.fNotExpanded = !pHHInfo->WinType.fNotExpanded;
1340 GetWindowRect(pHHInfo->WinType.hwndHelp, &r);
1341 NP_GetNavigationRect(pHHInfo, &nav);
1343 /* hide/show both the nav bar and the size bar */
1344 if (pHHInfo->WinType.fNotExpanded)
1346 ShowWindow(pHHInfo->WinType.hwndNavigation, SW_HIDE);
1347 ShowWindow(pHHInfo->hwndSizeBar, SW_HIDE);
1348 r.left = r.left + nav.right;
1350 SendMessageW(pHHInfo->WinType.hwndToolBar, TB_HIDEBUTTON, IDTB_EXPAND, MAKELPARAM(FALSE, 0));
1351 SendMessageW(pHHInfo->WinType.hwndToolBar, TB_HIDEBUTTON, IDTB_CONTRACT, MAKELPARAM(TRUE, 0));
1353 else
1355 ShowWindow(pHHInfo->WinType.hwndNavigation, SW_SHOW);
1356 ShowWindow(pHHInfo->hwndSizeBar, SW_SHOW);
1357 r.left = r.left - nav.right;
1359 SendMessageW(pHHInfo->WinType.hwndToolBar, TB_HIDEBUTTON, IDTB_EXPAND, MAKELPARAM(TRUE, 0));
1360 SendMessageW(pHHInfo->WinType.hwndToolBar, TB_HIDEBUTTON, IDTB_CONTRACT, MAKELPARAM(FALSE, 0));
1363 MoveWindow(pHHInfo->WinType.hwndHelp, r.left, r.top, r.right-r.left, r.bottom-r.top, TRUE);
1366 static LRESULT Help_OnSize(HWND hWnd)
1368 HHInfo *pHHInfo = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
1369 DWORD dwSize;
1370 RECT rc;
1372 if (!pHHInfo)
1373 return 0;
1375 if (!pHHInfo->WinType.fNotExpanded)
1377 NP_GetNavigationRect(pHHInfo, &rc);
1378 SetWindowPos(pHHInfo->WinType.hwndNavigation, HWND_TOP, 0, 0,
1379 rc.right, rc.bottom, SWP_NOMOVE);
1381 SB_GetSizeBarRect(pHHInfo, &rc);
1382 SetWindowPos(pHHInfo->hwndSizeBar, HWND_TOP, rc.left, rc.top,
1383 rc.right, rc.bottom, SWP_SHOWWINDOW);
1387 HP_GetHTMLRect(pHHInfo, &rc);
1388 SetWindowPos(pHHInfo->WinType.hwndHTML, HWND_TOP, rc.left, rc.top,
1389 rc.right, rc.bottom, SWP_SHOWWINDOW);
1391 /* Resize browser window taking the frame size into account */
1392 dwSize = GetSystemMetrics(SM_CXFRAME);
1393 ResizeWebBrowser(pHHInfo, rc.right - dwSize, rc.bottom - dwSize);
1395 return 0;
1398 static LRESULT CALLBACK Help_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1400 switch (message)
1402 case WM_COMMAND:
1403 if (HIWORD(wParam) == BN_CLICKED)
1404 TB_OnClick(hWnd, LOWORD(wParam));
1405 break;
1406 case WM_SIZE:
1407 return Help_OnSize(hWnd);
1408 case WM_CLOSE:
1409 ReleaseHelpViewer((HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA));
1410 return 0;
1411 case WM_DESTROY:
1412 if(hh_process)
1413 PostQuitMessage(0);
1414 break;
1416 default:
1417 return DefWindowProcW(hWnd, message, wParam, lParam);
1420 return 0;
1423 static BOOL HH_CreateHelpWindow(HHInfo *info)
1425 HWND hWnd;
1426 RECT winPos = info->WinType.rcWindowPos;
1427 WNDCLASSEXW wcex;
1428 DWORD dwStyles, dwExStyles;
1429 DWORD x, y, width = 0, height = 0;
1430 LPCWSTR caption;
1432 static const WCHAR windowClassW[] = {
1433 'H','H',' ', 'P','a','r','e','n','t',0
1436 wcex.cbSize = sizeof(WNDCLASSEXW);
1437 wcex.style = CS_HREDRAW | CS_VREDRAW;
1438 wcex.lpfnWndProc = Help_WndProc;
1439 wcex.cbClsExtra = 0;
1440 wcex.cbWndExtra = 0;
1441 wcex.hInstance = hhctrl_hinstance;
1442 wcex.hIcon = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1443 wcex.hCursor = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
1444 wcex.hbrBackground = (HBRUSH)(COLOR_MENU + 1);
1445 wcex.lpszMenuName = NULL;
1446 wcex.lpszClassName = windowClassW;
1447 wcex.hIconSm = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1449 RegisterClassExW(&wcex);
1451 /* Read in window parameters if available */
1452 if (info->WinType.fsValidMembers & HHWIN_PARAM_STYLES)
1453 dwStyles = info->WinType.dwStyles | WS_OVERLAPPEDWINDOW;
1454 else
1455 dwStyles = WS_OVERLAPPEDWINDOW | WS_VISIBLE |
1456 WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
1458 if (info->WinType.fsValidMembers & HHWIN_PARAM_EXSTYLES)
1459 dwExStyles = info->WinType.dwExStyles;
1460 else
1461 dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_APPWINDOW |
1462 WS_EX_WINDOWEDGE | WS_EX_RIGHTSCROLLBAR;
1464 if (info->WinType.fsValidMembers & HHWIN_PARAM_RECT)
1466 x = winPos.left;
1467 y = winPos.top;
1468 width = winPos.right - x;
1469 height = winPos.bottom - y;
1471 if (!width || !height)
1473 x = WINTYPE_DEFAULT_X;
1474 y = WINTYPE_DEFAULT_Y;
1475 width = WINTYPE_DEFAULT_WIDTH;
1476 height = WINTYPE_DEFAULT_HEIGHT;
1479 if (info->WinType.fNotExpanded)
1481 if (!(info->WinType.fsValidMembers & HHWIN_PARAM_NAV_WIDTH) &&
1482 info->WinType.iNavWidth == 0)
1484 info->WinType.iNavWidth = WINTYPE_DEFAULT_NAVWIDTH;
1487 x += info->WinType.iNavWidth;
1488 width -= info->WinType.iNavWidth;
1492 caption = info->WinType.pszCaption;
1493 if (!*caption) caption = info->pCHMInfo->defTitle;
1495 hWnd = CreateWindowExW(dwExStyles, windowClassW, caption,
1496 dwStyles, x, y, width, height, NULL, NULL, hhctrl_hinstance, NULL);
1497 if (!hWnd)
1498 return FALSE;
1500 ShowWindow(hWnd, SW_SHOW);
1501 UpdateWindow(hWnd);
1503 /* store the pointer to the HH info struct */
1504 SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)info);
1506 info->WinType.hwndHelp = hWnd;
1507 return TRUE;
1510 static void HH_CreateFont(HHInfo *pHHInfo)
1512 LOGFONTW lf;
1514 GetObjectW(GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONTW), &lf);
1515 lf.lfWeight = FW_NORMAL;
1516 lf.lfItalic = FALSE;
1517 lf.lfUnderline = FALSE;
1519 pHHInfo->hFont = CreateFontIndirectW(&lf);
1522 static void HH_InitRequiredControls(DWORD dwControls)
1524 INITCOMMONCONTROLSEX icex;
1526 icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
1527 icex.dwICC = dwControls;
1528 InitCommonControlsEx(&icex);
1531 /* Creates the whole package */
1532 static BOOL CreateViewer(HHInfo *pHHInfo)
1534 HH_CreateFont(pHHInfo);
1536 if (!HH_CreateHelpWindow(pHHInfo))
1537 return FALSE;
1539 HH_InitRequiredControls(ICC_BAR_CLASSES);
1541 if (!HH_AddToolbar(pHHInfo))
1542 return FALSE;
1544 HH_RegisterChildWndClass(pHHInfo);
1546 if (!HH_AddNavigationPane(pHHInfo))
1547 return FALSE;
1549 HH_RegisterSizeBarClass(pHHInfo);
1551 if (!HH_AddSizeBar(pHHInfo))
1552 return FALSE;
1554 if (!HH_AddHTMLPane(pHHInfo))
1555 return FALSE;
1557 if (!AddContentTab(pHHInfo))
1558 return FALSE;
1560 if (!AddIndexTab(pHHInfo))
1561 return FALSE;
1563 if (!AddIndexPopup(pHHInfo))
1564 return FALSE;
1566 if (!AddSearchTab(pHHInfo))
1567 return FALSE;
1569 InitContent(pHHInfo);
1570 InitIndex(pHHInfo);
1572 return TRUE;
1575 void ReleaseHelpViewer(HHInfo *info)
1577 TRACE("(%p)\n", info);
1579 if (!info)
1580 return;
1582 /* Free allocated strings */
1583 heap_free(info->pszType);
1584 heap_free(info->pszCaption);
1585 heap_free(info->pszToc);
1586 heap_free(info->pszIndex);
1587 heap_free(info->pszFile);
1588 heap_free(info->pszHome);
1589 heap_free(info->pszJump1);
1590 heap_free(info->pszJump2);
1591 heap_free(info->pszUrlJump1);
1592 heap_free(info->pszUrlJump2);
1594 if (info->pCHMInfo)
1595 CloseCHM(info->pCHMInfo);
1597 ReleaseWebBrowser(info);
1598 ReleaseContent(info);
1599 ReleaseIndex(info);
1600 ReleaseSearch(info);
1602 if(info->WinType.hwndHelp)
1603 DestroyWindow(info->WinType.hwndHelp);
1605 heap_free(info);
1606 OleUninitialize();
1609 HHInfo *CreateHelpViewer(LPCWSTR filename)
1611 HHInfo *info = heap_alloc_zero(sizeof(HHInfo));
1612 int i;
1614 /* Set the invalid tab ID (-1) as the default value for all
1615 * of the tabs, this matches a failed TCM_INSERTITEM call.
1617 for(i=0;i<sizeof(info->tabs)/sizeof(HHTab);i++)
1618 info->tabs[i].id = -1;
1620 OleInitialize(NULL);
1622 info->pCHMInfo = OpenCHM(filename);
1623 if(!info->pCHMInfo) {
1624 ReleaseHelpViewer(info);
1625 return NULL;
1628 if (!LoadWinTypeFromCHM(info)) {
1629 ReleaseHelpViewer(info);
1630 return NULL;
1633 if(!CreateViewer(info)) {
1634 ReleaseHelpViewer(info);
1635 return NULL;
1638 return info;