]>
Commit | Line | Data |
---|---|---|
fb896a32 DE |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/cocoa/window.mm | |
3 | // Purpose: wxWindowCocoa | |
4 | // Author: David Elliott | |
5 | // Modified by: | |
6 | // Created: 2002/12/26 | |
8d8d3633 | 7 | // RCS-ID: $Id$ |
fb896a32 | 8 | // Copyright: (c) 2002 David Elliott |
8d8d3633 | 9 | // Licence: wxWidgets licence |
fb896a32 DE |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
449c5673 | 12 | #include "wx/wxprec.h" |
da80ae71 | 13 | |
449c5673 DE |
14 | #ifndef WX_PRECOMP |
15 | #include "wx/log.h" | |
16 | #include "wx/window.h" | |
4db3c8ac | 17 | #include "wx/dc.h" |
ca5db7b2 | 18 | #include "wx/utils.h" |
449c5673 | 19 | #endif //WX_PRECOMP |
da80ae71 | 20 | |
e73ae747 | 21 | #include "wx/tooltip.h" |
fb896a32 | 22 | |
7fc77f30 | 23 | #include "wx/cocoa/autorelease.h" |
26191790 | 24 | #include "wx/cocoa/string.h" |
7c5a378f | 25 | #include "wx/cocoa/trackingrectmanager.h" |
8ea3a63e | 26 | #include "wx/mac/corefoundation/cfref.h" |
7fc77f30 | 27 | |
369559ca | 28 | #import <Foundation/NSArray.h> |
7c5a378f | 29 | #import <Foundation/NSRunLoop.h> |
829a2e95 | 30 | #include "wx/cocoa/objc/NSView.h" |
69dbb709 | 31 | #import <AppKit/NSEvent.h> |
816c52cf DE |
32 | #import <AppKit/NSScrollView.h> |
33 | #import <AppKit/NSColor.h> | |
34 | #import <AppKit/NSClipView.h> | |
dc5bcaef | 35 | #import <Foundation/NSException.h> |
67d2dac8 DE |
36 | #import <AppKit/NSApplication.h> |
37 | #import <AppKit/NSWindow.h> | |
15f37147 | 38 | #import <AppKit/NSScreen.h> |
dc5bcaef | 39 | |
75e4856b DE |
40 | // Turn this on to paint green over the dummy views for debugging |
41 | #undef WXCOCOA_FILL_DUMMY_VIEW | |
42 | ||
43 | #ifdef WXCOCOA_FILL_DUMMY_VIEW | |
44 | #import <AppKit/NSBezierPath.h> | |
45 | #endif //def WXCOCOA_FILL_DUMMY_VIEW | |
46 | ||
8ea3a63e DE |
47 | // STL list used by wxCocoaMouseMovedEventSynthesizer |
48 | #include <list> | |
49 | ||
4799f3ba DE |
50 | /* NSComparisonResult is typedef'd as an enum pre-Leopard but typedef'd as |
51 | * NSInteger post-Leopard. Pre-Leopard the Cocoa toolkit expects a function | |
52 | * returning int and not NSComparisonResult. Post-Leopard the Cocoa toolkit | |
53 | * expects a function returning the new non-enum NSComparsionResult. | |
54 | * Hence we create a typedef named CocoaWindowCompareFunctionResult. | |
55 | */ | |
56 | #if defined(NSINTEGER_DEFINED) | |
57 | typedef NSComparisonResult CocoaWindowCompareFunctionResult; | |
58 | #else | |
59 | typedef int CocoaWindowCompareFunctionResult; | |
60 | #endif | |
61 | ||
5dc47140 DE |
62 | // A category for methods that are only present in Panther's SDK |
63 | @interface NSView(wxNSViewPrePantherCompatibility) | |
64 | - (void)getRectsBeingDrawn:(const NSRect **)rects count:(int *)count; | |
65 | @end | |
66 | ||
15f37147 DE |
67 | // ======================================================================== |
68 | // Helper functions for converting to/from wxWidgets coordinates and a | |
69 | // specified NSView's coordinate system. | |
70 | // ======================================================================== | |
ee022549 DE |
71 | NSPoint CocoaTransformNSViewBoundsToWx(NSView *nsview, NSPoint pointBounds) |
72 | { | |
73 | wxCHECK_MSG(nsview, pointBounds, wxT("Need to have a Cocoa view to do translation")); | |
74 | if([nsview isFlipped]) | |
75 | return pointBounds; | |
76 | NSRect ourBounds = [nsview bounds]; | |
77 | return NSMakePoint | |
78 | ( pointBounds.x | |
79 | , ourBounds.size.height - pointBounds.y | |
80 | ); | |
81 | } | |
82 | ||
83 | NSRect CocoaTransformNSViewBoundsToWx(NSView *nsview, NSRect rectBounds) | |
84 | { | |
85 | wxCHECK_MSG(nsview, rectBounds, wxT("Need to have a Cocoa view to do translation")); | |
86 | if([nsview isFlipped]) | |
87 | return rectBounds; | |
88 | NSRect ourBounds = [nsview bounds]; | |
89 | return NSMakeRect | |
90 | ( rectBounds.origin.x | |
91 | , ourBounds.size.height - (rectBounds.origin.y + rectBounds.size.height) | |
92 | , rectBounds.size.width | |
93 | , rectBounds.size.height | |
94 | ); | |
95 | } | |
96 | ||
97 | NSPoint CocoaTransformNSViewWxToBounds(NSView *nsview, NSPoint pointWx) | |
98 | { | |
99 | wxCHECK_MSG(nsview, pointWx, wxT("Need to have a Cocoa view to do translation")); | |
100 | if([nsview isFlipped]) | |
101 | return pointWx; | |
102 | NSRect ourBounds = [nsview bounds]; | |
103 | return NSMakePoint | |
104 | ( pointWx.x | |
105 | , ourBounds.size.height - pointWx.y | |
106 | ); | |
107 | } | |
108 | ||
109 | NSRect CocoaTransformNSViewWxToBounds(NSView *nsview, NSRect rectWx) | |
110 | { | |
111 | wxCHECK_MSG(nsview, rectWx, wxT("Need to have a Cocoa view to do translation")); | |
112 | if([nsview isFlipped]) | |
113 | return rectWx; | |
114 | NSRect ourBounds = [nsview bounds]; | |
115 | return NSMakeRect | |
116 | ( rectWx.origin.x | |
117 | , ourBounds.size.height - (rectWx.origin.y + rectWx.size.height) | |
118 | , rectWx.size.width | |
119 | , rectWx.size.height | |
120 | ); | |
121 | } | |
122 | ||
15f37147 DE |
123 | // ============================================================================ |
124 | // Screen coordinate helpers | |
125 | // ============================================================================ | |
126 | ||
127 | /* | |
128 | General observation about Cocoa screen coordinates: | |
129 | It is documented that the first object of the [NSScreen screens] array is the screen with the menubar. | |
130 | ||
131 | It is not documented (but true as far as I can tell) that (0,0) in Cocoa screen coordinates is always | |
132 | the BOTTOM-right corner of this screen. Recall that Cocoa uses cartesian coordinates so y-increase is up. | |
133 | ||
134 | It isn't clearly documented but visibleFrame returns a rectangle in screen coordinates, not a rectangle | |
135 | relative to that screen's frame. The only real way to test this is to configure two screens one atop | |
136 | the other such that the menubar screen is on top. The Dock at the bottom of the screen will then | |
137 | eat into the visibleFrame of screen 1 by incrementing it's y-origin. Thus if you arrange two | |
138 | 1920x1200 screens top/bottom then screen 1 (the bottom screen) will have frame origin (0,-1200) and | |
139 | visibleFrame origin (0,-1149) which is exactly 51 pixels higher than the full frame origin. | |
140 | ||
141 | In wxCocoa, we somewhat arbitrarily declare that wx (0,0) is the TOP-left of screen 0's frame (the entire screen). | |
142 | However, this isn't entirely arbitrary because the Quartz Display Services (CGDisplay) uses this same scheme. | |
143 | This works out nicely because wxCocoa's wxDisplay is implemented using Quartz Display Services instead of NSScreen. | |
144 | */ | |
145 | ||
146 | namespace { // file namespace | |
147 | ||
148 | class wxCocoaPrivateScreenCoordinateTransformer | |
149 | { | |
150 | DECLARE_NO_COPY_CLASS(wxCocoaPrivateScreenCoordinateTransformer) | |
151 | public: | |
152 | wxCocoaPrivateScreenCoordinateTransformer(); | |
153 | ~wxCocoaPrivateScreenCoordinateTransformer(); | |
154 | wxPoint OriginInWxDisplayCoordinatesForRectInCocoaScreenCoordinates(NSRect windowFrame); | |
155 | NSPoint OriginInCocoaScreenCoordinatesForRectInWxDisplayCoordinates(wxCoord x, wxCoord y, wxCoord width, wxCoord height, bool keepOriginVisible); | |
156 | ||
157 | protected: | |
158 | NSScreen *m_screenZero; | |
159 | NSRect m_screenZeroFrame; | |
160 | }; | |
161 | ||
162 | // NOTE: This is intended to be a short-lived object. A future enhancment might | |
163 | // make it a global and reconfigure it upon some notification that the screen layout | |
164 | // has changed. | |
165 | inline wxCocoaPrivateScreenCoordinateTransformer::wxCocoaPrivateScreenCoordinateTransformer() | |
166 | { | |
167 | NSArray *screens = [NSScreen screens]; | |
168 | ||
169 | [screens retain]; | |
170 | ||
171 | m_screenZero = nil; | |
172 | if(screens != nil && [screens count] > 0) | |
173 | m_screenZero = [[screens objectAtIndex:0] retain]; | |
174 | ||
175 | [screens release]; | |
176 | ||
177 | if(m_screenZero != nil) | |
178 | m_screenZeroFrame = [m_screenZero frame]; | |
179 | else | |
180 | { | |
181 | wxLogWarning(wxT("Can't translate to/from wx screen coordinates and Cocoa screen coordinates")); | |
182 | // Just blindly assume 1024x768 so that at least we can sort of flip things around into | |
183 | // Cocoa coordinates. | |
184 | // NOTE: Theoretically this case should never happen anyway. | |
185 | m_screenZeroFrame = NSMakeRect(0,0,1024,768); | |
186 | } | |
187 | } | |
188 | ||
189 | inline wxCocoaPrivateScreenCoordinateTransformer::~wxCocoaPrivateScreenCoordinateTransformer() | |
190 | { | |
191 | [m_screenZero release]; | |
192 | m_screenZero = nil; | |
193 | } | |
194 | ||
195 | inline wxPoint wxCocoaPrivateScreenCoordinateTransformer::OriginInWxDisplayCoordinatesForRectInCocoaScreenCoordinates(NSRect windowFrame) | |
196 | { | |
197 | // x and y are in wx screen coordinates which we're going to arbitrarily define such that | |
198 | // (0,0) is the TOP-left of screen 0 (the one with the menubar) | |
199 | // NOTE WELL: This means that (0,0) is _NOT_ an appropriate position for a window. | |
200 | ||
201 | wxPoint theWxOrigin; | |
202 | ||
203 | // Working in Cocoa's screen coordinates we must realize that the x coordinate we want is | |
204 | // the distance between the left side (origin.x) of the window's frame and the left side of | |
205 | // screen zero's frame. | |
206 | theWxOrigin.x = windowFrame.origin.x - m_screenZeroFrame.origin.x; | |
207 | ||
208 | // Working in Cocoa's screen coordinates we must realize that the y coordinate we want is | |
209 | // actually the distance between the top-left of the screen zero frame and the top-left | |
210 | // of the window's frame. | |
211 | ||
212 | theWxOrigin.y = (m_screenZeroFrame.origin.y + m_screenZeroFrame.size.height) - (windowFrame.origin.y + windowFrame.size.height); | |
213 | ||
214 | return theWxOrigin; | |
215 | } | |
216 | ||
217 | inline NSPoint wxCocoaPrivateScreenCoordinateTransformer::OriginInCocoaScreenCoordinatesForRectInWxDisplayCoordinates(wxCoord x, wxCoord y, wxCoord width, wxCoord height, bool keepOriginVisible) | |
218 | { | |
219 | NSPoint theCocoaOrigin; | |
220 | ||
221 | // The position is in wx screen coordinates which we're going to arbitrarily define such that | |
222 | // (0,0) is the TOP-left of screen 0 (the one with the menubar) | |
223 | ||
224 | // NOTE: The usable rectangle is smaller and hence we have the keepOriginVisible flag | |
225 | // which will move the origin downward and/or left as necessary if the origin is | |
226 | // inside the screen0 rectangle (i.e. x/y >= 0 in wx coordinates) and outside the | |
227 | // visible frame (i.e. x/y < the top/left of the screen0 visible frame in wx coordinates) | |
228 | // We don't munge origin coordinates < 0 because it actually is possible that the menubar is on | |
229 | // the top of the bottom screen and thus that origin is completely valid! | |
230 | if(keepOriginVisible && (m_screenZero != nil)) | |
231 | { | |
232 | // Do al of this in wx coordinates because it's far simpler since we're dealing with top/left points | |
233 | wxPoint visibleOrigin = OriginInWxDisplayCoordinatesForRectInCocoaScreenCoordinates([m_screenZero visibleFrame]); | |
234 | if(x >= 0 && x < visibleOrigin.x) | |
235 | x = visibleOrigin.x; | |
236 | if(y >= 0 && y < visibleOrigin.y) | |
237 | y = visibleOrigin.y; | |
238 | } | |
239 | ||
240 | // The x coordinate is simple as it's just relative to screen zero's frame | |
241 | theCocoaOrigin.x = m_screenZeroFrame.origin.x + x; | |
242 | // Working in Cocoa's coordinates think to start at the bottom of screen zero's frame and add | |
243 | // the height of that rect which gives us the coordinate for the top of the visible rect. Now realize that | |
244 | // the wx coordinates are flipped so if y is say 10 then we want to be 10 pixels down from that and thus | |
245 | // we subtract y. But then we still need to take into account the size of the window which is h and subtract | |
246 | // that to get the bottom-left origin of the rectangle. | |
247 | theCocoaOrigin.y = m_screenZeroFrame.origin.y + m_screenZeroFrame.size.height - y - height; | |
248 | ||
249 | return theCocoaOrigin; | |
250 | } | |
251 | ||
252 | } // namespace | |
253 | ||
254 | wxPoint wxWindowCocoa::OriginInWxDisplayCoordinatesForRectInCocoaScreenCoordinates(NSRect windowFrame) | |
255 | { | |
256 | wxCocoaPrivateScreenCoordinateTransformer transformer; | |
257 | return transformer.OriginInWxDisplayCoordinatesForRectInCocoaScreenCoordinates(windowFrame); | |
258 | } | |
259 | ||
260 | NSPoint wxWindowCocoa::OriginInCocoaScreenCoordinatesForRectInWxDisplayCoordinates(wxCoord x, wxCoord y, wxCoord width, wxCoord height, bool keepOriginVisible) | |
261 | { | |
262 | wxCocoaPrivateScreenCoordinateTransformer transformer; | |
263 | return transformer.OriginInCocoaScreenCoordinatesForRectInWxDisplayCoordinates(x,y,width,height,keepOriginVisible); | |
264 | } | |
265 | ||
a82b8141 DE |
266 | // ======================================================================== |
267 | // wxWindowCocoaHider | |
268 | // ======================================================================== | |
269 | class wxWindowCocoaHider: protected wxCocoaNSView | |
270 | { | |
271 | DECLARE_NO_COPY_CLASS(wxWindowCocoaHider) | |
272 | public: | |
273 | wxWindowCocoaHider(wxWindow *owner); | |
274 | virtual ~wxWindowCocoaHider(); | |
275 | inline WX_NSView GetNSView() { return m_dummyNSView; } | |
276 | protected: | |
277 | wxWindowCocoa *m_owner; | |
278 | WX_NSView m_dummyNSView; | |
279 | virtual void Cocoa_FrameChanged(void); | |
7c5a378f | 280 | virtual void Cocoa_synthesizeMouseMoved(void) {} |
75e4856b DE |
281 | #ifdef WXCOCOA_FILL_DUMMY_VIEW |
282 | virtual bool Cocoa_drawRect(const NSRect& rect); | |
283 | #endif //def WXCOCOA_FILL_DUMMY_VIEW | |
a82b8141 DE |
284 | private: |
285 | wxWindowCocoaHider(); | |
286 | }; | |
287 | ||
816c52cf | 288 | // ======================================================================== |
f298b203 | 289 | // wxWindowCocoaScrollView |
816c52cf | 290 | // ======================================================================== |
f298b203 | 291 | class wxWindowCocoaScrollView: protected wxCocoaNSView |
816c52cf | 292 | { |
f298b203 | 293 | DECLARE_NO_COPY_CLASS(wxWindowCocoaScrollView) |
816c52cf | 294 | public: |
f298b203 DE |
295 | wxWindowCocoaScrollView(wxWindow *owner); |
296 | virtual ~wxWindowCocoaScrollView(); | |
816c52cf DE |
297 | inline WX_NSScrollView GetNSScrollView() { return m_cocoaNSScrollView; } |
298 | void ClientSizeToSize(int &width, int &height); | |
299 | void DoGetClientSize(int *x, int *y) const; | |
300 | void Encapsulate(); | |
301 | void Unencapsulate(); | |
302 | protected: | |
303 | wxWindowCocoa *m_owner; | |
304 | WX_NSScrollView m_cocoaNSScrollView; | |
305 | virtual void Cocoa_FrameChanged(void); | |
7c5a378f | 306 | virtual void Cocoa_synthesizeMouseMoved(void) {} |
816c52cf | 307 | private: |
f298b203 | 308 | wxWindowCocoaScrollView(); |
816c52cf DE |
309 | }; |
310 | ||
75e4856b DE |
311 | // ======================================================================== |
312 | // wxDummyNSView | |
313 | // ======================================================================== | |
314 | @interface wxDummyNSView : NSView | |
315 | - (NSView *)hitTest:(NSPoint)aPoint; | |
316 | @end | |
a24aa427 | 317 | WX_DECLARE_GET_OBJC_CLASS(wxDummyNSView,NSView) |
75e4856b DE |
318 | |
319 | @implementation wxDummyNSView : NSView | |
320 | - (NSView *)hitTest:(NSPoint)aPoint | |
321 | { | |
322 | return nil; | |
323 | } | |
324 | ||
325 | @end | |
a24aa427 | 326 | WX_IMPLEMENT_GET_OBJC_CLASS(wxDummyNSView,NSView) |
75e4856b | 327 | |
a82b8141 DE |
328 | // ======================================================================== |
329 | // wxWindowCocoaHider | |
330 | // ======================================================================== | |
331 | wxWindowCocoaHider::wxWindowCocoaHider(wxWindow *owner) | |
332 | : m_owner(owner) | |
333 | { | |
334 | wxASSERT(owner); | |
335 | wxASSERT(owner->GetNSViewForHiding()); | |
a24aa427 | 336 | m_dummyNSView = [[WX_GET_OBJC_CLASS(wxDummyNSView) alloc] |
a82b8141 | 337 | initWithFrame:[owner->GetNSViewForHiding() frame]]; |
75e4856b | 338 | [m_dummyNSView setAutoresizingMask: [owner->GetNSViewForHiding() autoresizingMask]]; |
a82b8141 DE |
339 | AssociateNSView(m_dummyNSView); |
340 | } | |
341 | ||
342 | wxWindowCocoaHider::~wxWindowCocoaHider() | |
343 | { | |
344 | DisassociateNSView(m_dummyNSView); | |
345 | [m_dummyNSView release]; | |
346 | } | |
347 | ||
348 | void wxWindowCocoaHider::Cocoa_FrameChanged(void) | |
349 | { | |
350 | // Keep the real window in synch with the dummy | |
351 | wxASSERT(m_dummyNSView); | |
352 | [m_owner->GetNSViewForHiding() setFrame:[m_dummyNSView frame]]; | |
353 | } | |
354 | ||
5558135c | 355 | |
75e4856b DE |
356 | #ifdef WXCOCOA_FILL_DUMMY_VIEW |
357 | bool wxWindowCocoaHider::Cocoa_drawRect(const NSRect& rect) | |
358 | { | |
359 | NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:rect]; | |
360 | [[NSColor greenColor] set]; | |
361 | [bezpath stroke]; | |
362 | [bezpath fill]; | |
363 | return true; | |
364 | } | |
365 | #endif //def WXCOCOA_FILL_DUMMY_VIEW | |
366 | ||
816c52cf DE |
367 | // ======================================================================== |
368 | // wxFlippedNSClipView | |
369 | // ======================================================================== | |
370 | @interface wxFlippedNSClipView : NSClipView | |
371 | - (BOOL)isFlipped; | |
372 | @end | |
a24aa427 | 373 | WX_DECLARE_GET_OBJC_CLASS(wxFlippedNSClipView,NSClipView) |
816c52cf DE |
374 | |
375 | @implementation wxFlippedNSClipView : NSClipView | |
376 | - (BOOL)isFlipped | |
377 | { | |
378 | return YES; | |
379 | } | |
380 | ||
381 | @end | |
a24aa427 | 382 | WX_IMPLEMENT_GET_OBJC_CLASS(wxFlippedNSClipView,NSClipView) |
816c52cf DE |
383 | |
384 | // ======================================================================== | |
f298b203 | 385 | // wxWindowCocoaScrollView |
816c52cf | 386 | // ======================================================================== |
f298b203 | 387 | wxWindowCocoaScrollView::wxWindowCocoaScrollView(wxWindow *owner) |
816c52cf DE |
388 | : m_owner(owner) |
389 | { | |
a9861c57 | 390 | wxAutoNSAutoreleasePool pool; |
816c52cf DE |
391 | wxASSERT(owner); |
392 | wxASSERT(owner->GetNSView()); | |
393 | m_cocoaNSScrollView = [[NSScrollView alloc] | |
394 | initWithFrame:[owner->GetNSView() frame]]; | |
395 | AssociateNSView(m_cocoaNSScrollView); | |
396 | ||
397 | /* Replace the default NSClipView with a flipped one. This ensures | |
398 | scrolling is "pinned" to the top-left instead of bottom-right. */ | |
a24aa427 | 399 | NSClipView *flippedClip = [[WX_GET_OBJC_CLASS(wxFlippedNSClipView) alloc] |
816c52cf DE |
400 | initWithFrame: [[m_cocoaNSScrollView contentView] frame]]; |
401 | [m_cocoaNSScrollView setContentView:flippedClip]; | |
402 | [flippedClip release]; | |
403 | ||
404 | [m_cocoaNSScrollView setBackgroundColor: [NSColor windowBackgroundColor]]; | |
405 | [m_cocoaNSScrollView setHasHorizontalScroller: YES]; | |
406 | [m_cocoaNSScrollView setHasVerticalScroller: YES]; | |
407 | Encapsulate(); | |
408 | } | |
409 | ||
f298b203 | 410 | void wxWindowCocoaScrollView::Encapsulate() |
816c52cf | 411 | { |
6f2ec3c3 DE |
412 | // Set the scroll view autoresizingMask to match the current NSView |
413 | [m_cocoaNSScrollView setAutoresizingMask: [m_owner->GetNSView() autoresizingMask]]; | |
414 | [m_owner->GetNSView() setAutoresizingMask: NSViewNotSizable]; | |
816c52cf DE |
415 | // NOTE: replaceSubView will cause m_cocaNSView to be released |
416 | // except when it hasn't been added into an NSView hierarchy in which | |
417 | // case it doesn't need to be and this should work out to a no-op | |
418 | m_owner->CocoaReplaceView(m_owner->GetNSView(), m_cocoaNSScrollView); | |
419 | // The NSView is still retained by owner | |
420 | [m_cocoaNSScrollView setDocumentView: m_owner->GetNSView()]; | |
421 | // Now it's also retained by the NSScrollView | |
422 | } | |
423 | ||
f298b203 | 424 | void wxWindowCocoaScrollView::Unencapsulate() |
816c52cf DE |
425 | { |
426 | [m_cocoaNSScrollView setDocumentView: nil]; | |
427 | m_owner->CocoaReplaceView(m_cocoaNSScrollView, m_owner->GetNSView()); | |
6f2ec3c3 DE |
428 | if(![[m_owner->GetNSView() superview] isFlipped]) |
429 | [m_owner->GetNSView() setAutoresizingMask: NSViewMinYMargin]; | |
816c52cf DE |
430 | } |
431 | ||
f298b203 | 432 | wxWindowCocoaScrollView::~wxWindowCocoaScrollView() |
816c52cf DE |
433 | { |
434 | DisassociateNSView(m_cocoaNSScrollView); | |
435 | [m_cocoaNSScrollView release]; | |
436 | } | |
437 | ||
f298b203 | 438 | void wxWindowCocoaScrollView::ClientSizeToSize(int &width, int &height) |
816c52cf DE |
439 | { |
440 | NSSize frameSize = [NSScrollView | |
441 | frameSizeForContentSize: NSMakeSize(width,height) | |
442 | hasHorizontalScroller: [m_cocoaNSScrollView hasHorizontalScroller] | |
443 | hasVerticalScroller: [m_cocoaNSScrollView hasVerticalScroller] | |
444 | borderType: [m_cocoaNSScrollView borderType]]; | |
e9cece45 VZ |
445 | width = (int)frameSize.width; |
446 | height = (int)frameSize.height; | |
816c52cf DE |
447 | } |
448 | ||
f298b203 | 449 | void wxWindowCocoaScrollView::DoGetClientSize(int *x, int *y) const |
816c52cf DE |
450 | { |
451 | NSSize nssize = [m_cocoaNSScrollView contentSize]; | |
452 | if(x) | |
e9cece45 | 453 | *x = (int)nssize.width; |
816c52cf | 454 | if(y) |
e9cece45 | 455 | *y = (int)nssize.height; |
816c52cf DE |
456 | } |
457 | ||
f298b203 | 458 | void wxWindowCocoaScrollView::Cocoa_FrameChanged(void) |
816c52cf | 459 | { |
48580976 | 460 | wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_FrameChanged")); |
816c52cf DE |
461 | wxSizeEvent event(m_owner->GetSize(), m_owner->GetId()); |
462 | event.SetEventObject(m_owner); | |
463 | m_owner->GetEventHandler()->ProcessEvent(event); | |
464 | } | |
465 | ||
a82b8141 DE |
466 | // ======================================================================== |
467 | // wxWindowCocoa | |
468 | // ======================================================================== | |
fb896a32 DE |
469 | // normally the base classes aren't included, but wxWindow is special |
470 | #ifdef __WXUNIVERSAL__ | |
471 | IMPLEMENT_ABSTRACT_CLASS(wxWindowCocoa, wxWindowBase) | |
472 | #else | |
473 | IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase) | |
474 | #endif | |
475 | ||
476 | BEGIN_EVENT_TABLE(wxWindowCocoa, wxWindowBase) | |
477 | END_EVENT_TABLE() | |
478 | ||
b9505233 DE |
479 | wxWindow *wxWindowCocoa::sm_capturedWindow = NULL; |
480 | ||
fb896a32 DE |
481 | // Constructor |
482 | void wxWindowCocoa::Init() | |
483 | { | |
fb896a32 | 484 | m_cocoaNSView = NULL; |
a82b8141 | 485 | m_cocoaHider = NULL; |
f298b203 | 486 | m_wxCocoaScrollView = NULL; |
8d8d3633 WS |
487 | m_isBeingDeleted = false; |
488 | m_isInPaint = false; | |
7c5a378f | 489 | m_visibleTrackingRectManager = NULL; |
fb896a32 DE |
490 | } |
491 | ||
492 | // Constructor | |
493 | bool wxWindow::Create(wxWindow *parent, wxWindowID winid, | |
494 | const wxPoint& pos, | |
495 | const wxSize& size, | |
496 | long style, | |
497 | const wxString& name) | |
498 | { | |
499 | if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name)) | |
500 | return false; | |
501 | ||
502 | // TODO: create the window | |
fb896a32 | 503 | m_cocoaNSView = NULL; |
a24aa427 | 504 | SetNSView([[WX_GET_OBJC_CLASS(WXNSView) alloc] initWithFrame: MakeDefaultNSRect(size)]); |
fb896a32 DE |
505 | [m_cocoaNSView release]; |
506 | ||
507 | if (m_parent) | |
508 | { | |
509 | m_parent->AddChild(this); | |
510 | m_parent->CocoaAddChild(this); | |
6d034f7d | 511 | SetInitialFrameRect(pos,size); |
fb896a32 DE |
512 | } |
513 | ||
8d8d3633 | 514 | return true; |
fb896a32 DE |
515 | } |
516 | ||
517 | // Destructor | |
518 | wxWindow::~wxWindow() | |
519 | { | |
7fc77f30 | 520 | wxAutoNSAutoreleasePool pool; |
fb896a32 DE |
521 | DestroyChildren(); |
522 | ||
065e208e | 523 | // Make sure our parent (in the wxWidgets sense) is our superview |
6ba13ca4 DE |
524 | // before we go removing from it. |
525 | if(m_parent && m_parent->GetNSView()==[GetNSViewForSuperview() superview]) | |
526 | CocoaRemoveFromParent(); | |
a82b8141 | 527 | delete m_cocoaHider; |
f298b203 | 528 | delete m_wxCocoaScrollView; |
9c85202a DE |
529 | if(m_cocoaNSView) |
530 | SendDestroyEvent(); | |
fb896a32 DE |
531 | SetNSView(NULL); |
532 | } | |
533 | ||
534 | void wxWindowCocoa::CocoaAddChild(wxWindowCocoa *child) | |
535 | { | |
b0a207df DE |
536 | // Pool here due to lack of one during wx init phase |
537 | wxAutoNSAutoreleasePool pool; | |
538 | ||
a82b8141 DE |
539 | NSView *childView = child->GetNSViewForSuperview(); |
540 | ||
541 | wxASSERT(childView); | |
542 | [m_cocoaNSView addSubview: childView]; | |
543 | child->m_isShown = !m_cocoaHider; | |
fb896a32 DE |
544 | } |
545 | ||
546 | void wxWindowCocoa::CocoaRemoveFromParent(void) | |
547 | { | |
a82b8141 | 548 | [GetNSViewForSuperview() removeFromSuperview]; |
fb896a32 DE |
549 | } |
550 | ||
551 | void wxWindowCocoa::SetNSView(WX_NSView cocoaNSView) | |
552 | { | |
7c5a378f DE |
553 | // Clear the visible area tracking rect if we have one. |
554 | delete m_visibleTrackingRectManager; | |
555 | m_visibleTrackingRectManager = NULL; | |
556 | ||
fb896a32 | 557 | bool need_debug = cocoaNSView || m_cocoaNSView; |
48580976 | 558 | if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxWindowCocoa=%p::SetNSView [m_cocoaNSView=%p retainCount]=%d"),this,m_cocoaNSView,[m_cocoaNSView retainCount]); |
bac6f234 | 559 | DisassociateNSView(m_cocoaNSView); |
fb896a32 DE |
560 | [cocoaNSView retain]; |
561 | [m_cocoaNSView release]; | |
562 | m_cocoaNSView = cocoaNSView; | |
bac6f234 | 563 | AssociateNSView(m_cocoaNSView); |
48580976 | 564 | if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxWindowCocoa=%p::SetNSView [cocoaNSView=%p retainCount]=%d"),this,cocoaNSView,[cocoaNSView retainCount]); |
fb896a32 DE |
565 | } |
566 | ||
a82b8141 DE |
567 | WX_NSView wxWindowCocoa::GetNSViewForSuperview() const |
568 | { | |
569 | return m_cocoaHider | |
570 | ? m_cocoaHider->GetNSView() | |
f298b203 DE |
571 | : m_wxCocoaScrollView |
572 | ? m_wxCocoaScrollView->GetNSScrollView() | |
816c52cf | 573 | : m_cocoaNSView; |
a82b8141 DE |
574 | } |
575 | ||
576 | WX_NSView wxWindowCocoa::GetNSViewForHiding() const | |
577 | { | |
f298b203 DE |
578 | return m_wxCocoaScrollView |
579 | ? m_wxCocoaScrollView->GetNSScrollView() | |
816c52cf | 580 | : m_cocoaNSView; |
a82b8141 DE |
581 | } |
582 | ||
34c9978d DE |
583 | NSPoint wxWindowCocoa::CocoaTransformBoundsToWx(NSPoint pointBounds) |
584 | { | |
585 | // TODO: Handle scrolling offset | |
ee022549 | 586 | return CocoaTransformNSViewBoundsToWx(GetNSView(), pointBounds); |
34c9978d DE |
587 | } |
588 | ||
589 | NSRect wxWindowCocoa::CocoaTransformBoundsToWx(NSRect rectBounds) | |
590 | { | |
591 | // TODO: Handle scrolling offset | |
ee022549 | 592 | return CocoaTransformNSViewBoundsToWx(GetNSView(), rectBounds); |
34c9978d DE |
593 | } |
594 | ||
595 | NSPoint wxWindowCocoa::CocoaTransformWxToBounds(NSPoint pointWx) | |
596 | { | |
597 | // TODO: Handle scrolling offset | |
ee022549 | 598 | return CocoaTransformNSViewWxToBounds(GetNSView(), pointWx); |
34c9978d DE |
599 | } |
600 | ||
601 | NSRect wxWindowCocoa::CocoaTransformWxToBounds(NSRect rectWx) | |
602 | { | |
603 | // TODO: Handle scrolling offset | |
ee022549 | 604 | return CocoaTransformNSViewWxToBounds(GetNSView(), rectWx); |
34c9978d DE |
605 | } |
606 | ||
4db3c8ac DE |
607 | WX_NSAffineTransform wxWindowCocoa::CocoaGetWxToBoundsTransform() |
608 | { | |
609 | // TODO: Handle scrolling offset | |
610 | NSAffineTransform *transform = wxDC::CocoaGetWxToBoundsTransform([GetNSView() isFlipped], [GetNSView() bounds].size.height); | |
611 | return transform; | |
612 | } | |
613 | ||
8ea5271e DE |
614 | bool wxWindowCocoa::Cocoa_drawRect(const NSRect &rect) |
615 | { | |
48580976 | 616 | wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_drawRect")); |
55c5be5e DE |
617 | // Recursion can happen if the event loop runs from within the paint |
618 | // handler. For instance, if an assertion dialog is shown. | |
619 | // FIXME: This seems less than ideal. | |
620 | if(m_isInPaint) | |
621 | { | |
2b030203 | 622 | wxLogDebug(wxT("Paint event recursion!")); |
55c5be5e DE |
623 | return false; |
624 | } | |
8d8d3633 | 625 | m_isInPaint = true; |
dc5bcaef DE |
626 | |
627 | // Set m_updateRegion | |
628 | const NSRect *rects = ▭ // The bounding box of the region | |
4799f3ba | 629 | NSInteger countRects = 1; |
dc5bcaef | 630 | // Try replacing the larger rectangle with a list of smaller ones: |
5dc47140 DE |
631 | if ([GetNSView() respondsToSelector:@selector(getRectsBeingDrawn:count:)]) |
632 | [GetNSView() getRectsBeingDrawn:&rects count:&countRects]; | |
34c9978d DE |
633 | |
634 | NSRect *transformedRects = (NSRect*)malloc(sizeof(NSRect)*countRects); | |
635 | for(int i=0; i<countRects; i++) | |
636 | { | |
637 | transformedRects[i] = CocoaTransformBoundsToWx(rects[i]); | |
638 | } | |
639 | m_updateRegion = wxRegion(transformedRects,countRects); | |
640 | free(transformedRects); | |
dc5bcaef | 641 | |
8ea5271e DE |
642 | wxPaintEvent event(m_windowId); |
643 | event.SetEventObject(this); | |
55c5be5e | 644 | bool ret = GetEventHandler()->ProcessEvent(event); |
8d8d3633 | 645 | m_isInPaint = false; |
55c5be5e | 646 | return ret; |
8ea5271e DE |
647 | } |
648 | ||
69dbb709 DE |
649 | void wxWindowCocoa::InitMouseEvent(wxMouseEvent& event, WX_NSEvent cocoaEvent) |
650 | { | |
2b030203 | 651 | wxASSERT_MSG([m_cocoaNSView window]==[cocoaEvent window],wxT("Mouse event for different NSWindow")); |
34c9978d DE |
652 | // Mouse events happen at the NSWindow level so we need to convert |
653 | // into our bounds coordinates then convert to wx coordinates. | |
a82b8141 | 654 | NSPoint cocoaPoint = [m_cocoaNSView convertPoint:[(NSEvent*)cocoaEvent locationInWindow] fromView:nil]; |
34c9978d DE |
655 | NSPoint pointWx = CocoaTransformBoundsToWx(cocoaPoint); |
656 | // FIXME: Should we be adjusting for client area origin? | |
69dbb709 | 657 | const wxPoint &clientorigin = GetClientAreaOrigin(); |
34c9978d DE |
658 | event.m_x = (wxCoord)pointWx.x - clientorigin.x; |
659 | event.m_y = (wxCoord)pointWx.y - clientorigin.y; | |
69dbb709 DE |
660 | |
661 | event.m_shiftDown = [cocoaEvent modifierFlags] & NSShiftKeyMask; | |
662 | event.m_controlDown = [cocoaEvent modifierFlags] & NSControlKeyMask; | |
663 | event.m_altDown = [cocoaEvent modifierFlags] & NSAlternateKeyMask; | |
664 | event.m_metaDown = [cocoaEvent modifierFlags] & NSCommandKeyMask; | |
665 | ||
666 | // TODO: set timestamp? | |
667 | event.SetEventObject(this); | |
668 | event.SetId(GetId()); | |
669 | } | |
670 | ||
671 | bool wxWindowCocoa::Cocoa_mouseMoved(WX_NSEvent theEvent) | |
672 | { | |
673 | wxMouseEvent event(wxEVT_MOTION); | |
674 | InitMouseEvent(event,theEvent); | |
7c5a378f | 675 | wxLogTrace(wxTRACE_COCOA,wxT("wxWindow=%p::Cocoa_mouseMoved @%d,%d"),this,event.m_x,event.m_y); |
69dbb709 DE |
676 | return GetEventHandler()->ProcessEvent(event); |
677 | } | |
678 | ||
7c5a378f DE |
679 | void wxWindowCocoa::Cocoa_synthesizeMouseMoved() |
680 | { | |
681 | wxMouseEvent event(wxEVT_MOTION); | |
682 | NSWindow *window = [GetNSView() window]; | |
683 | NSPoint locationInWindow = [window mouseLocationOutsideOfEventStream]; | |
684 | NSPoint cocoaPoint = [m_cocoaNSView convertPoint:locationInWindow fromView:nil]; | |
685 | ||
686 | NSPoint pointWx = CocoaTransformBoundsToWx(cocoaPoint); | |
687 | // FIXME: Should we be adjusting for client area origin? | |
688 | const wxPoint &clientorigin = GetClientAreaOrigin(); | |
689 | event.m_x = (wxCoord)pointWx.x - clientorigin.x; | |
690 | event.m_y = (wxCoord)pointWx.y - clientorigin.y; | |
691 | ||
692 | // TODO: Handle shift, control, alt, meta flags | |
693 | event.SetEventObject(this); | |
694 | event.SetId(GetId()); | |
695 | ||
696 | wxLogTrace(wxTRACE_COCOA,wxT("wxwin=%p Synthesized Mouse Moved @%d,%d"),this,event.m_x,event.m_y); | |
697 | GetEventHandler()->ProcessEvent(event); | |
698 | } | |
699 | ||
69dbb709 DE |
700 | bool wxWindowCocoa::Cocoa_mouseEntered(WX_NSEvent theEvent) |
701 | { | |
7c5a378f DE |
702 | if(m_visibleTrackingRectManager != NULL && m_visibleTrackingRectManager->IsOwnerOfEvent(theEvent)) |
703 | { | |
704 | m_visibleTrackingRectManager->BeginSynthesizingEvents(); | |
705 | ||
706 | // Although we synthesize the mouse moved events we don't poll for them but rather send them only when | |
707 | // some other event comes in. That other event is (guess what) mouse moved events that will be sent | |
708 | // to the NSWindow which will forward them on to the first responder. We are not likely to be the | |
709 | // first responder, so the mouseMoved: events are effectively discarded. | |
710 | [[GetNSView() window] setAcceptsMouseMovedEvents:YES]; | |
711 | ||
712 | wxMouseEvent event(wxEVT_ENTER_WINDOW); | |
713 | InitMouseEvent(event,theEvent); | |
714 | wxLogTrace(wxTRACE_COCOA,wxT("wxwin=%p Mouse Entered @%d,%d"),this,event.m_x,event.m_y); | |
715 | return GetEventHandler()->ProcessEvent(event); | |
716 | } | |
717 | else | |
718 | return false; | |
69dbb709 DE |
719 | } |
720 | ||
721 | bool wxWindowCocoa::Cocoa_mouseExited(WX_NSEvent theEvent) | |
722 | { | |
7c5a378f DE |
723 | if(m_visibleTrackingRectManager != NULL && m_visibleTrackingRectManager->IsOwnerOfEvent(theEvent)) |
724 | { | |
725 | m_visibleTrackingRectManager->StopSynthesizingEvents(); | |
726 | ||
727 | wxMouseEvent event(wxEVT_LEAVE_WINDOW); | |
728 | InitMouseEvent(event,theEvent); | |
729 | wxLogTrace(wxTRACE_COCOA,wxT("wxwin=%p Mouse Exited @%d,%d"),this,event.m_x,event.m_y); | |
730 | return GetEventHandler()->ProcessEvent(event); | |
731 | } | |
732 | else | |
733 | return false; | |
69dbb709 DE |
734 | } |
735 | ||
736 | bool wxWindowCocoa::Cocoa_mouseDown(WX_NSEvent theEvent) | |
737 | { | |
738 | wxMouseEvent event([theEvent clickCount]<2?wxEVT_LEFT_DOWN:wxEVT_LEFT_DCLICK); | |
739 | InitMouseEvent(event,theEvent); | |
48580976 | 740 | wxLogTrace(wxTRACE_COCOA,wxT("Mouse Down @%d,%d num clicks=%d"),event.m_x,event.m_y,[theEvent clickCount]); |
69dbb709 DE |
741 | return GetEventHandler()->ProcessEvent(event); |
742 | } | |
743 | ||
744 | bool wxWindowCocoa::Cocoa_mouseDragged(WX_NSEvent theEvent) | |
745 | { | |
746 | wxMouseEvent event(wxEVT_MOTION); | |
747 | InitMouseEvent(event,theEvent); | |
748 | event.m_leftDown = true; | |
48580976 | 749 | wxLogTrace(wxTRACE_COCOA,wxT("Mouse Drag @%d,%d"),event.m_x,event.m_y); |
69dbb709 DE |
750 | return GetEventHandler()->ProcessEvent(event); |
751 | } | |
752 | ||
753 | bool wxWindowCocoa::Cocoa_mouseUp(WX_NSEvent theEvent) | |
754 | { | |
755 | wxMouseEvent event(wxEVT_LEFT_UP); | |
756 | InitMouseEvent(event,theEvent); | |
48580976 | 757 | wxLogTrace(wxTRACE_COCOA,wxT("Mouse Up @%d,%d"),event.m_x,event.m_y); |
69dbb709 DE |
758 | return GetEventHandler()->ProcessEvent(event); |
759 | } | |
760 | ||
761 | bool wxWindowCocoa::Cocoa_rightMouseDown(WX_NSEvent theEvent) | |
762 | { | |
eafde5c7 DE |
763 | wxMouseEvent event([theEvent clickCount]<2?wxEVT_RIGHT_DOWN:wxEVT_RIGHT_DCLICK); |
764 | InitMouseEvent(event,theEvent); | |
765 | wxLogDebug(wxT("Mouse Down @%d,%d num clicks=%d"),event.m_x,event.m_y,[theEvent clickCount]); | |
766 | return GetEventHandler()->ProcessEvent(event); | |
69dbb709 DE |
767 | } |
768 | ||
769 | bool wxWindowCocoa::Cocoa_rightMouseDragged(WX_NSEvent theEvent) | |
770 | { | |
eafde5c7 DE |
771 | wxMouseEvent event(wxEVT_MOTION); |
772 | InitMouseEvent(event,theEvent); | |
773 | event.m_rightDown = true; | |
774 | wxLogDebug(wxT("Mouse Drag @%d,%d"),event.m_x,event.m_y); | |
775 | return GetEventHandler()->ProcessEvent(event); | |
69dbb709 DE |
776 | } |
777 | ||
778 | bool wxWindowCocoa::Cocoa_rightMouseUp(WX_NSEvent theEvent) | |
779 | { | |
eafde5c7 DE |
780 | wxMouseEvent event(wxEVT_RIGHT_UP); |
781 | InitMouseEvent(event,theEvent); | |
782 | wxLogDebug(wxT("Mouse Up @%d,%d"),event.m_x,event.m_y); | |
783 | return GetEventHandler()->ProcessEvent(event); | |
69dbb709 DE |
784 | } |
785 | ||
786 | bool wxWindowCocoa::Cocoa_otherMouseDown(WX_NSEvent theEvent) | |
787 | { | |
788 | return false; | |
789 | } | |
790 | ||
791 | bool wxWindowCocoa::Cocoa_otherMouseDragged(WX_NSEvent theEvent) | |
792 | { | |
793 | return false; | |
794 | } | |
795 | ||
796 | bool wxWindowCocoa::Cocoa_otherMouseUp(WX_NSEvent theEvent) | |
797 | { | |
798 | return false; | |
799 | } | |
800 | ||
fb896a32 DE |
801 | void wxWindowCocoa::Cocoa_FrameChanged(void) |
802 | { | |
7c5a378f DE |
803 | wxLogTrace(wxTRACE_COCOA,wxT("wxWindow=%p::Cocoa_FrameChanged"),this); |
804 | if(m_visibleTrackingRectManager != NULL) | |
805 | m_visibleTrackingRectManager->RebuildTrackingRect(); | |
fb896a32 DE |
806 | wxSizeEvent event(GetSize(), m_windowId); |
807 | event.SetEventObject(this); | |
808 | GetEventHandler()->ProcessEvent(event); | |
809 | } | |
810 | ||
5558135c RN |
811 | bool wxWindowCocoa::Cocoa_resetCursorRects() |
812 | { | |
7c5a378f DE |
813 | wxLogTrace(wxTRACE_COCOA,wxT("wxWindow=%p::Cocoa_resetCursorRects"),this); |
814 | if(m_visibleTrackingRectManager != NULL) | |
815 | m_visibleTrackingRectManager->RebuildTrackingRect(); | |
816 | ||
5558135c RN |
817 | if(!m_cursor.GetNSCursor()) |
818 | return false; | |
8d8d3633 WS |
819 | |
820 | [GetNSView() addCursorRect: [GetNSView() visibleRect] cursor: m_cursor.GetNSCursor()]; | |
821 | ||
5558135c RN |
822 | return true; |
823 | } | |
824 | ||
a8780ad5 DE |
825 | bool wxWindowCocoa::SetCursor(const wxCursor &cursor) |
826 | { | |
827 | if(!wxWindowBase::SetCursor(cursor)) | |
828 | return false; | |
829 | // Invalidate the cursor rects so the cursor will change | |
830 | [[GetNSView() window] invalidateCursorRectsForView:GetNSView()]; | |
831 | return true; | |
832 | } | |
833 | ||
7c5a378f DE |
834 | bool wxWindowCocoa::Cocoa_viewDidMoveToWindow() |
835 | { | |
836 | wxLogTrace(wxTRACE_COCOA,wxT("wxWindow=%p::viewDidMoveToWindow"),this); | |
837 | // Set up new tracking rects. I am reasonably sure the new window must be set before doing this. | |
838 | if(m_visibleTrackingRectManager != NULL) | |
839 | m_visibleTrackingRectManager->BuildTrackingRect(); | |
840 | return false; | |
841 | } | |
842 | ||
843 | bool wxWindowCocoa::Cocoa_viewWillMoveToWindow(WX_NSWindow newWindow) | |
844 | { | |
845 | wxLogTrace(wxTRACE_COCOA,wxT("wxWindow=%p::viewWillMoveToWindow:%p"),this, newWindow); | |
846 | // Clear tracking rects. It is imperative this be done before the new window is set. | |
847 | if(m_visibleTrackingRectManager != NULL) | |
848 | m_visibleTrackingRectManager->ClearTrackingRect(); | |
849 | return false; | |
850 | } | |
851 | ||
fb896a32 DE |
852 | bool wxWindow::Close(bool force) |
853 | { | |
cc6f960f DE |
854 | // The only reason this function exists is that it is virtual and |
855 | // wxTopLevelWindowCocoa will override it. | |
856 | return wxWindowBase::Close(force); | |
fb896a32 DE |
857 | } |
858 | ||
a82b8141 DE |
859 | void wxWindow::CocoaReplaceView(WX_NSView oldView, WX_NSView newView) |
860 | { | |
861 | [[oldView superview] replaceSubview:oldView with:newView]; | |
862 | } | |
863 | ||
47a8a4d5 | 864 | void wxWindow::DoEnable(bool enable) |
adb4816c | 865 | { |
47a8a4d5 | 866 | CocoaSetEnabled(enable); |
adb4816c DE |
867 | } |
868 | ||
fb896a32 DE |
869 | bool wxWindow::Show(bool show) |
870 | { | |
7fc77f30 | 871 | wxAutoNSAutoreleasePool pool; |
fb896a32 DE |
872 | // If the window is marked as visible, then it shouldn't have a dummy view |
873 | // If the window is marked hidden, then it should have a dummy view | |
addbdd29 | 874 | // wxSpinCtrl (generic) abuses m_isShown, don't use it for any logic |
2b030203 | 875 | // wxASSERT_MSG( (m_isShown && !m_dummyNSView) || (!m_isShown && m_dummyNSView),wxT("wxWindow: m_isShown does not agree with m_dummyNSView")); |
fb896a32 | 876 | // Return false if there isn't a window to show or hide |
a82b8141 DE |
877 | NSView *cocoaView = GetNSViewForHiding(); |
878 | if(!cocoaView) | |
fb896a32 | 879 | return false; |
fb896a32 DE |
880 | if(show) |
881 | { | |
addbdd29 | 882 | // If state isn't changing, return false |
a82b8141 | 883 | if(!m_cocoaHider) |
addbdd29 | 884 | return false; |
a82b8141 DE |
885 | CocoaReplaceView(m_cocoaHider->GetNSView(), cocoaView); |
886 | wxASSERT(![m_cocoaHider->GetNSView() superview]); | |
887 | delete m_cocoaHider; | |
888 | m_cocoaHider = NULL; | |
889 | wxASSERT([cocoaView superview]); | |
fb896a32 DE |
890 | } |
891 | else | |
892 | { | |
addbdd29 | 893 | // If state isn't changing, return false |
a82b8141 | 894 | if(m_cocoaHider) |
addbdd29 | 895 | return false; |
a82b8141 DE |
896 | m_cocoaHider = new wxWindowCocoaHider(this); |
897 | // NOTE: replaceSubview:with will cause m_cocaNSView to be | |
898 | // (auto)released which balances out addSubview | |
899 | CocoaReplaceView(cocoaView, m_cocoaHider->GetNSView()); | |
fb896a32 | 900 | // m_coocaNSView is now only retained by us |
a82b8141 DE |
901 | wxASSERT([m_cocoaHider->GetNSView() superview]); |
902 | wxASSERT(![cocoaView superview]); | |
fb896a32 | 903 | } |
a6b4ff2e DE |
904 | m_isShown = show; |
905 | return true; | |
fb896a32 DE |
906 | } |
907 | ||
908 | void wxWindowCocoa::DoSetSize(int x, int y, int width, int height, int sizeFlags) | |
909 | { | |
9879fa84 | 910 | wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoSetSizeWindow(%d,%d,%d,%d,Auto: %s%s)"),this,x,y,width,height,(sizeFlags&wxSIZE_AUTO_WIDTH)?"W":".",sizeFlags&wxSIZE_AUTO_HEIGHT?"H":"."); |
fb896a32 DE |
911 | int currentX, currentY; |
912 | int currentW, currentH; | |
913 | DoGetPosition(¤tX, ¤tY); | |
914 | DoGetSize(¤tW, ¤tH); | |
915 | if((x==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE)) | |
916 | x=currentX; | |
917 | if((y==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE)) | |
918 | y=currentY; | |
919 | ||
920 | AdjustForParentClientOrigin(x,y,sizeFlags); | |
921 | ||
8d8d3633 | 922 | wxSize size(wxDefaultSize); |
fb896a32 DE |
923 | |
924 | if((width==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE)) | |
925 | { | |
926 | if(sizeFlags&wxSIZE_AUTO_WIDTH) | |
927 | { | |
928 | size=DoGetBestSize(); | |
929 | width=size.x; | |
930 | } | |
931 | else | |
932 | width=currentW; | |
933 | } | |
934 | if((height==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE)) | |
935 | { | |
936 | if(sizeFlags&wxSIZE_AUTO_HEIGHT) | |
937 | { | |
938 | if(size.x==-1) | |
939 | size=DoGetBestSize(); | |
940 | height=size.y; | |
941 | } | |
942 | else | |
943 | height=currentH; | |
944 | } | |
945 | DoMoveWindow(x,y,width,height); | |
946 | } | |
947 | ||
1e151594 | 948 | #if wxUSE_TOOLTIPS |
26191790 RN |
949 | |
950 | void wxWindowCocoa::DoSetToolTip( wxToolTip *tip ) | |
951 | { | |
952 | wxWindowBase::DoSetToolTip(tip); | |
953 | ||
26191790 RN |
954 | if ( m_tooltip ) |
955 | { | |
956 | m_tooltip->SetWindow((wxWindow *)this); | |
26191790 RN |
957 | } |
958 | } | |
959 | ||
1e151594 RN |
960 | #endif |
961 | ||
fb896a32 DE |
962 | void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height) |
963 | { | |
bed6fe0c | 964 | wxAutoNSAutoreleasePool pool; |
9879fa84 | 965 | wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)"),this,x,y,width,height); |
fb896a32 | 966 | |
a82b8141 | 967 | NSView *nsview = GetNSViewForSuperview(); |
d449cf47 | 968 | NSView *superview = [nsview superview]; |
fb896a32 | 969 | |
34c9978d DE |
970 | wxCHECK_RET(GetParent(), wxT("Window can only be placed correctly when it has a parent")); |
971 | ||
972 | NSRect oldFrameRect = [nsview frame]; | |
973 | NSRect newFrameRect = GetParent()->CocoaTransformWxToBounds(NSMakeRect(x,y,width,height)); | |
974 | [nsview setFrame:newFrameRect]; | |
b915b805 | 975 | // Be sure to redraw the parent to reflect the changed position |
34c9978d DE |
976 | [superview setNeedsDisplayInRect:oldFrameRect]; |
977 | [superview setNeedsDisplayInRect:newFrameRect]; | |
fb896a32 DE |
978 | } |
979 | ||
d139c3a8 DE |
980 | void wxWindowCocoa::SetInitialFrameRect(const wxPoint& pos, const wxSize& size) |
981 | { | |
982 | NSView *nsview = GetNSViewForSuperview(); | |
983 | NSView *superview = [nsview superview]; | |
2b030203 | 984 | wxCHECK_RET(superview,wxT("NSView does not have a superview")); |
34c9978d | 985 | wxCHECK_RET(GetParent(), wxT("Window can only be placed correctly when it has a parent")); |
d139c3a8 DE |
986 | NSRect frameRect = [nsview frame]; |
987 | if(size.x!=-1) | |
988 | frameRect.size.width = size.x; | |
989 | if(size.y!=-1) | |
990 | frameRect.size.height = size.y; | |
991 | frameRect.origin.x = pos.x; | |
34c9978d | 992 | frameRect.origin.y = pos.y; |
c5bd9191 DE |
993 | // Tell Cocoa to change the margin between the bottom of the superview |
994 | // and the bottom of the control. Keeps the control pinned to the top | |
065e208e | 995 | // of its superview so that its position in the wxWidgets coordinate |
c5bd9191 DE |
996 | // system doesn't change. |
997 | if(![superview isFlipped]) | |
998 | [nsview setAutoresizingMask: NSViewMinYMargin]; | |
6f2ec3c3 DE |
999 | // MUST set the mask before setFrame: which can generate a size event |
1000 | // and cause a scroller to be added! | |
34c9978d | 1001 | frameRect = GetParent()->CocoaTransformWxToBounds(frameRect); |
6f2ec3c3 | 1002 | [nsview setFrame: frameRect]; |
d139c3a8 DE |
1003 | } |
1004 | ||
fb896a32 DE |
1005 | // Get total size |
1006 | void wxWindow::DoGetSize(int *w, int *h) const | |
1007 | { | |
a82b8141 | 1008 | NSRect cocoaRect = [GetNSViewForSuperview() frame]; |
fb896a32 DE |
1009 | if(w) |
1010 | *w=(int)cocoaRect.size.width; | |
1011 | if(h) | |
1012 | *h=(int)cocoaRect.size.height; | |
9879fa84 | 1013 | wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoGetSize = (%d,%d)"),this,(int)cocoaRect.size.width,(int)cocoaRect.size.height); |
fb896a32 DE |
1014 | } |
1015 | ||
1016 | void wxWindow::DoGetPosition(int *x, int *y) const | |
1017 | { | |
a82b8141 | 1018 | NSView *nsview = GetNSViewForSuperview(); |
fb896a32 | 1019 | |
576a1544 | 1020 | NSRect cocoaRect = [nsview frame]; |
34c9978d | 1021 | NSRect rectWx = GetParent()->CocoaTransformBoundsToWx(cocoaRect); |
fb896a32 | 1022 | if(x) |
34c9978d | 1023 | *x=(int)rectWx.origin.x; |
fb896a32 | 1024 | if(y) |
34c9978d | 1025 | *y=(int)rectWx.origin.y; |
9879fa84 | 1026 | wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoGetPosition = (%d,%d)"),this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y); |
fb896a32 DE |
1027 | } |
1028 | ||
1029 | WXWidget wxWindow::GetHandle() const | |
1030 | { | |
1031 | return m_cocoaNSView; | |
1032 | } | |
1033 | ||
f7e98dee RN |
1034 | wxWindow* wxWindow::GetWxWindow() const |
1035 | { | |
1036 | return (wxWindow*) this; | |
1037 | } | |
1038 | ||
ddf7346a DE |
1039 | void wxWindow::Refresh(bool eraseBack, const wxRect *rect) |
1040 | { | |
1041 | [m_cocoaNSView setNeedsDisplay:YES]; | |
1042 | } | |
1043 | ||
fb896a32 DE |
1044 | void wxWindow::SetFocus() |
1045 | { | |
67d2dac8 DE |
1046 | if([GetNSView() acceptsFirstResponder]) |
1047 | [[GetNSView() window] makeFirstResponder: GetNSView()]; | |
fb896a32 DE |
1048 | } |
1049 | ||
1050 | void wxWindow::DoCaptureMouse() | |
1051 | { | |
1052 | // TODO | |
b9505233 | 1053 | sm_capturedWindow = this; |
fb896a32 DE |
1054 | } |
1055 | ||
1056 | void wxWindow::DoReleaseMouse() | |
1057 | { | |
1058 | // TODO | |
b9505233 | 1059 | sm_capturedWindow = NULL; |
fb896a32 DE |
1060 | } |
1061 | ||
1062 | void wxWindow::DoScreenToClient(int *x, int *y) const | |
1063 | { | |
15f37147 DE |
1064 | // Point in cocoa screen coordinates: |
1065 | NSPoint cocoaScreenPoint = OriginInCocoaScreenCoordinatesForRectInWxDisplayCoordinates(x!=NULL?*x:0, y!=NULL?*y:0, 0, 0, false); | |
1066 | NSView *clientView = const_cast<wxWindow*>(this)->GetNSView(); | |
1067 | NSWindow *theWindow = [clientView window]; | |
1068 | ||
1069 | // Point in window's base coordinate system: | |
1070 | NSPoint windowPoint = [theWindow convertScreenToBase:cocoaScreenPoint]; | |
1071 | // Point in view's bounds coordinate system | |
1072 | NSPoint boundsPoint = [clientView convertPoint:windowPoint fromView:nil]; | |
1073 | // Point in wx client coordinates: | |
1074 | NSPoint theWxClientPoint = CocoaTransformNSViewBoundsToWx(clientView, boundsPoint); | |
1075 | if(x!=NULL) | |
1076 | *x = theWxClientPoint.x; | |
1077 | if(y!=NULL) | |
1078 | *y = theWxClientPoint.y; | |
fb896a32 DE |
1079 | } |
1080 | ||
1081 | void wxWindow::DoClientToScreen(int *x, int *y) const | |
1082 | { | |
15f37147 DE |
1083 | // Point in wx client coordinates |
1084 | NSPoint theWxClientPoint = NSMakePoint(x!=NULL?*x:0, y!=NULL?*y:0); | |
1085 | ||
1086 | NSView *clientView = const_cast<wxWindow*>(this)->GetNSView(); | |
1087 | ||
1088 | // Point in the view's bounds coordinate system | |
1089 | NSPoint boundsPoint = CocoaTransformNSViewWxToBounds(clientView, theWxClientPoint); | |
1090 | ||
1091 | // Point in the window's base coordinate system | |
1092 | NSPoint windowPoint = [clientView convertPoint:boundsPoint toView:nil]; | |
1093 | ||
1094 | NSWindow *theWindow = [clientView window]; | |
1095 | // Point in Cocoa's screen coordinates | |
1096 | NSPoint screenPoint = [theWindow convertBaseToScreen:windowPoint]; | |
1097 | ||
1098 | // Act as though this was the origin of a 0x0 rectangle | |
1099 | NSRect screenPointRect = NSMakeRect(screenPoint.x, screenPoint.y, 0, 0); | |
1100 | ||
1101 | // Convert that rectangle to wx coordinates | |
1102 | wxPoint theWxScreenPoint = OriginInWxDisplayCoordinatesForRectInCocoaScreenCoordinates(screenPointRect); | |
1103 | if(*x) | |
1104 | *x = theWxScreenPoint.x; | |
1105 | if(*y) | |
1106 | *y = theWxScreenPoint.y; | |
fb896a32 DE |
1107 | } |
1108 | ||
1109 | // Get size *available for subwindows* i.e. excluding menu bar etc. | |
1110 | void wxWindow::DoGetClientSize(int *x, int *y) const | |
1111 | { | |
48580976 | 1112 | wxLogTrace(wxTRACE_COCOA,wxT("DoGetClientSize:")); |
f298b203 DE |
1113 | if(m_wxCocoaScrollView) |
1114 | m_wxCocoaScrollView->DoGetClientSize(x,y); | |
816c52cf DE |
1115 | else |
1116 | wxWindowCocoa::DoGetSize(x,y); | |
fb896a32 DE |
1117 | } |
1118 | ||
1119 | void wxWindow::DoSetClientSize(int width, int height) | |
1120 | { | |
48580976 | 1121 | wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("DoSetClientSize=(%d,%d)"),width,height); |
f298b203 DE |
1122 | if(m_wxCocoaScrollView) |
1123 | m_wxCocoaScrollView->ClientSizeToSize(width,height); | |
e08efb8d DE |
1124 | CocoaSetWxWindowSize(width,height); |
1125 | } | |
1126 | ||
1127 | void wxWindow::CocoaSetWxWindowSize(int width, int height) | |
1128 | { | |
8d8d3633 WS |
1129 | wxWindowCocoa::DoSetSize(wxDefaultCoord,wxDefaultCoord,width,height,wxSIZE_USE_EXISTING); |
1130 | } | |
1131 | ||
1132 | void wxWindow::SetLabel(const wxString& WXUNUSED(label)) | |
1133 | { | |
ba64d0b6 | 1134 | // Intentional no-op. |
8d8d3633 WS |
1135 | } |
1136 | ||
1137 | wxString wxWindow::GetLabel() const | |
1138 | { | |
ba64d0b6 DE |
1139 | // General Get/Set of labels is implemented in wxControlBase |
1140 | wxLogDebug(wxT("wxWindow::GetLabel: Should be overridden if needed.")); | |
8d8d3633 | 1141 | return wxEmptyString; |
fb896a32 DE |
1142 | } |
1143 | ||
1144 | int wxWindow::GetCharHeight() const | |
1145 | { | |
1146 | // TODO | |
d89e391b | 1147 | return 10; |
fb896a32 DE |
1148 | } |
1149 | ||
1150 | int wxWindow::GetCharWidth() const | |
1151 | { | |
1152 | // TODO | |
d89e391b | 1153 | return 5; |
fb896a32 DE |
1154 | } |
1155 | ||
1156 | void wxWindow::GetTextExtent(const wxString& string, int *x, int *y, | |
1157 | int *descent, int *externalLeading, const wxFont *theFont) const | |
1158 | { | |
1159 | // TODO | |
1160 | } | |
1161 | ||
fb896a32 DE |
1162 | // Coordinates relative to the window |
1163 | void wxWindow::WarpPointer (int x_pos, int y_pos) | |
1164 | { | |
1165 | // TODO | |
1166 | } | |
1167 | ||
1168 | int wxWindow::GetScrollPos(int orient) const | |
1169 | { | |
1170 | // TODO | |
1171 | return 0; | |
1172 | } | |
1173 | ||
1174 | // This now returns the whole range, not just the number | |
1175 | // of positions that we can scroll. | |
1176 | int wxWindow::GetScrollRange(int orient) const | |
1177 | { | |
1178 | // TODO | |
1179 | return 0; | |
1180 | } | |
1181 | ||
1182 | int wxWindow::GetScrollThumb(int orient) const | |
1183 | { | |
1184 | // TODO | |
1185 | return 0; | |
1186 | } | |
1187 | ||
1188 | void wxWindow::SetScrollPos(int orient, int pos, bool refresh) | |
1189 | { | |
1190 | // TODO | |
1191 | } | |
1192 | ||
816c52cf DE |
1193 | void wxWindow::CocoaCreateNSScrollView() |
1194 | { | |
f298b203 | 1195 | if(!m_wxCocoaScrollView) |
816c52cf | 1196 | { |
f298b203 | 1197 | m_wxCocoaScrollView = new wxWindowCocoaScrollView(this); |
816c52cf DE |
1198 | } |
1199 | } | |
1200 | ||
fb896a32 DE |
1201 | // New function that will replace some of the above. |
1202 | void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible, | |
1203 | int range, bool refresh) | |
1204 | { | |
e08efb8d | 1205 | CocoaCreateNSScrollView(); |
fb896a32 DE |
1206 | // TODO |
1207 | } | |
1208 | ||
1209 | // Does a physical scroll | |
1210 | void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect) | |
1211 | { | |
1212 | // TODO | |
1213 | } | |
1214 | ||
816c52cf DE |
1215 | void wxWindow::DoSetVirtualSize( int x, int y ) |
1216 | { | |
1217 | wxWindowBase::DoSetVirtualSize(x,y); | |
1218 | CocoaCreateNSScrollView(); | |
1219 | [m_cocoaNSView setFrameSize:NSMakeSize(m_virtualSize.x,m_virtualSize.y)]; | |
1220 | } | |
1221 | ||
fb896a32 DE |
1222 | bool wxWindow::SetFont(const wxFont& font) |
1223 | { | |
e7e97a59 DE |
1224 | // FIXME: We may need to handle wx font inheritance. |
1225 | return wxWindowBase::SetFont(font); | |
fb896a32 DE |
1226 | } |
1227 | ||
aa25b7f9 DE |
1228 | #if 0 // these are used when debugging the algorithm. |
1229 | static char const * const comparisonresultStrings[] = | |
1230 | { "<" | |
1231 | , "==" | |
1232 | , ">" | |
1233 | }; | |
1234 | #endif | |
1235 | ||
1236 | class CocoaWindowCompareContext | |
1237 | { | |
1238 | DECLARE_NO_COPY_CLASS(CocoaWindowCompareContext) | |
1239 | public: | |
1240 | CocoaWindowCompareContext(); // Not implemented | |
1241 | CocoaWindowCompareContext(NSView *target, NSArray *subviews) | |
1242 | { | |
1243 | m_target = target; | |
1244 | // Cocoa sorts subviews in-place.. make a copy | |
1245 | m_subviews = [subviews copy]; | |
1246 | } | |
1247 | ~CocoaWindowCompareContext() | |
1248 | { // release the copy | |
1249 | [m_subviews release]; | |
1250 | } | |
1251 | NSView* target() | |
1252 | { return m_target; } | |
1253 | NSArray* subviews() | |
1254 | { return m_subviews; } | |
1255 | /* Helper function that returns the comparison based off of the original ordering */ | |
1256 | CocoaWindowCompareFunctionResult CompareUsingOriginalOrdering(id first, id second) | |
1257 | { | |
1258 | NSUInteger firstI = [m_subviews indexOfObjectIdenticalTo:first]; | |
1259 | NSUInteger secondI = [m_subviews indexOfObjectIdenticalTo:second]; | |
1260 | // NOTE: If either firstI or secondI is NSNotFound then it will be NSIntegerMax and thus will | |
1261 | // likely compare higher than the other view which is reasonable considering the only way that | |
1262 | // can happen is if the subview was added after our call to subviews but before the call to | |
1263 | // sortSubviewsUsingFunction:context:. Thus we don't bother checking. Particularly because | |
1264 | // that case should never occur anyway because that would imply a multi-threaded GUI call | |
1265 | // which is a big no-no with Cocoa. | |
1266 | ||
1267 | // Subviews are ordered from back to front meaning one that is already lower will have an lower index. | |
1268 | NSComparisonResult result = (firstI < secondI) | |
1269 | ? NSOrderedAscending /* -1 */ | |
1270 | : (firstI > secondI) | |
1271 | ? NSOrderedDescending /* 1 */ | |
1272 | : NSOrderedSame /* 0 */; | |
1273 | ||
1274 | #if 0 // Enable this if you need to debug the algorithm. | |
1275 | NSLog(@"%@ [%d] %s %@ [%d]\n", first, firstI, comparisonresultStrings[result+1], second, secondI); | |
1276 | #endif | |
1277 | return result; | |
1278 | } | |
1279 | private: | |
1280 | /* The subview we are trying to Raise or Lower */ | |
1281 | NSView *m_target; | |
1282 | /* A copy of the original array of subviews */ | |
1283 | NSArray *m_subviews; | |
1284 | }; | |
1285 | ||
1286 | /* Causes Cocoa to raise the target view to the top of the Z-Order by telling the sort function that | |
1287 | * the target view is always higher than every other view. When comparing two views neither of | |
1288 | * which is the target, it returns the correct response based on the original ordering | |
1289 | */ | |
1290 | static CocoaWindowCompareFunctionResult CocoaRaiseWindowCompareFunction(id first, id second, void *ctx) | |
4328a6ba | 1291 | { |
aa25b7f9 | 1292 | CocoaWindowCompareContext *compareContext = (CocoaWindowCompareContext*)ctx; |
4328a6ba | 1293 | // first should be ordered higher |
aa25b7f9 | 1294 | if(first==compareContext->target()) |
4328a6ba DE |
1295 | return NSOrderedDescending; |
1296 | // second should be ordered higher | |
aa25b7f9 | 1297 | if(second==compareContext->target()) |
4328a6ba | 1298 | return NSOrderedAscending; |
aa25b7f9 | 1299 | return compareContext->CompareUsingOriginalOrdering(first,second); |
4328a6ba DE |
1300 | } |
1301 | ||
fb896a32 DE |
1302 | // Raise the window to the top of the Z order |
1303 | void wxWindow::Raise() | |
1304 | { | |
4328a6ba | 1305 | // wxAutoNSAutoreleasePool pool; |
a82b8141 | 1306 | NSView *nsview = GetNSViewForSuperview(); |
aa25b7f9 DE |
1307 | NSView *superview = [nsview superview]; |
1308 | CocoaWindowCompareContext compareContext(nsview, [superview subviews]); | |
1309 | ||
1310 | [superview sortSubviewsUsingFunction: | |
4328a6ba | 1311 | CocoaRaiseWindowCompareFunction |
aa25b7f9 | 1312 | context: &compareContext]; |
4328a6ba DE |
1313 | } |
1314 | ||
aa25b7f9 DE |
1315 | /* Causes Cocoa to lower the target view to the bottom of the Z-Order by telling the sort function that |
1316 | * the target view is always lower than every other view. When comparing two views neither of | |
1317 | * which is the target, it returns the correct response based on the original ordering | |
1318 | */ | |
1319 | static CocoaWindowCompareFunctionResult CocoaLowerWindowCompareFunction(id first, id second, void *ctx) | |
4328a6ba | 1320 | { |
aa25b7f9 | 1321 | CocoaWindowCompareContext *compareContext = (CocoaWindowCompareContext*)ctx; |
4328a6ba | 1322 | // first should be ordered lower |
aa25b7f9 | 1323 | if(first==compareContext->target()) |
4328a6ba DE |
1324 | return NSOrderedAscending; |
1325 | // second should be ordered lower | |
aa25b7f9 | 1326 | if(second==compareContext->target()) |
4328a6ba | 1327 | return NSOrderedDescending; |
aa25b7f9 | 1328 | return compareContext->CompareUsingOriginalOrdering(first,second); |
fb896a32 DE |
1329 | } |
1330 | ||
1331 | // Lower the window to the bottom of the Z order | |
1332 | void wxWindow::Lower() | |
1333 | { | |
4328a6ba | 1334 | NSView *nsview = GetNSViewForSuperview(); |
aa25b7f9 DE |
1335 | NSView *superview = [nsview superview]; |
1336 | CocoaWindowCompareContext compareContext(nsview, [superview subviews]); | |
1337 | ||
1338 | #if 0 | |
1339 | NSLog(@"Target:\n%@\n", nsview); | |
1340 | NSLog(@"Before:\n%@\n", compareContext.subviews()); | |
1341 | #endif | |
1342 | [superview sortSubviewsUsingFunction: | |
4328a6ba | 1343 | CocoaLowerWindowCompareFunction |
aa25b7f9 DE |
1344 | context: &compareContext]; |
1345 | #if 0 | |
1346 | NSLog(@"After:\n%@\n", [superview subviews]); | |
1347 | #endif | |
fb896a32 DE |
1348 | } |
1349 | ||
1350 | bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y) | |
1351 | { | |
8d8d3633 | 1352 | return false; |
fb896a32 DE |
1353 | } |
1354 | ||
1355 | // Get the window with the focus | |
dcb68102 | 1356 | wxWindow *wxWindowBase::DoFindFocus() |
fb896a32 | 1357 | { |
67d2dac8 DE |
1358 | // Basically we are somewhat emulating the responder chain here except |
1359 | // we are only loking for the first responder in the key window or | |
1360 | // upon failing to find one if the main window is different we look | |
1361 | // for the first responder in the main window. | |
1362 | ||
1363 | // Note that the firstResponder doesn't necessarily have to be an | |
1364 | // NSView but wxCocoaNSView::GetFromCocoa() will simply return | |
1365 | // NULL unless it finds its argument in its hash map. | |
1366 | ||
1367 | wxCocoaNSView *win; | |
1368 | ||
1369 | NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow]; | |
9151dcec | 1370 | win = wxCocoaNSView::GetFromCocoa(static_cast<NSView*>([keyWindow firstResponder])); |
67d2dac8 DE |
1371 | if(win) |
1372 | return win->GetWxWindow(); | |
1373 | ||
1374 | NSWindow *mainWindow = [[NSApplication sharedApplication] keyWindow]; | |
1375 | if(mainWindow == keyWindow) | |
f7e98dee | 1376 | return NULL; |
9151dcec | 1377 | win = wxCocoaNSView::GetFromCocoa(static_cast<NSView*>([mainWindow firstResponder])); |
67d2dac8 DE |
1378 | if(win) |
1379 | return win->GetWxWindow(); | |
1380 | ||
1381 | return NULL; | |
fb896a32 DE |
1382 | } |
1383 | ||
1384 | /* static */ wxWindow *wxWindowBase::GetCapture() | |
1385 | { | |
1386 | // TODO | |
b9505233 | 1387 | return wxWindowCocoa::sm_capturedWindow; |
fb896a32 DE |
1388 | } |
1389 | ||
1390 | wxWindow *wxGetActiveWindow() | |
1391 | { | |
1392 | // TODO | |
1393 | return NULL; | |
1394 | } | |
1395 | ||
7c9428ab DE |
1396 | wxPoint wxGetMousePosition() |
1397 | { | |
1398 | // TODO | |
1399 | return wxDefaultPosition; | |
1400 | } | |
1401 | ||
ca5db7b2 WS |
1402 | wxMouseState wxGetMouseState() |
1403 | { | |
1404 | wxMouseState ms; | |
1405 | // TODO | |
1406 | return ms; | |
1407 | } | |
1408 | ||
7c9428ab DE |
1409 | wxWindow* wxFindWindowAtPointer(wxPoint& pt) |
1410 | { | |
1411 | pt = wxGetMousePosition(); | |
1412 | return NULL; | |
1413 | } | |
7c5a378f DE |
1414 | |
1415 | ||
8ea3a63e DE |
1416 | // ======================================================================== |
1417 | // wxCocoaMouseMovedEventSynthesizer | |
1418 | // ======================================================================== | |
1419 | ||
1420 | /* This class registers one run loop observer to cover all windows registered with it. | |
1421 | * It will register the observer when the first view is registerd and unregister the | |
1422 | * observer when the last view is unregistered. | |
1423 | * It is instantiated as a static s_mouseMovedSynthesizer in this file although there | |
1424 | * is no reason it couldn't be instantiated multiple times. | |
1425 | */ | |
1426 | class wxCocoaMouseMovedEventSynthesizer | |
1427 | { | |
1428 | DECLARE_NO_COPY_CLASS(wxCocoaMouseMovedEventSynthesizer) | |
1429 | public: | |
1430 | wxCocoaMouseMovedEventSynthesizer() | |
1431 | { m_lastScreenMouseLocation = NSZeroPoint; | |
1432 | } | |
1433 | ~wxCocoaMouseMovedEventSynthesizer(); | |
1434 | void RegisterWxCocoaView(wxCocoaNSView *aView); | |
1435 | void UnregisterWxCocoaView(wxCocoaNSView *aView); | |
1436 | void SynthesizeMouseMovedEvent(); | |
1437 | ||
1438 | protected: | |
1439 | void AddRunLoopObserver(); | |
1440 | void RemoveRunLoopObserver(); | |
1441 | wxCFRef<CFRunLoopObserverRef> m_runLoopObserver; | |
1442 | std::list<wxCocoaNSView*> m_registeredViews; | |
1443 | NSPoint m_lastScreenMouseLocation; | |
1444 | static void SynthesizeMouseMovedEvent(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info); | |
1445 | }; | |
1446 | ||
1447 | void wxCocoaMouseMovedEventSynthesizer::RegisterWxCocoaView(wxCocoaNSView *aView) | |
1448 | { | |
1449 | m_registeredViews.push_back(aView); | |
1450 | wxLogTrace(wxTRACE_COCOA_TrackingRect, wxT("Registered wxCocoaNSView=%p"), aView); | |
1451 | ||
1452 | if(!m_registeredViews.empty() && m_runLoopObserver == NULL) | |
1453 | { | |
1454 | AddRunLoopObserver(); | |
1455 | } | |
1456 | } | |
1457 | ||
1458 | void wxCocoaMouseMovedEventSynthesizer::UnregisterWxCocoaView(wxCocoaNSView *aView) | |
1459 | { | |
1460 | m_registeredViews.remove(aView); | |
1461 | wxLogTrace(wxTRACE_COCOA_TrackingRect, wxT("Unregistered wxCocoaNSView=%p"), aView); | |
1462 | if(m_registeredViews.empty() && m_runLoopObserver != NULL) | |
1463 | { | |
1464 | RemoveRunLoopObserver(); | |
1465 | } | |
1466 | } | |
1467 | ||
1468 | wxCocoaMouseMovedEventSynthesizer::~wxCocoaMouseMovedEventSynthesizer() | |
1469 | { | |
1470 | if(!m_registeredViews.empty()) | |
1471 | { | |
1472 | // This means failure to clean up so we report on it as a debug message. | |
1473 | wxLogDebug(wxT("There are still %d wxCocoaNSView registered to receive mouse moved events at static destruction time"), m_registeredViews.size()); | |
1474 | m_registeredViews.clear(); | |
1475 | } | |
1476 | if(m_runLoopObserver != NULL) | |
1477 | { | |
1478 | // This should not occur unless m_registeredViews was not empty since the last object unregistered should have done this. | |
1479 | wxLogDebug(wxT("Removing run loop observer during static destruction time.")); | |
1480 | RemoveRunLoopObserver(); | |
1481 | } | |
1482 | } | |
1483 | ||
1484 | void wxCocoaMouseMovedEventSynthesizer::SynthesizeMouseMovedEvent(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info) | |
1485 | { | |
1486 | reinterpret_cast<wxCocoaMouseMovedEventSynthesizer*>(info)->SynthesizeMouseMovedEvent(); | |
1487 | } | |
1488 | ||
1489 | void wxCocoaMouseMovedEventSynthesizer::AddRunLoopObserver() | |
1490 | { | |
1491 | CFRunLoopObserverContext observerContext = | |
1492 | { 0 | |
1493 | , this | |
1494 | , NULL | |
1495 | , NULL | |
1496 | , NULL | |
1497 | }; | |
1498 | m_runLoopObserver.reset(CFRunLoopObserverCreate(kCFAllocatorDefault, kCFRunLoopBeforeWaiting, TRUE, 0, SynthesizeMouseMovedEvent, &observerContext)); | |
1499 | CFRunLoopAddObserver([[NSRunLoop currentRunLoop] getCFRunLoop], m_runLoopObserver, kCFRunLoopCommonModes); | |
1500 | wxLogTrace(wxTRACE_COCOA_TrackingRect, wxT("Added tracking rect run loop observer")); | |
1501 | } | |
1502 | ||
1503 | void wxCocoaMouseMovedEventSynthesizer::RemoveRunLoopObserver() | |
1504 | { | |
1505 | CFRunLoopRemoveObserver([[NSRunLoop currentRunLoop] getCFRunLoop], m_runLoopObserver, kCFRunLoopCommonModes); | |
1506 | m_runLoopObserver.reset(); | |
1507 | wxLogTrace(wxTRACE_COCOA_TrackingRect, wxT("Removed tracking rect run loop observer")); | |
1508 | } | |
1509 | ||
1510 | void wxCocoaMouseMovedEventSynthesizer::SynthesizeMouseMovedEvent() | |
1511 | { | |
1512 | NSPoint screenMouseLocation = [NSEvent mouseLocation]; | |
1513 | // Checking the last mouse location is done for a few reasons: | |
1514 | // 1. We are observing every iteration of the event loop so we'd be sending out a lot of extraneous events | |
1515 | // telling the app the mouse moved when the user hit a key for instance. | |
1516 | // 2. When handling the mouse moved event, user code can do something to the view which will cause Cocoa to | |
1517 | // call resetCursorRects. Cocoa does this by using a delayed notification which means the event loop gets | |
1518 | // pumped once which would mean that if we didn't check the mouse location we'd get into a never-ending | |
1519 | // loop causing the tracking rectangles to constantly be reset. | |
1520 | if(screenMouseLocation.x != m_lastScreenMouseLocation.x || screenMouseLocation.y != m_lastScreenMouseLocation.y) | |
1521 | { | |
1522 | m_lastScreenMouseLocation = screenMouseLocation; | |
1523 | wxLogTrace(wxTRACE_COCOA_TrackingRect, wxT("Synthesizing mouse moved at screen (%f,%f)"), screenMouseLocation.x, screenMouseLocation.y); | |
1524 | for(std::list<wxCocoaNSView*>::iterator i = m_registeredViews.begin(); i != m_registeredViews.end(); ++i) | |
1525 | { | |
1526 | (*i)->Cocoa_synthesizeMouseMoved(); | |
1527 | } | |
1528 | } | |
1529 | } | |
1530 | ||
1531 | // Singleton used for all views: | |
1532 | static wxCocoaMouseMovedEventSynthesizer s_mouseMovedSynthesizer; | |
1533 | ||
7c5a378f DE |
1534 | // ======================================================================== |
1535 | // wxCocoaTrackingRectManager | |
1536 | // ======================================================================== | |
1537 | ||
1538 | wxCocoaTrackingRectManager::wxCocoaTrackingRectManager(wxWindow *window) | |
1539 | : m_window(window) | |
1540 | { | |
1541 | m_isTrackingRectActive = false; | |
7c5a378f DE |
1542 | BuildTrackingRect(); |
1543 | } | |
1544 | ||
1545 | void wxCocoaTrackingRectManager::ClearTrackingRect() | |
1546 | { | |
1547 | if(m_isTrackingRectActive) | |
1548 | { | |
1549 | [m_window->GetNSView() removeTrackingRect:m_trackingRectTag]; | |
1550 | m_isTrackingRectActive = false; | |
1551 | } | |
1552 | // If we were doing periodic events we need to clear those too | |
1553 | StopSynthesizingEvents(); | |
1554 | } | |
1555 | ||
1556 | void wxCocoaTrackingRectManager::StopSynthesizingEvents() | |
1557 | { | |
8ea3a63e | 1558 | s_mouseMovedSynthesizer.UnregisterWxCocoaView(m_window); |
7c5a378f DE |
1559 | } |
1560 | ||
1561 | void wxCocoaTrackingRectManager::BuildTrackingRect() | |
1562 | { | |
b0a207df DE |
1563 | // Pool here due to lack of one during wx init phase |
1564 | wxAutoNSAutoreleasePool pool; | |
1565 | ||
7c5a378f DE |
1566 | wxASSERT_MSG(!m_isTrackingRectActive, wxT("Tracking rect was not cleared")); |
1567 | if([m_window->GetNSView() window] != nil) | |
1568 | { | |
1569 | m_trackingRectTag = [m_window->GetNSView() addTrackingRect:[m_window->GetNSView() visibleRect] owner:m_window->GetNSView() userData:NULL assumeInside:NO]; | |
1570 | m_isTrackingRectActive = true; | |
1571 | } | |
1572 | } | |
1573 | ||
7c5a378f DE |
1574 | void wxCocoaTrackingRectManager::BeginSynthesizingEvents() |
1575 | { | |
8ea3a63e | 1576 | s_mouseMovedSynthesizer.RegisterWxCocoaView(m_window); |
7c5a378f DE |
1577 | } |
1578 | ||
1579 | void wxCocoaTrackingRectManager::RebuildTrackingRect() | |
1580 | { | |
1581 | ClearTrackingRect(); | |
1582 | BuildTrackingRect(); | |
1583 | } | |
1584 | ||
1585 | wxCocoaTrackingRectManager::~wxCocoaTrackingRectManager() | |
1586 | { | |
1587 | ClearTrackingRect(); | |
1588 | } | |
1589 | ||
1590 | bool wxCocoaTrackingRectManager::IsOwnerOfEvent(NSEvent *anEvent) | |
1591 | { | |
1592 | return m_isTrackingRectActive && (m_trackingRectTag == [anEvent trackingNumber]); | |
1593 | } | |
1594 |