1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/statbox.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
11
12 #if wxUSE_STATBOX
13
14 #include "wx/statbox.h"
15
16 #include <gtk/gtk.h>
17 #include "wx/gtk/private/gtk2-compat.h"
18 #include "wx/gtk/private/win_gtk.h"
19
20 // constants taken from GTK sources
21 #define LABEL_PAD 1
22 #define LABEL_SIDE_PAD 2
23
24 //-----------------------------------------------------------------------------
25 // "size_allocate" from m_widget
26 //-----------------------------------------------------------------------------
27
28 #ifndef __WXGTK3__
29 extern "C" {
size_allocate(GtkWidget * widget,GtkAllocation * alloc,void *)30 static void size_allocate(GtkWidget* widget, GtkAllocation* alloc, void*)
31 {
32 // clip label as GTK >= 2.12 does
33 GtkWidget* label_widget = gtk_frame_get_label_widget(GTK_FRAME(widget));
34 int w = alloc->width -
35 2 * gtk_widget_get_style(widget)->xthickness - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD;
36 if (w < 0)
37 w = 0;
38
39 GtkAllocation a;
40 gtk_widget_get_allocation(label_widget, &a);
41 if (a.width > w)
42 {
43 a.width = w;
44 gtk_widget_size_allocate(label_widget, &a);
45 }
46 }
47
expose_event(GtkWidget * widget,GdkEventExpose *,wxWindow *)48 static gboolean expose_event(GtkWidget* widget, GdkEventExpose*, wxWindow*)
49 {
50 const GtkAllocation& a = widget->allocation;
51 gtk_paint_flat_box(gtk_widget_get_style(widget), gtk_widget_get_window(widget),
52 GTK_STATE_NORMAL, GTK_SHADOW_NONE, NULL, widget, "", a.x, a.y, a.width, a.height);
53 return false;
54 }
55 }
56 #endif
57
58 //-----------------------------------------------------------------------------
59 // wxStaticBox
60 //-----------------------------------------------------------------------------
61
wxStaticBox()62 wxStaticBox::wxStaticBox()
63 {
64 }
65
wxStaticBox(wxWindow * parent,wxWindowID id,const wxString & label,const wxPoint & pos,const wxSize & size,long style,const wxString & name)66 wxStaticBox::wxStaticBox( wxWindow *parent,
67 wxWindowID id,
68 const wxString &label,
69 const wxPoint& pos,
70 const wxSize& size,
71 long style,
72 const wxString& name )
73 {
74 Create( parent, id, label, pos, size, style, name );
75 }
76
Create(wxWindow * parent,wxWindowID id,const wxString & label,const wxPoint & pos,const wxSize & size,long style,const wxString & name)77 bool wxStaticBox::Create( wxWindow *parent,
78 wxWindowID id,
79 const wxString& label,
80 const wxPoint& pos,
81 const wxSize& size,
82 long style,
83 const wxString& name )
84 {
85 if (!PreCreation( parent, pos, size ) ||
86 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
87 {
88 wxFAIL_MSG( wxT("wxStaticBox creation failed") );
89 return false;
90 }
91
92 m_widget = GTKCreateFrame(label);
93 g_object_ref(m_widget);
94
95 // only base SetLabel needs to be called after GTKCreateFrame
96 wxControl::SetLabel(label);
97
98 m_parent->DoAddChild( this );
99
100 PostCreation(size);
101
102 // need to set non default alignment?
103 gfloat xalign = 0;
104 if ( style & wxALIGN_CENTER )
105 xalign = 0.5;
106 else if ( style & wxALIGN_RIGHT )
107 xalign = 1.0;
108
109 gtk_frame_set_label_align(GTK_FRAME(m_widget), xalign, 0.5);
110
111 #ifndef __WXGTK3__
112 if (gtk_check_version(2, 12, 0))
113 {
114 // we connect this signal to perform label-clipping as GTK >= 2.12 does
115 g_signal_connect(m_widget, "size_allocate", G_CALLBACK(size_allocate), NULL);
116 }
117 #endif
118
119 m_container.DisableSelfFocus();
120
121 return true;
122 }
123
AddChild(wxWindowBase * child)124 void wxStaticBox::AddChild( wxWindowBase *child )
125 {
126 if (!m_wxwindow)
127 {
128 // make this window a container of other wxWindows by instancing a wxPizza
129 // and packing it into the GtkFrame:
130 m_wxwindow = wxPizza::New();
131 gtk_widget_show( m_wxwindow );
132 gtk_container_add( GTK_CONTAINER (m_widget), m_wxwindow );
133 GTKApplyWidgetStyle();
134 }
135
136 wxStaticBoxBase::AddChild(child);
137 }
138
SetLabel(const wxString & label)139 void wxStaticBox::SetLabel( const wxString& label )
140 {
141 wxCHECK_RET( m_widget != NULL, wxT("invalid staticbox") );
142
143 GTKSetLabelForFrame(GTK_FRAME(m_widget), label);
144 }
145
DoApplyWidgetStyle(GtkRcStyle * style)146 void wxStaticBox::DoApplyWidgetStyle(GtkRcStyle *style)
147 {
148 GTKFrameApplyWidgetStyle(GTK_FRAME(m_widget), style);
149 if (m_wxwindow)
150 GTKApplyStyle(m_wxwindow, style);
151
152 #ifndef __WXGTK3__
153 g_signal_handlers_disconnect_by_func(m_widget, (void*)expose_event, this);
154 if (m_backgroundColour.IsOk())
155 g_signal_connect(m_widget, "expose-event", G_CALLBACK(expose_event), this);
156 #endif
157 }
158
GTKWidgetNeedsMnemonic() const159 bool wxStaticBox::GTKWidgetNeedsMnemonic() const
160 {
161 return true;
162 }
163
GTKWidgetDoSetMnemonic(GtkWidget * w)164 void wxStaticBox::GTKWidgetDoSetMnemonic(GtkWidget* w)
165 {
166 GTKFrameSetMnemonicWidget(GTK_FRAME(m_widget), w);
167 }
168
169 // static
170 wxVisualAttributes
GetClassDefaultAttributes(wxWindowVariant WXUNUSED (variant))171 wxStaticBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
172 {
173 return GetDefaultAttributesFromGTKWidget(gtk_frame_new(""));
174 }
175
GetBordersForSizer(int * borderTop,int * borderOther) const176 void wxStaticBox::GetBordersForSizer(int *borderTop, int *borderOther) const
177 {
178 GtkAllocation alloc, child_alloc;
179 gtk_widget_get_allocation(m_widget, &alloc);
180 const int w_save = alloc.width;
181 const int h_save = alloc.height;
182 if (alloc.width < 50) alloc.width = 50;
183 if (alloc.height < 50) alloc.height = 50;
184 gtk_widget_set_allocation(m_widget, &alloc);
185
186 GTK_FRAME_GET_CLASS(m_widget)->compute_child_allocation(GTK_FRAME(m_widget), &child_alloc);
187
188 alloc.width = w_save;
189 alloc.height = h_save;
190 gtk_widget_set_allocation(m_widget, &alloc);
191
192 *borderTop = child_alloc.y - alloc.y;
193 *borderOther = child_alloc.x - alloc.x;
194 }
195
196 #endif // wxUSE_STATBOX
197