summaryrefslogtreecommitdiff
path: root/finch/libgnt/gnttree.h
blob: 894d3fb6329b7fa95c8c67e43e24c638b6b61286 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/**
 * @file gnttree.h Tree API
 * @ingroup gnt
 */
/*
 * GNT - The GLib Ncurses Toolkit
 *
 * GNT is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 */

#ifndef GNT_TREE_H
#define GNT_TREE_H

#include "gntwidget.h"
#include "gnt.h"
#include "gntcolors.h"
#include "gntkeys.h"
#include "gnttextview.h"

#define GNT_TYPE_TREE				(gnt_tree_get_gtype())
#define GNT_TREE(obj)				(G_TYPE_CHECK_INSTANCE_CAST((obj), GNT_TYPE_TREE, GntTree))
#define GNT_TREE_CLASS(klass)		(G_TYPE_CHECK_CLASS_CAST((klass), GNT_TYPE_TREE, GntTreeClass))
#define GNT_IS_TREE(obj)			(G_TYPE_CHECK_INSTANCE_TYPE((obj), GNT_TYPE_TREE))
#define GNT_IS_TREE_CLASS(klass)	(G_TYPE_CHECK_CLASS_TYPE((klass), GNT_TYPE_TREE))
#define GNT_TREE_GET_CLASS(obj)	(G_TYPE_INSTANCE_GET_CLASS((obj), GNT_TYPE_TREE, GntTreeClass))

typedef struct _GntTree			GntTree;
typedef struct _GntTreePriv		GntTreePriv;
typedef struct _GntTreeClass		GntTreeClass;

typedef struct _GntTreeRow		GntTreeRow;
typedef struct _GntTreeCol		GntTreeCol;

typedef enum _GntTreeColumnFlag {
	GNT_TREE_COLUMN_INVISIBLE    = 1 << 0,
	GNT_TREE_COLUMN_FIXED_SIZE   = 1 << 1,
	GNT_TREE_COLUMN_BINARY_DATA  = 1 << 2,
	GNT_TREE_COLUMN_RIGHT_ALIGNED = 1 << 3,
} GntTreeColumnFlag;

struct _GntTree
{
	GntWidget parent;

	GntTreeRow *current;    /* current selection */

	GntTreeRow *top;        /* The topmost visible item */
	GntTreeRow *bottom;     /* The bottommost visible item */
	
	GntTreeRow *root;       /* The root of all evil */
	
	GList *list;            /* List of GntTreeRow s */
	GHashTable *hash;       /* We need this for quickly referencing the rows */
	guint (*hash_func)(gconstpointer);
	gboolean (*hash_eq_func)(gconstpointer, gconstpointer);
	GDestroyNotify key_destroy;
	GDestroyNotify value_destroy;

	int ncol;               /* No. of columns */
	struct _GntTreeColInfo
	{
		int width;
		char *title;
		int width_ratio;
		GntTreeColumnFlag flags;
	} *columns;             /* Would a GList be better? */
	gboolean show_title;
	gboolean show_separator; /* Whether to show column separators */

	GntTreePriv *priv;
};

struct _GntTreeClass
{
	GntWidgetClass parent;

	void (*selection_changed)(GntTreeRow *old, GntTreeRow * current);
	void (*toggled)(GntTree *tree, gpointer key);

	void (*gnt_reserved1)(void);
	void (*gnt_reserved2)(void);
	void (*gnt_reserved3)(void);
	void (*gnt_reserved4)(void);
};

G_BEGIN_DECLS

/**
 * @return The GType for GntTree
 */
GType gnt_tree_get_gtype(void);

/**
 * Create a tree with one column.
 *
 * @return The newly created tree
 *
 * @see gnt_tree_new_with_columns
 */
GntWidget * gnt_tree_new(void);

/**
 * Create a tree with a specified number of columns.
 *
 * @param columns  Number of columns
 *
 * @return  The newly created tree
 *
 * @see gnt_tree_new
 */
GntWidget * gnt_tree_new_with_columns(int columns);

/**
 * The number of rows the tree should display at a time.
 *
 * @param tree  The tree
 * @param rows  The number of rows
 */
void gnt_tree_set_visible_rows(GntTree *tree, int rows);

/**
 * Get the number visible rows.
 *
 * @param tree  The tree
 *
 * @return  The number of visible rows
 */
int gnt_tree_get_visible_rows(GntTree *tree);

/**
 * Scroll the contents of the tree.
 *
 * @param tree   The tree
 * @param count  If positive, the tree will be scrolled down by count rows,
 *               otherwise, it will be scrolled up by count rows.
 */
void gnt_tree_scroll(GntTree *tree, int count);

