]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/tglbtn.cpp
Committing in .
[wxWidgets.git] / src / gtk / tglbtn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/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 // License: Rocketeer license
11 /////////////////////////////////////////////////////////////////////////////
12
13 #include "wx/tglbtn.h"
14 #include "wx/button.h"
15
16 #if wxUSE_TOGGLEBTN
17
18 #include "wx/gtk/private.h"
19
20 extern void wxapp_install_idle_handler();
21 extern bool g_isIdle;
22 extern bool g_blockEventsOnDrag;
23 extern wxCursor g_globalCursor;
24
25 // void gtk_togglebutton_clicked_callback(GtkWidget *widget, wxToggleButton *cb)
26 // Callback function given to gtk.
27 static void gtk_togglebutton_clicked_callback(GtkWidget *WXUNUSED(widget), wxToggleButton *cb)
28 {
29 if (g_isIdle)
30 wxapp_install_idle_handler();
31
32 if (!cb->m_hasVMT || g_blockEventsOnDrag)
33 return;
34
35 if (cb->m_blockEvent) return;
36
37 // Generate a wx event.
38 wxCommandEvent event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, cb->GetId());
39 event.SetInt(cb->GetValue());
40 event.SetEventObject(cb);
41 cb->GetEventHandler()->ProcessEvent(event);
42 }
43
44 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl)
45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED)
46
47 bool wxToggleButton::Create(wxWindow *parent, wxWindowID id,
48 const wxString &label, const wxPoint &pos,
49 const wxSize &size, long style,
50 const wxValidator& validator,
51 const wxString &name)
52 {
53 m_needParent = TRUE;
54 m_acceptsFocus = TRUE;
55
56 m_blockEvent = FALSE;
57
58 if (!PreCreation(parent, pos, size) ||
59 !CreateBase(parent, id, pos, size, style, validator, name )) {
60 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
61 return FALSE;
62 }
63
64 wxControl::SetLabel(label);
65
66 // Create the gtk widget.
67 m_widget = gtk_toggle_button_new_with_label(m_label.mbc_str());
68
69 gtk_signal_connect(GTK_OBJECT(m_widget), "clicked",
70 GTK_SIGNAL_FUNC(gtk_togglebutton_clicked_callback),
71 (gpointer *)this);
72
73 m_parent->DoAddChild(this);
74
75 PostCreation();
76
77 SetFont(parent->GetFont());
78
79 wxSize size_best(DoGetBestSize());
80 wxSize new_size(size);
81 if (new_size.x == -1)
82 new_size.x = size_best.x;
83 if (new_size.y == -1)
84 new_size.y = size_best.y;
85 if ((new_size.x != size.x) || (new_size.y != size.y))
86 SetSize(new_size.x, new_size.y);
87
88 SetBackgroundColour(parent->GetBackgroundColour());
89 SetForegroundColour(parent->GetForegroundColour());
90
91 Show(TRUE);
92
93 return TRUE;
94 }
95
96 // void SetValue(bool state)
97 // Set the value of the toggle button.
98 void wxToggleButton::SetValue(bool state)
99 {
100 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
101
102 if (state == GetValue())
103 return;
104
105 m_blockEvent = TRUE;
106
107 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget), state);
108
109 m_blockEvent = FALSE;
110 }
111
112 // bool GetValue() const
113 // Get the value of the toggle button.
114 bool wxToggleButton::GetValue() const
115 {
116 wxCHECK_MSG(m_widget != NULL, FALSE, wxT("invalid toggle button"));
117
118 return GTK_TOGGLE_BUTTON(m_widget)->active;
119 }
120
121 void wxToggleButton::SetLabel(const wxString& label)
122 {
123 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
124
125 wxControl::SetLabel(label);
126
127 gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget)), GetLabel().mbc_str());
128 }
129
130 bool wxToggleButton::Enable(bool enable /*=TRUE*/)
131 {
132 if (!wxControl::Enable(enable))
133 return FALSE;
134
135 gtk_widget_set_sensitive(BUTTON_CHILD(m_widget), enable);
136
137 return TRUE;
138 }
139
140 void wxToggleButton::ApplyWidgetStyle()
141 {
142 SetWidgetStyle();
143 gtk_widget_set_style(m_widget, m_widgetStyle);
144 gtk_widget_set_style(BUTTON_CHILD(m_widget), m_widgetStyle);
145 }
146
147 bool wxToggleButton::IsOwnGtkWindow(GdkWindow *window)
148 {
149 return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
150 }
151
152 void wxToggleButton::OnInternalIdle()
153 {
154 wxCursor cursor = m_cursor;
155
156 if (g_globalCursor.Ok())
157 cursor = g_globalCursor;
158
159 GdkWindow *win = TOGGLE_BUTTON_EVENT_WIN(m_widget);
160 if ( win && cursor.Ok() )
161 {
162 /* I now set the cursor the anew in every OnInternalIdle call
163 as setting the cursor in a parent window also effects the
164 windows above so that checking for the current cursor is
165 not possible. */
166
167 gdk_window_set_cursor(win, cursor.GetCursor());
168 }
169
170 UpdateWindowUI();
171 }
172
173 // wxSize DoGetBestSize() const
174 // Get the "best" size for this control.
175 wxSize wxToggleButton::DoGetBestSize() const
176 {
177 wxSize ret(wxControl::DoGetBestSize());
178
179 if (!HasFlag(wxBU_EXACTFIT))
180 {
181 if (ret.x < 80) ret.x = 80;
182 }
183
184
185 return ret;
186 }
187
188 #endif // wxUSE_TOGGLEBTN
189