]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/window.mm
When destroying the window, only remove it from its Cocoa parent if
[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 = (int)frameSize.width;
205 height = (int)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 = (int)nssize.width;
213 if(y)
214 *y = (int)nssize.height;
215 }
216
217 void wxWindowCocoaScroller::Cocoa_FrameChanged(void)
218 {
219 wxLogTrace(wxTRACE_COCOA,wxT("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 m_cocoaNSView = NULL;
244 m_cocoaHider = NULL;
245 m_cocoaScroller = NULL;
246 m_isBeingDeleted = FALSE;
247 m_isInPaint = FALSE;
248 m_shouldBeEnabled = true;
249 }
250
251 // Constructor
252 bool wxWindow::Create(wxWindow *parent, wxWindowID winid,
253 const wxPoint& pos,
254 const wxSize& size,
255 long style,
256 const wxString& name)
257 {
258 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
259 return false;
260
261 // TODO: create the window
262 m_cocoaNSView = NULL;
263 SetNSView([[NSView alloc] initWithFrame: MakeDefaultNSRect(size)]);
264 [m_cocoaNSView release];
265
266 if (m_parent)
267 {
268 m_parent->AddChild(this);
269 m_parent->CocoaAddChild(this);
270 }
271 SetInitialFrameRect(pos,size);
272
273 return TRUE;
274 }
275
276 // Destructor
277 wxWindow::~wxWindow()
278 {
279 wxAutoNSAutoreleasePool pool;
280 DestroyChildren();
281
282 // Make sure our parent (in the wxWindows sense) is our superview
283 // before we go removing from it.
284 if(m_parent && m_parent->GetNSView()==[GetNSViewForSuperview() superview])
285 CocoaRemoveFromParent();
286 delete m_cocoaHider;
287 delete m_cocoaScroller;
288 SetNSView(NULL);
289 }
290
291 void wxWindowCocoa::CocoaAddChild(wxWindowCocoa *child)
292 {
293 NSView *childView = child->GetNSViewForSuperview();
294
295 wxASSERT(childView);
296 [m_cocoaNSView addSubview: childView];
297 child->m_isShown = !m_cocoaHider;
298 }
299
300 void wxWindowCocoa::CocoaRemoveFromParent(void)
301 {
302 [GetNSViewForSuperview() removeFromSuperview];
303 }
304
305 void wxWindowCocoa::SetNSView(WX_NSView cocoaNSView)
306 {
307 bool need_debug = cocoaNSView || m_cocoaNSView;
308 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxWindowCocoa=%p::SetNSView [m_cocoaNSView=%p retainCount]=%d"),this,m_cocoaNSView,[m_cocoaNSView retainCount]);
309 DisassociateNSView(m_cocoaNSView);
310 [cocoaNSView retain];
311 [m_cocoaNSView release];
312 m_cocoaNSView = cocoaNSView;
313 AssociateNSView(m_cocoaNSView);
314 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxWindowCocoa=%p::SetNSView [cocoaNSView=%p retainCount]=%d"),this,cocoaNSView,[cocoaNSView retainCount]);
315 }
316
317 WX_NSView wxWindowCocoa::GetNSViewForSuperview() const
318 {
319 return m_cocoaHider
320 ? m_cocoaHider->GetNSView()
321 : m_cocoaScroller
322 ? m_cocoaScroller->GetNSScrollView()
323 : m_cocoaNSView;
324 }
325
326 WX_NSView wxWindowCocoa::GetNSViewForHiding() const
327 {
328 return m_cocoaScroller
329 ? m_cocoaScroller->GetNSScrollView()
330 : m_cocoaNSView;
331 }
332
333 bool wxWindowCocoa::Cocoa_drawRect(const NSRect &rect)
334 {
335 wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_drawRect"));
336 // Recursion can happen if the event loop runs from within the paint
337 // handler. For instance, if an assertion dialog is shown.
338 // FIXME: This seems less than ideal.
339 if(m_isInPaint)
340 {
341 wxLogDebug(wxT("Paint event recursion!"));
342 return false;
343 }
344 //FIXME: should probably turn that rect into the update region
345 m_isInPaint = TRUE;
346 wxPaintEvent event(m_windowId);
347 event.SetEventObject(this);
348 bool ret = GetEventHandler()->ProcessEvent(event);
349 m_isInPaint = FALSE;
350 return ret;
351 }
352
353 void wxWindowCocoa::InitMouseEvent(wxMouseEvent& event, WX_NSEvent cocoaEvent)
354 {
355 wxASSERT_MSG([m_cocoaNSView window]==[cocoaEvent window],wxT("Mouse event for different NSWindow"));
356 NSPoint cocoaPoint = [m_cocoaNSView convertPoint:[(NSEvent*)cocoaEvent locationInWindow] fromView:nil];
357 NSRect cocoaRect = [m_cocoaNSView frame];
358 const wxPoint &clientorigin = GetClientAreaOrigin();
359 event.m_x = (wxCoord)cocoaPoint.x - clientorigin.x;
360 event.m_y = (wxCoord)(cocoaRect.size.height - cocoaPoint.y) - clientorigin.y;
361
362 event.m_shiftDown = [cocoaEvent modifierFlags] & NSShiftKeyMask;
363 event.m_controlDown = [cocoaEvent modifierFlags] & NSControlKeyMask;
364 event.m_altDown = [cocoaEvent modifierFlags] & NSAlternateKeyMask;
365 event.m_metaDown = [cocoaEvent modifierFlags] & NSCommandKeyMask;
366
367 // TODO: set timestamp?
368 event.SetEventObject(this);
369 event.SetId(GetId());
370 }
371
372 bool wxWindowCocoa::Cocoa_mouseMoved(WX_NSEvent theEvent)
373 {
374 wxMouseEvent event(wxEVT_MOTION);
375 InitMouseEvent(event,theEvent);
376 wxLogTrace(wxTRACE_COCOA,wxT("Mouse Drag @%d,%d"),event.m_x,event.m_y);
377 return GetEventHandler()->ProcessEvent(event);
378 }
379
380 bool wxWindowCocoa::Cocoa_mouseEntered(WX_NSEvent theEvent)
381 {
382 return false;
383 }
384
385 bool wxWindowCocoa::Cocoa_mouseExited(WX_NSEvent theEvent)
386 {
387 return false;
388 }
389
390 bool wxWindowCocoa::Cocoa_mouseDown(WX_NSEvent theEvent)
391 {
392 wxMouseEvent event([theEvent clickCount]<2?wxEVT_LEFT_DOWN:wxEVT_LEFT_DCLICK);
393 InitMouseEvent(event,theEvent);
394 wxLogTrace(wxTRACE_COCOA,wxT("Mouse Down @%d,%d num clicks=%d"),event.m_x,event.m_y,[theEvent clickCount]);
395 return GetEventHandler()->ProcessEvent(event);
396 }
397
398 bool wxWindowCocoa::Cocoa_mouseDragged(WX_NSEvent theEvent)
399 {
400 wxMouseEvent event(wxEVT_MOTION);
401 InitMouseEvent(event,theEvent);
402 event.m_leftDown = true;
403 wxLogTrace(wxTRACE_COCOA,wxT("Mouse Drag @%d,%d"),event.m_x,event.m_y);
404 return GetEventHandler()->ProcessEvent(event);
405 }
406
407 bool wxWindowCocoa::Cocoa_mouseUp(WX_NSEvent theEvent)
408 {
409 wxMouseEvent event(wxEVT_LEFT_UP);
410 InitMouseEvent(event,theEvent);
411 wxLogTrace(wxTRACE_COCOA,wxT("Mouse Up @%d,%d"),event.m_x,event.m_y);
412 return GetEventHandler()->ProcessEvent(event);
413 }
414
415 bool wxWindowCocoa::Cocoa_rightMouseDown(WX_NSEvent theEvent)
416 {
417 return false;
418 }
419
420 bool wxWindowCocoa::Cocoa_rightMouseDragged(WX_NSEvent theEvent)
421 {
422 return false;
423 }
424
425 bool wxWindowCocoa::Cocoa_rightMouseUp(WX_NSEvent theEvent)
426 {
427 return false;
428 }
429
430 bool wxWindowCocoa::Cocoa_otherMouseDown(WX_NSEvent theEvent)
431 {
432 return false;
433 }
434
435 bool wxWindowCocoa::Cocoa_otherMouseDragged(WX_NSEvent theEvent)
436 {
437 return false;
438 }
439
440 bool wxWindowCocoa::Cocoa_otherMouseUp(WX_NSEvent theEvent)
441 {
442 return false;
443 }
444
445 void wxWindowCocoa::Cocoa_FrameChanged(void)
446 {
447 wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_FrameChanged"));
448 wxSizeEvent event(GetSize(), m_windowId);
449 event.SetEventObject(this);
450 GetEventHandler()->ProcessEvent(event);
451 }
452
453 bool wxWindow::Close(bool force)
454 {
455 // The only reason this function exists is that it is virtual and
456 // wxTopLevelWindowCocoa will override it.
457 return wxWindowBase::Close(force);
458 }
459
460 void wxWindow::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
461 {
462 [[oldView superview] replaceSubview:oldView with:newView];
463 }
464
465 bool wxWindow::EnableSelfAndChildren(bool enable)
466 {
467 // If the state isn't changing, don't do anything
468 if(!wxWindowBase::Enable(enable && m_shouldBeEnabled))
469 return false;
470 // Set the state of the Cocoa window
471 CocoaSetEnabled(m_isEnabled);
472 // Disable all children or (if enabling) return them to their proper state
473 for(wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
474 node; node = node->GetNext())
475 {
476 node->GetData()->EnableSelfAndChildren(enable);
477 }
478 return true;
479 }
480
481 bool wxWindow::Enable(bool enable)
482 {
483 // Keep track of what the window SHOULD be doing
484 m_shouldBeEnabled = enable;
485 // If the parent is disabled for any reason, then this window will be too.
486 if(!IsTopLevel() && GetParent())
487 {
488 enable = enable && GetParent()->IsEnabled();
489 }
490 return EnableSelfAndChildren(enable);
491 }
492
493 bool wxWindow::Show(bool show)
494 {
495 wxAutoNSAutoreleasePool pool;
496 // If the window is marked as visible, then it shouldn't have a dummy view
497 // If the window is marked hidden, then it should have a dummy view
498 // wxSpinCtrl (generic) abuses m_isShown, don't use it for any logic
499 // wxASSERT_MSG( (m_isShown && !m_dummyNSView) || (!m_isShown && m_dummyNSView),wxT("wxWindow: m_isShown does not agree with m_dummyNSView"));
500 // Return false if there isn't a window to show or hide
501 NSView *cocoaView = GetNSViewForHiding();
502 if(!cocoaView)
503 return false;
504 if(show)
505 {
506 // If state isn't changing, return false
507 if(!m_cocoaHider)
508 return false;
509 CocoaReplaceView(m_cocoaHider->GetNSView(), cocoaView);
510 wxASSERT(![m_cocoaHider->GetNSView() superview]);
511 delete m_cocoaHider;
512 m_cocoaHider = NULL;
513 wxASSERT([cocoaView superview]);
514 }
515 else
516 {
517 // If state isn't changing, return false
518 if(m_cocoaHider)
519 return false;
520 m_cocoaHider = new wxWindowCocoaHider(this);
521 // NOTE: replaceSubview:with will cause m_cocaNSView to be
522 // (auto)released which balances out addSubview
523 CocoaReplaceView(cocoaView, m_cocoaHider->GetNSView());
524 // m_coocaNSView is now only retained by us
525 wxASSERT([m_cocoaHider->GetNSView() superview]);
526 wxASSERT(![cocoaView superview]);
527 }
528 m_isShown = show;
529 return true;
530 }
531
532 void wxWindowCocoa::DoSetSize(int x, int y, int width, int height, int sizeFlags)
533 {
534 wxLogTrace(wxTRACE_COCOA_Window_Size,"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":".");
535 int currentX, currentY;
536 int currentW, currentH;
537 DoGetPosition(&currentX, &currentY);
538 DoGetSize(&currentW, &currentH);
539 if((x==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
540 x=currentX;
541 if((y==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
542 y=currentY;
543
544 AdjustForParentClientOrigin(x,y,sizeFlags);
545
546 wxSize size(-1,-1);
547
548 if((width==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
549 {
550 if(sizeFlags&wxSIZE_AUTO_WIDTH)
551 {
552 size=DoGetBestSize();
553 width=size.x;
554 }
555 else
556 width=currentW;
557 }
558 if((height==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
559 {
560 if(sizeFlags&wxSIZE_AUTO_HEIGHT)
561 {
562 if(size.x==-1)
563 size=DoGetBestSize();
564 height=size.y;
565 }
566 else
567 height=currentH;
568 }
569 DoMoveWindow(x,y,width,height);
570 }
571
572 void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
573 {
574 wxAutoNSAutoreleasePool pool;
575 wxLogTrace(wxTRACE_COCOA_Window_Size,"wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)",this,x,y,width,height);
576
577 NSView *nsview = GetNSViewForSuperview();
578 NSView *superview = [nsview superview];
579 wxCHECK_RET(superview,wxT("NSView does not have a superview"));
580 NSRect parentRect = [superview bounds];
581
582 NSRect cocoaRect = NSMakeRect(x,parentRect.size.height-(y+height),width,height);
583 [nsview setFrame: cocoaRect];
584 // Be sure to redraw the parent to reflect the changed position
585 [superview setNeedsDisplay:YES];
586 }
587
588 void wxWindowCocoa::SetInitialFrameRect(const wxPoint& pos, const wxSize& size)
589 {
590 NSView *nsview = GetNSViewForSuperview();
591 NSView *superview = [nsview superview];
592 wxCHECK_RET(superview,wxT("NSView does not have a superview"));
593 NSRect parentRect = [superview bounds];
594 NSRect frameRect = [nsview frame];
595 if(size.x!=-1)
596 frameRect.size.width = size.x;
597 if(size.y!=-1)
598 frameRect.size.height = size.y;
599 frameRect.origin.x = pos.x;
600 frameRect.origin.y = parentRect.size.height-(pos.y+frameRect.size.height);
601 // Tell Cocoa to change the margin between the bottom of the superview
602 // and the bottom of the control. Keeps the control pinned to the top
603 // of its superview so that its position in the wxWindows coordinate
604 // system doesn't change.
605 if(![superview isFlipped])
606 [nsview setAutoresizingMask: NSViewMinYMargin];
607 // MUST set the mask before setFrame: which can generate a size event
608 // and cause a scroller to be added!
609 [nsview setFrame: frameRect];
610 }
611
612 // Get total size
613 void wxWindow::DoGetSize(int *w, int *h) const
614 {
615 NSRect cocoaRect = [GetNSViewForSuperview() frame];
616 if(w)
617 *w=(int)cocoaRect.size.width;
618 if(h)
619 *h=(int)cocoaRect.size.height;
620 wxLogTrace(wxTRACE_COCOA_Window_Size,"wxWindow=%p::DoGetSize = (%d,%d)",this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
621 }
622
623 void wxWindow::DoGetPosition(int *x, int *y) const
624 {
625 NSView *nsview = GetNSViewForSuperview();
626 NSView *superview = [nsview superview];
627 wxCHECK_RET(superview,wxT("NSView does not have a superview"));
628 NSRect parentRect = [superview bounds];
629
630 NSRect cocoaRect = [nsview frame];
631 if(x)
632 *x=(int)cocoaRect.origin.x;
633 if(y)
634 *y=(int)(parentRect.size.height-(cocoaRect.origin.y+cocoaRect.size.height));
635 wxLogTrace(wxTRACE_COCOA_Window_Size,"wxWindow=%p::DoGetPosition = (%d,%d)",this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
636 }
637
638 WXWidget wxWindow::GetHandle() const
639 {
640 return m_cocoaNSView;
641 }
642
643 void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
644 {
645 [m_cocoaNSView setNeedsDisplay:YES];
646 }
647
648 void wxWindow::SetFocus()
649 {
650 // TODO
651 }
652
653 void wxWindow::DoCaptureMouse()
654 {
655 // TODO
656 sm_capturedWindow = this;
657 }
658
659 void wxWindow::DoReleaseMouse()
660 {
661 // TODO
662 sm_capturedWindow = NULL;
663 }
664
665 void wxWindow::DoScreenToClient(int *x, int *y) const
666 {
667 // TODO
668 }
669
670 void wxWindow::DoClientToScreen(int *x, int *y) const
671 {
672 // TODO
673 }
674
675 // Get size *available for subwindows* i.e. excluding menu bar etc.
676 void wxWindow::DoGetClientSize(int *x, int *y) const
677 {
678 wxLogTrace(wxTRACE_COCOA,wxT("DoGetClientSize:"));
679 if(m_cocoaScroller)
680 m_cocoaScroller->DoGetClientSize(x,y);
681 else
682 wxWindowCocoa::DoGetSize(x,y);
683 }
684
685 void wxWindow::DoSetClientSize(int width, int height)
686 {
687 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("DoSetClientSize=(%d,%d)"),width,height);
688 if(m_cocoaScroller)
689 m_cocoaScroller->ClientSizeToSize(width,height);
690 CocoaSetWxWindowSize(width,height);
691 }
692
693 void wxWindow::CocoaSetWxWindowSize(int width, int height)
694 {
695 wxWindowCocoa::DoSetSize(-1,-1,width,height,wxSIZE_USE_EXISTING);
696 }
697
698 int wxWindow::GetCharHeight() const
699 {
700 // TODO
701 return 0;
702 }
703
704 int wxWindow::GetCharWidth() const
705 {
706 // TODO
707 return 0;
708 }
709
710 void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
711 int *descent, int *externalLeading, const wxFont *theFont) const
712 {
713 // TODO
714 }
715
716 // Coordinates relative to the window
717 void wxWindow::WarpPointer (int x_pos, int y_pos)
718 {
719 // TODO
720 }
721
722 int wxWindow::GetScrollPos(int orient) const
723 {
724 // TODO
725 return 0;
726 }
727
728 // This now returns the whole range, not just the number
729 // of positions that we can scroll.
730 int wxWindow::GetScrollRange(int orient) const
731 {
732 // TODO
733 return 0;
734 }
735
736 int wxWindow::GetScrollThumb(int orient) const
737 {
738 // TODO
739 return 0;
740 }
741
742 void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
743 {
744 // TODO
745 }
746
747 void wxWindow::CocoaCreateNSScrollView()
748 {
749 if(!m_cocoaScroller)
750 {
751 m_cocoaScroller = new wxWindowCocoaScroller(this);
752 }
753 }
754
755 // New function that will replace some of the above.
756 void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible,
757 int range, bool refresh)
758 {
759 CocoaCreateNSScrollView();
760 // TODO
761 }
762
763 // Does a physical scroll
764 void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
765 {
766 // TODO
767 }
768
769 void wxWindow::DoSetVirtualSize( int x, int y )
770 {
771 wxWindowBase::DoSetVirtualSize(x,y);
772 CocoaCreateNSScrollView();
773 [m_cocoaNSView setFrameSize:NSMakeSize(m_virtualSize.x,m_virtualSize.y)];
774 }
775
776 bool wxWindow::SetFont(const wxFont& font)
777 {
778 // TODO
779 return TRUE;
780 }
781
782 static int CocoaRaiseWindowCompareFunction(id first, id second, void *target)
783 {
784 // first should be ordered higher
785 if(first==target)
786 return NSOrderedDescending;
787 // second should be ordered higher
788 if(second==target)
789 return NSOrderedAscending;
790 return NSOrderedSame;
791 }
792
793 // Raise the window to the top of the Z order
794 void wxWindow::Raise()
795 {
796 // wxAutoNSAutoreleasePool pool;
797 NSView *nsview = GetNSViewForSuperview();
798 [[nsview superview] sortSubviewsUsingFunction:
799 CocoaRaiseWindowCompareFunction
800 context: nsview];
801 }
802
803 static int CocoaLowerWindowCompareFunction(id first, id second, void *target)
804 {
805 // first should be ordered lower
806 if(first==target)
807 return NSOrderedAscending;
808 // second should be ordered lower
809 if(second==target)
810 return NSOrderedDescending;
811 return NSOrderedSame;
812 }
813
814 // Lower the window to the bottom of the Z order
815 void wxWindow::Lower()
816 {
817 NSView *nsview = GetNSViewForSuperview();
818 [[nsview superview] sortSubviewsUsingFunction:
819 CocoaLowerWindowCompareFunction
820 context: nsview];
821 }
822
823 bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
824 {
825 return FALSE;
826 }
827
828 // Get the window with the focus
829 wxWindow *wxWindowBase::FindFocus()
830 {
831 // TODO
832 return NULL;
833 }
834
835 /* static */ wxWindow *wxWindowBase::GetCapture()
836 {
837 // TODO
838 return wxWindowCocoa::sm_capturedWindow;
839 }
840
841 wxWindow *wxGetActiveWindow()
842 {
843 // TODO
844 return NULL;
845 }
846
847 wxPoint wxGetMousePosition()
848 {
849 // TODO
850 return wxDefaultPosition;
851 }
852
853 wxWindow* wxFindWindowAtPointer(wxPoint& pt)
854 {
855 pt = wxGetMousePosition();
856 return NULL;
857 }
858