]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/cocoa/control.mm
corrected #if check which was written as #ifdef
[wxWidgets.git] / src / cocoa / control.mm
... / ...
CommitLineData
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/trackingrectmanager.h"
22#include "wx/cocoa/objc/objc_uniquifying.h"
23
24#import <AppKit/NSControl.h>
25#import <AppKit/NSCell.h>
26#import <Foundation/NSException.h>
27
28#include <math.h>
29
30@interface wxNonControlNSControl : NSControl
31{
32}
33
34- (void)drawRect: (NSRect)rect;
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;
48- (void)viewDidMoveToWindow;
49- (void)viewWillMoveToWindow:(NSWindow *)newWindow;
50@end // wxNonControlNSControl
51WX_DECLARE_GET_OBJC_CLASS(wxNonControlNSControl,NSControl)
52
53@implementation wxNonControlNSControl : NSControl
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
64- (void)drawRect: (NSRect)rect
65{
66 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
67 if( !win || !win->Cocoa_drawRect(rect) )
68 [super drawRect:rect];
69}
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
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
176@end // wxNonControlNSControl
177WX_IMPLEMENT_GET_OBJC_CLASS(wxNonControlNSControl,NSControl)
178
179IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
180BEGIN_EVENT_TABLE(wxControl, wxControlBase)
181END_EVENT_TABLE()
182WX_IMPLEMENT_COCOA_OWNER(wxControl,NSControl,NSView,NSView)
183
184bool wxControl::Create(wxWindow *parent, wxWindowID winid,
185 const wxPoint& pos, const wxSize& size, long style,
186 const wxValidator& validator, const wxString& name)
187{
188 wxLogTrace(wxTRACE_COCOA,wxT("Creating control with id=%d"),winid);
189 if(!CreateControl(parent,winid,pos,size,style,validator,name))
190 return false;
191 wxLogTrace(wxTRACE_COCOA,wxT("Created control with id=%d"),GetId());
192 m_cocoaNSView = NULL;
193 SetNSControl([[WX_GET_OBJC_CLASS(wxNonControlNSControl) alloc] initWithFrame: MakeDefaultNSRect(size)]);
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);
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
210wxControl::~wxControl()
211{
212 DisassociateNSControl(GetNSControl());
213}
214
215wxSize 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;
221NS_DURING
222 cell = [GetNSControl() cell];
223NS_HANDLER
224 // TODO: if anything other than method not implemented, re-raise
225NS_ENDHANDLER
226 if(cell)
227 {
228 NSSize cellSize = [cell cellSize];
229 wxSize size((int)ceil(cellSize.width),(int)ceil(cellSize.height));
230 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from NSCell"),this,size.x,size.y);
231 return size;
232 }
233
234 /* multi-celled control? size to fit, get the size, then set it back */
235 NSRect storedRect = [m_cocoaNSView frame];
236 bool didFit = false;
237NS_DURING
238 [GetNSControl() sizeToFit];
239 didFit = true;
240NS_HANDLER
241 // TODO: if anything other than method not implemented, re-raise
242NS_ENDHANDLER
243 if(didFit)
244 {
245 NSRect cocoaRect = [m_cocoaNSView frame];
246 wxSize size((int)ceil(cocoaRect.size.width),(int)ceil(cocoaRect.size.height));
247 [m_cocoaNSView setFrame: storedRect];
248 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from sizeToFit"),this,size.x,size.y);
249 return size;
250 }
251 // Cocoa can't tell us the size, probably not an NSControl.
252 wxLogDebug(wxT("Class %s (or superclass still below wxControl) should implement DoGetBestSize()"),GetClassInfo()->GetClassName());
253 return wxControlBase::DoGetBestSize();
254}
255
256bool wxControl::ProcessCommand(wxCommandEvent& event)
257{
258 return GetEventHandler()->ProcessEvent(event);
259}
260
261void wxControl::CocoaSetEnabled(bool enable)
262{
263 [GetNSControl() setEnabled: enable];
264}