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_WINDOWTILE
= 4001;
46 static const int IDM_WINDOWTILEHOR
= 4001;
47 static const int IDM_WINDOWCASCADE
= 4002;
48 static const int IDM_WINDOWICONS
= 4003;
49 static const int IDM_WINDOWNEXT
= 4004;
50 static const int IDM_WINDOWTILEVERT
= 4005;
51 static const int IDM_WINDOWPREV
= 4006;
53 // This range gives a maximum of 500 MDI children. Should be enough :-)
54 static const int wxFIRST_MDI_CHILD
= 4100;
55 static const int wxLAST_MDI_CHILD
= 4600;
57 // Status border dimensions
58 static const int wxTHICK_LINE_BORDER
= 3;
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 void wxMDIParentFrame::Init()
66 m_clientWindow
= NULL
;
67 m_currentChild
= NULL
;
68 m_windowMenu
= (wxMenu
*) NULL
;
69 m_parentFrameActive
= true;
70 m_shouldBeShown
= false;
73 bool wxMDIParentFrame::Create(wxWindow
*parent
,
75 const wxString
& title
,
81 // this style can be used to prevent a window from having the standard MDI
83 if ( style
& wxFRAME_NO_WINDOW_MENU
)
85 m_windowMenu
= (wxMenu
*)NULL
;
86 style
-= wxFRAME_NO_WINDOW_MENU
;
88 else // normal case: we have the window menu, so construct it
90 m_windowMenu
= new wxMenu
;
92 m_windowMenu
->Append(IDM_WINDOWCASCADE
, wxT("&Cascade"));
93 m_windowMenu
->Append(IDM_WINDOWTILEHOR
, wxT("Tile &Horizontally"));
94 m_windowMenu
->Append(IDM_WINDOWTILEVERT
, wxT("Tile &Vertically"));
95 m_windowMenu
->AppendSeparator();
96 m_windowMenu
->Append(IDM_WINDOWICONS
, wxT("&Arrange Icons"));
97 m_windowMenu
->Append(IDM_WINDOWNEXT
, wxT("&Next"));
100 wxFrame::Create( parent
, id
, title
, pos
, size
, style
, name
) ;
101 m_parentFrameActive
= true;
108 wxMDIParentFrame::~wxMDIParentFrame()
112 // already deleted by DestroyChildren()
113 m_clientWindow
= NULL
;
118 void wxMDIParentFrame::SetMenuBar(wxMenuBar
*menu_bar
)
120 wxFrame::SetMenuBar( menu_bar
) ;
123 void wxMDIParentFrame::GetRectForTopLevelChildren(int *x
, int *y
, int *w
, int *h
)
133 void wxMDIParentFrame::AddChild(wxWindowBase
*child
)
135 if ( !m_currentChild
)
137 m_currentChild
= wxDynamicCast(child
, wxMDIChildFrame
);
139 if ( m_currentChild
&& IsShown() && !ShouldBeVisible() )
141 // we shouldn't remain visible any more
142 wxFrame::Show(false);
143 m_shouldBeShown
= true;
147 wxFrame::AddChild(child
);
150 void wxMDIParentFrame::RemoveChild(wxWindowBase
*child
)
152 if ( child
== m_currentChild
)
154 // the current child isn't active any more, try to find another one
155 m_currentChild
= NULL
;
157 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
159 node
= node
->GetNext() )
162 childCur
= wxDynamicCast(node
->GetData(), wxMDIChildFrame
);
163 if ( childCur
!= child
)
165 m_currentChild
= childCur
;
171 wxFrame::RemoveChild(child
);
173 // if there are no more children left we need to show the frame if we
174 // hadn't shown it before because there were active children and it was
175 // useless (note that we have to do it after fully removing the child, i.e.
176 // after calling the base class RemoveChild() as otherwise we risk to touch
177 // pointer to the child being deleted)
178 if ( !m_currentChild
&& m_shouldBeShown
&& !IsShown() )
180 // we have to show it, but at least move it out of sight and make it of
181 // smallest possible size (unfortunately (0, 0) doesn't work so that it
182 // doesn't appear in expose
183 SetSize(-10000, -10000, 1, 1);
188 void wxMDIParentFrame::MacActivate(long timestamp
, bool activating
)
190 wxLogTrace(TRACE_MDI
, wxT("MDI PARENT=%p MacActivate(0x%08lx,%s)"), this, timestamp
, activating
? wxT("ACTIV") : wxT("deact"));
194 if (s_macDeactivateWindow
&& s_macDeactivateWindow
->GetParent() == this)
196 wxLogTrace(TRACE_MDI
, wxT("child had been scheduled for deactivation, rehighlighting"));
198 UMAHighlightAndActivateWindow((WindowRef
)s_macDeactivateWindow
->MacGetWindowRef(), true);
200 wxLogTrace(TRACE_MDI
, wxT("finished highliting child"));
202 s_macDeactivateWindow
= NULL
;
204 else if (s_macDeactivateWindow
== this)
206 wxLogTrace(TRACE_MDI
, wxT("Avoided deactivation/activation of this=%p"), this);
208 s_macDeactivateWindow
= NULL
;
210 else // window to deactivate is NULL or is not us or one of our kids
212 // activate kid instead
214 m_currentChild
->MacActivate(timestamp
, activating
);
216 wxFrame::MacActivate(timestamp
, activating
);
221 // We were scheduled for deactivation, and now we do it.
222 if (s_macDeactivateWindow
== this)
224 s_macDeactivateWindow
= NULL
;
226 m_currentChild
->MacActivate(timestamp
, activating
);
227 wxFrame::MacActivate(timestamp
, activating
);
229 else // schedule ourselves for deactivation
231 if (s_macDeactivateWindow
)
232 wxLogTrace(TRACE_MDI
, wxT("window=%p SHOULD have been deactivated, oh well!"), s_macDeactivateWindow
);
233 wxLogTrace(TRACE_MDI
, wxT("Scheduling delayed MDI Parent deactivation"));
235 s_macDeactivateWindow
= this;
240 void wxMDIParentFrame::OnActivate(wxActivateEvent
& event
)
245 // Returns the active MDI child window
246 wxMDIChildFrame
*wxMDIParentFrame::GetActiveChild() const
248 return m_currentChild
;
251 // Create the client window class (don't Create the window,
252 // just return a new class)
253 wxMDIClientWindow
*wxMDIParentFrame::OnCreateClient()
255 m_clientWindow
= new wxMDIClientWindow( this );
257 return m_clientWindow
;
260 // Responds to colour changes, and passes event on to children.
261 void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
265 // Propagate the event to the non-top-level children
266 wxFrame::OnSysColourChanged(event
);
270 void wxMDIParentFrame::Cascade()
275 void wxMDIParentFrame::Tile(wxOrientation
WXUNUSED(orient
))
280 void wxMDIParentFrame::ArrangeIcons()
285 void wxMDIParentFrame::ActivateNext()
290 void wxMDIParentFrame::ActivatePrevious()
295 bool wxMDIParentFrame::ShouldBeVisible() const
297 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
299 node
= node
->GetNext() )
301 wxWindow
*win
= node
->GetData();
304 && !wxDynamicCast(win
, wxMDIChildFrame
)
306 && win
!= (wxWindow
*) GetStatusBar()
308 && win
!= GetClientWindow() )
310 // if we have a non-MDI child, do remain visible so that it could
319 bool wxMDIParentFrame::Show( bool show
)
321 m_shouldBeShown
= false;
323 // don't really show the MDI frame unless it has any children other than
324 // MDI children as it is pretty useless in this case
328 if ( !ShouldBeVisible() && m_currentChild
)
330 // don't make the window visible now but remember that we should
332 m_shouldBeShown
= true;
338 return wxFrame::Show(show
);
341 // ----------------------------------------------------------------------------
343 // ----------------------------------------------------------------------------
345 wxMDIChildFrame::wxMDIChildFrame()
349 void wxMDIChildFrame::Init()
353 bool wxMDIChildFrame::Create(wxMDIParentFrame
*parent
,
355 const wxString
& title
,
359 const wxString
& name
)
363 if ( id
== wxID_ANY
)
364 m_windowId
= (int)NewControlId();
369 parent
->AddChild(this);
371 MacCreateRealWindow( title
, pos
, size
, MacRemoveBordersFromStyle(style
) , name
) ;
373 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
375 wxModelessWindows
.Append(this);
380 wxMDIChildFrame::~wxMDIChildFrame()
385 void wxMDIChildFrame::SetMenuBar(wxMenuBar
*menu_bar
)
387 return wxFrame::SetMenuBar( menu_bar
) ;
390 void wxMDIChildFrame::MacActivate(long timestamp
, bool activating
)
392 wxLogTrace(TRACE_MDI
, wxT("MDI child=%p MacActivate(0x%08lx,%s)"),this, timestamp
, activating
? wxT("ACTIV") : wxT("deact"));
394 wxMDIParentFrame
*mdiparent
= wxDynamicCast(m_parent
, wxMDIParentFrame
);
399 if (s_macDeactivateWindow
== m_parent
)
401 wxLogTrace(TRACE_MDI
, wxT("parent had been scheduled for deactivation, rehighlighting"));
403 UMAHighlightAndActivateWindow((WindowRef
)s_macDeactivateWindow
->MacGetWindowRef(), true);
405 wxLogTrace(TRACE_MDI
, wxT("finished highliting parent"));
407 s_macDeactivateWindow
= NULL
;
409 else if ((mdiparent
->m_currentChild
== this) || !s_macDeactivateWindow
)
410 mdiparent
->wxFrame::MacActivate(timestamp
, activating
);
412 if (mdiparent
->m_currentChild
&& mdiparent
->m_currentChild
!= this)
413 mdiparent
->m_currentChild
->wxFrame::MacActivate(timestamp
, false);
414 mdiparent
->m_currentChild
= this;
416 if (s_macDeactivateWindow
== this)
418 wxLogTrace(TRACE_MDI
, wxT("Avoided deactivation/activation of this=%p"), this);
420 s_macDeactivateWindow
= NULL
;
423 wxFrame::MacActivate(timestamp
, activating
);
427 // We were scheduled for deactivation, and now we do it.
428 if (s_macDeactivateWindow
== this)
430 s_macDeactivateWindow
= NULL
;
431 wxFrame::MacActivate(timestamp
, activating
);
432 if (mdiparent
->m_currentChild
== this)
433 mdiparent
->wxFrame::MacActivate(timestamp
, activating
);
435 else // schedule ourselves for deactivation
437 if (s_macDeactivateWindow
)
438 wxLogTrace(TRACE_MDI
, wxT("window=%p SHOULD have been deactivated, oh well!"), s_macDeactivateWindow
);
439 wxLogTrace(TRACE_MDI
, wxT("Scheduling delayed deactivation"));
441 s_macDeactivateWindow
= this;
447 void wxMDIChildFrame::Maximize()
449 wxFrame::Maximize() ;
452 void wxMDIChildFrame::Restore()
457 void wxMDIChildFrame::Activate()
461 //-----------------------------------------------------------------------------
463 //-----------------------------------------------------------------------------
465 wxMDIClientWindow::wxMDIClientWindow()
469 wxMDIClientWindow::~wxMDIClientWindow()
474 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame
*parent
, long style
)
476 if ( !wxWindow::Create(parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, style
) )
479 wxModelessWindows
.Append(this);
484 // Get size *available for subwindows* i.e. excluding menu bar.
485 void wxMDIClientWindow::DoGetClientSize(int *x
, int *y
) const
487 wxDisplaySize( x
, y
) ;
490 // Explicitly call default scroll behaviour
491 void wxMDIClientWindow::OnScroll(wxScrollEvent
& event
)