Debian Codegraph @

libcolord-gtk/cd-sample-window-gtk4.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2021 Georges Basile Stavracas Neto <[email protected]>
4 * 2009-2012 Richard Hughes <[email protected]>
5 *
6 * Licensed under the GNU Lesser General Public License Version 2.1
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include "config.h"
24
25 #include <glib-object.h>
26
27 #include <colord.h>
28
29 #include "cd-sample-widget.h"
30 #include "cd-sample-window.h"
31
32 static void cd_sample_window_finalize (GObject *object);
33
34 #define CD_SAMPLE_WINDOW_PULSE_DELAY 80 /* ms */
35
36 /**
37 * CdSampleWindowPrivate:
38 *
39 * Private #CdSampleWindow data
40 **/
41 struct _CdSampleWindowPrivate
42 {
43 GtkWidget *sample_widget;
44 GtkWidget *progress_bar;
45 guint pulse_id;
46 };
47
48 G_DEFINE_TYPE_WITH_CODE (CdSampleWindow, cd_sample_window, GTK_TYPE_WINDOW,
49 G_ADD_PRIVATE (CdSampleWindow))
50
51 /**
52 * cd_sample_window_pulse_cb:
53 **/
54 static gboolean
55 cd_sample_window_pulse_cb (CdSampleWindow *sample_window)
56 {
57 CdSampleWindowPrivate *priv = cd_sample_window_get_instance_private (sample_window);
58 gtk_progress_bar_pulse (GTK_PROGRESS_BAR (priv->progress_bar));
59 return TRUE;
60 }
61
62 /**
63 * cd_sample_window_set_fraction:
64 * @sample_window: a valid #CdSampleWindow instance
65 * @fraction: the fraction value to show, or -1 for pulsing.
66 *
67 * Sets the percentage value on the window.
68 *
69 * Since: 0.1.22
70 **/
71 void
72 cd_sample_window_set_fraction (CdSampleWindow *sample_window,
73 gdouble fraction)
74 {
75 CdSampleWindowPrivate *priv = cd_sample_window_get_instance_private (sample_window);
76
77 /* make pulse */
78 if (fraction == -1) {
79 if (priv->pulse_id == 0) {
80 priv->pulse_id = g_timeout_add (CD_SAMPLE_WINDOW_PULSE_DELAY,
81 (GSourceFunc) cd_sample_window_pulse_cb,
82 sample_window);
83 }
84 return;
85 }
86
87 /* no more pulsing */
88 if (priv->pulse_id != 0) {
89 g_source_remove (priv->pulse_id);
90 priv->pulse_id = 0;
91 }
92
93 /* set static value */
94 gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (priv->progress_bar), fraction);
95 }
96
97 /**
98 * cd_sample_window_set_color:
99 * @sample_window: a valid #CdSampleWindow instance
100 * @color: the color
101 *
102 * Sets the window to a specific color.
103 *
104 * Since: 0.1.22
105 **/
106 void
107 cd_sample_window_set_color (CdSampleWindow *sample_window,
108 const CdColorRGB *color)
109 {
110 CdSampleWindowPrivate *priv = cd_sample_window_get_instance_private (sample_window);
111
112 g_debug ("setting RGB: %f, %f, %f", color->R, color->G, color->B);
113 cd_sample_widget_set_color (CD_SAMPLE_WIDGET (priv->sample_widget), color);
114
115 /* force redraw */
116 gtk_widget_set_visible (priv->sample_widget, FALSE);
117 gtk_widget_set_visible (priv->sample_widget, TRUE);
118 }
119
120 /**
121 * cd_sample_window_class_init:
122 **/
123 static void
124 cd_sample_window_class_init (CdSampleWindowClass *klass)
125 {
126 GObjectClass *object_class = G_OBJECT_CLASS (klass);
127 object_class->finalize = cd_sample_window_finalize;
128 }
129
130 /**
131 * cd_sample_window_init:
132 **/
133 static void
134 cd_sample_window_init (CdSampleWindow *sample_window)
135 {
136 CdSampleWindowPrivate *priv = cd_sample_window_get_instance_private (sample_window);
137 GtkWindow *window = GTK_WINDOW (sample_window);
138 GtkWidget *vbox;
139 priv->sample_widget = cd_sample_widget_new ();
140 priv->progress_bar = gtk_progress_bar_new ();
141
142 /* pack in two widgets into the window */
143 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
144 gtk_window_set_child (window, vbox);
145 gtk_box_append (GTK_BOX (vbox), priv->sample_widget);
146 gtk_box_append (GTK_BOX (vbox), priv->progress_bar);
147 gtk_widget_set_size_request (priv->sample_widget, 400, 400);
148
149 gtk_widget_set_cursor (GTK_WIDGET (window), gdk_cursor_new_from_name ("blank", NULL));
150 }
151
152 /**
153 * cd_sample_window_finalize:
154 **/
155 static void
156 cd_sample_window_finalize (GObject *object)
157 {
158 CdSampleWindow *sample_window = CD_SAMPLE_WINDOW (object);
159 CdSampleWindowPrivate *priv = cd_sample_window_get_instance_private (sample_window);
160
161 if (priv->pulse_id != 0)
162 g_source_remove (priv->pulse_id);
163
164 G_OBJECT_CLASS (cd_sample_window_parent_class)->finalize (object);
165 }
166
167 /**
168 * cd_sample_window_new:
169 *
170 * Return value: a new #CdSampleWindow object.
171 *
172 * Since: 0.1.22
173 **/
174 GtkWindow *
175 cd_sample_window_new (void)
176 {
177 CdSampleWindow *sample_window;
178 sample_window = g_object_new (CD_TYPE_SAMPLE_WINDOW,
179 "decorated", FALSE,
180 "default-height", 400,
181 "default-width", 400,
182 "deletable", FALSE,
183 "destroy-with-parent", TRUE,
184 "icon-name", "icc-profile",
185 "resizable", FALSE,
186 "title", "calibration square",
187 NULL);
188 return GTK_WINDOW (sample_window);
189 }