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