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