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