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"
22 #import <AppKit/NSControl.h>
23 #import <AppKit/NSCell.h>
24 #import <Foundation/NSException.h>
28 @interface wxNonControlNSControl : NSControl
32 - (void)drawRect: (NSRect)rect;
33 - (void)mouseDown:(NSEvent *)theEvent;
34 - (void)mouseDragged:(NSEvent *)theEvent;
35 - (void)mouseUp:(NSEvent *)theEvent;
36 - (void)mouseMoved:(NSEvent *)theEvent;
37 - (void)mouseEntered:(NSEvent *)theEvent;
38 - (void)mouseExited:(NSEvent *)theEvent;
39 - (void)rightMouseDown:(NSEvent *)theEvent;
40 - (void)rightMouseDragged:(NSEvent *)theEvent;
41 - (void)rightMouseUp:(NSEvent *)theEvent;
42 - (void)otherMouseDown:(NSEvent *)theEvent;
43 - (void)otherMouseDragged:(NSEvent *)theEvent;
44 - (void)otherMouseUp:(NSEvent *)theEvent;
45 - (void)resetCursorRects;
46 @end // wxNonControlNSControl
48 @implementation wxNonControlNSControl : NSControl
50 - (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
52 bool acceptsFirstMouse = false;
53 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
54 if(!win || !win->Cocoa_acceptsFirstMouse(acceptsFirstMouse, theEvent))
55 acceptsFirstMouse = [super acceptsFirstMouse:theEvent];
56 return acceptsFirstMouse;
59 - (void)drawRect: (NSRect)rect
61 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
62 if( !win || !win->Cocoa_drawRect(rect) )
63 [super drawRect:rect];
66 - (void)mouseDown:(NSEvent *)theEvent
68 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
69 if( !win || !win->Cocoa_mouseDown(theEvent) )
70 [super mouseDown:theEvent];
73 - (void)mouseDragged:(NSEvent *)theEvent
75 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
76 if( !win || !win->Cocoa_mouseDragged(theEvent) )
77 [super mouseDragged:theEvent];
80 - (void)mouseUp:(NSEvent *)theEvent
82 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
83 if( !win || !win->Cocoa_mouseUp(theEvent) )
84 [super mouseUp:theEvent];
87 - (void)mouseMoved:(NSEvent *)theEvent
89 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
90 if( !win || !win->Cocoa_mouseMoved(theEvent) )
91 [super mouseMoved:theEvent];
94 - (void)mouseEntered:(NSEvent *)theEvent
96 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
97 if( !win || !win->Cocoa_mouseEntered(theEvent) )
98 [super mouseEntered:theEvent];
101 - (void)mouseExited:(NSEvent *)theEvent
103 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
104 if( !win || !win->Cocoa_mouseExited(theEvent) )
105 [super mouseExited:theEvent];
108 - (void)rightMouseDown:(NSEvent *)theEvent
110 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
111 if( !win || !win->Cocoa_rightMouseDown(theEvent) )
112 [super rightMouseDown:theEvent];
115 - (void)rightMouseDragged:(NSEvent *)theEvent
117 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
118 if( !win || !win->Cocoa_rightMouseDragged(theEvent) )
119 [super rightMouseDragged:theEvent];
122 - (void)rightMouseUp:(NSEvent *)theEvent
124 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
125 if( !win || !win->Cocoa_rightMouseUp(theEvent) )
126 [super rightMouseUp:theEvent];
129 - (void)otherMouseDown:(NSEvent *)theEvent
131 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
132 if( !win || !win->Cocoa_otherMouseDown(theEvent) )
133 [super otherMouseDown:theEvent];
136 - (void)otherMouseDragged:(NSEvent *)theEvent
138 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
139 if( !win || !win->Cocoa_otherMouseDragged(theEvent) )
140 [super otherMouseDragged:theEvent];
143 - (void)otherMouseUp:(NSEvent *)theEvent
145 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
146 if( !win || !win->Cocoa_otherMouseUp(theEvent) )
147 [super otherMouseUp:theEvent];
150 - (void)resetCursorRects
152 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
153 if( !win || !win->Cocoa_resetCursorRects() )
154 [super resetCursorRects];
157 @end // wxNonControlNSControl
159 IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
160 BEGIN_EVENT_TABLE(wxControl, wxControlBase)
162 WX_IMPLEMENT_COCOA_OWNER(wxControl,NSControl,NSView,NSView)
164 bool wxControl::Create(wxWindow *parent, wxWindowID winid,
165 const wxPoint& pos, const wxSize& size, long style,
166 const wxValidator& validator, const wxString& name)
168 wxLogTrace(wxTRACE_COCOA,wxT("Creating control with id=%d"),winid);
169 if(!CreateControl(parent,winid,pos,size,style,validator,name))
171 wxLogTrace(wxTRACE_COCOA,wxT("Created control with id=%d"),GetId());
172 m_cocoaNSView = NULL;
173 SetNSControl([[wxNonControlNSControl alloc] initWithFrame: MakeDefaultNSRect(size)]);
174 // NOTE: YES we want to release this (to match the alloc).
175 // DoAddChild(this) will retain us again since addSubView doesn't.
176 [m_cocoaNSView release];
178 [GetNSControl() sizeToFit];
181 m_parent->CocoaAddChild(this);
182 SetInitialFrameRect(pos,size);
187 wxControl::~wxControl()
189 DisassociateNSControl(GetNSControl());
192 wxSize wxControl::DoGetBestSize() const
194 wxAutoNSAutoreleasePool pool;
195 wxASSERT(GetNSControl());
196 /* We can ask single-celled controls for their cell and get its size */
199 cell = [GetNSControl() cell];
201 // TODO: if anything other than method not implemented, re-raise
205 NSSize cellSize = [cell cellSize];
206 wxSize size((int)ceil(cellSize.width),(int)ceil(cellSize.height));
207 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from NSCell"),this,size.x,size.y);
211 /* multi-celled control? size to fit, get the size, then set it back */
212 NSRect storedRect = [m_cocoaNSView frame];
215 [GetNSControl() sizeToFit];
218 // TODO: if anything other than method not implemented, re-raise
222 NSRect cocoaRect = [m_cocoaNSView frame];
223 wxSize size((int)ceil(cocoaRect.size.width),(int)ceil(cocoaRect.size.height));
224 [m_cocoaNSView setFrame: storedRect];
225 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from sizeToFit"),this,size.x,size.y);
228 // Cocoa can't tell us the size, probably not an NSControl.
229 wxLogDebug(wxT("Class %s (or superclass still below wxControl) should implement DoGetBestSize()"),GetClassInfo()->GetClassName());
230 return wxControlBase::DoGetBestSize();
233 bool wxControl::ProcessCommand(wxCommandEvent& event)
235 return GetEventHandler()->ProcessEvent(event);
238 void wxControl::CocoaSetEnabled(bool enable)
240 [GetNSControl() setEnabled: enable];