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