Small modifications to Vadims changes
[wxWidgets.git] / src / gtk / button.cpp
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) wxapp_install_idle_handler();
45
46 if (!button->m_hasVMT) return;
47 if (g_blockEventsOnDrag) return;
48
49 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
50 event.SetEventObject(button);
51 button->GetEventHandler()->ProcessEvent(event);
52 }
53
54 //-----------------------------------------------------------------------------
55 // wxButton
56 //-----------------------------------------------------------------------------
57
58 IMPLEMENT_DYNAMIC_CLASS(wxButton,wxControl)
59
60 wxButton::wxButton()
61 {
62 }
63
64 wxButton::~wxButton()
65 {
66 if (m_clientData) delete m_clientData;
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_clientData = (wxClientData*) NULL;
74 m_needParent = TRUE;
75 m_acceptsFocus = TRUE;
76
77 wxSize newSize = size;
78
79 PreCreation( parent, id, pos, newSize, style, name );
80
81 SetValidator( validator );
82
83 m_widget = gtk_button_new_with_label( "" );
84
85 #if (GTK_MINOR_VERSION > 0)
86 if (style & wxNO_BORDER)
87 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
88 #endif
89
90 SetLabel(label);
91
92 if (newSize.x == -1) newSize.x = 15+gdk_string_measure( m_widget->style->font, label.mbc_str() );
93 if (newSize.y == -1) newSize.y = 26;
94 SetSize( newSize.x, newSize.y );
95
96 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
97 GTK_SIGNAL_FUNC(gtk_button_clicked_callback), (gpointer*)this );
98
99 m_parent->DoAddChild( this );
100
101 PostCreation();
102
103 SetBackgroundColour( parent->GetBackgroundColour() );
104 SetForegroundColour( parent->GetForegroundColour() );
105 SetFont( parent->GetFont() );
106
107 Show( TRUE );
108
109 return TRUE;
110 }
111
112 void wxButton::SetDefault(void)
113 {
114 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
115 gtk_widget_grab_default( m_widget );
116
117 SetSize( m_x, m_y, m_width, m_height );
118 }
119
120 void wxButton::SetLabel( const wxString &label )
121 {
122 wxCHECK_RET( m_widget != NULL, _T("invalid button") );
123
124 wxControl::SetLabel( label );
125
126 gtk_label_set( GTK_LABEL( GTK_BUTTON(m_widget)->child ), GetLabel().mbc_str() );
127 }
128
129 bool wxButton::Enable( bool enable )
130 {
131 if ( !wxControl::Enable( enable ) )
132 return FALSE;
133
134 gtk_widget_set_sensitive( GTK_BUTTON(m_widget)->child, enable );
135
136 return TRUE;
137 }
138
139 void wxButton::ApplyWidgetStyle()
140 {
141 SetWidgetStyle();
142 gtk_widget_set_style( m_widget, m_widgetStyle );
143 gtk_widget_set_style( GTK_BUTTON(m_widget)->child, m_widgetStyle );
144 }