1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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"
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 IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl)
30 BEGIN_EVENT_TABLE(wxStaticText, wxControl)
32 WX_IMPLEMENT_COCOA_OWNER(wxStaticText,NSTextField,NSControl,NSView)
34 bool wxStaticText::Create(wxWindow *parent, wxWindowID winid,
35 const wxString& label,
41 wxAutoNSAutoreleasePool pool;
42 if(!CreateControl(parent,winid,pos,size,style,wxDefaultValidator,name))
45 SetNSTextField([[NSTextField alloc] initWithFrame:MakeDefaultNSRect(size)]);
46 [m_cocoaNSView release];
47 [GetNSTextField() setStringValue:wxNSStringWithWxString(wxStripMenuCodes(label, wxStrip_Mnemonics))];
48 // [GetNSTextField() setBordered: NO];
49 [GetNSTextField() setBezeled: NO];
50 [GetNSTextField() setEditable: NO];
51 [GetNSTextField() setDrawsBackground: NO];
53 [GetNSControl() sizeToFit];
54 // Round-up to next integer size
55 NSRect nsrect = [m_cocoaNSView frame];
56 nsrect.size.width = ceil(nsrect.size.width);
57 [m_cocoaNSView setFrameSize: nsrect.size];
60 m_parent->CocoaAddChild(this);
61 SetInitialFrameRect(pos,size);
66 wxStaticText::~wxStaticText()
68 DisassociateNSTextField(GetNSTextField());
71 void wxStaticText::SetLabel(const wxString& label)
73 [GetNSTextField() setStringValue:wxNSStringWithWxString(wxStripMenuCodes(label, wxStrip_Mnemonics))];
74 NSRect oldFrameRect = [GetNSTextField() frame];
75 NSView *superview = [GetNSTextField() superview];
76 wxLogTrace(wxTRACE_COCOA_Window_Size, wxT("wxStaticText::SetLabel Old Position: (%d,%d)"), GetPosition().x, GetPosition().y);
77 [GetNSTextField() sizeToFit];
78 NSRect newFrameRect = [GetNSTextField() frame];
79 // Ensure new size is an integer so GetSize returns valid data
80 newFrameRect.size.height = ceil(newFrameRect.size.height);
81 newFrameRect.size.width = ceil(newFrameRect.size.width);
82 if(![superview isFlipped])
84 newFrameRect.origin.y = oldFrameRect.origin.y + oldFrameRect.size.height - newFrameRect.size.height;
86 [GetNSTextField() setFrame:newFrameRect];
87 // New origin (wx coords) should always match old origin
88 wxLogTrace(wxTRACE_COCOA_Window_Size, wxT("wxStaticText::SetLabel New Position: (%d,%d)"), GetPosition().x, GetPosition().y);
90 [[GetNSTextField() superview] setNeedsDisplayInRect:oldFrameRect];
91 [[GetNSTextField() superview] setNeedsDisplayInRect:newFrameRect];
94 wxString wxStaticText::GetLabel() const
96 wxAutoNSAutoreleasePool pool;
97 return wxStringWithNSString([GetNSTextField() stringValue]);
100 void wxStaticText::Cocoa_didChangeText(void)