misc updates that have been sitting on my hard drive for a while.

This commit is contained in:
Kelvin Sherlock 2023-12-27 10:58:42 -05:00
parent 734f9e0056
commit 5381f1f064
9 changed files with 169 additions and 40 deletions

View File

@ -215,7 +215,7 @@ class rObject:
r.rName, r.get_id(), r._format_attr()
), file=io)
print(content, file=io)
print("}\n", file=io)
print("};\n", file=io)
@staticmethod

View File

@ -35,24 +35,6 @@ fAlert = 0x2000
fClose = 0x4000
fTitle = 0x8000
# menu item flags (duplicate menu flags.)
rMIPlain = 0x0000
rMIBold = 0x0001
rMIItalic = 0x0002
rMIUnderline = 0x0004
rMIXOr = 0x0020
rMIDivider = 0x0040
rMIDisabled = 0x0080
rMIItemStruct = 0x0400
rMIOutline = 0x0800
rMIShadow = 0x1000
# menu flags
rmAllowCache = 0x0008
rmCustom = 0x0010
rmNo_Xor = 0x0020
rmDisabled = 0x0080
# common flags
ctlInvis = 0x0080
@ -139,3 +121,4 @@ TBVenice = b"\x05\x00"
TBTimes = b"\x14\x00"
TBHelvetica = b"\x15\x00"
TBCourier = b"\x16\x00"

View File

@ -57,6 +57,51 @@ class rControlTemplate(rObject):
# word background when selected
# word title when selected
# word title when not selected
class rSimpleButtonColors(rObject):
rName = "rCtlColorTbl"
rType = 0x800D
def __init__(self, *,
outline=Black,
background=White,
backgroundSelected=Black,
# textBackground should usually match the background color.
text=Black,
textBackground=None,
textSelected=White,
textSelectedBackground=None,
**kwargs):
super().__init__(**kwargs)
data = [0, 0, 0, 0, 0]
if textBackground == None: textBackground = background
if textSelectedBackground == None: textSelectedBackground = backgroundSelected
data[0] = outline.value << 4
data[1] = background.value << 4
data[2] = backgroundSelected.value << 4
data[3] = text.value | (textBackground.value << 4)
data[4] = textSelected.value | (textSeletectBackground.value << 4)
self.data = data
def __bytes__(self):
return struct.pack("<5H", *self.data)
def _rez_string(self):
return(
"\t0x{:04x}, /* outline color */\n"
"\t0x{:04x}, /* interior color */\n"
"\t0x{:04x}, /* interior selected color */\n"
"\t0x{:04x}, /* text color */\n"
"\t0x{:04x} /* text selected color */\n"
).format(*self.data)
class rSimpleButton(rControlTemplate):
rName = "rControlTemplate"
@ -151,7 +196,50 @@ class rSimpleButton(rControlTemplate):
# word reserved
# word box not selected
# word box checked
# word title
# word title
class rCheckControlColors(rObject):
rName = "rCtlColorTbl"
rType = 0x800D
def __init__(self, *,
background=White,
foreground=Black,
backgroundSelected=Black,
foregroundSelected=White,
text=Black,
textBackground=White,
**kwargs):
super().__init__(**kwargs)
data = [0, 0, 0, 0,]
if textBackground == None: textBackground = background
if textSelectedBackground == None: textSelectedBackground = backgroundSelected
data[1] = foreground.value | (background.value << 4)
data[2] = foregroundSelected.value | (backgroundSelected.value << 4)
data[3] = text.value | (textBackground.value << 4)
self.data = data
def __bytes__(self):
return struct.pack("<4H", *self.data)
def _rez_string(self):
return(
"\t0x{:04x}, /* reserved */\n"
"\t0x{:04x}, /* check box */\n"
"\t0x{:04x}, /* check box (selected) */\n"
"\t0x{:04x}, /* text */\n"
).format(*self.data)
# same format
rRadioControlColors = rCheckControlColors
class rCheckControl(rControlTemplate):
rName = "rControlTemplate"
rType = 0x8004
@ -237,6 +325,9 @@ class rCheckControl(rControlTemplate):
)
return rv
# TODO - colors
# color table (TB V1 Ch 4-88):
# word reserved

View File

