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