File: sj-cell-renderer-text.c

package info (click to toggle)
sound-juicer 3.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,264 kB
  • ctags: 1,014
  • sloc: sh: 11,456; ansic: 8,534; xml: 805; makefile: 229
file content (355 lines) | stat: -rw-r--r-- 10,797 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
 * Copyright (C) 2014 Phillip Wood <phillip.wood@dunelm.org.uk>
 *
 * Sound Juicer - sj-cell-renderer-text.c
 *
 * This is based on gtkcellrenderertext.c from GTK
 * Copyright (C) 2000  Red Hat, Inc.,  Jonathan Blandford <jrb@redhat.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include "sj-cell-renderer-text.h"

#define SJ_CELL_RENDERER_TEXT_PATH "sj-cell-renderer-text-path"

typedef struct _SjCellRendererTextPrivate SjCellRendererTextPrivate;

struct _SjCellRendererTextPrivate
{
  GtkWidget *entry;
  gulong focus_out_id;
  gulong populate_popup_id;
  guint entry_menu_popdown_timeout;
  gboolean in_entry_menu;
};

G_DEFINE_TYPE_WITH_PRIVATE (SjCellRendererText, sj_cell_renderer_text, GTK_TYPE_CELL_RENDERER_TEXT);

static gboolean
move_focus (GtkWidget        *entry,
            GtkWidget        *tree_view,
            GtkDirectionType  dir)
{
  gboolean ret_val;

  gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
  gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
  g_signal_emit_by_name (tree_view, "move-focus", dir, &ret_val);
  return ret_val;
}

