]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/clef-engraver.cc
Run fixcc + astyle2.02.
[lilypond.git] / lily / clef-engraver.cc
index 8195fbd9bf6c382b4e76f2f489e7fdcf15ac3ebf..00c345771728f4e46a06ed75ce08c023b0740f1c 100644 (file)
 /*
-  clef.cc -- implement Clef_engraver
+  This file is part of LilyPond, the GNU music typesetter.
 
-  source file of the GNU LilyPond music typesetter
-
-  (c)  1997--1998 Han-Wen Nienhuys <hanwen@cs.uu.nl>,
+  Copyright (C) 1997--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
   Mats Bengtsson <matsb@s3.kth.se>
+
+  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 <ctype.h>
-#include "bar.hh"
-#include "clef-engraver.hh"
-#include "clef-item.hh"
-#include "debug.hh"
-#include "command-request.hh"
-#include "time-description.hh"
+#include <cctype>
+using namespace std;
+
+#include "item.hh"
+#include "context.hh"
+#include "staff-symbol-referencer.hh"
+#include "engraver.hh"
+#include "direction.hh"
+#include "side-position-interface.hh"
 
+#include "translator.icc"
 
-#include "note-head.hh"
-#include "key-item.hh"
-#include "local-key-item.hh"
+class Clef_engraver : public Engraver
+{
+public:
+  TRANSLATOR_DECLARATIONS (Clef_engraver);
+
+protected:
+  void stop_translation_timestep ();
+  void process_music ();
+  DECLARE_ACKNOWLEDGER (bar_line);
+
+  virtual void derived_mark () const;
+private:
+  Item *clef_;
+  Item *octavate_;
+
+  SCM prev_glyph_;
+  SCM prev_cpos_;
+  SCM prev_octavation_;
+  void create_clef ();
+  void set_glyph ();
+  void inspect_clef_properties ();
+};
 
-Clef_engraver::Clef_engraver()
+void
+Clef_engraver::derived_mark () const
 {
-  clef_p_ = 0;
-  clef_req_l_ = 0;
-  clef_type_str_ = "";
-  c0_position_i_ = 0;
-  clef_position_i_ = 0;
-  octave_dir_ = CENTER;
+  scm_gc_mark (prev_octavation_);
+  scm_gc_mark (prev_cpos_);
+  scm_gc_mark (prev_glyph_);
 }
 
-/*
-  Ugh.  Should have support for Dictionaries in mudela.
- */
-bool
-Clef_engraver::set_type (String s)
+Clef_engraver::Clef_engraver ()
 {
-  if (s.right_str(2) == "_8") // Down one octave
-    {
-      octave_dir_ = DOWN;
-      s = s.left_str(s.length_i() - 2);
-    }
-  else if (s.right_str(2) == "^8") // Up one octave
-    {
-      octave_dir_ = UP;
-      s = s.left_str(s.length_i() - 2);
-    }
-  else
-    octave_dir_ = CENTER;
-  if (s == "treble" ||
-      s == "violin" ||
-      s == "G" || s == "G2")
-    {
-      clef_type_str_ = "violin";
-      clef_position_i_ = -2;
-    }
-  else if (s == "french")
-    {
-      clef_type_str_ = "violin";
-      clef_position_i_ = -4;
-    }
-  else if (s == "soprano")
-    {
-      clef_type_str_ = "alto";
-      clef_position_i_ = -4;
-    }
-  else if (s == "mezzosoprano")
-    {
-      clef_type_str_ = "alto";
-      clef_position_i_ = -2;
-    }
-  else if (s == "alto")
-    {
-      clef_type_str_ = "alto";
-      clef_position_i_ = 0;
-    }
-  else if (s == "tenor")
-    {
-      clef_type_str_ = "alto";
-      clef_position_i_ = 2;
-    }
-  else if (s == "baritone")
-    {
-      clef_type_str_ = "alto";
-      clef_position_i_ = 4;
-    }
-  else if (s == "varbaritone")
-    {
-      clef_type_str_ = "bass";
-      clef_position_i_ = 0;
-    }
-  else if (s == "bass" || s == "F")
-    {
-      clef_type_str_ = "bass";
-      clef_position_i_ = 2;
-    }
-  else if (s == "subbass")
-    {
-      clef_type_str_ = "bass";
-      clef_position_i_ = 4;
-    }
-  else 
-    {
-      switch(toupper (s[0]))
-       {
-       case 'F': 
-         clef_type_str_ = "bass";
-         break;
-       case  'G':
-         clef_type_str_ = "violin";
-         break;
-       case 'C': 
-         clef_type_str_ = "alto";
-         break;
-       default:
-         return false;
-       }
-      clef_position_i_ = 2 * (s[1] - '0') - 6;
-    }
-  if (clef_type_str_ == "violin")
-    c0_position_i_ = clef_position_i_ - 4;
-  else if (clef_type_str_ == "alto")
-    c0_position_i_ = clef_position_i_;
-  else if (clef_type_str_ == "bass")
-    c0_position_i_ = clef_position_i_ + 4;
-  else
-    assert (false);
-
-      
-  c0_position_i_ -= (int) octave_dir_ * 7;
-  
-  return true;
+  clef_ = 0;
+  octavate_ = 0;
+
+  /*
+    will trigger a clef at the start since #f != ' ()
+  */
+  prev_octavation_ = prev_cpos_ = prev_glyph_ = SCM_BOOL_F;
 }
 
 void
