]>
Commit | Line | Data |
---|---|---|
33e90275 SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/mac/cocoa/nonownedwnd.mm | |
3 | // Purpose: non owned window for cocoa | |
4 | // Author: DavidStefan Csomor | |
5 | // Modified by: | |
6 | // Created: 2008-06-20 | |
7 | // RCS-ID: $Id: nonownedwnd.mm 48805 2007-09-19 14:52:25Z SC $ | |
8 | // Copyright: (c) Stefan Csomor | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #if wxOSX_USE_COCOA | |
15 | #include <Cocoa/Cocoa.h> | |
16 | #else | |
17 | #include <UIKit/UIKit.h> | |
18 | #endif | |
19 | ||
20 | #ifdef __WXMAC__ | |
21 | #include "wx/osx/private.h" | |
22 | #endif | |
23 | ||
24 | #if wxOSX_USE_COCOA | |
25 | ||
26 | ||
27 | NSRect wxToNSRect( NSView* parent, const wxRect& r ) | |
28 | { | |
29 | NSRect frame = parent ? [parent bounds] : [[NSScreen mainScreen] frame]; | |
30 | int y = r.y; | |
31 | int x = r.x ; | |
32 | if ( parent != NULL && ![ parent isFlipped ] ) | |
33 | y = frame.size.height - ( r.y + r.height ); | |
34 | return NSMakeRect(x, y, r.width , r.height); | |
35 | } | |
36 | ||
37 | wxRect wxFromNSRect( NSView* parent, const NSRect& rect ) | |
38 | { | |
39 | NSRect frame = parent ? [parent bounds] : [[NSScreen mainScreen] frame]; | |
40 | int y = rect.origin.y; | |
41 | int x = rect.origin.x; | |
42 | if ( parent != NULL && ![ parent isFlipped ] ) | |
43 | y = frame.size.height - (rect.origin.y + rect.size.height); | |
44 | return wxRect( x, y, rect.size.width, rect.size.height ); | |
45 | } | |
46 | ||
47 | NSPoint wxToNSPoint( NSView* parent, const wxPoint& p ) | |
48 | { | |
49 | NSRect frame = parent ? [parent bounds] : [[NSScreen mainScreen] frame]; | |
50 | int x = p.x ; | |
51 | int y = p.y; | |
52 | if ( parent != NULL && ![ parent isFlipped ] ) | |
53 | y = frame.size.height - ( p.y ); | |
54 | return NSMakePoint(x, y); | |
55 | } | |
56 | ||
57 | wxPoint wxFromNSPoint( NSView* parent, const NSPoint& p ) | |
58 | { | |
59 | NSRect frame = parent ? [parent bounds] : [[NSScreen mainScreen] frame]; | |
60 | int x = p.x; | |
61 | int y = p.y; | |
62 | if ( parent != NULL && ![ parent isFlipped ] ) | |
63 | y = frame.size.height - ( p.y ); | |
64 | return wxPoint( x, y); | |
65 | } | |
66 | ||
67 | IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowCocoaImpl , wxNonOwnedWindowImpl ) | |
68 | ||
69 | wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl( wxNonOwnedWindow* nonownedwnd) : | |
70 | wxNonOwnedWindowImpl(nonownedwnd) | |
71 | { | |
72 | m_macWindow = NULL; | |
73 | m_macFullScreenData = NULL; | |
74 | } | |
75 | ||
76 | wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl() | |
77 | { | |
78 | m_macWindow = NULL; | |
79 | m_macFullScreenData = NULL; | |
80 | } | |
81 | ||
82 | wxNonOwnedWindowCocoaImpl::~wxNonOwnedWindowCocoaImpl() | |
83 | { | |
84 | [m_macWindow release]; | |
85 | } | |
86 | ||
87 | void wxNonOwnedWindowCocoaImpl::Destroy() | |
88 | { | |
89 | wxPendingDelete.Append( new wxDeferredObjectDeleter( this ) ); | |
90 | } | |
91 | ||
92 | void wxNonOwnedWindowCocoaImpl::Create( wxWindow* parent, const wxPoint& pos, const wxSize& size, | |
93 | long style, long extraStyle, const wxString& name ) | |
94 | { | |
95 | int windowstyle = NSBorderlessWindowMask; | |
96 | ||
97 | if ( style & wxFRAME_TOOL_WINDOW ) | |
98 | m_macWindow = [NSPanel alloc]; | |
99 | else | |
100 | m_macWindow = [NSWindow alloc]; | |
101 | ||
102 | CGWindowLevel level = kCGNormalWindowLevelKey; | |
103 | ||
104 | if ( style & wxFRAME_TOOL_WINDOW ) | |
105 | { | |
106 | if ( ( style & wxSTAY_ON_TOP ) ) | |
107 | level = kCGUtilityWindowLevel; | |
108 | else | |
109 | level = kCGFloatingWindowLevel ; | |
110 | ||
111 | } | |
112 | else if ( ( style & wxPOPUP_WINDOW ) ) | |
113 | { | |
114 | level = kCGPopUpMenuWindowLevel; | |
115 | /* | |
116 | if ( ( style & wxBORDER_NONE ) ) | |
117 | { | |
118 | wclass = kHelpWindowClass ; // has no border | |
119 | attr |= kWindowNoShadowAttribute; | |
120 | } | |
121 | else | |
122 | { | |
123 | wclass = kPlainWindowClass ; // has a single line border, it will have to do for now | |
124 | } | |
125 | */ | |
126 | } | |
127 | else if ( ( style & wxCAPTION ) ) | |
128 | { | |
129 | windowstyle |= NSTitledWindowMask ; | |
130 | } | |
131 | else if ( ( style & wxFRAME_DRAWER ) ) | |
132 | { | |
133 | /* | |
134 | wclass = kDrawerWindowClass; | |
135 | */ | |
136 | } | |
137 | else | |
138 | { | |
139 | // set these even if we have no title, otherwise the controls won't be visible | |
140 | if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) || | |
141 | ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) ) | |
142 | { | |
143 | windowstyle |= NSTitledWindowMask ; | |
144 | } | |
145 | /* | |
146 | else if ( ( style & wxNO_BORDER ) ) | |
147 | { | |
148 | wclass = kSimpleWindowClass ; | |
149 | } | |
150 | else | |
151 | { | |
152 | wclass = kPlainWindowClass ; | |
153 | } | |
154 | */ | |
155 | } | |
156 | ||
157 | if ( windowstyle & NSTitledWindowMask ) | |
158 | { | |
159 | if ( ( style & wxMINIMIZE_BOX ) ) | |
160 | windowstyle |= NSMiniaturizableWindowMask ; | |
161 | ||
162 | if ( ( style & wxMAXIMIZE_BOX ) ) | |
163 | windowstyle |= NSResizableWindowMask ; // TODO showing ZOOM ? | |
164 | ||
165 | if ( ( style & wxRESIZE_BORDER ) ) | |
166 | windowstyle |= NSResizableWindowMask ; | |
167 | ||
168 | if ( ( style & wxCLOSE_BOX) ) | |
169 | windowstyle |= NSClosableWindowMask ; | |
170 | } | |
171 | if ( extraStyle & wxFRAME_EX_METAL) | |
172 | windowstyle |= NSTexturedBackgroundWindowMask; | |
173 | ||
174 | if ( ( style & wxSTAY_ON_TOP ) ) | |
175 | level = kCGUtilityWindowLevel; | |
176 | /* | |
177 | if ( ( style & wxFRAME_FLOAT_ON_PARENT ) ) | |
178 | group = GetWindowGroupOfClass(kFloatingWindowClass); | |
179 | */ | |
180 | ||
181 | NSRect r = wxToNSRect( NULL, wxRect( pos, size) ); | |
182 | ||
183 | [m_macWindow initWithContentRect:r | |
184 | styleMask:windowstyle | |
185 | backing:NSBackingStoreBuffered | |
186 | defer:NO | |
187 | ]; | |
188 | ||
189 | [m_macWindow setLevel:level]; | |
190 | // [m_macWindow makeKeyAndOrderFront:nil]; | |
191 | } | |
192 | ||
193 | ||
194 | WXWindow wxNonOwnedWindowCocoaImpl::GetWXWindow() const | |
195 | { | |
196 | return m_macWindow; | |
197 | } | |
198 | ||
199 | void wxNonOwnedWindowCocoaImpl::Raise() | |
200 | { | |
201 | [m_macWindow orderWindow:NSWindowAbove relativeTo:0]; | |
202 | } | |
203 | ||
204 | void wxNonOwnedWindowCocoaImpl::Lower() | |
205 | { | |
206 | [m_macWindow orderWindow:NSWindowBelow relativeTo:0]; | |
207 | } | |
208 | ||
209 | bool wxNonOwnedWindowCocoaImpl::Show(bool show) | |
210 | { | |
211 | if ( show ) | |
212 | { | |
213 | [m_macWindow orderFront:nil]; | |
214 | [[m_macWindow contentView] setNeedsDisplay:YES]; | |
215 | } | |
216 | else | |
217 | [m_macWindow orderOut:nil]; | |
218 | return true; | |
219 | } | |
220 | ||
221 | bool wxNonOwnedWindowCocoaImpl::ShowWithEffect(bool show, wxShowEffect effect, unsigned timeout) | |
222 | { | |
223 | return Show(show); | |
224 | } | |
225 | ||
226 | void wxNonOwnedWindowCocoaImpl::Update() | |
227 | { | |
228 | [m_macWindow displayIfNeeded]; | |
229 | } | |
230 | ||
231 | bool wxNonOwnedWindowCocoaImpl::SetTransparent(wxByte alpha) | |
232 | { | |
233 | [m_macWindow setAlphaValue:(CGFloat) alpha/255.0]; | |
234 | return true; | |
235 | } | |
236 | ||
237 | bool wxNonOwnedWindowCocoaImpl::SetBackgroundColour(const wxColour& col ) | |
238 | { | |
239 | return true; | |
240 | } | |
241 | ||
242 | void wxNonOwnedWindowCocoaImpl::SetExtraStyle( long exStyle ) | |
243 | { | |
244 | if ( m_macWindow ) | |
245 | { | |
246 | bool metal = exStyle & wxFRAME_EX_METAL ; | |
247 | int windowStyle = [ m_macWindow styleMask]; | |
248 | if ( metal && !(windowStyle & NSTexturedBackgroundWindowMask) ) | |
249 | { | |
250 | wxFAIL_MSG( _T("Metal Style cannot be changed after creation") ); | |
251 | } | |
252 | else if ( !metal && (windowStyle & NSTexturedBackgroundWindowMask ) ) | |
253 | { | |
254 | wxFAIL_MSG( _T("Metal Style cannot be changed after creation") ); | |
255 | } | |
256 | } | |
257 | } | |
258 | ||
259 | bool wxNonOwnedWindowCocoaImpl::SetBackgroundStyle(wxBackgroundStyle style) | |
260 | { | |
261 | return true; | |
262 | } | |
263 | ||
264 | bool wxNonOwnedWindowCocoaImpl::CanSetTransparent() | |
265 | { | |
266 | return true; | |
267 | } | |
268 | ||
269 | void wxNonOwnedWindowCocoaImpl::MoveWindow(int x, int y, int width, int height) | |
270 | { | |
271 | NSRect r = wxToNSRect( NULL, wxRect(x,y,width, height) ); | |
272 | [m_macWindow setFrame:r display:YES]; | |
273 | } | |
274 | ||
275 | void wxNonOwnedWindowCocoaImpl::GetPosition( int &x, int &y ) const | |
276 | { | |
277 | wxRect r = wxFromNSRect( NULL, [m_macWindow frame] ); | |
278 | x = r.GetLeft(); | |
279 | y = r.GetTop(); | |
280 | } | |
281 | ||
282 | void wxNonOwnedWindowCocoaImpl::GetSize( int &width, int &height ) const | |
283 | { | |
284 | NSRect rect = [m_macWindow frame]; | |
285 | width = rect.size.width; | |
286 | height = rect.size.height; | |
287 | } | |
288 | ||
289 | void wxNonOwnedWindowCocoaImpl::GetContentArea( int& left, int &right, int &width, int &height ) const | |
290 | { | |
291 | NSRect outer = NSMakeRect(100,100,100,100); | |
292 | NSRect content = [NSWindow contentRectForFrameRect:outer styleMask:[m_macWindow styleMask] ]; | |
293 | NSRect rect = [[m_macWindow contentView] frame]; | |
294 | width = rect.size.width; | |
295 | height = rect.size.height; | |
296 | } | |
297 | ||
298 | bool wxNonOwnedWindowCocoaImpl::SetShape(const wxRegion& region) | |
299 | { | |
300 | return false; | |
301 | } | |
302 | ||
303 | void wxNonOwnedWindowCocoaImpl::SetTitle( const wxString& title, wxFontEncoding encoding ) | |
304 | { | |
305 | [m_macWindow setTitle:wxCFStringRef( title , encoding ).AsNSString()]; | |
306 | } | |
307 | ||
308 | bool wxNonOwnedWindowCocoaImpl::IsMaximized() const | |
309 | { | |
310 | return [m_macWindow isZoomed]; | |
311 | } | |
312 | ||
313 | bool wxNonOwnedWindowCocoaImpl::IsIconized() const | |
314 | { | |
315 | return [m_macWindow isMiniaturized]; | |
316 | } | |
317 | ||
318 | void wxNonOwnedWindowCocoaImpl::Iconize( bool iconize ) | |
319 | { | |
320 | if ( iconize ) | |
321 | [m_macWindow miniaturize:nil]; | |
322 | else | |
323 | [m_macWindow deminiaturize:nil]; | |
324 | } | |
325 | ||
326 | void wxNonOwnedWindowCocoaImpl::Maximize(bool maximize) | |
327 | { | |
328 | [m_macWindow zoom:nil]; | |
329 | } | |
330 | ||
331 | ||
332 | // http://cocoadevcentral.com/articles/000028.php | |
333 | ||
334 | typedef struct | |
335 | { | |
336 | int m_formerLevel; | |
337 | NSRect m_formerFrame; | |
338 | } FullScreenData ; | |
339 | ||
340 | bool wxNonOwnedWindowCocoaImpl::IsFullScreen() const | |
341 | { | |
342 | return m_macFullScreenData != NULL ; | |
343 | } | |
344 | ||
345 | bool wxNonOwnedWindowCocoaImpl::ShowFullScreen(bool show, long style) | |
346 | { | |
347 | if ( show ) | |
348 | { | |
349 | FullScreenData *data = (FullScreenData *)m_macFullScreenData ; | |
350 | delete data ; | |
351 | data = new FullScreenData(); | |
352 | ||
353 | m_macFullScreenData = data ; | |
354 | data->m_formerLevel = [m_macWindow level]; | |
355 | data->m_formerFrame = [m_macWindow frame]; | |
356 | CGDisplayCapture( kCGDirectMainDisplay ); | |
357 | [m_macWindow setLevel:CGShieldingWindowLevel()]; | |
358 | [m_macWindow setFrame:[[NSScreen mainScreen] frame] display:YES]; | |
359 | } | |
360 | else if ( m_macFullScreenData != NULL ) | |
361 | { | |
362 | FullScreenData *data = (FullScreenData *) m_macFullScreenData ; | |
363 | CGDisplayRelease( kCGDirectMainDisplay ); | |
364 | [m_macWindow setLevel:data->m_formerLevel]; | |
365 | [m_macWindow setFrame:data->m_formerFrame display:YES]; | |
366 | delete data ; | |
367 | m_macFullScreenData = NULL ; | |
368 | } | |
369 | ||
370 | return true; | |
371 | } | |
372 | ||
373 | void wxNonOwnedWindowCocoaImpl::RequestUserAttention(int WXUNUSED(flags)) | |
374 | { | |
375 | } | |
376 | #endif |