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