Update to 24f58c58bb8d22c0e8e6c5ce43c536c47b719bc6
[gnt.git] / gntcheckbox.c
blob6b6e1c054425627a59e0e5b4116f45d98244cd8e
1 /**
2 * GNT - The GLib Ncurses Toolkit
4 * GNT is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
6 * source distribution.
8 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
23 #include "gntcheckbox.h"
25 enum
27 SIG_TOGGLED = 1,
28 SIGS,
31 static GntButtonClass *parent_class = NULL;
32 static guint signals[SIGS] = { 0 };
34 static void
35 gnt_check_box_draw(GntWidget *widget)
37 GntCheckBox *cb = GNT_CHECK_BOX(widget);
38 GntColorType type;
39 char *text;
41 if (gnt_widget_has_focus(widget))
42 type = GNT_COLOR_HIGHLIGHT;
43 else
44 type = GNT_COLOR_NORMAL;
46 wbkgdset(widget->window, '\0' | gnt_color_pair(type));
48 text = g_strdup_printf("[%c]", cb->checked ? 'X' : ' ');
49 mvwaddstr(widget->window, 0, 0, text);
50 g_free(text);
52 wbkgdset(widget->window, '\0' | gnt_color_pair(GNT_COLOR_NORMAL));
53 mvwaddstr(widget->window, 0, 4, GNT_BUTTON(cb)->priv->text);
55 GNTDEBUG;
58 static void
59 toggle_selection(GntWidget *widget)
61 GNT_CHECK_BOX(widget)->checked = !GNT_CHECK_BOX(widget)->checked;
62 g_signal_emit(widget, signals[SIG_TOGGLED], 0);
63 gnt_widget_draw(widget);
66 static gboolean
67 gnt_check_box_key_pressed(GntWidget *widget, const char *text)
69 if (text[0] == ' ' && text[1] == '\0')
71 toggle_selection(widget);
72 return TRUE;
75 return FALSE;
78 static gboolean
79 gnt_check_box_clicked(GntWidget *widget, GntMouseEvent event, int x, int y)
81 if (event == GNT_LEFT_MOUSE_DOWN) {
82 toggle_selection(widget);
83 return TRUE;
85 return FALSE;
88 static void
89 gnt_check_box_class_init(GntCheckBoxClass *klass)
91 GntWidgetClass *wclass = GNT_WIDGET_CLASS(klass);
93 parent_class = GNT_BUTTON_CLASS(klass);
94 /*parent_class->destroy = gnt_check_box_destroy;*/
95 wclass->draw = gnt_check_box_draw;
96 /*parent_class->map = gnt_check_box_map;*/
97 /*parent_class->size_request = gnt_check_box_size_request;*/
98 wclass->key_pressed = gnt_check_box_key_pressed;
99 wclass->clicked = gnt_check_box_clicked;
101 signals[SIG_TOGGLED] =
102 g_signal_new("toggled",
103 G_TYPE_FROM_CLASS(klass),
104 G_SIGNAL_RUN_LAST,
105 G_STRUCT_OFFSET(GntCheckBoxClass, toggled),
106 NULL, NULL,
107 g_cclosure_marshal_VOID__VOID,
108 G_TYPE_NONE, 0);
109 GNTDEBUG;
112 static void
113 gnt_check_box_init(GTypeInstance *instance, gpointer class)
115 GntWidget *widget = GNT_WIDGET(instance);
116 widget->priv.minh = 1;
117 widget->priv.minw = 4;
118 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_NO_BORDER | GNT_WIDGET_NO_SHADOW);
119 GNTDEBUG;
122 /******************************************************************************
123 * GntCheckBox API
124 *****************************************************************************/
125 GType
126 gnt_check_box_get_gtype(void)
128 static GType type = 0;
130 if(type == 0)
132 static const GTypeInfo info = {
133 sizeof(GntCheckBoxClass),
134 NULL, /* base_init */
135 NULL, /* base_finalize */
136 (GClassInitFunc)gnt_check_box_class_init,
137 NULL, /* class_finalize */
138 NULL, /* class_data */
139 sizeof(GntCheckBox),
140 0, /* n_preallocs */
141 gnt_check_box_init, /* instance_init */
142 NULL /* value_table */
145 type = g_type_register_static(GNT_TYPE_BUTTON,
146 "GntCheckBox",
147 &info, 0);
150 return type;
153 GntWidget *gnt_check_box_new(const char *text)
155 GntWidget *widget = g_object_new(GNT_TYPE_CHECK_BOX, NULL);
157 GNT_BUTTON(widget)->priv->text = g_strdup(text);
158 gnt_widget_set_take_focus(widget, TRUE);
160 return widget;
163 void gnt_check_box_set_checked(GntCheckBox *box, gboolean set)
165 if (set != box->checked)
167 box->checked = set;
168 g_signal_emit(box, signals[SIG_TOGGLED], 0);
172 gboolean gnt_check_box_get_checked(GntCheckBox *box)
174 return box->checked;