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