]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: 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/ObjcPose.h" | |
27 | #include "wx/cocoa/NSWindow.h" | |
28 | ||
29 | #import <AppKit/NSWindow.h> | |
30 | #import <Foundation/NSNotification.h> | |
31 | #import <Foundation/NSString.h> | |
32 | ||
33 | // ============================================================================ | |
34 | // @class wxNSWindowDelegate | |
35 | // ============================================================================ | |
36 | @interface wxNSWindowDelegate : NSObject | |
37 | { | |
38 | wxCocoaNSWindow *m_wxCocoaInterface; | |
39 | } | |
40 | ||
41 | - (id)init; | |
42 | - (void)setWxCocoaInterface: (wxCocoaNSWindow *)wxCocoaInterface; | |
43 | - (wxCocoaNSWindow *)wxCocoaInterface; | |
44 | ||
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 | ||
53 | // Menu item handlers | |
54 | - (void)wxMenuItemAction: (NSMenuItem *)menuItem; | |
55 | - (BOOL)validateMenuItem: (NSMenuItem *)menuItem; | |
56 | @end //interface wxNSWindowDelegate | |
57 | ||
58 | @implementation wxNSWindowDelegate : NSObject | |
59 | ||
60 | - (id)init | |
61 | { | |
62 | m_wxCocoaInterface = NULL; | |
63 | return [super init]; | |
64 | } | |
65 | ||
66 | - (void)setWxCocoaInterface: (wxCocoaNSWindow *)wxCocoaInterface | |
67 | { | |
68 | m_wxCocoaInterface = wxCocoaInterface; | |
69 | } | |
70 | ||
71 | - (wxCocoaNSWindow *)wxCocoaInterface | |
72 | { | |
73 | return m_wxCocoaInterface; | |
74 | } | |
75 | ||
76 | // Delegate message handlers | |
77 | - (void)windowDidBecomeKey: (NSNotification *)notification | |
78 | { | |
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(); | |
83 | } | |
84 | ||
85 | - (void)windowDidResignKey: (NSNotification *)notification | |
86 | { | |
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(); | |
91 | } | |
92 | ||
93 | - (void)windowDidBecomeMain: (NSNotification *)notification | |
94 | { | |
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(); | |
99 | } | |
100 | ||
101 | - (void)windowDidResignMain: (NSNotification *)notification | |
102 | { | |
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(); | |
107 | } | |
108 | ||
109 | - (BOOL)windowShouldClose: (id)sender | |
110 | { | |
111 | wxLogTrace(wxTRACE_COCOA,wxT("windowShouldClose")); | |
112 | wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(sender); | |
113 | wxASSERT(tlw==m_wxCocoaInterface); | |
114 | if(tlw && !tlw->CocoaDelegate_windowShouldClose()) | |
115 | { | |
116 | wxLogTrace(wxTRACE_COCOA,wxT("Window will not be closed")); | |
117 | return NO; | |
118 | } | |
119 | wxLogTrace(wxTRACE_COCOA,wxT("Window will be closed")); | |
120 | return YES; | |
121 | } | |
122 | ||
123 | - (void)windowWillClose: (NSNotification *)notification | |
124 | { | |
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(); | |
129 | } | |
130 | ||
131 | // Menu item handlers | |
132 | - (void)wxMenuItemAction: (NSMenuItem *)sender | |
133 | { | |
134 | wxASSERT(m_wxCocoaInterface); | |
135 | m_wxCocoaInterface->CocoaDelegate_wxMenuItemAction(sender); | |
136 | } | |
137 | ||
138 | - (BOOL)validateMenuItem: (NSMenuItem *)sender | |
139 | { | |
140 | wxASSERT(m_wxCocoaInterface); | |
141 | return m_wxCocoaInterface->CocoaDelegate_validateMenuItem(sender); | |
142 | } | |
143 | ||
144 | @end //implementation wxNSWindowDelegate | |
145 | ||
146 | // ============================================================================ | |
147 | // class wxCocoaNSWindow | |
148 | // ============================================================================ | |
149 | ||
150 | WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSWindow) | |
151 | ||
152 | wxCocoaNSWindow::wxCocoaNSWindow(wxTopLevelWindowCocoa *tlw) | |
153 | : m_wxTopLevelWindowCocoa(tlw) | |
154 | { | |
155 | m_cocoaDelegate = [[wxNSWindowDelegate alloc] init]; | |
156 | [m_cocoaDelegate setWxCocoaInterface: this]; | |
157 | } | |
158 | ||
159 | wxCocoaNSWindow::~wxCocoaNSWindow() | |
160 | { | |
161 | [m_cocoaDelegate setWxCocoaInterface: NULL]; | |
162 | [m_cocoaDelegate release]; | |
163 | } | |
164 | ||
165 | void wxCocoaNSWindow::AssociateNSWindow(WX_NSWindow cocoaNSWindow) | |
166 | { | |
167 | if(cocoaNSWindow) | |
168 | { | |
169 | [cocoaNSWindow setReleasedWhenClosed: NO]; | |
170 | sm_cocoaHash.insert(wxCocoaNSWindowHash::value_type(cocoaNSWindow,this)); | |
171 | [cocoaNSWindow setDelegate: m_cocoaDelegate]; | |
172 | } | |
173 | } | |
174 | ||
175 | void wxCocoaNSWindow::DisassociateNSWindow(WX_NSWindow cocoaNSWindow) | |
176 | { | |
177 | if(cocoaNSWindow) | |
178 | { | |
179 | [cocoaNSWindow setDelegate: nil]; | |
180 | sm_cocoaHash.erase(cocoaNSWindow); | |
181 | } | |
182 | } | |
183 | ||
184 | wxMenuBar* wxCocoaNSWindow::GetAppMenuBar(wxCocoaNSWindow *win) | |
185 | { | |
186 | return NULL; | |
187 | } | |
188 | ||
189 | // ============================================================================ | |
190 | // @class wxPoserNSWindow | |
191 | // ============================================================================ | |
192 | @interface wxPoserNSWindow : NSWindow | |
193 | { | |
194 | } | |
195 | ||
196 | - (BOOL)canBecomeMainWindow; | |
197 | @end // wxPoserNSwindow | |
198 | ||
199 | WX_IMPLEMENT_POSER(wxPoserNSWindow); | |
200 | @implementation wxPoserNSWindow : NSWindow | |
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 wxPoserNSWindow | |
212 |