]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/stattext.mm
Add wxDataFormat GetMatchingPair() which wxGTK and wxMSW already had under different...
[wxWidgets.git] / src / cocoa / stattext.mm
CommitLineData
fb896a32 1/////////////////////////////////////////////////////////////////////////////
ccdc11bb 2// Name: src/cocoa/stattext.mm
fb896a32
DE
3// Purpose: wxStaticText
4// Author: David Elliott
5// Modified by:
6// Created: 2003/02/15
ccdc11bb 7// RCS-ID: $Id$
fb896a32 8// Copyright: (c) 2003 David Elliott
ccdc11bb 9// Licence: wxWidgets licence
fb896a32
DE
10/////////////////////////////////////////////////////////////////////////////
11
449c5673 12#include "wx/wxprec.h"
ccdc11bb
WS
13
14#include "wx/stattext.h"
15
449c5673
DE
16#ifndef WX_PRECOMP
17 #include "wx/app.h"
b9d29922 18 #include "wx/log.h"
449c5673 19#endif //WX_PRECOMP
fb896a32 20
7fc77f30 21#include "wx/cocoa/autorelease.h"
b0c0a393 22#include "wx/cocoa/string.h"
b9d29922 23#include "wx/cocoa/log.h"
7fc77f30 24
fb896a32
DE
25#import <Foundation/NSString.h>
26#import <AppKit/NSTextField.h>
1c54c792 27#include <math.h>
fb896a32
DE
28
29IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl)
30BEGIN_EVENT_TABLE(wxStaticText, wxControl)
31END_EVENT_TABLE()
32WX_IMPLEMENT_COCOA_OWNER(wxStaticText,NSTextField,NSControl,NSView)
33
34bool wxStaticText::Create(wxWindow *parent, wxWindowID winid,
35 const wxString& label,
36 const wxPoint& pos,
37 const wxSize& size,
38 long style,
39 const wxString& name)
40{
7fc77f30 41 wxAutoNSAutoreleasePool pool;
fb896a32
DE
42 if(!CreateControl(parent,winid,pos,size,style,wxDefaultValidator,name))
43 return false;
44 m_cocoaNSView = NULL;
8d656ea9 45 SetNSTextField([[NSTextField alloc] initWithFrame:MakeDefaultNSRect(size)]);
fb896a32 46 [m_cocoaNSView release];
5b70207e 47 [GetNSTextField() setStringValue:wxNSStringWithWxString(GetLabelText(label))];
fb896a32
DE
48// [GetNSTextField() setBordered: NO];
49 [GetNSTextField() setBezeled: NO];
50 [GetNSTextField() setEditable: NO];
51 [GetNSTextField() setDrawsBackground: NO];
1c54c792 52
c59112bb
DE
53 NSTextAlignment alignStyle;
54 if (style & wxALIGN_RIGHT)
55 alignStyle = NSRightTextAlignment;
56 else if (style & wxALIGN_CENTRE)
57 alignStyle = NSCenterTextAlignment;
58 else // default to wxALIGN_LEFT because it is 0 and can't be tested
59 alignStyle = NSLeftTextAlignment;
60 [GetNSControl() setAlignment:(NSTextAlignment)alignStyle];
61
fb896a32 62 [GetNSControl() sizeToFit];
1c54c792
DE
63 // Round-up to next integer size
64 NSRect nsrect = [m_cocoaNSView frame];
65 nsrect.size.width = ceil(nsrect.size.width);
66 [m_cocoaNSView setFrameSize: nsrect.size];
67
fb896a32
DE
68 if(m_parent)
69 m_parent->CocoaAddChild(this);
8d656ea9
DE
70 SetInitialFrameRect(pos,size);
71
fb896a32
DE
72 return true;
73}
74
75wxStaticText::~wxStaticText()
76{
911e17c6 77 DisassociateNSTextField(GetNSTextField());
fb896a32
DE
78}
79
80void wxStaticText::SetLabel(const wxString& label)
81{
5b70207e 82 [GetNSTextField() setStringValue:wxNSStringWithWxString(GetLabelText(label))];
b9d29922
DE
83 NSRect oldFrameRect = [GetNSTextField() frame];
84 NSView *superview = [GetNSTextField() superview];
e2b497f7
DE
85
86 if(!(GetWindowStyle() & wxST_NO_AUTORESIZE))
b9d29922 87 {
e2b497f7
DE
88 wxLogTrace(wxTRACE_COCOA_Window_Size, wxT("wxStaticText::SetLabel Old Position: (%d,%d)"), GetPosition().x, GetPosition().y);
89 [GetNSTextField() sizeToFit];
90 NSRect newFrameRect = [GetNSTextField() frame];
91 // Ensure new size is an integer so GetSize returns valid data
92 newFrameRect.size.height = ceil(newFrameRect.size.height);
93 newFrameRect.size.width = ceil(newFrameRect.size.width);
94 if(![superview isFlipped])
95 {
96 newFrameRect.origin.y = oldFrameRect.origin.y + oldFrameRect.size.height - newFrameRect.size.height;
97 }
98 [GetNSTextField() setFrame:newFrameRect];
99 // New origin (wx coords) should always match old origin
100 wxLogTrace(wxTRACE_COCOA_Window_Size, wxT("wxStaticText::SetLabel New Position: (%d,%d)"), GetPosition().x, GetPosition().y);
101 [superview setNeedsDisplayInRect:newFrameRect];
b9d29922 102 }
b9d29922 103
e2b497f7 104 [superview setNeedsDisplayInRect:oldFrameRect];
fb896a32
DE
105}
106
d174f457
VZ
107wxString wxStaticText::GetLabel() const
108{
109 wxAutoNSAutoreleasePool pool;
110 return wxStringWithNSString([GetNSTextField() stringValue]);
111}
112
fb896a32
DE
113void wxStaticText::Cocoa_didChangeText(void)
114{
115}