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;
54 - (void)wxMenuItemAction: (NSMenuItem *)menuItem;
55 - (BOOL)validateMenuItem: (NSMenuItem *)menuItem;
56 @end //interface wxNSWindowDelegate
58 @implementation wxNSWindowDelegate : NSObject
62 m_wxCocoaInterface = NULL;
66 - (void)setWxCocoaInterface: (wxCocoaNSWindow *)wxCocoaInterface
68 m_wxCocoaInterface = wxCocoaInterface;
71 - (wxCocoaNSWindow *)wxCocoaInterface
73 return m_wxCocoaInterface;
76 // Delegate message handlers
77 - (void)windowDidBecomeKey: (NSNotification *)notification
79 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
80 wxASSERT(win==m_wxCocoaInterface);
81 wxCHECK_RET(win,wxT("notificationDidBecomeKey received but no wxWindow exists"));
82 win->CocoaDelegate_windowDidBecomeKey();
85 - (void)windowDidResignKey: (NSNotification *)notification
87 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
88 wxASSERT(win==m_wxCocoaInterface);
89 wxCHECK_RET(win,wxT("notificationDidResignKey received but no wxWindow exists"));
90 win->CocoaDelegate_windowDidResignKey();
93 - (void)windowDidBecomeMain: (NSNotification *)notification
95 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
96 wxASSERT(win==m_wxCocoaInterface);
97 wxCHECK_RET(win,wxT("notificationDidBecomeMain received but no wxWindow exists"));
98 win->CocoaDelegate_windowDidBecomeMain();
101 - (void)windowDidResignMain: (NSNotification *)notification
103 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
104 wxASSERT(win==m_wxCocoaInterface);
105 wxCHECK_RET(win,wxT("notificationDidResignMain received but no wxWindow exists"));
106 win->CocoaDelegate_windowDidResignMain();
109 - (BOOL)windowShouldClose: (id)sender
111 wxLogTrace(wxTRACE_COCOA,wxT("windowShouldClose"));
112 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(sender);
113 wxASSERT(tlw==m_wxCocoaInterface);
114 if(tlw && !tlw->CocoaDelegate_windowShouldClose())
116 wxLogTrace(wxTRACE_COCOA,wxT("Window will not be closed"));
119 wxLogTrace(wxTRACE_COCOA,wxT("Window will be closed"));
123 - (void)windowWillClose: (NSNotification *)notification
125 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
126 wxASSERT(win==m_wxCocoaInterface);
127 wxCHECK_RET(win,wxT("windowWillClose received but no wxWindow exists"));
128 win->CocoaDelegate_windowWillClose();
131 // Menu item handlers
132 - (void)wxMenuItemAction: (NSMenuItem *)sender
134 wxASSERT(m_wxCocoaInterface);
135 m_wxCocoaInterface->CocoaDelegate_wxMenuItemAction(sender);
138 - (BOOL)validateMenuItem: (NSMenuItem *)sender
140 wxASSERT(m_wxCocoaInterface);
141 return m_wxCocoaInterface->CocoaDelegate_validateMenuItem(sender);
144 @end //implementation wxNSWindowDelegate
146 // ============================================================================
147 // class wxCocoaNSWindow
148 // ============================================================================
150 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSWindow)
152 wxCocoaNSWindow::wxCocoaNSWindow()
154 m_cocoaDelegate = [[wxNSWindowDelegate alloc] init];
155 [m_cocoaDelegate setWxCocoaInterface: this];
158 wxCocoaNSWindow::~wxCocoaNSWindow()
160 [m_cocoaDelegate setWxCocoaInterface: NULL];
161 [m_cocoaDelegate release];
164 void wxCocoaNSWindow::AssociateNSWindow(WX_NSWindow cocoaNSWindow)
168 [cocoaNSWindow setReleasedWhenClosed: NO];
169 sm_cocoaHash.insert(wxCocoaNSWindowHash::value_type(cocoaNSWindow,this));
170 [cocoaNSWindow setDelegate: m_cocoaDelegate];
174 void wxCocoaNSWindow::DisassociateNSWindow(WX_NSWindow cocoaNSWindow)
178 [cocoaNSWindow setDelegate: nil];
179 sm_cocoaHash.erase(cocoaNSWindow);
183 wxMenuBar* wxCocoaNSWindow::GetAppMenuBar(wxCocoaNSWindow *win)
188 // ============================================================================
189 // @class wxPoserNSWindow
190 // ============================================================================
191 @interface wxPoserNSWindow : NSWindow
195 - (BOOL)canBecomeMainWindow;
196 @end // wxPoserNSwindow
198 WX_IMPLEMENT_POSER(wxPoserNSWindow);
199 @implementation wxPoserNSWindow : NSWindow
201 - (BOOL)canBecomeMainWindow
203 bool canBecome = false;
204 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
205 if(!tlw || !tlw->Cocoa_canBecomeMainWindow(canBecome))
206 canBecome = [super canBecomeMainWindow];
210 @end // implementation wxPoserNSWindow