]>
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 | ||
12 | #include "wx/window.h" | |
13 | #include "wx/log.h" | |
14 | ||
7fc77f30 DE |
15 | #include "wx/cocoa/autorelease.h" |
16 | ||
fb896a32 | 17 | #import <Appkit/NSView.h> |
69dbb709 | 18 | #import <AppKit/NSEvent.h> |
fb896a32 DE |
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; | |
55c5be5e | 38 | m_isInPaint = FALSE; |
fb896a32 DE |
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 | { | |
7fc77f30 | 69 | wxAutoNSAutoreleasePool pool; |
fb896a32 DE |
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 | { | |
fb896a32 DE |
95 | if(m_dummyNSView) |
96 | { | |
c1f7d193 | 97 | wxASSERT(m_cocoaNSView); |
fb896a32 DE |
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]); | |
bac6f234 | 114 | DisassociateNSView(m_cocoaNSView); |
fb896a32 DE |
115 | [cocoaNSView retain]; |
116 | [m_cocoaNSView release]; | |
117 | m_cocoaNSView = cocoaNSView; | |
bac6f234 | 118 | AssociateNSView(m_cocoaNSView); |
fb896a32 DE |
119 | if(need_debug) wxLogDebug("wxWindowCocoa=%p::SetNSView [cocoaNSView=%p retainCount]=%d",this,cocoaNSView,[cocoaNSView retainCount]); |
120 | } | |
121 | ||
8ea5271e DE |
122 | bool wxWindowCocoa::Cocoa_drawRect(const NSRect &rect) |
123 | { | |
124 | wxLogDebug("Cocoa_drawRect"); | |
55c5be5e DE |
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 | } | |
8ea5271e | 133 | //FIXME: should probably turn that rect into the update region |
55c5be5e | 134 | m_isInPaint = TRUE; |
8ea5271e DE |
135 | wxPaintEvent event(m_windowId); |
136 | event.SetEventObject(this); | |
55c5be5e DE |
137 | bool ret = GetEventHandler()->ProcessEvent(event); |
138 | m_isInPaint = FALSE; | |
139 | return ret; | |
8ea5271e DE |
140 | } |
141 | ||
69dbb709 DE |
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 | ||
fb896a32 DE |
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 | { | |
7fc77f30 | 250 | wxAutoNSAutoreleasePool pool; |
fb896a32 DE |
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 | |
addbdd29 DE |
253 | // wxSpinCtrl (generic) abuses m_isShown, don't use it for any logic |
254 | // wxASSERT_MSG( (m_isShown && !m_dummyNSView) || (!m_isShown && m_dummyNSView),"wxWindow: m_isShown does not agree with m_dummyNSView"); | |
fb896a32 DE |
255 | // Return false if there isn't a window to show or hide |
256 | if(!m_cocoaNSView) | |
257 | return false; | |
fb896a32 DE |
258 | if(show) |
259 | { | |
addbdd29 DE |
260 | // If state isn't changing, return false |
261 | if(!m_dummyNSView) | |
262 | return false; | |
fb896a32 DE |
263 | // replaceSubView releases m_dummyNSView, balancing the alloc |
264 | [m_cocoaNSView retain]; | |
265 | [[m_dummyNSView superview] replaceSubview:m_dummyNSView with:m_cocoaNSView]; | |
266 | // But since we also retained it ourselves | |
69dbb709 | 267 | wxASSERT(![m_dummyNSView superview]); |
fb896a32 DE |
268 | [m_dummyNSView release]; |
269 | m_dummyNSView = nil; | |
69dbb709 | 270 | wxASSERT([m_cocoaNSView superview]); |
fb896a32 DE |
271 | } |
272 | else | |
273 | { | |
addbdd29 DE |
274 | // If state isn't changing, return false |
275 | if(m_dummyNSView) | |
276 | return false; | |
fb896a32 DE |
277 | m_dummyNSView = [[NSView alloc] initWithFrame: [m_cocoaNSView frame]]; |
278 | [m_dummyNSView retain]; | |
279 | // NOTE: replaceSubView will cause m_cocaNSView to be released | |
280 | [[m_cocoaNSView superview] replaceSubview:m_cocoaNSView with:m_dummyNSView]; | |
281 | // m_coocaNSView is now only retained by us | |
69dbb709 DE |
282 | wxASSERT([m_dummyNSView superview]); |
283 | wxASSERT(![m_cocoaNSView superview]); | |
fb896a32 | 284 | } |
a6b4ff2e DE |
285 | m_isShown = show; |
286 | return true; | |
fb896a32 DE |
287 | } |
288 | ||
289 | void wxWindowCocoa::DoSetSize(int x, int y, int width, int height, int sizeFlags) | |
290 | { | |
ddf7346a | 291 | // 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 |
292 | int currentX, currentY; |
293 | int currentW, currentH; | |
294 | DoGetPosition(¤tX, ¤tY); | |
295 | DoGetSize(¤tW, ¤tH); | |
296 | if((x==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE)) | |
297 | x=currentX; | |
298 | if((y==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE)) | |
299 | y=currentY; | |
300 | ||
301 | AdjustForParentClientOrigin(x,y,sizeFlags); | |
302 | ||
303 | wxSize size(-1,-1); | |
304 | ||
305 | if((width==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE)) | |
306 | { | |
307 | if(sizeFlags&wxSIZE_AUTO_WIDTH) | |
308 | { | |
309 | size=DoGetBestSize(); | |
310 | width=size.x; | |
311 | } | |
312 | else | |
313 | width=currentW; | |
314 | } | |
315 | if((height==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE)) | |
316 | { | |
317 | if(sizeFlags&wxSIZE_AUTO_HEIGHT) | |
318 | { | |
319 | if(size.x==-1) | |
320 | size=DoGetBestSize(); | |
321 | height=size.y; | |
322 | } | |
323 | else | |
324 | height=currentH; | |
325 | } | |
326 | DoMoveWindow(x,y,width,height); | |
327 | } | |
328 | ||
329 | void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height) | |
330 | { | |
ddf7346a | 331 | // wxLogDebug("wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)",this,x,y,width,height); |
fb896a32 | 332 | |
d449cf47 DE |
333 | NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView; |
334 | NSView *superview = [nsview superview]; | |
fb896a32 DE |
335 | wxCHECK_RET(superview,"NSView does not have a superview"); |
336 | NSRect parentRect = [superview frame]; | |
337 | ||
338 | NSRect cocoaRect = NSMakeRect(x,parentRect.size.height-(y+height),width,height); | |
339 | [m_cocoaNSView setFrame: cocoaRect]; | |
340 | // Also change the dummy's size | |
341 | if(m_dummyNSView) | |
342 | [m_dummyNSView setFrame: cocoaRect]; | |
343 | } | |
344 | ||
345 | // Get total size | |
346 | void wxWindow::DoGetSize(int *w, int *h) const | |
347 | { | |
348 | NSRect cocoaRect = [m_cocoaNSView frame]; | |
349 | if(w) | |
350 | *w=(int)cocoaRect.size.width; | |
351 | if(h) | |
352 | *h=(int)cocoaRect.size.height; | |
ddf7346a | 353 | // wxLogDebug("wxWindow=%p::DoGetSize = (%d,%d)",this,(int)cocoaRect.size.width,(int)cocoaRect.size.height); |
fb896a32 DE |
354 | } |
355 | ||
356 | void wxWindow::DoGetPosition(int *x, int *y) const | |
357 | { | |
576a1544 DE |
358 | NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView; |
359 | NSView *superview = [nsview superview]; | |
fb896a32 DE |
360 | wxCHECK_RET(superview,"NSView does not have a superview"); |
361 | NSRect parentRect = [superview frame]; | |
362 | ||
576a1544 | 363 | NSRect cocoaRect = [nsview frame]; |
fb896a32 DE |
364 | if(x) |
365 | *x=(int)cocoaRect.origin.x; | |
366 | if(y) | |
367 | *y=(int)(parentRect.size.height-(cocoaRect.origin.y+cocoaRect.size.height)); | |
ddf7346a | 368 | // wxLogDebug("wxWindow=%p::DoGetPosition = (%d,%d)",this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y); |
fb896a32 DE |
369 | } |
370 | ||
371 | WXWidget wxWindow::GetHandle() const | |
372 | { | |
373 | return m_cocoaNSView; | |
374 | } | |
375 | ||
ddf7346a DE |
376 | void wxWindow::Refresh(bool eraseBack, const wxRect *rect) |
377 | { | |
378 | [m_cocoaNSView setNeedsDisplay:YES]; | |
379 | } | |
380 | ||
fb896a32 DE |
381 | void wxWindow::SetFocus() |
382 | { | |
383 | // TODO | |
384 | } | |
385 | ||
386 | void wxWindow::DoCaptureMouse() | |
387 | { | |
388 | // TODO | |
389 | } | |
390 | ||
391 | void wxWindow::DoReleaseMouse() | |
392 | { | |
393 | // TODO | |
394 | } | |
395 | ||
396 | void wxWindow::DoScreenToClient(int *x, int *y) const | |
397 | { | |
398 | // TODO | |
399 | } | |
400 | ||
401 | void wxWindow::DoClientToScreen(int *x, int *y) const | |
402 | { | |
403 | // TODO | |
404 | } | |
405 | ||
406 | // Get size *available for subwindows* i.e. excluding menu bar etc. | |
407 | void wxWindow::DoGetClientSize(int *x, int *y) const | |
408 | { | |
409 | wxLogDebug("DoGetClientSize:"); | |
410 | wxWindowCocoa::DoGetSize(x,y); | |
411 | // TODO: Actually account for menubar, borders, etc... | |
412 | } | |
413 | ||
414 | void wxWindow::DoSetClientSize(int width, int height) | |
415 | { | |
416 | wxLogDebug("DoSetClientSize=(%d,%d)",width,height); | |
417 | // TODO | |
418 | } | |
419 | ||
420 | int wxWindow::GetCharHeight() const | |
421 | { | |
422 | // TODO | |
423 | return 0; | |
424 | } | |
425 | ||
426 | int wxWindow::GetCharWidth() const | |
427 | { | |
428 | // TODO | |
429 | return 0; | |
430 | } | |
431 | ||
432 | void wxWindow::GetTextExtent(const wxString& string, int *x, int *y, | |
433 | int *descent, int *externalLeading, const wxFont *theFont) const | |
434 | { | |
435 | // TODO | |
436 | } | |
437 | ||
fb896a32 DE |
438 | // Coordinates relative to the window |
439 | void wxWindow::WarpPointer (int x_pos, int y_pos) | |
440 | { | |
441 | // TODO | |
442 | } | |
443 | ||
444 | int wxWindow::GetScrollPos(int orient) const | |
445 | { | |
446 | // TODO | |
447 | return 0; | |
448 | } | |
449 | ||
450 | // This now returns the whole range, not just the number | |
451 | // of positions that we can scroll. | |
452 | int wxWindow::GetScrollRange(int orient) const | |
453 | { | |
454 | // TODO | |
455 | return 0; | |
456 | } | |
457 | ||
458 | int wxWindow::GetScrollThumb(int orient) const | |
459 | { | |
460 | // TODO | |
461 | return 0; | |
462 | } | |
463 | ||
464 | void wxWindow::SetScrollPos(int orient, int pos, bool refresh) | |
465 | { | |
466 | // TODO | |
467 | } | |
468 | ||
469 | // New function that will replace some of the above. | |
470 | void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible, | |
471 | int range, bool refresh) | |
472 | { | |
473 | // TODO | |
474 | } | |
475 | ||
476 | // Does a physical scroll | |
477 | void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect) | |
478 | { | |
479 | // TODO | |
480 | } | |
481 | ||
482 | bool wxWindow::SetFont(const wxFont& font) | |
483 | { | |
484 | // TODO | |
485 | return TRUE; | |
486 | } | |
487 | ||
488 | void wxWindow::Clear() | |
489 | { | |
490 | // TODO | |
491 | } | |
492 | ||
493 | // Raise the window to the top of the Z order | |
494 | void wxWindow::Raise() | |
495 | { | |
7fc77f30 | 496 | wxAutoNSAutoreleasePool pool; |
d5ca6629 DE |
497 | NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView; |
498 | NSView *superview = [nsview superview]; | |
499 | [nsview retain]; | |
500 | [nsview removeFromSuperview]; | |
501 | [superview addSubview:nsview]; | |
fb896a32 DE |
502 | } |
503 | ||
504 | // Lower the window to the bottom of the Z order | |
505 | void wxWindow::Lower() | |
506 | { | |
507 | // TODO | |
508 | } | |
509 | ||
510 | bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y) | |
511 | { | |
512 | return FALSE; | |
513 | } | |
514 | ||
515 | // Get the window with the focus | |
516 | wxWindow *wxWindowBase::FindFocus() | |
517 | { | |
518 | // TODO | |
519 | return NULL; | |
520 | } | |
521 | ||
522 | /* static */ wxWindow *wxWindowBase::GetCapture() | |
523 | { | |
524 | // TODO | |
525 | return NULL; | |
526 | } | |
527 | ||
528 | wxWindow *wxGetActiveWindow() | |
529 | { | |
530 | // TODO | |
531 | return NULL; | |
532 | } | |
533 |