]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/stattext.cpp
1. don't assert if we're passed an empty defaultDir and full path in
[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 #include "gdk/gdk.h"
19 #include "gtk/gtk.h"
20
21 extern "C"
22 void wxgtk_window_size_request_callback(GtkWidget *widget,
23 GtkRequisition *requisition,
24 wxWindow *win);
25
26 //-----------------------------------------------------------------------------
27 // wxStaticText
28 //-----------------------------------------------------------------------------
29
30 IMPLEMENT_DYNAMIC_CLASS(wxStaticText,wxControl)
31
32 wxStaticText::wxStaticText()
33 {
34 }
35
36 wxStaticText::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)
43 {
44 Create( parent, id, label, pos, size, style, name );
45 }
46
47 bool 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 )
54 {
55 m_needParent = TRUE;
56
57 if (!PreCreation( parent, pos, size ) ||
58 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
59 {
60 wxFAIL_MSG( wxT("wxStaticText creation failed") );
61 return FALSE;
62 }
63
64 const wxString labelGTK = GTKConvertMnemonics(label);
65 m_label = label;
66 m_widget = gtk_label_new_with_mnemonic(wxGTK_CONV(labelGTK));
67
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;
75
76 if (GetLayoutDirection() == wxLayout_RightToLeft)
77 {
78 if (justify == GTK_JUSTIFY_RIGHT)
79 justify = GTK_JUSTIFY_LEFT;
80 if (justify == GTK_JUSTIFY_LEFT)
81 justify = GTK_JUSTIFY_RIGHT;
82 }
83
84 gtk_label_set_justify(GTK_LABEL(m_widget), justify);
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 m_parent->DoAddChild( this );
93
94 PostCreation(size);
95
96 // the bug below only happens with GTK 2
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)
103 g_signal_handlers_disconnect_by_func (m_widget,
104 (gpointer) wxgtk_window_size_request_callback,
105 this);
106 }
107
108 return TRUE;
109 }
110
111 wxString wxStaticText::GetLabel() const
112 {
113 GtkLabel *label = GTK_LABEL(m_widget);
114 wxString str = wxGTK_CONV_BACK( gtk_label_get_text( label ) );
115
116 return wxString(str);
117 }
118
119 void wxStaticText::SetLabel( const wxString &label )
120 {
121 wxCHECK_RET( m_widget != NULL, wxT("invalid static text") );
122
123 GTKSetLabelForLabel(GTK_LABEL(m_widget), label);
124
125 // adjust the label size to the new label unless disabled
126 if ( !HasFlag(wxST_NO_AUTORESIZE) )
127 SetSize( GetBestSize() );
128 }
129
130 bool wxStaticText::SetFont( const wxFont &font )
131 {
132 bool ret = wxControl::SetFont(font);
133
134 // adjust the label size to the new label unless disabled
135 if (!HasFlag(wxST_NO_AUTORESIZE))
136 {
137 InvalidateBestSize();
138 SetSize( GetBestSize() );
139 }
140 return ret;
141 }
142
143 void wxStaticText::DoSetSize(int x, int y,
144 int width, int height,
145 int sizeFlags )
146 {
147 wxControl::DoSetSize( x, y, width, height, sizeFlags );
148 }
149
150 wxSize wxStaticText::DoGetBestSize() const
151 {
152 // Do not return any arbitrary default value...
153 wxASSERT_MSG( m_widget, wxT("wxStaticText::DoGetBestSize called before creation") );
154
155 // GetBestSize is supposed to return unwrapped size
156 gtk_label_set_line_wrap( GTK_LABEL(m_widget), FALSE );
157
158 GtkRequisition req;
159 req.width = -1;
160 req.height = -1;
161 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
162 (m_widget, &req );
163
164 gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE );
165
166 // Adding 1 to width to workaround GTK sometimes wrapping the text needlessly
167 return wxSize (req.width+1, req.height);
168 }
169
170 bool wxStaticText::SetForegroundColour(const wxColour& colour)
171 {
172 // First, we call the base class member
173 wxControl::SetForegroundColour(colour);
174 // Then, to force the color change, we set the label with the current label
175 SetLabel(GetLabel());
176 return true;
177 }
178
179 bool wxStaticText::GTKWidgetNeedsMnemonic() const
180 {
181 return true;
182 }
183
184 void wxStaticText::GTKWidgetDoSetMnemonic(GtkWidget* w)
185 {
186 gtk_label_set_mnemonic_widget(GTK_LABEL(m_widget), w);
187 }
188
189 // static
190 wxVisualAttributes
191 wxStaticText::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
192 {
193 return GetDefaultAttributesFromGTKWidget(gtk_label_new);
194 }
195
196 #endif // wxUSE_STATTEXT