Fix showing MDI child from wxDocManager::ActivateDocument().
[wxWidgets.git] / src / osx / carbon / mdi.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/mdi.cpp
3 // Purpose: MDI classes
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #if wxUSE_MDI
15
16 #include "wx/mdi.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/log.h"
20 #include "wx/menu.h"
21 #include "wx/settings.h"
22 #include "wx/statusbr.h"
23 #endif
24
25 #include "wx/osx/private.h"
26 #include "wx/osx/uma.h"
27
28 IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame, wxFrame)
29 IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame, wxFrame)
30 IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow, wxWindow)
31
32 BEGIN_EVENT_TABLE(wxMDIParentFrame, wxFrame)
33 EVT_ACTIVATE(wxMDIParentFrame::OnActivate)
34 EVT_SYS_COLOUR_CHANGED(wxMDIParentFrame::OnSysColourChanged)
35 END_EVENT_TABLE()
36
37 #define TRACE_MDI "mdi"
38
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;
44
45 // others
46
47 void UMAHighlightAndActivateWindow( WindowRef inWindowRef , bool inActivate )
48 {
49 #if wxOSX_USE_CARBON // TODO REMOVE
50 if ( inWindowRef )
51 {
52 // bool isHighlighted = IsWindowHighlited( inWindowRef ) ;
53 // if ( inActivate != isHighlighted )
54 #ifndef __LP64__
55 GrafPtr port ;
56 GetPort( &port ) ;
57 SetPortWindowPort( inWindowRef ) ;
58 #endif
59 HiliteWindow( inWindowRef , inActivate ) ;
60 ControlRef control = NULL ;
61 ::GetRootControl( inWindowRef , &control ) ;
62 if ( control )
63 {
64 if ( inActivate )
65 ::ActivateControl( control ) ;
66 else
67 ::DeactivateControl( control ) ;
68 }
69 #ifndef __LP64__
70 SetPort( port ) ;
71 #endif
72 }
73 #elif defined(wxOSX_USE_COCOA)
74 wxUnusedVar(inActivate);
75 wxUnusedVar(inWindowRef);
76 // TODO: implement me!
77 #endif
78 }
79
80 // ----------------------------------------------------------------------------
81 // Parent frame
82 // ----------------------------------------------------------------------------
83
84 void wxMDIParentFrame::Init()
85 {
86 m_parentFrameActive = true;
87 m_shouldBeShown = false;
88 }
89
90 bool wxMDIParentFrame::Create(wxWindow *parent,
91 wxWindowID winid,
92 const wxString& title,
93 const wxPoint& pos,
94 const wxSize& size,
95 long style,
96 const wxString& name)
97 {
98 // this style can be used to prevent a window from having the standard MDI
99 // "Window" menu
100 if ( style & wxFRAME_NO_WINDOW_MENU )
101 {
102 m_windowMenu = NULL;
103 style -= wxFRAME_NO_WINDOW_MENU ;
104 }
105 else // normal case: we have the window menu, so construct it
106 {
107 m_windowMenu = new wxMenu;
108
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"));
115 }
116
117 if ( !wxFrame::Create( parent , winid , title , pos , size , style , name ) )
118 return false;
119
120 m_parentFrameActive = true;
121
122 m_clientWindow = OnCreateClient();
123 if ( !m_clientWindow || !m_clientWindow->CreateClient(this, style) )
124 return false;
125
126 return true;
127 }
128
129 wxMDIParentFrame::~wxMDIParentFrame()
130 {
131 DestroyChildren();
132
133 // already deleted by DestroyChildren()
134 m_clientWindow = NULL ;
135 }
136
137 void wxMDIParentFrame::SetMenuBar(wxMenuBar *menu_bar)
138 {
139 wxFrame::SetMenuBar( menu_bar ) ;
140 }
141
142 void wxMDIParentFrame::GetRectForTopLevelChildren(int *x, int *y, int *w, int *h)
143 {
144 if (x)
145 *x = 0;
146 if (y)
147 *y = 0;
148
149 wxDisplaySize(w, h);
150 }
151
152 void wxMDIParentFrame::AddChild(wxWindowBase *child)
153 {
154 // moved this to front, so that we don't run into unset m_parent problems later
155 wxFrame::AddChild(child);
156
157 if ( !m_currentChild )
158 {
159 m_currentChild = wxDynamicCast(child, wxMDIChildFrame);
160
161 if ( m_currentChild && IsShown() && !ShouldBeVisible() )
162 {
163 // we shouldn't remain visible any more
164 wxFrame::Show(false);
165 m_shouldBeShown = true;
166 }
167 }
168 }
169
170 void wxMDIParentFrame::RemoveChild(wxWindowBase *child)
171 {
172 if ( child == m_currentChild )
173 {
174 // the current child isn't active any more, try to find another one
175 m_currentChild = NULL;
176
177 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
178 node;
179 node = node->GetNext() )
180 {
181 wxMDIChildFrame *
182 childCur = wxDynamicCast(node->GetData(), wxMDIChildFrame);
183 if ( childCur != child )
184 {
185 m_currentChild = childCur;
186 break;
187 }
188 }
189 }
190
191 wxFrame::RemoveChild(child);
192
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() )
199 {
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);
204 Show();
205 }
206 }
207
208 void wxMDIParentFrame::MacActivate(long timestamp, bool activating)
209 {
210 wxLogTrace(TRACE_MDI, wxT("MDI PARENT=%p MacActivate(0x%08lx,%s)"), this, timestamp, activating ? wxT("ACTIV") : wxT("deact"));
211
212 if (activating)
213 {
214 if (s_macDeactivateWindow && s_macDeactivateWindow->GetParent() == this)
215 {
216 wxLogTrace(TRACE_MDI, wxT("child had been scheduled for deactivation, rehighlighting"));
217
218 UMAHighlightAndActivateWindow((WindowRef)s_macDeactivateWindow->GetWXWindow(), true);
219
220 wxLogTrace(TRACE_MDI, wxT("finished highliting child"));
221
222 s_macDeactivateWindow = NULL;
223 }
224 else if (s_macDeactivateWindow == this)
225 {
226 wxLogTrace(TRACE_MDI, wxT("Avoided deactivation/activation of this=%p"), this);
227
228 s_macDeactivateWindow = NULL;
229 }
230 else // window to deactivate is NULL or is not us or one of our kids
231 {
232 // activate kid instead
233 if (m_currentChild)
234 m_currentChild->MacActivate(timestamp, activating);
235 else
236 wxFrame::MacActivate(timestamp, activating);
237 }
238 }
239 else
240 {
241 // We were scheduled for deactivation, and now we do it.
242 if (s_macDeactivateWindow == this)
243 {
244 s_macDeactivateWindow = NULL;
245 if (m_currentChild)
246 m_currentChild->MacActivate(timestamp, activating);
247 wxFrame::MacActivate(timestamp, activating);
248 }
249 else // schedule ourselves for deactivation
250 {
251 if (s_macDeactivateWindow)
252 {
253 wxLogTrace(TRACE_MDI, wxT("window=%p SHOULD have been deactivated, oh well!"), s_macDeactivateWindow);
254 }
255 wxLogTrace(TRACE_MDI, wxT("Scheduling delayed MDI Parent deactivation"));
256
257 s_macDeactivateWindow = this;
258 }
259 }
260 }
261
262 void wxMDIParentFrame::OnActivate(wxActivateEvent& event)
263 {
264 event.Skip();
265 }
266
267 // Responds to colour changes, and passes event on to children.
268 void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent& event)
269 {
270 // TODO
271
272 // Propagate the event to the non-top-level children
273 wxFrame::OnSysColourChanged(event);
274 }
275
276 bool wxMDIParentFrame::ShouldBeVisible() const
277 {
278 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
279 node;
280 node = node->GetNext() )
281 {
282 wxWindow *win = node->GetData();
283
284 if ( win->IsShown()
285 && !wxDynamicCast(win, wxMDIChildFrame)
286 #if wxUSE_STATUSBAR
287 && win != (wxWindow*) GetStatusBar()
288 #endif
289 && win != GetClientWindow() )
290 {
291 // if we have a non-MDI child, do remain visible so that it could
292 // be used
293 return true;
294 }
295 }
296
297 return false;
298 }
299
300 bool wxMDIParentFrame::Show( bool show )
301 {
302 m_shouldBeShown = false;
303
304 // don't really show the MDI frame unless it has any children other than
305 // MDI children as it is pretty useless in this case
306
307 if ( show )
308 {
309 if ( !ShouldBeVisible() && m_currentChild )
310 {
311 // don't make the window visible now but remember that we should
312 // have had done it
313 m_shouldBeShown = true;
314
315 return false;
316 }
317 }
318
319 return wxFrame::Show(show);
320 }
321
322 // ----------------------------------------------------------------------------
323 // Child frame
324 // ----------------------------------------------------------------------------
325
326 void wxMDIChildFrame::Init()
327 {
328 }
329
330 bool wxMDIChildFrame::Create(wxMDIParentFrame *parent,
331 wxWindowID winid,
332 const wxString& title,
333 const wxPoint& pos,
334 const wxSize& size,
335 long style,
336 const wxString& name)
337 {
338 m_mdiParent = parent;
339
340 SetName(name);
341
342 if ( winid == wxID_ANY )
343 winid = (int)NewControlId();
344
345 wxNonOwnedWindow::Create( parent, winid, pos , size , MacRemoveBordersFromStyle(style) , name ) ;
346
347 SetTitle( title );
348
349 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE));
350
351 return true;
352 }
353
354 wxMDIChildFrame::~wxMDIChildFrame()
355 {
356 DestroyChildren();
357 }
358
359 void wxMDIChildFrame::MacActivate(long timestamp, bool activating)
360 {
361 wxLogTrace(TRACE_MDI, wxT("MDI child=%p MacActivate(0x%08lx,%s)"),this, timestamp, activating ? wxT("ACTIV") : wxT("deact"));
362
363 wxMDIParentFrame *mdiparent = m_mdiParent;
364 wxASSERT(mdiparent);
365
366 if (activating)
367 {
368 if (s_macDeactivateWindow == m_parent)
369 {
370 wxLogTrace(TRACE_MDI, wxT("parent had been scheduled for deactivation, rehighlighting"));
371
372 UMAHighlightAndActivateWindow((WindowRef)s_macDeactivateWindow->GetWXWindow(), true);
373
374 wxLogTrace(TRACE_MDI, wxT("finished highliting parent"));
375
376 s_macDeactivateWindow = NULL;
377 }
378 else if ((mdiparent->m_currentChild == this) || !s_macDeactivateWindow)
379 mdiparent->wxFrame::MacActivate(timestamp, activating);
380
381 if (mdiparent->m_currentChild && mdiparent->m_currentChild != this)
382 mdiparent->m_currentChild->wxFrame::MacActivate(timestamp, false);
383 mdiparent->m_currentChild = this;
384
385 if (s_macDeactivateWindow == this)
386 {
387 wxLogTrace(TRACE_MDI, wxT("Avoided deactivation/activation of this=%p"), this);
388
389 s_macDeactivateWindow = NULL;
390 }
391 else
392 wxFrame::MacActivate(timestamp, activating);
393 }
394 else
395 {
396 // We were scheduled for deactivation, and now we do it.
397 if (s_macDeactivateWindow == this)
398 {
399 s_macDeactivateWindow = NULL;
400 wxFrame::MacActivate(timestamp, activating);
401 if (mdiparent->m_currentChild == this)
402 mdiparent->wxFrame::MacActivate(timestamp, activating);
403 }
404 else // schedule ourselves for deactivation
405 {
406 if (s_macDeactivateWindow)
407 {
408 wxLogTrace(TRACE_MDI, wxT("window=%p SHOULD have been deactivated, oh well!"), s_macDeactivateWindow);
409 }
410 wxLogTrace(TRACE_MDI, wxT("Scheduling delayed deactivation"));
411
412 s_macDeactivateWindow = this;
413 }
414 }
415 }
416
417 // MDI operations
418 void wxMDIChildFrame::Activate()
419 {
420 // The base class method calls Activate() so skip it to avoid infinite
421 // recursion and go directly to the real Raise() implementation.
422 wxFrame::Raise();
423 }
424
425 //-----------------------------------------------------------------------------
426 // wxMDIClientWindow
427 //-----------------------------------------------------------------------------
428
429 wxMDIClientWindow::~wxMDIClientWindow()
430 {
431 DestroyChildren();
432 }
433
434 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame *parent, long style)
435 {
436 if ( !wxWindow::Create(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, style) )
437 return false;
438
439 return true;
440 }
441
442 void wxMDIClientWindow::DoGetClientSize(int *x, int *y) const
443 {
444 wxDisplaySize( x , y ) ;
445 }
446
447 #endif // wxUSE_MDI