]>
Commit | Line | Data |
---|---|---|
fb896a32 | 1 | ///////////////////////////////////////////////////////////////////////////// |
8898456d | 2 | // Name: src/cocoa/control.mm |
fb896a32 DE |
3 | // Purpose: wxControl class |
4 | // Author: David Elliiott | |
5 | // Modified by: | |
6 | // Created: 2003/02/15 | |
8898456d | 7 | // RCS-ID: $Id$ |
fb896a32 | 8 | // Copyright: (c) 2003 David Elliott |
8898456d | 9 | // Licence: wxWidgets licence |
fb896a32 DE |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #include "wx/wxprec.h" | |
8898456d | 13 | |
93fbbe07 WS |
14 | #include "wx/control.h" |
15 | ||
fb896a32 | 16 | #ifndef WX_PRECOMP |
fb896a32 DE |
17 | #include "wx/log.h" |
18 | #endif | |
19 | ||
71bfb735 | 20 | #include "wx/cocoa/autorelease.h" |
7c5a378f | 21 | #include "wx/cocoa/trackingrectmanager.h" |
a24aa427 | 22 | #include "wx/cocoa/objc/objc_uniquifying.h" |
71bfb735 | 23 | |
fb896a32 | 24 | #import <AppKit/NSControl.h> |
058d7af6 DE |
25 | #import <AppKit/NSCell.h> |
26 | #import <Foundation/NSException.h> | |
27 | ||
28 | #include <math.h> | |
fb896a32 | 29 | |
b6567e32 DE |
30 | @interface wxNonControlNSControl : NSControl |
31 | { | |
32 | } | |
33 | ||
34 | - (void)drawRect: (NSRect)rect; | |
829a2e95 DE |
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; | |
7c5a378f DE |
48 | - (void)viewDidMoveToWindow; |
49 | - (void)viewWillMoveToWindow:(NSWindow *)newWindow; | |
b6567e32 | 50 | @end // wxNonControlNSControl |
a24aa427 | 51 | WX_DECLARE_GET_OBJC_CLASS(wxNonControlNSControl,NSControl) |
b6567e32 DE |
52 | |
53 | @implementation wxNonControlNSControl : NSControl | |
829a2e95 DE |
54 | |
55 | - (BOOL)acceptsFirstMouse:(NSEvent *)theEvent | |
56 | { | |
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; | |
62 | } | |
63 | ||
b6567e32 DE |
64 | - (void)drawRect: (NSRect)rect |
65 | { | |
66 | wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self); | |
67 | if( !win || !win->Cocoa_drawRect(rect) ) | |
68 | [super drawRect:rect]; | |
69 | } | |
829a2e95 DE |
70 | |
71 | - (void)mouseDown:(NSEvent *)theEvent | |
72 | { | |
73 | wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self); | |
74 | if( !win || !win->Cocoa_mouseDown(theEvent) ) | |
75 | [super mouseDown:theEvent]; | |
76 | } | |
77 | ||
78 | - (void)mouseDragged:(NSEvent *)theEvent | |
79 | { | |
80 | wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self); | |
81 | if( !win || !win->Cocoa_mouseDragged(theEvent) ) | |
82 | [super mouseDragged:theEvent]; | |
83 | } | |
84 | ||
85 | - (void)mouseUp:(NSEvent *)theEvent | |
86 | { | |
87 | wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self); | |
88 | if( !win || !win->Cocoa_mouseUp(theEvent) ) | |
89 | [super mouseUp:theEvent]; | |
90 | } | |
91 | ||
92 | - (void)mouseMoved:(NSEvent *)theEvent | |
93 | { | |
94 | wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self); | |
95 | if( !win || !win->Cocoa_mouseMoved(theEvent) ) | |
96 | [super mouseMoved:theEvent]; | |
97 | } | |
98 | ||
99 | - (void)mouseEntered:(NSEvent *)theEvent | |
100 | { | |
101 | wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self); | |
102 | if( !win || !win->Cocoa_mouseEntered(theEvent) ) | |
103 | [super mouseEntered:theEvent]; | |
104 | } | |
105 | ||
106 | - (void)mouseExited:(NSEvent *)theEvent | |
107 | { | |
108 | wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self); | |
109 | if( !win || !win->Cocoa_mouseExited(theEvent) ) | |
110 | [super mouseExited:theEvent]; | |
111 | } | |
112 | ||
113 | - (void)rightMouseDown:(NSEvent *)theEvent | |
114 | { | |
115 | wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self); | |
116 | if( !win || !win->Cocoa_rightMouseDown(theEvent) ) | |
117 | [super rightMouseDown:theEvent]; | |
118 | } | |
119 | ||
120 | - (void)rightMouseDragged:(NSEvent *)theEvent | |
121 | { | |
122 | wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self); | |
123 | if( !win || !win->Cocoa_rightMouseDragged(theEvent) ) | |
124 | [super rightMouseDragged:theEvent]; | |
125 | } | |
126 | ||
127 | - (void)rightMouseUp:(NSEvent *)theEvent | |
128 | { | |
129 | wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self); | |
130 | if( !win || !win->Cocoa_rightMouseUp(theEvent) ) | |
131 | [super rightMouseUp:theEvent]; | |
132 | } | |
133 | ||
134 | - (void)otherMouseDown:(NSEvent *)theEvent | |
135 | { | |
136 | wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self); | |
137 | if( !win || !win->Cocoa_otherMouseDown(theEvent) ) | |
138 | [super otherMouseDown:theEvent]; | |
139 | } | |
140 | ||
141 | - (void)otherMouseDragged:(NSEvent *)theEvent | |
142 | { | |
143 | wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self); | |
144 | if( !win || !win->Cocoa_otherMouseDragged(theEvent) ) | |
145 | [super otherMouseDragged:theEvent]; | |
146 | } | |
147 | ||
148 | - (void)otherMouseUp:(NSEvent *)theEvent | |
149 | { | |
150 | wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self); | |
151 | if( !win || !win->Cocoa_otherMouseUp(theEvent) ) | |
152 | [super otherMouseUp:theEvent]; | |
153 | } | |
154 | ||
155 | - (void)resetCursorRects | |
156 | { | |
157 | wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self); | |
158 | if( !win || !win->Cocoa_resetCursorRects() ) | |
159 | [super resetCursorRects]; | |
160 | } | |
161 | ||
7c5a378f DE |
162 | - (void)viewDidMoveToWindow |
163 | { | |
164 | wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self); | |
165 | if( !win || !win->Cocoa_viewDidMoveToWindow() ) | |
166 | [super viewDidMoveToWindow]; | |
167 | } | |
168 | ||
169 | - (void)viewWillMoveToWindow:(NSWindow *)newWindow | |
170 | { | |
171 | wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self); | |
172 | if( !win || !win->Cocoa_viewWillMoveToWindow(newWindow) ) | |
173 | [super viewWillMoveToWindow:newWindow]; | |
174 | } | |
175 | ||
b6567e32 | 176 | @end // wxNonControlNSControl |
a24aa427 | 177 | WX_IMPLEMENT_GET_OBJC_CLASS(wxNonControlNSControl,NSControl) |
b6567e32 | 178 | |
fb896a32 DE |
179 | IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow) |
180 | BEGIN_EVENT_TABLE(wxControl, wxControlBase) | |
181 | END_EVENT_TABLE() | |
182 | WX_IMPLEMENT_COCOA_OWNER(wxControl,NSControl,NSView,NSView) | |
183 | ||
184 | bool wxControl::Create(wxWindow *parent, wxWindowID winid, | |
185 | const wxPoint& pos, const wxSize& size, long style, | |
186 | const wxValidator& validator, const wxString& name) | |
187 | { | |
48580976 | 188 | wxLogTrace(wxTRACE_COCOA,wxT("Creating control with id=%d"),winid); |
fb896a32 DE |
189 | if(!CreateControl(parent,winid,pos,size,style,validator,name)) |
190 | return false; | |
48580976 | 191 | wxLogTrace(wxTRACE_COCOA,wxT("Created control with id=%d"),GetId()); |
fb896a32 | 192 | m_cocoaNSView = NULL; |
a24aa427 | 193 | SetNSControl([[WX_GET_OBJC_CLASS(wxNonControlNSControl) alloc] initWithFrame: MakeDefaultNSRect(size)]); |
fb896a32 DE |
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]; | |
197 | ||
198 | [GetNSControl() sizeToFit]; | |
199 | ||
200 | if(m_parent) | |
201 | m_parent->CocoaAddChild(this); | |
8d656ea9 | 202 | SetInitialFrameRect(pos,size); |
fb896a32 | 203 | |
7c5a378f DE |
204 | // Controls should have a viewable-area tracking rect by default |
205 | m_visibleTrackingRectManager = new wxCocoaTrackingRectManager(this); | |
206 | ||
fb896a32 DE |
207 | return true; |
208 | } | |
209 | ||
210 | wxControl::~wxControl() | |
211 | { | |
911e17c6 | 212 | DisassociateNSControl(GetNSControl()); |
fb896a32 DE |
213 | } |
214 | ||
215 | wxSize wxControl::DoGetBestSize() const | |
216 | { | |
71bfb735 | 217 | wxAutoNSAutoreleasePool pool; |
058d7af6 DE |
218 | wxASSERT(GetNSControl()); |
219 | /* We can ask single-celled controls for their cell and get its size */ | |
220 | NSCell *cell = nil; | |
2465458c DE |
221 | if([GetNSControl() respondsToSelector:@selector(cell)]) |
222 | cell = [GetNSControl() cell]; | |
058d7af6 DE |
223 | if(cell) |
224 | { | |
225 | NSSize cellSize = [cell cellSize]; | |
c05c7cb5 | 226 | wxSize size((int)ceil(cellSize.width),(int)ceil(cellSize.height)); |
058d7af6 DE |
227 | wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from NSCell"),this,size.x,size.y); |
228 | return size; | |
229 | } | |
230 | ||
231 | /* multi-celled control? size to fit, get the size, then set it back */ | |
2465458c | 232 | if([GetNSControl() respondsToSelector:@selector(sizeToFit)]) |
058d7af6 | 233 | { |
2465458c DE |
234 | NSRect storedRect = [m_cocoaNSView frame]; |
235 | [GetNSControl() sizeToFit]; | |
058d7af6 | 236 | NSRect cocoaRect = [m_cocoaNSView frame]; |
c05c7cb5 | 237 | wxSize size((int)ceil(cocoaRect.size.width),(int)ceil(cocoaRect.size.height)); |
058d7af6 DE |
238 | [m_cocoaNSView setFrame: storedRect]; |
239 | wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from sizeToFit"),this,size.x,size.y); | |
240 | return size; | |
241 | } | |
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(); | |
fb896a32 DE |
245 | } |
246 | ||
247 | bool wxControl::ProcessCommand(wxCommandEvent& event) | |
248 | { | |
fb896a32 DE |
249 | return GetEventHandler()->ProcessEvent(event); |
250 | } | |
251 | ||
c5172ed1 DE |
252 | void wxControl::CocoaSetEnabled(bool enable) |
253 | { | |
254 | [GetNSControl() setEnabled: enable]; | |
255 | } |