1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/mdi.cpp
3 // Purpose: MDI classes
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
21 #include "wx/settings.h"
22 #include "wx/statusbr.h"
25 #include "wx/osx/private.h"
26 #include "wx/osx/uma.h"
28 IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame
, wxFrame
)
29 IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame
, wxFrame
)
30 IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow
, wxWindow
)
32 BEGIN_EVENT_TABLE(wxMDIParentFrame
, wxFrame
)
33 EVT_ACTIVATE(wxMDIParentFrame::OnActivate
)
34 EVT_SYS_COLOUR_CHANGED(wxMDIParentFrame::OnSysColourChanged
)
37 #define TRACE_MDI "mdi"
39 static const int IDM_WINDOWTILEHOR
= 4001;
40 static const int IDM_WINDOWCASCADE
= 4002;
41 static const int IDM_WINDOWICONS
= 4003;
42 static const int IDM_WINDOWNEXT
= 4004;
43 static const int IDM_WINDOWTILEVERT
= 4005;
47 void UMAHighlightAndActivateWindow( WindowRef inWindowRef
, bool inActivate
)
49 #if wxOSX_USE_CARBON // TODO REMOVE
52 // bool isHighlighted = IsWindowHighlited( inWindowRef ) ;
53 // if ( inActivate != isHighlighted )
57 SetPortWindowPort( inWindowRef
) ;
59 HiliteWindow( inWindowRef
, inActivate
) ;
60 ControlRef control
= NULL
;
61 ::GetRootControl( inWindowRef
, &control
) ;
65 ::ActivateControl( control
) ;
67 ::DeactivateControl( control
) ;
73 #elif defined(wxOSX_USE_COCOA)
74 // TODO: implement me!
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 void wxMDIParentFrame::Init()
84 m_parentFrameActive
= true;
85 m_shouldBeShown
= false;
88 bool wxMDIParentFrame::Create(wxWindow
*parent
,
90 const wxString
& title
,
96 // this style can be used to prevent a window from having the standard MDI
98 if ( style
& wxFRAME_NO_WINDOW_MENU
)
101 style
-= wxFRAME_NO_WINDOW_MENU
;
103 else // normal case: we have the window menu, so construct it
105 m_windowMenu
= new wxMenu
;
107 m_windowMenu
->Append(IDM_WINDOWCASCADE
, wxT("&Cascade"));
108 m_windowMenu
->Append(IDM_WINDOWTILEHOR
, wxT("Tile &Horizontally"));
109 m_windowMenu
->Append(IDM_WINDOWTILEVERT
, wxT("Tile &Vertically"));
110 m_windowMenu
->AppendSeparator();
111 m_windowMenu
->Append(IDM_WINDOWICONS
, wxT("&Arrange Icons"));
112 m_windowMenu
->Append(IDM_WINDOWNEXT
, wxT("&Next"));
115 if ( !wxFrame::Create( parent
, id
, title
, pos
, size
, style
, name
) )
118 m_parentFrameActive
= true;
120 m_clientWindow
= OnCreateClient();
121 if ( !m_clientWindow
|| !m_clientWindow
->CreateClient(this, style
) )
127 wxMDIParentFrame::~wxMDIParentFrame()
131 // already deleted by DestroyChildren()
132 m_clientWindow
= NULL
;
135 void wxMDIParentFrame::SetMenuBar(wxMenuBar
*menu_bar
)
137 wxFrame::SetMenuBar( menu_bar
) ;
140 void wxMDIParentFrame::GetRectForTopLevelChildren(int *x
, int *y
, int *w
, int *h
)
150 void wxMDIParentFrame::AddChild(wxWindowBase
*child
)
152 // moved this to front, so that we don't run into unset m_parent problems later
153 wxFrame::AddChild(child
);
155 if ( !m_currentChild
)
157 m_currentChild
= wxDynamicCast(child
, wxMDIChildFrame
);
159 if ( m_currentChild
&& IsShown() && !ShouldBeVisible() )
161 // we shouldn't remain visible any more
162 wxFrame::Show(false);
163 m_shouldBeShown
= true;
168 void wxMDIParentFrame::RemoveChild(wxWindowBase
*child
)
170 if ( child
== m_currentChild
)
172 // the current child isn't active any more, try to find another one
173 m_currentChild
= NULL
;
175 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
177 node
= node
->GetNext() )
180 childCur
= wxDynamicCast(node
->GetData(), wxMDIChildFrame
);
181 if ( childCur
!= child
)
183 m_currentChild
= childCur
;
189 wxFrame::RemoveChild(child
);
191 // if there are no more children left we need to show the frame if we
192 // hadn't shown it before because there were active children and it was
193 // useless (note that we have to do it after fully removing the child, i.e.
194 // after calling the base class RemoveChild() as otherwise we risk to touch
195 // pointer to the child being deleted)
196 if ( !m_currentChild
&& m_shouldBeShown
&& !IsShown() )
198 // we have to show it, but at least move it out of sight and make it of
199 // smallest possible size (unfortunately (0, 0) doesn't work so that it
200 // doesn't appear in expose
201 SetSize(-10000, -10000, 1, 1);
206 void wxMDIParentFrame::MacActivate(long timestamp
, bool activating
)
208 wxLogTrace(TRACE_MDI
, wxT("MDI PARENT=%p MacActivate(0x%08lx,%s)"), this, timestamp
, activating
? wxT("ACTIV") : wxT("deact"));
212 if (s_macDeactivateWindow
&& s_macDeactivateWindow
->GetParent() == this)
214 wxLogTrace(TRACE_MDI
, wxT("child had been scheduled for deactivation, rehighlighting"));
216 UMAHighlightAndActivateWindow((WindowRef
)s_macDeactivateWindow
->GetWXWindow(), true);
218 wxLogTrace(TRACE_MDI
, wxT("finished highliting child"));
220 s_macDeactivateWindow
= NULL
;
222 else if (s_macDeactivateWindow
== this)
224 wxLogTrace(TRACE_MDI
, wxT("Avoided deactivation/activation of this=%p"), this);
226 s_macDeactivateWindow
= NULL
;
228 else // window to deactivate is NULL or is not us or one of our kids
230 // activate kid instead
232 m_currentChild
->MacActivate(timestamp
, activating
);
234 wxFrame::MacActivate(timestamp
, activating
);
239 // We were scheduled for deactivation, and now we do it.
240 if (s_macDeactivateWindow
== this)
242 s_macDeactivateWindow
= NULL
;
244 m_currentChild
->MacActivate(timestamp
, activating
);
245 wxFrame::MacActivate(timestamp
, activating
);
247 else // schedule ourselves for deactivation
249 if (s_macDeactivateWindow
)
250 wxLogTrace(TRACE_MDI
, wxT("window=%p SHOULD have been deactivated, oh well!"), s_macDeactivateWindow
);
251 wxLogTrace(TRACE_MDI
, wxT("Scheduling delayed MDI Parent deactivation"));
253 s_macDeactivateWindow
= this;
258 void wxMDIParentFrame::OnActivate(wxActivateEvent
& event
)
263 // Responds to colour changes, and passes event on to children.
264 void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
268 // Propagate the event to the non-top-level children
269 wxFrame::OnSysColourChanged(event
);
272 bool wxMDIParentFrame::ShouldBeVisible() const
274 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
276 node
= node
->GetNext() )
278 wxWindow
*win
= node
->GetData();
281 && !wxDynamicCast(win
, wxMDIChildFrame
)
283 && win
!= (wxWindow
*) GetStatusBar()
285 && win
!= GetClientWindow() )
287 // if we have a non-MDI child, do remain visible so that it could
296 bool wxMDIParentFrame::Show( bool show
)
298 m_shouldBeShown
= false;
300 // don't really show the MDI frame unless it has any children other than
301 // MDI children as it is pretty useless in this case
305 if ( !ShouldBeVisible() && m_currentChild
)
307 // don't make the window visible now but remember that we should
309 m_shouldBeShown
= true;
315 return wxFrame::Show(show
);
318 // ----------------------------------------------------------------------------
320 // ----------------------------------------------------------------------------
322 void wxMDIChildFrame::Init()
326 bool wxMDIChildFrame::Create(wxMDIParentFrame
*parent
,
328 const wxString
& title
,
332 const wxString
& name
)
334 m_mdiParent
= parent
;
338 if ( id
== wxID_ANY
)
339 id
= (int)NewControlId();
341 wxNonOwnedWindow::Create( parent
, id
, pos
, size
, MacRemoveBordersFromStyle(style
) , name
) ;
345 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
350 wxMDIChildFrame::~wxMDIChildFrame()
355 void wxMDIChildFrame::MacActivate(long timestamp
, bool activating
)
357 wxLogTrace(TRACE_MDI
, wxT("MDI child=%p MacActivate(0x%08lx,%s)"),this, timestamp
, activating
? wxT("ACTIV") : wxT("deact"));
359 wxMDIParentFrame
*mdiparent
= wxDynamicCast(m_parent
, wxMDIParentFrame
);
364 if (s_macDeactivateWindow
== m_parent
)
366 wxLogTrace(TRACE_MDI
, wxT("parent had been scheduled for deactivation, rehighlighting"));
368 UMAHighlightAndActivateWindow((WindowRef
)s_macDeactivateWindow
->GetWXWindow(), true);
370 wxLogTrace(TRACE_MDI
, wxT("finished highliting parent"));
372 s_macDeactivateWindow
= NULL
;
374 else if ((mdiparent
->m_currentChild
== this) || !s_macDeactivateWindow
)
375 mdiparent
->wxFrame::MacActivate(timestamp
, activating
);
377 if (mdiparent
->m_currentChild
&& mdiparent
->m_currentChild
!= this)
378 mdiparent
->m_currentChild
->wxFrame::MacActivate(timestamp
, false);
379 mdiparent
->m_currentChild
= this;
381 if (s_macDeactivateWindow
== this)
383 wxLogTrace(TRACE_MDI
, wxT("Avoided deactivation/activation of this=%p"), this);
385 s_macDeactivateWindow
= NULL
;
388 wxFrame::MacActivate(timestamp
, activating
);
392 // We were scheduled for deactivation, and now we do it.
393 if (s_macDeactivateWindow
== this)
395 s_macDeactivateWindow
= NULL
;
396 wxFrame::MacActivate(timestamp
, activating
);
397 if (mdiparent
->m_currentChild
== this)
398 mdiparent
->wxFrame::MacActivate(timestamp
, activating
);
400 else // schedule ourselves for deactivation
402 if (s_macDeactivateWindow
)
403 wxLogTrace(TRACE_MDI
, wxT("window=%p SHOULD have been deactivated, oh well!"), s_macDeactivateWindow
);
404 wxLogTrace(TRACE_MDI
, wxT("Scheduling delayed deactivation"));
406 s_macDeactivateWindow
= this;
412 void wxMDIChildFrame::Activate()
417 //-----------------------------------------------------------------------------
419 //-----------------------------------------------------------------------------
421 wxMDIClientWindow::~wxMDIClientWindow()
426 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame
*parent
, long style
)
428 if ( !wxWindow::Create(parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, style
) )
434 void wxMDIClientWindow::DoGetClientSize(int *x
, int *y
) const
436 wxDisplaySize( x
, y
) ;