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