merging back XTI branch part 2
[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 #include "wx/gtk/private/win_gtk.h" // for wxPizza
17
18 #include <gtk/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 extern "C" {
29 static void size_allocate(GtkWidget* widget, GtkAllocation* alloc, void*)
30 {
31 // clip label as GTK >= 2.12 does
32 GtkWidget* label_widget = gtk_frame_get_label_widget(GTK_FRAME(widget));
33 int w = alloc->width -
34 2 * widget->style->xthickness - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD;
35 if (w < 0)
36 w = 0;
37
38 if (label_widget->allocation.width > w)
39 {
40 GtkAllocation alloc2 = label_widget->allocation;
41 alloc2.width = w;
42 gtk_widget_size_allocate(label_widget, &alloc2);
43 }
44 }
45 }
46
47 //-----------------------------------------------------------------------------
48 // wxStaticBox
49 //-----------------------------------------------------------------------------
50
51 wxStaticBox::wxStaticBox()
52 {
53 }
54
55 wxStaticBox::wxStaticBox( wxWindow *parent,
56 wxWindowID id,
57 const wxString &label,
58 const wxPoint& pos,
59 const wxSize& size,
60 long style,
61 const wxString& name )
62 {
63 Create( parent, id, label, pos, size, style, name );
64 }
65
66 bool wxStaticBox::Create( 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 if (!PreCreation( parent, pos, size ) ||
75 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
76 {
77 wxFAIL_MSG( wxT("wxStaticBox creation failed") );
78 return false;
79 }
80
81 m_widget = GTKCreateFrame(label);
82 g_object_ref(m_widget);
83
84 // only base SetLabel needs to be called after GTKCreateFrame
85 wxControl::SetLabel(label);
86
87 m_parent->DoAddChild( this );
88
89 PostCreation(size);
90
91 // need to set non default alignment?
92 gfloat xalign = 0;
93 if ( style & wxALIGN_CENTER )
94 xalign = 0.5;
95 else if ( style & wxALIGN_RIGHT )
96 xalign = 1.0;
97
98 gtk_frame_set_label_align(GTK_FRAME(m_widget), xalign, 0.5);
99
100 if (gtk_check_version(2, 12, 0))
101 {
102 // we connect this signal to perform label-clipping as GTK >= 2.12 does
103 g_signal_connect(m_widget, "size_allocate", G_CALLBACK(size_allocate), NULL);
104 }
105
106 return true;
107 }
108
109 void wxStaticBox::AddChild( wxWindowBase *child )
110 {
111 if (!m_wxwindow)
112 {
113 // make this window a container of other wxWindows by instancing a wxPizza
114 // and packing it into the GtkFrame:
115 m_wxwindow = wxPizza::New();
116 gtk_widget_show( m_wxwindow );
117 gtk_container_add( GTK_CONTAINER (m_widget), m_wxwindow );
118 }
119
120 wxWindow::AddChild( child );
121 }
122
123 void wxStaticBox::SetLabel( const wxString& label )
124 {
125 wxCHECK_RET( m_widget != NULL, wxT("invalid staticbox") );
126
127 GTKSetLabelForFrame(GTK_FRAME(m_widget), label);
128 }
129
130 void wxStaticBox::DoApplyWidgetStyle(GtkRcStyle *style)
131 {
132 GTKFrameApplyWidgetStyle(GTK_FRAME(m_widget), style);
133 }
134
135 bool wxStaticBox::GTKWidgetNeedsMnemonic() const
136 {
137 return true;
138 }
139
140 void wxStaticBox::GTKWidgetDoSetMnemonic(GtkWidget* w)
141 {
142 GTKFrameSetMnemonicWidget(GTK_FRAME(m_widget), w);
143 }
144
145 // static
146 wxVisualAttributes
147 wxStaticBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
148 {
149 return GetDefaultAttributesFromGTKWidget(gtk_frame_new);
150 }
151
152
153 void wxStaticBox::GetBordersForSizer(int *borderTop, int *borderOther) const
154 {
155 const int BORDER = 5; // FIXME: hardcoded value
156
157 *borderTop = GetLabel().empty() ? 2*BORDER : GetCharHeight();
158 *borderOther = BORDER;
159 }
160
161 #endif // wxUSE_STATBOX