]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/control.mm
use typed containers in wxHtmlParser instead of type-unsafe wxList/wxHashTable
[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 20#include "wx/cocoa/autorelease.h"
525007cf 21#include "wx/cocoa/string.h"
7c5a378f 22#include "wx/cocoa/trackingrectmanager.h"
a24aa427 23#include "wx/cocoa/objc/objc_uniquifying.h"
71bfb735 24
fb896a32 25#import <AppKit/NSControl.h>
058d7af6
DE
26#import <AppKit/NSCell.h>
27#import <Foundation/NSException.h>
28
29#include <math.h>
fb896a32 30
b6567e32
DE
31@interface wxNonControlNSControl : NSControl
32{
33}
34
35- (void)drawRect: (NSRect)rect;
829a2e95
DE
36- (void)mouseDown:(NSEvent *)theEvent;
37- (void)mouseDragged:(NSEvent *)theEvent;
38- (void)mouseUp:(NSEvent *)theEvent;
39- (void)mouseMoved:(NSEvent *)theEvent;
40- (void)mouseEntered:(NSEvent *)theEvent;
41- (void)mouseExited:(NSEvent *)theEvent;
42- (void)rightMouseDown:(NSEvent *)theEvent;
43- (void)rightMouseDragged:(NSEvent *)theEvent;
44- (void)rightMouseUp:(NSEvent *)theEvent;
45- (void)otherMouseDown:(NSEvent *)theEvent;
46- (void)otherMouseDragged:(NSEvent *)theEvent;
47- (void)otherMouseUp:(NSEvent *)theEvent;
48- (void)resetCursorRects;
7c5a378f
DE
49- (void)viewDidMoveToWindow;
50- (void)viewWillMoveToWindow:(NSWindow *)newWindow;
b6567e32 51@end // wxNonControlNSControl
a24aa427 52WX_DECLARE_GET_OBJC_CLASS(wxNonControlNSControl,NSControl)
b6567e32
DE
53
54@implementation wxNonControlNSControl : NSControl
829a2e95
DE
55
56- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
57{
58 bool acceptsFirstMouse = false;
59 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
60 if(!win || !win->Cocoa_acceptsFirstMouse(acceptsFirstMouse, theEvent))
61 acceptsFirstMouse = [super acceptsFirstMouse:theEvent];
62 return acceptsFirstMouse;
63}
64
b6567e32
DE
65- (void)drawRect: (NSRect)rect
66{
67 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
68 if( !win || !win->Cocoa_drawRect(rect) )
69 [super drawRect:rect];
70}
829a2e95
DE
71
72- (void)mouseDown:(NSEvent *)theEvent
73{
74 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
75 if( !win || !win->Cocoa_mouseDown(theEvent) )
76 [super mouseDown:theEvent];
77}
78
79- (void)mouseDragged:(NSEvent *)theEvent
80{
81 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
82 if( !win || !win->Cocoa_mouseDragged(theEvent) )
83 [super mouseDragged:theEvent];
84}
85
86- (void)mouseUp:(NSEvent *)theEvent
87{
88 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
89 if( !win || !win->Cocoa_mouseUp(theEvent) )
90 [super mouseUp:theEvent];
91}
92
93- (void)mouseMoved:(NSEvent *)theEvent
94{
95 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
96 if( !win || !win->Cocoa_mouseMoved(theEvent) )
97 [super mouseMoved:theEvent];
98}
99
100- (void)mouseEntered:(NSEvent *)theEvent
101{
102 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
103 if( !win || !win->Cocoa_mouseEntered(theEvent) )
104 [super mouseEntered:theEvent];
105}
106
107- (void)mouseExited:(NSEvent *)theEvent
108{
109 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
110 if( !win || !win->Cocoa_mouseExited(theEvent) )
111 [super mouseExited:theEvent];
112}
113
114- (void)rightMouseDown:(NSEvent *)theEvent
115{
116 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
117 if( !win || !win->Cocoa_rightMouseDown(theEvent) )
118 [super rightMouseDown:theEvent];
119}
120
121- (void)rightMouseDragged:(NSEvent *)theEvent
122{
123 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
124 if( !win || !win->Cocoa_rightMouseDragged(theEvent) )
125 [super rightMouseDragged:theEvent];
126}
127
128- (void)rightMouseUp:(NSEvent *)theEvent
129{
130 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
131 if( !win || !win->Cocoa_rightMouseUp(theEvent) )
132 [super rightMouseUp:theEvent];
133}
134
135- (void)otherMouseDown:(NSEvent *)theEvent
136{
137 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
138 if( !win || !win->Cocoa_otherMouseDown(theEvent) )
139 [super otherMouseDown:theEvent];
140}
141
142- (void)otherMouseDragged:(NSEvent *)theEvent
143{
144 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
145 if( !win || !win->Cocoa_otherMouseDragged(theEvent) )
146 [super otherMouseDragged:theEvent];
147}
148
149- (void)otherMouseUp:(NSEvent *)theEvent
150{
151 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
152 if( !win || !win->Cocoa_otherMouseUp(theEvent) )
153 [super otherMouseUp:theEvent];
154}
155
156- (void)resetCursorRects
157{
158 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
159 if( !win || !win->Cocoa_resetCursorRects() )
160 [super resetCursorRects];
161}
162
7c5a378f
DE
163- (void)viewDidMoveToWindow
164{
165 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
166 if( !win || !win->Cocoa_viewDidMoveToWindow() )
167 [super viewDidMoveToWindow];
168}
169
170- (void)viewWillMoveToWindow:(NSWindow *)newWindow
171{
172 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
173 if( !win || !win->Cocoa_viewWillMoveToWindow(newWindow) )
174 [super viewWillMoveToWindow:newWindow];
175}
176
b6567e32 177@end // wxNonControlNSControl
a24aa427 178WX_IMPLEMENT_GET_OBJC_CLASS(wxNonControlNSControl,NSControl)
b6567e32 179
fb896a32
DE
180IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
181BEGIN_EVENT_TABLE(wxControl, wxControlBase)
182END_EVENT_TABLE()
183WX_IMPLEMENT_COCOA_OWNER(wxControl,NSControl,NSView,NSView)
184
185bool wxControl::Create(wxWindow *parent, wxWindowID winid,
186 const wxPoint& pos, const wxSize& size, long style,
187 const wxValidator& validator, const wxString& name)
188{
48580976 189 wxLogTrace(wxTRACE_COCOA,wxT("Creating control with id=%d"),winid);
fb896a32
DE
190 if(!CreateControl(parent,winid,pos,size,style,validator,name))
191 return false;
48580976 192 wxLogTrace(wxTRACE_COCOA,wxT("Created control with id=%d"),GetId());
fb896a32 193 m_cocoaNSView = NULL;
a24aa427 194 SetNSControl([[WX_GET_OBJC_CLASS(wxNonControlNSControl) alloc] initWithFrame: MakeDefaultNSRect(size)]);
fb896a32
DE
195 // NOTE: YES we want to release this (to match the alloc).
196 // DoAddChild(this) will retain us again since addSubView doesn't.
197 [m_cocoaNSView release];
198
199 [GetNSControl() sizeToFit];
200
201 if(m_parent)
202 m_parent->CocoaAddChild(this);
8d656ea9 203 SetInitialFrameRect(pos,size);
fb896a32 204
7c5a378f
DE
205 // Controls should have a viewable-area tracking rect by default
206 m_visibleTrackingRectManager = new wxCocoaTrackingRectManager(this);
207
fb896a32
DE
208 return true;
209}
210
211wxControl::~wxControl()
212{
911e17c6 213 DisassociateNSControl(GetNSControl());
fb896a32
DE
214}
215
216wxSize wxControl::DoGetBestSize() const
217{
71bfb735 218 wxAutoNSAutoreleasePool pool;
058d7af6
DE
219 wxASSERT(GetNSControl());
220 /* We can ask single-celled controls for their cell and get its size */
221 NSCell *cell = nil;
2465458c
DE
222 if([GetNSControl() respondsToSelector:@selector(cell)])
223 cell = [GetNSControl() cell];
058d7af6
DE
224 if(cell)
225 {
226 NSSize cellSize = [cell cellSize];
c05c7cb5 227 wxSize size((int)ceil(cellSize.width),(int)ceil(cellSize.height));
058d7af6
DE
228 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from NSCell"),this,size.x,size.y);
229 return size;
230 }
231
232 /* multi-celled control? size to fit, get the size, then set it back */
2465458c 233 if([GetNSControl() respondsToSelector:@selector(sizeToFit)])
058d7af6 234 {
2465458c
DE
235 NSRect storedRect = [m_cocoaNSView frame];
236 [GetNSControl() sizeToFit];
058d7af6 237 NSRect cocoaRect = [m_cocoaNSView frame];
c05c7cb5 238 wxSize size((int)ceil(cocoaRect.size.width),(int)ceil(cocoaRect.size.height));
058d7af6
DE
239 [m_cocoaNSView setFrame: storedRect];
240 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from sizeToFit"),this,size.x,size.y);
241 return size;
242 }
243 // Cocoa can't tell us the size, probably not an NSControl.
244 wxLogDebug(wxT("Class %s (or superclass still below wxControl) should implement DoGetBestSize()"),GetClassInfo()->GetClassName());
245 return wxControlBase::DoGetBestSize();
fb896a32
DE
246}
247
248bool wxControl::ProcessCommand(wxCommandEvent& event)
249{
fb896a32
DE
250 return GetEventHandler()->ProcessEvent(event);
251}
252
c5172ed1
DE
253void wxControl::CocoaSetEnabled(bool enable)
254{
255 [GetNSControl() setEnabled: enable];
256}
525007cf
DE
257
258/*static*/ void wxControl::CocoaSetLabelForObject(const wxString& label, struct objc_object *aView)
259{
260 [aView setTitle:wxNSStringWithWxString(GetLabelText(label))];
261}
262