Release 960717
[wine/multimedia.git] / programs / winhelp / winhelp.c
blob594efe08af33ef8c11b31b0e20758712e0b3db13
1 /*
2 * Help Viewer
4 * Copyright 1996 Ulrich Schmid <uschmid@mail.hh.provi.de>
5 */
7 #include <stdio.h>
8 #include <windows.h>
9 #ifdef WINELIB
10 #include <resource.h>
11 #include <options.h>
12 #include <shell.h>
13 extern const char people[];
14 #endif
15 #include "winhelp.h"
17 VOID LIBWINE_Register_De(void);
18 VOID LIBWINE_Register_En(void);
19 VOID LIBWINE_Register_Fr(void);
21 static BOOL WINHELP_RegisterWinClasses();
22 static LRESULT WINHELP_MainWndProc(HWND, UINT, WPARAM, LPARAM);
23 static LRESULT WINHELP_TextWndProc(HWND, UINT, WPARAM, LPARAM);
24 static LRESULT WINHELP_ButtonBoxWndProc(HWND, UINT, WPARAM, LPARAM);
25 static VOID WINHELP_CheckPopup(UINT);
26 static BOOL WINHELP_SplitLines(HWND hWnd, LPSIZE);
27 static VOID WINHELP_InitFonts(HWND hWnd);
28 static VOID WINHELP_DeleteLines(WINHELP_WINDOW*);
29 static VOID WINHELP_DeleteWindow(WINHELP_WINDOW*);
30 static VOID WINHELP_SetupText(HWND hWnd);
31 static BOOL WINHELP_AppendText(WINHELP_LINE***, WINHELP_LINE_PART***,
32 LPSIZE, LPSIZE, INT*, INT, LPCSTR, UINT,
33 HFONT, COLORREF, HLPFILE_LINK*);
35 WINHELP_GLOBALS Globals = {3, 0, 0, 0, 0, 0};
37 static BOOL MacroTest = FALSE;
39 /***********************************************************************
41 * WinMain
44 int PASCAL WinMain (HANDLE hInstance, HANDLE prev, LPSTR cmdline, int show)
46 LPCSTR opt_lang = "En";
47 CHAR lang[3];
48 MSG msg;
49 LONG lHash = 0;
50 INT langnum;
52 #if defined(WINELIB) && !defined(HAVE_WINE_CONSTRUCTOR)
53 /* Register resources */
54 LIBWINE_Register_De();
55 LIBWINE_Register_En();
56 LIBWINE_Register_Fr();
57 #endif
59 Globals.hInstance = hInstance;
61 /* Get options */
62 while (*cmdline && (*cmdline == ' ' || *cmdline == '-'))
64 CHAR option;
65 LPCSTR topic_id;
66 if (*cmdline++ == ' ') continue;
68 option = *cmdline;
69 if (option) cmdline++;
70 while (*cmdline && *cmdline == ' ') cmdline++;
71 switch(option)
73 case 'i':
74 case 'I':
75 topic_id = cmdline;
76 while (*cmdline && *cmdline != ' ') cmdline++;
77 if (*cmdline) *cmdline++ = '\0';
78 lHash = HLPFILE_Hash(topic_id);
79 break;
81 case '3':
82 case '4':
83 Globals.wVersion = option - '0';
84 break;
86 case 't':
87 MacroTest = TRUE;
88 break;
92 #ifdef WINELIB
93 opt_lang = langNames[Options.language];
94 #endif
96 /* Find language specific string table */
97 for (langnum = 0; langnum <= MAX_LANGUAGE_NUMBER; langnum++)
99 Globals.wStringTableOffset = langnum * 0x100;
100 if (LoadString(hInstance, IDS_LANGUAGE_ID, lang, sizeof(lang)) &&
101 !lstrcmp(opt_lang, lang))
102 break;
104 if (langnum > MAX_LANGUAGE_NUMBER)
106 /* Find fallback language */
107 for (langnum = 0; langnum <= MAX_LANGUAGE_NUMBER; langnum++)
109 Globals.wStringTableOffset = langnum * 0x100;
110 if (LoadString(hInstance, IDS_LANGUAGE_ID, lang, sizeof(lang)))
111 break;
113 if (langnum > MAX_LANGUAGE_NUMBER)
115 MessageBox(0, "No language found", "FATAL ERROR", MB_OK);
116 return(1);
120 /* Change Resource names */
121 lstrcpyn(STRING_MENU_Xx + lstrlen(STRING_MENU_Xx) - 2, lang, 3);
123 /* Create primary window */
124 WINHELP_RegisterWinClasses();
125 WINHELP_CreateHelpWindow(cmdline, lHash, "main", FALSE, NULL, NULL, show);
127 /* Message loop */
128 while (GetMessage (&msg, 0, 0, 0))
130 TranslateMessage (&msg);
131 DispatchMessage (&msg);
133 return 0;
136 /***********************************************************************
138 * RegisterWinClasses
141 static BOOL WINHELP_RegisterWinClasses()
143 WNDCLASS class_main, class_button_box, class_text, class_shadow;
145 class_main.style = CS_HREDRAW | CS_VREDRAW;
146 class_main.lpfnWndProc = WINHELP_MainWndProc;
147 class_main.cbClsExtra = 0;
148 class_main.cbWndExtra = sizeof(LONG);
149 class_main.hInstance = Globals.hInstance;
150 class_main.hIcon = LoadIcon (0, IDI_APPLICATION);
151 class_main.hCursor = LoadCursor (0, IDC_ARROW);
152 class_main.hbrBackground = GetStockObject (WHITE_BRUSH);
153 class_main.lpszMenuName = 0;
154 class_main.lpszClassName = MAIN_WIN_CLASS_NAME;
156 class_button_box = class_main;
157 class_button_box.lpfnWndProc = WINHELP_ButtonBoxWndProc;
158 class_button_box.hbrBackground = GetStockObject(GRAY_BRUSH);
159 class_button_box.lpszClassName = BUTTON_BOX_WIN_CLASS_NAME;
161 class_text = class_main;
162 class_text.lpfnWndProc = WINHELP_TextWndProc;
163 class_text.lpszClassName = TEXT_WIN_CLASS_NAME;
165 class_shadow = class_main;
166 class_shadow.lpfnWndProc = DefWindowProc;
167 class_shadow.hbrBackground = GetStockObject(GRAY_BRUSH);
168 class_shadow.lpszClassName = SHADOW_WIN_CLASS_NAME;
170 return (RegisterClass(&class_main) &&
171 RegisterClass(&class_button_box) &&
172 RegisterClass(&class_text) &&
173 RegisterClass(&class_shadow));
176 /***********************************************************************
178 * WINHELP_CreateHelpWindow
181 VOID WINHELP_CreateHelpWindow(LPCSTR lpszFile, LONG lHash, LPCSTR lpszWindow,
182 BOOL bPopup, HWND hParentWnd, LPPOINT mouse, INT nCmdShow)
184 CHAR szCaption[MAX_STRING_LEN];
185 CHAR szContents[MAX_STRING_LEN];
186 CHAR szSearch[MAX_STRING_LEN];
187 CHAR szBack[MAX_STRING_LEN];
188 CHAR szHistory[MAX_STRING_LEN];
189 SIZE size = {CW_USEDEFAULT, CW_USEDEFAULT};
190 POINT origin = {240, 0};
191 LPSTR ptr;
192 HGLOBAL handle;
193 WINHELP_WINDOW *win, *oldwin;
194 HLPFILE_PAGE *page;
195 HLPFILE_MACRO *macro;
196 HWND hWnd;
197 BOOL bPrimary;
199 if (bPopup)
200 lpszWindow = NULL;
201 else if (!lpszWindow || !lpszWindow[0])
202 lpszWindow = Globals.active_win->lpszName;
203 bPrimary = lpszWindow && !lstrcmpi(lpszWindow, "main");
205 /* Read help file */
206 if (lpszFile[0])
208 page = lHash ? HLPFILE_PageByHash(lpszFile, lHash) : HLPFILE_Contents(lpszFile);
210 /* Add Suffix `.hlp' */
211 if (!page && lstrcmpi(lpszFile + strlen(lpszFile) - 4, ".hlp"))
213 CHAR szFile_hlp[MAX_PATHNAME_LEN];
215 lstrcpyn(szFile_hlp, lpszFile, sizeof(szFile_hlp) - 4);
216 szFile_hlp[sizeof(szFile_hlp) - 5] = '\0';
217 lstrcat(szFile_hlp, ".hlp");
219 page = lHash ? HLPFILE_PageByHash(szFile_hlp, lHash) : HLPFILE_Contents(szFile_hlp);
220 if (!page)
222 WINHELP_MessageBoxIDS_s(IDS_HLPFILE_ERROR_s, lpszFile, IDS_ERROR, MB_OK);
223 if (Globals.win_list) return;
227 else page = 0;
229 /* Calculate horizontal size and position of a popup window */
230 if (bPopup)
232 RECT parent_rect;
233 GetWindowRect(hParentWnd, &parent_rect);
234 size.cx = (parent_rect.right - parent_rect.left) / 2;
236 origin = *mouse;
237 ClientToScreen(hParentWnd, &origin);
238 origin.x -= size.cx / 2;
239 origin.x = MIN(origin.x, GetSystemMetrics(SM_CXSCREEN) - size.cx);
240 origin.x = MAX(origin.x, 0);
243 /* Initialize WINHELP_WINDOW struct */
244 handle = GlobalAlloc(GMEM_FIXED, sizeof(WINHELP_WINDOW) +
245 (lpszWindow ? strlen(lpszWindow) + 1 : 0));
246 if (!handle) return;
247 win = GlobalLock(handle);
248 win->hSelf = handle;
249 win->next = Globals.win_list;
250 Globals.win_list = win;
251 if (lpszWindow)
253 ptr = GlobalLock(handle);
254 ptr += sizeof(WINHELP_WINDOW);
255 lstrcpy(ptr, (LPSTR) lpszWindow);
256 win->lpszName = ptr;
258 else win->lpszName = NULL;
259 win->page = page;
260 win->first_button = 0;
261 win->first_line = 0;
262 win->hMainWnd = 0;
263 win->hButtonBoxWnd = 0;
264 win->hTextWnd = 0;
265 win->hShadowWnd = 0;
267 Globals.active_win = win;
269 /* Initialize default pushbuttons */
270 if (MacroTest && !bPopup)
271 MACRO_CreateButton("BTN_TEST", "&Test", "MacroTest");
272 if (bPrimary && page)
274 LoadString(Globals.hInstance, IDS_CONTENTS, szContents, sizeof(szContents));
275 LoadString(Globals.hInstance, IDS_SEARCH, szSearch, sizeof(szSearch));
276 LoadString(Globals.hInstance, IDS_BACK, szBack, sizeof(szBack));
277 LoadString(Globals.hInstance, IDS_HISTORY, szHistory, sizeof(szHistory));
278 MACRO_CreateButton("BTN_CONTENTS", szContents, "Contents()");
279 MACRO_CreateButton("BTN_SEARCH", szSearch, "Search()");
280 MACRO_CreateButton("BTN_BACK", szBack, "Back()");
281 MACRO_CreateButton("BTN_HISTORY", szHistory, "History()");
284 /* Initialize file specific pushbuttons */
285 if (!bPopup && page)
286 for (macro = page->file->first_macro; macro; macro = macro->next)
287 MACRO_ExecuteMacro(macro->lpszMacro);
289 /* Reuse existing window */
290 if (lpszWindow)
291 for (oldwin = win->next; oldwin; oldwin = oldwin->next)
292 if (oldwin->lpszName && !lstrcmpi(oldwin->lpszName, lpszWindow))
294 WINHELP_BUTTON *button;
296 win->hMainWnd = oldwin->hMainWnd;
297 win->hButtonBoxWnd = oldwin->hButtonBoxWnd;
298 win->hTextWnd = oldwin->hTextWnd;
299 oldwin->hMainWnd = oldwin->hButtonBoxWnd = oldwin->hTextWnd = 0;
301 SetWindowLong(win->hMainWnd, 0, (LONG) win);
302 SetWindowLong(win->hButtonBoxWnd, 0, (LONG) win);
303 SetWindowLong(win->hTextWnd, 0, (LONG) win);
305 WINHELP_InitFonts(win->hMainWnd);
307 SetWindowText(win->hMainWnd, page->file->lpszTitle);
309 WINHELP_SetupText(win->hTextWnd);
310 InvalidateRect(win->hTextWnd, NULL, TRUE);
311 SendMessage(win->hMainWnd, WM_USER, 0, 0);
312 UpdateWindow(win->hTextWnd);
315 for (button = oldwin->first_button; button; button = button->next)
316 DestroyWindow(button->hWnd);
318 WINHELP_DeleteWindow(oldwin);
319 return;
322 /* Create main Window */
323 if (!page) LoadString(Globals.hInstance, IDS_WINE_HELP, szCaption, sizeof(szCaption));
324 hWnd = CreateWindow (bPopup ? TEXT_WIN_CLASS_NAME : MAIN_WIN_CLASS_NAME,
325 page ? page->file->lpszTitle : szCaption,
326 bPopup ? WS_POPUPWINDOW | WS_BORDER : WS_OVERLAPPEDWINDOW,
327 origin.x, origin.y, size.cx, size.cy,
328 0, bPrimary ? LoadMenu(Globals.hInstance, STRING_MENU_Xx) : 0,
329 Globals.hInstance, win);
331 ShowWindow (hWnd, nCmdShow);
332 UpdateWindow (hWnd);
335 /***********************************************************************
337 * WINHELP_MainWndProc
340 static LRESULT WINHELP_MainWndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
342 WINHELP_WINDOW *win;
343 WINHELP_BUTTON *button;
344 RECT rect, button_box_rect;
345 INT text_top;
347 WINHELP_CheckPopup(msg);
349 switch (msg)
351 case WM_NCCREATE:
352 win = (WINHELP_WINDOW*) ((LPCREATESTRUCT) lParam)->lpCreateParams;
353 SetWindowLong(hWnd, 0, (LONG) win);
354 win->hMainWnd = hWnd;
355 break;
357 case WM_CREATE:
358 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
360 /* Create button box and text Window */
361 CreateWindow(BUTTON_BOX_WIN_CLASS_NAME, "", WS_CHILD | WS_VISIBLE,
362 0, 0, 0, 0, hWnd, 0, Globals.hInstance, win);
364 CreateWindow(TEXT_WIN_CLASS_NAME, "", WS_CHILD | WS_VISIBLE,
365 0, 0, 0, 0, hWnd, 0, Globals.hInstance, win);
367 /* Fall through */
368 case WM_USER:
369 case WM_WINDOWPOSCHANGED:
370 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
371 GetClientRect(hWnd, &rect);
373 /* Update button box and text Window */
374 SetWindowPos(win->hButtonBoxWnd, HWND_TOP,
375 rect.left, rect.top,
376 rect.right - rect.left,
377 rect.bottom - rect.top, 0);
379 GetWindowRect(win->hButtonBoxWnd, &button_box_rect);
380 text_top = rect.top + button_box_rect.bottom - button_box_rect.top;
382 SetWindowPos(win->hTextWnd, HWND_TOP,
383 rect.left, text_top,
384 rect.right - rect.left,
385 rect.bottom - text_top, 0);
387 break;
389 case WM_COMMAND:
390 Globals.active_win = win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
391 switch (wParam)
393 /* Menu FILE */
394 case WH_OPEN: MACRO_FileOpen(); break;
395 case WH_PRINT: MACRO_Print(); break;
396 case WH_PRINTER_SETUP: MACRO_PrinterSetup(); break;
397 case WH_EXIT: MACRO_Exit(); break;
399 /* Menu EDIT */
400 case WH_COPY_DIALOG: MACRO_CopyDialog(); break;
401 case WH_ANNOTATE: MACRO_Annotate(); break;
403 /* Menu Bookmark */
404 case WH_BOOKMARK_DEFINE: MACRO_BookmarkDefine(); break;
406 /* Menu Help */
407 case WH_HELP_ON_HELP: MACRO_HelpOn(); break;
408 case WH_HELP_ON_TOP: MACRO_HelpOnTop(); break;
410 /* Menu Info */
411 case WH_ABOUT: MACRO_About(); break;
412 #ifdef WINELIB
413 case WH_ABOUT_WINE:
414 ShellAbout(hWnd, "WINE", people, 0);
415 break;
416 #endif
418 default:
419 /* Buttons */
420 for (button = win->first_button; button; button = button->next)
421 if (wParam == button->wParam) break;
422 if (button)
423 MACRO_ExecuteMacro(button->lpszMacro);
424 else
425 WINHELP_MessageBoxIDS(IDS_NOT_IMPLEMENTED, IDS_ERROR, MB_OK);
426 break;
428 break;
431 return DefWindowProc (hWnd, msg, wParam, lParam);
434 /***********************************************************************
436 * WINHELP_ButtonBoxWndProc
439 static LRESULT WINHELP_ButtonBoxWndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
441 WINDOWPOS *winpos;
442 WINHELP_WINDOW *win;
443 WINHELP_BUTTON *button;
444 SIZE button_size;
445 INT x, y;
447 WINHELP_CheckPopup(msg);
449 switch(msg)
451 case WM_NCCREATE:
452 win = (WINHELP_WINDOW*) ((LPCREATESTRUCT) lParam)->lpCreateParams;
453 SetWindowLong(hWnd, 0, (LONG) win);
454 win->hButtonBoxWnd = hWnd;
455 break;
457 case WM_WINDOWPOSCHANGING:
458 winpos = (WINDOWPOS*) lParam;
459 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
461 /* Update buttons */
462 button_size.cx = 0;
463 button_size.cy = 0;
464 for (button = win->first_button; button; button = button->next)
466 HDC hDc;
467 SIZE textsize;
468 if (!button->hWnd)
469 button->hWnd = CreateWindow(STRING_BUTTON, (LPSTR) button->lpszName,
470 WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
471 0, 0, 0, 0,
472 hWnd, (HMENU) button->wParam,
473 Globals.hInstance, 0);
474 hDc = GetDC(button->hWnd);
475 GetTextExtentPoint(hDc, button->lpszName,
476 lstrlen(button->lpszName), &textsize);
477 ReleaseDC(button->hWnd, hDc);
479 button_size.cx = MAX(button_size.cx, textsize.cx + BUTTON_CX);
480 button_size.cy = MAX(button_size.cy, textsize.cy + BUTTON_CY);
483 x = 0;
484 y = 0;
485 for (button = win->first_button; button; button = button->next)
487 SetWindowPos(button->hWnd, HWND_TOP, x, y, button_size.cx, button_size.cy, 0);
489 if (x + 2 * button_size.cx <= winpos->cx)
490 x += button_size.cx;
491 else
492 x = 0, y += button_size.cy;
494 winpos->cy = y + (x ? button_size.cy : 0);
495 break;
497 case WM_COMMAND:
498 SendMessage(GetParent(hWnd), msg, wParam, lParam);
499 break;
502 return(DefWindowProc(hWnd, msg, wParam, lParam));
505 /***********************************************************************
507 * WINHELP_TextWndProc
510 static LRESULT WINHELP_TextWndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
512 WINHELP_WINDOW *win;
513 WINHELP_LINE *line;
514 WINHELP_LINE_PART *part;
515 WINDOWPOS *winpos;
516 PAINTSTRUCT ps;
517 HDC hDc;
518 POINT mouse;
519 INT scroll_pos;
520 HWND hPopupWnd;
521 BOOL bExit;
523 if (msg != WM_LBUTTONDOWN)
524 WINHELP_CheckPopup(msg);
526 switch (msg)
528 case WM_NCCREATE:
529 win = (WINHELP_WINDOW*) ((LPCREATESTRUCT) lParam)->lpCreateParams;
530 SetWindowLong(hWnd, 0, (LONG) win);
531 win->hTextWnd = hWnd;
532 if (!win->lpszName) Globals.hPopupWnd = win->hMainWnd = hWnd;
533 WINHELP_InitFonts(hWnd);
534 break;
536 case WM_CREATE:
537 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
539 /* Calculate vertical size and position of a popup window */
540 if (!win->lpszName)
542 POINT origin;
543 RECT old_window_rect;
544 RECT old_client_rect;
545 SIZE old_window_size;
546 SIZE old_client_size;
547 SIZE new_client_size;
548 SIZE new_window_size;
550 GetWindowRect(hWnd, &old_window_rect);
551 origin.x = old_window_rect.left;
552 origin.y = old_window_rect.top;
553 old_window_size.cx = old_window_rect.right - old_window_rect.left;
554 old_window_size.cy = old_window_rect.bottom - old_window_rect.top;
556 GetClientRect(hWnd, &old_client_rect);
557 old_client_size.cx = old_client_rect.right - old_client_rect.left;
558 old_client_size.cy = old_client_rect.bottom - old_client_rect.top;
560 new_client_size = old_client_size;
561 WINHELP_SplitLines(hWnd, &new_client_size);
563 if (origin.y + POPUP_YDISTANCE + new_client_size.cy <= GetSystemMetrics(SM_CYSCREEN))
564 origin.y += POPUP_YDISTANCE;
565 else
566 origin.y -= POPUP_YDISTANCE + new_client_size.cy;
568 new_window_size.cx = old_window_size.cx - old_client_size.cx + new_client_size.cx;
569 new_window_size.cy = old_window_size.cy - old_client_size.cy + new_client_size.cy;
571 win->hShadowWnd =
572 CreateWindow(SHADOW_WIN_CLASS_NAME, "", WS_POPUP | WS_VISIBLE,
573 origin.x + SHADOW_DX, origin.y + SHADOW_DY,
574 new_window_size.cx, new_window_size.cy,
575 0, 0, Globals.hInstance, 0);
577 SetWindowPos(hWnd, HWND_TOP, origin.x, origin.y,
578 new_window_size.cx, new_window_size.cy,
579 SWP_NOZORDER | SWP_NOACTIVATE);
580 ShowWindow(win->hShadowWnd, SW_NORMAL);
582 break;
584 case WM_WINDOWPOSCHANGED:
585 winpos = (WINDOWPOS*) lParam;
586 if (!(winpos->flags & SWP_NOSIZE)) WINHELP_SetupText(hWnd);
587 break;
589 case WM_VSCROLL:
591 BOOL update = TRUE;
592 RECT rect;
593 INT16 Min, Max;
594 INT CurPos = GetScrollPos(hWnd, SB_VERT);
595 GetScrollRange(hWnd, SB_VERT, &Min, &Max);
596 GetClientRect(hWnd, &rect);
598 switch (wParam & 0xffff)
600 case SB_THUMBTRACK:
601 case SB_THUMBPOSITION: CurPos = wParam >> 16; break;
602 case SB_TOP: CurPos = Min; break;
603 case SB_BOTTOM: CurPos = Max; break;
604 case SB_PAGEUP: CurPos -= (rect.bottom - rect.top) / 2; break;
605 case SB_PAGEDOWN: CurPos += (rect.bottom - rect.top) / 2; break;
606 case SB_LINEUP: CurPos -= GetSystemMetrics(SM_CXVSCROLL); break;
607 case SB_LINEDOWN: CurPos += GetSystemMetrics(SM_CXVSCROLL); break;
608 default: update = FALSE;
610 if (update)
612 INT dy = GetScrollPos(hWnd, SB_VERT) - CurPos;
613 SetScrollPos(hWnd, SB_VERT, CurPos, TRUE);
614 ScrollWindow(hWnd, 0, dy, NULL, NULL);
615 UpdateWindow(hWnd);
618 break;
620 case WM_PAINT:
621 hDc = BeginPaint (hWnd, &ps);
622 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
623 scroll_pos = GetScrollPos(hWnd, SB_VERT);
625 for (line = win->first_line; line; line = line->next)
626 for (part = &line->first_part; part; part = part->next)
628 SelectObject(hDc, part->hFont);
629 SetTextColor(hDc, part->color);
630 TextOut(hDc, part->rect.left, part->rect.top - scroll_pos,
631 (LPSTR) part->lpsText, part->wTextLen);
634 EndPaint (hWnd, &ps);
635 break;
637 case WM_LBUTTONDOWN:
638 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
639 scroll_pos = GetScrollPos(hWnd, SB_VERT);
641 hPopupWnd = Globals.hPopupWnd;
642 Globals.hPopupWnd = 0;
644 mouse.x = LOWORD(lParam);
645 mouse.y = HIWORD(lParam);
646 for (line = win->first_line; line; line = line->next)
647 for (part = &line->first_part; part; part = part->next)
648 if (part->link.lpszPath &&
649 part->rect.left <= mouse.x &&
650 part->rect.right >= mouse.x &&
651 part->rect.top <= mouse.y + scroll_pos &&
652 part->rect.bottom >= mouse.y + scroll_pos)
653 WINHELP_CreateHelpWindow(part->link.lpszPath, part->link.lHash, NULL,
654 part->link.bPopup, hWnd, &mouse, SW_NORMAL);
655 if (hPopupWnd)
656 DestroyWindow(hPopupWnd);
657 break;
659 case WM_NCDESTROY:
660 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
662 if (hWnd == Globals.hPopupWnd) Globals.hPopupWnd = 0;
664 bExit = (Globals.wVersion >= 4 && !lstrcmpi(win->lpszName, "main"));
666 WINHELP_DeleteWindow(win);
668 if (bExit) MACRO_Exit();
670 if (!Globals.win_list)
671 PostQuitMessage (0);
672 break;
675 return DefWindowProc (hWnd, msg, wParam, lParam);
678 /***********************************************************************
680 * SetupText
683 static VOID WINHELP_SetupText(HWND hWnd)
685 HDC hDc = GetDC(hWnd);
686 RECT rect;
687 SIZE newsize;
689 ShowScrollBar(hWnd, SB_VERT, FALSE);
690 if (!WINHELP_SplitLines(hWnd, NULL))
692 ShowScrollBar(hWnd, SB_VERT, TRUE);
693 GetClientRect(hWnd, &rect);
695 WINHELP_SplitLines(hWnd, &newsize);
696 SetScrollRange(hWnd, SB_VERT, 0, rect.top + newsize.cy - rect.bottom, TRUE);
698 else SetScrollPos(hWnd, SB_VERT, 0, FALSE);
700 ReleaseDC(hWnd, hDc);
703 /***********************************************************************
705 * WINHELP_SplitLines
708 static BOOL WINHELP_SplitLines(HWND hWnd, LPSIZE newsize)
710 WINHELP_WINDOW *win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
711 HLPFILE_PARAGRAPH *p;
712 WINHELP_LINE **line = &win->first_line;
713 WINHELP_LINE_PART **part = 0;
714 INT line_ascent = 0;
715 SIZE space;
716 RECT rect;
717 HDC hDc;
719 if (newsize) newsize->cx = newsize->cy = 0;
721 if (!win->page) return TRUE;
723 WINHELP_DeleteLines(win);
725 GetClientRect(hWnd, &rect);
727 rect.top += INTERNAL_BORDER_WIDTH;
728 rect.left += INTERNAL_BORDER_WIDTH;
729 rect.right -= INTERNAL_BORDER_WIDTH;
730 rect.bottom -= INTERNAL_BORDER_WIDTH;
733 space.cy = rect.top;
734 space.cx = rect.left;
736 hDc = GetDC(hWnd);
738 for (p = win->page->first_paragraph; p; p = p->next)
740 TEXTMETRIC tm;
741 SIZE textsize = {0, 0};
742 LPCSTR text = p->lpszText;
743 UINT len = strlen(text);
744 UINT indent = 0;
746 UINT wFont = (p->wFont < win->fonts_len) ? p->wFont : 0;
747 BOOL bUnderline = p->link && !p->link->bPopup;
748 HFONT hFont = win->fonts[wFont][bUnderline ? 1 : 0];
750 COLORREF color = RGB(0, 0, 0);
751 if (p->link) color = RGB(0, 0x80, 0);
752 if (p->bDebug) color = RGB(0xff, 0, 0);
754 SelectObject(hDc, hFont);
756 GetTextMetrics (hDc, &tm);
758 if (p->wIndent)
760 indent = p->wIndent * 5 * tm.tmAveCharWidth;
761 if (!part)
762 space.cx = rect.left + indent - 2 * tm.tmAveCharWidth;
765 if (p->wVSpace)
767 part = 0;
768 space.cx = rect.left + indent;
769 space.cy += (p->wVSpace - 1) * tm.tmHeight;
772 if (p->wHSpace)
774 space.cx += p->wHSpace * 2 * tm.tmAveCharWidth;
777 while (len)
779 INT free_width = rect.right - (part ? (*line)->rect.right : rect.left) - space.cx;
780 UINT low = 0, curr = len, high = len, textlen = 0;
782 if (free_width > 0)
784 while (1)
786 GetTextExtentPoint(hDc, text, curr, &textsize);
788 if (textsize.cx <= free_width) low = curr;
789 else high = curr;
791 if (high <= low + 1) break;
793 if (textsize.cx) curr = (curr * free_width) / textsize.cx;
794 if (curr <= low) curr = low + 1;
795 else if (curr >= high) curr = high - 1;
797 textlen = low;
798 while (textlen && text[textlen] && text[textlen] != ' ') textlen--;
800 if (!part && !textlen) textlen = MAX(low, 1);
802 if (free_width <= 0 || !textlen)
804 part = 0;
805 space.cx = rect.left + indent;
806 space.cx = MIN(space.cx, rect.right - rect.left - 1);
807 continue;
810 if (!WINHELP_AppendText(&line, &part, &space, &textsize,
811 &line_ascent, tm.tmAscent,
812 text, textlen, hFont, color, p->link) ||
813 (!newsize && (*line)->rect.bottom > rect.bottom))
815 ReleaseDC(hWnd, hDc);
816 return FALSE;
819 if (newsize)
820 newsize->cx = MAX(newsize->cx, (*line)->rect.right + INTERNAL_BORDER_WIDTH);
822 len -= textlen;
823 text += textlen;
824 if (text[0] == ' ') text++, len--;
828 if (newsize)
829 newsize->cy = (*line)->rect.bottom + INTERNAL_BORDER_WIDTH;
831 ReleaseDC(hWnd, hDc);
832 return TRUE;
835 /***********************************************************************
837 * WINHELP_AppendText
840 static BOOL WINHELP_AppendText(WINHELP_LINE ***linep, WINHELP_LINE_PART ***partp,
841 LPSIZE space, LPSIZE textsize,
842 INT *line_ascent, INT ascent,
843 LPCSTR text, UINT textlen,
844 HFONT font, COLORREF color, HLPFILE_LINK *link)
846 HGLOBAL handle;
847 WINHELP_LINE *line;
848 WINHELP_LINE_PART *part;
849 LPSTR ptr;
851 if (!*partp) /* New line */
853 *line_ascent = ascent;
855 handle = GlobalAlloc(GMEM_FIXED, sizeof(WINHELP_LINE) + textlen +
856 (link ? lstrlen(link->lpszPath) + 1 : 0));
857 if (!handle) return FALSE;
858 line = GlobalLock(handle);
859 line->next = 0;
860 part = &line->first_part;
861 ptr = GlobalLock(handle);
862 ptr += sizeof(WINHELP_LINE);
864 line->rect.top = (**linep ? (**linep)->rect.bottom : 0) + space->cy;
865 line->rect.bottom = line->rect.top;
866 line->rect.left = space->cx;
867 line->rect.right = space->cx;
869 if (**linep) *linep = &(**linep)->next;
870 **linep = line;
871 space->cy = 0;
873 else /* Same line */
875 line = **linep;
877 if (*line_ascent < ascent)
879 WINHELP_LINE_PART *p;
880 for (p = &line->first_part; p; p = p->next)
882 p->rect.top += ascent - *line_ascent;
883 p->rect.bottom += ascent - *line_ascent;
885 line->rect.bottom += ascent - *line_ascent;
886 *line_ascent = ascent;
889 handle = GlobalAlloc(GMEM_FIXED, sizeof(WINHELP_LINE_PART) + textlen +
890 (link ? lstrlen(link->lpszPath) + 1 : 0));
891 if (!handle) return FALSE;
892 part = GlobalLock(handle);
893 **partp = part;
894 ptr = GlobalLock(handle);
895 ptr += sizeof(WINHELP_LINE_PART);
898 hmemcpy(ptr, text, textlen);
899 part->rect.left = line->rect.right + (*partp ? space->cx : 0);
900 part->rect.right = part->rect.left + textsize->cx;
901 line->rect.right = part->rect.right;
902 part->rect.top =
903 ((*partp) ? line->rect.top : line->rect.bottom) + *line_ascent - ascent;
904 part->rect.bottom = part->rect.top + textsize->cy;
905 line->rect.bottom = MAX(line->rect.bottom, part->rect.bottom);
906 part->hSelf = handle;
907 part->lpsText = ptr;
908 part->wTextLen = textlen;
909 part->hFont = font;
910 part->color = color;
911 if (link)
913 strcpy(ptr + textlen, link->lpszPath);
914 part->link.lpszPath = ptr + textlen;
915 part->link.lHash = link->lHash;
916 part->link.bPopup = link->bPopup;
918 else part->link.lpszPath = 0;
920 part->next = 0;
921 *partp = &part->next;
923 space->cx = 0;
925 return TRUE;
928 /***********************************************************************
930 * WINHELP_CheckPopup
933 static VOID WINHELP_CheckPopup(UINT msg)
935 if (!Globals.hPopupWnd) return;
937 switch (msg)
939 case WM_COMMAND:
940 case WM_LBUTTONDOWN:
941 case WM_MBUTTONDOWN:
942 case WM_RBUTTONDOWN:
943 case WM_NCLBUTTONDOWN:
944 case WM_NCMBUTTONDOWN:
945 case WM_NCRBUTTONDOWN:
946 DestroyWindow(Globals.hPopupWnd);
947 Globals.hPopupWnd = 0;
951 /***********************************************************************
953 * WINHELP_DeleteLines
956 static VOID WINHELP_DeleteLines(WINHELP_WINDOW *win)
958 WINHELP_LINE *line, *next_line;
959 WINHELP_LINE_PART *part, *next_part;
960 for(line = win->first_line; line; line = next_line)
962 next_line = line->next;
963 for(part = &line->first_part; part; part = next_part)
965 next_part = part->next;
966 GlobalFree(part->hSelf);
969 win->first_line = 0;
972 /***********************************************************************
974 * WINHELP_DeleteWindow
977 static VOID WINHELP_DeleteWindow(WINHELP_WINDOW *win)
979 WINHELP_WINDOW **w;
981 for (w = &Globals.win_list; *w; w = &(*w)->next)
982 if (*w == win)
984 *w = win->next;
985 break;
988 if (win->hShadowWnd) DestroyWindow(win->hShadowWnd);
989 HLPFILE_FreeHlpFilePage(win->page);
990 WINHELP_DeleteLines(win);
991 GlobalFree(win->hSelf);
994 /***********************************************************************
996 * WINHELP_InitFonts
999 static VOID WINHELP_InitFonts(HWND hWnd)
1001 WINHELP_WINDOW *win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1002 LOGFONT logfontlist[] = {
1003 {-10, 0, 0, 0, 400, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1004 {-12, 0, 0, 0, 700, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1005 {-12, 0, 0, 0, 700, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1006 {-12, 0, 0, 0, 400, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1007 {-12, 0, 0, 0, 700, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1008 {-10, 0, 0, 0, 700, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1009 { -8, 0, 0, 0, 400, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"}};
1010 #define FONTS_LEN (sizeof(logfontlist)/sizeof(*logfontlist))
1012 static HFONT fonts[FONTS_LEN][2];
1013 static BOOL init = 0;
1015 win->fonts_len = FONTS_LEN;
1016 win->fonts = fonts;
1018 if (!init)
1020 INT i;
1022 for(i = 0; i < FONTS_LEN; i++)
1024 LOGFONT logfont = logfontlist[i];
1026 fonts[i][0] = CreateFontIndirect(&logfont);
1027 logfont.lfUnderline = 1;
1028 fonts[i][1] = CreateFontIndirect(&logfont);
1031 init = 1;
1035 /***********************************************************************
1037 * WINHELP_MessageBoxIDS
1040 INT WINHELP_MessageBoxIDS(UINT ids_text, UINT ids_title, WORD type)
1042 CHAR text[MAX_STRING_LEN];
1043 CHAR title[MAX_STRING_LEN];
1045 LoadString(Globals.hInstance, ids_text, text, sizeof(text));
1046 LoadString(Globals.hInstance, ids_title, title, sizeof(title));
1048 return(MessageBox(0, text, title, type));
1051 /***********************************************************************
1053 * MAIN_MessageBoxIDS_s
1056 INT WINHELP_MessageBoxIDS_s(UINT ids_text, LPCSTR str, UINT ids_title, WORD type)
1058 CHAR text[MAX_STRING_LEN];
1059 CHAR title[MAX_STRING_LEN];
1060 CHAR newtext[MAX_STRING_LEN + MAX_PATHNAME_LEN];
1062 LoadString(Globals.hInstance, ids_text, text, sizeof(text));
1063 LoadString(Globals.hInstance, ids_title, title, sizeof(title));
1064 wsprintf(newtext, text, str);
1066 return(MessageBox(0, newtext, title, type));
1069 /* Local Variables: */
1070 /* c-file-style: "GNU" */
1071 /* End: */