goutput.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. """
  2. @package goutput
  3. @brief Command output log widget
  4. Classes:
  5. - GMConsole
  6. - GMStc
  7. - GMStdout
  8. - GMStderr
  9. (C) 2007-2008 by the GRASS Development Team
  10. This program is free software under the GNU General Public
  11. License (>=v2). Read the file COPYING that comes with GRASS
  12. for details.
  13. @author Michael Barton (Arizona State University)
  14. @author Martin Landa <landa.martin gmail.com>
  15. """
  16. import os
  17. import sys
  18. import textwrap
  19. import time
  20. import threading
  21. import Queue
  22. import wx
  23. import wx.stc
  24. from wx.lib.newevent import NewEvent
  25. import globalvar
  26. import gcmd
  27. from debug import Debug as Debug
  28. wxCmdOutput, EVT_CMD_OUTPUT = NewEvent()
  29. wxCmdProgress, EVT_CMD_PROGRESS = NewEvent()
  30. wxCmdRun, EVT_CMD_RUN = NewEvent()
  31. wxCmdDone, EVT_CMD_DONE = NewEvent()
  32. wxCmdAbort, EVT_CMD_ABORT = NewEvent()
  33. def GrassCmd(cmd, stdout, stderr):
  34. """Return GRASS command thread"""
  35. return gcmd.CommandThread(cmd=cmd,
  36. stdout=stdout, stderr=stderr)
  37. class CmdThread(threading.Thread):
  38. """Thread for GRASS commands"""
  39. requestId = 0
  40. def __init__(self, parent, requestQ, resultQ, **kwds):
  41. threading.Thread.__init__(self, **kwds)
  42. self.setDaemon(True)
  43. self.parent = parent # GMConsole
  44. self.requestQ = requestQ
  45. self.resultQ = resultQ
  46. self.start()
  47. def RunCmd(self, callable, *args, **kwds):
  48. CmdThread.requestId += 1
  49. self.requestCmd = None
  50. self.requestQ.put((CmdThread.requestId, callable, args, kwds))
  51. return CmdThread.requestId
  52. def run(self):
  53. while True:
  54. requestId, callable, args, kwds = self.requestQ.get()
  55. requestTime = time.time()
  56. event = wxCmdRun(cmd=args[0],
  57. pid=requestId)
  58. wx.PostEvent(self.parent, event)
  59. time.sleep(.1)
  60. self.requestCmd = callable(*args, **kwds)
  61. self.resultQ.put((requestId, self.requestCmd.run()))
  62. event = wxCmdDone(aborted=self.requestCmd.aborted,
  63. time=requestTime,
  64. pid=requestId)
  65. time.sleep(.1)
  66. wx.PostEvent(self.parent, event)
  67. def abort(self):
  68. self.requestCmd.abort()
  69. class GMConsole(wx.Panel):
  70. """
  71. Create and manage output console for commands entered on the
  72. GIS Manager command line.
  73. """
  74. def __init__(self, parent, id=wx.ID_ANY, margin=False, pageid=0,
  75. pos=wx.DefaultPosition, size=wx.DefaultSize,
  76. style=wx.TAB_TRAVERSAL | wx.FULL_REPAINT_ON_RESIZE):
  77. wx.Panel.__init__(self, parent, id, pos, size, style)
  78. # initialize variables
  79. self.Map = None
  80. self.parent = parent # GMFrame
  81. self.lineWidth = 80
  82. self.pageid = pageid
  83. #
  84. # create queues
  85. #
  86. self.requestQ = Queue.Queue()
  87. self.resultQ = Queue.Queue()
  88. #
  89. # progress bar
  90. #
  91. self.console_progressbar = wx.Gauge(parent=self, id=wx.ID_ANY,
  92. range=100, pos=(110, 50), size=(-1, 25),
  93. style=wx.GA_HORIZONTAL)
  94. self.console_progressbar.Bind(EVT_CMD_PROGRESS, self.OnCmdProgress)
  95. #
  96. # text control for command output
  97. #
  98. self.cmd_output = GMStc(parent=self, id=wx.ID_ANY, margin=margin,
  99. wrap=None)
  100. self.cmd_output_timer = wx.Timer(self.cmd_output, id=wx.ID_ANY)
  101. self.cmd_output.Bind(EVT_CMD_OUTPUT, self.OnCmdOutput)
  102. self.cmd_output.Bind(wx.EVT_TIMER, self.OnProcessPendingOutputWindowEvents)
  103. self.Bind(EVT_CMD_RUN, self.OnCmdRun)
  104. self.Bind(EVT_CMD_DONE, self.OnCmdDone)
  105. #
  106. # stream redirection
  107. #
  108. self.cmd_stdout = GMStdout(self)
  109. self.cmd_stderr = GMStderr(self)
  110. #
  111. # thread
  112. #
  113. self.cmdThread = CmdThread(self, self.requestQ, self.resultQ)
  114. #
  115. # buttons
  116. #
  117. self.console_clear = wx.Button(parent=self, id=wx.ID_CLEAR)
  118. self.console_save = wx.Button(parent=self, id=wx.ID_SAVE)
  119. self.Bind(wx.EVT_BUTTON, self.ClearHistory, self.console_clear)
  120. self.Bind(wx.EVT_BUTTON, self.SaveHistory, self.console_save)
  121. self.Bind(EVT_CMD_ABORT, self.OnCmdAbort)
  122. self.__layout()
  123. def __layout(self):
  124. """Do layout"""
  125. boxsizer1 = wx.BoxSizer(wx.VERTICAL)
  126. gridsizer1 = wx.GridSizer(rows=1, cols=2, vgap=0, hgap=0)
  127. boxsizer1.Add(item=self.cmd_output, proportion=1,
  128. flag=wx.EXPAND | wx.ADJUST_MINSIZE, border=0)
  129. gridsizer1.Add(item=self.console_clear, proportion=0,
  130. flag=wx.ALIGN_CENTER_HORIZONTAL | wx.ADJUST_MINSIZE, border=0)
  131. gridsizer1.Add(item=self.console_save, proportion=0,
  132. flag=wx.ALIGN_CENTER_HORIZONTAL | wx.ADJUST_MINSIZE, border=0)
  133. boxsizer1.Add(item=gridsizer1, proportion=0,
  134. flag=wx.EXPAND | wx.ALIGN_CENTRE_VERTICAL | wx.TOP | wx.BOTTOM,
  135. border=5)
  136. boxsizer1.Add(item=self.console_progressbar, proportion=0,
  137. flag=wx.EXPAND | wx.ADJUST_MINSIZE | wx.LEFT | wx.RIGHT | wx.BOTTOM, border=5)
  138. boxsizer1.Fit(self)
  139. boxsizer1.SetSizeHints(self)
  140. # layout
  141. self.SetAutoLayout(True)
  142. self.SetSizer(boxsizer1)
  143. def Redirect(self):
  144. """Redirect stderr
  145. @return True redirected
  146. @return False failed
  147. """
  148. if Debug.get_level() == 0:
  149. # don't redirect when debugging is enabled
  150. #sys.stdout = self.cmd_stdout
  151. #sys.stderr = self.cmd_stderr
  152. pass
  153. return True
  154. return False
  155. def WriteLog(self, line, style=None, wrap=None):
  156. """Generic method for writing log message in
  157. given style
  158. @param line text line
  159. @param style text style (see GMStc)
  160. @param stdout write to stdout or stderr
  161. """
  162. if not style:
  163. style = self.cmd_output.StyleDefault
  164. self.cmd_output.GotoPos(self.cmd_output.GetLength())
  165. p1 = self.cmd_output.GetCurrentPos()
  166. # fill space
  167. if len(line) < self.lineWidth:
  168. diff = 80 - len(line)
  169. line += diff * ' '
  170. self.cmd_output.AddTextWrapped(line, wrap=wrap) # adds os.linesep
  171. p2 = self.cmd_output.GetCurrentPos()
  172. self.cmd_output.StartStyling(p1, 0xff)
  173. self.cmd_output.SetStyling(p2 - p1, style)
  174. self.cmd_output.EnsureCaretVisible()
  175. def WriteCmdLog(self, line, pid=None):
  176. """Write out line in selected style"""
  177. if pid:
  178. line = '(' + str(pid) + ') ' + line
  179. self.WriteLog(line, self.cmd_output.StyleCommand)
  180. def RunCmd(self, command):
  181. """
  182. Run in GUI GRASS (or other) commands typed into
  183. console command text widget, and send stdout output to output
  184. text widget.
  185. Command is transformed into a list for processing.
  186. TODO: Display commands (*.d) are captured and
  187. processed separately by mapdisp.py. Display commands are
  188. rendered in map display widget that currently has
  189. the focus (as indicted by mdidx).
  190. """
  191. # map display window available ?
  192. try:
  193. curr_disp = self.parent.curr_page.maptree.mapdisplay
  194. self.Map = curr_disp.GetRender()
  195. except:
  196. curr_disp = None
  197. # switch to 'Command output'
  198. if hasattr(self.parent, "curr_page"):
  199. # change notebook page only for Layer Manager
  200. if self.parent.notebook.GetSelection() != 1:
  201. self.parent.notebook.SetSelection(1)
  202. # command given as a string ?
  203. try:
  204. cmdlist = command.strip().split(' ')
  205. except:
  206. cmdlist = command
  207. if cmdlist[0] in globalvar.grassCmd['all']:
  208. # send GRASS command without arguments to GUI command interface
  209. # except display commands (they are handled differently)
  210. if cmdlist[0][0:2] == "d.":
  211. #
  212. # display GRASS commands
  213. #
  214. try:
  215. layertype = {'d.rast' : 'raster',
  216. 'd.rgb' : 'rgb',
  217. 'd.his' : 'his',
  218. 'd.shaded' : 'shaded',
  219. 'd.legend' : 'rastleg',
  220. 'd.rast.arrow' : 'rastarrow',
  221. 'd.rast.num' : 'rastnum',
  222. 'd.vect' : 'vector',
  223. 'd.vect.thematic': 'thememap',
  224. 'd.vect.chart' : 'themechart',
  225. 'd.grid' : 'grid',
  226. 'd.geodesic' : 'geodesic',
  227. 'd.rhumbline' : 'rhumb',
  228. 'd.labels' : 'labels'}[cmdlist[0]]
  229. except KeyError:
  230. wx.MessageBox(message=_("Command '%s' not yet implemented.") % cmdlist[0])
  231. return None
  232. # add layer into layer tree
  233. self.parent.curr_page.maptree.AddLayer(ltype=layertype,
  234. lcmd=cmdlist)
  235. else:
  236. #
  237. # other GRASS commands (r|v|g|...)
  238. #
  239. # activate computational region (set with g.region)
  240. # for all non-display commands.
  241. tmpreg = os.getenv("GRASS_REGION")
  242. os.unsetenv("GRASS_REGION")
  243. if len(cmdlist) == 1:
  244. import menuform
  245. # process GRASS command without argument
  246. menuform.GUI().ParseCommand(cmdlist, parentframe=self)
  247. else:
  248. # process GRASS command with argument
  249. self.cmdThread.RunCmd(GrassCmd,
  250. cmdlist, self.cmd_stdout, self.cmd_stderr)
  251. self.cmd_output_timer.Start(50)
  252. return None
  253. # deactivate computational region and return to display settings
  254. if tmpreg:
  255. os.environ["GRASS_REGION"] = tmpreg
  256. else:
  257. # Send any other command to the shell. Send output to
  258. # console output window
  259. # if command is not a GRASS command, treat it like a shell command
  260. try:
  261. generalCmd = gcmd.Command(cmdlist,
  262. stdout=self.cmd_stdout,
  263. stderr=self.cmd_stderr)
  264. except gcmd.CmdError, e:
  265. print >> sys.stderr, e
  266. return None
  267. def ClearHistory(self, event):
  268. """Clear history of commands"""
  269. self.cmd_output.ClearAll()
  270. self.console_progressbar.SetValue(0)
  271. def SaveHistory(self, event):
  272. """Save history of commands"""
  273. self.history = self.cmd_output.GetSelectedText()
  274. if self.history == '':
  275. self.history = self.cmd_output.GetText()
  276. # add newline if needed
  277. if len(self.history) > 0 and self.history[-1] != os.linesep:
  278. self.history += os.linesep
  279. wildcard = "Text file (*.txt)|*.txt"
  280. dlg = wx.FileDialog(
  281. self, message=_("Save file as..."), defaultDir=os.getcwd(),
  282. defaultFile="grass_cmd_history.txt", wildcard=wildcard,
  283. style=wx.SAVE|wx.FD_OVERWRITE_PROMPT)
  284. # Show the dialog and retrieve the user response. If it is the OK response,
  285. # process the data.
  286. if dlg.ShowModal() == wx.ID_OK:
  287. path = dlg.GetPath()
  288. output = open(path, "w")
  289. output.write(self.history)
  290. output.close()
  291. dlg.Destroy()
  292. def GetCmd(self):
  293. """Get running command or None"""
  294. return self.requestQ.get()
  295. def OnCmdOutput(self, event):
  296. """Print command output"""
  297. message = event.text
  298. type = event.type
  299. # message prefix
  300. if type == 'warning':
  301. messege = 'WARNING: ' + message
  302. elif type == 'error':
  303. message = 'ERROR: ' + message
  304. p1 = self.cmd_output.GetCurrentPos()
  305. message = message.replace('\r', '')
  306. pc = -1
  307. if '\b' in message:
  308. pc = p1
  309. last_c = ''
  310. for c in message:
  311. if c == '\b':
  312. pc -= 1
  313. else:
  314. self.cmd_output.SetCurrentPos(pc)
  315. self.cmd_output.ReplaceSelection(c)
  316. pc = self.cmd_output.GetCurrentPos()
  317. if c != ' ':
  318. last_c = c
  319. if last_c not in ('0123456789'):
  320. self.cmd_output.AddText('\n')
  321. pc = -1
  322. else:
  323. if os.linesep not in message:
  324. self.cmd_output.AddTextWrapped(message, wrap=60)
  325. else:
  326. self.cmd_output.AddText(message)
  327. p2 = self.cmd_output.GetCurrentPos()
  328. self.cmd_output.StartStyling(p1, 0xff)
  329. if type == 'error':
  330. self.cmd_output.SetStyling(p2 - p1 + 1, self.cmd_output.StyleError)
  331. elif type == 'warning':
  332. self.cmd_output.SetStyling(p2 - p1 + 1, self.cmd_output.StyleWarning)
  333. elif type == 'message':
  334. self.cmd_output.SetStyling(p2 - p1 + 1, self.cmd_output.StyleMessage)
  335. else: # unknown
  336. self.cmd_output.SetStyling(p2 - p1 + 1, self.cmd_output.StyleUnknown)
  337. if pc > 0:
  338. self.cmd_output.SetCurrentPos(pc)
  339. self.cmd_output.EnsureCaretVisible()
  340. def OnCmdProgress(self, event):
  341. """Update progress message info"""
  342. self.console_progressbar.SetValue(event.value)
  343. def OnCmdAbort(self, event):
  344. """Abort running command"""
  345. self.cmdThread.abort()
  346. def OnCmdRun(self, event):
  347. """Run command"""
  348. self.WriteCmdLog('%s' % ' '.join(event.cmd), pid=event.pid)
  349. def OnCmdDone(self, event):
  350. """Command done (or aborted)"""
  351. if event.aborted:
  352. # Thread aborted (using our convention of None return)
  353. self.WriteLog(_('Please note that the data are left in incosistent stage '
  354. 'and can be corrupted'), self.cmd_output.StyleWarning)
  355. self.WriteCmdLog(_('Command aborted'),
  356. pid=self.cmdThread.requestId)
  357. else:
  358. try:
  359. # Process results here
  360. self.WriteCmdLog(_('Command finished (%d sec)') % (time.time() - event.time),
  361. pid=event.pid)
  362. except KeyError:
  363. # stopped deamon
  364. pass
  365. self.console_progressbar.SetValue(0) # reset progress bar on '0%'
  366. self.cmd_output_timer.Stop()
  367. # updated command dialog
  368. if hasattr(self.parent.parent, "btn_run"):
  369. dialog = self.parent.parent
  370. if hasattr(self.parent.parent, "btn_abort"):
  371. dialog.btn_abort.Enable(False)
  372. if hasattr(self.parent.parent, "btn_cancel"):
  373. dialog.btn_cancel.Enable(True)
  374. if hasattr(self.parent.parent, "btn_clipboard"):
  375. dialog.btn_clipboard.Enable(True)
  376. if hasattr(self.parent.parent, "btn_help"):
  377. dialog.btn_help.Enable(True)
  378. dialog.btn_run.Enable(True)
  379. if dialog.get_dcmd is None and \
  380. dialog.closebox.IsChecked():
  381. time.sleep(1)
  382. dialog.Close()
  383. event.Skip()
  384. def OnProcessPendingOutputWindowEvents(self, event):
  385. self.ProcessPendingEvents()
  386. class GMStdout:
  387. """GMConsole standard output
  388. Based on FrameOutErr.py
  389. Name: FrameOutErr.py
  390. Purpose: Redirecting stdout / stderr
  391. Author: Jean-Michel Fauth, Switzerland
  392. Copyright: (c) 2005-2007 Jean-Michel Fauth
  393. Licence: GPL
  394. """
  395. def __init__(self, parent):
  396. self.parent = parent # GMConsole
  397. def write(self, s):
  398. if len(s) == 0 or s == '\n':
  399. return
  400. s = s.replace('\n', os.linesep)
  401. for line in s.split(os.linesep):
  402. if len(line) == 0:
  403. continue
  404. evt = wxCmdOutput(text=line + os.linesep,
  405. type='')
  406. wx.PostEvent(self.parent.cmd_output, evt)
  407. class GMStderr:
  408. """GMConsole standard error output
  409. Based on FrameOutErr.py
  410. Name: FrameOutErr.py
  411. Purpose: Redirecting stdout / stderr
  412. Author: Jean-Michel Fauth, Switzerland
  413. Copyright: (c) 2005-2007 Jean-Michel Fauth
  414. Licence: GPL
  415. """
  416. def __init__(self, parent):
  417. self.parent = parent # GMConsole
  418. self.type = ''
  419. self.message = ''
  420. self.printMessage = False
  421. def write(self, s):
  422. s = s.replace('\n', os.linesep)
  423. # remove/replace escape sequences '\b' or '\r' from stream
  424. progressValue = -1
  425. for line in s.split(os.linesep):
  426. if len(line) == 0:
  427. continue
  428. if 'GRASS_INFO_PERCENT' in line:
  429. value = int(line.rsplit(':', 1)[1].strip())
  430. if value >= 0 and value < 100:
  431. progressValue = value
  432. else:
  433. progressValue = 0
  434. elif 'GRASS_INFO_MESSAGE' in line:
  435. self.type = 'message'
  436. self.message = line.split(':', 1)[1].strip()
  437. elif 'GRASS_INFO_WARNING' in line:
  438. self.type = 'warning'
  439. self.message = line.split(':', 1)[1].strip()
  440. elif 'GRASS_INFO_ERROR' in line:
  441. self.type = 'error'
  442. self.message = line.split(':', 1)[1].strip()
  443. elif 'GRASS_INFO_END' in line:
  444. self.printMessage = True
  445. elif self.type == '':
  446. if len(line) == 0:
  447. continue
  448. evt = wxCmdOutput(text=line,
  449. type='')
  450. wx.PostEvent(self.parent.cmd_output, evt)
  451. elif len(line) > 0:
  452. self.message += line.strip() + os.linesep
  453. if self.printMessage and len(self.message) > 0:
  454. evt = wxCmdOutput(text=self.message,
  455. type=self.type)
  456. wx.PostEvent(self.parent.cmd_output, evt)
  457. self.type = ''
  458. self.message = ''
  459. self.printMessage = False
  460. # update progress message
  461. if progressValue > -1:
  462. # self.gmgauge.SetValue(progressValue)
  463. evt = wxCmdProgress(value=progressValue)
  464. wx.PostEvent(self.parent.console_progressbar, evt)
  465. class GMStc(wx.stc.StyledTextCtrl):
  466. """Styled GMConsole
  467. Based on FrameOutErr.py
  468. Name: FrameOutErr.py
  469. Purpose: Redirecting stdout / stderr
  470. Author: Jean-Michel Fauth, Switzerland
  471. Copyright: (c) 2005-2007 Jean-Michel Fauth
  472. Licence: GPL
  473. """
  474. def __init__(self, parent, id, margin=False, wrap=None):
  475. wx.stc.StyledTextCtrl.__init__(self, parent, id)
  476. self.parent = parent
  477. self.wrap = wrap
  478. #
  479. # styles
  480. #
  481. self.StyleDefault = 0
  482. self.StyleDefaultSpec = "face:Courier New,size:10,fore:#000000,back:#FFFFFF"
  483. self.StyleCommand = 1
  484. self.StyleCommandSpec = "face:Courier New,size:10,fore:#000000,back:#bcbcbc"
  485. self.StyleOutput = 2
  486. self.StyleOutputSpec = "face:Courier New,size:10,fore:#000000,back:#FFFFFF"
  487. # fatal error
  488. self.StyleError = 3
  489. self.StyleErrorSpec = "face:Courier New,size:10,fore:#7F0000,back:#FFFFFF"
  490. # warning
  491. self.StyleWarning = 4
  492. self.StyleWarningSpec = "face:Courier New,size:10,fore:#0000FF,back:#FFFFFF"
  493. # message
  494. self.StyleMessage = 5
  495. self.StyleMessageSpec = "face:Courier New,size:10,fore:#000000,back:#FFFFFF"
  496. # unknown
  497. self.StyleUnknown = 6
  498. self.StyleUnknownSpec = "face:Courier New,size:10,fore:#000000,back:#FFFFFF"
  499. # default and clear => init
  500. self.StyleSetSpec(wx.stc.STC_STYLE_DEFAULT, self.StyleDefaultSpec)
  501. self.StyleClearAll()
  502. self.StyleSetSpec(self.StyleCommand, self.StyleCommandSpec)
  503. self.StyleSetSpec(self.StyleOutput, self.StyleOutputSpec)
  504. self.StyleSetSpec(self.StyleError, self.StyleErrorSpec)
  505. self.StyleSetSpec(self.StyleWarning, self.StyleWarningSpec)
  506. self.StyleSetSpec(self.StyleMessage, self.StyleMessageSpec)
  507. self.StyleSetSpec(self.StyleUnknown, self.StyleUnknownSpec)
  508. #
  509. # line margins
  510. #
  511. # TODO print number only from cmdlog
  512. self.SetMarginWidth(1, 0)
  513. self.SetMarginWidth(2, 0)
  514. if margin:
  515. self.SetMarginType(0, wx.stc.STC_MARGIN_NUMBER)
  516. self.SetMarginWidth(0, 30)
  517. else:
  518. self.SetMarginWidth(0, 0)
  519. #
  520. # miscellaneous
  521. #
  522. self.SetViewWhiteSpace(False)
  523. self.SetTabWidth(4)
  524. self.SetUseTabs(False)
  525. self.UsePopUp(True)
  526. self.SetSelBackground(True, "#FFFF00")
  527. self.SetUseHorizontalScrollBar(True)
  528. #
  529. # bindins
  530. #
  531. self.Bind(wx.EVT_WINDOW_DESTROY, self.OnDestroy)
  532. def OnDestroy(self, evt):
  533. """The clipboard contents can be preserved after
  534. the app has exited"""
  535. wx.TheClipboard.Flush()
  536. evt.Skip()
  537. def AddTextWrapped(self, txt, wrap=None):
  538. """Add string to text area.
  539. String is wrapped and linesep is also added to the end
  540. of the string"""
  541. if wrap is None and self.wrap:
  542. wrap = self.wrap
  543. if wrap is not None:
  544. txt = textwrap.fill(txt, wrap) + os.linesep
  545. else:
  546. txt += os.linesep
  547. self.AddText(txt)
  548. def SetWrap(self, wrap):
  549. """Set wrapping value
  550. @param wrap wrapping value
  551. @return current wrapping value
  552. """
  553. if wrap > 0:
  554. self.wrap = wrap
  555. return self.wrap