Release 971012
[wine.git] / programs / winhelp / winhelp.c
blob8b53055318985e998bc7d436169a77a91be883e6
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);
24 VOID LIBWINE_Register_Va(void);
26 static BOOL WINHELP_RegisterWinClasses();
27 static LRESULT WINHELP_MainWndProc(HWND, UINT, WPARAM, LPARAM);
28 static LRESULT WINHELP_TextWndProc(HWND, UINT, WPARAM, LPARAM);
29 static LRESULT WINHELP_ButtonBoxWndProc(HWND, UINT, WPARAM, LPARAM);
30 static VOID WINHELP_CheckPopup(UINT);
31 static BOOL WINHELP_SplitLines(HWND hWnd, LPSIZE);
32 static VOID WINHELP_InitFonts(HWND hWnd);
33 static VOID WINHELP_DeleteLines(WINHELP_WINDOW*);
34 static VOID WINHELP_DeleteWindow(WINHELP_WINDOW*);
35 static VOID WINHELP_SetupText(HWND hWnd);
36 static BOOL WINHELP_AppendText(WINHELP_LINE***, WINHELP_LINE_PART***,
37 LPSIZE, LPSIZE, INT*, INT, LPCSTR, UINT,
38 HFONT, COLORREF, HLPFILE_LINK*);
40 WINHELP_GLOBALS Globals = {3, 0, 0, 0, 0, 0};
42 static BOOL MacroTest = FALSE;
44 /***********************************************************************
46 * WinMain
49 int PASCAL WinMain (HANDLE hInstance, HANDLE prev, LPSTR cmdline, int show)
51 LPCSTR opt_lang = "En";
52 CHAR lang[3];
53 MSG msg;
54 LONG lHash = 0;
55 INT langnum;
57 #if defined(WINELIB) && !defined(HAVE_WINE_CONSTRUCTOR)
58 /* Register resources */
59 LIBWINE_Register_De();
60 LIBWINE_Register_En();
61 LIBWINE_Register_Fi();
62 LIBWINE_Register_Fr();
63 LIBWINE_Register_It();
64 LIBWINE_Register_Ko();
65 LIBWINE_Register_Hu();
66 LIBWINE_Register_Va();
67 #endif
69 Globals.hInstance = hInstance;
71 /* Get options */
72 while (*cmdline && (*cmdline == ' ' || *cmdline == '-'))
74 CHAR option;
75 LPCSTR topic_id;
76 if (*cmdline++ == ' ') continue;
78 option = *cmdline;
79 if (option) cmdline++;
80 while (*cmdline && *cmdline == ' ') cmdline++;
81 switch(option)
83 case 'i':
84 case 'I':
85 topic_id = cmdline;
86 while (*cmdline && *cmdline != ' ') cmdline++;
87 if (*cmdline) *cmdline++ = '\0';
88 lHash = HLPFILE_Hash(topic_id);
89 break;
91 case '3':
92 case '4':
93 Globals.wVersion = option - '0';
94 break;
96 case 't':
97 MacroTest = TRUE;
98 break;
102 #ifdef WINELIB
103 opt_lang = Languages[Options.language].name;
104 #endif
106 /* Find language specific string table */
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 !lstrcmp(opt_lang, lang))
112 break;
114 if (langnum > MAX_LANGUAGE_NUMBER)
116 /* Find fallback language */
117 for (langnum = 0; langnum <= MAX_LANGUAGE_NUMBER; langnum++)
119 Globals.wStringTableOffset = langnum * 0x100;
120 if (LoadString(hInstance, IDS_LANGUAGE_ID, lang, sizeof(lang)))
121 break;
123 if (langnum > MAX_LANGUAGE_NUMBER)
125 MessageBox(0, "No language found", "FATAL ERROR", MB_OK);
126 return(1);
130 /* Change Resource names */
131 lstrcpyn(STRING_MENU_Xx + lstrlen(STRING_MENU_Xx) - 2, lang, 3);
133 /* Create primary window */
134 WINHELP_RegisterWinClasses();
135 WINHELP_CreateHelpWindow(cmdline, lHash, "main", FALSE, NULL, NULL, show);
137 /* Message loop */
138 while (GetMessage (&msg, 0, 0, 0))
140 TranslateMessage (&msg);
141 DispatchMessage (&msg);
143 return 0;
146 /***********************************************************************
148 * RegisterWinClasses
151 static BOOL WINHELP_RegisterWinClasses()
153 WNDCLASS class_main, class_button_box, class_text, class_shadow;
155 class_main.style = CS_HREDRAW | CS_VREDRAW;
156 class_main.lpfnWndProc = WINHELP_MainWndProc;
157 class_main.cbClsExtra = 0;
158 class_main.cbWndExtra = sizeof(LONG);
159 class_main.hInstance = Globals.hInstance;
160 class_main.hIcon = LoadIcon (0, IDI_APPLICATION);
161 class_main.hCursor = LoadCursor (0, IDC_ARROW);
162 class_main.hbrBackground = GetStockObject (WHITE_BRUSH);
163 class_main.lpszMenuName = 0;
164 class_main.lpszClassName = MAIN_WIN_CLASS_NAME;
166 class_button_box = class_main;
167 class_button_box.lpfnWndProc = WINHELP_ButtonBoxWndProc;
168 class_button_box.hbrBackground = GetStockObject(GRAY_BRUSH);
169 class_button_box.lpszClassName = BUTTON_BOX_WIN_CLASS_NAME;
171 class_text = class_main;
172 class_text.lpfnWndProc = WINHELP_TextWndProc;
173 class_text.lpszClassName = TEXT_WIN_CLASS_NAME;
175 class_shadow = class_main;
176 class_shadow.lpfnWndProc = DefWindowProc;
177 class_shadow.hbrBackground = GetStockObject(GRAY_BRUSH);
178 class_shadow.lpszClassName = SHADOW_WIN_CLASS_NAME;
180 return (RegisterClass(&class_main) &&
181 RegisterClass(&class_button_box) &&
182 RegisterClass(&class_text) &&
183 RegisterClass(&class_shadow));
186 /***********************************************************************
188 * WINHELP_CreateHelpWindow
191 VOID WINHELP_CreateHelpWindow(LPCSTR lpszFile, LONG lHash, LPCSTR lpszWindow,
192 BOOL bPopup, HWND hParentWnd, LPPOINT mouse, INT nCmdShow)
194 CHAR szCaption[MAX_STRING_LEN];
195 CHAR szContents[MAX_STRING_LEN];
196 CHAR szSearch[MAX_STRING_LEN];
197 CHAR szBack[MAX_STRING_LEN];
198 CHAR szHistory[MAX_STRING_LEN];
199 SIZE size = {CW_USEDEFAULT, CW_USEDEFAULT};
200 POINT origin = {240, 0};
201 LPSTR ptr;
202 HGLOBAL handle;
203 WINHELP_WINDOW *win, *oldwin;
204 HLPFILE_PAGE *page;
205 HLPFILE_MACRO *macro;
206 HWND hWnd;
207 BOOL bPrimary;
209 if (bPopup)
210 lpszWindow = NULL;
211 else if (!lpszWindow || !lpszWindow[0])
212 lpszWindow = Globals.active_win->lpszName;
213 bPrimary = lpszWindow && !lstrcmpi(lpszWindow, "main");
215 /* Read help file */
216 if (lpszFile[0])
218 page = lHash ? HLPFILE_PageByHash(lpszFile, lHash) : HLPFILE_Contents(lpszFile);
220 /* Add Suffix `.hlp' */
221 if (!page && lstrcmpi(lpszFile + strlen(lpszFile) - 4, ".hlp"))
223 CHAR szFile_hlp[MAX_PATHNAME_LEN];
225 lstrcpyn(szFile_hlp, lpszFile, sizeof(szFile_hlp) - 4);
226 szFile_hlp[sizeof(szFile_hlp) - 5] = '\0';
227 lstrcat(szFile_hlp, ".hlp");
229 page = lHash ? HLPFILE_PageByHash(szFile_hlp, lHash) : HLPFILE_Contents(szFile_hlp);
230 if (!page)
232 WINHELP_MessageBoxIDS_s(IDS_HLPFILE_ERROR_s, lpszFile, IDS_ERROR, MB_OK);
233 if (Globals.win_list) return;
237 else page = 0;
239 /* Calculate horizontal size and position of a popup window */
240 if (bPopup)
242 RECT parent_rect;
243 GetWindowRect(hParentWnd, &parent_rect);
244 size.cx = (parent_rect.right - parent_rect.left) / 2;
246 origin = *mouse;
247 ClientToScreen(hParentWnd, &origin);
248 origin.x -= size.cx / 2;
249 origin.x = MIN(origin.x, GetSystemMetrics(SM_CXSCREEN) - size.cx);
250 origin.x = MAX(origin.x, 0);
253 /* Initialize WINHELP_WINDOW struct */
254 handle = GlobalAlloc(GMEM_FIXED, sizeof(WINHELP_WINDOW) +
255 (lpszWindow ? strlen(lpszWindow) + 1 : 0));
256 if (!handle) return;
257 win = GlobalLock(handle);
258 win->hSelf = handle;
259 win->next = Globals.win_list;
260 Globals.win_list = win;
261 if (lpszWindow)
263 ptr = GlobalLock(handle);
264 ptr += sizeof(WINHELP_WINDOW);
265 lstrcpy(ptr, (LPSTR) lpszWindow);
266 win->lpszName = ptr;
268 else win->lpszName = NULL;
269 win->page = page;
270 win->first_button = 0;
271 win->first_line = 0;
272 win->hMainWnd = 0;
273 win->hButtonBoxWnd = 0;
274 win->hTextWnd = 0;
275 win->hShadowWnd = 0;
277 Globals.active_win = win;
279 /* Initialize default pushbuttons */
280 if (MacroTest && !bPopup)
281 MACRO_CreateButton("BTN_TEST", "&Test", "MacroTest");
282 if (bPrimary && page)
284 LoadString(Globals.hInstance, IDS_CONTENTS, szContents, sizeof(szContents));
285 LoadString(Globals.hInstance, IDS_SEARCH, szSearch, sizeof(szSearch));
286 LoadString(Globals.hInstance, IDS_BACK, szBack, sizeof(szBack));
287 LoadString(Globals.hInstance, IDS_HISTORY, szHistory, sizeof(szHistory));
288 MACRO_CreateButton("BTN_CONTENTS", szContents, "Contents()");
289 MACRO_CreateButton("BTN_SEARCH", szSearch, "Search()");
290 MACRO_CreateButton("BTN_BACK", szBack, "Back()");
291 MACRO_CreateButton("BTN_HISTORY", szHistory, "History()");
294 /* Initialize file specific pushbuttons */
295 if (!bPopup && page)
296 for (macro = page->file->first_macro; macro; macro = macro->next)
297 MACRO_ExecuteMacro(macro->lpszMacro);
299 /* Reuse existing window */
300 if (lpszWindow)
301 for (oldwin = win->next; oldwin; oldwin = oldwin->next)
302 if (oldwin->lpszName && !lstrcmpi(oldwin->lpszName, lpszWindow))
304 WINHELP_BUTTON *button;
306 win->hMainWnd = oldwin->hMainWnd;
307 win->hButtonBoxWnd = oldwin->hButtonBoxWnd;
308 win->hTextWnd = oldwin->hTextWnd;
309 oldwin->hMainWnd = oldwin->hButtonBoxWnd = oldwin->hTextWnd = 0;
311 SetWindowLong(win->hMainWnd, 0, (LONG) win);
312 SetWindowLong(win->hButtonBoxWnd, 0, (LONG) win);
313 SetWindowLong(win->hTextWnd, 0, (LONG) win);
315 WINHELP_InitFonts(win->hMainWnd);
317 if (page) {
318 SetWindowText(win->hMainWnd, page->file->lpszTitle);
321 WINHELP_SetupText(win->hTextWnd);
322 InvalidateRect(win->hTextWnd, NULL, TRUE);
323 SendMessage(win->hMainWnd, WM_USER, 0, 0);
324 UpdateWindow(win->hTextWnd);
327 for (button = oldwin->first_button; button; button = button->next)
328 DestroyWindow(button->hWnd);
330 WINHELP_DeleteWindow(oldwin);
331 return;
334 /* Create main Window */
335 if (!page) LoadString(Globals.hInstance, IDS_WINE_HELP, szCaption, sizeof(szCaption));
336 hWnd = CreateWindow (bPopup ? TEXT_WIN_CLASS_NAME : MAIN_WIN_CLASS_NAME,
337 page ? page->file->lpszTitle : szCaption,
338 bPopup ? WS_POPUPWINDOW | WS_BORDER : WS_OVERLAPPEDWINDOW,
339 origin.x, origin.y, size.cx, size.cy,
340 0, bPrimary ? LoadMenu(Globals.hInstance, STRING_MENU_Xx) : 0,
341 Globals.hInstance, win);
343 ShowWindow (hWnd, nCmdShow);
344 UpdateWindow (hWnd);
347 /***********************************************************************
349 * WINHELP_MainWndProc
352 static LRESULT WINHELP_MainWndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
354 WINHELP_WINDOW *win;
355 WINHELP_BUTTON *button;
356 RECT rect, button_box_rect;
357 INT text_top;
359 WINHELP_CheckPopup(msg);
361 switch (msg)
363 case WM_NCCREATE:
364 win = (WINHELP_WINDOW*) ((LPCREATESTRUCT) lParam)->lpCreateParams;
365 SetWindowLong(hWnd, 0, (LONG) win);
366 win->hMainWnd = hWnd;
367 break;
369 case WM_CREATE:
370 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
372 /* Create button box and text Window */
373 CreateWindow(BUTTON_BOX_WIN_CLASS_NAME, "", WS_CHILD | WS_VISIBLE,
374 0, 0, 0, 0, hWnd, 0, Globals.hInstance, win);
376 CreateWindow(TEXT_WIN_CLASS_NAME, "", WS_CHILD | WS_VISIBLE,
377 0, 0, 0, 0, hWnd, 0, Globals.hInstance, win);
379 /* Fall through */
380 case WM_USER:
381 case WM_WINDOWPOSCHANGED:
382 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
383 GetClientRect(hWnd, &rect);
385 /* Update button box and text Window */
386 SetWindowPos(win->hButtonBoxWnd, HWND_TOP,
387 rect.left, rect.top,
388 rect.right - rect.left,
389 rect.bottom - rect.top, 0);
391 GetWindowRect(win->hButtonBoxWnd, &button_box_rect);
392 text_top = rect.top + button_box_rect.bottom - button_box_rect.top;
394 SetWindowPos(win->hTextWnd, HWND_TOP,
395 rect.left, text_top,
396 rect.right - rect.left,
397 rect.bottom - text_top, 0);
399 break;
401 case WM_COMMAND:
402 Globals.active_win = win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
403 switch (wParam)
405 /* Menu FILE */
406 case WH_OPEN: MACRO_FileOpen(); break;
407 case WH_PRINT: MACRO_Print(); break;
408 case WH_PRINTER_SETUP: MACRO_PrinterSetup(); break;
409 case WH_EXIT: MACRO_Exit(); break;
411 /* Menu EDIT */
412 case WH_COPY_DIALOG: MACRO_CopyDialog(); break;
413 case WH_ANNOTATE: MACRO_Annotate(); break;
415 /* Menu Bookmark */
416 case WH_BOOKMARK_DEFINE: MACRO_BookmarkDefine(); break;
418 /* Menu Help */
419 case WH_HELP_ON_HELP: MACRO_HelpOn(); break;
420 case WH_HELP_ON_TOP: MACRO_HelpOnTop(); break;
422 /* Menu Info */
423 case WH_ABOUT: MACRO_About(); break;
424 #ifdef WINELIB
425 case WH_ABOUT_WINE:
426 ShellAbout(hWnd, "WINE", people, 0);
427 break;
428 #endif
430 default:
431 /* Buttons */
432 for (button = win->first_button; button; button = button->next)
433 if (wParam == button->wParam) break;
434 if (button)
435 MACRO_ExecuteMacro(button->lpszMacro);
436 else
437 WINHELP_MessageBoxIDS(IDS_NOT_IMPLEMENTED, IDS_ERROR, MB_OK);
438 break;
440 break;
443 return DefWindowProc (hWnd, msg, wParam, lParam);
446 /***********************************************************************
448 * WINHELP_ButtonBoxWndProc
451 static LRESULT WINHELP_ButtonBoxWndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
453 WINDOWPOS *winpos;
454 WINHELP_WINDOW *win;
455 WINHELP_BUTTON *button;
456 SIZE button_size;
457 INT x, y;
459 WINHELP_CheckPopup(msg);
461 switch(msg)
463 case WM_NCCREATE:
464 win = (WINHELP_WINDOW*) ((LPCREATESTRUCT) lParam)->lpCreateParams;
465 SetWindowLong(hWnd, 0, (LONG) win);
466 win->hButtonBoxWnd = hWnd;
467 break;
469 case WM_WINDOWPOSCHANGING:
470 winpos = (WINDOWPOS*) lParam;
471 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
473 /* Update buttons */
474 button_size.cx = 0;
475 button_size.cy = 0;
476 for (button = win->first_button; button; button = button->next)
478 HDC hDc;
479 SIZE textsize;
480 if (!button->hWnd)
481 button->hWnd = CreateWindow(STRING_BUTTON, (LPSTR) button->lpszName,
482 WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
483 0, 0, 0, 0,
484 hWnd, (HMENU) button->wParam,
485 Globals.hInstance, 0);
486 hDc = GetDC(button->hWnd);
487 GetTextExtentPoint(hDc, button->lpszName,
488 lstrlen(button->lpszName), &textsize);
489 ReleaseDC(button->hWnd, hDc);
491 button_size.cx = MAX(button_size.cx, textsize.cx + BUTTON_CX);
492 button_size.cy = MAX(button_size.cy, textsize.cy + BUTTON_CY);
495 x = 0;
496 y = 0;
497 for (button = win->first_button; button; button = button->next)
499 SetWindowPos(button->hWnd, HWND_TOP, x, y, button_size.cx, button_size.cy, 0);
501 if (x + 2 * button_size.cx <= winpos->cx)
502 x += button_size.cx;
503 else
504 x = 0, y += button_size.cy;
506 winpos->cy = y + (x ? button_size.cy : 0);
507 break;
509 case WM_COMMAND:
510 SendMessage(GetParent(hWnd), msg, wParam, lParam);
511 break;
514 return(DefWindowProc(hWnd, msg, wParam, lParam));
517 /***********************************************************************
519 * WINHELP_TextWndProc
522 static LRESULT WINHELP_TextWndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
524 WINHELP_WINDOW *win;
525 WINHELP_LINE *line;
526 WINHELP_LINE_PART *part;
527 WINDOWPOS *winpos;
528 PAINTSTRUCT ps;
529 HDC hDc;
530 POINT mouse;
531 INT scroll_pos;
532 HWND hPopupWnd;
533 BOOL bExit;
535 if (msg != WM_LBUTTONDOWN)
536 WINHELP_CheckPopup(msg);
538 switch (msg)
540 case WM_NCCREATE:
541 win = (WINHELP_WINDOW*) ((LPCREATESTRUCT) lParam)->lpCreateParams;
542 SetWindowLong(hWnd, 0, (LONG) win);
543 win->hTextWnd = hWnd;
544 if (!win->lpszName) Globals.hPopupWnd = win->hMainWnd = hWnd;
545 WINHELP_InitFonts(hWnd);
546 break;
548 case WM_CREATE:
549 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
551 /* Calculate vertical size and position of a popup window */
552 if (!win->lpszName)
554 POINT origin;
555 RECT old_window_rect;
556 RECT old_client_rect;
557 SIZE old_window_size;
558 SIZE old_client_size;
559 SIZE new_client_size;
560 SIZE new_window_size;
562 GetWindowRect(hWnd, &old_window_rect);
563 origin.x = old_window_rect.left;
564 origin.y = old_window_rect.top;
565 old_window_size.cx = old_window_rect.right - old_window_rect.left;
566 old_window_size.cy = old_window_rect.bottom - old_window_rect.top;
568 GetClientRect(hWnd, &old_client_rect);
569 old_client_size.cx = old_client_rect.right - old_client_rect.left;
570 old_client_size.cy = old_client_rect.bottom - old_client_rect.top;
572 new_client_size = old_client_size;
573 WINHELP_SplitLines(hWnd, &new_client_size);
575 if (origin.y + POPUP_YDISTANCE + new_client_size.cy <= GetSystemMetrics(SM_CYSCREEN))
576 origin.y += POPUP_YDISTANCE;
577 else
578 origin.y -= POPUP_YDISTANCE + new_client_size.cy;
580 new_window_size.cx = old_window_size.cx - old_client_size.cx + new_client_size.cx;
581 new_window_size.cy = old_window_size.cy - old_client_size.cy + new_client_size.cy;
583 win->hShadowWnd =
584 CreateWindow(SHADOW_WIN_CLASS_NAME, "", WS_POPUP | WS_VISIBLE,
585 origin.x + SHADOW_DX, origin.y + SHADOW_DY,
586 new_window_size.cx, new_window_size.cy,
587 0, 0, Globals.hInstance, 0);
589 SetWindowPos(hWnd, HWND_TOP, origin.x, origin.y,
590 new_window_size.cx, new_window_size.cy,
591 SWP_NOZORDER | SWP_NOACTIVATE);
592 ShowWindow(win->hShadowWnd, SW_NORMAL);
594 break;
596 case WM_WINDOWPOSCHANGED:
597 winpos = (WINDOWPOS*) lParam;
598 if (!(winpos->flags & SWP_NOSIZE)) WINHELP_SetupText(hWnd);
599 break;
601 case WM_VSCROLL:
603 BOOL update = TRUE;
604 RECT rect;
605 INT Min, Max;
606 INT CurPos = GetScrollPos(hWnd, SB_VERT);
607 GetScrollRange(hWnd, SB_VERT, &Min, &Max);
608 GetClientRect(hWnd, &rect);
610 switch (wParam & 0xffff)
612 case SB_THUMBTRACK:
613 case SB_THUMBPOSITION: CurPos = wParam >> 16; break;
614 case SB_TOP: CurPos = Min; break;
615 case SB_BOTTOM: CurPos = Max; break;
616 case SB_PAGEUP: CurPos -= (rect.bottom - rect.top) / 2; break;
617 case SB_PAGEDOWN: CurPos += (rect.bottom - rect.top) / 2; break;
618 case SB_LINEUP: CurPos -= GetSystemMetrics(SM_CXVSCROLL); break;
619 case SB_LINEDOWN: CurPos += GetSystemMetrics(SM_CXVSCROLL); break;
620 default: update = FALSE;
622 if (update)
624 INT dy = GetScrollPos(hWnd, SB_VERT) - CurPos;
625 SetScrollPos(hWnd, SB_VERT, CurPos, TRUE);
626 ScrollWindow(hWnd, 0, dy, NULL, NULL);
627 UpdateWindow(hWnd);
630 break;
632 case WM_PAINT:
633 hDc = BeginPaint (hWnd, &ps);
634 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
635 scroll_pos = GetScrollPos(hWnd, SB_VERT);
637 for (line = win->first_line; line; line = line->next)
638 for (part = &line->first_part; part; part = part->next)
640 SelectObject(hDc, part->hFont);
641 SetTextColor(hDc, part->color);
642 TextOut(hDc, part->rect.left, part->rect.top - scroll_pos,
643 (LPSTR) part->lpsText, part->wTextLen);
646 EndPaint (hWnd, &ps);
647 break;
649 case WM_LBUTTONDOWN:
650 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
651 scroll_pos = GetScrollPos(hWnd, SB_VERT);
653 hPopupWnd = Globals.hPopupWnd;
654 Globals.hPopupWnd = 0;
656 mouse.x = LOWORD(lParam);
657 mouse.y = HIWORD(lParam);
658 for (line = win->first_line; line; line = line->next)
659 for (part = &line->first_part; part; part = part->next)
660 if (part->link.lpszPath &&
661 part->rect.left <= mouse.x &&
662 part->rect.right >= mouse.x &&
663 part->rect.top <= mouse.y + scroll_pos &&
664 part->rect.bottom >= mouse.y + scroll_pos)
665 WINHELP_CreateHelpWindow(part->link.lpszPath, part->link.lHash, NULL,
666 part->link.bPopup, hWnd, &mouse, SW_NORMAL);
667 if (hPopupWnd)
668 DestroyWindow(hPopupWnd);
669 break;
671 case WM_NCDESTROY:
672 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
674 if (hWnd == Globals.hPopupWnd) Globals.hPopupWnd = 0;
676 bExit = (Globals.wVersion >= 4 && !lstrcmpi(win->lpszName, "main"));
678 WINHELP_DeleteWindow(win);
680 if (bExit) MACRO_Exit();
682 if (!Globals.win_list)
683 PostQuitMessage (0);
684 break;
687 return DefWindowProc (hWnd, msg, wParam, lParam);
690 /***********************************************************************
692 * SetupText
695 static VOID WINHELP_SetupText(HWND hWnd)
697 HDC hDc = GetDC(hWnd);
698 RECT rect;
699 SIZE newsize;
701 ShowScrollBar(hWnd, SB_VERT, FALSE);
702 if (!WINHELP_SplitLines(hWnd, NULL))
704 ShowScrollBar(hWnd, SB_VERT, TRUE);
705 GetClientRect(hWnd, &rect);
707 WINHELP_SplitLines(hWnd, &newsize);
708 SetScrollRange(hWnd, SB_VERT, 0, rect.top + newsize.cy - rect.bottom, TRUE);
710 else SetScrollPos(hWnd, SB_VERT, 0, FALSE);
712 ReleaseDC(hWnd, hDc);
715 /***********************************************************************
717 * WINHELP_SplitLines
720 static BOOL WINHELP_SplitLines(HWND hWnd, LPSIZE newsize)
722 WINHELP_WINDOW *win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
723 HLPFILE_PARAGRAPH *p;
724 WINHELP_LINE **line = &win->first_line;
725 WINHELP_LINE_PART **part = 0;
726 INT line_ascent = 0;
727 SIZE space;
728 RECT rect;
729 HDC hDc;
731 if (newsize) newsize->cx = newsize->cy = 0;
733 if (!win->page) return TRUE;
735 WINHELP_DeleteLines(win);
737 GetClientRect(hWnd, &rect);
739 rect.top += INTERNAL_BORDER_WIDTH;
740 rect.left += INTERNAL_BORDER_WIDTH;
741 rect.right -= INTERNAL_BORDER_WIDTH;
742 rect.bottom -= INTERNAL_BORDER_WIDTH;
745 space.cy = rect.top;
746 space.cx = rect.left;
748 hDc = GetDC(hWnd);
750 for (p = win->page->first_paragraph; p; p = p->next)
752 TEXTMETRIC tm;
753 SIZE textsize = {0, 0};
754 LPCSTR text = p->lpszText;
755 UINT len = strlen(text);
756 UINT indent = 0;
758 UINT wFont = (p->wFont < win->fonts_len) ? p->wFont : 0;
759 BOOL bUnderline = p->link && !p->link->bPopup;
760 HFONT hFont = win->fonts[wFont][bUnderline ? 1 : 0];
762 COLORREF color = RGB(0, 0, 0);
763 if (p->link) color = RGB(0, 0x80, 0);
764 if (p->bDebug) color = RGB(0xff, 0, 0);
766 SelectObject(hDc, hFont);
768 GetTextMetrics (hDc, &tm);
770 if (p->wIndent)
772 indent = p->wIndent * 5 * tm.tmAveCharWidth;
773 if (!part)
774 space.cx = rect.left + indent - 2 * tm.tmAveCharWidth;
777 if (p->wVSpace)
779 part = 0;
780 space.cx = rect.left + indent;
781 space.cy += (p->wVSpace - 1) * tm.tmHeight;
784 if (p->wHSpace)
786 space.cx += p->wHSpace * 2 * tm.tmAveCharWidth;
789 while (len)
791 INT free_width = rect.right - (part ? (*line)->rect.right : rect.left) - space.cx;
792 UINT low = 0, curr = len, high = len, textlen = 0;
794 if (free_width > 0)
796 while (1)
798 GetTextExtentPoint(hDc, text, curr, &textsize);
800 if (textsize.cx <= free_width) low = curr;
801 else high = curr;
803 if (high <= low + 1) break;
805 if (textsize.cx) curr = (curr * free_width) / textsize.cx;
806 if (curr <= low) curr = low + 1;
807 else if (curr >= high) curr = high - 1;
809 textlen = low;
810 while (textlen && text[textlen] && text[textlen] != ' ') textlen--;
812 if (!part && !textlen) textlen = MAX(low, 1);
814 if (free_width <= 0 || !textlen)
816 part = 0;
817 space.cx = rect.left + indent;
818 space.cx = MIN(space.cx, rect.right - rect.left - 1);
819 continue;
822 if (!WINHELP_AppendText(&line, &part, &space, &textsize,
823 &line_ascent, tm.tmAscent,
824 text, textlen, hFont, color, p->link) ||
825 (!newsize && (*line)->rect.bottom > rect.bottom))
827 ReleaseDC(hWnd, hDc);
828 return FALSE;
831 if (newsize)
832 newsize->cx = MAX(newsize->cx, (*line)->rect.right + INTERNAL_BORDER_WIDTH);
834 len -= textlen;
835 text += textlen;
836 if (text[0] == ' ') text++, len--;
840 if (newsize)
841 newsize->cy = (*line)->rect.bottom + INTERNAL_BORDER_WIDTH;
843 ReleaseDC(hWnd, hDc);
844 return TRUE;
847 /***********************************************************************
849 * WINHELP_AppendText
852 static BOOL WINHELP_AppendText(WINHELP_LINE ***linep, WINHELP_LINE_PART ***partp,
853 LPSIZE space, LPSIZE textsize,
854 INT *line_ascent, INT ascent,
855 LPCSTR text, UINT textlen,
856 HFONT font, COLORREF color, HLPFILE_LINK *link)
858 HGLOBAL handle;
859 WINHELP_LINE *line;
860 WINHELP_LINE_PART *part;
861 LPSTR ptr;
863 if (!*partp) /* New line */
865 *line_ascent = ascent;
867 handle = GlobalAlloc(GMEM_FIXED, sizeof(WINHELP_LINE) + textlen +
868 (link ? lstrlen(link->lpszPath) + 1 : 0));
869 if (!handle) return FALSE;
870 line = GlobalLock(handle);
871 line->next = 0;
872 part = &line->first_part;
873 ptr = GlobalLock(handle);
874 ptr += sizeof(WINHELP_LINE);
876 line->rect.top = (**linep ? (**linep)->rect.bottom : 0) + space->cy;
877 line->rect.bottom = line->rect.top;
878 line->rect.left = space->cx;
879 line->rect.right = space->cx;
881 if (**linep) *linep = &(**linep)->next;
882 **linep = line;
883 space->cy = 0;
885 else /* Same line */
887 line = **linep;
889 if (*line_ascent < ascent)
891 WINHELP_LINE_PART *p;
892 for (p = &line->first_part; p; p = p->next)
894 p->rect.top += ascent - *line_ascent;
895 p->rect.bottom += ascent - *line_ascent;
897 line->rect.bottom += ascent - *line_ascent;
898 *line_ascent = ascent;
901 handle = GlobalAlloc(GMEM_FIXED, sizeof(WINHELP_LINE_PART) + textlen +
902 (link ? lstrlen(link->lpszPath) + 1 : 0));
903 if (!handle) return FALSE;
904 part = GlobalLock(handle);
905 **partp = part;
906 ptr = GlobalLock(handle);
907 ptr += sizeof(WINHELP_LINE_PART);
910 hmemcpy(ptr, text, textlen);
911 part->rect.left = line->rect.right + (*partp ? space->cx : 0);
912 part->rect.right = part->rect.left + textsize->cx;
913 line->rect.right = part->rect.right;
914 part->rect.top =
915 ((*partp) ? line->rect.top : line->rect.bottom) + *line_ascent - ascent;
916 part->rect.bottom = part->rect.top + textsize->cy;
917 line->rect.bottom = MAX(line->rect.bottom, part->rect.bottom);
918 part->hSelf = handle;
919 part->lpsText = ptr;
920 part->wTextLen = textlen;
921 part->hFont = font;
922 part->color = color;
923 if (link)
925 strcpy(ptr + textlen, link->lpszPath);
926 part->link.lpszPath = ptr + textlen;
927 part->link.lHash = link->lHash;
928 part->link.bPopup = link->bPopup;
930 else part->link.lpszPath = 0;
932 part->next = 0;
933 *partp = &part->next;
935 space->cx = 0;
937 return TRUE;
940 /***********************************************************************
942 * WINHELP_CheckPopup
945 static VOID WINHELP_CheckPopup(UINT msg)
947 if (!Globals.hPopupWnd) return;
949 switch (msg)
951 case WM_COMMAND:
952 case WM_LBUTTONDOWN:
953 case WM_MBUTTONDOWN:
954 case WM_RBUTTONDOWN:
955 case WM_NCLBUTTONDOWN:
956 case WM_NCMBUTTONDOWN:
957 case WM_NCRBUTTONDOWN:
958 DestroyWindow(Globals.hPopupWnd);
959 Globals.hPopupWnd = 0;
963 /***********************************************************************
965 * WINHELP_DeleteLines
968 static VOID WINHELP_DeleteLines(WINHELP_WINDOW *win)
970 WINHELP_LINE *line, *next_line;
971 WINHELP_LINE_PART *part, *next_part;
972 for(line = win->first_line; line; line = next_line)
974 next_line = line->next;
975 for(part = &line->first_part; part; part = next_part)
977 next_part = part->next;
978 GlobalFree(part->hSelf);
981 win->first_line = 0;
984 /***********************************************************************
986 * WINHELP_DeleteWindow
989 static VOID WINHELP_DeleteWindow(WINHELP_WINDOW *win)
991 WINHELP_WINDOW **w;
993 for (w = &Globals.win_list; *w; w = &(*w)->next)
994 if (*w == win)
996 *w = win->next;
997 break;
1000 if (win->hShadowWnd) DestroyWindow(win->hShadowWnd);
1001 HLPFILE_FreeHlpFilePage(win->page);
1002 WINHELP_DeleteLines(win);
1003 GlobalFree(win->hSelf);
1006 /***********************************************************************
1008 * WINHELP_InitFonts
1011 static VOID WINHELP_InitFonts(HWND hWnd)
1013 WINHELP_WINDOW *win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1014 LOGFONT logfontlist[] = {
1015 {-10, 0, 0, 0, 400, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1016 {-12, 0, 0, 0, 700, 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 {-12, 0, 0, 0, 400, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1019 {-12, 0, 0, 0, 700, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1020 {-10, 0, 0, 0, 700, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1021 { -8, 0, 0, 0, 400, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"}};
1022 #define FONTS_LEN (sizeof(logfontlist)/sizeof(*logfontlist))
1024 static HFONT fonts[FONTS_LEN][2];
1025 static BOOL init = 0;
1027 win->fonts_len = FONTS_LEN;
1028 win->fonts = fonts;
1030 if (!init)
1032 INT i;
1034 for(i = 0; i < FONTS_LEN; i++)
1036 LOGFONT logfont = logfontlist[i];
1038 fonts[i][0] = CreateFontIndirect(&logfont);
1039 logfont.lfUnderline = 1;
1040 fonts[i][1] = CreateFontIndirect(&logfont);
1043 init = 1;
1047 /***********************************************************************
1049 * WINHELP_MessageBoxIDS
1052 INT WINHELP_MessageBoxIDS(UINT ids_text, UINT ids_title, WORD type)
1054 CHAR text[MAX_STRING_LEN];
1055 CHAR title[MAX_STRING_LEN];
1057 LoadString(Globals.hInstance, ids_text, text, sizeof(text));
1058 LoadString(Globals.hInstance, ids_title, title, sizeof(title));
1060 return(MessageBox(0, text, title, type));
1063 /***********************************************************************
1065 * MAIN_MessageBoxIDS_s
1068 INT WINHELP_MessageBoxIDS_s(UINT ids_text, LPCSTR str, UINT ids_title, WORD type)
1070 CHAR text[MAX_STRING_LEN];
1071 CHAR title[MAX_STRING_LEN];
1072 CHAR newtext[MAX_STRING_LEN + MAX_PATHNAME_LEN];
1074 LoadString(Globals.hInstance, ids_text, text, sizeof(text));
1075 LoadString(Globals.hInstance, ids_title, title, sizeof(title));
1076 wsprintf(newtext, text, str);
1078 return(MessageBox(0, newtext, title, type));
1081 /* Local Variables: */
1082 /* c-file-style: "GNU" */
1083 /* End: */