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