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