]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - windows/winhelp.c
first pass
[PuTTY.git] / windows / winhelp.c
1 /*
2  * winhelp.c: centralised functions to launch Windows help files,
3  * and to decide whether to use .HLP or .CHM help in any given
4  * situation.
5  */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <assert.h>
11
12 #include "putty.h"
13
14 #ifndef NO_HTMLHELP
15 #include <htmlhelp.h>
16 #endif /* NO_HTMLHELP */
17
18 static int requested_help;
19 static char *help_path;
20 static int help_has_contents;
21 #ifndef NO_HTMLHELP
22 DECL_WINDOWS_FUNCTION(static, HWND, HtmlHelpA, (HWND, LPCSTR, UINT, DWORD));
23 static char *chm_path;
24 #endif /* NO_HTMLHELP */
25
26 void init_help(void)
27 {
28     char b[2048], *p, *q, *r;
29     FILE *fp;
30
31     GetModuleFileName(NULL, b, sizeof(b) - 1);
32     r = b;
33     p = strrchr(b, '\\');
34     if (p && p >= r) r = p+1;
35     q = strrchr(b, ':');
36     if (q && q >= r) r = q+1;
37     strcpy(r, PUTTY_HELP_FILE);
38     if ( (fp = fopen(b, "r")) != NULL) {
39         help_path = dupstr(b);
40         fclose(fp);
41     } else
42         help_path = NULL;
43     strcpy(r, PUTTY_HELP_CONTENTS);
44     if ( (fp = fopen(b, "r")) != NULL) {
45         help_has_contents = TRUE;
46         fclose(fp);
47     } else
48         help_has_contents = FALSE;
49
50 #ifndef NO_HTMLHELP
51     strcpy(r, PUTTY_CHM_FILE);
52     if ( (fp = fopen(b, "r")) != NULL) {
53         chm_path = dupstr(b);
54         fclose(fp);
55     } else
56         chm_path = NULL;
57     if (chm_path) {
58         HINSTANCE dllHH = load_system32_dll("hhctrl.ocx");
59         GET_WINDOWS_FUNCTION(dllHH, HtmlHelpA);
60         if (!p_HtmlHelpA) {
61             sfree(chm_path);
62             chm_path = NULL;
63             if (dllHH)
64                 FreeLibrary(dllHH);
65         }
66     }
67 #endif /* NO_HTMLHELP */
68 }
69
70 void shutdown_help(void)
71 {
72     /* Nothing to do currently.
73      * (If we were running HTML Help single-threaded, this is where we'd
74      * call HH_UNINITIALIZE.) */
75 }
76
77 int has_help(void)
78 {
79     /*
80      * FIXME: it would be nice here to disregard help_path on
81      * platforms that didn't have WINHLP32. But that's probably
82      * unrealistic, since even Vista will have it if the user
83      * specifically downloads it.
84      */
85     return (help_path != NULL
86 #ifndef NO_HTMLHELP
87             || chm_path
88 #endif /* NO_HTMLHELP */
89            );
90 }
91
92 void launch_help(HWND hwnd, const char *topic)
93 {
94     if (topic) {
95         int colonpos = strcspn(topic, ":");
96
97 #ifndef NO_HTMLHELP
98         if (chm_path) {
99             char *fname;
100             assert(topic[colonpos] != '\0');
101             fname = dupprintf("%s::/%s.html>main", chm_path,
102                               topic + colonpos + 1);
103             p_HtmlHelpA(hwnd, fname, HH_DISPLAY_TOPIC, 0);
104             sfree(fname);
105         } else
106 #endif /* NO_HTMLHELP */
107         if (help_path) {
108             char *cmd = dupprintf("JI(`',`%.*s')", colonpos, topic);
109             WinHelp(hwnd, help_path, HELP_COMMAND, (ULONG_PTR)cmd);
110             sfree(cmd);
111         }
112     } else {
113 #ifndef NO_HTMLHELP
114         if (chm_path) {
115             p_HtmlHelpA(hwnd, chm_path, HH_DISPLAY_TOPIC, 0);
116         } else
117 #endif /* NO_HTMLHELP */
118         if (help_path) {
119             WinHelp(hwnd, help_path,
120                     help_has_contents ? HELP_FINDER : HELP_CONTENTS, 0);
121         }
122     }
123     requested_help = TRUE;
124 }
125
126 void quit_help(HWND hwnd)
127 {
128     if (requested_help) {
129 #ifndef NO_HTMLHELP
130         if (chm_path) {
131             p_HtmlHelpA(NULL, NULL, HH_CLOSE_ALL, 0);
132         } else
133 #endif /* NO_HTMLHELP */
134         if (help_path) {
135             WinHelp(hwnd, help_path, HELP_QUIT, 0);
136         }
137         requested_help = FALSE;
138     }
139 }