gtk/demos/gtk-demo/cursors.c

88 lines
2.4 KiB
C
Raw Normal View History

2015-05-08 00:01:14 +00:00
/* Cursors
*
* Demonstrates a useful set of available cursors. The cursors shown here are the
* ones defined by CSS, which we assume to be available. The example shows creating
* cursors by name or from an image, with or without a fallback.
2015-05-08 00:01:14 +00:00
*/
#include <gtk/gtk.h>
2015-05-08 00:01:14 +00:00
static GtkWidget *window = NULL;
2015-05-08 00:01:14 +00:00
static void
on_destroy (gpointer data)
2015-05-08 00:01:14 +00:00
{
window = NULL;
2015-05-08 00:01:14 +00:00
}
static GdkTexture *
cursor_callback (GdkCursor *cursor,
int cursor_size,
double scale,
int *width,
int *height,
int *hotspot_x,
int *hotspot_y,
gpointer data)
{
GdkPixbuf *pixbuf;
GdkTexture *texture;
GError *error = NULL;
int scaled_size;
scaled_size = ceil (cursor_size * scale);
pixbuf = gdk_pixbuf_new_from_resource_at_scale ("/cursors/images/gtk-logo.svg",
scaled_size, scaled_size,
TRUE,
&error);
if (!pixbuf)
{
g_print ("%s\n", error->message);
g_error_free (error);
return NULL;
}
texture = gdk_texture_new_for_pixbuf (pixbuf);
g_object_unref (pixbuf);
*width = cursor_size;
*height = cursor_size;
*hotspot_x = 18 * cursor_size / 32.0;
*hotspot_y = 2 * cursor_size / 32.0;
return texture;
}
2015-05-08 00:01:14 +00:00
GtkWidget *
do_cursors (GtkWidget *do_widget)
{
if (!window)
{
GtkBuilder *builder;
GtkWidget *logo_callback;
GdkCursor *cursor;
2015-05-08 00:01:14 +00:00
builder = gtk_builder_new_from_resource ("/cursors/cursors.ui");
window = GTK_WIDGET (gtk_builder_get_object (builder, "window"));
g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window);
gtk_window_set_display (GTK_WINDOW (window),
gtk_widget_get_display (do_widget));
2015-05-08 00:01:14 +00:00
g_signal_connect (window, "destroy",
G_CALLBACK (on_destroy), NULL);
logo_callback = GTK_WIDGET (gtk_builder_get_object (builder, "logo_callback"));
cursor = gdk_cursor_new_from_callback (cursor_callback, NULL, NULL, NULL);
gtk_widget_set_cursor (logo_callback, cursor);
g_object_unref (cursor);
g_object_unref (builder);
2015-05-08 00:01:14 +00:00
}
if (!gtk_widget_get_visible (window))
gtk_widget_set_visible (window, TRUE);
2015-05-08 00:01:14 +00:00
else
gtk_window_destroy (GTK_WINDOW (window));
2015-05-08 00:01:14 +00:00
return window;
}