]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/mdi.mm
2006 version of Borland compiler still doesn't grok operator?: involving wxStrings
[wxWidgets.git] / src / cocoa / mdi.mm
CommitLineData
f99422e9 1/////////////////////////////////////////////////////////////////////////////
59cf2e49 2// Name: src/cocoa/mdi.mm
f99422e9
DE
3// Purpose: wxMDIParentFrame, wxMDIChildFrame, wxMDIClientWindow
4// Author: David Elliott
5// Modified by:
6// Created: 2003/09/08
7// RCS-ID: $Id$
8// Copyright: (c) 2003 David Elliott
065e208e 9// Licence: wxWidgets licence
f99422e9
DE
10/////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
16c81607
RN
13
14#if wxUSE_MDI
15
59cf2e49
WS
16#include "wx/mdi.h"
17
e7e1ad7d
DE
18#include "wx/cocoa/objc/objc_uniquifying.h"
19
f99422e9
DE
20#ifndef WX_PRECOMP
21 #include "wx/log.h"
f99422e9
DE
22#endif // WX_PRECOMP
23
24// #include "wx/cocoa/autorelease.h"
25#include "wx/cocoa/mbarman.h"
26
27#import <AppKit/NSWindow.h>
28#import <Foundation/NSNotification.h>
29// #import <AppKit/NSApplication.h>
30// #import <AppKit/NSView.h>
31
d8418952 32#include "wx/listimpl.cpp"
f99422e9
DE
33WX_DEFINE_LIST(wxCocoaMDIChildFrameList);
34
35WX_DECLARE_HASH_MAP(int, wxMDIChildFrame*, wxIntegerHash, wxIntegerEqual, wxIntMDIChildFrameHashMap);
36
37// ============================================================================
38// wxMDIParentFrameObserver
39// ============================================================================
40@interface wxMDIParentFrameObserver : NSObject
41{
42 wxMDIParentFrame *m_mdiParent;
43}
44
45- (id)init;
46- (id)initWithWxMDIParentFrame: (wxMDIParentFrame *)mdiParent;
47- (void)windowDidBecomeMain: (NSNotification *)notification;
48@end // interface wxMDIParentFrameObserver : NSObject
e7e1ad7d 49WX_DECLARE_GET_OBJC_CLASS(wxMDIParentFrameObserver,NSObject)
f99422e9
DE
50
51@implementation wxMDIParentFrameObserver : NSObject
52- (id)init
53{
2b030203 54 wxFAIL_MSG(wxT("[wxMDIParentFrameObserver -init] should never be called!"));
f99422e9
DE
55 m_mdiParent = NULL;
56 return self;
57}
58
59- (id)initWithWxMDIParentFrame: (wxMDIParentFrame *)mdiParent
60{
61 wxASSERT(mdiParent);
62 m_mdiParent = mdiParent;
63 return [super init];
64}
65
66- (void)windowDidBecomeMain: (NSNotification *)notification
67{
68 wxASSERT(m_mdiParent);
69 m_mdiParent->WindowDidBecomeMain(notification);
70}
71
72@end // implementation wxMDIParentFrameObserver : NSObject
e7e1ad7d 73WX_IMPLEMENT_GET_OBJC_CLASS(wxMDIParentFrameObserver,NSObject)
f99422e9
DE
74
75// ========================================================================
76// wxMDIParentFrame
77// ========================================================================
78IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame,wxFrame)
79BEGIN_EVENT_TABLE(wxMDIParentFrame,wxFrame)
80END_EVENT_TABLE()
81
82void wxMDIParentFrame::Init()
83{
84 m_clientWindow = NULL;
85 m_currentChild = NULL;
e7e1ad7d 86 m_observer = [[WX_GET_OBJC_CLASS(wxMDIParentFrameObserver) alloc]
f99422e9
DE
87 initWithWxMDIParentFrame:this];
88 [[NSNotificationCenter defaultCenter] addObserver:m_observer
89 selector:@selector(windowDidBecomeMain:)
90 name:NSWindowDidBecomeMainNotification object:nil];
91}
92
93bool wxMDIParentFrame::Create(wxWindow *parent,
94 wxWindowID winid, const wxString& title,
95 const wxPoint& pos, const wxSize& size,
96 long style, const wxString& name)
97{
6e42617a
VZ
98 if ( !wxFrame::Create(parent,winid,title,pos,size,style,name) )
99 return false;
100
101 m_clientWindow = OnCreateClient();
102
103 return m_clientWindow != NULL;
f99422e9
DE
104}
105
106wxMDIParentFrame::~wxMDIParentFrame()
107{
108 for(wxCocoaMDIChildFrameList::compatibility_iterator node =
109 m_mdiChildren.GetFirst(); node; node = m_mdiChildren.GetFirst())
110 {
111 wxMDIChildFrame *child = node->GetData();
112 // Delete it NOW
113 delete child;
114 wxASSERT_MSG(!m_mdiChildren.Find(child),
115 wxT("MDI child didn't remove itself using RemoveMDIChild()"));
116 }
117 [m_observer release];
118}
119
120void wxMDIParentFrame::AddMDIChild(wxMDIChildFrame *child)
121{
122 m_mdiChildren.Append(child);
123}
124
125void wxMDIParentFrame::RemoveMDIChild(wxMDIChildFrame *child)
126{
127 m_mdiChildren.DeleteObject(child);
128 if(child==m_currentChild)
129 SetActiveChild(NULL);
130}
131
132wxMDIChildFrame *wxMDIParentFrame::GetActiveChild() const
133{
134 return m_currentChild;
135}
136
137void wxMDIParentFrame::SetActiveChild(wxMDIChildFrame *child)
138{
139 m_currentChild = child;
140 wxMenuBarManager::GetInstance()->UpdateMenuBar();
141}
142
143wxMDIClientWindow *wxMDIParentFrame::GetClientWindow() const
144{
145 return m_clientWindow;
146}
147
148wxMDIClientWindow *wxMDIParentFrame::OnCreateClient()
149{
6e42617a 150 return new wxMDIClientWindow( this );
f99422e9
DE
151}
152
153void wxMDIParentFrame::ActivateNext()
154{
155}
156
157void wxMDIParentFrame::ActivatePrevious()
158{
159}
160
161wxMenuBar *wxMDIParentFrame::GetAppMenuBar(wxCocoaNSWindow *win)
162{
163 if(m_currentChild && (win==this))
164 return m_currentChild->GetAppMenuBar(win);
165 return wxFrame::GetAppMenuBar(win);
166}
167
168void wxMDIParentFrame::CocoaDelegate_windowDidBecomeKey(void)
169{
48580976 170 wxLogTrace(wxTRACE_COCOA,wxT("wxMDIParentFrame=%p::CocoaDelegate_windowDidBecomeKey"),this);
f99422e9
DE
171 if(sm_cocoaDeactivateWindow && sm_cocoaDeactivateWindow==m_currentChild)
172 {
173 sm_cocoaDeactivateWindow = NULL;
174 }
175 #if 0
176 else if(sm_cocoaDeactivateWindow == this)
177 {
178 sm_cocoaDeactivateWindow = NULL;
179 }
180 #endif
181 else
182 {
183 if(m_currentChild)
184 {
185 NSWindow *nswin = m_currentChild->GetNSWindow();
186 if(![nswin isMainWindow])
187 [nswin makeMainWindow];
188 }
189 wxFrame::CocoaDelegate_windowDidBecomeKey();
190 }
191}
192
193void wxMDIParentFrame::CocoaDelegate_windowDidResignKey(void)
194{
48580976 195 wxLogTrace(wxTRACE_COCOA,wxT("wxMDIParentFrame=%p::CocoaDelegate_windowDidResignKey"),this);
f99422e9
DE
196 if(m_closed)
197 wxFrame::CocoaDelegate_windowDidResignKey();
198 else
199 sm_cocoaDeactivateWindow = this;
200}
201
202// We should not become the main window as we aren't a document window
203// MDI "Children" should be the main window
204bool wxMDIParentFrame::Cocoa_canBecomeMainWindow(bool &canBecome)
205{
206 canBecome = m_mdiChildren.IsEmpty(); return true;
207}
208
209void wxMDIParentFrame::WindowDidBecomeMain(NSNotification *notification)
210{
211 // If we aren't the key window, we don't care
212 if(![m_cocoaNSWindow isKeyWindow])
213 return;
214 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
215 // If we are key and becoming main, that's great
216 if(win==this)
217 return;
218 // If one of our children is becoming main, also great
219 for(wxCocoaMDIChildFrameList::compatibility_iterator node =
220 m_mdiChildren.GetFirst(); node; node = node->GetNext())
221 {
222 wxMDIChildFrame *child = node->GetData();
223 if(win==child)
224 return;
225 }
226 // Some other window is becoming main, but we are key
227 // Make the new main window the key window
228 [[notification object] makeKeyWindow];
229 if(!m_currentChild)
230 {
231 wxIntMDIChildFrameHashMap hashmap;
232 for(wxCocoaMDIChildFrameList::compatibility_iterator node =
233 m_mdiChildren.GetFirst(); node; node = node->GetNext())
234 {
235 wxMDIChildFrame *child = node->GetData();
236 hashmap.insert(wxIntMDIChildFrameHashMap::value_type([child->m_cocoaNSWindow windowNumber],child));
237 }
238 if(!hashmap.empty())
239 {
4799f3ba 240 NSInteger windowCount = 0;
f99422e9
DE
241 NSCountWindows(&windowCount);
242 wxASSERT(windowCount>0);
4799f3ba 243 NSInteger *windowList = new NSInteger[windowCount];
f99422e9
DE
244 NSWindowList(windowCount, windowList);
245 wxIntMDIChildFrameHashMap::iterator iter = hashmap.end();
246 for(int i=0; i<windowCount && iter == hashmap.end(); i++)
247 iter=hashmap.find(windowList[i]);
248 if(iter != hashmap.end())
249 m_currentChild = iter->second;
250 }
251 }
252}
253
254// ========================================================================
255// wxMDIChildFrame
256// ========================================================================
257IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame,wxFrame)
258BEGIN_EVENT_TABLE(wxMDIChildFrame,wxFrame)
259END_EVENT_TABLE()
260
261void wxMDIChildFrame::Init()
262{
263 m_mdiParent = NULL;
264}
265
266bool wxMDIChildFrame::Create(wxMDIParentFrame *parent,
267 wxWindowID winid, const wxString& title,
268 const wxPoint& pos, const wxSize& size,
269 long style, const wxString& name)
270{
271 bool success = wxFrame::Create(parent,winid,title,pos,size,style,name);
272 if(success)
273 {
274 m_mdiParent = parent;
275 parent->AddMDIChild(this);
276 }
277 return success;
278}
279
280wxMDIChildFrame::~wxMDIChildFrame()
281{
282 // Just in case Destroy() wasn't called
283 m_mdiParent->RemoveMDIChild(this);
284}
285
286void wxMDIChildFrame::Activate()
287{
288}
289
290void wxMDIChildFrame::CocoaDelegate_windowDidBecomeKey(void)
291{
48580976 292 wxLogTrace(wxTRACE_COCOA,wxT("wxMDIChildFrame=%p::CocoaDelegate_windowDidBecomeKey"),this);
f99422e9
DE
293 if(sm_cocoaDeactivateWindow && sm_cocoaDeactivateWindow==m_mdiParent)
294 {
295 sm_cocoaDeactivateWindow = NULL;
296 if(m_mdiParent->GetActiveChild() != this)
297 sm_cocoaDeactivateWindow = m_mdiParent->GetActiveChild();
298 }
299 m_mdiParent->SetActiveChild(this);
300 wxFrame::CocoaDelegate_windowDidBecomeKey();
301}
302
303void wxMDIChildFrame::CocoaDelegate_windowDidBecomeMain(void)
304{
305 m_mdiParent->SetActiveChild(this);
306 wxFrame::CocoaDelegate_windowDidBecomeMain();
307}
308
309void wxMDIChildFrame::CocoaDelegate_windowDidResignKey(void)
310{
48580976 311 wxLogTrace(wxTRACE_COCOA,wxT("wxMDIChildFrame=%p::CocoaDelegate_windowDidResignKey"),this);
f99422e9
DE
312 sm_cocoaDeactivateWindow = this;
313}
314
315bool wxMDIChildFrame::Destroy()
316{
317 // It's good to do this here before we are really closed
318 m_mdiParent->RemoveMDIChild(this);
319 return wxFrame::Destroy();
320}
321
322// ========================================================================
323// wxMDIClientWindow
324// ========================================================================
325IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow,wxWindow)
326
327wxMDIClientWindow::wxMDIClientWindow()
328{
329}
330
331wxMDIClientWindow::wxMDIClientWindow(wxMDIParentFrame *parent, long style)
59cf2e49 332 :wxWindow(parent, wxID_ANY)
f99422e9
DE
333{
334}
335
336wxMDIClientWindow::~wxMDIClientWindow()
337{
338}
339
340bool wxMDIClientWindow::CreateClient( wxMDIParentFrame *parent, long style)
341{
342 return false;
343}
344
16c81607 345#endif