]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: checkbox.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | ||
11 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
12 | #pragma implementation "checkbox.h" | |
13 | #endif | |
14 | ||
15 | // For compilers that support precompilation, includes "wx.h". | |
16 | #include "wx/wxprec.h" | |
17 | ||
18 | #include "wx/defs.h" | |
19 | ||
20 | #if wxUSE_CHECKBOX | |
21 | ||
22 | #include "wx/checkbox.h" | |
23 | ||
24 | #include "wx/gtk/private.h" | |
25 | ||
26 | //----------------------------------------------------------------------------- | |
27 | // idle system | |
28 | //----------------------------------------------------------------------------- | |
29 | ||
30 | extern void wxapp_install_idle_handler(); | |
31 | extern bool g_isIdle; | |
32 | ||
33 | //----------------------------------------------------------------------------- | |
34 | // data | |
35 | //----------------------------------------------------------------------------- | |
36 | ||
37 | extern bool g_blockEventsOnDrag; | |
38 | extern wxCursor g_globalCursor; | |
39 | extern wxWindowGTK *g_delayedFocus; | |
40 | ||
41 | //----------------------------------------------------------------------------- | |
42 | // "clicked" | |
43 | //----------------------------------------------------------------------------- | |
44 | ||
45 | static void gtk_checkbox_clicked_callback( GtkWidget *WXUNUSED(widget), wxCheckBox *cb ) | |
46 | { | |
47 | if (g_isIdle) wxapp_install_idle_handler(); | |
48 | ||
49 | if (!cb->m_hasVMT) return; | |
50 | ||
51 | if (g_blockEventsOnDrag) return; | |
52 | ||
53 | if (cb->m_blockEvent) return; | |
54 | ||
55 | wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, cb->GetId()); | |
56 | event.SetInt( cb->GetValue() ); | |
57 | event.SetEventObject(cb); | |
58 | cb->GetEventHandler()->ProcessEvent(event); | |
59 | } | |
60 | ||
61 | //----------------------------------------------------------------------------- | |
62 | // wxCheckBox | |
63 | //----------------------------------------------------------------------------- | |
64 | ||
65 | IMPLEMENT_DYNAMIC_CLASS(wxCheckBox,wxControl) | |
66 | ||
67 | wxCheckBox::wxCheckBox() | |
68 | { | |
69 | } | |
70 | ||
71 | bool wxCheckBox::Create(wxWindow *parent, | |
72 | wxWindowID id, | |
73 | const wxString &label, | |
74 | const wxPoint &pos, | |
75 | const wxSize &size, | |
76 | long style, | |
77 | const wxValidator& validator, | |
78 | const wxString &name ) | |
79 | { | |
80 | m_needParent = TRUE; | |
81 | m_acceptsFocus = TRUE; | |
82 | m_blockEvent = FALSE; | |
83 | ||
84 | if (!PreCreation( parent, pos, size ) || | |
85 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
86 | { | |
87 | wxFAIL_MSG( wxT("wxCheckBox creation failed") ); | |
88 | return FALSE; | |
89 | } | |
90 | ||
91 | if ( style & wxALIGN_RIGHT ) | |
92 | { | |
93 | // VZ: as I don't know a way to create a right aligned checkbox with | |
94 | // GTK we will create a checkbox without label and a label at the | |
95 | // left of it | |
96 | m_widgetCheckbox = gtk_check_button_new(); | |
97 | ||
98 | m_widgetLabel = gtk_label_new(""); | |
99 | gtk_misc_set_alignment(GTK_MISC(m_widgetLabel), 0.0, 0.5); | |
100 | ||
101 | m_widget = gtk_hbox_new(FALSE, 0); | |
102 | gtk_box_pack_start(GTK_BOX(m_widget), m_widgetLabel, FALSE, FALSE, 3); | |
103 | gtk_box_pack_start(GTK_BOX(m_widget), m_widgetCheckbox, FALSE, FALSE, 3); | |
104 | ||
105 | gtk_widget_show( m_widgetLabel ); | |
106 | gtk_widget_show( m_widgetCheckbox ); | |
107 | } | |
108 | else | |
109 | { | |
110 | m_widgetCheckbox = gtk_check_button_new_with_label(""); | |
111 | m_widgetLabel = BUTTON_CHILD( m_widgetCheckbox ); | |
112 | m_widget = m_widgetCheckbox; | |
113 | } | |
114 | SetLabel( label ); | |
115 | ||
116 | gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox), | |
117 | "clicked", | |
118 | GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback), | |
119 | (gpointer *)this ); | |
120 | ||
121 | m_parent->DoAddChild( this ); | |
122 | ||
123 | PostCreation(size); | |
124 | ||
125 | return TRUE; | |
126 | } | |
127 | ||
128 | void wxCheckBox::SetValue( bool state ) | |
129 | { | |
130 | wxCHECK_RET( m_widgetCheckbox != NULL, wxT("invalid checkbox") ); | |
131 | ||
132 | if (state == GetValue()) | |
133 | return; | |
134 | ||
135 | m_blockEvent = TRUE; | |
136 | ||
137 | gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox), state ); | |
138 | ||
139 | m_blockEvent = FALSE; | |
140 | } | |
141 | ||
142 | bool wxCheckBox::GetValue() const | |
143 | { | |
144 | wxCHECK_MSG( m_widgetCheckbox != NULL, FALSE, wxT("invalid checkbox") ); | |
145 | ||
146 | return GTK_TOGGLE_BUTTON(m_widgetCheckbox)->active; | |
147 | } | |
148 | ||
149 | void wxCheckBox::SetLabel( const wxString& label ) | |
150 | { | |
151 | wxCHECK_RET( m_widgetLabel != NULL, wxT("invalid checkbox") ); | |
152 | ||
153 | wxControl::SetLabel( label ); | |
154 | ||
155 | #ifdef __WXGTK20__ | |
156 | wxString label2 = PrepareLabelMnemonics( label ); | |
157 | gtk_label_set_text_with_mnemonic( GTK_LABEL(m_widgetLabel), wxGTK_CONV( label2 ) ); | |
158 | #else | |
159 | gtk_label_set( GTK_LABEL(m_widgetLabel), wxGTK_CONV( GetLabel() ) ); | |
160 | #endif | |
161 | } | |
162 | ||
163 | bool wxCheckBox::Enable( bool enable ) | |
164 | { | |
165 | if ( !wxControl::Enable( enable ) ) | |
166 | return FALSE; | |
167 | ||
168 | gtk_widget_set_sensitive( m_widgetLabel, enable ); | |
169 | ||
170 | return TRUE; | |
171 | } | |
172 | ||
173 | void wxCheckBox::DoApplyWidgetStyle(GtkRcStyle *style) | |
174 | { | |
175 | gtk_widget_modify_style(m_widgetCheckbox, style); | |
176 | gtk_widget_modify_style(m_widgetLabel, style); | |
177 | } | |
178 | ||
179 | bool wxCheckBox::IsOwnGtkWindow( GdkWindow *window ) | |
180 | { | |
181 | return window == TOGGLE_BUTTON_EVENT_WIN(m_widget); | |
182 | } | |
183 | ||
184 | void wxCheckBox::OnInternalIdle() | |
185 | { | |
186 | wxCursor cursor = m_cursor; | |
187 | if (g_globalCursor.Ok()) cursor = g_globalCursor; | |
188 | ||
189 | GdkWindow *event_window = TOGGLE_BUTTON_EVENT_WIN(m_widgetCheckbox); | |
190 | if ( event_window && cursor.Ok() ) | |
191 | { | |
192 | /* I now set the cursor the anew in every OnInternalIdle call | |
193 | as setting the cursor in a parent window also effects the | |
194 | windows above so that checking for the current cursor is | |
195 | not possible. */ | |
196 | ||
197 | gdk_window_set_cursor( event_window, cursor.GetCursor() ); | |
198 | } | |
199 | ||
200 | if (g_delayedFocus == this) | |
201 | { | |
202 | if (GTK_WIDGET_REALIZED(m_widget)) | |
203 | { | |
204 | gtk_widget_grab_focus( m_widget ); | |
205 | g_delayedFocus = NULL; | |
206 | } | |
207 | } | |
208 | ||
209 | if (wxUpdateUIEvent::CanUpdate(this)) | |
210 | UpdateWindowUI(wxUPDATE_UI_FROMIDLE); | |
211 | } | |
212 | ||
213 | wxSize wxCheckBox::DoGetBestSize() const | |
214 | { | |
215 | return wxControl::DoGetBestSize(); | |
216 | } | |
217 | ||
218 | // static | |
219 | wxVisualAttributes | |
220 | wxCheckBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
221 | { | |
222 | return GetDefaultAttributesFromGTKWidget(gtk_check_button_new); | |
223 | } | |
224 | ||
225 | #endif |