]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/percent-repeat-engraver.cc
Run grand-replace (issue 3765)
[lilypond.git] / lily / percent-repeat-engraver.cc
index 208ba3600f42eddc85e7fff359f91702846df8e6..008081f138f4ee897e2814cf062d04d9292801ac 100644 (file)
 /*
-  new-chord-tremolo-engraver.cc -- implement Chord_tremolo_engraver
+  This file is part of LilyPond, the GNU music typesetter.
 
-  source file of the GNU LilyPond music typesetter
+  Copyright (C) 2000--2014 Han-Wen Nienhuys <hanwen@xs4all.nl>, Erik Sandberg <mandolaerik@gmail.com>
 
-  (c) 2000--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+  LilyPond 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 3 of the License, or
+  (at your option) any later version.
+
+  LilyPond 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 LilyPond.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#include "repeated-music.hh"
+#include "engraver.hh"
 #include "global-context.hh"
-#include "warn.hh"
-#include "misc.hh"
-#include "spanner.hh"
+#include "international.hh"
 #include "item.hh"
-#include "percent-repeat-iterator.hh"
-#include "bar-line.hh"
-#include "score-engraver.hh"
-
-/**
-   This acknowledges repeated music with "percent" style.  It typesets
-   a % sign.
-
-   TODO:
+#include "side-position-interface.hh"
+#include "spanner.hh"
+#include "stream-event.hh"
+#include "warn.hh"
 
-   - BEAT case: Create items for single beat repeats, i.e. c4 / / /
+#include "translator.icc"
 
-   - DOUBLE_MEASURE case: attach a % to an appropriate barline.
-*/
 class Percent_repeat_engraver : public Engraver
 {
   void typeset_perc ();
+
 public:
   TRANSLATOR_DECLARATIONS (Percent_repeat_engraver);
+
 protected:
-  Music *repeat_;
+  Stream_event *percent_event_;
 
-  /// moment (global time) where beam started.
+  // moment (global time) where percent started
   Moment start_mom_;
+  // moment (global time) where percent should end
   Moment stop_mom_;
 
-  /// location  within measure where beam started.
-  Moment beam_start_location_;
-  Moment next_moment_;
-  Moment body_length_;
+  Spanner *percent_;
+  Spanner *percent_counter_;
 
-  enum
-    {
-      UNKNOWN,
-      MEASURE,
-      DOUBLE_MEASURE,
-    }
-    repeat_sign_type_;
+  Grob *first_command_column_;
+  Moment command_moment_;
 
-  Item *double_percent_;
-  Spanner *perc_;
-  Spanner *finished_perc_;
-protected:
   virtual void finalize ();
-  virtual bool try_music (Music *);
-  PRECOMPUTED_VIRTUAL void stop_translation_timestep ();
-  PRECOMPUTED_VIRTUAL void start_translation_timestep ();
-  PRECOMPUTED_VIRTUAL void process_music ();
+  DECLARE_TRANSLATOR_LISTENER (percent);
+
+  void start_translation_timestep ();
+  void stop_translation_timestep ();
+  void process_music ();
 };
 
 Percent_repeat_engraver::Percent_repeat_engraver ()
 {
-  perc_ = 0;
-  finished_perc_ = 0;
-  repeat_ = 0;
+  percent_ = 0;
+  percent_counter_ = 0;
+  percent_event_ = 0;
 
-  double_percent_ = 0;
+  first_command_column_ = 0;
+  command_moment_ = Moment (-1);
 }
 
-bool
-Percent_repeat_engraver::try_music (Music *m)
+void
+Percent_repeat_engraver::start_translation_timestep ()
 {
-  if (m->is_mus_type ("repeated-music")
-      && m->get_property ("iterator-ctor")
-      == Percent_repeat_iterator::constructor_proc
-      && !repeat_)
+  if (now_mom ().main_part_ != command_moment_.main_part_)
     {
-      body_length_ = Repeated_music::body_get_length (m);
-      int count = Repeated_music::repeat_count (m);
-
-      Moment now = now_mom ();
-      start_mom_ = now;
-      stop_mom_ = start_mom_ + Moment (count) * body_length_;
-      next_moment_ = start_mom_ + body_length_;
-
-      Moment meas_len (robust_scm2moment (get_property ("measureLength"), Moment (1)));
-      if (meas_len == body_length_)
-       repeat_sign_type_ = MEASURE;
-      else if (Moment (2) * meas_len == body_length_)
-       {
-         repeat_sign_type_ = DOUBLE_MEASURE;
-       }
-      else
-       {
-         warning (_f ("can't handle a percent repeat of length: %s",
-                      body_length_.to_string ()));
-         return false;
-       }
-
-      repeat_ = m;
-
-      Global_context *global = get_global_context ();
-      for (int i = 1; i < count; i++)
-       {
-         Moment m = next_moment_ + Moment (i) * body_length_;
-         global->add_moment_to_process (m);
-
-         /* bars between % too.  */
-         if (repeat_sign_type_ == DOUBLE_MEASURE)
-           global->add_moment_to_process (m - meas_len);
-       }
-
-      if (repeat_sign_type_ == DOUBLE_MEASURE)
-       next_moment_ += meas_len;
-      
-      return true;
+      first_command_column_
+        = unsmob_grob (get_property ("currentCommandColumn"));
+      command_moment_ = now_mom ();
     }
 
-  return false;
+  if (stop_mom_.main_part_ == now_mom ().main_part_)
+    {
+      if (percent_)
+        typeset_perc ();
+      percent_event_ = 0;
+    }
 }
 
