2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
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.
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.
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.
32 #include <gdk/gdkkeysyms.h>
36 #include "ui_tree_edit.h"
39 *-------------------------------------------------------------------
41 *-------------------------------------------------------------------
44 static void tree_edit_close(TreeEditData *ted)
46 gtk_grab_remove(ted->window);
47 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
48 gdk_pointer_ungrab(GDK_CURRENT_TIME);
50 gtk_widget_destroy(ted->window);
52 g_free(ted->old_name);
53 g_free(ted->new_name);
54 gtk_tree_path_free(ted->path);
59 static void tree_edit_do(TreeEditData *ted)
61 ted->new_name = g_strdup(gtk_entry_get_text(GTK_ENTRY(ted->entry)));
63 if (strcmp(ted->new_name, ted->old_name) != 0)
67 if (ted->edit_func(ted, ted->old_name, ted->new_name, ted->edit_data))
69 /* hmm, should the caller be required to set text instead ? */
75 static gboolean tree_edit_click_end_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
77 TreeEditData *ted = data;
85 static gboolean tree_edit_click_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
87 TreeEditData *ted = data;
88 GdkWindow *window = gtk_widget_get_window(ted->window);
95 xr = (gint)event->x_root;
96 yr = (gint)event->y_root;
98 gdk_window_get_origin(window, &x, &y);
99 w = gdk_window_get_width(window);
100 h = gdk_window_get_height(window);
102 if (xr < x || yr < y || xr > x + w || yr > y + h)
104 /* gobble the release event, so it does not propgate to an underlying widget */
105 g_signal_connect(G_OBJECT(ted->window), "button_release_event",
106 G_CALLBACK(tree_edit_click_end_cb), ted);
112 static gboolean tree_edit_key_press_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
114 TreeEditData *ted = data;
116 switch (event->keyval)
119 case GDK_KEY_KP_Enter:
120 case GDK_KEY_Tab: /* ok, we are going to intercept the focus change
121 from keyboard and act like return was hit */
122 case GDK_KEY_ISO_Left_Tab:
126 case GDK_KEY_KP_Down:
127 case GDK_KEY_KP_Left:
128 case GDK_KEY_KP_Right:
130 tree_edit_close(ted);
133 tree_edit_close(ted);
142 static gboolean tree_edit_by_path_idle_cb(gpointer data)
144 TreeEditData *ted = data;
146 gint x, y, w, h; /* geometry of cell within tree */
147 gint wx, wy; /* geometry of tree from root window */
150 gtk_tree_view_get_cell_area(ted->tree, ted->path, ted->column, &rect);
157 if (gtk_tree_view_column_cell_get_position(ted->column, ted->cell, &sx, &sw))
163 gdk_window_get_origin(gtk_tree_view_get_bin_window(ted->tree), &wx, &wy);
165 x += wx - 2; /* the -val is to 'fix' alignment of entry position */
169 gtk_widget_set_size_request(ted->window, w, h);
170 gtk_widget_realize(ted->window);
171 gtk_window_move(GTK_WINDOW(ted->window), x, y);
172 gtk_window_resize(GTK_WINDOW(ted->window), w, h);
173 gtk_widget_show(ted->window);
176 gtk_widget_grab_focus(ted->entry);
177 /* explicitely set the focus flag for the entry, for some reason on popup windows this
178 * is not set, and causes no edit cursor to appear ( popups not allowed focus? )
180 gtk_widget_grab_focus(ted->entry);
181 gtk_grab_add(ted->window);
182 gdk_pointer_grab(gtk_widget_get_window(ted->window), TRUE,
183 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_MOTION_MASK,
184 NULL, NULL, GDK_CURRENT_TIME);
185 gdk_keyboard_grab(gtk_widget_get_window(ted->window), TRUE, GDK_CURRENT_TIME);
190 gboolean tree_edit_by_path(GtkTreeView *tree, GtkTreePath *tpath, gint column, const gchar *text,
191 gboolean (*edit_func)(TreeEditData *, const gchar *, const gchar *, gpointer), gpointer data)
194 GtkTreeViewColumn *tcolumn;
195 GtkCellRenderer *cell = NULL;
199 if (!edit_func) return FALSE;
200 if (!gtk_widget_get_visible(GTK_WIDGET(tree))) return FALSE;
202 tcolumn = gtk_tree_view_get_column(tree, column);
203 if (!tcolumn) return FALSE;
205 list = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(tcolumn));
207 while (work && !cell)
210 if (!GTK_IS_CELL_RENDERER_TEXT(cell))
218 if (!cell) return FALSE;
220 if (!text) text = "";
222 ted = g_new0(TreeEditData, 1);
224 ted->old_name = g_strdup(text);
226 ted->edit_func = edit_func;
227 ted->edit_data = data;
230 ted->path = gtk_tree_path_copy(tpath);
231 ted->column = tcolumn;
234 gtk_tree_view_scroll_to_cell(ted->tree, ted->path, ted->column, FALSE, 0.0, 0.0);
236 /* create the window */
238 ted->window = gtk_window_new(GTK_WINDOW_POPUP);
239 gtk_window_set_resizable(GTK_WINDOW(ted->window), FALSE);
240 g_signal_connect(G_OBJECT(ted->window), "button_press_event",
241 G_CALLBACK(tree_edit_click_cb), ted);
242 g_signal_connect(G_OBJECT(ted->window), "key_press_event",
243 G_CALLBACK(tree_edit_key_press_cb), ted);
245 ted->entry = gtk_entry_new();
246 gtk_entry_set_text(GTK_ENTRY(ted->entry), ted->old_name);
247 gtk_editable_select_region(GTK_EDITABLE(ted->entry), 0, strlen(ted->old_name));
248 gtk_container_add(GTK_CONTAINER(ted->window), ted->entry);
249 gtk_widget_show(ted->entry);
251 /* due to the fact that gtktreeview scrolls in an idle loop, we cannot
252 * reliably get the cell position until those scroll priority signals are processed
254 g_idle_add_full(G_PRIORITY_DEFAULT_IDLE - 2, tree_edit_by_path_idle_cb, ted, NULL);
260 *-------------------------------------------------------------------
261 * tree cell position retrieval
262 *-------------------------------------------------------------------
265 gboolean tree_view_get_cell_origin(GtkTreeView *widget, GtkTreePath *tpath, gint column, gboolean text_cell_only,
266 gint *x, gint *y, gint *width, gint *height)
268 gint x_origin, y_origin;
269 gint x_offset, y_offset;
271 GtkTreeViewColumn *tv_column;
274 tv_column = gtk_tree_view_get_column(widget, column);
275 if (!tv_column || !tpath) return FALSE;
277 /* hmm, appears the rect will not account for X scroll, but does for Y scroll
278 * use x_offset instead for X scroll (sigh)
280 gtk_tree_view_get_cell_area(widget, tpath, tv_column, &rect);
281 gtk_tree_view_convert_tree_to_widget_coords(widget, 0, 0, &x_offset, &y_offset);
282 gdk_window_get_origin(gtk_widget_get_window(GTK_WIDGET(widget)), &x_origin, &y_origin);
284 if (gtk_tree_view_get_headers_visible(widget))
286 GtkAllocation allocation;
287 #if GTK_CHECK_VERSION(3,0,0)
288 gtk_widget_get_allocation(gtk_tree_view_column_get_button(tv_column), &allocation);
290 gtk_widget_get_allocation(tv_column->button, &allocation);
292 header_size = allocation.height;
301 GtkCellRenderer *cell = NULL;
307 renderers = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(tv_column));
309 while (work && !cell)
313 if (!GTK_IS_CELL_RENDERER_TEXT(cell)) cell = NULL;
315 g_list_free(renderers);
317 if (!cell) return FALSE;
319 if (!gtk_tree_view_column_cell_get_position(tv_column, cell, &cell_x, &cell_width))
322 cell_width = rect.width;
324 *x = x_origin + x_offset + rect.x + cell_x;
329 *x = x_origin + x_offset + rect.x;
332 *y = y_origin + rect.y + header_size;
333 *height = rect.height;
337 void tree_view_get_cell_clamped(GtkTreeView *widget, GtkTreePath *tpath, gint column, gboolean text_cell_only,
338 gint *x, gint *y, gint *width, gint *height)
343 window = gtk_widget_get_window(GTK_WIDGET(widget));
344 gdk_window_get_origin(window, &wx, &wy);
346 ww = gdk_window_get_width(window);
347 wh = gdk_window_get_height(window);
349 if (!tree_view_get_cell_origin(widget, tpath, column, text_cell_only, x, y, width, height))
358 *width = MIN(*width, ww);
359 *x = CLAMP(*x, wx, wx + ww - (*width));
360 *y = CLAMP(*y, wy, wy + wh);
361 *height = MIN(*height, wy + wh - (*y));
364 /* an implementation that uses gtk_tree_view_get_visible_range */
365 gint tree_view_row_get_visibility(GtkTreeView *widget, GtkTreeIter *iter, gboolean fully_visible)
368 GtkTreePath *tpath, *start_path, *end_path;
371 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 */
373 store = gtk_tree_view_get_model(widget);
374 tpath = gtk_tree_model_get_path(store, iter);
378 if (gtk_tree_path_compare(tpath, start_path) <= 0)
382 else if (gtk_tree_path_compare(tpath, end_path) >= 0)
389 if (gtk_tree_path_compare(tpath, start_path) < 0)
393 else if (gtk_tree_path_compare(tpath, end_path) > 0)
399 gtk_tree_path_free(tpath);
400 gtk_tree_path_free(start_path);
401 gtk_tree_path_free(end_path);
405 gint tree_view_row_make_visible(GtkTreeView *widget, GtkTreeIter *iter, gboolean center)
410 vis = tree_view_row_get_visibility(widget, iter, TRUE);
412 tpath = gtk_tree_model_get_path(gtk_tree_view_get_model(widget), iter);
413 if (center && vis != 0)
415 gtk_tree_view_scroll_to_cell(widget, tpath, NULL, TRUE, 0.5, 0.0);
419 gtk_tree_view_scroll_to_cell(widget, tpath, NULL, TRUE, 0.0, 0.0);
423 gtk_tree_view_scroll_to_cell(widget, tpath, NULL, TRUE, 1.0, 0.0);
425 gtk_tree_path_free(tpath);
430 gboolean tree_view_move_cursor_away(GtkTreeView *widget, GtkTreeIter *iter, gboolean only_selected)
435 gboolean move = FALSE;
437 if (!iter) return FALSE;
439 store = gtk_tree_view_get_model(widget);
440 tpath = gtk_tree_model_get_path(store, iter);
441 gtk_tree_view_get_cursor(widget, &fpath, NULL);
443 if (fpath && gtk_tree_path_compare(tpath, fpath) == 0)
445 GtkTreeSelection *selection;
447 selection = gtk_tree_view_get_selection(widget);
449 if (!only_selected ||
450 gtk_tree_selection_path_is_selected(selection, tpath))
455 if (gtk_tree_model_iter_next(store, ¤t))
457 gtk_tree_path_next(tpath);
460 else if (gtk_tree_path_prev(tpath) &&
461 gtk_tree_model_get_iter(store, ¤t, tpath))
468 gtk_tree_view_set_cursor(widget, tpath, NULL, FALSE);
473 gtk_tree_path_free(tpath);
474 if (fpath) gtk_tree_path_free(fpath);
479 gint tree_path_to_row(GtkTreePath *tpath)
483 indices = gtk_tree_path_get_indices(tpath);
484 if (indices) return indices[0];
491 *-------------------------------------------------------------------
493 *-------------------------------------------------------------------
496 void shift_color(GdkColor *src, gshort val, gint direction)
502 val = STYLE_SHIFT_STANDARD;
506 val = CLAMP(val, 1, 100);
508 cs = 0xffff / 100 * val;
512 (direction == 0 &&((gint)src->red + (gint)src->green + (gint)src->blue) / 3 > 0xffff / 2))
514 src->red = MAX(0 , src->red - cs);
515 src->green = MAX(0 , src->green - cs);
516 src->blue = MAX(0 , src->blue - cs);
520 src->red = MIN(0xffff, src->red + cs);
521 src->green = MIN(0xffff, src->green + cs);
522 src->blue = MIN(0xffff, src->blue + cs);
526 /* darkens or lightens a style's color for given state
527 * esp. useful for alternating dark/light in (c)lists
529 void style_shift_color(GtkStyle *style, GtkStateType type, gshort shift_value, gint direction)
533 shift_color(&style->base[type], shift_value, direction);
534 shift_color(&style->bg[type], shift_value, direction);
538 *-------------------------------------------------------------------
539 * auto scroll by mouse position
540 *-------------------------------------------------------------------
543 #define AUTO_SCROLL_DEFAULT_SPEED 100
544 #define AUTO_SCROLL_DEFAULT_REGION 20
546 typedef struct _AutoScrollData AutoScrollData;
547 struct _AutoScrollData
549 guint timer_id; /* event source id */
555 gint (*notify_func)(GtkWidget *, gint, gint, gpointer);
556 gpointer notify_data;
559 void widget_auto_scroll_stop(GtkWidget *widget)
563 sd = g_object_get_data(G_OBJECT(widget), "autoscroll");
565 g_object_set_data(G_OBJECT(widget), "autoscroll", NULL);
567 if (sd->timer_id) g_source_remove(sd->timer_id);
571 static gboolean widget_auto_scroll_cb(gpointer data)
573 AutoScrollData *sd = data;
578 #if GTK_CHECK_VERSION(3,0,0)
579 GdkDeviceManager *device_manager;
583 if (sd->max_step < sd->region_size)
585 sd->max_step = MIN(sd->region_size, sd->max_step + 2);
588 window = gtk_widget_get_window(sd->widget);
589 #if GTK_CHECK_VERSION(3,0,0)
590 device_manager = gdk_display_get_device_manager(gdk_window_get_display(window));
591 device = gdk_device_manager_get_client_pointer(device_manager);
592 gdk_window_get_device_position(window, device, &x, &y, NULL);
594 gdk_window_get_pointer(window, &x, &y, NULL);
596 w = gdk_window_get_width(window);
597 h = gdk_window_get_height(window);
599 if (x < 0 || x >= w || y < 0 || y >= h)
602 widget_auto_scroll_stop(sd->widget);
606 if (h < sd->region_size * 3)
608 /* height is cramped, nicely divide into three equal regions */
609 if (y < h / 3 || y > h / 3 * 2)
611 amt = (y < h / 2) ? 0 - ((h / 2) - y) : y - (h / 2);
614 else if (y < sd->region_size)
616 amt = 0 - (sd->region_size - y);
618 else if (y >= h - sd->region_size)
620 amt = y - (h - sd->region_size);
625 amt = CLAMP(amt, 0 - sd->max_step, sd->max_step);
627 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)))
629 /* only notify when scrolling is needed */
630 if (sd->notify_func && !sd->notify_func(sd->widget, x, y, sd->notify_data))
633 widget_auto_scroll_stop(sd->widget);
637 gtk_adjustment_set_value(sd->adj,
638 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)));
645 gint widget_auto_scroll_start(GtkWidget *widget, GtkAdjustment *v_adj, gint scroll_speed, gint region_size,
646 gint (*notify_func)(GtkWidget *widget, gint x, gint y, gpointer data), gpointer notify_data)
650 if (!widget || !v_adj) return 0;
651 if (g_object_get_data(G_OBJECT(widget), "autoscroll")) return 0;
652 if (scroll_speed < 1) scroll_speed = AUTO_SCROLL_DEFAULT_SPEED;
653 if (region_size < 1) region_size = AUTO_SCROLL_DEFAULT_REGION;
655 sd = g_new0(AutoScrollData, 1);
658 sd->region_size = region_size;
660 sd->timer_id = g_timeout_add(scroll_speed, widget_auto_scroll_cb, sd);
662 sd->notify_func = notify_func;
663 sd->notify_data = notify_data;
665 g_object_set_data(G_OBJECT(widget), "autoscroll", sd);
672 *-------------------------------------------------------------------
674 *-------------------------------------------------------------------
677 GList *uig_list_insert_link(GList *list, GList *link, gpointer data)
681 if (!list || link == list) return g_list_prepend(list, data);
682 if (!link) return g_list_append(list, data);
684 new_list = g_list_alloc();
685 new_list->data = data;
689 link->prev->next = new_list;
690 new_list->prev = link->prev;
696 link->prev = new_list;
697 new_list->next = link;
702 GList *uig_list_insert_list(GList *parent, GList *insert_link, GList *list)
706 if (!insert_link) return g_list_concat(parent, list);
707 if (insert_link == parent) return g_list_concat(list, parent);
708 if (!parent) return list;
709 if (!list) return parent;
711 end = g_list_last(list);
713 if (insert_link->prev) insert_link->prev->next = list;
714 list->prev = insert_link->prev;
715 insert_link->prev = end;
716 end->next = insert_link;
720 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */