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