1 /*
2 * Copyright (C) 2020-2021 Alexandros Theodotou <alex at zrythm dot org>
3 *
4 * This file is part of Zrythm
5 *
6 * Zrythm is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Zrythm is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with Zrythm. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 #include "audio/region.h"
21 #include "audio/track.h"
22 #include "gui/widgets/dialogs/object_color_chooser_dialog.h"
23 #include "project.h"
24 #include "utils/color.h"
25 #include "utils/error.h"
26 #include "utils/flags.h"
27 #include "utils/gtk.h"
28 #include "zrythm_app.h"
29
30 #include <glib/gi18n.h>
31 #include <gtk/gtk.h>
32
G_DEFINE_TYPE(ObjectColorChooserDialogWidget,object_color_chooser_dialog_widget,GTK_TYPE_COLOR_CHOOSER_DIALOG)33 G_DEFINE_TYPE (
34 ObjectColorChooserDialogWidget,
35 object_color_chooser_dialog_widget,
36 GTK_TYPE_COLOR_CHOOSER_DIALOG)
37
38 static void
39 get_current_obj_color (
40 ObjectColorChooserDialogWidget * self,
41 GdkRGBA * color)
42 {
43 if (self->track)
44 {
45 g_return_if_fail (IS_TRACK (self->track));
46 *color = self->track->color;
47 }
48 else if (self->region)
49 {
50 /**color = self->region->color;*/
51 }
52 else
53 {
54 g_return_if_reached ();
55 }
56 }
57
58 /**
59 * Runs the widget and processes the result, then
60 * closes the dialog.
61 *
62 * @return Whether the color was set or not.
63 */
64 bool
object_color_chooser_dialog_widget_run(ObjectColorChooserDialogWidget * self)65 object_color_chooser_dialog_widget_run (
66 ObjectColorChooserDialogWidget * self)
67 {
68 int res = gtk_dialog_run (GTK_DIALOG (self));
69 bool color_set = false;
70 switch (res)
71 {
72 case GTK_RESPONSE_OK:
73 color_set = true;
74 break;
75 default:
76 break;
77 }
78
79 /* get selected color */
80 GdkRGBA sel_color;
81 gtk_color_chooser_get_rgba (
82 GTK_COLOR_CHOOSER (self), &sel_color);
83
84 if (color_set)
85 {
86 if (self->track)
87 {
88 /* get current object color */
89 GdkRGBA cur_color;
90 get_current_obj_color (self, &cur_color);
91
92 /* if changed, apply the change */
93 if (!color_is_same (
94 &sel_color, &cur_color))
95 {
96 track_set_color (
97 self->track, &sel_color, F_UNDOABLE,
98 F_PUBLISH_EVENTS);
99 }
100 }
101 else if (self->tracklist_selections)
102 {
103 TracklistSelections * sel =
104 self->tracklist_selections;
105
106 GError * err = NULL;
107 bool ret =
108 tracklist_selections_action_perform_edit_color (
109 sel, &sel_color, &err);
110 if (!ret)
111 {
112 HANDLE_ERROR (
113 err, "%s",
114 _("Failed to change color"));
115 }
116 }
117 }
118 gtk_widget_destroy (GTK_WIDGET (self));
119
120 return color_set;
121 }
122
123 /**
124 * Creates a new dialog.
125 */
126 ObjectColorChooserDialogWidget *
object_color_chooser_dialog_widget_new_for_track(Track * track)127 object_color_chooser_dialog_widget_new_for_track (
128 Track * track)
129 {
130 g_return_val_if_fail (IS_TRACK (track), NULL);
131
132 char * str =
133 g_strdup_printf (_("%s color"), track->name);
134 ObjectColorChooserDialogWidget * self =
135 g_object_new (
136 OBJECT_COLOR_CHOOSER_DIALOG_WIDGET_TYPE,
137 "title", str,
138 "rgba", &track->color,
139 "use-alpha", false,
140 NULL);
141 g_free (str);
142
143 self->track = track;
144 g_warn_if_fail (IS_TRACK (self->track));
145
146 return self;
147 }
148
149 /**
150 * Creates a new dialog.
151 */
152 ObjectColorChooserDialogWidget *
object_color_chooser_dialog_widget_new_for_tracklist_selections(TracklistSelections * sel)153 object_color_chooser_dialog_widget_new_for_tracklist_selections (
154 TracklistSelections * sel)
155 {
156 Track * track = sel->tracks[0];
157 g_return_val_if_fail (
158 IS_TRACK_AND_NONNULL (track), NULL);
159 ObjectColorChooserDialogWidget * self =
160 g_object_new (
161 OBJECT_COLOR_CHOOSER_DIALOG_WIDGET_TYPE,
162 "title", _("Track color"),
163 "rgba", &track->color,
164 "use-alpha", false,
165 NULL);
166
167 self->tracklist_selections = sel;
168
169 return self;
170 }
171
172 /**
173 * Creates a new dialog.
174 */
175 ObjectColorChooserDialogWidget *
object_color_chooser_dialog_widget_new_for_region(ZRegion * region)176 object_color_chooser_dialog_widget_new_for_region (
177 ZRegion * region)
178 {
179 ObjectColorChooserDialogWidget * self =
180 g_object_new (
181 OBJECT_COLOR_CHOOSER_DIALOG_WIDGET_TYPE,
182 "title", _("Region color"),
183 NULL);
184
185 self->region = region;
186
187 return self;
188 }
189
190 static void
object_color_chooser_dialog_widget_class_init(ObjectColorChooserDialogWidgetClass * _klass)191 object_color_chooser_dialog_widget_class_init (
192 ObjectColorChooserDialogWidgetClass * _klass)
193 {
194 /*GtkWidgetClass * klass = GTK_WIDGET_CLASS (_klass);*/
195 }
196
197 static void
object_color_chooser_dialog_widget_init(ObjectColorChooserDialogWidget * self)198 object_color_chooser_dialog_widget_init (
199 ObjectColorChooserDialogWidget * self)
200 {
201 z_gtk_widget_add_style_class (
202 GTK_WIDGET (self), "object-color-chooser-dialog");
203 }
204