/**
 * Insert a row in the tree.
 *
 * @param tree    The tree
 * @param key     The key for the row
 * @param row     The row to insert
 * @param parent  The key for the parent row
 * @param bigbro  The key for the row to insert the new row after.
 *
 * @return  The inserted row
 *
 * @see gnt_tree_create_row
 * @see gnt_tree_add_row_last
 * @see gnt_tree_add_choice
 */
GntTreeRow * gnt_tree_add_row_after(GntTree *tree, void *key, GntTreeRow *row, void *parent, void *bigbro);

/**
 * Insert a row at the end of the tree.
 *
 * @param tree    The tree
 * @param key     The key for the row
 * @param row     The row to insert
 * @param parent  The key for the parent row
 *
 * @return The inserted row
 *
 * @see gnt_tree_create_row
 * @see gnt_tree_add_row_after
 * @see gnt_tree_add_choice
 */
GntTreeRow * gnt_tree_add_row_last(GntTree *tree, void *key, GntTreeRow *row, void *parent);

/**
 * Get the key for the selected row.
 *
 * @param tree  The tree
 *
 * @return   The key for the selected row
 */
gpointer gnt_tree_get_selection_data(GntTree *tree);

/**
 * Get the text displayed for the selected row.
 *
 * @param tree  The tree
 *
 * @return  The text, which needs to be freed by the caller
 * @see gnt_tree_get_row_text_list
 * @see gnt_tree_get_selection_text_list
 */
char * gnt_tree_get_selection_text(GntTree *tree);

/**
 * Get a list of text for a row.
 *
 * @param tree  The tree
 * @param key   A key corresponding to the row in question. If key
 *              is @c NULL, the text list for the selected row will
 *              be returned.
 *
 * @return A list of texts of a row. The list and its data should be
 *         freed by the caller. The caller should make sure that if
 *         any column of the tree contains binary data, it's not freed.
 * @see gnt_tree_get_selection_text_list 
 * @see gnt_tree_get_selection_text
 */
GList * gnt_tree_get_row_text_list(GntTree *tree, gpointer key);

/**
 * Get the key of a row.
 *
 * @param tree   The tree
 * @param row    The GntTreeRow object
 *
 * @return The key of the row.
 * @since 2.8.0 (gnt), 2.7.2 (pidgin)
 */
gpointer gnt_tree_row_get_key(GntTree *tree, GntTreeRow *row);

/**
 * Get the next row.
 *
 * @param tree The tree
 * @param row  The GntTreeRow object
 *
 * @return The next row.
 * @since 2.8.0 (gnt), 2.7.2 (pidgin)
 */
GntTreeRow * gnt_tree_row_get_next(GntTree *tree, GntTreeRow *row);

/**
 * Get the previous row.
 *
 * @param tree The tree
 * @param row  The GntTreeRow object
 *
 * @return The previous row.
 * @since 2.8.0 (gnt), 2.7.2 (pidgin)
 */
GntTreeRow * gnt_tree_row_get_prev(GntTree *tree, GntTreeRow *row);

/**
 * Get the child row.
 *
 * @param tree The tree
 * @param row  The GntTreeRow object
 *
 * @return The child row.
 * @since 2.8.0 (gnt), 2.7.2 (pidgin)
 */
GntTreeRow * gnt_tree_row_get_child(GntTree *tree, GntTreeRow *row);

/**
 * Get the parent row.
 *
 * @param tree The tree
 * @param row  The GntTreeRow object
 *
 * @return The parent row.
 * @since 2.8.0 (gnt), 2.7.2 (pidgin)
 */
GntTreeRow * gnt_tree_row_get_parent(GntTree *tree, GntTreeRow *row);

/**
 * Get a list of text of the current row.
 *
 * @param tree  The tree
 *
 * @return A list of texts of the currently selected row. The list
 *         and its data should be freed by the caller. The caller
 *         should make sure that if any column of the tree contains
 *         binary data, it's not freed.
 * @see gnt_tree_get_row_text_list
 * @see gnt_tree_get_selection_text
 */
GList * gnt_tree_get_selection_text_list(GntTree *tree);

/**
 * Returns the list of rows in the tree.
 *
 * @param tree  The tree
 *
 * @return The list of the rows. The list should not be modified by the caller.
 */
GList *gnt_tree_get_rows(GntTree *tree);

/**
 * Remove a row from the tree.
 *
 * @param tree  The tree
 * @param key   The key for the row to remove
 */
void gnt_tree_remove(GntTree *tree, gpointer key);

/**
 * Remove all the item from the tree.
 *
 * @param tree  The tree
 */
void gnt_tree_remove_all(GntTree *tree);

/**
 * Get the visible line number of the selected row.
 *
 * @param tree  The tree
 *
 * @return  The line number of the currently selected row
 */
