]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: button.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
dbf858b5 | 5 | // Id: $Id$ |
01111366 | 6 | // Copyright: (c) 1998 Robert Roebling |
65571936 | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
14f355c2 | 10 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
c801d85f KB |
11 | #pragma implementation "button.h" |
12 | #endif | |
13 | ||
14f355c2 VS |
14 | // For compilers that support precompilation, includes "wx.h". |
15 | #include "wx/wxprec.h" | |
16 | ||
1e6feb95 VZ |
17 | #include "wx/defs.h" |
18 | ||
19 | #if wxUSE_BUTTON | |
20 | ||
c801d85f KB |
21 | #include "wx/button.h" |
22 | ||
9e691f46 | 23 | #include "wx/gtk/private.h" |
83624f79 | 24 | |
c801d85f KB |
25 | //----------------------------------------------------------------------------- |
26 | // classes | |
27 | //----------------------------------------------------------------------------- | |
28 | ||
29 | class wxButton; | |
30 | ||
acfd422a RR |
31 | //----------------------------------------------------------------------------- |
32 | // idle system | |
33 | //----------------------------------------------------------------------------- | |
34 | ||
35 | extern void wxapp_install_idle_handler(); | |
36 | extern bool g_isIdle; | |
37 | ||
66bd6b93 RR |
38 | //----------------------------------------------------------------------------- |
39 | // data | |
40 | //----------------------------------------------------------------------------- | |
41 | ||
42 | extern bool g_blockEventsOnDrag; | |
43 | ||
c801d85f | 44 | //----------------------------------------------------------------------------- |
e1e955e1 | 45 | // "clicked" |
c801d85f KB |
46 | //----------------------------------------------------------------------------- |
47 | ||
66bd6b93 | 48 | static void gtk_button_clicked_callback( GtkWidget *WXUNUSED(widget), wxButton *button ) |
c801d85f | 49 | { |
9e691f46 | 50 | if (g_isIdle) |
32ac755d | 51 | wxapp_install_idle_handler(); |
acfd422a | 52 | |
a2053b27 | 53 | if (!button->m_hasVMT) return; |
acfd422a | 54 | if (g_blockEventsOnDrag) return; |
9e691f46 | 55 | |
acfd422a RR |
56 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId()); |
57 | event.SetEventObject(button); | |
58 | button->GetEventHandler()->ProcessEvent(event); | |
6de97a3b | 59 | } |
c801d85f KB |
60 | |
61 | //----------------------------------------------------------------------------- | |
e1e955e1 RR |
62 | // wxButton |
63 | //----------------------------------------------------------------------------- | |
64 | ||
65 | IMPLEMENT_DYNAMIC_CLASS(wxButton,wxControl) | |
c801d85f | 66 | |
fd0eed64 | 67 | wxButton::wxButton() |
c801d85f | 68 | { |
6de97a3b | 69 | } |
c801d85f | 70 | |
fd0eed64 RR |
71 | wxButton::~wxButton() |
72 | { | |
fd0eed64 RR |
73 | } |
74 | ||
c801d85f | 75 | bool wxButton::Create( wxWindow *parent, wxWindowID id, const wxString &label, |
32c77a71 | 76 | const wxPoint &pos, const wxSize &size, |
6de97a3b | 77 | long style, const wxValidator& validator, const wxString &name ) |
c801d85f | 78 | { |
b292e2f5 RR |
79 | m_needParent = TRUE; |
80 | m_acceptsFocus = TRUE; | |
32c77a71 | 81 | |
4dcaf11a RR |
82 | if (!PreCreation( parent, pos, size ) || |
83 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
84 | { | |
223d09f6 | 85 | wxFAIL_MSG( wxT("wxButton creation failed") ); |
8ab696e0 | 86 | return FALSE; |
4dcaf11a | 87 | } |
c801d85f | 88 | |
354aa1e3 RR |
89 | /* |
90 | wxString label2( label ); | |
91 | for (size_t i = 0; i < label2.Len(); i++) | |
92 | { | |
93 | if (label2.GetChar(i) == wxT('&')) | |
8ab696e0 | 94 | label2.SetChar(i,wxT('_')); |
354aa1e3 | 95 | } |
9e691f46 | 96 | |
354aa1e3 RR |
97 | GtkWidget *accel_label = gtk_accel_label_new( label2.mb_str() ); |
98 | gtk_widget_show( accel_label ); | |
9e691f46 | 99 | |
354aa1e3 RR |
100 | m_widget = gtk_button_new(); |
101 | gtk_container_add( GTK_CONTAINER(m_widget), accel_label ); | |
9e691f46 | 102 | |
354aa1e3 | 103 | gtk_accel_label_set_accel_widget( GTK_ACCEL_LABEL(accel_label), m_widget ); |
9e691f46 | 104 | |
354aa1e3 RR |
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) ); | |
9e691f46 | 107 | |
354aa1e3 RR |
108 | wxControl::SetLabel( label ); |
109 | */ | |
9e691f46 | 110 | |
b2fcfd94 RD |
111 | #ifdef __WXGTK20__ |
112 | m_widget = gtk_button_new_with_mnemonic(""); | |
113 | #else | |
354aa1e3 | 114 | m_widget = gtk_button_new_with_label(""); |
b2fcfd94 | 115 | #endif |
354aa1e3 | 116 | |
2e8613b7 RR |
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)), | |
a696db45 VZ |
130 | x_alignment, y_alignment); |
131 | ||
354aa1e3 RR |
132 | SetLabel( label ); |
133 | ||
de1c750f RR |
134 | if (style & wxNO_BORDER) |
135 | gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE ); | |
de1c750f | 136 | |
b292e2f5 RR |
137 | gtk_signal_connect( GTK_OBJECT(m_widget), "clicked", |
138 | GTK_SIGNAL_FUNC(gtk_button_clicked_callback), (gpointer*)this ); | |
c801d85f | 139 | |
f03fc89f | 140 | m_parent->DoAddChild( this ); |
9e691f46 | 141 | |
abdeb9e7 | 142 | PostCreation(size); |
db434467 | 143 | |
b292e2f5 | 144 | return TRUE; |
6de97a3b | 145 | } |
32c77a71 | 146 | |
da048e3d | 147 | void wxButton::SetDefault() |
c801d85f | 148 | { |
97149f18 RD |
149 | wxWindow *parent = GetParent(); |
150 | wxCHECK_RET( parent, _T("button without parent?") ); | |
151 | ||
a00b2d0a | 152 | parent->SetDefaultItem(this); |
97149f18 | 153 | |
3502e687 RR |
154 | GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT ); |
155 | gtk_widget_grab_default( m_widget ); | |
9e691f46 | 156 | |
3502e687 | 157 | SetSize( m_x, m_y, m_width, m_height ); |
6de97a3b | 158 | } |
c801d85f | 159 | |
ebea0891 KB |
160 | /* static */ |
161 | wxSize wxButton::GetDefaultSize() | |
8dbf4589 RR |
162 | { |
163 | return wxSize(80,26); | |
164 | } | |
165 | ||
c801d85f KB |
166 | void wxButton::SetLabel( const wxString &label ) |
167 | { | |
223d09f6 | 168 | wxCHECK_RET( m_widget != NULL, wxT("invalid button") ); |
9e691f46 | 169 | |
b292e2f5 | 170 | wxControl::SetLabel( label ); |
9e691f46 | 171 | |
eaafd2f8 VS |
172 | #ifdef __WXGTK20__ |
173 | wxString label2 = PrepareLabelMnemonics( label ); | |
b2fcfd94 | 174 | gtk_button_set_label( GTK_BUTTON(m_widget), wxGTK_CONV(label2) ); |
eaafd2f8 | 175 | #else |
fab591c5 | 176 | gtk_label_set( GTK_LABEL( BUTTON_CHILD(m_widget) ), wxGTK_CONV( GetLabel() ) ); |
eaafd2f8 | 177 | #endif |
6de97a3b | 178 | } |
c801d85f | 179 | |
f03fc89f | 180 | bool wxButton::Enable( bool enable ) |
a9c96bcc | 181 | { |
f03fc89f VZ |
182 | if ( !wxControl::Enable( enable ) ) |
183 | return FALSE; | |
9e691f46 VZ |
184 | |
185 | gtk_widget_set_sensitive( BUTTON_CHILD(m_widget), enable ); | |
f03fc89f VZ |
186 | |
187 | return TRUE; | |
a9c96bcc RR |
188 | } |
189 | ||
2b5f62a0 VZ |
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 | ||
f40fdaa3 | 199 | void wxButton::DoApplyWidgetStyle(GtkRcStyle *style) |
868a2826 | 200 | { |
f40fdaa3 VS |
201 | gtk_widget_modify_style(m_widget, style); |
202 | gtk_widget_modify_style(BUTTON_CHILD(m_widget), style); | |
a81258be | 203 | } |
db434467 RR |
204 | |
205 | wxSize wxButton::DoGetBestSize() const | |
206 | { | |
4f819fe4 VZ |
207 | // the default button in wxGTK is bigger than the other ones because of an |
208 | // extra border around it, but we don't want to take it into account in | |
209 | // our size calculations (otherwsie the result is visually ugly), so | |
210 | // always return the size of non default button from here | |
211 | const bool isDefault = GTK_WIDGET_HAS_DEFAULT(m_widget); | |
212 | if ( isDefault ) | |
213 | { | |
214 | // temporarily unset default flag | |
215 | GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_DEFAULT ); | |
216 | } | |
217 | ||
db434467 | 218 | wxSize ret( wxControl::DoGetBestSize() ); |
9e691f46 | 219 | |
4f819fe4 VZ |
220 | if ( isDefault ) |
221 | { | |
222 | // set it back again | |
223 | GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT ); | |
224 | } | |
225 | ||
391ddf40 RD |
226 | #ifndef __WXGTK20__ |
227 | ret.x += 10; // add a few pixels for sloppy (but common) themes | |
228 | #endif | |
229 | ||
8ab696e0 RR |
230 | if (!HasFlag(wxBU_EXACTFIT)) |
231 | { | |
232 | if (ret.x < 80) ret.x = 80; | |
233 | } | |
9e691f46 | 234 | |
9f884528 | 235 | CacheBestSize(ret); |
db434467 RR |
236 | return ret; |
237 | } | |
238 | ||
9d522606 RD |
239 | // static |
240 | wxVisualAttributes | |
241 | wxButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
242 | { | |
243 | return GetDefaultAttributesFromGTKWidget(gtk_button_new); | |
244 | } | |
245 | ||
1e6feb95 VZ |
246 | #endif // wxUSE_BUTTON |
247 |