# -*- coding: ISO-8859-1 -*-
""" capellaScript -- 20.01.2005 Andreas Herzog
>>> AlteMeister

    Mit diesem Skript kann man Textfelder mit Generalbassziffern einfügen.|
    |
    
    |
    Version 1.3    |
    
    |
    Version 1.1: Fontgröße kann nun verändert werden. |
    Version 1.2: Gesamte Partitur kann nur einfacher verändert werden.|
    Version 1.3: Position über/unter der Notenzeile kann von der Gesamtveränderung ausgenommen werden. |

        |

<<<


"""

import xml.dom
import string


from xml.dom.minidom import NodeList


ZahlenDarstellung = [' ','2','3','4','5','6','7','8','9','10','6/','_']
ZahlenCode = [' ','2','3','4','5','6','7','8','9','?','&','z']
AlterationenDarstellung = ['','#','x','b','bb','Auflz.','-','_']
AlterationenCode = [' ','+','x','b','B','#','-','z']
FontGroessen = ['5','6','7','8','9','10','11','12','13','14','15','16']
LineStaerken = ['1fach', '2fach', '3fach','4fach','5fach']

# doc = [] # parentNode von score

def latin1_e(u):
    return u.encode('Latin-1')
def latin1_d(u):
    return u.decode('Latin-1')
    
class settings:
    def __init__(self):
        self.TextPosX = 0.5   
        self.TextPosY = 7.0
        self.SpaltenAbstand = 2.0
        self.ZeilenAbstand = 1.0
        self.AltAbstand = 0.7
        self.PositionUeberUnter = 1
        self.FontGroesse = 4
        self.LineWidth = 1
        self.StrichAbstand = 0.3
        self.NichtUnterUeber = 1
        self.Gruppierung = 0
  
     
defaults = settings()
dlgSet = settings()

options = ScriptOptions() 
opt = options.get()


def getOptions():
    global TextPosX, TextPosY, SpaltenAbstand, ZeilenAbstand, AltAbstand, PositionUeberUnter, FontGroesse, LineWidth, StrichAbstand, NichtUnterUeber, Gruppierung
    
    TextPosX	= eval(opt.get('TextPosX',str(TextPosX)))
    TextPosY	= eval(opt.get('TextPosY',str(TextPosY)))
    SpaltenAbstand  = eval(opt.get('SpaltenAbstand',str(SpaltenAbstand)))
    ZeilenAbstand   = eval(opt.get('ZeilenAbstand',str(ZeilenAbstand)))
    AltAbstand    = eval(opt.get('AltAbstand',str(AltAbstand)))
    PositionUeberUnter    = eval(opt.get('PositionUeberUnter',str(PositionUeberUnter)))
    FontGroesse = eval(opt.get('FontGroesse',str(FontGroesse)))
    LineWidth = eval(opt.get('LineWidth',str(LineWidth)))
    StrichAbstand = eval(opt.get('StrichAbstand',str(StrichAbstand)))
    NichtUnterUeber = eval(opt.get('NichtUnterUeber',str(NichtUnterUeber)))
    Gruppierung = eval(opt.get('Gruppierung', str(Gruppierung)))


def setOptions():
    global dlgSet
    opt.update(dict(TextPosX     	= str(TextPosX),
                    TextPosY 		= str(TextPosY),
                    SpaltenAbstand  = str(SpaltenAbstand),
                    ZeilenAbstand   = str(ZeilenAbstand),
                    AltAbstand	    = str(AltAbstand),
                    PositionUeberUnter    = str(PositionUeberUnter),
                    FontGroesse 	= str(FontGroesse),
                    LineWidth = str(LineWidth),
                    StrichAbstand = str(StrichAbstand),
                    NichtUnterUeber = str(NichtUnterUeber),
                    Gruppierung = str(Gruppierung)
                    
                    
                    ))
    options.set(opt)
    
def addElementNode(el,tagName):
    # add new Node to el if Node "tagName" does not exist
    # otherwise return the existing Node
    global doc
    childs = el.childNodes
    for n in range(childs.length):
        if childs[n].nodeType ==childs[n].ELEMENT_NODE and childs[n].tagName == tagName:
            return childs[n]
    newChild = doc.createElement(tagName)
    el.appendChild(newChild)
    return newChild
    

def addNewElementNode(el,tagName):
    # add new Node with tagName "tagName" to el 
    global doc
    newChild = doc.createElement(tagName)
    el.appendChild(newChild)
    return newChild

    
