Implemented SetLabel
[wxWidgets.git] / src / cocoa / stattext.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        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 #ifndef WX_PRECOMP
14     #include "wx/app.h"
15     #include "wx/stattext.h"
16     #include "wx/log.h"
17 #endif //WX_PRECOMP
18
19 #include "wx/cocoa/autorelease.h"
20 #include "wx/cocoa/string.h"
21 #include "wx/cocoa/log.h"
22
23 #import <Foundation/NSString.h>
24 #import <AppKit/NSTextField.h>
25 #include <math.h>
26
27 IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl)
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(label)];
46 //    [GetNSTextField() setBordered: NO];
47     [GetNSTextField() setBezeled: NO];
48     [GetNSTextField() setEditable: NO];
49     [GetNSTextField() setDrawsBackground: NO];
50
51     [GetNSControl() sizeToFit];
52     // Round-up to next integer size
53     NSRect nsrect = [m_cocoaNSView frame];
54     nsrect.size.width = ceil(nsrect.size.width);
55     [m_cocoaNSView setFrameSize: nsrect.size];
56
57     if(m_parent)
58         m_parent->CocoaAddChild(this);
59     SetInitialFrameRect(pos,size);
60
61     return true;
62 }
63
64 wxStaticText::~wxStaticText()
65 {
66     DisassociateNSTextField(GetNSTextField());
67 }
68
69 void wxStaticText::SetLabel(const wxString& label)
70 {
71     [GetNSTextField() setStringValue:wxNSStringWithWxString(label)];
72     NSRect oldFrameRect = [GetNSTextField() frame];
73     NSView *superview = [GetNSTextField() superview];
74     wxLogTrace(wxTRACE_COCOA_Window_Size, "wxStaticText::SetLabel Old Position: (%d,%d)", GetPosition().x, GetPosition().y);
75     [GetNSTextField() sizeToFit];
76     NSRect newFrameRect = [GetNSTextField() frame];
77     if(![superview isFlipped])
78     {
79         newFrameRect.origin.y = oldFrameRect.origin.y + oldFrameRect.size.height - newFrameRect.size.height;
80         [GetNSTextField() setFrame:newFrameRect];
81     }
82     wxLogTrace(wxTRACE_COCOA_Window_Size, "wxStaticText::SetLabel New Position: (%d,%d)", GetPosition().x, GetPosition().y);
83
84     [[GetNSTextField() superview] setNeedsDisplayInRect:oldFrameRect];
85     [[GetNSTextField() superview] setNeedsDisplayInRect:newFrameRect];
86 }
87
88 void wxStaticText::Cocoa_didChangeText(void)
89 {
90 }
91