]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/button.cpp
Support for new native OS/2 tree control with drag-and-drop.
[wxWidgets.git] / src / gtk1 / button.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: button.cpp
3// Purpose:
4// Author: Robert Roebling
dbf858b5 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
8ab696e0 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
11#pragma implementation "button.h"
12#endif
13
1e6feb95
VZ
14#include "wx/defs.h"
15
16#if wxUSE_BUTTON
17
c801d85f
KB
18#include "wx/button.h"
19
9e691f46 20#include "wx/gtk/private.h"
83624f79 21
c801d85f
KB
22//-----------------------------------------------------------------------------
23// classes
24//-----------------------------------------------------------------------------
25
26class wxButton;
27
acfd422a
RR
28//-----------------------------------------------------------------------------
29// idle system
30//-----------------------------------------------------------------------------
31
32extern void wxapp_install_idle_handler();
33extern bool g_isIdle;
34
66bd6b93
RR
35//-----------------------------------------------------------------------------
36// data
37//-----------------------------------------------------------------------------
38
39extern bool g_blockEventsOnDrag;
40
c801d85f 41//-----------------------------------------------------------------------------
e1e955e1 42// "clicked"
c801d85f
KB
43//-----------------------------------------------------------------------------
44
66bd6b93 45static void gtk_button_clicked_callback( GtkWidget *WXUNUSED(widget), wxButton *button )
c801d85f 46{
9e691f46 47 if (g_isIdle)
32ac755d 48 wxapp_install_idle_handler();
acfd422a 49
a2053b27 50 if (!button->m_hasVMT) return;
acfd422a 51 if (g_blockEventsOnDrag) return;
9e691f46 52
acfd422a
RR
53 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
54 event.SetEventObject(button);
55 button->GetEventHandler()->ProcessEvent(event);
6de97a3b 56}
c801d85f
KB
57
58//-----------------------------------------------------------------------------
e1e955e1
RR
59// wxButton
60//-----------------------------------------------------------------------------
61
62IMPLEMENT_DYNAMIC_CLASS(wxButton,wxControl)
c801d85f 63
fd0eed64 64wxButton::wxButton()
c801d85f 65{
6de97a3b 66}
c801d85f 67
fd0eed64
RR
68wxButton::~wxButton()
69{
fd0eed64
RR
70}
71
c801d85f 72bool wxButton::Create( wxWindow *parent, wxWindowID id, const wxString &label,
32c77a71 73 const wxPoint &pos, const wxSize &size,
6de97a3b 74 long style, const wxValidator& validator, const wxString &name )
c801d85f 75{
b292e2f5
RR
76 m_needParent = TRUE;
77 m_acceptsFocus = TRUE;
32c77a71 78
4dcaf11a
RR
79 if (!PreCreation( parent, pos, size ) ||
80 !CreateBase( parent, id, pos, size, style, validator, name ))
81 {
223d09f6 82 wxFAIL_MSG( wxT("wxButton creation failed") );
8ab696e0 83 return FALSE;
4dcaf11a 84 }
c801d85f 85
354aa1e3
RR
86/*
87 wxString label2( label );
88 for (size_t i = 0; i < label2.Len(); i++)
89 {
90 if (label2.GetChar(i) == wxT('&'))
8ab696e0 91 label2.SetChar(i,wxT('_'));
354aa1e3 92 }
9e691f46 93
354aa1e3
RR
94 GtkWidget *accel_label = gtk_accel_label_new( label2.mb_str() );
95 gtk_widget_show( accel_label );
9e691f46 96
354aa1e3
RR
97 m_widget = gtk_button_new();
98 gtk_container_add( GTK_CONTAINER(m_widget), accel_label );
9e691f46 99
354aa1e3 100 gtk_accel_label_set_accel_widget( GTK_ACCEL_LABEL(accel_label), m_widget );
9e691f46 101
354aa1e3
RR
102 guint accel_key = gtk_label_parse_uline (GTK_LABEL(accel_label), label2.mb_str() );
103 gtk_accel_label_refetch( GTK_ACCEL_LABEL(accel_label) );
9e691f46 104
354aa1e3
RR
105 wxControl::SetLabel( label );
106*/
9e691f46 107
354aa1e3
RR
108 m_widget = gtk_button_new_with_label("");
109
2e8613b7
RR
110 float x_alignment = 0.5;
111 if (HasFlag(wxBU_LEFT))
112 x_alignment = 0.0;
113 else if (HasFlag(wxBU_RIGHT))
114 x_alignment = 1.0;
115
116 float y_alignment = 0.5;
117 if (HasFlag(wxBU_TOP))
118 y_alignment = 0.0;
119 else if (HasFlag(wxBU_BOTTOM))
120 y_alignment = 1.0;
121
122 gtk_misc_set_alignment (GTK_MISC (BUTTON_CHILD (m_widget)),
a696db45
VZ
123 x_alignment, y_alignment);
124
354aa1e3
RR
125 SetLabel( label );
126
de1c750f
RR
127 if (style & wxNO_BORDER)
128 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
de1c750f 129
b292e2f5
RR
130 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
131 GTK_SIGNAL_FUNC(gtk_button_clicked_callback), (gpointer*)this );
c801d85f 132
f03fc89f 133 m_parent->DoAddChild( this );
9e691f46 134
b292e2f5 135 PostCreation();
9e691f46 136
db434467
RR
137 SetFont( parent->GetFont() );
138
139 wxSize best_size( DoGetBestSize() );
140 wxSize new_size( size );
141 if (new_size.x == -1)
142 new_size.x = best_size.x;
143 if (new_size.y == -1)
144 new_size.y = best_size.y;
145 if ((new_size.x != size.x) || (new_size.y != size.y))
146 SetSize( new_size.x, new_size.y );
147
148 SetSize( new_size );
149
b292e2f5
RR
150 SetBackgroundColour( parent->GetBackgroundColour() );
151 SetForegroundColour( parent->GetForegroundColour() );
32c77a71 152
b292e2f5 153 Show( TRUE );
32c77a71 154
b292e2f5 155 return TRUE;
6de97a3b 156}
32c77a71 157
da048e3d 158void wxButton::SetDefault()
c801d85f 159{
3502e687
RR
160 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
161 gtk_widget_grab_default( m_widget );
9e691f46 162
3502e687 163 SetSize( m_x, m_y, m_width, m_height );
6de97a3b 164}
c801d85f 165
ebea0891
KB
166/* static */
167wxSize wxButton::GetDefaultSize()
8dbf4589
RR
168{
169 return wxSize(80,26);
170}
171
c801d85f
KB
172void wxButton::SetLabel( const wxString &label )
173{
223d09f6 174 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
9e691f46 175
b292e2f5 176 wxControl::SetLabel( label );
9e691f46 177
fab591c5 178 gtk_label_set( GTK_LABEL( BUTTON_CHILD(m_widget) ), wxGTK_CONV( GetLabel() ) );
6de97a3b 179}
c801d85f 180
f03fc89f 181bool wxButton::Enable( bool enable )
a9c96bcc 182{
f03fc89f
VZ
183 if ( !wxControl::Enable( enable ) )
184 return FALSE;
9e691f46
VZ
185
186 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget), enable );
f03fc89f
VZ
187
188 return TRUE;
a9c96bcc
RR
189}
190
2b5f62a0
VZ
191bool wxButton::IsOwnGtkWindow( GdkWindow *window )
192{
193#ifdef __WXGTK20__
194 return GTK_BUTTON(m_widget)->event_window;
195#else
196 return (window == m_widget->window);
197#endif
198}
199
58614078 200void wxButton::ApplyWidgetStyle()
868a2826 201{
b292e2f5
RR
202 SetWidgetStyle();
203 gtk_widget_set_style( m_widget, m_widgetStyle );
9e691f46 204 gtk_widget_set_style( BUTTON_CHILD(m_widget), m_widgetStyle );
a81258be 205}
db434467
RR
206
207wxSize wxButton::DoGetBestSize() const
208{
209 wxSize ret( wxControl::DoGetBestSize() );
9e691f46 210
8ab696e0
RR
211 if (!HasFlag(wxBU_EXACTFIT))
212 {
213 if (ret.x < 80) ret.x = 80;
214 }
9e691f46 215
db434467
RR
216 return ret;
217}
218
1e6feb95
VZ
219#endif // wxUSE_BUTTON
220