1 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
2 from __future__ import absolute_import
8 from Cura.util import profile
10 class GcodeTextArea(wx.stc.StyledTextCtrl):
11 def __init__(self, parent):
12 super(GcodeTextArea, self).__init__(parent)
14 self.SetLexer(wx.stc.STC_LEX_CONTAINER)
15 self.Bind(wx.stc.EVT_STC_STYLENEEDED, self.OnStyle)
17 fontSize = wx.SystemSettings.GetFont(wx.SYS_ANSI_VAR_FONT).GetPointSize()
18 fontName = wx.Font(wx.SystemSettings.GetFont(wx.SYS_ANSI_VAR_FONT).GetPointSize(), wx.FONTFAMILY_MODERN, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL).GetFaceName()
20 self.StyleSetSpec(0, "face:%s,size:%d" % (fontName, fontSize))
21 self.StyleSetSpec(1, "fore:#006000,face:%s,size:%d" % (fontName, fontSize))
22 self.IndicatorSetStyle(0, wx.stc.STC_INDIC_TT)
23 self.IndicatorSetForeground(0, "#0000FF")
24 self.IndicatorSetStyle(1, wx.stc.STC_INDIC_SQUIGGLE)
25 self.IndicatorSetForeground(1, "#FF0000")
26 self.SetWrapMode(wx.stc.STC_WRAP_NONE)
27 self.SetScrollWidth(1000)
28 if sys.platform == 'darwin':
29 self.Bind(wx.EVT_KEY_DOWN, self.OnMacKeyDown)
31 #GCodes and MCodes as supported by Marlin
32 #GCode 21 is not really supported by Marlin, but we still do not report it as error as it's often used.
33 self.supportedGCodes = [0,1,2,3,4,21,28,90,91,92]
34 self.supportedMCodes = [17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,42,80,81,82,83,84,85,92,104,105,106,107,109,114,115,117,119,140,190,201,202,203,204,205,206,220,221,240,301,302,303,400,500,501,502,503,999]
36 def OnMacKeyDown(self, e):
37 code = e.GetKeyCode();
38 stopPropagation = True
41 if code == wx._core.WXK_LEFT:
42 self.GotoLine(self.GetCurrentLine())
43 elif code == wx._core.WXK_RIGHT:
44 self.GotoPos(self.GetLineEndPosition(self.GetCurrentLine()))
45 elif code == wx._core.WXK_UP:
47 elif code == wx._core.WXK_DOWN:
48 self.GotoPos(self.GetLength())
50 stopPropagation = False
52 elif e.GetModifiers() & 0xF0:
54 self.GotoLine(self.GetCurrentLine())
56 self.GotoPos(self.GetLineEndPosition(self.GetCurrentLine()))
58 stopPropagation = False
60 stopPropagation = False
68 lineNr = self.LineFromPosition(self.GetEndStyled())
69 while self.PositionFromLine(lineNr) > -1:
70 line = self.GetLine(lineNr)
71 start = self.PositionFromLine(lineNr)
72 length = self.LineLength(lineNr)
73 self.StartStyling(start, 255)
74 self.SetStyling(length, 0)
77 self.StartStyling(start + pos, 31)
78 self.SetStyling(length - pos, 1)
83 if line[pos] in " \t\n\r":
84 while pos < length and line[pos] in " \t\n\r":
88 while end < length and not line[end] in " \t\n\r":
90 if self.checkGCodePart(line[pos:end], start + pos):
91 self.StartStyling(start + pos, 0x20)
92 self.SetStyling(end - pos, 0x20)
96 def checkGCodePart(self, part, pos):
98 self.StartStyling(pos, 0x40)
99 self.SetStyling(1, 0x40)
101 if not part[0] in "GMXYZFESTBPIDCJ":
102 self.StartStyling(pos, 0x40)
103 self.SetStyling(1, 0x40)
109 if not profile.isProfileSetting(tag) and not profile.isPreference(tag):
110 self.StartStyling(pos + 2, 0x40)
111 self.SetStyling(len(tag), 0x40)
113 elif part[0] in "GM":
117 self.StartStyling(pos + 1, 0x40)
118 self.SetStyling(len(part) - 1, 0x40)
121 if not code in self.supportedGCodes:
124 if not code in self.supportedMCodes:
130 self.StartStyling(pos + 1, 0x40)
131 self.SetStyling(len(part) - 1, 0x40)
136 return self.GetText()
138 def SetValue(self, s):