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.
180 SetMenuBar(m_pMyMenuBar
);
182 // Make sure we know our menu bar is in use
187 if (pChild
->GetMenuBar() == NULL
)
190 // Do we need to save the current bar?
191 if (m_pMyMenuBar
== NULL
)
192 m_pMyMenuBar
= GetMenuBar();
194 SetMenuBar(pChild
->GetMenuBar());
196 #endif // wxUSE_MENUS
199 bool wxAuiMDIParentFrame::ProcessEvent(wxEvent
& event
)
201 // stops the same event being processed repeatedly
202 if (m_pLastEvt
== &event
)
206 // let the active child (if any) process the event first.
208 if (m_pActiveChild
&&
209 event
.IsCommandEvent() &&
210 event
.GetEventObject() != m_pClientWindow
&&
211 !(event
.GetEventType() == wxEVT_ACTIVATE
||
212 event
.GetEventType() == wxEVT_SET_FOCUS
||
213 event
.GetEventType() == wxEVT_KILL_FOCUS
||
214 event
.GetEventType() == wxEVT_CHILD_FOCUS
||
215 event
.GetEventType() == wxEVT_COMMAND_SET_FOCUS
||
216 event
.GetEventType() == wxEVT_COMMAND_KILL_FOCUS
)
219 res
= m_pActiveChild
->GetEventHandler()->ProcessEvent(event
);
224 // if the event was not handled this frame will handle it,
225 // which is why we need the protection code at the beginning
227 res
= wxEvtHandler::ProcessEvent(event
);
235 wxAuiMDIChildFrame
*wxAuiMDIParentFrame::GetActiveChild() const
237 return m_pActiveChild
;
240 void wxAuiMDIParentFrame::SetActiveChild(wxAuiMDIChildFrame
* pChildFrame
)
242 m_pActiveChild
= pChildFrame
;
245 wxAuiMDIClientWindow
*wxAuiMDIParentFrame::GetClientWindow() const
247 return m_pClientWindow
;
250 wxAuiMDIClientWindow
*wxAuiMDIParentFrame::OnCreateClient()
252 m_pClientWindow
= new wxAuiMDIClientWindow( this );
253 return m_pClientWindow
;
256 void wxAuiMDIParentFrame::ActivateNext()
258 if (m_pClientWindow
&& m_pClientWindow
->GetSelection() != wxNOT_FOUND
)
260 size_t active
= m_pClientWindow
->GetSelection() + 1;
261 if (active
>= m_pClientWindow
->GetPageCount())
264 m_pClientWindow
->SetSelection(active
);
268 void wxAuiMDIParentFrame::ActivatePrevious()
270 if (m_pClientWindow
&& m_pClientWindow
->GetSelection() != wxNOT_FOUND
)
272 int active
= m_pClientWindow
->GetSelection() - 1;
274 active
= m_pClientWindow
->GetPageCount() - 1;
276 m_pClientWindow
->SetSelection(active
);
280 void wxAuiMDIParentFrame::Init()
283 m_pClientWindow
= NULL
;
284 m_pActiveChild
= NULL
;
286 m_pWindowMenu
= NULL
;
288 #endif // wxUSE_MENUS
292 void wxAuiMDIParentFrame::RemoveWindowMenu(wxMenuBar
* pMenuBar
)
294 if (pMenuBar
&& m_pWindowMenu
)
296 // Remove old window menu
297 int pos
= pMenuBar
->FindMenu(_("&Window"));
298 if (pos
!= wxNOT_FOUND
)
300 // DBG:: We're going to delete the wrong menu!!!
301 wxASSERT(m_pWindowMenu
== pMenuBar
->GetMenu(pos
));
302 pMenuBar
->Remove(pos
);
307 void wxAuiMDIParentFrame::AddWindowMenu(wxMenuBar
*pMenuBar
)
309 if (pMenuBar
&& m_pWindowMenu
)
311 int pos
= pMenuBar
->FindMenu(wxGetStockLabel(wxID_HELP
,wxSTOCK_NOFLAGS
));
312 if (pos
== wxNOT_FOUND
)
313 pMenuBar
->Append(m_pWindowMenu
, _("&Window"));
315 pMenuBar
->Insert(pos
, m_pWindowMenu
, _("&Window"));
319 void wxAuiMDIParentFrame::DoHandleMenu(wxCommandEvent
& event
)
321 switch (event
.GetId())
325 m_pActiveChild
->Close();
327 case wxWINDOWCLOSEALL
:
328 while (m_pActiveChild
)
330 if (!m_pActiveChild
->Close())
336 delete m_pActiveChild
;
337 m_pActiveChild
= NULL
;
351 #endif // wxUSE_MENUS
353 void wxAuiMDIParentFrame::DoGetClientSize(int* width
, int* height
) const
355 wxFrame::DoGetClientSize(width
, height
);
358 void wxAuiMDIParentFrame::Tile(wxOrientation orient
)
360 wxAuiMDIClientWindow
* client_window
= GetClientWindow();
361 wxASSERT_MSG(client_window
, wxT("Missing MDI Client Window"));
363 int cur_idx
= client_window
->GetSelection();
367 if (orient
== wxVERTICAL
)
369 client_window
->Split(cur_idx
, wxLEFT
);
371 else if (orient
== wxHORIZONTAL
)
373 client_window
->Split(cur_idx
, wxTOP
);
378 //-----------------------------------------------------------------------------
379 // wxAuiMDIChildFrame
380 //-----------------------------------------------------------------------------
382 IMPLEMENT_DYNAMIC_CLASS(wxAuiMDIChildFrame
, wxPanel
)
384 BEGIN_EVENT_TABLE(wxAuiMDIChildFrame
, wxPanel
)
385 EVT_MENU_HIGHLIGHT_ALL(wxAuiMDIChildFrame::OnMenuHighlight
)
386 EVT_ACTIVATE(wxAuiMDIChildFrame::OnActivate
)
387 EVT_CLOSE(wxAuiMDIChildFrame::OnCloseWindow
)
390 wxAuiMDIChildFrame::wxAuiMDIChildFrame()
395 wxAuiMDIChildFrame::wxAuiMDIChildFrame(wxAuiMDIParentFrame
*parent
,
397 const wxString
& title
,
398 const wxPoint
& WXUNUSED(pos
),
401 const wxString
& name
)
405 // There are two ways to create an tabbed mdi child fram without
406 // making it the active document. Either Show(false) can be called
407 // before Create() (as is customary on some ports with wxFrame-type
408 // windows), or wxMINIMIZE can be passed in the style flags. Note that
409 // wxAuiMDIChildFrame is not really derived from wxFrame, as wxMDIChildFrame
410 // is, but those are the expected symantics. No style flag is passed
411 // onto the panel underneath.
412 if (style
& wxMINIMIZE
)
413 m_activate_on_create
= false;
415 Create(parent
, id
, title
, wxDefaultPosition
, size
, 0, name
);
418 wxAuiMDIChildFrame::~wxAuiMDIChildFrame()
420 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
421 if (pParentFrame
&& pParentFrame
->GetActiveChild() == this)
423 pParentFrame
->SetActiveChild(NULL
);
424 pParentFrame
->SetChildMenuBar(NULL
);
428 wxDELETE(m_pMenuBar
);
429 #endif // wxUSE_MENUS
432 bool wxAuiMDIChildFrame::Create(wxAuiMDIParentFrame
* parent
,
434 const wxString
& title
,
435 const wxPoint
& WXUNUSED(pos
),
438 const wxString
& name
)
440 wxAuiMDIClientWindow
* pClientWindow
= parent
->GetClientWindow();
441 wxASSERT_MSG((pClientWindow
!= (wxWindow
*) NULL
), wxT("Missing MDI client window."));
443 // see comment in constructor
444 if (style
& wxMINIMIZE
)
445 m_activate_on_create
= false;
447 wxSize cli_size
= pClientWindow
->GetClientSize();
449 // create the window off-screen to prevent flicker
450 wxPanel::Create(pClientWindow
,
452 wxPoint(cli_size
.x
+1, cli_size
.y
+1),
458 SetMDIParentFrame(parent
);
460 // this is the currently active child
461 parent
->SetActiveChild(this);
465 pClientWindow
->AddPage(this, title
, m_activate_on_create
);
466 pClientWindow
->Refresh();
471 bool wxAuiMDIChildFrame::Destroy()
473 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
474 wxASSERT_MSG(pParentFrame
, wxT("Missing MDI Parent Frame"));
476 wxAuiMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
477 wxASSERT_MSG(pClientWindow
, wxT("Missing MDI Client Window"));
479 if (pParentFrame
->GetActiveChild() == this)
481 // deactivate ourself
482 wxActivateEvent
event(wxEVT_ACTIVATE
, false, GetId());
483 event
.SetEventObject(this);
484 GetEventHandler()->ProcessEvent(event
);
486 pParentFrame
->SetActiveChild(NULL
);
487 pParentFrame
->SetChildMenuBar(NULL
);
490 size_t page_count
= pClientWindow
->GetPageCount();
491 for (size_t pos
= 0; pos
< page_count
; pos
++)
493 if (pClientWindow
->GetPage(pos
) == this)
494 return pClientWindow
->DeletePage(pos
);
501 void wxAuiMDIChildFrame::SetMenuBar(wxMenuBar
*menu_bar
)
503 wxMenuBar
*pOldMenuBar
= m_pMenuBar
;
504 m_pMenuBar
= menu_bar
;
508 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
509 wxASSERT_MSG(pParentFrame
, wxT("Missing MDI Parent Frame"));
511 m_pMenuBar
->SetParent(pParentFrame
);
512 if (pParentFrame
->GetActiveChild() == this)
514 // replace current menu bars
516 pParentFrame
->SetChildMenuBar(NULL
);
517 pParentFrame
->SetChildMenuBar(this);
522 wxMenuBar
*wxAuiMDIChildFrame::GetMenuBar() const
526 #endif // wxUSE_MENUS
528 void wxAuiMDIChildFrame::SetTitle(const wxString
& title
)
532 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
533 wxASSERT_MSG(pParentFrame
, wxT("Missing MDI Parent Frame"));
535 wxAuiMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
536 if (pClientWindow
!= NULL
)
539 for (pos
= 0; pos
< pClientWindow
->GetPageCount(); pos
++)
541 if (pClientWindow
->GetPage(pos
) == this)
543 pClientWindow
->SetPageText(pos
, m_title
);
550 wxString
wxAuiMDIChildFrame::GetTitle() const
555 void wxAuiMDIChildFrame::SetIcons(const wxIconBundle
& icons
)
557 // get icon with the system icon size
558 SetIcon(icons
.GetIcon(-1));
559 m_icon_bundle
= icons
;
562 const wxIconBundle
& wxAuiMDIChildFrame::GetIcons() const
564 return m_icon_bundle
;
567 void wxAuiMDIChildFrame::SetIcon(const wxIcon
& icon
)
569 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
570 wxASSERT_MSG(pParentFrame
, wxT("Missing MDI Parent Frame"));
575 bmp
.CopyFromIcon(m_icon
);
577 wxAuiMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
578 if (pClientWindow
!= NULL
)
580 int idx
= pClientWindow
->GetPageIndex(this);
584 pClientWindow
->SetPageBitmap((size_t)idx
, bmp
);
589 const wxIcon
& wxAuiMDIChildFrame::GetIcon() const
595 void wxAuiMDIChildFrame::Activate()
597 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
598 wxASSERT_MSG(pParentFrame
, wxT("Missing MDI Parent Frame"));
600 wxAuiMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
602 if (pClientWindow
!= NULL
)
605 for (pos
= 0; pos
< pClientWindow
->GetPageCount(); pos
++)
607 if (pClientWindow
->GetPage(pos
) == this)
609 pClientWindow
->SetSelection(pos
);
616 void wxAuiMDIChildFrame::OnMenuHighlight(wxMenuEvent
& event
)
619 if (m_pMDIParentFrame
)
621 // we don't have any help text for this item,
622 // but may be the MDI frame does?
623 m_pMDIParentFrame
->OnMenuHighlight(event
);
627 #endif // wxUSE_STATUSBAR
630 void wxAuiMDIChildFrame::OnActivate(wxActivateEvent
& WXUNUSED(event
))
635 void wxAuiMDIChildFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
640 void wxAuiMDIChildFrame::SetMDIParentFrame(wxAuiMDIParentFrame
* parentFrame
)
642 m_pMDIParentFrame
= parentFrame
;
645 wxAuiMDIParentFrame
* wxAuiMDIChildFrame::GetMDIParentFrame() const
647 return m_pMDIParentFrame
;
650 void wxAuiMDIChildFrame::Init()
652 m_activate_on_create
= true;
653 m_pMDIParentFrame
= NULL
;
656 #endif // wxUSE_MENUS
659 bool wxAuiMDIChildFrame::Show(bool show
)
661 m_activate_on_create
= show
;
667 void wxAuiMDIChildFrame::DoShow(bool show
)
669 wxWindow::Show(show
);
672 void wxAuiMDIChildFrame::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
674 m_mdi_newrect
= wxRect(x
, y
, width
, height
);
676 wxPanel::DoSetSize(x
,y
,width
, height
, sizeFlags
);
678 wxUnusedVar(sizeFlags
);
682 void wxAuiMDIChildFrame::DoMoveWindow(int x
, int y
, int width
, int height
)
684 m_mdi_newrect
= wxRect(x
, y
, width
, height
);
687 void wxAuiMDIChildFrame::ApplyMDIChildFrameRect()
689 if (m_mdi_currect
!= m_mdi_newrect
)
691 wxPanel::DoMoveWindow(m_mdi_newrect
.x
, m_mdi_newrect
.y
,
692 m_mdi_newrect
.width
, m_mdi_newrect
.height
);
693 m_mdi_currect
= m_mdi_newrect
;
698 //-----------------------------------------------------------------------------
699 // wxAuiMDIClientWindow
700 //-----------------------------------------------------------------------------
702 IMPLEMENT_DYNAMIC_CLASS(wxAuiMDIClientWindow
, wxAuiNotebook
)
704 BEGIN_EVENT_TABLE(wxAuiMDIClientWindow
, wxAuiNotebook
)
705 EVT_AUINOTEBOOK_PAGE_CHANGED(wxID_ANY
, wxAuiMDIClientWindow::OnPageChanged
)
706 EVT_AUINOTEBOOK_PAGE_CLOSE(wxID_ANY
, wxAuiMDIClientWindow::OnPageClose
)
707 EVT_SIZE(wxAuiMDIClientWindow::OnSize
)
710 wxAuiMDIClientWindow::wxAuiMDIClientWindow()
714 wxAuiMDIClientWindow::wxAuiMDIClientWindow(wxAuiMDIParentFrame
* parent
, long style
)
716 CreateClient(parent
, style
);
719 wxAuiMDIClientWindow::~wxAuiMDIClientWindow()
724 bool wxAuiMDIClientWindow::CreateClient(wxAuiMDIParentFrame
* parent
, long style
)
726 SetWindowStyleFlag(style
);
728 wxSize caption_icon_size
=
729 wxSize(wxSystemSettings::GetMetric(wxSYS_SMALLICON_X
),
730 wxSystemSettings::GetMetric(wxSYS_SMALLICON_Y
));
731 SetUniformBitmapSize(caption_icon_size
);
733 if (!wxAuiNotebook::Create(parent
,
737 wxAUI_NB_DEFAULT_STYLE
| wxNO_BORDER
))
742 wxColour bkcolour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
743 SetOwnBackgroundColour(bkcolour
);
745 m_mgr
.GetArtProvider()->SetColour(wxAUI_DOCKART_BACKGROUND_COLOUR
, bkcolour
);
750 int wxAuiMDIClientWindow::SetSelection(size_t nPage
)
752 return wxAuiNotebook::SetSelection(nPage
);
755 void wxAuiMDIClientWindow::PageChanged(int old_selection
, int new_selection
)
757 // don't do anything if the page doesn't actually change
758 if (old_selection
== new_selection
)
762 // don't do anything if the new page is already active
763 if (new_selection != -1)
765 wxAuiMDIChildFrame* child = (wxAuiMDIChildFrame*)GetPage(new_selection);
766 if (child->GetMDIParentFrame()->GetActiveChild() == child)
771 // notify old active child that it has been deactivated
772 if (old_selection
!= -1)
774 wxAuiMDIChildFrame
* old_child
= (wxAuiMDIChildFrame
*)GetPage(old_selection
);
775 wxASSERT_MSG(old_child
, wxT("wxAuiMDIClientWindow::PageChanged - null page pointer"));
777 wxActivateEvent
event(wxEVT_ACTIVATE
, false, old_child
->GetId());
778 event
.SetEventObject(old_child
);
779 old_child
->GetEventHandler()->ProcessEvent(event
);
782 // notify new active child that it has been activated
783 if (new_selection
!= -1)
785 wxAuiMDIChildFrame
* active_child
= (wxAuiMDIChildFrame
*)GetPage(new_selection
);
786 wxASSERT_MSG(active_child
, wxT("wxAuiMDIClientWindow::PageChanged - null page pointer"));
788 wxActivateEvent
event(wxEVT_ACTIVATE
, true, active_child
->GetId());
789 event
.SetEventObject(active_child
);
790 active_child
->GetEventHandler()->ProcessEvent(event
);
792 if (active_child
->GetMDIParentFrame())
794 active_child
->GetMDIParentFrame()->SetActiveChild(active_child
);
795 active_child
->GetMDIParentFrame()->SetChildMenuBar(active_child
);
802 void wxAuiMDIClientWindow::OnPageClose(wxAuiNotebookEvent
& evt
)
804 wxAuiMDIChildFrame
* wnd
;
805 wnd
= static_cast<wxAuiMDIChildFrame
*>(GetPage(evt
.GetSelection()));
809 // regardless of the result of wnd->Close(), we've
810 // already taken care of the close operations, so
811 // suppress further processing
815 void wxAuiMDIClientWindow::OnPageChanged(wxAuiNotebookEvent
& evt
)
817 PageChanged(evt
.GetOldSelection(), evt
.GetSelection());
820 void wxAuiMDIClientWindow::OnSize(wxSizeEvent
& evt
)
822 wxAuiNotebook::OnSize(evt
);
824 for (size_t pos
= 0; pos
< GetPageCount(); pos
++)
825 ((wxAuiMDIChildFrame
*)GetPage(pos
))->ApplyMDIChildFrameRect();