]>
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" |
7fc77f30 | 26 | |
369559ca | 27 | #import <Foundation/NSArray.h> |
7c5a378f | 28 | #import <Foundation/NSRunLoop.h> |
829a2e95 | 29 | #include "wx/cocoa/objc/NSView.h" |
69dbb709 | 30 | #import <AppKit/NSEvent.h> |
816c52cf DE |
31 | #import <AppKit/NSScrollView.h> |
32 | #import <AppKit/NSColor.h> | |
33 | #import <AppKit/NSClipView.h> | |
dc5bcaef | 34 | #import <Foundation/NSException.h> |
67d2dac8 DE |
35 | #import <AppKit/NSApplication.h> |
36 | #import <AppKit/NSWindow.h> | |
dc5bcaef | 37 | |
75e4856b DE |
38 | // Turn this on to paint green over the dummy views for debugging |
39 | #undef WXCOCOA_FILL_DUMMY_VIEW | |
40 | ||
41 | #ifdef WXCOCOA_FILL_DUMMY_VIEW | |
42 | #import <AppKit/NSBezierPath.h> | |
43 | #endif //def WXCOCOA_FILL_DUMMY_VIEW | |
44 | ||
4799f3ba DE |
45 | /* NSComparisonResult is typedef'd as an enum pre-Leopard but typedef'd as |
46 | * NSInteger post-Leopard. Pre-Leopard the Cocoa toolkit expects a function | |
47 | * returning int and not NSComparisonResult. Post-Leopard the Cocoa toolkit | |
48 | * expects a function returning the new non-enum NSComparsionResult. | |
49 | * Hence we create a typedef named CocoaWindowCompareFunctionResult. | |
50 | */ | |
51 | #if defined(NSINTEGER_DEFINED) | |
52 | typedef NSComparisonResult CocoaWindowCompareFunctionResult; | |
53 | #else | |
54 | typedef int CocoaWindowCompareFunctionResult; | |
55 | #endif | |
56 | ||
5dc47140 DE |
57 | // A category for methods that are only present in Panther's SDK |
58 | @interface NSView(wxNSViewPrePantherCompatibility) | |
59 | - (void)getRectsBeingDrawn:(const NSRect **)rects count:(int *)count; | |
60 | @end | |
61 | ||
ee022549 DE |
62 | NSPoint CocoaTransformNSViewBoundsToWx(NSView *nsview, NSPoint pointBounds) |
63 | { | |
64 | wxCHECK_MSG(nsview, pointBounds, wxT("Need to have a Cocoa view to do translation")); | |
65 | if([nsview isFlipped]) | |
66 | return pointBounds; | |
67 | NSRect ourBounds = [nsview bounds]; | |
68 | return NSMakePoint | |
69 | ( pointBounds.x | |
70 | , ourBounds.size.height - pointBounds.y | |
71 | ); | |
72 | } | |
73 | ||
74 | NSRect CocoaTransformNSViewBoundsToWx(NSView *nsview, NSRect rectBounds) | |
75 | { | |
76 | wxCHECK_MSG(nsview, rectBounds, wxT("Need to have a Cocoa view to do translation")); | |
77 | if([nsview isFlipped]) | |
78 | return rectBounds; | |
79 | NSRect ourBounds = [nsview bounds]; | |
80 | return NSMakeRect | |
81 | ( rectBounds.origin.x | |
82 | , ourBounds.size.height - (rectBounds.origin.y + rectBounds.size.height) | |
83 | , rectBounds.size.width | |
84 | , rectBounds.size.height | |
85 | ); | |
86 | } | |
87 | ||
88 | NSPoint CocoaTransformNSViewWxToBounds(NSView *nsview, NSPoint pointWx) | |
89 | { | |
90 | wxCHECK_MSG(nsview, pointWx, wxT("Need to have a Cocoa view to do translation")); | |
91 | if([nsview isFlipped]) | |
92 | return pointWx; | |
93 | NSRect ourBounds = [nsview bounds]; | |
94 | return NSMakePoint | |
95 | ( pointWx.x | |
96 | , ourBounds.size.height - pointWx.y | |
97 | ); | |
98 | } | |
99 | ||
100 | NSRect CocoaTransformNSViewWxToBounds(NSView *nsview, NSRect rectWx) | |
101 | { | |
102 | wxCHECK_MSG(nsview, rectWx, wxT("Need to have a Cocoa view to do translation")); | |
103 | if([nsview isFlipped]) | |
104 | return rectWx; | |
105 | NSRect ourBounds = [nsview bounds]; | |
106 | return NSMakeRect | |
107 | ( rectWx.origin.x | |
108 | , ourBounds.size.height - (rectWx.origin.y + rectWx.size.height) | |
109 | , rectWx.size.width | |
110 | , rectWx.size.height | |
111 | ); | |
112 | } | |
113 | ||
a82b8141 DE |
114 | // ======================================================================== |
115 | // wxWindowCocoaHider | |
116 | // ======================================================================== | |
117 | class wxWindowCocoaHider: protected wxCocoaNSView | |
118 | { | |
119 | DECLARE_NO_COPY_CLASS(wxWindowCocoaHider) | |
120 | public: | |
121 | wxWindowCocoaHider(wxWindow *owner); | |
122 | virtual ~wxWindowCocoaHider(); | |
123 | inline WX_NSView GetNSView() { return m_dummyNSView; } | |
124 | protected: | |
125 | wxWindowCocoa *m_owner; | |
126 | WX_NSView m_dummyNSView; | |
127 | virtual void Cocoa_FrameChanged(void); | |
7c5a378f | 128 | virtual void Cocoa_synthesizeMouseMoved(void) {} |
75e4856b DE |
129 | #ifdef WXCOCOA_FILL_DUMMY_VIEW |
130 | virtual bool Cocoa_drawRect(const NSRect& rect); | |
131 | #endif //def WXCOCOA_FILL_DUMMY_VIEW | |
a82b8141 DE |
132 | private: |
133 | wxWindowCocoaHider(); | |
134 | }; | |
135 | ||
816c52cf | 136 | // ======================================================================== |
f298b203 | 137 | // wxWindowCocoaScrollView |
816c52cf | 138 | // ======================================================================== |
f298b203 | 139 | class wxWindowCocoaScrollView: protected wxCocoaNSView |
816c52cf | 140 | { |
f298b203 | 141 | DECLARE_NO_COPY_CLASS(wxWindowCocoaScrollView) |
816c52cf | 142 | public: |
f298b203 DE |
143 | wxWindowCocoaScrollView(wxWindow *owner); |
144 | virtual ~wxWindowCocoaScrollView(); | |
816c52cf DE |
145 | inline WX_NSScrollView GetNSScrollView() { return m_cocoaNSScrollView; } |
146 | void ClientSizeToSize(int &width, int &height); | |
147 | void DoGetClientSize(int *x, int *y) const; | |
148 | void Encapsulate(); | |
149 | void Unencapsulate(); | |
150 | protected: | |
151 | wxWindowCocoa *m_owner; | |
152 | WX_NSScrollView m_cocoaNSScrollView; | |
153 | virtual void Cocoa_FrameChanged(void); | |
7c5a378f | 154 | virtual void Cocoa_synthesizeMouseMoved(void) {} |
816c52cf | 155 | private: |
f298b203 | 156 | wxWindowCocoaScrollView(); |
816c52cf DE |
157 | }; |
158 | ||
75e4856b DE |
159 | // ======================================================================== |
160 | // wxDummyNSView | |
161 | // ======================================================================== | |
162 | @interface wxDummyNSView : NSView | |
163 | - (NSView *)hitTest:(NSPoint)aPoint; | |
164 | @end | |
a24aa427 | 165 | WX_DECLARE_GET_OBJC_CLASS(wxDummyNSView,NSView) |
75e4856b DE |
166 | |
167 | @implementation wxDummyNSView : NSView | |
168 | - (NSView *)hitTest:(NSPoint)aPoint | |
169 | { | |
170 | return nil; | |
171 | } | |
172 | ||
173 | @end | |
a24aa427 | 174 | WX_IMPLEMENT_GET_OBJC_CLASS(wxDummyNSView,NSView) |
75e4856b | 175 | |
a82b8141 DE |
176 | // ======================================================================== |
177 | // wxWindowCocoaHider | |
178 | // ======================================================================== | |
179 | wxWindowCocoaHider::wxWindowCocoaHider(wxWindow *owner) | |
180 | : m_owner(owner) | |
181 | { | |
182 | wxASSERT(owner); | |
183 | wxASSERT(owner->GetNSViewForHiding()); | |
a24aa427 | 184 | m_dummyNSView = [[WX_GET_OBJC_CLASS(wxDummyNSView) alloc] |
a82b8141 | 185 | initWithFrame:[owner->GetNSViewForHiding() frame]]; |
75e4856b | 186 | [m_dummyNSView setAutoresizingMask: [owner->GetNSViewForHiding() autoresizingMask]]; |
a82b8141 DE |
187 | AssociateNSView(m_dummyNSView); |
188 | } | |
189 | ||
190 | wxWindowCocoaHider::~wxWindowCocoaHider() | |
191 | { | |
192 | DisassociateNSView(m_dummyNSView); | |
193 | [m_dummyNSView release]; | |
194 | } | |
195 | ||
196 | void wxWindowCocoaHider::Cocoa_FrameChanged(void) | |
197 | { | |
198 | // Keep the real window in synch with the dummy | |
199 | wxASSERT(m_dummyNSView); | |
200 | [m_owner->GetNSViewForHiding() setFrame:[m_dummyNSView frame]]; | |
201 | } | |
202 | ||
5558135c | 203 | |
75e4856b DE |
204 | #ifdef WXCOCOA_FILL_DUMMY_VIEW |
205 | bool wxWindowCocoaHider::Cocoa_drawRect(const NSRect& rect) | |
206 | { | |
207 | NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:rect]; | |
208 | [[NSColor greenColor] set]; | |
209 | [bezpath stroke]; | |
210 | [bezpath fill]; | |
211 | return true; | |
212 | } | |
213 | #endif //def WXCOCOA_FILL_DUMMY_VIEW | |
214 | ||
816c52cf DE |
215 | // ======================================================================== |
216 | // wxFlippedNSClipView | |
217 | // ======================================================================== | |
218 | @interface wxFlippedNSClipView : NSClipView | |
219 | - (BOOL)isFlipped; | |
220 | @end | |
a24aa427 | 221 | WX_DECLARE_GET_OBJC_CLASS(wxFlippedNSClipView,NSClipView) |
816c52cf DE |
222 | |
223 | @implementation wxFlippedNSClipView : NSClipView | |
224 | - (BOOL)isFlipped | |
225 | { | |
226 | return YES; | |
227 | } | |
228 | ||
229 | @end | |
a24aa427 | 230 | WX_IMPLEMENT_GET_OBJC_CLASS(wxFlippedNSClipView,NSClipView) |
816c52cf DE |
231 | |
232 | // ======================================================================== | |
f298b203 | 233 | // wxWindowCocoaScrollView |
816c52cf | 234 | // ======================================================================== |
f298b203 | 235 | wxWindowCocoaScrollView::wxWindowCocoaScrollView(wxWindow *owner) |
816c52cf DE |
236 | : m_owner(owner) |
237 | { | |
a9861c57 | 238 | wxAutoNSAutoreleasePool pool; |
816c52cf DE |
239 | wxASSERT(owner); |
240 | wxASSERT(owner->GetNSView()); | |
241 | m_cocoaNSScrollView = [[NSScrollView alloc] | |
242 | initWithFrame:[owner->GetNSView() frame]]; | |
243 | AssociateNSView(m_cocoaNSScrollView); | |
244 | ||
245 | /* Replace the default NSClipView with a flipped one. This ensures | |
246 | scrolling is "pinned" to the top-left instead of bottom-right. */ | |
a24aa427 | 247 | NSClipView *flippedClip = [[WX_GET_OBJC_CLASS(wxFlippedNSClipView) alloc] |
816c52cf DE |
248 | initWithFrame: [[m_cocoaNSScrollView contentView] frame]]; |
249 | [m_cocoaNSScrollView setContentView:flippedClip]; | |
250 | [flippedClip release]; | |
251 | ||
252 | [m_cocoaNSScrollView setBackgroundColor: [NSColor windowBackgroundColor]]; | |
253 | [m_cocoaNSScrollView setHasHorizontalScroller: YES]; | |
254 | [m_cocoaNSScrollView setHasVerticalScroller: YES]; | |
255 | Encapsulate(); | |
256 | } | |
257 | ||
f298b203 | 258 | void wxWindowCocoaScrollView::Encapsulate() |
816c52cf | 259 | { |
6f2ec3c3 DE |
260 | // Set the scroll view autoresizingMask to match the current NSView |
261 | [m_cocoaNSScrollView setAutoresizingMask: [m_owner->GetNSView() autoresizingMask]]; | |
262 | [m_owner->GetNSView() setAutoresizingMask: NSViewNotSizable]; | |
816c52cf DE |
263 | // NOTE: replaceSubView will cause m_cocaNSView to be released |
264 | // except when it hasn't been added into an NSView hierarchy in which | |
265 | // case it doesn't need to be and this should work out to a no-op | |
266 | m_owner->CocoaReplaceView(m_owner->GetNSView(), m_cocoaNSScrollView); | |
267 | // The NSView is still retained by owner | |
268 | [m_cocoaNSScrollView setDocumentView: m_owner->GetNSView()]; | |
269 | // Now it's also retained by the NSScrollView | |
270 | } | |
271 | ||
f298b203 | 272 | void wxWindowCocoaScrollView::Unencapsulate() |
816c52cf DE |
273 | { |
274 | [m_cocoaNSScrollView setDocumentView: nil]; | |
275 | m_owner->CocoaReplaceView(m_cocoaNSScrollView, m_owner->GetNSView()); | |
6f2ec3c3 DE |
276 | if(![[m_owner->GetNSView() superview] isFlipped]) |
277 | [m_owner->GetNSView() setAutoresizingMask: NSViewMinYMargin]; | |
816c52cf DE |
278 | } |
279 | ||
f298b203 | 280 | wxWindowCocoaScrollView::~wxWindowCocoaScrollView() |
816c52cf DE |
281 | { |
282 | DisassociateNSView(m_cocoaNSScrollView); | |
283 | [m_cocoaNSScrollView release]; | |
284 | } | |
285 | ||
f298b203 | 286 | void wxWindowCocoaScrollView::ClientSizeToSize(int &width, int &height) |
816c52cf DE |
287 | { |
288 | NSSize frameSize = [NSScrollView | |
289 | frameSizeForContentSize: NSMakeSize(width,height) | |
290 | hasHorizontalScroller: [m_cocoaNSScrollView hasHorizontalScroller] | |
291 | hasVerticalScroller: [m_cocoaNSScrollView hasVerticalScroller] | |
292 | borderType: [m_cocoaNSScrollView borderType]]; | |
e9cece45 VZ |
293 | width = (int)frameSize.width; |
294 | height = (int)frameSize.height; | |
816c52cf DE |
295 | } |
296 | ||
f298b203 | 297 | void wxWindowCocoaScrollView::DoGetClientSize(int *x, int *y) const |
816c52cf DE |
298 | { |
299 | NSSize nssize = [m_cocoaNSScrollView contentSize]; | |
300 | if(x) | |
e9cece45 | 301 | *x = (int)nssize.width; |
816c52cf | 302 | if(y) |
e9cece45 | 303 | *y = (int)nssize.height; |
816c52cf DE |
304 | } |
305 | ||
f298b203 | 306 | void wxWindowCocoaScrollView::Cocoa_FrameChanged(void) |
816c52cf | 307 | { |
48580976 | 308 | wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_FrameChanged")); |
816c52cf DE |
309 | wxSizeEvent event(m_owner->GetSize(), m_owner->GetId()); |
310 | event.SetEventObject(m_owner); | |
311 | m_owner->GetEventHandler()->ProcessEvent(event); | |
312 | } | |
313 | ||
a82b8141 DE |
314 | // ======================================================================== |
315 | // wxWindowCocoa | |
316 | // ======================================================================== | |
fb896a32 DE |
317 | // normally the base classes aren't included, but wxWindow is special |
318 | #ifdef __WXUNIVERSAL__ | |
319 | IMPLEMENT_ABSTRACT_CLASS(wxWindowCocoa, wxWindowBase) | |
320 | #else | |
321 | IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase) | |
322 | #endif | |
323 | ||
324 | BEGIN_EVENT_TABLE(wxWindowCocoa, wxWindowBase) | |
325 | END_EVENT_TABLE() | |
326 | ||
b9505233 DE |
327 | wxWindow *wxWindowCocoa::sm_capturedWindow = NULL; |
328 | ||
fb896a32 DE |
329 | // Constructor |
330 | void wxWindowCocoa::Init() | |
331 | { | |
fb896a32 | 332 | m_cocoaNSView = NULL; |
a82b8141 | 333 | m_cocoaHider = NULL; |
f298b203 | 334 | m_wxCocoaScrollView = NULL; |
8d8d3633 WS |
335 | m_isBeingDeleted = false; |
336 | m_isInPaint = false; | |
7c5a378f | 337 | m_visibleTrackingRectManager = NULL; |
fb896a32 DE |
338 | } |
339 | ||
340 | // Constructor | |
341 | bool wxWindow::Create(wxWindow *parent, wxWindowID winid, | |
342 | const wxPoint& pos, | |
343 | const wxSize& size, | |
344 | long style, | |
345 | const wxString& name) | |
346 | { | |
347 | if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name)) | |
348 | return false; | |
349 | ||
350 | // TODO: create the window | |
fb896a32 | 351 | m_cocoaNSView = NULL; |
a24aa427 | 352 | SetNSView([[WX_GET_OBJC_CLASS(WXNSView) alloc] initWithFrame: MakeDefaultNSRect(size)]); |
fb896a32 DE |
353 | [m_cocoaNSView release]; |
354 | ||
355 | if (m_parent) | |
356 | { | |
357 | m_parent->AddChild(this); | |
358 | m_parent->CocoaAddChild(this); | |
6d034f7d | 359 | SetInitialFrameRect(pos,size); |
fb896a32 DE |
360 | } |
361 | ||
8d8d3633 | 362 | return true; |
fb896a32 DE |
363 | } |
364 | ||
365 | // Destructor | |
366 | wxWindow::~wxWindow() | |
367 | { | |
7fc77f30 | 368 | wxAutoNSAutoreleasePool pool; |
fb896a32 DE |
369 | DestroyChildren(); |
370 | ||
065e208e | 371 | // Make sure our parent (in the wxWidgets sense) is our superview |
6ba13ca4 DE |
372 | // before we go removing from it. |
373 | if(m_parent && m_parent->GetNSView()==[GetNSViewForSuperview() superview]) | |
374 | CocoaRemoveFromParent(); | |
a82b8141 | 375 | delete m_cocoaHider; |
f298b203 | 376 | delete m_wxCocoaScrollView; |
9c85202a DE |
377 | if(m_cocoaNSView) |
378 | SendDestroyEvent(); | |
fb896a32 DE |
379 | SetNSView(NULL); |
380 | } | |
381 | ||
382 | void wxWindowCocoa::CocoaAddChild(wxWindowCocoa *child) | |
383 | { | |
b0a207df DE |
384 | // Pool here due to lack of one during wx init phase |
385 | wxAutoNSAutoreleasePool pool; | |
386 | ||
a82b8141 DE |
387 | NSView *childView = child->GetNSViewForSuperview(); |
388 | ||
389 | wxASSERT(childView); | |
390 | [m_cocoaNSView addSubview: childView]; | |
391 | child->m_isShown = !m_cocoaHider; | |
fb896a32 DE |
392 | } |
393 | ||
394 | void wxWindowCocoa::CocoaRemoveFromParent(void) | |
395 | { | |
a82b8141 | 396 | [GetNSViewForSuperview() removeFromSuperview]; |
fb896a32 DE |
397 | } |
398 | ||
399 | void wxWindowCocoa::SetNSView(WX_NSView cocoaNSView) | |
400 | { | |
7c5a378f DE |
401 | // Clear the visible area tracking rect if we have one. |
402 | delete m_visibleTrackingRectManager; | |
403 | m_visibleTrackingRectManager = NULL; | |
404 | ||
fb896a32 | 405 | bool need_debug = cocoaNSView || m_cocoaNSView; |
48580976 | 406 | if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxWindowCocoa=%p::SetNSView [m_cocoaNSView=%p retainCount]=%d"),this,m_cocoaNSView,[m_cocoaNSView retainCount]); |
bac6f234 | 407 | DisassociateNSView(m_cocoaNSView); |
fb896a32 DE |
408 | [cocoaNSView retain]; |
409 | [m_cocoaNSView release]; | |
410 | m_cocoaNSView = cocoaNSView; | |
bac6f234 | 411 | AssociateNSView(m_cocoaNSView); |
48580976 | 412 | if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxWindowCocoa=%p::SetNSView [cocoaNSView=%p retainCount]=%d"),this,cocoaNSView,[cocoaNSView retainCount]); |
fb896a32 DE |
413 | } |
414 | ||
a82b8141 DE |
415 | WX_NSView wxWindowCocoa::GetNSViewForSuperview() const |
416 | { | |
417 | return m_cocoaHider | |
418 | ? m_cocoaHider->GetNSView() | |
f298b203 DE |
419 | : m_wxCocoaScrollView |
420 | ? m_wxCocoaScrollView->GetNSScrollView() | |
816c52cf | 421 | : m_cocoaNSView; |
a82b8141 DE |
422 | } |
423 | ||
424 | WX_NSView wxWindowCocoa::GetNSViewForHiding() const | |
425 | { | |
f298b203 DE |
426 | return m_wxCocoaScrollView |
427 | ? m_wxCocoaScrollView->GetNSScrollView() | |
816c52cf | 428 | : m_cocoaNSView; |
a82b8141 DE |
429 | } |
430 | ||
34c9978d DE |
431 | NSPoint wxWindowCocoa::CocoaTransformBoundsToWx(NSPoint pointBounds) |
432 | { | |
433 | // TODO: Handle scrolling offset | |
ee022549 | 434 | return CocoaTransformNSViewBoundsToWx(GetNSView(), pointBounds); |
34c9978d DE |
435 | } |
436 | ||
437 | NSRect wxWindowCocoa::CocoaTransformBoundsToWx(NSRect rectBounds) | |
438 | { | |
439 | // TODO: Handle scrolling offset | |
ee022549 | 440 | return CocoaTransformNSViewBoundsToWx(GetNSView(), rectBounds); |
34c9978d DE |
441 | } |
442 | ||
443 | NSPoint wxWindowCocoa::CocoaTransformWxToBounds(NSPoint pointWx) | |
444 | { | |
445 | // TODO: Handle scrolling offset | |
ee022549 | 446 | return CocoaTransformNSViewWxToBounds(GetNSView(), pointWx); |
34c9978d DE |
447 | } |
448 | ||
449 | NSRect wxWindowCocoa::CocoaTransformWxToBounds(NSRect rectWx) | |
450 | { | |
451 | // TODO: Handle scrolling offset | |
ee022549 | 452 | return CocoaTransformNSViewWxToBounds(GetNSView(), rectWx); |
34c9978d DE |
453 | } |
454 | ||
4db3c8ac DE |
455 | WX_NSAffineTransform wxWindowCocoa::CocoaGetWxToBoundsTransform() |
456 | { | |
457 | // TODO: Handle scrolling offset | |
458 | NSAffineTransform *transform = wxDC::CocoaGetWxToBoundsTransform([GetNSView() isFlipped], [GetNSView() bounds].size.height); | |
459 | return transform; | |
460 | } | |
461 | ||
8ea5271e DE |
462 | bool wxWindowCocoa::Cocoa_drawRect(const NSRect &rect) |
463 | { | |
48580976 | 464 | wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_drawRect")); |
55c5be5e DE |
465 | // Recursion can happen if the event loop runs from within the paint |
466 | // handler. For instance, if an assertion dialog is shown. | |
467 | // FIXME: This seems less than ideal. | |
468 | if(m_isInPaint) | |
469 | { | |
2b030203 | 470 | wxLogDebug(wxT("Paint event recursion!")); |
55c5be5e DE |
471 | return false; |
472 | } | |
8d8d3633 | 473 | m_isInPaint = true; |
dc5bcaef DE |
474 | |
475 | // Set m_updateRegion | |
476 | const NSRect *rects = ▭ // The bounding box of the region | |
4799f3ba | 477 | NSInteger countRects = 1; |
dc5bcaef | 478 | // Try replacing the larger rectangle with a list of smaller ones: |
5dc47140 DE |
479 | if ([GetNSView() respondsToSelector:@selector(getRectsBeingDrawn:count:)]) |
480 | [GetNSView() getRectsBeingDrawn:&rects count:&countRects]; | |
34c9978d DE |
481 | |
482 | NSRect *transformedRects = (NSRect*)malloc(sizeof(NSRect)*countRects); | |
483 | for(int i=0; i<countRects; i++) | |
484 | { | |
485 | transformedRects[i] = CocoaTransformBoundsToWx(rects[i]); | |
486 | } | |
487 | m_updateRegion = wxRegion(transformedRects,countRects); | |
488 | free(transformedRects); | |
dc5bcaef | 489 | |
8ea5271e DE |
490 | wxPaintEvent event(m_windowId); |
491 | event.SetEventObject(this); | |
55c5be5e | 492 | bool ret = GetEventHandler()->ProcessEvent(event); |
8d8d3633 | 493 | m_isInPaint = false; |
55c5be5e | 494 | return ret; |
8ea5271e DE |
495 | } |
496 | ||
69dbb709 DE |
497 | void wxWindowCocoa::InitMouseEvent(wxMouseEvent& event, WX_NSEvent cocoaEvent) |
498 | { | |
2b030203 | 499 | wxASSERT_MSG([m_cocoaNSView window]==[cocoaEvent window],wxT("Mouse event for different NSWindow")); |
34c9978d DE |
500 | // Mouse events happen at the NSWindow level so we need to convert |
501 | // into our bounds coordinates then convert to wx coordinates. | |
a82b8141 | 502 | NSPoint cocoaPoint = [m_cocoaNSView convertPoint:[(NSEvent*)cocoaEvent locationInWindow] fromView:nil]; |
34c9978d DE |
503 | NSPoint pointWx = CocoaTransformBoundsToWx(cocoaPoint); |
504 | // FIXME: Should we be adjusting for client area origin? | |
69dbb709 | 505 | const wxPoint &clientorigin = GetClientAreaOrigin(); |
34c9978d DE |
506 | event.m_x = (wxCoord)pointWx.x - clientorigin.x; |
507 | event.m_y = (wxCoord)pointWx.y - clientorigin.y; | |
69dbb709 DE |
508 | |
509 | event.m_shiftDown = [cocoaEvent modifierFlags] & NSShiftKeyMask; | |
510 | event.m_controlDown = [cocoaEvent modifierFlags] & NSControlKeyMask; | |
511 | event.m_altDown = [cocoaEvent modifierFlags] & NSAlternateKeyMask; | |
512 | event.m_metaDown = [cocoaEvent modifierFlags] & NSCommandKeyMask; | |
513 | ||
514 | // TODO: set timestamp? | |
515 | event.SetEventObject(this); | |
516 | event.SetId(GetId()); | |
517 | } | |
518 | ||
519 | bool wxWindowCocoa::Cocoa_mouseMoved(WX_NSEvent theEvent) | |
520 | { | |
521 | wxMouseEvent event(wxEVT_MOTION); | |
522 | InitMouseEvent(event,theEvent); | |
7c5a378f | 523 | wxLogTrace(wxTRACE_COCOA,wxT("wxWindow=%p::Cocoa_mouseMoved @%d,%d"),this,event.m_x,event.m_y); |
69dbb709 DE |
524 | return GetEventHandler()->ProcessEvent(event); |
525 | } | |
526 | ||
7c5a378f DE |
527 | void wxWindowCocoa::Cocoa_synthesizeMouseMoved() |
528 | { | |
529 | wxMouseEvent event(wxEVT_MOTION); | |
530 | NSWindow *window = [GetNSView() window]; | |
531 | NSPoint locationInWindow = [window mouseLocationOutsideOfEventStream]; | |
532 | NSPoint cocoaPoint = [m_cocoaNSView convertPoint:locationInWindow fromView:nil]; | |
533 | ||
534 | NSPoint pointWx = CocoaTransformBoundsToWx(cocoaPoint); | |
535 | // FIXME: Should we be adjusting for client area origin? | |
536 | const wxPoint &clientorigin = GetClientAreaOrigin(); | |
537 | event.m_x = (wxCoord)pointWx.x - clientorigin.x; | |
538 | event.m_y = (wxCoord)pointWx.y - clientorigin.y; | |
539 | ||
540 | // TODO: Handle shift, control, alt, meta flags | |
541 | event.SetEventObject(this); | |
542 | event.SetId(GetId()); | |
543 | ||
544 | wxLogTrace(wxTRACE_COCOA,wxT("wxwin=%p Synthesized Mouse Moved @%d,%d"),this,event.m_x,event.m_y); | |
545 | GetEventHandler()->ProcessEvent(event); | |
546 | } | |
547 | ||
69dbb709 DE |
548 | bool wxWindowCocoa::Cocoa_mouseEntered(WX_NSEvent theEvent) |
549 | { | |
7c5a378f DE |
550 | if(m_visibleTrackingRectManager != NULL && m_visibleTrackingRectManager->IsOwnerOfEvent(theEvent)) |
551 | { | |
552 | m_visibleTrackingRectManager->BeginSynthesizingEvents(); | |
553 | ||
554 | // Although we synthesize the mouse moved events we don't poll for them but rather send them only when | |
555 | // some other event comes in. That other event is (guess what) mouse moved events that will be sent | |
556 | // to the NSWindow which will forward them on to the first responder. We are not likely to be the | |
557 | // first responder, so the mouseMoved: events are effectively discarded. | |
558 | [[GetNSView() window] setAcceptsMouseMovedEvents:YES]; | |
559 | ||
560 | wxMouseEvent event(wxEVT_ENTER_WINDOW); | |
561 | InitMouseEvent(event,theEvent); | |
562 | wxLogTrace(wxTRACE_COCOA,wxT("wxwin=%p Mouse Entered @%d,%d"),this,event.m_x,event.m_y); | |
563 | return GetEventHandler()->ProcessEvent(event); | |
564 | } | |
565 | else | |
566 | return false; | |
69dbb709 DE |
567 | } |
568 | ||
569 | bool wxWindowCocoa::Cocoa_mouseExited(WX_NSEvent theEvent) | |
570 | { | |
7c5a378f DE |
571 | if(m_visibleTrackingRectManager != NULL && m_visibleTrackingRectManager->IsOwnerOfEvent(theEvent)) |
572 | { | |
573 | m_visibleTrackingRectManager->StopSynthesizingEvents(); | |
574 | ||
575 | wxMouseEvent event(wxEVT_LEAVE_WINDOW); | |
576 | InitMouseEvent(event,theEvent); | |
577 | wxLogTrace(wxTRACE_COCOA,wxT("wxwin=%p Mouse Exited @%d,%d"),this,event.m_x,event.m_y); | |
578 | return GetEventHandler()->ProcessEvent(event); | |
579 | } | |
580 | else | |
581 | return false; | |
69dbb709 DE |
582 | } |
583 | ||
584 | bool wxWindowCocoa::Cocoa_mouseDown(WX_NSEvent theEvent) | |
585 | { | |
586 | wxMouseEvent event([theEvent clickCount]<2?wxEVT_LEFT_DOWN:wxEVT_LEFT_DCLICK); | |
587 | InitMouseEvent(event,theEvent); | |
48580976 | 588 | wxLogTrace(wxTRACE_COCOA,wxT("Mouse Down @%d,%d num clicks=%d"),event.m_x,event.m_y,[theEvent clickCount]); |
69dbb709 DE |
589 | return GetEventHandler()->ProcessEvent(event); |
590 | } | |
591 | ||
592 | bool wxWindowCocoa::Cocoa_mouseDragged(WX_NSEvent theEvent) | |
593 | { | |
594 | wxMouseEvent event(wxEVT_MOTION); | |
595 | InitMouseEvent(event,theEvent); | |
596 | event.m_leftDown = true; | |
48580976 | 597 | wxLogTrace(wxTRACE_COCOA,wxT("Mouse Drag @%d,%d"),event.m_x,event.m_y); |
69dbb709 DE |
598 | return GetEventHandler()->ProcessEvent(event); |
599 | } | |
600 | ||
601 | bool wxWindowCocoa::Cocoa_mouseUp(WX_NSEvent theEvent) | |
602 | { | |
603 | wxMouseEvent event(wxEVT_LEFT_UP); | |
604 | InitMouseEvent(event,theEvent); | |
48580976 | 605 | wxLogTrace(wxTRACE_COCOA,wxT("Mouse Up @%d,%d"),event.m_x,event.m_y); |
69dbb709 DE |
606 | return GetEventHandler()->ProcessEvent(event); |
607 | } | |
608 | ||
609 | bool wxWindowCocoa::Cocoa_rightMouseDown(WX_NSEvent theEvent) | |
610 | { | |
eafde5c7 DE |
611 | wxMouseEvent event([theEvent clickCount]<2?wxEVT_RIGHT_DOWN:wxEVT_RIGHT_DCLICK); |
612 | InitMouseEvent(event,theEvent); | |
613 | wxLogDebug(wxT("Mouse Down @%d,%d num clicks=%d"),event.m_x,event.m_y,[theEvent clickCount]); | |
614 | return GetEventHandler()->ProcessEvent(event); | |
69dbb709 DE |
615 | } |
616 | ||
617 | bool wxWindowCocoa::Cocoa_rightMouseDragged(WX_NSEvent theEvent) | |
618 | { | |
eafde5c7 DE |
619 | wxMouseEvent event(wxEVT_MOTION); |
620 | InitMouseEvent(event,theEvent); | |
621 | event.m_rightDown = true; | |
622 | wxLogDebug(wxT("Mouse Drag @%d,%d"),event.m_x,event.m_y); | |
623 | return GetEventHandler()->ProcessEvent(event); | |
69dbb709 DE |
624 | } |
625 | ||
626 | bool wxWindowCocoa::Cocoa_rightMouseUp(WX_NSEvent theEvent) | |
627 | { | |
eafde5c7 DE |
628 | wxMouseEvent event(wxEVT_RIGHT_UP); |
629 | InitMouseEvent(event,theEvent); | |
630 | wxLogDebug(wxT("Mouse Up @%d,%d"),event.m_x,event.m_y); | |
631 | return GetEventHandler()->ProcessEvent(event); | |
69dbb709 DE |
632 | } |
633 | ||
634 | bool wxWindowCocoa::Cocoa_otherMouseDown(WX_NSEvent theEvent) | |
635 | { | |
636 | return false; | |
637 | } | |
638 | ||
639 | bool wxWindowCocoa::Cocoa_otherMouseDragged(WX_NSEvent theEvent) | |
640 | { | |
641 | return false; | |
642 | } | |
643 | ||
644 | bool wxWindowCocoa::Cocoa_otherMouseUp(WX_NSEvent theEvent) | |
645 | { | |
646 | return false; | |
647 | } | |
648 | ||
fb896a32 DE |
649 | void wxWindowCocoa::Cocoa_FrameChanged(void) |
650 | { | |
7c5a378f DE |
651 | wxLogTrace(wxTRACE_COCOA,wxT("wxWindow=%p::Cocoa_FrameChanged"),this); |
652 | if(m_visibleTrackingRectManager != NULL) | |
653 | m_visibleTrackingRectManager->RebuildTrackingRect(); | |
fb896a32 DE |
654 | wxSizeEvent event(GetSize(), m_windowId); |
655 | event.SetEventObject(this); | |
656 | GetEventHandler()->ProcessEvent(event); | |
657 | } | |
658 | ||
5558135c RN |
659 | bool wxWindowCocoa::Cocoa_resetCursorRects() |
660 | { | |
7c5a378f DE |
661 | wxLogTrace(wxTRACE_COCOA,wxT("wxWindow=%p::Cocoa_resetCursorRects"),this); |
662 | if(m_visibleTrackingRectManager != NULL) | |
663 | m_visibleTrackingRectManager->RebuildTrackingRect(); | |
664 | ||
5558135c RN |
665 | if(!m_cursor.GetNSCursor()) |
666 | return false; | |
8d8d3633 WS |
667 | |
668 | [GetNSView() addCursorRect: [GetNSView() visibleRect] cursor: m_cursor.GetNSCursor()]; | |
669 | ||
5558135c RN |
670 | return true; |
671 | } | |
672 | ||
a8780ad5 DE |
673 | bool wxWindowCocoa::SetCursor(const wxCursor &cursor) |
674 | { | |
675 | if(!wxWindowBase::SetCursor(cursor)) | |
676 | return false; | |
677 | // Invalidate the cursor rects so the cursor will change | |
678 | [[GetNSView() window] invalidateCursorRectsForView:GetNSView()]; | |
679 | return true; | |
680 | } | |
681 | ||
7c5a378f DE |
682 | bool wxWindowCocoa::Cocoa_viewDidMoveToWindow() |
683 | { | |
684 | wxLogTrace(wxTRACE_COCOA,wxT("wxWindow=%p::viewDidMoveToWindow"),this); | |
685 | // Set up new tracking rects. I am reasonably sure the new window must be set before doing this. | |
686 | if(m_visibleTrackingRectManager != NULL) | |
687 | m_visibleTrackingRectManager->BuildTrackingRect(); | |
688 | return false; | |
689 | } | |
690 | ||
691 | bool wxWindowCocoa::Cocoa_viewWillMoveToWindow(WX_NSWindow newWindow) | |
692 | { | |
693 | wxLogTrace(wxTRACE_COCOA,wxT("wxWindow=%p::viewWillMoveToWindow:%p"),this, newWindow); | |
694 | // Clear tracking rects. It is imperative this be done before the new window is set. | |
695 | if(m_visibleTrackingRectManager != NULL) | |
696 | m_visibleTrackingRectManager->ClearTrackingRect(); | |
697 | return false; | |
698 | } | |
699 | ||
fb896a32 DE |
700 | bool wxWindow::Close(bool force) |
701 | { | |
cc6f960f DE |
702 | // The only reason this function exists is that it is virtual and |
703 | // wxTopLevelWindowCocoa will override it. | |
704 | return wxWindowBase::Close(force); | |
fb896a32 DE |
705 | } |
706 | ||
a82b8141 DE |
707 | void wxWindow::CocoaReplaceView(WX_NSView oldView, WX_NSView newView) |
708 | { | |
709 | [[oldView superview] replaceSubview:oldView with:newView]; | |
710 | } | |
711 | ||
47a8a4d5 | 712 | void wxWindow::DoEnable(bool enable) |
adb4816c | 713 | { |
47a8a4d5 | 714 | CocoaSetEnabled(enable); |
adb4816c DE |
715 | } |
716 | ||
fb896a32 DE |
717 | bool wxWindow::Show(bool show) |
718 | { | |
7fc77f30 | 719 | wxAutoNSAutoreleasePool pool; |
fb896a32 DE |
720 | // If the window is marked as visible, then it shouldn't have a dummy view |
721 | // If the window is marked hidden, then it should have a dummy view | |
addbdd29 | 722 | // wxSpinCtrl (generic) abuses m_isShown, don't use it for any logic |
2b030203 | 723 | // wxASSERT_MSG( (m_isShown && !m_dummyNSView) || (!m_isShown && m_dummyNSView),wxT("wxWindow: m_isShown does not agree with m_dummyNSView")); |
fb896a32 | 724 | // Return false if there isn't a window to show or hide |
a82b8141 DE |
725 | NSView *cocoaView = GetNSViewForHiding(); |
726 | if(!cocoaView) | |
fb896a32 | 727 | return false; |
fb896a32 DE |
728 | if(show) |
729 | { | |
addbdd29 | 730 | // If state isn't changing, return false |
a82b8141 | 731 | if(!m_cocoaHider) |
addbdd29 | 732 | return false; |
a82b8141 DE |
733 | CocoaReplaceView(m_cocoaHider->GetNSView(), cocoaView); |
734 | wxASSERT(![m_cocoaHider->GetNSView() superview]); | |
735 | delete m_cocoaHider; | |
736 | m_cocoaHider = NULL; | |
737 | wxASSERT([cocoaView superview]); | |
fb896a32 DE |
738 | } |
739 | else | |
740 | { | |
addbdd29 | 741 | // If state isn't changing, return false |
a82b8141 | 742 | if(m_cocoaHider) |
addbdd29 | 743 | return false; |
a82b8141 DE |
744 | m_cocoaHider = new wxWindowCocoaHider(this); |
745 | // NOTE: replaceSubview:with will cause m_cocaNSView to be | |
746 | // (auto)released which balances out addSubview | |
747 | CocoaReplaceView(cocoaView, m_cocoaHider->GetNSView()); | |
fb896a32 | 748 | // m_coocaNSView is now only retained by us |
a82b8141 DE |
749 | wxASSERT([m_cocoaHider->GetNSView() superview]); |
750 | wxASSERT(![cocoaView superview]); | |
fb896a32 | 751 | } |
a6b4ff2e DE |
752 | m_isShown = show; |
753 | return true; | |
fb896a32 DE |
754 | } |
755 | ||
756 | void wxWindowCocoa::DoSetSize(int x, int y, int width, int height, int sizeFlags) | |
757 | { | |
9879fa84 | 758 | 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 |
759 | int currentX, currentY; |
760 | int currentW, currentH; | |
761 | DoGetPosition(¤tX, ¤tY); | |
762 | DoGetSize(¤tW, ¤tH); | |
763 | if((x==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE)) | |
764 | x=currentX; | |
765 | if((y==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE)) | |
766 | y=currentY; | |
767 | ||
768 | AdjustForParentClientOrigin(x,y,sizeFlags); | |
769 | ||
8d8d3633 | 770 | wxSize size(wxDefaultSize); |
fb896a32 DE |
771 | |
772 | if((width==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE)) | |
773 | { | |
774 | if(sizeFlags&wxSIZE_AUTO_WIDTH) | |
775 | { | |
776 | size=DoGetBestSize(); | |
777 | width=size.x; | |
778 | } | |
779 | else | |
780 | width=currentW; | |
781 | } | |
782 | if((height==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE)) | |
783 | { | |
784 | if(sizeFlags&wxSIZE_AUTO_HEIGHT) | |
785 | { | |
786 | if(size.x==-1) | |
787 | size=DoGetBestSize(); | |
788 | height=size.y; | |
789 | } | |
790 | else | |
791 | height=currentH; | |
792 | } | |
793 | DoMoveWindow(x,y,width,height); | |
794 | } | |
795 | ||
1e151594 | 796 | #if wxUSE_TOOLTIPS |
26191790 RN |
797 | |
798 | void wxWindowCocoa::DoSetToolTip( wxToolTip *tip ) | |
799 | { | |
800 | wxWindowBase::DoSetToolTip(tip); | |
801 | ||
26191790 RN |
802 | if ( m_tooltip ) |
803 | { | |
804 | m_tooltip->SetWindow((wxWindow *)this); | |
26191790 RN |
805 | } |
806 | } | |
807 | ||
1e151594 RN |
808 | #endif |
809 | ||
fb896a32 DE |
810 | void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height) |
811 | { | |
bed6fe0c | 812 | wxAutoNSAutoreleasePool pool; |
9879fa84 | 813 | wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)"),this,x,y,width,height); |
fb896a32 | 814 | |
a82b8141 | 815 | NSView *nsview = GetNSViewForSuperview(); |
d449cf47 | 816 | NSView *superview = [nsview superview]; |
fb896a32 | 817 | |
34c9978d DE |
818 | wxCHECK_RET(GetParent(), wxT("Window can only be placed correctly when it has a parent")); |
819 | ||
820 | NSRect oldFrameRect = [nsview frame]; | |
821 | NSRect newFrameRect = GetParent()->CocoaTransformWxToBounds(NSMakeRect(x,y,width,height)); | |
822 | [nsview setFrame:newFrameRect]; | |
b915b805 | 823 | // Be sure to redraw the parent to reflect the changed position |
34c9978d DE |
824 | [superview setNeedsDisplayInRect:oldFrameRect]; |
825 | [superview setNeedsDisplayInRect:newFrameRect]; | |
fb896a32 DE |
826 | } |
827 | ||
d139c3a8 DE |
828 | void wxWindowCocoa::SetInitialFrameRect(const wxPoint& pos, const wxSize& size) |
829 | { | |
830 | NSView *nsview = GetNSViewForSuperview(); | |
831 | NSView *superview = [nsview superview]; | |
2b030203 | 832 | wxCHECK_RET(superview,wxT("NSView does not have a superview")); |
34c9978d | 833 | wxCHECK_RET(GetParent(), wxT("Window can only be placed correctly when it has a parent")); |
d139c3a8 DE |
834 | NSRect frameRect = [nsview frame]; |
835 | if(size.x!=-1) | |
836 | frameRect.size.width = size.x; | |
837 | if(size.y!=-1) | |
838 | frameRect.size.height = size.y; | |
839 | frameRect.origin.x = pos.x; | |
34c9978d | 840 | frameRect.origin.y = pos.y; |
c5bd9191 DE |
841 | // Tell Cocoa to change the margin between the bottom of the superview |
842 | // and the bottom of the control. Keeps the control pinned to the top | |
065e208e | 843 | // of its superview so that its position in the wxWidgets coordinate |
c5bd9191 DE |
844 | // system doesn't change. |
845 | if(![superview isFlipped]) | |
846 | [nsview setAutoresizingMask: NSViewMinYMargin]; | |
6f2ec3c3 DE |
847 | // MUST set the mask before setFrame: which can generate a size event |
848 | // and cause a scroller to be added! | |
34c9978d | 849 | frameRect = GetParent()->CocoaTransformWxToBounds(frameRect); |
6f2ec3c3 | 850 | [nsview setFrame: frameRect]; |
d139c3a8 DE |
851 | } |
852 | ||
fb896a32 DE |
853 | // Get total size |
854 | void wxWindow::DoGetSize(int *w, int *h) const | |
855 | { | |
a82b8141 | 856 | NSRect cocoaRect = [GetNSViewForSuperview() frame]; |
fb896a32 DE |
857 | if(w) |
858 | *w=(int)cocoaRect.size.width; | |
859 | if(h) | |
860 | *h=(int)cocoaRect.size.height; | |
9879fa84 | 861 | wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoGetSize = (%d,%d)"),this,(int)cocoaRect.size.width,(int)cocoaRect.size.height); |
fb896a32 DE |
862 | } |
863 | ||
864 | void wxWindow::DoGetPosition(int *x, int *y) const | |
865 | { | |
a82b8141 | 866 | NSView *nsview = GetNSViewForSuperview(); |
fb896a32 | 867 | |
576a1544 | 868 | NSRect cocoaRect = [nsview frame]; |
34c9978d | 869 | NSRect rectWx = GetParent()->CocoaTransformBoundsToWx(cocoaRect); |
fb896a32 | 870 | if(x) |
34c9978d | 871 | *x=(int)rectWx.origin.x; |
fb896a32 | 872 | if(y) |
34c9978d | 873 | *y=(int)rectWx.origin.y; |
9879fa84 | 874 | wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoGetPosition = (%d,%d)"),this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y); |
fb896a32 DE |
875 | } |
876 | ||
877 | WXWidget wxWindow::GetHandle() const | |
878 | { | |
879 | return m_cocoaNSView; | |
880 | } | |
881 | ||
f7e98dee RN |
882 | wxWindow* wxWindow::GetWxWindow() const |
883 | { | |
884 | return (wxWindow*) this; | |
885 | } | |
886 | ||
ddf7346a DE |
887 | void wxWindow::Refresh(bool eraseBack, const wxRect *rect) |
888 | { | |
889 | [m_cocoaNSView setNeedsDisplay:YES]; | |
890 | } | |
891 | ||
fb896a32 DE |
892 | void wxWindow::SetFocus() |
893 | { | |
67d2dac8 DE |
894 | if([GetNSView() acceptsFirstResponder]) |
895 | [[GetNSView() window] makeFirstResponder: GetNSView()]; | |
fb896a32 DE |
896 | } |
897 | ||
898 | void wxWindow::DoCaptureMouse() | |
899 | { | |
900 | // TODO | |
b9505233 | 901 | sm_capturedWindow = this; |
fb896a32 DE |
902 | } |
903 | ||
904 | void wxWindow::DoReleaseMouse() | |
905 | { | |
906 | // TODO | |
b9505233 | 907 | sm_capturedWindow = NULL; |
fb896a32 DE |
908 | } |
909 | ||
910 | void wxWindow::DoScreenToClient(int *x, int *y) const | |
911 | { | |
912 | // TODO | |
913 | } | |
914 | ||
915 | void wxWindow::DoClientToScreen(int *x, int *y) const | |
916 | { | |
917 | // TODO | |
918 | } | |
919 | ||
920 | // Get size *available for subwindows* i.e. excluding menu bar etc. | |
921 | void wxWindow::DoGetClientSize(int *x, int *y) const | |
922 | { | |
48580976 | 923 | wxLogTrace(wxTRACE_COCOA,wxT("DoGetClientSize:")); |
f298b203 DE |
924 | if(m_wxCocoaScrollView) |
925 | m_wxCocoaScrollView->DoGetClientSize(x,y); | |
816c52cf DE |
926 | else |
927 | wxWindowCocoa::DoGetSize(x,y); | |
fb896a32 DE |
928 | } |
929 | ||
930 | void wxWindow::DoSetClientSize(int width, int height) | |
931 | { | |
48580976 | 932 | wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("DoSetClientSize=(%d,%d)"),width,height); |
f298b203 DE |
933 | if(m_wxCocoaScrollView) |
934 | m_wxCocoaScrollView->ClientSizeToSize(width,height); | |
e08efb8d DE |
935 | CocoaSetWxWindowSize(width,height); |
936 | } | |
937 | ||
938 | void wxWindow::CocoaSetWxWindowSize(int width, int height) | |
939 | { | |
8d8d3633 WS |
940 | wxWindowCocoa::DoSetSize(wxDefaultCoord,wxDefaultCoord,width,height,wxSIZE_USE_EXISTING); |
941 | } | |
942 | ||
943 | void wxWindow::SetLabel(const wxString& WXUNUSED(label)) | |
944 | { | |
ba64d0b6 | 945 | // Intentional no-op. |
8d8d3633 WS |
946 | } |
947 | ||
948 | wxString wxWindow::GetLabel() const | |
949 | { | |
ba64d0b6 DE |
950 | // General Get/Set of labels is implemented in wxControlBase |
951 | wxLogDebug(wxT("wxWindow::GetLabel: Should be overridden if needed.")); | |
8d8d3633 | 952 | return wxEmptyString; |
fb896a32 DE |
953 | } |
954 | ||
955 | int wxWindow::GetCharHeight() const | |
956 | { | |
957 | // TODO | |
958 | return 0; | |
959 | } | |
960 | ||
961 | int wxWindow::GetCharWidth() const | |
962 | { | |
963 | // TODO | |
964 | return 0; | |
965 | } | |
966 | ||
967 | void wxWindow::GetTextExtent(const wxString& string, int *x, int *y, | |
968 | int *descent, int *externalLeading, const wxFont *theFont) const | |
969 | { | |
970 | // TODO | |
971 | } | |
972 | ||
fb896a32 DE |
973 | // Coordinates relative to the window |
974 | void wxWindow::WarpPointer (int x_pos, int y_pos) | |
975 | { | |
976 | // TODO | |
977 | } | |
978 | ||
979 | int wxWindow::GetScrollPos(int orient) const | |
980 | { | |
981 | // TODO | |
982 | return 0; | |
983 | } | |
984 | ||
985 | // This now returns the whole range, not just the number | |
986 | // of positions that we can scroll. | |
987 | int wxWindow::GetScrollRange(int orient) const | |
988 | { | |
989 | // TODO | |
990 | return 0; | |
991 | } | |
992 | ||
993 | int wxWindow::GetScrollThumb(int orient) const | |
994 | { | |
995 | // TODO | |
996 | return 0; | |
997 | } | |
998 | ||
999 | void wxWindow::SetScrollPos(int orient, int pos, bool refresh) | |
1000 | { | |
1001 | // TODO | |
1002 | } | |
1003 | ||
816c52cf DE |
1004 | void wxWindow::CocoaCreateNSScrollView() |
1005 | { | |
f298b203 | 1006 | if(!m_wxCocoaScrollView) |
816c52cf | 1007 | { |
f298b203 | 1008 | m_wxCocoaScrollView = new wxWindowCocoaScrollView(this); |
816c52cf DE |
1009 | } |
1010 | } | |
1011 | ||
fb896a32 DE |
1012 | // New function that will replace some of the above. |
1013 | void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible, | |
1014 | int range, bool refresh) | |
1015 | { | |
e08efb8d | 1016 | CocoaCreateNSScrollView(); |
fb896a32 DE |
1017 | // TODO |
1018 | } | |
1019 | ||
1020 | // Does a physical scroll | |
1021 | void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect) | |
1022 | { | |
1023 | // TODO | |
1024 | } | |
1025 | ||
816c52cf DE |
1026 | void wxWindow::DoSetVirtualSize( int x, int y ) |
1027 | { | |
1028 | wxWindowBase::DoSetVirtualSize(x,y); | |
1029 | CocoaCreateNSScrollView(); | |
1030 | [m_cocoaNSView setFrameSize:NSMakeSize(m_virtualSize.x,m_virtualSize.y)]; | |
1031 | } | |
1032 | ||
fb896a32 DE |
1033 | bool wxWindow::SetFont(const wxFont& font) |
1034 | { | |
e7e97a59 DE |
1035 | // FIXME: We may need to handle wx font inheritance. |
1036 | return wxWindowBase::SetFont(font); | |
fb896a32 DE |
1037 | } |
1038 | ||
aa25b7f9 DE |
1039 | #if 0 // these are used when debugging the algorithm. |
1040 | static char const * const comparisonresultStrings[] = | |
1041 | { "<" | |
1042 | , "==" | |
1043 | , ">" | |
1044 | }; | |
1045 | #endif | |
1046 | ||
1047 | class CocoaWindowCompareContext | |
1048 | { | |
1049 | DECLARE_NO_COPY_CLASS(CocoaWindowCompareContext) | |
1050 | public: | |
1051 | CocoaWindowCompareContext(); // Not implemented | |
1052 | CocoaWindowCompareContext(NSView *target, NSArray *subviews) | |
1053 | { | |
1054 | m_target = target; | |
1055 | // Cocoa sorts subviews in-place.. make a copy | |
1056 | m_subviews = [subviews copy]; | |
1057 | } | |
1058 | ~CocoaWindowCompareContext() | |
1059 | { // release the copy | |
1060 | [m_subviews release]; | |
1061 | } | |
1062 | NSView* target() | |
1063 | { return m_target; } | |
1064 | NSArray* subviews() | |
1065 | { return m_subviews; } | |
1066 | /* Helper function that returns the comparison based off of the original ordering */ | |
1067 | CocoaWindowCompareFunctionResult CompareUsingOriginalOrdering(id first, id second) | |
1068 | { | |
1069 | NSUInteger firstI = [m_subviews indexOfObjectIdenticalTo:first]; | |
1070 | NSUInteger secondI = [m_subviews indexOfObjectIdenticalTo:second]; | |
1071 | // NOTE: If either firstI or secondI is NSNotFound then it will be NSIntegerMax and thus will | |
1072 | // likely compare higher than the other view which is reasonable considering the only way that | |
1073 | // can happen is if the subview was added after our call to subviews but before the call to | |
1074 | // sortSubviewsUsingFunction:context:. Thus we don't bother checking. Particularly because | |
1075 | // that case should never occur anyway because that would imply a multi-threaded GUI call | |
1076 | // which is a big no-no with Cocoa. | |
1077 | ||
1078 | // Subviews are ordered from back to front meaning one that is already lower will have an lower index. | |
1079 | NSComparisonResult result = (firstI < secondI) | |
1080 | ? NSOrderedAscending /* -1 */ | |
1081 | : (firstI > secondI) | |
1082 | ? NSOrderedDescending /* 1 */ | |
1083 | : NSOrderedSame /* 0 */; | |
1084 | ||
1085 | #if 0 // Enable this if you need to debug the algorithm. | |
1086 | NSLog(@"%@ [%d] %s %@ [%d]\n", first, firstI, comparisonresultStrings[result+1], second, secondI); | |
1087 | #endif | |
1088 | return result; | |
1089 | } | |
1090 | private: | |
1091 | /* The subview we are trying to Raise or Lower */ | |
1092 | NSView *m_target; | |
1093 | /* A copy of the original array of subviews */ | |
1094 | NSArray *m_subviews; | |
1095 | }; | |
1096 | ||
1097 | /* Causes Cocoa to raise the target view to the top of the Z-Order by telling the sort function that | |
1098 | * the target view is always higher than every other view. When comparing two views neither of | |
1099 | * which is the target, it returns the correct response based on the original ordering | |
1100 | */ | |
1101 | static CocoaWindowCompareFunctionResult CocoaRaiseWindowCompareFunction(id first, id second, void *ctx) | |
4328a6ba | 1102 | { |
aa25b7f9 | 1103 | CocoaWindowCompareContext *compareContext = (CocoaWindowCompareContext*)ctx; |
4328a6ba | 1104 | // first should be ordered higher |
aa25b7f9 | 1105 | if(first==compareContext->target()) |
4328a6ba DE |
1106 | return NSOrderedDescending; |
1107 | // second should be ordered higher | |
aa25b7f9 | 1108 | if(second==compareContext->target()) |
4328a6ba | 1109 | return NSOrderedAscending; |
aa25b7f9 | 1110 | return compareContext->CompareUsingOriginalOrdering(first,second); |
4328a6ba DE |
1111 | } |
1112 | ||
fb896a32 DE |
1113 | // Raise the window to the top of the Z order |
1114 | void wxWindow::Raise() | |
1115 | { | |
4328a6ba | 1116 | // wxAutoNSAutoreleasePool pool; |
a82b8141 | 1117 | NSView *nsview = GetNSViewForSuperview(); |
aa25b7f9 DE |
1118 | NSView *superview = [nsview superview]; |
1119 | CocoaWindowCompareContext compareContext(nsview, [superview subviews]); | |
1120 | ||
1121 | [superview sortSubviewsUsingFunction: | |
4328a6ba | 1122 | CocoaRaiseWindowCompareFunction |
aa25b7f9 | 1123 | context: &compareContext]; |
4328a6ba DE |
1124 | } |
1125 | ||
aa25b7f9 DE |
1126 | /* Causes Cocoa to lower the target view to the bottom of the Z-Order by telling the sort function that |
1127 | * the target view is always lower than every other view. When comparing two views neither of | |
1128 | * which is the target, it returns the correct response based on the original ordering | |
1129 | */ | |
1130 | static CocoaWindowCompareFunctionResult CocoaLowerWindowCompareFunction(id first, id second, void *ctx) | |
4328a6ba | 1131 | { |
aa25b7f9 | 1132 | CocoaWindowCompareContext *compareContext = (CocoaWindowCompareContext*)ctx; |
4328a6ba | 1133 | // first should be ordered lower |
aa25b7f9 | 1134 | if(first==compareContext->target()) |
4328a6ba DE |
1135 | return NSOrderedAscending; |
1136 | // second should be ordered lower | |
aa25b7f9 | 1137 | if(second==compareContext->target()) |
4328a6ba | 1138 | return NSOrderedDescending; |
aa25b7f9 | 1139 | return compareContext->CompareUsingOriginalOrdering(first,second); |
fb896a32 DE |
1140 | } |
1141 | ||
1142 | // Lower the window to the bottom of the Z order | |
1143 | void wxWindow::Lower() | |
1144 | { | |
4328a6ba | 1145 | NSView *nsview = GetNSViewForSuperview(); |
aa25b7f9 DE |
1146 | NSView *superview = [nsview superview]; |
1147 | CocoaWindowCompareContext compareContext(nsview, [superview subviews]); | |
1148 | ||
1149 | #if 0 | |
1150 | NSLog(@"Target:\n%@\n", nsview); | |
1151 | NSLog(@"Before:\n%@\n", compareContext.subviews()); | |
1152 | #endif | |
1153 | [superview sortSubviewsUsingFunction: | |
4328a6ba | 1154 | CocoaLowerWindowCompareFunction |
aa25b7f9 DE |
1155 | context: &compareContext]; |
1156 | #if 0 | |
1157 | NSLog(@"After:\n%@\n", [superview subviews]); | |
1158 | #endif | |
fb896a32 DE |
1159 | } |
1160 | ||
1161 | bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y) | |
1162 | { | |
8d8d3633 | 1163 | return false; |
fb896a32 DE |
1164 | } |
1165 | ||
1166 | // Get the window with the focus | |
dcb68102 | 1167 | wxWindow *wxWindowBase::DoFindFocus() |
fb896a32 | 1168 | { |
67d2dac8 DE |
1169 | // Basically we are somewhat emulating the responder chain here except |
1170 | // we are only loking for the first responder in the key window or | |
1171 | // upon failing to find one if the main window is different we look | |
1172 | // for the first responder in the main window. | |
1173 | ||
1174 | // Note that the firstResponder doesn't necessarily have to be an | |
1175 | // NSView but wxCocoaNSView::GetFromCocoa() will simply return | |
1176 | // NULL unless it finds its argument in its hash map. | |
1177 | ||
1178 | wxCocoaNSView *win; | |
1179 | ||
1180 | NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow]; | |
9151dcec | 1181 | win = wxCocoaNSView::GetFromCocoa(static_cast<NSView*>([keyWindow firstResponder])); |
67d2dac8 DE |
1182 | if(win) |
1183 | return win->GetWxWindow(); | |
1184 | ||
1185 | NSWindow *mainWindow = [[NSApplication sharedApplication] keyWindow]; | |
1186 | if(mainWindow == keyWindow) | |
f7e98dee | 1187 | return NULL; |
9151dcec | 1188 | win = wxCocoaNSView::GetFromCocoa(static_cast<NSView*>([mainWindow firstResponder])); |
67d2dac8 DE |
1189 | if(win) |
1190 | return win->GetWxWindow(); | |
1191 | ||
1192 | return NULL; | |
fb896a32 DE |
1193 | } |
1194 | ||
1195 | /* static */ wxWindow *wxWindowBase::GetCapture() | |
1196 | { | |
1197 | // TODO | |
b9505233 | 1198 | return wxWindowCocoa::sm_capturedWindow; |
fb896a32 DE |
1199 | } |
1200 | ||
1201 | wxWindow *wxGetActiveWindow() | |
1202 | { | |
1203 | // TODO | |
1204 | return NULL; | |
1205 | } | |
1206 | ||
7c9428ab DE |
1207 | wxPoint wxGetMousePosition() |
1208 | { | |
1209 | // TODO | |
1210 | return wxDefaultPosition; | |
1211 | } | |
1212 | ||
ca5db7b2 WS |
1213 | wxMouseState wxGetMouseState() |
1214 | { | |
1215 | wxMouseState ms; | |
1216 | // TODO | |
1217 | return ms; | |
1218 | } | |
1219 | ||
7c9428ab DE |
1220 | wxWindow* wxFindWindowAtPointer(wxPoint& pt) |
1221 | { | |
1222 | pt = wxGetMousePosition(); | |
1223 | return NULL; | |
1224 | } | |
7c5a378f DE |
1225 | |
1226 | ||
1227 | // ======================================================================== | |
1228 | // wxCocoaTrackingRectManager | |
1229 | // ======================================================================== | |
1230 | ||
1231 | wxCocoaTrackingRectManager::wxCocoaTrackingRectManager(wxWindow *window) | |
1232 | : m_window(window) | |
1233 | { | |
1234 | m_isTrackingRectActive = false; | |
1235 | m_runLoopObserver = NULL; | |
1236 | BuildTrackingRect(); | |
1237 | } | |
1238 | ||
1239 | void wxCocoaTrackingRectManager::ClearTrackingRect() | |
1240 | { | |
1241 | if(m_isTrackingRectActive) | |
1242 | { | |
1243 | [m_window->GetNSView() removeTrackingRect:m_trackingRectTag]; | |
1244 | m_isTrackingRectActive = false; | |
1245 | } | |
1246 | // If we were doing periodic events we need to clear those too | |
1247 | StopSynthesizingEvents(); | |
1248 | } | |
1249 | ||
1250 | void wxCocoaTrackingRectManager::StopSynthesizingEvents() | |
1251 | { | |
1252 | if(m_runLoopObserver != NULL) | |
1253 | { | |
1254 | CFRunLoopRemoveObserver([[NSRunLoop currentRunLoop] getCFRunLoop], m_runLoopObserver, kCFRunLoopCommonModes); | |
1255 | CFRelease(m_runLoopObserver); | |
1256 | m_runLoopObserver = NULL; | |
1257 | } | |
1258 | } | |
1259 | ||
1260 | void wxCocoaTrackingRectManager::BuildTrackingRect() | |
1261 | { | |
b0a207df DE |
1262 | // Pool here due to lack of one during wx init phase |
1263 | wxAutoNSAutoreleasePool pool; | |
1264 | ||
7c5a378f DE |
1265 | wxASSERT_MSG(!m_isTrackingRectActive, wxT("Tracking rect was not cleared")); |
1266 | if([m_window->GetNSView() window] != nil) | |
1267 | { | |
1268 | m_trackingRectTag = [m_window->GetNSView() addTrackingRect:[m_window->GetNSView() visibleRect] owner:m_window->GetNSView() userData:NULL assumeInside:NO]; | |
1269 | m_isTrackingRectActive = true; | |
1270 | } | |
1271 | } | |
1272 | ||
1273 | static NSPoint s_lastScreenMouseLocation = NSZeroPoint; | |
1274 | ||
1275 | static void SynthesizeMouseMovedEvent(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info) | |
1276 | { | |
1277 | NSPoint screenMouseLocation = [NSEvent mouseLocation]; | |
1278 | if(screenMouseLocation.x != s_lastScreenMouseLocation.x || screenMouseLocation.y != s_lastScreenMouseLocation.y) | |
1279 | { | |
1280 | wxCocoaNSView *win = reinterpret_cast<wxCocoaNSView*>(info); | |
1281 | win->Cocoa_synthesizeMouseMoved(); | |
1282 | } | |
1283 | } | |
1284 | ||
1285 | void wxCocoaTrackingRectManager::BeginSynthesizingEvents() | |
1286 | { | |
1287 | CFRunLoopObserverContext observerContext = | |
1288 | { 0 | |
1289 | , static_cast<wxCocoaNSView*>(m_window) | |
1290 | , NULL | |
1291 | , NULL | |
1292 | , NULL | |
1293 | }; | |
1294 | m_runLoopObserver = CFRunLoopObserverCreate(kCFAllocatorDefault, kCFRunLoopBeforeWaiting, TRUE, 0, SynthesizeMouseMovedEvent, &observerContext); | |
1295 | CFRunLoopAddObserver([[NSRunLoop currentRunLoop] getCFRunLoop], m_runLoopObserver, kCFRunLoopCommonModes); | |
1296 | } | |
1297 | ||
1298 | void wxCocoaTrackingRectManager::RebuildTrackingRect() | |
1299 | { | |
1300 | ClearTrackingRect(); | |
1301 | BuildTrackingRect(); | |
1302 | } | |
1303 | ||
1304 | wxCocoaTrackingRectManager::~wxCocoaTrackingRectManager() | |
1305 | { | |
1306 | ClearTrackingRect(); | |
1307 | } | |
1308 | ||
1309 | bool wxCocoaTrackingRectManager::IsOwnerOfEvent(NSEvent *anEvent) | |
1310 | { | |
1311 | return m_isTrackingRectActive && (m_trackingRectTag == [anEvent trackingNumber]); | |
1312 | } | |
1313 |