Added wxAutoNSAutoreleasePool to DoGetBestSize method
[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 IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
24 BEGIN_EVENT_TABLE(wxControl, wxControlBase)
25 END_EVENT_TABLE()
26 WX_IMPLEMENT_COCOA_OWNER(wxControl,NSControl,NSView,NSView)
27
28 bool wxControl::Create(wxWindow *parent, wxWindowID winid,
29             const wxPoint& pos, const wxSize& size, long style,
30             const wxValidator& validator, const wxString& name)
31 {
32     wxLogDebug("Creating control with id=%d",winid);
33     if(!CreateControl(parent,winid,pos,size,style,validator,name))
34         return false;
35     wxLogDebug("Created control with id=%d",GetId());
36     NSRect cocoaRect = NSMakeRect(10,10,20,20);
37     m_cocoaNSView = NULL;
38     SetNSControl([[NSControl alloc] initWithFrame: cocoaRect]);
39     // NOTE: YES we want to release this (to match the alloc).
40     // DoAddChild(this) will retain us again since addSubView doesn't.
41     [m_cocoaNSView release];
42
43     [GetNSControl() sizeToFit];
44
45     if(m_parent)
46         m_parent->CocoaAddChild(this);
47
48     return true;
49 }
50
51 wxControl::~wxControl()
52 {
53     DisassociateNSControl(m_cocoaNSView);
54 }
55
56 wxSize wxControl::DoGetBestSize() const
57 {
58     wxAutoNSAutoreleasePool pool;
59     wxASSERT(m_cocoaNSView);
60     NSRect storedRect = [m_cocoaNSView frame];
61     [GetNSControl() sizeToFit];
62     NSRect cocoaRect = [m_cocoaNSView frame];
63     wxSize size((int)cocoaRect.size.width+10,(int)cocoaRect.size.height);
64     [m_cocoaNSView setFrame: storedRect];
65     wxLogDebug("wxControl=%p::DoGetBestSize()==(%d,%d)",this,size.x,size.y);
66     return size;
67 }
68
69 bool wxControl::ProcessCommand(wxCommandEvent& event)
70 {
71 #if WXWIN_COMPATIBILITY
72     if ( m_callback )
73     {
74         (void)(*m_callback)(*this, event);
75
76         return TRUE;
77     }
78     else
79 #endif // WXWIN_COMPATIBILITY
80
81     return GetEventHandler()->ProcessEvent(event);
82 }
83