+IMPLEMENT_TRANSLATOR_LISTENER (Percent_repeat_engraver, percent);
 void
-Percent_repeat_engraver::process_music ()
+Percent_repeat_engraver::listen_percent (Stream_event *ev)
 {
-  if (repeat_ && now_mom () == next_moment_)
+  if (!percent_event_)
+    {
+      Moment body_length = get_event_length (ev);
+      start_mom_ = now_mom ();
+      stop_mom_ = now_mom () + body_length;
+      get_global_context ()->add_moment_to_process (stop_mom_);
+      percent_event_ = ev;
+    }
+  else
     {
-      if (repeat_sign_type_ == MEASURE)
-       {
-         finished_perc_ = perc_;
-         typeset_perc ();
-         perc_ = make_spanner ("PercentRepeat", repeat_->self_scm ());
-         SCM col = get_property ("currentCommandColumn");
-         perc_->set_bound (LEFT, unsmob_grob (col));
-       }
-      else if (repeat_sign_type_ == DOUBLE_MEASURE)
-       {
-         double_percent_ = make_item ("DoublePercentRepeat", repeat_->self_scm ());
-         
-         /*
-           forbid breaks on a % line. Should forbid all breaks, really.
-
-           Ugh. Why can't this be regular communication between
-           contexts?
-         */
-         get_score_engraver ()->forbid_breaks ();
-       }
-      next_moment_ = next_moment_ + body_length_;
+      /*
+        print a warning: no assignment happens because
+        percent_event_ != 0
+      */
+      ASSIGN_EVENT_ONCE (percent_event_, ev);
     }
 }
 
 void
-Percent_repeat_engraver::finalize ()
+Percent_repeat_engraver::process_music ()
 {
-  typeset_perc ();
-  if (perc_)
+  if (percent_event_
+      && now_mom ().main_part_ == start_mom_.main_part_)
     {
-      repeat_->origin ()->warning (_ ("unterminated percent repeat"));
-      perc_->suicide ();
+      if (percent_)
+        typeset_perc ();
+
+      percent_ = make_spanner ("PercentRepeat", percent_event_->self_scm ());
+
+      Grob *col = first_command_column_;
+      percent_->set_bound (LEFT, col);
+
+      SCM count = percent_event_->get_property ("repeat-count");
+      if (count != SCM_EOL && to_boolean (get_property ("countPercentRepeats"))
+          && check_repeat_count_visibility (context (), count))
+        {
+          percent_counter_ = make_spanner ("PercentRepeatCounter",
+                                           percent_event_->self_scm ());
+
+          SCM text = scm_number_to_string (count, scm_from_int (10));
+          percent_counter_->set_property ("text", text);
+          percent_counter_->set_bound (LEFT, col);
+          Side_position_interface::add_support (percent_counter_, percent_);
+          percent_counter_->set_parent (percent_, Y_AXIS);
+        }
+      else
+        percent_counter_ = 0;
     }
 }
 
 void
-Percent_repeat_engraver::typeset_perc ()
+Percent_repeat_engraver::finalize ()
 {
-  if (finished_perc_)
+  if (percent_)
     {
-      SCM col = get_property ("currentCommandColumn");
-      finished_perc_->set_bound (RIGHT, unsmob_grob (col));
-      finished_perc_ = 0;
+      percent_event_->origin ()->warning (_ ("unterminated percent repeat"));
+      percent_->suicide ();
+      percent_counter_->suicide ();
     }
-
-  double_percent_ = 0;
 }
 
 void
-Percent_repeat_engraver::start_translation_timestep ()
+Percent_repeat_engraver::typeset_perc ()
 {
-  if (stop_mom_ == now_mom ())
-    {
-      if (perc_)
-       {
-         finished_perc_ = perc_;
-         typeset_perc ();
-       }
-      repeat_ = 0;
-      perc_ = 0;
-      repeat_sign_type_ = UNKNOWN;
-    }
+  Grob *col = first_command_column_;
+
+  percent_->set_bound (RIGHT, col);
+  percent_ = 0;
+
+  if (percent_counter_)
+    percent_counter_->set_bound (RIGHT, col);
+  percent_counter_ = 0;
 }
 
 void
 Percent_repeat_engraver::stop_translation_timestep ()
 {
-  typeset_perc ();
 }
 
-#include "translator.icc"
-
 ADD_TRANSLATOR (Percent_repeat_engraver,
-               /* descr */ "Make whole bar and double bar repeats.",
-               /* creats*/ "PercentRepeat DoublePercentRepeat",
-               /* accepts */ "repeated-music",
-               /* acks  */ "",
-               /* reads */ "measureLength currentCommandColumn",
-               /* write */ "");
+                /* doc */
+                "Make whole measure repeats.",
+
+                /* create */
+                "PercentRepeat "
+                "PercentRepeatCounter ",
+
+                /* read */
+                "countPercentRepeats "
+                "currentCommandColumn "
+                "repeatCountVisibility ",
+
+                /* write */
+                ""
+               );