wxDialogs now use GTK_WINDOW_DIALOG,
[wxWidgets.git] / src / gtk1 / checkbox.cpp
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 #ifdef __GNUG__
12 #pragma implementation "checkbox.h"
13 #endif
14
15 #include "wx/checkbox.h"
16
17 #if wxUSE_CHECKBOX
18
19 #include "gdk/gdk.h"
20 #include "gtk/gtk.h"
21
22 //-----------------------------------------------------------------------------
23 // idle system
24 //-----------------------------------------------------------------------------
25
26 extern void wxapp_install_idle_handler();
27 extern bool g_isIdle;
28
29 //-----------------------------------------------------------------------------
30 // data
31 //-----------------------------------------------------------------------------
32
33 extern bool g_blockEventsOnDrag;
34 extern wxCursor g_globalCursor;
35
36 //-----------------------------------------------------------------------------
37 // "clicked"
38 //-----------------------------------------------------------------------------
39
40 static void gtk_checkbox_clicked_callback( GtkWidget *WXUNUSED(widget), wxCheckBox *cb )
41 {
42 if (g_isIdle) wxapp_install_idle_handler();
43
44 if (!cb->m_hasVMT) return;
45
46 if (g_blockEventsOnDrag) return;
47
48 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, cb->GetId());
49 event.SetInt( cb->GetValue() );
50 event.SetEventObject(cb);
51 cb->GetEventHandler()->ProcessEvent(event);
52 }
53
54 //-----------------------------------------------------------------------------
55 // wxCheckBox
56 //-----------------------------------------------------------------------------
57
58 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox,wxControl)
59
60 wxCheckBox::wxCheckBox()
61 {
62 }
63
64 bool wxCheckBox::Create(wxWindow *parent,
65 wxWindowID id,
66 const wxString &label,
67 const wxPoint &pos,
68 const wxSize &size,
69 long style,
70 const wxValidator& validator,
71 const wxString &name )
72 {
73 m_needParent = TRUE;
74 m_acceptsFocus = TRUE;
75
76 if (!PreCreation( parent, pos, size ) ||
77 !CreateBase( parent, id, pos, size, style, validator, name ))
78 {
79 wxFAIL_MSG( wxT("wxCheckBox creation failed") );
80 return FALSE;
81 }
82
83 wxControl::SetLabel( label );
84
85 if ( style & wxALIGN_RIGHT )
86 {
87 // VZ: as I don't know a way to create a right aligned checkbox with
88 // GTK we will create a checkbox without label and a label at the
89 // left of it
90 m_widgetCheckbox = gtk_check_button_new();
91
92 m_widgetLabel = gtk_label_new(m_label.mbc_str());
93 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel), 0.0, 0.5);
94
95 m_widget = gtk_hbox_new(FALSE, 0);
96 gtk_box_pack_start(GTK_BOX(m_widget), m_widgetLabel, FALSE, FALSE, 3);
97 gtk_box_pack_start(GTK_BOX(m_widget), m_widgetCheckbox, FALSE, FALSE, 3);
98
99 gtk_widget_show( m_widgetLabel );
100 gtk_widget_show( m_widgetCheckbox );
101 }
102 else
103 {
104 m_widgetCheckbox = gtk_check_button_new_with_label( m_label.mbc_str() );
105 m_widgetLabel = GTK_BUTTON( m_widgetCheckbox )->child;
106 m_widget = m_widgetCheckbox;
107 }
108
109 wxSize newSize(size);
110 if (newSize.x == -1)
111 {
112 newSize.x = 25 + gdk_string_measure( m_widgetCheckbox->style->font,
113 m_label.mbc_str() );
114 }
115 if (newSize.y == -1)
116 newSize.y = 26;
117
118 SetSize( newSize.x, newSize.y );
119
120 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox),
121 "clicked",
122 GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback),
123 (gpointer *)this );
124
125 m_parent->DoAddChild( this );
126
127 PostCreation();
128
129 SetBackgroundColour( parent->GetBackgroundColour() );
130 SetForegroundColour( parent->GetForegroundColour() );
131 SetFont( parent->GetFont() );
132
133 Show( TRUE );
134
135 return TRUE;
136 }
137
138 void wxCheckBox::SetValue( bool state )
139 {
140 wxCHECK_RET( m_widgetCheckbox != NULL, wxT("invalid checkbox") );
141
142 if (state == GetValue())
143 return;
144
145 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widgetCheckbox),
146 GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback),
147 (gpointer *)this );
148
149 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox), state );
150
151 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox),
152 "clicked",
153 GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback),
154 (gpointer *)this );
155 }
156
157 bool wxCheckBox::GetValue() const
158 {
159 wxCHECK_MSG( m_widgetCheckbox != NULL, FALSE, wxT("invalid checkbox") );
160
161 return GTK_TOGGLE_BUTTON(m_widgetCheckbox)->active;
162 }
163
164 void wxCheckBox::SetLabel( const wxString& label )
165 {
166 wxCHECK_RET( m_widgetLabel != NULL, wxT("invalid checkbox") );
167
168 wxControl::SetLabel( label );
169
170 gtk_label_set( GTK_LABEL(m_widgetLabel), GetLabel().mbc_str() );
171 }
172
173 bool wxCheckBox::Enable( bool enable )
174 {
175 if ( !wxControl::Enable( enable ) )
176 return FALSE;
177
178 gtk_widget_set_sensitive( m_widgetLabel, enable );
179
180 return TRUE;
181 }
182
183 void wxCheckBox::ApplyWidgetStyle()
184 {
185 SetWidgetStyle();
186 gtk_widget_set_style( m_widgetCheckbox, m_widgetStyle );
187 gtk_widget_set_style( m_widgetLabel, m_widgetStyle );
188 }
189
190 bool wxCheckBox::IsOwnGtkWindow( GdkWindow *window )
191 {
192 return (window == GTK_TOGGLE_BUTTON(m_widget)->event_window);
193 }
194
195 void wxCheckBox::OnInternalIdle()
196 {
197 wxCursor cursor = m_cursor;
198 if (g_globalCursor.Ok()) cursor = g_globalCursor;
199
200 if (GTK_TOGGLE_BUTTON(m_widgetCheckbox)->event_window && cursor.Ok())
201 {
202 /* I now set the cursor the anew in every OnInternalIdle call
203 as setting the cursor in a parent window also effects the
204 windows above so that checking for the current cursor is
205 not possible. */
206
207 gdk_window_set_cursor( GTK_TOGGLE_BUTTON(m_widgetCheckbox)->event_window, cursor.GetCursor() );
208 }
209
210 UpdateWindowUI();
211 }
212
213 #endif