]>
Commit | Line | Data |
---|---|---|
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 | |
fb896a32 | 7 | // Copyright: (c) 2003 David Elliott |
526954c5 | 8 | // Licence: wxWindows licence |
fb896a32 DE |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
11 | #include "wx/wxprec.h" | |
8898456d | 12 | |
93fbbe07 WS |
13 | #include "wx/control.h" |
14 | ||
fb896a32 | 15 | #ifndef WX_PRECOMP |
fb896a32 DE |
16 | #include "wx/log.h" |
17 | #endif | |
18 | ||
71bfb735 | 19 | #include "wx/cocoa/autorelease.h" |
525007cf | 20 | #include "wx/cocoa/string.h" |
7c5a378f | 21 | #include "wx/cocoa/trackingrectmanager.h" |
a24aa427 | 22 | #include "wx/cocoa/objc/objc_uniquifying.h" |
f48408ae | 23 | #include "wx/cocoa/objc/NSView.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 DE |
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 | { | |
48580976 | 40 | wxLogTrace(wxTRACE_COCOA,wxT("Creating control with id=%d"),winid); |
fb896a32 DE |
41 | if(!CreateControl(parent,winid,pos,size,style,validator,name)) |
42 | return false; | |
48580976 | 43 | wxLogTrace(wxTRACE_COCOA,wxT("Created control with id=%d"),GetId()); |
fb896a32 | 44 | m_cocoaNSView = NULL; |
f48408ae | 45 | SetNSControl([[WX_GET_OBJC_CLASS(WXNSView) alloc] initWithFrame: MakeDefaultNSRect(size)]); |
fb896a32 DE |
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 | ||
fb896a32 DE |
50 | if(m_parent) |
51 | m_parent->CocoaAddChild(this); | |
8d656ea9 | 52 | SetInitialFrameRect(pos,size); |
fb896a32 | 53 | |
7c5a378f DE |
54 | // Controls should have a viewable-area tracking rect by default |
55 | m_visibleTrackingRectManager = new wxCocoaTrackingRectManager(this); | |
56 | ||
fb896a32 DE |
57 | return true; |
58 | } | |
59 | ||
60 | wxControl::~wxControl() | |
61 | { | |
911e17c6 | 62 | DisassociateNSControl(GetNSControl()); |
fb896a32 DE |
63 | } |
64 | ||
65 | wxSize wxControl::DoGetBestSize() const | |
66 | { | |
71bfb735 | 67 | wxAutoNSAutoreleasePool pool; |
058d7af6 DE |
68 | wxASSERT(GetNSControl()); |
69 | /* We can ask single-celled controls for their cell and get its size */ | |
70 | NSCell *cell = nil; | |
2465458c DE |
71 | if([GetNSControl() respondsToSelector:@selector(cell)]) |
72 | cell = [GetNSControl() cell]; | |
058d7af6 DE |
73 | if(cell) |
74 | { | |
75 | NSSize cellSize = [cell cellSize]; | |
c05c7cb5 | 76 | wxSize size((int)ceil(cellSize.width),(int)ceil(cellSize.height)); |
058d7af6 DE |
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 */ | |
2465458c | 82 | if([GetNSControl() respondsToSelector:@selector(sizeToFit)]) |
058d7af6 | 83 | { |
2465458c DE |
84 | NSRect storedRect = [m_cocoaNSView frame]; |
85 | [GetNSControl() sizeToFit]; | |
058d7af6 | 86 | NSRect cocoaRect = [m_cocoaNSView frame]; |
c05c7cb5 | 87 | wxSize size((int)ceil(cocoaRect.size.width),(int)ceil(cocoaRect.size.height)); |
058d7af6 DE |
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(); | |
fb896a32 DE |
95 | } |
96 | ||
97 | bool wxControl::ProcessCommand(wxCommandEvent& event) | |
98 | { | |
937013e0 | 99 | return HandleWindowEvent(event); |
fb896a32 DE |
100 | } |
101 | ||
c5172ed1 DE |
102 | void wxControl::CocoaSetEnabled(bool enable) |
103 | { | |
f48408ae DE |
104 | if([GetNSControl() respondsToSelector:@selector(setEnabled:)]) |
105 | [GetNSControl() setEnabled: enable]; | |
c5172ed1 | 106 | } |
525007cf DE |
107 | |
108 | /*static*/ void wxControl::CocoaSetLabelForObject(const wxString& label, struct objc_object *aView) | |
109 | { | |
110 | [aView setTitle:wxNSStringWithWxString(GetLabelText(label))]; | |
111 | } | |
112 |