@ -78,9 +78,9 @@ class rIcon(rObject):
"\t{:d}, /* height */\n"
"\t{:d}, /* width */\n"
).format(self.type, self.height, self.width)
s += "\n\t/*image */\n"
s += "\n\t/* image */\n"
s += rez_hex(self.image, width = self.width // 2, indent = 1, comma = True)
s += "\n\t/*mask */\n"
s += "\n\t/* mask */\n"
s += rez_hex(self.mask, width = self.width // 2, indent = 1, comma = False)
return s

View File

@ -3,7 +3,7 @@ import enum
from utils import *
import struct
__all__ = ["KeyEquiv"]
__all__ = ["KeyEquivalent"]
def export_enum(cls):
global __all__

66
menu.py
View File

@ -3,6 +3,7 @@ from base import *
from utils import *
from icon import rIcon
import struct
import enum
__all__ = [
'rMenuBar',
@ -19,6 +20,37 @@ __all__ = [
'DividerMenuItem'
]
def export_enum(cls):
global __all__
members = cls.__members__
globals().update(members)
if __all__ != None: __all__.extend(list(members))
return cls
@export_enum
class MenuFlags(enum.Flag):
# menu item flags (duplicate menu flags.)
mPlain = 0x0000
mBold = 0x0001
mItalic = 0x0002
mUnderline = 0x0004
mXOR = 0x0020
mDivider = 0x0040
mDisabled = 0x0080
# menuIItemStruct = 0x0400
mOutline = 0x0800
mShadow = 0x1000
# menu flags
mAllowCache = 0x0008
mCustom = 0x0010
# rmNo_Xor = 0x0020
# rmDisabled = 0x0080
# see:
# Programmer's Reference for System 6 (Ch 13 Menu Manager Update, pg 103+)
# TB Vol 3 Chapter 37
@ -73,10 +105,13 @@ class rMenuBar(rObject):
def __init__(self, *children, **kwargs):
super().__init__(**kwargs)
self.children = children[:]
for x in children:
if not isinstance(x, rMenu):
raise TypeError("bad type: {}".format(type(x)))
raise TypeError("rMenuBar: bad type: {}".format(type(x)))
self.children = children
def __bytes__(self):
bb = struct.pack("<HH", 0, 0x8000) # version, resIDs
@ -113,27 +148,30 @@ class rMenu(rObject):
#flags = all off = a080 (disabled)
def __init__(self, title, *children,
flags=0x0000, menuID=None,
menuID=None,
**kwargs
):
super().__init__(**kwargs)
flags = 0xa000 # title ref is resource, menus are resources
self.title = rPString.make_string(title)
self.children = children[:]
self.menuID = menuID
self.children = []
flags |= 0xa000 # title ref is resource, menus are resources
if kwargs.get("allowCache"): flags |= 0x0008
if kwargs.get("custom"): flags |= 0x0010
if kwargs.get("xor"): flags |= 0x0020
if kwargs.get("disabled"): flags |= 0x0080
if kwargs.get("mChoose"): flags |= 0x0100
VALID = mAllowCache | mCustom | mXOR | mDisabled
for x in children:
if type(x) == MenuFlags:
if x not in VALID:
raise ValueError("rMenu: bad flag: {}".format(x))
flags |= x.value
continue
if isinstance(x, rMenuItem):
self.children.append(x)
continue
raise TypeError("rMenu: bad type: {}".format(type(x)))
self.flags = flags
for x in children:
if not isinstance(x, rMenuItem):
raise TypeError("bad type: {}".format(type(x)))
def __bytes__(self):
menuID = self.menuID

View File

@ -4,9 +4,11 @@ from base import *
from window import *
from control import *
from menu import *
from version import *
from rect import rect, point, size
from sound import *
from alert import *
rPString("hello")
rCString("goodbye")
@ -57,5 +59,10 @@ rTextBlock(
UndoMenuItem()
UndoMenuItem()
rVersion("1.2.3b4", verFrance, "name", "description")
rAlertString(2, "this is an alert string", awButton(awOK, default=True), awButton(awCancel))
rObject.dump_hex()
rObject.dump_rez()

View File

@ -47,7 +47,7 @@ class rToolStartup(rObject):
if type(x) == tuple and len(x) == 2:
a,b = x
if type(a) == int and type(b) == int: continue
raise Type("rToolStartup: bad tool: {}".format(x))
raise TypeError("rToolStartup: bad tool: {}".format(x))
self.tools = [x if type(x) == tuple else (x, 0) for x in tools]

View File

@ -3,6 +3,16 @@
# __all__ = ("str_to_bytes", "make_string", "format_rect", "format_point", "format_size")
# decorators
def export_enum(cls):
global __all__
members = cls.__members__
globals().update(members)
if __all__ != None: __all__.extend(list(members))
return cls
# helper functions
def str_to_bytes(text):
if isinstance(text, str): return text.encode("macroman")
@ -24,8 +34,8 @@ def format_size(x):
def _generate_map():
map = { x: "\\${:02x}".format(x) for x in range(0, 256) if x < 32 or x > 126 }
map[0x0d] = "\\n" # intentionally backwards.
map[0x0a] = "\\r" # intentionally backwards.
map[0x0d] = "\\r" # intentionally backwards. -- no more
map[0x0a] = "\\n" # intentionally backwards. -- no more
map[0x09] = "\\t"
# \b \f \v \? also supported.
map[ord('"')] = '\\"'