2019-09-01 14:34:00 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
2019-09-09 10:46:52 +00:00
|
|
|
# GuiFrame.py
|
2019-09-04 14:23:59 +00:00
|
|
|
# Copyright (c) 2018, 2019 Lucio Andrés Illanes Albornoz <lucio@lucioillanes.de>
|
2019-09-01 14:34:00 +00:00
|
|
|
#
|
|
|
|
|
2019-09-10 08:14:12 +00:00
|
|
|
import os, sys, wx
|
2019-09-01 14:34:00 +00:00
|
|
|
|
2019-09-10 08:22:02 +00:00
|
|
|
#
|
|
|
|
# Decorators
|
|
|
|
# {{{ GuiCommandDecorator(targetObject)
|
|
|
|
def GuiCommandDecorator(caption, label, icon, accel, initialState):
|
|
|
|
def GuiCommandDecoratorOuter(targetObject):
|
|
|
|
if callable(targetObject):
|
|
|
|
if not hasattr(targetObject, "attrDict"):
|
|
|
|
setattr(targetObject, "attrDict", [])
|
|
|
|
targetObject.attrDict = {"caption": caption, "label": label, "icon": icon, "accel": accel, "initialState": initialState, "id": None}
|
|
|
|
return targetObject
|
|
|
|
return GuiCommandDecoratorOuter
|
|
|
|
# }}}
|
|
|
|
# {{{ GuiCommandListDecorator(targetObject)
|
|
|
|
def GuiCommandListDecorator(idx, caption, label, icon, accel, initialState):
|
|
|
|
def GuiCommandListDecoratorOuter(targetObject):
|
|
|
|
if callable(targetObject):
|
|
|
|
if not hasattr(targetObject, "attrList"):
|
|
|
|
setattr(targetObject, "attrList", [])
|
|
|
|
targetObject.attrList.insert(0, {"caption": caption, "label": label, "icon": icon, "accel": accel, "initialState": initialState, "id": None, "idx": idx})
|
|
|
|
return targetObject
|
|
|
|
return GuiCommandListDecoratorOuter
|
|
|
|
# }}}
|
|
|
|
# {{{ GuiSelectDecorator(targetObject)
|
|
|
|
def GuiSelectDecorator(idx, caption, label, icon, accel, initialState):
|
|
|
|
def GuiSelectDecoratorOuter(targetObject):
|
|
|
|
if callable(targetObject):
|
|
|
|
if not hasattr(targetObject, "attrList"):
|
|
|
|
setattr(targetObject, "attrList", [])
|
|
|
|
setattr(targetObject, "isSelect", True)
|
|
|
|
targetObject.attrList.insert(0, {"caption": caption, "label": label, "icon": icon, "accel": accel, "initialState": initialState, "id": None, "idx": idx})
|
|
|
|
return targetObject
|
|
|
|
return GuiSelectDecoratorOuter
|
|
|
|
# }}}
|
|
|
|
|
2019-09-08 14:57:45 +00:00
|
|
|
#
|
|
|
|
# Non-items (0xf000-0xffff)
|
2019-09-10 08:14:12 +00:00
|
|
|
NID_MENU_SEP = 0xf000
|
|
|
|
NID_TOOLBAR_HSEP = 0xf001
|
2019-09-04 10:50:21 +00:00
|
|
|
|
2019-09-08 14:57:45 +00:00
|
|
|
class GuiFrame(wx.Frame):
|
2019-09-10 08:14:12 +00:00
|
|
|
# {{{ _initIcon(self, iconPathName)
|
|
|
|
def _initIcon(self, iconPathName):
|
|
|
|
icon = wx.Icon()
|
|
|
|
icon.CopyFromBitmap(wx.Bitmap(iconPathName, wx.BITMAP_TYPE_ANY))
|
|
|
|
self.SetIcon(icon)
|
|
|
|
# }}}
|
|
|
|
|
2019-09-10 10:28:26 +00:00
|
|
|
# {{{ loadAccels(self, menus, toolBars)
|
|
|
|
def loadAccels(self, menus, toolBars):
|
|
|
|
def loadAccels_(accels):
|
|
|
|
nonlocal accelTableEntries
|
|
|
|
for accel in accels:
|
|
|
|
if (not accel in [NID_MENU_SEP, NID_TOOLBAR_HSEP]) \
|
|
|
|
and (accel.attrDict["accel"] != None):
|
|
|
|
accelTableEntries += [wx.AcceleratorEntry()]
|
|
|
|
if accel.attrDict["id"] == None:
|
|
|
|
accel.attrDict["id"] = self.lastId; self.lastId += 1;
|
|
|
|
accelTableEntries[-1].Set(*accel.attrDict["accel"], accel.attrDict["id"])
|
|
|
|
accel.attrDict["accelEntry"] = accelTableEntries[-1]
|
|
|
|
self.itemsById[accel.attrDict["id"]] = accel
|
|
|
|
self.Bind(wx.EVT_MENU, self.onMenu, id=accel.attrDict["id"])
|
2019-09-08 14:57:45 +00:00
|
|
|
accelTableEntries = []
|
2019-09-10 10:28:26 +00:00
|
|
|
[loadAccels_(menu[1:]) for menu in menus]; [loadAccels_(toolBar) for toolBar in toolBars];
|
2019-09-08 14:57:45 +00:00
|
|
|
self.SetAcceleratorTable(wx.AcceleratorTable(accelTableEntries))
|
2019-09-01 14:34:00 +00:00
|
|
|
# }}}
|
2019-09-10 08:14:12 +00:00
|
|
|
# {{{ loadBitmap(self, basePathName, descr, size=(16, 16))
|
|
|
|
def loadBitmap(self, basePathName, descr, size=(16, 16)):
|
|
|
|
if descr == None:
|
|
|
|
descr = ["", None, wx.ArtProvider.GetBitmap(wx.ART_HELP, wx.ART_TOOLBAR, size)]
|
|
|
|
elif (descr[0] == "") and (descr[1] != None):
|
|
|
|
descr = ["", None, wx.ArtProvider.GetBitmap(descr[1], wx.ART_TOOLBAR, size)]
|
|
|
|
elif descr[0] != "":
|
|
|
|
bitmap, bitmapPathName = wx.Bitmap((16, 16)), os.path.join(basePathName, descr[0])
|
|
|
|
bitmap.LoadFile(bitmapPathName, wx.BITMAP_TYPE_ANY)
|
|
|
|
descr = ["", None, bitmap]
|
|
|
|
elif len(descr) == 3:
|
|
|
|
descr = ("", None, descr[2])
|
|
|
|
return descr
|
2019-09-04 10:50:21 +00:00
|
|
|
# }}}
|
2019-09-10 08:14:12 +00:00
|
|
|
# {{{ loadMenus(self, menus)
|
|
|
|
def loadMenus(self, menus):
|
2019-09-08 14:57:45 +00:00
|
|
|
menuBar = wx.MenuBar()
|
|
|
|
for menu in menus:
|
|
|
|
menuWindow = wx.Menu()
|
|
|
|
for menuItem in menu[1:]:
|
|
|
|
if menuItem == NID_MENU_SEP:
|
|
|
|
menuWindow.AppendSeparator()
|
|
|
|
else:
|
|
|
|
if menuItem.attrDict["id"] == None:
|
|
|
|
menuItem.attrDict["id"] = self.lastId; self.lastId += 1;
|
|
|
|
self.itemsById[menuItem.attrDict["id"]] = menuItem
|
|
|
|
if hasattr(menuItem, "isSelect"):
|
|
|
|
menuItemWindow = menuWindow.AppendRadioItem(menuItem.attrDict["id"], menuItem.attrDict["label"], menuItem.attrDict["caption"])
|
|
|
|
else:
|
|
|
|
menuItemWindow = menuWindow.Append(menuItem.attrDict["id"], menuItem.attrDict["label"], menuItem.attrDict["caption"])
|
|
|
|
if menuItem.attrDict["accel"] != None:
|
|
|
|
menuItemWindow.SetAccel(menuItem.attrDict["accelEntry"])
|
|
|
|
self.menuItemsById[menuItem.attrDict["id"]] = menuItemWindow
|
2019-09-10 08:14:12 +00:00
|
|
|
self.Bind(wx.EVT_MENU, self.onMenu, menuItemWindow)
|
2019-09-08 14:57:45 +00:00
|
|
|
if menuItem.attrDict["initialState"] != None:
|
|
|
|
if hasattr(menuItem, "isSelect"):
|
|
|
|
menuItemWindow.Check(menuItem.attrDict["initialState"])
|
|
|
|
else:
|
|
|
|
menuItemWindow.Enable(menuItem.attrDict["initialState"])
|
|
|
|
menuBar.Append(menuWindow, menu[0])
|
|
|
|
self.SetMenuBar(menuBar)
|
2019-09-01 14:34:00 +00:00
|
|
|
# }}}
|
2019-09-10 08:14:12 +00:00
|
|
|
# {{{ loadToolBars(self, toolBars)
|
|
|
|
def loadToolBars(self, toolBars):
|
2019-09-08 14:57:45 +00:00
|
|
|
for toolBar in toolBars:
|
2019-09-10 08:14:12 +00:00
|
|
|
self.toolBars.append(wx.ToolBar(self.panelSkin, -1, style=wx.TB_FLAT | wx.HORIZONTAL | wx.TB_NODIVIDER))
|
2019-09-08 14:57:45 +00:00
|
|
|
self.toolBars[-1].SetToolBitmapSize((16, 16))
|
|
|
|
for toolBarItem in toolBar:
|
|
|
|
if toolBarItem == NID_TOOLBAR_HSEP:
|
|
|
|
self.toolBars[-1].AddSeparator()
|
|
|
|
else:
|
|
|
|
if toolBarItem.attrDict["id"] == None:
|
|
|
|
toolBarItem.attrDict["id"] = self.lastId; self.lastId += 1;
|
|
|
|
self.itemsById[toolBarItem.attrDict["id"]] = toolBarItem
|
|
|
|
if hasattr(toolBarItem, "isSelect"):
|
|
|
|
toolBarItemWindow = self.toolBars[-1].AddRadioTool(toolBarItem.attrDict["id"], toolBarItem.attrDict["caption"], toolBarItem.attrDict["icon"][2], shortHelp=toolBarItem.attrDict["label"])
|
|
|
|
else:
|
|
|
|
toolBarItemWindow = self.toolBars[-1].AddTool(toolBarItem.attrDict["id"], toolBarItem.attrDict["caption"], toolBarItem.attrDict["icon"][2], shortHelp=toolBarItem.attrDict["label"])
|
|
|
|
self.toolBarItemsById[toolBarItem.attrDict["id"]] = toolBarItemWindow
|
2019-09-10 08:14:12 +00:00
|
|
|
self.Bind(wx.EVT_TOOL, self.onMenu, toolBarItemWindow)
|
|
|
|
self.Bind(wx.EVT_TOOL_RCLICKED, self.onMenu, toolBarItemWindow)
|
2019-09-08 14:57:45 +00:00
|
|
|
if toolBarItem.attrDict["initialState"] != None:
|
|
|
|
if hasattr(toolBarItem, "isSelect"):
|
|
|
|
toolBarItemWindow.Toggle(toolBarItem.attrDict["initialState"])
|
|
|
|
else:
|
|
|
|
toolBarItemWindow.Enable(toolBarItem.attrDict["initialState"])
|
|
|
|
for toolBar in self.toolBars:
|
|
|
|
self.sizerSkin.Add(toolBar, 0, wx.ALIGN_LEFT | wx.ALL, 3)
|
|
|
|
toolBar.Realize(); toolBar.Fit();
|
2019-09-01 14:34:00 +00:00
|
|
|
# }}}
|
2019-09-10 08:14:12 +00:00
|
|
|
# {{{ addWindow(self, window, border=14, expand=False)
|
|
|
|
def addWindow(self, window, border=14, expand=False):
|
|
|
|
flags = wx.ALL; flags = flags | wx.EXPAND if expand else flags;
|
|
|
|
self.sizerSkin.Add(window, 0, flags, border); self.sizerSkin.Fit(self.panelSkin);
|
2019-09-08 14:57:45 +00:00
|
|
|
# }}}
|
2019-09-09 10:44:32 +00:00
|
|
|
# {{{ onChar(self, event)
|
|
|
|
def onChar(self, event):
|
2019-09-10 08:14:12 +00:00
|
|
|
event.Skip()
|
2019-09-09 10:44:32 +00:00
|
|
|
# }}}
|
2019-09-10 08:14:12 +00:00
|
|
|
# {{{ onMenu(self, event)
|
|
|
|
def onMenu(self, event):
|
2019-09-10 10:06:56 +00:00
|
|
|
eventId = event.GetId(); self.itemsById[eventId](event);
|
2019-09-01 14:34:00 +00:00
|
|
|
# }}}
|
Initial canvas panel scrollbar implementation.
assets/text/TODO: updated.
libgui/GuiCanvasPanel.py:__init__(): provide self.winSize & call SetScrollRate().
libgui/GuiCanvasPanel.py:{_drawPatch,dispatch{DeltaPatches,Patch},onPanel{Input,LeaveWindow},resize,update}(): receive and/or pass viewRect.
libgui/GuiCanvasPanel.py:onPanelPaint(): updated.
libgui/GuiCanvasPanel.py:resize(): call SetVirtualSize().
libgui/GuiCanvasWxBackend.py:_{draw{Brush,Char}Patch,_get{Brush,Char}PatchColours,xlatePoint}(): updated.
libgui/GuiCanvasWxBackend.py:{draw{CursorMaskWithJournal,Patch},getDeviceContext,xlateEventPoint}(): receive and/or pass viewRect.
libgui/GuiCanvasWxBackend.py:getDeviceContext(): return ClientDC() if viewRect > (0, 0)
libgui/GuiCanvasWxBackend.py:onPanelPaintEvent(): blit subset of canvasBitmap into BufferedPaintDC() if viewRect > (0, 0).
libtools/Tool{,Circle,Fill,Line,Rect,Select{,Clone,Move},Text}.py: receive & pass viewRect to dispatchFn().
2019-09-09 10:30:25 +00:00
|
|
|
# {{{ onMouseWheel(self, event)
|
|
|
|
def onMouseWheel(self, event):
|
2019-09-10 08:14:12 +00:00
|
|
|
event.Skip()
|
Initial canvas panel scrollbar implementation.
assets/text/TODO: updated.
libgui/GuiCanvasPanel.py:__init__(): provide self.winSize & call SetScrollRate().
libgui/GuiCanvasPanel.py:{_drawPatch,dispatch{DeltaPatches,Patch},onPanel{Input,LeaveWindow},resize,update}(): receive and/or pass viewRect.
libgui/GuiCanvasPanel.py:onPanelPaint(): updated.
libgui/GuiCanvasPanel.py:resize(): call SetVirtualSize().
libgui/GuiCanvasWxBackend.py:_{draw{Brush,Char}Patch,_get{Brush,Char}PatchColours,xlatePoint}(): updated.
libgui/GuiCanvasWxBackend.py:{draw{CursorMaskWithJournal,Patch},getDeviceContext,xlateEventPoint}(): receive and/or pass viewRect.
libgui/GuiCanvasWxBackend.py:getDeviceContext(): return ClientDC() if viewRect > (0, 0)
libgui/GuiCanvasWxBackend.py:onPanelPaintEvent(): blit subset of canvasBitmap into BufferedPaintDC() if viewRect > (0, 0).
libtools/Tool{,Circle,Fill,Line,Rect,Select{,Clone,Move},Text}.py: receive & pass viewRect to dispatchFn().
2019-09-09 10:30:25 +00:00
|
|
|
# }}}
|
2019-09-01 14:34:00 +00:00
|
|
|
|
|
|
|
#
|
2019-09-10 08:14:12 +00:00
|
|
|
# __init__(self, iconPathName, size, parent=None, title=""): initialisation method
|
|
|
|
def __init__(self, iconPathName, size, parent=None, title=""):
|
|
|
|
super().__init__(parent, wx.ID_ANY, title, size=size)
|
|
|
|
self.itemsById, self.lastId, self.menuItemsById, self.toolBarItemsById = {}, 0, {}, {}
|
2019-09-08 14:57:45 +00:00
|
|
|
self.panelSkin, self.sizerSkin, self.toolBars = wx.Panel(self, wx.ID_ANY), wx.BoxSizer(wx.VERTICAL), []
|
2019-09-10 08:14:12 +00:00
|
|
|
self.sizerSkin.AddSpacer(5); self.panelSkin.SetSizer(self.sizerSkin); self.panelSkin.SetAutoLayout(1);
|
|
|
|
self._initIcon(iconPathName); self.statusBar = self.CreateStatusBar();
|
|
|
|
self.sizerSkin.Fit(self.panelSkin); self.SetFocus(); self.Show(True);
|
|
|
|
for event, f in ((wx.EVT_CHAR, self.onChar), (wx.EVT_MENU, self.onMenu), (wx.EVT_MOUSEWHEEL, self.onMouseWheel)):
|
|
|
|
self.Bind(event, f)
|
2019-09-08 14:57:45 +00:00
|
|
|
|
|
|
|
# vim:expandtab foldmethod=marker sw=4 ts=4 tw=120
|