iphlpapi: Add support for TCP_TABLE_OWNER_PID_ALL in GetExtendedTcpTable.
[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     int tab_id, tab_index, i;
452
453     TRACE("%p\n", hwnd);
454
455     if (!info)
456         return 0;
457
458     if(info->tabs[info->current_tab].hwnd)
459         ShowWindow(info->tabs[info->current_tab].hwnd, SW_HIDE);
460
461     tab_id = (int) SendMessageW(info->hwndTabCtrl, TCM_GETCURSEL, 0, 0);
462     /* convert the ID of the tab to an index in our tab list */
463     tab_index = -1;
464     for (i=0; i<TAB_NUMTABS; i++)
465     {
466         if (info->tabs[i].id == tab_id)
467         {
468             tab_index = i;
469             break;
470         }
471     }
472     if (tab_index == -1)
473     {
474         FIXME("Tab ID %d does not correspond to a valid index in the tab list.\n", tab_id);
475         return 0;
476     }
477     info->current_tab = tab_index;
478
479     if(info->tabs[info->current_tab].hwnd)
480         ShowWindow(info->tabs[info->current_tab].hwnd, SW_SHOW);
481
482     return 0;
483 }
484
485 static LRESULT OnTopicChange(HHInfo *info, void *user_data)
486 {
487     LPCWSTR chmfile = NULL, name = NULL, local = NULL;
488     ContentItem *citer;
489     SearchItem *siter;
490     IndexItem *iiter;
491
492     if(!user_data || !info)
493         return 0;
494
495     switch (info->current_tab)
496     {
497     case TAB_CONTENTS:
498         citer = (ContentItem *) user_data;
499         name = citer->name;
500         local = citer->local;
501         while(citer) {
502             if(citer->merge.chm_file) {
503                 chmfile = citer->merge.chm_file;
504                 break;
505             }
506             citer = citer->parent;
507         }
508         break;
509     case TAB_INDEX:
510         iiter = (IndexItem *) user_data;
511         if(iiter->nItems == 0) {
512             FIXME("No entries for this item!\n");
513             return 0;
514         }
515         if(iiter->nItems > 1) {
516             int i = 0;
517             LVITEMW lvi;
518
519             SendMessageW(info->popup.hwndList, LVM_DELETEALLITEMS, 0, 0);
520             for(i=0;i<iiter->nItems;i++) {
521                 IndexSubItem *item = &iiter->items[i];
522                 WCHAR *name = iiter->keyword;
523
524                 if(item->name)
525                     name = item->name;
526                 memset(&lvi, 0, sizeof(lvi));
527                 lvi.iItem = i;
528                 lvi.mask = LVIF_TEXT|LVIF_PARAM;
529                 lvi.cchTextMax = strlenW(name)+1;
530                 lvi.pszText = name;
531                 lvi.lParam = (LPARAM) item;
532                 SendMessageW(info->popup.hwndList, LVM_INSERTITEMW, 0, (LPARAM)&lvi);
533             }
534             ShowWindow(info->popup.hwndPopup, SW_SHOW);
535             return 0;
536         }
537         name = iiter->items[0].name;
538         local = iiter->items[0].local;
539         chmfile = iiter->merge.chm_file;
540         break;
541     case TAB_SEARCH:
542         siter = (SearchItem *) user_data;
543         name = siter->filename;
544         local = siter->filename;
545         chmfile = info->pCHMInfo->szFile;
546         break;
547     default:
548         FIXME("Unhandled operation for this tab!\n");
549         return 0;
550     }
551
552     if(!chmfile)
553     {
554         FIXME("No help file found for this item!\n");
555         return 0;
556     }
557
558     TRACE("name %s loal %s\n", debugstr_w(name), debugstr_w(local));
559
560     NavigateToChm(info, chmfile, local);
561     return 0;
562 }
563
564 /* Capture the Enter/Return key and send it up to Child_WndProc as an NM_RETURN message */
565 static LRESULT CALLBACK EditChild_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
566 {
567     WNDPROC editWndProc = (WNDPROC)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
568
569     if(message == WM_KEYUP && wParam == VK_RETURN)
570     {
571         NMHDR nmhdr;
572
573         nmhdr.hwndFrom = hWnd;
574         nmhdr.code = NM_RETURN;
575         SendMessageW(GetParent(GetParent(hWnd)), WM_NOTIFY, wParam, (LPARAM)&nmhdr);
576     }
577     return editWndProc(hWnd, message, wParam, lParam);
578 }
579
580 static LRESULT CALLBACK Child_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
581 {
582     switch (message)
583     {
584     case WM_PAINT:
585         return Child_OnPaint(hWnd);
586     case WM_SIZE:
587         return Child_OnSize(hWnd);
588     case WM_NOTIFY: {
589         HHInfo *info = (HHInfo*)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
590         NMHDR *nmhdr = (NMHDR*)lParam;
591
592         switch(nmhdr->code) {
593         case TCN_SELCHANGE:
594             return OnTabChange(hWnd);
595         case TVN_SELCHANGEDW:
596             return OnTopicChange(info, (void*)((NMTREEVIEWW *)lParam)->itemNew.lParam);
597         case NM_DBLCLK:
598             if(!info)
599                 return 0;
600             switch(info->current_tab)
601             {
602             case TAB_INDEX:
603                 return OnTopicChange(info, (void*)((NMITEMACTIVATE *)lParam)->lParam);
604             case TAB_SEARCH:
605                 return OnTopicChange(info, (void*)((NMITEMACTIVATE *)lParam)->lParam);
606             }
607             break;
608         case NM_RETURN:
609             if(!info)
610                 return 0;
611             switch(info->current_tab) {
612             case TAB_INDEX: {
613                 HWND hwndList = info->tabs[TAB_INDEX].hwnd;
614                 LVITEMW lvItem;
615
616                 lvItem.iItem = (int) SendMessageW(hwndList, LVM_GETSELECTIONMARK, 0, 0);
617                 lvItem.mask = TVIF_PARAM;
618                 SendMessageW(hwndList, LVM_GETITEMW, 0, (LPARAM)&lvItem);
619                 OnTopicChange(info, (void*) lvItem.lParam);
620                 return 0;
621             }
622             case TAB_SEARCH: {
623                 if(nmhdr->hwndFrom == info->search.hwndEdit) {
624                     char needle[100];
625                     DWORD i, len;
626
627                     len = GetWindowTextA(info->search.hwndEdit, needle, sizeof(needle));
628                     if(!len)
629                     {
630                         FIXME("Unable to get search text.\n");
631                         return 0;
632                     }
633                     /* Convert the requested text for comparison later against the
634                      * lower case version of HTML file contents.
635                      */
636                     for(i=0;i<len;i++)
637                         needle[i] = tolower(needle[i]);
638                     InitSearch(info, needle);
639                     return 0;
640                 }else if(nmhdr->hwndFrom == info->search.hwndList) {
641                     HWND hwndList = info->search.hwndList;
642                     LVITEMW lvItem;
643
644                     lvItem.iItem = (int) SendMessageW(hwndList, LVM_GETSELECTIONMARK, 0, 0);
645                     lvItem.mask = TVIF_PARAM;
646                     SendMessageW(hwndList, LVM_GETITEMW, 0, (LPARAM)&lvItem);
647                     OnTopicChange(info, (void*) lvItem.lParam);
648                     return 0;
649                 }
650                 break;
651             }
652             }
653             break;
654         }
655         break;
656     }
657     default:
658         return DefWindowProcW(hWnd, message, wParam, lParam);
659     }
660
661     return 0;
662 }
663
664 static void HH_RegisterChildWndClass(HHInfo *pHHInfo)
665 {
666     WNDCLASSEXW wcex;
667
668     wcex.cbSize         = sizeof(WNDCLASSEXW);
669     wcex.style          = 0;
670     wcex.lpfnWndProc    = Child_WndProc;
671     wcex.cbClsExtra     = 0;
672     wcex.cbWndExtra     = 0;
673     wcex.hInstance      = hhctrl_hinstance;
674     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
675     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
676     wcex.hbrBackground  = (HBRUSH)(COLOR_BTNFACE + 1);
677     wcex.lpszMenuName   = NULL;
678     wcex.lpszClassName  = szChildClass;
679     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
680
681     RegisterClassExW(&wcex);
682 }
683
684 /* Toolbar */
685
686 #define ICON_SIZE   20
687
688 static void DisplayPopupMenu(HHInfo *info)
689 {
690     HMENU menu, submenu;
691     TBBUTTONINFOW button;
692     MENUITEMINFOW item;
693     POINT coords;
694     RECT rect;
695     DWORD index;
696
697     menu = LoadMenuW(hhctrl_hinstance, MAKEINTRESOURCEW(MENU_POPUP));
698
699     if (!menu)
700         return;
701
702     submenu = GetSubMenu(menu, 0);
703
704     /* Update the Show/Hide menu item */
705     item.cbSize = sizeof(MENUITEMINFOW);
706     item.fMask = MIIM_FTYPE | MIIM_STATE | MIIM_STRING;
707     item.fType = MFT_STRING;
708     item.fState = MF_ENABLED;
709
710     if (info->WinType.fNotExpanded)
711         item.dwTypeData = HH_LoadString(IDS_SHOWTABS);
712     else
713         item.dwTypeData = HH_LoadString(IDS_HIDETABS);
714
715     SetMenuItemInfoW(submenu, IDTB_EXPAND, FALSE, &item);
716     heap_free(item.dwTypeData);
717
718     /* Find the index toolbar button */
719     button.cbSize = sizeof(TBBUTTONINFOW);
720     button.dwMask = TBIF_COMMAND;
721     index = SendMessageW(info->WinType.hwndToolBar, TB_GETBUTTONINFOW, IDTB_OPTIONS, (LPARAM) &button);
722
723     if (index == -1)
724        return;
725
726     /* Get position */
727     SendMessageW(info->WinType.hwndToolBar, TB_GETITEMRECT, index, (LPARAM) &rect);
728
729     coords.x = rect.left;
730     coords.y = rect.bottom;
731
732     ClientToScreen(info->WinType.hwndToolBar, &coords);
733     TrackPopupMenu(submenu, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_LEFTBUTTON | TPM_NOANIMATION, coords.x, coords.y, 0, info->WinType.hwndHelp, NULL);
734 }
735
736 static void TB_OnClick(HWND hWnd, DWORD dwID)
737 {
738     HHInfo *info = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
739
740     switch (dwID)
741     {
742         case IDTB_STOP:
743             DoPageAction(info, WB_STOP);
744             break;
745         case IDTB_REFRESH:
746             DoPageAction(info, WB_REFRESH);
747             break;
748         case IDTB_BACK:
749             DoPageAction(info, WB_GOBACK);
750             break;
751         case IDTB_HOME:
752             NavigateToChm(info, info->pCHMInfo->szFile, info->WinType.pszHome);
753             break;
754         case IDTB_FORWARD:
755             DoPageAction(info, WB_GOFORWARD);
756             break;
757         case IDTB_PRINT:
758             DoPageAction(info, WB_PRINT);
759             break;
760         case IDTB_EXPAND:
761         case IDTB_CONTRACT:
762             ExpandContract(info);
763             break;
764         case IDTB_SYNC:
765             DoSync(info);
766             break;
767         case IDTB_OPTIONS:
768             DisplayPopupMenu(info);
769             break;
770         case IDTB_NOTES:
771         case IDTB_CONTENTS:
772         case IDTB_INDEX:
773         case IDTB_SEARCH:
774         case IDTB_HISTORY:
775         case IDTB_FAVORITES:
776             /* These are officially unimplemented as of the Windows 7 SDK */
777             break;
778         case IDTB_BROWSE_FWD:
779         case IDTB_BROWSE_BACK:
780         case IDTB_JUMP1:
781         case IDTB_JUMP2:
782         case IDTB_CUSTOMIZE:
783         case IDTB_ZOOM:
784         case IDTB_TOC_NEXT:
785         case IDTB_TOC_PREV:
786             break;
787     }
788 }
789
790 static void TB_AddButton(TBBUTTON *pButtons, DWORD dwIndex, DWORD dwID, DWORD dwBitmap)
791 {
792     pButtons[dwIndex].iBitmap = dwBitmap;
793     pButtons[dwIndex].idCommand = dwID;
794     pButtons[dwIndex].fsState = TBSTATE_ENABLED;
795     pButtons[dwIndex].fsStyle = BTNS_BUTTON;
796     pButtons[dwIndex].dwData = 0;
797     pButtons[dwIndex].iString = 0;
798 }
799
800 static void TB_AddButtonsFromFlags(HHInfo *pHHInfo, TBBUTTON *pButtons, DWORD dwButtonFlags, LPDWORD pdwNumButtons)
801 {
802     int nHistBitmaps = 0, nStdBitmaps = 0, nHHBitmaps = 0;
803     HWND hToolbar = pHHInfo->WinType.hwndToolBar;
804     TBADDBITMAP tbAB;
805     DWORD unsupported;
806
807     /* Common bitmaps */
808     tbAB.hInst = HINST_COMMCTRL;
809     tbAB.nID = IDB_HIST_LARGE_COLOR;
810     nHistBitmaps = SendMessageW(hToolbar, TB_ADDBITMAP, 0, (LPARAM)&tbAB);
811     tbAB.nID = IDB_STD_LARGE_COLOR;
812     nStdBitmaps = SendMessageW(hToolbar, TB_ADDBITMAP, 0, (LPARAM)&tbAB);
813     /* hhctrl.ocx bitmaps */
814     tbAB.hInst = hhctrl_hinstance;
815     tbAB.nID = IDB_HHTOOLBAR;
816     nHHBitmaps = SendMessageW(hToolbar, TB_ADDBITMAP, HHTB_NUMBITMAPS, (LPARAM)&tbAB);
817
818     *pdwNumButtons = 0;
819
820     unsupported = dwButtonFlags & (HHWIN_BUTTON_BROWSE_FWD |
821         HHWIN_BUTTON_BROWSE_BCK | HHWIN_BUTTON_NOTES | HHWIN_BUTTON_CONTENTS |
822         HHWIN_BUTTON_INDEX | HHWIN_BUTTON_SEARCH | HHWIN_BUTTON_HISTORY |
823         HHWIN_BUTTON_FAVORITES | HHWIN_BUTTON_JUMP1 | HHWIN_BUTTON_JUMP2 |
824         HHWIN_BUTTON_ZOOM | HHWIN_BUTTON_TOC_NEXT | HHWIN_BUTTON_TOC_PREV);
825     if (unsupported)
826         FIXME("got asked for unsupported buttons: %06x\n", unsupported);
827
828     if (dwButtonFlags & HHWIN_BUTTON_EXPAND)
829     {
830         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_EXPAND, nHHBitmaps + HHTB_EXPAND);
831         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_CONTRACT, nHHBitmaps + HHTB_CONTRACT);
832
833         if (pHHInfo->WinType.fNotExpanded)
834             pButtons[1].fsState |= TBSTATE_HIDDEN;
835         else
836             pButtons[0].fsState |= TBSTATE_HIDDEN;
837     }
838
839     if (dwButtonFlags & HHWIN_BUTTON_BACK)
840         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_BACK, nHistBitmaps + HIST_BACK);
841
842     if (dwButtonFlags & HHWIN_BUTTON_FORWARD)
843         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_FORWARD, nHistBitmaps + HIST_FORWARD);
844
845     if (dwButtonFlags & HHWIN_BUTTON_STOP)
846         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_STOP, nHHBitmaps + HHTB_STOP);
847
848     if (dwButtonFlags & HHWIN_BUTTON_REFRESH)
849         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_REFRESH, nHHBitmaps + HHTB_REFRESH);
850
851     if (dwButtonFlags & HHWIN_BUTTON_HOME)
852         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_HOME, nHHBitmaps + HHTB_HOME);
853
854     if (dwButtonFlags & HHWIN_BUTTON_SYNC)
855         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_SYNC, nHHBitmaps + HHTB_SYNC);
856
857     if (dwButtonFlags & HHWIN_BUTTON_OPTIONS)
858         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_OPTIONS, nStdBitmaps + STD_PROPERTIES);
859
860     if (dwButtonFlags & HHWIN_BUTTON_PRINT)
861         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_PRINT, nStdBitmaps + STD_PRINT);
862 }
863
864 static BOOL HH_AddToolbar(HHInfo *pHHInfo)
865 {
866     HWND hToolbar;
867     HWND hwndParent = pHHInfo->WinType.hwndHelp;
868     DWORD toolbarFlags;
869     TBBUTTON buttons[IDTB_TOC_PREV - IDTB_EXPAND];
870     DWORD dwStyles, dwExStyles;
871     DWORD dwNumButtons, dwIndex;
872
873     if (pHHInfo->WinType.fsWinProperties & HHWIN_PARAM_TB_FLAGS)
874         toolbarFlags = pHHInfo->WinType.fsToolBarFlags;
875     else
876         toolbarFlags = HHWIN_DEF_BUTTONS;
877
878     dwStyles = WS_CHILDWINDOW | WS_VISIBLE | TBSTYLE_FLAT |
879                TBSTYLE_WRAPABLE | TBSTYLE_TOOLTIPS | CCS_NODIVIDER;
880     dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
881
882     hToolbar = CreateWindowExW(dwExStyles, TOOLBARCLASSNAMEW, NULL, dwStyles,
883                                0, 0, 0, 0, hwndParent, NULL,
884                                hhctrl_hinstance, NULL);
885     if (!hToolbar)
886         return FALSE;
887     pHHInfo->WinType.hwndToolBar = hToolbar;
888
889     SendMessageW(hToolbar, TB_SETBITMAPSIZE, 0, MAKELONG(ICON_SIZE, ICON_SIZE));
890     SendMessageW(hToolbar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
891     SendMessageW(hToolbar, WM_SETFONT, (WPARAM)pHHInfo->hFont, TRUE);
892
893     TB_AddButtonsFromFlags(pHHInfo, buttons, toolbarFlags, &dwNumButtons);
894
895     for (dwIndex = 0; dwIndex < dwNumButtons; dwIndex++)
896     {
897         LPWSTR szBuf = HH_LoadString(buttons[dwIndex].idCommand);
898         DWORD dwLen = strlenW(szBuf);
899         szBuf[dwLen + 1] = 0; /* Double-null terminate */
900
901         buttons[dwIndex].iString = (DWORD)SendMessageW(hToolbar, TB_ADDSTRINGW, 0, (LPARAM)szBuf);
902         heap_free(szBuf);
903     }
904
905     SendMessageW(hToolbar, TB_ADDBUTTONSW, dwNumButtons, (LPARAM)buttons);
906     SendMessageW(hToolbar, TB_AUTOSIZE, 0, 0);
907     ShowWindow(hToolbar, SW_SHOW);
908
909     return TRUE;
910 }
911
912 /* Navigation Pane */
913
914 static void NP_GetNavigationRect(HHInfo *pHHInfo, RECT *rc)
915 {
916     HWND hwndParent = pHHInfo->WinType.hwndHelp;
917     HWND hwndToolbar = pHHInfo->WinType.hwndToolBar;
918     RECT rectWND, rectTB;
919
920     GetClientRect(hwndParent, &rectWND);
921     GetClientRect(hwndToolbar, &rectTB);
922
923     rc->left = 0;
924     rc->top = rectTB.bottom;
925     rc->bottom = rectWND.bottom - rectTB.bottom;
926
927     if (!(pHHInfo->WinType.fsValidMembers & HHWIN_PARAM_NAV_WIDTH) &&
928           pHHInfo->WinType.iNavWidth == 0)
929     {
930         pHHInfo->WinType.iNavWidth = WINTYPE_DEFAULT_NAVWIDTH;
931     }
932
933     rc->right = pHHInfo->WinType.iNavWidth;
934 }
935
936 static DWORD NP_CreateTab(HINSTANCE hInstance, HWND hwndTabCtrl, DWORD index)
937 {
938     TCITEMW tie;
939     LPWSTR tabText = HH_LoadString(index);
940     DWORD ret;
941
942     tie.mask = TCIF_TEXT;
943     tie.pszText = tabText;
944
945     ret = SendMessageW( hwndTabCtrl, TCM_INSERTITEMW, index, (LPARAM)&tie );
946
947     heap_free(tabText);
948     return ret;
949 }
950
951 static BOOL HH_AddNavigationPane(HHInfo *info)
952 {
953     HWND hWnd, hwndTabCtrl;
954     HWND hwndParent = info->WinType.hwndHelp;
955     DWORD dwStyles = WS_CHILDWINDOW;
956     DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
957     RECT rc;
958
959     if (!info->WinType.fNotExpanded)
960         dwStyles |= WS_VISIBLE;
961
962     NP_GetNavigationRect(info, &rc);
963
964     hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
965                            rc.left, rc.top, rc.right, rc.bottom,
966                            hwndParent, NULL, hhctrl_hinstance, NULL);
967     if (!hWnd)
968         return FALSE;
969
970     SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)info);
971
972     hwndTabCtrl = CreateWindowExW(dwExStyles, WC_TABCONTROLW, szEmpty, dwStyles | WS_VISIBLE,
973                                   0, TAB_TOP_PADDING,
974                                   rc.right - TAB_RIGHT_PADDING,
975                                   rc.bottom - TAB_TOP_PADDING,
976                                   hWnd, NULL, hhctrl_hinstance, NULL);
977     if (!hwndTabCtrl)
978         return FALSE;
979
980     if (*info->WinType.pszToc)
981         info->tabs[TAB_CONTENTS].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_CONTENTS);
982
983     if (*info->WinType.pszIndex)
984         info->tabs[TAB_INDEX].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_INDEX);
985
986     if (info->WinType.fsWinProperties & HHWIN_PROP_TAB_SEARCH)
987         info->tabs[TAB_SEARCH].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_SEARCH);
988
989     if (info->WinType.fsWinProperties & HHWIN_PROP_TAB_FAVORITES)
990         info->tabs[TAB_FAVORITES].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_FAVORITES);
991
992     SendMessageW(hwndTabCtrl, WM_SETFONT, (WPARAM)info->hFont, TRUE);
993
994     info->hwndTabCtrl = hwndTabCtrl;
995     info->WinType.hwndNavigation = hWnd;
996     return TRUE;
997 }
998
999 /* HTML Pane */
1000
1001 static void HP_GetHTMLRect(HHInfo *info, RECT *rc)
1002 {
1003     RECT rectTB, rectWND, rectNP, rectSB;
1004
1005     GetClientRect(info->WinType.hwndHelp, &rectWND);
1006     GetClientRect(info->WinType.hwndToolBar, &rectTB);
1007     GetClientRect(info->hwndSizeBar, &rectSB);
1008
1009     if (info->WinType.fNotExpanded)
1010         rc->left = 0;
1011     else
1012     {
1013         GetClientRect(info->WinType.hwndNavigation, &rectNP);
1014         rc->left = rectNP.right + rectSB.right;
1015     }
1016
1017     rc->top = rectTB.bottom;
1018     rc->right = rectWND.right - rc->left;
1019     rc->bottom = rectWND.bottom - rectTB.bottom;
1020 }
1021
1022 static BOOL HH_AddHTMLPane(HHInfo *pHHInfo)
1023 {
1024     HWND hWnd;
1025     HWND hwndParent = pHHInfo->WinType.hwndHelp;
1026     DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN;
1027     DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_CLIENTEDGE;
1028     RECT rc;
1029
1030     HP_GetHTMLRect(pHHInfo, &rc);
1031
1032     hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
1033                            rc.left, rc.top, rc.right, rc.bottom,
1034                            hwndParent, NULL, hhctrl_hinstance, NULL);
1035     if (!hWnd)
1036         return FALSE;
1037
1038     if (!InitWebBrowser(pHHInfo, hWnd))
1039         return FALSE;
1040
1041     /* store the pointer to the HH info struct */
1042     SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
1043
1044     ShowWindow(hWnd, SW_SHOW);
1045     UpdateWindow(hWnd);
1046
1047     pHHInfo->WinType.hwndHTML = hWnd;
1048     return TRUE;
1049 }
1050
1051 static BOOL AddContentTab(HHInfo *info)
1052 {
1053     if(info->tabs[TAB_CONTENTS].id == -1)
1054         return TRUE; /* No "Contents" tab */
1055     info->tabs[TAB_CONTENTS].hwnd = CreateWindowExW(WS_EX_CLIENTEDGE, WC_TREEVIEWW,
1056            szEmpty, WS_CHILD | WS_BORDER | 0x25, 50, 50, 100, 100,
1057            info->WinType.hwndNavigation, NULL, hhctrl_hinstance, NULL);
1058     if(!info->tabs[TAB_CONTENTS].hwnd) {
1059         ERR("Could not create treeview control\n");
1060         return FALSE;
1061     }
1062
1063     ResizeTabChild(info, TAB_CONTENTS);
1064     ShowWindow(info->tabs[TAB_CONTENTS].hwnd, SW_SHOW);
1065
1066     return TRUE;
1067 }
1068
1069 static BOOL AddIndexTab(HHInfo *info)
1070 {
1071     char hidden_column[] = "Column";
1072     LVCOLUMNA lvc;
1073
1074     if(info->tabs[TAB_INDEX].id == -1)
1075         return TRUE; /* No "Index" tab */
1076     info->tabs[TAB_INDEX].hwnd = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW,
1077            szEmpty, WS_CHILD | WS_BORDER | LVS_SINGLESEL | LVS_REPORT | LVS_NOCOLUMNHEADER, 50, 50, 100, 100,
1078            info->WinType.hwndNavigation, NULL, hhctrl_hinstance, NULL);
1079     if(!info->tabs[TAB_INDEX].hwnd) {
1080         ERR("Could not create ListView control\n");
1081         return FALSE;
1082     }
1083     memset(&lvc, 0, sizeof(lvc));
1084     lvc.mask = LVCF_TEXT;
1085     lvc.pszText = hidden_column;
1086     if(SendMessageW(info->tabs[TAB_INDEX].hwnd, LVM_INSERTCOLUMNA, 0, (LPARAM) &lvc) == -1)
1087     {
1088         ERR("Could not create ListView column\n");
1089         return FALSE;
1090     }
1091
1092     ResizeTabChild(info, TAB_INDEX);
1093     ShowWindow(info->tabs[TAB_INDEX].hwnd, SW_HIDE);
1094
1095     return TRUE;
1096 }
1097
1098 static BOOL AddSearchTab(HHInfo *info)
1099 {
1100     HWND hwndList, hwndEdit, hwndContainer;
1101     char hidden_column[] = "Column";
1102     WNDPROC editWndProc;
1103     LVCOLUMNA lvc;
1104
1105     if(info->tabs[TAB_SEARCH].id == -1)
1106         return TRUE; /* No "Search" tab */
1107     hwndContainer = CreateWindowExW(WS_EX_CONTROLPARENT, szChildClass, szEmpty,
1108                                     WS_CHILD, 0, 0, 0, 0, info->WinType.hwndNavigation,
1109                                     NULL, hhctrl_hinstance, NULL);
1110     if(!hwndContainer) {
1111         ERR("Could not create search window container control.\n");
1112         return FALSE;
1113     }
1114     hwndEdit = CreateWindowExW(WS_EX_CLIENTEDGE, WC_EDITW, szEmpty, WS_CHILD
1115                                 | WS_VISIBLE | ES_LEFT | SS_NOTIFY, 0, 0, 0, 0,
1116                                hwndContainer, NULL, hhctrl_hinstance, NULL);
1117     if(!hwndEdit) {
1118         ERR("Could not create search ListView control.\n");
1119         return FALSE;
1120     }
1121     if(SendMessageW(hwndEdit, WM_SETFONT, (WPARAM) info->hFont, (LPARAM) FALSE) == -1)
1122     {
1123         ERR("Could not set font for edit control.\n");
1124         return FALSE;
1125     }
1126     editWndProc = (WNDPROC) SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC, (LONG_PTR)EditChild_WndProc);
1127     if(!editWndProc) {
1128         ERR("Could not redirect messages for edit control.\n");
1129         return FALSE;
1130     }
1131     SetWindowLongPtrW(hwndEdit, GWLP_USERDATA, (LONG_PTR)editWndProc);
1132     hwndList = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW, szEmpty,
1133                                WS_CHILD | WS_VISIBLE | WS_BORDER | LVS_SINGLESEL
1134                                 | LVS_REPORT | LVS_NOCOLUMNHEADER, 0, 0, 0, 0,
1135                                hwndContainer, NULL, hhctrl_hinstance, NULL);
1136     if(!hwndList) {
1137         ERR("Could not create search ListView control.\n");
1138         return FALSE;
1139     }
1140     memset(&lvc, 0, sizeof(lvc));
1141     lvc.mask = LVCF_TEXT;
1142     lvc.pszText = hidden_column;
1143     if(SendMessageW(hwndList, LVM_INSERTCOLUMNA, 0, (LPARAM) &lvc) == -1)
1144     {
1145         ERR("Could not create ListView column\n");
1146         return FALSE;
1147     }
1148
1149     info->search.hwndEdit = hwndEdit;
1150     info->search.hwndList = hwndList;
1151     info->search.hwndContainer = hwndContainer;
1152     info->tabs[TAB_SEARCH].hwnd = hwndContainer;
1153
1154     SetWindowLongPtrW(hwndContainer, GWLP_USERDATA, (LONG_PTR)info);
1155
1156     ResizeTabChild(info, TAB_SEARCH);
1157
1158     return TRUE;
1159 }
1160
1161 /* The Index tab's sub-topic popup */
1162
1163 static void ResizePopupChild(HHInfo *info)
1164 {
1165     int scroll_width = GetSystemMetrics(SM_CXVSCROLL);
1166     int border_width = GetSystemMetrics(SM_CXBORDER);
1167     int edge_width = GetSystemMetrics(SM_CXEDGE);
1168     INT width, height;
1169     RECT rect;
1170
1171     if(!info)
1172         return;
1173
1174     GetClientRect(info->popup.hwndPopup, &rect);
1175     SetWindowPos(info->popup.hwndCallback, HWND_TOP, 0, 0,
1176                  rect.right, rect.bottom, SWP_NOMOVE);
1177
1178     rect.left = TAB_MARGIN;
1179     rect.top = TAB_TOP_PADDING + TAB_MARGIN;
1180     rect.right -= TAB_RIGHT_PADDING + TAB_MARGIN;
1181     rect.bottom -= TAB_MARGIN;
1182     width = rect.right-rect.left;
1183     height = rect.bottom-rect.top;
1184
1185     SetWindowPos(info->popup.hwndList, NULL, rect.left, rect.top, width, height,
1186                  SWP_NOZORDER | SWP_NOACTIVATE);
1187
1188     SendMessageW(info->popup.hwndList, LVM_SETCOLUMNWIDTH, 0,
1189                  width-scroll_width-2*border_width-2*edge_width);
1190 }
1191
1192 static LRESULT CALLBACK HelpPopup_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1193 {
1194     HHInfo *info = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
1195
1196     switch (message)
1197     {
1198     case WM_SIZE:
1199         ResizePopupChild(info);
1200         return 0;
1201     case WM_DESTROY:
1202         DestroyWindow(hWnd);
1203         return 0;
1204     case WM_CLOSE:
1205         ShowWindow(hWnd, SW_HIDE);
1206         return 0;
1207
1208     default:
1209         return DefWindowProcW(hWnd, message, wParam, lParam);
1210     }
1211
1212     return 0;
1213 }
1214
1215 static LRESULT CALLBACK PopupChild_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1216 {
1217     switch (message)
1218     {
1219     case WM_NOTIFY: {
1220         NMHDR *nmhdr = (NMHDR*)lParam;
1221         switch(nmhdr->code)
1222         {
1223         case NM_DBLCLK: {
1224             HHInfo *info = (HHInfo*)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
1225             IndexSubItem *iter;
1226
1227             if(info == 0 || lParam == 0)
1228                 return 0;
1229             iter = (IndexSubItem*) ((NMITEMACTIVATE *)lParam)->lParam;
1230             if(iter == 0)
1231                 return 0;
1232             NavigateToChm(info, info->index->merge.chm_file, iter->local);
1233             ShowWindow(info->popup.hwndPopup, SW_HIDE);
1234             return 0;
1235         }
1236         case NM_RETURN: {
1237             HHInfo *info = (HHInfo*)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
1238             IndexSubItem *iter;
1239             LVITEMW lvItem;
1240
1241             if(info == 0)
1242                 return 0;
1243
1244             lvItem.iItem = (int) SendMessageW(info->popup.hwndList, LVM_GETSELECTIONMARK, 0, 0);
1245             lvItem.mask = TVIF_PARAM;
1246             SendMessageW(info->popup.hwndList, LVM_GETITEMW, 0, (LPARAM)&lvItem);
1247             iter = (IndexSubItem*) lvItem.lParam;
1248             NavigateToChm(info, info->index->merge.chm_file, iter->local);
1249             ShowWindow(info->popup.hwndPopup, SW_HIDE);
1250             return 0;
1251         }
1252         }
1253         break;
1254     }
1255     default:
1256         return DefWindowProcW(hWnd, message, wParam, lParam);
1257     }
1258
1259     return 0;
1260 }
1261
1262 static BOOL AddIndexPopup(HHInfo *info)
1263 {
1264     static const WCHAR szPopupChildClass[] = {'H','H',' ','P','o','p','u','p',' ','C','h','i','l','d',0};
1265     static const WCHAR windowCaptionW[] = {'S','e','l','e','c','t',' ','T','o','p','i','c',':',0};
1266     static const WCHAR windowClassW[] = {'H','H',' ','P','o','p','u','p',0};
1267     HWND hwndList, hwndPopup, hwndCallback;
1268     char hidden_column[] = "Column";
1269     WNDCLASSEXW wcex;
1270     LVCOLUMNA lvc;
1271
1272     if(info->tabs[TAB_INDEX].id == -1)
1273         return TRUE; /* No "Index" tab */
1274
1275     wcex.cbSize         = sizeof(WNDCLASSEXW);
1276     wcex.style          = CS_HREDRAW | CS_VREDRAW;
1277     wcex.lpfnWndProc    = HelpPopup_WndProc;
1278     wcex.cbClsExtra     = 0;
1279     wcex.cbWndExtra     = 0;
1280     wcex.hInstance      = hhctrl_hinstance;
1281     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1282     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
1283     wcex.hbrBackground  = (HBRUSH)(COLOR_MENU + 1);
1284     wcex.lpszMenuName   = NULL;
1285     wcex.lpszClassName  = windowClassW;
1286     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1287     RegisterClassExW(&wcex);
1288
1289     wcex.cbSize         = sizeof(WNDCLASSEXW);
1290     wcex.style          = 0;
1291     wcex.lpfnWndProc    = PopupChild_WndProc;
1292     wcex.cbClsExtra     = 0;
1293     wcex.cbWndExtra     = 0;
1294     wcex.hInstance      = hhctrl_hinstance;
1295     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1296     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
1297     wcex.hbrBackground  = (HBRUSH)(COLOR_BTNFACE + 1);
1298     wcex.lpszMenuName   = NULL;
1299     wcex.lpszClassName  = szPopupChildClass;
1300     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1301     RegisterClassExW(&wcex);
1302
1303     hwndPopup = CreateWindowExW(WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_APPWINDOW
1304                                  | WS_EX_WINDOWEDGE | WS_EX_RIGHTSCROLLBAR,
1305                                 windowClassW, windowCaptionW, WS_POPUPWINDOW
1306                                  | WS_OVERLAPPEDWINDOW | WS_VISIBLE
1307                                  | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, CW_USEDEFAULT,
1308                                 CW_USEDEFAULT, 300, 200, info->WinType.hwndHelp,
1309                                 NULL, hhctrl_hinstance, NULL);
1310     if (!hwndPopup)
1311         return FALSE;
1312
1313     hwndCallback = CreateWindowExW(WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR,
1314                                    szPopupChildClass, szEmpty, WS_CHILDWINDOW | WS_VISIBLE,
1315                                    0, 0, 0, 0,
1316                                    hwndPopup, NULL, hhctrl_hinstance, NULL);
1317     if (!hwndCallback)
1318         return FALSE;
1319
1320     ShowWindow(hwndPopup, SW_HIDE);
1321     hwndList = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW, szEmpty,
1322                                WS_CHILD | WS_BORDER | LVS_SINGLESEL | LVS_REPORT
1323                                 | LVS_NOCOLUMNHEADER, 50, 50, 100, 100,
1324                                hwndCallback, NULL, hhctrl_hinstance, NULL);
1325     if(!hwndList) {
1326         ERR("Could not create popup ListView control\n");
1327         return FALSE;
1328     }
1329     memset(&lvc, 0, sizeof(lvc));
1330     lvc.mask = LVCF_TEXT;
1331     lvc.pszText = hidden_column;
1332     if(SendMessageW(hwndList, LVM_INSERTCOLUMNA, 0, (LPARAM) &lvc) == -1)
1333     {
1334         ERR("Could not create popup ListView column\n");
1335         return FALSE;
1336     }
1337
1338     info->popup.hwndCallback = hwndCallback;
1339     info->popup.hwndPopup = hwndPopup;
1340     info->popup.hwndList = hwndList;
1341     SetWindowLongPtrW(hwndPopup, GWLP_USERDATA, (LONG_PTR)info);
1342     SetWindowLongPtrW(hwndCallback, GWLP_USERDATA, (LONG_PTR)info);
1343
1344     ResizePopupChild(info);
1345     ShowWindow(hwndList, SW_SHOW);
1346
1347     return TRUE;
1348 }
1349
1350 /* Viewer Window */
1351
1352 static void ExpandContract(HHInfo *pHHInfo)
1353 {
1354     RECT r, nav;
1355
1356     pHHInfo->WinType.fNotExpanded = !pHHInfo->WinType.fNotExpanded;
1357     GetWindowRect(pHHInfo->WinType.hwndHelp, &r);
1358     NP_GetNavigationRect(pHHInfo, &nav);
1359
1360     /* hide/show both the nav bar and the size bar */
1361     if (pHHInfo->WinType.fNotExpanded)
1362     {
1363         ShowWindow(pHHInfo->WinType.hwndNavigation, SW_HIDE);
1364         ShowWindow(pHHInfo->hwndSizeBar, SW_HIDE);
1365         r.left = r.left + nav.right;
1366
1367         SendMessageW(pHHInfo->WinType.hwndToolBar, TB_HIDEBUTTON, IDTB_EXPAND, MAKELPARAM(FALSE, 0));
1368         SendMessageW(pHHInfo->WinType.hwndToolBar, TB_HIDEBUTTON, IDTB_CONTRACT, MAKELPARAM(TRUE, 0));
1369     }
1370     else
1371     {
1372         ShowWindow(pHHInfo->WinType.hwndNavigation, SW_SHOW);
1373         ShowWindow(pHHInfo->hwndSizeBar, SW_SHOW);
1374         r.left = r.left - nav.right;
1375
1376         SendMessageW(pHHInfo->WinType.hwndToolBar, TB_HIDEBUTTON, IDTB_EXPAND, MAKELPARAM(TRUE, 0));
1377         SendMessageW(pHHInfo->WinType.hwndToolBar, TB_HIDEBUTTON, IDTB_CONTRACT, MAKELPARAM(FALSE, 0));
1378     }
1379
1380     MoveWindow(pHHInfo->WinType.hwndHelp, r.left, r.top, r.right-r.left, r.bottom-r.top, TRUE);
1381 }
1382
1383 static LRESULT Help_OnSize(HWND hWnd)
1384 {
1385     HHInfo *pHHInfo = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
1386     DWORD dwSize;
1387     RECT rc;
1388
1389     if (!pHHInfo)
1390         return 0;
1391
1392     if (!pHHInfo->WinType.fNotExpanded)
1393     {
1394         NP_GetNavigationRect(pHHInfo, &rc);
1395         SetWindowPos(pHHInfo->WinType.hwndNavigation, HWND_TOP, 0, 0,
1396                      rc.right, rc.bottom, SWP_NOMOVE);
1397
1398         SB_GetSizeBarRect(pHHInfo, &rc);
1399         SetWindowPos(pHHInfo->hwndSizeBar, HWND_TOP, rc.left, rc.top,
1400                      rc.right, rc.bottom, SWP_SHOWWINDOW);
1401
1402     }
1403
1404     HP_GetHTMLRect(pHHInfo, &rc);
1405     SetWindowPos(pHHInfo->WinType.hwndHTML, HWND_TOP, rc.left, rc.top,
1406                  rc.right, rc.bottom, SWP_SHOWWINDOW);
1407
1408     /* Resize browser window taking the frame size into account */
1409     dwSize = GetSystemMetrics(SM_CXFRAME);
1410     ResizeWebBrowser(pHHInfo, rc.right - dwSize, rc.bottom - dwSize);
1411
1412     return 0;
1413 }
1414
1415 static LRESULT CALLBACK Help_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1416 {
1417     switch (message)
1418     {
1419     case WM_COMMAND:
1420         if (HIWORD(wParam) == BN_CLICKED)
1421             TB_OnClick(hWnd, LOWORD(wParam));
1422         break;
1423     case WM_SIZE:
1424         return Help_OnSize(hWnd);
1425     case WM_CLOSE:
1426         ReleaseHelpViewer((HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA));
1427         return 0;
1428     case WM_DESTROY:
1429         if(hh_process)
1430             PostQuitMessage(0);
1431         break;
1432
1433     default:
1434         return DefWindowProcW(hWnd, message, wParam, lParam);
1435     }
1436
1437     return 0;
1438 }
1439
1440 static BOOL HH_CreateHelpWindow(HHInfo *info)
1441 {
1442     HWND hWnd;
1443     RECT winPos = info->WinType.rcWindowPos;
1444     WNDCLASSEXW wcex;
1445     DWORD dwStyles, dwExStyles;
1446     DWORD x, y, width = 0, height = 0;
1447     LPCWSTR caption;
1448
1449     static const WCHAR windowClassW[] = {
1450         'H','H',' ', 'P','a','r','e','n','t',0
1451     };
1452
1453     wcex.cbSize         = sizeof(WNDCLASSEXW);
1454     wcex.style          = CS_HREDRAW | CS_VREDRAW;
1455     wcex.lpfnWndProc    = Help_WndProc;
1456     wcex.cbClsExtra     = 0;
1457     wcex.cbWndExtra     = 0;
1458     wcex.hInstance      = hhctrl_hinstance;
1459     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1460     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
1461     wcex.hbrBackground  = (HBRUSH)(COLOR_MENU + 1);
1462     wcex.lpszMenuName   = NULL;
1463     wcex.lpszClassName  = windowClassW;
1464     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1465
1466     RegisterClassExW(&wcex);
1467
1468     /* Read in window parameters if available */
1469     if (info->WinType.fsValidMembers & HHWIN_PARAM_STYLES)
1470         dwStyles = info->WinType.dwStyles | WS_OVERLAPPEDWINDOW;
1471     else
1472         dwStyles = WS_OVERLAPPEDWINDOW | WS_VISIBLE |
1473                    WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
1474
1475     if (info->WinType.fsValidMembers & HHWIN_PARAM_EXSTYLES)
1476         dwExStyles = info->WinType.dwExStyles;
1477     else
1478         dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_APPWINDOW |
1479                      WS_EX_WINDOWEDGE | WS_EX_RIGHTSCROLLBAR;
1480
1481     if (info->WinType.fsValidMembers & HHWIN_PARAM_RECT)
1482     {
1483         x = winPos.left;
1484         y = winPos.top;
1485         width = winPos.right - x;
1486         height = winPos.bottom - y;
1487     }
1488     if (!width || !height)
1489     {
1490         x = WINTYPE_DEFAULT_X;
1491         y = WINTYPE_DEFAULT_Y;
1492         width = WINTYPE_DEFAULT_WIDTH;
1493         height = WINTYPE_DEFAULT_HEIGHT;
1494     }
1495
1496     if (info->WinType.fNotExpanded)
1497     {
1498         if (!(info->WinType.fsValidMembers & HHWIN_PARAM_NAV_WIDTH) &&
1499               info->WinType.iNavWidth == 0)
1500         {
1501             info->WinType.iNavWidth = WINTYPE_DEFAULT_NAVWIDTH;
1502         }
1503
1504         x += info->WinType.iNavWidth;
1505         width -= info->WinType.iNavWidth;
1506     }
1507
1508
1509     caption = info->WinType.pszCaption;
1510     if (!*caption) caption = info->pCHMInfo->defTitle;
1511
1512     hWnd = CreateWindowExW(dwExStyles, windowClassW, caption,
1513                            dwStyles, x, y, width, height, NULL, NULL, hhctrl_hinstance, NULL);
1514     if (!hWnd)
1515         return FALSE;
1516
1517     ShowWindow(hWnd, SW_SHOW);
1518     UpdateWindow(hWnd);
1519
1520     /* store the pointer to the HH info struct */
1521     SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)info);
1522
1523     info->WinType.hwndHelp = hWnd;
1524     return TRUE;
1525 }
1526
1527 static void HH_CreateFont(HHInfo *pHHInfo)
1528 {
1529     LOGFONTW lf;
1530
1531     GetObjectW(GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONTW), &lf);
1532     lf.lfWeight = FW_NORMAL;
1533     lf.lfItalic = FALSE;
1534     lf.lfUnderline = FALSE;
1535
1536     pHHInfo->hFont = CreateFontIndirectW(&lf);
1537 }
1538
1539 static void HH_InitRequiredControls(DWORD dwControls)
1540 {
1541     INITCOMMONCONTROLSEX icex;
1542
1543     icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
1544     icex.dwICC = dwControls;
1545     InitCommonControlsEx(&icex);
1546 }
1547
1548 /* Creates the whole package */
1549 static BOOL CreateViewer(HHInfo *pHHInfo)
1550 {
1551     HH_CreateFont(pHHInfo);
1552
1553     if (!HH_CreateHelpWindow(pHHInfo))
1554         return FALSE;
1555
1556     HH_InitRequiredControls(ICC_BAR_CLASSES);
1557
1558     if (!HH_AddToolbar(pHHInfo))
1559         return FALSE;
1560
1561     HH_RegisterChildWndClass(pHHInfo);
1562
1563     if (!HH_AddNavigationPane(pHHInfo))
1564         return FALSE;
1565
1566     HH_RegisterSizeBarClass(pHHInfo);
1567
1568     if (!HH_AddSizeBar(pHHInfo))
1569         return FALSE;
1570
1571     if (!HH_AddHTMLPane(pHHInfo))
1572         return FALSE;
1573
1574     if (!AddContentTab(pHHInfo))
1575         return FALSE;
1576
1577     if (!AddIndexTab(pHHInfo))
1578         return FALSE;
1579
1580     if (!AddIndexPopup(pHHInfo))
1581         return FALSE;
1582
1583     if (!AddSearchTab(pHHInfo))
1584         return FALSE;
1585
1586     InitContent(pHHInfo);
1587     InitIndex(pHHInfo);
1588
1589     return TRUE;
1590 }
1591
1592 void ReleaseHelpViewer(HHInfo *info)
1593 {
1594     TRACE("(%p)\n", info);
1595
1596     if (!info)
1597         return;
1598
1599     /* Free allocated strings */
1600     heap_free(info->pszType);
1601     heap_free(info->pszCaption);
1602     heap_free(info->pszToc);
1603     heap_free(info->pszIndex);
1604     heap_free(info->pszFile);
1605     heap_free(info->pszHome);
1606     heap_free(info->pszJump1);
1607     heap_free(info->pszJump2);
1608     heap_free(info->pszUrlJump1);
1609     heap_free(info->pszUrlJump2);
1610
1611     if (info->pCHMInfo)
1612         CloseCHM(info->pCHMInfo);
1613
1614     ReleaseWebBrowser(info);
1615     ReleaseContent(info);
1616     ReleaseIndex(info);
1617     ReleaseSearch(info);
1618
1619     if(info->WinType.hwndHelp)
1620         DestroyWindow(info->WinType.hwndHelp);
1621
1622     heap_free(info);
1623     OleUninitialize();
1624 }
1625
1626 HHInfo *CreateHelpViewer(LPCWSTR filename)
1627 {
1628     HHInfo *info = heap_alloc_zero(sizeof(HHInfo));
1629     int i;
1630
1631     /* Set the invalid tab ID (-1) as the default value for all
1632      * of the tabs, this matches a failed TCM_INSERTITEM call.
1633      */
1634     for(i=0;i<sizeof(info->tabs)/sizeof(HHTab);i++)
1635         info->tabs[i].id = -1;
1636
1637     OleInitialize(NULL);
1638
1639     info->pCHMInfo = OpenCHM(filename);
1640     if(!info->pCHMInfo) {
1641         ReleaseHelpViewer(info);
1642         return NULL;
1643     }
1644
1645     if (!LoadWinTypeFromCHM(info)) {
1646         ReleaseHelpViewer(info);
1647         return NULL;
1648     }
1649
1650     if(!CreateViewer(info)) {
1651         ReleaseHelpViewer(info);
1652         return NULL;
1653     }
1654
1655     return info;
1656 }