1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/NSWindow.mm
3 // Purpose: wxCocoaNSWindow
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
23 #include "wx/menuitem.h"
26 #include "wx/cocoa/ObjcPose.h"
27 #include "wx/cocoa/NSWindow.h"
29 #import <AppKit/NSWindow.h>
30 #import <Foundation/NSNotification.h>
31 #import <Foundation/NSString.h>
33 // ============================================================================
34 // @class wxNSWindowDelegate
35 // ============================================================================
36 @interface wxNSWindowDelegate : NSObject
38 wxCocoaNSWindow *m_wxCocoaInterface;
42 - (void)setWxCocoaInterface: (wxCocoaNSWindow *)wxCocoaInterface;
43 - (wxCocoaNSWindow *)wxCocoaInterface;
45 // Delegate message handlers
46 - (void)windowDidBecomeKey: (NSNotification *)notification;
47 - (void)windowDidResignKey: (NSNotification *)notification;
48 - (void)windowDidBecomeMain: (NSNotification *)notification;
49 - (void)windowDidResignMain: (NSNotification *)notification;
50 - (BOOL)windowShouldClose: (id)sender;
51 - (void)windowWillClose: (NSNotification *)notification;
52 @end //interface wxNSWindowDelegate
54 @implementation wxNSWindowDelegate : NSObject
58 m_wxCocoaInterface = NULL;
62 - (void)setWxCocoaInterface: (wxCocoaNSWindow *)wxCocoaInterface
64 m_wxCocoaInterface = wxCocoaInterface;
67 - (wxCocoaNSWindow *)wxCocoaInterface
69 return m_wxCocoaInterface;
72 - (void)windowDidBecomeKey: (NSNotification *)notification
74 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
75 wxASSERT(win==m_wxCocoaInterface);
76 wxCHECK_RET(win,wxT("notificationDidBecomeKey received but no wxWindow exists"));
77 win->CocoaDelegate_windowDidBecomeKey();
80 - (void)windowDidResignKey: (NSNotification *)notification
82 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
83 wxASSERT(win==m_wxCocoaInterface);
84 wxCHECK_RET(win,wxT("notificationDidResignKey received but no wxWindow exists"));
85 win->CocoaDelegate_windowDidResignKey();
88 - (void)windowDidBecomeMain: (NSNotification *)notification
90 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
91 wxASSERT(win==m_wxCocoaInterface);
92 wxCHECK_RET(win,wxT("notificationDidBecomeMain received but no wxWindow exists"));
93 win->CocoaDelegate_windowDidBecomeMain();
96 - (void)windowDidResignMain: (NSNotification *)notification
98 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
99 wxASSERT(win==m_wxCocoaInterface);
100 wxCHECK_RET(win,wxT("notificationDidResignMain received but no wxWindow exists"));
101 win->CocoaDelegate_windowDidResignMain();
104 - (BOOL)windowShouldClose: (id)sender
106 wxLogTrace(wxTRACE_COCOA,wxT("windowShouldClose"));
107 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(sender);
108 wxASSERT(tlw==m_wxCocoaInterface);
109 if(tlw && !tlw->CocoaDelegate_windowShouldClose())
111 wxLogTrace(wxTRACE_COCOA,wxT("Window will not be closed"));
114 wxLogTrace(wxTRACE_COCOA,wxT("Window will be closed"));
118 - (void)windowWillClose: (NSNotification *)notification
120 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
121 wxASSERT(win==m_wxCocoaInterface);
122 wxCHECK_RET(win,wxT("windowWillClose received but no wxWindow exists"));
123 win->CocoaDelegate_windowWillClose();
126 @end //implementation wxNSWindowDelegate
128 // ============================================================================
129 // class wxCocoaNSWindow
130 // ============================================================================
132 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSWindow)
134 wxCocoaNSWindow::wxCocoaNSWindow()
136 m_cocoaDelegate = [[wxNSWindowDelegate alloc] init];
137 [m_cocoaDelegate setWxCocoaInterface: this];
140 wxCocoaNSWindow::~wxCocoaNSWindow()
142 [m_cocoaDelegate setWxCocoaInterface: NULL];
143 [m_cocoaDelegate release];
146 void wxCocoaNSWindow::AssociateNSWindow(WX_NSWindow cocoaNSWindow)
150 [cocoaNSWindow setReleasedWhenClosed: NO];
151 sm_cocoaHash.insert(wxCocoaNSWindowHash::value_type(cocoaNSWindow,this));
152 [cocoaNSWindow setDelegate: m_cocoaDelegate];
156 void wxCocoaNSWindow::DisassociateNSWindow(WX_NSWindow cocoaNSWindow)
160 [cocoaNSWindow setDelegate: nil];
161 sm_cocoaHash.erase(cocoaNSWindow);
165 wxMenuBar* wxCocoaNSWindow::GetAppMenuBar(wxCocoaNSWindow *win)
170 // ============================================================================
171 // @class wxPoserNSWindow
172 // ============================================================================
173 @interface wxPoserNSWindow : NSWindow
177 - (BOOL)canBecomeMainWindow;
178 @end // wxPoserNSwindow
180 WX_IMPLEMENT_POSER(wxPoserNSWindow);
181 @implementation wxPoserNSWindow : NSWindow
183 - (BOOL)canBecomeMainWindow
185 bool canBecome = false;
186 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
187 if(!tlw || !tlw->Cocoa_canBecomeMainWindow(canBecome))
188 canBecome = [super canBecomeMainWindow];
192 @end // implementation wxPoserNSWindow