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