1 /*-*- c++ -*-********************************************************
2 * wxexthlp.cpp - an external help controller for wxWindows *
4 * (C) 1998 by Karsten Ballüder (Ballueder@usa.net) *
7 *******************************************************************/
9 # pragma implementation "wxexthlp.h"
13 #include "wx/helpbase.h"
14 #include "wx/generic/helpext.h"
15 #include "wx/string.h"
25 class wxExtHelpMapEntry
: public wxObject
31 wxExtHelpMapEntry(int iid
, wxString
const &iurl
, wxString
const &idoc
)
32 { id
= iid
; url
= iurl
; doc
= idoc
; }
38 wxBusyCursor() { wxBeginBusyCursor(); }
39 ~wxBusyCursor() { wxEndBusyCursor(); }
42 IMPLEMENT_CLASS(wxExtHelpController
, wxHelpControllerBase
)
45 This class implements help via an external browser.
46 It requires the name of a directory containing the documentation
47 and a file mapping numerical Section numbers to relative URLS.
50 wxExtHelpController::wxExtHelpController(void)
53 m_BrowserName
= WXEXTHELP_DEFAULTBROWSER
;
54 m_BrowserIsNetscape
= WXEXTHELP_DEFAULTBROWSER_IS_NETSCAPE
;
57 char *browser
= getenv(WXEXTHELP_ENVVAR_BROWSER
);
60 m_BrowserName
= browser
;
61 browser
= getenv(WXEXTHELP_ENVVAR_BROWSERISNETSCAPE
);
62 m_BrowserIsNetscape
= browser
&& (atoi(browser
) != 0);
67 wxExtHelpController::DeleteList(void)
71 wxNode
*node
= m_MapList
->First();
74 delete (wxExtHelpMapEntry
*)node
->Data();
76 node
= m_MapList
->First();
83 wxExtHelpController::~wxExtHelpController(void)
89 wxExtHelpController::SetBrowser(wxString
const & browsername
, bool isNetscape
)
91 m_BrowserName
= browsername
;
92 m_BrowserIsNetscape
= isNetscape
;
95 /** This must be called to tell the controller where to find the
97 @param file - NOT a filename, but a directory name.
98 @return true on success
101 wxExtHelpController::Initialize(const wxString
& file
)
103 return LoadFile(file
);
108 wxExtHelpController::LoadFile(const wxString
& ifile
= "")
110 wxString mapFile
, file
, url
, doc
;
112 char buffer
[WXEXTHELP_BUFLEN
];
114 wxBusyCursor b
; // display a busy cursor
116 if(! ifile
.IsEmpty())
119 if(! wxIsAbsolutePath(file
))
121 file
= wxGetWorkingDirectory();
122 file
<< WXEXTHELP_SEPARATOR
<< ifile
;
127 if(! wxDirExists(file
))
130 mapFile
<< file
<< WXEXTHELP_SEPARATOR
<< WXEXTHELP_MAPFILE
;
132 else // try to reload old file
135 if(! wxFileExists(mapFile
))
139 m_MapList
= new wxList
;
142 FILE *input
= fopen(mapFile
.c_str(),"rt");
147 if(fgets(buffer
,WXEXTHELP_BUFLEN
,input
) && *buffer
!= WXEXTHELP_COMMENTCHAR
)
149 len
= strlen(buffer
);
150 if(buffer
[len
-1] == '\n')
151 buffer
[len
-1] = '\0'; // cut of trailing newline
152 if(sscanf(buffer
,"%d", &id
) != 1)
154 for(i
=0; isdigit(buffer
[i
])||isspace(buffer
[i
])||buffer
[i
]=='-'; i
++)
155 ; // find begin of URL
157 while(buffer
[i
] && ! isspace(buffer
[i
]) && buffer
[i
] !=
158 WXEXTHELP_COMMENTCHAR
)
160 while(buffer
[i
] && buffer
[i
] != WXEXTHELP_COMMENTCHAR
)
164 doc
= (buffer
+ i
+ 1); // skip the comment character
165 m_MapList
->Append(new wxExtHelpMapEntry(id
,url
,doc
));
168 }while(! feof(input
));
171 m_MapFile
= file
; // now it's valid
176 wxExtHelpController::CallBrowser(wxString
const &relativeURL
)
178 wxBusyCursor b
; // display a busy cursor
181 if(m_BrowserIsNetscape
) // try re-loading first
184 wxGetHomeDir(&lockfile
);
185 lockfile
<< WXEXTHELP_SEPARATOR
<< ".netscape/lock";
187 if(lstat(lockfile
.c_str(), &statbuf
) == 0)
188 // cannot use wxFileExists, because it's a link pointing to a
189 // non-existing location if(wxFileExists(lockfile))
192 command
<< m_BrowserName
<< " -remote openURL("
193 << "file://" << m_MapFile
194 << WXEXTHELP_SEPARATOR
<< relativeURL
<< ")";
195 success
= wxExecute(command
);
196 if(success
!= 0 ) // returns PID on success
200 command
= m_BrowserName
;
201 command
<< " file://"
202 << m_MapFile
<< WXEXTHELP_SEPARATOR
<< relativeURL
;
203 return wxExecute(command
) != 0;
207 wxExtHelpController::DisplayContents(void)
211 wxBusyCursor b
; // display a busy cursor
212 return KeywordSearch("");
216 wxExtHelpController::DisplaySection(int sectionNo
)
221 wxBusyCursor b
; // display a busy cursor
222 wxNode
*node
= m_MapList
->First();
223 wxExtHelpMapEntry
*entry
;
226 entry
= (wxExtHelpMapEntry
*)node
->Data();
227 if(entry
->id
== sectionNo
)
228 return CallBrowser(entry
->url
);
235 wxExtHelpController::DisplayBlock(long blockNo
)
237 return DisplaySection((int)blockNo
);
241 wxExtHelpController::KeywordSearch(const wxString
& k
)
246 wxBusyCursor b
; // display a busy cursor
247 wxString
*choices
= new wxString
[m_NumOfEntries
];
248 wxString
*urls
= new wxString
[m_NumOfEntries
];
249 wxString compA
, compB
;
253 bool showAll
= k
.IsEmpty();
254 wxNode
*node
= m_MapList
->First();
255 wxExtHelpMapEntry
*entry
;
257 compA
= k
; compA
.LowerCase(); // we compare case insensitive
260 entry
= (wxExtHelpMapEntry
*)node
->Data();
261 compB
= entry
->doc
; compB
.LowerCase();
262 if((showAll
|| compB
.Contains(k
)) && ! compB
.IsEmpty())
264 urls
[idx
] = entry
->url
;
266 // choices[idx] = (**i).doc.Contains((**i).doc.Before(WXEXTHELP_COMMENTCHAR));
267 //if(choices[idx].IsEmpty()) // didn't contain the ';'
268 // choices[idx] = (**i).doc;
270 for(j
=0;entry
->doc
.c_str()[j
]
271 && entry
->doc
.c_str()[j
] != WXEXTHELP_COMMENTCHAR
; j
++)
272 choices
[idx
] << entry
->doc
.c_str()[j
];
279 rc
= CallBrowser(urls
[0]);
282 wxMessageBox(_("No entries found."));
287 idx
= wxGetSingleChoiceIndex(showAll
? _("Help Index") : _("Relevant entries:"),
288 showAll
? _("Help Index") : _("Entries found"),
291 rc
= CallBrowser(urls
[idx
]);
303 wxExtHelpController::Quit(void)
309 wxExtHelpController::OnQuit(void)