int gnt_tree_get_selection_visible_line(GntTree *tree);

/**
 * Change the text of a column in a row.
 *
 * @param tree   The tree
 * @param key    The key for the row
 * @param colno  The index of the column
 * @param text   The new text
 */
void gnt_tree_change_text(GntTree *tree, gpointer key, int colno, const char *text);

/**
 * Add a checkable item in the tree.
 *
 * @param tree    The tree
 * @param key     The key for the row
 * @param row     The row to add
 * @param parent  The parent of the row, or @c NULL
 * @param bigbro  The row to insert after, or @c NULL
 *
 * @return  The row inserted.
 *
 * @see gnt_tree_create_row
 * @see gnt_tree_create_row_from_list
 * @see gnt_tree_add_row_last
 * @see gnt_tree_add_row_after
 */
GntTreeRow * gnt_tree_add_choice(GntTree *tree, void *key, GntTreeRow *row, void *parent, void *bigbro);

/**
 * Set whether a checkable item is checked or not.
 *
 * @param tree   The tree
 * @param key    The key for the row
 * @param set    @c TRUE if the item should be checked, @c FALSE if not
 */
void gnt_tree_set_choice(GntTree *tree, void *key, gboolean set);

/**
 * Return whether a row is selected or not, where the row is a checkable item.
 *
 * @param tree  The tree
 * @param key   The key for the row
 *
 * @return    @c TRUE if the row is checked, @c FALSE otherwise.
 */
gboolean gnt_tree_get_choice(GntTree *tree, void *key);

/**
 * Set flags for the text in a row in the tree.
 *
 * @param tree   The tree
 * @param key    The key for the row
 * @param flags  The flags to set
 */
void gnt_tree_set_row_flags(GntTree *tree, void *key, GntTextFormatFlags flags);

/**
 * Set color for the text in a row in the tree.
 *
 * @param tree   The tree
 * @param key    The key for the row
 * @param color  The color
 * @since 2.4.0
 */
void gnt_tree_set_row_color(GntTree *tree, void *key, int color);

/**
 * Select a row.
 *
 * @param tree  The tree
 * @param key   The key of the row to select
 */
void gnt_tree_set_selected(GntTree *tree , void *key);

/**
 * Create a row to insert in the tree.
 *
 * @param tree The tree
 * @param ...  A string for each column in the tree
 *
 * @return   The row
 *
 * @see gnt_tree_create_row_from_list
 * @see gnt_tree_add_row_after
 * @see gnt_tree_add_row_last
 * @see gnt_tree_add_choice
 */
GntTreeRow * gnt_tree_create_row(GntTree *tree, ...);

/**
 * Create a row from a list of text.
 *
 * @param tree  The tree
 * @param list  The list containing the text for each column
 *
 * @return   The row
 *
 * @see gnt_tree_create_row
 * @see gnt_tree_add_row_after
 * @see gnt_tree_add_row_last
 * @see gnt_tree_add_choice
 */
GntTreeRow * gnt_tree_create_row_from_list(GntTree *tree, GList *list);

/**
 * Set the width of a column in the tree.
 *
 * @param tree   The tree
 * @param col    The index of the column
 * @param width  The width for the column
 *
 * @see gnt_tree_set_column_width_ratio
 * @see gnt_tree_set_column_resizable
 */
void gnt_tree_set_col_width(GntTree *tree, int col, int width);

/**
 * Set the title for a column.
 *
 * @param tree   The tree
 * @param index  The index of the column
 * @param title  The title for the column
 *
 * @see gnt_tree_set_column_titles
 * @see gnt_tree_set_show_title
 *
 * @since 2.0.0 (gnt), 2.1.0 (pidgin)
 */
void gnt_tree_set_column_title(GntTree *tree, int index, const char *title);

/**
 * Set the titles of the columns
 *
 * @param tree  The tree
 * @param ...   One title for each column in the tree
 *
 * @see gnt_tree_set_column_title
 * @see gnt_tree_set_show_title
 */
void gnt_tree_set_column_titles(GntTree *tree, ...);

/**
 * Set whether to display the title of the columns.
 *
 * @param tree  The tree
 * @param set   If @c TRUE, the column titles are displayed
 *
 * @see gnt_tree_set_column_title
 * @see gnt_tree_set_column_titles
 */
void gnt_tree_set_show_title(GntTree *tree, gboolean set);

/**
 * Set the compare function for sorting the data.
 *
 * @param tree  The tree
 * @param func  The comparison function, which is used to compare
 *              the keys
 *
 * @see gnt_tree_sort_row 
 */
void gnt_tree_set_compare_func(GntTree *tree, GCompareFunc func);