static gboolean
sj_cell_renderer_text_key_press (GtkWidget   *entry,
                                 GdkEventKey *event,
                                 gpointer     tree_view)
{
  guint modifier = event->state & gtk_accelerator_get_default_mod_mask ();
  guint key = event->keyval;
  gboolean ret_val = FALSE;

  if (gtk_entry_im_context_filter_keypress (GTK_ENTRY (entry), event))
    return TRUE;

  if (modifier == 0 || modifier == GDK_SHIFT_MASK) {
    switch(key) {
    case GDK_KEY_Down:
    case GDK_KEY_KP_Down:
    case GDK_KEY_Up:
    case GDK_KEY_KP_Up:
    case GDK_KEY_Page_Down:
    case GDK_KEY_KP_Page_Down:
    case GDK_KEY_Page_Up:
    case  GDK_KEY_KP_Page_Up:
      g_signal_emit_by_name (tree_view, "key-press-event", event, &ret_val);
      return TRUE;
    case GDK_KEY_Tab:
    case GDK_KEY_KP_Tab:
    case GDK_KEY_ISO_Left_Tab:
    case GDK_KEY_3270_BackTab:
      g_signal_emit_by_name (tree_view, "key-press-event", event, &ret_val);
      if (!ret_val) {
        GtkDirectionType direction;
        if (modifier == 0 && (key == GDK_KEY_Tab ||
                              key == GDK_KEY_KP_Tab))
          direction = GTK_DIR_TAB_FORWARD;
        else
          direction = GTK_DIR_TAB_BACKWARD;

        move_focus (entry, tree_view, direction);
      }
      return TRUE;
    }
  }

  if (modifier == GDK_CONTROL_MASK ||
      modifier == (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) {
    gint count = 1;
    switch (key) {
    case GDK_KEY_Home:
    case GDK_KEY_KP_Home:
      count = -1;
      /* Fall through */
    case GDK_KEY_End:
    case GDK_KEY_KP_End:
      event->state ^= GDK_CONTROL_MASK;
      g_signal_emit_by_name (tree_view, "move-cursor",
                             GTK_MOVEMENT_BUFFER_ENDS, count, &ret_val);
      event->state ^= GDK_CONTROL_MASK;
      return TRUE;
    }
  }

  if (modifier == GDK_CONTROL_MASK) {
    switch (key) {
    case GDK_KEY_Tab:
    case GDK_KEY_KP_Tab:
      move_focus (entry, tree_view, GTK_DIR_TAB_FORWARD);
      return TRUE;
    case GDK_KEY_ISO_Left_Tab:
    case GDK_KEY_3270_BackTab:
      move_focus (entry, tree_view, GTK_DIR_TAB_BACKWARD);
      return TRUE;
    }
  }

  if (modifier == (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) {
    switch (key) {
    case GDK_KEY_Tab:
    case GDK_KEY_KP_Tab:
    case GDK_KEY_ISO_Left_Tab:
    case GDK_KEY_3270_BackTab:
      move_focus (entry, tree_view, GTK_DIR_TAB_BACKWARD);
      return TRUE;
    }
  }
  return FALSE;
}

static void
sj_cell_renderer_text_editing_done (GtkCellEditable *entry,
                                    gpointer         data)
{
  SjCellRendererTextPrivate *priv;
  const gchar *path;
  const gchar *new_text;
  gboolean canceled;

  priv = sj_cell_renderer_text_get_instance_private (data);

  g_clear_object (&priv->entry);

  if (priv->focus_out_id > 0) {
    g_signal_handler_disconnect (entry, priv->focus_out_id);
    priv->focus_out_id = 0;
  }

  if (priv->populate_popup_id > 0) {
    g_signal_handler_disconnect (entry, priv->populate_popup_id);
    priv->populate_popup_id = 0;
  }

  if (priv->entry_menu_popdown_timeout) {
    g_source_remove (priv->entry_menu_popdown_timeout);
    priv->entry_menu_popdown_timeout = 0;
  }

  g_object_get (entry,
                "editing-canceled", &canceled,
                NULL);
  gtk_cell_renderer_stop_editing (GTK_CELL_RENDERER (data), canceled);

  if (canceled)
    return;

  path = g_object_get_data (G_OBJECT (entry), SJ_CELL_RENDERER_TEXT_PATH);
  new_text = gtk_entry_get_text (GTK_ENTRY (entry));

  g_signal_emit_by_name (data, "edited", 0, path, new_text);
}

static gboolean
popdown_timeout (gpointer data)
{
  SjCellRendererTextPrivate *priv;

  priv = sj_cell_renderer_text_get_instance_private (data);

  priv->entry_menu_popdown_timeout = 0;

  if (!gtk_widget_is_focus (GTK_WIDGET (priv->entry)))
    sj_cell_renderer_text_editing_done (GTK_CELL_EDITABLE (priv->entry), data);

  return FALSE;
}

static void
sj_cell_renderer_text_popup_unmap (GtkMenu  *menu,
                                   gpointer  data)
{
  SjCellRendererTextPrivate *priv;

  priv = sj_cell_renderer_text_get_instance_private (data);

  priv->in_entry_menu = FALSE;

  if (priv->entry_menu_popdown_timeout)
    return;

  priv->entry_menu_popdown_timeout = g_timeout_add (500, popdown_timeout,
                                                    data);
  g_source_set_name_by_id (priv->entry_menu_popdown_timeout,
                           "[sjcellrendertext] popdown_timeout");
}

static void
sj_cell_renderer_text_populate_popup (GtkEntry *entry,
                                      GtkMenu  *menu,
                                      gpointer  data)
{
  SjCellRendererTextPrivate *priv;

  priv = sj_cell_renderer_text_get_instance_private (data);

  if (priv->entry_menu_popdown_timeout) {
    g_source_remove (priv->entry_menu_popdown_timeout);
    priv->entry_menu_popdown_timeout = 0;
  }

  priv->in_entry_menu = TRUE;

  g_signal_connect (menu,
                    "unmap",
                    G_CALLBACK (sj_cell_renderer_text_popup_unmap),
                    data);
}

static gboolean
sj_cell_renderer_text_focus_out_event (GtkWidget *entry,
                                       GdkEvent  *event,
                                       gpointer   data)
{
  SjCellRendererTextPrivate *priv;

  priv = sj_cell_renderer_text_get_instance_private (data);

  if (priv->in_entry_menu || gtk_widget_is_focus (entry))
    return FALSE;

  gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
  gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));

  return FALSE;
}

