]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/control.mm
XRC spec: document the "focused" window property.
[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 // Copyright: (c) 2003 David Elliott
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/wxprec.h"
12
13 #include "wx/control.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/log.h"
17 #endif
18
19 #include "wx/cocoa/autorelease.h"
20 #include "wx/cocoa/string.h"
21 #include "wx/cocoa/trackingrectmanager.h"
22 #include "wx/cocoa/objc/objc_uniquifying.h"
23 #include "wx/cocoa/objc/NSView.h"
24
25 #import <AppKit/NSControl.h>
26 #import <AppKit/NSCell.h>
27 #import <Foundation/NSException.h>
28
29 #include <math.h>
30
31 IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
32 BEGIN_EVENT_TABLE(wxControl, wxControlBase)
33 END_EVENT_TABLE()
34 WX_IMPLEMENT_COCOA_OWNER(wxControl,NSControl,NSView,NSView)
35
36 bool wxControl::Create(wxWindow *parent, wxWindowID winid,
37 const wxPoint& pos, const wxSize& size, long style,
38 const wxValidator& validator, const wxString& name)
39 {
40 wxLogTrace(wxTRACE_COCOA,wxT("Creating control with id=%d"),winid);
41 if(!CreateControl(parent,winid,pos,size,style,validator,name))
42 return false;
43 wxLogTrace(wxTRACE_COCOA,wxT("Created control with id=%d"),GetId());
44 m_cocoaNSView = NULL;
45 SetNSControl([[WX_GET_OBJC_CLASS(WXNSView) alloc] initWithFrame: MakeDefaultNSRect(size)]);
46 // NOTE: YES we want to release this (to match the alloc).
47 // DoAddChild(this) will retain us again since addSubView doesn't.
48 [m_cocoaNSView release];
49
50 if(m_parent)
51 m_parent->CocoaAddChild(this);
52 SetInitialFrameRect(pos,size);
53
54 // Controls should have a viewable-area tracking rect by default
55 m_visibleTrackingRectManager = new wxCocoaTrackingRectManager(this);
56
57 return true;
58 }
59
60 wxControl::~wxControl()
61 {
62 DisassociateNSControl(GetNSControl());
63 }
64
65 wxSize wxControl::DoGetBestSize() const
66 {
67 wxAutoNSAutoreleasePool pool;
68 wxASSERT(GetNSControl());
69 /* We can ask single-celled controls for their cell and get its size */
70 NSCell *cell = nil;
71 if([GetNSControl() respondsToSelector:@selector(cell)])
72 cell = [GetNSControl() cell];
73 if(cell)
74 {
75 NSSize cellSize = [cell cellSize];
76 wxSize size((int)ceil(cellSize.width),(int)ceil(cellSize.height));
77 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from NSCell"),this,size.x,size.y);
78 return size;
79 }
80
81 /* multi-celled control? size to fit, get the size, then set it back */
82 if([GetNSControl() respondsToSelector:@selector(sizeToFit)])
83 {
84 NSRect storedRect = [m_cocoaNSView frame];
85 [GetNSControl() sizeToFit];
86 NSRect cocoaRect = [m_cocoaNSView frame];
87 wxSize size((int)ceil(cocoaRect.size.width),(int)ceil(cocoaRect.size.height));
88 [m_cocoaNSView setFrame: storedRect];
89 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from sizeToFit"),this,size.x,size.y);
90 return size;
91 }
92 // Cocoa can't tell us the size, probably not an NSControl.
93 wxLogDebug(wxT("Class %s (or superclass still below wxControl) should implement DoGetBestSize()"),GetClassInfo()->GetClassName());
94 return wxControlBase::DoGetBestSize();
95 }
96
97 bool wxControl::ProcessCommand(wxCommandEvent& event)
98 {
99 return HandleWindowEvent(event);
100 }
101
102 void wxControl::CocoaSetEnabled(bool enable)
103 {
104 if([GetNSControl() respondsToSelector:@selector(setEnabled:)])
105 [GetNSControl() setEnabled: enable];
106 }
107
108 /*static*/ void wxControl::CocoaSetLabelForObject(const wxString& label, struct objc_object *aView)
109 {
110 [aView setTitle:wxNSStringWithWxString(GetLabelText(label))];
111 }
112