]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/window.mm
no message
[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 #import <Appkit/NSView.h>
16 #import <AppKit/NSEvent.h>
17
18 // normally the base classes aren't included, but wxWindow is special
19 #ifdef __WXUNIVERSAL__
20 IMPLEMENT_ABSTRACT_CLASS(wxWindowCocoa, wxWindowBase)
21 #else
22 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
23 #endif
24
25 BEGIN_EVENT_TABLE(wxWindowCocoa, wxWindowBase)
26 END_EVENT_TABLE()
27
28 // Constructor
29 void wxWindowCocoa::Init()
30 {
31 InitBase();
32
33 m_cocoaNSView = NULL;
34 m_dummyNSView = NULL;
35 m_isBeingDeleted = FALSE;
36 }
37
38 // Constructor
39 bool wxWindow::Create(wxWindow *parent, wxWindowID winid,
40 const wxPoint& pos,
41 const wxSize& size,
42 long style,
43 const wxString& name)
44 {
45 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
46 return false;
47
48 // TODO: create the window
49 NSRect cocoaRect = NSMakeRect(10,10,20,20);
50 m_cocoaNSView = NULL;
51 SetNSView([[NSView alloc] initWithFrame: cocoaRect]);
52 [m_cocoaNSView release];
53
54 if (m_parent)
55 {
56 m_parent->AddChild(this);
57 m_parent->CocoaAddChild(this);
58 }
59
60 return TRUE;
61 }
62
63 // Destructor
64 wxWindow::~wxWindow()
65 {
66 DestroyChildren();
67
68 if(m_parent)
69 m_parent->RemoveChild(this);
70
71 CocoaRemoveFromParent();
72 SetNSView(NULL);
73 }
74
75 void wxWindowCocoa::CocoaAddChild(wxWindowCocoa *child)
76 {
77 [child->m_cocoaNSView retain];
78 // NOTE: addSubView takes ownership of, but does not retain the subview
79 // Upon a removeFromView or closing the super view, the child WILL be
80 // released!!! I think the idea here is that normally you would alloc
81 // the subview and add it to the superview and this way you don't have
82 // to release what you just alloced. Unfortunately, that doesn't
83 // make sense for wxCocoa, so we do this instead.
84 [m_cocoaNSView addSubview: child->m_cocoaNSView];
85 wxASSERT(!child->m_dummyNSView);
86 child->m_isShown = true;
87 }
88
89 void wxWindowCocoa::CocoaRemoveFromParent(void)
90 {
91 if(m_dummyNSView)
92 {
93 wxASSERT(m_cocoaNSView);
94 // balances the alloc
95 [m_dummyNSView removeFromSuperview];
96 // But since we also retained it ourselves
97 [m_dummyNSView release];
98 m_dummyNSView = nil;
99 // m_cocoaNSView has of course already been removed by virtue of
100 // replaceSubview: m_cocoaNSView with: m_dummyNSView
101 }
102 else
103 [m_cocoaNSView removeFromSuperview];
104 }
105
106 void wxWindowCocoa::SetNSView(WX_NSView cocoaNSView)
107 {
108 bool need_debug = cocoaNSView || m_cocoaNSView;
109 if(need_debug) wxLogDebug("wxWindowCocoa=%p::SetNSView [m_cocoaNSView=%p retainCount]=%d",this,m_cocoaNSView,[m_cocoaNSView retainCount]);
110 if(m_cocoaNSView)
111 DisassociateNSView(m_cocoaNSView);
112 [cocoaNSView retain];
113 [m_cocoaNSView release];
114 m_cocoaNSView = cocoaNSView;
115 if(m_cocoaNSView)
116 AssociateNSView(m_cocoaNSView);
117 if(need_debug) wxLogDebug("wxWindowCocoa=%p::SetNSView [cocoaNSView=%p retainCount]=%d",this,cocoaNSView,[cocoaNSView retainCount]);
118 }
119
120 bool wxWindowCocoa::Cocoa_drawRect(const NSRect &rect)
121 {
122 wxLogDebug("Cocoa_drawRect");
123 //FIXME: should probably turn that rect into the update region
124 wxPaintEvent event(m_windowId);
125 event.SetEventObject(this);
126 return GetEventHandler()->ProcessEvent(event);
127 }
128
129 void wxWindowCocoa::InitMouseEvent(wxMouseEvent& event, WX_NSEvent cocoaEvent)
130 {
131 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
132 wxASSERT_MSG([nsview window]==[cocoaEvent window],"Mouse event for different NSWindow");
133 NSPoint cocoaPoint = [nsview convertPoint:[(NSEvent*)cocoaEvent locationInWindow] fromView:nil];
134 NSRect cocoaRect = [nsview frame];
135 const wxPoint &clientorigin = GetClientAreaOrigin();
136 event.m_x = (wxCoord)cocoaPoint.x - clientorigin.x;
137 event.m_y = (wxCoord)(cocoaRect.size.height - cocoaPoint.y) - clientorigin.y;
138
139 event.m_shiftDown = [cocoaEvent modifierFlags] & NSShiftKeyMask;
140 event.m_controlDown = [cocoaEvent modifierFlags] & NSControlKeyMask;
141 event.m_altDown = [cocoaEvent modifierFlags] & NSAlternateKeyMask;
142 event.m_metaDown = [cocoaEvent modifierFlags] & NSCommandKeyMask;
143
144 // TODO: set timestamp?
145 event.SetEventObject(this);
146 event.SetId(GetId());
147 }
148
149 bool wxWindowCocoa::Cocoa_mouseMoved(WX_NSEvent theEvent)
150 {
151 wxMouseEvent event(wxEVT_MOTION);
152 InitMouseEvent(event,theEvent);
153 wxLogDebug("Mouse Drag @%d,%d",event.m_x,event.m_y);
154 return GetEventHandler()->ProcessEvent(event);
155 }
156
157 bool wxWindowCocoa::Cocoa_mouseEntered(WX_NSEvent theEvent)
158 {
159 return false;
160 }
161
162 bool wxWindowCocoa::Cocoa_mouseExited(WX_NSEvent theEvent)
163 {
164 return false;
165 }
166
167 bool wxWindowCocoa::Cocoa_mouseDown(WX_NSEvent theEvent)
168 {
169 wxMouseEvent event([theEvent clickCount]<2?wxEVT_LEFT_DOWN:wxEVT_LEFT_DCLICK);
170 InitMouseEvent(event,theEvent);
171 wxLogDebug("Mouse Down @%d,%d num clicks=%d",event.m_x,event.m_y,[theEvent clickCount]);
172 return GetEventHandler()->ProcessEvent(event);
173 }
174
175 bool wxWindowCocoa::Cocoa_mouseDragged(WX_NSEvent theEvent)
176 {
177 wxMouseEvent event(wxEVT_MOTION);
178 InitMouseEvent(event,theEvent);
179 event.m_leftDown = true;
180 wxLogDebug("Mouse Drag @%d,%d",event.m_x,event.m_y);
181 return GetEventHandler()->ProcessEvent(event);
182 }
183
184 bool wxWindowCocoa::Cocoa_mouseUp(WX_NSEvent theEvent)
185 {
186 wxMouseEvent event(wxEVT_LEFT_UP);
187 InitMouseEvent(event,theEvent);
188 wxLogDebug("Mouse Up @%d,%d",event.m_x,event.m_y);
189 return GetEventHandler()->ProcessEvent(event);
190 }
191
192 bool wxWindowCocoa::Cocoa_rightMouseDown(WX_NSEvent theEvent)
193 {
194 return false;
195 }
196
197 bool wxWindowCocoa::Cocoa_rightMouseDragged(WX_NSEvent theEvent)
198 {
199 return false;
200 }
201
202 bool wxWindowCocoa::Cocoa_rightMouseUp(WX_NSEvent theEvent)
203 {
204 return false;
205 }
206
207 bool wxWindowCocoa::Cocoa_otherMouseDown(WX_NSEvent theEvent)
208 {
209 return false;
210 }
211
212 bool wxWindowCocoa::Cocoa_otherMouseDragged(WX_NSEvent theEvent)
213 {
214 return false;
215 }
216
217 bool wxWindowCocoa::Cocoa_otherMouseUp(WX_NSEvent theEvent)
218 {
219 return false;
220 }
221
222 void wxWindowCocoa::Cocoa_FrameChanged(void)
223 {
224 wxLogDebug("Cocoa_FrameChanged");
225 wxSizeEvent event(GetSize(), m_windowId);
226 event.SetEventObject(this);
227 GetEventHandler()->ProcessEvent(event);
228 }
229
230 bool wxWindow::Close(bool force)
231 {
232 return false;
233 }
234
235 bool wxWindow::Show(bool show)
236 {
237 // If the window is marked as visible, then it shouldn't have a dummy view
238 // If the window is marked hidden, then it should have a dummy view
239 wxASSERT_MSG( (m_isShown && !m_dummyNSView) || (!m_isShown && m_dummyNSView),"wxWindow: m_isShown does not agree with m_dummyNSView");
240 // Return false if there isn't a window to show or hide
241 if(!m_cocoaNSView)
242 return false;
243 // Return false if the state isn't changing
244 if( show == m_isShown )
245 return false;
246 if(show)
247 {
248 // replaceSubView releases m_dummyNSView, balancing the alloc
249 [m_cocoaNSView retain];
250 [[m_dummyNSView superview] replaceSubview:m_dummyNSView with:m_cocoaNSView];
251 // But since we also retained it ourselves
252 wxASSERT(![m_dummyNSView superview]);
253 [m_dummyNSView release];
254 m_dummyNSView = nil;
255 wxASSERT([m_cocoaNSView superview]);
256 }
257 else
258 {
259 m_dummyNSView = [[NSView alloc] initWithFrame: [m_cocoaNSView frame]];
260 [m_dummyNSView retain];
261 // NOTE: replaceSubView will cause m_cocaNSView to be released
262 [[m_cocoaNSView superview] replaceSubview:m_cocoaNSView with:m_dummyNSView];
263 // m_coocaNSView is now only retained by us
264 wxASSERT([m_dummyNSView superview]);
265 wxASSERT(![m_cocoaNSView superview]);
266 }
267 m_isShown = show;
268 return true;
269 }
270
271 void wxWindowCocoa::DoSetSize(int x, int y, int width, int height, int sizeFlags)
272 {
273 // 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":".");
274 int currentX, currentY;
275 int currentW, currentH;
276 DoGetPosition(&currentX, &currentY);
277 DoGetSize(&currentW, &currentH);
278 if((x==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
279 x=currentX;
280 if((y==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
281 y=currentY;
282
283 AdjustForParentClientOrigin(x,y,sizeFlags);
284
285 wxSize size(-1,-1);
286
287 if((width==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
288 {
289 if(sizeFlags&wxSIZE_AUTO_WIDTH)
290 {
291 size=DoGetBestSize();
292 width=size.x;
293 }
294 else
295 width=currentW;
296 }
297 if((height==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
298 {
299 if(sizeFlags&wxSIZE_AUTO_HEIGHT)
300 {
301 if(size.x==-1)
302 size=DoGetBestSize();
303 height=size.y;
304 }
305 else
306 height=currentH;
307 }
308 DoMoveWindow(x,y,width,height);
309 }
310
311 void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
312 {
313 // wxLogDebug("wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)",this,x,y,width,height);
314
315 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
316 NSView *superview = [nsview superview];
317 wxCHECK_RET(superview,"NSView does not have a superview");
318 NSRect parentRect = [superview frame];
319
320 NSRect cocoaRect = NSMakeRect(x,parentRect.size.height-(y+height),width,height);
321 [m_cocoaNSView setFrame: cocoaRect];
322 // Also change the dummy's size
323 if(m_dummyNSView)
324 [m_dummyNSView setFrame: cocoaRect];
325 }
326
327 // Get total size
328 void wxWindow::DoGetSize(int *w, int *h) const
329 {
330 NSRect cocoaRect = [m_cocoaNSView frame];
331 if(w)
332 *w=(int)cocoaRect.size.width;
333 if(h)
334 *h=(int)cocoaRect.size.height;
335 // wxLogDebug("wxWindow=%p::DoGetSize = (%d,%d)",this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
336 }
337
338 void wxWindow::DoGetPosition(int *x, int *y) const
339 {
340 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
341 NSView *superview = [nsview superview];
342 wxCHECK_RET(superview,"NSView does not have a superview");
343 NSRect parentRect = [superview frame];
344
345 NSRect cocoaRect = [nsview frame];
346 if(x)
347 *x=(int)cocoaRect.origin.x;
348 if(y)
349 *y=(int)(parentRect.size.height-(cocoaRect.origin.y+cocoaRect.size.height));
350 // wxLogDebug("wxWindow=%p::DoGetPosition = (%d,%d)",this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
351 }
352
353 WXWidget wxWindow::GetHandle() const
354 {
355 return m_cocoaNSView;
356 }
357
358 void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
359 {
360 [m_cocoaNSView setNeedsDisplay:YES];
361 }
362
363 void wxWindow::SetFocus()
364 {
365 // TODO
366 }
367
368 void wxWindow::DoCaptureMouse()
369 {
370 // TODO
371 }
372
373 void wxWindow::DoReleaseMouse()
374 {
375 // TODO
376 }
377
378 void wxWindow::DoScreenToClient(int *x, int *y) const
379 {
380 // TODO
381 }
382
383 void wxWindow::DoClientToScreen(int *x, int *y) const
384 {
385 // TODO
386 }
387
388 // Get size *available for subwindows* i.e. excluding menu bar etc.
389 void wxWindow::DoGetClientSize(int *x, int *y) const
390 {
391 wxLogDebug("DoGetClientSize:");
392 wxWindowCocoa::DoGetSize(x,y);
393 // TODO: Actually account for menubar, borders, etc...
394 }
395
396 void wxWindow::DoSetClientSize(int width, int height)
397 {
398 wxLogDebug("DoSetClientSize=(%d,%d)",width,height);
399 // TODO
400 }
401
402 int wxWindow::GetCharHeight() const
403 {
404 // TODO
405 return 0;
406 }
407
408 int wxWindow::GetCharWidth() const
409 {
410 // TODO
411 return 0;
412 }
413
414 void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
415 int *descent, int *externalLeading, const wxFont *theFont) const
416 {
417 // TODO
418 }
419
420 // Coordinates relative to the window
421 void wxWindow::WarpPointer (int x_pos, int y_pos)
422 {
423 // TODO
424 }
425
426 int wxWindow::GetScrollPos(int orient) const
427 {
428 // TODO
429 return 0;
430 }
431
432 // This now returns the whole range, not just the number
433 // of positions that we can scroll.
434 int wxWindow::GetScrollRange(int orient) const
435 {
436 // TODO
437 return 0;
438 }
439
440 int wxWindow::GetScrollThumb(int orient) const
441 {
442 // TODO
443 return 0;
444 }
445
446 void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
447 {
448 // TODO
449 }
450
451 // New function that will replace some of the above.
452 void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible,
453 int range, bool refresh)
454 {
455 // TODO
456 }
457
458 // Does a physical scroll
459 void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
460 {
461 // TODO
462 }
463
464 bool wxWindow::SetFont(const wxFont& font)
465 {
466 // TODO
467 return TRUE;
468 }
469
470 void wxWindow::Clear()
471 {
472 // TODO
473 }
474
475 // Raise the window to the top of the Z order
476 void wxWindow::Raise()
477 {
478 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
479 NSView *superview = [nsview superview];
480 [nsview retain];
481 [nsview removeFromSuperview];
482 [superview addSubview:nsview];
483 }
484
485 // Lower the window to the bottom of the Z order
486 void wxWindow::Lower()
487 {
488 // TODO
489 }
490
491 bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
492 {
493 return FALSE;
494 }
495
496 // Get the window with the focus
497 wxWindow *wxWindowBase::FindFocus()
498 {
499 // TODO
500 return NULL;
501 }
502
503 /* static */ wxWindow *wxWindowBase::GetCapture()
504 {
505 // TODO
506 return NULL;
507 }
508
509 wxWindow *wxGetActiveWindow()
510 {
511 // TODO
512 return NULL;
513 }
514