Allow many Objective-C classes to be uniquified at runtime.
[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/objc/objc_uniquifying.h"
22
23 #import <AppKit/NSControl.h>
24 #import <AppKit/NSCell.h>
25 #import <Foundation/NSException.h>
26
27 #include <math.h>
28
29 @interface wxNonControlNSControl : NSControl
30 {
31 }
32
33 - (void)drawRect: (NSRect)rect;
34 - (void)mouseDown:(NSEvent *)theEvent;
35 - (void)mouseDragged:(NSEvent *)theEvent;
36 - (void)mouseUp:(NSEvent *)theEvent;
37 - (void)mouseMoved:(NSEvent *)theEvent;
38 - (void)mouseEntered:(NSEvent *)theEvent;
39 - (void)mouseExited:(NSEvent *)theEvent;
40 - (void)rightMouseDown:(NSEvent *)theEvent;
41 - (void)rightMouseDragged:(NSEvent *)theEvent;
42 - (void)rightMouseUp:(NSEvent *)theEvent;
43 - (void)otherMouseDown:(NSEvent *)theEvent;
44 - (void)otherMouseDragged:(NSEvent *)theEvent;
45 - (void)otherMouseUp:(NSEvent *)theEvent;
46 - (void)resetCursorRects;
47 @end // wxNonControlNSControl
48 WX_DECLARE_GET_OBJC_CLASS(wxNonControlNSControl,NSControl)
49
50 @implementation wxNonControlNSControl : NSControl
51
52 - (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
53 {
54     bool acceptsFirstMouse = false;
55     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
56     if(!win || !win->Cocoa_acceptsFirstMouse(acceptsFirstMouse, theEvent))
57         acceptsFirstMouse = [super acceptsFirstMouse:theEvent];
58     return acceptsFirstMouse;
59 }
60
61 - (void)drawRect: (NSRect)rect
62 {
63     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
64     if( !win || !win->Cocoa_drawRect(rect) )
65         [super drawRect:rect];
66 }
67
68 - (void)mouseDown:(NSEvent *)theEvent
69 {
70     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
71     if( !win || !win->Cocoa_mouseDown(theEvent) )
72         [super mouseDown:theEvent];
73 }
74
75 - (void)mouseDragged:(NSEvent *)theEvent
76 {
77     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
78     if( !win || !win->Cocoa_mouseDragged(theEvent) )
79         [super mouseDragged:theEvent];
80 }
81
82 - (void)mouseUp:(NSEvent *)theEvent
83 {
84     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
85     if( !win || !win->Cocoa_mouseUp(theEvent) )
86         [super mouseUp:theEvent];
87 }
88
89 - (void)mouseMoved:(NSEvent *)theEvent
90 {
91     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
92     if( !win || !win->Cocoa_mouseMoved(theEvent) )
93         [super mouseMoved:theEvent];
94 }
95
96 - (void)mouseEntered:(NSEvent *)theEvent
97 {
98     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
99     if( !win || !win->Cocoa_mouseEntered(theEvent) )
100         [super mouseEntered:theEvent];
101 }
102
103 - (void)mouseExited:(NSEvent *)theEvent
104 {
105     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
106     if( !win || !win->Cocoa_mouseExited(theEvent) )
107         [super mouseExited:theEvent];
108 }
109
110 - (void)rightMouseDown:(NSEvent *)theEvent
111 {
112     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
113     if( !win || !win->Cocoa_rightMouseDown(theEvent) )
114         [super rightMouseDown:theEvent];
115 }
116
117 - (void)rightMouseDragged:(NSEvent *)theEvent
118 {
119     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
120     if( !win || !win->Cocoa_rightMouseDragged(theEvent) )
121         [super rightMouseDragged:theEvent];
122 }
123
124 - (void)rightMouseUp:(NSEvent *)theEvent
125 {
126     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
127     if( !win || !win->Cocoa_rightMouseUp(theEvent) )
128         [super rightMouseUp:theEvent];
129 }
130
131 - (void)otherMouseDown:(NSEvent *)theEvent
132 {
133     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
134     if( !win || !win->Cocoa_otherMouseDown(theEvent) )
135         [super otherMouseDown:theEvent];
136 }
137
138 - (void)otherMouseDragged:(NSEvent *)theEvent
139 {
140     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
141     if( !win || !win->Cocoa_otherMouseDragged(theEvent) )
142         [super otherMouseDragged:theEvent];
143 }
144
145 - (void)otherMouseUp:(NSEvent *)theEvent
146 {
147     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
148     if( !win || !win->Cocoa_otherMouseUp(theEvent) )
149         [super otherMouseUp:theEvent];
150 }
151
152 - (void)resetCursorRects
153 {
154     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
155     if( !win || !win->Cocoa_resetCursorRects() )
156         [super resetCursorRects];
157 }
158
159 @end // wxNonControlNSControl
160 WX_IMPLEMENT_GET_OBJC_CLASS(wxNonControlNSControl,NSControl)
161
162 IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
163 BEGIN_EVENT_TABLE(wxControl, wxControlBase)
164 END_EVENT_TABLE()
165 WX_IMPLEMENT_COCOA_OWNER(wxControl,NSControl,NSView,NSView)
166
167 bool wxControl::Create(wxWindow *parent, wxWindowID winid,
168             const wxPoint& pos, const wxSize& size, long style,
169             const wxValidator& validator, const wxString& name)
170 {
171     wxLogTrace(wxTRACE_COCOA,wxT("Creating control with id=%d"),winid);
172     if(!CreateControl(parent,winid,pos,size,style,validator,name))
173         return false;
174     wxLogTrace(wxTRACE_COCOA,wxT("Created control with id=%d"),GetId());
175     m_cocoaNSView = NULL;
176     SetNSControl([[WX_GET_OBJC_CLASS(wxNonControlNSControl) alloc] initWithFrame: MakeDefaultNSRect(size)]);
177     // NOTE: YES we want to release this (to match the alloc).
178     // DoAddChild(this) will retain us again since addSubView doesn't.
179     [m_cocoaNSView release];
180
181     [GetNSControl() sizeToFit];
182
183     if(m_parent)
184         m_parent->CocoaAddChild(this);
185     SetInitialFrameRect(pos,size);
186
187     return true;
188 }
189
190 wxControl::~wxControl()
191 {
192     DisassociateNSControl(GetNSControl());
193 }
194
195 wxSize wxControl::DoGetBestSize() const
196 {
197     wxAutoNSAutoreleasePool pool;
198     wxASSERT(GetNSControl());
199     /* We can ask single-celled controls for their cell and get its size */
200     NSCell *cell = nil;
201 NS_DURING
202     cell = [GetNSControl() cell];
203 NS_HANDLER
204     // TODO: if anything other than method not implemented, re-raise
205 NS_ENDHANDLER
206     if(cell)
207     {
208         NSSize cellSize = [cell cellSize];
209         wxSize size((int)ceil(cellSize.width),(int)ceil(cellSize.height));
210         wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from NSCell"),this,size.x,size.y);
211         return size;
212     }
213
214     /* multi-celled control? size to fit, get the size, then set it back */
215     NSRect storedRect = [m_cocoaNSView frame];
216     bool didFit = false;
217 NS_DURING
218     [GetNSControl() sizeToFit];
219     didFit = true;
220 NS_HANDLER
221     // TODO: if anything other than method not implemented, re-raise
222 NS_ENDHANDLER
223     if(didFit)
224     {
225         NSRect cocoaRect = [m_cocoaNSView frame];
226         wxSize size((int)ceil(cocoaRect.size.width),(int)ceil(cocoaRect.size.height));
227         [m_cocoaNSView setFrame: storedRect];
228         wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from sizeToFit"),this,size.x,size.y);
229         return size;
230     }
231     // Cocoa can't tell us the size, probably not an NSControl.
232     wxLogDebug(wxT("Class %s (or superclass still below wxControl) should implement DoGetBestSize()"),GetClassInfo()->GetClassName());
233     return wxControlBase::DoGetBestSize();
234 }
235
236 bool wxControl::ProcessCommand(wxCommandEvent& event)
237 {
238     return GetEventHandler()->ProcessEvent(event);
239 }
240
241 void wxControl::CocoaSetEnabled(bool enable)
242 {
243     [GetNSControl() setEnabled: enable];
244 }