]> Pileus Git - grits/blobdiff - src/plugins/test.c
Add grits_object_destroy functions and fix memory leaks
[grits] / src / plugins / test.c
index 94995b01f7979623922b38ff8fb1bba8c80d51c4..8d12dc1172faec9fe1736cc3b919af511077c821 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
+ * Copyright (C) 2009-2011 Andy Spencer <andy753421@gmail.com>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <gtk/gtkgl.h>
-#include <GL/gl.h>
-#include <GL/glu.h>
+/**
+ * SECTION:test
+ * @short_description: Testing plugin
+ *
+ * #GritsPluginTest is a testing plugin used during development and as an example
+ * for how to create a plugin.
+ */
 
-#include <gis.h>
+#include <string.h>
+
+#include <gdk/gdkkeysyms.h>
+#include <grits.h>
 
 #include "test.h"
 
-/***********
- * Methods *
- ***********/
-GisPluginTest *gis_plugin_test_new(GisViewer *viewer)
+static gboolean on_poly_enter(GritsPoly *poly)
 {
-       g_debug("GisPluginTest: new");
-       GisPluginTest *self = g_object_new(GIS_TYPE_PLUGIN_TEST, NULL);
-       self->viewer = viewer;
-       return self;
+       g_debug("GritsPluginTest: on_poly_enter");
+       poly->color[3] = 0.50;
+       grits_object_queue_draw(GRITS_OBJECT(poly));
+       return FALSE;
 }
 
-static void gis_plugin_test_expose(GisPlugin *_self)
+static gboolean on_poly_leave(GritsPoly *poly)
 {
-       GisPluginTest *self = GIS_PLUGIN_TEST(_self);
-       g_debug("GisPluginTest: expose");
+       g_debug("GritsPluginTest: on_poly_leave");
+       poly->color[3] = 0.2;
+       grits_object_queue_draw(GRITS_OBJECT(poly));
+       return FALSE;
+}
 
-       double width  = GTK_WIDGET(self->viewer)->allocation.width;
-       double height = GTK_WIDGET(self->viewer)->allocation.height;
+static gboolean on_poly_button(GritsPoly *poly, GdkEventButton *event)
+{
+       g_debug("GritsPluginTest: on_poly_button");
+       static int i = 0;
+       gdouble colors[][3] = {
+               {1, 0, 0}, {1, 1, 0},
+               {0, 1, 0}, {0, 1, 1},
+               {0, 0, 1}, {1, 0, 1},
+       };
+       int idx = i++ % G_N_ELEMENTS(colors);
+       memcpy(poly->color, colors[idx], sizeof(gdouble)*3);
+       grits_object_queue_draw(GRITS_OBJECT(poly));
+       return TRUE;
+}
 
-       // St. Charles
-       // lat =  38.841847
-       // lon = -90.491982
-       gdouble px, py, pz;
-       gis_viewer_project(self->viewer,
-               38.841847, -90.491982, 0, &px, &py, &pz);
-       py = height-py;
+static gboolean on_poly_key(GritsPoly *poly, GdkEventKey *event)
+{
+       g_debug("GritsPluginTest: on_poly_key - %d", event->keyval);
+       gdouble colors[0xff][3] = {
+               [GDK_KEY_r] {1, 0, 0},
+               [GDK_KEY_g] {0, 1, 0},
+               [GDK_KEY_b] {0, 0, 1},
+       };
+       if (event->keyval >= G_N_ELEMENTS(colors))
+               return FALSE;
+       int key = event->keyval;
+       memcpy(poly->color, colors[key], sizeof(gdouble)*3);
+       grits_object_queue_draw(GRITS_OBJECT(poly));
+       return TRUE;
+}
 
