-/*-*- c++ -*-********************************************************
- * wxexthlp.cpp - an external help controller for wxWindows *
- * *
- * (C) 1998 by Karsten Ballüder (Ballueder@usa.net) *
- * *
- * $Id$
- *******************************************************************/
-#ifdef __GNUG__
-# pragma implementation "wxexthlp.h"
+/////////////////////////////////////////////////////////////////////////////
+// Name: src/generic/helpext.cpp
+// Purpose: an external help controller for wxWidgets
+// Author: Karsten Ballueder
+// Modified by:
+// Created: 04/01/98
+// RCS-ID: $Id$
+// Copyright: (c) Karsten Ballueder
+// Licence: wxWindows licence
+/////////////////////////////////////////////////////////////////////////////
+
+#include "wx/wxprec.h"
+
+#ifdef __BORLANDC__
+ #pragma hdrstop
#endif
-#include "wx/setup.h"
-#include "wx/helpbase.h"
-#include "wx/generic/helpext.h"
-#include "wx/string.h"
-#include "wx/utils.h"
-#include <stdio.h>
-#include <ctype.h>
-#include <sys/stat.h>
-#include <unistd.h>
-
-
-#define WXEXTHELP_INCLUDE_KBLIST
-/**
- ** This class uses kbList, a simple linked list. Until someone
- ** rewrites it to use wxList instead, I include the relevant bits and
- ** pieces of kbList here. It's a tiny class anyway, so it won't make
- ** a big difference. The comments probably take up more space than
- ** its code.
- **/
-
-#ifdef WXEXTHELP_INCLUDE_KBLIST
-
-/********************* kbList.h, verbose copy: ****************************/
-/**@name Double linked list implementation. */
-//@{
-
-/** kbListNode is a class used by kbList. It represents a single
- element in the list. It is not intended for general use outside
- kbList functions.
-*/
-struct kbListNode
-{
- /// pointer to next node or NULL
- struct kbListNode *next;
- /// pointer to previous node or NULL
- struct kbListNode *prev;
- /// pointer to the actual data
- void *element;
- /** Constructor - it automatically links the node into the list, if
- the iprev, inext parameters are given.
- @param ielement pointer to the data for this node (i.e. the data itself)
- @param iprev if not NULL, use this as previous element in list
- @param inext if not NULL, use this as next element in list
- */
- kbListNode( void *ielement,
- kbListNode *iprev = NULL,
- kbListNode *inext = NULL);
- /// Destructor.
- ~kbListNode();
-};
-
-/** The main list class, handling void pointers as data.
- */
-
-class kbList
-{
-public:
- /// An iterator class for kbList, just like for the STL classes.
- class iterator
- {
- protected:
- /// the node to which this iterator points
- kbListNode *node;
- friend class kbList;
- public:
- /** Constructor.
- @param n if not NULL, the node to which to point
- */
- iterator(kbListNode *n = NULL);
- /** Dereference operator.
- @return the data pointer of the node belonging to this
- iterator
- */
- void * operator*();
-
- /** This operator allows us to write if(i). It is <em>not</em> a
- dereference operator and the result is always useless apart
- from its logical value!
- */
- operator void*() const { return node == NULL ? (void*)0 : (void*)(-1); }
-
- /** Increment operator - prefix, goes to next node in list.
- @return itself
- */
- iterator & operator++();
-
- /** Decrement operator - prefix, goes to previous node in list.
- @return itself
- */
- iterator & operator--();
-
- /** Increment operator - prefix, goes to next node in list.
- @return itself
- */
- iterator & operator++(int); //postfix
-
- /** Decrement operator - prefix, goes to previous node in list.
- @return itself
- */
- iterator & operator--(int); //postfix
-
- /** Comparison operator.
- @return true if not equal.
- */
- bool operator !=(iterator const &) const;
-
- /* Comparison operator.
- @return true if equal
- */
- bool operator ==(iterator const &) const;
-
- /** Returns a pointer to the node associated with this iterator.
- This function is not for general use and should be
- protected. However, if protected, it cannot be called from
- derived classes' iterators. (Is this a bug in gcc/egcs?)
- @return the node pointer
- */
- inline kbListNode * Node(void) const
- { return node; }
- };
-
- /** Constructor.
- @param ownsEntriesFlag if true, the list owns the entries and
- will issue a delete on each of them when deleting them. If
- false, the entries themselves will not get deleted. Do not use
- this with array types!
- */
- kbList(bool ownsEntriesFlag = true);
-
- /** Destructor.
- If entries are owned, they will all get deleted from here.
- */
- ~kbList();
-
- /** Tell list whether it owns objects. If owned, they can be
- deleted by list. See the constructor for more details.
- @param ownsflag if true, list will own entries
- */
- void ownsObjects(bool ownsflag = true)
- { ownsEntries = ownsflag; }
-
- /** Query whether list owns entries.
- @return true if list owns entries
- */
- bool ownsObjects(void)
- { return ownsEntries; }
-
- /** Add an entry at the end of the list.
- @param element pointer to data
- */
- void push_back(void *element);
-
- /** Add an entry at the head of the list.
- @param element pointer to data
- */
- void push_front(void *element);
-
- /** Get element from end of the list and delete it.
- NOTE: In this case the element's data will not get deleted by
- the list. It is the responsibility of the caller to free it.
- @return the element data
- */
- void *pop_back(void);
-
- /** Get element from head of the list and delete it.
- NOTE: In this case the element's data will not get deleted by
- the list. It is the responsibility of the caller to free it.
- @return the element data
- */
- void *pop_front(void);
-
- /** Insert an element into the list.
- @param i an iterator pointing to the element, before which the new one should be inserted
- @param element the element data
- */
- void insert(iterator & i, void *element);
-
- /** Remove an element from the list _without_ deleting the object.
- @param i iterator pointing to the element to be deleted
- @return the value of the element just removed
- */
- void *remove(iterator& i) { void *p = *i; doErase(i); return p; }
-
- /** Erase an element, move iterator to following element.
- @param i iterator pointing to the element to be deleted
- */
- void erase(iterator & i) { deleteContent(i); doErase(i); }
-
- /* Get head of list.
- @return iterator pointing to head of list
- */
- iterator begin(void) const;
-
- /* Get end of list.
- @return iterator pointing after the end of the list. This is an
- invalid iterator which cannot be dereferenced or decremented. It is
- only of use in comparisons. NOTE: this is different from STL!
- @see tail
- */
- iterator end(void) const;
-
- /* Get last element in list.
- @return iterator pointing to the last element in the list.
- @see end
- */
- iterator tail(void) const;
-
- /* Get the number of elements in the list.
- @return number of elements in the list
- */
- unsigned size(void) const;
-
- /* Query whether list is empty.
- @return true if list is empty
- */
- inline bool empty(void) const
- { return first == NULL ; }
-
-protected:
- /// if true, list owns entries
- bool ownsEntries;
- /// pointer to first element in list
- kbListNode *first;
- /// pointer to last element in list
- kbListNode *last;
-protected:
- /** Erase an element, move iterator to following element.
- @param i iterator pointing to the element to be deleted
- */
- void doErase(iterator & i);
-
- /** Deletes the actual content if ownsflag is set.
- param iterator i
- */
- inline void deleteContent(iterator i)
- { if(ownsEntries) delete *i; }
-
-
-private:
- /// forbid copy construction
- kbList(kbList const &foo);
- /// forbid assignments
- kbList& operator=(const kbList& foo);
-};
-
-/** Macro to define a kbList with a given name, having elements of
- pointer to the given type. I.e. KBLIST_DEFINE(Int,int) would
- create a kbListInt type holding int pointers.
-*/
-#define KBLIST_DEFINE(name,type) \
-class name : public kbList \
-{ \
-public: \
- class iterator : public kbList::iterator \
- { \
- protected: \
- inline iterator(kbList::iterator const & i) \
- { node = i.Node(); } \
- friend class name; \
- public: \
- inline iterator(kbListNode *n = NULL) \
- : kbList::iterator(n) {} \
- inline type * operator*() \
- /* the cast is needed for MS VC++ 5.0 */ \
- { return (type *)((kbList::iterator *)this)->operator*() ; } \
- }; \
- inline name(bool ownsEntriesFlag = TRUE) \
- : kbList(ownsEntriesFlag) {} \
- \
- inline type *pop_back(void) \
- { return (type *) kbList::pop_back(); } \
- \
- inline type *pop_front(void) \
- { return (type *) kbList::pop_front(); } \
- \
- type *remove(iterator& i) \
- { return (type *)kbList::remove(i); } \
- inline void erase(iterator & i) \
- { deleteContent(i); kbList::erase(i); } \
- \
- inline iterator begin(void) const \
- { return kbList::begin(); } \
- \
- inline iterator end(void) const \
- { return kbList::end(); } \
- \
- inline iterator tail(void) const \
- { return kbList::tail(); } \
- ~name() \
- { \
- kbListNode *next; \
- while ( first != NULL ) \
- { \
- next = first->next; \
- if(ownsEntries) \
- delete (type *)first->element; \
- delete first; \
- first = next; \
- } \
- } \
-protected: \
- inline void deleteContent(iterator i) \
- { if(ownsEntries) delete *i; } \
-}
+#if wxUSE_HELP && !defined(__WXWINCE__) && (!defined(__WXMAC__) || defined(__WXMAC_OSX__))
+#ifndef WX_PRECOMP
+ #include "wx/list.h"
+ #include "wx/string.h"
+ #include "wx/utils.h"
+ #include "wx/intl.h"
+ #include "wx/msgdlg.h"
+ #include "wx/choicdlg.h"
+ #include "wx/log.h"
+#endif
-/************************* copy of kbList.cpp: ****************************/
-kbListNode::kbListNode( void *ielement,
- kbListNode *iprev,
- kbListNode *inext)
-{
- next = inext;
- prev = iprev;
- if(prev)
- prev->next = this;
- if(next)
- next->prev = this;
- element = ielement;
-}
+#include "wx/filename.h"
+#include "wx/textfile.h"
+#include "wx/generic/helpext.h"
-kbListNode::~kbListNode()
-{
- if(prev)
- prev->next = next;
- if(next)
- next->prev = prev;
-}
+#include <stdio.h>
+#include <ctype.h>
+#include <sys/stat.h>
+#if !defined(__WINDOWS__) && !defined(__OS2__)
+ #include <unistd.h>
+#endif
-kbList::iterator::iterator(kbListNode *n)
-{
- node = n;
-}
+#ifdef __WINDOWS__
+#include "wx/msw/mslu.h"
+#endif
-void *
-kbList::iterator::operator*()
-{
- return node->element;
-}
+#ifdef __WXMSW__
+#include <windows.h>
+#include "wx/msw/winundef.h"
+#endif
-kbList::iterator &
-kbList::iterator::operator++()
-{
- node = node ? node->next : (kbListNode*)NULL;
- return *this;
-}
+// ----------------------------------------------------------------------------
+// constants
+// ----------------------------------------------------------------------------
-kbList::iterator &
-kbList::iterator::operator--()
-{
- node = node ? node->prev : (kbListNode*)NULL;
- return *this;
-}
-kbList::iterator &
-kbList::iterator::operator++(int /* foo */)
-{
- return operator++();
-}
+/// Name for map file.
+#define WXEXTHELP_MAPFILE _T("wxhelp.map")
-kbList::iterator &
-kbList::iterator::operator--(int /* bar */)
-{
- return operator--();
-}
+/// Character introducing comments/documentation field in map file.
+#define WXEXTHELP_COMMENTCHAR ';'
+#define CONTENTS_ID 0
-bool
-kbList::iterator::operator !=(kbList::iterator const & i) const
-{
- return node != i.node;
-}
+IMPLEMENT_CLASS(wxExtHelpController, wxHelpControllerBase)
-bool
-kbList::iterator::operator ==(kbList::iterator const & i) const
-{
- return node == i.node;
-}
+/// Name of environment variable to set help browser.
+#define WXEXTHELP_ENVVAR_BROWSER wxT("WX_HELPBROWSER")
+/// Is browser a netscape browser?
+#define WXEXTHELP_ENVVAR_BROWSERISNETSCAPE wxT("WX_HELPBROWSER_NS")
-kbList::kbList(bool ownsEntriesFlag)
-{
- first = NULL;
- last = NULL;
- ownsEntries = ownsEntriesFlag;
-}
+/**
+ This class implements help via an external browser.
+ It requires the name of a directory containing the documentation
+ and a file mapping numerical Section numbers to relative URLS.
+*/
-void
-kbList::push_back(void *element)
+wxExtHelpController::wxExtHelpController(wxWindow* parentWindow)
+ : wxHelpControllerBase(parentWindow)
{
- if(! first) // special case of empty list
- {
- first = new kbListNode(element);
- last = first;
- return;
- }
- else
- last = new kbListNode(element, last);
-}
+ m_MapList = NULL;
+ m_NumOfEntries = 0;
+ m_BrowserIsNetscape = false;
-void
-kbList::push_front(void *element)
-{
- if(! first) // special case of empty list
+ wxChar *browser = wxGetenv(WXEXTHELP_ENVVAR_BROWSER);
+ if (browser)
{
- push_back(element);
- return;
+ m_BrowserName = browser;
+ browser = wxGetenv(WXEXTHELP_ENVVAR_BROWSERISNETSCAPE);
+ m_BrowserIsNetscape = browser && (wxAtoi(browser) != 0);
}
- else
- first = new kbListNode(element, NULL, first);
}
-void *
-kbList::pop_back(void)
+wxExtHelpController::~wxExtHelpController()
{
- iterator i;
- void *data;
- bool ownsFlagBak = ownsEntries;
- i = tail();
- data = *i;
- ownsEntries = false;
- erase(i);
- ownsEntries = ownsFlagBak;
- return data;
+ DeleteList();
}
-void *
-kbList::pop_front(void)
+void wxExtHelpController::SetBrowser(const wxString& browsername, bool isNetscape)
{
- iterator i;
- void *data;
- bool ownsFlagBak = ownsEntries;
-
- i = begin();
- data = *i;
- ownsEntries = false;
- erase(i);
- ownsEntries = ownsFlagBak;
- return data;
-
-}
-
-void
-kbList::insert(kbList::iterator & i, void *element)
-{
- if(! i.Node())
- return;
- else if(i.Node() == first)
- {
- push_front(element);
- i = first;
- return;
- }
- i = kbList::iterator(new kbListNode(element, i.Node()->prev, i.Node()));
+ m_BrowserName = browsername;
+ m_BrowserIsNetscape = isNetscape;
}
-void
-kbList::doErase(kbList::iterator & i)
+// Set viewer: new, generic name for SetBrowser
+void wxExtHelpController::SetViewer(const wxString& viewer, long flags)
{
- kbListNode
- *node = i.Node(),
- *prev, *next;
-
- if(! node) // illegal iterator
- return;
-
- prev = node->prev;
- next = node->next;
-
- // correct first/last:
- if(node == first)
- first = node->next;
- if(node == last) // don't put else here!
- last = node->prev;
-
- // build new links:
- if(prev)
- prev->next = next;
- if(next)
- next->prev = prev;
-
- // delete this node and contents:
- // now done separately
- //if(ownsEntries)
- //delete *i;
- delete i.Node();
-
- // change the iterator to next element:
- i = kbList::iterator(next);
+ SetBrowser(viewer, (flags & wxHELP_NETSCAPE) != 0);
}
-kbList::~kbList()
+bool wxExtHelpController::DisplayHelp(const wxString &relativeURL)
{
- kbListNode *next;
+ // construct hte URL to open -- it's just a file
+ wxString url(_T("file://") + m_helpDir);
+ url << wxFILE_SEP_PATH << relativeURL;
- while ( first != NULL )
- {
- next = first->next;
- if(ownsEntries)
- delete first->element;
- delete first;
- first = next;
- }
-}
+ // use the explicit browser program if specified
+ if ( !m_BrowserName.empty() )
+ {
+ if ( m_BrowserIsNetscape )
+ {
+ wxString command;
+ command << m_BrowserName
+ << wxT(" -remote openURL(") << url << wxT(')');
+ if ( wxExecute(command, wxEXEC_SYNC) != -1 )
+ return true;
+ }
-kbList::iterator
-kbList::begin(void) const
-{
- return kbList::iterator(first);
-}
-
-kbList::iterator
-kbList::tail(void) const
-{
- return kbList::iterator(last);
-}
-
-kbList::iterator
-kbList::end(void) const
-{
- return kbList::iterator(NULL); // the one after the last
-}
+ if ( wxExecute(m_BrowserName + _T(' ') + url, wxEXEC_SYNC) != -1 )
+ return true;
+ }
+ //else: either no browser explicitly specified or we failed to open it
-unsigned
-kbList::size(void) const // inefficient
-{
- unsigned count = 0;
- kbList::iterator i;
- for(i = begin(); i != end(); i++, count++)
- ;
- return count;
+ // just use default browser
+ return wxLaunchDefaultBrowser(url);
}
-#endif
-/************************* end of kbList code *****************************/
-
-struct wxExtHelpMapEntry
+class wxExtHelpMapEntry : public wxObject
{
+public:
int id;
wxString url;
wxString doc;
wxExtHelpMapEntry(int iid, wxString const &iurl, wxString const &idoc)
{ id = iid; url = iurl; doc = idoc; }
};
-KBLIST_DEFINE(wxExtHelpMapList, wxExtHelpMapEntry);
-
-
-struct wxBusyCursor
-{
- wxBusyCursor() { wxBeginBusyCursor(); }
- ~wxBusyCursor() { wxEndBusyCursor(); }
-};
-IMPLEMENT_CLASS(wxExtHelpController, wxHelpControllerBase)
-
-/**
- This class implements help via an external browser.
- It requires the name of a directory containing the documentation
- and a file mapping numerical Section numbers to relative URLS.
-*/
-
-wxExtHelpController::wxExtHelpController(void)
+void wxExtHelpController::DeleteList()
{
- m_MapList = NULL;
- m_BrowserName = WXEXTHELP_DEFAULTBROWSER;
- m_BrowserIsNetscape = WXEXTHELP_DEFAULTBROWSER_IS_NETSCAPE;
-
- char *browser = getenv(WXEXTHELP_ENVVAR_BROWSER);
- if(browser)
+ if (m_MapList)
{
- m_BrowserName = browser;
- browser = getenv(WXEXTHELP_ENVVAR_BROWSERISNETSCAPE);
- m_BrowserIsNetscape = browser && (atoi(browser) != 0);
- }
-}
-
-wxExtHelpController::~wxExtHelpController(void)
-{
- if(m_MapList) delete m_MapList;
-}
+ wxList::compatibility_iterator node = m_MapList->GetFirst();
+ while (node)
+ {
+ delete (wxExtHelpMapEntry *)node->GetData();
+ m_MapList->Erase(node);
+ node = m_MapList->GetFirst();
+ }
-void
-wxExtHelpController::SetBrowser(wxString const & browsername, bool isNetscape)
-{
- m_BrowserName = browsername;
- m_BrowserIsNetscape = isNetscape;
+ delete m_MapList;
+ m_MapList = (wxList*) NULL;
+ }
}
-/** This must be called to tell the controller where to find the
- documentation.
- @param file - NOT a filename, but a directory name.
- @return true on success
-*/
-bool
-wxExtHelpController::Initialize(const wxString& file)
+// This must be called to tell the controller where to find the documentation.
+// @param file - NOT a filename, but a directory name.
+// @return true on success
+bool wxExtHelpController::Initialize(const wxString& file)
{
return LoadFile(file);
}
-
-bool
-wxExtHelpController::LoadFile(const wxString& ifile = "")
-{
- wxString mapFile, file, url, doc;
- int id,i,len;
- char buffer[WXEXTHELP_BUFLEN];
-
- wxBusyCursor b; // display a busy cursor
+bool wxExtHelpController::ParseMapFileLine(const wxString& line)
+{
+ const wxChar *p = line.c_str();
+
+ // skip whitespace
+ while ( isascii(*p) && isspace(*p) )
+ p++;
+
+ // skip empty lines and comments
+ if ( *p == _T('\0') || *p == WXEXTHELP_COMMENTCHAR )
+ return true;
+
+ // the line is of the form "num url" so we must have an integer now
+ wxChar *end;
+ const unsigned long id = wxStrtoul(p, &end, 0);
+
+ if ( end == p )
+ return false;
+
+ p = end;
+ while ( isascii(*p) && isspace(*p) )
+ p++;
+
+ // next should be the URL
+ wxString url;
+ url.reserve(line.length());
+ while ( isascii(*p) && !isspace(*p) )
+ url += *p++;
+
+ while ( isascii(*p) && isspace(*p) )
+ p++;
+
+ // and finally the optional description of the entry after comment
+ wxString doc;
+ if ( *p == WXEXTHELP_COMMENTCHAR )
+ {
+ p++;
+ while ( isascii(*p) && isspace(*p) )
+ p++;
+ doc = p;
+ }
+
+ m_MapList->Append(new wxExtHelpMapEntry(id, url, doc));
+ m_NumOfEntries++;
+
+ return true;
+}
+
+// file is a misnomer as it's the name of the base help directory
+bool wxExtHelpController::LoadFile(const wxString& file)
+{
+ wxFileName helpDir(wxFileName::DirName(file));
+ helpDir.MakeAbsolute();
+
+ bool dirExists = false;
+
+#if wxUSE_INTL
+ // If a locale is set, look in file/localename, i.e. If passed
+ // "/usr/local/myapp/help" and the current wxLocale is set to be "de", then
+ // look in "/usr/local/myapp/help/de/" first and fall back to
+ // "/usr/local/myapp/help" if that doesn't exist.
+ const wxLocale * const loc = wxGetLocale();
+ if ( loc )
+ {
+ wxString locName = loc->GetName();
+
+ // the locale is in general of the form xx_YY.zzzz, try the full firm
+ // first and then also more general ones
+ wxFileName helpDirLoc(helpDir);
+ helpDirLoc.AppendDir(locName);
+ dirExists = helpDirLoc.DirExists();
+
+ if ( ! dirExists )
+ {
+ // try without encoding
+ const wxString locNameWithoutEncoding = locName.BeforeLast(_T('.'));
+ if ( !locNameWithoutEncoding.empty() )
+ {
+ helpDirLoc = helpDir;
+ helpDirLoc.AppendDir(locNameWithoutEncoding);
+ dirExists = helpDirLoc.DirExists();
+ }
+ }
+
+ if ( !dirExists )
+ {
+ // try without country part
+ wxString locNameWithoutCountry = locName.BeforeLast(_T('_'));
+ if ( !locNameWithoutCountry.empty() )
+ {
+ helpDirLoc = helpDir;
+ helpDirLoc.AppendDir(locNameWithoutCountry);
+ dirExists = helpDirLoc.DirExists();
+ }
+ }
+
+ if ( dirExists )
+ helpDir = helpDirLoc;
+ }
+#endif // wxUSE_INTL
+
+ if ( ! dirExists && !helpDir.DirExists() )
+ {
+ wxLogError(_("Help directory \"%s\" not found."),
+ helpDir.GetFullPath().c_str());
+ return false;
+ }
+
+ const wxFileName mapFile(helpDir.GetFullPath(), WXEXTHELP_MAPFILE);
+ if ( ! mapFile.FileExists() )
+ {
+ wxLogError(_("Help file \"%s\" not found."),
+ mapFile.GetFullPath().c_str());
+ return false;
+ }
+
+ DeleteList();
+ m_MapList = new wxList;
+ m_NumOfEntries = 0;
+
+ wxTextFile input;
+ if ( !input.Open(mapFile.GetFullPath()) )
+ return false;
+
+ for ( wxString& line = input.GetFirstLine();
+ !input.Eof();
+ line = input.GetNextLine() )
+ {
+ if ( !ParseMapFileLine(line) )
+ {
+ wxLogWarning(_("Line %lu of map file \"%s\" has invalid syntax, skipped."),
+ (unsigned long)input.GetCurrentLine(),
+ mapFile.GetFullPath().c_str());
+ }
+ }
+
+ if ( !m_NumOfEntries )
+ {
+ wxLogError(_("No valid mappings found in the file \"%s\"."),
+ mapFile.GetFullPath().c_str());
+ return false;
+ }
+
+ m_helpDir = helpDir.GetFullPath(); // now it's valid
+ return true;
+}
+
+
+bool wxExtHelpController::DisplayContents()
+{
+ if (! m_NumOfEntries)
+ return false;
- if(! ifile.IsEmpty())
+ wxString contents;
+ wxList::compatibility_iterator node = m_MapList->GetFirst();
+ wxExtHelpMapEntry *entry;
+ while (node)
{
- file = ifile;
- if(! wxIsAbsolutePath(file))
+ entry = (wxExtHelpMapEntry *)node->GetData();
+ if (entry->id == CONTENTS_ID)
{
- file = wxGetWorkingDirectory();
- file << WXEXTHELP_SEPARATOR << ifile;
+ contents = entry->url;
+ break;
}
- else
- file = ifile;
-
- if(! wxDirExists(file))
- return false;
-
- mapFile << file << WXEXTHELP_SEPARATOR << WXEXTHELP_MAPFILE;
+
+ node = node->GetNext();
}
- else // try to reload old file
- mapFile = m_MapFile;
- if(! wxFileExists(mapFile))
- return false;
+ bool rc = false;
+ wxString file;
+ file << m_helpDir << wxFILE_SEP_PATH << contents;
+ if (file.Contains(wxT('#')))
+ file = file.BeforeLast(wxT('#'));
+ if (contents.length() && wxFileExists(file))
+ rc = DisplaySection(CONTENTS_ID);
- if(m_MapList) delete m_MapList;
- m_MapList = new wxExtHelpMapList;
- m_NumOfEntries = 0;
-
- FILE *input = fopen(mapFile.c_str(),"rt");
- if(! input)
- return false;
- do
- {
- if(fgets(buffer,WXEXTHELP_BUFLEN,input) && *buffer != WXEXTHELP_COMMENTCHAR)
- {
- len = strlen(buffer);
- if(buffer[len-1] == '\n')
- buffer[len-1] = '\0'; // cut of trailing newline
- if(sscanf(buffer,"%d", &id) != 1)
- break; // error
- for(i=0; isdigit(buffer[i])||isspace(buffer[i]); i++)
- ; // find begin of URL
- url = "";
- while(buffer[i] && ! isspace(buffer[i]) && buffer[i] !=
- WXEXTHELP_COMMENTCHAR)
- url << buffer[i++];
- while(buffer[i] && buffer[i] != WXEXTHELP_COMMENTCHAR)
- i++;
- doc = "";
- if(buffer[i])
- doc = (buffer + i + 1); // skip the comment character
- m_MapList->push_back(new wxExtHelpMapEntry(id,url,doc));
- m_NumOfEntries++;
- }
- else
- perror("");
- }while(! feof(input));
- fclose(input);
-
- m_MapFile = file; // now it's valid
- return true;
+ // if not found, open homemade toc:
+ return rc ? true : KeywordSearch(wxEmptyString);
}
-bool
-wxExtHelpController::CallBrowser(wxString const &relativeURL)
+bool wxExtHelpController::DisplaySection(int sectionNo)
{
- wxBusyCursor b; // display a busy cursor
- wxString command;
+ if (! m_NumOfEntries)
+ return false;
- if(m_BrowserIsNetscape) // try re-loading first
+ wxBusyCursor b; // display a busy cursor
+ wxList::compatibility_iterator node = m_MapList->GetFirst();
+ wxExtHelpMapEntry *entry;
+ while (node)
{
- wxString lockfile;
- wxGetHomeDir(&lockfile);
- lockfile << WXEXTHELP_SEPARATOR << ".netscape/lock";
- struct stat statbuf;
- if(lstat(lockfile.c_str(), &statbuf) == 0)
- // cannot use wxFileExists, because it's a link pointing to a
- // non-existing location if(wxFileExists(lockfile))
- {
- long success;
- command << m_BrowserName << " -remote openURL("
- << "file://" << m_MapFile
- << WXEXTHELP_SEPARATOR << relativeURL << ")";
- success = wxExecute(command);
- if(success != 0 ) // returns PID on success
- return true;
- }
+ entry = (wxExtHelpMapEntry *)node->GetData();
+ if (entry->id == sectionNo)
+ return DisplayHelp(entry->url);
+ node = node->GetNext();
}
- command = m_BrowserName;
- command << " file://"
- << m_MapFile << WXEXTHELP_SEPARATOR << relativeURL;
- return wxExecute(command) != 0;
-}
-bool
-wxExtHelpController::DisplayContents(void)
-{
- if(! m_NumOfEntries)
- return false;
- wxBusyCursor b; // display a busy cursor
- return KeywordSearch("");
+ return false;
}
-
-bool
-wxExtHelpController::DisplaySection(int sectionNo)
+
+bool wxExtHelpController::DisplaySection(const wxString& section)
{
- if(! m_NumOfEntries)
- return false;
+ bool isFilename = (section.Find(wxT(".htm")) != -1);
- wxBusyCursor b; // display a busy cursor
- wxExtHelpMapList::iterator i = m_MapList->begin();
- while(i != m_MapList->end())
- {
- if((**i).id == sectionNo)
- return CallBrowser((**i).url);
- i++;
- }
- return false;
+ if (isFilename)
+ return DisplayHelp(section);
+ else
+ return KeywordSearch(section);
}
-bool
-wxExtHelpController::DisplayBlock(long blockNo)
+bool wxExtHelpController::DisplayBlock(long blockNo)
{
return DisplaySection((int)blockNo);
}
-bool
-wxExtHelpController::KeywordSearch(const wxString& k)
+bool wxExtHelpController::KeywordSearch(const wxString& k,
+ wxHelpSearchMode WXUNUSED(mode))
{
- if(! m_NumOfEntries)
+ if (! m_NumOfEntries)
return false;
- wxBusyCursor b; // display a busy cursor
- wxString *choices = new wxString[m_NumOfEntries];
- wxString *urls = new wxString[m_NumOfEntries];
- wxString compA, compB;
-
- int idx = 0, j;
- bool rc;
- bool showAll = k.IsEmpty();
- wxExtHelpMapList::iterator i = m_MapList->begin();
-
- compA = k; compA.LowerCase(); // we compare case insensitive
- while(i != m_MapList->end())
- {
- compB = (**i).doc; compB.LowerCase();
- if((showAll || compB.Contains(k)) && ! compB.IsEmpty())
- {
- urls[idx] = (**i).url;
- // doesn't work:
- // choices[idx] = (**i).doc.Contains((**i).doc.Before(WXEXTHELP_COMMENTCHAR));
- //if(choices[idx].IsEmpty()) // didn't contain the ';'
- // choices[idx] = (**i).doc;
- choices[idx] = "";
- for(j=0;(**i).doc.c_str()[j]
- && (**i).doc.c_str()[j] != WXEXTHELP_COMMENTCHAR; j++)
- choices[idx] << (**i).doc.c_str()[j];
- idx++;
- }
- i++;
- }
+ wxString *choices = new wxString[m_NumOfEntries];
+ wxString *urls = new wxString[m_NumOfEntries];
- if(idx == 1)
- rc = CallBrowser(urls[0]);
- else if(idx == 0)
- {
- wxMessageBox(_("No entries found."));
- rc = false;
- }
- else
- {
- idx = wxGetSingleChoiceIndex(showAll ? _("Help Index") : _("Relevant entries:"),
- showAll ? _("Help Index") : _("Entries found"),
- idx,choices);
- if(idx != -1)
- rc = CallBrowser(urls[idx]);
- else
- rc = false;
- }
- delete[] urls;
- delete[] choices;
-
- return rc;
-}
+ int idx = 0;
+ bool rc = false;
+ bool showAll = k.empty();
+ wxList::compatibility_iterator node = m_MapList->GetFirst();
-bool
-wxExtHelpController::Quit(void)
+ {
+ // display a busy cursor
+ wxBusyCursor b;
+ wxString compA, compB;
+ wxExtHelpMapEntry *entry;
+
+ // we compare case insensitive
+ if (! showAll)
+ {
+ compA = k;
+ compA.LowerCase();
+ }
+
+ while (node)
+ {
+ entry = (wxExtHelpMapEntry *)node->GetData();
+ compB = entry->doc;
+
+ bool testTarget = ! compB.empty();
+ if (testTarget && ! showAll)
+ {
+ compB.LowerCase();
+ testTarget = compB.Contains(compA);
+ }
+
+ if (testTarget)
+ {
+ urls[idx] = entry->url;
+ // doesn't work:
+ // choices[idx] = (**i).doc.Contains((**i).doc.Before(WXEXTHELP_COMMENTCHAR));
+ //if (choices[idx].empty()) // didn't contain the ';'
+ // choices[idx] = (**i).doc;
+ choices[idx] = wxEmptyString;
+ for (int j=0; ; j++)
+ {
+ wxChar targetChar = entry->doc.c_str()[j];
+ if ((targetChar == 0) || (targetChar == WXEXTHELP_COMMENTCHAR))
+ break;
+
+ choices[idx] << targetChar;
+ }
+
+ idx++;
+ }
+
+ node = node->GetNext();
+ }
+ }
+
+ switch (idx)
+ {
+ case 0:
+ wxMessageBox(_("No entries found."));
+ break;
+
+ case 1:
+ rc = DisplayHelp(urls[0]);
+ break;
+
+ default:
+ idx = wxGetSingleChoiceIndex(
+ showAll ? _("Help Index") : _("Relevant entries:"),
+ showAll ? _("Help Index") : _("Entries found"),
+ idx, choices);
+ if (idx >= 0)
+ rc = DisplayHelp(urls[idx]);
+ break;
+ }
+
+ delete [] urls;
+ delete [] choices;
+
+ return rc;
+}
+
+
+bool wxExtHelpController::Quit()
{
return true;
}
-void
-wxExtHelpController::OnQuit(void)
+void wxExtHelpController::OnQuit()
{
}
+#endif // wxUSE_HELP