]> git.saurik.com Git - wxWidgets.git/blob - src/univ/notebook.cpp
create BS_GROUPBOX transparent
[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.y += indent.x;
749 if ( dir == wxLEFT )
750 {
751 rect.x += indent.y;
752 rect.width -= indent.y;
753 }
754 else // wxRIGHT
755 {
756 rect.width -= indent.y;
757 }
758 }
759 else // horz
760 {
761 rect.x += indent.x;
762 if ( dir == wxTOP )
763 {
764 rect.y += indent.y;
765 rect.height -= indent.y;
766 }
767 else // wxBOTTOM
768 {
769 rect.height -= indent.y;
770 }
771 }
772
773 return rect;
774 }
775
776 void wxNotebook::GetTabSize(int page, wxCoord *w, wxCoord *h) const
777 {
778 wxCHECK_RET( w && h, _T("NULL pointer in GetTabSize") );
779
780 if ( IsVertical() )
781 {
782 // width and height have inverted meaning
783 wxCoord *tmp = w;
784 w = h;
785 h = tmp;
786 }
787
788 // height is always fixed
789 *h = m_heightTab;
790
791 // width may also be fixed and be the same for all tabs
792 *w = GetTabWidth(page);
793 }
794
795 void wxNotebook::SetTabSize(const wxSize& sz)
796 {
797 wxCHECK_RET( FixedSizeTabs(), _T("SetTabSize() ignored") );
798
799 if ( IsVertical() )
800 {
801 m_heightTab = sz.x;
802 m_widthMax = sz.y;
803 }
804 else // horz
805 {
806 m_widthMax = sz.x;
807 m_heightTab = sz.y;
808 }
809 }
810
811 wxSize wxNotebook::CalcTabSize(int page) const
812 {
813 // NB: don't use m_widthMax, m_heightTab or m_widths here because this
814 // method is called to calculate them
815
816 wxSize size;
817
818 wxCHECK_MSG( IS_VALID_PAGE(page), size, _T("invalid notebook page") );
819
820 GetTextExtent(m_titles[page], &size.x, &size.y);
821
822 if ( HasImage(page) )
823 {
824 wxSize sizeImage;
825 m_imageList->GetSize(m_images[page], sizeImage.x, sizeImage.y);
826
827 size.x += sizeImage.x + 5; // FIXME: hard coded margin
828
829 if ( sizeImage.y > size.y )
830 size.y = sizeImage.y;
831 }
832
833 size.x += 2*m_sizePad.x;
834 size.y += 2*m_sizePad.y;
835
836 return size;
837 }
838
839 void wxNotebook::ResizeTab(int page)
840 {
841 wxSize sizeTab = CalcTabSize(page);
842
843 // we only need full relayout if the page size changes
844 bool needsRelayout = false;
845
846 if ( IsVertical() )
847 {
848 // swap coordinates
849 wxCoord tmp = sizeTab.x;
850 sizeTab.x = sizeTab.y;
851 sizeTab.y = tmp;
852 }
853
854 if ( sizeTab.y > m_heightTab )
855 {
856 needsRelayout = true;
857
858 m_heightTab = sizeTab.y;
859 }
860
861 m_widths[page] = sizeTab.x;
862
863 if ( sizeTab.x > m_widthMax )
864 m_widthMax = sizeTab.x;
865
866 // the total of the tabs has changed too
867 UpdateSpinBtn();
868
869 if ( needsRelayout )
870 Relayout();
871 else
872 RefreshAllTabs();
873 }
874
875 void wxNotebook::SetPadding(const wxSize& padding)
876 {
877 if ( padding != m_sizePad )
878 {
879 m_sizePad = padding;
880
881 Relayout();
882 }
883 }
884
885 void wxNotebook::Relayout()
886 {
887 if ( GetPageCount() )
888 {
889 RefreshAllTabs();
890
891 UpdateSpinBtn();
892
893 if ( m_sel != INVALID_PAGE )
894 {
895 // resize the currently shown page
896 wxRect rectPage = GetPageRect();
897
898 m_pages[m_sel]->SetSize(rectPage);
899
900 // also scroll it into view if needed (note that m_lastVisible
901 // was updated by the call to UpdateSpinBtn() above, this is why it
902 // is needed here)
903 if ( HasSpinBtn() )
904 {
905 if ( m_sel < m_firstVisible )
906 {
907 // selection is to the left of visible part of tabs
908 ScrollTo(m_sel);
909 }
910 else if ( m_sel > m_lastFullyVisible )
911 {
912 // selection is to the right of visible part of tabs
913 ScrollLastTo(m_sel);
914 }
915 }
916 }
917 }
918 else // we have no pages
919 {
920 // just refresh everything
921 Refresh();
922 }
923 }
924
925 wxRect wxNotebook::GetPagePart() const
926 {
927 wxRect rectPage = GetClientRect();
928
929 if ( GetPageCount() )
930 {
931 wxRect rectTabs = GetAllTabsRect();
932 wxDirection dir = GetTabOrientation();
933 if ( IsVertical() )
934 {
935 rectPage.width -= rectTabs.width;
936 if ( dir == wxLEFT )
937 rectPage.x += rectTabs.width;
938 }
939 else // horz
940 {
941 rectPage.height -= rectTabs.height;
942 if ( dir == wxTOP )
943 rectPage.y += rectTabs.height;
944 }
945 }
946 //else: no pages at all
947
948 return rectPage;
949 }
950
951 wxRect wxNotebook::GetPageRect() const
952 {
953 wxRect rect = GetPagePart();
954
955 // leave space for the border
956 wxRect rectBorder = GetRenderer()->GetBorderDimensions(wxBORDER_RAISED);
957
958 // FIXME: hardcoded +2!
959 rect.Inflate(-(rectBorder.x + rectBorder.width + 2),
960 -(rectBorder.y + rectBorder.height + 2));
961
962 return rect;
963 }
964
965 wxSize wxNotebook::GetSizeForPage(const wxSize& size) const
966 {
967 wxSize sizeNb = size;
968 wxRect rect = GetAllTabsRect();
969 if ( IsVertical() )
970 sizeNb.x += rect.width;
971 else
972 sizeNb.y += rect.height;
973
974 return sizeNb;
975 }
976
977 void wxNotebook::SetPageSize(const wxSize& size)
978 {
979 SetClientSize(GetSizeForPage(size));
980 }
981
982 wxSize wxNotebook::CalcSizeFromPage(const wxSize& sizePage) const
983 {
984 return AdjustSize(GetSizeForPage(sizePage));
985 }
986
987 // ----------------------------------------------------------------------------
988 // wxNotebook spin button
989 // ----------------------------------------------------------------------------
990
991 bool wxNotebook::HasSpinBtn() const
992 {
993 return m_spinbtn && m_spinbtn->IsShown();
994 }
995
996 void wxNotebook::CalcLastVisibleTab()
997 {
998 bool isVertical = IsVertical();
999
1000 wxCoord width = GetClientSize().x;
1001
1002 wxRect rect = GetTabsPart();
1003
1004 size_t count = GetPageCount();
1005
1006 wxCoord widthLast = 0;
1007 size_t n;
1008 for ( n = m_firstVisible; n < count; n++ )
1009 {
1010 GetTabSize(n, &rect.width, &rect.height);
1011 if ( rect.GetRight() > width )
1012 {
1013 break;
1014 }
1015
1016 // remember it to use below
1017 widthLast = rect.GetRight();
1018
1019 // move the rect to the next tab
1020 if ( isVertical )
1021 rect.y += rect.height;
1022 else
1023 rect.x += rect.width;
1024 }
1025
1026 if ( n == m_firstVisible )
1027 {
1028 // even the first tab isn't fully visible - but still pretend it is as
1029 // we have to show something
1030 m_lastFullyVisible = m_firstVisible;
1031 }
1032 else // more than one tab visible
1033 {
1034 m_lastFullyVisible = n - 1;
1035
1036 // but is it really fully visible? it shouldn't overlap with the spin
1037 // button if it is present (again, even if the first button does
1038 // overlap with it, we pretend that it doesn't as there is not much
1039 // else we can do)
1040 if ( (m_lastFullyVisible > m_firstVisible) && HasSpinBtn() )
1041 {
1042 // adjust width to be the width before the spin button
1043 wxSize sizeSpinBtn = m_spinbtn->GetSize();
1044 if ( IsVertical() )
1045 width -= sizeSpinBtn.y;
1046 else
1047 width -= sizeSpinBtn.x;
1048
1049 if ( widthLast > width )
1050 {
1051 // the last button overlaps with spin button, so take he
1052 // previous one
1053 m_lastFullyVisible--;
1054 }
1055 }
1056 }
1057
1058 if ( n == count )
1059 {
1060 // everything is visible
1061 m_lastVisible = n;
1062 }
1063 else
1064 {
1065 // this tab is still (partially) visible, so m_lastVisible is the
1066 // next tab (remember, this is "exclusive" last)
1067 m_lastVisible = n + 1;
1068
1069 }
1070 }
1071
1072 void wxNotebook::UpdateSpinBtn()
1073 {
1074 // first decide if we need a spin button
1075 bool allTabsShown;
1076
1077 size_t count = (size_t)GetPageCount();
1078 if ( count == 0 )
1079 {
1080 // this case is special, get rid of it immediately: everything is
1081 // visible and we don't need any spin buttons
1082 allTabsShown = true;
1083
1084 // have to reset them manually as we don't call CalcLastVisibleTab()
1085 m_firstVisible =
1086 m_lastVisible =
1087 m_lastFullyVisible = 0;
1088 }
1089 else
1090 {
1091 CalcLastVisibleTab();
1092
1093 // if all tabs after the first visible one are shown, it doesn't yet
1094 // mean that all tabs are shown - so we go backwards until we arrive to
1095 // the beginning (then all tabs are indeed shown) or find a tab such
1096 // that not all tabs after it are shown
1097 while ( (m_lastFullyVisible == count - 1) && (m_firstVisible > 0) )
1098 {
1099 // this is equivalent to ScrollTo(m_firstVisible - 1) but more
1100 // efficient
1101 m_offset -= GetTabWidth(m_firstVisible--);
1102
1103 // reclaculate after m_firstVisible change
1104 CalcLastVisibleTab();
1105 }
1106
1107 allTabsShown = m_lastFullyVisible == count - 1;
1108 }
1109
1110 if ( !allTabsShown )
1111 {
1112 if ( !m_spinbtn )
1113 {
1114 // create it once only
1115 m_spinbtn = new wxNotebookSpinBtn(this);
1116
1117 // set the correct value to keep it in sync
1118 m_spinbtn->SetValue(m_sel);
1119 }
1120
1121 // position it correctly
1122 PositionSpinBtn();
1123
1124 // and show it
1125 m_spinbtn->Show();
1126
1127 // also set/update the range
1128 m_spinbtn->SetRange(0, count - 1);
1129
1130 // update m_lastFullyVisible once again as it might have changed
1131 // because the spin button appeared
1132 //
1133 // FIXME: might do it more efficiently
1134 CalcLastVisibleTab();
1135 }
1136 else // all tabs are visible, we don't need spin button
1137 {
1138 if ( m_spinbtn && m_spinbtn -> IsShown() )
1139 {
1140 m_spinbtn->Hide();
1141 }
1142 }
1143 }
1144
1145 void wxNotebook::PositionSpinBtn()
1146 {
1147 if ( !m_spinbtn )
1148 return;
1149
1150 wxCoord wBtn, hBtn;
1151 m_spinbtn->GetSize(&wBtn, &hBtn);
1152
1153 wxRect rectTabs = GetAllTabsRect();
1154
1155 wxCoord x, y;
1156 switch ( GetTabOrientation() )
1157 {
1158 default:
1159 wxFAIL_MSG(_T("unknown tab orientation"));
1160 // fall through
1161
1162 case wxTOP:
1163 x = rectTabs.GetRight() - wBtn;
1164 y = rectTabs.GetBottom() - hBtn;
1165 break;
1166
1167 case wxBOTTOM:
1168 x = rectTabs.GetRight() - wBtn;
1169 y = rectTabs.GetTop();
1170 break;
1171
1172 case wxLEFT:
1173 x = rectTabs.GetRight() - wBtn;
1174 y = rectTabs.GetBottom() - hBtn;
1175 break;
1176
1177 case wxRIGHT:
1178 x = rectTabs.GetLeft();
1179 y = rectTabs.GetBottom() - hBtn;
1180 break;
1181 }
1182
1183 m_spinbtn->Move(x, y);
1184 }
1185
1186 // ----------------------------------------------------------------------------
1187 // wxNotebook scrolling
1188 // ----------------------------------------------------------------------------
1189
1190 void wxNotebook::ScrollTo(int page)
1191 {
1192 wxCHECK_RET( IS_VALID_PAGE(page), _T("invalid notebook page") );
1193
1194 // set the first visible tab and offset (easy)
1195 m_firstVisible = (size_t)page;
1196 m_offset = 0;
1197 for ( size_t n = 0; n < m_firstVisible; n++ )
1198 {
1199 m_offset += GetTabWidth(n);
1200 }
1201
1202 // find the last visible tab too
1203 CalcLastVisibleTab();
1204
1205 RefreshAllTabs();
1206 }
1207
1208 void wxNotebook::ScrollLastTo(int page)
1209 {
1210 wxCHECK_RET( IS_VALID_PAGE(page), _T("invalid notebook page") );
1211
1212 // go backwards until we find the first tab which can be made visible
1213 // without hiding the given one
1214 wxSize size = GetClientSize();
1215 wxCoord widthAll = IsVertical() ? size.y : size.x,
1216 widthTabs = GetTabWidth(page);
1217
1218 // the total width is less than the width of the window if we have the spin
1219 // button
1220 if ( HasSpinBtn() )
1221 {
1222 wxSize sizeSpinBtn = m_spinbtn->GetSize();
1223 if ( IsVertical() )
1224 widthAll -= sizeSpinBtn.y;
1225 else
1226 widthAll -= sizeSpinBtn.x;
1227 }
1228
1229 m_firstVisible = page;
1230 while ( (m_firstVisible > 0) && (widthTabs <= widthAll) )
1231 {
1232 widthTabs += GetTabWidth(--m_firstVisible);
1233 }
1234
1235 if ( widthTabs > widthAll )
1236 {
1237 // take one step back (that it is forward) if we can
1238 if ( m_firstVisible < (size_t)GetPageCount() - 1 )
1239 m_firstVisible++;
1240 }
1241
1242 // go to it
1243 ScrollTo(m_firstVisible);
1244
1245 // consitency check: the page we were asked to show should be shown
1246 wxASSERT_MSG( (size_t)page < m_lastVisible, _T("bug in ScrollLastTo") );
1247 }
1248
1249 // ----------------------------------------------------------------------------
1250 // wxNotebook sizing/moving
1251 // ----------------------------------------------------------------------------
1252
1253 wxSize wxNotebook::DoGetBestClientSize() const
1254 {
1255 // calculate the max page size
1256 wxSize size(0, 0);
1257
1258 size_t count = GetPageCount();
1259 if ( count )
1260 {
1261 for ( size_t n = 0; n < count; n++ )
1262 {
1263 wxSize sizePage = m_pages[n]->GetSize();
1264
1265 if ( size.x < sizePage.x )
1266 size.x = sizePage.x;
1267 if ( size.y < sizePage.y )
1268 size.y = sizePage.y;
1269 }
1270 }
1271 else // no pages
1272 {
1273 // use some arbitrary default size
1274 size.x =
1275 size.y = 100;
1276 }
1277
1278 return GetSizeForPage(size);
1279 }
1280
1281 void wxNotebook::DoMoveWindow(int x, int y, int width, int height)
1282 {
1283 wxControl::DoMoveWindow(x, y, width, height);
1284
1285 // move the spin ctrl too (NOP if it doesn't exist)
1286 PositionSpinBtn();
1287 }
1288
1289 void wxNotebook::DoSetSize(int x, int y,
1290 int width, int height,
1291 int sizeFlags)
1292 {
1293 wxSize old_client_size = GetClientSize();
1294
1295 wxControl::DoSetSize(x, y, width, height, sizeFlags);
1296
1297 wxSize new_client_size = GetClientSize();
1298
1299 if (old_client_size != new_client_size)
1300 Relayout();
1301 }
1302
1303 // ----------------------------------------------------------------------------
1304 // wxNotebook input processing
1305 // ----------------------------------------------------------------------------
1306
1307 bool wxNotebook::PerformAction(const wxControlAction& action,
1308 long numArg,
1309 const wxString& strArg)
1310 {
1311 if ( action == wxACTION_NOTEBOOK_NEXT )
1312 SetSelection(GetNextPage(true));
1313 else if ( action == wxACTION_NOTEBOOK_PREV )
1314 SetSelection(GetNextPage(false));
1315 else if ( action == wxACTION_NOTEBOOK_GOTO )
1316 SetSelection((int)numArg);
1317 else
1318 return wxControl::PerformAction(action, numArg, strArg);
1319
1320 return true;
1321 }
1322
1323 // ----------------------------------------------------------------------------
1324 // wxStdNotebookInputHandler
1325 // ----------------------------------------------------------------------------
1326
1327 wxStdNotebookInputHandler::wxStdNotebookInputHandler(wxInputHandler *inphand)
1328 : wxStdInputHandler(inphand)
1329 {
1330 }
1331
1332 bool wxStdNotebookInputHandler::HandleKey(wxInputConsumer *consumer,
1333 const wxKeyEvent& event,
1334 bool pressed)
1335 {
1336 // ignore the key releases
1337 if ( pressed )
1338 {
1339 wxNotebook *notebook = wxStaticCast(consumer->GetInputWindow(), wxNotebook);
1340
1341 int page = 0;
1342 wxControlAction action;
1343 switch ( event.GetKeyCode() )
1344 {
1345 case WXK_UP:
1346 if ( notebook->IsVertical() )
1347 action = wxACTION_NOTEBOOK_PREV;
1348 break;
1349
1350 case WXK_LEFT:
1351 if ( !notebook->IsVertical() )
1352 action = wxACTION_NOTEBOOK_PREV;
1353 break;
1354
1355 case WXK_DOWN:
1356 if ( notebook->IsVertical() )
1357 action = wxACTION_NOTEBOOK_NEXT;
1358 break;
1359
1360 case WXK_RIGHT:
1361 if ( !notebook->IsVertical() )
1362 action = wxACTION_NOTEBOOK_NEXT;
1363 break;
1364
1365 case WXK_HOME:
1366 action = wxACTION_NOTEBOOK_GOTO;
1367 // page = 0; -- already has this value
1368 break;
1369
1370 case WXK_END:
1371 action = wxACTION_NOTEBOOK_GOTO;
1372 page = notebook->GetPageCount() - 1;
1373 break;
1374 }
1375
1376 if ( !action.IsEmpty() )
1377 {
1378 return consumer->PerformAction(action, page);
1379 }
1380 }
1381
1382 return wxStdInputHandler::HandleKey(consumer, event, pressed);
1383 }
1384
1385 bool wxStdNotebookInputHandler::HandleMouse(wxInputConsumer *consumer,
1386 const wxMouseEvent& event)
1387 {
1388 if ( event.ButtonDown(1) )
1389 {
1390 wxNotebook *notebook = wxStaticCast(consumer->GetInputWindow(), wxNotebook);
1391 int page = notebook->HitTest(event.GetPosition());
1392 if ( page != -1 )
1393 {
1394 consumer->PerformAction(wxACTION_NOTEBOOK_GOTO, page);
1395
1396 return false;
1397 }
1398 }
1399
1400 return wxStdInputHandler::HandleMouse(consumer, event);
1401 }
1402
1403 bool wxStdNotebookInputHandler::HandleMouseMove(wxInputConsumer *consumer,
1404 const wxMouseEvent& event)
1405 {
1406 return wxStdInputHandler::HandleMouseMove(consumer, event);
1407 }
1408
1409 bool
1410 wxStdNotebookInputHandler::HandleFocus(wxInputConsumer *consumer,
1411 const wxFocusEvent& WXUNUSED(event))
1412 {
1413 HandleFocusChange(consumer);
1414
1415 return false;
1416 }
1417
1418 bool wxStdNotebookInputHandler::HandleActivation(wxInputConsumer *consumer,
1419 bool WXUNUSED(activated))
1420 {
1421 // we react to the focus change in the same way as to the [de]activation
1422 HandleFocusChange(consumer);
1423
1424 return false;
1425 }
1426
1427 void wxStdNotebookInputHandler::HandleFocusChange(wxInputConsumer *consumer)
1428 {
1429 wxNotebook *notebook = wxStaticCast(consumer->GetInputWindow(), wxNotebook);
1430 notebook->RefreshCurrent();
1431 }
1432
1433 #endif // wxUSE_NOTEBOOK
1434