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