]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/tglbtn.cpp
addind nonowned window implementation
[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();
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 89void 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 105bool 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 112void 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 122void 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
RR
128 {
129 // initial bitmap
e338053c
PC
130 image = gtk_image_new_from_pixbuf(m_bitmap.GetPixbuf());
131 gtk_widget_show(image);
132 gtk_container_add((GtkContainer*)m_widget, image);
4f856067
RR
133 }
134 else
135 { // subsequent bitmaps
e338053c 136 gtk_image_set_from_pixbuf((GtkImage*)image, m_bitmap.GetPixbuf());
4f856067
RR
137 }
138}
139
10ff9c61 140bool wxBitmapToggleButton::Enable(bool enable /*=true*/)
4f856067
RR
141{
142 if (!wxControl::Enable(enable))
91af0895 143 return false;
4f856067 144
afa7bd1e 145 gtk_widget_set_sensitive(GTK_BIN(m_widget)->child, enable);
4f856067 146
91af0895 147 return true;
4f856067
RR
148}
149
10ff9c61 150void wxBitmapToggleButton::DoApplyWidgetStyle(GtkRcStyle *style)
4f856067 151{
f40fdaa3 152 gtk_widget_modify_style(m_widget, style);
afa7bd1e 153 gtk_widget_modify_style(GTK_BIN(m_widget)->child, style);
4f856067
RR
154}
155
ef5c70f9 156GdkWindow *
10ff9c61 157wxBitmapToggleButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
4f856067 158{
ef5c70f9 159 return GTK_BUTTON(m_widget)->event_window;
4f856067
RR
160}
161
4f856067 162// Get the "best" size for this control.
10ff9c61 163wxSize wxBitmapToggleButton::DoGetBestSize() const
4f856067 164{
abdeb9e7 165 wxSize best;
91af0895 166
abdeb9e7 167 if (m_bitmap.Ok())
4f856067 168 {
abdeb9e7
RD
169 int border = HasFlag(wxNO_BORDER) ? 4 : 10;
170 best.x = m_bitmap.GetWidth()+border;
171 best.y = m_bitmap.GetHeight()+border;
4f856067 172 }
9f884528 173 CacheBestSize(best);
abdeb9e7 174 return best;
4f856067 175}
9d522606
RD
176
177
178// static
179wxVisualAttributes
10ff9c61 180wxBitmapToggleButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
9d522606
RD
181{
182 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new);
183}
184
185
4f856067
RR
186// ------------------------------------------------------------------------
187// wxToggleButton
188// ------------------------------------------------------------------------
189
190IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl)
191
1db8dc4a
VZ
192bool wxToggleButton::Create(wxWindow *parent, wxWindowID id,
193 const wxString &label, const wxPoint &pos,
194 const wxSize &size, long style,
195 const wxValidator& validator,
196 const wxString &name)
197{
91af0895 198 m_blockEvent = false;
1db8dc4a 199
91af0895 200 if (!PreCreation(parent, pos, size) ||
e338053c
PC
201 !CreateBase(parent, id, pos, size, style, validator, name ))
202 {
91af0895
WS
203 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
204 return false;
205 }
1db8dc4a 206
91af0895 207 // Create the gtk widget.
ecf3b4a5
RR
208 m_widget = gtk_toggle_button_new_with_mnemonic("");
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