| 1 |
/* Cursors |
| 2 |
* |
| 3 |
* Demonstrates a useful set of available cursors. |
| 4 |
*/ |
| 5 |
#include <gtk/gtk.h> |
| 6 |
|
| 7 |
static void |
| 8 |
set_cursor (GtkWidget *button, gpointer data) |
| 9 |
{ |
| 10 |
GtkWidget *toplevel; |
| 11 |
GdkCursor *cursor = data; |
| 12 |
GdkWindow *window; |
| 13 |
|
| 14 |
toplevel = gtk_widget_get_toplevel (button); |
| 15 |
window = gtk_widget_get_window (toplevel); |
| 16 |
gdk_window_set_cursor (window, cursor); |
| 17 |
} |
| 18 |
|
| 19 |
static GtkWidget * |
| 20 |
add_section (GtkWidget *box, |
| 21 |
const gchar *heading) |
| 22 |
{ |
| 23 |
GtkWidget *label; |
| 24 |
GtkWidget *section; |
| 25 |
|
| 26 |
label = gtk_label_new (heading); |
| 27 |
gtk_label_set_xalign (GTK_LABEL (label), 0.0); |
| 28 |
gtk_widget_set_margin_top (label, 10); |
| 29 |
gtk_widget_set_margin_bottom (label, 10); |
| 30 |
gtk_box_pack_start (GTK_BOX (box), label, FALSE, TRUE, 0); |
| 31 |
section = gtk_flow_box_new (); |
| 32 |
gtk_widget_set_halign (section, GTK_ALIGN_START); |
| 33 |
gtk_flow_box_set_selection_mode (GTK_FLOW_BOX (section), GTK_SELECTION_NONE); |
| 34 |
gtk_flow_box_set_min_children_per_line (GTK_FLOW_BOX (section), 2); |
| 35 |
gtk_flow_box_set_max_children_per_line (GTK_FLOW_BOX (section), 20); |
| 36 |
gtk_box_pack_start (GTK_BOX (box), section, FALSE, TRUE, 0); |
| 37 |
|
| 38 |
return section; |
| 39 |
} |
| 40 |
|
| 41 |
static void |
| 42 |
add_button (GtkWidget *section, |
| 43 |
const gchar *css_name) |
| 44 |
{ |
| 45 |
GtkWidget *image, *button; |
| 46 |
GdkDisplay *display; |
| 47 |
GdkCursor *cursor; |
| 48 |
|
| 49 |
display = gtk_widget_get_display (section); |
| 50 |
cursor = gdk_cursor_new_from_name (display, css_name); |
| 51 |
if (cursor == NULL) |
| 52 |
image = gtk_image_new_from_icon_name ("image-missing", GTK_ICON_SIZE_MENU); |
| 53 |
else |
| 54 |
{ |
| 55 |
gchar *path; |
| 56 |
|
| 57 |
path = g_strdup_printf ("/cursors/%s_cursor.png", css_name); |
| 58 |
g_strdelimit (path, "-", '_'); |
| 59 |
image = gtk_image_new_from_resource (path); |
| 60 |
g_free (path); |
| 61 |
} |
| 62 |
gtk_widget_set_size_request (image, 32, 32); |
| 63 |
button = gtk_button_new (); |
| 64 |
gtk_container_add (GTK_CONTAINER (button), image); |
| 65 |
gtk_style_context_add_class (gtk_widget_get_style_context (button), "image-button"); |
| 66 |
g_signal_connect (button, "clicked", G_CALLBACK (set_cursor), cursor); |
| 67 |
|
| 68 |
gtk_widget_set_tooltip_text (button, css_name); |
| 69 |
gtk_container_add (GTK_CONTAINER (section), button); |
| 70 |
} |
| 71 |
|
| 72 |
GtkWidget * |
| 73 |
do_cursors (GtkWidget *do_widget) |
| 74 |
{ |
| 75 |
static GtkWidget *window = NULL; |
| 76 |
|
| 77 |
if (!window) |
| 78 |
{ |
| 79 |
GtkWidget *sw; |
| 80 |
GtkWidget *box; |
| 81 |
GtkWidget *section; |
| 82 |
|
| 83 |
window = gtk_window_new (GTK_WINDOW_TOPLEVEL); |
| 84 |
gtk_window_set_screen (GTK_WINDOW (window), |
| 85 |
gtk_widget_get_screen (do_widget)); |
| 86 |
gtk_window_set_title (GTK_WINDOW (window), "Cursors"); |
| 87 |
gtk_window_set_default_size (GTK_WINDOW (window), 500, 500); |
| 88 |
|
| 89 |
g_signal_connect (window, "destroy", |
| 90 |
G_CALLBACK (gtk_widget_destroyed), |
| 91 |
&window); |
| 92 |
|
| 93 |
sw = gtk_scrolled_window_new (NULL, NULL); |
| 94 |
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw), |
| 95 |
GTK_POLICY_NEVER, |
| 96 |
GTK_POLICY_AUTOMATIC); |
| 97 |
gtk_container_add (GTK_CONTAINER (window), sw); |
| 98 |
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0); |
| 99 |
g_object_set (box, |
| 100 |
"margin-start", 20, |
| 101 |
"margin-end", 20, |
| 102 |
"margin-bottom", 10, |
| 103 |
NULL); |
| 104 |
gtk_container_add (GTK_CONTAINER (sw), box); |
| 105 |
|
| 106 |
section = add_section (box, "General"); |
| 107 |
add_button (section, "default"); |
| 108 |
add_button (section, "none"); |
| 109 |
|
| 110 |
section = add_section (box, "Link & Status"); |
| 111 |
add_button (section, "context-menu"); |
| 112 |
add_button (section, "help"); |
| 113 |
add_button (section, "pointer"); |
| 114 |
add_button (section, "progress"); |
| 115 |
add_button (section, "wait"); |
| 116 |
|
| 117 |
section = add_section (box, "Selection"); |
| 118 |
add_button (section, "cell"); |
| 119 |
add_button (section, "crosshair"); |
| 120 |
add_button (section, "text"); |
| 121 |
add_button (section, "vertical-text"); |
| 122 |
|
| 123 |
section = add_section (box, "Drag & Drop"); |
| 124 |
add_button (section, "alias"); |
| 125 |
add_button (section, "copy"); |
| 126 |
add_button (section, "move"); |
| 127 |
add_button (section, "no-drop"); |
| 128 |
add_button (section, "not-allowed"); |
| 129 |
add_button (section, "grab"); |
| 130 |
add_button (section, "grabbing"); |
| 131 |
|
| 132 |
section = add_section (box, "Resize & Scrolling"); |
| 133 |
add_button (section, "all-scroll"); |
| 134 |
add_button (section, "col-resize"); |
| 135 |
add_button (section, "row-resize"); |
| 136 |
add_button (section, "n-resize"); |
| 137 |
add_button (section, "e-resize"); |
| 138 |
add_button (section, "s-resize"); |
| 139 |
add_button (section, "w-resize"); |
| 140 |
add_button (section, "ne-resize"); |
| 141 |
add_button (section, "nw-resize"); |
| 142 |
add_button (section, "se-resize"); |
| 143 |
add_button (section, "sw-resize"); |
| 144 |
add_button (section, "ew-resize"); |
| 145 |
add_button (section, "ns-resize"); |
| 146 |
add_button (section, "nesw-resize"); |
| 147 |
add_button (section, "nwse-resize"); |
| 148 |
|
| 149 |
section = add_section (box, "Zoom"); |
| 150 |
add_button (section, "zoom-in"); |
| 151 |
add_button (section, "zoom-out"); |
| 152 |
} |
| 153 |
|
| 154 |
if (!gtk_widget_get_visible (window)) |
| 155 |
gtk_widget_show_all (window); |
| 156 |
else |
| 157 |
gtk_widget_destroy (window); |
| 158 |
|
| 159 |
|
| 160 |
return window; |
| 161 |
} |