Mercurial > pidgin.yaz
annotate finch/libgnt/gnttree.c @ 18404:9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
set a fixed with for a column, or maintain the width-ratio when resizing.
author | Sadrul Habib Chowdhury <imadil@gmail.com> |
---|---|
date | Sun, 01 Jul 2007 12:21:30 +0000 |
parents | 1f9db757efc2 |
children | 69cc1a4ef6ab |
rev | line source |
---|---|
18049
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16585
diff
changeset
|
1 /** |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16585
diff
changeset
|
2 * GNT - The GLib Ncurses Toolkit |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16585
diff
changeset
|
3 * |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16585
diff
changeset
|
4 * GNT is the legal property of its developers, whose names are too numerous |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16585
diff
changeset
|
5 * to list here. Please refer to the COPYRIGHT file distributed with this |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16585
diff
changeset
|
6 * source distribution. |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16585
diff
changeset
|
7 * |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16585
diff
changeset
|
8 * This library is free software; you can redistribute it and/or modify |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16585
diff
changeset
|
9 * it under the terms of the GNU General Public License as published by |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16585
diff
changeset
|
10 * the Free Software Foundation; either version 2 of the License, or |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16585
diff
changeset
|
11 * (at your option) any later version. |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16585
diff
changeset
|
12 * |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16585
diff
changeset
|
13 * This program is distributed in the hope that it will be useful, |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16585
diff
changeset
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16585
diff
changeset
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16585
diff
changeset
|
16 * GNU General Public License for more details. |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16585
diff
changeset
|
17 * |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16585
diff
changeset
|
18 * You should have received a copy of the GNU General Public License |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16585
diff
changeset
|
19 * along with this program; if not, write to the Free Software |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16585
diff
changeset
|
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16585
diff
changeset
|
21 */ |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16585
diff
changeset
|
22 |
15818 | 23 #include "gntmarshal.h" |
24 #include "gntstyle.h" | |
25 #include "gnttree.h" | |
26 #include "gntutils.h" | |
27 | |
28 #include <string.h> | |
29 #include <ctype.h> | |
30 | |
31 #define SEARCH_TIMEOUT 4000 /* 4 secs */ | |
16249
f02e611bdb6b
Show the search string in the tree.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16203
diff
changeset
|
32 #define SEARCHING(tree) (tree->search && tree->search->len > 0) |
15818 | 33 |
18404
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
34 #define COLUMN_INVISIBLE(tree, index) (tree->columns[index].flags & GNT_TREE_COLUMN_INVISIBLE) |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
35 |
15818 | 36 enum |
37 { | |
38 SIG_SELECTION_CHANGED, | |
39 SIG_SCROLLED, | |
40 SIG_TOGGLED, | |
16105
1983ecd15174
Remember the collapsed state of groups in the blist
Richard Nelson <wabz@pidgin.im>
parents:
15972
diff
changeset
|
41 SIG_COLLAPSED, |
15818 | 42 SIGS, |
43 }; | |
44 | |
45 #define TAB_SIZE 3 | |
46 | |
47 /* XXX: Make this one into a GObject? | |
48 * ... Probably not */ | |
15931
f00f2e283ffb
Some define changes. This helps in generating the python bindings.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
49 struct _GntTreeRow |
15818 | 50 { |
51 void *key; | |
52 void *data; /* XXX: unused */ | |
53 | |
54 gboolean collapsed; | |
55 gboolean choice; /* Is this a choice-box? | |
56 If choice is true, then child will be NULL */ | |
57 gboolean isselected; | |
58 GntTextFormatFlags flags; | |
59 | |
60 GntTreeRow *parent; | |
61 GntTreeRow *child; | |
62 GntTreeRow *next; | |
63 GntTreeRow *prev; | |
64 | |
65 GList *columns; | |
66 GntTree *tree; | |
67 }; | |
68 | |
15931
f00f2e283ffb
Some define changes. This helps in generating the python bindings.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
69 struct _GntTreeCol |
15818 | 70 { |
71 char *text; | |
72 int span; /* How many columns does it span? */ | |
73 }; | |
74 | |
16203
4f6a6443a1e3
emit/handle gnttree signals appropriately
Richard Nelson <wabz@pidgin.im>
parents:
16123
diff
changeset
|
75 static void tree_selection_changed(GntTree *, GntTreeRow *, GntTreeRow *); |
4f6a6443a1e3
emit/handle gnttree signals appropriately
Richard Nelson <wabz@pidgin.im>
parents:
16123
diff
changeset
|
76 |
15818 | 77 static GntWidgetClass *parent_class = NULL; |
78 static guint signals[SIGS] = { 0 }; | |
79 | |
18404
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
80 static void |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
81 readjust_columns(GntTree *tree) |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
82 { |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
83 int i, col, total; |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
84 int width; |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
85 #define WIDTH(i) tree->columns[i].width_ratio ? tree->columns[i].width_ratio : tree->columns[i].width |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
86 gnt_widget_get_size(GNT_WIDGET(tree), &width, NULL); |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
87 for (i = 0, total = 0; i < tree->ncol ; i++) { |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
88 if (tree->columns[i].flags & GNT_TREE_COLUMN_INVISIBLE) |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
89 continue; |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
90 if (tree->columns[i].flags & GNT_TREE_COLUMN_FIXED_SIZE) |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
91 width -= WIDTH(i); |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
92 else |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
93 total += WIDTH(i); |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
94 } |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
95 |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
96 if (total == 0) |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
97 return; |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
98 |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
99 for (i = 0; i < tree->ncol; i++) { |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
100 if (tree->columns[i].flags & GNT_TREE_COLUMN_INVISIBLE) |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
101 continue; |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
102 if (tree->columns[i].flags & GNT_TREE_COLUMN_FIXED_SIZE) |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
103 col = WIDTH(i); |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
104 else |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
105 col = WIDTH(i) * width / total; |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
106 gnt_tree_set_col_width(GNT_TREE(tree), i, col); |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
107 } |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
108 } |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
109 |
15818 | 110 /* Move the item at position old to position new */ |
111 static GList * | |
112 g_list_reposition_child(GList *list, int old, int new) | |
113 { | |
114 gpointer item = g_list_nth_data(list, old); | |
115 list = g_list_remove(list, item); | |
116 if (old < new) | |
117 new--; /* because the positions would have shifted after removing the item */ | |
118 list = g_list_insert(list, item, new); | |
119 return list; | |
120 } | |
121 | |
122 static GntTreeRow * | |
123 _get_next(GntTreeRow *row, gboolean godeep) | |
124 { | |
125 if (row == NULL) | |
126 return NULL; | |
127 if (godeep && row->child) | |
128 return row->child; | |
129 if (row->next) | |
130 return row->next; | |
131 return _get_next(row->parent, FALSE); | |
132 } | |
133 | |
134 static gboolean | |
135 row_matches_search(GntTreeRow *row) | |
136 { | |
137 GntTree *t = row->tree; | |
138 if (t->search && t->search->len > 0) { | |
139 char *one = g_utf8_casefold(((GntTreeCol*)row->columns->data)->text, -1); | |
140 char *two = g_utf8_casefold(t->search->str, -1); | |
141 char *z = strstr(one, two); | |
142 g_free(one); | |
143 g_free(two); | |
144 if (z == NULL) | |
145 return FALSE; | |
146 } | |
147 return TRUE; | |
148 } | |
149 | |
150 static GntTreeRow * | |
151 get_next(GntTreeRow *row) | |
152 { | |
153 if (row == NULL) | |
154 return NULL; | |
155 while ((row = _get_next(row, !row->collapsed)) != NULL) { | |
156 if (row_matches_search(row)) | |
157 break; | |
158 } | |
159 return row; | |
160 } | |
161 | |
162 /* Returns the n-th next row. If it doesn't exist, returns NULL */ | |
163 static GntTreeRow * | |
164 get_next_n(GntTreeRow *row, int n) | |
165 { | |
166 while (row && n--) | |
167 row = get_next(row); | |
168 return row; | |
169 } | |
170 | |
171 /* Returns the n-th next row. If it doesn't exist, then the last non-NULL node */ | |
172 static GntTreeRow * | |
173 get_next_n_opt(GntTreeRow *row, int n, int *pos) | |
174 { | |
175 GntTreeRow *next = row; | |
176 int r = 0; | |
177 | |
178 if (row == NULL) | |
179 return NULL; | |
180 | |
181 while (row && n--) | |
182 { | |
183 row = get_next(row); | |
184 if (row) | |
185 { | |
186 next = row; | |
187 r++; | |
188 } | |
189 } | |
190 | |
191 if (pos) | |
192 *pos = r; | |
193 | |
194 return next; | |
195 } | |
196 | |
197 static GntTreeRow * | |
198 get_last_child(GntTreeRow *row) | |
199 { | |
200 if (row == NULL) | |
201 return NULL; | |
202 if (!row->collapsed && row->child) | |
203 row = row->child; | |
204 else | |
205 return row; | |
206 | |
207 while(row->next) | |
208 row = row->next; | |
16585
467698ab6bf0
Fix an odd selection issue when moving up.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16283
diff
changeset
|
209 return get_last_child(row); |
15818 | 210 } |
211 | |
212 static GntTreeRow * | |
213 get_prev(GntTreeRow *row) | |
214 { | |
215 if (row == NULL) | |
216 return NULL; | |
217 while (row) { | |
218 if (row->prev) | |
219 row = get_last_child(row->prev); | |
220 else | |
221 row = row->parent; | |
222 if (!row || row_matches_search(row)) | |
223 break; | |
224 } | |
225 return row; | |
226 } | |
227 | |
228 static GntTreeRow * | |
229 get_prev_n(GntTreeRow *row, int n) | |
230 { | |
231 while (row && n--) | |
232 row = get_prev(row); | |
233 return row; | |
234 } | |
235 | |
236 /* Distance of row from the root */ | |
237 /* XXX: This is uber-inefficient */ | |
238 static int | |
239 get_root_distance(GntTreeRow *row) | |
240 { | |
241 if (row == NULL) | |
242 return -1; | |
243 return get_root_distance(get_prev(row)) + 1; | |
244 } | |
245 | |
246 /* Returns the distance between a and b. | |
247 * If a is 'above' b, then the distance is positive */ | |
248 static int | |
249 get_distance(GntTreeRow *a, GntTreeRow *b) | |
250 { | |
251 /* First get the distance from a to the root. | |
252 * Then the distance from b to the root. | |
253 * Subtract. | |
254 * It's not that good, but it works. */ | |
255 int ha = get_root_distance(a); | |
256 int hb = get_root_distance(b); | |
257 | |
258 return (hb - ha); | |
259 } | |
260 | |
261 static int | |
262 find_depth(GntTreeRow *row) | |
263 { | |
264 int dep = -1; | |
265 | |
266 while (row) | |
267 { | |
268 dep++; | |
269 row = row->parent; | |
270 } | |
271 | |
272 return dep; | |
273 } | |
274 | |
275 static char * | |
276 update_row_text(GntTree *tree, GntTreeRow *row) | |
277 { | |
278 GString *string = g_string_new(NULL); | |
279 GList *iter; | |
280 int i; | |
15970
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
281 gboolean notfirst = FALSE; |
15971
05d347516fcd
Fine tune column hiding.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15970
diff
changeset
|
282 int lastvisible = tree->ncol; |
05d347516fcd
Fine tune column hiding.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15970
diff
changeset
|
283 |
18404
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
284 while (lastvisible && COLUMN_INVISIBLE(tree, lastvisible)) |
15971
05d347516fcd
Fine tune column hiding.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15970
diff
changeset
|
285 lastvisible--; |
15818 | 286 |
287 for (i = 0, iter = row->columns; i < tree->ncol && iter; i++, iter = iter->next) | |
288 { | |
289 GntTreeCol *col = iter->data; | |
290 const char *text; | |
291 int len = gnt_util_onscreen_width(col->text, NULL); | |
292 int fl = 0; | |
293 gboolean cut = FALSE; | |
15971
05d347516fcd
Fine tune column hiding.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15970
diff
changeset
|
294 int width; |
15818 | 295 |
18404
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
296 if (COLUMN_INVISIBLE(tree, i)) |
15970
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
297 continue; |
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
298 |
15971
05d347516fcd
Fine tune column hiding.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15970
diff
changeset
|
299 if (i == lastvisible) |
05d347516fcd
Fine tune column hiding.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15970
diff
changeset
|
300 width = GNT_WIDGET(tree)->priv.width - gnt_util_onscreen_width(string->str, NULL); |
05d347516fcd
Fine tune column hiding.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15970
diff
changeset
|
301 else |
05d347516fcd
Fine tune column hiding.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15970
diff
changeset
|
302 width = tree->columns[i].width; |
05d347516fcd
Fine tune column hiding.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15970
diff
changeset
|
303 |
15818 | 304 if (i == 0) |
305 { | |
306 if (row->choice) | |
307 { | |
308 g_string_append_printf(string, "[%c] ", | |
309 row->isselected ? 'X' : ' '); | |
310 fl = 4; | |
311 } | |
312 else if (row->parent == NULL && row->child) | |
313 { | |
314 if (row->collapsed) | |
315 { | |
316 string = g_string_append(string, "+ "); | |
317 } | |
318 else | |
319 { | |
320 string = g_string_append(string, "- "); | |
321 } | |
322 fl = 2; | |
323 } | |
324 else | |
325 { | |
326 fl = TAB_SIZE * find_depth(row); | |
327 g_string_append_printf(string, "%*s", fl, ""); | |
328 } | |
329 len += fl; | |
330 } | |
15970
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
331 else if (notfirst) |
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
332 g_string_append_c(string, '|'); |
15818 | 333 else |
15970
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
334 g_string_append_c(string, ' '); |
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
335 |
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
336 notfirst = TRUE; |
15818 | 337 |
18183
99969affedf5
I think this fixes the ellipsization in the trees.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18118
diff
changeset
|
338 if (len > width) { |
15971
05d347516fcd
Fine tune column hiding.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15970
diff
changeset
|
339 len = width - 1; |
15818 | 340 cut = TRUE; |
341 } | |
342 text = gnt_util_onscreen_width_to_pointer(col->text, len - fl, NULL); | |
343 string = g_string_append_len(string, col->text, text - col->text); | |
344 if (cut) { /* ellipsis */ | |
345 if (gnt_ascii_only()) | |
346 g_string_append_c(string, '~'); | |
347 else | |
348 string = g_string_append(string, "\342\200\246"); | |
349 len++; | |
350 } | |
351 | |
352 if (len < tree->columns[i].width && iter->next) | |
15971
05d347516fcd
Fine tune column hiding.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15970
diff
changeset
|
353 g_string_append_printf(string, "%*s", width - len, ""); |
15818 | 354 } |
355 return g_string_free(string, FALSE); | |
356 } | |
357 | |
15970
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
358 #define NEXT_X x += tree->columns[i].width + (i > 0 ? 1 : 0) |
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
359 |
15818 | 360 static void |
361 tree_mark_columns(GntTree *tree, int pos, int y, chtype type) | |
362 { | |
363 GntWidget *widget = GNT_WIDGET(tree); | |
364 int i; | |
365 int x = pos; | |
15970
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
366 gboolean notfirst = FALSE; |
15818 | 367 |
368 for (i = 0; i < tree->ncol - 1; i++) | |
369 { | |
18404
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
370 if (!COLUMN_INVISIBLE(tree, i)) { |
15970
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
371 notfirst = TRUE; |
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
372 NEXT_X; |
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
373 } |
18404
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
374 if (!COLUMN_INVISIBLE(tree, i+1) && notfirst) |
15970
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
375 mvwaddch(widget->window, y, x, type); |
15818 | 376 } |
377 } | |
378 | |
379 static void | |
380 redraw_tree(GntTree *tree) | |
381 { | |
382 int start, i; | |
383 GntWidget *widget = GNT_WIDGET(tree); | |
384 GntTreeRow *row; | |
385 int pos, up, down; | |
386 int rows, scrcol; | |
387 | |
388 if (!GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(tree), GNT_WIDGET_MAPPED)) | |
389 return; | |
390 | |
391 if (GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_NO_BORDER)) | |
392 pos = 0; | |
393 else | |
394 pos = 1; | |
395 | |
396 if (tree->top == NULL) | |
397 tree->top = tree->root; | |
16203
4f6a6443a1e3
emit/handle gnttree signals appropriately
Richard Nelson <wabz@pidgin.im>
parents:
16123
diff
changeset
|
398 if (tree->current == NULL) { |
15818 | 399 tree->current = tree->root; |
16203
4f6a6443a1e3
emit/handle gnttree signals appropriately
Richard Nelson <wabz@pidgin.im>
parents:
16123
diff
changeset
|
400 tree_selection_changed(tree, NULL, tree->current); |
4f6a6443a1e3
emit/handle gnttree signals appropriately
Richard Nelson <wabz@pidgin.im>
parents:
16123
diff
changeset
|
401 } |
15818 | 402 |
403 wbkgd(widget->window, COLOR_PAIR(GNT_COLOR_NORMAL)); | |
404 | |
405 start = 0; | |
406 if (tree->show_title) | |
407 { | |
408 int i; | |
409 int x = pos; | |
410 | |
411 mvwhline(widget->window, pos + 1, pos, ACS_HLINE | COLOR_PAIR(GNT_COLOR_NORMAL), | |
412 widget->priv.width - pos - 1); | |
15970
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
413 mvwhline(widget->window, pos, pos, ' ' | COLOR_PAIR(GNT_COLOR_NORMAL), |
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
414 widget->priv.width - pos - 1); |
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
415 |
15818 | 416 for (i = 0; i < tree->ncol; i++) |
417 { | |
18404
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
418 if (COLUMN_INVISIBLE(tree, i)) { |
15970
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
419 continue; |
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
420 } |
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
421 mvwaddstr(widget->window, pos, x + 1, tree->columns[i].title); |
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
422 NEXT_X; |
15818 | 423 } |
424 if (pos) | |
425 { | |
426 tree_mark_columns(tree, pos, 0, ACS_TTEE | COLOR_PAIR(GNT_COLOR_NORMAL)); | |
427 tree_mark_columns(tree, pos, widget->priv.height - pos, | |
428 ACS_BTEE | COLOR_PAIR(GNT_COLOR_NORMAL)); | |
429 } | |
430 tree_mark_columns(tree, pos, pos + 1, | |
431 (tree->show_separator ? ACS_PLUS : ACS_HLINE) | COLOR_PAIR(GNT_COLOR_NORMAL)); | |
432 tree_mark_columns(tree, pos, pos, | |
433 (tree->show_separator ? ACS_VLINE : ' ') | COLOR_PAIR(GNT_COLOR_NORMAL)); | |
434 start = 2; | |
435 } | |
436 | |
437 rows = widget->priv.height - pos * 2 - start - 1; | |
438 tree->bottom = get_next_n_opt(tree->top, rows, &down); | |
439 if (down < rows) | |
440 { | |
441 tree->top = get_prev_n(tree->bottom, rows); | |
442 if (tree->top == NULL) | |
443 tree->top = tree->root; | |
444 } | |
445 | |
446 up = get_distance(tree->top, tree->current); | |
447 if (up < 0) | |
448 tree->top = tree->current; | |
449 else if (up >= widget->priv.height - pos) | |
450 tree->top = get_prev_n(tree->current, rows); | |
451 | |
452 if (tree->top && !row_matches_search(tree->top)) | |
453 tree->top = get_next(tree->top); | |
454 row = tree->top; | |
455 scrcol = widget->priv.width - 1 - 2 * pos; /* exclude the borders and the scrollbar */ | |
456 for (i = start + pos; row && i < widget->priv.height - pos; | |
457 i++, row = get_next(row)) | |
458 { | |
459 char *str; | |
460 int wr; | |
461 | |
462 GntTextFormatFlags flags = row->flags; | |
463 int attr = 0; | |
464 | |
465 if (!row_matches_search(row)) | |
466 continue; | |
467 str = update_row_text(tree, row); | |
468 | |
469 if ((wr = gnt_util_onscreen_width(str, NULL)) > scrcol) | |
470 { | |
471 char *s = (char*)gnt_util_onscreen_width_to_pointer(str, scrcol, &wr); | |
472 *s = '\0'; | |
473 } | |
474 | |
475 if (flags & GNT_TEXT_FLAG_BOLD) | |
476 attr |= A_BOLD; | |
477 if (flags & GNT_TEXT_FLAG_UNDERLINE) | |
478 attr |= A_UNDERLINE; | |
479 if (flags & GNT_TEXT_FLAG_BLINK) | |
480 attr |= A_BLINK; | |
481 | |
482 if (row == tree->current) | |
483 { | |
484 if (gnt_widget_has_focus(widget)) | |
485 attr |= COLOR_PAIR(GNT_COLOR_HIGHLIGHT); | |
486 else | |
487 attr |= COLOR_PAIR(GNT_COLOR_HIGHLIGHT_D); | |
488 } | |
489 else | |
490 { | |
491 if (flags & GNT_TEXT_FLAG_DIM) | |
492 attr |= (A_DIM | COLOR_PAIR(GNT_COLOR_DISABLED)); | |
493 else if (flags & GNT_TEXT_FLAG_HIGHLIGHT) | |
494 attr |= (A_DIM | COLOR_PAIR(GNT_COLOR_HIGHLIGHT)); | |
495 else | |
496 attr |= COLOR_PAIR(GNT_COLOR_NORMAL); | |
497 } | |
498 | |
499 wbkgdset(widget->window, '\0' | attr); | |
500 mvwaddstr(widget->window, i, pos, str); | |
501 whline(widget->window, ' ', scrcol - wr); | |
502 tree->bottom = row; | |
503 g_free(str); | |
504 tree_mark_columns(tree, pos, i, | |
505 (tree->show_separator ? ACS_VLINE : ' ') | attr); | |
506 } | |
507 | |
508 wbkgdset(widget->window, '\0' | COLOR_PAIR(GNT_COLOR_NORMAL)); | |
509 while (i < widget->priv.height - pos) | |
510 { | |
511 mvwhline(widget->window, i, pos, ' ', | |
512 widget->priv.width - pos * 2 - 1); | |
513 tree_mark_columns(tree, pos, i, | |
514 (tree->show_separator ? ACS_VLINE : ' ')); | |
515 i++; | |
516 } | |
517 | |
518 scrcol = widget->priv.width - pos - 1; /* position of the scrollbar */ | |
519 rows--; | |
520 if (rows > 0) | |
521 { | |
522 int total; | |
523 int showing, position; | |
524 | |
525 get_next_n_opt(tree->root, g_list_length(tree->list), &total); | |
526 showing = rows * rows / MAX(total, 1) + 1; | |
527 showing = MIN(rows, showing); | |
528 | |
529 total -= rows; | |
530 up = get_distance(tree->root, tree->top); | |
531 down = total - up; | |
532 | |
533 position = (rows - showing) * up / MAX(1, up + down); | |
534 position = MAX((tree->top != tree->root), position); | |
535 | |
536 if (showing + position > rows) | |
537 position = rows - showing; | |
538 | |
539 if (showing + position == rows && row) | |
540 position = MAX(0, rows - 1 - showing); | |
541 else if (showing + position < rows && !row) | |
542 position = rows - showing; | |
543 | |
544 position += pos + start + 1; | |
545 | |
546 mvwvline(widget->window, pos + start + 1, scrcol, | |
547 ' ' | COLOR_PAIR(GNT_COLOR_NORMAL), rows); | |
548 mvwvline(widget->window, position, scrcol, | |
549 ACS_CKBOARD | COLOR_PAIR(GNT_COLOR_HIGHLIGHT_D), showing); | |
550 } | |
551 | |
552 mvwaddch(widget->window, start + pos, scrcol, | |
553 ((tree->top != tree->root) ? ACS_UARROW : ' ') | | |
554 COLOR_PAIR(GNT_COLOR_HIGHLIGHT_D)); | |
555 | |
556 mvwaddch(widget->window, widget->priv.height - pos - 1, scrcol, | |
557 (row ? ACS_DARROW : ' ') | COLOR_PAIR(GNT_COLOR_HIGHLIGHT_D)); | |
558 | |
16249
f02e611bdb6b
Show the search string in the tree.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16203
diff
changeset
|
559 /* If there's a search-text, show it in the bottom of the tree */ |
f02e611bdb6b
Show the search string in the tree.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16203
diff
changeset
|
560 if (tree->search && tree->search->len > 0) { |
f02e611bdb6b
Show the search string in the tree.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16203
diff
changeset
|
561 const char *str = gnt_util_onscreen_width_to_pointer(tree->search->str, scrcol - 1, NULL); |
f02e611bdb6b
Show the search string in the tree.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16203
diff
changeset
|
562 wbkgdset(widget->window, '\0' | COLOR_PAIR(GNT_COLOR_HIGHLIGHT_D)); |
f02e611bdb6b
Show the search string in the tree.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16203
diff
changeset
|
563 mvwaddnstr(widget->window, widget->priv.height - pos - 1, pos, |
f02e611bdb6b
Show the search string in the tree.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16203
diff
changeset
|
564 tree->search->str, str - tree->search->str); |
f02e611bdb6b
Show the search string in the tree.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16203
diff
changeset
|
565 } |
f02e611bdb6b
Show the search string in the tree.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16203
diff
changeset
|
566 |
15818 | 567 gnt_widget_queue_update(widget); |
568 } | |
569 | |
570 static void | |
571 gnt_tree_draw(GntWidget *widget) | |
572 { | |
573 GntTree *tree = GNT_TREE(widget); | |
574 | |
575 redraw_tree(tree); | |
576 | |
577 GNTDEBUG; | |
578 } | |
579 | |
580 static void | |
581 gnt_tree_size_request(GntWidget *widget) | |
582 { | |
583 if (widget->priv.height == 0) | |
584 widget->priv.height = 10; /* XXX: Why?! */ | |
585 if (widget->priv.width == 0) | |
586 { | |
587 GntTree *tree = GNT_TREE(widget); | |
588 int i, width = 0; | |
589 for (i = 0; i < tree->ncol; i++) | |
18404
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
590 if (!COLUMN_INVISIBLE(tree, i)) |
15970
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
591 width += tree->columns[i].width + 1; |
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
592 widget->priv.width = width; |
15818 | 593 } |
594 } | |
595 | |
596 static void | |
597 gnt_tree_map(GntWidget *widget) | |
598 { | |
599 GntTree *tree = GNT_TREE(widget); | |
600 if (widget->priv.width == 0 || widget->priv.height == 0) | |
601 { | |
602 gnt_widget_size_request(widget); | |
603 } | |
604 tree->top = tree->root; | |
605 tree->current = tree->root; | |
606 GNTDEBUG; | |
607 } | |
608 | |
609 static void | |
610 tree_selection_changed(GntTree *tree, GntTreeRow *old, GntTreeRow *current) | |
611 { | |
612 g_signal_emit(tree, signals[SIG_SELECTION_CHANGED], 0, old ? old->key : NULL, | |
613 current ? current->key : NULL); | |
614 } | |
615 | |
616 static gboolean | |
617 action_down(GntBindable *bind, GList *null) | |
618 { | |
619 int dist; | |
620 GntTree *tree = GNT_TREE(bind); | |
621 GntTreeRow *old = tree->current; | |
622 GntTreeRow *row = get_next(tree->current); | |
623 if (row == NULL) | |
624 return FALSE; | |
625 tree->current = row; | |
626 if ((dist = get_distance(tree->current, tree->bottom)) < 0) | |
627 gnt_tree_scroll(tree, -dist); | |
628 else | |
629 redraw_tree(tree); | |
630 if (old != tree->current) | |
631 tree_selection_changed(tree, old, tree->current); | |
632 return TRUE; | |
633 } | |
634 | |
635 static gboolean | |
15972
5eb0621e0760
Backspace to jump to parent.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15971
diff
changeset
|
636 action_move_parent(GntBindable *bind, GList *null) |
5eb0621e0760
Backspace to jump to parent.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15971
diff
changeset
|
637 { |
5eb0621e0760
Backspace to jump to parent.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15971
diff
changeset
|
638 GntTree *tree = GNT_TREE(bind); |
5eb0621e0760
Backspace to jump to parent.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15971
diff
changeset
|
639 GntTreeRow *row = tree->current; |
16249
f02e611bdb6b
Show the search string in the tree.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16203
diff
changeset
|
640 int dist; |
f02e611bdb6b
Show the search string in the tree.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16203
diff
changeset
|
641 |
f02e611bdb6b
Show the search string in the tree.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16203
diff
changeset
|
642 if (!row->parent || SEARCHING(tree)) |
f02e611bdb6b
Show the search string in the tree.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16203
diff
changeset
|
643 return FALSE; |
f02e611bdb6b
Show the search string in the tree.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16203
diff
changeset
|
644 |
f02e611bdb6b
Show the search string in the tree.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16203
diff
changeset
|
645 tree->current = row->parent; |
f02e611bdb6b
Show the search string in the tree.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16203
diff
changeset
|
646 if ((dist = get_distance(tree->current, tree->top)) > 0) |
f02e611bdb6b
Show the search string in the tree.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16203
diff
changeset
|
647 gnt_tree_scroll(tree, -dist); |
f02e611bdb6b
Show the search string in the tree.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16203
diff
changeset
|
648 else |
f02e611bdb6b
Show the search string in the tree.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16203
diff
changeset
|
649 redraw_tree(tree); |
f02e611bdb6b
Show the search string in the tree.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16203
diff
changeset
|
650 tree_selection_changed(tree, row, tree->current); |
15972
5eb0621e0760
Backspace to jump to parent.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15971
diff
changeset
|
651 return TRUE; |
5eb0621e0760
Backspace to jump to parent.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15971
diff
changeset
|
652 } |
5eb0621e0760
Backspace to jump to parent.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15971
diff
changeset
|
653 |
5eb0621e0760
Backspace to jump to parent.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15971
diff
changeset
|
654 static gboolean |
15818 | 655 action_up(GntBindable *bind, GList *list) |
656 { | |
657 int dist; | |
658 GntTree *tree = GNT_TREE(bind); | |
659 GntTreeRow *old = tree->current; | |
660 GntTreeRow *row = get_prev(tree->current); | |
661 if (!row) | |
662 return FALSE; | |
663 tree->current = row; | |
664 if ((dist = get_distance(tree->current, tree->top)) > 0) | |
665 gnt_tree_scroll(tree, -dist); | |
666 else | |
667 redraw_tree(tree); | |
668 if (old != tree->current) | |
669 tree_selection_changed(tree, old, tree->current); | |
670 | |
671 return TRUE; | |
672 } | |
673 | |
674 static gboolean | |
675 action_page_down(GntBindable *bind, GList *null) | |
676 { | |
677 GntTree *tree = GNT_TREE(bind); | |
678 GntTreeRow *old = tree->current; | |
679 GntTreeRow *row = get_next(tree->bottom); | |
680 if (row) | |
681 { | |
682 int dist = get_distance(tree->top, tree->current); | |
683 tree->top = tree->bottom; | |
684 tree->current = get_next_n_opt(tree->top, dist, NULL); | |
685 redraw_tree(tree); | |
686 } | |
687 else if (tree->current != tree->bottom) | |
688 { | |
689 tree->current = tree->bottom; | |
690 redraw_tree(tree); | |
691 } | |
692 | |
693 if (old != tree->current) | |
694 tree_selection_changed(tree, old, tree->current); | |
695 return TRUE; | |
696 } | |
697 | |
698 static gboolean | |
699 action_page_up(GntBindable *bind, GList *null) | |
700 { | |
701 GntWidget *widget = GNT_WIDGET(bind); | |
702 GntTree *tree = GNT_TREE(bind); | |
703 GntTreeRow *row; | |
704 GntTreeRow *old = tree->current; | |
705 | |
706 if (tree->top != tree->root) | |
707 { | |
708 int dist = get_distance(tree->top, tree->current); | |
709 row = get_prev_n(tree->top, widget->priv.height - 1 - | |
710 tree->show_title * 2 - 2 * (GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_NO_BORDER) == 0)); | |
711 if (row == NULL) | |
712 row = tree->root; | |
713 tree->top = row; | |
714 tree->current = get_next_n_opt(tree->top, dist, NULL); | |
715 redraw_tree(tree); | |
716 } | |
717 else if (tree->current != tree->top) | |
718 { | |
719 tree->current = tree->top; | |
720 redraw_tree(tree); | |
721 } | |
722 if (old != tree->current) | |
723 tree_selection_changed(tree, old, tree->current); | |
724 return TRUE; | |
725 } | |
726 | |
727 static void | |
728 end_search(GntTree *tree) | |
729 { | |
730 if (tree->search) { | |
731 g_source_remove(tree->search_timeout); | |
732 g_string_free(tree->search, TRUE); | |
733 tree->search = NULL; | |
734 tree->search_timeout = 0; | |
735 } | |
736 } | |
737 | |
738 static gboolean | |
739 search_timeout(gpointer data) | |
740 { | |
741 GntTree *tree = data; | |
742 | |
743 end_search(tree); | |
744 redraw_tree(tree); | |
745 | |
746 return FALSE; | |
747 } | |
748 | |
749 static gboolean | |
750 gnt_tree_key_pressed(GntWidget *widget, const char *text) | |
751 { | |
752 GntTree *tree = GNT_TREE(widget); | |
753 GntTreeRow *old = tree->current; | |
754 | |
755 if (text[0] == '\r') { | |
756 end_search(tree); | |
757 gnt_widget_activate(widget); | |
758 } else if (tree->search) { | |
16249
f02e611bdb6b
Show the search string in the tree.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16203
diff
changeset
|
759 gboolean changed = TRUE; |
15818 | 760 if (isalnum(*text)) { |
761 tree->search = g_string_append_c(tree->search, *text); | |
16249
f02e611bdb6b
Show the search string in the tree.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16203
diff
changeset
|
762 } else if (g_utf8_collate(text, GNT_KEY_BACKSPACE) == 0) { |
f02e611bdb6b
Show the search string in the tree.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16203
diff
changeset
|
763 if (tree->search->len) |
f02e611bdb6b
Show the search string in the tree.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16203
diff
changeset
|
764 tree->search->str[--tree->search->len] = '\0'; |
f02e611bdb6b
Show the search string in the tree.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16203
diff
changeset
|
765 } else |
f02e611bdb6b
Show the search string in the tree.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16203
diff
changeset
|
766 changed = FALSE; |
f02e611bdb6b
Show the search string in the tree.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16203
diff
changeset
|
767 if (changed) { |
15818 | 768 redraw_tree(tree); |
769 g_source_remove(tree->search_timeout); | |
770 tree->search_timeout = g_timeout_add(SEARCH_TIMEOUT, search_timeout, tree); | |
771 } | |
772 return TRUE; | |
773 } else if (text[0] == ' ' && text[1] == 0) { | |
774 /* Space pressed */ | |
775 GntTreeRow *row = tree->current; | |
776 if (row && row->child) | |
777 { | |
778 row->collapsed = !row->collapsed; | |
779 redraw_tree(tree); | |
16105
1983ecd15174
Remember the collapsed state of groups in the blist
Richard Nelson <wabz@pidgin.im>
parents:
15972
diff
changeset
|
780 g_signal_emit(tree, signals[SIG_COLLAPSED], 0, row->key, row->collapsed); |
15818 | 781 } |
782 else if (row && row->choice) | |
783 { | |
784 row->isselected = !row->isselected; | |
785 g_signal_emit(tree, signals[SIG_TOGGLED], 0, row->key); | |
786 redraw_tree(tree); | |
787 } | |
18098
1c8853f6bfc6
I am not sure why I made this change. But it looks good.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18069
diff
changeset
|
788 } else { |
1c8853f6bfc6
I am not sure why I made this change. But it looks good.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18069
diff
changeset
|
789 return FALSE; |
15818 | 790 } |
791 | |
792 if (old != tree->current) | |
793 { | |
794 tree_selection_changed(tree, old, tree->current); | |
795 } | |
796 | |
18098
1c8853f6bfc6
I am not sure why I made this change. But it looks good.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18069
diff
changeset
|
797 return TRUE; |
15818 | 798 } |
799 | |
800 static void | |
801 gnt_tree_destroy(GntWidget *widget) | |
802 { | |
803 GntTree *tree = GNT_TREE(widget); | |
804 int i; | |
805 | |
806 end_search(tree); | |
807 if (tree->hash) | |
808 g_hash_table_destroy(tree->hash); | |
809 g_list_free(tree->list); | |
810 | |
811 for (i = 0; i < tree->ncol; i++) | |
812 { | |
813 g_free(tree->columns[i].title); | |
814 } | |
815 g_free(tree->columns); | |
816 } | |
817 | |
818 static gboolean | |
819 gnt_tree_clicked(GntWidget *widget, GntMouseEvent event, int x, int y) | |
820 { | |
821 GntTree *tree = GNT_TREE(widget); | |
822 GntTreeRow *old = tree->current; | |
823 if (event == GNT_MOUSE_SCROLL_UP) { | |
824 action_up(GNT_BINDABLE(widget), NULL); | |
825 } else if (event == GNT_MOUSE_SCROLL_DOWN) { | |
826 action_down(GNT_BINDABLE(widget), NULL); | |
827 } else if (event == GNT_LEFT_MOUSE_DOWN) { | |
828 GntTreeRow *row; | |
829 GntTree *tree = GNT_TREE(widget); | |
830 int pos = 1; | |
831 if (GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_NO_BORDER)) | |
832 pos = 0; | |
833 if (tree->show_title) | |
834 pos += 2; | |
835 pos = y - widget->priv.y - pos; | |
836 row = get_next_n(tree->top, pos); | |
837 if (row && tree->current != row) { | |
838 GntTreeRow *old = tree->current; | |
839 tree->current = row; | |
840 redraw_tree(tree); | |
841 tree_selection_changed(tree, old, tree->current); | |
842 } else if (row && row == tree->current) { | |
843 if (row->choice) { | |
844 row->isselected = !row->isselected; | |
845 g_signal_emit(tree, signals[SIG_TOGGLED], 0, row->key); | |
846 redraw_tree(tree); | |
847 } else { | |
848 gnt_widget_activate(widget); | |
849 } | |
850 } | |
851 } else { | |
852 return FALSE; | |
853 } | |
854 if (old != tree->current) { | |
855 tree_selection_changed(tree, old, tree->current); | |
856 } | |
857 return TRUE; | |
858 } | |
859 | |
860 static void | |
861 gnt_tree_size_changed(GntWidget *widget, int w, int h) | |
862 { | |
863 GntTree *tree = GNT_TREE(widget); | |
864 if (widget->priv.width <= 0) | |
865 return; | |
18404
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
866 |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
867 readjust_columns(tree); |
15818 | 868 } |
869 | |
870 static gboolean | |
871 start_search(GntBindable *bindable, GList *list) | |
872 { | |
873 GntTree *tree = GNT_TREE(bindable); | |
874 if (tree->search) | |
875 return FALSE; | |
876 tree->search = g_string_new(NULL); | |
877 tree->search_timeout = g_timeout_add(SEARCH_TIMEOUT, search_timeout, tree); | |
878 return TRUE; | |
879 } | |
880 | |
881 static gboolean | |
882 end_search_action(GntBindable *bindable, GList *list) | |
883 { | |
884 GntTree *tree = GNT_TREE(bindable); | |
885 if (tree->search == NULL) | |
886 return FALSE; | |
887 end_search(tree); | |
888 redraw_tree(tree); | |
889 return TRUE; | |
890 } | |
891 | |
892 static void | |
893 gnt_tree_class_init(GntTreeClass *klass) | |
894 { | |
895 GntBindableClass *bindable = GNT_BINDABLE_CLASS(klass); | |
896 parent_class = GNT_WIDGET_CLASS(klass); | |
897 parent_class->destroy = gnt_tree_destroy; | |
898 parent_class->draw = gnt_tree_draw; | |
899 parent_class->map = gnt_tree_map; | |
900 parent_class->size_request = gnt_tree_size_request; | |
901 parent_class->key_pressed = gnt_tree_key_pressed; | |
902 parent_class->clicked = gnt_tree_clicked; | |
903 parent_class->size_changed = gnt_tree_size_changed; | |
904 | |
905 signals[SIG_SELECTION_CHANGED] = | |
906 g_signal_new("selection-changed", | |
907 G_TYPE_FROM_CLASS(klass), | |
908 G_SIGNAL_RUN_LAST, | |
909 G_STRUCT_OFFSET(GntTreeClass, selection_changed), | |
910 NULL, NULL, | |
911 gnt_closure_marshal_VOID__POINTER_POINTER, | |
912 G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_POINTER); | |
913 signals[SIG_SCROLLED] = | |
914 g_signal_new("scrolled", | |
915 G_TYPE_FROM_CLASS(klass), | |
916 G_SIGNAL_RUN_LAST, | |
917 0, | |
918 NULL, NULL, | |
919 g_cclosure_marshal_VOID__INT, | |
920 G_TYPE_NONE, 1, G_TYPE_INT); | |
921 signals[SIG_TOGGLED] = | |
922 g_signal_new("toggled", | |
923 G_TYPE_FROM_CLASS(klass), | |
924 G_SIGNAL_RUN_LAST, | |
925 G_STRUCT_OFFSET(GntTreeClass, toggled), | |
926 NULL, NULL, | |
927 g_cclosure_marshal_VOID__POINTER, | |
928 G_TYPE_NONE, 1, G_TYPE_POINTER); | |
16105
1983ecd15174
Remember the collapsed state of groups in the blist
Richard Nelson <wabz@pidgin.im>
parents:
15972
diff
changeset
|
929 signals[SIG_COLLAPSED] = |
1983ecd15174
Remember the collapsed state of groups in the blist
Richard Nelson <wabz@pidgin.im>
parents:
15972
diff
changeset
|
930 g_signal_new("collapse-toggled", |
1983ecd15174
Remember the collapsed state of groups in the blist
Richard Nelson <wabz@pidgin.im>
parents:
15972
diff
changeset
|
931 G_TYPE_FROM_CLASS(klass), |
1983ecd15174
Remember the collapsed state of groups in the blist
Richard Nelson <wabz@pidgin.im>
parents:
15972
diff
changeset
|
932 G_SIGNAL_RUN_LAST, |
1983ecd15174
Remember the collapsed state of groups in the blist
Richard Nelson <wabz@pidgin.im>
parents:
15972
diff
changeset
|
933 0, |
1983ecd15174
Remember the collapsed state of groups in the blist
Richard Nelson <wabz@pidgin.im>
parents:
15972
diff
changeset
|
934 NULL, NULL, |
1983ecd15174
Remember the collapsed state of groups in the blist
Richard Nelson <wabz@pidgin.im>
parents:
15972
diff
changeset
|
935 gnt_closure_marshal_VOID__POINTER_BOOLEAN, |
1983ecd15174
Remember the collapsed state of groups in the blist
Richard Nelson <wabz@pidgin.im>
parents:
15972
diff
changeset
|
936 G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_BOOLEAN); |
15818 | 937 |
938 gnt_bindable_class_register_action(bindable, "move-up", action_up, | |
939 GNT_KEY_UP, NULL); | |
940 gnt_bindable_register_binding(bindable, "move-up", GNT_KEY_CTRL_P, NULL); | |
941 gnt_bindable_class_register_action(bindable, "move-down", action_down, | |
942 GNT_KEY_DOWN, NULL); | |
943 gnt_bindable_register_binding(bindable, "move-down", GNT_KEY_CTRL_N, NULL); | |
15972
5eb0621e0760
Backspace to jump to parent.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15971
diff
changeset
|
944 gnt_bindable_class_register_action(bindable, "move-parent", action_move_parent, |
5eb0621e0760
Backspace to jump to parent.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15971
diff
changeset
|
945 GNT_KEY_BACKSPACE, NULL); |
15818 | 946 gnt_bindable_class_register_action(bindable, "page-up", action_page_up, |
947 GNT_KEY_PGUP, NULL); | |
948 gnt_bindable_class_register_action(bindable, "page-down", action_page_down, | |
949 GNT_KEY_PGDOWN, NULL); | |
950 gnt_bindable_class_register_action(bindable, "start-search", start_search, | |
951 "/", NULL); | |
952 gnt_bindable_class_register_action(bindable, "end-search", end_search_action, | |
953 "\033", NULL); | |
954 | |
955 gnt_style_read_actions(G_OBJECT_CLASS_TYPE(klass), bindable); | |
956 GNTDEBUG; | |
957 } | |
958 | |
959 static void | |
960 gnt_tree_init(GTypeInstance *instance, gpointer class) | |
961 { | |
962 GntWidget *widget = GNT_WIDGET(instance); | |
963 GntTree *tree = GNT_TREE(widget); | |
964 tree->show_separator = TRUE; | |
965 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_GROW_X | GNT_WIDGET_GROW_Y); | |
966 widget->priv.minw = 4; | |
967 widget->priv.minh = 1; | |
968 GNTDEBUG; | |
969 } | |
970 | |
971 /****************************************************************************** | |
972 * GntTree API | |
973 *****************************************************************************/ | |
974 GType | |
975 gnt_tree_get_gtype(void) | |
976 { | |
977 static GType type = 0; | |
978 | |
979 if(type == 0) | |
980 { | |
981 static const GTypeInfo info = { | |
982 sizeof(GntTreeClass), | |
983 NULL, /* base_init */ | |
984 NULL, /* base_finalize */ | |
985 (GClassInitFunc)gnt_tree_class_init, | |
986 NULL, /* class_finalize */ | |
987 NULL, /* class_data */ | |
988 sizeof(GntTree), | |
989 0, /* n_preallocs */ | |
990 gnt_tree_init, /* instance_init */ | |
991 NULL /* value_table */ | |
992 }; | |
993 | |
994 type = g_type_register_static(GNT_TYPE_WIDGET, | |
995 "GntTree", | |
996 &info, 0); | |
997 } | |
998 | |
999 return type; | |
1000 } | |
1001 | |
1002 static void | |
1003 free_tree_col(gpointer data) | |
1004 { | |
1005 GntTreeCol *col = data; | |
1006 | |
1007 g_free(col->text); | |
1008 g_free(col); | |
1009 } | |
1010 | |
1011 static void | |
1012 free_tree_row(gpointer data) | |
1013 { | |
1014 GntTreeRow *row = data; | |
1015 | |
1016 if (!row) | |
1017 return; | |
1018 | |
1019 g_list_foreach(row->columns, (GFunc)free_tree_col, NULL); | |
1020 g_list_free(row->columns); | |
1021 g_free(row); | |
1022 } | |
1023 | |
1024 GntWidget *gnt_tree_new() | |
1025 { | |
1026 return gnt_tree_new_with_columns(1); | |
1027 } | |
1028 | |
1029 void gnt_tree_set_visible_rows(GntTree *tree, int rows) | |
1030 { | |
1031 GntWidget *widget = GNT_WIDGET(tree); | |
1032 widget->priv.height = rows; | |
1033 if (!GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_NO_BORDER)) | |
1034 widget->priv.height += 2; | |
1035 } | |
1036 | |
1037 int gnt_tree_get_visible_rows(GntTree *tree) | |
1038 { | |
1039 GntWidget *widget = GNT_WIDGET(tree); | |
1040 int ret = widget->priv.height; | |
1041 if (!GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_NO_BORDER)) | |
1042 ret -= 2; | |
1043 return ret; | |
1044 } | |
1045 | |
18118
ab6d2763b8d8
Re-fix the DBus list handling code by killing const GList* / const GSList*
Richard Laager <rlaager@wiktel.com>
parents:
18098
diff
changeset
|
1046 GList *gnt_tree_get_rows(GntTree *tree) |
15818 | 1047 { |
1048 return tree->list; | |
1049 } | |
1050 | |
1051 void gnt_tree_scroll(GntTree *tree, int count) | |
1052 { | |
1053 GntTreeRow *row; | |
1054 | |
1055 if (count < 0) | |
1056 { | |
1057 if (get_root_distance(tree->top) == 0) | |
1058 return; | |
1059 row = get_prev_n(tree->top, -count); | |
1060 if (row == NULL) | |
1061 row = tree->root; | |
1062 tree->top = row; | |
1063 } | |
1064 else | |
1065 { | |
1066 get_next_n_opt(tree->bottom, count, &count); | |
1067 tree->top = get_next_n(tree->top, count); | |
1068 } | |
1069 | |
1070 redraw_tree(tree); | |
1071 g_signal_emit(tree, signals[SIG_SCROLLED], 0, count); | |
1072 } | |
1073 | |
1074 static gpointer | |
1075 find_position(GntTree *tree, gpointer key, gpointer parent) | |
1076 { | |
1077 GntTreeRow *row; | |
1078 | |
1079 if (tree->compare == NULL) | |
1080 return NULL; | |
1081 | |
1082 if (parent == NULL) | |
1083 row = tree->root; | |
1084 else | |
1085 row = g_hash_table_lookup(tree->hash, parent); | |
1086 | |
1087 if (!row) | |
1088 return NULL; | |
1089 | |
1090 if (parent) | |
1091 row = row->child; | |
1092 | |
1093 while (row) | |
1094 { | |
1095 if (tree->compare(key, row->key) < 0) | |
1096 return (row->prev ? row->prev->key : NULL); | |
1097 if (row->next) | |
1098 row = row->next; | |
1099 else | |
1100 return row->key; | |
1101 } | |
1102 return NULL; | |
1103 } | |
1104 | |
1105 void gnt_tree_sort_row(GntTree *tree, gpointer key) | |
1106 { | |
1107 GntTreeRow *row, *q, *s; | |
1108 int current, newp; | |
1109 | |
1110 if (!tree->compare) | |
1111 return; | |
1112 | |
1113 row = g_hash_table_lookup(tree->hash, key); | |
1114 g_return_if_fail(row != NULL); | |
1115 | |
1116 current = g_list_index(tree->list, key); | |
1117 | |
1118 if (row->parent) | |
1119 s = row->parent->child; | |
1120 else | |
1121 s = tree->root; | |
1122 | |
1123 q = NULL; | |
1124 while (s) { | |
1125 if (tree->compare(row->key, s->key) < 0) | |
1126 break; | |
1127 q = s; | |
1128 s = s->next; | |
1129 } | |
1130 | |
1131 /* Move row between q and s */ | |
1132 if (row == q || row == s) | |
1133 return; | |
1134 | |
1135 if (q == NULL) { | |
1136 /* row becomes the first child of its parent */ | |
1137 row->prev->next = row->next; /* row->prev cannot be NULL at this point */ | |
1138 if (row->next) | |
1139 row->next->prev = row->prev; | |
1140 if (row->parent) | |
1141 row->parent->child = row; | |
1142 else | |
1143 tree->root = row; | |
1144 row->next = s; | |
1145 s->prev = row; /* s cannot be NULL */ | |
1146 row->prev = NULL; | |
1147 newp = g_list_index(tree->list, s) - 1; | |
1148 } else { | |
1149 if (row->prev) { | |
1150 row->prev->next = row->next; | |
1151 } else { | |
1152 /* row was the first child of its parent */ | |
1153 if (row->parent) | |
1154 row->parent->child = row->next; | |
1155 else | |
1156 tree->top = row->next; | |
1157 } | |
1158 | |
1159 if (row->next) | |
1160 row->next->prev = row->prev; | |
1161 | |
1162 q->next = row; | |
1163 row->prev = q; | |
1164 if (s) | |
1165 s->prev = row; | |
1166 row->next = s; | |
1167 newp = g_list_index(tree->list, q) + 1; | |
1168 } | |
1169 tree->list = g_list_reposition_child(tree->list, current, newp); | |
1170 | |
1171 redraw_tree(tree); | |
1172 } | |
1173 | |
1174 GntTreeRow *gnt_tree_add_row_after(GntTree *tree, void *key, GntTreeRow *row, void *parent, void *bigbro) | |
1175 { | |
1176 GntTreeRow *pr = NULL; | |
1177 | |
1178 g_hash_table_replace(tree->hash, key, row); | |
1179 row->tree = tree; | |
1180 | |
1181 if (bigbro == NULL && tree->compare) | |
1182 { | |
1183 bigbro = find_position(tree, key, parent); | |
1184 } | |
1185 | |
1186 if (tree->root == NULL) | |
1187 { | |
1188 tree->root = row; | |
1189 tree->list = g_list_prepend(tree->list, key); | |
1190 } | |
1191 else | |
1192 { | |
1193 int position = 0; | |
1194 | |
1195 if (bigbro) | |
1196 { | |
1197 pr = g_hash_table_lookup(tree->hash, bigbro); | |
1198 if (pr) | |
1199 { | |
1200 if (pr->next) pr->next->prev = row; | |
1201 row->next = pr->next; | |
1202 row->prev = pr; | |
1203 pr->next = row; | |
1204 row->parent = pr->parent; | |
1205 | |
1206 position = g_list_index(tree->list, bigbro); | |
1207 } | |
1208 } | |
1209 | |
1210 if (pr == NULL && parent) | |
1211 { | |
1212 pr = g_hash_table_lookup(tree->hash, parent); | |
1213 if (pr) | |
1214 { | |
1215 if (pr->child) pr->child->prev = row; | |
1216 row->next = pr->child; | |
1217 pr->child = row; | |
1218 row->parent = pr; | |
1219 | |
1220 position = g_list_index(tree->list, parent); | |
1221 } | |
1222 } | |
1223 | |
1224 if (pr == NULL) | |
1225 { | |
1226 GntTreeRow *r = tree->root; | |
1227 row->next = r; | |
1228 if (r) r->prev = row; | |
1229 if (tree->current == tree->root) | |
1230 tree->current = row; | |
1231 tree->root = row; | |
1232 tree->list = g_list_prepend(tree->list, key); | |
1233 } | |
1234 else | |
1235 { | |
1236 tree->list = g_list_insert(tree->list, key, position + 1); | |
1237 } | |
1238 } | |
1239 | |
1240 row->key = key; | |
1241 row->data = NULL; | |
1242 | |
1243 redraw_tree(tree); | |
1244 | |
1245 return row; | |
1246 } | |
1247 | |
1248 GntTreeRow *gnt_tree_add_row_last(GntTree *tree, void *key, GntTreeRow *row, void *parent) | |
1249 { | |
1250 GntTreeRow *pr = NULL, *br = NULL; | |
1251 | |
1252 if (parent) | |
1253 pr = g_hash_table_lookup(tree->hash, parent); | |
1254 | |
1255 if (pr) | |
1256 br = pr->child; | |
1257 else | |
1258 br = tree->root; | |
1259 | |
1260 if (br) | |
1261 { | |
1262 while (br->next) | |
1263 br = br->next; | |
1264 } | |
1265 | |
1266 return gnt_tree_add_row_after(tree, key, row, parent, br ? br->key : NULL); | |
1267 } | |
1268 | |
1269 gpointer gnt_tree_get_selection_data(GntTree *tree) | |
1270 { | |
1271 if (tree->current) | |
1272 return tree->current->key; /* XXX: perhaps we should just get rid of 'data' */ | |
1273 return NULL; | |
1274 } | |
1275 | |
1276 char *gnt_tree_get_selection_text(GntTree *tree) | |
1277 { | |
1278 if (tree->current) | |
1279 return update_row_text(tree, tree->current); | |
1280 return NULL; | |
1281 } | |
1282 | |
1283 GList *gnt_tree_get_selection_text_list(GntTree *tree) | |
1284 { | |
1285 GList *list = NULL, *iter; | |
1286 int i; | |
1287 | |
1288 if (!tree->current) | |
1289 return NULL; | |
1290 | |
1291 for (i = 0, iter = tree->current->columns; i < tree->ncol && iter; | |
1292 i++, iter = iter->next) | |
1293 { | |
1294 GntTreeCol *col = iter->data; | |
1295 list = g_list_append(list, g_strdup(col->text)); | |
1296 } | |
1297 | |
1298 return list; | |
1299 } | |
1300 | |
1301 void gnt_tree_remove(GntTree *tree, gpointer key) | |
1302 { | |
1303 GntTreeRow *row = g_hash_table_lookup(tree->hash, key); | |
1304 static int depth = 0; /* Only redraw after all child nodes are removed */ | |
1305 if (row) | |
1306 { | |
1307 gboolean redraw = FALSE; | |
1308 | |
1309 if (row->child) { | |
1310 depth++; | |
1311 while (row->child) { | |
1312 gnt_tree_remove(tree, row->child->key); | |
1313 } | |
1314 depth--; | |
1315 } | |
1316 | |
1317 if (get_distance(tree->top, row) >= 0 && get_distance(row, tree->bottom) >= 0) | |
1318 redraw = TRUE; | |
1319 | |
1320 /* Update root/top/current/bottom if necessary */ | |
1321 if (tree->root == row) | |
1322 tree->root = get_next(row); | |
1323 if (tree->top == row) | |
1324 { | |
1325 if (tree->top != tree->root) | |
1326 tree->top = get_prev(row); | |
1327 else | |
1328 tree->top = get_next(row); | |
1329 } | |
1330 if (tree->current == row) | |
1331 { | |
1332 if (tree->current != tree->root) | |
1333 tree->current = get_prev(row); | |
1334 else | |
1335 tree->current = get_next(row); | |
1336 tree_selection_changed(tree, row, tree->current); | |
1337 } | |
1338 if (tree->bottom == row) | |
1339 { | |
1340 tree->bottom = get_prev(row); | |
1341 } | |
1342 | |
1343 /* Fix the links */ | |
1344 if (row->next) | |
1345 row->next->prev = row->prev; | |
1346 if (row->parent && row->parent->child == row) | |
1347 row->parent->child = row->next; | |
1348 if (row->prev) | |
1349 row->prev->next = row->next; | |
1350 | |
1351 g_hash_table_remove(tree->hash, key); | |
1352 tree->list = g_list_remove(tree->list, key); | |
1353 | |
1354 if (redraw && depth == 0) | |
1355 { | |
1356 redraw_tree(tree); | |
1357 } | |
1358 } | |
1359 } | |
1360 | |
1361 static gboolean | |
1362 return_true(gpointer key, gpointer data, gpointer null) | |
1363 { | |
1364 return TRUE; | |
1365 } | |
1366 | |
1367 void gnt_tree_remove_all(GntTree *tree) | |
1368 { | |
1369 tree->root = NULL; | |
1370 g_hash_table_foreach_remove(tree->hash, (GHRFunc)return_true, tree); | |
1371 g_list_free(tree->list); | |
1372 tree->list = NULL; | |
1373 tree->current = tree->top = tree->bottom = NULL; | |
1374 } | |
1375 | |
1376 int gnt_tree_get_selection_visible_line(GntTree *tree) | |
1377 { | |
1378 return get_distance(tree->top, tree->current) + | |
1379 !!(GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(tree), GNT_WIDGET_NO_BORDER)); | |
1380 } | |
1381 | |
1382 void gnt_tree_change_text(GntTree *tree, gpointer key, int colno, const char *text) | |
1383 { | |
1384 GntTreeRow *row; | |
1385 GntTreeCol *col; | |
1386 | |
1387 g_return_if_fail(colno < tree->ncol); | |
1388 | |
1389 row = g_hash_table_lookup(tree->hash, key); | |
1390 if (row) | |
1391 { | |
1392 col = g_list_nth_data(row->columns, colno); | |
1393 g_free(col->text); | |
16283
5187395d6b78
Debug is not necessary anymore, and don't crash on null text.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16249
diff
changeset
|
1394 col->text = g_strdup(text ? text : ""); |
15818 | 1395 |
1396 if (get_distance(tree->top, row) >= 0 && get_distance(row, tree->bottom) >= 0) | |
1397 redraw_tree(tree); | |
1398 } | |
1399 } | |
1400 | |
1401 GntTreeRow *gnt_tree_add_choice(GntTree *tree, void *key, GntTreeRow *row, void *parent, void *bigbro) | |
1402 { | |
1403 GntTreeRow *r; | |
1404 r = g_hash_table_lookup(tree->hash, key); | |
1405 g_return_val_if_fail(!r || !r->choice, NULL); | |
1406 | |
1407 if (bigbro == NULL) { | |
1408 if (tree->compare) | |
1409 bigbro = find_position(tree, key, parent); | |
1410 else { | |
1411 r = g_hash_table_lookup(tree->hash, parent); | |
1412 if (!r) | |
1413 r = tree->root; | |
1414 else | |
1415 r = r->child; | |
1416 if (r) { | |
1417 while (r->next) | |
1418 r = r->next; | |
1419 bigbro = r->key; | |
1420 } | |
1421 } | |
1422 } | |
1423 row = gnt_tree_add_row_after(tree, key, row, parent, bigbro); | |
1424 row->choice = TRUE; | |
1425 | |
1426 return row; | |
1427 } | |
1428 | |
1429 void gnt_tree_set_choice(GntTree *tree, void *key, gboolean set) | |
1430 { | |
1431 GntTreeRow *row = g_hash_table_lookup(tree->hash, key); | |
1432 | |
1433 if (!row) | |
1434 return; | |
1435 g_return_if_fail(row->choice); | |
1436 | |
1437 row->isselected = set; | |
1438 redraw_tree(tree); | |
1439 } | |
1440 | |
1441 gboolean gnt_tree_get_choice(GntTree *tree, void *key) | |
1442 { | |
1443 GntTreeRow *row = g_hash_table_lookup(tree->hash, key); | |
1444 | |
1445 if (!row) | |
1446 return FALSE; | |
1447 g_return_val_if_fail(row->choice, FALSE); | |
1448 | |
1449 return row->isselected; | |
1450 } | |
1451 | |
1452 void gnt_tree_set_row_flags(GntTree *tree, void *key, GntTextFormatFlags flags) | |
1453 { | |
1454 GntTreeRow *row = g_hash_table_lookup(tree->hash, key); | |
1455 if (!row || row->flags == flags) | |
1456 return; | |
1457 | |
1458 row->flags = flags; | |
1459 redraw_tree(tree); /* XXX: It shouldn't be necessary to redraw the whole darned tree */ | |
1460 } | |
1461 | |
1462 void gnt_tree_set_selected(GntTree *tree , void *key) | |
1463 { | |
1464 int dist; | |
1465 GntTreeRow *row = g_hash_table_lookup(tree->hash, key); | |
16203
4f6a6443a1e3
emit/handle gnttree signals appropriately
Richard Nelson <wabz@pidgin.im>
parents:
16123
diff
changeset
|
1466 if (!row || row == tree->current) |
15818 | 1467 return; |
1468 | |
1469 if (tree->top == NULL) | |
1470 tree->top = row; | |
1471 if (tree->bottom == NULL) | |
1472 tree->bottom = row; | |
1473 | |
1474 tree->current = row; | |
1475 if ((dist = get_distance(tree->current, tree->bottom)) < 0) | |
1476 gnt_tree_scroll(tree, -dist); | |
1477 else if ((dist = get_distance(tree->current, tree->top)) > 0) | |
1478 gnt_tree_scroll(tree, -dist); | |
1479 else | |
1480 redraw_tree(tree); | |
16203
4f6a6443a1e3
emit/handle gnttree signals appropriately
Richard Nelson <wabz@pidgin.im>
parents:
16123
diff
changeset
|
1481 tree_selection_changed(tree, row, tree->current); |
15818 | 1482 } |
1483 | |
1484 void _gnt_tree_init_internals(GntTree *tree, int col) | |
1485 { | |
1486 tree->ncol = col; | |
1487 tree->hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, free_tree_row); | |
1488 tree->columns = g_new0(struct _GntTreeColInfo, col); | |
1489 while (col--) | |
1490 { | |
1491 tree->columns[col].width = 15; | |
1492 } | |
1493 tree->list = NULL; | |
1494 tree->show_title = FALSE; | |
1495 } | |
1496 | |
1497 GntWidget *gnt_tree_new_with_columns(int col) | |
1498 { | |
1499 GntWidget *widget = g_object_new(GNT_TYPE_TREE, NULL); | |
1500 GntTree *tree = GNT_TREE(widget); | |
1501 | |
1502 _gnt_tree_init_internals(tree, col); | |
1503 | |
1504 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_NO_SHADOW); | |
1505 gnt_widget_set_take_focus(widget, TRUE); | |
1506 | |
1507 return widget; | |
1508 } | |
1509 | |
1510 GntTreeRow *gnt_tree_create_row_from_list(GntTree *tree, GList *list) | |
1511 { | |
1512 GList *iter; | |
1513 int i; | |
1514 GntTreeRow *row = g_new0(GntTreeRow, 1); | |
1515 | |
1516 for (i = 0, iter = list; i < tree->ncol && iter; iter = iter->next, i++) | |
1517 { | |
1518 GntTreeCol *col = g_new0(GntTreeCol, 1); | |
1519 col->span = 1; | |
1520 col->text = g_strdup(iter->data ? iter->data : ""); | |
1521 | |
1522 row->columns = g_list_append(row->columns, col); | |
1523 } | |
1524 | |
1525 return row; | |
1526 } | |
1527 | |
1528 GntTreeRow *gnt_tree_create_row(GntTree *tree, ...) | |
1529 { | |
1530 int i; | |
1531 va_list args; | |
1532 GList *list = NULL; | |
1533 GntTreeRow *row; | |
1534 | |
1535 va_start(args, tree); | |
1536 for (i = 0; i < tree->ncol; i++) | |
1537 { | |
1538 list = g_list_append(list, va_arg(args, char *)); | |
1539 } | |
1540 va_end(args); | |
1541 | |
1542 row = gnt_tree_create_row_from_list(tree, list); | |
1543 g_list_free(list); | |
1544 | |
1545 return row; | |
1546 } | |
1547 | |
1548 void gnt_tree_set_col_width(GntTree *tree, int col, int width) | |
1549 { | |
1550 g_return_if_fail(col < tree->ncol); | |
1551 | |
1552 tree->columns[col].width = width; | |
18404
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
1553 if (tree->columns[col].width_ratio == 0) |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
1554 tree->columns[col].width_ratio = width; |
15818 | 1555 } |
1556 | |
18348
1f9db757efc2
Oops. I missed this. This allows setting tree-column titles one by one.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18183
diff
changeset
|
1557 void gnt_tree_set_column_title(GntTree *tree, int index, const char *title) |
1f9db757efc2
Oops. I missed this. This allows setting tree-column titles one by one.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18183
diff
changeset
|
1558 { |
1f9db757efc2
Oops. I missed this. This allows setting tree-column titles one by one.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18183
diff
changeset
|
1559 g_free(tree->columns[index].title); |
1f9db757efc2
Oops. I missed this. This allows setting tree-column titles one by one.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18183
diff
changeset
|
1560 tree->columns[index].title = g_strdup(title); |
1f9db757efc2
Oops. I missed this. This allows setting tree-column titles one by one.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18183
diff
changeset
|
1561 } |
1f9db757efc2
Oops. I missed this. This allows setting tree-column titles one by one.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18183
diff
changeset
|
1562 |
15818 | 1563 void gnt_tree_set_column_titles(GntTree *tree, ...) |
1564 { | |
1565 int i; | |
1566 va_list args; | |
1567 | |
1568 va_start(args, tree); | |
1569 for (i = 0; i < tree->ncol; i++) | |
1570 { | |
1571 const char *title = va_arg(args, const char *); | |
1572 tree->columns[i].title = g_strdup(title); | |
1573 } | |
1574 va_end(args); | |
1575 } | |
1576 | |
1577 void gnt_tree_set_show_title(GntTree *tree, gboolean set) | |
1578 { | |
1579 tree->show_title = set; | |
1580 GNT_WIDGET(tree)->priv.minh = (set ? 6 : 4); | |
1581 } | |
1582 | |
1583 void gnt_tree_set_compare_func(GntTree *tree, GCompareFunc func) | |
1584 { | |
1585 tree->compare = func; | |
1586 } | |
1587 | |
1588 void gnt_tree_set_expanded(GntTree *tree, void *key, gboolean expanded) | |
1589 { | |
1590 GntTreeRow *row = g_hash_table_lookup(tree->hash, key); | |
1591 if (row) { | |
1592 row->collapsed = !expanded; | |
1593 if (GNT_WIDGET(tree)->window) | |
1594 gnt_widget_draw(GNT_WIDGET(tree)); | |
16203
4f6a6443a1e3
emit/handle gnttree signals appropriately
Richard Nelson <wabz@pidgin.im>
parents:
16123
diff
changeset
|
1595 g_signal_emit(tree, signals[SIG_COLLAPSED], 0, key, row->collapsed); |
15818 | 1596 } |
1597 } | |
1598 | |
1599 void gnt_tree_set_show_separator(GntTree *tree, gboolean set) | |
1600 { | |
1601 tree->show_separator = set; | |
1602 } | |
1603 | |
1604 void gnt_tree_adjust_columns(GntTree *tree) | |
1605 { | |
1606 GntTreeRow *row = tree->root; | |
16123
bc280c341679
We don't need to do anything about the heights here.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16105
diff
changeset
|
1607 int *widths, i, twidth; |
15818 | 1608 |
1609 widths = g_new0(int, tree->ncol); | |
1610 while (row) { | |
1611 GList *iter; | |
1612 for (i = 0, iter = row->columns; iter; iter = iter->next, i++) { | |
1613 GntTreeCol *col = iter->data; | |
1614 int w = gnt_util_onscreen_width(col->text, NULL); | |
15968
0ab73bf1fef1
check-items are wider.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15931
diff
changeset
|
1615 if (i == 0 && row->choice) |
0ab73bf1fef1
check-items are wider.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15931
diff
changeset
|
1616 w += 4; |
15972
5eb0621e0760
Backspace to jump to parent.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15971
diff
changeset
|
1617 if (i == 0) { |
5eb0621e0760
Backspace to jump to parent.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15971
diff
changeset
|
1618 w += find_depth(row) * TAB_SIZE; |
5eb0621e0760
Backspace to jump to parent.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15971
diff
changeset
|
1619 } |
15818 | 1620 if (widths[i] < w) |
1621 widths[i] = w; | |
1622 } | |
15972
5eb0621e0760
Backspace to jump to parent.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15971
diff
changeset
|
1623 row = get_next(row); |
15818 | 1624 } |
1625 | |
1626 twidth = 1 + 2 * (!GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(tree), GNT_WIDGET_NO_BORDER)); | |
1627 for (i = 0; i < tree->ncol; i++) { | |
1628 gnt_tree_set_col_width(tree, i, widths[i]); | |
18404
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
1629 if (!COLUMN_INVISIBLE(tree, i)) |
15970
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
1630 twidth += widths[i] + (tree->show_separator ? 1 : 0) + 1; |
15818 | 1631 } |
1632 g_free(widths); | |
1633 | |
16123
bc280c341679
We don't need to do anything about the heights here.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16105
diff
changeset
|
1634 gnt_widget_set_size(GNT_WIDGET(tree), twidth, -1); |
15818 | 1635 } |
1636 | |
1637 void gnt_tree_set_hash_fns(GntTree *tree, gpointer hash, gpointer eq, gpointer kd) | |
1638 { | |
1639 g_hash_table_foreach_remove(tree->hash, return_true, NULL); | |
1640 g_hash_table_destroy(tree->hash); | |
1641 tree->hash = g_hash_table_new_full(hash, eq, kd, free_tree_row); | |
1642 } | |
1643 | |
18404
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
1644 static void |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
1645 set_column_flag(GntTree *tree, int col, GntTreeColumnFlag flag, gboolean set) |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
1646 { |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
1647 if (set) |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
1648 tree->columns[col].flags |= flag; |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
1649 else |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
1650 tree->columns[col].flags &= ~flag; |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
1651 } |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
1652 |
15970
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
1653 void gnt_tree_set_column_visible(GntTree *tree, int col, gboolean vis) |
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
1654 { |
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
1655 g_return_if_fail(col < tree->ncol); |
18404
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
1656 set_column_flag(tree, col, GNT_TREE_COLUMN_INVISIBLE, !vis); |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
1657 } |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
1658 |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
1659 void gnt_tree_set_column_resizable(GntTree *tree, int col, gboolean res) |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
1660 { |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
1661 g_return_if_fail(col < tree->ncol); |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
1662 set_column_flag(tree, col, GNT_TREE_COLUMN_FIXED_SIZE, !res); |
15970
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
1663 } |
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
1664 |
18404
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
1665 void gnt_tree_set_column_width_ratio(GntTree *tree, int cols[]) |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
1666 { |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
1667 int i; |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
1668 for (i = 0; i < tree->ncol && cols[i]; i++) { |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
1669 tree->columns[i].width_ratio = cols[i]; |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
1670 } |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
1671 } |
9a0f99ea664d
Resize tree-columns nicely when the tree is resized. We can tell it to
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18348
diff
changeset
|
1672 |