1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/aui/auibook.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"
26 #include "wx/settings.h"
30 #include "wx/aui/tabmdi.h"
31 #include "wx/dcbuffer.h"
33 #include "wx/arrimpl.cpp"
34 WX_DEFINE_OBJARRAY(wxAuiNotebookPageArray
)
35 WX_DEFINE_OBJARRAY(wxAuiTabContainerButtonArray
)
37 DEFINE_EVENT_TYPE(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGING
)
38 DEFINE_EVENT_TYPE(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGED
)
39 DEFINE_EVENT_TYPE(wxEVT_COMMAND_AUINOTEBOOK_BUTTON
)
40 DEFINE_EVENT_TYPE(wxEVT_COMMAND_AUINOTEBOOK_BEGIN_DRAG
)
41 DEFINE_EVENT_TYPE(wxEVT_COMMAND_AUINOTEBOOK_END_DRAG
)
42 DEFINE_EVENT_TYPE(wxEVT_COMMAND_AUINOTEBOOK_DRAG_MOTION
)
46 IMPLEMENT_DYNAMIC_CLASS(wxAuiNotebookEvent
, wxEvent
)
48 // -- wxAuiTabContainer class implementation --
51 // wxAuiTabContainer is a class which contains information about each
52 // tab. It also can render an entire tab control to a specified DC.
53 // It's not a window class itself, because this code will be used by
54 // the wxFrameMananger, where it is disadvantageous to have separate
55 // windows for each tab control in the case of "docked tabs"
57 // A derived class, wxAuiTabCtrl, is an actual wxWindow-derived window
58 // which can be used as a tab control in the normal sense.
61 // This functions are here for this proof of concept
62 // and will be factored out later. See dockart.cpp
63 static wxColor
StepColour(const wxColor
& c
, int percent
)
65 int r
= c
.Red(), g
= c
.Green(), b
= c
.Blue();
66 return wxColour((unsigned char)wxMin((r
*percent
)/100,255),
67 (unsigned char)wxMin((g
*percent
)/100,255),
68 (unsigned char)wxMin((b
*percent
)/100,255));
71 // This functions are here for this proof of concept
72 // and will be factored out later. See dockart.cpp
73 static wxBitmap
BitmapFromBits(const unsigned char bits
[], int w
, int h
,
74 const wxColour
& color
)
76 wxImage img
= wxBitmap((const char*)bits
, w
, h
).ConvertToImage();
77 img
.Replace(255,255,255,123,123,123);
78 img
.Replace(0,0,0,color
.Red(),color
.Green(),color
.Blue());
79 img
.SetMaskColour(123,123,123);
83 static void DrawButton(wxDC
& dc
,
86 const wxColour
& bkcolour
,
91 if (button_state
== wxAUI_BUTTON_STATE_PRESSED
)
97 if (button_state
== wxAUI_BUTTON_STATE_HOVER
||
98 button_state
== wxAUI_BUTTON_STATE_PRESSED
)
100 dc
.SetBrush(wxBrush(StepColour(bkcolour
, 120)));
101 dc
.SetPen(wxPen(StepColour(bkcolour
, 70)));
103 // draw the background behind the button
104 dc
.DrawRectangle(rect
.x
, rect
.y
, 15, 15);
107 // draw the button itself
108 dc
.DrawBitmap(bmp
, rect
.x
, rect
.y
, true);
114 wxAuiTabContainer::wxAuiTabContainer()
116 m_normal_font
= *wxNORMAL_FONT
;
117 m_selected_font
= *wxNORMAL_FONT
;
118 m_selected_font
.SetWeight(wxBOLD
);
119 m_measuring_font
= m_selected_font
;
121 wxColour base_colour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
123 wxColour background_colour
= StepColour(base_colour
, 95);
124 wxColour normaltab_colour
= base_colour
;
125 wxColour selectedtab_colour
= *wxWHITE
;
127 m_bkbrush
= wxBrush(background_colour
);
128 m_normal_bkbrush
= wxBrush(normaltab_colour
);
129 m_normal_bkpen
= wxPen(normaltab_colour
);
130 m_selected_bkbrush
= wxBrush(selectedtab_colour
);
131 m_selected_bkpen
= wxPen(selectedtab_colour
);
134 wxAuiTabContainer::~wxAuiTabContainer()
138 void wxAuiTabContainer::SetNormalFont(const wxFont
& font
)
140 m_normal_font
= font
;
143 void wxAuiTabContainer::SetSelectedFont(const wxFont
& font
)
145 m_selected_font
= font
;
148 void wxAuiTabContainer::SetMeasuringFont(const wxFont
& font
)
150 m_measuring_font
= font
;
153 void wxAuiTabContainer::SetRect(const wxRect
& rect
)
158 bool wxAuiTabContainer::AddPage(wxWindow
* page
,
159 const wxAuiNotebookPage
& info
)
161 wxAuiNotebookPage page_info
;
163 page_info
.window
= page
;
165 m_pages
.Add(page_info
);
170 bool wxAuiTabContainer::InsertPage(wxWindow
* page
,
171 const wxAuiNotebookPage
& info
,
174 wxAuiNotebookPage page_info
;
176 page_info
.window
= page
;
178 if (idx
>= m_pages
.GetCount())
179 m_pages
.Add(page_info
);
181 m_pages
.Insert(page_info
, idx
);
186 bool wxAuiTabContainer::RemovePage(wxWindow
* wnd
)
188 size_t i
, page_count
= m_pages
.GetCount();
189 for (i
= 0; i
< page_count
; ++i
)
191 wxAuiNotebookPage
& page
= m_pages
.Item(i
);
192 if (page
.window
== wnd
)
202 bool wxAuiTabContainer::SetActivePage(wxWindow
* wnd
)
206 size_t i
, page_count
= m_pages
.GetCount();
207 for (i
= 0; i
< page_count
; ++i
)
209 wxAuiNotebookPage
& page
= m_pages
.Item(i
);
210 if (page
.window
== wnd
)
224 void wxAuiTabContainer::SetNoneActive()
226 size_t i
, page_count
= m_pages
.GetCount();
227 for (i
= 0; i
< page_count
; ++i
)
229 wxAuiNotebookPage
& page
= m_pages
.Item(i
);
234 bool wxAuiTabContainer::SetActivePage(size_t page
)
236 if (page
>= m_pages
.GetCount())
239 return SetActivePage(m_pages
.Item(page
).window
);
242 int wxAuiTabContainer::GetActivePage() const
244 size_t i
, page_count
= m_pages
.GetCount();
245 for (i
= 0; i
< page_count
; ++i
)
247 wxAuiNotebookPage
& page
= m_pages
.Item(i
);
255 wxWindow
* wxAuiTabContainer::GetWindowFromIdx(size_t idx
) const
257 if (idx
>= m_pages
.GetCount())
260 return m_pages
[idx
].window
;
263 int wxAuiTabContainer::GetIdxFromWindow(wxWindow
* wnd
) const
265 size_t i
, page_count
= m_pages
.GetCount();
266 for (i
= 0; i
< page_count
; ++i
)
268 wxAuiNotebookPage
& page
= m_pages
.Item(i
);
269 if (page
.window
== wnd
)
275 wxAuiNotebookPage
& wxAuiTabContainer::GetPage(size_t idx
)
277 wxASSERT_MSG(idx
< m_pages
.GetCount(), wxT("Invalid Page index"));
282 wxAuiNotebookPageArray
& wxAuiTabContainer::GetPages()
287 size_t wxAuiTabContainer::GetPageCount() const
289 return m_pages
.GetCount();
292 void wxAuiTabContainer::AddButton(int id
, const wxBitmap
& bmp
)
294 wxAuiTabContainerButton button
;
297 button
.cur_state
= wxAUI_BUTTON_STATE_NORMAL
;
299 m_buttons
.Add(button
);
304 // DrawTab() draws an individual tab.
305 // As it is virtual it may be overridden.
308 // in_rect - rectangle the tab should be confined to
309 // caption - tab's caption
310 // active - whether or not the tab is active
311 // out_rect - actual output rectangle
312 // x_extent - the advance x; where the next tab should start
314 void wxAuiTabContainer::DrawTab(wxDC
* dc
,
315 const wxRect
& in_rect
,
316 const wxString
& caption
,
321 wxCoord normal_textx
, normal_texty
;
322 wxCoord selected_textx
, selected_texty
;
323 wxCoord measured_textx
, measured_texty
;
324 wxCoord textx
, texty
;
328 dc
->SetFont(m_measuring_font
);
329 dc
->GetTextExtent(caption
, &measured_textx
, &measured_texty
);
331 dc
->SetFont(m_selected_font
);
332 dc
->GetTextExtent(caption
, &selected_textx
, &selected_texty
);
334 dc
->SetFont(m_normal_font
);
335 dc
->GetTextExtent(caption
, &normal_textx
, &normal_texty
);
338 wxCoord tab_height
= measured_texty
+ 4;
339 wxCoord tab_width
= measured_textx
+ tab_height
+ 5;
340 wxCoord tab_x
= in_rect
.x
;
341 wxCoord tab_y
= in_rect
.y
+ in_rect
.height
- tab_height
;
344 // select pen, brush and font for the tab to be drawn
348 dc
->SetPen(m_selected_bkpen
);
349 dc
->SetBrush(m_selected_bkbrush
);
350 dc
->SetFont(m_selected_font
);
351 textx
= selected_textx
;
352 texty
= selected_texty
;
356 dc
->SetPen(m_normal_bkpen
);
357 dc
->SetBrush(m_normal_bkbrush
);
358 dc
->SetFont(m_normal_font
);
359 textx
= normal_textx
;
360 texty
= normal_texty
;
368 points
[0].y
= tab_y
+ tab_height
- 1;
369 points
[1].x
= tab_x
+ tab_height
- 3;
370 points
[1].y
= tab_y
+ 2;
371 points
[2].x
= tab_x
+ tab_height
+ 3;
373 points
[3].x
= tab_x
+ tab_width
- 2;
375 points
[4].x
= tab_x
+ tab_width
;
376 points
[4].y
= tab_y
+ 2;
377 points
[5].x
= tab_x
+ tab_width
;
378 points
[5].y
= tab_y
+ tab_height
- 1;
379 points
[6] = points
[0];
382 dc
->DrawPolygon(6, points
);
384 dc
->SetPen(*wxGREY_PEN
);
386 //dc->DrawLines(active ? 6 : 7, points);
387 dc
->DrawLines(7, points
);
391 dc
->DrawText(caption
,
392 tab_x
+ (tab_height
/3) + (tab_width
/2) - (textx
/2),
393 tab_y
+ tab_height
- texty
- 2);
395 *out_rect
= wxRect(tab_x
, tab_y
, tab_width
, tab_height
);
396 *x_extent
= tab_width
- (tab_height
/2) - 1;
400 // Render() renders the tab catalog to the specified DC
401 // It is a virtual function and can be overridden to
402 // provide custom drawing capabilities
403 void wxAuiTabContainer::Render(wxDC
* raw_dc
)
407 bmp
.Create(m_rect
.GetWidth(), m_rect
.GetHeight());
408 dc
.SelectObject(bmp
);
411 dc
.SetBrush(m_bkbrush
);
412 dc
.SetPen(*wxTRANSPARENT_PEN
);
413 dc
.DrawRectangle(-1, -1, m_rect
.GetWidth()+2, m_rect
.GetHeight()+2);
416 dc
.SetPen(*wxGREY_PEN
);
417 dc
.DrawLine(0, m_rect
.GetHeight()-1, m_rect
.GetWidth(), m_rect
.GetHeight()-1);
420 size_t i
, page_count
= m_pages
.GetCount();
424 int active_offset
= 0;
427 wxRect rect
= m_rect
;
430 rect
.height
= m_rect
.height
;
432 for (i
= 0; i
< page_count
; ++i
)
434 wxAuiNotebookPage
& page
= m_pages
.Item(i
);
448 active_offset
= offset
;
454 // draw the active tab again so it stands in the foreground
455 if (active
< m_pages
.GetCount())
457 wxAuiNotebookPage
& page
= m_pages
.Item(active
);
459 rect
.x
= active_offset
;
469 offset
= m_rect
.x
+ m_rect
.width
;
470 size_t button_count
= m_buttons
.GetCount();
471 for (i
= 0; i
< button_count
; ++i
)
473 wxAuiTabContainerButton
& button
= m_buttons
.Item(button_count
- i
- 1);
475 wxRect
button_rect(offset
- button
.bitmap
.GetWidth(), 1,
476 button
.bitmap
.GetWidth(), button
.bitmap
.GetHeight());
478 button
.rect
= button_rect
;
480 DrawButton(dc
, button
.rect
, button
.bitmap
,
481 m_bkbrush
.GetColour(),
484 offset
-= button
.bitmap
.GetWidth();
488 raw_dc
->Blit(m_rect
.x
, m_rect
.y
, m_rect
.GetWidth(), m_rect
.GetHeight(), &dc
, 0, 0);
492 // TabHitTest() tests if a tab was hit, passing the window pointer
493 // back if that condition was fulfilled. The function returns
494 // true if a tab was hit, otherwise false
495 bool wxAuiTabContainer::TabHitTest(int x
, int y
, wxWindow
** hit
) const
497 if (!m_rect
.Inside(x
,y
))
500 size_t i
, page_count
= m_pages
.GetCount();
502 for (i
= 0; i
< page_count
; ++i
)
504 wxAuiNotebookPage
& page
= m_pages
.Item(i
);
505 if (page
.rect
.Inside(x
,y
))
515 // ButtonHitTest() tests if a button was hit. The function returns
516 // true if a button was hit, otherwise false
517 bool wxAuiTabContainer::ButtonHitTest(int x
, int y
,
518 wxAuiTabContainerButton
** hit
) const
520 if (!m_rect
.Inside(x
,y
))
523 size_t i
, button_count
= m_buttons
.GetCount();
525 for (i
= 0; i
< button_count
; ++i
)
527 wxAuiTabContainerButton
& button
= m_buttons
.Item(i
);
528 if (button
.rect
.Inside(x
,y
))
540 // the utility function ShowWnd() is the same as show,
541 // except it handles wxTabMDIChildFrame windows as well,
542 // as the Show() method on this class is "unplugged"
543 static void ShowWnd(wxWindow
* wnd
, bool show
)
545 if (wnd
->IsKindOf(CLASSINFO(wxTabMDIChildFrame
)))
547 wxTabMDIChildFrame
* cf
= (wxTabMDIChildFrame
*)wnd
;
557 // DoShowHide() this function shows the active window, then
558 // hides all of the other windows (in that order)
559 void wxAuiTabContainer::DoShowHide()
561 wxAuiNotebookPageArray
& pages
= GetPages();
562 size_t i
, page_count
= pages
.GetCount();
564 // show new active page first
565 for (i
= 0; i
< page_count
; ++i
)
567 wxAuiNotebookPage
& page
= pages
.Item(i
);
570 ShowWnd(page
.window
, true);
575 // hide all other pages
576 for (i
= 0; i
< page_count
; ++i
)
578 wxAuiNotebookPage
& page
= pages
.Item(i
);
579 ShowWnd(page
.window
, page
.active
);
588 // -- wxAuiTabCtrl class implementation --
591 const int wxAuiButtonClose
= 101;
593 BEGIN_EVENT_TABLE(wxAuiTabCtrl
, wxControl
)
594 EVT_PAINT(wxAuiTabCtrl::OnPaint
)
595 EVT_ERASE_BACKGROUND(wxAuiTabCtrl::OnEraseBackground
)
596 EVT_SIZE(wxAuiTabCtrl::OnSize
)
597 EVT_LEFT_DOWN(wxAuiTabCtrl::OnLeftDown
)
598 EVT_LEFT_UP(wxAuiTabCtrl::OnLeftUp
)
599 EVT_MOTION(wxAuiTabCtrl::OnMotion
)
600 EVT_LEAVE_WINDOW(wxAuiTabCtrl::OnLeaveWindow
)
604 wxAuiTabCtrl::wxAuiTabCtrl(wxWindow
* parent
,
608 long style
) : wxControl(parent
, id
, pos
, size
, style
)
610 m_click_pt
= wxDefaultPosition
;
611 m_is_dragging
= false;
612 m_hover_button
= NULL
;
614 // FIXME: copied from dockart-- needs to put in a common place
615 static unsigned char close_bits
[]={
616 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xef,0xfb,0xcf,0xf9,
617 0x9f,0xfc,0x3f,0xfe,0x3f,0xfe,0x9f,0xfc,0xcf,0xf9,0xef,0xfb,
618 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
620 AddButton(101, BitmapFromBits(close_bits
, 16, 16, *wxBLACK
));
624 void wxAuiTabCtrl::OnPaint(wxPaintEvent
&)
628 dc
.SetFont(GetFont());
630 if (GetPageCount() > 0)
634 void wxAuiTabCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(evt
))
638 void wxAuiTabCtrl::OnSize(wxSizeEvent
& evt
)
640 wxSize s
= evt
.GetSize();
641 wxRect
r(0, 0, s
.GetWidth(), s
.GetHeight());
645 void wxAuiTabCtrl::OnLeftDown(wxMouseEvent
& evt
)
648 m_click_pt
= wxDefaultPosition
;
649 m_is_dragging
= false;
653 if (TabHitTest(evt
.m_x
, evt
.m_y
, &wnd
))
655 wxAuiNotebookEvent
e(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGING
, m_windowId
);
656 e
.SetSelection(GetIdxFromWindow(wnd
));
657 e
.SetOldSelection(GetActivePage());
658 e
.SetEventObject(this);
659 GetEventHandler()->ProcessEvent(e
);
661 m_click_pt
.x
= evt
.m_x
;
662 m_click_pt
.y
= evt
.m_y
;
663 m_click_tab
= e
.GetSelection();
668 m_hover_button
->cur_state
= wxAUI_BUTTON_STATE_PRESSED
;
674 void wxAuiTabCtrl::OnLeftUp(wxMouseEvent
&)
676 if (GetCapture() == this)
681 wxAuiNotebookEvent
evt(wxEVT_COMMAND_AUINOTEBOOK_END_DRAG
, m_windowId
);
682 evt
.SetSelection(m_click_tab
);
683 evt
.SetOldSelection(m_click_tab
);
684 evt
.SetEventObject(this);
685 GetEventHandler()->ProcessEvent(evt
);
691 m_hover_button
->cur_state
= wxAUI_BUTTON_STATE_HOVER
;
695 wxAuiNotebookEvent
evt(wxEVT_COMMAND_AUINOTEBOOK_BUTTON
, m_windowId
);
696 evt
.SetInt(m_hover_button
->id
);
697 evt
.SetEventObject(this);
698 GetEventHandler()->ProcessEvent(evt
);
701 m_click_pt
= wxDefaultPosition
;
702 m_is_dragging
= false;
706 void wxAuiTabCtrl::OnMotion(wxMouseEvent
& evt
)
708 wxPoint pos
= evt
.GetPosition();
710 // check if the mouse is hovering above a button
711 wxAuiTabContainerButton
* button
;
712 if (ButtonHitTest(pos
.x
, pos
.y
, &button
))
714 if (button
->cur_state
!= wxAUI_BUTTON_STATE_HOVER
)
716 button
->cur_state
= wxAUI_BUTTON_STATE_HOVER
;
719 m_hover_button
= button
;
727 m_hover_button
->cur_state
= wxAUI_BUTTON_STATE_NORMAL
;
728 m_hover_button
= NULL
;
735 if (!evt
.LeftIsDown() || m_click_pt
== wxDefaultPosition
)
740 wxAuiNotebookEvent
evt(wxEVT_COMMAND_AUINOTEBOOK_DRAG_MOTION
, m_windowId
);
741 evt
.SetSelection(m_click_tab
);
742 evt
.SetOldSelection(m_click_tab
);
743 evt
.SetEventObject(this);
744 GetEventHandler()->ProcessEvent(evt
);
749 int drag_x_threshold
= wxSystemSettings::GetMetric(wxSYS_DRAG_X
);
750 int drag_y_threshold
= wxSystemSettings::GetMetric(wxSYS_DRAG_Y
);
752 if (abs(pos
.x
- m_click_pt
.x
) > drag_x_threshold
||
753 abs(pos
.y
- m_click_pt
.y
) > drag_y_threshold
)
755 wxAuiNotebookEvent
evt(wxEVT_COMMAND_AUINOTEBOOK_BEGIN_DRAG
, m_windowId
);
756 evt
.SetSelection(m_click_tab
);
757 evt
.SetOldSelection(m_click_tab
);
758 evt
.SetEventObject(this);
759 GetEventHandler()->ProcessEvent(evt
);
761 m_is_dragging
= true;
765 void wxAuiTabCtrl::OnLeaveWindow(wxMouseEvent
& WXUNUSED(event
))
769 m_hover_button
->cur_state
= wxAUI_BUTTON_STATE_NORMAL
;
770 m_hover_button
= NULL
;
777 // wxTabFrame is an interesting case. It's important that all child pages
778 // of the multi-notebook control are all actually children of that control
779 // (and not grandchildren). wxTabFrame facilitates this. There is one
780 // instance of wxTabFrame for each tab control inside the multi-notebook.
781 // It's important to know that wxTabFrame is not a real window, but it merely
782 // used to capture the dimensions/positioning of the internal tab control and
783 // it's managed page windows
785 class wxTabFrame
: public wxWindow
792 m_rect
= wxRect(0,0,200,200);
793 m_tab_ctrl_height
= 20;
796 void SetTabCtrlHeight(int h
)
798 m_tab_ctrl_height
= h
;
801 void DoSetSize(int x
, int y
,
802 int width
, int height
,
803 int WXUNUSED(sizeFlags
= wxSIZE_AUTO
))
805 m_rect
= wxRect(x
, y
, width
, height
);
809 void DoGetClientSize(int* x
, int* y
) const
815 bool Show( bool WXUNUSED(show
= true) ) { return false; }
822 int tab_height
= wxMin(m_rect
.height
, m_tab_ctrl_height
);
823 m_tab_rect
= wxRect(m_rect
.x
, m_rect
.y
, m_rect
.width
, tab_height
);
824 m_tabs
->SetSize(m_rect
.x
, m_rect
.y
, m_rect
.width
, tab_height
);
825 m_tabs
->SetRect(wxRect(0, 0, m_rect
.width
, tab_height
));
828 wxAuiNotebookPageArray
& pages
= m_tabs
->GetPages();
829 size_t i
, page_count
= pages
.GetCount();
831 for (i
= 0; i
< page_count
; ++i
)
833 wxAuiNotebookPage
& page
= pages
.Item(i
);
834 page
.window
->SetSize(m_rect
.x
, m_rect
.y
+tab_height
, m_rect
.width
, m_rect
.height
-tab_height
);
836 if (page
.window
->IsKindOf(CLASSINFO(wxTabMDIChildFrame
)))
838 wxTabMDIChildFrame
* wnd
= (wxTabMDIChildFrame
*)page
.window
;
839 wnd
->ApplyMDIChildFrameRect();
844 void DoGetSize(int* x
, int* y
) const
847 *x
= m_rect
.GetWidth();
849 *y
= m_rect
.GetHeight();
861 wxAuiTabCtrl
* m_tabs
;
862 int m_tab_ctrl_height
;
869 // -- wxAuiMultiNotebook class implementation --
871 BEGIN_EVENT_TABLE(wxAuiMultiNotebook
, wxControl
)
872 //EVT_ERASE_BACKGROUND(wxAuiMultiNotebook::OnEraseBackground)
873 //EVT_SIZE(wxAuiMultiNotebook::OnSize)
874 //EVT_LEFT_DOWN(wxAuiMultiNotebook::OnLeftDown)
875 EVT_CHILD_FOCUS(wxAuiMultiNotebook::OnChildFocus
)
876 EVT_COMMAND_RANGE(10000, 10100,
877 wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGING
,
878 wxAuiMultiNotebook::OnTabClicked
)
879 EVT_COMMAND_RANGE(10000, 10100,
880 wxEVT_COMMAND_AUINOTEBOOK_BEGIN_DRAG
,
881 wxAuiMultiNotebook::OnTabBeginDrag
)
882 EVT_COMMAND_RANGE(10000, 10100,
883 wxEVT_COMMAND_AUINOTEBOOK_END_DRAG
,
884 wxAuiMultiNotebook::OnTabEndDrag
)
885 EVT_COMMAND_RANGE(10000, 10100,
886 wxEVT_COMMAND_AUINOTEBOOK_DRAG_MOTION
,
887 wxAuiMultiNotebook::OnTabDragMotion
)
888 EVT_COMMAND_RANGE(10000, 10100,
889 wxEVT_COMMAND_AUINOTEBOOK_BUTTON
,
890 wxAuiMultiNotebook::OnTabButton
)
893 wxAuiMultiNotebook::wxAuiMultiNotebook()
896 m_tab_id_counter
= 10000;
898 m_tab_ctrl_height
= 20;
901 wxAuiMultiNotebook::wxAuiMultiNotebook(wxWindow
*parent
,
905 long style
) : wxControl(parent
, id
, pos
, size
, style
)
910 bool wxAuiMultiNotebook::Create(wxWindow
* parent
,
916 if (!wxControl::Create(parent
, id
, pos
, size
, style
))
924 // InitNotebook() contains common initialization
925 // code called by all constructors
926 void wxAuiMultiNotebook::InitNotebook()
929 m_tab_id_counter
= 10000;
931 m_tab_ctrl_height
= 20;
933 m_normal_font
= *wxNORMAL_FONT
;
934 m_selected_font
= *wxNORMAL_FONT
;
935 m_selected_font
.SetWeight(wxBOLD
);
937 // choose a default for the tab height
940 dc
.SetFont(m_selected_font
);
941 dc
.GetTextExtent(wxT("ABCDEFGHhijklm"), &tx
, &ty
);
942 m_tab_ctrl_height
= (ty
*150)/100;
944 m_dummy_wnd
= new wxWindow(this, wxID_ANY
, wxPoint(0,0), wxSize(0,0));
945 m_dummy_wnd
->SetSize(200, 200);
946 m_dummy_wnd
->Show(false);
948 m_mgr
.SetManagedWindow(this);
950 m_mgr
.AddPane(m_dummy_wnd
,
951 wxPaneInfo().Name(wxT("dummy")).Bottom().Show(false));
956 wxAuiMultiNotebook::~wxAuiMultiNotebook()
961 bool wxAuiMultiNotebook::AddPage(wxWindow
* page
,
962 const wxString
& caption
,
964 const wxBitmap
& bitmap
)
966 return InsertPage(GetPageCount(), page
, caption
, select
, bitmap
);
969 bool wxAuiMultiNotebook::InsertPage(size_t page_idx
,
971 const wxString
& caption
,
973 const wxBitmap
& bitmap
)
975 wxAuiNotebookPage info
;
977 info
.caption
= caption
;
978 info
.bitmap
= bitmap
;
981 // if there are currently no tabs, the first added
982 // tab must be active
983 if (m_tabs
.GetPageCount() == 0)
986 m_tabs
.InsertPage(page
, info
, page_idx
);
988 wxAuiTabCtrl
* active_tabctrl
= GetActiveTabCtrl();
989 if (page_idx
>= active_tabctrl
->GetPageCount())
990 active_tabctrl
->AddPage(page
, info
);
992 active_tabctrl
->InsertPage(page
, info
, page_idx
);
995 active_tabctrl
->DoShowHide();
999 int idx
= m_tabs
.GetIdxFromWindow(page
);
1000 wxASSERT_MSG(idx
!= -1, wxT("Invalid Page index returned on wxAuiMultiNotebook::InsertPage()"));
1009 // DeletePage() removes a tab from the multi-notebook,
1010 // and destroys the window as well
1011 bool wxAuiMultiNotebook::DeletePage(size_t page_idx
)
1013 wxWindow
* wnd
= m_tabs
.GetWindowFromIdx(page_idx
);
1016 // find out which onscreen tab ctrl owns this tab
1019 if (!FindTab(wnd
, &ctrl
, &ctrl_idx
))
1022 // find a new page and set it as active
1023 int new_idx
= ctrl_idx
+1;
1024 if (new_idx
>= (int)ctrl
->GetPageCount())
1025 new_idx
= ctrl_idx
-1;
1027 if (new_idx
>= 0 && new_idx
< (int)ctrl
->GetPageCount())
1029 wxWindow
* new_wnd
= ctrl
->GetWindowFromIdx(new_idx
);
1030 int main_idx
= m_tabs
.GetIdxFromWindow(new_wnd
);
1031 wxASSERT(main_idx
!= -1);
1032 SetSelection(main_idx
);
1036 // set the active page to the first page that
1037 // isn't the one being deleted
1039 size_t i
, page_count
= m_tabs
.GetPageCount();
1040 for (i
= 0; i
< page_count
; ++i
)
1042 wxWindow
* w
= m_tabs
.GetWindowFromIdx(i
);
1056 // remove the tab from main catalog
1057 if (!m_tabs
.RemovePage(wnd
))
1060 // remove the tab from the onscreen tab ctrl
1061 ctrl
->RemovePage(wnd
);
1063 // actually destroy the window now
1064 if (wnd
->IsKindOf(CLASSINFO(wxTabMDIChildFrame
)))
1066 // delete the child frame with pending delete, as is
1067 // customary with frame windows
1068 if (!wxPendingDelete
.Member(wnd
))
1069 wxPendingDelete
.Append(wnd
);
1076 RemoveEmptyTabFrames();
1083 // RemovePage() removes a tab from the multi-notebook,
1084 // but does not destroy the window
1085 bool wxAuiMultiNotebook::RemovePage(size_t page_idx
)
1087 // remove the tab from our own catalog
1088 wxWindow
* wnd
= m_tabs
.GetWindowFromIdx(page_idx
);
1089 if (!m_tabs
.RemovePage(wnd
))
1092 // remove the tab from the onscreen tab ctrl
1095 if (FindTab(wnd
, &ctrl
, &ctrl_idx
))
1097 ctrl
->RemovePage(wnd
);
1104 // SetPageText() changes the tab caption of the specified page
1105 bool wxAuiMultiNotebook::SetPageText(size_t page_idx
, const wxString
& text
)
1107 if (page_idx
>= m_tabs
.GetPageCount())
1110 // update our own tab catalog
1111 wxAuiNotebookPage
& page_info
= m_tabs
.GetPage(page_idx
);
1112 page_info
.caption
= text
;
1114 // update what's on screen
1117 if (FindTab(page_info
.window
, &ctrl
, &ctrl_idx
))
1119 wxAuiNotebookPage
& info
= ctrl
->GetPage(ctrl_idx
);
1120 info
.caption
= text
;
1128 // GetSelection() returns the index of the currently active page
1129 int wxAuiMultiNotebook::GetSelection() const
1134 // SetSelection() sets the currently active page
1135 size_t wxAuiMultiNotebook::SetSelection(size_t new_page
)
1137 wxWindow
* wnd
= m_tabs
.GetWindowFromIdx(new_page
);
1141 wxAuiNotebookEvent
evt(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGING
, m_windowId
);
1142 evt
.SetSelection(new_page
);
1143 evt
.SetOldSelection(m_curpage
);
1144 evt
.SetEventObject(this);
1145 if (!GetEventHandler()->ProcessEvent(evt
) || evt
.IsAllowed())
1147 // program allows the page change
1148 evt
.SetEventType(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGED
);
1149 (void)GetEventHandler()->ProcessEvent(evt
);
1155 if (FindTab(wnd
, &ctrl
, &ctrl_idx
))
1157 m_tabs
.SetActivePage(wnd
);
1159 ctrl
->SetActivePage(ctrl_idx
);
1163 int old_curpage
= m_curpage
;
1164 m_curpage
= new_page
;
1168 wxPaneInfoArray
& all_panes
= m_mgr
.GetAllPanes();
1169 size_t i
, pane_count
= all_panes
.GetCount();
1170 for (i
= 0; i
< pane_count
; ++i
)
1172 wxPaneInfo
& pane
= all_panes
.Item(i
);
1173 if (pane
.name
== wxT("dummy"))
1175 wxAuiTabCtrl
* tabctrl
= ((wxTabFrame
*)pane
.window
)->m_tabs
;
1176 if (tabctrl
!= ctrl
)
1177 tabctrl
->SetSelectedFont(m_normal_font
);
1179 tabctrl
->SetSelectedFont(m_selected_font
);
1192 // GetPageCount() returns the total number of
1193 // pages managed by the multi-notebook
1194 size_t wxAuiMultiNotebook::GetPageCount() const
1196 return m_tabs
.GetPageCount();
1199 // GetPage() returns the wxWindow pointer of the
1201 wxWindow
* wxAuiMultiNotebook::GetPage(size_t page_idx
) const
1203 wxASSERT(page_idx
< m_tabs
.GetPageCount());
1205 return m_tabs
.GetWindowFromIdx(page_idx
);
1208 // DoSizing() performs all sizing operations in each tab control
1209 void wxAuiMultiNotebook::DoSizing()
1211 wxPaneInfoArray
& all_panes
= m_mgr
.GetAllPanes();
1212 size_t i
, pane_count
= all_panes
.GetCount();
1213 for (i
= 0; i
< pane_count
; ++i
)
1215 if (all_panes
.Item(i
).name
== wxT("dummy"))
1218 wxTabFrame
* tabframe
= (wxTabFrame
*)all_panes
.Item(i
).window
;
1219 tabframe
->DoSizing();
1223 // GetActiveTabCtrl() returns the active tab control. It is
1224 // called to determine which control gets new windows being added
1225 wxAuiTabCtrl
* wxAuiMultiNotebook::GetActiveTabCtrl()
1227 if (m_curpage
>= 0 && m_curpage
< (int)m_tabs
.GetPageCount())
1232 // find the tab ctrl with the current page
1233 if (FindTab(m_tabs
.GetPage(m_curpage
).window
,
1240 // no current page, just find the first tab ctrl
1241 wxPaneInfoArray
& all_panes
= m_mgr
.GetAllPanes();
1242 size_t i
, pane_count
= all_panes
.GetCount();
1243 for (i
= 0; i
< pane_count
; ++i
)
1245 if (all_panes
.Item(i
).name
== wxT("dummy"))
1248 wxTabFrame
* tabframe
= (wxTabFrame
*)all_panes
.Item(i
).window
;
1249 return tabframe
->m_tabs
;
1252 // If there is no tabframe at all, create one
1253 wxTabFrame
* tabframe
= new wxTabFrame
;
1254 tabframe
->SetTabCtrlHeight(m_tab_ctrl_height
);
1255 tabframe
->m_tabs
= new wxAuiTabCtrl(this,
1260 m_mgr
.AddPane(tabframe
,
1261 wxPaneInfo().Center().CaptionVisible(false));
1265 return tabframe
->m_tabs
;
1268 // FindTab() finds the tab control that currently contains the window as well
1269 // as the index of the window in the tab control. It returns true if the
1270 // window was found, otherwise false.
1271 bool wxAuiMultiNotebook::FindTab(wxWindow
* page
, wxAuiTabCtrl
** ctrl
, int* idx
)
1273 wxPaneInfoArray
& all_panes
= m_mgr
.GetAllPanes();
1274 size_t i
, pane_count
= all_panes
.GetCount();
1275 for (i
= 0; i
< pane_count
; ++i
)
1277 if (all_panes
.Item(i
).name
== wxT("dummy"))
1280 wxTabFrame
* tabframe
= (wxTabFrame
*)all_panes
.Item(i
).window
;
1282 int page_idx
= tabframe
->m_tabs
->GetIdxFromWindow(page
);
1285 *ctrl
= tabframe
->m_tabs
;
1295 void wxAuiMultiNotebook::OnEraseBackground(wxEraseEvent
&)
1299 void wxAuiMultiNotebook::OnSize(wxSizeEvent
&)
1303 void wxAuiMultiNotebook::OnTabClicked(wxCommandEvent
& command_evt
)
1305 wxAuiNotebookEvent
& evt
= (wxAuiNotebookEvent
&)command_evt
;
1307 wxAuiTabCtrl
* ctrl
= (wxAuiTabCtrl
*)evt
.GetEventObject();
1308 wxASSERT(ctrl
!= NULL
);
1310 wxWindow
* wnd
= ctrl
->GetWindowFromIdx(evt
.GetSelection());
1311 wxASSERT(wnd
!= NULL
);
1313 int idx
= m_tabs
.GetIdxFromWindow(wnd
);
1314 wxASSERT(idx
!= -1);
1319 void wxAuiMultiNotebook::OnTabBeginDrag(wxCommandEvent
&)
1323 void wxAuiMultiNotebook::OnTabDragMotion(wxCommandEvent
& evt
)
1325 wxPoint screen_pt
= ::wxGetMousePosition();
1326 wxPoint client_pt
= ScreenToClient(screen_pt
);
1329 wxAuiTabCtrl
* src_tabs
= (wxAuiTabCtrl
*)evt
.GetEventObject();
1331 wxAuiTabCtrl
* tab_ctrl
= GetTabCtrlFromPoint(client_pt
);
1332 if (tab_ctrl
== src_tabs
)
1334 // inner-tabctrl dragging is not yet implemented
1341 wxRect hint_rect
= tab_ctrl
->GetRect();
1342 ClientToScreen(&hint_rect
.x
, &hint_rect
.y
);
1343 m_mgr
.ShowHint(hint_rect
);
1347 m_mgr
.DrawHintRect(m_dummy_wnd
, client_pt
, zero
);
1353 void wxAuiMultiNotebook::OnTabEndDrag(wxCommandEvent
& command_evt
)
1355 wxAuiNotebookEvent
& evt
= (wxAuiNotebookEvent
&)command_evt
;
1360 // get the mouse position, which will be used to determine the drop point
1361 wxPoint mouse_screen_pt
= ::wxGetMousePosition();
1362 wxPoint mouse_client_pt
= ScreenToClient(mouse_screen_pt
);
1365 // the src tab control is the control that fired this event
1366 wxAuiTabCtrl
* src_tabs
= (wxAuiTabCtrl
*)evt
.GetEventObject();
1367 wxAuiTabCtrl
* dest_tabs
= NULL
;
1370 // If the pointer is in an existing tab frame, do a tab insert
1371 wxWindow
* hit_wnd
= ::wxFindWindowAtPoint(mouse_screen_pt
);
1372 wxTabFrame
* tab_frame
= (wxTabFrame
*)GetTabFrameFromTabCtrl(hit_wnd
);
1375 dest_tabs
= tab_frame
->m_tabs
;
1377 if (dest_tabs
== src_tabs
)
1382 // If there is no tabframe at all, create one
1383 wxTabFrame
* new_tabs
= new wxTabFrame
;
1384 new_tabs
->SetTabCtrlHeight(m_tab_ctrl_height
);
1385 new_tabs
->m_tabs
= new wxAuiTabCtrl(this,
1390 m_mgr
.AddPane(new_tabs
,
1391 wxPaneInfo().Bottom().CaptionVisible(false),
1394 dest_tabs
= new_tabs
->m_tabs
;
1399 // remove the page from the source tabs
1400 wxAuiNotebookPage page_info
= src_tabs
->GetPage(evt
.GetSelection());
1401 page_info
.active
= false;
1402 src_tabs
->RemovePage(page_info
.window
);
1403 if (src_tabs
->GetPageCount() > 0)
1405 src_tabs
->SetActivePage((size_t)0);
1406 src_tabs
->DoShowHide();
1407 src_tabs
->Refresh();
1412 // add the page to the destination tabs
1413 dest_tabs
->AddPage(page_info
.window
, page_info
);
1415 if (src_tabs
->GetPageCount() == 0)
1417 RemoveEmptyTabFrames();
1421 dest_tabs
->DoShowHide();
1422 dest_tabs
->Refresh();
1424 SetSelection(m_tabs
.GetIdxFromWindow(page_info
.window
));
1427 wxAuiTabCtrl
* wxAuiMultiNotebook::GetTabCtrlFromPoint(const wxPoint
& pt
)
1429 // if we've just removed the last tab from the source
1430 // tab set, the remove the tab control completely
1431 wxPaneInfoArray
& all_panes
= m_mgr
.GetAllPanes();
1432 size_t i
, pane_count
= all_panes
.GetCount();
1433 for (i
= 0; i
< pane_count
; ++i
)
1435 if (all_panes
.Item(i
).name
== wxT("dummy"))
1438 wxTabFrame
* tabframe
= (wxTabFrame
*)all_panes
.Item(i
).window
;
1439 if (tabframe
->m_tab_rect
.Inside(pt
))
1440 return tabframe
->m_tabs
;
1446 wxWindow
* wxAuiMultiNotebook::GetTabFrameFromTabCtrl(wxWindow
* tab_ctrl
)
1448 // if we've just removed the last tab from the source
1449 // tab set, the remove the tab control completely
1450 wxPaneInfoArray
& all_panes
= m_mgr
.GetAllPanes();
1451 size_t i
, pane_count
= all_panes
.GetCount();
1452 for (i
= 0; i
< pane_count
; ++i
)
1454 if (all_panes
.Item(i
).name
== wxT("dummy"))
1457 wxTabFrame
* tabframe
= (wxTabFrame
*)all_panes
.Item(i
).window
;
1458 if (tabframe
->m_tabs
== tab_ctrl
)
1467 void wxAuiMultiNotebook::RemoveEmptyTabFrames()
1469 bool must_update
= false;
1471 // if we've just removed the last tab from the source
1472 // tab set, the remove the tab control completely
1473 wxPaneInfoArray all_panes
= m_mgr
.GetAllPanes();
1474 size_t i
, pane_count
= all_panes
.GetCount();
1475 for (i
= 0; i
< pane_count
; ++i
)
1477 if (all_panes
.Item(i
).name
== wxT("dummy"))
1480 wxTabFrame
* tab_frame
= (wxTabFrame
*)all_panes
.Item(i
).window
;
1481 if (tab_frame
->m_tabs
->GetPageCount() == 0)
1483 m_mgr
.DetachPane(tab_frame
);
1485 // use pending delete because sometimes during
1486 // window closing, refreshs are pending
1487 if (!wxPendingDelete
.Member(tab_frame
->m_tabs
))
1488 wxPendingDelete
.Append(tab_frame
->m_tabs
);
1489 //tab_frame->m_tabs->Destroy();
1497 // check to see if there is still a center pane;
1498 // if there isn't, make a frame the center pane
1499 wxPaneInfoArray panes
= m_mgr
.GetAllPanes();
1500 pane_count
= panes
.GetCount();
1501 wxWindow
* first_good
= NULL
;
1502 bool center_found
= false;
1503 for (i
= 0; i
< pane_count
; ++i
)
1505 if (panes
.Item(i
).name
== wxT("dummy"))
1507 if (panes
.Item(i
).dock_direction
== wxAUI_DOCK_CENTRE
)
1508 center_found
= true;
1510 first_good
= panes
.Item(i
).window
;
1513 if (!center_found
&& first_good
)
1515 m_mgr
.GetPane(first_good
).Centre();
1522 void wxAuiMultiNotebook::OnChildFocus(wxChildFocusEvent
& evt
)
1524 int idx
= m_tabs
.GetIdxFromWindow(evt
.GetWindow());
1525 if (idx
!= -1 && idx
!= m_curpage
)
1532 void wxAuiMultiNotebook::OnTabButton(wxCommandEvent
& command_evt
)
1534 wxAuiNotebookEvent
& evt
= (wxAuiNotebookEvent
&)command_evt
;
1535 wxAuiTabCtrl
* tabs
= (wxAuiTabCtrl
*)evt
.GetEventObject();
1537 int button_id
= evt
.GetInt();
1539 if (button_id
== wxAuiButtonClose
)
1541 int selection
= tabs
->GetActivePage();
1543 if (selection
!= -1)
1545 wxWindow
* close_wnd
= tabs
->GetWindowFromIdx(selection
);
1547 if (close_wnd
->IsKindOf(CLASSINFO(wxTabMDIChildFrame
)))
1553 int main_idx
= m_tabs
.GetIdxFromWindow(close_wnd
);
1554 DeletePage(main_idx
);