pyWinAuto: c:\.projects\py_pywinauto\pywinauto\clipboard.py

0001# GUI Application automation and testing library
0002# Copyright (C) 2006 Mark Mc Mahon
0003#
0004# This library is free software; you can redistribute it and/or
0005# modify it under the terms of the GNU Lesser General Public License
0006# as published by the Free Software Foundation; either version 2.1
0007# of the License, or (at your option) any later version.
0008#
0009# This library is distributed in the hope that it will be useful,
0010# but WITHOUT ANY WARRANTY; without even the implied warranty of
0011# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
0012# See the GNU Lesser General Public License for more details.
0013#
0014# You should have received a copy of the GNU Lesser General Public
0015# License along with this library; if not, write to the
0016#    Free Software Foundation, Inc.,
0017#    59 Temple Place,
0018#    Suite 330,
0019#    Boston, MA 02111-1307 USA
0020
0021"Some clipboard wrapping functions - more to be added later"
0022
0023__revision__ = "$Revision: 606 $"
0024
0025import ctypes
0026
0027import win32functions
0028import win32defines
0029
0030#from ctypes.wintypes import *
0031
0032
0033#====================================================================
0034def _get_standard_formats():
0035    "Get the known formats by looking in win32defines"
0036    formats = {}
0037    for define_name in win32defines.__dict__.keys():
0038        if define_name.startswith("CF_"):
0039            formats[getattr(win32defines, define_name)] = define_name
0040    return formats
0041
0042# get all the formats names keyed on the value
0043_standard_formats = _get_standard_formats()
0044
0045
0046#====================================================================
0047def GetClipboardFormats():
0048    "Get a list of the formats currently in the clipboard"
0049    if not win32functions.OpenClipboard(0):
0050        raise WinError()
0051
0052    available_formats = []
0053    format = 0
0054    while True:
0055        # retrieve the next format
0056        format = win32functions.EnumClipboardFormats(format)
0057
0058        # stop enumerating because all formats have been
0059        # retrieved
0060        if not format:
0061            break
0062
0063        available_formats.append(format)
0064
0065    win32functions.CloseClipboard()
0066
0067    return available_formats
0068
0069
0070#====================================================================
0071def GetFormatName(format):
0072    "Get the string name for a format value"
0073
0074    # standard formats should not be passed to GetClipboardFormatName    
0075    if format in _standard_formats:
0076        return _standard_formats[format]
0077
0078    if not win32functions.OpenClipboard(0):
0079        raise WinError()
0080
0081    max_size = 500
0082    buffer_ = ctypes.create_unicode_buffer(max_size+1)
0083
0084    ret = win32functions.GetClipboardFormatName(
0085        format, ctypes.byref(buffer_), max_size)
0086
0087    if not ret:
0088        raise RuntimeError("test")
0089
0090    win32functions.CloseClipboard()
0091
0092    return buffer_.value
0093
0094
0095#====================================================================
0096def GetData(format = win32defines.CF_UNICODETEXT):
0097    "Return the data from the clipboard in the requested format"
0098    if format not in GetClipboardFormats():
0099        raise RuntimeError("That format is not available")
0100
0101    if not win32functions.OpenClipboard(0):
0102        raise WinError()
0103
0104    handle = win32functions.GetClipboardData(format)
0105
0106    if not handle:
0107        error = ctypes.WinError()
0108        win32functions.CloseClipboard()
0109        raise error
0110
0111    buffer_ = ctypes.c_wchar_p(win32functions.GlobalLock(handle))
0112
0113    data = buffer_.value
0114
0115    win32functions.GlobalUnlock(handle)
0116
0117    win32functions.CloseClipboard()
0118
0119    return data
0120
0121#====================================================================
0122# Todo: Implement setting clipboard data
0123#def SetData(data, formats = [win32defines.CF_UNICODETEXT, ]):
0124#    pass
0125
0126
0127#====================================================================
0128if __name__ == "__main__":
0129    _unittests()