]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/context.cc
* lily/output-def-scheme.cc: new file.
[lilypond.git] / lily / context.cc
index f11580d3943c3bdba542a8a337ac92bb5adc6825..4cad8ea641f59e266a9f0db88297490e87afb1c3 100644 (file)
@@ -7,10 +7,9 @@
 
 */
 
-#include "object-key.hh"
-#include "context-def.hh"
-#include "context-selector.hh"
 #include "context.hh"
+
+#include "context-def.hh"
 #include "ly-smobs.icc"
 #include "main.hh"
 #include "output-def.hh"
@@ -65,9 +64,7 @@ Context::add_context (Context *t)
   if (!t->init_)
     {
       t->init_ = true;
-#ifdef TWEAK 
-      Context_selector::register_context (t);
-#endif  
+
       scm_gc_unprotect_object (ts);
       Context_def *td = unsmob_context_def (t->definition_);
 
@@ -101,11 +98,62 @@ Context::Context (Object_key const* key)
   definition_ = SCM_EOL;
   
   smobify_self ();
-
   properties_scm_ = (new Scheme_hash_table)->self_scm ();
   scm_gc_unprotect_object (properties_scm_);
+  scm_gc_unprotect_object (key_->self_scm ());
 }
 
+/*
+  TODO:  this shares code with find_create_context().
+ */
+Context*
+Context::create_unique_context (SCM n, SCM operations)
+{
+  /*
+    Don't create multiple score contexts.
+   */
+  if (dynamic_cast<Global_context*> (this)
+      && dynamic_cast<Global_context*> (this)->get_score_context ())
+    return get_score_context ()->create_unique_context (n, operations);
+
+  /*
+    TODO: use accepts_list_.
+   */
+  Link_array<Context_def> path
+    = unsmob_context_def (definition_)->path_to_acceptable_context (n, get_output_def ());
+
+  if (path.size ())
+    {
+      Context * current = this;
+
+      // start at 1.  The first one (index 0) will be us.
+      for (int i = 0; i < path.size (); i++)
+       {
+         SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
+
+         current = current->create_context (path[i],
+                                            "\\new",
+                                            ops); 
+       }
+
+      return current;
+    }
+
+  /*
+    Don't go up to Global_context, because global goes down to
+    Score_context
+   */
+  Context *ret = 0;
+  if (daddy_context_ && !dynamic_cast<Global_context*> (daddy_context_))
+    ret = daddy_context_->create_unique_context (n, operations);
+  else
+    {
+      warning (_f ("Cannot find or create new `%s'",
+                  ly_symbol2string (n).to_str0 ()));
+      ret = 0;
+    }
+  return ret;
+}
 
 
 Context *
@@ -124,7 +172,6 @@ Context::find_create_context (SCM n, String id, SCM operations)
   if (n == ly_symbol2scm ("Bottom"))
     {
       Context* tg = get_default_interpreter ();
-      tg->id_string_ = id;
       return tg;
     }
 
@@ -139,7 +186,7 @@ Context::find_create_context (SCM n, String id, SCM operations)
       Context * current = this;
 
       // start at 1.  The first one (index 0) will be us.
-      for (int i=0; i < path.size (); i++)
+      for (int i = 0; i < path.size (); i++)
        {
          SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
 
@@ -150,9 +197,9 @@ Context::find_create_context (SCM n, String id, SCM operations)
            }
 
 
-         current = create_context (path[i],
-                                   this_id,
-                                   ops); 
+         current = current->create_context (path[i],
+                                            this_id,
+                                            ops); 
        }
 
       return current;
@@ -169,7 +216,7 @@ Context::find_create_context (SCM n, String id, SCM operations)
     {
       warning (_f ("Cannot find or create `%s' called `%s'",
                   ly_symbol2string (n).to_str0 (), id));
-      ret =0;
+      ret = 0;
     }
   return ret;
 }
@@ -184,7 +231,6 @@ Context::create_context (Context_def * cdef,
   Object_key const *key = get_context_key (type, id);
   Context * new_group
     = cdef->instantiate (ops, key);
-  scm_gc_unprotect_object (key->self_scm ());
          
   new_group->id_string_ = id;
   add_context (new_group);
@@ -193,6 +239,8 @@ Context::create_context (Context_def * cdef,
   return new_group;
 }
 
+
+
 Object_key const*
 Context::get_context_key (String type, String id)
 {
@@ -445,7 +493,7 @@ Context::print_smob (SCM s, SCM port, scm_print_state *)
       scm_display (d->get_context_name (), port);
     }
 
-  if (Context *td=dynamic_cast<Context *> (sc))
+  if (Context *td = dynamic_cast<Context *> (sc))
     {
       scm_puts ("=", port);
       scm_puts (td->id_string_.to_str0 (), port);