]>
Commit | Line | Data |
---|---|---|
1db8dc4a | 1 | ///////////////////////////////////////////////////////////////////////////// |
f1e01716 | 2 | // Name: src/gtk/tglbtn.cpp |
1db8dc4a VZ |
3 | // Purpose: Definition of the wxToggleButton class, which implements a |
4 | // toggle button under wxGTK. | |
5 | // Author: John Norris, minor changes by Axel Schlueter | |
6 | // Modified by: | |
7 | // Created: 08.02.01 | |
8 | // RCS-ID: $Id$ | |
9 | // Copyright: (c) 2000 Johnny C. Norris II | |
706fb893 | 10 | // License: wxWindows licence |
1db8dc4a VZ |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
14f355c2 VS |
13 | // For compilers that support precompilation, includes "wx.h". |
14 | #include "wx/wxprec.h" | |
15 | ||
f1e01716 WS |
16 | #if wxUSE_TOGGLEBTN |
17 | ||
1db8dc4a VZ |
18 | #include "wx/tglbtn.h" |
19 | ||
f1e01716 WS |
20 | #ifndef WX_PRECOMP |
21 | #include "wx/button.h" | |
22 | #endif | |
1db8dc4a | 23 | |
9e691f46 | 24 | #include "wx/gtk/private.h" |
1db8dc4a | 25 | |
1db8dc4a | 26 | extern bool g_blockEventsOnDrag; |
1db8dc4a | 27 | |
865bb325 | 28 | extern "C" { |
9864c56d | 29 | static void gtk_togglebutton_clicked_callback(GtkWidget *WXUNUSED(widget), wxToggleButton *cb) |
1db8dc4a | 30 | { |
91af0895 WS |
31 | if (!cb->m_hasVMT || g_blockEventsOnDrag) |
32 | return; | |
33 | ||
34 | if (cb->m_blockEvent) return; | |
35 | ||
36 | // Generate a wx event. | |
37 | wxCommandEvent event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, cb->GetId()); | |
38 | event.SetInt(cb->GetValue()); | |
39 | event.SetEventObject(cb); | |
937013e0 | 40 | cb->HandleWindowEvent(event); |
1db8dc4a | 41 | } |
865bb325 | 42 | } |
1db8dc4a | 43 | |
1db8dc4a VZ |
44 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED) |
45 | ||
4f856067 | 46 | // ------------------------------------------------------------------------ |
10ff9c61 | 47 | // wxBitmapToggleButton |
4f856067 RR |
48 | // ------------------------------------------------------------------------ |
49 | ||
10ff9c61 | 50 | IMPLEMENT_DYNAMIC_CLASS(wxBitmapToggleButton, wxControl) |
4f856067 | 51 | |
10ff9c61 | 52 | bool wxBitmapToggleButton::Create(wxWindow *parent, wxWindowID id, |
4f856067 RR |
53 | const wxBitmap &label, const wxPoint &pos, |
54 | const wxSize &size, long style, | |
55 | const wxValidator& validator, | |
56 | const wxString &name) | |
57 | { | |
91af0895 | 58 | m_blockEvent = false; |
4f856067 RR |
59 | |
60 | if (!PreCreation(parent, pos, size) || | |
61 | !CreateBase(parent, id, pos, size, style, validator, name )) | |
62 | { | |
10ff9c61 | 63 | wxFAIL_MSG(wxT("wxBitmapToggleButton creation failed")); |
91af0895 | 64 | return false; |
4f856067 | 65 | } |
91af0895 | 66 | |
4f856067 RR |
67 | // Create the gtk widget. |
68 | m_widget = gtk_toggle_button_new(); | |
69 | ||
70 | if (style & wxNO_BORDER) | |
e338053c | 71 | gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE ); |
4f856067 | 72 | |
e338053c PC |
73 | m_bitmap = label; |
74 | OnSetBitmap(); | |
4f856067 | 75 | |
9fa72bd2 MR |
76 | g_signal_connect (m_widget, "clicked", |
77 | G_CALLBACK (gtk_togglebutton_clicked_callback), | |
78 | this); | |
4f856067 RR |
79 | |
80 | m_parent->DoAddChild(this); | |
81 | ||
abdeb9e7 | 82 | PostCreation(size); |
4f856067 | 83 | |
91af0895 | 84 | return true; |
4f856067 RR |
85 | } |
86 | ||
87 | // void SetValue(bool state) | |
88 | // Set the value of the toggle button. | |
10ff9c61 | 89 | void wxBitmapToggleButton::SetValue(bool state) |
4f856067 | 90 | { |
91af0895 | 91 | wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button")); |
4f856067 | 92 | |
91af0895 WS |
93 | if (state == GetValue()) |
94 | return; | |
4f856067 | 95 | |
91af0895 | 96 | m_blockEvent = true; |
4f856067 | 97 | |
91af0895 | 98 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget), state); |
4f856067 | 99 | |
91af0895 | 100 | m_blockEvent = false; |
4f856067 RR |
101 | } |
102 | ||
103 | // bool GetValue() const | |
104 | // Get the value of the toggle button. | |
10ff9c61 | 105 | bool wxBitmapToggleButton::GetValue() const |
4f856067 | 106 | { |
91af0895 | 107 | wxCHECK_MSG(m_widget != NULL, false, wxT("invalid toggle button")); |
4f856067 | 108 | |
e338053c | 109 | return gtk_toggle_button_get_active((GtkToggleButton*)m_widget); |
4f856067 RR |
110 | } |
111 | ||
10ff9c61 | 112 | void wxBitmapToggleButton::SetLabel(const wxBitmap& label) |
4f856067 RR |
113 | { |
114 | wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button")); | |
115 | ||
116 | m_bitmap = label; | |
9f884528 | 117 | InvalidateBestSize(); |
91af0895 | 118 | |
4f856067 RR |
119 | OnSetBitmap(); |
120 | } | |
121 | ||
10ff9c61 | 122 | void wxBitmapToggleButton::OnSetBitmap() |
4f856067 RR |
123 | { |
124 | if (!m_bitmap.Ok()) return; | |
125 | ||
e338053c PC |
126 | GtkWidget* image = ((GtkBin*)m_widget)->child; |
127 | if (image == NULL) | |
4f856067 | 128 | { |
4a4a02ac | 129 | image = gtk_image_new(); |
e338053c PC |
130 | gtk_widget_show(image); |
131 | gtk_container_add((GtkContainer*)m_widget, image); | |
4f856067 | 132 | } |
4a4a02ac PC |
133 | // always use pixbuf, because pixmap mask does not |
134 | // work with disabled images in some themes | |
135 | gtk_image_set_from_pixbuf((GtkImage*)image, m_bitmap.GetPixbuf()); | |
4f856067 RR |
136 | } |
137 | ||
10ff9c61 | 138 | bool wxBitmapToggleButton::Enable(bool enable /*=true*/) |
4f856067 RR |
139 | { |
140 | if (!wxControl::Enable(enable)) | |
91af0895 | 141 | return false; |
4f856067 | 142 | |
afa7bd1e | 143 | gtk_widget_set_sensitive(GTK_BIN(m_widget)->child, enable); |
4f856067 | 144 | |
91af0895 | 145 | return true; |
4f856067 RR |
146 | } |
147 | ||
10ff9c61 | 148 | void wxBitmapToggleButton::DoApplyWidgetStyle(GtkRcStyle *style) |
4f856067 | 149 | { |
f40fdaa3 | 150 | gtk_widget_modify_style(m_widget, style); |
afa7bd1e | 151 | gtk_widget_modify_style(GTK_BIN(m_widget)->child, style); |
4f856067 RR |
152 | } |
153 | ||
ef5c70f9 | 154 | GdkWindow * |
10ff9c61 | 155 | wxBitmapToggleButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const |
4f856067 | 156 | { |
ef5c70f9 | 157 | return GTK_BUTTON(m_widget)->event_window; |
4f856067 RR |
158 | } |
159 | ||
4f856067 | 160 | // Get the "best" size for this control. |
10ff9c61 | 161 | wxSize wxBitmapToggleButton::DoGetBestSize() const |
4f856067 | 162 | { |
abdeb9e7 | 163 | wxSize best; |
91af0895 | 164 | |
abdeb9e7 | 165 | if (m_bitmap.Ok()) |
4f856067 | 166 | { |
abdeb9e7 RD |
167 | int border = HasFlag(wxNO_BORDER) ? 4 : 10; |
168 | best.x = m_bitmap.GetWidth()+border; | |
169 | best.y = m_bitmap.GetHeight()+border; | |
4f856067 | 170 | } |
9f884528 | 171 | CacheBestSize(best); |
abdeb9e7 | 172 | return best; |
4f856067 | 173 | } |
9d522606 RD |
174 | |
175 | ||
176 | // static | |
177 | wxVisualAttributes | |
10ff9c61 | 178 | wxBitmapToggleButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) |
9d522606 RD |
179 | { |
180 | return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new); | |
181 | } | |
182 | ||
183 | ||
4f856067 RR |
184 | // ------------------------------------------------------------------------ |
185 | // wxToggleButton | |
186 | // ------------------------------------------------------------------------ | |
187 | ||
188 | IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl) | |
189 | ||
1db8dc4a VZ |
190 | bool wxToggleButton::Create(wxWindow *parent, wxWindowID id, |
191 | const wxString &label, const wxPoint &pos, | |
192 | const wxSize &size, long style, | |
193 | const wxValidator& validator, | |
194 | const wxString &name) | |
195 | { | |
91af0895 | 196 | m_blockEvent = false; |
1db8dc4a | 197 | |
91af0895 | 198 | if (!PreCreation(parent, pos, size) || |
e338053c PC |
199 | !CreateBase(parent, id, pos, size, style, validator, name )) |
200 | { | |
91af0895 WS |
201 | wxFAIL_MSG(wxT("wxToggleButton creation failed")); |
202 | return false; | |
203 | } | |
1db8dc4a | 204 | |
91af0895 | 205 | // Create the gtk widget. |
ecf3b4a5 RR |
206 | m_widget = gtk_toggle_button_new_with_mnemonic(""); |
207 | ||
208 | SetLabel(label); | |
1db8dc4a | 209 | |
9fa72bd2 MR |
210 | g_signal_connect (m_widget, "clicked", |
211 | G_CALLBACK (gtk_togglebutton_clicked_callback), | |
212 | this); | |
1db8dc4a | 213 | |
91af0895 | 214 | m_parent->DoAddChild(this); |
1db8dc4a | 215 | |
91af0895 WS |
216 | PostCreation(size); |
217 | ||
218 | return true; | |
1db8dc4a VZ |
219 | } |
220 | ||
221 | // void SetValue(bool state) | |
222 | // Set the value of the toggle button. | |
223 | void wxToggleButton::SetValue(bool state) | |
224 | { | |
91af0895 | 225 | wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button")); |
1db8dc4a | 226 | |
91af0895 WS |
227 | if (state == GetValue()) |
228 | return; | |
1db8dc4a | 229 | |
91af0895 | 230 | m_blockEvent = true; |
1db8dc4a | 231 | |
91af0895 | 232 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget), state); |
1db8dc4a | 233 | |
91af0895 | 234 | m_blockEvent = false; |
1db8dc4a VZ |
235 | } |
236 | ||
237 | // bool GetValue() const | |
238 | // Get the value of the toggle button. | |
239 | bool wxToggleButton::GetValue() const | |
240 | { | |
91af0895 | 241 | wxCHECK_MSG(m_widget != NULL, false, wxT("invalid toggle button")); |
1db8dc4a | 242 | |
91af0895 | 243 | return GTK_TOGGLE_BUTTON(m_widget)->active; |
1db8dc4a VZ |
244 | } |
245 | ||
1db8dc4a VZ |
246 | void wxToggleButton::SetLabel(const wxString& label) |
247 | { | |
8ab696e0 | 248 | wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button")); |
1db8dc4a | 249 | |
8ab696e0 | 250 | wxControl::SetLabel(label); |
1db8dc4a | 251 | |
ecf3b4a5 RR |
252 | const wxString labelGTK = GTKConvertMnemonics(label); |
253 | ||
254 | gtk_button_set_label(GTK_BUTTON(m_widget), wxGTK_CONV(labelGTK)); | |
255 | ||
256 | ApplyWidgetStyle( false ); | |
1db8dc4a VZ |
257 | } |
258 | ||
91af0895 | 259 | bool wxToggleButton::Enable(bool enable /*=true*/) |
1db8dc4a | 260 | { |
8ab696e0 | 261 | if (!wxControl::Enable(enable)) |
91af0895 | 262 | return false; |
1db8dc4a | 263 | |
afa7bd1e | 264 | gtk_widget_set_sensitive(GTK_BIN(m_widget)->child, enable); |
1db8dc4a | 265 | |
91af0895 | 266 | return true; |
1db8dc4a VZ |
267 | } |
268 | ||
f40fdaa3 | 269 | void wxToggleButton::DoApplyWidgetStyle(GtkRcStyle *style) |
1db8dc4a | 270 | { |
f40fdaa3 | 271 | gtk_widget_modify_style(m_widget, style); |
afa7bd1e | 272 | gtk_widget_modify_style(GTK_BIN(m_widget)->child, style); |
1db8dc4a VZ |
273 | } |
274 | ||
ef5c70f9 VZ |
275 | GdkWindow * |
276 | wxToggleButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const | |
1db8dc4a | 277 | { |
ef5c70f9 | 278 | return GTK_BUTTON(m_widget)->event_window; |
1db8dc4a VZ |
279 | } |
280 | ||
1db8dc4a VZ |
281 | // Get the "best" size for this control. |
282 | wxSize wxToggleButton::DoGetBestSize() const | |
283 | { | |
8ab696e0 | 284 | wxSize ret(wxControl::DoGetBestSize()); |
91af0895 | 285 | |
8ab696e0 RR |
286 | if (!HasFlag(wxBU_EXACTFIT)) |
287 | { | |
288 | if (ret.x < 80) ret.x = 80; | |
289 | } | |
91af0895 | 290 | |
9f884528 RD |
291 | CacheBestSize(ret); |
292 | return ret; | |
1db8dc4a VZ |
293 | } |
294 | ||
9d522606 RD |
295 | // static |
296 | wxVisualAttributes | |
297 | wxToggleButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
298 | { | |
299 | return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new); | |
300 | } | |
301 | ||
1db8dc4a | 302 | #endif // wxUSE_TOGGLEBTN |