37dd6feb530e721e2116444ac2e42b1e1e1ff281
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / zwindow.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2012 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program 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 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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 this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "bcdialog.h"
23 #include "edl.h"
24 #include "filexml.h"
25 #include "language.h"
26 #include "mainsession.h"
27 #include "mwindow.h"
28 #include "mwindowgui.h"
29 #include "patchbay.h"
30 #include "patchgui.h"
31 #include "playbackengine.h"
32 #include "renderengine.h"
33 #include "timelinepane.h"
34 #include "track.h"
35 #include "tracks.h"
36 #include "transportque.h"
37 #include "zwindow.h"
38 #include "zwindowgui.h"
39
40 Mixers::Mixers()
41 {
42 }
43
44 Mixers::~Mixers()
45 {
46         remove_all_objects();
47 }
48
49 Mixer *Mixers::new_mixer(int show)
50 {
51         int idx = 0;
52         for( int i=0; i<size(); ++i ) {
53                 Mixer *mixer = get(i);
54                 if( idx < mixer->idx ) idx = mixer->idx;
55         }
56         return append(new Mixer(idx+1, show));
57 }
58
59 Mixer *Mixers::get_mixer(int idx)
60 {
61         for( int i=0; i<size(); ++i ) {
62                 Mixer *mixer = get(i);
63                 if( mixer->idx == idx ) return mixer;
64         }
65         return 0;
66 }
67
68 void Mixers::del_mixer(Mixer *mixer)
69 {
70         remove_object(mixer);
71 }
72
73 void Mixer::set_title(const char *tp)
74 {
75         if( tp == title ) return;
76         strncpy(title, !tp ? "" : tp, sizeof(title));
77         title[sizeof(title)-1] = 0;
78 }
79
80 void Mixers::save(FileXML *file)
81 {
82         file->tag.set_title("MIXERS");
83         file->append_tag();
84         file->append_newline();
85         for( int i=0; i<size(); ++i ) {
86                 Mixer *mixer = get(i);
87                 mixer->save(file);
88         }
89         file->tag.set_title("/MIXERS");
90         file->append_tag();
91         file->append_newline();
92 }
93
94 int Mixers::load(FileXML *file)
95 {
96         int result = 0;
97         while( !(result = file->read_tag()) ) {
98                 if( file->tag.title_is("/MIXERS") ) break;
99                 if( file->tag.title_is("MIXER") ) {
100                         Mixer *mixer = new_mixer();
101                         file->tag.get_property("TITLE", mixer->title);
102                         mixer->show = file->tag.get_property("SHOW", 1);
103                         mixer->x = file->tag.get_property("X", mixer->x);
104                         mixer->y = file->tag.get_property("Y", mixer->y);
105                         mixer->w = file->tag.get_property("W", mixer->w);
106                         mixer->h = file->tag.get_property("H", mixer->h);
107                         mixer->load(file);
108                 }
109         }
110         return 0;
111 }
112
113 void Mixers::copy_from(Mixers &that)
114 {
115         remove_all_objects();
116         for( int i=0; i<that.size(); ++i ) {
117                 Mixer *mixer = new_mixer();
118                 mixer->copy_from(*that[i]);
119         }
120 }
121
122 void Mixer::save(FileXML *file)
123 {
124         file->tag.set_title("MIXER");
125         file->tag.set_property("SHOW",show);
126         file->tag.set_property("TITLE",title);
127         file->tag.set_property("X",x);
128         file->tag.set_property("Y",y);
129         file->tag.set_property("W",w);
130         file->tag.set_property("H",h);
131         file->append_tag();
132         file->append_newline();
133         for( int i=0; i<mixer_ids.size(); ++i ) {
134                 file->tag.set_title("MIX");
135                 file->tag.set_property("ID", mixer_ids[i]);
136                 file->append_tag();
137                 file->tag.set_title("/MIX");
138                 file->append_tag();
139                 file->append_newline();
140         }
141         file->tag.set_title("/MIXER");
142         file->append_tag();
143         file->append_newline();
144 }
145
146 Mixer::Mixer(int idx, int show)
147 {
148         this->idx = idx;
149         this->show = show;
150         title[0] = 0;
151         x = y = 100 + idx*64;
152         w = 400;  h = 300;
153 }
154 void Mixer::reposition(int x, int y, int w, int h)
155 {
156         this->x = x;  this->y = y;
157         this->w = w;  this->h = h;
158 }
159
160 int Mixer::load(FileXML *file)
161 {
162         int result = 0;
163         while( !(result = file->read_tag()) ) {
164                 if( file->tag.title_is("/MIXER") ) break;
165                 if( file->tag.title_is("MIX") ) {
166                         mixer_ids.append(file->tag.get_property("ID", 0));
167                 }
168         }
169         return 0;
170 }
171
172 void Mixer::copy_from(Mixer &that)
173 {
174         mixer_ids.remove_all();
175         strncpy(title, that.title, sizeof(title));
176         title[sizeof(title)-1] = 0;
177         show = that.show;
178         x = that.x;  y = that.y;
179         w = that.w;  h = that.h;
180         for( int i=0; i<that.mixer_ids.size(); ++i )
181                 mixer_ids.append(that.mixer_ids[i]);
182 }
183
184
185 ZWindow::ZWindow(MWindow *mwindow)
186  : BC_DialogThread()
187 {
188         this->mwindow = mwindow;
189         idx = -1;
190         edl = 0;
191         highlighted = 0;
192         playable = 1;
193         title[0] = 0;
194         zgui = 0;
195         zoom = 0;
196 }
197
198 ZWindow::~ZWindow()
199 {
200         close_window();
201         if( edl )
202                 edl->remove_user();
203 }
204
205 BC_Window* ZWindow::new_gui()
206 {
207         Mixer *mixer = mwindow->edl->mixers.get_mixer(idx);
208         mixer->show = 1;
209         zgui = new ZWindowGUI(mwindow, this, mixer);
210         zgui->create_objects();
211         return zgui;
212 }
213
214 void ZWindow::handle_done_event(int result)
215 {
216         stop_playback(1);
217         if( result ) {
218                 mwindow->close_mixer(this);
219                 Mixer *mixer = mwindow->edl->mixers.get_mixer(idx);
220                 if( mixer ) mixer->show = 0;
221                 Track *track = mixer ? mwindow->edl->tracks->first : 0;
222                 while( track && track->index_in(mixer) < 0 ) track = track->next;
223 // if no refs to tracks, delete it
224                 if( !track ) mwindow->edl->mixers.del_mixer(mixer);
225         }
226         idx = -1;
227 }
228 void ZWindow::handle_close_event(int result)
229 {
230         zgui = 0;
231 }
232
233 void ZWindow::change_source(EDL *edl)
234 {
235         if( this->edl == edl ) return;
236         zgui->playback_engine->refresh_frame(CHANGE_ALL, edl);
237         if( this->edl )
238                 this->edl->remove_user();
239         this->edl = edl;
240 }
241
242 void ZWindow::stop_playback(int wait)
243 {
244         zgui->playback_engine->stop_playback(wait);
245 }
246
247 void ZWindow::handle_mixer(int command, int wait_tracking,
248                 int use_inout, int toggle_audio, int loop_play, float speed)
249 {
250         if( !playable ) return;
251         PlaybackEngine *engine = zgui->playback_engine;
252         engine->next_command->toggle_audio = toggle_audio;
253         engine->next_command->loop_play = loop_play;
254         engine->next_command->speed = speed;
255         engine->send_command(command, edl, wait_tracking, use_inout);
256 }
257
258 void ZWindow::update_mixer_ids()
259 {
260         if( !running() ) return;
261         Mixer *mixer = mwindow->edl->mixers.get_mixer(idx);
262         if( !mixer ) return;
263         mixer->mixer_ids.remove_all();
264         PatchBay *patchbay = mwindow->gui->pane[0]->patchbay;
265         for( int i=0; i<patchbay->patches.size(); ++i ) {
266                 PatchGUI *patchgui = patchbay->patches[i];
267                 if( !patchgui->mixer ) continue;
268                 int mixer_id = patchgui->track->get_mixer_id();
269                 if( mixer->mixer_ids.number_of(mixer_id) >= 0 ) continue;
270                 mixer->mixer_ids.append(mixer_id);
271         }
272 }
273
274 void ZWindow::set_title(const char *tp)
275 {
276         Track *track = 0;
277         Mixer *mixer = mwindow->edl->mixers.get_mixer(idx);
278         if( mixer ) {
279                 mixer->set_title(tp);
280                 for( track=mwindow->edl->tracks->first; track; track=track->next ) {
281                         if( track->data_type != TRACK_VIDEO ) continue;
282                         int mixer_id = track->get_mixer_id();
283                         int k = mixer->mixer_ids.size();
284                         while( --k >= 0 && mixer_id != mixer->mixer_ids[k] );
285                         if( k >= 0 ) break;
286                 }
287         }
288         char *cp = title, *ep = cp + sizeof(title)-1;
289         cp += snprintf(title, ep-cp, track ? track->title : _("Mixer %d"), idx);
290         if( tp ) cp += snprintf(cp, ep-cp, ": %s", tp);
291         *cp = 0;
292 }
293
294 void ZWindow::reposition(int x, int y, int w, int h)
295 {
296         Mixer *mixer = mwindow->edl->mixers.get_mixer(idx);
297         if( !mixer ) return;
298         mixer->reposition(x, y, w, h);
299 }
300