def getDialogValues1():
	
    global PositionUeberUnter, TextPosY, TextPosX, NurLoeschen, Intelligenz, ZeilenAbstand, NichtUnterUeber
    global AltAbstand, SpaltenAbstand, Gruppierung, FontGroesse, GesamtePartitur, LineWidth, StrichAbstand
    global Ton11, Ton12, Ton13, Ton21, Ton22, Ton23, Ton31, Ton32, Ton33
    global Alter11, Alter12, Alter13, Alter21, Alter22, Alter23, Alter31, Alter32, Alter33

    rad1 = Radio(['über der Notenzeile', 'unter der Notenzeile'], text='', padding = 8, value=PositionUeberUnter)
    rad2 = Radio(['Bruchschreibweise', 'in Worten abgekürzt', 'in Worten'], text='Schreibweise', padding = 8, value=0)
    edit1  = Edit (str(ZeilenAbstand), text = 'Zeilenabstand', width=4)
    edit2  = Edit (str(TextPosY), text = 'Abstand von der Mittelstimme', width=4)
    edit3  = Edit (str(TextPosX), text = 'Korrektur der horizontalen Lage', width=4)
    edit4  = Edit (str(AltAbstand), text = 'horizontaler Abstand der Alterationen', width=4)
    edit5  = Edit (str(SpaltenAbstand), text = 'Spaltenabstand', width=4)
    edit6  = Edit (str(StrichAbstand), text = 'Spaltenabstand', width=4)
    
    lab0a = Label('Fontgröße:   ', width = 4)
    lab0b = Label('Strichstärke:', width = 4)
    lab1 = Label('Zeilenabstand', width = 4)
    lab2 = Label('Abstand von der Mittelstimme', width = 4)
    lab3 = Label('Korrektur der horizontalen Lage', width = 4)
    lab4 = Label('horizontaler Abstand der Alterationen', width = 4)
    lab5 = Label('Spaltenabstand', width = 4)
    lab6 = Label('Korrektur der vertikalen Lage des Unterstrichs', width = 4)
    
    NurLoeschenCheck = CheckBox('Nur Elemente dieses Skripts löschen!', value=0)
    GruppierungCheck = CheckBox('Elemente gruppieren', value=Gruppierung)
    GesamtePartiturCheck = CheckBox('Formatierung für die gesamte Partitur übernehmen', value=0)
    NichtUnterUeberCheck = CheckBox('Position über/unter der Notenzeile belassen', value=NichtUnterUeber)
    FontGroesseBox = ComboBox(FontGroessen, value=FontGroesse, width=5)
    LineStaerkenBox = ComboBox(LineStaerken, value=(LineWidth-1), width=8)
    
    Ton11Box = ComboBox(ZahlenDarstellung, value=Ton11, width=5)
    Ton12Box = ComboBox(ZahlenDarstellung, value=Ton12, width=5)
    Ton13Box = ComboBox(ZahlenDarstellung, value=Ton13, width=5)
    
    Alter11Box = ComboBox(AlterationenDarstellung, value=Alter11, width=5)
    Alter12Box = ComboBox(AlterationenDarstellung, value=Alter12, width=5)
    Alter13Box = ComboBox(AlterationenDarstellung, value=Alter13, width=5)
    
    hbox11 = HBox([Ton11Box, Alter11Box], padding = 3)
    hbox12 = HBox([Ton12Box, Alter12Box], padding = 3)
    hbox13 = HBox([Ton13Box, Alter13Box], padding = 3)
    
    Ton21Box = ComboBox(ZahlenDarstellung, value=Ton21, width=5)
    Ton22Box = ComboBox(ZahlenDarstellung, value=Ton22, width=5)
    Ton23Box = ComboBox(ZahlenDarstellung, value=Ton23, width=5)
    
    Alter21Box = ComboBox(AlterationenDarstellung, value=Alter21, width=5)
    Alter22Box = ComboBox(AlterationenDarstellung, value=Alter22, width=5)
    Alter23Box = ComboBox(AlterationenDarstellung, value=Alter23, width=5)
    
    hbox21 = HBox([Ton21Box, Alter21Box], padding = 3)
    hbox22 = HBox([Ton22Box, Alter22Box], padding = 3)
    hbox23 = HBox([Ton23Box, Alter23Box], padding = 3)
    
    Ton31Box = ComboBox(ZahlenDarstellung, value=Ton31, width=5)
    Ton32Box = ComboBox(ZahlenDarstellung, value=Ton32, width=5)
    Ton33Box = ComboBox(ZahlenDarstellung, value=Ton33, width=5)
    
    Alter31Box = ComboBox(AlterationenDarstellung, value=Alter31, width=5)
    Alter32Box = ComboBox(AlterationenDarstellung, value=Alter32, width=5)
    Alter33Box = ComboBox(AlterationenDarstellung, value=Alter33, width=5)
    
    hbox31 = HBox([Ton31Box, Alter31Box], padding = 3)
    hbox32 = HBox([Ton32Box, Alter32Box], padding = 3)
    hbox33 = HBox([Ton33Box, Alter33Box], padding = 3)
    
    hbox1a = VBox([hbox11, hbox12, hbox13], padding = 16)
    hbox1b = VBox([hbox21, hbox22, hbox23], padding = 16)
    hbox1c = VBox([hbox31, hbox32, hbox33], padding = 16) 
        
    hbox2a = HBox([hbox1a], padding = 8,text = '1.Spalte')
    hbox2b = HBox([hbox1b], padding = 8,text = '2.Spalte')
    hbox2c = HBox([hbox1c], padding = 8,text = '3.Spalte') 
      
    hbox3 = HBox([hbox2a, hbox2b, hbox2c], padding = 8)	
    hbox4 = HBox([edit1, lab1], padding = 8)	
    hbox5 = HBox([edit2, lab2], padding = 8)
    hbox6 = HBox([edit3, lab3], padding = 8)
    hbox7 = HBox([edit4, lab4], padding = 8)
    hbox8 = HBox([edit5, lab5], padding = 8)
    hbox10 = HBox([edit6, lab6], padding = 8)
    hbox9a = HBox([lab0a, FontGroesseBox], padding = 8)
    hbox9b = HBox([lab0b, LineStaerkenBox], padding = 8)
    vbox9 =  VBox([hbox9a, hbox9b], padding = 8)
    vbox11 = VBox([NurLoeschenCheck, GruppierungCheck, GesamtePartiturCheck, NichtUnterUeberCheck], padding = 8)
    hbox11 = HBox([vbox11], padding = 8, text = 'Formatierungsoptionen')
    vbox12 = VBox([hbox5, hbox4, hbox6, hbox7, hbox8, hbox10,], padding = 8)
    hbox12 = HBox([vbox12], padding = 8, text = 'Abstände')
    hbox9 =  HBox([rad1, vbox9], padding = 8)
    hbox13 = HBox([hbox9], padding = 8, text = 'Position, Schriftgröße und Strichstärke')
    
    
    lab1 = Label('Zwischenräume', width = 4)
    vbox2= VBox([ hbox3,  hbox12, hbox11, hbox13], padding = 8)
    
    dlg = Dialog('Bitte wählen: ', vbox2)

	
    if dlg.run():
        PositionUeberUnter = rad1.value()
    	ZeilenAbstand = string.atof(edit1.value())
    	TextPosY = string.atof(edit2.value())
    	TextPosX = string.atof(edit3.value())
    	AltAbstand= string.atof(edit4.value())
    	SpaltenAbstand= string.atof(edit5.value())
    	StrichAbstand= string.atof(edit6.value())
    	NurLoeschen = NurLoeschenCheck.value()
    	Gruppierung = GruppierungCheck.value()
    	Intelligenz = rad2.value()
    	FontGroesse = FontGroesseBox.value()
    	GesamtePartitur = GesamtePartiturCheck.value()
    	NichtUnterUeber = NichtUnterUeberCheck.value()
    	LineWidth = LineStaerkenBox.value() + 1
    	Ton11 = Ton11Box.value()
    	Ton12 = Ton12Box.value()
    	Ton13 = Ton13Box.value()
    	Ton21 = Ton21Box.value()
    	Ton22 = Ton22Box.value()
    	Ton23 = Ton23Box.value()
    	Ton31 = Ton31Box.value()
    	Ton32 = Ton32Box.value()
    	Ton33 = Ton33Box.value()    	
    	Alter11 = Alter11Box.value()
    	Alter12 = Alter12Box.value()
    	Alter13 = Alter13Box.value()
    	Alter21 = Alter21Box.value()
    	Alter22 = Alter22Box.value()
    	Alter23 = Alter23Box.value()    
    	Alter31 = Alter31Box.value()
    	Alter32 = Alter32Box.value()
    	Alter33 = Alter33Box.value()       		
        if Ton11 == 0 and Ton12 == 0 and Ton13 == 0 and Alter11 == 0 and Alter12 == 0 and Alter13 == 0:
        	return False
        else: 
        	return True
    else:
        return False
        
