]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/tglbtn.cpp
Use a compatible bitmap if a depth of -1 is given
[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
14f355c2
VS
13// For compilers that support precompilation, includes "wx.h".
14#include "wx/wxprec.h"
15
1db8dc4a 16#include "wx/tglbtn.h"
8ab696e0 17#include "wx/button.h"
1db8dc4a
VZ
18
19#if wxUSE_TOGGLEBTN
20
9e691f46 21#include "wx/gtk/private.h"
1db8dc4a
VZ
22
23extern void wxapp_install_idle_handler();
24extern bool g_isIdle;
25extern bool g_blockEventsOnDrag;
26extern wxCursor g_globalCursor;
d7fa7eaa 27extern wxWindowGTK *g_delayedFocus;
1db8dc4a 28
9864c56d 29static void gtk_togglebutton_clicked_callback(GtkWidget *WXUNUSED(widget), wxToggleButton *cb)
1db8dc4a
VZ
30{
31 if (g_isIdle)
32 wxapp_install_idle_handler();
33
34 if (!cb->m_hasVMT || g_blockEventsOnDrag)
35 return;
9864c56d
RR
36
37 if (cb->m_blockEvent) return;
1db8dc4a
VZ
38
39 // Generate a wx event.
40 wxCommandEvent event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, cb->GetId());
41 event.SetInt(cb->GetValue());
42 event.SetEventObject(cb);
43 cb->GetEventHandler()->ProcessEvent(event);
44}
45
46IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl)
47DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED)
48
1db8dc4a
VZ
49bool wxToggleButton::Create(wxWindow *parent, wxWindowID id,
50 const wxString &label, const wxPoint &pos,
51 const wxSize &size, long style,
52 const wxValidator& validator,
53 const wxString &name)
54{
55 m_needParent = TRUE;
56 m_acceptsFocus = TRUE;
9864c56d
RR
57
58 m_blockEvent = FALSE;
1db8dc4a
VZ
59
60 if (!PreCreation(parent, pos, size) ||
61 !CreateBase(parent, id, pos, size, style, validator, name )) {
62 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
63 return FALSE;
64 }
65
66 wxControl::SetLabel(label);
67
68 // Create the gtk widget.
fab591c5 69 m_widget = gtk_toggle_button_new_with_label( wxGTK_CONV( m_label ) );
1db8dc4a
VZ
70
71 gtk_signal_connect(GTK_OBJECT(m_widget), "clicked",
72 GTK_SIGNAL_FUNC(gtk_togglebutton_clicked_callback),
73 (gpointer *)this);
74
75 m_parent->DoAddChild(this);
76
77 PostCreation();
e8e24dfa 78 InheritAttributes();
1db8dc4a
VZ
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
1db8dc4a
VZ
89 Show(TRUE);
90
91 return TRUE;
92}
93
94// void SetValue(bool state)
95// Set the value of the toggle button.
96void wxToggleButton::SetValue(bool state)
97{
98 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
99
100 if (state == GetValue())
101 return;
102
9864c56d 103 m_blockEvent = TRUE;
1db8dc4a 104
e2762ff0 105 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget), state);
1db8dc4a 106
9864c56d 107 m_blockEvent = FALSE;
1db8dc4a
VZ
108}
109
110// bool GetValue() const
111// Get the value of the toggle button.
112bool wxToggleButton::GetValue() const
113{
114 wxCHECK_MSG(m_widget != NULL, FALSE, wxT("invalid toggle button"));
115
116 return GTK_TOGGLE_BUTTON(m_widget)->active;
117}
118
1db8dc4a
VZ
119void wxToggleButton::SetLabel(const wxString& label)
120{
8ab696e0 121 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
1db8dc4a 122
8ab696e0 123 wxControl::SetLabel(label);
1db8dc4a 124
fab591c5 125 gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget)), wxGTK_CONV( GetLabel() ) );
1db8dc4a
VZ
126}
127
1db8dc4a
VZ
128bool wxToggleButton::Enable(bool enable /*=TRUE*/)
129{
8ab696e0
RR
130 if (!wxControl::Enable(enable))
131 return FALSE;
1db8dc4a 132
9e691f46 133 gtk_widget_set_sensitive(BUTTON_CHILD(m_widget), enable);
1db8dc4a 134
8ab696e0 135 return TRUE;
1db8dc4a
VZ
136}
137
1db8dc4a
VZ
138void wxToggleButton::ApplyWidgetStyle()
139{
8ab696e0
RR
140 SetWidgetStyle();
141 gtk_widget_set_style(m_widget, m_widgetStyle);
9e691f46 142 gtk_widget_set_style(BUTTON_CHILD(m_widget), m_widgetStyle);
1db8dc4a
VZ
143}
144
1db8dc4a
VZ
145bool wxToggleButton::IsOwnGtkWindow(GdkWindow *window)
146{
9e691f46 147 return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
1db8dc4a
VZ
148}
149
1db8dc4a
VZ
150void wxToggleButton::OnInternalIdle()
151{
8ab696e0
RR
152 wxCursor cursor = m_cursor;
153
154 if (g_globalCursor.Ok())
155 cursor = g_globalCursor;
1db8dc4a 156
9e691f46
VZ
157 GdkWindow *win = TOGGLE_BUTTON_EVENT_WIN(m_widget);
158 if ( win && cursor.Ok() )
159 {
1db8dc4a
VZ
160 /* I now set the cursor the anew in every OnInternalIdle call
161 as setting the cursor in a parent window also effects the
162 windows above so that checking for the current cursor is
163 not possible. */
164
9e691f46 165 gdk_window_set_cursor(win, cursor.GetCursor());
8ab696e0 166 }
1db8dc4a 167
e39af974
JS
168 if (wxUpdateUIEvent::CanUpdate(this))
169 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
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