Remove commented out code.
[geeqie.git] / src / ui_tree_edit.c
1 /*
2  * (SLIK) SimpLIstic sKin functions
3  * (C) 2004 John Ellis
4  * Copyright (C) 2008 - 2012 The Geeqie Team
5  *
6  * Author: John Ellis
7  *
8  * This software is released under the GNU General Public License (GNU GPL).
9  * Please read the included file COPYING for more information.
10  * This software comes with no warranty of any kind, use at your own risk!
11  */
12
13 #ifdef HAVE_CONFIG_H
14 #  include "config.h"
15 #endif
16 #include "intl.h"
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include <gtk/gtk.h>
23 #include <gdk/gdkkeysyms.h>
24
25 #include "compat.h"
26
27 #include "ui_tree_edit.h"
28
29 /*
30  *-------------------------------------------------------------------
31  * cell popup editor
32  *-------------------------------------------------------------------
33  */
34
35 static void tree_edit_close(TreeEditData *ted)
36 {
37         gtk_grab_remove(ted->window);
38         gdk_keyboard_ungrab(GDK_CURRENT_TIME);
39         gdk_pointer_ungrab(GDK_CURRENT_TIME);
40
41         gtk_widget_destroy(ted->window);
42
43         g_free(ted->old_name);
44         g_free(ted->new_name);
45         gtk_tree_path_free(ted->path);
46
47         g_free(ted);
48 }
49
50 static void tree_edit_do(TreeEditData *ted)
51 {
52         ted->new_name = g_strdup(gtk_entry_get_text(GTK_ENTRY(ted->entry)));
53
54         if (strcmp(ted->new_name, ted->old_name) != 0)
55                 {
56                 if (ted->edit_func)
57                         {
58                         if (ted->edit_func(ted, ted->old_name, ted->new_name, ted->edit_data))
59                                 {
60                                 /* hmm, should the caller be required to set text instead ? */
61                                 }
62                         }
63                 }
64 }
65
66 static gboolean tree_edit_click_end_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
67 {
68         TreeEditData *ted = data;
69
70         tree_edit_do(ted);
71         tree_edit_close(ted);
72
73         return TRUE;
74 }
75
76 static gboolean tree_edit_click_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
77 {
78         TreeEditData *ted = data;
79         GdkWindow *window = gtk_widget_get_window(ted->window);
80
81         gint x, y;
82         gint w, h;
83
84         gint xr, yr;
85
86         xr = (gint)event->x_root;
87         yr = (gint)event->y_root;
88
89         gdk_window_get_origin(window, &x, &y);
90         w = gdk_window_get_width(window);
91         h = gdk_window_get_height(window);
92
93         if (xr < x || yr < y || xr > x + w || yr > y + h)
94                 {
95                 /* gobble the release event, so it does not propgate to an underlying widget */
96                 g_signal_connect(G_OBJECT(ted->window), "button_release_event",
97                                  G_CALLBACK(tree_edit_click_end_cb), ted);
98                 return TRUE;
99                 }
100         return FALSE;
101 }
102
103 static gboolean tree_edit_key_press_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
104 {
105         TreeEditData *ted = data;
106
107         switch (event->keyval)
108                 {
109                 case GDK_KEY_Return:
110                 case GDK_KEY_KP_Enter:
111                 case GDK_KEY_Tab:               /* ok, we are going to intercept the focus change
112                                            from keyboard and act like return was hit */
113                 case GDK_KEY_ISO_Left_Tab:
114                 case GDK_KEY_Up:
115                 case GDK_KEY_Down:
116                 case GDK_KEY_KP_Up:
117                 case GDK_KEY_KP_Down:
118                 case GDK_KEY_KP_Left:
119                 case GDK_KEY_KP_Right:
120                         tree_edit_do(ted);
121                         tree_edit_close(ted);
122                         break;
123                 case GDK_KEY_Escape:
124                         tree_edit_close(ted);
125                         break;
126                 default:
127                         break;
128                 }
129
130         return FALSE;
131 }
132
133 static gboolean tree_edit_by_path_idle_cb(gpointer data)
134 {
135         TreeEditData *ted = data;
136         GdkRectangle rect;
137         gint x, y, w, h;        /* geometry of cell within tree */
138         gint wx, wy;            /* geometry of tree from root window */
139         gint sx, sw;
140
141         gtk_tree_view_get_cell_area(ted->tree, ted->path, ted->column, &rect);
142
143         x = rect.x;
144         y = rect.y;
145         w = rect.width + 4;
146         h = rect.height + 4;
147
148         if (gtk_tree_view_column_cell_get_position(ted->column, ted->cell, &sx, &sw))
149                 {
150                 x += sx;
151                 w = MAX(w - sx, sw);
152                 }
153
154         gdk_window_get_origin(gtk_tree_view_get_bin_window(ted->tree), &wx, &wy);
155
156         x += wx - 2; /* the -val is to 'fix' alignment of entry position */
157         y += wy - 2;
158
159         /* now show it */
160         gtk_widget_set_size_request(ted->window, w, h);
161         gtk_widget_realize(ted->window);
162         gtk_window_move(GTK_WINDOW(ted->window), x, y);
163         gtk_window_resize(GTK_WINDOW(ted->window), w, h);
164         gtk_widget_show(ted->window);
165
166         /* grab it */
167         gtk_widget_grab_focus(ted->entry);
168         /* explicitely set the focus flag for the entry, for some reason on popup windows this
169          * is not set, and causes no edit cursor to appear ( popups not allowed focus? )
170          */
171         gtk_widget_grab_focus(ted->entry);
172         gtk_grab_add(ted->window);
173         gdk_pointer_grab(gtk_widget_get_window(ted->window), TRUE,
174                          GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_MOTION_MASK,
175                          NULL, NULL, GDK_CURRENT_TIME);
176         gdk_keyboard_grab(gtk_widget_get_window(ted->window), TRUE, GDK_CURRENT_TIME);
177
178         return FALSE;
179 }
180
181 gboolean tree_edit_by_path(GtkTreeView *tree, GtkTreePath *tpath, gint column, const gchar *text,
182                            gboolean (*edit_func)(TreeEditData *, const gchar *, const gchar *, gpointer), gpointer data)
183 {
184         TreeEditData *ted;
185         GtkTreeViewColumn *tcolumn;
186         GtkCellRenderer *cell = NULL;
187         GList *list;
188         GList *work;
189
190         if (!edit_func) return FALSE;
191         if (!gtk_widget_get_visible(GTK_WIDGET(tree))) return FALSE;
192
193         tcolumn = gtk_tree_view_get_column(tree, column);
194         if (!tcolumn) return FALSE;
195
196         list = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(tcolumn));
197         work = list;
198         while (work && !cell)
199                 {
200                 cell = work->data;
201                 if (!GTK_IS_CELL_RENDERER_TEXT(cell))
202                         {
203                         cell = NULL;
204                         }
205                 work = work->next;
206                 }
207
208         g_list_free(list);
209         if (!cell) return FALSE;
210
211         if (!text) text = "";
212
213         ted = g_new0(TreeEditData, 1);
214
215         ted->old_name = g_strdup(text);
216
217         ted->edit_func = edit_func;
218         ted->edit_data = data;
219
220         ted->tree = tree;
221         ted->path = gtk_tree_path_copy(tpath);
222         ted->column = tcolumn;
223         ted->cell = cell;
224
225         gtk_tree_view_scroll_to_cell(ted->tree, ted->path, ted->column, FALSE, 0.0, 0.0);
226
227         /* create the window */
228
229         ted->window = gtk_window_new(GTK_WINDOW_POPUP);
230         gtk_window_set_resizable(GTK_WINDOW(ted->window), FALSE);
231         g_signal_connect(G_OBJECT(ted->window), "button_press_event",
232                          G_CALLBACK(tree_edit_click_cb), ted);
233         g_signal_connect(G_OBJECT(ted->window), "key_press_event",
234                          G_CALLBACK(tree_edit_key_press_cb), ted);
235
236         ted->entry = gtk_entry_new();
237         gtk_entry_set_text(GTK_ENTRY(ted->entry), ted->old_name);
238         gtk_editable_select_region(GTK_EDITABLE(ted->entry), 0, strlen(ted->old_name));
239         gtk_container_add(GTK_CONTAINER(ted->window), ted->entry);
240         gtk_widget_show(ted->entry);
241
242         /* due to the fact that gtktreeview scrolls in an idle loop, we cannot
243          * reliably get the cell position until those scroll priority signals are processed
244          */
245         g_idle_add_full(G_PRIORITY_DEFAULT_IDLE - 2, tree_edit_by_path_idle_cb, ted, NULL);
246
247         return TRUE;
248 }
249
250 /*
251  *-------------------------------------------------------------------
252  * tree cell position retrieval
253  *-------------------------------------------------------------------
254  */
255
256 gboolean tree_view_get_cell_origin(GtkTreeView *widget, GtkTreePath *tpath, gint column, gboolean text_cell_only,
257                                    gint *x, gint *y, gint *width, gint *height)
258 {
259         gint x_origin, y_origin;
260         gint x_offset, y_offset;
261         gint header_size;
262         GtkTreeViewColumn *tv_column;
263         GdkRectangle rect;
264
265         tv_column = gtk_tree_view_get_column(widget, column);
266         if (!tv_column || !tpath) return FALSE;
267
268         /* hmm, appears the rect will not account for X scroll, but does for Y scroll
269          * use x_offset instead for X scroll (sigh)
270          */
271         gtk_tree_view_get_cell_area(widget, tpath, tv_column, &rect);
272         gtk_tree_view_convert_tree_to_widget_coords(widget, 0, 0, &x_offset, &y_offset);
273         gdk_window_get_origin(gtk_widget_get_window(GTK_WIDGET(widget)), &x_origin, &y_origin);
274
275         if (gtk_tree_view_get_headers_visible(widget))
276                 {
277                 GtkAllocation allocation;
278 #if GTK_CHECK_VERSION(3,0,0)
279                 gtk_widget_get_allocation(gtk_tree_view_column_get_button(tv_column), &allocation);
280 #else
281                 gtk_widget_get_allocation(tv_column->button, &allocation);
282 #endif
283                 header_size = allocation.height;
284                 }
285         else
286                 {
287                 header_size = 0;
288                 }
289
290         if (text_cell_only)
291                 {
292                 GtkCellRenderer *cell = NULL;
293                 GList *renderers;
294                 GList *work;
295                 gint cell_x;
296                 gint cell_width;
297
298                 renderers = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(tv_column));
299                 work = renderers;
300                 while (work && !cell)
301                         {
302                         cell = work->data;
303                         work = work->next;
304                         if (!GTK_IS_CELL_RENDERER_TEXT(cell)) cell = NULL;
305                         }
306                 g_list_free(renderers);
307
308                 if (!cell) return FALSE;
309
310                 if (!gtk_tree_view_column_cell_get_position(tv_column, cell, &cell_x, &cell_width))
311                         {
312                         cell_x = 0;
313                         cell_width = rect.width;
314                         }
315                 *x = x_origin + x_offset + rect.x + cell_x;
316                 *width = cell_width;
317                 }
318         else
319                 {
320                 *x = x_origin + x_offset + rect.x;
321                 *width = rect.width;
322                 }
323         *y = y_origin + rect.y + header_size;
324         *height = rect.height;
325         return TRUE;
326 }
327
328 void tree_view_get_cell_clamped(GtkTreeView *widget, GtkTreePath *tpath, gint column, gboolean text_cell_only,
329                                 gint *x, gint *y, gint *width, gint *height)
330 {
331         gint wx, wy, ww, wh;
332         GdkWindow *window;
333
334         window = gtk_widget_get_window(GTK_WIDGET(widget));
335         gdk_window_get_origin(window, &wx, &wy);
336
337         ww = gdk_window_get_width(window);
338         wh = gdk_window_get_height(window);
339
340         if (!tree_view_get_cell_origin(widget, tpath, column, text_cell_only, x,  y, width, height))
341                 {
342                 *x = wx;
343                 *y = wy;
344                 *width = ww;
345                 *height = wh;
346                 return;
347                 }
348
349         *width = MIN(*width, ww);
350         *x = CLAMP(*x, wx, wx + ww - (*width));
351         *y = CLAMP(*y, wy, wy + wh);
352         *height = MIN(*height, wy + wh - (*y));
353 }
354
355 /* an implementation that uses gtk_tree_view_get_visible_range */
356 gint tree_view_row_get_visibility(GtkTreeView *widget, GtkTreeIter *iter, gboolean fully_visible)
357 {
358         GtkTreeModel *store;
359         GtkTreePath *tpath, *start_path, *end_path;
360         gint ret = 0;
361
362         if (!gtk_tree_view_get_visible_range(widget, &start_path, &end_path)) return -1; /* we will most probably scroll down, needed for tree_view_row_make_visible */
363
364         store = gtk_tree_view_get_model(widget);
365         tpath = gtk_tree_model_get_path(store, iter);
366
367         if (fully_visible)
368                 {
369                 if (gtk_tree_path_compare(tpath, start_path) <= 0)
370                         {
371                         ret = -1;
372                         }
373                 else if (gtk_tree_path_compare(tpath, end_path) >= 0)
374                         {
375                         ret = 1;
376                         }
377                 }
378         else
379                 {
380                 if (gtk_tree_path_compare(tpath, start_path) < 0)
381                         {
382                         ret = -1;
383                         }
384                 else if (gtk_tree_path_compare(tpath, end_path) > 0)
385                         {
386                         ret = 1;
387                         }
388                 }
389
390         gtk_tree_path_free(tpath);
391         gtk_tree_path_free(start_path);
392         gtk_tree_path_free(end_path);
393         return ret;
394 }
395
396 gint tree_view_row_make_visible(GtkTreeView *widget, GtkTreeIter *iter, gboolean center)
397 {
398         GtkTreePath *tpath;
399         gint vis;
400
401         vis = tree_view_row_get_visibility(widget, iter, TRUE);
402
403         tpath = gtk_tree_model_get_path(gtk_tree_view_get_model(widget), iter);
404         if (center && vis != 0)
405                 {
406                 gtk_tree_view_scroll_to_cell(widget, tpath, NULL, TRUE, 0.5, 0.0);
407                 }
408         else if (vis < 0)
409                 {
410                 gtk_tree_view_scroll_to_cell(widget, tpath, NULL, TRUE, 0.0, 0.0);
411                 }
412         else if (vis > 0)
413                 {
414                 gtk_tree_view_scroll_to_cell(widget, tpath, NULL, TRUE, 1.0, 0.0);
415                 }
416         gtk_tree_path_free(tpath);
417
418         return vis;
419 }
420
421 gboolean tree_view_move_cursor_away(GtkTreeView *widget, GtkTreeIter *iter, gboolean only_selected)
422 {
423         GtkTreeModel *store;
424         GtkTreePath *tpath;
425         GtkTreePath *fpath;
426         gboolean move = FALSE;
427
428         if (!iter) return FALSE;
429
430         store = gtk_tree_view_get_model(widget);
431         tpath = gtk_tree_model_get_path(store, iter);
432         gtk_tree_view_get_cursor(widget, &fpath, NULL);
433
434         if (fpath && gtk_tree_path_compare(tpath, fpath) == 0)
435                 {
436                 GtkTreeSelection *selection;
437
438                 selection = gtk_tree_view_get_selection(widget);
439
440                 if (!only_selected ||
441                     gtk_tree_selection_path_is_selected(selection, tpath))
442                         {
443                         GtkTreeIter current;
444
445                         current = *iter;
446                         if (gtk_tree_model_iter_next(store, &current))
447                                 {
448                                 gtk_tree_path_next(tpath);
449                                 move = TRUE;
450                                 }
451                         else if (gtk_tree_path_prev(tpath) &&
452                                  gtk_tree_model_get_iter(store, &current, tpath))
453                                 {
454                                 move = TRUE;
455                                 }
456
457                         if (move)
458                                 {
459                                 gtk_tree_view_set_cursor(widget, tpath, NULL, FALSE);
460                                 }
461                         }
462                 }
463
464         gtk_tree_path_free(tpath);
465         if (fpath) gtk_tree_path_free(fpath);
466
467         return move;
468 }
469
470 gint tree_path_to_row(GtkTreePath *tpath)
471 {
472         gint *indices;
473
474         indices = gtk_tree_path_get_indices(tpath);
475         if (indices) return indices[0];
476
477         return -1;
478 }
479
480
481 /*
482  *-------------------------------------------------------------------
483  * color utilities
484  *-------------------------------------------------------------------
485  */
486
487 void shift_color(GdkColor *src, gshort val, gint direction)
488 {
489         gshort cs;
490
491         if (val == -1)
492                 {
493                 val = STYLE_SHIFT_STANDARD;
494                 }
495         else
496                 {
497                 val = CLAMP(val, 1, 100);
498                 }
499         cs = 0xffff / 100 * val;
500
501         /* up or down ? */
502         if (direction < 0 ||
503             (direction == 0 &&((gint)src->red + (gint)src->green + (gint)src->blue) / 3 > 0xffff / 2))
504                 {
505                 src->red = MAX(0 , src->red - cs);
506                 src->green = MAX(0 , src->green - cs);
507                 src->blue = MAX(0 , src->blue - cs);
508                 }
509         else
510                 {
511                 src->red = MIN(0xffff, src->red + cs);
512                 src->green = MIN(0xffff, src->green + cs);
513                 src->blue = MIN(0xffff, src->blue + cs);
514                 }
515 }
516
517 /* darkens or lightens a style's color for given state
518  * esp. useful for alternating dark/light in (c)lists
519  */
520 void style_shift_color(GtkStyle *style, GtkStateType type, gshort shift_value, gint direction)
521 {
522         if (!style) return;
523
524         shift_color(&style->base[type], shift_value, direction);
525         shift_color(&style->bg[type], shift_value, direction);
526 }
527
528 /*
529  *-------------------------------------------------------------------
530  * auto scroll by mouse position
531  *-------------------------------------------------------------------
532  */
533
534 #define AUTO_SCROLL_DEFAULT_SPEED 100
535 #define AUTO_SCROLL_DEFAULT_REGION 20
536
537 typedef struct _AutoScrollData AutoScrollData;
538 struct _AutoScrollData
539 {
540         guint timer_id; /* event source id */
541         gint region_size;
542         GtkWidget *widget;
543         GtkAdjustment *adj;
544         gint max_step;
545
546         gint (*notify_func)(GtkWidget *, gint, gint, gpointer);
547         gpointer notify_data;
548 };
549
550 void widget_auto_scroll_stop(GtkWidget *widget)
551 {
552         AutoScrollData *sd;
553
554         sd = g_object_get_data(G_OBJECT(widget), "autoscroll");
555         if (!sd) return;
556         g_object_set_data(G_OBJECT(widget), "autoscroll", NULL);
557
558         if (sd->timer_id) g_source_remove(sd->timer_id);
559         g_free(sd);
560 }
561
562 static gboolean widget_auto_scroll_cb(gpointer data)
563 {
564         AutoScrollData *sd = data;
565         GdkWindow *window;
566         gint x, y;
567         gint w, h;
568         gint amt = 0;
569
570         if (sd->max_step < sd->region_size)
571                 {
572                 sd->max_step = MIN(sd->region_size, sd->max_step + 2);
573                 }
574
575         window = gtk_widget_get_window(sd->widget);
576         gdk_window_get_pointer(window, &x, &y, NULL);
577         w = gdk_window_get_width(window);
578         h = gdk_window_get_height(window);
579
580         if (x < 0 || x >= w || y < 0 || y >= h)
581                 {
582                 sd->timer_id = 0;
583                 widget_auto_scroll_stop(sd->widget);
584                 return FALSE;
585                 }
586
587         if (h < sd->region_size * 3)
588                 {
589                 /* height is cramped, nicely divide into three equal regions */
590                 if (y < h / 3 || y > h / 3 * 2)
591                         {
592                         amt = (y < h / 2) ? 0 - ((h / 2) - y) : y - (h / 2);
593                         }
594                 }
595         else if (y < sd->region_size)
596                 {
597                 amt = 0 - (sd->region_size - y);
598                 }
599         else if (y >= h - sd->region_size)
600                 {
601                 amt = y - (h - sd->region_size);
602                 }
603
604         if (amt != 0)
605                 {
606                 amt = CLAMP(amt, 0 - sd->max_step, sd->max_step);
607
608                 if (gtk_adjustment_get_value(sd->adj) != CLAMP(gtk_adjustment_get_value(sd->adj) + amt, gtk_adjustment_get_lower(sd->adj), gtk_adjustment_get_upper(sd->adj) - gtk_adjustment_get_page_size(sd->adj)))
609                         {
610                         /* only notify when scrolling is needed */
611                         if (sd->notify_func && !sd->notify_func(sd->widget, x, y, sd->notify_data))
612                                 {
613                                 sd->timer_id = 0;
614                                 widget_auto_scroll_stop(sd->widget);
615                                 return FALSE;
616                                 }
617
618                         gtk_adjustment_set_value(sd->adj,
619                                 CLAMP(gtk_adjustment_get_value(sd->adj) + amt, gtk_adjustment_get_lower(sd->adj), gtk_adjustment_get_upper(sd->adj) - gtk_adjustment_get_page_size(sd->adj)));
620                         }
621                 }
622
623         return TRUE;
624 }
625
626 gint widget_auto_scroll_start(GtkWidget *widget, GtkAdjustment *v_adj, gint scroll_speed, gint region_size,
627                               gint (*notify_func)(GtkWidget *widget, gint x, gint y, gpointer data), gpointer notify_data)
628 {
629         AutoScrollData *sd;
630
631         if (!widget || !v_adj) return 0;
632         if (g_object_get_data(G_OBJECT(widget), "autoscroll")) return 0;
633         if (scroll_speed < 1) scroll_speed = AUTO_SCROLL_DEFAULT_SPEED;
634         if (region_size < 1) region_size = AUTO_SCROLL_DEFAULT_REGION;
635
636         sd = g_new0(AutoScrollData, 1);
637         sd->widget = widget;
638         sd->adj = v_adj;
639         sd->region_size = region_size;
640         sd->max_step = 1;
641         sd->timer_id = g_timeout_add(scroll_speed, widget_auto_scroll_cb, sd);
642
643         sd->notify_func = notify_func;
644         sd->notify_data = notify_data;
645
646         g_object_set_data(G_OBJECT(widget), "autoscroll", sd);
647
648         return scroll_speed;
649 }
650
651
652 /*
653  *-------------------------------------------------------------------
654  * GList utils
655  *-------------------------------------------------------------------
656  */
657
658 GList *uig_list_insert_link(GList *list, GList *link, gpointer data)
659 {
660         GList *new_list;
661
662         if (!list || link == list) return g_list_prepend(list, data);
663         if (!link) return g_list_append(list, data);
664
665         new_list = g_list_alloc();
666         new_list->data = data;
667
668         if (link->prev)
669                 {
670                 link->prev->next = new_list;
671                 new_list->prev = link->prev;
672                 }
673         else
674                 {
675                 list = new_list;
676                 }
677         link->prev = new_list;
678         new_list->next = link;
679
680         return list;
681 }
682
683 GList *uig_list_insert_list(GList *parent, GList *insert_link, GList *list)
684 {
685         GList *end;
686
687         if (!insert_link) return g_list_concat(parent, list);
688         if (insert_link == parent) return g_list_concat(list, parent);
689         if (!parent) return list;
690         if (!list) return parent;
691
692         end  = g_list_last(list);
693
694         if (insert_link->prev) insert_link->prev->next = list;
695         list->prev = insert_link->prev;
696         insert_link->prev = end;
697         end->next = insert_link;
698
699         return parent;
700 }
701 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */