| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/generic/choicbkg.cpp |
| 3 | // Purpose: generic implementation of wxChoicebook |
| 4 | // Author: Vadim Zeitlin |
| 5 | // Modified by: Wlodzimierz ABX Skiba from generic/listbkg.cpp |
| 6 | // Created: 15.09.04 |
| 7 | // Copyright: (c) Vadim Zeitlin, Wlodzimierz Skiba |
| 8 | // Licence: wxWindows licence |
| 9 | /////////////////////////////////////////////////////////////////////////////// |
| 10 | |
| 11 | // ============================================================================ |
| 12 | // declarations |
| 13 | // ============================================================================ |
| 14 | |
| 15 | // ---------------------------------------------------------------------------- |
| 16 | // headers |
| 17 | // ---------------------------------------------------------------------------- |
| 18 | |
| 19 | // For compilers that support precompilation, includes "wx.h". |
| 20 | #include "wx/wxprec.h" |
| 21 | |
| 22 | #ifdef __BORLANDC__ |
| 23 | #pragma hdrstop |
| 24 | #endif |
| 25 | |
| 26 | #if wxUSE_CHOICEBOOK |
| 27 | |
| 28 | #include "wx/choicebk.h" |
| 29 | |
| 30 | #ifndef WX_PRECOMP |
| 31 | #include "wx/settings.h" |
| 32 | #include "wx/choice.h" |
| 33 | #include "wx/sizer.h" |
| 34 | #endif |
| 35 | |
| 36 | #include "wx/imaglist.h" |
| 37 | |
| 38 | // ---------------------------------------------------------------------------- |
| 39 | // various wxWidgets macros |
| 40 | // ---------------------------------------------------------------------------- |
| 41 | |
| 42 | // check that the page index is valid |
| 43 | #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount()) |
| 44 | |
| 45 | // ---------------------------------------------------------------------------- |
| 46 | // event table |
| 47 | // ---------------------------------------------------------------------------- |
| 48 | |
| 49 | IMPLEMENT_DYNAMIC_CLASS(wxChoicebook, wxBookCtrlBase) |
| 50 | |
| 51 | wxDEFINE_EVENT( wxEVT_CHOICEBOOK_PAGE_CHANGING, wxBookCtrlEvent ); |
| 52 | wxDEFINE_EVENT( wxEVT_CHOICEBOOK_PAGE_CHANGED, wxBookCtrlEvent ); |
| 53 | |
| 54 | BEGIN_EVENT_TABLE(wxChoicebook, wxBookCtrlBase) |
| 55 | EVT_CHOICE(wxID_ANY, wxChoicebook::OnChoiceSelected) |
| 56 | END_EVENT_TABLE() |
| 57 | |
| 58 | // ============================================================================ |
| 59 | // wxChoicebook implementation |
| 60 | // ============================================================================ |
| 61 | |
| 62 | // ---------------------------------------------------------------------------- |
| 63 | // wxChoicebook creation |
| 64 | // ---------------------------------------------------------------------------- |
| 65 | |
| 66 | bool |
| 67 | wxChoicebook::Create(wxWindow *parent, |
| 68 | wxWindowID id, |
| 69 | const wxPoint& pos, |
| 70 | const wxSize& size, |
| 71 | long style, |
| 72 | const wxString& name) |
| 73 | { |
| 74 | if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT ) |
| 75 | { |
| 76 | style |= wxBK_TOP; |
| 77 | } |
| 78 | |
| 79 | // no border for this control, it doesn't look nice together with |
| 80 | // wxChoice border |
| 81 | style &= ~wxBORDER_MASK; |
| 82 | style |= wxBORDER_NONE; |
| 83 | |
| 84 | if ( !wxControl::Create(parent, id, pos, size, style, |
| 85 | wxDefaultValidator, name) ) |
| 86 | return false; |
| 87 | |
| 88 | m_bookctrl = new wxChoice |
| 89 | ( |
| 90 | this, |
| 91 | wxID_ANY, |
| 92 | wxDefaultPosition, |
| 93 | wxDefaultSize |
| 94 | ); |
| 95 | |
| 96 | wxSizer* mainSizer = new wxBoxSizer(IsVertical() ? wxVERTICAL : wxHORIZONTAL); |
| 97 | |
| 98 | if (style & wxBK_RIGHT || style & wxBK_BOTTOM) |
| 99 | mainSizer->Add(0, 0, 1, wxEXPAND, 0); |
| 100 | |
| 101 | m_controlSizer = new wxBoxSizer(IsVertical() ? wxHORIZONTAL : wxVERTICAL); |
| 102 | m_controlSizer->Add(m_bookctrl, 1, (IsVertical() ? wxALIGN_CENTRE_VERTICAL : wxALIGN_CENTRE) |wxGROW, 0); |
| 103 | mainSizer->Add(m_controlSizer, 0, (IsVertical() ? (int) wxGROW : (int) wxALIGN_CENTRE_VERTICAL)|wxALL, m_controlMargin); |
| 104 | SetSizer(mainSizer); |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | // ---------------------------------------------------------------------------- |
| 109 | // accessing the pages |
| 110 | // ---------------------------------------------------------------------------- |
| 111 | |
| 112 | bool wxChoicebook::SetPageText(size_t n, const wxString& strText) |
| 113 | { |
| 114 | GetChoiceCtrl()->SetString(n, strText); |
| 115 | |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | wxString wxChoicebook::GetPageText(size_t n) const |
| 120 | { |
| 121 | return GetChoiceCtrl()->GetString(n); |
| 122 | } |
| 123 | |
| 124 | int wxChoicebook::GetPageImage(size_t WXUNUSED(n)) const |
| 125 | { |
| 126 | return wxNOT_FOUND; |
| 127 | } |
| 128 | |
| 129 | bool wxChoicebook::SetPageImage(size_t WXUNUSED(n), int WXUNUSED(imageId)) |
| 130 | { |
| 131 | // fail silently, the code may be written to use one of several book |
| 132 | // classes and call SetPageImage() unconditionally, it's better to just |
| 133 | // ignore it (which is the best we can do short of rewriting this class to |
| 134 | // use wxBitmapComboBox anyhow) than complain loudly about a rather |
| 135 | // harmless problem |
| 136 | |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | // ---------------------------------------------------------------------------- |
| 141 | // miscellaneous other stuff |
| 142 | // ---------------------------------------------------------------------------- |
| 143 | |
| 144 | void wxChoicebook::DoSetWindowVariant(wxWindowVariant variant) |
| 145 | { |
| 146 | wxBookCtrlBase::DoSetWindowVariant(variant); |
| 147 | if (m_bookctrl) |
| 148 | m_bookctrl->SetWindowVariant(variant); |
| 149 | } |
| 150 | |
| 151 | void wxChoicebook::SetImageList(wxImageList *imageList) |
| 152 | { |
| 153 | // TODO: can be implemented in form of static bitmap near choice control |
| 154 | |
| 155 | wxBookCtrlBase::SetImageList(imageList); |
| 156 | } |
| 157 | |
| 158 | // ---------------------------------------------------------------------------- |
| 159 | // selection |
| 160 | // ---------------------------------------------------------------------------- |
| 161 | |
| 162 | wxBookCtrlEvent* wxChoicebook::CreatePageChangingEvent() const |
| 163 | { |
| 164 | return new wxBookCtrlEvent(wxEVT_CHOICEBOOK_PAGE_CHANGING, m_windowId); |
| 165 | } |
| 166 | |
| 167 | void wxChoicebook::MakeChangedEvent(wxBookCtrlEvent &event) |
| 168 | { |
| 169 | event.SetEventType(wxEVT_CHOICEBOOK_PAGE_CHANGED); |
| 170 | } |
| 171 | |
| 172 | // ---------------------------------------------------------------------------- |
| 173 | // adding/removing the pages |
| 174 | // ---------------------------------------------------------------------------- |
| 175 | |
| 176 | bool |
| 177 | wxChoicebook::InsertPage(size_t n, |
| 178 | wxWindow *page, |
| 179 | const wxString& text, |
| 180 | bool bSelect, |
| 181 | int imageId) |
| 182 | { |
| 183 | if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) ) |
| 184 | return false; |
| 185 | |
| 186 | GetChoiceCtrl()->Insert(text, n); |
| 187 | |
| 188 | // if the inserted page is before the selected one, we must update the |
| 189 | // index of the selected page |
| 190 | if ( int(n) <= m_selection ) |
| 191 | { |
| 192 | // one extra page added |
| 193 | m_selection++; |
| 194 | GetChoiceCtrl()->Select(m_selection); |
| 195 | } |
| 196 | |
| 197 | if ( !DoSetSelectionAfterInsertion(n, bSelect) ) |
| 198 | page->Hide(); |
| 199 | |
| 200 | return true; |
| 201 | } |
| 202 | |
| 203 | wxWindow *wxChoicebook::DoRemovePage(size_t page) |
| 204 | { |
| 205 | wxWindow *win = wxBookCtrlBase::DoRemovePage(page); |
| 206 | |
| 207 | if ( win ) |
| 208 | { |
| 209 | GetChoiceCtrl()->Delete(page); |
| 210 | |
| 211 | DoSetSelectionAfterRemoval(page); |
| 212 | } |
| 213 | |
| 214 | return win; |
| 215 | } |
| 216 | |
| 217 | |
| 218 | bool wxChoicebook::DeleteAllPages() |
| 219 | { |
| 220 | GetChoiceCtrl()->Clear(); |
| 221 | return wxBookCtrlBase::DeleteAllPages(); |
| 222 | } |
| 223 | |
| 224 | // ---------------------------------------------------------------------------- |
| 225 | // wxChoicebook events |
| 226 | // ---------------------------------------------------------------------------- |
| 227 | |
| 228 | void wxChoicebook::OnChoiceSelected(wxCommandEvent& eventChoice) |
| 229 | { |
| 230 | if ( eventChoice.GetEventObject() != m_bookctrl ) |
| 231 | { |
| 232 | eventChoice.Skip(); |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | const int selNew = eventChoice.GetSelection(); |
| 237 | |
| 238 | if ( selNew == m_selection ) |
| 239 | { |
| 240 | // this event can only come from our own Select(m_selection) below |
| 241 | // which we call when the page change is vetoed, so we should simply |
| 242 | // ignore it |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | SetSelection(selNew); |
| 247 | |
| 248 | // change wasn't allowed, return to previous state |
| 249 | if (m_selection != selNew) |
| 250 | GetChoiceCtrl()->Select(m_selection); |
| 251 | } |
| 252 | |
| 253 | #endif // wxUSE_CHOICEBOOK |