]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/stattext.mm
Minor corrections to XRC format description.
[wxWidgets.git] / src / cocoa / stattext.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/stattext.mm
3 // Purpose: wxStaticText
4 // Author: David Elliott
5 // Modified by:
6 // Created: 2003/02/15
7 // Copyright: (c) 2003 David Elliott
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/wxprec.h"
12
13 #include "wx/stattext.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/app.h"
17 #include "wx/log.h"
18 #endif //WX_PRECOMP
19
20 #include "wx/cocoa/autorelease.h"
21 #include "wx/cocoa/string.h"
22 #include "wx/cocoa/log.h"
23
24 #import <Foundation/NSString.h>
25 #import <AppKit/NSTextField.h>
26 #include <math.h>
27
28 BEGIN_EVENT_TABLE(wxStaticText, wxControl)
29 END_EVENT_TABLE()
30 WX_IMPLEMENT_COCOA_OWNER(wxStaticText,NSTextField,NSControl,NSView)
31
32 bool wxStaticText::Create(wxWindow *parent, wxWindowID winid,
33 const wxString& label,
34 const wxPoint& pos,
35 const wxSize& size,
36 long style,
37 const wxString& name)
38 {
39 wxAutoNSAutoreleasePool pool;
40 if(!CreateControl(parent,winid,pos,size,style,wxDefaultValidator,name))
41 return false;
42 m_cocoaNSView = NULL;
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];
50
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];
59
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];
65
66 if(m_parent)
67 m_parent->CocoaAddChild(this);
68 SetInitialFrameRect(pos,size);
69
70 return true;
71 }
72
73 wxStaticText::~wxStaticText()
74 {
75 DisassociateNSTextField(GetNSTextField());
76 }
77
78 void wxStaticText::SetLabel(const wxString& label)
79 {
80 [GetNSTextField() setStringValue:wxNSStringWithWxString(GetLabelText(label))];
81 NSRect oldFrameRect = [GetNSTextField() frame];
82 NSView *superview = [GetNSTextField() superview];
83
84 if(!(GetWindowStyle() & wxST_NO_AUTORESIZE))
85 {
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])
93 {
94 newFrameRect.origin.y = oldFrameRect.origin.y + oldFrameRect.size.height - newFrameRect.size.height;
95 }
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];
100 }
101
102 [superview setNeedsDisplayInRect:oldFrameRect];
103 }
104
105 wxString wxStaticText::GetLabel() const
106 {
107 wxAutoNSAutoreleasePool pool;
108 return wxStringWithNSString([GetNSTextField() stringValue]);
109 }
110
111 void wxStaticText::Cocoa_didChangeText(void)
112 {
113 }