1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/mdig.cpp
3 // Purpose: Generic MDI (Multiple Document Interface) classes
4 // Author: Hans Van Leemputten
8 // Copyright: (c) Hans Van Leemputten
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "mdig.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
37 #include "wx/generic/mdig.h"
38 #include "wx/stockitem.h"
48 //-----------------------------------------------------------------------------
49 // wxGenericMDIParentFrame
50 //-----------------------------------------------------------------------------
52 IMPLEMENT_DYNAMIC_CLASS(wxGenericMDIParentFrame
, wxFrame
)
54 BEGIN_EVENT_TABLE(wxGenericMDIParentFrame
, wxFrame
)
56 EVT_MENU (wxID_ANY
, wxGenericMDIParentFrame::DoHandleMenu
)
60 wxGenericMDIParentFrame::wxGenericMDIParentFrame()
65 wxGenericMDIParentFrame::wxGenericMDIParentFrame(wxWindow
*parent
,
67 const wxString
& title
,
75 (void)Create(parent
, id
, title
, pos
, size
, style
, name
);
78 wxGenericMDIParentFrame::~wxGenericMDIParentFrame()
80 // Make sure the client window is destructed before the menu bars are!
81 wxDELETE(m_pClientWindow
);
87 m_pMyMenuBar
= (wxMenuBar
*) NULL
;
90 RemoveWindowMenu(GetMenuBar());
95 m_pWindowMenu
= (wxMenu
*) NULL
;
100 bool wxGenericMDIParentFrame::Create(wxWindow
*parent
,
102 const wxString
& title
,
106 const wxString
& name
)
108 // this style can be used to prevent a window from having the standard MDI
110 if ( !(style
& wxFRAME_NO_WINDOW_MENU
) )
113 m_pWindowMenu
= new wxMenu
;
115 m_pWindowMenu
->Append(wxWINDOWCLOSE
, _("Cl&ose"));
116 m_pWindowMenu
->Append(wxWINDOWCLOSEALL
, _("Close All"));
117 m_pWindowMenu
->AppendSeparator();
118 m_pWindowMenu
->Append(wxWINDOWNEXT
, _("&Next"));
119 m_pWindowMenu
->Append(wxWINDOWPREV
, _("&Previous"));
120 #endif // wxUSE_MENUS
123 wxFrame::Create( parent
, id
, title
, pos
, size
, style
, name
);
131 void wxGenericMDIParentFrame::SetWindowMenu(wxMenu
* pMenu
)
133 // Replace the window menu from the currently loaded menu bar.
134 wxMenuBar
*pMenuBar
= GetMenuBar();
138 RemoveWindowMenu(pMenuBar
);
140 wxDELETE(m_pWindowMenu
);
145 m_pWindowMenu
= pMenu
;
147 AddWindowMenu(pMenuBar
);
151 void wxGenericMDIParentFrame::SetMenuBar(wxMenuBar
*pMenuBar
)
153 // Remove the Window menu from the old menu bar
154 RemoveWindowMenu(GetMenuBar());
155 // Add the Window menu to the new menu bar.
156 AddWindowMenu(pMenuBar
);
158 wxFrame::SetMenuBar(pMenuBar
);
160 #endif // wxUSE_MENUS
162 void wxGenericMDIParentFrame::SetChildMenuBar(wxGenericMDIChildFrame
*pChild
)
165 if (pChild
== (wxGenericMDIChildFrame
*) NULL
)
167 // No Child, set Our menu bar back.
168 SetMenuBar(m_pMyMenuBar
);
170 // Make sure we know our menu bar is in use
171 m_pMyMenuBar
= (wxMenuBar
*) NULL
;
175 if (pChild
->GetMenuBar() == (wxMenuBar
*) NULL
)
178 // Do we need to save the current bar?
179 if (m_pMyMenuBar
== NULL
)
180 m_pMyMenuBar
= GetMenuBar();
182 SetMenuBar(pChild
->GetMenuBar());
184 #endif // wxUSE_MENUS
187 bool wxGenericMDIParentFrame::ProcessEvent(wxEvent
& event
)
190 * Redirect events to active child first.
193 // Stops the same event being processed repeatedly
194 static wxEventType inEvent
= wxEVT_NULL
;
195 if (inEvent
== event
.GetEventType())
198 inEvent
= event
.GetEventType();
200 // Let the active child (if any) process the event first.
202 if (m_pActiveChild
&& event
.IsKindOf(CLASSINFO(wxCommandEvent
))
204 /* This is sure to not give problems... */
205 && (event
.GetEventType() == wxEVT_COMMAND_MENU_SELECTED
||
206 event
.GetEventType() == wxEVT_UPDATE_UI
)
208 /* This was tested on wxMSW and worked... */
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
)
219 res
= m_pActiveChild
->GetEventHandler()->ProcessEvent(event
);
222 // If the event was not handled this frame will handle it!
225 res
= GetEventHandler()->wxEvtHandler::ProcessEvent(event
);
228 inEvent
= wxEVT_NULL
;
233 wxGenericMDIChildFrame
*wxGenericMDIParentFrame::GetActiveChild() const
235 return m_pActiveChild
;
238 void wxGenericMDIParentFrame::SetActiveChild(wxGenericMDIChildFrame
* pChildFrame
)
240 m_pActiveChild
= pChildFrame
;
243 wxGenericMDIClientWindow
*wxGenericMDIParentFrame::GetClientWindow() const
245 return m_pClientWindow
;
248 wxGenericMDIClientWindow
*wxGenericMDIParentFrame::OnCreateClient()
250 #if wxUSE_GENERIC_MDI_AS_NATIVE
251 m_pClientWindow
= new wxMDIClientWindow( this );
253 m_pClientWindow
= new wxGenericMDIClientWindow( this );
255 return m_pClientWindow
;
258 void wxGenericMDIParentFrame::ActivateNext()
260 if (m_pClientWindow
&& m_pClientWindow
->GetSelection() != -1)
262 size_t active
= m_pClientWindow
->GetSelection() + 1;
263 if (active
>= m_pClientWindow
->GetPageCount())
266 m_pClientWindow
->SetSelection(active
);
270 void wxGenericMDIParentFrame::ActivatePrevious()
272 if (m_pClientWindow
&& m_pClientWindow
->GetSelection() != -1)
274 int active
= m_pClientWindow
->GetSelection() - 1;
276 active
= m_pClientWindow
->GetPageCount() - 1;
278 m_pClientWindow
->SetSelection(active
);
282 void wxGenericMDIParentFrame::Init()
284 m_pClientWindow
= (wxGenericMDIClientWindow
*) NULL
;
285 m_pActiveChild
= (wxGenericMDIChildFrame
*) NULL
;
287 m_pWindowMenu
= (wxMenu
*) NULL
;
288 m_pMyMenuBar
= (wxMenuBar
*) NULL
;
289 #endif // wxUSE_MENUS
293 void wxGenericMDIParentFrame::RemoveWindowMenu(wxMenuBar
*pMenuBar
)
295 if (pMenuBar
&& m_pWindowMenu
)
297 // Remove old window menu
298 int pos
= pMenuBar
->FindMenu(_("&Window"));
299 if (pos
!= wxNOT_FOUND
)
301 wxASSERT(m_pWindowMenu
== pMenuBar
->GetMenu(pos
)); // DBG:: We're going to delete the wrong menu!!!
302 pMenuBar
->Remove(pos
);
307 void wxGenericMDIParentFrame::AddWindowMenu(wxMenuBar
*pMenuBar
)
309 if (pMenuBar
&& m_pWindowMenu
)
311 int pos
= pMenuBar
->FindMenu(wxGetStockLabel(wxID_HELP
,false));
312 if (pos
== wxNOT_FOUND
)
314 pMenuBar
->Append(m_pWindowMenu
, _("&Window"));
318 pMenuBar
->Insert(pos
, m_pWindowMenu
, _("&Window"));
323 void wxGenericMDIParentFrame::DoHandleMenu(wxCommandEvent
&event
)
325 switch (event
.GetId())
330 m_pActiveChild
->Close();
333 case wxWINDOWCLOSEALL
:
335 #if 0 // code is only needed if next #if is set to 0!
336 wxGenericMDIChildFrame
*pFirstActiveChild
= m_pActiveChild
;
338 while (m_pActiveChild
)
340 if (!m_pActiveChild
->Close())
342 return; // We failed...
346 #if 1 // What's best? Delayed deleting or immediate deleting?
347 delete m_pActiveChild
;
348 m_pActiveChild
= NULL
;
352 if (pFirstActiveChild
== m_pActiveChild
)
353 return; // We've called Close on all items, no need to continue.
369 #endif // wxUSE_MENUS
371 void wxGenericMDIParentFrame::DoGetClientSize(int *width
, int *height
) const
373 wxFrame::DoGetClientSize( width
, height
);
377 //-----------------------------------------------------------------------------
378 // wxGenericMDIChildFrame
379 //-----------------------------------------------------------------------------
381 IMPLEMENT_DYNAMIC_CLASS(wxGenericMDIChildFrame
, wxPanel
)
383 BEGIN_EVENT_TABLE(wxGenericMDIChildFrame
, wxPanel
)
384 EVT_MENU_HIGHLIGHT_ALL(wxGenericMDIChildFrame::OnMenuHighlight
)
385 EVT_ACTIVATE(wxGenericMDIChildFrame::OnActivate
)
387 EVT_CLOSE(wxGenericMDIChildFrame::OnCloseWindow
)
388 EVT_SIZE(wxGenericMDIChildFrame::OnSize
)
391 wxGenericMDIChildFrame::wxGenericMDIChildFrame()
396 wxGenericMDIChildFrame::wxGenericMDIChildFrame( wxGenericMDIParentFrame
*parent
,
397 wxWindowID id
, const wxString
& title
,
398 const wxPoint
& WXUNUSED(pos
), const wxSize
& size
,
399 long style
, const wxString
& name
)
403 Create( parent
, id
, title
, wxDefaultPosition
, size
, style
, name
);
407 wxGenericMDIChildFrame::~wxGenericMDIChildFrame()
409 wxGenericMDIParentFrame
*pParentFrame
= GetMDIParentFrame();
411 if (pParentFrame
!= NULL
)
413 bool bActive
= false;
414 if (pParentFrame
->GetActiveChild() == this)
416 pParentFrame
->SetActiveChild((wxGenericMDIChildFrame
*) NULL
);
417 pParentFrame
->SetChildMenuBar((wxGenericMDIChildFrame
*) NULL
);
421 wxGenericMDIClientWindow
*pClientWindow
= pParentFrame
->GetClientWindow();
423 // Remove page if still there
425 for (pos
= 0; pos
< pClientWindow
->GetPageCount(); pos
++)
427 if (pClientWindow
->GetPage(pos
) == this)
429 if (pClientWindow
->RemovePage(pos
))
430 pClientWindow
->Refresh();
437 // Set the new selection to the a remaining page
438 if (pClientWindow
->GetPageCount() > pos
)
440 pClientWindow
->SetSelection(pos
);
444 if ((int)pClientWindow
->GetPageCount() - 1 >= 0)
445 pClientWindow
->SetSelection(pClientWindow
->GetPageCount() - 1);
451 wxDELETE(m_pMenuBar
);
452 #endif // wxUSE_MENUS
455 bool wxGenericMDIChildFrame::Create( wxGenericMDIParentFrame
*parent
,
456 wxWindowID id
, const wxString
& title
,
457 const wxPoint
& WXUNUSED(pos
), const wxSize
& size
,
458 long style
, const wxString
& name
)
460 wxGenericMDIClientWindow
* pClientWindow
= parent
->GetClientWindow();
462 wxASSERT_MSG((pClientWindow
!= (wxWindow
*) NULL
), wxT("Missing MDI client window.") );
464 wxPanel::Create(pClientWindow
, id
, wxDefaultPosition
, size
, style
, name
);
466 SetMDIParentFrame(parent
);
468 // This is the currently active child
469 parent
->SetActiveChild(this);
473 pClientWindow
->AddPage(this, title
, true);
474 ApplyMDIChildFrameRect(); // Ok confirme the size change!
475 pClientWindow
->Refresh();
481 void wxGenericMDIChildFrame::SetMenuBar( wxMenuBar
*menu_bar
)
483 wxMenuBar
*pOldMenuBar
= m_pMenuBar
;
484 m_pMenuBar
= menu_bar
;
488 wxGenericMDIParentFrame
*pParentFrame
= GetMDIParentFrame();
490 if (pParentFrame
!= NULL
)
492 m_pMenuBar
->SetParent(pParentFrame
);
494 if (pParentFrame
->GetActiveChild() == this)
496 // Replace current menu bars
498 pParentFrame
->SetChildMenuBar((wxGenericMDIChildFrame
*) NULL
);
499 pParentFrame
->SetChildMenuBar((wxGenericMDIChildFrame
*) this);
505 wxMenuBar
*wxGenericMDIChildFrame::GetMenuBar() const
509 #endif // wxUSE_MENUS
511 void wxGenericMDIChildFrame::SetTitle(const wxString
& title
)
515 wxGenericMDIParentFrame
*pParentFrame
= GetMDIParentFrame();
517 if (pParentFrame
!= NULL
)
519 wxGenericMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
521 if (pClientWindow
!= NULL
)
524 for (pos
= 0; pos
< pClientWindow
->GetPageCount(); pos
++)
526 if (pClientWindow
->GetPage(pos
) == this)
528 pClientWindow
->SetPageText(pos
, m_Title
);
536 wxString
wxGenericMDIChildFrame::GetTitle() const
541 void wxGenericMDIChildFrame::Activate()
543 wxGenericMDIParentFrame
*pParentFrame
= GetMDIParentFrame();
545 if (pParentFrame
!= NULL
)
547 wxGenericMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
549 if (pClientWindow
!= NULL
)
552 for (pos
= 0; pos
< pClientWindow
->GetPageCount(); pos
++)
554 if (pClientWindow
->GetPage(pos
) == this)
556 pClientWindow
->SetSelection(pos
);
564 void wxGenericMDIChildFrame::OnMenuHighlight(wxMenuEvent
& event
)
567 if ( m_pMDIParentFrame
)
569 // we don't have any help text for this item,
570 // but may be the MDI frame does?
571 m_pMDIParentFrame
->OnMenuHighlight(event
);
575 #endif // wxUSE_STATUSBAR
578 void wxGenericMDIChildFrame::OnActivate(wxActivateEvent
& WXUNUSED(event
))
583 /*** Copied from top level..! ***/
584 // default resizing behaviour - if only ONE subwindow, resize to fill the
586 void wxGenericMDIChildFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
588 // if we're using constraints or sizers - do use them
589 if ( GetAutoLayout() )
595 // do we have _exactly_ one child?
596 wxWindow
*child
= (wxWindow
*)NULL
;
597 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
599 node
= node
->GetNext() )
601 wxWindow
*win
= node
->GetData();
603 // exclude top level and managed windows (status bar isn't
604 // currently in the children list except under wxMac anyhow, but
605 // it makes no harm to test for it)
606 if ( !win
->IsTopLevel() /*&& !IsOneOfBars(win)*/ )
610 return; // it's our second subwindow - nothing to do
617 // do we have any children at all?
620 // exactly one child - set it's size to fill the whole frame
621 int clientW
, clientH
;
622 DoGetClientSize(&clientW
, &clientH
);
624 // for whatever reasons, wxGTK wants to have a small offset - it
625 // probably looks better with it?
627 static const int ofs
= 1;
629 static const int ofs
= 0;
632 child
->SetSize(ofs
, ofs
, clientW
- 2*ofs
, clientH
- 2*ofs
);
637 /*** Copied from top level..! ***/
638 // The default implementation for the close window event.
639 void wxGenericMDIChildFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
644 void wxGenericMDIChildFrame::SetMDIParentFrame(wxGenericMDIParentFrame
* parentFrame
)
646 m_pMDIParentFrame
= parentFrame
;
649 wxGenericMDIParentFrame
* wxGenericMDIChildFrame::GetMDIParentFrame() const
651 return m_pMDIParentFrame
;
654 void wxGenericMDIChildFrame::Init()
656 m_pMDIParentFrame
= (wxGenericMDIParentFrame
*) NULL
;
658 m_pMenuBar
= (wxMenuBar
*) NULL
;
659 #endif // wxUSE_MENUS
662 void wxGenericMDIChildFrame::DoMoveWindow(int x
, int y
, int width
, int height
)
664 m_MDIRect
= wxRect(x
, y
, width
, height
);
667 void wxGenericMDIChildFrame::ApplyMDIChildFrameRect()
669 wxPanel::DoMoveWindow(m_MDIRect
.x
, m_MDIRect
.y
, m_MDIRect
.width
, m_MDIRect
.height
);
672 //-----------------------------------------------------------------------------
673 // wxGenericMDIClientWindow
674 //-----------------------------------------------------------------------------
676 #define wxID_NOTEBOOK_CLIENT_AREA wxID_HIGHEST + 100
678 IMPLEMENT_DYNAMIC_CLASS(wxGenericMDIClientWindow
, wxNotebook
)
680 BEGIN_EVENT_TABLE(wxGenericMDIClientWindow
, wxNotebook
)
681 EVT_NOTEBOOK_PAGE_CHANGED(wxID_NOTEBOOK_CLIENT_AREA
, wxGenericMDIClientWindow::OnPageChanged
)
682 EVT_SIZE(wxGenericMDIClientWindow::OnSize
)
686 wxGenericMDIClientWindow::wxGenericMDIClientWindow()
690 wxGenericMDIClientWindow::wxGenericMDIClientWindow( wxGenericMDIParentFrame
*parent
, long style
)
692 CreateClient( parent
, style
);
695 wxGenericMDIClientWindow::~wxGenericMDIClientWindow()
700 bool wxGenericMDIClientWindow::CreateClient( wxGenericMDIParentFrame
*parent
, long style
)
702 SetWindowStyleFlag(style
);
704 bool success
= wxNotebook::Create(parent
, wxID_NOTEBOOK_CLIENT_AREA
, wxPoint(0,0), wxSize(100, 100), 0);
708 wxFont font(10, wxSWISS, wxNORMAL, wxNORMAL);
709 wxFont selFont(10, wxSWISS, wxNORMAL, wxBOLD);
710 GetTabView()->SetTabFont(font);
711 GetTabView()->SetSelectedTabFont(selFont);
712 GetTabView()->SetTabSize(120, 18);
713 GetTabView()->SetTabSelectionHeight(20);
721 int wxGenericMDIClientWindow::SetSelection(size_t nPage
)
723 int oldSelection
= wxNotebook::SetSelection(nPage
);
725 #if !defined(__WXMSW__) // No need to do this for wxMSW as wxNotebook::SetSelection()
726 // will already cause this to be done!
727 // Handle the page change.
728 PageChanged(oldSelection
, nPage
);
734 void wxGenericMDIClientWindow::PageChanged(int OldSelection
, int newSelection
)
736 // Don't do to much work, only when something realy should change!
737 if (OldSelection
== newSelection
)
739 // Again check if we realy need to do this...
740 if (newSelection
!= -1)
742 wxGenericMDIChildFrame
* child
= (wxGenericMDIChildFrame
*)GetPage(newSelection
);
744 if (child
->GetMDIParentFrame()->GetActiveChild() == child
)
748 // Notify old active child that it has been deactivated
749 if (OldSelection
!= -1)
751 wxGenericMDIChildFrame
* oldChild
= (wxGenericMDIChildFrame
*)GetPage(OldSelection
);
754 wxActivateEvent
event(wxEVT_ACTIVATE
, false, oldChild
->GetId());
755 event
.SetEventObject( oldChild
);
756 oldChild
->GetEventHandler()->ProcessEvent(event
);
760 // Notify new active child that it has been activated
761 if (newSelection
!= -1)
763 wxGenericMDIChildFrame
* activeChild
= (wxGenericMDIChildFrame
*)GetPage(newSelection
);
766 wxActivateEvent
event(wxEVT_ACTIVATE
, true, activeChild
->GetId());
767 event
.SetEventObject( activeChild
);
768 activeChild
->GetEventHandler()->ProcessEvent(event
);
770 if (activeChild
->GetMDIParentFrame())
772 activeChild
->GetMDIParentFrame()->SetActiveChild(activeChild
);
773 activeChild
->GetMDIParentFrame()->SetChildMenuBar(activeChild
);
779 void wxGenericMDIClientWindow::OnPageChanged(wxNotebookEvent
& event
)
781 PageChanged(event
.GetOldSelection(), event
.GetSelection());
786 void wxGenericMDIClientWindow::OnSize(wxSizeEvent
& event
)
788 wxNotebook::OnSize(event
);
791 for (pos
= 0; pos
< GetPageCount(); pos
++)
793 ((wxGenericMDIChildFrame
*)GetPage(pos
))->ApplyMDIChildFrameRect();
799 * Define normal wxMDI classes based on wxGenericMDI
802 #if wxUSE_GENERIC_MDI_AS_NATIVE
804 wxMDIChildFrame
* wxMDIParentFrame::GetActiveChild() const
806 wxGenericMDIChildFrame
*pGFrame
= wxGenericMDIParentFrame::GetActiveChild();
807 wxMDIChildFrame
*pFrame
= wxDynamicCast(pGFrame
, wxMDIChildFrame
);
809 wxASSERT_MSG(!(pFrame
== NULL
&& pGFrame
!= NULL
), wxT("Active frame is class not derived from wxMDIChildFrame!"));
814 IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame
, wxGenericMDIParentFrame
)
815 IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame
, wxGenericMDIChildFrame
)
816 IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow
, wxGenericMDIClientWindow
)