Skip to content

Commit d491bfe

Browse files
committed
Added "ASCII View" Button to ContextMenu for cs.Bytes
1 parent 0b35e2c commit d491bfe

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

construct_editor/helper/wrapper.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def int_to_str(integer_format: "construct_editor.IntegerFormat", val: int) -> st
2929
return f"0x{val:X}"
3030
return f"{val}"
3131

32+
3233
def str_to_int(s: str) -> int:
3334
if len(s) == 0:
3435
s = "0"
@@ -40,7 +41,6 @@ def str_to_int(s: str) -> int:
4041
return i
4142

4243

43-
4444
# #####################################################################################################################
4545
# GUI Elements ########################################################################################################
4646
# #####################################################################################################################
@@ -94,7 +94,7 @@ def __init__(self, parent, entry: "EntryConstruct"):
9494

9595
self.SetSizer(hsizer)
9696
self.Layout()
97-
97+
9898
def get_new_obj(self) -> Any:
9999
return self.entry.obj
100100

@@ -300,7 +300,9 @@ def __init__(self, parent, entry: Union["EntryTFlagsEnum", "EntryFlagsEnum"]):
300300
items = self.entry.get_flagsenum_items_from_obj()
301301
for pos, item in enumerate(items):
302302
self.popup_ctrl.clbx.Insert(
303-
item=f"{int_to_str(entry.model.integer_format, item.value)} ({item.name})", pos=pos, clientData=item
303+
item=f"{int_to_str(entry.model.integer_format, item.value)} ({item.name})",
304+
pos=pos,
305+
clientData=item,
304306
)
305307
self.popup_ctrl.clbx.Check(pos, item.checked)
306308

@@ -763,7 +765,7 @@ def create_obj_panel(self, parent) -> ObjPanel:
763765
return ObjPanel_Default(parent, self) # TODO: create panel for cs.Array
764766

765767
def modify_context_menu(self, menu: "construct_editor.ContextMenu"):
766-
# If the subentry has no subentries itselfe, it makes no sense to create a list view.
768+
# If the subentry has no subentries itself, it makes no sense to create a list view.
767769
temp_subentry = create_entry_from_construct(
768770
self.model, self, self.construct.subcon
769771
)
@@ -1227,11 +1229,21 @@ def __init__(
12271229
construct: "cs.Bytes[Any, Any]",
12281230
):
12291231
super().__init__(model, parent, construct)
1232+
self.ascii_view = False
12301233

12311234
@property
12321235
def obj_str(self) -> str:
12331236
try:
1234-
return self.obj.hex(" ")
1237+
if self.ascii_view:
1238+
chars = []
1239+
for b in self.obj:
1240+
char = chr(b)
1241+
if char not in string.printable:
1242+
char = "."
1243+
chars.append(char)
1244+
return "".join(chars)
1245+
else:
1246+
return self.obj.hex(" ")
12351247
except Exception:
12361248
return str(self.obj)
12371249

@@ -1243,6 +1255,18 @@ def typ_str(self) -> str:
12431255
except Exception:
12441256
return "Byte[{}]".format(str(self.construct.length))
12451257

1258+
def modify_context_menu(self, menu: "construct_editor.ContextMenu"):
1259+
menu.Append(wx.MenuItem(menu, wx.ID_ANY, kind=wx.ITEM_SEPARATOR))
1260+
1261+
def on_menu_item_clicked(event: wx.MenuEvent):
1262+
self.ascii_view = not self.ascii_view
1263+
menu.parent.reload()
1264+
1265+
menu_item = wx.MenuItem(menu, wx.ID_ANY, "ASCII View", kind=wx.ITEM_CHECK)
1266+
menu.Append(menu_item)
1267+
menu_item.Check(self.ascii_view)
1268+
menu.Bind(wx.EVT_MENU, on_menu_item_clicked, menu_item)
1269+
12461270

12471271
# EntryRenamed ########################################################################################################
12481272
class EntryRenamed(EntrySubconstruct):

0 commit comments

Comments
 (0)