]> Pileus Git - grits/blob - src/plugin-example.c
Example plugin in GObject plugin format
[grits] / src / plugin-example.c
1 /*
2  * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
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 #include <config.h>
19 #include <gtk/gtk.h>
20 #include <gtk/gtkgl.h>
21 #include <GL/gl.h>
22
23 #include "aweather-gui.h"
24 #include "aweather-plugin.h"
25 #include "plugin-example.h"
26
27 /****************
28  * GObject code *
29  ****************/
30 static void aweather_example_plugin_init(AWeatherPluginInterface *iface);
31 static void aweather_example_expose(AWeatherPlugin *_example);
32 G_DEFINE_TYPE_WITH_CODE(AWeatherExample, aweather_example, G_TYPE_OBJECT,
33                 G_IMPLEMENT_INTERFACE(AWEATHER_TYPE_PLUGIN,
34                         aweather_example_plugin_init));
35 static void aweather_example_class_init(AWeatherExampleClass *klass)
36 {
37         GObjectClass *object_class = (GObjectClass*)klass;
38 }
39 static void aweather_example_plugin_init(AWeatherPluginInterface *iface)
40 {
41         /* Add methods to the interface */
42         iface->expose = aweather_example_expose;
43 }
44 static void aweather_example_init(AWeatherExample *example)
45 {
46         /* Set defaults */
47         example->gui      = NULL;
48         example->button   = NULL;
49         example->rotation = 30.0;
50 }
51
52 /***********
53  * Helpers *
54  ***********/
55 static gboolean rotate(gpointer _example)
56 {
57         AWeatherExample *example = _example;
58         if (gtk_toggle_button_get_active(example->button)) {
59                 example->rotation += 1.0;
60                 aweather_gui_gl_redraw(example->gui);
61         }
62         return TRUE;
63 }
64
65 /***********
66  * Methods *
67  ***********/
68 AWeatherExample *aweather_example_new(AWeatherGui *gui)
69 {
70         //g_message("aweather_view_new");
71         AWeatherExample *example = g_object_new(AWEATHER_TYPE_EXAMPLE, NULL);
72         example->gui = gui;
73
74         GtkWidget *drawing = aweather_gui_get_widget(gui, "drawing");
75         GtkWidget *config  = aweather_gui_get_widget(gui, "tabs");
76
77         /* Add configuration tab */
78         GtkWidget *label = gtk_label_new("example");
79         example->button = GTK_TOGGLE_BUTTON(gtk_toggle_button_new_with_label("Rotate"));
80         gtk_notebook_append_page(GTK_NOTEBOOK(config), GTK_WIDGET(example->button), label);
81
82         /* Set up OpenGL Stuff */
83         //g_signal_connect(drawing, "expose-event", G_CALLBACK(expose), NULL);
84         g_timeout_add(1000/60, rotate, example);
85
86         return example;
87 }
88
89 static void aweather_example_expose(AWeatherPlugin *_example)
90 {
91         AWeatherExample *example = AWEATHER_EXAMPLE(_example);
92         g_message("aweather_example_expose");
93         glDisable(GL_TEXTURE_2D);
94         glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity();
95         glOrtho(-1,1,-1,1,-10,10);
96         glMatrixMode(GL_MODELVIEW ); glPushMatrix(); glLoadIdentity();
97
98         float light_ambient[]  = {0.1f, 0.1f, 0.0f};
99         float light_diffuse[]  = {0.9f, 0.9f, 0.9f};
100         float light_position[] = {-30.0f, 50.0f, 40.0f, 1.0f};
101         glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
102         glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
103         glLightfv(GL_LIGHT0, GL_POSITION, light_position);
104         glEnable(GL_LIGHT0);
105         glEnable(GL_LIGHTING);
106         glEnable(GL_COLOR_MATERIAL);
107
108         glTranslatef(0.5, -0.5, -2);
109         glRotatef(example->rotation, 1, 0, 1);
110         glColor4f(0.9, 0.9, 0.7, 1.0);
111         gdk_gl_draw_teapot(TRUE, 0.25);
112         gdk_gl_draw_cube(TRUE, 0.25);
113         glColor4f(1.0, 1.0, 1.0, 1.0);
114
115         glDisable(GL_LIGHT0);
116         glDisable(GL_LIGHTING);
117         glDisable(GL_COLOR_MATERIAL);
118
119         glMatrixMode(GL_PROJECTION); glPopMatrix(); 
120         glMatrixMode(GL_MODELVIEW ); glPopMatrix();
121         return;
122 }
123