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