Release 970215
[wine/hacks.git] / programs / winhelp / winhelp.c
blob8c338e3b57147bd0d8abd83c44992acafa4b5265
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_Fi(void);
20 VOID LIBWINE_Register_Fr(void);
21 VOID LIBWINE_Register_It(void);
22 VOID LIBWINE_Register_Ko(void);
23 VOID LIBWINE_Register_Hu(void);
25 static BOOL WINHELP_RegisterWinClasses();
26 static LRESULT WINHELP_MainWndProc(HWND, UINT, WPARAM, LPARAM);
27 static LRESULT WINHELP_TextWndProc(HWND, UINT, WPARAM, LPARAM);
28 static LRESULT WINHELP_ButtonBoxWndProc(HWND, UINT, WPARAM, LPARAM);
29 static VOID WINHELP_CheckPopup(UINT);
30 static BOOL WINHELP_SplitLines(HWND hWnd, LPSIZE);
31 static VOID WINHELP_InitFonts(HWND hWnd);
32 static VOID WINHELP_DeleteLines(WINHELP_WINDOW*);
33 static VOID WINHELP_DeleteWindow(WINHELP_WINDOW*);
34 static VOID WINHELP_SetupText(HWND hWnd);
35 static BOOL WINHELP_AppendText(WINHELP_LINE***, WINHELP_LINE_PART***,
36 LPSIZE, LPSIZE, INT*, INT, LPCSTR, UINT,
37 HFONT, COLORREF, HLPFILE_LINK*);
39 WINHELP_GLOBALS Globals = {3, 0, 0, 0, 0, 0};
41 static BOOL MacroTest = FALSE;
43 /***********************************************************************
45 * WinMain
48 int PASCAL WinMain (HANDLE hInstance, HANDLE prev, LPSTR cmdline, int show)
50 LPCSTR opt_lang = "En";
51 CHAR lang[3];
52 MSG msg;
53 LONG lHash = 0;
54 INT langnum;
56 #if defined(WINELIB) && !defined(HAVE_WINE_CONSTRUCTOR)
57 /* Register resources */
58 LIBWINE_Register_De();
59 LIBWINE_Register_En();
60 LIBWINE_Register_Fi();
61 LIBWINE_Register_Fr();
62 LIBWINE_Register_It();
63 LIBWINE_Register_Ko();
64 LIBWINE_Register_Hu();
65 #endif
67 Globals.hInstance = hInstance;
69 /* Get options */
70 while (*cmdline && (*cmdline == ' ' || *cmdline == '-'))
72 CHAR option;
73 LPCSTR topic_id;
74 if (*cmdline++ == ' ') continue;
76 option = *cmdline;
77 if (option) cmdline++;
78 while (*cmdline && *cmdline == ' ') cmdline++;
79 switch(option)
81 case 'i':
82 case 'I':
83 topic_id = cmdline;
84 while (*cmdline && *cmdline != ' ') cmdline++;
85 if (*cmdline) *cmdline++ = '\0';
86 lHash = HLPFILE_Hash(topic_id);
87 break;
89 case '3':
90 case '4':
91 Globals.wVersion = option - '0';
92 break;
94 case 't':
95 MacroTest = TRUE;
96 break;
100 #ifdef WINELIB
101 opt_lang = Languages[Options.language].name;
102 #endif
104 /* Find language specific string table */
105 for (langnum = 0; langnum <= MAX_LANGUAGE_NUMBER; langnum++)
107 Globals.wStringTableOffset = langnum * 0x100;
108 if (LoadString(hInstance, IDS_LANGUAGE_ID, lang, sizeof(lang)) &&
109 !lstrcmp(opt_lang, lang))
110 break;
112 if (langnum > MAX_LANGUAGE_NUMBER)
114 /* Find fallback language */
115 for (langnum = 0; langnum <= MAX_LANGUAGE_NUMBER; langnum++)
117 Globals.wStringTableOffset = langnum * 0x100;
118 if (LoadString(hInstance, IDS_LANGUAGE_ID, lang, sizeof(lang)))
119 break;
121 if (langnum > MAX_LANGUAGE_NUMBER)
123 MessageBox(0, "No language found", "FATAL ERROR", MB_OK);
124 return(1);
128 /* Change Resource names */
129 lstrcpyn(STRING_MENU_Xx + lstrlen(STRING_MENU_Xx) - 2, lang, 3);
131 /* Create primary window */
132 WINHELP_RegisterWinClasses();
133 WINHELP_CreateHelpWindow(cmdline, lHash, "main", FALSE, NULL, NULL, show);
135 /* Message loop */
136 while (GetMessage (&msg, 0, 0, 0))
138 TranslateMessage (&msg);
139 DispatchMessage (&msg);
141 return 0;
144 /***********************************************************************
146 * RegisterWinClasses
149 static BOOL WINHELP_RegisterWinClasses()
151 WNDCLASS class_main, class_button_box, class_text, class_shadow;
153 class_main.style = CS_HREDRAW | CS_VREDRAW;
154 class_main.lpfnWndProc = WINHELP_MainWndProc;
155 class_main.cbClsExtra = 0;
156 class_main.cbWndExtra = sizeof(LONG);
157 class_main.hInstance = Globals.hInstance;
158 class_main.hIcon = LoadIcon (0, IDI_APPLICATION);
159 class_main.hCursor = LoadCursor (0, IDC_ARROW);
160 class_main.hbrBackground = GetStockObject (WHITE_BRUSH);
161 class_main.lpszMenuName = 0;
162 class_main.lpszClassName = MAIN_WIN_CLASS_NAME;
164 class_button_box = class_main;
165 class_button_box.lpfnWndProc = WINHELP_ButtonBoxWndProc;
166 class_button_box.hbrBackground = GetStockObject(GRAY_BRUSH);
167 class_button_box.lpszClassName = BUTTON_BOX_WIN_CLASS_NAME;
169 class_text = class_main;
170 class_text.lpfnWndProc = WINHELP_TextWndProc;
171 class_text.lpszClassName = TEXT_WIN_CLASS_NAME;
173 class_shadow = class_main;
174 class_shadow.lpfnWndProc = DefWindowProc;
175 class_shadow.hbrBackground = GetStockObject(GRAY_BRUSH);
176 class_shadow.lpszClassName = SHADOW_WIN_CLASS_NAME;
178 return (RegisterClass(&class_main) &&
179 RegisterClass(&class_button_box) &&
180 RegisterClass(&class_text) &&
181 RegisterClass(&class_shadow));
184 /***********************************************************************
186 * WINHELP_CreateHelpWindow
189 VOID WINHELP_CreateHelpWindow(LPCSTR lpszFile, LONG lHash, LPCSTR lpszWindow,
190 BOOL bPopup, HWND hParentWnd, LPPOINT mouse, INT nCmdShow)
192 CHAR szCaption[MAX_STRING_LEN];
193 CHAR szContents[MAX_STRING_LEN];
194 CHAR szSearch[MAX_STRING_LEN];
195 CHAR szBack[MAX_STRING_LEN];
196 CHAR szHistory[MAX_STRING_LEN];
197 SIZE size = {CW_USEDEFAULT, CW_USEDEFAULT};
198 POINT origin = {240, 0};
199 LPSTR ptr;
200 HGLOBAL handle;
201 WINHELP_WINDOW *win, *oldwin;
202 HLPFILE_PAGE *page;
203 HLPFILE_MACRO *macro;
204 HWND hWnd;
205 BOOL bPrimary;
207 if (bPopup)
208 lpszWindow = NULL;
209 else if (!lpszWindow || !lpszWindow[0])
210 lpszWindow = Globals.active_win->lpszName;
211 bPrimary = lpszWindow && !lstrcmpi(lpszWindow, "main");
213 /* Read help file */
214 if (lpszFile[0])
216 page = lHash ? HLPFILE_PageByHash(lpszFile, lHash) : HLPFILE_Contents(lpszFile);
218 /* Add Suffix `.hlp' */
219 if (!page && lstrcmpi(lpszFile + strlen(lpszFile) - 4, ".hlp"))
221 CHAR szFile_hlp[MAX_PATHNAME_LEN];
223 lstrcpyn(szFile_hlp, lpszFile, sizeof(szFile_hlp) - 4);
224 szFile_hlp[sizeof(szFile_hlp) - 5] = '\0';
225 lstrcat(szFile_hlp, ".hlp");
227 page = lHash ? HLPFILE_PageByHash(szFile_hlp, lHash) : HLPFILE_Contents(szFile_hlp);
228 if (!page)
230 WINHELP_MessageBoxIDS_s(IDS_HLPFILE_ERROR_s, lpszFile, IDS_ERROR, MB_OK);
231 if (Globals.win_list) return;
235 else page = 0;
237 /* Calculate horizontal size and position of a popup window */
238 if (bPopup)
240 RECT parent_rect;
241 GetWindowRect(hParentWnd, &parent_rect);
242 size.cx = (parent_rect.right - parent_rect.left) / 2;
244 origin = *mouse;
245 ClientToScreen(hParentWnd, &origin);
246 origin.x -= size.cx / 2;
247 origin.x = MIN(origin.x, GetSystemMetrics(SM_CXSCREEN) - size.cx);
248 origin.x = MAX(origin.x, 0);
251 /* Initialize WINHELP_WINDOW struct */
252 handle = GlobalAlloc(GMEM_FIXED, sizeof(WINHELP_WINDOW) +
253 (lpszWindow ? strlen(lpszWindow) + 1 : 0));
254 if (!handle) return;
255 win = GlobalLock(handle);
256 win->hSelf = handle;
257 win->next = Globals.win_list;
258 Globals.win_list = win;
259 if (lpszWindow)
261 ptr = GlobalLock(handle);
262 ptr += sizeof(WINHELP_WINDOW);
263 lstrcpy(ptr, (LPSTR) lpszWindow);
264 win->lpszName = ptr;
266 else win->lpszName = NULL;
267 win->page = page;
268 win->first_button = 0;
269 win->first_line = 0;
270 win->hMainWnd = 0;
271 win->hButtonBoxWnd = 0;
272 win->hTextWnd = 0;
273 win->hShadowWnd = 0;
275 Globals.active_win = win;
277 /* Initialize default pushbuttons */
278 if (MacroTest && !bPopup)
279 MACRO_CreateButton("BTN_TEST", "&Test", "MacroTest");
280 if (bPrimary && page)
282 LoadString(Globals.hInstance, IDS_CONTENTS, szContents, sizeof(szContents));
283 LoadString(Globals.hInstance, IDS_SEARCH, szSearch, sizeof(szSearch));
284 LoadString(Globals.hInstance, IDS_BACK, szBack, sizeof(szBack));
285 LoadString(Globals.hInstance, IDS_HISTORY, szHistory, sizeof(szHistory));
286 MACRO_CreateButton("BTN_CONTENTS", szContents, "Contents()");
287 MACRO_CreateButton("BTN_SEARCH", szSearch, "Search()");
288 MACRO_CreateButton("BTN_BACK", szBack, "Back()");
289 MACRO_CreateButton("BTN_HISTORY", szHistory, "History()");
292 /* Initialize file specific pushbuttons */
293 if (!bPopup && page)
294 for (macro = page->file->first_macro; macro; macro = macro->next)
295 MACRO_ExecuteMacro(macro->lpszMacro);
297 /* Reuse existing window */
298 if (lpszWindow)
299 for (oldwin = win->next; oldwin; oldwin = oldwin->next)
300 if (oldwin->lpszName && !lstrcmpi(oldwin->lpszName, lpszWindow))
302 WINHELP_BUTTON *button;
304 win->hMainWnd = oldwin->hMainWnd;
305 win->hButtonBoxWnd = oldwin->hButtonBoxWnd;
306 win->hTextWnd = oldwin->hTextWnd;
307 oldwin->hMainWnd = oldwin->hButtonBoxWnd = oldwin->hTextWnd = 0;
309 SetWindowLong(win->hMainWnd, 0, (LONG) win);
310 SetWindowLong(win->hButtonBoxWnd, 0, (LONG) win);
311 SetWindowLong(win->hTextWnd, 0, (LONG) win);
313 WINHELP_InitFonts(win->hMainWnd);
315 if (page) {
316 SetWindowText(win->hMainWnd, page->file->lpszTitle);
319 WINHELP_SetupText(win->hTextWnd);
320 InvalidateRect(win->hTextWnd, NULL, TRUE);
321 SendMessage(win->hMainWnd, WM_USER, 0, 0);
322 UpdateWindow(win->hTextWnd);
325 for (button = oldwin->first_button; button; button = button->next)
326 DestroyWindow(button->hWnd);
328 WINHELP_DeleteWindow(oldwin);
329 return;
332 /* Create main Window */
333 if (!page) LoadString(Globals.hInstance, IDS_WINE_HELP, szCaption, sizeof(szCaption));
334 hWnd = CreateWindow (bPopup ? TEXT_WIN_CLASS_NAME : MAIN_WIN_CLASS_NAME,
335 page ? page->file->lpszTitle : szCaption,
336 bPopup ? WS_POPUPWINDOW | WS_BORDER : WS_OVERLAPPEDWINDOW,
337 origin.x, origin.y, size.cx, size.cy,
338 0, bPrimary ? LoadMenu(Globals.hInstance, STRING_MENU_Xx) : 0,
339 Globals.hInstance, win);
341 ShowWindow (hWnd, nCmdShow);
342 UpdateWindow (hWnd);
345 /***********************************************************************
347 * WINHELP_MainWndProc
350 static LRESULT WINHELP_MainWndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
352 WINHELP_WINDOW *win;
353 WINHELP_BUTTON *button;
354 RECT rect, button_box_rect;
355 INT text_top;
357 WINHELP_CheckPopup(msg);
359 switch (msg)
361 case WM_NCCREATE:
362 win = (WINHELP_WINDOW*) ((LPCREATESTRUCT) lParam)->lpCreateParams;
363 SetWindowLong(hWnd, 0, (LONG) win);
364 win->hMainWnd = hWnd;
365 break;
367 case WM_CREATE:
368 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
370 /* Create button box and text Window */
371 CreateWindow(BUTTON_BOX_WIN_CLASS_NAME, "", WS_CHILD | WS_VISIBLE,
372 0, 0, 0, 0, hWnd, 0, Globals.hInstance, win);
374 CreateWindow(TEXT_WIN_CLASS_NAME, "", WS_CHILD | WS_VISIBLE,
375 0, 0, 0, 0, hWnd, 0, Globals.hInstance, win);
377 /* Fall through */
378 case WM_USER:
379 case WM_WINDOWPOSCHANGED:
380 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
381 GetClientRect(hWnd, &rect);
383 /* Update button box and text Window */
384 SetWindowPos(win->hButtonBoxWnd, HWND_TOP,
385 rect.left, rect.top,
386 rect.right - rect.left,
387 rect.bottom - rect.top, 0);
389 GetWindowRect(win->hButtonBoxWnd, &button_box_rect);
390 text_top = rect.top + button_box_rect.bottom - button_box_rect.top;
392 SetWindowPos(win->hTextWnd, HWND_TOP,
393 rect.left, text_top,
394 rect.right - rect.left,
395 rect.bottom - text_top, 0);
397 break;
399 case WM_COMMAND:
400 Globals.active_win = win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
401 switch (wParam)
403 /* Menu FILE */
404 case WH_OPEN: MACRO_FileOpen(); break;
405 case WH_PRINT: MACRO_Print(); break;
406 case WH_PRINTER_SETUP: MACRO_PrinterSetup(); break;
407 case WH_EXIT: MACRO_Exit(); break;
409 /* Menu EDIT */
410 case WH_COPY_DIALOG: MACRO_CopyDialog(); break;
411 case WH_ANNOTATE: MACRO_Annotate(); break;
413 /* Menu Bookmark */
414 case WH_BOOKMARK_DEFINE: MACRO_BookmarkDefine(); break;
416 /* Menu Help */
417 case WH_HELP_ON_HELP: MACRO_HelpOn(); break;
418 case WH_HELP_ON_TOP: MACRO_HelpOnTop(); break;
420 /* Menu Info */
421 case WH_ABOUT: MACRO_About(); break;
422 #ifdef WINELIB
423 case WH_ABOUT_WINE:
424 ShellAbout(hWnd, "WINE", people, 0);
425 break;
426 #endif
428 default:
429 /* Buttons */
430 for (button = win->first_button; button; button = button->next)
431 if (wParam == button->wParam) break;
432 if (button)
433 MACRO_ExecuteMacro(button->lpszMacro);
434 else
435 WINHELP_MessageBoxIDS(IDS_NOT_IMPLEMENTED, IDS_ERROR, MB_OK);
436 break;
438 break;
441 return DefWindowProc (hWnd, msg, wParam, lParam);
444 /***********************************************************************
446 * WINHELP_ButtonBoxWndProc
449 static LRESULT WINHELP_ButtonBoxWndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
451 WINDOWPOS *winpos;
452 WINHELP_WINDOW *win;
453 WINHELP_BUTTON *button;
454 SIZE button_size;
455 INT x, y;
457 WINHELP_CheckPopup(msg);
459 switch(msg)
461 case WM_NCCREATE:
462 win = (WINHELP_WINDOW*) ((LPCREATESTRUCT) lParam)->lpCreateParams;
463 SetWindowLong(hWnd, 0, (LONG) win);
464 win->hButtonBoxWnd = hWnd;
465 break;
467 case WM_WINDOWPOSCHANGING:
468 winpos = (WINDOWPOS*) lParam;
469 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
471 /* Update buttons */
472 button_size.cx = 0;
473 button_size.cy = 0;
474 for (button = win->first_button; button; button = button->next)
476 HDC hDc;
477 SIZE textsize;
478 if (!button->hWnd)
479 button->hWnd = CreateWindow(STRING_BUTTON, (LPSTR) button->lpszName,
480 WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
481 0, 0, 0, 0,
482 hWnd, (HMENU) button->wParam,
483 Globals.hInstance, 0);
484 hDc = GetDC(button->hWnd);
485 GetTextExtentPoint(hDc, button->lpszName,
486 lstrlen(button->lpszName), &textsize);
487 ReleaseDC(button->hWnd, hDc);
489 button_size.cx = MAX(button_size.cx, textsize.cx + BUTTON_CX);
490 button_size.cy = MAX(button_size.cy, textsize.cy + BUTTON_CY);
493 x = 0;
494 y = 0;
495 for (button = win->first_button; button; button = button->next)
497 SetWindowPos(button->hWnd, HWND_TOP, x, y, button_size.cx, button_size.cy, 0);
499 if (x + 2 * button_size.cx <= winpos->cx)
500 x += button_size.cx;
501 else
502 x = 0, y += button_size.cy;
504 winpos->cy = y + (x ? button_size.cy : 0);
505 break;
507 case WM_COMMAND:
508 SendMessage(GetParent(hWnd), msg, wParam, lParam);
509 break;
512 return(DefWindowProc(hWnd, msg, wParam, lParam));
515 /***********************************************************************
517 * WINHELP_TextWndProc
520 static LRESULT WINHELP_TextWndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
522 WINHELP_WINDOW *win;
523 WINHELP_LINE *line;
524 WINHELP_LINE_PART *part;
525 WINDOWPOS *winpos;
526 PAINTSTRUCT ps;
527 HDC hDc;
528 POINT mouse;
529 INT scroll_pos;
530 HWND hPopupWnd;
531 BOOL bExit;
533 if (msg != WM_LBUTTONDOWN)
534 WINHELP_CheckPopup(msg);
536 switch (msg)
538 case WM_NCCREATE:
539 win = (WINHELP_WINDOW*) ((LPCREATESTRUCT) lParam)->lpCreateParams;
540 SetWindowLong(hWnd, 0, (LONG) win);
541 win->hTextWnd = hWnd;
542 if (!win->lpszName) Globals.hPopupWnd = win->hMainWnd = hWnd;
543 WINHELP_InitFonts(hWnd);
544 break;
546 case WM_CREATE:
547 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
549 /* Calculate vertical size and position of a popup window */
550 if (!win->lpszName)
552 POINT origin;
553 RECT old_window_rect;
554 RECT old_client_rect;
555 SIZE old_window_size;
556 SIZE old_client_size;
557 SIZE new_client_size;
558 SIZE new_window_size;
560 GetWindowRect(hWnd, &old_window_rect);
561 origin.x = old_window_rect.left;
562 origin.y = old_window_rect.top;
563 old_window_size.cx = old_window_rect.right - old_window_rect.left;
564 old_window_size.cy = old_window_rect.bottom - old_window_rect.top;
566 GetClientRect(hWnd, &old_client_rect);
567 old_client_size.cx = old_client_rect.right - old_client_rect.left;
568 old_client_size.cy = old_client_rect.bottom - old_client_rect.top;
570 new_client_size = old_client_size;
571 WINHELP_SplitLines(hWnd, &new_client_size);
573 if (origin.y + POPUP_YDISTANCE + new_client_size.cy <= GetSystemMetrics(SM_CYSCREEN))
574 origin.y += POPUP_YDISTANCE;
575 else
576 origin.y -= POPUP_YDISTANCE + new_client_size.cy;
578 new_window_size.cx = old_window_size.cx - old_client_size.cx + new_client_size.cx;
579 new_window_size.cy = old_window_size.cy - old_client_size.cy + new_client_size.cy;
581 win->hShadowWnd =
582 CreateWindow(SHADOW_WIN_CLASS_NAME, "", WS_POPUP | WS_VISIBLE,
583 origin.x + SHADOW_DX, origin.y + SHADOW_DY,
584 new_window_size.cx, new_window_size.cy,
585 0, 0, Globals.hInstance, 0);
587 SetWindowPos(hWnd, HWND_TOP, origin.x, origin.y,
588 new_window_size.cx, new_window_size.cy,
589 SWP_NOZORDER | SWP_NOACTIVATE);
590 ShowWindow(win->hShadowWnd, SW_NORMAL);
592 break;
594 case WM_WINDOWPOSCHANGED:
595 winpos = (WINDOWPOS*) lParam;
596 if (!(winpos->flags & SWP_NOSIZE)) WINHELP_SetupText(hWnd);
597 break;
599 case WM_VSCROLL:
601 BOOL update = TRUE;
602 RECT rect;
603 INT Min, Max;
604 INT CurPos = GetScrollPos(hWnd, SB_VERT);
605 GetScrollRange(hWnd, SB_VERT, &Min, &Max);
606 GetClientRect(hWnd, &rect);
608 switch (wParam & 0xffff)
610 case SB_THUMBTRACK:
611 case SB_THUMBPOSITION: CurPos = wParam >> 16; break;
612 case SB_TOP: CurPos = Min; break;
613 case SB_BOTTOM: CurPos = Max; break;
614 case SB_PAGEUP: CurPos -= (rect.bottom - rect.top) / 2; break;
615 case SB_PAGEDOWN: CurPos += (rect.bottom - rect.top) / 2; break;
616 case SB_LINEUP: CurPos -= GetSystemMetrics(SM_CXVSCROLL); break;
617 case SB_LINEDOWN: CurPos += GetSystemMetrics(SM_CXVSCROLL); break;
618 default: update = FALSE;
620 if (update)
622 INT dy = GetScrollPos(hWnd, SB_VERT) - CurPos;
623 SetScrollPos(hWnd, SB_VERT, CurPos, TRUE);
624 ScrollWindow(hWnd, 0, dy, NULL, NULL);
625 UpdateWindow(hWnd);
628 break;
630 case WM_PAINT:
631 hDc = BeginPaint (hWnd, &ps);
632 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
633 scroll_pos = GetScrollPos(hWnd, SB_VERT);
635 for (line = win->first_line; line; line = line->next)
636 for (part = &line->first_part; part; part = part->next)
638 SelectObject(hDc, part->hFont);
639 SetTextColor(hDc, part->color);
640 TextOut(hDc, part->rect.left, part->rect.top - scroll_pos,
641 (LPSTR) part->lpsText, part->wTextLen);
644 EndPaint (hWnd, &ps);
645 break;
647 case WM_LBUTTONDOWN:
648 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
649 scroll_pos = GetScrollPos(hWnd, SB_VERT);
651 hPopupWnd = Globals.hPopupWnd;
652 Globals.hPopupWnd = 0;
654 mouse.x = LOWORD(lParam);
655 mouse.y = HIWORD(lParam);
656 for (line = win->first_line; line; line = line->next)
657 for (part = &line->first_part; part; part = part->next)
658 if (part->link.lpszPath &&
659 part->rect.left <= mouse.x &&
660 part->rect.right >= mouse.x &&
661 part->rect.top <= mouse.y + scroll_pos &&
662 part->rect.bottom >= mouse.y + scroll_pos)
663 WINHELP_CreateHelpWindow(part->link.lpszPath, part->link.lHash, NULL,
664 part->link.bPopup, hWnd, &mouse, SW_NORMAL);
665 if (hPopupWnd)
666 DestroyWindow(hPopupWnd);
667 break;
669 case WM_NCDESTROY:
670 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
672 if (hWnd == Globals.hPopupWnd) Globals.hPopupWnd = 0;
674 bExit = (Globals.wVersion >= 4 && !lstrcmpi(win->lpszName, "main"));
676 WINHELP_DeleteWindow(win);
678 if (bExit) MACRO_Exit();
680 if (!Globals.win_list)
681 PostQuitMessage (0);
682 break;
685 return DefWindowProc (hWnd, msg, wParam, lParam);
688 /***********************************************************************
690 * SetupText
693 static VOID WINHELP_SetupText(HWND hWnd)
695 HDC hDc = GetDC(hWnd);
696 RECT rect;
697 SIZE newsize;
699 ShowScrollBar(hWnd, SB_VERT, FALSE);
700 if (!WINHELP_SplitLines(hWnd, NULL))
702 ShowScrollBar(hWnd, SB_VERT, TRUE);
703 GetClientRect(hWnd, &rect);
705 WINHELP_SplitLines(hWnd, &newsize);
706 SetScrollRange(hWnd, SB_VERT, 0, rect.top + newsize.cy - rect.bottom, TRUE);
708 else SetScrollPos(hWnd, SB_VERT, 0, FALSE);
710 ReleaseDC(hWnd, hDc);
713 /***********************************************************************
715 * WINHELP_SplitLines
718 static BOOL WINHELP_SplitLines(HWND hWnd, LPSIZE newsize)
720 WINHELP_WINDOW *win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
721 HLPFILE_PARAGRAPH *p;
722 WINHELP_LINE **line = &win->first_line;
723 WINHELP_LINE_PART **part = 0;
724 INT line_ascent = 0;
725 SIZE space;
726 RECT rect;
727 HDC hDc;
729 if (newsize) newsize->cx = newsize->cy = 0;
731 if (!win->page) return TRUE;
733 WINHELP_DeleteLines(win);
735 GetClientRect(hWnd, &rect);
737 rect.top += INTERNAL_BORDER_WIDTH;
738 rect.left += INTERNAL_BORDER_WIDTH;
739 rect.right -= INTERNAL_BORDER_WIDTH;
740 rect.bottom -= INTERNAL_BORDER_WIDTH;
743 space.cy = rect.top;
744 space.cx = rect.left;
746 hDc = GetDC(hWnd);
748 for (p = win->page->first_paragraph; p; p = p->next)
750 TEXTMETRIC tm;
751 SIZE textsize = {0, 0};
752 LPCSTR text = p->lpszText;
753 UINT len = strlen(text);
754 UINT indent = 0;
756 UINT wFont = (p->wFont < win->fonts_len) ? p->wFont : 0;
757 BOOL bUnderline = p->link && !p->link->bPopup;
758 HFONT hFont = win->fonts[wFont][bUnderline ? 1 : 0];
760 COLORREF color = RGB(0, 0, 0);
761 if (p->link) color = RGB(0, 0x80, 0);
762 if (p->bDebug) color = RGB(0xff, 0, 0);
764 SelectObject(hDc, hFont);
766 GetTextMetrics (hDc, &tm);
768 if (p->wIndent)
770 indent = p->wIndent * 5 * tm.tmAveCharWidth;
771 if (!part)
772 space.cx = rect.left + indent - 2 * tm.tmAveCharWidth;
775 if (p->wVSpace)
777 part = 0;
778 space.cx = rect.left + indent;
779 space.cy += (p->wVSpace - 1) * tm.tmHeight;
782 if (p->wHSpace)
784 space.cx += p->wHSpace * 2 * tm.tmAveCharWidth;
787 while (len)
789 INT free_width = rect.right - (part ? (*line)->rect.right : rect.left) - space.cx;
790 UINT low = 0, curr = len, high = len, textlen = 0;
792 if (free_width > 0)
794 while (1)
796 GetTextExtentPoint(hDc, text, curr, &textsize);
798 if (textsize.cx <= free_width) low = curr;
799 else high = curr;
801 if (high <= low + 1) break;
803 if (textsize.cx) curr = (curr * free_width) / textsize.cx;
804 if (curr <= low) curr = low + 1;
805 else if (curr >= high) curr = high - 1;
807 textlen = low;
808 while (textlen && text[textlen] && text[textlen] != ' ') textlen--;
810 if (!part && !textlen) textlen = MAX(low, 1);
812 if (free_width <= 0 || !textlen)
814 part = 0;
815 space.cx = rect.left + indent;
816 space.cx = MIN(space.cx, rect.right - rect.left - 1);
817 continue;
820 if (!WINHELP_AppendText(&line, &part, &space, &textsize,
821 &line_ascent, tm.tmAscent,
822 text, textlen, hFont, color, p->link) ||
823 (!newsize && (*line)->rect.bottom > rect.bottom))
825 ReleaseDC(hWnd, hDc);
826 return FALSE;
829 if (newsize)
830 newsize->cx = MAX(newsize->cx, (*line)->rect.right + INTERNAL_BORDER_WIDTH);
832 len -= textlen;
833 text += textlen;
834 if (text[0] == ' ') text++, len--;
838 if (newsize)
839 newsize->cy = (*line)->rect.bottom + INTERNAL_BORDER_WIDTH;
841 ReleaseDC(hWnd, hDc);
842 return TRUE;
845 /***********************************************************************
847 * WINHELP_AppendText
850 static BOOL WINHELP_AppendText(WINHELP_LINE ***linep, WINHELP_LINE_PART ***partp,
851 LPSIZE space, LPSIZE textsize,
852 INT *line_ascent, INT ascent,
853 LPCSTR text, UINT textlen,
854 HFONT font, COLORREF color, HLPFILE_LINK *link)
856 HGLOBAL handle;
857 WINHELP_LINE *line;
858 WINHELP_LINE_PART *part;
859 LPSTR ptr;
861 if (!*partp) /* New line */
863 *line_ascent = ascent;
865 handle = GlobalAlloc(GMEM_FIXED, sizeof(WINHELP_LINE) + textlen +
866 (link ? lstrlen(link->lpszPath) + 1 : 0));
867 if (!handle) return FALSE;
868 line = GlobalLock(handle);
869 line->next = 0;
870 part = &line->first_part;
871 ptr = GlobalLock(handle);
872 ptr += sizeof(WINHELP_LINE);
874 line->rect.top = (**linep ? (**linep)->rect.bottom : 0) + space->cy;
875 line->rect.bottom = line->rect.top;
876 line->rect.left = space->cx;
877 line->rect.right = space->cx;
879 if (**linep) *linep = &(**linep)->next;
880 **linep = line;
881 space->cy = 0;
883 else /* Same line */
885 line = **linep;
887 if (*line_ascent < ascent)
889 WINHELP_LINE_PART *p;
890 for (p = &line->first_part; p; p = p->next)
892 p->rect.top += ascent - *line_ascent;
893 p->rect.bottom += ascent - *line_ascent;
895 line->rect.bottom += ascent - *line_ascent;
896 *line_ascent = ascent;
899 handle = GlobalAlloc(GMEM_FIXED, sizeof(WINHELP_LINE_PART) + textlen +
900 (link ? lstrlen(link->lpszPath) + 1 : 0));
901 if (!handle) return FALSE;
902 part = GlobalLock(handle);
903 **partp = part;
904 ptr = GlobalLock(handle);
905 ptr += sizeof(WINHELP_LINE_PART);
908 hmemcpy(ptr, text, textlen);
909 part->rect.left = line->rect.right + (*partp ? space->cx : 0);
910 part->rect.right = part->rect.left + textsize->cx;
911 line->rect.right = part->rect.right;
912 part->rect.top =
913 ((*partp) ? line->rect.top : line->rect.bottom) + *line_ascent - ascent;
914 part->rect.bottom = part->rect.top + textsize->cy;
915 line->rect.bottom = MAX(line->rect.bottom, part->rect.bottom);
916 part->hSelf = handle;
917 part->lpsText = ptr;
918 part->wTextLen = textlen;
919 part->hFont = font;
920 part->color = color;
921 if (link)
923 strcpy(ptr + textlen, link->lpszPath);
924 part->link.lpszPath = ptr + textlen;
925 part->link.lHash = link->lHash;
926 part->link.bPopup = link->bPopup;
928 else part->link.lpszPath = 0;
930 part->next = 0;
931 *partp = &part->next;
933 space->cx = 0;
935 return TRUE;
938 /***********************************************************************
940 * WINHELP_CheckPopup
943 static VOID WINHELP_CheckPopup(UINT msg)
945 if (!Globals.hPopupWnd) return;
947 switch (msg)
949 case WM_COMMAND:
950 case WM_LBUTTONDOWN:
951 case WM_MBUTTONDOWN:
952 case WM_RBUTTONDOWN:
953 case WM_NCLBUTTONDOWN:
954 case WM_NCMBUTTONDOWN:
955 case WM_NCRBUTTONDOWN:
956 DestroyWindow(Globals.hPopupWnd);
957 Globals.hPopupWnd = 0;
961 /***********************************************************************
963 * WINHELP_DeleteLines
966 static VOID WINHELP_DeleteLines(WINHELP_WINDOW *win)
968 WINHELP_LINE *line, *next_line;
969 WINHELP_LINE_PART *part, *next_part;
970 for(line = win->first_line; line; line = next_line)
972 next_line = line->next;
973 for(part = &line->first_part; part; part = next_part)
975 next_part = part->next;
976 GlobalFree(part->hSelf);
979 win->first_line = 0;
982 /***********************************************************************
984 * WINHELP_DeleteWindow
987 static VOID WINHELP_DeleteWindow(WINHELP_WINDOW *win)
989 WINHELP_WINDOW **w;
991 for (w = &Globals.win_list; *w; w = &(*w)->next)
992 if (*w == win)
994 *w = win->next;
995 break;
998 if (win->hShadowWnd) DestroyWindow(win->hShadowWnd);
999 HLPFILE_FreeHlpFilePage(win->page);
1000 WINHELP_DeleteLines(win);
1001 GlobalFree(win->hSelf);
1004 /***********************************************************************
1006 * WINHELP_InitFonts
1009 static VOID WINHELP_InitFonts(HWND hWnd)
1011 WINHELP_WINDOW *win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1012 LOGFONT logfontlist[] = {
1013 {-10, 0, 0, 0, 400, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1014 {-12, 0, 0, 0, 700, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1015 {-12, 0, 0, 0, 700, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1016 {-12, 0, 0, 0, 400, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1017 {-12, 0, 0, 0, 700, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1018 {-10, 0, 0, 0, 700, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1019 { -8, 0, 0, 0, 400, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"}};
1020 #define FONTS_LEN (sizeof(logfontlist)/sizeof(*logfontlist))
1022 static HFONT fonts[FONTS_LEN][2];
1023 static BOOL init = 0;
1025 win->fonts_len = FONTS_LEN;
1026 win->fonts = fonts;
1028 if (!init)
1030 INT i;
1032 for(i = 0; i < FONTS_LEN; i++)
1034 LOGFONT logfont = logfontlist[i];
1036 fonts[i][0] = CreateFontIndirect(&logfont);
1037 logfont.lfUnderline = 1;
1038 fonts[i][1] = CreateFontIndirect(&logfont);
1041 init = 1;
1045 /***********************************************************************
1047 * WINHELP_MessageBoxIDS
1050 INT WINHELP_MessageBoxIDS(UINT ids_text, UINT ids_title, WORD type)
1052 CHAR text[MAX_STRING_LEN];
1053 CHAR title[MAX_STRING_LEN];
1055 LoadString(Globals.hInstance, ids_text, text, sizeof(text));
1056 LoadString(Globals.hInstance, ids_title, title, sizeof(title));
1058 return(MessageBox(0, text, title, type));
1061 /***********************************************************************
1063 * MAIN_MessageBoxIDS_s
1066 INT WINHELP_MessageBoxIDS_s(UINT ids_text, LPCSTR str, UINT ids_title, WORD type)
1068 CHAR text[MAX_STRING_LEN];
1069 CHAR title[MAX_STRING_LEN];
1070 CHAR newtext[MAX_STRING_LEN + MAX_PATHNAME_LEN];
1072 LoadString(Globals.hInstance, ids_text, text, sizeof(text));
1073 LoadString(Globals.hInstance, ids_title, title, sizeof(title));
1074 wsprintf(newtext, text, str);
1076 return(MessageBox(0, newtext, title, type));
1079 /* Local Variables: */
1080 /* c-file-style: "GNU" */
1081 /* End: */