mac paths updated
[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
KB
17
18//-----------------------------------------------------------------------------
19// wxStaticText
20//-----------------------------------------------------------------------------
21
22IMPLEMENT_DYNAMIC_CLASS(wxStaticText,wxControl)
23
b58197f2 24wxStaticText::wxStaticText()
c801d85f 25{
3f659fd6 26}
c801d85f 27
b58197f2
VZ
28wxStaticText::wxStaticText(wxWindow *parent,
29 wxWindowID id,
30 const wxString &label,
31 const wxPoint &pos,
32 const wxSize &size,
33 long style,
34 const wxString &name)
c801d85f 35{
39bc0347 36 Create( parent, id, label, pos, size, style, name );
3f659fd6 37}
c801d85f 38
b58197f2
VZ
39bool wxStaticText::Create(wxWindow *parent,
40 wxWindowID id,
41 const wxString &label,
42 const wxPoint &pos,
43 const wxSize &size,
44 long style,
45 const wxString &name )
c801d85f 46{
4dcaf11a
RR
47 if (!PreCreation( parent, pos, size ) ||
48 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
49 {
8ccac798 50 wxFAIL_MSG( wxT("wxStaticText creation failed") );
e8375af8 51 return false;
4dcaf11a 52 }
b58197f2 53
39bc0347 54 m_widget = gtk_label_new(NULL);
3d257b8d 55
a93109d5
RR
56 GtkJustification justify;
57 if ( style & wxALIGN_CENTER )
58 justify = GTK_JUSTIFY_CENTER;
59 else if ( style & wxALIGN_RIGHT )
60 justify = GTK_JUSTIFY_RIGHT;
7f85af82 61 else
a93109d5 62 justify = GTK_JUSTIFY_LEFT;
f4322df6 63
69597639 64 if (GetLayoutDirection() == wxLayout_RightToLeft)
f4322df6 65 {
a1a48c8a 66 if (justify == GTK_JUSTIFY_RIGHT)
69597639 67 justify = GTK_JUSTIFY_LEFT;
a1a48c8a 68 else if (justify == GTK_JUSTIFY_LEFT)
69597639
RR
69 justify = GTK_JUSTIFY_RIGHT;
70 }
f4322df6 71
a93109d5 72 gtk_label_set_justify(GTK_LABEL(m_widget), justify);
1a5594b8 73
1a5594b8
VZ
74 // GTK_JUSTIFY_LEFT is 0, RIGHT 1 and CENTER 2
75 static const float labelAlignments[] = { 0.0, 1.0, 0.5 };
76 gtk_misc_set_alignment(GTK_MISC(m_widget), labelAlignments[justify], 0.0);
185fa6bf 77
d44a9337 78 gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE );
33720b2d 79
39bc0347
VZ
80#ifdef __WXGTK26__
81 if (!gtk_check_version(2,6,0))
82 {
83 // set ellipsize mode
84 PangoEllipsizeMode ellipsizeMode = PANGO_ELLIPSIZE_NONE;
85 if ( style & wxST_ELLIPSIZE_START )
86 ellipsizeMode = PANGO_ELLIPSIZE_START;
87 else if ( style & wxST_ELLIPSIZE_MIDDLE )
88 ellipsizeMode = PANGO_ELLIPSIZE_MIDDLE;
89 else if ( style & wxST_ELLIPSIZE_END )
90 ellipsizeMode = PANGO_ELLIPSIZE_END;
91
92 gtk_label_set_ellipsize( GTK_LABEL(m_widget), ellipsizeMode );
93 }
94#endif // __WXGTK26__
95
96 SetLabel(label);
97
f03fc89f 98 m_parent->DoAddChild( this );
b58197f2 99
abdeb9e7 100 PostCreation(size);
3d257b8d 101
7f85af82 102 return true;
3f659fd6 103}
c801d85f 104
39bc0347 105void wxStaticText::SetLabel( const wxString& str )
c801d85f 106{
d44a9337 107 wxCHECK_RET( m_widget != NULL, wxT("invalid static text") );
8ccac798 108
39bc0347
VZ
109 // save the label inside m_labelOrig in case user calls GetLabel() later
110 m_labelOrig = str;
111
7f85af82
PC
112 InvalidateBestSize();
113
39bc0347
VZ
114 wxString label(str);
115 if (gtk_check_version(2,6,0) &&
116 IsEllipsized())
117 {
118 // GTK+ < 2.6 does not support ellipsization:
119 // since we need to use our generic code for ellipsization (which does not
120 // behaves well in conjunction with markup; i.e. it may break the markup
121 // validity erasing portions of the string), we also need to strip out
122 // the markup (if present) from the label.
123
124 label = GetEllipsizedLabelWithoutMarkup();
125 }
126
127 if ( HasFlag(wxST_MARKUP) )
128 GTKSetLabelWithMarkupForLabel(GTK_LABEL(m_widget), label);
129 else
130 GTKSetLabelForLabel(GTK_LABEL(m_widget), label);
c0e6c051
RD
131
132 // adjust the label size to the new label unless disabled
39bc0347
VZ
133 if ( !HasFlag(wxST_NO_AUTORESIZE) &&
134 !IsEllipsized() ) // if ellipsize is ON, then we don't want to get resized!
c0e6c051 135 SetSize( GetBestSize() );
33720b2d
RR
136}
137
c0e6c051
RD
138bool wxStaticText::SetFont( const wxFont &font )
139{
ac6e0eb1
VZ
140 const bool wasUnderlined = GetFont().GetUnderlined();
141
c0e6c051
RD
142 bool ret = wxControl::SetFont(font);
143
ac6e0eb1
VZ
144 if ( font.GetUnderlined() != wasUnderlined )
145 {
146 // the underlines for mnemonics are incompatible with using attributes
147 // so turn them off when setting underlined font and restore them when
148 // unsetting it
149 gtk_label_set_use_underline(GTK_LABEL(m_widget), wasUnderlined);
150
151 if ( wasUnderlined )
152 {
153 // it's not underlined any more, remove the attributes we set
154 gtk_label_set_attributes(GTK_LABEL(m_widget), NULL);
155 }
156 else // the text is underlined now
157 {
158 PangoAttrList *attrs = pango_attr_list_new();
159 PangoAttribute *a = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
160 a->start_index = 0;
97a85898 161 a->end_index = (guint)-1;
ac6e0eb1
VZ
162 pango_attr_list_insert(attrs, a);
163 gtk_label_set_attributes(GTK_LABEL(m_widget), attrs);
164 pango_attr_list_unref(attrs);
165 }
166 }
167
c0e6c051
RD
168 // adjust the label size to the new label unless disabled
169 if (!HasFlag(wxST_NO_AUTORESIZE))
170 {
171 SetSize( GetBestSize() );
c0e6c051
RD
172 }
173 return ret;
174}
58614078 175
a5040b80
RR
176void wxStaticText::DoSetSize(int x, int y,
177 int width, int height,
178 int sizeFlags )
179{
7f85af82 180 wxStaticTextBase::DoSetSize(x, y, width, height, sizeFlags);
39bc0347
VZ
181
182 if (gtk_check_version(2,6,0))
183 {
184 // GTK+ < 2.6 does not support ellipsization - we need to run our
185 // generic code (actually it will be run only if IsEllipsized() == true)
186 UpdateLabel();
187 }
a5040b80
RR
188}
189
33720b2d
RR
190wxSize wxStaticText::DoGetBestSize() const
191{
192 // Do not return any arbitrary default value...
193 wxASSERT_MSG( m_widget, wxT("wxStaticText::DoGetBestSize called before creation") );
194
61053de4
VZ
195 // GetBestSize is supposed to return unwrapped size but calling
196 // gtk_label_set_line_wrap() from here is a bad idea as it queues another
197 // size request by calling gtk_widget_queue_resize() and we end up in
198 // infinite loop sometimes (notably when the control is in a toolbar)
199 GTK_LABEL(m_widget)->wrap = FALSE;
3d257b8d 200
7f85af82 201 wxSize size = wxStaticTextBase::DoGetBestSize();
33720b2d 202
61053de4 203 GTK_LABEL(m_widget)->wrap = TRUE; // restore old value
3d257b8d 204
8d1c5c3f 205 // Adding 1 to width to workaround GTK sometimes wrapping the text needlessly
7f85af82
PC
206 size.x++;
207 CacheBestSize(size);
208 return size;
174b10af
RR
209}
210
2e1f5012
VZ
211bool wxStaticText::GTKWidgetNeedsMnemonic() const
212{
213 return true;
214}
215
216void wxStaticText::GTKWidgetDoSetMnemonic(GtkWidget* w)
217{
218 gtk_label_set_mnemonic_widget(GTK_LABEL(m_widget), w);
219}
220
39bc0347
VZ
221
222// These functions should be used only when GTK+ < 2.6 by wxStaticTextBase::UpdateLabel()
223
224wxString wxStaticText::DoGetLabel() const
225{
226 GtkLabel *label = GTK_LABEL(m_widget);
227 return wxGTK_CONV_BACK( gtk_label_get_text( label ) );
228}
229
230void wxStaticText::DoSetLabel(const wxString& str)
231{
232 GTKSetLabelForLabel(GTK_LABEL(m_widget), str);
233}
234
9d522606
RD
235// static
236wxVisualAttributes
237wxStaticText::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
238{
239 return GetDefaultAttributesFromGTKWidget(gtk_label_new);
240}
241
1e6feb95 242#endif // wxUSE_STATTEXT