first round of debug/release merge: introduce wxDEBUG_LEVEL, for now defined as 1...
[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:     wxWidgets 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 IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl)
30 BEGIN_EVENT_TABLE(wxStaticText, wxControl)
31 END_EVENT_TABLE()
32 WX_IMPLEMENT_COCOA_OWNER(wxStaticText,NSTextField,NSControl,NSView)
33
34 bool 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 {
41     wxAutoNSAutoreleasePool pool;
42     if(!CreateControl(parent,winid,pos,size,style,wxDefaultValidator,name))
43         return false;
44     m_cocoaNSView = NULL;
45     SetNSTextField([[NSTextField alloc] initWithFrame:MakeDefaultNSRect(size)]);
46     [m_cocoaNSView release];
47     [GetNSTextField() setStringValue:wxNSStringWithWxString(GetLabelText(label))];
48 //    [GetNSTextField() setBordered: NO];
49     [GetNSTextField() setBezeled: NO];
50     [GetNSTextField() setEditable: NO];
51     [GetNSTextField() setDrawsBackground: NO];
52
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
62     [GetNSControl() sizeToFit];
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
68     if(m_parent)
69         m_parent->CocoaAddChild(this);
70     SetInitialFrameRect(pos,size);
71
72     return true;
73 }
74
75 wxStaticText::~wxStaticText()
76 {
77     DisassociateNSTextField(GetNSTextField());
78 }
79
80 void wxStaticText::SetLabel(const wxString& label)
81 {
82     [GetNSTextField() setStringValue:wxNSStringWithWxString(GetLabelText(label))];
83     NSRect oldFrameRect = [GetNSTextField() frame];
84     NSView *superview = [GetNSTextField() superview];
85
86     if(!(GetWindowStyle() & wxST_NO_AUTORESIZE))
87     {
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];
102     }
103
104     [superview setNeedsDisplayInRect:oldFrameRect];
105 }
106
107 wxString wxStaticText::GetLabel() const
108 {
109     wxAutoNSAutoreleasePool pool;
110     return wxStringWithNSString([GetNSTextField() stringValue]);
111 }
112
113 void wxStaticText::Cocoa_didChangeText(void)
114 {
115 }