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 "gui/backend/clipboard.h"
21 #include "utils/flags.h"
22 #include "utils/objects.h"
23 
24 /**
25  * Creates a new Clipboard instance for the given
26  * arranger selections.
27  */
28 Clipboard *
clipboard_new_for_arranger_selections(ArrangerSelections * sel,bool clone)29 clipboard_new_for_arranger_selections (
30   ArrangerSelections * sel,
31   bool                 clone)
32 {
33   Clipboard * self = object_new (Clipboard);
34 
35   if (clone)
36     {
37       sel = arranger_selections_clone (sel);
38     }
39 
40   switch (sel->type)
41     {
42     case ARRANGER_SELECTIONS_TYPE_AUTOMATION:
43       self->automation_sel =
44         (AutomationSelections *) sel;
45       self->type =
46         CLIPBOARD_TYPE_AUTOMATION_SELECTIONS;
47       break;
48     case ARRANGER_SELECTIONS_TYPE_TIMELINE:
49       self->timeline_sel =
50         (TimelineSelections *) sel;
51       self->type =
52         CLIPBOARD_TYPE_TIMELINE_SELECTIONS;
53       break;
54     case ARRANGER_SELECTIONS_TYPE_MIDI:
55       self->ma_sel =
56         (MidiArrangerSelections *) sel;
57       self->type =
58         CLIPBOARD_TYPE_MIDI_SELECTIONS;
59       break;
60     case ARRANGER_SELECTIONS_TYPE_CHORD:
61       self->chord_sel =
62         (ChordSelections *) sel;
63       self->type =
64         CLIPBOARD_TYPE_CHORD_SELECTIONS;
65       break;
66     default:
67       g_return_val_if_reached (NULL);
68     }
69 
70   return self;
71 }
72 
73 Clipboard *
clipboard_new_for_mixer_selections(MixerSelections * sel,bool clone)74 clipboard_new_for_mixer_selections (
75   MixerSelections * sel,
76   bool              clone)
77 {
78   Clipboard * self = object_new (Clipboard);
79 
80   if (clone)
81     {
82       sel = mixer_selections_clone (sel, F_PROJECT);
83     }
84 
85   self->mixer_sel = sel;
86   self->type = CLIPBOARD_TYPE_MIXER_SELECTIONS;
87 
88   return self;
89 }
90 
91 /**
92  * Gets the ArrangerSelections, if this clipboard
93  * contains arranger selections.
94  */
95 ArrangerSelections *
clipboard_get_selections(Clipboard * self)96 clipboard_get_selections (
97   Clipboard * self)
98 {
99 #define RETURN_IF_EXISTS(selections) \
100   if (self->selections) \
101     { \
102       return (ArrangerSelections *) self->selections; \
103     }
104 
105   RETURN_IF_EXISTS (ma_sel);
106   RETURN_IF_EXISTS (timeline_sel);
107   RETURN_IF_EXISTS (automation_sel);
108   RETURN_IF_EXISTS (chord_sel);
109 
110 #undef RETURN_IF_EXISTS
111 
112   g_return_val_if_reached (NULL);
113 }
114 
115 /**
116  * Frees the clipboard and all associated data.
117  */
118 void
clipboard_free(Clipboard * self)119 clipboard_free (
120   Clipboard * self)
121 {
122   switch (self->type)
123     {
124     case CLIPBOARD_TYPE_TIMELINE_SELECTIONS:
125     case CLIPBOARD_TYPE_MIDI_SELECTIONS:
126     case CLIPBOARD_TYPE_AUTOMATION_SELECTIONS:
127     case CLIPBOARD_TYPE_CHORD_SELECTIONS:
128       {
129         ArrangerSelections * sel =
130           clipboard_get_selections (self);
131         g_return_if_fail (sel);
132         arranger_selections_free_full (sel);
133       }
134       break;
135     case CLIPBOARD_TYPE_MIXER_SELECTIONS:
136       mixer_selections_free (self->mixer_sel);
137       break;
138     }
139 
140   g_free_and_null (self);
141 }
142