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