PageRenderTime 61ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/trelby.py

https://github.com/alopex94/trelby
Python | 2694 lines | 2633 code | 50 blank | 11 comment | 17 complexity | 2253f888f34b03267a6d464885926b7a MD5 | raw file
Possible License(s): GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. # -*- coding: iso-8859-1 -*-
  2. from error import *
  3. import autocompletiondlg
  4. import cfgdlg
  5. import characterreport
  6. import charmapdlg
  7. import commandsdlg
  8. import config
  9. import dialoguechart
  10. import finddlg
  11. import gutil
  12. import headersdlg
  13. import locationreport
  14. import locationsdlg
  15. import misc
  16. import myimport
  17. import mypickle
  18. import namesdlg
  19. import opts
  20. import pml
  21. import scenereport
  22. import scriptreport
  23. import screenplay
  24. import spellcheck
  25. import spellcheckdlg
  26. import spellcheckcfgdlg
  27. import splash
  28. import titlesdlg
  29. import util
  30. import viewmode
  31. import watermarkdlg
  32. import copy
  33. import datetime
  34. import os
  35. import os.path
  36. import signal
  37. import sys
  38. import time
  39. import wx
  40. from functools import partial
  41. #keycodes
  42. KC_CTRL_A = 1
  43. KC_CTRL_B = 2
  44. KC_CTRL_D = 4
  45. KC_CTRL_E = 5
  46. KC_CTRL_F = 6
  47. KC_CTRL_N = 14
  48. KC_CTRL_P = 16
  49. KC_CTRL_V = 22
  50. VIEWMODE_DRAFT,\
  51. VIEWMODE_LAYOUT,\
  52. VIEWMODE_SIDE_BY_SIDE,\
  53. VIEWMODE_OVERVIEW_SMALL,\
  54. VIEWMODE_OVERVIEW_LARGE,\
  55. = range(5)
  56. def refreshGuiConfig():
  57. global cfgGui
  58. cfgGui = config.ConfigGui(cfgGl)
  59. def getCfgGui():
  60. return cfgGui
  61. # keeps (some) global data
  62. class GlobalData:
  63. def __init__(self):
  64. self.confFilename = misc.confPath + "/default.conf"
  65. self.stateFilename = misc.confPath + "/state"
  66. self.scDictFilename = misc.confPath + "/spell_checker_dictionary"
  67. # current script config path
  68. self.scriptSettingsPath = misc.confPath
  69. # global spell checker (user) dictionary
  70. self.scDict = spellcheck.Dict()
  71. # recently used files list
  72. self.mru = misc.MRUFiles(5)
  73. if opts.conf:
  74. self.confFilename = opts.conf
  75. v = self.cvars = mypickle.Vars()
  76. v.addInt("posX", 0, "PositionX", -20, 9999)
  77. v.addInt("posY", 0, "PositionY", -20, 9999)
  78. # linux has bigger font by default so it needs a wider window
  79. defaultW = 750
  80. if misc.isUnix:
  81. defaultW = 800
  82. v.addInt("width", defaultW, "Width", 500, 9999)
  83. v.addInt("height", 830, "Height", 300, 9999)
  84. v.addInt("viewMode", VIEWMODE_DRAFT, "ViewMode", VIEWMODE_DRAFT,
  85. VIEWMODE_OVERVIEW_LARGE)
  86. v.addList("files", [], "Files",
  87. mypickle.StrUnicodeVar("", u"", ""))
  88. v.makeDicts()
  89. v.setDefaults(self)
  90. self.height = min(self.height,
  91. wx.SystemSettings_GetMetric(wx.SYS_SCREEN_Y) - 50)
  92. self.vmDraft = viewmode.ViewModeDraft()
  93. self.vmLayout = viewmode.ViewModeLayout()
  94. self.vmSideBySide = viewmode.ViewModeSideBySide()
  95. self.vmOverviewSmall = viewmode.ViewModeOverview(1)
  96. self.vmOverviewLarge = viewmode.ViewModeOverview(2)
  97. self.setViewMode(self.viewMode)
  98. self.makeConfDir()
  99. def makeConfDir(self):
  100. makeDir = not util.fileExists(misc.confPath)
  101. if makeDir:
  102. try:
  103. os.mkdir(misc.toPath(misc.confPath), 0755)
  104. except OSError, (errno, strerror):
  105. wx.MessageBox("Error creating configuration directory\n"
  106. "'%s': %s" % (misc.confPath, strerror),
  107. "Error", wx.OK, None)
  108. # set viewmode, the parameter is one of the VIEWMODE_ defines.
  109. def setViewMode(self, viewMode):
  110. self.viewMode = viewMode
  111. if viewMode == VIEWMODE_DRAFT:
  112. self.vm = self.vmDraft
  113. elif viewMode == VIEWMODE_LAYOUT:
  114. self.vm = self.vmLayout
  115. elif viewMode == VIEWMODE_SIDE_BY_SIDE:
  116. self.vm = self.vmSideBySide
  117. elif viewMode == VIEWMODE_OVERVIEW_SMALL:
  118. self.vm = self.vmOverviewSmall
  119. else:
  120. self.vm = self.vmOverviewLarge
  121. # load from string 's'. does not throw any exceptions and silently
  122. # ignores any errors.
  123. def load(self, s):
  124. self.cvars.load(self.cvars.makeVals(s), "", self)
  125. self.mru.items = self.files
  126. # save to a string and return that.
  127. def save(self):
  128. self.files = self.mru.items
  129. return self.cvars.save("", self)
  130. # save global spell checker dictionary to disk
  131. def saveScDict(self):
  132. util.writeToFile(self.scDictFilename, self.scDict.save(), mainFrame)
  133. class MyPanel(wx.Panel):
  134. def __init__(self, parent, id):
  135. wx.Panel.__init__(
  136. self, parent, id,
  137. # wxMSW/Windows does not seem to support
  138. # wx.NO_BORDER, which sucks
  139. style = wx.WANTS_CHARS | wx.NO_BORDER)
  140. hsizer = wx.BoxSizer(wx.HORIZONTAL)
  141. self.scrollBar = wx.ScrollBar(self, -1, style = wx.SB_VERTICAL)
  142. self.ctrl = MyCtrl(self, -1)
  143. hsizer.Add(self.ctrl, 1, wx.EXPAND)
  144. hsizer.Add(self.scrollBar, 0, wx.EXPAND)
  145. wx.EVT_COMMAND_SCROLL(self, self.scrollBar.GetId(),
  146. self.ctrl.OnScroll)
  147. wx.EVT_SET_FOCUS(self.scrollBar, self.OnScrollbarFocus)
  148. self.SetSizer(hsizer)
  149. # we never want the scrollbar to get the keyboard focus, pass it on to
  150. # the main widget
  151. def OnScrollbarFocus(self, event):
  152. self.ctrl.SetFocus()
  153. class MyCtrl(wx.Control):
  154. def __init__(self, parent, id):
  155. style = wx.WANTS_CHARS | wx.FULL_REPAINT_ON_RESIZE | wx.NO_BORDER
  156. wx.Control.__init__(self, parent, id, style = style)
  157. self.panel = parent
  158. wx.EVT_SIZE(self, self.OnSize)
  159. wx.EVT_PAINT(self, self.OnPaint)
  160. wx.EVT_ERASE_BACKGROUND(self, self.OnEraseBackground)
  161. wx.EVT_LEFT_DOWN(self, self.OnLeftDown)
  162. wx.EVT_LEFT_UP(self, self.OnLeftUp)
  163. wx.EVT_LEFT_DCLICK(self, self.OnLeftDown)
  164. wx.EVT_RIGHT_DOWN(self, self.OnRightDown)
  165. wx.EVT_MOTION(self, self.OnMotion)
  166. wx.EVT_MOUSEWHEEL(self, self.OnMouseWheel)
  167. wx.EVT_CHAR(self, self.OnKeyChar)
  168. self.createEmptySp()
  169. self.updateScreen(redraw = False)
  170. def OnChangeType(self, event):
  171. cs = screenplay.CommandState()
  172. lt = idToLTMap[event.GetId()]
  173. self.sp.convertTypeTo(lt, True)
  174. self.sp.cmdPost(cs)
  175. if cs.needsVisifying:
  176. self.makeLineVisible(self.sp.line)
  177. self.updateScreen()
  178. def clearVars(self):
  179. self.mouseSelectActive = False
  180. # find dialog stored settings
  181. self.findDlgFindText = ""
  182. self.findDlgReplaceText = ""
  183. self.findDlgMatchWholeWord= False
  184. self.findDlgMatchCase = False
  185. self.findDlgDirUp = False
  186. self.findDlgUseExtra = False
  187. self.findDlgElements = None
  188. def createEmptySp(self):
  189. self.clearVars()
  190. self.sp = screenplay.Screenplay(cfgGl)
  191. self.sp.titles.addDefaults()
  192. self.sp.headers.addDefaults()
  193. self.setFile(None)
  194. self.refreshCache()
  195. # update stuff that depends on configuration / view mode etc.
  196. def refreshCache(self):
  197. self.chX = util.getTextWidth(" ", pml.COURIER, self.sp.cfg.fontSize)
  198. self.chY = util.getTextHeight(self.sp.cfg.fontSize)
  199. self.pageW = gd.vm.getPageWidth(self)
  200. # conversion factor from mm to pixels
  201. self.mm2p = self.pageW / self.sp.cfg.paperWidth
  202. # page width and height on screen, in pixels
  203. self.pageW = int(self.pageW)
  204. self.pageH = int(self.mm2p * self.sp.cfg.paperHeight)
  205. def getCfgGui(self):
  206. return cfgGui
  207. def loadFile(self, fileName):
  208. s = util.loadFile(fileName, mainFrame)
  209. if s == None:
  210. return
  211. try:
  212. (sp, msg) = screenplay.Screenplay.load(s, cfgGl)
  213. except TrelbyError, e:
  214. wx.MessageBox("Error loading file:\n\n%s" % e, "Error",
  215. wx.OK, mainFrame)
  216. return
  217. if msg:
  218. misc.showText(mainFrame, msg, "Warning")
  219. self.clearVars()
  220. self.sp = sp
  221. self.setFile(fileName)
  222. self.refreshCache()
  223. # saved cursor position might be anywhere, so we can't just
  224. # display the first page
  225. self.makeLineVisible(self.sp.line)
  226. # save script to given filename. returns True on success.
  227. def saveFile(self, fileName):
  228. fileName = util.ensureEndsIn(fileName, ".trelby")
  229. if util.writeToFile(fileName, self.sp.save(), mainFrame):
  230. self.setFile(fileName)
  231. self.sp.markChanged(False)
  232. gd.mru.add(fileName)
  233. return True
  234. else:
  235. return False
  236. def importFile(self, fileName):
  237. if fileName.endswith("fdx"):
  238. lines = myimport.importFDX(fileName, mainFrame)
  239. elif fileName.endswith("celtx"):
  240. lines = myimport.importCeltx(fileName, mainFrame)
  241. elif fileName.endswith("astx"):
  242. lines = myimport.importAstx(fileName, mainFrame)
  243. elif fileName.endswith("fountain"):
  244. lines = myimport.importFountain(fileName, mainFrame)
  245. elif fileName.endswith("fadein"):
  246. lines = myimport.importFadein(fileName, mainFrame)
  247. else:
  248. lines = myimport.importTextFile(fileName, mainFrame)
  249. if not lines:
  250. return
  251. self.createEmptySp()
  252. self.sp.lines = lines
  253. self.sp.reformatAll()
  254. self.sp.paginate()
  255. self.sp.markChanged(True)
  256. # generate exportable text from given screenplay, or None.
  257. def getExportText(self, sp):
  258. inf = []
  259. inf.append(misc.CheckBoxItem("Include page markers"))
  260. dlg = misc.CheckBoxDlg(mainFrame, "Output options", inf,
  261. "Options:", False)
  262. if dlg.ShowModal() != wx.ID_OK:
  263. dlg.Destroy()
  264. return None
  265. return sp.generateText(inf[0].selected)
  266. def getExportHtml(self, sp):
  267. inf = []
  268. inf.append(misc.CheckBoxItem("Include Notes"))
  269. dlg = misc.CheckBoxDlg(mainFrame, "Output options", inf,
  270. "Options:", False)
  271. if dlg.ShowModal() != wx.ID_OK:
  272. dlg.Destroy()
  273. return None
  274. return sp.generateHtml(inf[0].selected)
  275. def setFile(self, fileName):
  276. self.fileName = fileName
  277. if fileName:
  278. self.setDisplayName(os.path.basename(fileName))
  279. else:
  280. self.setDisplayName(u"untitled")
  281. self.setTabText()
  282. mainFrame.setTitle(self.fileNameDisplay)
  283. def setDisplayName(self, name):
  284. i = 1
  285. while 1:
  286. if i == 1:
  287. tmp = name
  288. else:
  289. tmp = name + "-%d" % i
  290. matched = False
  291. for c in mainFrame.getCtrls():
  292. if c == self:
  293. continue
  294. if c.fileNameDisplay == tmp:
  295. matched = True
  296. break
  297. if not matched:
  298. break
  299. i += 1
  300. self.fileNameDisplay = tmp
  301. def setTabText(self):
  302. mainFrame.setTabText(self.panel, self.fileNameDisplay)
  303. # texts = gd.vm.getScreen(self, False)[0], or None, in which case it's
  304. # called in this function.
  305. def isLineVisible(self, line, texts = None):
  306. if texts == None:
  307. texts = gd.vm.getScreen(self, False)[0]
  308. # paranoia never hurts
  309. if len(texts) == 0:
  310. return False
  311. return (line >= texts[0].line) and (line <= texts[-1].line)
  312. def makeLineVisible(self, line, direction = config.SCROLL_CENTER):
  313. texts = gd.vm.getScreen(self, False)[0]
  314. if self.isLineVisible(line, texts):
  315. return
  316. gd.vm.makeLineVisible(self, line, texts, direction)
  317. def adjustScrollBar(self):
  318. height = self.GetClientSize().height
  319. # rough approximation of how many lines fit onto the screen.
  320. # accuracy is not that important for this, so we don't even care
  321. # about draft / layout mode differences.
  322. approx = int(((height / self.mm2p) / self.chY) / 1.3)
  323. self.panel.scrollBar.SetScrollbar(self.sp.getTopLine(), approx,
  324. len(self.sp.lines) + approx - 1, approx)
  325. def clearAutoComp(self):
  326. if self.sp.clearAutoComp():
  327. self.Refresh(False)
  328. # returns true if there are no contents at all and we're not
  329. # attached to any file
  330. def isUntouched(self):
  331. if self.fileName or (len(self.sp.lines) > 1) or \
  332. (len(self.sp.lines[0].text) > 0):
  333. return False
  334. else:
  335. return True
  336. def updateScreen(self, redraw = True, setCommon = True):
  337. self.adjustScrollBar()
  338. if setCommon:
  339. self.updateCommon()
  340. if redraw:
  341. self.Refresh(False)
  342. # update GUI elements shared by all scripts, like statusbar etc
  343. def updateCommon(self):
  344. cur = cfgGl.getType(self.sp.lines[self.sp.line].lt)
  345. if self.sp.tabMakesNew():
  346. tabNext = "%s" % cfgGl.getType(cur.newTypeTab).ti.name
  347. else:
  348. tabNext = "%s" % cfgGl.getType(cur.nextTypeTab).ti.name
  349. enterNext = cfgGl.getType(cur.newTypeEnter).ti.name
  350. page = self.sp.line2page(self.sp.line)
  351. pageCnt = self.sp.line2page(len(self.sp.lines) - 1)
  352. mainFrame.statusCtrl.SetValues(page, pageCnt, cur.ti.name, tabNext, enterNext)
  353. canUndo = self.sp.canUndo()
  354. canRedo = self.sp.canRedo()
  355. mainFrame.menuBar.Enable(ID_EDIT_UNDO, canUndo)
  356. mainFrame.menuBar.Enable(ID_EDIT_REDO, canRedo)
  357. mainFrame.toolBar.EnableTool(ID_EDIT_UNDO, canUndo)
  358. mainFrame.toolBar.EnableTool(ID_EDIT_REDO, canRedo)
  359. # apply per-script config
  360. def applyCfg(self, newCfg):
  361. self.sp.applyCfg(newCfg)
  362. self.refreshCache()
  363. self.makeLineVisible(self.sp.line)
  364. self.updateScreen()
  365. # apply global config
  366. def applyGlobalCfg(self, newCfgGl, writeCfg = True):
  367. global cfgGl
  368. oldCfgGl = cfgGl
  369. cfgGl = copy.deepcopy(newCfgGl)
  370. # if user has ventured from the old default directory, keep it as
  371. # the current one, otherwise set the new default as current.
  372. if misc.scriptDir == oldCfgGl.scriptDir:
  373. misc.scriptDir = cfgGl.scriptDir
  374. cfgGl.recalc()
  375. refreshGuiConfig()
  376. mainFrame.updateKbdCommands()
  377. for c in mainFrame.getCtrls():
  378. c.sp.cfgGl = cfgGl
  379. c.refreshCache()
  380. c.makeLineVisible(c.sp.line)
  381. c.adjustScrollBar()
  382. self.updateScreen()
  383. # in case tab colors have been changed
  384. mainFrame.tabCtrl.Refresh(False)
  385. mainFrame.statusCtrl.Refresh(False)
  386. mainFrame.noFSBtn.Refresh(False)
  387. mainFrame.toolBar.SetBackgroundColour(cfgGui.tabBarBgColor)
  388. if writeCfg:
  389. util.writeToFile(gd.confFilename, cfgGl.save(), mainFrame)
  390. mainFrame.checkFonts()
  391. def applyHeaders(self, newHeaders):
  392. self.sp.headers = newHeaders
  393. self.sp.markChanged()
  394. self.OnPaginate()
  395. # return an exportable, paginated Screenplay object, or None if for
  396. # some reason that's not possible / wanted. 'action' is the name of
  397. # the action, e.g. "export" or "print", that'll be done to the script,
  398. # and is used in dialogue with the user if needed.
  399. def getExportable(self, action):
  400. if cfgGl.checkOnExport:
  401. line = self.sp.findError(0)[0]
  402. if line != -1:
  403. if wx.MessageBox(
  404. "The script seems to contain errors.\n"
  405. "Are you sure you want to %s it?" % action, "Confirm",
  406. wx.YES_NO | wx.NO_DEFAULT, mainFrame) == wx.NO:
  407. return None
  408. sp = self.sp
  409. if sp.cfg.pdfRemoveNotes:
  410. sp = copy.deepcopy(self.sp)
  411. sp.removeElementTypes({screenplay.NOTE : None}, False)
  412. sp.paginate()
  413. return sp
  414. def OnEraseBackground(self, event):
  415. pass
  416. def OnSize(self, event):
  417. if misc.doDblBuf:
  418. size = self.GetClientSize()
  419. sb = wx.EmptyBitmap(size.width, size.height)
  420. old = getattr(self.__class__, "screenBuf", None)
  421. if (old == None) or (old.GetDepth() != sb.GetDepth()) or \
  422. (old.GetHeight() != sb.GetHeight()) or \
  423. (old.GetWidth() != sb.GetWidth()):
  424. self.__class__.screenBuf = sb
  425. self.makeLineVisible(self.sp.line)
  426. def OnLeftDown(self, event, mark = False):
  427. if not self.mouseSelectActive:
  428. self.sp.clearMark()
  429. self.updateScreen()
  430. pos = event.GetPosition()
  431. line, col = gd.vm.pos2linecol(self, pos.x, pos.y)
  432. self.mouseSelectActive = True
  433. if line is not None:
  434. self.sp.gotoPos(line, col, mark)
  435. self.updateScreen()
  436. def OnLeftUp(self, event):
  437. self.mouseSelectActive = False
  438. # to avoid phantom selections (Windows sends some strange events
  439. # sometimes), check if anything worthwhile is actually selected.
  440. cd = self.sp.getSelectedAsCD(False)
  441. if not cd or ((len(cd.lines) == 1) and (len(cd.lines[0].text) < 2)):
  442. self.sp.clearMark()
  443. def OnMotion(self, event):
  444. if event.LeftIsDown():
  445. self.OnLeftDown(event, mark = True)
  446. def OnRightDown(self, event):
  447. # No popup in the overview modes.
  448. if gd.viewMode in (VIEWMODE_OVERVIEW_SMALL, VIEWMODE_OVERVIEW_LARGE):
  449. return
  450. pos = event.GetPosition()
  451. line, col = gd.vm.pos2linecol(self, pos.x, pos.y)
  452. if self.sp.mark:
  453. m = mainFrame.rightClickMenuWithCut
  454. else:
  455. m = mainFrame.rightClickMenu
  456. if line is not None and (line != self.sp.line):
  457. self.sp.gotoPos(line, col, False)
  458. self.updateScreen()
  459. self.PopupMenu(m)
  460. def OnMouseWheel(self, event):
  461. if event.GetWheelRotation() > 0:
  462. delta = -cfgGl.mouseWheelLines
  463. else:
  464. delta = cfgGl.mouseWheelLines
  465. self.sp.setTopLine(self.sp.getTopLine() + delta)
  466. self.updateScreen()
  467. def OnScroll(self, event):
  468. pos = self.panel.scrollBar.GetThumbPosition()
  469. self.sp.setTopLine(pos)
  470. self.sp.clearAutoComp()
  471. self.updateScreen()
  472. def OnPaginate(self):
  473. self.sp.paginate()
  474. self.makeLineVisible(self.sp.line)
  475. self.updateScreen()
  476. def OnAutoCompletionDlg(self):
  477. dlg = autocompletiondlg.AutoCompletionDlg(mainFrame,
  478. copy.deepcopy(self.sp.autoCompletion))
  479. if dlg.ShowModal() == wx.ID_OK:
  480. self.sp.autoCompletion = dlg.autoCompletion
  481. self.sp.markChanged()
  482. dlg.Destroy()
  483. def OnTitlesDlg(self):
  484. dlg = titlesdlg.TitlesDlg(mainFrame, copy.deepcopy(self.sp.titles),
  485. self.sp.cfg, cfgGl)
  486. if dlg.ShowModal() == wx.ID_OK:
  487. self.sp.titles = dlg.titles
  488. self.sp.markChanged()
  489. dlg.Destroy()
  490. def OnHeadersDlg(self):
  491. dlg = headersdlg.HeadersDlg(mainFrame,
  492. copy.deepcopy(self.sp.headers), self.sp.cfg, cfgGl,
  493. self.applyHeaders)
  494. if dlg.ShowModal() == wx.ID_OK:
  495. self.applyHeaders(dlg.headers)
  496. dlg.Destroy()
  497. def OnLocationsDlg(self):
  498. dlg = locationsdlg.LocationsDlg(mainFrame, copy.deepcopy(self.sp))
  499. if dlg.ShowModal() == wx.ID_OK:
  500. self.sp.locations = dlg.sp.locations
  501. self.sp.markChanged()
  502. dlg.Destroy()
  503. def OnSpellCheckerScriptDictionaryDlg(self):
  504. dlg = spellcheckcfgdlg.SCDictDlg(mainFrame,
  505. copy.deepcopy(self.sp.scDict), False)
  506. if dlg.ShowModal() == wx.ID_OK:
  507. self.sp.scDict = dlg.scDict
  508. self.sp.markChanged()
  509. dlg.Destroy()
  510. def OnWatermark(self):
  511. dlg = watermarkdlg.WatermarkDlg(
  512. mainFrame, self.sp, self.fileNameDisplay.replace(".trelby", ""))
  513. dlg.ShowModal()
  514. dlg.Destroy()
  515. def OnReportDialogueChart(self):
  516. self.sp.paginate()
  517. dialoguechart.genDialogueChart(mainFrame, self.sp)
  518. def OnReportCharacter(self):
  519. self.sp.paginate()
  520. characterreport.genCharacterReport(mainFrame, self.sp)
  521. def OnReportLocation(self):
  522. self.sp.paginate()
  523. locationreport.genLocationReport(mainFrame, self.sp)
  524. def OnReportScene(self):
  525. self.sp.paginate()
  526. scenereport.genSceneReport(mainFrame, self.sp)
  527. def OnReportScript(self):
  528. self.sp.paginate()
  529. scriptreport.genScriptReport(mainFrame, self.sp)
  530. def OnCompareScripts(self):
  531. if mainFrame.tabCtrl.getPageCount() < 2:
  532. wx.MessageBox("You need at least two scripts open to"
  533. " compare them.", "Error", wx.OK, mainFrame)
  534. return
  535. items = []
  536. for c in mainFrame.getCtrls():
  537. items.append(c.fileNameDisplay)
  538. dlg = misc.ScriptChooserDlg(mainFrame, items)
  539. sel1 = -1
  540. sel2 = -1
  541. if dlg.ShowModal() == wx.ID_OK:
  542. sel1 = dlg.sel1
  543. sel2 = dlg.sel2
  544. force = dlg.forceSameCfg
  545. dlg.Destroy()
  546. if sel1 == -1:
  547. return
  548. if sel1 == sel2:
  549. wx.MessageBox("You can't compare a script to itself.", "Error",
  550. wx.OK, mainFrame)
  551. return
  552. c1 = mainFrame.tabCtrl.getPage(sel1).ctrl
  553. c2 = mainFrame.tabCtrl.getPage(sel2).ctrl
  554. sp1 = c1.getExportable("compare")
  555. sp2 = c2.getExportable("compare")
  556. if not sp1 or not sp2:
  557. return
  558. if force:
  559. sp2 = copy.deepcopy(sp2)
  560. sp2.cfg = copy.deepcopy(sp1.cfg)
  561. sp2.reformatAll()
  562. sp2.paginate()
  563. s = sp1.compareScripts(sp2)
  564. if s:
  565. gutil.showTempPDF(s, cfgGl, mainFrame)
  566. else:
  567. wx.MessageBox("The scripts are identical.", "Results", wx.OK,
  568. mainFrame)
  569. def canBeClosed(self):
  570. if self.sp.isModified():
  571. if wx.MessageBox("The script has been modified. Are you sure\n"
  572. "you want to discard the changes?", "Confirm",
  573. wx.YES_NO | wx.NO_DEFAULT, mainFrame) == wx.NO:
  574. return False
  575. return True
  576. # page up (dir == -1) or page down (dir == 1) was pressed, handle it.
  577. # cs = CommandState.
  578. def pageCmd(self, cs, dir):
  579. if self.sp.acItems:
  580. cs.doAutoComp = cs.AC_KEEP
  581. self.sp.pageScrollAutoComp(dir)
  582. return
  583. texts, dpages = gd.vm.getScreen(self, False)
  584. # if user has scrolled with scrollbar so that cursor isn't seen,
  585. # just make cursor visible and don't move
  586. if not self.isLineVisible(self.sp.line, texts):
  587. gd.vm.makeLineVisible(self, self.sp.line, texts)
  588. cs.needsVisifying = False
  589. return
  590. self.sp.maybeMark(cs.mark)
  591. gd.vm.pageCmd(self, cs, dir, texts, dpages)
  592. def OnRevertScript(self):
  593. if self.fileName:
  594. if not self.canBeClosed():
  595. return
  596. self.loadFile(self.fileName)
  597. self.updateScreen()
  598. def OnUndo(self):
  599. self.sp.cmd("undo")
  600. self.sp.paginate()
  601. self.makeLineVisible(self.sp.line)
  602. self.updateScreen()
  603. def OnRedo(self):
  604. self.sp.cmd("redo")
  605. self.sp.paginate()
  606. self.makeLineVisible(self.sp.line)
  607. self.updateScreen()
  608. # returns True if something was deleted
  609. def OnCut(self, doUpdate = True, doDelete = True, copyToClip = True):
  610. marked = self.sp.getMarkedLines()
  611. if not marked:
  612. return False
  613. cd = self.sp.getSelectedAsCD(doDelete)
  614. if copyToClip:
  615. mainFrame.clipboard = cd
  616. if doUpdate:
  617. self.makeLineVisible(self.sp.line)
  618. self.updateScreen()
  619. return doDelete
  620. def OnCopy(self):
  621. self.OnCut(doDelete = False)
  622. def OnCopySystem(self, formatted = False):
  623. cd = self.sp.getSelectedAsCD(False)
  624. if not cd:
  625. return
  626. tmpSp = screenplay.Screenplay(cfgGl)
  627. tmpSp.lines = cd.lines
  628. if formatted:
  629. # have to call paginate, otherwise generateText will not
  630. # process all the text
  631. tmpSp.paginate()
  632. s = tmpSp.generateText(False)
  633. else:
  634. s = util.String()
  635. for ln in tmpSp.lines:
  636. txt = ln.text
  637. if tmpSp.cfg.getType(ln.lt).export.isCaps:
  638. txt = util.upper(txt)
  639. s += txt + config.lb2str(ln.lb)
  640. s = str(s).replace("\n", os.linesep)
  641. if wx.TheClipboard.Open():
  642. wx.TheClipboard.UsePrimarySelection(False)
  643. wx.TheClipboard.Clear()
  644. wx.TheClipboard.AddData(wx.TextDataObject(s))
  645. wx.TheClipboard.Flush()
  646. wx.TheClipboard.Close()
  647. def OnPaste(self, clines = None):
  648. if not clines:
  649. cd = mainFrame.clipboard
  650. if not cd:
  651. return
  652. clines = cd.lines
  653. self.sp.paste(clines)
  654. self.makeLineVisible(self.sp.line)
  655. self.updateScreen()
  656. def OnPasteSystemCb(self):
  657. s = ""
  658. if wx.TheClipboard.Open():
  659. wx.TheClipboard.UsePrimarySelection(False)
  660. df = wx.DataFormat(wx.DF_TEXT)
  661. if wx.TheClipboard.IsSupported(df):
  662. data = wx.TextDataObject()
  663. wx.TheClipboard.GetData(data)
  664. s = util.cleanInput(data.GetText())
  665. wx.TheClipboard.Close()
  666. s = util.fixNL(s)
  667. if len(s) == 0:
  668. return
  669. inLines = s.split("\n")
  670. # shouldn't be possible, but...
  671. if len(inLines) == 0:
  672. return
  673. lines = []
  674. for s in inLines:
  675. if s:
  676. lines.append(screenplay.Line(screenplay.LB_LAST,
  677. screenplay.ACTION, s))
  678. self.OnPaste(lines)
  679. def OnSelectScene(self):
  680. self.sp.cmd("selectScene")
  681. self.makeLineVisible(self.sp.line)
  682. self.updateScreen()
  683. def OnSelectAll(self):
  684. self.sp.cmd("selectAll")
  685. self.makeLineVisible(self.sp.line)
  686. self.updateScreen()
  687. def OnGotoScene(self):
  688. self.sp.paginate()
  689. self.clearAutoComp()
  690. scenes = self.sp.getSceneLocations()
  691. def validateFunc(s):
  692. if s in [x[0] for x in scenes]:
  693. return ""
  694. else:
  695. return "Invalid scene number."
  696. dlg = misc.TextInputDlg(mainFrame, "Enter scene number (%s - %s):" %\
  697. (scenes[0][0], scenes[-1][0]), "Goto scene", validateFunc)
  698. if dlg.ShowModal() == wx.ID_OK:
  699. for it in scenes:
  700. if it[0] == dlg.input:
  701. self.sp.line = it[1]
  702. self.sp.column = 0
  703. break
  704. # we need to refresh the screen in all cases because pagination
  705. # might have changed
  706. self.makeLineVisible(self.sp.line)
  707. self.updateScreen()
  708. def OnGotoPage(self):
  709. self.sp.paginate()
  710. self.clearAutoComp()
  711. pages = self.sp.getPageNumbers()
  712. def validateFunc(s):
  713. if s in pages:
  714. return ""
  715. else:
  716. return "Invalid page number."
  717. dlg = misc.TextInputDlg(mainFrame, "Enter page number (%s - %s):" %\
  718. (pages[0], pages[-1]), "Goto page", validateFunc)
  719. if dlg.ShowModal() == wx.ID_OK:
  720. page = int(dlg.input)
  721. self.sp.line = self.sp.page2lines(page)[0]
  722. self.sp.column = 0
  723. # we need to refresh the screen in all cases because pagination
  724. # might have changed
  725. self.makeLineVisible(self.sp.line)
  726. self.updateScreen()
  727. def OnInsertNbsp(self):
  728. self.OnKeyChar(util.MyKeyEvent(160))
  729. def OnFindNextError(self):
  730. self.clearAutoComp()
  731. line, msg = self.sp.findError(self.sp.line)
  732. if line != -1:
  733. self.sp.line = line
  734. self.sp.column = 0
  735. self.makeLineVisible(self.sp.line)
  736. self.updateScreen()
  737. else:
  738. msg = "No errors found."
  739. wx.MessageBox(msg, "Results", wx.OK, mainFrame)
  740. def OnFind(self):
  741. self.sp.clearMark()
  742. self.clearAutoComp()
  743. self.updateScreen()
  744. dlg = finddlg.FindDlg(mainFrame, self)
  745. dlg.ShowModal()
  746. dlg.saveState()
  747. dlg.Destroy()
  748. self.sp.clearMark()
  749. self.makeLineVisible(self.sp.line)
  750. self.updateScreen()
  751. def OnSpellCheckerDlg(self):
  752. self.sp.clearMark()
  753. self.clearAutoComp()
  754. wasAtStart = self.sp.line == 0
  755. wx.BeginBusyCursor()
  756. if not spellcheck.loadDict(mainFrame):
  757. wx.EndBusyCursor()
  758. return
  759. sc = spellcheck.SpellChecker(self.sp, gd.scDict)
  760. found = sc.findNext()
  761. wx.EndBusyCursor()
  762. if not found:
  763. s = ""
  764. if not wasAtStart:
  765. s = "\n\n(Starting position was not at\n"\
  766. "the beginning of the script.)"
  767. wx.MessageBox("Spell checker found no errors." + s, "Results",
  768. wx.OK, mainFrame)
  769. return
  770. dlg = spellcheckdlg.SpellCheckDlg(mainFrame, self, sc, gd.scDict)
  771. dlg.ShowModal()
  772. if dlg.changedGlobalDict:
  773. gd.saveScDict()
  774. dlg.Destroy()
  775. self.sp.clearMark()
  776. self.makeLineVisible(self.sp.line)
  777. self.updateScreen()
  778. def OnDeleteElements(self):
  779. # even though Screenplay.removeElementTypes does this as well, do
  780. # it here so that screen is cleared from the auto-comp box before
  781. # we open the dialog
  782. self.clearAutoComp()
  783. types = []
  784. for t in config.getTIs():
  785. types.append(misc.CheckBoxItem(t.name, False, t.lt))
  786. dlg = misc.CheckBoxDlg(mainFrame, "Delete elements", types,
  787. "Element types to delete:", True)
  788. ok = False
  789. if dlg.ShowModal() == wx.ID_OK:
  790. ok = True
  791. tdict = misc.CheckBoxItem.getClientData(types)
  792. dlg.Destroy()
  793. if not ok or (len(tdict) == 0):
  794. return
  795. self.sp.removeElementTypes(tdict, True)
  796. self.sp.paginate()
  797. self.makeLineVisible(self.sp.line)
  798. self.updateScreen()
  799. def OnSave(self):
  800. if self.fileName:
  801. self.saveFile(self.fileName)
  802. else:
  803. self.OnSaveScriptAs()
  804. def OnSaveScriptAs(self):
  805. if self.fileName:
  806. dDir = os.path.dirname(self.fileName)
  807. dFile = os.path.basename(self.fileName)
  808. else:
  809. dDir = misc.scriptDir
  810. dFile = u""
  811. dlg = wx.FileDialog(mainFrame, "Filename to save as",
  812. defaultDir = dDir,
  813. defaultFile = dFile,
  814. wildcard = "Trelby files (*.trelby)|*.trelby|All files|*",
  815. style = wx.SAVE | wx.OVERWRITE_PROMPT)
  816. if dlg.ShowModal() == wx.ID_OK:
  817. self.saveFile(dlg.GetPath())
  818. dlg.Destroy()
  819. def OnExportScript(self):
  820. sp = self.getExportable("export")
  821. if not sp:
  822. return
  823. dlg = wx.FileDialog(mainFrame, "Filename to export as",
  824. misc.scriptDir,
  825. wildcard = "PDF|*.pdf|"
  826. "RTF|*.rtf|"
  827. "Final Draft XML|*.fdx|"
  828. "HTML|*.html|"
  829. "Fountain|*.fountain|"
  830. "Formatted text|*.txt",
  831. style = wx.SAVE | wx.OVERWRITE_PROMPT)
  832. if dlg.ShowModal() == wx.ID_OK:
  833. misc.scriptDir = dlg.GetDirectory()
  834. choice = dlg.GetFilterIndex()
  835. if choice == 0:
  836. data = sp.generatePDF(True)
  837. suffix = ".pdf"
  838. elif choice == 1:
  839. data = sp.generateRTF()
  840. suffix = ".rtf"
  841. elif choice == 2:
  842. data = sp.generateFDX()
  843. suffix = ".fdx"
  844. elif choice == 3:
  845. data = self.getExportHtml(sp)
  846. suffix = ".html"
  847. elif choice == 4:
  848. data = sp.generateFountain()
  849. suffix = ".fountain"
  850. else:
  851. data = self.getExportText(sp)
  852. suffix = ".txt"
  853. fileName = util.ensureEndsIn(dlg.GetPath(), suffix)
  854. if data:
  855. util.writeToFile(fileName, data, mainFrame)
  856. dlg.Destroy()
  857. def OnPrint(self):
  858. sp = self.getExportable("print")
  859. if not sp:
  860. return
  861. s = sp.generatePDF(False)
  862. gutil.showTempPDF(s, cfgGl, mainFrame)
  863. def OnSettings(self):
  864. dlg = cfgdlg.CfgDlg(mainFrame, copy.deepcopy(cfgGl),
  865. self.applyGlobalCfg, True)
  866. if dlg.ShowModal() == wx.ID_OK:
  867. self.applyGlobalCfg(dlg.cfg)
  868. dlg.Destroy()
  869. def OnScriptSettings(self):
  870. dlg = cfgdlg.CfgDlg(mainFrame, copy.deepcopy(self.sp.cfg),
  871. self.applyCfg, False)
  872. if dlg.ShowModal() == wx.ID_OK:
  873. self.applyCfg(dlg.cfg)
  874. dlg.Destroy()
  875. def cmdAbort(self, cs):
  876. self.sp.abortCmd(cs)
  877. def cmdChangeToAction(self, cs):
  878. self.sp.toActionCmd(cs)
  879. def cmdChangeToCharacter(self, cs):
  880. self.sp.toCharacterCmd(cs)
  881. def cmdChangeToDialogue(self, cs):
  882. self.sp.toDialogueCmd(cs)
  883. def cmdChangeToNote(self, cs):
  884. self.sp.toNoteCmd(cs)
  885. def cmdChangeToParenthetical(self, cs):
  886. self.sp.toParenCmd(cs)
  887. def cmdChangeToScene(self, cs):
  888. self.sp.toSceneCmd(cs)
  889. def cmdChangeToShot(self, cs):
  890. self.sp.toShotCmd(cs)
  891. def cmdChangeToActBreak(self,cs):
  892. self.sp.toActBreakCmd(cs)
  893. def cmdChangeToTransition(self, cs):
  894. self.sp.toTransitionCmd(cs)
  895. def cmdDelete(self, cs):
  896. if not self.sp.mark:
  897. self.sp.deleteForwardCmd(cs)
  898. else:
  899. self.OnCut(doUpdate = False, copyToClip = False)
  900. def cmdDeleteBackward(self, cs):
  901. if not self.sp.mark:
  902. self.sp.deleteBackwardCmd(cs)
  903. else:
  904. self.OnCut(doUpdate = False, copyToClip = False)
  905. def cmdForcedLineBreak(self, cs):
  906. self.sp.insertForcedLineBreakCmd(cs)
  907. def cmdMoveDown(self, cs):
  908. self.sp.moveDownCmd(cs)
  909. def cmdMoveEndOfLine(self, cs):
  910. self.sp.moveLineEndCmd(cs)
  911. def cmdMoveEndOfScript(self, cs):
  912. self.sp.moveEndCmd(cs)
  913. def cmdMoveLeft(self, cs):
  914. self.sp.moveLeftCmd(cs)
  915. def cmdMovePageDown(self, cs):
  916. self.pageCmd(cs, 1)
  917. def cmdMovePageUp(self, cs):
  918. self.pageCmd(cs, -1)
  919. def cmdMoveRight(self, cs):
  920. self.sp.moveRightCmd(cs)
  921. def cmdMoveSceneDown(self, cs):
  922. self.sp.moveSceneDownCmd(cs)
  923. def cmdMoveSceneUp(self, cs):
  924. self.sp.moveSceneUpCmd(cs)
  925. def cmdMoveStartOfLine(self, cs):
  926. self.sp.moveLineStartCmd(cs)
  927. def cmdMoveStartOfScript(self, cs):
  928. self.sp.moveStartCmd(cs)
  929. def cmdMoveUp(self, cs):
  930. self.sp.moveUpCmd(cs)
  931. def cmdNewElement(self, cs):
  932. self.sp.splitElementCmd(cs)
  933. def cmdSetMark(self, cs):
  934. self.sp.setMarkCmd(cs)
  935. def cmdTab(self, cs):
  936. self.sp.tabCmd(cs)
  937. def cmdTabPrev(self, cs):
  938. self.sp.toPrevTypeTabCmd(cs)
  939. def cmdSpeedTest(self, cs):
  940. import undo
  941. self.speedTestUndo = []
  942. def testUndoFullCopy():
  943. u = undo.FullCopy(self.sp)
  944. u.setAfter(self.sp)
  945. self.speedTestUndo.append(u)
  946. def testReformatAll():
  947. self.sp.reformatAll()
  948. def testPaginate():
  949. self.sp.paginate()
  950. def testUpdateScreen():
  951. self.updateScreen()
  952. self.Update()
  953. def testAddRemoveChar():
  954. self.OnKeyChar(util.MyKeyEvent(ord("a")))
  955. self.OnKeyChar(util.MyKeyEvent(wx.WXK_BACK))
  956. def testDeepcopy():
  957. copy.deepcopy(self.sp)
  958. # contains (name, func) tuples
  959. tests = []
  960. for name, var in locals().iteritems():
  961. if callable(var):
  962. tests.append((name, var))
  963. tests.sort()
  964. count = 100
  965. print "-" * 20
  966. for name, func in tests:
  967. t = time.time()
  968. for i in xrange(count):
  969. func()
  970. t = time.time() - t
  971. print "%.5f seconds per %s" % (t / count, name)
  972. print "-" * 20
  973. # it's annoying having the program ask if you want to save after
  974. # running these tests, so pretend the script hasn't changed
  975. self.sp.markChanged(False)
  976. def cmdTest(self, cs):
  977. pass
  978. def OnKeyChar(self, ev):
  979. kc = ev.GetKeyCode()
  980. #print "kc: %d, ctrl/alt/shift: %d, %d, %d" %\
  981. # (kc, ev.ControlDown(), ev.AltDown(), ev.ShiftDown())
  982. cs = screenplay.CommandState()
  983. cs.mark = bool(ev.ShiftDown())
  984. scrollDirection = config.SCROLL_CENTER
  985. if not ev.ControlDown() and not ev.AltDown() and \
  986. util.isValidInputChar(kc):
  987. # WX2.6-FIXME: we should probably use GetUnicodeKey() (dunno
  988. # how to get around the isValidInputChar test in the preceding
  989. # line, need to test what GetUnicodeKey() returns on
  990. # non-input-character events)
  991. addChar = True
  992. # If there's something selected, either remove it, or clear selection.
  993. if self.sp.mark and cfgGl.overwriteSelectionOnInsert:
  994. if not self.OnCut(doUpdate = False, copyToClip = False):
  995. self.sp.clearMark()
  996. addChar = False
  997. if addChar:
  998. cs.char = chr(kc)
  999. if opts.isTest and (cs.char == "ĺ"):
  1000. self.loadFile(u"sample.trelby")
  1001. elif opts.isTest and (cs.char == "¤"):
  1002. self.cmdTest(cs)
  1003. elif opts.isTest and (cs.char == "˝"):
  1004. self.cmdSpeedTest(cs)
  1005. else:
  1006. self.sp.addCharCmd(cs)
  1007. else:
  1008. cmd = mainFrame.kbdCommands.get(util.Key(kc,
  1009. ev.ControlDown(), ev.AltDown(), ev.ShiftDown()).toInt())
  1010. if cmd:
  1011. scrollDirection = cmd.scrollDirection
  1012. if cmd.isMenu:
  1013. getattr(mainFrame, "On" + cmd.name)()
  1014. return
  1015. else:
  1016. getattr(self, "cmd" + cmd.name)(cs)
  1017. else:
  1018. ev.Skip()
  1019. return
  1020. self.sp.cmdPost(cs)
  1021. if cfgGl.paginateInterval > 0:
  1022. now = time.time()
  1023. if (now - self.sp.lastPaginated) >= cfgGl.paginateInterval:
  1024. self.sp.paginate()
  1025. cs.needsVisifying = True
  1026. if cs.needsVisifying:
  1027. self.makeLineVisible(self.sp.line, scrollDirection)
  1028. self.updateScreen()
  1029. def OnPaint(self, event):
  1030. #ldkjfldsj = util.TimerDev("paint")
  1031. ls = self.sp.lines
  1032. if misc.doDblBuf:
  1033. dc = wx.BufferedPaintDC(self, self.screenBuf)
  1034. else:
  1035. dc = wx.PaintDC(self)
  1036. size = self.GetClientSize()
  1037. marked = self.sp.getMarkedLines()
  1038. lineh = gd.vm.getLineHeight(self)
  1039. posX = -1
  1040. cursorY = -1
  1041. # auto-comp FontInfo
  1042. acFi = None
  1043. # key = font, value = ([text, ...], [(x, y), ...], [wx.Colour, ...])
  1044. texts = {}
  1045. # lists of underline-lines to draw, one for normal text and one
  1046. # for header texts. list objects are (x, y, width) tuples.
  1047. ulines = []
  1048. ulinesHdr = []
  1049. strings, dpages = gd.vm.getScreen(self, True, True)
  1050. dc.SetBrush(cfgGui.workspaceBrush)
  1051. dc.SetPen(cfgGui.workspacePen)
  1052. dc.DrawRectangle(0, 0, size.width, size.height)
  1053. dc.SetPen(cfgGui.tabBorderPen)
  1054. dc.DrawLine(0,0,0,size.height)
  1055. if not dpages:
  1056. # draft mode; draw an infinite page
  1057. lx = util.clamp((size.width - self.pageW) // 2, 0)
  1058. rx = lx + self.pageW
  1059. dc.SetBrush(cfgGui.textBgBrush)
  1060. dc.SetPen(cfgGui.textBgPen)
  1061. dc.DrawRectangle(lx, 5, self.pageW, size.height - 5)
  1062. dc.SetPen(cfgGui.pageBorderPen)
  1063. dc.DrawLine(lx, 5, lx, size.height)
  1064. dc.DrawLine(rx, 5, rx, size.height)
  1065. else:
  1066. dc.SetBrush(cfgGui.textBgBrush)
  1067. dc.SetPen(cfgGui.pageBorderPen)
  1068. for dp in dpages:
  1069. dc.DrawRectangle(dp.x1, dp.y1, dp.x2 - dp.x1 + 1,
  1070. dp.y2 - dp.y1 + 1)
  1071. dc.SetPen(cfgGui.pageShadowPen)
  1072. for dp in dpages:
  1073. # + 2 because DrawLine doesn't draw to end point but stops
  1074. # one pixel short...
  1075. dc.DrawLine(dp.x1 + 1, dp.y2 + 1, dp.x2 + 1, dp.y2 + 1)
  1076. dc.DrawLine(dp.x2 + 1, dp.y1 + 1, dp.x2 + 1, dp.y2 + 2)
  1077. for t in strings:
  1078. i = t.line
  1079. y = t.y
  1080. fi = t.fi
  1081. fx = fi.fx
  1082. if i != -1:
  1083. l = ls[i]
  1084. if l.lt == screenplay.NOTE:
  1085. dc.SetPen(cfgGui.notePen)
  1086. dc.SetBrush(cfgGui.noteBrush)
  1087. nx = t.x - 5
  1088. nw = self.sp.cfg.getType(l.lt).width * fx + 10
  1089. dc.DrawRectangle(nx, y, nw, lineh)
  1090. dc.SetPen(cfgGui.textPen)
  1091. util.drawLine(dc, nx - 1, y, 0, lineh)
  1092. util.drawLine(dc, nx + nw, y, 0, lineh)
  1093. if self.sp.isFirstLineOfElem(i):
  1094. util.drawLine(dc, nx - 1, y - 1, nw + 2, 0)
  1095. if self.sp.isLastLineOfElem(i):
  1096. util.drawLine(dc, nx - 1, y + lineh,
  1097. nw + 2, 0)
  1098. if marked and self.sp.isLineMarked(i, marked):
  1099. c1, c2 = self.sp.getMarkedColumns(i, marked)
  1100. dc.SetPen(cfgGui.selectedPen)
  1101. dc.SetBrush(cfgGui.selectedBrush)
  1102. dc.DrawRectangle(t.x + c1 * fx, y, (c2 - c1 + 1) * fx,
  1103. lineh)
  1104. if mainFrame.showFormatting:
  1105. dc.SetPen(cfgGui.bluePen)
  1106. util.drawLine(dc, t.x, y, 0, lineh)
  1107. extraIndent = 1 if self.sp.needsExtraParenIndent(i) else 0
  1108. util.drawLine(dc,
  1109. t.x + (self.sp.cfg.getType(l.lt).width - extraIndent) * fx,
  1110. y, 0, lineh)
  1111. dc.SetTextForeground(cfgGui.redColor)
  1112. dc.SetFont(cfgGui.fonts[pml.NORMAL].font)
  1113. dc.DrawText(config.lb2char(l.lb), t.x - 10, y)
  1114. if not dpages:
  1115. if cfgGl.pbi == config.PBI_REAL_AND_UNADJ:
  1116. if self.sp.line2pageNoAdjust(i) != \
  1117. self.sp.line2pageNoAdjust(i + 1):
  1118. dc.SetPen(cfgGui.pagebreakNoAdjustPen)
  1119. util.drawLine(dc, 0, y + lineh - 1,
  1120. size.width, 0)
  1121. if cfgGl.pbi in (config.PBI_REAL,
  1122. config.PBI_REAL_AND_UNADJ):
  1123. thisPage = self.sp.line2page(i)
  1124. if thisPage != self.sp.line2page(i + 1):
  1125. dc.SetPen(cfgGui.pagebreakPen)
  1126. util.drawLine(dc, 0, y + lineh - 1,
  1127. size.width, 0)
  1128. if i == self.sp.line:
  1129. posX = t.x
  1130. cursorY = y
  1131. acFi = fi
  1132. dc.SetPen(cfgGui.cursorPen)
  1133. dc.SetBrush(cfgGui.cursorBrush)
  1134. dc.DrawRectangle(t.x + self.sp.column * fx, y, fx, fi.fy)
  1135. if len(t.text) != 0:
  1136. tl = texts.get(fi.font)
  1137. if tl == None:
  1138. tl = ([], [], [])
  1139. texts[fi.font] = tl
  1140. tl[0].append(t.text)
  1141. tl[1].append((t.x, y))
  1142. if t.line != -1:
  1143. if cfgGl.useCustomElemColors:
  1144. tl[2].append(cfgGui.lt2textColor(ls[t.line].lt))
  1145. else:
  1146. tl[2].append(cfgGui.textColor)
  1147. else:
  1148. tl[2].append(cfgGui.textHdrColor)
  1149. if t.isUnderlined:
  1150. if t.line != -1:
  1151. uli = ulines
  1152. else:
  1153. uli = ulinesHdr
  1154. uli.append((t.x, y + lineh - 1,
  1155. len(t.text) * fx - 1))
  1156. if ulines:
  1157. dc.SetPen(cfgGui.textPen)
  1158. for ul in ulines:
  1159. util.drawLine(dc, ul[0], ul[1], ul[2], 0)
  1160. if ulinesHdr:
  1161. dc.SetPen(cfgGui.textHdrPen)
  1162. for ul in ulinesHdr:
  1163. util.drawLine(dc, ul[0], ul[1], ul[2], 0)
  1164. for tl in texts.iteritems():
  1165. gd.vm.drawTexts(self, dc, tl)
  1166. if self.sp.acItems and (cursorY > 0):
  1167. self.drawAutoComp(dc, posX, cursorY, acFi)
  1168. def drawAutoComp(self, dc, posX, cursorY, fi):
  1169. ac = self.sp.acItems
  1170. asel = self.sp.acSel
  1171. offset = 5
  1172. selBleed = 2
  1173. # scroll bar width
  1174. sbw = 10
  1175. size = self.GetClientSize()
  1176. dc.SetFont(fi.font)
  1177. show = min(self.sp.acMax, len(ac))
  1178. doSbw = show < len(ac)
  1179. startPos = (asel // show) * show
  1180. endPos = min(startPos + show, len(ac))
  1181. if endPos == len(ac):
  1182. startPos = max(0, endPos - show)
  1183. w = 0
  1184. for i in range(len(ac)):
  1185. tw = dc.GetTextExtent(ac[i])[0]
  1186. w = max(w, tw)
  1187. w += offset * 2
  1188. h = show * fi.fy + offset * 2
  1189. itemW = w - offset * 2 + selBleed * 2
  1190. if doSbw:
  1191. w += sbw + offset * 2
  1192. sbh = h - offset * 2 + selBleed * 2
  1193. posY = cursorY + fi.fy + 5
  1194. # if the box doesn't fit on the screen in the normal position, put
  1195. # it above the current line. if it doesn't fit there either,
  1196. # that's just too bad, we don't support window sizes that small.
  1197. if (posY + h) > size.height:
  1198. posY = cursorY - h - 1
  1199. dc.SetPen(cfgGui.autoCompPen)
  1200. dc.SetBrush(cfgGui.autoCompBrush)
  1201. dc.DrawRectangle(posX, posY, w, h)
  1202. dc.SetTextForeground(cfgGui.autoCompFgColor)
  1203. for i in range(startPos, endPos):
  1204. if i == asel:
  1205. dc.SetPen(cfgGui.autoCompRevPen)
  1206. dc.SetBrush(cfgGui.autoCompRevBrush)
  1207. dc.SetTextForeground(cfgGui.autoCompBgColor)
  1208. dc.DrawRectangle(posX + offset - selBleed,
  1209. posY + offset + (i - startPos) * fi.fy - selBleed,
  1210. itemW,
  1211. fi.fy + selBleed * 2)
  1212. dc.SetTextForeground(cfgGui.autoCompBgColor)
  1213. dc.SetPen(cfgGui.autoCompPen)
  1214. dc.SetBrush(cfgGui.autoCompBrush)
  1215. dc.DrawText(ac[i], posX + offset, posY + offset +
  1216. (i - startPos) * fi.fy)
  1217. if i == asel:
  1218. dc.SetTextForeground(cfgGui.autoCompFgColor)
  1219. if doSbw:
  1220. dc.SetPen(cfgGui.autoCompPen)
  1221. dc.SetBrush(cfgGui.autoCompRevBrush)
  1222. util.drawLine(dc, posX + w - offset * 2 - sbw,
  1223. posY, 0, h)
  1224. dc.DrawRectangle(posX + w - offset - sbw,
  1225. posY + offset - selBleed + int((float(startPos) /
  1226. len(ac)) * sbh),
  1227. sbw, int((float(show) / len(ac)) * sbh))
  1228. class MyFrame(wx.Frame):
  1229. def __init__(self, parent, id, title):
  1230. wx.Frame.__init__(self, parent, id, title, name = "Trelby")
  1231. if misc.isUnix:
  1232. # automatically reaps zombies
  1233. signal.signal(signal.SIGCHLD, signal.SIG_IGN)
  1234. self.clipboard = None
  1235. self.showFormatting = False
  1236. self.SetSizeHints(gd.cvars.getMin("width"),
  1237. gd.cvars.getMin("height"))
  1238. self.MoveXY(gd.posX, gd.posY)
  1239. self.SetSize(wx.Size(gd.width, gd.height))
  1240. util.removeTempFiles(misc.tmpPrefix)
  1241. self.mySetIcons()
  1242. self.allocIds()
  1243. fileMenu = wx.Menu()
  1244. fileMenu.Append(ID_FILE_NEW, "&New\tCTRL-N")
  1245. fileMenu.Append(ID_FILE_OPEN, "&Open...\tCTRL-O")
  1246. fileMenu.Append(ID_FILE_SAVE, "&Save\tCTRL-S")
  1247. fileMenu.Append(ID_FILE_SAVE_AS, "Save &As...")
  1248. fileMenu.Append(ID_FILE_CLOSE, "&Close\tCTRL-W")
  1249. fileMenu.Append(ID_FILE_REVERT, "&Revert")
  1250. fileMenu.AppendSeparator()
  1251. fileMenu.Append(ID_FILE_IMPORT, "&Import...")
  1252. fileMenu.Append(ID_FILE_EXPORT, "&Export...")
  1253. fileMenu.AppendSeparator()
  1254. fileMenu.Append(ID_FILE_PRINT, "&Print (via PDF)\tCTRL-P")
  1255. fileMenu.AppendSeparator()
  1256. tmp = wx.Menu()
  1257. tmp.Append(ID_SETTINGS_CHANGE, "&Change...")
  1258. tmp.AppendSeparator()
  1259. tmp.Append(ID_SETTINGS_LOAD, "Load...")
  1260. tmp.Append(ID_SETTINGS_SAVE_AS, "Save as...")
  1261. tmp.AppendSeparator()
  1262. tmp.Append(ID_SETTINGS_SC_DICT, "&Spell checker dictionary...")
  1263. settingsMenu = tmp
  1264. fileMenu.AppendMenu(ID_FILE_SETTINGS, "Se&ttings", tmp)
  1265. fileMenu.AppendSeparator()
  1266. # "most recently used" list comes in here
  1267. fileMenu.AppendSeparator()
  1268. fileMenu.Append(ID_FILE_EXIT, "E&xit\tCTRL-Q")

Large files files are truncated, but you can click here to view the full file