def newText(drawObjects,  width, height, contents, IndexTag, width2):
	global Gruppierung, FontGroesse, LineWidth, StrichAbstand
	
	if Gruppierung == 0:
		drawObj = addNewElementNode(drawObjects,'drawObj')
	
 	if Gruppierung == 1:
 		groupDrawObj = addElementNode(drawObjects,'drawObj')
		group = addElementNode(groupDrawObj,'group')	
		drawObj = addNewElementNode(group,'drawObj')
		
	text = addElementNode(drawObj,'text')	
		
	text.setAttribute('x',width)
	text.setAttribute('y',height)

	content = addNewElementNode(text,'content')
	textNode = doc.createTextNode(contents)
	content.appendChild(textNode)
	text.setAttribute('align','left')
	GraficalTag = '56294-14'+IndexTag
	basic = addElementNode(drawObj,'basic')							
	basic.setAttribute('tag',GraficalTag)    
	if Gruppierung == 1:
		basic = addElementNode(groupDrawObj,'basic')							
		basic.setAttribute('tag','56294-1400')    
	font = addNewElementNode(text,'font')
	font.setAttribute('face','capella-Generalbass')
	font.setAttribute('color','')
	font.setAttribute('height',FontGroessen[FontGroesse])
	font.setAttribute('charSet','1')
	font.setAttribute('pitchAndFamily','2')	 
	if contents == 'z':
		if Gruppierung == 0:
			drawObj2 = addNewElementNode(drawObjects,'drawObj')
		if Gruppierung == 1:
 			groupDrawObj = addElementNode(drawObjects,'drawObj')
			group = addElementNode(groupDrawObj,'group')	
			drawObj2 = addNewElementNode(group,'drawObj')
		line = addNewElementNode(drawObj2,'line')
		line.setAttribute('x1',width)   
		line.setAttribute('x2',width2)
		line.setAttribute('y1',str(string.atof(height) + StrichAbstand))
		line.setAttribute('y2',str(string.atof(height) + StrichAbstand))
		line.setAttribute('lineWidth',str(0.05 * LineWidth))
		basic = addElementNode(drawObj2,'basic')							
		basic.setAttribute('tag',GraficalTag)
	return text

