Use WXNSView just like wxWindow does when creating a wxControl. There are
[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 #include "wx/cocoa/objc/NSView.h"
25
26 #import <AppKit/NSControl.h>
27 #import <AppKit/NSCell.h>
28 #import <Foundation/NSException.h>
29
30 #include <math.h>
31
32 @interface wxNonControlNSControl : NSControl
33 {
34 }
35
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)
54
55 @implementation wxNonControlNSControl : NSControl
56
57 - (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
58 {
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;
64 }
65
66 - (void)drawRect: (NSRect)rect
67 {
68     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
69     if( !win || !win->Cocoa_drawRect(rect) )
70         [super drawRect:rect];
71 }
72
73 - (void)mouseDown:(NSEvent *)theEvent
74 {
75     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
76     if( !win || !win->Cocoa_mouseDown(theEvent) )
77         [super mouseDown:theEvent];
78 }
79
80 - (void)mouseDragged:(NSEvent *)theEvent
81 {
82     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
83     if( !win || !win->Cocoa_mouseDragged(theEvent) )
84         [super mouseDragged:theEvent];
85 }
86
87 - (void)mouseUp:(NSEvent *)theEvent
88 {
89     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
90     if( !win || !win->Cocoa_mouseUp(theEvent) )
91         [super mouseUp:theEvent];
92 }
93
94 - (void)mouseMoved:(NSEvent *)theEvent
95 {
96     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
97     if( !win || !win->Cocoa_mouseMoved(theEvent) )
98         [super mouseMoved:theEvent];
99 }
100
101 - (void)mouseEntered:(NSEvent *)theEvent
102 {
103     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
104     if( !win || !win->Cocoa_mouseEntered(theEvent) )
105         [super mouseEntered:theEvent];
106 }
107
108 - (void)mouseExited:(NSEvent *)theEvent
109 {
110     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
111     if( !win || !win->Cocoa_mouseExited(theEvent) )
112         [super mouseExited:theEvent];
113 }
114
115 - (void)rightMouseDown:(NSEvent *)theEvent
116 {
117     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
118     if( !win || !win->Cocoa_rightMouseDown(theEvent) )
119         [super rightMouseDown:theEvent];
120 }
121
122 - (void)rightMouseDragged:(NSEvent *)theEvent
123 {
124     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
125     if( !win || !win->Cocoa_rightMouseDragged(theEvent) )
126         [super rightMouseDragged:theEvent];
127 }
128
129 - (void)rightMouseUp:(NSEvent *)theEvent
130 {
131     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
132     if( !win || !win->Cocoa_rightMouseUp(theEvent) )
133         [super rightMouseUp:theEvent];
134 }
135
136 - (void)otherMouseDown:(NSEvent *)theEvent
137 {
138     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
139     if( !win || !win->Cocoa_otherMouseDown(theEvent) )
140         [super otherMouseDown:theEvent];
141 }
142
143 - (void)otherMouseDragged:(NSEvent *)theEvent
144 {
145     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
146     if( !win || !win->Cocoa_otherMouseDragged(theEvent) )
147         [super otherMouseDragged:theEvent];
148 }
149
150 - (void)otherMouseUp:(NSEvent *)theEvent
151 {
152     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
153     if( !win || !win->Cocoa_otherMouseUp(theEvent) )
154         [super otherMouseUp:theEvent];
155 }
156
157 - (void)resetCursorRects
158 {
159     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
160     if( !win || !win->Cocoa_resetCursorRects() )
161         [super resetCursorRects];
162 }
163
164 - (void)viewDidMoveToWindow
165 {
166     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
167     if( !win || !win->Cocoa_viewDidMoveToWindow() )
168         [super viewDidMoveToWindow];
169 }
170
171 - (void)viewWillMoveToWindow:(NSWindow *)newWindow
172 {
173     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
174     if( !win || !win->Cocoa_viewWillMoveToWindow(newWindow) )
175         [super viewWillMoveToWindow:newWindow];
176 }
177
178 @end // wxNonControlNSControl
179 WX_IMPLEMENT_GET_OBJC_CLASS(wxNonControlNSControl,NSControl)
180
181 IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
182 BEGIN_EVENT_TABLE(wxControl, wxControlBase)
183 END_EVENT_TABLE()
184 WX_IMPLEMENT_COCOA_OWNER(wxControl,NSControl,NSView,NSView)
185
186 bool wxControl::Create(wxWindow *parent, wxWindowID winid,
187             const wxPoint& pos, const wxSize& size, long style,
188             const wxValidator& validator, const wxString& name)
189 {
190     wxLogTrace(wxTRACE_COCOA,wxT("Creating control with id=%d"),winid);
191     if(!CreateControl(parent,winid,pos,size,style,validator,name))
192         return false;
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];
199
200     if(m_parent)
201         m_parent->CocoaAddChild(this);
202     SetInitialFrameRect(pos,size);
203
204     // Controls should have a viewable-area tracking rect by default
205     m_visibleTrackingRectManager = new wxCocoaTrackingRectManager(this);
206
207     return true;
208 }
209
210 wxControl::~wxControl()
211 {
212     DisassociateNSControl(GetNSControl());
213 }
214
215 wxSize wxControl::DoGetBestSize() const
216 {
217     wxAutoNSAutoreleasePool pool;
218     wxASSERT(GetNSControl());
219     /* We can ask single-celled controls for their cell and get its size */
220     NSCell *cell = nil;
221     if([GetNSControl() respondsToSelector:@selector(cell)])
222         cell = [GetNSControl() cell];
223     if(cell)
224     {
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);
228         return size;
229     }
230
231     /* multi-celled control? size to fit, get the size, then set it back */
232     if([GetNSControl() respondsToSelector:@selector(sizeToFit)])
233     {
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);
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();
245 }
246
247 bool wxControl::ProcessCommand(wxCommandEvent& event)
248 {
249     return GetEventHandler()->ProcessEvent(event);
250 }
251
252 void wxControl::CocoaSetEnabled(bool enable)
253 {
254     if([GetNSControl() respondsToSelector:@selector(setEnabled:)])
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