hhctrl.ocx: Mark internal symbols with hidden visibility.
[wine] / dlls / hhctrl.ocx / help.c
1 /*
2  * Help Viewer Implementation
3  *
4  * Copyright 2005 James Hawkins
5  * Copyright 2007 Jacek Caban for CodeWeavers
6  * Copyright 2011 Owen Rudge for CodeWeavers
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "hhctrl.h"
24
25 #include "wingdi.h"
26 #include "commctrl.h"
27 #include "wininet.h"
28
29 #include "wine/debug.h"
30
31 #include "resource.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(htmlhelp);
34
35 static LRESULT Help_OnSize(HWND hWnd);
36 static void ExpandContract(HHInfo *pHHInfo);
37
38 /* Window type defaults */
39
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
45
46 #define TAB_TOP_PADDING     8
47 #define TAB_RIGHT_PADDING   4
48 #define TAB_MARGIN  8
49 #define EDIT_HEIGHT         20
50
51 static const WCHAR szEmpty[] = {0};
52
53 /* Loads a string from the resource file */
54 static LPWSTR HH_LoadString(DWORD dwID)
55 {
56     LPWSTR string = NULL;
57     LPCWSTR stringresource;
58     int iSize;
59
60     iSize = LoadStringW(hhctrl_hinstance, dwID, (LPWSTR)&stringresource, 0);
61
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;
65
66     return string;
67 }
68
69 static HRESULT navigate_url(HHInfo *info, LPCWSTR surl)
70 {
71     VARIANT url;
72     HRESULT hres;
73
74     TRACE("%s\n", debugstr_w(surl));
75
76     V_VT(&url) = VT_BSTR;
77     V_BSTR(&url) = SysAllocString(surl);
78
79     hres = IWebBrowser2_Navigate2(info->web_browser, &url, 0, 0, 0, 0);
80
81     VariantClear(&url);
82
83     if(FAILED(hres))
84         TRACE("Navigation failed: %08x\n", hres);
85
86     return hres;
87 }
88
89 BOOL NavigateToUrl(HHInfo *info, LPCWSTR surl)
90 {
91     ChmPath chm_path;
92     BOOL ret;
93     HRESULT hres;
94
95     static const WCHAR url_indicator[] = {':', '/', '/', 0};
96
97     TRACE("%s\n", debugstr_w(surl));
98
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 */
104
105     SetChmPath(&chm_path, info->pCHMInfo->szFile, surl);
106     ret = NavigateToChm(info, chm_path.chm_file, chm_path.chm_index);
107
108     heap_free(chm_path.chm_file);
109     heap_free(chm_path.chm_index);
110
111     return ret;
112 }
113
114 static BOOL AppendFullPathURL(LPCWSTR file, LPWSTR buf, LPCWSTR index)
115 {
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];
121
122     TRACE("%s %p %s\n", debugstr_w(file), buf, debugstr_w(index));
123
124     if(!GetFullPathNameW(file, sizeof(full_path)/sizeof(full_path[0]), full_path, NULL)) {
125         WARN("GetFullPathName failed: %u\n", GetLastError());
126         return FALSE;
127     }
128
129     wsprintfW(buf, url_format, full_path, (!index || index[0] == '/') ? empty : slash, index);
130     return TRUE;
131 }
132
133 BOOL NavigateToChm(HHInfo *info, LPCWSTR file, LPCWSTR index)
134 {
135     WCHAR buf[INTERNET_MAX_URL_LENGTH];
136     LPWSTR ptr;
137
138     TRACE("%p %s %s\n", info, debugstr_w(file), debugstr_w(index));
139
140     if ((!info->web_browser) || !AppendFullPathURL(file, buf, index))
141         return FALSE;
142
143     /* FIXME: HACK */
144     if((ptr = strchrW(buf, '#')))
145        *ptr = 0;
146
147     return SUCCEEDED(navigate_url(info, buf));
148 }
149
150 static void DoSync(HHInfo *info)
151 {
152     WCHAR buf[INTERNET_MAX_URL_LENGTH];
153     HRESULT hres;
154     DWORD len;
155     BSTR url;
156
157     hres = IWebBrowser2_get_LocationURL(info->web_browser, &url);
158
159     if (FAILED(hres))
160     {
161         WARN("get_LocationURL failed: %08x\n", hres);
162         return;
163     }
164
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)))
167     {
168         SysFreeString(url);
169         return;
170     }
171
172     if (lstrcmpiW(buf, url) > 0)
173     {
174         static const WCHAR delimW[] = {':',':','/',0};
175         const WCHAR *index;
176
177         index = strstrW(url, delimW);
178
179         if (index)
180             ActivateContentTopic(info->tabs[TAB_CONTENTS].hwnd, index + 3, info->content); /* skip over ::/ */
181     }
182
183     SysFreeString(url);
184 }
185
186 /* Size Bar */
187
188 #define SIZEBAR_WIDTH   4
189
190 static const WCHAR szSizeBarClass[] = {
191     'H','H',' ','S','i','z','e','B','a','r',0
192 };
193
194 /* Draw the SizeBar */
195 static void SB_OnPaint(HWND hWnd)
196 {
197     PAINTSTRUCT ps;
198     HDC hdc;
199     RECT rc;
200     
201     hdc = BeginPaint(hWnd, &ps);
202
203     GetClientRect(hWnd, &rc);
204
205     /* dark frame */
206     rc.right += 1;
207     rc.bottom -= 1;
208     FrameRect(hdc, &rc, GetStockObject(GRAY_BRUSH));
209
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);
215
216     
217     MoveToEx(hdc, 0, rc.bottom, NULL);
218     LineTo(hdc, rc.right, rc.bottom);
219
220     EndPaint(hWnd, &ps);
221 }
222
223 static void SB_OnLButtonDown(HWND hWnd, WPARAM wParam, LPARAM lParam)
224 {
225     SetCapture(hWnd);
226 }
227
228 static void SB_OnLButtonUp(HWND hWnd, WPARAM wParam, LPARAM lParam)
229 {
230     HHInfo *pHHInfo = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
231     POINT pt;
232
233     pt.x = (short)LOWORD(lParam);
234     pt.y = (short)HIWORD(lParam);
235
236     /* update the window sizes */
237     pHHInfo->WinType.iNavWidth += pt.x;
238     Help_OnSize(hWnd);
239
240     ReleaseCapture();
241 }
242
243 static void SB_OnMouseMove(HWND hWnd, WPARAM wParam, LPARAM lParam)
244 {
245     /* ignore WM_MOUSEMOVE if not dragging the SizeBar */
246     if (!(wParam & MK_LBUTTON))
247         return;
248 }
249
250 static LRESULT CALLBACK SizeBar_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
251 {
252     switch (message)
253     {
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);
268     }
269
270     return 0;
271 }
272
273 static void HH_RegisterSizeBarClass(HHInfo *pHHInfo)
274 {
275     WNDCLASSEXW wcex;
276
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);
289
290     RegisterClassExW(&wcex);
291 }
292
293 static void SB_GetSizeBarRect(HHInfo *info, RECT *rc)
294 {
295     RECT rectWND, rectTB, rectNP;
296
297     GetClientRect(info->WinType.hwndHelp, &rectWND);
298     GetClientRect(info->WinType.hwndToolBar, &rectTB);
299     GetClientRect(info->WinType.hwndNavigation, &rectNP);
300
301     rc->left = rectNP.right;
302     rc->top = rectTB.bottom;
303     rc->bottom = rectWND.bottom - rectTB.bottom;
304     rc->right = SIZEBAR_WIDTH;
305 }
306
307 static BOOL HH_AddSizeBar(HHInfo *pHHInfo)
308 {
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;
314
315     if (!pHHInfo->WinType.fNotExpanded)
316         dwStyles |= WS_VISIBLE;
317
318     SB_GetSizeBarRect(pHHInfo, &rc);
319
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;
325
326     /* store the pointer to the HH info struct */
327     SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
328
329     pHHInfo->hwndSizeBar = hWnd;
330     return TRUE;
331 }
332
333 /* Child Window */
334
335 static const WCHAR szChildClass[] = {
336     'H','H',' ','C','h','i','l','d',0
337 };
338
339 static LRESULT Child_OnPaint(HWND hWnd)
340 {
341     PAINTSTRUCT ps;
342     HDC hdc;
343     RECT rc;
344
345     hdc = BeginPaint(hWnd, &ps);
346
347     /* Only paint the Navigation pane, identified by the fact
348      * that it has a child window
349      */
350     if (GetWindow(hWnd, GW_CHILD))
351     {
352         GetClientRect(hWnd, &rc);
353
354         /* set the border color */
355         SelectObject(hdc, GetStockObject(DC_PEN));
356         SetDCPenColor(hdc, GetSysColor(COLOR_BTNSHADOW));
357
358         /* Draw the top border */
359         LineTo(hdc, rc.right, 0);
360
361         SelectObject(hdc, GetStockObject(WHITE_PEN));
362         MoveToEx(hdc, 0, 1, NULL);
363         LineTo(hdc, rc.right, 1);
364     }
365
366     EndPaint(hWnd, &ps);
367
368     return 0;
369 }
370
371 static void ResizeTabChild(HHInfo *info, int tab)
372 {
373     HWND hwnd = info->tabs[tab].hwnd;
374     INT width, height;
375     RECT rect, tabrc;
376     DWORD cnt;
377
378     GetClientRect(info->WinType.hwndNavigation, &rect);
379     SendMessageW(info->hwndTabCtrl, TCM_GETITEMRECT, 0, (LPARAM)&tabrc);
380     cnt = SendMessageW(info->hwndTabCtrl, TCM_GETROWCOUNT, 0, 0);
381
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;
388
389     SetWindowPos(hwnd, NULL, rect.left, rect.top, width, height,
390                  SWP_NOZORDER | SWP_NOACTIVATE);
391
392     switch (tab)
393     {
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);
398
399         /* Resize the tab widget column to perfectly fit the tab window and
400          * leave sufficient space for the scroll widget.
401          */
402         SendMessageW(info->tabs[TAB_INDEX].hwnd, LVM_SETCOLUMNWIDTH, 0,
403                      width-scroll_width-2*border_width-2*edge_width);
404
405         break;
406     }
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;
412
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.
420          */
421         SendMessageW(info->search.hwndList, LVM_SETCOLUMNWIDTH, 0,
422                      width-scroll_width-2*border_width-2*edge_width);
423
424         break;
425     }
426     }
427 }
428
429 static LRESULT Child_OnSize(HWND hwnd)
430 {
431     HHInfo *info = (HHInfo*)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
432     RECT rect;
433
434     if(!info || hwnd != info->WinType.hwndNavigation)
435         return 0;
436
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);
441
442     ResizeTabChild(info, TAB_CONTENTS);
443     ResizeTabChild(info, TAB_INDEX);
444     ResizeTabChild(info, TAB_SEARCH);
445     return 0;
446 }
447
448 static LRESULT OnTabChange(HWND hwnd)
449 {
450     HHInfo *info = (HHInfo*)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
451
452     TRACE("%p\n", hwnd);
453
454     if (!info)
455         return 0;
456
457     if(info->tabs[info->current_tab].hwnd)
458         ShowWindow(info->tabs[info->current_tab].hwnd, SW_HIDE);
459
460     info->current_tab = SendMessageW(info->hwndTabCtrl, TCM_GETCURSEL, 0, 0);
461
462     if(info->tabs[info->current_tab].hwnd)
463         ShowWindow(info->tabs[info->current_tab].hwnd, SW_SHOW);
464
465     return 0;
466 }
467
468 static LRESULT OnTopicChange(HHInfo *info, void *user_data)
469 {
470     LPCWSTR chmfile = NULL, name = NULL, local = NULL;
471     ContentItem *citer;
472     SearchItem *siter;
473     IndexItem *iiter;
474
475     if(!user_data || !info)
476         return 0;
477
478     switch (info->current_tab)
479     {
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;
488             }
489             citer = citer->parent;
490         }
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;
497         }
498         if(iiter->nItems > 1) {
499             int i = 0;
500             LVITEMW lvi;
501
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;
506
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);
516             }
517             ShowWindow(info->popup.hwndPopup, SW_SHOW);
518             return 0;
519         }
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;
533     }
534
535     if(!chmfile)
536     {
537         FIXME("No help file found for this item!\n");
538         return 0;
539     }
540
541     TRACE("name %s loal %s\n", debugstr_w(name), debugstr_w(local));
542
543     NavigateToChm(info, chmfile, local);
544     return 0;
545 }
546
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)
549 {
550     WNDPROC editWndProc = (WNDPROC)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
551
552     if(message == WM_KEYUP && wParam == VK_RETURN)
553     {
554         NMHDR nmhdr;
555
556         nmhdr.hwndFrom = hWnd;
557         nmhdr.code = NM_RETURN;
558         SendMessageW(GetParent(GetParent(hWnd)), WM_NOTIFY, wParam, (LPARAM)&nmhdr);
559     }
560     return editWndProc(hWnd, message, wParam, lParam);
561 }
562
563 static LRESULT CALLBACK Child_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
564 {
565     switch (message)
566     {
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;
574
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)
584             {
585             case TAB_INDEX:
586                 return OnTopicChange(info, (void*)((NMITEMACTIVATE *)lParam)->lParam);
587             case TAB_SEARCH:
588                 return OnTopicChange(info, (void*)((NMITEMACTIVATE *)lParam)->lParam);
589             }
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;
598
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;
604             }
605             case TAB_SEARCH: {
606                 if(nmhdr->hwndFrom == info->search.hwndEdit) {
607                     char needle[100];
608                     DWORD i, len;
609
610                     len = GetWindowTextA(info->search.hwndEdit, needle, sizeof(needle));
611                     if(!len)
612                     {
613                         FIXME("Unable to get search text.\n");
614                         return 0;
615                     }
616                     /* Convert the requested text for comparison later against the
617                      * lower case version of HTML file contents.
618                      */
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;
626
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;
632                 }
633                 break;
634             }
635             }
636             break;
637         }
638         break;
639     }
640     default:
641         return DefWindowProcW(hWnd, message, wParam, lParam);
642     }
643
644     return 0;
645 }
646
647 static void HH_RegisterChildWndClass(HHInfo *pHHInfo)
648 {
649     WNDCLASSEXW wcex;
650
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);
663
664     RegisterClassExW(&wcex);
665 }
666
667 /* Toolbar */
668
669 #define ICON_SIZE   20
670
671 static void DisplayPopupMenu(HHInfo *info)
672 {
673     HMENU menu, submenu;
674     TBBUTTONINFOW button;
675     MENUITEMINFOW item;
676     POINT coords;
677     RECT rect;
678     DWORD index;
679
680     menu = LoadMenuW(hhctrl_hinstance, MAKEINTRESOURCEW(MENU_POPUP));
681
682     if (!menu)
683         return;
684
685     submenu = GetSubMenu(menu, 0);
686
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;
692
693     if (info->WinType.fNotExpanded)
694         item.dwTypeData = HH_LoadString(IDS_SHOWTABS);
695     else
696         item.dwTypeData = HH_LoadString(IDS_HIDETABS);
697
698     SetMenuItemInfoW(submenu, IDTB_EXPAND, FALSE, &item);
699     heap_free(item.dwTypeData);
700
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);
705
706     if (index == -1)
707        return;
708
709     /* Get position */
710     SendMessageW(info->WinType.hwndToolBar, TB_GETITEMRECT, index, (LPARAM) &rect);
711
712     coords.x = rect.left;
713     coords.y = rect.bottom;
714
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);
717 }
718
719 static void TB_OnClick(HWND hWnd, DWORD dwID)
720 {
721     HHInfo *info = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
722
723     switch (dwID)
724     {
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_BROWSE_FWD:
754         case IDTB_BROWSE_BACK:
755         case IDTB_JUMP1:
756         case IDTB_JUMP2:
757         case IDTB_CUSTOMIZE:
758         case IDTB_ZOOM:
759         case IDTB_TOC_NEXT:
760         case IDTB_TOC_PREV:
761             break;
762     }
763 }
764
765 static void TB_AddButton(TBBUTTON *pButtons, DWORD dwIndex, DWORD dwID, DWORD dwBitmap)
766 {
767     pButtons[dwIndex].iBitmap = dwBitmap;
768     pButtons[dwIndex].idCommand = dwID;
769     pButtons[dwIndex].fsState = TBSTATE_ENABLED;
770     pButtons[dwIndex].fsStyle = BTNS_BUTTON;
771     pButtons[dwIndex].dwData = 0;
772     pButtons[dwIndex].iString = 0;
773 }
774
775 static void TB_AddButtonsFromFlags(HHInfo *pHHInfo, TBBUTTON *pButtons, DWORD dwButtonFlags, LPDWORD pdwNumButtons)
776 {
777     int nHistBitmaps = 0, nStdBitmaps = 0, nHHBitmaps = 0;
778     HWND hToolbar = pHHInfo->WinType.hwndToolBar;
779     TBADDBITMAP tbAB;
780
781     /* Common bitmaps */
782     tbAB.hInst = HINST_COMMCTRL;
783     tbAB.nID = IDB_HIST_LARGE_COLOR;
784     nHistBitmaps = SendMessageW(hToolbar, TB_ADDBITMAP, 0, (LPARAM)&tbAB);
785     tbAB.nID = IDB_STD_LARGE_COLOR;
786     nStdBitmaps = SendMessageW(hToolbar, TB_ADDBITMAP, 0, (LPARAM)&tbAB);
787     /* hhctrl.ocx bitmaps */
788     tbAB.hInst = hhctrl_hinstance;
789     tbAB.nID = IDB_HHTOOLBAR;
790     nHHBitmaps = SendMessageW(hToolbar, TB_ADDBITMAP, 0, (LPARAM)&tbAB);
791
792     *pdwNumButtons = 0;
793
794     if (dwButtonFlags & HHWIN_BUTTON_EXPAND)
795     {
796         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_EXPAND, nHistBitmaps + HIST_VIEWTREE);
797         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_CONTRACT, nHistBitmaps + HIST_VIEWTREE);
798
799         if (pHHInfo->WinType.fNotExpanded)
800             pButtons[1].fsState |= TBSTATE_HIDDEN;
801         else
802             pButtons[0].fsState |= TBSTATE_HIDDEN;
803     }
804
805     if (dwButtonFlags & HHWIN_BUTTON_BACK)
806         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_BACK, nHistBitmaps + HIST_BACK);
807
808     if (dwButtonFlags & HHWIN_BUTTON_FORWARD)
809         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_FORWARD, nHistBitmaps + HIST_FORWARD);
810
811     if (dwButtonFlags & HHWIN_BUTTON_STOP)
812         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_STOP, nHHBitmaps + HH_STOP);
813
814     if (dwButtonFlags & HHWIN_BUTTON_REFRESH)
815         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_REFRESH, nHHBitmaps + HH_REFRESH);
816
817     if (dwButtonFlags & HHWIN_BUTTON_HOME)
818         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_HOME, nHHBitmaps + HH_HOME);
819
820     /* FIXME: Load the correct button bitmaps */
821     if (dwButtonFlags & HHWIN_BUTTON_SYNC)
822         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_SYNC, nStdBitmaps + STD_PRINT);
823
824     if (dwButtonFlags & HHWIN_BUTTON_OPTIONS)
825         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_OPTIONS, nStdBitmaps + STD_PRINT);
826
827     if (dwButtonFlags & HHWIN_BUTTON_PRINT)
828         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_PRINT, nStdBitmaps + STD_PRINT);
829
830     if (dwButtonFlags & HHWIN_BUTTON_JUMP1)
831         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_JUMP1, nStdBitmaps + STD_PRINT);
832
833     if (dwButtonFlags & HHWIN_BUTTON_JUMP2)
834         TB_AddButton(pButtons,(*pdwNumButtons)++, IDTB_JUMP2, nStdBitmaps + STD_PRINT);
835
836     if (dwButtonFlags & HHWIN_BUTTON_ZOOM)
837         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_ZOOM, nStdBitmaps + STD_PRINT);
838
839     if (dwButtonFlags & HHWIN_BUTTON_TOC_NEXT)
840         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_TOC_NEXT, nStdBitmaps + STD_PRINT);
841
842     if (dwButtonFlags & HHWIN_BUTTON_TOC_PREV)
843         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_TOC_PREV, nStdBitmaps + STD_PRINT);
844 }
845
846 static BOOL HH_AddToolbar(HHInfo *pHHInfo)
847 {
848     HWND hToolbar;
849     HWND hwndParent = pHHInfo->WinType.hwndHelp;
850     DWORD toolbarFlags;
851     TBBUTTON buttons[IDTB_TOC_PREV - IDTB_EXPAND];
852     DWORD dwStyles, dwExStyles;
853     DWORD dwNumButtons, dwIndex;
854
855     if (pHHInfo->WinType.fsWinProperties & HHWIN_PARAM_TB_FLAGS)
856         toolbarFlags = pHHInfo->WinType.fsToolBarFlags;
857     else
858         toolbarFlags = HHWIN_DEF_BUTTONS;
859
860     dwStyles = WS_CHILDWINDOW | WS_VISIBLE | TBSTYLE_FLAT |
861                TBSTYLE_WRAPABLE | TBSTYLE_TOOLTIPS | CCS_NODIVIDER;
862     dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
863
864     hToolbar = CreateWindowExW(dwExStyles, TOOLBARCLASSNAMEW, NULL, dwStyles,
865                                0, 0, 0, 0, hwndParent, NULL,
866                                hhctrl_hinstance, NULL);
867     if (!hToolbar)
868         return FALSE;
869     pHHInfo->WinType.hwndToolBar = hToolbar;
870
871     SendMessageW(hToolbar, TB_SETBITMAPSIZE, 0, MAKELONG(ICON_SIZE, ICON_SIZE));
872     SendMessageW(hToolbar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
873     SendMessageW(hToolbar, WM_SETFONT, (WPARAM)pHHInfo->hFont, TRUE);
874
875     TB_AddButtonsFromFlags(pHHInfo, buttons, toolbarFlags, &dwNumButtons);
876
877     for (dwIndex = 0; dwIndex < dwNumButtons; dwIndex++)
878     {
879         LPWSTR szBuf = HH_LoadString(buttons[dwIndex].idCommand);
880         DWORD dwLen = strlenW(szBuf);
881         szBuf[dwLen + 1] = 0; /* Double-null terminate */
882
883         buttons[dwIndex].iString = (DWORD)SendMessageW(hToolbar, TB_ADDSTRINGW, 0, (LPARAM)szBuf);
884         heap_free(szBuf);
885     }
886
887     SendMessageW(hToolbar, TB_ADDBUTTONSW, dwNumButtons, (LPARAM)buttons);
888     SendMessageW(hToolbar, TB_AUTOSIZE, 0, 0);
889     ShowWindow(hToolbar, SW_SHOW);
890
891     return TRUE;
892 }
893
894 /* Navigation Pane */
895
896 static void NP_GetNavigationRect(HHInfo *pHHInfo, RECT *rc)
897 {
898     HWND hwndParent = pHHInfo->WinType.hwndHelp;
899     HWND hwndToolbar = pHHInfo->WinType.hwndToolBar;
900     RECT rectWND, rectTB;
901
902     GetClientRect(hwndParent, &rectWND);
903     GetClientRect(hwndToolbar, &rectTB);
904
905     rc->left = 0;
906     rc->top = rectTB.bottom;
907     rc->bottom = rectWND.bottom - rectTB.bottom;
908
909     if (!(pHHInfo->WinType.fsValidMembers & HHWIN_PARAM_NAV_WIDTH) &&
910           pHHInfo->WinType.iNavWidth == 0)
911     {
912         pHHInfo->WinType.iNavWidth = WINTYPE_DEFAULT_NAVWIDTH;
913     }
914
915     rc->right = pHHInfo->WinType.iNavWidth;
916 }
917
918 static DWORD NP_CreateTab(HINSTANCE hInstance, HWND hwndTabCtrl, DWORD index)
919 {
920     TCITEMW tie;
921     LPWSTR tabText = HH_LoadString(index);
922     DWORD ret;
923
924     tie.mask = TCIF_TEXT;
925     tie.pszText = tabText;
926
927     ret = SendMessageW( hwndTabCtrl, TCM_INSERTITEMW, index, (LPARAM)&tie );
928
929     heap_free(tabText);
930     return ret;
931 }
932
933 static BOOL HH_AddNavigationPane(HHInfo *info)
934 {
935     HWND hWnd, hwndTabCtrl;
936     HWND hwndParent = info->WinType.hwndHelp;
937     DWORD dwStyles = WS_CHILDWINDOW;
938     DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
939     RECT rc;
940
941     if (!info->WinType.fNotExpanded)
942         dwStyles |= WS_VISIBLE;
943
944     NP_GetNavigationRect(info, &rc);
945
946     hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
947                            rc.left, rc.top, rc.right, rc.bottom,
948                            hwndParent, NULL, hhctrl_hinstance, NULL);
949     if (!hWnd)
950         return FALSE;
951
952     SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)info);
953
954     hwndTabCtrl = CreateWindowExW(dwExStyles, WC_TABCONTROLW, szEmpty, dwStyles | WS_VISIBLE,
955                                   0, TAB_TOP_PADDING,
956                                   rc.right - TAB_RIGHT_PADDING,
957                                   rc.bottom - TAB_TOP_PADDING,
958                                   hWnd, NULL, hhctrl_hinstance, NULL);
959     if (!hwndTabCtrl)
960         return FALSE;
961
962     if (*info->WinType.pszToc)
963         info->tabs[TAB_CONTENTS].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_CONTENTS);
964
965     if (*info->WinType.pszIndex)
966         info->tabs[TAB_INDEX].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_INDEX);
967
968     if (info->WinType.fsWinProperties & HHWIN_PROP_TAB_SEARCH)
969         info->tabs[TAB_SEARCH].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_SEARCH);
970
971     if (info->WinType.fsWinProperties & HHWIN_PROP_TAB_FAVORITES)
972         info->tabs[TAB_FAVORITES].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_FAVORITES);
973
974     SendMessageW(hwndTabCtrl, WM_SETFONT, (WPARAM)info->hFont, TRUE);
975
976     info->hwndTabCtrl = hwndTabCtrl;
977     info->WinType.hwndNavigation = hWnd;
978     return TRUE;
979 }
980
981 /* HTML Pane */
982
983 static void HP_GetHTMLRect(HHInfo *info, RECT *rc)
984 {
985     RECT rectTB, rectWND, rectNP, rectSB;
986
987     GetClientRect(info->WinType.hwndHelp, &rectWND);
988     GetClientRect(info->WinType.hwndToolBar, &rectTB);
989     GetClientRect(info->hwndSizeBar, &rectSB);
990
991     if (info->WinType.fNotExpanded)
992         rc->left = 0;
993     else
994     {
995         GetClientRect(info->WinType.hwndNavigation, &rectNP);
996         rc->left = rectNP.right + rectSB.right;
997     }
998
999     rc->top = rectTB.bottom;
1000     rc->right = rectWND.right - rc->left;
1001     rc->bottom = rectWND.bottom - rectTB.bottom;
1002 }
1003
1004 static BOOL HH_AddHTMLPane(HHInfo *pHHInfo)
1005 {
1006     HWND hWnd;
1007     HWND hwndParent = pHHInfo->WinType.hwndHelp;
1008     DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN;
1009     DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_CLIENTEDGE;
1010     RECT rc;
1011
1012     HP_GetHTMLRect(pHHInfo, &rc);
1013
1014     hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
1015                            rc.left, rc.top, rc.right, rc.bottom,
1016                            hwndParent, NULL, hhctrl_hinstance, NULL);
1017     if (!hWnd)
1018         return FALSE;
1019
1020     if (!InitWebBrowser(pHHInfo, hWnd))
1021         return FALSE;
1022
1023     /* store the pointer to the HH info struct */
1024     SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
1025
1026     ShowWindow(hWnd, SW_SHOW);
1027     UpdateWindow(hWnd);
1028
1029     pHHInfo->WinType.hwndHTML = hWnd;
1030     return TRUE;
1031 }
1032
1033 static BOOL AddContentTab(HHInfo *info)
1034 {
1035     if(info->tabs[TAB_CONTENTS].id == -1)
1036         return TRUE; /* No "Contents" tab */
1037     info->tabs[TAB_CONTENTS].hwnd = CreateWindowExW(WS_EX_CLIENTEDGE, WC_TREEVIEWW,
1038            szEmpty, WS_CHILD | WS_BORDER | 0x25, 50, 50, 100, 100,
1039            info->WinType.hwndNavigation, NULL, hhctrl_hinstance, NULL);
1040     if(!info->tabs[TAB_CONTENTS].hwnd) {
1041         ERR("Could not create treeview control\n");
1042         return FALSE;
1043     }
1044
1045     ResizeTabChild(info, TAB_CONTENTS);
1046     ShowWindow(info->tabs[TAB_CONTENTS].hwnd, SW_SHOW);
1047
1048     return TRUE;
1049 }
1050
1051 static BOOL AddIndexTab(HHInfo *info)
1052 {
1053     char hidden_column[] = "Column";
1054     LVCOLUMNA lvc;
1055
1056     if(info->tabs[TAB_INDEX].id == -1)
1057         return TRUE; /* No "Index" tab */
1058     info->tabs[TAB_INDEX].hwnd = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW,
1059            szEmpty, WS_CHILD | WS_BORDER | LVS_SINGLESEL | LVS_REPORT | LVS_NOCOLUMNHEADER, 50, 50, 100, 100,
1060            info->WinType.hwndNavigation, NULL, hhctrl_hinstance, NULL);
1061     if(!info->tabs[TAB_INDEX].hwnd) {
1062         ERR("Could not create ListView control\n");
1063         return FALSE;
1064     }
1065     memset(&lvc, 0, sizeof(lvc));
1066     lvc.mask = LVCF_TEXT;
1067     lvc.pszText = hidden_column;
1068     if(SendMessageW(info->tabs[TAB_INDEX].hwnd, LVM_INSERTCOLUMNA, 0, (LPARAM) &lvc) == -1)
1069     {
1070         ERR("Could not create ListView column\n");
1071         return FALSE;
1072     }
1073
1074     ResizeTabChild(info, TAB_INDEX);
1075     ShowWindow(info->tabs[TAB_INDEX].hwnd, SW_HIDE);
1076
1077     return TRUE;
1078 }
1079
1080 static BOOL AddSearchTab(HHInfo *info)
1081 {
1082     HWND hwndList, hwndEdit, hwndContainer;
1083     char hidden_column[] = "Column";
1084     WNDPROC editWndProc;
1085     LVCOLUMNA lvc;
1086
1087     if(info->tabs[TAB_SEARCH].id == -1)
1088         return TRUE; /* No "Search" tab */
1089     hwndContainer = CreateWindowExW(WS_EX_CONTROLPARENT, szChildClass, szEmpty,
1090                                     WS_CHILD, 0, 0, 0, 0, info->WinType.hwndNavigation,
1091                                     NULL, hhctrl_hinstance, NULL);
1092     if(!hwndContainer) {
1093         ERR("Could not create search window container control.\n");
1094         return FALSE;
1095     }
1096     hwndEdit = CreateWindowExW(WS_EX_CLIENTEDGE, WC_EDITW, szEmpty, WS_CHILD
1097                                 | WS_VISIBLE | ES_LEFT | SS_NOTIFY, 0, 0, 0, 0,
1098                                hwndContainer, NULL, hhctrl_hinstance, NULL);
1099     if(!hwndEdit) {
1100         ERR("Could not create search ListView control.\n");
1101         return FALSE;
1102     }
1103     if(SendMessageW(hwndEdit, WM_SETFONT, (WPARAM) info->hFont, (LPARAM) FALSE) == -1)
1104     {
1105         ERR("Could not set font for edit control.\n");
1106         return FALSE;
1107     }
1108     editWndProc = (WNDPROC) SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC, (LONG_PTR)EditChild_WndProc);
1109     if(!editWndProc) {
1110         ERR("Could not redirect messages for edit control.\n");
1111         return FALSE;
1112     }
1113     SetWindowLongPtrW(hwndEdit, GWLP_USERDATA, (LONG_PTR)editWndProc);
1114     hwndList = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW, szEmpty,
1115                                WS_CHILD | WS_VISIBLE | WS_BORDER | LVS_SINGLESEL
1116                                 | LVS_REPORT | LVS_NOCOLUMNHEADER, 0, 0, 0, 0,
1117                                hwndContainer, NULL, hhctrl_hinstance, NULL);
1118     if(!hwndList) {
1119         ERR("Could not create search ListView control.\n");
1120         return FALSE;
1121     }
1122     memset(&lvc, 0, sizeof(lvc));
1123     lvc.mask = LVCF_TEXT;
1124     lvc.pszText = hidden_column;
1125     if(SendMessageW(hwndList, LVM_INSERTCOLUMNA, 0, (LPARAM) &lvc) == -1)
1126     {
1127         ERR("Could not create ListView column\n");
1128         return FALSE;
1129     }
1130
1131     info->search.hwndEdit = hwndEdit;
1132     info->search.hwndList = hwndList;
1133     info->search.hwndContainer = hwndContainer;
1134     info->tabs[TAB_SEARCH].hwnd = hwndContainer;
1135
1136     SetWindowLongPtrW(hwndContainer, GWLP_USERDATA, (LONG_PTR)info);
1137
1138     ResizeTabChild(info, TAB_SEARCH);
1139
1140     return TRUE;
1141 }
1142
1143 /* The Index tab's sub-topic popup */
1144
1145 static void ResizePopupChild(HHInfo *info)
1146 {
1147     int scroll_width = GetSystemMetrics(SM_CXVSCROLL);
1148     int border_width = GetSystemMetrics(SM_CXBORDER);
1149     int edge_width = GetSystemMetrics(SM_CXEDGE);
1150     INT width, height;
1151     RECT rect;
1152
1153     if(!info)
1154         return;
1155
1156     GetClientRect(info->popup.hwndPopup, &rect);
1157     SetWindowPos(info->popup.hwndCallback, HWND_TOP, 0, 0,
1158                  rect.right, rect.bottom, SWP_NOMOVE);
1159
1160     rect.left = TAB_MARGIN;
1161     rect.top = TAB_TOP_PADDING + TAB_MARGIN;
1162     rect.right -= TAB_RIGHT_PADDING + TAB_MARGIN;
1163     rect.bottom -= TAB_MARGIN;
1164     width = rect.right-rect.left;
1165     height = rect.bottom-rect.top;
1166
1167     SetWindowPos(info->popup.hwndList, NULL, rect.left, rect.top, width, height,
1168                  SWP_NOZORDER | SWP_NOACTIVATE);
1169
1170     SendMessageW(info->popup.hwndList, LVM_SETCOLUMNWIDTH, 0,
1171                  width-scroll_width-2*border_width-2*edge_width);
1172 }
1173
1174 static LRESULT CALLBACK HelpPopup_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1175 {
1176     HHInfo *info = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
1177
1178     switch (message)
1179     {
1180     case WM_SIZE:
1181         ResizePopupChild(info);
1182         return 0;
1183     case WM_DESTROY:
1184         DestroyWindow(hWnd);
1185         return 0;
1186     case WM_CLOSE:
1187         ShowWindow(hWnd, SW_HIDE);
1188         return 0;
1189
1190     default:
1191         return DefWindowProcW(hWnd, message, wParam, lParam);
1192     }
1193
1194     return 0;
1195 }
1196
1197 static LRESULT CALLBACK PopupChild_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1198 {
1199     switch (message)
1200     {
1201     case WM_NOTIFY: {
1202         NMHDR *nmhdr = (NMHDR*)lParam;
1203         switch(nmhdr->code)
1204         {
1205         case NM_DBLCLK: {
1206             HHInfo *info = (HHInfo*)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
1207             IndexSubItem *iter;
1208
1209             if(info == 0 || lParam == 0)
1210                 return 0;
1211             iter = (IndexSubItem*) ((NMITEMACTIVATE *)lParam)->lParam;
1212             if(iter == 0)
1213                 return 0;
1214             NavigateToChm(info, info->index->merge.chm_file, iter->local);
1215             ShowWindow(info->popup.hwndPopup, SW_HIDE);
1216             return 0;
1217         }
1218         case NM_RETURN: {
1219             HHInfo *info = (HHInfo*)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
1220             IndexSubItem *iter;
1221             LVITEMW lvItem;
1222
1223             if(info == 0)
1224                 return 0;
1225
1226             lvItem.iItem = (int) SendMessageW(info->popup.hwndList, LVM_GETSELECTIONMARK, 0, 0);
1227             lvItem.mask = TVIF_PARAM;
1228             SendMessageW(info->popup.hwndList, LVM_GETITEMW, 0, (LPARAM)&lvItem);
1229             iter = (IndexSubItem*) lvItem.lParam;
1230             NavigateToChm(info, info->index->merge.chm_file, iter->local);
1231             ShowWindow(info->popup.hwndPopup, SW_HIDE);
1232             return 0;
1233         }
1234         }
1235         break;
1236     }
1237     default:
1238         return DefWindowProcW(hWnd, message, wParam, lParam);
1239     }
1240
1241     return 0;
1242 }
1243
1244 static BOOL AddIndexPopup(HHInfo *info)
1245 {
1246     static const WCHAR szPopupChildClass[] = {'H','H',' ','P','o','p','u','p',' ','C','h','i','l','d',0};
1247     static const WCHAR windowCaptionW[] = {'S','e','l','e','c','t',' ','T','o','p','i','c',':',0};
1248     static const WCHAR windowClassW[] = {'H','H',' ','P','o','p','u','p',0};
1249     HWND hwndList, hwndPopup, hwndCallback;
1250     char hidden_column[] = "Column";
1251     WNDCLASSEXW wcex;
1252     LVCOLUMNA lvc;
1253
1254     if(info->tabs[TAB_INDEX].id == -1)
1255         return TRUE; /* No "Index" tab */
1256
1257     wcex.cbSize         = sizeof(WNDCLASSEXW);
1258     wcex.style          = CS_HREDRAW | CS_VREDRAW;
1259     wcex.lpfnWndProc    = HelpPopup_WndProc;
1260     wcex.cbClsExtra     = 0;
1261     wcex.cbWndExtra     = 0;
1262     wcex.hInstance      = hhctrl_hinstance;
1263     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1264     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
1265     wcex.hbrBackground  = (HBRUSH)(COLOR_MENU + 1);
1266     wcex.lpszMenuName   = NULL;
1267     wcex.lpszClassName  = windowClassW;
1268     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1269     RegisterClassExW(&wcex);
1270
1271     wcex.cbSize         = sizeof(WNDCLASSEXW);
1272     wcex.style          = 0;
1273     wcex.lpfnWndProc    = PopupChild_WndProc;
1274     wcex.cbClsExtra     = 0;
1275     wcex.cbWndExtra     = 0;
1276     wcex.hInstance      = hhctrl_hinstance;
1277     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1278     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
1279     wcex.hbrBackground  = (HBRUSH)(COLOR_BTNFACE + 1);
1280     wcex.lpszMenuName   = NULL;
1281     wcex.lpszClassName  = szPopupChildClass;
1282     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1283     RegisterClassExW(&wcex);
1284
1285     hwndPopup = CreateWindowExW(WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_APPWINDOW
1286                                  | WS_EX_WINDOWEDGE | WS_EX_RIGHTSCROLLBAR,
1287                                 windowClassW, windowCaptionW, WS_POPUPWINDOW
1288                                  | WS_OVERLAPPEDWINDOW | WS_VISIBLE
1289                                  | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, CW_USEDEFAULT,
1290                                 CW_USEDEFAULT, 300, 200, info->WinType.hwndHelp,
1291                                 NULL, hhctrl_hinstance, NULL);
1292     if (!hwndPopup)
1293         return FALSE;
1294
1295     hwndCallback = CreateWindowExW(WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR,
1296                                    szPopupChildClass, szEmpty, WS_CHILDWINDOW | WS_VISIBLE,
1297                                    0, 0, 0, 0,
1298                                    hwndPopup, NULL, hhctrl_hinstance, NULL);
1299     if (!hwndCallback)
1300         return FALSE;
1301
1302     ShowWindow(hwndPopup, SW_HIDE);
1303     hwndList = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW, szEmpty,
1304                                WS_CHILD | WS_BORDER | LVS_SINGLESEL | LVS_REPORT
1305                                 | LVS_NOCOLUMNHEADER, 50, 50, 100, 100,
1306                                hwndCallback, NULL, hhctrl_hinstance, NULL);
1307     if(!hwndList) {
1308         ERR("Could not create popup ListView control\n");
1309         return FALSE;
1310     }
1311     memset(&lvc, 0, sizeof(lvc));
1312     lvc.mask = LVCF_TEXT;
1313     lvc.pszText = hidden_column;
1314     if(SendMessageW(hwndList, LVM_INSERTCOLUMNA, 0, (LPARAM) &lvc) == -1)
1315     {
1316         ERR("Could not create popup ListView column\n");
1317         return FALSE;
1318     }
1319
1320     info->popup.hwndCallback = hwndCallback;
1321     info->popup.hwndPopup = hwndPopup;
1322     info->popup.hwndList = hwndList;
1323     SetWindowLongPtrW(hwndPopup, GWLP_USERDATA, (LONG_PTR)info);
1324     SetWindowLongPtrW(hwndCallback, GWLP_USERDATA, (LONG_PTR)info);
1325
1326     ResizePopupChild(info);
1327     ShowWindow(hwndList, SW_SHOW);
1328
1329     return TRUE;
1330 }
1331
1332 /* Viewer Window */
1333
1334 static void ExpandContract(HHInfo *pHHInfo)
1335 {
1336     RECT r, nav;
1337
1338     pHHInfo->WinType.fNotExpanded = !pHHInfo->WinType.fNotExpanded;
1339     GetWindowRect(pHHInfo->WinType.hwndHelp, &r);
1340     NP_GetNavigationRect(pHHInfo, &nav);
1341
1342     /* hide/show both the nav bar and the size bar */
1343     if (pHHInfo->WinType.fNotExpanded)
1344     {
1345         ShowWindow(pHHInfo->WinType.hwndNavigation, SW_HIDE);
1346         ShowWindow(pHHInfo->hwndSizeBar, SW_HIDE);
1347         r.left = r.left + nav.right;
1348
1349         SendMessageW(pHHInfo->WinType.hwndToolBar, TB_HIDEBUTTON, IDTB_EXPAND, MAKELPARAM(FALSE, 0));
1350         SendMessageW(pHHInfo->WinType.hwndToolBar, TB_HIDEBUTTON, IDTB_CONTRACT, MAKELPARAM(TRUE, 0));
1351     }
1352     else
1353     {
1354         ShowWindow(pHHInfo->WinType.hwndNavigation, SW_SHOW);
1355         ShowWindow(pHHInfo->hwndSizeBar, SW_SHOW);
1356         r.left = r.left - nav.right;
1357
1358         SendMessageW(pHHInfo->WinType.hwndToolBar, TB_HIDEBUTTON, IDTB_EXPAND, MAKELPARAM(TRUE, 0));
1359         SendMessageW(pHHInfo->WinType.hwndToolBar, TB_HIDEBUTTON, IDTB_CONTRACT, MAKELPARAM(FALSE, 0));
1360     }
1361
1362     MoveWindow(pHHInfo->WinType.hwndHelp, r.left, r.top, r.right-r.left, r.bottom-r.top, TRUE);
1363 }
1364
1365 static LRESULT Help_OnSize(HWND hWnd)
1366 {
1367     HHInfo *pHHInfo = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
1368     DWORD dwSize;
1369     RECT rc;
1370
1371     if (!pHHInfo)
1372         return 0;
1373
1374     if (!pHHInfo->WinType.fNotExpanded)
1375     {
1376         NP_GetNavigationRect(pHHInfo, &rc);
1377         SetWindowPos(pHHInfo->WinType.hwndNavigation, HWND_TOP, 0, 0,
1378                      rc.right, rc.bottom, SWP_NOMOVE);
1379
1380         SB_GetSizeBarRect(pHHInfo, &rc);
1381         SetWindowPos(pHHInfo->hwndSizeBar, HWND_TOP, rc.left, rc.top,
1382                      rc.right, rc.bottom, SWP_SHOWWINDOW);
1383
1384     }
1385
1386     HP_GetHTMLRect(pHHInfo, &rc);
1387     SetWindowPos(pHHInfo->WinType.hwndHTML, HWND_TOP, rc.left, rc.top,
1388                  rc.right, rc.bottom, SWP_SHOWWINDOW);
1389
1390     /* Resize browser window taking the frame size into account */
1391     dwSize = GetSystemMetrics(SM_CXFRAME);
1392     ResizeWebBrowser(pHHInfo, rc.right - dwSize, rc.bottom - dwSize);
1393
1394     return 0;
1395 }
1396
1397 static LRESULT CALLBACK Help_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1398 {
1399     switch (message)
1400     {
1401     case WM_COMMAND:
1402         if (HIWORD(wParam) == BN_CLICKED)
1403             TB_OnClick(hWnd, LOWORD(wParam));
1404         break;
1405     case WM_SIZE:
1406         return Help_OnSize(hWnd);
1407     case WM_CLOSE:
1408         ReleaseHelpViewer((HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA));
1409         return 0;
1410     case WM_DESTROY:
1411         if(hh_process)
1412             PostQuitMessage(0);
1413         break;
1414
1415     default:
1416         return DefWindowProcW(hWnd, message, wParam, lParam);
1417     }
1418
1419     return 0;
1420 }
1421
1422 static BOOL HH_CreateHelpWindow(HHInfo *info)
1423 {
1424     HWND hWnd;
1425     RECT winPos = info->WinType.rcWindowPos;
1426     WNDCLASSEXW wcex;
1427     DWORD dwStyles, dwExStyles;
1428     DWORD x, y, width = 0, height = 0;
1429     LPCWSTR caption;
1430
1431     static const WCHAR windowClassW[] = {
1432         'H','H',' ', 'P','a','r','e','n','t',0
1433     };
1434
1435     wcex.cbSize         = sizeof(WNDCLASSEXW);
1436     wcex.style          = CS_HREDRAW | CS_VREDRAW;
1437     wcex.lpfnWndProc    = Help_WndProc;
1438     wcex.cbClsExtra     = 0;
1439     wcex.cbWndExtra     = 0;
1440     wcex.hInstance      = hhctrl_hinstance;
1441     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1442     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
1443     wcex.hbrBackground  = (HBRUSH)(COLOR_MENU + 1);
1444     wcex.lpszMenuName   = NULL;
1445     wcex.lpszClassName  = windowClassW;
1446     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1447
1448     RegisterClassExW(&wcex);
1449
1450     /* Read in window parameters if available */
1451     if (info->WinType.fsValidMembers & HHWIN_PARAM_STYLES)
1452         dwStyles = info->WinType.dwStyles | WS_OVERLAPPEDWINDOW;
1453     else
1454         dwStyles = WS_OVERLAPPEDWINDOW | WS_VISIBLE |
1455                    WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
1456
1457     if (info->WinType.fsValidMembers & HHWIN_PARAM_EXSTYLES)
1458         dwExStyles = info->WinType.dwExStyles;
1459     else
1460         dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_APPWINDOW |
1461                      WS_EX_WINDOWEDGE | WS_EX_RIGHTSCROLLBAR;
1462
1463     if (info->WinType.fsValidMembers & HHWIN_PARAM_RECT)
1464     {
1465         x = winPos.left;
1466         y = winPos.top;
1467         width = winPos.right - x;
1468         height = winPos.bottom - y;
1469     }
1470     if (!width || !height)
1471     {
1472         x = WINTYPE_DEFAULT_X;
1473         y = WINTYPE_DEFAULT_Y;
1474         width = WINTYPE_DEFAULT_WIDTH;
1475         height = WINTYPE_DEFAULT_HEIGHT;
1476     }
1477
1478     if (info->WinType.fNotExpanded)
1479     {
1480         if (!(info->WinType.fsValidMembers & HHWIN_PARAM_NAV_WIDTH) &&
1481               info->WinType.iNavWidth == 0)
1482         {
1483             info->WinType.iNavWidth = WINTYPE_DEFAULT_NAVWIDTH;
1484         }
1485
1486         x += info->WinType.iNavWidth;
1487         width -= info->WinType.iNavWidth;
1488     }
1489
1490
1491     caption = info->WinType.pszCaption;
1492     if (!*caption) caption = info->pCHMInfo->defTitle;
1493
1494     hWnd = CreateWindowExW(dwExStyles, windowClassW, caption,
1495                            dwStyles, x, y, width, height, NULL, NULL, hhctrl_hinstance, NULL);
1496     if (!hWnd)
1497         return FALSE;
1498
1499     ShowWindow(hWnd, SW_SHOW);
1500     UpdateWindow(hWnd);
1501
1502     /* store the pointer to the HH info struct */
1503     SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)info);
1504
1505     info->WinType.hwndHelp = hWnd;
1506     return TRUE;
1507 }
1508
1509 static void HH_CreateFont(HHInfo *pHHInfo)
1510 {
1511     LOGFONTW lf;
1512
1513     GetObjectW(GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONTW), &lf);
1514     lf.lfWeight = FW_NORMAL;
1515     lf.lfItalic = FALSE;
1516     lf.lfUnderline = FALSE;
1517
1518     pHHInfo->hFont = CreateFontIndirectW(&lf);
1519 }
1520
1521 static void HH_InitRequiredControls(DWORD dwControls)
1522 {
1523     INITCOMMONCONTROLSEX icex;
1524
1525     icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
1526     icex.dwICC = dwControls;
1527     InitCommonControlsEx(&icex);
1528 }
1529
1530 /* Creates the whole package */
1531 static BOOL CreateViewer(HHInfo *pHHInfo)
1532 {
1533     HH_CreateFont(pHHInfo);
1534
1535     if (!HH_CreateHelpWindow(pHHInfo))
1536         return FALSE;
1537
1538     HH_InitRequiredControls(ICC_BAR_CLASSES);
1539
1540     if (!HH_AddToolbar(pHHInfo))
1541         return FALSE;
1542
1543     HH_RegisterChildWndClass(pHHInfo);
1544
1545     if (!HH_AddNavigationPane(pHHInfo))
1546         return FALSE;
1547
1548     HH_RegisterSizeBarClass(pHHInfo);
1549
1550     if (!HH_AddSizeBar(pHHInfo))
1551         return FALSE;
1552
1553     if (!HH_AddHTMLPane(pHHInfo))
1554         return FALSE;
1555
1556     if (!AddContentTab(pHHInfo))
1557         return FALSE;
1558
1559     if (!AddIndexTab(pHHInfo))
1560         return FALSE;
1561
1562     if (!AddIndexPopup(pHHInfo))
1563         return FALSE;
1564
1565     if (!AddSearchTab(pHHInfo))
1566         return FALSE;
1567
1568     InitContent(pHHInfo);
1569     InitIndex(pHHInfo);
1570
1571     return TRUE;
1572 }
1573
1574 void ReleaseHelpViewer(HHInfo *info)
1575 {
1576     TRACE("(%p)\n", info);
1577
1578     if (!info)
1579         return;
1580
1581     /* Free allocated strings */
1582     heap_free(info->pszType);
1583     heap_free(info->pszCaption);
1584     heap_free(info->pszToc);
1585     heap_free(info->pszIndex);
1586     heap_free(info->pszFile);
1587     heap_free(info->pszHome);
1588     heap_free(info->pszJump1);
1589     heap_free(info->pszJump2);
1590     heap_free(info->pszUrlJump1);
1591     heap_free(info->pszUrlJump2);
1592
1593     if (info->pCHMInfo)
1594         CloseCHM(info->pCHMInfo);
1595
1596     ReleaseWebBrowser(info);
1597     ReleaseContent(info);
1598     ReleaseIndex(info);
1599     ReleaseSearch(info);
1600
1601     if(info->WinType.hwndHelp)
1602         DestroyWindow(info->WinType.hwndHelp);
1603
1604     heap_free(info);
1605     OleUninitialize();
1606 }
1607
1608 HHInfo *CreateHelpViewer(LPCWSTR filename)
1609 {
1610     HHInfo *info = heap_alloc_zero(sizeof(HHInfo));
1611     int i;
1612
1613     /* Set the invalid tab ID (-1) as the default value for all
1614      * of the tabs, this matches a failed TCM_INSERTITEM call.
1615      */
1616     for(i=0;i<sizeof(info->tabs)/sizeof(HHTab);i++)
1617         info->tabs[i].id = -1;
1618
1619     OleInitialize(NULL);
1620
1621     info->pCHMInfo = OpenCHM(filename);
1622     if(!info->pCHMInfo) {
1623         ReleaseHelpViewer(info);
1624         return NULL;
1625     }
1626
1627     if (!LoadWinTypeFromCHM(info)) {
1628         ReleaseHelpViewer(info);
1629         return NULL;
1630     }
1631
1632     if(!CreateViewer(info)) {
1633         ReleaseHelpViewer(info);
1634         return NULL;
1635     }
1636
1637     return info;
1638 }