1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/control.mm
3 // Purpose: wxControl class
4 // Author: David Elliiott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/control.h"
20 #include "wx/cocoa/autorelease.h"
21 #include "wx/cocoa/trackingrectmanager.h"
22 #include "wx/cocoa/objc/objc_uniquifying.h"
24 #import <AppKit/NSControl.h>
25 #import <AppKit/NSCell.h>
26 #import <Foundation/NSException.h>
30 @interface wxNonControlNSControl : NSControl
34 - (void)drawRect: (NSRect)rect;
35 - (void)mouseDown:(NSEvent *)theEvent;
36 - (void)mouseDragged:(NSEvent *)theEvent;
37 - (void)mouseUp:(NSEvent *)theEvent;
38 - (void)mouseMoved:(NSEvent *)theEvent;
39 - (void)mouseEntered:(NSEvent *)theEvent;
40 - (void)mouseExited:(NSEvent *)theEvent;
41 - (void)rightMouseDown:(NSEvent *)theEvent;
42 - (void)rightMouseDragged:(NSEvent *)theEvent;
43 - (void)rightMouseUp:(NSEvent *)theEvent;
44 - (void)otherMouseDown:(NSEvent *)theEvent;
45 - (void)otherMouseDragged:(NSEvent *)theEvent;
46 - (void)otherMouseUp:(NSEvent *)theEvent;
47 - (void)resetCursorRects;
48 - (void)viewDidMoveToWindow;
49 - (void)viewWillMoveToWindow:(NSWindow *)newWindow;
50 @end // wxNonControlNSControl
51 WX_DECLARE_GET_OBJC_CLASS(wxNonControlNSControl,NSControl)
53 @implementation wxNonControlNSControl : NSControl
55 - (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
57 bool acceptsFirstMouse = false;
58 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
59 if(!win || !win->Cocoa_acceptsFirstMouse(acceptsFirstMouse, theEvent))
60 acceptsFirstMouse = [super acceptsFirstMouse:theEvent];
61 return acceptsFirstMouse;
64 - (void)drawRect: (NSRect)rect
66 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
67 if( !win || !win->Cocoa_drawRect(rect) )
68 [super drawRect:rect];
71 - (void)mouseDown:(NSEvent *)theEvent
73 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
74 if( !win || !win->Cocoa_mouseDown(theEvent) )
75 [super mouseDown:theEvent];
78 - (void)mouseDragged:(NSEvent *)theEvent
80 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
81 if( !win || !win->Cocoa_mouseDragged(theEvent) )
82 [super mouseDragged:theEvent];
85 - (void)mouseUp:(NSEvent *)theEvent
87 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
88 if( !win || !win->Cocoa_mouseUp(theEvent) )
89 [super mouseUp:theEvent];
92 - (void)mouseMoved:(NSEvent *)theEvent
94 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
95 if( !win || !win->Cocoa_mouseMoved(theEvent) )
96 [super mouseMoved:theEvent];
99 - (void)mouseEntered:(NSEvent *)theEvent
101 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
102 if( !win || !win->Cocoa_mouseEntered(theEvent) )
103 [super mouseEntered:theEvent];
106 - (void)mouseExited:(NSEvent *)theEvent
108 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
109 if( !win || !win->Cocoa_mouseExited(theEvent) )
110 [super mouseExited:theEvent];
113 - (void)rightMouseDown:(NSEvent *)theEvent
115 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
116 if( !win || !win->Cocoa_rightMouseDown(theEvent) )
117 [super rightMouseDown:theEvent];
120 - (void)rightMouseDragged:(NSEvent *)theEvent
122 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
123 if( !win || !win->Cocoa_rightMouseDragged(theEvent) )
124 [super rightMouseDragged:theEvent];
127 - (void)rightMouseUp:(NSEvent *)theEvent
129 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
130 if( !win || !win->Cocoa_rightMouseUp(theEvent) )
131 [super rightMouseUp:theEvent];
134 - (void)otherMouseDown:(NSEvent *)theEvent
136 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
137 if( !win || !win->Cocoa_otherMouseDown(theEvent) )
138 [super otherMouseDown:theEvent];
141 - (void)otherMouseDragged:(NSEvent *)theEvent
143 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
144 if( !win || !win->Cocoa_otherMouseDragged(theEvent) )
145 [super otherMouseDragged:theEvent];
148 - (void)otherMouseUp:(NSEvent *)theEvent
150 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
151 if( !win || !win->Cocoa_otherMouseUp(theEvent) )
152 [super otherMouseUp:theEvent];
155 - (void)resetCursorRects
157 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
158 if( !win || !win->Cocoa_resetCursorRects() )
159 [super resetCursorRects];
162 - (void)viewDidMoveToWindow
164 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
165 if( !win || !win->Cocoa_viewDidMoveToWindow() )
166 [super viewDidMoveToWindow];
169 - (void)viewWillMoveToWindow:(NSWindow *)newWindow
171 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
172 if( !win || !win->Cocoa_viewWillMoveToWindow(newWindow) )
173 [super viewWillMoveToWindow:newWindow];
176 @end // wxNonControlNSControl
177 WX_IMPLEMENT_GET_OBJC_CLASS(wxNonControlNSControl,NSControl)
179 IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
180 BEGIN_EVENT_TABLE(wxControl, wxControlBase)
182 WX_IMPLEMENT_COCOA_OWNER(wxControl,NSControl,NSView,NSView)
184 bool wxControl::Create(wxWindow *parent, wxWindowID winid,
185 const wxPoint& pos, const wxSize& size, long style,
186 const wxValidator& validator, const wxString& name)
188 wxLogTrace(wxTRACE_COCOA,wxT("Creating control with id=%d"),winid);
189 if(!CreateControl(parent,winid,pos,size,style,validator,name))
191 wxLogTrace(wxTRACE_COCOA,wxT("Created control with id=%d"),GetId());
192 m_cocoaNSView = NULL;
193 SetNSControl([[WX_GET_OBJC_CLASS(wxNonControlNSControl) alloc] initWithFrame: MakeDefaultNSRect(size)]);
194 // NOTE: YES we want to release this (to match the alloc).
195 // DoAddChild(this) will retain us again since addSubView doesn't.
196 [m_cocoaNSView release];
198 [GetNSControl() sizeToFit];
201 m_parent->CocoaAddChild(this);
202 SetInitialFrameRect(pos,size);
204 // Controls should have a viewable-area tracking rect by default
205 m_visibleTrackingRectManager = new wxCocoaTrackingRectManager(this);
210 wxControl::~wxControl()
212 DisassociateNSControl(GetNSControl());
215 wxSize wxControl::DoGetBestSize() const
217 wxAutoNSAutoreleasePool pool;
218 wxASSERT(GetNSControl());
219 /* We can ask single-celled controls for their cell and get its size */
222 cell = [GetNSControl() cell];
224 // TODO: if anything other than method not implemented, re-raise
228 NSSize cellSize = [cell cellSize];
229 wxSize size((int)ceil(cellSize.width),(int)ceil(cellSize.height));
230 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from NSCell"),this,size.x,size.y);
234 /* multi-celled control? size to fit, get the size, then set it back */
235 NSRect storedRect = [m_cocoaNSView frame];
238 [GetNSControl() sizeToFit];
241 // TODO: if anything other than method not implemented, re-raise
245 NSRect cocoaRect = [m_cocoaNSView frame];
246 wxSize size((int)ceil(cocoaRect.size.width),(int)ceil(cocoaRect.size.height));
247 [m_cocoaNSView setFrame: storedRect];
248 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from sizeToFit"),this,size.x,size.y);
251 // Cocoa can't tell us the size, probably not an NSControl.
252 wxLogDebug(wxT("Class %s (or superclass still below wxControl) should implement DoGetBestSize()"),GetClassInfo()->GetClassName());
253 return wxControlBase::DoGetBestSize();
256 bool wxControl::ProcessCommand(wxCommandEvent& event)
258 return GetEventHandler()->ProcessEvent(event);
261 void wxControl::CocoaSetEnabled(bool enable)
263 [GetNSControl() setEnabled: enable];