]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/mdi.cpp
c7d898778bc41aa4f1dffa9427b6118944284761
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "mdi.h"
14 #endif
15
16 #include "wx/wxprec.h"
17
18 #if wxUSE_MDI
19
20 #include "wx/mdi.h"
21 #include "wx/menu.h"
22 #include "wx/settings.h"
23 #include "wx/log.h"
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_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;
52
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;
56
57 // Status border dimensions
58 static const int wxTHICK_LINE_BORDER = 3;
59
60 // ----------------------------------------------------------------------------
61 // Parent frame
62 // ----------------------------------------------------------------------------
63
64 void wxMDIParentFrame::Init()
65 {
66 m_clientWindow = NULL;
67 m_currentChild = NULL;
68 m_windowMenu = (wxMenu*) NULL;
69 m_parentFrameActive = true;
70 m_shouldBeShown = false;
71 }
72
73 bool wxMDIParentFrame::Create(wxWindow *parent,
74 wxWindowID id,
75 const wxString& title,
76 const wxPoint& pos,
77 const wxSize& size,
78 long style,
79 const wxString& name)
80 {
81 // this style can be used to prevent a window from having the standard MDI
82 // "Window" menu
83 if ( style & wxFRAME_NO_WINDOW_MENU )
84 {
85 m_windowMenu = (wxMenu *)NULL;
86 style -= wxFRAME_NO_WINDOW_MENU ;
87 }
88 else // normal case: we have the window menu, so construct it
89 {
90 m_windowMenu = new wxMenu;
91
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"));
98 }
99
100 wxFrame::Create( parent , id , title , pos , size , style , name ) ;
101 m_parentFrameActive = true;
102
103 OnCreateClient();
104
105 return true;
106 }
107
108 wxMDIParentFrame::~wxMDIParentFrame()
109 {
110 DestroyChildren();
111 // already deleted by DestroyChildren()
112 m_clientWindow = NULL ;
113
114 delete m_windowMenu;
115 }
116
117
118 void wxMDIParentFrame::SetMenuBar(wxMenuBar *menu_bar)
119 {
120 wxFrame::SetMenuBar( menu_bar ) ;
121 }
122
123 void wxMDIParentFrame::GetRectForTopLevelChildren(int *x, int *y, int *w, int *h)
124 {
125 if(x)
126 *x = 0;
127 if(y)
128 *y = 0;
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 if(activating)
191 {
192 if(s_macDeactivateWindow && s_macDeactivateWindow->GetParent()==this)
193 {
194 wxLogTrace(TRACE_MDI, wxT("child had been scheduled for deactivation, rehighlighting"));
195 UMAHighlightAndActivateWindow((WindowRef)s_macDeactivateWindow->MacGetWindowRef(), true);
196 wxLogTrace(TRACE_MDI, wxT("done highliting child"));
197 s_macDeactivateWindow = NULL;
198 }
199 else if(s_macDeactivateWindow == this)
200 {
201 wxLogTrace(TRACE_MDI, wxT("Avoided deactivation/activation of this=%p"), this);
202 s_macDeactivateWindow = NULL;
203 }
204 else // window to deactivate is NULL or is not us or one of our kids
205 {
206 // activate kid instead
207 if(m_currentChild)
208 m_currentChild->MacActivate(timestamp,activating);
209 else
210 wxFrame::MacActivate(timestamp,activating);
211 }
212 }
213 else
214 {
215 // We were scheduled for deactivation, and now we do it.
216 if(s_macDeactivateWindow==this)
217 {
218 s_macDeactivateWindow = NULL;
219 if(m_currentChild)
220 m_currentChild->MacActivate(timestamp,activating);
221 wxFrame::MacActivate(timestamp,activating);
222 }
223 else // schedule ourselves for deactivation
224 {
225 if(s_macDeactivateWindow)
226 wxLogTrace(TRACE_MDI, wxT("window=%p SHOULD have been deactivated, oh well!"),s_macDeactivateWindow);
227 wxLogTrace(TRACE_MDI, wxT("Scheduling delayed MDI Parent deactivation"));
228 s_macDeactivateWindow = this;
229 }
230 }
231 }
232
233 void wxMDIParentFrame::OnActivate(wxActivateEvent& event)
234 {
235 event.Skip();
236 }
237
238 // Returns the active MDI child window
239 wxMDIChildFrame *wxMDIParentFrame::GetActiveChild() const
240 {
241 return m_currentChild ;
242 }
243
244 // Create the client window class (don't Create the window,
245 // just return a new class)
246 wxMDIClientWindow *wxMDIParentFrame::OnCreateClient()
247 {
248 m_clientWindow = new wxMDIClientWindow( this );
249 return m_clientWindow;
250 }
251
252 // Responds to colour changes, and passes event on to children.
253 void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent& event)
254 {
255 // TODO
256
257 // Propagate the event to the non-top-level children
258 wxFrame::OnSysColourChanged(event);
259 }
260
261 // MDI operations
262 void wxMDIParentFrame::Cascade()
263 {
264 // TODO
265 }
266
267 void wxMDIParentFrame::Tile(wxOrientation WXUNUSED(orient))
268 {
269 // TODO
270 }
271
272 void wxMDIParentFrame::ArrangeIcons()
273 {
274 // TODO
275 }
276
277 void wxMDIParentFrame::ActivateNext()
278 {
279 // TODO
280 }
281
282 void wxMDIParentFrame::ActivatePrevious()
283 {
284 // TODO
285 }
286
287 bool wxMDIParentFrame::ShouldBeVisible() const
288 {
289 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
290 node;
291 node = node->GetNext() )
292 {
293 if ( !wxDynamicCast(node->GetData(), wxMDIChildFrame)
294 #if wxUSE_STATUSBAR
295 && node->GetData() != GetStatusBar()
296 #endif // wxUSE_STATUSBAR
297 && node->GetData() != GetClientWindow() )
298 {
299 // if we have a non-MDI child, do remain visible so that it could
300 // be used
301 return true;
302 }
303 }
304
305 return false;
306 }
307
308 bool wxMDIParentFrame::Show( bool show )
309 {
310 m_shouldBeShown = false;
311
312 // don't really show the MDI frame unless it has any children other than
313 // MDI children as it is pretty useless in this case
314
315 if ( show )
316 {
317 if ( !ShouldBeVisible() && m_currentChild )
318 {
319 // don't make the window visible now but remember that we should
320 // have had done it
321 m_shouldBeShown = true;
322 return false;
323 }
324 }
325
326 return wxFrame::Show(show);
327 }
328
329 // ----------------------------------------------------------------------------
330 // Child frame
331 // ----------------------------------------------------------------------------
332
333 wxMDIChildFrame::wxMDIChildFrame()
334 {
335 Init() ;
336 }
337 void wxMDIChildFrame::Init()
338 {
339 }
340
341 bool wxMDIChildFrame::Create(wxMDIParentFrame *parent,
342 wxWindowID id,
343 const wxString& title,
344 const wxPoint& pos,
345 const wxSize& size,
346 long style,
347 const wxString& name)
348 {
349 SetName(name);
350
351 if ( id > -1 )
352 m_windowId = id;
353 else
354 m_windowId = (int)NewControlId();
355
356 if (parent) parent->AddChild(this);
357
358 MacCreateRealWindow( title, pos , size , MacRemoveBordersFromStyle(style) , name ) ;
359
360 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE));
361
362 wxModelessWindows.Append(this);
363 return false;
364 }
365
366 wxMDIChildFrame::~wxMDIChildFrame()
367 {
368 DestroyChildren();
369 }
370
371 void wxMDIChildFrame::SetMenuBar(wxMenuBar *menu_bar)
372 {
373 return wxFrame::SetMenuBar( menu_bar ) ;
374 }
375
376 void wxMDIChildFrame::MacActivate(long timestamp, bool activating)
377 {
378 wxLogTrace(TRACE_MDI, wxT("MDI child=%p MacActivate(0x%08lx,%s)"),this,timestamp,activating?wxT("ACTIV"):wxT("deact"));
379 wxMDIParentFrame *mdiparent = wxDynamicCast(m_parent, wxMDIParentFrame);
380 wxASSERT(mdiparent);
381 if(activating)
382 {
383 if(s_macDeactivateWindow == m_parent)
384 {
385 wxLogTrace(TRACE_MDI, wxT("parent had been scheduled for deactivation, rehighlighting"));
386 UMAHighlightAndActivateWindow((WindowRef)s_macDeactivateWindow->MacGetWindowRef(), true);
387 wxLogTrace(TRACE_MDI, wxT("done highliting parent"));
388 s_macDeactivateWindow = NULL;
389 }
390 else if((mdiparent->m_currentChild==this) || !s_macDeactivateWindow)
391 mdiparent->wxFrame::MacActivate(timestamp,activating);
392
393 if(mdiparent->m_currentChild && mdiparent->m_currentChild!=this)
394 mdiparent->m_currentChild->wxFrame::MacActivate(timestamp,false);
395 mdiparent->m_currentChild = this;
396
397 if(s_macDeactivateWindow==this)
398 {
399 wxLogTrace(TRACE_MDI, wxT("Avoided deactivation/activation of this=%p"),this);
400 s_macDeactivateWindow=NULL;
401 }
402 else
403 wxFrame::MacActivate(timestamp, activating);
404 }
405 else
406 {
407 // We were scheduled for deactivation, and now we do it.
408 if(s_macDeactivateWindow==this)
409 {
410 s_macDeactivateWindow = NULL;
411 wxFrame::MacActivate(timestamp,activating);
412 if(mdiparent->m_currentChild==this)
413 mdiparent->wxFrame::MacActivate(timestamp,activating);
414 }
415 else // schedule ourselves for deactivation
416 {
417 if(s_macDeactivateWindow)
418 wxLogTrace(TRACE_MDI, wxT("window=%p SHOULD have been deactivated, oh well!"),s_macDeactivateWindow);
419 wxLogTrace(TRACE_MDI, wxT("Scheduling delayed deactivation"));
420 s_macDeactivateWindow = this;
421 }
422 }
423 }
424
425 // MDI operations
426 void wxMDIChildFrame::Maximize()
427 {
428 wxFrame::Maximize() ;
429 }
430
431 void wxMDIChildFrame::Restore()
432 {
433 wxFrame::Restore() ;
434 }
435
436 void wxMDIChildFrame::Activate()
437 {
438 }
439
440 //-----------------------------------------------------------------------------
441 // wxMDIClientWindow
442 //-----------------------------------------------------------------------------
443
444 wxMDIClientWindow::wxMDIClientWindow()
445 {
446 }
447
448 wxMDIClientWindow::~wxMDIClientWindow()
449 {
450 DestroyChildren();
451 }
452
453 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame *parent, long style)
454 {
455 if ( !wxWindow::Create(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, style))
456 return false;
457
458 wxModelessWindows.Append(this);
459 return true;
460 }
461
462 // Get size *available for subwindows* i.e. excluding menu bar.
463 void wxMDIClientWindow::DoGetClientSize(int *x, int *y) const
464 {
465 wxDisplaySize( x , y ) ;
466 }
467
468 // Explicitly call default scroll behaviour
469 void wxMDIClientWindow::OnScroll(wxScrollEvent& event)
470 {
471 }
472
473 #endif // wxUSE_MDI
474