loupiottes/DMX-2.0/SeqMidiUI.cs
2018-10-08 08:42:56 +02:00

367 lines
10 KiB
C#

/*
Copyright (C) Arnaud Houdelette 2012-2014
Copyright (C) Emmanuel Langlois 2012-2014
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using Gtk;
using System.Text;
namespace DMX2
{
[System.ComponentModel.ToolboxItem(true)]
public partial class SeqMidiUI : SequenceurUI
{
bool fullUpdFlag = true;
bool updating;
SequenceurMidi sequenceur; /* pointe sur les données */
ListStore lsEffets=null; /* liste des effets */
//TreeViewColumn nomCol; /* inutile dans le contexte macro */
ListStore lsDest = null;
bool effetChange = false;
public void EffetChange ()
{
effetChange = true;
}
DateTime nextMaj = DateTime.Now;
void RenamePopup (object sender, ContextMenuEventArgs e)
{
Menu m = new Menu();
MenuItem renameItem = new MenuItem("Renommer le Sequenceur");
renameItem.ButtonPressEvent += new ButtonPressEventHandler(OnRenameItemButtonPressed);
m.Add(renameItem);
m.ShowAll();
m.Popup();
}
void OnRenameItemButtonPressed (object o, ButtonPressEventArgs args)
{
var dlg = new Dialog ("Nouveau Nom ?", GetAncestor(Gtk.Window.GType) as Gtk.Window , DialogFlags.Modal); var entry = new Entry (sequenceur.Name);
dlg.AddButton (Stock.Ok, ResponseType.Ok).GrabDefault(); dlg.AddButton (Stock.Cancel, ResponseType.Cancel);
dlg.VBox.Add (entry); dlg.VBox.ShowAll ();
entry.ActivatesDefault=true;
if ((ResponseType)dlg.Run () == ResponseType.Ok) {
sequenceur.Name = entry.Text;
titreLabel.Text = sequenceur.Name;
}
dlg.Destroy();
}
#region construction et gestion de la matrice d'affichage
protected void ConstruitMatrice () /* Matrice d'affichage des effets de la macro */
{
cmdList.EnableGridLines= TreeViewGridLines.Both;
var idCol = new Gtk.TreeViewColumn ();
var idCell = new Gtk.CellRendererText ();
idCol.Title = "Num";
idCol.PackStart (idCell, true);
idCol.SetCellDataFunc (idCell, new Gtk.TreeCellDataFunc (RenderMatriceNum));
cmdList.AppendColumn (idCol);
var nomCol = new Gtk.TreeViewColumn ();
var nomCell = new Gtk.CellRendererText ();
nomCol.Title = "Nom";
nomCol.PackStart (nomCell, true);
nomCol.SetCellDataFunc (nomCell, new Gtk.TreeCellDataFunc (RenderMatriceNom));
nomCol.Resizable = true;
nomCell.Editable = true;
nomCell.Edited += OnNomCellEdited;
cmdList.AppendColumn (nomCol);
var topCol = new Gtk.TreeViewColumn ();
var topCell = new Gtk.CellRendererText ();
topCol.Title = "Top";
topCol.PackStart (topCell, true);
topCol.SetCellDataFunc (topCell, new Gtk.TreeCellDataFunc (RenderMatriceTop));
topCell.Editable = true;
topCell.Edited += OnTopCellEdited;
cmdList.AppendColumn (topCol);
var cmdCol = new Gtk.TreeViewColumn ();
var cmdCell = new Gtk.CellRendererText ();
cmdCol.Title = "Commande Midi";
cmdCol.PackStart (cmdCell, true);
cmdCol.SetCellDataFunc (cmdCell, new TreeCellDataFunc (RenderMatriceCmd));
cmdCell.Editable = true;
cmdCell.Edited += OnCmdCellEdited;
cmdList.AppendColumn (cmdCol);
lsEffets = new Gtk.ListStore(typeof (SequenceurMidi.Ligne));
this.cmdList.Model = lsEffets;
UpdListeEffets();
}
void RenderMatriceNum (TreeViewColumn tree_column, CellRenderer cell, TreeModel tree_model, TreeIter iter)
{
if (Conduite.Courante==null) return;
string num=string.Empty;
SequenceurMidi.Ligne l = tree_model.GetValue (iter, 0) as SequenceurMidi.Ligne;
if( sequenceur.IndexLigneEnCours == sequenceur.Lignes.IndexOf(l) )
num+= "->";
if( sequenceur.IndexLigneaSuivre == sequenceur.Lignes.IndexOf(l) )
num+= "* ";
(cell as Gtk.CellRendererText).Text = num + (sequenceur.Lignes.IndexOf(l)+1).ToString();
}
void RenderMatriceNom (TreeViewColumn tree_column, CellRenderer cell, TreeModel tree_model, TreeIter iter)
{
if (Conduite.Courante==null) return;
SequenceurMidi.Ligne l = tree_model.GetValue (iter, 0) as SequenceurMidi.Ligne;
(cell as Gtk.CellRendererText).Text = l.Nom;
}
void RenderMatriceTop (TreeViewColumn tree_column, CellRenderer cell, TreeModel tree_model, TreeIter iter)
{
if (Conduite.Courante==null) return;
SequenceurMidi.Ligne l = tree_model.GetValue (iter, 0) as SequenceurMidi.Ligne;
if (l.Top< TimeSpan.Zero) (cell as Gtk.CellRendererText).Text = string.Empty;
else (cell as Gtk.CellRendererText).Text = (l.Top.TotalMilliseconds /100).ToString();
}
void RenderMatriceCmd (TreeViewColumn tree_column, CellRenderer cell, TreeModel tree_model, TreeIter iter)
{
if (Conduite.Courante==null) return;
SequenceurMidi.Ligne l = tree_model.GetValue (iter, 0) as SequenceurMidi.Ligne;
(cell as Gtk.CellRendererText).Text = l.Commande;
}
void OnNomCellEdited (object o, EditedArgs args)
{
Gtk.TreeIter iter;
lsEffets.GetIter (out iter, new Gtk.TreePath (args.Path));
SequenceurMidi.Ligne l = lsEffets.GetValue(iter,0) as SequenceurMidi.Ligne;
l.Nom = args.NewText;
}
void OnTopCellEdited (object o, EditedArgs args)
{
Gtk.TreeIter iter;
lsEffets.GetIter (out iter, new Gtk.TreePath (args.Path));
SequenceurMidi.Ligne l = lsEffets.GetValue (iter, 0) as SequenceurMidi.Ligne;
if (args.NewText.Length == 0)
l.Top = TimeSpan.MinValue;
else {
int val;
if(int.TryParse(args.NewText, out val))
l.Top = TimeSpan.FromMilliseconds(val *100);
}
}
void OnCmdCellEdited (object o, EditedArgs args)
{
Gtk.TreeIter iter;
lsEffets.GetIter (out iter, new Gtk.TreePath (args.Path));
SequenceurMidi.Ligne l = lsEffets.GetValue(iter,0) as SequenceurMidi.Ligne;
l.Commande = args.NewText;
}
void UpdListeEffets ()
{
lsEffets.Clear();
foreach (var ligne in sequenceur.Lignes) {
lsEffets.AppendValues(ligne);
}
}
void HandleCellLayoutDataFunc(CellLayout cell_layout, CellRenderer cell, TreeModel tree_model, TreeIter iter)
{
var s = (tree_model.GetValue(iter, 0) as string);
(cell as Gtk.CellRendererText).Text = "<<-- " + s + " -->>";
}
#endregion
public SeqMidiUI (SequenceurMidi s ) : base (s)
{
this.Build ();
titreLabel.Text = s.Name;
sequenceur = s;
ConstruitMatrice ();
new ContextMenuHelper(frame1,RenamePopup);
new ContextMenuHelper(evBBox,CompteurPopup);
/*lsDest = new ListStore(typeof(string));
lsDest.AppendValues("TEST 1");
lsDest.AppendValues("TEST 2");
cbDest.Model = lsDest;
cbDest.SetCellDataFunc(cbDest.Cells[0], HandleCellLayoutDataFunc);*/
}
void CompteurPopup (object sender, ContextMenuEventArgs e)
{
Menu m = new Menu();
MenuItem item = new MenuItem("Effet Suivant");
m.Add(item);
item.Submenu = Conduite.Courante.EventManager.GetMenu(null,
delegate(object o,string eventId){
sequenceur.BindEffetSuivantEvent(eventId);
}
);
item = new MenuItem("Effet Precedent");
m.Add(item);
item.Submenu = Conduite.Courante.EventManager.GetMenu(null,
delegate(object o,string eventId){
sequenceur.BindEffetPrecedentEvent(eventId);
}
);
item = new Gtk.SeparatorMenuItem ();
m.Add(item);
m.ShowAll();
m.Popup();
}
static public object PortKey = new object();
public override void Update (bool full)
{
if (fullUpdFlag || full)
FullUpdate ();
// UpdateValues (); inutil dans le contexte
timeLabel.LabelProp = string.Format (@"{0:0\.0}", sequenceur.TimeStamp.TotalMilliseconds / 100);
if (effetChange) {
//UpdListeEffets();
cmdList.QueueDraw();
SelectionneEffet (sequenceur.IndexLigneEnCours);
posLabel.Text = string.Format("n°{0}",sequenceur.IndexLigneEnCours +1 );
}
if (DateTime.Now > nextMaj ) {
nextMaj = DateTime.Now.AddMilliseconds(500);
StringBuilder str = new StringBuilder ();
actLabel.LabelProp = str.ToString ();
}
}
void SelectionneEffet (int index)
{
if(index <0 ) return;
cmdList.SetCursor( new TreePath( new int[1] {index }) ,null,false);
effetChange = false;
}
protected void OnCloseActionActivated (object sender, EventArgs e)
{
this.Destroy();
}
protected void FullUpdate ()
{
fullUpdFlag = true;
posLabel.Text = string.Format("n°{0}",sequenceur.IndexLigneEnCours +1);
fullUpdFlag=false;
}
int IndexEffetSelectionne()
{
var sel = cmdList.Selection.GetSelectedRows();
if(sel.Length ==0) return -1;
return cmdList.Selection.GetSelectedRows()[0].Indices[0];
}
protected void OnGoForwardActionActivated (object sender, EventArgs e)
{
sequenceur.LigneSuivante();
}
protected void OnGoBackActionActivated (object sender, EventArgs e)
{
if (sequenceur.IndexLigneEnCours > 0) {
sequenceur.IndexLigneaSuivre = sequenceur.IndexLigneEnCours - 1;
sequenceur.LigneSuivante ();
}
}
protected void OnMediaPauseActionActivated (object sender, EventArgs e)
{
sequenceur.Paused = ! sequenceur.Paused;
}
protected void OnBtnAjoutLigneActivated (object sender, EventArgs e)
{
int pos=IndexEffetSelectionne() + 1;
sequenceur.AjoutLigne(pos);
UpdListeEffets();
cmdList.SetCursor( new TreePath( new int[1] {pos }) , cmdList.Columns[1] ,true);
}
protected void OnRemoveLigneActivated (object sender, EventArgs e)
{
int pos = IndexEffetSelectionne();
if (pos==-1) return;
sequenceur.RetireLigne(pos);
UpdListeEffets();
}
protected void OnCmdListeCursorChanged (object sender, EventArgs e)
{
if(effetChange) return;
TreeViewColumn col;
TreePath path;
cmdList.GetCursor (out path, out col);
if (cmdList.Columns [0] == col) {
sequenceur.IndexLigneaSuivre = IndexEffetSelectionne();
}
}
protected void OnHbox2SizeAllocated (object o, SizeAllocatedArgs args)
{
actLabel.SetSizeRequest ( Math.Max(50, args.Allocation.Width - posLabel.Allocation.Width - timeLabel.Allocation.Width - 50),-1);
}
}
}