]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/tglbtn.cpp
fixed FindOrCreatePen/Brush() for the case when the object couldn't be created succes...
[wxWidgets.git] / src / gtk1 / tglbtn.cpp
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
20 extern void wxapp_install_idle_handler();
21 extern bool g_isIdle;
22 extern bool g_blockEventsOnDrag;
23 extern wxCursor g_globalCursor;
24
25 // void gtk_togglebutton_clicked_callback(GtkWidget *widget, wxToggleButton *cb)
26 // Callback function given to gtk.
27 void wxToggleButton::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 // Generate a wx event.
36 wxCommandEvent event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, cb->GetId());
37 event.SetInt(cb->GetValue());
38 event.SetEventObject(cb);
39 cb->GetEventHandler()->ProcessEvent(event);
40 }
41
42 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl)
43 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED)
44
45 // bool Create(wxWindow *parent, wxWindowID id, const wxString &label,
46 // const wxPoint &pos, const wxSize &size, long style,
47 // const wxValidator& validator, const wxString &name)
48 // Create the control.
49 bool 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;
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.
98 void wxToggleButton::SetValue(bool state)
99 {
100 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
101
102 if (state == GetValue())
103 return;
104
105 gtk_signal_disconnect_by_func(GTK_OBJECT(m_widget),
106 GTK_SIGNAL_FUNC(gtk_togglebutton_clicked_callback),
107 (gpointer *)this);
108
109 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(m_widget), state);
110
111 gtk_signal_connect(GTK_OBJECT(m_widget), "clicked",
112 GTK_SIGNAL_FUNC(gtk_togglebutton_clicked_callback),
113 (gpointer *)this);
114 }
115
116 // bool GetValue() const
117 // Get the value of the toggle button.
118 bool 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.
127 void 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.
139 bool 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.
151 void 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.
160 bool 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.
167 void 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.
188 wxSize 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