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