|
1 | 1 | # -*- coding: UTF-8 -*- |
2 | | -#A part of NonVisual Desktop Access (NVDA) |
3 | | -#Copyright (C) 2016-2018 NV Access Limited, Derek Riemer |
4 | | -#This file is covered by the GNU General Public License. |
5 | | -#See the file COPYING for more details. |
| 2 | +# A part of NonVisual Desktop Access (NVDA) |
| 3 | +# Copyright (C) 2016-2021 NV Access Limited, Derek Riemer |
| 4 | +# This file is covered by the GNU General Public License. |
| 5 | +# See the file COPYING for more details. |
6 | 6 |
|
7 | | -from ctypes.wintypes import BOOL |
8 | | -from typing import Any, Tuple, Optional |
9 | 7 | import wx |
10 | | -from comtypes import GUID |
| 8 | +from wx.lib import scrolledpanel |
11 | 9 | from wx.lib.mixins import listctrl as listmix |
12 | 10 | from .dpiScalingHelper import DpiScalingHelperMixin |
13 | 11 | from . import guiHelper |
14 | | -import oleacc |
15 | 12 | import winUser |
16 | 13 | import winsound |
| 14 | +import math |
| 15 | + |
17 | 16 | from collections.abc import Callable |
18 | 17 |
|
19 | 18 | class AutoWidthColumnListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin): |
@@ -355,3 +354,63 @@ def onSliderChar(self, evt): |
355 | 354 | evt.Skip() |
356 | 355 | return |
357 | 356 | self.SetValue(newValue) |
| 357 | + |
| 358 | + |
| 359 | +class TabbableScrolledPanel(scrolledpanel.ScrolledPanel): |
| 360 | + """ |
| 361 | + This class was created to ensure a ScrolledPanel scrolls to nested children of the panel when navigating |
| 362 | + with tabs (#12224). A PR to wxPython implementing this fix can be tracked on |
| 363 | + https://github.com/wxWidgets/Phoenix/pull/1950 |
| 364 | + """ |
| 365 | + def GetChildRectRelativeToSelf(self, child: wx.Window) -> wx.Rect: |
| 366 | + """ |
| 367 | + window.GetRect returns the size of a window, and its position relative to its parent. |
| 368 | + When calculating ScrollChildIntoView, the position relative to its parent is not relevant unless the |
| 369 | + parent is the ScrolledPanel itself. Instead, calculate the position relative to scrolledPanel |
| 370 | + """ |
| 371 | + cr = child.GetScreenRect() |
| 372 | + spr = self.GetScreenPosition() |
| 373 | + return wx.Rect(cr.x - spr.x, cr.y - spr.y, cr.width, cr.height) |
| 374 | + |
| 375 | + def ScrollChildIntoView(self, child: wx.Window) -> None: |
| 376 | + """ |
| 377 | + Uses the same logic as super().ScrollChildIntoView(self, child) |
| 378 | + except cr = self.GetChildRectRelativeToSelf(child) instead of cr = GetRect() |
| 379 | + """ |
| 380 | + sppu_x, sppu_y = self.GetScrollPixelsPerUnit() |
| 381 | + vs_x, vs_y = self.GetViewStart() |
| 382 | + cr = self.GetChildRectRelativeToSelf(child) |
| 383 | + clntsz = self.GetClientSize() |
| 384 | + new_vs_x, new_vs_y = -1, -1 |
| 385 | + |
| 386 | + # is it before the left edge? |
| 387 | + if cr.x < 0 and sppu_x > 0: |
| 388 | + new_vs_x = vs_x + (cr.x / sppu_x) |
| 389 | + |
| 390 | + # is it above the top? |
| 391 | + if cr.y < 0 and sppu_y > 0: |
| 392 | + new_vs_y = vs_y + (cr.y / sppu_y) |
| 393 | + |
| 394 | + # For the right and bottom edges, scroll enough to show the |
| 395 | + # whole control if possible, but if not just scroll such that |
| 396 | + # the top/left edges are still visible |
| 397 | + |
| 398 | + # is it past the right edge ? |
| 399 | + if cr.right > clntsz.width and sppu_x > 0: |
| 400 | + diff = math.ceil(1.0 * (cr.right - clntsz.width + 1) / sppu_x) |
| 401 | + if cr.x - diff * sppu_x > 0: |
| 402 | + new_vs_x = vs_x + diff |
| 403 | + else: |
| 404 | + new_vs_x = vs_x + (cr.x / sppu_x) |
| 405 | + |
| 406 | + # is it below the bottom ? |
| 407 | + if cr.bottom > clntsz.height and sppu_y > 0: |
| 408 | + diff = math.ceil(1.0 * (cr.bottom - clntsz.height + 1) / sppu_y) |
| 409 | + if cr.y - diff * sppu_y > 0: |
| 410 | + new_vs_y = vs_y + diff |
| 411 | + else: |
| 412 | + new_vs_y = vs_y + (cr.y / sppu_y) |
| 413 | + |
| 414 | + # if we need to adjust |
| 415 | + if new_vs_x != -1 or new_vs_y != -1: |
| 416 | + self.Scroll(new_vs_x, new_vs_y) |
0 commit comments