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"
25 #import <AppKit/NSControl.h>
26 #import <AppKit/NSCell.h>
27 #import <Foundation/NSException.h>
31 @interface wxNonControlNSControl : NSControl
35 - (void)drawRect: (NSRect)rect;
36 - (void)mouseDown:(NSEvent *)theEvent;
37 - (void)mouseDragged:(NSEvent *)theEvent;
38 - (void)mouseUp:(NSEvent *)theEvent;
39 - (void)mouseMoved:(NSEvent *)theEvent;
40 - (void)mouseEntered:(NSEvent *)theEvent;
41 - (void)mouseExited:(NSEvent *)theEvent;
42 - (void)rightMouseDown:(NSEvent *)theEvent;
43 - (void)rightMouseDragged:(NSEvent *)theEvent;
44 - (void)rightMouseUp:(NSEvent *)theEvent;
45 - (void)otherMouseDown:(NSEvent *)theEvent;
46 - (void)otherMouseDragged:(NSEvent *)theEvent;
47 - (void)otherMouseUp:(NSEvent *)theEvent;
48 - (void)resetCursorRects;
49 - (void)viewDidMoveToWindow;
50 - (void)viewWillMoveToWindow:(NSWindow *)newWindow;
51 @end // wxNonControlNSControl
52 WX_DECLARE_GET_OBJC_CLASS(wxNonControlNSControl,NSControl)
54 @implementation wxNonControlNSControl : NSControl
56 - (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
58 bool acceptsFirstMouse = false;
59 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
60 if(!win || !win->Cocoa_acceptsFirstMouse(acceptsFirstMouse, theEvent))
61 acceptsFirstMouse = [super acceptsFirstMouse:theEvent];
62 return acceptsFirstMouse;
65 - (void)drawRect: (NSRect)rect
67 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
68 if( !win || !win->Cocoa_drawRect(rect) )
69 [super drawRect:rect];
72 - (void)mouseDown:(NSEvent *)theEvent
74 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
75 if( !win || !win->Cocoa_mouseDown(theEvent) )
76 [super mouseDown:theEvent];
79 - (void)mouseDragged:(NSEvent *)theEvent
81 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
82 if( !win || !win->Cocoa_mouseDragged(theEvent) )
83 [super mouseDragged:theEvent];
86 - (void)mouseUp:(NSEvent *)theEvent
88 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
89 if( !win || !win->Cocoa_mouseUp(theEvent) )
90 [super mouseUp:theEvent];
93 - (void)mouseMoved:(NSEvent *)theEvent
95 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
96 if( !win || !win->Cocoa_mouseMoved(theEvent) )
97 [super mouseMoved:theEvent];
100 - (void)mouseEntered:(NSEvent *)theEvent
102 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
103 if( !win || !win->Cocoa_mouseEntered(theEvent) )
104 [super mouseEntered:theEvent];
107 - (void)mouseExited:(NSEvent *)theEvent
109 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
110 if( !win || !win->Cocoa_mouseExited(theEvent) )
111 [super mouseExited:theEvent];
114 - (void)rightMouseDown:(NSEvent *)theEvent
116 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
117 if( !win || !win->Cocoa_rightMouseDown(theEvent) )
118 [super rightMouseDown:theEvent];
121 - (void)rightMouseDragged:(NSEvent *)theEvent
123 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
124 if( !win || !win->Cocoa_rightMouseDragged(theEvent) )
125 [super rightMouseDragged:theEvent];
128 - (void)rightMouseUp:(NSEvent *)theEvent
130 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
131 if( !win || !win->Cocoa_rightMouseUp(theEvent) )
132 [super rightMouseUp:theEvent];
135 - (void)otherMouseDown:(NSEvent *)theEvent
137 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
138 if( !win || !win->Cocoa_otherMouseDown(theEvent) )
139 [super otherMouseDown:theEvent];
142 - (void)otherMouseDragged:(NSEvent *)theEvent
144 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
145 if( !win || !win->Cocoa_otherMouseDragged(theEvent) )
146 [super otherMouseDragged:theEvent];
149 - (void)otherMouseUp:(NSEvent *)theEvent
151 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
152 if( !win || !win->Cocoa_otherMouseUp(theEvent) )
153 [super otherMouseUp:theEvent];
156 - (void)resetCursorRects
158 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
159 if( !win || !win->Cocoa_resetCursorRects() )
160 [super resetCursorRects];
163 - (void)viewDidMoveToWindow
165 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
166 if( !win || !win->Cocoa_viewDidMoveToWindow() )
167 [super viewDidMoveToWindow];
170 - (void)viewWillMoveToWindow:(NSWindow *)newWindow
172 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
173 if( !win || !win->Cocoa_viewWillMoveToWindow(newWindow) )
174 [super viewWillMoveToWindow:newWindow];
177 @end // wxNonControlNSControl
178 WX_IMPLEMENT_GET_OBJC_CLASS(wxNonControlNSControl,NSControl)
180 IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
181 BEGIN_EVENT_TABLE(wxControl, wxControlBase)
183 WX_IMPLEMENT_COCOA_OWNER(wxControl,NSControl,NSView,NSView)
185 bool wxControl::Create(wxWindow *parent, wxWindowID winid,
186 const wxPoint& pos, const wxSize& size, long style,
187 const wxValidator& validator, const wxString& name)
189 wxLogTrace(wxTRACE_COCOA,wxT("Creating control with id=%d"),winid);
190 if(!CreateControl(parent,winid,pos,size,style,validator,name))
192 wxLogTrace(wxTRACE_COCOA,wxT("Created control with id=%d"),GetId());
193 m_cocoaNSView = NULL;
194 SetNSControl([[WX_GET_OBJC_CLASS(wxNonControlNSControl) alloc] initWithFrame: MakeDefaultNSRect(size)]);
195 // NOTE: YES we want to release this (to match the alloc).
196 // DoAddChild(this) will retain us again since addSubView doesn't.
197 [m_cocoaNSView release];
199 [GetNSControl() sizeToFit];
202 m_parent->CocoaAddChild(this);
203 SetInitialFrameRect(pos,size);
205 // Controls should have a viewable-area tracking rect by default
206 m_visibleTrackingRectManager = new wxCocoaTrackingRectManager(this);
211 wxControl::~wxControl()
213 DisassociateNSControl(GetNSControl());
216 wxSize wxControl::DoGetBestSize() const
218 wxAutoNSAutoreleasePool pool;
219 wxASSERT(GetNSControl());
220 /* We can ask single-celled controls for their cell and get its size */
222 if([GetNSControl() respondsToSelector:@selector(cell)])
223 cell = [GetNSControl() cell];
226 NSSize cellSize = [cell cellSize];
227 wxSize size((int)ceil(cellSize.width),(int)ceil(cellSize.height));
228 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from NSCell"),this,size.x,size.y);
232 /* multi-celled control? size to fit, get the size, then set it back */
233 if([GetNSControl() respondsToSelector:@selector(sizeToFit)])
235 NSRect storedRect = [m_cocoaNSView frame];
236 [GetNSControl() sizeToFit];
237 NSRect cocoaRect = [m_cocoaNSView frame];
238 wxSize size((int)ceil(cocoaRect.size.width),(int)ceil(cocoaRect.size.height));
239 [m_cocoaNSView setFrame: storedRect];
240 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from sizeToFit"),this,size.x,size.y);
243 // Cocoa can't tell us the size, probably not an NSControl.
244 wxLogDebug(wxT("Class %s (or superclass still below wxControl) should implement DoGetBestSize()"),GetClassInfo()->GetClassName());
245 return wxControlBase::DoGetBestSize();
248 bool wxControl::ProcessCommand(wxCommandEvent& event)
250 return GetEventHandler()->ProcessEvent(event);
253 void wxControl::CocoaSetEnabled(bool enable)
255 [GetNSControl() setEnabled: enable];
258 /*static*/ void wxControl::CocoaSetLabelForObject(const wxString& label, struct objc_object *aView)
260 [aView setTitle:wxNSStringWithWxString(GetLabelText(label))];