1 /////////////////////////////////////////////////////////////////////////////
 
   2 // Name:        src/cocoa/stattext.mm
 
   3 // Purpose:     wxStaticText
 
   4 // Author:      David Elliott
 
   8 // Copyright:   (c) 2003 David Elliott
 
   9 // Licence:     wxWidgets licence
 
  10 /////////////////////////////////////////////////////////////////////////////
 
  12 #include "wx/wxprec.h"
 
  14 #include "wx/stattext.h"
 
  21 #include "wx/cocoa/autorelease.h"
 
  22 #include "wx/cocoa/string.h"
 
  23 #include "wx/cocoa/log.h"
 
  25 #import <Foundation/NSString.h>
 
  26 #import <AppKit/NSTextField.h>
 
  29 IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl)
 
  30 BEGIN_EVENT_TABLE(wxStaticText, wxControl)
 
  32 WX_IMPLEMENT_COCOA_OWNER(wxStaticText,NSTextField,NSControl,NSView)
 
  34 bool wxStaticText::Create(wxWindow *parent, wxWindowID winid,
 
  35            const wxString& label,
 
  41     wxAutoNSAutoreleasePool pool;
 
  42     if(!CreateControl(parent,winid,pos,size,style,wxDefaultValidator,name))
 
  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];
 
  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];
 
  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];
 
  69         m_parent->CocoaAddChild(this);
 
  70     SetInitialFrameRect(pos,size);
 
  75 wxStaticText::~wxStaticText()
 
  77     DisassociateNSTextField(GetNSTextField());
 
  80 void wxStaticText::SetLabel(const wxString& label)
 
  82     [GetNSTextField() setStringValue:wxNSStringWithWxString(GetLabelText(label))];
 
  83     NSRect oldFrameRect = [GetNSTextField() frame];
 
  84     NSView *superview = [GetNSTextField() superview];
 
  86     if(!(GetWindowStyle() & wxST_NO_AUTORESIZE))
 
  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])
 
  96             newFrameRect.origin.y = oldFrameRect.origin.y + oldFrameRect.size.height - newFrameRect.size.height;
 
  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];
 
 104     [superview setNeedsDisplayInRect:oldFrameRect];
 
 107 wxString wxStaticText::GetLabel() const
 
 109     wxAutoNSAutoreleasePool pool;
 
 110     return wxStringWithNSString([GetNSTextField() stringValue]);
 
 113 void wxStaticText::Cocoa_didChangeText(void)