]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/NSWindow.mm
Add Objective-C uniquifying to WXNSWindow and WXNSPanel
[wxWidgets.git] / src / cocoa / NSWindow.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/NSWindow.mm
3 // Purpose: wxCocoaNSWindow
4 // Author: David Elliott
5 // Modified by:
6 // Created: 2003/03/16
7 // RCS-ID: $Id$
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include "wx/wxprec.h"
21 #ifndef WX_PRECOMP
22 #include "wx/log.h"
23 #include "wx/menuitem.h"
24 #endif // WX_PRECOMP
25
26 #include "wx/cocoa/NSWindow.h"
27
28 #import <Foundation/NSNotification.h>
29 #import <Foundation/NSString.h>
30 #include "wx/cocoa/objc/NSWindow.h"
31
32 // ============================================================================
33 // @class wxNSWindowDelegate
34 // ============================================================================
35 @interface wxNSWindowDelegate : NSObject
36 {
37 wxCocoaNSWindow *m_wxCocoaInterface;
38 }
39
40 - (id)init;
41 - (void)setWxCocoaInterface: (wxCocoaNSWindow *)wxCocoaInterface;
42 - (wxCocoaNSWindow *)wxCocoaInterface;
43
44 // Delegate message handlers
45 - (void)windowDidBecomeKey: (NSNotification *)notification;
46 - (void)windowDidResignKey: (NSNotification *)notification;
47 - (void)windowDidBecomeMain: (NSNotification *)notification;
48 - (void)windowDidResignMain: (NSNotification *)notification;
49 - (BOOL)windowShouldClose: (id)sender;
50 - (void)windowWillClose: (NSNotification *)notification;
51
52 // Menu item handlers
53 - (void)wxMenuItemAction: (NSMenuItem *)menuItem;
54 - (BOOL)validateMenuItem: (NSMenuItem *)menuItem;
55 @end //interface wxNSWindowDelegate
56
57 @implementation wxNSWindowDelegate : NSObject
58
59 - (id)init
60 {
61 m_wxCocoaInterface = NULL;
62 return [super init];
63 }
64
65 - (void)setWxCocoaInterface: (wxCocoaNSWindow *)wxCocoaInterface
66 {
67 m_wxCocoaInterface = wxCocoaInterface;
68 }
69
70 - (wxCocoaNSWindow *)wxCocoaInterface
71 {
72 return m_wxCocoaInterface;
73 }
74
75 // Delegate message handlers
76 - (void)windowDidBecomeKey: (NSNotification *)notification
77 {
78 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
79 wxASSERT(win==m_wxCocoaInterface);
80 wxCHECK_RET(win,wxT("notificationDidBecomeKey received but no wxWindow exists"));
81 win->CocoaDelegate_windowDidBecomeKey();
82 }
83
84 - (void)windowDidResignKey: (NSNotification *)notification
85 {
86 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
87 wxASSERT(win==m_wxCocoaInterface);
88 wxCHECK_RET(win,wxT("notificationDidResignKey received but no wxWindow exists"));
89 win->CocoaDelegate_windowDidResignKey();
90 }
91
92 - (void)windowDidBecomeMain: (NSNotification *)notification
93 {
94 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
95 wxASSERT(win==m_wxCocoaInterface);
96 wxCHECK_RET(win,wxT("notificationDidBecomeMain received but no wxWindow exists"));
97 win->CocoaDelegate_windowDidBecomeMain();
98 }
99
100 - (void)windowDidResignMain: (NSNotification *)notification
101 {
102 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
103 wxASSERT(win==m_wxCocoaInterface);
104 wxCHECK_RET(win,wxT("notificationDidResignMain received but no wxWindow exists"));
105 win->CocoaDelegate_windowDidResignMain();
106 }
107
108 - (BOOL)windowShouldClose: (id)sender
109 {
110 wxLogTrace(wxTRACE_COCOA,wxT("windowShouldClose"));
111 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(sender);
112 wxASSERT(tlw==m_wxCocoaInterface);
113 if(tlw && !tlw->CocoaDelegate_windowShouldClose())
114 {
115 wxLogTrace(wxTRACE_COCOA,wxT("Window will not be closed"));
116 return NO;
117 }
118 wxLogTrace(wxTRACE_COCOA,wxT("Window will be closed"));
119 return YES;
120 }
121
122 - (void)windowWillClose: (NSNotification *)notification
123 {
124 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
125 wxASSERT(win==m_wxCocoaInterface);
126 wxCHECK_RET(win,wxT("windowWillClose received but no wxWindow exists"));
127 win->CocoaDelegate_windowWillClose();
128 }
129
130 // Menu item handlers
131 - (void)wxMenuItemAction: (NSMenuItem *)sender
132 {
133 wxASSERT(m_wxCocoaInterface);
134 m_wxCocoaInterface->CocoaDelegate_wxMenuItemAction(sender);
135 }
136
137 - (BOOL)validateMenuItem: (NSMenuItem *)sender
138 {
139 wxASSERT(m_wxCocoaInterface);
140 return m_wxCocoaInterface->CocoaDelegate_validateMenuItem(sender);
141 }
142
143 @end //implementation wxNSWindowDelegate
144
145 // ============================================================================
146 // class wxCocoaNSWindow
147 // ============================================================================
148
149 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSWindow)
150
151 wxCocoaNSWindow::wxCocoaNSWindow(wxTopLevelWindowCocoa *tlw)
152 : m_wxTopLevelWindowCocoa(tlw)
153 {
154 m_cocoaDelegate = [[wxNSWindowDelegate alloc] init];
155 [m_cocoaDelegate setWxCocoaInterface: this];
156 }
157
158 wxCocoaNSWindow::~wxCocoaNSWindow()
159 {
160 [m_cocoaDelegate setWxCocoaInterface: NULL];
161 [m_cocoaDelegate release];
162 }
163
164 void wxCocoaNSWindow::AssociateNSWindow(WX_NSWindow cocoaNSWindow)
165 {
166 if(cocoaNSWindow)
167 {
168 [cocoaNSWindow setReleasedWhenClosed: NO];
169 sm_cocoaHash.insert(wxCocoaNSWindowHash::value_type(cocoaNSWindow,this));
170 [cocoaNSWindow setDelegate: m_cocoaDelegate];
171 }
172 }
173
174 void wxCocoaNSWindow::DisassociateNSWindow(WX_NSWindow cocoaNSWindow)
175 {
176 if(cocoaNSWindow)
177 {
178 [cocoaNSWindow setDelegate: nil];
179 sm_cocoaHash.erase(cocoaNSWindow);
180 }
181 }
182
183 wxMenuBar* wxCocoaNSWindow::GetAppMenuBar(wxCocoaNSWindow *win)
184 {
185 return NULL;
186 }
187
188 // ============================================================================
189 // @class WXNSWindow
190 // ============================================================================
191 @implementation WXNSWindow : NSWindow
192
193 - (BOOL)canBecomeKeyWindow
194 {
195 bool canBecome = false;
196 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
197 if(!tlw || !tlw->Cocoa_canBecomeKeyWindow(canBecome))
198 canBecome = [super canBecomeKeyWindow];
199 return canBecome;
200 }
201
202 - (BOOL)canBecomeMainWindow
203 {
204 bool canBecome = false;
205 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
206 if(!tlw || !tlw->Cocoa_canBecomeMainWindow(canBecome))
207 canBecome = [super canBecomeMainWindow];
208 return canBecome;
209 }
210
211 @end // implementation WXNSWindow
212 WX_IMPLEMENT_GET_OBJC_CLASS(WXNSWindow,NSWindow)
213
214 // ============================================================================
215 // @class WXNSPanel
216 // ============================================================================
217 @implementation WXNSPanel : NSPanel
218
219 - (BOOL)canBecomeKeyWindow
220 {
221 bool canBecome = false;
222 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
223 if(!tlw || !tlw->Cocoa_canBecomeKeyWindow(canBecome))
224 canBecome = [super canBecomeKeyWindow];
225 return canBecome;
226 }
227
228 - (BOOL)canBecomeMainWindow
229 {
230 bool canBecome = false;
231 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
232 if(!tlw || !tlw->Cocoa_canBecomeMainWindow(canBecome))
233 canBecome = [super canBecomeMainWindow];
234 return canBecome;
235 }
236
237 @end // implementation WXNSPanel
238 WX_IMPLEMENT_GET_OBJC_CLASS(WXNSPanel,NSPanel)