1 /* LIBGIMP - The GIMP Library
2  * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
3  *
4  * gimppickbutton.c
5  * Copyright (C) 2002 Michael Natterer <mitch@gimp.org>
6  *
7  * based on gtk+/gtk/gtkcolorsel.c
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library.  If not, see
16  * <https://www.gnu.org/licenses/>.
17  */
18 
19 #include "config.h"
20 
21 #include <gegl.h>
22 #include <gtk/gtk.h>
23 #include <gdk/gdkkeysyms.h>
24 
25 #include "libgimpcolor/gimpcolor.h"
26 
27 #include "gimpwidgetstypes.h"
28 
29 #include "gimpcairo-utils.h"
30 #include "gimphelpui.h"
31 #include "gimpicons.h"
32 #include "gimppickbutton.h"
33 #include "gimppickbutton-default.h"
34 #include "gimppickbutton-kwin.h"
35 #include "gimppickbutton-xdg.h"
36 
37 #ifdef GDK_WINDOWING_QUARTZ
38 #include "gimppickbutton-quartz.h"
39 #endif
40 
41 #include "libgimp/libgimp-intl.h"
42 
43 /**
44  * SECTION: gimppickbutton
45  * @title: GimpPickButton
46  * @short_description: Widget to pick a color from screen.
47  *
48  * #GimpPickButton is a specialized button. When clicked, it changes
49  * the cursor to a color-picker pipette and allows the user to pick a
50  * color from any point on the screen.
51  **/
52 
53 
54 enum
55 {
56   COLOR_PICKED,
57   LAST_SIGNAL
58 };
59 
60 static void       gimp_pick_button_dispose         (GObject        *object);
61 
62 static void       gimp_pick_button_clicked         (GtkButton      *button);
63 
64 
65 G_DEFINE_TYPE (GimpPickButton, gimp_pick_button, GTK_TYPE_BUTTON)
66 
67 #define parent_class gimp_pick_button_parent_class
68 
69 static guint pick_button_signals[LAST_SIGNAL] = { 0 };
70 
71 
72 static void
gimp_pick_button_class_init(GimpPickButtonClass * klass)73 gimp_pick_button_class_init (GimpPickButtonClass* klass)
74 {
75   GObjectClass   *object_class = G_OBJECT_CLASS (klass);
76   GtkButtonClass *button_class = GTK_BUTTON_CLASS (klass);
77 
78   /**
79    * GimpPickButton::color-picked:
80    * @gimppickbutton: the object which received the signal.
81    * @arg1: pointer to a #GimpRGB structure that holds the picked color
82    *
83    * This signal is emitted when the user has picked a color.
84    **/
85   pick_button_signals[COLOR_PICKED] =
86     g_signal_new ("color-picked",
87                   G_TYPE_FROM_CLASS (klass),
88                   G_SIGNAL_RUN_FIRST,
89                   G_STRUCT_OFFSET (GimpPickButtonClass, color_picked),
90                   NULL, NULL,
91                   g_cclosure_marshal_VOID__POINTER,
92                   G_TYPE_NONE, 1,
93                   G_TYPE_POINTER);
94 
95   object_class->dispose = gimp_pick_button_dispose;
96 
97   button_class->clicked = gimp_pick_button_clicked;
98 
99   klass->color_picked   = NULL;
100 }
101 
102 static void
gimp_pick_button_init(GimpPickButton * button)103 gimp_pick_button_init (GimpPickButton *button)
104 {
105   GtkWidget *image;
106 
107   image = gtk_image_new_from_icon_name (GIMP_ICON_COLOR_PICK_FROM_SCREEN,
108                                         GTK_ICON_SIZE_BUTTON);
109   gtk_container_add (GTK_CONTAINER (button), image);
110   gtk_widget_show (image);
111 
112   gimp_help_set_help_data (GTK_WIDGET (button),
113                            _("Click the eyedropper, then click a color "
114                              "anywhere on your screen to select that color."),
115                            NULL);
116 }
117 
118 static void
gimp_pick_button_dispose(GObject * object)119 gimp_pick_button_dispose (GObject *object)
120 {
121   GimpPickButton *button = GIMP_PICK_BUTTON (object);
122 
123   if (button->cursor)
124     {
125       gdk_cursor_unref (button->cursor);
126       button->cursor = NULL;
127     }
128 
129   if (button->grab_widget)
130     {
131       gtk_widget_destroy (button->grab_widget);
132       button->grab_widget = NULL;
133     }
134 
135   G_OBJECT_CLASS (parent_class)->dispose (object);
136 }
137 
138 static void
gimp_pick_button_clicked(GtkButton * button)139 gimp_pick_button_clicked (GtkButton *button)
140 {
141 #ifdef GDK_WINDOWING_QUARTZ
142   _gimp_pick_button_quartz_pick (GIMP_PICK_BUTTON (button));
143 #else
144   if (_gimp_pick_button_xdg_available ())
145     _gimp_pick_button_xdg_pick (GIMP_PICK_BUTTON (button));
146   else if (_gimp_pick_button_kwin_available ())
147     _gimp_pick_button_kwin_pick (GIMP_PICK_BUTTON (button));
148   else
149     _gimp_pick_button_default_pick (GIMP_PICK_BUTTON (button));
150 #endif
151 }
152 
153 
154 /*  public functions  */
155 
156 /**
157  * gimp_pick_button_new:
158  *
159  * Creates a new #GimpPickButton widget.
160  *
161  * Returns: A new #GimpPickButton widget.
162  **/
163 GtkWidget *
gimp_pick_button_new(void)164 gimp_pick_button_new (void)
165 {
166   return g_object_new (GIMP_TYPE_PICK_BUTTON, NULL);
167 }
168