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