]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/stattext.cpp
fix PangoFontMetrics leak in GetCharHeight() (bug 1691180)
[wxWidgets.git] / src / gtk / 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 63
d44a9337
VZ
64 const wxString labelGTK = GTKConvertMnemonics(label);
65 m_label = label;
66 m_widget = gtk_label_new_with_mnemonic(wxGTK_CONV(labelGTK));
3d257b8d 67
a93109d5
RR
68 GtkJustification justify;
69 if ( style & wxALIGN_CENTER )
70 justify = GTK_JUSTIFY_CENTER;
71 else if ( style & wxALIGN_RIGHT )
72 justify = GTK_JUSTIFY_RIGHT;
73 else // wxALIGN_LEFT is 0
74 justify = GTK_JUSTIFY_LEFT;
f4322df6 75
69597639 76 if (GetLayoutDirection() == wxLayout_RightToLeft)
f4322df6 77 {
69597639
RR
78 if (justify == GTK_JUSTIFY_RIGHT)
79 justify = GTK_JUSTIFY_LEFT;
80 if (justify == GTK_JUSTIFY_LEFT)
81 justify = GTK_JUSTIFY_RIGHT;
82 }
f4322df6 83
a93109d5 84 gtk_label_set_justify(GTK_LABEL(m_widget), justify);
1a5594b8 85
1a5594b8
VZ
86 // GTK_JUSTIFY_LEFT is 0, RIGHT 1 and CENTER 2
87 static const float labelAlignments[] = { 0.0, 1.0, 0.5 };
88 gtk_misc_set_alignment(GTK_MISC(m_widget), labelAlignments[justify], 0.0);
185fa6bf 89
d44a9337 90 gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE );
33720b2d 91
f03fc89f 92 m_parent->DoAddChild( this );
b58197f2 93
abdeb9e7 94 PostCreation(size);
3d257b8d 95
e1f448ee 96 // the bug below only happens with GTK 2
e1f448ee
VZ
97 if ( justify != GTK_JUSTIFY_LEFT )
98 {
99 // if we let GTK call wxgtk_window_size_request_callback the label
100 // always shrinks to its minimal size for some reason and so no
101 // alignment except the default left doesn't work (in fact it does,
102 // but you don't see it)
9fa72bd2
MR
103 g_signal_handlers_disconnect_by_func (m_widget,
104 (gpointer) wxgtk_window_size_request_callback,
105 this);
e1f448ee 106 }
e1f448ee 107
a93109d5 108 return TRUE;
3f659fd6 109}
c801d85f 110
ed58dbea 111wxString wxStaticText::GetLabel() const
c801d85f 112{
3b2e67f6 113 GtkLabel *label = GTK_LABEL(m_widget);
3b2e67f6 114 wxString str = wxGTK_CONV_BACK( gtk_label_get_text( label ) );
b58197f2
VZ
115
116 return wxString(str);
3f659fd6 117}
c801d85f
KB
118
119void wxStaticText::SetLabel( const wxString &label )
120{
d44a9337 121 wxCHECK_RET( m_widget != NULL, wxT("invalid static text") );
8ccac798 122
d44a9337 123 GTKSetLabelForLabel(GTK_LABEL(m_widget), label);
c0e6c051
RD
124
125 // adjust the label size to the new label unless disabled
d44a9337 126 if ( !HasFlag(wxST_NO_AUTORESIZE) )
c0e6c051 127 SetSize( GetBestSize() );
33720b2d
RR
128}
129
c0e6c051
RD
130bool wxStaticText::SetFont( const wxFont &font )
131{
ac6e0eb1
VZ
132 const bool wasUnderlined = GetFont().GetUnderlined();
133
c0e6c051
RD
134 bool ret = wxControl::SetFont(font);
135
ac6e0eb1
VZ
136 if ( font.GetUnderlined() != wasUnderlined )
137 {
138 // the underlines for mnemonics are incompatible with using attributes
139 // so turn them off when setting underlined font and restore them when
140 // unsetting it
141 gtk_label_set_use_underline(GTK_LABEL(m_widget), wasUnderlined);
142
143 if ( wasUnderlined )
144 {
145 // it's not underlined any more, remove the attributes we set
146 gtk_label_set_attributes(GTK_LABEL(m_widget), NULL);
147 }
148 else // the text is underlined now
149 {
150 PangoAttrList *attrs = pango_attr_list_new();
151 PangoAttribute *a = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
152 a->start_index = 0;
97a85898 153 a->end_index = (guint)-1;
ac6e0eb1
VZ
154 pango_attr_list_insert(attrs, a);
155 gtk_label_set_attributes(GTK_LABEL(m_widget), attrs);
156 pango_attr_list_unref(attrs);
157 }
158 }
159
c0e6c051
RD
160 // adjust the label size to the new label unless disabled
161 if (!HasFlag(wxST_NO_AUTORESIZE))
162 {
9f884528 163 InvalidateBestSize();
c0e6c051 164 SetSize( GetBestSize() );
c0e6c051
RD
165 }
166 return ret;
167}
58614078 168
a5040b80
RR
169void wxStaticText::DoSetSize(int x, int y,
170 int width, int height,
171 int sizeFlags )
172{
173 wxControl::DoSetSize( x, y, width, height, sizeFlags );
174}
175
33720b2d
RR
176wxSize wxStaticText::DoGetBestSize() const
177{
178 // Do not return any arbitrary default value...
179 wxASSERT_MSG( m_widget, wxT("wxStaticText::DoGetBestSize called before creation") );
180
088ddc4e
RR
181 // GetBestSize is supposed to return unwrapped size
182 gtk_label_set_line_wrap( GTK_LABEL(m_widget), FALSE );
3d257b8d 183
33720b2d 184 GtkRequisition req;
a5040b80
RR
185 req.width = -1;
186 req.height = -1;
2afa14f2 187 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
33720b2d
RR
188 (m_widget, &req );
189
088ddc4e 190 gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE );
3d257b8d 191
8d1c5c3f
RD
192 // Adding 1 to width to workaround GTK sometimes wrapping the text needlessly
193 return wxSize (req.width+1, req.height);
33720b2d
RR
194}
195
174b10af
RR
196bool wxStaticText::SetForegroundColour(const wxColour& colour)
197{
198 // First, we call the base class member
199 wxControl::SetForegroundColour(colour);
200 // Then, to force the color change, we set the label with the current label
201 SetLabel(GetLabel());
202 return true;
203}
204
2e1f5012
VZ
205bool wxStaticText::GTKWidgetNeedsMnemonic() const
206{
207 return true;
208}
209
210void wxStaticText::GTKWidgetDoSetMnemonic(GtkWidget* w)
211{
212 gtk_label_set_mnemonic_widget(GTK_LABEL(m_widget), w);
213}
214
9d522606
RD
215// static
216wxVisualAttributes
217wxStaticText::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
218{
219 return GetDefaultAttributesFromGTKWidget(gtk_label_new);
220}
221
1e6feb95 222#endif // wxUSE_STATTEXT