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