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 // ---------------------------------------------------------------------------
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"
47 //-----------------------------------------------------------------------------
48 // wxGenericMDIParentFrame
49 //-----------------------------------------------------------------------------
51 IMPLEMENT_DYNAMIC_CLASS(wxGenericMDIParentFrame
, wxFrame
)
53 BEGIN_EVENT_TABLE(wxGenericMDIParentFrame
, wxFrame
)
55 EVT_MENU (-1, wxGenericMDIParentFrame::DoHandleMenu
)
59 wxGenericMDIParentFrame::wxGenericMDIParentFrame()
64 wxGenericMDIParentFrame::wxGenericMDIParentFrame(wxWindow
*parent
,
66 const wxString
& title
,
74 (void)Create(parent
, id
, title
, pos
, size
, style
, name
);
77 wxGenericMDIParentFrame::~wxGenericMDIParentFrame()
79 // Make sure the client window is destructed before the menu bars are!
80 wxDELETE(m_pClientWindow
);
86 m_pMyMenuBar
= (wxMenuBar
*) NULL
;
89 RemoveWindowMenu(GetMenuBar());
94 m_pWindowMenu
= (wxMenu
*) NULL
;
99 bool wxGenericMDIParentFrame::Create(wxWindow
*parent
,
101 const wxString
& title
,
105 const wxString
& name
)
107 // this style can be used to prevent a window from having the standard MDI
109 if ( !(style
& wxFRAME_NO_WINDOW_MENU
) )
112 m_pWindowMenu
= new wxMenu
;
114 m_pWindowMenu
->Append(wxWINDOWCLOSE
, _("Cl&ose"));
115 m_pWindowMenu
->Append(wxWINDOWCLOSEALL
, _("Close All"));
116 m_pWindowMenu
->AppendSeparator();
117 m_pWindowMenu
->Append(wxWINDOWNEXT
, _("&Next"));
118 m_pWindowMenu
->Append(wxWINDOWPREV
, _("&Previous"));
119 #endif // wxUSE_MENUS
122 wxFrame::Create( parent
, id
, title
, pos
, size
, style
, name
);
130 void wxGenericMDIParentFrame::SetWindowMenu(wxMenu
* pMenu
)
132 // Replace the window menu from the currently loaded menu bar.
133 wxMenuBar
*pMenuBar
= GetMenuBar();
137 RemoveWindowMenu(pMenuBar
);
139 wxDELETE(m_pWindowMenu
);
144 m_pWindowMenu
= pMenu
;
146 AddWindowMenu(pMenuBar
);
150 void wxGenericMDIParentFrame::SetMenuBar(wxMenuBar
*pMenuBar
)
152 // Remove the Window menu from the old menu bar
153 RemoveWindowMenu(GetMenuBar());
154 // Add the Window menu to the new menu bar.
155 AddWindowMenu(pMenuBar
);
157 wxFrame::SetMenuBar(pMenuBar
);
159 #endif // wxUSE_MENUS
161 void wxGenericMDIParentFrame::SetChildMenuBar(wxGenericMDIChildFrame
*pChild
)
164 if (pChild
== (wxGenericMDIChildFrame
*) NULL
)
166 // No Child, set Our menu bar back.
167 SetMenuBar(m_pMyMenuBar
);
169 // Make sure we know our menu bar is in use
170 m_pMyMenuBar
= (wxMenuBar
*) NULL
;
174 if (pChild
->GetMenuBar() == (wxMenuBar
*) NULL
)
177 // Do we need to save the current bar?
178 if (m_pMyMenuBar
== NULL
)
179 m_pMyMenuBar
= GetMenuBar();
181 SetMenuBar(pChild
->GetMenuBar());
183 #endif // wxUSE_MENUS
186 bool wxGenericMDIParentFrame::ProcessEvent(wxEvent
& event
)
189 * Redirect events to active child first.
192 // Stops the same event being processed repeatedly
193 static wxEventType inEvent
= wxEVT_NULL
;
194 if (inEvent
== event
.GetEventType())
197 inEvent
= event
.GetEventType();
199 // Let the active child (if any) process the event first.
201 if (m_pActiveChild
&& event
.IsKindOf(CLASSINFO(wxCommandEvent
))
203 /* This is sure to not give problems... */
204 && (event
.GetEventType() == wxEVT_COMMAND_MENU_SELECTED
||
205 event
.GetEventType() == wxEVT_UPDATE_UI
)
207 /* This was tested on wxMSW and worked... */
208 && event
.GetEventObject() != m_pClientWindow
209 && !(event
.GetEventType() == wxEVT_ACTIVATE
||
210 event
.GetEventType() == wxEVT_SET_FOCUS
||
211 event
.GetEventType() == wxEVT_KILL_FOCUS
||
212 event
.GetEventType() == wxEVT_CHILD_FOCUS
||
213 event
.GetEventType() == wxEVT_COMMAND_SET_FOCUS
||
214 event
.GetEventType() == wxEVT_COMMAND_KILL_FOCUS
)
218 res
= m_pActiveChild
->GetEventHandler()->ProcessEvent(event
);
221 // If the event was not handled this frame will handle it!
224 res
= GetEventHandler()->wxEvtHandler::ProcessEvent(event
);
227 inEvent
= wxEVT_NULL
;
232 wxGenericMDIChildFrame
*wxGenericMDIParentFrame::GetActiveChild() const
234 return m_pActiveChild
;
237 void wxGenericMDIParentFrame::SetActiveChild(wxGenericMDIChildFrame
* pChildFrame
)
239 m_pActiveChild
= pChildFrame
;
242 wxGenericMDIClientWindow
*wxGenericMDIParentFrame::GetClientWindow() const
244 return m_pClientWindow
;
247 wxGenericMDIClientWindow
*wxGenericMDIParentFrame::OnCreateClient()
249 #if wxUSE_GENERIC_MDI_AS_NATIVE
250 m_pClientWindow
= new wxMDIClientWindow( this );
252 m_pClientWindow
= new wxGenericMDIClientWindow( this );
254 return m_pClientWindow
;
257 void wxGenericMDIParentFrame::ActivateNext()
259 if (m_pClientWindow
&& m_pClientWindow
->GetSelection() != -1)
261 int active
= m_pClientWindow
->GetSelection() + 1;
262 if (active
>= m_pClientWindow
->GetPageCount())
265 m_pClientWindow
->SetSelection(active
);
269 void wxGenericMDIParentFrame::ActivatePrevious()
271 if (m_pClientWindow
&& m_pClientWindow
->GetSelection() != -1)
273 int active
= m_pClientWindow
->GetSelection() - 1;
275 active
= m_pClientWindow
->GetPageCount() - 1;
277 m_pClientWindow
->SetSelection(active
);
281 void wxGenericMDIParentFrame::Init()
283 m_pClientWindow
= (wxGenericMDIClientWindow
*) NULL
;
284 m_pActiveChild
= (wxGenericMDIChildFrame
*) NULL
;
286 m_pWindowMenu
= (wxMenu
*) NULL
;
287 m_pMyMenuBar
= (wxMenuBar
*) NULL
;
288 #endif // wxUSE_MENUS
292 void wxGenericMDIParentFrame::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 wxASSERT(m_pWindowMenu
== pMenuBar
->GetMenu(pos
)); // DBG:: We're going to delete the wrong menu!!!
301 pMenuBar
->Remove(pos
);
306 void wxGenericMDIParentFrame::AddWindowMenu(wxMenuBar
*pMenuBar
)
308 if (pMenuBar
&& m_pWindowMenu
)
310 int pos
= pMenuBar
->FindMenu(_("Help"));
311 if (pos
== wxNOT_FOUND
)
313 pMenuBar
->Append(m_pWindowMenu
, _("&Window"));
317 pMenuBar
->Insert(pos
, m_pWindowMenu
, _("&Window"));
322 void wxGenericMDIParentFrame::DoHandleMenu(wxCommandEvent
&event
)
324 switch (event
.GetId())
329 m_pActiveChild
->Close();
332 case wxWINDOWCLOSEALL
:
334 #if 0 // code is only needed if next #if is set to 0!
335 wxGenericMDIChildFrame
*pFirstActiveChild
= m_pActiveChild
;
337 while (m_pActiveChild
)
339 if (!m_pActiveChild
->Close())
341 return; // We failed...
345 #if 1 // What's best? Delayed deleting or immediate deleting?
346 delete m_pActiveChild
;
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
);
405 wxGenericMDIChildFrame::~wxGenericMDIChildFrame()
407 wxGenericMDIParentFrame
*pParentFrame
= GetMDIParentFrame();
409 if (pParentFrame
!= NULL
)
411 bool bActive
= FALSE
;
412 if (pParentFrame
->GetActiveChild() == this)
414 pParentFrame
->SetActiveChild((wxGenericMDIChildFrame
*) NULL
);
415 pParentFrame
->SetChildMenuBar((wxGenericMDIChildFrame
*) NULL
);
419 wxGenericMDIClientWindow
*pClientWindow
= pParentFrame
->GetClientWindow();
421 // Remove page if still there
423 for (pos
= 0; pos
< pClientWindow
->GetPageCount(); pos
++)
425 if (pClientWindow
->GetPage(pos
) == this)
427 if (pClientWindow
->RemovePage(pos
))
428 pClientWindow
->Refresh();
435 // Set the new selection to the a remaining page
436 if (pClientWindow
->GetPageCount() > pos
)
438 pClientWindow
->SetSelection(pos
);
442 if (pClientWindow
->GetPageCount() - 1 >= 0)
443 pClientWindow
->SetSelection(pClientWindow
->GetPageCount() - 1);
449 wxDELETE(m_pMenuBar
);
450 #endif // wxUSE_MENUS
453 bool wxGenericMDIChildFrame::Create( wxGenericMDIParentFrame
*parent
,
454 wxWindowID id
, const wxString
& title
,
455 const wxPoint
& WXUNUSED(pos
), const wxSize
& size
,
456 long style
, const wxString
& name
)
458 wxGenericMDIClientWindow
* pClientWindow
= parent
->GetClientWindow();
460 wxASSERT_MSG((pClientWindow
!= (wxWindow
*) NULL
), wxT("Missing MDI client window.") );
462 wxPanel::Create(pClientWindow
, id
, wxDefaultPosition
, size
, style
, name
);
464 SetMDIParentFrame(parent
);
466 // This is the currently active child
467 parent
->SetActiveChild(this);
471 pClientWindow
->AddPage(this, title
, TRUE
);
472 ApplyMDIChildFrameRect(); // Ok confirme the size change!
473 pClientWindow
->Refresh();
479 void wxGenericMDIChildFrame::SetMenuBar( wxMenuBar
*menu_bar
)
481 wxMenuBar
*pOldMenuBar
= m_pMenuBar
;
482 m_pMenuBar
= menu_bar
;
486 wxGenericMDIParentFrame
*pParentFrame
= GetMDIParentFrame();
488 if (pParentFrame
!= NULL
)
490 m_pMenuBar
->SetParent(pParentFrame
);
492 if (pParentFrame
->GetActiveChild() == this)
494 // Replace current menu bars
496 pParentFrame
->SetChildMenuBar((wxGenericMDIChildFrame
*) NULL
);
497 pParentFrame
->SetChildMenuBar((wxGenericMDIChildFrame
*) this);
503 wxMenuBar
*wxGenericMDIChildFrame::GetMenuBar() const
507 #endif // wxUSE_MENUS
509 void wxGenericMDIChildFrame::SetTitle(const wxString
& title
)
513 wxGenericMDIParentFrame
*pParentFrame
= GetMDIParentFrame();
515 if (pParentFrame
!= NULL
)
517 wxGenericMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
519 if (pClientWindow
!= NULL
)
522 for (pos
= 0; pos
< pClientWindow
->GetPageCount(); pos
++)
524 if (pClientWindow
->GetPage(pos
) == this)
526 pClientWindow
->SetPageText(pos
, m_Title
);
534 wxString
wxGenericMDIChildFrame::GetTitle() const
539 void wxGenericMDIChildFrame::Activate()
541 wxGenericMDIParentFrame
*pParentFrame
= GetMDIParentFrame();
543 if (pParentFrame
!= NULL
)
545 wxGenericMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
547 if (pClientWindow
!= NULL
)
550 for (pos
= 0; pos
< pClientWindow
->GetPageCount(); pos
++)
552 if (pClientWindow
->GetPage(pos
) == this)
554 pClientWindow
->SetSelection(pos
);
562 void wxGenericMDIChildFrame::OnMenuHighlight(wxMenuEvent
& event
)
565 if ( m_pMDIParentFrame
)
567 // we don't have any help text for this item,
568 // but may be the MDI frame does?
569 m_pMDIParentFrame
->OnMenuHighlight(event
);
571 #endif // wxUSE_STATUSBAR
574 void wxGenericMDIChildFrame::OnActivate(wxActivateEvent
& event
)
579 /*** Copied from top level..! ***/
580 // default resizing behaviour - if only ONE subwindow, resize to fill the
582 void wxGenericMDIChildFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
584 // if we're using constraints or sizers - do use them
585 if ( GetAutoLayout() )
591 // do we have _exactly_ one child?
592 wxWindow
*child
= (wxWindow
*)NULL
;
593 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
595 node
= node
->GetNext() )
597 wxWindow
*win
= node
->GetData();
599 // exclude top level and managed windows (status bar isn't
600 // currently in the children list except under wxMac anyhow, but
601 // it makes no harm to test for it)
602 if ( !win
->IsTopLevel() /*&& !IsOneOfBars(win)*/ )
606 return; // it's our second subwindow - nothing to do
613 // do we have any children at all?
616 // exactly one child - set it's size to fill the whole frame
617 int clientW
, clientH
;
618 DoGetClientSize(&clientW
, &clientH
);
620 // for whatever reasons, wxGTK wants to have a small offset - it
621 // probably looks better with it?
623 static const int ofs
= 1;
625 static const int ofs
= 0;
628 child
->SetSize(ofs
, ofs
, clientW
- 2*ofs
, clientH
- 2*ofs
);
633 /*** Copied from top level..! ***/
634 // The default implementation for the close window event.
635 void wxGenericMDIChildFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
640 void wxGenericMDIChildFrame::SetMDIParentFrame(wxGenericMDIParentFrame
* parentFrame
)
642 m_pMDIParentFrame
= parentFrame
;
645 wxGenericMDIParentFrame
* wxGenericMDIChildFrame::GetMDIParentFrame() const
647 return m_pMDIParentFrame
;
650 void wxGenericMDIChildFrame::Init()
652 m_pMDIParentFrame
= (wxGenericMDIParentFrame
*) NULL
;
654 m_pMenuBar
= (wxMenuBar
*) NULL
;
655 #endif // wxUSE_MENUS
658 void wxGenericMDIChildFrame::DoMoveWindow(int x
, int y
, int width
, int height
)
660 m_MDIRect
= wxRect(x
, y
, width
, height
);
663 void wxGenericMDIChildFrame::ApplyMDIChildFrameRect()
665 wxPanel::DoMoveWindow(m_MDIRect
.x
, m_MDIRect
.y
, m_MDIRect
.width
, m_MDIRect
.height
);
668 //-----------------------------------------------------------------------------
669 // wxGenericMDIClientWindow
670 //-----------------------------------------------------------------------------
672 #define wxID_NOTEBOOK_CLIENT_AREA wxID_HIGHEST + 100
674 IMPLEMENT_DYNAMIC_CLASS(wxGenericMDIClientWindow
, wxNotebook
)
676 BEGIN_EVENT_TABLE(wxGenericMDIClientWindow
, wxNotebook
)
677 EVT_NOTEBOOK_PAGE_CHANGED(wxID_NOTEBOOK_CLIENT_AREA
, wxGenericMDIClientWindow::OnPageChanged
)
678 EVT_SIZE(wxGenericMDIClientWindow::OnSize
)
682 wxGenericMDIClientWindow::wxGenericMDIClientWindow()
686 wxGenericMDIClientWindow::wxGenericMDIClientWindow( wxGenericMDIParentFrame
*parent
, long style
)
688 CreateClient( parent
, style
);
691 wxGenericMDIClientWindow::~wxGenericMDIClientWindow()
696 bool wxGenericMDIClientWindow::CreateClient( wxGenericMDIParentFrame
*parent
, long style
)
698 SetWindowStyleFlag(style
);
700 bool success
= wxNotebook::Create(parent
, wxID_NOTEBOOK_CLIENT_AREA
, wxPoint(0, 0), wxSize(100, 100), 0);
704 wxFont font(10, wxSWISS, wxNORMAL, wxNORMAL);
705 wxFont selFont(10, wxSWISS, wxNORMAL, wxBOLD);
706 GetTabView()->SetTabFont(font);
707 GetTabView()->SetSelectedTabFont(selFont);
708 GetTabView()->SetTabSize(120, 18);
709 GetTabView()->SetTabSelectionHeight(20);
717 int wxGenericMDIClientWindow::SetSelection(int nPage
)
719 int oldSelection
= wxNotebook::SetSelection(nPage
);
721 #if !defined(__WXMSW__) // No need to do this for wxMSW as wxNotebook::SetSelection()
722 // will already cause this to be done!
723 // Handle the page change.
724 PageChanged(oldSelection
, nPage
);
730 void wxGenericMDIClientWindow::PageChanged(int OldSelection
, int newSelection
)
732 // Don't do to much work, only when something realy should change!
733 if (OldSelection
== newSelection
)
735 // Again check if we realy need to do this...
736 if (newSelection
!= -1)
738 wxGenericMDIChildFrame
* child
= (wxGenericMDIChildFrame
*)GetPage(newSelection
);
740 if (child
->GetMDIParentFrame()->GetActiveChild() == child
)
744 // Notify old active child that it has been deactivated
745 if (OldSelection
!= -1)
747 wxGenericMDIChildFrame
* oldChild
= (wxGenericMDIChildFrame
*)GetPage(OldSelection
);
750 wxActivateEvent
event(wxEVT_ACTIVATE
, FALSE
, oldChild
->GetId());
751 event
.SetEventObject( oldChild
);
752 oldChild
->GetEventHandler()->ProcessEvent(event
);
756 // Notify new active child that it has been activated
757 if (newSelection
!= -1)
759 wxGenericMDIChildFrame
* activeChild
= (wxGenericMDIChildFrame
*)GetPage(newSelection
);
762 wxActivateEvent
event(wxEVT_ACTIVATE
, TRUE
, activeChild
->GetId());
763 event
.SetEventObject( activeChild
);
764 activeChild
->GetEventHandler()->ProcessEvent(event
);
766 if (activeChild
->GetMDIParentFrame())
768 activeChild
->GetMDIParentFrame()->SetActiveChild(activeChild
);
769 activeChild
->GetMDIParentFrame()->SetChildMenuBar(activeChild
);
775 void wxGenericMDIClientWindow::OnPageChanged(wxNotebookEvent
& event
)
777 PageChanged(event
.GetOldSelection(), event
.GetSelection());
782 void wxGenericMDIClientWindow::OnSize(wxSizeEvent
& event
)
784 wxNotebook::OnSize(event
);
787 for (pos
= 0; pos
< GetPageCount(); pos
++)
789 ((wxGenericMDIChildFrame
*)GetPage(pos
))->ApplyMDIChildFrameRect();
795 * Define normal wxMDI classes based on wxGenericMDI
798 #if wxUSE_GENERIC_MDI_AS_NATIVE
800 wxMDIChildFrame
* wxMDIParentFrame::GetActiveChild() const
802 wxGenericMDIChildFrame
*pGFrame
= wxGenericMDIParentFrame::GetActiveChild();
803 wxMDIChildFrame
*pFrame
= wxDynamicCast(pGFrame
, wxMDIChildFrame
);
805 wxASSERT_MSG(!(pFrame
== NULL
&& pGFrame
!= NULL
), wxT("Active frame is class not derived from wxMDIChildFrame!"));
810 IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame
, wxGenericMDIParentFrame
)
811 IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame
, wxGenericMDIChildFrame
)
812 IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow
, wxGenericMDIClientWindow
)