1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
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  * Lesser 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 <http://www.gnu.org/licenses/>.
16  */
17 
18 /*
19  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20  * file for a list of people on the GTK+ Team.  See the ChangeLog
21  * files for a list of changes.  These files are distributed with
22  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
23  */
24 #include "config.h"
25 
26 #define GDK_DISABLE_DEPRECATION_WARNINGS
27 
28 #include <string.h>
29 #include <glib.h>
30 #include "gtkcolorseldialog.h"
31 #include "gtkframe.h"
32 #include "gtkbutton.h"
33 #include "gtkstock.h"
34 #include "gtkintl.h"
35 #include "gtkbuildable.h"
36 
37 
38 /**
39  * SECTION:gtkcolorseldlg
40  * @Short_description: Deprecated dialog box for selecting a color
41  * @Title: GtkColorSelectionDialog
42  *
43  * The #GtkColorSelectionDialog provides a standard dialog which
44  * allows the user to select a color much like the #GtkFileChooserDialog
45  * provides a standard dialog for file selection.
46  *
47  * Use gtk_color_selection_dialog_get_color_selection() to get the
48  * #GtkColorSelection widget contained within the dialog. Use this widget
49  * and its gtk_color_selection_get_current_color()
50  * function to gain access to the selected color.  Connect a handler
51  * for this widget’s #GtkColorSelection::color-changed signal to be notified
52  * when the color changes.
53  *
54  * # GtkColorSelectionDialog as GtkBuildable # {#GtkColorSelectionDialog-BUILDER-UI}
55  *
56  * The GtkColorSelectionDialog implementation of the GtkBuildable interface
57  * exposes the embedded #GtkColorSelection as internal child with the
58  * name “color_selection”. It also exposes the buttons with the names
59  * “ok_button”, “cancel_button” and “help_button”.
60  */
61 
62 
63 struct _GtkColorSelectionDialogPrivate
64 {
65   GtkWidget *colorsel;
66   GtkWidget *ok_button;
67   GtkWidget *cancel_button;
68   GtkWidget *help_button;
69 };
70 
71 enum {
72   PROP_0,
73   PROP_COLOR_SELECTION,
74   PROP_OK_BUTTON,
75   PROP_CANCEL_BUTTON,
76   PROP_HELP_BUTTON
77 };
78 
79 
80 /***************************/
81 /* GtkColorSelectionDialog */
82 /***************************/
83 
84 static void gtk_color_selection_dialog_buildable_interface_init     (GtkBuildableIface *iface);
85 static GObject * gtk_color_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
86 									  GtkBuilder   *builder,
87 									  const gchar  *childname);
88 
89 G_DEFINE_TYPE_WITH_CODE (GtkColorSelectionDialog, gtk_color_selection_dialog, GTK_TYPE_DIALOG,
90                          G_ADD_PRIVATE (GtkColorSelectionDialog)
91                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
92                                                 gtk_color_selection_dialog_buildable_interface_init))
93 
94 static GtkBuildableIface *parent_buildable_iface;
95 
96 static void
gtk_color_selection_dialog_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)97 gtk_color_selection_dialog_get_property (GObject         *object,
98 					 guint            prop_id,
99 					 GValue          *value,
100 					 GParamSpec      *pspec)
101 {
102   GtkColorSelectionDialog *colorsel = GTK_COLOR_SELECTION_DIALOG (object);
103   GtkColorSelectionDialogPrivate *priv = colorsel->priv;
104 
105   switch (prop_id)
106     {
107     case PROP_COLOR_SELECTION:
108       g_value_set_object (value, priv->colorsel);
109       break;
110     case PROP_OK_BUTTON:
111       g_value_set_object (value, priv->ok_button);
112       break;
113     case PROP_CANCEL_BUTTON:
114       g_value_set_object (value, priv->cancel_button);
115       break;
116     case PROP_HELP_BUTTON:
117       g_value_set_object (value, priv->help_button);
118       break;
119     default:
120       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
121       break;
122     }
123 }
124 
125 static void
gtk_color_selection_dialog_class_init(GtkColorSelectionDialogClass * klass)126 gtk_color_selection_dialog_class_init (GtkColorSelectionDialogClass *klass)
127 {
128   GObjectClass   *gobject_class = G_OBJECT_CLASS (klass);
129   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
130 
131   gobject_class->get_property = gtk_color_selection_dialog_get_property;
132 
133   g_object_class_install_property (gobject_class,
134 				   PROP_COLOR_SELECTION,
135 				   g_param_spec_object ("color-selection",
136 						     P_("Color Selection"),
137 						     P_("The color selection embedded in the dialog."),
138 						     GTK_TYPE_WIDGET,
139 						     G_PARAM_READABLE));
140   g_object_class_install_property (gobject_class,
141 				   PROP_OK_BUTTON,
142 				   g_param_spec_object ("ok-button",
143 						     P_("OK Button"),
144 						     P_("The OK button of the dialog."),
145 						     GTK_TYPE_WIDGET,
146 						     G_PARAM_READABLE));
147   g_object_class_install_property (gobject_class,
148 				   PROP_CANCEL_BUTTON,
149 				   g_param_spec_object ("cancel-button",
150 						     P_("Cancel Button"),
151 						     P_("The cancel button of the dialog."),
152 						     GTK_TYPE_WIDGET,
153 						     G_PARAM_READABLE));
154   g_object_class_install_property (gobject_class,
155 				   PROP_HELP_BUTTON,
156 				   g_param_spec_object ("help-button",
157 						     P_("Help Button"),
158 						     P_("The help button of the dialog."),
159 						     GTK_TYPE_WIDGET,
160 						     G_PARAM_READABLE));
161 
162   gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_COLOR_CHOOSER);
163 }
164 
165 static void
gtk_color_selection_dialog_init(GtkColorSelectionDialog * colorseldiag)166 gtk_color_selection_dialog_init (GtkColorSelectionDialog *colorseldiag)
167 {
168   GtkColorSelectionDialogPrivate *priv;
169   GtkDialog *dialog = GTK_DIALOG (colorseldiag);
170   GtkWidget *action_area, *content_area;
171 
172   colorseldiag->priv = gtk_color_selection_dialog_get_instance_private (colorseldiag);
173   priv = colorseldiag->priv;
174 
175   content_area = gtk_dialog_get_content_area (dialog);
176   action_area = gtk_dialog_get_action_area (dialog);
177 
178   gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
179   gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
180   gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
181   gtk_box_set_spacing (GTK_BOX (action_area), 6);
182 
183   priv->colorsel = gtk_color_selection_new ();
184   gtk_container_set_border_width (GTK_CONTAINER (priv->colorsel), 5);
185   gtk_color_selection_set_has_palette (GTK_COLOR_SELECTION (priv->colorsel), FALSE);
186   gtk_color_selection_set_has_opacity_control (GTK_COLOR_SELECTION (priv->colorsel), FALSE);
187   gtk_container_add (GTK_CONTAINER (content_area), priv->colorsel);
188   gtk_widget_show (priv->colorsel);
189 
190   priv->cancel_button = gtk_dialog_add_button (dialog,
191                                                _("_Cancel"),
192                                                GTK_RESPONSE_CANCEL);
193 
194   priv->ok_button = gtk_dialog_add_button (dialog,
195                                            _("_Select"),
196                                            GTK_RESPONSE_OK);
197 
198   gtk_widget_grab_default (priv->ok_button);
199 
200   priv->help_button = gtk_dialog_add_button (dialog,
201                                              _("_Help"),
202                                              GTK_RESPONSE_HELP);
203 
204   gtk_widget_hide (priv->help_button);
205 
206   gtk_dialog_set_alternative_button_order (dialog,
207 					   GTK_RESPONSE_OK,
208 					   GTK_RESPONSE_CANCEL,
209 					   GTK_RESPONSE_HELP,
210 					   -1);
211 
212   gtk_window_set_title (GTK_WINDOW (colorseldiag),
213                         _("Color Selection"));
214 }
215 
216 /**
217  * gtk_color_selection_dialog_new:
218  * @title: a string containing the title text for the dialog.
219  *
220  * Creates a new #GtkColorSelectionDialog.
221  *
222  * Returns: a #GtkColorSelectionDialog.
223  */
224 GtkWidget*
gtk_color_selection_dialog_new(const gchar * title)225 gtk_color_selection_dialog_new (const gchar *title)
226 {
227   GtkColorSelectionDialog *colorseldiag;
228 
229   colorseldiag = g_object_new (GTK_TYPE_COLOR_SELECTION_DIALOG, NULL);
230 
231   if (title)
232     gtk_window_set_title (GTK_WINDOW (colorseldiag), title);
233 
234   gtk_window_set_resizable (GTK_WINDOW (colorseldiag), FALSE);
235 
236   return GTK_WIDGET (colorseldiag);
237 }
238 
239 /**
240  * gtk_color_selection_dialog_get_color_selection:
241  * @colorsel: a #GtkColorSelectionDialog
242  *
243  * Retrieves the #GtkColorSelection widget embedded in the dialog.
244  *
245  * Returns: (transfer none): the embedded #GtkColorSelection
246  *
247  * Since: 2.14
248  **/
249 GtkWidget*
gtk_color_selection_dialog_get_color_selection(GtkColorSelectionDialog * colorsel)250 gtk_color_selection_dialog_get_color_selection (GtkColorSelectionDialog *colorsel)
251 {
252   g_return_val_if_fail (GTK_IS_COLOR_SELECTION_DIALOG (colorsel), NULL);
253 
254   return colorsel->priv->colorsel;
255 }
256 
257 static void
gtk_color_selection_dialog_buildable_interface_init(GtkBuildableIface * iface)258 gtk_color_selection_dialog_buildable_interface_init (GtkBuildableIface *iface)
259 {
260   parent_buildable_iface = g_type_interface_peek_parent (iface);
261   iface->get_internal_child = gtk_color_selection_dialog_buildable_get_internal_child;
262 }
263 
264 static GObject *
gtk_color_selection_dialog_buildable_get_internal_child(GtkBuildable * buildable,GtkBuilder * builder,const gchar * childname)265 gtk_color_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
266 							 GtkBuilder   *builder,
267 							 const gchar  *childname)
268 {
269   GtkColorSelectionDialog *selection_dialog = GTK_COLOR_SELECTION_DIALOG (buildable);
270   GtkColorSelectionDialogPrivate *priv = selection_dialog->priv;
271 
272   if (g_strcmp0 (childname, "ok_button") == 0)
273     return G_OBJECT (priv->ok_button);
274   else if (g_strcmp0 (childname, "cancel_button") == 0)
275     return G_OBJECT (priv->cancel_button);
276   else if (g_strcmp0 (childname, "help_button") == 0)
277     return G_OBJECT (priv->help_button);
278   else if (g_strcmp0 (childname, "color_selection") == 0)
279     return G_OBJECT (priv->colorsel);
280 
281   return parent_buildable_iface->get_internal_child (buildable, builder, childname);
282 }
283