Esempio n. 1
0
    def insertAtCursor( self, textDict ):
        """
        Inserts the given text at the cursor position without
        replacing anything.
        Returns a boolean telling whether insertion succeeded.
        """

        # LONGTERM TODO:
        # find a better way to do this? It's currently not really inserting
        # at cursor, it's replacing text selection with text selection + new
        # text.  This means that, for instance, if I do an insert when
        # the cursor is at the beginning of the selection, my new text
        # will actually appear at the end of the selection.
        #
        # If there were a programmatic way to deselect the selection without
        # changing the text or moving the cursor, we could do that, then
        # issue paste... but I don't know of a way to do this.

        # Try to do a copy:
        ClipboardBackend.prepareForClipboardToChange()
        self.simulateCopyKeystroke()
        changed = ClipboardBackend.waitForClipboardToChange( STANDARD_WAIT_TIME )

        if not changed:
            # There was no selection to copy; just paste the text:
            return self._pasteText( textDict )
        else:
            # There was an existing selection: Concatenate it with
            # the new text:
            currentText = self._getClipboardText()
            newText = _concatenate( currentText, textDict )
            return self._pasteText( newText )
Esempio n. 2
0
    def _pasteText( self, textDict ):
        """
        Puts the given textObject into the clipboard, then simulates
        a paste keystroke in the application.  If all goes well, the
        text will be inserted into the application, and this function
        returns True.  If the attempt fails, returns False.
        """

        # Create the function which will be used for callback
        def getPendingData( formatCode ):
            try:
                if formatCode == ContextUtils.CF_CLIPBOARD_VIEWER_IGNORE:
                    return "HumanizedEnsoTextSelectionContext\0"
                else:
                    return self._renderClipboardFormat( textDict, formatCode )
            except Exception:
                import traceback
                logging.error( "Traceback in getPendingData():\n%s" %
                                  traceback.format_exc() )
                raise

        # Give the above function to clipboard backend, along with the
        # list of formats in which we can support pasting
        ClipboardBackend.prepareForPasting( getPendingData,
                                            SUPPORTED_FORMATS )
        # then type the paste command key, which will cause the app to
        # draw the data out of getPendingData.
        self.simulatePasteKeystroke()

        ClipboardBackend.waitForPaste( STANDARD_WAIT_TIME )

        success = ClipboardBackend.finalizePasting()
        return success
Esempio n. 3
0
    def replaceSelection( self, textDict ):
        """
        Replace the selected text with the given text by first
        cutting and discarding the selected text, then pasting in
        the new text.
        Returns a boolean telling whether replacement succeeded.
        """
        ClipboardBackend.prepareForClipboardToChange()
        self.simulateCutKeystroke()
        ClipboardBackend.waitForClipboardToChange( STANDARD_WAIT_TIME )

        return self._pasteText( textDict )
Esempio n. 4
0
def _init():
    """
    Import and initalize ClipboardBackend and any context sub-modules
    that need initialization, if they haven't already been initialized.
    Register their shutdown functions to run at exit time as well.
    """
    global _isInitialized    
    if not _isInitialized:
        logging.info( "Now initializing ClipboardBackend." )
        import ClipboardBackend
        ClipboardBackend.init()
        atexit.register( ClipboardBackend.shutdown )
        _isInitialized = True
Esempio n. 5
0
    def getSelection( self ):
        """
        Returns a textDict ( with, potentially, "text" and "html" keys )
        containing the text that is selected in the application.
        Attempts to do so without clobbering the clipboard.
        """

        ContextUtils.clearClipboard()

        ClipboardBackend.prepareForClipboardToChange()
        self.simulateCopyKeystroke()
        ClipboardBackend.waitForClipboardToChange( STANDARD_WAIT_TIME )

        result = self._getClipboardText()
        return result
Esempio n. 6
0
    def getSelectedFiles(self):
        """
        Returns a list of the names of the files that are selected.
        Each element of the list is an absolute path.  If no files are
        selected, it returns None (not an empty list).
        """

        ClipboardBackend.prepareForClipboardToChange()
        ContextUtils.typeCommandKey("C")
        success = ClipboardBackend.waitForClipboardToChange(
            FILE_COPY_WAIT_TIME)

        if not success:
            return None

        return self.__getHdropFiles()
Esempio n. 7
0
    def getSelectedFiles( self ):
        """
        Returns a list of the names of the files that are selected.
        Each element of the list is an absolute path.  If no files are
        selected, it returns None (not an empty list).
        """

        ClipboardBackend.prepareForClipboardToChange()
        ContextUtils.typeCommandKey( "C" )
        success = ClipboardBackend.waitForClipboardToChange(
            FILE_COPY_WAIT_TIME
            )

        if not success:
            return None

        return self.__getHdropFiles()
Esempio n. 8
0
    def replaceSelection(self, textDict):
        """
        Replace the selected text with the given text by first
        cutting and discarding the selected text, then pasting in
        the new text.
        Returns a boolean telling whether replacement succeeded.
        """
        text = textDict.get('text')
        if text:
            win32clipboard.OpenClipboard()
            win32clipboard.EmptyClipboard()
            win32clipboard.SetClipboardText(text)
            win32clipboard.CloseClipboard()

            # then type the paste command key, which will cause the app to
            # draw the data out of getPendingData.
            self.simulatePasteKeystroke()

            ClipboardBackend.waitForPaste(STANDARD_WAIT_TIME)

            ClipboardBackend.finalizePasting()
            return True
        else:
            return DefaultTextSelection.replaceSelection(self, textDict)