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