]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/stattext.cpp
don't use deprecated toolbar API
[wxWidgets.git] / src / gtk / stattext.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #include "wx/gtk/private.h"
17
18 //-----------------------------------------------------------------------------
19 // wxStaticText
20 //-----------------------------------------------------------------------------
21
22 IMPLEMENT_DYNAMIC_CLASS(wxStaticText,wxControl)
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
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 // 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);
77
78 gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE );
79
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
98 m_parent->DoAddChild( this );
99
100 PostCreation(size);
101
102 return true;
103 }
104
105 void wxStaticText::SetLabel( const wxString& str )
106 {
107 wxCHECK_RET( m_widget != NULL, wxT("invalid static text") );
108
109 // save the label inside m_labelOrig in case user calls GetLabel() later
110 m_labelOrig = str;
111
112 InvalidateBestSize();
113
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);
131
132 // adjust the label size to the new label unless disabled
133 if ( !HasFlag(wxST_NO_AUTORESIZE) &&
134 !IsEllipsized() ) // if ellipsize is ON, then we don't want to get resized!
135 SetSize( GetBestSize() );
136 }
137
138 bool wxStaticText::SetFont( const wxFont &font )
139 {
140 const bool wasUnderlined = GetFont().GetUnderlined();
141
142 bool ret = wxControl::SetFont(font);
143
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;
161 a->end_index = (guint)-1;
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
168 // adjust the label size to the new label unless disabled
169 if (!HasFlag(wxST_NO_AUTORESIZE))
170 {
171 SetSize( GetBestSize() );
172 }
173 return ret;
174 }
175
176 void wxStaticText::DoSetSize(int x, int y,
177 int width, int height,
178 int sizeFlags )
179 {
180 wxStaticTextBase::DoSetSize(x, y, width, height, sizeFlags);
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 }
188 }
189
190 wxSize wxStaticText::DoGetBestSize() const
191 {
192 // Do not return any arbitrary default value...
193 wxASSERT_MSG( m_widget, wxT("wxStaticText::DoGetBestSize called before creation") );
194
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;
200
201 wxSize size = wxStaticTextBase::DoGetBestSize();
202
203 GTK_LABEL(m_widget)->wrap = TRUE; // restore old value
204
205 // Adding 1 to width to workaround GTK sometimes wrapping the text needlessly
206 size.x++;
207 CacheBestSize(size);
208 return size;
209 }
210
211 bool wxStaticText::GTKWidgetNeedsMnemonic() const
212 {
213 return true;
214 }
215
216 void wxStaticText::GTKWidgetDoSetMnemonic(GtkWidget* w)
217 {
218 gtk_label_set_mnemonic_widget(GTK_LABEL(m_widget), w);
219 }
220
221
222 // These functions should be used only when GTK+ < 2.6 by wxStaticTextBase::UpdateLabel()
223
224 wxString wxStaticText::DoGetLabel() const
225 {
226 GtkLabel *label = GTK_LABEL(m_widget);
227 return wxGTK_CONV_BACK( gtk_label_get_text( label ) );
228 }
229
230 void wxStaticText::DoSetLabel(const wxString& str)
231 {
232 GTKSetLabelForLabel(GTK_LABEL(m_widget), str);
233 }
234
235 // static
236 wxVisualAttributes
237 wxStaticText::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
238 {
239 return GetDefaultAttributesFromGTKWidget(gtk_label_new);
240 }
241
242 #endif // wxUSE_STATTEXT