Remove all lines containing cvs/svn "$Id$" keyword.
[wxWidgets.git] / src / gtk / statbox.cpp
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" {
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 }
48 #endif
49
50 //-----------------------------------------------------------------------------
51 // wxStaticBox
52 //-----------------------------------------------------------------------------
53
54 wxStaticBox::wxStaticBox()
55 {
56 }
57
58 wxStaticBox::wxStaticBox( wxWindow *parent,
59 wxWindowID id,
60 const wxString &label,
61 const wxPoint& pos,
62 const wxSize& size,
63 long style,
64 const wxString& name )
65 {
66 Create( parent, id, label, pos, size, style, name );
67 }
68
69 bool wxStaticBox::Create( wxWindow *parent,
70 wxWindowID id,
71 const wxString& label,
72 const wxPoint& pos,
73 const wxSize& size,
74 long style,
75 const wxString& name )
76 {
77 if (!PreCreation( parent, pos, size ) ||
78 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
79 {
80 wxFAIL_MSG( wxT("wxStaticBox creation failed") );
81 return false;
82 }
83
84 m_widget = GTKCreateFrame(label);
85 g_object_ref(m_widget);
86
87 // only base SetLabel needs to be called after GTKCreateFrame
88 wxControl::SetLabel(label);
89
90 m_parent->DoAddChild( this );
91
92 PostCreation(size);
93
94 // need to set non default alignment?
95 gfloat xalign = 0;
96 if ( style & wxALIGN_CENTER )
97 xalign = 0.5;
98 else if ( style & wxALIGN_RIGHT )
99 xalign = 1.0;
100
101 gtk_frame_set_label_align(GTK_FRAME(m_widget), xalign, 0.5);
102
103 #ifndef __WXGTK3__
104 if (gtk_check_version(2, 12, 0))
105 {
106 // we connect this signal to perform label-clipping as GTK >= 2.12 does
107 g_signal_connect(m_widget, "size_allocate", G_CALLBACK(size_allocate), NULL);
108 }
109 #endif
110
111 return true;
112 }
113
114 void wxStaticBox::AddChild( wxWindowBase *child )
115 {
116 if (!m_wxwindow)
117 {
118 // make this window a container of other wxWindows by instancing a wxPizza
119 // and packing it into the GtkFrame:
120 m_wxwindow = wxPizza::New();
121 gtk_widget_show( m_wxwindow );
122 gtk_container_add( GTK_CONTAINER (m_widget), m_wxwindow );
123 }
124
125 wxWindow::AddChild( child );
126 }
127
128 void wxStaticBox::SetLabel( const wxString& label )
129 {
130 wxCHECK_RET( m_widget != NULL, wxT("invalid staticbox") );
131
132 GTKSetLabelForFrame(GTK_FRAME(m_widget), label);
133 }
134
135 void wxStaticBox::DoApplyWidgetStyle(GtkRcStyle *style)
136 {
137 GTKFrameApplyWidgetStyle(GTK_FRAME(m_widget), style);
138 }
139
140 bool wxStaticBox::GTKWidgetNeedsMnemonic() const
141 {
142 return true;
143 }
144
145 void wxStaticBox::GTKWidgetDoSetMnemonic(GtkWidget* w)
146 {
147 GTKFrameSetMnemonicWidget(GTK_FRAME(m_widget), w);
148 }
149
150 // static
151 wxVisualAttributes
152 wxStaticBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
153 {
154 return GetDefaultAttributesFromGTKWidget(gtk_frame_new(""));
155 }
156
157
158 void wxStaticBox::GetBordersForSizer(int *borderTop, int *borderOther) const
159 {
160 *borderTop = GetCharHeight();
161 *borderOther = GetCharWidth()/2;
162 }
163
164 #endif // wxUSE_STATBOX