clipboard.py :  » GUI » Python-Win32-GUI-Automation » pywinauto-0.4.0 » pywinauto » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » GUI » Python Win32 GUI Automation 
Python Win32 GUI Automation » pywinauto 0.4.0 » pywinauto » clipboard.py
# GUI Application automation and testing library
# Copyright (C) 2006 Mark Mc Mahon
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation; either version 2.1
# of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
#    Free Software Foundation, Inc.,
#    59 Temple Place,
#    Suite 330,
#    Boston, MA 02111-1307 USA

"Some clipboard wrapping functions - more to be added later"

__revision__ = "$Revision: 726 $"

import ctypes

import win32functions
import win32defines

#from ctypes.wintypes import *


#====================================================================
def _get_standard_formats():
    "Get the known formats by looking in win32defines"
    formats = {}
    for define_name in win32defines.__dict__.keys():
        if define_name.startswith("CF_"):
            formats[getattr(win32defines, define_name)] = define_name
    return formats

# get all the formats names keyed on the value
_standard_formats = _get_standard_formats()


#====================================================================
def GetClipboardFormats():
    "Get a list of the formats currently in the clipboard"
    if not win32functions.OpenClipboard(0):
        raise WinError()

    available_formats = []
    format = 0
    while True:
        # retrieve the next format
        format = win32functions.EnumClipboardFormats(format)

        # stop enumerating because all formats have been
        # retrieved
        if not format:
            break

        available_formats.append(format)

    win32functions.CloseClipboard()

    return available_formats


#====================================================================
def GetFormatName(format):
    "Get the string name for a format value"

    # standard formats should not be passed to GetClipboardFormatName    
    if format in _standard_formats:
        return _standard_formats[format]

    if not win32functions.OpenClipboard(0):
        raise WinError()    

    max_size = 500
    buffer_ = ctypes.create_unicode_buffer(max_size+1)

    ret = win32functions.GetClipboardFormatName(
        format, ctypes.byref(buffer_), max_size)
    
    if not ret:
        raise RuntimeError("test")
    
    win32functions.CloseClipboard()

    return buffer_.value


#====================================================================
def GetData(format = win32defines.CF_UNICODETEXT):
    "Return the data from the clipboard in the requested format"
    if format not in GetClipboardFormats():
        raise RuntimeError("That format is not available")
    
    if not win32functions.OpenClipboard(0):
        raise WinError()

    handle = win32functions.GetClipboardData(format)
    
    if not handle:
        error = ctypes.WinError()
        win32functions.CloseClipboard()
        raise error
    
    buffer_ = ctypes.c_wchar_p(win32functions.GlobalLock(handle))
    
    data = buffer_.value
    
    win32functions.GlobalUnlock(handle)

    win32functions.CloseClipboard()

    return data


#====================================================================
def EmptyClipboard():
    if not win32functions.OpenClipboard(0):
        raise RuntimeError("Couldn't open clipboard")
    win32functions.EmptyClipboard()
    win32functions.CloseClipboard()


#====================================================================
# Todo: Implement setting clipboard data
#def SetData(data, formats = [win32defines.CF_UNICODETEXT, ]):
#    pass


#====================================================================
if __name__ == "__main__":
    _unittests()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.