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