]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/window.mm
Corrected m_depth assignment in CopyFromIcon
[wxWidgets.git] / src / cocoa / window.mm
CommitLineData
fb896a32
DE
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/cocoa/window.mm
3// Purpose: wxWindowCocoa
4// Author: David Elliott
5// Modified by:
6// Created: 2002/12/26
7// RCS-ID: $Id:
8// Copyright: (c) 2002 David Elliott
065e208e 9// Licence: wxWidgets licence
fb896a32
DE
10/////////////////////////////////////////////////////////////////////////////
11
449c5673
DE
12#include "wx/wxprec.h"
13#ifndef WX_PRECOMP
14 #include "wx/log.h"
15 #include "wx/window.h"
16#endif //WX_PRECOMP
e73ae747 17#include "wx/tooltip.h"
fb896a32 18
7fc77f30 19#include "wx/cocoa/autorelease.h"
26191790 20#include "wx/cocoa/string.h"
7fc77f30 21
f910a887 22#import <AppKit/NSView.h>
69dbb709 23#import <AppKit/NSEvent.h>
816c52cf
DE
24#import <AppKit/NSScrollView.h>
25#import <AppKit/NSColor.h>
26#import <AppKit/NSClipView.h>
dc5bcaef 27#import <Foundation/NSException.h>
67d2dac8
DE
28#import <AppKit/NSApplication.h>
29#import <AppKit/NSWindow.h>
dc5bcaef 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
5dc47140
DE
38// A category for methods that are only present in Panther's SDK
39@interface NSView(wxNSViewPrePantherCompatibility)
40- (void)getRectsBeingDrawn:(const NSRect **)rects count:(int *)count;
41@end
42
a82b8141
DE
43// ========================================================================
44// wxWindowCocoaHider
45// ========================================================================
46class wxWindowCocoaHider: protected wxCocoaNSView
47{
48 DECLARE_NO_COPY_CLASS(wxWindowCocoaHider)
49public:
50 wxWindowCocoaHider(wxWindow *owner);
51 virtual ~wxWindowCocoaHider();
52 inline WX_NSView GetNSView() { return m_dummyNSView; }
53protected:
54 wxWindowCocoa *m_owner;
55 WX_NSView m_dummyNSView;
56 virtual void Cocoa_FrameChanged(void);
75e4856b
DE
57#ifdef WXCOCOA_FILL_DUMMY_VIEW
58 virtual bool Cocoa_drawRect(const NSRect& rect);
59#endif //def WXCOCOA_FILL_DUMMY_VIEW
a82b8141
DE
60private:
61 wxWindowCocoaHider();
62};
63
816c52cf 64// ========================================================================
f298b203 65// wxWindowCocoaScrollView
816c52cf 66// ========================================================================
f298b203 67class wxWindowCocoaScrollView: protected wxCocoaNSView
816c52cf 68{
f298b203 69 DECLARE_NO_COPY_CLASS(wxWindowCocoaScrollView)
816c52cf 70public:
f298b203
DE
71 wxWindowCocoaScrollView(wxWindow *owner);
72 virtual ~wxWindowCocoaScrollView();
816c52cf
DE
73 inline WX_NSScrollView GetNSScrollView() { return m_cocoaNSScrollView; }
74 void ClientSizeToSize(int &width, int &height);
75 void DoGetClientSize(int *x, int *y) const;
76 void Encapsulate();
77 void Unencapsulate();
78protected:
79 wxWindowCocoa *m_owner;
80 WX_NSScrollView m_cocoaNSScrollView;
81 virtual void Cocoa_FrameChanged(void);
82private:
f298b203 83 wxWindowCocoaScrollView();
816c52cf
DE
84};
85
75e4856b
DE
86// ========================================================================
87// wxDummyNSView
88// ========================================================================
89@interface wxDummyNSView : NSView
90- (NSView *)hitTest:(NSPoint)aPoint;
91@end
92
93@implementation wxDummyNSView : NSView
94- (NSView *)hitTest:(NSPoint)aPoint
95{
96 return nil;
97}
98
99@end
100
a82b8141
DE
101// ========================================================================
102// wxWindowCocoaHider
103// ========================================================================
104wxWindowCocoaHider::wxWindowCocoaHider(wxWindow *owner)
105: m_owner(owner)
106{
107 wxASSERT(owner);
108 wxASSERT(owner->GetNSViewForHiding());
75e4856b 109 m_dummyNSView = [[wxDummyNSView alloc]
a82b8141 110 initWithFrame:[owner->GetNSViewForHiding() frame]];
75e4856b 111 [m_dummyNSView setAutoresizingMask: [owner->GetNSViewForHiding() autoresizingMask]];
a82b8141
DE
112 AssociateNSView(m_dummyNSView);
113}
114
115wxWindowCocoaHider::~wxWindowCocoaHider()
116{
117 DisassociateNSView(m_dummyNSView);
118 [m_dummyNSView release];
119}
120
121void wxWindowCocoaHider::Cocoa_FrameChanged(void)
122{
123 // Keep the real window in synch with the dummy
124 wxASSERT(m_dummyNSView);
125 [m_owner->GetNSViewForHiding() setFrame:[m_dummyNSView frame]];
126}
127
5558135c 128
75e4856b
DE
129#ifdef WXCOCOA_FILL_DUMMY_VIEW
130bool wxWindowCocoaHider::Cocoa_drawRect(const NSRect& rect)
131{
132 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:rect];
133 [[NSColor greenColor] set];
134 [bezpath stroke];
135 [bezpath fill];
136 return true;
137}
138#endif //def WXCOCOA_FILL_DUMMY_VIEW
139
816c52cf
DE
140// ========================================================================
141// wxFlippedNSClipView
142// ========================================================================
143@interface wxFlippedNSClipView : NSClipView
144- (BOOL)isFlipped;
145@end
146
147@implementation wxFlippedNSClipView : NSClipView
148- (BOOL)isFlipped
149{
150 return YES;
151}
152
153@end
154
155// ========================================================================
f298b203 156// wxWindowCocoaScrollView
816c52cf 157// ========================================================================
f298b203 158wxWindowCocoaScrollView::wxWindowCocoaScrollView(wxWindow *owner)
816c52cf
DE
159: m_owner(owner)
160{
a9861c57 161 wxAutoNSAutoreleasePool pool;
816c52cf
DE
162 wxASSERT(owner);
163 wxASSERT(owner->GetNSView());
164 m_cocoaNSScrollView = [[NSScrollView alloc]
165 initWithFrame:[owner->GetNSView() frame]];
166 AssociateNSView(m_cocoaNSScrollView);
167
168 /* Replace the default NSClipView with a flipped one. This ensures
169 scrolling is "pinned" to the top-left instead of bottom-right. */
170 NSClipView *flippedClip = [[wxFlippedNSClipView alloc]
171 initWithFrame: [[m_cocoaNSScrollView contentView] frame]];
172 [m_cocoaNSScrollView setContentView:flippedClip];
173 [flippedClip release];
174
175 [m_cocoaNSScrollView setBackgroundColor: [NSColor windowBackgroundColor]];
176 [m_cocoaNSScrollView setHasHorizontalScroller: YES];
177 [m_cocoaNSScrollView setHasVerticalScroller: YES];
178 Encapsulate();
179}
180
f298b203 181void wxWindowCocoaScrollView::Encapsulate()
816c52cf 182{
6f2ec3c3
DE
183 // Set the scroll view autoresizingMask to match the current NSView
184 [m_cocoaNSScrollView setAutoresizingMask: [m_owner->GetNSView() autoresizingMask]];
185 [m_owner->GetNSView() setAutoresizingMask: NSViewNotSizable];
816c52cf
DE
186 // NOTE: replaceSubView will cause m_cocaNSView to be released
187 // except when it hasn't been added into an NSView hierarchy in which
188 // case it doesn't need to be and this should work out to a no-op
189 m_owner->CocoaReplaceView(m_owner->GetNSView(), m_cocoaNSScrollView);
190 // The NSView is still retained by owner
191 [m_cocoaNSScrollView setDocumentView: m_owner->GetNSView()];
192 // Now it's also retained by the NSScrollView
193}
194
f298b203 195void wxWindowCocoaScrollView::Unencapsulate()
816c52cf
DE
196{
197 [m_cocoaNSScrollView setDocumentView: nil];
198 m_owner->CocoaReplaceView(m_cocoaNSScrollView, m_owner->GetNSView());
6f2ec3c3
DE
199 if(![[m_owner->GetNSView() superview] isFlipped])
200 [m_owner->GetNSView() setAutoresizingMask: NSViewMinYMargin];
816c52cf
DE
201}
202
f298b203 203wxWindowCocoaScrollView::~wxWindowCocoaScrollView()
816c52cf
DE
204{
205 DisassociateNSView(m_cocoaNSScrollView);
206 [m_cocoaNSScrollView release];
207}
208
f298b203 209void wxWindowCocoaScrollView::ClientSizeToSize(int &width, int &height)
816c52cf
DE
210{
211 NSSize frameSize = [NSScrollView
212 frameSizeForContentSize: NSMakeSize(width,height)
213 hasHorizontalScroller: [m_cocoaNSScrollView hasHorizontalScroller]
214 hasVerticalScroller: [m_cocoaNSScrollView hasVerticalScroller]
215 borderType: [m_cocoaNSScrollView borderType]];
e9cece45
VZ
216 width = (int)frameSize.width;
217 height = (int)frameSize.height;
816c52cf
DE
218}
219
f298b203 220void wxWindowCocoaScrollView::DoGetClientSize(int *x, int *y) const
816c52cf
DE
221{
222 NSSize nssize = [m_cocoaNSScrollView contentSize];
223 if(x)
e9cece45 224 *x = (int)nssize.width;
816c52cf 225 if(y)
e9cece45 226 *y = (int)nssize.height;
816c52cf
DE
227}
228
f298b203 229void wxWindowCocoaScrollView::Cocoa_FrameChanged(void)
816c52cf 230{
48580976 231 wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_FrameChanged"));
816c52cf
DE
232 wxSizeEvent event(m_owner->GetSize(), m_owner->GetId());
233 event.SetEventObject(m_owner);
234 m_owner->GetEventHandler()->ProcessEvent(event);
235}
236
a82b8141
DE
237// ========================================================================
238// wxWindowCocoa
239// ========================================================================
fb896a32
DE
240// normally the base classes aren't included, but wxWindow is special
241#ifdef __WXUNIVERSAL__
242IMPLEMENT_ABSTRACT_CLASS(wxWindowCocoa, wxWindowBase)
243#else
244IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
245#endif
246
247BEGIN_EVENT_TABLE(wxWindowCocoa, wxWindowBase)
248END_EVENT_TABLE()
249
b9505233
DE
250wxWindow *wxWindowCocoa::sm_capturedWindow = NULL;
251
fb896a32
DE
252// Constructor
253void wxWindowCocoa::Init()
254{
fb896a32 255 m_cocoaNSView = NULL;
a82b8141 256 m_cocoaHider = NULL;
f298b203 257 m_wxCocoaScrollView = NULL;
fb896a32 258 m_isBeingDeleted = FALSE;
55c5be5e 259 m_isInPaint = FALSE;
adb4816c 260 m_shouldBeEnabled = true;
fb896a32
DE
261}
262
263// Constructor
264bool wxWindow::Create(wxWindow *parent, wxWindowID winid,
265 const wxPoint& pos,
266 const wxSize& size,
267 long style,
268 const wxString& name)
269{
270 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
271 return false;
272
273 // TODO: create the window
fb896a32 274 m_cocoaNSView = NULL;
d139c3a8 275 SetNSView([[NSView alloc] initWithFrame: MakeDefaultNSRect(size)]);
fb896a32
DE
276 [m_cocoaNSView release];
277
278 if (m_parent)
279 {
280 m_parent->AddChild(this);
281 m_parent->CocoaAddChild(this);
6d034f7d 282 SetInitialFrameRect(pos,size);
fb896a32
DE
283 }
284
285 return TRUE;
286}
287
288// Destructor
289wxWindow::~wxWindow()
290{
7fc77f30 291 wxAutoNSAutoreleasePool pool;
fb896a32
DE
292 DestroyChildren();
293
065e208e 294 // Make sure our parent (in the wxWidgets sense) is our superview
6ba13ca4
DE
295 // before we go removing from it.
296 if(m_parent && m_parent->GetNSView()==[GetNSViewForSuperview() superview])
297 CocoaRemoveFromParent();
a82b8141 298 delete m_cocoaHider;
f298b203 299 delete m_wxCocoaScrollView;
9c85202a
DE
300 if(m_cocoaNSView)
301 SendDestroyEvent();
fb896a32
DE
302 SetNSView(NULL);
303}
304
305void wxWindowCocoa::CocoaAddChild(wxWindowCocoa *child)
306{
a82b8141
DE
307 NSView *childView = child->GetNSViewForSuperview();
308
309 wxASSERT(childView);
310 [m_cocoaNSView addSubview: childView];
311 child->m_isShown = !m_cocoaHider;
fb896a32
DE
312}
313
314void wxWindowCocoa::CocoaRemoveFromParent(void)
315{
a82b8141 316 [GetNSViewForSuperview() removeFromSuperview];
fb896a32
DE
317}
318
319void wxWindowCocoa::SetNSView(WX_NSView cocoaNSView)
320{
321 bool need_debug = cocoaNSView || m_cocoaNSView;
48580976 322 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxWindowCocoa=%p::SetNSView [m_cocoaNSView=%p retainCount]=%d"),this,m_cocoaNSView,[m_cocoaNSView retainCount]);
bac6f234 323 DisassociateNSView(m_cocoaNSView);
fb896a32
DE
324 [cocoaNSView retain];
325 [m_cocoaNSView release];
326 m_cocoaNSView = cocoaNSView;
bac6f234 327 AssociateNSView(m_cocoaNSView);
48580976 328 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxWindowCocoa=%p::SetNSView [cocoaNSView=%p retainCount]=%d"),this,cocoaNSView,[cocoaNSView retainCount]);
fb896a32
DE
329}
330
a82b8141
DE
331WX_NSView wxWindowCocoa::GetNSViewForSuperview() const
332{
333 return m_cocoaHider
334 ? m_cocoaHider->GetNSView()
f298b203
DE
335 : m_wxCocoaScrollView
336 ? m_wxCocoaScrollView->GetNSScrollView()
816c52cf 337 : m_cocoaNSView;
a82b8141
DE
338}
339
340WX_NSView wxWindowCocoa::GetNSViewForHiding() const
341{
f298b203
DE
342 return m_wxCocoaScrollView
343 ? m_wxCocoaScrollView->GetNSScrollView()
816c52cf 344 : m_cocoaNSView;
a82b8141
DE
345}
346
8ea5271e
DE
347bool wxWindowCocoa::Cocoa_drawRect(const NSRect &rect)
348{
48580976 349 wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_drawRect"));
55c5be5e
DE
350 // Recursion can happen if the event loop runs from within the paint
351 // handler. For instance, if an assertion dialog is shown.
352 // FIXME: This seems less than ideal.
353 if(m_isInPaint)
354 {
2b030203 355 wxLogDebug(wxT("Paint event recursion!"));
55c5be5e
DE
356 return false;
357 }
55c5be5e 358 m_isInPaint = TRUE;
dc5bcaef
DE
359
360 // Set m_updateRegion
361 const NSRect *rects = &rect; // The bounding box of the region
362 int countRects = 1;
363 // Try replacing the larger rectangle with a list of smaller ones:
5dc47140
DE
364 if ([GetNSView() respondsToSelector:@selector(getRectsBeingDrawn:count:)])
365 [GetNSView() getRectsBeingDrawn:&rects count:&countRects];
dc5bcaef
DE
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
1e151594 614#if wxUSE_TOOLTIPS
26191790
RN
615
616void wxWindowCocoa::DoSetToolTip( wxToolTip *tip )
617{
618 wxWindowBase::DoSetToolTip(tip);
619
26191790
RN
620 if ( m_tooltip )
621 {
622 m_tooltip->SetWindow((wxWindow *)this);
26191790
RN
623 }
624}
625
1e151594
RN
626#endif
627
fb896a32
DE
628void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
629{
bed6fe0c 630 wxAutoNSAutoreleasePool pool;
9879fa84 631 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)"),this,x,y,width,height);
fb896a32 632
a82b8141 633 NSView *nsview = GetNSViewForSuperview();
d449cf47 634 NSView *superview = [nsview superview];
2b030203 635 wxCHECK_RET(superview,wxT("NSView does not have a superview"));
8ea5801e 636 NSRect parentRect = [superview bounds];
fb896a32
DE
637
638 NSRect cocoaRect = NSMakeRect(x,parentRect.size.height-(y+height),width,height);
a82b8141 639 [nsview setFrame: cocoaRect];
b915b805
DE
640 // Be sure to redraw the parent to reflect the changed position
641 [superview setNeedsDisplay:YES];
fb896a32
DE
642}
643
d139c3a8
DE
644void wxWindowCocoa::SetInitialFrameRect(const wxPoint& pos, const wxSize& size)
645{
646 NSView *nsview = GetNSViewForSuperview();
647 NSView *superview = [nsview superview];
2b030203 648 wxCHECK_RET(superview,wxT("NSView does not have a superview"));
8ea5801e 649 NSRect parentRect = [superview bounds];
d139c3a8
DE
650 NSRect frameRect = [nsview frame];
651 if(size.x!=-1)
652 frameRect.size.width = size.x;
653 if(size.y!=-1)
654 frameRect.size.height = size.y;
655 frameRect.origin.x = pos.x;
c22469c6 656 frameRect.origin.y = parentRect.size.height-(pos.y+frameRect.size.height);
c5bd9191
DE
657 // Tell Cocoa to change the margin between the bottom of the superview
658 // and the bottom of the control. Keeps the control pinned to the top
065e208e 659 // of its superview so that its position in the wxWidgets coordinate
c5bd9191
DE
660 // system doesn't change.
661 if(![superview isFlipped])
662 [nsview setAutoresizingMask: NSViewMinYMargin];
6f2ec3c3
DE
663 // MUST set the mask before setFrame: which can generate a size event
664 // and cause a scroller to be added!
665 [nsview setFrame: frameRect];
d139c3a8
DE
666}
667
fb896a32
DE
668// Get total size
669void wxWindow::DoGetSize(int *w, int *h) const
670{
a82b8141 671 NSRect cocoaRect = [GetNSViewForSuperview() frame];
fb896a32
DE
672 if(w)
673 *w=(int)cocoaRect.size.width;
674 if(h)
675 *h=(int)cocoaRect.size.height;
9879fa84 676 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoGetSize = (%d,%d)"),this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
fb896a32
DE
677}
678
679void wxWindow::DoGetPosition(int *x, int *y) const
680{
a82b8141 681 NSView *nsview = GetNSViewForSuperview();
576a1544 682 NSView *superview = [nsview superview];
2b030203 683 wxCHECK_RET(superview,wxT("NSView does not have a superview"));
8ea5801e 684 NSRect parentRect = [superview bounds];
fb896a32 685
576a1544 686 NSRect cocoaRect = [nsview frame];
fb896a32
DE
687 if(x)
688 *x=(int)cocoaRect.origin.x;
689 if(y)
690 *y=(int)(parentRect.size.height-(cocoaRect.origin.y+cocoaRect.size.height));
9879fa84 691 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoGetPosition = (%d,%d)"),this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
fb896a32
DE
692}
693
694WXWidget wxWindow::GetHandle() const
695{
696 return m_cocoaNSView;
697}
698
f7e98dee
RN
699wxWindow* wxWindow::GetWxWindow() const
700{
701 return (wxWindow*) this;
702}
703
ddf7346a
DE
704void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
705{
706 [m_cocoaNSView setNeedsDisplay:YES];
707}
708
fb896a32
DE
709void wxWindow::SetFocus()
710{
67d2dac8
DE
711 if([GetNSView() acceptsFirstResponder])
712 [[GetNSView() window] makeFirstResponder: GetNSView()];
fb896a32
DE
713}
714
715void wxWindow::DoCaptureMouse()
716{
717 // TODO
b9505233 718 sm_capturedWindow = this;
fb896a32
DE
719}
720
721void wxWindow::DoReleaseMouse()
722{
723 // TODO
b9505233 724 sm_capturedWindow = NULL;
fb896a32
DE
725}
726
727void wxWindow::DoScreenToClient(int *x, int *y) const
728{
729 // TODO
730}
731
732void wxWindow::DoClientToScreen(int *x, int *y) const
733{
734 // TODO
735}
736
737// Get size *available for subwindows* i.e. excluding menu bar etc.
738void wxWindow::DoGetClientSize(int *x, int *y) const
739{
48580976 740 wxLogTrace(wxTRACE_COCOA,wxT("DoGetClientSize:"));
f298b203
DE
741 if(m_wxCocoaScrollView)
742 m_wxCocoaScrollView->DoGetClientSize(x,y);
816c52cf
DE
743 else
744 wxWindowCocoa::DoGetSize(x,y);
fb896a32
DE
745}
746
747void wxWindow::DoSetClientSize(int width, int height)
748{
48580976 749 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("DoSetClientSize=(%d,%d)"),width,height);
f298b203
DE
750 if(m_wxCocoaScrollView)
751 m_wxCocoaScrollView->ClientSizeToSize(width,height);
e08efb8d
DE
752 CocoaSetWxWindowSize(width,height);
753}
754
755void wxWindow::CocoaSetWxWindowSize(int width, int height)
756{
757 wxWindowCocoa::DoSetSize(-1,-1,width,height,wxSIZE_USE_EXISTING);
fb896a32
DE
758}
759
760int wxWindow::GetCharHeight() const
761{
762 // TODO
763 return 0;
764}
765
766int wxWindow::GetCharWidth() const
767{
768 // TODO
769 return 0;
770}
771
772void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
773 int *descent, int *externalLeading, const wxFont *theFont) const
774{
775 // TODO
776}
777
fb896a32
DE
778// Coordinates relative to the window
779void wxWindow::WarpPointer (int x_pos, int y_pos)
780{
781 // TODO
782}
783
784int wxWindow::GetScrollPos(int orient) const
785{
786 // TODO
787 return 0;
788}
789
790// This now returns the whole range, not just the number
791// of positions that we can scroll.
792int wxWindow::GetScrollRange(int orient) const
793{
794 // TODO
795 return 0;
796}
797
798int wxWindow::GetScrollThumb(int orient) const
799{
800 // TODO
801 return 0;
802}
803
804void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
805{
806 // TODO
807}
808
816c52cf
DE
809void wxWindow::CocoaCreateNSScrollView()
810{
f298b203 811 if(!m_wxCocoaScrollView)
816c52cf 812 {
f298b203 813 m_wxCocoaScrollView = new wxWindowCocoaScrollView(this);
816c52cf
DE
814 }
815}
816
fb896a32
DE
817// New function that will replace some of the above.
818void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible,
819 int range, bool refresh)
820{
e08efb8d 821 CocoaCreateNSScrollView();
fb896a32
DE
822 // TODO
823}
824
825// Does a physical scroll
826void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
827{
828 // TODO
829}
830
816c52cf
DE
831void wxWindow::DoSetVirtualSize( int x, int y )
832{
833 wxWindowBase::DoSetVirtualSize(x,y);
834 CocoaCreateNSScrollView();
835 [m_cocoaNSView setFrameSize:NSMakeSize(m_virtualSize.x,m_virtualSize.y)];
836}
837
fb896a32
DE
838bool wxWindow::SetFont(const wxFont& font)
839{
840 // TODO
841 return TRUE;
842}
843
4328a6ba
DE
844static int CocoaRaiseWindowCompareFunction(id first, id second, void *target)
845{
846 // first should be ordered higher
847 if(first==target)
848 return NSOrderedDescending;
849 // second should be ordered higher
850 if(second==target)
851 return NSOrderedAscending;
852 return NSOrderedSame;
853}
854
fb896a32
DE
855// Raise the window to the top of the Z order
856void wxWindow::Raise()
857{
4328a6ba 858// wxAutoNSAutoreleasePool pool;
a82b8141 859 NSView *nsview = GetNSViewForSuperview();
4328a6ba
DE
860 [[nsview superview] sortSubviewsUsingFunction:
861 CocoaRaiseWindowCompareFunction
862 context: nsview];
863}
864
865static int CocoaLowerWindowCompareFunction(id first, id second, void *target)
866{
867 // first should be ordered lower
868 if(first==target)
869 return NSOrderedAscending;
870 // second should be ordered lower
871 if(second==target)
872 return NSOrderedDescending;
873 return NSOrderedSame;
fb896a32
DE
874}
875
876// Lower the window to the bottom of the Z order
877void wxWindow::Lower()
878{
4328a6ba
DE
879 NSView *nsview = GetNSViewForSuperview();
880 [[nsview superview] sortSubviewsUsingFunction:
881 CocoaLowerWindowCompareFunction
882 context: nsview];
fb896a32
DE
883}
884
885bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
886{
887 return FALSE;
888}
889
890// Get the window with the focus
dcb68102 891wxWindow *wxWindowBase::DoFindFocus()
fb896a32 892{
67d2dac8
DE
893 // Basically we are somewhat emulating the responder chain here except
894 // we are only loking for the first responder in the key window or
895 // upon failing to find one if the main window is different we look
896 // for the first responder in the main window.
897
898 // Note that the firstResponder doesn't necessarily have to be an
899 // NSView but wxCocoaNSView::GetFromCocoa() will simply return
900 // NULL unless it finds its argument in its hash map.
901
902 wxCocoaNSView *win;
903
904 NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
905 win = wxCocoaNSView::GetFromCocoa([keyWindow firstResponder]);
906 if(win)
907 return win->GetWxWindow();
908
909 NSWindow *mainWindow = [[NSApplication sharedApplication] keyWindow];
910 if(mainWindow == keyWindow)
f7e98dee 911 return NULL;
67d2dac8
DE
912 win = wxCocoaNSView::GetFromCocoa([mainWindow firstResponder]);
913 if(win)
914 return win->GetWxWindow();
915
916 return NULL;
fb896a32
DE
917}
918
919/* static */ wxWindow *wxWindowBase::GetCapture()
920{
921 // TODO
b9505233 922 return wxWindowCocoa::sm_capturedWindow;
fb896a32
DE
923}
924
925wxWindow *wxGetActiveWindow()
926{
927 // TODO
928 return NULL;
929}
930
7c9428ab
DE
931wxPoint wxGetMousePosition()
932{
933 // TODO
934 return wxDefaultPosition;
935}
936
937wxWindow* wxFindWindowAtPointer(wxPoint& pt)
938{
939 pt = wxGetMousePosition();
940 return NULL;
941}
942