1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/stattext.mm
3 // Purpose: wxStaticText
4 // Author: David Elliott
7 // Copyright: (c) 2003 David Elliott
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
13 #include "wx/stattext.h"
20 #include "wx/cocoa/autorelease.h"
21 #include "wx/cocoa/string.h"
22 #include "wx/cocoa/log.h"
24 #import <Foundation/NSString.h>
25 #import <AppKit/NSTextField.h>
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(GetLabelText(label))];
46 // [GetNSTextField() setBordered: NO];
47 [GetNSTextField() setBezeled: NO];
48 [GetNSTextField() setEditable: NO];
49 [GetNSTextField() setDrawsBackground: NO];
51 NSTextAlignment alignStyle;
52 if (style & wxALIGN_RIGHT)
53 alignStyle = NSRightTextAlignment;
54 else if (style & wxALIGN_CENTRE)
55 alignStyle = NSCenterTextAlignment;
56 else // default to wxALIGN_LEFT because it is 0 and can't be tested
57 alignStyle = NSLeftTextAlignment;
58 [GetNSControl() setAlignment:(NSTextAlignment)alignStyle];
60 [GetNSControl() sizeToFit];
61 // Round-up to next integer size
62 NSRect nsrect = [m_cocoaNSView frame];
63 nsrect.size.width = ceil(nsrect.size.width);
64 [m_cocoaNSView setFrameSize: nsrect.size];
67 m_parent->CocoaAddChild(this);
68 SetInitialFrameRect(pos,size);
73 wxStaticText::~wxStaticText()
75 DisassociateNSTextField(GetNSTextField());
78 void wxStaticText::SetLabel(const wxString& label)
80 [GetNSTextField() setStringValue:wxNSStringWithWxString(GetLabelText(label))];
81 NSRect oldFrameRect = [GetNSTextField() frame];
82 NSView *superview = [GetNSTextField() superview];
84 if(!(GetWindowStyle() & wxST_NO_AUTORESIZE))
86 wxLogTrace(wxTRACE_COCOA_Window_Size, wxT("wxStaticText::SetLabel Old Position: (%d,%d)"), GetPosition().x, GetPosition().y);
87 [GetNSTextField() sizeToFit];
88 NSRect newFrameRect = [GetNSTextField() frame];
89 // Ensure new size is an integer so GetSize returns valid data
90 newFrameRect.size.height = ceil(newFrameRect.size.height);
91 newFrameRect.size.width = ceil(newFrameRect.size.width);
92 if(![superview isFlipped])
94 newFrameRect.origin.y = oldFrameRect.origin.y + oldFrameRect.size.height - newFrameRect.size.height;
96 [GetNSTextField() setFrame:newFrameRect];
97 // New origin (wx coords) should always match old origin
98 wxLogTrace(wxTRACE_COCOA_Window_Size, wxT("wxStaticText::SetLabel New Position: (%d,%d)"), GetPosition().x, GetPosition().y);
99 [superview setNeedsDisplayInRect:newFrameRect];
102 [superview setNeedsDisplayInRect:oldFrameRect];
105 wxString wxStaticText::GetLabel() const
107 wxAutoNSAutoreleasePool pool;
108 return wxStringWithNSString([GetNSTextField() stringValue]);
111 void wxStaticText::Cocoa_didChangeText(void)