#!/usr/bin/env python

import wx

#from Debug import _line as line
#from Debug import p_print


class CopyAndPaste(object):
    def __init__(self):
        pass

    def set_copy_and_paste(self):
        """
        Setting clipboard copy-and-pasting (only copying from the application to the
        clipboard is supported).
        The "menu" menu is hidded, and is only there to facilitate the acceleration table.
        Both CTRL-C and CTRL-Ins are supported.
        """
        menu = wx.Menu()        
        copy_ = menu.Append(-1, "&Copy\tCtrl-Ins") # Copy with accelerator
        minimize_ = menu.Append(-1, "Minimize") # 
        close_ = menu.Append(-1, "Close window\tCtrl-W") # Close window
        exit_ = menu.Append(-1, "E&xit\tCtrl-X") # Close window
        """
        #copy2_  = menu.Append(-1, "&Copy\tCtrl-C") # Copy with accelerator
        paste_  = menu.Append(-1, "&Paste\tShift-Ins") # Paste with accelerator
        paste2_  = menu.Append(-1, "&Paste\tCtrl-V") # Paste with accelerator
        """
        
        self.Bind(wx.EVT_MENU, self.on_copy, copy_)
        self.Bind(wx.EVT_MENU, self.on_minimize, minimize_)
        self.Bind(wx.EVT_MENU, self.on_close, close_)
        self.Bind(wx.EVT_MENU, self.on_exit, exit_)
        #self.Bind(wx.EVT_MENU, self.on_paste, paste_)
                  
        menuBar = wx.MenuBar()
        self.SetMenuBar(menuBar)

        acceltbl = wx.AcceleratorTable( [
                (wx.ACCEL_CTRL, ord('C'), copy_.GetId()),
                (wx.ACCEL_CTRL, ord('W'), close_.GetId()),
                (wx.ACCEL_CTRL, ord('X'), exit_.GetId()),
                (wx.ACCEL_CTRL, ord('Q'), exit_.GetId()),
                (wx.ACCEL_CTRL, wx.WXK_INSERT, copy_.GetId()),
                (wx.ACCEL_CTRL, wx.WXK_NUMPAD_INSERT, copy_.GetId()),
                #(wx.ACCEL_CTRL, ord('V'), paste_.GetId()),
                #(wx.ACCEL_SHIFT, wx.WXK_INSERT, paste_.GetId()),
            ])
        self.SetAcceleratorTable(acceltbl)

        # Setting popup menu

        self.popupmenu = menu
        self.Bind(wx.EVT_CONTEXT_MENU, self.on_show_popup)
        self.Bind(wx.EVT_RIGHT_UP, self.on_show_popup)

    """    
    def on_paste(self, evt):
        wx.MessageBox("You selected 'paste'")
        """

    
    def on_show_popup(self, evt):
        """
        Displaying the Popup menu

        This is an abstract method - concrete subclasses must override this.
        In the line "pos = self.list_ctrl.ScreenToClient(pos)" in the example below,
        change list_ctrl with the object you want the popup to appear on.

        Sample implementation of  on_show_popup(self, evt) is:

        def on_show_popup(self, evt):
            pos = evt.GetPosition()
            pos = self.list_ctrl.ScreenToClient(pos)
            self.PopupMenu(self.popupmenu, pos)
        """
        evt.Skip()
        raise NotImplementedError

    def get_data_for_clipboard(self,format="text"):
        """
        Return data ready to be copied to the clipboard.

        This is an abstract method - concrete subclasses must override this.

        Sample implementation of get_data_for_clipboard() is:

        def get_data_for_clipboard(self,format="text"):
            first_selected = self.list_ctrl.GetFirstSelected()
            selected_item_count = self.list_ctrl.GetSelectedItemCount()
            text_for_clipboard = ""
            for i in range(first_selected,first_selected+selected_item_count):
                text_for_clipboard = "%s%s\n" % (text_for_clipboard, self.list_ctrl.GetItemText(i))
            return(text_for_clipboard)   
        """
        raise NotImplementedError

    def on_copy(self, evt):
        """
        """
        text_for_clipboard = self.get_data_for_clipboard()

        data = wx.TextDataObject()
        data.SetText(text_for_clipboard)
        if wx.TheClipboard.Open():
            wx.TheClipboard.SetData(data)
            wx.TheClipboard.Close()
        else:
            wx.MessageBox("Unable to copy to the clipboard", "Error")
        evt.Skip()

    def on_minimize(self, evt):
        self.Iconize()
        evt.Skip()

    def on_close(self, evt):
        print line()+". self.Parent:",self.Parent
        self.Close()
        evt.Skip()

    def on_exit(self, evt):
        try:
            self.Parent.Close()
        except AttributeError:
            self.Close()
        evt.Skip()

class TestFrame(wx.Frame, CopyAndPaste):
    def __init__(self):
        wx.Frame.__init__(self,None)
        CopyAndPaste.__init__(self)
        self.set_copy_and_paste()

        
if __name__ == "__main__":

    app = wx.App(redirect=False)
    tf = TestFrame()
    tf.Show()
    app.MainLoop()

