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