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