label clipping hack is not needed for GTK+ >= 2.12
[wxWidgets.git] / src / gtk / statbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/statbox.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_STATBOX
14
15 #include "wx/statbox.h"
16
17 #include <gtk/gtk.h>
18
19 // constants taken from GTK sources
20 #define LABEL_PAD 1
21 #define LABEL_SIDE_PAD 2
22
23 //-----------------------------------------------------------------------------
24 // "size_allocate" from m_widget
25 //-----------------------------------------------------------------------------
26
27 extern "C" {
28 static void size_allocate(GtkWidget* widget, GtkAllocation* alloc, void*)
29 {
30 // clip label as GTK >= 2.12 does
31 GtkWidget* label_widget = gtk_frame_get_label_widget(GTK_FRAME(widget));
32 int w = alloc->width -
33 2 * widget->style->xthickness - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD;
34 if (w < 0) w = 0;
35 if (label_widget->allocation.width > w)
36 {
37 GtkAllocation alloc2 = label_widget->allocation;
38 alloc2.width = w;
39 gtk_widget_size_allocate(label_widget, &alloc2);
40 }
41 }
42 }
43
44 //-----------------------------------------------------------------------------
45 // wxStaticBox
46 //-----------------------------------------------------------------------------
47
48 IMPLEMENT_DYNAMIC_CLASS(wxStaticBox, wxControl)
49
50 wxStaticBox::wxStaticBox()
51 {
52 }
53
54 wxStaticBox::wxStaticBox( wxWindow *parent,
55 wxWindowID id,
56 const wxString &label,
57 const wxPoint& pos,
58 const wxSize& size,
59 long style,
60 const wxString& name )
61 {
62 Create( parent, id, label, pos, size, style, name );
63 }
64
65 bool wxStaticBox::Create( wxWindow *parent,
66 wxWindowID id,
67 const wxString& label,
68 const wxPoint& pos,
69 const wxSize& size,
70 long style,
71 const wxString& name )
72 {
73 if (!PreCreation( parent, pos, size ) ||
74 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
75 {
76 wxFAIL_MSG( wxT("wxStaticBox creation failed") );
77 return false;
78 }
79
80 m_widget = GTKCreateFrame(label);
81 // only base SetLabel needs to be called after GTKCreateFrame
82 wxControl::SetLabel(label);
83
84 m_parent->DoAddChild( this );
85
86 PostCreation(size);
87
88 // need to set non default alignment?
89 gfloat xalign = 0;
90 if ( style & wxALIGN_CENTER )
91 xalign = 0.5;
92 else if ( style & wxALIGN_RIGHT )
93 xalign = 1.0;
94
95 gtk_frame_set_label_align(GTK_FRAME(m_widget), xalign, 0.5);
96
97 if (gtk_check_version(2, 12, 0))
98 {
99 // for clipping label as GTK >= 2.12 does
100 g_signal_connect(m_widget, "size_allocate",
101 G_CALLBACK(size_allocate), NULL);
102 }
103
104 return true;
105 }
106
107 void wxStaticBox::SetLabel( const wxString& label )
108 {
109 wxCHECK_RET( m_widget != NULL, wxT("invalid staticbox") );
110
111 GTKSetLabelForFrame(GTK_FRAME(m_widget), label);
112 }
113
114 void wxStaticBox::DoApplyWidgetStyle(GtkRcStyle *style)
115 {
116 GTKFrameApplyWidgetStyle(GTK_FRAME(m_widget), style);
117 }
118
119 bool wxStaticBox::GTKWidgetNeedsMnemonic() const
120 {
121 return true;
122 }
123
124 void wxStaticBox::GTKWidgetDoSetMnemonic(GtkWidget* w)
125 {
126 GTKFrameSetMnemonicWidget(GTK_FRAME(m_widget), w);
127 }
128
129 // static
130 wxVisualAttributes
131 wxStaticBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
132 {
133 return GetDefaultAttributesFromGTKWidget(gtk_frame_new);
134 }
135
136
137 void wxStaticBox::GetBordersForSizer(int *borderTop, int *borderOther) const
138 {
139 const int BORDER = 5; // FIXME: hardcoded value
140
141 *borderTop = GetLabel().empty() ? 2*BORDER : GetCharHeight();
142 *borderOther = BORDER;
143 }
144
145 #endif // wxUSE_STATBOX