home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1805 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  13.8 KB  |  448 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __docformat__ = 'restructuredtext en'
  5. import wx
  6. import wx.stc as stc
  7. from wx.py import editwindow
  8. import time
  9. import sys
  10. import string
  11. LINESEP = '\n'
  12. if sys.platform == 'win32':
  13.     LINESEP = '\n\r'
  14.  
  15. import re
  16. _COMPLETE_BUFFER_MARKER = 31
  17. _ERROR_MARKER = 30
  18. _INPUT_MARKER = 29
  19. _DEFAULT_SIZE = 10
  20. if sys.platform == 'darwin':
  21.     _DEFAULT_SIZE = 12
  22.  
  23. _DEFAULT_STYLE = {
  24.     'default': 'size:%d' % _DEFAULT_SIZE,
  25.     'bracegood': 'fore:#00AA00,back:#000000,bold',
  26.     'bracebad': 'fore:#FF0000,back:#000000,bold',
  27.     'edge_column': -1,
  28.     'comment': 'fore:#007F00',
  29.     'number': 'fore:#007F7F',
  30.     'string': 'fore:#7F007F,italic',
  31.     'char': 'fore:#7F007F,italic',
  32.     'keyword': 'fore:#00007F,bold',
  33.     'triple': 'fore:#7F0000',
  34.     'tripledouble': 'fore:#7F0000',
  35.     'class': 'fore:#0000FF,bold,underline',
  36.     'def': 'fore:#007F7F,bold',
  37.     'operator': 'bold',
  38.     'trace': '#FAFAF1',
  39.     'stdout': '#FDFFD3',
  40.     'stderr': '#FFF1F1',
  41.     'antialiasing': True,
  42.     'carret_color': 'BLACK',
  43.     'background_color': 'WHITE',
  44.     'prompt_in1': '\n\x01\x1b[0;34m\x02In [\x01\x1b[1;34m\x02$number\x01\x1b[0;34m\x02]: \x01\x1b[0m\x02',
  45.     'prompt_out': '\x01\x1b[0;31m\x02Out[\x01\x1b[1;31m\x02$number\x01\x1b[0;31m\x02]: \x01\x1b[0m\x02' }
  46. _STDOUT_STYLE = 15
  47. _STDERR_STYLE = 16
  48. _TRACE_STYLE = 17
  49. ANSI_STYLES = {
  50.     '0;30': [
  51.         0,
  52.         'BLACK'],
  53.     '0;31': [
  54.         1,
  55.         'RED'],
  56.     '0;32': [
  57.         2,
  58.         'GREEN'],
  59.     '0;33': [
  60.         3,
  61.         'BROWN'],
  62.     '0;34': [
  63.         4,
  64.         'BLUE'],
  65.     '0;35': [
  66.         5,
  67.         'PURPLE'],
  68.     '0;36': [
  69.         6,
  70.         'CYAN'],
  71.     '0;37': [
  72.         7,
  73.         'LIGHT GREY'],
  74.     '1;30': [
  75.         8,
  76.         'DARK GREY'],
  77.     '1;31': [
  78.         9,
  79.         'RED'],
  80.     '1;32': [
  81.         10,
  82.         'SEA GREEN'],
  83.     '1;33': [
  84.         11,
  85.         'YELLOW'],
  86.     '1;34': [
  87.         12,
  88.         'LIGHT BLUE'],
  89.     '1;35': [
  90.         13,
  91.         'MEDIUM VIOLET RED'],
  92.     '1;36': [
  93.         14,
  94.         'LIGHT STEEL BLUE'],
  95.     '1;37': [
  96.         15,
  97.         'YELLOW'] }
  98. if wx.Platform == '__WXMSW__':
  99.     FACES = {
  100.         'times': 'Times New Roman',
  101.         'mono': 'Courier New',
  102.         'helv': 'Arial',
  103.         'other': 'Comic Sans MS',
  104.         'size': 10,
  105.         'size2': 8 }
  106. elif wx.Platform == '__WXMAC__':
  107.     FACES = {
  108.         'times': 'Times New Roman',
  109.         'mono': 'Monaco',
  110.         'helv': 'Arial',
  111.         'other': 'Comic Sans MS',
  112.         'size': 10,
  113.         'size2': 8 }
  114. else:
  115.     FACES = {
  116.         'times': 'Times',
  117.         'mono': 'Courier',
  118.         'helv': 'Helvetica',
  119.         'other': 'new century schoolbook',
  120.         'size': 10,
  121.         'size2': 8 }
  122.  
  123. class ConsoleWidget(editwindow.EditWindow):
  124.     title = 'Console'
  125.     last_prompt = ''
  126.     
  127.     def _set_input_buffer(self, string):
  128.         self.SetSelection(self.current_prompt_pos, self.GetLength())
  129.         self.ReplaceSelection(string)
  130.         self.GotoPos(self.GetLength())
  131.  
  132.     
  133.     def _get_input_buffer(self):
  134.         input_buffer = self.GetTextRange(self.current_prompt_pos, self.GetLength())
  135.         input_buffer = input_buffer.replace(LINESEP, '\n')
  136.         return input_buffer
  137.  
  138.     input_buffer = property(_get_input_buffer, _set_input_buffer)
  139.     style = _DEFAULT_STYLE.copy()
  140.     ANSI_STYLES = ANSI_STYLES.copy()
  141.     faces = FACES.copy()
  142.     _last_refresh_time = 0
  143.     
  144.     def __init__(self, parent, id = wx.ID_ANY, pos = wx.DefaultPosition, size = wx.DefaultSize, style = wx.WANTS_CHARS):
  145.         editwindow.EditWindow.__init__(self, parent, id, pos, size, style)
  146.         self.configure_scintilla()
  147.         self.enter_catched = False
  148.         self.current_prompt_pos = 0
  149.         self.Bind(wx.EVT_KEY_DOWN, self._on_key_down)
  150.         self.Bind(wx.EVT_KEY_UP, self._on_key_up)
  151.  
  152.     
  153.     def write(self, text, refresh = True):
  154.         title = self.title_pat.split(text)
  155.         if len(title) > 1:
  156.             self.title = title[-2]
  157.         
  158.         text = self.title_pat.sub('', text)
  159.         segments = self.color_pat.split(text)
  160.         segment = segments.pop(0)
  161.         self.GotoPos(self.GetLength())
  162.         self.StartStyling(self.GetLength(), 255)
  163.         
  164.         try:
  165.             self.AppendText(segment)
  166.         except UnicodeDecodeError:
  167.             pass
  168.  
  169.         if segments:
  170.             for ansi_tag, text in zip(segments[::2], segments[1::2]):
  171.                 self.StartStyling(self.GetLength(), 255)
  172.                 
  173.                 try:
  174.                     self.AppendText(text)
  175.                 except UnicodeDecodeError:
  176.                     pass
  177.  
  178.                 if ansi_tag not in self.ANSI_STYLES:
  179.                     style = 0
  180.                 else:
  181.                     style = self.ANSI_STYLES[ansi_tag][0]
  182.                 self.SetStyling(len(text), style)
  183.             
  184.         
  185.         self.GotoPos(self.GetLength())
  186.         if refresh:
  187.             current_time = time.time()
  188.             if current_time - self._last_refresh_time > 0.03:
  189.                 if sys.platform == 'win32':
  190.                     wx.SafeYield()
  191.                 else:
  192.                     wx.Yield()
  193.                 self._last_refresh_time = current_time
  194.             
  195.         
  196.  
  197.     
  198.     def new_prompt(self, prompt):
  199.         self.write(prompt, refresh = False)
  200.         self.current_prompt_pos = self.GetLength()
  201.         self.current_prompt_line = self.GetCurrentLine()
  202.         self.EnsureCaretVisible()
  203.         self.last_prompt = prompt
  204.  
  205.     
  206.     def continuation_prompt(self):
  207.         ascii_less = ''.join(self.color_pat.split(self.last_prompt)[2::2])
  208.         return '.' * (len(ascii_less) - 2) + ': '
  209.  
  210.     
  211.     def scroll_to_bottom(self):
  212.         maxrange = self.GetScrollRange(wx.VERTICAL)
  213.         self.ScrollLines(maxrange)
  214.  
  215.     
  216.     def pop_completion(self, possibilities, offset = 0):
  217.         self.AutoCompSetIgnoreCase(False)
  218.         self.AutoCompSetAutoHide(False)
  219.         self.AutoCompSetMaxHeight(len(possibilities))
  220.         self.AutoCompShow(offset, ' '.join(possibilities))
  221.  
  222.     
  223.     def get_line_width(self):
  224.         return self.GetSize()[0] / self.GetCharWidth()
  225.  
  226.     
  227.     def configure_scintilla(self):
  228.         p = self.style.copy()
  229.         self.MarkerDefine(_COMPLETE_BUFFER_MARKER, stc.STC_MARK_BACKGROUND, background = p['trace'])
  230.         self.MarkerDefine(_INPUT_MARKER, stc.STC_MARK_BACKGROUND, background = p['stdout'])
  231.         self.MarkerDefine(_ERROR_MARKER, stc.STC_MARK_BACKGROUND, background = p['stderr'])
  232.         self.SetEOLMode(stc.STC_EOL_LF)
  233.         self.CmdKeyAssign(ord('+'), stc.STC_SCMOD_CTRL, stc.STC_CMD_ZOOMIN)
  234.         self.CmdKeyAssign(ord('-'), stc.STC_SCMOD_CTRL, stc.STC_CMD_ZOOMOUT)
  235.         self.CmdKeyAssign(ord('='), stc.STC_SCMOD_CTRL | stc.STC_SCMOD_SHIFT, stc.STC_CMD_ZOOMIN)
  236.         self.CmdKeyClear(ord('D'), stc.STC_SCMOD_CTRL)
  237.         self.CmdKeyClear(ord('L'), stc.STC_SCMOD_CTRL)
  238.         self.CmdKeyClear(ord('T'), stc.STC_SCMOD_CTRL)
  239.         self.CmdKeyClear(ord('A'), stc.STC_SCMOD_CTRL)
  240.         self.SetEOLMode(stc.STC_EOL_CRLF)
  241.         self.SetWrapMode(stc.STC_WRAP_CHAR)
  242.         self.SetWrapMode(stc.STC_WRAP_WORD)
  243.         self.SetBufferedDraw(True)
  244.         self.SetUseAntiAliasing(p['antialiasing'])
  245.         self.SetLayoutCache(stc.STC_CACHE_PAGE)
  246.         self.SetUndoCollection(False)
  247.         self.SetUseTabs(True)
  248.         self.SetIndent(4)
  249.         self.SetTabWidth(4)
  250.         self.AutoCompSetChooseSingle(False)
  251.         self.AutoCompSetMaxHeight(10)
  252.         self.AutoCompSetFillUps('\n')
  253.         self.SetMargins(3, 3)
  254.         self.SetMarginWidth(0, 0)
  255.         self.SetMarginWidth(1, 0)
  256.         self.SetMarginWidth(2, 0)
  257.         self.color_pat = re.compile('\x01?\x1b\\[(.*?)m\x02?')
  258.         self.title_pat = re.compile('\x1b]0;(.*?)\x07')
  259.         self.SetCaretForeground(p['carret_color'])
  260.         background_color = p['background_color']
  261.         if 'default' in p:
  262.             if 'back' not in p['default']:
  263.                 p['default'] += ',back:%s' % background_color
  264.             
  265.             if 'size' not in p['default']:
  266.                 p['default'] += ',size:%s' % self.faces['size']
  267.             
  268.             if 'face' not in p['default']:
  269.                 p['default'] += ',face:%s' % self.faces['mono']
  270.             
  271.             self.StyleSetSpec(stc.STC_STYLE_DEFAULT, p['default'])
  272.         else:
  273.             self.StyleSetSpec(stc.STC_STYLE_DEFAULT, 'fore:%s,back:%s,size:%d,face:%s' % (self.ANSI_STYLES['0;30'][1], background_color, self.faces['size'], self.faces['mono']))
  274.         self.StyleClearAll()
  275.         self.prompt_in1 = p['prompt_in1']
  276.         self.prompt_out = p['prompt_out']
  277.         self.output_prompt_template = string.Template(self.prompt_out)
  278.         self.input_prompt_template = string.Template(self.prompt_in1)
  279.         self.StyleSetSpec(_STDOUT_STYLE, p['stdout'])
  280.         self.StyleSetSpec(_STDERR_STYLE, p['stderr'])
  281.         self.StyleSetSpec(_TRACE_STYLE, p['trace'])
  282.         self.StyleSetSpec(stc.STC_STYLE_BRACELIGHT, p['bracegood'])
  283.         self.StyleSetSpec(stc.STC_STYLE_BRACEBAD, p['bracebad'])
  284.         self.StyleSetSpec(stc.STC_P_COMMENTLINE, p['comment'])
  285.         self.StyleSetSpec(stc.STC_P_NUMBER, p['number'])
  286.         self.StyleSetSpec(stc.STC_P_STRING, p['string'])
  287.         self.StyleSetSpec(stc.STC_P_CHARACTER, p['char'])
  288.         self.StyleSetSpec(stc.STC_P_WORD, p['keyword'])
  289.         self.StyleSetSpec(stc.STC_P_WORD2, p['keyword'])
  290.         self.StyleSetSpec(stc.STC_P_TRIPLE, p['triple'])
  291.         self.StyleSetSpec(stc.STC_P_TRIPLEDOUBLE, p['tripledouble'])
  292.         self.StyleSetSpec(stc.STC_P_CLASSNAME, p['class'])
  293.         self.StyleSetSpec(stc.STC_P_DEFNAME, p['def'])
  294.         self.StyleSetSpec(stc.STC_P_OPERATOR, p['operator'])
  295.         self.StyleSetSpec(stc.STC_P_COMMENTBLOCK, p['comment'])
  296.         edge_column = p['edge_column']
  297.         if edge_column is not None and edge_column > 0:
  298.             self.SetEdgeMode(stc.STC_EDGE_LINE)
  299.             self.SetEdgeColumn(edge_column)
  300.         
  301.  
  302.     
  303.     def OnUpdateUI(self, event):
  304.         pass
  305.  
  306.     
  307.     def _on_key_down(self, event, skip = True):
  308.         catched = True
  309.         key_code = event.GetKeyCode()
  310.         if key_code == ord('L') and event.ControlDown():
  311.             self.scroll_to_bottom()
  312.         elif key_code == ord('K') and event.ControlDown():
  313.             self.input_buffer = ''
  314.         elif key_code == ord('A') and event.ControlDown():
  315.             self.GotoPos(self.GetLength())
  316.             self.SetSelectionStart(self.current_prompt_pos)
  317.             self.SetSelectionEnd(self.GetCurrentPos())
  318.             catched = True
  319.         elif key_code == ord('E') and event.ControlDown():
  320.             self.GotoPos(self.GetLength())
  321.             catched = True
  322.         elif key_code == wx.WXK_PAGEUP:
  323.             self.ScrollPages(-1)
  324.         elif key_code == wx.WXK_PAGEDOWN:
  325.             self.ScrollPages(1)
  326.         elif key_code == wx.WXK_HOME:
  327.             self.GotoPos(self.GetLength())
  328.         elif key_code == wx.WXK_END:
  329.             self.GotoPos(self.GetLength())
  330.         elif key_code == wx.WXK_UP and event.ShiftDown():
  331.             self.ScrollLines(-1)
  332.         elif key_code == wx.WXK_DOWN and event.ShiftDown():
  333.             self.ScrollLines(1)
  334.         else:
  335.             catched = False
  336.         if self.AutoCompActive():
  337.             event.Skip()
  338.         elif key_code in (13, wx.WXK_NUMPAD_ENTER):
  339.             catched = True
  340.             if not self.enter_catched:
  341.                 self.CallTipCancel()
  342.                 if event.ShiftDown():
  343.                     self.GotoPos(self.GetLength())
  344.                     self.write('\n' + self.continuation_prompt(), refresh = False)
  345.                     self._on_enter()
  346.                 else:
  347.                     self._on_enter()
  348.                 self.enter_catched = True
  349.             
  350.         elif key_code == wx.WXK_HOME:
  351.             if not event.ShiftDown():
  352.                 self.GotoPos(self.current_prompt_pos)
  353.                 catched = True
  354.             else:
  355.                 self.SetSelectionStart(self.current_prompt_pos)
  356.                 self.SetSelectionEnd(self.GetCurrentPos())
  357.                 catched = True
  358.         elif key_code == wx.WXK_UP:
  359.             if self.GetCurrentLine() > self.current_prompt_line:
  360.                 if self.GetCurrentLine() == self.current_prompt_line + 1 and self.GetColumn(self.GetCurrentPos()) < self.GetColumn(self.current_prompt_pos):
  361.                     self.GotoPos(self.current_prompt_pos)
  362.                 else:
  363.                     event.Skip()
  364.             
  365.             catched = True
  366.         elif key_code in (wx.WXK_LEFT, wx.WXK_BACK):
  367.             if not self._keep_cursor_in_buffer(self.GetCurrentPos() - 1):
  368.                 event.Skip()
  369.             
  370.             catched = True
  371.         elif key_code == wx.WXK_RIGHT:
  372.             if not self._keep_cursor_in_buffer(self.GetCurrentPos() + 1):
  373.                 event.Skip()
  374.             
  375.             catched = True
  376.         elif key_code == wx.WXK_DELETE:
  377.             if not self._keep_cursor_in_buffer(self.GetCurrentPos() - 1):
  378.                 event.Skip()
  379.             
  380.             catched = True
  381.         
  382.         if skip and not catched:
  383.             if not self._keep_cursor_in_buffer():
  384.                 if not self.GetCurrentPos() == self.GetLength() and key_code == wx.WXK_DELETE:
  385.                     event.Skip()
  386.                 
  387.                 catched = True
  388.             
  389.         
  390.         return catched
  391.  
  392.     
  393.     def _on_key_up(self, event, skip = True):
  394.         if skip:
  395.             event.Skip()
  396.         
  397.         self._keep_cursor_in_buffer()
  398.  
  399.     
  400.     def _keep_cursor_in_buffer(self, pos = None):
  401.         if pos is None:
  402.             current_pos = self.GetCurrentPos()
  403.         else:
  404.             current_pos = pos
  405.         if current_pos < self.current_prompt_pos:
  406.             self.GotoPos(self.current_prompt_pos)
  407.             return True
  408.         line_num = self.LineFromPosition(current_pos)
  409.         if not current_pos > self.GetLength():
  410.             line_pos = self.GetColumn(current_pos)
  411.         else:
  412.             line_pos = self.GetColumn(self.GetLength())
  413.         line = self.GetLine(line_num)
  414.         continuation_prompt = self.continuation_prompt()
  415.         if line.startswith(continuation_prompt) and line_pos < len(continuation_prompt):
  416.             if line_pos < 2:
  417.                 self.GotoPos(current_pos + 1 + len(continuation_prompt) - line_pos)
  418.             else:
  419.                 self.GotoPos(self.GetLineEndPosition(line_num - 1))
  420.             return True
  421.         if current_pos > self.GetLineEndPosition(line_num) and not (current_pos == self.GetLength()):
  422.             self.GotoPos(current_pos + 1 + len(continuation_prompt))
  423.             return True
  424.         self.enter_catched = False
  425.         return False
  426.  
  427.  
  428. if __name__ == '__main__':
  429.     
  430.     class MainWindow(wx.Frame):
  431.         
  432.         def __init__(self, parent, id, title):
  433.             wx.Frame.__init__(self, parent, id, title, size = (300, 250))
  434.             self._sizer = wx.BoxSizer(wx.VERTICAL)
  435.             self.console_widget = ConsoleWidget(self)
  436.             self._sizer.Add(self.console_widget, 1, wx.EXPAND)
  437.             self.SetSizer(self._sizer)
  438.             self.SetAutoLayout(1)
  439.             self.Show(True)
  440.  
  441.  
  442.     app = wx.PySimpleApp()
  443.     w = MainWindow(None, wx.ID_ANY, 'ConsoleWidget')
  444.     w.SetSize((780, 460))
  445.     w.Show()
  446.     app.MainLoop()
  447.  
  448.