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