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