-Clef_engraver::read_req (Clef_change_req*c_l)
+Clef_engraver::set_glyph ()
 {
-  if (!set_type (c_l->clef_str_))
-    c_l->error (_ ("unknown clef type "));
+  SCM glyph_sym = ly_symbol2scm ("glyph");
+  SCM basic = ly_symbol2scm ("Clef");
+
+  execute_pushpop_property (context (), basic, glyph_sym, SCM_UNDEFINED);
+  execute_pushpop_property (context (), basic, glyph_sym, get_property ("clefGlyph"));
 }
 
+/**
+   Generate a clef at the start of a measure. (when you see a Bar,
+   ie. a breakpoint)
+*/
+void
+Clef_engraver::acknowledge_bar_line (Grob_info info)
+{
+  Item *item = info.item ();
+  if (item && scm_is_string (get_property ("clefGlyph")))
+    create_clef ();
+}
 
-/** 
-  Generate a clef at the start of a measure. (when you see a Bar,
-  ie. a breakpoint) 
-  */
 void
-Clef_engraver::acknowledge_element (Score_element_info info)
+Clef_engraver::create_clef ()
 {
-  if (info.elem_l_->is_type_b (Bar::static_name ()) 
-      && clef_type_str_.length_i())
+  if (!clef_)
     {
-      create_clef();
-      if (!clef_req_l_)
-       clef_p_->default_b_ = true;
-    }
+      Item *c = make_item ("Clef", SCM_EOL);
 
-  /* ugh; should make Clef_referenced baseclass */
-  Item * it_l =info.elem_l_->access_Item ();
-if (it_l)
-  {
-  if (it_l->is_type_b (Note_head::static_name ()))
-    {
-      Note_head * h = (Note_head*)it_l;
-      h->position_i_ += c0_position_i_;
-    }
-  else if (it_l->is_type_b (Local_key_item::static_name ()))
-    {
-      Local_key_item *i = (Local_key_item*)it_l;
-      i->c0_position_i_ =c0_position_i_;
-    }
-  else if (it_l->is_type_b (Key_item::static_name ()))
-    {
-      Key_item *k = (Key_item*)it_l;
-      k-> set_c_position (c0_position_i_);
+      clef_ = c;
+      SCM cpos = get_property ("clefPosition");
+
+      if (scm_is_number (cpos))
+        clef_->set_property ("staff-position", cpos);
+
+      SCM oct = get_property ("clefOctavation");
+      if (scm_is_number (oct) && scm_to_int (oct))
+        {
+          Item *g = make_item ("OctavateEight", SCM_EOL);
+
+          int abs_oct = scm_to_int (oct);
+          int dir = sign (abs_oct);
+          abs_oct = abs (abs_oct) + 1;
+
+          SCM txt = scm_number_to_string (scm_from_int (abs_oct),
+                                          scm_from_int (10));
+
+          g->set_property ("text",
+                           scm_list_n (ly_lily_module_constant ("vcenter-markup"),
+                                       txt, SCM_UNDEFINED));
+          Side_position_interface::add_support (g, clef_);
+
+          g->set_parent (clef_, Y_AXIS);
+          g->set_parent (clef_, X_AXIS);
+          g->set_property ("direction", scm_from_int (dir));
+          octavate_ = g;
+        }
     }
-  } 
 }
-
 void
