]>
Commit | Line | Data |
---|---|---|
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 | #include "wx/stockitem.h" | |
17 | ||
18 | #include "wx/gtk1/private.h" | |
19 | #include "wx/gtk1/win_gtk.h" | |
20 | ||
21 | //----------------------------------------------------------------------------- | |
22 | // classes | |
23 | //----------------------------------------------------------------------------- | |
24 | ||
25 | class wxButton; | |
26 | ||
27 | //----------------------------------------------------------------------------- | |
28 | // idle system | |
29 | //----------------------------------------------------------------------------- | |
30 | ||
31 | extern void wxapp_install_idle_handler(); | |
32 | extern bool g_isIdle; | |
33 | ||
34 | //----------------------------------------------------------------------------- | |
35 | // data | |
36 | //----------------------------------------------------------------------------- | |
37 | ||
38 | extern bool g_blockEventsOnDrag; | |
39 | ||
40 | //----------------------------------------------------------------------------- | |
41 | // "clicked" | |
42 | //----------------------------------------------------------------------------- | |
43 | ||
44 | extern "C" { | |
45 | static void gtk_button_clicked_callback( GtkWidget *WXUNUSED(widget), wxButton *button ) | |
46 | { | |
47 | if (g_isIdle) | |
48 | wxapp_install_idle_handler(); | |
49 | ||
50 | if (!button->m_hasVMT) return; | |
51 | if (g_blockEventsOnDrag) return; | |
52 | ||
53 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId()); | |
54 | event.SetEventObject(button); | |
55 | button->GetEventHandler()->ProcessEvent(event); | |
56 | } | |
57 | } | |
58 | ||
59 | //----------------------------------------------------------------------------- | |
60 | // "style_set" from m_widget | |
61 | //----------------------------------------------------------------------------- | |
62 | ||
63 | static gint | |
64 | gtk_button_style_set_callback( GtkWidget *m_widget, GtkStyle *WXUNUSED(style), wxButton *win ) | |
65 | { | |
66 | if (g_isIdle) | |
67 | wxapp_install_idle_handler(); | |
68 | ||
69 | int left_border = 0; | |
70 | int right_border = 0; | |
71 | int top_border = 0; | |
72 | int bottom_border = 0; | |
73 | ||
74 | /* the default button has a border around it */ | |
75 | if (GTK_WIDGET_CAN_DEFAULT(m_widget)) | |
76 | { | |
77 | left_border = 6; | |
78 | right_border = 6; | |
79 | top_border = 6; | |
80 | bottom_border = 5; | |
81 | win->DoMoveWindow( win->m_x-top_border, | |
82 | win->m_y-left_border, | |
83 | win->m_width+left_border+right_border, | |
84 | win->m_height+top_border+bottom_border ); | |
85 | } | |
86 | ||
87 | return FALSE; | |
88 | } | |
89 | ||
90 | //----------------------------------------------------------------------------- | |
91 | // wxButton | |
92 | //----------------------------------------------------------------------------- | |
93 | ||
94 | IMPLEMENT_DYNAMIC_CLASS(wxButton,wxControl) | |
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 | void wxButton::SetDefault() | |
156 | { | |
157 | wxWindow *parent = GetParent(); | |
158 | wxCHECK_RET( parent, _T("button without parent?") ); | |
159 | ||
160 | parent->SetDefaultItem(this); | |
161 | ||
162 | GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT ); | |
163 | gtk_widget_grab_default( m_widget ); | |
164 | ||
165 | // resize for default border | |
166 | gtk_button_style_set_callback( m_widget, NULL, this ); | |
167 | } | |
168 | ||
169 | /* static */ | |
170 | wxSize wxButtonBase::GetDefaultSize() | |
171 | { | |
172 | return wxSize(80,26); | |
173 | } | |
174 | ||
175 | void wxButton::SetLabel( const wxString &lbl ) | |
176 | { | |
177 | wxCHECK_RET( m_widget != NULL, wxT("invalid button") ); | |
178 | ||
179 | wxString label(lbl); | |
180 | ||
181 | if (label.empty() && wxIsStockID(m_windowId)) | |
182 | label = wxGetStockLabel(m_windowId); | |
183 | ||
184 | wxControl::SetLabel(label); | |
185 | ||
186 | const wxString labelGTK = GTKRemoveMnemonics(label); | |
187 | ||
188 | gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget)), wxGTK_CONV(labelGTK)); | |
189 | } | |
190 | ||
191 | bool wxButton::Enable( bool enable ) | |
192 | { | |
193 | if ( !wxControl::Enable( enable ) ) | |
194 | return false; | |
195 | ||
196 | gtk_widget_set_sensitive( BUTTON_CHILD(m_widget), enable ); | |
197 | ||
198 | return true; | |
199 | } | |
200 | ||
201 | bool wxButton::IsOwnGtkWindow( GdkWindow *window ) | |
202 | { | |
203 | return (window == m_widget->window); | |
204 | } | |
205 | ||
206 | void wxButton::DoApplyWidgetStyle(GtkRcStyle *style) | |
207 | { | |
208 | gtk_widget_modify_style(m_widget, style); | |
209 | gtk_widget_modify_style(BUTTON_CHILD(m_widget), style); | |
210 | } | |
211 | ||
212 | wxSize wxButton::DoGetBestSize() const | |
213 | { | |
214 | // the default button in wxGTK is bigger than the other ones because of an | |
215 | // extra border around it, but we don't want to take it into account in | |
216 | // our size calculations (otherwsie the result is visually ugly), so | |
217 | // always return the size of non default button from here | |
218 | const bool isDefault = GTK_WIDGET_HAS_DEFAULT(m_widget); | |
219 | if ( isDefault ) | |
220 | { | |
221 | // temporarily unset default flag | |
222 | GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_DEFAULT ); | |
223 | } | |
224 | ||
225 | wxSize ret( wxControl::DoGetBestSize() ); | |
226 | ||
227 | if ( isDefault ) | |
228 | { | |
229 | // set it back again | |
230 | GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT ); | |
231 | } | |
232 | ||
233 | ret.x += 10; // add a few pixels for sloppy (but common) themes | |
234 | ||
235 | if (!HasFlag(wxBU_EXACTFIT)) | |
236 | { | |
237 | wxSize defaultSize = GetDefaultSize(); | |
238 | if (ret.x < defaultSize.x) ret.x = defaultSize.x; | |
239 | if (ret.y < defaultSize.y) ret.y = defaultSize.y; | |
240 | } | |
241 | ||
242 | CacheBestSize(ret); | |
243 | return ret; | |
244 | } | |
245 | ||
246 | // static | |
247 | wxVisualAttributes | |
248 | wxButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
249 | { | |
250 | return GetDefaultAttributesFromGTKWidget(gtk_button_new); | |
251 | } | |
252 | ||
253 | #endif // wxUSE_BUTTON |