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