# -*- coding: ISO-8859-1 -*-
""" capellaScript -- 06.04.2005 Andreas Herzog
>>> Gruppenzwang

    Das Skript gruppiert die graphischen Objekte an einer Note.|
    |
    
    |
    Version 1.0   |
    
    |
    
        |

<<<


"""
# Version 1.0: Ursprungsversion


import xml.dom
import string


from xml.dom.minidom import NodeList



# doc = [] # parentNode von score

def latin1_e(u):
    return u.encode('Latin-1')
def latin1_d(u):
    return u.decode('Latin-1')
    
    
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 Test(text):
	messageBox('Test',str(text))
			
def handleDrawobjects(object):
	for drawObjects in object.getElementsByTagName('drawObjects'):
		i = 0
		for drawObj in drawObjects.getElementsByTagName('drawObj'):
			if drawObj.parentNode == drawObjects:
				i+=1
		if i > 1:
			newDrawObj = addNewElementNode(drawObjects,'drawObj')
			group = addNewElementNode(newDrawObj, 'group')
			basic = addNewElementNode(newDrawObj,'basic')
			basic.setAttribute('tag','56294-28')
			drawObjects.insertBefore(newDrawObj,drawObjects.firstChild)
			j = 0
			for drawObj in drawObjects.getElementsByTagName('drawObj'):
				if j > 0:
					if drawObj.parentNode == drawObjects:
						copyDrawObj = drawObj.cloneNode(True)
						group.appendChild(copyDrawObj)
						drawObjects.removeChild(drawObj)
				j+=1

    
            
def getCursor():
    sel = curSelection()
    result = None
    if sel == 0:
        messageBox('Fehler', 'keine aktive Partitur')
        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 changeDoc(score):

    
	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]]
		handleDrawobjects(obj)

		
# 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("graphische Objekte gruppieren")
	tempInput = tempfile.mktemp('.capx')
	tempOutput = tempfile.mktemp('.capx')
	activeScore().write(tempInput)
	ScoreChange(tempInput, tempOutput)
	activeScore().read(tempOutput)
	os.remove(tempInput)
	os.remove(tempOutput)

