+ZWindow *MWindow::get_mixer(Mixer *&mixer)
+{
+ zwindows_lock->lock("MWindow::get_mixer");
+ if( !mixer ) mixer = edl->mixers.new_mixer();
+ ZWindow *zwindow = 0;
+ for( int i=0; !zwindow && i<zwindows.size(); ++i )
+ if( zwindows[i]->idx < 0 ) zwindow = zwindows[i];
+ if( !zwindow )
+ zwindows.append(zwindow = new ZWindow(this));
+ zwindow->idx = mixer->idx;
+ zwindows_lock->unlock();
+ return zwindow;
+}
+
+void MWindow::del_mixer(ZWindow *zwindow)
+{
+ zwindows_lock->lock("MWindow::del_mixer 0");
+ edl->mixers.del_mixer(zwindow->idx);
+ if( session->selected_zwindow >= 0 ) {
+ int i = zwindows.number_of(zwindow);
+ if( i >= 0 && i < session->selected_zwindow )
+ --session->selected_zwindow;
+ else if( i == session->selected_zwindow )
+ session->selected_zwindow = -1;
+ }
+ zwindows_lock->unlock();
+ gui->lock_window("MWindow::del_mixer 1");
+ gui->update_mixers(0, -1);
+ gui->unlock_window();
+}
+
+void MWindow::start_mixer()
+{
+ Mixer *mixer = 0;
+ ZWindow *zwindow = get_mixer(mixer);
+ const char *title = 0;
+
+ for( Track *track=edl->tracks->first; track!=0; track=track->next ) {
+ PatchGUI *patchgui = get_patchgui(track);
+ if( !patchgui || !patchgui->mixer ) continue;
+ mixer->mixer_ids.append(track->get_mixer_id());
+ if( !title ) title = track->title;
+ }
+
+ session->selected_zwindow = -1;
+ gui->lock_window("MWindow::start_mixer");
+ gui->update_mixers(0, 0);
+ gui->unlock_window();
+
+ zwindow->set_title(title);
+ zwindow->start();
+ refresh_mixers();
+}
+
+int MWindow::mixer_track_active(Track *track)
+{
+ int i = session->selected_zwindow;
+ if( i < 0 || i >= zwindows.size() ) return 0;
+ ZWindow *zwindow = zwindows[i];
+ Mixer *mixer = edl->mixers.get_mixer(zwindow->idx);
+ if( !mixer ) return 0;
+ int n = mixer->mixer_ids.number_of(track->get_mixer_id());
+ return n >= 0 ? 1 : 0;
+}
+
+void MWindow::update_mixer_tracks()
+{
+ zwindows_lock->lock("MixPatch::handle_event");
+ int i = session->selected_zwindow;
+ if( i >= 0 && i < zwindows.size() ) {
+ ZWindow *zwindow = zwindows[i];
+ zwindow->update_mixer_ids();
+ }
+ zwindows_lock->unlock();
+}
+
+void MWindow::queue_mixers(EDL *edl, int command, int wait_tracking,
+ int use_inout, int update_refresh, int toggle_audio, int loop_play)
+{
+ zwindows_lock->lock("MWindow::queue_mixers");
+ for( int vidx=0; vidx<zwindows.size(); ++vidx ) {
+ ZWindow *zwindow = zwindows[vidx];
+ if( zwindow->idx < 0 ) continue;
+ Mixer *mixer = edl->mixers.get_mixer(zwindow->idx);
+ if( !mixer || !mixer->mixer_ids.size() ) continue;
+ int k = -1;
+ for( Track *track = edl->tracks->first; k<0 && track!=0; track=track->next ) {
+ if( track->data_type != TRACK_VIDEO ) continue;
+ int mixer_id = track->get_mixer_id();
+ k = mixer->mixer_ids.size();
+ while( --k >= 0 && mixer_id != mixer->mixer_ids[k] );
+ }
+ if( k < 0 ) continue;
+ EDL *mixer_edl = new EDL(this->edl);
+ mixer_edl->create_objects();
+ mixer_edl->copy_all(edl);
+ mixer_edl->remove_vwindow_edls();
+ for( Track *track = mixer_edl->tracks->first; track!=0; track=track->next ) {
+ k = mixer->mixer_ids.size();
+ while( --k >= 0 && track->get_mixer_id() != mixer->mixer_ids[k] );
+ if( k >= 0 ) {
+ track->record = 1;
+ track->play = track->data_type == TRACK_VIDEO ? 1 : 0;
+ }
+ else
+ track->record = track->play = 0;
+ }
+ zwindow->change_source(mixer_edl);
+ zwindow->issue_command(command,
+ wait_tracking, use_inout, update_refresh, toggle_audio, loop_play);
+ }
+ zwindows_lock->unlock();
+}
+
+void MWindow::refresh_mixers(int dir)
+{
+ int command = dir >= 0 ? CURRENT_FRAME : LAST_FRAME;
+ queue_mixers(edl,command,0,0,1,0,0);
+}
+
+void MWindow::stop_mixers()
+{
+ for( int vidx=0; vidx<zwindows.size(); ++vidx ) {
+ ZWindow *zwindow = zwindows[vidx];
+ if( zwindow->idx < 0 ) continue;
+ zwindow->issue_command(STOP, 0, 0, 0, 0, 0);
+ }
+}
+
+void MWindow::close_mixers(int destroy)
+{
+ ArrayList<ZWindow*> closed;
+ zwindows_lock->lock("MWindow::close_mixers");
+ for( int i=zwindows.size(); --i>=0; ) {
+ ZWindow *zwindow = zwindows[i];
+ if( zwindow->idx < 0 ) continue;
+ zwindow->destroy = destroy;
+ ZWindowGUI *zgui = zwindow->zgui;
+ zgui->lock_window("MWindow::select_zwindow 0");
+ zgui->set_done(0);
+ zgui->unlock_window();
+ closed.append(zwindow);
+ }
+ zwindows_lock->unlock();
+ for( int i=0; i<closed.size(); ++i ) {
+ ZWindow *zwindow = closed[i];
+ zwindow->join();
+ }
+}
+
+ZWindow *MWindow::create_mixer(Indexable *indexable)
+{
+ ArrayList<Indexable*> new_assets;
+ new_assets.append(indexable);
+ Track *track = edl->tracks->last;
+ load_assets(&new_assets, 0, LOADMODE_NEW_TRACKS, 0, 0, 0, 0, 0, 0);
+ track = !track ? edl->tracks->first : track->next;
+ Mixer *mixer = 0;
+ ZWindow *zwindow = get_mixer(mixer);
+ while( track ) {
+ track->play = track->record = 0;
+ if( track->data_type == TRACK_VIDEO ) {
+ sprintf(track->title, _("Mixer %d"), zwindow->idx);
+ }
+ mixer->mixer_ids.append(track->get_mixer_id());
+ track = track->next;
+ }
+ if( indexable->is_asset ) {
+ char *path = indexable->path;
+ char *tp = strrchr(path, '/');
+ if( !tp ) tp = path; else ++tp;
+ zwindow->set_title(tp);
+ }
+ else {
+ char *title = ((EDL*)indexable)->local_session->clip_title;
+ zwindow->set_title(title);
+ }
+ return zwindow;
+}
+
+void MWindow::create_mixers()
+{
+ if( !session->drag_assets->size() &&
+ !session->drag_clips->size() ) return;
+ undo->update_undo_before();
+
+ select_zwindow(0);
+ ArrayList<ZWindow *>new_mixers;
+
+ for( int i=0; i<session->drag_assets->size(); ++i ) {
+ Indexable *indexable = session->drag_assets->get(i);
+ if( !indexable->have_video() ) continue;
+ ZWindow *zwindow = create_mixer(indexable);
+ new_mixers.append(zwindow);
+ }
+ for( int i=0; i<session->drag_clips->size(); ++i ) {
+ Indexable *indexable = (Indexable*)session->drag_clips->get(i);
+ if( !indexable->have_video() ) continue;
+ ZWindow *zwindow = create_mixer(indexable);
+ new_mixers.append(zwindow);
+ }
+
+ tile_mixers();
+ for( int i=0; i<new_mixers.size(); ++i )
+ new_mixers[i]->start();
+
+ refresh_mixers();
+ save_backup();
+ undo->update_undo_after(_("create mixers"), LOAD_ALL);
+ restart_brender();
+ gui->update(1, 2, 1, 1, 1, 1, 0);
+ sync_parameters(CHANGE_ALL);
+}
+
+void MWindow::open_mixers()
+{
+ for( int i=0; i<edl->mixers.size(); ++i ) {
+ Mixer *mixer = edl->mixers[i];
+ ZWindow *zwindow = get_mixer(mixer);
+ zwindow->set_title(mixer->title);
+ zwindow->start();
+ }
+ refresh_mixers();
+}
+
+int MWindow::select_zwindow(ZWindow *zwindow)
+{
+ int ret = 0, n = zwindows.number_of(zwindow);
+ if( session->selected_zwindow != n ) {
+ session->selected_zwindow = n;
+ for( int i=0; i<zwindows.size(); ++i ) {
+ ZWindow *zwindow = zwindows[i];
+ if( zwindow->idx < 0 ) continue;
+ ZWindowGUI *zgui = zwindow->zgui;
+ zgui->lock_window("MWindow::select_zwindow 0");
+ zwindow->highlighted = i == n ? 1 : 0;
+ if( zgui->draw_overlays() )
+ zgui->canvas->get_canvas()->flash(1);
+ zgui->unlock_window();
+ }
+ ret = 1;
+ gui->lock_window("MWindow::select_window 1");
+ gui->update_mixers(0, -1);
+ gui->unlock_window();
+ }
+ return ret;
+}
+
+void MWindow::tile_mixers()
+{
+ int nz = 0;
+ for( int i=0; i<zwindows.size(); ++i ) {
+ ZWindow *zwindow = zwindows[i];
+ if( zwindow->idx < 0 ) continue;
+ ++nz;
+ }
+ if( !nz ) return;
+ int zn = ceil(sqrt(nz));
+ int x1 = 1 + gui->get_x(), x2 = cwindow->gui->get_x();
+ int y1 = 1, y2 = gui->get_y();
+ int rw = gui->get_root_w(0), rh = gui->get_root_h(0);
+ if( x1 < 0 ) x1 = 0;
+ if( y1 < 0 ) y1 = 0;
+ if( x2 > rw ) x2 = rw;
+ if( y2 > rh ) y2 = rh;
+ int dx = x2 - x1, dy = y2 - y1;
+ int zw = dx / zn;
+ int lt = BC_DisplayInfo::get_left_border();
+ int top = BC_DisplayInfo::get_top_border();
+ int bw = lt + BC_DisplayInfo::get_right_border(); // borders
+ int bh = top + BC_DisplayInfo::get_bottom_border();
+ int zx = 0, zy = 0; // window origins
+ int mw = 10+10, mh = 10+10; // canvas margins
+ int rsz = 0, n = 0, dz = 0;
+ int ow = edl->session->output_w, oh = edl->session->output_h;
+ for( int i=0; i<zwindows.size(); ++i ) {
+ ZWindow *zwindow = zwindows[i];
+ if( zwindow->idx < 0 ) continue;
+ int ww = zw - bw, hh = (ww - mw) * oh / ow + mh, zh = hh + bh;
+ if( rsz < hh ) rsz = hh;
+ int xx = zx + x1, yy = zy + y1;
+ int mx = x2 - zw, my = y2 - zh;
+ if( xx > mx ) xx = mx;
+ if( yy > my ) yy = my;
+ xx += lt + dz; yy += top + dz;
+ zwindow->reposition(xx,yy, ww,hh);
+ if( zwindow->running() ) {
+ ZWindowGUI *gui = (ZWindowGUI *)zwindow->get_gui();
+ gui->lock_window("MWindow::tile_mixers");
+ gui->BC_WindowBase::reposition_window(xx,yy, ww,hh);
+ gui->unlock_window();
+ }
+ if( ++n >= zn ) {
+ n = 0; rsz += bh;
+ if( (zy += rsz) > (dy - rsz) ) dz += 10;
+ rsz = 0;
+ zx = 0;
+ }
+ else
+ zx += zw;
+ }
+}
+