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