- Add the HH Child window class.
[wine] / dlls / hhctrl.ocx / help.c
1 /*
2  * Help Viewer Implementation
3  *
4  * Copyright 2005 James Hawkins
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "winuser.h"
27 #include "winnls.h"
28 #include "commctrl.h"
29 #include "htmlhelp.h"
30 #include "ole2.h"
31 #include "wine/unicode.h"
32
33 /* Window type defaults */
34
35 #define WINTYPE_DEFAULT_X           280
36 #define WINTYPE_DEFAULT_Y           100
37 #define WINTYPE_DEFAULT_WIDTH       740
38 #define WINTYPE_DEFAULT_HEIGHT      640
39 #define WINTYPE_DEFAULT_NAVWIDTH    250
40
41 typedef struct tagHHInfo
42 {
43     HH_WINTYPEW *pHHWinType;
44     HINSTANCE hInstance;
45     LPCWSTR szCmdLine;
46     DWORD dwNumTBButtons;
47     HFONT hFont;
48 } HHInfo;
49
50 extern HINSTANCE hhctrl_hinstance;
51
52 static LPWSTR HH_ANSIToUnicode(LPCSTR ansi)
53 {
54     LPWSTR unicode;
55     int count;
56
57     count = MultiByteToWideChar(CP_ACP, 0, ansi, -1, NULL, 0);
58     unicode = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR));
59     MultiByteToWideChar(CP_ACP, 0, ansi, -1, unicode, count);
60
61     return unicode;
62 }
63
64 /* Loads a string from the resource file */
65 static LPWSTR HH_LoadString(DWORD dwID)
66 {
67     LPWSTR string = NULL;
68     int iSize;
69
70     iSize = LoadStringW(hhctrl_hinstance, dwID, NULL, 0);
71     iSize += 2; /* some strings (tab text) needs double-null termination */
72
73     string = HeapAlloc(GetProcessHeap(), 0, iSize * sizeof(WCHAR));
74     LoadStringW(hhctrl_hinstance, dwID, string, iSize);
75
76     return string;
77 }
78
79 /* Child Window */
80
81 static const WCHAR szChildClass[] = {
82     'H','H',' ','C','h','i','l','d',0
83 };
84
85 static const WCHAR szEmpty[] = {0};
86
87 LRESULT CALLBACK Child_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
88 {
89     switch (message)
90     {
91         default:
92             return DefWindowProcW(hWnd, message, wParam, lParam);
93     }
94
95     return 0;
96 }
97
98 static void HH_RegisterChildWndClass(HHInfo *pHHInfo)
99 {
100     WNDCLASSEXW wcex;
101
102     wcex.cbSize         = sizeof(WNDCLASSEXW);
103     wcex.style          = 0;
104     wcex.lpfnWndProc    = (WNDPROC)Child_WndProc;
105     wcex.cbClsExtra     = 0;
106     wcex.cbWndExtra     = 0;
107     wcex.hInstance      = pHHInfo->hInstance;
108     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
109     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
110     wcex.hbrBackground  = (HBRUSH)(COLOR_3DFACE);
111     wcex.lpszMenuName   = NULL;
112     wcex.lpszClassName  = szChildClass;
113     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
114
115     RegisterClassExW(&wcex);
116 }
117
118 /* Toolbar */
119
120 #define ICON_SIZE   20
121
122 static void TB_AddButton(TBBUTTON *pButtons, DWORD dwIndex, DWORD dwID)
123 {
124     /* FIXME: Load the correct button bitmaps */
125     pButtons[dwIndex].iBitmap = STD_PRINT;
126     pButtons[dwIndex].idCommand = dwID;
127     pButtons[dwIndex].fsState = TBSTATE_ENABLED;
128     pButtons[dwIndex].fsStyle = BTNS_BUTTON;
129     pButtons[dwIndex].dwData = 0;
130     pButtons[dwIndex].iString = 0;
131 }
132
133 static void TB_AddButtonsFromFlags(TBBUTTON *pButtons, DWORD dwButtonFlags, LPDWORD pdwNumButtons)
134 {
135     *pdwNumButtons = 0;
136
137     if (dwButtonFlags & HHWIN_BUTTON_EXPAND)
138         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_EXPAND);
139
140     if (dwButtonFlags & HHWIN_BUTTON_BACK)
141         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_BACK);
142
143     if (dwButtonFlags & HHWIN_BUTTON_FORWARD)
144         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_FORWARD);
145
146     if (dwButtonFlags & HHWIN_BUTTON_STOP)
147         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_STOP);
148
149     if (dwButtonFlags & HHWIN_BUTTON_REFRESH)
150         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_REFRESH);
151
152     if (dwButtonFlags & HHWIN_BUTTON_HOME)
153         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_HOME);
154
155     if (dwButtonFlags & HHWIN_BUTTON_SYNC)
156         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_SYNC);
157
158     if (dwButtonFlags & HHWIN_BUTTON_OPTIONS)
159         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_OPTIONS);
160
161     if (dwButtonFlags & HHWIN_BUTTON_PRINT)
162         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_PRINT);
163
164     if (dwButtonFlags & HHWIN_BUTTON_JUMP1)
165         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_JUMP1);
166
167     if (dwButtonFlags & HHWIN_BUTTON_JUMP2)
168         TB_AddButton(pButtons,(*pdwNumButtons)++, IDTB_JUMP2);
169
170     if (dwButtonFlags & HHWIN_BUTTON_ZOOM)
171         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_ZOOM);
172
173     if (dwButtonFlags & HHWIN_BUTTON_TOC_NEXT)
174         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_TOC_NEXT);
175
176     if (dwButtonFlags & HHWIN_BUTTON_TOC_PREV)
177         TB_AddButton(pButtons, (*pdwNumButtons)++, IDTB_TOC_PREV);
178 }
179
180 static BOOL HH_AddToolbar(HHInfo *pHHInfo)
181 {
182     HWND hToolbar;
183     HWND hwndParent = pHHInfo->pHHWinType->hwndHelp;
184     DWORD toolbarFlags = pHHInfo->pHHWinType->fsToolBarFlags;
185     TBBUTTON buttons[IDTB_TOC_PREV - IDTB_EXPAND];
186     TBADDBITMAP tbAB;
187     DWORD dwStyles, dwExStyles;
188     DWORD dwNumButtons, dwIndex;
189
190     /* FIXME: Remove the following line once we read the CHM file */
191     toolbarFlags = HHWIN_BUTTON_EXPAND | HHWIN_BUTTON_BACK | HHWIN_BUTTON_STOP |
192                    HHWIN_BUTTON_REFRESH | HHWIN_BUTTON_HOME | HHWIN_BUTTON_PRINT;
193     TB_AddButtonsFromFlags(buttons, toolbarFlags, &dwNumButtons);
194     pHHInfo->dwNumTBButtons = dwNumButtons;
195
196     dwStyles = WS_CHILDWINDOW | WS_VISIBLE | TBSTYLE_FLAT |
197                TBSTYLE_WRAPABLE | TBSTYLE_TOOLTIPS | CCS_NODIVIDER;
198     dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
199
200     hToolbar = CreateWindowExW(dwExStyles, TOOLBARCLASSNAMEW, NULL, dwStyles,
201                                0, 0, 0, 0, hwndParent, NULL,
202                                pHHInfo->hInstance, NULL);
203     if (!hToolbar)
204         return FALSE;
205
206     SendMessageW(hToolbar, TB_SETBITMAPSIZE, 0, MAKELONG(ICON_SIZE, ICON_SIZE));
207     SendMessageW(hToolbar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
208     SendMessageW(hToolbar, WM_SETFONT, (WPARAM)pHHInfo->hFont, TRUE);
209
210     /* FIXME: Load correct icons for all buttons */
211     tbAB.hInst = HINST_COMMCTRL;
212     tbAB.nID = IDB_STD_LARGE_COLOR;
213     SendMessageW(hToolbar, TB_ADDBITMAP, 0, (LPARAM)&tbAB);
214
215     for (dwIndex = 0; dwIndex < dwNumButtons; dwIndex++)
216     {
217         LPWSTR szBuf = HH_LoadString(buttons[dwIndex].idCommand);
218         DWORD dwLen = strlenW(szBuf);
219         szBuf[dwLen + 2] = 0; /* Double-null terminate */
220
221         buttons[dwIndex].iString = (DWORD)SendMessageW(hToolbar, TB_ADDSTRINGW, 0, (LPARAM)szBuf);
222         HeapFree(GetProcessHeap(), 0, szBuf);
223     }
224
225     SendMessageW(hToolbar, TB_ADDBUTTONSW, dwNumButtons, (LPARAM)&buttons);
226     SendMessageW(hToolbar, TB_AUTOSIZE, 0, 0);
227     ShowWindow(hToolbar, SW_SHOW);
228
229     pHHInfo->pHHWinType->hwndToolBar = hToolbar;
230     return TRUE;
231 }
232
233 /* Navigation Pane */
234
235 static void NP_GetNavigationRect(HHInfo *pHHInfo, RECT *rc)
236 {
237     HWND hwndParent = pHHInfo->pHHWinType->hwndHelp;
238     HWND hwndToolbar = pHHInfo->pHHWinType->hwndToolBar;
239     RECT rectWND, rectTB;
240
241     GetClientRect(hwndParent, &rectWND);
242     GetClientRect(hwndToolbar, &rectTB);
243
244     rc->left = 0;
245     rc->top = rectTB.bottom;
246     rc->bottom = rectWND.bottom - rectTB.bottom;
247
248     if (pHHInfo->pHHWinType->fsValidMembers & HHWIN_PARAM_NAV_WIDTH)
249         rc->right = pHHInfo->pHHWinType->iNavWidth;
250     else
251         rc->right = WINTYPE_DEFAULT_NAVWIDTH;
252 }
253
254 static BOOL HH_AddNavigationPane(HHInfo *pHHInfo)
255 {
256     HWND hWnd;
257     HWND hwndParent = pHHInfo->pHHWinType->hwndHelp;
258     DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE;
259     DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
260     RECT rc;
261
262     NP_GetNavigationRect(pHHInfo, &rc);
263
264     hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
265                            rc.left, rc.top, rc.right, rc.bottom,
266                            hwndParent, NULL, pHHInfo->hInstance, NULL);
267     if (!hWnd)
268         return FALSE;
269
270     pHHInfo->pHHWinType->hwndNavigation = hWnd;
271     return TRUE;
272 }
273
274 /* HTML Pane */
275
276 static void HP_GetHTMLRect(HHInfo *pHHInfo, RECT *rc)
277 {
278     HWND hwndParent = pHHInfo->pHHWinType->hwndHelp;
279     HWND hwndToolbar = pHHInfo->pHHWinType->hwndToolBar;
280     HWND hwndNavigation = pHHInfo->pHHWinType->hwndNavigation;
281     RECT rectTB, rectWND, rectNP;
282
283     GetClientRect(hwndParent, &rectWND);
284     GetClientRect(hwndToolbar, &rectTB);
285     GetClientRect(hwndNavigation, &rectNP);
286
287     rc->left = rectNP.right;
288     rc->top = rectTB.bottom;
289     rc->right = rectWND.right - rectNP.right;
290     rc->bottom = rectWND.bottom - rectTB.bottom;
291 }
292
293 static BOOL HH_AddHTMLPane(HHInfo *pHHInfo)
294 {
295     HWND hWnd;
296     HWND hwndParent = pHHInfo->pHHWinType->hwndHelp;
297     DWORD dwStyles = WS_CHILDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN;
298     DWORD dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_CLIENTEDGE;
299     RECT rc;
300
301     HP_GetHTMLRect(pHHInfo, &rc);
302
303     hWnd = CreateWindowExW(dwExStyles, szChildClass, szEmpty, dwStyles,
304                            rc.left, rc.top, rc.right, rc.bottom,
305                            hwndParent, NULL, pHHInfo->hInstance, NULL);
306     if (!hWnd)
307         return FALSE;
308
309     /* store the pointer to the HH info struct */
310     SetWindowLongPtrA(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
311
312     ShowWindow(hWnd, SW_SHOW);
313     UpdateWindow(hWnd);
314
315     pHHInfo->pHHWinType->hwndHTML = hWnd;
316     return TRUE;
317 }
318
319 /* Viewer Window */
320
321 LRESULT CALLBACK Help_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
322 {
323     PAINTSTRUCT ps;
324     HDC hdc;
325
326     switch (message)
327     {
328
329         case WM_PAINT:
330             hdc = BeginPaint(hWnd, &ps);
331             EndPaint(hWnd, &ps);
332             break;
333         case WM_DESTROY:
334             PostQuitMessage(0);
335             break;
336
337         default:
338             return DefWindowProcW(hWnd, message, wParam, lParam);
339     }
340
341     return 0;
342 }
343
344 static BOOL HH_CreateHelpWindow(HHInfo *pHHInfo)
345 {
346     HWND hWnd;
347     HINSTANCE hInstance = pHHInfo->hInstance;
348     WNDCLASSEXW wcex;
349     DWORD dwStyles, dwExStyles;
350     DWORD x, y, width, height;
351
352     static const WCHAR windowClassW[] = {
353         'H','H',' ', 'P','a','r','e','n','t',0
354     };
355
356     static const WCHAR windowTitleW[] = {
357         'H','T','M','L',' ','H','e','l','p',0
358     };
359
360     wcex.cbSize         = sizeof(WNDCLASSEXW);
361     wcex.style          = CS_HREDRAW | CS_VREDRAW;
362     wcex.lpfnWndProc    = (WNDPROC)Help_WndProc;
363     wcex.cbClsExtra     = 0;
364     wcex.cbWndExtra     = 0;
365     wcex.hInstance      = hInstance;
366     wcex.hIcon          = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
367     wcex.hCursor        = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
368     wcex.hbrBackground  = (HBRUSH)(COLOR_BACKGROUND + 1);
369     wcex.lpszMenuName   = NULL;
370     wcex.lpszClassName  = windowClassW;
371     wcex.hIconSm        = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
372
373     RegisterClassExW(&wcex);
374
375     dwStyles = WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
376     dwExStyles = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR |
377                  WS_EX_WINDOWEDGE | WS_EX_APPWINDOW;
378
379     /* these will be loaded from the CHM file in the future if they're provided */
380     x = WINTYPE_DEFAULT_X;
381     y = WINTYPE_DEFAULT_Y;
382     width = WINTYPE_DEFAULT_WIDTH;
383     height = WINTYPE_DEFAULT_HEIGHT;
384
385     hWnd = CreateWindowExW(dwExStyles, windowClassW, windowTitleW, dwStyles,
386                            x, y, width, height, NULL, NULL, hInstance, NULL);
387     if (!hWnd)
388         return FALSE;
389
390     ShowWindow(hWnd, SW_SHOW);
391     UpdateWindow(hWnd);
392
393     /* store the pointer to the HH info struct */
394     SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pHHInfo);
395
396     pHHInfo->pHHWinType->hwndHelp = hWnd;
397     return TRUE;
398 }
399
400 static void HH_CreateFont(HHInfo *pHHInfo)
401 {
402     LOGFONTW lf;
403
404     GetObjectW(GetStockObject(ANSI_VAR_FONT), sizeof(LOGFONTW), &lf);
405     lf.lfWeight = FW_NORMAL;
406     lf.lfItalic = FALSE;
407     lf.lfUnderline = FALSE;
408
409     pHHInfo->hFont = CreateFontIndirectW(&lf);
410 }
411
412 static void HH_InitRequiredControls(DWORD dwControls)
413 {
414     INITCOMMONCONTROLSEX icex;
415
416     icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
417     icex.dwICC = dwControls;
418     InitCommonControlsEx(&icex);
419 }
420
421 /* Creates the whole package */
422 static BOOL HH_CreateViewer(HHInfo *pHHInfo)
423 {
424     HH_CreateFont(pHHInfo);
425
426     if (!HH_CreateHelpWindow(pHHInfo))
427         return FALSE;
428
429     HH_InitRequiredControls(ICC_BAR_CLASSES);
430
431     if (!HH_AddToolbar(pHHInfo))
432         return FALSE;
433
434     HH_RegisterChildWndClass(pHHInfo);
435
436     if (!HH_AddNavigationPane(pHHInfo))
437         return FALSE;
438
439     if (!HH_AddHTMLPane(pHHInfo))
440         return FALSE;
441
442     return TRUE;
443 }
444
445 static HHInfo *HH_OpenHH(HINSTANCE hInstance, LPCWSTR szCmdLine)
446 {
447     HHInfo *pHHInfo = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HHInfo));
448
449     pHHInfo->pHHWinType = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HH_WINTYPEW));
450     pHHInfo->hInstance = hInstance;
451     pHHInfo->szCmdLine = szCmdLine;
452
453     return pHHInfo;
454 }
455
456 static void HH_Close(HHInfo *pHHInfo)
457 {
458     if (!pHHInfo)
459         return;
460
461     HeapFree(GetProcessHeap(), 0, pHHInfo->pHHWinType);
462 }
463
464 /* FIXME: Check szCmdLine for bad arguments */
465 int WINAPI doWinMain(HINSTANCE hInstance, LPSTR szCmdLine)
466 {
467     MSG msg;
468     HHInfo *pHHInfo;
469
470     if (OleInitialize(NULL) != S_OK)
471         return -1;
472
473     pHHInfo = HH_OpenHH(hInstance, HH_ANSIToUnicode(szCmdLine));
474     if (!pHHInfo || !HH_CreateViewer(pHHInfo))
475     {
476         OleUninitialize();
477         return -1;
478     }
479
480     while (GetMessageW(&msg, 0, 0, 0))
481     {
482         TranslateMessage(&msg);
483         DispatchMessageW(&msg);
484     }
485
486     HH_Close(pHHInfo);
487     HeapFree(GetProcessHeap(), 0, pHHInfo);
488     OleUninitialize();
489
490     return 0;
491 }