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