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