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();
122 return m_clientWindow
!= NULL
;
125 wxMDIParentFrame::~wxMDIParentFrame()
129 // already deleted by DestroyChildren()
130 m_clientWindow
= NULL
;
133 void wxMDIParentFrame::SetMenuBar(wxMenuBar
*menu_bar
)
135 wxFrame::SetMenuBar( menu_bar
) ;
138 void wxMDIParentFrame::GetRectForTopLevelChildren(int *x
, int *y
, int *w
, int *h
)
148 void wxMDIParentFrame::AddChild(wxWindowBase
*child
)
150 // moved this to front, so that we don't run into unset m_parent problems later
151 wxFrame::AddChild(child
);
153 if ( !m_currentChild
)
155 m_currentChild
= wxDynamicCast(child
, wxMDIChildFrame
);
157 if ( m_currentChild
&& IsShown() && !ShouldBeVisible() )
159 // we shouldn't remain visible any more
160 wxFrame::Show(false);
161 m_shouldBeShown
= true;
166 void wxMDIParentFrame::RemoveChild(wxWindowBase
*child
)
168 if ( child
== m_currentChild
)
170 // the current child isn't active any more, try to find another one
171 m_currentChild
= NULL
;
173 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
175 node
= node
->GetNext() )
178 childCur
= wxDynamicCast(node
->GetData(), wxMDIChildFrame
);
179 if ( childCur
!= child
)
181 m_currentChild
= childCur
;
187 wxFrame::RemoveChild(child
);
189 // if there are no more children left we need to show the frame if we
190 // hadn't shown it before because there were active children and it was
191 // useless (note that we have to do it after fully removing the child, i.e.
192 // after calling the base class RemoveChild() as otherwise we risk to touch
193 // pointer to the child being deleted)
194 if ( !m_currentChild
&& m_shouldBeShown
&& !IsShown() )
196 // we have to show it, but at least move it out of sight and make it of
197 // smallest possible size (unfortunately (0, 0) doesn't work so that it
198 // doesn't appear in expose
199 SetSize(-10000, -10000, 1, 1);
204 void wxMDIParentFrame::MacActivate(long timestamp
, bool activating
)
206 wxLogTrace(TRACE_MDI
, wxT("MDI PARENT=%p MacActivate(0x%08lx,%s)"), this, timestamp
, activating
? wxT("ACTIV") : wxT("deact"));
210 if (s_macDeactivateWindow
&& s_macDeactivateWindow
->GetParent() == this)
212 wxLogTrace(TRACE_MDI
, wxT("child had been scheduled for deactivation, rehighlighting"));
214 UMAHighlightAndActivateWindow((WindowRef
)s_macDeactivateWindow
->GetWXWindow(), true);
216 wxLogTrace(TRACE_MDI
, wxT("finished highliting child"));
218 s_macDeactivateWindow
= NULL
;
220 else if (s_macDeactivateWindow
== this)
222 wxLogTrace(TRACE_MDI
, wxT("Avoided deactivation/activation of this=%p"), this);
224 s_macDeactivateWindow
= NULL
;
226 else // window to deactivate is NULL or is not us or one of our kids
228 // activate kid instead
230 m_currentChild
->MacActivate(timestamp
, activating
);
232 wxFrame::MacActivate(timestamp
, activating
);
237 // We were scheduled for deactivation, and now we do it.
238 if (s_macDeactivateWindow
== this)
240 s_macDeactivateWindow
= NULL
;
242 m_currentChild
->MacActivate(timestamp
, activating
);
243 wxFrame::MacActivate(timestamp
, activating
);
245 else // schedule ourselves for deactivation
247 if (s_macDeactivateWindow
)
248 wxLogTrace(TRACE_MDI
, wxT("window=%p SHOULD have been deactivated, oh well!"), s_macDeactivateWindow
);
249 wxLogTrace(TRACE_MDI
, wxT("Scheduling delayed MDI Parent deactivation"));
251 s_macDeactivateWindow
= this;
256 void wxMDIParentFrame::OnActivate(wxActivateEvent
& event
)
261 // Responds to colour changes, and passes event on to children.
262 void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
266 // Propagate the event to the non-top-level children
267 wxFrame::OnSysColourChanged(event
);
270 bool wxMDIParentFrame::ShouldBeVisible() const
272 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
274 node
= node
->GetNext() )
276 wxWindow
*win
= node
->GetData();
279 && !wxDynamicCast(win
, wxMDIChildFrame
)
281 && win
!= (wxWindow
*) GetStatusBar()
283 && win
!= GetClientWindow() )
285 // if we have a non-MDI child, do remain visible so that it could
294 bool wxMDIParentFrame::Show( bool show
)
296 m_shouldBeShown
= false;
298 // don't really show the MDI frame unless it has any children other than
299 // MDI children as it is pretty useless in this case
303 if ( !ShouldBeVisible() && m_currentChild
)
305 // don't make the window visible now but remember that we should
307 m_shouldBeShown
= true;
313 return wxFrame::Show(show
);
316 // ----------------------------------------------------------------------------
318 // ----------------------------------------------------------------------------
320 void wxMDIChildFrame::Init()
324 bool wxMDIChildFrame::Create(wxMDIParentFrame
*parent
,
326 const wxString
& title
,
330 const wxString
& name
)
332 m_mdiParent
= parent
;
336 if ( id
== wxID_ANY
)
337 id
= (int)NewControlId();
339 wxNonOwnedWindow::Create( parent
, id
, pos
, size
, MacRemoveBordersFromStyle(style
) , name
) ;
343 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
348 wxMDIChildFrame::~wxMDIChildFrame()
353 void wxMDIChildFrame::MacActivate(long timestamp
, bool activating
)
355 wxLogTrace(TRACE_MDI
, wxT("MDI child=%p MacActivate(0x%08lx,%s)"),this, timestamp
, activating
? wxT("ACTIV") : wxT("deact"));
357 wxMDIParentFrame
*mdiparent
= wxDynamicCast(m_parent
, wxMDIParentFrame
);
362 if (s_macDeactivateWindow
== m_parent
)
364 wxLogTrace(TRACE_MDI
, wxT("parent had been scheduled for deactivation, rehighlighting"));
366 UMAHighlightAndActivateWindow((WindowRef
)s_macDeactivateWindow
->GetWXWindow(), true);
368 wxLogTrace(TRACE_MDI
, wxT("finished highliting parent"));
370 s_macDeactivateWindow
= NULL
;
372 else if ((mdiparent
->m_currentChild
== this) || !s_macDeactivateWindow
)
373 mdiparent
->wxFrame::MacActivate(timestamp
, activating
);
375 if (mdiparent
->m_currentChild
&& mdiparent
->m_currentChild
!= this)
376 mdiparent
->m_currentChild
->wxFrame::MacActivate(timestamp
, false);
377 mdiparent
->m_currentChild
= this;
379 if (s_macDeactivateWindow
== this)
381 wxLogTrace(TRACE_MDI
, wxT("Avoided deactivation/activation of this=%p"), this);
383 s_macDeactivateWindow
= NULL
;
386 wxFrame::MacActivate(timestamp
, activating
);
390 // We were scheduled for deactivation, and now we do it.
391 if (s_macDeactivateWindow
== this)
393 s_macDeactivateWindow
= NULL
;
394 wxFrame::MacActivate(timestamp
, activating
);
395 if (mdiparent
->m_currentChild
== this)
396 mdiparent
->wxFrame::MacActivate(timestamp
, activating
);
398 else // schedule ourselves for deactivation
400 if (s_macDeactivateWindow
)
401 wxLogTrace(TRACE_MDI
, wxT("window=%p SHOULD have been deactivated, oh well!"), s_macDeactivateWindow
);
402 wxLogTrace(TRACE_MDI
, wxT("Scheduling delayed deactivation"));
404 s_macDeactivateWindow
= this;
410 void wxMDIChildFrame::Activate()
415 //-----------------------------------------------------------------------------
417 //-----------------------------------------------------------------------------
419 wxMDIClientWindow::~wxMDIClientWindow()
424 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame
*parent
, long style
)
426 if ( !wxWindow::Create(parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, style
) )
432 void wxMDIClientWindow::DoGetClientSize(int *x
, int *y
) const
434 wxDisplaySize( x
, y
) ;