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