Skip to content

Commit

Permalink
Added convenience wrappers around dialog prompting
Browse files Browse the repository at this point in the history
  • Loading branch information
robmcmullen committed Feb 2, 2016
1 parent 21e348a commit b511833
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 52 deletions.
52 changes: 11 additions & 41 deletions omnivore/tasks/hex_edit/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from omnivore.framework.actions import *
from commands import *
from omnivore.utils.wx.antic_colors import AnticColorDialog
from omnivore.utils.wx.dialogs import HexEntryDialog
from omnivore.utils.wx.dialogs import prompt_for_hex, prompt_for_string
from omnivore.framework.minibuffer import *

class FontChoiceGroup(TaskDynamicSubmenuGroup):
Expand Down Expand Up @@ -100,17 +100,8 @@ class BitmapWidthAction(EditorAction):

def perform(self, event):
e = self.active_editor
dlg = wx.TextEntryDialog(event.task.window.control, 'Enter new bitmap width in bytes', 'Set Bitmap Width', str(e.bitmap_width))

if dlg.ShowModal() == wx.ID_OK:
try:
width = int(dlg.GetValue())
except ValueError:
log.debug("Bad value: %s" % dlg.GetValue())
width = 0

dlg.Destroy()
if width > 0:
width = prompt_for_hex(e.window.control, 'Enter new bitmap width in bytes', 'Set Bitmap Width', e.bitmap_width)
if width is not None and width > 0:
wx.CallAfter(e.set_bitmap_width, width)


Expand All @@ -119,17 +110,8 @@ class FontMappingWidthAction(EditorAction):

def perform(self, event):
e = self.active_editor
dlg = wx.TextEntryDialog(event.task.window.control, 'Enter new map width in bytes', 'Set Map Width', str(e.map_width))

if dlg.ShowModal() == wx.ID_OK:
try:
width = int(dlg.GetValue())
except ValueError:
log.debug("Bad value: %s" % dlg.GetValue())
width = 0

dlg.Destroy()
if width > 0:
width = prompt_for_hex(e.window.control, 'Enter new map width in bytes', 'Set Map Width', str(e.map_width))
if width is not None and width > 0:
wx.CallAfter(e.set_map_width, width)


Expand Down Expand Up @@ -271,19 +253,13 @@ class GetSegmentFromSelectionAction(EditorAction):

def perform(self, event):
e = self.active_editor
dialog = wx.TextEntryDialog(e.window.control, "Enter segment name", "New Segment")

result = dialog.ShowModal()
if result == wx.ID_OK:
text = prompt_for_string(e.window.control, "Enter segment name", "New Segment")
if text is not None:
segment = e.get_segment_from_selection()
text = dialog.GetValue()
if not text:
text = "%04x-%04x" % (segment.start_addr, segment.start_addr + len(segment) - 1)
segment.name = text
else:
value = None
dialog.Destroy()
e.add_user_segment(segment)
e.add_user_segment(segment)


class GotoIndexAction(Action):
Expand All @@ -304,8 +280,7 @@ class SegmentGotoAction(EditorAction):

def perform(self, event):
e = self.active_editor
dialog = HexEntryDialog(e.window.control, "Enter address value: (prefix with 0x or $ for hex)", "Goto Address in a Segment")
addr = dialog.show_and_get_value()
addr = prompt_for_hex(e.window.control, "Enter address value: (prefix with 0x or $ for hex)", "Goto Address in a Segment")
if addr is not None:
segment_num, segment, index = e.document.find_segment_in_range(addr)
if segment_num >= 0:
Expand Down Expand Up @@ -364,13 +339,8 @@ class IndexRangeValueAction(IndexRangeAction):
def _name_default(self):
return self.cmd.pretty_name + "..."

def get_value(self, editor):
dialog = HexEntryDialog(editor.window.control, "Enter byte value: (prefix with 0x or $ for hex)", "Byte Value")
return dialog.show_and_get_value()

def show_dialog(self, editor):
e = editor
value = self.get_value(editor)
def show_dialog(self, e):
value = prompt_for_hex(e.window.control, "Enter byte value: (prefix with 0x or $ for hex)", "Byte Value")
if value is not None:
cmd = self.cmd(e.segment, e.anchor_start_index, e.anchor_end_index, value)
self.active_editor.process_command(cmd)
Expand Down
47 changes: 36 additions & 11 deletions omnivore/utils/wx/dialogs.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import wx

class HexEntryDialog(wx.TextEntryDialog):
class SimplePromptDialog(wx.TextEntryDialog):
"""Simple subclass of wx.TextEntryDialog to convert text result to a
specific format
"""
def convert_text(self, text):
return text

def show_and_get_value(self):
result = self.ShowModal()
if result == wx.ID_OK:
text = self.GetValue()
value = self.convert_text(text)
else:
value = None
self.Destroy()
return value

class HexEntryDialog(SimplePromptDialog):
"""Simple subclass of wx.TextEntryDialog to convert text result from
hexidecimal if necessary.
"""
def get_int(self):
text = self.GetValue()
def convert_text(self, text):
try:
if text.startswith("0x"):
value = int(text[2:], 16)
Expand All @@ -17,11 +33,20 @@ def get_int(self):
value = None
return value

def show_and_get_value(self):
result = self.ShowModal()
if result == wx.ID_OK:
value = self.get_int()
else:
value = None
self.Destroy()
return value
def prompt_for_string(parent, message, title, default=None):
if default is not None:
default = str(default)
else:
default = ""
d = SimplePromptDialog(parent, message, title, default)
return d.show_and_get_value()

def prompt_for_hex(parent, message, title, default=None):
if default is not None:
default = str(default)
else:
default = ""
d = HexEntryDialog(parent, message, title, default)
if default:
d.SetValue(default)
return d.show_and_get_value()

0 comments on commit b511833

Please sign in to comment.