void panelTrackName_DragDrop(object sender, DragEventArgs e)
 {
     var d = e.GetDropObject<PEMidiTrack>();
     if (d != null && d != this)
     {
         ItemDropped.IfObjectNotNull(x => x(this, e));
     }
 }
 void panelTrackName_DragEnter(object sender, DragEventArgs e)
 {
     e.Effect = DragDropEffects.None;
     var d = e.GetDropObject<PEMidiTrack>();
     if (d != null && d != this)
     {
         e.Effect = DragDropEffects.All;
     }
 }
        void Button_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.None;
            var dropDiff = e.GetDropObject<PETrackDifficulty>();
            if (dropDiff != null)
            {
                var btn = DifficultyButtons.SingleOrDefault(x => x.Button == sender);

                if ((dropDiff.MidiTrack != btn.MidiTrack) || (dropDiff.MidiTrack == btn.MidiTrack && dropDiff.Difficulty != btn.Difficulty))
                {
                    e.Effect = DragDropEffects.All;
                }
            }
        }
        void Button_DragDrop(object sender, DragEventArgs e)
        {
            var dropDiff = e.GetDropObject<PETrackDifficulty>();
            if (dropDiff != null)
            {
                var btn = DifficultyButtons.SingleOrDefault(x => x.Button == sender);

                if ((dropDiff.MidiTrack != btn.MidiTrack) || (dropDiff.MidiTrack == btn.MidiTrack && dropDiff.Difficulty != btn.Difficulty))
                {
                    DifficultyItemDropped.IfObjectNotNull(d => d(this, DifficultyButtons.Single(x => x.Button == sender).Difficulty, e));
                }
            }
        }
        void peTrack_ItemDropped(PEMidiTrack sender, DragEventArgs e)
        {
            var peTrack = e.GetDropObject<PEMidiTrack>();
            peTrack.IfObjectNotNull(x =>
            {
                if (peTrack != sender)
                {
                    DoRequestBackup();

                    Track newTrack = null;
                    if (sender.Track.Sequence == peTrack.Track.Sequence)
                    {
                        newTrack = peTrack.Track;
                        sender.Track.Sequence.MoveTrack(peTrack.Track.GetTrackIndex(), GetInsertAt());
                    }
                    else
                    {
                        if (IsPro && peTrack.Track.Name.IsProTrackName())
                        {
                            newTrack = new Track(FileType.Pro, peTrack.Track.Name);

                            newTrack.Merge(peTrack.Track);
                        }
                        else if (IsPro == false && peTrack.Track.Name.IsProTrackName() == false)
                        {
                            newTrack = new Track(FileType.Guitar5, peTrack.Track.Name);

                            newTrack.Merge(sender.Track);
                        }
                        else
                        {
                            newTrack = peTrack.Track.Clone(sender.Track.FileType);
                        }

                        newTrack.Name = sender.Track.Name;

                        sender.Track.Sequence.Insert(GetInsertAt(), newTrack);

                        if (!ModifierKeys.HasFlag(Keys.Shift))
                        {
                            sender.Track.Sequence.Remove(sender.Track);
                        }
                    }
                    Refresh();
                    SetSelectedItem(newTrack, SelectedDifficulty);

                    t_TrackClicked(this, newTrack, SelectedDifficulty);

                }
            });
            dragItem = null;
        }
        void peTrack_DifficultyItemDropped(PEMidiTrack sender, GuitarDifficulty difficulty, DragEventArgs e)
        {
            var o = e.GetDropObject<PETrackDifficulty>();
            o.IfObjectNotNull(op =>
            {
                DoRequestBackup();

                var messages = sender.Track.GetChanMessagesByDifficulty(difficulty);
                sender.Track.Remove(messages);

                var otrack = o.MidiTrack.Track;

                var clonedTrack = otrack.CloneDifficulty(o.Difficulty, difficulty, sender.Track.FileType);

                var clonedMessages = clonedTrack.GetChanMessagesByDifficulty(difficulty).ToList();

                foreach(var msg in clonedMessages)
                {
                    sender.Track.Insert(msg.AbsoluteTicks, msg.Clone());
                }

                SetSelectedItem(sender.Track, o.Difficulty);

                TrackClicked.IfObjectNotNull(x => x(this, sender.Track.Sequence, sender.Track, difficulty));
            });
        }
        private void panelTracks_DragDrop(object sender, DragEventArgs e)
        {
            var droppedPETrack = e.GetDropObject<PEMidiTrack>();
            droppedPETrack.IfObjectNotNull(xx =>
            {
                if (droppedPETrack != sender)
                {
                    DoRequestBackup();

                    Track newTrack = null;
                    if (Sequence == null)
                    {
                        var targetType = this.IsPro ? FileType.Pro : FileType.Guitar5;

                        var seq = new Sequence(targetType, droppedPETrack.Track.Sequence.Division);
                        this.sequence = seq;
                        if (!droppedPETrack.Track.IsTempo())
                        {
                            var tempo = droppedPETrack.Track.Sequence.Tracks.Where(x => x.IsTempo());
                            if (tempo.Any())
                            {
                                seq.AddTempo(tempo.First().ConvertToPro());
                            }
                        }
                        newTrack = droppedPETrack.Track.Clone(seq.FileType);
                        seq.Add(newTrack);
                    }
                    else
                    {

                        if (this.Sequence == droppedPETrack.Track.Sequence)
                        {
                            this.Sequence.MoveTrack(droppedPETrack.Track.GetTrackIndex(), GetInsertAt());
                        }
                        else
                        {
                            newTrack = droppedPETrack.Track.Clone(Sequence.FileType);

                            sequence.Insert(GetInsertAt(), newTrack);

                            if (!sequence.Tracks.Any(x => x.IsTempo()))
                            {
                                var tempo = droppedPETrack.Track.Sequence.Tracks.Where(x => x.IsTempo());
                                if (tempo.Any())
                                {
                                    sequence.AddTempo(tempo.First().ConvertToPro());
                                }
                            }
                        }
                    }

                    CreatePanelTracks(this.sequence);
                    TrackAdded.IfObjectNotNull(tc => tc(this, this.sequence, newTrack, SelectedDifficulty));
                }
            });
            dragItem = null;
        }