ObjectChoice.h

00001 
00006 #ifndef _ObjectChoice_h
00007 #define _ObjectChoice_h
00008 
00009 // SYSTEM INCLUDES
00010 //
00011 
00012 // PROJECT INCLUDES
00013 //
00014 
00015 // LOCAL INCLUDES
00016 //
00017 #include "../CountedPtr.h"
00018 
00019 // FORWARD REFERENCES
00020 //
00021 
00022 template<class T>
00023 class ObjectChoice
00024     : public wxChoice
00025 {
00026 public:
00027 // LIFECYCLE
00028 
00031     ObjectChoice()
00032         : wxChoice()
00033     {
00034     }// ObjectChoice
00035 
00038     ObjectChoice(wxWindow             *pParent,
00039                  wxWindowID           id,
00040                  const wxPoint&       pos = wxDefaultPosition,
00041                  const wxSize&        size = wxDefaultSize,
00042                  long                 style = 0,
00043                  const wxString&      name = wxT(""))
00044         : wxChoice(pParent, id, pos, size, 0, NULL, style, wxDefaultValidator,
00045                 name)
00046     {
00047     }// ObjectChoice
00048 
00051     ~ObjectChoice()
00052     {
00053     }// ~ObjectChoice
00054 
00055 // OPERATORS
00056 
00057 // OPERATIONS
00058 
00062     bool            Create(wxWindow          *pParent,
00063                            wxWindowID        id,
00064                            const wxPoint&    pos,
00065                            const wxSize&     size,
00066                            long              style,
00067                            const wxString&   name)
00068     {
00069         return wxChoice::Create(pParent, id, pos, size, 0, NULL,
00070                 wxDefaultValidator, name);
00071     }
00072 
00075     void            Insert(const wxString& label, CountedPtr<T> cpObject, bool sort = false);
00076 
00079     CountedPtr<T>   GetSelected() const;
00080 
00081 // INQUIRY
00082 
00083 protected:
00084 private:
00085     vector<CountedPtr<T> > mObjects;
00086 };
00087 
00088 // IMPLEMENTATION
00089 
00090 
00091 template<class T>
00092 void
00093 ObjectChoice<T>::Insert(const wxString& label, CountedPtr<T> cpObject, bool sort)
00094 {
00095     int pos;
00096 
00097     if (sort)
00098     {// ZnajdŸ odpowiednie miejsce do wstawienia tej etykiety.
00099         unsigned i;
00100         for (i = 0; i < GetCount(); ++i)
00101         {
00102             if (GetString(i) > label) break;    // znaleziono
00103         }
00104         pos = wxChoice::Insert(label, i);
00105     }
00106     else
00107     {
00108         pos = Append(label);
00109     }
00110 
00111     assert(pos > -1);
00112     SetSelection(0);
00113 
00114     // Dodaj do wektora obiektów.
00115     try
00116     {
00117         mObjects.insert(mObjects.begin() + pos, cpObject);
00118     }
00119     catch(...)
00120     {
00121         Delete(pos);
00122         throw;
00123     }
00124 }// Insert
00125 
00126 
00127 template<class T>
00128 CountedPtr<T>
00129 ObjectChoice<T>::GetSelected() const
00130 {
00131     int pos = GetSelection();
00132 
00133     // W Insert zawsze wybieramy pierwszy, więc jakiœ musi być zaznaczony.
00134     assert(pos != wxNOT_FOUND);
00135 
00136     return mObjects[pos];
00137 }// GetSelected
00138 
00139 #endif

Wygenerowano Fri Sep 29 21:04:49 2006 dla EduOptim2 programem  doxygen 1.4.6