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