psppire-cell-renderer-button: Add "slash" property.
[pspp] / src / ui / gui / psppire-button-editable.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2011, 2012 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "ui/gui/psppire-button-editable.h"
20
21 #include <gettext.h>
22 #define _(msgid) gettext (msgid)
23 #define N_(msgid) msgid
24
25 /* GtkCellEditable interface. */
26 static void gtk_cell_editable_interface_init (GtkCellEditableIface *iface);
27 static void button_editable_editing_done (GtkCellEditable *cell_editable);
28 static void button_editable_remove_widget (GtkCellEditable *cell_editable);
29 static void button_editable_start_editing (GtkCellEditable *cell_editable,
30                                            GdkEvent        *event);
31
32 G_DEFINE_TYPE_EXTENDED (PsppireButtonEditable,
33                         psppire_button_editable,
34                         GTK_TYPE_BUTTON,
35                         0,
36                         G_IMPLEMENT_INTERFACE (
37                           GTK_TYPE_CELL_EDITABLE,
38                           gtk_cell_editable_interface_init));
39
40 enum
41   {
42     PROP_0,
43     PROP_PATH,
44     PROP_SLASH
45   };
46
47 static void
48 psppire_button_editable_set_property (GObject      *object,
49                                       guint         prop_id,
50                                       const GValue *value,
51                                       GParamSpec   *pspec)
52 {
53   PsppireButtonEditable *obj = PSPPIRE_BUTTON_EDITABLE (object);
54
55   switch (prop_id)
56     {
57     case PROP_PATH:
58       g_free (obj->path);
59       obj->path = g_value_dup_string (value);
60       break;
61
62     case PROP_SLASH:
63       psppire_button_editable_set_slash (obj, g_value_get_boolean (value));
64       break;
65
66     default:
67       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
68       break;
69     }
70 }
71
72 static void
73 psppire_button_editable_get_property (GObject      *object,
74                                       guint         prop_id,
75                                       GValue       *value,
76                                       GParamSpec   *pspec)
77 {
78   PsppireButtonEditable *obj = PSPPIRE_BUTTON_EDITABLE (object);
79
80   switch (prop_id)
81     {
82     case PROP_PATH:
83       g_value_set_string (value, obj->path);
84       break;
85
86     case PROP_SLASH:
87       g_value_set_boolean (value, psppire_button_editable_get_slash (obj));
88       break;
89
90     default:
91       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
92       break;
93     }
94 }
95
96 static void
97 psppire_button_editable_dispose (GObject *gobject)
98 {
99   PsppireButtonEditable *obj = PSPPIRE_BUTTON_EDITABLE (gobject);
100
101   g_free (obj->path);
102   obj->path = NULL;
103
104   G_OBJECT_CLASS (psppire_button_editable_parent_class)->dispose (gobject);
105 }
106
107 static gboolean
108 psppire_button_editable_button_release (GtkWidget      *widget,
109                                         GdkEventButton *event)
110 {
111   GtkButton *button;
112
113   if (event->button == 1)
114     {
115       button = GTK_BUTTON (widget);
116       gtk_button_released (button);
117     }
118
119   return TRUE;
120 }
121
122 static gboolean
123 psppire_button_editable_expose_event (GtkWidget      *widget,
124                                       GdkEventExpose *event)
125 {
126   GtkWidgetClass *widget_class;
127   GtkStyle *style = gtk_widget_get_style (widget);
128   GtkAllocation allocation;
129   gboolean retval;
130
131   widget_class = GTK_WIDGET_CLASS (psppire_button_editable_parent_class);
132   retval = widget_class->expose_event (widget, event);
133
134   gtk_widget_get_allocation (widget, &allocation);
135   if (PSPPIRE_BUTTON_EDITABLE (widget)->slash)
136     gdk_draw_line (gtk_widget_get_window (widget), style->black_gc,
137                    allocation.x,
138                    allocation.y + allocation.height,
139                    allocation.x + allocation.width,
140                    allocation.y);
141   return retval;
142 }
143
144 static void
145 psppire_button_editable_class_init (PsppireButtonEditableClass *class)
146 {
147   GObjectClass *gobject_class;
148   GtkWidgetClass *widget_class;
149
150   gobject_class = G_OBJECT_CLASS (class);
151   widget_class = GTK_WIDGET_CLASS (class);
152
153   gobject_class->set_property = psppire_button_editable_set_property;
154   gobject_class->get_property = psppire_button_editable_get_property;
155   gobject_class->dispose = psppire_button_editable_dispose;
156
157   widget_class->button_release_event = psppire_button_editable_button_release;
158   widget_class->expose_event = psppire_button_editable_expose_event;
159
160   g_object_class_install_property (gobject_class,
161                                    PROP_PATH,
162                                    g_param_spec_string ("path",
163                                                         _("TreeView path"),
164                                                         _("The path to the row in the GtkTreeView, as a string"),
165                                                         "",
166                                                         G_PARAM_READWRITE));
167
168   g_object_class_install_property (gobject_class,
169                                    PROP_SLASH,
170                                    g_param_spec_boolean ("slash",
171                                                          _("Diagonal slash"),
172                                                          _("Whether to draw a diagonal slash across the button."),
173                                                          FALSE,
174                                                          G_PARAM_READWRITE));
175
176 }
177
178 static void
179 psppire_button_editable_init (PsppireButtonEditable *obj)
180 {
181   obj->path = g_strdup ("");
182   obj->slash = FALSE;
183 }
184
185 PsppireButtonEditable *
186 psppire_button_editable_new (void)
187 {
188   return PSPPIRE_BUTTON_EDITABLE (g_object_new (PSPPIRE_TYPE_BUTTON_EDITABLE, NULL));
189 }
190
191 void
192 psppire_button_editable_set_slash (PsppireButtonEditable *button,
193                                    gboolean slash)
194 {
195   g_return_if_fail (button != NULL);
196   if ((button->slash != 0) != (slash != 0))
197     {
198       button->slash = slash;
199       gtk_widget_queue_draw (GTK_WIDGET (button));
200     }
201 }
202
203 gboolean
204 psppire_button_editable_get_slash (const PsppireButtonEditable *button)
205 {
206   g_return_val_if_fail (button != NULL, FALSE);
207   return button->slash;
208 }
209 \f
210 /* GtkCellEditable interface. */
211
212 static void
213 gtk_cell_editable_interface_init (GtkCellEditableIface *iface)
214 {
215   g_return_if_fail (iface != NULL);
216
217   iface->editing_done = button_editable_editing_done;
218   iface->remove_widget = button_editable_remove_widget;
219   iface->start_editing = button_editable_start_editing;
220 }
221
222 static void
223 button_editable_editing_done (GtkCellEditable *cell_editable)
224 {
225
226
227 }
228
229 static void
230 button_editable_remove_widget (GtkCellEditable *cell_editable)
231 {
232
233
234 }
235
236 static void
237 button_editable_start_editing (GtkCellEditable *cell_editable,
238                                GdkEvent        *event)
239 {
240 }