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