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>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "winhelp_res.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(winhelp);
37 static BOOL WINHELP_RegisterWinClasses(void);
38 static LRESULT CALLBACK WINHELP_MainWndProc(HWND, UINT, WPARAM, LPARAM);
39 static LRESULT CALLBACK WINHELP_TextWndProc(HWND, UINT, WPARAM, LPARAM);
40 static LRESULT CALLBACK WINHELP_ButtonBoxWndProc(HWND, UINT, WPARAM, LPARAM);
41 static LRESULT CALLBACK WINHELP_HistoryWndProc(HWND, UINT, WPARAM, LPARAM);
42 static void WINHELP_CheckPopup(UINT);
43 static BOOL WINHELP_SplitLines(HWND hWnd, LPSIZE);
44 static void WINHELP_InitFonts(HWND hWnd);
45 static void WINHELP_DeleteLines(WINHELP_WINDOW*);
46 static void WINHELP_DeleteWindow(WINHELP_WINDOW*);
47 static void WINHELP_SetupText(HWND hWnd);
48 static WINHELP_LINE_PART* WINHELP_IsOverLink(WINHELP_WINDOW*, WPARAM, LPARAM);
50 WINHELP_GLOBALS Globals = {3, 0, 0, 0, 1, 0, 0};
52 /***********************************************************************
54 * WINHELP_LookupHelpFile
56 HLPFILE* WINHELP_LookupHelpFile(LPCSTR lpszFile)
60 hlpfile = HLPFILE_ReadHlpFile(lpszFile);
62 /* Add Suffix `.hlp' */
63 if (!hlpfile && lstrcmpi(lpszFile + strlen(lpszFile) - 4, ".hlp") != 0)
65 char szFile_hlp[MAX_PATHNAME_LEN];
67 lstrcpyn(szFile_hlp, lpszFile, sizeof(szFile_hlp) - 4);
68 szFile_hlp[sizeof(szFile_hlp) - 5] = '\0';
69 lstrcat(szFile_hlp, ".hlp");
71 hlpfile = HLPFILE_ReadHlpFile(szFile_hlp);
75 WINHELP_MessageBoxIDS_s(STID_HLPFILE_ERROR_s, lpszFile, STID_WHERROR, MB_OK);
76 if (Globals.win_list) return NULL;
81 /******************************************************************
82 * WINHELP_GetWindowInfo
86 HLPFILE_WINDOWINFO* WINHELP_GetWindowInfo(HLPFILE* hlpfile, LPCSTR name)
88 static HLPFILE_WINDOWINFO mwi;
91 if (!name || !name[0])
92 name = Globals.active_win->lpszName;
94 for (i = 0; i < hlpfile->numWindows; i++)
95 if (!strcmp(hlpfile->windows[i].name, name))
96 return &hlpfile->windows[i];
98 if (strcmp(name, "main") != 0)
100 WINE_FIXME("Couldn't find window info for %s\n", name);
106 strcpy(mwi.type, "primary");
107 strcpy(mwi.name, "main");
108 if (!LoadString(Globals.hInstance, STID_WINE_HELP,
109 mwi.caption, sizeof(mwi.caption)))
110 strcpy(mwi.caption, hlpfile->lpszTitle);
111 mwi.origin.x = mwi.origin.y = mwi.size.cx = mwi.size.cy = CW_USEDEFAULT;
113 mwi.sr_color = mwi.sr_color = 0xFFFFFF;
118 /******************************************************************
119 * HLPFILE_GetPopupWindowInfo
123 HLPFILE_WINDOWINFO* WINHELP_GetPopupWindowInfo(HLPFILE* hlpfile, HWND hParentWnd, POINT* mouse)
125 static HLPFILE_WINDOWINFO wi;
129 wi.type[0] = wi.name[0] = wi.caption[0] = '\0';
131 /* Calculate horizontal size and position of a popup window */
132 GetWindowRect(hParentWnd, &parent_rect);
133 wi.size.cx = (parent_rect.right - parent_rect.left) / 2;
134 wi.size.cy = 10; /* need a non null value, so that border are taken into account while computing */
137 ClientToScreen(hParentWnd, &wi.origin);
138 wi.origin.x -= wi.size.cx / 2;
139 wi.origin.x = min(wi.origin.x, GetSystemMetrics(SM_CXSCREEN) - wi.size.cx);
140 wi.origin.x = max(wi.origin.x, 0);
143 wi.win_style = WS_POPUPWINDOW;
144 wi.sr_color = wi.sr_color = 0xFFFFFF;
149 /***********************************************************************
153 int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
159 Globals.hInstance = hInstance;
162 while (*cmdline && (*cmdline == ' ' || *cmdline == '-'))
166 if (*cmdline++ == ' ') continue;
169 if (option) cmdline++;
170 while (*cmdline && *cmdline == ' ') cmdline++;
176 while (*cmdline && *cmdline != ' ') cmdline++;
177 if (*cmdline) *cmdline++ = '\0';
178 lHash = HLPFILE_Hash(topic_id);
183 Globals.wVersion = option - '0';
188 Globals.isBook = FALSE;
192 WINE_FIXME("Unsupported cmd line: %s\n", cmdline);
197 /* Create primary window */
198 WINHELP_RegisterWinClasses();
199 hlpfile = WINHELP_LookupHelpFile(cmdline);
202 WINHELP_CreateHelpWindowByHash(hlpfile, lHash,
203 WINHELP_GetWindowInfo(hlpfile, "main"), show);
206 while (GetMessage(&msg, 0, 0, 0))
208 TranslateMessage(&msg);
209 DispatchMessage(&msg);
214 /***********************************************************************
218 static BOOL WINHELP_RegisterWinClasses(void)
220 WNDCLASS class_main, class_button_box, class_text, class_shadow, class_history;
222 class_main.style = CS_HREDRAW | CS_VREDRAW;
223 class_main.lpfnWndProc = WINHELP_MainWndProc;
224 class_main.cbClsExtra = 0;
225 class_main.cbWndExtra = sizeof(LONG);
226 class_main.hInstance = Globals.hInstance;
227 class_main.hIcon = LoadIcon(0, IDI_APPLICATION);
228 class_main.hCursor = LoadCursor(0, IDC_ARROW);
229 class_main.hbrBackground = GetStockObject(WHITE_BRUSH);
230 class_main.lpszMenuName = 0;
231 class_main.lpszClassName = MAIN_WIN_CLASS_NAME;
233 class_button_box = class_main;
234 class_button_box.lpfnWndProc = WINHELP_ButtonBoxWndProc;
235 class_button_box.hbrBackground = GetStockObject(GRAY_BRUSH);
236 class_button_box.lpszClassName = BUTTON_BOX_WIN_CLASS_NAME;
238 class_text = class_main;
239 class_text.lpfnWndProc = WINHELP_TextWndProc;
240 class_text.lpszClassName = TEXT_WIN_CLASS_NAME;
242 class_shadow = class_main;
243 class_shadow.lpfnWndProc = DefWindowProc;
244 class_shadow.hbrBackground = GetStockObject(GRAY_BRUSH);
245 class_shadow.lpszClassName = SHADOW_WIN_CLASS_NAME;
247 class_history = class_main;
248 class_history.lpfnWndProc = WINHELP_HistoryWndProc;
249 class_history.lpszClassName = HISTORY_WIN_CLASS_NAME;
251 return (RegisterClass(&class_main) &&
252 RegisterClass(&class_button_box) &&
253 RegisterClass(&class_text) &&
254 RegisterClass(&class_shadow) &&
255 RegisterClass(&class_history));
266 } WINHELP,*LPWINHELP;
268 /******************************************************************
269 * WINHELP_HandleCommand
273 static LRESULT WINHELP_HandleCommand(HWND hSrcWnd, LPARAM lParam)
275 COPYDATASTRUCT* cds = (COPYDATASTRUCT*)lParam;
278 if (cds->dwData != 0xA1DE505)
280 WINE_FIXME("Wrong magic number (%08lx)\n", cds->dwData);
284 wh = (WINHELP*)cds->lpData;
288 WINE_TRACE("Got[%u]: cmd=%u data=%08lx fn=%s\n",
289 wh->size, wh->command, wh->data,
290 wh->ofsFilename ? (LPSTR)wh + wh->ofsFilename : "");
297 /* in fact, should be the topic dialog box */
300 MACRO_JumpHash((LPSTR)wh + wh->ofsFilename, "main", 0);
304 case HELP_SETCONTENTS:
306 case HELP_CONTEXTPOPUP:
308 case HELP_HELPONHELP:
310 case HELP_PARTIALKEY:
321 /******************************************************************
322 * WINHELP_ReuseWindow
326 static BOOL WINHELP_ReuseWindow(WINHELP_WINDOW* win, WINHELP_WINDOW* oldwin,
327 HLPFILE_PAGE* page, int nCmdShow)
331 win->hMainWnd = oldwin->hMainWnd;
332 win->hButtonBoxWnd = oldwin->hButtonBoxWnd;
333 win->hTextWnd = oldwin->hTextWnd;
334 win->hHistoryWnd = oldwin->hHistoryWnd;
335 oldwin->hMainWnd = oldwin->hButtonBoxWnd = oldwin->hTextWnd = oldwin->hHistoryWnd = 0;
337 SetWindowLong(win->hMainWnd, 0, (LONG)win);
338 SetWindowLong(win->hButtonBoxWnd, 0, (LONG)win);
339 SetWindowLong(win->hTextWnd, 0, (LONG)win);
340 SetWindowLong(win->hHistoryWnd, 0, (LONG)win);
342 WINHELP_InitFonts(win->hMainWnd);
345 SetWindowText(win->hMainWnd, page->file->lpszTitle);
347 WINHELP_SetupText(win->hTextWnd);
348 InvalidateRect(win->hTextWnd, NULL, TRUE);
349 SendMessage(win->hMainWnd, WM_USER, 0, 0);
350 ShowWindow(win->hMainWnd, nCmdShow);
351 UpdateWindow(win->hTextWnd);
353 if (!(win->info->win_style & WS_POPUP))
357 memcpy(win->history, oldwin->history, sizeof(win->history));
358 win->histIndex = oldwin->histIndex;
360 /* FIXME: when using back, we shouldn't update the history... */
364 for (i = 0; i < win->histIndex; i++)
365 if (win->history[i] == page) break;
367 /* if the new page is already in the history, do nothing */
368 if (i == win->histIndex)
370 num = sizeof(win->history) / sizeof(win->history[0]);
371 if (win->histIndex == num)
373 /* we're full, remove latest entry */
374 HLPFILE_FreeHlpFile(win->history[0]->file);
375 memmove(&win->history[0], &win->history[1],
376 (num - 1) * sizeof(win->history[0]));
379 win->history[win->histIndex++] = page;
380 page->file->wRefCount++;
381 if (win->hHistoryWnd) InvalidateRect(win->hHistoryWnd, NULL, TRUE);
385 memcpy(win->back, oldwin->back, sizeof(win->back));
386 win->backIndex = oldwin->backIndex;
390 num = sizeof(win->back) / sizeof(win->back[0]);
391 if (win->backIndex == num)
393 /* we're full, remove latest entry */
394 HLPFILE_FreeHlpFile(win->back[0]->file);
395 memmove(&win->back[0], &win->back[1],
396 (num - 1) * sizeof(win->back[0]));
399 win->back[win->backIndex++] = page;
400 page->file->wRefCount++;
404 win->backIndex = win->histIndex = 0;
406 oldwin->histIndex = oldwin->backIndex = 0;
407 WINHELP_DeleteWindow(oldwin);
411 /***********************************************************************
413 * WINHELP_CreateHelpWindow
415 BOOL WINHELP_CreateHelpWindow(HLPFILE_PAGE* page, HLPFILE_WINDOWINFO* wi,
418 WINHELP_WINDOW *win, *oldwin;
423 bPrimary = !lstrcmpi(wi->name, "main");
424 bPopup = wi->win_style & WS_POPUP;
426 /* Initialize WINHELP_WINDOW struct */
427 win = HeapAlloc(GetProcessHeap(), 0,
428 sizeof(WINHELP_WINDOW) + strlen(wi->name) + 1);
429 if (!win) return FALSE;
431 win->next = Globals.win_list;
432 Globals.win_list = win;
434 win->lpszName = (char*)win + sizeof(WINHELP_WINDOW);
435 lstrcpy((char*)win->lpszName, wi->name);
438 win->first_button = 0;
441 win->hButtonBoxWnd = 0;
444 win->hHistoryWnd = 0;
446 win->hArrowCur = LoadCursorA(0, IDC_ARROWA);
447 win->hHandCur = LoadCursorA(0, IDC_HANDA);
451 Globals.active_win = win;
453 /* Initialize default pushbuttons */
454 if (bPrimary && page)
456 CHAR buffer[MAX_STRING_LEN];
458 LoadString(Globals.hInstance, STID_CONTENTS, buffer, sizeof(buffer));
459 MACRO_CreateButton("BTN_CONTENTS", buffer, "Contents()");
460 LoadString(Globals.hInstance, STID_SEARCH,buffer, sizeof(buffer));
461 MACRO_CreateButton("BTN_SEARCH", buffer, "Search()");
462 LoadString(Globals.hInstance, STID_BACK, buffer, sizeof(buffer));
463 MACRO_CreateButton("BTN_BACK", buffer, "Back()");
464 LoadString(Globals.hInstance, STID_HISTORY, buffer, sizeof(buffer));
465 MACRO_CreateButton("BTN_HISTORY", buffer, "History()");
466 LoadString(Globals.hInstance, STID_TOPICS, buffer, sizeof(buffer));
467 MACRO_CreateButton("BTN_TOPICS", buffer, "Finder()");
470 /* Initialize file specific pushbuttons */
471 if (!(wi->win_style & WS_POPUP) && page)
473 HLPFILE_MACRO *macro;
474 for (macro = page->file->first_macro; macro; macro = macro->next)
475 MACRO_ExecuteMacro(macro->lpszMacro);
477 for (macro = page->first_macro; macro; macro = macro->next)
478 MACRO_ExecuteMacro(macro->lpszMacro);
481 /* Reuse existing window */
484 for (oldwin = win->next; oldwin; oldwin = oldwin->next)
486 if (!lstrcmpi(oldwin->lpszName, wi->name))
488 return WINHELP_ReuseWindow(win, oldwin, page, nCmdShow);
492 win->histIndex = win->backIndex = 1;
493 win->history[0] = win->back[0] = page;
494 page->file->wRefCount += 2;
497 win->histIndex = win->backIndex = 0;
499 hWnd = CreateWindow(bPopup ? TEXT_WIN_CLASS_NAME : MAIN_WIN_CLASS_NAME,
500 wi->caption, wi->win_style,
501 wi->origin.x, wi->origin.y, wi->size.cx, wi->size.cy,
502 0, bPrimary ? LoadMenu(Globals.hInstance, MAKEINTRESOURCE(MAIN_MENU)) : 0,
503 Globals.hInstance, win);
505 ShowWindow(hWnd, nCmdShow);
511 /***********************************************************************
513 * WINHELP_CreateHelpWindowByHash
515 BOOL WINHELP_CreateHelpWindowByHash(HLPFILE* hlpfile, LONG lHash,
516 HLPFILE_WINDOWINFO* wi, int nCmdShow)
518 HLPFILE_PAGE* page = NULL;
521 page = lHash ? HLPFILE_PageByHash(hlpfile, lHash) :
522 HLPFILE_Contents(hlpfile);
523 if (page) page->file->wRefCount++;
524 return WINHELP_CreateHelpWindow(page, wi, nCmdShow);
527 /***********************************************************************
529 * WINHELP_MainWndProc
531 static LRESULT CALLBACK WINHELP_MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
534 WINHELP_BUTTON *button;
535 RECT rect, button_box_rect;
538 WINHELP_CheckPopup(msg);
543 win = (WINHELP_WINDOW*) ((LPCREATESTRUCT) lParam)->lpCreateParams;
544 SetWindowLong(hWnd, 0, (LONG) win);
545 win->hMainWnd = hWnd;
549 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
551 /* Create button box and text Window */
552 CreateWindow(BUTTON_BOX_WIN_CLASS_NAME, "", WS_CHILD | WS_VISIBLE,
553 0, 0, 0, 0, hWnd, 0, Globals.hInstance, win);
555 CreateWindow(TEXT_WIN_CLASS_NAME, "", WS_CHILD | WS_VISIBLE,
556 0, 0, 0, 0, hWnd, 0, Globals.hInstance, win);
560 case WM_WINDOWPOSCHANGED:
561 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
562 GetClientRect(hWnd, &rect);
564 /* Update button box and text Window */
565 SetWindowPos(win->hButtonBoxWnd, HWND_TOP,
567 rect.right - rect.left,
568 rect.bottom - rect.top, 0);
570 GetWindowRect(win->hButtonBoxWnd, &button_box_rect);
571 text_top = rect.top + button_box_rect.bottom - button_box_rect.top;
573 SetWindowPos(win->hTextWnd, HWND_TOP,
575 rect.right - rect.left,
576 rect.bottom - text_top, 0);
581 Globals.active_win = win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
585 case MNID_FILE_OPEN: MACRO_FileOpen(); break;
586 case MNID_FILE_PRINT: MACRO_Print(); break;
587 case MNID_FILE_SETUP: MACRO_PrinterSetup(); break;
588 case MNID_FILE_EXIT: MACRO_Exit(); break;
591 case MNID_EDIT_COPYDLG: MACRO_CopyDialog(); break;
592 case MNID_EDIT_ANNOTATE:MACRO_Annotate(); break;
595 case MNID_BKMK_DEFINE: MACRO_BookmarkDefine(); break;
598 case MNID_HELP_HELPON: MACRO_HelpOn(); break;
599 case MNID_HELP_HELPTOP: MACRO_HelpOnTop(); break;
600 case MNID_HELP_ABOUT: MACRO_About(); break;
601 case MNID_HELP_WINE: ShellAbout(hWnd, "WINE", "Help", 0); break;
605 for (button = win->first_button; button; button = button->next)
606 if (wParam == button->wParam) break;
608 MACRO_ExecuteMacro(button->lpszMacro);
610 WINHELP_MessageBoxIDS(STID_NOT_IMPLEMENTED, 0x121, MB_OK);
615 if (Globals.hPopupWnd) DestroyWindow(Globals.hPopupWnd);
618 return WINHELP_HandleCommand((HWND)wParam, lParam);
620 return DefWindowProc(hWnd, msg, wParam, lParam);
623 /***********************************************************************
625 * WINHELP_ButtonBoxWndProc
627 static LRESULT CALLBACK WINHELP_ButtonBoxWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
631 WINHELP_BUTTON *button;
635 WINHELP_CheckPopup(msg);
640 win = (WINHELP_WINDOW*) ((LPCREATESTRUCT) lParam)->lpCreateParams;
641 SetWindowLong(hWnd, 0, (LONG) win);
642 win->hButtonBoxWnd = hWnd;
645 case WM_WINDOWPOSCHANGING:
646 winpos = (WINDOWPOS*) lParam;
647 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
652 for (button = win->first_button; button; button = button->next)
657 button->hWnd = CreateWindow(STRING_BUTTON, (LPSTR) button->lpszName,
658 WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
660 hWnd, (HMENU) button->wParam,
661 Globals.hInstance, 0);
662 hDc = GetDC(button->hWnd);
663 GetTextExtentPoint(hDc, button->lpszName,
664 lstrlen(button->lpszName), &textsize);
665 ReleaseDC(button->hWnd, hDc);
667 button_size.cx = max(button_size.cx, textsize.cx + BUTTON_CX);
668 button_size.cy = max(button_size.cy, textsize.cy + BUTTON_CY);
673 for (button = win->first_button; button; button = button->next)
675 SetWindowPos(button->hWnd, HWND_TOP, x, y, button_size.cx, button_size.cy, 0);
677 if (x + 2 * button_size.cx <= winpos->cx)
680 x = 0, y += button_size.cy;
682 winpos->cy = y + (x ? button_size.cy : 0);
686 SendMessage(GetParent(hWnd), msg, wParam, lParam);
690 return DefWindowProc(hWnd, msg, wParam, lParam);
693 /***********************************************************************
695 * WINHELP_TextWndProc
697 static LRESULT CALLBACK WINHELP_TextWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
701 WINHELP_LINE_PART *part;
710 if (msg != WM_LBUTTONDOWN)
711 WINHELP_CheckPopup(msg);
716 win = (WINHELP_WINDOW*) ((LPCREATESTRUCT) lParam)->lpCreateParams;
717 SetWindowLong(hWnd, 0, (LONG) win);
718 win->hTextWnd = hWnd;
719 if (win->info->win_style & WS_POPUP) Globals.hPopupWnd = win->hMainWnd = hWnd;
720 WINHELP_InitFonts(hWnd);
724 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
726 /* Calculate vertical size and position of a popup window */
727 if (win->info->win_style & WS_POPUP)
730 RECT old_window_rect;
731 RECT old_client_rect;
732 SIZE old_window_size;
733 SIZE old_client_size;
734 SIZE new_client_size;
735 SIZE new_window_size;
737 GetWindowRect(hWnd, &old_window_rect);
738 origin.x = old_window_rect.left;
739 origin.y = old_window_rect.top;
740 old_window_size.cx = old_window_rect.right - old_window_rect.left;
741 old_window_size.cy = old_window_rect.bottom - old_window_rect.top;
743 GetClientRect(hWnd, &old_client_rect);
744 old_client_size.cx = old_client_rect.right - old_client_rect.left;
745 old_client_size.cy = old_client_rect.bottom - old_client_rect.top;
747 new_client_size = old_client_size;
748 WINHELP_SplitLines(hWnd, &new_client_size);
750 if (origin.y + POPUP_YDISTANCE + new_client_size.cy <= GetSystemMetrics(SM_CYSCREEN))
751 origin.y += POPUP_YDISTANCE;
753 origin.y -= POPUP_YDISTANCE + new_client_size.cy;
755 new_window_size.cx = old_window_size.cx - old_client_size.cx + new_client_size.cx;
756 new_window_size.cy = old_window_size.cy - old_client_size.cy + new_client_size.cy;
759 CreateWindow(SHADOW_WIN_CLASS_NAME, "", WS_POPUP,
760 origin.x + SHADOW_DX, origin.y + SHADOW_DY,
761 new_window_size.cx, new_window_size.cy,
762 0, 0, Globals.hInstance, 0);
764 SetWindowPos(hWnd, HWND_TOP, origin.x, origin.y,
765 new_window_size.cx, new_window_size.cy,
767 SetWindowPos(win->hShadowWnd, hWnd, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
768 ShowWindow(win->hShadowWnd, SW_NORMAL);
769 SetActiveWindow(hWnd);
773 case WM_WINDOWPOSCHANGED:
774 winpos = (WINDOWPOS*) lParam;
776 if (!(winpos->flags & SWP_NOSIZE)) WINHELP_SetupText(hWnd);
784 INT CurPos = GetScrollPos(hWnd, SB_VERT);
785 GetScrollRange(hWnd, SB_VERT, &Min, &Max);
786 GetClientRect(hWnd, &rect);
788 switch (wParam & 0xffff)
791 case SB_THUMBPOSITION: CurPos = wParam >> 16; break;
792 case SB_TOP: CurPos = Min; break;
793 case SB_BOTTOM: CurPos = Max; break;
794 case SB_PAGEUP: CurPos -= (rect.bottom - rect.top) / 2; break;
795 case SB_PAGEDOWN: CurPos += (rect.bottom - rect.top) / 2; break;
796 case SB_LINEUP: CurPos -= GetSystemMetrics(SM_CXVSCROLL); break;
797 case SB_LINEDOWN: CurPos += GetSystemMetrics(SM_CXVSCROLL); break;
798 default: update = FALSE;
802 INT dy = GetScrollPos(hWnd, SB_VERT) - CurPos;
803 SetScrollPos(hWnd, SB_VERT, CurPos, TRUE);
804 ScrollWindow(hWnd, 0, dy, NULL, NULL);
811 hDc = BeginPaint(hWnd, &ps);
812 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
813 scroll_pos = GetScrollPos(hWnd, SB_VERT);
815 for (line = win->first_line; line; line = line->next)
817 for (part = &line->first_part; part; part = part->next)
819 switch (part->cookie)
821 case hlp_line_part_text:
822 SelectObject(hDc, part->u.text.hFont);
823 SetTextColor(hDc, part->u.text.color);
824 TextOut(hDc, part->rect.left, part->rect.top - scroll_pos,
825 part->u.text.lpsText, part->u.text.wTextLen);
826 if (part->u.text.wUnderline)
830 switch (part->u.text.wUnderline)
834 hPen = CreatePen(PS_SOLID, 1, part->u.text.color);
837 hPen = CreatePen(PS_DOT, 1, part->u.text.color);
840 WINE_FIXME("Unknow underline type\n");
844 SelectObject(hDc, hPen);
845 MoveToEx(hDc, part->rect.left, part->rect.bottom - scroll_pos - 1, NULL);
846 LineTo(hDc, part->rect.right, part->rect.bottom - scroll_pos - 1);
847 if (part->u.text.wUnderline == 2)
849 MoveToEx(hDc, part->rect.left, part->rect.bottom - scroll_pos + 1, NULL);
850 LineTo(hDc, part->rect.right, part->rect.bottom - scroll_pos + 1);
855 case hlp_line_part_bitmap:
859 hMemDC = CreateCompatibleDC(hDc);
860 SelectObject(hMemDC, part->u.bitmap.hBitmap);
861 BitBlt(hDc, part->rect.left, part->rect.top - scroll_pos,
862 part->rect.right - part->rect.left, part->rect.bottom - part->rect.top,
863 hMemDC, 0, 0, SRCCOPY);
867 case hlp_line_part_metafile:
871 SetViewportOrgEx(hDc, part->rect.left, part->rect.top - scroll_pos, &pt);
872 PlayMetaFile(hDc, part->u.metafile.hMetaFile);
873 SetViewportOrgEx(hDc, pt.x, pt.y, NULL);
884 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
886 if (WINHELP_IsOverLink(win, wParam, lParam))
887 SetCursor(win->hHandCur); /* set to hand pointer cursor to indicate a link */
889 SetCursor(win->hArrowCur); /* set to hand pointer cursor to indicate a link */
894 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
896 hPopupWnd = Globals.hPopupWnd;
897 Globals.hPopupWnd = 0;
899 part = WINHELP_IsOverLink(win, wParam, lParam);
903 HLPFILE_WINDOWINFO* wi;
905 mouse.x = LOWORD(lParam);
906 mouse.y = HIWORD(lParam);
908 if (part->link) switch (part->link->cookie)
911 hlpfile = WINHELP_LookupHelpFile(part->link->lpszString);
912 if (part->link->window == -1)
914 else if (part->link->window < hlpfile->numWindows)
915 wi = &hlpfile->windows[part->link->window];
918 WINE_WARN("link to window %d/%d\n", part->link->window, hlpfile->numWindows);
921 WINHELP_CreateHelpWindowByHash(hlpfile, part->link->lHash, wi,
925 hlpfile = WINHELP_LookupHelpFile(part->link->lpszString);
926 WINHELP_CreateHelpWindowByHash(hlpfile, part->link->lHash,
927 WINHELP_GetPopupWindowInfo(hlpfile, hWnd, &mouse),
931 MACRO_ExecuteMacro(part->link->lpszString);
934 WINE_FIXME("Unknown link cookie %d\n", part->link->cookie);
939 DestroyWindow(hPopupWnd);
943 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
945 if (hWnd == Globals.hPopupWnd) Globals.hPopupWnd = 0;
947 bExit = (Globals.wVersion >= 4 && !lstrcmpi(win->lpszName, "main"));
949 WINHELP_DeleteWindow(win);
951 if (bExit) MACRO_Exit();
953 if (!Globals.win_list)
958 return DefWindowProc(hWnd, msg, wParam, lParam);
961 /******************************************************************
962 * WINHELP_HistoryWndProc
966 static LRESULT CALLBACK WINHELP_HistoryWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
978 win = (WINHELP_WINDOW*)((LPCREATESTRUCT)lParam)->lpCreateParams;
979 SetWindowLong(hWnd, 0, (LONG)win);
980 win->hHistoryWnd = hWnd;
983 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
985 GetTextMetrics(hDc, &tm);
986 GetWindowRect(hWnd, &r);
988 r.right = r.left + 30 * tm.tmAveCharWidth;
989 r.bottom = r.top + (sizeof(win->history) / sizeof(win->history[0])) * tm.tmHeight;
990 AdjustWindowRect(&r, GetWindowLong(hWnd, GWL_STYLE), FALSE);
991 if (r.left < 0) {r.right -= r.left; r.left = 0;}
992 if (r.top < 0) {r.bottom -= r.top; r.top = 0;}
994 MoveWindow(hWnd, r.left, r.top, r.right, r.bottom, TRUE);
995 ReleaseDC(hWnd, hDc);
998 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1000 GetTextMetrics(hDc, &tm);
1001 i = HIWORD(lParam) / tm.tmHeight;
1002 if (i < win->histIndex)
1003 WINHELP_CreateHelpWindow(win->history[i], win->info, SW_SHOW);
1004 ReleaseDC(hWnd, hDc);
1007 hDc = BeginPaint(hWnd, &ps);
1008 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1009 GetTextMetrics(hDc, &tm);
1011 for (i = 0; i < win->histIndex; i++)
1013 TextOut(hDc, 0, i * tm.tmHeight, win->history[i]->lpszTitle,
1014 strlen(win->history[i]->lpszTitle));
1016 EndPaint(hWnd, &ps);
1019 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1020 if (hWnd == win->hHistoryWnd)
1021 win->hHistoryWnd = 0;
1024 return DefWindowProc(hWnd, msg, wParam, lParam);
1027 /***********************************************************************
1031 static void WINHELP_SetupText(HWND hWnd)
1033 HDC hDc = GetDC(hWnd);
1037 ShowScrollBar(hWnd, SB_VERT, FALSE);
1038 if (!WINHELP_SplitLines(hWnd, NULL))
1040 ShowScrollBar(hWnd, SB_VERT, TRUE);
1041 GetClientRect(hWnd, &rect);
1043 WINHELP_SplitLines(hWnd, &newsize);
1044 SetScrollRange(hWnd, SB_VERT, 0, rect.top + newsize.cy - rect.bottom, TRUE);
1046 else SetScrollPos(hWnd, SB_VERT, 0, FALSE);
1048 ReleaseDC(hWnd, hDc);
1051 /***********************************************************************
1053 * WINHELP_AppendText
1055 static BOOL WINHELP_AppendText(WINHELP_LINE ***linep, WINHELP_LINE_PART ***partp,
1056 LPSIZE space, LPSIZE textsize,
1057 INT *line_ascent, INT ascent,
1058 LPCSTR text, UINT textlen,
1059 HFONT font, COLORREF color, HLPFILE_LINK *link,
1063 WINHELP_LINE_PART *part;
1066 if (!*partp) /* New line */
1068 *line_ascent = ascent;
1070 line = HeapAlloc(GetProcessHeap(), 0,
1071 sizeof(WINHELP_LINE) + textlen);
1072 if (!line) return FALSE;
1075 part = &line->first_part;
1076 ptr = (char*)line + sizeof(WINHELP_LINE);
1078 line->rect.top = (**linep ? (**linep)->rect.bottom : 0) + space->cy;
1079 line->rect.bottom = line->rect.top;
1080 line->rect.left = space->cx;
1081 line->rect.right = space->cx;
1083 if (**linep) *linep = &(**linep)->next;
1087 else /* Same line */
1091 if (*line_ascent < ascent)
1093 WINHELP_LINE_PART *p;
1094 for (p = &line->first_part; p; p = p->next)
1096 p->rect.top += ascent - *line_ascent;
1097 p->rect.bottom += ascent - *line_ascent;
1099 line->rect.bottom += ascent - *line_ascent;
1100 *line_ascent = ascent;
1103 part = HeapAlloc(GetProcessHeap(), 0,
1104 sizeof(WINHELP_LINE_PART) + textlen);
1105 if (!part) return FALSE;
1107 ptr = (char*)part + sizeof(WINHELP_LINE_PART);
1110 memcpy(ptr, text, textlen);
1111 part->cookie = hlp_line_part_text;
1112 part->rect.left = line->rect.right + (*partp ? space->cx : 0);
1113 part->rect.right = part->rect.left + textsize->cx;
1114 line->rect.right = part->rect.right;
1116 ((*partp) ? line->rect.top : line->rect.bottom) + *line_ascent - ascent;
1117 part->rect.bottom = part->rect.top + textsize->cy;
1118 line->rect.bottom = max(line->rect.bottom, part->rect.bottom);
1119 part->u.text.lpsText = ptr;
1120 part->u.text.wTextLen = textlen;
1121 part->u.text.hFont = font;
1122 part->u.text.color = color;
1123 part->u.text.wUnderline = underline;
1125 WINE_TRACE("Appended text '%*.*s'[%d] @ (%ld,%ld-%ld,%ld)\n",
1126 part->u.text.wTextLen,
1127 part->u.text.wTextLen,
1128 part->u.text.lpsText,
1129 part->u.text.wTextLen,
1130 part->rect.left, part->rect.top, part->rect.right, part->rect.bottom);
1133 if (link) link->wRefCount++;
1136 *partp = &part->next;
1143 /***********************************************************************
1145 * WINHELP_AppendGfxObject
1147 static WINHELP_LINE_PART* WINHELP_AppendGfxObject(WINHELP_LINE ***linep, WINHELP_LINE_PART ***partp,
1148 LPSIZE space, LPSIZE gfxSize,
1149 HLPFILE_LINK *link, unsigned pos)
1152 WINHELP_LINE_PART *part;
1155 if (!*partp || pos == 1) /* New line */
1157 line = HeapAlloc(GetProcessHeap(), 0, sizeof(WINHELP_LINE));
1158 if (!line) return NULL;
1161 part = &line->first_part;
1163 line->rect.top = (**linep ? (**linep)->rect.bottom : 0) + space->cy;
1164 line->rect.bottom = line->rect.top;
1165 line->rect.left = space->cx;
1166 line->rect.right = space->cx;
1168 if (**linep) *linep = &(**linep)->next;
1171 ptr = (char*)line + sizeof(WINHELP_LINE);
1173 else /* Same line */
1175 if (pos == 2) WINE_FIXME("Left alignment not handled\n");
1178 part = HeapAlloc(GetProcessHeap(), 0, sizeof(WINHELP_LINE_PART));
1179 if (!part) return NULL;
1181 ptr = (char*)part + sizeof(WINHELP_LINE_PART);
1184 /* part->cookie should be set by caller (image or metafile) */
1185 part->rect.left = line->rect.right + (*partp ? space->cx : 0);
1186 part->rect.right = part->rect.left + gfxSize->cx;
1187 line->rect.right = part->rect.right;
1188 part->rect.top = (*partp) ? line->rect.top : line->rect.bottom;
1189 part->rect.bottom = part->rect.top + gfxSize->cy;
1190 line->rect.bottom = max(line->rect.bottom, part->rect.bottom);
1192 WINE_TRACE("Appended gfx @ (%ld,%ld-%ld,%ld)\n",
1193 part->rect.left, part->rect.top, part->rect.right, part->rect.bottom);
1196 if (link) link->wRefCount++;
1199 *partp = &part->next;
1207 /***********************************************************************
1209 * WINHELP_SplitLines
1211 static BOOL WINHELP_SplitLines(HWND hWnd, LPSIZE newsize)
1213 WINHELP_WINDOW *win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1214 HLPFILE_PARAGRAPH *p;
1215 WINHELP_LINE **line = &win->first_line;
1216 WINHELP_LINE_PART **part = 0;
1217 INT line_ascent = 0;
1222 if (newsize) newsize->cx = newsize->cy = 0;
1224 if (!win->page) return TRUE;
1226 WINHELP_DeleteLines(win);
1228 GetClientRect(hWnd, &rect);
1230 rect.top += INTERNAL_BORDER_WIDTH;
1231 rect.left += INTERNAL_BORDER_WIDTH;
1232 rect.right -= INTERNAL_BORDER_WIDTH;
1233 rect.bottom -= INTERNAL_BORDER_WIDTH;
1235 space.cy = rect.top;
1236 space.cx = rect.left;
1240 for (p = win->page->first_paragraph; p; p = p->next)
1244 case para_normal_text:
1245 case para_debug_text:
1248 SIZE textsize = {0, 0};
1249 LPCSTR text = p->u.text.lpszText;
1251 UINT len = strlen(text);
1252 unsigned underline = 0;
1255 COLORREF color = RGB(0, 0, 0);
1257 if (p->u.text.wFont < win->page->file->numFonts)
1259 HLPFILE* hlpfile = win->page->file;
1261 if (!hlpfile->fonts[p->u.text.wFont].hFont)
1262 hlpfile->fonts[p->u.text.wFont].hFont = CreateFontIndirect(&hlpfile->fonts[p->u.text.wFont].LogFont);
1263 hFont = hlpfile->fonts[p->u.text.wFont].hFont;
1264 color = hlpfile->fonts[p->u.text.wFont].color;
1268 UINT wFont = (p->u.text.wFont < win->fonts_len) ? p->u.text.wFont : 0;
1270 hFont = win->fonts[wFont];
1273 if (p->link && p->link->bClrChange)
1275 underline = (p->link->cookie == hlp_link_popup) ? 3 : 1;
1276 color = RGB(0, 0x80, 0);
1278 if (p->cookie == para_debug_text) color = RGB(0xff, 0, 0);
1280 SelectObject(hDc, hFont);
1282 GetTextMetrics(hDc, &tm);
1284 if (p->u.text.wIndent)
1286 indent = p->u.text.wIndent * 5 * tm.tmAveCharWidth;
1288 space.cx = rect.left + indent - 2 * tm.tmAveCharWidth;
1291 if (p->u.text.wVSpace)
1294 space.cx = rect.left + indent;
1295 space.cy += (p->u.text.wVSpace - 1) * tm.tmHeight;
1298 if (p->u.text.wHSpace)
1300 space.cx += p->u.text.wHSpace * 2 * tm.tmAveCharWidth;
1303 WINE_TRACE("splitting text %s\n", text);
1307 INT free_width = rect.right - (part ? (*line)->rect.right : rect.left) - space.cx;
1308 UINT low = 0, curr = len, high = len, textlen = 0;
1314 GetTextExtentPoint(hDc, text, curr, &textsize);
1316 if (textsize.cx <= free_width) low = curr;
1319 if (high <= low + 1) break;
1321 if (textsize.cx) curr = (curr * free_width) / textsize.cx;
1322 if (curr <= low) curr = low + 1;
1323 else if (curr >= high) curr = high - 1;
1326 while (textlen && text[textlen] && text[textlen] != ' ') textlen--;
1328 if (!part && !textlen) textlen = max(low, 1);
1330 if (free_width <= 0 || !textlen)
1333 space.cx = rect.left + indent;
1334 space.cx = min(space.cx, rect.right - rect.left - 1);
1338 WINE_TRACE("\t => %d %*s\n", textlen, textlen, text);
1340 if (!WINHELP_AppendText(&line, &part, &space, &textsize,
1341 &line_ascent, tm.tmAscent,
1342 text, textlen, hFont, color, p->link, underline) ||
1343 (!newsize && (*line)->rect.bottom > rect.bottom))
1345 ReleaseDC(hWnd, hDc);
1350 newsize->cx = max(newsize->cx, (*line)->rect.right + INTERNAL_BORDER_WIDTH);
1354 if (text[0] == ' ') text++, len--;
1363 WINHELP_LINE_PART* ref_part;
1365 if (p->u.gfx.pos & 0x8000)
1367 space.cx = rect.left;
1369 space.cy += (*line)->rect.bottom - (*line)->rect.top;
1373 if (p->cookie == para_bitmap)
1377 GetObject(p->u.gfx.u.bmp.hBitmap, sizeof(dibs), &dibs);
1378 gfxSize.cx = dibs.dsBm.bmWidth;
1379 gfxSize.cy = dibs.dsBm.bmHeight;
1381 else gfxSize = p->u.gfx.u.mf.mfSize;
1383 free_width = rect.right - ((part && *line) ? (*line)->rect.right : rect.left) - space.cx;
1384 if (free_width <= 0)
1387 space.cx = rect.left;
1388 space.cx = min(space.cx, rect.right - rect.left - 1);
1390 ref_part = WINHELP_AppendGfxObject(&line, &part, &space, &gfxSize,
1391 p->link, p->u.gfx.pos);
1392 if (!ref_part || (!newsize && (*line)->rect.bottom > rect.bottom))
1396 if (p->cookie == para_bitmap)
1398 ref_part->cookie = hlp_line_part_bitmap;
1399 ref_part->u.bitmap.hBitmap = p->u.gfx.u.bmp.hBitmap;
1403 ref_part->cookie = hlp_line_part_metafile;
1404 ref_part->u.metafile.hMetaFile = p->u.gfx.u.mf.hMetaFile;
1412 newsize->cy = (*line)->rect.bottom + INTERNAL_BORDER_WIDTH;
1414 ReleaseDC(hWnd, hDc);
1418 /***********************************************************************
1420 * WINHELP_CheckPopup
1422 static void WINHELP_CheckPopup(UINT msg)
1424 if (!Globals.hPopupWnd) return;
1429 case WM_LBUTTONDOWN:
1430 case WM_MBUTTONDOWN:
1431 case WM_RBUTTONDOWN:
1432 case WM_NCLBUTTONDOWN:
1433 case WM_NCMBUTTONDOWN:
1434 case WM_NCRBUTTONDOWN:
1435 DestroyWindow(Globals.hPopupWnd);
1436 Globals.hPopupWnd = 0;
1440 /***********************************************************************
1442 * WINHELP_DeleteLines
1444 static void WINHELP_DeleteLines(WINHELP_WINDOW *win)
1446 WINHELP_LINE *line, *next_line;
1447 WINHELP_LINE_PART *part, *next_part;
1448 for (line = win->first_line; line; line = next_line)
1450 next_line = line->next;
1451 for (part = &line->first_part; part; part = next_part)
1453 next_part = part->next;
1454 HLPFILE_FreeLink(part->link);
1455 HeapFree(GetProcessHeap(), 0, part);
1458 win->first_line = 0;
1461 /***********************************************************************
1463 * WINHELP_DeleteWindow
1465 static void WINHELP_DeleteWindow(WINHELP_WINDOW* win)
1472 for (w = &Globals.win_list; *w; w = &(*w)->next)
1481 for (b = win->first_button; b; b = bp)
1483 DestroyWindow(b->hWnd);
1485 HeapFree(GetProcessHeap(), 0, b);
1488 if (win->hShadowWnd) DestroyWindow(win->hShadowWnd);
1489 if (win->hHistoryWnd) DestroyWindow(win->hHistoryWnd);
1491 for (i = 0; i < win->histIndex; i++)
1493 HLPFILE_FreeHlpFile(win->history[i]->file);
1496 for (i = 0; i < win->backIndex; i++)
1497 HLPFILE_FreeHlpFile(win->back[i]->file);
1499 if (win->page) HLPFILE_FreeHlpFile(win->page->file);
1500 WINHELP_DeleteLines(win);
1501 HeapFree(GetProcessHeap(), 0, win);
1504 /***********************************************************************
1508 static void WINHELP_InitFonts(HWND hWnd)
1510 WINHELP_WINDOW *win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1511 LOGFONT logfontlist[] = {
1512 {-10, 0, 0, 0, 400, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1513 {-12, 0, 0, 0, 700, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1514 {-12, 0, 0, 0, 700, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1515 {-12, 0, 0, 0, 400, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1516 {-12, 0, 0, 0, 700, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1517 {-10, 0, 0, 0, 700, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1518 { -8, 0, 0, 0, 400, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"}};
1519 #define FONTS_LEN (sizeof(logfontlist)/sizeof(*logfontlist))
1521 static HFONT fonts[FONTS_LEN];
1522 static BOOL init = 0;
1524 win->fonts_len = FONTS_LEN;
1531 for (i = 0; i < FONTS_LEN; i++)
1533 fonts[i] = CreateFontIndirect(&logfontlist[i]);
1540 /***********************************************************************
1542 * WINHELP_MessageBoxIDS
1544 INT WINHELP_MessageBoxIDS(UINT ids_text, UINT ids_title, WORD type)
1546 CHAR text[MAX_STRING_LEN];
1547 CHAR title[MAX_STRING_LEN];
1549 LoadString(Globals.hInstance, ids_text, text, sizeof(text));
1550 LoadString(Globals.hInstance, ids_title, title, sizeof(title));
1552 return MessageBox(0, text, title, type);
1555 /***********************************************************************
1557 * MAIN_MessageBoxIDS_s
1559 INT WINHELP_MessageBoxIDS_s(UINT ids_text, LPCSTR str, UINT ids_title, WORD type)
1561 CHAR text[MAX_STRING_LEN];
1562 CHAR title[MAX_STRING_LEN];
1563 CHAR newtext[MAX_STRING_LEN + MAX_PATHNAME_LEN];
1565 LoadString(Globals.hInstance, ids_text, text, sizeof(text));
1566 LoadString(Globals.hInstance, ids_title, title, sizeof(title));
1567 wsprintf(newtext, text, str);
1569 return MessageBox(0, newtext, title, type);
1572 /******************************************************************
1573 * WINHELP_IsOverLink
1577 WINHELP_LINE_PART* WINHELP_IsOverLink(WINHELP_WINDOW* win, WPARAM wParam, LPARAM lParam)
1581 WINHELP_LINE_PART *part;
1582 int scroll_pos = GetScrollPos(win->hMainWnd, SB_VERT);
1584 mouse.x = LOWORD(lParam);
1585 mouse.y = HIWORD(lParam);
1586 for (line = win->first_line; line; line = line->next)
1588 for (part = &line->first_part; part; part = part->next)
1591 part->link->lpszString &&
1592 part->rect.left <= mouse.x &&
1593 part->rect.right >= mouse.x &&
1594 part->rect.top <= mouse.y + scroll_pos &&
1595 part->rect.bottom >= mouse.y + scroll_pos)