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