2017-09-10 18:19:47 -03:00
|
|
|
"Zoom a window to maximum height."
|
2000-08-14 22:13:23 -03:00
|
|
|
|
|
|
|
import re
|
|
|
|
import sys
|
2019-06-17 16:41:00 -03:00
|
|
|
import tkinter
|
Merged revisions 56443-56466 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/p3yk
................
r56454 | kurt.kaiser | 2007-07-18 22:26:14 -0700 (Wed, 18 Jul 2007) | 2 lines
Make relative imports explicit for py3k
................
r56455 | kurt.kaiser | 2007-07-18 23:12:15 -0700 (Wed, 18 Jul 2007) | 2 lines
Was modifying dict during iteration.
................
r56457 | guido.van.rossum | 2007-07-19 07:33:19 -0700 (Thu, 19 Jul 2007) | 2 lines
Fix failing test.
................
r56466 | guido.van.rossum | 2007-07-19 20:58:16 -0700 (Thu, 19 Jul 2007) | 35 lines
Merged revisions 56413-56465 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r56439 | georg.brandl | 2007-07-17 23:37:55 -0700 (Tue, 17 Jul 2007) | 2 lines
Use "Unix" as platform name, not "UNIX".
........
r56441 | guido.van.rossum | 2007-07-18 10:19:14 -0700 (Wed, 18 Jul 2007) | 3 lines
SF patch# 1755885 by Kurt Kaiser: show location of Unicode escape errors.
(Slightly tweaked for style and refcounts.)
........
r56444 | kurt.kaiser | 2007-07-18 12:58:42 -0700 (Wed, 18 Jul 2007) | 2 lines
Fix failing unicode test caused by change to ast.c at r56441
........
r56451 | georg.brandl | 2007-07-18 15:36:53 -0700 (Wed, 18 Jul 2007) | 2 lines
Add description for wave.setcomptype() values
........
r56456 | walter.doerwald | 2007-07-19 06:04:38 -0700 (Thu, 19 Jul 2007) | 3 lines
Document that codecs.lookup() returns a CodecInfo object.
(fixes SF bug #1754453).
........
r56463 | facundo.batista | 2007-07-19 16:57:38 -0700 (Thu, 19 Jul 2007) | 6 lines
Added a select.select call in the test server loop to make sure the
socket is ready to be read from before attempting a read (this
prevents an error 10035 on some Windows platforms). [GSoC - Alan
McIntyre]
........
................
2007-07-20 01:05:57 -03:00
|
|
|
|
2019-06-17 16:41:00 -03:00
|
|
|
|
|
|
|
class WmInfoGatheringError(Exception):
|
|
|
|
pass
|
2000-08-14 22:13:23 -03:00
|
|
|
|
2016-08-31 01:50:55 -03:00
|
|
|
|
2000-08-14 22:13:23 -03:00
|
|
|
class ZoomHeight:
|
2019-06-17 16:41:00 -03:00
|
|
|
# Cached values for maximized window dimensions, one for each set
|
|
|
|
# of screen dimensions.
|
|
|
|
_max_height_and_y_coords = {}
|
2000-08-14 22:13:23 -03:00
|
|
|
|
|
|
|
def __init__(self, editwin):
|
|
|
|
self.editwin = editwin
|
2019-06-17 16:41:00 -03:00
|
|
|
self.top = self.editwin.top
|
2000-08-14 22:13:23 -03:00
|
|
|
|
2018-06-19 20:12:52 -03:00
|
|
|
def zoom_height_event(self, event=None):
|
2019-06-17 16:41:00 -03:00
|
|
|
zoomed = self.zoom_height()
|
|
|
|
|
|
|
|
if zoomed is None:
|
|
|
|
self.top.bell()
|
|
|
|
else:
|
|
|
|
menu_status = 'Restore' if zoomed else 'Zoom'
|
|
|
|
self.editwin.update_menu_label(menu='options', index='* Height',
|
|
|
|
label=f'{menu_status} Height')
|
|
|
|
|
2017-06-27 01:02:32 -03:00
|
|
|
return "break"
|
2000-08-14 22:13:23 -03:00
|
|
|
|
2019-06-17 16:41:00 -03:00
|
|
|
def zoom_height(self):
|
|
|
|
top = self.top
|
|
|
|
|
|
|
|
width, height, x, y = get_window_geometry(top)
|
|
|
|
|
|
|
|
if top.wm_state() != 'normal':
|
|
|
|
# Can't zoom/restore window height for windows not in the 'normal'
|
|
|
|
# state, e.g. maximized and full-screen windows.
|
|
|
|
return None
|
|
|
|
|
|
|
|
try:
|
|
|
|
maxheight, maxy = self.get_max_height_and_y_coord()
|
|
|
|
except WmInfoGatheringError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if height != maxheight:
|
|
|
|
# Maximize the window's height.
|
|
|
|
set_window_geometry(top, (width, maxheight, x, maxy))
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
# Restore the window's height.
|
|
|
|
#
|
|
|
|
# .wm_geometry('') makes the window revert to the size requested
|
|
|
|
# by the widgets it contains.
|
|
|
|
top.wm_geometry('')
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_max_height_and_y_coord(self):
|
|
|
|
top = self.top
|
|
|
|
|
|
|
|
screen_dimensions = (top.winfo_screenwidth(),
|
|
|
|
top.winfo_screenheight())
|
|
|
|
if screen_dimensions not in self._max_height_and_y_coords:
|
|
|
|
orig_state = top.wm_state()
|
2016-08-31 01:50:55 -03:00
|
|
|
|
2019-06-17 16:41:00 -03:00
|
|
|
# Get window geometry info for maximized windows.
|
|
|
|
try:
|
|
|
|
top.wm_state('zoomed')
|
|
|
|
except tkinter.TclError:
|
|
|
|
# The 'zoomed' state is not supported by some esoteric WMs,
|
|
|
|
# such as Xvfb.
|
|
|
|
raise WmInfoGatheringError(
|
|
|
|
'Failed getting geometry of maximized windows, because ' +
|
|
|
|
'the "zoomed" window state is unavailable.')
|
|
|
|
top.update()
|
|
|
|
maxwidth, maxheight, maxx, maxy = get_window_geometry(top)
|
|
|
|
if sys.platform == 'win32':
|
|
|
|
# On Windows, the returned Y coordinate is the one before
|
|
|
|
# maximizing, so we use 0 which is correct unless a user puts
|
|
|
|
# their dock on the top of the screen (very rare).
|
|
|
|
maxy = 0
|
|
|
|
maxrooty = top.winfo_rooty()
|
|
|
|
|
|
|
|
# Get the "root y" coordinate for non-maximized windows with their
|
|
|
|
# y coordinate set to that of maximized windows. This is needed
|
|
|
|
# to properly handle different title bar heights for non-maximized
|
|
|
|
# vs. maximized windows, as seen e.g. in Windows 10.
|
|
|
|
top.wm_state('normal')
|
|
|
|
top.update()
|
|
|
|
orig_geom = get_window_geometry(top)
|
|
|
|
max_y_geom = orig_geom[:3] + (maxy,)
|
|
|
|
set_window_geometry(top, max_y_geom)
|
|
|
|
top.update()
|
|
|
|
max_y_geom_rooty = top.winfo_rooty()
|
|
|
|
|
|
|
|
# Adjust the maximum window height to account for the different
|
|
|
|
# title bar heights of non-maximized vs. maximized windows.
|
|
|
|
maxheight += maxrooty - max_y_geom_rooty
|
|
|
|
|
|
|
|
self._max_height_and_y_coords[screen_dimensions] = maxheight, maxy
|
|
|
|
|
|
|
|
set_window_geometry(top, orig_geom)
|
|
|
|
top.wm_state(orig_state)
|
|
|
|
|
|
|
|
return self._max_height_and_y_coords[screen_dimensions]
|
|
|
|
|
|
|
|
|
|
|
|
def get_window_geometry(top):
|
2000-08-14 22:13:23 -03:00
|
|
|
geom = top.wm_geometry()
|
|
|
|
m = re.match(r"(\d+)x(\d+)\+(-?\d+)\+(-?\d+)", geom)
|
2019-06-17 16:41:00 -03:00
|
|
|
return tuple(map(int, m.groups()))
|
|
|
|
|
|
|
|
|
|
|
|
def set_window_geometry(top, geometry):
|
|
|
|
top.wm_geometry("{:d}x{:d}+{:d}+{:d}".format(*geometry))
|
2018-06-19 20:12:52 -03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
from unittest import main
|
|
|
|
main('idlelib.idle_test.test_zoomheight', verbosity=2, exit=False)
|
|
|
|
|
|
|
|
# Add htest?
|