Another UNicode conversion fix.
[wxWidgets.git] / src / gtk1 / stattext.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: stattext.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10
11 #ifdef __GNUG__
12 #pragma implementation "stattext.h"
13 #endif
14
15 #include "wx/defs.h"
16
17 #if wxUSE_STATTEXT
18
19 #include "wx/stattext.h"
20 #include "wx/gtk/private.h"
21
22 #include "gdk/gdk.h"
23 #include "gtk/gtk.h"
24
25 //-----------------------------------------------------------------------------
26 // wxStaticText
27 //-----------------------------------------------------------------------------
28
29 IMPLEMENT_DYNAMIC_CLASS(wxStaticText,wxControl)
30
31 wxStaticText::wxStaticText()
32 {
33 }
34
35 wxStaticText::wxStaticText(wxWindow *parent,
36 wxWindowID id,
37 const wxString &label,
38 const wxPoint &pos,
39 const wxSize &size,
40 long style,
41 const wxString &name)
42 {
43 Create( parent, id, label, pos, size, style, name );
44 }
45
46 bool wxStaticText::Create(wxWindow *parent,
47 wxWindowID id,
48 const wxString &label,
49 const wxPoint &pos,
50 const wxSize &size,
51 long style,
52 const wxString &name )
53 {
54 m_needParent = TRUE;
55
56 if (!PreCreation( parent, pos, size ) ||
57 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
58 {
59 wxFAIL_MSG( wxT("wxXX creation failed") );
60 return FALSE;
61 }
62
63 // notice that we call the base class version which will just remove the
64 // '&' characters from the string, but not set the label's text to it
65 // because the label is not yet created and because SetLabel() has a side
66 // effect of changing the control size which might not be desirable
67 wxControl::SetLabel(label);
68 m_widget = gtk_label_new( wxGTK_CONV( m_label ) );
69
70 GtkJustification justify;
71 if ( style & wxALIGN_CENTER )
72 justify = GTK_JUSTIFY_CENTER;
73 else if ( style & wxALIGN_RIGHT )
74 justify = GTK_JUSTIFY_RIGHT;
75 else // wxALIGN_LEFT is 0
76 justify = GTK_JUSTIFY_LEFT;
77 gtk_label_set_justify(GTK_LABEL(m_widget), justify);
78
79 // GTK_JUSTIFY_LEFT is 0, RIGHT 1 and CENTER 2
80 static const float labelAlignments[] = { 0.0, 1.0, 0.5 };
81 gtk_misc_set_alignment(GTK_MISC(m_widget), labelAlignments[justify], 0.0);
82
83 // do not move this call elsewhere
84 gtk_label_set_line_wrap( GTK_LABEL(m_widget), FALSE );
85
86 m_parent->DoAddChild( this );
87
88 PostCreation();
89
90 ApplyWidgetStyle();
91
92 wxControl::SetFont( parent->GetFont() );
93
94 wxSize size_best( DoGetBestSize() );
95 wxSize new_size( size );
96 if (new_size.x == -1)
97 new_size.x = size_best.x;
98 if (new_size.y == -1)
99 new_size.y = size_best.y;
100 if ((new_size.x != size.x) || (new_size.y != size.y))
101 SetSize( new_size.x, new_size.y );
102
103 SetBackgroundColour( parent->GetBackgroundColour() );
104 SetForegroundColour( parent->GetForegroundColour() );
105 Show( TRUE );
106
107 return TRUE;
108 }
109
110 wxString wxStaticText::GetLabel() const
111 {
112 GtkLabel *label = GTK_LABEL(m_widget);
113
114 #ifdef __WXGTK20__
115 wxString str = wxGTK_CONV_BACK( gtk_label_get_text( label ) );
116 #else
117 wxString str = wxString( label->label );
118 #endif
119
120 return wxString(str);
121 }
122
123 void wxStaticText::SetLabel( const wxString &label )
124 {
125 wxControl::SetLabel(label);
126
127 gtk_label_set( GTK_LABEL(m_widget), wxGTK_CONV( m_label ) );
128
129 // adjust the label size to the new label unless disabled
130 if (!HasFlag(wxST_NO_AUTORESIZE))
131 SetSize( GetBestSize() );
132 }
133
134 bool wxStaticText::SetFont( const wxFont &font )
135 {
136 bool ret = wxControl::SetFont(font);
137
138 // adjust the label size to the new label unless disabled
139 if (!HasFlag(wxST_NO_AUTORESIZE))
140 SetSize( GetBestSize() );
141
142 return ret;
143 }
144
145 void wxStaticText::ApplyWidgetStyle()
146 {
147 SetWidgetStyle();
148 gtk_widget_set_style( m_widget, m_widgetStyle );
149 }
150
151 wxSize wxStaticText::DoGetBestSize() const
152 {
153 // Do not return any arbitrary default value...
154 wxASSERT_MSG( m_widget, wxT("wxStaticText::DoGetBestSize called before creation") );
155
156 // this invalidates the size request
157 gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE );
158 gtk_label_set_line_wrap( GTK_LABEL(m_widget), FALSE );
159
160 GtkRequisition req;
161 req.width = 2;
162 req.height = 2;
163 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
164 (m_widget, &req );
165
166 return wxSize(req.width, req.height);
167 }
168
169 #endif // wxUSE_STATTEXT