]> git.saurik.com Git - wxWidgets.git/blame - src/univ/notebook.cpp
Add #if checks fixing minimal wxGTK build.
[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
681be2ef 228 if ( (size_t)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
681be2ef 247 size_t 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
681be2ef 266 if ( m_selection < m_firstVisible )
1e6feb95
VZ
267 {
268 // selection is to the left of visible part of tabs
681be2ef 269 ScrollTo(m_selection);
1e6feb95 270 }
681be2ef 271 else if ( m_selection > m_lastFullyVisible )
1e6feb95
VZ
272 {
273 // selection is to the right of visible part of tabs
681be2ef 274 ScrollLastTo(m_selection);
1e6feb95
VZ
275 }
276 else // we already see this tab
277 {
278 // no need to scroll
681be2ef 279 RefreshTab(m_selection);
1e6feb95
VZ
280 }
281
681be2ef
VZ
282 m_pages[m_selection]->SetSize(GetPageRect());
283 m_pages[m_selection]->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 {
681be2ef 403 if ( m_selection == (size_t)nPage )
1e6feb95
VZ
404 {
405 // avoid sending event to this page which doesn't exist in the
406 // notebook any more
681be2ef 407 m_selection = wxNOT_FOUND;
1e6feb95
VZ
408
409 SetSelection(nPage == count ? nPage - 1 : nPage);
410 }
681be2ef 411 else if ( m_selection > (size_t)nPage )
1e6feb95
VZ
412 {
413 // no need to change selection, just adjust the index
681be2ef 414 m_selection--;
1e6feb95
VZ
415 }
416 }
417 else // no more tabs left
418 {
681be2ef 419 m_selection = wxNOT_FOUND;
1e6feb95
VZ
420 }
421
422 // have to refresh everything
423 Relayout();
424
425 return page;
426}
427
428// ----------------------------------------------------------------------------
429// wxNotebook drawing
430// ----------------------------------------------------------------------------
431
432void wxNotebook::RefreshCurrent()
433{
681be2ef 434 if ( m_selection != wxNOT_FOUND )
1e6feb95 435 {
681be2ef 436 RefreshTab(m_selection);
1e6feb95
VZ
437 }
438}
439
1d5c2a8e 440void wxNotebook::RefreshTab(int page, bool forceSelected)
1e6feb95 441{
9a83f860 442 wxCHECK_RET( IS_VALID_PAGE(page), wxT("invalid notebook page") );
1e6feb95
VZ
443
444 wxRect rect = GetTabRect(page);
681be2ef 445 if ( forceSelected || ((size_t)page == m_selection) )
1e6feb95
VZ
446 {
447 const wxSize indent = GetRenderer()->GetTabIndent();
448 rect.Inflate(indent.x, indent.y);
449 }
450
451 RefreshRect(rect);
452}
453
454void wxNotebook::RefreshAllTabs()
455{
456 wxRect rect = GetAllTabsRect();
457 if ( rect.width || rect.height )
458 {
459 RefreshRect(rect);
460 }
461 //else: we don't have tabs at all
462}
463
464void wxNotebook::DoDrawTab(wxDC& dc, const wxRect& rect, size_t n)
465{
466 wxBitmap bmp;
467 if ( HasImage(n) )
468 {
469 int image = m_images[n];
470
c482b99e
JS
471 // Not needed now that wxGenericImageList is being
472 // used for wxUniversal under MSW
473#if 0 // def __WXMSW__ // FIXME
1e6feb95
VZ
474 int w, h;
475 m_imageList->GetSize(n, w, h);
476 bmp.Create(w, h);
477 wxMemoryDC dc;
478 dc.SelectObject(bmp);
479 dc.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID));
a290fa5a 480 m_imageList->Draw(image, dc, 0, 0, wxIMAGELIST_DRAW_NORMAL, true);
62e1ba75 481 dc.SelectObject(wxNullBitmap);
1e6feb95 482#else
49bf4e3e 483 bmp = m_imageList->GetBitmap(image);
1e6feb95
VZ
484#endif
485 }
486
487 int flags = 0;
681be2ef 488 if ( n == m_selection )
1e6feb95
VZ
489 {
490 flags |= wxCONTROL_SELECTED;
491
492 if ( IsFocused() )
493 flags |= wxCONTROL_FOCUSED;
494 }
495
496 GetRenderer()->DrawTab
497 (
498 dc,
499 rect,
500 GetTabOrientation(),
501 m_titles[n],
502 bmp,
503 flags,
504 m_accels[n]
505 );
506}
507
508void wxNotebook::DoDraw(wxControlRenderer *renderer)
509{
510 //wxRect rectUpdate = GetUpdateClientRect(); -- unused
511
512 wxDC& dc = renderer->GetDC();
513 dc.SetFont(GetFont());
514 dc.SetTextForeground(GetForegroundColour());
515
516 // redraw the border - it's simpler to always do it instead of checking
517 // whether this needs to be done
518 GetRenderer()->DrawBorder(dc, wxBORDER_RAISED, GetPagePart());
519
520 // avoid overwriting the spin button
521 if ( HasSpinBtn() )
522 {
523 wxRect rectTabs = GetAllTabsRect();
524 wxSize sizeSpinBtn = m_spinbtn->GetSize();
525
526 if ( IsVertical() )
2b5f62a0 527 {
1e6feb95 528 rectTabs.height -= sizeSpinBtn.y;
2b5f62a0
VZ
529
530 // Allow for erasing the line under selected tab
531 rectTabs.width += 2;
532 }
1e6feb95 533 else
2b5f62a0 534 {
1e6feb95
VZ
535 rectTabs.width -= sizeSpinBtn.x;
536
2b5f62a0
VZ
537 // Allow for erasing the line under selected tab
538 rectTabs.height += 2;
539 }
540
1e6feb95
VZ
541 dc.SetClippingRegion(rectTabs);
542 }
543
544 wxRect rect = GetTabsPart();
545 bool isVertical = IsVertical();
546
547 wxRect rectSel;
548 for ( size_t n = m_firstVisible; n < m_lastVisible; n++ )
549 {
550 GetTabSize(n, &rect.width, &rect.height);
551
681be2ef 552 if ( n == m_selection )
1e6feb95
VZ
553 {
554 // don't redraw it now as this tab has to be drawn over the other
555 // ones as it takes more place and spills over to them
556 rectSel = rect;
557 }
558 else // not selected tab
559 {
560 // unfortunately we can't do this because the selected tab hangs
561 // over its neighbours and so we might need to refresh more tabs -
562 // of course, we could still avoid rereshing some of them with more
563 // complicated checks, but it doesn't seem too bad to refresh all
564 // of them, I still don't see flicker, so leaving as is for now
565
566 //if ( rectUpdate.Intersects(rect) )
567 {
568 DoDrawTab(dc, rect, n);
569 }
570 //else: doesn't need to be refreshed
571 }
572
573 // move the rect to the next tab
574 if ( isVertical )
575 rect.y += rect.height;
576 else
577 rect.x += rect.width;
578 }
579
580 // now redraw the selected tab
581 if ( rectSel.width )
582 {
681be2ef 583 DoDrawTab(dc, rectSel, m_selection);
1e6feb95 584 }
2b5f62a0
VZ
585
586 dc.DestroyClippingRegion();
1e6feb95
VZ
587}
588
589// ----------------------------------------------------------------------------
590// wxNotebook geometry
591// ----------------------------------------------------------------------------
592
e450aa69 593int wxNotebook::HitTest(const wxPoint& pt, long *flags) const
1e6feb95 594{
e450aa69 595 if ( flags )
9804d540 596 *flags = wxBK_HITTEST_NOWHERE;
e450aa69 597
1e6feb95 598 // first check that it is in this window at all
22a35096 599 if ( !GetClientRect().Contains(pt) )
1e6feb95
VZ
600 {
601 return -1;
602 }
603
604 wxRect rectTabs = GetAllTabsRect();
605
606 switch ( GetTabOrientation() )
607 {
608 default:
9a83f860 609 wxFAIL_MSG(wxT("unknown tab orientation"));
1e6feb95
VZ
610 // fall through
611
612 case wxTOP:
613 if ( pt.y > rectTabs.GetBottom() )
614 return -1;
615 break;
616
617 case wxBOTTOM:
618 if ( pt.y < rectTabs.y )
619 return -1;
620 break;
621
622 case wxLEFT:
623 if ( pt.x > rectTabs.GetRight() )
624 return -1;
625 break;
626
627 case wxRIGHT:
628 if ( pt.x < rectTabs.x )
629 return -1;
630 break;
631 }
632
633 for ( size_t n = m_firstVisible; n < m_lastVisible; n++ )
634 {
635 GetTabSize(n, &rectTabs.width, &rectTabs.height);
636
22a35096 637 if ( rectTabs.Contains(pt) )
e450aa69
VZ
638 {
639 if ( flags )
640 {
641 // TODO: be more precise
9804d540 642 *flags = wxBK_HITTEST_ONITEM;
e450aa69
VZ
643 }
644
1e6feb95 645 return n;
e450aa69 646 }
1e6feb95
VZ
647
648 // move the rectTabs to the next tab
649 if ( IsVertical() )
650 rectTabs.y += rectTabs.height;
651 else
652 rectTabs.x += rectTabs.width;
653 }
654
655 return -1;
656}
657
658bool wxNotebook::IsVertical() const
659{
660 wxDirection dir = GetTabOrientation();
661
662 return dir == wxLEFT || dir == wxRIGHT;
663}
664
665wxDirection wxNotebook::GetTabOrientation() const
666{
667 long style = GetWindowStyle();
90c0f5a9 668 if ( style & wxBK_BOTTOM )
1e6feb95 669 return wxBOTTOM;
90c0f5a9 670 else if ( style & wxBK_RIGHT )
1e6feb95 671 return wxRIGHT;
90c0f5a9 672 else if ( style & wxBK_LEFT )
1e6feb95
VZ
673 return wxLEFT;
674
90c0f5a9 675 // wxBK_TOP == 0 so we don't have to test for it
1e6feb95
VZ
676 return wxTOP;
677}
678
679wxRect wxNotebook::GetTabRect(int page) const
680{
681 wxRect rect;
9a83f860 682 wxCHECK_MSG( IS_VALID_PAGE(page), rect, wxT("invalid notebook page") );
1e6feb95
VZ
683
684 // calc the size of this tab and of the preceding ones
685 wxCoord widthThis, widthBefore;
686 if ( FixedSizeTabs() )
687 {
688 widthThis = m_widthMax;
689 widthBefore = page*m_widthMax;
690 }
691 else
692 {
693 widthBefore = 0;
694 for ( int n = 0; n < page; n++ )
695 {
696 widthBefore += m_widths[n];
697 }
698
699 widthThis = m_widths[page];
700 }
701
702 rect = GetTabsPart();
703 if ( IsVertical() )
704 {
705 rect.y += widthBefore - m_offset;
706 rect.height = widthThis;
707 }
708 else // horz
709 {
710 rect.x += widthBefore - m_offset;
711 rect.width = widthThis;
712 }
713
714 return rect;
715}
716
717wxRect wxNotebook::GetAllTabsRect() const
718{
719 wxRect rect;
720
721 if ( GetPageCount() )
722 {
723 const wxSize indent = GetRenderer()->GetTabIndent();
724 wxSize size = GetClientSize();
725
726 if ( IsVertical() )
727 {
728 rect.width = m_heightTab + indent.x;
729 rect.x = GetTabOrientation() == wxLEFT ? 0 : size.x - rect.width;
730 rect.y = 0;
731 rect.height = size.y;
732 }
733 else // horz
734 {
735 rect.x = 0;
736 rect.width = size.x;
737 rect.height = m_heightTab + indent.y;
738 rect.y = GetTabOrientation() == wxTOP ? 0 : size.y - rect.height;
739 }
740 }
741 //else: no pages
742
743 return rect;
744}
745
746wxRect wxNotebook::GetTabsPart() const
747{
748 wxRect rect = GetAllTabsRect();
749
750 wxDirection dir = GetTabOrientation();
751
752 const wxSize indent = GetRenderer()->GetTabIndent();
753 if ( IsVertical() )
754 {
1e6feb95 755 rect.y += indent.x;
00e086a7
WS
756 if ( dir == wxLEFT )
757 {
758 rect.x += indent.y;
759 rect.width -= indent.y;
760 }
761 else // wxRIGHT
762 {
763 rect.width -= indent.y;
764 }
1e6feb95
VZ
765 }
766 else // horz
767 {
768 rect.x += indent.x;
769 if ( dir == wxTOP )
770 {
771 rect.y += indent.y;
772 rect.height -= indent.y;
773 }
774 else // wxBOTTOM
775 {
776 rect.height -= indent.y;
777 }
778 }
779
780 return rect;
781}
782
783void wxNotebook::GetTabSize(int page, wxCoord *w, wxCoord *h) const
784{
9a83f860 785 wxCHECK_RET( w && h, wxT("NULL pointer in GetTabSize") );
1e6feb95
VZ
786
787 if ( IsVertical() )
788 {
789 // width and height have inverted meaning
790 wxCoord *tmp = w;
791 w = h;
792 h = tmp;
793 }
794
795 // height is always fixed
796 *h = m_heightTab;
797
798 // width may also be fixed and be the same for all tabs
799 *w = GetTabWidth(page);
800}
801
802void wxNotebook::SetTabSize(const wxSize& sz)
803{
9a83f860 804 wxCHECK_RET( FixedSizeTabs(), wxT("SetTabSize() ignored") );
1e6feb95
VZ
805
806 if ( IsVertical() )
807 {
808 m_heightTab = sz.x;
809 m_widthMax = sz.y;
810 }
811 else // horz
812 {
813 m_widthMax = sz.x;
814 m_heightTab = sz.y;
815 }
816}
817
818wxSize wxNotebook::CalcTabSize(int page) const
819{
820 // NB: don't use m_widthMax, m_heightTab or m_widths here because this
821 // method is called to calculate them
822
823 wxSize size;
824
9a83f860 825 wxCHECK_MSG( IS_VALID_PAGE(page), size, wxT("invalid notebook page") );
1e6feb95
VZ
826
827 GetTextExtent(m_titles[page], &size.x, &size.y);
828
829 if ( HasImage(page) )
830 {
831 wxSize sizeImage;
832 m_imageList->GetSize(m_images[page], sizeImage.x, sizeImage.y);
833
834 size.x += sizeImage.x + 5; // FIXME: hard coded margin
835
836 if ( sizeImage.y > size.y )
837 size.y = sizeImage.y;
838 }
839
840 size.x += 2*m_sizePad.x;
841 size.y += 2*m_sizePad.y;
842
843 return size;
844}
845
846void wxNotebook::ResizeTab(int page)
847{
848 wxSize sizeTab = CalcTabSize(page);
849
850 // we only need full relayout if the page size changes
a290fa5a 851 bool needsRelayout = false;
1e6feb95
VZ
852
853 if ( IsVertical() )
854 {
855 // swap coordinates
856 wxCoord tmp = sizeTab.x;
857 sizeTab.x = sizeTab.y;
858 sizeTab.y = tmp;
859 }
860
861 if ( sizeTab.y > m_heightTab )
862 {
a290fa5a 863 needsRelayout = true;
1e6feb95
VZ
864
865 m_heightTab = sizeTab.y;
866 }
867
868 m_widths[page] = sizeTab.x;
869
870 if ( sizeTab.x > m_widthMax )
871 m_widthMax = sizeTab.x;
872
873 // the total of the tabs has changed too
874 UpdateSpinBtn();
875
876 if ( needsRelayout )
877 Relayout();
878 else
879 RefreshAllTabs();
880}
881
882void wxNotebook::SetPadding(const wxSize& padding)
883{
884 if ( padding != m_sizePad )
885 {
886 m_sizePad = padding;
887
888 Relayout();
889 }
890}
891
892void wxNotebook::Relayout()
893{
894 if ( GetPageCount() )
895 {
896 RefreshAllTabs();
897
898 UpdateSpinBtn();
899
681be2ef 900 if ( m_selection != wxNOT_FOUND )
1e6feb95
VZ
901 {
902 // resize the currently shown page
903 wxRect rectPage = GetPageRect();
904
681be2ef 905 m_pages[m_selection]->SetSize(rectPage);
1e6feb95
VZ
906
907 // also scroll it into view if needed (note that m_lastVisible
908 // was updated by the call to UpdateSpinBtn() above, this is why it
909 // is needed here)
910 if ( HasSpinBtn() )
911 {
681be2ef 912 if ( m_selection < m_firstVisible )
1e6feb95
VZ
913 {
914 // selection is to the left of visible part of tabs
681be2ef 915 ScrollTo(m_selection);
1e6feb95 916 }
681be2ef 917 else if ( m_selection > m_lastFullyVisible )
1e6feb95
VZ
918 {
919 // selection is to the right of visible part of tabs
681be2ef 920 ScrollLastTo(m_selection);
1e6feb95
VZ
921 }
922 }
923 }
924 }
925 else // we have no pages
926 {
927 // just refresh everything
928 Refresh();
929 }
930}
931
932wxRect wxNotebook::GetPagePart() const
933{
934 wxRect rectPage = GetClientRect();
935
936 if ( GetPageCount() )
937 {
938 wxRect rectTabs = GetAllTabsRect();
939 wxDirection dir = GetTabOrientation();
940 if ( IsVertical() )
941 {
942 rectPage.width -= rectTabs.width;
943 if ( dir == wxLEFT )
944 rectPage.x += rectTabs.width;
945 }
946 else // horz
947 {
948 rectPage.height -= rectTabs.height;
949 if ( dir == wxTOP )
950 rectPage.y += rectTabs.height;
951 }
952 }
953 //else: no pages at all
954
955 return rectPage;
956}
957
958wxRect wxNotebook::GetPageRect() const
959{
960 wxRect rect = GetPagePart();
961
962 // leave space for the border
963 wxRect rectBorder = GetRenderer()->GetBorderDimensions(wxBORDER_RAISED);
964
965 // FIXME: hardcoded +2!
966 rect.Inflate(-(rectBorder.x + rectBorder.width + 2),
967 -(rectBorder.y + rectBorder.height + 2));
968
969 return rect;
970}
971
972wxSize wxNotebook::GetSizeForPage(const wxSize& size) const
973{
974 wxSize sizeNb = size;
975 wxRect rect = GetAllTabsRect();
976 if ( IsVertical() )
977 sizeNb.x += rect.width;
978 else
979 sizeNb.y += rect.height;
980
981 return sizeNb;
982}
983
984void wxNotebook::SetPageSize(const wxSize& size)
985{
986 SetClientSize(GetSizeForPage(size));
987}
988
2ce7af35 989wxSize wxNotebook::CalcSizeFromPage(const wxSize& sizePage) const
1e6feb95
VZ
990{
991 return AdjustSize(GetSizeForPage(sizePage));
992}
993
994// ----------------------------------------------------------------------------
995// wxNotebook spin button
996// ----------------------------------------------------------------------------
997
998bool wxNotebook::HasSpinBtn() const
999{
1000 return m_spinbtn && m_spinbtn->IsShown();
1001}
1002
1003void wxNotebook::CalcLastVisibleTab()
1004{
1005 bool isVertical = IsVertical();
1006
1007 wxCoord width = GetClientSize().x;
1008
1009 wxRect rect = GetTabsPart();
1010
1011 size_t count = GetPageCount();
1012
1013 wxCoord widthLast = 0;
1014 size_t n;
1015 for ( n = m_firstVisible; n < count; n++ )
1016 {
1017 GetTabSize(n, &rect.width, &rect.height);
1018 if ( rect.GetRight() > width )
1019 {
1020 break;
1021 }
1022
1023 // remember it to use below
1024 widthLast = rect.GetRight();
1025
1026 // move the rect to the next tab
1027 if ( isVertical )
1028 rect.y += rect.height;
1029 else
1030 rect.x += rect.width;
1031 }
1032
1033 if ( n == m_firstVisible )
1034 {
1035 // even the first tab isn't fully visible - but still pretend it is as
1036 // we have to show something
1037 m_lastFullyVisible = m_firstVisible;
1038 }
1039 else // more than one tab visible
1040 {
1041 m_lastFullyVisible = n - 1;
1042
1043 // but is it really fully visible? it shouldn't overlap with the spin
1044 // button if it is present (again, even if the first button does
1045 // overlap with it, we pretend that it doesn't as there is not much
1046 // else we can do)
1047 if ( (m_lastFullyVisible > m_firstVisible) && HasSpinBtn() )
1048 {
1049 // adjust width to be the width before the spin button
1050 wxSize sizeSpinBtn = m_spinbtn->GetSize();
1051 if ( IsVertical() )
1052 width -= sizeSpinBtn.y;
1053 else
1054 width -= sizeSpinBtn.x;
1055
1056 if ( widthLast > width )
1057 {
1058 // the last button overlaps with spin button, so take he
1059 // previous one
1060 m_lastFullyVisible--;
1061 }
1062 }
1063 }
1064
1065 if ( n == count )
1066 {
1067 // everything is visible
1068 m_lastVisible = n;
1069 }
1070 else
1071 {
1072 // this tab is still (partially) visible, so m_lastVisible is the
1073 // next tab (remember, this is "exclusive" last)
1074 m_lastVisible = n + 1;
1075
1076 }
1077}
1078
1079void wxNotebook::UpdateSpinBtn()
1080{
1081 // first decide if we need a spin button
1082 bool allTabsShown;
1083
1084 size_t count = (size_t)GetPageCount();
1085 if ( count == 0 )
1086 {
1087 // this case is special, get rid of it immediately: everything is
1088 // visible and we don't need any spin buttons
a290fa5a 1089 allTabsShown = true;
1e6feb95
VZ
1090
1091 // have to reset them manually as we don't call CalcLastVisibleTab()
1092 m_firstVisible =
1093 m_lastVisible =
1094 m_lastFullyVisible = 0;
1095 }
1096 else
1097 {
1098 CalcLastVisibleTab();
1099
1100 // if all tabs after the first visible one are shown, it doesn't yet
1101 // mean that all tabs are shown - so we go backwards until we arrive to
1102 // the beginning (then all tabs are indeed shown) or find a tab such
1103 // that not all tabs after it are shown
1104 while ( (m_lastFullyVisible == count - 1) && (m_firstVisible > 0) )
1105 {
1106 // this is equivalent to ScrollTo(m_firstVisible - 1) but more
1107 // efficient
1108 m_offset -= GetTabWidth(m_firstVisible--);
1109
1110 // reclaculate after m_firstVisible change
1111 CalcLastVisibleTab();
1112 }
1113
1114 allTabsShown = m_lastFullyVisible == count - 1;
1115 }
1116
1117 if ( !allTabsShown )
1118 {
1119 if ( !m_spinbtn )
1120 {
1121 // create it once only
1122 m_spinbtn = new wxNotebookSpinBtn(this);
1123
1124 // set the correct value to keep it in sync
681be2ef 1125 m_spinbtn->SetValue(m_selection);
1e6feb95
VZ
1126 }
1127
1128 // position it correctly
1129 PositionSpinBtn();
1130
1131 // and show it
1132 m_spinbtn->Show();
1133
1134 // also set/update the range
1135 m_spinbtn->SetRange(0, count - 1);
1136
1137 // update m_lastFullyVisible once again as it might have changed
1138 // because the spin button appeared
1139 //
1140 // FIXME: might do it more efficiently
1141 CalcLastVisibleTab();
1142 }
1143 else // all tabs are visible, we don't need spin button
1144 {
b039ef1d 1145 if ( m_spinbtn && m_spinbtn -> IsShown() )
1e6feb95
VZ
1146 {
1147 m_spinbtn->Hide();
1148 }
1149 }
1150}
1151
1152void wxNotebook::PositionSpinBtn()
1153{
1154 if ( !m_spinbtn )
1155 return;
1156
1157 wxCoord wBtn, hBtn;
1158 m_spinbtn->GetSize(&wBtn, &hBtn);
1159
1160 wxRect rectTabs = GetAllTabsRect();
1161
1162 wxCoord x, y;
1163 switch ( GetTabOrientation() )
1164 {
1165 default:
9a83f860 1166 wxFAIL_MSG(wxT("unknown tab orientation"));
1e6feb95
VZ
1167 // fall through
1168
1169 case wxTOP:
1170 x = rectTabs.GetRight() - wBtn;
1171 y = rectTabs.GetBottom() - hBtn;
1172 break;
1173
1174 case wxBOTTOM:
1175 x = rectTabs.GetRight() - wBtn;
1176 y = rectTabs.GetTop();
1177 break;
1178
1179 case wxLEFT:
1180 x = rectTabs.GetRight() - wBtn;
1181 y = rectTabs.GetBottom() - hBtn;
1182 break;
1183
1184 case wxRIGHT:
1185 x = rectTabs.GetLeft();
1186 y = rectTabs.GetBottom() - hBtn;
1187 break;
1188 }
1189
1190 m_spinbtn->Move(x, y);
1191}
1192
1193// ----------------------------------------------------------------------------
1194// wxNotebook scrolling
1195// ----------------------------------------------------------------------------
1196
1197void wxNotebook::ScrollTo(int page)
1198{
9a83f860 1199 wxCHECK_RET( IS_VALID_PAGE(page), wxT("invalid notebook page") );
1e6feb95
VZ
1200
1201 // set the first visible tab and offset (easy)
1202 m_firstVisible = (size_t)page;
1203 m_offset = 0;
1204 for ( size_t n = 0; n < m_firstVisible; n++ )
1205 {
1206 m_offset += GetTabWidth(n);
1207 }
1208
1209 // find the last visible tab too
1210 CalcLastVisibleTab();
1211
1212 RefreshAllTabs();
1213}
1214
1215void wxNotebook::ScrollLastTo(int page)
1216{
9a83f860 1217 wxCHECK_RET( IS_VALID_PAGE(page), wxT("invalid notebook page") );
1e6feb95
VZ
1218
1219 // go backwards until we find the first tab which can be made visible
1220 // without hiding the given one
1221 wxSize size = GetClientSize();
1222 wxCoord widthAll = IsVertical() ? size.y : size.x,
1223 widthTabs = GetTabWidth(page);
1224
1225 // the total width is less than the width of the window if we have the spin
1226 // button
1227 if ( HasSpinBtn() )
1228 {
1229 wxSize sizeSpinBtn = m_spinbtn->GetSize();
1230 if ( IsVertical() )
1231 widthAll -= sizeSpinBtn.y;
1232 else
1233 widthAll -= sizeSpinBtn.x;
1234 }
1235
1236 m_firstVisible = page;
1237 while ( (m_firstVisible > 0) && (widthTabs <= widthAll) )
1238 {
1239 widthTabs += GetTabWidth(--m_firstVisible);
1240 }
1241
1242 if ( widthTabs > widthAll )
1243 {
1244 // take one step back (that it is forward) if we can
1245 if ( m_firstVisible < (size_t)GetPageCount() - 1 )
1246 m_firstVisible++;
1247 }
1248
1249 // go to it
1250 ScrollTo(m_firstVisible);
1251
1252 // consitency check: the page we were asked to show should be shown
9a83f860 1253 wxASSERT_MSG( (size_t)page < m_lastVisible, wxT("bug in ScrollLastTo") );
1e6feb95
VZ
1254}
1255
1256// ----------------------------------------------------------------------------
1257// wxNotebook sizing/moving
1258// ----------------------------------------------------------------------------
1259
1260wxSize wxNotebook::DoGetBestClientSize() const
1261{
1262 // calculate the max page size
8b5d5223 1263 wxSize size;
1e6feb95
VZ
1264
1265 size_t count = GetPageCount();
1266 if ( count )
1267 {
1268 for ( size_t n = 0; n < count; n++ )
1269 {
1270 wxSize sizePage = m_pages[n]->GetSize();
1271
1272 if ( size.x < sizePage.x )
1273 size.x = sizePage.x;
1274 if ( size.y < sizePage.y )
1275 size.y = sizePage.y;
1276 }
1277 }
1278 else // no pages
1279 {
1280 // use some arbitrary default size
1281 size.x =
1282 size.y = 100;
1283 }
1284
1285 return GetSizeForPage(size);
1286}
1287
1288void wxNotebook::DoMoveWindow(int x, int y, int width, int height)
1289{
1290 wxControl::DoMoveWindow(x, y, width, height);
1291
1292 // move the spin ctrl too (NOP if it doesn't exist)
1293 PositionSpinBtn();
1294}
1295
1296void wxNotebook::DoSetSize(int x, int y,
1297 int width, int height,
1298 int sizeFlags)
1299{
2d4b5a5e 1300 wxSize old_client_size = GetClientSize();
1e6feb95 1301
2d4b5a5e 1302 wxControl::DoSetSize(x, y, width, height, sizeFlags);
32b13913 1303
2d4b5a5e 1304 wxSize new_client_size = GetClientSize();
32b13913 1305
2d4b5a5e
RR
1306 if (old_client_size != new_client_size)
1307 Relayout();
1e6feb95
VZ
1308}
1309
1310// ----------------------------------------------------------------------------
1311// wxNotebook input processing
1312// ----------------------------------------------------------------------------
1313
1314bool wxNotebook::PerformAction(const wxControlAction& action,
1315 long numArg,
1316 const wxString& strArg)
1317{
1318 if ( action == wxACTION_NOTEBOOK_NEXT )
a290fa5a 1319 SetSelection(GetNextPage(true));
1e6feb95 1320 else if ( action == wxACTION_NOTEBOOK_PREV )
a290fa5a 1321 SetSelection(GetNextPage(false));
1e6feb95 1322 else if ( action == wxACTION_NOTEBOOK_GOTO )
01c6f372 1323 SetSelection((int)numArg);
1e6feb95
VZ
1324 else
1325 return wxControl::PerformAction(action, numArg, strArg);
1326
a290fa5a 1327 return true;
1e6feb95
VZ
1328}
1329
9467bdb7
VZ
1330/* static */
1331wxInputHandler *wxNotebook::GetStdInputHandler(wxInputHandler *handlerDef)
1332{
1333 static wxStdNotebookInputHandler s_handler(handlerDef);
1334
1335 return &s_handler;
1336}
1337
1e6feb95
VZ
1338// ----------------------------------------------------------------------------
1339// wxStdNotebookInputHandler
1340// ----------------------------------------------------------------------------
1341
1342wxStdNotebookInputHandler::wxStdNotebookInputHandler(wxInputHandler *inphand)
1343 : wxStdInputHandler(inphand)
1344{
1345}
1346
23645bfa 1347bool wxStdNotebookInputHandler::HandleKey(wxInputConsumer *consumer,
1e6feb95
VZ
1348 const wxKeyEvent& event,
1349 bool pressed)
1350{
1351 // ignore the key releases
1352 if ( pressed )
1353 {
23645bfa 1354 wxNotebook *notebook = wxStaticCast(consumer->GetInputWindow(), wxNotebook);
1e6feb95
VZ
1355
1356 int page = 0;
1357 wxControlAction action;
1358 switch ( event.GetKeyCode() )
1359 {
1360 case WXK_UP:
1361 if ( notebook->IsVertical() )
1362 action = wxACTION_NOTEBOOK_PREV;
1363 break;
1364
1365 case WXK_LEFT:
1366 if ( !notebook->IsVertical() )
1367 action = wxACTION_NOTEBOOK_PREV;
1368 break;
1369
1370 case WXK_DOWN:
1371 if ( notebook->IsVertical() )
1372 action = wxACTION_NOTEBOOK_NEXT;
1373 break;
1374
1375 case WXK_RIGHT:
1376 if ( !notebook->IsVertical() )
1377 action = wxACTION_NOTEBOOK_NEXT;
1378 break;
1379
1380 case WXK_HOME:
1381 action = wxACTION_NOTEBOOK_GOTO;
1382 // page = 0; -- already has this value
1383 break;
1384
1385 case WXK_END:
1386 action = wxACTION_NOTEBOOK_GOTO;
1387 page = notebook->GetPageCount() - 1;
1388 break;
1389 }
1390
a290fa5a 1391 if ( !action.IsEmpty() )
1e6feb95 1392 {
23645bfa 1393 return consumer->PerformAction(action, page);
1e6feb95
VZ
1394 }
1395 }
1396
23645bfa 1397 return wxStdInputHandler::HandleKey(consumer, event, pressed);
1e6feb95
VZ
1398}
1399
23645bfa 1400bool wxStdNotebookInputHandler::HandleMouse(wxInputConsumer *consumer,
1e6feb95
VZ
1401 const wxMouseEvent& event)
1402{
1403 if ( event.ButtonDown(1) )
1404 {
23645bfa 1405 wxNotebook *notebook = wxStaticCast(consumer->GetInputWindow(), wxNotebook);
1e6feb95
VZ
1406 int page = notebook->HitTest(event.GetPosition());
1407 if ( page != -1 )
1408 {
23645bfa 1409 consumer->PerformAction(wxACTION_NOTEBOOK_GOTO, page);
1e6feb95 1410
a290fa5a 1411 return false;
1e6feb95
VZ
1412 }
1413 }
1414
23645bfa 1415 return wxStdInputHandler::HandleMouse(consumer, event);
1e6feb95
VZ
1416}
1417
23645bfa 1418bool wxStdNotebookInputHandler::HandleMouseMove(wxInputConsumer *consumer,
1e6feb95
VZ
1419 const wxMouseEvent& event)
1420{
23645bfa 1421 return wxStdInputHandler::HandleMouseMove(consumer, event);
1e6feb95
VZ
1422}
1423
61fef19b
VZ
1424bool
1425wxStdNotebookInputHandler::HandleFocus(wxInputConsumer *consumer,
1426 const wxFocusEvent& WXUNUSED(event))
1e6feb95 1427{
23645bfa 1428 HandleFocusChange(consumer);
1e6feb95 1429
a290fa5a 1430 return false;
1e6feb95
VZ
1431}
1432
23645bfa 1433bool wxStdNotebookInputHandler::HandleActivation(wxInputConsumer *consumer,
1e6feb95
VZ
1434 bool WXUNUSED(activated))
1435{
1436 // we react to the focus change in the same way as to the [de]activation
23645bfa 1437 HandleFocusChange(consumer);
1e6feb95 1438
a290fa5a 1439 return false;
1e6feb95
VZ
1440}
1441
23645bfa 1442void wxStdNotebookInputHandler::HandleFocusChange(wxInputConsumer *consumer)
1e6feb95 1443{
23645bfa 1444 wxNotebook *notebook = wxStaticCast(consumer->GetInputWindow(), wxNotebook);
1e6feb95
VZ
1445 notebook->RefreshCurrent();
1446}
1447
1448#endif // wxUSE_NOTEBOOK