1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: MDI classes
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
20 #include "wx/settings.h"
21 #include "wx/statusbr.h"
24 #include "wx/mac/private.h"
25 #include "wx/mac/uma.h"
27 extern wxWindowList wxModelessWindows
;
29 IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame
, wxFrame
)
30 IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame
, wxFrame
)
31 IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow
, wxWindow
)
33 BEGIN_EVENT_TABLE(wxMDIParentFrame
, wxFrame
)
34 EVT_ACTIVATE(wxMDIParentFrame::OnActivate
)
35 EVT_SYS_COLOUR_CHANGED(wxMDIParentFrame::OnSysColourChanged
)
38 BEGIN_EVENT_TABLE(wxMDIClientWindow
, wxWindow
)
39 EVT_SCROLL(wxMDIClientWindow::OnScroll
)
42 static const wxChar
*TRACE_MDI
= _T("mdi");
44 static const int IDM_WINDOWTILE
= 4001;
45 static const int IDM_WINDOWTILEHOR
= 4001;
46 static const int IDM_WINDOWCASCADE
= 4002;
47 static const int IDM_WINDOWICONS
= 4003;
48 static const int IDM_WINDOWNEXT
= 4004;
49 static const int IDM_WINDOWTILEVERT
= 4005;
50 static const int IDM_WINDOWPREV
= 4006;
52 // This range gives a maximum of 500 MDI children. Should be enough :-)
53 static const int wxFIRST_MDI_CHILD
= 4100;
54 static const int wxLAST_MDI_CHILD
= 4600;
56 // Status border dimensions
57 static const int wxTHICK_LINE_BORDER
= 3;
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 void wxMDIParentFrame::Init()
65 m_clientWindow
= NULL
;
66 m_currentChild
= NULL
;
67 m_windowMenu
= (wxMenu
*) NULL
;
68 m_parentFrameActive
= true;
69 m_shouldBeShown
= false;
72 bool wxMDIParentFrame::Create(wxWindow
*parent
,
74 const wxString
& title
,
80 // this style can be used to prevent a window from having the standard MDI
82 if ( style
& wxFRAME_NO_WINDOW_MENU
)
84 m_windowMenu
= (wxMenu
*)NULL
;
85 style
-= wxFRAME_NO_WINDOW_MENU
;
87 else // normal case: we have the window menu, so construct it
89 m_windowMenu
= new wxMenu
;
91 m_windowMenu
->Append(IDM_WINDOWCASCADE
, wxT("&Cascade"));
92 m_windowMenu
->Append(IDM_WINDOWTILEHOR
, wxT("Tile &Horizontally"));
93 m_windowMenu
->Append(IDM_WINDOWTILEVERT
, wxT("Tile &Vertically"));
94 m_windowMenu
->AppendSeparator();
95 m_windowMenu
->Append(IDM_WINDOWICONS
, wxT("&Arrange Icons"));
96 m_windowMenu
->Append(IDM_WINDOWNEXT
, wxT("&Next"));
99 wxFrame::Create( parent
, id
, title
, pos
, size
, style
, name
) ;
100 m_parentFrameActive
= true;
107 wxMDIParentFrame::~wxMDIParentFrame()
110 // already deleted by DestroyChildren()
111 m_clientWindow
= NULL
;
117 void wxMDIParentFrame::SetMenuBar(wxMenuBar
*menu_bar
)
119 wxFrame::SetMenuBar( menu_bar
) ;
122 void wxMDIParentFrame::GetRectForTopLevelChildren(int *x
, int *y
, int *w
, int *h
)
131 void wxMDIParentFrame::AddChild(wxWindowBase
*child
)
133 if ( !m_currentChild
)
135 m_currentChild
= wxDynamicCast(child
, wxMDIChildFrame
);
137 if ( m_currentChild
&& IsShown() && !ShouldBeVisible() )
139 // we shouldn't remain visible any more
140 wxFrame::Show(false);
141 m_shouldBeShown
= true;
145 wxFrame::AddChild(child
);
148 void wxMDIParentFrame::RemoveChild(wxWindowBase
*child
)
150 if ( child
== m_currentChild
)
152 // the current child isn't active any more, try to find another one
153 m_currentChild
= NULL
;
155 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
157 node
= node
->GetNext() )
160 childCur
= wxDynamicCast(node
->GetData(), wxMDIChildFrame
);
161 if ( childCur
!= child
)
163 m_currentChild
= childCur
;
169 wxFrame::RemoveChild(child
);
171 // if there are no more children left we need to show the frame if we
172 // hadn't shown it before because there were active children and it was
173 // useless (note that we have to do it after fully removing the child, i.e.
174 // after calling the base class RemoveChild() as otherwise we risk to touch
175 // pointer to the child being deleted)
176 if ( !m_currentChild
&& m_shouldBeShown
&& !IsShown() )
178 // we have to show it, but at least move it out of sight and make it of
179 // smallest possible size (unfortunately (0, 0) doesn't work so that it
180 // doesn't appear in expose
181 SetSize(-10000, -10000, 1, 1);
186 void wxMDIParentFrame::MacActivate(long timestamp
, bool activating
)
188 wxLogTrace(TRACE_MDI
, wxT("MDI PARENT=%p MacActivate(0x%08lx,%s)"),this,timestamp
,activating
?wxT("ACTIV"):wxT("deact"));
191 if(s_macDeactivateWindow
&& s_macDeactivateWindow
->GetParent()==this)
193 wxLogTrace(TRACE_MDI
, wxT("child had been scheduled for deactivation, rehighlighting"));
194 UMAHighlightAndActivateWindow((WindowRef
)s_macDeactivateWindow
->MacGetWindowRef(), true);
195 wxLogTrace(TRACE_MDI
, wxT("done highliting child"));
196 s_macDeactivateWindow
= NULL
;
198 else if(s_macDeactivateWindow
== this)
200 wxLogTrace(TRACE_MDI
, wxT("Avoided deactivation/activation of this=%p"), this);
201 s_macDeactivateWindow
= NULL
;
203 else // window to deactivate is NULL or is not us or one of our kids
205 // activate kid instead
207 m_currentChild
->MacActivate(timestamp
,activating
);
209 wxFrame::MacActivate(timestamp
,activating
);
214 // We were scheduled for deactivation, and now we do it.
215 if(s_macDeactivateWindow
==this)
217 s_macDeactivateWindow
= NULL
;
219 m_currentChild
->MacActivate(timestamp
,activating
);
220 wxFrame::MacActivate(timestamp
,activating
);
222 else // schedule ourselves for deactivation
224 if(s_macDeactivateWindow
)
225 wxLogTrace(TRACE_MDI
, wxT("window=%p SHOULD have been deactivated, oh well!"),s_macDeactivateWindow
);
226 wxLogTrace(TRACE_MDI
, wxT("Scheduling delayed MDI Parent deactivation"));
227 s_macDeactivateWindow
= this;
232 void wxMDIParentFrame::OnActivate(wxActivateEvent
& event
)
237 // Returns the active MDI child window
238 wxMDIChildFrame
*wxMDIParentFrame::GetActiveChild() const
240 return m_currentChild
;
243 // Create the client window class (don't Create the window,
244 // just return a new class)
245 wxMDIClientWindow
*wxMDIParentFrame::OnCreateClient()
247 m_clientWindow
= new wxMDIClientWindow( this );
248 return m_clientWindow
;
251 // Responds to colour changes, and passes event on to children.
252 void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
256 // Propagate the event to the non-top-level children
257 wxFrame::OnSysColourChanged(event
);
261 void wxMDIParentFrame::Cascade()
266 void wxMDIParentFrame::Tile(wxOrientation
WXUNUSED(orient
))
271 void wxMDIParentFrame::ArrangeIcons()
276 void wxMDIParentFrame::ActivateNext()
281 void wxMDIParentFrame::ActivatePrevious()
286 bool wxMDIParentFrame::ShouldBeVisible() const
288 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
290 node
= node
->GetNext() )
292 wxWindow
*win
= node
->GetData();
295 && !wxDynamicCast(win
, wxMDIChildFrame
)
297 && win
!= GetStatusBar()
298 #endif // wxUSE_STATUSBAR
299 && win
!= GetClientWindow() )
301 // if we have a non-MDI child, do remain visible so that it could
310 bool wxMDIParentFrame::Show( bool show
)
312 m_shouldBeShown
= false;
314 // don't really show the MDI frame unless it has any children other than
315 // MDI children as it is pretty useless in this case
319 if ( !ShouldBeVisible() && m_currentChild
)
321 // don't make the window visible now but remember that we should
323 m_shouldBeShown
= true;
328 return wxFrame::Show(show
);
331 // ----------------------------------------------------------------------------
333 // ----------------------------------------------------------------------------
335 wxMDIChildFrame::wxMDIChildFrame()
339 void wxMDIChildFrame::Init()
343 bool wxMDIChildFrame::Create(wxMDIParentFrame
*parent
,
345 const wxString
& title
,
349 const wxString
& name
)
356 m_windowId
= (int)NewControlId();
358 if (parent
) parent
->AddChild(this);
360 MacCreateRealWindow( title
, pos
, size
, MacRemoveBordersFromStyle(style
) , name
) ;
362 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
364 wxModelessWindows
.Append(this);
368 wxMDIChildFrame::~wxMDIChildFrame()
373 void wxMDIChildFrame::SetMenuBar(wxMenuBar
*menu_bar
)
375 return wxFrame::SetMenuBar( menu_bar
) ;
378 void wxMDIChildFrame::MacActivate(long timestamp
, bool activating
)
380 wxLogTrace(TRACE_MDI
, wxT("MDI child=%p MacActivate(0x%08lx,%s)"),this,timestamp
,activating
?wxT("ACTIV"):wxT("deact"));
381 wxMDIParentFrame
*mdiparent
= wxDynamicCast(m_parent
, wxMDIParentFrame
);
385 if(s_macDeactivateWindow
== m_parent
)
387 wxLogTrace(TRACE_MDI
, wxT("parent had been scheduled for deactivation, rehighlighting"));
388 UMAHighlightAndActivateWindow((WindowRef
)s_macDeactivateWindow
->MacGetWindowRef(), true);
389 wxLogTrace(TRACE_MDI
, wxT("done highliting parent"));
390 s_macDeactivateWindow
= NULL
;
392 else if((mdiparent
->m_currentChild
==this) || !s_macDeactivateWindow
)
393 mdiparent
->wxFrame::MacActivate(timestamp
,activating
);
395 if(mdiparent
->m_currentChild
&& mdiparent
->m_currentChild
!=this)
396 mdiparent
->m_currentChild
->wxFrame::MacActivate(timestamp
,false);
397 mdiparent
->m_currentChild
= this;
399 if(s_macDeactivateWindow
==this)
401 wxLogTrace(TRACE_MDI
, wxT("Avoided deactivation/activation of this=%p"),this);
402 s_macDeactivateWindow
=NULL
;
405 wxFrame::MacActivate(timestamp
, activating
);
409 // We were scheduled for deactivation, and now we do it.
410 if(s_macDeactivateWindow
==this)
412 s_macDeactivateWindow
= NULL
;
413 wxFrame::MacActivate(timestamp
,activating
);
414 if(mdiparent
->m_currentChild
==this)
415 mdiparent
->wxFrame::MacActivate(timestamp
,activating
);
417 else // schedule ourselves for deactivation
419 if(s_macDeactivateWindow
)
420 wxLogTrace(TRACE_MDI
, wxT("window=%p SHOULD have been deactivated, oh well!"),s_macDeactivateWindow
);
421 wxLogTrace(TRACE_MDI
, wxT("Scheduling delayed deactivation"));
422 s_macDeactivateWindow
= this;
428 void wxMDIChildFrame::Maximize()
430 wxFrame::Maximize() ;
433 void wxMDIChildFrame::Restore()
438 void wxMDIChildFrame::Activate()
442 //-----------------------------------------------------------------------------
444 //-----------------------------------------------------------------------------
446 wxMDIClientWindow::wxMDIClientWindow()
450 wxMDIClientWindow::~wxMDIClientWindow()
455 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame
*parent
, long style
)
457 if ( !wxWindow::Create(parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, style
))
460 wxModelessWindows
.Append(this);
464 // Get size *available for subwindows* i.e. excluding menu bar.
465 void wxMDIClientWindow::DoGetClientSize(int *x
, int *y
) const
467 wxDisplaySize( x
, y
) ;
470 // Explicitly call default scroll behaviour
471 void wxMDIClientWindow::OnScroll(wxScrollEvent
& event
)