1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/stattext.mm
3 // Purpose: wxStaticText
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
15 #include "wx/stattext.h"
19 #include "wx/cocoa/autorelease.h"
20 #include "wx/cocoa/string.h"
21 #include "wx/cocoa/log.h"
23 #import <Foundation/NSString.h>
24 #import <AppKit/NSTextField.h>
27 IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl)
28 BEGIN_EVENT_TABLE(wxStaticText, wxControl)
30 WX_IMPLEMENT_COCOA_OWNER(wxStaticText,NSTextField,NSControl,NSView)
32 bool wxStaticText::Create(wxWindow *parent, wxWindowID winid,
33 const wxString& label,
39 wxAutoNSAutoreleasePool pool;
40 if(!CreateControl(parent,winid,pos,size,style,wxDefaultValidator,name))
43 SetNSTextField([[NSTextField alloc] initWithFrame:MakeDefaultNSRect(size)]);
44 [m_cocoaNSView release];
45 [GetNSTextField() setStringValue:wxNSStringWithWxString(label)];
46 // [GetNSTextField() setBordered: NO];
47 [GetNSTextField() setBezeled: NO];
48 [GetNSTextField() setEditable: NO];
49 [GetNSTextField() setDrawsBackground: NO];
51 [GetNSControl() sizeToFit];
52 // Round-up to next integer size
53 NSRect nsrect = [m_cocoaNSView frame];
54 nsrect.size.width = ceil(nsrect.size.width);
55 [m_cocoaNSView setFrameSize: nsrect.size];
58 m_parent->CocoaAddChild(this);
59 SetInitialFrameRect(pos,size);
64 wxStaticText::~wxStaticText()
66 DisassociateNSTextField(GetNSTextField());
69 void wxStaticText::SetLabel(const wxString& label)
71 [GetNSTextField() setStringValue:wxNSStringWithWxString(label)];
72 NSRect oldFrameRect = [GetNSTextField() frame];
73 NSView *superview = [GetNSTextField() superview];
74 wxLogTrace(wxTRACE_COCOA_Window_Size, wxT("wxStaticText::SetLabel Old Position: (%d,%d)"), GetPosition().x, GetPosition().y);
75 [GetNSTextField() sizeToFit];
76 NSRect newFrameRect = [GetNSTextField() frame];
77 // Ensure new size is an integer so GetSize returns valid data
78 newFrameRect.size.height = ceil(newFrameRect.size.height);
79 newFrameRect.size.width = ceil(newFrameRect.size.width);
80 if(![superview isFlipped])
82 newFrameRect.origin.y = oldFrameRect.origin.y + oldFrameRect.size.height - newFrameRect.size.height;
84 [GetNSTextField() setFrame:newFrameRect];
85 // New origin (wx coords) should always match old origin
86 wxLogTrace(wxTRACE_COCOA_Window_Size, wxT("wxStaticText::SetLabel New Position: (%d,%d)"), GetPosition().x, GetPosition().y);
88 [[GetNSTextField() superview] setNeedsDisplayInRect:oldFrameRect];
89 [[GetNSTextField() superview] setNeedsDisplayInRect:newFrameRect];
92 void wxStaticText::Cocoa_didChangeText(void)