winhelp: Start feeding the richedit control with relevant RTF stream.
[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, 2008 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 "commdlg.h"
35 #include "winhelp.h"
36 #include "winhelp_res.h"
37 #include "shellapi.h"
38 #include "richedit.h"
39
40 #include "wine/debug.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(winhelp);
43
44 static BOOL    WINHELP_RegisterWinClasses(void);
45 static LRESULT CALLBACK WINHELP_MainWndProc(HWND, UINT, WPARAM, LPARAM);
46 static LRESULT CALLBACK WINHELP_TextWndProc(HWND, UINT, WPARAM, LPARAM);
47 static LRESULT CALLBACK WINHELP_ButtonBoxWndProc(HWND, UINT, WPARAM, LPARAM);
48 static LRESULT CALLBACK WINHELP_ButtonWndProc(HWND, UINT, WPARAM, LPARAM);
49 static LRESULT CALLBACK WINHELP_HistoryWndProc(HWND, UINT, WPARAM, LPARAM);
50 static LRESULT CALLBACK WINHELP_ShadowWndProc(HWND, UINT, WPARAM, LPARAM);
51 static void    WINHELP_CheckPopup(UINT);
52 static BOOL    WINHELP_SplitLines(HWND hWnd, LPSIZE);
53 static void    WINHELP_InitFonts(HWND hWnd);
54 static void    WINHELP_DeleteLines(WINHELP_WINDOW*);
55 static void    WINHELP_DeleteWindow(WINHELP_WINDOW*);
56 static void    WINHELP_DeleteButtons(WINHELP_WINDOW*);
57 static void    WINHELP_SetupText(HWND hWnd, WINHELP_WINDOW *win, ULONG relative);
58 static WINHELP_LINE_PART* WINHELP_IsOverLink(WINHELP_WINDOW*, WPARAM, LPARAM);
59
60 WINHELP_GLOBALS Globals = {3, NULL, TRUE, NULL, NULL, NULL, NULL, NULL, {{{NULL,NULL}},0}};
61
62 static BOOL use_richedit;
63
64 #define CTL_ID_BUTTON   0x700
65 #define CTL_ID_TEXT     0x701
66
67 /***********************************************************************
68  *
69  *           WINHELP_GetOpenFileName
70  */
71 BOOL WINHELP_GetOpenFileName(LPSTR lpszFile, int len)
72 {
73     OPENFILENAME openfilename;
74     CHAR szDir[MAX_PATH];
75     CHAR szzFilter[2 * MAX_STRING_LEN + 100];
76     LPSTR p = szzFilter;
77
78     WINE_TRACE("()\n");
79
80     LoadString(Globals.hInstance, STID_HELP_FILES_HLP, p, MAX_STRING_LEN);
81     p += strlen(p) + 1;
82     lstrcpy(p, "*.hlp");
83     p += strlen(p) + 1;
84     LoadString(Globals.hInstance, STID_ALL_FILES, p, MAX_STRING_LEN);
85     p += strlen(p) + 1;
86     lstrcpy(p, "*.*");
87     p += strlen(p) + 1;
88     *p = '\0';
89
90     GetCurrentDirectory(sizeof(szDir), szDir);
91
92     lpszFile[0]='\0';
93
94     openfilename.lStructSize       = sizeof(OPENFILENAME);
95     openfilename.hwndOwner         = NULL;
96     openfilename.hInstance         = Globals.hInstance;
97     openfilename.lpstrFilter       = szzFilter;
98     openfilename.lpstrCustomFilter = 0;
99     openfilename.nMaxCustFilter    = 0;
100     openfilename.nFilterIndex      = 1;
101     openfilename.lpstrFile         = lpszFile;
102     openfilename.nMaxFile          = len;
103     openfilename.lpstrFileTitle    = 0;
104     openfilename.nMaxFileTitle     = 0;
105     openfilename.lpstrInitialDir   = szDir;
106     openfilename.lpstrTitle        = 0;
107     openfilename.Flags             = 0;
108     openfilename.nFileOffset       = 0;
109     openfilename.nFileExtension    = 0;
110     openfilename.lpstrDefExt       = 0;
111     openfilename.lCustData         = 0;
112     openfilename.lpfnHook          = 0;
113     openfilename.lpTemplateName    = 0;
114
115     return GetOpenFileName(&openfilename);
116 }
117
118 static char* WINHELP_GetCaption(WINHELP_WNDPAGE* wpage)
119 {
120     if (wpage->wininfo->caption[0]) return wpage->wininfo->caption;
121     return wpage->page->file->lpszTitle;
122 }
123
124 /***********************************************************************
125  *
126  *           WINHELP_LookupHelpFile
127  */
128 HLPFILE* WINHELP_LookupHelpFile(LPCSTR lpszFile)
129 {
130     HLPFILE*        hlpfile;
131     char szFullName[MAX_PATH];
132     char szAddPath[MAX_PATH];
133     char *p;
134
135     /*
136      * NOTE: This is needed by popup windows only.
137      * In other cases it's not needed but does not hurt though.
138      */
139     if (Globals.active_win && Globals.active_win->page && Globals.active_win->page->file)
140     {
141         strcpy(szAddPath, Globals.active_win->page->file->lpszPath);
142         p = strrchr(szAddPath, '\\');
143         if (p) *p = 0;
144     }
145
146     /*
147      * FIXME: Should we swap conditions?
148      */
149     if (!SearchPath(NULL, lpszFile, ".hlp", MAX_PATH, szFullName, NULL) &&
150         !SearchPath(szAddPath, lpszFile, ".hlp", MAX_PATH, szFullName, NULL))
151     {
152         if (WINHELP_MessageBoxIDS_s(STID_FILE_NOT_FOUND_s, lpszFile, STID_WHERROR,
153                                     MB_YESNO|MB_ICONQUESTION) != IDYES)
154             return NULL;
155         if (!WINHELP_GetOpenFileName(szFullName, MAX_PATH))
156             return NULL;
157     }
158     hlpfile = HLPFILE_ReadHlpFile(szFullName);
159     if (!hlpfile)
160         WINHELP_MessageBoxIDS_s(STID_HLPFILE_ERROR_s, lpszFile,
161                                 STID_WHERROR, MB_OK|MB_ICONSTOP);
162     return hlpfile;
163 }
164
165 /******************************************************************
166  *              WINHELP_GetWindowInfo
167  *
168  *
169  */
170 HLPFILE_WINDOWINFO*     WINHELP_GetWindowInfo(HLPFILE* hlpfile, LPCSTR name)
171 {
172     static      HLPFILE_WINDOWINFO      mwi;
173     unsigned int     i;
174
175     if (!name || !name[0])
176         name = Globals.active_win->lpszName;
177
178     if (hlpfile)
179         for (i = 0; i < hlpfile->numWindows; i++)
180             if (!strcmp(hlpfile->windows[i].name, name))
181                 return &hlpfile->windows[i];
182
183     if (strcmp(name, "main") != 0)
184     {
185         WINE_FIXME("Couldn't find window info for %s\n", name);
186         assert(0);
187         return NULL;
188     }
189     if (!mwi.name[0])
190     {
191         strcpy(mwi.type, "primary");
192         strcpy(mwi.name, "main");
193         if (!LoadString(Globals.hInstance, STID_WINE_HELP, 
194                         mwi.caption, sizeof(mwi.caption)))
195             strcpy(mwi.caption, hlpfile->lpszTitle);
196         mwi.origin.x = mwi.origin.y = mwi.size.cx = mwi.size.cy = CW_USEDEFAULT;
197         mwi.style = SW_SHOW;
198         mwi.win_style = WS_OVERLAPPEDWINDOW;
199         mwi.sr_color = mwi.sr_color = 0xFFFFFF;
200     }
201     return &mwi;
202 }
203
204 /******************************************************************
205  *              HLPFILE_GetPopupWindowInfo
206  *
207  *
208  */
209 static HLPFILE_WINDOWINFO*     WINHELP_GetPopupWindowInfo(HLPFILE* hlpfile,
210                                                           WINHELP_WINDOW* parent, POINT* mouse)
211 {
212     static      HLPFILE_WINDOWINFO      wi;
213
214     RECT parent_rect;
215     
216     wi.type[0] = wi.name[0] = wi.caption[0] = '\0';
217
218     /* Calculate horizontal size and position of a popup window */
219     GetWindowRect(parent->hMainWnd, &parent_rect);
220     wi.size.cx = (parent_rect.right  - parent_rect.left) / 2;
221     wi.size.cy = 10; /* need a non null value, so that border are taken into account while computing */
222
223     wi.origin = *mouse;
224     ClientToScreen(parent->hMainWnd, &wi.origin);
225     wi.origin.x -= wi.size.cx / 2;
226     wi.origin.x  = min(wi.origin.x, GetSystemMetrics(SM_CXSCREEN) - wi.size.cx);
227     wi.origin.x  = max(wi.origin.x, 0);
228
229     wi.style = SW_SHOW;
230     wi.win_style = WS_POPUP | WS_BORDER;
231     wi.sr_color = parent->info->sr_color;
232     wi.nsr_color = 0xFFFFFF;
233
234     return &wi;
235 }
236
237 /***********************************************************************
238  *
239  *           WinMain
240  */
241 int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
242 {
243     MSG                 msg;
244     LONG                lHash = 0;
245     HLPFILE*            hlpfile;
246     static CHAR         default_wndname[] = "main";
247     LPSTR               wndname = default_wndname;
248     WINHELP_DLL*        dll;
249
250     Globals.hInstance = hInstance;
251
252     use_richedit = getenv("WINHELP_RICHEDIT") != NULL;
253     if (use_richedit && LoadLibrary("riched20.dll") == NULL)
254         return MessageBox(0, MAKEINTRESOURCE(STID_NO_RICHEDIT),
255                           MAKEINTRESOURCE(STID_WHERROR), MB_OK);
256
257     /* Get options */
258     while (*cmdline && (*cmdline == ' ' || *cmdline == '-'))
259     {
260         CHAR   option;
261         LPCSTR topic_id;
262         if (*cmdline++ == ' ') continue;
263
264         option = *cmdline;
265         if (option) cmdline++;
266         while (*cmdline && *cmdline == ' ') cmdline++;
267         switch (option)
268         {
269         case 'i':
270         case 'I':
271             topic_id = cmdline;
272             while (*cmdline && *cmdline != ' ') cmdline++;
273             if (*cmdline) *cmdline++ = '\0';
274             lHash = HLPFILE_Hash(topic_id);
275             break;
276
277         case '3':
278         case '4':
279             Globals.wVersion = option - '0';
280             break;
281
282         case 'x':
283             show = SW_HIDE; 
284             Globals.isBook = FALSE;
285             break;
286
287         default:
288             WINE_FIXME("Unsupported cmd line: %s\n", cmdline);
289             break;
290         }
291     }
292
293     /* Create primary window */
294     if (!WINHELP_RegisterWinClasses())
295     {
296         WINE_FIXME("Couldn't register classes\n");
297         return 0;
298     }
299
300     if (*cmdline)
301     {
302         char*   ptr;
303         if ((*cmdline == '"') && (ptr = strchr(cmdline+1, '"')))
304         {
305             cmdline++;
306             *ptr = '\0';
307         }
308         if ((ptr = strchr(cmdline, '>')))
309         {
310             *ptr = '\0';
311             wndname = ptr + 1;
312         }
313         hlpfile = WINHELP_LookupHelpFile(cmdline);
314         if (!hlpfile) return 0;
315     }
316     else hlpfile = NULL;
317     WINHELP_OpenHelpWindow(HLPFILE_PageByHash, hlpfile, lHash,
318                            WINHELP_GetWindowInfo(hlpfile, wndname), show);
319
320     /* Message loop */
321     while (GetMessage(&msg, 0, 0, 0))
322     {
323         TranslateMessage(&msg);
324         DispatchMessage(&msg);
325     }
326     for (dll = Globals.dlls; dll; dll = dll->next)
327     {
328         if (dll->class & DC_INITTERM) dll->handler(DW_TERM, 0, 0);
329     }
330     return 0;
331 }
332
333 /***********************************************************************
334  *
335  *           RegisterWinClasses
336  */
337 static BOOL WINHELP_RegisterWinClasses(void)
338 {
339     WNDCLASS class_main, class_button_box, class_text, class_shadow, class_history;
340
341     class_main.style               = CS_HREDRAW | CS_VREDRAW;
342     class_main.lpfnWndProc         = WINHELP_MainWndProc;
343     class_main.cbClsExtra          = 0;
344     class_main.cbWndExtra          = sizeof(LONG);
345     class_main.hInstance           = Globals.hInstance;
346     class_main.hIcon               = LoadIcon(Globals.hInstance, MAKEINTRESOURCE(IDI_WINHELP));
347     class_main.hCursor             = LoadCursor(0, IDC_ARROW);
348     class_main.hbrBackground       = (HBRUSH)(COLOR_WINDOW+1);
349     class_main.lpszMenuName        = 0;
350     class_main.lpszClassName       = MAIN_WIN_CLASS_NAME;
351
352     class_button_box               = class_main;
353     class_button_box.lpfnWndProc   = WINHELP_ButtonBoxWndProc;
354     class_button_box.cbWndExtra    = 0;
355     class_button_box.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
356     class_button_box.lpszClassName = BUTTON_BOX_WIN_CLASS_NAME;
357
358     class_text                     = class_main;
359     class_text.lpfnWndProc         = WINHELP_TextWndProc;
360     class_text.hbrBackground       = 0;
361     class_text.lpszClassName       = TEXT_WIN_CLASS_NAME;
362
363     class_shadow                   = class_main;
364     class_shadow.lpfnWndProc       = WINHELP_ShadowWndProc;
365     class_shadow.cbWndExtra        = 0;
366     class_shadow.hbrBackground     = (HBRUSH)(COLOR_3DDKSHADOW+1);
367     class_shadow.lpszClassName     = SHADOW_WIN_CLASS_NAME;
368
369     class_history                  = class_main;
370     class_history.lpfnWndProc      = WINHELP_HistoryWndProc;
371     class_history.lpszClassName    = HISTORY_WIN_CLASS_NAME;
372
373     return (RegisterClass(&class_main) &&
374             RegisterClass(&class_button_box) &&
375             RegisterClass(&class_text) &&
376             RegisterClass(&class_shadow) &&
377             RegisterClass(&class_history));
378 }
379
380 typedef struct
381 {
382     WORD size;
383     WORD command;
384     LONG data;
385     LONG reserved;
386     WORD ofsFilename;
387     WORD ofsData;
388 } WINHELP,*LPWINHELP;
389
390 /******************************************************************
391  *              WINHELP_HandleCommand
392  *
393  *
394  */
395 static LRESULT  WINHELP_HandleCommand(HWND hSrcWnd, LPARAM lParam)
396 {
397     COPYDATASTRUCT*     cds = (COPYDATASTRUCT*)lParam;
398     WINHELP*            wh;
399
400     if (cds->dwData != 0xA1DE505)
401     {
402         WINE_FIXME("Wrong magic number (%08lx)\n", cds->dwData);
403         return 0;
404     }
405
406     wh = (WINHELP*)cds->lpData;
407
408     if (wh)
409     {
410         char*   ptr = (wh->ofsFilename) ? (LPSTR)wh + wh->ofsFilename : NULL;
411
412         WINE_TRACE("Got[%u]: cmd=%u data=%08x fn=%s\n",
413                    wh->size, wh->command, wh->data, ptr);
414         switch (wh->command)
415         {
416         case HELP_CONTEXT:
417             if (ptr)
418             {
419                 MACRO_JumpContext(ptr, "main", wh->data);
420             }
421             break;
422         case HELP_QUIT:
423             MACRO_Exit();
424             break;
425         case HELP_CONTENTS:
426             if (ptr)
427             {
428                 MACRO_JumpContents(ptr, "main");
429             }
430             break;
431         case HELP_HELPONHELP:
432             MACRO_HelpOn();
433             break;
434         /* case HELP_SETINDEX: */
435         case HELP_SETCONTENTS:
436             if (ptr)
437             {
438                 MACRO_SetContents(ptr, wh->data);
439             }
440             break;
441         case HELP_CONTEXTPOPUP:
442             if (ptr)
443             {
444                 MACRO_PopupContext(ptr, wh->data);
445             }
446             break;
447         /* case HELP_FORCEFILE:*/
448         /* case HELP_CONTEXTMENU: */
449         case HELP_FINDER:
450             /* in fact, should be the topic dialog box */
451             WINE_FIXME("HELP_FINDER: stub\n");
452             if (ptr)
453             {
454                 MACRO_JumpHash(ptr, "main", 0);
455             }
456             break;
457         /* case HELP_WM_HELP: */
458         /* case HELP_SETPOPUP_POS: */
459         /* case HELP_KEY: */
460         /* case HELP_COMMAND: */
461         /* case HELP_PARTIALKEY: */
462         /* case HELP_MULTIKEY: */
463         /* case HELP_SETWINPOS: */
464         default:
465             WINE_FIXME("Unhandled command (%x) for remote winhelp control\n", wh->command);
466             break;
467         }
468     }
469     /* Always return success for now */
470     return 1;
471 }
472
473 void            WINHELP_LayoutMainWindow(WINHELP_WINDOW* win)
474 {
475     RECT        rect, button_box_rect;
476     INT         text_top = 0;
477     HWND        hButtonBoxWnd = GetDlgItem(win->hMainWnd, CTL_ID_BUTTON);
478     HWND        hTextWnd = GetDlgItem(win->hMainWnd, CTL_ID_TEXT);
479
480     GetClientRect(win->hMainWnd, &rect);
481
482     /* Update button box and text Window */
483     SetWindowPos(hButtonBoxWnd, HWND_TOP,
484                  rect.left, rect.top,
485                  rect.right - rect.left,
486                  rect.bottom - rect.top, 0);
487
488     if (GetWindowRect(hButtonBoxWnd, &button_box_rect))
489         text_top = rect.top + button_box_rect.bottom - button_box_rect.top;
490
491     SetWindowPos(hTextWnd, HWND_TOP,
492                  rect.left, text_top,
493                  rect.right - rect.left,
494                  rect.bottom - text_top, 0);
495
496 }
497
498 static void     WINHELP_RememberPage(WINHELP_WINDOW* win, WINHELP_WNDPAGE* wpage)
499 {
500     unsigned        num;
501
502     if (!Globals.history.index || Globals.history.set[0].page != wpage->page)
503     {
504         num = sizeof(Globals.history.set) / sizeof(Globals.history.set[0]);
505         /* we're full, remove latest entry */
506         if (Globals.history.index == num)
507         {
508             HLPFILE_FreeHlpFile(Globals.history.set[num - 1].page->file);
509             Globals.history.index--;
510         }
511         memmove(&Globals.history.set[1], &Globals.history.set[0],
512                 Globals.history.index * sizeof(Globals.history.set[0]));
513         Globals.history.set[0] = *wpage;
514         Globals.history.index++;
515         wpage->page->file->wRefCount++;
516     }
517     if (win->hHistoryWnd) InvalidateRect(win->hHistoryWnd, NULL, TRUE);
518
519     num = sizeof(win->back.set) / sizeof(win->back.set[0]);
520     if (win->back.index == num)
521     {
522         /* we're full, remove latest entry */
523         HLPFILE_FreeHlpFile(win->back.set[0].page->file);
524         memmove(&win->back.set[0], &win->back.set[1],
525                 (num - 1) * sizeof(win->back.set[0]));
526         win->back.index--;
527     }
528     win->back.set[win->back.index++] = *wpage;
529     wpage->page->file->wRefCount++;
530 }
531
532 /***********************************************************************
533  *
534  *           WINHELP_CreateHelpWindow
535  */
536 BOOL WINHELP_CreateHelpWindow(WINHELP_WNDPAGE* wpage, int nCmdShow, BOOL remember)
537 {
538     WINHELP_WINDOW*     win = NULL;
539     BOOL                bPrimary, bPopup, bReUsed = FALSE;
540     LPSTR               name;
541     HICON               hIcon;
542     HWND                hTextWnd = NULL;
543
544     bPrimary = !lstrcmpi(wpage->wininfo->name, "main");
545     bPopup = !bPrimary && (wpage->wininfo->win_style & WS_POPUP);
546
547     if (wpage->page && !wpage->page->first_paragraph) HLPFILE_BrowsePage(wpage->page, NULL);
548
549     if (!bPopup)
550     {
551         for (win = Globals.win_list; win; win = win->next)
552         {
553             if (!lstrcmpi(win->lpszName, wpage->wininfo->name))
554             {
555                 POINT   pt = {0, 0};
556                 SIZE    sz = {0, 0};
557                 DWORD   flags = SWP_NOSIZE | SWP_NOMOVE;
558
559                 WINHELP_DeleteButtons(win);
560                 bReUsed = TRUE;
561                 SetWindowText(win->hMainWnd, WINHELP_GetCaption(wpage));
562                 if (wpage->wininfo->origin.x != CW_USEDEFAULT &&
563                     wpage->wininfo->origin.y != CW_USEDEFAULT)
564                 {
565                     pt = wpage->wininfo->origin;
566                     flags &= ~SWP_NOSIZE;
567                 }
568                 if (wpage->wininfo->size.cx != CW_USEDEFAULT &&
569                     wpage->wininfo->size.cy != CW_USEDEFAULT)
570                 {
571                     sz = wpage->wininfo->size;
572                     flags &= ~SWP_NOMOVE;
573                 }
574                 SetWindowPos(win->hMainWnd, HWND_TOP, pt.x, pt.y, sz.cx, sz.cy, flags);
575
576                 if (wpage->page && wpage->page->file != win->page->file)
577                     WINHELP_DeleteBackSet(win);
578                 WINHELP_InitFonts(win->hMainWnd);
579
580                 win->page = wpage->page;
581                 win->info = wpage->wininfo;
582                 hTextWnd = GetDlgItem(win->hMainWnd, CTL_ID_TEXT);
583                 WINHELP_SetupText(hTextWnd, win, wpage->relative);
584
585                 InvalidateRect(win->hMainWnd, NULL, TRUE);
586                 if (win->hHistoryWnd) InvalidateRect(win->hHistoryWnd, NULL, TRUE);
587                 break;
588             }
589         }
590     }
591
592     if (!win)
593     {
594         /* Initialize WINHELP_WINDOW struct */
595         win = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
596                         sizeof(WINHELP_WINDOW) + strlen(wpage->wininfo->name) + 1);
597         if (!win) return FALSE;
598         win->next = Globals.win_list;
599         Globals.win_list = win;
600
601         name = (char*)win + sizeof(WINHELP_WINDOW);
602         lstrcpy(name, wpage->wininfo->name);
603         win->lpszName = name;
604         win->hHandCur = LoadCursorW(0, (LPWSTR)IDC_HAND);
605         win->back.index = 0;
606     }
607     win->page = wpage->page;
608     win->info = wpage->wininfo;
609
610     win->hArrowCur = LoadCursorA(0, (LPSTR)IDC_ARROW);
611     win->hHandCur = LoadCursorA(0, (LPSTR)IDC_HAND);
612
613     if (!bPopup && wpage->page && remember)
614     {
615         WINHELP_RememberPage(win, wpage);
616     }
617
618     if (bPopup)
619         Globals.active_popup = win;
620     else
621         Globals.active_win = win;
622
623     /* Initialize default pushbuttons */
624     if (bPrimary && wpage->page)
625     {
626         CHAR    buffer[MAX_STRING_LEN];
627
628         LoadString(Globals.hInstance, STID_CONTENTS, buffer, sizeof(buffer));
629         MACRO_CreateButton("BTN_CONTENTS", buffer, "Contents()");
630         LoadString(Globals.hInstance, STID_SEARCH,buffer, sizeof(buffer));
631         MACRO_CreateButton("BTN_SEARCH", buffer, "Search()");
632         LoadString(Globals.hInstance, STID_BACK, buffer, sizeof(buffer));
633         MACRO_CreateButton("BTN_BACK", buffer, "Back()");
634         if (win->back.index <= 1) MACRO_DisableButton("BTN_BACK");
635         LoadString(Globals.hInstance, STID_HISTORY, buffer, sizeof(buffer));
636         MACRO_CreateButton("BTN_HISTORY", buffer, "History()");
637         LoadString(Globals.hInstance, STID_TOPICS, buffer, sizeof(buffer));
638         MACRO_CreateButton("BTN_TOPICS", buffer, "Finder()");
639     }
640
641     if (!bReUsed)
642     {
643         win->hMainWnd = CreateWindowEx((bPopup) ? WS_EX_TOOLWINDOW : 0, MAIN_WIN_CLASS_NAME,
644                                        WINHELP_GetCaption(wpage),
645                                        bPrimary ? WS_OVERLAPPEDWINDOW : wpage->wininfo->win_style,
646                                        wpage->wininfo->origin.x, wpage->wininfo->origin.y,
647                                        wpage->wininfo->size.cx, wpage->wininfo->size.cy,
648                                        bPopup ? Globals.active_win->hMainWnd : NULL,
649                                        bPrimary ? LoadMenu(Globals.hInstance, MAKEINTRESOURCE(MAIN_MENU)) : 0,
650                                        Globals.hInstance, win);
651         if (!bPopup)
652             /* Create button box and text Window */
653             CreateWindow(BUTTON_BOX_WIN_CLASS_NAME, "", WS_CHILD | WS_VISIBLE,
654                          0, 0, 0, 0, win->hMainWnd, (HMENU)CTL_ID_BUTTON, Globals.hInstance, NULL);
655
656         if (!use_richedit)
657             hTextWnd = CreateWindow(TEXT_WIN_CLASS_NAME, "", WS_CHILD | WS_VISIBLE,
658                                     0, 0, 0, 0, win->hMainWnd, (HMENU)CTL_ID_TEXT, Globals.hInstance, win);
659         else
660             hTextWnd = CreateWindow(RICHEDIT_CLASS, NULL,
661                                     ES_MULTILINE | ES_READONLY | WS_CHILD | WS_HSCROLL | WS_VSCROLL | WS_VISIBLE,
662                                     0, 0, 0, 0, win->hMainWnd, (HMENU)CTL_ID_TEXT, Globals.hInstance, NULL);
663     }
664
665     hIcon = (wpage->page) ? wpage->page->file->hIcon : NULL;
666     if (!hIcon) hIcon = LoadIcon(Globals.hInstance, MAKEINTRESOURCE(IDI_WINHELP));
667     SendMessage(win->hMainWnd, WM_SETICON, ICON_SMALL, (DWORD_PTR)hIcon);
668
669     /* Initialize file specific pushbuttons */
670     if (!(wpage->wininfo->win_style & WS_POPUP) && wpage->page)
671     {
672         HLPFILE_MACRO  *macro;
673         for (macro = wpage->page->file->first_macro; macro; macro = macro->next)
674             MACRO_ExecuteMacro(macro->lpszMacro);
675
676         for (macro = wpage->page->first_macro; macro; macro = macro->next)
677             MACRO_ExecuteMacro(macro->lpszMacro);
678     }
679
680     WINHELP_LayoutMainWindow(win);
681
682     ShowWindow(win->hMainWnd, nCmdShow);
683     WINHELP_SetupText(hTextWnd, win, wpage->relative);
684
685     return TRUE;
686 }
687
688 /******************************************************************
689  *             WINHELP_OpenHelpWindow
690  * Main function to search for a page and display it in a window
691  */
692 BOOL WINHELP_OpenHelpWindow(HLPFILE_PAGE* (*lookup)(HLPFILE*, LONG, ULONG*),
693                             HLPFILE* hlpfile, LONG val, HLPFILE_WINDOWINFO* wi,
694                             int nCmdShow)
695 {
696     WINHELP_WNDPAGE     wpage;
697
698     wpage.page = lookup(hlpfile, val, &wpage.relative);
699     if (wpage.page) wpage.page->file->wRefCount++;
700     wpage.wininfo = wi;
701     return WINHELP_CreateHelpWindow(&wpage, nCmdShow, TRUE);
702 }
703
704 /***********************************************************************
705  *
706  *           WINHELP_MainWndProc
707  */
708 static LRESULT CALLBACK WINHELP_MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
709 {
710     WINHELP_WINDOW *win;
711     WINHELP_BUTTON *button;
712     RECT rect;
713     INT  curPos, min, max, dy, keyDelta;
714     HWND hTextWnd;
715
716     WINHELP_CheckPopup(msg);
717
718     switch (msg)
719     {
720     case WM_NCCREATE:
721         win = (WINHELP_WINDOW*) ((LPCREATESTRUCT) lParam)->lpCreateParams;
722         SetWindowLongPtr(hWnd, 0, (ULONG_PTR) win);
723         if (!win->page && Globals.isBook)
724             PostMessage(hWnd, WM_COMMAND, MNID_FILE_OPEN, 0);
725         win->hMainWnd = hWnd;
726         break;
727
728     case WM_WINDOWPOSCHANGED:
729         WINHELP_LayoutMainWindow((WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0));
730         break;
731
732     case WM_COMMAND:
733         win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
734         switch (wParam)
735         {
736             /* Menu FILE */
737         case MNID_FILE_OPEN:    MACRO_FileOpen();       break;
738         case MNID_FILE_PRINT:   MACRO_Print();          break;
739         case MNID_FILE_SETUP:   MACRO_PrinterSetup();   break;
740         case MNID_FILE_EXIT:    MACRO_Exit();           break;
741
742             /* Menu EDIT */
743         case MNID_EDIT_COPYDLG: MACRO_CopyDialog();     break;
744         case MNID_EDIT_ANNOTATE:MACRO_Annotate();       break;
745
746             /* Menu Bookmark */
747         case MNID_BKMK_DEFINE:  MACRO_BookmarkDefine(); break;
748
749             /* Menu Help */
750         case MNID_HELP_HELPON:  MACRO_HelpOn();         break;
751         case MNID_HELP_HELPTOP: MACRO_HelpOnTop();      break;
752         case MNID_HELP_ABOUT:   MACRO_About();          break;
753         case MNID_HELP_WINE:    ShellAbout(hWnd, "WINE", "Help", 0); break;
754
755         default:
756             /* Buttons */
757             for (button = win->first_button; button; button = button->next)
758                 if (wParam == button->wParam) break;
759             if (button)
760                 MACRO_ExecuteMacro(button->lpszMacro);
761             else if (!HIWORD(wParam))
762                 MessageBox(0, MAKEINTRESOURCE(STID_NOT_IMPLEMENTED),
763                            MAKEINTRESOURCE(STID_WHERROR), MB_OK);
764             break;
765         }
766         break;
767 /* EPP     case WM_DESTROY: */
768 /* EPP         if (Globals.hPopupWnd) DestroyWindow(Globals.hPopupWnd); */
769 /* EPP         break; */
770     case WM_COPYDATA:
771         return WINHELP_HandleCommand((HWND)wParam, lParam);
772
773     case WM_KEYDOWN:
774         keyDelta = 0;
775
776         switch (wParam)
777         {
778         case VK_UP:
779         case VK_DOWN:
780             keyDelta = GetSystemMetrics(SM_CXVSCROLL);
781             if (wParam == VK_UP)
782                 keyDelta = -keyDelta;
783
784         case VK_PRIOR:
785         case VK_NEXT:
786             win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
787             hTextWnd = GetDlgItem(win->hMainWnd, CTL_ID_TEXT);
788             curPos = GetScrollPos(hTextWnd, SB_VERT);
789             GetScrollRange(hTextWnd, SB_VERT, &min, &max);
790
791             if (keyDelta == 0)
792             {            
793                 GetClientRect(hTextWnd, &rect);
794                 keyDelta = (rect.bottom - rect.top) / 2;
795                 if (wParam == VK_PRIOR)
796                     keyDelta = -keyDelta;
797             }
798
799             curPos += keyDelta;
800             if (curPos > max)
801                  curPos = max;
802             else if (curPos < min)
803                  curPos = min;
804
805             dy = GetScrollPos(hTextWnd, SB_VERT) - curPos;
806             SetScrollPos(hTextWnd, SB_VERT, curPos, TRUE);
807             ScrollWindow(hTextWnd, 0, dy, NULL, NULL);
808             UpdateWindow(hTextWnd);
809             return 0;
810
811         case VK_ESCAPE:
812             MACRO_Exit();
813             return 0;
814         }
815         break;
816     case WM_NCDESTROY:
817         {
818             BOOL bExit;
819             win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
820             bExit = (Globals.wVersion >= 4 && !lstrcmpi(win->lpszName, "main"));
821             WINHELP_DeleteWindow(win);
822
823             if (bExit) MACRO_Exit();
824             if (!Globals.win_list)
825                 PostQuitMessage(0);
826         }
827         break;
828     }
829     return DefWindowProc(hWnd, msg, wParam, lParam);
830 }
831
832 static DWORD CALLBACK WINHELP_RtfStreamIn(DWORD_PTR cookie, BYTE* buff,
833                                           LONG cb, LONG* pcb)
834 {
835     struct RtfData*     rd = (struct RtfData*)cookie;
836
837     if (rd->where >= rd->ptr) return 1;
838     if (rd->where + cb > rd->ptr)
839         cb = rd->ptr - rd->where;
840     memcpy(buff, rd->where, cb);
841     rd->where += cb;
842     *pcb = cb;
843     return 0;
844 }
845
846 static void WINHELP_FillRichEdit(HWND hTextWnd, WINHELP_WINDOW *win, ULONG relative)
847 {
848     SendMessage(hTextWnd, WM_SETREDRAW, FALSE, 0);
849     SendMessage(hTextWnd, EM_SETBKGNDCOLOR, 0, (LPARAM)win->info->sr_color);
850     /* set word-wrap to window size (undocumented) */
851     SendMessage(hTextWnd, EM_SETTARGETDEVICE, 0, 0);
852     if (win->page)
853     {
854         struct RtfData  rd;
855         EDITSTREAM      es;
856
857         if (HLPFILE_BrowsePage(win->page, &rd))
858         {
859             rd.where = rd.data;
860             es.dwCookie = (DWORD_PTR)&rd;
861             es.dwError = 0;
862             es.pfnCallback = WINHELP_RtfStreamIn;
863
864             SendMessageW(hTextWnd, EM_STREAMIN, SF_RTF, (LPARAM)&es);
865         }
866         /* FIXME: else leaking potentially the rd.first_link chain */
867         HeapFree(GetProcessHeap(), 0, rd.data);
868     }
869     else
870     {
871         SendMessage(hTextWnd, WM_SETTEXT, 0, (LPARAM)"");
872     }
873     SendMessage(hTextWnd, WM_SETREDRAW, TRUE, 0);
874     SendMessage(hTextWnd, EM_SETSEL, 0, 0);
875     SendMessage(hTextWnd, EM_SCROLLCARET, 0, 0);
876     InvalidateRect(hTextWnd, NULL, TRUE);
877 }
878
879 /***********************************************************************
880  *
881  *           WINHELP_ButtonBoxWndProc
882  */
883 static LRESULT CALLBACK WINHELP_ButtonBoxWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
884 {
885     WINDOWPOS      *winpos;
886     WINHELP_WINDOW *win;
887     WINHELP_BUTTON *button;
888     SIZE button_size;
889     INT  x, y;
890
891     WINHELP_CheckPopup(msg);
892
893     switch (msg)
894     {
895     case WM_WINDOWPOSCHANGING:
896         winpos = (WINDOWPOS*) lParam;
897         win = (WINHELP_WINDOW*) GetWindowLongPtr(GetParent(hWnd), 0);
898
899         /* Update buttons */
900         button_size.cx = 0;
901         button_size.cy = 0;
902         for (button = win->first_button; button; button = button->next)
903         {
904             HDC  hDc;
905             SIZE textsize;
906             if (!button->hWnd)
907             {
908                 button->hWnd = CreateWindow(STRING_BUTTON, button->lpszName,
909                                             WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
910                                             0, 0, 0, 0,
911                                             hWnd, (HMENU) button->wParam,
912                                             Globals.hInstance, 0);
913                 if (button->hWnd) {
914                     if (Globals.button_proc == NULL)
915                         Globals.button_proc = (WNDPROC) GetWindowLongPtr(button->hWnd, GWLP_WNDPROC);
916                     SetWindowLongPtr(button->hWnd, GWLP_WNDPROC, (LONG_PTR) WINHELP_ButtonWndProc);
917                 }
918             }
919             hDc = GetDC(button->hWnd);
920             GetTextExtentPoint(hDc, button->lpszName,
921                                lstrlen(button->lpszName), &textsize);
922             ReleaseDC(button->hWnd, hDc);
923
924             button_size.cx = max(button_size.cx, textsize.cx + BUTTON_CX);
925             button_size.cy = max(button_size.cy, textsize.cy + BUTTON_CY);
926         }
927
928         x = 0;
929         y = 0;
930         for (button = win->first_button; button; button = button->next)
931         {
932             SetWindowPos(button->hWnd, HWND_TOP, x, y, button_size.cx, button_size.cy, 0);
933
934             if (x + 2 * button_size.cx <= winpos->cx)
935                 x += button_size.cx;
936             else
937                 x = 0, y += button_size.cy;
938         }
939         winpos->cy = y + (x ? button_size.cy : 0);
940         break;
941
942     case WM_COMMAND:
943         SendMessage(GetParent(hWnd), msg, wParam, lParam);
944         break;
945
946     case WM_KEYDOWN:
947         switch (wParam)
948         {
949         case VK_UP:
950         case VK_DOWN:
951         case VK_PRIOR:
952         case VK_NEXT:
953         case VK_ESCAPE:
954             return SendMessage(GetParent(hWnd), msg, wParam, lParam);
955         }
956         break;
957     }
958
959     return DefWindowProc(hWnd, msg, wParam, lParam);
960 }
961
962 /***********************************************************************
963  *
964  *           WINHELP_ButtonWndProc
965  */
966 static LRESULT CALLBACK WINHELP_ButtonWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
967 {
968     if (msg == WM_KEYDOWN)
969     {
970         switch (wParam)
971         {
972         case VK_UP:
973         case VK_DOWN:
974         case VK_PRIOR:
975         case VK_NEXT:
976         case VK_ESCAPE:
977             return SendMessage(GetParent(hWnd), msg, wParam, lParam);
978         }
979     }
980
981     return CallWindowProc(Globals.button_proc, hWnd, msg, wParam, lParam);
982 }
983
984 /***********************************************************************
985  *
986  *           WINHELP_TextWndProc
987  */
988 static LRESULT CALLBACK WINHELP_TextWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
989 {
990     WINHELP_WINDOW    *win;
991     WINHELP_LINE      *line;
992     WINHELP_LINE_PART *part;
993     WINDOWPOS         *winpos;
994     PAINTSTRUCT        ps;
995     HDC   hDc;
996     POINT mouse;
997     INT   scroll_pos;
998     HWND  hPopupWnd;
999
1000     if (msg != WM_LBUTTONDOWN)
1001         WINHELP_CheckPopup(msg);
1002
1003     switch (msg)
1004     {
1005     case WM_NCCREATE:
1006         win = (WINHELP_WINDOW*) ((LPCREATESTRUCT) lParam)->lpCreateParams;
1007         SetWindowLongPtr(hWnd, 0, (ULONG_PTR) win);
1008         win->hBrush = CreateSolidBrush(win->info->sr_color);
1009         WINHELP_InitFonts(hWnd);
1010         break;
1011
1012     case WM_CREATE:
1013         win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1014
1015         /* Calculate vertical size and position of a popup window */
1016         if (win->info->win_style & WS_POPUP)
1017         {
1018             POINT origin;
1019             RECT old_window_rect;
1020             RECT old_client_rect;
1021             SIZE old_window_size;
1022             SIZE old_client_size;
1023             SIZE new_client_size;
1024             SIZE new_window_size;
1025
1026             GetWindowRect(GetParent(hWnd), &old_window_rect);
1027             origin.x = old_window_rect.left;
1028             origin.y = old_window_rect.top;
1029             old_window_size.cx = old_window_rect.right  - old_window_rect.left;
1030             old_window_size.cy = old_window_rect.bottom - old_window_rect.top;
1031
1032             WINHELP_LayoutMainWindow(win);
1033             GetClientRect(hWnd, &old_client_rect);
1034             old_client_size.cx = old_client_rect.right  - old_client_rect.left;
1035             old_client_size.cy = old_client_rect.bottom - old_client_rect.top;
1036
1037             new_client_size = old_client_size;
1038             WINHELP_SplitLines(hWnd, &new_client_size);
1039
1040             if (origin.y + POPUP_YDISTANCE + new_client_size.cy <= GetSystemMetrics(SM_CYSCREEN))
1041                 origin.y += POPUP_YDISTANCE;
1042             else
1043                 origin.y -= POPUP_YDISTANCE + new_client_size.cy;
1044
1045             new_window_size.cx = old_window_size.cx - old_client_size.cx + new_client_size.cx;
1046             new_window_size.cy = old_window_size.cy - old_client_size.cy + new_client_size.cy;
1047
1048             win->hShadowWnd =
1049                 CreateWindowEx(WS_EX_TOOLWINDOW, SHADOW_WIN_CLASS_NAME, "", WS_POPUP,
1050                              origin.x + SHADOW_DX, origin.y + SHADOW_DY,
1051                              new_window_size.cx, new_window_size.cy,
1052                              0, 0, Globals.hInstance, 0);
1053
1054             SetWindowPos(GetParent(hWnd), HWND_TOP, origin.x, origin.y,
1055                          new_window_size.cx, new_window_size.cy,
1056                          0);
1057             SetWindowPos(win->hShadowWnd, hWnd, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
1058             ShowWindow(win->hShadowWnd, SW_NORMAL);
1059             SetActiveWindow(hWnd);
1060         }
1061         break;
1062
1063     case WM_WINDOWPOSCHANGED:
1064         winpos = (WINDOWPOS*) lParam;
1065
1066         if (!(winpos->flags & SWP_NOSIZE)) WINHELP_SetupText(hWnd, NULL, 0);
1067         break;
1068
1069     case WM_MOUSEWHEEL:
1070     {
1071        int wheelDelta = 0;
1072        UINT scrollLines = 3;
1073        int curPos = GetScrollPos(hWnd, SB_VERT);
1074        int min, max;
1075
1076        GetScrollRange(hWnd, SB_VERT, &min, &max);
1077
1078        SystemParametersInfo(SPI_GETWHEELSCROLLLINES,0, &scrollLines, 0);
1079        if (wParam & (MK_SHIFT | MK_CONTROL))
1080            return DefWindowProc(hWnd, msg, wParam, lParam);
1081        wheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
1082        if (abs(wheelDelta) >= WHEEL_DELTA && scrollLines) {
1083            int dy;
1084
1085            curPos += wheelDelta;
1086            if (curPos > max)
1087                 curPos = max;
1088            else if (curPos < min)
1089                 curPos = min;
1090
1091            dy = GetScrollPos(hWnd, SB_VERT) - curPos;
1092            SetScrollPos(hWnd, SB_VERT, curPos, TRUE);
1093            ScrollWindow(hWnd, 0, dy, NULL, NULL);
1094            UpdateWindow(hWnd);
1095        }
1096     }
1097     break;
1098
1099     case WM_VSCROLL:
1100     {
1101         BOOL  update = TRUE;
1102         RECT  rect;
1103         INT   Min, Max;
1104         INT   CurPos = GetScrollPos(hWnd, SB_VERT);
1105         INT   dy;
1106         
1107         GetScrollRange(hWnd, SB_VERT, &Min, &Max);
1108         GetClientRect(hWnd, &rect);
1109
1110         switch (wParam & 0xffff)
1111         {
1112         case SB_THUMBTRACK:
1113         case SB_THUMBPOSITION: CurPos  = wParam >> 16;                   break;
1114         case SB_TOP:           CurPos  = Min;                            break;
1115         case SB_BOTTOM:        CurPos  = Max;                            break;
1116         case SB_PAGEUP:        CurPos -= (rect.bottom - rect.top) / 2;   break;
1117         case SB_PAGEDOWN:      CurPos += (rect.bottom - rect.top) / 2;   break;
1118         case SB_LINEUP:        CurPos -= GetSystemMetrics(SM_CXVSCROLL); break;
1119         case SB_LINEDOWN:      CurPos += GetSystemMetrics(SM_CXVSCROLL); break;
1120         default: update = FALSE;
1121         }
1122         if (update)
1123         {
1124             if (CurPos > Max)
1125                 CurPos = Max;
1126             else if (CurPos < Min)
1127                 CurPos = Min;
1128             dy = GetScrollPos(hWnd, SB_VERT) - CurPos;
1129             SetScrollPos(hWnd, SB_VERT, CurPos, TRUE);
1130             ScrollWindow(hWnd, 0, dy, NULL, NULL);
1131             UpdateWindow(hWnd);
1132         }
1133     }
1134     break;
1135
1136     case WM_PAINT:
1137         hDc = BeginPaint(hWnd, &ps);
1138         win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1139         scroll_pos = GetScrollPos(hWnd, SB_VERT);
1140
1141         /* No DPtoLP needed - MM_TEXT map mode */
1142         if (ps.fErase) FillRect(hDc, &ps.rcPaint, win->hBrush);
1143         for (line = win->first_line; line; line = line->next)
1144         {
1145             for (part = &line->first_part; part; part = part->next)
1146             {
1147                 switch (part->cookie)
1148                 {
1149                 case hlp_line_part_text:
1150                     SelectObject(hDc, part->u.text.hFont);
1151                     SetTextColor(hDc, part->u.text.color);
1152                     SetBkColor(hDc, win->info->sr_color);
1153                     TextOut(hDc, part->rect.left, part->rect.top - scroll_pos,
1154                             part->u.text.lpsText, part->u.text.wTextLen);
1155                     if (part->u.text.wUnderline)
1156                     {
1157                         HPEN    hPen;
1158
1159                         switch (part->u.text.wUnderline)
1160                         {
1161                         case 1: /* simple */
1162                         case 2: /* double */
1163                             hPen = CreatePen(PS_SOLID, 1, part->u.text.color);
1164                             break;
1165                         case 3: /* dotted */
1166                             hPen = CreatePen(PS_DOT, 1, part->u.text.color);
1167                             break;
1168                         default:
1169                             WINE_FIXME("Unknow underline type\n");
1170                             continue;
1171                         }
1172
1173                         SelectObject(hDc, hPen);
1174                         MoveToEx(hDc, part->rect.left, part->rect.bottom - scroll_pos - 1, NULL);
1175                         LineTo(hDc, part->rect.right, part->rect.bottom - scroll_pos - 1);
1176                         if (part->u.text.wUnderline == 2)
1177                         {
1178                             MoveToEx(hDc, part->rect.left, part->rect.bottom - scroll_pos + 1, NULL);
1179                             LineTo(hDc, part->rect.right, part->rect.bottom - scroll_pos + 1);
1180                         }
1181                         DeleteObject(hPen);
1182                     }
1183                     break;
1184                 case hlp_line_part_bitmap:
1185                     {
1186                         HDC hMemDC;
1187
1188                         hMemDC = CreateCompatibleDC(hDc);
1189                         SelectObject(hMemDC, part->u.bitmap.hBitmap);
1190                         BitBlt(hDc, part->rect.left, part->rect.top - scroll_pos,
1191                                part->rect.right - part->rect.left, part->rect.bottom - part->rect.top,
1192                                hMemDC, 0, 0, SRCCOPY);
1193                         DeleteDC(hMemDC);
1194                     }
1195                     break;
1196                 case hlp_line_part_metafile:
1197                     {
1198                         HDC hMemDC;
1199                         HBITMAP hBitmap;
1200                         SIZE sz;
1201                         RECT rc;
1202
1203                         sz.cx = part->rect.right - part->rect.left;
1204                         sz.cy = part->rect.bottom - part->rect.top;
1205                         hMemDC = CreateCompatibleDC(hDc);
1206                         hBitmap = CreateCompatibleBitmap(hDc, sz.cx, sz.cy);
1207                         SelectObject(hMemDC, hBitmap);
1208                         SelectObject(hMemDC, win->hBrush);
1209                         rc.left = 0;
1210                         rc.top = 0;
1211                         rc.right = sz.cx;
1212                         rc.bottom = sz.cy;
1213                         FillRect(hMemDC, &rc, win->hBrush);
1214                         SetMapMode(hMemDC, part->u.metafile.mm);
1215                         SetWindowExtEx(hMemDC, sz.cx, sz.cy, 0);
1216                         SetViewportExtEx(hMemDC, sz.cx, sz.cy, 0);
1217                         SetWindowOrgEx(hMemDC, 0, 0, 0);
1218                         SetViewportOrgEx(hMemDC, 0, 0, 0);
1219                         PlayMetaFile(hMemDC, part->u.metafile.hMetaFile);
1220                         SetMapMode(hMemDC, MM_TEXT);
1221                         SetWindowOrgEx(hMemDC, 0, 0, 0);
1222                         SetViewportOrgEx(hMemDC, 0, 0, 0);
1223                         BitBlt(hDc, part->rect.left, part->rect.top - scroll_pos,
1224                                sz.cx, sz.cy, hMemDC, 0, 0, SRCCOPY);
1225                         DeleteDC(hMemDC);
1226                         DeleteObject(hBitmap);
1227                     }
1228                     break;
1229                 }
1230             }
1231         }
1232
1233         EndPaint(hWnd, &ps);
1234         break;
1235
1236     case WM_MOUSEMOVE:
1237         win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1238
1239         if (WINHELP_IsOverLink(win, wParam, lParam))
1240             SetCursor(win->hHandCur); /* set to hand pointer cursor to indicate a link */
1241         else
1242             SetCursor(win->hArrowCur); /* set to hand pointer cursor to indicate a link */
1243
1244         break;
1245
1246     case WM_LBUTTONDOWN:
1247         win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1248
1249         if (Globals.active_popup)
1250         {
1251             hPopupWnd = Globals.active_popup->hMainWnd;
1252             Globals.active_popup = NULL;
1253         }
1254         else hPopupWnd = NULL;
1255
1256         part = WINHELP_IsOverLink(win, wParam, lParam);
1257         if (part)
1258         {
1259             HLPFILE*            hlpfile;
1260             HLPFILE_WINDOWINFO* wi;
1261
1262             mouse.x = (short)LOWORD(lParam);
1263             mouse.y = (short)HIWORD(lParam);
1264
1265             if (part->link) switch (part->link->cookie)
1266             {
1267             case hlp_link_link:
1268                 hlpfile = WINHELP_LookupHelpFile(part->link->lpszString);
1269                 if (part->link->window == -1)
1270                     wi = win->info;
1271                 else if (part->link->window < hlpfile->numWindows)
1272                     wi = &hlpfile->windows[part->link->window];
1273                 else
1274                 {
1275                     WINE_WARN("link to window %d/%d\n", part->link->window, hlpfile->numWindows);
1276                     break;
1277                 }
1278                 WINHELP_OpenHelpWindow(HLPFILE_PageByHash, hlpfile, part->link->lHash,
1279                                        wi, SW_NORMAL);
1280                 break;
1281             case hlp_link_popup:
1282                 hlpfile = WINHELP_LookupHelpFile(part->link->lpszString);
1283                 if (hlpfile) WINHELP_OpenHelpWindow(HLPFILE_PageByHash, hlpfile, part->link->lHash,
1284                                                     WINHELP_GetPopupWindowInfo(hlpfile, win, &mouse),
1285                                                     SW_NORMAL);
1286                 break;
1287             case hlp_link_macro:
1288                 MACRO_ExecuteMacro(part->link->lpszString);
1289                 break;
1290             default:
1291                 WINE_FIXME("Unknown link cookie %d\n", part->link->cookie);
1292             }
1293         }
1294
1295         if (hPopupWnd)
1296             DestroyWindow(hPopupWnd);
1297         break;
1298
1299     }
1300
1301     return DefWindowProc(hWnd, msg, wParam, lParam);
1302 }
1303
1304 /******************************************************************
1305  *              WINHELP_HistoryWndProc
1306  *
1307  *
1308  */
1309 static LRESULT CALLBACK WINHELP_HistoryWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1310 {
1311     WINHELP_WINDOW*     win;
1312     PAINTSTRUCT         ps;
1313     HDC                 hDc;
1314     TEXTMETRIC          tm;
1315     unsigned int        i;
1316     RECT                r;
1317
1318     switch (msg)
1319     {
1320     case WM_NCCREATE:
1321         win = (WINHELP_WINDOW*)((LPCREATESTRUCT)lParam)->lpCreateParams;
1322         SetWindowLongPtr(hWnd, 0, (ULONG_PTR)win);
1323         win->hHistoryWnd = hWnd;
1324         break;
1325     case WM_CREATE:
1326         win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1327         hDc = GetDC(hWnd);
1328         GetTextMetrics(hDc, &tm);
1329         GetWindowRect(hWnd, &r);
1330
1331         r.right = r.left + 30 * tm.tmAveCharWidth;
1332         r.bottom = r.top + (sizeof(Globals.history.set) / sizeof(Globals.history.set[0])) * tm.tmHeight;
1333         AdjustWindowRect(&r, GetWindowLong(hWnd, GWL_STYLE), FALSE);
1334         if (r.left < 0) {r.right -= r.left; r.left = 0;}
1335         if (r.top < 0) {r.bottom -= r.top; r.top = 0;}
1336
1337         MoveWindow(hWnd, r.left, r.top, r.right, r.bottom, TRUE);
1338         ReleaseDC(hWnd, hDc);
1339         break;
1340     case WM_LBUTTONDOWN:
1341         win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1342         hDc = GetDC(hWnd);
1343         GetTextMetrics(hDc, &tm);
1344         i = HIWORD(lParam) / tm.tmHeight;
1345         if (i < Globals.history.index)
1346             WINHELP_CreateHelpWindow(&Globals.history.set[i], SW_SHOW, TRUE);
1347         ReleaseDC(hWnd, hDc);
1348         break;
1349     case WM_PAINT:
1350         hDc = BeginPaint(hWnd, &ps);
1351         win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1352         GetTextMetrics(hDc, &tm);
1353
1354         for (i = 0; i < Globals.history.index; i++)
1355         {
1356             if (Globals.history.set[i].page->file == Globals.active_win->page->file)
1357             {
1358                 TextOut(hDc, 0, i * tm.tmHeight,
1359                         Globals.history.set[i].page->lpszTitle,
1360                         strlen(Globals.history.set[i].page->lpszTitle));
1361             }
1362             else
1363             {
1364                 char        buffer[1024];
1365                 const char* ptr1;
1366                 const char* ptr2;
1367                 unsigned    len;
1368
1369                 ptr1 = strrchr(Globals.history.set[i].page->file->lpszPath, '\\');
1370                 if (!ptr1) ptr1 = Globals.history.set[i].page->file->lpszPath;
1371                 else ptr1++;
1372                 ptr2 = strrchr(ptr1, '.');
1373                 len = ptr2 ? ptr2 - ptr1 : strlen(ptr1);
1374                 if (len > sizeof(buffer)) len = sizeof(buffer);
1375                 memcpy(buffer, ptr1, len);
1376                 if (len < sizeof(buffer)) buffer[len++] = ':';
1377                 strncpy(&buffer[len], Globals.history.set[i].page->lpszTitle, sizeof(buffer) - len);
1378                 buffer[sizeof(buffer) - 1] = '\0';
1379                 TextOut(hDc, 0, i * tm.tmHeight, buffer, strlen(buffer));
1380             }
1381         }
1382         EndPaint(hWnd, &ps);
1383         break;
1384     case WM_DESTROY:
1385         win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1386         if (hWnd == win->hHistoryWnd)
1387             win->hHistoryWnd = 0;
1388         break;
1389     }
1390     return DefWindowProc(hWnd, msg, wParam, lParam);
1391 }
1392
1393 /***********************************************************************
1394  *
1395  *           WINHELP_ShadowWndProc
1396  */
1397 static LRESULT CALLBACK WINHELP_ShadowWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1398 {
1399     WINHELP_CheckPopup(msg);
1400     return DefWindowProc(hWnd, msg, wParam, lParam);
1401 }
1402
1403 /***********************************************************************
1404  *
1405  *           SetupText
1406  */
1407 static void WINHELP_SetupText(HWND hWnd, WINHELP_WINDOW* win, ULONG relative)
1408 {
1409     HDC  hDc;
1410     RECT rect;
1411     SIZE newsize;
1412
1413     if (!win) win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1414     if (use_richedit) return WINHELP_FillRichEdit(hWnd, win, relative);
1415     hDc = GetDC(hWnd);
1416     ShowScrollBar(hWnd, SB_VERT, FALSE);
1417     if (!WINHELP_SplitLines(hWnd, NULL))
1418     {
1419         ShowScrollBar(hWnd, SB_VERT, TRUE);
1420         GetClientRect(hWnd, &rect);
1421
1422         WINHELP_SplitLines(hWnd, &newsize);
1423         SetScrollRange(hWnd, SB_VERT, 0, rect.top + newsize.cy - rect.bottom, TRUE);
1424     }
1425     else
1426     {
1427         SetScrollPos(hWnd, SB_VERT, 0, FALSE);
1428         SetScrollRange(hWnd, SB_VERT, 0, 0, FALSE);
1429     }
1430
1431     ReleaseDC(hWnd, hDc);
1432 }
1433
1434 /***********************************************************************
1435  *
1436  *           WINHELP_AppendText
1437  */
1438 static BOOL WINHELP_AppendText(WINHELP_LINE ***linep, WINHELP_LINE_PART ***partp,
1439                                LPSIZE space, LPSIZE textsize,
1440                                INT *line_ascent, INT ascent,
1441                                LPCSTR text, UINT textlen,
1442                                HFONT font, COLORREF color, HLPFILE_LINK *link,
1443                                unsigned underline)
1444 {
1445     WINHELP_LINE      *line;
1446     WINHELP_LINE_PART *part;
1447     LPSTR ptr;
1448
1449     if (!*partp) /* New line */
1450     {
1451         *line_ascent  = ascent;
1452
1453         line = HeapAlloc(GetProcessHeap(), 0,
1454                          sizeof(WINHELP_LINE) + textlen);
1455         if (!line) return FALSE;
1456
1457         line->next    = 0;
1458         part          = &line->first_part;
1459         ptr           = (char*)line + sizeof(WINHELP_LINE);
1460
1461         line->rect.top    = (**linep ? (**linep)->rect.bottom : 0) + space->cy;
1462         line->rect.bottom = line->rect.top;
1463         line->rect.left   = space->cx;
1464         line->rect.right  = space->cx;
1465
1466         if (**linep) *linep = &(**linep)->next;
1467         **linep = line;
1468         space->cy = 0;
1469     }
1470     else /* Same line */
1471     {
1472         line = **linep;
1473
1474         if (*line_ascent < ascent)
1475         {
1476             WINHELP_LINE_PART *p;
1477             for (p = &line->first_part; p; p = p->next)
1478             {
1479                 p->rect.top    += ascent - *line_ascent;
1480                 p->rect.bottom += ascent - *line_ascent;
1481             }
1482             line->rect.bottom += ascent - *line_ascent;
1483             *line_ascent = ascent;
1484         }
1485
1486         part = HeapAlloc(GetProcessHeap(), 0,
1487                          sizeof(WINHELP_LINE_PART) + textlen);
1488         if (!part) return FALSE;
1489         **partp = part;
1490         ptr     = (char*)part + sizeof(WINHELP_LINE_PART);
1491     }
1492
1493     memcpy(ptr, text, textlen);
1494     part->cookie            = hlp_line_part_text;
1495     part->rect.left         = line->rect.right + (*partp ? space->cx : 0);
1496     part->rect.right        = part->rect.left + textsize->cx;
1497     line->rect.right        = part->rect.right;
1498     part->rect.top          =
1499         ((*partp) ? line->rect.top : line->rect.bottom) + *line_ascent - ascent;
1500     part->rect.bottom       = part->rect.top + textsize->cy;
1501     line->rect.bottom       = max(line->rect.bottom, part->rect.bottom);
1502     part->u.text.lpsText    = ptr;
1503     part->u.text.wTextLen   = textlen;
1504     part->u.text.hFont      = font;
1505     part->u.text.color      = color;
1506     part->u.text.wUnderline = underline;
1507
1508     WINE_TRACE("Appended text '%*.*s'[%d] @ (%d,%d-%d,%d)\n",
1509                part->u.text.wTextLen,
1510                part->u.text.wTextLen,
1511                part->u.text.lpsText,
1512                part->u.text.wTextLen,
1513                part->rect.left, part->rect.top, part->rect.right, part->rect.bottom);
1514
1515     part->link = link;
1516     if (link) link->wRefCount++;
1517
1518     part->next          = 0;
1519     *partp              = &part->next;
1520
1521     space->cx = 0;
1522
1523     return TRUE;
1524 }
1525
1526 /***********************************************************************
1527  *
1528  *           WINHELP_AppendGfxObject
1529  */
1530 static WINHELP_LINE_PART* WINHELP_AppendGfxObject(WINHELP_LINE ***linep, WINHELP_LINE_PART ***partp,
1531                                                   LPSIZE space, LPSIZE gfxSize,
1532                                                   HLPFILE_LINK *link, unsigned pos)
1533 {
1534     WINHELP_LINE      *line;
1535     WINHELP_LINE_PART *part;
1536     LPSTR              ptr;
1537
1538     if (!*partp || pos == 1) /* New line */
1539     {
1540         line = HeapAlloc(GetProcessHeap(), 0, sizeof(WINHELP_LINE));
1541         if (!line) return NULL;
1542
1543         line->next    = NULL;
1544         part          = &line->first_part;
1545
1546         line->rect.top    = (**linep ? (**linep)->rect.bottom : 0) + space->cy;
1547         line->rect.bottom = line->rect.top;
1548         line->rect.left   = space->cx;
1549         line->rect.right  = space->cx;
1550
1551         if (**linep) *linep = &(**linep)->next;
1552         **linep = line;
1553         space->cy = 0;
1554         ptr = (char*)line + sizeof(WINHELP_LINE);
1555     }
1556     else /* Same line */
1557     {
1558         if (pos == 2) WINE_FIXME("Left alignment not handled\n");
1559         line = **linep;
1560
1561         part = HeapAlloc(GetProcessHeap(), 0, sizeof(WINHELP_LINE_PART));
1562         if (!part) return NULL;
1563         **partp = part;
1564         ptr = (char*)part + sizeof(WINHELP_LINE_PART);
1565     }
1566
1567     /* part->cookie should be set by caller (image or metafile) */
1568     part->rect.left       = line->rect.right + (*partp ? space->cx : 0);
1569     part->rect.right      = part->rect.left + gfxSize->cx;
1570     line->rect.right      = part->rect.right;
1571     part->rect.top        = (*partp) ? line->rect.top : line->rect.bottom;
1572     part->rect.bottom     = part->rect.top + gfxSize->cy;
1573     line->rect.bottom     = max(line->rect.bottom, part->rect.bottom);
1574
1575     WINE_TRACE("Appended gfx @ (%d,%d-%d,%d)\n",
1576                part->rect.left, part->rect.top, part->rect.right, part->rect.bottom);
1577
1578     part->link = link;
1579     if (link) link->wRefCount++;
1580
1581     part->next            = NULL;
1582     *partp                = &part->next;
1583
1584     space->cx = 0;
1585
1586     return part;
1587 }
1588
1589
1590 /***********************************************************************
1591  *
1592  *           WINHELP_SplitLines
1593  */
1594 static BOOL WINHELP_SplitLines(HWND hWnd, LPSIZE newsize)
1595 {
1596     WINHELP_WINDOW     *win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1597     HLPFILE_PARAGRAPH  *p;
1598     WINHELP_LINE      **line = &win->first_line;
1599     WINHELP_LINE_PART **part = 0;
1600     INT                 line_ascent = 0;
1601     SIZE                space;
1602     RECT                rect;
1603     HDC                 hDc;
1604
1605     if (newsize) newsize->cx = newsize->cy = 0;
1606
1607     if (!win->page) return TRUE;
1608
1609     WINHELP_DeleteLines(win);
1610
1611     GetClientRect(hWnd, &rect);
1612
1613     rect.top    += INTERNAL_BORDER_WIDTH;
1614     rect.left   += INTERNAL_BORDER_WIDTH;
1615     rect.right  -= INTERNAL_BORDER_WIDTH;
1616     rect.bottom -= INTERNAL_BORDER_WIDTH;
1617
1618     space.cy = rect.top;
1619     space.cx = rect.left;
1620
1621     hDc = GetDC(hWnd);
1622
1623     for (p = win->page->first_paragraph; p; p = p->next)
1624     {
1625         switch (p->cookie)
1626         {
1627         case para_normal_text:
1628         case para_debug_text:
1629             {
1630                 TEXTMETRIC tm;
1631                 SIZE textsize    = {0, 0};
1632                 LPCSTR text      = p->u.text.lpszText;
1633                 UINT indent      = 0;
1634                 UINT len         = strlen(text);
1635                 unsigned underline = 0;
1636
1637                 HFONT hFont = 0;
1638                 COLORREF color = RGB(0, 0, 0);
1639
1640                 if (p->u.text.wFont < win->page->file->numFonts)
1641                 {
1642                     HLPFILE*    hlpfile = win->page->file;
1643
1644                     if (!hlpfile->fonts[p->u.text.wFont].hFont)
1645                         hlpfile->fonts[p->u.text.wFont].hFont = CreateFontIndirect(&hlpfile->fonts[p->u.text.wFont].LogFont);
1646                     hFont = hlpfile->fonts[p->u.text.wFont].hFont;
1647                     color = hlpfile->fonts[p->u.text.wFont].color;
1648                 }
1649                 else
1650                 {
1651                     UINT  wFont = (p->u.text.wFont < win->fonts_len) ? p->u.text.wFont : 0;
1652
1653                     hFont = win->fonts[wFont];
1654                 }
1655
1656                 if (p->link && p->link->bClrChange)
1657                 {
1658                     underline = (p->link->cookie == hlp_link_popup) ? 3 : 1;
1659                     color = RGB(0, 0x80, 0);
1660                 }
1661                 if (p->cookie == para_debug_text) color = RGB(0xff, 0, 0);
1662
1663                 SelectObject(hDc, hFont);
1664
1665                 GetTextMetrics(hDc, &tm);
1666
1667                 if (p->u.text.wIndent)
1668                 {
1669                     indent = p->u.text.wIndent * 5 * tm.tmAveCharWidth;
1670                     if (!part)
1671                         space.cx = rect.left + indent - 2 * tm.tmAveCharWidth;
1672                 }
1673
1674                 if (p->u.text.wVSpace)
1675                 {
1676                     part = 0;
1677                     space.cx = rect.left + indent;
1678                     space.cy += (p->u.text.wVSpace - 1) * tm.tmHeight;
1679                 }
1680
1681                 if (p->u.text.wHSpace)
1682                 {
1683                     space.cx += p->u.text.wHSpace * 2 * tm.tmAveCharWidth;
1684                 }
1685
1686                 WINE_TRACE("splitting text %s\n", text);
1687
1688                 while (len)
1689                 {
1690                     INT free_width = rect.right - (part ? (*line)->rect.right : rect.left) - space.cx;
1691                     UINT low = 0, curr = len, high = len, textlen = 0;
1692
1693                     if (free_width > 0)
1694                     {
1695                         while (1)
1696                         {
1697                             GetTextExtentPoint(hDc, text, curr, &textsize);
1698
1699                             if (textsize.cx <= free_width) low = curr;
1700                             else high = curr;
1701
1702                             if (high <= low + 1) break;
1703
1704                             if (textsize.cx) curr = (curr * free_width) / textsize.cx;
1705                             if (curr <= low) curr = low + 1;
1706                             else if (curr >= high) curr = high - 1;
1707                         }
1708                         textlen = low;
1709                         while (textlen && text[textlen] && text[textlen] != ' ') textlen--;
1710                     }
1711                     if (!part && !textlen) textlen = max(low, 1);
1712
1713                     if (free_width <= 0 || !textlen)
1714                     {
1715                         part = 0;
1716                         space.cx = rect.left + indent;
1717                         space.cx = min(space.cx, rect.right - rect.left - 1);
1718                         continue;
1719                     }
1720
1721                     WINE_TRACE("\t => %d %*s\n", textlen, textlen, text);
1722
1723                     if (!WINHELP_AppendText(&line, &part, &space, &textsize,
1724                                             &line_ascent, tm.tmAscent,
1725                                             text, textlen, hFont, color, p->link, underline) ||
1726                         (!newsize && (*line)->rect.bottom > rect.bottom))
1727                     {
1728                         ReleaseDC(hWnd, hDc);
1729                         return FALSE;
1730                     }
1731
1732                     if (newsize)
1733                         newsize->cx = max(newsize->cx, (*line)->rect.right + INTERNAL_BORDER_WIDTH);
1734
1735                     len -= textlen;
1736                     text += textlen;
1737                     if (text[0] == ' ') text++, len--;
1738                 }
1739             }
1740             break;
1741         case para_bitmap:
1742         case para_metafile:
1743             {
1744                 SIZE                    gfxSize;
1745                 INT                     free_width;
1746                 WINHELP_LINE_PART*      ref_part;
1747
1748                 if (p->u.gfx.pos & 0x8000)
1749                 {
1750                     space.cx = rect.left;
1751                     if (*line)
1752                         space.cy += (*line)->rect.bottom - (*line)->rect.top;
1753                     part = 0;
1754                 }
1755
1756                 if (p->cookie == para_bitmap)
1757                 {
1758                     DIBSECTION              dibs;
1759                     
1760                     GetObject(p->u.gfx.u.bmp.hBitmap, sizeof(dibs), &dibs);
1761                     gfxSize.cx = dibs.dsBm.bmWidth;
1762                     gfxSize.cy = dibs.dsBm.bmHeight;
1763                 }
1764                 else
1765                 {
1766                     LPMETAFILEPICT lpmfp = &p->u.gfx.u.mfp;
1767                     if (lpmfp->mm == MM_ANISOTROPIC || lpmfp->mm == MM_ISOTROPIC)
1768                     {
1769                         gfxSize.cx = MulDiv(lpmfp->xExt, GetDeviceCaps(hDc, HORZRES),
1770                                             100*GetDeviceCaps(hDc, HORZSIZE));
1771                         gfxSize.cy = MulDiv(lpmfp->yExt, GetDeviceCaps(hDc, VERTRES),
1772                                             100*GetDeviceCaps(hDc, VERTSIZE));
1773                     }
1774                     else
1775                     {
1776                         gfxSize.cx = lpmfp->xExt;
1777                         gfxSize.cy = lpmfp->yExt;
1778                     }
1779                 }
1780
1781                 free_width = rect.right - ((part && *line) ? (*line)->rect.right : rect.left) - space.cx;
1782                 if (free_width <= 0)
1783                 {
1784                     part = NULL;
1785                     space.cx = rect.left;
1786                     space.cx = min(space.cx, rect.right - rect.left - 1);
1787                 }
1788                 ref_part = WINHELP_AppendGfxObject(&line, &part, &space, &gfxSize,
1789                                                    p->link, p->u.gfx.pos);
1790                 if (!ref_part || (!newsize && (*line)->rect.bottom > rect.bottom))
1791                 {
1792                     return FALSE;
1793                 }
1794                 if (p->cookie == para_bitmap)
1795                 {
1796                     ref_part->cookie = hlp_line_part_bitmap;
1797                     ref_part->u.bitmap.hBitmap = p->u.gfx.u.bmp.hBitmap;
1798                 }
1799                 else
1800                 {
1801                     ref_part->cookie = hlp_line_part_metafile;
1802                     ref_part->u.metafile.hMetaFile = p->u.gfx.u.mfp.hMF;
1803                     ref_part->u.metafile.mm = p->u.gfx.u.mfp.mm;
1804                 }
1805             }
1806             break;
1807         }
1808     }
1809
1810     if (newsize)
1811         newsize->cy = (*line)->rect.bottom + INTERNAL_BORDER_WIDTH;
1812
1813     ReleaseDC(hWnd, hDc);
1814     return TRUE;
1815 }
1816
1817 /***********************************************************************
1818  *
1819  *           WINHELP_CheckPopup
1820  */
1821 static void WINHELP_CheckPopup(UINT msg)
1822 {
1823     HWND        hPopup;
1824
1825     if (!Globals.active_popup) return;
1826
1827     switch (msg)
1828     {
1829     case WM_COMMAND:
1830     case WM_LBUTTONDOWN:
1831     case WM_MBUTTONDOWN:
1832     case WM_RBUTTONDOWN:
1833     case WM_NCLBUTTONDOWN:
1834     case WM_NCMBUTTONDOWN:
1835     case WM_NCRBUTTONDOWN:
1836         hPopup = Globals.active_popup->hMainWnd;
1837         Globals.active_popup = NULL;
1838         DestroyWindow(hPopup);
1839     }
1840 }
1841
1842 /***********************************************************************
1843  *
1844  *           WINHELP_DeleteLines
1845  */
1846 static void WINHELP_DeleteLines(WINHELP_WINDOW *win)
1847 {
1848     WINHELP_LINE      *line, *next_line;
1849     WINHELP_LINE_PART *part, *next_part;
1850     for (line = win->first_line; line; line = next_line)
1851     {
1852         next_line = line->next;
1853         for (part = &line->first_part; part; part = next_part)
1854         {
1855             next_part = part->next;
1856             HLPFILE_FreeLink(part->link);
1857             HeapFree(GetProcessHeap(), 0, part);
1858         }
1859     }
1860     win->first_line = 0;
1861 }
1862
1863 /******************************************************************
1864  *              WINHELP_DeleteButtons
1865  *
1866  */
1867 static void WINHELP_DeleteButtons(WINHELP_WINDOW* win)
1868 {
1869     WINHELP_BUTTON*     b;
1870     WINHELP_BUTTON*     bp;
1871
1872     for (b = win->first_button; b; b = bp)
1873     {
1874         DestroyWindow(b->hWnd);
1875         bp = b->next;
1876         HeapFree(GetProcessHeap(), 0, b);
1877     }
1878     win->first_button = NULL;
1879 }
1880
1881 /******************************************************************
1882  *              WINHELP_DeleteBackSet
1883  *
1884  */
1885 void WINHELP_DeleteBackSet(WINHELP_WINDOW* win)
1886 {
1887     unsigned int i;
1888
1889     for (i = 0; i < win->back.index; i++)
1890     {
1891         HLPFILE_FreeHlpFile(win->back.set[i].page->file);
1892         win->back.set[i].page = NULL;
1893     }
1894     win->back.index = 0;
1895 }
1896
1897 /***********************************************************************
1898  *
1899  *           WINHELP_DeleteWindow
1900  */
1901 static void WINHELP_DeleteWindow(WINHELP_WINDOW* win)
1902 {
1903     WINHELP_WINDOW**    w;
1904
1905     for (w = &Globals.win_list; *w; w = &(*w)->next)
1906     {
1907         if (*w == win)
1908         {
1909             *w = win->next;
1910             break;
1911         }
1912     }
1913
1914     if (Globals.active_win == win)
1915     {
1916         Globals.active_win = Globals.win_list;
1917         if (Globals.win_list)
1918             SetActiveWindow(Globals.win_list->hMainWnd);
1919     }
1920
1921     if (win == Globals.active_popup)
1922         Globals.active_popup = NULL;
1923
1924     WINHELP_DeleteButtons(win);
1925
1926     if (win->hShadowWnd) DestroyWindow(win->hShadowWnd);
1927     if (win->hHistoryWnd) DestroyWindow(win->hHistoryWnd);
1928
1929     DeleteObject(win->hBrush);
1930
1931     WINHELP_DeleteBackSet(win);
1932
1933     if (win->page) HLPFILE_FreeHlpFile(win->page->file);
1934     WINHELP_DeleteLines(win);
1935     HeapFree(GetProcessHeap(), 0, win);
1936 }
1937
1938 /***********************************************************************
1939  *
1940  *           WINHELP_InitFonts
1941  */
1942 static void WINHELP_InitFonts(HWND hWnd)
1943 {
1944     WINHELP_WINDOW *win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1945     LOGFONT logfontlist[] = {
1946         {-10, 0, 0, 0, 400, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 32, "Helv"},
1947         {-12, 0, 0, 0, 700, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 32, "Helv"},
1948         {-12, 0, 0, 0, 700, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 32, "Helv"},
1949         {-12, 0, 0, 0, 400, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 32, "Helv"},
1950         {-12, 0, 0, 0, 700, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 32, "Helv"},
1951         {-10, 0, 0, 0, 700, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 32, "Helv"},
1952         { -8, 0, 0, 0, 400, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 32, "Helv"}};
1953 #define FONTS_LEN (sizeof(logfontlist)/sizeof(*logfontlist))
1954
1955     static HFONT fonts[FONTS_LEN];
1956     static BOOL init = 0;
1957
1958     win->fonts_len = FONTS_LEN;
1959     win->fonts = fonts;
1960
1961     if (!init)
1962     {
1963         UINT i;
1964
1965         for (i = 0; i < FONTS_LEN; i++)
1966         {
1967             fonts[i] = CreateFontIndirect(&logfontlist[i]);
1968         }
1969
1970         init = 1;
1971     }
1972 }
1973
1974 /***********************************************************************
1975  *
1976  *           WINHELP_MessageBoxIDS_s
1977  */
1978 INT WINHELP_MessageBoxIDS_s(UINT ids_text, LPCSTR str, UINT ids_title, WORD type)
1979 {
1980     CHAR text[MAX_STRING_LEN];
1981     CHAR newtext[MAX_STRING_LEN + MAX_PATH];
1982
1983     LoadString(Globals.hInstance, ids_text, text, sizeof(text));
1984     wsprintf(newtext, text, str);
1985
1986     return MessageBox(0, newtext, MAKEINTRESOURCE(ids_title), type);
1987 }
1988
1989 /******************************************************************
1990  *              WINHELP_IsOverLink
1991  *
1992  *
1993  */
1994 WINHELP_LINE_PART* WINHELP_IsOverLink(WINHELP_WINDOW* win, WPARAM wParam, LPARAM lParam)
1995 {
1996     POINT mouse;
1997     WINHELP_LINE      *line;
1998     WINHELP_LINE_PART *part;
1999     int scroll_pos = GetScrollPos(GetDlgItem(win->hMainWnd, CTL_ID_TEXT), SB_VERT);
2000
2001     mouse.x = LOWORD(lParam);
2002     mouse.y = HIWORD(lParam);
2003     for (line = win->first_line; line; line = line->next)
2004     {
2005         for (part = &line->first_part; part; part = part->next)
2006         {
2007             if (part->link && 
2008                 part->link->lpszString &&
2009                 part->rect.left   <= mouse.x &&
2010                 part->rect.right  >= mouse.x &&
2011                 part->rect.top    <= mouse.y + scroll_pos &&
2012                 part->rect.bottom >= mouse.y + scroll_pos)
2013             {
2014                 return part;
2015             }
2016         }
2017     }
2018
2019     return NULL;
2020 }
2021
2022 /**************************************************************************
2023  * cb_KWBTree
2024  *
2025  * HLPFILE_BPTreeCallback enumeration function for '|KWBTREE' internal file.
2026  *
2027  */
2028 static void cb_KWBTree(void *p, void **next, void *cookie)
2029 {
2030     HWND hListWnd = (HWND)cookie;
2031     int count;
2032
2033     WINE_TRACE("Adding '%s' to search list\n", (char *)p);
2034     SendMessage(hListWnd, LB_INSERTSTRING, -1, (LPARAM)p);
2035     count = SendMessage(hListWnd, LB_GETCOUNT, 0, 0);
2036     SendMessage(hListWnd, LB_SETITEMDATA, count-1, (LPARAM)p);
2037     *next = (char*)p + strlen((char*)p) + 7;
2038 }
2039
2040 /**************************************************************************
2041  * WINHELP_IndexDlgProc
2042  *
2043  * Index dialog callback function.
2044  *
2045  * nResult passed to EndDialog:
2046  *   1: CANCEL button
2047  *  >1: valid offset value +2.
2048  *  EndDialog itself can return 0 (error).
2049  */
2050 INT_PTR CALLBACK WINHELP_SearchDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2051 {
2052     static HLPFILE *file;
2053     int sel;
2054     ULONG offset = 1;
2055
2056     switch (msg)
2057     {
2058     case WM_INITDIALOG:
2059         file = (HLPFILE *)lParam;
2060         HLPFILE_BPTreeEnum(file->kwbtree, cb_KWBTree,
2061                            GetDlgItem(hWnd, IDC_INDEXLIST));
2062         return TRUE;
2063     case WM_COMMAND:
2064         switch (LOWORD(wParam))
2065         {
2066         case IDOK:
2067             sel = SendDlgItemMessage(hWnd, IDC_INDEXLIST, LB_GETCURSEL, 0, 0);
2068             if (sel != LB_ERR)
2069             {
2070                 BYTE *p;
2071                 int count;
2072
2073                 p = (BYTE*)SendDlgItemMessage(hWnd, IDC_INDEXLIST,
2074                                               LB_GETITEMDATA, sel, 0);
2075                 count = *(short*)((char *)p + strlen((char *)p) + 1);
2076                 if (count > 1)
2077                 {
2078                     MessageBox(hWnd, "count > 1 not supported yet", "Error", MB_OK | MB_ICONSTOP);
2079                     return TRUE;
2080                 }
2081                 offset = *(ULONG*)((char *)p + strlen((char *)p) + 3);
2082                 offset = *(long*)(file->kwdata + offset + 9);
2083                 if (offset == 0xFFFFFFFF)
2084                 {
2085                     MessageBox(hWnd, "macro keywords not supported yet", "Error", MB_OK | MB_ICONSTOP);
2086                     return TRUE;
2087                 }
2088                 offset += 2;
2089             }
2090             /* Fall through */
2091         case IDCANCEL:
2092             EndDialog(hWnd, offset);
2093             return TRUE;
2094         default:
2095             break;
2096         }
2097     default:
2098         break;
2099     }
2100     return FALSE;
2101 }
2102
2103 /**************************************************************************
2104  * WINHELP_CreateIndexWindow
2105  *
2106  * Displays a dialog with keywords of current help file.
2107  *
2108  */
2109 BOOL WINHELP_CreateIndexWindow(void)
2110 {
2111     int ret;
2112     HLPFILE *hlpfile;
2113
2114     if (Globals.active_win && Globals.active_win->page && Globals.active_win->page->file)
2115         hlpfile = Globals.active_win->page->file;
2116     else
2117         return FALSE;
2118
2119     if (hlpfile->kwbtree == NULL)
2120     {
2121         WINE_TRACE("No index provided\n");
2122         return FALSE;
2123     }
2124
2125     ret = DialogBoxParam(Globals.hInstance, MAKEINTRESOURCE(IDD_INDEX),
2126                          Globals.active_win->hMainWnd, WINHELP_SearchDlgProc,
2127                          (LPARAM)hlpfile);
2128     if (ret > 1)
2129     {
2130         ret -= 2;
2131         WINE_TRACE("got %d as an offset\n", ret);
2132         WINHELP_OpenHelpWindow(HLPFILE_PageByOffset, hlpfile, ret,
2133                                Globals.active_win->info, SW_NORMAL);
2134     }
2135     return TRUE;
2136 }