fixed compilation with gtk 2.20
[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_CHECK_VERSION(2,20,0)
192         if (!gtk_widget_get_visible(GTK_WIDGET(tree))) return FALSE;
193 #else
194         if (!GTK_WIDGET_VISIBLE(tree)) return FALSE;
195 #endif
196
197         tcolumn = gtk_tree_view_get_column(tree, column);
198         if (!tcolumn) return FALSE;
199
200 #if GTK_CHECK_VERSION(2,18,0)
201         list = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(tcolumn));
202 #else
203         list = gtk_tree_view_column_get_cell_renderers(tcolumn);
204 #endif
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, FALSE, 0.0, 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 #if GTK_CHECK_VERSION(2,12,0)
281         gtk_tree_view_convert_tree_to_widget_coords(widget, 0, 0, &x_offset, &y_offset);
282 #else
283         gtk_tree_view_tree_to_widget_coords(widget, 0, 0, &x_offset, &y_offset);
284 #endif
285         gdk_window_get_origin(gtk_widget_get_window(GTK_WIDGET(widget)), &x_origin, &y_origin);
286
287         if (gtk_tree_view_get_headers_visible(widget))
288                 {
289                 GtkAllocation allocation;
290 #if GTK_CHECK_VERSION(3,0,0)
291                 gtk_widget_get_allocation(gtk_tree_view_column_get_button(tv_column), &allocation);
292 #else
293                 gtk_widget_get_allocation(tv_column->button, &allocation);
294 #endif
295                 header_size = allocation.height;
296                 }
297         else
298                 {
299                 header_size = 0;
300                 }
301
302         if (text_cell_only)
303                 {
304                 GtkCellRenderer *cell = NULL;
305                 GList *renderers;
306                 GList *work;
307                 gint cell_x;
308                 gint cell_width;
309
310 #if GTK_CHECK_VERSION(2,18,0)
311                 renderers = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(tv_column));
312 #else
313                 renderers = gtk_tree_view_column_get_cell_renderers(tv_column);
314 #endif
315                 work = renderers;
316                 while (work && !cell)
317                         {
318                         cell = work->data;
319                         work = work->next;
320                         if (!GTK_IS_CELL_RENDERER_TEXT(cell)) cell = NULL;
321                         }
322                 g_list_free(renderers);
323
324                 if (!cell) return FALSE;
325
326                 if (!gtk_tree_view_column_cell_get_position(tv_column, cell, &cell_x, &cell_width))
327                         {
328                         cell_x = 0;
329                         cell_width = rect.width;
330                         }
331                 *x = x_origin + x_offset + rect.x + cell_x;
332                 *width = cell_width;
333                 }
334         else
335                 {
336                 *x = x_origin + x_offset + rect.x;
337                 *width = rect.width;
338                 }
339         *y = y_origin + rect.y + header_size;
340         *height = rect.height;
341         return TRUE;
342 }
343
344 void tree_view_get_cell_clamped(GtkTreeView *widget, GtkTreePath *tpath, gint column, gboolean text_cell_only,
345                                 gint *x, gint *y, gint *width, gint *height)
346 {
347         gint wx, wy, ww, wh;
348         GdkWindow *window;
349
350         window = gtk_widget_get_window(GTK_WIDGET(widget));
351         gdk_window_get_origin(window, &wx, &wy);
352
353         ww = gdk_window_get_width(window);
354         wh = gdk_window_get_height(window);
355
356         if (!tree_view_get_cell_origin(widget, tpath, column, text_cell_only, x,  y, width, height))
357                 {
358                 *x = wx;
359                 *y = wy;
360                 *width = ww;
361                 *height = wh;
362                 return;
363                 }
364
365         *width = MIN(*width, ww);
366         *x = CLAMP(*x, wx, wx + ww - (*width));
367         *y = CLAMP(*y, wy, wy + wh);
368         *height = MIN(*height, wy + wh - (*y));
369 }
370
371 #if GTK_CHECK_VERSION(2,8,0)
372 /* an implementation that uses gtk_tree_view_get_visible_range */
373 gint tree_view_row_get_visibility(GtkTreeView *widget, GtkTreeIter *iter, gboolean fully_visible)
374 {
375         GtkTreeModel *store;
376         GtkTreePath *tpath, *start_path, *end_path;
377         gint ret = 0;
378
379         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 */
380
381         store = gtk_tree_view_get_model(widget);
382         tpath = gtk_tree_model_get_path(store, iter);
383
384         if (fully_visible)
385                 {
386                 if (gtk_tree_path_compare(tpath, start_path) <= 0)
387                         {
388                         ret = -1;
389                         }
390                 else if (gtk_tree_path_compare(tpath, end_path) >= 0)
391                         {
392                         ret = 1;
393                         }
394                 }
395         else
396                 {
397                 if (gtk_tree_path_compare(tpath, start_path) < 0)
398                         {
399                         ret = -1;
400                         }
401                 else if (gtk_tree_path_compare(tpath, end_path) > 0)
402                         {
403                         ret = 1;
404                         }
405                 }
406
407         gtk_tree_path_free(tpath);
408         gtk_tree_path_free(start_path);
409         gtk_tree_path_free(end_path);
410         return ret;
411 }
412
413 #else 
414 /* an implementation that uses gtk_tree_view_get_visible_rect, it seems to be more error prone than the variant above */
415
416 gint tree_view_row_get_visibility(GtkTreeView *widget, GtkTreeIter *iter, gboolean fully_visible)
417 {
418         GtkTreeModel *store;
419         GtkTreePath *tpath;
420         gint cx, cy;
421
422         GdkRectangle vrect;
423         GdkRectangle crect;
424
425         if (!GTK_WIDGET_REALIZED(GTK_WIDGET(widget))) return -1; /* we will most probably scroll down, needed for tree_view_row_make_visible */
426
427         store = gtk_tree_view_get_model(widget);
428         tpath = gtk_tree_model_get_path(store, iter);
429
430         gtk_tree_view_get_visible_rect(widget, &vrect);
431         gtk_tree_view_get_cell_area(widget, tpath, NULL, &crect);
432         gtk_tree_path_free(tpath);
433
434
435 #if GTK_CHECK_VERSION(2,12,0)
436         gtk_tree_view_convert_widget_to_tree_coords(widget, crect.x, crect.y, &cx, &cy);
437 #else
438         gtk_tree_view_widget_to_tree_coords(widget, crect.x, crect.y, &cx, &cy);
439 #endif
440
441         if (fully_visible)
442                 {
443                 if (cy < vrect.y) return -1;
444                 if (cy + crect.height > vrect.y + vrect.height) return 1;
445                 return 0;
446                 }
447
448         if (cy + crect.height < vrect.y) return -1;
449         if (cy > vrect.y + vrect.height) return 1;
450         return 0;
451 }
452 #endif
453
454 gint tree_view_row_make_visible(GtkTreeView *widget, GtkTreeIter *iter, gboolean center)
455 {
456         GtkTreePath *tpath;
457         gint vis;
458
459         vis = tree_view_row_get_visibility(widget, iter, TRUE);
460
461         tpath = gtk_tree_model_get_path(gtk_tree_view_get_model(widget), iter);
462         if (center && vis != 0)
463                 {
464                 gtk_tree_view_scroll_to_cell(widget, tpath, NULL, TRUE, 0.5, 0.0);
465                 }
466         else if (vis < 0)
467                 {
468                 gtk_tree_view_scroll_to_cell(widget, tpath, NULL, TRUE, 0.0, 0.0);
469                 }
470         else if (vis > 0)
471                 {
472                 gtk_tree_view_scroll_to_cell(widget, tpath, NULL, TRUE, 1.0, 0.0);
473                 }
474         gtk_tree_path_free(tpath);
475
476         return vis;
477 }
478
479 gboolean tree_view_move_cursor_away(GtkTreeView *widget, GtkTreeIter *iter, gboolean only_selected)
480 {
481         GtkTreeModel *store;
482         GtkTreePath *tpath;
483         GtkTreePath *fpath;
484         gboolean move = FALSE;
485
486         if (!iter) return FALSE;
487
488         store = gtk_tree_view_get_model(widget);
489         tpath = gtk_tree_model_get_path(store, iter);
490         gtk_tree_view_get_cursor(widget, &fpath, NULL);
491
492         if (fpath && gtk_tree_path_compare(tpath, fpath) == 0)
493                 {
494                 GtkTreeSelection *selection;
495
496                 selection = gtk_tree_view_get_selection(widget);
497
498                 if (!only_selected ||
499                     gtk_tree_selection_path_is_selected(selection, tpath))
500                         {
501                         GtkTreeIter current;
502
503                         current = *iter;
504                         if (gtk_tree_model_iter_next(store, &current))
505                                 {
506                                 gtk_tree_path_next(tpath);
507                                 move = TRUE;
508                                 }
509                         else if (gtk_tree_path_prev(tpath) &&
510                                  gtk_tree_model_get_iter(store, &current, tpath))
511                                 {
512                                 move = TRUE;
513                                 }
514
515                         if (move)
516                                 {
517                                 gtk_tree_view_set_cursor(widget, tpath, NULL, FALSE);
518                                 }
519                         }
520                 }
521
522         gtk_tree_path_free(tpath);
523         if (fpath) gtk_tree_path_free(fpath);
524
525         return move;
526 }
527
528 gint tree_path_to_row(GtkTreePath *tpath)
529 {
530         gint *indices;
531
532         indices = gtk_tree_path_get_indices(tpath);
533         if (indices) return indices[0];
534
535         return -1;
536 }
537
538
539 /*
540  *-------------------------------------------------------------------
541  * color utilities
542  *-------------------------------------------------------------------
543  */
544
545 void shift_color(GdkColor *src, gshort val, gint direction)
546 {
547         gshort cs;
548
549         if (val == -1)
550                 {
551                 val = STYLE_SHIFT_STANDARD;
552                 }
553         else
554                 {
555                 val = CLAMP(val, 1, 100);
556                 }
557         cs = 0xffff / 100 * val;
558
559         /* up or down ? */
560         if (direction < 0 ||
561             (direction == 0 &&((gint)src->red + (gint)src->green + (gint)src->blue) / 3 > 0xffff / 2))
562                 {
563                 src->red = MAX(0 , src->red - cs);
564                 src->green = MAX(0 , src->green - cs);
565                 src->blue = MAX(0 , src->blue - cs);
566                 }
567         else
568                 {
569                 src->red = MIN(0xffff, src->red + cs);
570                 src->green = MIN(0xffff, src->green + cs);
571                 src->blue = MIN(0xffff, src->blue + cs);
572                 }
573 }
574
575 /* darkens or lightens a style's color for given state
576  * esp. useful for alternating dark/light in (c)lists
577  */
578 void style_shift_color(GtkStyle *style, GtkStateType type, gshort shift_value, gint direction)
579 {
580         if (!style) return;
581
582         shift_color(&style->base[type], shift_value, direction);
583         shift_color(&style->bg[type], shift_value, direction);
584 }
585
586 /*
587  *-------------------------------------------------------------------
588  * auto scroll by mouse position
589  *-------------------------------------------------------------------
590  */
591
592 #define AUTO_SCROLL_DEFAULT_SPEED 100
593 #define AUTO_SCROLL_DEFAULT_REGION 20
594
595 typedef struct _AutoScrollData AutoScrollData;
596 struct _AutoScrollData
597 {
598         guint timer_id; /* event source id */
599         gint region_size;
600         GtkWidget *widget;
601         GtkAdjustment *adj;
602         gint max_step;
603
604         gint (*notify_func)(GtkWidget *, gint, gint, gpointer);
605         gpointer notify_data;
606 };
607
608 void widget_auto_scroll_stop(GtkWidget *widget)
609 {
610         AutoScrollData *sd;
611
612         sd = g_object_get_data(G_OBJECT(widget), "autoscroll");
613         if (!sd) return;
614         g_object_set_data(G_OBJECT(widget), "autoscroll", NULL);
615
616         if (sd->timer_id) g_source_remove(sd->timer_id);
617         g_free(sd);
618 }
619
620 static gboolean widget_auto_scroll_cb(gpointer data)
621 {
622         AutoScrollData *sd = data;
623         GdkWindow *window;
624         gint x, y;
625         gint w, h;
626         gint amt = 0;
627
628         if (sd->max_step < sd->region_size)
629                 {
630                 sd->max_step = MIN(sd->region_size, sd->max_step + 2);
631                 }
632
633         window = gtk_widget_get_window(sd->widget);
634         gdk_window_get_pointer(window, &x, &y, NULL);
635         w = gdk_window_get_width(window);
636         h = gdk_window_get_height(window);
637
638         if (x < 0 || x >= w || y < 0 || y >= h)
639                 {
640                 sd->timer_id = 0;
641                 widget_auto_scroll_stop(sd->widget);
642                 return FALSE;
643                 }
644
645         if (h < sd->region_size * 3)
646                 {
647                 /* height is cramped, nicely divide into three equal regions */
648                 if (y < h / 3 || y > h / 3 * 2)
649                         {
650                         amt = (y < h / 2) ? 0 - ((h / 2) - y) : y - (h / 2);
651                         }
652                 }
653         else if (y < sd->region_size)
654                 {
655                 amt = 0 - (sd->region_size - y);
656                 }
657         else if (y >= h - sd->region_size)
658                 {
659                 amt = y - (h - sd->region_size);
660                 }
661
662         if (amt != 0)
663                 {
664                 amt = CLAMP(amt, 0 - sd->max_step, sd->max_step);
665
666                 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)))
667                         {
668                         /* only notify when scrolling is needed */
669                         if (sd->notify_func && !sd->notify_func(sd->widget, x, y, sd->notify_data))
670                                 {
671                                 sd->timer_id = 0;
672                                 widget_auto_scroll_stop(sd->widget);
673                                 return FALSE;
674                                 }
675
676                         gtk_adjustment_set_value(sd->adj,
677                                 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)));
678                         }
679                 }
680
681         return TRUE;
682 }
683
684 gint widget_auto_scroll_start(GtkWidget *widget, GtkAdjustment *v_adj, gint scroll_speed, gint region_size,
685                               gint (*notify_func)(GtkWidget *widget, gint x, gint y, gpointer data), gpointer notify_data)
686 {
687         AutoScrollData *sd;
688
689         if (!widget || !v_adj) return 0;
690         if (g_object_get_data(G_OBJECT(widget), "autoscroll")) return 0;
691         if (scroll_speed < 1) scroll_speed = AUTO_SCROLL_DEFAULT_SPEED;
692         if (region_size < 1) region_size = AUTO_SCROLL_DEFAULT_REGION;
693
694         sd = g_new0(AutoScrollData, 1);
695         sd->widget = widget;
696         sd->adj = v_adj;
697         sd->region_size = region_size;
698         sd->max_step = 1;
699         sd->timer_id = g_timeout_add(scroll_speed, widget_auto_scroll_cb, sd);
700
701         sd->notify_func = notify_func;
702         sd->notify_data = notify_data;
703
704         g_object_set_data(G_OBJECT(widget), "autoscroll", sd);
705
706         return scroll_speed;
707 }
708
709
710 /*
711  *-------------------------------------------------------------------
712  * GList utils
713  *-------------------------------------------------------------------
714  */
715
716 GList *uig_list_insert_link(GList *list, GList *link, gpointer data)
717 {
718         GList *new_list;
719
720         if (!list || link == list) return g_list_prepend(list, data);
721         if (!link) return g_list_append(list, data);
722
723         new_list = g_list_alloc();
724         new_list->data = data;
725
726         if (link->prev)
727                 {
728                 link->prev->next = new_list;
729                 new_list->prev = link->prev;
730                 }
731         else
732                 {
733                 list = new_list;
734                 }
735         link->prev = new_list;
736         new_list->next = link;
737
738         return list;
739 }
740
741 GList *uig_list_insert_list(GList *parent, GList *insert_link, GList *list)
742 {
743         GList *end;
744
745         if (!insert_link) return g_list_concat(parent, list);
746         if (insert_link == parent) return g_list_concat(list, parent);
747         if (!parent) return list;
748         if (!list) return parent;
749
750         end  = g_list_last(list);
751
752         if (insert_link->prev) insert_link->prev->next = list;
753         list->prev = insert_link->prev;
754         insert_link->prev = end;
755         end->next = insert_link;
756
757         return parent;
758 }
759 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */