#!/usr/bin/env python
#---------------------------------
#Programming Exercise 2
#Wordpad
#---------------------------------

import wx

#Top-level frame class
class WordpadFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "Wordpad", (75,75), (400,500))

        #create windows
        self.panel = wx.Panel(self)

        self.textBox = wx.TextCtrl(self.panel, -1, "", (0,0), (400,250))

        self.funButton = wx.Button(self.panel, -1, "Fun Button", (100, 300))
        self.Bind(wx.EVT_ENTER_WINDOW, self.OnEnterButton2)
        self.funButton.Bind(wx.EVT_ENTER_WINDOW, self.OnEnterButton1)

        self.statusBar = self.CreateStatusBar()

        #set toolbar
        self.toolBar = self.CreateToolBar()
        self.newFile = self.toolBar.AddSimpleTool(wx.NewId(), wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE), "New", "Create a new file")
        self.openFile = self.toolBar.AddSimpleTool(wx.NewId(), wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN), "Open", "Open an existing file")
        self.saveFile = self.toolBar.AddSimpleTool(wx.NewId(), wx.ArtProvider.GetBitmap(wx.ART_FILE_SAVE), "Save", "Save current file")
        self.quitTool = self.toolBar.AddSimpleTool(wx.NewId(), wx.ArtProvider.GetBitmap(wx.ART_QUIT), "Quit", "Close the program")
        self.toolBar.Realize()
        self.Bind(wx.EVT_TOOL, self.OnNewFile, self.newFile)
        self.Bind(wx.EVT_TOOL, self.OnOpenFile, self.openFile)
        self.Bind(wx.EVT_TOOL, self.OnSaveFile, self.saveFile)
        self.Bind(wx.EVT_TOOL, self.OnQuit, self.quitTool)

        #set File menu
        self.menuBar = wx.MenuBar()
        self.fileMenu = wx.Menu()
        self.fileMenu.newItem = self.fileMenu.Append(wx.NewId(), "&New", "Create a new file")
        self.fileMenu.openItem = self.fileMenu.Append(wx.NewId(), "&Open", "Open an existing file")
        self.fileMenu.saveItem = self.fileMenu.Append(wx.NewId(), "&Save", "Save current file")
        self.fileMenu.AppendSeparator()
        self.fileMenu.quitItem = self.fileMenu.Append(wx.NewId(), "&Quit", "Close the program")
        self.menuBar.Append(self.fileMenu, "&File")
        self.Bind(wx.EVT_MENU, self.OnNewFile, self.fileMenu.newItem)
        self.Bind(wx.EVT_MENU, self.OnOpenFile, self.fileMenu.openItem)
        self.Bind(wx.EVT_MENU, self.OnSaveFile, self.fileMenu.saveItem)
        self.Bind(wx.EVT_MENU, self.OnQuit, self.fileMenu.quitItem)

        #set Edit menu
        self.editMenu = wx.Menu()
        self.editMenu.copyItem = self.editMenu.Append(wx.NewId(), "&Copy", "Copy selected text")
        self.editMenu.pasteItem = self.editMenu.Append(wx.NewId(), "&Paste", "Paste text from clipboard")
        self.editMenu.AppendSeparator()
        self.editMenu.optionsItem = self.editMenu.Append(wx.NewId(), "&Options", "Change editor options")
        self.menuBar.Append(self.editMenu, "&Edit")

        self.SetMenuBar(self.menuBar)

    #New File button/menu item was clicked
    #Feature not implemented
    def OnNewFile(self, event):
        self.FeatureNotImplemented("New File")

    #Open File button/menu item was clicked
    #Display prompt for file path, then
    #display path in editor
    def OnOpenFile(self, event):
        dialog = wx.TextEntryDialog(None, "Enter path of file to open:", "Open File", "")
        if dialog.ShowModal() == wx.ID_OK:
            self.textBox.SetValue(dialog.GetValue())
        dialog.Destroy()

    #Save File button/menu item was clicked
    #Feature not implemented
    def OnSaveFile(self, event):
        self.FeatureNotImplemented("Save File")

    #User clicked window w/ no implemented feature
    def FeatureNotImplemented(self, title):
        dialog = wx.MessageDialog(None, "Sorry, this feature is not yet implemented.", title, wx.OK | wx.ICON_ERROR)
        dialog.ShowModal()
        dialog.Destroy()

    #Mouse entered "Fun" button
    #This handler is bound to the button
    def OnEnterButton1(self, event):
        print "The mouse entered the button (button handler)"
        event.Skip()

    #Mouse entered "Fun" button
    #This handler is bound to the frame
    def OnEnterButton2(self, event):
        print "The mouse entered the button (frame handler)"
        event.Skip()

    #Quit button/menu item was clicked
    #Determine which window was pressed, then exit program
    def OnQuit(self, event):
        eventObj = event.GetEventObject()

        if eventObj == self.toolBar:
            print "Quit button in toolbar was clicked. Goodbye!"
        elif eventObj == self:
            print "Quit menu item was clicked. Goodbye!"

        wx.Exit()


#Application class
class WordpadApplication(wx.App):
    def __init__(self, redirect=False, filename=None):
        wx.App.__init__(self, redirect, filename)

    def OnInit(self):
        self.frame=WordpadFrame()
        self.frame.Show()
        self.SetTopWindow(self.frame)
        return True

if __name__ == "__main__":
    app=WordpadApplication()
    app.MainLoop()
