From 1e77651b0c1a57911785dcee2d8f96bf5187243a Mon Sep 17 00:00:00 2001 From: Kelvin Sherlock Date: Sun, 9 Aug 2020 00:32:43 -0400 Subject: [PATCH] rItemStruct support --- examples/icon-menu.prez | 26 +++++++++++++++++++ menu.py | 55 ++++++++++++++++++++++++++++++++--------- 2 files changed, 69 insertions(+), 12 deletions(-) create mode 100644 examples/icon-menu.prez diff --git a/examples/icon-menu.prez b/examples/icon-menu.prez new file mode 100644 index 0000000..dc8b1db --- /dev/null +++ b/examples/icon-menu.prez @@ -0,0 +1,26 @@ +# +# a menu item using an rItemStruct to include text and an icon. +# +# + + +rMenuItem( + rItemStruct( + "text", + rIcon(color = True, height = 4, width = 4, + image = ( + "0ff0" + "f00f" + "f00f" + "0ff0" + ), + mask = ( + "0ff0" + "f00f" + "f00f" + "0ff0" + ), + + ) + ) +) diff --git a/menu.py b/menu.py index 1142d29..ef442e6 100644 --- a/menu.py +++ b/menu.py @@ -1,12 +1,14 @@ from base import * from utils import * +from icon import rIcon import struct __all__ = [ 'rMenuBar', 'rMenu', 'rMenuItem', + 'rItemStruct', 'UndoMenuItem', 'CutMenuItem', @@ -22,8 +24,6 @@ __all__ = [ # TB Vol 3 Chapter 37 # TB Vol 1 Chapter 13 -# TODO - menu item icon support (system 6) rItemStruct - # A menu ID must be unique for each menu; that is, no two menus # can have the same ID or the system will fall. Similarly, no two # items can have the same Item ID. @@ -32,14 +32,40 @@ _menu_ids = {} _menu_item_ids = {} +# ref is resource flag for the text is stored in the menu item flags +# icon could be null but in that case, why are you using an rItemStruct???? +class rItemStruct(rObject): + rName = "rItemStruct" + rType = 0x8028 + + def __init__(self, text, icon, **kwargs): + super().__init__(**kwargs) + + self.text = rPString.make_string(text) + if type(icon) != rIcon: + raise TypeError("rItemStruct: bad icon type: {}".format(type(icon))) + + self.icon = icon + + def __bytes__(self): + return struct.pack("= 32 and x < 0x7e: return '"' + chr(x) + '"' -# return "\\${:02x}".format(x) class rMenuBar(rObject): rName = "rMenuBar" @@ -173,7 +199,11 @@ class rMenuItem(rObject): **kwargs): super().__init__(**kwargs) - self.title = rPString.make_string(title) + if isinstance(title, rItemStruct): + self.title = title + flags |= 0b0000_0110_0000_0000 # title is rItem struct, is resource + else: + self.title = rPString.make_string(title) flags |= 0x8000 # title ref is resource if kwargs.get("bold"): flags |= 0x0001 @@ -234,14 +264,15 @@ class rMenuItem(rObject): "\t{}, {}, /* chars */\n" "\t0x{:04x}, /* check */\n" "\t0x{:04x}, /* flags */\n" - "\t0x{:04x} /* title ref (rPString) */" + "\t0x{:04x} /* title ref ({}) */" ).format( itemID, format_char(self.itemChar), format_char(self.altItemChar), self.checkMark, self.flags, - self.title.get_id() + self.title.get_id(), + self.title.rName )