]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/button.cpp
cd85a483aec4614ef04b00a9002c058026f79376
[wxWidgets.git] / src / gtk1 / 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)
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 m_widget = gtk_button_new_with_label( "" );
85
86 #if (GTK_MINOR_VERSION > 0)
87 if (style & wxNO_BORDER)
88 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
89 #endif
90
91 SetLabel(label);
92
93 int x = 0; int y = 0;
94 wxFont new_font( parent->GetFont() );
95 GetTextExtent( m_label, &x, &y, (int*)NULL, (int*)NULL, &new_font );
96
97 wxSize newSize = size;
98 if (newSize.x == -1)
99 {
100 newSize.x = 12+x;
101 if (newSize.x < 80) newSize.x = 80;
102 }
103 if (newSize.y == -1)
104 {
105 newSize.y = 11+y;
106 if (newSize.x < 26) newSize.x = 26;
107 }
108
109 SetSize( newSize.x, newSize.y );
110
111 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
112 GTK_SIGNAL_FUNC(gtk_button_clicked_callback), (gpointer*)this );
113
114 m_parent->DoAddChild( this );
115
116 PostCreation();
117
118 SetBackgroundColour( parent->GetBackgroundColour() );
119 SetForegroundColour( parent->GetForegroundColour() );
120 SetFont( parent->GetFont() );
121
122 Show( TRUE );
123
124 return TRUE;
125 }
126
127 void wxButton::SetDefault()
128 {
129 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
130 gtk_widget_grab_default( m_widget );
131
132 SetSize( m_x, m_y, m_width, m_height );
133 }
134
135 /* static */
136 wxSize wxButton::GetDefaultSize()
137 {
138 return wxSize(80,26);
139 }
140
141 void wxButton::SetLabel( const wxString &label )
142 {
143 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
144
145 wxControl::SetLabel( label );
146
147 gtk_label_set( GTK_LABEL( GTK_BUTTON(m_widget)->child ), GetLabel().mbc_str() );
148 }
149
150 bool wxButton::Enable( bool enable )
151 {
152 if ( !wxControl::Enable( enable ) )
153 return FALSE;
154
155 gtk_widget_set_sensitive( GTK_BUTTON(m_widget)->child, enable );
156
157 return TRUE;
158 }
159
160 void wxButton::ApplyWidgetStyle()
161 {
162 SetWidgetStyle();
163 gtk_widget_set_style( m_widget, m_widgetStyle );
164 gtk_widget_set_style( GTK_BUTTON(m_widget)->child, m_widgetStyle );
165 }