# -*- coding: ISO-8859-1 -*-
""" capellaScript
>>> 
Stichnoten kopieren
<<<
"""
# 27.11.2004 Hartmut Lemmel

from caplib.capDOM import ScoreChange
from xml.dom.minidom import parse

import tempfile

def getElement(parent, *children):
     """ liefert Kind/Enkel/Urenkel..., z.B. parent.child0.child1.child2... """
     p = parent
     for c in children:
         found = 0
         for n in p.childNodes:
             if n.nodeType == parent.ELEMENT_NODE and n.tagName == c:
                 p = n
                 found = 1
                 break
         if not found: return 0
     return p

class ScoreChange(ScoreChange):  
    def changeScore(self, score):
      global sy1,st1,vo1,ob1,ob2
      sys=score.getElementsByTagName('system')[sy1]
      staff=sys.getElementsByTagName('staff')[st1]
      voice=staff.getElementsByTagName('voice')[vo1]
      notes=voice.getElementsByTagName('noteObjects')[0]
      abbrev=''  
      for staffLayout in score.getElementsByTagName('staffLayout'):
        if staffLayout.getAttribute('description') == staff.getAttribute('layout'):
            instr = getElement(staffLayout,'instrument')
            abbrev=instr.getAttribute('abbrev')
            break;
      
      filename=getUserDataDir()+'cuenotes.tmp'
      f = file(filename, 'wt')
      f.write('<noteObjects>\n')
      no=0
      first=1
      for node in notes.childNodes:
          if node.nodeType == notes.ELEMENT_NODE:
              if no>=ob1:
                  if node.tagName=='chord' or node.tagName=='rest':
                        disp=getElement(node,'display')
                        if not disp:
                            disp=self.doc.createElement('display')
                            node.appendChild(disp)
                        disp.setAttribute('small','true')
                        if first and abbrev!='':
                          first=0  
                          drawobjects=getElement(node,'drawObjects')
                          if not drawobjects:
                            drawobjects=self.doc.createElement('drawObjects')
                            node.appendChild(drawobjects)
                          drawobj = self.doc.createElement('drawObj')
                          drawobjects.appendChild(drawobj) 
                          txt = self.doc.createElement('text')
                          drawobj.appendChild(txt)
                          txt.setAttribute('x','-1')
                          txt.setAttribute('y','-2.5')
                          cnt = self.doc.createElement('content')
                          txt.appendChild(cnt)
                          cnt.appendChild(self.doc.createTextNode(abbrev))
                          font = self.doc.createElement('font')
                          txt.appendChild(font)
                          font.setAttribute('face','Times New Roman')
                          font.setAttribute('height','9')
                          font.setAttribute('weight','0')
                          font.setAttribute('pitchAndFamily','0')
                          
                  
                  for text in node.getElementsByTagName('content'):
                   if text.firstChild:                     
                      str=text.firstChild.nodeValue
                      str=str.encode('Latin-1')        # unicode -> string
                      #messageBox("test",str)
                      str=str.encode('string_escape')  # string codieren mit Zeichen < 128
                      #messageBox("test",str)
                      text.firstChild.nodeValue=str
                      
                  node.writexml(f)
                  f.write('\n')
              no+=1
              if no>=ob2:
                  break
      f.write('</noteObjects>\n')
      f.close()
         

      

if activeScore():
    sel = curSelection()
    if sel <> 0:
        global sy1,st1,vo1,ob1,ob2
        (sy1,st1,vo1,ob1),(sy2,st2,vo2,ob2) = sel
        sel = (min(sy1,sy2), min(st1,st2),min(vo1,vo2),min(ob1,ob2)),(max(sy1,sy2), max(st1,st2),max(vo1,vo2),max(ob1,ob2))
        (sy1,st1,vo1,ob1),(sy2,st2,vo2,ob2) = sel                
        if sy1!=sy2 or st1!=st2 or vo1!=vo2:
            messageBox('Stichnoten kopieren','Fehler: Nur einstimmige Markierung zulässig')
        elif ob1==ob2:
            messageBox('Stichnoten kopieren','Fehler: Keine Noten markiert')
        else:
            tempInput = tempfile.mktemp('.capx')
            tempOutput = tempfile.mktemp('.capx')
            activeScore().write(tempInput)
            ScoreChange(tempInput, tempOutput)
            os.remove(tempInput)
            os.remove(tempOutput)


