Hold a reference on m_widget for the life of the associated wxWindow object.
[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 g_object_ref(m_widget);
82 // only base SetLabel needs to be called after GTKCreateFrame
83 wxControl::SetLabel(label);
84
85 m_parent->DoAddChild( this );
86
87 PostCreation(size);
88
89 // need to set non default alignment?
90 gfloat xalign = 0;
91 if ( style & wxALIGN_CENTER )
92 xalign = 0.5;
93 else if ( style & wxALIGN_RIGHT )
94 xalign = 1.0;
95
96 gtk_frame_set_label_align(GTK_FRAME(m_widget), xalign, 0.5);
97
98 if (gtk_check_version(2, 12, 0))
99 {
100 // for clipping label as GTK >= 2.12 does
101 g_signal_connect(m_widget, "size_allocate",
102 G_CALLBACK(size_allocate), NULL);
103 }
104
105 return true;
106 }
107
108 void wxStaticBox::SetLabel( const wxString& label )
109 {
110 wxCHECK_RET( m_widget != NULL, wxT("invalid staticbox") );
111
112 GTKSetLabelForFrame(GTK_FRAME(m_widget), label);
113 }
114
115 void wxStaticBox::DoApplyWidgetStyle(GtkRcStyle *style)
116 {
117 GTKFrameApplyWidgetStyle(GTK_FRAME(m_widget), style);
118 }
119
120 bool wxStaticBox::GTKWidgetNeedsMnemonic() const
121 {
122 return true;
123 }
124
125 void wxStaticBox::GTKWidgetDoSetMnemonic(GtkWidget* w)
126 {
127 GTKFrameSetMnemonicWidget(GTK_FRAME(m_widget), w);
128 }
129
130 // static
131 wxVisualAttributes
132 wxStaticBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
133 {
134 return GetDefaultAttributesFromGTKWidget(gtk_frame_new);
135 }
136
137
138 void wxStaticBox::GetBordersForSizer(int *borderTop, int *borderOther) const
139 {
140 const int BORDER = 5; // FIXME: hardcoded value
141
142 *borderTop = GetLabel().empty() ? 2*BORDER : GetCharHeight();
143 *borderOther = BORDER;
144 }
145
146 #endif // wxUSE_STATBOX