NetSurf
treeview.h
Go to the documentation of this file.
1/*
2 * Copyright 2012 - 2013 Michael Drake <tlsa@netsurf-browser.org>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/**
20 * \file
21 * Treeview handling interface.
22 */
23
24#ifndef _NETSURF_DESKTOP_TREEVIEW_H_
25#define _NETSURF_DESKTOP_TREEVIEW_H_
26
27#include <stdbool.h>
28#include <stdint.h>
29#include <libwapcaplet/libwapcaplet.h>
30
31#include "netsurf/mouse.h"
32
33struct redraw_context;
34struct core_window;
35
36typedef struct treeview treeview;
38
39
40/**
41 * treeview node type
42 */
44 TREE_NODE_NONE = 0, /**< No node */
45 TREE_NODE_ROOT = (1 << 0), /**< Node is treeview's root */
46 TREE_NODE_FOLDER = (1 << 1), /**< Node is folder */
47 TREE_NODE_ENTRY = (1 << 2) /**< Node is an entry */
48};
49
50
51/**
52 * Relationship between nodes
53 */
57};
58
59
60/**
61 * Node change handling options
62 */
63typedef enum {
64 TREE_OPTION_NONE = (0), /* No flags set */
65 TREE_OPTION_SPECIAL_DIR = (1 << 0), /* Special folder */
66 TREE_OPTION_SUPPRESS_RESIZE = (1 << 1), /* Suppress callback */
67 TREE_OPTION_SUPPRESS_REDRAW = (1 << 2) /* Suppress callback */
69
70/**
71 * treeview control flags
72 */
73typedef enum {
74 TREEVIEW_NO_FLAGS = (0), /**< No flags set */
75 TREEVIEW_NO_MOVES = (1 << 0), /**< No node drags */
76 TREEVIEW_NO_DELETES = (1 << 1), /**< No node deletes */
78 TREEVIEW_DEL_EMPTY_DIRS = (1 << 2), /**< Delete dirs on empty */
79 TREEVIEW_SEARCHABLE = (1 << 3), /**< Treeview has search bar */
81
82/**
83 * treeview message types
84 */
86 TREE_MSG_NODE_DELETE, /**< Node to be deleted */
87 TREE_MSG_NODE_EDIT, /**< Node to be edited */
88 TREE_MSG_NODE_LAUNCH /**< Node to be launched */
89};
90
91
92/**
93 * treeview message
94 */
96 enum treeview_msg msg; /**< The message type */
97 union {
98 struct {
99 bool user; /**< True iff delete by user interaction */
100 } delete;
101 struct {
102 lwc_string *field; /**< The field being edited */
103 const char *text; /**< The proposed new value */
104 } node_edit; /* Client may call treeview_update_node_* */
105 struct {
106 browser_mouse_state mouse; /* Button / modifier used */
108 } data; /**< The message data. */
109};
110
111
112/**
113 * treeview field flags
114 */
116 TREE_FLAG_NONE = 0, /**< No flags set */
117 TREE_FLAG_ALLOW_EDIT = (1 << 0), /**< Whether allow edit field */
118 TREE_FLAG_DEFAULT = (1 << 1), /**< Whether field is default */
119 TREE_FLAG_SHOW_NAME = (1 << 2), /**< Whether field name shown */
120 TREE_FLAG_COPY_TEXT = (1 << 3), /**< Whether to copy to clipb */
121 TREE_FLAG_SEARCHABLE = (1 << 4), /**< Whether field is searchable */
122};
123
124
125/**
126 * Treeview field description
127 */
129 lwc_string *field; /**< A treeview field name */
130 enum treeview_field_flags flags; /**< Flags for field */
131};
132
133
134/**
135 * Treeview field data
136 */
138 lwc_string *field; /**< Field name */
139 const char *value; /**< Field value */
140 size_t value_len; /**< Field value length (bytes) */
141};
142
143
144/**
145 * Client callbacks for events concerning nodes
146 */
150};
151
152
153/**
154 * Prepare treeview module for treeview usage
155 *
156 * \return NSERROR_OK on success, appropriate error otherwise
157 */
159
160
161/**
162 * Finalise the treeview module (all treeviews must have been destroyed first)
163 *
164 * \return NSERROR_OK on success, appropriate error otherwise
165 */
167
168
169/**
170 * Create a treeview
171 *
172 * The fields array order is as follows (N = n_fields):
173 *
174 * fields[0] Main field for entries (shown when not expanded)
175 * fields[1]...fields[N-2] Additional fields for entries
176 * fields[N-1] Field for folder nodes
177 *
178 * So fields[0] and fields[N-1] have TREE_FLAG_DEFAULT set.
179 *
180 * \param tree Returns created treeview object
181 * \param callbacks Treeview client node event callbacks
182 * \param n_fields Number of treeview fields (see description)
183 * \param fields Array of treeview fields
184 * \param cw_t Callback table for core_window containing the treeview
185 * \param cw The core_window in which the treeview is shown
186 * \param flags Treeview behaviour flags
187 * \return NSERROR_OK on success, appropriate error otherwise
188 */
191 int n_fields,
192 struct treeview_field_desc fields[],
193 struct core_window *cw,
194 treeview_flags flags);
195
196
197/**
198 * Attach a treeview to a corewindow.
199 *
200 * Treeview must be detached.
201 *
202 * \param tree Treeview object
203 * \param cw_t Callback table for core_window containing the treeview
204 * \param cw The core_window in which the treeview is shown
205 * \return NSERROR_OK on success, appropriate error otherwise
206 */
207nserror treeview_cw_attach(treeview *tree, struct core_window *cw);
208
209
210/**
211 * Detach a treeview from a corewindow
212 *
213 * \param tree Treeview object
214 * \return NSERROR_OK on success, appropriate error otherwise
215 */
217
218
219/**
220 * Destroy a treeview object
221 *
222 * Will emit folder and entry deletion msg callbacks for all nodes in treeview.
223 *
224 * \param tree Treeview object to destroy
225 * \return NSERROR_OK on success, appropriate error otherwise
226 */
228
229
230/**
231 * Find a relation for node creation.
232 *
233 * If at_y is set, we find a relation that will put the created node at that
234 * position.
235 *
236 * If at_y is unset, we find a relation that would put the node below the first
237 * selected node, or at the end of the treeview if no nodes selected.
238 *
239 * \param tree Treeview object in which to create folder
240 * \param relation Existing node to insert as relation of, or NULL
241 * \param rel Folder's relationship to relation
242 * \param at_y Iff true, insert at y-offset
243 * \param y Y-offset in px from top of hotlist. Ignored if (!at_y).
244 * \return NSERROR_OK on success, appropriate error otherwise
245 */
247 enum treeview_relationship *rel,
248 bool at_y, int y);
249
250
251/**
252 * Create a folder node in given treeview
253 *
254 * \param tree Treeview object in which to create folder
255 * \param folder Returns created folder node
256 * \param relation Existing node to insert as relation of, or NULL
257 * \param rel Folder's relationship to relation
258 * \param field Field data
259 * \param data Client data for node event callbacks
260 * \param flags Treeview node options flags
261 * \return NSERROR_OK on success, appropriate error otherwise
262 *
263 * Field name must match name past in treeview_create fields[N-1].
264 *
265 * If relation is NULL, will insert as child of root node.
266 */
268 treeview_node **folder,
269 treeview_node *relation,
270 enum treeview_relationship rel,
271 const struct treeview_field_data *field,
272 void *data,
274
275
276/**
277 * Create an entry node in given treeview
278 *
279 * \param tree Treeview object in which to create entry
280 * \param entry Returns created entry node
281 * \param relation Existing node to insert as relation of, or NULL
282 * \param rel Folder's relationship to relation
283 * \param fields Array of field data
284 * \param data Client data for node event callbacks
285 * \param flags Treeview node options flags
286 * \return NSERROR_OK on success, appropriate error otherwise
287 *
288 * Fields array names must match names past in treeview_create fields[0...N-2].
289 *
290 * If relation is NULL, will insert as child of root node.
291 */
293 treeview_node **entry,
294 treeview_node *relation,
295 enum treeview_relationship rel,
296 const struct treeview_field_data fields[],
297 void *data,
299
300
301/**
302 * Update an folder node in given treeview
303 *
304 * \param tree Treeview object in which to create entry
305 * \param folder Folder node to update
306 * \param field New field data
307 * \param data Client data for node event callbacks
308 * \return NSERROR_OK on success, appropriate error otherwise
309 *
310 * Field name must match name past in treeview_create fields[N-1].
311 */
313 treeview_node *folder,
314 const struct treeview_field_data *field,
315 void *data);
316
317
318/**
319 * Update an entry node in given treeview
320 *
321 * \param tree Treeview object in which to create entry
322 * \param entry Entry node to update
323 * \param fields Array of new field data
324 * \param data Client data for node event callbacks
325 * \return NSERROR_OK on success, appropriate error otherwise
326 *
327 * Fields array names must match names past in treeview_create fields[0...N-2].
328 */
330 treeview_node *entry,
331 const struct treeview_field_data fields[],
332 void *data);
333
334
335/**
336 * Client callback for treeview_walk
337 *
338 * \param ctx Client context
339 * \param node_data Client data for the current treeview node
340 * \param type The node type
341 * \param abort Set to true to abort treeview walk prematurely
342 * \return NSERROR_OK on success, or appropriate error otherwise
343 */
344typedef nserror (*treeview_walk_cb)(void *ctx, void *node_data,
345 enum treeview_node_type type, bool *abort);
346
347
348/**
349 * Walk (depth first) a treeview subtree, calling a callback at each node of
350 * required type.
351 *
352 * Example usage: To export a treeview as XML, XML elements can be opened in
353 * enter_cb, and closed in leave_cb.
354 *
355 * Note, if deleting returned node in enter_cb, the walk must be terminated by
356 * setting abort to true.
357 *
358 * \param tree Treeview object to walk
359 * \param root Root node to walk tree from (or NULL for tree root)
360 * \param enter_cb Function to call on entering nodes, or NULL
361 * \param leave_cb Function to call on leaving nodes, or NULL
362 * \param ctx Client context, passed back to callback function
363 * \param type The node type(s) of interest
364 * \return NSERROR_OK on success, or appropriate error otherwise
365 */
367 treeview_walk_cb enter_cb, treeview_walk_cb leave_cb,
368 void *ctx, enum treeview_node_type type);
369
370
371/**
372 * Delete a treeview node
373 *
374 * \param tree Treeview object to delete node from
375 * \param n Node to delete
376 * \param flags Treeview node options flags
377 * \return NSERROR_OK on success, appropriate error otherwise
378 *
379 * Will emit folder or entry deletion msg callback.
380 */
383
384
385/**
386 * Expand a treeview node
387 *
388 * \param tree Treeview object to expand node in
389 * \param node Node to expand
390 * \return NSERROR_OK on success, appropriate error otherwise
391 */
393
394
395/**
396 * Contract a treeview node
397 *
398 * \param tree Treeview object to contract node in
399 * \param node Node to contract
400 * \return NSERROR_OK on success, appropriate error otherwise
401 */
403
404
405/**
406 * Expand a treeview's nodes
407 *
408 * \param tree Treeview object to expand nodes in
409 * \param only_folders Iff true, only folders are expanded.
410 * \return NSERROR_OK on success, appropriate error otherwise
411 */
412nserror treeview_expand(treeview *tree, bool only_folders);
413
414
415/**
416 * Contract a treeview's nodes
417 *
418 * \param tree Treeview object to contract nodes in
419 * \param all Iff false, only entries are contracted.
420 * \return NSERROR_OK on success, appropriate error otherwise
421 */
422nserror treeview_contract(treeview *tree, bool all);
423
424
425/**
426 * Redraw a treeview object
427 *
428 * \param tree Treeview object to render
429 * \param x X coordinate to render treeview at
430 * \param y Y coordinate to render treeview at
431 * \param clip Current clip rectangle (wrt tree origin)
432 * \param ctx Current redraw context
433 */
434void treeview_redraw(treeview *tree, int x, int y, struct rect *clip,
435 const struct redraw_context *ctx);
436
437
438/**
439 * Key press handling for treeviews.
440 *
441 * \param tree The treeview which got the keypress
442 * \param key The ucs4 character codepoint
443 * \return true if the keypress is dealt with, false otherwise.
444 */
445bool treeview_keypress(treeview *tree, uint32_t key);
446
447
448/**
449 * Handles all kinds of mouse action
450 *
451 * \param tree Treeview object
452 * \param mouse The current mouse state
453 * \param x X coordinate
454 * \param y Y coordinate
455 */
457 browser_mouse_state mouse, int x, int y);
458
459
460/**
461 * Determine whether treeview has a selection
462 *
463 * \param tree Treeview object to delete node from
464 * \return true iff treeview has a selection
465 */
467
468
469/**
470 * Get the first selected node
471 *
472 * \param tree Treeview object to get selected node in
473 * \param node_data Client data for the selected treeview node, or NULL
474 * \return node type of first selected node.
475 */
477 void **node_data);
478
479
480/**
481 * Edit the first selected node
482 *
483 * \param tree Treeview object to edit selected node in
484 */
486
487
488/**
489 * Find current height of a treeview
490 *
491 * \param tree Treeview object to find height of
492 * \return height of treeview in px
493 */
495
496
497/**
498 * Set the search string for a treeview with \ref TREEVIEW_SEARCHABLE
499 *
500 * \param tree Tree to set the search string for.
501 * \return NSERROR_OK on success, appropriate error otherwise
502 */
504 treeview *tree,
505 const char *string);
506
507#endif
nserror treeview_cw_detach(treeview *tree)
Detach a treeview from a corewindow.
Definition: treeview.c:2147
nserror treeview_cw_attach(treeview *tree, struct core_window *cw)
Attach a treeview to a corewindow.
Definition: treeview.c:2132
nserror treeview_node_contract(treeview *tree, treeview_node *node)
Contract a treeview node.
Definition: treeview.c:2415
nserror(* treeview_walk_cb)(void *ctx, void *node_data, enum treeview_node_type type, bool *abort)
Client callback for treeview_walk.
Definition: treeview.h:344
void treeview_redraw(treeview *tree, int x, int y, struct rect *clip, const struct redraw_context *ctx)
Redraw a treeview object.
Definition: treeview.c:2997
void treeview_mouse_action(treeview *tree, browser_mouse_state mouse, int x, int y)
Handles all kinds of mouse action.
Definition: treeview.c:4723
nserror treeview_create(treeview **tree, const struct treeview_callback_table *callbacks, int n_fields, struct treeview_field_desc fields[], struct core_window *cw, treeview_flags flags)
Create a treeview.
Definition: treeview.c:2022
nserror treeview_contract(treeview *tree, bool all)
Contract a treeview's nodes.
Definition: treeview.c:2434
nserror treeview_delete_node(treeview *tree, treeview_node *n, treeview_node_options_flags flags)
Delete a treeview node.
Definition: treeview.c:1924
treeview_msg
treeview message types
Definition: treeview.h:85
@ TREE_MSG_NODE_EDIT
Node to be edited.
Definition: treeview.h:87
@ TREE_MSG_NODE_LAUNCH
Node to be launched.
Definition: treeview.h:88
@ TREE_MSG_NODE_DELETE
Node to be deleted.
Definition: treeview.h:86
enum treeview_node_type treeview_get_selection(treeview *tree, void **node_data)
Get the first selected node.
Definition: treeview.c:3363
nserror treeview_fini(void)
Finalise the treeview module (all treeviews must have been destroyed first)
Definition: treeview.c:5384
void treeview_edit_selection(treeview *tree)
Edit the first selected node.
Definition: treeview.c:4389
bool treeview_has_selection(treeview *tree)
Determine whether treeview has a selection.
Definition: treeview.c:3326
bool treeview_keypress(treeview *tree, uint32_t key)
Key press handling for treeviews.
Definition: treeview.c:4006
nserror treeview_update_node_entry(treeview *tree, treeview_node *entry, const struct treeview_field_data fields[], void *data)
Update an entry node in given treeview.
Definition: treeview.c:1314
nserror treeview_get_relation(treeview *tree, treeview_node **relation, enum treeview_relationship *rel, bool at_y, int y)
Find a relation for node creation.
Definition: treeview.c:3776
nserror treeview_create_node_folder(treeview *tree, treeview_node **folder, treeview_node *relation, enum treeview_relationship rel, const struct treeview_field_data *field, void *data, treeview_node_options_flags flags)
Create a folder node in given treeview.
Definition: treeview.c:1198
nserror treeview_destroy(treeview *tree)
Destroy a treeview object.
Definition: treeview.c:2158
nserror treeview_walk(treeview *tree, treeview_node *root, treeview_walk_cb enter_cb, treeview_walk_cb leave_cb, void *ctx, enum treeview_node_type type)
Walk (depth first) a treeview subtree, calling a callback at each node of required type.
Definition: treeview.c:1534
int treeview_get_height(treeview *tree)
Find current height of a treeview.
Definition: treeview.c:4908
nserror treeview_node_expand(treeview *tree, treeview_node *node)
Expand a treeview node.
Definition: treeview.c:2291
nserror treeview_init(void)
Prepare treeview module for treeview usage.
Definition: treeview.c:5335
nserror treeview_create_node_entry(treeview *tree, treeview_node **entry, treeview_node *relation, enum treeview_relationship rel, const struct treeview_field_data fields[], void *data, treeview_node_options_flags flags)
Create an entry node in given treeview.
Definition: treeview.c:1388
nserror treeview_expand(treeview *tree, bool only_folders)
Expand a treeview's nodes.
Definition: treeview.c:2525
treeview_node_type
treeview node type
Definition: treeview.h:43
@ TREE_NODE_ENTRY
Node is an entry.
Definition: treeview.h:47
@ TREE_NODE_NONE
No node
Definition: treeview.h:44
@ TREE_NODE_FOLDER
Node is folder.
Definition: treeview.h:46
@ TREE_NODE_ROOT
Node is treeview's root.
Definition: treeview.h:45
nserror treeview_set_search_string(treeview *tree, const char *string)
Set the search string for a treeview with TREEVIEW_SEARCHABLE.
Definition: treeview.c:4922
treeview_node_options_flags
Node change handling options.
Definition: treeview.h:63
@ TREE_OPTION_NONE
Definition: treeview.h:64
@ TREE_OPTION_SUPPRESS_RESIZE
Definition: treeview.h:66
@ TREE_OPTION_SPECIAL_DIR
Definition: treeview.h:65
@ TREE_OPTION_SUPPRESS_REDRAW
Definition: treeview.h:67
treeview_field_flags
treeview field flags
Definition: treeview.h:115
@ TREE_FLAG_SEARCHABLE
Whether field is searchable.
Definition: treeview.h:121
@ TREE_FLAG_SHOW_NAME
Whether field name shown.
Definition: treeview.h:119
@ TREE_FLAG_COPY_TEXT
Whether to copy to clipb.
Definition: treeview.h:120
@ TREE_FLAG_ALLOW_EDIT
Whether allow edit field.
Definition: treeview.h:117
@ TREE_FLAG_NONE
No flags set.
Definition: treeview.h:116
@ TREE_FLAG_DEFAULT
Whether field is default.
Definition: treeview.h:118
treeview_relationship
Relationship between nodes.
Definition: treeview.h:54
@ TREE_REL_FIRST_CHILD
Definition: treeview.h:55
@ TREE_REL_NEXT_SIBLING
Definition: treeview.h:56
treeview_flags
treeview control flags
Definition: treeview.h:73
@ TREEVIEW_NO_DELETES
No node deletes.
Definition: treeview.h:76
@ TREEVIEW_DEL_EMPTY_DIRS
Delete dirs on empty.
Definition: treeview.h:78
@ TREEVIEW_READ_ONLY
Definition: treeview.h:77
@ TREEVIEW_NO_FLAGS
No flags set.
Definition: treeview.h:74
@ TREEVIEW_SEARCHABLE
Treeview has search bar.
Definition: treeview.h:79
@ TREEVIEW_NO_MOVES
No node drags.
Definition: treeview.h:75
nserror treeview_update_node_folder(treeview *tree, treeview_node *folder, const struct treeview_field_data *field, void *data)
Update an folder node in given treeview.
Definition: treeview.c:1266
nserror
Enumeration of error codes.
Definition: errors.h:29
static struct directory * root
Definition: filename.c:55
const char * type
Definition: filetype.cpp:44
Core mouse and pointer states.
browser_mouse_state
Mouse state.
Definition: mouse.h:43
static BList * callbacks
List of all callbacks.
Definition: schedule.cpp:44
Rectangle coordinates.
Definition: types.h:40
Redraw context.
Definition: plotters.h:51
Client callbacks for events concerning nodes.
Definition: treeview.h:147
nserror(* folder)(struct treeview_node_msg msg, void *data)
Definition: treeview.h:148
nserror(* entry)(struct treeview_node_msg msg, void *data)
Definition: treeview.h:149
Treeview field data.
Definition: treeview.h:137
const char * value
Field value.
Definition: treeview.h:139
lwc_string * field
Field name.
Definition: treeview.h:138
size_t value_len
Field value length (bytes)
Definition: treeview.h:140
Treeview field description.
Definition: treeview.h:128
lwc_string * field
A treeview field name.
Definition: treeview.h:129
enum treeview_field_flags flags
Flags for field.
Definition: treeview.h:130
treeview message
Definition: treeview.h:95
bool user
True iff delete by user interaction.
Definition: treeview.h:99
lwc_string * field
The field being edited.
Definition: treeview.h:102
union treeview_node_msg::@94 data
The message data.
const char * text
The proposed new value.
Definition: treeview.h:103
browser_mouse_state mouse
Definition: treeview.h:106
struct treeview_node_msg::@94::@97 node_launch
struct treeview_node_msg::@94::@96 node_edit
enum treeview_msg msg
The message type.
Definition: treeview.h:96
Treeview node.
Definition: treeview.c:133
The treeview context.
Definition: treeview.c:232
static nserror clip(const struct redraw_context *ctx, const struct rect *clip)
Sets a clip rectangle for subsequent plot operations.
Definition: plot.c:357