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