Ahoj,
chtěl bych přeměnit jednu class ve wxPythonu tak, abych ji mohl použít v tab ve wx.Notebooku,
zatím je vytvořena tak, aby se použila v novém okně a to mi nevyhovuje. Díky za jakoukoliv radu. Tady je köd:
class MacroEditor(wx.Dialog):
"""Really simple editor to edit macro definitions"""
def __init__(self, macro_name, definition, callback, gcode = False):
self.indent_chars = " "
title = " macro %s"
if gcode:
title = " %s"
self.gcode = gcode
wx.Dialog.__init__(self, None, title = title % macro_name,
style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER)
self.callback = callback
self.panel = wx.Panel(self, -1)
titlesizer = wx.BoxSizer(wx.HORIZONTAL)
self.titletext = wx.StaticText(self.panel, -1, " _") # title%macro_name)
titlesizer.Add(self.titletext, 1)
self.findb = wx.Button(self.panel, -1, _("Find"), style = wx.BU_EXACTFIT) # New button for "Find" (Jezmy)
self.findb.Bind(wx.EVT_BUTTON, self.find)
self.okb = wx.Button(self.panel, -1, _("Save"), style = wx.BU_EXACTFIT)
self.okb.Bind(wx.EVT_BUTTON, self.save)
self.Bind(wx.EVT_CLOSE, self.close)
titlesizer.Add(self.findb)
titlesizer.Add(self.okb)
self.cancelb = wx.Button(self.panel, -1, _("Cancel"), style = wx.BU_EXACTFIT)
self.cancelb.Bind(wx.EVT_BUTTON, self.close)
titlesizer.Add(self.cancelb)
topsizer = wx.BoxSizer(wx.VERTICAL)
topsizer.Add(titlesizer, 0, wx.EXPAND)
self.e = wx.TextCtrl(self.panel, style = wx.HSCROLL | wx.TE_MULTILINE | wx.TE_RICH2, size = (400, 400))
if not self.gcode:
self.e.SetValue(self.unindent(definition))
else:
self.e.SetValue("\n".join(definition))
topsizer.Add(self.e, 1, wx.ALL | wx.EXPAND)
self.panel.SetSizer(topsizer)
topsizer.Layout()
topsizer.Fit(self)
self.Show()
self.e.SetFocus()
def find(self, ev):
# Ask user what to look for, find it and point at it ... (Jezmy)
S = self.e.GetStringSelection()
if not S:
S = "Z"
FindValue = wx.GetTextFromUser('Please enter a search string:', caption = "Search", default_value = S, parent = None)
somecode = self.e.GetValue()
position = somecode.find(FindValue, self.e.GetInsertionPoint())
if position == -1:
self.titletext.SetLabel(_("Not Found!"))
else:
self.titletext.SetLabel(str(position))
# ananswer = wx.MessageBox(str(numLines)+" Lines detected in file\n"+str(position), "OK")
self.e.SetFocus()
self.e.SetInsertionPoint(position)
self.e.SetSelection(position, position + len(FindValue))
self.e.ShowPosition(position)
def ShowMessage(self, ev, message):
dlg = wx.MessageDialog(self, message,
"Info!", wx.OK | wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
def save(self, ev):
self.Destroy()
if not self.gcode:
self.callback(self.reindent(self.e.GetValue()))
else:
self.callback(self.e.GetValue().split("\n"))
def close(self, ev):
self.Destroy()
def unindent(self, text):
self.indent_chars = text[:len(text) - len(text.lstrip())]
if len(self.indent_chars) == 0:
self.indent_chars = " "
unindented = ""
lines = re.split(r"(?:\r\n?|\n)", text)
if len(lines) <= 1:
return text
for line in lines:
if line.startswith(self.indent_chars):
unindented += line[len(self.indent_chars):] + "\n"
else:
unindented += line + "\n"
return unindented
def reindent(self, text):
lines = re.split(r"(?:\r\n?|\n)", text)
if len(lines) <= 1:
return text
reindented = ""
for line in lines:
if line.strip() != "":
reindented += self.indent_chars + line + "\n"
return reindented
SETTINGS_GROUPS = {"Printer": _("Printer settings"),
"UI": _("User interface"),
"Viewer": _("Viewer"),
"Colors": _("Colors"),
"External": _("External commands")}