]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/button.cpp
be good citizen, don't force window position; instead, set transiency relation so...
[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 #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
29 class wxButton;
30
31 //-----------------------------------------------------------------------------
32 // idle system
33 //-----------------------------------------------------------------------------
34
35 extern void wxapp_install_idle_handler();
36 extern bool g_isIdle;
37
38 //-----------------------------------------------------------------------------
39 // data
40 //-----------------------------------------------------------------------------
41
42 extern bool g_blockEventsOnDrag;
43
44 //-----------------------------------------------------------------------------
45 // "clicked"
46 //-----------------------------------------------------------------------------
47
48 static 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
65 IMPLEMENT_DYNAMIC_CLASS(wxButton,wxControl)
66
67 wxButton::wxButton()
68 {
69 }
70
71 wxButton::~wxButton()
72 {
73 }
74
75 bool 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 #ifdef __WXGTK20__
112 m_widget = gtk_button_new_with_mnemonic("");
113 #else
114 m_widget = gtk_button_new_with_label("");
115 #endif
116
117 float x_alignment = 0.5;
118 if (HasFlag(wxBU_LEFT))
119 x_alignment = 0.0;
120 else if (HasFlag(wxBU_RIGHT))
121 x_alignment = 1.0;
122
123 float y_alignment = 0.5;
124 if (HasFlag(wxBU_TOP))
125 y_alignment = 0.0;
126 else if (HasFlag(wxBU_BOTTOM))
127 y_alignment = 1.0;
128
129 gtk_misc_set_alignment (GTK_MISC (BUTTON_CHILD (m_widget)),
130 x_alignment, y_alignment);
131
132 SetLabel( label );
133
134 if (style & wxNO_BORDER)
135 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
136
137 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
138 GTK_SIGNAL_FUNC(gtk_button_clicked_callback), (gpointer*)this );
139
140 m_parent->DoAddChild( this );
141
142 PostCreation(size);
143
144 return TRUE;
145 }
146
147 void wxButton::SetDefault()
148 {
149 wxWindow *parent = GetParent();
150 wxCHECK_RET( parent, _T("button without parent?") );
151
152 parent->SetDefaultItem(this);
153
154 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
155 gtk_widget_grab_default( m_widget );
156
157 SetSize( m_x, m_y, m_width, m_height );
158 }
159
160 /* static */
161 wxSize wxButton::GetDefaultSize()
162 {
163 return wxSize(80,26);
164 }
165
166 void wxButton::SetLabel( const wxString &label )
167 {
168 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
169
170 wxControl::SetLabel( label );
171
172 #ifdef __WXGTK20__
173 wxString label2 = PrepareLabelMnemonics( label );
174 gtk_button_set_label( GTK_BUTTON(m_widget), wxGTK_CONV(label2) );
175 #else
176 gtk_label_set( GTK_LABEL( BUTTON_CHILD(m_widget) ), wxGTK_CONV( GetLabel() ) );
177 #endif
178 }
179
180 bool wxButton::Enable( bool enable )
181 {
182 if ( !wxControl::Enable( enable ) )
183 return FALSE;
184
185 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget), enable );
186
187 return TRUE;
188 }
189
190 bool wxButton::IsOwnGtkWindow( GdkWindow *window )
191 {
192 #ifdef __WXGTK20__
193 return GTK_BUTTON(m_widget)->event_window;
194 #else
195 return (window == m_widget->window);
196 #endif
197 }
198
199 void wxButton::ApplyWidgetStyle()
200 {
201 SetWidgetStyle();
202 gtk_widget_set_style( m_widget, m_widgetStyle );
203 gtk_widget_set_style( BUTTON_CHILD(m_widget), m_widgetStyle );
204 }
205
206 wxSize wxButton::DoGetBestSize() const
207 {
208 // the default button in wxGTK is bigger than the other ones because of an
209 // extra border around it, but we don't want to take it into account in
210 // our size calculations (otherwsie the result is visually ugly), so
211 // always return the size of non default button from here
212 const bool isDefault = GTK_WIDGET_HAS_DEFAULT(m_widget);
213 if ( isDefault )
214 {
215 // temporarily unset default flag
216 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_DEFAULT );
217 }
218
219 wxSize ret( wxControl::DoGetBestSize() );
220
221 if ( isDefault )
222 {
223 // set it back again
224 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
225 }
226
227 #ifndef __WXGTK20__
228 ret.x += 10; // add a few pixels for sloppy (but common) themes
229 #endif
230
231 if (!HasFlag(wxBU_EXACTFIT))
232 {
233 if (ret.x < 80) ret.x = 80;
234 }
235
236 return ret;
237 }
238
239 // static
240 wxVisualAttributes
241 wxButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
242 {
243 return GetDefaultAttributesFromGTKWidget(gtk_button_new);
244 }
245
246 #endif // wxUSE_BUTTON
247