]> git.saurik.com Git - wxWidgets.git/blob - src/generic/choicbkg.cpp
Get/SetTitle only for wxTopLevelWindow (wxMotif part).
[wxWidgets.git] / src / generic / choicbkg.cpp
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 // RCS-ID: $Id$
8 // Copyright: (c) Vadim Zeitlin, Wlodzimierz Skiba
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_CHOICEBOOK
28
29 #include "wx/choice.h"
30 #include "wx/choicebk.h"
31 #include "wx/imaglist.h"
32 #include "wx/settings.h"
33
34 // ----------------------------------------------------------------------------
35 // various wxWidgets macros
36 // ----------------------------------------------------------------------------
37
38 // check that the page index is valid
39 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
40
41 // ----------------------------------------------------------------------------
42 // event table
43 // ----------------------------------------------------------------------------
44
45 IMPLEMENT_DYNAMIC_CLASS(wxChoicebook, wxBookCtrlBase)
46 IMPLEMENT_DYNAMIC_CLASS(wxChoicebookEvent, wxNotifyEvent)
47
48 const wxEventType wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING = wxNewEventType();
49 const wxEventType wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED = wxNewEventType();
50 const int wxID_CHOICEBOOKCHOICE = wxNewId();
51
52 BEGIN_EVENT_TABLE(wxChoicebook, wxBookCtrlBase)
53 EVT_CHOICE(wxID_CHOICEBOOKCHOICE, wxChoicebook::OnChoiceSelected)
54 END_EVENT_TABLE()
55
56 // ============================================================================
57 // wxChoicebook implementation
58 // ============================================================================
59
60 // ----------------------------------------------------------------------------
61 // wxChoicebook creation
62 // ----------------------------------------------------------------------------
63
64 void wxChoicebook::Init()
65 {
66 m_selection = wxNOT_FOUND;
67 }
68
69 bool
70 wxChoicebook::Create(wxWindow *parent,
71 wxWindowID id,
72 const wxPoint& pos,
73 const wxSize& size,
74 long style,
75 const wxString& name)
76 {
77 if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT )
78 {
79 style |= wxBK_TOP;
80 }
81
82 // no border for this control, it doesn't look nice together with
83 // wxChoice border
84 style &= ~wxBORDER_MASK;
85 style |= wxBORDER_NONE;
86
87 if ( !wxControl::Create(parent, id, pos, size, style,
88 wxDefaultValidator, name) )
89 return false;
90
91 m_bookctrl = new wxChoice
92 (
93 this,
94 wxID_CHOICEBOOKCHOICE,
95 wxDefaultPosition,
96 wxDefaultSize
97 );
98
99 return true;
100 }
101
102 // ----------------------------------------------------------------------------
103 // wxChoicebook geometry management
104 // ----------------------------------------------------------------------------
105
106 wxSize wxChoicebook::GetControllerSize() const
107 {
108 const wxSize sizeClient = GetClientSize(),
109 sizeChoice = m_bookctrl->GetBestFittingSize();
110
111 wxSize size;
112 if ( IsVertical() )
113 {
114 size.x = sizeClient.x;
115 size.y = sizeChoice.y;
116 }
117 else // left/right aligned
118 {
119 size.x = sizeChoice.x;
120 size.y = sizeClient.y;
121 }
122
123 return size;
124 }
125
126 wxSize wxChoicebook::CalcSizeFromPage(const wxSize& sizePage) const
127 {
128 // we need to add the size of the choice control and the border between
129 const wxSize sizeChoice = GetControllerSize();
130
131 wxSize size = sizePage;
132 if ( IsVertical() )
133 {
134 size.y += sizeChoice.y + GetInternalBorder();
135 }
136 else // left/right aligned
137 {
138 size.x += sizeChoice.x + GetInternalBorder();
139 }
140
141 return size;
142 }
143
144
145 // ----------------------------------------------------------------------------
146 // accessing the pages
147 // ----------------------------------------------------------------------------
148
149 bool wxChoicebook::SetPageText(size_t n, const wxString& strText)
150 {
151 GetChoiceCtrl()->SetString(n, strText);
152
153 return true;
154 }
155
156 wxString wxChoicebook::GetPageText(size_t n) const
157 {
158 return GetChoiceCtrl()->GetString(n);
159 }
160
161 int wxChoicebook::GetPageImage(size_t WXUNUSED(n)) const
162 {
163 wxFAIL_MSG( _T("wxChoicebook::GetPageImage() not implemented") );
164
165 return wxNOT_FOUND;
166 }
167
168 bool wxChoicebook::SetPageImage(size_t WXUNUSED(n), int WXUNUSED(imageId))
169 {
170 wxFAIL_MSG( _T("wxChoicebook::SetPageImage() not implemented") );
171
172 return false;
173 }
174
175 // ----------------------------------------------------------------------------
176 // image list stuff
177 // ----------------------------------------------------------------------------
178
179 void wxChoicebook::SetImageList(wxImageList *imageList)
180 {
181 // TODO: can be implemented in form of static bitmap near choice control
182
183 wxBookCtrlBase::SetImageList(imageList);
184 }
185
186 // ----------------------------------------------------------------------------
187 // selection
188 // ----------------------------------------------------------------------------
189
190 int wxChoicebook::GetSelection() const
191 {
192 return m_selection;
193 }
194
195 int wxChoicebook::SetSelection(size_t n)
196 {
197 wxCHECK_MSG( IS_VALID_PAGE(n), wxNOT_FOUND,
198 wxT("invalid page index in wxChoicebook::SetSelection()") );
199
200 const int oldSel = m_selection;
201
202 if ( int(n) != m_selection )
203 {
204 wxChoicebookEvent event(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING, m_windowId);
205 event.SetSelection(n);
206 event.SetOldSelection(m_selection);
207 event.SetEventObject(this);
208 if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
209 {
210 if ( m_selection != wxNOT_FOUND )
211 m_pages[m_selection]->Hide();
212
213 wxWindow *page = m_pages[n];
214 page->SetSize(GetPageRect());
215 page->Show();
216
217 // change m_selection now to ignore the selection change event
218 m_selection = n;
219 GetChoiceCtrl()->Select(n);
220
221 // program allows the page change
222 event.SetEventType(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED);
223 (void)GetEventHandler()->ProcessEvent(event);
224 }
225 }
226
227 return oldSel;
228 }
229
230 // ----------------------------------------------------------------------------
231 // adding/removing the pages
232 // ----------------------------------------------------------------------------
233
234 bool
235 wxChoicebook::InsertPage(size_t n,
236 wxWindow *page,
237 const wxString& text,
238 bool bSelect,
239 int imageId)
240 {
241 if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) )
242 return false;
243
244 GetChoiceCtrl()->Insert(text, n);
245
246 // if the inserted page is before the selected one, we must update the
247 // index of the selected page
248 if ( int(n) <= m_selection )
249 {
250 // one extra page added
251 m_selection++;
252 GetChoiceCtrl()->Select(m_selection);
253 }
254
255 // some page should be selected: either this one or the first one if there
256 // is still no selection
257 int selNew = wxNOT_FOUND;
258 if ( bSelect )
259 selNew = n;
260 else if ( m_selection == wxNOT_FOUND )
261 selNew = 0;
262
263 if ( selNew != m_selection )
264 page->Hide();
265
266 if ( selNew != wxNOT_FOUND )
267 SetSelection(selNew);
268
269 InvalidateBestSize();
270 return true;
271 }
272
273 wxWindow *wxChoicebook::DoRemovePage(size_t page)
274 {
275 const size_t page_count = GetPageCount();
276 wxWindow *win = wxBookCtrlBase::DoRemovePage(page);
277
278 if ( win )
279 {
280 GetChoiceCtrl()->Delete(page);
281
282 if (m_selection >= (int)page)
283 {
284 // force new sel valid if possible
285 int sel = m_selection - 1;
286 if (page_count == 1)
287 sel = wxNOT_FOUND;
288 else if ((page_count == 2) || (sel == -1))
289 sel = 0;
290
291 // force sel invalid if deleting current page - don't try to hide it
292 m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1;
293
294 if ((sel != wxNOT_FOUND) && (sel != m_selection))
295 SetSelection(sel);
296 }
297 }
298
299 return win;
300 }
301
302
303 bool wxChoicebook::DeleteAllPages()
304 {
305 GetChoiceCtrl()->Clear();
306 return wxBookCtrlBase::DeleteAllPages();
307 }
308
309 // ----------------------------------------------------------------------------
310 // wxChoicebook events
311 // ----------------------------------------------------------------------------
312
313 void wxChoicebook::OnChoiceSelected(wxCommandEvent& eventChoice)
314 {
315 const int selNew = eventChoice.GetSelection();
316
317 if ( selNew == m_selection )
318 {
319 // this event can only come from our own Select(m_selection) below
320 // which we call when the page change is vetoed, so we should simply
321 // ignore it
322 return;
323 }
324
325 SetSelection(selNew);
326
327 // change wasn't allowed, return to previous state
328 if (m_selection != selNew)
329 GetChoiceCtrl()->Select(m_selection);
330 }
331
332 #endif // wxUSE_CHOICEBOOK