-       //cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
-       //cairo_t *cairo = cairo_create(surface);
-       //cairo_set_source_rgba(cairo, 1, 1, 1, 1);
-       //cairo_arc(cairo, px, py, 4, 0, 2*G_PI);
-       //cairo_fill(cairo);
-       //cairo_move_to(cairo, px+4, py-8);
-       //cairo_set_font_size(cairo, 10);
-       //cairo_show_text(cairo, "Marker!");
+static gboolean on_marker_button(GritsMarker *marker, GdkEventButton *event)
+{
+       g_debug("GritsPluginTest: on_marker_button");
+       GtkWidget *dialog = gtk_dialog_new_with_buttons(
+                       "St. Charles!", NULL, 0, GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
+       gtk_dialog_run(GTK_DIALOG(dialog));
+       return TRUE;
+}
 
-       //guint tex;
-       //glEnable(GL_TEXTURE_2D);
-       //glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
-       //glPixelStorei(GL_PACK_ALIGNMENT, 1);
-       //glGenTextures(1, &tex);
-       //glBindTexture(GL_TEXTURE_2D, tex);
-       //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
-       //              cairo_image_surface_get_data(surface));
-       //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
-       //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+/***********
+ * Methods *
+ ***********/
+void _load_marker(GritsPluginTest *test)
+{
+       test->marker = grits_marker_new("St. Charles");
+       GRITS_OBJECT(test->marker)->center.lat  =  38.841847;
+       GRITS_OBJECT(test->marker)->center.lon  = -90.491982;
+       GRITS_OBJECT(test->marker)->center.elev =   0.0;
+       GRITS_OBJECT(test->marker)->lod         = EARTH_R*3;
+       grits_object_set_cursor(GRITS_OBJECT(test->marker), GDK_HAND2);
+       grits_viewer_add(test->viewer, GRITS_OBJECT(test->marker), GRITS_LEVEL_HUD, FALSE);
+       g_signal_connect(test->marker, "clicked", G_CALLBACK(on_marker_button), test->viewer);
+}
 
-       //glMatrixMode(GL_PROJECTION);
-       //glLoadIdentity();
-       //glMatrixMode(GL_MODELVIEW);
-       //glLoadIdentity();
+void _load_poly(GritsPluginTest *test)
+{
+       test->poly = grits_poly_parse("35,-90 35,-110 45,-110 45,-90", "\t", " ", ",");
+       test->poly->color[0]  = test->poly->border[0] = 1;
+       test->poly->color[1]  = test->poly->border[1] = 0;
+       test->poly->color[2]  = test->poly->border[2] = 0;
+       test->poly->color[3]  = 0.2;
+       test->poly->border[3] = 1;
+       test->poly->width     = 6;
+       grits_viewer_add(test->viewer, GRITS_OBJECT(test->poly),  GRITS_LEVEL_OVERLAY, FALSE);
+       g_signal_connect(test->poly, "enter",     G_CALLBACK(on_poly_enter),  NULL);
+       g_signal_connect(test->poly, "leave",     G_CALLBACK(on_poly_leave),  NULL);
+       g_signal_connect(test->poly, "clicked",   G_CALLBACK(on_poly_button), NULL);
+       g_signal_connect(test->poly, "key-press", G_CALLBACK(on_poly_key),    NULL);
+}
+
+void _load_line(GritsPluginTest *test)
+{
+       test->line = grits_line_parse("30,-80 30,-120 50,-120 50,-80", "\t", " ", ",");
+       test->line->color[0]  = 1;
+       test->line->color[1]  = 0;
+       test->line->color[2]  = 0;
+       test->line->color[3]  = 1;
+       test->line->width     = 8;
+       grits_viewer_add(test->viewer, GRITS_OBJECT(test->line),  GRITS_LEVEL_OVERLAY, FALSE);
+       g_signal_connect(test->line, "enter",        G_CALLBACK(on_poly_enter),  NULL);
+       g_signal_connect(test->line, "leave",        G_CALLBACK(on_poly_leave),  NULL);
+       g_signal_connect(test->line, "button-press", G_CALLBACK(on_poly_button), NULL);
+       g_signal_connect(test->line, "key-press",    G_CALLBACK(on_poly_key),    NULL);
+}
 
-       //glDisable(GL_COLOR_MATERIAL);
-       //glDisable(GL_CULL_FACE);
-       //glDisable(GL_DEPTH_TEST);
-       //glDisable(GL_LIGHTING);
-       //glBegin(GL_QUADS);
-       //glTexCoord2d(0, 0); glVertex3f(-1,  1, 1);
-       //glTexCoord2d(1, 0); glVertex3f( 1,  1, 1);
-       //glTexCoord2d(1, 1); glVertex3f( 1, -1, 1);
-       //glTexCoord2d(0, 1); glVertex3f(-1, -1, 1);
-       //glEnd();
-       //glDeleteTextures(1, &tex);
-       //cairo_destroy(cairo);
-       //cairo_surface_destroy(surface);
+/**
+ * grits_plugin_test_new:
+ * @viewer: the #GritsViewer to use for drawing
+ *
+ * Create a new instance of the testing plugin.
+ *
+ * Returns: the new #GritsPluginTest
+ */
+GritsPluginTest *grits_plugin_test_new(GritsViewer *viewer)
+{
+       g_debug("GritsPluginTest: new");
+       GritsPluginTest *test = g_object_new(GRITS_TYPE_PLUGIN_TEST, NULL);
+       test->viewer = g_object_ref(viewer);
+       _load_marker(test);
+       _load_poly(test);
+       _load_line(test);
+       return test;
 }
 
 
@@ -95,22 +156,37 @@ static void gis_plugin_test_expose(GisPlugin *_self)
  * GObject code *
  ****************/
 /* Plugin init */
-static void gis_plugin_test_plugin_init(GisPluginInterface *iface);
-G_DEFINE_TYPE_WITH_CODE(GisPluginTest, gis_plugin_test, G_TYPE_OBJECT,
-               G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
-                       gis_plugin_test_plugin_init));
-static void gis_plugin_test_plugin_init(GisPluginInterface *iface)
+static void grits_plugin_test_plugin_init(GritsPluginInterface *iface);
+G_DEFINE_TYPE_WITH_CODE(GritsPluginTest, grits_plugin_test, G_TYPE_OBJECT,
+               G_IMPLEMENT_INTERFACE(GRITS_TYPE_PLUGIN,
+                       grits_plugin_test_plugin_init));
+static void grits_plugin_test_plugin_init(GritsPluginInterface *iface)
 {
-       g_debug("GisPluginTest: plugin_init");
+       g_debug("GritsPluginTest: plugin_init");
        /* Add methods to the interface */
-       iface->expose = gis_plugin_test_expose;
 }
 /* Class/Object init */
-static void gis_plugin_test_init(GisPluginTest *self)
+static void grits_plugin_test_init(GritsPluginTest *test)
+{
+       g_debug("GritsPluginTest: init");
+}
+static void grits_plugin_test_dispose(GObject *_test)
 {
-       g_debug("GisPluginTest: init");
+       g_debug("GritsPluginTest: dispose");
+       GritsPluginTest *test = GRITS_PLUGIN_TEST(_test);
+       if (test->viewer) {
+               GritsViewer *viewer = test->viewer;
+               test->viewer = NULL;
+               grits_object_destroy_pointer(&test->marker);
+               grits_object_destroy_pointer(&test->poly);
+               grits_object_destroy_pointer(&test->line);
+               g_object_unref(viewer);
+       }
+       G_OBJECT_CLASS(grits_plugin_test_parent_class)->dispose(_test);
 }
-static void gis_plugin_test_class_init(GisPluginTestClass *klass)
+static void grits_plugin_test_class_init(GritsPluginTestClass *klass)
 {
-       g_debug("GisPluginTest: class_init");
+       g_debug("GritsPluginTest: class_init");
+       GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
+       gobject_class->dispose = grits_plugin_test_dispose;
 }