cb72236f |
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 | |
db6452be |
14 | #ifndef NO_HTMLHELP |
cb72236f |
15 | #include <htmlhelp.h> |
db6452be |
16 | #endif /* NO_HTMLHELP */ |
cb72236f |
17 | |
cb72236f |
18 | static int requested_help; |
db6452be |
19 | static char *help_path; |
20 | static int help_has_contents; |
21 | #ifndef NO_HTMLHELP |
9099600a |
22 | DECL_WINDOWS_FUNCTION(static, HWND, HtmlHelpA, (HWND, LPCSTR, UINT, DWORD)); |
db6452be |
23 | static char *chm_path; |
db6452be |
24 | #endif /* NO_HTMLHELP */ |
cb72236f |
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 | |
db6452be |
50 | #ifndef NO_HTMLHELP |
cb72236f |
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 = LoadLibrary("hhctrl.ocx"); |
9099600a |
59 | GET_WINDOWS_FUNCTION(dllHH, HtmlHelpA); |
60 | if (!p_HtmlHelpA) { |
61 | chm_path = NULL; |
62 | if (dllHH) |
cb72236f |
63 | FreeLibrary(dllHH); |
64 | } |
cb72236f |
65 | } |
db6452be |
66 | #endif /* NO_HTMLHELP */ |
cb72236f |
67 | } |
68 | |
69 | void shutdown_help(void) |
70 | { |
1b2214cb |
71 | /* Nothing to do currently. |
72 | * (If we were running HTML Help single-threaded, this is where we'd |
73 | * call HH_UNINITIALIZE.) */ |
cb72236f |
74 | } |
75 | |
76 | int has_help(void) |
77 | { |
78 | /* |
79 | * FIXME: it would be nice here to disregard help_path on |
80 | * platforms that didn't have WINHLP32. But that's probably |
81 | * unrealistic, since even Vista will have it if the user |
82 | * specifically downloads it. |
83 | */ |
db6452be |
84 | return (help_path |
85 | #ifndef NO_HTMLHELP |
86 | || chm_path |
87 | #endif /* NO_HTMLHELP */ |
88 | ); |
cb72236f |
89 | } |
90 | |
91 | void launch_help(HWND hwnd, const char *topic) |
92 | { |
93 | if (topic) { |
94 | int colonpos = strcspn(topic, ":"); |
95 | |
db6452be |
96 | #ifndef NO_HTMLHELP |
cb72236f |
97 | if (chm_path) { |
98 | char *fname; |
99 | assert(topic[colonpos] != '\0'); |
100 | fname = dupprintf("%s::/%s.html>main", chm_path, |
101 | topic + colonpos + 1); |
9099600a |
102 | p_HtmlHelpA(hwnd, fname, HH_DISPLAY_TOPIC, 0); |
cb72236f |
103 | sfree(fname); |
db6452be |
104 | } else |
105 | #endif /* NO_HTMLHELP */ |
106 | if (help_path) { |
cb72236f |
107 | char *cmd = dupprintf("JI(`',`%.*s')", colonpos, topic); |
108 | WinHelp(hwnd, help_path, HELP_COMMAND, (DWORD)cmd); |
109 | sfree(cmd); |
110 | } |
111 | } else { |
db6452be |
112 | #ifndef NO_HTMLHELP |
cb72236f |
113 | if (chm_path) { |
9099600a |
114 | p_HtmlHelpA(hwnd, chm_path, HH_DISPLAY_TOPIC, 0); |
db6452be |
115 | } else |
116 | #endif /* NO_HTMLHELP */ |
117 | if (help_path) { |
cb72236f |
118 | WinHelp(hwnd, help_path, |
119 | help_has_contents ? HELP_FINDER : HELP_CONTENTS, 0); |
120 | } |
121 | } |
122 | requested_help = TRUE; |
123 | } |
124 | |
125 | void quit_help(HWND hwnd) |
126 | { |
127 | if (requested_help) { |
db6452be |
128 | #ifndef NO_HTMLHELP |
cb72236f |
129 | if (chm_path) { |
9099600a |
130 | p_HtmlHelpA(NULL, NULL, HH_CLOSE_ALL, 0); |
db6452be |
131 | } else |
132 | #endif /* NO_HTMLHELP */ |
133 | if (help_path) { |
cb72236f |
134 | WinHelp(hwnd, help_path, HELP_QUIT, 0); |
135 | } |
136 | requested_help = FALSE; |
137 | } |
138 | } |