push 0a6b4a6c9e6bf38a9b3d5ed71f415701d1ca15d1
[wine/hacks.git] / programs / winhelp / winhelp.c
blob44e640ed89cdec06dfcd366a106b236da0509568
1 /*
2 * Help Viewer
4 * Copyright 1996 Ulrich Schmid <uschmid@mail.hh.provi.de>
5 * 2002 Sylvain Petreolle <spetreolle@yahoo.fr>
6 * 2002 Eric Pouech <eric.pouech@wanadoo.fr>
7 * 2004 Ken Belleau <jamez@ivic.qc.ca>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include <assert.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winuser.h"
34 #include "commdlg.h"
35 #include "winhelp.h"
36 #include "winhelp_res.h"
37 #include "shellapi.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(winhelp);
43 static BOOL WINHELP_RegisterWinClasses(void);
44 static LRESULT CALLBACK WINHELP_MainWndProc(HWND, UINT, WPARAM, LPARAM);
45 static LRESULT CALLBACK WINHELP_TextWndProc(HWND, UINT, WPARAM, LPARAM);
46 static LRESULT CALLBACK WINHELP_ButtonBoxWndProc(HWND, UINT, WPARAM, LPARAM);
47 static LRESULT CALLBACK WINHELP_ButtonWndProc(HWND, UINT, WPARAM, LPARAM);
48 static LRESULT CALLBACK WINHELP_HistoryWndProc(HWND, UINT, WPARAM, LPARAM);
49 static LRESULT CALLBACK WINHELP_ShadowWndProc(HWND, UINT, WPARAM, LPARAM);
50 static void WINHELP_CheckPopup(UINT);
51 static BOOL WINHELP_SplitLines(HWND hWnd, LPSIZE);
52 static void WINHELP_InitFonts(HWND hWnd);
53 static void WINHELP_DeleteLines(WINHELP_WINDOW*);
54 static void WINHELP_DeleteWindow(WINHELP_WINDOW*);
55 static void WINHELP_SetupText(HWND hWnd);
56 static WINHELP_LINE_PART* WINHELP_IsOverLink(WINHELP_WINDOW*, WPARAM, LPARAM);
58 WINHELP_GLOBALS Globals = {3, NULL, NULL, 0, TRUE, NULL, NULL, NULL, NULL};
61 /***********************************************************************
63 * WINHELP_GetOpenFileName
65 BOOL WINHELP_GetOpenFileName(LPSTR lpszFile, int len)
67 OPENFILENAME openfilename;
68 CHAR szDir[MAX_PATH];
69 CHAR szzFilter[2 * MAX_STRING_LEN + 100];
70 LPSTR p = szzFilter;
72 WINE_TRACE("()\n");
74 LoadString(Globals.hInstance, STID_HELP_FILES_HLP, p, MAX_STRING_LEN);
75 p += strlen(p) + 1;
76 lstrcpy(p, "*.hlp");
77 p += strlen(p) + 1;
78 LoadString(Globals.hInstance, STID_ALL_FILES, p, MAX_STRING_LEN);
79 p += strlen(p) + 1;
80 lstrcpy(p, "*.*");
81 p += strlen(p) + 1;
82 *p = '\0';
84 GetCurrentDirectory(sizeof(szDir), szDir);
86 lpszFile[0]='\0';
88 openfilename.lStructSize = sizeof(OPENFILENAME);
89 openfilename.hwndOwner = NULL;
90 openfilename.hInstance = Globals.hInstance;
91 openfilename.lpstrFilter = szzFilter;
92 openfilename.lpstrCustomFilter = 0;
93 openfilename.nMaxCustFilter = 0;
94 openfilename.nFilterIndex = 1;
95 openfilename.lpstrFile = lpszFile;
96 openfilename.nMaxFile = len;
97 openfilename.lpstrFileTitle = 0;
98 openfilename.nMaxFileTitle = 0;
99 openfilename.lpstrInitialDir = szDir;
100 openfilename.lpstrTitle = 0;
101 openfilename.Flags = 0;
102 openfilename.nFileOffset = 0;
103 openfilename.nFileExtension = 0;
104 openfilename.lpstrDefExt = 0;
105 openfilename.lCustData = 0;
106 openfilename.lpfnHook = 0;
107 openfilename.lpTemplateName = 0;
109 return GetOpenFileName(&openfilename);
112 /***********************************************************************
114 * WINHELP_LookupHelpFile
116 HLPFILE* WINHELP_LookupHelpFile(LPCSTR lpszFile)
118 HLPFILE* hlpfile;
119 char szFullName[MAX_PATH];
120 char szAddPath[MAX_PATH];
121 char *p;
124 * NOTE: This is needed by popup windows only.
125 * In other cases it's not needed but does not hurt though.
127 if (Globals.active_win && Globals.active_win->page && Globals.active_win->page->file)
129 strcpy(szAddPath, Globals.active_win->page->file->lpszPath);
130 p = strrchr(szAddPath, '\\');
131 if (p) *p = 0;
135 * FIXME: Should we swap conditions?
137 if (!SearchPath(NULL, lpszFile, ".hlp", MAX_PATH, szFullName, NULL) &&
138 !SearchPath(szAddPath, lpszFile, ".hlp", MAX_PATH, szFullName, NULL))
140 if (WINHELP_MessageBoxIDS_s(STID_FILE_NOT_FOUND_s, lpszFile, STID_WHERROR,
141 MB_YESNO|MB_ICONQUESTION) != IDYES)
142 return NULL;
143 if (!WINHELP_GetOpenFileName(szFullName, MAX_PATH))
144 return NULL;
146 hlpfile = HLPFILE_ReadHlpFile(szFullName);
147 if (!hlpfile)
148 WINHELP_MessageBoxIDS_s(STID_HLPFILE_ERROR_s, lpszFile,
149 STID_WHERROR, MB_OK|MB_ICONSTOP);
150 return hlpfile;
153 /******************************************************************
154 * WINHELP_GetWindowInfo
158 HLPFILE_WINDOWINFO* WINHELP_GetWindowInfo(HLPFILE* hlpfile, LPCSTR name)
160 static HLPFILE_WINDOWINFO mwi;
161 unsigned int i;
163 if (!name || !name[0])
164 name = Globals.active_win->lpszName;
166 if (hlpfile)
167 for (i = 0; i < hlpfile->numWindows; i++)
168 if (!strcmp(hlpfile->windows[i].name, name))
169 return &hlpfile->windows[i];
171 if (strcmp(name, "main") != 0)
173 WINE_FIXME("Couldn't find window info for %s\n", name);
174 assert(0);
175 return NULL;
177 if (!mwi.name[0])
179 strcpy(mwi.type, "primary");
180 strcpy(mwi.name, "main");
181 if (!LoadString(Globals.hInstance, STID_WINE_HELP,
182 mwi.caption, sizeof(mwi.caption)))
183 strcpy(mwi.caption, hlpfile->lpszTitle);
184 mwi.origin.x = mwi.origin.y = mwi.size.cx = mwi.size.cy = CW_USEDEFAULT;
185 mwi.style = SW_SHOW;
186 mwi.win_style = WS_OVERLAPPEDWINDOW;
187 mwi.sr_color = mwi.sr_color = 0xFFFFFF;
189 return &mwi;
192 /******************************************************************
193 * HLPFILE_GetPopupWindowInfo
197 static HLPFILE_WINDOWINFO* WINHELP_GetPopupWindowInfo(HLPFILE* hlpfile, HWND hParentWnd, POINT* mouse)
199 static HLPFILE_WINDOWINFO wi;
201 RECT parent_rect;
203 wi.type[0] = wi.name[0] = wi.caption[0] = '\0';
205 /* Calculate horizontal size and position of a popup window */
206 GetWindowRect(hParentWnd, &parent_rect);
207 wi.size.cx = (parent_rect.right - parent_rect.left) / 2;
208 wi.size.cy = 10; /* need a non null value, so that border are taken into account while computing */
210 wi.origin = *mouse;
211 ClientToScreen(hParentWnd, &wi.origin);
212 wi.origin.x -= wi.size.cx / 2;
213 wi.origin.x = min(wi.origin.x, GetSystemMetrics(SM_CXSCREEN) - wi.size.cx);
214 wi.origin.x = max(wi.origin.x, 0);
216 wi.style = SW_SHOW;
217 wi.win_style = WS_POPUP | WS_BORDER;
218 wi.sr_color = wi.sr_color = 0xFFFFFF;
220 return &wi;
223 /***********************************************************************
225 * WinMain
227 int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
229 MSG msg;
230 LONG lHash = 0;
231 HLPFILE* hlpfile;
232 static CHAR default_wndname[] = "main";
233 LPSTR wndname = default_wndname;
234 WINHELP_DLL* dll;
236 Globals.hInstance = hInstance;
238 /* Get options */
239 while (*cmdline && (*cmdline == ' ' || *cmdline == '-'))
241 CHAR option;
242 LPCSTR topic_id;
243 if (*cmdline++ == ' ') continue;
245 option = *cmdline;
246 if (option) cmdline++;
247 while (*cmdline && *cmdline == ' ') cmdline++;
248 switch (option)
250 case 'i':
251 case 'I':
252 topic_id = cmdline;
253 while (*cmdline && *cmdline != ' ') cmdline++;
254 if (*cmdline) *cmdline++ = '\0';
255 lHash = HLPFILE_Hash(topic_id);
256 break;
258 case '3':
259 case '4':
260 Globals.wVersion = option - '0';
261 break;
263 case 'x':
264 show = SW_HIDE;
265 Globals.isBook = FALSE;
266 break;
268 default:
269 WINE_FIXME("Unsupported cmd line: %s\n", cmdline);
270 break;
274 /* Create primary window */
275 if (!WINHELP_RegisterWinClasses())
277 WINE_FIXME("Couldn't register classes\n");
278 return 0;
281 if (*cmdline)
283 char* ptr;
284 if ((*cmdline == '"') && (ptr = strchr(cmdline+1, '"')))
286 cmdline++;
287 *ptr = '\0';
289 if ((ptr = strchr(cmdline, '>')))
291 *ptr = '\0';
292 wndname = ptr + 1;
294 hlpfile = WINHELP_LookupHelpFile(cmdline);
295 if (!hlpfile) return 0;
297 else hlpfile = NULL;
298 WINHELP_CreateHelpWindowByHash(hlpfile, lHash,
299 WINHELP_GetWindowInfo(hlpfile, wndname), show);
301 /* Message loop */
302 while (GetMessage(&msg, 0, 0, 0))
304 TranslateMessage(&msg);
305 DispatchMessage(&msg);
307 for (dll = Globals.dlls; dll; dll = dll->next)
309 if (dll->class & DC_INITTERM) dll->handler(DW_TERM, 0, 0);
311 return 0;
314 /***********************************************************************
316 * RegisterWinClasses
318 static BOOL WINHELP_RegisterWinClasses(void)
320 WNDCLASS class_main, class_button_box, class_text, class_shadow, class_history;
322 class_main.style = CS_HREDRAW | CS_VREDRAW;
323 class_main.lpfnWndProc = WINHELP_MainWndProc;
324 class_main.cbClsExtra = 0;
325 class_main.cbWndExtra = sizeof(LONG);
326 class_main.hInstance = Globals.hInstance;
327 class_main.hIcon = LoadIcon(0, IDI_APPLICATION);
328 class_main.hCursor = LoadCursor(0, IDC_ARROW);
329 class_main.hbrBackground = GetStockObject(WHITE_BRUSH);
330 class_main.lpszMenuName = 0;
331 class_main.lpszClassName = MAIN_WIN_CLASS_NAME;
333 class_button_box = class_main;
334 class_button_box.lpfnWndProc = WINHELP_ButtonBoxWndProc;
335 class_button_box.hbrBackground = GetStockObject(GRAY_BRUSH);
336 class_button_box.lpszClassName = BUTTON_BOX_WIN_CLASS_NAME;
338 class_text = class_main;
339 class_text.lpfnWndProc = WINHELP_TextWndProc;
340 class_text.hbrBackground = 0;
341 class_text.lpszClassName = TEXT_WIN_CLASS_NAME;
343 class_shadow = class_main;
344 class_shadow.lpfnWndProc = WINHELP_ShadowWndProc;
345 class_shadow.hbrBackground = GetStockObject(GRAY_BRUSH);
346 class_shadow.lpszClassName = SHADOW_WIN_CLASS_NAME;
348 class_history = class_main;
349 class_history.lpfnWndProc = WINHELP_HistoryWndProc;
350 class_history.lpszClassName = HISTORY_WIN_CLASS_NAME;
352 return (RegisterClass(&class_main) &&
353 RegisterClass(&class_button_box) &&
354 RegisterClass(&class_text) &&
355 RegisterClass(&class_shadow) &&
356 RegisterClass(&class_history));
359 typedef struct
361 WORD size;
362 WORD command;
363 LONG data;
364 LONG reserved;
365 WORD ofsFilename;
366 WORD ofsData;
367 } WINHELP,*LPWINHELP;
369 /******************************************************************
370 * WINHELP_HandleCommand
374 static LRESULT WINHELP_HandleCommand(HWND hSrcWnd, LPARAM lParam)
376 COPYDATASTRUCT* cds = (COPYDATASTRUCT*)lParam;
377 WINHELP* wh;
379 if (cds->dwData != 0xA1DE505)
381 WINE_FIXME("Wrong magic number (%08lx)\n", cds->dwData);
382 return 0;
385 wh = (WINHELP*)cds->lpData;
387 if (wh)
389 char* ptr = (wh->ofsFilename) ? (LPSTR)wh + wh->ofsFilename : NULL;
391 WINE_TRACE("Got[%u]: cmd=%u data=%08x fn=%s\n",
392 wh->size, wh->command, wh->data, ptr);
393 switch (wh->command)
395 case HELP_CONTEXT:
396 if (ptr)
398 MACRO_JumpContext(ptr, "main", wh->data);
400 break;
401 case HELP_QUIT:
402 MACRO_Exit();
403 break;
404 case HELP_CONTENTS:
405 if (ptr)
407 MACRO_JumpContents(ptr, "main");
409 break;
410 case HELP_HELPONHELP:
411 MACRO_HelpOn();
412 break;
413 /* case HELP_SETINDEX: */
414 case HELP_SETCONTENTS:
415 if (ptr)
417 MACRO_SetContents(ptr, wh->data);
419 break;
420 case HELP_CONTEXTPOPUP:
421 if (ptr)
423 MACRO_PopupContext(ptr, wh->data);
425 break;
426 /* case HELP_FORCEFILE:*/
427 /* case HELP_CONTEXTMENU: */
428 case HELP_FINDER:
429 /* in fact, should be the topic dialog box */
430 WINE_FIXME("HELP_FINDER: stub\n");
431 if (ptr)
433 MACRO_JumpHash(ptr, "main", 0);
435 break;
436 /* case HELP_WM_HELP: */
437 /* case HELP_SETPOPUP_POS: */
438 /* case HELP_KEY: */
439 /* case HELP_COMMAND: */
440 /* case HELP_PARTIALKEY: */
441 /* case HELP_MULTIKEY: */
442 /* case HELP_SETWINPOS: */
443 default:
444 WINE_FIXME("Unhandled command (%x) for remote winhelp control\n", wh->command);
445 break;
448 return 0L;
451 /******************************************************************
452 * WINHELP_ReuseWindow
456 static BOOL WINHELP_ReuseWindow(WINHELP_WINDOW* win, WINHELP_WINDOW* oldwin,
457 HLPFILE_PAGE* page, int nCmdShow)
459 unsigned int i;
461 win->hMainWnd = oldwin->hMainWnd;
462 win->hButtonBoxWnd = oldwin->hButtonBoxWnd;
463 win->hTextWnd = oldwin->hTextWnd;
464 win->hHistoryWnd = oldwin->hHistoryWnd;
465 oldwin->hMainWnd = oldwin->hButtonBoxWnd = oldwin->hTextWnd = oldwin->hHistoryWnd = 0;
466 win->hBrush = oldwin->hBrush;
468 SetWindowLong(win->hMainWnd, 0, (LONG)win);
469 SetWindowLong(win->hButtonBoxWnd, 0, (LONG)win);
470 SetWindowLong(win->hTextWnd, 0, (LONG)win);
471 SetWindowLong(win->hHistoryWnd, 0, (LONG)win);
473 WINHELP_InitFonts(win->hMainWnd);
475 if (page)
476 SetWindowText(win->hMainWnd, page->file->lpszTitle);
478 WINHELP_SetupText(win->hTextWnd);
479 InvalidateRect(win->hTextWnd, NULL, TRUE);
480 SendMessage(win->hMainWnd, WM_USER, 0, 0);
481 ShowWindow(win->hMainWnd, nCmdShow);
482 UpdateWindow(win->hTextWnd);
484 if (!(win->info->win_style & WS_POPUP))
486 unsigned num;
488 memcpy(win->history, oldwin->history, sizeof(win->history));
489 win->histIndex = oldwin->histIndex;
491 /* FIXME: when using back, we shouldn't update the history... */
493 if (page)
495 for (i = 0; i < win->histIndex; i++)
496 if (win->history[i] == page) break;
498 /* if the new page is already in the history, do nothing */
499 if (i == win->histIndex)
501 num = sizeof(win->history) / sizeof(win->history[0]);
502 if (win->histIndex == num)
504 /* we're full, remove latest entry */
505 HLPFILE_FreeHlpFile(win->history[0]->file);
506 memmove(&win->history[0], &win->history[1],
507 (num - 1) * sizeof(win->history[0]));
508 win->histIndex--;
510 win->history[win->histIndex++] = page;
511 page->file->wRefCount++;
512 if (win->hHistoryWnd) InvalidateRect(win->hHistoryWnd, NULL, TRUE);
516 memcpy(win->back, oldwin->back, sizeof(win->back));
517 win->backIndex = oldwin->backIndex;
519 if (page)
521 num = sizeof(win->back) / sizeof(win->back[0]);
522 if (win->backIndex == num)
524 /* we're full, remove latest entry */
525 HLPFILE_FreeHlpFile(win->back[0]->file);
526 memmove(&win->back[0], &win->back[1],
527 (num - 1) * sizeof(win->back[0]));
528 win->backIndex--;
530 win->back[win->backIndex++] = page;
531 page->file->wRefCount++;
534 else
535 win->backIndex = win->histIndex = 0;
537 oldwin->histIndex = oldwin->backIndex = 0;
538 WINHELP_DeleteWindow(oldwin);
539 return TRUE;
542 /***********************************************************************
544 * WINHELP_CreateHelpWindow
546 BOOL WINHELP_CreateHelpWindow(HLPFILE_PAGE* page, HLPFILE_WINDOWINFO* wi,
547 int nCmdShow)
549 WINHELP_WINDOW *win, *oldwin;
550 HWND hWnd;
551 BOOL bPrimary;
552 BOOL bPopup;
553 LPSTR name;
554 DWORD ex_style;
556 bPrimary = !lstrcmpi(wi->name, "main");
557 bPopup = wi->win_style & WS_POPUP;
559 /* Initialize WINHELP_WINDOW struct */
560 win = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
561 sizeof(WINHELP_WINDOW) + strlen(wi->name) + 1);
562 if (!win) return FALSE;
564 win->next = Globals.win_list;
565 Globals.win_list = win;
567 name = (char*)win + sizeof(WINHELP_WINDOW);
568 lstrcpy(name, wi->name);
569 win->lpszName = name;
571 win->page = page;
573 win->hArrowCur = LoadCursorA(0, (LPSTR)IDC_ARROW);
574 win->hHandCur = LoadCursorA(0, (LPSTR)IDC_HAND);
576 win->info = wi;
578 Globals.active_win = win;
580 /* Initialize default pushbuttons */
581 if (bPrimary && page)
583 CHAR buffer[MAX_STRING_LEN];
585 LoadString(Globals.hInstance, STID_CONTENTS, buffer, sizeof(buffer));
586 MACRO_CreateButton("BTN_CONTENTS", buffer, "Contents()");
587 LoadString(Globals.hInstance, STID_SEARCH,buffer, sizeof(buffer));
588 MACRO_CreateButton("BTN_SEARCH", buffer, "Search()");
589 LoadString(Globals.hInstance, STID_BACK, buffer, sizeof(buffer));
590 MACRO_CreateButton("BTN_BACK", buffer, "Back()");
591 LoadString(Globals.hInstance, STID_HISTORY, buffer, sizeof(buffer));
592 MACRO_CreateButton("BTN_HISTORY", buffer, "History()");
593 LoadString(Globals.hInstance, STID_TOPICS, buffer, sizeof(buffer));
594 MACRO_CreateButton("BTN_TOPICS", buffer, "Finder()");
597 /* Initialize file specific pushbuttons */
598 if (!(wi->win_style & WS_POPUP) && page)
600 HLPFILE_MACRO *macro;
601 for (macro = page->file->first_macro; macro; macro = macro->next)
602 MACRO_ExecuteMacro(macro->lpszMacro);
604 for (macro = page->first_macro; macro; macro = macro->next)
605 MACRO_ExecuteMacro(macro->lpszMacro);
608 /* Reuse existing window */
609 if (!bPopup)
611 for (oldwin = win->next; oldwin; oldwin = oldwin->next)
613 if (!lstrcmpi(oldwin->lpszName, wi->name))
615 return WINHELP_ReuseWindow(win, oldwin, page, nCmdShow);
618 if (page)
620 win->histIndex = win->backIndex = 1;
621 win->history[0] = win->back[0] = page;
622 page->file->wRefCount += 2;
623 strcpy(wi->caption, page->file->lpszTitle);
627 ex_style = 0;
628 if (bPopup) ex_style = WS_EX_TOOLWINDOW;
629 hWnd = CreateWindowEx(ex_style, bPopup ? TEXT_WIN_CLASS_NAME : MAIN_WIN_CLASS_NAME,
630 wi->caption,
631 bPrimary ? WS_OVERLAPPEDWINDOW : wi->win_style,
632 wi->origin.x, wi->origin.y, wi->size.cx, wi->size.cy,
633 NULL, bPrimary ? LoadMenu(Globals.hInstance, MAKEINTRESOURCE(MAIN_MENU)) : 0,
634 Globals.hInstance, win);
636 ShowWindow(hWnd, nCmdShow);
637 UpdateWindow(hWnd);
639 return TRUE;
642 /***********************************************************************
644 * WINHELP_CreateHelpWindowByHash
646 BOOL WINHELP_CreateHelpWindowByHash(HLPFILE* hlpfile, LONG lHash,
647 HLPFILE_WINDOWINFO* wi, int nCmdShow)
649 HLPFILE_PAGE* page = NULL;
651 if (hlpfile)
652 page = lHash ? HLPFILE_PageByHash(hlpfile, lHash) :
653 HLPFILE_Contents(hlpfile);
654 if (page) page->file->wRefCount++;
655 return WINHELP_CreateHelpWindow(page, wi, nCmdShow);
658 /***********************************************************************
660 * WINHELP_CreateHelpWindowByMap
662 BOOL WINHELP_CreateHelpWindowByMap(HLPFILE* hlpfile, LONG lMap,
663 HLPFILE_WINDOWINFO* wi, int nCmdShow)
665 HLPFILE_PAGE* page = NULL;
667 page = HLPFILE_PageByMap(hlpfile, lMap);
668 if (page) page->file->wRefCount++;
669 return WINHELP_CreateHelpWindow(page, wi, nCmdShow);
672 /***********************************************************************
674 * WINHELP_MainWndProc
676 static LRESULT CALLBACK WINHELP_MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
678 WINHELP_WINDOW *win;
679 WINHELP_BUTTON *button;
680 RECT rect, button_box_rect;
681 INT text_top, curPos, min, max, dy, keyDelta;
683 WINHELP_CheckPopup(msg);
685 switch (msg)
687 case WM_NCCREATE:
688 win = (WINHELP_WINDOW*) ((LPCREATESTRUCT) lParam)->lpCreateParams;
689 SetWindowLong(hWnd, 0, (LONG) win);
690 win->hMainWnd = hWnd;
691 break;
693 case WM_CREATE:
694 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
696 /* Create button box and text Window */
697 CreateWindow(BUTTON_BOX_WIN_CLASS_NAME, "", WS_CHILD | WS_VISIBLE,
698 0, 0, 0, 0, hWnd, 0, Globals.hInstance, win);
700 CreateWindow(TEXT_WIN_CLASS_NAME, "", WS_CHILD | WS_VISIBLE,
701 0, 0, 0, 0, hWnd, 0, Globals.hInstance, win);
703 /* Fall through */
704 case WM_USER:
705 case WM_WINDOWPOSCHANGED:
706 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
707 GetClientRect(hWnd, &rect);
709 /* Update button box and text Window */
710 SetWindowPos(win->hButtonBoxWnd, HWND_TOP,
711 rect.left, rect.top,
712 rect.right - rect.left,
713 rect.bottom - rect.top, 0);
715 GetWindowRect(win->hButtonBoxWnd, &button_box_rect);
716 text_top = rect.top + button_box_rect.bottom - button_box_rect.top;
718 SetWindowPos(win->hTextWnd, HWND_TOP,
719 rect.left, text_top,
720 rect.right - rect.left,
721 rect.bottom - text_top, 0);
723 break;
725 case WM_COMMAND:
726 Globals.active_win = win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
727 switch (wParam)
729 /* Menu FILE */
730 case MNID_FILE_OPEN: MACRO_FileOpen(); break;
731 case MNID_FILE_PRINT: MACRO_Print(); break;
732 case MNID_FILE_SETUP: MACRO_PrinterSetup(); break;
733 case MNID_FILE_EXIT: MACRO_Exit(); break;
735 /* Menu EDIT */
736 case MNID_EDIT_COPYDLG: MACRO_CopyDialog(); break;
737 case MNID_EDIT_ANNOTATE:MACRO_Annotate(); break;
739 /* Menu Bookmark */
740 case MNID_BKMK_DEFINE: MACRO_BookmarkDefine(); break;
742 /* Menu Help */
743 case MNID_HELP_HELPON: MACRO_HelpOn(); break;
744 case MNID_HELP_HELPTOP: MACRO_HelpOnTop(); break;
745 case MNID_HELP_ABOUT: MACRO_About(); break;
746 case MNID_HELP_WINE: ShellAbout(hWnd, "WINE", "Help", 0); break;
748 default:
749 /* Buttons */
750 for (button = win->first_button; button; button = button->next)
751 if (wParam == button->wParam) break;
752 if (button)
753 MACRO_ExecuteMacro(button->lpszMacro);
754 else
755 WINHELP_MessageBoxIDS(STID_NOT_IMPLEMENTED, 0x121, MB_OK);
756 break;
758 break;
759 case WM_DESTROY:
760 if (Globals.hPopupWnd) DestroyWindow(Globals.hPopupWnd);
761 break;
762 case WM_COPYDATA:
763 return WINHELP_HandleCommand((HWND)wParam, lParam);
765 case WM_KEYDOWN:
766 keyDelta = 0;
768 switch (wParam)
770 case VK_UP:
771 case VK_DOWN:
772 keyDelta = GetSystemMetrics(SM_CXVSCROLL);
773 if (wParam == VK_UP)
774 keyDelta = -keyDelta;
776 case VK_PRIOR:
777 case VK_NEXT:
778 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
779 curPos = GetScrollPos(win->hTextWnd, SB_VERT);
780 GetScrollRange(win->hTextWnd, SB_VERT, &min, &max);
782 if (keyDelta == 0)
784 GetClientRect(win->hTextWnd, &rect);
785 keyDelta = (rect.bottom - rect.top) / 2;
786 if (wParam == VK_PRIOR)
787 keyDelta = -keyDelta;
790 curPos += keyDelta;
791 if (curPos > max)
792 curPos = max;
793 else if (curPos < min)
794 curPos = min;
796 dy = GetScrollPos(win->hTextWnd, SB_VERT) - curPos;
797 SetScrollPos(win->hTextWnd, SB_VERT, curPos, TRUE);
798 ScrollWindow(win->hTextWnd, 0, dy, NULL, NULL);
799 UpdateWindow(win->hTextWnd);
800 return 0;
802 case VK_ESCAPE:
803 MACRO_Exit();
804 return 0;
806 break;
808 return DefWindowProc(hWnd, msg, wParam, lParam);
811 /***********************************************************************
813 * WINHELP_ButtonBoxWndProc
815 static LRESULT CALLBACK WINHELP_ButtonBoxWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
817 WINDOWPOS *winpos;
818 WINHELP_WINDOW *win;
819 WINHELP_BUTTON *button;
820 SIZE button_size;
821 INT x, y;
823 WINHELP_CheckPopup(msg);
825 switch (msg)
827 case WM_NCCREATE:
828 win = (WINHELP_WINDOW*) ((LPCREATESTRUCT) lParam)->lpCreateParams;
829 SetWindowLong(hWnd, 0, (LONG) win);
830 win->hButtonBoxWnd = hWnd;
831 break;
833 case WM_WINDOWPOSCHANGING:
834 winpos = (WINDOWPOS*) lParam;
835 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
837 /* Update buttons */
838 button_size.cx = 0;
839 button_size.cy = 0;
840 for (button = win->first_button; button; button = button->next)
842 HDC hDc;
843 SIZE textsize;
844 if (!button->hWnd)
846 button->hWnd = CreateWindow(STRING_BUTTON, button->lpszName,
847 WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
848 0, 0, 0, 0,
849 hWnd, (HMENU) button->wParam,
850 Globals.hInstance, 0);
851 if (button->hWnd) {
852 if (Globals.button_proc == NULL)
853 Globals.button_proc = (WNDPROC) GetWindowLongPtr(button->hWnd, GWLP_WNDPROC);
854 SetWindowLongPtr(button->hWnd, GWLP_WNDPROC, (LONG_PTR) WINHELP_ButtonWndProc);
857 hDc = GetDC(button->hWnd);
858 GetTextExtentPoint(hDc, button->lpszName,
859 lstrlen(button->lpszName), &textsize);
860 ReleaseDC(button->hWnd, hDc);
862 button_size.cx = max(button_size.cx, textsize.cx + BUTTON_CX);
863 button_size.cy = max(button_size.cy, textsize.cy + BUTTON_CY);
866 x = 0;
867 y = 0;
868 for (button = win->first_button; button; button = button->next)
870 SetWindowPos(button->hWnd, HWND_TOP, x, y, button_size.cx, button_size.cy, 0);
872 if (x + 2 * button_size.cx <= winpos->cx)
873 x += button_size.cx;
874 else
875 x = 0, y += button_size.cy;
877 winpos->cy = y + (x ? button_size.cy : 0);
878 break;
880 case WM_COMMAND:
881 SendMessage(GetParent(hWnd), msg, wParam, lParam);
882 break;
884 case WM_KEYDOWN:
885 switch (wParam)
887 case VK_UP:
888 case VK_DOWN:
889 case VK_PRIOR:
890 case VK_NEXT:
891 case VK_ESCAPE:
892 return SendMessage(GetParent(hWnd), msg, wParam, lParam);
894 break;
897 return DefWindowProc(hWnd, msg, wParam, lParam);
900 /***********************************************************************
902 * WINHELP_ButtonWndProc
904 static LRESULT CALLBACK WINHELP_ButtonWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
906 if (msg == WM_KEYDOWN)
908 switch (wParam)
910 case VK_UP:
911 case VK_DOWN:
912 case VK_PRIOR:
913 case VK_NEXT:
914 case VK_ESCAPE:
915 return SendMessage(GetParent(hWnd), msg, wParam, lParam);
919 return CallWindowProc(Globals.button_proc, hWnd, msg, wParam, lParam);
922 /***********************************************************************
924 * WINHELP_TextWndProc
926 static LRESULT CALLBACK WINHELP_TextWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
928 WINHELP_WINDOW *win;
929 WINHELP_LINE *line;
930 WINHELP_LINE_PART *part;
931 WINDOWPOS *winpos;
932 PAINTSTRUCT ps;
933 HDC hDc;
934 POINT mouse;
935 INT scroll_pos;
936 HWND hPopupWnd;
937 BOOL bExit;
939 if (msg != WM_LBUTTONDOWN)
940 WINHELP_CheckPopup(msg);
942 switch (msg)
944 case WM_NCCREATE:
945 win = (WINHELP_WINDOW*) ((LPCREATESTRUCT) lParam)->lpCreateParams;
946 SetWindowLong(hWnd, 0, (LONG) win);
947 win->hTextWnd = hWnd;
948 win->hBrush = CreateSolidBrush(win->info->sr_color);
949 if (win->info->win_style & WS_POPUP) Globals.hPopupWnd = win->hMainWnd = hWnd;
950 WINHELP_InitFonts(hWnd);
951 break;
953 case WM_CREATE:
954 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
956 /* Calculate vertical size and position of a popup window */
957 if (win->info->win_style & WS_POPUP)
959 POINT origin;
960 RECT old_window_rect;
961 RECT old_client_rect;
962 SIZE old_window_size;
963 SIZE old_client_size;
964 SIZE new_client_size;
965 SIZE new_window_size;
967 GetWindowRect(hWnd, &old_window_rect);
968 origin.x = old_window_rect.left;
969 origin.y = old_window_rect.top;
970 old_window_size.cx = old_window_rect.right - old_window_rect.left;
971 old_window_size.cy = old_window_rect.bottom - old_window_rect.top;
973 GetClientRect(hWnd, &old_client_rect);
974 old_client_size.cx = old_client_rect.right - old_client_rect.left;
975 old_client_size.cy = old_client_rect.bottom - old_client_rect.top;
977 new_client_size = old_client_size;
978 WINHELP_SplitLines(hWnd, &new_client_size);
980 if (origin.y + POPUP_YDISTANCE + new_client_size.cy <= GetSystemMetrics(SM_CYSCREEN))
981 origin.y += POPUP_YDISTANCE;
982 else
983 origin.y -= POPUP_YDISTANCE + new_client_size.cy;
985 new_window_size.cx = old_window_size.cx - old_client_size.cx + new_client_size.cx;
986 new_window_size.cy = old_window_size.cy - old_client_size.cy + new_client_size.cy;
988 win->hShadowWnd =
989 CreateWindowEx(WS_EX_TOOLWINDOW, SHADOW_WIN_CLASS_NAME, "", WS_POPUP,
990 origin.x + SHADOW_DX, origin.y + SHADOW_DY,
991 new_window_size.cx, new_window_size.cy,
992 0, 0, Globals.hInstance, 0);
994 SetWindowPos(hWnd, HWND_TOP, origin.x, origin.y,
995 new_window_size.cx, new_window_size.cy,
997 SetWindowPos(win->hShadowWnd, hWnd, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
998 ShowWindow(win->hShadowWnd, SW_NORMAL);
999 SetActiveWindow(hWnd);
1001 break;
1003 case WM_WINDOWPOSCHANGED:
1004 winpos = (WINDOWPOS*) lParam;
1006 if (!(winpos->flags & SWP_NOSIZE)) WINHELP_SetupText(hWnd);
1007 break;
1009 case WM_MOUSEWHEEL:
1011 int wheelDelta = 0;
1012 UINT scrollLines = 3;
1013 int curPos = GetScrollPos(hWnd, SB_VERT);
1014 int min, max;
1016 GetScrollRange(hWnd, SB_VERT, &min, &max);
1018 SystemParametersInfo(SPI_GETWHEELSCROLLLINES,0, &scrollLines, 0);
1019 if (wParam & (MK_SHIFT | MK_CONTROL))
1020 return DefWindowProc(hWnd, msg, wParam, lParam);
1021 wheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
1022 if (abs(wheelDelta) >= WHEEL_DELTA && scrollLines) {
1023 int dy;
1025 curPos += wheelDelta;
1026 if (curPos > max)
1027 curPos = max;
1028 else if (curPos < min)
1029 curPos = min;
1031 dy = GetScrollPos(hWnd, SB_VERT) - curPos;
1032 SetScrollPos(hWnd, SB_VERT, curPos, TRUE);
1033 ScrollWindow(hWnd, 0, dy, NULL, NULL);
1034 UpdateWindow(hWnd);
1037 break;
1039 case WM_VSCROLL:
1041 BOOL update = TRUE;
1042 RECT rect;
1043 INT Min, Max;
1044 INT CurPos = GetScrollPos(hWnd, SB_VERT);
1045 INT dy;
1047 GetScrollRange(hWnd, SB_VERT, &Min, &Max);
1048 GetClientRect(hWnd, &rect);
1050 switch (wParam & 0xffff)
1052 case SB_THUMBTRACK:
1053 case SB_THUMBPOSITION: CurPos = wParam >> 16; break;
1054 case SB_TOP: CurPos = Min; break;
1055 case SB_BOTTOM: CurPos = Max; break;
1056 case SB_PAGEUP: CurPos -= (rect.bottom - rect.top) / 2; break;
1057 case SB_PAGEDOWN: CurPos += (rect.bottom - rect.top) / 2; break;
1058 case SB_LINEUP: CurPos -= GetSystemMetrics(SM_CXVSCROLL); break;
1059 case SB_LINEDOWN: CurPos += GetSystemMetrics(SM_CXVSCROLL); break;
1060 default: update = FALSE;
1062 if (update)
1064 if (CurPos > Max)
1065 CurPos = Max;
1066 else if (CurPos < Min)
1067 CurPos = Min;
1068 dy = GetScrollPos(hWnd, SB_VERT) - CurPos;
1069 SetScrollPos(hWnd, SB_VERT, CurPos, TRUE);
1070 ScrollWindow(hWnd, 0, dy, NULL, NULL);
1071 UpdateWindow(hWnd);
1074 break;
1076 case WM_PAINT:
1077 hDc = BeginPaint(hWnd, &ps);
1078 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1079 scroll_pos = GetScrollPos(hWnd, SB_VERT);
1081 /* No DPtoLP needed - MM_TEXT map mode */
1082 if (ps.fErase) FillRect(hDc, &ps.rcPaint, win->hBrush);
1083 for (line = win->first_line; line; line = line->next)
1085 for (part = &line->first_part; part; part = part->next)
1087 switch (part->cookie)
1089 case hlp_line_part_text:
1090 SelectObject(hDc, part->u.text.hFont);
1091 SetTextColor(hDc, part->u.text.color);
1092 SetBkColor(hDc, win->info->sr_color);
1093 TextOut(hDc, part->rect.left, part->rect.top - scroll_pos,
1094 part->u.text.lpsText, part->u.text.wTextLen);
1095 if (part->u.text.wUnderline)
1097 HPEN hPen;
1099 switch (part->u.text.wUnderline)
1101 case 1: /* simple */
1102 case 2: /* double */
1103 hPen = CreatePen(PS_SOLID, 1, part->u.text.color);
1104 break;
1105 case 3: /* dotted */
1106 hPen = CreatePen(PS_DOT, 1, part->u.text.color);
1107 break;
1108 default:
1109 WINE_FIXME("Unknow underline type\n");
1110 continue;
1113 SelectObject(hDc, hPen);
1114 MoveToEx(hDc, part->rect.left, part->rect.bottom - scroll_pos - 1, NULL);
1115 LineTo(hDc, part->rect.right, part->rect.bottom - scroll_pos - 1);
1116 if (part->u.text.wUnderline == 2)
1118 MoveToEx(hDc, part->rect.left, part->rect.bottom - scroll_pos + 1, NULL);
1119 LineTo(hDc, part->rect.right, part->rect.bottom - scroll_pos + 1);
1121 DeleteObject(hPen);
1123 break;
1124 case hlp_line_part_bitmap:
1126 HDC hMemDC;
1128 hMemDC = CreateCompatibleDC(hDc);
1129 SelectObject(hMemDC, part->u.bitmap.hBitmap);
1130 BitBlt(hDc, part->rect.left, part->rect.top - scroll_pos,
1131 part->rect.right - part->rect.left, part->rect.bottom - part->rect.top,
1132 hMemDC, 0, 0, SRCCOPY);
1133 DeleteDC(hMemDC);
1135 break;
1136 case hlp_line_part_metafile:
1138 HDC hMemDC;
1139 HBITMAP hBitmap;
1140 SIZE sz;
1141 RECT rc;
1143 sz.cx = part->rect.right - part->rect.left;
1144 sz.cy = part->rect.bottom - part->rect.top;
1145 hMemDC = CreateCompatibleDC(hDc);
1146 hBitmap = CreateCompatibleBitmap(hDc, sz.cx, sz.cy);
1147 SelectObject(hMemDC, hBitmap);
1148 SelectObject(hMemDC, win->hBrush);
1149 rc.left = 0;
1150 rc.top = 0;
1151 rc.right = sz.cx;
1152 rc.bottom = sz.cy;
1153 FillRect(hMemDC, &rc, win->hBrush);
1154 SetMapMode(hMemDC, part->u.metafile.mm);
1155 SetWindowExtEx(hMemDC, sz.cx, sz.cy, 0);
1156 SetViewportExtEx(hMemDC, sz.cx, sz.cy, 0);
1157 SetWindowOrgEx(hMemDC, 0, 0, 0);
1158 SetViewportOrgEx(hMemDC, 0, 0, 0);
1159 PlayMetaFile(hMemDC, part->u.metafile.hMetaFile);
1160 SetMapMode(hMemDC, MM_TEXT);
1161 SetWindowOrgEx(hMemDC, 0, 0, 0);
1162 SetViewportOrgEx(hMemDC, 0, 0, 0);
1163 BitBlt(hDc, part->rect.left, part->rect.top - scroll_pos,
1164 sz.cx, sz.cy, hMemDC, 0, 0, SRCCOPY);
1165 DeleteDC(hMemDC);
1166 DeleteObject(hBitmap);
1168 break;
1173 EndPaint(hWnd, &ps);
1174 break;
1176 case WM_MOUSEMOVE:
1177 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1179 if (WINHELP_IsOverLink(win, wParam, lParam))
1180 SetCursor(win->hHandCur); /* set to hand pointer cursor to indicate a link */
1181 else
1182 SetCursor(win->hArrowCur); /* set to hand pointer cursor to indicate a link */
1184 break;
1186 case WM_LBUTTONDOWN:
1187 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1189 hPopupWnd = Globals.hPopupWnd;
1190 Globals.hPopupWnd = 0;
1192 part = WINHELP_IsOverLink(win, wParam, lParam);
1193 if (part)
1195 HLPFILE* hlpfile;
1196 HLPFILE_WINDOWINFO* wi;
1198 mouse.x = (short)LOWORD(lParam);
1199 mouse.y = (short)HIWORD(lParam);
1201 if (part->link) switch (part->link->cookie)
1203 case hlp_link_link:
1204 hlpfile = WINHELP_LookupHelpFile(part->link->lpszString);
1205 if (part->link->window == -1)
1206 wi = win->info;
1207 else if ((part->link->window >= 0) && (part->link->window < hlpfile->numWindows))
1208 wi = &hlpfile->windows[part->link->window];
1209 else
1211 WINE_WARN("link to window %d/%d\n", part->link->window, hlpfile->numWindows);
1212 break;
1214 WINHELP_CreateHelpWindowByHash(hlpfile, part->link->lHash, wi,
1215 SW_NORMAL);
1216 break;
1217 case hlp_link_popup:
1218 hlpfile = WINHELP_LookupHelpFile(part->link->lpszString);
1219 if (hlpfile) WINHELP_CreateHelpWindowByHash(hlpfile, part->link->lHash,
1220 WINHELP_GetPopupWindowInfo(hlpfile, hWnd, &mouse),
1221 SW_NORMAL);
1222 break;
1223 case hlp_link_macro:
1224 MACRO_ExecuteMacro(part->link->lpszString);
1225 break;
1226 default:
1227 WINE_FIXME("Unknown link cookie %d\n", part->link->cookie);
1231 if (hPopupWnd)
1232 DestroyWindow(hPopupWnd);
1233 break;
1235 case WM_NCDESTROY:
1236 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1238 if (hWnd == Globals.hPopupWnd) Globals.hPopupWnd = 0;
1240 bExit = (Globals.wVersion >= 4 && !lstrcmpi(win->lpszName, "main"));
1241 DeleteObject(win->hBrush);
1243 WINHELP_DeleteWindow(win);
1245 if (bExit) MACRO_Exit();
1247 if (!Globals.win_list)
1248 PostQuitMessage(0);
1249 break;
1252 return DefWindowProc(hWnd, msg, wParam, lParam);
1255 /******************************************************************
1256 * WINHELP_HistoryWndProc
1260 static LRESULT CALLBACK WINHELP_HistoryWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1262 WINHELP_WINDOW* win;
1263 PAINTSTRUCT ps;
1264 HDC hDc;
1265 TEXTMETRIC tm;
1266 unsigned int i;
1267 RECT r;
1269 switch (msg)
1271 case WM_NCCREATE:
1272 win = (WINHELP_WINDOW*)((LPCREATESTRUCT)lParam)->lpCreateParams;
1273 SetWindowLong(hWnd, 0, (LONG)win);
1274 win->hHistoryWnd = hWnd;
1275 break;
1276 case WM_CREATE:
1277 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1278 hDc = GetDC(hWnd);
1279 GetTextMetrics(hDc, &tm);
1280 GetWindowRect(hWnd, &r);
1282 r.right = r.left + 30 * tm.tmAveCharWidth;
1283 r.bottom = r.top + (sizeof(win->history) / sizeof(win->history[0])) * tm.tmHeight;
1284 AdjustWindowRect(&r, GetWindowLong(hWnd, GWL_STYLE), FALSE);
1285 if (r.left < 0) {r.right -= r.left; r.left = 0;}
1286 if (r.top < 0) {r.bottom -= r.top; r.top = 0;}
1288 MoveWindow(hWnd, r.left, r.top, r.right, r.bottom, TRUE);
1289 ReleaseDC(hWnd, hDc);
1290 break;
1291 case WM_LBUTTONDOWN:
1292 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1293 hDc = GetDC(hWnd);
1294 GetTextMetrics(hDc, &tm);
1295 i = HIWORD(lParam) / tm.tmHeight;
1296 if (i < win->histIndex)
1297 WINHELP_CreateHelpWindow(win->history[i], win->info, SW_SHOW);
1298 ReleaseDC(hWnd, hDc);
1299 break;
1300 case WM_PAINT:
1301 hDc = BeginPaint(hWnd, &ps);
1302 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1303 GetTextMetrics(hDc, &tm);
1305 for (i = 0; i < win->histIndex; i++)
1307 TextOut(hDc, 0, i * tm.tmHeight, win->history[i]->lpszTitle,
1308 strlen(win->history[i]->lpszTitle));
1310 EndPaint(hWnd, &ps);
1311 break;
1312 case WM_DESTROY:
1313 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1314 if (hWnd == win->hHistoryWnd)
1315 win->hHistoryWnd = 0;
1316 break;
1318 return DefWindowProc(hWnd, msg, wParam, lParam);
1321 /***********************************************************************
1323 * WINHELP_ShadowWndProc
1325 static LRESULT CALLBACK WINHELP_ShadowWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1327 WINHELP_CheckPopup(msg);
1328 return DefWindowProc(hWnd, msg, wParam, lParam);
1331 /***********************************************************************
1333 * SetupText
1335 static void WINHELP_SetupText(HWND hWnd)
1337 HDC hDc = GetDC(hWnd);
1338 RECT rect;
1339 SIZE newsize;
1341 ShowScrollBar(hWnd, SB_VERT, FALSE);
1342 if (!WINHELP_SplitLines(hWnd, NULL))
1344 ShowScrollBar(hWnd, SB_VERT, TRUE);
1345 GetClientRect(hWnd, &rect);
1347 WINHELP_SplitLines(hWnd, &newsize);
1348 SetScrollRange(hWnd, SB_VERT, 0, rect.top + newsize.cy - rect.bottom, TRUE);
1350 else
1352 SetScrollPos(hWnd, SB_VERT, 0, FALSE);
1353 SetScrollRange(hWnd, SB_VERT, 0, 0, FALSE);
1356 ReleaseDC(hWnd, hDc);
1359 /***********************************************************************
1361 * WINHELP_AppendText
1363 static BOOL WINHELP_AppendText(WINHELP_LINE ***linep, WINHELP_LINE_PART ***partp,
1364 LPSIZE space, LPSIZE textsize,
1365 INT *line_ascent, INT ascent,
1366 LPCSTR text, UINT textlen,
1367 HFONT font, COLORREF color, HLPFILE_LINK *link,
1368 unsigned underline)
1370 WINHELP_LINE *line;
1371 WINHELP_LINE_PART *part;
1372 LPSTR ptr;
1374 if (!*partp) /* New line */
1376 *line_ascent = ascent;
1378 line = HeapAlloc(GetProcessHeap(), 0,
1379 sizeof(WINHELP_LINE) + textlen);
1380 if (!line) return FALSE;
1382 line->next = 0;
1383 part = &line->first_part;
1384 ptr = (char*)line + sizeof(WINHELP_LINE);
1386 line->rect.top = (**linep ? (**linep)->rect.bottom : 0) + space->cy;
1387 line->rect.bottom = line->rect.top;
1388 line->rect.left = space->cx;
1389 line->rect.right = space->cx;
1391 if (**linep) *linep = &(**linep)->next;
1392 **linep = line;
1393 space->cy = 0;
1395 else /* Same line */
1397 line = **linep;
1399 if (*line_ascent < ascent)
1401 WINHELP_LINE_PART *p;
1402 for (p = &line->first_part; p; p = p->next)
1404 p->rect.top += ascent - *line_ascent;
1405 p->rect.bottom += ascent - *line_ascent;
1407 line->rect.bottom += ascent - *line_ascent;
1408 *line_ascent = ascent;
1411 part = HeapAlloc(GetProcessHeap(), 0,
1412 sizeof(WINHELP_LINE_PART) + textlen);
1413 if (!part) return FALSE;
1414 **partp = part;
1415 ptr = (char*)part + sizeof(WINHELP_LINE_PART);
1418 memcpy(ptr, text, textlen);
1419 part->cookie = hlp_line_part_text;
1420 part->rect.left = line->rect.right + (*partp ? space->cx : 0);
1421 part->rect.right = part->rect.left + textsize->cx;
1422 line->rect.right = part->rect.right;
1423 part->rect.top =
1424 ((*partp) ? line->rect.top : line->rect.bottom) + *line_ascent - ascent;
1425 part->rect.bottom = part->rect.top + textsize->cy;
1426 line->rect.bottom = max(line->rect.bottom, part->rect.bottom);
1427 part->u.text.lpsText = ptr;
1428 part->u.text.wTextLen = textlen;
1429 part->u.text.hFont = font;
1430 part->u.text.color = color;
1431 part->u.text.wUnderline = underline;
1433 WINE_TRACE("Appended text '%*.*s'[%d] @ (%d,%d-%d,%d)\n",
1434 part->u.text.wTextLen,
1435 part->u.text.wTextLen,
1436 part->u.text.lpsText,
1437 part->u.text.wTextLen,
1438 part->rect.left, part->rect.top, part->rect.right, part->rect.bottom);
1440 part->link = link;
1441 if (link) link->wRefCount++;
1443 part->next = 0;
1444 *partp = &part->next;
1446 space->cx = 0;
1448 return TRUE;
1451 /***********************************************************************
1453 * WINHELP_AppendGfxObject
1455 static WINHELP_LINE_PART* WINHELP_AppendGfxObject(WINHELP_LINE ***linep, WINHELP_LINE_PART ***partp,
1456 LPSIZE space, LPSIZE gfxSize,
1457 HLPFILE_LINK *link, unsigned pos)
1459 WINHELP_LINE *line;
1460 WINHELP_LINE_PART *part;
1461 LPSTR ptr;
1463 if (!*partp || pos == 1) /* New line */
1465 line = HeapAlloc(GetProcessHeap(), 0, sizeof(WINHELP_LINE));
1466 if (!line) return NULL;
1468 line->next = NULL;
1469 part = &line->first_part;
1471 line->rect.top = (**linep ? (**linep)->rect.bottom : 0) + space->cy;
1472 line->rect.bottom = line->rect.top;
1473 line->rect.left = space->cx;
1474 line->rect.right = space->cx;
1476 if (**linep) *linep = &(**linep)->next;
1477 **linep = line;
1478 space->cy = 0;
1479 ptr = (char*)line + sizeof(WINHELP_LINE);
1481 else /* Same line */
1483 if (pos == 2) WINE_FIXME("Left alignment not handled\n");
1484 line = **linep;
1486 part = HeapAlloc(GetProcessHeap(), 0, sizeof(WINHELP_LINE_PART));
1487 if (!part) return NULL;
1488 **partp = part;
1489 ptr = (char*)part + sizeof(WINHELP_LINE_PART);
1492 /* part->cookie should be set by caller (image or metafile) */
1493 part->rect.left = line->rect.right + (*partp ? space->cx : 0);
1494 part->rect.right = part->rect.left + gfxSize->cx;
1495 line->rect.right = part->rect.right;
1496 part->rect.top = (*partp) ? line->rect.top : line->rect.bottom;
1497 part->rect.bottom = part->rect.top + gfxSize->cy;
1498 line->rect.bottom = max(line->rect.bottom, part->rect.bottom);
1500 WINE_TRACE("Appended gfx @ (%d,%d-%d,%d)\n",
1501 part->rect.left, part->rect.top, part->rect.right, part->rect.bottom);
1503 part->link = link;
1504 if (link) link->wRefCount++;
1506 part->next = NULL;
1507 *partp = &part->next;
1509 space->cx = 0;
1511 return part;
1515 /***********************************************************************
1517 * WINHELP_SplitLines
1519 static BOOL WINHELP_SplitLines(HWND hWnd, LPSIZE newsize)
1521 WINHELP_WINDOW *win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1522 HLPFILE_PARAGRAPH *p;
1523 WINHELP_LINE **line = &win->first_line;
1524 WINHELP_LINE_PART **part = 0;
1525 INT line_ascent = 0;
1526 SIZE space;
1527 RECT rect;
1528 HDC hDc;
1530 if (newsize) newsize->cx = newsize->cy = 0;
1532 if (!win->page) return TRUE;
1534 WINHELP_DeleteLines(win);
1536 GetClientRect(hWnd, &rect);
1538 rect.top += INTERNAL_BORDER_WIDTH;
1539 rect.left += INTERNAL_BORDER_WIDTH;
1540 rect.right -= INTERNAL_BORDER_WIDTH;
1541 rect.bottom -= INTERNAL_BORDER_WIDTH;
1543 space.cy = rect.top;
1544 space.cx = rect.left;
1546 hDc = GetDC(hWnd);
1548 for (p = win->page->first_paragraph; p; p = p->next)
1550 switch (p->cookie)
1552 case para_normal_text:
1553 case para_debug_text:
1555 TEXTMETRIC tm;
1556 SIZE textsize = {0, 0};
1557 LPCSTR text = p->u.text.lpszText;
1558 UINT indent = 0;
1559 UINT len = strlen(text);
1560 unsigned underline = 0;
1562 HFONT hFont = 0;
1563 COLORREF color = RGB(0, 0, 0);
1565 if (p->u.text.wFont < win->page->file->numFonts)
1567 HLPFILE* hlpfile = win->page->file;
1569 if (!hlpfile->fonts[p->u.text.wFont].hFont)
1570 hlpfile->fonts[p->u.text.wFont].hFont = CreateFontIndirect(&hlpfile->fonts[p->u.text.wFont].LogFont);
1571 hFont = hlpfile->fonts[p->u.text.wFont].hFont;
1572 color = hlpfile->fonts[p->u.text.wFont].color;
1574 else
1576 UINT wFont = (p->u.text.wFont < win->fonts_len) ? p->u.text.wFont : 0;
1578 hFont = win->fonts[wFont];
1581 if (p->link && p->link->bClrChange)
1583 underline = (p->link->cookie == hlp_link_popup) ? 3 : 1;
1584 color = RGB(0, 0x80, 0);
1586 if (p->cookie == para_debug_text) color = RGB(0xff, 0, 0);
1588 SelectObject(hDc, hFont);
1590 GetTextMetrics(hDc, &tm);
1592 if (p->u.text.wIndent)
1594 indent = p->u.text.wIndent * 5 * tm.tmAveCharWidth;
1595 if (!part)
1596 space.cx = rect.left + indent - 2 * tm.tmAveCharWidth;
1599 if (p->u.text.wVSpace)
1601 part = 0;
1602 space.cx = rect.left + indent;
1603 space.cy += (p->u.text.wVSpace - 1) * tm.tmHeight;
1606 if (p->u.text.wHSpace)
1608 space.cx += p->u.text.wHSpace * 2 * tm.tmAveCharWidth;
1611 WINE_TRACE("splitting text %s\n", text);
1613 while (len)
1615 INT free_width = rect.right - (part ? (*line)->rect.right : rect.left) - space.cx;
1616 UINT low = 0, curr = len, high = len, textlen = 0;
1618 if (free_width > 0)
1620 while (1)
1622 GetTextExtentPoint(hDc, text, curr, &textsize);
1624 if (textsize.cx <= free_width) low = curr;
1625 else high = curr;
1627 if (high <= low + 1) break;
1629 if (textsize.cx) curr = (curr * free_width) / textsize.cx;
1630 if (curr <= low) curr = low + 1;
1631 else if (curr >= high) curr = high - 1;
1633 textlen = low;
1634 while (textlen && text[textlen] && text[textlen] != ' ') textlen--;
1636 if (!part && !textlen) textlen = max(low, 1);
1638 if (free_width <= 0 || !textlen)
1640 part = 0;
1641 space.cx = rect.left + indent;
1642 space.cx = min(space.cx, rect.right - rect.left - 1);
1643 continue;
1646 WINE_TRACE("\t => %d %*s\n", textlen, textlen, text);
1648 if (!WINHELP_AppendText(&line, &part, &space, &textsize,
1649 &line_ascent, tm.tmAscent,
1650 text, textlen, hFont, color, p->link, underline) ||
1651 (!newsize && (*line)->rect.bottom > rect.bottom))
1653 ReleaseDC(hWnd, hDc);
1654 return FALSE;
1657 if (newsize)
1658 newsize->cx = max(newsize->cx, (*line)->rect.right + INTERNAL_BORDER_WIDTH);
1660 len -= textlen;
1661 text += textlen;
1662 if (text[0] == ' ') text++, len--;
1665 break;
1666 case para_bitmap:
1667 case para_metafile:
1669 SIZE gfxSize;
1670 INT free_width;
1671 WINHELP_LINE_PART* ref_part;
1673 if (p->u.gfx.pos & 0x8000)
1675 space.cx = rect.left;
1676 if (*line)
1677 space.cy += (*line)->rect.bottom - (*line)->rect.top;
1678 part = 0;
1681 if (p->cookie == para_bitmap)
1683 DIBSECTION dibs;
1685 GetObject(p->u.gfx.u.bmp.hBitmap, sizeof(dibs), &dibs);
1686 gfxSize.cx = dibs.dsBm.bmWidth;
1687 gfxSize.cy = dibs.dsBm.bmHeight;
1689 else
1691 LPMETAFILEPICT lpmfp = &p->u.gfx.u.mfp;
1692 if (lpmfp->mm == MM_ANISOTROPIC || lpmfp->mm == MM_ISOTROPIC)
1694 gfxSize.cx = MulDiv(lpmfp->xExt, GetDeviceCaps(hDc, HORZRES),
1695 100*GetDeviceCaps(hDc, HORZSIZE));
1696 gfxSize.cy = MulDiv(lpmfp->yExt, GetDeviceCaps(hDc, VERTRES),
1697 100*GetDeviceCaps(hDc, VERTSIZE));
1699 else
1701 gfxSize.cx = lpmfp->xExt;
1702 gfxSize.cy = lpmfp->yExt;
1706 free_width = rect.right - ((part && *line) ? (*line)->rect.right : rect.left) - space.cx;
1707 if (free_width <= 0)
1709 part = NULL;
1710 space.cx = rect.left;
1711 space.cx = min(space.cx, rect.right - rect.left - 1);
1713 ref_part = WINHELP_AppendGfxObject(&line, &part, &space, &gfxSize,
1714 p->link, p->u.gfx.pos);
1715 if (!ref_part || (!newsize && (*line)->rect.bottom > rect.bottom))
1717 return FALSE;
1719 if (p->cookie == para_bitmap)
1721 ref_part->cookie = hlp_line_part_bitmap;
1722 ref_part->u.bitmap.hBitmap = p->u.gfx.u.bmp.hBitmap;
1724 else
1726 ref_part->cookie = hlp_line_part_metafile;
1727 ref_part->u.metafile.hMetaFile = p->u.gfx.u.mfp.hMF;
1728 ref_part->u.metafile.mm = p->u.gfx.u.mfp.mm;
1731 break;
1735 if (newsize)
1736 newsize->cy = (*line)->rect.bottom + INTERNAL_BORDER_WIDTH;
1738 ReleaseDC(hWnd, hDc);
1739 return TRUE;
1742 /***********************************************************************
1744 * WINHELP_CheckPopup
1746 static void WINHELP_CheckPopup(UINT msg)
1748 if (!Globals.hPopupWnd) return;
1750 switch (msg)
1752 case WM_COMMAND:
1753 case WM_LBUTTONDOWN:
1754 case WM_MBUTTONDOWN:
1755 case WM_RBUTTONDOWN:
1756 case WM_NCLBUTTONDOWN:
1757 case WM_NCMBUTTONDOWN:
1758 case WM_NCRBUTTONDOWN:
1759 DestroyWindow(Globals.hPopupWnd);
1760 Globals.hPopupWnd = 0;
1764 /***********************************************************************
1766 * WINHELP_DeleteLines
1768 static void WINHELP_DeleteLines(WINHELP_WINDOW *win)
1770 WINHELP_LINE *line, *next_line;
1771 WINHELP_LINE_PART *part, *next_part;
1772 for (line = win->first_line; line; line = next_line)
1774 next_line = line->next;
1775 for (part = &line->first_part; part; part = next_part)
1777 next_part = part->next;
1778 HLPFILE_FreeLink(part->link);
1779 HeapFree(GetProcessHeap(), 0, part);
1782 win->first_line = 0;
1785 /***********************************************************************
1787 * WINHELP_DeleteWindow
1789 static void WINHELP_DeleteWindow(WINHELP_WINDOW* win)
1791 WINHELP_WINDOW** w;
1792 unsigned int i;
1793 WINHELP_BUTTON* b;
1794 WINHELP_BUTTON* bp;
1796 for (w = &Globals.win_list; *w; w = &(*w)->next)
1798 if (*w == win)
1800 *w = win->next;
1801 break;
1805 if (Globals.active_win == win)
1807 Globals.active_win = Globals.win_list;
1808 if (Globals.win_list)
1809 SetActiveWindow(Globals.win_list->hMainWnd);
1812 for (b = win->first_button; b; b = bp)
1814 DestroyWindow(b->hWnd);
1815 bp = b->next;
1816 HeapFree(GetProcessHeap(), 0, b);
1819 if (win->hShadowWnd) DestroyWindow(win->hShadowWnd);
1820 if (win->hHistoryWnd) DestroyWindow(win->hHistoryWnd);
1822 for (i = 0; i < win->histIndex; i++)
1824 HLPFILE_FreeHlpFile(win->history[i]->file);
1827 for (i = 0; i < win->backIndex; i++)
1828 HLPFILE_FreeHlpFile(win->back[i]->file);
1830 if (win->page) HLPFILE_FreeHlpFile(win->page->file);
1831 WINHELP_DeleteLines(win);
1832 HeapFree(GetProcessHeap(), 0, win);
1835 /***********************************************************************
1837 * WINHELP_InitFonts
1839 static void WINHELP_InitFonts(HWND hWnd)
1841 WINHELP_WINDOW *win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1842 LOGFONT logfontlist[] = {
1843 {-10, 0, 0, 0, 400, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1844 {-12, 0, 0, 0, 700, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1845 {-12, 0, 0, 0, 700, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1846 {-12, 0, 0, 0, 400, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1847 {-12, 0, 0, 0, 700, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1848 {-10, 0, 0, 0, 700, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1849 { -8, 0, 0, 0, 400, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"}};
1850 #define FONTS_LEN (sizeof(logfontlist)/sizeof(*logfontlist))
1852 static HFONT fonts[FONTS_LEN];
1853 static BOOL init = 0;
1855 win->fonts_len = FONTS_LEN;
1856 win->fonts = fonts;
1858 if (!init)
1860 UINT i;
1862 for (i = 0; i < FONTS_LEN; i++)
1864 fonts[i] = CreateFontIndirect(&logfontlist[i]);
1867 init = 1;
1871 /***********************************************************************
1873 * WINHELP_MessageBoxIDS
1875 INT WINHELP_MessageBoxIDS(UINT ids_text, UINT ids_title, WORD type)
1877 CHAR text[MAX_STRING_LEN];
1878 CHAR title[MAX_STRING_LEN];
1880 LoadString(Globals.hInstance, ids_text, text, sizeof(text));
1881 LoadString(Globals.hInstance, ids_title, title, sizeof(title));
1883 return MessageBox(0, text, title, type);
1886 /***********************************************************************
1888 * MAIN_MessageBoxIDS_s
1890 INT WINHELP_MessageBoxIDS_s(UINT ids_text, LPCSTR str, UINT ids_title, WORD type)
1892 CHAR text[MAX_STRING_LEN];
1893 CHAR title[MAX_STRING_LEN];
1894 CHAR newtext[MAX_STRING_LEN + MAX_PATH];
1896 LoadString(Globals.hInstance, ids_text, text, sizeof(text));
1897 LoadString(Globals.hInstance, ids_title, title, sizeof(title));
1898 wsprintf(newtext, text, str);
1900 return MessageBox(0, newtext, title, type);
1903 /******************************************************************
1904 * WINHELP_IsOverLink
1908 WINHELP_LINE_PART* WINHELP_IsOverLink(WINHELP_WINDOW* win, WPARAM wParam, LPARAM lParam)
1910 POINT mouse;
1911 WINHELP_LINE *line;
1912 WINHELP_LINE_PART *part;
1913 int scroll_pos = GetScrollPos(win->hTextWnd, SB_VERT);
1915 mouse.x = LOWORD(lParam);
1916 mouse.y = HIWORD(lParam);
1917 for (line = win->first_line; line; line = line->next)
1919 for (part = &line->first_part; part; part = part->next)
1921 if (part->link &&
1922 part->link->lpszString &&
1923 part->rect.left <= mouse.x &&
1924 part->rect.right >= mouse.x &&
1925 part->rect.top <= mouse.y + scroll_pos &&
1926 part->rect.bottom >= mouse.y + scroll_pos)
1928 return part;
1933 return NULL;