]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/mdi.cpp
use WX_DEFINE_ARRAY_INT for an array of ints (bug 1536482)
[wxWidgets.git] / src / mac / carbon / mdi.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/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/mac/private.h"
26 #include "wx/mac/uma.h"
27
28 extern wxWindowList wxModelessWindows;
29
30 IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame, wxFrame)
31 IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame, wxFrame)
32 IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow, wxWindow)
33
34 BEGIN_EVENT_TABLE(wxMDIParentFrame, wxFrame)
35 EVT_ACTIVATE(wxMDIParentFrame::OnActivate)
36 EVT_SYS_COLOUR_CHANGED(wxMDIParentFrame::OnSysColourChanged)
37 END_EVENT_TABLE()
38
39 BEGIN_EVENT_TABLE(wxMDIClientWindow, wxWindow)
40 EVT_SCROLL(wxMDIClientWindow::OnScroll)
41 END_EVENT_TABLE()
42
43 static const wxChar *TRACE_MDI = _T("mdi");
44
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;
50
51 // ----------------------------------------------------------------------------
52 // Parent frame
53 // ----------------------------------------------------------------------------
54
55 void wxMDIParentFrame::Init()
56 {
57 m_clientWindow = NULL;
58 m_currentChild = NULL;
59 m_windowMenu = (wxMenu*) NULL;
60 m_parentFrameActive = true;
61 m_shouldBeShown = false;
62 }
63
64 bool wxMDIParentFrame::Create(wxWindow *parent,
65 wxWindowID id,
66 const wxString& title,
67 const wxPoint& pos,
68 const wxSize& size,
69 long style,
70 const wxString& name)
71 {
72 // this style can be used to prevent a window from having the standard MDI
73 // "Window" menu
74 if ( style & wxFRAME_NO_WINDOW_MENU )
75 {
76 m_windowMenu = (wxMenu *)NULL;
77 style -= wxFRAME_NO_WINDOW_MENU ;
78 }
79 else // normal case: we have the window menu, so construct it
80 {
81 m_windowMenu = new wxMenu;
82
83 m_windowMenu->Append(IDM_WINDOWCASCADE, wxT("&Cascade"));
84 m_windowMenu->Append(IDM_WINDOWTILEHOR, wxT("Tile &Horizontally"));
85 m_windowMenu->Append(IDM_WINDOWTILEVERT, wxT("Tile &Vertically"));
86 m_windowMenu->AppendSeparator();
87 m_windowMenu->Append(IDM_WINDOWICONS, wxT("&Arrange Icons"));
88 m_windowMenu->Append(IDM_WINDOWNEXT, wxT("&Next"));
89 }
90
91 wxFrame::Create( parent , id , title , pos , size , style , name ) ;
92 m_parentFrameActive = true;
93
94 OnCreateClient();
95
96 return true;
97 }
98
99 wxMDIParentFrame::~wxMDIParentFrame()
100 {
101 DestroyChildren();
102
103 // already deleted by DestroyChildren()
104 m_clientWindow = NULL ;
105
106 delete m_windowMenu;
107 }
108
109 void wxMDIParentFrame::SetMenuBar(wxMenuBar *menu_bar)
110 {
111 wxFrame::SetMenuBar( menu_bar ) ;
112 }
113
114 void wxMDIParentFrame::GetRectForTopLevelChildren(int *x, int *y, int *w, int *h)
115 {
116 if (x)
117 *x = 0;
118 if (y)
119 *y = 0;
120
121 wxDisplaySize(w, h);
122 }
123
124 void wxMDIParentFrame::AddChild(wxWindowBase *child)
125 {
126 if ( !m_currentChild )
127 {
128 m_currentChild = wxDynamicCast(child, wxMDIChildFrame);
129
130 if ( m_currentChild && IsShown() && !ShouldBeVisible() )
131 {
132 // we shouldn't remain visible any more
133 wxFrame::Show(false);
134 m_shouldBeShown = true;
135 }
136 }
137
138 wxFrame::AddChild(child);
139 }
140
141 void wxMDIParentFrame::RemoveChild(wxWindowBase *child)
142 {
143 if ( child == m_currentChild )
144 {
145 // the current child isn't active any more, try to find another one
146 m_currentChild = NULL;
147
148 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
149 node;
150 node = node->GetNext() )
151 {
152 wxMDIChildFrame *
153 childCur = wxDynamicCast(node->GetData(), wxMDIChildFrame);
154 if ( childCur != child )
155 {
156 m_currentChild = childCur;
157 break;
158 }
159 }
160 }
161
162 wxFrame::RemoveChild(child);
163
164 // if there are no more children left we need to show the frame if we
165 // hadn't shown it before because there were active children and it was
166 // useless (note that we have to do it after fully removing the child, i.e.
167 // after calling the base class RemoveChild() as otherwise we risk to touch
168 // pointer to the child being deleted)
169 if ( !m_currentChild && m_shouldBeShown && !IsShown() )
170 {
171 // we have to show it, but at least move it out of sight and make it of
172 // smallest possible size (unfortunately (0, 0) doesn't work so that it
173 // doesn't appear in expose
174 SetSize(-10000, -10000, 1, 1);
175 Show();
176 }
177 }
178
179 void wxMDIParentFrame::MacActivate(long timestamp, bool activating)
180 {
181 wxLogTrace(TRACE_MDI, wxT("MDI PARENT=%p MacActivate(0x%08lx,%s)"), this, timestamp, activating ? wxT("ACTIV") : wxT("deact"));
182
183 if (activating)
184 {
185 if (s_macDeactivateWindow && s_macDeactivateWindow->GetParent() == this)
186 {
187 wxLogTrace(TRACE_MDI, wxT("child had been scheduled for deactivation, rehighlighting"));
188
189 UMAHighlightAndActivateWindow((WindowRef)s_macDeactivateWindow->MacGetWindowRef(), true);
190
191 wxLogTrace(TRACE_MDI, wxT("finished highliting child"));
192
193 s_macDeactivateWindow = NULL;
194 }
195 else if (s_macDeactivateWindow == this)
196 {
197 wxLogTrace(TRACE_MDI, wxT("Avoided deactivation/activation of this=%p"), this);
198
199 s_macDeactivateWindow = NULL;
200 }
201 else // window to deactivate is NULL or is not us or one of our kids
202 {
203 // activate kid instead
204 if (m_currentChild)
205 m_currentChild->MacActivate(timestamp, activating);
206 else
207 wxFrame::MacActivate(timestamp, activating);
208 }
209 }
210 else
211 {
212 // We were scheduled for deactivation, and now we do it.
213 if (s_macDeactivateWindow == this)
214 {
215 s_macDeactivateWindow = NULL;
216 if (m_currentChild)
217 m_currentChild->MacActivate(timestamp, activating);
218 wxFrame::MacActivate(timestamp, activating);
219 }
220 else // schedule ourselves for deactivation
221 {
222 if (s_macDeactivateWindow)
223 wxLogTrace(TRACE_MDI, wxT("window=%p SHOULD have been deactivated, oh well!"), s_macDeactivateWindow);
224 wxLogTrace(TRACE_MDI, wxT("Scheduling delayed MDI Parent deactivation"));
225
226 s_macDeactivateWindow = this;
227 }
228 }
229 }
230
231 void wxMDIParentFrame::OnActivate(wxActivateEvent& event)
232 {
233 event.Skip();
234 }
235
236 // Returns the active MDI child window
237 wxMDIChildFrame *wxMDIParentFrame::GetActiveChild() const
238 {
239 return m_currentChild ;
240 }
241
242 // Create the client window class (don't Create the window,
243 // just return a new class)
244 wxMDIClientWindow *wxMDIParentFrame::OnCreateClient()
245 {
246 m_clientWindow = new wxMDIClientWindow( this );
247
248 return m_clientWindow;
249 }
250
251 // Responds to colour changes, and passes event on to children.
252 void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent& event)
253 {
254 // TODO
255
256 // Propagate the event to the non-top-level children
257 wxFrame::OnSysColourChanged(event);
258 }
259
260 // MDI operations
261 void wxMDIParentFrame::Cascade()
262 {
263 // TODO
264 }
265
266 void wxMDIParentFrame::Tile(wxOrientation WXUNUSED(orient))
267 {
268 // TODO
269 }
270
271 void wxMDIParentFrame::ArrangeIcons()
272 {
273 // TODO
274 }
275
276 void wxMDIParentFrame::ActivateNext()
277 {
278 // TODO
279 }
280
281 void wxMDIParentFrame::ActivatePrevious()
282 {
283 // TODO
284 }
285
286 bool wxMDIParentFrame::ShouldBeVisible() const
287 {
288 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
289 node;
290 node = node->GetNext() )
291 {
292 wxWindow *win = node->GetData();
293
294 if ( win->IsShown()
295 && !wxDynamicCast(win, wxMDIChildFrame)
296 #if wxUSE_STATUSBAR
297 && win != (wxWindow*) GetStatusBar()
298 #endif
299 && win != GetClientWindow() )
300 {
301 // if we have a non-MDI child, do remain visible so that it could
302 // be used
303 return true;
304 }
305 }
306
307 return false;
308 }
309
310 bool wxMDIParentFrame::Show( bool show )
311 {
312 m_shouldBeShown = false;
313
314 // don't really show the MDI frame unless it has any children other than
315 // MDI children as it is pretty useless in this case
316
317 if ( show )
318 {
319 if ( !ShouldBeVisible() && m_currentChild )
320 {
321 // don't make the window visible now but remember that we should
322 // have had done it
323 m_shouldBeShown = true;
324
325 return false;
326 }
327 }
328
329 return wxFrame::Show(show);
330 }
331
332 // ----------------------------------------------------------------------------
333 // Child frame
334 // ----------------------------------------------------------------------------
335
336 wxMDIChildFrame::wxMDIChildFrame()
337 {
338 Init() ;
339 }
340 void wxMDIChildFrame::Init()
341 {
342 }
343
344 bool wxMDIChildFrame::Create(wxMDIParentFrame *parent,
345 wxWindowID id,
346 const wxString& title,
347 const wxPoint& pos,
348 const wxSize& size,
349 long style,
350 const wxString& name)
351 {
352 SetName(name);
353
354 if ( id == wxID_ANY )
355 m_windowId = (int)NewControlId();
356 else
357 m_windowId = id;
358
359 if (parent)
360 parent->AddChild(this);
361
362 MacCreateRealWindow( title, pos , size , MacRemoveBordersFromStyle(style) , name ) ;
363
364 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE));
365
366 wxModelessWindows.Append(this);
367
368 return true;
369 }
370
371 wxMDIChildFrame::~wxMDIChildFrame()
372 {
373 DestroyChildren();
374 }
375
376 void wxMDIChildFrame::SetMenuBar(wxMenuBar *menu_bar)
377 {
378 return wxFrame::SetMenuBar( menu_bar ) ;
379 }
380
381 void wxMDIChildFrame::MacActivate(long timestamp, bool activating)
382 {
383 wxLogTrace(TRACE_MDI, wxT("MDI child=%p MacActivate(0x%08lx,%s)"),this, timestamp, activating ? wxT("ACTIV") : wxT("deact"));
384
385 wxMDIParentFrame *mdiparent = wxDynamicCast(m_parent, wxMDIParentFrame);
386 wxASSERT(mdiparent);
387
388 if (activating)
389 {
390 if (s_macDeactivateWindow == m_parent)
391 {
392 wxLogTrace(TRACE_MDI, wxT("parent had been scheduled for deactivation, rehighlighting"));
393
394 UMAHighlightAndActivateWindow((WindowRef)s_macDeactivateWindow->MacGetWindowRef(), true);
395
396 wxLogTrace(TRACE_MDI, wxT("finished highliting parent"));
397
398 s_macDeactivateWindow = NULL;
399 }
400 else if ((mdiparent->m_currentChild == this) || !s_macDeactivateWindow)
401 mdiparent->wxFrame::MacActivate(timestamp, activating);
402
403 if (mdiparent->m_currentChild && mdiparent->m_currentChild != this)
404 mdiparent->m_currentChild->wxFrame::MacActivate(timestamp, false);
405 mdiparent->m_currentChild = this;
406
407 if (s_macDeactivateWindow == this)
408 {
409 wxLogTrace(TRACE_MDI, wxT("Avoided deactivation/activation of this=%p"), this);
410
411 s_macDeactivateWindow = NULL;
412 }
413 else
414 wxFrame::MacActivate(timestamp, activating);
415 }
416 else
417 {
418 // We were scheduled for deactivation, and now we do it.
419 if (s_macDeactivateWindow == this)
420 {
421 s_macDeactivateWindow = NULL;
422 wxFrame::MacActivate(timestamp, activating);
423 if (mdiparent->m_currentChild == this)
424 mdiparent->wxFrame::MacActivate(timestamp, activating);
425 }
426 else // schedule ourselves for deactivation
427 {
428 if (s_macDeactivateWindow)
429 wxLogTrace(TRACE_MDI, wxT("window=%p SHOULD have been deactivated, oh well!"), s_macDeactivateWindow);
430 wxLogTrace(TRACE_MDI, wxT("Scheduling delayed deactivation"));
431
432 s_macDeactivateWindow = this;
433 }
434 }
435 }
436
437 // MDI operations
438 void wxMDIChildFrame::Maximize()
439 {
440 wxFrame::Maximize() ;
441 }
442
443 void wxMDIChildFrame::Restore()
444 {
445 wxFrame::Restore() ;
446 }
447
448 void wxMDIChildFrame::Activate()
449 {
450 }
451
452 //-----------------------------------------------------------------------------
453 // wxMDIClientWindow
454 //-----------------------------------------------------------------------------
455
456 wxMDIClientWindow::wxMDIClientWindow()
457 {
458 }
459
460 wxMDIClientWindow::~wxMDIClientWindow()
461 {
462 DestroyChildren();
463 }
464
465 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame *parent, long style)
466 {
467 if ( !wxWindow::Create(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, style) )
468 return false;
469
470 wxModelessWindows.Append(this);
471
472 return true;
473 }
474
475 // Get size *available for subwindows* i.e. excluding menu bar.
476 void wxMDIClientWindow::DoGetClientSize(int *x, int *y) const
477 {
478 wxDisplaySize( x , y ) ;
479 }
480
481 // Explicitly call default scroll behaviour
482 void wxMDIClientWindow::OnScroll(wxScrollEvent& event)
483 {
484 }
485
486 #endif // wxUSE_MDI