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