def getContent(chord):															# Holt die Werte einer eventuell bestehende Generalbass-
	global Ton11, Ton12, Ton13, Ton21, Ton22, Ton23, Ton31, Ton32, Ton33		# bezifferung zurück
	global Alter11, Alter12, Alter13, Alter21, Alter22, Alter23, Alter31, Alter32, Alter33
	global TextPosY, TextPosX, PositionUeberUnter, ZeilenAbstand, AltAbstand, SpaltenAbstand
	for drawObj in chord.getElementsByTagName('drawObj'):
		tag = '0'	
		for content in drawObj.getElementsByTagName('content'):
			ObjectContent = content.firstChild.nodeValue
		for text in drawObj.getElementsByTagName('text'):
			Textposx = round(string.atof(text.getAttribute('x')),1)
			Textposy = round(string.atof(text.getAttribute('y')),1)
			
		for basic in drawObj.getElementsByTagName('basic'):
			tag = basic.getAttribute('tag')
		if tag == '56294-1411':
			Ton11 = ZahlenCode.index(str(ObjectContent))
			if Textposy <= 0.0:
				PositionUeberUnter = 0
				TextPosY = 0.0 - Textposy
			if Textposy > 0.0:
				PositionUeberUnter = 1				
				TextPosY = Textposy

		if tag == '56294-1412':
			Ton12 = ZahlenCode.index(str(ObjectContent))
			if Textposy <= 0.0:
				ZeilenAbstand = Textposy - (TextPosY* (-1))
			if Textposy > 0.0:
				ZeilenAbstand = Textposy -TextPosY				
		if tag == '56294-1413':
			Ton13 = ZahlenCode.index(str(ObjectContent))
		if tag == '56294-1421':
			Ton21 = ZahlenCode.index(str(ObjectContent))
			if Textposx <= 0.0:
				SpaltenAbstand = Textposx - (TextPosX* (-1))
			if Textposx > 0.0:
				SpaltenAbstand = Textposx -TextPosX							
		if tag == '56294-1422':
			Ton22 = ZahlenCode.index(str(ObjectContent))
		if tag == '56294-1423':
			Ton23 = ZahlenCode.index(str(ObjectContent))						
		if tag == '56294-1431':
			Ton31 = ZahlenCode.index(str(ObjectContent))			
		if tag == '56294-1432':
			Ton32 = ZahlenCode.index(str(ObjectContent))
		if tag == '56294-1433':
			Ton33 = ZahlenCode.index(str(ObjectContent))	
						
		if tag == '56294-1414':				
			Alter11 = AlterationenCode.index(str(ObjectContent))		
			AltAbstand =  Textposx - TextPosX
		if tag == '56294-1415':				
			Alter12 = AlterationenCode.index(str(ObjectContent))
		if tag == '56294-1416':				
			Alter13 = AlterationenCode.index(str(ObjectContent))
		if tag == '56294-1424':				
			Alter21 = AlterationenCode.index(str(ObjectContent))		
		if tag == '56294-1425':				
			Alter22 = AlterationenCode.index(str(ObjectContent))
		if tag == '56294-1426':				
			Alter23 = AlterationenCode.index(str(ObjectContent))
			
		if tag == '56294-1434':				
			Alter31 = AlterationenCode.index(str(ObjectContent))		
		if tag == '56294-1435':				
			Alter32 = AlterationenCode.index(str(ObjectContent))
		if tag == '56294-1436':				
			Alter33 = AlterationenCode.index(str(ObjectContent))			
												
