Table of Contents
- xtk_graph.h
- Types
- XtkGraphNode
- XtkGraphEdge
- XtkGraph
- XtkGraphPresentNodeVisitor
- XtkGraphNodeClickCallback
- XtkGraphNodeMovedCallback
- XtkGraphScrollCallback
- Constants
- Functions
- xtk_graph_create
- xtk_graph_destroy
- xtk_graph_set_theme
- xtk_graph_get_window
- xtk_graph_resize
- xtk_graph_set_position
- xtk_graph_set_data
- xtk_graph_resort
- xtk_graph_get_present_node_count
- xtk_graph_visit_present_nodes
- xtk_graph_get_content_width
- xtk_graph_get_content_height
- xtk_graph_handle_event
- xtk_graph_draw
- xtk_graph_set_node_click_callback
- xtk_graph_set_node_moved_callback
- xtk_graph_set_scroll_callback
- xtk_graph_set_scroll_offset
- xtk_graph_set_selected_node
- xtk_graph_set_initial_position
- xtk_graph_set_node_position
- xtk_graph_get_node_position
xtk_graph.h
A view of labeled nodes joined by lines, which the user can drag about.
A graph view shows a set of nodes, each a rounded box with a label, and the edges between them, drawn as lines that bend around the boxes in their way. It is a map of connections (which document links to which, for example), not a chart. The widget does not know what the nodes mean: the program gives it nodes (an id and a label) and edges (two ids).
The widget places new nodes itself. A node keeps its place across calls of xtk_graph_set_data(), matched by id, so a program can give the whole set again whenever its data changes. The user can drag a node to another place, and xtk_graph_resort() arranges all nodes again on request.
Two kinds of node are colored. The selected node (xtk_graph_set_selected_node()) and the nodes joined to it have colors of their own. The node with the most edges (the hub) has a third color, which fades with each step away from it. A node with both kinds of color gets an even mix of them. An edge that touches a colored node is drawn in a color that stands out from that node.
Making one. xtk_graph_create() takes the parent window, the position and size of the window, and a GraphBlock, usually &config.graph. The window is mapped at once. The graph keeps the GraphBlock pointer and reads it on each draw, so it must stay valid. Free the graph with xtk_graph_destroy().
Scrolling. The window is only the visible area; the content can be much larger. The graph has no scrollbars: the program tells it which part of the content to show with xtk_graph_set_scroll_offset(). In a tool window, for example:
- Make the graph a child of
xtk_toolwindow_get_viewport_window()and keep it that size withxtk_graph_resize(). - Give
xtk_toolwindow_set_content_size()the values ofxtk_graph_get_content_width()andxtk_graph_get_content_height(), so that the tool window's scrollbars cover the content. - In the callback of
xtk_toolwindow_set_scroll_changed_callback(), callxtk_graph_set_scroll_offset()and thenxtk_graph_draw(). - Pass the graph's wheel callback (
xtk_graph_set_scroll_callback()) on to the tool window.
Only what is in view is drawn, so drawing costs the same however large the graph is.
Events. Pass every X event to xtk_graph_handle_event().
- Expose redraws the graph.
- Pressing on a node and moving the pointer by more than 4 pixels drags the node; it is drawn with a focus-border outline while it moves. On release the moved callback fires with the node's new place.
- A press and release on a node without moving it fires the click callback. The click does not select the node: the program does that with
xtk_graph_set_selected_node()when it wants to. - The wheel fires the scroll callback, with 40 pixels a step.
The calls that change the data, the selection or the scroll offset do not redraw; call xtk_graph_draw() after them.
Theme keys. [graph] (GraphBlock, config.graph):
background: behind the nodes (default #2E2E2E).node-background: the fill of a node (default #383838).node-foreground: the labels (default D9D9D9).edge: the lines between nodes that are not colored (default #808080).selected-background: the selected node (default E68C26).neighbor-background: the nodes joined to the selected one (default BF9959).hub-background: the node with the most edges (default #8C59D9).focus-border: the outline of the node being dragged (default #80B3FF).
background and the four node fills can be gradients. The labels are always in the "sans" font at 12 pixels; there is no font key.
Example.
static void clicked(XtkGraph *graph, const char *node_id, void *user_data) {
(void)user_data;
xtk_graph_set_selected_node(graph, node_id);
xtk_graph_draw(graph);
}
XtkConfig config;
xtk_config_init_defaults(&config);
XtkTheme *theme = xtk_theme_new();
if (theme && xtk_theme_builtin("Dark", theme)) xtk_theme_apply(&config, theme);
xtk_theme_free(theme);
XtkGraph *graph = xtk_graph_create(display, screen, window, 0, 0, 600, 400, &config.graph);
XtkGraphNode nodes[3] = { { "a", "Intro" }, { "b", "Setup" }, { "c", "Usage" } };
XtkGraphEdge edges[2] = { { "a", "b" }, { "a", "c" } };
xtk_graph_set_data(graph, nodes, 3, edges, 2);
xtk_graph_set_node_click_callback(graph, clicked, NULL);
XEvent event;
for (;;) {
XNextEvent(display, &event);
xtk_graph_handle_event(graph, &event);
}
*
Limitations.
- There is no keyboard control, no zoom, and no panning by dragging the background.
- With more than 400 nodes shown, new nodes are placed on a spiral without the force layout, overlaps are not removed, and edges are drawn straight. With more than 3000 edges, an edge is drawn only when its less connected end is in view.
#include "xtk_graph.h"
Types: XtkGraphNode, XtkGraphEdge, XtkGraph, XtkGraphPresentNodeVisitor, XtkGraphNodeClickCallback, XtkGraphNodeMovedCallback, XtkGraphScrollCallback
Constants: XTK_GRAPH_ID_MAX, XTK_GRAPH_LABEL_MAX
Functions: xtk_graph_create, xtk_graph_destroy, xtk_graph_set_theme, xtk_graph_get_window, xtk_graph_resize, xtk_graph_set_position, xtk_graph_set_data, xtk_graph_resort, xtk_graph_get_present_node_count, xtk_graph_visit_present_nodes, xtk_graph_get_content_width, xtk_graph_get_content_height, xtk_graph_handle_event, xtk_graph_draw, xtk_graph_set_node_click_callback, xtk_graph_set_node_moved_callback, xtk_graph_set_scroll_callback, xtk_graph_set_scroll_offset, xtk_graph_set_selected_node, xtk_graph_set_initial_position, xtk_graph_set_node_position, xtk_graph_get_node_position
Types
XtkGraphNode
One node, as the program gives it to xtk_graph_set_data().
| Field | Type | Description |
|---|---|---|
id[XTK_GRAPH_ID_MAX] |
char |
The id the program knows the node by. It must be unique; the node's place is kept by it. |
label[XTK_GRAPH_LABEL_MAX] |
char |
The text drawn in the node. The box is as wide as the text (at least 48 pixels). |
XtkGraphEdge
One edge between two nodes, as the program gives it to xtk_graph_set_data().
| Field | Type | Description |
|---|---|---|
from_id[XTK_GRAPH_ID_MAX] |
char |
The id of one end. Edges have no direction. |
to_id[XTK_GRAPH_ID_MAX] |
char |
The id of the other end. |
XtkGraph
typedef struct XtkGraph XtkGraph;
A graph view (opaque). Made by xtk_graph_create().
XtkGraphPresentNodeVisitor
typedef void (*XtkGraphPresentNodeVisitor)(const char *id, void *user_data);
Called with the id of each shown node.
XtkGraphNodeClickCallback
typedef void (*XtkGraphNodeClickCallback)(XtkGraph *graph, const char *node_id, void *user_data);
Called when a node is clicked without being dragged.
It fires on the release. It may destroy the graph or give it new data.
XtkGraphNodeMovedCallback
typedef void (*XtkGraphNodeMovedCallback)(XtkGraph *graph, const char *node_id, int x, int y, void *user_data);
Called once, on the release, after the user dragged a node.
The graph keeps the new place itself; the callback lets the program save it. It may destroy the graph or give it new data.
XtkGraphScrollCallback
typedef void (*XtkGraphScrollCallback)(XtkGraph *graph, int delta_y, void *user_data);
Called for the mouse wheel over the graph.
The graph takes the button events of its window (to drag nodes), so the wheel does not reach the container around it; the program scrolls the container from this callback.
Constants
XTK_GRAPH_ID_MAX
#define XTK_GRAPH_ID_MAX 64
The size of a node id, the NUL included.
XTK_GRAPH_LABEL_MAX
#define XTK_GRAPH_LABEL_MAX 128
The size of a node label, the NUL included.
Functions
xtk_graph_create
XtkGraph *xtk_graph_create(
Display *display,
int screen,
Window parent,
int x,
int y,
int width,
int height,
const GraphBlock *theme);
Creates a graph view, mapped, with no nodes.
| Parameter | Description |
|---|---|
display |
The X display. |
screen |
The screen number. |
parent |
The parent window. |
x |
The x position in the parent. |
y |
The y position in the parent. |
width |
The width of the window in pixels; it must be more than 0. |
height |
The height of the window in pixels; it must be more than 0. |
theme |
The colors, usually &config.graph. The pointer is kept and must stay valid. When it is NULL, nothing is drawn until xtk_graph_set_theme() gives one. |
Returns: The new graph, or NULL when display is NULL, the size is not more than 0, or the window cannot be made.
xtk_graph_destroy
void xtk_graph_destroy(XtkGraph *graph);
Destroys the graph and its window.
| Parameter | Description |
|---|---|
graph |
The graph. NULL is allowed and does nothing. |
xtk_graph_set_theme
void xtk_graph_set_theme(XtkGraph *graph, const GraphBlock *theme);
Gives the graph new colors, and redraws it.
Call it after a theme change, even with the same pointer. The fills of the nodes are worked out again from the new colors.
| Parameter | Description |
|---|---|
graph |
The graph. |
theme |
The new colors, usually &config.graph. NULL does nothing. The pointer is kept. |
xtk_graph_get_window
Window xtk_graph_get_window(const XtkGraph *graph);
Gives the graph's window.
| Parameter | Description |
|---|---|
graph |
The graph, or NULL. |
Returns: The window, or None when graph is NULL.
xtk_graph_resize
void xtk_graph_resize(XtkGraph *graph, int width, int height);
Resizes the graph's window (the visible area).
The X server's Expose after the resize redraws the graph.
| Parameter | Description |
|---|---|
graph |
The graph. |
width |
The new width in pixels. A value of 0 or less is ignored. |
height |
The new height in pixels. A value of 0 or less is ignored. |
xtk_graph_set_position
void xtk_graph_set_position(XtkGraph *graph, int x, int y);
Moves the graph's window within its parent.
| Parameter | Description |
|---|---|
graph |
The graph. |
x |
The new x position in the parent. |
y |
The new y position in the parent. |
xtk_graph_set_data
void xtk_graph_set_data(
XtkGraph *graph,
const XtkGraphNode *nodes,
int node_count,
const XtkGraphEdge *edges,
int edge_count);
Sets the nodes and edges to show.
Both arrays are copied; the caller keeps its own. A node whose id was given before keeps its place (where it was placed or dragged to), even if it was left out of calls in between. A node that is not yet placed gets a place: from xtk_graph_set_initial_position() if one was given; otherwise, when no node shown has a place yet, all are laid out together (in rings around the selected node if there is one, else by a force layout of each connected group, packed side by side); when some nodes already have a place, the new ones are fitted in around them, without moving them. An edge with an end whose id the graph has never been given is left out. The colors are worked out again. The graph is not redrawn.
| Parameter | Description |
|---|---|
graph |
The graph. |
nodes |
The nodes to show. |
node_count |
The number of nodes. |
edges |
The edges to show. |
edge_count |
The number of edges. |
xtk_graph_resort
void xtk_graph_resort(XtkGraph *graph);
Arranges all shown nodes again from the start.
When a selected node is shown, it goes in the middle of the content and the other nodes on rings around it, with no two boxes overlapping. With no selected node, the nodes get the same force layout as a new graph, and then nodes are swapped where that makes fewer edges cross. Every place is lost, those the user dragged to included; this is for a command such as "Arrange", not for every update. It does nothing on an empty graph, and it does not redraw.
| Parameter | Description |
|---|---|
graph |
The graph. |
xtk_graph_get_present_node_count
int xtk_graph_get_present_node_count(const XtkGraph *graph);
Gives the number of nodes shown.
These are the nodes of the last xtk_graph_set_data() call. A program can use it, for example, to disable an "Arrange" command on an empty graph.
| Parameter | Description |
|---|---|
graph |
The graph, or NULL. |
Returns: The number of nodes shown; 0 when graph is NULL.
xtk_graph_visit_present_nodes
void xtk_graph_visit_present_nodes(
const XtkGraph *graph,
XtkGraphPresentNodeVisitor visitor,
void *user_data);
Calls a function for every shown node.
Only the nodes of the last xtk_graph_set_data() call are visited, not every node the graph has ever been given; a program that saves the places of the nodes (with xtk_graph_get_node_position()) can use it to save only those.
| Parameter | Description |
|---|---|
graph |
The graph. |
visitor |
The function called with each id. |
user_data |
Passed to visitor. |
xtk_graph_get_content_width
int xtk_graph_get_content_width(const XtkGraph *graph);
Gives the width of the content.
It is the right edge of the rightmost shown node plus a 20-pixel margin: the width to give a scrolling container.
| Parameter | Description |
|---|---|
graph |
The graph, or NULL. |
Returns: The width in pixels; 0 when no node is shown or graph is NULL.
xtk_graph_get_content_height
int xtk_graph_get_content_height(const XtkGraph *graph);
Gives the height of the content.
It is the bottom edge of the lowest shown node plus a 20-pixel margin: the height to give a scrolling container.
| Parameter | Description |
|---|---|
graph |
The graph, or NULL. |
Returns: The height in pixels; 0 when no node is shown or graph is NULL.
xtk_graph_handle_event
int xtk_graph_handle_event(XtkGraph *graph, XEvent *event);
Handles one X event.
Pass every event of the program's event loop; events for other windows are ignored. See the top of this page for what each event does.
| Parameter | Description |
|---|---|
graph |
The graph. |
event |
The event. |
Returns: 1 if the event was for the graph's window (it is then handled, or ignored if the graph has no use for it), 0 if it was for another window or graph is NULL.
xtk_graph_draw
void xtk_graph_draw(XtkGraph *graph);
Draws the part of the graph in view.
| Parameter | Description |
|---|---|
graph |
The graph. NULL, or a graph with no theme, does nothing. |
xtk_graph_set_node_click_callback
void xtk_graph_set_node_click_callback(
XtkGraph *graph,
XtkGraphNodeClickCallback callback,
void *user_data);
Sets the function called when a node is clicked.
| Parameter | Description |
|---|---|
graph |
The graph. |
callback |
The function, or NULL for none. |
user_data |
Passed to the function. |
xtk_graph_set_node_moved_callback
void xtk_graph_set_node_moved_callback(
XtkGraph *graph,
XtkGraphNodeMovedCallback callback,
void *user_data);
Sets the function called when a node has been dragged to a new place.
| Parameter | Description |
|---|---|
graph |
The graph. |
callback |
The function, or NULL for none. |
user_data |
Passed to the function. |
xtk_graph_set_scroll_callback
void xtk_graph_set_scroll_callback(
XtkGraph *graph,
XtkGraphScrollCallback callback,
void *user_data);
Sets the function called for the mouse wheel over the graph.
| Parameter | Description |
|---|---|
graph |
The graph. |
callback |
The function, or NULL for none (the wheel then does nothing). |
user_data |
Passed to the function. |
xtk_graph_set_scroll_offset
void xtk_graph_set_scroll_offset(XtkGraph *graph, int offset_x, int offset_y);
Sets which part of the content the window shows.
Only what is in view at this offset is drawn and can be clicked. It does not redraw; call xtk_graph_draw() after it.
| Parameter | Description |
|---|---|
graph |
The graph. |
offset_x |
The content x shown at the left edge of the window; 0 shows the content from its left edge. |
offset_y |
The content y shown at the top edge of the window. |
xtk_graph_set_selected_node
void xtk_graph_set_selected_node(XtkGraph *graph, const char *node_id);
Selects a node, which colors it and the nodes joined to it.
The selected node gets selected-background and the nodes one edge away neighbor-background; the other nodes keep their fill (or their hub color). The selection also decides the layout of xtk_graph_resort() and of a new graph. It does not redraw; call xtk_graph_draw() after it.
| Parameter | Description |
|---|---|
graph |
The graph. |
node_id |
The id of the node, or NULL or "" for none. |
xtk_graph_set_initial_position
void xtk_graph_set_initial_position(XtkGraph *graph, const char *node_id, int x, int y);
Gives a node the place it will have when it first appears.
Use it for a node the program has a saved place for, before the xtk_graph_set_data() call that first shows it. It does nothing for a node that already has a place (placed by the graph, dragged, or given before). Nodes with no saved place need no call; the graph places them.
| Parameter | Description |
|---|---|
graph |
The graph. |
node_id |
The node's id. |
x |
The x of the node's top left corner, in content coordinates. |
y |
The y of the node's top left corner, in content coordinates. |
xtk_graph_set_node_position
void xtk_graph_set_node_position(XtkGraph *graph, const char *node_id, int x, int y);
Moves a node the graph already knows.
Unlike xtk_graph_set_initial_position(), this always applies, also to a node the user has dragged; use it to undo or redo a drag. It does nothing for an id the graph has never been given, and it does not redraw; call xtk_graph_draw() after it.
| Parameter | Description |
|---|---|
graph |
The graph. |
node_id |
The node's id. |
x |
The x of the node's top left corner, in content coordinates. |
y |
The y of the node's top left corner, in content coordinates. |
xtk_graph_get_node_position
int xtk_graph_get_node_position(
const XtkGraph *graph,
const char *node_id,
int *x_out,
int *y_out);
Gives the place of a node.
A program can use it to save the places the graph has, for example after xtk_graph_resort(); the moved callback covers only drags.
| Parameter | Description |
|---|---|
graph |
The graph, or NULL. |
node_id |
The node's id. |
x_out |
Receives the x of the node's top left corner, in content coordinates, or NULL. |
y_out |
Receives the y of the node's top left corner, or NULL. |
Returns: 1 if the node is known and has a place; 0 if not, and the outputs are then not changed.
xtk API
Widgets
- button
- checkbox
- radiobutton
- label
- entry
- textview
- edit_history
- spinbutton
- slider
- progressbar
- combobox
- scrollbar
- swatch
- panel
- statusbar
- tooltip
- toolbar
- icongrid
- iconview
- pathbar
- treeview
- datagrid
- graph
- scheme_wheel
- zoom
- tray
Containers and menus
Dialogs
Rich text
Drawing and images
Color
Configuration and themes
Input and the X server
Safety
Data formats