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();
829 void DoGetSize(int* x
, int* y
) const
832 *x
= m_rect
.GetWidth();
834 *y
= m_rect
.GetHeight();
846 wxAuiTabCtrl
* m_tabs
;
853 // -- wxAuiMultiNotebook class implementation --
855 BEGIN_EVENT_TABLE(wxAuiMultiNotebook
, wxControl
)
856 //EVT_ERASE_BACKGROUND(wxAuiMultiNotebook::OnEraseBackground)
857 //EVT_SIZE(wxAuiMultiNotebook::OnSize)
858 //EVT_LEFT_DOWN(wxAuiMultiNotebook::OnLeftDown)
859 EVT_CHILD_FOCUS(wxAuiMultiNotebook::OnChildFocus
)
860 EVT_COMMAND_RANGE(10000, 10100,
861 wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGING
,
862 wxAuiMultiNotebook::OnTabClicked
)
863 EVT_COMMAND_RANGE(10000, 10100,
864 wxEVT_COMMAND_AUINOTEBOOK_BEGIN_DRAG
,
865 wxAuiMultiNotebook::OnTabBeginDrag
)
866 EVT_COMMAND_RANGE(10000, 10100,
867 wxEVT_COMMAND_AUINOTEBOOK_END_DRAG
,
868 wxAuiMultiNotebook::OnTabEndDrag
)
869 EVT_COMMAND_RANGE(10000, 10100,
870 wxEVT_COMMAND_AUINOTEBOOK_DRAG_MOTION
,
871 wxAuiMultiNotebook::OnTabDragMotion
)
872 EVT_COMMAND_RANGE(10000, 10100,
873 wxEVT_COMMAND_AUINOTEBOOK_BUTTON
,
874 wxAuiMultiNotebook::OnTabButton
)
877 wxAuiMultiNotebook::wxAuiMultiNotebook()
880 m_tab_id_counter
= 10000;
884 wxAuiMultiNotebook::wxAuiMultiNotebook(wxWindow
*parent
,
888 long style
) : wxControl(parent
, id
, pos
, size
, style
)
893 bool wxAuiMultiNotebook::Create(wxWindow
* parent
,
899 if (!wxControl::Create(parent
, id
, pos
, size
, style
))
907 // InitNotebook() contains common initialization
908 // code called by all constructors
909 void wxAuiMultiNotebook::InitNotebook()
911 m_normal_font
= *wxNORMAL_FONT
;
912 m_selected_font
= *wxNORMAL_FONT
;
913 m_selected_font
.SetWeight(wxBOLD
);
916 m_tab_id_counter
= 10000;
918 m_dummy_wnd
= new wxWindow(this, -1, wxPoint(0,0), wxSize(0,0));
919 m_dummy_wnd
->SetSize(200, 200);
920 m_dummy_wnd
->Show(false);
922 m_mgr
.SetManagedWindow(this);
924 m_mgr
.AddPane(m_dummy_wnd
,
925 wxPaneInfo().Name(wxT("dummy")).Bottom().Show(false));
930 wxAuiMultiNotebook::~wxAuiMultiNotebook()
935 bool wxAuiMultiNotebook::AddPage(wxWindow
* page
,
936 const wxString
& caption
,
938 const wxBitmap
& bitmap
)
940 return InsertPage(GetPageCount(), page
, caption
, select
, bitmap
);
943 bool wxAuiMultiNotebook::InsertPage(size_t page_idx
,
945 const wxString
& caption
,
947 const wxBitmap
& bitmap
)
949 wxAuiNotebookPage info
;
951 info
.caption
= caption
;
952 info
.bitmap
= bitmap
;
955 // if there are currently no tabs, the first added
956 // tab must be active
957 if (m_tabs
.GetPageCount() == 0)
960 m_tabs
.InsertPage(page
, info
, page_idx
);
962 wxAuiTabCtrl
* active_tabctrl
= GetActiveTabCtrl();
963 if (page_idx
>= active_tabctrl
->GetPageCount())
964 active_tabctrl
->AddPage(page
, info
);
966 active_tabctrl
->InsertPage(page
, info
, page_idx
);
969 active_tabctrl
->DoShowHide();
973 int idx
= m_tabs
.GetIdxFromWindow(page
);
974 wxASSERT_MSG(idx
!= -1, wxT("Invalid Page index returned on wxAuiMultiNotebook::InsertPage()"));
983 // DeletePage() removes a tab from the multi-notebook,
984 // and destroys the window as well
985 bool wxAuiMultiNotebook::DeletePage(size_t page_idx
)
987 wxWindow
* wnd
= m_tabs
.GetWindowFromIdx(page_idx
);
990 // find out which onscreen tab ctrl owns this tab
993 if (!FindTab(wnd
, &ctrl
, &ctrl_idx
))
996 // find a new page and set it as active
997 int new_idx
= ctrl_idx
+1;
998 if (new_idx
>= (int)ctrl
->GetPageCount())
999 new_idx
= ctrl_idx
-1;
1001 if (new_idx
>= 0 && new_idx
< (int)ctrl
->GetPageCount())
1003 wxWindow
* new_wnd
= ctrl
->GetWindowFromIdx(new_idx
);
1004 int main_idx
= m_tabs
.GetIdxFromWindow(new_wnd
);
1005 wxASSERT(main_idx
!= -1);
1006 SetSelection(main_idx
);
1010 // set the active page to the first page that
1011 // isn't the one being deleted
1013 size_t i
, page_count
= m_tabs
.GetPageCount();
1014 for (i
= 0; i
< page_count
; ++i
)
1016 wxWindow
* w
= m_tabs
.GetWindowFromIdx(i
);
1030 // remove the tab from main catalog
1031 if (!m_tabs
.RemovePage(wnd
))
1034 // remove the tab from the onscreen tab ctrl
1035 ctrl
->RemovePage(wnd
);
1037 // actually destroy the window now
1038 if (wnd
->IsKindOf(CLASSINFO(wxTabMDIChildFrame
)))
1040 // delete the child frame with pending delete, as is
1041 // customary with frame windows
1042 if (!wxPendingDelete
.Member(wnd
))
1043 wxPendingDelete
.Append(wnd
);
1050 RemoveEmptyTabFrames();
1057 // RemovePage() removes a tab from the multi-notebook,
1058 // but does not destroy the window
1059 bool wxAuiMultiNotebook::RemovePage(size_t page_idx
)
1061 // remove the tab from our own catalog
1062 wxWindow
* wnd
= m_tabs
.GetWindowFromIdx(page_idx
);
1063 if (!m_tabs
.RemovePage(wnd
))
1066 // remove the tab from the onscreen tab ctrl
1069 if (FindTab(wnd
, &ctrl
, &ctrl_idx
))
1071 ctrl
->RemovePage(wnd
);
1078 // SetPageText() changes the tab caption of the specified page
1079 bool wxAuiMultiNotebook::SetPageText(size_t page_idx
, const wxString
& text
)
1081 if (page_idx
>= m_tabs
.GetPageCount())
1084 // update our own tab catalog
1085 wxAuiNotebookPage
& page_info
= m_tabs
.GetPage(page_idx
);
1086 page_info
.caption
= text
;
1088 // update what's on screen
1091 if (FindTab(page_info
.window
, &ctrl
, &ctrl_idx
))
1093 wxAuiNotebookPage
& info
= ctrl
->GetPage(ctrl_idx
);
1094 info
.caption
= text
;
1102 // GetSelection() returns the index of the currently active page
1103 int wxAuiMultiNotebook::GetSelection() const
1108 // SetSelection() sets the currently active page
1109 size_t wxAuiMultiNotebook::SetSelection(size_t new_page
)
1111 wxWindow
* wnd
= m_tabs
.GetWindowFromIdx(new_page
);
1115 wxAuiNotebookEvent
evt(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGING
, m_windowId
);
1116 evt
.SetSelection(new_page
);
1117 evt
.SetOldSelection(m_curpage
);
1118 evt
.SetEventObject(this);
1119 if (!GetEventHandler()->ProcessEvent(evt
) || evt
.IsAllowed())
1121 // program allows the page change
1122 evt
.SetEventType(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGED
);
1123 (void)GetEventHandler()->ProcessEvent(evt
);
1129 if (FindTab(wnd
, &ctrl
, &ctrl_idx
))
1131 m_tabs
.SetActivePage(wnd
);
1133 ctrl
->SetActivePage(ctrl_idx
);
1137 int old_curpage
= m_curpage
;
1138 m_curpage
= new_page
;
1142 wxPaneInfoArray
& all_panes
= m_mgr
.GetAllPanes();
1143 size_t i
, pane_count
= all_panes
.GetCount();
1144 for (i
= 0; i
< pane_count
; ++i
)
1146 wxPaneInfo
& pane
= all_panes
.Item(i
);
1147 if (pane
.name
== wxT("dummy"))
1149 wxAuiTabCtrl
* tabctrl
= ((wxTabFrame
*)pane
.window
)->m_tabs
;
1150 if (tabctrl
!= ctrl
)
1151 tabctrl
->SetSelectedFont(m_normal_font
);
1153 tabctrl
->SetSelectedFont(m_selected_font
);
1166 // GetPageCount() returns the total number of
1167 // pages managed by the multi-notebook
1168 size_t wxAuiMultiNotebook::GetPageCount() const
1170 return m_tabs
.GetPageCount();
1173 // GetPage() returns the wxWindow pointer of the
1175 wxWindow
* wxAuiMultiNotebook::GetPage(size_t page_idx
) const
1177 wxASSERT(page_idx
< m_tabs
.GetPageCount());
1179 return m_tabs
.GetWindowFromIdx(page_idx
);
1182 // DoSizing() performs all sizing operations in each tab control
1183 void wxAuiMultiNotebook::DoSizing()
1185 wxPaneInfoArray
& all_panes
= m_mgr
.GetAllPanes();
1186 size_t i
, pane_count
= all_panes
.GetCount();
1187 for (i
= 0; i
< pane_count
; ++i
)
1189 if (all_panes
.Item(i
).name
== wxT("dummy"))
1192 wxTabFrame
* tabframe
= (wxTabFrame
*)all_panes
.Item(i
).window
;
1193 tabframe
->DoSizing();
1197 // GetActiveTabCtrl() returns the active tab control. It is
1198 // called to determine which control gets new windows being added
1199 wxAuiTabCtrl
* wxAuiMultiNotebook::GetActiveTabCtrl()
1201 if (m_curpage
>= 0 && m_curpage
< (int)m_tabs
.GetPageCount())
1206 // find the tab ctrl with the current page
1207 if (FindTab(m_tabs
.GetPage(m_curpage
).window
,
1214 // no current page, just find the first tab ctrl
1215 wxPaneInfoArray
& all_panes
= m_mgr
.GetAllPanes();
1216 size_t i
, pane_count
= all_panes
.GetCount();
1217 for (i
= 0; i
< pane_count
; ++i
)
1219 if (all_panes
.Item(i
).name
== wxT("dummy"))
1222 wxTabFrame
* tabframe
= (wxTabFrame
*)all_panes
.Item(i
).window
;
1223 return tabframe
->m_tabs
;
1226 // If there is no tabframe at all, create one
1227 wxTabFrame
* tabframe
= new wxTabFrame
;
1228 tabframe
->m_tabs
= new wxAuiTabCtrl(this,
1233 m_mgr
.AddPane(tabframe
,
1234 wxPaneInfo().Center().CaptionVisible(false));
1238 return tabframe
->m_tabs
;
1241 // FindTab() finds the tab control that currently contains the window as well
1242 // as the index of the window in the tab control. It returns true if the
1243 // window was found, otherwise false.
1244 bool wxAuiMultiNotebook::FindTab(wxWindow
* page
, wxAuiTabCtrl
** ctrl
, int* idx
)
1246 wxPaneInfoArray
& all_panes
= m_mgr
.GetAllPanes();
1247 size_t i
, pane_count
= all_panes
.GetCount();
1248 for (i
= 0; i
< pane_count
; ++i
)
1250 if (all_panes
.Item(i
).name
== wxT("dummy"))
1253 wxTabFrame
* tabframe
= (wxTabFrame
*)all_panes
.Item(i
).window
;
1255 int page_idx
= tabframe
->m_tabs
->GetIdxFromWindow(page
);
1258 *ctrl
= tabframe
->m_tabs
;
1268 void wxAuiMultiNotebook::OnEraseBackground(wxEraseEvent
&)
1272 void wxAuiMultiNotebook::OnSize(wxSizeEvent
&)
1276 void wxAuiMultiNotebook::OnTabClicked(wxCommandEvent
& command_evt
)
1278 wxAuiNotebookEvent
& evt
= (wxAuiNotebookEvent
&)command_evt
;
1280 wxAuiTabCtrl
* ctrl
= (wxAuiTabCtrl
*)evt
.GetEventObject();
1281 wxASSERT(ctrl
!= NULL
);
1283 wxWindow
* wnd
= ctrl
->GetWindowFromIdx(evt
.GetSelection());
1284 wxASSERT(wnd
!= NULL
);
1286 int idx
= m_tabs
.GetIdxFromWindow(wnd
);
1287 wxASSERT(idx
!= -1);
1292 void wxAuiMultiNotebook::OnTabBeginDrag(wxCommandEvent
&)
1296 void wxAuiMultiNotebook::OnTabDragMotion(wxCommandEvent
& evt
)
1298 wxPoint screen_pt
= ::wxGetMousePosition();
1299 wxPoint client_pt
= ScreenToClient(screen_pt
);
1302 wxAuiTabCtrl
* src_tabs
= (wxAuiTabCtrl
*)evt
.GetEventObject();
1304 wxAuiTabCtrl
* tab_ctrl
= GetTabCtrlFromPoint(client_pt
);
1305 if (tab_ctrl
== src_tabs
)
1307 // inner-tabctrl dragging is not yet implemented
1314 wxRect hint_rect
= tab_ctrl
->GetRect();
1315 ClientToScreen(&hint_rect
.x
, &hint_rect
.y
);
1316 m_mgr
.ShowHint(hint_rect
);
1320 m_mgr
.DrawHintRect(m_dummy_wnd
, client_pt
, zero
);
1326 void wxAuiMultiNotebook::OnTabEndDrag(wxCommandEvent
& command_evt
)
1328 wxAuiNotebookEvent
& evt
= (wxAuiNotebookEvent
&)command_evt
;
1333 // get the mouse position, which will be used to determine the drop point
1334 wxPoint mouse_screen_pt
= ::wxGetMousePosition();
1335 wxPoint mouse_client_pt
= ScreenToClient(mouse_screen_pt
);
1338 // the src tab control is the control that fired this event
1339 wxAuiTabCtrl
* src_tabs
= (wxAuiTabCtrl
*)evt
.GetEventObject();
1340 wxAuiTabCtrl
* dest_tabs
= NULL
;
1343 // If the pointer is in an existing tab frame, do a tab insert
1344 wxWindow
* hit_wnd
= ::wxFindWindowAtPoint(mouse_screen_pt
);
1345 wxTabFrame
* tab_frame
= (wxTabFrame
*)GetTabFrameFromTabCtrl(hit_wnd
);
1348 dest_tabs
= tab_frame
->m_tabs
;
1350 if (dest_tabs
== src_tabs
)
1355 // If there is no tabframe at all, create one
1356 wxTabFrame
* new_tabs
= new wxTabFrame
;
1357 new_tabs
->m_tabs
= new wxAuiTabCtrl(this,
1362 m_mgr
.AddPane(new_tabs
,
1363 wxPaneInfo().Bottom().CaptionVisible(false),
1366 dest_tabs
= new_tabs
->m_tabs
;
1371 // remove the page from the source tabs
1372 wxAuiNotebookPage page_info
= src_tabs
->GetPage(evt
.GetSelection());
1373 page_info
.active
= false;
1374 src_tabs
->RemovePage(page_info
.window
);
1375 if (src_tabs
->GetPageCount() > 0)
1377 src_tabs
->SetActivePage((size_t)0);
1378 src_tabs
->DoShowHide();
1379 src_tabs
->Refresh();
1384 // add the page to the destination tabs
1385 dest_tabs
->AddPage(page_info
.window
, page_info
);
1387 if (src_tabs
->GetPageCount() == 0)
1389 RemoveEmptyTabFrames();
1393 dest_tabs
->DoShowHide();
1394 dest_tabs
->Refresh();
1396 SetSelection(m_tabs
.GetIdxFromWindow(page_info
.window
));
1399 wxAuiTabCtrl
* wxAuiMultiNotebook::GetTabCtrlFromPoint(const wxPoint
& pt
)
1401 // if we've just removed the last tab from the source
1402 // tab set, the remove the tab control completely
1403 wxPaneInfoArray
& all_panes
= m_mgr
.GetAllPanes();
1404 size_t i
, pane_count
= all_panes
.GetCount();
1405 for (i
= 0; i
< pane_count
; ++i
)
1407 if (all_panes
.Item(i
).name
== wxT("dummy"))
1410 wxTabFrame
* tabframe
= (wxTabFrame
*)all_panes
.Item(i
).window
;
1411 if (tabframe
->m_tab_rect
.Inside(pt
))
1412 return tabframe
->m_tabs
;
1418 wxWindow
* wxAuiMultiNotebook::GetTabFrameFromTabCtrl(wxWindow
* tab_ctrl
)
1420 // if we've just removed the last tab from the source
1421 // tab set, the remove the tab control completely
1422 wxPaneInfoArray
& all_panes
= m_mgr
.GetAllPanes();
1423 size_t i
, pane_count
= all_panes
.GetCount();
1424 for (i
= 0; i
< pane_count
; ++i
)
1426 if (all_panes
.Item(i
).name
== wxT("dummy"))
1429 wxTabFrame
* tabframe
= (wxTabFrame
*)all_panes
.Item(i
).window
;
1430 if (tabframe
->m_tabs
== tab_ctrl
)
1439 void wxAuiMultiNotebook::RemoveEmptyTabFrames()
1441 bool must_update
= false;
1443 // if we've just removed the last tab from the source
1444 // tab set, the remove the tab control completely
1445 wxPaneInfoArray all_panes
= m_mgr
.GetAllPanes();
1446 size_t i
, pane_count
= all_panes
.GetCount();
1447 for (i
= 0; i
< pane_count
; ++i
)
1449 if (all_panes
.Item(i
).name
== wxT("dummy"))
1452 wxTabFrame
* tab_frame
= (wxTabFrame
*)all_panes
.Item(i
).window
;
1453 if (tab_frame
->m_tabs
->GetPageCount() == 0)
1455 m_mgr
.DetachPane(tab_frame
);
1457 // use pending delete because sometimes during
1458 // window closing, refreshs are pending
1459 if (!wxPendingDelete
.Member(tab_frame
->m_tabs
))
1460 wxPendingDelete
.Append(tab_frame
->m_tabs
);
1461 //tab_frame->m_tabs->Destroy();
1469 // check to see if there is still a center pane;
1470 // if there isn't, make a frame the center pane
1471 wxPaneInfoArray panes
= m_mgr
.GetAllPanes();
1472 pane_count
= panes
.GetCount();
1473 wxWindow
* first_good
= NULL
;
1474 bool center_found
= false;
1475 for (i
= 0; i
< pane_count
; ++i
)
1477 if (panes
.Item(i
).name
== wxT("dummy"))
1479 if (panes
.Item(i
).dock_direction
== wxAUI_DOCK_CENTRE
)
1480 center_found
= true;
1482 first_good
= panes
.Item(i
).window
;
1485 if (!center_found
&& first_good
)
1487 m_mgr
.GetPane(first_good
).Centre();
1494 void wxAuiMultiNotebook::OnChildFocus(wxChildFocusEvent
& evt
)
1496 int idx
= m_tabs
.GetIdxFromWindow(evt
.GetWindow());
1497 if (idx
!= -1 && idx
!= m_curpage
)
1504 void wxAuiMultiNotebook::OnTabButton(wxCommandEvent
& command_evt
)
1506 wxAuiNotebookEvent
& evt
= (wxAuiNotebookEvent
&)command_evt
;
1507 wxAuiTabCtrl
* tabs
= (wxAuiTabCtrl
*)evt
.GetEventObject();
1509 int button_id
= evt
.GetInt();
1511 if (button_id
== wxAuiButtonClose
)
1513 int selection
= tabs
->GetActivePage();
1515 if (selection
!= -1)
1517 wxWindow
* close_wnd
= tabs
->GetWindowFromIdx(selection
);
1519 if (close_wnd
->IsKindOf(CLASSINFO(wxTabMDIChildFrame
)))
1525 int main_idx
= m_tabs
.GetIdxFromWindow(close_wnd
);
1526 DeletePage(main_idx
);