def handleNotes(score, voice, chord):
    global TextPosY, TextPosX, NoteHight,AltAbstand, ZeilenAbstand, AlterAbstand,  PositionUeberUnter, Intelligenz, TextPositionY
    global Ton11, Ton12, Ton13, Ton21, Ton22, Ton23 
	

	
															
    for drawObjects in chord.getElementsByTagName('drawObjects'):					# Alle drawObj mit tag == GraficalTag löschen
          for drawObj in drawObjects.childNodes:
             if drawObj.nodeType == drawObj.ELEMENT_NODE and drawObj.tagName == 'drawObj':
             	for basic in drawObj.getElementsByTagName('basic'):
                     if basic.hasAttribute('tag') and basic.getAttribute('tag') == '56294-1400':
                        drawObjects.removeChild(drawObj)
              
    for drawObjects in chord.getElementsByTagName('drawObjects'):					# Alle drawObj mit tag == GraficalTag löschen
         for drawObj in drawObjects.childNodes:
             if drawObj.nodeType == drawObj.ELEMENT_NODE and drawObj.tagName == 'drawObj':
            	for basic in drawObj.getElementsByTagName('basic'):
                     if basic.hasAttribute('tag') and basic.getAttribute('tag')[0:8] == '56294-14':
                        drawObjects.removeChild(drawObj)     	
         	  		  	               
         if drawObjects.getElementsByTagName('drawObj').length == 0:					# Wenn drawObjects keine Elemente mehr enthält, dann löschen  
             drawObjects.parentNode.removeChild(drawObjects)




    drawObjs = chord.getElementsByTagName('drawObjs')
       			
    for head in chord.getElementsByTagName('head'):
                    
            if PositionUeberUnter == 0:    									# Positionen festlegen
            	TextPositionY = 0.0-TextPosY

            if PositionUeberUnter == 1:
            	TextPositionY = TextPosY


    if NurLoeschen == 0:           
        drawObjects = addElementNode(chord,'drawObjects')				# Notennamen
        
        TextPosX1 = str(TextPosX)
        TextPosX2 = str(TextPosX + SpaltenAbstand)
        TextPosX3 = str(TextPosX + (SpaltenAbstand * 2))
        TextPosX4 = str(TextPosX + (SpaltenAbstand * 3))
        TextPosXA1 = str(TextPosX + AltAbstand)
        TextPosXA2 = str(TextPosX + SpaltenAbstand + AltAbstand)
        TextPosXA3 = str(TextPosX + (SpaltenAbstand * 2) + AltAbstand)        
        
        TextPosY1 = str(TextPositionY + ZeilenAbstand*0)
        TextPosY2 = str(TextPositionY + ZeilenAbstand*1)
        TextPosY3 = str(TextPositionY + ZeilenAbstand*2)
        
        text11 = newText(drawObjects, TextPosX1, TextPosY1, ZahlenCode[Ton11], '11',TextPosXA1)
        text12 = newText(drawObjects, TextPosX1, TextPosY2, ZahlenCode[Ton12], '12',TextPosXA1)
        text13 = newText(drawObjects, TextPosX1, TextPosY3, ZahlenCode[Ton13], '13',TextPosXA1)
        
        text21 = newText(drawObjects, TextPosX2, TextPosY1, ZahlenCode[Ton21], '21',TextPosXA2)
        text22 = newText(drawObjects, TextPosX2, TextPosY2, ZahlenCode[Ton22], '22',TextPosXA2)
        text23 = newText(drawObjects, TextPosX2, TextPosY3, ZahlenCode[Ton23], '23',TextPosXA2)

        text31 = newText(drawObjects, TextPosX3, TextPosY1, ZahlenCode[Ton31], '31',TextPosXA3)
        text32 = newText(drawObjects, TextPosX3, TextPosY2, ZahlenCode[Ton32], '32',TextPosXA3)
        text33 = newText(drawObjects, TextPosX3, TextPosY3, ZahlenCode[Ton33], '33',TextPosXA3)
        
        text14 = newText(drawObjects, TextPosXA1, TextPosY1, AlterationenCode[Alter11], '14', TextPosX2)
        text15 = newText(drawObjects, TextPosXA1, TextPosY2, AlterationenCode[Alter12], '15', TextPosX2)
        text16 = newText(drawObjects, TextPosXA1, TextPosY3, AlterationenCode[Alter13], '16', TextPosX2)

        text24 = newText(drawObjects, TextPosXA2, TextPosY1, AlterationenCode[Alter21], '24', TextPosX3)
        text25 = newText(drawObjects, TextPosXA2, TextPosY2, AlterationenCode[Alter22], '25', TextPosX3)
        text26 = newText(drawObjects, TextPosXA2, TextPosY3, AlterationenCode[Alter23], '26', TextPosX3)

        text34 = newText(drawObjects, TextPosXA3, TextPosY1, AlterationenCode[Alter31], '34', TextPosX4)
        text35 = newText(drawObjects, TextPosXA3, TextPosY2, AlterationenCode[Alter32], '35', TextPosX4)
        text36 = newText(drawObjects, TextPosXA3, TextPosY3, AlterationenCode[Alter33], '36', TextPosX4)
            
