15817
|
1 #include "gntcheckbox.h"
|
|
2
|
|
3 enum
|
|
4 {
|
|
5 SIG_TOGGLED = 1,
|
|
6 SIGS,
|
|
7 };
|
|
8
|
|
9 static GntButtonClass *parent_class = NULL;
|
|
10 static guint signals[SIGS] = { 0 };
|
|
11
|
|
12 static void
|
|
13 gnt_check_box_draw(GntWidget *widget)
|
|
14 {
|
|
15 GntCheckBox *cb = GNT_CHECK_BOX(widget);
|
|
16 GntColorType type;
|
|
17 char *text;
|
|
18
|
|
19 if (gnt_widget_has_focus(widget))
|
|
20 type = GNT_COLOR_HIGHLIGHT;
|
|
21 else
|
|
22 type = GNT_COLOR_NORMAL;
|
|
23
|
|
24 wbkgdset(widget->window, '\0' | COLOR_PAIR(type));
|
|
25
|
|
26 text = g_strdup_printf("[%c]", cb->checked ? 'X' : ' ');
|
|
27 mvwaddstr(widget->window, 0, 0, text);
|
|
28 g_free(text);
|
|
29
|
|
30 wbkgdset(widget->window, '\0' | COLOR_PAIR(GNT_COLOR_NORMAL));
|
|
31 mvwaddstr(widget->window, 0, 4, GNT_BUTTON(cb)->priv->text);
|
|
32
|
|
33 GNTDEBUG;
|
|
34 }
|
|
35
|
|
36 static void
|
|
37 toggle_selection(GntWidget *widget)
|
|
38 {
|
|
39 GNT_CHECK_BOX(widget)->checked = !GNT_CHECK_BOX(widget)->checked;
|
|
40 g_signal_emit(widget, signals[SIG_TOGGLED], 0);
|
|
41 gnt_widget_draw(widget);
|
|
42 }
|
|
43
|
|
44 static gboolean
|
|
45 gnt_check_box_key_pressed(GntWidget *widget, const char *text)
|
|
46 {
|
|
47 if (text[0] == ' ' && text[1] == '\0')
|
|
48 {
|
|
49 toggle_selection(widget);
|
|
50 return TRUE;
|
|
51 }
|
|
52
|
|
53 return FALSE;
|
|
54 }
|
|
55
|
|
56 static gboolean
|
|
57 gnt_check_box_clicked(GntWidget *widget, GntMouseEvent event, int x, int y)
|
|
58 {
|
|
59 if (event == GNT_LEFT_MOUSE_DOWN) {
|
|
60 toggle_selection(widget);
|
|
61 return TRUE;
|
|
62 }
|
|
63 return FALSE;
|
|
64 }
|
|
65
|
|
66 static void
|
|
67 gnt_check_box_class_init(GntCheckBoxClass *klass)
|
|
68 {
|
|
69 GntWidgetClass *wclass = GNT_WIDGET_CLASS(klass);
|
|
70
|
|
71 parent_class = GNT_BUTTON_CLASS(klass);
|
|
72 /*parent_class->destroy = gnt_check_box_destroy;*/
|
|
73 wclass->draw = gnt_check_box_draw;
|
|
74 /*parent_class->map = gnt_check_box_map;*/
|
|
75 /*parent_class->size_request = gnt_check_box_size_request;*/
|
|
76 wclass->key_pressed = gnt_check_box_key_pressed;
|
|
77 wclass->clicked = gnt_check_box_clicked;
|
|
78
|
|
79 signals[SIG_TOGGLED] =
|
|
80 g_signal_new("toggled",
|
|
81 G_TYPE_FROM_CLASS(klass),
|
|
82 G_SIGNAL_RUN_LAST,
|
|
83 G_STRUCT_OFFSET(GntCheckBoxClass, toggled),
|
|
84 NULL, NULL,
|
|
85 g_cclosure_marshal_VOID__VOID,
|
|
86 G_TYPE_NONE, 0);
|
|
87 GNTDEBUG;
|
|
88 }
|
|
89
|
|
90 static void
|
|
91 gnt_check_box_init(GTypeInstance *instance, gpointer class)
|
|
92 {
|
|
93 GntWidget *widget = GNT_WIDGET(instance);
|
|
94 widget->priv.minh = 1;
|
|
95 widget->priv.minw = 4;
|
|
96 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_NO_BORDER | GNT_WIDGET_NO_SHADOW);
|
|
97 GNTDEBUG;
|
|
98 }
|
|
99
|
|
100 /******************************************************************************
|
|
101 * GntCheckBox API
|
|
102 *****************************************************************************/
|
|
103 GType
|
|
104 gnt_check_box_get_gtype(void)
|
|
105 {
|
|
106 static GType type = 0;
|
|
107
|
|
108 if(type == 0)
|
|
109 {
|
|
110 static const GTypeInfo info = {
|
|
111 sizeof(GntCheckBoxClass),
|
|
112 NULL, /* base_init */
|
|
113 NULL, /* base_finalize */
|
|
114 (GClassInitFunc)gnt_check_box_class_init,
|
|
115 NULL, /* class_finalize */
|
|
116 NULL, /* class_data */
|
|
117 sizeof(GntCheckBox),
|
|
118 0, /* n_preallocs */
|
|
119 gnt_check_box_init, /* instance_init */
|
|
120 NULL /* value_table */
|
|
121 };
|
|
122
|
|
123 type = g_type_register_static(GNT_TYPE_BUTTON,
|
|
124 "GntCheckBox",
|
|
125 &info, 0);
|
|
126 }
|
|
127
|
|
128 return type;
|
|
129 }
|
|
130
|
|
131 GntWidget *gnt_check_box_new(const char *text)
|
|
132 {
|
|
133 GntWidget *widget = g_object_new(GNT_TYPE_CHECK_BOX, NULL);
|
|
134
|
|
135 GNT_BUTTON(widget)->priv->text = g_strdup(text);
|
|
136 gnt_widget_set_take_focus(widget, TRUE);
|
|
137
|
|
138 return widget;
|
|
139 }
|
|
140
|
|
141 void gnt_check_box_set_checked(GntCheckBox *box, gboolean set)
|
|
142 {
|
|
143 if (set != box->checked)
|
|
144 {
|
|
145 box->checked = set;
|
|
146 g_signal_emit(box, signals[SIG_TOGGLED], 0);
|
|
147 }
|
|
148 }
|
|
149
|
|
150 gboolean gnt_check_box_get_checked(GntCheckBox *box)
|
|
151 {
|
|
152 return box->checked;
|
|
153 }
|
|
154
|
|
155
|
|
156
|