]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk1/stattext.cpp
Added missing include
[wxWidgets.git] / src / gtk1 / stattext.cpp
... / ...
CommitLineData
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
21extern "C"
22void wxgtk_window_size_request_callback(GtkWidget *widget,
23 GtkRequisition *requisition,
24 wxWindow *win);
25
26//-----------------------------------------------------------------------------
27// wxStaticText
28//-----------------------------------------------------------------------------
29
30IMPLEMENT_DYNAMIC_CLASS(wxStaticText,wxControl)
31
32wxStaticText::wxStaticText()
33{
34}
35
36wxStaticText::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
47bool 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 // notice that we call the base class version which will just remove the
65 // '&' characters from the string, but not set the label's text to it
66 // because the label is not yet created and because SetLabel() has a side
67 // effect of changing the control size which might not be desirable
68 wxControl::SetLabel(label);
69 m_widget = gtk_label_new( wxGTK_CONV( m_label ) );
70
71 GtkJustification justify;
72 if ( style & wxALIGN_CENTER )
73 justify = GTK_JUSTIFY_CENTER;
74 else if ( style & wxALIGN_RIGHT )
75 justify = GTK_JUSTIFY_RIGHT;
76 else // wxALIGN_LEFT is 0
77 justify = GTK_JUSTIFY_LEFT;
78 gtk_label_set_justify(GTK_LABEL(m_widget), justify);
79
80 // GTK_JUSTIFY_LEFT is 0, RIGHT 1 and CENTER 2
81 static const float labelAlignments[] = { 0.0, 1.0, 0.5 };
82 gtk_misc_set_alignment(GTK_MISC(m_widget), labelAlignments[justify], 0.0);
83
84 gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE );
85
86 m_parent->DoAddChild( this );
87
88 PostCreation(size);
89
90 return TRUE;
91}
92
93wxString wxStaticText::GetLabel() const
94{
95 GtkLabel *label = GTK_LABEL(m_widget);
96 wxString str = wxString( label->label );
97 return wxString(str);
98}
99
100void wxStaticText::SetLabel( const wxString &label )
101{
102 wxControl::SetLabel(label);
103
104 gtk_label_set( GTK_LABEL(m_widget), wxGTK_CONV( m_label ) );
105
106 // adjust the label size to the new label unless disabled
107 if (!HasFlag(wxST_NO_AUTORESIZE))
108 {
109 InvalidateBestSize();
110 SetSize( GetBestSize() );
111 }
112}
113
114bool wxStaticText::SetFont( const wxFont &font )
115{
116 bool ret = wxControl::SetFont(font);
117
118 // adjust the label size to the new label unless disabled
119 if (!HasFlag(wxST_NO_AUTORESIZE))
120 {
121 InvalidateBestSize();
122 SetSize( GetBestSize() );
123 }
124 return ret;
125}
126
127void wxStaticText::DoSetSize(int x, int y,
128 int width, int height,
129 int sizeFlags )
130{
131 wxControl::DoSetSize( x, y, width, height, sizeFlags );
132}
133
134wxSize wxStaticText::DoGetBestSize() const
135{
136 // Do not return any arbitrary default value...
137 wxASSERT_MSG( m_widget, wxT("wxStaticText::DoGetBestSize called before creation") );
138
139 // This resets the internal GTK1 size calculation, which
140 // otherwise would be cashed (incorrectly)
141 gtk_label_set_pattern( GTK_LABEL(m_widget), NULL );
142
143 // GetBestSize is supposed to return unwrapped size
144 gtk_label_set_line_wrap( GTK_LABEL(m_widget), FALSE );
145
146 GtkRequisition req;
147 req.width = -1;
148 req.height = -1;
149 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
150 (m_widget, &req );
151
152 gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE );
153
154 return wxSize (req.width, req.height);
155}
156
157bool wxStaticText::SetForegroundColour(const wxColour& colour)
158{
159 // First, we call the base class member
160 wxControl::SetForegroundColour(colour);
161 // Then, to force the color change, we set the label with the current label
162 SetLabel(GetLabel());
163 return true;
164}
165
166// static
167wxVisualAttributes
168wxStaticText::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
169{
170 return GetDefaultAttributesFromGTKWidget(gtk_label_new);
171}
172
173#endif // wxUSE_STATTEXT