# -*- coding: ISO-8859-1 -*-

from Tkinter import *
import string

wholeNotes   = 'yz&%[^]m'  # Zeichen für c, d, e, f, g, a, h, Pause
halfNotes    = 'nopqrstu'
quarterNotes = '456789wv'
eighthNotes  = 'defghijx'

Notes = [wholeNotes, halfNotes, quarterNotes, eighthNotes, wholeNotes, halfNotes, quarterNotes, eighthNotes ]
def escHandler(event):
    root.quit()
    # print str(event)
    # sys.exit(0)

if activeScore():
    (sy1, st1, vo1, ob1),(sy2, st2, vo2, ob2) = curSelection()
    voice = activeScore().system(sy1).staff(st1).voice(vo1)
    # Cursor muss vor einem Objekt stehen
    if ob1 < voice.nNoteObjs():
        # Takt bestimmen
        objCount = 0
        barCount = 0
        noteCount = 0
        dur = Rational(0)
        impBL = False
        while objCount < ob1:
            if impBL:
                if dur > Rational(0):
                    barCount += 1
                dur = Rational(0)
                noteCount = 0
                impBL = False
                
            noteObj=voice.noteObj(objCount)
            if noteObj.subType() in [noteObj.KEY,noteObj.METER,noteObj.EXPL_BARLINE]:
                if dur > Rational(0):
                    barCount += 1
                dur = Rational(0)
                noteCount = 0
            if noteObj.implBarline():
                impBL = True
            if noteObj.isChord() or noteObj.isRest():
                dur += noteObj.duration()
                noteCount += 1
            objCount += 1

        if impBL:
            noteCount = 0
            barCount += 1
                
        note= voice.noteObj(ob1)
        if note.isChord() or note.isRest():
            dur = note.duration()
            count = 0
            while 0 < dur < 1:
                count += 1
                dur = dur * 2
            if Rational('7/5') < dur < Rational('8/5'):     dots = 1
            elif Rational('8/5') < dur < Rational('9/5'):   dots = 2
            else:                   dots = 0
            dot = '.' * dots

        pitches = ''
        isNote = False
        if note.isChord():
            for i in range(note.nHeads()):
                pitch, alter = note.diatonicPitch(i)
                chord = Notes[count][pitch % 7]
                okt = ('', '""', '"', '>', '_', '!', '$', '<', "'")[pitch // 7]
                alt = ['22','2', '', '3', '33'][alter + 2]
                pitches += alt + okt + chord + dot + ' '
            isNote = True
        elif note.isRest():
            pitches = Notes[count][7] + dot
            isNote = True
        elif note.subType() == note.EXPL_BARLINE:
            pitches = '- Taktstrich -'
        elif note.subType() == note.METER:
            pitches = '- Takt -'
        elif note.subType() == note.KEY:
            pitches = '- Tonart -'
        elif note.subType() == note.CLEF:
            pitches = '- Schlüssel -'
        
            
        pitches.strip()
        
        root = Tk()
        root
        if isNote:
            l = Label(root, text="System %d\n Zeile %d\n Stimme %d\nTakt %d\n Note %d\n %s" % (sy1 + 1, st1 + 1, vo1 + 1, barCount + 1,noteCount+1, str(pitches)), font= "Arial 24 bold")
        else:
            l = Label(root, text="System %d\n Zeile %d\n Stimme %d\nTakt %d\n %s" % (sy1 + 1, st1 + 1, vo1 + 1, barCount + 1, str(pitches)), font= "Arial 24 bold")
            
        l.pack()

        # b = Button(root, text="Ok")
        # b.pack()

        root.title('Cursor Location')  
        root.bind("<Key>", escHandler)
        root.bind("<Enter>", escHandler)
        root.bind("<FocusOut>", escHandler)
        root.bind("<1>", escHandler)
        root.bind("<2>", escHandler)
        root.bind("<3>", escHandler)
        root.focus_force()
        root.update()

        root.mainloop()
        try:
            root.destroy()
        except:
            pass
        
