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 RemoveWindowMenu(GetMenuBar());
90 bool wxAuiMDIParentFrame::Create(wxWindow
*parent
,
92 const wxString
& title
,
99 // this style can be used to prevent a window from having the standard MDI
101 if (!(style
& wxFRAME_NO_WINDOW_MENU
))
103 m_pWindowMenu
= new wxMenu
;
104 m_pWindowMenu
->Append(wxWINDOWCLOSE
, _("Cl&ose"));
105 m_pWindowMenu
->Append(wxWINDOWCLOSEALL
, _("Close All"));
106 m_pWindowMenu
->AppendSeparator();
107 m_pWindowMenu
->Append(wxWINDOWNEXT
, _("&Next"));
108 m_pWindowMenu
->Append(wxWINDOWPREV
, _("&Previous"));
110 #endif // wxUSE_MENUS
112 wxFrame::Create(parent
, id
, title
, pos
, size
, style
, name
);
118 void wxAuiMDIParentFrame::SetWindowMenu(wxMenu
* pMenu
)
120 // Replace the window menu from the currently loaded menu bar.
121 wxMenuBar
*pMenuBar
= GetMenuBar();
125 RemoveWindowMenu(pMenuBar
);
126 wxDELETE(m_pWindowMenu
);
131 m_pWindowMenu
= pMenu
;
132 AddWindowMenu(pMenuBar
);
136 void wxAuiMDIParentFrame::SetMenuBar(wxMenuBar
* pMenuBar
)
138 // Remove the Window menu from the old menu bar
139 RemoveWindowMenu(GetMenuBar());
141 // Add the Window menu to the new menu bar.
142 AddWindowMenu(pMenuBar
);
144 wxFrame::SetMenuBar(pMenuBar
);
145 m_pMyMenuBar
= GetMenuBar();
147 #endif // wxUSE_MENUS
149 void wxAuiMDIParentFrame::SetChildMenuBar(wxAuiMDIChildFrame
* pChild
)
154 // No Child, set Our menu bar back.
155 SetMenuBar(m_pMyMenuBar
);
157 // Make sure we know our menu bar is in use
158 //m_pMyMenuBar = NULL;
162 if (pChild
->GetMenuBar() == NULL
)
165 // Do we need to save the current bar?
166 if (m_pMyMenuBar
== NULL
)
167 m_pMyMenuBar
= GetMenuBar();
169 SetMenuBar(pChild
->GetMenuBar());
171 #endif // wxUSE_MENUS
174 bool wxAuiMDIParentFrame::ProcessEvent(wxEvent
& event
)
176 // Stops the same event being processed repeatedly
177 static wxEventType inEvent
= wxEVT_NULL
;
178 if (inEvent
== event
.GetEventType())
181 inEvent
= event
.GetEventType();
183 // Let the active child (if any) process the event first.
185 if (m_pActiveChild
&&
186 event
.IsCommandEvent() &&
187 event
.GetEventObject() != m_pClientWindow
&&
188 !(event
.GetEventType() == wxEVT_ACTIVATE
||
189 event
.GetEventType() == wxEVT_SET_FOCUS
||
190 event
.GetEventType() == wxEVT_KILL_FOCUS
||
191 event
.GetEventType() == wxEVT_CHILD_FOCUS
||
192 event
.GetEventType() == wxEVT_COMMAND_SET_FOCUS
||
193 event
.GetEventType() == wxEVT_COMMAND_KILL_FOCUS
)
196 res
= m_pActiveChild
->GetEventHandler()->ProcessEvent(event
);
199 // If the event was not handled this frame will handle it!
202 //res = GetEventHandler()->ProcessEvent(event);
203 res
= wxEvtHandler::ProcessEvent(event
);
206 inEvent
= wxEVT_NULL
;
211 wxAuiMDIChildFrame
*wxAuiMDIParentFrame::GetActiveChild() const
213 return m_pActiveChild
;
216 void wxAuiMDIParentFrame::SetActiveChild(wxAuiMDIChildFrame
* pChildFrame
)
218 m_pActiveChild
= pChildFrame
;
221 wxAuiMDIClientWindow
*wxAuiMDIParentFrame::GetClientWindow() const
223 return m_pClientWindow
;
226 wxAuiMDIClientWindow
*wxAuiMDIParentFrame::OnCreateClient()
228 m_pClientWindow
= new wxAuiMDIClientWindow( this );
229 return m_pClientWindow
;
232 void wxAuiMDIParentFrame::ActivateNext()
234 if (m_pClientWindow
&& m_pClientWindow
->GetSelection() != wxNOT_FOUND
)
236 size_t active
= m_pClientWindow
->GetSelection() + 1;
237 if (active
>= m_pClientWindow
->GetPageCount())
240 m_pClientWindow
->SetSelection(active
);
244 void wxAuiMDIParentFrame::ActivatePrevious()
246 if (m_pClientWindow
&& m_pClientWindow
->GetSelection() != wxNOT_FOUND
)
248 int active
= m_pClientWindow
->GetSelection() - 1;
250 active
= m_pClientWindow
->GetPageCount() - 1;
252 m_pClientWindow
->SetSelection(active
);
256 void wxAuiMDIParentFrame::Init()
258 m_pClientWindow
= NULL
;
259 m_pActiveChild
= NULL
;
261 m_pWindowMenu
= NULL
;
263 #endif // wxUSE_MENUS
267 void wxAuiMDIParentFrame::RemoveWindowMenu(wxMenuBar
* pMenuBar
)
269 if (pMenuBar
&& m_pWindowMenu
)
271 // Remove old window menu
272 int pos
= pMenuBar
->FindMenu(_("&Window"));
273 if (pos
!= wxNOT_FOUND
)
275 // DBG:: We're going to delete the wrong menu!!!
276 wxASSERT(m_pWindowMenu
== pMenuBar
->GetMenu(pos
));
277 pMenuBar
->Remove(pos
);
282 void wxAuiMDIParentFrame::AddWindowMenu(wxMenuBar
*pMenuBar
)
284 if (pMenuBar
&& m_pWindowMenu
)
286 int pos
= pMenuBar
->FindMenu(wxGetStockLabel(wxID_HELP
,wxSTOCK_NOFLAGS
));
287 if (pos
== wxNOT_FOUND
)
288 pMenuBar
->Append(m_pWindowMenu
, _("&Window"));
290 pMenuBar
->Insert(pos
, m_pWindowMenu
, _("&Window"));
294 void wxAuiMDIParentFrame::DoHandleMenu(wxCommandEvent
& event
)
296 switch (event
.GetId())
300 m_pActiveChild
->Close();
302 case wxWINDOWCLOSEALL
:
303 while (m_pActiveChild
)
305 if (!m_pActiveChild
->Close())
311 delete m_pActiveChild
;
312 m_pActiveChild
= NULL
;
326 #endif // wxUSE_MENUS
328 void wxAuiMDIParentFrame::DoGetClientSize(int* width
, int* height
) const
330 wxFrame::DoGetClientSize(width
, height
);
333 //-----------------------------------------------------------------------------
334 // wxAuiMDIChildFrame
335 //-----------------------------------------------------------------------------
337 IMPLEMENT_DYNAMIC_CLASS(wxAuiMDIChildFrame
, wxPanel
)
339 BEGIN_EVENT_TABLE(wxAuiMDIChildFrame
, wxPanel
)
340 EVT_MENU_HIGHLIGHT_ALL(wxAuiMDIChildFrame::OnMenuHighlight
)
341 EVT_ACTIVATE(wxAuiMDIChildFrame::OnActivate
)
342 EVT_CLOSE(wxAuiMDIChildFrame::OnCloseWindow
)
345 wxAuiMDIChildFrame::wxAuiMDIChildFrame()
350 wxAuiMDIChildFrame::wxAuiMDIChildFrame(wxAuiMDIParentFrame
*parent
,
352 const wxString
& title
,
353 const wxPoint
& WXUNUSED(pos
),
356 const wxString
& name
)
359 Create(parent
, id
, title
, wxDefaultPosition
, size
, style
, name
);
362 wxAuiMDIChildFrame::~wxAuiMDIChildFrame()
365 wxDELETE(m_pMenuBar
);
366 #endif // wxUSE_MENUS
369 bool wxAuiMDIChildFrame::Create(wxAuiMDIParentFrame
* parent
,
371 const wxString
& title
,
372 const wxPoint
& WXUNUSED(pos
),
375 const wxString
& name
)
377 wxAuiMDIClientWindow
* pClientWindow
= parent
->GetClientWindow();
378 wxASSERT_MSG((pClientWindow
!= (wxWindow
*) NULL
), wxT("Missing MDI client window."));
380 wxPanel::Create(pClientWindow
, id
, wxDefaultPosition
, size
, style
|wxNO_BORDER
, name
);
382 SetMDIParentFrame(parent
);
384 // this is the currently active child
385 parent
->SetActiveChild(this);
389 pClientWindow
->AddPage(this, title
, true);
390 pClientWindow
->Refresh();
395 bool wxAuiMDIChildFrame::Destroy()
397 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
398 wxASSERT_MSG(pParentFrame
, wxT("Missing MDI Parent Frame"));
400 wxAuiMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
401 wxASSERT_MSG(pClientWindow
, wxT("Missing MDI Client Window"));
403 if (pParentFrame
->GetActiveChild() == this)
405 pParentFrame
->SetActiveChild(NULL
);
406 pParentFrame
->SetChildMenuBar(NULL
);
409 const size_t page_count
= pClientWindow
->GetPageCount();
410 for (size_t pos
= 0; pos
< page_count
; pos
++)
412 if (pClientWindow
->GetPage(pos
) == this)
413 return pClientWindow
->DeletePage(pos
);
420 void wxAuiMDIChildFrame::SetMenuBar(wxMenuBar
*menu_bar
)
422 wxMenuBar
*pOldMenuBar
= m_pMenuBar
;
423 m_pMenuBar
= menu_bar
;
427 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
428 wxASSERT_MSG(pParentFrame
, wxT("Missing MDI Parent Frame"));
430 m_pMenuBar
->SetParent(pParentFrame
);
431 if (pParentFrame
->GetActiveChild() == this)
433 // replace current menu bars
435 pParentFrame
->SetChildMenuBar(NULL
);
436 pParentFrame
->SetChildMenuBar(this);
441 wxMenuBar
*wxAuiMDIChildFrame::GetMenuBar() const
445 #endif // wxUSE_MENUS
447 void wxAuiMDIChildFrame::SetTitle(const wxString
& title
)
451 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
452 wxASSERT_MSG(pParentFrame
, wxT("Missing MDI Parent Frame"));
454 wxAuiMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
455 if (pClientWindow
!= NULL
)
458 for (pos
= 0; pos
< pClientWindow
->GetPageCount(); pos
++)
460 if (pClientWindow
->GetPage(pos
) == this)
462 pClientWindow
->SetPageText(pos
, m_title
);
469 wxString
wxAuiMDIChildFrame::GetTitle() const
474 void wxAuiMDIChildFrame::Activate()
476 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
477 wxASSERT_MSG(pParentFrame
, wxT("Missing MDI Parent Frame"));
479 wxAuiMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
481 if (pClientWindow
!= NULL
)
484 for (pos
= 0; pos
< pClientWindow
->GetPageCount(); pos
++)
486 if (pClientWindow
->GetPage(pos
) == this)
488 pClientWindow
->SetSelection(pos
);
495 void wxAuiMDIChildFrame::OnMenuHighlight(wxMenuEvent
& event
)
498 if (m_pMDIParentFrame
)
500 // we don't have any help text for this item,
501 // but may be the MDI frame does?
502 m_pMDIParentFrame
->OnMenuHighlight(event
);
506 #endif // wxUSE_STATUSBAR
509 void wxAuiMDIChildFrame::OnActivate(wxActivateEvent
& WXUNUSED(event
))
514 void wxAuiMDIChildFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
519 void wxAuiMDIChildFrame::SetMDIParentFrame(wxAuiMDIParentFrame
* parentFrame
)
521 m_pMDIParentFrame
= parentFrame
;
524 wxAuiMDIParentFrame
* wxAuiMDIChildFrame::GetMDIParentFrame() const
526 return m_pMDIParentFrame
;
529 void wxAuiMDIChildFrame::Init()
531 m_pMDIParentFrame
= NULL
;
534 #endif // wxUSE_MENUS
537 bool wxAuiMDIChildFrame::Show(bool WXUNUSED(show
))
543 void wxAuiMDIChildFrame::DoShow(bool show
)
545 wxWindow::Show(show
);
548 void wxAuiMDIChildFrame::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
550 m_mdi_newrect
= wxRect(x
, y
, width
, height
);
552 wxPanel::DoSetSize(x
,y
,width
, height
, sizeFlags
);
554 wxUnusedVar(sizeFlags
);
558 void wxAuiMDIChildFrame::DoMoveWindow(int x
, int y
, int width
, int height
)
560 m_mdi_newrect
= wxRect(x
, y
, width
, height
);
563 void wxAuiMDIChildFrame::ApplyMDIChildFrameRect()
565 if (m_mdi_currect
!= m_mdi_newrect
)
567 wxPanel::DoMoveWindow(m_mdi_newrect
.x
, m_mdi_newrect
.y
,
568 m_mdi_newrect
.width
, m_mdi_newrect
.height
);
569 m_mdi_currect
= m_mdi_newrect
;
574 //-----------------------------------------------------------------------------
575 // wxAuiMDIClientWindow
576 //-----------------------------------------------------------------------------
578 IMPLEMENT_DYNAMIC_CLASS(wxAuiMDIClientWindow
, wxAuiNotebook
)
580 BEGIN_EVENT_TABLE(wxAuiMDIClientWindow
, wxAuiNotebook
)
581 EVT_AUINOTEBOOK_PAGE_CHANGED(wxID_ANY
, wxAuiMDIClientWindow::OnPageChanged
)
582 EVT_SIZE(wxAuiMDIClientWindow::OnSize
)
585 wxAuiMDIClientWindow::wxAuiMDIClientWindow()
589 wxAuiMDIClientWindow::wxAuiMDIClientWindow(wxAuiMDIParentFrame
* parent
, long style
)
591 CreateClient(parent
, style
);
594 wxAuiMDIClientWindow::~wxAuiMDIClientWindow()
599 bool wxAuiMDIClientWindow::CreateClient(wxAuiMDIParentFrame
* parent
, long style
)
601 SetWindowStyleFlag(style
);
603 if (!wxAuiNotebook::Create(parent
,
607 wxAUI_NB_DEFAULT_STYLE
| wxNO_BORDER
))
612 wxColour bkcolour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
613 SetBackgroundColour(bkcolour
);
615 m_mgr
.GetArtProvider()->SetColour(wxAUI_ART_BACKGROUND_COLOUR
, bkcolour
);
620 int wxAuiMDIClientWindow::SetSelection(size_t nPage
)
622 return wxAuiNotebook::SetSelection(nPage
);
625 void wxAuiMDIClientWindow::PageChanged(int old_selection
, int new_selection
)
627 // don't do anything if the page doesn't actually change
628 if (old_selection
== new_selection
)
631 // don't do anything if the new page is already active
632 if (new_selection
!= -1)
634 wxAuiMDIChildFrame
* child
= (wxAuiMDIChildFrame
*)GetPage(new_selection
);
635 if (child
->GetMDIParentFrame()->GetActiveChild() == child
)
639 // notify old active child that it has been deactivated
640 if (old_selection
!= -1)
642 wxAuiMDIChildFrame
* old_child
= (wxAuiMDIChildFrame
*)GetPage(old_selection
);
643 wxASSERT_MSG(old_child
, wxT("wxAuiMDIClientWindow::PageChanged - null page pointer"));
645 wxActivateEvent
event(wxEVT_ACTIVATE
, false, old_child
->GetId());
646 event
.SetEventObject(old_child
);
647 old_child
->GetEventHandler()->ProcessEvent(event
);
650 // notify new active child that it has been activated
651 if (new_selection
!= -1)
653 wxAuiMDIChildFrame
* active_child
= (wxAuiMDIChildFrame
*)GetPage(new_selection
);
654 wxASSERT_MSG(active_child
, wxT("wxAuiMDIClientWindow::PageChanged - null page pointer"));
656 wxActivateEvent
event(wxEVT_ACTIVATE
, true, active_child
->GetId());
657 event
.SetEventObject(active_child
);
658 active_child
->GetEventHandler()->ProcessEvent(event
);
660 if (active_child
->GetMDIParentFrame())
662 active_child
->GetMDIParentFrame()->SetActiveChild(active_child
);
663 active_child
->GetMDIParentFrame()->SetChildMenuBar(active_child
);
668 void wxAuiMDIClientWindow::OnPageChanged(wxAuiNotebookEvent
& evt
)
670 PageChanged(evt
.GetOldSelection(), evt
.GetSelection());
674 void wxAuiMDIClientWindow::OnSize(wxSizeEvent
& evt
)
676 wxAuiNotebook::OnSize(evt
);
678 for (size_t pos
= 0; pos
< GetPageCount(); pos
++)
679 ((wxAuiMDIChildFrame
*)GetPage(pos
))->ApplyMDIChildFrameRect();