]> Pileus Git - grits/blob - src/plugins/test.c
a83cd374a84cb0ee2d92e642dc1065e0c33d410f
[grits] / src / plugins / test.c
1 /*
2  * Copyright (C) 2009-2011 Andy Spencer <andy753421@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 /**
19  * SECTION:test
20  * @short_description: Testing plugin
21  *
22  * #GritsPluginTest is a testing plugin used during development and as an example
23  * for how to create a plugin.
24  */
25
26 #include <string.h>
27
28 #include <gdk/gdkkeysyms.h>
29 #include <grits.h>
30
31 #include "test.h"
32
33 static void on_poly_enter(GritsPoly *poly)
34 {
35         g_debug("GritsPluginTest: on_poly_enter");
36         poly->color[3] = 0.50;
37         grits_object_queue_draw(GRITS_OBJECT(poly));
38 }
39
40 static void on_poly_leave(GritsPoly *poly)
41 {
42         g_debug("GritsPluginTest: on_poly_leave");
43         poly->color[3] = 0.2;
44         grits_object_queue_draw(GRITS_OBJECT(poly));
45 }
46
47 static void on_poly_button(GritsPoly *poly, GdkEventButton *event)
48 {
49         g_debug("GritsPluginTest: on_poly_button");
50         static int i = 0;
51         gdouble colors[][3] = {
52                 {1, 0, 0}, {1, 1, 0},
53                 {0, 1, 0}, {0, 1, 1},
54                 {0, 0, 1}, {1, 0, 1},
55         };
56         int idx = i++ % G_N_ELEMENTS(colors);
57         memcpy(poly->color, colors[idx], sizeof(gdouble)*3);
58         grits_object_queue_draw(GRITS_OBJECT(poly));
59 }
60
61 static void on_poly_key(GritsPoly *poly, GdkEventKey *event)
62 {
63         g_debug("GritsPluginTest: on_poly_key - %d", event->keyval);
64         gdouble colors[0xff][3] = {
65                 [GDK_r] {1, 0, 0},
66                 [GDK_g] {0, 1, 0},
67                 [GDK_b] {0, 0, 1},
68         };
69         if (event->keyval >= G_N_ELEMENTS(colors))
70                 return;
71         int key = event->keyval;
72         memcpy(poly->color, colors[key], sizeof(gdouble)*3);
73         grits_object_queue_draw(GRITS_OBJECT(poly));
74 }
75
76 static void on_marker_enter(GritsMarker *marker, GritsViewer *viewer)
77 {
78         g_debug("GritsPluginTest: on_marker_enter");
79         GdkWindow *window = gtk_widget_get_window(GTK_WIDGET(viewer));
80         GdkCursor *cursor = gdk_cursor_new(GDK_HAND1);
81         gdk_window_set_cursor(window, cursor);
82 }
83
84 static void on_marker_leave(GritsMarker *marker, GritsViewer *viewer)
85 {
86         g_debug("GritsPluginTest: on_marker_leave");
87         GdkWindow *window = gtk_widget_get_window(GTK_WIDGET(viewer));
88         gdk_window_set_cursor(window, NULL);
89 }
90
91 static void on_marker_button(GritsMarker *marker, GdkEventButton *event)
92 {
93         g_debug("GritsPluginTest: on_marker_button");
94         GtkWidget *dialog = gtk_dialog_new_with_buttons(
95                         "St. Charles!", NULL, 0, GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
96         gtk_dialog_run(GTK_DIALOG(dialog));
97 }
98
99 /***********
100  * Methods *
101  ***********/
102 void _load_marker(GritsPluginTest *test)
103 {
104         test->marker = grits_marker_new("St. Charles");
105         GRITS_OBJECT(test->marker)->center.lat  =  38.841847;
106         GRITS_OBJECT(test->marker)->center.lon  = -90.491982;
107         GRITS_OBJECT(test->marker)->center.elev =   0.0;
108         GRITS_OBJECT(test->marker)->lod         = EARTH_R*3;
109         grits_viewer_add(test->viewer, GRITS_OBJECT(test->marker), GRITS_LEVEL_OVERLAY, FALSE);
110         /* These do not work on marker yet */
111         //g_signal_connect(test->marker, "enter",        G_CALLBACK(on_marker_enter),  NULL);
112         //g_signal_connect(test->marker, "leave",        G_CALLBACK(on_marker_leave),  NULL);
113         //g_signal_connect(test->marker, "button-press", G_CALLBACK(on_marker_button), NULL);
114         (void)on_marker_enter;
115         (void)on_marker_leave;
116         (void)on_marker_button;
117 }
118
119 void _load_poly(GritsPluginTest *test)
120 {
121         test->poly = grits_poly_parse("35,-90 35,-110 45,-110 45,-90", "\t", " ", ",");
122         test->poly->color[0]  = test->poly->border[0] = 1;
123         test->poly->color[1]  = test->poly->border[1] = 0;
124         test->poly->color[2]  = test->poly->border[2] = 0;
125         test->poly->color[3]  = 0.2;
126         test->poly->border[3] = 1;
127         test->poly->width     = 6;
128         grits_viewer_add(test->viewer, GRITS_OBJECT(test->poly),  GRITS_LEVEL_OVERLAY, FALSE);
129         g_signal_connect(test->poly, "enter",        G_CALLBACK(on_poly_enter),  NULL);
130         g_signal_connect(test->poly, "leave",        G_CALLBACK(on_poly_leave),  NULL);
131         g_signal_connect(test->poly, "button-press", G_CALLBACK(on_poly_button), NULL);
132         g_signal_connect(test->poly, "key-press",    G_CALLBACK(on_poly_key),    NULL);
133 }
134
135 void _load_line(GritsPluginTest *test)
136 {
137         test->line = grits_line_parse("30,-80 30,-120 50,-120 50,-80", "\t", " ", ",");
138         test->line->color[0]  = 1;
139         test->line->color[1]  = 0;
140         test->line->color[2]  = 0;
141         test->line->color[3]  = 1;
142         test->line->width     = 8;
143         grits_viewer_add(test->viewer, GRITS_OBJECT(test->line),  GRITS_LEVEL_OVERLAY, FALSE);
144         g_signal_connect(test->line, "enter",        G_CALLBACK(on_poly_enter),  NULL);
145         g_signal_connect(test->line, "leave",        G_CALLBACK(on_poly_leave),  NULL);
146         g_signal_connect(test->line, "button-press", G_CALLBACK(on_poly_button), NULL);
147         g_signal_connect(test->line, "key-press",    G_CALLBACK(on_poly_key),    NULL);
148 }
149
150 /**
151  * grits_plugin_test_new:
152  * @viewer: the #GritsViewer to use for drawing
153  *
154  * Create a new instance of the testing plugin.
155  *
156  * Returns: the new #GritsPluginTest
157  */
158 GritsPluginTest *grits_plugin_test_new(GritsViewer *viewer)
159 {
160         g_debug("GritsPluginTest: new");
161         GritsPluginTest *test = g_object_new(GRITS_TYPE_PLUGIN_TEST, NULL);
162         test->viewer = g_object_ref(viewer);
163         _load_marker(test);
164         _load_poly(test);
165         _load_line(test);
166         return test;
167 }
168
169
170 /****************
171  * GObject code *
172  ****************/
173 /* Plugin init */
174 static void grits_plugin_test_plugin_init(GritsPluginInterface *iface);
175 G_DEFINE_TYPE_WITH_CODE(GritsPluginTest, grits_plugin_test, G_TYPE_OBJECT,
176                 G_IMPLEMENT_INTERFACE(GRITS_TYPE_PLUGIN,
177                         grits_plugin_test_plugin_init));
178 static void grits_plugin_test_plugin_init(GritsPluginInterface *iface)
179 {
180         g_debug("GritsPluginTest: plugin_init");
181         /* Add methods to the interface */
182 }
183 /* Class/Object init */
184 static void grits_plugin_test_init(GritsPluginTest *test)
185 {
186         g_debug("GritsPluginTest: init");
187 }
188 static void grits_plugin_test_dispose(GObject *_test)
189 {
190         g_debug("GritsPluginTest: dispose");
191         GritsPluginTest *test = GRITS_PLUGIN_TEST(_test);
192         if (test->viewer) {
193                 grits_viewer_remove(test->viewer, GRITS_OBJECT(test->marker));
194                 grits_viewer_remove(test->viewer, GRITS_OBJECT(test->poly));
195                 grits_viewer_remove(test->viewer, GRITS_OBJECT(test->line));
196                 g_object_unref(test->viewer);
197                 test->viewer = NULL;
198         }
199         G_OBJECT_CLASS(grits_plugin_test_parent_class)->dispose(_test);
200 }
201 static void grits_plugin_test_class_init(GritsPluginTestClass *klass)
202 {
203         g_debug("GritsPluginTest: class_init");
204         GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
205         gobject_class->dispose = grits_plugin_test_dispose;
206 }