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