]> Pileus Git - grits/blob - src/plugins/test.c
Add parameters and return values to GritsObject events
[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 gboolean 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         return FALSE;
39 }
40
41 static gboolean on_poly_leave(GritsPoly *poly)
42 {
43         g_debug("GritsPluginTest: on_poly_leave");
44         poly->color[3] = 0.2;
45         grits_object_queue_draw(GRITS_OBJECT(poly));
46         return FALSE;
47 }
48
49 static gboolean on_poly_button(GritsPoly *poly, GdkEventButton *event)
50 {
51         g_debug("GritsPluginTest: on_poly_button");
52         static int i = 0;
53         gdouble colors[][3] = {
54                 {1, 0, 0}, {1, 1, 0},
55                 {0, 1, 0}, {0, 1, 1},
56                 {0, 0, 1}, {1, 0, 1},
57         };
58         int idx = i++ % G_N_ELEMENTS(colors);
59         memcpy(poly->color, colors[idx], sizeof(gdouble)*3);
60         grits_object_queue_draw(GRITS_OBJECT(poly));
61         return TRUE;
62 }
63
64 static gboolean on_poly_key(GritsPoly *poly, GdkEventKey *event)
65 {
66         g_debug("GritsPluginTest: on_poly_key - %d", event->keyval);
67         gdouble colors[0xff][3] = {
68                 [GDK_r] {1, 0, 0},
69                 [GDK_g] {0, 1, 0},
70                 [GDK_b] {0, 0, 1},
71         };
72         if (event->keyval >= G_N_ELEMENTS(colors))
73                 return FALSE;
74         int key = event->keyval;
75         memcpy(poly->color, colors[key], sizeof(gdouble)*3);
76         grits_object_queue_draw(GRITS_OBJECT(poly));
77         return TRUE;
78 }
79
80 static gboolean on_marker_enter(GritsMarker *marker, GdkEvent *event, GritsViewer *viewer)
81 {
82         g_debug("GritsPluginTest: on_marker_enter");
83         GdkWindow *window = gtk_widget_get_window(GTK_WIDGET(viewer));
84         GdkCursor *cursor = gdk_cursor_new(GDK_HAND2);
85         gdk_window_set_cursor(window, cursor);
86         return FALSE;
87 }
88
89 static gboolean on_marker_leave(GritsMarker *marker, GdkEvent *event, GritsViewer *viewer)
90 {
91         g_debug("GritsPluginTest: on_marker_leave");
92         GdkWindow *window = gtk_widget_get_window(GTK_WIDGET(viewer));
93         gdk_window_set_cursor(window, NULL);
94         return FALSE;
95 }
96
97 static gboolean on_marker_button(GritsMarker *marker, GdkEventButton *event)
98 {
99         g_debug("GritsPluginTest: on_marker_button");
100         GtkWidget *dialog = gtk_dialog_new_with_buttons(
101                         "St. Charles!", NULL, 0, GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
102         gtk_dialog_run(GTK_DIALOG(dialog));
103         return TRUE;
104 }
105
106 /***********
107  * Methods *
108  ***********/
109 void _load_marker(GritsPluginTest *test)
110 {
111         test->marker = grits_marker_new("St. Charles");
112         GRITS_OBJECT(test->marker)->center.lat  =  38.841847;
113         GRITS_OBJECT(test->marker)->center.lon  = -90.491982;
114         GRITS_OBJECT(test->marker)->center.elev =   0.0;
115         GRITS_OBJECT(test->marker)->lod         = EARTH_R*3;
116         grits_viewer_add(test->viewer, GRITS_OBJECT(test->marker), GRITS_LEVEL_HUD, FALSE);
117         g_signal_connect(test->marker, "enter",   G_CALLBACK(on_marker_enter),  test->viewer);
118         g_signal_connect(test->marker, "leave",   G_CALLBACK(on_marker_leave),  test->viewer);
119         g_signal_connect(test->marker, "clicked", G_CALLBACK(on_marker_button), test->viewer);
120 }
121
122 void _load_poly(GritsPluginTest *test)
123 {
124         test->poly = grits_poly_parse("35,-90 35,-110 45,-110 45,-90", "\t", " ", ",");
125         test->poly->color[0]  = test->poly->border[0] = 1;
126         test->poly->color[1]  = test->poly->border[1] = 0;
127         test->poly->color[2]  = test->poly->border[2] = 0;
128         test->poly->color[3]  = 0.2;
129         test->poly->border[3] = 1;
130         test->poly->width     = 6;
131         grits_viewer_add(test->viewer, GRITS_OBJECT(test->poly),  GRITS_LEVEL_OVERLAY, FALSE);
132         g_signal_connect(test->poly, "enter",     G_CALLBACK(on_poly_enter),  NULL);
133         g_signal_connect(test->poly, "leave",     G_CALLBACK(on_poly_leave),  NULL);
134         g_signal_connect(test->poly, "clicked",   G_CALLBACK(on_poly_button), NULL);
135         g_signal_connect(test->poly, "key-press", G_CALLBACK(on_poly_key),    NULL);
136 }
137
138 void _load_line(GritsPluginTest *test)
139 {
140         test->line = grits_line_parse("30,-80 30,-120 50,-120 50,-80", "\t", " ", ",");
141         test->line->color[0]  = 1;
142         test->line->color[1]  = 0;
143         test->line->color[2]  = 0;
144         test->line->color[3]  = 1;
145         test->line->width     = 8;
146         grits_viewer_add(test->viewer, GRITS_OBJECT(test->line),  GRITS_LEVEL_OVERLAY, FALSE);
147         g_signal_connect(test->line, "enter",        G_CALLBACK(on_poly_enter),  NULL);
148         g_signal_connect(test->line, "leave",        G_CALLBACK(on_poly_leave),  NULL);
149         g_signal_connect(test->line, "button-press", G_CALLBACK(on_poly_button), NULL);
150         g_signal_connect(test->line, "key-press",    G_CALLBACK(on_poly_key),    NULL);
151 }
152
153 /**
154  * grits_plugin_test_new:
155  * @viewer: the #GritsViewer to use for drawing
156  *
157  * Create a new instance of the testing plugin.
158  *
159  * Returns: the new #GritsPluginTest
160  */
161 GritsPluginTest *grits_plugin_test_new(GritsViewer *viewer)
162 {
163         g_debug("GritsPluginTest: new");
164         GritsPluginTest *test = g_object_new(GRITS_TYPE_PLUGIN_TEST, NULL);
165         test->viewer = g_object_ref(viewer);
166         _load_marker(test);
167         _load_poly(test);
168         _load_line(test);
169         return test;
170 }
171
172
173 /****************
174  * GObject code *
175  ****************/
176 /* Plugin init */
177 static void grits_plugin_test_plugin_init(GritsPluginInterface *iface);
178 G_DEFINE_TYPE_WITH_CODE(GritsPluginTest, grits_plugin_test, G_TYPE_OBJECT,
179                 G_IMPLEMENT_INTERFACE(GRITS_TYPE_PLUGIN,
180                         grits_plugin_test_plugin_init));
181 static void grits_plugin_test_plugin_init(GritsPluginInterface *iface)
182 {
183         g_debug("GritsPluginTest: plugin_init");
184         /* Add methods to the interface */
185 }
186 /* Class/Object init */
187 static void grits_plugin_test_init(GritsPluginTest *test)
188 {
189         g_debug("GritsPluginTest: init");
190 }
191 static void grits_plugin_test_dispose(GObject *_test)
192 {
193         g_debug("GritsPluginTest: dispose");
194         GritsPluginTest *test = GRITS_PLUGIN_TEST(_test);
195         if (test->viewer) {
196                 grits_viewer_remove(test->viewer, GRITS_OBJECT(test->marker));
197                 grits_viewer_remove(test->viewer, GRITS_OBJECT(test->poly));
198                 grits_viewer_remove(test->viewer, GRITS_OBJECT(test->line));
199                 g_object_unref(test->viewer);
200                 test->viewer = NULL;
201         }
202         G_OBJECT_CLASS(grits_plugin_test_parent_class)->dispose(_test);
203 }
204 static void grits_plugin_test_class_init(GritsPluginTestClass *klass)
205 {
206         g_debug("GritsPluginTest: class_init");
207         GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
208         gobject_class->dispose = grits_plugin_test_dispose;
209 }