/**
 * Set whether a row, which has child rows, should be expanded.
 *
 * @param tree      The tree
 * @param key       The key of the row
 * @param expanded  Whether to expand the child rows
 */
void gnt_tree_set_expanded(GntTree *tree, void *key, gboolean expanded);

/**
 * Set whether to show column separators.
 *
 * @param tree  The tree
 * @param set   If @c TRUE, the column separators are displayed
 */
void gnt_tree_set_show_separator(GntTree *tree, gboolean set);

/**
 * Sort a row in the tree.
 *
 * @param tree  The tree
 * @param row   The row to sort
 *
 * @see gnt_tree_set_compare_func
 */
void gnt_tree_sort_row(GntTree *tree, void *row);

/**
 * Automatically adjust the width of the columns in the tree.
 *
 * @param tree  The tree
 */
void gnt_tree_adjust_columns(GntTree *tree);

/**
 * Set the hash functions to use to hash, compare and free the keys.
 *
 * @param tree  The tree
 * @param hash  The hashing function
 * @param eq    The function to compare keys
 * @param kd    The function to use to free the keys when a row is removed
 *              from the tree
 */
void gnt_tree_set_hash_fns(GntTree *tree, gpointer hash, gpointer eq, gpointer kd);

/**
 * Set whether a column is visible or not.
 * This can be useful when, for example, we want to store some data
 * which we don't want/need to display.
 * 
 * @param tree  The tree
 * @param col   The index of the column
 * @param vis   If @c FALSE, the column will not be displayed
 */
void gnt_tree_set_column_visible(GntTree *tree, int col, gboolean vis);

/**
 * Set whether a column can be resized to keep the same ratio when the
 * tree is resized.
 * 
 * @param tree  The tree
 * @param col   The index of the column
 * @param res   If @c FALSE, the column will not be resized when the
 *              tree is resized
 *
 * @see gnt_tree_set_col_width
 * @see gnt_tree_set_column_width_ratio
 *
 * @since 2.0.0 (gnt), 2.1.0 (pidgin)
 */
void gnt_tree_set_column_resizable(GntTree *tree, int col, gboolean res);

/**
 * Set whether data in a column should be considered as binary data, and
 * not as strings. A column containing binary data will be display empty text.
 *
 * @param tree  The tree
 * @param col   The index of the column
 * @param bin   @c TRUE if the data for the column is binary
 */
void gnt_tree_set_column_is_binary(GntTree *tree, int col, gboolean bin);

/**
 * Set whether text in a column should be right-aligned.
 *
 * @param tree  The tree
 * @param col   The index of the column
 * @param right @c TRUE if the text in the column should be right aligned
 *
 * @since 2.0.0 (gnt), 2.1.0 (pidgin)
 */
void gnt_tree_set_column_is_right_aligned(GntTree *tree, int col, gboolean right);

/**
 * Set column widths to use when calculating column widths after a tree
 * is resized.
 *
 * @param tree   The tree
 * @param cols   Array of widths. The width must have the same number
 *               of entries as the number of columns in the tree, or
 *               end with a negative value for a column-width.
 *
 * @see gnt_tree_set_col_width
 * @see gnt_tree_set_column_resizable
 *
 * @since 2.0.0 (gnt), 2.1.0 (pidgin)
 */
void gnt_tree_set_column_width_ratio(GntTree *tree, int cols[]);

/**
 * Set the column to use for typeahead searching.
 *
 * @param tree   The tree
 * @param col    The index of the column
 *
 * @since 2.0.0 (gnt), 2.1.0 (pidgin)
 */
void gnt_tree_set_search_column(GntTree *tree, int col);

/**
 * Check whether the user is currently in the middle of a search.
 *
 * @param tree   The tree
 * @return  @c TRUE if the user is searching, @c FALSE otherwise.
 *
 * @since 2.0.0 (gnt), 2.1.0 (pidgin)
 */
gboolean gnt_tree_is_searching(GntTree *tree);

/**
 * Set a custom search function.
 *
 * @param tree  The tree
 * @param func  The custom search function. The search function is
 *              sent the tree itself, the key of a row, the search
 *              string and the content of row in the search column.
 *              If the function returns @c TRUE, the row is dislayed,
 *              otherwise it's not.
 *
 * @since 2.0.0 (gnt), 2.1.0 (pidgin)
 */
void gnt_tree_set_search_function(GntTree *tree,
		gboolean (*func)(GntTree *tree, gpointer key, const char *search, const char *current));

/**
 * Get the parent key for a row.
 *
 * @param  tree  The tree
 * @param  key   The key for the row.
 *
 * @return The key of the parent row.
 * @since 2.4.0
 */
gpointer gnt_tree_get_parent_key(GntTree *tree, gpointer key);

G_END_DECLS

#endif /* GNT_TREE_H */