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