When using a non-subclassed NSControl, use a new wxNonControlNSControl
[wxWidgets.git] / src / cocoa / control.mm
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
9 // Licence:     wxWindows license
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
19 #include "wx/cocoa/autorelease.h"
20
21 #import <AppKit/NSControl.h>
22
23 @interface wxNonControlNSControl : NSControl
24 {
25 }
26
27 - (void)drawRect: (NSRect)rect;
28 @end // wxNonControlNSControl
29
30 @implementation wxNonControlNSControl : NSControl
31 - (void)drawRect: (NSRect)rect
32 {
33     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
34     if( !win || !win->Cocoa_drawRect(rect) )
35         [super drawRect:rect];
36 }
37 @end // wxNonControlNSControl
38
39 IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
40 BEGIN_EVENT_TABLE(wxControl, wxControlBase)
41 END_EVENT_TABLE()
42 WX_IMPLEMENT_COCOA_OWNER(wxControl,NSControl,NSView,NSView)
43
44 bool wxControl::Create(wxWindow *parent, wxWindowID winid,
45             const wxPoint& pos, const wxSize& size, long style,
46             const wxValidator& validator, const wxString& name)
47 {
48     wxLogDebug("Creating control with id=%d",winid);
49     if(!CreateControl(parent,winid,pos,size,style,validator,name))
50         return false;
51     wxLogDebug("Created control with id=%d",GetId());
52     m_cocoaNSView = NULL;
53     SetNSControl([[wxNonControlNSControl alloc] initWithFrame: MakeDefaultNSRect(size)]);
54     // NOTE: YES we want to release this (to match the alloc).
55     // DoAddChild(this) will retain us again since addSubView doesn't.
56     [m_cocoaNSView release];
57
58     [GetNSControl() sizeToFit];
59
60     if(m_parent)
61         m_parent->CocoaAddChild(this);
62     SetInitialFrameRect(pos,size);
63
64     return true;
65 }
66
67 wxControl::~wxControl()
68 {
69     DisassociateNSControl(GetNSControl());
70 }
71
72 wxSize wxControl::DoGetBestSize() const
73 {
74     wxAutoNSAutoreleasePool pool;
75     wxASSERT(m_cocoaNSView);
76     NSRect storedRect = [m_cocoaNSView frame];
77     [GetNSControl() sizeToFit];
78     NSRect cocoaRect = [m_cocoaNSView frame];
79     wxSize size((int)cocoaRect.size.width+10,(int)cocoaRect.size.height);
80     [m_cocoaNSView setFrame: storedRect];
81     wxLogDebug("wxControl=%p::DoGetBestSize()==(%d,%d)",this,size.x,size.y);
82     return size;
83 }
84
85 bool wxControl::ProcessCommand(wxCommandEvent& event)
86 {
87     return GetEventHandler()->ProcessEvent(event);
88 }
89
90 void wxControl::CocoaSetEnabled(bool enable)
91 {
92     [GetNSControl() setEnabled: enable];
93 }
94