static GtkCellEditable *
sj_cell_renderer_text_start_editing (GtkCellRenderer      *cell,
                                     GdkEvent             *event,
                                     GtkWidget            *widget,
                                     const gchar          *path,
                                     const GdkRectangle   *background_area,
                                     const GdkRectangle   *cell_area,
                                     GtkCellRendererState  flags)
{
  SjCellRendererText *celltext;
  SjCellRendererTextPrivate *priv;
  gfloat xalign, yalign;
  GtkCellRendererMode mode;
  gchar *text = NULL;

  celltext = SJ_CELL_RENDERER_TEXT (cell);
  priv = sj_cell_renderer_text_get_instance_private (celltext);

  /* If the cell isn't editable we return NULL. */
  g_object_get (cell, "mode", &mode, NULL);
  if (mode != GTK_CELL_RENDERER_MODE_EDITABLE)
    return NULL;

  gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);

  priv->entry = gtk_entry_new ();
  g_object_ref_sink (G_OBJECT (priv->entry));

  gtk_entry_set_has_frame (GTK_ENTRY (priv->entry), FALSE);
  gtk_entry_set_alignment (GTK_ENTRY (priv->entry), xalign);

  g_object_get (cell, "text", &text, NULL);
  gtk_entry_set_text (GTK_ENTRY (priv->entry), text);
  g_free (text);

  g_object_set_data_full (G_OBJECT (priv->entry), SJ_CELL_RENDERER_TEXT_PATH,
                          g_strdup (path), g_free);

  gtk_editable_select_region (GTK_EDITABLE (priv->entry), 0, -1);

  priv->in_entry_menu = FALSE;
  if (priv->entry_menu_popdown_timeout) {
    g_source_remove (priv->entry_menu_popdown_timeout);
    priv->entry_menu_popdown_timeout = 0;
  }

  g_signal_connect (priv->entry,
                    "key-press-event",
                    G_CALLBACK (sj_cell_renderer_text_key_press),
                    widget);
  g_signal_connect (priv->entry,
                    "editing-done",
                    G_CALLBACK (sj_cell_renderer_text_editing_done),
                    celltext);
  priv->focus_out_id = g_signal_connect_after (priv->entry,
                                               "focus-out-event",
                                               G_CALLBACK (sj_cell_renderer_text_focus_out_event),
                                               celltext);
  priv->populate_popup_id = g_signal_connect (priv->entry,
                                              "populate-popup",
                                              G_CALLBACK (sj_cell_renderer_text_populate_popup),
                                              celltext);

  gtk_widget_show (priv->entry);

  return GTK_CELL_EDITABLE (priv->entry);
}

static void
sj_cell_renderer_text_init (SjCellRendererText *sj_cell_renderer_text)
{
  /* No Op */
}

static void
sj_cell_renderer_text_finalize (GObject *object)
{
  SjCellRendererTextPrivate *priv;

  priv = sj_cell_renderer_text_get_instance_private (SJ_CELL_RENDERER_TEXT (object));
  g_clear_object (&priv->entry);
  G_OBJECT_CLASS (sj_cell_renderer_text_parent_class)->finalize (object);
}

static void
sj_cell_renderer_text_class_init (SjCellRendererTextClass *class)
{
  GObjectClass* object_class = G_OBJECT_CLASS (class);
  GtkCellRendererClass *renderer_class = GTK_CELL_RENDERER_CLASS (class);

  object_class->finalize = sj_cell_renderer_text_finalize;
  renderer_class->start_editing = sj_cell_renderer_text_start_editing;
}


GtkCellRenderer*
sj_cell_renderer_text_new (void)
{
  return g_object_new (SJ_TYPE_CELL_RENDERER_TEXT, NULL);
}