1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/aui/notebook.cpp
3 // Purpose: wxaui: wx advanced user interface - notebook
4 // Author: Benjamin I. Williams
7 // Copyright: (C) Copyright 2006, Kirix Corporation, All Rights Reserved
8 // Licence: wxWindows Library Licence, Version 3.1
9 ///////////////////////////////////////////////////////////////////////////////
11 // ----------------------------------------------------------------------------
13 // ----------------------------------------------------------------------------
15 #include "wx/wxprec.h"
23 #include "wx/aui/auibook.h"
24 #include "wx/aui/tabmdi.h"
25 #include "wx/dcbuffer.h"
30 #include "wx/arrimpl.cpp"
31 WX_DEFINE_OBJARRAY(wxAuiNotebookPageArray
)
32 WX_DEFINE_OBJARRAY(wxAuiTabContainerButtonArray
)
34 DEFINE_EVENT_TYPE(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGING
)
35 DEFINE_EVENT_TYPE(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGED
)
36 DEFINE_EVENT_TYPE(wxEVT_COMMAND_AUINOTEBOOK_BUTTON
)
37 DEFINE_EVENT_TYPE(wxEVT_COMMAND_AUINOTEBOOK_BEGIN_DRAG
)
38 DEFINE_EVENT_TYPE(wxEVT_COMMAND_AUINOTEBOOK_END_DRAG
)
39 DEFINE_EVENT_TYPE(wxEVT_COMMAND_AUINOTEBOOK_DRAG_MOTION
)
43 IMPLEMENT_DYNAMIC_CLASS(wxAuiNotebookEvent
, wxEvent
)
45 // -- wxAuiTabContainer class implementation --
48 // wxAuiTabContainer is a class which contains information about each
49 // tab. It also can render an entire tab control to a specified DC.
50 // It's not a window class itself, because this code will be used by
51 // the wxFrameMananger, where it is disadvantageous to have separate
52 // windows for each tab control in the case of "docked tabs"
54 // A derived class, wxAuiTabCtrl, is an actual wxWindow-derived window
55 // which can be used as a tab control in the normal sense.
58 // This functions are here for this proof of concept
59 // and will be factored out later. See dockart.cpp
60 static wxColor
StepColour(const wxColor
& c
, int percent
)
62 int r
= c
.Red(), g
= c
.Green(), b
= c
.Blue();
63 return wxColour((unsigned char)wxMin((r
*percent
)/100,255),
64 (unsigned char)wxMin((g
*percent
)/100,255),
65 (unsigned char)wxMin((b
*percent
)/100,255));
68 // This functions are here for this proof of concept
69 // and will be factored out later. See dockart.cpp
70 static wxBitmap
BitmapFromBits(const unsigned char bits
[], int w
, int h
,
71 const wxColour
& color
)
73 wxImage img
= wxBitmap((const char*)bits
, w
, h
).ConvertToImage();
74 img
.Replace(255,255,255,123,123,123);
75 img
.Replace(0,0,0,color
.Red(),color
.Green(),color
.Blue());
76 img
.SetMaskColour(123,123,123);
80 static void DrawButton(wxDC
& dc
,
83 const wxColour
& bkcolour
,
88 if (button_state
== wxAUI_BUTTON_STATE_PRESSED
)
94 if (button_state
== wxAUI_BUTTON_STATE_HOVER
||
95 button_state
== wxAUI_BUTTON_STATE_PRESSED
)
97 dc
.SetBrush(wxBrush(StepColour(bkcolour
, 120)));
98 dc
.SetPen(wxPen(StepColour(bkcolour
, 70)));
100 // draw the background behind the button
101 dc
.DrawRectangle(rect
.x
, rect
.y
, 15, 15);
104 // draw the button itself
105 dc
.DrawBitmap(bmp
, rect
.x
, rect
.y
, true);
111 wxAuiTabContainer::wxAuiTabContainer()
113 m_normal_font
= *wxNORMAL_FONT
;
114 m_selected_font
= *wxNORMAL_FONT
;
115 m_selected_font
.SetWeight(wxBOLD
);
116 m_measuring_font
= m_selected_font
;
118 wxColour base_colour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
120 wxColour background_colour
= StepColour(base_colour
, 95);
121 wxColour normaltab_colour
= base_colour
;
122 wxColour selectedtab_colour
= *wxWHITE
;
124 m_bkbrush
= wxBrush(background_colour
);
125 m_normal_bkbrush
= wxBrush(normaltab_colour
);
126 m_normal_bkpen
= wxPen(normaltab_colour
);
127 m_selected_bkbrush
= wxBrush(selectedtab_colour
);
128 m_selected_bkpen
= wxPen(selectedtab_colour
);
131 void wxAuiTabContainer::SetNormalFont(const wxFont
& font
)
133 m_normal_font
= font
;
136 void wxAuiTabContainer::SetSelectedFont(const wxFont
& font
)
138 m_selected_font
= font
;
141 void wxAuiTabContainer::SetMeasuringFont(const wxFont
& font
)
143 m_measuring_font
= font
;
146 void wxAuiTabContainer::SetRect(const wxRect
& rect
)
151 bool wxAuiTabContainer::AddPage(wxWindow
* page
,
152 const wxAuiNotebookPage
& info
)
154 wxAuiNotebookPage page_info
;
156 page_info
.window
= page
;
158 m_pages
.Add(page_info
);
163 bool wxAuiTabContainer::InsertPage(wxWindow
* page
,
164 const wxAuiNotebookPage
& info
,
167 wxAuiNotebookPage page_info
;
169 page_info
.window
= page
;
171 if (idx
>= m_pages
.GetCount())
172 m_pages
.Add(page_info
);
174 m_pages
.Insert(page_info
, idx
);
179 bool wxAuiTabContainer::RemovePage(wxWindow
* wnd
)
181 size_t i
, page_count
= m_pages
.GetCount();
182 for (i
= 0; i
< page_count
; ++i
)
184 wxAuiNotebookPage
& page
= m_pages
.Item(i
);
185 if (page
.window
== wnd
)
195 bool wxAuiTabContainer::SetActivePage(wxWindow
* wnd
)
199 size_t i
, page_count
= m_pages
.GetCount();
200 for (i
= 0; i
< page_count
; ++i
)
202 wxAuiNotebookPage
& page
= m_pages
.Item(i
);
203 if (page
.window
== wnd
)
217 void wxAuiTabContainer::SetNoneActive()
219 size_t i
, page_count
= m_pages
.GetCount();
220 for (i
= 0; i
< page_count
; ++i
)
222 wxAuiNotebookPage
& page
= m_pages
.Item(i
);
227 bool wxAuiTabContainer::SetActivePage(size_t page
)
229 if (page
>= m_pages
.GetCount())
232 return SetActivePage(m_pages
.Item(page
).window
);
235 int wxAuiTabContainer::GetActivePage() const
237 size_t i
, page_count
= m_pages
.GetCount();
238 for (i
= 0; i
< page_count
; ++i
)
240 wxAuiNotebookPage
& page
= m_pages
.Item(i
);
248 wxWindow
* wxAuiTabContainer::GetWindowFromIdx(size_t idx
) const
250 if (idx
>= m_pages
.GetCount())
253 return m_pages
[idx
].window
;
256 int wxAuiTabContainer::GetIdxFromWindow(wxWindow
* wnd
) const
258 size_t i
, page_count
= m_pages
.GetCount();
259 for (i
= 0; i
< page_count
; ++i
)
261 wxAuiNotebookPage
& page
= m_pages
.Item(i
);
262 if (page
.window
== wnd
)
268 wxAuiNotebookPage
& wxAuiTabContainer::GetPage(size_t idx
)
270 wxASSERT_MSG(idx
< m_pages
.GetCount(), wxT("Invalid Page index"));
275 wxAuiNotebookPageArray
& wxAuiTabContainer::GetPages()
280 size_t wxAuiTabContainer::GetPageCount() const
282 return m_pages
.GetCount();
285 void wxAuiTabContainer::AddButton(int id
, const wxBitmap
& bmp
)
287 wxAuiTabContainerButton button
;
290 button
.cur_state
= wxAUI_BUTTON_STATE_NORMAL
;
292 m_buttons
.Add(button
);
297 // DrawTab() draws an individual tab.
298 // As it is virtual it may be overridden.
301 // in_rect - rectangle the tab should be confined to
302 // caption - tab's caption
303 // active - whether or not the tab is active
304 // out_rect - actual output rectangle
305 // x_extent - the advance x; where the next tab should start
307 void wxAuiTabContainer::DrawTab(wxDC
* dc
,
308 const wxRect
& in_rect
,
309 const wxString
& caption
,
314 wxCoord normal_textx
, normal_texty
;
315 wxCoord selected_textx
, selected_texty
;
316 wxCoord measured_textx
, measured_texty
;
317 wxCoord textx
, texty
;
321 dc
->SetFont(m_measuring_font
);
322 dc
->GetTextExtent(caption
, &measured_textx
, &measured_texty
);
324 dc
->SetFont(m_selected_font
);
325 dc
->GetTextExtent(caption
, &selected_textx
, &selected_texty
);
327 dc
->SetFont(m_normal_font
);
328 dc
->GetTextExtent(caption
, &normal_textx
, &normal_texty
);
331 wxCoord tab_height
= measured_texty
+ 4;
332 wxCoord tab_width
= measured_textx
+ tab_height
+ 5;
333 wxCoord tab_x
= in_rect
.x
;
334 wxCoord tab_y
= in_rect
.y
+ in_rect
.height
- tab_height
;
337 // select pen, brush and font for the tab to be drawn
341 dc
->SetPen(m_selected_bkpen
);
342 dc
->SetBrush(m_selected_bkbrush
);
343 dc
->SetFont(m_selected_font
);
344 textx
= selected_textx
;
345 texty
= selected_texty
;
349 dc
->SetPen(m_normal_bkpen
);
350 dc
->SetBrush(m_normal_bkbrush
);
351 dc
->SetFont(m_normal_font
);
352 textx
= normal_textx
;
353 texty
= normal_texty
;
361 points
[0].y
= tab_y
+ tab_height
- 1;
362 points
[1].x
= tab_x
+ tab_height
- 3;
363 points
[1].y
= tab_y
+ 2;
364 points
[2].x
= tab_x
+ tab_height
+ 3;
366 points
[3].x
= tab_x
+ tab_width
- 2;
368 points
[4].x
= tab_x
+ tab_width
;
369 points
[4].y
= tab_y
+ 2;
370 points
[5].x
= tab_x
+ tab_width
;
371 points
[5].y
= tab_y
+ tab_height
- 1;
372 points
[6] = points
[0];
375 dc
->DrawPolygon(6, points
);
377 dc
->SetPen(*wxGREY_PEN
);
379 //dc->DrawLines(active ? 6 : 7, points);
380 dc
->DrawLines(7, points
);
384 dc
->DrawText(caption
,
385 tab_x
+ (tab_height
/3) + (tab_width
/2) - (textx
/2),
386 tab_y
+ tab_height
- texty
- 2);
388 *out_rect
= wxRect(tab_x
, tab_y
, tab_width
, tab_height
);
389 *x_extent
= tab_width
- (tab_height
/2) - 1;
393 // Render() renders the tab catalog to the specified DC
394 // It is a virtual function and can be overridden to
395 // provide custom drawing capabilities
396 void wxAuiTabContainer::Render(wxDC
* raw_dc
)
400 bmp
.Create(m_rect
.GetWidth(), m_rect
.GetHeight());
401 dc
.SelectObject(bmp
);
404 dc
.SetBrush(m_bkbrush
);
405 dc
.SetPen(*wxTRANSPARENT_PEN
);
406 dc
.DrawRectangle(-1, -1, m_rect
.GetWidth()+2, m_rect
.GetHeight()+2);
409 dc
.SetPen(*wxGREY_PEN
);
410 dc
.DrawLine(0, m_rect
.GetHeight()-1, m_rect
.GetWidth(), m_rect
.GetHeight()-1);
413 size_t i
, page_count
= m_pages
.GetCount();
417 int active_offset
= 0;
420 wxRect rect
= m_rect
;
423 rect
.height
= m_rect
.height
;
425 for (i
= 0; i
< page_count
; ++i
)
427 wxAuiNotebookPage
& page
= m_pages
.Item(i
);
441 active_offset
= offset
;
447 // draw the active tab again so it stands in the foreground
448 if (active
< m_pages
.GetCount())
450 wxAuiNotebookPage
& page
= m_pages
.Item(active
);
452 rect
.x
= active_offset
;
462 offset
= m_rect
.x
+ m_rect
.width
;
463 size_t button_count
= m_buttons
.GetCount();
464 for (i
= 0; i
< button_count
; ++i
)
466 wxAuiTabContainerButton
& button
= m_buttons
.Item(button_count
- i
- 1);
468 wxRect
button_rect(offset
- button
.bitmap
.GetWidth(), 1,
469 button
.bitmap
.GetWidth(), button
.bitmap
.GetHeight());
471 button
.rect
= button_rect
;
473 DrawButton(dc
, button
.rect
, button
.bitmap
,
474 m_bkbrush
.GetColour(),
477 offset
-= button
.bitmap
.GetWidth();
481 raw_dc
->Blit(m_rect
.x
, m_rect
.y
, m_rect
.GetWidth(), m_rect
.GetHeight(), &dc
, 0, 0);
485 // TabHitTest() tests if a tab was hit, passing the window pointer
486 // back if that condition was fulfilled. The function returns
487 // true if a tab was hit, otherwise false
488 bool wxAuiTabContainer::TabHitTest(int x
, int y
, wxWindow
** hit
) const
490 if (!m_rect
.Inside(x
,y
))
493 size_t i
, page_count
= m_pages
.GetCount();
495 for (i
= 0; i
< page_count
; ++i
)
497 wxAuiNotebookPage
& page
= m_pages
.Item(i
);
498 if (page
.rect
.Inside(x
,y
))
508 // ButtonHitTest() tests if a button was hit. The function returns
509 // true if a button was hit, otherwise false
510 bool wxAuiTabContainer::ButtonHitTest(int x
, int y
,
511 wxAuiTabContainerButton
** hit
) const
513 if (!m_rect
.Inside(x
,y
))
516 size_t i
, button_count
= m_buttons
.GetCount();
518 for (i
= 0; i
< button_count
; ++i
)
520 wxAuiTabContainerButton
& button
= m_buttons
.Item(i
);
521 if (button
.rect
.Inside(x
,y
))
533 // the utility function ShowWnd() is the same as show,
534 // except it handles wxTabMDIChildFrame windows as well,
535 // as the Show() method on this class is "unplugged"
536 static void ShowWnd(wxWindow
* wnd
, bool show
)
538 if (wnd
->IsKindOf(CLASSINFO(wxTabMDIChildFrame
)))
540 wxTabMDIChildFrame
* cf
= (wxTabMDIChildFrame
*)wnd
;
550 // DoShowHide() this function shows the active window, then
551 // hides all of the other windows (in that order)
552 void wxAuiTabContainer::DoShowHide()
554 wxAuiNotebookPageArray
& pages
= GetPages();
555 size_t i
, page_count
= pages
.GetCount();
557 // show new active page first
558 for (i
= 0; i
< page_count
; ++i
)
560 wxAuiNotebookPage
& page
= pages
.Item(i
);
563 ShowWnd(page
.window
, true);
568 // hide all other pages
569 for (i
= 0; i
< page_count
; ++i
)
571 wxAuiNotebookPage
& page
= pages
.Item(i
);
572 ShowWnd(page
.window
, page
.active
);
581 // -- wxAuiTabCtrl class implementation --
584 const int wxAuiButtonClose
= 101;
586 BEGIN_EVENT_TABLE(wxAuiTabCtrl
, wxControl
)
587 EVT_PAINT(wxAuiTabCtrl::OnPaint
)
588 EVT_ERASE_BACKGROUND(wxAuiTabCtrl::OnEraseBackground
)
589 EVT_SIZE(wxAuiTabCtrl::OnSize
)
590 EVT_LEFT_DOWN(wxAuiTabCtrl::OnLeftDown
)
591 EVT_LEFT_UP(wxAuiTabCtrl::OnLeftUp
)
592 EVT_MOTION(wxAuiTabCtrl::OnMotion
)
593 EVT_LEAVE_WINDOW(wxAuiTabCtrl::OnLeaveWindow
)
597 wxAuiTabCtrl::wxAuiTabCtrl(wxWindow
* parent
,
601 long style
) : wxControl(parent
, id
, pos
, size
, style
)
603 m_click_pt
= wxDefaultPosition
;
604 m_is_dragging
= false;
605 m_hover_button
= NULL
;
607 // copied from dockart-- needs to put in a common place
608 static unsigned char close_bits
[]={
609 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xef,0xfb,0xcf,0xf9,
610 0x9f,0xfc,0x3f,0xfe,0x3f,0xfe,0x9f,0xfc,0xcf,0xf9,0xef,0xfb,
611 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
613 AddButton(101, BitmapFromBits(close_bits
, 16, 16, *wxBLACK
));
617 void wxAuiTabCtrl::OnPaint(wxPaintEvent
&)
621 dc
.SetFont(GetFont());
623 if (GetPageCount() > 0)
627 void wxAuiTabCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(evt
))
631 void wxAuiTabCtrl::OnSize(wxSizeEvent
& evt
)
633 wxSize s
= evt
.GetSize();
634 wxRect
r(0, 0, s
.GetWidth(), s
.GetHeight());
638 void wxAuiTabCtrl::OnLeftDown(wxMouseEvent
& evt
)
641 m_click_pt
= wxDefaultPosition
;
642 m_is_dragging
= false;
646 if (TabHitTest(evt
.m_x
, evt
.m_y
, &wnd
))
648 wxAuiNotebookEvent
e(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGING
, m_windowId
);
649 e
.SetSelection(GetIdxFromWindow(wnd
));
650 e
.SetOldSelection(GetActivePage());
651 e
.SetEventObject(this);
652 GetEventHandler()->ProcessEvent(e
);
654 m_click_pt
.x
= evt
.m_x
;
655 m_click_pt
.y
= evt
.m_y
;
656 m_click_tab
= e
.GetSelection();
661 m_hover_button
->cur_state
= wxAUI_BUTTON_STATE_PRESSED
;
667 void wxAuiTabCtrl::OnLeftUp(wxMouseEvent
&)
669 if (GetCapture() == this)
674 wxAuiNotebookEvent
evt(wxEVT_COMMAND_AUINOTEBOOK_END_DRAG
, m_windowId
);
675 evt
.SetSelection(m_click_tab
);
676 evt
.SetOldSelection(m_click_tab
);
677 evt
.SetEventObject(this);
678 GetEventHandler()->ProcessEvent(evt
);
684 m_hover_button
->cur_state
= wxAUI_BUTTON_STATE_HOVER
;
688 wxAuiNotebookEvent
evt(wxEVT_COMMAND_AUINOTEBOOK_BUTTON
, m_windowId
);
689 evt
.SetInt(m_hover_button
->id
);
690 evt
.SetEventObject(this);
691 GetEventHandler()->ProcessEvent(evt
);
694 m_click_pt
= wxDefaultPosition
;
695 m_is_dragging
= false;
699 void wxAuiTabCtrl::OnMotion(wxMouseEvent
& evt
)
701 wxPoint pos
= evt
.GetPosition();
703 // check if the mouse is hovering above a button
704 wxAuiTabContainerButton
* button
;
705 if (ButtonHitTest(pos
.x
, pos
.y
, &button
))
707 if (button
->cur_state
!= wxAUI_BUTTON_STATE_HOVER
)
709 button
->cur_state
= wxAUI_BUTTON_STATE_HOVER
;
712 m_hover_button
= button
;
720 m_hover_button
->cur_state
= wxAUI_BUTTON_STATE_NORMAL
;
721 m_hover_button
= NULL
;
728 if (!evt
.LeftIsDown() || m_click_pt
== wxDefaultPosition
)
733 wxAuiNotebookEvent
evt(wxEVT_COMMAND_AUINOTEBOOK_DRAG_MOTION
, m_windowId
);
734 evt
.SetSelection(m_click_tab
);
735 evt
.SetOldSelection(m_click_tab
);
736 evt
.SetEventObject(this);
737 GetEventHandler()->ProcessEvent(evt
);
742 int drag_x_threshold
= wxSystemSettings::GetMetric(wxSYS_DRAG_X
);
743 int drag_y_threshold
= wxSystemSettings::GetMetric(wxSYS_DRAG_Y
);
745 if (abs(pos
.x
- m_click_pt
.x
) > drag_x_threshold
||
746 abs(pos
.y
- m_click_pt
.y
) > drag_y_threshold
)
748 wxAuiNotebookEvent
evt(wxEVT_COMMAND_AUINOTEBOOK_BEGIN_DRAG
, m_windowId
);
749 evt
.SetSelection(m_click_tab
);
750 evt
.SetOldSelection(m_click_tab
);
751 evt
.SetEventObject(this);
752 GetEventHandler()->ProcessEvent(evt
);
754 m_is_dragging
= true;
758 void wxAuiTabCtrl::OnLeaveWindow(wxMouseEvent
& WXUNUSED(event
))
762 m_hover_button
->cur_state
= wxAUI_BUTTON_STATE_NORMAL
;
763 m_hover_button
= NULL
;
770 // wxTabFrame is an interesting case. It's important that all child pages
771 // of the multi-notebook control are all actually children of that control
772 // (and not grandchildren). wxTabFrame facilitates this. There is one
773 // instance of wxTabFrame for each tab control inside the multi-notebook.
774 // It's important to know that wxTabFrame is not a real window, but it merely
775 // used to capture the dimensions/positioning of the internal tab control and
776 // it's managed page windows
778 class wxTabFrame
: public wxWindow
785 m_rect
= wxRect(0,0,200,200);
788 void DoSetSize(int x
, int y
,
789 int width
, int height
,
790 int WXUNUSED(sizeFlags
= wxSIZE_AUTO
))
792 m_rect
= wxRect(x
, y
, width
, height
);
796 void DoGetClientSize(int* x
, int* y
) const
807 int tab_height
= wxMin(m_rect
.height
, 19);
808 m_tab_rect
= wxRect(m_rect
.x
, m_rect
.y
, m_rect
.width
, tab_height
);
809 m_tabs
->SetSize(m_rect
.x
, m_rect
.y
, m_rect
.width
, tab_height
);
810 m_tabs
->SetRect(wxRect(0, 0, m_rect
.width
, tab_height
));
813 wxAuiNotebookPageArray
& pages
= m_tabs
->GetPages();
814 size_t i
, page_count
= pages
.GetCount();
816 for (i
= 0; i
< page_count
; ++i
)
818 wxAuiNotebookPage
& page
= pages
.Item(i
);
819 page
.window
->SetSize(m_rect
.x
, m_rect
.y
+tab_height
, m_rect
.width
, m_rect
.height
-tab_height
);
821 if (page
.window
->IsKindOf(CLASSINFO(wxTabMDIChildFrame
)))
823 wxTabMDIChildFrame
* wnd
= (wxTabMDIChildFrame
*)page
.window
;
824 wnd
->ApplyMDIChildFrameRect();
833 wxAuiTabCtrl
* m_tabs
;
840 // -- wxAuiMultiNotebook class implementation --
842 BEGIN_EVENT_TABLE(wxAuiMultiNotebook
, wxControl
)
843 //EVT_ERASE_BACKGROUND(wxAuiMultiNotebook::OnEraseBackground)
844 //EVT_SIZE(wxAuiMultiNotebook::OnSize)
845 //EVT_LEFT_DOWN(wxAuiMultiNotebook::OnLeftDown)
846 EVT_CHILD_FOCUS(wxAuiMultiNotebook::OnChildFocus
)
847 EVT_COMMAND_RANGE(10000, 10100,
848 wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGING
,
849 wxAuiMultiNotebook::OnTabClicked
)
850 EVT_COMMAND_RANGE(10000, 10100,
851 wxEVT_COMMAND_AUINOTEBOOK_BEGIN_DRAG
,
852 wxAuiMultiNotebook::OnTabBeginDrag
)
853 EVT_COMMAND_RANGE(10000, 10100,
854 wxEVT_COMMAND_AUINOTEBOOK_END_DRAG
,
855 wxAuiMultiNotebook::OnTabEndDrag
)
856 EVT_COMMAND_RANGE(10000, 10100,
857 wxEVT_COMMAND_AUINOTEBOOK_DRAG_MOTION
,
858 wxAuiMultiNotebook::OnTabDragMotion
)
859 EVT_COMMAND_RANGE(10000, 10100,
860 wxEVT_COMMAND_AUINOTEBOOK_BUTTON
,
861 wxAuiMultiNotebook::OnTabButton
)
864 wxAuiMultiNotebook::wxAuiMultiNotebook()
867 m_tab_id_counter
= 10000;
871 wxAuiMultiNotebook::wxAuiMultiNotebook(wxWindow
*parent
,
875 long style
) : wxControl(parent
, id
, pos
, size
, style
)
880 bool wxAuiMultiNotebook::Create(wxWindow
* parent
,
886 if (!wxControl::Create(parent
, id
, pos
, size
, style
))
894 // InitNotebook() contains common initialization
895 // code called by all constructors
896 void wxAuiMultiNotebook::InitNotebook()
898 m_normal_font
= *wxNORMAL_FONT
;
899 m_selected_font
= *wxNORMAL_FONT
;
900 m_selected_font
.SetWeight(wxBOLD
);
903 m_tab_id_counter
= 10000;
905 m_dummy_wnd
= new wxWindow(this, -1, wxPoint(0,0), wxSize(0,0));
906 m_dummy_wnd
->SetSize(200, 200);
907 m_dummy_wnd
->Show(false);
909 m_mgr
.SetManagedWindow(this);
911 m_mgr
.AddPane(m_dummy_wnd
,
912 wxPaneInfo().Name(wxT("dummy")).Bottom().Show(false));
917 wxAuiMultiNotebook::~wxAuiMultiNotebook()
922 bool wxAuiMultiNotebook::AddPage(wxWindow
* page
,
923 const wxString
& caption
,
925 const wxBitmap
& bitmap
)
927 return InsertPage(GetPageCount(), page
, caption
, select
, bitmap
);
930 bool wxAuiMultiNotebook::InsertPage(size_t page_idx
,
932 const wxString
& caption
,
934 const wxBitmap
& bitmap
)
936 wxAuiNotebookPage info
;
938 info
.caption
= caption
;
939 info
.bitmap
= bitmap
;
942 // if there are currently no tabs, the first added
943 // tab must be active
944 if (m_tabs
.GetPageCount() == 0)
947 m_tabs
.InsertPage(page
, info
, page_idx
);
949 wxAuiTabCtrl
* active_tabctrl
= GetActiveTabCtrl();
950 if (page_idx
>= active_tabctrl
->GetPageCount())
951 active_tabctrl
->AddPage(page
, info
);
953 active_tabctrl
->InsertPage(page
, info
, page_idx
);
956 active_tabctrl
->DoShowHide();
960 int idx
= m_tabs
.GetIdxFromWindow(page
);
961 wxASSERT_MSG(idx
!= -1, wxT("Invalid Page index returned on wxAuiMultiNotebook::InsertPage()"));
970 // DeletePage() removes a tab from the multi-notebook,
971 // and destroys the window as well
972 bool wxAuiMultiNotebook::DeletePage(size_t page_idx
)
974 wxWindow
* wnd
= m_tabs
.GetWindowFromIdx(page_idx
);
977 // find out which onscreen tab ctrl owns this tab
980 if (!FindTab(wnd
, &ctrl
, &ctrl_idx
))
983 // find a new page and set it as active
984 int new_idx
= ctrl_idx
+1;
985 if (new_idx
>= (int)ctrl
->GetPageCount())
986 new_idx
= ctrl_idx
-1;
988 if (new_idx
>= 0 && new_idx
< (int)ctrl
->GetPageCount())
990 wxWindow
* new_wnd
= ctrl
->GetWindowFromIdx(new_idx
);
991 int main_idx
= m_tabs
.GetIdxFromWindow(new_wnd
);
992 wxASSERT(main_idx
!= -1);
993 SetSelection(main_idx
);
997 // set the active page to the first page that
998 // isn't the one being deleted
1000 size_t i
, page_count
= m_tabs
.GetPageCount();
1001 for (i
= 0; i
< page_count
; ++i
)
1003 wxWindow
* w
= m_tabs
.GetWindowFromIdx(i
);
1017 // remove the tab from main catalog
1018 if (!m_tabs
.RemovePage(wnd
))
1021 // remove the tab from the onscreen tab ctrl
1022 ctrl
->RemovePage(wnd
);
1024 // actually destroy the window now
1025 if (wnd
->IsKindOf(CLASSINFO(wxTabMDIChildFrame
)))
1027 // delete the child frame with pending delete, as is
1028 // customary with frame windows
1029 if (!wxPendingDelete
.Member(wnd
))
1030 wxPendingDelete
.Append(wnd
);
1037 RemoveEmptyTabFrames();
1044 // RemovePage() removes a tab from the multi-notebook,
1045 // but does not destroy the window
1046 bool wxAuiMultiNotebook::RemovePage(size_t page_idx
)
1048 // remove the tab from our own catalog
1049 wxWindow
* wnd
= m_tabs
.GetWindowFromIdx(page_idx
);
1050 if (!m_tabs
.RemovePage(wnd
))
1053 // remove the tab from the onscreen tab ctrl
1056 if (FindTab(wnd
, &ctrl
, &ctrl_idx
))
1058 ctrl
->RemovePage(wnd
);
1065 // SetPageText() changes the tab caption of the specified page
1066 bool wxAuiMultiNotebook::SetPageText(size_t page_idx
, const wxString
& text
)
1068 if (page_idx
>= m_tabs
.GetPageCount())
1071 // update our own tab catalog
1072 wxAuiNotebookPage
& page_info
= m_tabs
.GetPage(page_idx
);
1073 page_info
.caption
= text
;
1075 // update what's on screen
1078 if (FindTab(page_info
.window
, &ctrl
, &ctrl_idx
))
1080 wxAuiNotebookPage
& info
= ctrl
->GetPage(ctrl_idx
);
1081 info
.caption
= text
;
1089 // GetSelection() returns the index of the currently active page
1090 int wxAuiMultiNotebook::GetSelection() const
1095 // SetSelection() sets the currently active page
1096 size_t wxAuiMultiNotebook::SetSelection(size_t new_page
)
1098 wxWindow
* wnd
= m_tabs
.GetWindowFromIdx(new_page
);
1102 wxAuiNotebookEvent
evt(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGING
, m_windowId
);
1103 evt
.SetSelection(new_page
);
1104 evt
.SetOldSelection(m_curpage
);
1105 evt
.SetEventObject(this);
1106 if (!GetEventHandler()->ProcessEvent(evt
) || evt
.IsAllowed())
1108 // program allows the page change
1109 evt
.SetEventType(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGED
);
1110 (void)GetEventHandler()->ProcessEvent(evt
);
1116 if (FindTab(wnd
, &ctrl
, &ctrl_idx
))
1118 m_tabs
.SetActivePage(wnd
);
1120 ctrl
->SetActivePage(ctrl_idx
);
1124 int old_curpage
= m_curpage
;
1125 m_curpage
= new_page
;
1129 wxPaneInfoArray
& all_panes
= m_mgr
.GetAllPanes();
1130 size_t i
, pane_count
= all_panes
.GetCount();
1131 for (i
= 0; i
< pane_count
; ++i
)
1133 wxPaneInfo
& pane
= all_panes
.Item(i
);
1134 if (pane
.name
== wxT("dummy"))
1136 wxAuiTabCtrl
* tabctrl
= ((wxTabFrame
*)pane
.window
)->m_tabs
;
1137 if (tabctrl
!= ctrl
)
1138 tabctrl
->SetSelectedFont(m_normal_font
);
1140 tabctrl
->SetSelectedFont(m_selected_font
);
1153 // GetPageCount() returns the total number of
1154 // pages managed by the multi-notebook
1155 size_t wxAuiMultiNotebook::GetPageCount() const
1157 return m_tabs
.GetPageCount();
1160 // GetPage() returns the wxWindow pointer of the
1162 wxWindow
* wxAuiMultiNotebook::GetPage(size_t page_idx
) const
1164 wxASSERT(page_idx
< m_tabs
.GetPageCount());
1166 return m_tabs
.GetWindowFromIdx(page_idx
);
1169 // DoSizing() performs all sizing operations in each tab control
1170 void wxAuiMultiNotebook::DoSizing()
1172 wxPaneInfoArray
& all_panes
= m_mgr
.GetAllPanes();
1173 size_t i
, pane_count
= all_panes
.GetCount();
1174 for (i
= 0; i
< pane_count
; ++i
)
1176 if (all_panes
.Item(i
).name
== wxT("dummy"))
1179 wxTabFrame
* tabframe
= (wxTabFrame
*)all_panes
.Item(i
).window
;
1180 tabframe
->DoSizing();
1184 // GetActiveTabCtrl() returns the active tab control. It is
1185 // called to determine which control gets new windows being added
1186 wxAuiTabCtrl
* wxAuiMultiNotebook::GetActiveTabCtrl()
1188 if (m_curpage
>= 0 && m_curpage
< (int)m_tabs
.GetPageCount())
1193 // find the tab ctrl with the current page
1194 if (FindTab(m_tabs
.GetPage(m_curpage
).window
,
1201 // no current page, just find the first tab ctrl
1202 wxPaneInfoArray
& all_panes
= m_mgr
.GetAllPanes();
1203 size_t i
, pane_count
= all_panes
.GetCount();
1204 for (i
= 0; i
< pane_count
; ++i
)
1206 if (all_panes
.Item(i
).name
== wxT("dummy"))
1209 wxTabFrame
* tabframe
= (wxTabFrame
*)all_panes
.Item(i
).window
;
1210 return tabframe
->m_tabs
;
1213 // If there is no tabframe at all, create one
1214 wxTabFrame
* tabframe
= new wxTabFrame
;
1215 tabframe
->m_tabs
= new wxAuiTabCtrl(this,
1220 m_mgr
.AddPane(tabframe
,
1221 wxPaneInfo().Center().CaptionVisible(false));
1225 return tabframe
->m_tabs
;
1228 // FindTab() finds the tab control that currently contains the window as well
1229 // as the index of the window in the tab control. It returns true if the
1230 // window was found, otherwise false.
1231 bool wxAuiMultiNotebook::FindTab(wxWindow
* page
, wxAuiTabCtrl
** ctrl
, int* idx
)
1233 wxPaneInfoArray
& all_panes
= m_mgr
.GetAllPanes();
1234 size_t i
, pane_count
= all_panes
.GetCount();
1235 for (i
= 0; i
< pane_count
; ++i
)
1237 if (all_panes
.Item(i
).name
== wxT("dummy"))
1240 wxTabFrame
* tabframe
= (wxTabFrame
*)all_panes
.Item(i
).window
;
1242 int page_idx
= tabframe
->m_tabs
->GetIdxFromWindow(page
);
1245 *ctrl
= tabframe
->m_tabs
;
1255 void wxAuiMultiNotebook::OnEraseBackground(wxEraseEvent
&)
1259 void wxAuiMultiNotebook::OnSize(wxSizeEvent
&)
1263 void wxAuiMultiNotebook::OnTabClicked(wxCommandEvent
& command_evt
)
1265 wxAuiNotebookEvent
& evt
= (wxAuiNotebookEvent
&)command_evt
;
1267 wxAuiTabCtrl
* ctrl
= (wxAuiTabCtrl
*)evt
.GetEventObject();
1268 wxASSERT(ctrl
!= NULL
);
1270 wxWindow
* wnd
= ctrl
->GetWindowFromIdx(evt
.GetSelection());
1271 wxASSERT(wnd
!= NULL
);
1273 int idx
= m_tabs
.GetIdxFromWindow(wnd
);
1274 wxASSERT(idx
!= -1);
1279 void wxAuiMultiNotebook::OnTabBeginDrag(wxCommandEvent
&)
1283 void wxAuiMultiNotebook::OnTabDragMotion(wxCommandEvent
& evt
)
1285 wxPoint screen_pt
= ::wxGetMousePosition();
1286 wxPoint client_pt
= ScreenToClient(screen_pt
);
1289 wxAuiTabCtrl
* src_tabs
= (wxAuiTabCtrl
*)evt
.GetEventObject();
1291 wxAuiTabCtrl
* tab_ctrl
= GetTabCtrlFromPoint(client_pt
);
1292 if (tab_ctrl
== src_tabs
)
1294 // inner-tabctrl dragging is not yet implemented
1301 wxRect hint_rect
= tab_ctrl
->GetRect();
1302 ClientToScreen(&hint_rect
.x
, &hint_rect
.y
);
1303 m_mgr
.ShowHint(hint_rect
);
1307 m_mgr
.DrawHintRect(m_dummy_wnd
, client_pt
, zero
);
1313 void wxAuiMultiNotebook::OnTabEndDrag(wxCommandEvent
& command_evt
)
1315 wxAuiNotebookEvent
& evt
= (wxAuiNotebookEvent
&)command_evt
;
1320 // get the mouse position, which will be used to determine the drop point
1321 wxPoint mouse_screen_pt
= ::wxGetMousePosition();
1322 wxPoint mouse_client_pt
= ScreenToClient(mouse_screen_pt
);
1325 // the src tab control is the control that fired this event
1326 wxAuiTabCtrl
* src_tabs
= (wxAuiTabCtrl
*)evt
.GetEventObject();
1327 wxAuiTabCtrl
* dest_tabs
= NULL
;
1330 // If the pointer is in an existing tab frame, do a tab insert
1331 wxWindow
* hit_wnd
= ::wxFindWindowAtPoint(mouse_screen_pt
);
1332 wxTabFrame
* tab_frame
= (wxTabFrame
*)GetTabFrameFromTabCtrl(hit_wnd
);
1335 dest_tabs
= tab_frame
->m_tabs
;
1337 if (dest_tabs
== src_tabs
)
1342 // If there is no tabframe at all, create one
1343 wxTabFrame
* new_tabs
= new wxTabFrame
;
1344 new_tabs
->m_tabs
= new wxAuiTabCtrl(this,
1349 m_mgr
.AddPane(new_tabs
,
1350 wxPaneInfo().Bottom().CaptionVisible(false),
1353 dest_tabs
= new_tabs
->m_tabs
;
1358 // remove the page from the source tabs
1359 wxAuiNotebookPage page_info
= src_tabs
->GetPage(evt
.GetSelection());
1360 page_info
.active
= false;
1361 src_tabs
->RemovePage(page_info
.window
);
1362 if (src_tabs
->GetPageCount() > 0)
1364 src_tabs
->SetActivePage((size_t)0);
1365 src_tabs
->DoShowHide();
1366 src_tabs
->Refresh();
1371 // add the page to the destination tabs
1372 dest_tabs
->AddPage(page_info
.window
, page_info
);
1374 if (src_tabs
->GetPageCount() == 0)
1376 RemoveEmptyTabFrames();
1380 dest_tabs
->DoShowHide();
1381 dest_tabs
->Refresh();
1383 SetSelection(m_tabs
.GetIdxFromWindow(page_info
.window
));
1386 wxAuiTabCtrl
* wxAuiMultiNotebook::GetTabCtrlFromPoint(const wxPoint
& pt
)
1388 // if we've just removed the last tab from the source
1389 // tab set, the remove the tab control completely
1390 wxPaneInfoArray
& all_panes
= m_mgr
.GetAllPanes();
1391 size_t i
, pane_count
= all_panes
.GetCount();
1392 for (i
= 0; i
< pane_count
; ++i
)
1394 if (all_panes
.Item(i
).name
== wxT("dummy"))
1397 wxTabFrame
* tabframe
= (wxTabFrame
*)all_panes
.Item(i
).window
;
1398 if (tabframe
->m_tab_rect
.Inside(pt
))
1399 return tabframe
->m_tabs
;
1405 wxWindow
* wxAuiMultiNotebook::GetTabFrameFromTabCtrl(wxWindow
* tab_ctrl
)
1407 // if we've just removed the last tab from the source
1408 // tab set, the remove the tab control completely
1409 wxPaneInfoArray
& all_panes
= m_mgr
.GetAllPanes();
1410 size_t i
, pane_count
= all_panes
.GetCount();
1411 for (i
= 0; i
< pane_count
; ++i
)
1413 if (all_panes
.Item(i
).name
== wxT("dummy"))
1416 wxTabFrame
* tabframe
= (wxTabFrame
*)all_panes
.Item(i
).window
;
1417 if (tabframe
->m_tabs
== tab_ctrl
)
1426 void wxAuiMultiNotebook::RemoveEmptyTabFrames()
1428 bool must_update
= false;
1430 // if we've just removed the last tab from the source
1431 // tab set, the remove the tab control completely
1432 wxPaneInfoArray all_panes
= m_mgr
.GetAllPanes();
1433 size_t i
, pane_count
= all_panes
.GetCount();
1434 for (i
= 0; i
< pane_count
; ++i
)
1436 if (all_panes
.Item(i
).name
== wxT("dummy"))
1439 wxTabFrame
* tab_frame
= (wxTabFrame
*)all_panes
.Item(i
).window
;
1440 if (tab_frame
->m_tabs
->GetPageCount() == 0)
1442 m_mgr
.DetachPane(tab_frame
);
1444 // use pending delete because sometimes during
1445 // window closing, refreshs are pending
1446 if (!wxPendingDelete
.Member(tab_frame
->m_tabs
))
1447 wxPendingDelete
.Append(tab_frame
->m_tabs
);
1448 //tab_frame->m_tabs->Destroy();
1456 // check to see if there is still a center pane;
1457 // if there isn't, make a frame the center pane
1458 wxPaneInfoArray panes
= m_mgr
.GetAllPanes();
1459 pane_count
= panes
.GetCount();
1460 wxWindow
* first_good
= NULL
;
1461 bool center_found
= false;
1462 for (i
= 0; i
< pane_count
; ++i
)
1464 if (panes
.Item(i
).name
== wxT("dummy"))
1466 if (panes
.Item(i
).dock_direction
== wxAUI_DOCK_CENTRE
)
1467 center_found
= true;
1469 first_good
= panes
.Item(i
).window
;
1472 if (!center_found
&& first_good
)
1474 m_mgr
.GetPane(first_good
).Centre();
1481 void wxAuiMultiNotebook::OnChildFocus(wxChildFocusEvent
& evt
)
1483 int idx
= m_tabs
.GetIdxFromWindow(evt
.GetWindow());
1484 if (idx
!= -1 && idx
!= m_curpage
)
1491 void wxAuiMultiNotebook::OnTabButton(wxCommandEvent
& command_evt
)
1493 wxAuiNotebookEvent
& evt
= (wxAuiNotebookEvent
&)command_evt
;
1494 wxAuiTabCtrl
* tabs
= (wxAuiTabCtrl
*)evt
.GetEventObject();
1496 int button_id
= evt
.GetInt();
1498 if (button_id
== wxAuiButtonClose
)
1500 int selection
= tabs
->GetActivePage();
1502 if (selection
!= -1)
1504 wxWindow
* close_wnd
= tabs
->GetWindowFromIdx(selection
);
1506 if (close_wnd
->IsKindOf(CLASSINFO(wxTabMDIChildFrame
)))
1512 int main_idx
= m_tabs
.GetIdxFromWindow(close_wnd
);
1513 DeletePage(main_idx
);