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