Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

configui.py 11KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import win32api
  2. import win32con
  3. import win32ui
  4. from pywin.mfc import dialog
  5. # Used to indicate that style should use default color
  6. from win32con import CLR_INVALID
  7. from . import scintillacon
  8. ######################################################
  9. # Property Page for syntax formatting options
  10. # The standard 16 color VGA palette should always be possible
  11. paletteVGA = (
  12. ("Black", win32api.RGB(0, 0, 0)),
  13. ("Navy", win32api.RGB(0, 0, 128)),
  14. ("Green", win32api.RGB(0, 128, 0)),
  15. ("Cyan", win32api.RGB(0, 128, 128)),
  16. ("Maroon", win32api.RGB(128, 0, 0)),
  17. ("Purple", win32api.RGB(128, 0, 128)),
  18. ("Olive", win32api.RGB(128, 128, 0)),
  19. ("Gray", win32api.RGB(128, 128, 128)),
  20. ("Silver", win32api.RGB(192, 192, 192)),
  21. ("Blue", win32api.RGB(0, 0, 255)),
  22. ("Lime", win32api.RGB(0, 255, 0)),
  23. ("Aqua", win32api.RGB(0, 255, 255)),
  24. ("Red", win32api.RGB(255, 0, 0)),
  25. ("Fuchsia", win32api.RGB(255, 0, 255)),
  26. ("Yellow", win32api.RGB(255, 255, 0)),
  27. ("White", win32api.RGB(255, 255, 255)),
  28. # and a few others will generally be possible.
  29. ("DarkGrey", win32api.RGB(64, 64, 64)),
  30. ("PurpleBlue", win32api.RGB(64, 64, 192)),
  31. ("DarkGreen", win32api.RGB(0, 96, 0)),
  32. ("DarkOlive", win32api.RGB(128, 128, 64)),
  33. ("MediumBlue", win32api.RGB(0, 0, 192)),
  34. ("DarkNavy", win32api.RGB(0, 0, 96)),
  35. ("Magenta", win32api.RGB(96, 0, 96)),
  36. ("OffWhite", win32api.RGB(255, 255, 220)),
  37. ("LightPurple", win32api.RGB(220, 220, 255)),
  38. ("<Default>", win32con.CLR_INVALID),
  39. )
  40. class ScintillaFormatPropertyPage(dialog.PropertyPage):
  41. def __init__(self, scintillaClass=None, caption=0):
  42. self.scintillaClass = scintillaClass
  43. dialog.PropertyPage.__init__(self, win32ui.IDD_PP_FORMAT, caption=caption)
  44. def OnInitDialog(self):
  45. try:
  46. if self.scintillaClass is None:
  47. from . import control
  48. sc = control.CScintillaEdit
  49. else:
  50. sc = self.scintillaClass
  51. self.scintilla = sc()
  52. style = win32con.WS_CHILD | win32con.WS_VISIBLE | win32con.ES_MULTILINE
  53. # Convert the rect size
  54. rect = self.MapDialogRect((5, 5, 120, 75))
  55. self.scintilla.CreateWindow(style, rect, self, 111)
  56. self.HookNotify(self.OnBraceMatch, scintillacon.SCN_CHECKBRACE)
  57. self.scintilla.HookKeyStroke(self.OnEsc, 27)
  58. self.scintilla.SCISetViewWS(1)
  59. self.pos_bstart = self.pos_bend = self.pos_bbad = 0
  60. colorizer = self.scintilla._GetColorizer()
  61. text = colorizer.GetSampleText()
  62. items = text.split("|", 2)
  63. pos = len(items[0])
  64. self.scintilla.SCIAddText("".join(items))
  65. self.scintilla.SetSel(pos, pos)
  66. self.scintilla.ApplyFormattingStyles()
  67. self.styles = self.scintilla._GetColorizer().styles
  68. self.cbo = self.GetDlgItem(win32ui.IDC_COMBO1)
  69. for c in paletteVGA:
  70. self.cbo.AddString(c[0])
  71. self.cboBoldItalic = self.GetDlgItem(win32ui.IDC_COMBO2)
  72. for item in ("Bold Italic", "Bold", "Italic", "Regular"):
  73. self.cboBoldItalic.InsertString(0, item)
  74. self.butIsDefault = self.GetDlgItem(win32ui.IDC_CHECK1)
  75. self.butIsDefaultBackground = self.GetDlgItem(win32ui.IDC_CHECK2)
  76. self.listbox = self.GetDlgItem(win32ui.IDC_LIST1)
  77. self.HookCommand(self.OnListCommand, win32ui.IDC_LIST1)
  78. names = list(self.styles.keys())
  79. names.sort()
  80. for name in names:
  81. if self.styles[name].aliased is None:
  82. self.listbox.AddString(name)
  83. self.listbox.SetCurSel(0)
  84. idc = win32ui.IDC_RADIO1
  85. if not self.scintilla._GetColorizer().bUseFixed:
  86. idc = win32ui.IDC_RADIO2
  87. self.GetDlgItem(idc).SetCheck(1)
  88. self.UpdateUIForStyle(self.styles[names[0]])
  89. self.scintilla.HookFormatter(self)
  90. self.HookCommand(self.OnButDefaultFixedFont, win32ui.IDC_BUTTON1)
  91. self.HookCommand(self.OnButDefaultPropFont, win32ui.IDC_BUTTON2)
  92. self.HookCommand(self.OnButThisFont, win32ui.IDC_BUTTON3)
  93. self.HookCommand(self.OnButUseDefaultFont, win32ui.IDC_CHECK1)
  94. self.HookCommand(self.OnButThisBackground, win32ui.IDC_BUTTON4)
  95. self.HookCommand(self.OnButUseDefaultBackground, win32ui.IDC_CHECK2)
  96. self.HookCommand(self.OnStyleUIChanged, win32ui.IDC_COMBO1)
  97. self.HookCommand(self.OnStyleUIChanged, win32ui.IDC_COMBO2)
  98. self.HookCommand(self.OnButFixedOrDefault, win32ui.IDC_RADIO1)
  99. self.HookCommand(self.OnButFixedOrDefault, win32ui.IDC_RADIO2)
  100. except:
  101. import traceback
  102. traceback.print_exc()
  103. def OnEsc(self, ch):
  104. self.GetParent().EndDialog(win32con.IDCANCEL)
  105. def OnBraceMatch(self, std, extra):
  106. import pywin.scintilla.view
  107. pywin.scintilla.view.DoBraceMatch(self.scintilla)
  108. def GetSelectedStyle(self):
  109. return self.styles[self.listbox.GetText(self.listbox.GetCurSel())]
  110. def _DoButDefaultFont(self, extra_flags, attr):
  111. baseFormat = getattr(self.scintilla._GetColorizer(), attr)
  112. flags = (
  113. extra_flags
  114. | win32con.CF_SCREENFONTS
  115. | win32con.CF_EFFECTS
  116. | win32con.CF_FORCEFONTEXIST
  117. )
  118. d = win32ui.CreateFontDialog(baseFormat, flags, None, self)
  119. if d.DoModal() == win32con.IDOK:
  120. setattr(self.scintilla._GetColorizer(), attr, d.GetCharFormat())
  121. self.OnStyleUIChanged(0, win32con.BN_CLICKED)
  122. def OnButDefaultFixedFont(self, id, code):
  123. if code == win32con.BN_CLICKED:
  124. self._DoButDefaultFont(win32con.CF_FIXEDPITCHONLY, "baseFormatFixed")
  125. return 1
  126. def OnButDefaultPropFont(self, id, code):
  127. if code == win32con.BN_CLICKED:
  128. self._DoButDefaultFont(win32con.CF_SCALABLEONLY, "baseFormatProp")
  129. return 1
  130. def OnButFixedOrDefault(self, id, code):
  131. if code == win32con.BN_CLICKED:
  132. bUseFixed = id == win32ui.IDC_RADIO1
  133. self.GetDlgItem(win32ui.IDC_RADIO1).GetCheck() != 0
  134. self.scintilla._GetColorizer().bUseFixed = bUseFixed
  135. self.scintilla.ApplyFormattingStyles(0)
  136. return 1
  137. def OnButThisFont(self, id, code):
  138. if code == win32con.BN_CLICKED:
  139. flags = (
  140. win32con.CF_SCREENFONTS
  141. | win32con.CF_EFFECTS
  142. | win32con.CF_FORCEFONTEXIST
  143. )
  144. style = self.GetSelectedStyle()
  145. # If the selected style is based on the default, we need to apply
  146. # the default to it.
  147. def_format = self.scintilla._GetColorizer().GetDefaultFormat()
  148. format = style.GetCompleteFormat(def_format)
  149. d = win32ui.CreateFontDialog(format, flags, None, self)
  150. if d.DoModal() == win32con.IDOK:
  151. style.format = d.GetCharFormat()
  152. self.scintilla.ApplyFormattingStyles(0)
  153. return 1
  154. def OnButUseDefaultFont(self, id, code):
  155. if code == win32con.BN_CLICKED:
  156. isDef = self.butIsDefault.GetCheck()
  157. self.GetDlgItem(win32ui.IDC_BUTTON3).EnableWindow(not isDef)
  158. if isDef: # Being reset to the default font.
  159. style = self.GetSelectedStyle()
  160. style.ForceAgainstDefault()
  161. self.UpdateUIForStyle(style)
  162. self.scintilla.ApplyFormattingStyles(0)
  163. else:
  164. # User wants to override default -
  165. # do nothing!
  166. pass
  167. def OnButThisBackground(self, id, code):
  168. if code == win32con.BN_CLICKED:
  169. style = self.GetSelectedStyle()
  170. bg = win32api.RGB(0xFF, 0xFF, 0xFF)
  171. if style.background != CLR_INVALID:
  172. bg = style.background
  173. d = win32ui.CreateColorDialog(bg, 0, self)
  174. if d.DoModal() == win32con.IDOK:
  175. style.background = d.GetColor()
  176. self.scintilla.ApplyFormattingStyles(0)
  177. return 1
  178. def OnButUseDefaultBackground(self, id, code):
  179. if code == win32con.BN_CLICKED:
  180. isDef = self.butIsDefaultBackground.GetCheck()
  181. self.GetDlgItem(win32ui.IDC_BUTTON4).EnableWindow(not isDef)
  182. if isDef: # Being reset to the default color
  183. style = self.GetSelectedStyle()
  184. style.background = style.default_background
  185. self.UpdateUIForStyle(style)
  186. self.scintilla.ApplyFormattingStyles(0)
  187. else:
  188. # User wants to override default -
  189. # do nothing!
  190. pass
  191. def OnListCommand(self, id, code):
  192. if code == win32con.LBN_SELCHANGE:
  193. style = self.GetSelectedStyle()
  194. self.UpdateUIForStyle(style)
  195. return 1
  196. def UpdateUIForStyle(self, style):
  197. format = style.format
  198. sel = 0
  199. for c in paletteVGA:
  200. if format[4] == c[1]:
  201. # print "Style", style.name, "is", c[0]
  202. break
  203. sel = sel + 1
  204. else:
  205. sel = -1
  206. self.cbo.SetCurSel(sel)
  207. self.butIsDefault.SetCheck(style.IsBasedOnDefault())
  208. self.GetDlgItem(win32ui.IDC_BUTTON3).EnableWindow(not style.IsBasedOnDefault())
  209. self.butIsDefaultBackground.SetCheck(
  210. style.background == style.default_background
  211. )
  212. self.GetDlgItem(win32ui.IDC_BUTTON4).EnableWindow(
  213. style.background != style.default_background
  214. )
  215. bold = format[1] & win32con.CFE_BOLD != 0
  216. italic = format[1] & win32con.CFE_ITALIC != 0
  217. self.cboBoldItalic.SetCurSel(bold * 2 + italic)
  218. def OnStyleUIChanged(self, id, code):
  219. if code in [win32con.BN_CLICKED, win32con.CBN_SELCHANGE]:
  220. style = self.GetSelectedStyle()
  221. self.ApplyUIFormatToStyle(style)
  222. self.scintilla.ApplyFormattingStyles(0)
  223. return 0
  224. return 1
  225. def ApplyUIFormatToStyle(self, style):
  226. format = style.format
  227. color = paletteVGA[self.cbo.GetCurSel()]
  228. effect = 0
  229. sel = self.cboBoldItalic.GetCurSel()
  230. if sel == 0:
  231. effect = 0
  232. elif sel == 1:
  233. effect = win32con.CFE_ITALIC
  234. elif sel == 2:
  235. effect = win32con.CFE_BOLD
  236. else:
  237. effect = win32con.CFE_BOLD | win32con.CFE_ITALIC
  238. maskFlags = (
  239. format[0] | win32con.CFM_COLOR | win32con.CFM_BOLD | win32con.CFM_ITALIC
  240. )
  241. style.format = (
  242. maskFlags,
  243. effect,
  244. style.format[2],
  245. style.format[3],
  246. color[1],
  247. ) + style.format[5:]
  248. def OnOK(self):
  249. self.scintilla._GetColorizer().SavePreferences()
  250. return 1
  251. def test():
  252. page = ColorEditorPropertyPage()
  253. sheet = pywin.mfc.dialog.PropertySheet("Test")
  254. sheet.AddPage(page)
  255. sheet.CreateWindow()