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