]> git.saurik.com Git - wxWidgets.git/blame - src/generic/listbkg.cpp
Switched antialiasing off since GetTextExtent doesn't work properly
[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>
65571936 9// Licence: wxWindows licence
e9c0df38
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
e9c0df38
VZ
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_LISTBOOK
28
29#include "wx/listctrl.h"
30#include "wx/statline.h"
31#include "wx/listbook.h"
5b24f0d3 32#include "wx/imaglist.h"
c59da60a 33#include "wx/settings.h"
e9c0df38 34
e9c0df38 35// ----------------------------------------------------------------------------
77ffb593 36// various wxWidgets macros
e9c0df38
VZ
37// ----------------------------------------------------------------------------
38
1f30c176
WS
39// check that the page index is valid
40#define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
41
42// ----------------------------------------------------------------------------
43// event table
44// ----------------------------------------------------------------------------
45
e9c0df38
VZ
46IMPLEMENT_DYNAMIC_CLASS(wxListbook, wxControl)
47IMPLEMENT_DYNAMIC_CLASS(wxListbookEvent, wxNotifyEvent)
48
49const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING = wxNewEventType();
50const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED = wxNewEventType();
f29395d0 51const int wxID_LISTBOOKLISTVIEW = wxNewId();
e9c0df38 52
61c083e7 53BEGIN_EVENT_TABLE(wxListbook, wxBookCtrlBase)
e9c0df38 54 EVT_SIZE(wxListbook::OnSize)
f29395d0 55 EVT_LIST_ITEM_SELECTED(wxID_LISTBOOKLISTVIEW, wxListbook::OnListSelected)
e9c0df38
VZ
56END_EVENT_TABLE()
57
58// ============================================================================
59// wxListbook implementation
60// ============================================================================
61
62// ----------------------------------------------------------------------------
63// wxListbook creation
64// ----------------------------------------------------------------------------
65
66void wxListbook::Init()
67{
68 m_list = NULL;
ef0120c1 69#if wxUSE_LINE_IN_LISTBOOK
e9c0df38 70 m_line = NULL;
ef0120c1 71#endif // wxUSE_LINE_IN_LISTBOOK
e9c0df38
VZ
72 m_selection = wxNOT_FOUND;
73}
74
75bool
76wxListbook::Create(wxWindow *parent,
77 wxWindowID id,
78 const wxPoint& pos,
79 const wxSize& size,
80 long style,
81 const wxString& name)
82{
83 if ( (style & wxLB_ALIGN_MASK) == wxLB_DEFAULT )
84 {
85#ifdef __WXMAC__
86 style |= wxLB_TOP;
87#else // !__WXMAC__
88 style |= wxLB_LEFT;
89#endif // __WXMAC__/!__WXMAC__
90 }
91
ef0120c1
VZ
92 // no border for this control, it doesn't look nice together with
93 // wxListCtrl border
94 style &= ~wxBORDER_MASK;
95 style |= wxBORDER_NONE;
96
e9c0df38
VZ
97 if ( !wxControl::Create(parent, id, pos, size, style,
98 wxDefaultValidator, name) )
99 return false;
100
101 m_list = new wxListView
102 (
103 this,
f29395d0 104 wxID_LISTBOOKLISTVIEW,
e9c0df38
VZ
105 wxDefaultPosition,
106 wxDefaultSize,
ef0120c1 107 wxLC_ICON | wxLC_SINGLE_SEL |
4a06b348 108 (IsVertical() ? wxLC_ALIGN_LEFT : wxLC_ALIGN_TOP)
e9c0df38
VZ
109 );
110
ef0120c1 111#if wxUSE_LINE_IN_LISTBOOK
e9c0df38
VZ
112 m_line = new wxStaticLine
113 (
114 this,
ca65c044 115 wxID_ANY,
e9c0df38
VZ
116 wxDefaultPosition,
117 wxDefaultSize,
118 IsVertical() ? wxLI_HORIZONTAL : wxLI_VERTICAL
119 );
ef0120c1 120#endif // wxUSE_LINE_IN_LISTBOOK
e9c0df38 121
a9092ede
RD
122#ifdef __WXMSW__
123 // On XP with themes enabled the GetViewRect used in GetListSize to
124 // determine the space needed for the list view will incorrectly return
7b45094a 125 // (0,0,0,0) the first time. So send a pending event so OnSize will be
a9092ede
RD
126 // called again after the window is ready to go. Technically we don't
127 // need to do this on non-XP windows, but if things are already sized
128 // correctly then nothing changes and so there is no harm.
129 wxSizeEvent evt;
130 GetEventHandler()->AddPendingEvent(evt);
131#endif
e9c0df38
VZ
132 return true;
133}
134
135// ----------------------------------------------------------------------------
136// wxListbook geometry management
137// ----------------------------------------------------------------------------
138
139wxSize wxListbook::GetListSize() const
140{
4a06b348 141 const wxSize sizeClient = GetClientSize(),
9f29226d
WS
142 sizeBorder = m_list->GetSize() - m_list->GetClientSize(),
143 sizeList = m_list->GetViewRect().GetSize() + sizeBorder;
e9c0df38
VZ
144
145 wxSize size;
146 if ( IsVertical() )
147 {
148 size.x = sizeClient.x;
4a06b348 149 size.y = sizeList.y;
e9c0df38
VZ
150 }
151 else // left/right aligned
152 {
4a06b348 153 size.x = sizeList.x;
e9c0df38
VZ
154 size.y = sizeClient.y;
155 }
156
157 return size;
158}
159
160wxRect wxListbook::GetPageRect() const
161{
ae03fa2c 162 const wxSize sizeList = m_list->GetSize();
e9c0df38 163
2997ca30 164 wxPoint pt;
42841dfc 165 wxRect rectPage(pt, GetClientSize());
e9c0df38
VZ
166 switch ( GetWindowStyle() & wxLB_ALIGN_MASK )
167 {
168 default:
169 wxFAIL_MSG( _T("unexpected wxListbook alignment") );
170 // fall through
171
172 case wxLB_TOP:
159e6235 173 rectPage.y = sizeList.y + GetInternalBorder();
e9c0df38
VZ
174 // fall through
175
176 case wxLB_BOTTOM:
159e6235 177 rectPage.height -= sizeList.y + GetInternalBorder();
e9c0df38
VZ
178 break;
179
180 case wxLB_LEFT:
159e6235 181 rectPage.x = sizeList.x + GetInternalBorder();
e9c0df38
VZ
182 // fall through
183
184 case wxLB_RIGHT:
159e6235 185 rectPage.width -= sizeList.x + GetInternalBorder();
e9c0df38
VZ
186 break;
187 }
188
189 return rectPage;
190}
191
192void wxListbook::OnSize(wxSizeEvent& event)
193{
f1160963
VZ
194 event.Skip();
195
196 if ( !m_list )
197 {
198 // we're not fully created yet
199 return;
200 }
201
9f29226d
WS
202 // arrange the icons before calling SetClientSize(), otherwise it wouldn't
203 // account for the scrollbars the list control might need and, at least
204 // under MSW, we'd finish with an ugly looking list control with both
205 // vertical and horizontal scrollbar (with one of them being added because
206 // the other one is not accounted for in client size computations)
207 m_list->Arrange();
208
e9c0df38
VZ
209 // resize the list control and the page area to fit inside our new size
210 const wxSize sizeClient = GetClientSize(),
9f29226d 211 sizeBorder = m_list->GetSize() - m_list->GetClientSize(),
e9c0df38
VZ
212 sizeList = GetListSize();
213
9f29226d
WS
214 m_list->SetClientSize( sizeList.x - sizeBorder.x, sizeList.y - sizeBorder.y );
215
216 const wxSize sizeNew = m_list->GetSize();
e9c0df38
VZ
217 wxPoint posList;
218 switch ( GetWindowStyle() & wxLB_ALIGN_MASK )
219 {
220 default:
221 wxFAIL_MSG( _T("unexpected wxListbook alignment") );
222 // fall through
223
224 case wxLB_TOP:
225 case wxLB_LEFT:
226 // posList is already ok
227 break;
228
229 case wxLB_BOTTOM:
9f29226d 230 posList.y = sizeClient.y - sizeNew.y;
e9c0df38
VZ
231 break;
232
233 case wxLB_RIGHT:
9f29226d 234 posList.x = sizeClient.x - sizeNew.x;
e9c0df38
VZ
235 break;
236 }
237
a1f9a880 238 if ( m_list->GetPosition() != posList )
9f29226d 239 m_list->Move(posList);
e9c0df38 240
ef0120c1 241#if wxUSE_LINE_IN_LISTBOOK
e9c0df38
VZ
242 if ( m_line )
243 {
3f0785de 244 wxRect rectLine(sizeClient);
e9c0df38
VZ
245
246 switch ( GetWindowStyle() & wxLB_ALIGN_MASK )
247 {
248 case wxLB_TOP:
9f29226d 249 rectLine.y = sizeNew.y + 1;
159e6235 250 rectLine.height = GetInternalBorder() - 2;
e9c0df38
VZ
251 break;
252
253 case wxLB_BOTTOM:
159e6235 254 rectLine.height = GetInternalBorder() - 2;
9f29226d 255 rectLine.y = sizeClient.y - sizeNew.y - rectLine.height;
e9c0df38
VZ
256 break;
257
258 case wxLB_LEFT:
9f29226d 259 rectLine.x = sizeNew.x + 1;
159e6235 260 rectLine.width = GetInternalBorder() - 2;
e9c0df38
VZ
261 break;
262
263 case wxLB_RIGHT:
159e6235 264 rectLine.width = GetInternalBorder() - 2;
9f29226d 265 rectLine.x = sizeClient.x - sizeNew.x - rectLine.width;
e9c0df38
VZ
266 break;
267 }
268
269 m_line->SetSize(rectLine);
270 }
ef0120c1 271#endif // wxUSE_LINE_IN_LISTBOOK
e9c0df38 272
33ebfc3b
VZ
273 // resize the currently shown page
274 if (m_selection != wxNOT_FOUND )
e9c0df38
VZ
275 {
276 wxWindow *page = m_pages[m_selection];
277 wxCHECK_RET( page, _T("NULL page in wxListbook?") );
e9c0df38 278 page->SetSize(GetPageRect());
7a19fb6e 279 }
e9c0df38
VZ
280}
281
282wxSize wxListbook::CalcSizeFromPage(const wxSize& sizePage) const
283{
159e6235 284 // we need to add the size of the list control and the border between
e9c0df38
VZ
285 const wxSize sizeList = GetListSize();
286
287 wxSize size = sizePage;
288 if ( IsVertical() )
289 {
159e6235 290 size.y += sizeList.y + GetInternalBorder();
e9c0df38
VZ
291 }
292 else // left/right aligned
293 {
159e6235 294 size.x += sizeList.x + GetInternalBorder();
e9c0df38
VZ
295 }
296
297 return size;
298}
299
300
301// ----------------------------------------------------------------------------
302// accessing the pages
303// ----------------------------------------------------------------------------
304
305bool wxListbook::SetPageText(size_t n, const wxString& strText)
306{
307 m_list->SetItemText(n, strText);
308
309 return true;
310}
311
312wxString wxListbook::GetPageText(size_t n) const
313{
314 return m_list->GetItemText(n);
315}
316
317int wxListbook::GetPageImage(size_t WXUNUSED(n)) const
318{
319 wxFAIL_MSG( _T("wxListbook::GetPageImage() not implemented") );
320
9f29226d 321 return wxNOT_FOUND;
e9c0df38
VZ
322}
323
324bool wxListbook::SetPageImage(size_t n, int imageId)
325{
c3627a00 326 return m_list->SetItemImage(n, imageId);
e9c0df38
VZ
327}
328
329// ----------------------------------------------------------------------------
330// image list stuff
331// ----------------------------------------------------------------------------
332
333void wxListbook::SetImageList(wxImageList *imageList)
334{
335 m_list->SetImageList(imageList, wxIMAGE_LIST_NORMAL);
336
61c083e7 337 wxBookCtrlBase::SetImageList(imageList);
e9c0df38
VZ
338}
339
340// ----------------------------------------------------------------------------
341// selection
342// ----------------------------------------------------------------------------
343
344int wxListbook::GetSelection() const
345{
346 return m_selection;
347}
348
349int wxListbook::SetSelection(size_t n)
350{
1f30c176
WS
351 wxCHECK_MSG( IS_VALID_PAGE(n), wxNOT_FOUND,
352 wxT("invalid page index in wxListbook::SetSelection()") );
e9c0df38 353
1f30c176 354 const int oldSel = m_selection;
e9c0df38 355
1f30c176 356 if ( int(n) != m_selection )
e9c0df38 357 {
1f30c176
WS
358 wxListbookEvent event(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING, m_windowId);
359 event.SetSelection(n);
360 event.SetOldSelection(m_selection);
361 event.SetEventObject(this);
362 if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
363 {
364 if ( m_selection != wxNOT_FOUND )
365 m_pages[m_selection]->Hide();
e9c0df38 366
1f30c176
WS
367 wxWindow *page = m_pages[n];
368 page->SetSize(GetPageRect());
369 page->Show();
bb08a4a1 370
716dc245 371 // change m_selection now to ignore the selection change event
1f30c176
WS
372 m_selection = n;
373 m_list->Select(n);
374 m_list->Focus(n);
375
376 // program allows the page change
377 event.SetEventType(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED);
378 (void)GetEventHandler()->ProcessEvent(event);
379 }
e9c0df38
VZ
380 }
381
1f30c176 382 return oldSel;
e9c0df38
VZ
383}
384
e9c0df38
VZ
385// ----------------------------------------------------------------------------
386// adding/removing the pages
387// ----------------------------------------------------------------------------
388
389bool
390wxListbook::InsertPage(size_t n,
391 wxWindow *page,
392 const wxString& text,
393 bool bSelect,
394 int imageId)
395{
61c083e7 396 if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) )
e9c0df38
VZ
397 return false;
398
399 m_list->InsertItem(n, text, imageId);
400
716dc245
WS
401 // if the inserted page is before the selected one, we must update the
402 // index of the selected page
403 if ( int(n) <= m_selection )
e9c0df38 404 {
42841dfc 405 // one extra page added
716dc245
WS
406 m_selection++;
407 m_list->Select(m_selection);
408 m_list->Focus(m_selection);
e9c0df38 409 }
716dc245
WS
410
411 // some page should be selected: either this one or the first one if there
412 // is still no selection
413 int selNew = -1;
414 if ( bSelect )
415 selNew = n;
416 else if ( m_selection == -1 )
417 selNew = 0;
418
419 if ( selNew != m_selection )
e9c0df38 420 page->Hide();
716dc245
WS
421
422 if ( selNew != -1 )
423 SetSelection(selNew);
e9c0df38 424
37144cf0 425 InvalidateBestSize();
9f29226d 426 m_list->Arrange();
e9c0df38
VZ
427 return true;
428}
429
430wxWindow *wxListbook::DoRemovePage(size_t page)
431{
4a10ea8b 432 const size_t page_count = GetPageCount();
61c083e7 433 wxWindow *win = wxBookCtrlBase::DoRemovePage(page);
bb08a4a1 434
e9c0df38
VZ
435 if ( win )
436 {
437 m_list->DeleteItem(page);
33ebfc3b
VZ
438
439 if (m_selection >= (int)page)
440 {
441 // force new sel valid if possible
442 int sel = m_selection - 1;
443 if (page_count == 1)
bb08a4a1 444 sel = wxNOT_FOUND;
33ebfc3b
VZ
445 else if ((page_count == 2) || (sel == -1))
446 sel = 0;
bb08a4a1 447
33ebfc3b 448 // force sel invalid if deleting current page - don't try to hide it
bb08a4a1
WS
449 m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1;
450
451 if ((sel != wxNOT_FOUND) && (sel != m_selection))
33ebfc3b
VZ
452 SetSelection(sel);
453 }
9f29226d
WS
454
455 m_list->Arrange();
e9c0df38
VZ
456 }
457
458 return win;
459}
460
fbd11d30
RD
461
462bool wxListbook::DeleteAllPages()
463{
464 m_list->DeleteAllItems();
61c083e7 465 return wxBookCtrlBase::DeleteAllPages();
fbd11d30
RD
466}
467
e9c0df38
VZ
468// ----------------------------------------------------------------------------
469// wxListbook events
470// ----------------------------------------------------------------------------
471
472void wxListbook::OnListSelected(wxListEvent& eventList)
473{
474 const int selNew = eventList.GetIndex();
475
476 if ( selNew == m_selection )
477 {
478 // this event can only come from our own Select(m_selection) below
479 // which we call when the page change is vetoed, so we should simply
480 // ignore it
481 return;
482 }
483
716dc245 484 SetSelection(selNew);
e9c0df38 485
716dc245
WS
486 // change wasn't allowed, return to previous state
487 if (m_selection != selNew)
e9c0df38
VZ
488 {
489 m_list->Select(m_selection);
716dc245 490 m_list->Focus(m_selection);
e9c0df38 491 }
e9c0df38
VZ
492}
493
494#endif // wxUSE_LISTBOOK