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

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

Loading…
Cancel
Save