Strip mnemonics from the label before passing it to Cocoa.
[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(wxStripMenuCodes(label, wxStrip_Mnemonics))];
48 //    [GetNSTextField() setBordered: NO];
49     [GetNSTextField() setBezeled: NO];
50     [GetNSTextField() setEditable: NO];
51     [GetNSTextField() setDrawsBackground: NO];
52
53     [GetNSControl() sizeToFit];
54     // Round-up to next integer size
55     NSRect nsrect = [m_cocoaNSView frame];
56     nsrect.size.width = ceil(nsrect.size.width);
57     [m_cocoaNSView setFrameSize: nsrect.size];
58
59     if(m_parent)
60         m_parent->CocoaAddChild(this);
61     SetInitialFrameRect(pos,size);
62
63     return true;
64 }
65
66 wxStaticText::~wxStaticText()
67 {
68     DisassociateNSTextField(GetNSTextField());
69 }
70
71 void wxStaticText::SetLabel(const wxString& label)
72 {
73     [GetNSTextField() setStringValue:wxNSStringWithWxString(wxStripMenuCodes(label, wxStrip_Mnemonics))];
74     NSRect oldFrameRect = [GetNSTextField() frame];
75     NSView *superview = [GetNSTextField() superview];
76     wxLogTrace(wxTRACE_COCOA_Window_Size, wxT("wxStaticText::SetLabel Old Position: (%d,%d)"), GetPosition().x, GetPosition().y);
77     [GetNSTextField() sizeToFit];
78     NSRect newFrameRect = [GetNSTextField() frame];
79     // Ensure new size is an integer so GetSize returns valid data
80     newFrameRect.size.height = ceil(newFrameRect.size.height);
81     newFrameRect.size.width = ceil(newFrameRect.size.width);
82     if(![superview isFlipped])
83     {
84         newFrameRect.origin.y = oldFrameRect.origin.y + oldFrameRect.size.height - newFrameRect.size.height;
85     }
86     [GetNSTextField() setFrame:newFrameRect];
87     // New origin (wx coords) should always match old origin
88     wxLogTrace(wxTRACE_COCOA_Window_Size, wxT("wxStaticText::SetLabel New Position: (%d,%d)"), GetPosition().x, GetPosition().y);
89
90     [[GetNSTextField() superview] setNeedsDisplayInRect:oldFrameRect];
91     [[GetNSTextField() superview] setNeedsDisplayInRect:newFrameRect];
92 }
93
94 wxString wxStaticText::GetLabel() const
95 {
96     wxAutoNSAutoreleasePool pool;
97     return wxStringWithNSString([GetNSTextField() stringValue]);
98 }
99
100 void wxStaticText::Cocoa_didChangeText(void)
101 {
102 }