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