1 /* GSequencer - Advanced GTK Sequencer
2  * Copyright (C) 2005-2021 Joël Krähemann
3  *
4  * This file is part of GSequencer.
5  *
6  * GSequencer is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU 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  * GSequencer 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 General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GSequencer.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <ags/X/ags_pad_editor.h>
21 #include <ags/X/ags_pad_editor_callbacks.h>
22 
23 #include <ags/X/ags_machine_editor.h>
24 #include <ags/X/ags_connection_editor.h>
25 #include <ags/X/ags_line_editor.h>
26 
27 #include <ags/i18n.h>
28 
29 void ags_pad_editor_class_init(AgsPadEditorClass *pad_editor);
30 void ags_pad_editor_connectable_interface_init(AgsConnectableInterface *connectable);
31 void ags_pad_editor_applicable_interface_init(AgsApplicableInterface *applicable);
32 void ags_pad_editor_init(AgsPadEditor *pad_editor);
33 void ags_pad_editor_set_property(GObject *gobject,
34 				 guint prop_id,
35 				 const GValue *value,
36 				 GParamSpec *param_spec);
37 void ags_pad_editor_get_property(GObject *gobject,
38 				 guint prop_id,
39 				 GValue *value,
40 				 GParamSpec *param_spec);
41 void ags_pad_editor_finalize(GObject *gobject);
42 
43 void ags_pad_editor_connect(AgsConnectable *connectable);
44 void ags_pad_editor_disconnect(AgsConnectable *connectable);
45 
46 void ags_pad_editor_set_update(AgsApplicable *applicable, gboolean update);
47 void ags_pad_editor_apply(AgsApplicable *applicable);
48 void ags_pad_editor_reset(AgsApplicable *applicable);
49 
50 /**
51  * SECTION:ags_pad_editor
52  * @short_description: A composite widget to edit #AgsChannel
53  * @title: AgsPadEditor
54  * @section_id:
55  * @include: ags/X/ags_pad_editor.h
56  *
57  * #AgsPadEditor is a composite widget to edit #AgsChannel. It should be
58  * packed by an #AgsPadEditor.
59  */
60 
61 enum{
62   PROP_0,
63   PROP_CHANNEL,
64 };
65 
66 static gpointer ags_pad_editor_parent_class = NULL;
67 
68 GType
ags_pad_editor_get_type(void)69 ags_pad_editor_get_type(void)
70 {
71   static volatile gsize g_define_type_id__volatile = 0;
72 
73   if(g_once_init_enter (&g_define_type_id__volatile)){
74     GType ags_type_pad_editor = 0;
75 
76     static const GTypeInfo ags_pad_editor_info = {
77       sizeof (AgsPadEditorClass),
78       NULL, /* base_init */
79       NULL, /* base_finalize */
80       (GClassInitFunc) ags_pad_editor_class_init,
81       NULL, /* class_finalize */
82       NULL, /* class_data */
83       sizeof (AgsPadEditor),
84       0,    /* n_preallocs */
85       (GInstanceInitFunc) ags_pad_editor_init,
86     };
87 
88     static const GInterfaceInfo ags_connectable_interface_info = {
89       (GInterfaceInitFunc) ags_pad_editor_connectable_interface_init,
90       NULL, /* interface_finalize */
91       NULL, /* interface_data */
92     };
93 
94     static const GInterfaceInfo ags_applicable_interface_info = {
95       (GInterfaceInitFunc) ags_pad_editor_applicable_interface_init,
96       NULL, /* interface_finalize */
97       NULL, /* interface_data */
98     };
99 
100     ags_type_pad_editor = g_type_register_static(GTK_TYPE_BOX,
101 						 "AgsPadEditor", &ags_pad_editor_info,
102 						 0);
103 
104     g_type_add_interface_static(ags_type_pad_editor,
105 				AGS_TYPE_CONNECTABLE,
106 				&ags_connectable_interface_info);
107 
108     g_type_add_interface_static(ags_type_pad_editor,
109 				AGS_TYPE_APPLICABLE,
110 				&ags_applicable_interface_info);
111 
112     g_once_init_leave(&g_define_type_id__volatile, ags_type_pad_editor);
113   }
114 
115   return g_define_type_id__volatile;
116 }
117 
118 void
ags_pad_editor_class_init(AgsPadEditorClass * pad_editor)119 ags_pad_editor_class_init(AgsPadEditorClass *pad_editor)
120 {
121   GObjectClass *gobject;
122 
123   GParamSpec *param_spec;
124 
125   ags_pad_editor_parent_class = g_type_class_peek_parent(pad_editor);
126 
127   /* GObjectClass */
128   gobject = (GObjectClass *) pad_editor;
129 
130   gobject->set_property = ags_pad_editor_set_property;
131   gobject->get_property = ags_pad_editor_get_property;
132 
133   gobject->finalize = ags_pad_editor_finalize;
134 
135   /* properties */
136   param_spec = g_param_spec_object("channel",
137 				   i18n_pspec("assigned channel"),
138 				   i18n_pspec("The channel which this pad editor is assigned with"),
139 				   AGS_TYPE_CHANNEL,
140 				   G_PARAM_READABLE | G_PARAM_WRITABLE);
141   g_object_class_install_property(gobject,
142 				  PROP_CHANNEL,
143 				  param_spec);
144 }
145 
146 void
ags_pad_editor_connectable_interface_init(AgsConnectableInterface * connectable)147 ags_pad_editor_connectable_interface_init(AgsConnectableInterface *connectable)
148 {
149   connectable->is_ready = NULL;
150   connectable->is_connected = NULL;
151   connectable->connect = ags_pad_editor_connect;
152   connectable->disconnect = ags_pad_editor_disconnect;
153 }
154 
155 void
ags_pad_editor_applicable_interface_init(AgsApplicableInterface * applicable)156 ags_pad_editor_applicable_interface_init(AgsApplicableInterface *applicable)
157 {
158   applicable->set_update = ags_pad_editor_set_update;
159   applicable->apply = ags_pad_editor_apply;
160   applicable->reset = ags_pad_editor_reset;
161 }
162 
163 void
ags_pad_editor_init(AgsPadEditor * pad_editor)164 ags_pad_editor_init(AgsPadEditor *pad_editor)
165 {
166   gtk_orientable_set_orientation(GTK_ORIENTABLE(pad_editor),
167 				 GTK_ORIENTATION_VERTICAL);
168 
169   pad_editor->flags = 0;
170 
171   pad_editor->version = AGS_PAD_EDITOR_DEFAULT_VERSION;
172   pad_editor->build_id = AGS_PAD_EDITOR_DEFAULT_BUILD_ID;
173 
174   pad_editor->pad = NULL;
175 
176   pad_editor->line_editor_expander = (GtkExpander *) gtk_expander_new(NULL);
177   gtk_box_pack_start(GTK_BOX(pad_editor),
178 		     GTK_WIDGET(pad_editor->line_editor_expander),
179 		     FALSE, FALSE,
180 		     0);
181 
182   pad_editor->line_editor = NULL;
183 }
184 
185 
186 void
ags_pad_editor_set_property(GObject * gobject,guint prop_id,const GValue * value,GParamSpec * param_spec)187 ags_pad_editor_set_property(GObject *gobject,
188 			    guint prop_id,
189 			    const GValue *value,
190 			    GParamSpec *param_spec)
191 {
192   AgsPadEditor *pad_editor;
193 
194   pad_editor = AGS_PAD_EDITOR(gobject);
195 
196   switch(prop_id){
197   case PROP_CHANNEL:
198     {
199       AgsChannel *channel;
200 
201       channel = (AgsChannel *) g_value_get_object(value);
202 
203       ags_pad_editor_set_channel(pad_editor, channel);
204     }
205     break;
206   default:
207     G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, param_spec);
208     break;
209   }
210 }
211 
212 void
ags_pad_editor_get_property(GObject * gobject,guint prop_id,GValue * value,GParamSpec * param_spec)213 ags_pad_editor_get_property(GObject *gobject,
214 			    guint prop_id,
215 			    GValue *value,
216 			    GParamSpec *param_spec)
217 {
218   AgsPadEditor *pad_editor;
219 
220   pad_editor = AGS_PAD_EDITOR(gobject);
221 
222   switch(prop_id){
223   case PROP_CHANNEL:
224     g_value_set_object(value, pad_editor->pad);
225     break;
226   default:
227     G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, param_spec);
228     break;
229   }
230 }
231 
232 void
ags_pad_editor_finalize(GObject * gobject)233 ags_pad_editor_finalize(GObject *gobject)
234 {
235   AgsPadEditor *pad_editor;
236 
237   pad_editor = (AgsPadEditor *) gobject;
238 
239   if(pad_editor->pad != NULL){
240     g_object_unref(pad_editor->pad);
241   }
242 
243   /* call parent */
244   G_OBJECT_CLASS(ags_pad_editor_parent_class)->finalize(gobject);
245 }
246 
247 void
ags_pad_editor_connect(AgsConnectable * connectable)248 ags_pad_editor_connect(AgsConnectable *connectable)
249 {
250   AgsMachine *machine;
251   AgsMachineEditor *machine_editor;
252   AgsConnectionEditor *connection_editor;
253   AgsPadEditor *pad_editor;
254 
255   GList *line_editor, *line_editor_start;
256 
257   pad_editor = AGS_PAD_EDITOR(connectable);
258 
259   if((AGS_PAD_EDITOR_CONNECTED & (pad_editor->flags)) != 0){
260     return;
261   }
262 
263   pad_editor->flags |= AGS_PAD_EDITOR_CONNECTED;
264 
265   machine = NULL;
266 
267   machine_editor = (AgsMachineEditor *) gtk_widget_get_ancestor(GTK_WIDGET(pad_editor),
268 								AGS_TYPE_MACHINE_EDITOR);
269 
270   connection_editor = (AgsConnectionEditor *) gtk_widget_get_ancestor(GTK_WIDGET(pad_editor),
271 								      AGS_TYPE_CONNECTION_EDITOR);
272 
273   if(machine_editor != NULL){
274     machine = machine_editor->machine;
275   }else if(connection_editor != NULL){
276     machine = connection_editor->machine;
277   }
278 
279   /*  */
280   if(machine != NULL){
281     g_signal_connect_after(G_OBJECT(machine), "resize-audio-channels",
282 			   G_CALLBACK(ags_pad_editor_resize_audio_channels_callback), pad_editor);
283   }
284 
285   /* AgsLineEditor */
286   line_editor_start =
287     line_editor = gtk_container_get_children(GTK_CONTAINER(pad_editor->line_editor));
288 
289   while(line_editor != NULL){
290     ags_connectable_connect(AGS_CONNECTABLE(line_editor->data));
291 
292     line_editor = line_editor->next;
293   }
294 
295   g_list_free(line_editor_start);
296 }
297 
298 void
ags_pad_editor_disconnect(AgsConnectable * connectable)299 ags_pad_editor_disconnect(AgsConnectable *connectable)
300 {
301   AgsMachine *machine;
302   AgsMachineEditor *machine_editor;
303   AgsConnectionEditor *connection_editor;
304   AgsPadEditor *pad_editor;
305 
306   GList *line_editor, *line_editor_start;
307 
308   pad_editor = AGS_PAD_EDITOR(connectable);
309 
310   if((AGS_PAD_EDITOR_CONNECTED & (pad_editor->flags)) == 0){
311     return;
312   }
313 
314   pad_editor->flags &= (~AGS_PAD_EDITOR_CONNECTED);
315 
316   machine = NULL;
317 
318   machine_editor = (AgsMachineEditor *) gtk_widget_get_ancestor(GTK_WIDGET(pad_editor),
319 								AGS_TYPE_MACHINE_EDITOR);
320 
321   connection_editor = (AgsConnectionEditor *) gtk_widget_get_ancestor(GTK_WIDGET(pad_editor),
322 								      AGS_TYPE_CONNECTION_EDITOR);
323 
324   if(machine_editor != NULL){
325     machine = machine_editor->machine;
326   }else if(connection_editor != NULL){
327     machine = connection_editor->machine;
328   }
329 
330   /*  */
331   if(machine != NULL){
332     g_object_disconnect(G_OBJECT(machine),
333 			"any_signal::resize-audio-channels",
334 			G_CALLBACK(ags_pad_editor_resize_audio_channels_callback),
335 			pad_editor,
336 			NULL);
337   }
338 
339   /* AgsLineEditor */
340   line_editor_start =
341     line_editor = gtk_container_get_children(GTK_CONTAINER(pad_editor->line_editor));
342 
343   while(line_editor != NULL){
344     ags_connectable_disconnect(AGS_CONNECTABLE(line_editor->data));
345 
346     line_editor = line_editor->next;
347   }
348 
349   g_list_free(line_editor_start);
350 }
351 
352 void
ags_pad_editor_set_update(AgsApplicable * applicable,gboolean update)353 ags_pad_editor_set_update(AgsApplicable *applicable, gboolean update)
354 {
355   AgsPadEditor *pad_editor;
356   GList *line_editor, *line_editor_start;
357 
358   pad_editor = AGS_PAD_EDITOR(applicable);
359 
360   line_editor_start =
361     line_editor = gtk_container_get_children(GTK_CONTAINER(pad_editor->line_editor));
362 
363   while(line_editor != NULL){
364     ags_applicable_set_update(AGS_APPLICABLE(line_editor->data), update);
365 
366     line_editor = line_editor->next;
367   }
368 
369   g_list_free(line_editor_start);
370 }
371 
372 void
ags_pad_editor_apply(AgsApplicable * applicable)373 ags_pad_editor_apply(AgsApplicable *applicable)
374 {
375   AgsPadEditor *pad_editor;
376   GList *line_editor, *line_editor_start;
377 
378   pad_editor = AGS_PAD_EDITOR(applicable);
379 
380   line_editor_start =
381     line_editor = gtk_container_get_children(GTK_CONTAINER(pad_editor->line_editor));
382 
383   while(line_editor != NULL){
384     ags_applicable_apply(AGS_APPLICABLE(line_editor->data));
385 
386     line_editor = line_editor->next;
387   }
388 
389   g_list_free(line_editor_start);
390 }
391 
392 void
ags_pad_editor_reset(AgsApplicable * applicable)393 ags_pad_editor_reset(AgsApplicable *applicable)
394 {
395   AgsPadEditor *pad_editor;
396   GList *line_editor, *line_editor_start;
397 
398   pad_editor = AGS_PAD_EDITOR(applicable);
399 
400   line_editor_start =
401     line_editor = gtk_container_get_children(GTK_CONTAINER(pad_editor->line_editor));
402 
403   while(line_editor != NULL){
404     ags_applicable_reset(AGS_APPLICABLE(line_editor->data));
405 
406     line_editor = line_editor->next;
407   }
408 
409   g_list_free(line_editor_start);
410 }
411 
412 /**
413  * ags_pad_editor_set_channel:
414  * @pad_editor: an #AgsPadEditor
415  * @channel: the new #AgsChannel
416  *
417  * Is called as channel gets modified.
418  *
419  * Since: 3.0.0
420  */
421 void
ags_pad_editor_set_channel(AgsPadEditor * pad_editor,AgsChannel * start_channel)422 ags_pad_editor_set_channel(AgsPadEditor *pad_editor, AgsChannel *start_channel)
423 {
424   GtkVBox *vbox;
425 
426   if(pad_editor->line_editor != NULL){
427     vbox = pad_editor->line_editor;
428     pad_editor->line_editor = NULL;
429     gtk_widget_destroy(GTK_WIDGET(vbox));
430   }
431 
432   if(pad_editor->pad != start_channel){
433     if(pad_editor->pad != NULL){
434       g_object_unref(pad_editor->pad);
435     }
436 
437     if(start_channel != NULL){
438       g_object_ref(start_channel);
439     }
440 
441     pad_editor->pad = start_channel;
442   }
443 
444   if(start_channel != NULL){
445     AgsLineEditor *line_editor;
446 
447     AgsChannel *channel;
448     AgsChannel *next_pad, *next_channel;
449 
450     gchar *str;
451 
452     guint pad;
453     guint i;
454 
455     /* get some channel fields */
456     pad = 0;
457 
458     g_object_get(start_channel,
459 		 "pad", &pad,
460 		 NULL);
461 
462     /* set label */
463     str = g_strdup_printf("%s: %u",
464 			  i18n("pad"),
465 			  pad + 1);
466     gtk_expander_set_label(pad_editor->line_editor_expander,
467 			   str);
468 
469     g_free(str);
470 
471     pad_editor->line_editor = (GtkBox *) gtk_box_new(GTK_ORIENTATION_VERTICAL,
472 						     0);
473     gtk_container_add(GTK_CONTAINER(pad_editor->line_editor_expander),
474 		      GTK_WIDGET(pad_editor->line_editor));
475 
476     channel = start_channel;
477     next_pad = NULL;
478 
479     if(channel != NULL){
480       g_object_ref(channel);
481 
482       next_pad = ags_channel_next_pad(start_channel);
483     }
484 
485     next_channel = NULL;
486 
487     while(channel != next_pad && channel != NULL){
488       /* instantiate line editor */
489       line_editor = ags_line_editor_new(NULL);
490       line_editor->editor_type_count = pad_editor->editor_type_count;
491       line_editor->editor_type = (GType *) malloc(line_editor->editor_type_count * sizeof(GType));
492 
493       for(i = 0; i < line_editor->editor_type_count; i++){
494 	line_editor->editor_type[i] = pad_editor->editor_type[i];
495       }
496 
497       g_object_set(line_editor,
498 		   "channel", channel,
499 		   NULL);
500 
501       gtk_box_pack_start(GTK_BOX(pad_editor->line_editor),
502 			 GTK_WIDGET(line_editor),
503 			 FALSE, FALSE,
504 			 0);
505 
506       /* iterate */
507       next_channel = ags_channel_next(channel);
508 
509       g_object_unref(channel);
510 
511       channel = next_channel;
512     }
513 
514     if(channel != NULL){
515       g_object_unref(channel);
516     }
517 
518     if(next_pad != NULL){
519       g_object_unref(next_pad);
520     }
521   }else{
522     gtk_expander_set_label(pad_editor->line_editor_expander,
523 			   NULL);
524   }
525 }
526 
527 /**
528  * ags_pad_editor_new:
529  * @channel: the #AgsChannel to edit
530  *
531  * Create a new instance of #AgsPadEditor
532  *
533  * Returns: the new #AgsPadEditor
534  *
535  * Since: 3.0.0
536  */
537 AgsPadEditor*
ags_pad_editor_new(AgsChannel * channel)538 ags_pad_editor_new(AgsChannel *channel)
539 {
540   AgsPadEditor *pad_editor;
541 
542   pad_editor = (AgsPadEditor *) g_object_new(AGS_TYPE_PAD_EDITOR,
543 					     "channel", channel,
544 					     NULL);
545 
546   return(pad_editor);
547 }
548