Skip to content

Commit

Permalink
Add HiDPI support for Wx (#595)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwiggins committed Feb 19, 2021
1 parent 6994900 commit f2042a4
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 12 deletions.
17 changes: 11 additions & 6 deletions enable/wx/base_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ def __init__(self, parent, wid=-1, pos=wx.DefaultPosition,
# Create the delegate:
self.control = control = self._create_control(parent, wid, pos, size)

# Figure out the pixel scale factor
if self.high_resolution:
self.base_pixel_scale = self.control.GetContentScaleFactor()

# Set up the 'erase background' event handler:
control.Bind(wx.EVT_ERASE_BACKGROUND, self._on_erase_background)

Expand Down Expand Up @@ -263,7 +267,8 @@ def _on_close(self, event):
def _flip_y(self, y):
""" Convert from a Kiva to a wxPython y coordinate
"""
return int(self._size[1] - 1 - y)
# Handle the device pixel ratio adjustment here
return int(self._size[1] / self.base_pixel_scale - 1 - y)

def _on_erase_background(self, event):
pass
Expand Down Expand Up @@ -325,15 +330,15 @@ def _create_key_event(self, event_type, event):

if focus_owner is not None:
if event_type == "character":
key = chr(event.GetUniChar())
key = chr(event.GetUnicodeKey())
if not key:
return None
else:
key_code = event.GetKeyCode()
if key_code in KEY_MAP:
key = KEY_MAP.get(key_code)
else:
key = chr(event.GetUniChar()).lower()
key = chr(event.GetUnicodeKey()).lower()

# Use the last-seen mouse coordinates instead of GetX/GetY due
# to wx bug.
Expand Down Expand Up @@ -439,7 +444,7 @@ def _get_control_size(self):
"""
result = None
if self.control:
result = self.control.GetSize()
result = self.control.GetSize() * self.base_pixel_scale
return result

def _window_paint(self, event):
Expand All @@ -451,8 +456,8 @@ def set_pointer(self, pointer):
""" Set the current pointer (i.e. cursor) shape
"""
ptr = POINTER_MAP[pointer]
if type(ptr) is int:
POINTER_MAP[pointer] = ptr = wx.StockCursor(ptr)
if isinstance(ptr, type(wx.CURSOR_ARROW)):
POINTER_MAP[pointer] = ptr = wx.Cursor(ptr)
self.control.SetCursor(ptr)

def set_tooltip(self, tooltip):
Expand Down
5 changes: 4 additions & 1 deletion enable/wx/cairo.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
class Window(BaseWindow):
def _create_gc(self, size, pix_format="bgra32"):
"Create a Kiva graphics context of a specified size"
gc = GraphicsContext((size[0] + 1, size[1] + 1))
gc = GraphicsContext(
(size[0] + 1, size[1] + 1),
base_pixel_scale=self.base_pixel_scale,
)
gc.translate_ctm(0.5, 0.5)
return gc

Expand Down
5 changes: 4 additions & 1 deletion enable/wx/gl.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ def _create_control(self, parent, wid, pos=wx.DefaultPosition,
def _create_gc(self, size, pix_format=None):
""" Create a GraphicsContext instance.
"""
gc = GraphicsContext((size[0] + 1, size[1] + 1))
gc = GraphicsContext(
(size[0] + 1, size[1] + 1),
base_pixel_scale=self.base_pixel_scale,
)
if self._pyglet_gl_context is None:
self._pyglet_gl_context = FakePygletContext()
gc.gl_init()
Expand Down
4 changes: 3 additions & 1 deletion enable/wx/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def _wx_bitmap_from_buffer(buf, width, height):
copy[..., 1::4] = arr[..., 1::4]
copy[..., 2::4] = arr[..., 0::4]
copy[..., 3::4] = arr[..., 3::4]
return wx.BitmapFromBufferRGBA(width, height, np.ravel(copy))
return wx.Bitmap.FromBufferRGBA(width, height, np.ravel(copy))


class Window(BaseWindow):
Expand All @@ -41,6 +41,7 @@ def _create_gc(self, size, pix_format="bgra32"):
gc = GraphicsContext(
(size[0] + 1, size[1] + 1),
pix_format=pix_format,
base_pixel_scale=self.base_pixel_scale,
bottom_up=bottom_up,
)
gc.translate_ctm(0.5, 0.5)
Expand All @@ -65,6 +66,7 @@ def _window_paint(self, event):
self._gc.width(),
self._gc.height(),
)
bmp.SetSize(control.GetSize())
wdc.DrawBitmap(bmp, 0, 0)

control._dc = None
Expand Down
4 changes: 1 addition & 3 deletions enable/wx/quartz.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ class Window(BaseWindow):

def _create_gc(self, size, pix_format="bgra32"):
self.dc = wx.ClientDC(self.control)
gc = _WindowGraphicsContext(
self.dc.GetSizeTuple(), get_macport(self.dc)
)
gc = _WindowGraphicsContext(self.dc.GetSize(), get_macport(self.dc))
gc.begin()
return gc

Expand Down
2 changes: 2 additions & 0 deletions kiva/cairo.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def draw_to_wxwindow(self, window, x, y):
image = wx.EmptyImage(self.width, self.height)
image.SetDataBuffer(arr.data)
bmp = wx.BitmapFromImage(image, depth=-1)
# Scale HiDPI images to fit window
bmp.SetSize(window.GetSize())

window_dc.BeginDrawing()
window_dc.DrawBitmap(bmp, x, y)
Expand Down

0 comments on commit f2042a4

Please sign in to comment.