]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/stattext.cpp
Added wxRichTextTableBlock class to help with table UI operations
[wxWidgets.git] / src / gtk / stattext.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/gtk/stattext.cpp
3// Purpose:
4// Author: Robert Roebling
5// Copyright: (c) 1998 Robert Roebling
6// Licence: wxWindows licence
7/////////////////////////////////////////////////////////////////////////////
8
9// For compilers that support precompilation, includes "wx.h".
10#include "wx/wxprec.h"
11
12#if wxUSE_STATTEXT
13
14#include "wx/stattext.h"
15
16#include <gtk/gtk.h>
17#include "wx/gtk/private.h"
18
19//-----------------------------------------------------------------------------
20// wxStaticText
21//-----------------------------------------------------------------------------
22
23wxStaticText::wxStaticText()
24{
25}
26
27wxStaticText::wxStaticText(wxWindow *parent,
28 wxWindowID id,
29 const wxString &label,
30 const wxPoint &pos,
31 const wxSize &size,
32 long style,
33 const wxString &name)
34{
35 Create( parent, id, label, pos, size, style, name );
36}
37
38bool wxStaticText::Create(wxWindow *parent,
39 wxWindowID id,
40 const wxString &label,
41 const wxPoint &pos,
42 const wxSize &size,
43 long style,
44 const wxString &name )
45{
46 if (!PreCreation( parent, pos, size ) ||
47 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
48 {
49 wxFAIL_MSG( wxT("wxStaticText creation failed") );
50 return false;
51 }
52
53 m_widget = gtk_label_new(NULL);
54 g_object_ref(m_widget);
55
56 GtkJustification justify;
57 if ( style & wxALIGN_CENTER_HORIZONTAL )
58 justify = GTK_JUSTIFY_CENTER;
59 else if ( style & wxALIGN_RIGHT )
60 justify = GTK_JUSTIFY_RIGHT;
61 else
62 justify = GTK_JUSTIFY_LEFT;
63
64 if (GetLayoutDirection() == wxLayout_RightToLeft)
65 {
66 if (justify == GTK_JUSTIFY_RIGHT)
67 justify = GTK_JUSTIFY_LEFT;
68 else if (justify == GTK_JUSTIFY_LEFT)
69 justify = GTK_JUSTIFY_RIGHT;
70 }
71
72 gtk_label_set_justify(GTK_LABEL(m_widget), justify);
73
74 // set ellipsize mode
75 PangoEllipsizeMode ellipsizeMode = PANGO_ELLIPSIZE_NONE;
76 if ( style & wxST_ELLIPSIZE_START )
77 ellipsizeMode = PANGO_ELLIPSIZE_START;
78 else if ( style & wxST_ELLIPSIZE_MIDDLE )
79 ellipsizeMode = PANGO_ELLIPSIZE_MIDDLE;
80 else if ( style & wxST_ELLIPSIZE_END )
81 ellipsizeMode = PANGO_ELLIPSIZE_END;
82
83 gtk_label_set_ellipsize( GTK_LABEL(m_widget), ellipsizeMode );
84
85 // GTK_JUSTIFY_LEFT is 0, RIGHT 1 and CENTER 2
86 static const float labelAlignments[] = { 0.0, 1.0, 0.5 };
87 gtk_misc_set_alignment(GTK_MISC(m_widget), labelAlignments[justify], 0.0);
88
89 gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE );
90
91 SetLabel(label);
92
93 m_parent->DoAddChild( this );
94
95 PostCreation(size);
96
97 return true;
98}
99
100void wxStaticText::GTKDoSetLabel(GTKLabelSetter setter, const wxString& label)
101{
102 wxCHECK_RET( m_widget != NULL, wxT("invalid static text") );
103
104 InvalidateBestSize();
105
106 (this->*setter)(GTK_LABEL(m_widget), label);
107
108 // adjust the label size to the new label unless disabled
109 if ( !HasFlag(wxST_NO_AUTORESIZE) &&
110 !IsEllipsized() ) // if ellipsization is ON, then we don't want to get resized!
111 SetSize( GetBestSize() );
112}
113
114void wxStaticText::SetLabel(const wxString& label)
115{
116 m_labelOrig = label;
117
118 GTKDoSetLabel(&wxStaticText::GTKSetLabelForLabel, label);
119}
120
121#if wxUSE_MARKUP
122
123bool wxStaticText::DoSetLabelMarkup(const wxString& markup)
124{
125 const wxString stripped = RemoveMarkup(markup);
126 if ( stripped.empty() && !markup.empty() )
127 return false;
128
129 m_labelOrig = stripped;
130
131 GTKDoSetLabel(&wxStaticText::GTKSetLabelWithMarkupForLabel, markup);
132
133 return true;
134}
135
136#endif // wxUSE_MARKUP
137
138bool wxStaticText::SetFont( const wxFont &font )
139{
140 const bool wasUnderlined = GetFont().GetUnderlined();
141 const bool wasStrickenThrough = GetFont().GetStrikethrough();
142
143 bool ret = wxControl::SetFont(font);
144
145 const bool isUnderlined = GetFont().GetUnderlined();
146 const bool isStrickenThrough = GetFont().GetStrikethrough();
147
148 if ( (isUnderlined != wasUnderlined) ||
149 (isStrickenThrough != wasStrickenThrough) )
150 {
151 // We need to update the Pango attributes used for the text.
152 if ( isUnderlined || isStrickenThrough )
153 {
154 PangoAttrList* const attrs = pango_attr_list_new();
155 if ( isUnderlined )
156 {
157 PangoAttribute *a = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
158 a->start_index = 0;
159 a->end_index = (guint)-1;
160 pango_attr_list_insert(attrs, a);
161 }
162
163 if ( isStrickenThrough )
164 {
165 PangoAttribute *a = pango_attr_strikethrough_new( TRUE );
166 a->start_index = 0;
167 a->end_index = (guint) -1;
168 pango_attr_list_insert(attrs, a);
169 }
170
171 gtk_label_set_attributes(GTK_LABEL(m_widget), attrs);
172 pango_attr_list_unref(attrs);
173 }
174 else // No special attributes any more.
175 {
176 // Just remove any attributes we had set.
177 gtk_label_set_attributes(GTK_LABEL(m_widget), NULL);
178 }
179
180 // The underlines for mnemonics are incompatible with using attributes
181 // so turn them off when setting underlined font.
182 gtk_label_set_use_underline(GTK_LABEL(m_widget), !isUnderlined);
183 }
184
185 // adjust the label size to the new label unless disabled
186 if (!HasFlag(wxST_NO_AUTORESIZE))
187 {
188 SetSize( GetBestSize() );
189 }
190 return ret;
191}
192
193wxSize wxStaticText::DoGetBestSize() const
194{
195 // Do not return any arbitrary default value...
196 wxASSERT_MSG( m_widget, wxT("wxStaticText::DoGetBestSize called before creation") );
197
198 // GetBestSize is supposed to return unwrapped size but calling
199 // gtk_label_set_line_wrap() from here is a bad idea as it queues another
200 // size request by calling gtk_widget_queue_resize() and we end up in
201 // infinite loop sometimes (notably when the control is in a toolbar)
202 // With GTK3 however, there is no simple alternative, and the sizing loop
203 // no longer seems to occur.
204#ifdef __WXGTK3__
205 gtk_label_set_line_wrap(GTK_LABEL(m_widget), false);
206#else
207 GTK_LABEL(m_widget)->wrap = FALSE;
208#endif
209 wxSize size = wxStaticTextBase::DoGetBestSize();
210#ifdef __WXGTK3__
211 gtk_label_set_line_wrap(GTK_LABEL(m_widget), true);
212#else
213 GTK_LABEL(m_widget)->wrap = TRUE; // restore old value
214#endif
215
216 // Adding 1 to width to workaround GTK sometimes wrapping the text needlessly
217 size.x++;
218 CacheBestSize(size);
219 return size;
220}
221
222bool wxStaticText::GTKWidgetNeedsMnemonic() const
223{
224 return true;
225}
226
227void wxStaticText::GTKWidgetDoSetMnemonic(GtkWidget* w)
228{
229 gtk_label_set_mnemonic_widget(GTK_LABEL(m_widget), w);
230}
231
232
233// These functions should be used only when GTK+ < 2.6 by wxStaticTextBase::UpdateLabel()
234
235wxString wxStaticText::DoGetLabel() const
236{
237 GtkLabel *label = GTK_LABEL(m_widget);
238 return wxGTK_CONV_BACK( gtk_label_get_text( label ) );
239}
240
241void wxStaticText::DoSetLabel(const wxString& str)
242{
243 GTKSetLabelForLabel(GTK_LABEL(m_widget), str);
244}
245
246// static
247wxVisualAttributes
248wxStaticText::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
249{
250 return GetDefaultAttributesFromGTKWidget(gtk_label_new(""));
251}
252
253#endif // wxUSE_STATTEXT