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
::SetArtProvider(wxAuiTabArt
* provider
)
122 m_pClientWindow
->SetArtProvider(provider
);
126 wxAuiTabArt
* wxAuiMDIParentFrame
::GetArtProvider()
128 if (!m_pClientWindow
)
131 return m_pClientWindow
->GetArtProvider();
134 wxAuiNotebook
* wxAuiMDIParentFrame
::GetNotebook() const
136 return static_cast<wxAuiNotebook
*>(m_pClientWindow
);
142 void wxAuiMDIParentFrame
::SetWindowMenu(wxMenu
* pMenu
)
144 // Replace the window menu from the currently loaded menu bar.
145 wxMenuBar
*pMenuBar
= GetMenuBar();
149 RemoveWindowMenu(pMenuBar
);
150 wxDELETE(m_pWindowMenu
);
155 m_pWindowMenu
= pMenu
;
156 AddWindowMenu(pMenuBar
);
160 void wxAuiMDIParentFrame
::SetMenuBar(wxMenuBar
* pMenuBar
)
162 // Remove the Window menu from the old menu bar
163 RemoveWindowMenu(GetMenuBar());
165 // Add the Window menu to the new menu bar.
166 AddWindowMenu(pMenuBar
);
168 wxFrame
::SetMenuBar(pMenuBar
);
169 m_pMyMenuBar
= GetMenuBar();
171 #endif // wxUSE_MENUS
173 void wxAuiMDIParentFrame
::SetChildMenuBar(wxAuiMDIChildFrame
* pChild
)
178 // No Child, set Our menu bar back.
179 SetMenuBar(m_pMyMenuBar
);
181 // Make sure we know our menu bar is in use
182 //m_pMyMenuBar = NULL;
186 if (pChild
->GetMenuBar() == NULL
)
189 // Do we need to save the current bar?
190 if (m_pMyMenuBar
== NULL
)
191 m_pMyMenuBar
= GetMenuBar();
193 SetMenuBar(pChild
->GetMenuBar());
195 #endif // wxUSE_MENUS
198 bool wxAuiMDIParentFrame
::ProcessEvent(wxEvent
& event
)
200 // stops the same event being processed repeatedly
201 if (m_pLastEvt
== &event
)
205 // let the active child (if any) process the event first.
207 if (m_pActiveChild
&&
208 event
.IsCommandEvent() &&
209 event
.GetEventObject() != m_pClientWindow
&&
210 !(event
.GetEventType() == wxEVT_ACTIVATE
||
211 event
.GetEventType() == wxEVT_SET_FOCUS
||
212 event
.GetEventType() == wxEVT_KILL_FOCUS
||
213 event
.GetEventType() == wxEVT_CHILD_FOCUS
||
214 event
.GetEventType() == wxEVT_COMMAND_SET_FOCUS
||
215 event
.GetEventType() == wxEVT_COMMAND_KILL_FOCUS
)
218 res
= m_pActiveChild
->GetEventHandler()->ProcessEvent(event
);
223 // if the event was not handled this frame will handle it,
224 // which is why we need the protection code at the beginning
226 res
= wxEvtHandler
::ProcessEvent(event
);
234 wxAuiMDIChildFrame
*wxAuiMDIParentFrame
::GetActiveChild() const
236 return m_pActiveChild
;
239 void wxAuiMDIParentFrame
::SetActiveChild(wxAuiMDIChildFrame
* pChildFrame
)
241 m_pActiveChild
= pChildFrame
;
244 wxAuiMDIClientWindow
*wxAuiMDIParentFrame
::GetClientWindow() const
246 return m_pClientWindow
;
249 wxAuiMDIClientWindow
*wxAuiMDIParentFrame
::OnCreateClient()
251 m_pClientWindow
= new wxAuiMDIClientWindow( this );
252 return m_pClientWindow
;
255 void wxAuiMDIParentFrame
::ActivateNext()
257 if (m_pClientWindow
&& m_pClientWindow
->GetSelection() != wxNOT_FOUND
)
259 size_t active
= m_pClientWindow
->GetSelection() + 1;
260 if (active
>= m_pClientWindow
->GetPageCount())
263 m_pClientWindow
->SetSelection(active
);
267 void wxAuiMDIParentFrame
::ActivatePrevious()
269 if (m_pClientWindow
&& m_pClientWindow
->GetSelection() != wxNOT_FOUND
)
271 int active
= m_pClientWindow
->GetSelection() - 1;
273 active
= m_pClientWindow
->GetPageCount() - 1;
275 m_pClientWindow
->SetSelection(active
);
279 void wxAuiMDIParentFrame
::Init()
282 m_pClientWindow
= NULL
;
283 m_pActiveChild
= NULL
;
285 m_pWindowMenu
= NULL
;
287 #endif // wxUSE_MENUS
291 void wxAuiMDIParentFrame
::RemoveWindowMenu(wxMenuBar
* pMenuBar
)
293 if (pMenuBar
&& m_pWindowMenu
)
295 // Remove old window menu
296 int pos
= pMenuBar
->FindMenu(_("&Window"));
297 if (pos
!= wxNOT_FOUND
)
299 // DBG:: We're going to delete the wrong menu!!!
300 wxASSERT(m_pWindowMenu
== pMenuBar
->GetMenu(pos
));
301 pMenuBar
->Remove(pos
);
306 void wxAuiMDIParentFrame
::AddWindowMenu(wxMenuBar
*pMenuBar
)
308 if (pMenuBar
&& m_pWindowMenu
)
310 int pos
= pMenuBar
->FindMenu(wxGetStockLabel(wxID_HELP
,wxSTOCK_NOFLAGS
));
311 if (pos
== wxNOT_FOUND
)
312 pMenuBar
->Append(m_pWindowMenu
, _("&Window"));
314 pMenuBar
->Insert(pos
, m_pWindowMenu
, _("&Window"));
318 void wxAuiMDIParentFrame
::DoHandleMenu(wxCommandEvent
& event
)
320 switch (event
.GetId())
324 m_pActiveChild
->Close();
326 case wxWINDOWCLOSEALL
:
327 while (m_pActiveChild
)
329 if (!m_pActiveChild
->Close())
335 delete m_pActiveChild
;
336 m_pActiveChild
= NULL
;
350 #endif // wxUSE_MENUS
352 void wxAuiMDIParentFrame
::DoGetClientSize(int* width
, int* height
) const
354 wxFrame
::DoGetClientSize(width
, height
);
357 //-----------------------------------------------------------------------------
358 // wxAuiMDIChildFrame
359 //-----------------------------------------------------------------------------
361 IMPLEMENT_DYNAMIC_CLASS(wxAuiMDIChildFrame
, wxPanel
)
363 BEGIN_EVENT_TABLE(wxAuiMDIChildFrame
, wxPanel
)
364 EVT_MENU_HIGHLIGHT_ALL(wxAuiMDIChildFrame
::OnMenuHighlight
)
365 EVT_ACTIVATE(wxAuiMDIChildFrame
::OnActivate
)
366 EVT_CLOSE(wxAuiMDIChildFrame
::OnCloseWindow
)
369 wxAuiMDIChildFrame
::wxAuiMDIChildFrame()
374 wxAuiMDIChildFrame
::wxAuiMDIChildFrame(wxAuiMDIParentFrame
*parent
,
376 const wxString
& title
,
377 const wxPoint
& WXUNUSED(pos
),
380 const wxString
& name
)
383 Create(parent
, id
, title
, wxDefaultPosition
, size
, style
, name
);
386 wxAuiMDIChildFrame
::~wxAuiMDIChildFrame()
389 wxDELETE(m_pMenuBar
);
390 #endif // wxUSE_MENUS
393 bool wxAuiMDIChildFrame
::Create(wxAuiMDIParentFrame
* parent
,
395 const wxString
& title
,
396 const wxPoint
& WXUNUSED(pos
),
399 const wxString
& name
)
401 wxAuiMDIClientWindow
* pClientWindow
= parent
->GetClientWindow();
402 wxASSERT_MSG((pClientWindow
!= (wxWindow
*) NULL
), wxT("Missing MDI client window."));
404 wxSize cli_size
= pClientWindow
->GetClientSize();
406 // create the window off-screen to prevent flicker
407 wxPanel
::Create(pClientWindow
,
409 wxPoint(cli_size
.x
+1, cli_size
.y
+1),
411 style
|wxNO_BORDER
, name
);
413 SetMDIParentFrame(parent
);
415 // this is the currently active child
416 parent
->SetActiveChild(this);
420 pClientWindow
->AddPage(this, title
, true);
421 pClientWindow
->Refresh();
426 bool wxAuiMDIChildFrame
::Destroy()
428 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
429 wxASSERT_MSG(pParentFrame
, wxT("Missing MDI Parent Frame"));
431 wxAuiMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
432 wxASSERT_MSG(pClientWindow
, wxT("Missing MDI Client Window"));
434 if (pParentFrame
->GetActiveChild() == this)
436 pParentFrame
->SetActiveChild(NULL
);
437 pParentFrame
->SetChildMenuBar(NULL
);
440 const size_t page_count
= pClientWindow
->GetPageCount();
441 for (size_t pos
= 0; pos
< page_count
; pos
++)
443 if (pClientWindow
->GetPage(pos
) == this)
444 return pClientWindow
->DeletePage(pos
);
451 void wxAuiMDIChildFrame
::SetMenuBar(wxMenuBar
*menu_bar
)
453 wxMenuBar
*pOldMenuBar
= m_pMenuBar
;
454 m_pMenuBar
= menu_bar
;
458 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
459 wxASSERT_MSG(pParentFrame
, wxT("Missing MDI Parent Frame"));
461 m_pMenuBar
->SetParent(pParentFrame
);
462 if (pParentFrame
->GetActiveChild() == this)
464 // replace current menu bars
466 pParentFrame
->SetChildMenuBar(NULL
);
467 pParentFrame
->SetChildMenuBar(this);
472 wxMenuBar
*wxAuiMDIChildFrame
::GetMenuBar() const
476 #endif // wxUSE_MENUS
478 void wxAuiMDIChildFrame
::SetTitle(const wxString
& title
)
482 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
483 wxASSERT_MSG(pParentFrame
, wxT("Missing MDI Parent Frame"));
485 wxAuiMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
486 if (pClientWindow
!= NULL
)
489 for (pos
= 0; pos
< pClientWindow
->GetPageCount(); pos
++)
491 if (pClientWindow
->GetPage(pos
) == this)
493 pClientWindow
->SetPageText(pos
, m_title
);
500 wxString wxAuiMDIChildFrame
::GetTitle() const
505 void wxAuiMDIChildFrame
::SetIcons(const wxIconBundle
& icons
)
507 // get icon with the system icon size
508 SetIcon(icons
.GetIcon(-1));
509 m_icon_bundle
= icons
;
512 const wxIconBundle
& wxAuiMDIChildFrame
::GetIcons() const
514 return m_icon_bundle
;
517 void wxAuiMDIChildFrame
::SetIcon(const wxIcon
& icon
)
519 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
520 wxASSERT_MSG(pParentFrame
, wxT("Missing MDI Parent Frame"));
525 bmp
.CopyFromIcon(m_icon
);
527 wxAuiMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
528 if (pClientWindow
!= NULL
)
530 int idx
= pClientWindow
->GetPageIndex(this);
534 pClientWindow
->SetPageBitmap((size_t)idx
, bmp
);
539 const wxIcon
& wxAuiMDIChildFrame
::GetIcon() const
545 void wxAuiMDIChildFrame
::Activate()
547 wxAuiMDIParentFrame
* pParentFrame
= GetMDIParentFrame();
548 wxASSERT_MSG(pParentFrame
, wxT("Missing MDI Parent Frame"));
550 wxAuiMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
552 if (pClientWindow
!= NULL
)
555 for (pos
= 0; pos
< pClientWindow
->GetPageCount(); pos
++)
557 if (pClientWindow
->GetPage(pos
) == this)
559 pClientWindow
->SetSelection(pos
);
566 void wxAuiMDIChildFrame
::OnMenuHighlight(wxMenuEvent
& event
)
569 if (m_pMDIParentFrame
)
571 // we don't have any help text for this item,
572 // but may be the MDI frame does?
573 m_pMDIParentFrame
->OnMenuHighlight(event
);
577 #endif // wxUSE_STATUSBAR
580 void wxAuiMDIChildFrame
::OnActivate(wxActivateEvent
& WXUNUSED(event
))
585 void wxAuiMDIChildFrame
::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
590 void wxAuiMDIChildFrame
::SetMDIParentFrame(wxAuiMDIParentFrame
* parentFrame
)
592 m_pMDIParentFrame
= parentFrame
;
595 wxAuiMDIParentFrame
* wxAuiMDIChildFrame
::GetMDIParentFrame() const
597 return m_pMDIParentFrame
;
600 void wxAuiMDIChildFrame
::Init()
602 m_pMDIParentFrame
= NULL
;
605 #endif // wxUSE_MENUS
608 bool wxAuiMDIChildFrame
::Show(bool WXUNUSED(show
))
614 void wxAuiMDIChildFrame
::DoShow(bool show
)
616 wxWindow
::Show(show
);
619 void wxAuiMDIChildFrame
::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
621 m_mdi_newrect
= wxRect(x
, y
, width
, height
);
623 wxPanel
::DoSetSize(x
,y
,width
, height
, sizeFlags
);
625 wxUnusedVar(sizeFlags
);
629 void wxAuiMDIChildFrame
::DoMoveWindow(int x
, int y
, int width
, int height
)
631 m_mdi_newrect
= wxRect(x
, y
, width
, height
);
634 void wxAuiMDIChildFrame
::ApplyMDIChildFrameRect()
636 if (m_mdi_currect
!= m_mdi_newrect
)
638 wxPanel
::DoMoveWindow(m_mdi_newrect
.x
, m_mdi_newrect
.y
,
639 m_mdi_newrect
.width
, m_mdi_newrect
.height
);
640 m_mdi_currect
= m_mdi_newrect
;
645 //-----------------------------------------------------------------------------
646 // wxAuiMDIClientWindow
647 //-----------------------------------------------------------------------------
649 IMPLEMENT_DYNAMIC_CLASS(wxAuiMDIClientWindow
, wxAuiNotebook
)
651 BEGIN_EVENT_TABLE(wxAuiMDIClientWindow
, wxAuiNotebook
)
652 EVT_AUINOTEBOOK_PAGE_CHANGED(wxID_ANY
, wxAuiMDIClientWindow
::OnPageChanged
)
653 EVT_SIZE(wxAuiMDIClientWindow
::OnSize
)
656 wxAuiMDIClientWindow
::wxAuiMDIClientWindow()
660 wxAuiMDIClientWindow
::wxAuiMDIClientWindow(wxAuiMDIParentFrame
* parent
, long style
)
662 CreateClient(parent
, style
);
665 wxAuiMDIClientWindow
::~wxAuiMDIClientWindow()
670 bool wxAuiMDIClientWindow
::CreateClient(wxAuiMDIParentFrame
* parent
, long style
)
672 SetWindowStyleFlag(style
);
674 if (!wxAuiNotebook
::Create(parent
,
678 wxAUI_NB_DEFAULT_STYLE
| wxNO_BORDER
))
683 wxColour bkcolour
= wxSystemSettings
::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
684 SetBackgroundColour(bkcolour
);
686 m_mgr
.GetArtProvider()->SetColour(wxAUI_ART_BACKGROUND_COLOUR
, bkcolour
);
691 int wxAuiMDIClientWindow
::SetSelection(size_t nPage
)
693 return wxAuiNotebook
::SetSelection(nPage
);
696 void wxAuiMDIClientWindow
::PageChanged(int old_selection
, int new_selection
)
698 // don't do anything if the page doesn't actually change
699 if (old_selection
== new_selection
)
702 // don't do anything if the new page is already active
703 if (new_selection
!= -1)
705 wxAuiMDIChildFrame
* child
= (wxAuiMDIChildFrame
*)GetPage(new_selection
);
706 if (child
->GetMDIParentFrame()->GetActiveChild() == child
)
710 // notify old active child that it has been deactivated
711 if (old_selection
!= -1)
713 wxAuiMDIChildFrame
* old_child
= (wxAuiMDIChildFrame
*)GetPage(old_selection
);
714 wxASSERT_MSG(old_child
, wxT("wxAuiMDIClientWindow::PageChanged - null page pointer"));
716 wxActivateEvent
event(wxEVT_ACTIVATE
, false, old_child
->GetId());
717 event
.SetEventObject(old_child
);
718 old_child
->GetEventHandler()->ProcessEvent(event
);
721 // notify new active child that it has been activated
722 if (new_selection
!= -1)
724 wxAuiMDIChildFrame
* active_child
= (wxAuiMDIChildFrame
*)GetPage(new_selection
);
725 wxASSERT_MSG(active_child
, wxT("wxAuiMDIClientWindow::PageChanged - null page pointer"));
727 wxActivateEvent
event(wxEVT_ACTIVATE
, true, active_child
->GetId());
728 event
.SetEventObject(active_child
);
729 active_child
->GetEventHandler()->ProcessEvent(event
);
731 if (active_child
->GetMDIParentFrame())
733 active_child
->GetMDIParentFrame()->SetActiveChild(active_child
);
734 active_child
->GetMDIParentFrame()->SetChildMenuBar(active_child
);
739 void wxAuiMDIClientWindow
::OnPageChanged(wxAuiNotebookEvent
& evt
)
741 PageChanged(evt
.GetOldSelection(), evt
.GetSelection());
745 void wxAuiMDIClientWindow
::OnSize(wxSizeEvent
& evt
)
747 wxAuiNotebook
::OnSize(evt
);
749 for (size_t pos
= 0; pos
< GetPageCount(); pos
++)
750 ((wxAuiMDIChildFrame
*)GetPage(pos
))->ApplyMDIChildFrameRect();