]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/control.mm
Tell NSTextField to send us its action message and respond to it so we can generate...
[wxWidgets.git] / src / cocoa / control.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/control.mm
3 // Purpose: wxControl class
4 // Author: David Elliiott
5 // Modified by:
6 // Created: 2003/02/15
7 // RCS-ID: $Id$
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/control.h"
15
16 #ifndef WX_PRECOMP
17 #include "wx/log.h"
18 #endif
19
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
25 #import <AppKit/NSControl.h>
26 #import <AppKit/NSCell.h>
27 #import <Foundation/NSException.h>
28
29 #include <math.h>
30
31 @interface wxNonControlNSControl : NSControl
32 {
33 }
34
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)
53
54 @implementation wxNonControlNSControl : NSControl
55
56 - (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
57 {
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;
63 }
64
65 - (void)drawRect: (NSRect)rect
66 {
67 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
68 if( !win || !win->Cocoa_drawRect(rect) )
69 [super drawRect:rect];
70 }
71
72 - (void)mouseDown:(NSEvent *)theEvent
73 {
74 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
75 if( !win || !win->Cocoa_mouseDown(theEvent) )
76 [super mouseDown:theEvent];
77 }
78
79 - (void)mouseDragged:(NSEvent *)theEvent
80 {
81 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
82 if( !win || !win->Cocoa_mouseDragged(theEvent) )
83 [super mouseDragged:theEvent];
84 }
85
86 - (void)mouseUp:(NSEvent *)theEvent
87 {
88 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
89 if( !win || !win->Cocoa_mouseUp(theEvent) )
90 [super mouseUp:theEvent];
91 }
92
93 - (void)mouseMoved:(NSEvent *)theEvent
94 {
95 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
96 if( !win || !win->Cocoa_mouseMoved(theEvent) )
97 [super mouseMoved:theEvent];
98 }
99
100 - (void)mouseEntered:(NSEvent *)theEvent
101 {
102 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
103 if( !win || !win->Cocoa_mouseEntered(theEvent) )
104 [super mouseEntered:theEvent];
105 }
106
107 - (void)mouseExited:(NSEvent *)theEvent
108 {
109 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
110 if( !win || !win->Cocoa_mouseExited(theEvent) )
111 [super mouseExited:theEvent];
112 }
113
114 - (void)rightMouseDown:(NSEvent *)theEvent
115 {
116 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
117 if( !win || !win->Cocoa_rightMouseDown(theEvent) )
118 [super rightMouseDown:theEvent];
119 }
120
121 - (void)rightMouseDragged:(NSEvent *)theEvent
122 {
123 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
124 if( !win || !win->Cocoa_rightMouseDragged(theEvent) )
125 [super rightMouseDragged:theEvent];
126 }
127
128 - (void)rightMouseUp:(NSEvent *)theEvent
129 {
130 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
131 if( !win || !win->Cocoa_rightMouseUp(theEvent) )
132 [super rightMouseUp:theEvent];
133 }
134
135 - (void)otherMouseDown:(NSEvent *)theEvent
136 {
137 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
138 if( !win || !win->Cocoa_otherMouseDown(theEvent) )
139 [super otherMouseDown:theEvent];
140 }
141
142 - (void)otherMouseDragged:(NSEvent *)theEvent
143 {
144 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
145 if( !win || !win->Cocoa_otherMouseDragged(theEvent) )
146 [super otherMouseDragged:theEvent];
147 }
148
149 - (void)otherMouseUp:(NSEvent *)theEvent
150 {
151 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
152 if( !win || !win->Cocoa_otherMouseUp(theEvent) )
153 [super otherMouseUp:theEvent];
154 }
155
156 - (void)resetCursorRects
157 {
158 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
159 if( !win || !win->Cocoa_resetCursorRects() )
160 [super resetCursorRects];
161 }
162
163 - (void)viewDidMoveToWindow
164 {
165 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
166 if( !win || !win->Cocoa_viewDidMoveToWindow() )
167 [super viewDidMoveToWindow];
168 }
169
170 - (void)viewWillMoveToWindow:(NSWindow *)newWindow
171 {
172 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
173 if( !win || !win->Cocoa_viewWillMoveToWindow(newWindow) )
174 [super viewWillMoveToWindow:newWindow];
175 }
176
177 @end // wxNonControlNSControl
178 WX_IMPLEMENT_GET_OBJC_CLASS(wxNonControlNSControl,NSControl)
179
180 IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
181 BEGIN_EVENT_TABLE(wxControl, wxControlBase)
182 END_EVENT_TABLE()
183 WX_IMPLEMENT_COCOA_OWNER(wxControl,NSControl,NSView,NSView)
184
185 bool wxControl::Create(wxWindow *parent, wxWindowID winid,
186 const wxPoint& pos, const wxSize& size, long style,
187 const wxValidator& validator, const wxString& name)
188 {
189 wxLogTrace(wxTRACE_COCOA,wxT("Creating control with id=%d"),winid);
190 if(!CreateControl(parent,winid,pos,size,style,validator,name))
191 return false;
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];
198
199 [GetNSControl() sizeToFit];
200
201 if(m_parent)
202 m_parent->CocoaAddChild(this);
203 SetInitialFrameRect(pos,size);
204
205 // Controls should have a viewable-area tracking rect by default
206 m_visibleTrackingRectManager = new wxCocoaTrackingRectManager(this);
207
208 return true;
209 }
210
211 wxControl::~wxControl()
212 {
213 DisassociateNSControl(GetNSControl());
214 }
215
216 wxSize wxControl::DoGetBestSize() const
217 {
218 wxAutoNSAutoreleasePool pool;
219 wxASSERT(GetNSControl());
220 /* We can ask single-celled controls for their cell and get its size */
221 NSCell *cell = nil;
222 if([GetNSControl() respondsToSelector:@selector(cell)])
223 cell = [GetNSControl() cell];
224 if(cell)
225 {
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);
229 return size;
230 }
231
232 /* multi-celled control? size to fit, get the size, then set it back */
233 if([GetNSControl() respondsToSelector:@selector(sizeToFit)])
234 {
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);
241 return size;
242 }
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();
246 }
247
248 bool wxControl::ProcessCommand(wxCommandEvent& event)
249 {
250 return GetEventHandler()->ProcessEvent(event);
251 }
252
253 void wxControl::CocoaSetEnabled(bool enable)
254 {
255 [GetNSControl() setEnabled: enable];
256 }
257
258 /*static*/ void wxControl::CocoaSetLabelForObject(const wxString& label, struct objc_object *aView)
259 {
260 [aView setTitle:wxNSStringWithWxString(GetLabelText(label))];
261 }
262