]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/control.mm
final revision
[wxWidgets.git] / src / cocoa / control.mm
CommitLineData
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
DE
20#include "wx/cocoa/autorelease.h"
21
fb896a32 22#import <AppKit/NSControl.h>
058d7af6
DE
23#import <AppKit/NSCell.h>
24#import <Foundation/NSException.h>
25
26#include <math.h>
fb896a32 27
b6567e32
DE
28@interface wxNonControlNSControl : NSControl
29{
30}
31
32- (void)drawRect: (NSRect)rect;
829a2e95
DE
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;
b6567e32
DE
46@end // wxNonControlNSControl
47
48@implementation wxNonControlNSControl : NSControl
829a2e95
DE
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
b6567e32
DE
59- (void)drawRect: (NSRect)rect
60{
61 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
62 if( !win || !win->Cocoa_drawRect(rect) )
63 [super drawRect:rect];
64}
829a2e95
DE
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
b6567e32
DE
157@end // wxNonControlNSControl
158
fb896a32
DE
159IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
160BEGIN_EVENT_TABLE(wxControl, wxControlBase)
161END_EVENT_TABLE()
162WX_IMPLEMENT_COCOA_OWNER(wxControl,NSControl,NSView,NSView)
163
164bool wxControl::Create(wxWindow *parent, wxWindowID winid,
165 const wxPoint& pos, const wxSize& size, long style,
166 const wxValidator& validator, const wxString& name)
167{
48580976 168 wxLogTrace(wxTRACE_COCOA,wxT("Creating control with id=%d"),winid);
fb896a32
DE
169 if(!CreateControl(parent,winid,pos,size,style,validator,name))
170 return false;
48580976 171 wxLogTrace(wxTRACE_COCOA,wxT("Created control with id=%d"),GetId());
fb896a32 172 m_cocoaNSView = NULL;
b6567e32 173 SetNSControl([[wxNonControlNSControl alloc] initWithFrame: MakeDefaultNSRect(size)]);
fb896a32
DE
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);
8d656ea9 182 SetInitialFrameRect(pos,size);
fb896a32
DE
183
184 return true;
185}
186
187wxControl::~wxControl()
188{
911e17c6 189 DisassociateNSControl(GetNSControl());
fb896a32
DE
190}
191
192wxSize wxControl::DoGetBestSize() const
193{
71bfb735 194 wxAutoNSAutoreleasePool pool;
058d7af6
DE
195 wxASSERT(GetNSControl());
196 /* We can ask single-celled controls for their cell and get its size */
197 NSCell *cell = nil;
198NS_DURING
199 cell = [GetNSControl() cell];
200NS_HANDLER
201 // TODO: if anything other than method not implemented, re-raise
202NS_ENDHANDLER
203 if(cell)
204 {
205 NSSize cellSize = [cell cellSize];
c05c7cb5 206 wxSize size((int)ceil(cellSize.width),(int)ceil(cellSize.height));
058d7af6
DE
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 */
fb896a32 212 NSRect storedRect = [m_cocoaNSView frame];
058d7af6
DE
213 bool didFit = false;
214NS_DURING
fb896a32 215 [GetNSControl() sizeToFit];
058d7af6
DE
216 didFit = true;
217NS_HANDLER
218 // TODO: if anything other than method not implemented, re-raise
219NS_ENDHANDLER
220 if(didFit)
221 {
222 NSRect cocoaRect = [m_cocoaNSView frame];
c05c7cb5 223 wxSize size((int)ceil(cocoaRect.size.width),(int)ceil(cocoaRect.size.height));
058d7af6
DE
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();
fb896a32
DE
231}
232
233bool wxControl::ProcessCommand(wxCommandEvent& event)
234{
fb896a32
DE
235 return GetEventHandler()->ProcessEvent(event);
236}
237
c5172ed1
DE
238void wxControl::CocoaSetEnabled(bool enable)
239{
240 [GetNSControl() setEnabled: enable];
241}