1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/aui/tabmdi.cpp
3 // Purpose: Generic MDI (Multiple Document Interface) classes
4 // Author: Hans Van Leemputten
5 // Modified by: Benjamin I. Williams / Kirix Corporation
8 // Copyright: (c) Hans Van Leemputten
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
30 #include "wx/aui/tabmdi.h"
37 #include "wx/settings.h"
40 #include "wx/stockitem.h"
50 //-----------------------------------------------------------------------------
51 // wxAuiMDIParentFrame
52 //-----------------------------------------------------------------------------
54 IMPLEMENT_DYNAMIC_CLASS(wxAuiMDIParentFrame
, wxFrame
)
56 BEGIN_EVENT_TABLE(wxAuiMDIParentFrame
, wxFrame
)
58 EVT_MENU (wxID_ANY
, wxAuiMDIParentFrame::DoHandleMenu
)
62 wxAuiMDIParentFrame::wxAuiMDIParentFrame()
67 wxAuiMDIParentFrame::wxAuiMDIParentFrame(wxWindow
*parent
,
69 const wxString
& title
,
76 (void)Create(parent
, id
, title
, pos
, size
, style
, name
);
79 wxAuiMDIParentFrame::~wxAuiMDIParentFrame()
81 // Make sure the client window is destructed before the menu bars are!
82 wxDELETE(m_pClientWindow
);
85 wxDELETE(m_pMyMenuBar
);
86 RemoveWindowMenu(GetMenuBar());
87 wxDELETE(m_pWindowMenu
);
91 bool wxAuiMDIParentFrame::Create(wxWindow
*parent
,
93 const wxString
& title
,
100 // this style can be used to prevent a window from having the standard MDI
102 if (!(style
& wxFRAME_NO_WINDOW_MENU
))
104 m_pWindowMenu
= new wxMenu
;
105 m_pWindowMenu
->Append(wxWINDOWCLOSE
, _("Cl&ose"));
106 m_pWindowMenu
->Append(wxWINDOWCLOSEALL
, _("Close All"));
107 m_pWindowMenu
->AppendSeparator();
108 m_pWindowMenu
->Append(wxWINDOWNEXT
, _("&Next"));
109 m_pWindowMenu
->Append(wxWINDOWPREV
, _("&Previous"));
111 #endif // wxUSE_MENUS
113 wxFrame::Create(parent
, id
, title
, pos
, size
, style
, name
);
119 void wxAuiMDIParentFrame::SetArtProvider(wxAuiTabArt
* provider
)
123 m_pClientWindow
->SetArtProvider(provider
);
127 wxAuiTabArt
* wxAuiMDIParentFrame::GetArtProvider()
129 if (!m_pClientWindow
)
132 return m_pClientWindow
->GetArtProvider();
135 wxAuiNotebook
* wxAuiMDIParentFrame::GetNotebook() const
137 return static_cast<wxAuiNotebook
*>(m_pClientWindow
);
143 void wxAuiMDIParentFrame::SetWindowMenu(wxMenu
* pMenu
)
145 // Replace the window menu from the currently loaded menu bar.
146 wxMenuBar
*pMenuBar
= GetMenuBar();
150 RemoveWindowMenu(pMenuBar
);
151 wxDELETE(m_pWindowMenu
);
156 m_pWindowMenu
= pMenu
;
157 AddWindowMenu(pMenuBar
);
161 void wxAuiMDIParentFrame::SetMenuBar(wxMenuBar
* pMenuBar
)
163 // Remove the Window menu from the old menu bar
164 RemoveWindowMenu(GetMenuBar());
166 // Add the Window menu to the new menu bar.
167 AddWindowMenu(pMenuBar
);
169 wxFrame::SetMenuBar(pMenuBar
);
170 //m_pMyMenuBar = GetMenuBar();
172 #endif // wxUSE_MENUS
174 void wxAuiMDIParentFrame::SetChildMenuBar(wxAuiMDIChildFrame
* pChild
)
179 // No Child, set Our menu bar back.
181 SetMenuBar(m_pMyMenuBar
);
183 SetMenuBar(GetMenuBar());
185 // Make sure we know our menu bar is in use
190 if (pChild
->GetMenuBar() == NULL
)
193 // Do we need to save the current bar?
194 if (m_pMyMenuBar
== NULL
)
195 m_pMyMenuBar
= GetMenuBar();
197 SetMenuBar(pChild
->GetMenuBar());
199 #endif // wxUSE_MENUS
202 bool wxAuiMDIParentFrame::ProcessEvent(wxEvent
& event
)
204 // stops the same event being processed repeatedly
205 if (m_pLastEvt
== &event
)
209 // let the active child (if any) process the event first.
211 if (m_pActiveChild
&&
212 event
.IsCommandEvent() &&
213 event
.GetEventObject() != m_pClientWindow
&&
214 !(event
.GetEventType() == wxEVT_ACTIVATE
||
215 event
.GetEventType() == wxEVT_SET_FOCUS
||
216 event
.GetEventType() == wxEVT_KILL_FOCUS
||
217 event
.GetEventType() == wxEVT_CHILD_FOCUS
||
218 event
.GetEventType() == wxEVT_COMMAND_SET_FOCUS
||
219 event
.GetEventType() == wxEVT_COMMAND_KILL_FOCUS
)
222 res
= m_pActiveChild
->GetEventHandler()->ProcessEvent(event
);
227 // if the event was not handled this frame will handle it,
228 // which is why we need the protection code at the beginning
230 res
= wxEvtHandler::ProcessEvent(event
);
238 wxAuiMDIChildFrame
*wxAuiMDIParentFrame::GetActiveChild() const
240 return m_pActiveChild
;
243 void wxAuiMDIParentFrame::SetActiveChild(wxAuiMDIChildFrame
* pChildFrame
)
245 m_pActiveChild
= pChildFrame
;
248 wxAuiMDIClientWindow
*wxAuiMDIParentFrame::GetClientWindow() const
250 return m_pClientWindow
;
253 wxAuiMDIClientWindow
*wxAuiMDIParentFrame::OnCreateClient()
255 m_pClientWindow
= new wxAuiMDIClientWindow( this );
256 return m_pClientWindow
;
259 void wxAuiMDIParentFrame::ActivateNext()
261 if (m_pClientWindow
&& m_pClientWindow
->GetSelection() != wxNOT_FOUND
)
263 size_t active
= m_pClientWindow
->GetSelection() + 1;
264 if (active
>= m_pClientWindow
->GetPageCount())
267 m_pClientWindow
->SetSelection(active
);
271 void wxAuiMDIParentFrame::ActivatePrevious()
273 if (m_pClientWindow
&& m_pClientWindow
->GetSelection() != wxNOT_FOUND
)
275 int active
= m_pClientWindow
->GetSelection() - 1;
277 active
= m_pClientWindow
->GetPageCount() - 1;
279 m_pClientWindow
->SetSelection(active
);
283 void wxAuiMDIParentFrame::Init()
286 m_pClientWindow
= NULL
;
287 m_pActiveChild
= NULL
;
289 m_pWindowMenu
= NULL
;
291 #endif // wxUSE_MENUS
295 void wxAuiMDIParentFrame::RemoveWindowMenu(wxMenuBar
* pMenuBar
)
297 if (pMenuBar
&& m_pWindowMenu
)
299 // Remove old window menu
300 int pos
= pMenuBar
->FindMenu(_("&Window"));
301 if (pos
!= wxNOT_FOUND
)
303 // DBG:: We're going to delete the wrong menu!!!
304 wxASSERT(m_pWindowMenu
== pMenuBar
->GetMenu(pos
));
305 pMenuBar
->Remove(pos
);
310 void wxAuiMDIParentFrame::AddWindowMenu(wxMenuBar
*pMenuBar
)
312 if (pMenuBar
&& m_pWindowMenu
)
314 int pos
= pMenuBar
->FindMenu(wxGetStockLabel(wxID_HELP
,wxSTOCK_NOFLAGS
));
315 if (pos
== wxNOT_FOUND
)
316 pMenuBar
->Append(m_pWindowMenu
, _("&Window"));
318 pMenuBar
->Insert(pos
, m_pWindowMenu
, _("&Window"));
322 void wxAuiMDIParentFrame::DoHandleMenu(wxCommandEvent
& event
)
324 switch (event
.GetId())
328 m_pActiveChild
->Close();
330 case wxWINDOWCLOSEALL
:
331 while (m_pActiveChild
)
333 if (!m_pActiveChild
->Close())
349 #endif // wxUSE_MENUS
351 void wxAuiMDIParentFrame::DoGetClientSize(int* width
, int* height
) const
353 wxFrame::DoGetClientSize(width
, height
);
356 void wxAuiMDIParentFrame::Tile(wxOrientation orient
)
358 wxAuiMDIClientWindow
* client_window
= GetClientWindow();
359 wxASSERT_MSG(client_window
, wxT("Missing MDI Client Window"));
361 int cur_idx
= client_window
->GetSelection();
365 if (orient
== wxVERTICAL
)
367 client_window
->Split(cur_idx
, wxLEFT
);
369 else if (orient
== wxHORIZONTAL
)
371 client_window
->Split(cur_idx
, wxTOP
);
376 //-----------------------------------------------------------------------------
377 // wxAuiMDIChildFrame
378 //-----------------------------------------------------------------------------
380 IMPLEMENT_DYNAMIC_CLASS(wxAuiMDIChildFrame
, wxPanel
)
382 BEGIN_EVENT_TABLE(wxAuiMDIChildFrame
, wxPanel
)
383 EVT_MENU_HIGHLIGHT_ALL(wxAuiMDIChildFrame::OnMenuHighlight
)
384 EVT_ACTIVATE(wxAuiMDIChildFrame::OnActivate
)
385 EVT_CLOSE(wxAuiMDIChildFrame::OnCloseWindow
)
388 wxAuiMDIChildFrame::wxAuiMDIChildFrame()
393 wxAuiMDIChildFrame::wxAuiMDIChildFrame(wxAuiMDIParentFrame
*parent
,
395 const wxString
& title
,
396 const wxPoint
& WXUNUSED(pos
),
399 const wxString
& name
)
403 // There are two ways to create an tabbed mdi child fram without
404 // making it the active document. Either Show(false) can be called
405 // before Create() (as is customary on some ports with wxFrame-type
406 // windows), or wxMINIMIZE can be passed in the style flags. Note that
407 // wxAuiMDIChildFrame is not really derived from wxFrame, as wxMDIChildFrame
408 // is, but those are the expected symantics. No style flag is passed
409 // onto the panel underneath.
410 if (style
& wxMINIMIZE
)
411 m_activate_on_create
= false;
413 Create(parent
, id
, title
, wxDefaultPosition
, size
, 0, name
);
416 wxAuiMDIChildFrame::~wxAuiMDIChildFrame()
418 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
419 if (pParentFrame
&& pParentFrame
->GetActiveChild() == this)
421 pParentFrame
->SetActiveChild(NULL
);
422 pParentFrame
->SetChildMenuBar(NULL
);
426 wxDELETE(m_pMenuBar
);
427 #endif // wxUSE_MENUS
430 bool wxAuiMDIChildFrame::Create(wxAuiMDIParentFrame
* parent
,
432 const wxString
& title
,
433 const wxPoint
& WXUNUSED(pos
),
436 const wxString
& name
)
438 wxAuiMDIClientWindow
* pClientWindow
= parent
->GetClientWindow();
439 wxASSERT_MSG((pClientWindow
!= (wxWindow
*) NULL
), wxT("Missing MDI client window."));
441 // see comment in constructor
442 if (style
& wxMINIMIZE
)
443 m_activate_on_create
= false;
445 wxSize cli_size
= pClientWindow
->GetClientSize();
447 // create the window off-screen to prevent flicker
448 wxPanel::Create(pClientWindow
,
450 wxPoint(cli_size
.x
+1, cli_size
.y
+1),
456 SetMDIParentFrame(parent
);
458 // this is the currently active child
459 parent
->SetActiveChild(this);
463 pClientWindow
->AddPage(this, title
, m_activate_on_create
);
464 pClientWindow
->Refresh();
469 bool wxAuiMDIChildFrame::Destroy()
471 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
472 wxASSERT_MSG(pParentFrame
, wxT("Missing MDI Parent Frame"));
474 wxAuiMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
475 wxASSERT_MSG(pClientWindow
, wxT("Missing MDI Client Window"));
477 if (pParentFrame
->GetActiveChild() == this)
479 // deactivate ourself
480 wxActivateEvent
event(wxEVT_ACTIVATE
, false, GetId());
481 event
.SetEventObject(this);
482 GetEventHandler()->ProcessEvent(event
);
484 pParentFrame
->SetActiveChild(NULL
);
485 pParentFrame
->SetChildMenuBar(NULL
);
488 size_t page_count
= pClientWindow
->GetPageCount();
489 for (size_t pos
= 0; pos
< page_count
; pos
++)
491 if (pClientWindow
->GetPage(pos
) == this)
492 return pClientWindow
->DeletePage(pos
);
499 void wxAuiMDIChildFrame::SetMenuBar(wxMenuBar
*menu_bar
)
501 wxMenuBar
*pOldMenuBar
= m_pMenuBar
;
502 m_pMenuBar
= menu_bar
;
506 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
507 wxASSERT_MSG(pParentFrame
, wxT("Missing MDI Parent Frame"));
509 m_pMenuBar
->SetParent(pParentFrame
);
510 if (pParentFrame
->GetActiveChild() == this)
512 // replace current menu bars
514 pParentFrame
->SetChildMenuBar(NULL
);
515 pParentFrame
->SetChildMenuBar(this);
520 wxMenuBar
*wxAuiMDIChildFrame::GetMenuBar() const
524 #endif // wxUSE_MENUS
526 void wxAuiMDIChildFrame::SetTitle(const wxString
& title
)
530 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
531 wxASSERT_MSG(pParentFrame
, wxT("Missing MDI Parent Frame"));
533 wxAuiMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
534 if (pClientWindow
!= NULL
)
537 for (pos
= 0; pos
< pClientWindow
->GetPageCount(); pos
++)
539 if (pClientWindow
->GetPage(pos
) == this)
541 pClientWindow
->SetPageText(pos
, m_title
);
548 wxString
wxAuiMDIChildFrame::GetTitle() const
553 void wxAuiMDIChildFrame::SetIcons(const wxIconBundle
& icons
)
555 // get icon with the system icon size
556 SetIcon(icons
.GetIcon(-1));
557 m_icon_bundle
= icons
;
560 const wxIconBundle
& wxAuiMDIChildFrame::GetIcons() const
562 return m_icon_bundle
;
565 void wxAuiMDIChildFrame::SetIcon(const wxIcon
& icon
)
567 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
568 wxASSERT_MSG(pParentFrame
, wxT("Missing MDI Parent Frame"));
573 bmp
.CopyFromIcon(m_icon
);
575 wxAuiMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
576 if (pClientWindow
!= NULL
)
578 int idx
= pClientWindow
->GetPageIndex(this);
582 pClientWindow
->SetPageBitmap((size_t)idx
, bmp
);
587 const wxIcon
& wxAuiMDIChildFrame::GetIcon() const
593 void wxAuiMDIChildFrame::Activate()
595 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
596 wxASSERT_MSG(pParentFrame
, wxT("Missing MDI Parent Frame"));
598 wxAuiMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
600 if (pClientWindow
!= NULL
)
603 for (pos
= 0; pos
< pClientWindow
->GetPageCount(); pos
++)
605 if (pClientWindow
->GetPage(pos
) == this)
607 pClientWindow
->SetSelection(pos
);
614 void wxAuiMDIChildFrame::OnMenuHighlight(wxMenuEvent
& event
)
617 if (m_pMDIParentFrame
)
619 // we don't have any help text for this item,
620 // but may be the MDI frame does?
621 m_pMDIParentFrame
->OnMenuHighlight(event
);
625 #endif // wxUSE_STATUSBAR
628 void wxAuiMDIChildFrame::OnActivate(wxActivateEvent
& WXUNUSED(event
))
633 void wxAuiMDIChildFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
638 void wxAuiMDIChildFrame::SetMDIParentFrame(wxAuiMDIParentFrame
* parentFrame
)
640 m_pMDIParentFrame
= parentFrame
;
643 wxAuiMDIParentFrame
* wxAuiMDIChildFrame::GetMDIParentFrame() const
645 return m_pMDIParentFrame
;
648 void wxAuiMDIChildFrame::Init()
650 m_activate_on_create
= true;
651 m_pMDIParentFrame
= NULL
;
654 #endif // wxUSE_MENUS
657 bool wxAuiMDIChildFrame::Show(bool show
)
659 m_activate_on_create
= show
;
665 void wxAuiMDIChildFrame::DoShow(bool show
)
667 wxWindow::Show(show
);
670 void wxAuiMDIChildFrame::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
672 m_mdi_newrect
= wxRect(x
, y
, width
, height
);
674 wxPanel::DoSetSize(x
,y
,width
, height
, sizeFlags
);
676 wxUnusedVar(sizeFlags
);
680 void wxAuiMDIChildFrame::DoMoveWindow(int x
, int y
, int width
, int height
)
682 m_mdi_newrect
= wxRect(x
, y
, width
, height
);
685 void wxAuiMDIChildFrame::ApplyMDIChildFrameRect()
687 if (m_mdi_currect
!= m_mdi_newrect
)
689 wxPanel::DoMoveWindow(m_mdi_newrect
.x
, m_mdi_newrect
.y
,
690 m_mdi_newrect
.width
, m_mdi_newrect
.height
);
691 m_mdi_currect
= m_mdi_newrect
;
696 //-----------------------------------------------------------------------------
697 // wxAuiMDIClientWindow
698 //-----------------------------------------------------------------------------
700 IMPLEMENT_DYNAMIC_CLASS(wxAuiMDIClientWindow
, wxAuiNotebook
)
702 BEGIN_EVENT_TABLE(wxAuiMDIClientWindow
, wxAuiNotebook
)
703 EVT_AUINOTEBOOK_PAGE_CHANGED(wxID_ANY
, wxAuiMDIClientWindow::OnPageChanged
)
704 EVT_AUINOTEBOOK_PAGE_CLOSE(wxID_ANY
, wxAuiMDIClientWindow::OnPageClose
)
705 EVT_SIZE(wxAuiMDIClientWindow::OnSize
)
708 wxAuiMDIClientWindow::wxAuiMDIClientWindow()
712 wxAuiMDIClientWindow::wxAuiMDIClientWindow(wxAuiMDIParentFrame
* parent
, long style
)
714 CreateClient(parent
, style
);
717 wxAuiMDIClientWindow::~wxAuiMDIClientWindow()
722 bool wxAuiMDIClientWindow::CreateClient(wxAuiMDIParentFrame
* parent
, long style
)
724 SetWindowStyleFlag(style
);
726 wxSize caption_icon_size
=
727 wxSize(wxSystemSettings::GetMetric(wxSYS_SMALLICON_X
),
728 wxSystemSettings::GetMetric(wxSYS_SMALLICON_Y
));
729 SetUniformBitmapSize(caption_icon_size
);
731 if (!wxAuiNotebook::Create(parent
,
735 wxAUI_NB_DEFAULT_STYLE
| wxNO_BORDER
))
740 wxColour bkcolour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
741 SetOwnBackgroundColour(bkcolour
);
743 m_mgr
.GetArtProvider()->SetColour(wxAUI_DOCKART_BACKGROUND_COLOUR
, bkcolour
);
748 int wxAuiMDIClientWindow::SetSelection(size_t nPage
)
750 return wxAuiNotebook::SetSelection(nPage
);
753 void wxAuiMDIClientWindow::PageChanged(int old_selection
, int new_selection
)
755 // don't do anything if the page doesn't actually change
756 if (old_selection
== new_selection
)
760 // don't do anything if the new page is already active
761 if (new_selection != -1)
763 wxAuiMDIChildFrame* child = (wxAuiMDIChildFrame*)GetPage(new_selection);
764 if (child->GetMDIParentFrame()->GetActiveChild() == child)
769 // notify old active child that it has been deactivated
770 if (old_selection
!= -1)
772 wxAuiMDIChildFrame
* old_child
= (wxAuiMDIChildFrame
*)GetPage(old_selection
);
773 wxASSERT_MSG(old_child
, wxT("wxAuiMDIClientWindow::PageChanged - null page pointer"));
775 wxActivateEvent
event(wxEVT_ACTIVATE
, false, old_child
->GetId());
776 event
.SetEventObject(old_child
);
777 old_child
->GetEventHandler()->ProcessEvent(event
);
780 // notify new active child that it has been activated
781 if (new_selection
!= -1)
783 wxAuiMDIChildFrame
* active_child
= (wxAuiMDIChildFrame
*)GetPage(new_selection
);
784 wxASSERT_MSG(active_child
, wxT("wxAuiMDIClientWindow::PageChanged - null page pointer"));
786 wxActivateEvent
event(wxEVT_ACTIVATE
, true, active_child
->GetId());
787 event
.SetEventObject(active_child
);
788 active_child
->GetEventHandler()->ProcessEvent(event
);
790 if (active_child
->GetMDIParentFrame())
792 active_child
->GetMDIParentFrame()->SetActiveChild(active_child
);
793 active_child
->GetMDIParentFrame()->SetChildMenuBar(active_child
);
800 void wxAuiMDIClientWindow::OnPageClose(wxAuiNotebookEvent
& evt
)
802 wxAuiMDIChildFrame
* wnd
;
803 wnd
= static_cast<wxAuiMDIChildFrame
*>(GetPage(evt
.GetSelection()));
807 // regardless of the result of wnd->Close(), we've
808 // already taken care of the close operations, so
809 // suppress further processing
813 void wxAuiMDIClientWindow::OnPageChanged(wxAuiNotebookEvent
& evt
)
815 PageChanged(evt
.GetOldSelection(), evt
.GetSelection());
818 void wxAuiMDIClientWindow::OnSize(wxSizeEvent
& evt
)
820 wxAuiNotebook::OnSize(evt
);
822 for (size_t pos
= 0; pos
< GetPageCount(); pos
++)
823 ((wxAuiMDIChildFrame
*)GetPage(pos
))->ApplyMDIChildFrameRect();