1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        gtk/statbox.cpp
3 // Purpose:
4 // Author:      Robert Roebling
5 // Id:          $Id: statbox.cpp 45367 2007-04-09 21:41:54Z VZ $
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.h"
17 
18 #include "gdk/gdk.h"
19 #include "gtk/gtk.h"
20 
21 
22 // ============================================================================
23 // implementation
24 // ============================================================================
25 
26 // constants taken from GTK sources
27 #define LABEL_PAD 1
28 #define LABEL_SIDE_PAD 2
29 
30 //-----------------------------------------------------------------------------
31 // "gtk_frame_size_allocate" signal
32 //-----------------------------------------------------------------------------
33 
34 extern "C" {
35 
36 static void
gtk_frame_size_allocate(GtkWidget * widget,GtkAllocation * allocation,wxStaticBox * p)37 gtk_frame_size_allocate (GtkWidget     *widget,
38                          GtkAllocation *allocation,
39                          wxStaticBox *p)
40 {
41     GtkFrame *frame = GTK_FRAME (widget);
42 
43     // this handler gets called _after_ the GTK+'s own signal handler; thus we
44     // need to fix only the width of the GtkLabel
45     // (everything else has already been handled by the GTK+ signal handler).
46 
47     if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget))
48     {
49         GtkAllocation ca = frame->label_widget->allocation;
50 
51         // we want the GtkLabel to not exceed maxWidth:
52         int maxWidth = allocation->width - 2*LABEL_SIDE_PAD - 2*LABEL_PAD;
53         maxWidth = wxMax(2, maxWidth);      // maxWidth must always be positive!
54 
55         // truncate the label to the GtkFrame width...
56         ca.width = wxMin(ca.width, maxWidth);
57         gtk_widget_size_allocate(frame->label_widget, &ca);
58     }
59 }
60 
61 }
62 
63 
64 //-----------------------------------------------------------------------------
65 // wxStaticBox
66 //-----------------------------------------------------------------------------
67 
IMPLEMENT_DYNAMIC_CLASS(wxStaticBox,wxControl)68 IMPLEMENT_DYNAMIC_CLASS(wxStaticBox, wxControl)
69 
70 wxStaticBox::wxStaticBox()
71 {
72 }
73 
wxStaticBox(wxWindow * parent,wxWindowID id,const wxString & label,const wxPoint & pos,const wxSize & size,long style,const wxString & name)74 wxStaticBox::wxStaticBox( wxWindow *parent,
75                           wxWindowID id,
76                           const wxString &label,
77                           const wxPoint& pos,
78                           const wxSize& size,
79                           long style,
80                           const wxString& name )
81 {
82     Create( parent, id, label, pos, size, style, name );
83 }
84 
Create(wxWindow * parent,wxWindowID id,const wxString & label,const wxPoint & pos,const wxSize & size,long style,const wxString & name)85 bool wxStaticBox::Create( wxWindow *parent,
86                           wxWindowID id,
87                           const wxString& label,
88                           const wxPoint& pos,
89                           const wxSize& size,
90                           long style,
91                           const wxString& name )
92 {
93     m_needParent = TRUE;
94 
95     if (!PreCreation( parent, pos, size ) ||
96         !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
97     {
98         wxFAIL_MSG( wxT("wxStaticBox creation failed") );
99         return FALSE;
100     }
101 
102     m_widget = GTKCreateFrame(label);
103     wxControl::SetLabel(label);
104 
105     m_parent->DoAddChild( this );
106 
107     PostCreation(size);
108 
109     // need to set non default alignment?
110     gfloat xalign;
111     if ( style & wxALIGN_CENTER )
112         xalign = 0.5;
113     else if ( style & wxALIGN_RIGHT )
114         xalign = 1.0;
115     else // wxALIGN_LEFT
116         xalign = 0.0;
117 
118     if ( style & (wxALIGN_RIGHT | wxALIGN_CENTER) ) // left alignment is default
119         gtk_frame_set_label_align(GTK_FRAME( m_widget ), xalign, 0.5);
120 
121     // in order to clip the label widget, we must connect to the size allocate
122     // signal of this GtkFrame after the default GTK+'s allocate size function
123     g_signal_connect_after (m_widget, "size_allocate",
124                             G_CALLBACK (gtk_frame_size_allocate), this);
125 
126     return TRUE;
127 }
128 
SetLabel(const wxString & label)129 void wxStaticBox::SetLabel( const wxString& label )
130 {
131     wxCHECK_RET( m_widget != NULL, wxT("invalid staticbox") );
132 
133     GTKSetLabelForFrame(GTK_FRAME(m_widget), label);
134 }
135 
DoApplyWidgetStyle(GtkRcStyle * style)136 void wxStaticBox::DoApplyWidgetStyle(GtkRcStyle *style)
137 {
138     GTKFrameApplyWidgetStyle(GTK_FRAME(m_widget), style);
139 }
140 
GTKWidgetNeedsMnemonic() const141 bool wxStaticBox::GTKWidgetNeedsMnemonic() const
142 {
143     return true;
144 }
145 
GTKWidgetDoSetMnemonic(GtkWidget * w)146 void wxStaticBox::GTKWidgetDoSetMnemonic(GtkWidget* w)
147 {
148     GTKFrameSetMnemonicWidget(GTK_FRAME(m_widget), w);
149 }
150 
151 // static
152 wxVisualAttributes
GetClassDefaultAttributes(wxWindowVariant WXUNUSED (variant))153 wxStaticBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
154 {
155     return GetDefaultAttributesFromGTKWidget(gtk_frame_new);
156 }
157 
158 
GetBordersForSizer(int * borderTop,int * borderOther) const159 void wxStaticBox::GetBordersForSizer(int *borderTop, int *borderOther) const
160 {
161     const int BORDER = 5; // FIXME: hardcoded value
162 
163     *borderTop = GetLabel().empty() ? 2*BORDER : GetCharHeight();
164     *borderOther = BORDER;
165 }
166 
167 #endif // wxUSE_STATBOX
168