]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/cocoa/window.mm | |
3 | // Purpose: wxWindowCocoa | |
4 | // Author: David Elliott | |
5 | // Modified by: | |
6 | // Created: 2002/12/26 | |
7 | // RCS-ID: $Id: | |
8 | // Copyright: (c) 2002 David Elliott | |
9 | // Licence: wxWidgets licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #include "wx/wxprec.h" | |
13 | #ifndef WX_PRECOMP | |
14 | #include "wx/log.h" | |
15 | #include "wx/window.h" | |
16 | #include "wx/dc.h" | |
17 | #endif //WX_PRECOMP | |
18 | #include "wx/tooltip.h" | |
19 | ||
20 | #include "wx/cocoa/autorelease.h" | |
21 | #include "wx/cocoa/string.h" | |
22 | ||
23 | #import <AppKit/NSView.h> | |
24 | #import <AppKit/NSEvent.h> | |
25 | #import <AppKit/NSScrollView.h> | |
26 | #import <AppKit/NSColor.h> | |
27 | #import <AppKit/NSClipView.h> | |
28 | #import <Foundation/NSException.h> | |
29 | #import <AppKit/NSApplication.h> | |
30 | #import <AppKit/NSWindow.h> | |
31 | ||
32 | // Turn this on to paint green over the dummy views for debugging | |
33 | #undef WXCOCOA_FILL_DUMMY_VIEW | |
34 | ||
35 | #ifdef WXCOCOA_FILL_DUMMY_VIEW | |
36 | #import <AppKit/NSBezierPath.h> | |
37 | #endif //def WXCOCOA_FILL_DUMMY_VIEW | |
38 | ||
39 | // A category for methods that are only present in Panther's SDK | |
40 | @interface NSView(wxNSViewPrePantherCompatibility) | |
41 | - (void)getRectsBeingDrawn:(const NSRect **)rects count:(int *)count; | |
42 | @end | |
43 | ||
44 | NSPoint CocoaTransformNSViewBoundsToWx(NSView *nsview, NSPoint pointBounds) | |
45 | { | |
46 | wxCHECK_MSG(nsview, pointBounds, wxT("Need to have a Cocoa view to do translation")); | |
47 | if([nsview isFlipped]) | |
48 | return pointBounds; | |
49 | NSRect ourBounds = [nsview bounds]; | |
50 | return NSMakePoint | |
51 | ( pointBounds.x | |
52 | , ourBounds.size.height - pointBounds.y | |
53 | ); | |
54 | } | |
55 | ||
56 | NSRect CocoaTransformNSViewBoundsToWx(NSView *nsview, NSRect rectBounds) | |
57 | { | |
58 | wxCHECK_MSG(nsview, rectBounds, wxT("Need to have a Cocoa view to do translation")); | |
59 | if([nsview isFlipped]) | |
60 | return rectBounds; | |
61 | NSRect ourBounds = [nsview bounds]; | |
62 | return NSMakeRect | |
63 | ( rectBounds.origin.x | |
64 | , ourBounds.size.height - (rectBounds.origin.y + rectBounds.size.height) | |
65 | , rectBounds.size.width | |
66 | , rectBounds.size.height | |
67 | ); | |
68 | } | |
69 | ||
70 | NSPoint CocoaTransformNSViewWxToBounds(NSView *nsview, NSPoint pointWx) | |
71 | { | |
72 | wxCHECK_MSG(nsview, pointWx, wxT("Need to have a Cocoa view to do translation")); | |
73 | if([nsview isFlipped]) | |
74 | return pointWx; | |
75 | NSRect ourBounds = [nsview bounds]; | |
76 | return NSMakePoint | |
77 | ( pointWx.x | |
78 | , ourBounds.size.height - pointWx.y | |
79 | ); | |
80 | } | |
81 | ||
82 | NSRect CocoaTransformNSViewWxToBounds(NSView *nsview, NSRect rectWx) | |
83 | { | |
84 | wxCHECK_MSG(nsview, rectWx, wxT("Need to have a Cocoa view to do translation")); | |
85 | if([nsview isFlipped]) | |
86 | return rectWx; | |
87 | NSRect ourBounds = [nsview bounds]; | |
88 | return NSMakeRect | |
89 | ( rectWx.origin.x | |
90 | , ourBounds.size.height - (rectWx.origin.y + rectWx.size.height) | |
91 | , rectWx.size.width | |
92 | , rectWx.size.height | |
93 | ); | |
94 | } | |
95 | ||
96 | // ======================================================================== | |
97 | // wxWindowCocoaHider | |
98 | // ======================================================================== | |
99 | class wxWindowCocoaHider: protected wxCocoaNSView | |
100 | { | |
101 | DECLARE_NO_COPY_CLASS(wxWindowCocoaHider) | |
102 | public: | |
103 | wxWindowCocoaHider(wxWindow *owner); | |
104 | virtual ~wxWindowCocoaHider(); | |
105 | inline WX_NSView GetNSView() { return m_dummyNSView; } | |
106 | protected: | |
107 | wxWindowCocoa *m_owner; | |
108 | WX_NSView m_dummyNSView; | |
109 | virtual void Cocoa_FrameChanged(void); | |
110 | #ifdef WXCOCOA_FILL_DUMMY_VIEW | |
111 | virtual bool Cocoa_drawRect(const NSRect& rect); | |
112 | #endif //def WXCOCOA_FILL_DUMMY_VIEW | |
113 | private: | |
114 | wxWindowCocoaHider(); | |
115 | }; | |
116 | ||
117 | // ======================================================================== | |
118 | // wxWindowCocoaScrollView | |
119 | // ======================================================================== | |
120 | class wxWindowCocoaScrollView: protected wxCocoaNSView | |
121 | { | |
122 | DECLARE_NO_COPY_CLASS(wxWindowCocoaScrollView) | |
123 | public: | |
124 | wxWindowCocoaScrollView(wxWindow *owner); | |
125 | virtual ~wxWindowCocoaScrollView(); | |
126 | inline WX_NSScrollView GetNSScrollView() { return m_cocoaNSScrollView; } | |
127 | void ClientSizeToSize(int &width, int &height); | |
128 | void DoGetClientSize(int *x, int *y) const; | |
129 | void Encapsulate(); | |
130 | void Unencapsulate(); | |
131 | protected: | |
132 | wxWindowCocoa *m_owner; | |
133 | WX_NSScrollView m_cocoaNSScrollView; | |
134 | virtual void Cocoa_FrameChanged(void); | |
135 | private: | |
136 | wxWindowCocoaScrollView(); | |
137 | }; | |
138 | ||
139 | // ======================================================================== | |
140 | // wxDummyNSView | |
141 | // ======================================================================== | |
142 | @interface wxDummyNSView : NSView | |
143 | - (NSView *)hitTest:(NSPoint)aPoint; | |
144 | @end | |
145 | ||
146 | @implementation wxDummyNSView : NSView | |
147 | - (NSView *)hitTest:(NSPoint)aPoint | |
148 | { | |
149 | return nil; | |
150 | } | |
151 | ||
152 | @end | |
153 | ||
154 | // ======================================================================== | |
155 | // wxWindowCocoaHider | |
156 | // ======================================================================== | |
157 | wxWindowCocoaHider::wxWindowCocoaHider(wxWindow *owner) | |
158 | : m_owner(owner) | |
159 | { | |
160 | wxASSERT(owner); | |
161 | wxASSERT(owner->GetNSViewForHiding()); | |
162 | m_dummyNSView = [[wxDummyNSView alloc] | |
163 | initWithFrame:[owner->GetNSViewForHiding() frame]]; | |
164 | [m_dummyNSView setAutoresizingMask: [owner->GetNSViewForHiding() autoresizingMask]]; | |
165 | AssociateNSView(m_dummyNSView); | |
166 | } | |
167 | ||
168 | wxWindowCocoaHider::~wxWindowCocoaHider() | |
169 | { | |
170 | DisassociateNSView(m_dummyNSView); | |
171 | [m_dummyNSView release]; | |
172 | } | |
173 | ||
174 | void wxWindowCocoaHider::Cocoa_FrameChanged(void) | |
175 | { | |
176 | // Keep the real window in synch with the dummy | |
177 | wxASSERT(m_dummyNSView); | |
178 | [m_owner->GetNSViewForHiding() setFrame:[m_dummyNSView frame]]; | |
179 | } | |
180 | ||
181 | ||
182 | #ifdef WXCOCOA_FILL_DUMMY_VIEW | |
183 | bool wxWindowCocoaHider::Cocoa_drawRect(const NSRect& rect) | |
184 | { | |
185 | NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:rect]; | |
186 | [[NSColor greenColor] set]; | |
187 | [bezpath stroke]; | |
188 | [bezpath fill]; | |
189 | return true; | |
190 | } | |
191 | #endif //def WXCOCOA_FILL_DUMMY_VIEW | |
192 | ||
193 | // ======================================================================== | |
194 | // wxFlippedNSClipView | |
195 | // ======================================================================== | |
196 | @interface wxFlippedNSClipView : NSClipView | |
197 | - (BOOL)isFlipped; | |
198 | @end | |
199 | ||
200 | @implementation wxFlippedNSClipView : NSClipView | |
201 | - (BOOL)isFlipped | |
202 | { | |
203 | return YES; | |
204 | } | |
205 | ||
206 | @end | |
207 | ||
208 | // ======================================================================== | |
209 | // wxWindowCocoaScrollView | |
210 | // ======================================================================== | |
211 | wxWindowCocoaScrollView::wxWindowCocoaScrollView(wxWindow *owner) | |
212 | : m_owner(owner) | |
213 | { | |
214 | wxAutoNSAutoreleasePool pool; | |
215 | wxASSERT(owner); | |
216 | wxASSERT(owner->GetNSView()); | |
217 | m_cocoaNSScrollView = [[NSScrollView alloc] | |
218 | initWithFrame:[owner->GetNSView() frame]]; | |
219 | AssociateNSView(m_cocoaNSScrollView); | |
220 | ||
221 | /* Replace the default NSClipView with a flipped one. This ensures | |
222 | scrolling is "pinned" to the top-left instead of bottom-right. */ | |
223 | NSClipView *flippedClip = [[wxFlippedNSClipView alloc] | |
224 | initWithFrame: [[m_cocoaNSScrollView contentView] frame]]; | |
225 | [m_cocoaNSScrollView setContentView:flippedClip]; | |
226 | [flippedClip release]; | |
227 | ||
228 | [m_cocoaNSScrollView setBackgroundColor: [NSColor windowBackgroundColor]]; | |
229 | [m_cocoaNSScrollView setHasHorizontalScroller: YES]; | |
230 | [m_cocoaNSScrollView setHasVerticalScroller: YES]; | |
231 | Encapsulate(); | |
232 | } | |
233 | ||
234 | void wxWindowCocoaScrollView::Encapsulate() | |
235 | { | |
236 | // Set the scroll view autoresizingMask to match the current NSView | |
237 | [m_cocoaNSScrollView setAutoresizingMask: [m_owner->GetNSView() autoresizingMask]]; | |
238 | [m_owner->GetNSView() setAutoresizingMask: NSViewNotSizable]; | |
239 | // NOTE: replaceSubView will cause m_cocaNSView to be released | |
240 | // except when it hasn't been added into an NSView hierarchy in which | |
241 | // case it doesn't need to be and this should work out to a no-op | |
242 | m_owner->CocoaReplaceView(m_owner->GetNSView(), m_cocoaNSScrollView); | |
243 | // The NSView is still retained by owner | |
244 | [m_cocoaNSScrollView setDocumentView: m_owner->GetNSView()]; | |
245 | // Now it's also retained by the NSScrollView | |
246 | } | |
247 | ||
248 | void wxWindowCocoaScrollView::Unencapsulate() | |
249 | { | |
250 | [m_cocoaNSScrollView setDocumentView: nil]; | |
251 | m_owner->CocoaReplaceView(m_cocoaNSScrollView, m_owner->GetNSView()); | |
252 | if(![[m_owner->GetNSView() superview] isFlipped]) | |
253 | [m_owner->GetNSView() setAutoresizingMask: NSViewMinYMargin]; | |
254 | } | |
255 | ||
256 | wxWindowCocoaScrollView::~wxWindowCocoaScrollView() | |
257 | { | |
258 | DisassociateNSView(m_cocoaNSScrollView); | |
259 | [m_cocoaNSScrollView release]; | |
260 | } | |
261 | ||
262 | void wxWindowCocoaScrollView::ClientSizeToSize(int &width, int &height) | |
263 | { | |
264 | NSSize frameSize = [NSScrollView | |
265 | frameSizeForContentSize: NSMakeSize(width,height) | |
266 | hasHorizontalScroller: [m_cocoaNSScrollView hasHorizontalScroller] | |
267 | hasVerticalScroller: [m_cocoaNSScrollView hasVerticalScroller] | |
268 | borderType: [m_cocoaNSScrollView borderType]]; | |
269 | width = (int)frameSize.width; | |
270 | height = (int)frameSize.height; | |
271 | } | |
272 | ||
273 | void wxWindowCocoaScrollView::DoGetClientSize(int *x, int *y) const | |
274 | { | |
275 | NSSize nssize = [m_cocoaNSScrollView contentSize]; | |
276 | if(x) | |
277 | *x = (int)nssize.width; | |
278 | if(y) | |
279 | *y = (int)nssize.height; | |
280 | } | |
281 | ||
282 | void wxWindowCocoaScrollView::Cocoa_FrameChanged(void) | |
283 | { | |
284 | wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_FrameChanged")); | |
285 | wxSizeEvent event(m_owner->GetSize(), m_owner->GetId()); | |
286 | event.SetEventObject(m_owner); | |
287 | m_owner->GetEventHandler()->ProcessEvent(event); | |
288 | } | |
289 | ||
290 | // ======================================================================== | |
291 | // wxWindowCocoa | |
292 | // ======================================================================== | |
293 | // normally the base classes aren't included, but wxWindow is special | |
294 | #ifdef __WXUNIVERSAL__ | |
295 | IMPLEMENT_ABSTRACT_CLASS(wxWindowCocoa, wxWindowBase) | |
296 | #else | |
297 | IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase) | |
298 | #endif | |
299 | ||
300 | BEGIN_EVENT_TABLE(wxWindowCocoa, wxWindowBase) | |
301 | END_EVENT_TABLE() | |
302 | ||
303 | wxWindow *wxWindowCocoa::sm_capturedWindow = NULL; | |
304 | ||
305 | // Constructor | |
306 | void wxWindowCocoa::Init() | |
307 | { | |
308 | m_cocoaNSView = NULL; | |
309 | m_cocoaHider = NULL; | |
310 | m_wxCocoaScrollView = NULL; | |
311 | m_isBeingDeleted = FALSE; | |
312 | m_isInPaint = FALSE; | |
313 | m_shouldBeEnabled = true; | |
314 | } | |
315 | ||
316 | // Constructor | |
317 | bool wxWindow::Create(wxWindow *parent, wxWindowID winid, | |
318 | const wxPoint& pos, | |
319 | const wxSize& size, | |
320 | long style, | |
321 | const wxString& name) | |
322 | { | |
323 | if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name)) | |
324 | return false; | |
325 | ||
326 | // TODO: create the window | |
327 | m_cocoaNSView = NULL; | |
328 | SetNSView([[NSView alloc] initWithFrame: MakeDefaultNSRect(size)]); | |
329 | [m_cocoaNSView release]; | |
330 | ||
331 | if (m_parent) | |
332 | { | |
333 | m_parent->AddChild(this); | |
334 | m_parent->CocoaAddChild(this); | |
335 | SetInitialFrameRect(pos,size); | |
336 | } | |
337 | ||
338 | return TRUE; | |
339 | } | |
340 | ||
341 | // Destructor | |
342 | wxWindow::~wxWindow() | |
343 | { | |
344 | wxAutoNSAutoreleasePool pool; | |
345 | DestroyChildren(); | |
346 | ||
347 | // Make sure our parent (in the wxWidgets sense) is our superview | |
348 | // before we go removing from it. | |
349 | if(m_parent && m_parent->GetNSView()==[GetNSViewForSuperview() superview]) | |
350 | CocoaRemoveFromParent(); | |
351 | delete m_cocoaHider; | |
352 | delete m_wxCocoaScrollView; | |
353 | if(m_cocoaNSView) | |
354 | SendDestroyEvent(); | |
355 | SetNSView(NULL); | |
356 | } | |
357 | ||
358 | void wxWindowCocoa::CocoaAddChild(wxWindowCocoa *child) | |
359 | { | |
360 | NSView *childView = child->GetNSViewForSuperview(); | |
361 | ||
362 | wxASSERT(childView); | |
363 | [m_cocoaNSView addSubview: childView]; | |
364 | child->m_isShown = !m_cocoaHider; | |
365 | } | |
366 | ||
367 | void wxWindowCocoa::CocoaRemoveFromParent(void) | |
368 | { | |
369 | [GetNSViewForSuperview() removeFromSuperview]; | |
370 | } | |
371 | ||
372 | void wxWindowCocoa::SetNSView(WX_NSView cocoaNSView) | |
373 | { | |
374 | bool need_debug = cocoaNSView || m_cocoaNSView; | |
375 | if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxWindowCocoa=%p::SetNSView [m_cocoaNSView=%p retainCount]=%d"),this,m_cocoaNSView,[m_cocoaNSView retainCount]); | |
376 | DisassociateNSView(m_cocoaNSView); | |
377 | [cocoaNSView retain]; | |
378 | [m_cocoaNSView release]; | |
379 | m_cocoaNSView = cocoaNSView; | |
380 | AssociateNSView(m_cocoaNSView); | |
381 | if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxWindowCocoa=%p::SetNSView [cocoaNSView=%p retainCount]=%d"),this,cocoaNSView,[cocoaNSView retainCount]); | |
382 | } | |
383 | ||
384 | WX_NSView wxWindowCocoa::GetNSViewForSuperview() const | |
385 | { | |
386 | return m_cocoaHider | |
387 | ? m_cocoaHider->GetNSView() | |
388 | : m_wxCocoaScrollView | |
389 | ? m_wxCocoaScrollView->GetNSScrollView() | |
390 | : m_cocoaNSView; | |
391 | } | |
392 | ||
393 | WX_NSView wxWindowCocoa::GetNSViewForHiding() const | |
394 | { | |
395 | return m_wxCocoaScrollView | |
396 | ? m_wxCocoaScrollView->GetNSScrollView() | |
397 | : m_cocoaNSView; | |
398 | } | |
399 | ||
400 | NSPoint wxWindowCocoa::CocoaTransformBoundsToWx(NSPoint pointBounds) | |
401 | { | |
402 | // TODO: Handle scrolling offset | |
403 | return CocoaTransformNSViewBoundsToWx(GetNSView(), pointBounds); | |
404 | } | |
405 | ||
406 | NSRect wxWindowCocoa::CocoaTransformBoundsToWx(NSRect rectBounds) | |
407 | { | |
408 | // TODO: Handle scrolling offset | |
409 | return CocoaTransformNSViewBoundsToWx(GetNSView(), rectBounds); | |
410 | } | |
411 | ||
412 | NSPoint wxWindowCocoa::CocoaTransformWxToBounds(NSPoint pointWx) | |
413 | { | |
414 | // TODO: Handle scrolling offset | |
415 | return CocoaTransformNSViewWxToBounds(GetNSView(), pointWx); | |
416 | } | |
417 | ||
418 | NSRect wxWindowCocoa::CocoaTransformWxToBounds(NSRect rectWx) | |
419 | { | |
420 | // TODO: Handle scrolling offset | |
421 | return CocoaTransformNSViewWxToBounds(GetNSView(), rectWx); | |
422 | } | |
423 | ||
424 | WX_NSAffineTransform wxWindowCocoa::CocoaGetWxToBoundsTransform() | |
425 | { | |
426 | // TODO: Handle scrolling offset | |
427 | NSAffineTransform *transform = wxDC::CocoaGetWxToBoundsTransform([GetNSView() isFlipped], [GetNSView() bounds].size.height); | |
428 | return transform; | |
429 | } | |
430 | ||
431 | bool wxWindowCocoa::Cocoa_drawRect(const NSRect &rect) | |
432 | { | |
433 | wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_drawRect")); | |
434 | // Recursion can happen if the event loop runs from within the paint | |
435 | // handler. For instance, if an assertion dialog is shown. | |
436 | // FIXME: This seems less than ideal. | |
437 | if(m_isInPaint) | |
438 | { | |
439 | wxLogDebug(wxT("Paint event recursion!")); | |
440 | return false; | |
441 | } | |
442 | m_isInPaint = TRUE; | |
443 | ||
444 | // Set m_updateRegion | |
445 | const NSRect *rects = ▭ // The bounding box of the region | |
446 | int countRects = 1; | |
447 | // Try replacing the larger rectangle with a list of smaller ones: | |
448 | if ([GetNSView() respondsToSelector:@selector(getRectsBeingDrawn:count:)]) | |
449 | [GetNSView() getRectsBeingDrawn:&rects count:&countRects]; | |
450 | ||
451 | NSRect *transformedRects = (NSRect*)malloc(sizeof(NSRect)*countRects); | |
452 | for(int i=0; i<countRects; i++) | |
453 | { | |
454 | transformedRects[i] = CocoaTransformBoundsToWx(rects[i]); | |
455 | } | |
456 | m_updateRegion = wxRegion(transformedRects,countRects); | |
457 | free(transformedRects); | |
458 | ||
459 | wxPaintEvent event(m_windowId); | |
460 | event.SetEventObject(this); | |
461 | bool ret = GetEventHandler()->ProcessEvent(event); | |
462 | m_isInPaint = FALSE; | |
463 | return ret; | |
464 | } | |
465 | ||
466 | void wxWindowCocoa::InitMouseEvent(wxMouseEvent& event, WX_NSEvent cocoaEvent) | |
467 | { | |
468 | wxASSERT_MSG([m_cocoaNSView window]==[cocoaEvent window],wxT("Mouse event for different NSWindow")); | |
469 | // Mouse events happen at the NSWindow level so we need to convert | |
470 | // into our bounds coordinates then convert to wx coordinates. | |
471 | NSPoint cocoaPoint = [m_cocoaNSView convertPoint:[(NSEvent*)cocoaEvent locationInWindow] fromView:nil]; | |
472 | NSPoint pointWx = CocoaTransformBoundsToWx(cocoaPoint); | |
473 | // FIXME: Should we be adjusting for client area origin? | |
474 | const wxPoint &clientorigin = GetClientAreaOrigin(); | |
475 | event.m_x = (wxCoord)pointWx.x - clientorigin.x; | |
476 | event.m_y = (wxCoord)pointWx.y - clientorigin.y; | |
477 | ||
478 | event.m_shiftDown = [cocoaEvent modifierFlags] & NSShiftKeyMask; | |
479 | event.m_controlDown = [cocoaEvent modifierFlags] & NSControlKeyMask; | |
480 | event.m_altDown = [cocoaEvent modifierFlags] & NSAlternateKeyMask; | |
481 | event.m_metaDown = [cocoaEvent modifierFlags] & NSCommandKeyMask; | |
482 | ||
483 | // TODO: set timestamp? | |
484 | event.SetEventObject(this); | |
485 | event.SetId(GetId()); | |
486 | } | |
487 | ||
488 | bool wxWindowCocoa::Cocoa_mouseMoved(WX_NSEvent theEvent) | |
489 | { | |
490 | wxMouseEvent event(wxEVT_MOTION); | |
491 | InitMouseEvent(event,theEvent); | |
492 | wxLogTrace(wxTRACE_COCOA,wxT("Mouse Drag @%d,%d"),event.m_x,event.m_y); | |
493 | return GetEventHandler()->ProcessEvent(event); | |
494 | } | |
495 | ||
496 | bool wxWindowCocoa::Cocoa_mouseEntered(WX_NSEvent theEvent) | |
497 | { | |
498 | return false; | |
499 | } | |
500 | ||
501 | bool wxWindowCocoa::Cocoa_mouseExited(WX_NSEvent theEvent) | |
502 | { | |
503 | return false; | |
504 | } | |
505 | ||
506 | bool wxWindowCocoa::Cocoa_mouseDown(WX_NSEvent theEvent) | |
507 | { | |
508 | wxMouseEvent event([theEvent clickCount]<2?wxEVT_LEFT_DOWN:wxEVT_LEFT_DCLICK); | |
509 | InitMouseEvent(event,theEvent); | |
510 | wxLogTrace(wxTRACE_COCOA,wxT("Mouse Down @%d,%d num clicks=%d"),event.m_x,event.m_y,[theEvent clickCount]); | |
511 | return GetEventHandler()->ProcessEvent(event); | |
512 | } | |
513 | ||
514 | bool wxWindowCocoa::Cocoa_mouseDragged(WX_NSEvent theEvent) | |
515 | { | |
516 | wxMouseEvent event(wxEVT_MOTION); | |
517 | InitMouseEvent(event,theEvent); | |
518 | event.m_leftDown = true; | |
519 | wxLogTrace(wxTRACE_COCOA,wxT("Mouse Drag @%d,%d"),event.m_x,event.m_y); | |
520 | return GetEventHandler()->ProcessEvent(event); | |
521 | } | |
522 | ||
523 | bool wxWindowCocoa::Cocoa_mouseUp(WX_NSEvent theEvent) | |
524 | { | |
525 | wxMouseEvent event(wxEVT_LEFT_UP); | |
526 | InitMouseEvent(event,theEvent); | |
527 | wxLogTrace(wxTRACE_COCOA,wxT("Mouse Up @%d,%d"),event.m_x,event.m_y); | |
528 | return GetEventHandler()->ProcessEvent(event); | |
529 | } | |
530 | ||
531 | bool wxWindowCocoa::Cocoa_rightMouseDown(WX_NSEvent theEvent) | |
532 | { | |
533 | wxMouseEvent event([theEvent clickCount]<2?wxEVT_RIGHT_DOWN:wxEVT_RIGHT_DCLICK); | |
534 | InitMouseEvent(event,theEvent); | |
535 | wxLogDebug(wxT("Mouse Down @%d,%d num clicks=%d"),event.m_x,event.m_y,[theEvent clickCount]); | |
536 | return GetEventHandler()->ProcessEvent(event); | |
537 | } | |
538 | ||
539 | bool wxWindowCocoa::Cocoa_rightMouseDragged(WX_NSEvent theEvent) | |
540 | { | |
541 | wxMouseEvent event(wxEVT_MOTION); | |
542 | InitMouseEvent(event,theEvent); | |
543 | event.m_rightDown = true; | |
544 | wxLogDebug(wxT("Mouse Drag @%d,%d"),event.m_x,event.m_y); | |
545 | return GetEventHandler()->ProcessEvent(event); | |
546 | } | |
547 | ||
548 | bool wxWindowCocoa::Cocoa_rightMouseUp(WX_NSEvent theEvent) | |
549 | { | |
550 | wxMouseEvent event(wxEVT_RIGHT_UP); | |
551 | InitMouseEvent(event,theEvent); | |
552 | wxLogDebug(wxT("Mouse Up @%d,%d"),event.m_x,event.m_y); | |
553 | return GetEventHandler()->ProcessEvent(event); | |
554 | } | |
555 | ||
556 | bool wxWindowCocoa::Cocoa_otherMouseDown(WX_NSEvent theEvent) | |
557 | { | |
558 | return false; | |
559 | } | |
560 | ||
561 | bool wxWindowCocoa::Cocoa_otherMouseDragged(WX_NSEvent theEvent) | |
562 | { | |
563 | return false; | |
564 | } | |
565 | ||
566 | bool wxWindowCocoa::Cocoa_otherMouseUp(WX_NSEvent theEvent) | |
567 | { | |
568 | return false; | |
569 | } | |
570 | ||
571 | void wxWindowCocoa::Cocoa_FrameChanged(void) | |
572 | { | |
573 | wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_FrameChanged")); | |
574 | wxSizeEvent event(GetSize(), m_windowId); | |
575 | event.SetEventObject(this); | |
576 | GetEventHandler()->ProcessEvent(event); | |
577 | } | |
578 | ||
579 | bool wxWindowCocoa::Cocoa_resetCursorRects() | |
580 | { | |
581 | if(!m_cursor.GetNSCursor()) | |
582 | return false; | |
583 | ||
584 | [GetNSView() addCursorRect: [GetNSView() visibleRect] cursor: m_cursor.GetNSCursor()]; | |
585 | ||
586 | return true; | |
587 | } | |
588 | ||
589 | bool wxWindow::Close(bool force) | |
590 | { | |
591 | // The only reason this function exists is that it is virtual and | |
592 | // wxTopLevelWindowCocoa will override it. | |
593 | return wxWindowBase::Close(force); | |
594 | } | |
595 | ||
596 | void wxWindow::CocoaReplaceView(WX_NSView oldView, WX_NSView newView) | |
597 | { | |
598 | [[oldView superview] replaceSubview:oldView with:newView]; | |
599 | } | |
600 | ||
601 | bool wxWindow::EnableSelfAndChildren(bool enable) | |
602 | { | |
603 | // If the state isn't changing, don't do anything | |
604 | if(!wxWindowBase::Enable(enable && m_shouldBeEnabled)) | |
605 | return false; | |
606 | // Set the state of the Cocoa window | |
607 | CocoaSetEnabled(m_isEnabled); | |
608 | // Disable all children or (if enabling) return them to their proper state | |
609 | for(wxWindowList::compatibility_iterator node = GetChildren().GetFirst(); | |
610 | node; node = node->GetNext()) | |
611 | { | |
612 | node->GetData()->EnableSelfAndChildren(enable); | |
613 | } | |
614 | return true; | |
615 | } | |
616 | ||
617 | bool wxWindow::Enable(bool enable) | |
618 | { | |
619 | // Keep track of what the window SHOULD be doing | |
620 | m_shouldBeEnabled = enable; | |
621 | // If the parent is disabled for any reason, then this window will be too. | |
622 | if(!IsTopLevel() && GetParent()) | |
623 | { | |
624 | enable = enable && GetParent()->IsEnabled(); | |
625 | } | |
626 | return EnableSelfAndChildren(enable); | |
627 | } | |
628 | ||
629 | bool wxWindow::Show(bool show) | |
630 | { | |
631 | wxAutoNSAutoreleasePool pool; | |
632 | // If the window is marked as visible, then it shouldn't have a dummy view | |
633 | // If the window is marked hidden, then it should have a dummy view | |
634 | // wxSpinCtrl (generic) abuses m_isShown, don't use it for any logic | |
635 | // wxASSERT_MSG( (m_isShown && !m_dummyNSView) || (!m_isShown && m_dummyNSView),wxT("wxWindow: m_isShown does not agree with m_dummyNSView")); | |
636 | // Return false if there isn't a window to show or hide | |
637 | NSView *cocoaView = GetNSViewForHiding(); | |
638 | if(!cocoaView) | |
639 | return false; | |
640 | if(show) | |
641 | { | |
642 | // If state isn't changing, return false | |
643 | if(!m_cocoaHider) | |
644 | return false; | |
645 | CocoaReplaceView(m_cocoaHider->GetNSView(), cocoaView); | |
646 | wxASSERT(![m_cocoaHider->GetNSView() superview]); | |
647 | delete m_cocoaHider; | |
648 | m_cocoaHider = NULL; | |
649 | wxASSERT([cocoaView superview]); | |
650 | } | |
651 | else | |
652 | { | |
653 | // If state isn't changing, return false | |
654 | if(m_cocoaHider) | |
655 | return false; | |
656 | m_cocoaHider = new wxWindowCocoaHider(this); | |
657 | // NOTE: replaceSubview:with will cause m_cocaNSView to be | |
658 | // (auto)released which balances out addSubview | |
659 | CocoaReplaceView(cocoaView, m_cocoaHider->GetNSView()); | |
660 | // m_coocaNSView is now only retained by us | |
661 | wxASSERT([m_cocoaHider->GetNSView() superview]); | |
662 | wxASSERT(![cocoaView superview]); | |
663 | } | |
664 | m_isShown = show; | |
665 | return true; | |
666 | } | |
667 | ||
668 | void wxWindowCocoa::DoSetSize(int x, int y, int width, int height, int sizeFlags) | |
669 | { | |
670 | 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":"."); | |
671 | int currentX, currentY; | |
672 | int currentW, currentH; | |
673 | DoGetPosition(¤tX, ¤tY); | |
674 | DoGetSize(¤tW, ¤tH); | |
675 | if((x==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE)) | |
676 | x=currentX; | |
677 | if((y==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE)) | |
678 | y=currentY; | |
679 | ||
680 | AdjustForParentClientOrigin(x,y,sizeFlags); | |
681 | ||
682 | wxSize size(-1,-1); | |
683 | ||
684 | if((width==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE)) | |
685 | { | |
686 | if(sizeFlags&wxSIZE_AUTO_WIDTH) | |
687 | { | |
688 | size=DoGetBestSize(); | |
689 | width=size.x; | |
690 | } | |
691 | else | |
692 | width=currentW; | |
693 | } | |
694 | if((height==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE)) | |
695 | { | |
696 | if(sizeFlags&wxSIZE_AUTO_HEIGHT) | |
697 | { | |
698 | if(size.x==-1) | |
699 | size=DoGetBestSize(); | |
700 | height=size.y; | |
701 | } | |
702 | else | |
703 | height=currentH; | |
704 | } | |
705 | DoMoveWindow(x,y,width,height); | |
706 | } | |
707 | ||
708 | #if wxUSE_TOOLTIPS | |
709 | ||
710 | void wxWindowCocoa::DoSetToolTip( wxToolTip *tip ) | |
711 | { | |
712 | wxWindowBase::DoSetToolTip(tip); | |
713 | ||
714 | if ( m_tooltip ) | |
715 | { | |
716 | m_tooltip->SetWindow((wxWindow *)this); | |
717 | } | |
718 | } | |
719 | ||
720 | #endif | |
721 | ||
722 | void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height) | |
723 | { | |
724 | wxAutoNSAutoreleasePool pool; | |
725 | wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)"),this,x,y,width,height); | |
726 | ||
727 | NSView *nsview = GetNSViewForSuperview(); | |
728 | NSView *superview = [nsview superview]; | |
729 | ||
730 | wxCHECK_RET(GetParent(), wxT("Window can only be placed correctly when it has a parent")); | |
731 | ||
732 | NSRect oldFrameRect = [nsview frame]; | |
733 | NSRect newFrameRect = GetParent()->CocoaTransformWxToBounds(NSMakeRect(x,y,width,height)); | |
734 | [nsview setFrame:newFrameRect]; | |
735 | // Be sure to redraw the parent to reflect the changed position | |
736 | [superview setNeedsDisplayInRect:oldFrameRect]; | |
737 | [superview setNeedsDisplayInRect:newFrameRect]; | |
738 | } | |
739 | ||
740 | void wxWindowCocoa::SetInitialFrameRect(const wxPoint& pos, const wxSize& size) | |
741 | { | |
742 | NSView *nsview = GetNSViewForSuperview(); | |
743 | NSView *superview = [nsview superview]; | |
744 | wxCHECK_RET(superview,wxT("NSView does not have a superview")); | |
745 | wxCHECK_RET(GetParent(), wxT("Window can only be placed correctly when it has a parent")); | |
746 | NSRect frameRect = [nsview frame]; | |
747 | if(size.x!=-1) | |
748 | frameRect.size.width = size.x; | |
749 | if(size.y!=-1) | |
750 | frameRect.size.height = size.y; | |
751 | frameRect.origin.x = pos.x; | |
752 | frameRect.origin.y = pos.y; | |
753 | // Tell Cocoa to change the margin between the bottom of the superview | |
754 | // and the bottom of the control. Keeps the control pinned to the top | |
755 | // of its superview so that its position in the wxWidgets coordinate | |
756 | // system doesn't change. | |
757 | if(![superview isFlipped]) | |
758 | [nsview setAutoresizingMask: NSViewMinYMargin]; | |
759 | // MUST set the mask before setFrame: which can generate a size event | |
760 | // and cause a scroller to be added! | |
761 | frameRect = GetParent()->CocoaTransformWxToBounds(frameRect); | |
762 | [nsview setFrame: frameRect]; | |
763 | } | |
764 | ||
765 | // Get total size | |
766 | void wxWindow::DoGetSize(int *w, int *h) const | |
767 | { | |
768 | NSRect cocoaRect = [GetNSViewForSuperview() frame]; | |
769 | if(w) | |
770 | *w=(int)cocoaRect.size.width; | |
771 | if(h) | |
772 | *h=(int)cocoaRect.size.height; | |
773 | wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoGetSize = (%d,%d)"),this,(int)cocoaRect.size.width,(int)cocoaRect.size.height); | |
774 | } | |
775 | ||
776 | void wxWindow::DoGetPosition(int *x, int *y) const | |
777 | { | |
778 | NSView *nsview = GetNSViewForSuperview(); | |
779 | ||
780 | NSRect cocoaRect = [nsview frame]; | |
781 | NSRect rectWx = GetParent()->CocoaTransformBoundsToWx(cocoaRect); | |
782 | if(x) | |
783 | *x=(int)rectWx.origin.x; | |
784 | if(y) | |
785 | *y=(int)rectWx.origin.y; | |
786 | wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoGetPosition = (%d,%d)"),this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y); | |
787 | } | |
788 | ||
789 | WXWidget wxWindow::GetHandle() const | |
790 | { | |
791 | return m_cocoaNSView; | |
792 | } | |
793 | ||
794 | wxWindow* wxWindow::GetWxWindow() const | |
795 | { | |
796 | return (wxWindow*) this; | |
797 | } | |
798 | ||
799 | void wxWindow::Refresh(bool eraseBack, const wxRect *rect) | |
800 | { | |
801 | [m_cocoaNSView setNeedsDisplay:YES]; | |
802 | } | |
803 | ||
804 | void wxWindow::SetFocus() | |
805 | { | |
806 | if([GetNSView() acceptsFirstResponder]) | |
807 | [[GetNSView() window] makeFirstResponder: GetNSView()]; | |
808 | } | |
809 | ||
810 | void wxWindow::DoCaptureMouse() | |
811 | { | |
812 | // TODO | |
813 | sm_capturedWindow = this; | |
814 | } | |
815 | ||
816 | void wxWindow::DoReleaseMouse() | |
817 | { | |
818 | // TODO | |
819 | sm_capturedWindow = NULL; | |
820 | } | |
821 | ||
822 | void wxWindow::DoScreenToClient(int *x, int *y) const | |
823 | { | |
824 | // TODO | |
825 | } | |
826 | ||
827 | void wxWindow::DoClientToScreen(int *x, int *y) const | |
828 | { | |
829 | // TODO | |
830 | } | |
831 | ||
832 | // Get size *available for subwindows* i.e. excluding menu bar etc. | |
833 | void wxWindow::DoGetClientSize(int *x, int *y) const | |
834 | { | |
835 | wxLogTrace(wxTRACE_COCOA,wxT("DoGetClientSize:")); | |
836 | if(m_wxCocoaScrollView) | |
837 | m_wxCocoaScrollView->DoGetClientSize(x,y); | |
838 | else | |
839 | wxWindowCocoa::DoGetSize(x,y); | |
840 | } | |
841 | ||
842 | void wxWindow::DoSetClientSize(int width, int height) | |
843 | { | |
844 | wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("DoSetClientSize=(%d,%d)"),width,height); | |
845 | if(m_wxCocoaScrollView) | |
846 | m_wxCocoaScrollView->ClientSizeToSize(width,height); | |
847 | CocoaSetWxWindowSize(width,height); | |
848 | } | |
849 | ||
850 | void wxWindow::CocoaSetWxWindowSize(int width, int height) | |
851 | { | |
852 | wxWindowCocoa::DoSetSize(-1,-1,width,height,wxSIZE_USE_EXISTING); | |
853 | } | |
854 | ||
855 | int wxWindow::GetCharHeight() const | |
856 | { | |
857 | // TODO | |
858 | return 0; | |
859 | } | |
860 | ||
861 | int wxWindow::GetCharWidth() const | |
862 | { | |
863 | // TODO | |
864 | return 0; | |
865 | } | |
866 | ||
867 | void wxWindow::GetTextExtent(const wxString& string, int *x, int *y, | |
868 | int *descent, int *externalLeading, const wxFont *theFont) const | |
869 | { | |
870 | // TODO | |
871 | } | |
872 | ||
873 | // Coordinates relative to the window | |
874 | void wxWindow::WarpPointer (int x_pos, int y_pos) | |
875 | { | |
876 | // TODO | |
877 | } | |
878 | ||
879 | int wxWindow::GetScrollPos(int orient) const | |
880 | { | |
881 | // TODO | |
882 | return 0; | |
883 | } | |
884 | ||
885 | // This now returns the whole range, not just the number | |
886 | // of positions that we can scroll. | |
887 | int wxWindow::GetScrollRange(int orient) const | |
888 | { | |
889 | // TODO | |
890 | return 0; | |
891 | } | |
892 | ||
893 | int wxWindow::GetScrollThumb(int orient) const | |
894 | { | |
895 | // TODO | |
896 | return 0; | |
897 | } | |
898 | ||
899 | void wxWindow::SetScrollPos(int orient, int pos, bool refresh) | |
900 | { | |
901 | // TODO | |
902 | } | |
903 | ||
904 | void wxWindow::CocoaCreateNSScrollView() | |
905 | { | |
906 | if(!m_wxCocoaScrollView) | |
907 | { | |
908 | m_wxCocoaScrollView = new wxWindowCocoaScrollView(this); | |
909 | } | |
910 | } | |
911 | ||
912 | // New function that will replace some of the above. | |
913 | void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible, | |
914 | int range, bool refresh) | |
915 | { | |
916 | CocoaCreateNSScrollView(); | |
917 | // TODO | |
918 | } | |
919 | ||
920 | // Does a physical scroll | |
921 | void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect) | |
922 | { | |
923 | // TODO | |
924 | } | |
925 | ||
926 | void wxWindow::DoSetVirtualSize( int x, int y ) | |
927 | { | |
928 | wxWindowBase::DoSetVirtualSize(x,y); | |
929 | CocoaCreateNSScrollView(); | |
930 | [m_cocoaNSView setFrameSize:NSMakeSize(m_virtualSize.x,m_virtualSize.y)]; | |
931 | } | |
932 | ||
933 | bool wxWindow::SetFont(const wxFont& font) | |
934 | { | |
935 | // TODO | |
936 | return TRUE; | |
937 | } | |
938 | ||
939 | static int CocoaRaiseWindowCompareFunction(id first, id second, void *target) | |
940 | { | |
941 | // first should be ordered higher | |
942 | if(first==target) | |
943 | return NSOrderedDescending; | |
944 | // second should be ordered higher | |
945 | if(second==target) | |
946 | return NSOrderedAscending; | |
947 | return NSOrderedSame; | |
948 | } | |
949 | ||
950 | // Raise the window to the top of the Z order | |
951 | void wxWindow::Raise() | |
952 | { | |
953 | // wxAutoNSAutoreleasePool pool; | |
954 | NSView *nsview = GetNSViewForSuperview(); | |
955 | [[nsview superview] sortSubviewsUsingFunction: | |
956 | CocoaRaiseWindowCompareFunction | |
957 | context: nsview]; | |
958 | } | |
959 | ||
960 | static int CocoaLowerWindowCompareFunction(id first, id second, void *target) | |
961 | { | |
962 | // first should be ordered lower | |
963 | if(first==target) | |
964 | return NSOrderedAscending; | |
965 | // second should be ordered lower | |
966 | if(second==target) | |
967 | return NSOrderedDescending; | |
968 | return NSOrderedSame; | |
969 | } | |
970 | ||
971 | // Lower the window to the bottom of the Z order | |
972 | void wxWindow::Lower() | |
973 | { | |
974 | NSView *nsview = GetNSViewForSuperview(); | |
975 | [[nsview superview] sortSubviewsUsingFunction: | |
976 | CocoaLowerWindowCompareFunction | |
977 | context: nsview]; | |
978 | } | |
979 | ||
980 | bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y) | |
981 | { | |
982 | return FALSE; | |
983 | } | |
984 | ||
985 | // Get the window with the focus | |
986 | wxWindow *wxWindowBase::DoFindFocus() | |
987 | { | |
988 | // Basically we are somewhat emulating the responder chain here except | |
989 | // we are only loking for the first responder in the key window or | |
990 | // upon failing to find one if the main window is different we look | |
991 | // for the first responder in the main window. | |
992 | ||
993 | // Note that the firstResponder doesn't necessarily have to be an | |
994 | // NSView but wxCocoaNSView::GetFromCocoa() will simply return | |
995 | // NULL unless it finds its argument in its hash map. | |
996 | ||
997 | wxCocoaNSView *win; | |
998 | ||
999 | NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow]; | |
1000 | win = wxCocoaNSView::GetFromCocoa([keyWindow firstResponder]); | |
1001 | if(win) | |
1002 | return win->GetWxWindow(); | |
1003 | ||
1004 | NSWindow *mainWindow = [[NSApplication sharedApplication] keyWindow]; | |
1005 | if(mainWindow == keyWindow) | |
1006 | return NULL; | |
1007 | win = wxCocoaNSView::GetFromCocoa([mainWindow firstResponder]); | |
1008 | if(win) | |
1009 | return win->GetWxWindow(); | |
1010 | ||
1011 | return NULL; | |
1012 | } | |
1013 | ||
1014 | /* static */ wxWindow *wxWindowBase::GetCapture() | |
1015 | { | |
1016 | // TODO | |
1017 | return wxWindowCocoa::sm_capturedWindow; | |
1018 | } | |
1019 | ||
1020 | wxWindow *wxGetActiveWindow() | |
1021 | { | |
1022 | // TODO | |
1023 | return NULL; | |
1024 | } | |
1025 | ||
1026 | wxPoint wxGetMousePosition() | |
1027 | { | |
1028 | // TODO | |
1029 | return wxDefaultPosition; | |
1030 | } | |
1031 | ||
1032 | wxWindow* wxFindWindowAtPointer(wxPoint& pt) | |
1033 | { | |
1034 | pt = wxGetMousePosition(); | |
1035 | return NULL; | |
1036 | } | |
1037 |