1 | #include <gtk/gtk.h> |
---|
2 | #include "gtksourceview/gtktextregion.h" |
---|
3 | |
---|
4 | |
---|
5 | int |
---|
6 | main (int argc, char **argv) |
---|
7 | { |
---|
8 | GtkTextBuffer *buffer; |
---|
9 | GtkTextRegion *region, *intersection; |
---|
10 | GtkTextIter iter1, iter2; |
---|
11 | gint i; |
---|
12 | |
---|
13 | #define NUM_OPS 23 |
---|
14 | |
---|
15 | gint ops [NUM_OPS][3] = { |
---|
16 | /* add/remove a 0-length region */ |
---|
17 | { 1, 5, 5 }, |
---|
18 | { -1, 5, 5 }, |
---|
19 | /* add a region */ |
---|
20 | { 1, 5, 10 }, |
---|
21 | /* add two adjacent regions */ |
---|
22 | { 1, 3, 5 }, |
---|
23 | { 1, 10, 12 }, |
---|
24 | /* remove all */ |
---|
25 | { -1, 1, 15 }, |
---|
26 | /* add two separate regions */ |
---|
27 | { 1, 5, 10 }, |
---|
28 | { 1, 15, 20 }, |
---|
29 | /* join them */ |
---|
30 | { 1, 7, 17 }, |
---|
31 | /* remove from the middle */ |
---|
32 | { -1, 10, 15 }, |
---|
33 | /* exactly remove a subregion */ |
---|
34 | { -1, 15, 20 }, |
---|
35 | /* try to remove an adjacent region */ |
---|
36 | { -1, 10, 20 }, |
---|
37 | /* try to remove an adjacent region */ |
---|
38 | { -1, 0, 5 }, |
---|
39 | /* add another separate */ |
---|
40 | { 1, 15, 20 }, |
---|
41 | /* join with excess */ |
---|
42 | { 1, 0, 25 }, |
---|
43 | /* do two holes */ |
---|
44 | { -1, 5, 10 }, |
---|
45 | { -1, 15, 20 }, |
---|
46 | /* remove the middle subregion */ |
---|
47 | { -1, 8, 22 }, |
---|
48 | /* add the subregion we just removed */ |
---|
49 | { 1, 10, 15 }, |
---|
50 | /* remove the middle subregion */ |
---|
51 | { -1, 3, 17 }, |
---|
52 | /* add the subregion we just removed */ |
---|
53 | { 1, 10, 15 }, |
---|
54 | /* remove the middle subregion */ |
---|
55 | { -1, 2, 23 }, |
---|
56 | /* add the subregion we just removed */ |
---|
57 | { 1, 10, 15 }, |
---|
58 | }; |
---|
59 | |
---|
60 | #define NUM_INTERSECTS 5 |
---|
61 | |
---|
62 | gint inter [NUM_INTERSECTS][2] = { |
---|
63 | { 0, 25 }, |
---|
64 | { 10, 15 }, |
---|
65 | { 8, 17 }, |
---|
66 | { 1, 24 }, |
---|
67 | { 3, 7 } |
---|
68 | }; |
---|
69 | |
---|
70 | gtk_init (&argc, &argv); |
---|
71 | |
---|
72 | buffer = gtk_text_buffer_new (NULL); |
---|
73 | region = gtk_text_region_new (buffer); |
---|
74 | |
---|
75 | gtk_text_buffer_get_start_iter (buffer, &iter1); |
---|
76 | gtk_text_buffer_insert (buffer, &iter1, "This is a test of GtkTextRegion", -1); |
---|
77 | |
---|
78 | for (i = 0; i < NUM_OPS; i++) { |
---|
79 | gchar *op_name; |
---|
80 | |
---|
81 | gtk_text_buffer_get_iter_at_offset (buffer, &iter1, ops [i][1]); |
---|
82 | gtk_text_buffer_get_iter_at_offset (buffer, &iter2, ops [i][2]); |
---|
83 | |
---|
84 | if (ops [i][0] > 0) { |
---|
85 | op_name = "added"; |
---|
86 | gtk_text_region_add (region, &iter1, &iter2); |
---|
87 | } else { |
---|
88 | op_name = "deleted"; |
---|
89 | gtk_text_region_substract (region, &iter1, &iter2); |
---|
90 | } |
---|
91 | g_print ("%s %d-%d\n", op_name, ops [i][1], ops [i][2]); |
---|
92 | |
---|
93 | gtk_text_region_debug_print (region); |
---|
94 | } |
---|
95 | |
---|
96 | for (i = 0; i < NUM_INTERSECTS; i++) { |
---|
97 | gtk_text_buffer_get_iter_at_offset (buffer, &iter1, inter [i][0]); |
---|
98 | gtk_text_buffer_get_iter_at_offset (buffer, &iter2, inter [i][1]); |
---|
99 | |
---|
100 | g_print ("intersect %d-%d\n", inter [i][0], inter [i][1]); |
---|
101 | intersection = gtk_text_region_intersect (region, &iter1, &iter2); |
---|
102 | if (intersection) { |
---|
103 | gtk_text_region_debug_print (intersection); |
---|
104 | gtk_text_region_destroy (intersection, TRUE); |
---|
105 | } else { |
---|
106 | g_print ("no intersection\n"); |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | gtk_text_region_destroy (region, TRUE); |
---|
111 | g_object_unref (buffer); |
---|
112 | |
---|
113 | return 0; |
---|
114 | } |
---|
115 | |
---|