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 if ( !wxFrame::Create(parent
, id
, title
, pos
, size
, style
, name
) )
116 m_pClientWindow
= OnCreateClient();
117 return m_pClientWindow
!= NULL
;
121 void wxAuiMDIParentFrame::SetArtProvider(wxAuiTabArt
* provider
)
125 m_pClientWindow
->SetArtProvider(provider
);
129 wxAuiTabArt
* wxAuiMDIParentFrame::GetArtProvider()
131 if (!m_pClientWindow
)
134 return m_pClientWindow
->GetArtProvider();
137 wxAuiNotebook
* wxAuiMDIParentFrame::GetNotebook() const
139 return static_cast<wxAuiNotebook
*>(m_pClientWindow
);
145 void wxAuiMDIParentFrame::SetWindowMenu(wxMenu
* pMenu
)
147 // Replace the window menu from the currently loaded menu bar.
148 wxMenuBar
*pMenuBar
= GetMenuBar();
152 RemoveWindowMenu(pMenuBar
);
153 wxDELETE(m_pWindowMenu
);
158 m_pWindowMenu
= pMenu
;
159 AddWindowMenu(pMenuBar
);
163 void wxAuiMDIParentFrame::SetMenuBar(wxMenuBar
* pMenuBar
)
165 // Remove the Window menu from the old menu bar
166 RemoveWindowMenu(GetMenuBar());
168 // Add the Window menu to the new menu bar.
169 AddWindowMenu(pMenuBar
);
171 wxFrame::SetMenuBar(pMenuBar
);
172 //m_pMyMenuBar = GetMenuBar();
174 #endif // wxUSE_MENUS
176 void wxAuiMDIParentFrame::SetChildMenuBar(wxAuiMDIChildFrame
* pChild
)
181 // No Child, set Our menu bar back.
183 SetMenuBar(m_pMyMenuBar
);
185 SetMenuBar(GetMenuBar());
187 // Make sure we know our menu bar is in use
192 if (pChild
->GetMenuBar() == NULL
)
195 // Do we need to save the current bar?
196 if (m_pMyMenuBar
== NULL
)
197 m_pMyMenuBar
= GetMenuBar();
199 SetMenuBar(pChild
->GetMenuBar());
201 #endif // wxUSE_MENUS
204 bool wxAuiMDIParentFrame::ProcessEvent(wxEvent
& event
)
206 // stops the same event being processed repeatedly
207 if (m_pLastEvt
== &event
)
211 // let the active child (if any) process the event first.
213 if (m_pActiveChild
&&
214 event
.IsCommandEvent() &&
215 event
.GetEventObject() != m_pClientWindow
&&
216 !(event
.GetEventType() == wxEVT_ACTIVATE
||
217 event
.GetEventType() == wxEVT_SET_FOCUS
||
218 event
.GetEventType() == wxEVT_KILL_FOCUS
||
219 event
.GetEventType() == wxEVT_CHILD_FOCUS
||
220 event
.GetEventType() == wxEVT_COMMAND_SET_FOCUS
||
221 event
.GetEventType() == wxEVT_COMMAND_KILL_FOCUS
)
224 res
= m_pActiveChild
->GetEventHandler()->ProcessEvent(event
);
229 // if the event was not handled this frame will handle it,
230 // which is why we need the protection code at the beginning
232 res
= wxEvtHandler::ProcessEvent(event
);
240 wxAuiMDIChildFrame
*wxAuiMDIParentFrame::GetActiveChild() const
242 return m_pActiveChild
;
245 void wxAuiMDIParentFrame::SetActiveChild(wxAuiMDIChildFrame
* pChildFrame
)
247 m_pActiveChild
= pChildFrame
;
250 wxAuiMDIClientWindow
*wxAuiMDIParentFrame::GetClientWindow() const
252 return m_pClientWindow
;
255 wxAuiMDIClientWindow
*wxAuiMDIParentFrame::OnCreateClient()
257 return new wxAuiMDIClientWindow( this );
260 void wxAuiMDIParentFrame::ActivateNext()
262 if (m_pClientWindow
&& m_pClientWindow
->GetSelection() != wxNOT_FOUND
)
264 size_t active
= m_pClientWindow
->GetSelection() + 1;
265 if (active
>= m_pClientWindow
->GetPageCount())
268 m_pClientWindow
->SetSelection(active
);
272 void wxAuiMDIParentFrame::ActivatePrevious()
274 if (m_pClientWindow
&& m_pClientWindow
->GetSelection() != wxNOT_FOUND
)
276 int active
= m_pClientWindow
->GetSelection() - 1;
278 active
= m_pClientWindow
->GetPageCount() - 1;
280 m_pClientWindow
->SetSelection(active
);
284 void wxAuiMDIParentFrame::Init()
287 m_pClientWindow
= NULL
;
288 m_pActiveChild
= NULL
;
290 m_pWindowMenu
= NULL
;
292 #endif // wxUSE_MENUS
296 void wxAuiMDIParentFrame::RemoveWindowMenu(wxMenuBar
* pMenuBar
)
298 if (pMenuBar
&& m_pWindowMenu
)
300 // Remove old window menu
301 int pos
= pMenuBar
->FindMenu(_("&Window"));
302 if (pos
!= wxNOT_FOUND
)
304 // DBG:: We're going to delete the wrong menu!!!
305 wxASSERT(m_pWindowMenu
== pMenuBar
->GetMenu(pos
));
306 pMenuBar
->Remove(pos
);
311 void wxAuiMDIParentFrame::AddWindowMenu(wxMenuBar
*pMenuBar
)
313 if (pMenuBar
&& m_pWindowMenu
)
315 int pos
= pMenuBar
->FindMenu(wxGetStockLabel(wxID_HELP
,wxSTOCK_NOFLAGS
));
316 if (pos
== wxNOT_FOUND
)
317 pMenuBar
->Append(m_pWindowMenu
, _("&Window"));
319 pMenuBar
->Insert(pos
, m_pWindowMenu
, _("&Window"));
323 void wxAuiMDIParentFrame::DoHandleMenu(wxCommandEvent
& event
)
325 switch (event
.GetId())
329 m_pActiveChild
->Close();
331 case wxWINDOWCLOSEALL
:
332 while (m_pActiveChild
)
334 if (!m_pActiveChild
->Close())
350 #endif // wxUSE_MENUS
352 void wxAuiMDIParentFrame::DoGetClientSize(int* width
, int* height
) const
354 wxFrame::DoGetClientSize(width
, height
);
357 void wxAuiMDIParentFrame::Tile(wxOrientation orient
)
359 wxAuiMDIClientWindow
* client_window
= GetClientWindow();
360 wxASSERT_MSG(client_window
, wxT("Missing MDI Client Window"));
362 int cur_idx
= client_window
->GetSelection();
366 if (orient
== wxVERTICAL
)
368 client_window
->Split(cur_idx
, wxLEFT
);
370 else if (orient
== wxHORIZONTAL
)
372 client_window
->Split(cur_idx
, wxTOP
);
377 //-----------------------------------------------------------------------------
378 // wxAuiMDIChildFrame
379 //-----------------------------------------------------------------------------
381 IMPLEMENT_DYNAMIC_CLASS(wxAuiMDIChildFrame
, wxPanel
)
383 BEGIN_EVENT_TABLE(wxAuiMDIChildFrame
, wxPanel
)
384 EVT_MENU_HIGHLIGHT_ALL(wxAuiMDIChildFrame::OnMenuHighlight
)
385 EVT_ACTIVATE(wxAuiMDIChildFrame::OnActivate
)
386 EVT_CLOSE(wxAuiMDIChildFrame::OnCloseWindow
)
389 wxAuiMDIChildFrame::wxAuiMDIChildFrame()
394 wxAuiMDIChildFrame::wxAuiMDIChildFrame(wxAuiMDIParentFrame
*parent
,
396 const wxString
& title
,
397 const wxPoint
& WXUNUSED(pos
),
400 const wxString
& name
)
404 // There are two ways to create an tabbed mdi child fram without
405 // making it the active document. Either Show(false) can be called
406 // before Create() (as is customary on some ports with wxFrame-type
407 // windows), or wxMINIMIZE can be passed in the style flags. Note that
408 // wxAuiMDIChildFrame is not really derived from wxFrame, as wxMDIChildFrame
409 // is, but those are the expected symantics. No style flag is passed
410 // onto the panel underneath.
411 if (style
& wxMINIMIZE
)
412 m_activate_on_create
= false;
414 Create(parent
, id
, title
, wxDefaultPosition
, size
, 0, name
);
417 wxAuiMDIChildFrame::~wxAuiMDIChildFrame()
419 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
422 if (pParentFrame
->GetActiveChild() == this)
424 pParentFrame
->SetActiveChild(NULL
);
425 pParentFrame
->SetChildMenuBar(NULL
);
427 wxAuiMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
428 wxASSERT(pClientWindow
);
429 int idx
= pClientWindow
->GetPageIndex(this);
430 if (idx
!= wxNOT_FOUND
)
432 pClientWindow
->RemovePage(idx
);
437 wxDELETE(m_pMenuBar
);
438 #endif // wxUSE_MENUS
441 bool wxAuiMDIChildFrame::Create(wxAuiMDIParentFrame
* parent
,
443 const wxString
& title
,
444 const wxPoint
& WXUNUSED(pos
),
447 const wxString
& name
)
449 wxAuiMDIClientWindow
* pClientWindow
= parent
->GetClientWindow();
450 wxASSERT_MSG((pClientWindow
!= NULL
), wxT("Missing MDI client window."));
452 // see comment in constructor
453 if (style
& wxMINIMIZE
)
454 m_activate_on_create
= false;
456 wxSize cli_size
= pClientWindow
->GetClientSize();
458 // create the window off-screen to prevent flicker
459 wxPanel::Create(pClientWindow
,
461 wxPoint(cli_size
.x
+1, cli_size
.y
+1),
467 SetMDIParentFrame(parent
);
469 // this is the currently active child
470 parent
->SetActiveChild(this);
474 pClientWindow
->AddPage(this, title
, m_activate_on_create
);
475 pClientWindow
->Refresh();
480 bool wxAuiMDIChildFrame::Destroy()
482 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
483 wxASSERT_MSG(pParentFrame
, wxT("Missing MDI Parent Frame"));
485 wxAuiMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
486 wxASSERT_MSG(pClientWindow
, wxT("Missing MDI Client Window"));
488 if (pParentFrame
->GetActiveChild() == this)
490 // deactivate ourself
491 wxActivateEvent
event(wxEVT_ACTIVATE
, false, GetId());
492 event
.SetEventObject(this);
493 GetEventHandler()->ProcessEvent(event
);
495 pParentFrame
->SetActiveChild(NULL
);
496 pParentFrame
->SetChildMenuBar(NULL
);
499 size_t page_count
= pClientWindow
->GetPageCount();
500 for (size_t pos
= 0; pos
< page_count
; pos
++)
502 if (pClientWindow
->GetPage(pos
) == this)
503 return pClientWindow
->DeletePage(pos
);
510 void wxAuiMDIChildFrame::SetMenuBar(wxMenuBar
*menu_bar
)
512 wxMenuBar
*pOldMenuBar
= m_pMenuBar
;
513 m_pMenuBar
= menu_bar
;
517 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
518 wxASSERT_MSG(pParentFrame
, wxT("Missing MDI Parent Frame"));
520 m_pMenuBar
->SetParent(pParentFrame
);
521 if (pParentFrame
->GetActiveChild() == this)
523 // replace current menu bars
525 pParentFrame
->SetChildMenuBar(NULL
);
526 pParentFrame
->SetChildMenuBar(this);
531 wxMenuBar
*wxAuiMDIChildFrame::GetMenuBar() const
535 #endif // wxUSE_MENUS
537 void wxAuiMDIChildFrame::SetTitle(const wxString
& title
)
541 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
542 wxASSERT_MSG(pParentFrame
, wxT("Missing MDI Parent Frame"));
544 wxAuiMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
545 if (pClientWindow
!= NULL
)
548 for (pos
= 0; pos
< pClientWindow
->GetPageCount(); pos
++)
550 if (pClientWindow
->GetPage(pos
) == this)
552 pClientWindow
->SetPageText(pos
, m_title
);
559 wxString
wxAuiMDIChildFrame::GetTitle() const
564 void wxAuiMDIChildFrame::SetIcons(const wxIconBundle
& icons
)
566 // get icon with the system icon size
567 SetIcon(icons
.GetIcon(-1));
568 m_icon_bundle
= icons
;
571 const wxIconBundle
& wxAuiMDIChildFrame::GetIcons() const
573 return m_icon_bundle
;
576 void wxAuiMDIChildFrame::SetIcon(const wxIcon
& icon
)
578 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
579 wxASSERT_MSG(pParentFrame
, wxT("Missing MDI Parent Frame"));
584 bmp
.CopyFromIcon(m_icon
);
586 wxAuiMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
587 if (pClientWindow
!= NULL
)
589 int idx
= pClientWindow
->GetPageIndex(this);
593 pClientWindow
->SetPageBitmap((size_t)idx
, bmp
);
598 const wxIcon
& wxAuiMDIChildFrame::GetIcon() const
604 void wxAuiMDIChildFrame::Activate()
606 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
607 wxASSERT_MSG(pParentFrame
, wxT("Missing MDI Parent Frame"));
609 wxAuiMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
611 if (pClientWindow
!= NULL
)
614 for (pos
= 0; pos
< pClientWindow
->GetPageCount(); pos
++)
616 if (pClientWindow
->GetPage(pos
) == this)
618 pClientWindow
->SetSelection(pos
);
625 void wxAuiMDIChildFrame::OnMenuHighlight(wxMenuEvent
& event
)
628 if (m_pMDIParentFrame
)
630 // we don't have any help text for this item,
631 // but may be the MDI frame does?
632 m_pMDIParentFrame
->OnMenuHighlight(event
);
636 #endif // wxUSE_STATUSBAR
639 void wxAuiMDIChildFrame::OnActivate(wxActivateEvent
& WXUNUSED(event
))
644 void wxAuiMDIChildFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
649 void wxAuiMDIChildFrame::SetMDIParentFrame(wxAuiMDIParentFrame
* parentFrame
)
651 m_pMDIParentFrame
= parentFrame
;
654 wxAuiMDIParentFrame
* wxAuiMDIChildFrame::GetMDIParentFrame() const
656 return m_pMDIParentFrame
;
659 void wxAuiMDIChildFrame::Init()
661 m_activate_on_create
= true;
662 m_pMDIParentFrame
= NULL
;
665 #endif // wxUSE_MENUS
668 bool wxAuiMDIChildFrame::Show(bool show
)
670 m_activate_on_create
= show
;
676 void wxAuiMDIChildFrame::DoShow(bool show
)
678 wxWindow::Show(show
);
681 void wxAuiMDIChildFrame::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
683 m_mdi_newrect
= wxRect(x
, y
, width
, height
);
685 wxPanel::DoSetSize(x
,y
,width
, height
, sizeFlags
);
687 wxUnusedVar(sizeFlags
);
691 void wxAuiMDIChildFrame::DoMoveWindow(int x
, int y
, int width
, int height
)
693 m_mdi_newrect
= wxRect(x
, y
, width
, height
);
696 void wxAuiMDIChildFrame::ApplyMDIChildFrameRect()
698 if (m_mdi_currect
!= m_mdi_newrect
)
700 wxPanel::DoMoveWindow(m_mdi_newrect
.x
, m_mdi_newrect
.y
,
701 m_mdi_newrect
.width
, m_mdi_newrect
.height
);
702 m_mdi_currect
= m_mdi_newrect
;
707 //-----------------------------------------------------------------------------
708 // wxAuiMDIClientWindow
709 //-----------------------------------------------------------------------------
711 IMPLEMENT_DYNAMIC_CLASS(wxAuiMDIClientWindow
, wxAuiNotebook
)
713 BEGIN_EVENT_TABLE(wxAuiMDIClientWindow
, wxAuiNotebook
)
714 EVT_AUINOTEBOOK_PAGE_CHANGED(wxID_ANY
, wxAuiMDIClientWindow::OnPageChanged
)
715 EVT_AUINOTEBOOK_PAGE_CLOSE(wxID_ANY
, wxAuiMDIClientWindow::OnPageClose
)
716 EVT_SIZE(wxAuiMDIClientWindow::OnSize
)
719 wxAuiMDIClientWindow::wxAuiMDIClientWindow()
723 wxAuiMDIClientWindow::wxAuiMDIClientWindow(wxAuiMDIParentFrame
* parent
, long style
)
725 CreateClient(parent
, style
);
728 wxAuiMDIClientWindow::~wxAuiMDIClientWindow()
733 bool wxAuiMDIClientWindow::CreateClient(wxAuiMDIParentFrame
* parent
, long style
)
735 SetWindowStyleFlag(style
);
737 wxSize caption_icon_size
=
738 wxSize(wxSystemSettings::GetMetric(wxSYS_SMALLICON_X
),
739 wxSystemSettings::GetMetric(wxSYS_SMALLICON_Y
));
740 SetUniformBitmapSize(caption_icon_size
);
742 if (!wxAuiNotebook::Create(parent
,
746 wxAUI_NB_DEFAULT_STYLE
| wxNO_BORDER
))
751 wxColour bkcolour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
752 SetOwnBackgroundColour(bkcolour
);
754 m_mgr
.GetArtProvider()->SetColour(wxAUI_DOCKART_BACKGROUND_COLOUR
, bkcolour
);
759 int wxAuiMDIClientWindow::SetSelection(size_t nPage
)
761 return wxAuiNotebook::SetSelection(nPage
);
764 void wxAuiMDIClientWindow::PageChanged(int old_selection
, int new_selection
)
766 // don't do anything if the page doesn't actually change
767 if (old_selection
== new_selection
)
771 // don't do anything if the new page is already active
772 if (new_selection != -1)
774 wxAuiMDIChildFrame* child = (wxAuiMDIChildFrame*)GetPage(new_selection);
775 if (child->GetMDIParentFrame()->GetActiveChild() == child)
780 // notify old active child that it has been deactivated
781 if ((old_selection
!= -1) && (old_selection
< (int)GetPageCount()))
783 wxAuiMDIChildFrame
* old_child
= (wxAuiMDIChildFrame
*)GetPage(old_selection
);
784 wxASSERT_MSG(old_child
, wxT("wxAuiMDIClientWindow::PageChanged - null page pointer"));
786 wxActivateEvent
event(wxEVT_ACTIVATE
, false, old_child
->GetId());
787 event
.SetEventObject(old_child
);
788 old_child
->GetEventHandler()->ProcessEvent(event
);
791 // notify new active child that it has been activated
792 if (new_selection
!= -1)
794 wxAuiMDIChildFrame
* active_child
= (wxAuiMDIChildFrame
*)GetPage(new_selection
);
795 wxASSERT_MSG(active_child
, wxT("wxAuiMDIClientWindow::PageChanged - null page pointer"));
797 wxActivateEvent
event(wxEVT_ACTIVATE
, true, active_child
->GetId());
798 event
.SetEventObject(active_child
);
799 active_child
->GetEventHandler()->ProcessEvent(event
);
801 if (active_child
->GetMDIParentFrame())
803 active_child
->GetMDIParentFrame()->SetActiveChild(active_child
);
804 active_child
->GetMDIParentFrame()->SetChildMenuBar(active_child
);
811 void wxAuiMDIClientWindow::OnPageClose(wxAuiNotebookEvent
& evt
)
813 wxAuiMDIChildFrame
* wnd
;
814 wnd
= static_cast<wxAuiMDIChildFrame
*>(GetPage(evt
.GetSelection()));
818 // regardless of the result of wnd->Close(), we've
819 // already taken care of the close operations, so
820 // suppress further processing
824 void wxAuiMDIClientWindow::OnPageChanged(wxAuiNotebookEvent
& evt
)
826 PageChanged(evt
.GetOldSelection(), evt
.GetSelection());
829 void wxAuiMDIClientWindow::OnSize(wxSizeEvent
& evt
)
831 wxAuiNotebook::OnSize(evt
);
833 for (size_t pos
= 0; pos
< GetPageCount(); pos
++)
834 ((wxAuiMDIChildFrame
*)GetPage(pos
))->ApplyMDIChildFrameRect();