]> git.saurik.com Git - wxWidgets.git/blob - src/generic/helpxlp.cpp
*** empty log message ***
[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 #if !USE_SHARED_LIBRARY
57
58 #ifdef __WXMSW__
59 IMPLEMENT_CLASS(wxXLPHelpClient, wxDDEClient)
60 IMPLEMENT_CLASS(wxXLPHelpConnection, wxDDEConnection)
61 #else
62 IMPLEMENT_CLASS(wxXLPHelpClient, wxTCPClient)
63 IMPLEMENT_CLASS(wxXLPHelpConnection, wxTCPConnection)
64 #endif
65
66 IMPLEMENT_CLASS(wxXLPHelpController, wxHelpControllerBase)
67 #endif
68
69 // suppress annoying warning "'this' used in base member init list" (so what?)
70 #ifdef _MSC_VER
71 #pragma warning(disable: 4355)
72 #endif // Visual C++
73
74 wxXLPHelpController::wxXLPHelpController(void)
75 : helpClient(this)
76 {
77 helpFile = ""; helpServer = -1; helpHost = "";
78 helpRunning = FALSE; helpConnection = NULL;
79 }
80
81 #ifdef _MSC_VER
82 #pragma warning(default: 4355)
83 #endif // Visual C++
84
85 wxXLPHelpController::~wxXLPHelpController(void)
86 {
87 }
88
89 bool wxXLPHelpController::Initialize(const wxString& filename, int server)
90 {
91 #ifdef __X__
92 char host_buf[255];
93 if (wxGetHostName(host_buf, sizeof(host_buf)))
94 helpHost = host_buf;
95 else helpHost = "";
96 #endif
97
98 helpFile = filename;
99 helpServer = server;
100 wxIPCInitialize();
101 return TRUE;
102 }
103
104 bool wxXLPHelpController::LoadFile(const wxString& file)
105 {
106 helpFile = file;
107
108 if (!helpRunning)
109 {
110 if (!Run())
111 return FALSE;
112 }
113 char buf[_MAX_HELP_LEN];
114 sprintf(buf, "f %s", (const char*) file);
115 if (helpConnection)
116 return helpConnection->Execute(buf);
117 else return FALSE;
118 }
119
120 bool wxXLPHelpController::DisplayContents(void)
121 {
122 if (!helpRunning)
123 {
124 if (!Run())
125 return FALSE;
126 }
127 if (helpConnection)
128 return helpConnection->Execute("s -1");
129 else
130 return FALSE;
131 }
132
133 bool wxXLPHelpController::DisplaySection(int section)
134 {
135 if (!helpRunning)
136 {
137 if (!Run())
138 return FALSE;
139 }
140 char buf[_MAX_HELP_LEN];
141 sprintf(buf, "s %d", section);
142 if (helpConnection)
143 return helpConnection->Execute(buf);
144 else return FALSE;
145 }
146
147 bool wxXLPHelpController::DisplayBlock(long block)
148 {
149 if (!helpRunning)
150 {
151 if (!Run())
152 return FALSE;
153 }
154 char buf[_MAX_HELP_LEN];
155 sprintf(buf, "b %ld", block);
156 if (helpConnection)
157 return helpConnection->Execute(buf);
158 else return FALSE;
159 }
160
161 bool wxXLPHelpController::KeywordSearch(const wxString& k)
162 {
163 if (!helpRunning)
164 {
165 if (!Run())
166 return FALSE;
167 }
168 char buf[500];
169 sprintf(buf, "k %s", (const char*) k);
170 if (helpConnection)
171 return helpConnection->Execute(buf);
172 else return FALSE;
173 }
174
175 bool wxXLPHelpController::Quit(void)
176 {
177 if (helpConnection)
178 return helpConnection->Disconnect(); // Calls OnQuit via OnDisconnect
179 else return TRUE;
180 }
181
182 void wxXLPHelpController::OnQuit(void)
183 {
184 }
185
186 bool wxXLPHelpController::Run(void)
187 {
188 #ifdef __X__
189 if (!helpFile || !helpHost || helpRunning)
190 return FALSE;
191 #endif
192 #ifdef __WXMSW__
193 if (!helpFile || helpRunning)
194 return FALSE;
195 #endif
196
197 time_t current_time;
198 #ifdef __X__
199 // Invent a server name that's likely to be unique but different from
200 // last time
201 (void)time(&current_time);
202 if (helpServer == -1)
203 helpServer = (int)(4000 + (current_time % 4000));
204 #else
205 // Only one instance of wxHelp at a time
206 helpServer = 4000;
207 #endif
208
209 char server[32];
210 sprintf(server, "%d", helpServer);
211 #ifdef __WXMSW__
212 // Only one instance of wxHelp under Windows.
213 // See if there's already an instance of wxHelp
214 if ((helpConnection = (wxXLPHelpConnection *)helpClient.MakeConnection(helpHost, server, "WXHELP")))
215 {
216 helpRunning = TRUE;
217 return TRUE;
218 }
219 #endif
220
221 // Start help process in server modus
222 // char *argv[] = {"wxhelp", "-server", server, NULL}; // HP compiler complains
223 char *argv[4];
224 argv[0] = "wxhelp";
225 argv[1] = "-server";
226 argv[2] = server;
227 argv[3] = NULL;
228
229 if (wxExecute((char **)argv) == FALSE)
230 return FALSE; // Maybe we should print a message?
231
232 time_t start_time;
233 (void)time(&start_time);
234 // Give it some time to respond
235 do {
236 wxSleep(1);
237 helpConnection = (wxXLPHelpConnection *)helpClient.MakeConnection(helpHost, server, "WXHELP");
238 (void)time(&current_time);
239 } while (!helpConnection && ((current_time - start_time) < WX_HELP_TIMEOUT));
240
241 if (helpConnection == NULL) {
242 char buf[100];
243 sprintf(buf, (const char *) _("Connection to wxHelp timed out in %d seconds"), WX_HELP_TIMEOUT);
244 (void)wxMessageBox(buf, _("Error"));
245 return FALSE;
246 }
247 helpRunning = TRUE;
248 return TRUE;
249 }
250
251 wxXLPHelpConnection::wxXLPHelpConnection(wxXLPHelpController *instance)
252 {
253 helpInstance = instance;
254 }
255
256 bool wxXLPHelpConnection::OnDisconnect(void)
257 {
258 helpInstance->OnQuit();
259 helpInstance->helpRunning = FALSE;
260 helpInstance->helpConnection = NULL;
261 helpInstance->helpServer = -1;
262 delete this;
263 return TRUE;
264 }
265
266 #endif // wxUSE_HELP