# Version 0.1.0
# This file is part of EventGhost.
# Copyright (C)  2008 Pako  (lubos.ruckl@quick.cz)
# 
# EventGhost is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# EventGhost 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 General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with EventGhost; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
def GetStatusBarText(hwnd, buf_len = 128):
    """If success, return statusbar texts like list of strings.
    Otherwise return either '>>> No process ! <<<' or '>>> No parts ! <<<'.
    Mandatory argument: handle of statusbar.
    Option argument: length of text buffer."""
    
    from ctypes import create_unicode_buffer
    from eg.WinApi import GetWindowThreadProcessId
    from eg.WinApi.Dynamic import SendMessage,_kernel32
    
    PAGE_READWRITE            = 4
    PROCESS_VM_OPERATION      = 8
    PROCESS_VM_READ           = 16
    PROCESS_QUERY_INFORMATION = 1024
    SB_GETPARTS               = 1030
    SB_GETTEXTW               = 1037
    MEM_COMMIT                = 4096
    MEM_RELEASE               = 32768

    pid = GetWindowThreadProcessId(hwnd)[1]
    process = _kernel32.OpenProcess(PROCESS_VM_OPERATION|PROCESS_VM_READ|PROCESS_QUERY_INFORMATION, False, pid)
    res_val = ['>>> No process ! <<<',]
    if process <> 0:
        parts = SendMessage(hwnd, SB_GETPARTS, 0, 0)
        partList = []
        res_val = ['>>> No parts ! <<<',]
        if parts > 0:
            remBuf = _kernel32.VirtualAllocEx(process, None, buf_len, MEM_COMMIT, PAGE_READWRITE)
            locBuf = create_unicode_buffer(buf_len)
            for item in range(parts):
                SendMessage(hwnd, SB_GETTEXTW, item, remBuf)
                _kernel32.ReadProcessMemory(process, remBuf, locBuf, buf_len, None) #copy remBuf to locBuf
                partList.append(locBuf.value)
            res_val =  partList
            _kernel32.VirtualFreeEx(process, remBuf, 0, MEM_RELEASE)
    return res_val