]>
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 | |
fb896a32 | 14 | #ifndef WX_PRECOMP |
fb896a32 DE |
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 |
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 | { | |
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 | ||
71 | wxControl::~wxControl() | |
72 | { | |
911e17c6 | 73 | DisassociateNSControl(GetNSControl()); |
fb896a32 DE |
74 | } |
75 | ||
76 | wxSize 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; | |
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]; | |
c05c7cb5 | 90 | wxSize size((int)ceil(cellSize.width),(int)ceil(cellSize.height)); |
058d7af6 DE |
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; |
98 | NS_DURING | |
fb896a32 | 99 | [GetNSControl() sizeToFit]; |
058d7af6 DE |
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]; | |
c05c7cb5 | 107 | wxSize size((int)ceil(cocoaRect.size.width),(int)ceil(cocoaRect.size.height)); |
058d7af6 DE |
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 | ||
117 | bool wxControl::ProcessCommand(wxCommandEvent& event) | |
118 | { | |
fb896a32 DE |
119 | return GetEventHandler()->ProcessEvent(event); |
120 | } | |
121 | ||
c5172ed1 DE |
122 | void wxControl::CocoaSetEnabled(bool enable) |
123 | { | |
124 | [GetNSControl() setEnabled: enable]; | |
125 | } |