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(HWND hWnd, WPARAM wParam, LPARAM 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_image:
859 hMemDC = CreateCompatibleDC(hDc);
860 SelectObject(hMemDC, part->u.image.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);
875 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
877 if (WINHELP_IsOverLink(hWnd, wParam, lParam))
878 SetCursor(win->hHandCur); /* set to hand pointer cursor to indicate a link */
880 SetCursor(win->hArrowCur); /* set to hand pointer cursor to indicate a link */
885 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
887 hPopupWnd = Globals.hPopupWnd;
888 Globals.hPopupWnd = 0;
890 part = WINHELP_IsOverLink(hWnd, wParam, lParam);
894 HLPFILE_WINDOWINFO* wi;
896 mouse.x = LOWORD(lParam);
897 mouse.y = HIWORD(lParam);
899 switch (part->link.cookie)
904 hlpfile = WINHELP_LookupHelpFile(part->link.lpszString);
905 if (part->link.window == -1)
907 else if (part->link.window < hlpfile->numWindows)
908 wi = &hlpfile->windows[part->link.window];
911 WINE_WARN("link to window %d/%d\n", part->link.window, hlpfile->numWindows);
914 WINHELP_CreateHelpWindowByHash(hlpfile, part->link.lHash, wi,
918 hlpfile = WINHELP_LookupHelpFile(part->link.lpszString);
919 WINHELP_CreateHelpWindowByHash(hlpfile, part->link.lHash,
920 WINHELP_GetPopupWindowInfo(hlpfile, hWnd, &mouse),
924 MACRO_ExecuteMacro(part->link.lpszString);
927 WINE_FIXME("Unknown link cookie %d\n", part->link.cookie);
932 DestroyWindow(hPopupWnd);
936 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
938 if (hWnd == Globals.hPopupWnd) Globals.hPopupWnd = 0;
940 bExit = (Globals.wVersion >= 4 && !lstrcmpi(win->lpszName, "main"));
942 WINHELP_DeleteWindow(win);
944 if (bExit) MACRO_Exit();
946 if (!Globals.win_list)
951 return DefWindowProc(hWnd, msg, wParam, lParam);
954 /******************************************************************
955 * WINHELP_HistoryWndProc
959 static LRESULT CALLBACK WINHELP_HistoryWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
971 win = (WINHELP_WINDOW*)((LPCREATESTRUCT)lParam)->lpCreateParams;
972 SetWindowLong(hWnd, 0, (LONG)win);
973 win->hHistoryWnd = hWnd;
976 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
978 GetTextMetrics(hDc, &tm);
979 GetWindowRect(hWnd, &r);
981 r.right = r.left + 30 * tm.tmAveCharWidth;
982 r.bottom = r.top + (sizeof(win->history) / sizeof(win->history[0])) * tm.tmHeight;
983 AdjustWindowRect(&r, GetWindowLong(hWnd, GWL_STYLE), FALSE);
984 if (r.left < 0) {r.right -= r.left; r.left = 0;}
985 if (r.top < 0) {r.bottom -= r.top; r.top = 0;}
987 MoveWindow(hWnd, r.left, r.top, r.right, r.bottom, TRUE);
988 ReleaseDC(hWnd, hDc);
991 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
993 GetTextMetrics(hDc, &tm);
994 i = HIWORD(lParam) / tm.tmHeight;
995 if (i < win->histIndex)
996 WINHELP_CreateHelpWindow(win->history[i], win->info, SW_SHOW);
997 ReleaseDC(hWnd, hDc);
1000 hDc = BeginPaint(hWnd, &ps);
1001 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1002 GetTextMetrics(hDc, &tm);
1004 for (i = 0; i < win->histIndex; i++)
1006 TextOut(hDc, 0, i * tm.tmHeight, win->history[i]->lpszTitle,
1007 strlen(win->history[i]->lpszTitle));
1009 EndPaint(hWnd, &ps);
1012 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1013 if (hWnd == win->hHistoryWnd)
1014 win->hHistoryWnd = 0;
1017 return DefWindowProc(hWnd, msg, wParam, lParam);
1020 /***********************************************************************
1024 static void WINHELP_SetupText(HWND hWnd)
1026 HDC hDc = GetDC(hWnd);
1030 ShowScrollBar(hWnd, SB_VERT, FALSE);
1031 if (!WINHELP_SplitLines(hWnd, NULL))
1033 ShowScrollBar(hWnd, SB_VERT, TRUE);
1034 GetClientRect(hWnd, &rect);
1036 WINHELP_SplitLines(hWnd, &newsize);
1037 SetScrollRange(hWnd, SB_VERT, 0, rect.top + newsize.cy - rect.bottom, TRUE);
1039 else SetScrollPos(hWnd, SB_VERT, 0, FALSE);
1041 ReleaseDC(hWnd, hDc);
1044 /***********************************************************************
1046 * WINHELP_AppendText
1048 static BOOL WINHELP_AppendText(WINHELP_LINE ***linep, WINHELP_LINE_PART ***partp,
1049 LPSIZE space, LPSIZE textsize,
1050 INT *line_ascent, INT ascent,
1051 LPCSTR text, UINT textlen,
1052 HFONT font, COLORREF color, HLPFILE_LINK *link,
1056 WINHELP_LINE_PART *part;
1059 if (!*partp) /* New line */
1061 *line_ascent = ascent;
1063 line = HeapAlloc(GetProcessHeap(), 0,
1064 sizeof(WINHELP_LINE) + textlen + (link ? lstrlen(link->lpszString) + 1 : 0));
1065 if (!line) return FALSE;
1068 part = &line->first_part;
1069 ptr = (char*)line + sizeof(WINHELP_LINE);
1071 line->rect.top = (**linep ? (**linep)->rect.bottom : 0) + space->cy;
1072 line->rect.bottom = line->rect.top;
1073 line->rect.left = space->cx;
1074 line->rect.right = space->cx;
1076 if (**linep) *linep = &(**linep)->next;
1080 else /* Same line */
1084 if (*line_ascent < ascent)
1086 WINHELP_LINE_PART *p;
1087 for (p = &line->first_part; p; p = p->next)
1089 p->rect.top += ascent - *line_ascent;
1090 p->rect.bottom += ascent - *line_ascent;
1092 line->rect.bottom += ascent - *line_ascent;
1093 *line_ascent = ascent;
1096 part = HeapAlloc(GetProcessHeap(), 0,
1097 sizeof(WINHELP_LINE_PART) + textlen +
1098 (link ? lstrlen(link->lpszString) + 1 : 0));
1099 if (!part) return FALSE;
1101 ptr = (char*)part + sizeof(WINHELP_LINE_PART);
1104 memcpy(ptr, text, textlen);
1105 part->cookie = hlp_line_part_text;
1106 part->rect.left = line->rect.right + (*partp ? space->cx : 0);
1107 part->rect.right = part->rect.left + textsize->cx;
1108 line->rect.right = part->rect.right;
1110 ((*partp) ? line->rect.top : line->rect.bottom) + *line_ascent - ascent;
1111 part->rect.bottom = part->rect.top + textsize->cy;
1112 line->rect.bottom = max(line->rect.bottom, part->rect.bottom);
1113 part->u.text.lpsText = ptr;
1114 part->u.text.wTextLen = textlen;
1115 part->u.text.hFont = font;
1116 part->u.text.color = color;
1117 part->u.text.wUnderline = underline;
1119 WINE_TRACE("Appended text '%*.*s'[%d] @ (%d,%d-%d,%d)\n",
1120 part->u.text.wTextLen,
1121 part->u.text.wTextLen,
1122 part->u.text.lpsText,
1123 part->u.text.wTextLen,
1124 part->rect.left, part->rect.top, part->rect.right, part->rect.bottom);
1127 strcpy(ptr + textlen, link->lpszString);
1128 part->link.lpszString = ptr + textlen;
1129 part->link.cookie = link->cookie;
1130 part->link.lHash = link->lHash;
1131 part->link.bClrChange = link->bClrChange;
1132 part->link.window = link->window;
1134 else part->link.cookie = hlp_link_none;
1137 *partp = &part->next;
1144 /***********************************************************************
1146 * WINHELP_AppendBitmap
1148 static BOOL WINHELP_AppendBitmap(WINHELP_LINE ***linep, WINHELP_LINE_PART ***partp,
1150 HBITMAP hBmp, LPSIZE bmpSize,
1151 HLPFILE_LINK *link, unsigned pos)
1154 WINHELP_LINE_PART *part;
1157 if (!*partp || pos == 1) /* New line */
1159 line = HeapAlloc(GetProcessHeap(), 0,
1160 sizeof(WINHELP_LINE) + (link ? lstrlen(link->lpszString) + 1 : 0));
1161 if (!line) return FALSE;
1164 part = &line->first_part;
1166 line->rect.top = (**linep ? (**linep)->rect.bottom : 0) + space->cy;
1167 line->rect.bottom = line->rect.top;
1168 line->rect.left = space->cx;
1169 line->rect.right = space->cx;
1171 if (**linep) *linep = &(**linep)->next;
1174 ptr = (char*)line + sizeof(WINHELP_LINE);
1176 else /* Same line */
1178 if (pos == 2) WINE_FIXME("Left alignment not handled\n");
1181 part = HeapAlloc(GetProcessHeap(), 0,
1182 sizeof(WINHELP_LINE_PART) +
1183 (link ? lstrlen(link->lpszString) + 1 : 0));
1184 if (!part) return FALSE;
1186 ptr = (char*)part + sizeof(WINHELP_LINE_PART);
1189 part->cookie = hlp_line_part_image;
1190 part->rect.left = line->rect.right + (*partp ? space->cx : 0);
1191 part->rect.right = part->rect.left + bmpSize->cx;
1192 line->rect.right = part->rect.right;
1194 ((*partp) ? line->rect.top : line->rect.bottom);
1195 part->rect.bottom = part->rect.top + bmpSize->cy;
1196 line->rect.bottom = max(line->rect.bottom, part->rect.bottom);
1197 part->u.image.hBitmap = hBmp;
1199 WINE_TRACE("Appended bitmap '%d' @ (%d,%d-%d,%d)\n",
1200 (unsigned)part->u.image.hBitmap,
1201 part->rect.left, part->rect.top, part->rect.right, part->rect.bottom);
1205 strcpy(ptr, link->lpszString);
1206 part->link.lpszString = ptr;
1207 part->link.cookie = link->cookie;
1208 part->link.lHash = link->lHash;
1209 part->link.bClrChange = link->bClrChange;
1210 part->link.window = link->window;
1212 else part->link.cookie = hlp_link_none;
1215 *partp = &part->next;
1223 /***********************************************************************
1225 * WINHELP_SplitLines
1227 static BOOL WINHELP_SplitLines(HWND hWnd, LPSIZE newsize)
1229 WINHELP_WINDOW *win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1230 HLPFILE_PARAGRAPH *p;
1231 WINHELP_LINE **line = &win->first_line;
1232 WINHELP_LINE_PART **part = 0;
1233 INT line_ascent = 0;
1238 if (newsize) newsize->cx = newsize->cy = 0;
1240 if (!win->page) return TRUE;
1242 WINHELP_DeleteLines(win);
1244 GetClientRect(hWnd, &rect);
1246 rect.top += INTERNAL_BORDER_WIDTH;
1247 rect.left += INTERNAL_BORDER_WIDTH;
1248 rect.right -= INTERNAL_BORDER_WIDTH;
1249 rect.bottom -= INTERNAL_BORDER_WIDTH;
1251 space.cy = rect.top;
1252 space.cx = rect.left;
1256 for (p = win->page->first_paragraph; p; p = p->next)
1260 case para_normal_text:
1261 case para_debug_text:
1264 SIZE textsize = {0, 0};
1265 LPCSTR text = p->u.text.lpszText;
1267 UINT len = strlen(text);
1268 unsigned underline = 0;
1271 COLORREF color = RGB(0, 0, 0);
1273 if (p->u.text.wFont < win->page->file->numFonts)
1275 HLPFILE* hlpfile = win->page->file;
1277 if (!hlpfile->fonts[p->u.text.wFont].hFont)
1278 hlpfile->fonts[p->u.text.wFont].hFont = CreateFontIndirect(&hlpfile->fonts[p->u.text.wFont].LogFont);
1279 hFont = hlpfile->fonts[p->u.text.wFont].hFont;
1280 color = hlpfile->fonts[p->u.text.wFont].color;
1284 UINT wFont = (p->u.text.wFont < win->fonts_len) ? p->u.text.wFont : 0;
1286 hFont = win->fonts[wFont];
1291 underline = (p->link->cookie == hlp_link_popup) ? 3 : 1;
1292 color = RGB(0, 0x80, 0);
1294 if (p->cookie == para_debug_text) color = RGB(0xff, 0, 0);
1296 SelectObject(hDc, hFont);
1298 GetTextMetrics(hDc, &tm);
1300 if (p->u.text.wIndent)
1302 indent = p->u.text.wIndent * 5 * tm.tmAveCharWidth;
1304 space.cx = rect.left + indent - 2 * tm.tmAveCharWidth;
1307 if (p->u.text.wVSpace)
1310 space.cx = rect.left + indent;
1311 space.cy += (p->u.text.wVSpace - 1) * tm.tmHeight;
1314 if (p->u.text.wHSpace)
1316 space.cx += p->u.text.wHSpace * 2 * tm.tmAveCharWidth;
1319 WINE_TRACE("splitting text %s\n", text);
1323 INT free_width = rect.right - (part ? (*line)->rect.right : rect.left) - space.cx;
1324 UINT low = 0, curr = len, high = len, textlen = 0;
1330 GetTextExtentPoint(hDc, text, curr, &textsize);
1332 if (textsize.cx <= free_width) low = curr;
1335 if (high <= low + 1) break;
1337 if (textsize.cx) curr = (curr * free_width) / textsize.cx;
1338 if (curr <= low) curr = low + 1;
1339 else if (curr >= high) curr = high - 1;
1342 while (textlen && text[textlen] && text[textlen] != ' ') textlen--;
1344 if (!part && !textlen) textlen = max(low, 1);
1346 if (free_width <= 0 || !textlen)
1349 space.cx = rect.left + indent;
1350 space.cx = min(space.cx, rect.right - rect.left - 1);
1354 WINE_TRACE("\t => %d %*s\n", textlen, textlen, text);
1356 if (!WINHELP_AppendText(&line, &part, &space, &textsize,
1357 &line_ascent, tm.tmAscent,
1358 text, textlen, hFont, color, p->link, underline) ||
1359 (!newsize && (*line)->rect.bottom > rect.bottom))
1361 ReleaseDC(hWnd, hDc);
1366 newsize->cx = max(newsize->cx, (*line)->rect.right + INTERNAL_BORDER_WIDTH);
1370 if (text[0] == ' ') text++, len--;
1380 if (p->u.image.pos & 0x8000)
1382 space.cx = rect.left;
1384 space.cy += (*line)->rect.bottom - (*line)->rect.top;
1388 GetObject(p->u.image.hBitmap, sizeof(dibs), &dibs);
1389 bmpSize.cx = dibs.dsBm.bmWidth;
1390 bmpSize.cy = dibs.dsBm.bmHeight;
1392 free_width = rect.right - ((part && *line) ? (*line)->rect.right : rect.left) - space.cx;
1393 if (free_width <= 0)
1396 space.cx = rect.left;
1397 space.cx = min(space.cx, rect.right - rect.left - 1);
1399 if (!WINHELP_AppendBitmap(&line, &part, &space,
1400 p->u.image.hBitmap, &bmpSize,
1401 p->link, p->u.image.pos) ||
1402 (!newsize && (*line)->rect.bottom > rect.bottom))
1413 newsize->cy = (*line)->rect.bottom + INTERNAL_BORDER_WIDTH;
1415 ReleaseDC(hWnd, hDc);
1419 /***********************************************************************
1421 * WINHELP_CheckPopup
1423 static void WINHELP_CheckPopup(UINT msg)
1425 if (!Globals.hPopupWnd) return;
1430 case WM_LBUTTONDOWN:
1431 case WM_MBUTTONDOWN:
1432 case WM_RBUTTONDOWN:
1433 case WM_NCLBUTTONDOWN:
1434 case WM_NCMBUTTONDOWN:
1435 case WM_NCRBUTTONDOWN:
1436 DestroyWindow(Globals.hPopupWnd);
1437 Globals.hPopupWnd = 0;
1441 /***********************************************************************
1443 * WINHELP_DeleteLines
1445 static void WINHELP_DeleteLines(WINHELP_WINDOW *win)
1447 WINHELP_LINE *line, *next_line;
1448 WINHELP_LINE_PART *part, *next_part;
1449 for (line = win->first_line; line; line = next_line)
1451 next_line = line->next;
1452 for (part = &line->first_part; part; part = next_part)
1454 next_part = part->next;
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(HWND hWnd, WPARAM wParam, LPARAM lParam)
1579 WINHELP_WINDOW* win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1582 WINHELP_LINE_PART *part;
1583 int scroll_pos = GetScrollPos(hWnd, SB_VERT);
1585 mouse.x = LOWORD(lParam);
1586 mouse.y = HIWORD(lParam);
1587 for (line = win->first_line; line; line = line->next)
1589 for (part = &line->first_part; part; part = part->next)
1591 if (part->link.cookie != hlp_link_none &&
1592 part->link.lpszString &&
1593 part->rect.left <= mouse.x &&
1594 part->rect.right >= mouse.x &&
1595 part->rect.top <= mouse.y + scroll_pos &&
1596 part->rect.bottom >= mouse.y + scroll_pos)