def getCursor():
    sel = curSelection()
    result = None
    if sel == 0:
        messageBox('Fehler', 'keine aktive Partitur')
        return result
    #if sel[0] != sel[1]:
    #    messageBox('Fehler', 'Markierung ist nicht leer')
    #    return result
    result = sel[0]
    return result

def getElementObjects(objList):  # returns a List
    newList = NodeList()
    for n in range(objList.length):
        if objList[n].nodeType == objList[n].ELEMENT_NODE:
            newList.append(objList[n])
    return newList

def textChange(changeText,drawObjectTag, changeTag, changeX, changeY):
	global TextGroesse, PositionUeberUnter, NichtUnterUeber
	
	if drawObjectTag == changeTag:
		changeText.setAttribute('x',changeX)
		
		
		if NichtUnterUeber == 0: 
			changeText.setAttribute('y',changeY)		
		if NichtUnterUeber == 1: 
			if (string.atof(changeText.getAttribute('y')) >= 0 and PositionUeberUnter == 1) or (string.atof(changeText.getAttribute('y')) <= 0 and PositionUeberUnter == 0):
				changeText.setAttribute('y',changeY)
	for font in changeText.getElementsByTagName('font'):
		font.setAttribute('height',FontGroessen[FontGroesse])


def lineChange(changeLine,drawObjectTag, changeTag, changeX, changeY, changeX2):	
	global LineWidth, StrichAbstand, PositionUeberUnter, NichtUnterUeber
	
	if drawObjectTag == changeTag:
		changeLine.setAttribute('x1',changeX)   
		changeLine.setAttribute('x2',changeX2)
		if NichtUnterUeber == 0: 
			changeLine.setAttribute('y1',str(string.atof(changeY) + StrichAbstand))
			changeLine.setAttribute('y2',str(string.atof(changeY) + StrichAbstand))
		if NichtUnterUeber == 1: 
			if (string.atof(changeLine.getAttribute('y1')) >= 0 and PositionUeberUnter == 1) or (string.atof(changeLine.getAttribute('y1')) <= 0 and PositionUeberUnter == 0):
				changeLine.setAttribute('y1',str(string.atof(changeY) + StrichAbstand))
				changeLine.setAttribute('y2',str(string.atof(changeY) + StrichAbstand))			
		changeLine.setAttribute('lineWidth',str(0.05 * LineWidth))

    
