]>
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 (g_isIdle) |
32 | wxapp_install_idle_handler(); | |
33 | ||
34 | if (!cb->m_hasVMT || g_blockEventsOnDrag) | |
35 | return; | |
36 | ||
37 | if (cb->m_blockEvent) return; | |
38 | ||
39 | // Generate a wx event. | |
40 | wxCommandEvent event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, cb->GetId()); | |
41 | event.SetInt(cb->GetValue()); | |
42 | event.SetEventObject(cb); | |
43 | cb->GetEventHandler()->ProcessEvent(event); | |
1db8dc4a | 44 | } |
865bb325 | 45 | } |
1db8dc4a | 46 | |
1db8dc4a VZ |
47 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED) |
48 | ||
4f856067 RR |
49 | // ------------------------------------------------------------------------ |
50 | // wxToggleBitmapButton | |
51 | // ------------------------------------------------------------------------ | |
52 | ||
53 | IMPLEMENT_DYNAMIC_CLASS(wxToggleBitmapButton, wxControl) | |
54 | ||
55 | bool wxToggleBitmapButton::Create(wxWindow *parent, wxWindowID id, | |
56 | const wxBitmap &label, const wxPoint &pos, | |
57 | const wxSize &size, long style, | |
58 | const wxValidator& validator, | |
59 | const wxString &name) | |
60 | { | |
91af0895 | 61 | m_needParent = true; |
91af0895 WS |
62 | |
63 | m_blockEvent = false; | |
4f856067 RR |
64 | |
65 | if (!PreCreation(parent, pos, size) || | |
66 | !CreateBase(parent, id, pos, size, style, validator, name )) | |
67 | { | |
68 | wxFAIL_MSG(wxT("wxToggleBitmapButton creation failed")); | |
91af0895 | 69 | return false; |
4f856067 | 70 | } |
91af0895 | 71 | |
4f856067 RR |
72 | // Create the gtk widget. |
73 | m_widget = gtk_toggle_button_new(); | |
74 | ||
75 | if (style & wxNO_BORDER) | |
e338053c | 76 | gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE ); |
4f856067 | 77 | |
e338053c PC |
78 | m_bitmap = label; |
79 | OnSetBitmap(); | |
4f856067 | 80 | |
9fa72bd2 MR |
81 | g_signal_connect (m_widget, "clicked", |
82 | G_CALLBACK (gtk_togglebutton_clicked_callback), | |
83 | this); | |
4f856067 RR |
84 | |
85 | m_parent->DoAddChild(this); | |
86 | ||
abdeb9e7 | 87 | PostCreation(size); |
4f856067 | 88 | |
91af0895 | 89 | return true; |
4f856067 RR |
90 | } |
91 | ||
92 | // void SetValue(bool state) | |
93 | // Set the value of the toggle button. | |
94 | void wxToggleBitmapButton::SetValue(bool state) | |
95 | { | |
91af0895 | 96 | wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button")); |
4f856067 | 97 | |
91af0895 WS |
98 | if (state == GetValue()) |
99 | return; | |
4f856067 | 100 | |
91af0895 | 101 | m_blockEvent = true; |
4f856067 | 102 | |
91af0895 | 103 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget), state); |
4f856067 | 104 | |
91af0895 | 105 | m_blockEvent = false; |
4f856067 RR |
106 | } |
107 | ||
108 | // bool GetValue() const | |
109 | // Get the value of the toggle button. | |
110 | bool wxToggleBitmapButton::GetValue() const | |
111 | { | |
91af0895 | 112 | wxCHECK_MSG(m_widget != NULL, false, wxT("invalid toggle button")); |
4f856067 | 113 | |
e338053c | 114 | return gtk_toggle_button_get_active((GtkToggleButton*)m_widget); |
4f856067 RR |
115 | } |
116 | ||
117 | void wxToggleBitmapButton::SetLabel(const wxBitmap& label) | |
118 | { | |
119 | wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button")); | |
120 | ||
121 | m_bitmap = label; | |
9f884528 | 122 | InvalidateBestSize(); |
91af0895 | 123 | |
4f856067 RR |
124 | OnSetBitmap(); |
125 | } | |
126 | ||
127 | void wxToggleBitmapButton::OnSetBitmap() | |
128 | { | |
129 | if (!m_bitmap.Ok()) return; | |
130 | ||
e338053c PC |
131 | GtkWidget* image = ((GtkBin*)m_widget)->child; |
132 | if (image == NULL) | |
4f856067 RR |
133 | { |
134 | // initial bitmap | |
e338053c PC |
135 | image = gtk_image_new_from_pixbuf(m_bitmap.GetPixbuf()); |
136 | gtk_widget_show(image); | |
137 | gtk_container_add((GtkContainer*)m_widget, image); | |
4f856067 RR |
138 | } |
139 | else | |
140 | { // subsequent bitmaps | |
e338053c | 141 | gtk_image_set_from_pixbuf((GtkImage*)image, m_bitmap.GetPixbuf()); |
4f856067 RR |
142 | } |
143 | } | |
144 | ||
91af0895 | 145 | bool wxToggleBitmapButton::Enable(bool enable /*=true*/) |
4f856067 RR |
146 | { |
147 | if (!wxControl::Enable(enable)) | |
91af0895 | 148 | return false; |
4f856067 | 149 | |
afa7bd1e | 150 | gtk_widget_set_sensitive(GTK_BIN(m_widget)->child, enable); |
4f856067 | 151 | |
91af0895 | 152 | return true; |
4f856067 RR |
153 | } |
154 | ||
f40fdaa3 | 155 | void wxToggleBitmapButton::DoApplyWidgetStyle(GtkRcStyle *style) |
4f856067 | 156 | { |
f40fdaa3 | 157 | gtk_widget_modify_style(m_widget, style); |
afa7bd1e | 158 | gtk_widget_modify_style(GTK_BIN(m_widget)->child, style); |
4f856067 RR |
159 | } |
160 | ||
ef5c70f9 VZ |
161 | GdkWindow * |
162 | wxToggleBitmapButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const | |
4f856067 | 163 | { |
ef5c70f9 | 164 | return GTK_BUTTON(m_widget)->event_window; |
4f856067 RR |
165 | } |
166 | ||
4f856067 RR |
167 | // Get the "best" size for this control. |
168 | wxSize wxToggleBitmapButton::DoGetBestSize() const | |
169 | { | |
abdeb9e7 | 170 | wxSize best; |
91af0895 | 171 | |
abdeb9e7 | 172 | if (m_bitmap.Ok()) |
4f856067 | 173 | { |
abdeb9e7 RD |
174 | int border = HasFlag(wxNO_BORDER) ? 4 : 10; |
175 | best.x = m_bitmap.GetWidth()+border; | |
176 | best.y = m_bitmap.GetHeight()+border; | |
4f856067 | 177 | } |
9f884528 | 178 | CacheBestSize(best); |
abdeb9e7 | 179 | return best; |
4f856067 | 180 | } |
9d522606 RD |
181 | |
182 | ||
183 | // static | |
184 | wxVisualAttributes | |
185 | wxToggleBitmapButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
186 | { | |
187 | return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new); | |
188 | } | |
189 | ||
190 | ||
4f856067 RR |
191 | // ------------------------------------------------------------------------ |
192 | // wxToggleButton | |
193 | // ------------------------------------------------------------------------ | |
194 | ||
195 | IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl) | |
196 | ||
1db8dc4a VZ |
197 | bool wxToggleButton::Create(wxWindow *parent, wxWindowID id, |
198 | const wxString &label, const wxPoint &pos, | |
199 | const wxSize &size, long style, | |
200 | const wxValidator& validator, | |
201 | const wxString &name) | |
202 | { | |
91af0895 | 203 | m_needParent = true; |
1db8dc4a | 204 | |
91af0895 | 205 | m_blockEvent = false; |
1db8dc4a | 206 | |
91af0895 | 207 | if (!PreCreation(parent, pos, size) || |
e338053c PC |
208 | !CreateBase(parent, id, pos, size, style, validator, name )) |
209 | { | |
91af0895 WS |
210 | wxFAIL_MSG(wxT("wxToggleButton creation failed")); |
211 | return false; | |
212 | } | |
1db8dc4a | 213 | |
91af0895 | 214 | // Create the gtk widget. |
ecf3b4a5 RR |
215 | m_widget = gtk_toggle_button_new_with_mnemonic(""); |
216 | ||
217 | SetLabel(label); | |
1db8dc4a | 218 | |
9fa72bd2 MR |
219 | g_signal_connect (m_widget, "clicked", |
220 | G_CALLBACK (gtk_togglebutton_clicked_callback), | |
221 | this); | |
1db8dc4a | 222 | |
91af0895 | 223 | m_parent->DoAddChild(this); |
1db8dc4a | 224 | |
91af0895 WS |
225 | PostCreation(size); |
226 | ||
227 | return true; | |
1db8dc4a VZ |
228 | } |
229 | ||
230 | // void SetValue(bool state) | |
231 | // Set the value of the toggle button. | |
232 | void wxToggleButton::SetValue(bool state) | |
233 | { | |
91af0895 | 234 | wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button")); |
1db8dc4a | 235 | |
91af0895 WS |
236 | if (state == GetValue()) |
237 | return; | |
1db8dc4a | 238 | |
91af0895 | 239 | m_blockEvent = true; |
1db8dc4a | 240 | |
91af0895 | 241 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget), state); |
1db8dc4a | 242 | |
91af0895 | 243 | m_blockEvent = false; |
1db8dc4a VZ |
244 | } |
245 | ||
246 | // bool GetValue() const | |
247 | // Get the value of the toggle button. | |
248 | bool wxToggleButton::GetValue() const | |
249 | { | |
91af0895 | 250 | wxCHECK_MSG(m_widget != NULL, false, wxT("invalid toggle button")); |
1db8dc4a | 251 | |
91af0895 | 252 | return GTK_TOGGLE_BUTTON(m_widget)->active; |
1db8dc4a VZ |
253 | } |
254 | ||
1db8dc4a VZ |
255 | void wxToggleButton::SetLabel(const wxString& label) |
256 | { | |
8ab696e0 | 257 | wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button")); |
1db8dc4a | 258 | |
8ab696e0 | 259 | wxControl::SetLabel(label); |
1db8dc4a | 260 | |
ecf3b4a5 RR |
261 | const wxString labelGTK = GTKConvertMnemonics(label); |
262 | ||
263 | gtk_button_set_label(GTK_BUTTON(m_widget), wxGTK_CONV(labelGTK)); | |
264 | ||
265 | ApplyWidgetStyle( false ); | |
1db8dc4a VZ |
266 | } |
267 | ||
91af0895 | 268 | bool wxToggleButton::Enable(bool enable /*=true*/) |
1db8dc4a | 269 | { |
8ab696e0 | 270 | if (!wxControl::Enable(enable)) |
91af0895 | 271 | return false; |
1db8dc4a | 272 | |
afa7bd1e | 273 | gtk_widget_set_sensitive(GTK_BIN(m_widget)->child, enable); |
1db8dc4a | 274 | |
91af0895 | 275 | return true; |
1db8dc4a VZ |
276 | } |
277 | ||
f40fdaa3 | 278 | void wxToggleButton::DoApplyWidgetStyle(GtkRcStyle *style) |
1db8dc4a | 279 | { |
f40fdaa3 | 280 | gtk_widget_modify_style(m_widget, style); |
afa7bd1e | 281 | gtk_widget_modify_style(GTK_BIN(m_widget)->child, style); |
1db8dc4a VZ |
282 | } |
283 | ||
ef5c70f9 VZ |
284 | GdkWindow * |
285 | wxToggleButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const | |
1db8dc4a | 286 | { |
ef5c70f9 | 287 | return GTK_BUTTON(m_widget)->event_window; |
1db8dc4a VZ |
288 | } |
289 | ||
1db8dc4a VZ |
290 | // Get the "best" size for this control. |
291 | wxSize wxToggleButton::DoGetBestSize() const | |
292 | { | |
8ab696e0 | 293 | wxSize ret(wxControl::DoGetBestSize()); |
91af0895 | 294 | |
8ab696e0 RR |
295 | if (!HasFlag(wxBU_EXACTFIT)) |
296 | { | |
297 | if (ret.x < 80) ret.x = 80; | |
298 | } | |
91af0895 | 299 | |
9f884528 RD |
300 | CacheBestSize(ret); |
301 | return ret; | |
1db8dc4a VZ |
302 | } |
303 | ||
9d522606 RD |
304 | // static |
305 | wxVisualAttributes | |
306 | wxToggleButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
307 | { | |
308 | return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new); | |
309 | } | |
310 | ||
1db8dc4a | 311 | #endif // wxUSE_TOGGLEBTN |