summaryrefslogtreecommitdiff
path: root/desktop/tree.c
blob: ae4821e3c0fd4a239fa79463c81b3a56111ee26b (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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
/*
 * This file is part of NetSurf, http://netsurf.sourceforge.net/
 * Licensed under the GNU General Public License,
 *		  http://www.opensource.org/licenses/gpl-license
 * Copyright 2004 Richard Wilson <not_ginger_matt@users.sourceforge.net>
 */

/** \file
 * Generic tree handling (implementation).
 */

#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "netsurf/content/urldb.h"
#include "netsurf/desktop/tree.h"
#include "netsurf/desktop/options.h"
#include "netsurf/utils/log.h"
#include "netsurf/utils/utils.h"

static void tree_draw_node(struct tree *tree, struct node *node, int clip_x,
		int clip_y, int clip_width, int clip_height);
static struct node_element *tree_create_node_element(struct node *parent,
		node_element_data data);
static int tree_get_node_width(struct node *node);
static int tree_get_node_height(struct node *node);
static void tree_handle_selection_area_node(struct tree *tree,
		struct node *node, int x, int y, int width, int height,
		bool invert);
static void tree_selected_to_processing(struct node *node);
void tree_clear_processing(struct node *node);
struct node *tree_move_processing_node(struct node *node, struct node *link,
		bool before, bool first);

static int tree_initialising = 0;


/**
 * Initialises a user-created tree
 *
 * \param tree  the tree to initialise
 */
void tree_initialise(struct tree *tree) {

	assert(tree);

	tree_set_node_expanded(tree->root, true);
	tree_initialise_nodes(tree->root);
	tree_recalculate_node_positions(tree->root);
	tree_set_node_expanded(tree->root, false);
	tree->root->expanded = true;
	tree_recalculate_node_positions(tree->root);
	tree_recalculate_size(tree);
}


/**
 * Initialises a user-created node structure
 *
 * \param root  the root node to update from
 */
void tree_initialise_nodes(struct node *root) {
	struct node *node;

	assert(root);

	tree_initialising++;
	for (node = root; node; node = node->next) {
		tree_recalculate_node(node, true);
		if (node->child) {
			tree_initialise_nodes(node->child);
		}
	}
	tree_initialising--;

	if (tree_initialising == 0)
		tree_recalculate_node_positions(root);
}


/**
 * Recalculate the node data and redraw the relevant section of the tree.
 *
 * \param tree		     the tree to redraw
 * \param node		     the node to update
 * \param recalculate_sizes  whether the elements have changed
 * \param expansion	     the request is the result of a node expansion
 */
void tree_handle_node_changed(struct tree *tree, struct node *node,
		bool recalculate_sizes, bool expansion) {
	int width, height;

	assert(node);

	if ((expansion) && (node->expanded) && (node->child)) {
		tree_set_node_expanded(node->child, false);
		tree_set_node_selected(tree, node->child, false);
	}

	width = node->box.width;
	height = node->box.height;
	if ((recalculate_sizes) || (expansion))
		tree_recalculate_node(node, true);
	if ((node->box.height != height) || (expansion)) {
		tree_recalculate_node_positions(tree->root);
		tree_redraw_area(tree, 0, node->box.y, 16384, 16384);
	} else {
		width = (width > node->box.width) ? width : node->box.width;
		tree_redraw_area(tree, node->box.x, node->box.y, width, node->box.height);
	}
	if ((recalculate_sizes) || (expansion))
		tree_recalculate_size(tree);
}


/**
 * Recalculate the node element and redraw the relevant section of the tree.
 * The tree size is not updated.
 *
 * \param tree	   the tree to redraw
 * \param element  the node element to update
 */
void tree_handle_node_element_changed(struct tree *tree, struct node_element *element) {
	int width, height;

	assert(element);

	width = element->box.width;
	height = element->box.height;
	tree_recalculate_node_element(element);

	if (element->box.height != height) {
		tree_recalculate_node(element->parent, false);
		tree_redraw_area(tree, 0, element->box.y, 16384, 16384);
	} else {
		if (element->box.width != width)
			tree_recalculate_node(element->parent, false);
		width = (width > element->box.width) ? width :
				element->box.width;
		tree_redraw_area(tree, element->box.x, element->box.y, width, element->box.height);
	}
}


/**
 * Recalculates the size of a node.
 *
 * \param node		     the node to update
 * \param recalculate_sizes  whether the node elements have changed
 */
void tree_recalculate_node(struct node *node, bool recalculate_sizes) {
	struct node_element *element;
	int width, height;

	assert(node);

	width = node->box.width;
	height = node->box.height;
	node->box.width = 0;
	node->box.height = 0;
	if (node->expanded) {
		for (element = &node->data; element; element = element->next) {
			if (recalculate_sizes)
				tree_recalculate_node_element(element);
			node->box.width = (node->box.width >
				element->box.x + element->box.width - node->box.x) ?
				node->box.width :
				element->box.width + element->box.x - node->box.x;
			node->box.height += element->box.height;
		}
	} else {
		if (recalculate_sizes)
			for (element = &node->data; element; element = element->next)
				tree_recalculate_node_element(element);
		else
			tree_recalculate_node_element(&node->data);
		node->box.width = node->data.box.width;
		node->box.height = node->data.box.height;
	}

	if (height != node->box.height) {
		for (; node->parent; node = node->parent);
		if (tree_initialising == 0)
			tree_recalculate_node_positions(node);
	}
}


/**
 * Recalculates the position of a node, its siblings and children.
 *
 * \param root  the root node to update from
 */
void tree_recalculate_node_positions(struct node *root) {
	struct node *parent;
	struct node *node;
	struct node *child;
	struct node_element *element;
	int y;

	for (node = root; node; node = node->next) {
		if (node->previous) {
			node->box.x = node->previous->box.x;
			node->box.y = node->previous->box.y +
					tree_get_node_height(node->previous);
		} else if ((parent = node->parent)) {
			node->box.x = parent->box.x + NODE_INSTEP;
			node->box.y = parent->box.y +
					parent->box.height;
			for (child = parent->child; child != node;
					child = child->next)
				node->box.y += child->box.height;
		} else {
			node->box.x = 0;
			node->box.y = -40;
		}
		if (node->expanded) {
			if (node->folder) {
				node->data.box.x = node->box.x;
				node->data.box.y = node->box.y;
				tree_recalculate_node_positions(node->child);
			} else {
				y = node->box.y;
				for (element = &node->data; element;
						element = element->next) {
					if (element->type == NODE_ELEMENT_TEXT_PLUS_SPRITE) {
						element->box.x = node->box.x;
					} else {
						element->box.x = node->box.x + NODE_INSTEP;
					}
					element->box.y = y;
					y += element->box.height;
				}
			}
		} else {
			node->data.box.x = node->box.x;
			node->data.box.y = node->box.y;
		}
	}
}


/**
 * Calculates the width of a node including any children
 *
 * \param node  the node to calculate the height of
 * \return the total width of the node and children
 */
int tree_get_node_width(struct node *node) {
	int width = 0;
	int child_width;

	assert(node);

	for (; node; node = node->next) {
		if (width < (node->box.x + node->box.width))
			width = node->box.x + node->box.width;
		if ((node->child) && (node->expanded)) {
			child_width = tree_get_node_width(node->child);
			if (width < child_width)
				width = child_width;
		}
	}
	return width;
}


/**
 * Calculates the height of a node including any children
 *
 * \param node  the node to calculate the height of
 * \return the total height of the node and children
 */
int tree_get_node_height(struct node *node) {
	int y1;

	assert(node);

	if ((node->child) && (node->expanded)) {
		y1 = node->box.y;
		if (y1 < 0)
			y1 = 0;
		node = node->child;
		while ((node->next) || ((node->child) && (node->expanded))) {
			for (; node->next; node = node->next);
			if ((node->child) && (node->expanded))
				node = node->child;
		}
		return node->box.y + node->box.height - y1;
	} else {
		return node->box.height;
	}
}


/**
 * Updates all siblinds and descendants of a node to an expansion state.
 * No update is performed for the tree changes.
 *
 * \param node	    the node to set all siblings and descendants of
 * \param expanded  the expansion state to set
 */
void tree_set_node_expanded(struct node *node, bool expanded) {
	for (; node; node = node->next) {
		if (node->expanded != expanded) {
			node->expanded = expanded;
			tree_recalculate_node(node, false);
		}
		if ((node->child) && (node->expanded))
			tree_set_node_expanded(node->child, expanded);
	}
}


/**
 * Updates all siblinds and descendants of a node to an expansion state.
 *
 * \param tree	    the tree to update
 * \param node	    the node to set all siblings and descendants of
 * \param expanded  the expansion state to set
 * \param folder    whether to update folders
 * \param leaf	    whether to update leaves
 * \return whether any changes were made
 */
bool tree_handle_expansion(struct tree *tree, struct node *node, bool expanded, bool folder,
		bool leaf) {
	struct node *entry = node;
	bool redraw = false;

	for (; node; node = node->next) {
		if ((node->expanded != expanded) && (node != tree->root) &&
				((folder && (node->folder)) || (leaf && (!node->folder)))) {
			node->expanded = expanded;
			if (node->child)
				tree_set_node_expanded(node->child, false);
			if ((node->data.next) && (node->data.next->box.height == 0))
				tree_recalculate_node(node, true);
			else
				tree_recalculate_node(node, false);
			redraw = true;
		}
		if ((node->child) && (node->expanded))
			redraw |= tree_handle_expansion(tree, node->child, expanded, folder, leaf);
	}
	if ((entry == tree->root) && (redraw)) {
		tree_recalculate_node_positions(tree->root);
		tree_redraw_area(tree, 0, 0, 16384, 16384);
		tree_recalculate_size(tree);
	}
	return redraw;
}


/**
 * Updates all siblinds and descendants of a node to an selected state.
 * The required areas of the tree are redrawn.
 *
 * \param tree	    the tree to update nodes for
 * \param node	    the node to set all siblings and descendants of
 * \param selected  the selection state to set
 */
void tree_set_node_selected(struct tree *tree, struct node *node, bool selected) {
	for (; node; node = node->next) {
		if ((node->selected != selected) && (node != tree->root)) {
			node->selected = selected;
			tree_redraw_area(tree, node->box.x, node->box.y, node->box.width,
					node->data.box.height);
		}
		if ((node->child) && (node->expanded))
			tree_set_node_selected(tree, node->child, selected);
	}
}


/**
 * Finds a node at a specific location.
 *
 * \param root	     the root node to check from
 * \param x	     the x co-ordinate
 * \param y	     the y co-ordinate
 * \param furniture  whether the returned area was in an elements furniture
 * \return the node at the specified position, or NULL for none
 */
struct node *tree_get_node_at(struct node *root, int x, int y, bool *furniture) {
	struct node_element *result;

	if ((result = tree_get_node_element_at(root, x, y, furniture)))
		return result->parent;
	return NULL;
}


/**
 * Finds a node element at a specific location.
 *
 * \param node	     the root node to check from
 * \param x	     the x co-ordinate
 * \param y	     the y co-ordinate
 * \param furniture  whether the returned area was in an elements furniture
 * \return the node at the specified position, or NULL for none
 */
struct node_element *tree_get_node_element_at(struct node *node, int x, int y,
		bool *furniture) {
	struct node_element *element;

	*furniture = false;
	for (; node; node = node->next) {
		if (node->box.y > y) return NULL;
		if ((node->box.x - NODE_INSTEP < x) && (node->box.y < y) &&
				(node->box.x + node->box.width >= x) &&
				(node->box.y + node->box.height >= y)) {
			if (node->expanded) {
				for (element = &node->data; element;
						element = element->next) {
					if ((element->box.x < x) && (element->box.y < y) &&
							(element->box.x + element->box.width >= x) &&
							(element->box.y + element->box.height >= y))
						return element;
				}
			} else if ((node->data.box.x < x) &&
					(node->data.box.y < y) &&
					(node->data.box.x + node->data.box.width >= x) &&
					(node->data.box.y + node->data.box.height >= y))
				return &node->data;
			if (((node->child) || (node->data.next)) &&
					(node->data.box.x - NODE_INSTEP + 8 < x) &&
					(node->data.box.y + 8 < y) &&
					(node->data.box.x > x) &&
					(node->data.box.y + 32 > y)) {
				*furniture = true;
				return &node->data;
			}
		}

		if ((node->child) && (node->expanded) &&
				((element = tree_get_node_element_at(node->child, x, y,
				furniture))))
			return element;
	}
	return NULL;
}


/**
 * Finds a node element from a node with a specific user_type
 *
 * \param node	     the node to examine
 * \param user_type  the user_type to check for
 * \return the corresponding element
 */
struct node_element *tree_find_element(struct node *node, node_element_data data) {
	struct node_element *element;
	for (element = &node->data; element; element = element->next)
		if (element->data == data) return element;
	return NULL;
}


/**
 * Moves nodes within a tree.
 *
 * \param tree	  the tree to process
 * \param link	  the node to link before/as a child (folders) or before/after (link)
 * \param before  whether to link siblings before or after the supplied node
 */
void tree_move_selected_nodes(struct tree *tree, struct node *destination, bool before) {
	struct node *link;
	struct node *test;
	bool error;

	tree_clear_processing(tree->root);
	tree_selected_to_processing(tree->root);

	/* the destination node cannot be a child of any node with the processing flag set */
	error = destination->processing;
	for (test = destination; test; test = test->parent)
		error |= test->processing;
	if (error) {
		tree_clear_processing(tree->root);
		return;
	}
	if ((destination->folder) && (!destination->expanded) && (!before)) {
		destination->expanded = true;
		tree_handle_node_changed(tree, destination, false, true);
	}
	link = tree_move_processing_node(tree->root, destination, before, true);
	while (link)
		link = tree_move_processing_node(tree->root, link, false, false);

	tree_clear_processing(tree->root);
	tree_recalculate_node_positions(tree->root);
	tree_redraw_area(tree, 0, 0, 16384, 16384);
}


/**
 * Sets the processing flag to the selection state.
 *
 * \param node	  the node to process siblings and children of
 */
void tree_selected_to_processing(struct node *node) {
	for (; node; node = node->next) {
		node->processing = node->selected;
		if ((node->child) && (node->expanded))
			tree_selected_to_processing(node->child);
	}
}


/**
 * Clears the processing flag.
 *
 * \param node	  the node to process siblings and children of
 */
void tree_clear_processing(struct node *node) {
	for (; node; node = node->next) {
		node->processing = false;
		if (node->child)
			tree_clear_processing(node->child);
	}
}


/**
 * Moves the first node in a tree with the processing flag set.
 *
 * \param tree	  the node to move siblings/children of
 * \param link	  the node to link before/as a child (folders) or before/after (link)
 * \param before  whether to link siblings before or after the supplied node
 * \param first   whether to always link after the supplied node (ie not inside of folders)
 * \return the node moved
 */
struct node *tree_move_processing_node(struct node *node, struct node *link, bool before,
		bool first) {
	struct node *result;

	bool folder = link->folder;
	for (; node; node = node->next) {
	  	if (node->processing) {
	  		node->processing = false;
	  	  	tree_delink_node(node);
	  	  	if (!first)
	  	  		link->folder = false;
	  	  	tree_link_node(link, node, before);
	  	  	if (!first)
	  	  		link->folder = folder;
	  		return node;
	  	}
		if (node->child) {
		  	result = tree_move_processing_node(node->child, link, before, first);
		  	if (result)
		  		return result;
		}
	}
	return NULL;
}

/**
 * Checks whether a node, its siblings or any children are selected.
 *
 * \param node  the root node to check from
 */
bool tree_has_selection(struct node *node) {
	for (; node; node = node->next) {
		if (node->selected)
			return true;
		if ((node->child) && (node->expanded) &&
				(tree_has_selection(node->child)))
			return true;
	}
	return false;
}


/**
 * Updates the selected state for a region of nodes.
 *
 * \param tree	  the tree to update
 * \param x	  the minimum x of the selection rectangle
 * \param y	  the minimum y of the selection rectangle
 * \param width	  the width of the selection rectangle
 * \param height  the height of the selection rectangle
 * \param invert  whether to invert the selected state
 */
void tree_handle_selection_area(struct tree *tree, int x, int y, int width, int height,
		bool invert) {
	assert(tree);
	assert(tree->root);

	if (!tree->root->child) return;

	if (width < 0) {
		x += width;
		width =- width;
	}
	if (height < 0) {
		y += height;
		height =- height;
	}

	tree_handle_selection_area_node(tree, tree->root->child, x, y, width, height, invert);
}


/**
 * Updates the selected state for a region of nodes.
 *
 * \param tree	  the tree to update
 * \param node	  the node to update children and siblings of
 * \param x	  the minimum x of the selection rectangle
 * \param y	  the minimum y of the selection rectangle
 * \param width	  the width of the selection rectangle
 * \param height  the height of the selection rectangle
 * \param invert  whether to invert the selected state
 */
void tree_handle_selection_area_node(struct tree *tree, struct node *node, int x, int y,
		int width, int height, bool invert) {

	struct node_element *element;
	struct node *update;
	int x_max, y_max;

	assert(tree);
	assert(node);

	x_max = x + width;
	y_max = y + height;

	for (; node; node = node->next) {
		if (node->box.y > y_max) return;
		if ((node->box.x < x_max) && (node->box.y < y_max) &&
				(node->box.x + node->box.width + NODE_INSTEP >= x) &&
				(node->box.y + node->box.height >= y)) {
			update = NULL;
			if (node->expanded) {
				for (element = &node->data; element;
						element = element->next) {
					if ((element->box.x < x_max) && (element->box.y < y_max) &&
							(element->box.x + element->box.width >= x) &&
							(element->box.y + element->box.height >= y)) {
						update = element->parent;
						break;
					}
				}
			} else if ((node->data.box.x < x_max) &&
					(node->data.box.y < y_max) &&
					(node->data.box.x + node->data.box.width >= x) &&
					(node->data.box.y + node->data.box.height >= y))
				update = node->data.parent;
			if ((update) && (node != tree->root)) {
				if (invert) {
					node->selected = !node->selected;
					tree_handle_node_element_changed(tree, &node->data);
				} else if (!node->selected) {
					node->selected = true;
					tree_handle_node_element_changed(tree, &node->data);
				}
			}
		}
		if ((node->child) && (node->expanded))
			tree_handle_selection_area_node(tree, node->child, x, y, width, height,
					invert);
	}
}


/**
 * Redraws a tree.
 *
 * \param tree	       the tree to draw
 * \param clip_x       the minimum x of the clipping rectangle
 * \param clip_y       the minimum y of the clipping rectangle
 * \param clip_width   the width of the clipping rectangle
 * \param clip_height  the height of the clipping rectangle
 */
void tree_draw(struct tree *tree, int clip_x, int clip_y, int clip_width,
		int clip_height) {
	assert(tree);
	assert(tree->root);

	if (!tree->root->child) return;

	tree_initialise_redraw(tree);
	tree_draw_node(tree, tree->root->child, clip_x,
			clip_y, clip_width, clip_height);
}


/**
 * Redraws a node.
 *
 * \param tree	       the tree to draw
 * \param node	       the node to draw children and siblings of
 * \param clip_x       the minimum x of the clipping rectangle
 * \param clip_y       the minimum y of the clipping rectangle
 * \param clip_width   the width of the clipping rectangle
 * \param clip_height  the height of the clipping rectangle
 */
void tree_draw_node(struct tree *tree, struct node *node, int clip_x, int clip_y,
		int clip_width, int clip_height) {

	struct node_element *element;
	int x_max, y_max;

	assert(tree);
	assert(node);

	x_max = clip_x + clip_width + NODE_INSTEP;
	y_max = clip_y + clip_height;

	if ((node->parent->next) && (node->parent->next->box.y < clip_y))
		return;

	for (; node; node = node->next) {
		if (node->box.y > y_max) return;
		if (node->next)
			tree_draw_line(node->box.x - (NODE_INSTEP / 2),
					node->box.y + (40 / 2), 0,
					node->next->box.y - node->box.y);
		if ((node->box.x < x_max) && (node->box.y < y_max) &&
				(node->box.x + node->box.width + NODE_INSTEP >= clip_x) &&
				(node->box.y + node->box.height >= clip_y)) {
			if ((node->expanded) && (node->child))
				tree_draw_line(node->box.x + (NODE_INSTEP / 2),
						node->data.box.y + node->data.box.height, 0,
						(40 / 2));
			tree_draw_line(node->box.x - (NODE_INSTEP / 2),
					node->data.box.y +
					node->data.box.height - (40 / 2),
					(NODE_INSTEP / 2) - 4, 0);
			tree_draw_node_expansion(tree, node);
			if (node->expanded)
				for (element = &node->data; element;
						element = element->next)
					tree_draw_node_element(tree, element);
			else
				tree_draw_node_element(tree, &node->data);
		}
		if ((node->child) && (node->expanded))
			tree_draw_node(tree, node->child, clip_x, clip_y, clip_width,
					clip_height);
	}
}


/**
 * Gets link characteristics to insert a node at a specified position.
 *
 * \param tree	  the tree to find link information for
 * \param x	  the x co-ordinate
 * \param y	  the y co-ordinate
 * \param before  set to whether the node should be linked before on exit
 * \return the node to link with
 */
struct node *tree_get_link_details(struct tree *tree, int x, int y, bool *before) {
	struct node *node = NULL;
	bool furniture;

	assert(tree);
	assert(tree->root);

	*before = false;
	if (tree->root->child)
		node = tree_get_node_at(tree->root->child, x, y, &furniture);
	if ((!node) || (furniture))
		return tree->root;

	if (y < (node->box.y + (node->box.height / 2))) {
		*before = true;
	} else if ((node->folder) && (node->expanded) && (node->child)) {
		node = node->child;
		*before = true;
	}
	return node;
}


/**
 * Links a node into the tree.
 *
 * \param link	  the node to link before/as a child (folders) or before/after (link)
 * \param node	  the node to link
 * \param before  whether to link siblings before or after the supplied node
 */
void tree_link_node(struct node *link, struct node *node, bool before) {
	assert(link);
	assert(node);

	if ((!link->folder) || (before)) {
		node->parent = link->parent;
		if (before) {
			node->next = link;
			node->previous = link->previous;
			if (link->previous) link->previous->next = node;
			link->previous = node;
			if ((link->parent) && (link->parent->child == link))
				link->parent->child = node;
		} else {
			node->previous = link;
			node->next = link->next;
			if (link->next) link->next->previous = node;
			link->next = node;
		}
	} else {
		if (!link->child) {
			link->child = link->last_child = node;
			node->previous = NULL;
		} else {
			link->last_child->next = node;
			node->previous = link->last_child;
			link->last_child = node;
		}
		node->parent = link;
		node->next = NULL;
	}
	node->deleted = false;
}


/**
 * Delinks a node from the tree.
 *
 * \param node  the node to delink
 */
void tree_delink_node(struct node *node) {
	assert(node);

	if (node->parent) {
		if (node->parent->child == node)
			node->parent->child = node->next;
		if (node->parent->last_child == node)
			node->parent->last_child = node->previous;
		if (node->parent->child == NULL)
			node->parent->expanded = false;
		node->parent = NULL;
	}
	if (node->previous)
		node->previous->next = node->next;
	if (node->next)
		node->next->previous = node->previous;
	node->previous = NULL;
	node->next = NULL;
}


/**
 * Deletes all selected node from the tree.
 *
 * \param tree  the tree to delete from
 * \param node  the node to delete
 */
void tree_delete_selected_nodes(struct tree *tree, struct node *node) {
	struct node *next;

	while (node) {
		next = node->next;
		if ((node->selected) && (node != tree->root))
			tree_delete_node(tree, node, false);
		else if (node->child)
			tree_delete_selected_nodes(tree, node->child);
		node = next;
	}
}


/**
 * Deletes a node from the tree.
 *
 * \param tree	    the tree to delete from
 * \param node	    the node to delete
 * \param siblings  whether to delete all siblings
 */
void tree_delete_node(struct tree *tree, struct node *node, bool siblings) {
	struct node *next;
	struct node *parent;
	struct node_element *e, *f;

	assert(node);

	if (tree->temp_selection == node)
		tree->temp_selection = NULL;

	next = node->next;
	if (node->child)
		tree_delete_node(tree, node->child, true);
	node->child = NULL;
	parent = node->parent;
	tree_delink_node(node);

	if (!node->retain_in_memory) {
		for (e = &node->data; e; e = f) {
			f = e->next;

			if (e->text) {
				/* we don't free non-editable titles */
				if (node->editable)
					free(e->text);
				else {
					if (e->data == TREE_ELEMENT_URL) {
						/* reset URL characteristics */
						urldb_reset_url_visit_data(e->text);
					}

					if (e->data != TREE_ELEMENT_TITLE)
						free(e->text);
				}
			}
			if (e->sprite)
				free(e->sprite);	/* \todo platform specific bits */

			if (e != &node->data)
				free(e);
		}
		free(node);
	} else {
		node->deleted = true;
	}
	if (siblings)
		tree_delete_node(tree, next, true);

	tree_recalculate_node_positions(tree->root);
	tree_redraw_area(tree, 0, 0, 16384, 16384);	/* \todo correct area */
	tree_recalculate_size(tree);
}


/**
 * Creates a folder node with the specified title, and links it into the tree.
 *
 * \param parent  the parent node, or NULL not to link
 * \param title	  the node title (copied)
 * \return the newly created node.
 */
struct node *tree_create_folder_node(struct node *parent, const char *title) {
	struct node *node;

	assert(title);

	node = calloc(sizeof(struct node), 1);
	if (!node) return NULL;
	node->editable = true;
	node->folder = true;
	node->data.parent = node;
	node->data.type = NODE_ELEMENT_TEXT;
	node->data.text = squash_whitespace(title);
	node->data.data = TREE_ELEMENT_TITLE;
	tree_set_node_sprite_folder(node);
	if (parent)
		tree_link_node(parent, node, false);
	tree_recalculate_node(node, true);
	return node;
}


/**
 * Creates a leaf node with the specified title, and links it into the tree.
 *
 * \param parent  the parent node, or NULL not to link
 * \param title	  the node title (copied)
 * \return the newly created node.
 */
struct node *tree_create_leaf_node(struct node *parent, const char *title) {
	struct node *node;

	assert(title);

	node = calloc(sizeof(struct node), 1);
	if (!node) return NULL;
	node->folder = false;
	node->data.parent = node;
	node->data.type = NODE_ELEMENT_TEXT;
	node->data.text = squash_whitespace(title);
	node->data.data = TREE_ELEMENT_TITLE;
	if (parent)
		tree_link_node(parent, node, false);
	return node;
}


/**
 * Creates a tree entry for a URL, and links it into the tree
 *
 *
 * \param parent     the node to link to
 * \param url        the URL (copied)
 * \param data	     the URL data to use
 * \param title	     the custom title to use
 * \return the node created, or NULL for failure
 */
struct node *tree_create_URL_node(struct node *parent,
		const char *url, const struct url_data *data,
		const char *title) {
	struct node *node;
	struct node_element *element;

	assert(data);

	if (!title) {
		if (data->title)
			title = strdup(data->title);
		else
			title = strdup(url);
		if (!title)
			return NULL;
	}
	node = tree_create_leaf_node(parent, title);
	if (!node)
		return NULL;
	node->editable = true;

	element = tree_create_node_element(node, TREE_ELEMENT_THUMBNAIL);
	if (element)
		element->type = NODE_ELEMENT_THUMBNAIL;
	tree_create_node_element(node, TREE_ELEMENT_VISITS);
	tree_create_node_element(node, TREE_ELEMENT_LAST_VISIT);
	element = tree_create_node_element(node, TREE_ELEMENT_URL);
	if (element)
		element->text = strdup(url);

	tree_update_URL_node(node, url, NULL);
	tree_recalculate_node(node, false);

	return node;
}


/**
 * Creates a tree entry for a URL, and links it into the tree.
 *
 * All information is used directly from the url_data, and as such cannot be
 * edited and should never be freed.
 *
 * \param parent      the node to link to
 * \param url         the URL (copied)
 * \param data	      the URL data to use
 * \return the node created, or NULL for failure
 */
struct node *tree_create_URL_node_shared(struct node *parent,
		const char *url, const struct url_data *data) {
	struct node *node;
	struct node_element *element;
	char *title;

	assert(data);

	/* If title isn't set, set it to the URL */
	if (!data->title)
		urldb_set_url_title(url, url);

	if (data->title)
		title = data->title;
	else
		return NULL;
	node = tree_create_leaf_node(parent, title);
	if (!node)
		return NULL;
	free(node->data.text);
	node->data.text = title;
	node->editable = false;

	element = tree_create_node_element(node, TREE_ELEMENT_THUMBNAIL);
	if (element)
		element->type = NODE_ELEMENT_THUMBNAIL;
	tree_create_node_element(node, TREE_ELEMENT_VISITS);
	tree_create_node_element(node, TREE_ELEMENT_LAST_VISIT);
	element = tree_create_node_element(node, TREE_ELEMENT_URL);
	if (element)
		element->text = strdup(url);

	tree_update_URL_node(node, url, data);
	tree_recalculate_node(node, false);

	return node;
}


/**
 * Creates an empty text node element and links it to a node.
 *
 * \param parent     the parent node
 * \param user_type  the required user_type
 * \return the newly created element.
 */
struct node_element *tree_create_node_element(struct node *parent, node_element_data data) {
	struct node_element *element;

	element = calloc(sizeof(struct node_element), 1);
	if (!element) return NULL;
	element->parent = parent;
	element->data = data;
	element->type = NODE_ELEMENT_TEXT;
	element->next = parent->data.next;
	parent->data.next = element;
	return element;
}


/**
 * Recalculates the size of a tree.
 *
 * \param tree  the tree to recalculate
 */
void tree_recalculate_size(struct tree *tree) {
	int width, height;

	assert(tree);

	if (!tree->handle)
		return;
	width = tree->width;
	height = tree->height;
	tree->width = tree_get_node_width(tree->root);
	tree->height = tree_get_node_height(tree->root);
	if ((width != tree->width) || (height != tree->height))
		tree_resized(tree);
}


/**
 * Returns the selected node, or NULL if multiple nodes are selected.
 *
 * \param node  the node to search sibling and children
 * \return the selected node, or NULL if multiple nodes are selected
 */
struct node *tree_get_selected_node(struct node *node) {
	struct node *result = NULL;
	struct node *temp;

	for (; node; node = node->next) {
		if (node->selected) {
			if (result)
				return NULL;
			result = node;
		}
		if ((node->child) && (node->expanded)) {
			temp = tree_get_selected_node(node->child);
			if (temp) {
				if (result)
					return NULL;
				else
					result = temp;
			}
		}
	}
	return result;
}