#!/usr/bin/env python

import wx

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 hidden, 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 application\tCtrl-X") # Close window
        
        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_)
                  
        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()),
            ])
        self.SetAcceleratorTable(acceltbl)

        # Setting popup menu

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

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

    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")

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

    def on_close(self, evt):
        self.Close()

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

        
if __name__ == "__main__":

    app = wx.App(redirect=False)
    copy_and_paste = CopyAndPaste()
    app.MainLoop()

