winhlp32: Updated Swedish translation.
[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  *              2008 Kirill K. Smirnov <lich@math.spbu.ru>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24
25 #include <assert.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #include <stdlib.h>
30
31 #define NONAMELESSUNION
32 #define NONAMELESSSTRUCT
33
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wingdi.h"
37 #include "winuser.h"
38 #include "commdlg.h"
39 #include "winhelp.h"
40 #include "winhelp_res.h"
41 #include "shellapi.h"
42 #include "richedit.h"
43 #include "commctrl.h"
44
45 #include "wine/debug.h"
46
47 WINE_DEFAULT_DEBUG_CHANNEL(winhelp);
48
49 static BOOL    WINHELP_RegisterWinClasses(void);
50 static LRESULT CALLBACK WINHELP_MainWndProc(HWND, UINT, WPARAM, LPARAM);
51 static LRESULT CALLBACK WINHELP_ButtonBoxWndProc(HWND, UINT, WPARAM, LPARAM);
52 static LRESULT CALLBACK WINHELP_ButtonWndProc(HWND, UINT, WPARAM, LPARAM);
53 static LRESULT CALLBACK WINHELP_HistoryWndProc(HWND, UINT, WPARAM, LPARAM);
54 static LRESULT CALLBACK WINHELP_ShadowWndProc(HWND, UINT, WPARAM, LPARAM);
55 static BOOL    WINHELP_CheckPopup(HWND, UINT, WPARAM, LPARAM, LRESULT*);
56 static void    WINHELP_InitFonts(HWND hWnd);
57 static void    WINHELP_DeleteWindow(WINHELP_WINDOW*);
58 static void    WINHELP_DeleteButtons(WINHELP_WINDOW*);
59 static void    WINHELP_SetupText(HWND hWnd, WINHELP_WINDOW *win, ULONG relative);
60 static void    WINHELP_DeletePageLinks(HLPFILE_PAGE* page);
61
62 WINHELP_GLOBALS Globals = {3, NULL, TRUE, NULL, NULL, NULL, NULL, NULL, {{{NULL,NULL}},0}, NULL};
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, LPARAM 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.x = (short)LOWORD(mouse);
224     wi.origin.y = (short)HIWORD(mouse);
225     ClientToScreen(parent->hMainWnd, &wi.origin);
226     wi.origin.x -= wi.size.cx / 2;
227     wi.origin.x  = min(wi.origin.x, GetSystemMetrics(SM_CXSCREEN) - wi.size.cx);
228     wi.origin.x  = max(wi.origin.x, 0);
229
230     wi.style = SW_SHOW;
231     wi.win_style = WS_POPUP | WS_BORDER;
232     if (parent->page->file->has_popup_color)
233         wi.sr_color = parent->page->file->popup_color;
234     else
235         wi.sr_color = parent->info->sr_color;
236     wi.nsr_color = 0xFFFFFF;
237
238     return &wi;
239 }
240
241 /***********************************************************************
242  *
243  *           WinMain
244  */
245 int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
246 {
247     MSG                 msg;
248     LONG                lHash = 0;
249     HLPFILE*            hlpfile;
250     static CHAR         default_wndname[] = "main";
251     LPSTR               wndname = default_wndname;
252     WINHELP_DLL*        dll;
253
254     Globals.hInstance = hInstance;
255
256     if (LoadLibrary("riched20.dll") == NULL)
257         return MessageBox(0, MAKEINTRESOURCE(STID_NO_RICHEDIT),
258                           MAKEINTRESOURCE(STID_WHERROR), MB_OK);
259
260     /* Get options */
261     while (*cmdline && (*cmdline == ' ' || *cmdline == '-'))
262     {
263         CHAR   option;
264         LPCSTR topic_id;
265         if (*cmdline++ == ' ') continue;
266
267         option = *cmdline;
268         if (option) cmdline++;
269         while (*cmdline && *cmdline == ' ') cmdline++;
270         switch (option)
271         {
272         case 'i':
273         case 'I':
274             topic_id = cmdline;
275             while (*cmdline && *cmdline != ' ') cmdline++;
276             if (*cmdline) *cmdline++ = '\0';
277             lHash = HLPFILE_Hash(topic_id);
278             break;
279
280         case '3':
281         case '4':
282             Globals.wVersion = option - '0';
283             break;
284
285         case 'x':
286             show = SW_HIDE; 
287             Globals.isBook = FALSE;
288             break;
289
290         default:
291             WINE_FIXME("Unsupported cmd line: %s\n", cmdline);
292             break;
293         }
294     }
295
296     /* Create primary window */
297     if (!WINHELP_RegisterWinClasses())
298     {
299         WINE_FIXME("Couldn't register classes\n");
300         return 0;
301     }
302
303     if (*cmdline)
304     {
305         char*   ptr;
306         if ((*cmdline == '"') && (ptr = strchr(cmdline+1, '"')))
307         {
308             cmdline++;
309             *ptr = '\0';
310         }
311         if ((ptr = strchr(cmdline, '>')))
312         {
313             *ptr = '\0';
314             wndname = ptr + 1;
315         }
316         hlpfile = WINHELP_LookupHelpFile(cmdline);
317         if (!hlpfile) return 0;
318     }
319     else hlpfile = NULL;
320     WINHELP_OpenHelpWindow(HLPFILE_PageByHash, hlpfile, lHash,
321                            WINHELP_GetWindowInfo(hlpfile, wndname), show);
322
323     /* Message loop */
324     while (GetMessage(&msg, 0, 0, 0))
325     {
326         TranslateMessage(&msg);
327         DispatchMessage(&msg);
328     }
329     for (dll = Globals.dlls; dll; dll = dll->next)
330     {
331         if (dll->class & DC_INITTERM) dll->handler(DW_TERM, 0, 0);
332     }
333     return 0;
334 }
335
336 /***********************************************************************
337  *
338  *           RegisterWinClasses
339  */
340 static BOOL WINHELP_RegisterWinClasses(void)
341 {
342     WNDCLASS class_main, class_button_box, class_shadow, class_history;
343
344     class_main.style               = CS_HREDRAW | CS_VREDRAW;
345     class_main.lpfnWndProc         = WINHELP_MainWndProc;
346     class_main.cbClsExtra          = 0;
347     class_main.cbWndExtra          = sizeof(LONG);
348     class_main.hInstance           = Globals.hInstance;
349     class_main.hIcon               = LoadIcon(Globals.hInstance, MAKEINTRESOURCE(IDI_WINHELP));
350     class_main.hCursor             = LoadCursor(0, IDC_ARROW);
351     class_main.hbrBackground       = (HBRUSH)(COLOR_WINDOW+1);
352     class_main.lpszMenuName        = 0;
353     class_main.lpszClassName       = MAIN_WIN_CLASS_NAME;
354
355     class_button_box               = class_main;
356     class_button_box.lpfnWndProc   = WINHELP_ButtonBoxWndProc;
357     class_button_box.cbWndExtra    = 0;
358     class_button_box.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
359     class_button_box.lpszClassName = BUTTON_BOX_WIN_CLASS_NAME;
360
361     class_shadow                   = class_main;
362     class_shadow.lpfnWndProc       = WINHELP_ShadowWndProc;
363     class_shadow.cbWndExtra        = 0;
364     class_shadow.hbrBackground     = (HBRUSH)(COLOR_3DDKSHADOW+1);
365     class_shadow.lpszClassName     = SHADOW_WIN_CLASS_NAME;
366
367     class_history                  = class_main;
368     class_history.lpfnWndProc      = WINHELP_HistoryWndProc;
369     class_history.lpszClassName    = HISTORY_WIN_CLASS_NAME;
370
371     return (RegisterClass(&class_main) &&
372             RegisterClass(&class_button_box) &&
373             RegisterClass(&class_shadow) &&
374             RegisterClass(&class_history));
375 }
376
377 typedef struct
378 {
379     WORD size;
380     WORD command;
381     LONG data;
382     LONG reserved;
383     WORD ofsFilename;
384     WORD ofsData;
385 } WINHELP,*LPWINHELP;
386
387 static BOOL WINHELP_HasWorkingWindow(void)
388 {
389     if (!Globals.active_win) return FALSE;
390     if (Globals.active_win->next || Globals.win_list != Globals.active_win) return TRUE;
391     return Globals.active_win->page != NULL && Globals.active_win->page->file != NULL;
392 }
393
394 /******************************************************************
395  *              WINHELP_HandleCommand
396  *
397  *
398  */
399 static LRESULT  WINHELP_HandleCommand(HWND hSrcWnd, LPARAM lParam)
400 {
401     COPYDATASTRUCT*     cds = (COPYDATASTRUCT*)lParam;
402     WINHELP*            wh;
403
404     if (cds->dwData != 0xA1DE505)
405     {
406         WINE_FIXME("Wrong magic number (%08lx)\n", cds->dwData);
407         return 0;
408     }
409
410     wh = (WINHELP*)cds->lpData;
411
412     if (wh)
413     {
414         char*   ptr = (wh->ofsFilename) ? (LPSTR)wh + wh->ofsFilename : NULL;
415
416         WINE_TRACE("Got[%u]: cmd=%u data=%08x fn=%s\n",
417                    wh->size, wh->command, wh->data, ptr);
418         switch (wh->command)
419         {
420         case HELP_CONTEXT:
421             if (ptr)
422             {
423                 MACRO_JumpContext(ptr, "main", wh->data);
424             }
425             if (!WINHELP_HasWorkingWindow()) MACRO_Exit();
426             break;
427         case HELP_QUIT:
428             MACRO_Exit();
429             break;
430         case HELP_CONTENTS:
431             if (ptr)
432             {
433                 MACRO_JumpContents(ptr, "main");
434             }
435             if (!WINHELP_HasWorkingWindow()) MACRO_Exit();
436             break;
437         case HELP_HELPONHELP:
438             MACRO_HelpOn();
439             if (!WINHELP_HasWorkingWindow()) MACRO_Exit();
440             break;
441         /* case HELP_SETINDEX: */
442         case HELP_SETCONTENTS:
443             if (ptr)
444             {
445                 MACRO_SetContents(ptr, wh->data);
446             }
447             break;
448         case HELP_CONTEXTPOPUP:
449             if (ptr)
450             {
451                 MACRO_PopupContext(ptr, wh->data);
452             }
453             break;
454         /* case HELP_FORCEFILE:*/
455         /* case HELP_CONTEXTMENU: */
456         case HELP_FINDER:
457             /* in fact, should be the topic dialog box */
458             WINE_FIXME("HELP_FINDER: stub\n");
459             if (ptr)
460             {
461                 MACRO_JumpHash(ptr, "main", 0);
462             }
463             break;
464         /* case HELP_WM_HELP: */
465         /* case HELP_SETPOPUP_POS: */
466         /* case HELP_KEY: */
467         /* case HELP_COMMAND: */
468         /* case HELP_PARTIALKEY: */
469         /* case HELP_MULTIKEY: */
470         /* case HELP_SETWINPOS: */
471         default:
472             WINE_FIXME("Unhandled command (%x) for remote winhelp control\n", wh->command);
473             break;
474         }
475     }
476     /* Always return success for now */
477     return 1;
478 }
479
480 void            WINHELP_LayoutMainWindow(WINHELP_WINDOW* win)
481 {
482     RECT        rect, button_box_rect;
483     INT         text_top = 0;
484     HWND        hButtonBoxWnd = GetDlgItem(win->hMainWnd, CTL_ID_BUTTON);
485     HWND        hTextWnd = GetDlgItem(win->hMainWnd, CTL_ID_TEXT);
486
487     GetClientRect(win->hMainWnd, &rect);
488
489     /* Update button box and text Window */
490     SetWindowPos(hButtonBoxWnd, HWND_TOP,
491                  rect.left, rect.top,
492                  rect.right - rect.left,
493                  rect.bottom - rect.top, 0);
494
495     if (GetWindowRect(hButtonBoxWnd, &button_box_rect))
496         text_top = rect.top + button_box_rect.bottom - button_box_rect.top;
497
498     SetWindowPos(hTextWnd, HWND_TOP,
499                  rect.left, text_top,
500                  rect.right - rect.left,
501                  rect.bottom - text_top, 0);
502
503 }
504
505 static void     WINHELP_RememberPage(WINHELP_WINDOW* win, WINHELP_WNDPAGE* wpage)
506 {
507     unsigned        num;
508
509     if (!Globals.history.index || Globals.history.set[0].page != wpage->page)
510     {
511         num = sizeof(Globals.history.set) / sizeof(Globals.history.set[0]);
512         /* we're full, remove latest entry */
513         if (Globals.history.index == num)
514         {
515             HLPFILE_FreeHlpFile(Globals.history.set[num - 1].page->file);
516             Globals.history.index--;
517         }
518         memmove(&Globals.history.set[1], &Globals.history.set[0],
519                 Globals.history.index * sizeof(Globals.history.set[0]));
520         Globals.history.set[0] = *wpage;
521         Globals.history.index++;
522         wpage->page->file->wRefCount++;
523     }
524     if (win->hHistoryWnd) InvalidateRect(win->hHistoryWnd, NULL, TRUE);
525
526     num = sizeof(win->back.set) / sizeof(win->back.set[0]);
527     if (win->back.index == num)
528     {
529         /* we're full, remove latest entry */
530         HLPFILE_FreeHlpFile(win->back.set[0].page->file);
531         memmove(&win->back.set[0], &win->back.set[1],
532                 (num - 1) * sizeof(win->back.set[0]));
533         win->back.index--;
534     }
535     win->back.set[win->back.index++] = *wpage;
536     wpage->page->file->wRefCount++;
537 }
538
539 /***********************************************************************
540  *
541  *           WINHELP_CreateHelpWindow
542  */
543 BOOL WINHELP_CreateHelpWindow(WINHELP_WNDPAGE* wpage, int nCmdShow, BOOL remember)
544 {
545     WINHELP_WINDOW*     win = NULL;
546     BOOL                bPrimary, bPopup, bReUsed = FALSE;
547     LPSTR               name;
548     HICON               hIcon;
549     HWND                hTextWnd = NULL;
550
551     bPrimary = !lstrcmpi(wpage->wininfo->name, "main");
552     bPopup = !bPrimary && (wpage->wininfo->win_style & WS_POPUP);
553
554     if (!bPopup)
555     {
556         for (win = Globals.win_list; win; win = win->next)
557         {
558             if (!lstrcmpi(win->lpszName, wpage->wininfo->name))
559             {
560                 POINT   pt = {0, 0};
561                 SIZE    sz = {0, 0};
562                 DWORD   flags = SWP_NOSIZE | SWP_NOMOVE;
563
564                 WINHELP_DeleteButtons(win);
565                 bReUsed = TRUE;
566                 SetWindowText(win->hMainWnd, WINHELP_GetCaption(wpage));
567                 if (wpage->wininfo->origin.x != CW_USEDEFAULT &&
568                     wpage->wininfo->origin.y != CW_USEDEFAULT)
569                 {
570                     pt = wpage->wininfo->origin;
571                     flags &= ~SWP_NOSIZE;
572                 }
573                 if (wpage->wininfo->size.cx != CW_USEDEFAULT &&
574                     wpage->wininfo->size.cy != CW_USEDEFAULT)
575                 {
576                     sz = wpage->wininfo->size;
577                     flags &= ~SWP_NOMOVE;
578                 }
579                 SetWindowPos(win->hMainWnd, HWND_TOP, pt.x, pt.y, sz.cx, sz.cy, flags);
580
581                 if (wpage->page && win->page && wpage->page->file != win->page->file)
582                     WINHELP_DeleteBackSet(win);
583                 WINHELP_InitFonts(win->hMainWnd);
584
585                 win->page = wpage->page;
586                 win->info = wpage->wininfo;
587                 hTextWnd = GetDlgItem(win->hMainWnd, CTL_ID_TEXT);
588                 WINHELP_SetupText(hTextWnd, win, wpage->relative);
589
590                 InvalidateRect(win->hMainWnd, NULL, TRUE);
591                 if (win->hHistoryWnd) InvalidateRect(win->hHistoryWnd, NULL, TRUE);
592
593                 break;
594             }
595         }
596     }
597
598     if (!win)
599     {
600         /* Initialize WINHELP_WINDOW struct */
601         win = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
602                         sizeof(WINHELP_WINDOW) + strlen(wpage->wininfo->name) + 1);
603         if (!win) return FALSE;
604         win->next = Globals.win_list;
605         Globals.win_list = win;
606
607         name = (char*)win + sizeof(WINHELP_WINDOW);
608         lstrcpy(name, wpage->wininfo->name);
609         win->lpszName = name;
610         win->hHandCur = LoadCursorW(0, (LPWSTR)IDC_HAND);
611         win->back.index = 0;
612         win->font_scale = 1;
613     }
614     win->page = wpage->page;
615     win->info = wpage->wininfo;
616
617     if (!bPopup && wpage->page && remember)
618     {
619         WINHELP_RememberPage(win, wpage);
620     }
621
622     if (bPopup)
623         Globals.active_popup = win;
624     else
625         Globals.active_win = win;
626
627     /* Initialize default pushbuttons */
628     if (bPrimary && wpage->page)
629     {
630         CHAR    buffer[MAX_STRING_LEN];
631
632         LoadString(Globals.hInstance, STID_CONTENTS, buffer, sizeof(buffer));
633         MACRO_CreateButton("BTN_CONTENTS", buffer, "Contents()");
634         LoadString(Globals.hInstance, STID_INDEX, buffer, sizeof(buffer));
635         MACRO_CreateButton("BTN_INDEX", buffer, "Finder()");
636         LoadString(Globals.hInstance, STID_BACK, buffer, sizeof(buffer));
637         MACRO_CreateButton("BTN_BACK", buffer, "Back()");
638         if (win->back.index <= 1) MACRO_DisableButton("BTN_BACK");
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         hTextWnd = CreateWindow(RICHEDIT_CLASS, NULL,
657                                 ES_MULTILINE | ES_READONLY | WS_CHILD | WS_HSCROLL | WS_VSCROLL | WS_VISIBLE,
658                                 0, 0, 0, 0, win->hMainWnd, (HMENU)CTL_ID_TEXT, Globals.hInstance, NULL);
659         SendMessage(hTextWnd, EM_SETEVENTMASK, 0,
660                     SendMessage(hTextWnd, EM_GETEVENTMASK, 0, 0) | ENM_MOUSEEVENTS);
661     }
662
663     hIcon = (wpage->page) ? wpage->page->file->hIcon : NULL;
664     if (!hIcon) hIcon = LoadIcon(Globals.hInstance, MAKEINTRESOURCE(IDI_WINHELP));
665     SendMessage(win->hMainWnd, WM_SETICON, ICON_SMALL, (DWORD_PTR)hIcon);
666
667     /* Initialize file specific pushbuttons */
668     if (!(wpage->wininfo->win_style & WS_POPUP) && wpage->page)
669     {
670         HLPFILE_MACRO  *macro;
671         for (macro = wpage->page->file->first_macro; macro; macro = macro->next)
672             MACRO_ExecuteMacro(macro->lpszMacro);
673
674         for (macro = wpage->page->first_macro; macro; macro = macro->next)
675             MACRO_ExecuteMacro(macro->lpszMacro);
676     }
677
678     if (bPopup)
679     {
680         DWORD   mask = SendMessage(hTextWnd, EM_GETEVENTMASK, 0, 0);
681         RECT    rect;
682
683         win->font_scale = Globals.active_win->font_scale;
684         WINHELP_SetupText(hTextWnd, win, wpage->relative);
685
686         /* we need the window to be shown for richedit to compute the size */
687         ShowWindow(win->hMainWnd, nCmdShow);
688         SendMessage(hTextWnd, EM_SETEVENTMASK, 0, mask | ENM_REQUESTRESIZE);
689         SendMessage(hTextWnd, EM_REQUESTRESIZE, 0, 0);
690         SendMessage(hTextWnd, EM_SETEVENTMASK, 0, mask);
691
692         GetWindowRect(win->hMainWnd, &rect);
693         win->hShadowWnd = CreateWindowEx(WS_EX_TOOLWINDOW, SHADOW_WIN_CLASS_NAME,
694                                          "", WS_POPUP | WS_VISIBLE,
695                                          rect.left + SHADOW_DX, rect.top + SHADOW_DY,
696                                          rect.right - rect.left,
697                                          rect.bottom - rect.top,
698                                          Globals.active_win->hMainWnd, 0,
699                                          Globals.hInstance, NULL);
700         SetWindowPos(win->hMainWnd, win->hShadowWnd, 0, 0, 0, 0,
701                      SWP_NOSIZE | SWP_NOMOVE);
702     }
703     else
704     {
705         WINHELP_SetupText(hTextWnd, win, wpage->relative);
706         WINHELP_LayoutMainWindow(win);
707         ShowWindow(win->hMainWnd, nCmdShow);
708     }
709
710     return TRUE;
711 }
712
713 /******************************************************************
714  *             WINHELP_OpenHelpWindow
715  * Main function to search for a page and display it in a window
716  */
717 BOOL WINHELP_OpenHelpWindow(HLPFILE_PAGE* (*lookup)(HLPFILE*, LONG, ULONG*),
718                             HLPFILE* hlpfile, LONG val, HLPFILE_WINDOWINFO* wi,
719                             int nCmdShow)
720 {
721     WINHELP_WNDPAGE     wpage;
722
723     wpage.page = lookup(hlpfile, val, &wpage.relative);
724     if (wpage.page) wpage.page->file->wRefCount++;
725     wpage.wininfo = wi;
726     return WINHELP_CreateHelpWindow(&wpage, nCmdShow, TRUE);
727 }
728
729 /***********************************************************************
730  *
731  *           WINHELP_FindLink
732  */
733 static HLPFILE_LINK* WINHELP_FindLink(WINHELP_WINDOW* win, LPARAM pos)
734 {
735     HLPFILE_LINK*           link;
736     POINTL                  mouse_ptl, char_ptl, char_next_ptl;
737     DWORD                   cp;
738
739     if (!win->page) return NULL;
740
741     mouse_ptl.x = (short)LOWORD(pos);
742     mouse_ptl.y = (short)HIWORD(pos);
743     cp = SendMessageW(GetDlgItem(win->hMainWnd, CTL_ID_TEXT), EM_CHARFROMPOS,
744                       0, (LPARAM)&mouse_ptl);
745
746     for (link = win->page->first_link; link; link = link->next)
747     {
748         if (link->cpMin <= cp && cp <= link->cpMax)
749         {
750             /* check whether we're at end of line */
751             SendMessageW(GetDlgItem(win->hMainWnd, CTL_ID_TEXT), EM_POSFROMCHAR,
752                          (LPARAM)&char_ptl, cp);
753             SendMessageW(GetDlgItem(win->hMainWnd, CTL_ID_TEXT), EM_POSFROMCHAR,
754                          (LPARAM)&char_next_ptl, cp + 1);
755             if (char_next_ptl.y != char_ptl.y || mouse_ptl.x >= char_next_ptl.x)
756                 link = NULL;
757             break;
758         }
759     }
760     return link;
761 }
762
763 /******************************************************************
764  *             WINHELP_HandleTextMouse
765  *
766  */
767 static BOOL WINHELP_HandleTextMouse(WINHELP_WINDOW* win, UINT msg, LPARAM lParam)
768 {
769     HLPFILE*                hlpfile;
770     HLPFILE_LINK*           link;
771     BOOL                    ret = FALSE;
772
773     switch (msg)
774     {
775     case WM_MOUSEMOVE:
776         if (WINHELP_FindLink(win, lParam))
777             SetCursor(win->hHandCur);
778         else
779             SetCursor(LoadCursor(0, IDC_ARROW));
780         break;
781
782      case WM_LBUTTONDOWN:
783          if ((win->current_link = WINHELP_FindLink(win, lParam)))
784              ret = TRUE;
785          break;
786
787     case WM_LBUTTONUP:
788         if ((link = WINHELP_FindLink(win, lParam)) && link == win->current_link)
789         {
790             HLPFILE_WINDOWINFO*     wi;
791
792             switch (link->cookie)
793             {
794             case hlp_link_link:
795                 if ((hlpfile = WINHELP_LookupHelpFile(link->string)))
796                 {
797                     if (link->window == -1)
798                         wi = win->info;
799                     else if (link->window < hlpfile->numWindows)
800                         wi = &hlpfile->windows[link->window];
801                     else
802                     {
803                         WINE_WARN("link to window %d/%d\n", link->window, hlpfile->numWindows);
804                         break;
805                     }
806                     WINHELP_OpenHelpWindow(HLPFILE_PageByHash, hlpfile, link->hash, wi, SW_NORMAL);
807                 }
808                 break;
809             case hlp_link_popup:
810                 if ((hlpfile = WINHELP_LookupHelpFile(link->string)))
811                     WINHELP_OpenHelpWindow(HLPFILE_PageByHash, hlpfile, link->hash,
812                                            WINHELP_GetPopupWindowInfo(hlpfile, win, lParam),
813                                            SW_NORMAL);
814                 break;
815             case hlp_link_macro:
816                 MACRO_ExecuteMacro(link->string);
817                 break;
818             default:
819                 WINE_FIXME("Unknown link cookie %d\n", link->cookie);
820             }
821             ret = TRUE;
822         }
823         win->current_link = NULL;
824         break;
825     }
826     return ret;
827 }
828
829 /***********************************************************************
830  *
831  *           WINHELP_MainWndProc
832  */
833 static LRESULT CALLBACK WINHELP_MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
834 {
835     WINHELP_WINDOW *win;
836     WINHELP_BUTTON *button;
837     RECT rect;
838     INT  curPos, min, max, dy, keyDelta;
839     HWND hTextWnd;
840     LRESULT ret;
841
842     if (WINHELP_CheckPopup(hWnd, msg, wParam, lParam, &ret)) return ret;
843
844     switch (msg)
845     {
846     case WM_NCCREATE:
847         win = (WINHELP_WINDOW*) ((LPCREATESTRUCT) lParam)->lpCreateParams;
848         SetWindowLongPtr(hWnd, 0, (ULONG_PTR) win);
849         if (!win->page && Globals.isBook)
850             PostMessage(hWnd, WM_COMMAND, MNID_FILE_OPEN, 0);
851         win->hMainWnd = hWnd;
852         break;
853
854     case WM_WINDOWPOSCHANGED:
855         WINHELP_LayoutMainWindow((WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0));
856         break;
857
858     case WM_COMMAND:
859         win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
860         switch (wParam)
861         {
862             /* Menu FILE */
863         case MNID_FILE_OPEN:    MACRO_FileOpen();       break;
864         case MNID_FILE_PRINT:   MACRO_Print();          break;
865         case MNID_FILE_SETUP:   MACRO_PrinterSetup();   break;
866         case MNID_FILE_EXIT:    MACRO_Exit();           break;
867
868             /* Menu EDIT */
869         case MNID_EDIT_COPYDLG:
870             SendMessage(GetDlgItem(hWnd, CTL_ID_TEXT), WM_COPY, 0, 0);
871             break;
872         case MNID_EDIT_ANNOTATE:MACRO_Annotate();       break;
873
874             /* Menu Bookmark */
875         case MNID_BKMK_DEFINE:  MACRO_BookmarkDefine(); break;
876
877             /* Menu Help */
878         case MNID_HELP_HELPON:  MACRO_HelpOn();         break;
879         case MNID_HELP_HELPTOP: MACRO_HelpOnTop();      break;
880         case MNID_HELP_ABOUT:   MACRO_About();          break;
881         case MNID_HELP_WINE:    ShellAbout(hWnd, "WINE", "Help", 0); break;
882
883             /* Context help */
884         case MNID_CTXT_ANNOTATE:MACRO_Annotate();       break;
885         case MNID_CTXT_COPY:    MACRO_CopyDialog();     break;
886         case MNID_CTXT_PRINT:   MACRO_Print();          break;
887         case MNID_OPTS_HISTORY: MACRO_History();        break;
888         case MNID_OPTS_FONTS_SMALL:
889         case MNID_CTXT_FONTS_SMALL:
890             win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
891             if (win->font_scale != 0)
892             {
893                 win->font_scale = 0;
894                 WINHELP_SetupText(GetDlgItem(hWnd, CTL_ID_TEXT), win, 0 /* FIXME */);
895             }
896             break;
897         case MNID_OPTS_FONTS_NORMAL:
898         case MNID_CTXT_FONTS_NORMAL:
899             win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
900             if (win->font_scale != 1)
901             {
902                 win->font_scale = 1;
903                 WINHELP_SetupText(GetDlgItem(hWnd, CTL_ID_TEXT), win, 0 /* FIXME */);
904             }
905             break;
906         case MNID_OPTS_FONTS_LARGE:
907         case MNID_CTXT_FONTS_LARGE:
908             win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
909             if (win->font_scale != 2)
910             {
911                 win->font_scale = 2;
912                 WINHELP_SetupText(GetDlgItem(hWnd, CTL_ID_TEXT), win, 0 /* FIXME */);
913             }
914             break;
915         case MNID_OPTS_HELP_DEFAULT:
916         case MNID_OPTS_HELP_VISIBLE:
917         case MNID_OPTS_HELP_NONVISIBLE:
918         case MNID_OPTS_SYSTEM_COLORS:
919         case MNID_CTXT_HELP_DEFAULT:
920         case MNID_CTXT_HELP_VISIBLE:
921         case MNID_CTXT_HELP_NONVISIBLE:
922         case MNID_CTXT_SYSTEM_COLORS:
923             /* FIXME: NIY */
924
925         default:
926             /* Buttons */
927             for (button = win->first_button; button; button = button->next)
928                 if (wParam == button->wParam) break;
929             if (button)
930                 MACRO_ExecuteMacro(button->lpszMacro);
931             else if (!HIWORD(wParam))
932                 MessageBox(0, MAKEINTRESOURCE(STID_NOT_IMPLEMENTED),
933                            MAKEINTRESOURCE(STID_WHERROR), MB_OK);
934             break;
935         }
936         break;
937 /* EPP     case WM_DESTROY: */
938 /* EPP         if (Globals.hPopupWnd) DestroyWindow(Globals.hPopupWnd); */
939 /* EPP         break; */
940     case WM_COPYDATA:
941         return WINHELP_HandleCommand((HWND)wParam, lParam);
942
943     case WM_CHAR:
944         if (wParam == 3)
945         {
946             SendMessage(GetDlgItem(hWnd, CTL_ID_TEXT), WM_COPY, 0, 0);
947             return 0;
948         }
949         break;
950
951     case WM_KEYDOWN:
952         keyDelta = 0;
953
954         switch (wParam)
955         {
956         case VK_UP:
957         case VK_DOWN:
958             keyDelta = GetSystemMetrics(SM_CXVSCROLL);
959             if (wParam == VK_UP)
960                 keyDelta = -keyDelta;
961
962         case VK_PRIOR:
963         case VK_NEXT:
964             win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
965             hTextWnd = GetDlgItem(win->hMainWnd, CTL_ID_TEXT);
966             curPos = GetScrollPos(hTextWnd, SB_VERT);
967             GetScrollRange(hTextWnd, SB_VERT, &min, &max);
968
969             if (keyDelta == 0)
970             {            
971                 GetClientRect(hTextWnd, &rect);
972                 keyDelta = (rect.bottom - rect.top) / 2;
973                 if (wParam == VK_PRIOR)
974                     keyDelta = -keyDelta;
975             }
976
977             curPos += keyDelta;
978             if (curPos > max)
979                  curPos = max;
980             else if (curPos < min)
981                  curPos = min;
982
983             dy = GetScrollPos(hTextWnd, SB_VERT) - curPos;
984             SetScrollPos(hTextWnd, SB_VERT, curPos, TRUE);
985             ScrollWindow(hTextWnd, 0, dy, NULL, NULL);
986             UpdateWindow(hTextWnd);
987             return 0;
988
989         case VK_ESCAPE:
990             MACRO_Exit();
991             return 0;
992         }
993         break;
994
995     case WM_NOTIFY:
996         if (wParam == CTL_ID_TEXT)
997         {
998             RECT        rc;
999
1000             switch (((NMHDR*)lParam)->code)
1001             {
1002             case EN_MSGFILTER:
1003                 {
1004                     const MSGFILTER*    msgf = (const MSGFILTER*)lParam;
1005                     switch (msgf->msg)
1006                     {
1007                     case WM_KEYUP:
1008                         if (msgf->wParam == VK_ESCAPE) DestroyWindow(hWnd);
1009                         break;
1010                     case WM_RBUTTONDOWN:
1011                     {
1012                         HMENU       hMenu;
1013                         POINT       pt;
1014
1015                         win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1016                         hMenu = LoadMenu(Globals.hInstance, (LPSTR)CONTEXT_MENU);
1017                         switch (win->font_scale)
1018                         {
1019                         case 0:
1020                             CheckMenuItem(hMenu, MNID_CTXT_FONTS_SMALL,
1021                                           MF_BYCOMMAND|MF_CHECKED);
1022                             break;
1023                         default:
1024                             WINE_FIXME("Unsupported %d\n", win->font_scale);
1025                         case 1:
1026                             CheckMenuItem(hMenu, MNID_CTXT_FONTS_NORMAL,
1027                                           MF_BYCOMMAND|MF_CHECKED);
1028                             break;
1029                         case 2:
1030                             CheckMenuItem(hMenu, MNID_CTXT_FONTS_LARGE,
1031                                           MF_BYCOMMAND|MF_CHECKED);
1032                             break;
1033                         }
1034                         pt.x = (int)(short)LOWORD(msgf->lParam);
1035                         pt.y = (int)(short)HIWORD(msgf->lParam);
1036                         ClientToScreen(msgf->nmhdr.hwndFrom, &pt);
1037                         TrackPopupMenu(GetSubMenu(hMenu, 0), TPM_LEFTALIGN|TPM_TOPALIGN,
1038                                        pt.x, pt.y, 0, hWnd, NULL);
1039                         DestroyMenu(hMenu);
1040                     }
1041                     break;
1042                     default:
1043                         return WINHELP_HandleTextMouse((WINHELP_WINDOW*)GetWindowLongPtr(hWnd, 0),
1044                                                        msgf->msg, msgf->lParam);
1045                     }
1046                 }
1047                 break;
1048
1049             case EN_REQUESTRESIZE:
1050                 rc = ((REQRESIZE*)lParam)->rc;
1051                 win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1052                 AdjustWindowRect(&rc, GetWindowLong(win->hMainWnd, GWL_STYLE),
1053                                  FALSE);
1054                 SetWindowPos(win->hMainWnd, HWND_TOP, 0, 0,
1055                              rc.right - rc.left, rc.bottom - rc.top,
1056                              SWP_NOMOVE | SWP_NOZORDER);
1057                 WINHELP_LayoutMainWindow(win);
1058                 break;
1059             }
1060         }
1061         break;
1062
1063     case WM_INITMENUPOPUP:
1064         win = (WINHELP_WINDOW*) GetWindowLong(hWnd, 0);
1065         CheckMenuItem((HMENU)wParam, MNID_OPTS_FONTS_SMALL,
1066                       MF_BYCOMMAND | (win->font_scale == 0) ? MF_CHECKED : 0);
1067         CheckMenuItem((HMENU)wParam, MNID_OPTS_FONTS_NORMAL,
1068                       MF_BYCOMMAND | (win->font_scale == 1) ? MF_CHECKED : 0);
1069         CheckMenuItem((HMENU)wParam, MNID_OPTS_FONTS_LARGE,
1070                       MF_BYCOMMAND | (win->font_scale == 2) ? MF_CHECKED : 0);
1071         break;
1072
1073     case WM_NCDESTROY:
1074         {
1075             BOOL bExit;
1076             win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1077             bExit = (Globals.wVersion >= 4 && !lstrcmpi(win->lpszName, "main"));
1078             WINHELP_DeleteWindow(win);
1079
1080             if (bExit) MACRO_Exit();
1081             if (!Globals.win_list)
1082                 PostQuitMessage(0);
1083         }
1084         break;
1085     }
1086     return DefWindowProc(hWnd, msg, wParam, lParam);
1087 }
1088
1089 static DWORD CALLBACK WINHELP_RtfStreamIn(DWORD_PTR cookie, BYTE* buff,
1090                                           LONG cb, LONG* pcb)
1091 {
1092     struct RtfData*     rd = (struct RtfData*)cookie;
1093
1094     if (rd->where >= rd->ptr) return 1;
1095     if (rd->where + cb > rd->ptr)
1096         cb = rd->ptr - rd->where;
1097     memcpy(buff, rd->where, cb);
1098     rd->where += cb;
1099     *pcb = cb;
1100     return 0;
1101 }
1102
1103 static void WINHELP_SetupText(HWND hTextWnd, WINHELP_WINDOW* win, ULONG relative)
1104 {
1105     /* At first clear area - needed by EM_POSFROMCHAR/EM_SETSCROLLPOS */
1106     SendMessage(hTextWnd, WM_SETTEXT, 0, (LPARAM)"");
1107     SendMessage(hTextWnd, WM_SETREDRAW, FALSE, 0);
1108     SendMessage(hTextWnd, EM_SETBKGNDCOLOR, 0, (LPARAM)win->info->sr_color);
1109     /* set word-wrap to window size (undocumented) */
1110     SendMessage(hTextWnd, EM_SETTARGETDEVICE, 0, 0);
1111     if (win->page)
1112     {
1113         struct RtfData  rd;
1114         EDITSTREAM      es;
1115         unsigned        cp = 0;
1116         POINTL          ptl;
1117         POINT           pt;
1118
1119
1120         if (HLPFILE_BrowsePage(win->page, &rd, win->font_scale, relative))
1121         {
1122             rd.where = rd.data;
1123             es.dwCookie = (DWORD_PTR)&rd;
1124             es.dwError = 0;
1125             es.pfnCallback = WINHELP_RtfStreamIn;
1126
1127             SendMessageW(hTextWnd, EM_STREAMIN, SF_RTF, (LPARAM)&es);
1128             cp = rd.char_pos_rel;
1129         }
1130         /* FIXME: else leaking potentially the rd.first_link chain */
1131         HeapFree(GetProcessHeap(), 0, rd.data);
1132         SendMessage(hTextWnd, EM_POSFROMCHAR, (WPARAM)&ptl, cp ? cp - 1 : 0);
1133         pt.x = 0; pt.y = ptl.y;
1134         SendMessage(hTextWnd, EM_SETSCROLLPOS, 0, (LPARAM)&pt);
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                 {
1176                     if (Globals.button_proc == NULL)
1177                     {
1178                         NONCLIENTMETRICSW ncm;
1179                         Globals.button_proc = (WNDPROC) GetWindowLongPtr(button->hWnd, GWLP_WNDPROC);
1180
1181                         ncm.cbSize = sizeof(NONCLIENTMETRICSW);
1182                         SystemParametersInfoW(SPI_GETNONCLIENTMETRICS,
1183                                               sizeof(NONCLIENTMETRICSW), &ncm, 0);
1184                         Globals.hButtonFont = CreateFontIndirectW(&ncm.lfMenuFont);
1185                     }
1186                     SetWindowLongPtr(button->hWnd, GWLP_WNDPROC, (LONG_PTR) WINHELP_ButtonWndProc);
1187                     if (Globals.hButtonFont)
1188                         SendMessage(button->hWnd, WM_SETFONT, (WPARAM)Globals.hButtonFont, TRUE);
1189                 }
1190             }
1191             hDc = GetDC(button->hWnd);
1192             GetTextExtentPoint(hDc, button->lpszName,
1193                                lstrlen(button->lpszName), &textsize);
1194             ReleaseDC(button->hWnd, hDc);
1195
1196             button_size.cx = max(button_size.cx, textsize.cx + BUTTON_CX);
1197             button_size.cy = max(button_size.cy, textsize.cy + BUTTON_CY);
1198         }
1199
1200         x = 0;
1201         y = 0;
1202         for (button = win->first_button; button; button = button->next)
1203         {
1204             SetWindowPos(button->hWnd, HWND_TOP, x, y, button_size.cx, button_size.cy, 0);
1205
1206             if (x + 2 * button_size.cx <= winpos->cx)
1207                 x += button_size.cx;
1208             else
1209                 x = 0, y += button_size.cy;
1210         }
1211         winpos->cy = y + (x ? button_size.cy : 0);
1212         break;
1213
1214     case WM_COMMAND:
1215         SendMessage(GetParent(hWnd), msg, wParam, lParam);
1216         break;
1217
1218     case WM_KEYDOWN:
1219         switch (wParam)
1220         {
1221         case VK_UP:
1222         case VK_DOWN:
1223         case VK_PRIOR:
1224         case VK_NEXT:
1225         case VK_ESCAPE:
1226             return SendMessage(GetParent(hWnd), msg, wParam, lParam);
1227         }
1228         break;
1229     }
1230
1231     return DefWindowProc(hWnd, msg, wParam, lParam);
1232 }
1233
1234 /***********************************************************************
1235  *
1236  *           WINHELP_ButtonWndProc
1237  */
1238 static LRESULT CALLBACK WINHELP_ButtonWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1239 {
1240     if (WINHELP_CheckPopup(hWnd, msg, wParam, lParam, NULL)) return 0;
1241
1242     if (msg == WM_KEYDOWN)
1243     {
1244         switch (wParam)
1245         {
1246         case VK_UP:
1247         case VK_DOWN:
1248         case VK_PRIOR:
1249         case VK_NEXT:
1250         case VK_ESCAPE:
1251             return SendMessage(GetParent(hWnd), msg, wParam, lParam);
1252         }
1253     }
1254
1255     return CallWindowProc(Globals.button_proc, hWnd, msg, wParam, lParam);
1256 }
1257
1258 /******************************************************************
1259  *              WINHELP_HistoryWndProc
1260  *
1261  *
1262  */
1263 static LRESULT CALLBACK WINHELP_HistoryWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1264 {
1265     WINHELP_WINDOW*     win;
1266     PAINTSTRUCT         ps;
1267     HDC                 hDc;
1268     TEXTMETRIC          tm;
1269     unsigned int        i;
1270     RECT                r;
1271
1272     switch (msg)
1273     {
1274     case WM_NCCREATE:
1275         win = (WINHELP_WINDOW*)((LPCREATESTRUCT)lParam)->lpCreateParams;
1276         SetWindowLongPtr(hWnd, 0, (ULONG_PTR)win);
1277         win->hHistoryWnd = hWnd;
1278         break;
1279     case WM_CREATE:
1280         win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1281         hDc = GetDC(hWnd);
1282         GetTextMetrics(hDc, &tm);
1283         GetWindowRect(hWnd, &r);
1284
1285         r.right = r.left + 30 * tm.tmAveCharWidth;
1286         r.bottom = r.top + (sizeof(Globals.history.set) / sizeof(Globals.history.set[0])) * tm.tmHeight;
1287         AdjustWindowRect(&r, GetWindowLong(hWnd, GWL_STYLE), FALSE);
1288         if (r.left < 0) {r.right -= r.left; r.left = 0;}
1289         if (r.top < 0) {r.bottom -= r.top; r.top = 0;}
1290
1291         MoveWindow(hWnd, r.left, r.top, r.right, r.bottom, TRUE);
1292         ReleaseDC(hWnd, hDc);
1293         break;
1294     case WM_LBUTTONDOWN:
1295         win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1296         hDc = GetDC(hWnd);
1297         GetTextMetrics(hDc, &tm);
1298         i = HIWORD(lParam) / tm.tmHeight;
1299         if (i < Globals.history.index)
1300             WINHELP_CreateHelpWindow(&Globals.history.set[i], SW_SHOW, TRUE);
1301         ReleaseDC(hWnd, hDc);
1302         break;
1303     case WM_PAINT:
1304         hDc = BeginPaint(hWnd, &ps);
1305         win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1306         GetTextMetrics(hDc, &tm);
1307
1308         for (i = 0; i < Globals.history.index; i++)
1309         {
1310             if (Globals.history.set[i].page->file == Globals.active_win->page->file)
1311             {
1312                 TextOut(hDc, 0, i * tm.tmHeight,
1313                         Globals.history.set[i].page->lpszTitle,
1314                         strlen(Globals.history.set[i].page->lpszTitle));
1315             }
1316             else
1317             {
1318                 char        buffer[1024];
1319                 const char* ptr1;
1320                 const char* ptr2;
1321                 unsigned    len;
1322
1323                 ptr1 = strrchr(Globals.history.set[i].page->file->lpszPath, '\\');
1324                 if (!ptr1) ptr1 = Globals.history.set[i].page->file->lpszPath;
1325                 else ptr1++;
1326                 ptr2 = strrchr(ptr1, '.');
1327                 len = ptr2 ? ptr2 - ptr1 : strlen(ptr1);
1328                 if (len > sizeof(buffer)) len = sizeof(buffer);
1329                 memcpy(buffer, ptr1, len);
1330                 if (len < sizeof(buffer)) buffer[len++] = ':';
1331                 strncpy(&buffer[len], Globals.history.set[i].page->lpszTitle, sizeof(buffer) - len);
1332                 buffer[sizeof(buffer) - 1] = '\0';
1333                 TextOut(hDc, 0, i * tm.tmHeight, buffer, strlen(buffer));
1334             }
1335         }
1336         EndPaint(hWnd, &ps);
1337         break;
1338     case WM_DESTROY:
1339         win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1340         if (hWnd == win->hHistoryWnd)
1341             win->hHistoryWnd = 0;
1342         break;
1343     }
1344     return DefWindowProc(hWnd, msg, wParam, lParam);
1345 }
1346
1347 /***********************************************************************
1348  *
1349  *           WINHELP_ShadowWndProc
1350  */
1351 static LRESULT CALLBACK WINHELP_ShadowWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1352 {
1353     if (WINHELP_CheckPopup(hWnd, msg, wParam, lParam, NULL)) return 0;
1354     return WINHELP_CheckPopup(hWnd, msg, wParam, lParam, NULL) ? 0L : DefWindowProc(hWnd, msg, wParam, lParam);
1355 }
1356
1357 /***********************************************************************
1358  *
1359  *           WINHELP_CheckPopup
1360  */
1361 static BOOL WINHELP_CheckPopup(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, LRESULT* lret)
1362 {
1363     HWND        hPopup;
1364
1365     if (!Globals.active_popup) return FALSE;
1366
1367     switch (msg)
1368     {
1369     case WM_NOTIFY:
1370         {
1371             MSGFILTER*  msgf = (MSGFILTER*)lParam;
1372             if (msgf->nmhdr.code == EN_MSGFILTER)
1373             {
1374                 if (!WINHELP_CheckPopup(hWnd, msgf->msg, msgf->wParam, msgf->lParam, NULL))
1375                     return FALSE;
1376                 if (lret) *lret = 1;
1377                 return TRUE;
1378             }
1379         }
1380         break;
1381     case WM_ACTIVATE:
1382         if (wParam != WA_INACTIVE || (HWND)lParam == Globals.active_win->hMainWnd ||
1383             (HWND)lParam == Globals.active_popup->hMainWnd ||
1384             GetWindow((HWND)lParam, GW_OWNER) == Globals.active_win->hMainWnd)
1385             break;
1386     case WM_LBUTTONUP:
1387     case WM_LBUTTONDOWN:
1388         if (WINHELP_HandleTextMouse(Globals.active_popup, msg, lParam) && msg == WM_LBUTTONDOWN)
1389             return FALSE;
1390         /* fall through */
1391     case WM_MBUTTONDOWN:
1392     case WM_RBUTTONDOWN:
1393     case WM_NCLBUTTONDOWN:
1394     case WM_NCMBUTTONDOWN:
1395     case WM_NCRBUTTONDOWN:
1396         hPopup = Globals.active_popup->hMainWnd;
1397         Globals.active_popup = NULL;
1398         DestroyWindow(hPopup);
1399         return TRUE;
1400     }
1401     return FALSE;
1402 }
1403
1404 /******************************************************************
1405  *              WINHELP_DeleteButtons
1406  *
1407  */
1408 static void WINHELP_DeleteButtons(WINHELP_WINDOW* win)
1409 {
1410     WINHELP_BUTTON*     b;
1411     WINHELP_BUTTON*     bp;
1412
1413     for (b = win->first_button; b; b = bp)
1414     {
1415         DestroyWindow(b->hWnd);
1416         bp = b->next;
1417         HeapFree(GetProcessHeap(), 0, b);
1418     }
1419     win->first_button = NULL;
1420 }
1421
1422 /******************************************************************
1423  *              WINHELP_DeleteBackSet
1424  *
1425  */
1426 void WINHELP_DeleteBackSet(WINHELP_WINDOW* win)
1427 {
1428     unsigned int i;
1429
1430     for (i = 0; i < win->back.index; i++)
1431     {
1432         HLPFILE_FreeHlpFile(win->back.set[i].page->file);
1433         win->back.set[i].page = NULL;
1434     }
1435     win->back.index = 0;
1436 }
1437
1438 /******************************************************************
1439  *             WINHELP_DeletePageLinks
1440  *
1441  */
1442 static void WINHELP_DeletePageLinks(HLPFILE_PAGE* page)
1443 {
1444     HLPFILE_LINK*       curr;
1445     HLPFILE_LINK*       next;
1446
1447     for (curr = page->first_link; curr; curr = next)
1448     {
1449         next = curr->next;
1450         HeapFree(GetProcessHeap(), 0, curr);
1451     }
1452 }
1453
1454 /***********************************************************************
1455  *
1456  *           WINHELP_DeleteWindow
1457  */
1458 static void WINHELP_DeleteWindow(WINHELP_WINDOW* win)
1459 {
1460     WINHELP_WINDOW**    w;
1461
1462     for (w = &Globals.win_list; *w; w = &(*w)->next)
1463     {
1464         if (*w == win)
1465         {
1466             *w = win->next;
1467             break;
1468         }
1469     }
1470
1471     if (Globals.active_win == win)
1472     {
1473         Globals.active_win = Globals.win_list;
1474         if (Globals.win_list)
1475             SetActiveWindow(Globals.win_list->hMainWnd);
1476     }
1477
1478     if (win == Globals.active_popup)
1479         Globals.active_popup = NULL;
1480
1481     WINHELP_DeleteButtons(win);
1482
1483     if (win->page) WINHELP_DeletePageLinks(win->page);
1484     if (win->hShadowWnd) DestroyWindow(win->hShadowWnd);
1485     if (win->hHistoryWnd) DestroyWindow(win->hHistoryWnd);
1486
1487     DeleteObject(win->hBrush);
1488
1489     WINHELP_DeleteBackSet(win);
1490
1491     if (win->page) HLPFILE_FreeHlpFile(win->page->file);
1492     HeapFree(GetProcessHeap(), 0, win);
1493 }
1494
1495 /***********************************************************************
1496  *
1497  *           WINHELP_InitFonts
1498  */
1499 static void WINHELP_InitFonts(HWND hWnd)
1500 {
1501     WINHELP_WINDOW *win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1502     LOGFONT logfontlist[] = {
1503         {-10, 0, 0, 0, 400, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 32, "Helv"},
1504         {-12, 0, 0, 0, 700, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 32, "Helv"},
1505         {-12, 0, 0, 0, 700, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 32, "Helv"},
1506         {-12, 0, 0, 0, 400, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 32, "Helv"},
1507         {-12, 0, 0, 0, 700, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 32, "Helv"},
1508         {-10, 0, 0, 0, 700, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 32, "Helv"},
1509         { -8, 0, 0, 0, 400, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 32, "Helv"}};
1510 #define FONTS_LEN (sizeof(logfontlist)/sizeof(*logfontlist))
1511
1512     static HFONT fonts[FONTS_LEN];
1513     static BOOL init = 0;
1514
1515     win->fonts_len = FONTS_LEN;
1516     win->fonts = fonts;
1517
1518     if (!init)
1519     {
1520         UINT i;
1521
1522         for (i = 0; i < FONTS_LEN; i++)
1523         {
1524             fonts[i] = CreateFontIndirect(&logfontlist[i]);
1525         }
1526
1527         init = 1;
1528     }
1529 }
1530
1531 /***********************************************************************
1532  *
1533  *           WINHELP_MessageBoxIDS_s
1534  */
1535 INT WINHELP_MessageBoxIDS_s(UINT ids_text, LPCSTR str, UINT ids_title, WORD type)
1536 {
1537     CHAR text[MAX_STRING_LEN];
1538     CHAR newtext[MAX_STRING_LEN + MAX_PATH];
1539
1540     LoadString(Globals.hInstance, ids_text, text, sizeof(text));
1541     wsprintf(newtext, text, str);
1542
1543     return MessageBox(0, newtext, MAKEINTRESOURCE(ids_title), type);
1544 }
1545
1546 /**************************************************************************
1547  * cb_KWBTree
1548  *
1549  * HLPFILE_BPTreeCallback enumeration function for '|KWBTREE' internal file.
1550  *
1551  */
1552 static void cb_KWBTree(void *p, void **next, void *cookie)
1553 {
1554     HWND hListWnd = (HWND)cookie;
1555     int count;
1556
1557     WINE_TRACE("Adding '%s' to search list\n", (char *)p);
1558     SendMessage(hListWnd, LB_INSERTSTRING, -1, (LPARAM)p);
1559     count = SendMessage(hListWnd, LB_GETCOUNT, 0, 0);
1560     SendMessage(hListWnd, LB_SETITEMDATA, count-1, (LPARAM)p);
1561     *next = (char*)p + strlen((char*)p) + 7;
1562 }
1563
1564 struct index_data
1565 {
1566     HLPFILE*    hlpfile;
1567     BOOL        jump;
1568     ULONG       offset;
1569 };
1570
1571 /**************************************************************************
1572  * WINHELP_IndexDlgProc
1573  *
1574  */
1575 INT_PTR CALLBACK WINHELP_IndexDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1576 {
1577     static struct index_data* id;
1578     int sel;
1579
1580     switch (msg)
1581     {
1582     case WM_INITDIALOG:
1583         id = (struct index_data*)((PROPSHEETPAGE*)lParam)->lParam;
1584         HLPFILE_BPTreeEnum(id->hlpfile->kwbtree, cb_KWBTree,
1585                            GetDlgItem(hWnd, IDC_INDEXLIST));
1586         id->jump = FALSE;
1587         id->offset = 1;
1588         return TRUE;
1589     case WM_COMMAND:
1590         switch (HIWORD(wParam))
1591         {
1592         case LBN_DBLCLK:
1593             if (LOWORD(wParam) == IDC_INDEXLIST)
1594                 SendMessage(GetParent(hWnd), PSM_PRESSBUTTON, PSBTN_OK, 0);
1595             break;
1596         }
1597         break;
1598     case WM_NOTIFY:
1599         switch (((NMHDR*)lParam)->code)
1600         {
1601         case PSN_APPLY:
1602             sel = SendDlgItemMessage(hWnd, IDC_INDEXLIST, LB_GETCURSEL, 0, 0);
1603             if (sel != LB_ERR)
1604             {
1605                 BYTE *p;
1606                 int count;
1607
1608                 p = (BYTE*)SendDlgItemMessage(hWnd, IDC_INDEXLIST,
1609                                               LB_GETITEMDATA, sel, 0);
1610                 count = *(short*)((char *)p + strlen((char *)p) + 1);
1611                 if (count > 1)
1612                 {
1613                     MessageBox(hWnd, "count > 1 not supported yet", "Error", MB_OK | MB_ICONSTOP);
1614                     SetWindowLongPtr(hWnd, DWLP_MSGRESULT, PSNRET_INVALID);
1615                     return TRUE;
1616                 }
1617                 id->offset = *(ULONG*)((char *)p + strlen((char *)p) + 3);
1618                 id->offset = *(long*)(id->hlpfile->kwdata + id->offset + 9);
1619                 if (id->offset == 0xFFFFFFFF)
1620                 {
1621                     MessageBox(hWnd, "macro keywords not supported yet", "Error", MB_OK | MB_ICONSTOP);
1622                     SetWindowLongPtr(hWnd, DWLP_MSGRESULT, PSNRET_INVALID);
1623                     return TRUE;
1624                 }
1625                 id->jump = TRUE;
1626                 SetWindowLongPtr(hWnd, DWLP_MSGRESULT, PSNRET_NOERROR);
1627             }
1628             return TRUE;
1629         default:
1630             return FALSE;
1631         }
1632         break;
1633     default:
1634         break;
1635     }
1636     return FALSE;
1637 }
1638
1639 /**************************************************************************
1640  * WINHELP_SearchDlgProc
1641  *
1642  */
1643 INT_PTR CALLBACK WINHELP_SearchDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1644 {
1645     static struct index_data* id;
1646
1647     switch (msg)
1648     {
1649     case WM_INITDIALOG:
1650         id = (struct index_data*)((PROPSHEETPAGE*)lParam)->lParam;
1651         return TRUE;
1652     case WM_NOTIFY:
1653         switch (((NMHDR*)lParam)->code)
1654         {
1655         case PSN_APPLY:
1656             SetWindowLongPtr(hWnd, DWLP_MSGRESULT, PSNRET_NOERROR);
1657             return TRUE;
1658         default:
1659             return FALSE;
1660         }
1661         break;
1662     default:
1663         break;
1664     }
1665     return FALSE;
1666 }
1667
1668 /**************************************************************************
1669  * WINHELP_CreateIndexWindow
1670  *
1671  * Displays a dialog with keywords of current help file.
1672  *
1673  */
1674 BOOL WINHELP_CreateIndexWindow(BOOL is_search)
1675 {
1676     HPROPSHEETPAGE      psPage[3];
1677     PROPSHEETPAGE       psp;
1678     PROPSHEETHEADER     psHead;
1679     struct index_data   id;
1680     char                buf[256];
1681
1682     if (Globals.active_win && Globals.active_win->page && Globals.active_win->page->file)
1683         id.hlpfile = Globals.active_win->page->file;
1684     else
1685         return FALSE;
1686
1687     if (id.hlpfile->kwbtree == NULL)
1688     {
1689         WINE_TRACE("No index provided\n");
1690         return FALSE;
1691     }
1692
1693     InitCommonControls();
1694
1695     id.jump = FALSE;
1696     memset(&psp, 0, sizeof(psp));
1697     psp.dwSize = sizeof(psp);
1698     psp.dwFlags = 0;
1699     psp.hInstance = Globals.hInstance;
1700
1701     psp.u.pszTemplate = MAKEINTRESOURCE(IDD_INDEX);
1702     psp.lParam = (LPARAM)&id;
1703     psp.pfnDlgProc = WINHELP_IndexDlgProc;
1704     psPage[0] = CreatePropertySheetPage(&psp);
1705
1706     psp.u.pszTemplate = MAKEINTRESOURCE(IDD_SEARCH);
1707     psp.lParam = (LPARAM)&id;
1708     psp.pfnDlgProc = WINHELP_SearchDlgProc;
1709     psPage[1] = CreatePropertySheetPage(&psp);
1710
1711     memset(&psHead, 0, sizeof(psHead));
1712     psHead.dwSize = sizeof(psHead);
1713
1714     LoadString(Globals.hInstance, STID_PSH_INDEX, buf, sizeof(buf));
1715     strcat(buf, Globals.active_win->info->caption);
1716
1717     psHead.pszCaption = buf;
1718     psHead.nPages = 2;
1719     psHead.u2.nStartPage = is_search ? 1 : 0;
1720     psHead.hwndParent = Globals.active_win->hMainWnd;
1721     psHead.u3.phpage = psPage;
1722     psHead.dwFlags = PSH_NOAPPLYNOW;
1723
1724     PropertySheet(&psHead);
1725     if (id.jump)
1726     {
1727         WINE_TRACE("got %d as an offset\n", id.offset);
1728         WINHELP_OpenHelpWindow(HLPFILE_PageByOffset, id.hlpfile, id.offset,
1729                                Globals.active_win->info, SW_NORMAL);
1730     }
1731     return TRUE;
1732 }