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