Restructure graph view and add background color property

This commit is contained in:
Mario Hüttel 2020-04-03 21:43:20 +02:00
parent 234722df85
commit 998628bb6c
3 changed files with 123 additions and 8 deletions

View File

@ -32,6 +32,7 @@ int main(int argc, char **argv)
{
GtkWidget *main_window;
GtkWidget *graph_view;
GdkRGBA color;
gtk_init(&argc, &argv);
@ -41,8 +42,15 @@ int main(int argc, char **argv)
graph_view = gtk_graph_view_new();
gtk_container_add(GTK_CONTAINER(main_window), graph_view);
gtk_widget_show(graph_view);
gtk_widget_show_all(main_window);
color.red = 0.6;
color.green = 0.4;
color.blue = 0.1;
color.alpha = 1.0;
gtk_graph_view_set_background_color(GTK_GRAPH_VIEW(graph_view), &color);
gtk_main();
return 0;

View File

@ -34,7 +34,7 @@ G_BEGIN_DECLS
#define GTK_TYPE_GRAPH_VIEW (gtk_graph_view_get_type())
G_DECLARE_FINAL_TYPE(GtkGraphView, gtk_graph_view, GTK, GRAPH_VIEW, GtkGLArea)
G_DECLARE_FINAL_TYPE(GtkGraphView, gtk_graph_view, GTK, GRAPH_VIEW, GtkBox)
/**
* @brief Instanciate new GtkGraphView object
@ -42,6 +42,16 @@ G_DECLARE_FINAL_TYPE(GtkGraphView, gtk_graph_view, GTK, GRAPH_VIEW, GtkGLArea)
*/
GtkWidget *gtk_graph_view_new();
/**
* @brief Set the background color of the graph view
*
* This is equivalent to setting the "background-color" property
*
* @param[in] graph_view GtkGraphView object
* @param[in] RGBA background color.
*/
void gtk_graph_view_set_background_color(GtkGraphView* graph_view, const GdkRGBA *rgba);
G_END_DECLS
#endif /* __GTK_GRAPH_VIEW_H__ */

View File

@ -18,14 +18,79 @@
#include <epoxy/gl.h>
struct _GtkGraphView {
GtkGLArea parent;
GtkBox super;
};
typedef struct {
int i; /**< @note This is just a dummy. Remove! */
/**
* @brief The gl area displaying the graph view.
* @note This variable is not referenced, because the gl_area is tightly bound to the GrapgView widget itself.
*/
GtkWidget *gl_area;
GdkRGBA background_color;
} GtkGraphViewPrivate;
G_DEFINE_TYPE_WITH_PRIVATE(GtkGraphView, gtk_graph_view, GTK_TYPE_GL_AREA)
G_DEFINE_TYPE_WITH_PRIVATE(GtkGraphView, gtk_graph_view, GTK_TYPE_BOX)
enum {
PROP_BG_COLOR = 1,
N_PROPERTIES
};
/**
* @brief Make the gl area the current context and set the clear color
* @param[in] gl_area openGL area
* @param[in] rgba Clear color
*/
static void gl_area_set_clear_color(GtkGLArea *gl_area, const GdkRGBA *rgba)
{
g_return_if_fail(gl_area);
g_return_if_fail(rgba != NULL);
gtk_gl_area_make_current(gl_area);
glClearColor(rgba->red, rgba->green, rgba->blue, rgba->alpha);
}
static gboolean gl_render(GtkGLArea *gl_area, GdkGLContext *context, GtkGraphView *view)
{
GtkGraphViewPrivate *priv;
priv = gtk_graph_view_get_instance_private(view);
gl_area_set_clear_color(gl_area, &priv->background_color);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
return TRUE;
}
static void gtk_graph_view_get_property(GObject *obj, guint property_id, GValue *value, GParamSpec *pspec)
{
}
static void gtk_graph_view_set_property(GObject *obj, guint property_id, const GValue *value, GParamSpec *pspec)
{
gpointer data_ptr;
GtkGraphViewPrivate *priv;
priv = gtk_graph_view_get_instance_private(GTK_GRAPH_VIEW(obj));
switch (property_id) {
case PROP_BG_COLOR:
data_ptr = g_value_get_boxed(value);
if (!data_ptr)
break;
priv->background_color.red = ((GdkRGBA *)data_ptr)->red;
priv->background_color.green = ((GdkRGBA *)data_ptr)->green;
priv->background_color.blue = ((GdkRGBA *)data_ptr)->blue;
priv->background_color.alpha = ((GdkRGBA *)data_ptr)->alpha;
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, property_id, pspec);
break;
}
}
static GParamSpec *gtk_graph_view_properties[N_PROPERTIES] = {NULL};
/**
* @brief Init class structure of GtkGraphView
@ -33,9 +98,17 @@ G_DEFINE_TYPE_WITH_PRIVATE(GtkGraphView, gtk_graph_view, GTK_TYPE_GL_AREA)
*/
static void gtk_graph_view_class_init(GtkGraphViewClass *klass)
{
(void)klass;
GObjectClass *oclass;
return;
oclass = G_OBJECT_CLASS(klass);
oclass->set_property = gtk_graph_view_set_property;
oclass->get_property = gtk_graph_view_get_property;
gtk_graph_view_properties[PROP_BG_COLOR] =
g_param_spec_boxed("background-color", "background color", "Background color of Graph View",
GDK_TYPE_RGBA, G_PARAM_READWRITE);
g_object_class_install_properties(oclass, N_PROPERTIES, gtk_graph_view_properties);
}
/**
@ -47,10 +120,34 @@ static void gtk_graph_view_init(GtkGraphView *obj)
GtkGraphViewPrivate *priv;
priv = gtk_graph_view_get_instance_private(obj);
priv->i = 0;
/* Create new GTK GL_Area as only child of the box */
priv->gl_area = gtk_gl_area_new();
g_signal_connect(priv->gl_area, "render", G_CALLBACK(gl_render), obj);
gtk_container_add(GTK_CONTAINER(obj), priv->gl_area);
gtk_box_set_child_packing(GTK_BOX(obj), priv->gl_area, TRUE, TRUE, 0, GTK_PACK_START);
gtk_widget_show(priv->gl_area);
priv->background_color.red = 0.0;
priv->background_color.green = 0.0;
priv->background_color.blue = 0.0;
priv->background_color.alpha = 1.0;
}
GtkWidget *gtk_graph_view_new()
{
return gtk_widget_new(GTK_TYPE_GRAPH_VIEW, NULL);
}
void gtk_graph_view_set_background_color(GtkGraphView *graph_view, const GdkRGBA *rgba)
{
GValue color_value = G_VALUE_INIT;
g_return_if_fail(GTK_IS_GRAPH_VIEW(graph_view));
g_return_if_fail(rgba != NULL);
g_value_init(&color_value, GDK_TYPE_RGBA);
g_value_set_boxed(&color_value, rgba);
g_object_set_property(G_OBJECT(graph_view), "background-color", &color_value);
g_value_unset(&color_value);
}