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