]> git.saurik.com Git - wxWidgets.git/blob - src/generic/listbkg.cpp
fixed text control creation broken by my MacPreControlCreate() change
[wxWidgets.git] / src / generic / listbkg.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: generic/listbkg.cpp
3 // Purpose: generic implementation of wxListbook
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 19.08.03
7 // RCS-ID: $Id$
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "listbook.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #if wxUSE_LISTBOOK
32
33 #include "wx/listctrl.h"
34 #include "wx/statline.h"
35 #include "wx/listbook.h"
36 #include "wx/imaglist.h"
37 #include "wx/settings.h"
38
39 // ----------------------------------------------------------------------------
40 // constants
41 // ----------------------------------------------------------------------------
42
43 // margin between the list and the page, should be bigger than wxStaticLine
44 // size
45 const wxCoord MARGIN = 5;
46
47 // ----------------------------------------------------------------------------
48 // various wxWindows macros
49 // ----------------------------------------------------------------------------
50
51 IMPLEMENT_DYNAMIC_CLASS(wxListbook, wxControl)
52 IMPLEMENT_DYNAMIC_CLASS(wxListbookEvent, wxNotifyEvent)
53
54 const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING = wxNewEventType();
55 const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED = wxNewEventType();
56
57 BEGIN_EVENT_TABLE(wxListbook, wxBookCtrl)
58 EVT_SIZE(wxListbook::OnSize)
59
60 EVT_LIST_ITEM_SELECTED(wxID_ANY, wxListbook::OnListSelected)
61 END_EVENT_TABLE()
62
63 // ============================================================================
64 // wxListbook implementation
65 // ============================================================================
66
67 // ----------------------------------------------------------------------------
68 // wxListbook creation
69 // ----------------------------------------------------------------------------
70
71 void wxListbook::Init()
72 {
73 m_list = NULL;
74 m_line = NULL;
75 m_selection = wxNOT_FOUND;
76 }
77
78 bool
79 wxListbook::Create(wxWindow *parent,
80 wxWindowID id,
81 const wxPoint& pos,
82 const wxSize& size,
83 long style,
84 const wxString& name)
85 {
86 if ( (style & wxLB_ALIGN_MASK) == wxLB_DEFAULT )
87 {
88 #ifdef __WXMAC__
89 style |= wxLB_TOP;
90 #else // !__WXMAC__
91 style |= wxLB_LEFT;
92 #endif // __WXMAC__/!__WXMAC__
93 }
94
95 if ( !wxControl::Create(parent, id, pos, size, style,
96 wxDefaultValidator, name) )
97 return false;
98
99 m_list = new wxListView
100 (
101 this,
102 -1,
103 wxDefaultPosition,
104 wxDefaultSize,
105 wxLC_ICON | wxLC_SINGLE_SEL
106 );
107
108 m_line = new wxStaticLine
109 (
110 this,
111 -1,
112 wxDefaultPosition,
113 wxDefaultSize,
114 IsVertical() ? wxLI_HORIZONTAL : wxLI_VERTICAL
115 );
116
117 return true;
118 }
119
120 // ----------------------------------------------------------------------------
121 // wxListbook geometry management
122 // ----------------------------------------------------------------------------
123
124 wxSize wxListbook::GetListSize() const
125 {
126 const wxSize sizeClient = GetClientSize();
127
128 // we need to find the longest/tallest label
129 wxCoord widthMax = 0,
130 heightMax = 0;
131 const int count = m_list->GetItemCount();
132 if ( count )
133 {
134 for ( int i = 0; i < count; i++ )
135 {
136 wxRect r;
137 m_list->GetItemRect(i, r);
138
139 wxCoord w = r.x + r.width,
140 h = r.y + r.height;
141
142 if ( w > widthMax )
143 widthMax = w;
144 if ( h > heightMax )
145 heightMax = h;
146 }
147 }
148
149 wxSize size;
150 if ( IsVertical() )
151 {
152 size.x = sizeClient.x;
153 size.y = heightMax;
154
155 if ( widthMax >= sizeClient.x )
156 {
157 // account for the scrollbar
158 size.y += wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y);
159 }
160 }
161 else // left/right aligned
162 {
163 // +10 is due to an apparent bug in wxListCtrl::GetItemRect() but I
164 // can't fix it there right now so just add a fudge here...
165 size.x = widthMax + 10;
166 size.y = sizeClient.y;
167
168 if ( heightMax >= sizeClient.y )
169 {
170 // account for the scrollbar
171 size.x += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
172 }
173 }
174
175 return size;
176 }
177
178 wxRect wxListbook::GetPageRect() const
179 {
180 const wxSize sizeList = GetListSize();
181
182 wxRect rectPage(wxPoint(0, 0), GetClientSize());
183 switch ( GetWindowStyle() & wxLB_ALIGN_MASK )
184 {
185 default:
186 wxFAIL_MSG( _T("unexpected wxListbook alignment") );
187 // fall through
188
189 case wxLB_TOP:
190 rectPage.y = sizeList.y + MARGIN;
191 // fall through
192
193 case wxLB_BOTTOM:
194 rectPage.height -= sizeList.y + MARGIN;
195 break;
196
197 case wxLB_LEFT:
198 rectPage.x = sizeList.x + MARGIN;
199 // fall through
200
201 case wxLB_RIGHT:
202 rectPage.width -= sizeList.x + MARGIN;
203 break;
204 }
205
206 return rectPage;
207 }
208
209 void wxListbook::OnSize(wxSizeEvent& event)
210 {
211 event.Skip();
212
213 if ( !m_list )
214 {
215 // we're not fully created yet
216 return;
217 }
218
219 // resize the list control and the page area to fit inside our new size
220 const wxSize sizeClient = GetClientSize(),
221 sizeList = GetListSize();
222
223 wxPoint posList;
224 switch ( GetWindowStyle() & wxLB_ALIGN_MASK )
225 {
226 default:
227 wxFAIL_MSG( _T("unexpected wxListbook alignment") );
228 // fall through
229
230 case wxLB_TOP:
231 case wxLB_LEFT:
232 // posList is already ok
233 break;
234
235 case wxLB_BOTTOM:
236 posList.y = sizeClient.y - sizeList.y;
237 break;
238
239 case wxLB_RIGHT:
240 posList.x = sizeClient.x - sizeList.x;
241 break;
242 }
243
244 m_list->SetSize(posList.x, posList.y, sizeList.x, sizeList.y);
245
246 if ( m_line )
247 {
248 wxRect rectLine(wxPoint(0, 0), sizeClient);
249
250 switch ( GetWindowStyle() & wxLB_ALIGN_MASK )
251 {
252 case wxLB_TOP:
253 rectLine.y = sizeList.y + 1;
254 rectLine.height = MARGIN - 2;
255 break;
256
257 case wxLB_BOTTOM:
258 rectLine.height = MARGIN - 2;
259 rectLine.y = sizeClient.y - sizeList.y - rectLine.height;
260 break;
261
262 case wxLB_LEFT:
263 rectLine.x = sizeList.x + 1;
264 rectLine.width = MARGIN - 2;
265 break;
266
267 case wxLB_RIGHT:
268 rectLine.width = MARGIN - 2;
269 rectLine.x = sizeClient.x - sizeList.x - rectLine.width;
270 break;
271 }
272
273 m_line->SetSize(rectLine);
274 }
275
276 // we should always have some selection if possible
277 if ( m_selection == wxNOT_FOUND && GetPageCount() )
278 {
279 SetSelection(0);
280 }
281
282 if ( m_selection != wxNOT_FOUND )
283 {
284 wxWindow *page = m_pages[m_selection];
285 wxCHECK_RET( page, _T("NULL page in wxListbook?") );
286
287 page->SetSize(GetPageRect());
288 if ( !page->IsShown() )
289 {
290 page->Show();
291 }
292 }
293 }
294
295 wxSize wxListbook::CalcSizeFromPage(const wxSize& sizePage) const
296 {
297 // we need to add the size of the list control and the margin
298 const wxSize sizeList = GetListSize();
299
300 wxSize size = sizePage;
301 if ( IsVertical() )
302 {
303 size.y += sizeList.y + MARGIN;
304 }
305 else // left/right aligned
306 {
307 size.x += sizeList.x + MARGIN;
308 }
309
310 return size;
311 }
312
313
314 // ----------------------------------------------------------------------------
315 // accessing the pages
316 // ----------------------------------------------------------------------------
317
318 bool wxListbook::SetPageText(size_t n, const wxString& strText)
319 {
320 m_list->SetItemText(n, strText);
321
322 return true;
323 }
324
325 wxString wxListbook::GetPageText(size_t n) const
326 {
327 return m_list->GetItemText(n);
328 }
329
330 int wxListbook::GetPageImage(size_t WXUNUSED(n)) const
331 {
332 wxFAIL_MSG( _T("wxListbook::GetPageImage() not implemented") );
333
334 return -1;
335 }
336
337 bool wxListbook::SetPageImage(size_t n, int imageId)
338 {
339 return m_list->SetItemImage(n, imageId, imageId);
340 }
341
342 // ----------------------------------------------------------------------------
343 // image list stuff
344 // ----------------------------------------------------------------------------
345
346 void wxListbook::SetImageList(wxImageList *imageList)
347 {
348 m_list->SetImageList(imageList, wxIMAGE_LIST_NORMAL);
349
350 wxBookCtrl::SetImageList(imageList);
351 }
352
353 // ----------------------------------------------------------------------------
354 // selection
355 // ----------------------------------------------------------------------------
356
357 int wxListbook::GetSelection() const
358 {
359 return m_selection;
360 }
361
362 int wxListbook::SetSelection(size_t n)
363 {
364 wxCHECK_MSG( n < GetPageCount(), wxNOT_FOUND,
365 _T("invalid page index in wxListbook::SetSelection()") );
366
367 int selOld = m_selection;
368
369 if ( (int)n != m_selection )
370 {
371 m_selection = n;
372
373 m_list->Select(m_selection);
374 m_list->Focus(m_selection);
375 }
376
377 return selOld;
378 }
379
380
381 // ----------------------------------------------------------------------------
382 // adding/removing the pages
383 // ----------------------------------------------------------------------------
384
385 bool
386 wxListbook::InsertPage(size_t n,
387 wxWindow *page,
388 const wxString& text,
389 bool bSelect,
390 int imageId)
391 {
392 if ( !wxBookCtrl::InsertPage(n, page, text, bSelect, imageId) )
393 return false;
394
395 m_list->InsertItem(n, text, imageId);
396
397 if ( bSelect )
398 {
399 m_list->Select(n);
400 m_list->Focus(n);
401 }
402 else // don't select this page
403 {
404 // it will be shown only when selected
405 page->Hide();
406 }
407
408 return true;
409 }
410
411 wxWindow *wxListbook::DoRemovePage(size_t page)
412 {
413 wxWindow *win = wxBookCtrl::DoRemovePage(page);
414 if ( win )
415 {
416 m_list->DeleteItem(page);
417 }
418
419 return win;
420 }
421
422 // ----------------------------------------------------------------------------
423 // wxListbook events
424 // ----------------------------------------------------------------------------
425
426 void wxListbook::OnListSelected(wxListEvent& eventList)
427 {
428 const int selNew = eventList.GetIndex();
429
430 if ( selNew == m_selection )
431 {
432 // this event can only come from our own Select(m_selection) below
433 // which we call when the page change is vetoed, so we should simply
434 // ignore it
435 return;
436 }
437
438 // first send "change in progress" event which may be vetoed by user
439 wxListbookEvent eventIng(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING, GetId());
440
441 eventIng.SetEventObject(this);
442 eventIng.SetSelection(selNew);
443 eventIng.SetOldSelection(m_selection);
444 if ( GetEventHandler()->ProcessEvent(eventIng) && !eventIng.IsAllowed() )
445 {
446 m_list->Select(m_selection);
447 return;
448 }
449
450 // change allowed: do change the page and notify the user about it
451 if ( m_selection != wxNOT_FOUND )
452 m_pages[m_selection]->Hide();
453 wxWindow *page = m_pages[m_selection = selNew];
454 page->SetSize(GetPageRect());
455 page->Show();
456
457 wxListbookEvent eventEd(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED, GetId());
458
459 eventEd.SetEventObject(this);
460 eventEd.SetSelection(selNew);
461 eventEd.SetOldSelection(m_selection);
462
463 (void)GetEventHandler()->ProcessEvent(eventEd);
464 }
465
466 #endif // wxUSE_LISTBOOK
467