]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/window.mm
Don't send idle events while an assertion dialog is showing.
[wxWidgets.git] / src / cocoa / window.mm
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
12 #include "wx/window.h"
13 #include "wx/log.h"
14
15 #include "wx/cocoa/autorelease.h"
16
17 #import <Appkit/NSView.h>
18 #import <AppKit/NSEvent.h>
19
20 // ========================================================================
21 // wxWindowCocoaHider
22 // ========================================================================
23 class wxWindowCocoaHider: protected wxCocoaNSView
24 {
25 DECLARE_NO_COPY_CLASS(wxWindowCocoaHider)
26 public:
27 wxWindowCocoaHider(wxWindow *owner);
28 virtual ~wxWindowCocoaHider();
29 inline WX_NSView GetNSView() { return m_dummyNSView; }
30 protected:
31 wxWindowCocoa *m_owner;
32 WX_NSView m_dummyNSView;
33 virtual void Cocoa_FrameChanged(void);
34 private:
35 wxWindowCocoaHider();
36 };
37
38 // ========================================================================
39 // wxWindowCocoaHider
40 // ========================================================================
41 wxWindowCocoaHider::wxWindowCocoaHider(wxWindow *owner)
42 : m_owner(owner)
43 {
44 wxASSERT(owner);
45 wxASSERT(owner->GetNSViewForHiding());
46 m_dummyNSView = [[NSView alloc]
47 initWithFrame:[owner->GetNSViewForHiding() frame]];
48 AssociateNSView(m_dummyNSView);
49 }
50
51 wxWindowCocoaHider::~wxWindowCocoaHider()
52 {
53 DisassociateNSView(m_dummyNSView);
54 [m_dummyNSView release];
55 }
56
57 void wxWindowCocoaHider::Cocoa_FrameChanged(void)
58 {
59 // Keep the real window in synch with the dummy
60 wxASSERT(m_dummyNSView);
61 [m_owner->GetNSViewForHiding() setFrame:[m_dummyNSView frame]];
62 }
63
64 // ========================================================================
65 // wxWindowCocoa
66 // ========================================================================
67 // normally the base classes aren't included, but wxWindow is special
68 #ifdef __WXUNIVERSAL__
69 IMPLEMENT_ABSTRACT_CLASS(wxWindowCocoa, wxWindowBase)
70 #else
71 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
72 #endif
73
74 BEGIN_EVENT_TABLE(wxWindowCocoa, wxWindowBase)
75 END_EVENT_TABLE()
76
77 wxWindow *wxWindowCocoa::sm_capturedWindow = NULL;
78
79 // Constructor
80 void wxWindowCocoa::Init()
81 {
82 InitBase();
83
84 m_cocoaNSView = NULL;
85 m_cocoaHider = NULL;
86 m_isBeingDeleted = FALSE;
87 m_isInPaint = FALSE;
88 }
89
90 // Constructor
91 bool wxWindow::Create(wxWindow *parent, wxWindowID winid,
92 const wxPoint& pos,
93 const wxSize& size,
94 long style,
95 const wxString& name)
96 {
97 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
98 return false;
99
100 // TODO: create the window
101 NSRect cocoaRect = NSMakeRect(10,10,20,20);
102 m_cocoaNSView = NULL;
103 SetNSView([[NSView alloc] initWithFrame: cocoaRect]);
104 [m_cocoaNSView release];
105
106 if (m_parent)
107 {
108 m_parent->AddChild(this);
109 m_parent->CocoaAddChild(this);
110 }
111
112 return TRUE;
113 }
114
115 // Destructor
116 wxWindow::~wxWindow()
117 {
118 wxAutoNSAutoreleasePool pool;
119 DestroyChildren();
120
121 if(m_parent)
122 m_parent->RemoveChild(this);
123
124 CocoaRemoveFromParent();
125 delete m_cocoaHider;
126 SetNSView(NULL);
127 }
128
129 void wxWindowCocoa::CocoaAddChild(wxWindowCocoa *child)
130 {
131 NSView *childView = child->GetNSViewForSuperview();
132
133 wxASSERT(childView);
134 [m_cocoaNSView addSubview: childView];
135 child->m_isShown = !m_cocoaHider;
136 }
137
138 void wxWindowCocoa::CocoaRemoveFromParent(void)
139 {
140 [GetNSViewForSuperview() removeFromSuperview];
141 }
142
143 void wxWindowCocoa::SetNSView(WX_NSView cocoaNSView)
144 {
145 bool need_debug = cocoaNSView || m_cocoaNSView;
146 if(need_debug) wxLogDebug("wxWindowCocoa=%p::SetNSView [m_cocoaNSView=%p retainCount]=%d",this,m_cocoaNSView,[m_cocoaNSView retainCount]);
147 DisassociateNSView(m_cocoaNSView);
148 [cocoaNSView retain];
149 [m_cocoaNSView release];
150 m_cocoaNSView = cocoaNSView;
151 AssociateNSView(m_cocoaNSView);
152 if(need_debug) wxLogDebug("wxWindowCocoa=%p::SetNSView [cocoaNSView=%p retainCount]=%d",this,cocoaNSView,[cocoaNSView retainCount]);
153 }
154
155 WX_NSView wxWindowCocoa::GetNSViewForSuperview() const
156 {
157 return m_cocoaHider
158 ? m_cocoaHider->GetNSView()
159 : m_cocoaNSView;
160 }
161
162 WX_NSView wxWindowCocoa::GetNSViewForHiding() const
163 {
164 return m_cocoaNSView;
165 }
166
167 bool wxWindowCocoa::Cocoa_drawRect(const NSRect &rect)
168 {
169 wxLogDebug("Cocoa_drawRect");
170 // Recursion can happen if the event loop runs from within the paint
171 // handler. For instance, if an assertion dialog is shown.
172 // FIXME: This seems less than ideal.
173 if(m_isInPaint)
174 {
175 wxLogDebug("Paint event recursion!");
176 return false;
177 }
178 //FIXME: should probably turn that rect into the update region
179 m_isInPaint = TRUE;
180 wxPaintEvent event(m_windowId);
181 event.SetEventObject(this);
182 bool ret = GetEventHandler()->ProcessEvent(event);
183 m_isInPaint = FALSE;
184 return ret;
185 }
186
187 void wxWindowCocoa::InitMouseEvent(wxMouseEvent& event, WX_NSEvent cocoaEvent)
188 {
189 wxASSERT_MSG([m_cocoaNSView window]==[cocoaEvent window],"Mouse event for different NSWindow");
190 NSPoint cocoaPoint = [m_cocoaNSView convertPoint:[(NSEvent*)cocoaEvent locationInWindow] fromView:nil];
191 NSRect cocoaRect = [m_cocoaNSView frame];
192 const wxPoint &clientorigin = GetClientAreaOrigin();
193 event.m_x = (wxCoord)cocoaPoint.x - clientorigin.x;
194 event.m_y = (wxCoord)(cocoaRect.size.height - cocoaPoint.y) - clientorigin.y;
195
196 event.m_shiftDown = [cocoaEvent modifierFlags] & NSShiftKeyMask;
197 event.m_controlDown = [cocoaEvent modifierFlags] & NSControlKeyMask;
198 event.m_altDown = [cocoaEvent modifierFlags] & NSAlternateKeyMask;
199 event.m_metaDown = [cocoaEvent modifierFlags] & NSCommandKeyMask;
200
201 // TODO: set timestamp?
202 event.SetEventObject(this);
203 event.SetId(GetId());
204 }
205
206 bool wxWindowCocoa::Cocoa_mouseMoved(WX_NSEvent theEvent)
207 {
208 wxMouseEvent event(wxEVT_MOTION);
209 InitMouseEvent(event,theEvent);
210 wxLogDebug("Mouse Drag @%d,%d",event.m_x,event.m_y);
211 return GetEventHandler()->ProcessEvent(event);
212 }
213
214 bool wxWindowCocoa::Cocoa_mouseEntered(WX_NSEvent theEvent)
215 {
216 return false;
217 }
218
219 bool wxWindowCocoa::Cocoa_mouseExited(WX_NSEvent theEvent)
220 {
221 return false;
222 }
223
224 bool wxWindowCocoa::Cocoa_mouseDown(WX_NSEvent theEvent)
225 {
226 wxMouseEvent event([theEvent clickCount]<2?wxEVT_LEFT_DOWN:wxEVT_LEFT_DCLICK);
227 InitMouseEvent(event,theEvent);
228 wxLogDebug("Mouse Down @%d,%d num clicks=%d",event.m_x,event.m_y,[theEvent clickCount]);
229 return GetEventHandler()->ProcessEvent(event);
230 }
231
232 bool wxWindowCocoa::Cocoa_mouseDragged(WX_NSEvent theEvent)
233 {
234 wxMouseEvent event(wxEVT_MOTION);
235 InitMouseEvent(event,theEvent);
236 event.m_leftDown = true;
237 wxLogDebug("Mouse Drag @%d,%d",event.m_x,event.m_y);
238 return GetEventHandler()->ProcessEvent(event);
239 }
240
241 bool wxWindowCocoa::Cocoa_mouseUp(WX_NSEvent theEvent)
242 {
243 wxMouseEvent event(wxEVT_LEFT_UP);
244 InitMouseEvent(event,theEvent);
245 wxLogDebug("Mouse Up @%d,%d",event.m_x,event.m_y);
246 return GetEventHandler()->ProcessEvent(event);
247 }
248
249 bool wxWindowCocoa::Cocoa_rightMouseDown(WX_NSEvent theEvent)
250 {
251 return false;
252 }
253
254 bool wxWindowCocoa::Cocoa_rightMouseDragged(WX_NSEvent theEvent)
255 {
256 return false;
257 }
258
259 bool wxWindowCocoa::Cocoa_rightMouseUp(WX_NSEvent theEvent)
260 {
261 return false;
262 }
263
264 bool wxWindowCocoa::Cocoa_otherMouseDown(WX_NSEvent theEvent)
265 {
266 return false;
267 }
268
269 bool wxWindowCocoa::Cocoa_otherMouseDragged(WX_NSEvent theEvent)
270 {
271 return false;
272 }
273
274 bool wxWindowCocoa::Cocoa_otherMouseUp(WX_NSEvent theEvent)
275 {
276 return false;
277 }
278
279 void wxWindowCocoa::Cocoa_FrameChanged(void)
280 {
281 wxLogDebug("Cocoa_FrameChanged");
282 wxSizeEvent event(GetSize(), m_windowId);
283 event.SetEventObject(this);
284 GetEventHandler()->ProcessEvent(event);
285 }
286
287 bool wxWindow::Close(bool force)
288 {
289 return false;
290 }
291
292 void wxWindow::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
293 {
294 [[oldView superview] replaceSubview:oldView with:newView];
295 }
296
297 bool wxWindow::Show(bool show)
298 {
299 wxAutoNSAutoreleasePool pool;
300 // If the window is marked as visible, then it shouldn't have a dummy view
301 // If the window is marked hidden, then it should have a dummy view
302 // wxSpinCtrl (generic) abuses m_isShown, don't use it for any logic
303 // wxASSERT_MSG( (m_isShown && !m_dummyNSView) || (!m_isShown && m_dummyNSView),"wxWindow: m_isShown does not agree with m_dummyNSView");
304 // Return false if there isn't a window to show or hide
305 NSView *cocoaView = GetNSViewForHiding();
306 if(!cocoaView)
307 return false;
308 if(show)
309 {
310 // If state isn't changing, return false
311 if(!m_cocoaHider)
312 return false;
313 CocoaReplaceView(m_cocoaHider->GetNSView(), cocoaView);
314 wxASSERT(![m_cocoaHider->GetNSView() superview]);
315 delete m_cocoaHider;
316 m_cocoaHider = NULL;
317 wxASSERT([cocoaView superview]);
318 }
319 else
320 {
321 // If state isn't changing, return false
322 if(m_cocoaHider)
323 return false;
324 m_cocoaHider = new wxWindowCocoaHider(this);
325 // NOTE: replaceSubview:with will cause m_cocaNSView to be
326 // (auto)released which balances out addSubview
327 CocoaReplaceView(cocoaView, m_cocoaHider->GetNSView());
328 // m_coocaNSView is now only retained by us
329 wxASSERT([m_cocoaHider->GetNSView() superview]);
330 wxASSERT(![cocoaView superview]);
331 }
332 m_isShown = show;
333 return true;
334 }
335
336 void wxWindowCocoa::DoSetSize(int x, int y, int width, int height, int sizeFlags)
337 {
338 // 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":".");
339 int currentX, currentY;
340 int currentW, currentH;
341 DoGetPosition(&currentX, &currentY);
342 DoGetSize(&currentW, &currentH);
343 if((x==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
344 x=currentX;
345 if((y==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
346 y=currentY;
347
348 AdjustForParentClientOrigin(x,y,sizeFlags);
349
350 wxSize size(-1,-1);
351
352 if((width==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
353 {
354 if(sizeFlags&wxSIZE_AUTO_WIDTH)
355 {
356 size=DoGetBestSize();
357 width=size.x;
358 }
359 else
360 width=currentW;
361 }
362 if((height==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
363 {
364 if(sizeFlags&wxSIZE_AUTO_HEIGHT)
365 {
366 if(size.x==-1)
367 size=DoGetBestSize();
368 height=size.y;
369 }
370 else
371 height=currentH;
372 }
373 DoMoveWindow(x,y,width,height);
374 }
375
376 void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
377 {
378 // wxLogDebug("wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)",this,x,y,width,height);
379
380 NSView *nsview = GetNSViewForSuperview();
381 NSView *superview = [nsview superview];
382 wxCHECK_RET(superview,"NSView does not have a superview");
383 NSRect parentRect = [superview frame];
384
385 NSRect cocoaRect = NSMakeRect(x,parentRect.size.height-(y+height),width,height);
386 [nsview setFrame: cocoaRect];
387 }
388
389 // Get total size
390 void wxWindow::DoGetSize(int *w, int *h) const
391 {
392 NSRect cocoaRect = [GetNSViewForSuperview() frame];
393 if(w)
394 *w=(int)cocoaRect.size.width;
395 if(h)
396 *h=(int)cocoaRect.size.height;
397 // wxLogDebug("wxWindow=%p::DoGetSize = (%d,%d)",this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
398 }
399
400 void wxWindow::DoGetPosition(int *x, int *y) const
401 {
402 NSView *nsview = GetNSViewForSuperview();
403 NSView *superview = [nsview superview];
404 wxCHECK_RET(superview,"NSView does not have a superview");
405 NSRect parentRect = [superview frame];
406
407 NSRect cocoaRect = [nsview frame];
408 if(x)
409 *x=(int)cocoaRect.origin.x;
410 if(y)
411 *y=(int)(parentRect.size.height-(cocoaRect.origin.y+cocoaRect.size.height));
412 // wxLogDebug("wxWindow=%p::DoGetPosition = (%d,%d)",this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
413 }
414
415 WXWidget wxWindow::GetHandle() const
416 {
417 return m_cocoaNSView;
418 }
419
420 void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
421 {
422 [m_cocoaNSView setNeedsDisplay:YES];
423 }
424
425 void wxWindow::SetFocus()
426 {
427 // TODO
428 }
429
430 void wxWindow::DoCaptureMouse()
431 {
432 // TODO
433 sm_capturedWindow = this;
434 }
435
436 void wxWindow::DoReleaseMouse()
437 {
438 // TODO
439 sm_capturedWindow = NULL;
440 }
441
442 void wxWindow::DoScreenToClient(int *x, int *y) const
443 {
444 // TODO
445 }
446
447 void wxWindow::DoClientToScreen(int *x, int *y) const
448 {
449 // TODO
450 }
451
452 // Get size *available for subwindows* i.e. excluding menu bar etc.
453 void wxWindow::DoGetClientSize(int *x, int *y) const
454 {
455 wxLogDebug("DoGetClientSize:");
456 wxWindowCocoa::DoGetSize(x,y);
457 // TODO: Actually account for menubar, borders, etc...
458 }
459
460 void wxWindow::DoSetClientSize(int width, int height)
461 {
462 wxLogDebug("DoSetClientSize=(%d,%d)",width,height);
463 // TODO
464 }
465
466 int wxWindow::GetCharHeight() const
467 {
468 // TODO
469 return 0;
470 }
471
472 int wxWindow::GetCharWidth() const
473 {
474 // TODO
475 return 0;
476 }
477
478 void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
479 int *descent, int *externalLeading, const wxFont *theFont) const
480 {
481 // TODO
482 }
483
484 // Coordinates relative to the window
485 void wxWindow::WarpPointer (int x_pos, int y_pos)
486 {
487 // TODO
488 }
489
490 int wxWindow::GetScrollPos(int orient) const
491 {
492 // TODO
493 return 0;
494 }
495
496 // This now returns the whole range, not just the number
497 // of positions that we can scroll.
498 int wxWindow::GetScrollRange(int orient) const
499 {
500 // TODO
501 return 0;
502 }
503
504 int wxWindow::GetScrollThumb(int orient) const
505 {
506 // TODO
507 return 0;
508 }
509
510 void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
511 {
512 // TODO
513 }
514
515 // New function that will replace some of the above.
516 void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible,
517 int range, bool refresh)
518 {
519 // TODO
520 }
521
522 // Does a physical scroll
523 void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
524 {
525 // TODO
526 }
527
528 bool wxWindow::SetFont(const wxFont& font)
529 {
530 // TODO
531 return TRUE;
532 }
533
534 static int CocoaRaiseWindowCompareFunction(id first, id second, void *target)
535 {
536 // first should be ordered higher
537 if(first==target)
538 return NSOrderedDescending;
539 // second should be ordered higher
540 if(second==target)
541 return NSOrderedAscending;
542 return NSOrderedSame;
543 }
544
545 // Raise the window to the top of the Z order
546 void wxWindow::Raise()
547 {
548 // wxAutoNSAutoreleasePool pool;
549 NSView *nsview = GetNSViewForSuperview();
550 [[nsview superview] sortSubviewsUsingFunction:
551 CocoaRaiseWindowCompareFunction
552 context: nsview];
553 }
554
555 static int CocoaLowerWindowCompareFunction(id first, id second, void *target)
556 {
557 // first should be ordered lower
558 if(first==target)
559 return NSOrderedAscending;
560 // second should be ordered lower
561 if(second==target)
562 return NSOrderedDescending;
563 return NSOrderedSame;
564 }
565
566 // Lower the window to the bottom of the Z order
567 void wxWindow::Lower()
568 {
569 NSView *nsview = GetNSViewForSuperview();
570 [[nsview superview] sortSubviewsUsingFunction:
571 CocoaLowerWindowCompareFunction
572 context: nsview];
573 }
574
575 bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
576 {
577 return FALSE;
578 }
579
580 // Get the window with the focus
581 wxWindow *wxWindowBase::FindFocus()
582 {
583 // TODO
584 return NULL;
585 }
586
587 /* static */ wxWindow *wxWindowBase::GetCapture()
588 {
589 // TODO
590 return wxWindowCocoa::sm_capturedWindow;
591 }
592
593 wxWindow *wxGetActiveWindow()
594 {
595 // TODO
596 return NULL;
597 }
598
599 wxPoint wxGetMousePosition()
600 {
601 // TODO
602 return wxDefaultPosition;
603 }
604
605 wxWindow* wxFindWindowAtPointer(wxPoint& pt)
606 {
607 pt = wxGetMousePosition();
608 return NULL;
609 }
610