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"
33 #include "wx/generic/mdig.h"
34 #include "wx/stockitem.h"
44 //-----------------------------------------------------------------------------
45 // wxGenericMDIParentFrame
46 //-----------------------------------------------------------------------------
48 IMPLEMENT_DYNAMIC_CLASS(wxGenericMDIParentFrame
, wxFrame
)
50 BEGIN_EVENT_TABLE(wxGenericMDIParentFrame
, wxFrame
)
52 EVT_MENU (wxID_ANY
, wxGenericMDIParentFrame::DoHandleMenu
)
56 wxGenericMDIParentFrame::wxGenericMDIParentFrame()
61 wxGenericMDIParentFrame::wxGenericMDIParentFrame(wxWindow
*parent
,
63 const wxString
& title
,
71 (void)Create(parent
, id
, title
, pos
, size
, style
, name
);
74 wxGenericMDIParentFrame::~wxGenericMDIParentFrame()
76 // Make sure the client window is destructed before the menu bars are!
77 wxDELETE(m_pClientWindow
);
83 m_pMyMenuBar
= (wxMenuBar
*) NULL
;
86 RemoveWindowMenu(GetMenuBar());
91 m_pWindowMenu
= (wxMenu
*) NULL
;
96 bool wxGenericMDIParentFrame::Create(wxWindow
*parent
,
98 const wxString
& title
,
102 const wxString
& name
)
104 // this style can be used to prevent a window from having the standard MDI
106 if ( !(style
& wxFRAME_NO_WINDOW_MENU
) )
109 m_pWindowMenu
= new wxMenu
;
111 m_pWindowMenu
->Append(wxWINDOWCLOSE
, _("Cl&ose"));
112 m_pWindowMenu
->Append(wxWINDOWCLOSEALL
, _("Close All"));
113 m_pWindowMenu
->AppendSeparator();
114 m_pWindowMenu
->Append(wxWINDOWNEXT
, _("&Next"));
115 m_pWindowMenu
->Append(wxWINDOWPREV
, _("&Previous"));
116 #endif // wxUSE_MENUS
119 wxFrame::Create( parent
, id
, title
, pos
, size
, style
, name
);
127 void wxGenericMDIParentFrame::SetWindowMenu(wxMenu
* pMenu
)
129 // Replace the window menu from the currently loaded menu bar.
130 wxMenuBar
*pMenuBar
= GetMenuBar();
134 RemoveWindowMenu(pMenuBar
);
136 wxDELETE(m_pWindowMenu
);
141 m_pWindowMenu
= pMenu
;
143 AddWindowMenu(pMenuBar
);
147 void wxGenericMDIParentFrame::SetMenuBar(wxMenuBar
*pMenuBar
)
149 // Remove the Window menu from the old menu bar
150 RemoveWindowMenu(GetMenuBar());
151 // Add the Window menu to the new menu bar.
152 AddWindowMenu(pMenuBar
);
154 wxFrame::SetMenuBar(pMenuBar
);
156 #endif // wxUSE_MENUS
158 void wxGenericMDIParentFrame::SetChildMenuBar(wxGenericMDIChildFrame
*pChild
)
161 if (pChild
== (wxGenericMDIChildFrame
*) NULL
)
163 // No Child, set Our menu bar back.
164 SetMenuBar(m_pMyMenuBar
);
166 // Make sure we know our menu bar is in use
167 m_pMyMenuBar
= (wxMenuBar
*) NULL
;
171 if (pChild
->GetMenuBar() == (wxMenuBar
*) NULL
)
174 // Do we need to save the current bar?
175 if (m_pMyMenuBar
== NULL
)
176 m_pMyMenuBar
= GetMenuBar();
178 SetMenuBar(pChild
->GetMenuBar());
180 #endif // wxUSE_MENUS
183 bool wxGenericMDIParentFrame::ProcessEvent(wxEvent
& event
)
186 * Redirect events to active child first.
189 // Stops the same event being processed repeatedly
190 static wxEventType inEvent
= wxEVT_NULL
;
191 if (inEvent
== event
.GetEventType())
194 inEvent
= event
.GetEventType();
196 // Let the active child (if any) process the event first.
198 if (m_pActiveChild
&& event
.IsKindOf(CLASSINFO(wxCommandEvent
))
200 /* This is sure to not give problems... */
201 && (event
.GetEventType() == wxEVT_COMMAND_MENU_SELECTED
||
202 event
.GetEventType() == wxEVT_UPDATE_UI
)
204 /* This was tested on wxMSW and worked... */
205 && event
.GetEventObject() != m_pClientWindow
206 && !(event
.GetEventType() == wxEVT_ACTIVATE
||
207 event
.GetEventType() == wxEVT_SET_FOCUS
||
208 event
.GetEventType() == wxEVT_KILL_FOCUS
||
209 event
.GetEventType() == wxEVT_CHILD_FOCUS
||
210 event
.GetEventType() == wxEVT_COMMAND_SET_FOCUS
||
211 event
.GetEventType() == wxEVT_COMMAND_KILL_FOCUS
)
215 res
= m_pActiveChild
->GetEventHandler()->ProcessEvent(event
);
218 // If the event was not handled this frame will handle it!
221 res
= GetEventHandler()->wxEvtHandler::ProcessEvent(event
);
224 inEvent
= wxEVT_NULL
;
229 wxGenericMDIChildFrame
*wxGenericMDIParentFrame::GetActiveChild() const
231 return m_pActiveChild
;
234 void wxGenericMDIParentFrame::SetActiveChild(wxGenericMDIChildFrame
* pChildFrame
)
236 m_pActiveChild
= pChildFrame
;
239 wxGenericMDIClientWindow
*wxGenericMDIParentFrame::GetClientWindow() const
241 return m_pClientWindow
;
244 wxGenericMDIClientWindow
*wxGenericMDIParentFrame::OnCreateClient()
246 #if wxUSE_GENERIC_MDI_AS_NATIVE
247 m_pClientWindow
= new wxMDIClientWindow( this );
249 m_pClientWindow
= new wxGenericMDIClientWindow( this );
251 return m_pClientWindow
;
254 void wxGenericMDIParentFrame::ActivateNext()
256 if (m_pClientWindow
&& m_pClientWindow
->GetSelection() != -1)
258 size_t active
= m_pClientWindow
->GetSelection() + 1;
259 if (active
>= m_pClientWindow
->GetPageCount())
262 m_pClientWindow
->SetSelection(active
);
266 void wxGenericMDIParentFrame::ActivatePrevious()
268 if (m_pClientWindow
&& m_pClientWindow
->GetSelection() != -1)
270 int active
= m_pClientWindow
->GetSelection() - 1;
272 active
= m_pClientWindow
->GetPageCount() - 1;
274 m_pClientWindow
->SetSelection(active
);
278 void wxGenericMDIParentFrame::Init()
280 m_pClientWindow
= (wxGenericMDIClientWindow
*) NULL
;
281 m_pActiveChild
= (wxGenericMDIChildFrame
*) NULL
;
283 m_pWindowMenu
= (wxMenu
*) NULL
;
284 m_pMyMenuBar
= (wxMenuBar
*) NULL
;
285 #endif // wxUSE_MENUS
289 void wxGenericMDIParentFrame::RemoveWindowMenu(wxMenuBar
*pMenuBar
)
291 if (pMenuBar
&& m_pWindowMenu
)
293 // Remove old window menu
294 int pos
= pMenuBar
->FindMenu(_("&Window"));
295 if (pos
!= wxNOT_FOUND
)
297 wxASSERT(m_pWindowMenu
== pMenuBar
->GetMenu(pos
)); // DBG:: We're going to delete the wrong menu!!!
298 pMenuBar
->Remove(pos
);
303 void wxGenericMDIParentFrame::AddWindowMenu(wxMenuBar
*pMenuBar
)
305 if (pMenuBar
&& m_pWindowMenu
)
307 int pos
= pMenuBar
->FindMenu(wxGetStockLabel(wxID_HELP
,false));
308 if (pos
== wxNOT_FOUND
)
310 pMenuBar
->Append(m_pWindowMenu
, _("&Window"));
314 pMenuBar
->Insert(pos
, m_pWindowMenu
, _("&Window"));
319 void wxGenericMDIParentFrame::DoHandleMenu(wxCommandEvent
&event
)
321 switch (event
.GetId())
326 m_pActiveChild
->Close();
329 case wxWINDOWCLOSEALL
:
331 #if 0 // code is only needed if next #if is set to 0!
332 wxGenericMDIChildFrame
*pFirstActiveChild
= m_pActiveChild
;
334 while (m_pActiveChild
)
336 if (!m_pActiveChild
->Close())
338 return; // We failed...
342 #if 1 // What's best? Delayed deleting or immediate deleting?
343 delete m_pActiveChild
;
344 m_pActiveChild
= NULL
;
348 if (pFirstActiveChild
== m_pActiveChild
)
349 return; // We've called Close on all items, no need to continue.
365 #endif // wxUSE_MENUS
367 void wxGenericMDIParentFrame::DoGetClientSize(int *width
, int *height
) const
369 wxFrame::DoGetClientSize( width
, height
);
373 //-----------------------------------------------------------------------------
374 // wxGenericMDIChildFrame
375 //-----------------------------------------------------------------------------
377 IMPLEMENT_DYNAMIC_CLASS(wxGenericMDIChildFrame
, wxPanel
)
379 BEGIN_EVENT_TABLE(wxGenericMDIChildFrame
, wxPanel
)
380 EVT_MENU_HIGHLIGHT_ALL(wxGenericMDIChildFrame::OnMenuHighlight
)
381 EVT_ACTIVATE(wxGenericMDIChildFrame::OnActivate
)
383 EVT_CLOSE(wxGenericMDIChildFrame::OnCloseWindow
)
384 EVT_SIZE(wxGenericMDIChildFrame::OnSize
)
387 wxGenericMDIChildFrame::wxGenericMDIChildFrame()
392 wxGenericMDIChildFrame::wxGenericMDIChildFrame( wxGenericMDIParentFrame
*parent
,
393 wxWindowID id
, const wxString
& title
,
394 const wxPoint
& WXUNUSED(pos
), const wxSize
& size
,
395 long style
, const wxString
& name
)
399 Create( parent
, id
, title
, wxDefaultPosition
, size
, style
, name
);
403 wxGenericMDIChildFrame::~wxGenericMDIChildFrame()
405 wxGenericMDIParentFrame
*pParentFrame
= GetMDIParentFrame();
407 if (pParentFrame
!= NULL
)
409 bool bActive
= false;
410 if (pParentFrame
->GetActiveChild() == this)
412 pParentFrame
->SetActiveChild((wxGenericMDIChildFrame
*) NULL
);
413 pParentFrame
->SetChildMenuBar((wxGenericMDIChildFrame
*) NULL
);
417 wxGenericMDIClientWindow
*pClientWindow
= pParentFrame
->GetClientWindow();
419 // Remove page if still there
421 for (pos
= 0; pos
< pClientWindow
->GetPageCount(); pos
++)
423 if (pClientWindow
->GetPage(pos
) == this)
425 if (pClientWindow
->RemovePage(pos
))
426 pClientWindow
->Refresh();
433 // Set the new selection to the a remaining page
434 if (pClientWindow
->GetPageCount() > pos
)
436 pClientWindow
->SetSelection(pos
);
440 if ((int)pClientWindow
->GetPageCount() - 1 >= 0)
441 pClientWindow
->SetSelection(pClientWindow
->GetPageCount() - 1);
447 wxDELETE(m_pMenuBar
);
448 #endif // wxUSE_MENUS
451 bool wxGenericMDIChildFrame::Create( wxGenericMDIParentFrame
*parent
,
452 wxWindowID id
, const wxString
& title
,
453 const wxPoint
& WXUNUSED(pos
), const wxSize
& size
,
454 long style
, const wxString
& name
)
456 wxGenericMDIClientWindow
* pClientWindow
= parent
->GetClientWindow();
458 wxASSERT_MSG((pClientWindow
!= (wxWindow
*) NULL
), wxT("Missing MDI client window.") );
460 wxPanel::Create(pClientWindow
, id
, wxDefaultPosition
, size
, style
, name
);
462 SetMDIParentFrame(parent
);
464 // This is the currently active child
465 parent
->SetActiveChild(this);
469 pClientWindow
->AddPage(this, title
, true);
470 ApplyMDIChildFrameRect(); // Ok confirme the size change!
471 pClientWindow
->Refresh();
477 void wxGenericMDIChildFrame::SetMenuBar( wxMenuBar
*menu_bar
)
479 wxMenuBar
*pOldMenuBar
= m_pMenuBar
;
480 m_pMenuBar
= menu_bar
;
484 wxGenericMDIParentFrame
*pParentFrame
= GetMDIParentFrame();
486 if (pParentFrame
!= NULL
)
488 m_pMenuBar
->SetParent(pParentFrame
);
490 if (pParentFrame
->GetActiveChild() == this)
492 // Replace current menu bars
494 pParentFrame
->SetChildMenuBar((wxGenericMDIChildFrame
*) NULL
);
495 pParentFrame
->SetChildMenuBar((wxGenericMDIChildFrame
*) this);
501 wxMenuBar
*wxGenericMDIChildFrame::GetMenuBar() const
505 #endif // wxUSE_MENUS
507 void wxGenericMDIChildFrame::SetTitle(const wxString
& title
)
511 wxGenericMDIParentFrame
*pParentFrame
= GetMDIParentFrame();
513 if (pParentFrame
!= NULL
)
515 wxGenericMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
517 if (pClientWindow
!= NULL
)
520 for (pos
= 0; pos
< pClientWindow
->GetPageCount(); pos
++)
522 if (pClientWindow
->GetPage(pos
) == this)
524 pClientWindow
->SetPageText(pos
, m_Title
);
532 wxString
wxGenericMDIChildFrame::GetTitle() const
537 void wxGenericMDIChildFrame::Activate()
539 wxGenericMDIParentFrame
*pParentFrame
= GetMDIParentFrame();
541 if (pParentFrame
!= NULL
)
543 wxGenericMDIClientWindow
* pClientWindow
= pParentFrame
->GetClientWindow();
545 if (pClientWindow
!= NULL
)
548 for (pos
= 0; pos
< pClientWindow
->GetPageCount(); pos
++)
550 if (pClientWindow
->GetPage(pos
) == this)
552 pClientWindow
->SetSelection(pos
);
560 void wxGenericMDIChildFrame::OnMenuHighlight(wxMenuEvent
& event
)
563 if ( m_pMDIParentFrame
)
565 // we don't have any help text for this item,
566 // but may be the MDI frame does?
567 m_pMDIParentFrame
->OnMenuHighlight(event
);
571 #endif // wxUSE_STATUSBAR
574 void wxGenericMDIChildFrame::OnActivate(wxActivateEvent
& WXUNUSED(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(size_t 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
)