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 wxUnusedVar(inActivate
);
75 wxUnusedVar(inWindowRef
);
76 // TODO: implement me!
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 void wxMDIParentFrame::Init()
86 m_parentFrameActive
= true;
87 m_shouldBeShown
= false;
90 bool wxMDIParentFrame::Create(wxWindow
*parent
,
92 const wxString
& title
,
98 // this style can be used to prevent a window from having the standard MDI
100 if ( style
& wxFRAME_NO_WINDOW_MENU
)
103 style
-= wxFRAME_NO_WINDOW_MENU
;
105 else // normal case: we have the window menu, so construct it
107 m_windowMenu
= new wxMenu
;
109 m_windowMenu
->Append(IDM_WINDOWCASCADE
, wxT("&Cascade"));
110 m_windowMenu
->Append(IDM_WINDOWTILEHOR
, wxT("Tile &Horizontally"));
111 m_windowMenu
->Append(IDM_WINDOWTILEVERT
, wxT("Tile &Vertically"));
112 m_windowMenu
->AppendSeparator();
113 m_windowMenu
->Append(IDM_WINDOWICONS
, wxT("&Arrange Icons"));
114 m_windowMenu
->Append(IDM_WINDOWNEXT
, wxT("&Next"));
117 if ( !wxFrame::Create( parent
, id
, title
, pos
, size
, style
, name
) )
120 m_parentFrameActive
= true;
122 m_clientWindow
= OnCreateClient();
123 if ( !m_clientWindow
|| !m_clientWindow
->CreateClient(this, style
) )
129 wxMDIParentFrame::~wxMDIParentFrame()
133 // already deleted by DestroyChildren()
134 m_clientWindow
= NULL
;
137 void wxMDIParentFrame::SetMenuBar(wxMenuBar
*menu_bar
)
139 wxFrame::SetMenuBar( menu_bar
) ;
142 void wxMDIParentFrame::GetRectForTopLevelChildren(int *x
, int *y
, int *w
, int *h
)
152 void wxMDIParentFrame::AddChild(wxWindowBase
*child
)
154 // moved this to front, so that we don't run into unset m_parent problems later
155 wxFrame::AddChild(child
);
157 if ( !m_currentChild
)
159 m_currentChild
= wxDynamicCast(child
, wxMDIChildFrame
);
161 if ( m_currentChild
&& IsShown() && !ShouldBeVisible() )
163 // we shouldn't remain visible any more
164 wxFrame::Show(false);
165 m_shouldBeShown
= true;
170 void wxMDIParentFrame::RemoveChild(wxWindowBase
*child
)
172 if ( child
== m_currentChild
)
174 // the current child isn't active any more, try to find another one
175 m_currentChild
= NULL
;
177 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
179 node
= node
->GetNext() )
182 childCur
= wxDynamicCast(node
->GetData(), wxMDIChildFrame
);
183 if ( childCur
!= child
)
185 m_currentChild
= childCur
;
191 wxFrame::RemoveChild(child
);
193 // if there are no more children left we need to show the frame if we
194 // hadn't shown it before because there were active children and it was
195 // useless (note that we have to do it after fully removing the child, i.e.
196 // after calling the base class RemoveChild() as otherwise we risk to touch
197 // pointer to the child being deleted)
198 if ( !m_currentChild
&& m_shouldBeShown
&& !IsShown() )
200 // we have to show it, but at least move it out of sight and make it of
201 // smallest possible size (unfortunately (0, 0) doesn't work so that it
202 // doesn't appear in expose
203 SetSize(-10000, -10000, 1, 1);
208 void wxMDIParentFrame::MacActivate(long timestamp
, bool activating
)
210 wxLogTrace(TRACE_MDI
, wxT("MDI PARENT=%p MacActivate(0x%08lx,%s)"), this, timestamp
, activating
? wxT("ACTIV") : wxT("deact"));
214 if (s_macDeactivateWindow
&& s_macDeactivateWindow
->GetParent() == this)
216 wxLogTrace(TRACE_MDI
, wxT("child had been scheduled for deactivation, rehighlighting"));
218 UMAHighlightAndActivateWindow((WindowRef
)s_macDeactivateWindow
->GetWXWindow(), true);
220 wxLogTrace(TRACE_MDI
, wxT("finished highliting child"));
222 s_macDeactivateWindow
= NULL
;
224 else if (s_macDeactivateWindow
== this)
226 wxLogTrace(TRACE_MDI
, wxT("Avoided deactivation/activation of this=%p"), this);
228 s_macDeactivateWindow
= NULL
;
230 else // window to deactivate is NULL or is not us or one of our kids
232 // activate kid instead
234 m_currentChild
->MacActivate(timestamp
, activating
);
236 wxFrame::MacActivate(timestamp
, activating
);
241 // We were scheduled for deactivation, and now we do it.
242 if (s_macDeactivateWindow
== this)
244 s_macDeactivateWindow
= NULL
;
246 m_currentChild
->MacActivate(timestamp
, activating
);
247 wxFrame::MacActivate(timestamp
, activating
);
249 else // schedule ourselves for deactivation
251 if (s_macDeactivateWindow
)
252 wxLogTrace(TRACE_MDI
, wxT("window=%p SHOULD have been deactivated, oh well!"), s_macDeactivateWindow
);
253 wxLogTrace(TRACE_MDI
, wxT("Scheduling delayed MDI Parent deactivation"));
255 s_macDeactivateWindow
= this;
260 void wxMDIParentFrame::OnActivate(wxActivateEvent
& event
)
265 // Responds to colour changes, and passes event on to children.
266 void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
270 // Propagate the event to the non-top-level children
271 wxFrame::OnSysColourChanged(event
);
274 bool wxMDIParentFrame::ShouldBeVisible() const
276 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
278 node
= node
->GetNext() )
280 wxWindow
*win
= node
->GetData();
283 && !wxDynamicCast(win
, wxMDIChildFrame
)
285 && win
!= (wxWindow
*) GetStatusBar()
287 && win
!= GetClientWindow() )
289 // if we have a non-MDI child, do remain visible so that it could
298 bool wxMDIParentFrame::Show( bool show
)
300 m_shouldBeShown
= false;
302 // don't really show the MDI frame unless it has any children other than
303 // MDI children as it is pretty useless in this case
307 if ( !ShouldBeVisible() && m_currentChild
)
309 // don't make the window visible now but remember that we should
311 m_shouldBeShown
= true;
317 return wxFrame::Show(show
);
320 // ----------------------------------------------------------------------------
322 // ----------------------------------------------------------------------------
324 void wxMDIChildFrame::Init()
328 bool wxMDIChildFrame::Create(wxMDIParentFrame
*parent
,
330 const wxString
& title
,
334 const wxString
& name
)
336 m_mdiParent
= parent
;
340 if ( id
== wxID_ANY
)
341 id
= (int)NewControlId();
343 wxNonOwnedWindow::Create( parent
, id
, pos
, size
, MacRemoveBordersFromStyle(style
) , name
) ;
347 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
352 wxMDIChildFrame::~wxMDIChildFrame()
357 void wxMDIChildFrame::MacActivate(long timestamp
, bool activating
)
359 wxLogTrace(TRACE_MDI
, wxT("MDI child=%p MacActivate(0x%08lx,%s)"),this, timestamp
, activating
? wxT("ACTIV") : wxT("deact"));
361 wxMDIParentFrame
*mdiparent
= wxDynamicCast(m_parent
, wxMDIParentFrame
);
366 if (s_macDeactivateWindow
== m_parent
)
368 wxLogTrace(TRACE_MDI
, wxT("parent had been scheduled for deactivation, rehighlighting"));
370 UMAHighlightAndActivateWindow((WindowRef
)s_macDeactivateWindow
->GetWXWindow(), true);
372 wxLogTrace(TRACE_MDI
, wxT("finished highliting parent"));
374 s_macDeactivateWindow
= NULL
;
376 else if ((mdiparent
->m_currentChild
== this) || !s_macDeactivateWindow
)
377 mdiparent
->wxFrame::MacActivate(timestamp
, activating
);
379 if (mdiparent
->m_currentChild
&& mdiparent
->m_currentChild
!= this)
380 mdiparent
->m_currentChild
->wxFrame::MacActivate(timestamp
, false);
381 mdiparent
->m_currentChild
= this;
383 if (s_macDeactivateWindow
== this)
385 wxLogTrace(TRACE_MDI
, wxT("Avoided deactivation/activation of this=%p"), this);
387 s_macDeactivateWindow
= NULL
;
390 wxFrame::MacActivate(timestamp
, activating
);
394 // We were scheduled for deactivation, and now we do it.
395 if (s_macDeactivateWindow
== this)
397 s_macDeactivateWindow
= NULL
;
398 wxFrame::MacActivate(timestamp
, activating
);
399 if (mdiparent
->m_currentChild
== this)
400 mdiparent
->wxFrame::MacActivate(timestamp
, activating
);
402 else // schedule ourselves for deactivation
404 if (s_macDeactivateWindow
)
405 wxLogTrace(TRACE_MDI
, wxT("window=%p SHOULD have been deactivated, oh well!"), s_macDeactivateWindow
);
406 wxLogTrace(TRACE_MDI
, wxT("Scheduling delayed deactivation"));
408 s_macDeactivateWindow
= this;
414 void wxMDIChildFrame::Activate()
419 //-----------------------------------------------------------------------------
421 //-----------------------------------------------------------------------------
423 wxMDIClientWindow::~wxMDIClientWindow()
428 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame
*parent
, long style
)
430 if ( !wxWindow::Create(parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, style
) )
436 void wxMDIClientWindow::DoGetClientSize(int *x
, int *y
) const
438 wxDisplaySize( x
, y
) ;