]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/tglbtn.cpp
Add some version checks to help compiling on OSX.
[wxWidgets.git] / src / gtk / tglbtn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/tglbtn.cpp
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
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 // For compilers that support precompilation, includes "wx.h".
14 #include "wx/wxprec.h"
15
16 #if wxUSE_TOGGLEBTN
17
18 #include "wx/tglbtn.h"
19
20 #ifndef WX_PRECOMP
21 #include "wx/button.h"
22 #endif
23
24 #include "wx/gtk/private.h"
25
26 extern bool g_blockEventsOnDrag;
27
28 extern "C" {
29 static void gtk_togglebutton_clicked_callback(GtkWidget *WXUNUSED(widget), wxToggleButton *cb)
30 {
31 if (!cb->m_hasVMT || g_blockEventsOnDrag)
32 return;
33
34 // Generate a wx event.
35 wxCommandEvent event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, cb->GetId());
36 event.SetInt(cb->GetValue());
37 event.SetEventObject(cb);
38 cb->HandleWindowEvent(event);
39 }
40 }
41
42 wxDEFINE_EVENT( wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, wxCommandEvent );
43
44 // ------------------------------------------------------------------------
45 // wxBitmapToggleButton
46 // ------------------------------------------------------------------------
47
48 IMPLEMENT_DYNAMIC_CLASS(wxBitmapToggleButton, wxToggleButton)
49
50 bool wxBitmapToggleButton::Create(wxWindow *parent, wxWindowID id,
51 const wxBitmap &bitmap, const wxPoint &pos,
52 const wxSize &size, long style,
53 const wxValidator& validator,
54 const wxString &name)
55 {
56 if ( !wxToggleButton::Create(parent, id, wxEmptyString, pos, size, style | wxBU_NOTEXT | wxBU_EXACTFIT,
57 validator, name) )
58 return false;
59
60 if ( bitmap.IsOk() )
61 {
62 SetBitmapLabel(bitmap);
63
64 // we need to adjust the size after setting the bitmap as it may be too
65 // big for the default button size
66 SetInitialSize(size);
67 }
68
69 return true;
70 }
71
72
73 // ------------------------------------------------------------------------
74 // wxToggleButton
75 // ------------------------------------------------------------------------
76
77 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl)
78
79 bool wxToggleButton::Create(wxWindow *parent, wxWindowID id,
80 const wxString &label, const wxPoint &pos,
81 const wxSize &size, long style,
82 const wxValidator& validator,
83 const wxString &name)
84 {
85 if (!PreCreation(parent, pos, size) ||
86 !CreateBase(parent, id, pos, size, style, validator, name ))
87 {
88 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
89 return false;
90 }
91
92 // create either a standard toggle button with text label (which may still contain
93 // an image under GTK+ 2.6+) or a bitmap-only toggle button if we don't have any
94 // label
95 const bool
96 useLabel = !(style & wxBU_NOTEXT) && !label.empty();
97 if ( useLabel )
98 {
99 m_widget = gtk_toggle_button_new_with_mnemonic("");
100 }
101 else // no label, suppose we will have a bitmap
102 {
103 m_widget = gtk_toggle_button_new();
104
105 GtkWidget *image = gtk_image_new();
106 gtk_widget_show(image);
107 gtk_container_add(GTK_CONTAINER(m_widget), image);
108 }
109
110 g_object_ref(m_widget);
111
112 if ( useLabel )
113 SetLabel(label);
114
115 g_signal_connect (m_widget, "clicked",
116 G_CALLBACK (gtk_togglebutton_clicked_callback),
117 this);
118
119 m_parent->DoAddChild(this);
120
121 PostCreation(size);
122
123 return true;
124 }
125
126 void wxToggleButton::GTKDisableEvents()
127 {
128 g_signal_handlers_block_by_func(m_widget,
129 (gpointer) gtk_togglebutton_clicked_callback, this);
130 }
131
132 void wxToggleButton::GTKEnableEvents()
133 {
134 g_signal_handlers_unblock_by_func(m_widget,
135 (gpointer) gtk_togglebutton_clicked_callback, this);
136 }
137
138 // void SetValue(bool state)
139 // Set the value of the toggle button.
140 void wxToggleButton::SetValue(bool state)
141 {
142 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
143
144 if (state == GetValue())
145 return;
146
147 GTKDisableEvents();
148
149 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget), state);
150
151 GTKEnableEvents();
152 }
153
154 // bool GetValue() const
155 // Get the value of the toggle button.
156 bool wxToggleButton::GetValue() const
157 {
158 wxCHECK_MSG(m_widget != NULL, false, wxT("invalid toggle button"));
159
160 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_widget)) != 0;
161 }
162
163 void wxToggleButton::SetLabel(const wxString& label)
164 {
165 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
166
167 wxAnyButton::SetLabel(label);
168
169 const wxString labelGTK = GTKConvertMnemonics(label);
170
171 gtk_button_set_label(GTK_BUTTON(m_widget), wxGTK_CONV(labelGTK));
172
173 GTKApplyWidgetStyle( false );
174 }
175
176 #if wxUSE_MARKUP
177 bool wxToggleButton::DoSetLabelMarkup(const wxString& markup)
178 {
179 wxCHECK_MSG( m_widget != NULL, false, "invalid toggle button" );
180
181 const wxString stripped = RemoveMarkup(markup);
182 if ( stripped.empty() && !markup.empty() )
183 return false;
184
185 wxControl::SetLabel(stripped);
186
187 GtkLabel * const label = GTKGetLabel();
188 wxCHECK_MSG( label, false, "no label in this toggle button?" );
189
190 GTKSetLabelWithMarkupForLabel(label, markup);
191
192 return true;
193 }
194 #endif // wxUSE_MARKUP
195
196 GtkLabel *wxToggleButton::GTKGetLabel() const
197 {
198 GtkWidget* child = gtk_bin_get_child(GTK_BIN(m_widget));
199 return GTK_LABEL(child);
200 }
201
202 void wxToggleButton::DoApplyWidgetStyle(GtkRcStyle *style)
203 {
204 gtk_widget_modify_style(m_widget, style);
205 gtk_widget_modify_style(gtk_bin_get_child(GTK_BIN(m_widget)), style);
206 }
207
208 // Get the "best" size for this control.
209 wxSize wxToggleButton::DoGetBestSize() const
210 {
211 wxSize ret(wxAnyButton::DoGetBestSize());
212
213 if (!HasFlag(wxBU_EXACTFIT))
214 {
215 if (ret.x < 80) ret.x = 80;
216 }
217
218 CacheBestSize(ret);
219 return ret;
220 }
221
222 // static
223 wxVisualAttributes
224 wxToggleButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
225 {
226 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new);
227 }
228
229 #endif // wxUSE_TOGGLEBTN