1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/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/mac/private.h"
26 #include "wx/mac/uma.h"
28 extern wxWindowList wxModelessWindows
;
30 IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame
, wxFrame
)
31 IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame
, wxFrame
)
32 IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow
, wxWindow
)
34 BEGIN_EVENT_TABLE(wxMDIParentFrame
, wxFrame
)
35 EVT_ACTIVATE(wxMDIParentFrame::OnActivate
)
36 EVT_SYS_COLOUR_CHANGED(wxMDIParentFrame::OnSysColourChanged
)
39 BEGIN_EVENT_TABLE(wxMDIClientWindow
, wxWindow
)
40 EVT_SCROLL(wxMDIClientWindow::OnScroll
)
43 static const wxChar
*TRACE_MDI
= _T("mdi");
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;
53 void UMAHighlightAndActivateWindow( WindowRef inWindowRef
, bool inActivate
)
58 // bool isHighlighted = IsWindowHighlited( inWindowRef ) ;
59 // if ( inActivate != isHighlighted )
63 SetPortWindowPort( inWindowRef
) ;
65 HiliteWindow( inWindowRef
, inActivate
) ;
66 ControlRef control
= NULL
;
67 ::GetRootControl( inWindowRef
, &control
) ;
71 ::ActivateControl( control
) ;
73 ::DeactivateControl( control
) ;
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 void wxMDIParentFrame::Init()
88 m_clientWindow
= NULL
;
89 m_currentChild
= NULL
;
90 m_windowMenu
= (wxMenu
*) NULL
;
91 m_parentFrameActive
= true;
92 m_shouldBeShown
= false;
95 bool wxMDIParentFrame::Create(wxWindow
*parent
,
97 const wxString
& title
,
101 const wxString
& name
)
103 // this style can be used to prevent a window from having the standard MDI
105 if ( style
& wxFRAME_NO_WINDOW_MENU
)
107 m_windowMenu
= (wxMenu
*)NULL
;
108 style
-= wxFRAME_NO_WINDOW_MENU
;
110 else // normal case: we have the window menu, so construct it
112 m_windowMenu
= new wxMenu
;
114 m_windowMenu
->Append(IDM_WINDOWCASCADE
, wxT("&Cascade"));
115 m_windowMenu
->Append(IDM_WINDOWTILEHOR
, wxT("Tile &Horizontally"));
116 m_windowMenu
->Append(IDM_WINDOWTILEVERT
, wxT("Tile &Vertically"));
117 m_windowMenu
->AppendSeparator();
118 m_windowMenu
->Append(IDM_WINDOWICONS
, wxT("&Arrange Icons"));
119 m_windowMenu
->Append(IDM_WINDOWNEXT
, wxT("&Next"));
122 if ( !wxFrame::Create( parent
, id
, title
, pos
, size
, style
, name
) )
125 m_parentFrameActive
= true;
127 m_clientWindow
= OnCreateClient();
129 return m_clientWindow
!= NULL
;
132 wxMDIParentFrame::~wxMDIParentFrame()
136 // already deleted by DestroyChildren()
137 m_clientWindow
= NULL
;
142 void wxMDIParentFrame::SetMenuBar(wxMenuBar
*menu_bar
)
144 wxFrame::SetMenuBar( menu_bar
) ;
147 void wxMDIParentFrame::GetRectForTopLevelChildren(int *x
, int *y
, int *w
, int *h
)
157 void wxMDIParentFrame::AddChild(wxWindowBase
*child
)
159 // moved this to front, so that we don't run into unset m_parent problems later
160 wxFrame::AddChild(child
);
162 if ( !m_currentChild
)
164 m_currentChild
= wxDynamicCast(child
, wxMDIChildFrame
);
166 if ( m_currentChild
&& IsShown() && !ShouldBeVisible() )
168 // we shouldn't remain visible any more
169 wxFrame::Show(false);
170 m_shouldBeShown
= true;
175 void wxMDIParentFrame::RemoveChild(wxWindowBase
*child
)
177 if ( child
== m_currentChild
)
179 // the current child isn't active any more, try to find another one
180 m_currentChild
= NULL
;
182 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
184 node
= node
->GetNext() )
187 childCur
= wxDynamicCast(node
->GetData(), wxMDIChildFrame
);
188 if ( childCur
!= child
)
190 m_currentChild
= childCur
;
196 wxFrame::RemoveChild(child
);
198 // if there are no more children left we need to show the frame if we
199 // hadn't shown it before because there were active children and it was
200 // useless (note that we have to do it after fully removing the child, i.e.
201 // after calling the base class RemoveChild() as otherwise we risk to touch
202 // pointer to the child being deleted)
203 if ( !m_currentChild
&& m_shouldBeShown
&& !IsShown() )
205 // we have to show it, but at least move it out of sight and make it of
206 // smallest possible size (unfortunately (0, 0) doesn't work so that it
207 // doesn't appear in expose
208 SetSize(-10000, -10000, 1, 1);
213 void wxMDIParentFrame::MacActivate(long timestamp
, bool activating
)
215 wxLogTrace(TRACE_MDI
, wxT("MDI PARENT=%p MacActivate(0x%08lx,%s)"), this, timestamp
, activating
? wxT("ACTIV") : wxT("deact"));
219 if (s_macDeactivateWindow
&& s_macDeactivateWindow
->GetParent() == this)
221 wxLogTrace(TRACE_MDI
, wxT("child had been scheduled for deactivation, rehighlighting"));
223 UMAHighlightAndActivateWindow((WindowRef
)s_macDeactivateWindow
->MacGetWindowRef(), true);
225 wxLogTrace(TRACE_MDI
, wxT("finished highliting child"));
227 s_macDeactivateWindow
= NULL
;
229 else if (s_macDeactivateWindow
== this)
231 wxLogTrace(TRACE_MDI
, wxT("Avoided deactivation/activation of this=%p"), this);
233 s_macDeactivateWindow
= NULL
;
235 else // window to deactivate is NULL or is not us or one of our kids
237 // activate kid instead
239 m_currentChild
->MacActivate(timestamp
, activating
);
241 wxFrame::MacActivate(timestamp
, activating
);
246 // We were scheduled for deactivation, and now we do it.
247 if (s_macDeactivateWindow
== this)
249 s_macDeactivateWindow
= NULL
;
251 m_currentChild
->MacActivate(timestamp
, activating
);
252 wxFrame::MacActivate(timestamp
, activating
);
254 else // schedule ourselves for deactivation
256 if (s_macDeactivateWindow
)
257 wxLogTrace(TRACE_MDI
, wxT("window=%p SHOULD have been deactivated, oh well!"), s_macDeactivateWindow
);
258 wxLogTrace(TRACE_MDI
, wxT("Scheduling delayed MDI Parent deactivation"));
260 s_macDeactivateWindow
= this;
265 void wxMDIParentFrame::OnActivate(wxActivateEvent
& event
)
270 // Returns the active MDI child window
271 wxMDIChildFrame
*wxMDIParentFrame::GetActiveChild() const
273 return m_currentChild
;
276 // Create the client window class (don't Create the window,
277 // just return a new class)
278 wxMDIClientWindow
*wxMDIParentFrame::OnCreateClient()
280 return new wxMDIClientWindow( this );
283 // Responds to colour changes, and passes event on to children.
284 void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
288 // Propagate the event to the non-top-level children
289 wxFrame::OnSysColourChanged(event
);
293 void wxMDIParentFrame::Cascade()
298 void wxMDIParentFrame::Tile(wxOrientation
WXUNUSED(orient
))
303 void wxMDIParentFrame::ArrangeIcons()
308 void wxMDIParentFrame::ActivateNext()
313 void wxMDIParentFrame::ActivatePrevious()
318 bool wxMDIParentFrame::ShouldBeVisible() const
320 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
322 node
= node
->GetNext() )
324 wxWindow
*win
= node
->GetData();
327 && !wxDynamicCast(win
, wxMDIChildFrame
)
329 && win
!= (wxWindow
*) GetStatusBar()
331 && win
!= GetClientWindow() )
333 // if we have a non-MDI child, do remain visible so that it could
342 bool wxMDIParentFrame::Show( bool show
)
344 m_shouldBeShown
= false;
346 // don't really show the MDI frame unless it has any children other than
347 // MDI children as it is pretty useless in this case
351 if ( !ShouldBeVisible() && m_currentChild
)
353 // don't make the window visible now but remember that we should
355 m_shouldBeShown
= true;
361 return wxFrame::Show(show
);
364 // ----------------------------------------------------------------------------
366 // ----------------------------------------------------------------------------
368 wxMDIChildFrame::wxMDIChildFrame()
372 void wxMDIChildFrame::Init()
376 bool wxMDIChildFrame::Create(wxMDIParentFrame
*parent
,
378 const wxString
& title
,
382 const wxString
& name
)
386 if ( id
== wxID_ANY
)
387 m_windowId
= (int)NewControlId();
392 parent
->AddChild(this);
394 MacCreateRealWindow( title
, pos
, size
, MacRemoveBordersFromStyle(style
) , name
) ;
396 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
398 wxModelessWindows
.Append(this);
403 wxMDIChildFrame::~wxMDIChildFrame()
408 void wxMDIChildFrame::SetMenuBar(wxMenuBar
*menu_bar
)
410 return wxFrame::SetMenuBar( menu_bar
) ;
413 void wxMDIChildFrame::MacActivate(long timestamp
, bool activating
)
415 wxLogTrace(TRACE_MDI
, wxT("MDI child=%p MacActivate(0x%08lx,%s)"),this, timestamp
, activating
? wxT("ACTIV") : wxT("deact"));
417 wxMDIParentFrame
*mdiparent
= wxDynamicCast(m_parent
, wxMDIParentFrame
);
422 if (s_macDeactivateWindow
== m_parent
)
424 wxLogTrace(TRACE_MDI
, wxT("parent had been scheduled for deactivation, rehighlighting"));
426 UMAHighlightAndActivateWindow((WindowRef
)s_macDeactivateWindow
->MacGetWindowRef(), true);
428 wxLogTrace(TRACE_MDI
, wxT("finished highliting parent"));
430 s_macDeactivateWindow
= NULL
;
432 else if ((mdiparent
->m_currentChild
== this) || !s_macDeactivateWindow
)
433 mdiparent
->wxFrame::MacActivate(timestamp
, activating
);
435 if (mdiparent
->m_currentChild
&& mdiparent
->m_currentChild
!= this)
436 mdiparent
->m_currentChild
->wxFrame::MacActivate(timestamp
, false);
437 mdiparent
->m_currentChild
= this;
439 if (s_macDeactivateWindow
== this)
441 wxLogTrace(TRACE_MDI
, wxT("Avoided deactivation/activation of this=%p"), this);
443 s_macDeactivateWindow
= NULL
;
446 wxFrame::MacActivate(timestamp
, activating
);
450 // We were scheduled for deactivation, and now we do it.
451 if (s_macDeactivateWindow
== this)
453 s_macDeactivateWindow
= NULL
;
454 wxFrame::MacActivate(timestamp
, activating
);
455 if (mdiparent
->m_currentChild
== this)
456 mdiparent
->wxFrame::MacActivate(timestamp
, activating
);
458 else // schedule ourselves for deactivation
460 if (s_macDeactivateWindow
)
461 wxLogTrace(TRACE_MDI
, wxT("window=%p SHOULD have been deactivated, oh well!"), s_macDeactivateWindow
);
462 wxLogTrace(TRACE_MDI
, wxT("Scheduling delayed deactivation"));
464 s_macDeactivateWindow
= this;
470 void wxMDIChildFrame::Maximize()
472 wxFrame::Maximize() ;
475 void wxMDIChildFrame::Restore()
480 void wxMDIChildFrame::Activate()
484 //-----------------------------------------------------------------------------
486 //-----------------------------------------------------------------------------
488 wxMDIClientWindow::wxMDIClientWindow()
492 wxMDIClientWindow::~wxMDIClientWindow()
497 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame
*parent
, long style
)
499 if ( !wxWindow::Create(parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, style
) )
502 wxModelessWindows
.Append(this);
507 // Get size *available for subwindows* i.e. excluding menu bar.
508 void wxMDIClientWindow::DoGetClientSize(int *x
, int *y
) const
510 wxDisplaySize( x
, y
) ;
513 // Explicitly call default scroll behaviour
514 void wxMDIClientWindow::OnScroll(wxScrollEvent
& WXUNUSED(event
))