<commit_before># GUI Application automation and testing library
# Copyright (C) 2006 <NAME>
#
# 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

"""Functions to retrieve properties from a window handle

These are implemented in a procedural way so as to to be
useful to other modules with the least conceptual overhead
"""
from __future__ import absolute_import

__revision__ = "$Revision$"

import ctypes

from . import win32functions
from . import win32defines
from . import win32structures


#=========================================================================
def text(handle):
    "Return the text of the window"

#    length = ctypes.c_long()
#    win32functions.SendMessageTimeout(
#        handle,
#        win32defines.WM_GETTEXTLENGTH,
#        0,
#        0,
#        win32defines.SMTO_ABORTIFHUNG,
#        100,  # .1 of a second
#        ctypes.byref(length))
#    length = length.value
    
    class_name = classname(handle)
    if class_name == 'IME':
        return 'Default IME'
    if class_name == 'MSCTFIME UI':
        return 'M'
    length = win32functions.SendMessage(handle, win32defines.WM_GETTEXTLENGTH, 0, 0)
    '''
    # XXX: there are some very rare cases when WM_GETTEXTLENGTH hangs!
    c_length = win32structures.DWORD(0)
    result = win32functions.SendMessageTimeout(handle, win32defines.WM_GETTEXTLENGTH, 0, 0, win32defines.SMTO_ABORTIFHUNG, 500, ctypes.byref(c_length))
    if result == 0:
        print('WARNING! Cannot retrieve text length for handle = ' + str(handle))
        return ''
    else:
        length = c_length.value
    '''
    textval = ''
    if length:
        length += 1

        buffer_ = ctypes.create_unicode_buffer(length)

        ret = win32functions.SendMessage(
            handle, win32defines.WM_GETTEXT, length, ctypes.byref(buffer_))

        if ret:
            textval = buffer_.value

    return textval

#=========================================================================<commit_msg>Old forgotten patch in handleprops.py.<commit_after># GUI Application automation and testing library
# Copyright (C) 2006 <NAME>
#
# 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

"""Functions to retrieve properties from a window handle

These are implemented in a procedural way so as to to be
useful to other modules with the least conceptual overhead
"""
from __future__ import absolute_import

__revision__ = "$Revision$"

import ctypes

from . import win32functions
from . import win32defines
from . import win32structures


#=========================================================================
def text(handle):
    "Return the text of the window"

#    length = ctypes.c_long()
#    win32functions.SendMessageTimeout(
#        handle,
#        win32defines.WM_GETTEXTLENGTH,
#        0,
#        0,
#        win32defines.SMTO_ABORTIFHUNG,
#        100,  # .1 of a second
#        ctypes.byref(length))
#    length = length.value
    
    class_name = classname(handle)
    if class_name == 'IME':
        return 'Default IME'
    if class_name == 'MSCTFIME UI':
        return 'M'
    length = win32functions.SendMessage(handle, win32defines.WM_GETTEXTLENGTH, 0, 0)
    '''
    # XXX: there are some very rare cases when WM_GETTEXTLENGTH hangs!
    c_length = win32structures.DWORD(0)
    result = win32functions.SendMessageTimeout(handle, win32defines.WM_GETTEXTLENGTH, 0, 0, win32defines.SMTO_ABORTIFHUNG, 500, ctypes.byref(c_length))
    if result == 0:
        print('WARNING! Cannot retrieve text length for handle = ' + str(handle))
        return ''
    else:
        length = c_length.value
    '''
    textval = ''
    # In some rare cases, the length returned by WM_GETTEXTLENGTH is <0.
    # Guard against this by checking it is >0 (==0 is not of interest):
    if length > 0:
        length += 1

        buffer_ = ctypes.create_unicode_buffer(length)

        ret = win32functions.SendMessage(
            handle, win32defines.WM_GETTEXT, length, ctypes.byref(buffer_))

        if ret:
            textval = buffer_.value

    return textval

#=========================================================================
{'fraction_non_alphanumeric': 0.12636562272396212, 'fraction_numerical': 0.02403495994173343, 'mean_word_length': 3.3944, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '628840', 'n_tokens_mistral': 1622, 'n_tokens_neox': 1500, 'n_words': 571}