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