]> git.saurik.com Git - wxWidgets.git/blob - src/generic/helpxlp.cpp
fix for wxFrame's last focus bug (finally?)
[wxWidgets.git] / src / generic / helpxlp.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: helpxlp.cpp
3 // Purpose: Help system: wxHelp implementation
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "helpxlp.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/defs.h"
25 #endif
26
27 #include "wx/generic/helpxlp.h"
28
29 #if wxUSE_HELP
30 #include <time.h>
31
32 #ifdef __X__
33 #include <netdb.h>
34
35 #ifdef SUN_CC
36 #include <sysent.h>
37 #endif // SUN_CC
38 #ifdef ____HPUX__
39 #include <sys/unistd.h>
40 #endif // ____HPUX__
41 #endif // __X__
42
43 #include <string.h>
44
45 // Timeout in seconds
46 #define WX_HELP_TIMEOUT 15 /* was 30 */
47
48 // MAX path length
49 #define _MAXPATHLEN 500
50
51 // MAX length of Help descriptor
52 #define _MAX_HELP_LEN 500
53
54 #include "wx/generic/helpxlp.h"
55
56
57 #ifdef __WXMSW__
58 IMPLEMENT_CLASS(wxXLPHelpClient, wxDDEClient)
59 IMPLEMENT_CLASS(wxXLPHelpConnection, wxDDEConnection)
60 #else
61 IMPLEMENT_CLASS(wxXLPHelpClient, wxTCPClient)
62 IMPLEMENT_CLASS(wxXLPHelpConnection, wxTCPConnection)
63 #endif
64
65 IMPLEMENT_CLASS(wxXLPHelpController, wxHelpControllerBase)
66
67 // suppress annoying warning "'this' used in base member init list" (so what?)
68 #ifdef __VISUALC__
69 #pragma warning(disable: 4355)
70 #endif // Visual C++
71
72 wxXLPHelpController::wxXLPHelpController(void)
73 : helpClient(this)
74 {
75 helpFile = ""; helpServer = -1; helpHost = "";
76 helpRunning = FALSE; helpConnection = NULL;
77 }
78
79 #ifdef __VISUALC__
80 #pragma warning(default: 4355)
81 #endif // Visual C++
82
83 wxXLPHelpController::~wxXLPHelpController(void)
84 {
85 }
86
87 bool wxXLPHelpController::Initialize(const wxString& filename, int server)
88 {
89 #ifdef __X__
90 char host_buf[255];
91 if (wxGetHostName(host_buf, sizeof(host_buf)))
92 helpHost = host_buf;
93 else helpHost = "";
94 #endif
95
96 helpFile = filename;
97 helpServer = server;
98 return TRUE;
99 }
100
101 bool wxXLPHelpController::LoadFile(const wxString& file)
102 {
103 helpFile = file;
104
105 if (!helpRunning)
106 {
107 if (!Run())
108 return FALSE;
109 }
110 char buf[_MAX_HELP_LEN];
111 sprintf(buf, "f %s", (const char*) file);
112 if (helpConnection)
113 return helpConnection->Execute(buf);
114 else return FALSE;
115 }
116
117 bool wxXLPHelpController::DisplayContents(void)
118 {
119 if (!helpRunning)
120 {
121 if (!Run())
122 return FALSE;
123 }
124 if (helpConnection)
125 return helpConnection->Execute("s -1");
126 else
127 return FALSE;
128 }
129
130 bool wxXLPHelpController::DisplaySection(int section)
131 {
132 if (!helpRunning)
133 {
134 if (!Run())
135 return FALSE;
136 }
137 char buf[_MAX_HELP_LEN];
138 sprintf(buf, "s %d", section);
139 if (helpConnection)
140 return helpConnection->Execute(buf);
141 else return FALSE;
142 }
143
144 bool wxXLPHelpController::DisplayBlock(long block)
145 {
146 if (!helpRunning)
147 {
148 if (!Run())
149 return FALSE;
150 }
151 char buf[_MAX_HELP_LEN];
152 sprintf(buf, "b %ld", block);
153 if (helpConnection)
154 return helpConnection->Execute(buf);
155 else return FALSE;
156 }
157
158 bool wxXLPHelpController::KeywordSearch(const wxString& k)
159 {
160 if (!helpRunning)
161 {
162 if (!Run())
163 return FALSE;
164 }
165 char buf[500];
166 sprintf(buf, "k %s", (const char*) k);
167 if (helpConnection)
168 return helpConnection->Execute(buf);
169 else return FALSE;
170 }
171
172 bool wxXLPHelpController::Quit(void)
173 {
174 if (helpConnection)
175 return helpConnection->Disconnect(); // Calls OnQuit via OnDisconnect
176 else return TRUE;
177 }
178
179 void wxXLPHelpController::OnQuit(void)
180 {
181 }
182
183 bool wxXLPHelpController::Run(void)
184 {
185 #ifdef __X__
186 if (!helpFile || !helpHost || helpRunning)
187 return FALSE;
188 #endif
189 #ifdef __WXMSW__
190 if (!helpFile || helpRunning)
191 return FALSE;
192 #endif
193
194 time_t current_time;
195 #ifdef __X__
196 // Invent a server name that's likely to be unique but different from
197 // last time
198 (void)time(&current_time);
199 if (helpServer == -1)
200 helpServer = (int)(4000 + (current_time % 4000));
201 #else
202 // Only one instance of wxHelp at a time
203 helpServer = 4000;
204 #endif
205
206 char server[32];
207 sprintf(server, "%d", helpServer);
208 #ifdef __WXMSW__
209 // Only one instance of wxHelp under Windows.
210 // See if there's already an instance of wxHelp
211 if ((helpConnection = (wxXLPHelpConnection *)helpClient.MakeConnection(helpHost, server, "WXHELP")))
212 {
213 helpRunning = TRUE;
214 return TRUE;
215 }
216 #endif
217
218 // Start help process in server modus
219 // char *argv[] = {"wxhelp", "-server", server, NULL}; // HP compiler complains
220 char *argv[4];
221 argv[0] = "wxhelp";
222 argv[1] = "-server";
223 argv[2] = server;
224 argv[3] = NULL;
225
226 if (wxExecute((char **)argv) == FALSE)
227 return FALSE; // Maybe we should print a message?
228
229 time_t start_time;
230 (void)time(&start_time);
231 // Give it some time to respond
232 do {
233 wxSleep(1);
234 helpConnection = (wxXLPHelpConnection *)helpClient.MakeConnection(helpHost, server, "WXHELP");
235 (void)time(&current_time);
236 } while (!helpConnection && ((current_time - start_time) < WX_HELP_TIMEOUT));
237
238 if (helpConnection == NULL) {
239 char buf[100];
240 sprintf(buf, (const char *) _("Connection to wxHelp timed out in %d seconds"), WX_HELP_TIMEOUT);
241 (void)wxMessageBox(buf, _("Error"));
242 return FALSE;
243 }
244 helpRunning = TRUE;
245 return TRUE;
246 }
247
248 wxXLPHelpConnection::wxXLPHelpConnection(wxXLPHelpController *instance)
249 {
250 helpInstance = instance;
251 }
252
253 bool wxXLPHelpConnection::OnDisconnect(void)
254 {
255 helpInstance->OnQuit();
256 helpInstance->helpRunning = FALSE;
257 helpInstance->helpConnection = NULL;
258 helpInstance->helpServer = -1;
259 delete this;
260 return TRUE;
261 }
262
263 #endif // wxUSE_HELP