def changeAll(score):
	global TextPosX, SpaltenAbstand, AltAbstand,ZeilenAbstand, TextPositionY
	
	TextPosX1 = str(TextPosX)
	TextPosX2 = str(TextPosX + SpaltenAbstand)
	TextPosX3 = str(TextPosX + (SpaltenAbstand * 2))
	TextPosX4 = str(TextPosX + (SpaltenAbstand * 3))
	TextPosXA1 = str(TextPosX + AltAbstand)
	TextPosXA2 = str(TextPosX + SpaltenAbstand + AltAbstand)
	TextPosXA3 = str(TextPosX + (SpaltenAbstand * 2) + AltAbstand)        
    
	TextPosY1 = str(TextPositionY + ZeilenAbstand*0)
	TextPosY2 = str(TextPositionY + ZeilenAbstand*1)
	TextPosY3 = str(TextPositionY + ZeilenAbstand*2)
    
	for system in score.getElementsByTagName('system'):
		for drawObj in system.getElementsByTagName('drawObj'):
			for basic in drawObj.getElementsByTagName('basic'):
				drawObjectTag = basic.getAttribute('tag')
				if (drawObjectTag[0:8] == '56294-14') and (drawObjectTag <> '56294-1400'):
					changeObject = basic.parentNode	
					for text in changeObject.getElementsByTagName('text'):
						changeText = changeObject.getElementsByTagName('text')[0]
					
						textChange(changeText,drawObjectTag, '56294-1411',TextPosX1,TextPosY1)
						textChange(changeText,drawObjectTag, '56294-1412',TextPosX1,TextPosY2)
						textChange(changeText,drawObjectTag, '56294-1413',TextPosX1,TextPosY3)
						
						textChange(changeText,drawObjectTag, '56294-1414',TextPosXA1,TextPosY1)
						textChange(changeText,drawObjectTag, '56294-1415',TextPosXA1,TextPosY2)
						textChange(changeText,drawObjectTag, '56294-1416',TextPosXA1,TextPosY3)									
				
						textChange(changeText,drawObjectTag, '56294-1421',TextPosX2,TextPosY1)
						textChange(changeText,drawObjectTag, '56294-1422',TextPosX2,TextPosY2)
						textChange(changeText,drawObjectTag, '56294-1423',TextPosX2,TextPosY3)
						
						textChange(changeText,drawObjectTag, '56294-1424',TextPosXA2,TextPosY1)
						textChange(changeText,drawObjectTag, '56294-1425',TextPosXA2,TextPosY2)
						textChange(changeText,drawObjectTag, '56294-1426',TextPosXA2,TextPosY3)	
		
						textChange(changeText,drawObjectTag, '56294-1431',TextPosX3,TextPosY1)
						textChange(changeText,drawObjectTag, '56294-1432',TextPosX3,TextPosY2)
						textChange(changeText,drawObjectTag, '56294-1433',TextPosX3,TextPosY3)
						
						textChange(changeText,drawObjectTag, '56294-1434',TextPosXA3,TextPosY1)
						textChange(changeText,drawObjectTag, '56294-1435',TextPosXA3,TextPosY2)
						textChange(changeText,drawObjectTag, '56294-1436',TextPosXA3,TextPosY3)	

					for line in changeObject.getElementsByTagName('line'):
						changeLine = changeObject.getElementsByTagName('line')[0]
					
						lineChange(changeLine,drawObjectTag, '56294-1411',TextPosX1,TextPosY1,TextPosXA1)
						lineChange(changeLine,drawObjectTag, '56294-1412',TextPosX1,TextPosY2,TextPosXA1)
						lineChange(changeLine,drawObjectTag, '56294-1413',TextPosX1,TextPosY3,TextPosXA1)
						
						lineChange(changeLine,drawObjectTag, '56294-1414',TextPosXA1,TextPosY1,TextPosX2)
						lineChange(changeLine,drawObjectTag, '56294-1415',TextPosXA1,TextPosY2,TextPosX2)
						lineChange(changeLine,drawObjectTag, '56294-1416',TextPosXA1,TextPosY3,TextPosX2)									
				
						lineChange(changeLine,drawObjectTag, '56294-1421',TextPosX2,TextPosY1,TextPosXA2)
						lineChange(changeLine,drawObjectTag, '56294-1422',TextPosX2,TextPosY2,TextPosXA2)
						lineChange(changeLine,drawObjectTag, '56294-1423',TextPosX2,TextPosY3,TextPosXA2)
						
						lineChange(changeLine,drawObjectTag, '56294-1424',TextPosXA2,TextPosY1,TextPosX3)
						lineChange(changeLine,drawObjectTag, '56294-1425',TextPosXA2,TextPosY2,TextPosX3)
						lineChange(changeLine,drawObjectTag, '56294-1426',TextPosXA2,TextPosY3,TextPosX3)	
		
						lineChange(changeLine,drawObjectTag, '56294-1431',TextPosX3,TextPosY1,TextPosXA3)
						lineChange(changeLine,drawObjectTag, '56294-1432',TextPosX3,TextPosY2,TextPosXA3)
						lineChange(changeLine,drawObjectTag, '56294-1433',TextPosX3,TextPosY3,TextPosXA3)
						
						lineChange(changeLine,drawObjectTag, '56294-1434',TextPosXA3,TextPosY1,TextPosX4)
						lineChange(changeLine,drawObjectTag, '56294-1435',TextPosXA3,TextPosY2,TextPosX4)
						lineChange(changeLine,drawObjectTag, '56294-1436',TextPosXA3,TextPosY3,TextPosX4)										
				#messageBox('Inhalt', drawObjectTag[8])

