]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: stattext.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
a81258be RR |
5 | // Id: $Id$ |
6 | // Copyright: (c) 1998 Robert Roebling | |
65571936 | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
14f355c2 VS |
10 | // For compilers that support precompilation, includes "wx.h". |
11 | #include "wx/wxprec.h" | |
1e6feb95 VZ |
12 | |
13 | #if wxUSE_STATTEXT | |
14 | ||
c801d85f | 15 | #include "wx/stattext.h" |
3cbab641 | 16 | #include "wx/gtk1/private.h" |
c801d85f | 17 | |
83624f79 RR |
18 | #include "gdk/gdk.h" |
19 | #include "gtk/gtk.h" | |
20 | ||
e1f448ee VZ |
21 | extern "C" |
22 | void wxgtk_window_size_request_callback(GtkWidget *widget, | |
23 | GtkRequisition *requisition, | |
24 | wxWindow *win); | |
25 | ||
c801d85f KB |
26 | //----------------------------------------------------------------------------- |
27 | // wxStaticText | |
28 | //----------------------------------------------------------------------------- | |
29 | ||
30 | IMPLEMENT_DYNAMIC_CLASS(wxStaticText,wxControl) | |
31 | ||
b58197f2 | 32 | wxStaticText::wxStaticText() |
c801d85f | 33 | { |
3f659fd6 | 34 | } |
c801d85f | 35 | |
b58197f2 VZ |
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) | |
c801d85f KB |
43 | { |
44 | Create( parent, id, label, pos, size, style, name ); | |
3f659fd6 | 45 | } |
c801d85f | 46 | |
b58197f2 VZ |
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 ) | |
c801d85f | 54 | { |
a93109d5 | 55 | m_needParent = TRUE; |
b58197f2 | 56 | |
4dcaf11a RR |
57 | if (!PreCreation( parent, pos, size ) || |
58 | !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name )) | |
59 | { | |
8ccac798 | 60 | wxFAIL_MSG( wxT("wxStaticText creation failed") ); |
185fa6bf | 61 | return FALSE; |
4dcaf11a | 62 | } |
b58197f2 VZ |
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 | |
a93109d5 | 68 | wxControl::SetLabel(label); |
02c0348e | 69 | m_widget = gtk_label_new( wxGTK_CONV( m_label ) ); |
3d257b8d | 70 | |
a93109d5 RR |
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); | |
1a5594b8 | 79 | |
1a5594b8 VZ |
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); | |
185fa6bf | 83 | |
a5040b80 | 84 | gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE ); |
33720b2d | 85 | |
f03fc89f | 86 | m_parent->DoAddChild( this ); |
b58197f2 | 87 | |
abdeb9e7 | 88 | PostCreation(size); |
3d257b8d | 89 | |
a93109d5 | 90 | return TRUE; |
3f659fd6 | 91 | } |
c801d85f | 92 | |
ed58dbea | 93 | wxString wxStaticText::GetLabel() const |
c801d85f | 94 | { |
3b2e67f6 | 95 | GtkLabel *label = GTK_LABEL(m_widget); |
3b2e67f6 | 96 | wxString str = wxString( label->label ); |
b58197f2 | 97 | return wxString(str); |
3f659fd6 | 98 | } |
c801d85f KB |
99 | |
100 | void wxStaticText::SetLabel( const wxString &label ) | |
101 | { | |
8ccac798 VS |
102 | wxControl::SetLabel(label); |
103 | ||
3cbab641 | 104 | gtk_label_set( GTK_LABEL(m_widget), wxGTK_CONV( m_label ) ); |
c0e6c051 RD |
105 | |
106 | // adjust the label size to the new label unless disabled | |
107 | if (!HasFlag(wxST_NO_AUTORESIZE)) | |
108 | { | |
9f884528 | 109 | InvalidateBestSize(); |
c0e6c051 | 110 | SetSize( GetBestSize() ); |
c0e6c051 | 111 | } |
33720b2d RR |
112 | } |
113 | ||
c0e6c051 RD |
114 | bool 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 | { | |
9f884528 | 121 | InvalidateBestSize(); |
c0e6c051 | 122 | SetSize( GetBestSize() ); |
c0e6c051 RD |
123 | } |
124 | return ret; | |
125 | } | |
58614078 | 126 | |
a5040b80 RR |
127 | void 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 | ||
33720b2d RR |
134 | wxSize wxStaticText::DoGetBestSize() const |
135 | { | |
136 | // Do not return any arbitrary default value... | |
137 | wxASSERT_MSG( m_widget, wxT("wxStaticText::DoGetBestSize called before creation") ); | |
138 | ||
088ddc4e RR |
139 | // This resets the internal GTK1 size calculation, which |
140 | // otherwise would be cashed (incorrectly) | |
a5040b80 | 141 | gtk_label_set_pattern( GTK_LABEL(m_widget), NULL ); |
088ddc4e RR |
142 | |
143 | // GetBestSize is supposed to return unwrapped size | |
144 | gtk_label_set_line_wrap( GTK_LABEL(m_widget), FALSE ); | |
3d257b8d | 145 | |
33720b2d | 146 | GtkRequisition req; |
a5040b80 RR |
147 | req.width = -1; |
148 | req.height = -1; | |
2afa14f2 | 149 | (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request ) |
33720b2d RR |
150 | (m_widget, &req ); |
151 | ||
088ddc4e | 152 | gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE ); |
3d257b8d | 153 | |
a5040b80 | 154 | return wxSize (req.width, req.height); |
33720b2d RR |
155 | } |
156 | ||
174b10af RR |
157 | bool 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 | ||
9d522606 RD |
166 | // static |
167 | wxVisualAttributes | |
168 | wxStaticText::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
169 | { | |
170 | return GetDefaultAttributesFromGTKWidget(gtk_label_new); | |
171 | } | |
172 | ||
1e6feb95 | 173 | #endif // wxUSE_STATTEXT |