From 940c60c813f15e350642232d7e359c6dde69b9e2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Karsten=20Ball=C3=BCder?= Date: Sun, 15 Nov 1998 17:01:25 +0000 Subject: [PATCH] Replaced kbList with wxList. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@1000 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/generic/helpext.h | 6 +- src/generic/helpext.cpp | 580 +++-------------------------------- 2 files changed, 44 insertions(+), 542 deletions(-) diff --git a/include/wx/generic/helpext.h b/include/wx/generic/helpext.h index 0ee2fa51b4..c920afef85 100644 --- a/include/wx/generic/helpext.h +++ b/include/wx/generic/helpext.h @@ -24,7 +24,7 @@ /// Default browser name. # define WXEXTHELP_DEFAULTBROWSER "netscape" /// Is default browse a variant of netscape? -# define WXEXTHELP_DEFAULTBROWSER_IS_NETSCAPE true +# define WXEXTHELP_DEFAULTBROWSER_IS_NETSCAPE TRUE #endif /// Name of environment variable to set help browser. #define WXEXTHELP_ENVVAR_BROWSER "WX_HELPBROWSER" @@ -125,13 +125,15 @@ DECLARE_CLASS(wxExtHelpController) /// How many entries do we have in the map file? int m_NumOfEntries; /// A list containing all id,url,documentation triples. - wxExtHelpMapList *m_MapList; + wxList *m_MapList; /// How to call the html viewer. wxString m_BrowserName; /// Is the viewer a variant of netscape? bool m_BrowserIsNetscape; /// Call the browser using a relative URL. bool CallBrowser(wxString const &); + /// Deletes the list and all objects. + void DeleteList(void); }; #endif diff --git a/src/generic/helpext.cpp b/src/generic/helpext.cpp index eb8318d545..caf3afba31 100644 --- a/src/generic/helpext.cpp +++ b/src/generic/helpext.cpp @@ -14,544 +14,23 @@ #include "wx/generic/helpext.h" #include "wx/string.h" #include "wx/utils.h" +#include "wx/list.h" #include #include #include #include -#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 +class wxExtHelpMapEntry : public wxObject { 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 not 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; } \ -} - - -/************************* 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; -} - -kbListNode::~kbListNode() -{ - if(prev) - prev->next = next; - if(next) - next->prev = prev; -} - - -kbList::iterator::iterator(kbListNode *n) -{ - node = n; -} - -void * -kbList::iterator::operator*() -{ - return node->element; -} - -kbList::iterator & -kbList::iterator::operator++() -{ - node = node ? node->next : (kbListNode*)NULL; - return *this; -} - -kbList::iterator & -kbList::iterator::operator--() -{ - node = node ? node->prev : (kbListNode*)NULL; - return *this; -} -kbList::iterator & -kbList::iterator::operator++(int /* foo */) -{ - return operator++(); -} - -kbList::iterator & -kbList::iterator::operator--(int /* bar */) -{ - return operator--(); -} - - -bool -kbList::iterator::operator !=(kbList::iterator const & i) const -{ - return node != i.node; -} - -bool -kbList::iterator::operator ==(kbList::iterator const & i) const -{ - return node == i.node; -} - -kbList::kbList(bool ownsEntriesFlag) -{ - first = NULL; - last = NULL; - ownsEntries = ownsEntriesFlag; -} - -void -kbList::push_back(void *element) -{ - if(! first) // special case of empty list - { - first = new kbListNode(element); - last = first; - return; - } - else - last = new kbListNode(element, last); -} - -void -kbList::push_front(void *element) -{ - if(! first) // special case of empty list - { - push_back(element); - return; - } - else - first = new kbListNode(element, NULL, first); -} - -void * -kbList::pop_back(void) -{ - iterator i; - void *data; - bool ownsFlagBak = ownsEntries; - i = tail(); - data = *i; - ownsEntries = false; - erase(i); - ownsEntries = ownsFlagBak; - return data; -} - -void * -kbList::pop_front(void) -{ - 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())); -} - -void -kbList::doErase(kbList::iterator & i) -{ - 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); -} - -kbList::~kbList() -{ - kbListNode *next; - - while ( first != NULL ) - { - next = first->next; - if(ownsEntries) - delete first->element; - delete first; - first = next; - } -} - -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 -} - -unsigned -kbList::size(void) const // inefficient -{ - unsigned count = 0; - kbList::iterator i; - for(i = begin(); i != end(); i++, count++) - ; - return count; -} - -#endif -/************************* end of kbList code *****************************/ - -struct wxExtHelpMapEntry -{ 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 @@ -583,9 +62,26 @@ wxExtHelpController::wxExtHelpController(void) } } +void +wxExtHelpController::DeleteList(void) +{ + if(m_MapList) + { + wxNode *node = m_MapList->First(); + while (node) + { + delete (wxExtHelpMapEntry *)node->Data(); + delete node; + node = m_MapList->First(); + } + delete m_MapList; + m_MapList = NULL; + } +} + wxExtHelpController::~wxExtHelpController(void) { - if(m_MapList) delete m_MapList; + DeleteList(); } void @@ -638,8 +134,8 @@ wxExtHelpController::LoadFile(const wxString& ifile = "") if(! wxFileExists(mapFile)) return false; - if(m_MapList) delete m_MapList; - m_MapList = new wxExtHelpMapList; + DeleteList(); + m_MapList = new wxList; m_NumOfEntries = 0; FILE *input = fopen(mapFile.c_str(),"rt"); @@ -665,7 +161,7 @@ wxExtHelpController::LoadFile(const wxString& ifile = "") doc = ""; if(buffer[i]) doc = (buffer + i + 1); // skip the comment character - m_MapList->push_back(new wxExtHelpMapEntry(id,url,doc)); + m_MapList->Append(new wxExtHelpMapEntry(id,url,doc)); m_NumOfEntries++; } else @@ -724,12 +220,14 @@ wxExtHelpController::DisplaySection(int sectionNo) return false; wxBusyCursor b; // display a busy cursor - wxExtHelpMapList::iterator i = m_MapList->begin(); - while(i != m_MapList->end()) + wxNode *node = m_MapList->First(); + wxExtHelpMapEntry *entry; + while(node) { - if((**i).id == sectionNo) - return CallBrowser((**i).url); - i++; + entry = (wxExtHelpMapEntry *)node->Data(); + if(entry->id == sectionNo) + return CallBrowser(entry->url); + node = node->Next(); } return false; } @@ -754,26 +252,28 @@ wxExtHelpController::KeywordSearch(const wxString& k) int idx = 0, j; bool rc; bool showAll = k.IsEmpty(); - wxExtHelpMapList::iterator i = m_MapList->begin(); + wxNode *node = m_MapList->First(); + wxExtHelpMapEntry *entry; compA = k; compA.LowerCase(); // we compare case insensitive - while(i != m_MapList->end()) + while(node) { - compB = (**i).doc; compB.LowerCase(); + entry = (wxExtHelpMapEntry *)node->Data(); + compB = entry->doc; compB.LowerCase(); if((showAll || compB.Contains(k)) && ! compB.IsEmpty()) { - urls[idx] = (**i).url; + urls[idx] = entry->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]; + for(j=0;entry->doc.c_str()[j] + && entry->doc.c_str()[j] != WXEXTHELP_COMMENTCHAR; j++) + choices[idx] << entry->doc.c_str()[j]; idx++; } - i++; + node = node->Next(); } if(idx == 1) -- 2.45.2