]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/control.mm
* Made GSocket::GetError() exp/imp from net library and provide an inline
[wxWidgets.git] / src / cocoa / control.mm
CommitLineData
fb896a32
DE
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
065e208e 9// Licence: wxWidgets licence
fb896a32
DE
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
71bfb735
DE
19#include "wx/cocoa/autorelease.h"
20
fb896a32 21#import <AppKit/NSControl.h>
058d7af6
DE
22#import <AppKit/NSCell.h>
23#import <Foundation/NSException.h>
24
25#include <math.h>
fb896a32 26
b6567e32
DE
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
fb896a32
DE
43IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
44BEGIN_EVENT_TABLE(wxControl, wxControlBase)
45END_EVENT_TABLE()
46WX_IMPLEMENT_COCOA_OWNER(wxControl,NSControl,NSView,NSView)
47
48bool wxControl::Create(wxWindow *parent, wxWindowID winid,
49 const wxPoint& pos, const wxSize& size, long style,
50 const wxValidator& validator, const wxString& name)
51{
48580976 52 wxLogTrace(wxTRACE_COCOA,wxT("Creating control with id=%d"),winid);
fb896a32
DE
53 if(!CreateControl(parent,winid,pos,size,style,validator,name))
54 return false;
48580976 55 wxLogTrace(wxTRACE_COCOA,wxT("Created control with id=%d"),GetId());
fb896a32 56 m_cocoaNSView = NULL;
b6567e32 57 SetNSControl([[wxNonControlNSControl alloc] initWithFrame: MakeDefaultNSRect(size)]);
fb896a32
DE
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);
8d656ea9 66 SetInitialFrameRect(pos,size);
fb896a32
DE
67
68 return true;
69}
70
71wxControl::~wxControl()
72{
911e17c6 73 DisassociateNSControl(GetNSControl());
fb896a32
DE
74}
75
76wxSize wxControl::DoGetBestSize() const
77{
71bfb735 78 wxAutoNSAutoreleasePool pool;
058d7af6
DE
79 wxASSERT(GetNSControl());
80 /* We can ask single-celled controls for their cell and get its size */
81 NSCell *cell = nil;
82NS_DURING
83 cell = [GetNSControl() cell];
84NS_HANDLER
85 // TODO: if anything other than method not implemented, re-raise
86NS_ENDHANDLER
87 if(cell)
88 {
89 NSSize cellSize = [cell cellSize];
90 wxSize size((int)ceilf(cellSize.width),(int)ceilf(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 */
fb896a32 96 NSRect storedRect = [m_cocoaNSView frame];
058d7af6
DE
97 bool didFit = false;
98NS_DURING
fb896a32 99 [GetNSControl() sizeToFit];
058d7af6
DE
100 didFit = true;
101NS_HANDLER
102 // TODO: if anything other than method not implemented, re-raise
103NS_ENDHANDLER
104 if(didFit)
105 {
106 NSRect cocoaRect = [m_cocoaNSView frame];
107 wxSize size((int)ceilf(cocoaRect.size.width),(int)ceilf(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();
fb896a32
DE
115}
116
117bool wxControl::ProcessCommand(wxCommandEvent& event)
118{
fb896a32
DE
119 return GetEventHandler()->ProcessEvent(event);
120}
121
c5172ed1
DE
122void wxControl::CocoaSetEnabled(bool enable)
123{
124 [GetNSControl() setEnabled: enable];
125}
126