wxList and wxNode

team bbl


wxList and wxNode

The wxList class is a doubly linked list that can store data of an arbitrary type. wxWidgets requires that you explicitly define a new list type for each type of list data, providing strong type checking for the list's data type. The wxList class also allows you to optionally specify a key type for primitive lookups (see the wxHashMap section if you need a structure with fast random access).

The wxList class makes use of an abstract wxNode class. When you define a new list, a new node type deriving from wxNodeBase is also created, providing type-safe node operations. The most important methods of the node class are the self-explanatory GetNext, GetPrevious, and GeTData, which provide access to the next node, the previous node, and the current node's data.

The only remarkable operation for a wxList is data deletion. By default, removing a node does not delete the data being stored by that node. The DeleteContents method allows you to change this behavior and set the data itself to be deleted along with the nodes. If you want to empty a list of all data and delete the data, be sure to call DeleteContents with TRue before calling Clear.

Rather than rehash the contents of the manual, a small but comprehensive code example shows the wxList methods as well as how to create and use your custom list type. Note that the WX_DECLARE_LIST macro would typically appear in a header file, while the WX_DEFINE_LIST macro would almost always appear in a source file.

 // Our data class to store in the list class Customer { public:     int CustID;     wxString CustName; }; // this part might be in a header or source file // declare our list class: // this macro declares and partly implements CustomerList class // (which derives from wxListBase) WX_DECLARE_LIST(Customer, CustomerList); // the only requirement for the rest is to be AFTER the full // declaration of Customer (for WX_DECLARE_LIST forward declaration // is enough), but usually it will be found in the source file and // not in the header #include <wx/listimpl.cpp> WX_DEFINE_LIST(CustomerList); // Used for sorting to compare objects int listcompare(const Customer** arg1, const Customer** arg2) {     return ((*arg1)->CustID < (*arg2)->CustID); } // Show List operations void ListTest() {     // Declare an instance of our list     CustomerList* MyList = new CustomerList();     bool IsEmpty = MyList->IsEmpty(); // will be true     // Create some customers     Customer* CustA = new Customer;     CustA->CustID = 10;     CustA->CustName = wxT("Bob");     Customer* CustB = new Customer;     CustB->CustID = 20;     CustB->CustName = wxT("Sally");     Customer* CustC = new Customer;     CustC->CustID = 5;     CustC->CustName = wxT("Dmitri");     // Append two customers to the list     MyList->Append(CustA);     MyList->Append(CustB);     // Insert last customer into arbitrary place in the list     MyList->Insert((size_t)0, CustC);     int Count = MyList->GetCount(); // will be 3     // If not found, wxNOT_FOUND is returned     int index = MyList->IndexOf(CustB); // will be 2     // Customized node holds our customized data     CustomerList::Node* node = MyList->GetFirst();     // Traverse the nodes and process the customers     while (node)     {         Customer* Cust = node->GetData();         // Process Customer         node = node->GetNext();     }     // Returns the node at the specified position     node = MyList->Item(0);     // Sort the customers according to the sort function     MyList->Sort(listcompare);     // Remove Customer A node from the list     MyList->DeleteObject(CustA);     // CustA object is NOT deleted by removing the node     delete CustA;     // Returns the node whose client data is the object     node = MyList->Find(CustB);     // Specifies that data should be deleted when node is deleted     MyList->DeleteContents(true);     // Removes node from the list and deletes that node's     // data (CustB)     MyList->DeleteNode(node);     // Clears the list, and deletes all stored data     // (DeleteContents is true)     MyList->Clear();     delete MyList; } 

    team bbl



    Cross-Platform GUI Programming with wxWidgets
    Cross-Platform GUI Programming with wxWidgets
    ISBN: 0131473816
    EAN: 2147483647
    Year: 2005
    Pages: 262

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net