]> git.saurik.com Git - wxWidgets.git/blob - src/generic/listbkg.cpp
Cygwin compile fix
[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 wxWidgets macros
49 // ----------------------------------------------------------------------------
50
51 // check that the page index is valid
52 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
53
54 // ----------------------------------------------------------------------------
55 // event table
56 // ----------------------------------------------------------------------------
57
58 IMPLEMENT_DYNAMIC_CLASS(wxListbook, wxControl)
59 IMPLEMENT_DYNAMIC_CLASS(wxListbookEvent, wxNotifyEvent)
60
61 const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING = wxNewEventType();
62 const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED = wxNewEventType();
63 const int wxID_LISTBOOKLISTVIEW = wxNewId();
64
65 BEGIN_EVENT_TABLE(wxListbook, wxBookCtrlBase)
66 EVT_SIZE(wxListbook::OnSize)
67 EVT_LIST_ITEM_SELECTED(wxID_LISTBOOKLISTVIEW, wxListbook::OnListSelected)
68 END_EVENT_TABLE()
69
70 // ============================================================================
71 // wxListbook implementation
72 // ============================================================================
73
74 // ----------------------------------------------------------------------------
75 // wxListbook creation
76 // ----------------------------------------------------------------------------
77
78 void wxListbook::Init()
79 {
80 m_list = NULL;
81 #if wxUSE_LINE_IN_LISTBOOK
82 m_line = NULL;
83 #endif // wxUSE_LINE_IN_LISTBOOK
84 m_selection = wxNOT_FOUND;
85 }
86
87 bool
88 wxListbook::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
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
109 if ( !wxControl::Create(parent, id, pos, size, style,
110 wxDefaultValidator, name) )
111 return false;
112
113 m_list = new wxListView
114 (
115 this,
116 wxID_LISTBOOKLISTVIEW,
117 wxDefaultPosition,
118 wxDefaultSize,
119 wxLC_ICON | wxLC_SINGLE_SEL |
120 (IsVertical() ? wxLC_ALIGN_LEFT : wxLC_ALIGN_TOP)
121 );
122
123 #if wxUSE_LINE_IN_LISTBOOK
124 m_line = new wxStaticLine
125 (
126 this,
127 wxID_ANY,
128 wxDefaultPosition,
129 wxDefaultSize,
130 IsVertical() ? wxLI_HORIZONTAL : wxLI_VERTICAL
131 );
132 #endif // wxUSE_LINE_IN_LISTBOOK
133
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 will 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
144 return true;
145 }
146
147 // ----------------------------------------------------------------------------
148 // wxListbook geometry management
149 // ----------------------------------------------------------------------------
150
151 wxSize wxListbook::GetListSize() const
152 {
153 const wxSize sizeClient = GetClientSize(),
154 sizeBorder = m_list->GetSize() - m_list->GetClientSize(),
155 sizeList = m_list->GetViewRect().GetSize() + sizeBorder;
156
157 wxSize size;
158 if ( IsVertical() )
159 {
160 size.x = sizeClient.x;
161 size.y = sizeList.y;
162 }
163 else // left/right aligned
164 {
165 size.x = sizeList.x;
166 size.y = sizeClient.y;
167 }
168
169 return size;
170 }
171
172 wxRect wxListbook::GetPageRect() const
173 {
174 const wxSize sizeList = m_list->GetSize();
175
176 wxPoint pt;
177 wxRect rectPage(pt, GetClientSize());
178 switch ( GetWindowStyle() & wxLB_ALIGN_MASK )
179 {
180 default:
181 wxFAIL_MSG( _T("unexpected wxListbook alignment") );
182 // fall through
183
184 case wxLB_TOP:
185 rectPage.y = sizeList.y + MARGIN;
186 // fall through
187
188 case wxLB_BOTTOM:
189 rectPage.height -= sizeList.y + MARGIN;
190 break;
191
192 case wxLB_LEFT:
193 rectPage.x = sizeList.x + MARGIN;
194 // fall through
195
196 case wxLB_RIGHT:
197 rectPage.width -= sizeList.x + MARGIN;
198 break;
199 }
200
201 return rectPage;
202 }
203
204 void wxListbook::OnSize(wxSizeEvent& event)
205 {
206 event.Skip();
207
208 if ( !m_list )
209 {
210 // we're not fully created yet
211 return;
212 }
213
214 // arrange the icons before calling SetClientSize(), otherwise it wouldn't
215 // account for the scrollbars the list control might need and, at least
216 // under MSW, we'd finish with an ugly looking list control with both
217 // vertical and horizontal scrollbar (with one of them being added because
218 // the other one is not accounted for in client size computations)
219 m_list->Arrange();
220
221 // resize the list control and the page area to fit inside our new size
222 const wxSize sizeClient = GetClientSize(),
223 sizeBorder = m_list->GetSize() - m_list->GetClientSize(),
224 sizeList = GetListSize();
225
226 m_list->SetClientSize( sizeList.x - sizeBorder.x, sizeList.y - sizeBorder.y );
227
228 const wxSize sizeNew = m_list->GetSize();
229 wxPoint posList;
230 switch ( GetWindowStyle() & wxLB_ALIGN_MASK )
231 {
232 default:
233 wxFAIL_MSG( _T("unexpected wxListbook alignment") );
234 // fall through
235
236 case wxLB_TOP:
237 case wxLB_LEFT:
238 // posList is already ok
239 break;
240
241 case wxLB_BOTTOM:
242 posList.y = sizeClient.y - sizeNew.y;
243 break;
244
245 case wxLB_RIGHT:
246 posList.x = sizeClient.x - sizeNew.x;
247 break;
248 }
249
250 if ( m_list->GetPosition() != posList )
251 m_list->Move(posList);
252
253 #if wxUSE_LINE_IN_LISTBOOK
254 if ( m_line )
255 {
256 wxRect rectLine(sizeClient);
257
258 switch ( GetWindowStyle() & wxLB_ALIGN_MASK )
259 {
260 case wxLB_TOP:
261 rectLine.y = sizeNew.y + 1;
262 rectLine.height = MARGIN - 2;
263 break;
264
265 case wxLB_BOTTOM:
266 rectLine.height = MARGIN - 2;
267 rectLine.y = sizeClient.y - sizeNew.y - rectLine.height;
268 break;
269
270 case wxLB_LEFT:
271 rectLine.x = sizeNew.x + 1;
272 rectLine.width = MARGIN - 2;
273 break;
274
275 case wxLB_RIGHT:
276 rectLine.width = MARGIN - 2;
277 rectLine.x = sizeClient.x - sizeNew.x - rectLine.width;
278 break;
279 }
280
281 m_line->SetSize(rectLine);
282 }
283 #endif // wxUSE_LINE_IN_LISTBOOK
284
285 // resize the currently shown page
286 if (m_selection != wxNOT_FOUND )
287 {
288 wxWindow *page = m_pages[m_selection];
289 wxCHECK_RET( page, _T("NULL page in wxListbook?") );
290 page->SetSize(GetPageRect());
291 }
292 }
293
294 wxSize wxListbook::CalcSizeFromPage(const wxSize& sizePage) const
295 {
296 // we need to add the size of the list control and the margin
297 const wxSize sizeList = GetListSize();
298
299 wxSize size = sizePage;
300 if ( IsVertical() )
301 {
302 size.y += sizeList.y + MARGIN;
303 }
304 else // left/right aligned
305 {
306 size.x += sizeList.x + MARGIN;
307 }
308
309 return size;
310 }
311
312
313 // ----------------------------------------------------------------------------
314 // accessing the pages
315 // ----------------------------------------------------------------------------
316
317 bool wxListbook::SetPageText(size_t n, const wxString& strText)
318 {
319 m_list->SetItemText(n, strText);
320
321 return true;
322 }
323
324 wxString wxListbook::GetPageText(size_t n) const
325 {
326 return m_list->GetItemText(n);
327 }
328
329 int wxListbook::GetPageImage(size_t WXUNUSED(n)) const
330 {
331 wxFAIL_MSG( _T("wxListbook::GetPageImage() not implemented") );
332
333 return wxNOT_FOUND;
334 }
335
336 bool wxListbook::SetPageImage(size_t n, int imageId)
337 {
338 return m_list->SetItemImage(n, imageId);
339 }
340
341 // ----------------------------------------------------------------------------
342 // image list stuff
343 // ----------------------------------------------------------------------------
344
345 void wxListbook::SetImageList(wxImageList *imageList)
346 {
347 m_list->SetImageList(imageList, wxIMAGE_LIST_NORMAL);
348
349 wxBookCtrlBase::SetImageList(imageList);
350 }
351
352 // ----------------------------------------------------------------------------
353 // selection
354 // ----------------------------------------------------------------------------
355
356 int wxListbook::GetSelection() const
357 {
358 return m_selection;
359 }
360
361 int wxListbook::SetSelection(size_t n)
362 {
363 wxCHECK_MSG( IS_VALID_PAGE(n), wxNOT_FOUND,
364 wxT("invalid page index in wxListbook::SetSelection()") );
365
366 const int oldSel = m_selection;
367
368 if ( int(n) != m_selection )
369 {
370 wxListbookEvent event(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING, m_windowId);
371 event.SetSelection(n);
372 event.SetOldSelection(m_selection);
373 event.SetEventObject(this);
374 if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
375 {
376 if ( m_selection != wxNOT_FOUND )
377 m_pages[m_selection]->Hide();
378
379 wxWindow *page = m_pages[n];
380 page->SetSize(GetPageRect());
381 page->Show();
382
383 // change m_selection now to ignore the selection change event
384 m_selection = n;
385 m_list->Select(n);
386 m_list->Focus(n);
387
388 // program allows the page change
389 event.SetEventType(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED);
390 (void)GetEventHandler()->ProcessEvent(event);
391 }
392 }
393
394 return oldSel;
395 }
396
397 // ----------------------------------------------------------------------------
398 // adding/removing the pages
399 // ----------------------------------------------------------------------------
400
401 bool
402 wxListbook::InsertPage(size_t n,
403 wxWindow *page,
404 const wxString& text,
405 bool bSelect,
406 int imageId)
407 {
408 if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) )
409 return false;
410
411 m_list->InsertItem(n, text, imageId);
412
413 // if the inserted page is before the selected one, we must update the
414 // index of the selected page
415 if ( int(n) <= m_selection )
416 {
417 // one extra page added
418 m_selection++;
419 m_list->Select(m_selection);
420 m_list->Focus(m_selection);
421 }
422
423 // some page should be selected: either this one or the first one if there
424 // is still no selection
425 int selNew = -1;
426 if ( bSelect )
427 selNew = n;
428 else if ( m_selection == -1 )
429 selNew = 0;
430
431 if ( selNew != m_selection )
432 page->Hide();
433
434 if ( selNew != -1 )
435 SetSelection(selNew);
436
437 InvalidateBestSize();
438 m_list->Arrange();
439 return true;
440 }
441
442 wxWindow *wxListbook::DoRemovePage(size_t page)
443 {
444 const int page_count = GetPageCount();
445 wxWindow *win = wxBookCtrlBase::DoRemovePage(page);
446
447 if ( win )
448 {
449 m_list->DeleteItem(page);
450
451 if (m_selection >= (int)page)
452 {
453 // force new sel valid if possible
454 int sel = m_selection - 1;
455 if (page_count == 1)
456 sel = wxNOT_FOUND;
457 else if ((page_count == 2) || (sel == -1))
458 sel = 0;
459
460 // force sel invalid if deleting current page - don't try to hide it
461 m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1;
462
463 if ((sel != wxNOT_FOUND) && (sel != m_selection))
464 SetSelection(sel);
465 }
466
467 m_list->Arrange();
468 }
469
470 return win;
471 }
472
473
474 bool wxListbook::DeleteAllPages()
475 {
476 m_list->DeleteAllItems();
477 return wxBookCtrlBase::DeleteAllPages();
478 }
479
480 // ----------------------------------------------------------------------------
481 // wxListbook events
482 // ----------------------------------------------------------------------------
483
484 void wxListbook::OnListSelected(wxListEvent& eventList)
485 {
486 const int selNew = eventList.GetIndex();
487
488 if ( selNew == m_selection )
489 {
490 // this event can only come from our own Select(m_selection) below
491 // which we call when the page change is vetoed, so we should simply
492 // ignore it
493 return;
494 }
495
496 SetSelection(selNew);
497
498 // change wasn't allowed, return to previous state
499 if (m_selection != selNew)
500 {
501 m_list->Select(m_selection);
502 m_list->Focus(m_selection);
503 }
504 }
505
506 #endif // wxUSE_LISTBOOK
507