]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/mdi.cpp
cleanup - reformat (part 2)
[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 // already deleted by DestroyChildren()
111 m_clientWindow = NULL ;
112
113 delete m_windowMenu;
114 }
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 wxDisplaySize(w,h);
129 }
130
131 void wxMDIParentFrame::AddChild(wxWindowBase *child)
132 {
133 if ( !m_currentChild )
134 {
135 m_currentChild = wxDynamicCast(child, wxMDIChildFrame);
136
137 if ( m_currentChild && IsShown() && !ShouldBeVisible() )
138 {
139 // we shouldn't remain visible any more
140 wxFrame::Show(false);
141 m_shouldBeShown = true;
142 }
143 }
144
145 wxFrame::AddChild(child);
146 }
147
148 void wxMDIParentFrame::RemoveChild(wxWindowBase *child)
149 {
150 if ( child == m_currentChild )
151 {
152 // the current child isn't active any more, try to find another one
153 m_currentChild = NULL;
154
155 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
156 node;
157 node = node->GetNext() )
158 {
159 wxMDIChildFrame *
160 childCur = wxDynamicCast(node->GetData(), wxMDIChildFrame);
161 if ( childCur != child )
162 {
163 m_currentChild = childCur;
164 break;
165 }
166 }
167 }
168
169 wxFrame::RemoveChild(child);
170
171 // if there are no more children left we need to show the frame if we
172 // hadn't shown it before because there were active children and it was
173 // useless (note that we have to do it after fully removing the child, i.e.
174 // after calling the base class RemoveChild() as otherwise we risk to touch
175 // pointer to the child being deleted)
176 if ( !m_currentChild && m_shouldBeShown && !IsShown() )
177 {
178 // we have to show it, but at least move it out of sight and make it of
179 // smallest possible size (unfortunately (0, 0) doesn't work so that it
180 // doesn't appear in expose
181 SetSize(-10000, -10000, 1, 1);
182 Show();
183 }
184 }
185
186 void wxMDIParentFrame::MacActivate(long timestamp, bool activating)
187 {
188 wxLogTrace(TRACE_MDI, wxT("MDI PARENT=%p MacActivate(0x%08lx,%s)"),this,timestamp,activating?wxT("ACTIV"):wxT("deact"));
189 if(activating)
190 {
191 if(s_macDeactivateWindow && s_macDeactivateWindow->GetParent()==this)
192 {
193 wxLogTrace(TRACE_MDI, wxT("child had been scheduled for deactivation, rehighlighting"));
194 UMAHighlightAndActivateWindow((WindowRef)s_macDeactivateWindow->MacGetWindowRef(), true);
195 wxLogTrace(TRACE_MDI, wxT("done highliting child"));
196 s_macDeactivateWindow = NULL;
197 }
198 else if(s_macDeactivateWindow == this)
199 {
200 wxLogTrace(TRACE_MDI, wxT("Avoided deactivation/activation of this=%p"), this);
201 s_macDeactivateWindow = NULL;
202 }
203 else // window to deactivate is NULL or is not us or one of our kids
204 {
205 // activate kid instead
206 if(m_currentChild)
207 m_currentChild->MacActivate(timestamp,activating);
208 else
209 wxFrame::MacActivate(timestamp,activating);
210 }
211 }
212 else
213 {
214 // We were scheduled for deactivation, and now we do it.
215 if(s_macDeactivateWindow==this)
216 {
217 s_macDeactivateWindow = NULL;
218 if(m_currentChild)
219 m_currentChild->MacActivate(timestamp,activating);
220 wxFrame::MacActivate(timestamp,activating);
221 }
222 else // schedule ourselves for deactivation
223 {
224 if(s_macDeactivateWindow)
225 wxLogTrace(TRACE_MDI, wxT("window=%p SHOULD have been deactivated, oh well!"),s_macDeactivateWindow);
226 wxLogTrace(TRACE_MDI, wxT("Scheduling delayed MDI Parent deactivation"));
227 s_macDeactivateWindow = this;
228 }
229 }
230 }
231
232 void wxMDIParentFrame::OnActivate(wxActivateEvent& event)
233 {
234 event.Skip();
235 }
236
237 // Returns the active MDI child window
238 wxMDIChildFrame *wxMDIParentFrame::GetActiveChild() const
239 {
240 return m_currentChild ;
241 }
242
243 // Create the client window class (don't Create the window,
244 // just return a new class)
245 wxMDIClientWindow *wxMDIParentFrame::OnCreateClient()
246 {
247 m_clientWindow = new wxMDIClientWindow( this );
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 != GetStatusBar()
298 #endif // wxUSE_STATUSBAR
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 return false;
325 }
326 }
327
328 return wxFrame::Show(show);
329 }
330
331 // ----------------------------------------------------------------------------
332 // Child frame
333 // ----------------------------------------------------------------------------
334
335 wxMDIChildFrame::wxMDIChildFrame()
336 {
337 Init() ;
338 }
339 void wxMDIChildFrame::Init()
340 {
341 }
342
343 bool wxMDIChildFrame::Create(wxMDIParentFrame *parent,
344 wxWindowID id,
345 const wxString& title,
346 const wxPoint& pos,
347 const wxSize& size,
348 long style,
349 const wxString& name)
350 {
351 SetName(name);
352
353 if ( id > -1 )
354 m_windowId = id;
355 else
356 m_windowId = (int)NewControlId();
357
358 if (parent) parent->AddChild(this);
359
360 MacCreateRealWindow( title, pos , size , MacRemoveBordersFromStyle(style) , name ) ;
361
362 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE));
363
364 wxModelessWindows.Append(this);
365 return false;
366 }
367
368 wxMDIChildFrame::~wxMDIChildFrame()
369 {
370 DestroyChildren();
371 }
372
373 void wxMDIChildFrame::SetMenuBar(wxMenuBar *menu_bar)
374 {
375 return wxFrame::SetMenuBar( menu_bar ) ;
376 }
377
378 void wxMDIChildFrame::MacActivate(long timestamp, bool activating)
379 {
380 wxLogTrace(TRACE_MDI, wxT("MDI child=%p MacActivate(0x%08lx,%s)"),this,timestamp,activating?wxT("ACTIV"):wxT("deact"));
381 wxMDIParentFrame *mdiparent = wxDynamicCast(m_parent, wxMDIParentFrame);
382 wxASSERT(mdiparent);
383 if(activating)
384 {
385 if(s_macDeactivateWindow == m_parent)
386 {
387 wxLogTrace(TRACE_MDI, wxT("parent had been scheduled for deactivation, rehighlighting"));
388 UMAHighlightAndActivateWindow((WindowRef)s_macDeactivateWindow->MacGetWindowRef(), true);
389 wxLogTrace(TRACE_MDI, wxT("done highliting parent"));
390 s_macDeactivateWindow = NULL;
391 }
392 else if((mdiparent->m_currentChild==this) || !s_macDeactivateWindow)
393 mdiparent->wxFrame::MacActivate(timestamp,activating);
394
395 if(mdiparent->m_currentChild && mdiparent->m_currentChild!=this)
396 mdiparent->m_currentChild->wxFrame::MacActivate(timestamp,false);
397 mdiparent->m_currentChild = this;
398
399 if(s_macDeactivateWindow==this)
400 {
401 wxLogTrace(TRACE_MDI, wxT("Avoided deactivation/activation of this=%p"),this);
402 s_macDeactivateWindow=NULL;
403 }
404 else
405 wxFrame::MacActivate(timestamp, activating);
406 }
407 else
408 {
409 // We were scheduled for deactivation, and now we do it.
410 if(s_macDeactivateWindow==this)
411 {
412 s_macDeactivateWindow = NULL;
413 wxFrame::MacActivate(timestamp,activating);
414 if(mdiparent->m_currentChild==this)
415 mdiparent->wxFrame::MacActivate(timestamp,activating);
416 }
417 else // schedule ourselves for deactivation
418 {
419 if(s_macDeactivateWindow)
420 wxLogTrace(TRACE_MDI, wxT("window=%p SHOULD have been deactivated, oh well!"),s_macDeactivateWindow);
421 wxLogTrace(TRACE_MDI, wxT("Scheduling delayed deactivation"));
422 s_macDeactivateWindow = this;
423 }
424 }
425 }
426
427 // MDI operations
428 void wxMDIChildFrame::Maximize()
429 {
430 wxFrame::Maximize() ;
431 }
432
433 void wxMDIChildFrame::Restore()
434 {
435 wxFrame::Restore() ;
436 }
437
438 void wxMDIChildFrame::Activate()
439 {
440 }
441
442 //-----------------------------------------------------------------------------
443 // wxMDIClientWindow
444 //-----------------------------------------------------------------------------
445
446 wxMDIClientWindow::wxMDIClientWindow()
447 {
448 }
449
450 wxMDIClientWindow::~wxMDIClientWindow()
451 {
452 DestroyChildren();
453 }
454
455 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame *parent, long style)
456 {
457 if ( !wxWindow::Create(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, style))
458 return false;
459
460 wxModelessWindows.Append(this);
461 return true;
462 }
463
464 // Get size *available for subwindows* i.e. excluding menu bar.
465 void wxMDIClientWindow::DoGetClientSize(int *x, int *y) const
466 {
467 wxDisplaySize( x , y ) ;
468 }
469
470 // Explicitly call default scroll behaviour
471 void wxMDIClientWindow::OnScroll(wxScrollEvent& event)
472 {
473 }
474
475 #endif // wxUSE_MDI
476