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