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
)
59 EVT_UPDATE_UI (wxID_ANY
, wxAuiMDIParentFrame::DoHandleUpdateUI
)
63 wxAuiMDIParentFrame::wxAuiMDIParentFrame()
68 wxAuiMDIParentFrame::wxAuiMDIParentFrame(wxWindow
*parent
,
70 const wxString
& title
,
77 (void)Create(parent
, id
, title
, pos
, size
, style
, name
);
80 wxAuiMDIParentFrame::~wxAuiMDIParentFrame()
82 // Make sure the client window is destructed before the menu bars are!
83 wxDELETE(m_pClientWindow
);
86 wxDELETE(m_pMyMenuBar
);
87 RemoveWindowMenu(GetMenuBar());
88 wxDELETE(m_pWindowMenu
);
92 bool wxAuiMDIParentFrame::Create(wxWindow
*parent
,
94 const wxString
& title
,
101 // this style can be used to prevent a window from having the standard MDI
103 if (!(style
& wxFRAME_NO_WINDOW_MENU
))
105 m_pWindowMenu
= new wxMenu
;
106 m_pWindowMenu
->Append(wxWINDOWCLOSE
, _("Cl&ose"));
107 m_pWindowMenu
->Append(wxWINDOWCLOSEALL
, _("Close All"));
108 m_pWindowMenu
->AppendSeparator();
109 m_pWindowMenu
->Append(wxWINDOWNEXT
, _("&Next"));
110 m_pWindowMenu
->Append(wxWINDOWPREV
, _("&Previous"));
112 #endif // wxUSE_MENUS
114 if ( !wxFrame::Create(parent
, id
, title
, pos
, size
, style
, name
) )
117 m_pClientWindow
= OnCreateClient();
118 return m_pClientWindow
!= NULL
;
122 void wxAuiMDIParentFrame::SetArtProvider(wxAuiTabArt
* provider
)
126 m_pClientWindow
->SetArtProvider(provider
);
130 wxAuiTabArt
* wxAuiMDIParentFrame::GetArtProvider()
132 if (!m_pClientWindow
)
135 return m_pClientWindow
->GetArtProvider();
138 wxAuiNotebook
* wxAuiMDIParentFrame::GetNotebook() const
140 return static_cast<wxAuiNotebook
*>(m_pClientWindow
);
146 void wxAuiMDIParentFrame::SetWindowMenu(wxMenu
* pMenu
)
148 // Replace the window menu from the currently loaded menu bar.
149 wxMenuBar
*pMenuBar
= GetMenuBar();
153 RemoveWindowMenu(pMenuBar
);
154 wxDELETE(m_pWindowMenu
);
159 m_pWindowMenu
= pMenu
;
160 AddWindowMenu(pMenuBar
);
164 void wxAuiMDIParentFrame::SetMenuBar(wxMenuBar
* pMenuBar
)
166 // Remove the Window menu from the old menu bar
167 RemoveWindowMenu(GetMenuBar());
169 // Add the Window menu to the new menu bar.
170 AddWindowMenu(pMenuBar
);
172 wxFrame::SetMenuBar(pMenuBar
);
173 //m_pMyMenuBar = GetMenuBar();
175 #endif // wxUSE_MENUS
177 void wxAuiMDIParentFrame::SetChildMenuBar(wxAuiMDIChildFrame
* pChild
)
182 // No Child, set Our menu bar back.
184 SetMenuBar(m_pMyMenuBar
);
186 SetMenuBar(GetMenuBar());
188 // Make sure we know our menu bar is in use
193 if (pChild
->GetMenuBar() == NULL
)
196 // Do we need to save the current bar?
197 if (m_pMyMenuBar
== NULL
)
198 m_pMyMenuBar
= GetMenuBar();
200 SetMenuBar(pChild
->GetMenuBar());
202 #endif // wxUSE_MENUS
205 bool wxAuiMDIParentFrame::ProcessEvent(wxEvent
& event
)
207 // stops the same event being processed repeatedly
208 if (m_pLastEvt
== &event
)
212 // let the active child (if any) process the event first.
214 if (m_pActiveChild
&&
215 event
.IsCommandEvent() &&
216 event
.GetEventObject() != m_pClientWindow
&&
217 !(event
.GetEventType() == wxEVT_ACTIVATE
||
218 event
.GetEventType() == wxEVT_SET_FOCUS
||
219 event
.GetEventType() == wxEVT_KILL_FOCUS
||
220 event
.GetEventType() == wxEVT_CHILD_FOCUS
||
221 event
.GetEventType() == wxEVT_COMMAND_SET_FOCUS
||
222 event
.GetEventType() == wxEVT_COMMAND_KILL_FOCUS
)
225 res
= m_pActiveChild
->GetEventHandler()->ProcessEvent(event
);
230 // if the event was not handled this frame will handle it,
231 // which is why we need the protection code at the beginning
233 res
= wxEvtHandler::ProcessEvent(event
);
241 wxAuiMDIChildFrame
*wxAuiMDIParentFrame::GetActiveChild() const
243 return m_pActiveChild
;
246 void wxAuiMDIParentFrame::SetActiveChild(wxAuiMDIChildFrame
* pChildFrame
)
248 m_pActiveChild
= pChildFrame
;
251 wxAuiMDIClientWindow
*wxAuiMDIParentFrame::GetClientWindow() const
253 return m_pClientWindow
;
256 wxAuiMDIClientWindow
*wxAuiMDIParentFrame::OnCreateClient()
258 return new wxAuiMDIClientWindow( this );
261 void wxAuiMDIParentFrame::ActivateNext()
263 if (m_pClientWindow
&& m_pClientWindow
->GetSelection() != wxNOT_FOUND
)
265 size_t active
= m_pClientWindow
->GetSelection() + 1;
266 if (active
>= m_pClientWindow
->GetPageCount())
269 m_pClientWindow
->SetSelection(active
);
273 void wxAuiMDIParentFrame::ActivatePrevious()
275 if (m_pClientWindow
&& m_pClientWindow
->GetSelection() != wxNOT_FOUND
)
277 int active
= m_pClientWindow
->GetSelection() - 1;
279 active
= m_pClientWindow
->GetPageCount() - 1;
281 m_pClientWindow
->SetSelection(active
);
285 void wxAuiMDIParentFrame::Init()
288 m_pClientWindow
= NULL
;
289 m_pActiveChild
= NULL
;
291 m_pWindowMenu
= NULL
;
293 #endif // wxUSE_MENUS
297 void wxAuiMDIParentFrame::RemoveWindowMenu(wxMenuBar
* pMenuBar
)
299 if (pMenuBar
&& m_pWindowMenu
)
301 // Remove old window menu
302 int pos
= pMenuBar
->FindMenu(_("&Window"));
303 if (pos
!= wxNOT_FOUND
)
305 // DBG:: We're going to delete the wrong menu!!!
306 wxASSERT(m_pWindowMenu
== pMenuBar
->GetMenu(pos
));
307 pMenuBar
->Remove(pos
);
312 void wxAuiMDIParentFrame::AddWindowMenu(wxMenuBar
*pMenuBar
)
314 if (pMenuBar
&& m_pWindowMenu
)
316 int pos
= pMenuBar
->FindMenu(wxGetStockLabel(wxID_HELP
,wxSTOCK_NOFLAGS
));
317 if (pos
== wxNOT_FOUND
)
318 pMenuBar
->Append(m_pWindowMenu
, _("&Window"));
320 pMenuBar
->Insert(pos
, m_pWindowMenu
, _("&Window"));
324 void wxAuiMDIParentFrame::DoHandleMenu(wxCommandEvent
& event
)
326 switch (event
.GetId())
330 m_pActiveChild
->Close();
332 case wxWINDOWCLOSEALL
:
333 while (m_pActiveChild
)
335 if (!m_pActiveChild
->Close())
352 void wxAuiMDIParentFrame::DoHandleUpdateUI(wxUpdateUIEvent
& event
)
354 switch (event
.GetId())
357 case wxWINDOWCLOSEALL
:
359 wxAuiMDIClientWindow
* client_window
= GetClientWindow();
360 wxCHECK_RET(client_window
, wxS("Missing MDI Client Window"));
361 size_t pages
= client_window
->GetPageCount();
362 event
.Enable(pages
>= 1);
369 wxAuiMDIClientWindow
* client_window
= GetClientWindow();
370 wxCHECK_RET(client_window
, wxS("Missing MDI Client Window"));
371 size_t pages
= client_window
->GetPageCount();
372 event
.Enable(pages
>= 2);
380 #endif // wxUSE_MENUS
382 void wxAuiMDIParentFrame::DoGetClientSize(int* width
, int* height
) const
384 wxFrame::DoGetClientSize(width
, height
);
387 void wxAuiMDIParentFrame::Tile(wxOrientation orient
)
389 wxAuiMDIClientWindow
* client_window
= GetClientWindow();
390 wxASSERT_MSG(client_window
, wxT("Missing MDI Client Window"));
392 int cur_idx
= client_window
->GetSelection();
396 if (orient
== wxVERTICAL
)
398 client_window
->Split(cur_idx
, wxLEFT
);
400 else if (orient
== wxHORIZONTAL
)
402 client_window
->Split(cur_idx
, wxTOP
);
407 //-----------------------------------------------------------------------------
408 // wxAuiMDIChildFrame
409 //-----------------------------------------------------------------------------
411 IMPLEMENT_DYNAMIC_CLASS(wxAuiMDIChildFrame
, wxPanel
)
413 BEGIN_EVENT_TABLE(wxAuiMDIChildFrame
, wxPanel
)
414 EVT_MENU_HIGHLIGHT_ALL(wxAuiMDIChildFrame::OnMenuHighlight
)
415 EVT_ACTIVATE(wxAuiMDIChildFrame::OnActivate
)
416 EVT_CLOSE(wxAuiMDIChildFrame::OnCloseWindow
)
419 wxAuiMDIChildFrame::wxAuiMDIChildFrame()
424 wxAuiMDIChildFrame::wxAuiMDIChildFrame(wxAuiMDIParentFrame
*parent
,
426 const wxString
& title
,
427 const wxPoint
& WXUNUSED(pos
),
430 const wxString
& name
)
434 // There are two ways to create an tabbed mdi child fram without
435 // making it the active document. Either Show(false) can be called
436 // before Create() (as is customary on some ports with wxFrame-type
437 // windows), or wxMINIMIZE can be passed in the style flags. Note that
438 // wxAuiMDIChildFrame is not really derived from wxFrame, as wxMDIChildFrame
439 // is, but those are the expected symantics. No style flag is passed
440 // onto the panel underneath.
441 if (style
& wxMINIMIZE
)
442 m_activateOnCreate
= false;
444 Create(parent
, id
, title
, wxDefaultPosition
, size
, 0, name
);
447 wxAuiMDIChildFrame::~wxAuiMDIChildFrame()
449 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
452 if (pParentFrame
->GetActiveChild() == this)
454 pParentFrame
->SetActiveChild(NULL
);
455 pParentFrame
->SetChildMenuBar(NULL
);
457 wxAuiMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
458 wxASSERT(pClientWindow
);
459 int idx
= pClientWindow
->GetPageIndex(this);
460 if (idx
!= wxNOT_FOUND
)
462 pClientWindow
->RemovePage(idx
);
467 wxDELETE(m_pMenuBar
);
468 #endif // wxUSE_MENUS
471 bool wxAuiMDIChildFrame::Create(wxAuiMDIParentFrame
* parent
,
473 const wxString
& title
,
474 const wxPoint
& WXUNUSED(pos
),
477 const wxString
& name
)
479 wxAuiMDIClientWindow
* pClientWindow
= parent
->GetClientWindow();
480 wxASSERT_MSG((pClientWindow
!= NULL
), wxT("Missing MDI client window."));
482 // see comment in constructor
483 if (style
& wxMINIMIZE
)
484 m_activateOnCreate
= false;
486 wxSize cli_size
= pClientWindow
->GetClientSize();
488 // create the window off-screen to prevent flicker
489 wxPanel::Create(pClientWindow
,
491 wxPoint(cli_size
.x
+1, cli_size
.y
+1),
497 SetMDIParentFrame(parent
);
499 // this is the currently active child
500 parent
->SetActiveChild(this);
504 pClientWindow
->AddPage(this, title
, m_activateOnCreate
);
505 pClientWindow
->Refresh();
510 bool wxAuiMDIChildFrame::Destroy()
512 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
513 wxASSERT_MSG(pParentFrame
, wxT("Missing MDI Parent Frame"));
515 wxAuiMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
516 wxASSERT_MSG(pClientWindow
, wxT("Missing MDI Client Window"));
518 if (pParentFrame
->GetActiveChild() == this)
520 // deactivate ourself
521 wxActivateEvent
event(wxEVT_ACTIVATE
, false, GetId());
522 event
.SetEventObject(this);
523 GetEventHandler()->ProcessEvent(event
);
525 pParentFrame
->SetActiveChild(NULL
);
526 pParentFrame
->SetChildMenuBar(NULL
);
529 size_t page_count
= pClientWindow
->GetPageCount();
530 for (size_t pos
= 0; pos
< page_count
; pos
++)
532 if (pClientWindow
->GetPage(pos
) == this)
533 return pClientWindow
->DeletePage(pos
);
540 void wxAuiMDIChildFrame::SetMenuBar(wxMenuBar
*menu_bar
)
542 wxMenuBar
*pOldMenuBar
= m_pMenuBar
;
543 m_pMenuBar
= menu_bar
;
547 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
548 wxASSERT_MSG(pParentFrame
, wxT("Missing MDI Parent Frame"));
550 m_pMenuBar
->SetParent(pParentFrame
);
551 if (pParentFrame
->GetActiveChild() == this)
553 // replace current menu bars
555 pParentFrame
->SetChildMenuBar(NULL
);
556 pParentFrame
->SetChildMenuBar(this);
561 wxMenuBar
*wxAuiMDIChildFrame::GetMenuBar() const
565 #endif // wxUSE_MENUS
567 void wxAuiMDIChildFrame::SetTitle(const wxString
& title
)
571 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
572 wxASSERT_MSG(pParentFrame
, wxT("Missing MDI Parent Frame"));
574 wxAuiMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
575 if (pClientWindow
!= NULL
)
578 for (pos
= 0; pos
< pClientWindow
->GetPageCount(); pos
++)
580 if (pClientWindow
->GetPage(pos
) == this)
582 pClientWindow
->SetPageText(pos
, m_title
);
589 wxString
wxAuiMDIChildFrame::GetTitle() const
594 void wxAuiMDIChildFrame::SetIcons(const wxIconBundle
& icons
)
596 // get icon with the system icon size
597 SetIcon(icons
.GetIcon(-1));
598 m_iconBundle
= icons
;
601 const wxIconBundle
& wxAuiMDIChildFrame::GetIcons() const
606 void wxAuiMDIChildFrame::SetIcon(const wxIcon
& icon
)
608 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
609 wxASSERT_MSG(pParentFrame
, wxT("Missing MDI Parent Frame"));
614 bmp
.CopyFromIcon(m_icon
);
616 wxAuiMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
617 if (pClientWindow
!= NULL
)
619 int idx
= pClientWindow
->GetPageIndex(this);
623 pClientWindow
->SetPageBitmap((size_t)idx
, bmp
);
628 const wxIcon
& wxAuiMDIChildFrame::GetIcon() const
634 void wxAuiMDIChildFrame::Activate()
636 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
637 wxASSERT_MSG(pParentFrame
, wxT("Missing MDI Parent Frame"));
639 wxAuiMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
641 if (pClientWindow
!= NULL
)
644 for (pos
= 0; pos
< pClientWindow
->GetPageCount(); pos
++)
646 if (pClientWindow
->GetPage(pos
) == this)
648 pClientWindow
->SetSelection(pos
);
655 void wxAuiMDIChildFrame::OnMenuHighlight(wxMenuEvent
& event
)
658 if (m_pMDIParentFrame
)
660 // we don't have any help text for this item,
661 // but may be the MDI frame does?
662 m_pMDIParentFrame
->OnMenuHighlight(event
);
666 #endif // wxUSE_STATUSBAR
669 void wxAuiMDIChildFrame::OnActivate(wxActivateEvent
& WXUNUSED(event
))
674 void wxAuiMDIChildFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
679 void wxAuiMDIChildFrame::SetMDIParentFrame(wxAuiMDIParentFrame
* parentFrame
)
681 m_pMDIParentFrame
= parentFrame
;
684 wxAuiMDIParentFrame
* wxAuiMDIChildFrame::GetMDIParentFrame() const
686 return m_pMDIParentFrame
;
689 void wxAuiMDIChildFrame::Init()
691 m_activateOnCreate
= true;
692 m_pMDIParentFrame
= NULL
;
695 #endif // wxUSE_MENUS
698 bool wxAuiMDIChildFrame::Show(bool show
)
700 m_activateOnCreate
= show
;
706 void wxAuiMDIChildFrame::DoShow(bool show
)
708 wxWindow::Show(show
);
711 void wxAuiMDIChildFrame::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
713 m_mdiNewRect
= wxRect(x
, y
, width
, height
);
715 wxPanel::DoSetSize(x
,y
,width
, height
, sizeFlags
);
717 wxUnusedVar(sizeFlags
);
721 void wxAuiMDIChildFrame::DoMoveWindow(int x
, int y
, int width
, int height
)
723 m_mdiNewRect
= wxRect(x
, y
, width
, height
);
726 void wxAuiMDIChildFrame::ApplyMDIChildFrameRect()
728 if (m_mdiCurRect
!= m_mdiNewRect
)
730 wxPanel::DoMoveWindow(m_mdiNewRect
.x
, m_mdiNewRect
.y
,
731 m_mdiNewRect
.width
, m_mdiNewRect
.height
);
732 m_mdiCurRect
= m_mdiNewRect
;
737 //-----------------------------------------------------------------------------
738 // wxAuiMDIClientWindow
739 //-----------------------------------------------------------------------------
741 IMPLEMENT_DYNAMIC_CLASS(wxAuiMDIClientWindow
, wxAuiNotebook
)
743 BEGIN_EVENT_TABLE(wxAuiMDIClientWindow
, wxAuiNotebook
)
744 EVT_AUINOTEBOOK_PAGE_CHANGED(wxID_ANY
, wxAuiMDIClientWindow::OnPageChanged
)
745 EVT_AUINOTEBOOK_PAGE_CLOSE(wxID_ANY
, wxAuiMDIClientWindow::OnPageClose
)
746 EVT_SIZE(wxAuiMDIClientWindow::OnSize
)
749 wxAuiMDIClientWindow::wxAuiMDIClientWindow()
753 wxAuiMDIClientWindow::wxAuiMDIClientWindow(wxAuiMDIParentFrame
* parent
, long style
)
755 CreateClient(parent
, style
);
758 bool wxAuiMDIClientWindow::CreateClient(wxAuiMDIParentFrame
* parent
, long style
)
760 SetWindowStyleFlag(style
);
762 wxSize caption_icon_size
=
763 wxSize(wxSystemSettings::GetMetric(wxSYS_SMALLICON_X
),
764 wxSystemSettings::GetMetric(wxSYS_SMALLICON_Y
));
765 SetUniformBitmapSize(caption_icon_size
);
767 if (!wxAuiNotebook::Create(parent
,
771 wxAUI_NB_DEFAULT_STYLE
| wxNO_BORDER
))
776 wxColour bkcolour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
777 SetOwnBackgroundColour(bkcolour
);
779 m_mgr
.GetArtProvider()->SetColour(wxAUI_DOCKART_BACKGROUND_COLOUR
, bkcolour
);
784 int wxAuiMDIClientWindow::SetSelection(size_t nPage
)
786 return wxAuiNotebook::SetSelection(nPage
);
789 void wxAuiMDIClientWindow::PageChanged(int old_selection
, int new_selection
)
791 // don't do anything if the page doesn't actually change
792 if (old_selection
== new_selection
)
796 // don't do anything if the new page is already active
797 if (new_selection != -1)
799 wxAuiMDIChildFrame* child = (wxAuiMDIChildFrame*)GetPage(new_selection);
800 if (child->GetMDIParentFrame()->GetActiveChild() == child)
805 // notify old active child that it has been deactivated
806 if ((old_selection
!= -1) && (old_selection
< (int)GetPageCount()))
808 wxAuiMDIChildFrame
* old_child
= (wxAuiMDIChildFrame
*)GetPage(old_selection
);
809 wxASSERT_MSG(old_child
, wxT("wxAuiMDIClientWindow::PageChanged - null page pointer"));
811 wxActivateEvent
event(wxEVT_ACTIVATE
, false, old_child
->GetId());
812 event
.SetEventObject(old_child
);
813 old_child
->GetEventHandler()->ProcessEvent(event
);
816 // notify new active child that it has been activated
817 if (new_selection
!= -1)
819 wxAuiMDIChildFrame
* active_child
= (wxAuiMDIChildFrame
*)GetPage(new_selection
);
820 wxASSERT_MSG(active_child
, wxT("wxAuiMDIClientWindow::PageChanged - null page pointer"));
822 wxActivateEvent
event(wxEVT_ACTIVATE
, true, active_child
->GetId());
823 event
.SetEventObject(active_child
);
824 active_child
->GetEventHandler()->ProcessEvent(event
);
826 if (active_child
->GetMDIParentFrame())
828 active_child
->GetMDIParentFrame()->SetActiveChild(active_child
);
829 active_child
->GetMDIParentFrame()->SetChildMenuBar(active_child
);
836 void wxAuiMDIClientWindow::OnPageClose(wxAuiNotebookEvent
& evt
)
838 wxAuiMDIChildFrame
* wnd
;
839 wnd
= static_cast<wxAuiMDIChildFrame
*>(GetPage(evt
.GetSelection()));
843 // regardless of the result of wnd->Close(), we've
844 // already taken care of the close operations, so
845 // suppress further processing
849 void wxAuiMDIClientWindow::OnPageChanged(wxAuiNotebookEvent
& evt
)
851 PageChanged(evt
.GetOldSelection(), evt
.GetSelection());
854 void wxAuiMDIClientWindow::OnSize(wxSizeEvent
& evt
)
856 wxAuiNotebook::OnSize(evt
);
858 for (size_t pos
= 0; pos
< GetPageCount(); pos
++)
859 ((wxAuiMDIChildFrame
*)GetPage(pos
))->ApplyMDIChildFrameRect();