]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/button.cpp
Forward declare wxMacListControl
[wxWidgets.git] / src / gtk1 / button.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/button.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_BUTTON
14
15 #include "wx/button.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/toplevel.h"
19 #endif
20
21 #include "wx/stockitem.h"
22
23 #include "wx/gtk1/private.h"
24 #include "wx/gtk1/win_gtk.h"
25
26 //-----------------------------------------------------------------------------
27 // classes
28 //-----------------------------------------------------------------------------
29
30 class wxButton;
31
32 //-----------------------------------------------------------------------------
33 // idle system
34 //-----------------------------------------------------------------------------
35
36 extern void wxapp_install_idle_handler();
37 extern bool g_isIdle;
38
39 //-----------------------------------------------------------------------------
40 // data
41 //-----------------------------------------------------------------------------
42
43 extern bool g_blockEventsOnDrag;
44
45 //-----------------------------------------------------------------------------
46 // "clicked"
47 //-----------------------------------------------------------------------------
48
49 extern "C" {
50 static void gtk_button_clicked_callback( GtkWidget *WXUNUSED(widget), wxButton *button )
51 {
52 if (g_isIdle)
53 wxapp_install_idle_handler();
54
55 if (!button->m_hasVMT) return;
56 if (g_blockEventsOnDrag) return;
57
58 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
59 event.SetEventObject(button);
60 button->GetEventHandler()->ProcessEvent(event);
61 }
62 }
63
64 //-----------------------------------------------------------------------------
65 // "style_set" from m_widget
66 //-----------------------------------------------------------------------------
67
68 static gint
69 gtk_button_style_set_callback( GtkWidget *m_widget, GtkStyle *WXUNUSED(style), wxButton *win )
70 {
71 if (g_isIdle)
72 wxapp_install_idle_handler();
73
74 int left_border = 0;
75 int right_border = 0;
76 int top_border = 0;
77 int bottom_border = 0;
78
79 /* the default button has a border around it */
80 if (GTK_WIDGET_CAN_DEFAULT(m_widget))
81 {
82 left_border = 6;
83 right_border = 6;
84 top_border = 6;
85 bottom_border = 5;
86 win->DoMoveWindow( win->m_x-top_border,
87 win->m_y-left_border,
88 win->m_width+left_border+right_border,
89 win->m_height+top_border+bottom_border );
90 }
91
92 return FALSE;
93 }
94
95 //-----------------------------------------------------------------------------
96 // wxButton
97 //-----------------------------------------------------------------------------
98
99 IMPLEMENT_DYNAMIC_CLASS(wxButton,wxControl)
100
101 wxButton::wxButton()
102 {
103 }
104
105 wxButton::~wxButton()
106 {
107 }
108
109 bool wxButton::Create( wxWindow *parent, wxWindowID id, const wxString &label,
110 const wxPoint &pos, const wxSize &size,
111 long style, const wxValidator& validator, const wxString &name )
112 {
113 m_needParent = true;
114 m_acceptsFocus = true;
115
116 if (!PreCreation( parent, pos, size ) ||
117 !CreateBase( parent, id, pos, size, style, validator, name ))
118 {
119 wxFAIL_MSG( wxT("wxButton creation failed") );
120 return false;
121 }
122
123 m_widget = gtk_button_new_with_label("");
124
125 float x_alignment = 0.5;
126 if (HasFlag(wxBU_LEFT))
127 x_alignment = 0.0;
128 else if (HasFlag(wxBU_RIGHT))
129 x_alignment = 1.0;
130
131 float y_alignment = 0.5;
132 if (HasFlag(wxBU_TOP))
133 y_alignment = 0.0;
134 else if (HasFlag(wxBU_BOTTOM))
135 y_alignment = 1.0;
136
137 if (GTK_IS_MISC(BUTTON_CHILD(m_widget)))
138 gtk_misc_set_alignment (GTK_MISC (BUTTON_CHILD (m_widget)),
139 x_alignment, y_alignment);
140
141 SetLabel(label);
142
143 if (style & wxNO_BORDER)
144 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
145
146 gtk_signal_connect_after( GTK_OBJECT(m_widget), "clicked",
147 GTK_SIGNAL_FUNC(gtk_button_clicked_callback), (gpointer*)this );
148
149 gtk_signal_connect_after( GTK_OBJECT(m_widget), "style_set",
150 GTK_SIGNAL_FUNC(gtk_button_style_set_callback), (gpointer*) this );
151
152 m_parent->DoAddChild( this );
153
154 PostCreation(size);
155
156 return true;
157 }
158
159
160 void wxButton::SetDefault()
161 {
162 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
163 wxCHECK_RET( tlw, _T("button without top level window?") );
164
165 tlw->SetDefaultItem(this);
166
167 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
168 gtk_widget_grab_default( m_widget );
169
170 // resize for default border
171 gtk_button_style_set_callback( m_widget, NULL, this );
172 }
173
174 /* static */
175 wxSize wxButtonBase::GetDefaultSize()
176 {
177 return wxSize(80,26);
178 }
179
180 void wxButton::SetLabel( const wxString &lbl )
181 {
182 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
183
184 wxString label(lbl);
185
186 if (label.empty() && wxIsStockID(m_windowId))
187 label = wxGetStockLabel(m_windowId);
188
189 wxControl::SetLabel(label);
190
191 const wxString labelGTK = GTKRemoveMnemonics(label);
192
193 gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget)), wxGTK_CONV(labelGTK));
194 }
195
196 bool wxButton::Enable( bool enable )
197 {
198 if ( !wxControl::Enable( enable ) )
199 return false;
200
201 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget), enable );
202
203 return true;
204 }
205
206 bool wxButton::IsOwnGtkWindow( GdkWindow *window )
207 {
208 return (window == m_widget->window);
209 }
210
211 void wxButton::DoApplyWidgetStyle(GtkRcStyle *style)
212 {
213 gtk_widget_modify_style(m_widget, style);
214 gtk_widget_modify_style(BUTTON_CHILD(m_widget), style);
215 }
216
217 wxSize wxButton::DoGetBestSize() const
218 {
219 // the default button in wxGTK is bigger than the other ones because of an
220 // extra border around it, but we don't want to take it into account in
221 // our size calculations (otherwsie the result is visually ugly), so
222 // always return the size of non default button from here
223 const bool isDefault = GTK_WIDGET_HAS_DEFAULT(m_widget);
224 if ( isDefault )
225 {
226 // temporarily unset default flag
227 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_DEFAULT );
228 }
229
230 wxSize ret( wxControl::DoGetBestSize() );
231
232 if ( isDefault )
233 {
234 // set it back again
235 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
236 }
237
238 ret.x += 10; // add a few pixels for sloppy (but common) themes
239
240 if (!HasFlag(wxBU_EXACTFIT))
241 {
242 wxSize defaultSize = GetDefaultSize();
243 if (ret.x < defaultSize.x) ret.x = defaultSize.x;
244 if (ret.y < defaultSize.y) ret.y = defaultSize.y;
245 }
246
247 CacheBestSize(ret);
248 return ret;
249 }
250
251 // static
252 wxVisualAttributes
253 wxButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
254 {
255 return GetDefaultAttributesFromGTKWidget(gtk_button_new);
256 }
257
258 #endif // wxUSE_BUTTON