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