chiark / gitweb /
Add proper copyright statements.
[cura.git] / Cura / gui / util / gcodeTextArea.py
1 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
2 from __future__ import absolute_import
3
4 import wx
5 import wx.stc
6 import sys
7
8 from Cura.util import profile
9
10 class GcodeTextArea(wx.stc.StyledTextCtrl):
11         def __init__(self, parent):
12                 super(GcodeTextArea, self).__init__(parent)
13
14                 self.SetLexer(wx.stc.STC_LEX_CONTAINER)
15                 self.Bind(wx.stc.EVT_STC_STYLENEEDED, self.OnStyle)
16         
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()
19                 self.SetStyleBits(5)
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)
30         
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]
35         
36         def OnMacKeyDown(self, e):
37                 code = e.GetKeyCode();
38                 stopPropagation = True
39                 #Command
40                 if e.CmdDown():
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:
46                                 self.GotoPos(0)
47                         elif code == wx._core.WXK_DOWN:
48                                 self.GotoPos(self.GetLength())
49                         else:
50                                 stopPropagation = False
51                 # Control
52                 elif e.GetModifiers() & 0xF0:
53                         if code == 65: # A
54                                 self.GotoLine(self.GetCurrentLine())
55                         elif code == 69: # E
56                                 self.GotoPos(self.GetLineEndPosition(self.GetCurrentLine()))
57                         else:
58                                 stopPropagation = False
59                 else:
60                         stopPropagation = False
61                 # Event propagation
62                 if stopPropagation:
63                         e.StopPropagation()
64                 else:
65                         e.Skip()
66         
67         def OnStyle(self, e):
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)
75                         if ';' in line:
76                                 pos = line.index(';')
77                                 self.StartStyling(start + pos, 31)
78                                 self.SetStyling(length - pos, 1)
79                                 length = pos
80                 
81                         pos = 0
82                         while pos < length:
83                                 if line[pos] in " \t\n\r":
84                                         while pos < length and line[pos] in " \t\n\r":
85                                                 pos += 1
86                                 else:
87                                         end = pos
88                                         while end < length and not line[end] in " \t\n\r":
89                                                 end += 1
90                                         if self.checkGCodePart(line[pos:end], start + pos):
91                                                 self.StartStyling(start + pos, 0x20)
92                                                 self.SetStyling(end - pos, 0x20)
93                                         pos = end
94                         lineNr += 1
95
96         def checkGCodePart(self, part, pos):
97                 if len(part) < 2:
98                         self.StartStyling(pos, 0x40)
99                         self.SetStyling(1, 0x40)
100                         return True
101                 if not part[0] in "GMXYZFESTBPIDCJ":
102                         self.StartStyling(pos, 0x40)
103                         self.SetStyling(1, 0x40)
104                         return True
105                 if part[1] == '{':
106                         if part[-1] != '}':
107                                 return True
108                         tag = part[2:-1]
109                         if not profile.isProfileSetting(tag) and not profile.isPreference(tag):
110                                 self.StartStyling(pos + 2, 0x40)
111                                 self.SetStyling(len(tag), 0x40)
112                                 return True
113                 elif part[0] in "GM":
114                         try:
115                                 code = int(part[1:])
116                         except (ValueError):
117                                 self.StartStyling(pos + 1, 0x40)
118                                 self.SetStyling(len(part) - 1, 0x40)
119                                 return True
120                         if part[0] == 'G':
121                                 if not code in self.supportedGCodes:
122                                         return True
123                         if part[0] == 'M':
124                                 if not code in self.supportedMCodes:
125                                         return True
126                 else:
127                         try:
128                                 float(part[1:])
129                         except (ValueError):
130                                 self.StartStyling(pos + 1, 0x40)
131                                 self.SetStyling(len(part) - 1, 0x40)
132                                 return True
133                 return False
134
135         def GetValue(self):
136                 return self.GetText()
137
138         def SetValue(self, s):
139                 self.SetText(s)
140