]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/stattext.cpp
Silence warning about truncation since the comment says it ok
[wxWidgets.git] / src / gtk1 / stattext.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: stattext.cpp
3// Purpose:
4// Author: Robert Roebling
a81258be
RR
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
1e6feb95
VZ
12
13#if wxUSE_STATTEXT
14
c801d85f 15#include "wx/stattext.h"
fab591c5 16#include "wx/gtk/private.h"
c801d85f 17
83624f79
RR
18#include "gdk/gdk.h"
19#include "gtk/gtk.h"
20
e1f448ee
VZ
21extern "C"
22void wxgtk_window_size_request_callback(GtkWidget *widget,
23 GtkRequisition *requisition,
24 wxWindow *win);
25
c801d85f
KB
26//-----------------------------------------------------------------------------
27// wxStaticText
28//-----------------------------------------------------------------------------
29
30IMPLEMENT_DYNAMIC_CLASS(wxStaticText,wxControl)
31
b58197f2 32wxStaticText::wxStaticText()
c801d85f 33{
3f659fd6 34}
c801d85f 35
b58197f2
VZ
36wxStaticText::wxStaticText(wxWindow *parent,
37 wxWindowID id,
38 const wxString &label,
39 const wxPoint &pos,
40 const wxSize &size,
41 long style,
42 const wxString &name)
c801d85f
KB
43{
44 Create( parent, id, label, pos, size, style, name );
3f659fd6 45}
c801d85f 46
b58197f2
VZ
47bool wxStaticText::Create(wxWindow *parent,
48 wxWindowID id,
49 const wxString &label,
50 const wxPoint &pos,
51 const wxSize &size,
52 long style,
53 const wxString &name )
c801d85f 54{
a93109d5 55 m_needParent = TRUE;
b58197f2 56
4dcaf11a
RR
57 if (!PreCreation( parent, pos, size ) ||
58 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
59 {
8ccac798 60 wxFAIL_MSG( wxT("wxStaticText creation failed") );
185fa6bf 61 return FALSE;
4dcaf11a 62 }
b58197f2
VZ
63
64 // notice that we call the base class version which will just remove the
65 // '&' characters from the string, but not set the label's text to it
66 // because the label is not yet created and because SetLabel() has a side
67 // effect of changing the control size which might not be desirable
a93109d5 68 wxControl::SetLabel(label);
02c0348e 69 m_widget = gtk_label_new( wxGTK_CONV( m_label ) );
97d7bfb8 70
a93109d5
RR
71 GtkJustification justify;
72 if ( style & wxALIGN_CENTER )
73 justify = GTK_JUSTIFY_CENTER;
74 else if ( style & wxALIGN_RIGHT )
75 justify = GTK_JUSTIFY_RIGHT;
76 else // wxALIGN_LEFT is 0
77 justify = GTK_JUSTIFY_LEFT;
78 gtk_label_set_justify(GTK_LABEL(m_widget), justify);
1a5594b8 79
1a5594b8
VZ
80 // GTK_JUSTIFY_LEFT is 0, RIGHT 1 and CENTER 2
81 static const float labelAlignments[] = { 0.0, 1.0, 0.5 };
82 gtk_misc_set_alignment(GTK_MISC(m_widget), labelAlignments[justify], 0.0);
185fa6bf 83
a5040b80 84 gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE );
33720b2d 85
f03fc89f 86 m_parent->DoAddChild( this );
b58197f2 87
abdeb9e7 88 PostCreation(size);
7e2b55cd 89
e1f448ee
VZ
90 // the bug below only happens with GTK 2
91#ifdef __WXGTK20__
92 if ( justify != GTK_JUSTIFY_LEFT )
93 {
94 // if we let GTK call wxgtk_window_size_request_callback the label
95 // always shrinks to its minimal size for some reason and so no
96 // alignment except the default left doesn't work (in fact it does,
97 // but you don't see it)
98 gtk_signal_disconnect_by_func
99 (
100 GTK_OBJECT(m_widget),
101 GTK_SIGNAL_FUNC(wxgtk_window_size_request_callback),
102 (gpointer) this
103 );
104 }
105#endif // __WXGTK20__
106
a93109d5 107 return TRUE;
3f659fd6 108}
c801d85f 109
ed58dbea 110wxString wxStaticText::GetLabel() const
c801d85f 111{
3b2e67f6
RR
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
b58197f2
VZ
119
120 return wxString(str);
3f659fd6 121}
c801d85f
KB
122
123void wxStaticText::SetLabel( const wxString &label )
124{
8ccac798
VS
125 wxControl::SetLabel(label);
126
127#ifdef __WXGTK20__
128 // Build the colorized version of the label (markup only allowed
129 // under GTK2):
174b10af
RR
130 if (m_foregroundColour.Ok())
131 {
66bd83b4
VS
132 // If the color has been set, create a markup string to pass to
133 // the label setter
134 wxString colorlabel;
8ccac798
VS
135 colorlabel.Printf(_T("<span foreground=\"#%02x%02x%02x\">%s</span>"),
136 m_foregroundColour.Red(), m_foregroundColour.Green(),
66bd83b4
VS
137 m_foregroundColour.Blue(),
138 wxEscapeStringForPangoMarkup(label).c_str());
139 gtk_label_set_markup( GTK_LABEL(m_widget), wxGTK_CONV( colorlabel ) );
174b10af 140 }
66bd83b4 141 else
174b10af 142#endif
1171e2e5 143 gtk_label_set( GTK_LABEL(m_widget), wxGTK_CONV( m_label ) );
c0e6c051
RD
144
145 // adjust the label size to the new label unless disabled
146 if (!HasFlag(wxST_NO_AUTORESIZE))
147 {
9f884528 148 InvalidateBestSize();
c0e6c051 149 SetSize( GetBestSize() );
c0e6c051 150 }
33720b2d
RR
151}
152
c0e6c051
RD
153bool wxStaticText::SetFont( const wxFont &font )
154{
155 bool ret = wxControl::SetFont(font);
156
157 // adjust the label size to the new label unless disabled
158 if (!HasFlag(wxST_NO_AUTORESIZE))
159 {
9f884528 160 InvalidateBestSize();
c0e6c051 161 SetSize( GetBestSize() );
c0e6c051
RD
162 }
163 return ret;
164}
58614078 165
a5040b80
RR
166void wxStaticText::DoSetSize(int x, int y,
167 int width, int height,
168 int sizeFlags )
169{
170 wxControl::DoSetSize( x, y, width, height, sizeFlags );
171}
172
33720b2d
RR
173wxSize wxStaticText::DoGetBestSize() const
174{
175 // Do not return any arbitrary default value...
176 wxASSERT_MSG( m_widget, wxT("wxStaticText::DoGetBestSize called before creation") );
177
a5040b80 178#ifndef __WXGTK20__
088ddc4e
RR
179 // This resets the internal GTK1 size calculation, which
180 // otherwise would be cashed (incorrectly)
a5040b80 181 gtk_label_set_pattern( GTK_LABEL(m_widget), NULL );
a5040b80 182#endif
088ddc4e
RR
183
184 // GetBestSize is supposed to return unwrapped size
185 gtk_label_set_line_wrap( GTK_LABEL(m_widget), FALSE );
a5040b80 186
33720b2d 187 GtkRequisition req;
a5040b80
RR
188 req.width = -1;
189 req.height = -1;
2afa14f2 190 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
33720b2d
RR
191 (m_widget, &req );
192
088ddc4e
RR
193 gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE );
194
a5040b80 195 return wxSize (req.width, req.height);
33720b2d
RR
196}
197
174b10af
RR
198bool wxStaticText::SetForegroundColour(const wxColour& colour)
199{
200 // First, we call the base class member
201 wxControl::SetForegroundColour(colour);
202 // Then, to force the color change, we set the label with the current label
203 SetLabel(GetLabel());
204 return true;
205}
206
9d522606
RD
207// static
208wxVisualAttributes
209wxStaticText::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
210{
211 return GetDefaultAttributesFromGTKWidget(gtk_label_new);
212}
213
1e6feb95 214#endif // wxUSE_STATTEXT