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