2015-05-08 00:01:14 +00:00
|
|
|
/* Cursors
|
|
|
|
*
|
2020-06-07 02:05:52 +00:00
|
|
|
* 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
|
|
|
*/
|
|
|
|
|
2017-11-04 23:04:23 +00:00
|
|
|
#include <gtk/gtk.h>
|
2015-05-08 00:01:14 +00:00
|
|
|
|
2017-11-04 23:04:23 +00:00
|
|
|
static GtkWidget *window = NULL;
|
2015-05-08 00:01:14 +00:00
|
|
|
|
|
|
|
static void
|
2017-11-04 23:04:23 +00:00
|
|
|
on_destroy (gpointer data)
|
2015-05-08 00:01:14 +00:00
|
|
|
{
|
2017-11-04 23:04:23 +00:00
|
|
|
window = NULL;
|
2015-05-08 00:01:14 +00:00
|
|
|
}
|
|
|
|
|
2024-03-21 11:15:44 +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)
|
|
|
|
{
|
2017-11-04 23:04:23 +00:00
|
|
|
GtkBuilder *builder;
|
2024-03-21 11:15:44 +00:00
|
|
|
GtkWidget *logo_callback;
|
|
|
|
GdkCursor *cursor;
|
2015-05-08 00:01:14 +00:00
|
|
|
|
2017-11-04 23:04:23 +00:00
|
|
|
builder = gtk_builder_new_from_resource ("/cursors/cursors.ui");
|
|
|
|
window = GTK_WIDGET (gtk_builder_get_object (builder, "window"));
|
2021-12-05 03:16:01 +00:00
|
|
|
g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window);
|
2017-10-31 06:41:15 +00:00
|
|
|
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",
|
2017-11-04 23:04:23 +00:00
|
|
|
G_CALLBACK (on_destroy), NULL);
|
2024-03-21 11:15:44 +00:00
|
|
|
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);
|
2020-05-21 19:41:00 +00:00
|
|
|
g_object_unref (builder);
|
2015-05-08 00:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!gtk_widget_get_visible (window))
|
2022-11-28 04:03:14 +00:00
|
|
|
gtk_widget_set_visible (window, TRUE);
|
2015-05-08 00:01:14 +00:00
|
|
|
else
|
2021-12-05 03:16:01 +00:00
|
|
|
gtk_window_destroy (GTK_WINDOW (window));
|
2015-05-08 00:01:14 +00:00
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|