]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/control.mm
added wxIsSameDouble() which wraps double comparison in a pragmas disabling icc warni...
[wxWidgets.git] / src / cocoa / control.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #ifndef WX_PRECOMP
14 #include "wx/defs.h"
15 #include "wx/control.h"
16 #include "wx/log.h"
17 #endif
18
19 #include "wx/cocoa/autorelease.h"
20
21 #import <AppKit/NSControl.h>
22 #import <AppKit/NSCell.h>
23 #import <Foundation/NSException.h>
24
25 #include <math.h>
26
27 @interface wxNonControlNSControl : NSControl
28 {
29 }
30
31 - (void)drawRect: (NSRect)rect;
32 @end // wxNonControlNSControl
33
34 @implementation wxNonControlNSControl : NSControl
35 - (void)drawRect: (NSRect)rect
36 {
37 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
38 if( !win || !win->Cocoa_drawRect(rect) )
39 [super drawRect:rect];
40 }
41 @end // wxNonControlNSControl
42
43 IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
44 BEGIN_EVENT_TABLE(wxControl, wxControlBase)
45 END_EVENT_TABLE()
46 WX_IMPLEMENT_COCOA_OWNER(wxControl,NSControl,NSView,NSView)
47
48 bool wxControl::Create(wxWindow *parent, wxWindowID winid,
49 const wxPoint& pos, const wxSize& size, long style,
50 const wxValidator& validator, const wxString& name)
51 {
52 wxLogTrace(wxTRACE_COCOA,wxT("Creating control with id=%d"),winid);
53 if(!CreateControl(parent,winid,pos,size,style,validator,name))
54 return false;
55 wxLogTrace(wxTRACE_COCOA,wxT("Created control with id=%d"),GetId());
56 m_cocoaNSView = NULL;
57 SetNSControl([[wxNonControlNSControl alloc] initWithFrame: MakeDefaultNSRect(size)]);
58 // NOTE: YES we want to release this (to match the alloc).
59 // DoAddChild(this) will retain us again since addSubView doesn't.
60 [m_cocoaNSView release];
61
62 [GetNSControl() sizeToFit];
63
64 if(m_parent)
65 m_parent->CocoaAddChild(this);
66 SetInitialFrameRect(pos,size);
67
68 return true;
69 }
70
71 wxControl::~wxControl()
72 {
73 DisassociateNSControl(GetNSControl());
74 }
75
76 wxSize wxControl::DoGetBestSize() const
77 {
78 wxAutoNSAutoreleasePool pool;
79 wxASSERT(GetNSControl());
80 /* We can ask single-celled controls for their cell and get its size */
81 NSCell *cell = nil;
82 NS_DURING
83 cell = [GetNSControl() cell];
84 NS_HANDLER
85 // TODO: if anything other than method not implemented, re-raise
86 NS_ENDHANDLER
87 if(cell)
88 {
89 NSSize cellSize = [cell cellSize];
90 wxSize size((int)ceil(cellSize.width),(int)ceil(cellSize.height));
91 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from NSCell"),this,size.x,size.y);
92 return size;
93 }
94
95 /* multi-celled control? size to fit, get the size, then set it back */
96 NSRect storedRect = [m_cocoaNSView frame];
97 bool didFit = false;
98 NS_DURING
99 [GetNSControl() sizeToFit];
100 didFit = true;
101 NS_HANDLER
102 // TODO: if anything other than method not implemented, re-raise
103 NS_ENDHANDLER
104 if(didFit)
105 {
106 NSRect cocoaRect = [m_cocoaNSView frame];
107 wxSize size((int)ceil(cocoaRect.size.width),(int)ceil(cocoaRect.size.height));
108 [m_cocoaNSView setFrame: storedRect];
109 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from sizeToFit"),this,size.x,size.y);
110 return size;
111 }
112 // Cocoa can't tell us the size, probably not an NSControl.
113 wxLogDebug(wxT("Class %s (or superclass still below wxControl) should implement DoGetBestSize()"),GetClassInfo()->GetClassName());
114 return wxControlBase::DoGetBestSize();
115 }
116
117 bool wxControl::ProcessCommand(wxCommandEvent& event)
118 {
119 return GetEventHandler()->ProcessEvent(event);
120 }
121
122 void wxControl::CocoaSetEnabled(bool enable)
123 {
124 [GetNSControl() setEnabled: enable];
125 }
126