-Clef_engraver::do_creation_processing()
+Clef_engraver::process_music ()
 {
-  Scalar def = get_property ("defaultClef");
-  if (def.to_bool ()) // egcs: Scalar to bool is ambiguous
-    set_type (def);
-  
-  if (clef_type_str_.length_i ())
-    { 
-      create_clef();
-      clef_p_->default_b_ = false;
-    }
+  inspect_clef_properties ();
 }
 
-bool
-Clef_engraver::do_try_request (Request * r_l)
+static void apply_on_children (Context *context, SCM fun)
 {
-  Command_req* creq_l= r_l->access_Command_req ();
-  if (!creq_l || !creq_l->access_Clef_change_req ())
-    return false;
-
-  clef_req_l_ = creq_l->access_Clef_change_req ();
-  read_req (clef_req_l_);
-  return true;
+  scm_call_1 (fun, context->self_scm ());
+  for (SCM s = context->children_contexts ();
+       scm_is_pair (s); s = scm_cdr (s))
+    apply_on_children (unsmob_context (scm_car (s)), fun);
 }
 
 void
-Clef_engraver::create_clef()
+Clef_engraver::inspect_clef_properties ()
 {
-  if (!clef_p_)
-    {
-      clef_p_ = new Clef_item;
-      clef_p_->break_priority_i_ = -2; // ugh
-      announce_element (Score_element_info (clef_p_,clef_req_l_));
-    }
-  clef_p_->read (*this);
-}
+  SCM glyph = get_property ("clefGlyph");
+  SCM clefpos = get_property ("clefPosition");
+  SCM octavation = get_property ("clefOctavation");
+  SCM force_clef = get_property ("forceClef");
 
-void
-Clef_engraver::do_process_requests()
-{
-  if (clef_req_l_)
+  if (clefpos == SCM_EOL
+      || scm_equal_p (glyph, prev_glyph_) == SCM_BOOL_F
+      || scm_equal_p (clefpos, prev_cpos_) == SCM_BOOL_F
+      || scm_equal_p (octavation, prev_octavation_) == SCM_BOOL_F
+      || to_boolean (force_clef))
     {
-      create_clef();
-      clef_p_->default_b_ = false;
+      apply_on_children (context (),
+                         ly_lily_module_constant ("invalidate-alterations"));
+
+      set_glyph ();
+      if (prev_cpos_ != SCM_BOOL_F || to_boolean (get_property ("firstClef")))
+        create_clef ();
+
+      if (clef_)
+        clef_->set_property ("non-default", SCM_BOOL_T);
+
+      prev_cpos_ = clefpos;
+      prev_glyph_ = glyph;
+      prev_octavation_ = octavation;
     }
-}
 
-void
-Clef_engraver::do_pre_move_processing()
-{
-  if (clef_p_)
+  if (to_boolean (force_clef))
     {
-      typeset_element (clef_p_);
-      clef_p_ = 0;
+      SCM prev;
+      Context *w = context ()->where_defined (ly_symbol2scm ("forceClef"), &prev);
+      w->set_property ("forceClef", SCM_EOL);
     }
 }
-void
-Clef_engraver::do_post_move_processing()
-{
-  clef_req_l_ = 0;
-}
 
 void
-Clef_engraver::do_removal_processing()
+Clef_engraver::stop_translation_timestep ()
 {
-  if (clef_p_)
+  if (clef_)
     {
-      clef_p_->unlink ();      
-      delete clef_p_;
-      clef_p_ =0;
+      SCM vis = 0;
+      if (to_boolean (clef_->get_property ("non-default")))
+        vis = get_property ("explicitClefVisibility");
+
+      if (vis)
+        clef_->set_property ("break-visibility", vis);
+
+      clef_ = 0;
+
+      octavate_ = 0;
     }
 }
 
-
-IMPLEMENT_IS_TYPE_B1(Clef_engraver,Engraver);
-ADD_THIS_TRANSLATOR(Clef_engraver);
+ADD_ACKNOWLEDGER (Clef_engraver, bar_line);
+ADD_TRANSLATOR (Clef_engraver,
+                /* doc */
+                "Determine and set reference point for pitches.",
+
+                /* create */
+                "Clef "
+                "OctavateEight ",
+
+                /* read */
+                "clefGlyph "
+                "clefOctavation "
+                "clefPosition "
+                "explicitClefVisibility "
+                "forceClef ",
+
+                /* write */
+                ""
+               );