1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
|
/* LIBGIMP - The GIMP Library
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
*
* gimpcolorbutton.c
* Copyright (C) 1999-2001 Sven Neumann
*
* This library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <string.h>
#include <gegl.h>
#include <gtk/gtk.h>
#include "libgimpbase/gimpbase.h"
#include "libgimpcolor/gimpcolor.h"
#include "libgimpconfig/gimpconfig.h"
#include "gimpwidgetstypes.h"
#include "gimpcolorarea.h"
#include "gimpcolorbutton.h"
#include "gimpcolornotebook.h"
#include "gimpcolorselection.h"
#include "gimpdialog.h"
#include "gimphelpui.h"
#include "gimpicons.h"
#include "gimpwidgets-private.h"
#include "libgimp/libgimp-intl.h"
/**
* SECTION: gimpcolorbutton
* @title: GimpColorButton
* @short_description: Widget for selecting a color from a simple button.
* @see_also: #libgimpcolor-gimpcolorspace
*
* This widget provides a simple button with a preview showing the
* color.
*
* On click a color selection dialog is opened. Additionally the
* button supports Drag and Drop and has a right-click menu that
* allows one to choose the color from the current FG or BG color. If
* the user changes the color, the "color-changed" signal is emitted.
**/
#define COLOR_BUTTON_KEY "gimp-color-button"
#define RESPONSE_RESET 1
#define TODOUBLE(i) (i / 65535.0)
#define TOUINT16(d) ((guint16) (d * 65535 + 0.5))
#define GIMP_COLOR_BUTTON_COLOR_FG "use-foreground"
#define GIMP_COLOR_BUTTON_COLOR_BG "use-background"
#define GIMP_COLOR_BUTTON_COLOR_BLACK "use-black"
#define GIMP_COLOR_BUTTON_COLOR_WHITE "use-white"
#define GIMP_COLOR_BUTTON_GROUP_PREFIX "gimp-color-button"
enum
{
COLOR_CHANGED,
LAST_SIGNAL
};
enum
{
PROP_0,
PROP_TITLE,
PROP_COLOR,
PROP_TYPE,
PROP_UPDATE,
PROP_AREA_WIDTH,
PROP_AREA_HEIGHT,
PROP_COLOR_CONFIG
};
typedef struct _GimpColorButtonPrivate
{
gchar *title;
gboolean continuous_update;
GtkWidget *color_area;
GtkWidget *dialog;
GtkWidget *selection;
GSimpleActionGroup *group;
GMenu *menu;
GimpColorConfig *config;
} GimpColorButtonPrivate;
#define GET_PRIVATE(obj) ((GimpColorButtonPrivate *) gimp_color_button_get_instance_private ((GimpColorButton *) (obj)))
static void gimp_color_button_constructed (GObject *object);
static void gimp_color_button_finalize (GObject *object);
static void gimp_color_button_dispose (GObject *object);
static void gimp_color_button_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec);
static void gimp_color_button_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec);
static gboolean gimp_color_button_button_press (GtkWidget *widget,
GdkEventButton *bevent);
static void gimp_color_button_state_flags_changed (GtkWidget *widget,
GtkStateFlags prev_state);
static void gimp_color_button_clicked (GtkButton *button);
static GType gimp_color_button_get_action_type (GimpColorButton *button);
static void gimp_color_button_dialog_response (GtkWidget *dialog,
gint response_id,
GimpColorButton *button);
static void gimp_color_button_use_color (GAction *action,
GVariant *parameter,
GimpColorButton *button);
static void gimp_color_button_area_changed (GtkWidget *color_area,
GimpColorButton *button);
static void gimp_color_button_selection_changed (GtkWidget *selection,
GimpColorButton *button);
static void gimp_color_button_help_func (const gchar *help_id,
gpointer help_data);
typedef void (* GimpColorButtonActionCallback) (GAction *action,
GVariant *parameter,
GimpColorButton *button);
typedef struct _GimpColorButtonActionEntry
{
const gchar *name;
GimpColorButtonActionCallback callback;
} GimpColorButtonActionEntry;
static const GimpColorButtonActionEntry actions[] =
{
{
GIMP_COLOR_BUTTON_COLOR_FG,
gimp_color_button_use_color
},
{
GIMP_COLOR_BUTTON_COLOR_BG,
gimp_color_button_use_color
},
{
GIMP_COLOR_BUTTON_COLOR_BLACK,
gimp_color_button_use_color
},
{
GIMP_COLOR_BUTTON_COLOR_WHITE,
gimp_color_button_use_color
}
};
G_DEFINE_TYPE_WITH_CODE (GimpColorButton, gimp_color_button, GIMP_TYPE_BUTTON,
G_ADD_PRIVATE (GimpColorButton))
#define parent_class gimp_color_button_parent_class
static guint gimp_color_button_signals[LAST_SIGNAL] = { 0 };
static void
gimp_color_button_class_init (GimpColorButtonClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
GtkButtonClass *button_class = GTK_BUTTON_CLASS (klass);
parent_class = g_type_class_peek_parent (klass);
gimp_color_button_signals[COLOR_CHANGED] =
g_signal_new ("color-changed",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (GimpColorButtonClass, color_changed),
NULL, NULL, NULL,
G_TYPE_NONE, 0);
object_class->constructed = gimp_color_button_constructed;
object_class->finalize = gimp_color_button_finalize;
object_class->dispose = gimp_color_button_dispose;
object_class->get_property = gimp_color_button_get_property;
object_class->set_property = gimp_color_button_set_property;
widget_class->button_press_event = gimp_color_button_button_press;
widget_class->state_flags_changed = gimp_color_button_state_flags_changed;
button_class->clicked = gimp_color_button_clicked;
klass->color_changed = NULL;
klass->get_action_type = gimp_color_button_get_action_type;
/**
* GimpColorButton:title:
*
* The title to be used for the color selection dialog.
*
* Since: 2.4
*/
g_object_class_install_property (object_class, PROP_TITLE,
g_param_spec_string ("title",
"Title",
"The title to be used for the color selection dialog",
NULL,
GIMP_PARAM_READWRITE |
G_PARAM_CONSTRUCT));
/**
* GimpColorButton:color:
*
* The color displayed in the button's color area.
*
* Since: 2.4
*/
g_object_class_install_property (object_class, PROP_COLOR,
gimp_param_spec_color_from_string ("color",
"Color",
"The color displayed in the button's color area",
TRUE, "black",
GIMP_PARAM_READWRITE |
G_PARAM_CONSTRUCT));
/**
* GimpColorButton:type:
*
* The type of the button's color area.
*
* Since: 2.4
*/
g_object_class_install_property (object_class, PROP_TYPE,
g_param_spec_enum ("type",
"Type",
"The type of the button's color area",
GIMP_TYPE_COLOR_AREA_TYPE,
GIMP_COLOR_AREA_FLAT,
GIMP_PARAM_READWRITE |
G_PARAM_CONSTRUCT));
/**
* GimpColorButton:continuous-update:
*
* The update policy of the color button.
*
* Since: 2.4
*/
g_object_class_install_property (object_class, PROP_UPDATE,
g_param_spec_boolean ("continuous-update",
"Contiguous Update",
"The update policy of the color button",
FALSE,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT));
/**
* GimpColorButton:area-width:
*
* The minimum width of the button's #GimpColorArea.
*
* Since: 2.8
*/
g_object_class_install_property (object_class, PROP_AREA_WIDTH,
g_param_spec_int ("area-width",
"Area Width",
"The minimum width of the button's GimpColorArea",
1, G_MAXINT, 16,
G_PARAM_WRITABLE |
G_PARAM_CONSTRUCT));
/**
* GimpColorButton:area-height:
*
* The minimum height of the button's #GimpColorArea.
*
* Since: 2.8
*/
g_object_class_install_property (object_class, PROP_AREA_HEIGHT,
g_param_spec_int ("area-height",
"Area Height",
"The minimum height of the button's GimpColorArea",
1, G_MAXINT, 16,
G_PARAM_WRITABLE |
G_PARAM_CONSTRUCT));
/**
* GimpColorButton:color-config:
*
* The #GimpColorConfig object used for the button's #GimpColorArea
* and #GimpColorSelection.
*
* Since: 2.10
*/
g_object_class_install_property (object_class, PROP_COLOR_CONFIG,
g_param_spec_object ("color-config",
"Color Config",
"The color config object used",
GIMP_TYPE_COLOR_CONFIG,
G_PARAM_READWRITE));
}
static void
gimp_color_button_init (GimpColorButton *button)
{
GimpColorButtonPrivate *priv = GET_PRIVATE (button);
priv->color_area = g_object_new (GIMP_TYPE_COLOR_AREA,
"drag-mask", GDK_BUTTON1_MASK,
NULL);
g_signal_connect (priv->color_area, "color-changed",
G_CALLBACK (gimp_color_button_area_changed),
button);
gtk_container_add (GTK_CONTAINER (button), priv->color_area);
gtk_widget_show (priv->color_area);
}
static void
gimp_color_button_constructed (GObject *object)
{
GimpColorButton *button = GIMP_COLOR_BUTTON (object);
GimpColorButtonClass *klass = GIMP_COLOR_BUTTON_GET_CLASS (object);
GimpColorButtonPrivate *priv = GET_PRIVATE (object);
GMenu *section;
gint i;
G_OBJECT_CLASS (parent_class)->constructed (object);
priv->group = g_simple_action_group_new ();
for (i = 0; i < G_N_ELEMENTS (actions); i++)
{
GAction *action;
action = g_object_new (klass->get_action_type (button),
"name", actions[i].name,
NULL);
if (actions[i].callback)
g_signal_connect (action, "activate",
G_CALLBACK (actions[i].callback),
button);
g_action_map_add_action (G_ACTION_MAP (priv->group), action);
g_object_unref (action);
}
gtk_widget_insert_action_group (GTK_WIDGET (button),
GIMP_COLOR_BUTTON_GROUP_PREFIX,
G_ACTION_GROUP (priv->group));
/* right-click opens a popup */
priv->menu = g_menu_new ();
section = g_menu_new ();
g_menu_append (section, _("_Foreground Color"), GIMP_COLOR_BUTTON_GROUP_PREFIX "." GIMP_COLOR_BUTTON_COLOR_FG);
g_menu_append (section, _("_Background Color"), GIMP_COLOR_BUTTON_GROUP_PREFIX "." GIMP_COLOR_BUTTON_COLOR_BG);
g_menu_append_section (priv->menu, NULL, G_MENU_MODEL (section));
g_clear_object (§ion);
section = g_menu_new ();
g_menu_append (section, _("Blac_k"), GIMP_COLOR_BUTTON_GROUP_PREFIX "." GIMP_COLOR_BUTTON_COLOR_BLACK);
g_menu_append (section, _("_White"), GIMP_COLOR_BUTTON_GROUP_PREFIX "." GIMP_COLOR_BUTTON_COLOR_WHITE);
g_menu_append_section (priv->menu, NULL, G_MENU_MODEL (section));
g_clear_object (§ion);
}
static void
gimp_color_button_finalize (GObject *object)
{
GimpColorButtonPrivate *priv = GET_PRIVATE (object);
g_clear_pointer (&priv->title, g_free);
g_clear_object (&priv->menu);
g_clear_object (&priv->group);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gimp_color_button_dispose (GObject *object)
{
GimpColorButton *button = GIMP_COLOR_BUTTON (object);
GimpColorButtonPrivate *priv = GET_PRIVATE (button);
g_clear_pointer (&priv->dialog, gtk_widget_destroy);
priv->selection = NULL;
g_clear_pointer (&priv->color_area, gtk_widget_destroy);
gimp_color_button_set_color_config (button, NULL);
G_OBJECT_CLASS (parent_class)->dispose (object);
}
static void
gimp_color_button_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
GimpColorButtonPrivate *priv = GET_PRIVATE (object);
switch (property_id)
{
case PROP_TITLE:
g_value_set_string (value, priv->title);
break;
case PROP_COLOR:
g_object_get_property (G_OBJECT (priv->color_area), "color", value);
break;
case PROP_TYPE:
g_object_get_property (G_OBJECT (priv->color_area), "type", value);
break;
case PROP_UPDATE:
g_value_set_boolean (value, priv->continuous_update);
break;
case PROP_COLOR_CONFIG:
g_value_set_object (value, priv->config);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
gimp_color_button_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
GimpColorButton *button = GIMP_COLOR_BUTTON (object);
GimpColorButtonPrivate *priv = GET_PRIVATE (object);
gint other;
switch (property_id)
{
case PROP_TITLE:
gimp_color_button_set_title (button, g_value_get_string (value));
break;
case PROP_COLOR:
g_object_set_property (G_OBJECT (priv->color_area), "color", value);
break;
case PROP_TYPE:
g_object_set_property (G_OBJECT (priv->color_area), "type", value);
break;
case PROP_UPDATE:
gimp_color_button_set_update (button, g_value_get_boolean (value));
break;
case PROP_AREA_WIDTH:
gtk_widget_get_size_request (priv->color_area, NULL, &other);
gtk_widget_set_size_request (priv->color_area,
g_value_get_int (value), other);
break;
case PROP_AREA_HEIGHT:
gtk_widget_get_size_request (priv->color_area, &other, NULL);
gtk_widget_set_size_request (priv->color_area,
other, g_value_get_int (value));
break;
case PROP_COLOR_CONFIG:
gimp_color_button_set_color_config (button, g_value_get_object (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static gboolean
gimp_color_button_button_press (GtkWidget *widget,
GdkEventButton *bevent)
{
GimpColorButtonPrivate *priv = GET_PRIVATE (widget);
if (gdk_event_triggers_context_menu ((GdkEvent *) bevent))
{
GtkWidget *menu;
menu = gtk_menu_new_from_model (G_MENU_MODEL (priv->menu));
/*gtk_menu_set_screen (GTK_MENU (menu), gtk_widget_get_screen (widget));*/
gtk_menu_attach_to_widget (GTK_MENU (menu), widget, NULL);
gtk_menu_popup_at_pointer (GTK_MENU (menu), (GdkEvent *) bevent);
}
return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, bevent);
}
static void
gimp_color_button_state_flags_changed (GtkWidget *widget,
GtkStateFlags previous_state)
{
GimpColorButtonPrivate *priv = GET_PRIVATE (widget);
if (! gtk_widget_is_sensitive (widget) && priv->dialog)
gtk_widget_hide (priv->dialog);
if (GTK_WIDGET_CLASS (parent_class)->state_flags_changed)
GTK_WIDGET_CLASS (parent_class)->state_flags_changed (widget,
previous_state);
}
static void
gimp_color_button_clicked (GtkButton *button)
{
GimpColorButton *color_button = GIMP_COLOR_BUTTON (button);
GimpColorButtonPrivate *priv = GET_PRIVATE (button);
GeglColor *color;
if (! priv->dialog)
{
GtkWidget *dialog;
dialog = priv->dialog =
gimp_dialog_new (priv->title, "gimp-color-button",
gtk_widget_get_toplevel (GTK_WIDGET (button)), 0,
gimp_color_button_help_func, NULL,
_("_Reset"), RESPONSE_RESET,
_("_Cancel"), GTK_RESPONSE_CANCEL,
_("_OK"), GTK_RESPONSE_OK,
NULL);
g_object_set_data (G_OBJECT (dialog), COLOR_BUTTON_KEY, button);
gimp_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
RESPONSE_RESET,
GTK_RESPONSE_OK,
GTK_RESPONSE_CANCEL,
-1);
g_signal_connect (dialog, "response",
G_CALLBACK (gimp_color_button_dialog_response),
color_button);
g_signal_connect (dialog, "destroy",
G_CALLBACK (gtk_widget_destroyed),
&priv->dialog);
priv->selection = gimp_color_selection_new ();
gtk_container_set_border_width (GTK_CONTAINER (priv->selection), 6);
gimp_color_selection_set_show_alpha (GIMP_COLOR_SELECTION (priv->selection),
gimp_color_button_has_alpha (color_button));
gimp_color_selection_set_config (GIMP_COLOR_SELECTION (priv->selection),
priv->config);
gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
priv->selection, TRUE, TRUE, 0);
gtk_widget_show (priv->selection);
g_signal_connect (priv->selection, "color-changed",
G_CALLBACK (gimp_color_button_selection_changed),
button);
}
color = gimp_color_button_get_color (color_button);
g_signal_handlers_block_by_func (priv->selection,
gimp_color_button_selection_changed,
button);
gimp_color_selection_set_color (GIMP_COLOR_SELECTION (priv->selection), color);
gimp_color_selection_set_old_color (GIMP_COLOR_SELECTION (priv->selection), color);
g_signal_handlers_unblock_by_func (priv->selection,
gimp_color_button_selection_changed,
button);
gtk_window_present (GTK_WINDOW (priv->dialog));
g_object_unref (color);
}
static GType
gimp_color_button_get_action_type (GimpColorButton *button)
{
return G_TYPE_SIMPLE_ACTION;
}
/* public functions */
/**
* gimp_color_button_new:
* @title: String that will be used as title for the color_selector.
* @width: Width of the colorpreview in pixels.
* @height: Height of the colorpreview in pixels.
* @color: A [class@Gegl.Color].
* @type: The type of transparency to be displayed.
*
* Creates a new #GimpColorButton widget.
*
* This returns a button with a preview showing the color.
* When the button is clicked a GtkColorSelectionDialog is opened.
* If the user changes the color the new color is written into the
* array that was used to pass the initial color and the "color-changed"
* signal is emitted.
*
* Returns: Pointer to the new #GimpColorButton widget.
**/
GtkWidget *
gimp_color_button_new (const gchar *title,
gint width,
gint height,
GeglColor *color,
GimpColorAreaType type)
{
g_return_val_if_fail (GEGL_IS_COLOR (color), NULL);
g_return_val_if_fail (width > 0, NULL);
g_return_val_if_fail (height > 0, NULL);
return g_object_new (GIMP_TYPE_COLOR_BUTTON,
"title", title,
"type", type,
"color", color,
"area-width", width,
"area-height", height,
NULL);
}
/**
* gimp_color_button_set_title:
* @button: a #GimpColorButton.
* @title: the new title.
*
* Sets the @button dialog's title.
*
* Since: 2.10
**/
void
gimp_color_button_set_title (GimpColorButton *button,
const gchar *title)
{
GimpColorButtonPrivate *priv;
g_return_if_fail (GIMP_IS_COLOR_BUTTON (button));
g_return_if_fail (title != NULL);
priv = GET_PRIVATE (button);
g_free (priv->title);
priv->title = g_strdup (title);
if (priv->dialog)
gtk_window_set_title (GTK_WINDOW (priv->dialog), title);
g_object_notify (G_OBJECT (button), "title");
}
/**
* gimp_color_button_get_title:
* @button: a #GimpColorButton.
*
* Returns: The @button dialog's title.
*
* Since: 2.10
**/
const gchar *
gimp_color_button_get_title (GimpColorButton *button)
{
GimpColorButtonPrivate *priv;
g_return_val_if_fail (GIMP_IS_COLOR_BUTTON (button), NULL);
priv = GET_PRIVATE (button);
return priv->title;
}
/**
* gimp_color_button_set_color:
* @button: Pointer to a #GimpColorButton.
* @color: A new [class@Gegl.Color].
*
* Sets the @button to the given @color.
**/
void
gimp_color_button_set_color (GimpColorButton *button,
GeglColor *color)
{
GimpColorButtonPrivate *priv;
g_return_if_fail (GIMP_IS_COLOR_BUTTON (button));
g_return_if_fail (GEGL_IS_COLOR (color));
priv = GET_PRIVATE (button);
gimp_color_area_set_color (GIMP_COLOR_AREA (priv->color_area), color);
g_object_notify (G_OBJECT (button), "color");
}
/**
* gimp_color_button_get_color:
* @button: Pointer to a #GimpColorButton.
*
* Retrieves the currently set color from the @button.
*
* Returns: (transfer full): a copy of @button's [class@Gegl.Color].
**/
GeglColor *
gimp_color_button_get_color (GimpColorButton *button)
{
GimpColorButtonPrivate *priv;
g_return_val_if_fail (GIMP_IS_COLOR_BUTTON (button), NULL);
priv = GET_PRIVATE (button);
return gimp_color_area_get_color (GIMP_COLOR_AREA (priv->color_area));
}
/**
* gimp_color_button_has_alpha:
* @button: Pointer to a #GimpColorButton.
*
* Checks whether the @buttons shows transparency information.
*
* Returns: %TRUE if the @button shows transparency information, %FALSE
* otherwise.
**/
gboolean
gimp_color_button_has_alpha (GimpColorButton *button)
{
GimpColorButtonPrivate *priv;
g_return_val_if_fail (GIMP_IS_COLOR_BUTTON (button), FALSE);
priv = GET_PRIVATE (button);
return gimp_color_area_has_alpha (GIMP_COLOR_AREA (priv->color_area));
}
/**
* gimp_color_button_set_type:
* @button: Pointer to a #GimpColorButton.
* @type: the new #GimpColorAreaType
*
* Sets the @button to the given @type. See also gimp_color_area_set_type().
**/
void
gimp_color_button_set_type (GimpColorButton *button,
GimpColorAreaType type)
{
GimpColorButtonPrivate *priv;
g_return_if_fail (GIMP_IS_COLOR_BUTTON (button));
priv = GET_PRIVATE (button);
gimp_color_area_set_type (GIMP_COLOR_AREA (priv->color_area), type);
g_object_notify (G_OBJECT (button), "type");
}
/**
* gimp_color_button_get_update:
* @button: A #GimpColorButton widget.
*
* Returns the color button's @continuous_update property.
*
* Returns: the @continuous_update property.
**/
gboolean
gimp_color_button_get_update (GimpColorButton *button)
{
GimpColorButtonPrivate *priv;
g_return_val_if_fail (GIMP_IS_COLOR_BUTTON (button), FALSE);
priv = GET_PRIVATE (button);
return priv->continuous_update;
}
/**
* gimp_color_button_set_update:
* @button: A #GimpColorButton widget.
* @continuous: The new setting of the @continuous_update property.
*
* When set to %TRUE, the @button will emit the "color-changed"
* continuously while the color is changed in the color selection
* dialog.
**/
void
gimp_color_button_set_update (GimpColorButton *button,
gboolean continuous)
{
GimpColorButtonPrivate *priv;
g_return_if_fail (GIMP_IS_COLOR_BUTTON (button));
priv = GET_PRIVATE (button);
if (continuous != priv->continuous_update)
{
priv->continuous_update = continuous ? TRUE : FALSE;
if (priv->selection)
{
GeglColor *color;
if (priv->continuous_update)
color = gimp_color_selection_get_color (GIMP_COLOR_SELECTION (priv->selection));
else
color = gimp_color_selection_get_old_color (GIMP_COLOR_SELECTION (priv->selection));
gimp_color_button_set_color (button, color);
g_object_unref (color);
}
g_object_notify (G_OBJECT (button), "continuous-update");
}
}
/**
* gimp_color_button_set_color_config:
* @button: a #GimpColorButton widget.
* @config: a #GimpColorConfig object.
*
* Sets the color management configuration to use with this color button's
* #GimpColorArea.
*
* Since: 2.10
*/
void
gimp_color_button_set_color_config (GimpColorButton *button,
GimpColorConfig *config)
{
GimpColorButtonPrivate *priv;
g_return_if_fail (GIMP_IS_COLOR_BUTTON (button));
g_return_if_fail (config == NULL || GIMP_IS_COLOR_CONFIG (config));
priv = GET_PRIVATE (button);
if (g_set_object (&priv->config, config))
{
if (priv->color_area)
gimp_color_area_set_color_config (GIMP_COLOR_AREA (priv->color_area),
priv->config);
if (priv->selection)
gimp_color_selection_set_config (GIMP_COLOR_SELECTION (priv->selection),
priv->config);
}
}
/**
* gimp_color_button_get_action_group:
* @button: a #GimpColorButton.
*
* Returns: (transfer none): The @button's #GSimpleActionGroup.
*
* Since: 3.0
**/
GSimpleActionGroup *
gimp_color_button_get_action_group (GimpColorButton *button)
{
GimpColorButtonPrivate *priv;
g_return_val_if_fail (GIMP_IS_COLOR_BUTTON (button), NULL);
priv = GET_PRIVATE (button);
return priv->group;
}
/* private functions */
static void
gimp_color_button_dialog_response (GtkWidget *dialog,
gint response_id,
GimpColorButton *button)
{
GimpColorButtonPrivate *priv = GET_PRIVATE (button);
GeglColor *color = NULL;
switch (response_id)
{
case RESPONSE_RESET:
gimp_color_selection_reset (GIMP_COLOR_SELECTION (priv->selection));
break;
case GTK_RESPONSE_OK:
if (! priv->continuous_update)
{
color = gimp_color_selection_get_color (GIMP_COLOR_SELECTION (priv->selection));
gimp_color_button_set_color (button, color);
}
gtk_widget_hide (dialog);
break;
default:
if (priv->continuous_update)
{
color = gimp_color_selection_get_old_color (GIMP_COLOR_SELECTION (priv->selection)),
gimp_color_button_set_color (button, color);
}
gtk_widget_hide (dialog);
break;
}
g_clear_object (&color);
}
static void
gimp_color_button_use_color (GAction *action,
GVariant *parameter,
GimpColorButton *button)
{
const gchar *name;
GeglColor *color;
name = g_action_get_name (action);
if (! strcmp (name, GIMP_COLOR_BUTTON_COLOR_FG))
{
if (_gimp_get_foreground_func)
color = _gimp_get_foreground_func ();
else
color = gegl_color_new ("black");
}
else if (! strcmp (name, GIMP_COLOR_BUTTON_COLOR_BG))
{
if (_gimp_get_background_func)
color = _gimp_get_background_func ();
else
color = gegl_color_new ("white");
}
else if (! strcmp (name, GIMP_COLOR_BUTTON_COLOR_BLACK))
{
color = gegl_color_new ("black");
}
else if (! strcmp (name, GIMP_COLOR_BUTTON_COLOR_WHITE))
{
color = gegl_color_new ("white");
}
else
{
color = gimp_color_button_get_color (button);
}
gimp_color_button_set_color (button, color);
g_clear_object (&color);
}
static void
gimp_color_button_area_changed (GtkWidget *color_area,
GimpColorButton *button)
{
GimpColorButtonPrivate *priv = GET_PRIVATE (button);
if (priv->selection)
{
GeglColor *color;
color = gimp_color_button_get_color (button);
g_signal_handlers_block_by_func (priv->selection,
gimp_color_button_selection_changed,
button);
gimp_color_selection_set_color (GIMP_COLOR_SELECTION (priv->selection), color);
g_signal_handlers_unblock_by_func (priv->selection,
gimp_color_button_selection_changed,
button);
g_object_unref (color);
}
g_signal_emit (button, gimp_color_button_signals[COLOR_CHANGED], 0);
}
static void
gimp_color_button_selection_changed (GtkWidget *selection,
GimpColorButton *button)
{
GimpColorButtonPrivate *priv = GET_PRIVATE (button);
if (priv->continuous_update)
{
GeglColor *color;
color = gimp_color_selection_get_color (GIMP_COLOR_SELECTION (selection));
g_signal_handlers_block_by_func (priv->color_area,
gimp_color_button_area_changed,
button);
gimp_color_area_set_color (GIMP_COLOR_AREA (priv->color_area), color);
g_signal_handlers_unblock_by_func (priv->color_area,
gimp_color_button_area_changed,
button);
g_signal_emit (button, gimp_color_button_signals[COLOR_CHANGED], 0);
g_object_unref (color);
}
}
static void
gimp_color_button_help_func (const gchar *help_id,
gpointer help_data)
{
GimpColorButton *button;
GimpColorButtonPrivate *priv;
GimpColorSelection *selection;
GimpColorNotebook *notebook;
GimpColorSelector *current;
button = g_object_get_data (G_OBJECT (help_data), COLOR_BUTTON_KEY);
priv = GET_PRIVATE (button);
selection = GIMP_COLOR_SELECTION (priv->selection);
notebook = GIMP_COLOR_NOTEBOOK (gimp_color_selection_get_notebook (selection));
current = gimp_color_notebook_get_current_selector (notebook);
help_id = GIMP_COLOR_SELECTOR_GET_CLASS (current)->help_id;
gimp_standard_help_func (help_id, NULL);
}
|