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
@ -74,7 +76,7 @@ def _getScreenSize(wnds):
pos[3] -= pos[1]
pos[2] -= pos[0]
_show_windows(hwnd, SW_MAXIMIZE)
_SCREENSIZE = list(GetWindowRect(hwnd[0])) # Screen size
_SCREENSIZE = list(GetWindowRect(hwnd[0])) # Screen size
_SCREENSIZE[3] -= _SCREENSIZE[1]
_SCREENSIZE[2] -= _SCREENSIZE[0]
_SCREENSIZE = tuple(_SCREENSIZE)
@ -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,10 +125,11 @@ def findallfigs():
figs.sort()
return figs
def _figparse(*args):
figs = []
for arg in args:
if isinstance(arg, (list, tuple, set)):
if isinstance(arg, (list, tuple, set)):
for val in arg:
figs.append(int(val))
elif isinstance(arg, int):
@ -130,12 +139,13 @@ def _figparse(*args):
break
else:
raise TypeError('Only integers arguments accepted!')
#raise TypeError('Unrecognized argument type (%s)!'%type(arg))
# raise TypeError('Unrecognized argument type (%s)!'%type(arg))
if len(figs) == 0 or figs == 'all':
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
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.
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.
'''
BringWindowToTop = win32gui.BringWindowToTop
FindWindow = win32gui.FindWindow
@ -188,48 +208,59 @@ def _show_figure(figs, cmdshow):
winTitle = format_ + ' %d' % fig
hwnd = FindWindow(None, winTitle)
if not hwnd == 0:
#ShowWindow(hwnd,cmdshow)
# ShowWindow(hwnd,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_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.
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.
'''
ShowWindow = win32gui.ShowWindow
BringWindowToTop = win32gui.BringWindowToTop
for hwnd in wnds:
if not hwnd == 0:
#ShowWindow(hwnd,cmdshow)
# ShowWindow(hwnd,cmdshow)
BringWindowToTop(hwnd)
ShowWindow(hwnd, cmdshow)
def keep(*figs):
''' Keeps figure windows of your choice and closes the rest.
@ -255,7 +286,7 @@ def keep(*figs):
'''
figs2keep = []
for fig in figs:
if isinstance(fig, (list, tuple, set)):
if isinstance(fig, (list, tuple, set)):
for val in fig:
figs2keep.append(int(val))
elif isinstance(fig, int):
@ -269,9 +300,10 @@ def keep(*figs):
# Remove figure handles in the "keep" list
figs2delete = allfigs.difference(figs2keep)
close(figs2delete)
#for fig in figs2delete:
# for fig in figs2delete:
# close(fig)
def close(*figs):
""" Close figure window(s)
@ -337,9 +369,10 @@ def restore(*figs):
figlist = _figparse(*figs)
SW_RESTORE = win32con.SW_SHOWNORMAL #SW_RESTORE
SW_RESTORE = win32con.SW_SHOWNORMAL # SW_RESTORE
_show_figure(figlist, SW_RESTORE)
def hide(*figs):
'''hide figure(s) window size
@ -372,9 +405,10 @@ def hide(*figs):
figlist = _figparse(*figs)
SW_HIDE = win32con.SW_HIDE
#SW_hide = win32con.SW_hide
# 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
@ -441,9 +476,10 @@ def maximize(*figs):
figlist = _figparse(*figs)
SW_MAXIMIZE = win32con.SW_SHOWMAXIMIZED
#SW_MAXIMIZE = win32con.SW_MAXIMIZE
# SW_MAXIMIZE = win32con.SW_MAXIMIZE
_show_figure(figlist, SW_MAXIMIZE)
def pile(*figs):
''' Pile figure windows
@ -534,17 +570,18 @@ 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)
# print('Screensz = ',screenpos)
for iy in range(numfigs):
pos = list(GetWindowRect(wnds[iy]))
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)
xpos = int(iy * 5 + 15 + screenpos[0]) # figure location (column)
# print('[x, y, w, h] = ', pos)
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)
@ -596,62 +633,63 @@ 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.
nh = int(ceil(sqrt(nfigspertile))) # Number of figures horisontally.
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
screenpos = _getScreenSize(wnds)
#scrdim = win32gui.GetWindowPlacement(h)[4]
# scrdim = win32gui.GetWindowPlacement(h)[4]
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)
# print('scrwid = %d' % scrwid)
figwid = (scrwid - (nh + 1) * hspc) / nh
#print('figwid = %d' % figwid)
fighgt = (scrhgt - (topspc + botspc) - (nv - 1) * medspc) / nv;
# print('figwid = %d' % figwid)
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 (row) * nh + col < nfigspertile:
if idx < nfigs:
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.
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:
@ -681,11 +720,12 @@ def cycle(*figs, **kwds):
>>> import wafo.fig as fig
>>> 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() #Cycle through all figures one at a time
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() #Cycle through all figures one at a time
fig.tile(pairs=2)
fig.cycle(pairs=2) #Cycle through all figures two at a time
fig.cycle(pairs=2) #Cycle through all figures two at a time
>>> fig.close()
See also
@ -702,16 +742,16 @@ 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
#n = length(figs);
#nlayers = ceil(n/nfigspercycle);
# n = length(figs);
# 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,27 +766,28 @@ 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)
# 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
if B == backspace_key: # Back space
i -= nfigspercycle
elif B == escape_key:
break
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,18 +795,19 @@ def test_docstrings():
if __name__ == '__main__':
test_docstrings()
#def _errcheck(result, func, args):
# def _errcheck(result, func, args):
# if not result:
# raise WinError()
# return args
#
#def bring_window2top():
##WINUSERAPI BOOL WINAPI
##GetWindowRect(
## HWND hWnd,
## LPRECT lpRect);
##
##Here is the wrapping with ctypes:
# def bring_window2top():
# #WINUSERAPI BOOL WINAPI
# #GetWindowRect(
# # HWND hWnd,
# # LPRECT lpRect);
# #
# #Here is the wrapping with ctypes:
# NULL = 0
# SW_HIDE = 0
# SW_NORMAL = 1
@ -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