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