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