]> git.saurik.com Git - wxWidgets.git/blob - src/univ/notebook.cpp
Watcom compilation fix
[wxWidgets.git] / src / univ / notebook.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: univ/notebook.cpp
3 // Purpose: wxNotebook implementation
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 01.02.01
7 // RCS-ID: $Id$
8 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "univnotebook.h"
22 #endif
23
24 #include "wx/wxprec.h"
25
26 #ifdef __BORLANDC__
27 #pragma hdrstop
28 #endif
29
30 #if wxUSE_NOTEBOOK
31
32 #include "wx/imaglist.h"
33 #include "wx/notebook.h"
34 #include "wx/spinbutt.h"
35 #include "wx/dcmemory.h"
36
37 #include "wx/univ/renderer.h"
38
39 // ----------------------------------------------------------------------------
40 // macros
41 // ----------------------------------------------------------------------------
42
43 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
44
45 // ----------------------------------------------------------------------------
46 // constants
47 // ----------------------------------------------------------------------------
48
49 static const size_t INVALID_PAGE = (size_t)-1;
50
51 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
52 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)
53
54 // ----------------------------------------------------------------------------
55 // private classes
56 // ----------------------------------------------------------------------------
57
58 class wxNotebookSpinBtn : public wxSpinButton
59 {
60 public:
61 wxNotebookSpinBtn(wxNotebook *nb)
62 : wxSpinButton(nb, -1,
63 wxDefaultPosition, wxDefaultSize,
64 nb->IsVertical() ? wxSP_VERTICAL : wxSP_HORIZONTAL)
65 {
66 m_nb = nb;
67 }
68
69 protected:
70 void OnSpin(wxSpinEvent& event)
71 {
72 m_nb->PerformAction(wxACTION_NOTEBOOK_GOTO, event.GetPosition());
73 }
74
75 private:
76 wxNotebook *m_nb;
77
78 DECLARE_EVENT_TABLE()
79 };
80
81 BEGIN_EVENT_TABLE(wxNotebookSpinBtn, wxSpinButton)
82 EVT_SPIN(-1, wxNotebookSpinBtn::OnSpin)
83 END_EVENT_TABLE()
84
85 // ============================================================================
86 // implementation
87 // ============================================================================
88
89 IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
90 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent)
91
92 // ----------------------------------------------------------------------------
93 // wxNotebook creation
94 // ----------------------------------------------------------------------------
95
96 void wxNotebook::Init()
97 {
98 m_sel = INVALID_PAGE;
99
100 m_heightTab =
101 m_widthMax = 0;
102
103 m_firstVisible =
104 m_lastVisible =
105 m_lastFullyVisible = 0;
106
107 m_offset = 0;
108
109 m_spinbtn = NULL;
110 }
111
112 bool wxNotebook::Create(wxWindow *parent,
113 wxWindowID id,
114 const wxPoint& pos,
115 const wxSize& size,
116 long style,
117 const wxString& name)
118 {
119 if ( !wxControl::Create(parent, id, pos, size, style,
120 wxDefaultValidator, name) )
121 return FALSE;
122
123 m_sizePad = GetRenderer()->GetTabPadding();
124
125 SetBestSize(size);
126
127 CreateInputHandler(wxINP_HANDLER_NOTEBOOK);
128
129 return TRUE;
130 }
131
132 // ----------------------------------------------------------------------------
133 // wxNotebook page titles and images
134 // ----------------------------------------------------------------------------
135
136 wxString wxNotebook::GetPageText(int nPage) const
137 {
138 wxCHECK_MSG( IS_VALID_PAGE(nPage), _T(""), _T("invalid notebook page") );
139
140 return m_titles[nPage];
141 }
142
143 bool wxNotebook::SetPageText(int nPage, const wxString& strText)
144 {
145 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, _T("invalid notebook page") );
146
147 if ( strText != m_titles[nPage] )
148 {
149 m_accels[nPage] = FindAccelIndex(strText, &m_titles[nPage]);
150
151 if ( FixedSizeTabs() )
152 {
153 // it's enough to just reresh this one
154 RefreshTab(nPage);
155 }
156 else // var width tabs
157 {
158 // we need to resize the tab to fit the new string
159 ResizeTab(nPage);
160 }
161 }
162
163 return TRUE;
164 }
165
166 int wxNotebook::GetPageImage(int nPage) const
167 {
168 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, _T("invalid notebook page") );
169
170 return m_images[nPage];
171 }
172
173 bool wxNotebook::SetPageImage(int nPage, int nImage)
174 {
175 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, _T("invalid notebook page") );
176
177 wxCHECK_MSG( m_imageList && nImage < m_imageList->GetImageCount(), FALSE,
178 _T("invalid image index in SetPageImage()") );
179
180 if ( nImage != m_images[nPage] )
181 {
182 // if the item didn't have an icon before or, on the contrary, did have
183 // it but has lost it now, its size will change - but if the icon just
184 // changes, it won't
185 bool tabSizeChanges = nImage == -1 || m_images[nPage] == -1;
186 m_images[nPage] = nImage;
187
188 if ( tabSizeChanges )
189 RefreshAllTabs();
190 else
191 RefreshTab(nPage);
192 }
193
194 return TRUE;
195 }
196
197 wxNotebook::~wxNotebook()
198 {
199 }
200
201 // ----------------------------------------------------------------------------
202 // wxNotebook page switching
203 // ----------------------------------------------------------------------------
204
205 int wxNotebook::SetSelection(int nPage)
206 {
207 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, _T("invalid notebook page") );
208
209 if ( (size_t)nPage == m_sel )
210 {
211 // don't do anything if there is nothing to do
212 return m_sel;
213 }
214
215 // event handling
216 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId);
217 event.SetSelection(nPage);
218 event.SetOldSelection(m_sel);
219 event.SetEventObject(this);
220 if ( GetEventHandler()->ProcessEvent(event) && !event.IsAllowed() )
221 {
222 // program doesn't allow the page change
223 return m_sel;
224 }
225
226 // we need to change m_sel first, before calling RefreshTab() below as
227 // otherwise the previously selected tab wouldn't be redrawn properly under
228 // wxGTK which calls Refresh() immediately and not during the next event
229 // loop iteration as wxMSW does and as it should
230 size_t selOld = m_sel;
231
232 m_sel = nPage;
233
234 if ( selOld != INVALID_PAGE )
235 {
236 RefreshTab(selOld, TRUE /* this tab was selected */);
237
238 m_pages[selOld]->Hide();
239 }
240
241 if ( m_sel != INVALID_PAGE ) // this is impossible - but test nevertheless
242 {
243 if ( HasSpinBtn() )
244 {
245 // keep it in sync
246 m_spinbtn->SetValue(m_sel);
247 }
248
249 if ( m_sel < m_firstVisible )
250 {
251 // selection is to the left of visible part of tabs
252 ScrollTo(m_sel);
253 }
254 else if ( m_sel > m_lastFullyVisible )
255 {
256 // selection is to the right of visible part of tabs
257 ScrollLastTo(m_sel);
258 }
259 else // we already see this tab
260 {
261 // no need to scroll
262 RefreshTab(m_sel);
263 }
264
265 m_pages[m_sel]->SetSize(GetPageRect());
266 m_pages[m_sel]->Show();
267 }
268
269 // event handling
270 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
271 GetEventHandler()->ProcessEvent(event);
272
273 return selOld;
274 }
275
276 // ----------------------------------------------------------------------------
277 // wxNotebook pages adding/deleting
278 // ----------------------------------------------------------------------------
279
280 bool wxNotebook::InsertPage(int nPage,
281 wxNotebookPage *pPage,
282 const wxString& strText,
283 bool bSelect,
284 int imageId)
285 {
286 int nPages = GetPageCount();
287 wxCHECK_MSG( nPage == nPages || IS_VALID_PAGE(nPage), FALSE,
288 _T("invalid notebook page in InsertPage()") );
289
290 // modify the data
291 m_pages.Insert(pPage, nPage);
292
293 wxString label;
294 m_accels.Insert(FindAccelIndex(strText, &label), nPage);
295 m_titles.Insert(label, nPage);
296
297 m_images.Insert(imageId, nPage);
298
299 // cache the tab geometry here
300 wxSize sizeTab = CalcTabSize(nPage);
301
302 if ( sizeTab.y > m_heightTab )
303 m_heightTab = sizeTab.y;
304
305 if ( FixedSizeTabs() && sizeTab.x > m_widthMax )
306 m_widthMax = sizeTab.x;
307
308 m_widths.Insert(sizeTab.x, nPage);
309
310 // spin button may appear if we didn't have it before - but even if we did,
311 // its range should change, so update it unconditionally
312 UpdateSpinBtn();
313
314 // if the tab has just appeared, we have to relayout everything, otherwise
315 // it's enough to just redraw the tabs
316 if ( nPages == 0 )
317 {
318 // always select the first tab to have at least some selection
319 bSelect = TRUE;
320
321 Relayout();
322 Refresh();
323 }
324 else // not the first tab
325 {
326 RefreshAllTabs();
327 }
328
329 if ( bSelect )
330 {
331 SetSelection(nPage);
332 }
333 else // pages added to the notebook are initially hidden
334 {
335 pPage->Hide();
336 }
337
338 return TRUE;
339 }
340
341 bool wxNotebook::DeleteAllPages()
342 {
343 if ( !wxNotebookBase::DeleteAllPages() )
344 return FALSE;
345
346 // clear the other arrays as well
347 m_titles.Clear();
348 m_images.Clear();
349 m_accels.Clear();
350 m_widths.Clear();
351
352 // it is not valid any longer
353 m_sel = INVALID_PAGE;
354
355 // spin button is not needed any more
356 UpdateSpinBtn();
357
358 Relayout();
359
360 return TRUE;
361 }
362
363 wxNotebookPage *wxNotebook::DoRemovePage(int nPage)
364 {
365 wxCHECK_MSG( IS_VALID_PAGE(nPage), NULL, _T("invalid notebook page") );
366
367 wxNotebookPage *page = m_pages[nPage];
368 m_pages.RemoveAt(nPage);
369 m_titles.RemoveAt(nPage);
370 m_accels.RemoveAt(nPage);
371 m_widths.RemoveAt(nPage);
372 m_images.RemoveAt(nPage);
373
374 // the spin button might not be needed any more
375 // 2002-08-12 'if' commented out by JACS on behalf
376 // of Hans Van Leemputten <Hansvl@softhome.net> who
377 // points out that UpdateSpinBtn should always be called,
378 // to ensure m_lastVisible is up to date.
379 // if ( HasSpinBtn() )
380 {
381 UpdateSpinBtn();
382 }
383
384 int count = GetPageCount();
385 if ( count )
386 {
387 if ( m_sel == (size_t)nPage )
388 {
389 // avoid sending event to this page which doesn't exist in the
390 // notebook any more
391 m_sel = INVALID_PAGE;
392
393 SetSelection(nPage == count ? nPage - 1 : nPage);
394 }
395 else if ( m_sel > (size_t)nPage )
396 {
397 // no need to change selection, just adjust the index
398 m_sel--;
399 }
400 }
401 else // no more tabs left
402 {
403 m_sel = INVALID_PAGE;
404 }
405
406 // have to refresh everything
407 Relayout();
408
409 return page;
410 }
411
412 // ----------------------------------------------------------------------------
413 // wxNotebook drawing
414 // ----------------------------------------------------------------------------
415
416 void wxNotebook::RefreshCurrent()
417 {
418 if ( m_sel != INVALID_PAGE )
419 {
420 RefreshTab(m_sel);
421 }
422 }
423
424 void wxNotebook::RefreshTab(int page, bool forceSelected)
425 {
426 wxCHECK_RET( IS_VALID_PAGE(page), _T("invalid notebook page") );
427
428 wxRect rect = GetTabRect(page);
429 if ( forceSelected || ((size_t)page == m_sel) )
430 {
431 const wxSize indent = GetRenderer()->GetTabIndent();
432 rect.Inflate(indent.x, indent.y);
433 }
434
435 RefreshRect(rect);
436 }
437
438 void wxNotebook::RefreshAllTabs()
439 {
440 wxRect rect = GetAllTabsRect();
441 if ( rect.width || rect.height )
442 {
443 RefreshRect(rect);
444 }
445 //else: we don't have tabs at all
446 }
447
448 void wxNotebook::DoDrawTab(wxDC& dc, const wxRect& rect, size_t n)
449 {
450 wxBitmap bmp;
451 if ( HasImage(n) )
452 {
453 int image = m_images[n];
454
455 // Not needed now that wxGenericImageList is being
456 // used for wxUniversal under MSW
457 #if 0 // def __WXMSW__ // FIXME
458 int w, h;
459 m_imageList->GetSize(n, w, h);
460 bmp.Create(w, h);
461 wxMemoryDC dc;
462 dc.SelectObject(bmp);
463 dc.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID));
464 m_imageList->Draw(image, dc, 0, 0, wxIMAGELIST_DRAW_NORMAL, TRUE);
465 dc.SelectObject(wxNullBitmap);
466 #else
467 bmp = *m_imageList->GetBitmap(image);
468 #endif
469 }
470
471 int flags = 0;
472 if ( n == m_sel )
473 {
474 flags |= wxCONTROL_SELECTED;
475
476 if ( IsFocused() )
477 flags |= wxCONTROL_FOCUSED;
478 }
479
480 GetRenderer()->DrawTab
481 (
482 dc,
483 rect,
484 GetTabOrientation(),
485 m_titles[n],
486 bmp,
487 flags,
488 m_accels[n]
489 );
490 }
491
492 void wxNotebook::DoDraw(wxControlRenderer *renderer)
493 {
494 //wxRect rectUpdate = GetUpdateClientRect(); -- unused
495
496 wxDC& dc = renderer->GetDC();
497 dc.SetFont(GetFont());
498 dc.SetTextForeground(GetForegroundColour());
499
500 // redraw the border - it's simpler to always do it instead of checking
501 // whether this needs to be done
502 GetRenderer()->DrawBorder(dc, wxBORDER_RAISED, GetPagePart());
503
504 // avoid overwriting the spin button
505 if ( HasSpinBtn() )
506 {
507 wxRect rectTabs = GetAllTabsRect();
508 wxSize sizeSpinBtn = m_spinbtn->GetSize();
509
510 if ( IsVertical() )
511 {
512 rectTabs.height -= sizeSpinBtn.y;
513
514 // Allow for erasing the line under selected tab
515 rectTabs.width += 2;
516 }
517 else
518 {
519 rectTabs.width -= sizeSpinBtn.x;
520
521 // Allow for erasing the line under selected tab
522 rectTabs.height += 2;
523 }
524
525 dc.SetClippingRegion(rectTabs);
526 }
527
528 wxRect rect = GetTabsPart();
529 bool isVertical = IsVertical();
530
531 wxRect rectSel;
532 for ( size_t n = m_firstVisible; n < m_lastVisible; n++ )
533 {
534 GetTabSize(n, &rect.width, &rect.height);
535
536 if ( n == m_sel )
537 {
538 // don't redraw it now as this tab has to be drawn over the other
539 // ones as it takes more place and spills over to them
540 rectSel = rect;
541 }
542 else // not selected tab
543 {
544 // unfortunately we can't do this because the selected tab hangs
545 // over its neighbours and so we might need to refresh more tabs -
546 // of course, we could still avoid rereshing some of them with more
547 // complicated checks, but it doesn't seem too bad to refresh all
548 // of them, I still don't see flicker, so leaving as is for now
549
550 //if ( rectUpdate.Intersects(rect) )
551 {
552 DoDrawTab(dc, rect, n);
553 }
554 //else: doesn't need to be refreshed
555 }
556
557 // move the rect to the next tab
558 if ( isVertical )
559 rect.y += rect.height;
560 else
561 rect.x += rect.width;
562 }
563
564 // now redraw the selected tab
565 if ( rectSel.width )
566 {
567 DoDrawTab(dc, rectSel, m_sel);
568 }
569
570 dc.DestroyClippingRegion();
571 }
572
573 // ----------------------------------------------------------------------------
574 // wxNotebook geometry
575 // ----------------------------------------------------------------------------
576
577 int wxNotebook::HitTest(const wxPoint& pt, long *flags) const
578 {
579 if ( flags )
580 *flags = wxNB_HITTEST_NOWHERE;
581
582 // first check that it is in this window at all
583 if ( !GetClientRect().Inside(pt) )
584 {
585 return -1;
586 }
587
588 wxRect rectTabs = GetAllTabsRect();
589
590 switch ( GetTabOrientation() )
591 {
592 default:
593 wxFAIL_MSG(_T("unknown tab orientation"));
594 // fall through
595
596 case wxTOP:
597 if ( pt.y > rectTabs.GetBottom() )
598 return -1;
599 break;
600
601 case wxBOTTOM:
602 if ( pt.y < rectTabs.y )
603 return -1;
604 break;
605
606 case wxLEFT:
607 if ( pt.x > rectTabs.GetRight() )
608 return -1;
609 break;
610
611 case wxRIGHT:
612 if ( pt.x < rectTabs.x )
613 return -1;
614 break;
615 }
616
617 for ( size_t n = m_firstVisible; n < m_lastVisible; n++ )
618 {
619 GetTabSize(n, &rectTabs.width, &rectTabs.height);
620
621 if ( rectTabs.Inside(pt) )
622 {
623 if ( flags )
624 {
625 // TODO: be more precise
626 *flags = wxNB_HITTEST_ONITEM;
627 }
628
629 return n;
630 }
631
632 // move the rectTabs to the next tab
633 if ( IsVertical() )
634 rectTabs.y += rectTabs.height;
635 else
636 rectTabs.x += rectTabs.width;
637 }
638
639 return -1;
640 }
641
642 bool wxNotebook::IsVertical() const
643 {
644 wxDirection dir = GetTabOrientation();
645
646 return dir == wxLEFT || dir == wxRIGHT;
647 }
648
649 wxDirection wxNotebook::GetTabOrientation() const
650 {
651 long style = GetWindowStyle();
652 if ( style & wxNB_BOTTOM )
653 return wxBOTTOM;
654 else if ( style & wxNB_RIGHT )
655 return wxRIGHT;
656 else if ( style & wxNB_LEFT )
657 return wxLEFT;
658
659 // wxNB_TOP == 0 so we don't have to test for it
660 return wxTOP;
661 }
662
663 wxRect wxNotebook::GetTabRect(int page) const
664 {
665 wxRect rect;
666 wxCHECK_MSG( IS_VALID_PAGE(page), rect, _T("invalid notebook page") );
667
668 // calc the size of this tab and of the preceding ones
669 wxCoord widthThis, widthBefore;
670 if ( FixedSizeTabs() )
671 {
672 widthThis = m_widthMax;
673 widthBefore = page*m_widthMax;
674 }
675 else
676 {
677 widthBefore = 0;
678 for ( int n = 0; n < page; n++ )
679 {
680 widthBefore += m_widths[n];
681 }
682
683 widthThis = m_widths[page];
684 }
685
686 rect = GetTabsPart();
687 if ( IsVertical() )
688 {
689 rect.y += widthBefore - m_offset;
690 rect.height = widthThis;
691 }
692 else // horz
693 {
694 rect.x += widthBefore - m_offset;
695 rect.width = widthThis;
696 }
697
698 return rect;
699 }
700
701 wxRect wxNotebook::GetAllTabsRect() const
702 {
703 wxRect rect;
704
705 if ( GetPageCount() )
706 {
707 const wxSize indent = GetRenderer()->GetTabIndent();
708 wxSize size = GetClientSize();
709
710 if ( IsVertical() )
711 {
712 rect.width = m_heightTab + indent.x;
713 rect.x = GetTabOrientation() == wxLEFT ? 0 : size.x - rect.width;
714 rect.y = 0;
715 rect.height = size.y;
716 }
717 else // horz
718 {
719 rect.x = 0;
720 rect.width = size.x;
721 rect.height = m_heightTab + indent.y;
722 rect.y = GetTabOrientation() == wxTOP ? 0 : size.y - rect.height;
723 }
724 }
725 //else: no pages
726
727 return rect;
728 }
729
730 wxRect wxNotebook::GetTabsPart() const
731 {
732 wxRect rect = GetAllTabsRect();
733
734 wxDirection dir = GetTabOrientation();
735
736 const wxSize indent = GetRenderer()->GetTabIndent();
737 if ( IsVertical() )
738 {
739 rect.x += indent.y;
740 rect.y += indent.x;
741 }
742 else // horz
743 {
744 rect.x += indent.x;
745 if ( dir == wxTOP )
746 {
747 rect.y += indent.y;
748 rect.height -= indent.y;
749 }
750 else // wxBOTTOM
751 {
752 rect.height -= indent.y;
753 }
754 }
755
756 return rect;
757 }
758
759 void wxNotebook::GetTabSize(int page, wxCoord *w, wxCoord *h) const
760 {
761 wxCHECK_RET( w && h, _T("NULL pointer in GetTabSize") );
762
763 if ( IsVertical() )
764 {
765 // width and height have inverted meaning
766 wxCoord *tmp = w;
767 w = h;
768 h = tmp;
769 }
770
771 // height is always fixed
772 *h = m_heightTab;
773
774 // width may also be fixed and be the same for all tabs
775 *w = GetTabWidth(page);
776 }
777
778 void wxNotebook::SetTabSize(const wxSize& sz)
779 {
780 wxCHECK_RET( FixedSizeTabs(), _T("SetTabSize() ignored") );
781
782 if ( IsVertical() )
783 {
784 m_heightTab = sz.x;
785 m_widthMax = sz.y;
786 }
787 else // horz
788 {
789 m_widthMax = sz.x;
790 m_heightTab = sz.y;
791 }
792 }
793
794 wxSize wxNotebook::CalcTabSize(int page) const
795 {
796 // NB: don't use m_widthMax, m_heightTab or m_widths here because this
797 // method is called to calculate them
798
799 wxSize size;
800
801 wxCHECK_MSG( IS_VALID_PAGE(page), size, _T("invalid notebook page") );
802
803 GetTextExtent(m_titles[page], &size.x, &size.y);
804
805 if ( HasImage(page) )
806 {
807 wxSize sizeImage;
808 m_imageList->GetSize(m_images[page], sizeImage.x, sizeImage.y);
809
810 size.x += sizeImage.x + 5; // FIXME: hard coded margin
811
812 if ( sizeImage.y > size.y )
813 size.y = sizeImage.y;
814 }
815
816 size.x += 2*m_sizePad.x;
817 size.y += 2*m_sizePad.y;
818
819 return size;
820 }
821
822 void wxNotebook::ResizeTab(int page)
823 {
824 wxSize sizeTab = CalcTabSize(page);
825
826 // we only need full relayout if the page size changes
827 bool needsRelayout = FALSE;
828
829 if ( IsVertical() )
830 {
831 // swap coordinates
832 wxCoord tmp = sizeTab.x;
833 sizeTab.x = sizeTab.y;
834 sizeTab.y = tmp;
835 }
836
837 if ( sizeTab.y > m_heightTab )
838 {
839 needsRelayout = TRUE;
840
841 m_heightTab = sizeTab.y;
842 }
843
844 m_widths[page] = sizeTab.x;
845
846 if ( sizeTab.x > m_widthMax )
847 m_widthMax = sizeTab.x;
848
849 // the total of the tabs has changed too
850 UpdateSpinBtn();
851
852 if ( needsRelayout )
853 Relayout();
854 else
855 RefreshAllTabs();
856 }
857
858 void wxNotebook::SetPadding(const wxSize& padding)
859 {
860 if ( padding != m_sizePad )
861 {
862 m_sizePad = padding;
863
864 Relayout();
865 }
866 }
867
868 void wxNotebook::Relayout()
869 {
870 if ( GetPageCount() )
871 {
872 RefreshAllTabs();
873
874 UpdateSpinBtn();
875
876 if ( m_sel != INVALID_PAGE )
877 {
878 // resize the currently shown page
879 wxRect rectPage = GetPageRect();
880
881 m_pages[m_sel]->SetSize(rectPage);
882
883 // also scroll it into view if needed (note that m_lastVisible
884 // was updated by the call to UpdateSpinBtn() above, this is why it
885 // is needed here)
886 if ( HasSpinBtn() )
887 {
888 if ( m_sel < m_firstVisible )
889 {
890 // selection is to the left of visible part of tabs
891 ScrollTo(m_sel);
892 }
893 else if ( m_sel > m_lastFullyVisible )
894 {
895 // selection is to the right of visible part of tabs
896 ScrollLastTo(m_sel);
897 }
898 }
899 }
900 }
901 else // we have no pages
902 {
903 // just refresh everything
904 Refresh();
905 }
906 }
907
908 wxRect wxNotebook::GetPagePart() const
909 {
910 wxRect rectPage = GetClientRect();
911
912 if ( GetPageCount() )
913 {
914 wxRect rectTabs = GetAllTabsRect();
915 wxDirection dir = GetTabOrientation();
916 if ( IsVertical() )
917 {
918 rectPage.width -= rectTabs.width;
919 if ( dir == wxLEFT )
920 rectPage.x += rectTabs.width;
921 }
922 else // horz
923 {
924 rectPage.height -= rectTabs.height;
925 if ( dir == wxTOP )
926 rectPage.y += rectTabs.height;
927 }
928 }
929 //else: no pages at all
930
931 return rectPage;
932 }
933
934 wxRect wxNotebook::GetPageRect() const
935 {
936 wxRect rect = GetPagePart();
937
938 // leave space for the border
939 wxRect rectBorder = GetRenderer()->GetBorderDimensions(wxBORDER_RAISED);
940
941 // FIXME: hardcoded +2!
942 rect.Inflate(-(rectBorder.x + rectBorder.width + 2),
943 -(rectBorder.y + rectBorder.height + 2));
944
945 return rect;
946 }
947
948 wxSize wxNotebook::GetSizeForPage(const wxSize& size) const
949 {
950 wxSize sizeNb = size;
951 wxRect rect = GetAllTabsRect();
952 if ( IsVertical() )
953 sizeNb.x += rect.width;
954 else
955 sizeNb.y += rect.height;
956
957 return sizeNb;
958 }
959
960 void wxNotebook::SetPageSize(const wxSize& size)
961 {
962 SetClientSize(GetSizeForPage(size));
963 }
964
965 wxSize wxNotebook::CalcSizeFromPage(const wxSize& sizePage) const
966 {
967 return AdjustSize(GetSizeForPage(sizePage));
968 }
969
970 // ----------------------------------------------------------------------------
971 // wxNotebook spin button
972 // ----------------------------------------------------------------------------
973
974 bool wxNotebook::HasSpinBtn() const
975 {
976 return m_spinbtn && m_spinbtn->IsShown();
977 }
978
979 void wxNotebook::CalcLastVisibleTab()
980 {
981 bool isVertical = IsVertical();
982
983 wxCoord width = GetClientSize().x;
984
985 wxRect rect = GetTabsPart();
986
987 size_t count = GetPageCount();
988
989 wxCoord widthLast = 0;
990 size_t n;
991 for ( n = m_firstVisible; n < count; n++ )
992 {
993 GetTabSize(n, &rect.width, &rect.height);
994 if ( rect.GetRight() > width )
995 {
996 break;
997 }
998
999 // remember it to use below
1000 widthLast = rect.GetRight();
1001
1002 // move the rect to the next tab
1003 if ( isVertical )
1004 rect.y += rect.height;
1005 else
1006 rect.x += rect.width;
1007 }
1008
1009 if ( n == m_firstVisible )
1010 {
1011 // even the first tab isn't fully visible - but still pretend it is as
1012 // we have to show something
1013 m_lastFullyVisible = m_firstVisible;
1014 }
1015 else // more than one tab visible
1016 {
1017 m_lastFullyVisible = n - 1;
1018
1019 // but is it really fully visible? it shouldn't overlap with the spin
1020 // button if it is present (again, even if the first button does
1021 // overlap with it, we pretend that it doesn't as there is not much
1022 // else we can do)
1023 if ( (m_lastFullyVisible > m_firstVisible) && HasSpinBtn() )
1024 {
1025 // adjust width to be the width before the spin button
1026 wxSize sizeSpinBtn = m_spinbtn->GetSize();
1027 if ( IsVertical() )
1028 width -= sizeSpinBtn.y;
1029 else
1030 width -= sizeSpinBtn.x;
1031
1032 if ( widthLast > width )
1033 {
1034 // the last button overlaps with spin button, so take he
1035 // previous one
1036 m_lastFullyVisible--;
1037 }
1038 }
1039 }
1040
1041 if ( n == count )
1042 {
1043 // everything is visible
1044 m_lastVisible = n;
1045 }
1046 else
1047 {
1048 // this tab is still (partially) visible, so m_lastVisible is the
1049 // next tab (remember, this is "exclusive" last)
1050 m_lastVisible = n + 1;
1051
1052 }
1053 }
1054
1055 void wxNotebook::UpdateSpinBtn()
1056 {
1057 // first decide if we need a spin button
1058 bool allTabsShown;
1059
1060 size_t count = (size_t)GetPageCount();
1061 if ( count == 0 )
1062 {
1063 // this case is special, get rid of it immediately: everything is
1064 // visible and we don't need any spin buttons
1065 allTabsShown = TRUE;
1066
1067 // have to reset them manually as we don't call CalcLastVisibleTab()
1068 m_firstVisible =
1069 m_lastVisible =
1070 m_lastFullyVisible = 0;
1071 }
1072 else
1073 {
1074 CalcLastVisibleTab();
1075
1076 // if all tabs after the first visible one are shown, it doesn't yet
1077 // mean that all tabs are shown - so we go backwards until we arrive to
1078 // the beginning (then all tabs are indeed shown) or find a tab such
1079 // that not all tabs after it are shown
1080 while ( (m_lastFullyVisible == count - 1) && (m_firstVisible > 0) )
1081 {
1082 // this is equivalent to ScrollTo(m_firstVisible - 1) but more
1083 // efficient
1084 m_offset -= GetTabWidth(m_firstVisible--);
1085
1086 // reclaculate after m_firstVisible change
1087 CalcLastVisibleTab();
1088 }
1089
1090 allTabsShown = m_lastFullyVisible == count - 1;
1091 }
1092
1093 if ( !allTabsShown )
1094 {
1095 if ( !m_spinbtn )
1096 {
1097 // create it once only
1098 m_spinbtn = new wxNotebookSpinBtn(this);
1099
1100 // set the correct value to keep it in sync
1101 m_spinbtn->SetValue(m_sel);
1102 }
1103
1104 // position it correctly
1105 PositionSpinBtn();
1106
1107 // and show it
1108 m_spinbtn->Show();
1109
1110 // also set/update the range
1111 m_spinbtn->SetRange(0, count - 1);
1112
1113 // update m_lastFullyVisible once again as it might have changed
1114 // because the spin button appeared
1115 //
1116 // FIXME: might do it more efficiently
1117 CalcLastVisibleTab();
1118 }
1119 else // all tabs are visible, we don't need spin button
1120 {
1121 if ( m_spinbtn )
1122 {
1123 m_spinbtn->Hide();
1124 }
1125 }
1126 }
1127
1128 void wxNotebook::PositionSpinBtn()
1129 {
1130 if ( !m_spinbtn )
1131 return;
1132
1133 wxCoord wBtn, hBtn;
1134 m_spinbtn->GetSize(&wBtn, &hBtn);
1135
1136 wxRect rectTabs = GetAllTabsRect();
1137
1138 wxCoord x, y;
1139 switch ( GetTabOrientation() )
1140 {
1141 default:
1142 wxFAIL_MSG(_T("unknown tab orientation"));
1143 // fall through
1144
1145 case wxTOP:
1146 x = rectTabs.GetRight() - wBtn;
1147 y = rectTabs.GetBottom() - hBtn;
1148 break;
1149
1150 case wxBOTTOM:
1151 x = rectTabs.GetRight() - wBtn;
1152 y = rectTabs.GetTop();
1153 break;
1154
1155 case wxLEFT:
1156 x = rectTabs.GetRight() - wBtn;
1157 y = rectTabs.GetBottom() - hBtn;
1158 break;
1159
1160 case wxRIGHT:
1161 x = rectTabs.GetLeft();
1162 y = rectTabs.GetBottom() - hBtn;
1163 break;
1164 }
1165
1166 m_spinbtn->Move(x, y);
1167 }
1168
1169 // ----------------------------------------------------------------------------
1170 // wxNotebook scrolling
1171 // ----------------------------------------------------------------------------
1172
1173 void wxNotebook::ScrollTo(int page)
1174 {
1175 wxCHECK_RET( IS_VALID_PAGE(page), _T("invalid notebook page") );
1176
1177 // set the first visible tab and offset (easy)
1178 m_firstVisible = (size_t)page;
1179 m_offset = 0;
1180 for ( size_t n = 0; n < m_firstVisible; n++ )
1181 {
1182 m_offset += GetTabWidth(n);
1183 }
1184
1185 // find the last visible tab too
1186 CalcLastVisibleTab();
1187
1188 RefreshAllTabs();
1189 }
1190
1191 void wxNotebook::ScrollLastTo(int page)
1192 {
1193 wxCHECK_RET( IS_VALID_PAGE(page), _T("invalid notebook page") );
1194
1195 // go backwards until we find the first tab which can be made visible
1196 // without hiding the given one
1197 wxSize size = GetClientSize();
1198 wxCoord widthAll = IsVertical() ? size.y : size.x,
1199 widthTabs = GetTabWidth(page);
1200
1201 // the total width is less than the width of the window if we have the spin
1202 // button
1203 if ( HasSpinBtn() )
1204 {
1205 wxSize sizeSpinBtn = m_spinbtn->GetSize();
1206 if ( IsVertical() )
1207 widthAll -= sizeSpinBtn.y;
1208 else
1209 widthAll -= sizeSpinBtn.x;
1210 }
1211
1212 m_firstVisible = page;
1213 while ( (m_firstVisible > 0) && (widthTabs <= widthAll) )
1214 {
1215 widthTabs += GetTabWidth(--m_firstVisible);
1216 }
1217
1218 if ( widthTabs > widthAll )
1219 {
1220 // take one step back (that it is forward) if we can
1221 if ( m_firstVisible < (size_t)GetPageCount() - 1 )
1222 m_firstVisible++;
1223 }
1224
1225 // go to it
1226 ScrollTo(m_firstVisible);
1227
1228 // consitency check: the page we were asked to show should be shown
1229 wxASSERT_MSG( (size_t)page < m_lastVisible, _T("bug in ScrollLastTo") );
1230 }
1231
1232 // ----------------------------------------------------------------------------
1233 // wxNotebook sizing/moving
1234 // ----------------------------------------------------------------------------
1235
1236 wxSize wxNotebook::DoGetBestClientSize() const
1237 {
1238 // calculate the max page size
1239 wxSize size(0, 0);
1240
1241 size_t count = GetPageCount();
1242 if ( count )
1243 {
1244 for ( size_t n = 0; n < count; n++ )
1245 {
1246 wxSize sizePage = m_pages[n]->GetSize();
1247
1248 if ( size.x < sizePage.x )
1249 size.x = sizePage.x;
1250 if ( size.y < sizePage.y )
1251 size.y = sizePage.y;
1252 }
1253 }
1254 else // no pages
1255 {
1256 // use some arbitrary default size
1257 size.x =
1258 size.y = 100;
1259 }
1260
1261 return GetSizeForPage(size);
1262 }
1263
1264 void wxNotebook::DoMoveWindow(int x, int y, int width, int height)
1265 {
1266 wxControl::DoMoveWindow(x, y, width, height);
1267
1268 // move the spin ctrl too (NOP if it doesn't exist)
1269 PositionSpinBtn();
1270 }
1271
1272 void wxNotebook::DoSetSize(int x, int y,
1273 int width, int height,
1274 int sizeFlags)
1275 {
1276 wxSize old_client_size = GetClientSize();
1277
1278 wxControl::DoSetSize(x, y, width, height, sizeFlags);
1279
1280 wxSize new_client_size = GetClientSize();
1281
1282 if (old_client_size != new_client_size)
1283 Relayout();
1284 }
1285
1286 // ----------------------------------------------------------------------------
1287 // wxNotebook input processing
1288 // ----------------------------------------------------------------------------
1289
1290 bool wxNotebook::PerformAction(const wxControlAction& action,
1291 long numArg,
1292 const wxString& strArg)
1293 {
1294 if ( action == wxACTION_NOTEBOOK_NEXT )
1295 SetSelection(GetNextPage(TRUE));
1296 else if ( action == wxACTION_NOTEBOOK_PREV )
1297 SetSelection(GetNextPage(FALSE));
1298 else if ( action == wxACTION_NOTEBOOK_GOTO )
1299 SetSelection((int)numArg);
1300 else
1301 return wxControl::PerformAction(action, numArg, strArg);
1302
1303 return TRUE;
1304 }
1305
1306 // ----------------------------------------------------------------------------
1307 // wxStdNotebookInputHandler
1308 // ----------------------------------------------------------------------------
1309
1310 wxStdNotebookInputHandler::wxStdNotebookInputHandler(wxInputHandler *inphand)
1311 : wxStdInputHandler(inphand)
1312 {
1313 }
1314
1315 bool wxStdNotebookInputHandler::HandleKey(wxInputConsumer *consumer,
1316 const wxKeyEvent& event,
1317 bool pressed)
1318 {
1319 // ignore the key releases
1320 if ( pressed )
1321 {
1322 wxNotebook *notebook = wxStaticCast(consumer->GetInputWindow(), wxNotebook);
1323
1324 int page = 0;
1325 wxControlAction action;
1326 switch ( event.GetKeyCode() )
1327 {
1328 case WXK_UP:
1329 if ( notebook->IsVertical() )
1330 action = wxACTION_NOTEBOOK_PREV;
1331 break;
1332
1333 case WXK_LEFT:
1334 if ( !notebook->IsVertical() )
1335 action = wxACTION_NOTEBOOK_PREV;
1336 break;
1337
1338 case WXK_DOWN:
1339 if ( notebook->IsVertical() )
1340 action = wxACTION_NOTEBOOK_NEXT;
1341 break;
1342
1343 case WXK_RIGHT:
1344 if ( !notebook->IsVertical() )
1345 action = wxACTION_NOTEBOOK_NEXT;
1346 break;
1347
1348 case WXK_HOME:
1349 action = wxACTION_NOTEBOOK_GOTO;
1350 // page = 0; -- already has this value
1351 break;
1352
1353 case WXK_END:
1354 action = wxACTION_NOTEBOOK_GOTO;
1355 page = notebook->GetPageCount() - 1;
1356 break;
1357 }
1358
1359 if ( !!action )
1360 {
1361 return consumer->PerformAction(action, page);
1362 }
1363 }
1364
1365 return wxStdInputHandler::HandleKey(consumer, event, pressed);
1366 }
1367
1368 bool wxStdNotebookInputHandler::HandleMouse(wxInputConsumer *consumer,
1369 const wxMouseEvent& event)
1370 {
1371 if ( event.ButtonDown(1) )
1372 {
1373 wxNotebook *notebook = wxStaticCast(consumer->GetInputWindow(), wxNotebook);
1374 int page = notebook->HitTest(event.GetPosition());
1375 if ( page != -1 )
1376 {
1377 consumer->PerformAction(wxACTION_NOTEBOOK_GOTO, page);
1378
1379 return FALSE;
1380 }
1381 }
1382
1383 return wxStdInputHandler::HandleMouse(consumer, event);
1384 }
1385
1386 bool wxStdNotebookInputHandler::HandleMouseMove(wxInputConsumer *consumer,
1387 const wxMouseEvent& event)
1388 {
1389 return wxStdInputHandler::HandleMouseMove(consumer, event);
1390 }
1391
1392 bool wxStdNotebookInputHandler::HandleFocus(wxInputConsumer *consumer,
1393 const wxFocusEvent& event)
1394 {
1395 HandleFocusChange(consumer);
1396
1397 return FALSE;
1398 }
1399
1400 bool wxStdNotebookInputHandler::HandleActivation(wxInputConsumer *consumer,
1401 bool WXUNUSED(activated))
1402 {
1403 // we react to the focus change in the same way as to the [de]activation
1404 HandleFocusChange(consumer);
1405
1406 return FALSE;
1407 }
1408
1409 void wxStdNotebookInputHandler::HandleFocusChange(wxInputConsumer *consumer)
1410 {
1411 wxNotebook *notebook = wxStaticCast(consumer->GetInputWindow(), wxNotebook);
1412 notebook->RefreshCurrent();
1413 }
1414
1415 #endif // wxUSE_NOTEBOOK
1416