]> icculus.org git repositories - dana/openbox.git/blob - loco/list.c
split loco into a bunch of files.
[dana/openbox.git] / loco / list.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    list.c for the Openbox window manager
4    Copyright (c) 2008        Derek Foreman
5    Copyright (c) 2008        Dana Jansens
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "list.h"
21 #include "window.h"
22
23 LocoList* loco_list_prepend(LocoList **top, LocoList **bottom,
24                             LocoWindow *window)
25 {
26     LocoList *n = g_new(LocoList, 1);
27     n->window = window;
28
29     n->prev = NULL;
30     n->next = *top;
31     if (n->next) n->next->prev = n;
32
33     *top = n;
34     if (!*bottom) *bottom = n;
35     return n;
36 }
37
38 void loco_list_delete_link(LocoList **top, LocoList **bottom,
39                            LocoList *pos)
40 {
41     LocoList *prev = pos->prev;
42     LocoList *next = pos->next;
43
44     if (next)
45         next->prev = prev;
46     if (prev)
47         prev->next = next;
48     if (!next)
49         *bottom = prev;
50     if (!prev)
51         *top = next;
52
53     g_free(pos);
54 }
55
56 void loco_list_move_before(LocoList **top, LocoList **bottom,
57                            LocoList *move, LocoList *before)
58 {
59     LocoList *prev, *next;
60
61     /* these won't move it anywhere */
62     if (move == before || move->next == before) return;
63
64     prev = move->prev;
65     next = move->next;
66
67     /* remove it from the list */
68     if (next) next->prev = prev;
69     else      *bottom = prev;
70     if (prev) prev->next = next;
71     else      *top = next;
72
73     /* reinsert it */
74     if (before) {
75         move->next = before;
76         move->prev = before->prev;
77         move->next->prev = move;
78         if (move->prev) move->prev->next = move;
79     }
80     else {
81         /* after the bottom */
82         move->prev = *bottom;
83         move->next = NULL;
84         if (move->prev) move->prev->next = move;
85         *bottom = move;
86     }
87
88     if (!move->prev) *top = move;
89 }
90