hhctrl.ocx: Add widgets for the Search tab.
[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  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "hhctrl.h"
23
24 #include "wingdi.h"
25 #include "commctrl.h"
26 #include "wininet.h"
27
28 #include "wine/debug.h"
29
30 #include "resource.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(htmlhelp);
33
34 static LRESULT Help_OnSize(HWND hWnd);
35
36 /* Window type defaults */
37
38 #define WINTYPE_DEFAULT_X           280
39 #define WINTYPE_DEFAULT_Y           100
40 #define WINTYPE_DEFAULT_WIDTH       740
41 #define WINTYPE_DEFAULT_HEIGHT      640
42 #define WINTYPE_DEFAULT_NAVWIDTH    250
43
44 #define TAB_TOP_PADDING     8
45 #define TAB_RIGHT_PADDING   4
46 #define TAB_MARGIN  8
47 #define EDIT_HEIGHT         20
48
49 static const WCHAR szEmpty[] = {0};
50
51 /* Loads a string from the resource file */
52 static LPWSTR HH_LoadString(DWORD dwID)
53 {
54     LPWSTR string = NULL;
55     LPCWSTR stringresource;
56     int iSize;
57
58     iSize = LoadStringW(hhctrl_hinstance, dwID, (LPWSTR)&stringresource, 0);
59
60     string = heap_alloc((iSize + 2) * sizeof(WCHAR)); /* some strings (tab text) needs double-null termination */
61     memcpy(string, stringresource, iSize*sizeof(WCHAR));
62     string[iSize] = 0;
63
64     return string;
65 }
66
67 static HRESULT navigate_url(HHInfo *info, LPCWSTR surl)
68 {
69     VARIANT url;
70     HRESULT hres;
71
72     TRACE("%s\n", debugstr_w(surl));
73
74     V_VT(&url) = VT_BSTR;
75     V_BSTR(&url) = SysAllocString(surl);
76
77     hres = IWebBrowser2_Navigate2(info->web_browser, &url, 0, 0, 0, 0);
78
79     VariantClear(&url);
80
81     if(FAILED(hres))
82         TRACE("Navigation failed: %08x\n", hres);
83
84     return hres;
85 }
86
87 BOOL NavigateToUrl(HHInfo *info, LPCWSTR surl)
88 {
89     ChmPath chm_path;
90     BOOL ret;
91     HRESULT hres;
92
93     static const WCHAR url_indicator[] = {':', '/', '/', 0};
94
95     TRACE("%s\n", debugstr_w(surl));
96
97     if (strstrW(surl, url_indicator)) {
98         hres = navigate_url(info, surl);
99         if(SUCCEEDED(hres))
100             return TRUE;
101     } /* look up in chm if it doesn't look like a full url */
102
103     SetChmPath(&chm_path, info->pCHMInfo->szFile, surl);
104     ret = NavigateToChm(info, chm_path.chm_file, chm_path.chm_index);
105
106     heap_free(chm_path.chm_file);
107     heap_free(chm_path.chm_index);
108
109     return ret;
110 }
111
112 BOOL NavigateToChm(HHInfo *info, LPCWSTR file, LPCWSTR index)
113 {
114     WCHAR buf[INTERNET_MAX_URL_LENGTH];
115     WCHAR full_path[MAX_PATH];
116     LPWSTR ptr;
117
118     static const WCHAR url_format[] =
119         {'m','k',':','@','M','S','I','T','S','t','o','r','e',':','%','s',':',':','%','s','%','s',0};
120     static const WCHAR slash[] = {'/',0};
121     static const WCHAR empty[] = {0};
122
123     TRACE("%p %s %s\n", info, debugstr_w(file), debugstr_w(index));
124
125     if (!info->web_browser)
126         return FALSE;
127
128     if(!GetFullPathNameW(file, sizeof(full_path)/sizeof(full_path[0]), full_path, NULL)) {
129         WARN("GetFullPathName failed: %u\n", GetLastError());
130         return FALSE;
131     }
132
133     wsprintfW(buf, url_format, full_path, (!index || index[0] == '/') ? empty : slash, index);
134
135     /* FIXME: HACK */
136     if((ptr = strchrW(buf, '#')))
137        *ptr = 0;
138
139     return SUCCEEDED(navigate_url(info, buf));
140 }
141
142 /* Size Bar */
143
144 #define SIZEBAR_WIDTH   4
145
146 static const WCHAR szSizeBarClass[] = {
147     'H','H',' ','S','i','z','e','B','a','r',0
148 };
149
150 /* Draw the SizeBar */
151 static void SB_OnPaint(HWND hWnd)
152 {
153     PAINTSTRUCT ps;
154     HDC hdc;
155     RECT rc;
156     
157     hdc = BeginPaint(hWnd, &ps);
158
159     GetClientRect(hWnd, &rc);
160
161     /* dark frame */
162     rc.right += 1;
163     rc.bottom -= 1;
164     FrameRect(hdc, &rc, GetStockObject(GRAY_BRUSH));
165
166     /* white highlight */
167     SelectObject(hdc, GetStockObject(WHITE_PEN));
168     MoveToEx(hdc, rc.right, 1, NULL);
169     LineTo(hdc, 1, 1);
170     LineTo(hdc, 1, rc.bottom - 1);
171
172     
173     MoveToEx(hdc, 0, rc.bottom, NULL);
174     LineTo(hdc, rc.right, rc.bottom);
175
176     EndPaint(hWnd, &ps);
177 }
178
179 static void SB_OnLButtonDown(HWND hWnd, WPARAM wParam, LPARAM lParam)
180 {
181     SetCapture(hWnd);
182 }
183
184 static void SB_OnLButtonUp(HWND hWnd, WPARAM wParam, LPARAM lParam)
185 {
186     HHInfo *pHHInfo = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
187     POINT pt;
188
189     pt.x = (short)LOWORD(lParam);
190     pt.y = (short)HIWORD(lParam);
191
192     /* update the window sizes */
193     pHHInfo->WinType.iNavWidth += pt.x;
194     Help_OnSize(hWnd);
195
196     ReleaseCapture();
197 }
198
199 static void SB_OnMouseMove(HWND hWnd, WPARAM wParam, LPARAM lParam)
200 {
201     /* ignore WM_MOUSEMOVE if not dragging the SizeBar */
202     if (!(wParam & MK_LBUTTON))
203         return;
204 }
205
206 static LRESULT CALLBACK SizeBar_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
207 {
208     switch (message)
209     {
210         case WM_LBUTTONDOWN:
211             SB_OnLButtonDown(hWnd, wParam, lParam);
212             break;
213         case WM_LBUTTONUP:
214             SB_OnLButtonUp(hWnd, wParam, lParam);
215             break;
216         case WM_MOUSEMOVE:
217             SB_OnMouseMove(hWnd, wParam, lParam);
218             break;
219         case WM_PAINT:
220             SB_OnPaint(hWnd);
221             break;
222         default:
223             return DefWindowProcW(hWnd, message, wParam, lParam);
224     }
225
226     return 0;
227 }
228
229 static void HH_RegisterSizeBarClass(HHInfo *pHHInfo)
230 {
231     WNDCLASSEXW wcex;
232
233     wcex.cbSize         = sizeof(WNDCLASSEXW);
234     wcex.style          = 0;
235     wcex.lpfnWndProc    = SizeBar_WndProc;
236     wcex.cbClsExtra     = 0;
237     wcex.cbWndExtra     = 0;
238     wcex.hInstance      = hhctrl_hinstance;
239     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
240     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_SIZEWE);
241     wcex.hbrBackground  = (HBRUSH)(COLOR_MENU + 1);
242     wcex.lpszMenuName   = NULL;
243     wcex.lpszClassName  = szSizeBarClass;
244     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
245
246     RegisterClassExW(&wcex);
247 }
248
249 static void SB_GetSizeBarRect(HHInfo *info, RECT *rc)
250 {
251     RECT rectWND, rectTB, rectNP;
252
253     GetClientRect(info->WinType.hwndHelp, &rectWND);
254     GetClientRect(info->WinType.hwndToolBar, &rectTB);
255     GetClientRect(info->WinType.hwndNavigation, &rectNP);
256
257     rc->left = rectNP.right;
258     rc->top = rectTB.bottom;
259     rc->bottom = rectWND.bottom - rectTB.bottom;
260     rc->right = SIZEBAR_WIDTH;
261 }
262
263 static BOOL HH_AddSizeBar(HHInfo *pHHInfo)
264 {
265     HWND hWnd;
266     HWND hwndParent = pHHInfo->WinType.hwndHelp;
267     DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE | WS_OVERLAPPED;
268     DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
269     RECT rc;
270
271     SB_GetSizeBarRect(pHHInfo, &rc);
272
273     hWnd = CreateWindowExW(dwExStyles, szSizeBarClass, szEmpty, dwStyles,
274                            rc.left, rc.top, rc.right, rc.bottom,
275                            hwndParent, NULL, hhctrl_hinstance, NULL);
276     if (!hWnd)
277         return FALSE;
278
279     /* store the pointer to the HH info struct */
280     SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
281
282     pHHInfo->hwndSizeBar = hWnd;
283     return TRUE;
284 }
285
286 /* Child Window */
287
288 static const WCHAR szChildClass[] = {
289     'H','H',' ','C','h','i','l','d',0
290 };
291
292 static LRESULT Child_OnPaint(HWND hWnd)
293 {
294     PAINTSTRUCT ps;
295     HDC hdc;
296     RECT rc;
297
298     hdc = BeginPaint(hWnd, &ps);
299
300     /* Only paint the Navigation pane, identified by the fact
301      * that it has a child window
302      */
303     if (GetWindow(hWnd, GW_CHILD))
304     {
305         GetClientRect(hWnd, &rc);
306
307         /* set the border color */
308         SelectObject(hdc, GetStockObject(DC_PEN));
309         SetDCPenColor(hdc, GetSysColor(COLOR_BTNSHADOW));
310
311         /* Draw the top border */
312         LineTo(hdc, rc.right, 0);
313
314         SelectObject(hdc, GetStockObject(WHITE_PEN));
315         MoveToEx(hdc, 0, 1, NULL);
316         LineTo(hdc, rc.right, 1);
317     }
318
319     EndPaint(hWnd, &ps);
320
321     return 0;
322 }
323
324 static void ResizeTabChild(HHInfo *info, int tab)
325 {
326     HWND hwnd = info->tabs[tab].hwnd;
327     INT width, height;
328     RECT rect, tabrc;
329     DWORD cnt;
330
331     GetClientRect(info->WinType.hwndNavigation, &rect);
332     SendMessageW(info->hwndTabCtrl, TCM_GETITEMRECT, 0, (LPARAM)&tabrc);
333     cnt = SendMessageW(info->hwndTabCtrl, TCM_GETROWCOUNT, 0, 0);
334
335     rect.left = TAB_MARGIN;
336     rect.top = TAB_TOP_PADDING + cnt*(tabrc.bottom-tabrc.top) + TAB_MARGIN;
337     rect.right -= TAB_RIGHT_PADDING + TAB_MARGIN;
338     rect.bottom -= TAB_MARGIN;
339     width = rect.right-rect.left;
340     height = rect.bottom-rect.top;
341
342     SetWindowPos(hwnd, NULL, rect.left, rect.top, width, height,
343                  SWP_NOZORDER | SWP_NOACTIVATE);
344
345     switch (tab)
346     {
347     case TAB_INDEX: {
348         int scroll_width = GetSystemMetrics(SM_CXVSCROLL);
349         int border_width = GetSystemMetrics(SM_CXBORDER);
350         int edge_width = GetSystemMetrics(SM_CXEDGE);
351
352         /* Resize the tab widget column to perfectly fit the tab window and
353          * leave sufficient space for the scroll widget.
354          */
355         SendMessageW(info->tabs[TAB_INDEX].hwnd, LVM_SETCOLUMNWIDTH, 0,
356                      width-scroll_width-2*border_width-2*edge_width);
357
358         break;
359     }
360     case TAB_SEARCH: {
361         int scroll_width = GetSystemMetrics(SM_CXVSCROLL);
362         int border_width = GetSystemMetrics(SM_CXBORDER);
363         int edge_width = GetSystemMetrics(SM_CXEDGE);
364         int top_pos = 0;
365
366         SetWindowPos(info->search.hwndEdit, NULL, 0, top_pos, width,
367                       EDIT_HEIGHT, SWP_NOZORDER | SWP_NOACTIVATE);
368         top_pos += EDIT_HEIGHT + TAB_MARGIN;
369         SetWindowPos(info->search.hwndList, NULL, 0, top_pos, width,
370                       height-top_pos, SWP_NOZORDER | SWP_NOACTIVATE);
371         /* Resize the tab widget column to perfectly fit the tab window and
372          * leave sufficient space for the scroll widget.
373          */
374         SendMessageW(info->search.hwndList, LVM_SETCOLUMNWIDTH, 0,
375                      width-scroll_width-2*border_width-2*edge_width);
376
377         break;
378     }
379     }
380 }
381
382 static LRESULT Child_OnSize(HWND hwnd)
383 {
384     HHInfo *info = (HHInfo*)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
385     RECT rect;
386
387     if(!info || hwnd != info->WinType.hwndNavigation)
388         return 0;
389
390     GetClientRect(hwnd, &rect);
391     SetWindowPos(info->hwndTabCtrl, HWND_TOP, 0, 0,
392                  rect.right - TAB_RIGHT_PADDING,
393                  rect.bottom - TAB_TOP_PADDING, SWP_NOMOVE);
394
395     ResizeTabChild(info, TAB_CONTENTS);
396     ResizeTabChild(info, TAB_INDEX);
397     return 0;
398 }
399
400 static LRESULT OnTabChange(HWND hwnd)
401 {
402     HHInfo *info = (HHInfo*)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
403
404     TRACE("%p\n", hwnd);
405
406     if (!info)
407         return 0;
408
409     if(info->tabs[info->current_tab].hwnd)
410         ShowWindow(info->tabs[info->current_tab].hwnd, SW_HIDE);
411
412     info->current_tab = SendMessageW(info->hwndTabCtrl, TCM_GETCURSEL, 0, 0);
413
414     if(info->tabs[info->current_tab].hwnd)
415         ShowWindow(info->tabs[info->current_tab].hwnd, SW_SHOW);
416
417     return 0;
418 }
419
420 static LRESULT OnTopicChange(HHInfo *info, void *user_data)
421 {
422     LPCWSTR chmfile = NULL, name = NULL, local = NULL;
423     ContentItem *citer;
424     IndexItem *iiter;
425
426     if(!user_data || !info)
427         return 0;
428
429     switch (info->current_tab)
430     {
431     case TAB_CONTENTS:
432         citer = (ContentItem *) user_data;
433         name = citer->name;
434         local = citer->local;
435         while(citer) {
436             if(citer->merge.chm_file) {
437                 chmfile = citer->merge.chm_file;
438                 break;
439             }
440             citer = citer->parent;
441         }
442         break;
443     case TAB_INDEX:
444         iiter = (IndexItem *) user_data;
445         if(iiter->nItems == 0) {
446             FIXME("No entries for this item!\n");
447             return 0;
448         }
449         if(iiter->nItems > 1) {
450             int i = 0;
451             LVITEMW lvi;
452
453             SendMessageW(info->popup.hwndList, LVM_DELETEALLITEMS, 0, 0);
454             for(i=0;i<iiter->nItems;i++) {
455                 IndexSubItem *item = &iiter->items[i];
456                 WCHAR *name = iiter->keyword;
457
458                 if(item->name)
459                     name = item->name;
460                 memset(&lvi, 0, sizeof(lvi));
461                 lvi.iItem = i;
462                 lvi.mask = LVIF_TEXT|LVIF_PARAM;
463                 lvi.cchTextMax = strlenW(name)+1;
464                 lvi.pszText = name;
465                 lvi.lParam = (LPARAM) item;
466                 SendMessageW(info->popup.hwndList, LVM_INSERTITEMW, 0, (LPARAM)&lvi);
467             }
468             ShowWindow(info->popup.hwndPopup, SW_SHOW);
469             return 0;
470         }
471         name = iiter->items[0].name;
472         local = iiter->items[0].local;
473         chmfile = iiter->merge.chm_file;
474         break;
475     default:
476         FIXME("Unhandled operation for this tab!\n");
477         return 0;
478     }
479
480     if(!chmfile)
481     {
482         FIXME("No help file found for this item!\n");
483         return 0;
484     }
485
486     TRACE("name %s loal %s\n", debugstr_w(name), debugstr_w(local));
487
488     NavigateToChm(info, chmfile, local);
489     return 0;
490 }
491
492 /* Capture the Enter/Return key and send it up to Child_WndProc as an NM_RETURN message */
493 static LRESULT CALLBACK EditChild_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
494 {
495     WNDPROC editWndProc = (WNDPROC)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
496
497     if(message == WM_KEYUP && wParam == VK_RETURN)
498     {
499         NMHDR nmhdr;
500
501         nmhdr.hwndFrom = hWnd;
502         nmhdr.code = NM_RETURN;
503         SendMessageW(GetParent(GetParent(hWnd)), WM_NOTIFY, wParam, (LPARAM)&nmhdr);
504     }
505     return editWndProc(hWnd, message, wParam, lParam);
506 }
507
508 static LRESULT CALLBACK Child_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
509 {
510     switch (message)
511     {
512     case WM_PAINT:
513         return Child_OnPaint(hWnd);
514     case WM_SIZE:
515         return Child_OnSize(hWnd);
516     case WM_NOTIFY: {
517         HHInfo *info = (HHInfo*)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
518         NMHDR *nmhdr = (NMHDR*)lParam;
519
520         switch(nmhdr->code) {
521         case TCN_SELCHANGE:
522             return OnTabChange(hWnd);
523         case TVN_SELCHANGEDW:
524             return OnTopicChange(info, (void*)((NMTREEVIEWW *)lParam)->itemNew.lParam);
525         case NM_DBLCLK:
526             if(info && info->current_tab == TAB_INDEX)
527                 return OnTopicChange(info, (void*)((NMITEMACTIVATE *)lParam)->lParam);
528         case NM_RETURN:
529             if(!info)
530                 return 0;
531             switch(info->current_tab) {
532             case TAB_INDEX: {
533                 HWND hwndList = info->tabs[TAB_INDEX].hwnd;
534                 LVITEMW lvItem;
535
536                 lvItem.iItem = (int) SendMessageW(hwndList, LVM_GETSELECTIONMARK, 0, 0);
537                 lvItem.mask = TVIF_PARAM;
538                 ListView_GetItemW(hwndList, &lvItem);
539                 OnTopicChange(info, (void*) lvItem.lParam);
540                 return 0;
541             }
542             case TAB_SEARCH: {
543                 WCHAR needle[100];
544
545                 GetWindowTextW(info->search.hwndEdit, needle, sizeof(needle)/sizeof(WCHAR));
546                 FIXME("Search for text: %s\n", debugstr_w(needle));
547                 return 0;
548             }
549             }
550             break;
551         }
552         break;
553     }
554     default:
555         return DefWindowProcW(hWnd, message, wParam, lParam);
556     }
557
558     return 0;
559 }
560
561 static void HH_RegisterChildWndClass(HHInfo *pHHInfo)
562 {
563     WNDCLASSEXW wcex;
564
565     wcex.cbSize         = sizeof(WNDCLASSEXW);
566     wcex.style          = 0;
567     wcex.lpfnWndProc    = Child_WndProc;
568     wcex.cbClsExtra     = 0;
569     wcex.cbWndExtra     = 0;
570     wcex.hInstance      = hhctrl_hinstance;
571     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
572     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
573     wcex.hbrBackground  = (HBRUSH)(COLOR_BTNFACE + 1);
574     wcex.lpszMenuName   = NULL;
575     wcex.lpszClassName  = szChildClass;
576     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
577
578     RegisterClassExW(&wcex);
579 }
580
581 /* Toolbar */
582
583 #define ICON_SIZE   20
584
585 static void TB_OnClick(HWND hWnd, DWORD dwID)
586 {
587     HHInfo *info = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
588
589     switch (dwID)
590     {
591         case IDTB_STOP:
592             DoPageAction(info, WB_STOP);
593             break;
594         case IDTB_REFRESH:
595             DoPageAction(info, WB_REFRESH);
596             break;
597         case IDTB_BACK:
598             DoPageAction(info, WB_GOBACK);
599             break;
600         case IDTB_HOME:
601             NavigateToChm(info, info->pCHMInfo->szFile, info->WinType.pszHome);
602             break;
603         case IDTB_FORWARD:
604             DoPageAction(info, WB_GOFORWARD);
605             break;
606         case IDTB_EXPAND:
607         case IDTB_CONTRACT:
608         case IDTB_SYNC:
609         case IDTB_PRINT:
610         case IDTB_OPTIONS:
611         case IDTB_BROWSE_FWD:
612         case IDTB_BROWSE_BACK:
613         case IDTB_JUMP1:
614         case IDTB_JUMP2:
615         case IDTB_CUSTOMIZE:
616         case IDTB_ZOOM:
617         case IDTB_TOC_NEXT:
618         case IDTB_TOC_PREV:
619             break;
620     }
621 }
622
623 static void TB_AddButton(TBBUTTON *pButtons, DWORD dwIndex, DWORD dwID)
624 {
625     /* FIXME: Load the correct button bitmaps */
626     pButtons[dwIndex].iBitmap = STD_PRINT;
627     pButtons[dwIndex].idCommand = dwID;
628     pButtons[dwIndex].fsState = TBSTATE_ENABLED;
629     pButtons[dwIndex].fsStyle = BTNS_BUTTON;
630     pButtons[dwIndex].dwData = 0;
631     pButtons[dwIndex].iString = 0;
632 }
633
634 static void TB_AddButtonsFromFlags(TBBUTTON *pButtons, DWORD dwButtonFlags, LPDWORD pdwNumButtons)
635 {
636     *pdwNumButtons = 0;
637
638     if (dwButtonFlags & HHWIN_BUTTON_EXPAND)
639         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_EXPAND);
640
641     if (dwButtonFlags & HHWIN_BUTTON_BACK)
642         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_BACK);
643
644     if (dwButtonFlags & HHWIN_BUTTON_FORWARD)
645         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_FORWARD);
646
647     if (dwButtonFlags & HHWIN_BUTTON_STOP)
648         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_STOP);
649
650     if (dwButtonFlags & HHWIN_BUTTON_REFRESH)
651         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_REFRESH);
652
653     if (dwButtonFlags & HHWIN_BUTTON_HOME)
654         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_HOME);
655
656     if (dwButtonFlags & HHWIN_BUTTON_SYNC)
657         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_SYNC);
658
659     if (dwButtonFlags & HHWIN_BUTTON_OPTIONS)
660         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_OPTIONS);
661
662     if (dwButtonFlags & HHWIN_BUTTON_PRINT)
663         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_PRINT);
664
665     if (dwButtonFlags & HHWIN_BUTTON_JUMP1)
666         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_JUMP1);
667
668     if (dwButtonFlags & HHWIN_BUTTON_JUMP2)
669         TB_AddButton(pButtons,(*pdwNumButtons)++, IDTB_JUMP2);
670
671     if (dwButtonFlags & HHWIN_BUTTON_ZOOM)
672         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_ZOOM);
673
674     if (dwButtonFlags & HHWIN_BUTTON_TOC_NEXT)
675         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_TOC_NEXT);
676
677     if (dwButtonFlags & HHWIN_BUTTON_TOC_PREV)
678         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_TOC_PREV);
679 }
680
681 static BOOL HH_AddToolbar(HHInfo *pHHInfo)
682 {
683     HWND hToolbar;
684     HWND hwndParent = pHHInfo->WinType.hwndHelp;
685     DWORD toolbarFlags;
686     TBBUTTON buttons[IDTB_TOC_PREV - IDTB_EXPAND];
687     TBADDBITMAP tbAB;
688     DWORD dwStyles, dwExStyles;
689     DWORD dwNumButtons, dwIndex;
690
691     if (pHHInfo->WinType.fsWinProperties & HHWIN_PARAM_TB_FLAGS)
692         toolbarFlags = pHHInfo->WinType.fsToolBarFlags;
693     else
694         toolbarFlags = HHWIN_DEF_BUTTONS;
695
696     TB_AddButtonsFromFlags(buttons, toolbarFlags, &dwNumButtons);
697
698     dwStyles = WS_CHILDWINDOW | WS_VISIBLE | TBSTYLE_FLAT |
699                TBSTYLE_WRAPABLE | TBSTYLE_TOOLTIPS | CCS_NODIVIDER;
700     dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
701
702     hToolbar = CreateWindowExW(dwExStyles, TOOLBARCLASSNAMEW, NULL, dwStyles,
703                                0, 0, 0, 0, hwndParent, NULL,
704                                hhctrl_hinstance, NULL);
705     if (!hToolbar)
706         return FALSE;
707
708     SendMessageW(hToolbar, TB_SETBITMAPSIZE, 0, MAKELONG(ICON_SIZE, ICON_SIZE));
709     SendMessageW(hToolbar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
710     SendMessageW(hToolbar, WM_SETFONT, (WPARAM)pHHInfo->hFont, TRUE);
711
712     /* FIXME: Load correct icons for all buttons */
713     tbAB.hInst = HINST_COMMCTRL;
714     tbAB.nID = IDB_STD_LARGE_COLOR;
715     SendMessageW(hToolbar, TB_ADDBITMAP, 0, (LPARAM)&tbAB);
716
717     for (dwIndex = 0; dwIndex < dwNumButtons; dwIndex++)
718     {
719         LPWSTR szBuf = HH_LoadString(buttons[dwIndex].idCommand);
720         DWORD dwLen = strlenW(szBuf);
721         szBuf[dwLen + 1] = 0; /* Double-null terminate */
722
723         buttons[dwIndex].iString = (DWORD)SendMessageW(hToolbar, TB_ADDSTRINGW, 0, (LPARAM)szBuf);
724         heap_free(szBuf);
725     }
726
727     SendMessageW(hToolbar, TB_ADDBUTTONSW, dwNumButtons, (LPARAM)buttons);
728     SendMessageW(hToolbar, TB_AUTOSIZE, 0, 0);
729     ShowWindow(hToolbar, SW_SHOW);
730
731     pHHInfo->WinType.hwndToolBar = hToolbar;
732     return TRUE;
733 }
734
735 /* Navigation Pane */
736
737 static void NP_GetNavigationRect(HHInfo *pHHInfo, RECT *rc)
738 {
739     HWND hwndParent = pHHInfo->WinType.hwndHelp;
740     HWND hwndToolbar = pHHInfo->WinType.hwndToolBar;
741     RECT rectWND, rectTB;
742
743     GetClientRect(hwndParent, &rectWND);
744     GetClientRect(hwndToolbar, &rectTB);
745
746     rc->left = 0;
747     rc->top = rectTB.bottom;
748     rc->bottom = rectWND.bottom - rectTB.bottom;
749
750     if (!(pHHInfo->WinType.fsValidMembers & HHWIN_PARAM_NAV_WIDTH) &&
751           pHHInfo->WinType.iNavWidth == 0)
752     {
753         pHHInfo->WinType.iNavWidth = WINTYPE_DEFAULT_NAVWIDTH;
754     }
755
756     rc->right = pHHInfo->WinType.iNavWidth;
757 }
758
759 static DWORD NP_CreateTab(HINSTANCE hInstance, HWND hwndTabCtrl, DWORD index)
760 {
761     TCITEMW tie;
762     LPWSTR tabText = HH_LoadString(index);
763     DWORD ret;
764
765     tie.mask = TCIF_TEXT;
766     tie.pszText = tabText;
767
768     ret = SendMessageW( hwndTabCtrl, TCM_INSERTITEMW, index, (LPARAM)&tie );
769
770     heap_free(tabText);
771     return ret;
772 }
773
774 static BOOL HH_AddNavigationPane(HHInfo *info)
775 {
776     HWND hWnd, hwndTabCtrl;
777     HWND hwndParent = info->WinType.hwndHelp;
778     DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE;
779     DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
780     RECT rc;
781
782     NP_GetNavigationRect(info, &rc);
783
784     hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
785                            rc.left, rc.top, rc.right, rc.bottom,
786                            hwndParent, NULL, hhctrl_hinstance, NULL);
787     if (!hWnd)
788         return FALSE;
789
790     SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)info);
791
792     hwndTabCtrl = CreateWindowExW(dwExStyles, WC_TABCONTROLW, szEmpty, dwStyles,
793                                   0, TAB_TOP_PADDING,
794                                   rc.right - TAB_RIGHT_PADDING,
795                                   rc.bottom - TAB_TOP_PADDING,
796                                   hWnd, NULL, hhctrl_hinstance, NULL);
797     if (!hwndTabCtrl)
798         return FALSE;
799
800     if (*info->WinType.pszToc)
801         info->tabs[TAB_CONTENTS].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_CONTENTS);
802
803     if (*info->WinType.pszIndex)
804         info->tabs[TAB_INDEX].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_INDEX);
805
806     if (info->WinType.fsWinProperties & HHWIN_PROP_TAB_SEARCH)
807         info->tabs[TAB_SEARCH].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_SEARCH);
808
809     if (info->WinType.fsWinProperties & HHWIN_PROP_TAB_FAVORITES)
810         info->tabs[TAB_FAVORITES].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_FAVORITES);
811
812     SendMessageW(hwndTabCtrl, WM_SETFONT, (WPARAM)info->hFont, TRUE);
813
814     info->hwndTabCtrl = hwndTabCtrl;
815     info->WinType.hwndNavigation = hWnd;
816     return TRUE;
817 }
818
819 /* HTML Pane */
820
821 static void HP_GetHTMLRect(HHInfo *info, RECT *rc)
822 {
823     RECT rectTB, rectWND, rectNP, rectSB;
824
825     GetClientRect(info->WinType.hwndHelp, &rectWND);
826     GetClientRect(info->WinType.hwndToolBar, &rectTB);
827     GetClientRect(info->WinType.hwndNavigation, &rectNP);
828     GetClientRect(info->hwndSizeBar, &rectSB);
829
830     rc->left = rectNP.right + rectSB.right;
831     rc->top = rectTB.bottom;
832     rc->right = rectWND.right - rc->left;
833     rc->bottom = rectWND.bottom - rectTB.bottom;
834 }
835
836 static BOOL HH_AddHTMLPane(HHInfo *pHHInfo)
837 {
838     HWND hWnd;
839     HWND hwndParent = pHHInfo->WinType.hwndHelp;
840     DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN;
841     DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_CLIENTEDGE;
842     RECT rc;
843
844     HP_GetHTMLRect(pHHInfo, &rc);
845
846     hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
847                            rc.left, rc.top, rc.right, rc.bottom,
848                            hwndParent, NULL, hhctrl_hinstance, NULL);
849     if (!hWnd)
850         return FALSE;
851
852     if (!InitWebBrowser(pHHInfo, hWnd))
853         return FALSE;
854
855     /* store the pointer to the HH info struct */
856     SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
857
858     ShowWindow(hWnd, SW_SHOW);
859     UpdateWindow(hWnd);
860
861     pHHInfo->WinType.hwndHTML = hWnd;
862     return TRUE;
863 }
864
865 static BOOL AddContentTab(HHInfo *info)
866 {
867     if(info->tabs[TAB_CONTENTS].id == -1)
868         return TRUE; /* No "Contents" tab */
869     info->tabs[TAB_CONTENTS].hwnd = CreateWindowExW(WS_EX_CLIENTEDGE, WC_TREEVIEWW,
870            szEmpty, WS_CHILD | WS_BORDER | 0x25, 50, 50, 100, 100,
871            info->WinType.hwndNavigation, NULL, hhctrl_hinstance, NULL);
872     if(!info->tabs[TAB_CONTENTS].hwnd) {
873         ERR("Could not create treeview control\n");
874         return FALSE;
875     }
876
877     ResizeTabChild(info, TAB_CONTENTS);
878     ShowWindow(info->tabs[TAB_CONTENTS].hwnd, SW_SHOW);
879
880     return TRUE;
881 }
882
883 static BOOL AddIndexTab(HHInfo *info)
884 {
885     char hidden_column[] = "Column";
886     LVCOLUMNA lvc;
887
888     if(info->tabs[TAB_INDEX].id == -1)
889         return TRUE; /* No "Index" tab */
890     info->tabs[TAB_INDEX].hwnd = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW,
891            szEmpty, WS_CHILD | WS_BORDER | LVS_SINGLESEL | LVS_REPORT | LVS_NOCOLUMNHEADER, 50, 50, 100, 100,
892            info->WinType.hwndNavigation, NULL, hhctrl_hinstance, NULL);
893     if(!info->tabs[TAB_INDEX].hwnd) {
894         ERR("Could not create ListView control\n");
895         return FALSE;
896     }
897     memset(&lvc, 0, sizeof(lvc));
898     lvc.mask = LVCF_TEXT;
899     lvc.pszText = hidden_column;
900     if(SendMessageW(info->tabs[TAB_INDEX].hwnd, LVM_INSERTCOLUMNA, 0, (LPARAM) &lvc) == -1)
901     {
902         ERR("Could not create ListView column\n");
903         return FALSE;
904     }
905
906     ResizeTabChild(info, TAB_INDEX);
907     ShowWindow(info->tabs[TAB_INDEX].hwnd, SW_HIDE);
908
909     return TRUE;
910 }
911
912 static BOOL AddSearchTab(HHInfo *info)
913 {
914     HWND hwndList, hwndEdit, hwndContainer;
915     char hidden_column[] = "Column";
916     WNDPROC editWndProc;
917     LVCOLUMNA lvc;
918
919     if(info->tabs[TAB_SEARCH].id == -1)
920         return TRUE; /* No "Search" tab */
921     hwndContainer = CreateWindowExW(WS_EX_CONTROLPARENT, szChildClass, szEmpty,
922                                     WS_CHILD, 0, 0, 0, 0, info->WinType.hwndNavigation,
923                                     NULL, hhctrl_hinstance, NULL);
924     if(!hwndContainer) {
925         ERR("Could not create search window container control.\n");
926         return FALSE;
927     }
928     hwndEdit = CreateWindowExW(WS_EX_CLIENTEDGE, WC_EDITW, szEmpty, WS_CHILD
929                                 | WS_VISIBLE | ES_LEFT | SS_NOTIFY, 0, 0, 0, 0,
930                                hwndContainer, NULL, hhctrl_hinstance, NULL);
931     if(!hwndEdit) {
932         ERR("Could not create search ListView control.\n");
933         return FALSE;
934     }
935     if(SendMessageW(hwndEdit, WM_SETFONT, (WPARAM) info->hFont, (LPARAM) FALSE) == -1)
936     {
937         ERR("Could not set font for edit control.\n");
938         return FALSE;
939     }
940     editWndProc = (WNDPROC) SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC, (LONG_PTR)EditChild_WndProc);
941     if(!editWndProc) {
942         ERR("Could not redirect messages for edit control.\n");
943         return FALSE;
944     }
945     SetWindowLongPtrW(hwndEdit, GWLP_USERDATA, (LONG_PTR)editWndProc);
946     hwndList = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW, szEmpty,
947                                WS_CHILD | WS_VISIBLE | WS_BORDER | LVS_SINGLESEL
948                                 | LVS_REPORT | LVS_NOCOLUMNHEADER, 0, 0, 0, 0,
949                                hwndContainer, NULL, hhctrl_hinstance, NULL);
950     if(!hwndList) {
951         ERR("Could not create search ListView control.\n");
952         return FALSE;
953     }
954     memset(&lvc, 0, sizeof(lvc));
955     lvc.mask = LVCF_TEXT;
956     lvc.pszText = hidden_column;
957     if(SendMessageW(hwndList, LVM_INSERTCOLUMNA, 0, (LPARAM) &lvc) == -1)
958     {
959         ERR("Could not create ListView column\n");
960         return FALSE;
961     }
962
963     info->search.hwndEdit = hwndEdit;
964     info->search.hwndList = hwndList;
965     info->search.hwndContainer = hwndContainer;
966     info->tabs[TAB_SEARCH].hwnd = hwndContainer;
967
968     SetWindowLongPtrW(hwndContainer, GWLP_USERDATA, (LONG_PTR)info);
969
970     ResizeTabChild(info, TAB_SEARCH);
971
972     return TRUE;
973 }
974
975 /* The Index tab's sub-topic popup */
976
977 static void ResizePopupChild(HHInfo *info)
978 {
979     int scroll_width = GetSystemMetrics(SM_CXVSCROLL);
980     int border_width = GetSystemMetrics(SM_CXBORDER);
981     int edge_width = GetSystemMetrics(SM_CXEDGE);
982     INT width, height;
983     RECT rect;
984
985     if(!info)
986         return;
987
988     GetClientRect(info->popup.hwndPopup, &rect);
989     SetWindowPos(info->popup.hwndCallback, HWND_TOP, 0, 0,
990                  rect.right, rect.bottom, SWP_NOMOVE);
991
992     rect.left = TAB_MARGIN;
993     rect.top = TAB_TOP_PADDING + TAB_MARGIN;
994     rect.right -= TAB_RIGHT_PADDING + TAB_MARGIN;
995     rect.bottom -= TAB_MARGIN;
996     width = rect.right-rect.left;
997     height = rect.bottom-rect.top;
998
999     SetWindowPos(info->popup.hwndList, NULL, rect.left, rect.top, width, height,
1000                  SWP_NOZORDER | SWP_NOACTIVATE);
1001
1002     SendMessageW(info->popup.hwndList, LVM_SETCOLUMNWIDTH, 0,
1003                  width-scroll_width-2*border_width-2*edge_width);
1004 }
1005
1006 static LRESULT CALLBACK HelpPopup_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1007 {
1008     HHInfo *info = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
1009
1010     switch (message)
1011     {
1012     case WM_SIZE:
1013         ResizePopupChild(info);
1014         return 0;
1015     case WM_DESTROY:
1016         DestroyWindow(hWnd);
1017         return 0;
1018     case WM_CLOSE:
1019         ShowWindow(hWnd, SW_HIDE);
1020         return 0;
1021
1022     default:
1023         return DefWindowProcW(hWnd, message, wParam, lParam);
1024     }
1025
1026     return 0;
1027 }
1028
1029 static LRESULT CALLBACK PopupChild_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1030 {
1031     switch (message)
1032     {
1033     case WM_NOTIFY: {
1034         NMHDR *nmhdr = (NMHDR*)lParam;
1035         switch(nmhdr->code)
1036         {
1037         case NM_DBLCLK: {
1038             HHInfo *info = (HHInfo*)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
1039             IndexSubItem *iter;
1040
1041             if(info == 0 || lParam == 0)
1042                 return 0;
1043             iter = (IndexSubItem*) ((NMITEMACTIVATE *)lParam)->lParam;
1044             if(iter == 0)
1045                 return 0;
1046             NavigateToChm(info, info->index->merge.chm_file, iter->local);
1047             ShowWindow(info->popup.hwndPopup, SW_HIDE);
1048             return 0;
1049         }
1050         case NM_RETURN: {
1051             HHInfo *info = (HHInfo*)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
1052             IndexSubItem *iter;
1053             LVITEMW lvItem;
1054
1055             if(info == 0)
1056                 return 0;
1057
1058             lvItem.iItem = (int) SendMessageW(info->popup.hwndList, LVM_GETSELECTIONMARK, 0, 0);
1059             lvItem.mask = TVIF_PARAM;
1060             ListView_GetItemW(info->popup.hwndList, &lvItem);
1061             iter = (IndexSubItem*) lvItem.lParam;
1062             NavigateToChm(info, info->index->merge.chm_file, iter->local);
1063             ShowWindow(info->popup.hwndPopup, SW_HIDE);
1064             return 0;
1065         }
1066         }
1067         break;
1068     }
1069     default:
1070         return DefWindowProcW(hWnd, message, wParam, lParam);
1071     }
1072
1073     return 0;
1074 }
1075
1076 static BOOL AddIndexPopup(HHInfo *info)
1077 {
1078     static const WCHAR szPopupChildClass[] = {'H','H',' ','P','o','p','u','p',' ','C','h','i','l','d',0};
1079     static const WCHAR windowCaptionW[] = {'S','e','l','e','c','t',' ','T','o','p','i','c',':',0};
1080     static const WCHAR windowClassW[] = {'H','H',' ','P','o','p','u','p',0};
1081     HWND hwndList, hwndPopup, hwndCallback;
1082     char hidden_column[] = "Column";
1083     WNDCLASSEXW wcex;
1084     LVCOLUMNA lvc;
1085
1086     if(info->tabs[TAB_INDEX].id == -1)
1087         return TRUE; /* No "Index" tab */
1088
1089     wcex.cbSize         = sizeof(WNDCLASSEXW);
1090     wcex.style          = CS_HREDRAW | CS_VREDRAW;
1091     wcex.lpfnWndProc    = HelpPopup_WndProc;
1092     wcex.cbClsExtra     = 0;
1093     wcex.cbWndExtra     = 0;
1094     wcex.hInstance      = hhctrl_hinstance;
1095     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1096     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
1097     wcex.hbrBackground  = (HBRUSH)(COLOR_MENU + 1);
1098     wcex.lpszMenuName   = NULL;
1099     wcex.lpszClassName  = windowClassW;
1100     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1101     RegisterClassExW(&wcex);
1102
1103     wcex.cbSize         = sizeof(WNDCLASSEXW);
1104     wcex.style          = 0;
1105     wcex.lpfnWndProc    = PopupChild_WndProc;
1106     wcex.cbClsExtra     = 0;
1107     wcex.cbWndExtra     = 0;
1108     wcex.hInstance      = hhctrl_hinstance;
1109     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1110     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
1111     wcex.hbrBackground  = (HBRUSH)(COLOR_BTNFACE + 1);
1112     wcex.lpszMenuName   = NULL;
1113     wcex.lpszClassName  = szPopupChildClass;
1114     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1115     RegisterClassExW(&wcex);
1116
1117     hwndPopup = CreateWindowExW(WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_APPWINDOW
1118                                  | WS_EX_WINDOWEDGE | WS_EX_RIGHTSCROLLBAR,
1119                                 windowClassW, windowCaptionW, WS_POPUPWINDOW
1120                                  | WS_OVERLAPPEDWINDOW | WS_VISIBLE
1121                                  | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, CW_USEDEFAULT,
1122                                 CW_USEDEFAULT, 300, 200, info->WinType.hwndHelp,
1123                                 NULL, hhctrl_hinstance, NULL);
1124     if (!hwndPopup)
1125         return FALSE;
1126
1127     hwndCallback = CreateWindowExW(WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR,
1128                                    szPopupChildClass, szEmpty, WS_CHILDWINDOW | WS_VISIBLE,
1129                                    0, 0, 0, 0,
1130                                    hwndPopup, NULL, hhctrl_hinstance, NULL);
1131     if (!hwndCallback)
1132         return FALSE;
1133
1134     ShowWindow(hwndPopup, SW_HIDE);
1135     hwndList = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW, szEmpty,
1136                                WS_CHILD | WS_BORDER | LVS_SINGLESEL | LVS_REPORT
1137                                 | LVS_NOCOLUMNHEADER, 50, 50, 100, 100,
1138                                hwndCallback, NULL, hhctrl_hinstance, NULL);
1139     if(!hwndList) {
1140         ERR("Could not create popup ListView control\n");
1141         return FALSE;
1142     }
1143     memset(&lvc, 0, sizeof(lvc));
1144     lvc.mask = LVCF_TEXT;
1145     lvc.pszText = hidden_column;
1146     if(SendMessageW(hwndList, LVM_INSERTCOLUMNA, 0, (LPARAM) &lvc) == -1)
1147     {
1148         ERR("Could not create popup ListView column\n");
1149         return FALSE;
1150     }
1151
1152     info->popup.hwndCallback = hwndCallback;
1153     info->popup.hwndPopup = hwndPopup;
1154     info->popup.hwndList = hwndList;
1155     SetWindowLongPtrW(hwndPopup, GWLP_USERDATA, (LONG_PTR)info);
1156     SetWindowLongPtrW(hwndCallback, GWLP_USERDATA, (LONG_PTR)info);
1157
1158     ResizePopupChild(info);
1159     ShowWindow(hwndList, SW_SHOW);
1160
1161     return TRUE;
1162 }
1163
1164 /* Viewer Window */
1165
1166 static LRESULT Help_OnSize(HWND hWnd)
1167 {
1168     HHInfo *pHHInfo = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
1169     DWORD dwSize;
1170     RECT rc;
1171
1172     if (!pHHInfo)
1173         return 0;
1174
1175     NP_GetNavigationRect(pHHInfo, &rc);
1176     SetWindowPos(pHHInfo->WinType.hwndNavigation, HWND_TOP, 0, 0,
1177                  rc.right, rc.bottom, SWP_NOMOVE);
1178
1179     SB_GetSizeBarRect(pHHInfo, &rc);
1180     SetWindowPos(pHHInfo->hwndSizeBar, HWND_TOP, rc.left, rc.top,
1181                  rc.right, rc.bottom, SWP_SHOWWINDOW);
1182
1183     HP_GetHTMLRect(pHHInfo, &rc);
1184     SetWindowPos(pHHInfo->WinType.hwndHTML, HWND_TOP, rc.left, rc.top,
1185                  rc.right, rc.bottom, SWP_SHOWWINDOW);
1186
1187     /* Resize browser window taking the frame size into account */
1188     dwSize = GetSystemMetrics(SM_CXFRAME);
1189     ResizeWebBrowser(pHHInfo, rc.right - dwSize, rc.bottom - dwSize);
1190
1191     return 0;
1192 }
1193
1194 static LRESULT CALLBACK Help_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1195 {
1196     switch (message)
1197     {
1198     case WM_COMMAND:
1199         if (HIWORD(wParam) == BN_CLICKED)
1200             TB_OnClick(hWnd, LOWORD(wParam));
1201         break;
1202     case WM_SIZE:
1203         return Help_OnSize(hWnd);
1204     case WM_CLOSE:
1205         ReleaseHelpViewer((HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA));
1206         return 0;
1207     case WM_DESTROY:
1208         if(hh_process)
1209             PostQuitMessage(0);
1210         break;
1211
1212     default:
1213         return DefWindowProcW(hWnd, message, wParam, lParam);
1214     }
1215
1216     return 0;
1217 }
1218
1219 static BOOL HH_CreateHelpWindow(HHInfo *info)
1220 {
1221     HWND hWnd;
1222     RECT winPos = info->WinType.rcWindowPos;
1223     WNDCLASSEXW wcex;
1224     DWORD dwStyles, dwExStyles;
1225     DWORD x, y, width, height;
1226
1227     static const WCHAR windowClassW[] = {
1228         'H','H',' ', 'P','a','r','e','n','t',0
1229     };
1230
1231     wcex.cbSize         = sizeof(WNDCLASSEXW);
1232     wcex.style          = CS_HREDRAW | CS_VREDRAW;
1233     wcex.lpfnWndProc    = Help_WndProc;
1234     wcex.cbClsExtra     = 0;
1235     wcex.cbWndExtra     = 0;
1236     wcex.hInstance      = hhctrl_hinstance;
1237     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1238     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
1239     wcex.hbrBackground  = (HBRUSH)(COLOR_MENU + 1);
1240     wcex.lpszMenuName   = NULL;
1241     wcex.lpszClassName  = windowClassW;
1242     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
1243
1244     RegisterClassExW(&wcex);
1245
1246     /* Read in window parameters if available */
1247     if (info->WinType.fsValidMembers & HHWIN_PARAM_STYLES)
1248         dwStyles = info->WinType.dwStyles | WS_OVERLAPPEDWINDOW;
1249     else
1250         dwStyles = WS_OVERLAPPEDWINDOW | WS_VISIBLE |
1251                    WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
1252
1253     if (info->WinType.fsValidMembers & HHWIN_PARAM_EXSTYLES)
1254         dwExStyles = info->WinType.dwExStyles;
1255     else
1256         dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_APPWINDOW |
1257                      WS_EX_WINDOWEDGE | WS_EX_RIGHTSCROLLBAR;
1258
1259     if (info->WinType.fsValidMembers & HHWIN_PARAM_RECT)
1260     {
1261         x = winPos.left;
1262         y = winPos.top;
1263         width = winPos.right - x;
1264         height = winPos.bottom - y;
1265     }
1266     else
1267     {
1268         x = WINTYPE_DEFAULT_X;
1269         y = WINTYPE_DEFAULT_Y;
1270         width = WINTYPE_DEFAULT_WIDTH;
1271         height = WINTYPE_DEFAULT_HEIGHT;
1272     }
1273
1274     hWnd = CreateWindowExW(dwExStyles, windowClassW, info->WinType.pszCaption,
1275                            dwStyles, x, y, width, height, NULL, NULL, hhctrl_hinstance, NULL);
1276     if (!hWnd)
1277         return FALSE;
1278
1279     ShowWindow(hWnd, SW_SHOW);
1280     UpdateWindow(hWnd);
1281
1282     /* store the pointer to the HH info struct */
1283     SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)info);
1284
1285     info->WinType.hwndHelp = hWnd;
1286     return TRUE;
1287 }
1288
1289 static void HH_CreateFont(HHInfo *pHHInfo)
1290 {
1291     LOGFONTW lf;
1292
1293     GetObjectW(GetStockObject(ANSI_VAR_FONT), sizeof(LOGFONTW), &lf);
1294     lf.lfWeight = FW_NORMAL;
1295     lf.lfItalic = FALSE;
1296     lf.lfUnderline = FALSE;
1297
1298     pHHInfo->hFont = CreateFontIndirectW(&lf);
1299 }
1300
1301 static void HH_InitRequiredControls(DWORD dwControls)
1302 {
1303     INITCOMMONCONTROLSEX icex;
1304
1305     icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
1306     icex.dwICC = dwControls;
1307     InitCommonControlsEx(&icex);
1308 }
1309
1310 /* Creates the whole package */
1311 static BOOL CreateViewer(HHInfo *pHHInfo)
1312 {
1313     HH_CreateFont(pHHInfo);
1314
1315     if (!HH_CreateHelpWindow(pHHInfo))
1316         return FALSE;
1317
1318     HH_InitRequiredControls(ICC_BAR_CLASSES);
1319
1320     if (!HH_AddToolbar(pHHInfo))
1321         return FALSE;
1322
1323     HH_RegisterChildWndClass(pHHInfo);
1324
1325     if (!HH_AddNavigationPane(pHHInfo))
1326         return FALSE;
1327
1328     HH_RegisterSizeBarClass(pHHInfo);
1329
1330     if (!HH_AddSizeBar(pHHInfo))
1331         return FALSE;
1332
1333     if (!HH_AddHTMLPane(pHHInfo))
1334         return FALSE;
1335
1336     if (!AddContentTab(pHHInfo))
1337         return FALSE;
1338
1339     if (!AddIndexTab(pHHInfo))
1340         return FALSE;
1341
1342     if (!AddIndexPopup(pHHInfo))
1343         return FALSE;
1344
1345     if (!AddSearchTab(pHHInfo))
1346         return FALSE;
1347
1348     InitContent(pHHInfo);
1349     InitIndex(pHHInfo);
1350
1351     return TRUE;
1352 }
1353
1354 void ReleaseHelpViewer(HHInfo *info)
1355 {
1356     TRACE("(%p)\n", info);
1357
1358     if (!info)
1359         return;
1360
1361     /* Free allocated strings */
1362     heap_free(info->pszType);
1363     heap_free(info->pszCaption);
1364     heap_free(info->pszToc);
1365     heap_free(info->pszIndex);
1366     heap_free(info->pszFile);
1367     heap_free(info->pszHome);
1368     heap_free(info->pszJump1);
1369     heap_free(info->pszJump2);
1370     heap_free(info->pszUrlJump1);
1371     heap_free(info->pszUrlJump2);
1372
1373     if (info->pCHMInfo)
1374         CloseCHM(info->pCHMInfo);
1375
1376     ReleaseWebBrowser(info);
1377     ReleaseContent(info);
1378     ReleaseIndex(info);
1379
1380     if(info->WinType.hwndHelp)
1381         DestroyWindow(info->WinType.hwndHelp);
1382
1383     heap_free(info);
1384     OleUninitialize();
1385 }
1386
1387 HHInfo *CreateHelpViewer(LPCWSTR filename)
1388 {
1389     HHInfo *info = heap_alloc_zero(sizeof(HHInfo));
1390     int i;
1391
1392     /* Set the invalid tab ID (-1) as the default value for all
1393      * of the tabs, this matches a failed TCM_INSERTITEM call.
1394      */
1395     for(i=0;i<sizeof(info->tabs)/sizeof(HHTab);i++)
1396         info->tabs[i].id = -1;
1397
1398     OleInitialize(NULL);
1399
1400     info->pCHMInfo = OpenCHM(filename);
1401     if(!info->pCHMInfo) {
1402         ReleaseHelpViewer(info);
1403         return NULL;
1404     }
1405
1406     if (!LoadWinTypeFromCHM(info)) {
1407         ReleaseHelpViewer(info);
1408         return NULL;
1409     }
1410
1411     if(!CreateViewer(info)) {
1412         ReleaseHelpViewer(info);
1413         return NULL;
1414     }
1415
1416     return info;
1417 }