user: Defer all ExitWindowsEx processing to wineboot.
[wine] / programs / winhelp / winhelp.c
1 /*
2  * Help Viewer
3  *
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>
7  *              2004 Ken Belleau <jamez@ivic.qc.ca>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 #include <assert.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winuser.h"
34 #include "winhelp.h"
35 #include "winhelp_res.h"
36 #include "shellapi.h"
37
38 #include "wine/debug.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(winhelp);
41
42 static BOOL    WINHELP_RegisterWinClasses(void);
43 static LRESULT CALLBACK WINHELP_MainWndProc(HWND, UINT, WPARAM, LPARAM);
44 static LRESULT CALLBACK WINHELP_TextWndProc(HWND, UINT, WPARAM, LPARAM);
45 static LRESULT CALLBACK WINHELP_ButtonBoxWndProc(HWND, UINT, WPARAM, LPARAM);
46 static LRESULT CALLBACK WINHELP_ButtonWndProc(HWND, UINT, WPARAM, LPARAM);
47 static LRESULT CALLBACK WINHELP_HistoryWndProc(HWND, UINT, WPARAM, LPARAM);
48 static LRESULT CALLBACK WINHELP_ShadowWndProc(HWND, UINT, WPARAM, LPARAM);
49 static void    WINHELP_CheckPopup(UINT);
50 static BOOL    WINHELP_SplitLines(HWND hWnd, LPSIZE);
51 static void    WINHELP_InitFonts(HWND hWnd);
52 static void    WINHELP_DeleteLines(WINHELP_WINDOW*);
53 static void    WINHELP_DeleteWindow(WINHELP_WINDOW*);
54 static void    WINHELP_SetupText(HWND hWnd);
55 static WINHELP_LINE_PART* WINHELP_IsOverLink(WINHELP_WINDOW*, WPARAM, LPARAM);
56
57 WINHELP_GLOBALS Globals = {3, NULL, NULL, 0, TRUE, NULL, NULL, NULL, NULL};
58
59 /***********************************************************************
60  *
61  *           WINHELP_LookupHelpFile
62  */
63 HLPFILE* WINHELP_LookupHelpFile(LPCSTR lpszFile)
64 {
65     HLPFILE*        hlpfile;
66
67     hlpfile = HLPFILE_ReadHlpFile(lpszFile);
68
69     /* Add Suffix `.hlp' */
70     if (!hlpfile && lstrcmpi(lpszFile + strlen(lpszFile) - 4, ".hlp") != 0)
71     {
72         char      szFile_hlp[MAX_PATHNAME_LEN];
73
74         lstrcpyn(szFile_hlp, lpszFile, sizeof(szFile_hlp) - 4);
75         szFile_hlp[sizeof(szFile_hlp) - 5] = '\0';
76         lstrcat(szFile_hlp, ".hlp");
77
78         hlpfile = HLPFILE_ReadHlpFile(szFile_hlp);
79     }
80     if (!hlpfile)
81     {
82         WINHELP_MessageBoxIDS_s(STID_HLPFILE_ERROR_s, lpszFile, STID_WHERROR, MB_OK);
83         if (Globals.win_list) return NULL;
84     }
85     return hlpfile;
86 }
87
88 /******************************************************************
89  *              WINHELP_GetWindowInfo
90  *
91  *
92  */
93 HLPFILE_WINDOWINFO*     WINHELP_GetWindowInfo(HLPFILE* hlpfile, LPCSTR name)
94 {
95     static      HLPFILE_WINDOWINFO      mwi;
96     unsigned int     i;
97
98     if (!name || !name[0])
99         name = Globals.active_win->lpszName;
100
101     if (hlpfile)
102         for (i = 0; i < hlpfile->numWindows; i++)
103             if (!strcmp(hlpfile->windows[i].name, name))
104                 return &hlpfile->windows[i];
105
106     if (strcmp(name, "main") != 0)
107     {
108         WINE_FIXME("Couldn't find window info for %s\n", name);
109         assert(0);
110         return NULL;
111     }
112     if (!mwi.name[0])
113     {
114         strcpy(mwi.type, "primary");
115         strcpy(mwi.name, "main");
116         if (!LoadString(Globals.hInstance, STID_WINE_HELP, 
117                         mwi.caption, sizeof(mwi.caption)))
118             strcpy(mwi.caption, hlpfile->lpszTitle);
119         mwi.origin.x = mwi.origin.y = mwi.size.cx = mwi.size.cy = CW_USEDEFAULT;
120         mwi.style = SW_SHOW;
121         mwi.win_style = WS_OVERLAPPEDWINDOW;
122         mwi.sr_color = mwi.sr_color = 0xFFFFFF;
123     }
124     return &mwi;
125 }
126
127 /******************************************************************
128  *              HLPFILE_GetPopupWindowInfo
129  *
130  *
131  */
132 static HLPFILE_WINDOWINFO*     WINHELP_GetPopupWindowInfo(HLPFILE* hlpfile, HWND hParentWnd, POINT* mouse)
133 {
134     static      HLPFILE_WINDOWINFO      wi;
135
136     RECT parent_rect;
137     
138     wi.type[0] = wi.name[0] = wi.caption[0] = '\0';
139
140     /* Calculate horizontal size and position of a popup window */
141     GetWindowRect(hParentWnd, &parent_rect);
142     wi.size.cx = (parent_rect.right  - parent_rect.left) / 2;
143     wi.size.cy = 10; /* need a non null value, so that border are taken into account while computing */
144
145     wi.origin = *mouse;
146     ClientToScreen(hParentWnd, &wi.origin);
147     wi.origin.x -= wi.size.cx / 2;
148     wi.origin.x  = min(wi.origin.x, GetSystemMetrics(SM_CXSCREEN) - wi.size.cx);
149     wi.origin.x  = max(wi.origin.x, 0);
150
151     wi.style = SW_SHOW;
152     wi.win_style = WS_POPUPWINDOW;
153     wi.sr_color = wi.sr_color = 0xFFFFFF;
154
155     return &wi;
156 }
157
158 /***********************************************************************
159  *
160  *           WinMain
161  */
162 int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
163 {
164     MSG                 msg;
165     LONG                lHash = 0;
166     HLPFILE*            hlpfile;
167     static CHAR         default_wndname[] = "main";
168     LPSTR               wndname = default_wndname;
169     WINHELP_DLL*        dll;
170
171     Globals.hInstance = hInstance;
172
173     /* Get options */
174     while (*cmdline && (*cmdline == ' ' || *cmdline == '-'))
175     {
176         CHAR   option;
177         LPCSTR topic_id;
178         if (*cmdline++ == ' ') continue;
179
180         option = *cmdline;
181         if (option) cmdline++;
182         while (*cmdline && *cmdline == ' ') cmdline++;
183         switch (option)
184         {
185         case 'i':
186         case 'I':
187             topic_id = cmdline;
188             while (*cmdline && *cmdline != ' ') cmdline++;
189             if (*cmdline) *cmdline++ = '\0';
190             lHash = HLPFILE_Hash(topic_id);
191             break;
192
193         case '3':
194         case '4':
195             Globals.wVersion = option - '0';
196             break;
197
198         case 'x':
199             show = SW_HIDE; 
200             Globals.isBook = FALSE;
201             break;
202
203         default:
204             WINE_FIXME("Unsupported cmd line: %s\n", cmdline);
205             break;
206         }
207     }
208
209     /* Create primary window */
210     if (!WINHELP_RegisterWinClasses())
211     {
212         WINE_FIXME("Couldn't register classes\n");
213         return 0;
214     }
215
216     if (*cmdline)
217     {
218         char*   ptr;
219         if ((*cmdline == '"') && (ptr = strchr(cmdline+1, '"')))
220         {
221             cmdline++;
222             *ptr = '\0';
223         }
224         if ((ptr = strchr(cmdline, '>')))
225         {
226             *ptr = '\0';
227             wndname = ptr + 1;
228         }
229         hlpfile = WINHELP_LookupHelpFile(cmdline);
230         if (!hlpfile) return 0;
231     }
232     else hlpfile = NULL;
233     WINHELP_CreateHelpWindowByHash(hlpfile, lHash, 
234                                    WINHELP_GetWindowInfo(hlpfile, wndname), show);
235
236     /* Message loop */
237     while (GetMessage(&msg, 0, 0, 0))
238     {
239         TranslateMessage(&msg);
240         DispatchMessage(&msg);
241     }
242     for (dll = Globals.dlls; dll; dll = dll->next)
243     {
244         if (dll->class & DC_INITTERM) dll->handler(DW_TERM, 0, 0);
245     }
246     return 0;
247 }
248
249 /***********************************************************************
250  *
251  *           RegisterWinClasses
252  */
253 static BOOL WINHELP_RegisterWinClasses(void)
254 {
255     WNDCLASS class_main, class_button_box, class_text, class_shadow, class_history;
256
257     class_main.style               = CS_HREDRAW | CS_VREDRAW;
258     class_main.lpfnWndProc         = WINHELP_MainWndProc;
259     class_main.cbClsExtra          = 0;
260     class_main.cbWndExtra          = sizeof(LONG);
261     class_main.hInstance           = Globals.hInstance;
262     class_main.hIcon               = LoadIcon(0, IDI_APPLICATION);
263     class_main.hCursor             = LoadCursor(0, IDC_ARROW);
264     class_main.hbrBackground       = GetStockObject(WHITE_BRUSH);
265     class_main.lpszMenuName        = 0;
266     class_main.lpszClassName       = MAIN_WIN_CLASS_NAME;
267
268     class_button_box               = class_main;
269     class_button_box.lpfnWndProc   = WINHELP_ButtonBoxWndProc;
270     class_button_box.hbrBackground = GetStockObject(GRAY_BRUSH);
271     class_button_box.lpszClassName = BUTTON_BOX_WIN_CLASS_NAME;
272
273     class_text = class_main;
274     class_text.lpfnWndProc         = WINHELP_TextWndProc;
275     class_text.lpszClassName       = TEXT_WIN_CLASS_NAME;
276
277     class_shadow = class_main;
278     class_shadow.lpfnWndProc       = WINHELP_ShadowWndProc;
279     class_shadow.hbrBackground     = GetStockObject(GRAY_BRUSH);
280     class_shadow.lpszClassName     = SHADOW_WIN_CLASS_NAME;
281
282     class_history = class_main;
283     class_history.lpfnWndProc      = WINHELP_HistoryWndProc;
284     class_history.lpszClassName    = HISTORY_WIN_CLASS_NAME;
285
286     return (RegisterClass(&class_main) &&
287             RegisterClass(&class_button_box) &&
288             RegisterClass(&class_text) &&
289             RegisterClass(&class_shadow) &&
290             RegisterClass(&class_history));
291 }
292
293 typedef struct
294 {
295     WORD size;
296     WORD command;
297     LONG data;
298     LONG reserved;
299     WORD ofsFilename;
300     WORD ofsData;
301 } WINHELP,*LPWINHELP;
302
303 /******************************************************************
304  *              WINHELP_HandleCommand
305  *
306  *
307  */
308 static LRESULT  WINHELP_HandleCommand(HWND hSrcWnd, LPARAM lParam)
309 {
310     COPYDATASTRUCT*     cds = (COPYDATASTRUCT*)lParam;
311     WINHELP*            wh;
312
313     if (cds->dwData != 0xA1DE505)
314     {
315         WINE_FIXME("Wrong magic number (%08lx)\n", cds->dwData);
316         return 0;
317     }
318
319     wh = (WINHELP*)cds->lpData;
320
321     if (wh)
322     {
323         char*   ptr = (wh->ofsFilename) ? (LPSTR)wh + wh->ofsFilename : NULL;
324
325         WINE_TRACE("Got[%u]: cmd=%u data=%08lx fn=%s\n", 
326                    wh->size, wh->command, wh->data, ptr);
327         switch (wh->command)
328         {
329         case HELP_CONTEXT:
330             if (ptr)
331             {
332                 MACRO_JumpContext(ptr, "main", wh->data);
333             }
334             break;
335         case HELP_QUIT:
336             MACRO_Exit();
337             break;
338         case HELP_CONTENTS:
339             if (ptr)
340             {
341                 MACRO_JumpContents(ptr, "main");
342             }
343             break;
344         case HELP_HELPONHELP:
345             MACRO_HelpOn();
346             break;
347         /* case HELP_SETINDEX: */
348         case HELP_SETCONTENTS:
349             if (ptr)
350             {
351                 MACRO_SetContents(ptr, wh->data);
352             }
353             break;
354         case HELP_CONTEXTPOPUP:
355             if (ptr)
356             {
357                 MACRO_PopupContext(ptr, wh->data);
358             }
359             break;
360         /* case HELP_FORCEFILE:*/
361         /* case HELP_CONTEXTMENU: */
362         case HELP_FINDER:
363             /* in fact, should be the topic dialog box */
364             if (ptr)
365             {
366                 MACRO_JumpHash(ptr, "main", 0);
367             }
368             break;
369         /* case HELP_WM_HELP: */
370         /* case HELP_SETPOPUP_POS: */
371         /* case HELP_KEY: */
372         /* case HELP_COMMAND: */
373         /* case HELP_PARTIALKEY: */
374         /* case HELP_MULTIKEY: */
375         /* case HELP_SETWINPOS: */
376             WINE_FIXME("Unknown command (%x) for remote winhelp control\n", wh->command);
377             break;
378         }
379     }
380     return 0L;
381 }
382
383 /******************************************************************
384  *              WINHELP_ReuseWindow
385  *
386  *
387  */
388 static BOOL     WINHELP_ReuseWindow(WINHELP_WINDOW* win, WINHELP_WINDOW* oldwin, 
389                                     HLPFILE_PAGE* page, int nCmdShow)
390 {
391     unsigned int i;
392
393     win->hMainWnd      = oldwin->hMainWnd;
394     win->hButtonBoxWnd = oldwin->hButtonBoxWnd;
395     win->hTextWnd      = oldwin->hTextWnd;
396     win->hHistoryWnd   = oldwin->hHistoryWnd;
397     oldwin->hMainWnd = oldwin->hButtonBoxWnd = oldwin->hTextWnd = oldwin->hHistoryWnd = 0;
398
399     SetWindowLong(win->hMainWnd,      0, (LONG)win);
400     SetWindowLong(win->hButtonBoxWnd, 0, (LONG)win);
401     SetWindowLong(win->hTextWnd,      0, (LONG)win);
402     SetWindowLong(win->hHistoryWnd,   0, (LONG)win);
403
404     WINHELP_InitFonts(win->hMainWnd);
405
406     if (page)
407         SetWindowText(win->hMainWnd, page->file->lpszTitle);
408
409     WINHELP_SetupText(win->hTextWnd);
410     InvalidateRect(win->hTextWnd, NULL, TRUE);
411     SendMessage(win->hMainWnd, WM_USER, 0, 0);
412     ShowWindow(win->hMainWnd, nCmdShow);
413     UpdateWindow(win->hTextWnd);
414
415     if (!(win->info->win_style & WS_POPUP))
416     {
417         unsigned        num;
418
419         memcpy(win->history, oldwin->history, sizeof(win->history));
420         win->histIndex = oldwin->histIndex;
421
422         /* FIXME: when using back, we shouldn't update the history... */
423
424         if (page)
425         {
426             for (i = 0; i < win->histIndex; i++)
427                 if (win->history[i] == page) break;
428
429             /* if the new page is already in the history, do nothing */
430             if (i == win->histIndex)
431             {
432                 num = sizeof(win->history) / sizeof(win->history[0]);
433                 if (win->histIndex == num)
434                 {
435                     /* we're full, remove latest entry */
436                     HLPFILE_FreeHlpFile(win->history[0]->file);
437                     memmove(&win->history[0], &win->history[1], 
438                             (num - 1) * sizeof(win->history[0]));
439                     win->histIndex--;
440                 }
441                 win->history[win->histIndex++] = page;
442                 page->file->wRefCount++;
443                 if (win->hHistoryWnd) InvalidateRect(win->hHistoryWnd, NULL, TRUE);
444             }
445         }
446
447         memcpy(win->back, oldwin->back, sizeof(win->back));
448         win->backIndex = oldwin->backIndex;
449
450         if (page)
451         {
452             num = sizeof(win->back) / sizeof(win->back[0]);
453             if (win->backIndex == num)
454             {
455                 /* we're full, remove latest entry */
456                 HLPFILE_FreeHlpFile(win->back[0]->file);
457                 memmove(&win->back[0], &win->back[1], 
458                         (num - 1) * sizeof(win->back[0]));
459                 win->backIndex--;
460             }
461             win->back[win->backIndex++] = page;
462             page->file->wRefCount++;
463         }
464     }
465     else
466         win->backIndex = win->histIndex = 0;
467
468     oldwin->histIndex = oldwin->backIndex = 0;
469     WINHELP_DeleteWindow(oldwin);
470     return TRUE;
471 }
472
473 /***********************************************************************
474  *
475  *           WINHELP_CreateHelpWindow
476  */
477 BOOL WINHELP_CreateHelpWindow(HLPFILE_PAGE* page, HLPFILE_WINDOWINFO* wi,
478                               int nCmdShow)
479 {
480     WINHELP_WINDOW *win, *oldwin;
481     HWND hWnd;
482     BOOL bPrimary;
483     BOOL bPopup;
484
485     bPrimary = !lstrcmpi(wi->name, "main");
486     bPopup = wi->win_style & WS_POPUP;
487
488     /* Initialize WINHELP_WINDOW struct */
489     win = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
490                     sizeof(WINHELP_WINDOW) + strlen(wi->name) + 1);
491     if (!win) return FALSE;
492
493     win->next = Globals.win_list;
494     Globals.win_list = win;
495
496     win->lpszName = (char*)win + sizeof(WINHELP_WINDOW);
497     lstrcpy((char*)win->lpszName, wi->name);
498
499     win->page = page;
500
501     win->hArrowCur = LoadCursorA(0, (LPSTR)IDC_ARROW);
502     win->hHandCur = LoadCursorA(0, (LPSTR)IDC_HAND);
503
504     win->info = wi;
505
506     Globals.active_win = win;
507
508     /* Initialize default pushbuttons */
509     if (bPrimary && page)
510     {
511         CHAR    buffer[MAX_STRING_LEN];
512
513         LoadString(Globals.hInstance, STID_CONTENTS, buffer, sizeof(buffer));
514         MACRO_CreateButton("BTN_CONTENTS", buffer, "Contents()");
515         LoadString(Globals.hInstance, STID_SEARCH,buffer, sizeof(buffer));
516         MACRO_CreateButton("BTN_SEARCH", buffer, "Search()");
517         LoadString(Globals.hInstance, STID_BACK, buffer, sizeof(buffer));
518         MACRO_CreateButton("BTN_BACK", buffer, "Back()");
519         LoadString(Globals.hInstance, STID_HISTORY, buffer, sizeof(buffer));
520         MACRO_CreateButton("BTN_HISTORY", buffer, "History()");
521         LoadString(Globals.hInstance, STID_TOPICS, buffer, sizeof(buffer));
522         MACRO_CreateButton("BTN_TOPICS", buffer, "Finder()");
523     }
524
525     /* Initialize file specific pushbuttons */
526     if (!(wi->win_style & WS_POPUP) && page)
527     {
528         HLPFILE_MACRO  *macro;
529         for (macro = page->file->first_macro; macro; macro = macro->next)
530             MACRO_ExecuteMacro(macro->lpszMacro);
531
532         for (macro = page->first_macro; macro; macro = macro->next)
533             MACRO_ExecuteMacro(macro->lpszMacro);
534     }
535
536     /* Reuse existing window */
537     if (!bPopup)
538     {
539         for (oldwin = win->next; oldwin; oldwin = oldwin->next)
540         {
541             if (!lstrcmpi(oldwin->lpszName, wi->name))
542             {
543                 return WINHELP_ReuseWindow(win, oldwin, page, nCmdShow);
544             }
545         }
546         if (page)
547         {
548             win->histIndex = win->backIndex = 1;
549             win->history[0] = win->back[0] = page;
550             page->file->wRefCount += 2;
551             strcpy(wi->caption, page->file->lpszTitle);
552         }
553     }
554
555     hWnd = CreateWindow(bPopup ? TEXT_WIN_CLASS_NAME : MAIN_WIN_CLASS_NAME,
556                         wi->caption, 
557                         bPrimary ? WS_OVERLAPPEDWINDOW : wi->win_style,
558                         wi->origin.x, wi->origin.y, wi->size.cx, wi->size.cy,
559                         NULL, bPrimary ? LoadMenu(Globals.hInstance, MAKEINTRESOURCE(MAIN_MENU)) : 0,
560                         Globals.hInstance, win);
561
562     ShowWindow(hWnd, nCmdShow);
563     UpdateWindow(hWnd);
564
565     return TRUE;
566 }
567
568 /***********************************************************************
569  *
570  *           WINHELP_CreateHelpWindowByHash
571  */
572 BOOL WINHELP_CreateHelpWindowByHash(HLPFILE* hlpfile, LONG lHash, 
573                                     HLPFILE_WINDOWINFO* wi, int nCmdShow)
574 {
575     HLPFILE_PAGE*       page = NULL;
576
577     if (hlpfile)
578         page = lHash ? HLPFILE_PageByHash(hlpfile, lHash) : 
579             HLPFILE_Contents(hlpfile);
580     if (page) page->file->wRefCount++;
581     return WINHELP_CreateHelpWindow(page, wi, nCmdShow);
582 }
583
584 /***********************************************************************
585  *
586  *           WINHELP_MainWndProc
587  */
588 static LRESULT CALLBACK WINHELP_MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
589 {
590     WINHELP_WINDOW *win;
591     WINHELP_BUTTON *button;
592     RECT rect, button_box_rect;
593     INT  text_top, curPos, min, max, dy, keyDelta;
594
595     WINHELP_CheckPopup(msg);
596
597     switch (msg)
598     {
599     case WM_NCCREATE:
600         win = (WINHELP_WINDOW*) ((LPCREATESTRUCT) lParam)->lpCreateParams;
601         SetWindowLong(hWnd, 0, (LONG) win);
602         win->hMainWnd = hWnd;
603         break;
604
605     case WM_CREATE:
606         win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
607
608         /* Create button box and text Window */
609         CreateWindow(BUTTON_BOX_WIN_CLASS_NAME, "", WS_CHILD | WS_VISIBLE,
610                      0, 0, 0, 0, hWnd, 0, Globals.hInstance, win);
611
612         CreateWindow(TEXT_WIN_CLASS_NAME, "", WS_CHILD | WS_VISIBLE,
613                      0, 0, 0, 0, hWnd, 0, Globals.hInstance, win);
614
615         /* Fall through */
616     case WM_USER:
617     case WM_WINDOWPOSCHANGED:
618         win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
619         GetClientRect(hWnd, &rect);
620
621         /* Update button box and text Window */
622         SetWindowPos(win->hButtonBoxWnd, HWND_TOP,
623                      rect.left, rect.top,
624                      rect.right - rect.left,
625                      rect.bottom - rect.top, 0);
626
627         GetWindowRect(win->hButtonBoxWnd, &button_box_rect);
628         text_top = rect.top + button_box_rect.bottom - button_box_rect.top;
629
630         SetWindowPos(win->hTextWnd, HWND_TOP,
631                      rect.left, text_top,
632                      rect.right - rect.left,
633                      rect.bottom - text_top, 0);
634
635         break;
636
637     case WM_COMMAND:
638         Globals.active_win = win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
639         switch (wParam)
640         {
641             /* Menu FILE */
642         case MNID_FILE_OPEN:    MACRO_FileOpen();       break;
643         case MNID_FILE_PRINT:   MACRO_Print();          break;
644         case MNID_FILE_SETUP:   MACRO_PrinterSetup();   break;
645         case MNID_FILE_EXIT:    MACRO_Exit();           break;
646
647             /* Menu EDIT */
648         case MNID_EDIT_COPYDLG: MACRO_CopyDialog();     break;
649         case MNID_EDIT_ANNOTATE:MACRO_Annotate();       break;
650
651             /* Menu Bookmark */
652         case MNID_BKMK_DEFINE:  MACRO_BookmarkDefine(); break;
653
654             /* Menu Help */
655         case MNID_HELP_HELPON:  MACRO_HelpOn();         break;
656         case MNID_HELP_HELPTOP: MACRO_HelpOnTop();      break;
657         case MNID_HELP_ABOUT:   MACRO_About();          break;
658         case MNID_HELP_WINE:    ShellAbout(hWnd, "WINE", "Help", 0); break;
659
660         default:
661             /* Buttons */
662             for (button = win->first_button; button; button = button->next)
663                 if (wParam == button->wParam) break;
664             if (button)
665                 MACRO_ExecuteMacro(button->lpszMacro);
666             else
667                 WINHELP_MessageBoxIDS(STID_NOT_IMPLEMENTED, 0x121, MB_OK);
668             break;
669         }
670         break;
671     case WM_DESTROY:
672         if (Globals.hPopupWnd) DestroyWindow(Globals.hPopupWnd);
673         break;
674     case WM_COPYDATA:
675         return WINHELP_HandleCommand((HWND)wParam, lParam);
676
677     case WM_KEYDOWN:
678         keyDelta = 0;
679
680         switch (wParam)
681         {
682         case VK_UP:
683         case VK_DOWN:
684             keyDelta = GetSystemMetrics(SM_CXVSCROLL);
685             if (wParam == VK_UP)
686                 keyDelta = -keyDelta;
687
688         case VK_PRIOR:
689         case VK_NEXT:
690             win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
691             curPos = GetScrollPos(win->hTextWnd, SB_VERT);
692             GetScrollRange(win->hTextWnd, SB_VERT, &min, &max);
693
694             if (keyDelta == 0)
695             {            
696                 GetClientRect(win->hTextWnd, &rect);
697                 keyDelta = (rect.bottom - rect.top) / 2;
698                 if (wParam == VK_PRIOR)
699                     keyDelta = -keyDelta;
700             }
701
702             curPos += keyDelta;
703             if (curPos > max)
704                  curPos = max;
705             else if (curPos < min)
706                  curPos = min;
707
708             dy = GetScrollPos(win->hTextWnd, SB_VERT) - curPos;
709             SetScrollPos(win->hTextWnd, SB_VERT, curPos, TRUE);
710             ScrollWindow(win->hTextWnd, 0, dy, NULL, NULL);
711             UpdateWindow(win->hTextWnd);
712             return 0;
713
714         case VK_ESCAPE:
715             MACRO_Exit();
716             return 0;
717         }
718         break;
719     }
720     return DefWindowProc(hWnd, msg, wParam, lParam);
721 }
722
723 /***********************************************************************
724  *
725  *           WINHELP_ButtonBoxWndProc
726  */
727 static LRESULT CALLBACK WINHELP_ButtonBoxWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
728 {
729     WINDOWPOS      *winpos;
730     WINHELP_WINDOW *win;
731     WINHELP_BUTTON *button;
732     SIZE button_size;
733     INT  x, y;
734
735     WINHELP_CheckPopup(msg);
736
737     switch (msg)
738     {
739     case WM_NCCREATE:
740         win = (WINHELP_WINDOW*) ((LPCREATESTRUCT) lParam)->lpCreateParams;
741         SetWindowLong(hWnd, 0, (LONG) win);
742         win->hButtonBoxWnd = hWnd;
743         break;
744
745     case WM_WINDOWPOSCHANGING:
746         winpos = (WINDOWPOS*) lParam;
747         win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
748
749         /* Update buttons */
750         button_size.cx = 0;
751         button_size.cy = 0;
752         for (button = win->first_button; button; button = button->next)
753         {
754             HDC  hDc;
755             SIZE textsize;
756             if (!button->hWnd)
757             {
758                 button->hWnd = CreateWindow(STRING_BUTTON, (LPSTR) button->lpszName,
759                                             WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
760                                             0, 0, 0, 0,
761                                             hWnd, (HMENU) button->wParam,
762                                             Globals.hInstance, 0);
763                 if (button->hWnd) {
764                     if (Globals.button_proc == NULL)
765                         Globals.button_proc = (WNDPROC) GetWindowLongPtr(button->hWnd, GWLP_WNDPROC);
766                     SetWindowLongPtr(button->hWnd, GWLP_WNDPROC, (LONG_PTR) WINHELP_ButtonWndProc);
767                 }
768             }
769             hDc = GetDC(button->hWnd);
770             GetTextExtentPoint(hDc, button->lpszName,
771                                lstrlen(button->lpszName), &textsize);
772             ReleaseDC(button->hWnd, hDc);
773
774             button_size.cx = max(button_size.cx, textsize.cx + BUTTON_CX);
775             button_size.cy = max(button_size.cy, textsize.cy + BUTTON_CY);
776         }
777
778         x = 0;
779         y = 0;
780         for (button = win->first_button; button; button = button->next)
781         {
782             SetWindowPos(button->hWnd, HWND_TOP, x, y, button_size.cx, button_size.cy, 0);
783
784             if (x + 2 * button_size.cx <= winpos->cx)
785                 x += button_size.cx;
786             else
787                 x = 0, y += button_size.cy;
788         }
789         winpos->cy = y + (x ? button_size.cy : 0);
790         break;
791
792     case WM_COMMAND:
793         SendMessage(GetParent(hWnd), msg, wParam, lParam);
794         break;
795
796     case WM_KEYDOWN:
797         switch (wParam)
798         {
799         case VK_UP:
800         case VK_DOWN:
801         case VK_PRIOR:
802         case VK_NEXT:
803         case VK_ESCAPE:
804             return SendMessage(GetParent(hWnd), msg, wParam, lParam);
805         }
806         break;
807     }
808
809     return DefWindowProc(hWnd, msg, wParam, lParam);
810 }
811
812 /***********************************************************************
813  *
814  *           WINHELP_ButtonWndProc
815  */
816 static LRESULT CALLBACK WINHELP_ButtonWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
817 {
818     if (msg == WM_KEYDOWN)
819     {
820         switch (wParam)
821         {
822         case VK_UP:
823         case VK_DOWN:
824         case VK_PRIOR:
825         case VK_NEXT:
826         case VK_ESCAPE:
827             return SendMessage(GetParent(hWnd), msg, wParam, lParam);
828         }
829     }
830
831     return CallWindowProc(Globals.button_proc, hWnd, msg, wParam, lParam);
832 }
833
834 /***********************************************************************
835  *
836  *           WINHELP_TextWndProc
837  */
838 static LRESULT CALLBACK WINHELP_TextWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
839 {
840     WINHELP_WINDOW    *win;
841     WINHELP_LINE      *line;
842     WINHELP_LINE_PART *part;
843     WINDOWPOS         *winpos;
844     PAINTSTRUCT        ps;
845     HDC   hDc;
846     POINT mouse;
847     INT   scroll_pos;
848     HWND  hPopupWnd;
849     BOOL  bExit;
850
851     if (msg != WM_LBUTTONDOWN)
852         WINHELP_CheckPopup(msg);
853
854     switch (msg)
855     {
856     case WM_NCCREATE:
857         win = (WINHELP_WINDOW*) ((LPCREATESTRUCT) lParam)->lpCreateParams;
858         SetWindowLong(hWnd, 0, (LONG) win);
859         win->hTextWnd = hWnd;
860         if (win->info->win_style & WS_POPUP) Globals.hPopupWnd = win->hMainWnd = hWnd;
861         WINHELP_InitFonts(hWnd);
862         break;
863
864     case WM_CREATE:
865         win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
866
867         /* Calculate vertical size and position of a popup window */
868         if (win->info->win_style & WS_POPUP)
869         {
870             POINT origin;
871             RECT old_window_rect;
872             RECT old_client_rect;
873             SIZE old_window_size;
874             SIZE old_client_size;
875             SIZE new_client_size;
876             SIZE new_window_size;
877
878             GetWindowRect(hWnd, &old_window_rect);
879             origin.x = old_window_rect.left;
880             origin.y = old_window_rect.top;
881             old_window_size.cx = old_window_rect.right  - old_window_rect.left;
882             old_window_size.cy = old_window_rect.bottom - old_window_rect.top;
883
884             GetClientRect(hWnd, &old_client_rect);
885             old_client_size.cx = old_client_rect.right  - old_client_rect.left;
886             old_client_size.cy = old_client_rect.bottom - old_client_rect.top;
887
888             new_client_size = old_client_size;
889             WINHELP_SplitLines(hWnd, &new_client_size);
890
891             if (origin.y + POPUP_YDISTANCE + new_client_size.cy <= GetSystemMetrics(SM_CYSCREEN))
892                 origin.y += POPUP_YDISTANCE;
893             else
894                 origin.y -= POPUP_YDISTANCE + new_client_size.cy;
895
896             new_window_size.cx = old_window_size.cx - old_client_size.cx + new_client_size.cx;
897             new_window_size.cy = old_window_size.cy - old_client_size.cy + new_client_size.cy;
898
899             win->hShadowWnd =
900                 CreateWindow(SHADOW_WIN_CLASS_NAME, "", WS_POPUP,
901                              origin.x + SHADOW_DX, origin.y + SHADOW_DY,
902                              new_window_size.cx, new_window_size.cy,
903                              0, 0, Globals.hInstance, 0);
904
905             SetWindowPos(hWnd, HWND_TOP, origin.x, origin.y,
906                          new_window_size.cx, new_window_size.cy,
907                          0);
908             SetWindowPos(win->hShadowWnd, hWnd, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
909             ShowWindow(win->hShadowWnd, SW_NORMAL);
910             SetActiveWindow(hWnd);
911         }
912         break;
913
914     case WM_WINDOWPOSCHANGED:
915         winpos = (WINDOWPOS*) lParam;
916
917         if (!(winpos->flags & SWP_NOSIZE)) WINHELP_SetupText(hWnd);
918         break;
919
920     case WM_MOUSEWHEEL:
921     {
922        int wheelDelta = 0;
923        UINT scrollLines = 3;
924        int curPos = GetScrollPos(hWnd, SB_VERT);
925        int min, max;
926
927        GetScrollRange(hWnd, SB_VERT, &min, &max);
928
929        SystemParametersInfo(SPI_GETWHEELSCROLLLINES,0, &scrollLines, 0);
930        if (wParam & (MK_SHIFT | MK_CONTROL))
931            return DefWindowProc(hWnd, msg, wParam, lParam);
932        wheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
933        if (abs(wheelDelta) >= WHEEL_DELTA && scrollLines) {
934            int dy;
935
936            curPos += wheelDelta;
937            if (curPos > max)
938                 curPos = max;
939            else if (curPos < min)
940                 curPos = min;
941
942            dy = GetScrollPos(hWnd, SB_VERT) - curPos;
943            SetScrollPos(hWnd, SB_VERT, curPos, TRUE);
944            ScrollWindow(hWnd, 0, dy, NULL, NULL);
945            UpdateWindow(hWnd);
946        }
947     }
948     break;
949
950     case WM_VSCROLL:
951     {
952         BOOL  update = TRUE;
953         RECT  rect;
954         INT   Min, Max;
955         INT   CurPos = GetScrollPos(hWnd, SB_VERT);
956         INT   dy;
957         
958         GetScrollRange(hWnd, SB_VERT, &Min, &Max);
959         GetClientRect(hWnd, &rect);
960
961         switch (wParam & 0xffff)
962         {
963         case SB_THUMBTRACK:
964         case SB_THUMBPOSITION: CurPos  = wParam >> 16;                   break;
965         case SB_TOP:           CurPos  = Min;                            break;
966         case SB_BOTTOM:        CurPos  = Max;                            break;
967         case SB_PAGEUP:        CurPos -= (rect.bottom - rect.top) / 2;   break;
968         case SB_PAGEDOWN:      CurPos += (rect.bottom - rect.top) / 2;   break;
969         case SB_LINEUP:        CurPos -= GetSystemMetrics(SM_CXVSCROLL); break;
970         case SB_LINEDOWN:      CurPos += GetSystemMetrics(SM_CXVSCROLL); break;
971         default: update = FALSE;
972         }
973         if (update)
974         {
975             if (CurPos > Max)
976                 CurPos = Max;
977             else if (CurPos < Min)
978                 CurPos = Min;
979             dy = GetScrollPos(hWnd, SB_VERT) - CurPos;
980             SetScrollPos(hWnd, SB_VERT, CurPos, TRUE);
981             ScrollWindow(hWnd, 0, dy, NULL, NULL);
982             UpdateWindow(hWnd);
983         }
984     }
985     break;
986
987     case WM_PAINT:
988         hDc = BeginPaint(hWnd, &ps);
989         win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
990         scroll_pos = GetScrollPos(hWnd, SB_VERT);
991
992         for (line = win->first_line; line; line = line->next)
993         {
994             for (part = &line->first_part; part; part = part->next)
995             {
996                 switch (part->cookie)
997                 {
998                 case hlp_line_part_text:
999                     SelectObject(hDc, part->u.text.hFont);
1000                     SetTextColor(hDc, part->u.text.color);
1001                     TextOut(hDc, part->rect.left, part->rect.top - scroll_pos,
1002                             part->u.text.lpsText, part->u.text.wTextLen);
1003                     if (part->u.text.wUnderline)
1004                     {
1005                         HPEN    hPen;
1006
1007                         switch (part->u.text.wUnderline)
1008                         {
1009                         case 1: /* simple */
1010                         case 2: /* double */
1011                             hPen = CreatePen(PS_SOLID, 1, part->u.text.color);
1012                             break;
1013                         case 3: /* dotted */
1014                             hPen = CreatePen(PS_DOT, 1, part->u.text.color);
1015                             break;
1016                         default:
1017                             WINE_FIXME("Unknow underline type\n");
1018                             continue;
1019                         }
1020
1021                         SelectObject(hDc, hPen);
1022                         MoveToEx(hDc, part->rect.left, part->rect.bottom - scroll_pos - 1, NULL);
1023                         LineTo(hDc, part->rect.right, part->rect.bottom - scroll_pos - 1);
1024                         if (part->u.text.wUnderline == 2)
1025                         {
1026                             MoveToEx(hDc, part->rect.left, part->rect.bottom - scroll_pos + 1, NULL);
1027                             LineTo(hDc, part->rect.right, part->rect.bottom - scroll_pos + 1);
1028                         }
1029                         DeleteObject(hPen);
1030                     }
1031                     break;
1032                 case hlp_line_part_bitmap:
1033                     {
1034                         HDC hMemDC;
1035
1036                         hMemDC = CreateCompatibleDC(hDc);
1037                         SelectObject(hMemDC, part->u.bitmap.hBitmap);
1038                         BitBlt(hDc, part->rect.left, part->rect.top - scroll_pos,
1039                                part->rect.right - part->rect.left, part->rect.bottom - part->rect.top,
1040                                hMemDC, 0, 0, SRCCOPY);
1041                         DeleteDC(hMemDC);
1042                     }
1043                     break;
1044                 case hlp_line_part_metafile:
1045                     {
1046                         POINT   pt;
1047
1048                         SetViewportOrgEx(hDc, part->rect.left, part->rect.top - scroll_pos, &pt);
1049                         PlayMetaFile(hDc, part->u.metafile.hMetaFile);
1050                         SetViewportOrgEx(hDc, pt.x, pt.y, NULL);
1051                     }
1052                     break;
1053                 }
1054             }
1055         }
1056
1057         EndPaint(hWnd, &ps);
1058         break;
1059
1060     case WM_MOUSEMOVE:
1061         win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1062
1063         if (WINHELP_IsOverLink(win, wParam, lParam))
1064             SetCursor(win->hHandCur); /* set to hand pointer cursor to indicate a link */
1065         else
1066             SetCursor(win->hArrowCur); /* set to hand pointer cursor to indicate a link */
1067
1068         break;
1069
1070     case WM_LBUTTONDOWN:
1071         win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1072
1073         hPopupWnd = Globals.hPopupWnd;
1074         Globals.hPopupWnd = 0;
1075
1076         part = WINHELP_IsOverLink(win, wParam, lParam);
1077         if (part)
1078         {
1079             HLPFILE*            hlpfile;
1080             HLPFILE_WINDOWINFO* wi;
1081
1082             mouse.x = LOWORD(lParam);
1083             mouse.y = HIWORD(lParam);
1084
1085             if (part->link) switch (part->link->cookie)
1086             {
1087             case hlp_link_link:
1088                 hlpfile = WINHELP_LookupHelpFile(part->link->lpszString);
1089                 if (part->link->window == -1)
1090                     wi = win->info;
1091                 else if ((part->link->window >= 0) && (part->link->window < hlpfile->numWindows))
1092                     wi = &hlpfile->windows[part->link->window];
1093                 else
1094                 {
1095                     WINE_WARN("link to window %d/%d\n", part->link->window, hlpfile->numWindows);
1096                     break;
1097                 }
1098                 WINHELP_CreateHelpWindowByHash(hlpfile, part->link->lHash, wi,
1099                                                SW_NORMAL);
1100                 break;
1101             case hlp_link_popup:
1102                 hlpfile = WINHELP_LookupHelpFile(part->link->lpszString);
1103                 WINHELP_CreateHelpWindowByHash(hlpfile, part->link->lHash, 
1104                                                WINHELP_GetPopupWindowInfo(hlpfile, hWnd, &mouse),
1105                                                SW_NORMAL);
1106                 break;
1107             case hlp_link_macro:
1108                 MACRO_ExecuteMacro(part->link->lpszString);
1109                 break;
1110             default:
1111                 WINE_FIXME("Unknown link cookie %d\n", part->link->cookie);
1112             }
1113         }
1114
1115         if (hPopupWnd)
1116             DestroyWindow(hPopupWnd);
1117         break;
1118
1119     case WM_NCDESTROY:
1120         win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1121
1122         if (hWnd == Globals.hPopupWnd) Globals.hPopupWnd = 0;
1123
1124         bExit = (Globals.wVersion >= 4 && !lstrcmpi(win->lpszName, "main"));
1125
1126         WINHELP_DeleteWindow(win);
1127
1128         if (bExit) MACRO_Exit();
1129
1130         if (!Globals.win_list)
1131             PostQuitMessage(0);
1132         break;
1133     }
1134
1135     return DefWindowProc(hWnd, msg, wParam, lParam);
1136 }
1137
1138 /******************************************************************
1139  *              WINHELP_HistoryWndProc
1140  *
1141  *
1142  */
1143 static LRESULT CALLBACK WINHELP_HistoryWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1144 {
1145     WINHELP_WINDOW*     win;
1146     PAINTSTRUCT         ps;
1147     HDC                 hDc;
1148     TEXTMETRIC          tm;
1149     unsigned int        i;
1150     RECT                r;
1151
1152     switch (msg)
1153     {
1154     case WM_NCCREATE:
1155         win = (WINHELP_WINDOW*)((LPCREATESTRUCT)lParam)->lpCreateParams;
1156         SetWindowLong(hWnd, 0, (LONG)win);
1157         win->hHistoryWnd = hWnd;
1158         break;
1159     case WM_CREATE:
1160         win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1161         hDc = GetDC(hWnd);
1162         GetTextMetrics(hDc, &tm);
1163         GetWindowRect(hWnd, &r);
1164
1165         r.right = r.left + 30 * tm.tmAveCharWidth;
1166         r.bottom = r.top + (sizeof(win->history) / sizeof(win->history[0])) * tm.tmHeight;
1167         AdjustWindowRect(&r, GetWindowLong(hWnd, GWL_STYLE), FALSE);
1168         if (r.left < 0) {r.right -= r.left; r.left = 0;}
1169         if (r.top < 0) {r.bottom -= r.top; r.top = 0;}
1170
1171         MoveWindow(hWnd, r.left, r.top, r.right, r.bottom, TRUE);
1172         ReleaseDC(hWnd, hDc);
1173         break;
1174     case WM_LBUTTONDOWN:
1175         win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1176         hDc = GetDC(hWnd);
1177         GetTextMetrics(hDc, &tm);
1178         i = HIWORD(lParam) / tm.tmHeight;
1179         if (i < win->histIndex)
1180             WINHELP_CreateHelpWindow(win->history[i], win->info, SW_SHOW);
1181         ReleaseDC(hWnd, hDc);
1182         break;
1183     case WM_PAINT:
1184         hDc = BeginPaint(hWnd, &ps);
1185         win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1186         GetTextMetrics(hDc, &tm);
1187
1188         for (i = 0; i < win->histIndex; i++)
1189         {
1190             TextOut(hDc, 0, i * tm.tmHeight, win->history[i]->lpszTitle, 
1191                     strlen(win->history[i]->lpszTitle));
1192         }
1193         EndPaint(hWnd, &ps);
1194         break;
1195     case WM_DESTROY:
1196         win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1197         if (hWnd == win->hHistoryWnd)
1198             win->hHistoryWnd = 0;
1199         break;
1200     }
1201     return DefWindowProc(hWnd, msg, wParam, lParam);
1202 }
1203
1204 /***********************************************************************
1205  *
1206  *           WINHELP_ShadowWndProc
1207  */
1208 static LRESULT CALLBACK WINHELP_ShadowWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1209 {
1210     WINHELP_CheckPopup(msg);
1211     return DefWindowProc(hWnd, msg, wParam, lParam);
1212 }
1213
1214 /***********************************************************************
1215  *
1216  *           SetupText
1217  */
1218 static void WINHELP_SetupText(HWND hWnd)
1219 {
1220     HDC  hDc = GetDC(hWnd);
1221     RECT rect;
1222     SIZE newsize;
1223
1224     ShowScrollBar(hWnd, SB_VERT, FALSE);
1225     if (!WINHELP_SplitLines(hWnd, NULL))
1226     {
1227         ShowScrollBar(hWnd, SB_VERT, TRUE);
1228         GetClientRect(hWnd, &rect);
1229
1230         WINHELP_SplitLines(hWnd, &newsize);
1231         SetScrollRange(hWnd, SB_VERT, 0, rect.top + newsize.cy - rect.bottom, TRUE);
1232     }
1233     else
1234     {
1235         SetScrollPos(hWnd, SB_VERT, 0, FALSE);
1236         SetScrollRange(hWnd, SB_VERT, 0, 0, FALSE);
1237     }
1238
1239     ReleaseDC(hWnd, hDc);
1240 }
1241
1242 /***********************************************************************
1243  *
1244  *           WINHELP_AppendText
1245  */
1246 static BOOL WINHELP_AppendText(WINHELP_LINE ***linep, WINHELP_LINE_PART ***partp,
1247                                LPSIZE space, LPSIZE textsize,
1248                                INT *line_ascent, INT ascent,
1249                                LPCSTR text, UINT textlen,
1250                                HFONT font, COLORREF color, HLPFILE_LINK *link,
1251                                unsigned underline)
1252 {
1253     WINHELP_LINE      *line;
1254     WINHELP_LINE_PART *part;
1255     LPSTR ptr;
1256
1257     if (!*partp) /* New line */
1258     {
1259         *line_ascent  = ascent;
1260
1261         line = HeapAlloc(GetProcessHeap(), 0,
1262                          sizeof(WINHELP_LINE) + textlen);
1263         if (!line) return FALSE;
1264
1265         line->next    = 0;
1266         part          = &line->first_part;
1267         ptr           = (char*)line + sizeof(WINHELP_LINE);
1268
1269         line->rect.top    = (**linep ? (**linep)->rect.bottom : 0) + space->cy;
1270         line->rect.bottom = line->rect.top;
1271         line->rect.left   = space->cx;
1272         line->rect.right  = space->cx;
1273
1274         if (**linep) *linep = &(**linep)->next;
1275         **linep = line;
1276         space->cy = 0;
1277     }
1278     else /* Same line */
1279     {
1280         line = **linep;
1281
1282         if (*line_ascent < ascent)
1283         {
1284             WINHELP_LINE_PART *p;
1285             for (p = &line->first_part; p; p = p->next)
1286             {
1287                 p->rect.top    += ascent - *line_ascent;
1288                 p->rect.bottom += ascent - *line_ascent;
1289             }
1290             line->rect.bottom += ascent - *line_ascent;
1291             *line_ascent = ascent;
1292         }
1293
1294         part = HeapAlloc(GetProcessHeap(), 0,
1295                          sizeof(WINHELP_LINE_PART) + textlen);
1296         if (!part) return FALSE;
1297         **partp = part;
1298         ptr     = (char*)part + sizeof(WINHELP_LINE_PART);
1299     }
1300
1301     memcpy(ptr, text, textlen);
1302     part->cookie            = hlp_line_part_text;
1303     part->rect.left         = line->rect.right + (*partp ? space->cx : 0);
1304     part->rect.right        = part->rect.left + textsize->cx;
1305     line->rect.right        = part->rect.right;
1306     part->rect.top          =
1307         ((*partp) ? line->rect.top : line->rect.bottom) + *line_ascent - ascent;
1308     part->rect.bottom       = part->rect.top + textsize->cy;
1309     line->rect.bottom       = max(line->rect.bottom, part->rect.bottom);
1310     part->u.text.lpsText    = ptr;
1311     part->u.text.wTextLen   = textlen;
1312     part->u.text.hFont      = font;
1313     part->u.text.color      = color;
1314     part->u.text.wUnderline = underline;
1315
1316     WINE_TRACE("Appended text '%*.*s'[%d] @ (%ld,%ld-%ld,%ld)\n",
1317                part->u.text.wTextLen,
1318                part->u.text.wTextLen,
1319                part->u.text.lpsText,
1320                part->u.text.wTextLen,
1321                part->rect.left, part->rect.top, part->rect.right, part->rect.bottom);
1322
1323     part->link = link;
1324     if (link) link->wRefCount++;
1325
1326     part->next          = 0;
1327     *partp              = &part->next;
1328
1329     space->cx = 0;
1330
1331     return TRUE;
1332 }
1333
1334 /***********************************************************************
1335  *
1336  *           WINHELP_AppendGfxObject
1337  */
1338 static WINHELP_LINE_PART* WINHELP_AppendGfxObject(WINHELP_LINE ***linep, WINHELP_LINE_PART ***partp,
1339                                                   LPSIZE space, LPSIZE gfxSize,
1340                                                   HLPFILE_LINK *link, unsigned pos)
1341 {
1342     WINHELP_LINE      *line;
1343     WINHELP_LINE_PART *part;
1344     LPSTR              ptr;
1345
1346     if (!*partp || pos == 1) /* New line */
1347     {
1348         line = HeapAlloc(GetProcessHeap(), 0, sizeof(WINHELP_LINE));
1349         if (!line) return NULL;
1350
1351         line->next    = NULL;
1352         part          = &line->first_part;
1353
1354         line->rect.top    = (**linep ? (**linep)->rect.bottom : 0) + space->cy;
1355         line->rect.bottom = line->rect.top;
1356         line->rect.left   = space->cx;
1357         line->rect.right  = space->cx;
1358
1359         if (**linep) *linep = &(**linep)->next;
1360         **linep = line;
1361         space->cy = 0;
1362         ptr = (char*)line + sizeof(WINHELP_LINE);
1363     }
1364     else /* Same line */
1365     {
1366         if (pos == 2) WINE_FIXME("Left alignment not handled\n");
1367         line = **linep;
1368
1369         part = HeapAlloc(GetProcessHeap(), 0, sizeof(WINHELP_LINE_PART));
1370         if (!part) return NULL;
1371         **partp = part;
1372         ptr = (char*)part + sizeof(WINHELP_LINE_PART);
1373     }
1374
1375     /* part->cookie should be set by caller (image or metafile) */
1376     part->rect.left       = line->rect.right + (*partp ? space->cx : 0);
1377     part->rect.right      = part->rect.left + gfxSize->cx;
1378     line->rect.right      = part->rect.right;
1379     part->rect.top        = (*partp) ? line->rect.top : line->rect.bottom;
1380     part->rect.bottom     = part->rect.top + gfxSize->cy;
1381     line->rect.bottom     = max(line->rect.bottom, part->rect.bottom);
1382
1383     WINE_TRACE("Appended gfx @ (%ld,%ld-%ld,%ld)\n",
1384                part->rect.left, part->rect.top, part->rect.right, part->rect.bottom);
1385
1386     part->link = link;
1387     if (link) link->wRefCount++;
1388
1389     part->next            = NULL;
1390     *partp                = &part->next;
1391
1392     space->cx = 0;
1393
1394     return part;
1395 }
1396
1397
1398 /***********************************************************************
1399  *
1400  *           WINHELP_SplitLines
1401  */
1402 static BOOL WINHELP_SplitLines(HWND hWnd, LPSIZE newsize)
1403 {
1404     WINHELP_WINDOW     *win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1405     HLPFILE_PARAGRAPH  *p;
1406     WINHELP_LINE      **line = &win->first_line;
1407     WINHELP_LINE_PART **part = 0;
1408     INT                 line_ascent = 0;
1409     SIZE                space;
1410     RECT                rect;
1411     HDC                 hDc;
1412
1413     if (newsize) newsize->cx = newsize->cy = 0;
1414
1415     if (!win->page) return TRUE;
1416
1417     WINHELP_DeleteLines(win);
1418
1419     GetClientRect(hWnd, &rect);
1420
1421     rect.top    += INTERNAL_BORDER_WIDTH;
1422     rect.left   += INTERNAL_BORDER_WIDTH;
1423     rect.right  -= INTERNAL_BORDER_WIDTH;
1424     rect.bottom -= INTERNAL_BORDER_WIDTH;
1425
1426     space.cy = rect.top;
1427     space.cx = rect.left;
1428
1429     hDc = GetDC(hWnd);
1430
1431     for (p = win->page->first_paragraph; p; p = p->next)
1432     {
1433         switch (p->cookie)
1434         {
1435         case para_normal_text:
1436         case para_debug_text:
1437             {
1438                 TEXTMETRIC tm;
1439                 SIZE textsize    = {0, 0};
1440                 LPCSTR text      = p->u.text.lpszText;
1441                 UINT indent      = 0;
1442                 UINT len         = strlen(text);
1443                 unsigned underline = 0;
1444
1445                 HFONT hFont = 0;
1446                 COLORREF color = RGB(0, 0, 0);
1447
1448                 if (p->u.text.wFont < win->page->file->numFonts)
1449                 {
1450                     HLPFILE*    hlpfile = win->page->file;
1451
1452                     if (!hlpfile->fonts[p->u.text.wFont].hFont)
1453                         hlpfile->fonts[p->u.text.wFont].hFont = CreateFontIndirect(&hlpfile->fonts[p->u.text.wFont].LogFont);
1454                     hFont = hlpfile->fonts[p->u.text.wFont].hFont;
1455                     color = hlpfile->fonts[p->u.text.wFont].color;
1456                 }
1457                 else
1458                 {
1459                     UINT  wFont = (p->u.text.wFont < win->fonts_len) ? p->u.text.wFont : 0;
1460
1461                     hFont = win->fonts[wFont];
1462                 }
1463
1464                 if (p->link && p->link->bClrChange)
1465                 {
1466                     underline = (p->link->cookie == hlp_link_popup) ? 3 : 1;
1467                     color = RGB(0, 0x80, 0);
1468                 }
1469                 if (p->cookie == para_debug_text) color = RGB(0xff, 0, 0);
1470
1471                 SelectObject(hDc, hFont);
1472
1473                 GetTextMetrics(hDc, &tm);
1474
1475                 if (p->u.text.wIndent)
1476                 {
1477                     indent = p->u.text.wIndent * 5 * tm.tmAveCharWidth;
1478                     if (!part)
1479                         space.cx = rect.left + indent - 2 * tm.tmAveCharWidth;
1480                 }
1481
1482                 if (p->u.text.wVSpace)
1483                 {
1484                     part = 0;
1485                     space.cx = rect.left + indent;
1486                     space.cy += (p->u.text.wVSpace - 1) * tm.tmHeight;
1487                 }
1488
1489                 if (p->u.text.wHSpace)
1490                 {
1491                     space.cx += p->u.text.wHSpace * 2 * tm.tmAveCharWidth;
1492                 }
1493
1494                 WINE_TRACE("splitting text %s\n", text);
1495
1496                 while (len)
1497                 {
1498                     INT free_width = rect.right - (part ? (*line)->rect.right : rect.left) - space.cx;
1499                     UINT low = 0, curr = len, high = len, textlen = 0;
1500
1501                     if (free_width > 0)
1502                     {
1503                         while (1)
1504                         {
1505                             GetTextExtentPoint(hDc, text, curr, &textsize);
1506
1507                             if (textsize.cx <= free_width) low = curr;
1508                             else high = curr;
1509
1510                             if (high <= low + 1) break;
1511
1512                             if (textsize.cx) curr = (curr * free_width) / textsize.cx;
1513                             if (curr <= low) curr = low + 1;
1514                             else if (curr >= high) curr = high - 1;
1515                         }
1516                         textlen = low;
1517                         while (textlen && text[textlen] && text[textlen] != ' ') textlen--;
1518                     }
1519                     if (!part && !textlen) textlen = max(low, 1);
1520
1521                     if (free_width <= 0 || !textlen)
1522                     {
1523                         part = 0;
1524                         space.cx = rect.left + indent;
1525                         space.cx = min(space.cx, rect.right - rect.left - 1);
1526                         continue;
1527                     }
1528
1529                     WINE_TRACE("\t => %d %*s\n", textlen, textlen, text);
1530
1531                     if (!WINHELP_AppendText(&line, &part, &space, &textsize,
1532                                             &line_ascent, tm.tmAscent,
1533                                             text, textlen, hFont, color, p->link, underline) ||
1534                         (!newsize && (*line)->rect.bottom > rect.bottom))
1535                     {
1536                         ReleaseDC(hWnd, hDc);
1537                         return FALSE;
1538                     }
1539
1540                     if (newsize)
1541                         newsize->cx = max(newsize->cx, (*line)->rect.right + INTERNAL_BORDER_WIDTH);
1542
1543                     len -= textlen;
1544                     text += textlen;
1545                     if (text[0] == ' ') text++, len--;
1546                 }
1547             }
1548             break;
1549         case para_bitmap:
1550         case para_metafile:
1551             {
1552                 SIZE                    gfxSize;
1553                 INT                     free_width;
1554                 WINHELP_LINE_PART*      ref_part;
1555
1556                 if (p->u.gfx.pos & 0x8000)
1557                 {
1558                     space.cx = rect.left;
1559                     if (*line)
1560                         space.cy += (*line)->rect.bottom - (*line)->rect.top;
1561                     part = 0;
1562                 }
1563
1564                 if (p->cookie == para_bitmap)
1565                 {
1566                     DIBSECTION              dibs;
1567                     
1568                     GetObject(p->u.gfx.u.bmp.hBitmap, sizeof(dibs), &dibs);
1569                     gfxSize.cx = dibs.dsBm.bmWidth;
1570                     gfxSize.cy = dibs.dsBm.bmHeight;
1571                 }
1572                 else gfxSize = p->u.gfx.u.mf.mfSize;
1573                     
1574                 free_width = rect.right - ((part && *line) ? (*line)->rect.right : rect.left) - space.cx;
1575                 if (free_width <= 0)
1576                 {
1577                     part = NULL;
1578                     space.cx = rect.left;
1579                     space.cx = min(space.cx, rect.right - rect.left - 1);
1580                 }
1581                 ref_part = WINHELP_AppendGfxObject(&line, &part, &space, &gfxSize,
1582                                                    p->link, p->u.gfx.pos);
1583                 if (!ref_part || (!newsize && (*line)->rect.bottom > rect.bottom))
1584                 {
1585                     return FALSE;
1586                 }
1587                 if (p->cookie == para_bitmap)
1588                 {
1589                     ref_part->cookie = hlp_line_part_bitmap;
1590                     ref_part->u.bitmap.hBitmap = p->u.gfx.u.bmp.hBitmap;
1591                 }
1592                 else
1593                 {
1594                     ref_part->cookie = hlp_line_part_metafile;
1595                     ref_part->u.metafile.hMetaFile = p->u.gfx.u.mf.hMetaFile;
1596                 }
1597             }
1598             break;
1599         }
1600     }
1601
1602     if (newsize)
1603         newsize->cy = (*line)->rect.bottom + INTERNAL_BORDER_WIDTH;
1604
1605     ReleaseDC(hWnd, hDc);
1606     return TRUE;
1607 }
1608
1609 /***********************************************************************
1610  *
1611  *           WINHELP_CheckPopup
1612  */
1613 static void WINHELP_CheckPopup(UINT msg)
1614 {
1615     if (!Globals.hPopupWnd) return;
1616
1617     switch (msg)
1618     {
1619     case WM_COMMAND:
1620     case WM_LBUTTONDOWN:
1621     case WM_MBUTTONDOWN:
1622     case WM_RBUTTONDOWN:
1623     case WM_NCLBUTTONDOWN:
1624     case WM_NCMBUTTONDOWN:
1625     case WM_NCRBUTTONDOWN:
1626         DestroyWindow(Globals.hPopupWnd);
1627         Globals.hPopupWnd = 0;
1628     }
1629 }
1630
1631 /***********************************************************************
1632  *
1633  *           WINHELP_DeleteLines
1634  */
1635 static void WINHELP_DeleteLines(WINHELP_WINDOW *win)
1636 {
1637     WINHELP_LINE      *line, *next_line;
1638     WINHELP_LINE_PART *part, *next_part;
1639     for (line = win->first_line; line; line = next_line)
1640     {
1641         next_line = line->next;
1642         for (part = &line->first_part; part; part = next_part)
1643         {
1644             next_part = part->next;
1645             HLPFILE_FreeLink(part->link);
1646             HeapFree(GetProcessHeap(), 0, part);
1647         }
1648     }
1649     win->first_line = 0;
1650 }
1651
1652 /***********************************************************************
1653  *
1654  *           WINHELP_DeleteWindow
1655  */
1656 static void WINHELP_DeleteWindow(WINHELP_WINDOW* win)
1657 {
1658     WINHELP_WINDOW**    w;
1659     unsigned int        i;
1660     WINHELP_BUTTON*     b;
1661     WINHELP_BUTTON*     bp;
1662
1663     for (w = &Globals.win_list; *w; w = &(*w)->next)
1664     {
1665         if (*w == win)
1666         {
1667             *w = win->next;
1668             break;
1669         }
1670     }
1671
1672     for (b = win->first_button; b; b = bp)
1673     {
1674         DestroyWindow(b->hWnd);
1675         bp = b->next;
1676         HeapFree(GetProcessHeap(), 0, b);
1677     }
1678
1679     if (win->hShadowWnd) DestroyWindow(win->hShadowWnd);
1680     if (win->hHistoryWnd) DestroyWindow(win->hHistoryWnd);
1681
1682     for (i = 0; i < win->histIndex; i++)
1683     {
1684         HLPFILE_FreeHlpFile(win->history[i]->file);
1685     }
1686
1687     for (i = 0; i < win->backIndex; i++)
1688         HLPFILE_FreeHlpFile(win->back[i]->file);
1689
1690     if (win->page) HLPFILE_FreeHlpFile(win->page->file);
1691     WINHELP_DeleteLines(win);
1692     HeapFree(GetProcessHeap(), 0, win);
1693 }
1694
1695 /***********************************************************************
1696  *
1697  *           WINHELP_InitFonts
1698  */
1699 static void WINHELP_InitFonts(HWND hWnd)
1700 {
1701     WINHELP_WINDOW *win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1702     LOGFONT logfontlist[] = {
1703         {-10, 0, 0, 0, 400, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1704         {-12, 0, 0, 0, 700, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1705         {-12, 0, 0, 0, 700, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1706         {-12, 0, 0, 0, 400, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1707         {-12, 0, 0, 0, 700, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1708         {-10, 0, 0, 0, 700, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"},
1709         { -8, 0, 0, 0, 400, 0, 0, 0, 0, 0, 0, 0, 32, "Helv"}};
1710 #define FONTS_LEN (sizeof(logfontlist)/sizeof(*logfontlist))
1711
1712     static HFONT fonts[FONTS_LEN];
1713     static BOOL init = 0;
1714
1715     win->fonts_len = FONTS_LEN;
1716     win->fonts = fonts;
1717
1718     if (!init)
1719     {
1720         UINT i;
1721
1722         for (i = 0; i < FONTS_LEN; i++)
1723         {
1724             fonts[i] = CreateFontIndirect(&logfontlist[i]);
1725         }
1726
1727         init = 1;
1728     }
1729 }
1730
1731 /***********************************************************************
1732  *
1733  *           WINHELP_MessageBoxIDS
1734  */
1735 INT WINHELP_MessageBoxIDS(UINT ids_text, UINT ids_title, WORD type)
1736 {
1737     CHAR text[MAX_STRING_LEN];
1738     CHAR title[MAX_STRING_LEN];
1739
1740     LoadString(Globals.hInstance, ids_text, text, sizeof(text));
1741     LoadString(Globals.hInstance, ids_title, title, sizeof(title));
1742
1743     return MessageBox(0, text, title, type);
1744 }
1745
1746 /***********************************************************************
1747  *
1748  *           MAIN_MessageBoxIDS_s
1749  */
1750 INT WINHELP_MessageBoxIDS_s(UINT ids_text, LPCSTR str, UINT ids_title, WORD type)
1751 {
1752     CHAR text[MAX_STRING_LEN];
1753     CHAR title[MAX_STRING_LEN];
1754     CHAR newtext[MAX_STRING_LEN + MAX_PATHNAME_LEN];
1755
1756     LoadString(Globals.hInstance, ids_text, text, sizeof(text));
1757     LoadString(Globals.hInstance, ids_title, title, sizeof(title));
1758     wsprintf(newtext, text, str);
1759
1760     return MessageBox(0, newtext, title, type);
1761 }
1762
1763 /******************************************************************
1764  *              WINHELP_IsOverLink
1765  *
1766  *
1767  */
1768 WINHELP_LINE_PART* WINHELP_IsOverLink(WINHELP_WINDOW* win, WPARAM wParam, LPARAM lParam)
1769 {
1770     POINT mouse;
1771     WINHELP_LINE      *line;
1772     WINHELP_LINE_PART *part;
1773     int scroll_pos = GetScrollPos(win->hTextWnd, SB_VERT);
1774
1775     mouse.x = LOWORD(lParam);
1776     mouse.y = HIWORD(lParam);
1777     for (line = win->first_line; line; line = line->next)
1778     {
1779         for (part = &line->first_part; part; part = part->next)
1780         {
1781             if (part->link && 
1782                 part->link->lpszString &&
1783                 part->rect.left   <= mouse.x &&
1784                 part->rect.right  >= mouse.x &&
1785                 part->rect.top    <= mouse.y + scroll_pos &&
1786                 part->rect.bottom >= mouse.y + scroll_pos)
1787             {
1788                 return part;
1789             }
1790         }
1791     }
1792
1793     return NULL;
1794 }