]>
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 | |
b58197f2 | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
10 | ||
11 | #ifdef __GNUG__ | |
b58197f2 | 12 | #pragma implementation "stattext.h" |
c801d85f KB |
13 | #endif |
14 | ||
1e6feb95 VZ |
15 | #include "wx/defs.h" |
16 | ||
17 | #if wxUSE_STATTEXT | |
18 | ||
c801d85f KB |
19 | #include "wx/stattext.h" |
20 | ||
83624f79 RR |
21 | #include "gdk/gdk.h" |
22 | #include "gtk/gtk.h" | |
23 | ||
c801d85f KB |
24 | //----------------------------------------------------------------------------- |
25 | // wxStaticText | |
26 | //----------------------------------------------------------------------------- | |
27 | ||
28 | IMPLEMENT_DYNAMIC_CLASS(wxStaticText,wxControl) | |
29 | ||
b58197f2 | 30 | wxStaticText::wxStaticText() |
c801d85f | 31 | { |
3f659fd6 | 32 | } |
c801d85f | 33 | |
b58197f2 VZ |
34 | wxStaticText::wxStaticText(wxWindow *parent, |
35 | wxWindowID id, | |
36 | const wxString &label, | |
37 | const wxPoint &pos, | |
38 | const wxSize &size, | |
39 | long style, | |
40 | const wxString &name) | |
c801d85f KB |
41 | { |
42 | Create( parent, id, label, pos, size, style, name ); | |
3f659fd6 | 43 | } |
c801d85f | 44 | |
b58197f2 VZ |
45 | bool wxStaticText::Create(wxWindow *parent, |
46 | wxWindowID id, | |
47 | const wxString &label, | |
48 | const wxPoint &pos, | |
49 | const wxSize &size, | |
50 | long style, | |
51 | const wxString &name ) | |
c801d85f | 52 | { |
a93109d5 | 53 | m_needParent = TRUE; |
b58197f2 | 54 | |
4dcaf11a RR |
55 | if (!PreCreation( parent, pos, size ) || |
56 | !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name )) | |
57 | { | |
223d09f6 | 58 | wxFAIL_MSG( wxT("wxXX creation failed") ); |
185fa6bf | 59 | return FALSE; |
4dcaf11a | 60 | } |
b58197f2 VZ |
61 | |
62 | // notice that we call the base class version which will just remove the | |
63 | // '&' characters from the string, but not set the label's text to it | |
64 | // because the label is not yet created and because SetLabel() has a side | |
65 | // effect of changing the control size which might not be desirable | |
a93109d5 | 66 | wxControl::SetLabel(label); |
9f15c5fe | 67 | m_widget = gtk_label_new( m_label.mbc_str() ); |
97d7bfb8 | 68 | |
a93109d5 RR |
69 | GtkJustification justify; |
70 | if ( style & wxALIGN_CENTER ) | |
71 | justify = GTK_JUSTIFY_CENTER; | |
72 | else if ( style & wxALIGN_RIGHT ) | |
73 | justify = GTK_JUSTIFY_RIGHT; | |
74 | else // wxALIGN_LEFT is 0 | |
75 | justify = GTK_JUSTIFY_LEFT; | |
76 | gtk_label_set_justify(GTK_LABEL(m_widget), justify); | |
1a5594b8 | 77 | |
1a5594b8 VZ |
78 | // GTK_JUSTIFY_LEFT is 0, RIGHT 1 and CENTER 2 |
79 | static const float labelAlignments[] = { 0.0, 1.0, 0.5 }; | |
80 | gtk_misc_set_alignment(GTK_MISC(m_widget), labelAlignments[justify], 0.0); | |
185fa6bf | 81 | |
33720b2d RR |
82 | // do not move this call elsewhere |
83 | gtk_label_set_line_wrap( GTK_LABEL(m_widget), FALSE ); | |
84 | ||
f03fc89f | 85 | m_parent->DoAddChild( this ); |
b58197f2 | 86 | |
a93109d5 | 87 | PostCreation(); |
7e2b55cd RR |
88 | |
89 | ApplyWidgetStyle(); | |
b58197f2 | 90 | |
db434467 RR |
91 | wxControl::SetFont( parent->GetFont() ); |
92 | ||
93 | wxSize size_best( DoGetBestSize() ); | |
94 | wxSize new_size( size ); | |
95 | if (new_size.x == -1) | |
96 | new_size.x = size_best.x; | |
97 | if (new_size.y == -1) | |
98 | new_size.y = size_best.y; | |
99 | if ((new_size.x != size.x) || (new_size.y != size.y)) | |
100 | SetSize( new_size.x, new_size.y ); | |
101 | ||
a93109d5 RR |
102 | SetBackgroundColour( parent->GetBackgroundColour() ); |
103 | SetForegroundColour( parent->GetForegroundColour() ); | |
a93109d5 | 104 | Show( TRUE ); |
b58197f2 | 105 | |
a93109d5 | 106 | return TRUE; |
3f659fd6 | 107 | } |
c801d85f | 108 | |
ed58dbea | 109 | wxString wxStaticText::GetLabel() const |
c801d85f | 110 | { |
a93109d5 RR |
111 | char *str = (char *) NULL; |
112 | gtk_label_get( GTK_LABEL(m_widget), &str ); | |
b58197f2 VZ |
113 | |
114 | return wxString(str); | |
3f659fd6 | 115 | } |
c801d85f KB |
116 | |
117 | void wxStaticText::SetLabel( const wxString &label ) | |
118 | { | |
a93109d5 | 119 | wxControl::SetLabel(label); |
32c77a71 | 120 | |
9f15c5fe | 121 | gtk_label_set( GTK_LABEL(m_widget), m_label.mbc_str() ); |
b58197f2 | 122 | |
185fa6bf | 123 | // adjust the label size to the new label unless disabled |
33720b2d | 124 | if (!HasFlag(wxST_NO_AUTORESIZE)) |
f68586e5 | 125 | SetSize( GetBestSize() ); |
33720b2d RR |
126 | } |
127 | ||
128 | bool wxStaticText::SetFont( const wxFont &font ) | |
129 | { | |
130 | bool ret = wxControl::SetFont(font); | |
131 | ||
132 | // adjust the label size to the new label unless disabled | |
133 | if (!HasFlag(wxST_NO_AUTORESIZE)) | |
134 | SetSize( GetBestSize() ); | |
135 | ||
136 | return ret; | |
b58197f2 | 137 | } |
58614078 RR |
138 | |
139 | void wxStaticText::ApplyWidgetStyle() | |
140 | { | |
a93109d5 RR |
141 | SetWidgetStyle(); |
142 | gtk_widget_set_style( m_widget, m_widgetStyle ); | |
58614078 | 143 | } |
33720b2d RR |
144 | |
145 | wxSize wxStaticText::DoGetBestSize() const | |
146 | { | |
147 | // Do not return any arbitrary default value... | |
148 | wxASSERT_MSG( m_widget, wxT("wxStaticText::DoGetBestSize called before creation") ); | |
149 | ||
150 | // this invalidates the size request | |
151 | gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE ); | |
152 | gtk_label_set_line_wrap( GTK_LABEL(m_widget), FALSE ); | |
153 | ||
154 | GtkRequisition req; | |
155 | req.width = 2; | |
156 | req.height = 2; | |
2afa14f2 | 157 | (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request ) |
33720b2d RR |
158 | (m_widget, &req ); |
159 | ||
160 | return wxSize(req.width, req.height); | |
161 | } | |
162 | ||
1e6feb95 | 163 | #endif // wxUSE_STATTEXT |