]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/control.mm
File/dir dialog styles and other changes (patch 1488371):
[wxWidgets.git] / src / cocoa / control.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/control.h"
15
16 #ifndef WX_PRECOMP
17 #include "wx/log.h"
18 #endif
19
20 #include "wx/cocoa/autorelease.h"
21
22 #import <AppKit/NSControl.h>
23 #import <AppKit/NSCell.h>
24 #import <Foundation/NSException.h>
25
26 #include <math.h>
27
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
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 {
53 wxLogTrace(wxTRACE_COCOA,wxT("Creating control with id=%d"),winid);
54 if(!CreateControl(parent,winid,pos,size,style,validator,name))
55 return false;
56 wxLogTrace(wxTRACE_COCOA,wxT("Created control with id=%d"),GetId());
57 m_cocoaNSView = NULL;
58 SetNSControl([[wxNonControlNSControl alloc] initWithFrame: MakeDefaultNSRect(size)]);
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);
67 SetInitialFrameRect(pos,size);
68
69 return true;
70 }
71
72 wxControl::~wxControl()
73 {
74 DisassociateNSControl(GetNSControl());
75 }
76
77 wxSize wxControl::DoGetBestSize() const
78 {
79 wxAutoNSAutoreleasePool pool;
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];
91 wxSize size((int)ceil(cellSize.width),(int)ceil(cellSize.height));
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 */
97 NSRect storedRect = [m_cocoaNSView frame];
98 bool didFit = false;
99 NS_DURING
100 [GetNSControl() sizeToFit];
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];
108 wxSize size((int)ceil(cocoaRect.size.width),(int)ceil(cocoaRect.size.height));
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();
116 }
117
118 bool wxControl::ProcessCommand(wxCommandEvent& event)
119 {
120 return GetEventHandler()->ProcessEvent(event);
121 }
122
123 void wxControl::CocoaSetEnabled(bool enable)
124 {
125 [GetNSControl() setEnabled: enable];
126 }