]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/tglbtn.cpp
1. implemented radio menu items for wxGTK
[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;
24
25// void gtk_togglebutton_clicked_callback(GtkWidget *widget, wxToggleButton *cb)
26// Callback function given to gtk.
9864c56d 27static void gtk_togglebutton_clicked_callback(GtkWidget *WXUNUSED(widget), wxToggleButton *cb)
1db8dc4a
VZ
28{
29 if (g_isIdle)
30 wxapp_install_idle_handler();
31
32 if (!cb->m_hasVMT || g_blockEventsOnDrag)
33 return;
9864c56d
RR
34
35 if (cb->m_blockEvent) return;
1db8dc4a
VZ
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
1db8dc4a
VZ
47bool 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;
9864c56d
RR
55
56 m_blockEvent = FALSE;
1db8dc4a
VZ
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.
98void wxToggleButton::SetValue(bool state)
99{
100 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
101
102 if (state == GetValue())
103 return;
104
9864c56d 105 m_blockEvent = TRUE;
1db8dc4a 106
e2762ff0 107 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget), state);
1db8dc4a 108
9864c56d 109 m_blockEvent = FALSE;
1db8dc4a
VZ
110}
111
112// bool GetValue() const
113// Get the value of the toggle button.
114bool 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
1db8dc4a
VZ
121void wxToggleButton::SetLabel(const wxString& label)
122{
8ab696e0 123 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
1db8dc4a 124
8ab696e0 125 wxControl::SetLabel(label);
1db8dc4a 126
9e691f46 127 gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget)), GetLabel().mbc_str());
1db8dc4a
VZ
128}
129
1db8dc4a
VZ
130bool wxToggleButton::Enable(bool enable /*=TRUE*/)
131{
8ab696e0
RR
132 if (!wxControl::Enable(enable))
133 return FALSE;
1db8dc4a 134
9e691f46 135 gtk_widget_set_sensitive(BUTTON_CHILD(m_widget), enable);
1db8dc4a 136
8ab696e0 137 return TRUE;
1db8dc4a
VZ
138}
139
1db8dc4a
VZ
140void wxToggleButton::ApplyWidgetStyle()
141{
8ab696e0
RR
142 SetWidgetStyle();
143 gtk_widget_set_style(m_widget, m_widgetStyle);
9e691f46 144 gtk_widget_set_style(BUTTON_CHILD(m_widget), m_widgetStyle);
1db8dc4a
VZ
145}
146
1db8dc4a
VZ
147bool wxToggleButton::IsOwnGtkWindow(GdkWindow *window)
148{
9e691f46 149 return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
1db8dc4a
VZ
150}
151
1db8dc4a
VZ
152void wxToggleButton::OnInternalIdle()
153{
8ab696e0
RR
154 wxCursor cursor = m_cursor;
155
156 if (g_globalCursor.Ok())
157 cursor = g_globalCursor;
1db8dc4a 158
9e691f46
VZ
159 GdkWindow *win = TOGGLE_BUTTON_EVENT_WIN(m_widget);
160 if ( win && cursor.Ok() )
161 {
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
9e691f46 167 gdk_window_set_cursor(win, cursor.GetCursor());
8ab696e0 168 }
1db8dc4a 169
8ab696e0 170 UpdateWindowUI();
1db8dc4a
VZ
171}
172
173// wxSize DoGetBestSize() const
174// Get the "best" size for this control.
175wxSize wxToggleButton::DoGetBestSize() const
176{
8ab696e0
RR
177 wxSize ret(wxControl::DoGetBestSize());
178
179 if (!HasFlag(wxBU_EXACTFIT))
180 {
181 if (ret.x < 80) ret.x = 80;
182 }
183
1db8dc4a
VZ
184
185 return ret;
186}
187
188#endif // wxUSE_TOGGLEBTN
189