merging back XTI branch part 2
[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 // RCS-ID:      $Id$
8 // Copyright:   (c) 2003 David Elliott
9 // Licence:     wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/stattext.h"
15
16 #ifndef WX_PRECOMP
17     #include "wx/app.h"
18     #include "wx/log.h"
19 #endif //WX_PRECOMP
20
21 #include "wx/cocoa/autorelease.h"
22 #include "wx/cocoa/string.h"
23 #include "wx/cocoa/log.h"
24
25 #import <Foundation/NSString.h>
26 #import <AppKit/NSTextField.h>
27 #include <math.h>
28
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 {
40     wxAutoNSAutoreleasePool pool;
41     if(!CreateControl(parent,winid,pos,size,style,wxDefaultValidator,name))
42         return false;
43     m_cocoaNSView = NULL;
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];
51
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
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];
66
67     if(m_parent)
68         m_parent->CocoaAddChild(this);
69     SetInitialFrameRect(pos,size);
70
71     return true;
72 }
73
74 wxStaticText::~wxStaticText()
75 {
76     DisassociateNSTextField(GetNSTextField());
77 }
78
79 void wxStaticText::SetLabel(const wxString& label)
80 {
81     [GetNSTextField() setStringValue:wxNSStringWithWxString(GetLabelText(label))];
82     NSRect oldFrameRect = [GetNSTextField() frame];
83     NSView *superview = [GetNSTextField() superview];
84
85     if(!(GetWindowStyle() & wxST_NO_AUTORESIZE))
86     {
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];
101     }
102
103     [superview setNeedsDisplayInRect:oldFrameRect];
104 }
105
106 wxString wxStaticText::GetLabel() const
107 {
108     wxAutoNSAutoreleasePool pool;
109     return wxStringWithNSString([GetNSTextField() stringValue]);
110 }
111
112 void wxStaticText::Cocoa_didChangeText(void)
113 {
114 }