push cc8bc80451cc24f4d7cf75168b569f0ebfe19547
[wine/hacks.git] / programs / winhlp32 / winhelp.c
blob08d3875aba1d12cb3d37cbb51d78baaeaeec3880
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 #define NONAMELESSUNION
32 #define NONAMELESSSTRUCT
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"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(winhelp);
49 WINHELP_GLOBALS Globals = {3, NULL, TRUE, NULL, NULL, NULL, NULL, NULL, {{{NULL,NULL}},0}, NULL};
51 #define CTL_ID_BUTTON 0x700
52 #define CTL_ID_TEXT 0x701
55 /***********************************************************************
57 * WINHELP_InitFonts
59 static void WINHELP_InitFonts(HWND hWnd)
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))
72 static HFONT fonts[FONTS_LEN];
73 static BOOL init = 0;
75 win->fonts_len = FONTS_LEN;
76 win->fonts = fonts;
78 if (!init)
80 UINT i;
82 for (i = 0; i < FONTS_LEN; i++)
84 fonts[i] = CreateFontIndirect(&logfontlist[i]);
87 init = 1;
91 static DWORD CALLBACK WINHELP_RtfStreamIn(DWORD_PTR cookie, BYTE* buff,
92 LONG cb, LONG* pcb)
94 struct RtfData* rd = (struct RtfData*)cookie;
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;
105 static void WINHELP_SetupText(HWND hTextWnd, WINHELP_WINDOW* win, ULONG relative)
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)
115 struct RtfData rd;
116 EDITSTREAM es;
117 unsigned cp = 0;
118 POINTL ptl;
119 POINT pt;
122 if (HLPFILE_BrowsePage(win->page, &rd, win->font_scale, relative))
124 rd.where = rd.data;
125 es.dwCookie = (DWORD_PTR)&rd;
126 es.dwError = 0;
127 es.pfnCallback = WINHELP_RtfStreamIn;
129 SendMessageW(hTextWnd, EM_STREAMIN, SF_RTF, (LPARAM)&es);
130 cp = rd.char_pos_rel;
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);
138 SendMessage(hTextWnd, WM_SETREDRAW, TRUE, 0);
139 RedrawWindow(hTextWnd, NULL, NULL, RDW_FRAME|RDW_INVALIDATE);
142 /***********************************************************************
144 * WINHELP_GetOpenFileName
146 BOOL WINHELP_GetOpenFileName(LPSTR lpszFile, int len)
148 OPENFILENAME openfilename;
149 CHAR szDir[MAX_PATH];
150 CHAR szzFilter[2 * MAX_STRING_LEN + 100];
151 LPSTR p = szzFilter;
153 WINE_TRACE("()\n");
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';
165 GetCurrentDirectory(sizeof(szDir), szDir);
167 lpszFile[0]='\0';
169 openfilename.lStructSize = sizeof(OPENFILENAME);
170 openfilename.hwndOwner = NULL;
171 openfilename.hInstance = Globals.hInstance;
172 openfilename.lpstrFilter = szzFilter;
173 openfilename.lpstrCustomFilter = 0;
174 openfilename.nMaxCustFilter = 0;
175 openfilename.nFilterIndex = 1;
176 openfilename.lpstrFile = lpszFile;
177 openfilename.nMaxFile = len;
178 openfilename.lpstrFileTitle = 0;
179 openfilename.nMaxFileTitle = 0;
180 openfilename.lpstrInitialDir = szDir;
181 openfilename.lpstrTitle = 0;
182 openfilename.Flags = OFN_ENABLESIZING;
183 openfilename.nFileOffset = 0;
184 openfilename.nFileExtension = 0;
185 openfilename.lpstrDefExt = 0;
186 openfilename.lCustData = 0;
187 openfilename.lpfnHook = 0;
188 openfilename.lpTemplateName = 0;
190 return GetOpenFileName(&openfilename);
193 /***********************************************************************
195 * WINHELP_MessageBoxIDS_s
197 static INT WINHELP_MessageBoxIDS_s(UINT ids_text, LPCSTR str, UINT ids_title, WORD type)
199 CHAR text[MAX_STRING_LEN];
200 CHAR newtext[MAX_STRING_LEN + MAX_PATH];
202 LoadString(Globals.hInstance, ids_text, text, sizeof(text));
203 wsprintf(newtext, text, str);
205 return MessageBox(0, newtext, MAKEINTRESOURCE(ids_title), type);
208 /***********************************************************************
210 * WINHELP_LookupHelpFile
212 HLPFILE* WINHELP_LookupHelpFile(LPCSTR lpszFile)
214 HLPFILE* hlpfile;
215 char szFullName[MAX_PATH];
216 char szAddPath[MAX_PATH];
217 char *p;
220 * NOTE: This is needed by popup windows only.
221 * In other cases it's not needed but does not hurt though.
223 if (Globals.active_win && Globals.active_win->page && Globals.active_win->page->file)
225 strcpy(szAddPath, Globals.active_win->page->file->lpszPath);
226 p = strrchr(szAddPath, '\\');
227 if (p) *p = 0;
231 * FIXME: Should we swap conditions?
233 if (!SearchPath(NULL, lpszFile, ".hlp", MAX_PATH, szFullName, NULL) &&
234 !SearchPath(szAddPath, lpszFile, ".hlp", MAX_PATH, szFullName, NULL))
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;
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;
249 /******************************************************************
250 * WINHELP_GetWindowInfo
254 HLPFILE_WINDOWINFO* WINHELP_GetWindowInfo(HLPFILE* hlpfile, LPCSTR name)
256 static HLPFILE_WINDOWINFO mwi;
257 unsigned int i;
259 if (!name || !name[0])
260 name = Globals.active_win->info->name;
262 if (hlpfile)
263 for (i = 0; i < hlpfile->numWindows; i++)
264 if (!lstrcmpi(hlpfile->windows[i].name, name))
265 return &hlpfile->windows[i];
267 if (strcmp(name, "main") != 0)
269 WINE_FIXME("Couldn't find window info for %s\n", name);
270 assert(0);
271 return NULL;
273 if (!mwi.name[0])
275 strcpy(mwi.type, "primary");
276 strcpy(mwi.name, "main");
277 if (hlpfile && hlpfile->lpszTitle[0])
279 char tmp[128];
280 LoadString(Globals.hInstance, STID_WINE_HELP, tmp, sizeof(tmp));
281 snprintf(mwi.caption, sizeof(mwi.caption), "%s %s - %s",
282 hlpfile->lpszTitle, tmp, hlpfile->lpszPath);
284 else
285 LoadString(Globals.hInstance, STID_WINE_HELP, mwi.caption, sizeof(mwi.caption));
286 mwi.origin.x = mwi.origin.y = mwi.size.cx = mwi.size.cy = CW_USEDEFAULT;
287 mwi.style = SW_SHOW;
288 mwi.win_style = WS_OVERLAPPEDWINDOW;
289 mwi.sr_color = mwi.nsr_color = 0xFFFFFF;
291 return &mwi;
294 /******************************************************************
295 * HLPFILE_GetPopupWindowInfo
299 static HLPFILE_WINDOWINFO* WINHELP_GetPopupWindowInfo(HLPFILE* hlpfile,
300 WINHELP_WINDOW* parent, LPARAM mouse)
302 static HLPFILE_WINDOWINFO wi;
304 RECT parent_rect;
306 wi.type[0] = wi.name[0] = wi.caption[0] = '\0';
308 /* Calculate horizontal size and position of a popup window */
309 GetWindowRect(parent->hMainWnd, &parent_rect);
310 wi.size.cx = (parent_rect.right - parent_rect.left) / 2;
311 wi.size.cy = 10; /* need a non null value, so that border are taken into account while computing */
313 wi.origin.x = (short)LOWORD(mouse);
314 wi.origin.y = (short)HIWORD(mouse);
315 ClientToScreen(parent->hMainWnd, &wi.origin);
316 wi.origin.x -= wi.size.cx / 2;
317 wi.origin.x = min(wi.origin.x, GetSystemMetrics(SM_CXSCREEN) - wi.size.cx);
318 wi.origin.x = max(wi.origin.x, 0);
320 wi.style = SW_SHOW;
321 wi.win_style = WS_POPUP | WS_BORDER;
322 if (parent->page->file->has_popup_color)
323 wi.sr_color = parent->page->file->popup_color;
324 else
325 wi.sr_color = parent->info->sr_color;
326 wi.nsr_color = 0xFFFFFF;
328 return &wi;
331 typedef struct
333 WORD size;
334 WORD command;
335 LONG data;
336 LONG reserved;
337 WORD ofsFilename;
338 WORD ofsData;
339 } WINHELP,*LPWINHELP;
341 static BOOL WINHELP_HasWorkingWindow(void)
343 if (!Globals.active_win) return FALSE;
344 if (Globals.active_win->next || Globals.win_list != Globals.active_win) return TRUE;
345 return Globals.active_win->page != NULL && Globals.active_win->page->file != NULL;
348 /******************************************************************
349 * WINHELP_HandleCommand
353 static LRESULT WINHELP_HandleCommand(HWND hSrcWnd, LPARAM lParam)
355 COPYDATASTRUCT* cds = (COPYDATASTRUCT*)lParam;
356 WINHELP* wh;
358 if (cds->dwData != 0xA1DE505)
360 WINE_FIXME("Wrong magic number (%08lx)\n", cds->dwData);
361 return 0;
364 wh = cds->lpData;
366 if (wh)
368 char* ptr = (wh->ofsFilename) ? (LPSTR)wh + wh->ofsFilename : NULL;
370 WINE_TRACE("Got[%u]: cmd=%u data=%08x fn=%s\n",
371 wh->size, wh->command, wh->data, ptr);
372 switch (wh->command)
374 case HELP_CONTEXT:
375 if (ptr)
377 MACRO_JumpContext(ptr, "main", wh->data);
379 if (!WINHELP_HasWorkingWindow()) MACRO_Exit();
380 break;
381 case HELP_QUIT:
382 MACRO_Exit();
383 break;
384 case HELP_CONTENTS:
385 if (ptr)
387 MACRO_JumpContents(ptr, "main");
389 if (!WINHELP_HasWorkingWindow()) MACRO_Exit();
390 break;
391 case HELP_HELPONHELP:
392 MACRO_HelpOn();
393 if (!WINHELP_HasWorkingWindow()) MACRO_Exit();
394 break;
395 /* case HELP_SETINDEX: */
396 case HELP_SETCONTENTS:
397 if (ptr)
399 MACRO_SetContents(ptr, wh->data);
401 break;
402 case HELP_CONTEXTPOPUP:
403 if (ptr)
405 MACRO_PopupContext(ptr, wh->data);
407 break;
408 /* case HELP_FORCEFILE:*/
409 /* case HELP_CONTEXTMENU: */
410 case HELP_FINDER:
411 /* in fact, should be the topic dialog box */
412 WINE_FIXME("HELP_FINDER: stub\n");
413 if (ptr)
415 MACRO_JumpHash(ptr, "main", 0);
417 break;
418 /* case HELP_WM_HELP: */
419 /* case HELP_SETPOPUP_POS: */
420 /* case HELP_KEY: */
421 /* case HELP_COMMAND: */
422 /* case HELP_PARTIALKEY: */
423 /* case HELP_MULTIKEY: */
424 /* case HELP_SETWINPOS: */
425 default:
426 WINE_FIXME("Unhandled command (%x) for remote winhelp control\n", wh->command);
427 break;
430 /* Always return success for now */
431 return 1;
434 void WINHELP_LayoutMainWindow(WINHELP_WINDOW* win)
436 RECT rect, button_box_rect;
437 INT text_top = 0;
438 HWND hButtonBoxWnd = GetDlgItem(win->hMainWnd, CTL_ID_BUTTON);
439 HWND hTextWnd = GetDlgItem(win->hMainWnd, CTL_ID_TEXT);
441 GetClientRect(win->hMainWnd, &rect);
443 /* Update button box and text Window */
444 SetWindowPos(hButtonBoxWnd, HWND_TOP,
445 rect.left, rect.top,
446 rect.right - rect.left,
447 rect.bottom - rect.top, 0);
449 if (GetWindowRect(hButtonBoxWnd, &button_box_rect))
450 text_top = rect.top + button_box_rect.bottom - button_box_rect.top;
452 SetWindowPos(hTextWnd, HWND_TOP,
453 rect.left, text_top,
454 rect.right - rect.left,
455 rect.bottom - text_top, 0);
459 /******************************************************************
460 * WINHELP_DeleteButtons
463 static void WINHELP_DeleteButtons(WINHELP_WINDOW* win)
465 WINHELP_BUTTON* b;
466 WINHELP_BUTTON* bp;
468 for (b = win->first_button; b; b = bp)
470 DestroyWindow(b->hWnd);
471 bp = b->next;
472 HeapFree(GetProcessHeap(), 0, b);
474 win->first_button = NULL;
477 /******************************************************************
478 * WINHELP_DeleteBackSet
481 void WINHELP_DeleteBackSet(WINHELP_WINDOW* win)
483 unsigned int i;
485 for (i = 0; i < win->back.index; i++)
487 HLPFILE_FreeHlpFile(win->back.set[i].page->file);
488 win->back.set[i].page = NULL;
490 win->back.index = 0;
493 /******************************************************************
494 * WINHELP_DeletePageLinks
497 static void WINHELP_DeletePageLinks(HLPFILE_PAGE* page)
499 HLPFILE_LINK* curr;
500 HLPFILE_LINK* next;
502 for (curr = page->first_link; curr; curr = next)
504 next = curr->next;
505 HeapFree(GetProcessHeap(), 0, curr);
509 /***********************************************************************
511 * WINHELP_GrabWindow
513 WINHELP_WINDOW* WINHELP_GrabWindow(WINHELP_WINDOW* win)
515 WINE_TRACE("Grab %p#%d++\n", win, win->ref_count);
516 win->ref_count++;
517 return win;
520 /***********************************************************************
522 * WINHELP_RelaseWindow
524 BOOL WINHELP_ReleaseWindow(WINHELP_WINDOW* win)
526 WINE_TRACE("Release %p#%d--\n", win, win->ref_count);
528 if (!--win->ref_count)
530 DestroyWindow(win->hMainWnd);
531 return FALSE;
533 return TRUE;
536 /***********************************************************************
538 * WINHELP_DeleteWindow
540 static void WINHELP_DeleteWindow(WINHELP_WINDOW* win)
542 WINHELP_WINDOW** w;
543 BOOL bExit;
545 for (w = &Globals.win_list; *w; w = &(*w)->next)
547 if (*w == win)
549 *w = win->next;
550 break;
553 bExit = (Globals.wVersion >= 4 && !lstrcmpi(win->info->name, "main"));
555 if (Globals.active_win == win)
557 Globals.active_win = Globals.win_list;
558 if (Globals.win_list)
559 SetActiveWindow(Globals.win_list->hMainWnd);
562 if (win == Globals.active_popup)
563 Globals.active_popup = NULL;
565 WINHELP_DeleteButtons(win);
567 if (win->page) WINHELP_DeletePageLinks(win->page);
568 if (win->hShadowWnd) DestroyWindow(win->hShadowWnd);
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 HeapFree(GetProcessHeap(), 0, 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 = sizeof(Globals.history.set) / sizeof(Globals.history.set[0]);
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 = sizeof(win->back.set) / sizeof(win->back.set[0]);
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 (char_next_ptl.y != char_ptl.y || mouse_ptl.x >= char_next_ptl.x)
650 link = NULL;
651 break;
654 return link;
657 static LRESULT CALLBACK WINHELP_RicheditWndProc(HWND hWnd, UINT msg,
658 WPARAM wParam, LPARAM lParam)
660 WINHELP_WINDOW *win = (WINHELP_WINDOW*) GetWindowLongPtr(GetParent(hWnd), 0);
661 DWORD messagePos;
662 POINT pt;
663 switch(msg)
665 case WM_SETCURSOR:
666 messagePos = GetMessagePos();
667 pt.x = (short)LOWORD(messagePos);
668 pt.y = (short)HIWORD(messagePos);
669 ScreenToClient(hWnd, &pt);
670 if (win->page && WINHELP_FindLink(win, MAKELPARAM(pt.x, pt.y)))
672 SetCursor(win->hHandCur);
673 return 0;
675 /* fall through */
676 default:
677 return CallWindowProcA(win->origRicheditWndProc, hWnd, msg, wParam, lParam);
681 /***********************************************************************
683 * WINHELP_CreateHelpWindow
685 BOOL WINHELP_CreateHelpWindow(WINHELP_WNDPAGE* wpage, int nCmdShow, BOOL remember)
687 WINHELP_WINDOW* win = NULL;
688 BOOL bPrimary, bPopup, bReUsed = FALSE;
689 HICON hIcon;
690 HWND hTextWnd = NULL;
692 bPrimary = !lstrcmpi(wpage->wininfo->name, "main");
693 bPopup = !bPrimary && (wpage->wininfo->win_style & WS_POPUP);
695 if (!bPopup)
697 for (win = Globals.win_list; win; win = win->next)
699 if (!lstrcmpi(win->info->name, wpage->wininfo->name))
701 POINT pt = {0, 0};
702 SIZE sz = {0, 0};
703 DWORD flags = SWP_NOSIZE | SWP_NOMOVE;
705 WINHELP_DeleteButtons(win);
706 bReUsed = TRUE;
707 SetWindowText(win->hMainWnd, WINHELP_GetCaption(wpage));
708 if (wpage->wininfo->origin.x != CW_USEDEFAULT &&
709 wpage->wininfo->origin.y != CW_USEDEFAULT)
711 pt = wpage->wininfo->origin;
712 flags &= ~SWP_NOSIZE;
714 if (wpage->wininfo->size.cx != CW_USEDEFAULT &&
715 wpage->wininfo->size.cy != CW_USEDEFAULT)
717 sz = wpage->wininfo->size;
718 flags &= ~SWP_NOMOVE;
720 SetWindowPos(win->hMainWnd, HWND_TOP, pt.x, pt.y, sz.cx, sz.cy, flags);
722 if (wpage->page && win->page && wpage->page->file != win->page->file)
723 WINHELP_DeleteBackSet(win);
724 WINHELP_InitFonts(win->hMainWnd);
726 win->page = wpage->page;
727 win->info = wpage->wininfo;
728 hTextWnd = GetDlgItem(win->hMainWnd, CTL_ID_TEXT);
729 WINHELP_SetupText(hTextWnd, win, wpage->relative);
731 InvalidateRect(win->hMainWnd, NULL, TRUE);
732 if (win->hHistoryWnd) InvalidateRect(win->hHistoryWnd, NULL, TRUE);
734 break;
739 if (!win)
741 /* Initialize WINHELP_WINDOW struct */
742 win = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINHELP_WINDOW));
743 if (!win) return FALSE;
744 win->next = Globals.win_list;
745 Globals.win_list = win;
747 win->hHandCur = LoadCursorW(0, (LPWSTR)IDC_HAND);
748 win->back.index = 0;
749 win->font_scale = 1;
750 WINHELP_GrabWindow(win);
752 win->page = wpage->page;
753 win->info = wpage->wininfo;
754 WINHELP_GrabWindow(win);
756 if (!bPopup && wpage->page && remember)
758 WINHELP_RememberPage(win, wpage);
761 if (bPopup)
762 Globals.active_popup = win;
763 else
764 Globals.active_win = win;
766 /* Initialize default pushbuttons */
767 if (bPrimary && wpage->page)
769 CHAR buffer[MAX_STRING_LEN];
771 LoadString(Globals.hInstance, STID_CONTENTS, buffer, sizeof(buffer));
772 MACRO_CreateButton("BTN_CONTENTS", buffer, "Contents()");
773 LoadString(Globals.hInstance, STID_INDEX, buffer, sizeof(buffer));
774 MACRO_CreateButton("BTN_INDEX", buffer, "Finder()");
775 LoadString(Globals.hInstance, STID_BACK, buffer, sizeof(buffer));
776 MACRO_CreateButton("BTN_BACK", buffer, "Back()");
777 if (win->back.index <= 1) MACRO_DisableButton("BTN_BACK");
780 if (!bReUsed)
782 win->hMainWnd = CreateWindowEx((bPopup) ? WS_EX_TOOLWINDOW : 0, MAIN_WIN_CLASS_NAME,
783 WINHELP_GetCaption(wpage),
784 bPrimary ? WS_OVERLAPPEDWINDOW : wpage->wininfo->win_style,
785 wpage->wininfo->origin.x, wpage->wininfo->origin.y,
786 wpage->wininfo->size.cx, wpage->wininfo->size.cy,
787 bPopup ? Globals.active_win->hMainWnd : NULL,
788 bPrimary ? LoadMenu(Globals.hInstance, MAKEINTRESOURCE(MAIN_MENU)) : 0,
789 Globals.hInstance, win);
790 if (!bPopup)
791 /* Create button box and text Window */
792 CreateWindow(BUTTON_BOX_WIN_CLASS_NAME, "", WS_CHILD | WS_VISIBLE,
793 0, 0, 0, 0, win->hMainWnd, (HMENU)CTL_ID_BUTTON, Globals.hInstance, NULL);
795 hTextWnd = CreateWindow(RICHEDIT_CLASS, NULL,
796 ES_MULTILINE | ES_READONLY | WS_CHILD | WS_HSCROLL | WS_VSCROLL | WS_VISIBLE,
797 0, 0, 0, 0, win->hMainWnd, (HMENU)CTL_ID_TEXT, Globals.hInstance, NULL);
798 SendMessage(hTextWnd, EM_SETEVENTMASK, 0,
799 SendMessage(hTextWnd, EM_GETEVENTMASK, 0, 0) | ENM_MOUSEEVENTS);
800 win->origRicheditWndProc = (WNDPROC)SetWindowLongPtr(hTextWnd, GWLP_WNDPROC,
801 (LONG_PTR)WINHELP_RicheditWndProc);
804 hIcon = (wpage->page) ? wpage->page->file->hIcon : NULL;
805 if (!hIcon) hIcon = LoadIcon(Globals.hInstance, MAKEINTRESOURCE(IDI_WINHELP));
806 SendMessage(win->hMainWnd, WM_SETICON, ICON_SMALL, (DWORD_PTR)hIcon);
808 /* Initialize file specific pushbuttons */
809 if (!(wpage->wininfo->win_style & WS_POPUP) && wpage->page)
811 HLPFILE_MACRO *macro;
812 for (macro = wpage->page->file->first_macro; macro; macro = macro->next)
813 MACRO_ExecuteMacro(win, macro->lpszMacro);
815 for (macro = wpage->page->first_macro; macro; macro = macro->next)
816 MACRO_ExecuteMacro(win, macro->lpszMacro);
818 /* See #17681, in some cases, the newly created window is closed by the macros it contains
819 * (braindead), so deal with this case
821 for (win = Globals.win_list; win; win = win->next)
823 if (!lstrcmpi(win->info->name, wpage->wininfo->name)) break;
825 if (!win || !WINHELP_ReleaseWindow(win)) return TRUE;
827 if (bPopup)
829 DWORD mask = SendMessage(hTextWnd, EM_GETEVENTMASK, 0, 0);
830 RECT rect;
832 win->font_scale = Globals.active_win->font_scale;
833 WINHELP_SetupText(hTextWnd, win, wpage->relative);
835 /* we need the window to be shown for richedit to compute the size */
836 ShowWindow(win->hMainWnd, nCmdShow);
837 SendMessage(hTextWnd, EM_SETEVENTMASK, 0, mask | ENM_REQUESTRESIZE);
838 SendMessage(hTextWnd, EM_REQUESTRESIZE, 0, 0);
839 SendMessage(hTextWnd, EM_SETEVENTMASK, 0, mask);
841 GetWindowRect(win->hMainWnd, &rect);
842 win->hShadowWnd = CreateWindowEx(WS_EX_TOOLWINDOW, SHADOW_WIN_CLASS_NAME,
843 "", WS_POPUP | WS_VISIBLE,
844 rect.left + SHADOW_DX, rect.top + SHADOW_DY,
845 rect.right - rect.left,
846 rect.bottom - rect.top,
847 Globals.active_win->hMainWnd, 0,
848 Globals.hInstance, NULL);
849 SetWindowPos(win->hMainWnd, win->hShadowWnd, 0, 0, 0, 0,
850 SWP_NOSIZE | SWP_NOMOVE);
852 else
854 WINHELP_SetupText(hTextWnd, win, wpage->relative);
855 WINHELP_LayoutMainWindow(win);
856 ShowWindow(win->hMainWnd, nCmdShow);
859 return TRUE;
862 /******************************************************************
863 * WINHELP_OpenHelpWindow
864 * Main function to search for a page and display it in a window
866 BOOL WINHELP_OpenHelpWindow(HLPFILE_PAGE* (*lookup)(HLPFILE*, LONG, ULONG*),
867 HLPFILE* hlpfile, LONG val, HLPFILE_WINDOWINFO* wi,
868 int nCmdShow)
870 WINHELP_WNDPAGE wpage;
872 wpage.page = lookup(hlpfile, val, &wpage.relative);
873 if (wpage.page) wpage.page->file->wRefCount++;
874 wpage.wininfo = wi;
875 return WINHELP_CreateHelpWindow(&wpage, nCmdShow, TRUE);
878 /******************************************************************
879 * WINHELP_HandleTextMouse
882 static BOOL WINHELP_HandleTextMouse(WINHELP_WINDOW* win, UINT msg, LPARAM lParam)
884 HLPFILE* hlpfile;
885 HLPFILE_LINK* link;
886 BOOL ret = FALSE;
888 switch (msg)
890 case WM_LBUTTONDOWN:
891 if ((link = WINHELP_FindLink(win, lParam)))
893 HLPFILE_WINDOWINFO* wi;
895 switch (link->cookie)
897 case hlp_link_link:
898 if ((hlpfile = WINHELP_LookupHelpFile(link->string)))
900 if (link->window == -1)
901 wi = win->info;
902 else if (link->window < hlpfile->numWindows)
903 wi = &hlpfile->windows[link->window];
904 else
906 WINE_WARN("link to window %d/%d\n", link->window, hlpfile->numWindows);
907 break;
909 WINHELP_OpenHelpWindow(HLPFILE_PageByHash, hlpfile, link->hash, wi, SW_NORMAL);
911 break;
912 case hlp_link_popup:
913 if ((hlpfile = WINHELP_LookupHelpFile(link->string)))
914 WINHELP_OpenHelpWindow(HLPFILE_PageByHash, hlpfile, link->hash,
915 WINHELP_GetPopupWindowInfo(hlpfile, win, lParam),
916 SW_NORMAL);
917 break;
918 case hlp_link_macro:
919 MACRO_ExecuteMacro(win, link->string);
920 break;
921 default:
922 WINE_FIXME("Unknown link cookie %d\n", link->cookie);
924 ret = TRUE;
926 break;
928 return ret;
931 /***********************************************************************
933 * WINHELP_CheckPopup
935 static BOOL WINHELP_CheckPopup(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, LRESULT* lret)
937 WINHELP_WINDOW* popup;
939 if (!Globals.active_popup) return FALSE;
941 switch (msg)
943 case WM_NOTIFY:
945 MSGFILTER* msgf = (MSGFILTER*)lParam;
946 if (msgf->nmhdr.code == EN_MSGFILTER)
948 if (!WINHELP_CheckPopup(hWnd, msgf->msg, msgf->wParam, msgf->lParam, NULL))
949 return FALSE;
950 if (lret) *lret = 1;
951 return TRUE;
954 break;
955 case WM_ACTIVATE:
956 if (wParam != WA_INACTIVE || (HWND)lParam == Globals.active_win->hMainWnd ||
957 (HWND)lParam == Globals.active_popup->hMainWnd ||
958 GetWindow((HWND)lParam, GW_OWNER) == Globals.active_win->hMainWnd)
959 break;
960 case WM_LBUTTONDOWN:
961 if (WINHELP_HandleTextMouse(Globals.active_popup, msg, lParam))
962 return FALSE;
963 /* fall through */
964 case WM_LBUTTONUP:
965 case WM_MBUTTONDOWN:
966 case WM_RBUTTONDOWN:
967 case WM_NCLBUTTONDOWN:
968 case WM_NCMBUTTONDOWN:
969 case WM_NCRBUTTONDOWN:
970 popup = Globals.active_popup;
971 Globals.active_popup = NULL;
972 WINHELP_ReleaseWindow(popup);
973 return TRUE;
975 return FALSE;
978 /***********************************************************************
980 * WINHELP_ButtonWndProc
982 static LRESULT CALLBACK WINHELP_ButtonWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
984 if (WINHELP_CheckPopup(hWnd, msg, wParam, lParam, NULL)) return 0;
986 if (msg == WM_KEYDOWN)
988 switch (wParam)
990 case VK_UP:
991 case VK_DOWN:
992 case VK_PRIOR:
993 case VK_NEXT:
994 case VK_ESCAPE:
995 return SendMessage(GetParent(hWnd), msg, wParam, lParam);
999 return CallWindowProc(Globals.button_proc, hWnd, msg, wParam, lParam);
1002 /***********************************************************************
1004 * WINHELP_ButtonBoxWndProc
1006 static LRESULT CALLBACK WINHELP_ButtonBoxWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1008 WINDOWPOS *winpos;
1009 WINHELP_WINDOW *win;
1010 WINHELP_BUTTON *button;
1011 SIZE button_size;
1012 INT x, y;
1014 if (WINHELP_CheckPopup(hWnd, msg, wParam, lParam, NULL)) return 0L;
1016 switch (msg)
1018 case WM_WINDOWPOSCHANGING:
1019 winpos = (WINDOWPOS*) lParam;
1020 win = (WINHELP_WINDOW*) GetWindowLongPtr(GetParent(hWnd), 0);
1022 /* Update buttons */
1023 button_size.cx = 0;
1024 button_size.cy = 0;
1025 for (button = win->first_button; button; button = button->next)
1027 HDC hDc;
1028 SIZE textsize;
1029 if (!button->hWnd)
1031 button->hWnd = CreateWindow(STRING_BUTTON, button->lpszName,
1032 WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
1033 0, 0, 0, 0,
1034 hWnd, (HMENU) button->wParam,
1035 Globals.hInstance, 0);
1036 if (button->hWnd)
1038 if (Globals.button_proc == NULL)
1040 NONCLIENTMETRICSW ncm;
1041 Globals.button_proc = (WNDPROC) GetWindowLongPtr(button->hWnd, GWLP_WNDPROC);
1043 ncm.cbSize = sizeof(NONCLIENTMETRICSW);
1044 SystemParametersInfoW(SPI_GETNONCLIENTMETRICS,
1045 sizeof(NONCLIENTMETRICSW), &ncm, 0);
1046 Globals.hButtonFont = CreateFontIndirectW(&ncm.lfMenuFont);
1048 SetWindowLongPtr(button->hWnd, GWLP_WNDPROC, (LONG_PTR) WINHELP_ButtonWndProc);
1049 if (Globals.hButtonFont)
1050 SendMessage(button->hWnd, WM_SETFONT, (WPARAM)Globals.hButtonFont, TRUE);
1053 hDc = GetDC(button->hWnd);
1054 GetTextExtentPoint(hDc, button->lpszName,
1055 lstrlen(button->lpszName), &textsize);
1056 ReleaseDC(button->hWnd, hDc);
1058 button_size.cx = max(button_size.cx, textsize.cx + BUTTON_CX);
1059 button_size.cy = max(button_size.cy, textsize.cy + BUTTON_CY);
1062 x = 0;
1063 y = 0;
1064 for (button = win->first_button; button; button = button->next)
1066 SetWindowPos(button->hWnd, HWND_TOP, x, y, button_size.cx, button_size.cy, 0);
1068 if (x + 2 * button_size.cx <= winpos->cx)
1069 x += button_size.cx;
1070 else
1071 x = 0, y += button_size.cy;
1073 winpos->cy = y + (x ? button_size.cy : 0);
1074 break;
1076 case WM_COMMAND:
1077 SendMessage(GetParent(hWnd), msg, wParam, lParam);
1078 break;
1080 case WM_KEYDOWN:
1081 switch (wParam)
1083 case VK_UP:
1084 case VK_DOWN:
1085 case VK_PRIOR:
1086 case VK_NEXT:
1087 case VK_ESCAPE:
1088 return SendMessage(GetParent(hWnd), msg, wParam, lParam);
1090 break;
1093 return DefWindowProc(hWnd, msg, wParam, lParam);
1096 /******************************************************************
1097 * WINHELP_HistoryWndProc
1101 static LRESULT CALLBACK WINHELP_HistoryWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1103 WINHELP_WINDOW* win;
1104 PAINTSTRUCT ps;
1105 HDC hDc;
1106 TEXTMETRIC tm;
1107 unsigned int i;
1108 RECT r;
1110 switch (msg)
1112 case WM_NCCREATE:
1113 win = (WINHELP_WINDOW*)((LPCREATESTRUCT)lParam)->lpCreateParams;
1114 SetWindowLongPtr(hWnd, 0, (ULONG_PTR)win);
1115 win->hHistoryWnd = hWnd;
1116 break;
1117 case WM_CREATE:
1118 win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1119 hDc = GetDC(hWnd);
1120 GetTextMetrics(hDc, &tm);
1121 GetWindowRect(hWnd, &r);
1123 r.right = r.left + 30 * tm.tmAveCharWidth;
1124 r.bottom = r.top + (sizeof(Globals.history.set) / sizeof(Globals.history.set[0])) * tm.tmHeight;
1125 AdjustWindowRect(&r, GetWindowLong(hWnd, GWL_STYLE), FALSE);
1126 if (r.left < 0) {r.right -= r.left; r.left = 0;}
1127 if (r.top < 0) {r.bottom -= r.top; r.top = 0;}
1129 MoveWindow(hWnd, r.left, r.top, r.right, r.bottom, TRUE);
1130 ReleaseDC(hWnd, hDc);
1131 break;
1132 case WM_LBUTTONDOWN:
1133 win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1134 hDc = GetDC(hWnd);
1135 GetTextMetrics(hDc, &tm);
1136 i = HIWORD(lParam) / tm.tmHeight;
1137 if (i < Globals.history.index)
1138 WINHELP_CreateHelpWindow(&Globals.history.set[i], SW_SHOW, TRUE);
1139 ReleaseDC(hWnd, hDc);
1140 break;
1141 case WM_PAINT:
1142 hDc = BeginPaint(hWnd, &ps);
1143 win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1144 GetTextMetrics(hDc, &tm);
1146 for (i = 0; i < Globals.history.index; i++)
1148 if (Globals.history.set[i].page->file == Globals.active_win->page->file)
1150 TextOut(hDc, 0, i * tm.tmHeight,
1151 Globals.history.set[i].page->lpszTitle,
1152 strlen(Globals.history.set[i].page->lpszTitle));
1154 else
1156 char buffer[1024];
1157 const char* ptr1;
1158 const char* ptr2;
1159 unsigned len;
1161 ptr1 = strrchr(Globals.history.set[i].page->file->lpszPath, '\\');
1162 if (!ptr1) ptr1 = Globals.history.set[i].page->file->lpszPath;
1163 else ptr1++;
1164 ptr2 = strrchr(ptr1, '.');
1165 len = ptr2 ? ptr2 - ptr1 : strlen(ptr1);
1166 if (len > sizeof(buffer)) len = sizeof(buffer);
1167 memcpy(buffer, ptr1, len);
1168 if (len < sizeof(buffer)) buffer[len++] = ':';
1169 strncpy(&buffer[len], Globals.history.set[i].page->lpszTitle, sizeof(buffer) - len);
1170 buffer[sizeof(buffer) - 1] = '\0';
1171 TextOut(hDc, 0, i * tm.tmHeight, buffer, strlen(buffer));
1174 EndPaint(hWnd, &ps);
1175 break;
1176 case WM_DESTROY:
1177 win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1178 if (hWnd == win->hHistoryWnd)
1179 win->hHistoryWnd = 0;
1180 break;
1182 return DefWindowProc(hWnd, msg, wParam, lParam);
1185 /***********************************************************************
1187 * WINHELP_ShadowWndProc
1189 static LRESULT CALLBACK WINHELP_ShadowWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1191 if (WINHELP_CheckPopup(hWnd, msg, wParam, lParam, NULL)) return 0;
1192 return WINHELP_CheckPopup(hWnd, msg, wParam, lParam, NULL) ? 0L : DefWindowProc(hWnd, msg, wParam, lParam);
1195 /**************************************************************************
1196 * cb_KWBTree
1198 * HLPFILE_BPTreeCallback enumeration function for '|KWBTREE' internal file.
1201 static void cb_KWBTree(void *p, void **next, void *cookie)
1203 HWND hListWnd = cookie;
1204 int count;
1206 WINE_TRACE("Adding '%s' to search list\n", (char *)p);
1207 SendMessage(hListWnd, LB_INSERTSTRING, -1, (LPARAM)p);
1208 count = SendMessage(hListWnd, LB_GETCOUNT, 0, 0);
1209 SendMessage(hListWnd, LB_SETITEMDATA, count-1, (LPARAM)p);
1210 *next = (char*)p + strlen((char*)p) + 7;
1213 struct index_data
1215 HLPFILE* hlpfile;
1216 BOOL jump;
1217 ULONG offset;
1220 /**************************************************************************
1221 * WINHELP_IndexDlgProc
1224 static INT_PTR CALLBACK WINHELP_IndexDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1226 static struct index_data* id;
1227 int sel;
1229 switch (msg)
1231 case WM_INITDIALOG:
1232 id = (struct index_data*)((PROPSHEETPAGE*)lParam)->lParam;
1233 HLPFILE_BPTreeEnum(id->hlpfile->kwbtree, cb_KWBTree,
1234 GetDlgItem(hWnd, IDC_INDEXLIST));
1235 id->jump = FALSE;
1236 id->offset = 1;
1237 return TRUE;
1238 case WM_COMMAND:
1239 switch (HIWORD(wParam))
1241 case LBN_DBLCLK:
1242 if (LOWORD(wParam) == IDC_INDEXLIST)
1243 SendMessage(GetParent(hWnd), PSM_PRESSBUTTON, PSBTN_OK, 0);
1244 break;
1246 break;
1247 case WM_NOTIFY:
1248 switch (((NMHDR*)lParam)->code)
1250 case PSN_APPLY:
1251 sel = SendDlgItemMessage(hWnd, IDC_INDEXLIST, LB_GETCURSEL, 0, 0);
1252 if (sel != LB_ERR)
1254 BYTE *p;
1255 int count;
1257 p = (BYTE*)SendDlgItemMessage(hWnd, IDC_INDEXLIST,
1258 LB_GETITEMDATA, sel, 0);
1259 count = *(short*)((char *)p + strlen((char *)p) + 1);
1260 if (count > 1)
1262 MessageBox(hWnd, "count > 1 not supported yet", "Error", MB_OK | MB_ICONSTOP);
1263 SetWindowLongPtr(hWnd, DWLP_MSGRESULT, PSNRET_INVALID);
1264 return TRUE;
1266 id->offset = *(ULONG*)((char *)p + strlen((char *)p) + 3);
1267 id->offset = *(long*)(id->hlpfile->kwdata + id->offset + 9);
1268 if (id->offset == 0xFFFFFFFF)
1270 MessageBox(hWnd, "macro keywords not supported yet", "Error", MB_OK | MB_ICONSTOP);
1271 SetWindowLongPtr(hWnd, DWLP_MSGRESULT, PSNRET_INVALID);
1272 return TRUE;
1274 id->jump = TRUE;
1275 SetWindowLongPtr(hWnd, DWLP_MSGRESULT, PSNRET_NOERROR);
1277 return TRUE;
1278 default:
1279 return FALSE;
1281 break;
1282 default:
1283 break;
1285 return FALSE;
1288 /**************************************************************************
1289 * WINHELP_SearchDlgProc
1292 static INT_PTR CALLBACK WINHELP_SearchDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1294 static struct index_data* id;
1296 switch (msg)
1298 case WM_INITDIALOG:
1299 id = (struct index_data*)((PROPSHEETPAGE*)lParam)->lParam;
1300 return TRUE;
1301 case WM_NOTIFY:
1302 switch (((NMHDR*)lParam)->code)
1304 case PSN_APPLY:
1305 SetWindowLongPtr(hWnd, DWLP_MSGRESULT, PSNRET_NOERROR);
1306 return TRUE;
1307 default:
1308 return FALSE;
1310 break;
1311 default:
1312 break;
1314 return FALSE;
1317 /***********************************************************************
1319 * WINHELP_MainWndProc
1321 static LRESULT CALLBACK WINHELP_MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1323 WINHELP_WINDOW *win;
1324 WINHELP_BUTTON *button;
1325 INT keyDelta;
1326 HWND hTextWnd;
1327 LRESULT ret;
1329 if (WINHELP_CheckPopup(hWnd, msg, wParam, lParam, &ret)) return ret;
1331 switch (msg)
1333 case WM_NCCREATE:
1334 win = (WINHELP_WINDOW*) ((LPCREATESTRUCT) lParam)->lpCreateParams;
1335 SetWindowLongPtr(hWnd, 0, (ULONG_PTR) win);
1336 if (!win->page && Globals.isBook)
1337 PostMessage(hWnd, WM_COMMAND, MNID_FILE_OPEN, 0);
1338 win->hMainWnd = hWnd;
1339 break;
1341 case WM_WINDOWPOSCHANGED:
1342 WINHELP_LayoutMainWindow((WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0));
1343 break;
1345 case WM_COMMAND:
1346 win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1347 switch (wParam)
1349 /* Menu FILE */
1350 case MNID_FILE_OPEN: MACRO_FileOpen(); break;
1351 case MNID_FILE_PRINT: MACRO_Print(); break;
1352 case MNID_FILE_SETUP: MACRO_PrinterSetup(); break;
1353 case MNID_FILE_EXIT: MACRO_Exit(); break;
1355 /* Menu EDIT */
1356 case MNID_EDIT_COPYDLG:
1357 SendMessage(GetDlgItem(hWnd, CTL_ID_TEXT), WM_COPY, 0, 0);
1358 break;
1359 case MNID_EDIT_ANNOTATE:MACRO_Annotate(); break;
1361 /* Menu Bookmark */
1362 case MNID_BKMK_DEFINE: MACRO_BookmarkDefine(); break;
1364 /* Menu Help */
1365 case MNID_HELP_HELPON: MACRO_HelpOn(); break;
1366 case MNID_HELP_HELPTOP: MACRO_HelpOnTop(); break;
1367 case MNID_HELP_ABOUT: MACRO_About(); break;
1368 case MNID_HELP_WINE: ShellAbout(hWnd, "WINE", "Help", 0); break;
1370 /* Context help */
1371 case MNID_CTXT_ANNOTATE:MACRO_Annotate(); break;
1372 case MNID_CTXT_COPY: MACRO_CopyDialog(); break;
1373 case MNID_CTXT_PRINT: MACRO_Print(); break;
1374 case MNID_OPTS_HISTORY: MACRO_History(); break;
1375 case MNID_OPTS_FONTS_SMALL:
1376 case MNID_CTXT_FONTS_SMALL:
1377 win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1378 if (win->font_scale != 0)
1380 win->font_scale = 0;
1381 WINHELP_SetupText(GetDlgItem(hWnd, CTL_ID_TEXT), win, 0 /* FIXME */);
1383 break;
1384 case MNID_OPTS_FONTS_NORMAL:
1385 case MNID_CTXT_FONTS_NORMAL:
1386 win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1387 if (win->font_scale != 1)
1389 win->font_scale = 1;
1390 WINHELP_SetupText(GetDlgItem(hWnd, CTL_ID_TEXT), win, 0 /* FIXME */);
1392 break;
1393 case MNID_OPTS_FONTS_LARGE:
1394 case MNID_CTXT_FONTS_LARGE:
1395 win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1396 if (win->font_scale != 2)
1398 win->font_scale = 2;
1399 WINHELP_SetupText(GetDlgItem(hWnd, CTL_ID_TEXT), win, 0 /* FIXME */);
1401 break;
1402 case MNID_OPTS_HELP_DEFAULT:
1403 case MNID_OPTS_HELP_VISIBLE:
1404 case MNID_OPTS_HELP_NONVISIBLE:
1405 case MNID_OPTS_SYSTEM_COLORS:
1406 case MNID_CTXT_HELP_DEFAULT:
1407 case MNID_CTXT_HELP_VISIBLE:
1408 case MNID_CTXT_HELP_NONVISIBLE:
1409 case MNID_CTXT_SYSTEM_COLORS:
1410 /* FIXME: NIY */
1412 default:
1413 /* Buttons */
1414 for (button = win->first_button; button; button = button->next)
1415 if (wParam == button->wParam) break;
1416 if (button)
1417 MACRO_ExecuteMacro(win, button->lpszMacro);
1418 else if (!HIWORD(wParam))
1419 MessageBox(0, MAKEINTRESOURCE(STID_NOT_IMPLEMENTED),
1420 MAKEINTRESOURCE(STID_WHERROR), MB_OK);
1421 break;
1423 break;
1424 /* EPP case WM_DESTROY: */
1425 /* EPP if (Globals.hPopupWnd) DestroyWindow(Globals.hPopupWnd); */
1426 /* EPP break; */
1427 case WM_COPYDATA:
1428 return WINHELP_HandleCommand((HWND)wParam, lParam);
1430 case WM_CHAR:
1431 if (wParam == 3)
1433 SendMessage(GetDlgItem(hWnd, CTL_ID_TEXT), WM_COPY, 0, 0);
1434 return 0;
1436 break;
1438 case WM_KEYDOWN:
1439 keyDelta = 0;
1440 win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1441 hTextWnd = GetDlgItem(win->hMainWnd, CTL_ID_TEXT);
1443 switch (wParam)
1445 case VK_UP:
1446 SendMessage(hTextWnd, EM_SCROLL, SB_LINEUP, 0);
1447 return 0;
1448 case VK_DOWN:
1449 SendMessage(hTextWnd, EM_SCROLL, SB_LINEDOWN, 0);
1450 return 0;
1451 case VK_PRIOR:
1452 SendMessage(hTextWnd, EM_SCROLL, SB_PAGEUP, 0);
1453 return 0;
1454 case VK_NEXT:
1455 SendMessage(hTextWnd, EM_SCROLL, SB_PAGEDOWN, 0);
1456 return 0;
1457 case VK_ESCAPE:
1458 MACRO_Exit();
1459 return 0;
1461 break;
1463 case WM_NOTIFY:
1464 if (wParam == CTL_ID_TEXT)
1466 RECT rc;
1468 switch (((NMHDR*)lParam)->code)
1470 case EN_MSGFILTER:
1472 const MSGFILTER* msgf = (const MSGFILTER*)lParam;
1473 switch (msgf->msg)
1475 case WM_KEYUP:
1476 if (msgf->wParam == VK_ESCAPE)
1477 WINHELP_ReleaseWindow((WINHELP_WINDOW*)GetWindowLongPtr(hWnd, 0));
1478 break;
1479 case WM_RBUTTONDOWN:
1481 HMENU hMenu;
1482 POINT pt;
1484 win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1485 hMenu = LoadMenu(Globals.hInstance, (LPSTR)CONTEXT_MENU);
1486 switch (win->font_scale)
1488 case 0:
1489 CheckMenuItem(hMenu, MNID_CTXT_FONTS_SMALL,
1490 MF_BYCOMMAND|MF_CHECKED);
1491 break;
1492 default:
1493 WINE_FIXME("Unsupported %d\n", win->font_scale);
1494 case 1:
1495 CheckMenuItem(hMenu, MNID_CTXT_FONTS_NORMAL,
1496 MF_BYCOMMAND|MF_CHECKED);
1497 break;
1498 case 2:
1499 CheckMenuItem(hMenu, MNID_CTXT_FONTS_LARGE,
1500 MF_BYCOMMAND|MF_CHECKED);
1501 break;
1503 pt.x = (int)(short)LOWORD(msgf->lParam);
1504 pt.y = (int)(short)HIWORD(msgf->lParam);
1505 ClientToScreen(msgf->nmhdr.hwndFrom, &pt);
1506 TrackPopupMenu(GetSubMenu(hMenu, 0), TPM_LEFTALIGN|TPM_TOPALIGN,
1507 pt.x, pt.y, 0, hWnd, NULL);
1508 DestroyMenu(hMenu);
1510 break;
1511 default:
1512 return WINHELP_HandleTextMouse((WINHELP_WINDOW*)GetWindowLongPtr(hWnd, 0),
1513 msgf->msg, msgf->lParam);
1516 break;
1518 case EN_REQUESTRESIZE:
1519 rc = ((REQRESIZE*)lParam)->rc;
1520 win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1521 AdjustWindowRect(&rc, GetWindowLong(win->hMainWnd, GWL_STYLE),
1522 FALSE);
1523 SetWindowPos(win->hMainWnd, HWND_TOP, 0, 0,
1524 rc.right - rc.left, rc.bottom - rc.top,
1525 SWP_NOMOVE | SWP_NOZORDER);
1526 WINHELP_LayoutMainWindow(win);
1527 break;
1530 break;
1532 case WM_INITMENUPOPUP:
1533 win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1534 CheckMenuItem((HMENU)wParam, MNID_OPTS_FONTS_SMALL,
1535 MF_BYCOMMAND | (win->font_scale == 0) ? MF_CHECKED : 0);
1536 CheckMenuItem((HMENU)wParam, MNID_OPTS_FONTS_NORMAL,
1537 MF_BYCOMMAND | (win->font_scale == 1) ? MF_CHECKED : 0);
1538 CheckMenuItem((HMENU)wParam, MNID_OPTS_FONTS_LARGE,
1539 MF_BYCOMMAND | (win->font_scale == 2) ? MF_CHECKED : 0);
1540 break;
1541 case WM_DESTROY:
1542 win = (WINHELP_WINDOW*) GetWindowLongPtr(hWnd, 0);
1543 WINHELP_DeleteWindow(win);
1544 break;
1546 return DefWindowProc(hWnd, msg, wParam, lParam);
1549 /**************************************************************************
1550 * WINHELP_CreateIndexWindow
1552 * Displays a dialog with keywords of current help file.
1555 BOOL WINHELP_CreateIndexWindow(BOOL is_search)
1557 HPROPSHEETPAGE psPage[3];
1558 PROPSHEETPAGE psp;
1559 PROPSHEETHEADER psHead;
1560 struct index_data id;
1561 char buf[256];
1563 if (Globals.active_win && Globals.active_win->page && Globals.active_win->page->file)
1564 id.hlpfile = Globals.active_win->page->file;
1565 else
1566 return FALSE;
1568 if (id.hlpfile->kwbtree == NULL)
1570 WINE_TRACE("No index provided\n");
1571 return FALSE;
1574 InitCommonControls();
1576 id.jump = FALSE;
1577 memset(&psp, 0, sizeof(psp));
1578 psp.dwSize = sizeof(psp);
1579 psp.dwFlags = 0;
1580 psp.hInstance = Globals.hInstance;
1582 psp.u.pszTemplate = MAKEINTRESOURCE(IDD_INDEX);
1583 psp.lParam = (LPARAM)&id;
1584 psp.pfnDlgProc = WINHELP_IndexDlgProc;
1585 psPage[0] = CreatePropertySheetPage(&psp);
1587 psp.u.pszTemplate = MAKEINTRESOURCE(IDD_SEARCH);
1588 psp.lParam = (LPARAM)&id;
1589 psp.pfnDlgProc = WINHELP_SearchDlgProc;
1590 psPage[1] = CreatePropertySheetPage(&psp);
1592 memset(&psHead, 0, sizeof(psHead));
1593 psHead.dwSize = sizeof(psHead);
1595 LoadString(Globals.hInstance, STID_PSH_INDEX, buf, sizeof(buf));
1596 strcat(buf, Globals.active_win->info->caption);
1598 psHead.pszCaption = buf;
1599 psHead.nPages = 2;
1600 psHead.u2.nStartPage = is_search ? 1 : 0;
1601 psHead.hwndParent = Globals.active_win->hMainWnd;
1602 psHead.u3.phpage = psPage;
1603 psHead.dwFlags = PSH_NOAPPLYNOW;
1605 PropertySheet(&psHead);
1606 if (id.jump)
1608 WINE_TRACE("got %d as an offset\n", id.offset);
1609 WINHELP_OpenHelpWindow(HLPFILE_PageByOffset, id.hlpfile, id.offset,
1610 Globals.active_win->info, SW_NORMAL);
1612 return TRUE;
1615 /***********************************************************************
1617 * RegisterWinClasses
1619 static BOOL WINHELP_RegisterWinClasses(void)
1621 WNDCLASS class_main, class_button_box, class_shadow, class_history;
1623 class_main.style = CS_HREDRAW | CS_VREDRAW;
1624 class_main.lpfnWndProc = WINHELP_MainWndProc;
1625 class_main.cbClsExtra = 0;
1626 class_main.cbWndExtra = sizeof(WINHELP_WINDOW *);
1627 class_main.hInstance = Globals.hInstance;
1628 class_main.hIcon = LoadIcon(Globals.hInstance, MAKEINTRESOURCE(IDI_WINHELP));
1629 class_main.hCursor = LoadCursor(0, IDC_ARROW);
1630 class_main.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
1631 class_main.lpszMenuName = 0;
1632 class_main.lpszClassName = MAIN_WIN_CLASS_NAME;
1634 class_button_box = class_main;
1635 class_button_box.lpfnWndProc = WINHELP_ButtonBoxWndProc;
1636 class_button_box.cbWndExtra = 0;
1637 class_button_box.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
1638 class_button_box.lpszClassName = BUTTON_BOX_WIN_CLASS_NAME;
1640 class_shadow = class_main;
1641 class_shadow.lpfnWndProc = WINHELP_ShadowWndProc;
1642 class_shadow.cbWndExtra = 0;
1643 class_shadow.hbrBackground = (HBRUSH)(COLOR_3DDKSHADOW+1);
1644 class_shadow.lpszClassName = SHADOW_WIN_CLASS_NAME;
1646 class_history = class_main;
1647 class_history.lpfnWndProc = WINHELP_HistoryWndProc;
1648 class_history.lpszClassName = HISTORY_WIN_CLASS_NAME;
1650 return (RegisterClass(&class_main) &&
1651 RegisterClass(&class_button_box) &&
1652 RegisterClass(&class_shadow) &&
1653 RegisterClass(&class_history));
1656 /***********************************************************************
1658 * WinMain
1660 int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
1662 MSG msg;
1663 LONG lHash = 0;
1664 HLPFILE* hlpfile;
1665 static CHAR default_wndname[] = "main";
1666 LPSTR wndname = default_wndname;
1667 WINHELP_DLL* dll;
1669 Globals.hInstance = hInstance;
1671 if (LoadLibrary("riched20.dll") == NULL)
1672 return MessageBox(0, MAKEINTRESOURCE(STID_NO_RICHEDIT),
1673 MAKEINTRESOURCE(STID_WHERROR), MB_OK);
1675 /* Get options */
1676 while (*cmdline && (*cmdline == ' ' || *cmdline == '-'))
1678 CHAR option;
1679 LPCSTR topic_id;
1680 if (*cmdline++ == ' ') continue;
1682 option = *cmdline;
1683 if (option) cmdline++;
1684 while (*cmdline && *cmdline == ' ') cmdline++;
1685 switch (option)
1687 case 'i':
1688 case 'I':
1689 topic_id = cmdline;
1690 while (*cmdline && *cmdline != ' ') cmdline++;
1691 if (*cmdline) *cmdline++ = '\0';
1692 lHash = HLPFILE_Hash(topic_id);
1693 break;
1695 case '3':
1696 case '4':
1697 Globals.wVersion = option - '0';
1698 break;
1700 case 'x':
1701 show = SW_HIDE;
1702 Globals.isBook = FALSE;
1703 break;
1705 default:
1706 WINE_FIXME("Unsupported cmd line: %s\n", cmdline);
1707 break;
1711 /* Create primary window */
1712 if (!WINHELP_RegisterWinClasses())
1714 WINE_FIXME("Couldn't register classes\n");
1715 return 0;
1718 if (*cmdline)
1720 char* ptr;
1721 if ((*cmdline == '"') && (ptr = strchr(cmdline+1, '"')))
1723 cmdline++;
1724 *ptr = '\0';
1726 if ((ptr = strchr(cmdline, '>')))
1728 *ptr = '\0';
1729 wndname = ptr + 1;
1731 hlpfile = WINHELP_LookupHelpFile(cmdline);
1732 if (!hlpfile) return 0;
1734 else hlpfile = NULL;
1735 WINHELP_OpenHelpWindow(HLPFILE_PageByHash, hlpfile, lHash,
1736 WINHELP_GetWindowInfo(hlpfile, wndname), show);
1738 /* Message loop */
1739 while (GetMessage(&msg, 0, 0, 0))
1741 TranslateMessage(&msg);
1742 DispatchMessage(&msg);
1744 for (dll = Globals.dlls; dll; dll = dll->next)
1746 if (dll->class & DC_INITTERM) dll->handler(DW_TERM, 0, 0);
1748 return 0;