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