winhelp: Let CtrlC do something useful thanks to richedit.
[wine] / programs / winhlp32 / 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 #define NONAMELESSUNION
31 #define NONAMELESSSTRUCT
32
33 #include "windef.h"
34 #include "winbase.h"
35 #include "wingdi.h"
36 #include "winuser.h"
37 #include "commdlg.h"
38 #include "winhelp.h"
39 #include "winhelp_res.h"
40 #include "shellapi.h"
41 #include "richedit.h"
42 #include "commctrl.h"
43
44 #include "wine/debug.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(winhelp);
47
48 static BOOL    WINHELP_RegisterWinClasses(void);
49 static LRESULT CALLBACK WINHELP_MainWndProc(HWND, UINT, WPARAM, LPARAM);
50 static LRESULT CALLBACK WINHELP_ButtonBoxWndProc(HWND, UINT, WPARAM, LPARAM);
51 static LRESULT CALLBACK WINHELP_ButtonWndProc(HWND, UINT, WPARAM, LPARAM);
52 static LRESULT CALLBACK WINHELP_HistoryWndProc(HWND, UINT, WPARAM, LPARAM);
53 static LRESULT CALLBACK WINHELP_ShadowWndProc(HWND, UINT, WPARAM, LPARAM);
54 static BOOL    WINHELP_CheckPopup(HWND, UINT, WPARAM, LPARAM, LRESULT*);
55 static void    WINHELP_InitFonts(HWND hWnd);
56 static void    WINHELP_DeleteWindow(WINHELP_WINDOW*);
57 static void    WINHELP_DeleteButtons(WINHELP_WINDOW*);
58 static void    WINHELP_SetupText(HWND hWnd, WINHELP_WINDOW *win, ULONG relative);
59 static void    WINHELP_DeletePageLinks(HLPFILE_PAGE* page);
60
61 WINHELP_GLOBALS Globals = {3, NULL, TRUE, NULL, NULL, NULL, NULL, NULL, {{{NULL,NULL}},0}};
62
63 #define CTL_ID_BUTTON   0x700
64 #define CTL_ID_TEXT     0x701
65
66 /***********************************************************************
67  *
68  *           WINHELP_GetOpenFileName
69  */
70 BOOL WINHELP_GetOpenFileName(LPSTR lpszFile, int len)
71 {
72     OPENFILENAME openfilename;
73     CHAR szDir[MAX_PATH];
74     CHAR szzFilter[2 * MAX_STRING_LEN + 100];
75     LPSTR p = szzFilter;
76
77     WINE_TRACE("()\n");
78
79     LoadString(Globals.hInstance, STID_HELP_FILES_HLP, p, MAX_STRING_LEN);
80     p += strlen(p) + 1;
81     lstrcpy(p, "*.hlp");
82     p += strlen(p) + 1;
83     LoadString(Globals.hInstance, STID_ALL_FILES, p, MAX_STRING_LEN);
84     p += strlen(p) + 1;
85     lstrcpy(p, "*.*");
86     p += strlen(p) + 1;
87     *p = '\0';
88
89     GetCurrentDirectory(sizeof(szDir), szDir);
90
91     lpszFile[0]='\0';
92
93     openfilename.lStructSize       = sizeof(OPENFILENAME);
94     openfilename.hwndOwner         = NULL;
95     openfilename.hInstance         = Globals.hInstance;
96     openfilename.lpstrFilter       = szzFilter;
97     openfilename.lpstrCustomFilter = 0;
98     openfilename.nMaxCustFilter    = 0;
99     openfilename.nFilterIndex      = 1;
100     openfilename.lpstrFile         = lpszFile;
101     openfilename.nMaxFile          = len;
102     openfilename.lpstrFileTitle    = 0;
103     openfilename.nMaxFileTitle     = 0;
104     openfilename.lpstrInitialDir   = szDir;
105     openfilename.lpstrTitle        = 0;
106     openfilename.Flags             = 0;
107     openfilename.nFileOffset       = 0;
108     openfilename.nFileExtension    = 0;
109     openfilename.lpstrDefExt       = 0;
110     openfilename.lCustData         = 0;
111     openfilename.lpfnHook          = 0;
112     openfilename.lpTemplateName    = 0;
113
114     return GetOpenFileName(&openfilename);
115 }
116
117 static char* WINHELP_GetCaption(WINHELP_WNDPAGE* wpage)
118 {
119     if (wpage->wininfo->caption[0]) return wpage->wininfo->caption;
120     return wpage->page->file->lpszTitle;
121 }
122
123 /***********************************************************************
124  *
125  *           WINHELP_LookupHelpFile
126  */
127 HLPFILE* WINHELP_LookupHelpFile(LPCSTR lpszFile)
128 {
129     HLPFILE*        hlpfile;
130     char szFullName[MAX_PATH];
131     char szAddPath[MAX_PATH];
132     char *p;
133
134     /*
135      * NOTE: This is needed by popup windows only.
136      * In other cases it's not needed but does not hurt though.
137      */
138     if (Globals.active_win && Globals.active_win->page && Globals.active_win->page->file)
139     {
140         strcpy(szAddPath, Globals.active_win->page->file->lpszPath);
141         p = strrchr(szAddPath, '\\');
142         if (p) *p = 0;
143     }
144
145     /*
146      * FIXME: Should we swap conditions?
147      */
148     if (!SearchPath(NULL, lpszFile, ".hlp", MAX_PATH, szFullName, NULL) &&
149         !SearchPath(szAddPath, lpszFile, ".hlp", MAX_PATH, szFullName, NULL))
150     {
151         if (WINHELP_MessageBoxIDS_s(STID_FILE_NOT_FOUND_s, lpszFile, STID_WHERROR,
152                                     MB_YESNO|MB_ICONQUESTION) != IDYES)
153             return NULL;
154         if (!WINHELP_GetOpenFileName(szFullName, MAX_PATH))
155             return NULL;
156     }
157     hlpfile = HLPFILE_ReadHlpFile(szFullName);
158     if (!hlpfile)
159         WINHELP_MessageBoxIDS_s(STID_HLPFILE_ERROR_s, lpszFile,
160                                 STID_WHERROR, MB_OK|MB_ICONSTOP);
161     return hlpfile;
162 }
163
164 /******************************************************************
165  *              WINHELP_GetWindowInfo
166  *
167  *
168  */
169 HLPFILE_WINDOWINFO*     WINHELP_GetWindowInfo(HLPFILE* hlpfile, LPCSTR name)
170 {
171     static      HLPFILE_WINDOWINFO      mwi;
172     unsigned int     i;
173
174     if (!name || !name[0])
175         name = Globals.active_win->lpszName;
176
177     if (hlpfile)
178         for (i = 0; i < hlpfile->numWindows; i++)
179             if (!strcmp(hlpfile->windows[i].name, name))
180                 return &hlpfile->windows[i];
181
182     if (strcmp(name, "main") != 0)
183     {
184         WINE_FIXME("Couldn't find window info for %s\n", name);
185         assert(0);
186         return NULL;
187     }
188     if (!mwi.name[0])
189     {
190         strcpy(mwi.type, "primary");
191         strcpy(mwi.name, "main");
192         if (!LoadString(Globals.hInstance, STID_WINE_HELP, 
193                         mwi.caption, sizeof(mwi.caption)))
194             strcpy(mwi.caption, hlpfile->lpszTitle);
195         mwi.origin.x = mwi.origin.y = mwi.size.cx = mwi.size.cy = CW_USEDEFAULT;
196         mwi.style = SW_SHOW;
197         mwi.win_style = WS_OVERLAPPEDWINDOW;
198         mwi.sr_color = mwi.sr_color = 0xFFFFFF;
199     }
200     return &mwi;
201 }
202
203 /******************************************************************
204  *              HLPFILE_GetPopupWindowInfo
205  *
206  *
207  */
208 static HLPFILE_WINDOWINFO*     WINHELP_GetPopupWindowInfo(HLPFILE* hlpfile,
209                                                           WINHELP_WINDOW* parent, LPARAM mouse)
210 {
211     static      HLPFILE_WINDOWINFO      wi;
212
213     RECT parent_rect;
214     
215     wi.type[0] = wi.name[0] = wi.caption[0] = '\0';
216
217     /* Calculate horizontal size and position of a popup window */
218     GetWindowRect(parent->hMainWnd, &parent_rect);
219     wi.size.cx = (parent_rect.right  - parent_rect.left) / 2;
220     wi.size.cy = 10; /* need a non null value, so that border are taken into account while computing */
221
222     wi.origin.x = (short)LOWORD(mouse);
223     wi.origin.y = (short)HIWORD(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     if (LoadLibrary("riched20.dll") == NULL)
253         return MessageBox(0, MAKEINTRESOURCE(STID_NO_RICHEDIT),
254                           MAKEINTRESOURCE(STID_WHERROR), MB_OK);
255
256     /* Get options */
257     while (*cmdline && (*cmdline == ' ' || *cmdline == '-'))
258     {
259         CHAR   option;
260         LPCSTR topic_id;
261         if (*cmdline++ == ' ') continue;
262
263         option = *cmdline;
264         if (option) cmdline++;
265         while (*cmdline && *cmdline == ' ') cmdline++;
266         switch (option)
267         {
268         case 'i':
269         case 'I':
270             topic_id = cmdline;
271             while (*cmdline && *cmdline != ' ') cmdline++;
272             if (*cmdline) *cmdline++ = '\0';
273             lHash = HLPFILE_Hash(topic_id);
274             break;
275
276         case '3':
277         case '4':
278             Globals.wVersion = option - '0';
279             break;
280
281         case 'x':
282             show = SW_HIDE; 
283             Globals.isBook = FALSE;
284             break;
285
286         default:
287             WINE_FIXME("Unsupported cmd line: %s\n", cmdline);
288             break;
289         }
290     }
291
292     /* Create primary window */
293     if (!WINHELP_RegisterWinClasses())
294     {
295         WINE_FIXME("Couldn't register classes\n");
296         return 0;
297     }
298
299     if (*cmdline)
300     {
301         char*   ptr;
302         if ((*cmdline == '"') && (ptr = strchr(cmdline+1, '"')))
303         {
304             cmdline++;
305             *ptr = '\0';
306         }
307         if ((ptr = strchr(cmdline, '>')))
308         {
309             *ptr = '\0';
310             wndname = ptr + 1;
311         }
312         hlpfile = WINHELP_LookupHelpFile(cmdline);
313         if (!hlpfile) return 0;
314     }
315     else hlpfile = NULL;
316     WINHELP_OpenHelpWindow(HLPFILE_PageByHash, hlpfile, lHash,
317                            WINHELP_GetWindowInfo(hlpfile, wndname), show);
318
319     /* Message loop */
320     while (GetMessage(&msg, 0, 0, 0))
321     {
322         TranslateMessage(&msg);
323         DispatchMessage(&msg);
324     }
325     for (dll = Globals.dlls; dll; dll = dll->next)
326     {
327         if (dll->class & DC_INITTERM) dll->handler(DW_TERM, 0, 0);
328     }
329     return 0;
330 }
331
332 /***********************************************************************
333  *
334  *           RegisterWinClasses
335  */
336 static BOOL WINHELP_RegisterWinClasses(void)
337 {
338     WNDCLASS class_main, class_button_box, class_shadow, class_history;
339
340     class_main.style               = CS_HREDRAW | CS_VREDRAW;
341     class_main.lpfnWndProc         = WINHELP_MainWndProc;
342     class_main.cbClsExtra          = 0;
343     class_main.cbWndExtra          = sizeof(LONG);
344     class_main.hInstance           = Globals.hInstance;
345     class_main.hIcon               = LoadIcon(Globals.hInstance, MAKEINTRESOURCE(IDI_WINHELP));
346     class_main.hCursor             = LoadCursor(0, IDC_ARROW);
347     class_main.hbrBackground       = (HBRUSH)(COLOR_WINDOW+1);
348     class_main.lpszMenuName        = 0;
349     class_main.lpszClassName       = MAIN_WIN_CLASS_NAME;
350
351     class_button_box               = class_main;
352     class_button_box.lpfnWndProc   = WINHELP_ButtonBoxWndProc;
353     class_button_box.cbWndExtra    = 0;
354     class_button_box.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
355     class_button_box.lpszClassName = BUTTON_BOX_WIN_CLASS_NAME;
356
357     class_shadow                   = class_main;
358     class_shadow.lpfnWndProc       = WINHELP_ShadowWndProc;
359     class_shadow.cbWndExtra        = 0;
360     class_shadow.hbrBackground     = (HBRUSH)(COLOR_3DDKSHADOW+1);
361     class_shadow.lpszClassName     = SHADOW_WIN_CLASS_NAME;
362
363     class_history                  = class_main;
364     class_history.lpfnWndProc      = WINHELP_HistoryWndProc;
365     class_history.lpszClassName    = HISTORY_WIN_CLASS_NAME;
366
367     return (RegisterClass(&class_main) &&
368             RegisterClass(&class_button_box) &&
369             RegisterClass(&class_shadow) &&
370             RegisterClass(&class_history));
371 }
372
373 typedef struct
374 {
375     WORD size;
376     WORD command;
377     LONG data;
378     LONG reserved;
379     WORD ofsFilename;
380     WORD ofsData;
381 } WINHELP,*LPWINHELP;
382
383 static BOOL WINHELP_HasWorkingWindow(void)
384 {
385     if (!Globals.active_win) return FALSE;
386     if (Globals.active_win->next || Globals.win_list != Globals.active_win) return TRUE;
387     return Globals.active_win->page != NULL && Globals.active_win->page->file != NULL;
388 }
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             if (!WINHELP_HasWorkingWindow()) MACRO_Exit();
422             break;
423         case HELP_QUIT:
424             MACRO_Exit();
425             break;
426         case HELP_CONTENTS:
427             if (ptr)
428             {
429                 MACRO_JumpContents(ptr, "main");
430             }
431             if (!WINHELP_HasWorkingWindow()) MACRO_Exit();
432             break;
433         case HELP_HELPONHELP:
434             MACRO_HelpOn();
435             if (!WINHELP_HasWorkingWindow()) MACRO_Exit();
436             break;
437         /* case HELP_SETINDEX: */
438         case HELP_SETCONTENTS:
439             if (ptr)
440             {
441                 MACRO_SetContents(ptr, wh->data);
442             }
443             break;
444         case HELP_CONTEXTPOPUP:
445             if (ptr)
446             {
447                 MACRO_PopupContext(ptr, wh->data);
448             }
449             break;
450         /* case HELP_FORCEFILE:*/
451         /* case HELP_CONTEXTMENU: */
452         case HELP_FINDER:
453             /* in fact, should be the topic dialog box */
454             WINE_FIXME("HELP_FINDER: stub\n");
455             if (ptr)
456             {
457                 MACRO_JumpHash(ptr, "main", 0);
458             }
459             break;
460         /* case HELP_WM_HELP: */
461         /* case HELP_SETPOPUP_POS: */
462         /* case HELP_KEY: */
463         /* case HELP_COMMAND: */
464         /* case HELP_PARTIALKEY: */
465         /* case HELP_MULTIKEY: */
466         /* case HELP_SETWINPOS: */
467         default:
468             WINE_FIXME("Unhandled command (%x) for remote winhelp control\n", wh->command);
469             break;
470         }
471     }
472     /* Always return success for now */
473     return 1;
474 }
475
476 void            WINHELP_LayoutMainWindow(WINHELP_WINDOW* win)
477 {
478     RECT        rect, button_box_rect;
479     INT         text_top = 0;
480     HWND        hButtonBoxWnd = GetDlgItem(win->hMainWnd, CTL_ID_BUTTON);
481     HWND        hTextWnd = GetDlgItem(win->hMainWnd, CTL_ID_TEXT);
482
483     GetClientRect(win->hMainWnd, &rect);
484
485     /* Update button box and text Window */
486     SetWindowPos(hButtonBoxWnd, HWND_TOP,
487                  rect.left, rect.top,
488                  rect.right - rect.left,
489                  rect.bottom - rect.top, 0);
490
491     if (GetWindowRect(hButtonBoxWnd, &button_box_rect))
492         text_top = rect.top + button_box_rect.bottom - button_box_rect.top;
493
494     SetWindowPos(hTextWnd, HWND_TOP,
495                  rect.left, text_top,
496                  rect.right - rect.left,
497                  rect.bottom - text_top, 0);
498
499 }
500
501 static void     WINHELP_RememberPage(WINHELP_WINDOW* win, WINHELP_WNDPAGE* wpage)
502 {
503     unsigned        num;
504
505     if (!Globals.history.index || Globals.history.set[0].page != wpage->page)
506     {
507         num = sizeof(Globals.history.set) / sizeof(Globals.history.set[0]);
508         /* we're full, remove latest entry */
509         if (Globals.history.index == num)
510         {
511             HLPFILE_FreeHlpFile(Globals.history.set[num - 1].page->file);
512             Globals.history.index--;
513         }
514         memmove(&Globals.history.set[1], &Globals.history.set[0],
515                 Globals.history.index * sizeof(Globals.history.set[0]));
516         Globals.history.set[0] = *wpage;
517         Globals.history.index++;
518         wpage->page->file->wRefCount++;
519     }
520     if (win->hHistoryWnd) InvalidateRect(win->hHistoryWnd, NULL, TRUE);
521
522     num = sizeof(win->back.set) / sizeof(win->back.set[0]);
523     if (win->back.index == num)
524     {
525         /* we're full, remove latest entry */
526         HLPFILE_FreeHlpFile(win->back.set[0].page->file);
527         memmove(&win->back.set[0], &win->back.set[1],
528                 (num - 1) * sizeof(win->back.set[0]));
529         win->back.index--;
530     }
531     win->back.set[win->back.index++] = *wpage;
532     wpage->page->file->wRefCount++;
533 }
534
535 /***********************************************************************
536  *
537  *           WINHELP_CreateHelpWindow
538  */
539 BOOL WINHELP_CreateHelpWindow(WINHELP_WNDPAGE* wpage, int nCmdShow, BOOL remember)
540 {
541     WINHELP_WINDOW*     win = NULL;
542     BOOL                bPrimary, bPopup, bReUsed = FALSE;
543     LPSTR               name;
544     HICON               hIcon;
545     HWND                hTextWnd = NULL;
546
547     bPrimary = !lstrcmpi(wpage->wininfo->name, "main");
548     bPopup = !bPrimary && (wpage->wininfo->win_style & WS_POPUP);
549
550     if (!bPopup)
551     {
552         for (win = Globals.win_list; win; win = win->next)
553         {
554             if (!lstrcmpi(win->lpszName, wpage->wininfo->name))
555             {
556                 POINT   pt = {0, 0};
557                 SIZE    sz = {0, 0};
558                 DWORD   flags = SWP_NOSIZE | SWP_NOMOVE;
559
560                 WINHELP_DeleteButtons(win);
561                 bReUsed = TRUE;
562                 SetWindowText(win->hMainWnd, WINHELP_GetCaption(wpage));
563                 if (wpage->wininfo->origin.x != CW_USEDEFAULT &&
564                     wpage->wininfo->origin.y != CW_USEDEFAULT)
565                 {
566                     pt = wpage->wininfo->origin;
567                     flags &= ~SWP_NOSIZE;
568                 }
569                 if (wpage->wininfo->size.cx != CW_USEDEFAULT &&
570                     wpage->wininfo->size.cy != CW_USEDEFAULT)
571                 {
572                     sz = wpage->wininfo->size;
573                     flags &= ~SWP_NOMOVE;
574                 }
575                 SetWindowPos(win->hMainWnd, HWND_TOP, pt.x, pt.y, sz.cx, sz.cy, flags);
576
577                 if (wpage->page && win->page && wpage->page->file != win->page->file)
578                     WINHELP_DeleteBackSet(win);
579                 WINHELP_InitFonts(win->hMainWnd);
580
581                 win->page = wpage->page;
582                 win->info = wpage->wininfo;
583                 hTextWnd = GetDlgItem(win->hMainWnd, CTL_ID_TEXT);
584                 WINHELP_SetupText(hTextWnd, win, wpage->relative);
585
586                 InvalidateRect(win->hMainWnd, NULL, TRUE);
587                 if (win->hHistoryWnd) InvalidateRect(win->hHistoryWnd, NULL, TRUE);
588
589                 break;
590             }
591         }
592     }
593
594     if (!win)
595     {
596         /* Initialize WINHELP_WINDOW struct */
597         win = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
598                         sizeof(WINHELP_WINDOW) + strlen(wpage->wininfo->name) + 1);
599         if (!win) return FALSE;
600         win->next = Globals.win_list;
601         Globals.win_list = win;
602
603         name = (char*)win + sizeof(WINHELP_WINDOW);
604         lstrcpy(name, wpage->wininfo->name);
605         win->lpszName = name;
606         win->hHandCur = LoadCursorW(0, (LPWSTR)IDC_HAND);
607         win->back.index = 0;
608         win->font_scale = 1;
609     }
610     win->page = wpage->page;
611     win->info = wpage->wininfo;
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_TOPICS, buffer, sizeof(buffer));
636         MACRO_CreateButton("BTN_TOPICS", buffer, "Finder()");
637     }
638
639     if (!bReUsed)
640     {
641         win->hMainWnd = CreateWindowEx((bPopup) ? WS_EX_TOOLWINDOW : 0, MAIN_WIN_CLASS_NAME,
642                                        WINHELP_GetCaption(wpage),
643                                        bPrimary ? WS_OVERLAPPEDWINDOW : wpage->wininfo->win_style,
644                                        wpage->wininfo->origin.x, wpage->wininfo->origin.y,
645                                        wpage->wininfo->size.cx, wpage->wininfo->size.cy,
646                                        bPopup ? Globals.active_win->hMainWnd : NULL,
647                                        bPrimary ? LoadMenu(Globals.hInstance, MAKEINTRESOURCE(MAIN_MENU)) : 0,
648                                        Globals.hInstance, win);
649         if (!bPopup)
650             /* Create button box and text Window */
651             CreateWindow(BUTTON_BOX_WIN_CLASS_NAME, "", WS_CHILD | WS_VISIBLE,
652                          0, 0, 0, 0, win->hMainWnd, (HMENU)CTL_ID_BUTTON, Globals.hInstance, NULL);
653
654         hTextWnd = CreateWindow(RICHEDIT_CLASS, NULL,
655                                 ES_MULTILINE | ES_READONLY | WS_CHILD | WS_HSCROLL | WS_VSCROLL | WS_VISIBLE,
656                                 0, 0, 0, 0, win->hMainWnd, (HMENU)CTL_ID_TEXT, Globals.hInstance, NULL);
657         SendMessage(hTextWnd, EM_SETEVENTMASK, 0,
658                     SendMessage(hTextWnd, EM_GETEVENTMASK, 0, 0) | ENM_MOUSEEVENTS);
659     }
660
661     hIcon = (wpage->page) ? wpage->page->file->hIcon : NULL;
662     if (!hIcon) hIcon = LoadIcon(Globals.hInstance, MAKEINTRESOURCE(IDI_WINHELP));
663     SendMessage(win->hMainWnd, WM_SETICON, ICON_SMALL, (DWORD_PTR)hIcon);
664
665     /* Initialize file specific pushbuttons */
666     if (!(wpage->wininfo->win_style & WS_POPUP) && wpage->page)
667     {
668         HLPFILE_MACRO  *macro;
669         for (macro = wpage->page->file->first_macro; macro; macro = macro->next)
670             MACRO_ExecuteMacro(macro->lpszMacro);
671
672         for (macro = wpage->page->first_macro; macro; macro = macro->next)
673             MACRO_ExecuteMacro(macro->lpszMacro);
674     }
675
676     if (bPopup)
677     {
678         DWORD   mask = SendMessage(hTextWnd, EM_GETEVENTMASK, 0, 0);
679         RECT    rect;
680
681         win->font_scale = Globals.active_win->font_scale;
682         WINHELP_SetupText(hTextWnd, win, wpage->relative);
683
684         /* we need the window to be shown for richedit to compute the size */
685         ShowWindow(win->hMainWnd, nCmdShow);
686         SendMessage(hTextWnd, EM_SETEVENTMASK, 0, mask | ENM_REQUESTRESIZE);
687         SendMessage(hTextWnd, EM_REQUESTRESIZE, 0, 0);
688         SendMessage(hTextWnd, EM_SETEVENTMASK, 0, mask);
689
690         GetWindowRect(win->hMainWnd, &rect);
691         win->hShadowWnd = CreateWindowEx(WS_EX_TOOLWINDOW, SHADOW_WIN_CLASS_NAME,
692                                          "", WS_POPUP | WS_VISIBLE,
693                                          rect.left + SHADOW_DX, rect.top + SHADOW_DY,
694                                          rect.right - rect.left,
695                                          rect.bottom - rect.top,
696                                          Globals.active_win->hMainWnd, 0,
697                                          Globals.hInstance, NULL);
698         SetWindowPos(win->hMainWnd, win->hShadowWnd, 0, 0, 0, 0,
699                      SWP_NOSIZE | SWP_NOMOVE);
700     }
701     else
702     {
703         WINHELP_SetupText(hTextWnd, win, wpage->relative);
704         WINHELP_LayoutMainWindow(win);
705         ShowWindow(win->hMainWnd, nCmdShow);
706     }
707
708     return TRUE;
709 }
710
711 /******************************************************************
712  *             WINHELP_OpenHelpWindow
713  * Main function to search for a page and display it in a window
714  */
715 BOOL WINHELP_OpenHelpWindow(HLPFILE_PAGE* (*lookup)(HLPFILE*, LONG, ULONG*),
716                             HLPFILE* hlpfile, LONG val, HLPFILE_WINDOWINFO* wi,
717                             int nCmdShow)
718 {
719     WINHELP_WNDPAGE     wpage;
720
721     wpage.page = lookup(hlpfile, val, &wpage.relative);
722     if (wpage.page) wpage.page->file->wRefCount++;
723     wpage.wininfo = wi;
724     return WINHELP_CreateHelpWindow(&wpage, nCmdShow, TRUE);
725 }
726
727 /***********************************************************************
728  *
729  *           WINHELP_FindLink
730  */
731 static HLPFILE_LINK* WINHELP_FindLink(WINHELP_WINDOW* win, LPARAM pos)
732 {
733     HLPFILE_LINK*           link;
734     POINTL                  mouse_ptl, char_ptl, char_next_ptl;
735     DWORD                   cp;
736
737     if (!win->page) return NULL;
738
739     mouse_ptl.x = (short)LOWORD(pos);
740     mouse_ptl.y = (short)HIWORD(pos);
741     cp = SendMessageW(GetDlgItem(win->hMainWnd, CTL_ID_TEXT), EM_CHARFROMPOS,
742                       0, (LPARAM)&mouse_ptl);
743
744     for (link = win->page->first_link; link; link = link->next)
745     {
746         if (link->cpMin <= cp && cp <= link->cpMax)
747         {
748             /* check whether we're at end of line */
749             SendMessageW(GetDlgItem(win->hMainWnd, CTL_ID_TEXT), EM_POSFROMCHAR,
750                          (LPARAM)&char_ptl, cp);
751             SendMessageW(GetDlgItem(win->hMainWnd, CTL_ID_TEXT), EM_POSFROMCHAR,
752                          (LPARAM)&char_next_ptl, cp + 1);
753             if (char_next_ptl.y != char_ptl.y || mouse_ptl.x >= char_next_ptl.x)
754                 link = NULL;
755             break;
756         }
757     }
758     return link;
759 }
760
761 /******************************************************************
762  *             WINHELP_HandleTextMouse
763  *
764  */
765 static BOOL WINHELP_HandleTextMouse(WINHELP_WINDOW* win, UINT msg, LPARAM lParam)
766 {
767     HLPFILE*                hlpfile;
768     HLPFILE_LINK*           link;
769     BOOL                    ret = FALSE;
770
771     switch (msg)
772     {
773     case WM_MOUSEMOVE:
774         if (WINHELP_FindLink(win, lParam))
775             SetCursor(win->hHandCur);
776         else
777             SetCursor(LoadCursor(0, IDC_ARROW));
778         break;
779
780      case WM_LBUTTONDOWN:
781          if ((win->current_link = WINHELP_FindLink(win, lParam)))
782              ret = TRUE;
783          break;
784
785     case WM_LBUTTONUP:
786         if ((link = WINHELP_FindLink(win, lParam)) && link == win->current_link)
787         {
788             HLPFILE_WINDOWINFO*     wi;
789
790             switch (link->cookie)
791             {
792             case hlp_link_link:
793                 if ((hlpfile = WINHELP_LookupHelpFile(link->string)))
794                 {
795                     if (link->window == -1)
796                         wi = win->info;
797                     else if (link->window < hlpfile->numWindows)
798                         wi = &hlpfile->windows[link->window];
799                     else
800                     {
801                         WINE_WARN("link to window %d/%d\n", link->window, hlpfile->numWindows);
802                         break;
803                     }
804                     WINHELP_OpenHelpWindow(HLPFILE_PageByHash, hlpfile, link->hash, wi, SW_NORMAL);
805                 }
806                 break;
807             case hlp_link_popup:
808                 if ((hlpfile = WINHELP_LookupHelpFile(link->string)))
809                     WINHELP_OpenHelpWindow(HLPFILE_PageByHash, hlpfile, link->hash,
810                                            WINHELP_GetPopupWindowInfo(hlpfile, win, lParam),
811                                            SW_NORMAL);
812                 break;
813             case hlp_link_macro:
814                 MACRO_ExecuteMacro(link->string);
815                 break;
816             default:
817                 WINE_FIXME("Unknown link cookie %d\n", link->cookie);
818             }
819             ret = TRUE;
820         }
821         win->current_link = NULL;
822         break;
823     }
824     return ret;
825 }
826
827 /***********************************************************************
828  *
829  *           WINHELP_MainWndProc
830  */
831 static LRESULT CALLBACK WINHELP_MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
832 {
833     WINHELP_WINDOW *win;
834     WINHELP_BUTTON *button;
835     RECT rect;
836     INT  curPos, min, max, dy, keyDelta;
837     HWND hTextWnd;
838     LRESULT ret;
839
840     if (WINHELP_CheckPopup(hWnd, msg, wParam, lParam, &ret)) return ret;
841
842     switch (msg)
843     {
844     case WM_NCCREATE:
845         win = (WINHELP_WINDOW*) ((LPCREATESTRUCT) lParam)->lpCreateParams;
846         SetWindowLongPtr(hWnd, 0, (ULONG_PTR) win);
847         if (!win->page && Globals.isBook)
848             PostMessage(hWnd, WM_COMMAND, MNID_FILE_OPEN, 0);
849         win->hMainWnd = hWnd;
850         break;
851
852     case WM_WINDOWPOSCHANGED:
853         WINHELP_LayoutMainWindow((WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0));
854         break;
855
856     case WM_COMMAND:
857         win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
858         switch (wParam)
859         {
860             /* Menu FILE */
861         case MNID_FILE_OPEN:    MACRO_FileOpen();       break;
862         case MNID_FILE_PRINT:   MACRO_Print();          break;
863         case MNID_FILE_SETUP:   MACRO_PrinterSetup();   break;
864         case MNID_FILE_EXIT:    MACRO_Exit();           break;
865
866             /* Menu EDIT */
867         case MNID_EDIT_COPYDLG:
868             SendMessage(GetDlgItem(hWnd, CTL_ID_TEXT), WM_COPY, 0, 0);
869             break;
870         case MNID_EDIT_ANNOTATE:MACRO_Annotate();       break;
871
872             /* Menu Bookmark */
873         case MNID_BKMK_DEFINE:  MACRO_BookmarkDefine(); break;
874
875             /* Menu Help */
876         case MNID_HELP_HELPON:  MACRO_HelpOn();         break;
877         case MNID_HELP_HELPTOP: MACRO_HelpOnTop();      break;
878         case MNID_HELP_ABOUT:   MACRO_About();          break;
879         case MNID_HELP_WINE:    ShellAbout(hWnd, "WINE", "Help", 0); break;
880
881             /* Context help */
882         case MNID_CTXT_ANNOTATE:MACRO_Annotate();       break;
883         case MNID_CTXT_COPY:    MACRO_CopyDialog();     break;
884         case MNID_CTXT_PRINT:   MACRO_Print();          break;
885         case MNID_OPTS_HISTORY: MACRO_History();        break;
886         case MNID_OPTS_FONTS_SMALL:
887         case MNID_CTXT_FONTS_SMALL:
888             win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
889             if (win->font_scale != 0)
890             {
891                 win->font_scale = 0;
892                 WINHELP_SetupText(GetDlgItem(hWnd, CTL_ID_TEXT), win, 0 /* FIXME */);
893             }
894             break;
895         case MNID_OPTS_FONTS_NORMAL:
896         case MNID_CTXT_FONTS_NORMAL:
897             win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
898             if (win->font_scale != 1)
899             {
900                 win->font_scale = 1;
901                 WINHELP_SetupText(GetDlgItem(hWnd, CTL_ID_TEXT), win, 0 /* FIXME */);
902             }
903             break;
904         case MNID_OPTS_FONTS_LARGE:
905         case MNID_CTXT_FONTS_LARGE:
906             win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
907             if (win->font_scale != 2)
908             {
909                 win->font_scale = 2;
910                 WINHELP_SetupText(GetDlgItem(hWnd, CTL_ID_TEXT), win, 0 /* FIXME */);
911             }
912             break;
913         case MNID_OPTS_HELP_DEFAULT:
914         case MNID_OPTS_HELP_VISIBLE:
915         case MNID_OPTS_HELP_NONVISIBLE:
916         case MNID_OPTS_SYSTEM_COLORS:
917         case MNID_CTXT_HELP_DEFAULT:
918         case MNID_CTXT_HELP_VISIBLE:
919         case MNID_CTXT_HELP_NONVISIBLE:
920         case MNID_CTXT_SYSTEM_COLORS:
921             /* FIXME: NIY */
922
923         default:
924             /* Buttons */
925             for (button = win->first_button; button; button = button->next)
926                 if (wParam == button->wParam) break;
927             if (button)
928                 MACRO_ExecuteMacro(button->lpszMacro);
929             else if (!HIWORD(wParam))
930                 MessageBox(0, MAKEINTRESOURCE(STID_NOT_IMPLEMENTED),
931                            MAKEINTRESOURCE(STID_WHERROR), MB_OK);
932             break;
933         }
934         break;
935 /* EPP     case WM_DESTROY: */
936 /* EPP         if (Globals.hPopupWnd) DestroyWindow(Globals.hPopupWnd); */
937 /* EPP         break; */
938     case WM_COPYDATA:
939         return WINHELP_HandleCommand((HWND)wParam, lParam);
940
941     case WM_CHAR:
942         if (wParam == 3)
943         {
944             SendMessage(GetDlgItem(hWnd, CTL_ID_TEXT), WM_COPY, 0, 0);
945             return 0;
946         }
947         break;
948
949     case WM_KEYDOWN:
950         keyDelta = 0;
951
952         switch (wParam)
953         {
954         case VK_UP:
955         case VK_DOWN:
956             keyDelta = GetSystemMetrics(SM_CXVSCROLL);
957             if (wParam == VK_UP)
958                 keyDelta = -keyDelta;
959
960         case VK_PRIOR:
961         case VK_NEXT:
962             win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
963             hTextWnd = GetDlgItem(win->hMainWnd, CTL_ID_TEXT);
964             curPos = GetScrollPos(hTextWnd, SB_VERT);
965             GetScrollRange(hTextWnd, SB_VERT, &min, &max);
966
967             if (keyDelta == 0)
968             {            
969                 GetClientRect(hTextWnd, &rect);
970                 keyDelta = (rect.bottom - rect.top) / 2;
971                 if (wParam == VK_PRIOR)
972                     keyDelta = -keyDelta;
973             }
974
975             curPos += keyDelta;
976             if (curPos > max)
977                  curPos = max;
978             else if (curPos < min)
979                  curPos = min;
980
981             dy = GetScrollPos(hTextWnd, SB_VERT) - curPos;
982             SetScrollPos(hTextWnd, SB_VERT, curPos, TRUE);
983             ScrollWindow(hTextWnd, 0, dy, NULL, NULL);
984             UpdateWindow(hTextWnd);
985             return 0;
986
987         case VK_ESCAPE:
988             MACRO_Exit();
989             return 0;
990         }
991         break;
992
993     case WM_NOTIFY:
994         if (wParam == CTL_ID_TEXT)
995         {
996             RECT        rc;
997
998             switch (((NMHDR*)lParam)->code)
999             {
1000             case EN_MSGFILTER:
1001                 {
1002                     const MSGFILTER*    msgf = (const MSGFILTER*)lParam;
1003                     switch (msgf->msg)
1004                     {
1005                     case WM_KEYUP:
1006                         if (msgf->wParam == VK_ESCAPE) DestroyWindow(hWnd);
1007                         break;
1008                     case WM_RBUTTONDOWN:
1009                     {
1010                         HMENU       hMenu;
1011                         POINT       pt;
1012
1013                         win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1014                         hMenu = LoadMenu(Globals.hInstance, (LPSTR)CONTEXT_MENU);
1015                         switch (win->font_scale)
1016                         {
1017                         case 0:
1018                             CheckMenuItem(hMenu, MNID_CTXT_FONTS_SMALL,
1019                                           MF_BYCOMMAND|MF_CHECKED);
1020                             break;
1021                         default:
1022                             WINE_FIXME("Unsupported %d\n", win->font_scale);
1023                         case 1:
1024                             CheckMenuItem(hMenu, MNID_CTXT_FONTS_NORMAL,
1025                                           MF_BYCOMMAND|MF_CHECKED);
1026                             break;
1027                         case 2:
1028                             CheckMenuItem(hMenu, MNID_CTXT_FONTS_LARGE,
1029                                           MF_BYCOMMAND|MF_CHECKED);
1030                             break;
1031                         }
1032                         pt.x = (int)(short)LOWORD(msgf->lParam);
1033                         pt.y = (int)(short)HIWORD(msgf->lParam);
1034                         ClientToScreen(msgf->nmhdr.hwndFrom, &pt);
1035                         TrackPopupMenu(GetSubMenu(hMenu, 0), TPM_LEFTALIGN|TPM_TOPALIGN,
1036                                        pt.x, pt.y, 0, hWnd, NULL);
1037                         DestroyMenu(hMenu);
1038                     }
1039                     break;
1040                     default:
1041                         return WINHELP_HandleTextMouse((WINHELP_WINDOW*)GetWindowLongPtr(hWnd, 0),
1042                                                        msgf->msg, msgf->lParam);
1043                     }
1044                 }
1045                 break;
1046
1047             case EN_REQUESTRESIZE:
1048                 rc = ((REQRESIZE*)lParam)->rc;
1049                 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1050                 AdjustWindowRect(&rc, GetWindowLong(win->hMainWnd, GWL_STYLE),
1051                                  FALSE);
1052                 SetWindowPos(win->hMainWnd, HWND_TOP, 0, 0,
1053                              rc.right - rc.left, rc.bottom - rc.top,
1054                              SWP_NOMOVE | SWP_NOZORDER);
1055                 WINHELP_LayoutMainWindow(win);
1056                 break;
1057             }
1058         }
1059         break;
1060
1061     case WM_INITMENUPOPUP:
1062         win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1063         CheckMenuItem((HMENU)wParam, MNID_OPTS_FONTS_SMALL,
1064                       MF_BYCOMMAND | (win->font_scale == 0) ? MF_CHECKED : 0);
1065         CheckMenuItem((HMENU)wParam, MNID_OPTS_FONTS_NORMAL,
1066                       MF_BYCOMMAND | (win->font_scale == 1) ? MF_CHECKED : 0);
1067         CheckMenuItem((HMENU)wParam, MNID_OPTS_FONTS_LARGE,
1068                       MF_BYCOMMAND | (win->font_scale == 2) ? MF_CHECKED : 0);
1069         break;
1070
1071     case WM_NCDESTROY:
1072         {
1073             BOOL bExit;
1074             win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1075             bExit = (Globals.wVersion >= 4 && !lstrcmpi(win->lpszName, "main"));
1076             WINHELP_DeleteWindow(win);
1077
1078             if (bExit) MACRO_Exit();
1079             if (!Globals.win_list)
1080                 PostQuitMessage(0);
1081         }
1082         break;
1083     }
1084     return DefWindowProc(hWnd, msg, wParam, lParam);
1085 }
1086
1087 static DWORD CALLBACK WINHELP_RtfStreamIn(DWORD_PTR cookie, BYTE* buff,
1088                                           LONG cb, LONG* pcb)
1089 {
1090     struct RtfData*     rd = (struct RtfData*)cookie;
1091
1092     if (rd->where >= rd->ptr) return 1;
1093     if (rd->where + cb > rd->ptr)
1094         cb = rd->ptr - rd->where;
1095     memcpy(buff, rd->where, cb);
1096     rd->where += cb;
1097     *pcb = cb;
1098     return 0;
1099 }
1100
1101 static void WINHELP_SetupText(HWND hTextWnd, WINHELP_WINDOW* win, ULONG relative)
1102 {
1103     SendMessage(hTextWnd, WM_SETREDRAW, FALSE, 0);
1104     SendMessage(hTextWnd, EM_SETBKGNDCOLOR, 0, (LPARAM)win->info->sr_color);
1105     /* set word-wrap to window size (undocumented) */
1106     SendMessage(hTextWnd, EM_SETTARGETDEVICE, 0, 0);
1107     if (win->page)
1108     {
1109         struct RtfData  rd;
1110         EDITSTREAM      es;
1111         unsigned        cp = 0;
1112         POINTL          ptl;
1113         POINT           pt;
1114
1115
1116         if (HLPFILE_BrowsePage(win->page, &rd, win->font_scale, relative))
1117         {
1118             rd.where = rd.data;
1119             es.dwCookie = (DWORD_PTR)&rd;
1120             es.dwError = 0;
1121             es.pfnCallback = WINHELP_RtfStreamIn;
1122
1123             SendMessageW(hTextWnd, EM_STREAMIN, SF_RTF, (LPARAM)&es);
1124             cp = rd.char_pos_rel;
1125         }
1126         /* FIXME: else leaking potentially the rd.first_link chain */
1127         HeapFree(GetProcessHeap(), 0, rd.data);
1128         SendMessage(hTextWnd, EM_POSFROMCHAR, (WPARAM)&ptl, cp ? cp - 1 : 0);
1129         pt.x = 0; pt.y = ptl.y;
1130         SendMessage(hTextWnd, EM_SETSCROLLPOS, 0, (LPARAM)&pt);
1131     }
1132     else
1133     {
1134         SendMessage(hTextWnd, WM_SETTEXT, 0, (LPARAM)"");
1135     }
1136     SendMessage(hTextWnd, WM_SETREDRAW, TRUE, 0);
1137     InvalidateRect(hTextWnd, NULL, TRUE);
1138 }
1139
1140 /***********************************************************************
1141  *
1142  *           WINHELP_ButtonBoxWndProc
1143  */
1144 static LRESULT CALLBACK WINHELP_ButtonBoxWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1145 {
1146     WINDOWPOS      *winpos;
1147     WINHELP_WINDOW *win;
1148     WINHELP_BUTTON *button;
1149     SIZE button_size;
1150     INT  x, y;
1151
1152     if (WINHELP_CheckPopup(hWnd, msg, wParam, lParam, NULL)) return 0L;
1153
1154     switch (msg)
1155     {
1156     case WM_WINDOWPOSCHANGING:
1157         winpos = (WINDOWPOS*) lParam;
1158         win = (WINHELP_WINDOW*) GetWindowLongPtr(GetParent(hWnd), 0);
1159
1160         /* Update buttons */
1161         button_size.cx = 0;
1162         button_size.cy = 0;
1163         for (button = win->first_button; button; button = button->next)
1164         {
1165             HDC  hDc;
1166             SIZE textsize;
1167             if (!button->hWnd)
1168             {
1169                 button->hWnd = CreateWindow(STRING_BUTTON, button->lpszName,
1170                                             WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
1171                                             0, 0, 0, 0,
1172                                             hWnd, (HMENU) button->wParam,
1173                                             Globals.hInstance, 0);
1174                 if (button->hWnd) {
1175                     if (Globals.button_proc == NULL)
1176                         Globals.button_proc = (WNDPROC) GetWindowLongPtr(button->hWnd, GWLP_WNDPROC);
1177                     SetWindowLongPtr(button->hWnd, GWLP_WNDPROC, (LONG_PTR) WINHELP_ButtonWndProc);
1178                 }
1179             }
1180             hDc = GetDC(button->hWnd);
1181             GetTextExtentPoint(hDc, button->lpszName,
1182                                lstrlen(button->lpszName), &textsize);
1183             ReleaseDC(button->hWnd, hDc);
1184
1185             button_size.cx = max(button_size.cx, textsize.cx + BUTTON_CX);
1186             button_size.cy = max(button_size.cy, textsize.cy + BUTTON_CY);
1187         }
1188
1189         x = 0;
1190         y = 0;
1191         for (button = win->first_button; button; button = button->next)
1192         {
1193             SetWindowPos(button->hWnd, HWND_TOP, x, y, button_size.cx, button_size.cy, 0);
1194
1195             if (x + 2 * button_size.cx <= winpos->cx)
1196                 x += button_size.cx;
1197             else
1198                 x = 0, y += button_size.cy;
1199         }
1200         winpos->cy = y + (x ? button_size.cy : 0);
1201         break;
1202
1203     case WM_COMMAND:
1204         SendMessage(GetParent(hWnd), msg, wParam, lParam);
1205         break;
1206
1207     case WM_KEYDOWN:
1208         switch (wParam)
1209         {
1210         case VK_UP:
1211         case VK_DOWN:
1212         case VK_PRIOR:
1213         case VK_NEXT:
1214         case VK_ESCAPE:
1215             return SendMessage(GetParent(hWnd), msg, wParam, lParam);
1216         }
1217         break;
1218     }
1219
1220     return DefWindowProc(hWnd, msg, wParam, lParam);
1221 }
1222
1223 /***********************************************************************
1224  *
1225  *           WINHELP_ButtonWndProc
1226  */
1227 static LRESULT CALLBACK WINHELP_ButtonWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1228 {
1229     if (WINHELP_CheckPopup(hWnd, msg, wParam, lParam, NULL)) return 0;
1230
1231     if (msg == WM_KEYDOWN)
1232     {
1233         switch (wParam)
1234         {
1235         case VK_UP:
1236         case VK_DOWN:
1237         case VK_PRIOR:
1238         case VK_NEXT:
1239         case VK_ESCAPE:
1240             return SendMessage(GetParent(hWnd), msg, wParam, lParam);
1241         }
1242     }
1243
1244     return CallWindowProc(Globals.button_proc, hWnd, msg, wParam, lParam);
1245 }
1246
1247 /******************************************************************
1248  *              WINHELP_HistoryWndProc
1249  *
1250  *
1251  */
1252 static LRESULT CALLBACK WINHELP_HistoryWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1253 {
1254     WINHELP_WINDOW*     win;
1255     PAINTSTRUCT         ps;
1256     HDC                 hDc;
1257     TEXTMETRIC          tm;
1258     unsigned int        i;
1259     RECT                r;
1260
1261     switch (msg)
1262     {
1263     case WM_NCCREATE:
1264         win = (WINHELP_WINDOW*)((LPCREATESTRUCT)lParam)->lpCreateParams;
1265         SetWindowLongPtr(hWnd, 0, (ULONG_PTR)win);
1266         win->hHistoryWnd = hWnd;
1267         break;
1268     case WM_CREATE:
1269         win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1270         hDc = GetDC(hWnd);
1271         GetTextMetrics(hDc, &tm);
1272         GetWindowRect(hWnd, &r);
1273
1274         r.right = r.left + 30 * tm.tmAveCharWidth;
1275         r.bottom = r.top + (sizeof(Globals.history.set) / sizeof(Globals.history.set[0])) * tm.tmHeight;
1276         AdjustWindowRect(&r, GetWindowLong(hWnd, GWL_STYLE), FALSE);
1277         if (r.left < 0) {r.right -= r.left; r.left = 0;}
1278         if (r.top < 0) {r.bottom -= r.top; r.top = 0;}
1279
1280         MoveWindow(hWnd, r.left, r.top, r.right, r.bottom, TRUE);
1281         ReleaseDC(hWnd, hDc);
1282         break;
1283     case WM_LBUTTONDOWN:
1284         win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1285         hDc = GetDC(hWnd);
1286         GetTextMetrics(hDc, &tm);
1287         i = HIWORD(lParam) / tm.tmHeight;
1288         if (i < Globals.history.index)
1289             WINHELP_CreateHelpWindow(&Globals.history.set[i], SW_SHOW, TRUE);
1290         ReleaseDC(hWnd, hDc);
1291         break;
1292     case WM_PAINT:
1293         hDc = BeginPaint(hWnd, &ps);
1294         win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1295         GetTextMetrics(hDc, &tm);
1296
1297         for (i = 0; i < Globals.history.index; i++)
1298         {
1299             if (Globals.history.set[i].page->file == Globals.active_win->page->file)
1300             {
1301                 TextOut(hDc, 0, i * tm.tmHeight,
1302                         Globals.history.set[i].page->lpszTitle,
1303                         strlen(Globals.history.set[i].page->lpszTitle));
1304             }
1305             else
1306             {
1307                 char        buffer[1024];
1308                 const char* ptr1;
1309                 const char* ptr2;
1310                 unsigned    len;
1311
1312                 ptr1 = strrchr(Globals.history.set[i].page->file->lpszPath, '\\');
1313                 if (!ptr1) ptr1 = Globals.history.set[i].page->file->lpszPath;
1314                 else ptr1++;
1315                 ptr2 = strrchr(ptr1, '.');
1316                 len = ptr2 ? ptr2 - ptr1 : strlen(ptr1);
1317                 if (len > sizeof(buffer)) len = sizeof(buffer);
1318                 memcpy(buffer, ptr1, len);
1319                 if (len < sizeof(buffer)) buffer[len++] = ':';
1320                 strncpy(&buffer[len], Globals.history.set[i].page->lpszTitle, sizeof(buffer) - len);
1321                 buffer[sizeof(buffer) - 1] = '\0';
1322                 TextOut(hDc, 0, i * tm.tmHeight, buffer, strlen(buffer));
1323             }
1324         }
1325         EndPaint(hWnd, &ps);
1326         break;
1327     case WM_DESTROY:
1328         win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1329         if (hWnd == win->hHistoryWnd)
1330             win->hHistoryWnd = 0;
1331         break;
1332     }
1333     return DefWindowProc(hWnd, msg, wParam, lParam);
1334 }
1335
1336 /***********************************************************************
1337  *
1338  *           WINHELP_ShadowWndProc
1339  */
1340 static LRESULT CALLBACK WINHELP_ShadowWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1341 {
1342     if (WINHELP_CheckPopup(hWnd, msg, wParam, lParam, NULL)) return 0;
1343     return WINHELP_CheckPopup(hWnd, msg, wParam, lParam, NULL) ? 0L : DefWindowProc(hWnd, msg, wParam, lParam);
1344 }
1345
1346 /***********************************************************************
1347  *
1348  *           WINHELP_CheckPopup
1349  */
1350 static BOOL WINHELP_CheckPopup(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, LRESULT* lret)
1351 {
1352     HWND        hPopup;
1353
1354     if (!Globals.active_popup) return FALSE;
1355
1356     switch (msg)
1357     {
1358     case WM_NOTIFY:
1359         {
1360             MSGFILTER*  msgf = (MSGFILTER*)lParam;
1361             if (msgf->nmhdr.code == EN_MSGFILTER)
1362             {
1363                 if (!WINHELP_CheckPopup(hWnd, msgf->msg, msgf->wParam, msgf->lParam, NULL))
1364                     return FALSE;
1365                 if (lret) *lret = 1;
1366                 return TRUE;
1367             }
1368         }
1369         break;
1370     case WM_ACTIVATE:
1371         if (wParam != WA_INACTIVE || (HWND)lParam == Globals.active_win->hMainWnd ||
1372             (HWND)lParam == Globals.active_popup->hMainWnd ||
1373             GetWindow((HWND)lParam, GW_OWNER) == Globals.active_win->hMainWnd)
1374             break;
1375     case WM_LBUTTONUP:
1376     case WM_LBUTTONDOWN:
1377         if (WINHELP_HandleTextMouse(Globals.active_popup, msg, lParam) && msg == WM_LBUTTONDOWN)
1378             return FALSE;
1379         /* fall through */
1380     case WM_MBUTTONDOWN:
1381     case WM_RBUTTONDOWN:
1382     case WM_NCLBUTTONDOWN:
1383     case WM_NCMBUTTONDOWN:
1384     case WM_NCRBUTTONDOWN:
1385         hPopup = Globals.active_popup->hMainWnd;
1386         Globals.active_popup = NULL;
1387         DestroyWindow(hPopup);
1388         return TRUE;
1389     }
1390     return FALSE;
1391 }
1392
1393 /******************************************************************
1394  *              WINHELP_DeleteButtons
1395  *
1396  */
1397 static void WINHELP_DeleteButtons(WINHELP_WINDOW* win)
1398 {
1399     WINHELP_BUTTON*     b;
1400     WINHELP_BUTTON*     bp;
1401
1402     for (b = win->first_button; b; b = bp)
1403     {
1404         DestroyWindow(b->hWnd);
1405         bp = b->next;
1406         HeapFree(GetProcessHeap(), 0, b);
1407     }
1408     win->first_button = NULL;
1409 }
1410
1411 /******************************************************************
1412  *              WINHELP_DeleteBackSet
1413  *
1414  */
1415 void WINHELP_DeleteBackSet(WINHELP_WINDOW* win)
1416 {
1417     unsigned int i;
1418
1419     for (i = 0; i < win->back.index; i++)
1420     {
1421         HLPFILE_FreeHlpFile(win->back.set[i].page->file);
1422         win->back.set[i].page = NULL;
1423     }
1424     win->back.index = 0;
1425 }
1426
1427 /******************************************************************
1428  *             WINHELP_DeletePageLinks
1429  *
1430  */
1431 static void WINHELP_DeletePageLinks(HLPFILE_PAGE* page)
1432 {
1433     HLPFILE_LINK*       curr;
1434     HLPFILE_LINK*       next;
1435
1436     for (curr = page->first_link; curr; curr = next)
1437     {
1438         next = curr->next;
1439         HeapFree(GetProcessHeap(), 0, curr);
1440     }
1441 }
1442
1443 /***********************************************************************
1444  *
1445  *           WINHELP_DeleteWindow
1446  */
1447 static void WINHELP_DeleteWindow(WINHELP_WINDOW* win)
1448 {
1449     WINHELP_WINDOW**    w;
1450
1451     for (w = &Globals.win_list; *w; w = &(*w)->next)
1452     {
1453         if (*w == win)
1454         {
1455             *w = win->next;
1456             break;
1457         }
1458     }
1459
1460     if (Globals.active_win == win)
1461     {
1462         Globals.active_win = Globals.win_list;
1463         if (Globals.win_list)
1464             SetActiveWindow(Globals.win_list->hMainWnd);
1465     }
1466
1467     if (win == Globals.active_popup)
1468         Globals.active_popup = NULL;
1469
1470     WINHELP_DeleteButtons(win);
1471
1472     if (win->page) WINHELP_DeletePageLinks(win->page);
1473     if (win->hShadowWnd) DestroyWindow(win->hShadowWnd);
1474     if (win->hHistoryWnd) DestroyWindow(win->hHistoryWnd);
1475
1476     DeleteObject(win->hBrush);
1477
1478     WINHELP_DeleteBackSet(win);
1479
1480     if (win->page) HLPFILE_FreeHlpFile(win->page->file);
1481     HeapFree(GetProcessHeap(), 0, win);
1482 }
1483
1484 /***********************************************************************
1485  *
1486  *           WINHELP_InitFonts
1487  */
1488 static void WINHELP_InitFonts(HWND hWnd)
1489 {
1490     WINHELP_WINDOW *win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1491     LOGFONT logfontlist[] = {
1492         {-10, 0, 0, 0, 400, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 32, "Helv"},
1493         {-12, 0, 0, 0, 700, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 32, "Helv"},
1494         {-12, 0, 0, 0, 700, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 32, "Helv"},
1495         {-12, 0, 0, 0, 400, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 32, "Helv"},
1496         {-12, 0, 0, 0, 700, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 32, "Helv"},
1497         {-10, 0, 0, 0, 700, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 32, "Helv"},
1498         { -8, 0, 0, 0, 400, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 32, "Helv"}};
1499 #define FONTS_LEN (sizeof(logfontlist)/sizeof(*logfontlist))
1500
1501     static HFONT fonts[FONTS_LEN];
1502     static BOOL init = 0;
1503
1504     win->fonts_len = FONTS_LEN;
1505     win->fonts = fonts;
1506
1507     if (!init)
1508     {
1509         UINT i;
1510
1511         for (i = 0; i < FONTS_LEN; i++)
1512         {
1513             fonts[i] = CreateFontIndirect(&logfontlist[i]);
1514         }
1515
1516         init = 1;
1517     }
1518 }
1519
1520 /***********************************************************************
1521  *
1522  *           WINHELP_MessageBoxIDS_s
1523  */
1524 INT WINHELP_MessageBoxIDS_s(UINT ids_text, LPCSTR str, UINT ids_title, WORD type)
1525 {
1526     CHAR text[MAX_STRING_LEN];
1527     CHAR newtext[MAX_STRING_LEN + MAX_PATH];
1528
1529     LoadString(Globals.hInstance, ids_text, text, sizeof(text));
1530     wsprintf(newtext, text, str);
1531
1532     return MessageBox(0, newtext, MAKEINTRESOURCE(ids_title), type);
1533 }
1534
1535 /**************************************************************************
1536  * cb_KWBTree
1537  *
1538  * HLPFILE_BPTreeCallback enumeration function for '|KWBTREE' internal file.
1539  *
1540  */
1541 static void cb_KWBTree(void *p, void **next, void *cookie)
1542 {
1543     HWND hListWnd = (HWND)cookie;
1544     int count;
1545
1546     WINE_TRACE("Adding '%s' to search list\n", (char *)p);
1547     SendMessage(hListWnd, LB_INSERTSTRING, -1, (LPARAM)p);
1548     count = SendMessage(hListWnd, LB_GETCOUNT, 0, 0);
1549     SendMessage(hListWnd, LB_SETITEMDATA, count-1, (LPARAM)p);
1550     *next = (char*)p + strlen((char*)p) + 7;
1551 }
1552
1553 struct index_data
1554 {
1555     HLPFILE*    hlpfile;
1556     BOOL        jump;
1557     ULONG       offset;
1558 };
1559
1560 /**************************************************************************
1561  * WINHELP_IndexDlgProc
1562  *
1563  * Index dialog callback function.
1564  *
1565  * nResult passed to EndDialog:
1566  *   1: CANCEL button
1567  *  >1: valid offset value +2.
1568  *  EndDialog itself can return 0 (error).
1569  */
1570 INT_PTR CALLBACK WINHELP_IndexDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1571 {
1572     static struct index_data* id;
1573     int sel;
1574
1575     switch (msg)
1576     {
1577     case WM_INITDIALOG:
1578         id = (struct index_data*)((PROPSHEETPAGE*)lParam)->lParam;
1579         HLPFILE_BPTreeEnum(id->hlpfile->kwbtree, cb_KWBTree,
1580                            GetDlgItem(hWnd, IDC_INDEXLIST));
1581         id->jump = FALSE;
1582         id->offset = 1;
1583         return TRUE;
1584     case WM_NOTIFY:
1585         switch (((NMHDR*)lParam)->code)
1586         {
1587         case PSN_APPLY:
1588             sel = SendDlgItemMessage(hWnd, IDC_INDEXLIST, LB_GETCURSEL, 0, 0);
1589             if (sel != LB_ERR)
1590             {
1591                 BYTE *p;
1592                 int count;
1593
1594                 p = (BYTE*)SendDlgItemMessage(hWnd, IDC_INDEXLIST,
1595                                               LB_GETITEMDATA, sel, 0);
1596                 count = *(short*)((char *)p + strlen((char *)p) + 1);
1597                 if (count > 1)
1598                 {
1599                     MessageBox(hWnd, "count > 1 not supported yet", "Error", MB_OK | MB_ICONSTOP);
1600                     SetWindowLongPtr(hWnd, DWLP_MSGRESULT, PSNRET_INVALID);
1601                     return TRUE;
1602                 }
1603                 id->offset = *(ULONG*)((char *)p + strlen((char *)p) + 3);
1604                 id->offset = *(long*)(id->hlpfile->kwdata + id->offset + 9);
1605                 if (id->offset == 0xFFFFFFFF)
1606                 {
1607                     MessageBox(hWnd, "macro keywords not supported yet", "Error", MB_OK | MB_ICONSTOP);
1608                     SetWindowLongPtr(hWnd, DWLP_MSGRESULT, PSNRET_INVALID);
1609                     return TRUE;
1610                 }
1611                 id->jump = TRUE;
1612                 SetWindowLongPtr(hWnd, DWLP_MSGRESULT, PSNRET_NOERROR);
1613             }
1614             return TRUE;
1615         default:
1616             return FALSE;
1617         }
1618         break;
1619     default:
1620         break;
1621     }
1622     return FALSE;
1623 }
1624
1625 /**************************************************************************
1626  * WINHELP_CreateIndexWindow
1627  *
1628  * Displays a dialog with keywords of current help file.
1629  *
1630  */
1631 BOOL WINHELP_CreateIndexWindow(void)
1632 {
1633     HPROPSHEETPAGE      psPage[3];
1634     PROPSHEETPAGE       psp;
1635     PROPSHEETHEADER     psHead;
1636     struct index_data   id;
1637     char                buf[256];
1638
1639     if (Globals.active_win && Globals.active_win->page && Globals.active_win->page->file)
1640         id.hlpfile = Globals.active_win->page->file;
1641     else
1642         return FALSE;
1643
1644     if (id.hlpfile->kwbtree == NULL)
1645     {
1646         WINE_TRACE("No index provided\n");
1647         return FALSE;
1648     }
1649
1650     InitCommonControls();
1651
1652     id.jump = FALSE;
1653     memset(&psp, 0, sizeof(psp));
1654     psp.dwSize = sizeof(psp);
1655     psp.dwFlags = 0;
1656     psp.hInstance = Globals.hInstance;
1657
1658     psp.u.pszTemplate = MAKEINTRESOURCE(IDD_INDEX);
1659     psp.lParam = (LPARAM)&id;
1660     psp.pfnDlgProc = WINHELP_IndexDlgProc;
1661     psPage[0] = CreatePropertySheetPage(&psp);
1662
1663     memset(&psHead, 0, sizeof(psHead));
1664     psHead.dwSize = sizeof(psHead);
1665
1666     LoadString(Globals.hInstance, STID_PSH_INDEX, buf, sizeof(buf));
1667     strcat(buf, Globals.active_win->info->caption);
1668
1669     psHead.pszCaption = buf;
1670     psHead.nPages = 1;
1671     psHead.hwndParent = Globals.active_win->hMainWnd;
1672     psHead.u3.phpage = psPage;
1673     psHead.dwFlags = PSH_NOAPPLYNOW;
1674
1675     PropertySheet(&psHead);
1676     if (id.jump)
1677     {
1678         WINE_TRACE("got %d as an offset\n", id.offset);
1679         WINHELP_OpenHelpWindow(HLPFILE_PageByOffset, id.hlpfile, id.offset,
1680                                Globals.active_win->info, SW_NORMAL);
1681     }
1682     return TRUE;
1683 }