hhctrl.ocx: Nul-terminate url_indicator constant in NavigateToUrl.
[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
48 static const WCHAR szEmpty[] = {0};
49
50 /* Loads a string from the resource file */
51 static LPWSTR HH_LoadString(DWORD dwID)
52 {
53     LPWSTR string = NULL;
54     LPCWSTR stringresource;
55     int iSize;
56
57     iSize = LoadStringW(hhctrl_hinstance, dwID, (LPWSTR)&stringresource, 0);
58
59     string = heap_alloc((iSize + 2) * sizeof(WCHAR)); /* some strings (tab text) needs double-null termination */
60     memcpy(string, stringresource, iSize*sizeof(WCHAR));
61     string[iSize] = 0;
62
63     return string;
64 }
65
66 static HRESULT navigate_url(HHInfo *info, LPCWSTR surl)
67 {
68     VARIANT url;
69     HRESULT hres;
70
71     TRACE("%s\n", debugstr_w(surl));
72
73     V_VT(&url) = VT_BSTR;
74     V_BSTR(&url) = SysAllocString(surl);
75
76     hres = IWebBrowser2_Navigate2(info->web_browser, &url, 0, 0, 0, 0);
77
78     VariantClear(&url);
79
80     if(FAILED(hres))
81         TRACE("Navigation failed: %08x\n", hres);
82
83     return hres;
84 }
85
86 BOOL NavigateToUrl(HHInfo *info, LPCWSTR surl)
87 {
88     ChmPath chm_path;
89     BOOL ret;
90     HRESULT hres;
91
92     static const WCHAR url_indicator[] = {':', '/', '/', 0};
93
94     TRACE("%s\n", debugstr_w(surl));
95
96     if (strstrW(surl, url_indicator)) {
97         hres = navigate_url(info, surl);
98         if(SUCCEEDED(hres))
99             return TRUE;
100     } /* look up in chm if it doesn't look like a full url */
101
102     SetChmPath(&chm_path, info->pCHMInfo->szFile, surl);
103     ret = NavigateToChm(info, chm_path.chm_file, chm_path.chm_index);
104
105     heap_free(chm_path.chm_file);
106     heap_free(chm_path.chm_index);
107
108     return ret;
109 }
110
111 BOOL NavigateToChm(HHInfo *info, LPCWSTR file, LPCWSTR index)
112 {
113     WCHAR buf[INTERNET_MAX_URL_LENGTH];
114     WCHAR full_path[MAX_PATH];
115     LPWSTR ptr;
116
117     static const WCHAR url_format[] =
118         {'m','k',':','@','M','S','I','T','S','t','o','r','e',':','%','s',':',':','%','s',0};
119
120     TRACE("%p %s %s\n", info, debugstr_w(file), debugstr_w(index));
121
122     if (!info->web_browser)
123         return FALSE;
124
125     if(!GetFullPathNameW(file, sizeof(full_path)/sizeof(full_path[0]), full_path, NULL)) {
126         WARN("GetFullPathName failed: %u\n", GetLastError());
127         return FALSE;
128     }
129
130     wsprintfW(buf, url_format, full_path, index);
131
132     /* FIXME: HACK */
133     if((ptr = strchrW(buf, '#')))
134        *ptr = 0;
135
136     return SUCCEEDED(navigate_url(info, buf));
137 }
138
139 /* Size Bar */
140
141 #define SIZEBAR_WIDTH   4
142
143 static const WCHAR szSizeBarClass[] = {
144     'H','H',' ','S','i','z','e','B','a','r',0
145 };
146
147 /* Draw the SizeBar */
148 static void SB_OnPaint(HWND hWnd)
149 {
150     PAINTSTRUCT ps;
151     HDC hdc;
152     RECT rc;
153     
154     hdc = BeginPaint(hWnd, &ps);
155
156     GetClientRect(hWnd, &rc);
157
158     /* dark frame */
159     rc.right += 1;
160     rc.bottom -= 1;
161     FrameRect(hdc, &rc, GetStockObject(GRAY_BRUSH));
162
163     /* white highlight */
164     SelectObject(hdc, GetStockObject(WHITE_PEN));
165     MoveToEx(hdc, rc.right, 1, NULL);
166     LineTo(hdc, 1, 1);
167     LineTo(hdc, 1, rc.bottom - 1);
168
169     
170     MoveToEx(hdc, 0, rc.bottom, NULL);
171     LineTo(hdc, rc.right, rc.bottom);
172
173     EndPaint(hWnd, &ps);
174 }
175
176 static void SB_OnLButtonDown(HWND hWnd, WPARAM wParam, LPARAM lParam)
177 {
178     SetCapture(hWnd);
179 }
180
181 static void SB_OnLButtonUp(HWND hWnd, WPARAM wParam, LPARAM lParam)
182 {
183     HHInfo *pHHInfo = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
184     POINT pt;
185
186     pt.x = (short)LOWORD(lParam);
187     pt.y = (short)HIWORD(lParam);
188
189     /* update the window sizes */
190     pHHInfo->WinType.iNavWidth += pt.x;
191     Help_OnSize(hWnd);
192
193     ReleaseCapture();
194 }
195
196 static void SB_OnMouseMove(HWND hWnd, WPARAM wParam, LPARAM lParam)
197 {
198     /* ignore WM_MOUSEMOVE if not dragging the SizeBar */
199     if (!(wParam & MK_LBUTTON))
200         return;
201 }
202
203 static LRESULT CALLBACK SizeBar_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
204 {
205     switch (message)
206     {
207         case WM_LBUTTONDOWN:
208             SB_OnLButtonDown(hWnd, wParam, lParam);
209             break;
210         case WM_LBUTTONUP:
211             SB_OnLButtonUp(hWnd, wParam, lParam);
212             break;
213         case WM_MOUSEMOVE:
214             SB_OnMouseMove(hWnd, wParam, lParam);
215             break;
216         case WM_PAINT:
217             SB_OnPaint(hWnd);
218             break;
219         default:
220             return DefWindowProcW(hWnd, message, wParam, lParam);
221     }
222
223     return 0;
224 }
225
226 static void HH_RegisterSizeBarClass(HHInfo *pHHInfo)
227 {
228     WNDCLASSEXW wcex;
229
230     wcex.cbSize         = sizeof(WNDCLASSEXW);
231     wcex.style          = 0;
232     wcex.lpfnWndProc    = SizeBar_WndProc;
233     wcex.cbClsExtra     = 0;
234     wcex.cbWndExtra     = 0;
235     wcex.hInstance      = hhctrl_hinstance;
236     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
237     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_SIZEWE);
238     wcex.hbrBackground  = (HBRUSH)(COLOR_MENU + 1);
239     wcex.lpszMenuName   = NULL;
240     wcex.lpszClassName  = szSizeBarClass;
241     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
242
243     RegisterClassExW(&wcex);
244 }
245
246 static void SB_GetSizeBarRect(HHInfo *info, RECT *rc)
247 {
248     RECT rectWND, rectTB, rectNP;
249
250     GetClientRect(info->WinType.hwndHelp, &rectWND);
251     GetClientRect(info->WinType.hwndToolBar, &rectTB);
252     GetClientRect(info->WinType.hwndNavigation, &rectNP);
253
254     rc->left = rectNP.right;
255     rc->top = rectTB.bottom;
256     rc->bottom = rectWND.bottom - rectTB.bottom;
257     rc->right = SIZEBAR_WIDTH;
258 }
259
260 static BOOL HH_AddSizeBar(HHInfo *pHHInfo)
261 {
262     HWND hWnd;
263     HWND hwndParent = pHHInfo->WinType.hwndHelp;
264     DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE | WS_OVERLAPPED;
265     DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
266     RECT rc;
267
268     SB_GetSizeBarRect(pHHInfo, &rc);
269
270     hWnd = CreateWindowExW(dwExStyles, szSizeBarClass, szEmpty, dwStyles,
271                            rc.left, rc.top, rc.right, rc.bottom,
272                            hwndParent, NULL, hhctrl_hinstance, NULL);
273     if (!hWnd)
274         return FALSE;
275
276     /* store the pointer to the HH info struct */
277     SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
278
279     pHHInfo->hwndSizeBar = hWnd;
280     return TRUE;
281 }
282
283 /* Child Window */
284
285 static const WCHAR szChildClass[] = {
286     'H','H',' ','C','h','i','l','d',0
287 };
288
289 static LRESULT Child_OnPaint(HWND hWnd)
290 {
291     PAINTSTRUCT ps;
292     HDC hdc;
293     RECT rc;
294
295     hdc = BeginPaint(hWnd, &ps);
296
297     /* Only paint the Navigation pane, identified by the fact
298      * that it has a child window
299      */
300     if (GetWindow(hWnd, GW_CHILD))
301     {
302         GetClientRect(hWnd, &rc);
303
304         /* set the border color */
305         SelectObject(hdc, GetStockObject(DC_PEN));
306         SetDCPenColor(hdc, GetSysColor(COLOR_BTNSHADOW));
307
308         /* Draw the top border */
309         LineTo(hdc, rc.right, 0);
310
311         SelectObject(hdc, GetStockObject(WHITE_PEN));
312         MoveToEx(hdc, 0, 1, NULL);
313         LineTo(hdc, rc.right, 1);
314     }
315
316     EndPaint(hWnd, &ps);
317
318     return 0;
319 }
320
321 static void ResizeTabChild(HHInfo *info, HWND hwnd)
322 {
323     RECT rect, tabrc;
324     DWORD cnt;
325
326     GetClientRect(info->WinType.hwndNavigation, &rect);
327     SendMessageW(info->hwndTabCtrl, TCM_GETITEMRECT, 0, (LPARAM)&tabrc);
328     cnt = SendMessageW(info->hwndTabCtrl, TCM_GETROWCOUNT, 0, 0);
329
330     rect.left = TAB_MARGIN;
331     rect.top = TAB_TOP_PADDING + cnt*(tabrc.bottom-tabrc.top) + TAB_MARGIN;
332     rect.right -= TAB_RIGHT_PADDING + TAB_MARGIN;
333     rect.bottom -= TAB_MARGIN;
334
335     SetWindowPos(hwnd, NULL, rect.left, rect.top, rect.right-rect.left,
336                  rect.bottom-rect.top, SWP_NOZORDER | SWP_NOACTIVATE);
337 }
338
339 static LRESULT Child_OnSize(HWND hwnd)
340 {
341     HHInfo *info = (HHInfo*)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
342     RECT rect;
343
344     if(!info || hwnd != info->WinType.hwndNavigation)
345         return 0;
346
347     GetClientRect(hwnd, &rect);
348     SetWindowPos(info->hwndTabCtrl, HWND_TOP, 0, 0,
349                  rect.right - TAB_RIGHT_PADDING,
350                  rect.bottom - TAB_TOP_PADDING, SWP_NOMOVE);
351
352     ResizeTabChild(info, info->tabs[TAB_CONTENTS].hwnd);
353     return 0;
354 }
355
356 static LRESULT OnTabChange(HWND hwnd)
357 {
358     HHInfo *info = (HHInfo*)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
359
360     TRACE("%p\n", hwnd);
361
362     if (!info)
363         return 0;
364
365     if(info->tabs[info->current_tab].hwnd)
366         ShowWindow(info->tabs[info->current_tab].hwnd, SW_HIDE);
367
368     info->current_tab = SendMessageW(info->hwndTabCtrl, TCM_GETCURSEL, 0, 0);
369
370     if(info->tabs[info->current_tab].hwnd)
371         ShowWindow(info->tabs[info->current_tab].hwnd, SW_SHOW);
372
373     return 0;
374 }
375
376 static LRESULT OnTopicChange(HWND hwnd, ContentItem *item)
377 {
378     HHInfo *info = (HHInfo*)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
379     LPCWSTR chmfile = NULL;
380     ContentItem *iter = item;
381
382     if(!item || !info)
383         return 0;
384
385     TRACE("name %s loal %s\n", debugstr_w(item->name), debugstr_w(item->local));
386
387     while(iter) {
388         if(iter->merge.chm_file) {
389             chmfile = iter->merge.chm_file;
390             break;
391         }
392         iter = iter->parent;
393     }
394
395     NavigateToChm(info, chmfile, item->local);
396     return 0;
397 }
398
399 static LRESULT CALLBACK Child_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
400 {
401     switch (message)
402     {
403     case WM_PAINT:
404         return Child_OnPaint(hWnd);
405     case WM_SIZE:
406         return Child_OnSize(hWnd);
407     case WM_NOTIFY: {
408         NMHDR *nmhdr = (NMHDR*)lParam;
409         switch(nmhdr->code) {
410         case TCN_SELCHANGE:
411             return OnTabChange(hWnd);
412         case TVN_SELCHANGEDW:
413             return OnTopicChange(hWnd, (ContentItem*)((NMTREEVIEWW *)lParam)->itemNew.lParam);
414         }
415         break;
416     }
417     default:
418         return DefWindowProcW(hWnd, message, wParam, lParam);
419     }
420
421     return 0;
422 }
423
424 static void HH_RegisterChildWndClass(HHInfo *pHHInfo)
425 {
426     WNDCLASSEXW wcex;
427
428     wcex.cbSize         = sizeof(WNDCLASSEXW);
429     wcex.style          = 0;
430     wcex.lpfnWndProc    = Child_WndProc;
431     wcex.cbClsExtra     = 0;
432     wcex.cbWndExtra     = 0;
433     wcex.hInstance      = hhctrl_hinstance;
434     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
435     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
436     wcex.hbrBackground  = (HBRUSH)(COLOR_BTNFACE + 1);
437     wcex.lpszMenuName   = NULL;
438     wcex.lpszClassName  = szChildClass;
439     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
440
441     RegisterClassExW(&wcex);
442 }
443
444 /* Toolbar */
445
446 #define ICON_SIZE   20
447
448 static void TB_OnClick(HWND hWnd, DWORD dwID)
449 {
450     HHInfo *info = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
451
452     switch (dwID)
453     {
454         case IDTB_STOP:
455             DoPageAction(info, WB_STOP);
456             break;
457         case IDTB_REFRESH:
458             DoPageAction(info, WB_REFRESH);
459             break;
460         case IDTB_BACK:
461             DoPageAction(info, WB_GOBACK);
462             break;
463         case IDTB_HOME:
464             NavigateToChm(info, info->pCHMInfo->szFile, info->WinType.pszHome);
465             break;
466         case IDTB_FORWARD:
467             DoPageAction(info, WB_GOFORWARD);
468             break;
469         case IDTB_EXPAND:
470         case IDTB_CONTRACT:
471         case IDTB_SYNC:
472         case IDTB_PRINT:
473         case IDTB_OPTIONS:
474         case IDTB_BROWSE_FWD:
475         case IDTB_BROWSE_BACK:
476         case IDTB_JUMP1:
477         case IDTB_JUMP2:
478         case IDTB_CUSTOMIZE:
479         case IDTB_ZOOM:
480         case IDTB_TOC_NEXT:
481         case IDTB_TOC_PREV:
482             break;
483     }
484 }
485
486 static void TB_AddButton(TBBUTTON *pButtons, DWORD dwIndex, DWORD dwID)
487 {
488     /* FIXME: Load the correct button bitmaps */
489     pButtons[dwIndex].iBitmap = STD_PRINT;
490     pButtons[dwIndex].idCommand = dwID;
491     pButtons[dwIndex].fsState = TBSTATE_ENABLED;
492     pButtons[dwIndex].fsStyle = BTNS_BUTTON;
493     pButtons[dwIndex].dwData = 0;
494     pButtons[dwIndex].iString = 0;
495 }
496
497 static void TB_AddButtonsFromFlags(TBBUTTON *pButtons, DWORD dwButtonFlags, LPDWORD pdwNumButtons)
498 {
499     *pdwNumButtons = 0;
500
501     if (dwButtonFlags & HHWIN_BUTTON_EXPAND)
502         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_EXPAND);
503
504     if (dwButtonFlags & HHWIN_BUTTON_BACK)
505         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_BACK);
506
507     if (dwButtonFlags & HHWIN_BUTTON_FORWARD)
508         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_FORWARD);
509
510     if (dwButtonFlags & HHWIN_BUTTON_STOP)
511         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_STOP);
512
513     if (dwButtonFlags & HHWIN_BUTTON_REFRESH)
514         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_REFRESH);
515
516     if (dwButtonFlags & HHWIN_BUTTON_HOME)
517         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_HOME);
518
519     if (dwButtonFlags & HHWIN_BUTTON_SYNC)
520         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_SYNC);
521
522     if (dwButtonFlags & HHWIN_BUTTON_OPTIONS)
523         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_OPTIONS);
524
525     if (dwButtonFlags & HHWIN_BUTTON_PRINT)
526         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_PRINT);
527
528     if (dwButtonFlags & HHWIN_BUTTON_JUMP1)
529         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_JUMP1);
530
531     if (dwButtonFlags & HHWIN_BUTTON_JUMP2)
532         TB_AddButton(pButtons,(*pdwNumButtons)++, IDTB_JUMP2);
533
534     if (dwButtonFlags & HHWIN_BUTTON_ZOOM)
535         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_ZOOM);
536
537     if (dwButtonFlags & HHWIN_BUTTON_TOC_NEXT)
538         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_TOC_NEXT);
539
540     if (dwButtonFlags & HHWIN_BUTTON_TOC_PREV)
541         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_TOC_PREV);
542 }
543
544 static BOOL HH_AddToolbar(HHInfo *pHHInfo)
545 {
546     HWND hToolbar;
547     HWND hwndParent = pHHInfo->WinType.hwndHelp;
548     DWORD toolbarFlags;
549     TBBUTTON buttons[IDTB_TOC_PREV - IDTB_EXPAND];
550     TBADDBITMAP tbAB;
551     DWORD dwStyles, dwExStyles;
552     DWORD dwNumButtons, dwIndex;
553
554     if (pHHInfo->WinType.fsWinProperties & HHWIN_PARAM_TB_FLAGS)
555         toolbarFlags = pHHInfo->WinType.fsToolBarFlags;
556     else
557         toolbarFlags = HHWIN_DEF_BUTTONS;
558
559     TB_AddButtonsFromFlags(buttons, toolbarFlags, &dwNumButtons);
560
561     dwStyles = WS_CHILDWINDOW | WS_VISIBLE | TBSTYLE_FLAT |
562                TBSTYLE_WRAPABLE | TBSTYLE_TOOLTIPS | CCS_NODIVIDER;
563     dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
564
565     hToolbar = CreateWindowExW(dwExStyles, TOOLBARCLASSNAMEW, NULL, dwStyles,
566                                0, 0, 0, 0, hwndParent, NULL,
567                                hhctrl_hinstance, NULL);
568     if (!hToolbar)
569         return FALSE;
570
571     SendMessageW(hToolbar, TB_SETBITMAPSIZE, 0, MAKELONG(ICON_SIZE, ICON_SIZE));
572     SendMessageW(hToolbar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
573     SendMessageW(hToolbar, WM_SETFONT, (WPARAM)pHHInfo->hFont, TRUE);
574
575     /* FIXME: Load correct icons for all buttons */
576     tbAB.hInst = HINST_COMMCTRL;
577     tbAB.nID = IDB_STD_LARGE_COLOR;
578     SendMessageW(hToolbar, TB_ADDBITMAP, 0, (LPARAM)&tbAB);
579
580     for (dwIndex = 0; dwIndex < dwNumButtons; dwIndex++)
581     {
582         LPWSTR szBuf = HH_LoadString(buttons[dwIndex].idCommand);
583         DWORD dwLen = strlenW(szBuf);
584         szBuf[dwLen + 1] = 0; /* Double-null terminate */
585
586         buttons[dwIndex].iString = (DWORD)SendMessageW(hToolbar, TB_ADDSTRINGW, 0, (LPARAM)szBuf);
587         heap_free(szBuf);
588     }
589
590     SendMessageW(hToolbar, TB_ADDBUTTONSW, dwNumButtons, (LPARAM)buttons);
591     SendMessageW(hToolbar, TB_AUTOSIZE, 0, 0);
592     ShowWindow(hToolbar, SW_SHOW);
593
594     pHHInfo->WinType.hwndToolBar = hToolbar;
595     return TRUE;
596 }
597
598 /* Navigation Pane */
599
600 static void NP_GetNavigationRect(HHInfo *pHHInfo, RECT *rc)
601 {
602     HWND hwndParent = pHHInfo->WinType.hwndHelp;
603     HWND hwndToolbar = pHHInfo->WinType.hwndToolBar;
604     RECT rectWND, rectTB;
605
606     GetClientRect(hwndParent, &rectWND);
607     GetClientRect(hwndToolbar, &rectTB);
608
609     rc->left = 0;
610     rc->top = rectTB.bottom;
611     rc->bottom = rectWND.bottom - rectTB.bottom;
612
613     if (!(pHHInfo->WinType.fsValidMembers & HHWIN_PARAM_NAV_WIDTH) &&
614           pHHInfo->WinType.iNavWidth == 0)
615     {
616         pHHInfo->WinType.iNavWidth = WINTYPE_DEFAULT_NAVWIDTH;
617     }
618
619     rc->right = pHHInfo->WinType.iNavWidth;
620 }
621
622 static DWORD NP_CreateTab(HINSTANCE hInstance, HWND hwndTabCtrl, DWORD index)
623 {
624     TCITEMW tie;
625     LPWSTR tabText = HH_LoadString(index);
626     DWORD ret;
627
628     tie.mask = TCIF_TEXT;
629     tie.pszText = tabText;
630
631     ret = SendMessageW( hwndTabCtrl, TCM_INSERTITEMW, index, (LPARAM)&tie );
632
633     heap_free(tabText);
634     return ret;
635 }
636
637 static BOOL HH_AddNavigationPane(HHInfo *info)
638 {
639     HWND hWnd, hwndTabCtrl;
640     HWND hwndParent = info->WinType.hwndHelp;
641     DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE;
642     DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
643     RECT rc;
644
645     NP_GetNavigationRect(info, &rc);
646
647     hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
648                            rc.left, rc.top, rc.right, rc.bottom,
649                            hwndParent, NULL, hhctrl_hinstance, NULL);
650     if (!hWnd)
651         return FALSE;
652
653     SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)info);
654
655     hwndTabCtrl = CreateWindowExW(dwExStyles, WC_TABCONTROLW, szEmpty, dwStyles,
656                                   0, TAB_TOP_PADDING,
657                                   rc.right - TAB_RIGHT_PADDING,
658                                   rc.bottom - TAB_TOP_PADDING,
659                                   hWnd, NULL, hhctrl_hinstance, NULL);
660     if (!hwndTabCtrl)
661         return FALSE;
662
663     if (*info->WinType.pszToc)
664         info->tabs[TAB_CONTENTS].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_CONTENTS);
665
666     if (*info->WinType.pszIndex)
667         info->tabs[TAB_INDEX].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_INDEX);
668
669     if (info->WinType.fsWinProperties & HHWIN_PROP_TAB_SEARCH)
670         info->tabs[TAB_SEARCH].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_SEARCH);
671
672     if (info->WinType.fsWinProperties & HHWIN_PROP_TAB_FAVORITES)
673         info->tabs[TAB_FAVORITES].id = NP_CreateTab(hhctrl_hinstance, hwndTabCtrl, IDS_FAVORITES);
674
675     SendMessageW(hwndTabCtrl, WM_SETFONT, (WPARAM)info->hFont, TRUE);
676
677     info->hwndTabCtrl = hwndTabCtrl;
678     info->WinType.hwndNavigation = hWnd;
679     return TRUE;
680 }
681
682 /* HTML Pane */
683
684 static void HP_GetHTMLRect(HHInfo *info, RECT *rc)
685 {
686     RECT rectTB, rectWND, rectNP, rectSB;
687
688     GetClientRect(info->WinType.hwndHelp, &rectWND);
689     GetClientRect(info->WinType.hwndToolBar, &rectTB);
690     GetClientRect(info->WinType.hwndNavigation, &rectNP);
691     GetClientRect(info->hwndSizeBar, &rectSB);
692
693     rc->left = rectNP.right + rectSB.right;
694     rc->top = rectTB.bottom;
695     rc->right = rectWND.right - rc->left;
696     rc->bottom = rectWND.bottom - rectTB.bottom;
697 }
698
699 static BOOL HH_AddHTMLPane(HHInfo *pHHInfo)
700 {
701     HWND hWnd;
702     HWND hwndParent = pHHInfo->WinType.hwndHelp;
703     DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN;
704     DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_CLIENTEDGE;
705     RECT rc;
706
707     HP_GetHTMLRect(pHHInfo, &rc);
708
709     hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
710                            rc.left, rc.top, rc.right, rc.bottom,
711                            hwndParent, NULL, hhctrl_hinstance, NULL);
712     if (!hWnd)
713         return FALSE;
714
715     if (!InitWebBrowser(pHHInfo, hWnd))
716         return FALSE;
717
718     /* store the pointer to the HH info struct */
719     SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
720
721     ShowWindow(hWnd, SW_SHOW);
722     UpdateWindow(hWnd);
723
724     pHHInfo->WinType.hwndHTML = hWnd;
725     return TRUE;
726 }
727
728 static BOOL AddContentTab(HHInfo *info)
729 {
730     info->tabs[TAB_CONTENTS].hwnd = CreateWindowExW(WS_EX_CLIENTEDGE, WC_TREEVIEWW,
731            szEmpty, WS_CHILD | WS_BORDER | 0x25, 50, 50, 100, 100,
732            info->WinType.hwndNavigation, NULL, hhctrl_hinstance, NULL);
733     if(!info->tabs[TAB_CONTENTS].hwnd) {
734         ERR("Could not create treeview control\n");
735         return FALSE;
736     }
737
738     ResizeTabChild(info, info->tabs[TAB_CONTENTS].hwnd);
739     ShowWindow(info->tabs[TAB_CONTENTS].hwnd, SW_SHOW);
740
741     return TRUE;
742 }
743
744 /* Viewer Window */
745
746 static LRESULT Help_OnSize(HWND hWnd)
747 {
748     HHInfo *pHHInfo = (HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
749     DWORD dwSize;
750     RECT rc;
751
752     if (!pHHInfo)
753         return 0;
754
755     NP_GetNavigationRect(pHHInfo, &rc);
756     SetWindowPos(pHHInfo->WinType.hwndNavigation, HWND_TOP, 0, 0,
757                  rc.right, rc.bottom, SWP_NOMOVE);
758
759     SB_GetSizeBarRect(pHHInfo, &rc);
760     SetWindowPos(pHHInfo->hwndSizeBar, HWND_TOP, rc.left, rc.top,
761                  rc.right, rc.bottom, SWP_SHOWWINDOW);
762
763     HP_GetHTMLRect(pHHInfo, &rc);
764     SetWindowPos(pHHInfo->WinType.hwndHTML, HWND_TOP, rc.left, rc.top,
765                  rc.right, rc.bottom, SWP_SHOWWINDOW);
766
767     /* Resize browser window taking the frame size into account */
768     dwSize = GetSystemMetrics(SM_CXFRAME);
769     ResizeWebBrowser(pHHInfo, rc.right - dwSize, rc.bottom - dwSize);
770
771     return 0;
772 }
773
774 static LRESULT CALLBACK Help_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
775 {
776     switch (message)
777     {
778     case WM_COMMAND:
779         if (HIWORD(wParam) == BN_CLICKED)
780             TB_OnClick(hWnd, LOWORD(wParam));
781         break;
782     case WM_SIZE:
783         return Help_OnSize(hWnd);
784     case WM_CLOSE:
785         ReleaseHelpViewer((HHInfo *)GetWindowLongPtrW(hWnd, GWLP_USERDATA));
786         return 0;
787     case WM_DESTROY:
788         if(hh_process)
789             PostQuitMessage(0);
790         break;
791
792     default:
793         return DefWindowProcW(hWnd, message, wParam, lParam);
794     }
795
796     return 0;
797 }
798
799 static BOOL HH_CreateHelpWindow(HHInfo *info)
800 {
801     HWND hWnd;
802     RECT winPos = info->WinType.rcWindowPos;
803     WNDCLASSEXW wcex;
804     DWORD dwStyles, dwExStyles;
805     DWORD x, y, width, height;
806
807     static const WCHAR windowClassW[] = {
808         'H','H',' ', 'P','a','r','e','n','t',0
809     };
810
811     wcex.cbSize         = sizeof(WNDCLASSEXW);
812     wcex.style          = CS_HREDRAW | CS_VREDRAW;
813     wcex.lpfnWndProc    = Help_WndProc;
814     wcex.cbClsExtra     = 0;
815     wcex.cbWndExtra     = 0;
816     wcex.hInstance      = hhctrl_hinstance;
817     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
818     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
819     wcex.hbrBackground  = (HBRUSH)(COLOR_MENU + 1);
820     wcex.lpszMenuName   = NULL;
821     wcex.lpszClassName  = windowClassW;
822     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
823
824     RegisterClassExW(&wcex);
825
826     /* Read in window parameters if available */
827     if (info->WinType.fsValidMembers & HHWIN_PARAM_STYLES)
828         dwStyles = info->WinType.dwStyles;
829     else
830         dwStyles = WS_OVERLAPPEDWINDOW | WS_VISIBLE |
831                    WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
832
833     if (info->WinType.fsValidMembers & HHWIN_PARAM_EXSTYLES)
834         dwExStyles = info->WinType.dwExStyles;
835     else
836         dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_APPWINDOW |
837                      WS_EX_WINDOWEDGE | WS_EX_RIGHTSCROLLBAR;
838
839     if (info->WinType.fsValidMembers & HHWIN_PARAM_RECT)
840     {
841         x = winPos.left;
842         y = winPos.top;
843         width = winPos.right - x;
844         height = winPos.bottom - y;
845     }
846     else
847     {
848         x = WINTYPE_DEFAULT_X;
849         y = WINTYPE_DEFAULT_Y;
850         width = WINTYPE_DEFAULT_WIDTH;
851         height = WINTYPE_DEFAULT_HEIGHT;
852     }
853
854     hWnd = CreateWindowExW(dwExStyles, windowClassW, info->WinType.pszCaption,
855                            dwStyles, x, y, width, height, NULL, NULL, hhctrl_hinstance, NULL);
856     if (!hWnd)
857         return FALSE;
858
859     ShowWindow(hWnd, SW_SHOW);
860     UpdateWindow(hWnd);
861
862     /* store the pointer to the HH info struct */
863     SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)info);
864
865     info->WinType.hwndHelp = hWnd;
866     return TRUE;
867 }
868
869 static void HH_CreateFont(HHInfo *pHHInfo)
870 {
871     LOGFONTW lf;
872
873     GetObjectW(GetStockObject(ANSI_VAR_FONT), sizeof(LOGFONTW), &lf);
874     lf.lfWeight = FW_NORMAL;
875     lf.lfItalic = FALSE;
876     lf.lfUnderline = FALSE;
877
878     pHHInfo->hFont = CreateFontIndirectW(&lf);
879 }
880
881 static void HH_InitRequiredControls(DWORD dwControls)
882 {
883     INITCOMMONCONTROLSEX icex;
884
885     icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
886     icex.dwICC = dwControls;
887     InitCommonControlsEx(&icex);
888 }
889
890 /* Creates the whole package */
891 static BOOL CreateViewer(HHInfo *pHHInfo)
892 {
893     HH_CreateFont(pHHInfo);
894
895     if (!HH_CreateHelpWindow(pHHInfo))
896         return FALSE;
897
898     HH_InitRequiredControls(ICC_BAR_CLASSES);
899
900     if (!HH_AddToolbar(pHHInfo))
901         return FALSE;
902
903     HH_RegisterChildWndClass(pHHInfo);
904
905     if (!HH_AddNavigationPane(pHHInfo))
906         return FALSE;
907
908     HH_RegisterSizeBarClass(pHHInfo);
909
910     if (!HH_AddSizeBar(pHHInfo))
911         return FALSE;
912
913     if (!HH_AddHTMLPane(pHHInfo))
914         return FALSE;
915
916     if (!AddContentTab(pHHInfo))
917         return FALSE;
918
919     InitContent(pHHInfo);
920
921     return TRUE;
922 }
923
924 void ReleaseHelpViewer(HHInfo *info)
925 {
926     TRACE("(%p)\n", info);
927
928     if (!info)
929         return;
930
931     /* Free allocated strings */
932     heap_free(info->pszType);
933     heap_free(info->pszCaption);
934     heap_free(info->pszToc);
935     heap_free(info->pszIndex);
936     heap_free(info->pszFile);
937     heap_free(info->pszHome);
938     heap_free(info->pszJump1);
939     heap_free(info->pszJump2);
940     heap_free(info->pszUrlJump1);
941     heap_free(info->pszUrlJump2);
942
943     if (info->pCHMInfo)
944         CloseCHM(info->pCHMInfo);
945
946     ReleaseWebBrowser(info);
947     ReleaseContent(info);
948
949     if(info->WinType.hwndHelp)
950         DestroyWindow(info->WinType.hwndHelp);
951
952     heap_free(info);
953     OleUninitialize();
954 }
955
956 HHInfo *CreateHelpViewer(LPCWSTR filename)
957 {
958     HHInfo *info = heap_alloc_zero(sizeof(HHInfo));
959
960     OleInitialize(NULL);
961
962     info->pCHMInfo = OpenCHM(filename);
963     if(!info->pCHMInfo) {
964         ReleaseHelpViewer(info);
965         return NULL;
966     }
967
968     if (!LoadWinTypeFromCHM(info)) {
969         ReleaseHelpViewer(info);
970         return NULL;
971     }
972
973     if(!CreateViewer(info)) {
974         ReleaseHelpViewer(info);
975         return NULL;
976     }
977
978     return info;
979 }