Mercurial > pidgin
annotate finch/libgnt/gnttree.c @ 18362:65ed0916d3bc
Allowing specifying different styles for different applications.
author | Sadrul Habib Chowdhury <imadil@gmail.com> |
---|---|
date | Sat, 30 Jun 2007 00:53:24 +0000 |
parents | 1f9db757efc2 |
children | 9a0f99ea664d |
rev | line source |
---|---|
18049
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16577
diff
changeset
|
1 /** |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16577
diff
changeset
|
2 * GNT - The GLib Ncurses Toolkit |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16577
diff
changeset
|
3 * |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16577
diff
changeset
|
4 * GNT is the legal property of its developers, whose names are too numerous |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16577
diff
changeset
|
5 * to list here. Please refer to the COPYRIGHT file distributed with this |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16577
diff
changeset
|
6 * source distribution. |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16577
diff
changeset
|
7 * |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16577
diff
changeset
|
8 * This library is free software; you can redistribute it and/or modify |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16577
diff
changeset
|
9 * it under the terms of the GNU General Public License as published by |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16577
diff
changeset
|
10 * the Free Software Foundation; either version 2 of the License, or |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16577
diff
changeset
|
11 * (at your option) any later version. |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16577
diff
changeset
|
12 * |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16577
diff
changeset
|
13 * This program is distributed in the hope that it will be useful, |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16577
diff
changeset
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16577
diff
changeset
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16577
diff
changeset
|
16 * GNU General Public License for more details. |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16577
diff
changeset
|
17 * |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16577
diff
changeset
|
18 * You should have received a copy of the GNU General Public License |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16577
diff
changeset
|
19 * along with this program; if not, write to the Free Software |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16577
diff
changeset
|
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16577
diff
changeset
|
21 */ |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16577
diff
changeset
|
22 |
15817 | 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) |
15817 | 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, |
15817 | 40 SIGS, |
41 }; | |
42 | |
43 #define TAB_SIZE 3 | |
44 | |
45 /* XXX: Make this one into a GObject? | |
46 * ... Probably not */ | |
15928
f00f2e283ffb
Some define changes. This helps in generating the python bindings.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
47 struct _GntTreeRow |
15817 | 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 | |
15928
f00f2e283ffb
Some define changes. This helps in generating the python bindings.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
67 struct _GntTreeCol |
15817 | 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 |
15817 | 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; | |
16577
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); |
15817 | 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--; |
15817 | 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; |
15817 | 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 |
15817 | 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, '|'); |
15817 | 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; |
15817 | 305 |
18183
99969affedf5
I think this fixes the ellipsization in the trees.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18118
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; |
15817 | 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, ""); |
15817 | 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 |
15817 | 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; |
15817 | 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); |
15817 | 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) { |
15817 | 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 } |
15817 | 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 |
15817 | 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; |
15817 | 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 |
15817 | 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; |
15817 | 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 |
15817 | 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; |
15817 | 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) { |
15817 | 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); |
15817 | 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 } | |
18098
1c8853f6bfc6
I am not sure why I made this change. But it looks good.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18069
diff
changeset
|
756 } else { |
1c8853f6bfc6
I am not sure why I made this change. But it looks good.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18069
diff
changeset
|
757 return FALSE; |
15817 | 758 } |
759 | |
760 if (old != tree->current) | |
761 { | |
762 tree_selection_changed(tree, old, tree->current); | |
763 } | |
764 | |
18098
1c8853f6bfc6
I am not sure why I made this change. But it looks good.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18069
diff
changeset
|
765 return TRUE; |
15817 | 766 } |
767 | |
768 static void | |
769 gnt_tree_destroy(GntWidget *widget) | |
770 { | |
771 GntTree *tree = GNT_TREE(widget); | |
772 int i; | |
773 | |
774 end_search(tree); | |
775 if (tree->hash) | |
776 g_hash_table_destroy(tree->hash); | |
777 g_list_free(tree->list); | |
778 | |
779 for (i = 0; i < tree->ncol; i++) | |
780 { | |
781 g_free(tree->columns[i].title); | |
782 } | |
783 g_free(tree->columns); | |
784 } | |
785 | |
786 static gboolean | |
787 gnt_tree_clicked(GntWidget *widget, GntMouseEvent event, int x, int y) | |
788 { | |
789 GntTree *tree = GNT_TREE(widget); | |
790 GntTreeRow *old = tree->current; | |
791 if (event == GNT_MOUSE_SCROLL_UP) { | |
792 action_up(GNT_BINDABLE(widget), NULL); | |
793 } else if (event == GNT_MOUSE_SCROLL_DOWN) { | |
794 action_down(GNT_BINDABLE(widget), NULL); | |
795 } else if (event == GNT_LEFT_MOUSE_DOWN) { | |
796 GntTreeRow *row; | |
797 GntTree *tree = GNT_TREE(widget); | |
798 int pos = 1; | |
799 if (GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_NO_BORDER)) | |
800 pos = 0; | |
801 if (tree->show_title) | |
802 pos += 2; | |
803 pos = y - widget->priv.y - pos; | |
804 row = get_next_n(tree->top, pos); | |
805 if (row && tree->current != row) { | |
806 GntTreeRow *old = tree->current; | |
807 tree->current = row; | |
808 redraw_tree(tree); | |
809 tree_selection_changed(tree, old, tree->current); | |
810 } else if (row && row == tree->current) { | |
811 if (row->choice) { | |
812 row->isselected = !row->isselected; | |
813 g_signal_emit(tree, signals[SIG_TOGGLED], 0, row->key); | |
814 redraw_tree(tree); | |
815 } else { | |
816 gnt_widget_activate(widget); | |
817 } | |
818 } | |
819 } else { | |
820 return FALSE; | |
821 } | |
822 if (old != tree->current) { | |
823 tree_selection_changed(tree, old, tree->current); | |
824 } | |
825 return TRUE; | |
826 } | |
827 | |
828 static void | |
829 gnt_tree_size_changed(GntWidget *widget, int w, int h) | |
830 { | |
831 GntTree *tree = GNT_TREE(widget); | |
832 int i; | |
833 int n = 0; | |
834 if (widget->priv.width <= 0) | |
835 return; | |
836 for (i = 0; i < tree->ncol; ++i) | |
837 n += tree->columns[i].width; | |
838 if (GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_NO_BORDER)) | |
839 tree->columns[tree->ncol - 1].width += widget->priv.width - n - 1 * tree->ncol; | |
840 else | |
841 tree->columns[tree->ncol - 1].width += widget->priv.width - n - 2 - 1 * tree->ncol; | |
842 } | |
843 | |
844 static gboolean | |
845 start_search(GntBindable *bindable, GList *list) | |
846 { | |
847 GntTree *tree = GNT_TREE(bindable); | |
848 if (tree->search) | |
849 return FALSE; | |
850 tree->search = g_string_new(NULL); | |
851 tree->search_timeout = g_timeout_add(SEARCH_TIMEOUT, search_timeout, tree); | |
852 return TRUE; | |
853 } | |
854 | |
855 static gboolean | |
856 end_search_action(GntBindable *bindable, GList *list) | |
857 { | |
858 GntTree *tree = GNT_TREE(bindable); | |
859 if (tree->search == NULL) | |
860 return FALSE; | |
861 end_search(tree); | |
862 redraw_tree(tree); | |
863 return TRUE; | |
864 } | |
865 | |
866 static void | |
867 gnt_tree_class_init(GntTreeClass *klass) | |
868 { | |
869 GntBindableClass *bindable = GNT_BINDABLE_CLASS(klass); | |
870 parent_class = GNT_WIDGET_CLASS(klass); | |
871 parent_class->destroy = gnt_tree_destroy; | |
872 parent_class->draw = gnt_tree_draw; | |
873 parent_class->map = gnt_tree_map; | |
874 parent_class->size_request = gnt_tree_size_request; | |
875 parent_class->key_pressed = gnt_tree_key_pressed; | |
876 parent_class->clicked = gnt_tree_clicked; | |
877 parent_class->size_changed = gnt_tree_size_changed; | |
878 | |
879 signals[SIG_SELECTION_CHANGED] = | |
880 g_signal_new("selection-changed", | |
881 G_TYPE_FROM_CLASS(klass), | |
882 G_SIGNAL_RUN_LAST, | |
883 G_STRUCT_OFFSET(GntTreeClass, selection_changed), | |
884 NULL, NULL, | |
885 gnt_closure_marshal_VOID__POINTER_POINTER, | |
886 G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_POINTER); | |
887 signals[SIG_SCROLLED] = | |
888 g_signal_new("scrolled", | |
889 G_TYPE_FROM_CLASS(klass), | |
890 G_SIGNAL_RUN_LAST, | |
891 0, | |
892 NULL, NULL, | |
893 g_cclosure_marshal_VOID__INT, | |
894 G_TYPE_NONE, 1, G_TYPE_INT); | |
895 signals[SIG_TOGGLED] = | |
896 g_signal_new("toggled", | |
897 G_TYPE_FROM_CLASS(klass), | |
898 G_SIGNAL_RUN_LAST, | |
899 G_STRUCT_OFFSET(GntTreeClass, toggled), | |
900 NULL, NULL, | |
901 g_cclosure_marshal_VOID__POINTER, | |
902 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
|
903 signals[SIG_COLLAPSED] = |
1983ecd15174
Remember the collapsed state of groups in the blist
Richard Nelson <wabz@pidgin.im>
parents:
15972
diff
changeset
|
904 g_signal_new("collapse-toggled", |
1983ecd15174
Remember the collapsed state of groups in the blist
Richard Nelson <wabz@pidgin.im>
parents:
15972
diff
changeset
|
905 G_TYPE_FROM_CLASS(klass), |
1983ecd15174
Remember the collapsed state of groups in the blist
Richard Nelson <wabz@pidgin.im>
parents:
15972
diff
changeset
|
906 G_SIGNAL_RUN_LAST, |
1983ecd15174
Remember the collapsed state of groups in the blist
Richard Nelson <wabz@pidgin.im>
parents:
15972
diff
changeset
|
907 0, |
1983ecd15174
Remember the collapsed state of groups in the blist
Richard Nelson <wabz@pidgin.im>
parents:
15972
diff
changeset
|
908 NULL, NULL, |
1983ecd15174
Remember the collapsed state of groups in the blist
Richard Nelson <wabz@pidgin.im>
parents:
15972
diff
changeset
|
909 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
|
910 G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_BOOLEAN); |
15817 | 911 |
912 gnt_bindable_class_register_action(bindable, "move-up", action_up, | |
913 GNT_KEY_UP, NULL); | |
914 gnt_bindable_register_binding(bindable, "move-up", GNT_KEY_CTRL_P, NULL); | |
915 gnt_bindable_class_register_action(bindable, "move-down", action_down, | |
916 GNT_KEY_DOWN, NULL); | |
917 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
|
918 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
|
919 GNT_KEY_BACKSPACE, NULL); |
15817 | 920 gnt_bindable_class_register_action(bindable, "page-up", action_page_up, |
921 GNT_KEY_PGUP, NULL); | |
922 gnt_bindable_class_register_action(bindable, "page-down", action_page_down, | |
923 GNT_KEY_PGDOWN, NULL); | |
924 gnt_bindable_class_register_action(bindable, "start-search", start_search, | |
925 "/", NULL); | |
926 gnt_bindable_class_register_action(bindable, "end-search", end_search_action, | |
927 "\033", NULL); | |
928 | |
929 gnt_style_read_actions(G_OBJECT_CLASS_TYPE(klass), bindable); | |
930 GNTDEBUG; | |
931 } | |
932 | |
933 static void | |
934 gnt_tree_init(GTypeInstance *instance, gpointer class) | |
935 { | |
936 GntWidget *widget = GNT_WIDGET(instance); | |
937 GntTree *tree = GNT_TREE(widget); | |
938 tree->show_separator = TRUE; | |
939 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_GROW_X | GNT_WIDGET_GROW_Y); | |
940 widget->priv.minw = 4; | |
941 widget->priv.minh = 1; | |
942 GNTDEBUG; | |
943 } | |
944 | |
945 /****************************************************************************** | |
946 * GntTree API | |
947 *****************************************************************************/ | |
948 GType | |
949 gnt_tree_get_gtype(void) | |
950 { | |
951 static GType type = 0; | |
952 | |
953 if(type == 0) | |
954 { | |
955 static const GTypeInfo info = { | |
956 sizeof(GntTreeClass), | |
957 NULL, /* base_init */ | |
958 NULL, /* base_finalize */ | |
959 (GClassInitFunc)gnt_tree_class_init, | |
960 NULL, /* class_finalize */ | |
961 NULL, /* class_data */ | |
962 sizeof(GntTree), | |
963 0, /* n_preallocs */ | |
964 gnt_tree_init, /* instance_init */ | |
965 NULL /* value_table */ | |
966 }; | |
967 | |
968 type = g_type_register_static(GNT_TYPE_WIDGET, | |
969 "GntTree", | |
970 &info, 0); | |
971 } | |
972 | |
973 return type; | |
974 } | |
975 | |
976 static void | |
977 free_tree_col(gpointer data) | |
978 { | |
979 GntTreeCol *col = data; | |
980 | |
981 g_free(col->text); | |
982 g_free(col); | |
983 } | |
984 | |
985 static void | |
986 free_tree_row(gpointer data) | |
987 { | |
988 GntTreeRow *row = data; | |
989 | |
990 if (!row) | |
991 return; | |
992 | |
993 g_list_foreach(row->columns, (GFunc)free_tree_col, NULL); | |
994 g_list_free(row->columns); | |
995 g_free(row); | |
996 } | |
997 | |
998 GntWidget *gnt_tree_new() | |
999 { | |
1000 return gnt_tree_new_with_columns(1); | |
1001 } | |
1002 | |
1003 void gnt_tree_set_visible_rows(GntTree *tree, int rows) | |
1004 { | |
1005 GntWidget *widget = GNT_WIDGET(tree); | |
1006 widget->priv.height = rows; | |
1007 if (!GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_NO_BORDER)) | |
1008 widget->priv.height += 2; | |
1009 } | |
1010 | |
1011 int gnt_tree_get_visible_rows(GntTree *tree) | |
1012 { | |
1013 GntWidget *widget = GNT_WIDGET(tree); | |
1014 int ret = widget->priv.height; | |
1015 if (!GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_NO_BORDER)) | |
1016 ret -= 2; | |
1017 return ret; | |
1018 } | |
1019 | |
18118
ab6d2763b8d8
Re-fix the DBus list handling code by killing const GList* / const GSList*
Richard Laager <rlaager@wiktel.com>
parents:
18098
diff
changeset
|
1020 GList *gnt_tree_get_rows(GntTree *tree) |
15817 | 1021 { |
1022 return tree->list; | |
1023 } | |
1024 | |
1025 void gnt_tree_scroll(GntTree *tree, int count) | |
1026 { | |
1027 GntTreeRow *row; | |
1028 | |
1029 if (count < 0) | |
1030 { | |
1031 if (get_root_distance(tree->top) == 0) | |
1032 return; | |
1033 row = get_prev_n(tree->top, -count); | |
1034 if (row == NULL) | |
1035 row = tree->root; | |
1036 tree->top = row; | |
1037 } | |
1038 else | |
1039 { | |
1040 get_next_n_opt(tree->bottom, count, &count); | |
1041 tree->top = get_next_n(tree->top, count); | |
1042 } | |
1043 | |
1044 redraw_tree(tree); | |
1045 g_signal_emit(tree, signals[SIG_SCROLLED], 0, count); | |
1046 } | |
1047 | |
1048 static gpointer | |
1049 find_position(GntTree *tree, gpointer key, gpointer parent) | |
1050 { | |
1051 GntTreeRow *row; | |
1052 | |
1053 if (tree->compare == NULL) | |
1054 return NULL; | |
1055 | |
1056 if (parent == NULL) | |
1057 row = tree->root; | |
1058 else | |
1059 row = g_hash_table_lookup(tree->hash, parent); | |
1060 | |
1061 if (!row) | |
1062 return NULL; | |
1063 | |
1064 if (parent) | |
1065 row = row->child; | |
1066 | |
1067 while (row) | |
1068 { | |
1069 if (tree->compare(key, row->key) < 0) | |
1070 return (row->prev ? row->prev->key : NULL); | |
1071 if (row->next) | |
1072 row = row->next; | |
1073 else | |
1074 return row->key; | |
1075 } | |
1076 return NULL; | |
1077 } | |
1078 | |
1079 void gnt_tree_sort_row(GntTree *tree, gpointer key) | |
1080 { | |
1081 GntTreeRow *row, *q, *s; | |
1082 int current, newp; | |
1083 | |
1084 if (!tree->compare) | |
1085 return; | |
1086 | |
1087 row = g_hash_table_lookup(tree->hash, key); | |
1088 g_return_if_fail(row != NULL); | |
1089 | |
1090 current = g_list_index(tree->list, key); | |
1091 | |
1092 if (row->parent) | |
1093 s = row->parent->child; | |
1094 else | |
1095 s = tree->root; | |
1096 | |
1097 q = NULL; | |
1098 while (s) { | |
1099 if (tree->compare(row->key, s->key) < 0) | |
1100 break; | |
1101 q = s; | |
1102 s = s->next; | |
1103 } | |
1104 | |
1105 /* Move row between q and s */ | |
1106 if (row == q || row == s) | |
1107 return; | |
1108 | |
1109 if (q == NULL) { | |
1110 /* row becomes the first child of its parent */ | |
1111 row->prev->next = row->next; /* row->prev cannot be NULL at this point */ | |
1112 if (row->next) | |
1113 row->next->prev = row->prev; | |
1114 if (row->parent) | |
1115 row->parent->child = row; | |
1116 else | |
1117 tree->root = row; | |
1118 row->next = s; | |
1119 s->prev = row; /* s cannot be NULL */ | |
1120 row->prev = NULL; | |
1121 newp = g_list_index(tree->list, s) - 1; | |
1122 } else { | |
1123 if (row->prev) { | |
1124 row->prev->next = row->next; | |
1125 } else { | |
1126 /* row was the first child of its parent */ | |
1127 if (row->parent) | |
1128 row->parent->child = row->next; | |
1129 else | |
1130 tree->top = row->next; | |
1131 } | |
1132 | |
1133 if (row->next) | |
1134 row->next->prev = row->prev; | |
1135 | |
1136 q->next = row; | |
1137 row->prev = q; | |
1138 if (s) | |
1139 s->prev = row; | |
1140 row->next = s; | |
1141 newp = g_list_index(tree->list, q) + 1; | |
1142 } | |
1143 tree->list = g_list_reposition_child(tree->list, current, newp); | |
1144 | |
1145 redraw_tree(tree); | |
1146 } | |
1147 | |
1148 GntTreeRow *gnt_tree_add_row_after(GntTree *tree, void *key, GntTreeRow *row, void *parent, void *bigbro) | |
1149 { | |
1150 GntTreeRow *pr = NULL; | |
1151 | |
1152 g_hash_table_replace(tree->hash, key, row); | |
1153 row->tree = tree; | |
1154 | |
1155 if (bigbro == NULL && tree->compare) | |
1156 { | |
1157 bigbro = find_position(tree, key, parent); | |
1158 } | |
1159 | |
1160 if (tree->root == NULL) | |
1161 { | |
1162 tree->root = row; | |
1163 tree->list = g_list_prepend(tree->list, key); | |
1164 } | |
1165 else | |
1166 { | |
1167 int position = 0; | |
1168 | |
1169 if (bigbro) | |
1170 { | |
1171 pr = g_hash_table_lookup(tree->hash, bigbro); | |
1172 if (pr) | |
1173 { | |
1174 if (pr->next) pr->next->prev = row; | |
1175 row->next = pr->next; | |
1176 row->prev = pr; | |
1177 pr->next = row; | |
1178 row->parent = pr->parent; | |
1179 | |
1180 position = g_list_index(tree->list, bigbro); | |
1181 } | |
1182 } | |
1183 | |
1184 if (pr == NULL && parent) | |
1185 { | |
1186 pr = g_hash_table_lookup(tree->hash, parent); | |
1187 if (pr) | |
1188 { | |
1189 if (pr->child) pr->child->prev = row; | |
1190 row->next = pr->child; | |
1191 pr->child = row; | |
1192 row->parent = pr; | |
1193 | |
1194 position = g_list_index(tree->list, parent); | |
1195 } | |
1196 } | |
1197 | |
1198 if (pr == NULL) | |
1199 { | |
1200 GntTreeRow *r = tree->root; | |
1201 row->next = r; | |
1202 if (r) r->prev = row; | |
1203 if (tree->current == tree->root) | |
1204 tree->current = row; | |
1205 tree->root = row; | |
1206 tree->list = g_list_prepend(tree->list, key); | |
1207 } | |
1208 else | |
1209 { | |
1210 tree->list = g_list_insert(tree->list, key, position + 1); | |
1211 } | |
1212 } | |
1213 | |
1214 row->key = key; | |
1215 row->data = NULL; | |
1216 | |
1217 redraw_tree(tree); | |
1218 | |
1219 return row; | |
1220 } | |
1221 | |
1222 GntTreeRow *gnt_tree_add_row_last(GntTree *tree, void *key, GntTreeRow *row, void *parent) | |
1223 { | |
1224 GntTreeRow *pr = NULL, *br = NULL; | |
1225 | |
1226 if (parent) | |
1227 pr = g_hash_table_lookup(tree->hash, parent); | |
1228 | |
1229 if (pr) | |
1230 br = pr->child; | |
1231 else | |
1232 br = tree->root; | |
1233 | |
1234 if (br) | |
1235 { | |
1236 while (br->next) | |
1237 br = br->next; | |
1238 } | |
1239 | |
1240 return gnt_tree_add_row_after(tree, key, row, parent, br ? br->key : NULL); | |
1241 } | |
1242 | |
1243 gpointer gnt_tree_get_selection_data(GntTree *tree) | |
1244 { | |
1245 if (tree->current) | |
1246 return tree->current->key; /* XXX: perhaps we should just get rid of 'data' */ | |
1247 return NULL; | |
1248 } | |
1249 | |
1250 char *gnt_tree_get_selection_text(GntTree *tree) | |
1251 { | |
1252 if (tree->current) | |
1253 return update_row_text(tree, tree->current); | |
1254 return NULL; | |
1255 } | |
1256 | |
1257 GList *gnt_tree_get_selection_text_list(GntTree *tree) | |
1258 { | |
1259 GList *list = NULL, *iter; | |
1260 int i; | |
1261 | |
1262 if (!tree->current) | |
1263 return NULL; | |
1264 | |
1265 for (i = 0, iter = tree->current->columns; i < tree->ncol && iter; | |
1266 i++, iter = iter->next) | |
1267 { | |
1268 GntTreeCol *col = iter->data; | |
1269 list = g_list_append(list, g_strdup(col->text)); | |
1270 } | |
1271 | |
1272 return list; | |
1273 } | |
1274 | |
1275 void gnt_tree_remove(GntTree *tree, gpointer key) | |
1276 { | |
1277 GntTreeRow *row = g_hash_table_lookup(tree->hash, key); | |
1278 static int depth = 0; /* Only redraw after all child nodes are removed */ | |
1279 if (row) | |
1280 { | |
1281 gboolean redraw = FALSE; | |
1282 | |
1283 if (row->child) { | |
1284 depth++; | |
1285 while (row->child) { | |
1286 gnt_tree_remove(tree, row->child->key); | |
1287 } | |
1288 depth--; | |
1289 } | |
1290 | |
1291 if (get_distance(tree->top, row) >= 0 && get_distance(row, tree->bottom) >= 0) | |
1292 redraw = TRUE; | |
1293 | |
1294 /* Update root/top/current/bottom if necessary */ | |
1295 if (tree->root == row) | |
1296 tree->root = get_next(row); | |
1297 if (tree->top == row) | |
1298 { | |
1299 if (tree->top != tree->root) | |
1300 tree->top = get_prev(row); | |
1301 else | |
1302 tree->top = get_next(row); | |
1303 } | |
1304 if (tree->current == row) | |
1305 { | |
1306 if (tree->current != tree->root) | |
1307 tree->current = get_prev(row); | |
1308 else | |
1309 tree->current = get_next(row); | |
1310 tree_selection_changed(tree, row, tree->current); | |
1311 } | |
1312 if (tree->bottom == row) | |
1313 { | |
1314 tree->bottom = get_prev(row); | |
1315 } | |
1316 | |
1317 /* Fix the links */ | |
1318 if (row->next) | |
1319 row->next->prev = row->prev; | |
1320 if (row->parent && row->parent->child == row) | |
1321 row->parent->child = row->next; | |
1322 if (row->prev) | |
1323 row->prev->next = row->next; | |
1324 | |
1325 g_hash_table_remove(tree->hash, key); | |
1326 tree->list = g_list_remove(tree->list, key); | |
1327 | |
1328 if (redraw && depth == 0) | |
1329 { | |
1330 redraw_tree(tree); | |
1331 } | |
1332 } | |
1333 } | |
1334 | |
1335 static gboolean | |
1336 return_true(gpointer key, gpointer data, gpointer null) | |
1337 { | |
1338 return TRUE; | |
1339 } | |
1340 | |
1341 void gnt_tree_remove_all(GntTree *tree) | |
1342 { | |
1343 tree->root = NULL; | |
1344 g_hash_table_foreach_remove(tree->hash, (GHRFunc)return_true, tree); | |
1345 g_list_free(tree->list); | |
1346 tree->list = NULL; | |
1347 tree->current = tree->top = tree->bottom = NULL; | |
1348 } | |
1349 | |
1350 int gnt_tree_get_selection_visible_line(GntTree *tree) | |
1351 { | |
1352 return get_distance(tree->top, tree->current) + | |
1353 !!(GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(tree), GNT_WIDGET_NO_BORDER)); | |
1354 } | |
1355 | |
1356 void gnt_tree_change_text(GntTree *tree, gpointer key, int colno, const char *text) | |
1357 { | |
1358 GntTreeRow *row; | |
1359 GntTreeCol *col; | |
1360 | |
1361 g_return_if_fail(colno < tree->ncol); | |
1362 | |
1363 row = g_hash_table_lookup(tree->hash, key); | |
1364 if (row) | |
1365 { | |
1366 col = g_list_nth_data(row->columns, colno); | |
1367 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
|
1368 col->text = g_strdup(text ? text : ""); |
15817 | 1369 |
1370 if (get_distance(tree->top, row) >= 0 && get_distance(row, tree->bottom) >= 0) | |
1371 redraw_tree(tree); | |
1372 } | |
1373 } | |
1374 | |
1375 GntTreeRow *gnt_tree_add_choice(GntTree *tree, void *key, GntTreeRow *row, void *parent, void *bigbro) | |
1376 { | |
1377 GntTreeRow *r; | |
1378 r = g_hash_table_lookup(tree->hash, key); | |
1379 g_return_val_if_fail(!r || !r->choice, NULL); | |
1380 | |
1381 if (bigbro == NULL) { | |
1382 if (tree->compare) | |
1383 bigbro = find_position(tree, key, parent); | |
1384 else { | |
1385 r = g_hash_table_lookup(tree->hash, parent); | |
1386 if (!r) | |
1387 r = tree->root; | |
1388 else | |
1389 r = r->child; | |
1390 if (r) { | |
1391 while (r->next) | |
1392 r = r->next; | |
1393 bigbro = r->key; | |
1394 } | |
1395 } | |
1396 } | |
1397 row = gnt_tree_add_row_after(tree, key, row, parent, bigbro); | |
1398 row->choice = TRUE; | |
1399 | |
1400 return row; | |
1401 } | |
1402 | |
1403 void gnt_tree_set_choice(GntTree *tree, void *key, gboolean set) | |
1404 { | |
1405 GntTreeRow *row = g_hash_table_lookup(tree->hash, key); | |
1406 | |
1407 if (!row) | |
1408 return; | |
1409 g_return_if_fail(row->choice); | |
1410 | |
1411 row->isselected = set; | |
1412 redraw_tree(tree); | |
1413 } | |
1414 | |
1415 gboolean gnt_tree_get_choice(GntTree *tree, void *key) | |
1416 { | |
1417 GntTreeRow *row = g_hash_table_lookup(tree->hash, key); | |
1418 | |
1419 if (!row) | |
1420 return FALSE; | |
1421 g_return_val_if_fail(row->choice, FALSE); | |
1422 | |
1423 return row->isselected; | |
1424 } | |
1425 | |
1426 void gnt_tree_set_row_flags(GntTree *tree, void *key, GntTextFormatFlags flags) | |
1427 { | |
1428 GntTreeRow *row = g_hash_table_lookup(tree->hash, key); | |
1429 if (!row || row->flags == flags) | |
1430 return; | |
1431 | |
1432 row->flags = flags; | |
1433 redraw_tree(tree); /* XXX: It shouldn't be necessary to redraw the whole darned tree */ | |
1434 } | |
1435 | |
1436 void gnt_tree_set_selected(GntTree *tree , void *key) | |
1437 { | |
1438 int dist; | |
1439 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
|
1440 if (!row || row == tree->current) |
15817 | 1441 return; |
1442 | |
1443 if (tree->top == NULL) | |
1444 tree->top = row; | |
1445 if (tree->bottom == NULL) | |
1446 tree->bottom = row; | |
1447 | |
1448 tree->current = row; | |
1449 if ((dist = get_distance(tree->current, tree->bottom)) < 0) | |
1450 gnt_tree_scroll(tree, -dist); | |
1451 else if ((dist = get_distance(tree->current, tree->top)) > 0) | |
1452 gnt_tree_scroll(tree, -dist); | |
1453 else | |
1454 redraw_tree(tree); | |
16203
4f6a6443a1e3
emit/handle gnttree signals appropriately
Richard Nelson <wabz@pidgin.im>
parents:
16123
diff
changeset
|
1455 tree_selection_changed(tree, row, tree->current); |
15817 | 1456 } |
1457 | |
1458 void _gnt_tree_init_internals(GntTree *tree, int col) | |
1459 { | |
1460 tree->ncol = col; | |
1461 tree->hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, free_tree_row); | |
1462 tree->columns = g_new0(struct _GntTreeColInfo, col); | |
1463 while (col--) | |
1464 { | |
1465 tree->columns[col].width = 15; | |
1466 } | |
1467 tree->list = NULL; | |
1468 tree->show_title = FALSE; | |
1469 } | |
1470 | |
1471 GntWidget *gnt_tree_new_with_columns(int col) | |
1472 { | |
1473 GntWidget *widget = g_object_new(GNT_TYPE_TREE, NULL); | |
1474 GntTree *tree = GNT_TREE(widget); | |
1475 | |
1476 _gnt_tree_init_internals(tree, col); | |
1477 | |
1478 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_NO_SHADOW); | |
1479 gnt_widget_set_take_focus(widget, TRUE); | |
1480 | |
1481 return widget; | |
1482 } | |
1483 | |
1484 GntTreeRow *gnt_tree_create_row_from_list(GntTree *tree, GList *list) | |
1485 { | |
1486 GList *iter; | |
1487 int i; | |
1488 GntTreeRow *row = g_new0(GntTreeRow, 1); | |
1489 | |
1490 for (i = 0, iter = list; i < tree->ncol && iter; iter = iter->next, i++) | |
1491 { | |
1492 GntTreeCol *col = g_new0(GntTreeCol, 1); | |
1493 col->span = 1; | |
1494 col->text = g_strdup(iter->data ? iter->data : ""); | |
1495 | |
1496 row->columns = g_list_append(row->columns, col); | |
1497 } | |
1498 | |
1499 return row; | |
1500 } | |
1501 | |
1502 GntTreeRow *gnt_tree_create_row(GntTree *tree, ...) | |
1503 { | |
1504 int i; | |
1505 va_list args; | |
1506 GList *list = NULL; | |
1507 GntTreeRow *row; | |
1508 | |
1509 va_start(args, tree); | |
1510 for (i = 0; i < tree->ncol; i++) | |
1511 { | |
1512 list = g_list_append(list, va_arg(args, char *)); | |
1513 } | |
1514 va_end(args); | |
1515 | |
1516 row = gnt_tree_create_row_from_list(tree, list); | |
1517 g_list_free(list); | |
1518 | |
1519 return row; | |
1520 } | |
1521 | |
1522 void gnt_tree_set_col_width(GntTree *tree, int col, int width) | |
1523 { | |
1524 g_return_if_fail(col < tree->ncol); | |
1525 | |
1526 tree->columns[col].width = width; | |
1527 } | |
1528 | |
18348
1f9db757efc2
Oops. I missed this. This allows setting tree-column titles one by one.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18183
diff
changeset
|
1529 void gnt_tree_set_column_title(GntTree *tree, int index, const char *title) |
1f9db757efc2
Oops. I missed this. This allows setting tree-column titles one by one.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18183
diff
changeset
|
1530 { |
1f9db757efc2
Oops. I missed this. This allows setting tree-column titles one by one.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18183
diff
changeset
|
1531 g_free(tree->columns[index].title); |
1f9db757efc2
Oops. I missed this. This allows setting tree-column titles one by one.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18183
diff
changeset
|
1532 tree->columns[index].title = g_strdup(title); |
1f9db757efc2
Oops. I missed this. This allows setting tree-column titles one by one.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18183
diff
changeset
|
1533 } |
1f9db757efc2
Oops. I missed this. This allows setting tree-column titles one by one.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18183
diff
changeset
|
1534 |
15817 | 1535 void gnt_tree_set_column_titles(GntTree *tree, ...) |
1536 { | |
1537 int i; | |
1538 va_list args; | |
1539 | |
1540 va_start(args, tree); | |
1541 for (i = 0; i < tree->ncol; i++) | |
1542 { | |
1543 const char *title = va_arg(args, const char *); | |
1544 tree->columns[i].title = g_strdup(title); | |
1545 } | |
1546 va_end(args); | |
1547 } | |
1548 | |
1549 void gnt_tree_set_show_title(GntTree *tree, gboolean set) | |
1550 { | |
1551 tree->show_title = set; | |
1552 GNT_WIDGET(tree)->priv.minh = (set ? 6 : 4); | |
1553 } | |
1554 | |
1555 void gnt_tree_set_compare_func(GntTree *tree, GCompareFunc func) | |
1556 { | |
1557 tree->compare = func; | |
1558 } | |
1559 | |
1560 void gnt_tree_set_expanded(GntTree *tree, void *key, gboolean expanded) | |
1561 { | |
1562 GntTreeRow *row = g_hash_table_lookup(tree->hash, key); | |
1563 if (row) { | |
1564 row->collapsed = !expanded; | |
1565 if (GNT_WIDGET(tree)->window) | |
1566 gnt_widget_draw(GNT_WIDGET(tree)); | |
16203
4f6a6443a1e3
emit/handle gnttree signals appropriately
Richard Nelson <wabz@pidgin.im>
parents:
16123
diff
changeset
|
1567 g_signal_emit(tree, signals[SIG_COLLAPSED], 0, key, row->collapsed); |
15817 | 1568 } |
1569 } | |
1570 | |
1571 void gnt_tree_set_show_separator(GntTree *tree, gboolean set) | |
1572 { | |
1573 tree->show_separator = set; | |
1574 } | |
1575 | |
1576 void gnt_tree_adjust_columns(GntTree *tree) | |
1577 { | |
1578 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
|
1579 int *widths, i, twidth; |
15817 | 1580 |
1581 widths = g_new0(int, tree->ncol); | |
1582 while (row) { | |
1583 GList *iter; | |
1584 for (i = 0, iter = row->columns; iter; iter = iter->next, i++) { | |
1585 GntTreeCol *col = iter->data; | |
1586 int w = gnt_util_onscreen_width(col->text, NULL); | |
15968
0ab73bf1fef1
check-items are wider.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15928
diff
changeset
|
1587 if (i == 0 && row->choice) |
0ab73bf1fef1
check-items are wider.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15928
diff
changeset
|
1588 w += 4; |
15972
5eb0621e0760
Backspace to jump to parent.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15971
diff
changeset
|
1589 if (i == 0) { |
5eb0621e0760
Backspace to jump to parent.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15971
diff
changeset
|
1590 w += find_depth(row) * TAB_SIZE; |
5eb0621e0760
Backspace to jump to parent.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15971
diff
changeset
|
1591 } |
15817 | 1592 if (widths[i] < w) |
1593 widths[i] = w; | |
1594 } | |
15972
5eb0621e0760
Backspace to jump to parent.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15971
diff
changeset
|
1595 row = get_next(row); |
15817 | 1596 } |
1597 | |
1598 twidth = 1 + 2 * (!GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(tree), GNT_WIDGET_NO_BORDER)); | |
1599 for (i = 0; i < tree->ncol; i++) { | |
1600 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
|
1601 if (!tree->columns[i].invisible) |
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
1602 twidth += widths[i] + (tree->show_separator ? 1 : 0) + 1; |
15817 | 1603 } |
1604 g_free(widths); | |
1605 | |
16123
bc280c341679
We don't need to do anything about the heights here.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16105
diff
changeset
|
1606 gnt_widget_set_size(GNT_WIDGET(tree), twidth, -1); |
15817 | 1607 } |
1608 | |
1609 void gnt_tree_set_hash_fns(GntTree *tree, gpointer hash, gpointer eq, gpointer kd) | |
1610 { | |
1611 g_hash_table_foreach_remove(tree->hash, return_true, NULL); | |
1612 g_hash_table_destroy(tree->hash); | |
1613 tree->hash = g_hash_table_new_full(hash, eq, kd, free_tree_row); | |
1614 } | |
1615 | |
15970
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
1616 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
|
1617 { |
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
1618 g_return_if_fail(col < tree->ncol); |
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
1619 tree->columns[col].invisible = !vis; |
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
1620 } |
790d1d003825
Allow making some columns invisible.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15968
diff
changeset
|
1621 |