cmd: Add CHOICE builtin with DOS6 to XP commandline parameter.
[wine] / programs / winhlp32 / macro.c
1 /*
2  * Help Viewer
3  *
4  * Copyright 1996 Ulrich Schmid
5  * Copyright 2002, 2008 Eric Pouech
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #define WIN32_LEAN_AND_MEAN
23
24 #include <stdio.h>
25
26 #include "windows.h"
27 #include "commdlg.h"
28 #include "shellapi.h"
29 #include "winhelp.h"
30
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(winhelp);
34
35 /**************************************************/
36 /*               Macro table                      */
37 /**************************************************/
38 struct MacroDesc {
39     const char* name;
40     const char* alias;
41     BOOL        isBool;
42     const char* arguments;
43     void       *fn;
44 };
45
46 static struct MacroDesc*MACRO_Loaded /* = NULL */;
47 static unsigned         MACRO_NumLoaded /* = 0 */;
48
49 /*******      helper functions     *******/
50
51 static char* StrDup(const char* str)
52 {
53     char* dst;
54     dst=HeapAlloc(GetProcessHeap(),0,strlen(str)+1);
55     strcpy(dst, str);
56     return dst;
57 }
58
59 static WINHELP_BUTTON**        MACRO_LookupButton(WINHELP_WINDOW* win, LPCSTR name)
60 {
61     WINHELP_BUTTON**    b;
62
63     for (b = &win->first_button; *b; b = &(*b)->next)
64         if (!lstrcmpi(name, (*b)->lpszID)) break;
65     return b;
66 }
67
68 /******* some forward declarations *******/
69 static void CALLBACK MACRO_JumpID(LPCSTR lpszPathWindow, LPCSTR topic_id);
70
71 /******* real macro implementation *******/
72
73 void CALLBACK MACRO_CreateButton(LPCSTR id, LPCSTR name, LPCSTR macro)
74 {
75     WINHELP_WINDOW *win = MACRO_CurrentWindow();
76     WINHELP_BUTTON *button, **b;
77     LONG            size;
78     LPSTR           ptr;
79
80     WINE_TRACE("(\"%s\", \"%s\", %s)\n", id, name, macro);
81
82     size = sizeof(WINHELP_BUTTON) + lstrlen(id) + lstrlen(name) + lstrlen(macro) + 3;
83
84     button = HeapAlloc(GetProcessHeap(), 0, size);
85     if (!button) return;
86
87     button->next  = 0;
88     button->hWnd  = 0;
89
90     ptr = (char*)button + sizeof(WINHELP_BUTTON);
91
92     lstrcpy(ptr, id);
93     button->lpszID = ptr;
94     ptr += lstrlen(id) + 1;
95
96     lstrcpy(ptr, name);
97     button->lpszName = ptr;
98     ptr += lstrlen(name) + 1;
99
100     lstrcpy(ptr, macro);
101     button->lpszMacro = ptr;
102
103     button->wParam = WH_FIRST_BUTTON;
104     for (b = &win->first_button; *b; b = &(*b)->next)
105         button->wParam = max(button->wParam, (*b)->wParam + 1);
106     *b = button;
107
108     WINHELP_LayoutMainWindow(win);
109 }
110
111 static void CALLBACK MACRO_DestroyButton(LPCSTR str)
112 {
113     WINE_FIXME("(\"%s\")\n", str);
114 }
115
116 void CALLBACK MACRO_DisableButton(LPCSTR id)
117 {
118     WINHELP_BUTTON**    b;
119
120     WINE_TRACE("(\"%s\")\n", id);
121
122     b = MACRO_LookupButton(MACRO_CurrentWindow(), id);
123     if (!*b) {WINE_FIXME("Couldn't find button '%s'\n", id); return;}
124
125     EnableWindow((*b)->hWnd, FALSE);
126 }
127
128 static void CALLBACK MACRO_EnableButton(LPCSTR id)
129 {
130     WINHELP_BUTTON**    b;
131
132     WINE_TRACE("(\"%s\")\n", id);
133
134     b = MACRO_LookupButton(MACRO_CurrentWindow(), id);
135     if (!*b) {WINE_FIXME("Couldn't find button '%s'\n", id); return;}
136
137     EnableWindow((*b)->hWnd, TRUE);
138 }
139
140 void CALLBACK MACRO_JumpContents(LPCSTR lpszPath, LPCSTR lpszWindow)
141 {
142     HLPFILE*    hlpfile;
143
144     WINE_TRACE("(\"%s\", \"%s\")\n", lpszPath, lpszWindow);
145     if ((hlpfile = WINHELP_LookupHelpFile(lpszPath)))
146         WINHELP_OpenHelpWindow(HLPFILE_PageByHash, hlpfile, 0,
147                                WINHELP_GetWindowInfo(hlpfile, lpszWindow),
148                                SW_NORMAL);
149 }
150
151
152 void CALLBACK MACRO_About(void)
153 {
154     WCHAR name[256];
155     HICON icon = LoadImageW( Globals.hInstance, MAKEINTRESOURCEW(IDI_WINHELP),
156                              IMAGE_ICON, 48, 48, LR_SHARED );
157     LoadStringW( Globals.hInstance, STID_WINE_HELP, name, sizeof(name)/sizeof(WCHAR) );
158     ShellAboutW( MACRO_CurrentWindow()->hMainWnd, name, NULL, icon );
159 }
160
161 static void CALLBACK MACRO_AddAccelerator(LONG u1, LONG u2, LPCSTR str)
162 {
163     WINE_FIXME("(%u, %u, \"%s\")\n", u1, u2, str);
164 }
165
166 static void CALLBACK MACRO_ALink(LPCSTR str1, LONG u, LPCSTR str2)
167 {
168     WINE_FIXME("(\"%s\", %u, \"%s\")\n", str1, u, str2);
169 }
170
171 void CALLBACK MACRO_Annotate(void)
172 {
173     WINE_FIXME("()\n");
174 }
175
176 static void CALLBACK MACRO_AppendItem(LPCSTR str1, LPCSTR str2, LPCSTR str3, LPCSTR str4)
177 {
178     WINE_FIXME("(\"%s\", \"%s\", \"%s\", \"%s\")\n", str1, str2, str3, str4);
179 }
180
181 static void CALLBACK MACRO_Back(void)
182 {
183     WINHELP_WINDOW* win = MACRO_CurrentWindow();
184
185     WINE_TRACE("()\n");
186
187     if (win && win->back.index >= 2)
188         WINHELP_CreateHelpWindow(&win->back.set[--win->back.index - 1], SW_SHOW, FALSE);
189 }
190
191 static void CALLBACK MACRO_BackFlush(void)
192 {
193     WINHELP_WINDOW* win = MACRO_CurrentWindow();
194
195     WINE_TRACE("()\n");
196
197     if (win) WINHELP_DeleteBackSet(win);
198 }
199
200 void CALLBACK MACRO_BookmarkDefine(void)
201 {
202     WINE_FIXME("()\n");
203 }
204
205 static void CALLBACK MACRO_BookmarkMore(void)
206 {
207     WINE_FIXME("()\n");
208 }
209
210 static void CALLBACK MACRO_BrowseButtons(void)
211 {
212     HLPFILE_PAGE*       page = MACRO_CurrentWindow()->page;
213     ULONG               relative;
214
215     WINE_TRACE("()\n");
216
217     MACRO_CreateButton("BTN_PREV", "&<<", "Prev()");
218     MACRO_CreateButton("BTN_NEXT", "&>>", "Next()");
219
220     if (!HLPFILE_PageByOffset(page->file, page->browse_bwd, &relative))
221         MACRO_DisableButton("BTN_PREV");
222     if (!HLPFILE_PageByOffset(page->file, page->browse_fwd, &relative))
223         MACRO_DisableButton("BTN_NEXT");
224 }
225
226 static void CALLBACK MACRO_ChangeButtonBinding(LPCSTR id, LPCSTR macro)
227 {
228     WINHELP_WINDOW*     win = MACRO_CurrentWindow();
229     WINHELP_BUTTON*     button;
230     WINHELP_BUTTON**    b;
231     LONG                size;
232     LPSTR               ptr;
233
234     WINE_TRACE("(\"%s\", \"%s\")\n", id, macro);
235
236     b = MACRO_LookupButton(win, id);
237     if (!*b) {WINE_FIXME("Couldn't find button '%s'\n", id); return;}
238
239     size = sizeof(WINHELP_BUTTON) + lstrlen(id) +
240         lstrlen((*b)->lpszName) + lstrlen(macro) + 3;
241
242     button = HeapAlloc(GetProcessHeap(), 0, size);
243     if (!button) return;
244
245     button->next  = (*b)->next;
246     button->hWnd  = (*b)->hWnd;
247     button->wParam = (*b)->wParam;
248
249     ptr = (char*)button + sizeof(WINHELP_BUTTON);
250
251     lstrcpy(ptr, id);
252     button->lpszID = ptr;
253     ptr += lstrlen(id) + 1;
254
255     lstrcpy(ptr, (*b)->lpszName);
256     button->lpszName = ptr;
257     ptr += lstrlen((*b)->lpszName) + 1;
258
259     lstrcpy(ptr, macro);
260     button->lpszMacro = ptr;
261
262     *b = button;
263
264     WINHELP_LayoutMainWindow(win);
265 }
266
267 static void CALLBACK MACRO_ChangeEnable(LPCSTR id, LPCSTR macro)
268 {
269     WINE_TRACE("(\"%s\", \"%s\")\n", id, macro);
270
271     MACRO_ChangeButtonBinding(id, macro);
272     MACRO_EnableButton(id);
273 }
274
275 static void CALLBACK MACRO_ChangeItemBinding(LPCSTR str1, LPCSTR str2)
276 {
277     WINE_FIXME("(\"%s\", \"%s\")\n", str1, str2);
278 }
279
280 static void CALLBACK MACRO_CheckItem(LPCSTR str)
281 {
282     WINE_FIXME("(\"%s\")\n", str);
283 }
284
285 static void CALLBACK MACRO_CloseSecondarys(void)
286 {
287     WINHELP_WINDOW *win;
288     WINHELP_WINDOW *next;
289
290     WINE_TRACE("()\n");
291     for (win = Globals.win_list; win; win = next)
292     {
293         next = win->next;
294         if (lstrcmpi(win->info->name, "main"))
295             WINHELP_ReleaseWindow(win);
296     }
297 }
298
299 static void CALLBACK MACRO_CloseWindow(LPCSTR lpszWindow)
300 {
301     WINHELP_WINDOW *win;
302     WINHELP_WINDOW *next;
303
304     WINE_TRACE("(\"%s\")\n", lpszWindow);
305
306     if (!lpszWindow || !lpszWindow[0]) lpszWindow = "main";
307
308     for (win = Globals.win_list; win; win = next)
309     {
310         next = win->next;
311         if (!lstrcmpi(win->info->name, lpszWindow))
312             WINHELP_ReleaseWindow(win);
313     }
314 }
315
316 static void CALLBACK MACRO_Compare(LPCSTR str)
317 {
318     WINE_FIXME("(\"%s\")\n", str);
319 }
320
321 static void CALLBACK MACRO_Contents(void)
322 {
323     HLPFILE_PAGE*       page = MACRO_CurrentWindow()->page;
324
325     WINE_TRACE("()\n");
326
327     if (page)
328         MACRO_JumpContents(page->file->lpszPath, NULL);
329 }
330
331 static void CALLBACK MACRO_ControlPanel(LPCSTR str1, LPCSTR str2, LONG u)
332 {
333     WINE_FIXME("(\"%s\", \"%s\", %u)\n", str1, str2, u);
334 }
335
336 void CALLBACK MACRO_CopyDialog(void)
337 {
338     WINE_FIXME("()\n");
339 }
340
341 static void CALLBACK MACRO_CopyTopic(void)
342 {
343     WINE_FIXME("()\n");
344 }
345
346 static void CALLBACK MACRO_DeleteItem(LPCSTR str)
347 {
348     WINE_FIXME("(\"%s\")\n", str);
349 }
350
351 static void CALLBACK MACRO_DeleteMark(LPCSTR str)
352 {
353     WINE_FIXME("(\"%s\")\n", str);
354 }
355
356 static void CALLBACK MACRO_DisableItem(LPCSTR str)
357 {
358     WINE_FIXME("(\"%s\")\n", str);
359 }
360
361 static void CALLBACK MACRO_EnableItem(LPCSTR str)
362 {
363     WINE_FIXME("(\"%s\")\n", str);
364 }
365
366 static void CALLBACK MACRO_EndMPrint(void)
367 {
368     WINE_FIXME("()\n");
369 }
370
371 static void CALLBACK MACRO_ExecFile(LPCSTR pgm, LPCSTR args, LONG cmd_show, LPCSTR topic)
372 {
373     HINSTANCE ret;
374
375     WINE_TRACE("(%s, %s, %u, %s)\n",
376                wine_dbgstr_a(pgm), wine_dbgstr_a(args), cmd_show, wine_dbgstr_a(topic));
377
378     ret = ShellExecuteA(Globals.active_win ? Globals.active_win->hMainWnd : NULL, "open",
379                         pgm, args, ".", cmd_show);
380     if ((DWORD_PTR)ret < 32)
381     {
382         WINE_WARN("Failed with %p\n", ret);
383         if (topic) MACRO_JumpID(NULL, topic);
384     }
385 }
386
387 static void CALLBACK MACRO_ExecProgram(LPCSTR str, LONG u)
388 {
389     WINE_FIXME("(\"%s\", %u)\n", str, u);
390 }
391
392 void CALLBACK MACRO_Exit(void)
393 {
394     WINE_TRACE("()\n");
395
396     while (Globals.win_list)
397         WINHELP_ReleaseWindow(Globals.win_list);
398 }
399
400 static void CALLBACK MACRO_ExtAbleItem(LPCSTR str, LONG u)
401 {
402     WINE_FIXME("(\"%s\", %u)\n", str, u);
403 }
404
405 static void CALLBACK MACRO_ExtInsertItem(LPCSTR str1, LPCSTR str2, LPCSTR str3, LPCSTR str4, LONG u1, LONG u2)
406 {
407     WINE_FIXME("(\"%s\", \"%s\", \"%s\", \"%s\", %u, %u)\n", str1, str2, str3, str4, u1, u2);
408 }
409
410 static void CALLBACK MACRO_ExtInsertMenu(LPCSTR str1, LPCSTR str2, LPCSTR str3, LONG u1, LONG u2)
411 {
412     WINE_FIXME("(\"%s\", \"%s\", \"%s\", %u, %u)\n", str1, str2, str3, u1, u2);
413 }
414
415 static BOOL CALLBACK MACRO_FileExist(LPCSTR str)
416 {
417     WINE_TRACE("(\"%s\")\n", str);
418     return GetFileAttributes(str) != INVALID_FILE_ATTRIBUTES;
419 }
420
421 void CALLBACK MACRO_FileOpen(void)
422 {
423     char szFile[MAX_PATH];
424
425     if (WINHELP_GetOpenFileName(szFile, MAX_PATH))
426     {
427         MACRO_JumpContents(szFile, "main");
428     }
429 }
430
431 static void CALLBACK MACRO_Find(void)
432 {
433     WINE_FIXME("()\n");
434 }
435
436 static void CALLBACK MACRO_Finder(void)
437 {
438     WINHELP_CreateIndexWindow(FALSE);
439 }
440
441 static void CALLBACK MACRO_FloatingMenu(void)
442 {
443     WINE_FIXME("()\n");
444 }
445
446 static void CALLBACK MACRO_Flush(void)
447 {
448     WINE_FIXME("()\n");
449 }
450
451 static void CALLBACK MACRO_FocusWindow(LPCSTR lpszWindow)
452 {
453     WINHELP_WINDOW *win;
454
455     WINE_TRACE("(\"%s\")\n", lpszWindow);
456
457     if (!lpszWindow || !lpszWindow[0]) lpszWindow = "main";
458
459     for (win = Globals.win_list; win; win = win->next)
460         if (!lstrcmpi(win->info->name, lpszWindow))
461             SetFocus(win->hMainWnd);
462 }
463
464 static void CALLBACK MACRO_Generate(LPCSTR str, LONG w, LONG l)
465 {
466     WINE_FIXME("(\"%s\", %x, %x)\n", str, w, l);
467 }
468
469 static void CALLBACK MACRO_GotoMark(LPCSTR str)
470 {
471     WINE_FIXME("(\"%s\")\n", str);
472 }
473
474 void CALLBACK MACRO_HelpOn(void)
475 {
476     WINHELP_WINDOW *win = MACRO_CurrentWindow();
477     LPCSTR      file = NULL;
478
479     WINE_TRACE("()\n");
480     if (win && win->page && win->page->file)
481         file = win->page->file->help_on_file;
482
483     if (!file)
484         file = (Globals.wVersion > 4) ? "winhlp32.hlp" : "winhelp.hlp";
485
486     MACRO_JumpContents(file, NULL);
487 }
488
489 void CALLBACK MACRO_HelpOnTop(void)
490 {
491     WINE_FIXME("()\n");
492 }
493
494 void CALLBACK MACRO_History(void)
495 {
496     WINE_TRACE("()\n");
497
498     if (Globals.active_win && !Globals.active_win->hHistoryWnd)
499     {
500         HWND hWnd = CreateWindow(HISTORY_WIN_CLASS_NAME, "History", WS_OVERLAPPEDWINDOW,
501                                  0, 0, 0, 0, 0, 0, Globals.hInstance, Globals.active_win);
502         ShowWindow(hWnd, SW_NORMAL);
503     }
504 }
505
506 static void CALLBACK MACRO_IfThen(BOOL b, LPCSTR t)
507 {
508     if (b) MACRO_ExecuteMacro(MACRO_CurrentWindow(), t);
509 }
510
511 static void CALLBACK MACRO_IfThenElse(BOOL b, LPCSTR t, LPCSTR f)
512 {
513     if (b) MACRO_ExecuteMacro(MACRO_CurrentWindow(), t);
514     else MACRO_ExecuteMacro(MACRO_CurrentWindow(), f);
515 }
516
517 static BOOL CALLBACK MACRO_InitMPrint(void)
518 {
519     WINE_FIXME("()\n");
520     return FALSE;
521 }
522
523 static void CALLBACK MACRO_InsertItem(LPCSTR str1, LPCSTR str2, LPCSTR str3, LPCSTR str4, LONG u)
524 {
525     WINE_FIXME("(\"%s\", \"%s\", \"%s\", \"%s\", %u)\n", str1, str2, str3, str4, u);
526 }
527
528 static void CALLBACK MACRO_InsertMenu(LPCSTR str1, LPCSTR str2, LONG u)
529 {
530     WINE_FIXME("(\"%s\", \"%s\", %u)\n", str1, str2, u);
531 }
532
533 static BOOL CALLBACK MACRO_IsBook(void)
534 {
535     WINE_TRACE("()\n");
536     return Globals.isBook;
537 }
538
539 static BOOL CALLBACK MACRO_IsMark(LPCSTR str)
540 {
541     WINE_FIXME("(\"%s\")\n", str);
542     return FALSE;
543 }
544
545 static BOOL CALLBACK MACRO_IsNotMark(LPCSTR str)
546 {
547     WINE_FIXME("(\"%s\")\n", str);
548     return TRUE;
549 }
550
551 void CALLBACK MACRO_JumpContext(LPCSTR lpszPath, LPCSTR lpszWindow, LONG context)
552 {
553     HLPFILE*    hlpfile;
554
555     WINE_TRACE("(\"%s\", \"%s\", %d)\n", lpszPath, lpszWindow, context);
556     hlpfile = WINHELP_LookupHelpFile(lpszPath);
557     /* Some madness: what user calls 'context', hlpfile calls 'map' */
558     WINHELP_OpenHelpWindow(HLPFILE_PageByMap, hlpfile, context,
559                            WINHELP_GetWindowInfo(hlpfile, lpszWindow),
560                            SW_NORMAL);
561 }
562
563 void CALLBACK MACRO_JumpHash(LPCSTR lpszPath, LPCSTR lpszWindow, LONG lHash)
564 {
565     HLPFILE*    hlpfile;
566
567     WINE_TRACE("(\"%s\", \"%s\", %u)\n", lpszPath, lpszWindow, lHash);
568     if (!lpszPath || !lpszPath[0])
569         hlpfile = MACRO_CurrentWindow()->page->file;
570     else
571         hlpfile = WINHELP_LookupHelpFile(lpszPath);
572     WINHELP_OpenHelpWindow(HLPFILE_PageByHash, hlpfile, lHash,
573                            WINHELP_GetWindowInfo(hlpfile, lpszWindow),
574                            SW_NORMAL);
575 }
576
577 static void CALLBACK MACRO_JumpHelpOn(void)
578 {
579     WINE_FIXME("()\n");
580 }
581
582 static void CALLBACK MACRO_JumpID(LPCSTR lpszPathWindow, LPCSTR topic_id)
583 {
584     LPSTR       ptr;
585
586     WINE_TRACE("(\"%s\", \"%s\")\n", lpszPathWindow, topic_id);
587     if ((ptr = strchr(lpszPathWindow, '>')) != NULL)
588     {
589         LPSTR   tmp;
590         size_t  sz;
591
592         tmp = HeapAlloc(GetProcessHeap(), 0, strlen(lpszPathWindow) + 1);
593         if (tmp)
594         {
595             strcpy(tmp, lpszPathWindow);
596             tmp[ptr - lpszPathWindow] = '\0';
597             ptr += tmp - lpszPathWindow; /* ptr now points to '>' in tmp buffer */
598             /* in some cases, we have a trailing space that we need to get rid of */
599             /* FIXME: check if it has to be done in lexer rather than here */
600             for (sz = strlen(ptr + 1); sz >= 1 && ptr[sz] == ' '; sz--) ptr[sz] = '\0';
601             MACRO_JumpHash(tmp, ptr + 1, HLPFILE_Hash(topic_id));
602             HeapFree(GetProcessHeap(), 0, tmp);
603         }
604     }
605     else
606         MACRO_JumpHash(lpszPathWindow, NULL, HLPFILE_Hash(topic_id));
607 }
608
609 /* FIXME: this macros is wrong
610  * it should only contain 2 strings, path & window are coded as path>window
611  */
612 static void CALLBACK MACRO_JumpKeyword(LPCSTR lpszPath, LPCSTR lpszWindow, LPCSTR keyword)
613 {
614     WINE_FIXME("(\"%s\", \"%s\", \"%s\")\n", lpszPath, lpszWindow, keyword);
615 }
616
617 static void CALLBACK MACRO_KLink(LPCSTR str1, LONG u, LPCSTR str2, LPCSTR str3)
618 {
619     WINE_FIXME("(\"%s\", %u, \"%s\", \"%s\")\n", str1, u, str2, str3);
620 }
621
622 static void CALLBACK MACRO_Menu(void)
623 {
624     WINE_FIXME("()\n");
625 }
626
627 static void CALLBACK MACRO_MPrintHash(LONG u)
628 {
629     WINE_FIXME("(%u)\n", u);
630 }
631
632 static void CALLBACK MACRO_MPrintID(LPCSTR str)
633 {
634     WINE_FIXME("(\"%s\")\n", str);
635 }
636
637 static void CALLBACK MACRO_Next(void)
638 {
639     WINHELP_WNDPAGE     wp;
640
641     WINE_TRACE("()\n");
642     wp.page = MACRO_CurrentWindow()->page;
643     wp.page = HLPFILE_PageByOffset(wp.page->file, wp.page->browse_fwd, &wp.relative);
644     if (wp.page)
645     {
646         wp.page->file->wRefCount++;
647         wp.wininfo = MACRO_CurrentWindow()->info;
648         WINHELP_CreateHelpWindow(&wp, SW_NORMAL, TRUE);
649     }
650 }
651
652 static void CALLBACK MACRO_NoShow(void)
653 {
654     WINE_FIXME("()\n");
655 }
656
657 void CALLBACK MACRO_PopupContext(LPCSTR str, LONG u)
658 {
659     WINE_FIXME("(\"%s\", %u)\n", str, u);
660 }
661
662 static void CALLBACK MACRO_PopupHash(LPCSTR str, LONG u)
663 {
664     WINE_FIXME("(\"%s\", %u)\n", str, u);
665 }
666
667 static void CALLBACK MACRO_PopupId(LPCSTR str1, LPCSTR str2)
668 {
669     WINE_FIXME("(\"%s\", \"%s\")\n", str1, str2);
670 }
671
672 static void CALLBACK MACRO_PositionWindow(LONG i1, LONG i2, LONG u1, LONG u2, LONG u3, LPCSTR str)
673 {
674     WINE_FIXME("(%i, %i, %u, %u, %u, \"%s\")\n", i1, i2, u1, u2, u3, str);
675 }
676
677 static void CALLBACK MACRO_Prev(void)
678 {
679     WINHELP_WNDPAGE     wp;
680
681     WINE_TRACE("()\n");
682     wp.page = MACRO_CurrentWindow()->page;
683     wp.page = HLPFILE_PageByOffset(wp.page->file, wp.page->browse_bwd, &wp.relative);
684     if (wp.page)
685     {
686         wp.page->file->wRefCount++;
687         wp.wininfo = MACRO_CurrentWindow()->info;
688         WINHELP_CreateHelpWindow(&wp, SW_NORMAL, TRUE);
689     }
690 }
691
692 void CALLBACK MACRO_Print(void)
693 {
694     PRINTDLG printer;
695
696     WINE_TRACE("()\n");
697
698     printer.lStructSize         = sizeof(printer);
699     printer.hwndOwner           = MACRO_CurrentWindow()->hMainWnd;
700     printer.hInstance           = Globals.hInstance;
701     printer.hDevMode            = 0;
702     printer.hDevNames           = 0;
703     printer.hDC                 = 0;
704     printer.Flags               = 0;
705     printer.nFromPage           = 0;
706     printer.nToPage             = 0;
707     printer.nMinPage            = 0;
708     printer.nMaxPage            = 0;
709     printer.nCopies             = 0;
710     printer.lCustData           = 0;
711     printer.lpfnPrintHook       = 0;
712     printer.lpfnSetupHook       = 0;
713     printer.lpPrintTemplateName = 0;
714     printer.lpSetupTemplateName = 0;
715     printer.hPrintTemplate      = 0;
716     printer.hSetupTemplate      = 0;
717
718     if (PrintDlgA(&printer)) {
719         WINE_FIXME("Print()\n");
720     }
721 }
722
723 void CALLBACK MACRO_PrinterSetup(void)
724 {
725     WINE_FIXME("()\n");
726 }
727
728 static void CALLBACK MACRO_RegisterRoutine(LPCSTR dll_name, LPCSTR proc, LPCSTR args)
729 {
730     void               *fn = NULL;
731     int                 size;
732     WINHELP_DLL*        dll;
733
734     WINE_TRACE("(\"%s\", \"%s\", \"%s\")\n", dll_name, proc, args);
735
736     /* FIXME: are the registered DLLs global or linked to the current file ???
737      * We assume globals (as we did for macros, but is this really the case ???)
738      */
739     for (dll = Globals.dlls; dll; dll = dll->next)
740     {
741         if (!strcmp(dll->name, dll_name)) break;
742     }
743     if (!dll)
744     {
745         HANDLE hLib = LoadLibrary(dll_name);
746
747         /* FIXME: the library will not be unloaded until exit of program 
748          * We don't send the DW_TERM message
749          */
750         WINE_TRACE("Loading %s\n", dll_name);
751         /* FIXME: should look in the directory where current hlpfile
752          * is loaded from
753          */
754         if (hLib == NULL)
755         {
756             /* FIXME: internationalisation for error messages */
757             WINE_FIXME("Cannot find dll %s\n", dll_name);
758         }
759         else if ((dll = HeapAlloc(GetProcessHeap(), 0, sizeof(*dll))))
760         {
761             dll->hLib = hLib;
762             dll->name = StrDup(dll_name); /* FIXME: never freed */
763             dll->next = Globals.dlls;
764             Globals.dlls = dll;
765             dll->handler = (WINHELP_LDLLHandler)GetProcAddress(dll->hLib, "LDLLHandler");
766             dll->class = dll->handler ? (dll->handler)(DW_WHATMSG, 0, 0) : DC_NOMSG;
767             WINE_TRACE("Got class %x for DLL %s\n", dll->class, dll_name);
768             if (dll->class & DC_INITTERM) dll->handler(DW_INIT, 0, 0);
769             if (dll->class & DC_CALLBACKS) dll->handler(DW_CALLBACKS, (LONG_PTR)&Callbacks, 0);
770         }
771         else WINE_WARN("OOM\n");
772     }
773     if (dll && !(fn = GetProcAddress(dll->hLib, proc)))
774     {
775         /* FIXME: internationalisation for error messages */
776         WINE_FIXME("Cannot find proc %s in dll %s\n", dll_name, proc);
777     }
778
779     size = ++MACRO_NumLoaded * sizeof(struct MacroDesc);
780     if (!MACRO_Loaded) MACRO_Loaded = HeapAlloc(GetProcessHeap(), 0, size);
781     else MACRO_Loaded = HeapReAlloc(GetProcessHeap(), 0, MACRO_Loaded, size);
782     MACRO_Loaded[MACRO_NumLoaded - 1].name      = StrDup(proc); /* FIXME: never freed */
783     MACRO_Loaded[MACRO_NumLoaded - 1].alias     = NULL;
784     MACRO_Loaded[MACRO_NumLoaded - 1].isBool    = 0;
785     MACRO_Loaded[MACRO_NumLoaded - 1].arguments = StrDup(args); /* FIXME: never freed */
786     MACRO_Loaded[MACRO_NumLoaded - 1].fn        = fn;
787     WINE_TRACE("Added %s(%s) at %p\n", proc, args, fn);
788 }
789
790 static void CALLBACK MACRO_RemoveAccelerator(LONG u1, LONG u2)
791 {
792     WINE_FIXME("(%u, %u)\n", u1, u2);
793 }
794
795 static void CALLBACK MACRO_ResetMenu(void)
796 {
797     WINE_FIXME("()\n");
798 }
799
800 static void CALLBACK MACRO_SaveMark(LPCSTR str)
801 {
802     WINE_FIXME("(\"%s\")\n", str);
803 }
804
805 static void CALLBACK MACRO_Search(void)
806 {
807     WINHELP_CreateIndexWindow(TRUE);
808 }
809
810 void CALLBACK MACRO_SetContents(LPCSTR str, LONG u)
811 {
812     WINE_FIXME("(\"%s\", %u)\n", str, u);
813 }
814
815 static void CALLBACK MACRO_SetHelpOnFile(LPCSTR str)
816 {
817     HLPFILE_PAGE*       page = MACRO_CurrentWindow()->page;
818
819     WINE_TRACE("(\"%s\")\n", str);
820
821     HeapFree(GetProcessHeap(), 0, page->file->help_on_file);
822     page->file->help_on_file = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
823     if (page->file->help_on_file)
824         strcpy(page->file->help_on_file, str);
825 }
826
827 static void CALLBACK MACRO_SetPopupColor(LONG r, LONG g, LONG b)
828 {
829     HLPFILE_PAGE*       page = MACRO_CurrentWindow()->page;
830
831     WINE_TRACE("(%x, %x, %x)\n", r, g, b);
832     page->file->has_popup_color = TRUE;
833     page->file->popup_color = RGB(r, g, b);
834 }
835
836 static void CALLBACK MACRO_ShellExecute(LPCSTR str1, LPCSTR str2, LONG u1, LONG u2, LPCSTR str3, LPCSTR str4)
837 {
838     WINE_FIXME("(\"%s\", \"%s\", %u, %u, \"%s\", \"%s\")\n", str1, str2, u1, u2, str3, str4);
839 }
840
841 static void CALLBACK MACRO_ShortCut(LPCSTR str1, LPCSTR str2, LONG w, LONG l, LPCSTR str)
842 {
843     WINE_FIXME("(\"%s\", \"%s\", %x, %x, \"%s\")\n", str1, str2, w, l, str);
844 }
845
846 static void CALLBACK MACRO_TCard(LONG u)
847 {
848     WINE_FIXME("(%u)\n", u);
849 }
850
851 static void CALLBACK MACRO_Test(LONG u)
852 {
853     WINE_FIXME("(%u)\n", u);
854 }
855
856 static BOOL CALLBACK MACRO_TestALink(LPCSTR str)
857 {
858     WINE_FIXME("(\"%s\")\n", str);
859     return FALSE;
860 }
861
862 static BOOL CALLBACK MACRO_TestKLink(LPCSTR str)
863 {
864     WINE_FIXME("(\"%s\")\n", str);
865     return FALSE;
866 }
867
868 static void CALLBACK MACRO_UncheckItem(LPCSTR str)
869 {
870     WINE_FIXME("(\"%s\")\n", str);
871 }
872
873 static void CALLBACK MACRO_UpdateWindow(LPCSTR str1, LPCSTR str2)
874 {
875     WINE_FIXME("(\"%s\", \"%s\")\n", str1, str2);
876 }
877
878
879 /**************************************************/
880 /*               Macro table                      */
881 /**************************************************/
882
883 /* types:
884  *      U:      32 bit unsigned int
885  *      I:      32 bit signed int
886  *      S:      string
887  *      v:      unknown (32 bit entity)
888  */
889
890 static struct MacroDesc MACRO_Builtins[] = {
891     {"About",               NULL, 0, "",       MACRO_About},
892     {"AddAccelerator",      "AA", 0, "UUS",    MACRO_AddAccelerator},
893     {"ALink",               "AL", 0, "SUS",    MACRO_ALink},
894     {"Annotate",            NULL, 0, "",       MACRO_Annotate},
895     {"AppendItem",          NULL, 0, "SSSS",   MACRO_AppendItem},
896     {"Back",                NULL, 0, "",       MACRO_Back},
897     {"BackFlush",           "BF", 0, "",       MACRO_BackFlush},
898     {"BookmarkDefine",      NULL, 0, "",       MACRO_BookmarkDefine},
899     {"BookmarkMore",        NULL, 0, "",       MACRO_BookmarkMore},
900     {"BrowseButtons",       NULL, 0, "",       MACRO_BrowseButtons},
901     {"ChangeButtonBinding", "CBB",0, "SS",     MACRO_ChangeButtonBinding},
902     {"ChangeEnable",        "CE", 0, "SS",     MACRO_ChangeEnable},
903     {"ChangeItemBinding",   "CIB",0, "SS",     MACRO_ChangeItemBinding},
904     {"CheckItem",           "CI", 0, "S",      MACRO_CheckItem},
905     {"CloseSecondarys",     "CS", 0, "",       MACRO_CloseSecondarys},
906     {"CloseWindow",         "CW", 0, "S",      MACRO_CloseWindow},
907     {"Compare",             NULL, 0, "S",      MACRO_Compare},
908     {"Contents",            NULL, 0, "",       MACRO_Contents},
909     {"ControlPanel",        NULL, 0, "SSU",    MACRO_ControlPanel},
910     {"CopyDialog",          NULL, 0, "",       MACRO_CopyDialog},
911     {"CopyTopic",           "CT", 0, "",       MACRO_CopyTopic},
912     {"CreateButton",        "CB", 0, "SSS",    MACRO_CreateButton},
913     {"DeleteItem",          NULL, 0, "S",      MACRO_DeleteItem},
914     {"DeleteMark",          NULL, 0, "S",      MACRO_DeleteMark},
915     {"DestroyButton",       NULL, 0, "S",      MACRO_DestroyButton},
916     {"DisableButton",       "DB", 0, "S",      MACRO_DisableButton},
917     {"DisableItem",         "DI", 0, "S",      MACRO_DisableItem},
918     {"EnableButton",        "EB", 0, "S",      MACRO_EnableButton},
919     {"EnableItem",          "EI", 0, "S",      MACRO_EnableItem},
920     {"EndMPrint",           NULL, 0, "",       MACRO_EndMPrint},
921     {"ExecFile",            "EF", 0, "SSUS",   MACRO_ExecFile},
922     {"ExecProgram",         "EP", 0, "SU",     MACRO_ExecProgram},
923     {"Exit",                NULL, 0, "",       MACRO_Exit},
924     {"ExtAbleItem",         NULL, 0, "SU",     MACRO_ExtAbleItem},
925     {"ExtInsertItem",       NULL, 0, "SSSSUU", MACRO_ExtInsertItem},
926     {"ExtInsertMenu",       NULL, 0, "SSSUU",  MACRO_ExtInsertMenu},
927     {"FileExist",           "FE", 1, "S",      MACRO_FileExist},
928     {"FileOpen",            "FO", 0, "",       MACRO_FileOpen},
929     {"Find",                NULL, 0, "",       MACRO_Find},
930     {"Finder",              "FD", 0, "",       MACRO_Finder},
931     {"FloatingMenu",        NULL, 0, "",       MACRO_FloatingMenu},
932     {"Flush",               "FH", 0, "",       MACRO_Flush},
933     {"FocusWindow",         NULL, 0, "S",      MACRO_FocusWindow},
934     {"Generate",            NULL, 0, "SUU",    MACRO_Generate},
935     {"GotoMark",            NULL, 0, "S",      MACRO_GotoMark},
936     {"HelpOn",              NULL, 0, "",       MACRO_HelpOn},
937     {"HelpOnTop",           NULL, 0, "",       MACRO_HelpOnTop},
938     {"History",             NULL, 0, "",       MACRO_History},
939     {"InitMPrint",          NULL, 1, "",       MACRO_InitMPrint},
940     {"InsertItem",          NULL, 0, "SSSSU",  MACRO_InsertItem},
941     {"InsertMenu",          NULL, 0, "SSU",    MACRO_InsertMenu},
942     {"IfThen",              "IF", 0, "BS",     MACRO_IfThen},
943     {"IfThenElse",          "IE", 0, "BSS",    MACRO_IfThenElse},
944     {"IsBook",              NULL, 1, "",       MACRO_IsBook},
945     {"IsMark",              NULL, 1, "S",      MACRO_IsMark},
946     {"IsNotMark",           "NM", 1, "S",      MACRO_IsNotMark},
947     {"JumpContents",        NULL, 0, "SS",     MACRO_JumpContents},
948     {"JumpContext",         "JC", 0, "SSU",    MACRO_JumpContext},
949     {"JumpHash",            "JH", 0, "SSU",    MACRO_JumpHash},
950     {"JumpHelpOn",          NULL, 0, "",       MACRO_JumpHelpOn},
951     {"JumpID",              "JI", 0, "SS",     MACRO_JumpID},
952     {"JumpKeyword",         "JK", 0, "SSS",    MACRO_JumpKeyword},
953     {"KLink",               "KL", 0, "SUSS",   MACRO_KLink},
954     {"Menu",                "MU", 0, "",       MACRO_Menu},
955     {"MPrintHash",          NULL, 0, "U",      MACRO_MPrintHash},
956     {"MPrintID",            NULL, 0, "S",      MACRO_MPrintID},
957     {"Next",                NULL, 0, "",       MACRO_Next},
958     {"NoShow",              "NS", 0, "",       MACRO_NoShow},
959     {"PopupContext",        "PC", 0, "SU",     MACRO_PopupContext},
960     {"PopupHash",           NULL, 0, "SU",     MACRO_PopupHash},
961     {"PopupId",             "PI", 0, "SS",     MACRO_PopupId},
962     {"PositionWindow",      "PW", 0, "IIUUUS", MACRO_PositionWindow},
963     {"Prev",                NULL, 0, "",       MACRO_Prev},
964     {"Print",               NULL, 0, "",       MACRO_Print},
965     {"PrinterSetup",        NULL, 0, "",       MACRO_PrinterSetup},
966     {"RegisterRoutine",     "RR", 0, "SSS",    MACRO_RegisterRoutine},
967     {"RemoveAccelerator",   "RA", 0, "UU",     MACRO_RemoveAccelerator},
968     {"ResetMenu",           NULL, 0, "",       MACRO_ResetMenu},
969     {"SaveMark",            NULL, 0, "S",      MACRO_SaveMark},
970     {"Search",              NULL, 0, "",       MACRO_Search},
971     {"SetContents",         NULL, 0, "SU",     MACRO_SetContents},
972     {"SetHelpOnFile",       NULL, 0, "S",      MACRO_SetHelpOnFile},
973     {"SetPopupColor",       "SPC",0, "UUU",    MACRO_SetPopupColor},
974     {"ShellExecute",        "SE", 0, "SSUUSS", MACRO_ShellExecute},
975     {"ShortCut",            "SH", 0, "SSUUS",  MACRO_ShortCut},
976     {"TCard",               NULL, 0, "U",      MACRO_TCard},
977     {"Test",                NULL, 0, "U",      MACRO_Test},
978     {"TestALink",           NULL, 1, "S",      MACRO_TestALink},
979     {"TestKLink",           NULL, 1, "S",      MACRO_TestKLink},
980     {"UncheckItem",         "UI", 0, "S",      MACRO_UncheckItem},
981     {"UpdateWindow",        "UW", 0, "SS",     MACRO_UpdateWindow},
982     {NULL,                  NULL, 0, NULL,     NULL}
983 };
984
985 static int MACRO_DoLookUp(struct MacroDesc* start, const char* name, struct lexret* lr, unsigned len)
986 {
987     struct MacroDesc*   md;
988
989     for (md = start; md->name && len != 0; md++, len--)
990     {
991         if (strcasecmp(md->name, name) == 0 || (md->alias != NULL && strcasecmp(md->alias, name) == 0))
992         {
993             lr->proto = md->arguments;
994             lr->function = md->fn;
995             return md->isBool ? BOOL_FUNCTION : VOID_FUNCTION;
996         }
997     }
998     return EMPTY;
999 }
1000
1001 int MACRO_Lookup(const char* name, struct lexret* lr)
1002 {
1003     int ret;
1004
1005     if ((ret = MACRO_DoLookUp(MACRO_Builtins, name, lr, -1)) != EMPTY)
1006         return ret;
1007     if (MACRO_Loaded && (ret = MACRO_DoLookUp(MACRO_Loaded, name, lr, MACRO_NumLoaded)) != EMPTY)
1008         return ret;
1009
1010     lr->string = name;
1011     return IDENTIFIER;
1012 }