master
Per A Brodtkorb 9 years ago
parent 918bf837a8
commit 96eec04b0e

@ -41,12 +41,14 @@ import win32con
import msvcrt
import numpy
__all__ = ['close', 'cycle', 'hide', 'keep', 'maximize', 'minimize', 'pile', 'restore', 'stack', 'tile']
__all__ = ['close', 'cycle', 'hide', 'keep', 'maximize', 'minimize', 'pile',
'restore', 'stack', 'tile']
# Figure format strings to recognize in window title
_FIG_FORMATS = ('Figure', 'TVTK Scene', 'Chaco Plot Window: Figure')
_SCREENSIZE = None
def _getScreenSize(wnds):
''' Return screen size X,Y,W,H
@ -65,7 +67,7 @@ def _getScreenSize(wnds):
'''
global _SCREENSIZE
if _SCREENSIZE == None:
if _SCREENSIZE is None:
MoveWindow = win32gui.MoveWindow
GetWindowRect = win32gui.GetWindowRect
SW_MAXIMIZE = win32con.SW_SHOWMAXIMIZED
@ -83,11 +85,15 @@ def _getScreenSize(wnds):
return list(_SCREENSIZE)
def _windowEnumerationHandler(hwnd, resultList):
'''Pass to win32gui.EnumWindows() to generate list of window handle, window text tuples.'''
'''Pass to win32gui.EnumWindows() to generate list of window handle,
window text tuples.
'''
resultList.append((hwnd, win32gui.GetWindowText(hwnd)))
def _findTopWindows(wantedTitle=None):
''' Return list of window handle and window title tuples
@ -95,10 +101,12 @@ def _findTopWindows(wantedTitle=None):
'''
topWindows = []
win32gui.EnumWindows(_windowEnumerationHandler, topWindows)
if wantedTitle == None:
if wantedTitle is None:
return topWindows
else:
return [(hwnd, windowTxt) for hwnd, windowTxt in topWindows if windowTxt.startswith(wantedTitle)]
return [(hwnd, windowTxt) for hwnd, windowTxt in topWindows
if windowTxt.startswith(wantedTitle)]
def findallfigs():
'''
@ -117,6 +125,7 @@ def findallfigs():
figs.sort()
return figs
def _figparse(*args):
figs = []
for arg in args:
@ -136,6 +145,7 @@ def _figparse(*args):
figs = findallfigs()
return figs
def _fig2wnd(figs):
''' Find figure handle from figure number
'''
@ -150,34 +160,44 @@ def _fig2wnd(figs):
wnd_handles.append(hwnd)
return wnd_handles
def _show_figure(figs, cmdshow):
''' sets the specified figure's show state.
@param figs: vector for figure numbers
@param cmdshow: one of following commands:
SW_FORCEMINIMIZE: Minimizes a window, even if the thread that owns the window is not
responding. This flag should only be used when minimizing windows
from a different thread.
SW_FORCEMINIMIZE: Minimizes a window, even if the thread that owns the
window is not responding. This flag should only be used
when minimizing windows from a different thread.
SW_HIDE: Hides the window and activates another window.
SW_MAXIMIZE: Maximizes the specified window.
SW_MINIMIZE: Minimizes the specified window and activates the next top-level window in the Z order.
SW_RESTORE: Activates and displays the window. If the window is minimized or maximized,
the system restores it to its original size and position. An application should
SW_MINIMIZE: Minimizes the specified window and activates the next
top-level window in the Z order.
SW_RESTORE: Activates and displays the window. If the window is
minimized or maximized, the system restores it to its
original size and position. An application should
specify this flag when restoring a minimized window.
SW_SHOW: Activates the window and displays it in its current size and position.
SW_SHOWDEFAULT: Sets the show state based on the SW_ value specified in the STARTUPINFO
structure passed to the CreateProcess function by the program that started the application.
SW_SHOWMAXIMIZED: Activates the window and displays it as a maximized window.
SW_SHOWMINIMIZED: Activates the window and displays it as a minimized window.
SW_SHOWMINNOACTIVE: Displays the window as a minimized window. This value is similar to
SW_SHOWMINIMIZED, except the window is not activated.
SW_SHOWNA: Displays the window in its current size and position. This value is similar to
SW_SHOW, except the window is not activated.
SW_SHOW: Activates the window and displays it in its current size
and position.
SW_SHOWDEFAULT: Sets the show state based on the SW_ value specified in the
STARTUPINFO structure passed to the CreateProcess function
by the program that started the application.
SW_SHOWMAXIMIZED: Activates the window and displays it as a maximized
window.
SW_SHOWMINIMIZED: Activates the window and displays it as a minimized
window.
SW_SHOWMINNOACTIVE: Displays the window as a minimized window. This value
is similar to SW_SHOWMINIMIZED, except the window is not
activated.
SW_SHOWNA: Displays the window in its current size and position. This
value is similar to SW_SHOW, except the window is not activated.
SW_SHOWNOACTIVATE: Displays a window in its most recent size and position.
This value is similar to SW_SHOWNORMAL, except the window is not actived.
SW_SHOWNORMAL: Activates and displays a window. If the window is minimized or maximized,
the system restores it to its original size and position. An application should
specify this flag when displaying the window for the first time.
This value is similar to SW_SHOWNORMAL, except the window
is not actived.
SW_SHOWNORMAL: Activates and displays a window. If the window is minimized
or maximized, the system restores it to its original size and
position. An application should specify this flag when
displaying the window for the first time.
'''
BringWindowToTop = win32gui.BringWindowToTop
FindWindow = win32gui.FindWindow
@ -192,34 +212,44 @@ def _show_figure(figs, cmdshow):
BringWindowToTop(hwnd)
ShowWindow(hwnd, cmdshow)
def _show_windows(wnds, cmdshow):
''' sets the specified window's show state.
@param wnds: list of window handles numbers
@param cmdshow: one of following commands:
SW_FORCEMINIMIZE: Minimizes a window, even if the thread that owns the window is not
responding. This flag should only be used when minimizing windows
from a different thread.
SW_FORCEMINIMIZE: Minimizes a window, even if the thread that owns the
window is not responding. This flag should only be used
when minimizing windows from a different thread.
SW_HIDE: Hides the window and activates another window.
SW_MAXIMIZE: Maximizes the specified window.
SW_MINIMIZE: Minimizes the specified window and activates the next top-level window in the Z order.
SW_RESTORE: Activates and displays the window. If the window is minimized or maximized,
the system restores it to its original size and position. An application should
specify this flag when restoring a minimized window.
SW_SHOW: Activates the window and displays it in its current size and position.
SW_SHOWDEFAULT: Sets the show state based on the SW_ value specified in the STARTUPINFO
structure passed to the CreateProcess function by the program that started the application.
SW_SHOWMAXIMIZED: Activates the window and displays it as a maximized window.
SW_SHOWMINIMIZED: Activates the window and displays it as a minimized window.
SW_SHOWMINNOACTIVE: Displays the window as a minimized window. This value is similar to
SW_SHOWMINIMIZED, except the window is not activated.
SW_SHOWNA: Displays the window in its current size and position. This value is similar to
SW_SHOW, except the window is not activated.
SW_MINIMIZE: Minimizes the specified window and activates the next
top-level window in the Z order.
SW_RESTORE: Activates and displays the window. If the window is
minimized or maximized, the system restores it to its original
size and position. An application should specify this flag when
restoring a minimized window.
SW_SHOW: Activates the window and displays it in its current size and
position.
SW_SHOWDEFAULT: Sets the show state based on the SW_ value specified in
the STARTUPINFO structure passed to the CreateProcess
function by the program that started the application.
SW_SHOWMAXIMIZED: Activates the window and displays it as a maximized
window.
SW_SHOWMINIMIZED: Activates the window and displays it as a minimized
window.
SW_SHOWMINNOACTIVE: Displays the window as a minimized window. This
value is similar to SW_SHOWMINIMIZED, except the window
is not activated.
SW_SHOWNA: Displays the window in its current size and position. This
value is similar to SW_SHOW, except the window is not activated.
SW_SHOWNOACTIVATE: Displays a window in its most recent size and position.
This value is similar to SW_SHOWNORMAL, except the window is not actived.
SW_SHOWNORMAL: Activates and displays a window. If the window is minimized or maximized,
the system restores it to its original size and position. An application should
specify this flag when displaying the window for the first time.
This value is similar to SW_SHOWNORMAL, except the
window is not actived.
SW_SHOWNORMAL: Activates and displays a window. If the window is minimized
or maximized, the system restores it to its original size and
position. An application should specify this flag when
displaying the window for the first time.
'''
ShowWindow = win32gui.ShowWindow
@ -230,6 +260,7 @@ def _show_windows(wnds, cmdshow):
BringWindowToTop(hwnd)
ShowWindow(hwnd, cmdshow)
def keep(*figs):
''' Keeps figure windows of your choice and closes the rest.
@ -272,6 +303,7 @@ def keep(*figs):
# for fig in figs2delete:
# close(fig)
def close(*figs):
""" Close figure window(s)
@ -340,6 +372,7 @@ def restore(*figs):
SW_RESTORE = win32con.SW_SHOWNORMAL # SW_RESTORE
_show_figure(figlist, SW_RESTORE)
def hide(*figs):
'''hide figure(s) window size
@ -375,6 +408,7 @@ def hide(*figs):
# SW_hide = win32con.SW_hide
_show_figure(figlist, SW_HIDE)
def minimize(*figs):
'''Minimize figure(s) window size
@ -409,6 +443,7 @@ def minimize(*figs):
SW_MINIMIZE = win32con.SW_SHOWMINIMIZED
_show_figure(figlist, SW_MINIMIZE)
def maximize(*figs):
'''Maximize figure(s) window size
@ -444,6 +479,7 @@ def maximize(*figs):
# SW_MAXIMIZE = win32con.SW_MAXIMIZE
_show_figure(figlist, SW_MAXIMIZE)
def pile(*figs):
''' Pile figure windows
@ -534,8 +570,8 @@ def stack(*figs):
GetWindowRect = win32gui.GetWindowRect
#
#
#% tile figures by postiion
#% Location (1,1) is at bottom left corner
# tile figures by postiion
# Location (1,1) is at bottom left corner
#
# print('Screensz = ',screenpos)
for iy in range(numfigs):
@ -543,7 +579,8 @@ def stack(*figs):
pos[3] -= pos[1]
pos[2] -= pos[0]
# print('[x, y, w, h] = ', pos)
ypos = screenpos[1] + iy * 20 #int(screenpos[3] - iy*20 -pos[3] -70) # figure location (row)
ypos = screenpos[1] + iy * 20
# int(screenpos[3] - iy*20 -pos[3] -70) # figure location (row)
xpos = int(iy * 5 + 15 + screenpos[0]) # figure location (column)
MoveWindow(wnds[iy], xpos, ypos, pos[2], pos[3], 1)
BringWindowToTop(wnds[iy])
@ -585,7 +622,7 @@ def tile(*figs, **kwds):
figlist = _figparse(*figs)
wnds = _fig2wnd(figlist)
nfigs = len(wnds); # Number of windows.
nfigs = len(wnds) # Number of windows.
if nfigs > 0:
nfigspertile = kwds.get('pairs', nfigs)
@ -597,14 +634,13 @@ def tile(*figs, **kwds):
nlayers = int(ceil(nfigs / nfigspertile))
nh = int(ceil(sqrt(nfigspertile))) # Number of figures horisontally.
nv = int(ceil(nfigspertile / nh)); # Number of figures vertically.
nv = int(ceil(nfigspertile / nh)) # Number of figures vertically.
nh = maximum(nh, 2);
nv = maximum(nv, 2);
nh = maximum(nh, 2)
nv = maximum(nv, 2)
## Get the screen size.
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Get the screen size.
# --------------------
BringWindowToTop = win32gui.BringWindowToTop
MoveWindow = win32gui.MoveWindow
@ -614,44 +650,46 @@ def tile(*figs, **kwds):
scrwid = screenpos[2] # Screen width.
scrhgt = screenpos[3] # Screen height.
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#% The elements in the vector specifying the position.
#% 1 - Window left position
#% 2 - Window top position
#% 3 - Window horizontal size
#% 4 - Window vertical size
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#
# The elements in the vector specifying the position.
# 1 - Window left position
# 2 - Window top position
# 3 - Window horizontal size
# 4 - Window vertical size
# ------------------------------------------
hspc = 10 # Horisontal space.
topspc = 20; # Space above top figure.
medspc = 10; # Space between figures.
botspc = 20; # Space below bottom figure.
topspc = 20 # Space above top figure.
medspc = 10 # Space between figures.
botspc = 20 # Space below bottom figure.
# print('scrwid = %d' % scrwid)
figwid = (scrwid - (nh + 1) * hspc) / nh
# print('figwid = %d' % figwid)
fighgt = (scrhgt - (topspc + botspc) - (nv - 1) * medspc) / nv;
fighgt = (scrhgt - (topspc + botspc) - (nv - 1) * medspc) / nv
figwid = int(numpy.round(figwid))
fighgt = int(numpy.round(fighgt))
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#% Put the figures where they belong.
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#
# Put the figures where they belong.
# -----------------------------------
idx = 0
for unused_ix in range(nlayers):
for row in range(nv):
for col in range(nh):
if (row) * nh + col < nfigspertile:
if idx < nfigs:
figlft = int(screenpos[0] + (col + 1) * hspc + col * figwid)
figtop = int(screenpos[1] + topspc + row * (fighgt + medspc))
figlft = int(screenpos[0] + (col + 1) * hspc +
col * figwid)
figtop = int(screenpos[1] + topspc +
row * (fighgt + medspc))
# figpos = [ figlft figtop figwid fighgt ]; # Figure position.
# fighnd = FindWindow(0,'Figure %d' % figs[idx]) # Figure handle.
fighnd = wnds[idx]
MoveWindow(fighnd, figlft, figtop, figwid, fighgt, 1); # Set position.
MoveWindow(fighnd, figlft, figtop, figwid, fighgt,
1) # Set position.
BringWindowToTop(fighnd)
#figure(figs[idx]); # Raise figure.
# figure(figs[idx]) # Raise figure.
idx += 1
@ -673,7 +711,8 @@ def cycle(*figs, **kwds):
figure(s) and press any other key to display next figure(s)
When done, the figures are sorted in ascending order.
CYCLE(maximize=True) does the same thing, except that figures are maximized.
CYCLE(maximize=True) does the same thing, except that figures are
maximized.
CYCLE(pairs=2) cycle through all figures in pairs of 2.
Examples:
@ -682,7 +721,8 @@ def cycle(*figs, **kwds):
>>> for ix in range(4): f = p.figure(ix)
fig.cycle(range(3)) #Cycle trough figure 0 to 2
fig.cycle(range(3) maximize=True) #Cycle trough figure 1 to 3 with figs maximized
fig.cycle(range(3) maximize=True) #Cycle trough figure 1 to 3 with figs
# maximized
fig.cycle() #Cycle through all figures one at a time
fig.tile(pairs=2)
fig.cycle(pairs=2) #Cycle through all figures two at a time
@ -702,7 +742,7 @@ def cycle(*figs, **kwds):
maximize = kwds.get('maximize', False)
pairs = kwds.get('pairs', 1)
if maximize or pairs == None:
if maximize or pairs is None:
nfigspercycle = 1
else:
nfigspercycle = pairs
@ -711,7 +751,7 @@ def cycle(*figs, **kwds):
# nlayers = ceil(n/nfigspercycle);
# Bring one figure up at a time.
i = 0;
i = 0
escape_key = chr(27)
backspace_key = chr(8)
while 0 <= i and i < numfigs:
@ -726,16 +766,16 @@ def cycle(*figs, **kwds):
_show_windows(wnd, cmdshow)
if i + nfigspercycle - 1 < numfigs:
print('Press escape to quit, backspace to display previous figure(s) and any other key to display next figure(s)');
print('Press escape to quit, backspace to display previous ' +
'figure(s) and any other key to display next figure(s)')
# time.sleep(0.5)
B = msvcrt.getch()
if maximize: #, %restore window position
if maximize: # restore window position
_show_windows(wnd, win32con.SW_RESTORE)
if B == backspace_key: # Back space
i -= nfigspercycle
elif B == escape_key:
@ -743,10 +783,11 @@ def cycle(*figs, **kwds):
else:
i += nfigspercycle
#% Sort the figures.
# Sort the figures.
wnds.reverse()
_show_windows(wnds, win32con.SW_SHOWNORMAL)
def test_docstrings():
import doctest
doctest.testmod()
@ -754,6 +795,7 @@ def test_docstrings():
if __name__ == '__main__':
test_docstrings()
# def _errcheck(result, func, args):
# if not result:
# raise WinError()
@ -802,7 +844,8 @@ if __name__ == '__main__':
# prototype2 = WINFUNCTYPE(BOOL,HWND)
# paramflags = (1, "hwnd"),
# #Not Ok.
# BringWindowToTop = prototype2(("BringWindowToTop", windll.user32), paramflags)
# BringWindowToTop = prototype2(("BringWindowToTop", windll.user32),
# paramflags)
# # Ok
# CloseWindow = prototype2(("CloseWindow", windll.user32), paramflags)
# #Not ok

Loading…
Cancel
Save