tweestyler.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. from tweelexer import TweeLexer
  2. import wx, wx.stc
  3. class TweeStyler(TweeLexer):
  4. """Applies syntax highlighting for Twee syntax in a wx.StyledTextCtrl.
  5. This needs to be passed the control it will be lexing, so it can
  6. look up the body text as needed.
  7. """
  8. def __init__(self, control, frame):
  9. self.ctrl = control
  10. self.frame = frame
  11. self.app = frame.app
  12. self.ctrl.Bind(wx.stc.EVT_STC_STYLENEEDED, lambda event: self.lex())
  13. self.initStyles()
  14. def initStyles(self):
  15. """
  16. Initialize style definitions. This is automatically invoked when
  17. the constructor is called, but should be called any time font
  18. preferences are changed.
  19. """
  20. bodyFont = wx.Font(self.app.config.ReadInt('windowedFontSize'), wx.MODERN, wx.NORMAL, \
  21. wx.NORMAL, False, self.app.config.Read('windowedFontFace'))
  22. monoFont = wx.Font(self.app.config.ReadInt('monospaceFontSize'), wx.MODERN, wx.NORMAL, \
  23. wx.NORMAL, False, self.app.config.Read('monospaceFontFace'))
  24. self.ctrl.StyleSetFont(wx.stc.STC_STYLE_DEFAULT, bodyFont)
  25. self.ctrl.StyleClearAll()
  26. for i in self.STYLE_CONSTANTS:
  27. self.ctrl.StyleSetFont(i, bodyFont)
  28. # Styles 1-8 are BOLD, ITALIC, UNDERLINE, and bitwise combinations thereof
  29. for i in range(0,8):
  30. if i & 1:
  31. self.ctrl.StyleSetBold(i, True)
  32. if i & 2:
  33. self.ctrl.StyleSetItalic(i, True)
  34. if i & 4:
  35. self.ctrl.StyleSetUnderline(i, True)
  36. self.ctrl.StyleSetBold(self.GOOD_LINK, True)
  37. self.ctrl.StyleSetForeground(self.GOOD_LINK, self.GOOD_LINK_COLOR)
  38. self.ctrl.StyleSetBold(self.BAD_LINK, True)
  39. self.ctrl.StyleSetForeground(self.BAD_LINK, self.BAD_LINK_COLOR)
  40. self.ctrl.StyleSetBold(self.BAD_MACRO, True)
  41. self.ctrl.StyleSetForeground(self.BAD_MACRO, self.BAD_LINK_COLOR)
  42. self.ctrl.StyleSetBold(self.STORYINCLUDE_LINK, True)
  43. self.ctrl.StyleSetForeground(self.STORYINCLUDE_LINK, self.STORYINCLUDE_COLOR)
  44. self.ctrl.StyleSetForeground(self.MARKUP, self.MARKUP_COLOR)
  45. self.ctrl.StyleSetForeground(self.INLINE_STYLE, self.MARKUP_COLOR)
  46. self.ctrl.StyleSetBold(self.BAD_INLINE_STYLE, True)
  47. self.ctrl.StyleSetForeground(self.BAD_INLINE_STYLE, self.BAD_LINK_COLOR)
  48. self.ctrl.StyleSetBold(self.HTML, True)
  49. self.ctrl.StyleSetForeground(self.HTML, self.HTML_COLOR)
  50. self.ctrl.StyleSetForeground(self.HTML_BLOCK, self.HTML_COLOR)
  51. self.ctrl.StyleSetBold(self.MACRO, True)
  52. self.ctrl.StyleSetForeground(self.MACRO, self.MACRO_COLOR)
  53. self.ctrl.StyleSetItalic(self.COMMENT, True)
  54. self.ctrl.StyleSetForeground(self.COMMENT, self.COMMENT_COLOR)
  55. self.ctrl.StyleSetForeground(self.SILENT, self.COMMENT_COLOR)
  56. self.ctrl.StyleSetFont(self.MONO, monoFont)
  57. self.ctrl.StyleSetBold(self.EXTERNAL, True)
  58. self.ctrl.StyleSetForeground(self.EXTERNAL, self.EXTERNAL_COLOR)
  59. self.ctrl.StyleSetBold(self.IMAGE, True)
  60. self.ctrl.StyleSetForeground(self.IMAGE, self.IMAGE_COLOR)
  61. self.ctrl.StyleSetBold(self.PARAM, True)
  62. self.ctrl.StyleSetForeground(self.PARAM, self.PARAM_COLOR)
  63. self.ctrl.StyleSetBold(self.PARAM_VAR, True)
  64. self.ctrl.StyleSetForeground(self.PARAM_VAR, self.PARAM_VAR_COLOR)
  65. self.ctrl.StyleSetBold(self.PARAM_STR, True)
  66. self.ctrl.StyleSetForeground(self.PARAM_STR, self.PARAM_STR_COLOR)
  67. self.ctrl.StyleSetBold(self.PARAM_NUM, True)
  68. self.ctrl.StyleSetForeground(self.PARAM_NUM, self.PARAM_NUM_COLOR)
  69. self.ctrl.StyleSetBold(self.PARAM_BOOL, True)
  70. self.ctrl.StyleSetForeground(self.PARAM_BOOL, self.PARAM_BOOL_COLOR)
  71. def getText(self):
  72. return self.ctrl.GetTextUTF8()
  73. def passageExists(self, title):
  74. return self.frame.widget.parent.passageExists(title, False)
  75. def includedPassageExists(self, title):
  76. return self.frame.widget.parent.includedPassageExists(title)
  77. def applyStyle(self, start, end, style):
  78. self.ctrl.StartStyling(start, self.TEXT_STYLES)
  79. self.ctrl.SetStyling(end, style)
  80. def getHeader(self):
  81. return self.frame.getHeader()
  82. # style colors
  83. GOOD_LINK_COLOR = '#3333cc'
  84. EXTERNAL_COLOR = '#337acc'
  85. STORYINCLUDE_COLOR = '#906fe2'
  86. BAD_LINK_COLOR = '#cc3333'
  87. MARKUP_COLOR = '#008200'
  88. MACRO_COLOR = '#a94286'
  89. COMMENT_COLOR = '#868686'
  90. IMAGE_COLOR = '#088A85'
  91. HTML_COLOR = '#4d4d9d'
  92. # param colours
  93. PARAM_COLOR = '#7f456a'
  94. PARAM_VAR_COLOR = '#005682'
  95. PARAM_BOOL_COLOR = '#626262'
  96. PARAM_STR_COLOR = '#008282'
  97. PARAM_NUM_COLOR = '#A15000'
  98. TEXT_STYLES = 31 # mask for StartStyling() to indicate we're only changing text styles