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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/generic/mdig.h"
36 #include "wx/stockitem.h"
46 //-----------------------------------------------------------------------------
47 // wxGenericMDIParentFrame
48 //-----------------------------------------------------------------------------
50 IMPLEMENT_DYNAMIC_CLASS(wxGenericMDIParentFrame
, wxFrame
)
52 BEGIN_EVENT_TABLE(wxGenericMDIParentFrame
, wxFrame
)
54 EVT_MENU (wxID_ANY
, wxGenericMDIParentFrame::DoHandleMenu
)
58 wxGenericMDIParentFrame::wxGenericMDIParentFrame()
63 wxGenericMDIParentFrame::wxGenericMDIParentFrame(wxWindow
*parent
,
65 const wxString
& title
,
73 (void)Create(parent
, id
, title
, pos
, size
, style
, name
);
76 wxGenericMDIParentFrame::~wxGenericMDIParentFrame()
78 // Make sure the client window is destructed before the menu bars are!
79 wxDELETE(m_pClientWindow
);
85 m_pMyMenuBar
= (wxMenuBar
*) NULL
;
88 RemoveWindowMenu(GetMenuBar());
93 m_pWindowMenu
= (wxMenu
*) NULL
;
98 bool wxGenericMDIParentFrame::Create(wxWindow
*parent
,
100 const wxString
& title
,
104 const wxString
& name
)
106 // this style can be used to prevent a window from having the standard MDI
108 if ( !(style
& wxFRAME_NO_WINDOW_MENU
) )
111 m_pWindowMenu
= new wxMenu
;
113 m_pWindowMenu
->Append(wxWINDOWCLOSE
, _("Cl&ose"));
114 m_pWindowMenu
->Append(wxWINDOWCLOSEALL
, _("Close All"));
115 m_pWindowMenu
->AppendSeparator();
116 m_pWindowMenu
->Append(wxWINDOWNEXT
, _("&Next"));
117 m_pWindowMenu
->Append(wxWINDOWPREV
, _("&Previous"));
118 #endif // wxUSE_MENUS
121 wxFrame::Create( parent
, id
, title
, pos
, size
, style
, name
);
129 void wxGenericMDIParentFrame::SetWindowMenu(wxMenu
* pMenu
)
131 // Replace the window menu from the currently loaded menu bar.
132 wxMenuBar
*pMenuBar
= GetMenuBar();
136 RemoveWindowMenu(pMenuBar
);
138 wxDELETE(m_pWindowMenu
);
143 m_pWindowMenu
= pMenu
;
145 AddWindowMenu(pMenuBar
);
149 void wxGenericMDIParentFrame::SetMenuBar(wxMenuBar
*pMenuBar
)
151 // Remove the Window menu from the old menu bar
152 RemoveWindowMenu(GetMenuBar());
153 // Add the Window menu to the new menu bar.
154 AddWindowMenu(pMenuBar
);
156 wxFrame::SetMenuBar(pMenuBar
);
158 #endif // wxUSE_MENUS
160 void wxGenericMDIParentFrame::SetChildMenuBar(wxGenericMDIChildFrame
*pChild
)
163 if (pChild
== (wxGenericMDIChildFrame
*) NULL
)
165 // No Child, set Our menu bar back.
166 SetMenuBar(m_pMyMenuBar
);
168 // Make sure we know our menu bar is in use
169 m_pMyMenuBar
= (wxMenuBar
*) NULL
;
173 if (pChild
->GetMenuBar() == (wxMenuBar
*) NULL
)
176 // Do we need to save the current bar?
177 if (m_pMyMenuBar
== NULL
)
178 m_pMyMenuBar
= GetMenuBar();
180 SetMenuBar(pChild
->GetMenuBar());
182 #endif // wxUSE_MENUS
185 bool wxGenericMDIParentFrame::ProcessEvent(wxEvent
& event
)
188 * Redirect events to active child first.
191 // Stops the same event being processed repeatedly
192 static wxEventType inEvent
= wxEVT_NULL
;
193 if (inEvent
== event
.GetEventType())
196 inEvent
= event
.GetEventType();
198 // Let the active child (if any) process the event first.
200 if (m_pActiveChild
&& event
.IsKindOf(CLASSINFO(wxCommandEvent
))
202 /* This is sure to not give problems... */
203 && (event
.GetEventType() == wxEVT_COMMAND_MENU_SELECTED
||
204 event
.GetEventType() == wxEVT_UPDATE_UI
)
206 /* This was tested on wxMSW and worked... */
207 && event
.GetEventObject() != m_pClientWindow
208 && !(event
.GetEventType() == wxEVT_ACTIVATE
||
209 event
.GetEventType() == wxEVT_SET_FOCUS
||
210 event
.GetEventType() == wxEVT_KILL_FOCUS
||
211 event
.GetEventType() == wxEVT_CHILD_FOCUS
||
212 event
.GetEventType() == wxEVT_COMMAND_SET_FOCUS
||
213 event
.GetEventType() == wxEVT_COMMAND_KILL_FOCUS
)
217 res
= m_pActiveChild
->GetEventHandler()->ProcessEvent(event
);
220 // If the event was not handled this frame will handle it!
223 res
= GetEventHandler()->wxEvtHandler::ProcessEvent(event
);
226 inEvent
= wxEVT_NULL
;
231 wxGenericMDIChildFrame
*wxGenericMDIParentFrame::GetActiveChild() const
233 return m_pActiveChild
;
236 void wxGenericMDIParentFrame::SetActiveChild(wxGenericMDIChildFrame
* pChildFrame
)
238 m_pActiveChild
= pChildFrame
;
241 wxGenericMDIClientWindow
*wxGenericMDIParentFrame::GetClientWindow() const
243 return m_pClientWindow
;
246 wxGenericMDIClientWindow
*wxGenericMDIParentFrame::OnCreateClient()
248 #if wxUSE_GENERIC_MDI_AS_NATIVE
249 m_pClientWindow
= new wxMDIClientWindow( this );
251 m_pClientWindow
= new wxGenericMDIClientWindow( this );
253 return m_pClientWindow
;
256 void wxGenericMDIParentFrame::ActivateNext()
258 if (m_pClientWindow
&& m_pClientWindow
->GetSelection() != -1)
260 size_t active
= m_pClientWindow
->GetSelection() + 1;
261 if (active
>= m_pClientWindow
->GetPageCount())
264 m_pClientWindow
->SetSelection(active
);
268 void wxGenericMDIParentFrame::ActivatePrevious()
270 if (m_pClientWindow
&& m_pClientWindow
->GetSelection() != -1)
272 int active
= m_pClientWindow
->GetSelection() - 1;
274 active
= m_pClientWindow
->GetPageCount() - 1;
276 m_pClientWindow
->SetSelection(active
);
280 void wxGenericMDIParentFrame::Init()
282 m_pClientWindow
= (wxGenericMDIClientWindow
*) NULL
;
283 m_pActiveChild
= (wxGenericMDIChildFrame
*) NULL
;
285 m_pWindowMenu
= (wxMenu
*) NULL
;
286 m_pMyMenuBar
= (wxMenuBar
*) NULL
;
287 #endif // wxUSE_MENUS
291 void wxGenericMDIParentFrame::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 wxASSERT(m_pWindowMenu
== pMenuBar
->GetMenu(pos
)); // DBG:: We're going to delete the wrong menu!!!
300 pMenuBar
->Remove(pos
);
305 void wxGenericMDIParentFrame::AddWindowMenu(wxMenuBar
*pMenuBar
)
307 if (pMenuBar
&& m_pWindowMenu
)
309 int pos
= pMenuBar
->FindMenu(wxGetStockLabel(wxID_HELP
,false));
310 if (pos
== wxNOT_FOUND
)
312 pMenuBar
->Append(m_pWindowMenu
, _("&Window"));
316 pMenuBar
->Insert(pos
, m_pWindowMenu
, _("&Window"));
321 void wxGenericMDIParentFrame::DoHandleMenu(wxCommandEvent
&event
)
323 switch (event
.GetId())
328 m_pActiveChild
->Close();
331 case wxWINDOWCLOSEALL
:
333 #if 0 // code is only needed if next #if is set to 0!
334 wxGenericMDIChildFrame
*pFirstActiveChild
= m_pActiveChild
;
336 while (m_pActiveChild
)
338 if (!m_pActiveChild
->Close())
340 return; // We failed...
344 #if 1 // What's best? Delayed deleting or immediate deleting?
345 delete m_pActiveChild
;
346 m_pActiveChild
= NULL
;
350 if (pFirstActiveChild
== m_pActiveChild
)
351 return; // We've called Close on all items, no need to continue.
367 #endif // wxUSE_MENUS
369 void wxGenericMDIParentFrame::DoGetClientSize(int *width
, int *height
) const
371 wxFrame::DoGetClientSize( width
, height
);
375 //-----------------------------------------------------------------------------
376 // wxGenericMDIChildFrame
377 //-----------------------------------------------------------------------------
379 IMPLEMENT_DYNAMIC_CLASS(wxGenericMDIChildFrame
, wxPanel
)
381 BEGIN_EVENT_TABLE(wxGenericMDIChildFrame
, wxPanel
)
382 EVT_MENU_HIGHLIGHT_ALL(wxGenericMDIChildFrame::OnMenuHighlight
)
383 EVT_ACTIVATE(wxGenericMDIChildFrame::OnActivate
)
385 EVT_CLOSE(wxGenericMDIChildFrame::OnCloseWindow
)
386 EVT_SIZE(wxGenericMDIChildFrame::OnSize
)
389 wxGenericMDIChildFrame::wxGenericMDIChildFrame()
394 wxGenericMDIChildFrame::wxGenericMDIChildFrame( wxGenericMDIParentFrame
*parent
,
395 wxWindowID id
, const wxString
& title
,
396 const wxPoint
& WXUNUSED(pos
), const wxSize
& size
,
397 long style
, const wxString
& name
)
401 Create( parent
, id
, title
, wxDefaultPosition
, size
, style
, name
);
404 wxGenericMDIChildFrame::~wxGenericMDIChildFrame()
406 wxGenericMDIParentFrame
*pParentFrame
= GetMDIParentFrame();
408 if (pParentFrame
!= NULL
)
410 bool bActive
= false;
411 if (pParentFrame
->GetActiveChild() == this)
413 pParentFrame
->SetActiveChild((wxGenericMDIChildFrame
*) NULL
);
414 pParentFrame
->SetChildMenuBar((wxGenericMDIChildFrame
*) NULL
);
418 wxGenericMDIClientWindow
*pClientWindow
= pParentFrame
->GetClientWindow();
420 // Remove page if still there
422 for (pos
= 0; pos
< pClientWindow
->GetPageCount(); pos
++)
424 if (pClientWindow
->GetPage(pos
) == this)
426 if (pClientWindow
->RemovePage(pos
))
427 pClientWindow
->Refresh();
434 // Set the new selection to the a remaining page
435 if (pClientWindow
->GetPageCount() > pos
)
437 pClientWindow
->SetSelection(pos
);
441 if ((int)pClientWindow
->GetPageCount() - 1 >= 0)
442 pClientWindow
->SetSelection(pClientWindow
->GetPageCount() - 1);
448 wxDELETE(m_pMenuBar
);
449 #endif // wxUSE_MENUS
452 bool wxGenericMDIChildFrame::Create( wxGenericMDIParentFrame
*parent
,
453 wxWindowID id
, const wxString
& title
,
454 const wxPoint
& WXUNUSED(pos
), const wxSize
& size
,
455 long style
, const wxString
& name
)
457 wxGenericMDIClientWindow
* pClientWindow
= parent
->GetClientWindow();
459 wxASSERT_MSG((pClientWindow
!= (wxWindow
*) NULL
), wxT("Missing MDI client window.") );
461 wxPanel::Create(pClientWindow
, id
, wxDefaultPosition
, size
, style
, name
);
463 SetMDIParentFrame(parent
);
465 // This is the currently active child
466 parent
->SetActiveChild(this);
470 pClientWindow
->AddPage(this, title
, true);
471 ApplyMDIChildFrameRect(); // Ok confirme the size change!
472 pClientWindow
->Refresh();
478 void wxGenericMDIChildFrame::SetMenuBar( wxMenuBar
*menu_bar
)
480 wxMenuBar
*pOldMenuBar
= m_pMenuBar
;
481 m_pMenuBar
= menu_bar
;
485 wxGenericMDIParentFrame
*pParentFrame
= GetMDIParentFrame();
487 if (pParentFrame
!= NULL
)
489 m_pMenuBar
->SetParent(pParentFrame
);
491 if (pParentFrame
->GetActiveChild() == this)
493 // Replace current menu bars
495 pParentFrame
->SetChildMenuBar((wxGenericMDIChildFrame
*) NULL
);
496 pParentFrame
->SetChildMenuBar((wxGenericMDIChildFrame
*) this);
502 wxMenuBar
*wxGenericMDIChildFrame::GetMenuBar() const
506 #endif // wxUSE_MENUS
508 void wxGenericMDIChildFrame::SetTitle(const wxString
& title
)
512 wxGenericMDIParentFrame
*pParentFrame
= GetMDIParentFrame();
514 if (pParentFrame
!= NULL
)
516 wxGenericMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
518 if (pClientWindow
!= NULL
)
521 for (pos
= 0; pos
< pClientWindow
->GetPageCount(); pos
++)
523 if (pClientWindow
->GetPage(pos
) == this)
525 pClientWindow
->SetPageText(pos
, m_Title
);
533 wxString
wxGenericMDIChildFrame::GetTitle() const
538 void wxGenericMDIChildFrame::Activate()
540 wxGenericMDIParentFrame
*pParentFrame
= GetMDIParentFrame();
542 if (pParentFrame
!= NULL
)
544 wxGenericMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
546 if (pClientWindow
!= NULL
)
549 for (pos
= 0; pos
< pClientWindow
->GetPageCount(); pos
++)
551 if (pClientWindow
->GetPage(pos
) == this)
553 pClientWindow
->SetSelection(pos
);
561 void wxGenericMDIChildFrame::OnMenuHighlight(wxMenuEvent
& event
)
564 if ( m_pMDIParentFrame
)
566 // we don't have any help text for this item,
567 // but may be the MDI frame does?
568 m_pMDIParentFrame
->OnMenuHighlight(event
);
572 #endif // wxUSE_STATUSBAR
575 void wxGenericMDIChildFrame::OnActivate(wxActivateEvent
& WXUNUSED(event
))
580 /*** Copied from top level..! ***/
581 // default resizing behaviour - if only ONE subwindow, resize to fill the
583 void wxGenericMDIChildFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
585 // if we're using constraints or sizers - do use them
586 if ( GetAutoLayout() )
592 // do we have _exactly_ one child?
593 wxWindow
*child
= (wxWindow
*)NULL
;
594 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
596 node
= node
->GetNext() )
598 wxWindow
*win
= node
->GetData();
600 // exclude top level and managed windows (status bar isn't
601 // currently in the children list except under wxMac anyhow, but
602 // it makes no harm to test for it)
603 if ( !win
->IsTopLevel() /*&& !IsOneOfBars(win)*/ )
607 return; // it's our second subwindow - nothing to do
614 // do we have any children at all?
617 // exactly one child - set it's size to fill the whole frame
618 int clientW
, clientH
;
619 DoGetClientSize(&clientW
, &clientH
);
621 // for whatever reasons, wxGTK wants to have a small offset - it
622 // probably looks better with it?
624 static const int ofs
= 1;
626 static const int ofs
= 0;
629 child
->SetSize(ofs
, ofs
, clientW
- 2*ofs
, clientH
- 2*ofs
);
634 /*** Copied from top level..! ***/
635 // The default implementation for the close window event.
636 void wxGenericMDIChildFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
641 void wxGenericMDIChildFrame::SetMDIParentFrame(wxGenericMDIParentFrame
* parentFrame
)
643 m_pMDIParentFrame
= parentFrame
;
646 wxGenericMDIParentFrame
* wxGenericMDIChildFrame::GetMDIParentFrame() const
648 return m_pMDIParentFrame
;
651 void wxGenericMDIChildFrame::Init()
653 m_pMDIParentFrame
= (wxGenericMDIParentFrame
*) NULL
;
655 m_pMenuBar
= (wxMenuBar
*) NULL
;
656 #endif // wxUSE_MENUS
659 void wxGenericMDIChildFrame::DoMoveWindow(int x
, int y
, int width
, int height
)
661 m_MDIRect
= wxRect(x
, y
, width
, height
);
664 void wxGenericMDIChildFrame::ApplyMDIChildFrameRect()
666 wxPanel::DoMoveWindow(m_MDIRect
.x
, m_MDIRect
.y
, m_MDIRect
.width
, m_MDIRect
.height
);
669 //-----------------------------------------------------------------------------
670 // wxGenericMDIClientWindow
671 //-----------------------------------------------------------------------------
673 #define wxID_NOTEBOOK_CLIENT_AREA wxID_HIGHEST + 100
675 IMPLEMENT_DYNAMIC_CLASS(wxGenericMDIClientWindow
, wxNotebook
)
677 BEGIN_EVENT_TABLE(wxGenericMDIClientWindow
, wxNotebook
)
678 EVT_NOTEBOOK_PAGE_CHANGED(wxID_NOTEBOOK_CLIENT_AREA
, wxGenericMDIClientWindow::OnPageChanged
)
679 EVT_SIZE(wxGenericMDIClientWindow::OnSize
)
683 wxGenericMDIClientWindow::wxGenericMDIClientWindow()
687 wxGenericMDIClientWindow::wxGenericMDIClientWindow( wxGenericMDIParentFrame
*parent
, long style
)
689 CreateClient( parent
, style
);
692 wxGenericMDIClientWindow::~wxGenericMDIClientWindow()
697 bool wxGenericMDIClientWindow::CreateClient( wxGenericMDIParentFrame
*parent
, long style
)
699 SetWindowStyleFlag(style
);
701 bool success
= wxNotebook::Create(parent
, wxID_NOTEBOOK_CLIENT_AREA
, wxPoint(0,0), wxSize(100, 100), 0);
705 wxFont font(10, wxSWISS, wxNORMAL, wxNORMAL);
706 wxFont selFont(10, wxSWISS, wxNORMAL, wxBOLD);
707 GetTabView()->SetTabFont(font);
708 GetTabView()->SetSelectedTabFont(selFont);
709 GetTabView()->SetTabSize(120, 18);
710 GetTabView()->SetTabSelectionHeight(20);
718 int wxGenericMDIClientWindow::SetSelection(size_t nPage
)
720 int oldSelection
= wxNotebook::SetSelection(nPage
);
722 #if !defined(__WXMSW__) // No need to do this for wxMSW as wxNotebook::SetSelection()
723 // will already cause this to be done!
724 // Handle the page change.
725 PageChanged(oldSelection
, nPage
);
731 void wxGenericMDIClientWindow::PageChanged(int OldSelection
, int newSelection
)
733 // Don't do to much work, only when something realy should change!
734 if (OldSelection
== newSelection
)
736 // Again check if we realy need to do this...
737 if (newSelection
!= -1)
739 wxGenericMDIChildFrame
* child
= (wxGenericMDIChildFrame
*)GetPage(newSelection
);
741 if (child
->GetMDIParentFrame()->GetActiveChild() == child
)
745 // Notify old active child that it has been deactivated
746 if (OldSelection
!= -1)
748 wxGenericMDIChildFrame
* oldChild
= (wxGenericMDIChildFrame
*)GetPage(OldSelection
);
751 wxActivateEvent
event(wxEVT_ACTIVATE
, false, oldChild
->GetId());
752 event
.SetEventObject( oldChild
);
753 oldChild
->GetEventHandler()->ProcessEvent(event
);
757 // Notify new active child that it has been activated
758 if (newSelection
!= -1)
760 wxGenericMDIChildFrame
* activeChild
= (wxGenericMDIChildFrame
*)GetPage(newSelection
);
763 wxActivateEvent
event(wxEVT_ACTIVATE
, true, activeChild
->GetId());
764 event
.SetEventObject( activeChild
);
765 activeChild
->GetEventHandler()->ProcessEvent(event
);
767 if (activeChild
->GetMDIParentFrame())
769 activeChild
->GetMDIParentFrame()->SetActiveChild(activeChild
);
770 activeChild
->GetMDIParentFrame()->SetChildMenuBar(activeChild
);
776 void wxGenericMDIClientWindow::OnPageChanged(wxNotebookEvent
& event
)
778 PageChanged(event
.GetOldSelection(), event
.GetSelection());
783 void wxGenericMDIClientWindow::OnSize(wxSizeEvent
& event
)
785 wxNotebook::OnSize(event
);
788 for (pos
= 0; pos
< GetPageCount(); pos
++)
790 ((wxGenericMDIChildFrame
*)GetPage(pos
))->ApplyMDIChildFrameRect();
796 * Define normal wxMDI classes based on wxGenericMDI
799 #if wxUSE_GENERIC_MDI_AS_NATIVE
801 wxMDIChildFrame
* wxMDIParentFrame::GetActiveChild() const
803 wxGenericMDIChildFrame
*pGFrame
= wxGenericMDIParentFrame::GetActiveChild();
804 wxMDIChildFrame
*pFrame
= wxDynamicCast(pGFrame
, wxMDIChildFrame
);
806 wxASSERT_MSG(!(pFrame
== NULL
&& pGFrame
!= NULL
), wxT("Active frame is class not derived from wxMDIChildFrame!"));
811 IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame
, wxGenericMDIParentFrame
)
812 IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame
, wxGenericMDIChildFrame
)
813 IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow
, wxGenericMDIClientWindow
)