]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/stattext.cpp
The mmedia contrib requires sys/soundcard.h on unix, so disable it on unix
[wxWidgets.git] / src / gtk1 / stattext.cpp
CommitLineData
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 10#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
b58197f2 11 #pragma implementation "stattext.h"
c801d85f
KB
12#endif
13
14f355c2
VS
14// For compilers that support precompilation, includes "wx.h".
15#include "wx/wxprec.h"
1e6feb95
VZ
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
e1f448ee
VZ
25extern "C"
26void wxgtk_window_size_request_callback(GtkWidget *widget,
27 GtkRequisition *requisition,
28 wxWindow *win);
29
c801d85f
KB
30//-----------------------------------------------------------------------------
31// wxStaticText
32//-----------------------------------------------------------------------------
33
34IMPLEMENT_DYNAMIC_CLASS(wxStaticText,wxControl)
35
b58197f2 36wxStaticText::wxStaticText()
c801d85f 37{
3f659fd6 38}
c801d85f 39
b58197f2
VZ
40wxStaticText::wxStaticText(wxWindow *parent,
41 wxWindowID id,
42 const wxString &label,
43 const wxPoint &pos,
44 const wxSize &size,
45 long style,
46 const wxString &name)
c801d85f
KB
47{
48 Create( parent, id, label, pos, size, style, name );
3f659fd6 49}
c801d85f 50
b58197f2
VZ
51bool wxStaticText::Create(wxWindow *parent,
52 wxWindowID id,
53 const wxString &label,
54 const wxPoint &pos,
55 const wxSize &size,
56 long style,
57 const wxString &name )
c801d85f 58{
a93109d5 59 m_needParent = TRUE;
b58197f2 60
4dcaf11a
RR
61 if (!PreCreation( parent, pos, size ) ||
62 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
63 {
8ccac798 64 wxFAIL_MSG( wxT("wxStaticText creation failed") );
185fa6bf 65 return FALSE;
4dcaf11a 66 }
b58197f2
VZ
67
68 // notice that we call the base class version which will just remove the
69 // '&' characters from the string, but not set the label's text to it
70 // because the label is not yet created and because SetLabel() has a side
71 // effect of changing the control size which might not be desirable
a93109d5 72 wxControl::SetLabel(label);
02c0348e 73 m_widget = gtk_label_new( wxGTK_CONV( m_label ) );
97d7bfb8 74
a93109d5
RR
75 GtkJustification justify;
76 if ( style & wxALIGN_CENTER )
77 justify = GTK_JUSTIFY_CENTER;
78 else if ( style & wxALIGN_RIGHT )
79 justify = GTK_JUSTIFY_RIGHT;
80 else // wxALIGN_LEFT is 0
81 justify = GTK_JUSTIFY_LEFT;
82 gtk_label_set_justify(GTK_LABEL(m_widget), justify);
1a5594b8 83
1a5594b8
VZ
84 // GTK_JUSTIFY_LEFT is 0, RIGHT 1 and CENTER 2
85 static const float labelAlignments[] = { 0.0, 1.0, 0.5 };
86 gtk_misc_set_alignment(GTK_MISC(m_widget), labelAlignments[justify], 0.0);
185fa6bf 87
a5040b80 88 gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE );
33720b2d 89
f03fc89f 90 m_parent->DoAddChild( this );
b58197f2 91
abdeb9e7 92 PostCreation(size);
7e2b55cd 93
e1f448ee
VZ
94 // the bug below only happens with GTK 2
95#ifdef __WXGTK20__
96 if ( justify != GTK_JUSTIFY_LEFT )
97 {
98 // if we let GTK call wxgtk_window_size_request_callback the label
99 // always shrinks to its minimal size for some reason and so no
100 // alignment except the default left doesn't work (in fact it does,
101 // but you don't see it)
102 gtk_signal_disconnect_by_func
103 (
104 GTK_OBJECT(m_widget),
105 GTK_SIGNAL_FUNC(wxgtk_window_size_request_callback),
106 (gpointer) this
107 );
108 }
109#endif // __WXGTK20__
110
a93109d5 111 return TRUE;
3f659fd6 112}
c801d85f 113
ed58dbea 114wxString wxStaticText::GetLabel() const
c801d85f 115{
3b2e67f6
RR
116 GtkLabel *label = GTK_LABEL(m_widget);
117
118#ifdef __WXGTK20__
119 wxString str = wxGTK_CONV_BACK( gtk_label_get_text( label ) );
120#else
121 wxString str = wxString( label->label );
122#endif
b58197f2
VZ
123
124 return wxString(str);
3f659fd6 125}
c801d85f
KB
126
127void wxStaticText::SetLabel( const wxString &label )
128{
8ccac798
VS
129 wxControl::SetLabel(label);
130
131#ifdef __WXGTK20__
132 // Build the colorized version of the label (markup only allowed
133 // under GTK2):
174b10af
RR
134 if (m_foregroundColour.Ok())
135 {
66bd83b4
VS
136 // If the color has been set, create a markup string to pass to
137 // the label setter
138 wxString colorlabel;
8ccac798
VS
139 colorlabel.Printf(_T("<span foreground=\"#%02x%02x%02x\">%s</span>"),
140 m_foregroundColour.Red(), m_foregroundColour.Green(),
66bd83b4
VS
141 m_foregroundColour.Blue(),
142 wxEscapeStringForPangoMarkup(label).c_str());
143 gtk_label_set_markup( GTK_LABEL(m_widget), wxGTK_CONV( colorlabel ) );
174b10af 144 }
66bd83b4 145 else
174b10af 146#endif
1171e2e5 147 gtk_label_set( GTK_LABEL(m_widget), wxGTK_CONV( m_label ) );
c0e6c051
RD
148
149 // adjust the label size to the new label unless disabled
150 if (!HasFlag(wxST_NO_AUTORESIZE))
151 {
9f884528 152 InvalidateBestSize();
c0e6c051 153 SetSize( GetBestSize() );
c0e6c051 154 }
33720b2d
RR
155}
156
c0e6c051
RD
157bool wxStaticText::SetFont( const wxFont &font )
158{
159 bool ret = wxControl::SetFont(font);
160
161 // adjust the label size to the new label unless disabled
162 if (!HasFlag(wxST_NO_AUTORESIZE))
163 {
9f884528 164 InvalidateBestSize();
c0e6c051 165 SetSize( GetBestSize() );
c0e6c051
RD
166 }
167 return ret;
168}
58614078 169
a5040b80
RR
170void wxStaticText::DoSetSize(int x, int y,
171 int width, int height,
172 int sizeFlags )
173{
174 wxControl::DoSetSize( x, y, width, height, sizeFlags );
175}
176
33720b2d
RR
177wxSize wxStaticText::DoGetBestSize() const
178{
179 // Do not return any arbitrary default value...
180 wxASSERT_MSG( m_widget, wxT("wxStaticText::DoGetBestSize called before creation") );
181
a5040b80 182#ifndef __WXGTK20__
088ddc4e
RR
183 // This resets the internal GTK1 size calculation, which
184 // otherwise would be cashed (incorrectly)
a5040b80 185 gtk_label_set_pattern( GTK_LABEL(m_widget), NULL );
a5040b80 186#endif
088ddc4e
RR
187
188 // GetBestSize is supposed to return unwrapped size
189 gtk_label_set_line_wrap( GTK_LABEL(m_widget), FALSE );
a5040b80 190
33720b2d 191 GtkRequisition req;
a5040b80
RR
192 req.width = -1;
193 req.height = -1;
2afa14f2 194 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
33720b2d
RR
195 (m_widget, &req );
196
088ddc4e
RR
197 gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE );
198
a5040b80 199 return wxSize (req.width, req.height);
33720b2d
RR
200}
201
174b10af
RR
202bool wxStaticText::SetForegroundColour(const wxColour& colour)
203{
204 // First, we call the base class member
205 wxControl::SetForegroundColour(colour);
206 // Then, to force the color change, we set the label with the current label
207 SetLabel(GetLabel());
208 return true;
209}
210
9d522606
RD
211// static
212wxVisualAttributes
213wxStaticText::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
214{
215 return GetDefaultAttributesFromGTKWidget(gtk_label_new);
216}
217
1e6feb95 218#endif // wxUSE_STATTEXT