def changeDoc(score):
	global Ton11, Ton12, Ton13, Ton21, Ton22, Ton23, Ton31, Ton32, Ton33
	global Alter11, Alter12, Alter13, Alter21, Alter22, Alter23, Alter31, Alter32, Alter33
	global ZeilenAbstand, TextPosY, TextPosX, PositionUeberUnter, AltAbstand, SpaltenAbstand
	global FontGroesse, GesamtePartitur, LineWidth, StrichAbstand, NichtUnterUeber, Gruppierung
	
	LineWidth = 1
	FontGroesse = 4
	Ton11 = 0
	Ton12 = 0
	Ton13 = 0
	Ton21 = 0
	Ton22 = 0
	Ton23 = 0
	Ton31 = 0
	Ton32 = 0
	Ton33 = 0	
	Alter11 = 0
	Alter12 = 0
	Alter13 = 0
	Alter21 = 0
	Alter22 = 0
	Alter23 = 0	
	Alter31 = 0
	Alter32 = 0
	Alter33 = 0	
	NichtUnterUeber = 1	
	TextPosX = 0.5
	TextPosY = 7.0
	SpaltenAbstand = 2.0
	ZeilenAbstand = 1.0
	AltAbstand = 0.7
	PositionUeberUnter = 1
	GesamtePartitur = 0
	StrichAbstand = 0.3
	Gruppierung = 0
	getOptions()
    
	sel = getCursor()
	if sel == None:
        #
		return
	else:
		system = score.getElementsByTagName('system')[sel[0]]
		staff = system.getElementsByTagName('staff')[sel[1]]
		voice = staff.getElementsByTagName('voice')[sel[2]]
		noteObject = voice.getElementsByTagName('noteObjects')[0]
		objList = getElementObjects(noteObject.childNodes)
		if objList.length <= sel[3]:
			return

        
		obj = objList[sel[3]]
		if obj.tagName == 'chord':
			getContent(obj)
			if getDialogValues1():
				handleNotes(score, voice, obj)
				setOptions()

	if GesamtePartitur == 1:
		changeAll(score)
		
# Hauptprogramm:

from caplib.capDOM import ScoreChange
import tempfile

class ScoreChange(ScoreChange):
	
	def changeScore(self, score):
	   global doc
	   doc = score.parentNode  
	   changeDoc(score)
		
        

if activeScore():
	
	activeScore().registerUndo("Notenlängen")
	tempInput = tempfile.mktemp('.capx')
	tempOutput = tempfile.mktemp('.capx')
	activeScore().write(tempInput)
	ScoreChange(tempInput, tempOutput)
	activeScore().read(tempOutput)
	os.remove(tempInput)
	os.remove(tempOutput)
