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