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/string.h"
22 #include "wx/cocoa/trackingrectmanager.h"
23 #include "wx/cocoa/objc/objc_uniquifying.h"
24 #include "wx/cocoa/objc/NSView.h"
26 #import <AppKit/NSControl.h>
27 #import <AppKit/NSCell.h>
28 #import <Foundation/NSException.h>
32 @interface wxNonControlNSControl : NSControl
36 - (void)drawRect: (NSRect)rect;
37 - (void)mouseDown:(NSEvent *)theEvent;
38 - (void)mouseDragged:(NSEvent *)theEvent;
39 - (void)mouseUp:(NSEvent *)theEvent;
40 - (void)mouseMoved:(NSEvent *)theEvent;
41 - (void)mouseEntered:(NSEvent *)theEvent;
42 - (void)mouseExited:(NSEvent *)theEvent;
43 - (void)rightMouseDown:(NSEvent *)theEvent;
44 - (void)rightMouseDragged:(NSEvent *)theEvent;
45 - (void)rightMouseUp:(NSEvent *)theEvent;
46 - (void)otherMouseDown:(NSEvent *)theEvent;
47 - (void)otherMouseDragged:(NSEvent *)theEvent;
48 - (void)otherMouseUp:(NSEvent *)theEvent;
49 - (void)resetCursorRects;
50 - (void)viewDidMoveToWindow;
51 - (void)viewWillMoveToWindow:(NSWindow *)newWindow;
52 @end // wxNonControlNSControl
53 WX_DECLARE_GET_OBJC_CLASS(wxNonControlNSControl,NSControl)
55 @implementation wxNonControlNSControl : NSControl
57 - (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
59 bool acceptsFirstMouse = false;
60 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
61 if(!win || !win->Cocoa_acceptsFirstMouse(acceptsFirstMouse, theEvent))
62 acceptsFirstMouse = [super acceptsFirstMouse:theEvent];
63 return acceptsFirstMouse;
66 - (void)drawRect: (NSRect)rect
68 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
69 if( !win || !win->Cocoa_drawRect(rect) )
70 [super drawRect:rect];
73 - (void)mouseDown:(NSEvent *)theEvent
75 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
76 if( !win || !win->Cocoa_mouseDown(theEvent) )
77 [super mouseDown:theEvent];
80 - (void)mouseDragged:(NSEvent *)theEvent
82 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
83 if( !win || !win->Cocoa_mouseDragged(theEvent) )
84 [super mouseDragged:theEvent];
87 - (void)mouseUp:(NSEvent *)theEvent
89 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
90 if( !win || !win->Cocoa_mouseUp(theEvent) )
91 [super mouseUp:theEvent];
94 - (void)mouseMoved:(NSEvent *)theEvent
96 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
97 if( !win || !win->Cocoa_mouseMoved(theEvent) )
98 [super mouseMoved:theEvent];
101 - (void)mouseEntered:(NSEvent *)theEvent
103 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
104 if( !win || !win->Cocoa_mouseEntered(theEvent) )
105 [super mouseEntered:theEvent];
108 - (void)mouseExited:(NSEvent *)theEvent
110 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
111 if( !win || !win->Cocoa_mouseExited(theEvent) )
112 [super mouseExited:theEvent];
115 - (void)rightMouseDown:(NSEvent *)theEvent
117 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
118 if( !win || !win->Cocoa_rightMouseDown(theEvent) )
119 [super rightMouseDown:theEvent];
122 - (void)rightMouseDragged:(NSEvent *)theEvent
124 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
125 if( !win || !win->Cocoa_rightMouseDragged(theEvent) )
126 [super rightMouseDragged:theEvent];
129 - (void)rightMouseUp:(NSEvent *)theEvent
131 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
132 if( !win || !win->Cocoa_rightMouseUp(theEvent) )
133 [super rightMouseUp:theEvent];
136 - (void)otherMouseDown:(NSEvent *)theEvent
138 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
139 if( !win || !win->Cocoa_otherMouseDown(theEvent) )
140 [super otherMouseDown:theEvent];
143 - (void)otherMouseDragged:(NSEvent *)theEvent
145 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
146 if( !win || !win->Cocoa_otherMouseDragged(theEvent) )
147 [super otherMouseDragged:theEvent];
150 - (void)otherMouseUp:(NSEvent *)theEvent
152 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
153 if( !win || !win->Cocoa_otherMouseUp(theEvent) )
154 [super otherMouseUp:theEvent];
157 - (void)resetCursorRects
159 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
160 if( !win || !win->Cocoa_resetCursorRects() )
161 [super resetCursorRects];
164 - (void)viewDidMoveToWindow
166 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
167 if( !win || !win->Cocoa_viewDidMoveToWindow() )
168 [super viewDidMoveToWindow];
171 - (void)viewWillMoveToWindow:(NSWindow *)newWindow
173 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
174 if( !win || !win->Cocoa_viewWillMoveToWindow(newWindow) )
175 [super viewWillMoveToWindow:newWindow];
178 @end // wxNonControlNSControl
179 WX_IMPLEMENT_GET_OBJC_CLASS(wxNonControlNSControl,NSControl)
181 IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
182 BEGIN_EVENT_TABLE(wxControl, wxControlBase)
184 WX_IMPLEMENT_COCOA_OWNER(wxControl,NSControl,NSView,NSView)
186 bool wxControl::Create(wxWindow *parent, wxWindowID winid,
187 const wxPoint& pos, const wxSize& size, long style,
188 const wxValidator& validator, const wxString& name)
190 wxLogTrace(wxTRACE_COCOA,wxT("Creating control with id=%d"),winid);
191 if(!CreateControl(parent,winid,pos,size,style,validator,name))
193 wxLogTrace(wxTRACE_COCOA,wxT("Created control with id=%d"),GetId());
194 m_cocoaNSView = NULL;
195 SetNSControl([[WX_GET_OBJC_CLASS(WXNSView) alloc] initWithFrame: MakeDefaultNSRect(size)]);
196 // NOTE: YES we want to release this (to match the alloc).
197 // DoAddChild(this) will retain us again since addSubView doesn't.
198 [m_cocoaNSView release];
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 */
221 if([GetNSControl() respondsToSelector:@selector(cell)])
222 cell = [GetNSControl() cell];
225 NSSize cellSize = [cell cellSize];
226 wxSize size((int)ceil(cellSize.width),(int)ceil(cellSize.height));
227 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from NSCell"),this,size.x,size.y);
231 /* multi-celled control? size to fit, get the size, then set it back */
232 if([GetNSControl() respondsToSelector:@selector(sizeToFit)])
234 NSRect storedRect = [m_cocoaNSView frame];
235 [GetNSControl() sizeToFit];
236 NSRect cocoaRect = [m_cocoaNSView frame];
237 wxSize size((int)ceil(cocoaRect.size.width),(int)ceil(cocoaRect.size.height));
238 [m_cocoaNSView setFrame: storedRect];
239 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from sizeToFit"),this,size.x,size.y);
242 // Cocoa can't tell us the size, probably not an NSControl.
243 wxLogDebug(wxT("Class %s (or superclass still below wxControl) should implement DoGetBestSize()"),GetClassInfo()->GetClassName());
244 return wxControlBase::DoGetBestSize();
247 bool wxControl::ProcessCommand(wxCommandEvent& event)
249 return GetEventHandler()->ProcessEvent(event);
252 void wxControl::CocoaSetEnabled(bool enable)
254 if([GetNSControl() respondsToSelector:@selector(setEnabled:)])
255 [GetNSControl() setEnabled: enable];
258 /*static*/ void wxControl::CocoaSetLabelForObject(const wxString& label, struct objc_object *aView)
260 [aView setTitle:wxNSStringWithWxString(GetLabelText(label))];