1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/stattext.mm
3 // Purpose: wxStaticText
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/stattext.h"
21 #include "wx/cocoa/autorelease.h"
22 #include "wx/cocoa/string.h"
23 #include "wx/cocoa/log.h"
25 #import <Foundation/NSString.h>
26 #import <AppKit/NSTextField.h>
29 BEGIN_EVENT_TABLE(wxStaticText, wxControl)
31 WX_IMPLEMENT_COCOA_OWNER(wxStaticText,NSTextField,NSControl,NSView)
33 bool wxStaticText::Create(wxWindow *parent, wxWindowID winid,
34 const wxString& label,
40 wxAutoNSAutoreleasePool pool;
41 if(!CreateControl(parent,winid,pos,size,style,wxDefaultValidator,name))
44 SetNSTextField([[NSTextField alloc] initWithFrame:MakeDefaultNSRect(size)]);
45 [m_cocoaNSView release];
46 [GetNSTextField() setStringValue:wxNSStringWithWxString(GetLabelText(label))];
47 // [GetNSTextField() setBordered: NO];
48 [GetNSTextField() setBezeled: NO];
49 [GetNSTextField() setEditable: NO];
50 [GetNSTextField() setDrawsBackground: NO];
52 NSTextAlignment alignStyle;
53 if (style & wxALIGN_RIGHT)
54 alignStyle = NSRightTextAlignment;
55 else if (style & wxALIGN_CENTRE)
56 alignStyle = NSCenterTextAlignment;
57 else // default to wxALIGN_LEFT because it is 0 and can't be tested
58 alignStyle = NSLeftTextAlignment;
59 [GetNSControl() setAlignment:(NSTextAlignment)alignStyle];
61 [GetNSControl() sizeToFit];
62 // Round-up to next integer size
63 NSRect nsrect = [m_cocoaNSView frame];
64 nsrect.size.width = ceil(nsrect.size.width);
65 [m_cocoaNSView setFrameSize: nsrect.size];
68 m_parent->CocoaAddChild(this);
69 SetInitialFrameRect(pos,size);
74 wxStaticText::~wxStaticText()
76 DisassociateNSTextField(GetNSTextField());
79 void wxStaticText::SetLabel(const wxString& label)
81 [GetNSTextField() setStringValue:wxNSStringWithWxString(GetLabelText(label))];
82 NSRect oldFrameRect = [GetNSTextField() frame];
83 NSView *superview = [GetNSTextField() superview];
85 if(!(GetWindowStyle() & wxST_NO_AUTORESIZE))
87 wxLogTrace(wxTRACE_COCOA_Window_Size, wxT("wxStaticText::SetLabel Old Position: (%d,%d)"), GetPosition().x, GetPosition().y);
88 [GetNSTextField() sizeToFit];
89 NSRect newFrameRect = [GetNSTextField() frame];
90 // Ensure new size is an integer so GetSize returns valid data
91 newFrameRect.size.height = ceil(newFrameRect.size.height);
92 newFrameRect.size.width = ceil(newFrameRect.size.width);
93 if(![superview isFlipped])
95 newFrameRect.origin.y = oldFrameRect.origin.y + oldFrameRect.size.height - newFrameRect.size.height;
97 [GetNSTextField() setFrame:newFrameRect];
98 // New origin (wx coords) should always match old origin
99 wxLogTrace(wxTRACE_COCOA_Window_Size, wxT("wxStaticText::SetLabel New Position: (%d,%d)"), GetPosition().x, GetPosition().y);
100 [superview setNeedsDisplayInRect:newFrameRect];
103 [superview setNeedsDisplayInRect:oldFrameRect];
106 wxString wxStaticText::GetLabel() const
108 wxAutoNSAutoreleasePool pool;
109 return wxStringWithNSString([GetNSTextField() stringValue]);
112 void wxStaticText::Cocoa_didChangeText(void)