]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/stattext.cpp
Fixed home and end accelerators.
[wxWidgets.git] / src / gtk1 / 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/gtk1/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 m_label = label;
65 m_widget = gtk_label_new( wxGTK_CONV( GTKRemoveMnemonics(label)) );
66
67 GtkJustification justify;
68 if ( style & wxALIGN_CENTER )
69 justify = GTK_JUSTIFY_CENTER;
70 else if ( style & wxALIGN_RIGHT )
71 justify = GTK_JUSTIFY_RIGHT;
72 else // wxALIGN_LEFT is 0
73 justify = GTK_JUSTIFY_LEFT;
74 gtk_label_set_justify(GTK_LABEL(m_widget), justify);
75
76 // GTK_JUSTIFY_LEFT is 0, RIGHT 1 and CENTER 2
77 static const float labelAlignments[] = { 0.0, 1.0, 0.5 };
78 gtk_misc_set_alignment(GTK_MISC(m_widget), labelAlignments[justify], 0.0);
79
80 gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE );
81
82 m_parent->DoAddChild( this );
83
84 PostCreation(size);
85
86 return TRUE;
87 }
88
89 wxString wxStaticText::GetLabel() const
90 {
91 GtkLabel *label = GTK_LABEL(m_widget);
92 wxString str = wxString( label->label );
93 return wxString(str);
94 }
95
96 void wxStaticText::SetLabel( const wxString &label )
97 {
98 wxCHECK_RET( m_widget != NULL, wxT("invalid static text") );
99
100 GTKSetLabelForLabel(GTK_LABEL(m_widget), label);
101
102 // adjust the label size to the new label unless disabled
103 if (!HasFlag(wxST_NO_AUTORESIZE))
104 SetSize( GetBestSize() );
105 }
106
107 bool wxStaticText::SetFont( const wxFont &font )
108 {
109 bool ret = wxControl::SetFont(font);
110
111 // adjust the label size to the new label unless disabled
112 if (!HasFlag(wxST_NO_AUTORESIZE))
113 {
114 InvalidateBestSize();
115 SetSize( GetBestSize() );
116 }
117 return ret;
118 }
119
120 void wxStaticText::DoSetSize(int x, int y,
121 int width, int height,
122 int sizeFlags )
123 {
124 wxControl::DoSetSize( x, y, width, height, sizeFlags );
125 }
126
127 wxSize wxStaticText::DoGetBestSize() const
128 {
129 // Do not return any arbitrary default value...
130 wxASSERT_MSG( m_widget, wxT("wxStaticText::DoGetBestSize called before creation") );
131
132 // This resets the internal GTK1 size calculation, which
133 // otherwise would be cashed (incorrectly)
134 gtk_label_set_pattern( GTK_LABEL(m_widget), NULL );
135
136 // GetBestSize is supposed to return unwrapped size
137 gtk_label_set_line_wrap( GTK_LABEL(m_widget), FALSE );
138
139 GtkRequisition req;
140 req.width = -1;
141 req.height = -1;
142 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
143 (m_widget, &req );
144
145 gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE );
146
147 return wxSize (req.width, req.height);
148 }
149
150 bool wxStaticText::SetForegroundColour(const wxColour& colour)
151 {
152 // First, we call the base class member
153 wxControl::SetForegroundColour(colour);
154 // Then, to force the color change, we set the label with the current label
155 SetLabel(GetLabel());
156 return true;
157 }
158
159 // static
160 wxVisualAttributes
161 wxStaticText::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
162 {
163 return GetDefaultAttributesFromGTKWidget(gtk_label_new);
164 }
165
166 #endif // wxUSE_STATTEXT