| 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 | |
| 11 | #ifdef __GNUG__ |
| 12 | #pragma implementation "stattext.h" |
| 13 | #endif |
| 14 | |
| 15 | #include "wx/defs.h" |
| 16 | |
| 17 | #if wxUSE_STATTEXT |
| 18 | |
| 19 | #include "wx/stattext.h" |
| 20 | |
| 21 | #include "gdk/gdk.h" |
| 22 | #include "gtk/gtk.h" |
| 23 | |
| 24 | //----------------------------------------------------------------------------- |
| 25 | // wxStaticText |
| 26 | //----------------------------------------------------------------------------- |
| 27 | |
| 28 | IMPLEMENT_DYNAMIC_CLASS(wxStaticText,wxControl) |
| 29 | |
| 30 | wxStaticText::wxStaticText() |
| 31 | { |
| 32 | } |
| 33 | |
| 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) |
| 41 | { |
| 42 | Create( parent, id, label, pos, size, style, name ); |
| 43 | } |
| 44 | |
| 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 ) |
| 52 | { |
| 53 | m_needParent = TRUE; |
| 54 | |
| 55 | if (!PreCreation( parent, pos, size ) || |
| 56 | !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name )) |
| 57 | { |
| 58 | wxFAIL_MSG( wxT("wxXX creation failed") ); |
| 59 | return FALSE; |
| 60 | } |
| 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 |
| 66 | wxControl::SetLabel(label); |
| 67 | m_widget = gtk_label_new( m_label.mbc_str() ); |
| 68 | |
| 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); |
| 77 | |
| 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); |
| 81 | |
| 82 | // do not move this call elsewhere |
| 83 | gtk_label_set_line_wrap( GTK_LABEL(m_widget), FALSE ); |
| 84 | |
| 85 | m_parent->DoAddChild( this ); |
| 86 | |
| 87 | PostCreation(); |
| 88 | |
| 89 | ApplyWidgetStyle(); |
| 90 | |
| 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 | |
| 102 | SetBackgroundColour( parent->GetBackgroundColour() ); |
| 103 | SetForegroundColour( parent->GetForegroundColour() ); |
| 104 | Show( TRUE ); |
| 105 | |
| 106 | return TRUE; |
| 107 | } |
| 108 | |
| 109 | wxString wxStaticText::GetLabel() const |
| 110 | { |
| 111 | char *str = (char *) NULL; |
| 112 | gtk_label_get( GTK_LABEL(m_widget), &str ); |
| 113 | |
| 114 | return wxString(str); |
| 115 | } |
| 116 | |
| 117 | void wxStaticText::SetLabel( const wxString &label ) |
| 118 | { |
| 119 | wxControl::SetLabel(label); |
| 120 | |
| 121 | gtk_label_set( GTK_LABEL(m_widget), m_label.mbc_str() ); |
| 122 | |
| 123 | // adjust the label size to the new label unless disabled |
| 124 | if (!HasFlag(wxST_NO_AUTORESIZE)) |
| 125 | SetSize( GetBestSize() ); |
| 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; |
| 137 | } |
| 138 | |
| 139 | void wxStaticText::ApplyWidgetStyle() |
| 140 | { |
| 141 | SetWidgetStyle(); |
| 142 | gtk_widget_set_style( m_widget, m_widgetStyle ); |
| 143 | } |
| 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; |
| 157 | (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request ) |
| 158 | (m_widget, &req ); |
| 159 | |
| 160 | return wxSize(req.width, req.height); |
| 161 | } |
| 162 | |
| 163 | #endif // wxUSE_STATTEXT |