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