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