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