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