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