Update Cursor Demo

Add an example of all cursor options and a IcoFX-like widget test area.
This commit is contained in:
Metallicow
2016-10-04 12:40:59 -05:00
parent 0c3721e0ed
commit 6861fdf482
9 changed files with 246 additions and 51 deletions

View File

@@ -1,14 +1,76 @@
#!/usr/bin/env python
# Tags: phoenix-port, py3-port
#-Imports----------------------------------------------------------------------
#--Python Imports.
import os
import sys
#--wxPython Imports.
import wx
import images
from wx.lib.embeddedimage import PyEmbeddedImage
#----------------------------------------------------------------------
#-Globals----------------------------------------------------------------------
CUSTOMID = 1111
paperairplane_arrow_blue24 = PyEmbeddedImage(
"iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAACpUlEQVR42q3VC0hTYRgG4M6m"
"m8uRmGaVV8SQSrDMbJ6azpZ40qXLXM05y2GikFdKS4PUrEINUygVQ0FalmU3soiIhEohbEGS"
"RlQkRChUUAVBCL19/zBBNNDT+eBh8AO87LtsC6h+ExNRE45IWwo3d9DHBNETleQhfqEahMVa"
"WMhPEkaUkgYErtHCWNSOkAgBnEz+mZ4CiKtkAaFRBmxNPwJDbj2Cw7eA42Tv6NmHuEgSEKY1"
"wZjXgJiUYtiqLmNZcDjouZ94SxISrtsNS2kHNPG5EDKqkXe8G55LA1nIJeJJ5ER8rYszI6vC"
"Dk1CgVNqXhMKTnVjoYc3C6kjHkQmOiBSn46cym5oth2ckl1uR9FJO5QqNbuRbKIWHbIxPgP5"
"J65DY6iYpvT0Lew/2gq5q/L/boQXrDhQ34vo5GPT8Ck1qOvoQ1ZhDWRyF/E3EpOYiYqm+4je"
"UTuDztwA+53nSDJls/UVdyO67XtQ3dKH6J2NsxJsbbj75BUieT0b+vxvJN64F7XtA+BNrf+U"
"X30bjx2v4eamYiH3iNec11dItaHRPgje3DErU3EPHMMfoNE6v8E3kkaC5jwPQ5oNzVdegLdc"
"nGGztQuOkTHssu7DAo5jgzaS9fNqU1KqFed7hsBbr86QU/UQff0OtkXsHkpIFFlOFHNeWSEx"
"BeNfvuPZ8Bh6H42irWcElrIH2JR5Ew2dQzhUWft3uLFTrZlnwcvbBzp9AvJLDqOz6xrejI6j"
"7MwgLvS+hZBsZgEtJIJ4ijm2QnKDvCSfqNe/fANCMPDUgfcffyAoZBULsJKVzmsWUWriT1aT"
"tWQDKXdVKCfOnmuGr58/67+WrBD7yyojCuJOFk22YQmJI1/JBOHZm5T/1zKimhzq4GT/FxNJ"
"iyMK4kP8nK0UUX8Azg5aSnmghYAAAAAASUVORK5CYII=")
paperairplane_arrow_white24 = PyEmbeddedImage(
"iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAB2ElEQVR4Aa3VsatSYRiA8Twd"
"y5Pa5SoXi0RpErQuQehQIEREi+ka5KSLew4uDaKLgy26C7bkpIHgaOlgSINIEjgo0f0HqiFo"
"eXve4UxO174PfogqPpzj971eYf3EQ/jggdnl8/mEh+84xzXjgUQiIdlsViNfEYfXeKDZbEoy"
"mdTIR0RgGw30+31pt9sSjUY18h4hXDUWGAwGsl6vpV6vSzgc1shbnMAyFtjv97JYLKRarYrf"
"79fIawRhGQuo6XQqlUpFvF7vX95+BT88xgJqPB5LsVgUy7J+85En7hkxFlD6Wj6f11v1wz0j"
"RgO73U56vd7hGTEVUNvtVrrdrqRSqcMzYiKgNpuNtFqtwzNiKqBms5luX91ZGqnjJiwjgdVq"
"JY1Gw72CL3iKW7D/O6A/dKfTcefUNzzHPZwauYLlcimlUkm//A+KeIAz2JeeprpbdOiNRiPR"
"26KByWQimUxGAx/wCHeO2ariOI7E43FJp9NSKBSkVqvJfD6X4XAosVjMnUtJBI450c/wBu/w"
"CXsNlstl0fsfCAQ08AKxY/+MHERwFwncx0vm0EUul5NgMPiL548ROXayemDjOm4giBDO8RkX"
"yCAEY8sDDd5GF0mcwPiycYozOLj0+gej7JQuh90YaAAAAABJRU5ErkJggg==")
gFileDir = os.path.dirname(os.path.abspath(__file__))
ID_PAPERAIRPLANE_ARROW_BLUE = 2001
ID_PAPERAIRPLANE_ARROW_RED = 2002
ID_PAPERAIRPLANE_ARROW_GREY = 2003
ID_PAPERAIRPLANE_ARROW_DARK = 2004
ID_PAPERAIRPLANE_ARROW_BLUE_FADEOUT80 = 2005
ID_PAPERAIRPLANE_ARROW_COLORSHIFT = 2006
ID_PAPERAIRPLANE_ARROW_WHITE = 2006
ID_PAPERAIRPLANE_ARROW_WHITE_PNG = 2007
ID_PAPERAIRPLANE_ARROW_BLUE_PY = 2008
ID_PAPERAIRPLANE_ARROW_WHITE_PY = 2009
cursors = {
# .cur, .ani loose files.
"paperairplane_arrow_blue.cur" : ID_PAPERAIRPLANE_ARROW_BLUE,
"paperairplane_arrow_red.cur" : ID_PAPERAIRPLANE_ARROW_RED,
"paperairplane_arrow_grey.cur" : ID_PAPERAIRPLANE_ARROW_GREY,
"paperairplane_arrow_dark.cur" : ID_PAPERAIRPLANE_ARROW_DARK,
"paperairplane_arrow_blue_fadeout80.cur" : ID_PAPERAIRPLANE_ARROW_BLUE_FADEOUT80,
"paperairplane_arrow_white.cur" : ID_PAPERAIRPLANE_ARROW_WHITE,
"paperairplane_arrow_colorshift.ani" : ID_PAPERAIRPLANE_ARROW_COLORSHIFT,
# .png loose files.
"paperairplane_arrow_white24.png" : ID_PAPERAIRPLANE_ARROW_WHITE_PNG,
# PyEmbeddedImages
"paperairplane_arrow_blue24 [PyEmbeddedImage]" : ID_PAPERAIRPLANE_ARROW_BLUE_PY,
"paperairplane_arrow_white24 [PyEmbeddedImage]" : ID_PAPERAIRPLANE_ARROW_WHITE_PY,
# wxPython Stock Cursors.
"wx.CURSOR_ARROW" : wx.CURSOR_ARROW,
"wx.CURSOR_RIGHT_ARROW" : wx.CURSOR_RIGHT_ARROW,
"wx.CURSOR_BULLSEYE" : wx.CURSOR_BULLSEYE,
@@ -38,90 +100,224 @@ cursors = {
"wx.CURSOR_DEFAULT" : wx.CURSOR_DEFAULT,
"wx.CURSOR_COPY_ARROW" : wx.CURSOR_COPY_ARROW,
"wx.CURSOR_ARROWWAIT" : wx.CURSOR_ARROWWAIT,
"zz [custom cursor]" : CUSTOMID,
}
#-Classes----------------------------------------------------------------------
class DrawWindow(wx.Window):
def __init__(self, parent, log, id=wx.ID_ANY, pos=wx.DefaultPosition,
size=wx.DefaultSize, style=0):
wx.Window.__init__(self, parent, id, pos, size, style)
self.log = log
self.SetBackgroundColour(wx.WHITE)
self.lines = []
self.x = self.y = 0
self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
self.Bind(wx.EVT_MOTION, self.OnMotion)
self.Bind(wx.EVT_PAINT, self.OnPaint)
def OnPaint(self, event):
dc = wx.PaintDC(self)
self.DrawSavedLines(dc)
def DrawSavedLines(self, dc):
dc.SetPen(wx.Pen(wx.BLUE, 1))
for line in self.lines:
for coords in line:
dc.DrawLine(*coords)
def OnLeftDown(self, event):
self.curLine = []
self.x, self.y = event.GetPosition()
self.CaptureMouse()
def OnLeftUp(self, event):
if self.HasCapture():
self.lines.append(self.curLine)
self.curLine = []
self.ReleaseMouse()
def OnMotion(self, event):
if self.HasCapture() and event.Dragging():
dc = wx.ClientDC(self)
dc.SetPen(wx.Pen(wx.BLUE, 1))
evtPos = event.GetPosition()
coords = (self.x, self.y) + (evtPos.x, evtPos.y)
self.curLine.append(coords)
dc.DrawLine(*coords)
self.x, self.y = event.GetPosition()
class CursorTestPanel(wx.Panel):
"""
Cursor Test Panel inspired by AniFX cursor test panel.
"""
def __init__(self, parent, log):
self.log = log
wx.Panel.__init__(self, parent, -1, style=wx.BORDER_SUNKEN)
# Create all the widgets for the test panel.
pnl = wx.Panel(self, -1, style=wx.BORDER_SIMPLE)
pnl.SetBackgroundColour(wx.BLACK)
self.win = wx.Window(pnl, -1, size=(200, 100))
self.win.SetBackgroundColour("white")
self.win.Bind(wx.EVT_LEFT_DOWN, self.OnDrawDot)
self.drawWin = DrawWindow(pnl, log, size=(200, 100))
vbSizer0 = wx.BoxSizer(wx.VERTICAL)
vbSizer0.Add(self.win, 1, wx.EXPAND | wx.BOTTOM, 1)
vbSizer0.Add(self.drawWin, 1, wx.EXPAND)
pnl.SetSizer(vbSizer0)
b = wx.Button(self, -1, 'Button')
tc = wx.TextCtrl(self, -1, 'Text Ctrl')
rb1 = wx.RadioButton(self, -1, 'Radio Button 1')
rb2 = wx.RadioButton(self, -1, 'Radio Button 2')
cb = wx.CheckBox(self, -1, 'Check Box')
combo = wx.ComboBox(self, -1, 'One', choices=('One', 'Two', 'Three', 'Four', 'Five'))
sl = wx.Slider(self, -1)
sc = wx.SpinCtrl(self, -1)
# Add all the widgets to a tuple that we will access when changing cursors.
self.allWidgets = (self, pnl, self.win, self.drawWin, b, tc, rb1, rb2, cb, combo, sl, sc)
# Do the panel layout.
vbSizer = wx.BoxSizer(wx.VERTICAL)
hbSizer = wx.BoxSizer(wx.HORIZONTAL)
gSizer = wx.GridSizer(rows=4, cols=2, vgap=5, hgap=5)
gSizer.AddMany((b, tc, rb1, rb2, cb, combo, sl, sc))
hbSizer.Add(pnl, 0, wx.EXPAND | wx.ALL, 10)
hbSizer.Add(gSizer, 0, wx.EXPAND | wx.ALL, 10)
vbSizer.Add(hbSizer, 0, wx.ALL, 10)
self.SetSizer(vbSizer)
def OnDrawDot(self, event):
# Draw a dot so the user can see where the hotspot is.
dc = wx.ClientDC(self.win)
dc.SetPen(wx.Pen("RED"))
dc.SetBrush(wx.Brush("RED"))
pos = event.GetPosition()
dc.DrawCircle(pos.x, pos.y, 4)
class TestPanel(wx.Panel):
def __init__(self, parent, log):
self.log = log
wx.Panel.__init__(self, parent, -1)
# create a list of choices from the dictionary above
choices = sorted(cursors.keys())
# Create a list of choices from the dictionary above.
choices = cursors.keys()
choices = sorted(choices)
# create the controls
# Create the controls.
self.cb = wx.ComboBox(self, -1, "wx.CURSOR_DEFAULT", choices=choices,
style=wx.CB_READONLY)
self.tx = wx.StaticText(self, -1,
"This sample allows you to see all the stock cursors \n"
"available to wxPython. Simply select a name from the \n"
"wx.Choice and then move the mouse into the window \n"
"below to see the cursor. NOTE: not all stock cursors \n"
"have a specific representaion on all platforms.")
"This sample allows you to see all the stock cursors available to wxPython,\n"
"and also custom cursors loaded from images, .cur, or .ani files.\n"
"Simply select a name from the wx.Choice and then move the mouse into the window \n"
"below to see the cursor.\n"
"NOTE: not all stock cursors have a specific representaion on all platforms.")
self.win = wx.Window(self, -1, size=(200,200), style=wx.SIMPLE_BORDER)
self.win.SetBackgroundColour("white")
self.testPanel = CursorTestPanel(self, log)
# bind an event or two
# Bind events.
self.Bind(wx.EVT_COMBOBOX, self.OnChooseCursor, self.cb)
self.win.Bind(wx.EVT_LEFT_DOWN, self.OnDrawDot)
# Setup the layout.
vbSizer = wx.BoxSizer(wx.VERTICAL)
vbSizer.Add(self.tx, 0, wx.ALL, 10)
vbSizer.Add(self.cb, 0, wx.LEFT | wx.BOTTOM, 10)
# Setup the layout
gbs = wx.GridBagSizer()
gbs.Add(self.cb, (2,1))
gbs.Add(self.tx, (2,3))
gbs.Add(self.win, (5,0), (1, 6), wx.ALIGN_CENTER)
self.SetSizer(gbs)
gbs = wx.GridBagSizer(8, 8)
gbs.Add(self.testPanel, (0, 1), (1, 4), wx.ALIGN_LEFT)
vbSizer.Add(gbs, 0, wx.ALL)
self.SetSizer(vbSizer)
wx.CallAfter(self.cb.SetFocus) # Convienience start for mousewheel switching.
def OnChooseCursor(self, evt):
# clear the dots
self.win.Refresh()
# Clear the dots.
self.testPanel.win.Refresh()
self.testPanel.drawWin.lines = []
self.testPanel.drawWin.Refresh()
choice = self.cb.GetStringSelection()
self.log.write("Selecting the %s cursor\n" % choice)
self.log.WriteText("Selecting the %s cursor\n" % choice)
cnum = cursors[choice]
if cnum == CUSTOMID:
image = images.Pointy.GetImage()
if cnum in (ID_PAPERAIRPLANE_ARROW_BLUE,
ID_PAPERAIRPLANE_ARROW_RED,
ID_PAPERAIRPLANE_ARROW_GREY,
ID_PAPERAIRPLANE_ARROW_DARK,
ID_PAPERAIRPLANE_ARROW_BLUE_FADEOUT80,
ID_PAPERAIRPLANE_ARROW_WHITE,
ID_PAPERAIRPLANE_ARROW_COLORSHIFT): # .cur or .ani loose files.
# since this image didn't come from a .cur file, tell it where the hotspot is
image.SetOption(wx.IMAGE_OPTION_CUR_HOTSPOT_X, 1)
image.SetOption(wx.IMAGE_OPTION_CUR_HOTSPOT_Y, 1)
if choice.endswith('.ani'):
cursor = wx.Cursor(gFileDir + os.sep + 'cursors' + os.sep + choice, wx.BITMAP_TYPE_ANI)
if choice.endswith('.cur'):
cursor = wx.Cursor(gFileDir + os.sep + 'cursors' + os.sep + choice, wx.BITMAP_TYPE_CUR)
# make the image into a cursor
elif cnum == ID_PAPERAIRPLANE_ARROW_WHITE_PNG: # .png loose files.
image = wx.Image(gFileDir + os.sep + 'cursors' + os.sep + choice, wx.BITMAP_TYPE_PNG)
# Since these image didn't come from a .cur or .ani file,
# tell it where the hotspot is.
image.SetOption(wx.IMAGE_OPTION_CUR_HOTSPOT_X, 0)
image.SetOption(wx.IMAGE_OPTION_CUR_HOTSPOT_Y, 0)
# Make the image into a cursor.
cursor = wx.Cursor(image)
elif cnum in (ID_PAPERAIRPLANE_ARROW_BLUE_PY,
ID_PAPERAIRPLANE_ARROW_WHITE_PY): # PyEmbeddedImages
if cnum == ID_PAPERAIRPLANE_ARROW_BLUE_PY:
image = paperairplane_arrow_blue24.GetImage()
elif cnum == ID_PAPERAIRPLANE_ARROW_WHITE_PY:
image = paperairplane_arrow_white24.GetImage()
# Since these image didn't come from a .cur or .ani file,
# tell it where the hotspot is.
image.SetOption(wx.IMAGE_OPTION_CUR_HOTSPOT_X, 0)
image.SetOption(wx.IMAGE_OPTION_CUR_HOTSPOT_Y, 0)
# Make the image into a cursor.
cursor = wx.Cursor(image)
else:
# create one of the stock (built-in) cursors
# Create one of the stock (built-in) cursors.
cursor = wx.Cursor(cnum)
# set the cursor for the window
self.win.SetCursor(cursor)
# Set the cursors for all the testPanels widgets.
[widget.SetCursor(cursor) for widget in self.testPanel.allWidgets]
def OnDrawDot(self, evt):
# Draw a dot so the user can see where the hotspot is
# Draw a dot so the user can see where the hotspot is.
dc = wx.ClientDC(self.win)
dc.SetPen(wx.Pen("RED"))
dc.SetBrush(wx.Brush("RED"))
pos = evt.GetPosition()
dc.DrawCircle(pos.x, pos.y, 4)
dc.DrawCircle(pos.x, pos.y, 3)
#----------------------------------------------------------------------
#-wxPython Demo----------------------------------------------------------------
def runTest(frame, nb, log):
win = TestPanel(nb, log)
return win
#----------------------------------------------------------------------
overview = """<html><body>
<h2><center>wx.Cursor</center></h2>
@@ -132,9 +328,8 @@ This demo shows the stock mouse cursors that are available to wxPython.
"""
if __name__ == '__main__':
import sys,os
import os
import sys
import run
run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 371 B