Add relevent extensions to allow treeviews to be used as an embedded list component.

svn path=/trunk/netsurf/; revision=2753
This commit is contained in:
Richard Wilson 2006-07-15 15:39:33 +00:00
parent 4c564c28bf
commit 546bf17a7f
5 changed files with 102 additions and 75 deletions

View File

@ -49,12 +49,12 @@ void tree_initialise(struct tree *tree) {
assert(tree); assert(tree);
tree_set_node_expanded(tree->root, true); tree_set_node_expanded(tree, tree->root, true);
tree_initialise_nodes(tree->root); tree_initialise_nodes(tree, tree->root);
tree_recalculate_node_positions(tree->root); tree_recalculate_node_positions(tree, tree->root);
tree_set_node_expanded(tree->root, false); tree_set_node_expanded(tree, tree->root, false);
tree->root->expanded = true; tree->root->expanded = true;
tree_recalculate_node_positions(tree->root); tree_recalculate_node_positions(tree, tree->root);
tree_recalculate_size(tree); tree_recalculate_size(tree);
} }
@ -64,22 +64,22 @@ void tree_initialise(struct tree *tree) {
* *
* \param root the root node to update from * \param root the root node to update from
*/ */
void tree_initialise_nodes(struct node *root) { void tree_initialise_nodes(struct tree *tree, struct node *root) {
struct node *node; struct node *node;
assert(root); assert(root);
tree_initialising++; tree_initialising++;
for (node = root; node; node = node->next) { for (node = root; node; node = node->next) {
tree_recalculate_node(node, true); tree_recalculate_node(tree, node, true);
if (node->child) { if (node->child) {
tree_initialise_nodes(node->child); tree_initialise_nodes(tree, node->child);
} }
} }
tree_initialising--; tree_initialising--;
if (tree_initialising == 0) if (tree_initialising == 0)
tree_recalculate_node_positions(root); tree_recalculate_node_positions(tree, root);
} }
@ -98,16 +98,16 @@ void tree_handle_node_changed(struct tree *tree, struct node *node,
assert(node); assert(node);
if ((expansion) && (node->expanded) && (node->child)) { if ((expansion) && (node->expanded) && (node->child)) {
tree_set_node_expanded(node->child, false); tree_set_node_expanded(tree, node->child, false);
tree_set_node_selected(tree, node->child, false); tree_set_node_selected(tree, node->child, false);
} }
width = node->box.width; width = node->box.width;
height = node->box.height; height = node->box.height;
if ((recalculate_sizes) || (expansion)) if ((recalculate_sizes) || (expansion))
tree_recalculate_node(node, true); tree_recalculate_node(tree, node, true);
if ((node->box.height != height) || (expansion)) { if ((node->box.height != height) || (expansion)) {
tree_recalculate_node_positions(tree->root); tree_recalculate_node_positions(tree, tree->root);
tree_redraw_area(tree, 0, node->box.y, 16384, 16384); tree_redraw_area(tree, 0, node->box.y, 16384, 16384);
} else { } else {
width = (width > node->box.width) ? width : node->box.width; width = (width > node->box.width) ? width : node->box.width;
@ -135,11 +135,11 @@ void tree_handle_node_element_changed(struct tree *tree, struct node_element *el
tree_recalculate_node_element(element); tree_recalculate_node_element(element);
if (element->box.height != height) { if (element->box.height != height) {
tree_recalculate_node(element->parent, false); tree_recalculate_node(tree, element->parent, false);
tree_redraw_area(tree, 0, element->box.y, 16384, 16384); tree_redraw_area(tree, 0, element->box.y, 16384, 16384);
} else { } else {
if (element->box.width != width) if (element->box.width != width)
tree_recalculate_node(element->parent, false); tree_recalculate_node(tree, element->parent, false);
width = (width > element->box.width) ? width : width = (width > element->box.width) ? width :
element->box.width; element->box.width;
tree_redraw_area(tree, element->box.x, element->box.y, width, element->box.height); tree_redraw_area(tree, element->box.x, element->box.y, width, element->box.height);
@ -153,7 +153,7 @@ void tree_handle_node_element_changed(struct tree *tree, struct node_element *el
* \param node the node to update * \param node the node to update
* \param recalculate_sizes whether the node elements have changed * \param recalculate_sizes whether the node elements have changed
*/ */
void tree_recalculate_node(struct node *node, bool recalculate_sizes) { void tree_recalculate_node(struct tree *tree, struct node *node, bool recalculate_sizes) {
struct node_element *element; struct node_element *element;
int width, height; int width, height;
@ -186,7 +186,7 @@ void tree_recalculate_node(struct node *node, bool recalculate_sizes) {
if (height != node->box.height) { if (height != node->box.height) {
for (; node->parent; node = node->parent); for (; node->parent; node = node->parent);
if (tree_initialising == 0) if (tree_initialising == 0)
tree_recalculate_node_positions(node); tree_recalculate_node_positions(tree, node);
} }
} }
@ -196,7 +196,7 @@ void tree_recalculate_node(struct node *node, bool recalculate_sizes) {
* *
* \param root the root node to update from * \param root the root node to update from
*/ */
void tree_recalculate_node_positions(struct node *root) { void tree_recalculate_node_positions(struct tree *tree, struct node *root) {
struct node *parent; struct node *parent;
struct node *node; struct node *node;
struct node *child; struct node *child;
@ -216,14 +216,14 @@ void tree_recalculate_node_positions(struct node *root) {
child = child->next) child = child->next)
node->box.y += child->box.height; node->box.y += child->box.height;
} else { } else {
node->box.x = 0; node->box.x = tree->no_furniture ? -NODE_INSTEP + 8 : 0;
node->box.y = -40; node->box.y = -40;
} }
if (node->expanded) { if (node->expanded) {
if (node->folder) { if (node->folder) {
node->data.box.x = node->box.x; node->data.box.x = node->box.x;
node->data.box.y = node->box.y; node->data.box.y = node->box.y;
tree_recalculate_node_positions(node->child); tree_recalculate_node_positions(tree, node->child);
} else { } else {
y = node->box.y; y = node->box.y;
for (element = &node->data; element; for (element = &node->data; element;
@ -305,14 +305,14 @@ int tree_get_node_height(struct node *node) {
* \param node the node to set all siblings and descendants of * \param node the node to set all siblings and descendants of
* \param expanded the expansion state to set * \param expanded the expansion state to set
*/ */
void tree_set_node_expanded(struct node *node, bool expanded) { void tree_set_node_expanded(struct tree *tree, struct node *node, bool expanded) {
for (; node; node = node->next) { for (; node; node = node->next) {
if (node->expanded != expanded) { if (node->expanded != expanded) {
node->expanded = expanded; node->expanded = expanded;
tree_recalculate_node(node, false); tree_recalculate_node(tree, node, false);
} }
if ((node->child) && (node->expanded)) if ((node->child) && (node->expanded))
tree_set_node_expanded(node->child, expanded); tree_set_node_expanded(tree, node->child, expanded);
} }
} }
@ -337,18 +337,18 @@ bool tree_handle_expansion(struct tree *tree, struct node *node, bool expanded,
((folder && (node->folder)) || (leaf && (!node->folder)))) { ((folder && (node->folder)) || (leaf && (!node->folder)))) {
node->expanded = expanded; node->expanded = expanded;
if (node->child) if (node->child)
tree_set_node_expanded(node->child, false); tree_set_node_expanded(tree, node->child, false);
if ((node->data.next) && (node->data.next->box.height == 0)) if ((node->data.next) && (node->data.next->box.height == 0))
tree_recalculate_node(node, true); tree_recalculate_node(tree, node, true);
else else
tree_recalculate_node(node, false); tree_recalculate_node(tree, node, false);
redraw = true; redraw = true;
} }
if ((node->child) && (node->expanded)) if ((node->child) && (node->expanded))
redraw |= tree_handle_expansion(tree, node->child, expanded, folder, leaf); redraw |= tree_handle_expansion(tree, node->child, expanded, folder, leaf);
} }
if ((entry == tree->root) && (redraw)) { if ((entry == tree->root) && (redraw)) {
tree_recalculate_node_positions(tree->root); tree_recalculate_node_positions(tree, tree->root);
tree_redraw_area(tree, 0, 0, 16384, 16384); tree_redraw_area(tree, 0, 0, 16384, 16384);
tree_recalculate_size(tree); tree_recalculate_size(tree);
} }
@ -493,7 +493,7 @@ void tree_move_selected_nodes(struct tree *tree, struct node *destination, bool
link = tree_move_processing_node(tree->root, link, false, false); link = tree_move_processing_node(tree->root, link, false, false);
tree_clear_processing(tree->root); tree_clear_processing(tree->root);
tree_recalculate_node_positions(tree->root); tree_recalculate_node_positions(tree, tree->root);
tree_redraw_area(tree, 0, 0, 16384, 16384); tree_redraw_area(tree, 0, 0, 16384, 16384);
} }
@ -718,13 +718,14 @@ void tree_draw_node(struct tree *tree, struct node *node, int clip_x, int clip_y
for (; node; node = node->next) { for (; node; node = node->next) {
if (node->box.y > y_max) return; if (node->box.y > y_max) return;
if (node->next) if ((node->next) && (!tree->no_furniture))
tree_draw_line(node->box.x - (NODE_INSTEP / 2), tree_draw_line(node->box.x - (NODE_INSTEP / 2),
node->box.y + (40 / 2), 0, node->box.y + (40 / 2), 0,
node->next->box.y - node->box.y); node->next->box.y - node->box.y);
if ((node->box.x < x_max) && (node->box.y < y_max) && if ((node->box.x < x_max) && (node->box.y < y_max) &&
(node->box.x + node->box.width + NODE_INSTEP >= clip_x) && (node->box.x + node->box.width + NODE_INSTEP >= clip_x) &&
(node->box.y + node->box.height >= clip_y)) { (node->box.y + node->box.height >= clip_y)) {
if (!tree->no_furniture) {
if ((node->expanded) && (node->child)) if ((node->expanded) && (node->child))
tree_draw_line(node->box.x + (NODE_INSTEP / 2), tree_draw_line(node->box.x + (NODE_INSTEP / 2),
node->data.box.y + node->data.box.height, 0, node->data.box.y + node->data.box.height, 0,
@ -740,6 +741,7 @@ void tree_draw_node(struct tree *tree, struct node *node, int clip_x, int clip_y
node->data.box.height - (40 / 2), node->data.box.height - (40 / 2),
(NODE_INSTEP / 2) - 4, 0); (NODE_INSTEP / 2) - 4, 0);
tree_draw_node_expansion(tree, node); tree_draw_node_expansion(tree, node);
}
if (node->expanded) if (node->expanded)
for (element = &node->data; element; for (element = &node->data; element;
element = element->next) element = element->next)
@ -935,7 +937,7 @@ void tree_delete_node(struct tree *tree, struct node *node, bool siblings) {
if (siblings && next) if (siblings && next)
tree_delete_node(tree, next, true); tree_delete_node(tree, next, true);
tree_recalculate_node_positions(tree->root); tree_recalculate_node_positions(tree, tree->root);
tree_redraw_area(tree, 0, 0, 16384, 16384); /* \todo correct area */ tree_redraw_area(tree, 0, 0, 16384, 16384); /* \todo correct area */
tree_recalculate_size(tree); tree_recalculate_size(tree);
} }
@ -964,7 +966,6 @@ struct node *tree_create_folder_node(struct node *parent, const char *title) {
tree_set_node_sprite_folder(node); tree_set_node_sprite_folder(node);
if (parent) if (parent)
tree_link_node(parent, node, false); tree_link_node(parent, node, false);
tree_recalculate_node(node, true);
return node; return node;
} }
@ -1061,7 +1062,6 @@ struct node *tree_create_URL_node(struct node *parent,
element->text = strdup(url); element->text = strdup(url);
tree_update_URL_node(node, url, NULL); tree_update_URL_node(node, url, NULL);
tree_recalculate_node(node, false);
return node; return node;
} }
@ -1104,7 +1104,6 @@ struct node *tree_create_URL_node_shared(struct node *parent,
element->text = url; element->text = url;
tree_update_URL_node(node, url, data); tree_update_URL_node(node, url, data);
tree_recalculate_node(node, false);
return node; return node;
} }
@ -1113,8 +1112,8 @@ struct node *tree_create_URL_node_shared(struct node *parent,
/** /**
* Creates a tree entry for a cookie, and links it into the tree. * Creates a tree entry for a cookie, and links it into the tree.
* *
* All information is used directly from the cookie_data, and as such cannot * All information is copied from the cookie_data, and as such can
* be edited and should never be freed. * be edited and should be freed.
* *
* \param parent the node to link to * \param parent the node to link to
* \param url the URL * \param url the URL
@ -1201,10 +1200,7 @@ struct node *tree_create_cookie_node(struct node *parent,
element->text = strdup(buffer); element->text = strdup(buffer);
} }
/* add version, last_used, expires,
* path, domain, comment, value */
tree_set_node_sprite(node, "small_xxx", "small_xxx"); tree_set_node_sprite(node, "small_xxx", "small_xxx");
tree_recalculate_node(node, false);
return node; return node;
} }

View File

@ -96,6 +96,9 @@ struct tree {
int height; /* <-- Tree height */ int height; /* <-- Tree height */
int window_width; /* <-- Tree window width */ int window_width; /* <-- Tree window width */
int window_height; /* <-- Tree window height */ int window_height; /* <-- Tree window height */
bool no_vscroll; /* <-- Tree has a vertical scroll only when needed */
bool no_furniture; /* <-- Tree does not have connecting lines */
bool single_selection; /* <-- There can only be one item selected */
int edit_handle; /* <-- Handle for editing information */ int edit_handle; /* <-- Handle for editing information */
uintptr_t textarea_handle; /* <-- Handle for UTF-8 textarea */ uintptr_t textarea_handle; /* <-- Handle for UTF-8 textarea */
bool movable; /* <-- Whether nodes can be moved */ bool movable; /* <-- Whether nodes can be moved */
@ -107,13 +110,13 @@ struct tree {
/* Non-platform specific code */ /* Non-platform specific code */
void tree_initialise(struct tree *tree); void tree_initialise(struct tree *tree);
void tree_initialise_nodes(struct node *root); void tree_initialise_nodes(struct tree *tree, struct node *root);
void tree_handle_node_changed(struct tree *tree, struct node *node, void tree_handle_node_changed(struct tree *tree, struct node *node,
bool recalculate_sizes, bool expansion); bool recalculate_sizes, bool expansion);
void tree_handle_node_element_changed(struct tree *tree, void tree_handle_node_element_changed(struct tree *tree,
struct node_element *element); struct node_element *element);
void tree_recalculate_node(struct node *node, bool recalculate_sizes); void tree_recalculate_node(struct tree *tree, struct node *node, bool recalculate_sizes);
void tree_recalculate_node_positions(struct node *root); void tree_recalculate_node_positions(struct tree *tree, struct node *root);
struct node *tree_get_node_at(struct node *root, int x, int y, bool *furniture); struct node *tree_get_node_at(struct node *root, int x, int y, bool *furniture);
struct node_element *tree_get_node_element_at(struct node *node, int x, int y, struct node_element *tree_get_node_element_at(struct node *node, int x, int y,
bool *furniture); bool *furniture);
@ -135,7 +138,7 @@ struct node *tree_create_URL_node_shared(struct node *parent,
const char *url, const struct url_data *data); const char *url, const struct url_data *data);
struct node *tree_create_cookie_node(struct node *parent, struct node *tree_create_cookie_node(struct node *parent,
const struct cookie_data *data); const struct cookie_data *data);
void tree_set_node_expanded(struct node *node, bool expanded); void tree_set_node_expanded(struct tree *tree, struct node *node, bool expanded);
void tree_set_node_selected(struct tree *tree, struct node *node, void tree_set_node_selected(struct tree *tree, struct node *node,
bool selected); bool selected);
void tree_handle_selection_area(struct tree *tree, int x, int y, int width, void tree_handle_selection_area(struct tree *tree, int x, int y, int width,

View File

@ -314,7 +314,7 @@ bool global_history_add_internal(const char *url,
tree_link_node(link, parent, before); tree_link_node(link, parent, before);
if (!global_history_init) { if (!global_history_init) {
tree_recalculate_node_positions( tree_recalculate_node_positions(global_history_tree,
global_history_tree->root); global_history_tree->root);
tree_redraw_area(global_history_tree, tree_redraw_area(global_history_tree,
0, 0, 16384, 16384); 0, 0, 16384, 16384);

View File

@ -344,6 +344,7 @@ void tree_draw_node_element(struct tree *tree, struct node_element *element) {
0xffffff, 0xffffff,
false, false, false, false, false, false,
IMAGE_PLOT_TINCT_OPAQUE); IMAGE_PLOT_TINCT_OPAQUE);
if (!tree->no_furniture) {
tree_draw_line(element->box.x, tree_draw_line(element->box.x,
element->box.y, element->box.y,
element->box.width - 1, element->box.width - 1,
@ -361,6 +362,7 @@ void tree_draw_node_element(struct tree *tree, struct node_element *element) {
0, 0,
element->box.height - 3); element->box.height - 3);
} }
}
break; break;
} }
} }
@ -716,6 +718,10 @@ bool ro_gui_tree_click(wimp_pointer *pointer, struct tree *tree) {
/* no item either means cancel selection on (select) click or a drag */ /* no item either means cancel selection on (select) click or a drag */
if (!element) { if (!element) {
if (tree->single_selection) {
tree_set_node_selected(tree, tree->root->child, false);
return true;
}
if ((pointer->buttons == (wimp_CLICK_SELECT << 4)) || if ((pointer->buttons == (wimp_CLICK_SELECT << 4)) ||
(pointer->buttons == (wimp_CLICK_SELECT << 8))) (pointer->buttons == (wimp_CLICK_SELECT << 8)))
tree_set_node_selected(tree, tree->root->child, false); tree_set_node_selected(tree, tree->root->child, false);
@ -824,7 +830,9 @@ bool ro_gui_tree_click(wimp_pointer *pointer, struct tree *tree) {
} }
/* single click (select) cancels current selection and selects item */ /* single click (select) cancels current selection and selects item */
if (pointer->buttons == (wimp_CLICK_SELECT << 8)) { if ((pointer->buttons == (wimp_CLICK_SELECT << 8)) ||
((pointer->buttons == (wimp_CLICK_ADJUST << 8)) &&
(tree->single_selection))) {
if (!node->selected) { if (!node->selected) {
tree_set_node_selected(tree, tree->root->child, false); tree_set_node_selected(tree, tree->root->child, false);
node->selected = true; node->selected = true;
@ -843,6 +851,8 @@ bool ro_gui_tree_click(wimp_pointer *pointer, struct tree *tree) {
/* drag starts a drag operation */ /* drag starts a drag operation */
if ((!tree->editing) && ((pointer->buttons == (wimp_CLICK_SELECT << 4)) || if ((!tree->editing) && ((pointer->buttons == (wimp_CLICK_SELECT << 4)) ||
(pointer->buttons == (wimp_CLICK_ADJUST << 4)))) { (pointer->buttons == (wimp_CLICK_ADJUST << 4)))) {
if (tree->single_selection)
return true;
if (!node->selected) { if (!node->selected) {
node->selected = true; node->selected = true;
@ -1159,6 +1169,7 @@ void ro_gui_tree_open(wimp_open *open) {
int width; int width;
int height; int height;
int toolbar_height = 0; int toolbar_height = 0;
bool vscroll;
tree = (struct tree *)ro_gui_wimp_event_get_user_data(open->w); tree = (struct tree *)ro_gui_wimp_event_get_user_data(open->w);
@ -1182,6 +1193,21 @@ void ro_gui_tree_open(wimp_open *open) {
error->errnum, error->errmess)); error->errnum, error->errmess));
warn_user("WimpError", error->errmess); warn_user("WimpError", error->errmess);
} }
/* hide the scroll bar? */
if ((tree->no_vscroll) && (height != tree->window_height)) {
vscroll = (tree->height > height);
if (ro_gui_wimp_check_window_furniture(open->w,
wimp_WINDOW_VSCROLL) != vscroll) {
ro_gui_wimp_update_window_furniture(open->w,
0, wimp_WINDOW_VSCROLL);
if (vscroll)
open->visible.x1 -= ro_get_vscroll_width(open->w);
else
open->visible.x1 += ro_get_vscroll_width(open->w);
}
}
tree->window_width = width; tree->window_width = width;
tree->window_height = height; tree->window_height = height;
} }

View File

@ -868,11 +868,13 @@ void ro_gui_user_redraw(wimp_draw *redraw, bool user_fill,
void ro_gui_wimp_update_window_furniture(wimp_w w, wimp_window_flags bic_mask, void ro_gui_wimp_update_window_furniture(wimp_w w, wimp_window_flags bic_mask,
wimp_window_flags xor_mask) { wimp_window_flags xor_mask) {
wimp_window_state state; wimp_window_state state;
wimp_w parent;
bits linkage;
os_error *error; os_error *error;
bool open; bool open;
state.w = w; state.w = w;
error = xwimp_get_window_state(&state); error = xwimp_get_window_state_and_nesting(&state, &parent, &linkage);
if (error) { if (error) {
LOG(("xwimp_get_window_state: 0x%x: %s", LOG(("xwimp_get_window_state: 0x%x: %s",
error->errnum, error->errmess)); error->errnum, error->errmess));
@ -886,7 +888,7 @@ void ro_gui_wimp_update_window_furniture(wimp_w w, wimp_window_flags bic_mask,
state.flags ^= xor_mask; state.flags ^= xor_mask;
if (!open) if (!open)
state.next = wimp_HIDDEN; state.next = wimp_HIDDEN;
error = xwimp_open_window_nested_with_flags(&state, (wimp_w)-1, 0); error = xwimp_open_window_nested_with_flags(&state, parent, linkage);
if (error) { if (error) {
LOG(("xwimp_open_window: 0x%x: %s", LOG(("xwimp_open_window: 0x%x: %s",
error->errnum, error->errmess)); error->errnum, error->errmess));