| 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 | #include "wx/intl.h" |
| 29 | #include "wx/msgdlg.h" |
| 30 | |
| 31 | #if wxUSE_HELP |
| 32 | #include <time.h> |
| 33 | |
| 34 | #ifdef __X__ |
| 35 | #include <netdb.h> |
| 36 | |
| 37 | #ifdef SUN_CC |
| 38 | #include <sysent.h> |
| 39 | #endif // SUN_CC |
| 40 | #ifdef ____HPUX__ |
| 41 | #include <sys/unistd.h> |
| 42 | #endif // ____HPUX__ |
| 43 | #endif // __X__ |
| 44 | |
| 45 | #include <string.h> |
| 46 | |
| 47 | // Timeout in seconds |
| 48 | #define WX_HELP_TIMEOUT 15 /* was 30 */ |
| 49 | |
| 50 | // MAX path length |
| 51 | #define _MAXPATHLEN 500 |
| 52 | |
| 53 | // MAX length of Help descriptor |
| 54 | #define _MAX_HELP_LEN 500 |
| 55 | |
| 56 | #include "wx/generic/helpxlp.h" |
| 57 | |
| 58 | |
| 59 | #ifdef __WXMSW__ |
| 60 | IMPLEMENT_CLASS(wxXLPHelpClient, wxDDEClient) |
| 61 | IMPLEMENT_CLASS(wxXLPHelpConnection, wxDDEConnection) |
| 62 | #else |
| 63 | IMPLEMENT_CLASS(wxXLPHelpClient, wxTCPClient) |
| 64 | IMPLEMENT_CLASS(wxXLPHelpConnection, wxTCPConnection) |
| 65 | #endif |
| 66 | |
| 67 | IMPLEMENT_CLASS(wxXLPHelpController, wxHelpControllerBase) |
| 68 | |
| 69 | // suppress annoying warning "'this' used in base member init list" (so what?) |
| 70 | #ifdef __VISUALC__ |
| 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 __VISUALC__ |
| 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 | return TRUE; |
| 101 | } |
| 102 | |
| 103 | bool 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 | |
| 119 | bool wxXLPHelpController::DisplayContents(void) |
| 120 | { |
| 121 | if (!helpRunning) |
| 122 | { |
| 123 | if (!Run()) |
| 124 | return FALSE; |
| 125 | } |
| 126 | if (helpConnection) |
| 127 | return helpConnection->Execute("s -1"); |
| 128 | else |
| 129 | return FALSE; |
| 130 | } |
| 131 | |
| 132 | bool wxXLPHelpController::DisplaySection(int section) |
| 133 | { |
| 134 | if (!helpRunning) |
| 135 | { |
| 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 | |
| 146 | bool wxXLPHelpController::DisplayBlock(long block) |
| 147 | { |
| 148 | if (!helpRunning) |
| 149 | { |
| 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 | |
| 160 | bool wxXLPHelpController::KeywordSearch(const wxString& k) |
| 161 | { |
| 162 | if (!helpRunning) |
| 163 | { |
| 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 | |
| 174 | bool wxXLPHelpController::Quit(void) |
| 175 | { |
| 176 | if (helpConnection) |
| 177 | return helpConnection->Disconnect(); // Calls OnQuit via OnDisconnect |
| 178 | else return TRUE; |
| 179 | } |
| 180 | |
| 181 | void wxXLPHelpController::OnQuit(void) |
| 182 | { |
| 183 | } |
| 184 | |
| 185 | bool wxXLPHelpController::Run(void) |
| 186 | { |
| 187 | #ifdef __X__ |
| 188 | if (!helpFile || !helpHost || helpRunning) |
| 189 | return FALSE; |
| 190 | #endif |
| 191 | #ifdef __WXMSW__ |
| 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(¤t_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); |
| 210 | #ifdef __WXMSW__ |
| 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(¤t_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 | |
| 250 | wxXLPHelpConnection::wxXLPHelpConnection(wxXLPHelpController *instance) |
| 251 | { |
| 252 | helpInstance = instance; |
| 253 | } |
| 254 | |
| 255 | bool 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 | |
| 265 | #endif // wxUSE_HELP |