]>
Commit | Line | Data |
---|---|---|
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 |
526954c5 | 9 | // Licence: wxWindows 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 | 28 | |
fb896a32 DE |
29 | BEGIN_EVENT_TABLE(wxStaticText, wxControl) |
30 | END_EVENT_TABLE() | |
31 | WX_IMPLEMENT_COCOA_OWNER(wxStaticText,NSTextField,NSControl,NSView) | |
32 | ||
33 | bool wxStaticText::Create(wxWindow *parent, wxWindowID winid, | |
34 | const wxString& label, | |
35 | const wxPoint& pos, | |
36 | const wxSize& size, | |
37 | long style, | |
38 | const wxString& name) | |
39 | { | |
7fc77f30 | 40 | wxAutoNSAutoreleasePool pool; |
fb896a32 DE |
41 | if(!CreateControl(parent,winid,pos,size,style,wxDefaultValidator,name)) |
42 | return false; | |
43 | m_cocoaNSView = NULL; | |
8d656ea9 | 44 | SetNSTextField([[NSTextField alloc] initWithFrame:MakeDefaultNSRect(size)]); |
fb896a32 | 45 | [m_cocoaNSView release]; |
5b70207e | 46 | [GetNSTextField() setStringValue:wxNSStringWithWxString(GetLabelText(label))]; |
fb896a32 DE |
47 | // [GetNSTextField() setBordered: NO]; |
48 | [GetNSTextField() setBezeled: NO]; | |
49 | [GetNSTextField() setEditable: NO]; | |
50 | [GetNSTextField() setDrawsBackground: NO]; | |
1c54c792 | 51 | |
c59112bb DE |
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]; | |
60 | ||
fb896a32 | 61 | [GetNSControl() sizeToFit]; |
1c54c792 DE |
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]; | |
66 | ||
fb896a32 DE |
67 | if(m_parent) |
68 | m_parent->CocoaAddChild(this); | |
8d656ea9 DE |
69 | SetInitialFrameRect(pos,size); |
70 | ||
fb896a32 DE |
71 | return true; |
72 | } | |
73 | ||
74 | wxStaticText::~wxStaticText() | |
75 | { | |
911e17c6 | 76 | DisassociateNSTextField(GetNSTextField()); |
fb896a32 DE |
77 | } |
78 | ||
79 | void wxStaticText::SetLabel(const wxString& label) | |
80 | { | |
5b70207e | 81 | [GetNSTextField() setStringValue:wxNSStringWithWxString(GetLabelText(label))]; |
b9d29922 DE |
82 | NSRect oldFrameRect = [GetNSTextField() frame]; |
83 | NSView *superview = [GetNSTextField() superview]; | |
e2b497f7 DE |
84 | |
85 | if(!(GetWindowStyle() & wxST_NO_AUTORESIZE)) | |
b9d29922 | 86 | { |
e2b497f7 DE |
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]) | |
94 | { | |
95 | newFrameRect.origin.y = oldFrameRect.origin.y + oldFrameRect.size.height - newFrameRect.size.height; | |
96 | } | |
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]; | |
b9d29922 | 101 | } |
b9d29922 | 102 | |
e2b497f7 | 103 | [superview setNeedsDisplayInRect:oldFrameRect]; |
fb896a32 DE |
104 | } |
105 | ||
d174f457 VZ |
106 | wxString wxStaticText::GetLabel() const |
107 | { | |
108 | wxAutoNSAutoreleasePool pool; | |
109 | return wxStringWithNSString([GetNSTextField() stringValue]); | |
110 | } | |
111 | ||
fb896a32 DE |
112 | void wxStaticText::Cocoa_didChangeText(void) |
113 | { | |
114 | } |