| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
z> |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include <config.h> |
| 23 |
|
| 24 |
#include <glib/gi18n.h> |
| 25 |
#include <gtk/gtk.h> |
| 26 |
#include <librsvg/rsvg.h> |
| 27 |
|
| 28 |
#include "gsm_color_button.h" |
| 29 |
|
| 30 |
typedef struct |
| 31 |
{ |
| 32 |
GtkColorDialog *cc_dialog; |
| 33 |
|
| 34 |
gchar *title; |
| 35 |
|
| 36 |
GdkRGBA color; |
| 37 |
gdouble fraction; |
| 38 |
guint type; |
| 39 |
cairo_surface_t *image_buffer; |
| 40 |
cairo_surface_t *mask_buffer; |
| 41 |
gdouble highlight; |
| 42 |
GCancellable *cancellable; |
| 43 |
} GsmColorButtonPrivate; |
| 44 |
|
| 45 |
G_DEFINE_TYPE_WITH_PRIVATE (GsmColorButton, gsm_color_button, GTK_TYPE_WIDGET) |
| 46 |
|
| 47 |
|
| 48 |
enum |
| 49 |
{ |
| 50 |
PROP_0, |
| 51 |
PROP_PERCENTAGE, |
| 52 |
PROP_TITLE, |
| 53 |
PROP_COLOR, |
| 54 |
PROP_TYPE, |
| 55 |
}; |
| 56 |
|
| 57 |
|
| 58 |
enum |
| 59 |
{ |
| 60 |
COLOR_SET, |
| 61 |
LAST_SIGNAL |
| 62 |
}; |
| 63 |
|
| 64 |
#define GSMCP_MIN_WIDTH 15 |
| 65 |
#define GSMCP_MIN_HEIGHT 15 |
| 66 |
|
| 67 |
static guint color_button_signals[LAST_SIGNAL] = { |
| 68 |
0 |
| 69 |
}; |
| 70 |
|
| 71 |
static cairo_surface_t * |
| 72 |
fill_image_buffer_from_resource (cairo_t *cr, |
| 73 |
const char *path) |
| 74 |
{ |
| 75 |
GBytes *bytes; |
| 76 |
const guint8 *data; |
| 77 |
gsize len; |
| 78 |
GError *error = NULL; |
| 79 |
RsvgHandle *handle; |
| 80 |
RsvgRectangle viewport = { |
| 81 |
0, 0, 32, 32 |
| 82 |
}; |
| 83 |
cairo_surface_t *tmp_surface; |
| 84 |
cairo_t *tmp_cr; |
| 85 |
|
| 86 |
bytes = g_resources_lookup_data (path, 0, NULL); |
| 87 |
data = g_bytes_get_data (bytes, &len); |
| 88 |
|
| 89 |
handle = rsvg_handle_new_from_data (data, len, &error); |
| 90 |
if (handle == NULL) |
| 91 |
{ |
| 92 |
g_warning ("rsvg_handle_new_from_data(\"%s\") failed: %s", |
| 93 |
path, (error ? error->message : "unknown error")); |
| 94 |
if (error) |
| 95 |
g_error_free (error); |
| 96 |
g_bytes_unref (bytes); |
| 97 |
return NULL; |
| 98 |
} |
| 99 |
|
| 100 |
tmp_surface = cairo_surface_create_similar (cairo_get_target (cr), |
| 101 |
CAIRO_CONTENT_COLOR_ALPHA, |
| 102 |
32, 32); |
| 103 |
tmp_cr = cairo_create (tmp_surface); |
| 104 |
if (!rsvg_handle_render_document (handle, tmp_cr, &viewport, &error)) |
| 105 |
{ |
| 106 |
g_warning ("rsvg_handle_render_document(\"%s\") failed: %s", |
| 107 |
path, (error ? error->message : "unknown error")); |
| 108 |
if (error) |
| 109 |
g_error_free (error); |
| 110 |
g_bytes_unref (bytes); |
| 111 |
return NULL; |
| 112 |
} |
| 113 |
|
| 114 |
cairo_destroy (tmp_cr); |
| 115 |
g_object_unref (handle); |
| 116 |
g_bytes_unref (bytes); |
| 117 |
return tmp_surface; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
static void |
| 122 |
gsm_color_button_draw_colored_icon (cairo_t *cr, |
| 123 |
cairo_surface_t *image_buffer, |
| 124 |
cairo_surface_t *mask_buffer, |
| 125 |
gboolean upside_down) |
| 126 |
{ |
| 127 |
if (upside_down) |
| 128 |
{ |
| 129 |
cairo_translate (cr, 16, 16); |
| 130 |
cairo_rotate (cr, G_PI); |
| 131 |
cairo_translate (cr, -16, -13); |
| 132 |
} |
| 133 |
|
| 134 |
cairo_mask_surface (cr, mask_buffer, 0.0, 0.0); |
| 135 |
cairo_stroke (cr); |
| 136 |
|
| 137 |
cairo_set_source_surface (cr, image_buffer, 0.0, 0.0); |
| 138 |
cairo_paint (cr); |
| 139 |
|
| 140 |
if (upside_down) |
| 141 |
{ |
| 142 |
cairo_translate (cr, 16, 13); |
| 143 |
cairo_rotate (cr, -G_PI); |
| 144 |
cairo_translate (cr, -16, -16); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
static void |
| 149 |
gsm_color_button_snapshot (GtkWidget *widget, |
| 150 |
GtkSnapshot *snapshot) |
| 151 |
{ |
| 152 |
GsmColorButton *color_button = GSM_COLOR_BUTTON (widget); |
| 153 |
GsmColorButtonPrivate *priv = gsm_color_button_get_instance_private (color_button); |
| 154 |
GdkRGBA *color = gdk_rgba_copy (&priv->color); |
| 155 |
GtkStateFlags state_flags = gtk_widget_get_state_flags (widget); |
| 156 |
gint width, height; |
| 157 |
gdouble radius, arc_start, arc_end; |
| 158 |
gdouble highlight_factor; |
| 159 |
gboolean sensitive = gtk_widget_get_sensitive (widget); |
| 160 |
|
| 161 |
width = gtk_widget_get_width (widget); |
| 162 |
height = gtk_widget_get_height (widget); |
| 163 |
|
| 164 |
graphene_rect_t bounds; |
| 165 |
|
| 166 |
graphene_rect_init (&bounds, 0, 0, width, height); |
| 167 |
cairo_t *cr = gtk_snapshot_append_cairo (snapshot, &bounds); |
| 168 |
|
| 169 |
if (sensitive && state_flags & GTK_STATE_FLAG_PRELIGHT) |
| 170 |
{ |
| 171 |
highlight_factor = 0.125; |
| 172 |
|
| 173 |
color->red = MIN (1.0, color->red + highlight_factor); |
| 174 |
|
| 175 |
color->blue = MIN (1.0, color->blue + highlight_factor); |
| 176 |
|
| 177 |
color->green = MIN (1.0, color->green + highlight_factor); |
| 178 |
} |
| 179 |
else if (!sensitive) |
| 180 |
{ |
| 181 |
gtk_widget_get_color (widget, color); |
| 182 |
} |
| 183 |
gdk_cairo_set_source_rgba (cr, color); |
| 184 |
gdk_rgba_free (color); |
| 185 |
|
| 186 |
switch (priv->type) |
| 187 |
{ |
| 188 |
case GSMCP_TYPE_CPU: |
| 189 |
|
| 190 |
cairo_paint (cr); |
| 191 |
cairo_set_line_width (cr, 1); |
| 192 |
cairo_set_source_rgba (cr, 0, 0, 0, 0.5); |
| 193 |
cairo_rectangle (cr, 0.5, 0.5, width - 1, height - 1); |
| 194 |
cairo_stroke (cr); |
| 195 |
cairo_set_line_width (cr, 1); |
| 196 |
cairo_set_source_rgba (cr, 1, 1, 1, 0.4); |
| 197 |
cairo_rectangle (cr, 1.5, 1.5, width - 3, height - 3); |
| 198 |
cairo_stroke (cr); |
| 199 |
break; |
| 200 |
|
| 201 |
case GSMCP_TYPE_PIE: |
| 202 |
if (width < 32) |
| 203 |
gtk_widget_set_size_request (widget, 32, 32); |
| 204 |
if (width < height) |
| 205 |
radius = width / 2; |
| 206 |
else |
| 207 |
radius = height / 2; |
| 208 |
|
| 209 |
arc_start = -G_PI_2 + 2 * G_PI * priv->fraction; |
| 210 |
arc_end = -G_PI_2; |
| 211 |
|
| 212 |
cairo_set_line_width (cr, 1); |
| 213 |
|
| 214 |
|
| 215 |
if (priv->fraction < 0.01) |
| 216 |
{ |
| 217 |
cairo_arc (cr, (width / 2) + .5, (height / 2) + .5, 4.5, |
| 218 |
0, 2 * G_PI); |
| 219 |
} |
| 220 |
else if (priv->fraction > 0.99) |
| 221 |
{ |
| 222 |
cairo_arc (cr, (width / 2) + .5, (height / 2) + .5, radius - 2.25, |
| 223 |
0, 2 * G_PI); |
| 224 |
} |
| 225 |
else |
| 226 |
{ |
| 227 |
cairo_arc_negative (cr, (width / 2) + .5, (height / 2) + .5, radius - 2.25, |
| 228 |
arc_start, arc_end); |
| 229 |
cairo_arc_negative (cr, (width / 2) + .5, (height / 2) + .5, 4.5, |
| 230 |
arc_end, arc_start); |
| 231 |
cairo_arc_negative (cr, (width / 2) + .5, (height / 2) + .5, radius - 2.25, |
| 232 |
arc_start, arc_start); |
| 233 |
} |
| 234 |
cairo_fill_preserve (cr); |
| 235 |
cairo_set_source_rgba (cr, 0, 0, 0, 0.7); |
| 236 |
cairo_stroke (cr); |
| 237 |
|
| 238 |
|
| 239 |
cairo_set_source_rgba (cr, 1, 1, 1, 0.45); |
| 240 |
cairo_set_line_width (cr, 1); |
| 241 |
|
| 242 |
if (priv->fraction < 0.03) |
| 243 |
{ |
| 244 |
cairo_arc (cr, (width / 2) + .5, (height / 2) + .5, 3.25, |
| 245 |
0, 2 * G_PI); |
| 246 |
} |
| 247 |
else if (priv->fraction > 0.99) |
| 248 |
{ |
| 249 |
cairo_arc (cr, (width / 2) + .5, (height / 2) + .5, radius - 3.5, |
| 250 |
0, 2 * G_PI); |
| 251 |
} |
| 252 |
else |
| 253 |
{ |
| 254 |
cairo_arc_negative (cr, (width / 2) + .5, (height / 2) + .5, radius - 3.5, |
| 255 |
arc_start + (1 / (radius - 3.75)), |
| 256 |
arc_end - (1 / (radius - 3.75))); |
| 257 |
cairo_arc_negative (cr, (width / 2) + .5, (height / 2) + .5, 3.25, |
| 258 |
arc_end - (1 / (radius - 3.75)), |
| 259 |
arc_start + (1 / (radius - 3.75))); |
| 260 |
cairo_arc_negative (cr, (width / 2) + .5, (height / 2) + .5, radius - 3.5, |
| 261 |
arc_start + (1 / (radius - 3.75)), |
| 262 |
arc_start + (1 / (radius - 3.75))); |
| 263 |
} |
| 264 |
cairo_stroke (cr); |
| 265 |
|
| 266 |
|
| 267 |
cairo_set_line_width (cr, 1); |
| 268 |
cairo_set_source_rgba (cr, 0, 0, 0, 0.2); |
| 269 |
cairo_arc (cr, (width / 2) + .5, (height / 2) + .5, radius - 1.25, 0, |
| 270 |
G_PI * 2); |
| 271 |
cairo_stroke (cr); |
| 272 |
|
| 273 |
break; |
| 274 |
|
| 275 |
case GSMCP_TYPE_NETWORK_IN: |
| 276 |
if (priv->image_buffer == NULL) |
| 277 |
priv->image_buffer = |
| 278 |
fill_image_buffer_from_resource (cr, "/org/gnome/gnome-system-monitor/pixmaps/arrow_overlay.svg"); |
| 279 |
if (priv->mask_buffer == NULL) |
| 280 |
priv->mask_buffer = |
| 281 |
fill_image_buffer_from_resource (cr, "/org/gnome/gnome-system-monitor/pixmaps/arrow_mask.svg"); |
| 282 |
|
| 283 |
gtk_widget_set_size_request (widget, 32, 32); |
| 284 |
gsm_color_button_draw_colored_icon (cr, priv->image_buffer, priv->mask_buffer, TRUE); |
| 285 |
|
| 286 |
break; |
| 287 |
|
| 288 |
case GSMCP_TYPE_NETWORK_OUT: |
| 289 |
if (priv->image_buffer == NULL) |
| 290 |
priv->image_buffer = |
| 291 |
fill_image_buffer_from_resource (cr, "/org/gnome/gnome-system-monitor/pixmaps/arrow_overlay.svg"); |
| 292 |
if (priv->mask_buffer == NULL) |
| 293 |
priv->mask_buffer = |
| 294 |
fill_image_buffer_from_resource (cr, "/org/gnome/gnome-system-monitor/pixmaps/arrow_mask.svg"); |
| 295 |
|
| 296 |
gtk_widget_set_size_request (widget, 32, 32); |
| 297 |
gsm_color_button_draw_colored_icon (cr, priv->image_buffer, priv->mask_buffer, FALSE); |
| 298 |
|
| 299 |
break; |
| 300 |
|
| 301 |
case GSMCP_TYPE_DISK_READ: |
| 302 |
if (priv->image_buffer == NULL) |
| 303 |
priv->image_buffer = |
| 304 |
fill_image_buffer_from_resource (cr, "/org/gnome/gnome-system-monitor/pixmaps/arrow_overlay.svg"); |
| 305 |
if (priv->mask_buffer == NULL) |
| 306 |
priv->mask_buffer = |
| 307 |
fill_image_buffer_from_resource (cr, "/org/gnome/gnome-system-monitor/pixmaps/arrow_mask.svg"); |
| 308 |
|
| 309 |
gtk_widget_set_size_request (widget, 32, 32); |
| 310 |
gsm_color_button_draw_colored_icon (cr, priv->image_buffer, priv->mask_buffer, FALSE); |
| 311 |
|
| 312 |
break; |
| 313 |
|
| 314 |
case GSMCP_TYPE_DISK_WRITE: |
| 315 |
if (priv->image_buffer == NULL) |
| 316 |
priv->image_buffer = |
| 317 |
fill_image_buffer_from_resource (cr, "/org/gnome/gnome-system-monitor/pixmaps/arrow_overlay.svg"); |
| 318 |
if (priv->mask_buffer == NULL) |
| 319 |
priv->mask_buffer = |
| 320 |
fill_image_buffer_from_resource (cr, "/org/gnome/gnome-system-monitor/pixmaps/arrow_mask.svg"); |
| 321 |
|
| 322 |
gtk_widget_set_size_request (widget, 32, 32); |
| 323 |
gsm_color_button_draw_colored_icon (cr, priv->image_buffer, priv->mask_buffer, TRUE); |
| 324 |
|
| 325 |
break; |
| 326 |
} |
| 327 |
|
| 328 |
cairo_destroy (cr); |
| 329 |
} |
| 330 |
|
| 331 |
static void |
| 332 |
gsm_color_button_measure (GtkWidget*, |
| 333 |
GtkOrientation orientation, |
| 334 |
int, |
| 335 |
int *minimum, |
| 336 |
int *natural, |
| 337 |
int*, |
| 338 |
int*) |
| 339 |
{ |
| 340 |
if (orientation & GTK_ORIENTATION_HORIZONTAL) |
| 341 |
{ |
| 342 |
*minimum = GSMCP_MIN_WIDTH; |
| 343 |
*natural = GSMCP_MIN_WIDTH; |
| 344 |
} |
| 345 |
if (orientation & GTK_ORIENTATION_VERTICAL) |
| 346 |
{ |
| 347 |
*minimum = GSMCP_MIN_HEIGHT; |
| 348 |
*natural = GSMCP_MIN_HEIGHT; |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
static void |
| 353 |
gsm_color_button_state_flags_changed (GtkWidget *self, |
| 354 |
GtkStateFlags) |
| 355 |
{ |
| 356 |
gtk_widget_queue_draw (self); |
| 357 |
} |
| 358 |
|
| 359 |
static void |
| 360 |
dialog_response (GObject *source, |
| 361 |
GAsyncResult *result, |
| 362 |
gpointer data) |
| 363 |
{ |
| 364 |
GtkColorDialog *dialog = GTK_COLOR_DIALOG (source); |
| 365 |
GsmColorButton *color_button = data; |
| 366 |
GdkRGBA *color; |
| 367 |
|
| 368 |
color = gtk_color_dialog_choose_rgba_finish (dialog, result, NULL); |
| 369 |
|
| 370 |
if (color) |
| 371 |
{ |
| 372 |
gsm_color_button_set_color (color_button, color); |
| 373 |
gdk_rgba_free (color); |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
static void |
| 378 |
gsm_color_button_released (GtkGestureClick*, |
| 379 |
gint, |
| 380 |
gdouble, |
| 381 |
gdouble, |
| 382 |
GsmColorButton *color_button) |
| 383 |
{ |
| 384 |
GsmColorButtonPrivate *priv = gsm_color_button_get_instance_private (color_button); |
| 385 |
GtkRoot *parent; |
| 386 |
|
| 387 |
parent = gtk_widget_get_root (GTK_WIDGET (color_button)); |
| 388 |
|
| 389 |
|
| 390 |
if (!priv->cc_dialog) |
| 391 |
{ |
| 392 |
|
| 393 |
GtkColorDialog *cc_dialog; |
| 394 |
|
| 395 |
cc_dialog = gtk_color_dialog_new (); |
| 396 |
|
| 397 |
gtk_color_dialog_set_title (cc_dialog, priv->title); |
| 398 |
gtk_color_dialog_set_modal (cc_dialog, TRUE); |
| 399 |
|
| 400 |
priv->cc_dialog = cc_dialog; |
| 401 |
} |
| 402 |
|
| 403 |
g_cancellable_cancel (priv->cancellable); |
| 404 |
priv->cancellable = g_cancellable_new (); |
| 405 |
|
| 406 |
gtk_color_dialog_choose_rgba (priv->cc_dialog, |
| 407 |
GTK_WINDOW (parent), |
| 408 |
&priv->color, |
| 409 |
priv->cancellable, |
| 410 |
dialog_response, |
| 411 |
color_button); |
| 412 |
} |
| 413 |
|
| 414 |
static gboolean |
| 415 |
gsm_color_button_drag_data_drop (GtkDropTarget*, |
| 416 |
const GValue *value, |
| 417 |
gdouble, |
| 418 |
gdouble, |
| 419 |
GsmColorButton *color_button) |
| 420 |
{ |
| 421 |
if (G_VALUE_HOLDS (value, GDK_TYPE_RGBA)) |
| 422 |
gsm_color_button_set_color (color_button, g_value_get_boxed (value)); |
| 423 |
else |
| 424 |
return FALSE; |
| 425 |
|
| 426 |
return TRUE; |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
static void |
| 431 |
gsm_color_button_drag_begin (GtkDragSource *drag_source, |
| 432 |
GdkDrag*, |
| 433 |
GsmColorButton *color_button) |
| 434 |
{ |
| 435 |
GsmColorButtonPrivate *priv = |
| 436 |
gsm_color_button_get_instance_private (color_button); |
| 437 |
g_autoptr (GtkSnapshot) snapshot = gtk_snapshot_new (); |
| 438 |
g_autoptr (GdkPaintable) paintable = NULL; |
| 439 |
|
| 440 |
gtk_snapshot_append_color (snapshot, |
| 441 |
&priv->color, |
| 442 |
&GRAPHENE_RECT_INIT (0, 0, 32, 24)); |
| 443 |
|
| 444 |
paintable = |
| 445 |
gtk_snapshot_free_to_paintable (g_steal_pointer (&snapshot), NULL); |
| 446 |
|
| 447 |
gtk_drag_source_set_icon (drag_source, paintable, -2, -2); |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
static GdkContentProvider * |
| 452 |
gsm_color_button_prepare (GtkDragSource*, |
| 453 |
gdouble, |
| 454 |
gdouble, |
| 455 |
GsmColorButton *color_button) |
| 456 |
{ |
| 457 |
GsmColorButtonPrivate *priv = gsm_color_button_get_instance_private (color_button); |
| 458 |
|
| 459 |
return gdk_content_provider_new_typed (GDK_TYPE_RGBA, &priv->color); |
| 460 |
} |
| 461 |
|
| 462 |
static void |
| 463 |
gsm_color_button_set_property (GObject *object, |
| 464 |
guint param_id, |
| 465 |
const GValue *value, |
| 466 |
GParamSpec *pspec) |
| 467 |
{ |
| 468 |
GsmColorButton *color_button = GSM_COLOR_BUTTON (object); |
| 469 |
|
| 470 |
switch (param_id) |
| 471 |
{ |
| 472 |
case PROP_PERCENTAGE: |
| 473 |
gsm_color_button_set_fraction (color_button, |
| 474 |
g_value_get_double (value)); |
| 475 |
break; |
| 476 |
|
| 477 |
case PROP_TITLE: |
| 478 |
gsm_color_button_set_title (color_button, g_value_get_string (value)); |
| 479 |
break; |
| 480 |
|
| 481 |
case PROP_COLOR: |
| 482 |
gsm_color_button_set_color (color_button, g_value_get_boxed (value)); |
| 483 |
break; |
| 484 |
|
| 485 |
case PROP_TYPE: |
| 486 |
gsm_color_button_set_cbtype (color_button, g_value_get_uint (value)); |
| 487 |
break; |
| 488 |
|
| 489 |
default: |
| 490 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); |
| 491 |
break; |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
static void |
| 496 |
gsm_color_button_get_property (GObject *object, |
| 497 |
guint param_id, |
| 498 |
GValue *value, |
| 499 |
GParamSpec *pspec) |
| 500 |
{ |
| 501 |
GsmColorButton *color_button = GSM_COLOR_BUTTON (object); |
| 502 |
GdkRGBA color; |
| 503 |
|
| 504 |
switch (param_id) |
| 505 |
{ |
| 506 |
case PROP_PERCENTAGE: |
| 507 |
g_value_set_double (value, |
| 508 |
gsm_color_button_get_fraction (color_button)); |
| 509 |
break; |
| 510 |
|
| 511 |
case PROP_TITLE: |
| 512 |
g_value_set_string (value, gsm_color_button_get_title (color_button)); |
| 513 |
break; |
| 514 |
|
| 515 |
case PROP_COLOR: |
| 516 |
gsm_color_button_get_color (color_button, &color); |
| 517 |
g_value_set_boxed (value, &color); |
| 518 |
break; |
| 519 |
|
| 520 |
case PROP_TYPE: |
| 521 |
g_value_set_uint (value, gsm_color_button_get_cbtype (color_button)); |
| 522 |
break; |
| 523 |
|
| 524 |
default: |
| 525 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); |
| 526 |
break; |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
static void |
| 531 |
gsm_color_button_finalize (GObject *object) |
| 532 |
{ |
| 533 |
GsmColorButton *color_button = GSM_COLOR_BUTTON (object); |
| 534 |
GsmColorButtonPrivate *priv = gsm_color_button_get_instance_private (color_button); |
| 535 |
|
| 536 |
if (priv->cc_dialog != NULL) |
| 537 |
g_object_unref (G_OBJECT (priv->cc_dialog)); |
| 538 |
priv->cc_dialog = NULL; |
| 539 |
|
| 540 |
g_free (priv->title); |
| 541 |
priv->title = NULL; |
| 542 |
|
| 543 |
cairo_surface_destroy (priv->image_buffer); |
| 544 |
priv->image_buffer = NULL; |
| 545 |
|
| 546 |
G_OBJECT_CLASS (gsm_color_button_parent_class)->finalize (object); |
| 547 |
} |
| 548 |
|
| 549 |
static void |
| 550 |
gsm_color_button_class_init (GsmColorButtonClass *klass) |
| 551 |
{ |
| 552 |
GObjectClass *gobject_class; |
| 553 |
GtkWidgetClass *widget_class; |
| 554 |
|
| 555 |
gobject_class = G_OBJECT_CLASS (klass); |
| 556 |
widget_class = GTK_WIDGET_CLASS (klass); |
| 557 |
|
| 558 |
gobject_class->get_property = gsm_color_button_get_property; |
| 559 |
gobject_class->set_property = gsm_color_button_set_property; |
| 560 |
gobject_class->finalize = gsm_color_button_finalize; |
| 561 |
widget_class->snapshot = gsm_color_button_snapshot; |
| 562 |
widget_class->measure = gsm_color_button_measure; |
| 563 |
widget_class->state_flags_changed = gsm_color_button_state_flags_changed; |
| 564 |
|
| 565 |
g_object_class_install_property (gobject_class, |
| 566 |
PROP_PERCENTAGE, |
| 567 |
g_param_spec_double ("fraction", |
| 568 |
_("Fraction"), |
| 569 |
|
| 570 |
_("Percentage full for pie color pickers"), |
| 571 |
0, 1, 0.5, |
| 572 |
G_PARAM_READWRITE)); |
| 573 |
|
| 574 |
g_object_class_install_property (gobject_class, |
| 575 |
PROP_TITLE, |
| 576 |
g_param_spec_string ("title", |
| 577 |
_("Title"), |
| 578 |
_("The title of the color selection dialog"), |
| 579 |
_("Pick a Color"), |
| 580 |
G_PARAM_READWRITE)); |
| 581 |
|
| 582 |
g_object_class_install_property (gobject_class, |
| 583 |
PROP_COLOR, |
| 584 |
g_param_spec_boxed ("color", |
| 585 |
_("Current Color"), |
| 586 |
_("The selected color"), |
| 587 |
GDK_TYPE_RGBA, |
| 588 |
G_PARAM_READWRITE)); |
| 589 |
|
| 590 |
g_object_class_install_property (gobject_class, |
| 591 |
PROP_TYPE, |
| 592 |
g_param_spec_uint ("type", _("Type"), |
| 593 |
_("Type of color picker"), |
| 594 |
0, 6, 0, |
| 595 |
G_PARAM_READWRITE)); |
| 596 |
|
| 597 |
color_button_signals[COLOR_SET] = g_signal_new ("color-set", |
| 598 |
G_TYPE_FROM_CLASS |
| 599 |
(gobject_class), |
| 600 |
G_SIGNAL_RUN_FIRST, |
| 601 |
0, NULL, NULL, |
| 602 |
g_cclosure_marshal_VOID__VOID, |
| 603 |
G_TYPE_NONE, 0); |
| 604 |
} |
| 605 |
|
| 606 |
static void |
| 607 |
gsm_color_button_init (GsmColorButton *color_button) |
| 608 |
{ |
| 609 |
GsmColorButtonPrivate *priv = gsm_color_button_get_instance_private (color_button); |
| 610 |
|
| 611 |
priv->color.red = 0; |
| 612 |
priv->color.green = 0; |
| 613 |
priv->color.blue = 0; |
| 614 |
priv->fraction = 0.5; |
| 615 |
priv->type = GSMCP_TYPE_CPU; |
| 616 |
priv->image_buffer = NULL; |
| 617 |
priv->title = g_strdup (_("Pick a Color")); |
| 618 |
|
| 619 |
GtkGesture *click_controller = gtk_gesture_click_new (); |
| 620 |
|
| 621 |
g_signal_connect (click_controller, "released", |
| 622 |
G_CALLBACK (gsm_color_button_released), color_button); |
| 623 |
gtk_widget_add_controller (GTK_WIDGET (color_button), GTK_EVENT_CONTROLLER (click_controller)); |
| 624 |
|
| 625 |
GtkDragSource *drag_source = gtk_drag_source_new (); |
| 626 |
|
| 627 |
g_signal_connect (drag_source, "drag-begin", |
| 628 |
G_CALLBACK (gsm_color_button_drag_begin), color_button); |
| 629 |
g_signal_connect (drag_source, "prepare", |
| 630 |
G_CALLBACK (gsm_color_button_prepare), color_button); |
| 631 |
gtk_widget_add_controller (GTK_WIDGET (color_button), GTK_EVENT_CONTROLLER (drag_source)); |
| 632 |
|
| 633 |
GtkDropTarget *drop_target = gtk_drop_target_new (GDK_TYPE_RGBA, |
| 634 |
GDK_ACTION_COPY); |
| 635 |
|
| 636 |
g_signal_connect (drop_target, "drop", |
| 637 |
G_CALLBACK (gsm_color_button_drag_data_drop), color_button); |
| 638 |
gtk_widget_add_controller (GTK_WIDGET (color_button), GTK_EVENT_CONTROLLER (drop_target)); |
| 639 |
|
| 640 |
gtk_widget_set_tooltip_text (GTK_WIDGET (color_button), _("Click to set graph colors")); |
| 641 |
} |
| 642 |
|
| 643 |
GsmColorButton * |
| 644 |
gsm_color_button_new (const GdkRGBA *color, |
| 645 |
guint type) |
| 646 |
{ |
| 647 |
return g_object_new (GSM_TYPE_COLOR_BUTTON, "color", color, "type", type, |
| 648 |
"visible", TRUE, NULL); |
| 649 |
} |
| 650 |
|
| 651 |
guint |
| 652 |
gsm_color_button_get_cbtype (GsmColorButton *color_button) |
| 653 |
{ |
| 654 |
GsmColorButtonPrivate *priv; |
| 655 |
|
| 656 |
g_return_val_if_fail (GSM_IS_COLOR_BUTTON (color_button), 0); |
| 657 |
|
| 658 |
priv = gsm_color_button_get_instance_private (color_button); |
| 659 |
return priv->type; |
| 660 |
} |
| 661 |
|
| 662 |
void |
| 663 |
gsm_color_button_set_cbtype (GsmColorButton *color_button, |
| 664 |
guint type) |
| 665 |
{ |
| 666 |
GsmColorButtonPrivate *priv; |
| 667 |
|
| 668 |
g_return_if_fail (GSM_IS_COLOR_BUTTON (color_button)); |
| 669 |
|
| 670 |
priv = gsm_color_button_get_instance_private (color_button); |
| 671 |
priv->type = type; |
| 672 |
|
| 673 |
gtk_widget_queue_draw (GTK_WIDGET (color_button)); |
| 674 |
|
| 675 |
g_object_notify (G_OBJECT (color_button), "type"); |
| 676 |
} |
| 677 |
|
| 678 |
gdouble |
| 679 |
gsm_color_button_get_fraction (GsmColorButton *color_button) |
| 680 |
{ |
| 681 |
GsmColorButtonPrivate *priv; |
| 682 |
|
| 683 |
g_return_val_if_fail (GSM_IS_COLOR_BUTTON (color_button), 0); |
| 684 |
|
| 685 |
priv = gsm_color_button_get_instance_private (color_button); |
| 686 |
return priv->fraction; |
| 687 |
} |
| 688 |
|
| 689 |
void |
| 690 |
gsm_color_button_set_fraction (GsmColorButton *color_button, |
| 691 |
gdouble fraction) |
| 692 |
{ |
| 693 |
GsmColorButtonPrivate *priv; |
| 694 |
|
| 695 |
g_return_if_fail (GSM_IS_COLOR_BUTTON (color_button)); |
| 696 |
|
| 697 |
priv = gsm_color_button_get_instance_private (color_button); |
| 698 |
|
| 699 |
priv->fraction = fraction; |
| 700 |
|
| 701 |
gtk_widget_queue_draw (GTK_WIDGET (color_button)); |
| 702 |
|
| 703 |
g_object_notify (G_OBJECT (color_button), "fraction"); |
| 704 |
} |
| 705 |
|
| 706 |
void |
| 707 |
gsm_color_button_get_color (GsmColorButton *color_button, |
| 708 |
GdkRGBA *color) |
| 709 |
{ |
| 710 |
GsmColorButtonPrivate *priv; |
| 711 |
|
| 712 |
g_return_if_fail (GSM_IS_COLOR_BUTTON (color_button)); |
| 713 |
|
| 714 |
priv = gsm_color_button_get_instance_private (color_button); |
| 715 |
|
| 716 |
*color = priv->color; |
| 717 |
} |
| 718 |
|
| 719 |
void |
| 720 |
gsm_color_button_set_color (GsmColorButton *color_button, |
| 721 |
const GdkRGBA *color) |
| 722 |
{ |
| 723 |
GsmColorButtonPrivate *priv; |
| 724 |
|
| 725 |
g_return_if_fail (GSM_IS_COLOR_BUTTON (color_button)); |
| 726 |
g_return_if_fail (color != NULL); |
| 727 |
|
| 728 |
priv = gsm_color_button_get_instance_private (color_button); |
| 729 |
|
| 730 |
priv->color = *color; |
| 731 |
|
| 732 |
gtk_widget_queue_draw (GTK_WIDGET (color_button)); |
| 733 |
|
| 734 |
g_signal_emit (color_button, color_button_signals[COLOR_SET], 0); |
| 735 |
g_object_notify (G_OBJECT (color_button), "color"); |
| 736 |
} |
| 737 |
|
| 738 |
void |
| 739 |
gsm_color_button_set_title (GsmColorButton *color_button, |
| 740 |
const gchar *title) |
| 741 |
{ |
| 742 |
GsmColorButtonPrivate *priv; |
| 743 |
gchar *old_title; |
| 744 |
|
| 745 |
g_return_if_fail (GSM_IS_COLOR_BUTTON (color_button)); |
| 746 |
|
| 747 |
priv = gsm_color_button_get_instance_private (color_button); |
| 748 |
|
| 749 |
old_title = priv->title; |
| 750 |
priv->title = g_strdup (title); |
| 751 |
g_free (old_title); |
| 752 |
|
| 753 |
if (priv->cc_dialog) |
| 754 |
gtk_color_dialog_set_title (priv->cc_dialog, |
| 755 |
priv->title); |
| 756 |
|
| 757 |
g_object_notify (G_OBJECT (color_button), "title"); |
| 758 |
} |
| 759 |
|
| 760 |
gchar * |
| 761 |
gsm_color_button_get_title (GsmColorButton *color_button) |
| 762 |
{ |
| 763 |
GsmColorButtonPrivate *priv; |
| 764 |
|
| 765 |
g_return_val_if_fail (GSM_IS_COLOR_BUTTON (color_button), NULL); |
| 766 |
|
| 767 |
priv = gsm_color_button_get_instance_private (color_button); |
| 768 |
return priv->title; |
| 769 |
} |