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