summaryrefslogtreecommitdiff
path: root/src/color.c
blob: d1f7c8e4ab12c667953f4a16b7c24eddb2fd5269 (plain)
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
/* vim: colorcolumn=80 ts=4 sw=4
 */
/*
 * color.c
 *
 * Copyright © 2010 Berislav Kovacki
 * Copyright © 2021-2023 Logan Rathbone
 *
 * 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 2 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
 * Lesser 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, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * Authors: Berislav Kovacki <pantokrator@pantokrator.net>
 */

#include "config.h"

#include "util.h"
#include "zenity.h"
#include <string.h>

/* TODO: port to GtkColorDialog */
G_GNUC_BEGIN_IGNORE_DEPRECATIONS

static ZenityData *zen_data;

static void zenity_colorselection_dialog_response (GtkWidget *widget, int response, gpointer data);

void
zenity_colorselection (ZenityData *data, ZenityColorData *color_data)
{
	GtkWidget *dialog;
	GdkRGBA color;

	zen_data = data;

	dialog = gtk_color_chooser_dialog_new (data->dialog_title, NULL);

	g_signal_connect (dialog, "response", G_CALLBACK (zenity_colorselection_dialog_response), color_data);

	if (color_data->color &&
			gdk_rgba_parse (&color, color_data->color))
	{
		gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER(dialog), &color);
	}

	if (data->extra_label)
	{
		ZENITY_UTIL_ADD_EXTRA_LABELS (dialog)
	}

	if (data->modal)
		gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);

	g_object_set (dialog, "show-editor", !color_data->show_palette, NULL);

	zenity_util_show_dialog (dialog);

	if (data->timeout_delay > 0) {
		g_timeout_add_seconds (data->timeout_delay,
			(GSourceFunc) zenity_util_timeout_handle,
			dialog);
	}
	zenity_util_gapp_main (GTK_WINDOW(dialog));
}

static void
zenity_colorselection_dialog_response (GtkWidget *widget, int response, gpointer data)
{
	GdkRGBA color;

	switch (response)
	{
		case GTK_RESPONSE_OK:
			zenity_util_exit_code_with_data (ZENITY_OK, zen_data);
			gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (widget), &color);
			g_print ("%s\n", gdk_rgba_to_string (&color));
			break;

		case GTK_RESPONSE_CANCEL:
			zen_data->exit_code = zenity_util_return_exit_code (ZENITY_CANCEL);
			break;

		default:
			if (zen_data->extra_label &&
				response < (int)g_strv_length (zen_data->extra_label))
				printf ("%s\n", zen_data->extra_label[response]);
			zen_data->exit_code = zenity_util_return_exit_code (ZENITY_ESC);
			break;
	}
	zenity_util_gapp_quit (GTK_WINDOW(widget), zen_data);
}

G_GNUC_END_IGNORE_DEPRECATIONS