| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: button.cpp |
| 3 | // Purpose: |
| 4 | // Author: Robert Roebling |
| 5 | // Id: $Id$ |
| 6 | // Copyright: (c) 1998 Robert Roebling |
| 7 | // Licence: wxWindows licence |
| 8 | ///////////////////////////////////////////////////////////////////////////// |
| 9 | |
| 10 | #ifdef __GNUG__ |
| 11 | #pragma implementation "button.h" |
| 12 | #endif |
| 13 | |
| 14 | #include "wx/button.h" |
| 15 | |
| 16 | #include <gdk/gdk.h> |
| 17 | #include <gtk/gtk.h> |
| 18 | |
| 19 | //----------------------------------------------------------------------------- |
| 20 | // classes |
| 21 | //----------------------------------------------------------------------------- |
| 22 | |
| 23 | class wxButton; |
| 24 | |
| 25 | //----------------------------------------------------------------------------- |
| 26 | // idle system |
| 27 | //----------------------------------------------------------------------------- |
| 28 | |
| 29 | extern void wxapp_install_idle_handler(); |
| 30 | extern bool g_isIdle; |
| 31 | |
| 32 | //----------------------------------------------------------------------------- |
| 33 | // data |
| 34 | //----------------------------------------------------------------------------- |
| 35 | |
| 36 | extern bool g_blockEventsOnDrag; |
| 37 | |
| 38 | //----------------------------------------------------------------------------- |
| 39 | // "clicked" |
| 40 | //----------------------------------------------------------------------------- |
| 41 | |
| 42 | static void gtk_button_clicked_callback( GtkWidget *WXUNUSED(widget), wxButton *button ) |
| 43 | { |
| 44 | if (g_isIdle) |
| 45 | wxapp_install_idle_handler(); |
| 46 | |
| 47 | if (!button->m_hasVMT) return; |
| 48 | if (g_blockEventsOnDrag) return; |
| 49 | |
| 50 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId()); |
| 51 | event.SetEventObject(button); |
| 52 | button->GetEventHandler()->ProcessEvent(event); |
| 53 | } |
| 54 | |
| 55 | //----------------------------------------------------------------------------- |
| 56 | // wxButton |
| 57 | //----------------------------------------------------------------------------- |
| 58 | |
| 59 | IMPLEMENT_DYNAMIC_CLASS(wxButton,wxControl) |
| 60 | |
| 61 | wxButton::wxButton() |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | wxButton::~wxButton() |
| 66 | { |
| 67 | } |
| 68 | |
| 69 | bool wxButton::Create( wxWindow *parent, wxWindowID id, const wxString &label, |
| 70 | const wxPoint &pos, const wxSize &size, |
| 71 | long style, const wxValidator& validator, const wxString &name ) |
| 72 | { |
| 73 | m_needParent = TRUE; |
| 74 | m_acceptsFocus = TRUE; |
| 75 | |
| 76 | if (!PreCreation( parent, pos, size ) || |
| 77 | !CreateBase( parent, id, pos, size, style, validator, name )) |
| 78 | { |
| 79 | wxFAIL_MSG( wxT("wxButton creation failed") ); |
| 80 | return FALSE; |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | wxString label2( label ); |
| 85 | for (size_t i = 0; i < label2.Len(); i++) |
| 86 | { |
| 87 | if (label2.GetChar(i) == wxT('&')) |
| 88 | label2.SetChar(i,wxT('_')); |
| 89 | } |
| 90 | |
| 91 | GtkWidget *accel_label = gtk_accel_label_new( label2.mb_str() ); |
| 92 | gtk_widget_show( accel_label ); |
| 93 | |
| 94 | m_widget = gtk_button_new(); |
| 95 | gtk_container_add( GTK_CONTAINER(m_widget), accel_label ); |
| 96 | |
| 97 | gtk_accel_label_set_accel_widget( GTK_ACCEL_LABEL(accel_label), m_widget ); |
| 98 | |
| 99 | guint accel_key = gtk_label_parse_uline (GTK_LABEL(accel_label), label2.mb_str() ); |
| 100 | gtk_accel_label_refetch( GTK_ACCEL_LABEL(accel_label) ); |
| 101 | |
| 102 | wxControl::SetLabel( label ); |
| 103 | */ |
| 104 | |
| 105 | m_widget = gtk_button_new_with_label(""); |
| 106 | |
| 107 | SetLabel( label ); |
| 108 | |
| 109 | #if (GTK_MINOR_VERSION > 0) |
| 110 | if (style & wxNO_BORDER) |
| 111 | gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE ); |
| 112 | #endif |
| 113 | |
| 114 | gtk_signal_connect( GTK_OBJECT(m_widget), "clicked", |
| 115 | GTK_SIGNAL_FUNC(gtk_button_clicked_callback), (gpointer*)this ); |
| 116 | |
| 117 | m_parent->DoAddChild( this ); |
| 118 | |
| 119 | PostCreation(); |
| 120 | |
| 121 | SetFont( parent->GetFont() ); |
| 122 | |
| 123 | wxSize best_size( DoGetBestSize() ); |
| 124 | wxSize new_size( size ); |
| 125 | if (new_size.x == -1) |
| 126 | new_size.x = best_size.x; |
| 127 | if (new_size.y == -1) |
| 128 | new_size.y = best_size.y; |
| 129 | if ((new_size.x != size.x) || (new_size.y != size.y)) |
| 130 | SetSize( new_size.x, new_size.y ); |
| 131 | |
| 132 | SetSize( new_size ); |
| 133 | |
| 134 | SetBackgroundColour( parent->GetBackgroundColour() ); |
| 135 | SetForegroundColour( parent->GetForegroundColour() ); |
| 136 | |
| 137 | Show( TRUE ); |
| 138 | |
| 139 | return TRUE; |
| 140 | } |
| 141 | |
| 142 | void wxButton::SetDefault() |
| 143 | { |
| 144 | GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT ); |
| 145 | gtk_widget_grab_default( m_widget ); |
| 146 | |
| 147 | SetSize( m_x, m_y, m_width, m_height ); |
| 148 | } |
| 149 | |
| 150 | /* static */ |
| 151 | wxSize wxButton::GetDefaultSize() |
| 152 | { |
| 153 | return wxSize(80,26); |
| 154 | } |
| 155 | |
| 156 | void wxButton::SetLabel( const wxString &label ) |
| 157 | { |
| 158 | wxCHECK_RET( m_widget != NULL, wxT("invalid button") ); |
| 159 | |
| 160 | wxControl::SetLabel( label ); |
| 161 | |
| 162 | gtk_label_set( GTK_LABEL( GTK_BUTTON(m_widget)->child ), GetLabel().mbc_str() ); |
| 163 | } |
| 164 | |
| 165 | bool wxButton::Enable( bool enable ) |
| 166 | { |
| 167 | if ( !wxControl::Enable( enable ) ) |
| 168 | return FALSE; |
| 169 | |
| 170 | gtk_widget_set_sensitive( GTK_BUTTON(m_widget)->child, enable ); |
| 171 | |
| 172 | return TRUE; |
| 173 | } |
| 174 | |
| 175 | void wxButton::ApplyWidgetStyle() |
| 176 | { |
| 177 | SetWidgetStyle(); |
| 178 | gtk_widget_set_style( m_widget, m_widgetStyle ); |
| 179 | gtk_widget_set_style( GTK_BUTTON(m_widget)->child, m_widgetStyle ); |
| 180 | } |
| 181 | |
| 182 | wxSize wxButton::DoGetBestSize() const |
| 183 | { |
| 184 | wxSize ret( wxControl::DoGetBestSize() ); |
| 185 | if (ret.x < 80) ret.x = 80; |
| 186 | return ret; |
| 187 | } |
| 188 | |