#!/usr/bin/python
#-*- coding: UTF-8 -*-


#Import the modules#
import Tkinter
from Tkinter import *
import tkFileDialog
import tkMessageBox
import time
import sys
import codecs
from PIL import Image, ImageTk

import commands


#import the version number out of the file VERSION
version = open("text/version", "r")
version = version.read()

#import about text
abouttext = open("text/abouttext", "r")
abouttext = abouttext.read()

#import Developer list
devellist = open("text/developers", "r")
devellist = devellist.read()


#filenames and fileformats in sPOP Editor
filename = open("tmp/_filename", "r")
filename = filename.read()
_filetypes = [("All","*"),("Text","*.txt"),("Python","*.py")]


#Commands
def new_file(whatever=None):
    text.delete("1.0","end")
    filename = open("tmp/_filename", "w")
    filename.write("")
    filename.close()

def save_as_file(whatever=None):
    filename = tkFileDialog.asksaveasfilename(filetypes=_filetypes)
    filename1 = open("tmp/_filename", "w")
    filename1 = filename1.write(filename)
    save = codecs.open(filename, "w", "utf-8")
    save.write(text.get("1.0","end"))
    save.close()

def save_file(whatever=None):
    filename = open("tmp/_filename", "r")
    filename = filename.read()
    save = codecs.open(filename, "w", "utf-8")
    save.write(text.get("1.0","end"))
    save.close()


def open_file(whatever=None,filename=None):
    if not filename:
         _filename = tkFileDialog.askopenfilename(filetypes=_filetypes)
    else:
         _filename = filename
    if not (_filename == ""):
        try:
            f = open(_filename, "r")
            f2 = f.read()
        except:
            f = open(_filename, "w")
            f.write("\n")
            f.close()
            f = open(_filename, "r")
            f2 = f.read()
        filename1 = open("tmp/_filename", "w")
        filename1 = filename1.write(_filename)
        text.delete("1.0", "end")
        text.insert("1.0", f2)
        text.delete("end-1c","end")
        f.close()


def quit_editor(whatever=None):
    if tkMessageBox.askokcancel("Quit", "Do you really wish to quit?"):
       filename = open("tmp/_filename", "w")
       filename.write("")
       filename.close()
       root.destroy()
#Commands Copy,Paste,Select All
def copy_text(whatever=None):
    commands.copy_text(text)
 
def paste_text(whatever=None):
    commands.paste_text(text)

def cut_text(whatever=None):
    commands.cut_text(text)

def select_all_text(whatever=None):
    commands.select_all(text)


#About Window
def about_window(whatever=None):
	about_window = Tk()
	about_window.title("About sPOP note")
	about_label = Label(about_window, text=abouttext)
	about_label.pack()
	about_devel_label = Label(about_window, text=devellist)
	about_devel_label.pack()
	about_window_close = Button(about_window, text="Close", command=about_window.destroy)
	about_window_close.pack()
	about_window.mainloop()


#sPOP Editor Window
root = Tkinter.Tk()
root.title("sPOP Editor "+ version)
root.protocol("WM_DELETE_WINDOW", quit_editor)

menu = Menu(root)
root.config(menu=menu)


#File Menu
filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=new_file)
filemenu.add_command(label="Open...", command=open_file)
filemenu.add_command(label="Save", command=save_file)
filemenu.add_command(label="Save As...", command=save_as_file)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=quit_editor)

# Edit Menu
editmenu = Menu(menu)
menu.add_cascade(label="Edit", menu=editmenu)
editmenu.add_command(label="Cut", command=cut_text)
editmenu.add_command(label="Copy", command=copy_text)
editmenu.add_command(label="Paste", command=paste_text)


#Help Menu
helpmenu = Menu(menu)
menu.add_cascade(label="Help", menu=helpmenu)
helpmenu.add_command(label="About...", command=about_window)

#Icons Toolbar
iconframe = Frame(root)
iconframe.pack()

#Load Button Images
image_new_file_load = "icons/new_file.png"
image_new_file = ImageTk.PhotoImage(Image.open(image_new_file_load))

image_save_file_load = "icons/save_file.png"
image_save_file = ImageTk.PhotoImage(Image.open(image_save_file_load))

image_open_file_load = "icons/open_file.png"
image_open_file = ImageTk.PhotoImage(Image.open(image_open_file_load))

image_about_load = "icons/about.png"
image_about = ImageTk.PhotoImage(Image.open(image_about_load))

image_quit_load = "icons/quit.png"
image_quit = ImageTk.PhotoImage(Image.open(image_quit_load))


#Icons Buttons
new_file_image_button = Button(iconframe, image=image_new_file, command=new_file)
new_file_image_button.pack(side=LEFT)

open_file_image_button = Button(iconframe, image=image_open_file, command=open_file)
open_file_image_button.pack(side=LEFT)

save_file_image_button = Button(iconframe, image=image_save_file, command=save_file)
save_file_image_button.pack(side=LEFT)

about_image_button = Button(iconframe, image=image_about, command=about_window)
about_image_button.pack(side=LEFT)

quit_image_button = Button(iconframe, image=image_quit, command=quit_editor)
quit_image_button.pack(side=LEFT)


#Textfield
textframe = Frame(root)
textframe.pack(expand=1)

#Scrollbar
scrollbar = Scrollbar(textframe, relief="raised")
scrollbar.pack(side=RIGHT, fill=Y, expand=1)


#Text Field
text = Tkinter.Text(textframe, bg="white", yscrollcommand=scrollbar.set, relief="raised")
text.pack(expand=1, fill="both")

scrollbar.config(command=text.yview)

#Hotkeys
text.bind("<Control-O>",open_file)
text.bind("<Control-o>",open_file)

text.bind("<Control-S>",save_file)
text.bind("<Control-s>",save_file)

text.bind("<Control-N>",new_file)
text.bind("<Control-n>",new_file)

text.bind("<Control-X>",cut_text)
text.bind("<Control-x>",cut_text)

text.bind("<Control-C>",copy_text)
text.bind("<Control-c>",copy_text)

text.bind("<Control-V>",paste_text)
text.bind("<Control-v>",paste_text)

text.bind("<Control-A>",select_all_text)
text.bind("<Control-a>",select_all_text)


root.mainloop()



