dmsynth: Fix synth download of articulations list.
[wine.git] / programs / winhlp32 / winhelp.c
blobd648cab7982413967d704181e18011a76a4848a8
1 /*
2 * Help Viewer
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>
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.
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.
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
25 #include <assert.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #include <stdlib.h>
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35 #include "commdlg.h"
36 #include "winhelp.h"
37 #include "winhelp_res.h"
38 #include "shellapi.h"
39 #include "richedit.h"
40 #include "commctrl.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(winhelp);
46 WINHELP_GLOBALS Globals = {3, NULL, TRUE, NULL, NULL, NULL, NULL, NULL, {{{NULL,NULL}},0}, NULL};
48 #define CTL_ID_BUTTON 0x700
49 #define CTL_ID_TEXT 0x701
52 /***********************************************************************
54 * WINHELP_InitFonts
56 static void WINHELP_InitFonts(HWND hWnd)
58 WINHELP_WINDOW *win = (WINHELP_WINDOW*) GetWindowLongPtrW(hWnd, 0);
59 LOGFONTW logfontlist[] = {
60 {-10, 0, 0, 0, 400, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 32, {'H','e','l','v',0}},
61 {-12, 0, 0, 0, 700, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 32, {'H','e','l','v',0}},
62 {-12, 0, 0, 0, 700, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 32, {'H','e','l','v',0}},
63 {-12, 0, 0, 0, 400, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 32, {'H','e','l','v',0}},
64 {-12, 0, 0, 0, 700, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 32, {'H','e','l','v',0}},
65 {-10, 0, 0, 0, 700, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 32, {'H','e','l','v',0}},
66 { -8, 0, 0, 0, 400, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 32, {'H','e','l','v',0}}};
68 static HFONT fonts[ARRAY_SIZE(logfontlist)];
69 static BOOL init = FALSE;
71 win->fonts_len = ARRAY_SIZE(logfontlist);
72 win->fonts = fonts;
74 if (!init)
76 UINT i;
78 for (i = 0; i < ARRAY_SIZE(logfontlist); i++)
80 fonts[i] = CreateFontIndirectW(&logfontlist[i]);
83 init = TRUE;
87 static DWORD CALLBACK WINHELP_RtfStreamIn(DWORD_PTR cookie, BYTE* buff,
88 LONG cb, LONG* pcb)
90 struct RtfData* rd = (struct RtfData*)cookie;
92 if (rd->where >= rd->ptr) return 1;
93 if (rd->where + cb > rd->ptr)
94 cb = rd->ptr - rd->where;
95 memcpy(buff, rd->where, cb);
96 rd->where += cb;
97 *pcb = cb;
98 return 0;
101 static void WINHELP_SetupText(HWND hTextWnd, WINHELP_WINDOW* win, ULONG relative)
103 static const WCHAR emptyW[1];
104 /* At first clear area - needed by EM_POSFROMCHAR/EM_SETSCROLLPOS */
105 SendMessageW(hTextWnd, WM_SETTEXT, 0, (LPARAM)emptyW);
106 SendMessageW(hTextWnd, WM_SETREDRAW, FALSE, 0);
107 SendMessageW(hTextWnd, EM_SETBKGNDCOLOR, 0, (LPARAM)win->info->sr_color);
108 /* set word-wrap to window size (undocumented) */
109 SendMessageW(hTextWnd, EM_SETTARGETDEVICE, 0, 0);
110 if (win->page)
112 struct RtfData rd;
113 EDITSTREAM es;
114 unsigned cp = 0;
115 POINTL ptl;
116 POINT pt;
119 if (HLPFILE_BrowsePage(win->page, &rd, win->font_scale, relative))
121 rd.where = rd.data;
122 es.dwCookie = (DWORD_PTR)&rd;
123 es.dwError = 0;
124 es.pfnCallback = WINHELP_RtfStreamIn;
126 SendMessageW(hTextWnd, EM_STREAMIN, SF_RTF, (LPARAM)&es);
127 cp = rd.char_pos_rel;
129 /* FIXME: else leaking potentially the rd.first_link chain */
130 free(rd.data);
131 SendMessageW(hTextWnd, EM_POSFROMCHAR, (WPARAM)&ptl, cp ? cp - 1 : 0);
132 pt.x = 0; pt.y = ptl.y;
133 SendMessageW(hTextWnd, EM_SETSCROLLPOS, 0, (LPARAM)&pt);
135 SendMessageW(hTextWnd, WM_SETREDRAW, TRUE, 0);
136 RedrawWindow(hTextWnd, NULL, NULL, RDW_FRAME|RDW_INVALIDATE);
139 /***********************************************************************
141 * WINHELP_GetOpenFileName
143 BOOL WINHELP_GetOpenFileName(LPSTR lpszFile, int len)
145 OPENFILENAMEA openfilename;
146 CHAR szDir[MAX_PATH];
147 CHAR szzFilter[2 * MAX_STRING_LEN + 100];
148 LPSTR p = szzFilter;
150 WINE_TRACE("()\n");
152 LoadStringA(Globals.hInstance, STID_HELP_FILES_HLP, p, MAX_STRING_LEN);
153 p += strlen(p) + 1;
154 strcpy(p, "*.hlp");
155 p += strlen(p) + 1;
156 LoadStringA(Globals.hInstance, STID_ALL_FILES, p, MAX_STRING_LEN);
157 p += strlen(p) + 1;
158 strcpy(p, "*.*");
159 p += strlen(p) + 1;
160 *p = '\0';
162 GetCurrentDirectoryA(sizeof(szDir), szDir);
164 lpszFile[0]='\0';
166 openfilename.lStructSize = sizeof(openfilename);
167 openfilename.hwndOwner = (Globals.active_win ? Globals.active_win->hMainWnd : 0);
168 openfilename.hInstance = Globals.hInstance;
169 openfilename.lpstrFilter = szzFilter;
170 openfilename.lpstrCustomFilter = 0;
171 openfilename.nMaxCustFilter = 0;
172 openfilename.nFilterIndex = 1;
173 openfilename.lpstrFile = lpszFile;
174 openfilename.nMaxFile = len;
175 openfilename.lpstrFileTitle = 0;
176 openfilename.nMaxFileTitle = 0;
177 openfilename.lpstrInitialDir = szDir;
178 openfilename.lpstrTitle = 0;
179 openfilename.Flags = OFN_ENABLESIZING | OFN_HIDEREADONLY | OFN_READONLY;
180 openfilename.nFileOffset = 0;
181 openfilename.nFileExtension = 0;
182 openfilename.lpstrDefExt = 0;
183 openfilename.lCustData = 0;
184 openfilename.lpfnHook = 0;
185 openfilename.lpTemplateName = 0;
187 return GetOpenFileNameA(&openfilename);
190 /***********************************************************************
192 * WINHELP_MessageBoxIDS_s
194 static INT WINHELP_MessageBoxIDS_s(UINT ids_text, LPCSTR str, UINT ids_title, WORD type)
196 CHAR text[MAX_STRING_LEN];
197 CHAR newtext[MAX_STRING_LEN + MAX_PATH];
199 LoadStringA(Globals.hInstance, ids_text, text, sizeof(text));
200 wsprintfA(newtext, text, str);
202 return MessageBoxA(0, newtext, MAKEINTRESOURCEA(ids_title), type);
205 /***********************************************************************
207 * WINHELP_LookupHelpFile
209 HLPFILE* WINHELP_LookupHelpFile(LPCSTR lpszFile)
211 HLPFILE* hlpfile;
212 char szFullName[MAX_PATH];
213 char szAddPath[MAX_PATH];
214 char *p;
217 * NOTE: This is needed by popup windows only.
218 * In other cases it's not needed but does not hurt though.
220 if (Globals.active_win && Globals.active_win->page && Globals.active_win->page->file)
222 strcpy(szAddPath, Globals.active_win->page->file->lpszPath);
223 p = strrchr(szAddPath, '\\');
224 if (p) *p = 0;
228 * FIXME: Should we swap conditions?
230 if (!SearchPathA(NULL, lpszFile, ".hlp", MAX_PATH, szFullName, NULL) &&
231 !SearchPathA(szAddPath, lpszFile, ".hlp", MAX_PATH, szFullName, NULL))
233 if (WINHELP_MessageBoxIDS_s(STID_FILE_NOT_FOUND_s, lpszFile, STID_WHERROR,
234 MB_YESNO|MB_ICONQUESTION) != IDYES)
235 return NULL;
236 if (!WINHELP_GetOpenFileName(szFullName, MAX_PATH))
237 return NULL;
239 hlpfile = HLPFILE_ReadHlpFile(szFullName);
240 if (!hlpfile)
241 WINHELP_MessageBoxIDS_s(STID_HLPFILE_ERROR_s, lpszFile,
242 STID_WHERROR, MB_OK|MB_ICONSTOP);
243 return hlpfile;
246 /******************************************************************
247 * WINHELP_GetWindowInfo
251 HLPFILE_WINDOWINFO* WINHELP_GetWindowInfo(HLPFILE* hlpfile, LPCSTR name)
253 static HLPFILE_WINDOWINFO mwi;
254 unsigned int i;
256 if (!name || !name[0])
257 name = Globals.active_win->info->name;
259 if (hlpfile)
260 for (i = 0; i < hlpfile->numWindows; i++)
261 if (!lstrcmpiA(hlpfile->windows[i].name, name))
262 return &hlpfile->windows[i];
264 if (strcmp(name, "main") != 0)
266 WINE_FIXME("Couldn't find window info for %s\n", debugstr_a(name));
267 assert(0);
268 return NULL;
270 if (!mwi.name[0])
272 strcpy(mwi.type, "primary");
273 strcpy(mwi.name, "main");
274 if (hlpfile && hlpfile->lpszTitle[0])
276 char tmp[40];
277 LoadStringA(Globals.hInstance, STID_WINE_HELP, tmp, sizeof(tmp));
278 _snprintf(mwi.caption, sizeof(mwi.caption), "%s %s - %s",
279 hlpfile->lpszTitle, tmp, hlpfile->lpszPath);
281 else
282 LoadStringA(Globals.hInstance, STID_WINE_HELP, mwi.caption, sizeof(mwi.caption));
283 mwi.origin.x = mwi.origin.y = mwi.size.cx = mwi.size.cy = CW_USEDEFAULT;
284 mwi.style = SW_SHOW;
285 mwi.win_style = WS_OVERLAPPEDWINDOW;
286 mwi.sr_color = mwi.nsr_color = 0xFFFFFF;
288 return &mwi;
291 /******************************************************************
292 * HLPFILE_GetPopupWindowInfo
296 static HLPFILE_WINDOWINFO* WINHELP_GetPopupWindowInfo(HLPFILE* hlpfile,
297 WINHELP_WINDOW* parent, LPARAM mouse)
299 static HLPFILE_WINDOWINFO wi;
301 RECT parent_rect;
303 wi.type[0] = wi.name[0] = wi.caption[0] = '\0';
305 /* Calculate horizontal size and position of a popup window */
306 GetWindowRect(parent->hMainWnd, &parent_rect);
307 wi.size.cx = (parent_rect.right - parent_rect.left) / 2;
308 wi.size.cy = 10; /* need a non null value, so that borders are taken into account while computing */
310 wi.origin.x = (short)LOWORD(mouse);
311 wi.origin.y = (short)HIWORD(mouse);
312 ClientToScreen(parent->hMainWnd, &wi.origin);
313 wi.origin.x -= wi.size.cx / 2;
314 wi.origin.x = min(wi.origin.x, GetSystemMetrics(SM_CXSCREEN) - wi.size.cx);
315 wi.origin.x = max(wi.origin.x, 0);
317 wi.style = SW_SHOW;
318 wi.win_style = WS_POPUP | WS_BORDER;
319 if (parent->page->file->has_popup_color)
320 wi.sr_color = parent->page->file->popup_color;
321 else
322 wi.sr_color = parent->info->sr_color;
323 wi.nsr_color = 0xFFFFFF;
325 return &wi;
328 typedef struct
330 WORD size;
331 WORD command;
332 LONG data;
333 LONG reserved;
334 WORD ofsFilename;
335 WORD ofsData;
336 } WINHELP,*LPWINHELP;
338 static BOOL WINHELP_HasWorkingWindow(void)
340 if (!Globals.active_win) return FALSE;
341 if (Globals.active_win->next || Globals.win_list != Globals.active_win) return TRUE;
342 return Globals.active_win->page != NULL && Globals.active_win->page->file != NULL;
345 /******************************************************************
346 * WINHELP_HandleCommand
350 static LRESULT WINHELP_HandleCommand(HWND hSrcWnd, LPARAM lParam)
352 COPYDATASTRUCT* cds = (COPYDATASTRUCT*)lParam;
353 WINHELP* wh;
355 if (cds->dwData != 0xA1DE505)
357 WINE_FIXME("Wrong magic number (%08Ix)\n", cds->dwData);
358 return 0;
361 wh = cds->lpData;
363 if (wh)
365 char* ptr = (wh->ofsFilename) ? (LPSTR)wh + wh->ofsFilename : NULL;
367 WINE_TRACE("Got[%u]: cmd=%u data=%08lx fn=%s\n",
368 wh->size, wh->command, wh->data, debugstr_a(ptr));
369 switch (wh->command)
371 case HELP_CONTEXT:
372 if (ptr)
374 MACRO_JumpContext(ptr, "main", wh->data);
376 if (!WINHELP_HasWorkingWindow()) MACRO_Exit();
377 break;
378 case HELP_QUIT:
379 MACRO_Exit();
380 break;
381 case HELP_CONTENTS:
382 if (ptr)
384 MACRO_JumpContents(ptr, "main");
386 if (!WINHELP_HasWorkingWindow()) MACRO_Exit();
387 break;
388 case HELP_HELPONHELP:
389 MACRO_HelpOn();
390 if (!WINHELP_HasWorkingWindow()) MACRO_Exit();
391 break;
392 /* case HELP_SETINDEX: */
393 case HELP_SETCONTENTS:
394 if (ptr)
396 MACRO_SetContents(ptr, wh->data);
398 break;
399 case HELP_CONTEXTPOPUP:
400 if (ptr)
402 MACRO_PopupContext(ptr, wh->data);
404 break;
405 /* case HELP_FORCEFILE:*/
406 /* case HELP_CONTEXTMENU: */
407 case HELP_FINDER:
408 /* in fact, should be the topic dialog box */
409 WINE_FIXME("HELP_FINDER: stub\n");
410 if (ptr)
412 MACRO_JumpHash(ptr, "main", 0);
414 break;
415 /* case HELP_WM_HELP: */
416 /* case HELP_SETPOPUP_POS: */
417 /* case HELP_KEY: */
418 /* case HELP_COMMAND: */
419 /* case HELP_PARTIALKEY: */
420 /* case HELP_MULTIKEY: */
421 /* case HELP_SETWINPOS: */
422 default:
423 WINE_FIXME("Unhandled command (%x) for remote winhelp control\n", wh->command);
424 break;
427 /* Always return success for now */
428 return 1;
431 void WINHELP_LayoutMainWindow(WINHELP_WINDOW* win)
433 RECT rect, button_box_rect;
434 INT text_top = 0;
435 HWND hButtonBoxWnd = GetDlgItem(win->hMainWnd, CTL_ID_BUTTON);
436 HWND hTextWnd = GetDlgItem(win->hMainWnd, CTL_ID_TEXT);
438 GetClientRect(win->hMainWnd, &rect);
440 /* Update button box and text Window */
441 SetWindowPos(hButtonBoxWnd, HWND_TOP,
442 rect.left, rect.top,
443 rect.right - rect.left,
444 rect.bottom - rect.top, 0);
446 if (GetWindowRect(hButtonBoxWnd, &button_box_rect))
447 text_top = rect.top + button_box_rect.bottom - button_box_rect.top;
449 SetWindowPos(hTextWnd, HWND_TOP,
450 rect.left, text_top,
451 rect.right - rect.left,
452 rect.bottom - text_top, 0);
456 /******************************************************************
457 * WINHELP_DeleteButtons
460 static void WINHELP_DeleteButtons(WINHELP_WINDOW* win)
462 WINHELP_BUTTON* b;
463 WINHELP_BUTTON* bp;
465 for (b = win->first_button; b; b = bp)
467 DestroyWindow(b->hWnd);
468 bp = b->next;
469 free(b);
471 win->first_button = NULL;
474 /******************************************************************
475 * WINHELP_DeleteBackSet
478 void WINHELP_DeleteBackSet(WINHELP_WINDOW* win)
480 unsigned int i;
482 for (i = 0; i < win->back.index; i++)
484 HLPFILE_FreeHlpFile(win->back.set[i].page->file);
485 win->back.set[i].page = NULL;
487 win->back.index = 0;
490 /******************************************************************
491 * WINHELP_DeletePageLinks
494 static void WINHELP_DeletePageLinks(HLPFILE_PAGE* page)
496 HLPFILE_LINK* curr;
497 HLPFILE_LINK* next;
499 for (curr = page->first_link; curr; curr = next)
501 next = curr->next;
502 free(curr);
506 /***********************************************************************
508 * WINHELP_GrabWindow
510 WINHELP_WINDOW* WINHELP_GrabWindow(WINHELP_WINDOW* win)
512 WINE_TRACE("Grab %p#%d++\n", win, win->ref_count);
513 win->ref_count++;
514 return win;
517 /***********************************************************************
519 * WINHELP_ReleaseWindow
521 BOOL WINHELP_ReleaseWindow(WINHELP_WINDOW* win)
523 WINE_TRACE("Release %p#%d--\n", win, win->ref_count);
525 if (!--win->ref_count)
527 DestroyWindow(win->hMainWnd);
528 return FALSE;
530 return TRUE;
533 /***********************************************************************
535 * WINHELP_DeleteWindow
537 static void WINHELP_DeleteWindow(WINHELP_WINDOW* win)
539 WINHELP_WINDOW** w;
540 BOOL bExit;
541 HWND hTextWnd;
543 for (w = &Globals.win_list; *w; w = &(*w)->next)
545 if (*w == win)
547 *w = win->next;
548 break;
551 bExit = (Globals.wVersion >= 4 && !lstrcmpiA(win->info->name, "main"));
553 if (Globals.active_win == win)
555 Globals.active_win = Globals.win_list;
556 if (Globals.win_list)
557 SetActiveWindow(Globals.win_list->hMainWnd);
560 if (win == Globals.active_popup)
561 Globals.active_popup = NULL;
563 hTextWnd = GetDlgItem(win->hMainWnd, CTL_ID_TEXT);
564 SetWindowLongPtrA(hTextWnd, GWLP_WNDPROC, (LONG_PTR)win->origRicheditWndProc);
566 WINHELP_DeleteButtons(win);
568 if (win->page) WINHELP_DeletePageLinks(win->page);
569 if (win->hHistoryWnd) DestroyWindow(win->hHistoryWnd);
571 DeleteObject(win->hBrush);
573 WINHELP_DeleteBackSet(win);
575 if (win->page) HLPFILE_FreeHlpFile(win->page->file);
576 free(win);
578 if (bExit) MACRO_Exit();
579 if (!Globals.win_list)
580 PostQuitMessage(0);
583 static char* WINHELP_GetCaption(WINHELP_WNDPAGE* wpage)
585 if (wpage->wininfo->caption[0]) return wpage->wininfo->caption;
586 return wpage->page->file->lpszTitle;
589 static void WINHELP_RememberPage(WINHELP_WINDOW* win, WINHELP_WNDPAGE* wpage)
591 unsigned num;
593 if (!Globals.history.index || Globals.history.set[0].page != wpage->page)
595 num = ARRAY_SIZE(Globals.history.set);
596 /* we're full, remove latest entry */
597 if (Globals.history.index == num)
599 HLPFILE_FreeHlpFile(Globals.history.set[num - 1].page->file);
600 Globals.history.index--;
602 memmove(&Globals.history.set[1], &Globals.history.set[0],
603 Globals.history.index * sizeof(Globals.history.set[0]));
604 Globals.history.set[0] = *wpage;
605 Globals.history.index++;
606 wpage->page->file->wRefCount++;
608 if (win->hHistoryWnd) InvalidateRect(win->hHistoryWnd, NULL, TRUE);
610 num = ARRAY_SIZE(win->back.set);
611 if (win->back.index == num)
613 /* we're full, remove latest entry */
614 HLPFILE_FreeHlpFile(win->back.set[0].page->file);
615 memmove(&win->back.set[0], &win->back.set[1],
616 (num - 1) * sizeof(win->back.set[0]));
617 win->back.index--;
619 win->back.set[win->back.index++] = *wpage;
620 wpage->page->file->wRefCount++;
623 /***********************************************************************
625 * WINHELP_FindLink
627 static HLPFILE_LINK* WINHELP_FindLink(WINHELP_WINDOW* win, LPARAM pos)
629 HLPFILE_LINK* link;
630 POINTL mouse_ptl, char_ptl, char_next_ptl;
631 DWORD cp;
633 if (!win->page) return NULL;
635 mouse_ptl.x = (short)LOWORD(pos);
636 mouse_ptl.y = (short)HIWORD(pos);
637 cp = SendMessageW(GetDlgItem(win->hMainWnd, CTL_ID_TEXT), EM_CHARFROMPOS,
638 0, (LPARAM)&mouse_ptl);
640 for (link = win->page->first_link; link; link = link->next)
642 if (link->cpMin <= cp && cp <= link->cpMax)
644 /* check whether we're at end of line */
645 SendMessageW(GetDlgItem(win->hMainWnd, CTL_ID_TEXT), EM_POSFROMCHAR,
646 (LPARAM)&char_ptl, cp);
647 SendMessageW(GetDlgItem(win->hMainWnd, CTL_ID_TEXT), EM_POSFROMCHAR,
648 (LPARAM)&char_next_ptl, cp + 1);
649 if (link->bHotSpot)
651 HLPFILE_HOTSPOTLINK* hslink = (HLPFILE_HOTSPOTLINK*)link;
652 if ((mouse_ptl.x < char_ptl.x + hslink->x) ||
653 (mouse_ptl.x >= char_ptl.x + hslink->x + hslink->width) ||
654 (mouse_ptl.y < char_ptl.y + hslink->y) ||
655 (mouse_ptl.y >= char_ptl.y + hslink->y + hslink->height))
656 continue;
657 break;
659 if (char_next_ptl.y != char_ptl.y || mouse_ptl.x >= char_next_ptl.x)
660 link = NULL;
661 break;
664 return link;
667 static LRESULT CALLBACK WINHELP_RicheditWndProc(HWND hWnd, UINT msg,
668 WPARAM wParam, LPARAM lParam)
670 WINHELP_WINDOW *win = (WINHELP_WINDOW*) GetWindowLongPtrW(GetParent(hWnd), 0);
671 DWORD messagePos;
672 POINT pt;
673 switch(msg)
675 case WM_SETCURSOR:
676 messagePos = GetMessagePos();
677 pt.x = (short)LOWORD(messagePos);
678 pt.y = (short)HIWORD(messagePos);
679 ScreenToClient(hWnd, &pt);
680 if (win->page && WINHELP_FindLink(win, MAKELPARAM(pt.x, pt.y)))
682 SetCursor(win->hHandCur);
683 return 0;
685 /* fall through */
686 default:
687 return CallWindowProcA(win->origRicheditWndProc, hWnd, msg, wParam, lParam);
691 /***********************************************************************
693 * WINHELP_CreateHelpWindow
695 BOOL WINHELP_CreateHelpWindow(WINHELP_WNDPAGE* wpage, int nCmdShow, BOOL remember)
697 WINHELP_WINDOW* win = NULL;
698 BOOL bPrimary, bPopup, bReUsed = FALSE;
699 HICON hIcon;
700 HWND hTextWnd = NULL;
702 bPrimary = !lstrcmpiA(wpage->wininfo->name, "main");
703 bPopup = !bPrimary && (wpage->wininfo->win_style & WS_POPUP);
705 if (!bPopup)
707 for (win = Globals.win_list; win; win = win->next)
709 if (!lstrcmpiA(win->info->name, wpage->wininfo->name))
711 if (win->page == wpage->page && win->info == wpage->wininfo)
713 /* see #22979, some hlp files have a macro (run at page opening), which
714 * jumps to the very same page
715 * Exit gracefully in that case
717 return TRUE;
719 WINHELP_DeleteButtons(win);
720 bReUsed = TRUE;
721 SetWindowTextA(win->hMainWnd, WINHELP_GetCaption(wpage));
722 if (win->info != wpage->wininfo)
724 POINT pt = {0, 0};
725 SIZE sz = {0, 0};
726 DWORD flags = SWP_NOSIZE | SWP_NOMOVE;
728 if (wpage->wininfo->origin.x != CW_USEDEFAULT &&
729 wpage->wininfo->origin.y != CW_USEDEFAULT)
731 pt = wpage->wininfo->origin;
732 flags &= ~SWP_NOSIZE;
734 if (wpage->wininfo->size.cx != CW_USEDEFAULT &&
735 wpage->wininfo->size.cy != CW_USEDEFAULT)
737 sz = wpage->wininfo->size;
738 flags &= ~SWP_NOMOVE;
740 SetWindowPos(win->hMainWnd, HWND_TOP, pt.x, pt.y, sz.cx, sz.cy, flags);
743 if (wpage->page && win->page && wpage->page->file != win->page->file)
744 WINHELP_DeleteBackSet(win);
745 WINHELP_InitFonts(win->hMainWnd);
747 win->page = wpage->page;
748 win->info = wpage->wininfo;
749 hTextWnd = GetDlgItem(win->hMainWnd, CTL_ID_TEXT);
750 WINHELP_SetupText(hTextWnd, win, wpage->relative);
752 InvalidateRect(win->hMainWnd, NULL, TRUE);
753 if (win->hHistoryWnd) InvalidateRect(win->hHistoryWnd, NULL, TRUE);
755 break;
760 if (!win)
762 /* Initialize WINHELP_WINDOW struct */
763 win = calloc(1, sizeof(WINHELP_WINDOW));
764 if (!win) return FALSE;
765 win->next = Globals.win_list;
766 Globals.win_list = win;
768 win->hHandCur = LoadCursorW(0, (LPWSTR)IDC_HAND);
769 win->back.index = 0;
770 win->font_scale = 1;
771 WINHELP_GrabWindow(win);
773 win->page = wpage->page;
774 win->info = wpage->wininfo;
775 WINHELP_GrabWindow(win);
777 if (!bPopup && wpage->page && remember)
779 WINHELP_RememberPage(win, wpage);
782 if (bPopup)
783 Globals.active_popup = win;
784 else
785 Globals.active_win = win;
787 /* Initialize default pushbuttons */
788 if (bPrimary && wpage->page)
790 CHAR buffer[MAX_STRING_LEN];
792 LoadStringA(Globals.hInstance, STID_CONTENTS, buffer, sizeof(buffer));
793 MACRO_CreateButton("BTN_CONTENTS", buffer, "Contents()");
794 LoadStringA(Globals.hInstance, STID_INDEX, buffer, sizeof(buffer));
795 MACRO_CreateButton("BTN_INDEX", buffer, "Finder()");
796 LoadStringA(Globals.hInstance, STID_BACK, buffer, sizeof(buffer));
797 MACRO_CreateButton("BTN_BACK", buffer, "Back()");
798 if (win->back.index <= 1) MACRO_DisableButton("BTN_BACK");
801 if (!bReUsed)
803 win->hMainWnd = CreateWindowExA((bPopup) ? WS_EX_TOOLWINDOW : 0, MAIN_WIN_CLASS_NAME,
804 WINHELP_GetCaption(wpage),
805 bPrimary ? WS_OVERLAPPEDWINDOW : wpage->wininfo->win_style,
806 wpage->wininfo->origin.x, wpage->wininfo->origin.y,
807 wpage->wininfo->size.cx, wpage->wininfo->size.cy,
808 bPopup ? Globals.active_win->hMainWnd : NULL,
809 bPrimary ? LoadMenuW(Globals.hInstance, MAKEINTRESOURCEW(MAIN_MENU)) : 0,
810 Globals.hInstance, win);
811 if (!bPopup)
812 /* Create button box and text Window */
813 CreateWindowA(BUTTON_BOX_WIN_CLASS_NAME, "", WS_CHILD | WS_VISIBLE,
814 0, 0, 0, 0, win->hMainWnd, (HMENU)CTL_ID_BUTTON, Globals.hInstance, NULL);
816 hTextWnd = CreateWindowA(RICHEDIT_CLASS20A, NULL,
817 ES_MULTILINE | ES_READONLY | WS_CHILD | WS_HSCROLL | WS_VSCROLL | WS_VISIBLE,
818 0, 0, 0, 0, win->hMainWnd, (HMENU)CTL_ID_TEXT, Globals.hInstance, NULL);
819 SendMessageW(hTextWnd, EM_SETEVENTMASK, 0,
820 SendMessageW(hTextWnd, EM_GETEVENTMASK, 0, 0) | ENM_MOUSEEVENTS);
821 win->origRicheditWndProc = (WNDPROC)SetWindowLongPtrA(hTextWnd, GWLP_WNDPROC,
822 (LONG_PTR)WINHELP_RicheditWndProc);
825 hIcon = (wpage->page) ? wpage->page->file->hIcon : NULL;
826 if (!hIcon) hIcon = LoadImageW(Globals.hInstance, MAKEINTRESOURCEW(IDI_WINHELP), IMAGE_ICON,
827 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
828 SendMessageW(win->hMainWnd, WM_SETICON, ICON_SMALL, (DWORD_PTR)hIcon);
830 /* Initialize file specific pushbuttons */
831 if (!(wpage->wininfo->win_style & WS_POPUP) && wpage->page)
833 HLPFILE_MACRO *macro;
834 for (macro = wpage->page->file->first_macro; macro; macro = macro->next)
835 MACRO_ExecuteMacro(win, macro->lpszMacro);
837 for (macro = wpage->page->first_macro; macro; macro = macro->next)
838 MACRO_ExecuteMacro(win, macro->lpszMacro);
840 /* See #17681, in some cases, the newly created window is closed by the macros it contains
841 * (braindead), so deal with this case
843 for (win = Globals.win_list; win; win = win->next)
845 if (!lstrcmpiA(win->info->name, wpage->wininfo->name)) break;
847 if (!win || !WINHELP_ReleaseWindow(win)) return TRUE;
849 if (bPopup)
851 DWORD mask = SendMessageW(hTextWnd, EM_GETEVENTMASK, 0, 0);
853 win->font_scale = Globals.active_win->font_scale;
854 WINHELP_SetupText(hTextWnd, win, wpage->relative);
856 /* we need the window to be shown for richedit to compute the size */
857 ShowWindow(win->hMainWnd, nCmdShow);
858 SendMessageW(hTextWnd, EM_SETEVENTMASK, 0, mask | ENM_REQUESTRESIZE);
859 SendMessageW(hTextWnd, EM_REQUESTRESIZE, 0, 0);
860 SendMessageW(hTextWnd, EM_SETEVENTMASK, 0, mask);
862 else
864 WINHELP_SetupText(hTextWnd, win, wpage->relative);
865 WINHELP_LayoutMainWindow(win);
866 ShowWindow(win->hMainWnd, nCmdShow);
869 return TRUE;
872 /******************************************************************
873 * WINHELP_OpenHelpWindow
874 * Main function to search for a page and display it in a window
876 BOOL WINHELP_OpenHelpWindow(HLPFILE_PAGE* (*lookup)(HLPFILE*, LONG, ULONG*),
877 HLPFILE* hlpfile, LONG val, HLPFILE_WINDOWINFO* wi,
878 int nCmdShow)
880 WINHELP_WNDPAGE wpage;
882 wpage.page = lookup(hlpfile, val, &wpage.relative);
883 if (wpage.page) wpage.page->file->wRefCount++;
884 wpage.wininfo = wi;
885 return WINHELP_CreateHelpWindow(&wpage, nCmdShow, TRUE);
888 /******************************************************************
889 * WINHELP_HandleTextMouse
892 static BOOL WINHELP_HandleTextMouse(WINHELP_WINDOW* win, UINT msg, LPARAM lParam)
894 HLPFILE* hlpfile;
895 HLPFILE_LINK* link;
896 BOOL ret = FALSE;
898 switch (msg)
900 case WM_LBUTTONDOWN:
901 if ((link = WINHELP_FindLink(win, lParam)))
903 HLPFILE_WINDOWINFO* wi;
905 switch (link->cookie)
907 case hlp_link_link:
908 if ((hlpfile = WINHELP_LookupHelpFile(link->string)))
910 if (link->window == -1)
912 wi = win->info;
913 if (wi->win_style & WS_POPUP) wi = Globals.active_win->info;
915 else if (link->window < hlpfile->numWindows)
916 wi = &hlpfile->windows[link->window];
917 else
919 WINE_WARN("link to window %d/%d\n", link->window, hlpfile->numWindows);
920 break;
922 WINHELP_OpenHelpWindow(HLPFILE_PageByHash, hlpfile, link->hash, wi, SW_NORMAL);
924 break;
925 case hlp_link_popup:
926 if ((hlpfile = WINHELP_LookupHelpFile(link->string)))
927 WINHELP_OpenHelpWindow(HLPFILE_PageByHash, hlpfile, link->hash,
928 WINHELP_GetPopupWindowInfo(hlpfile, win, lParam),
929 SW_NORMAL);
930 break;
931 case hlp_link_macro:
932 MACRO_ExecuteMacro(win, link->string);
933 break;
934 default:
935 WINE_FIXME("Unknown link cookie %d\n", link->cookie);
937 ret = TRUE;
939 break;
941 return ret;
944 /***********************************************************************
946 * WINHELP_CheckPopup
948 static BOOL WINHELP_CheckPopup(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, LRESULT* lret)
950 WINHELP_WINDOW* popup;
952 if (!Globals.active_popup) return FALSE;
954 switch (msg)
956 case WM_NOTIFY:
958 MSGFILTER* msgf = (MSGFILTER*)lParam;
959 if (msgf->nmhdr.code == EN_MSGFILTER)
961 if (!WINHELP_CheckPopup(hWnd, msgf->msg, msgf->wParam, msgf->lParam, NULL))
962 return FALSE;
963 if (lret) *lret = 1;
964 return TRUE;
967 break;
968 case WM_ACTIVATE:
969 if (LOWORD(wParam) != WA_INACTIVE || (HWND)lParam == Globals.active_win->hMainWnd ||
970 (HWND)lParam == Globals.active_popup->hMainWnd ||
971 GetWindow((HWND)lParam, GW_OWNER) == Globals.active_win->hMainWnd)
972 break;
973 /* fall through */
974 case WM_LBUTTONDOWN:
975 if (msg == WM_LBUTTONDOWN)
976 WINHELP_HandleTextMouse(Globals.active_popup, msg, lParam);
977 /* fall through */
978 case WM_MBUTTONDOWN:
979 case WM_RBUTTONDOWN:
980 case WM_NCLBUTTONDOWN:
981 case WM_NCMBUTTONDOWN:
982 case WM_NCRBUTTONDOWN:
983 popup = Globals.active_popup;
984 Globals.active_popup = NULL;
985 WINHELP_ReleaseWindow(popup);
986 if (lret) *lret = 1;
987 return TRUE;
989 return FALSE;
992 /***********************************************************************
994 * WINHELP_ButtonWndProc
996 static LRESULT CALLBACK WINHELP_ButtonWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
998 if (WINHELP_CheckPopup(hWnd, msg, wParam, lParam, NULL)) return 0;
1000 if (msg == WM_KEYDOWN)
1002 switch (wParam)
1004 case VK_UP:
1005 case VK_DOWN:
1006 case VK_PRIOR:
1007 case VK_NEXT:
1008 case VK_ESCAPE:
1009 return SendMessageA(GetParent(hWnd), msg, wParam, lParam);
1013 return CallWindowProcA(Globals.button_proc, hWnd, msg, wParam, lParam);
1016 /***********************************************************************
1018 * WINHELP_ButtonBoxWndProc
1020 static LRESULT CALLBACK WINHELP_ButtonBoxWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1022 WINDOWPOS *winpos;
1023 WINHELP_WINDOW *win;
1024 WINHELP_BUTTON *button;
1025 SIZE button_size;
1026 INT x, y;
1028 if (WINHELP_CheckPopup(hWnd, msg, wParam, lParam, NULL)) return 0L;
1030 switch (msg)
1032 case WM_WINDOWPOSCHANGING:
1033 winpos = (WINDOWPOS*) lParam;
1034 win = (WINHELP_WINDOW*) GetWindowLongPtrW(GetParent(hWnd), 0);
1036 /* Update buttons */
1037 button_size.cx = 0;
1038 button_size.cy = 0;
1039 for (button = win->first_button; button; button = button->next)
1041 HDC hDc;
1042 SIZE textsize;
1043 if (!button->hWnd)
1045 button->hWnd = CreateWindowA(STRING_BUTTON, button->lpszName,
1046 WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
1047 0, 0, 0, 0,
1048 hWnd, (HMENU) button->wParam,
1049 Globals.hInstance, 0);
1050 if (button->hWnd)
1052 if (Globals.button_proc == NULL)
1054 NONCLIENTMETRICSW ncm;
1055 Globals.button_proc = (WNDPROC) GetWindowLongPtrA(button->hWnd, GWLP_WNDPROC);
1057 ncm.cbSize = sizeof(NONCLIENTMETRICSW);
1058 SystemParametersInfoW(SPI_GETNONCLIENTMETRICS,
1059 sizeof(NONCLIENTMETRICSW), &ncm, 0);
1060 Globals.hButtonFont = CreateFontIndirectW(&ncm.lfMenuFont);
1062 SetWindowLongPtrA(button->hWnd, GWLP_WNDPROC, (LONG_PTR) WINHELP_ButtonWndProc);
1063 if (Globals.hButtonFont)
1064 SendMessageW(button->hWnd, WM_SETFONT, (WPARAM)Globals.hButtonFont, TRUE);
1067 hDc = GetDC(button->hWnd);
1068 GetTextExtentPointA(hDc, button->lpszName, strlen(button->lpszName), &textsize);
1069 ReleaseDC(button->hWnd, hDc);
1071 button_size.cx = max(button_size.cx, textsize.cx + BUTTON_CX);
1072 button_size.cy = max(button_size.cy, textsize.cy + BUTTON_CY);
1075 x = 0;
1076 y = 0;
1077 for (button = win->first_button; button; button = button->next)
1079 SetWindowPos(button->hWnd, HWND_TOP, x, y, button_size.cx, button_size.cy, 0);
1081 if (x + 2 * button_size.cx <= winpos->cx)
1082 x += button_size.cx;
1083 else
1084 x = 0, y += button_size.cy;
1086 winpos->cy = y + (x ? button_size.cy : 0);
1087 break;
1089 case WM_COMMAND:
1090 SendMessageW(GetParent(hWnd), msg, wParam, lParam);
1091 break;
1093 case WM_KEYDOWN:
1094 switch (wParam)
1096 case VK_UP:
1097 case VK_DOWN:
1098 case VK_PRIOR:
1099 case VK_NEXT:
1100 case VK_ESCAPE:
1101 return SendMessageA(GetParent(hWnd), msg, wParam, lParam);
1103 break;
1106 return DefWindowProcA(hWnd, msg, wParam, lParam);
1109 /******************************************************************
1110 * WINHELP_HistoryWndProc
1114 static LRESULT CALLBACK WINHELP_HistoryWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1116 WINHELP_WINDOW* win;
1117 PAINTSTRUCT ps;
1118 HDC hDc;
1119 TEXTMETRICW tm;
1120 unsigned int i;
1121 RECT r;
1123 switch (msg)
1125 case WM_NCCREATE:
1126 win = (WINHELP_WINDOW*)((LPCREATESTRUCTA)lParam)->lpCreateParams;
1127 SetWindowLongPtrW(hWnd, 0, (ULONG_PTR)win);
1128 win->hHistoryWnd = hWnd;
1129 break;
1130 case WM_CREATE:
1131 hDc = GetDC(hWnd);
1132 GetTextMetricsW(hDc, &tm);
1133 GetWindowRect(hWnd, &r);
1135 r.right = r.left + 30 * tm.tmAveCharWidth;
1136 r.bottom = r.top + ARRAY_SIZE(Globals.history.set) * tm.tmHeight;
1137 AdjustWindowRect(&r, GetWindowLongW(hWnd, GWL_STYLE), FALSE);
1138 if (r.left < 0) {r.right -= r.left; r.left = 0;}
1139 if (r.top < 0) {r.bottom -= r.top; r.top = 0;}
1141 MoveWindow(hWnd, r.left, r.top, r.right, r.bottom, TRUE);
1142 ReleaseDC(hWnd, hDc);
1143 break;
1144 case WM_LBUTTONDOWN:
1145 hDc = GetDC(hWnd);
1146 GetTextMetricsW(hDc, &tm);
1147 i = HIWORD(lParam) / tm.tmHeight;
1148 if (i < Globals.history.index)
1149 WINHELP_CreateHelpWindow(&Globals.history.set[i], SW_SHOW, TRUE);
1150 ReleaseDC(hWnd, hDc);
1151 break;
1152 case WM_PAINT:
1153 hDc = BeginPaint(hWnd, &ps);
1154 GetTextMetricsW(hDc, &tm);
1156 for (i = 0; i < Globals.history.index; i++)
1158 if (Globals.history.set[i].page->file == Globals.active_win->page->file)
1160 TextOutA(hDc, 0, i * tm.tmHeight,
1161 Globals.history.set[i].page->lpszTitle,
1162 strlen(Globals.history.set[i].page->lpszTitle));
1164 else
1166 char buffer[1024];
1167 const char* ptr1;
1168 const char* ptr2;
1169 unsigned len;
1171 ptr1 = strrchr(Globals.history.set[i].page->file->lpszPath, '\\');
1172 if (!ptr1) ptr1 = Globals.history.set[i].page->file->lpszPath;
1173 else ptr1++;
1174 ptr2 = strrchr(ptr1, '.');
1175 len = ptr2 ? ptr2 - ptr1 : strlen(ptr1);
1176 if (len > sizeof(buffer)) len = sizeof(buffer);
1177 memcpy(buffer, ptr1, len);
1178 if (len < sizeof(buffer)) buffer[len++] = ':';
1179 lstrcpynA(&buffer[len], Globals.history.set[i].page->lpszTitle, sizeof(buffer) - len);
1180 TextOutA(hDc, 0, i * tm.tmHeight, buffer, strlen(buffer));
1183 EndPaint(hWnd, &ps);
1184 break;
1185 case WM_DESTROY:
1186 win = (WINHELP_WINDOW*) GetWindowLongPtrW(hWnd, 0);
1187 if (hWnd == win->hHistoryWnd)
1188 win->hHistoryWnd = 0;
1189 break;
1191 return DefWindowProcA(hWnd, msg, wParam, lParam);
1194 /**************************************************************************
1195 * cb_KWBTree
1197 * HLPFILE_BPTreeCallback enumeration function for '|KWBTREE' internal file.
1200 static void cb_KWBTree(void *p, void **next, void *cookie)
1202 HWND hListWnd = cookie;
1203 int count;
1205 WINE_TRACE("Adding %s to search list\n", debugstr_a((char *)p));
1206 SendMessageA(hListWnd, LB_INSERTSTRING, -1, (LPARAM)p);
1207 count = SendMessageW(hListWnd, LB_GETCOUNT, 0, 0);
1208 SendMessageW(hListWnd, LB_SETITEMDATA, count-1, (LPARAM)p);
1209 *next = (char*)p + strlen((char*)p) + 7;
1212 struct index_data
1214 HLPFILE* hlpfile;
1215 BOOL jump;
1216 ULONG offset;
1219 /**************************************************************************
1220 * WINHELP_IndexDlgProc
1223 static INT_PTR CALLBACK WINHELP_IndexDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1225 static struct index_data* id;
1226 int sel;
1228 switch (msg)
1230 case WM_INITDIALOG:
1231 id = (struct index_data*)((PROPSHEETPAGEA*)lParam)->lParam;
1232 HLPFILE_BPTreeEnum(id->hlpfile->kwbtree, cb_KWBTree,
1233 GetDlgItem(hWnd, IDC_INDEXLIST));
1234 id->jump = FALSE;
1235 id->offset = 1;
1236 return TRUE;
1237 case WM_COMMAND:
1238 switch (HIWORD(wParam))
1240 case LBN_DBLCLK:
1241 if (LOWORD(wParam) == IDC_INDEXLIST)
1242 SendMessageW(GetParent(hWnd), PSM_PRESSBUTTON, PSBTN_OK, 0);
1243 break;
1245 break;
1246 case WM_NOTIFY:
1247 switch (((NMHDR*)lParam)->code)
1249 case PSN_APPLY:
1250 sel = SendDlgItemMessageW(hWnd, IDC_INDEXLIST, LB_GETCURSEL, 0, 0);
1251 if (sel != LB_ERR)
1253 BYTE *p;
1254 int count;
1256 p = (BYTE*)SendDlgItemMessageW(hWnd, IDC_INDEXLIST, LB_GETITEMDATA, sel, 0);
1257 count = *(short*)((char *)p + strlen((char *)p) + 1);
1258 if (count > 1)
1260 MessageBoxA(hWnd, "count > 1 not supported yet", "Error", MB_OK | MB_ICONSTOP);
1261 SetWindowLongPtrW(hWnd, DWLP_MSGRESULT, PSNRET_INVALID);
1262 return TRUE;
1264 id->offset = *(ULONG*)((char *)p + strlen((char *)p) + 3);
1265 id->offset = *(long*)(id->hlpfile->kwdata + id->offset + 9);
1266 if (id->offset == 0xFFFFFFFF)
1268 MessageBoxA(hWnd, "macro keywords not supported yet", "Error", MB_OK | MB_ICONSTOP);
1269 SetWindowLongPtrW(hWnd, DWLP_MSGRESULT, PSNRET_INVALID);
1270 return TRUE;
1272 id->jump = TRUE;
1273 SetWindowLongPtrW(hWnd, DWLP_MSGRESULT, PSNRET_NOERROR);
1275 return TRUE;
1276 default:
1277 return FALSE;
1279 break;
1280 default:
1281 break;
1283 return FALSE;
1286 /**************************************************************************
1287 * WINHELP_SearchDlgProc
1290 static INT_PTR CALLBACK WINHELP_SearchDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1292 switch (msg)
1294 case WM_INITDIALOG:
1295 return TRUE;
1296 case WM_NOTIFY:
1297 switch (((NMHDR*)lParam)->code)
1299 case PSN_APPLY:
1300 SetWindowLongPtrW(hWnd, DWLP_MSGRESULT, PSNRET_NOERROR);
1301 return TRUE;
1302 default:
1303 return FALSE;
1305 break;
1306 default:
1307 break;
1309 return FALSE;
1312 /***********************************************************************
1314 * WINHELP_MainWndProc
1316 static LRESULT CALLBACK WINHELP_MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1318 WINHELP_WINDOW *win;
1319 WINHELP_BUTTON *button;
1320 HWND hTextWnd;
1321 LRESULT ret;
1323 if (WINHELP_CheckPopup(hWnd, msg, wParam, lParam, &ret)) return ret;
1325 switch (msg)
1327 case WM_NCCREATE:
1328 win = (WINHELP_WINDOW*) ((LPCREATESTRUCTA) lParam)->lpCreateParams;
1329 SetWindowLongPtrW(hWnd, 0, (ULONG_PTR) win);
1330 if (!win->page && Globals.isBook)
1331 PostMessageW(hWnd, WM_COMMAND, MNID_FILE_OPEN, 0);
1332 win->hMainWnd = hWnd;
1333 break;
1335 case WM_WINDOWPOSCHANGED:
1336 WINHELP_LayoutMainWindow((WINHELP_WINDOW*) GetWindowLongPtrW(hWnd, 0));
1337 break;
1339 case WM_COMMAND:
1340 win = (WINHELP_WINDOW*) GetWindowLongPtrW(hWnd, 0);
1341 switch (LOWORD(wParam))
1343 /* Menu FILE */
1344 case MNID_FILE_OPEN: MACRO_FileOpen(); break;
1345 case MNID_FILE_PRINT: MACRO_Print(); break;
1346 case MNID_FILE_SETUP: MACRO_PrinterSetup(); break;
1347 case MNID_FILE_EXIT: MACRO_Exit(); break;
1349 /* Menu EDIT */
1350 case MNID_EDIT_COPYDLG:
1351 SendDlgItemMessageW(hWnd, CTL_ID_TEXT, WM_COPY, 0, 0);
1352 break;
1353 case MNID_EDIT_ANNOTATE:MACRO_Annotate(); break;
1355 /* Menu Bookmark */
1356 case MNID_BKMK_DEFINE: MACRO_BookmarkDefine(); break;
1358 /* Menu Help */
1359 case MNID_HELP_HELPON: MACRO_HelpOn(); break;
1360 case MNID_HELP_HELPTOP: MACRO_HelpOnTop(); break;
1361 case MNID_HELP_ABOUT: MACRO_About(); break;
1363 /* Context help */
1364 case MNID_CTXT_ANNOTATE:MACRO_Annotate(); break;
1365 case MNID_CTXT_COPY: MACRO_CopyDialog(); break;
1366 case MNID_CTXT_PRINT: MACRO_Print(); break;
1367 case MNID_OPTS_HISTORY: MACRO_History(); break;
1368 case MNID_OPTS_FONTS_SMALL:
1369 case MNID_CTXT_FONTS_SMALL:
1370 win = (WINHELP_WINDOW*) GetWindowLongPtrW(hWnd, 0);
1371 if (win->font_scale != 0)
1373 win->font_scale = 0;
1374 WINHELP_SetupText(GetDlgItem(hWnd, CTL_ID_TEXT), win, 0 /* FIXME */);
1376 break;
1377 case MNID_OPTS_FONTS_NORMAL:
1378 case MNID_CTXT_FONTS_NORMAL:
1379 win = (WINHELP_WINDOW*) GetWindowLongPtrW(hWnd, 0);
1380 if (win->font_scale != 1)
1382 win->font_scale = 1;
1383 WINHELP_SetupText(GetDlgItem(hWnd, CTL_ID_TEXT), win, 0 /* FIXME */);
1385 break;
1386 case MNID_OPTS_FONTS_LARGE:
1387 case MNID_CTXT_FONTS_LARGE:
1388 win = (WINHELP_WINDOW*) GetWindowLongPtrW(hWnd, 0);
1389 if (win->font_scale != 2)
1391 win->font_scale = 2;
1392 WINHELP_SetupText(GetDlgItem(hWnd, CTL_ID_TEXT), win, 0 /* FIXME */);
1394 break;
1396 default:
1397 /* Buttons */
1398 for (button = win->first_button; button; button = button->next)
1399 if (wParam == button->wParam) break;
1400 if (button)
1401 MACRO_ExecuteMacro(win, button->lpszMacro);
1402 else if (!HIWORD(wParam))
1403 MessageBoxW(0, MAKEINTRESOURCEW(STID_NOT_IMPLEMENTED),
1404 MAKEINTRESOURCEW(STID_WHERROR), MB_OK);
1405 break;
1407 break;
1408 /* EPP case WM_DESTROY: */
1409 /* EPP if (Globals.hPopupWnd) DestroyWindow(Globals.hPopupWnd); */
1410 /* EPP break; */
1411 case WM_COPYDATA:
1412 return WINHELP_HandleCommand((HWND)wParam, lParam);
1414 case WM_CHAR:
1415 if (wParam == 3)
1417 SendDlgItemMessageW(hWnd, CTL_ID_TEXT, WM_COPY, 0, 0);
1418 return 0;
1420 break;
1422 case WM_KEYDOWN:
1423 win = (WINHELP_WINDOW*) GetWindowLongPtrW(hWnd, 0);
1424 hTextWnd = GetDlgItem(win->hMainWnd, CTL_ID_TEXT);
1426 switch (wParam)
1428 case VK_UP:
1429 SendMessageW(hTextWnd, EM_SCROLL, SB_LINEUP, 0);
1430 return 0;
1431 case VK_DOWN:
1432 SendMessageW(hTextWnd, EM_SCROLL, SB_LINEDOWN, 0);
1433 return 0;
1434 case VK_PRIOR:
1435 SendMessageW(hTextWnd, EM_SCROLL, SB_PAGEUP, 0);
1436 return 0;
1437 case VK_NEXT:
1438 SendMessageW(hTextWnd, EM_SCROLL, SB_PAGEDOWN, 0);
1439 return 0;
1440 case VK_ESCAPE:
1441 MACRO_Exit();
1442 return 0;
1444 break;
1446 case WM_NOTIFY:
1447 if (wParam == CTL_ID_TEXT)
1449 RECT rc;
1451 switch (((NMHDR*)lParam)->code)
1453 case EN_MSGFILTER:
1455 const MSGFILTER* msgf = (const MSGFILTER*)lParam;
1456 switch (msgf->msg)
1458 case WM_KEYUP:
1459 if (msgf->wParam == VK_ESCAPE)
1460 WINHELP_ReleaseWindow((WINHELP_WINDOW*)GetWindowLongPtrW(hWnd, 0));
1461 break;
1462 case WM_RBUTTONDOWN:
1464 HMENU hMenu;
1465 POINT pt;
1467 win = (WINHELP_WINDOW*) GetWindowLongPtrW(hWnd, 0);
1468 hMenu = LoadMenuW(Globals.hInstance, MAKEINTRESOURCEW(CONTEXT_MENU));
1469 switch (win->font_scale)
1471 case 0:
1472 CheckMenuItem(hMenu, MNID_CTXT_FONTS_SMALL,
1473 MF_BYCOMMAND|MF_CHECKED);
1474 break;
1475 default:
1476 WINE_FIXME("Unsupported %d\n", win->font_scale);
1477 /* fall through */
1478 case 1:
1479 CheckMenuItem(hMenu, MNID_CTXT_FONTS_NORMAL,
1480 MF_BYCOMMAND|MF_CHECKED);
1481 break;
1482 case 2:
1483 CheckMenuItem(hMenu, MNID_CTXT_FONTS_LARGE,
1484 MF_BYCOMMAND|MF_CHECKED);
1485 break;
1487 pt.x = (int)(short)LOWORD(msgf->lParam);
1488 pt.y = (int)(short)HIWORD(msgf->lParam);
1489 ClientToScreen(msgf->nmhdr.hwndFrom, &pt);
1490 TrackPopupMenu(GetSubMenu(hMenu, 0), TPM_LEFTALIGN|TPM_TOPALIGN,
1491 pt.x, pt.y, 0, hWnd, NULL);
1492 DestroyMenu(hMenu);
1494 break;
1495 default:
1496 return WINHELP_HandleTextMouse((WINHELP_WINDOW*)GetWindowLongPtrW(hWnd, 0),
1497 msgf->msg, msgf->lParam);
1500 break;
1502 case EN_REQUESTRESIZE:
1503 rc = ((REQRESIZE*)lParam)->rc;
1504 win = (WINHELP_WINDOW*) GetWindowLongPtrW(hWnd, 0);
1505 AdjustWindowRect(&rc, GetWindowLongW(win->hMainWnd, GWL_STYLE),
1506 FALSE);
1507 SetWindowPos(win->hMainWnd, HWND_TOP, 0, 0,
1508 rc.right - rc.left, rc.bottom - rc.top,
1509 SWP_NOMOVE | SWP_NOZORDER);
1510 WINHELP_LayoutMainWindow(win);
1511 break;
1514 break;
1516 case WM_INITMENUPOPUP:
1517 win = (WINHELP_WINDOW*) GetWindowLongPtrW(hWnd, 0);
1518 CheckMenuItem((HMENU)wParam, MNID_OPTS_FONTS_SMALL,
1519 (win->font_scale == 0) ? MF_CHECKED : MF_UNCHECKED);
1520 CheckMenuItem((HMENU)wParam, MNID_OPTS_FONTS_NORMAL,
1521 (win->font_scale == 1) ? MF_CHECKED : MF_UNCHECKED);
1522 CheckMenuItem((HMENU)wParam, MNID_OPTS_FONTS_LARGE,
1523 (win->font_scale == 2) ? MF_CHECKED : MF_UNCHECKED);
1524 break;
1525 case WM_DESTROY:
1526 win = (WINHELP_WINDOW*) GetWindowLongPtrW(hWnd, 0);
1527 WINHELP_DeleteWindow(win);
1528 break;
1530 return DefWindowProcA(hWnd, msg, wParam, lParam);
1533 /**************************************************************************
1534 * WINHELP_CreateIndexWindow
1536 * Displays a dialog with keywords of current help file.
1539 BOOL WINHELP_CreateIndexWindow(BOOL is_search)
1541 HPROPSHEETPAGE psPage[3];
1542 PROPSHEETPAGEA psp;
1543 PROPSHEETHEADERA psHead;
1544 struct index_data id;
1545 char buf[256];
1547 if (Globals.active_win && Globals.active_win->page && Globals.active_win->page->file)
1548 id.hlpfile = Globals.active_win->page->file;
1549 else
1550 return FALSE;
1552 if (id.hlpfile->kwbtree == NULL)
1554 WINE_TRACE("No index provided\n");
1555 return FALSE;
1558 InitCommonControls();
1560 id.jump = FALSE;
1561 memset(&psp, 0, sizeof(psp));
1562 psp.dwSize = sizeof(psp);
1563 psp.dwFlags = 0;
1564 psp.hInstance = Globals.hInstance;
1566 psp.pszTemplate = MAKEINTRESOURCEA(IDD_INDEX);
1567 psp.lParam = (LPARAM)&id;
1568 psp.pfnDlgProc = WINHELP_IndexDlgProc;
1569 psPage[0] = CreatePropertySheetPageA(&psp);
1571 psp.pszTemplate = MAKEINTRESOURCEA(IDD_SEARCH);
1572 psp.lParam = (LPARAM)&id;
1573 psp.pfnDlgProc = WINHELP_SearchDlgProc;
1574 psPage[1] = CreatePropertySheetPageA(&psp);
1576 memset(&psHead, 0, sizeof(psHead));
1577 psHead.dwSize = sizeof(psHead);
1579 LoadStringA(Globals.hInstance, STID_PSH_INDEX, buf, sizeof(buf));
1580 strcat(buf, Globals.active_win->info->caption);
1582 psHead.pszCaption = buf;
1583 psHead.nPages = 2;
1584 psHead.nStartPage = is_search ? 1 : 0;
1585 psHead.hwndParent = Globals.active_win->hMainWnd;
1586 psHead.phpage = psPage;
1587 psHead.dwFlags = PSH_NOAPPLYNOW;
1589 PropertySheetA(&psHead);
1590 if (id.jump)
1592 WINE_TRACE("got %ld as an offset\n", id.offset);
1593 WINHELP_OpenHelpWindow(HLPFILE_PageByOffset, id.hlpfile, id.offset,
1594 Globals.active_win->info, SW_NORMAL);
1596 return TRUE;
1599 /***********************************************************************
1601 * RegisterWinClasses
1603 static BOOL WINHELP_RegisterWinClasses(void)
1605 WNDCLASSEXA class_main, class_button_box, class_history;
1607 class_main.cbSize = sizeof(class_main);
1608 class_main.style = CS_HREDRAW | CS_VREDRAW;
1609 class_main.lpfnWndProc = WINHELP_MainWndProc;
1610 class_main.cbClsExtra = 0;
1611 class_main.cbWndExtra = sizeof(WINHELP_WINDOW *);
1612 class_main.hInstance = Globals.hInstance;
1613 class_main.hIcon = LoadIconW(Globals.hInstance, MAKEINTRESOURCEW(IDI_WINHELP));
1614 class_main.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
1615 class_main.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
1616 class_main.lpszMenuName = 0;
1617 class_main.lpszClassName = MAIN_WIN_CLASS_NAME;
1618 class_main.hIconSm = LoadImageW(Globals.hInstance, MAKEINTRESOURCEW(IDI_WINHELP), IMAGE_ICON,
1619 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
1620 LR_SHARED);
1622 class_button_box = class_main;
1623 class_button_box.lpfnWndProc = WINHELP_ButtonBoxWndProc;
1624 class_button_box.cbWndExtra = 0;
1625 class_button_box.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
1626 class_button_box.lpszClassName = BUTTON_BOX_WIN_CLASS_NAME;
1628 class_history = class_main;
1629 class_history.lpfnWndProc = WINHELP_HistoryWndProc;
1630 class_history.lpszClassName = HISTORY_WIN_CLASS_NAME;
1632 return (RegisterClassExA(&class_main) &&
1633 RegisterClassExA(&class_button_box) &&
1634 RegisterClassExA(&class_history));
1637 /***********************************************************************
1639 * WinMain
1641 int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
1643 MSG msg;
1644 LONG lHash = 0;
1645 HLPFILE* hlpfile;
1646 static CHAR default_wndname[] = "main";
1647 LPSTR wndname = default_wndname;
1648 WINHELP_DLL* dll;
1649 HACCEL hAccel;
1651 Globals.hInstance = hInstance;
1653 if (LoadLibraryA("riched20.dll") == NULL)
1654 return MessageBoxW(0, MAKEINTRESOURCEW(STID_NO_RICHEDIT),
1655 MAKEINTRESOURCEW(STID_WHERROR), MB_OK);
1657 /* Get options */
1658 while (*cmdline && (*cmdline == ' ' || *cmdline == '-'))
1660 CHAR option;
1661 LPCSTR topic_id;
1662 if (*cmdline++ == ' ') continue;
1664 option = *cmdline;
1665 if (option) cmdline++;
1666 while (*cmdline == ' ') cmdline++;
1667 switch (option)
1669 case 'i':
1670 case 'I':
1671 topic_id = cmdline;
1672 while (*cmdline && *cmdline != ' ') cmdline++;
1673 if (*cmdline) *cmdline++ = '\0';
1674 lHash = HLPFILE_Hash(topic_id);
1675 break;
1677 case '3':
1678 case '4':
1679 Globals.wVersion = option - '0';
1680 break;
1682 case 'x':
1683 show = SW_HIDE;
1684 Globals.isBook = FALSE;
1685 break;
1687 default:
1688 WINE_FIXME("Unsupported cmd line: %s\n", debugstr_a(cmdline));
1689 break;
1693 /* Create primary window */
1694 if (!WINHELP_RegisterWinClasses())
1696 WINE_FIXME("Couldn't register classes\n");
1697 return 0;
1700 if (*cmdline)
1702 char* ptr;
1703 if ((*cmdline == '"') && (ptr = strchr(cmdline+1, '"')))
1705 cmdline++;
1706 *ptr = '\0';
1708 if ((ptr = strchr(cmdline, '>')))
1710 *ptr = '\0';
1711 wndname = ptr + 1;
1713 hlpfile = WINHELP_LookupHelpFile(cmdline);
1714 if (!hlpfile) return 0;
1716 else hlpfile = NULL;
1717 WINHELP_OpenHelpWindow(HLPFILE_PageByHash, hlpfile, lHash,
1718 WINHELP_GetWindowInfo(hlpfile, wndname), show);
1720 /* Message loop */
1721 hAccel = LoadAcceleratorsW(hInstance, MAKEINTRESOURCEW(MAIN_ACCEL));
1722 while ((Globals.win_list || Globals.active_popup) && GetMessageW(&msg, 0, 0, 0))
1724 HWND hWnd = Globals.active_win ? Globals.active_win->hMainWnd : NULL;
1725 if (!TranslateAcceleratorW(hWnd, hAccel, &msg))
1727 TranslateMessage(&msg);
1728 DispatchMessageW(&msg);
1731 for (dll = Globals.dlls; dll; dll = dll->next)
1733 if (dll->class & DC_INITTERM) dll->handler(DW_TERM, 0, 0);
1735 return 0;