Remove Objective-C class posing for everything except for NSApplication.
[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
22 #import <AppKit/NSControl.h>
23 #import <AppKit/NSCell.h>
24 #import <Foundation/NSException.h>
25
26 #include <math.h>
27
28 @interface wxNonControlNSControl : NSControl
29 {
30 }
31
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
47
48 @implementation wxNonControlNSControl : NSControl
49
50 - (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
51 {
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;
57 }
58
59 - (void)drawRect: (NSRect)rect
60 {
61     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
62     if( !win || !win->Cocoa_drawRect(rect) )
63         [super drawRect:rect];
64 }
65
66 - (void)mouseDown:(NSEvent *)theEvent
67 {
68     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
69     if( !win || !win->Cocoa_mouseDown(theEvent) )
70         [super mouseDown:theEvent];
71 }
72
73 - (void)mouseDragged:(NSEvent *)theEvent
74 {
75     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
76     if( !win || !win->Cocoa_mouseDragged(theEvent) )
77         [super mouseDragged:theEvent];
78 }
79
80 - (void)mouseUp:(NSEvent *)theEvent
81 {
82     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
83     if( !win || !win->Cocoa_mouseUp(theEvent) )
84         [super mouseUp:theEvent];
85 }
86
87 - (void)mouseMoved:(NSEvent *)theEvent
88 {
89     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
90     if( !win || !win->Cocoa_mouseMoved(theEvent) )
91         [super mouseMoved:theEvent];
92 }
93
94 - (void)mouseEntered:(NSEvent *)theEvent
95 {
96     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
97     if( !win || !win->Cocoa_mouseEntered(theEvent) )
98         [super mouseEntered:theEvent];
99 }
100
101 - (void)mouseExited:(NSEvent *)theEvent
102 {
103     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
104     if( !win || !win->Cocoa_mouseExited(theEvent) )
105         [super mouseExited:theEvent];
106 }
107
108 - (void)rightMouseDown:(NSEvent *)theEvent
109 {
110     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
111     if( !win || !win->Cocoa_rightMouseDown(theEvent) )
112         [super rightMouseDown:theEvent];
113 }
114
115 - (void)rightMouseDragged:(NSEvent *)theEvent
116 {
117     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
118     if( !win || !win->Cocoa_rightMouseDragged(theEvent) )
119         [super rightMouseDragged:theEvent];
120 }
121
122 - (void)rightMouseUp:(NSEvent *)theEvent
123 {
124     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
125     if( !win || !win->Cocoa_rightMouseUp(theEvent) )
126         [super rightMouseUp:theEvent];
127 }
128
129 - (void)otherMouseDown:(NSEvent *)theEvent
130 {
131     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
132     if( !win || !win->Cocoa_otherMouseDown(theEvent) )
133         [super otherMouseDown:theEvent];
134 }
135
136 - (void)otherMouseDragged:(NSEvent *)theEvent
137 {
138     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
139     if( !win || !win->Cocoa_otherMouseDragged(theEvent) )
140         [super otherMouseDragged:theEvent];
141 }
142
143 - (void)otherMouseUp:(NSEvent *)theEvent
144 {
145     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
146     if( !win || !win->Cocoa_otherMouseUp(theEvent) )
147         [super otherMouseUp:theEvent];
148 }
149
150 - (void)resetCursorRects
151 {
152     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
153     if( !win || !win->Cocoa_resetCursorRects() )
154         [super resetCursorRects];
155 }
156
157 @end // wxNonControlNSControl
158
159 IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
160 BEGIN_EVENT_TABLE(wxControl, wxControlBase)
161 END_EVENT_TABLE()
162 WX_IMPLEMENT_COCOA_OWNER(wxControl,NSControl,NSView,NSView)
163
164 bool wxControl::Create(wxWindow *parent, wxWindowID winid,
165             const wxPoint& pos, const wxSize& size, long style,
166             const wxValidator& validator, const wxString& name)
167 {
168     wxLogTrace(wxTRACE_COCOA,wxT("Creating control with id=%d"),winid);
169     if(!CreateControl(parent,winid,pos,size,style,validator,name))
170         return false;
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];
177
178     [GetNSControl() sizeToFit];
179
180     if(m_parent)
181         m_parent->CocoaAddChild(this);
182     SetInitialFrameRect(pos,size);
183
184     return true;
185 }
186
187 wxControl::~wxControl()
188 {
189     DisassociateNSControl(GetNSControl());
190 }
191
192 wxSize wxControl::DoGetBestSize() const
193 {
194     wxAutoNSAutoreleasePool pool;
195     wxASSERT(GetNSControl());
196     /* We can ask single-celled controls for their cell and get its size */
197     NSCell *cell = nil;
198 NS_DURING
199     cell = [GetNSControl() cell];
200 NS_HANDLER
201     // TODO: if anything other than method not implemented, re-raise
202 NS_ENDHANDLER
203     if(cell)
204     {
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);
208         return size;
209     }
210
211     /* multi-celled control? size to fit, get the size, then set it back */
212     NSRect storedRect = [m_cocoaNSView frame];
213     bool didFit = false;
214 NS_DURING
215     [GetNSControl() sizeToFit];
216     didFit = true;
217 NS_HANDLER
218     // TODO: if anything other than method not implemented, re-raise
219 NS_ENDHANDLER
220     if(didFit)
221     {
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);
226         return size;
227     }
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();
231 }
232
233 bool wxControl::ProcessCommand(wxCommandEvent& event)
234 {
235     return GetEventHandler()->ProcessEvent(event);
236 }
237
238 void wxControl::CocoaSetEnabled(bool enable)
239 {
240     [GetNSControl() setEnabled: enable];
241 }