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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __version__ = 0.9
  5. __author__ = 'Laurent Dufrechou'
  6. __email__ = 'laurent.dufrechou _at_ gmail.com'
  7. __license__ = 'BSD'
  8. import wx
  9. import wx.stc as stc
  10. import re
  11. from StringIO import StringIO
  12. import sys
  13. import codecs
  14. import locale
  15. import time
  16. for enc in (locale.getpreferredencoding(), sys.getfilesystemencoding(), sys.getdefaultencoding()):
  17.     
  18.     try:
  19.         codecs.lookup(enc)
  20.         ENCODING = enc
  21.     except LookupError:
  22.         pass
  23.  
  24. else:
  25.     ENCODING = 'utf-8'
  26. from ipshell_nonblocking import NonBlockingIPShell
  27.  
  28. class WxNonBlockingIPShell(NonBlockingIPShell):
  29.     
  30.     def __init__(self, parent, argv = [], user_ns = { }, user_global_ns = None, cin = None, cout = None, cerr = None, ask_exit_handler = None):
  31.         NonBlockingIPShell.__init__(self, argv, user_ns, user_global_ns, cin, cout, cerr, ask_exit_handler)
  32.         self.parent = parent
  33.         self.ask_exit_callback = ask_exit_handler
  34.         self._IP.exit = self._ask_exit
  35.  
  36.     
  37.     def addGUIShortcut(self, text, func):
  38.         wx.CallAfter(self.parent.add_button_handler, button_info = {
  39.             'text': text,
  40.             'func': self.parent.doExecuteLine(func) })
  41.  
  42.     
  43.     def _raw_input(self, prompt = ''):
  44.         self.answer = None
  45.         if self._threading == True:
  46.             wx.CallAfter(self._yesNoBox, prompt)
  47.             while self.answer is None:
  48.                 time.sleep(0.1)
  49.         else:
  50.             self._yesNoBox(prompt)
  51.         return self.answer
  52.  
  53.     
  54.     def _yesNoBox(self, prompt):
  55.         dlg = wx.TextEntryDialog(self.parent, prompt, 'Input requested', 'Python')
  56.         dlg.SetValue('')
  57.         answer = ''
  58.         if dlg.ShowModal() == wx.ID_OK:
  59.             answer = dlg.GetValue()
  60.         
  61.         dlg.Destroy()
  62.         self.answer = answer
  63.  
  64.     
  65.     def _ask_exit(self):
  66.         wx.CallAfter(self.ask_exit_callback, ())
  67.  
  68.     
  69.     def _after_execute(self):
  70.         wx.CallAfter(self.parent.evtStateExecuteDone, ())
  71.  
  72.  
  73.  
  74. class WxConsoleView(stc.StyledTextCtrl):
  75.     ANSI_STYLES_BLACK = {
  76.         '0;30': [
  77.             0,
  78.             'WHITE'],
  79.         '0;31': [
  80.             1,
  81.             'RED'],
  82.         '0;32': [
  83.             2,
  84.             'GREEN'],
  85.         '0;33': [
  86.             3,
  87.             'BROWN'],
  88.         '0;34': [
  89.             4,
  90.             'BLUE'],
  91.         '0;35': [
  92.             5,
  93.             'PURPLE'],
  94.         '0;36': [
  95.             6,
  96.             'CYAN'],
  97.         '0;37': [
  98.             7,
  99.             'LIGHT GREY'],
  100.         '1;30': [
  101.             8,
  102.             'DARK GREY'],
  103.         '1;31': [
  104.             9,
  105.             'RED'],
  106.         '1;32': [
  107.             10,
  108.             'SEA GREEN'],
  109.         '1;33': [
  110.             11,
  111.             'YELLOW'],
  112.         '1;34': [
  113.             12,
  114.             'LIGHT BLUE'],
  115.         '1;35': [
  116.             13,
  117.             'MEDIUM VIOLET RED'],
  118.         '1;36': [
  119.             14,
  120.             'LIGHT STEEL BLUE'],
  121.         '1;37': [
  122.             15,
  123.             'YELLOW'] }
  124.     ANSI_STYLES_WHITE = {
  125.         '0;30': [
  126.             0,
  127.             'BLACK'],
  128.         '0;31': [
  129.             1,
  130.             'RED'],
  131.         '0;32': [
  132.             2,
  133.             'GREEN'],
  134.         '0;33': [
  135.             3,
  136.             'BROWN'],
  137.         '0;34': [
  138.             4,
  139.             'BLUE'],
  140.         '0;35': [
  141.             5,
  142.             'PURPLE'],
  143.         '0;36': [
  144.             6,
  145.             'CYAN'],
  146.         '0;37': [
  147.             7,
  148.             'LIGHT GREY'],
  149.         '1;30': [
  150.             8,
  151.             'DARK GREY'],
  152.         '1;31': [
  153.             9,
  154.             'RED'],
  155.         '1;32': [
  156.             10,
  157.             'SEA GREEN'],
  158.         '1;33': [
  159.             11,
  160.             'YELLOW'],
  161.         '1;34': [
  162.             12,
  163.             'LIGHT BLUE'],
  164.         '1;35': [
  165.             13,
  166.             'MEDIUM VIOLET RED'],
  167.         '1;36': [
  168.             14,
  169.             'LIGHT STEEL BLUE'],
  170.         '1;37': [
  171.             15,
  172.             'YELLOW'] }
  173.     
  174.     def __init__(self, parent, prompt, intro = '', background_color = 'BLACK', pos = wx.DefaultPosition, ID = -1, size = wx.DefaultSize, style = 0, autocomplete_mode = 'IPYTHON'):
  175.         stc.StyledTextCtrl.__init__(self, parent, ID, pos, size, style)
  176.         self.CmdKeyAssign(ord('B'), stc.STC_SCMOD_CTRL, stc.STC_CMD_ZOOMIN)
  177.         self.CmdKeyAssign(ord('N'), stc.STC_SCMOD_CTRL, stc.STC_CMD_ZOOMOUT)
  178.         self.SetEdgeMode(stc.STC_EDGE_LINE)
  179.         self.SetEdgeColumn(80)
  180.         self.SetEdgeColour(wx.LIGHT_GREY)
  181.         self.SetEOLMode(stc.STC_EOL_CRLF)
  182.         self.SetBufferedDraw(True)
  183.         self.SetLayoutCache(stc.STC_CACHE_PAGE)
  184.         self.SetUndoCollection(False)
  185.         self.SetUseTabs(True)
  186.         self.SetIndent(4)
  187.         self.SetTabWidth(4)
  188.         self.EnsureCaretVisible()
  189.         self.SetMargins(3, 3)
  190.         self.SetMarginWidth(0, 0)
  191.         self.SetMarginWidth(1, 0)
  192.         self.SetMarginWidth(2, 0)
  193.         self.background_color = background_color
  194.         self.buildStyles()
  195.         self.indent = 0
  196.         self.prompt_count = 0
  197.         self.color_pat = re.compile('\x01?\x1b\\[(.*?)m\x02?')
  198.         self.write(intro)
  199.         self.setPrompt(prompt)
  200.         self.showPrompt()
  201.         self.autocomplete_mode = autocomplete_mode
  202.         self.Bind(wx.EVT_KEY_DOWN, self._onKeypress)
  203.  
  204.     
  205.     def buildStyles(self):
  206.         if wx.Platform == '__WXMSW__':
  207.             faces = {
  208.                 'times': 'Times New Roman',
  209.                 'mono': 'Courier New',
  210.                 'helv': 'Arial',
  211.                 'other': 'Comic Sans MS',
  212.                 'size': 10,
  213.                 'size2': 8 }
  214.         elif wx.Platform == '__WXMAC__':
  215.             faces = {
  216.                 'times': 'Times New Roman',
  217.                 'mono': 'Monaco',
  218.                 'helv': 'Arial',
  219.                 'other': 'Comic Sans MS',
  220.                 'size': 10,
  221.                 'size2': 8 }
  222.         else:
  223.             faces = {
  224.                 'times': 'Times',
  225.                 'mono': 'Courier',
  226.                 'helv': 'Helvetica',
  227.                 'other': 'new century schoolbook',
  228.                 'size': 10,
  229.                 'size2': 8 }
  230.         if self.background_color != 'BLACK':
  231.             self.background_color = 'WHITE'
  232.             self.SetCaretForeground('BLACK')
  233.             self.ANSI_STYLES = self.ANSI_STYLES_WHITE
  234.         else:
  235.             self.SetCaretForeground('WHITE')
  236.             self.ANSI_STYLES = self.ANSI_STYLES_BLACK
  237.         self.StyleSetSpec(stc.STC_STYLE_DEFAULT, 'fore:%s,back:%s,size:%d,face:%s' % (self.ANSI_STYLES['0;30'][1], self.background_color, faces['size'], faces['mono']))
  238.         self.StyleClearAll()
  239.         self.StyleSetSpec(stc.STC_STYLE_BRACELIGHT, 'fore:#FF0000,back:#0000FF,bold')
  240.         self.StyleSetSpec(stc.STC_STYLE_BRACEBAD, 'fore:#000000,back:#FF0000,bold')
  241.         for style in self.ANSI_STYLES.values():
  242.             self.StyleSetSpec(style[0], 'bold,fore:%s' % style[1])
  243.         
  244.  
  245.     
  246.     def setBackgroundColor(self, color):
  247.         self.background_color = color
  248.         self.buildStyles()
  249.  
  250.     
  251.     def getBackgroundColor(self, color):
  252.         return self.background_color
  253.  
  254.     
  255.     def asyncWrite(self, text):
  256.         
  257.         try:
  258.             wx.MutexGuiEnter()
  259.             self.write(text)
  260.         except KeyboardInterrupt:
  261.             wx.MutexGuiLeave()
  262.             raise KeyboardInterrupt
  263.  
  264.         wx.MutexGuiLeave()
  265.  
  266.     
  267.     def write(self, text):
  268.         segments = self.color_pat.split(text)
  269.         segment = segments.pop(0)
  270.         self.StartStyling(self.getCurrentLineEnd(), 255)
  271.         self.AppendText(segment)
  272.         if segments:
  273.             ansi_tags = self.color_pat.findall(text)
  274.             for tag in ansi_tags:
  275.                 i = segments.index(tag)
  276.                 self.StartStyling(self.getCurrentLineEnd(), 255)
  277.                 self.AppendText(segments[i + 1])
  278.                 if tag != '0':
  279.                     self.SetStyling(len(segments[i + 1]), self.ANSI_STYLES[tag][0])
  280.                 
  281.                 segments.pop(i)
  282.             
  283.         
  284.         self.moveCursor(self.getCurrentLineEnd())
  285.  
  286.     
  287.     def getPromptLen(self):
  288.         return len(str(self.prompt_count)) + 7
  289.  
  290.     
  291.     def setPrompt(self, prompt):
  292.         self.prompt = prompt
  293.  
  294.     
  295.     def setIndentation(self, indentation):
  296.         self.indent = indentation
  297.  
  298.     
  299.     def setPromptCount(self, count):
  300.         self.prompt_count = count
  301.  
  302.     
  303.     def showPrompt(self):
  304.         self.write(self.prompt)
  305.         self.current_start = self.getCurrentLineEnd()
  306.         autoindent = self.indent * ' '
  307.         autoindent = autoindent.replace('    ', '\t')
  308.         self.write(autoindent)
  309.  
  310.     
  311.     def changeLine(self, text):
  312.         self.SetSelection(self.getCurrentPromptStart(), self.getCurrentLineEnd())
  313.         self.ReplaceSelection(text)
  314.         self.moveCursor(self.getCurrentLineEnd())
  315.  
  316.     
  317.     def getCurrentPromptStart(self):
  318.         return self.current_start
  319.  
  320.     
  321.     def getCurrentLineStart(self):
  322.         return self.GotoLine(self.LineFromPosition(self.GetCurrentPos()))
  323.  
  324.     
  325.     def getCurrentLineEnd(self):
  326.         return self.GetLength()
  327.  
  328.     
  329.     def getCurrentLine(self):
  330.         return self.GetTextRange(self.getCurrentPromptStart(), self.getCurrentLineEnd())
  331.  
  332.     
  333.     def moveCursorOnNewValidKey(self):
  334.         if self.GetCurrentPos() < self.getCurrentPromptStart():
  335.             self.GotoPos(self.getCurrentPromptStart())
  336.         
  337.  
  338.     
  339.     def removeFromTo(self, from_pos, to_pos):
  340.         if from_pos < to_pos:
  341.             self.SetSelection(from_pos, to_pos)
  342.             self.DeleteBack()
  343.         
  344.  
  345.     
  346.     def removeCurrentLine(self):
  347.         self.LineDelete()
  348.  
  349.     
  350.     def moveCursor(self, position):
  351.         self.GotoPos(position)
  352.  
  353.     
  354.     def getCursorPos(self):
  355.         return self.GetCurrentPos()
  356.  
  357.     
  358.     def selectFromTo(self, from_pos, to_pos):
  359.         self.SetSelectionStart(from_pos)
  360.         self.SetSelectionEnd(to_pos)
  361.  
  362.     
  363.     def writeHistory(self, history):
  364.         self.removeFromTo(self.getCurrentPromptStart(), self.getCurrentLineEnd())
  365.         self.changeLine(history)
  366.  
  367.     
  368.     def setCompletionMethod(self, completion):
  369.         if completion in ('IPYTHON', 'STC'):
  370.             self.autocomplete_mode = completion
  371.         else:
  372.             raise AttributeError
  373.         return completion in ('IPYTHON', 'STC')
  374.  
  375.     
  376.     def getCompletionMethod(self, completion):
  377.         return self.autocomplete_mode
  378.  
  379.     
  380.     def writeCompletion(self, possibilities):
  381.         if self.autocomplete_mode == 'IPYTHON':
  382.             max_len = len(max(possibilities, key = len))
  383.             max_symbol = ' ' * max_len
  384.             test_buffer = max_symbol + '    '
  385.             allowed_symbols = 80 / len(test_buffer)
  386.             if allowed_symbols == 0:
  387.                 allowed_symbols = 1
  388.             
  389.             pos = 1
  390.             buf = ''
  391.             for symbol in possibilities:
  392.                 if pos < allowed_symbols:
  393.                     spaces = (max_len - len(symbol)) + 4
  394.                     buf += symbol + ' ' * spaces
  395.                     pos += 1
  396.                     continue
  397.                 buf += symbol + '\n'
  398.                 pos = 1
  399.             
  400.             self.write(buf)
  401.         else:
  402.             possibilities.sort()
  403.             self.AutoCompSetIgnoreCase(False)
  404.             self.AutoCompSetAutoHide(False)
  405.             splitter = [
  406.                 ' ',
  407.                 '(',
  408.                 '[',
  409.                 '{',
  410.                 '=']
  411.             last_word = self.getCurrentLine()
  412.             for breaker in splitter:
  413.                 last_word = last_word.split(breaker)[-1]
  414.             
  415.             self.AutoCompShow(len(last_word), ' '.join(possibilities))
  416.  
  417.     
  418.     def _onKeypress(self, event, skip = True):
  419.         if not self.AutoCompActive():
  420.             if event.GetKeyCode() == wx.WXK_HOME:
  421.                 if event.Modifiers == wx.MOD_NONE:
  422.                     self.moveCursorOnNewValidKey()
  423.                     self.moveCursor(self.getCurrentPromptStart())
  424.                     return True
  425.                 if event.Modifiers == wx.MOD_SHIFT:
  426.                     self.moveCursorOnNewValidKey()
  427.                     self.selectFromTo(self.getCurrentPromptStart(), self.getCursorPos())
  428.                     return True
  429.                 return False
  430.             event.GetKeyCode() == wx.WXK_HOME
  431.             if event.GetKeyCode() == wx.WXK_LEFT:
  432.                 if event.Modifiers == wx.MOD_NONE:
  433.                     self.moveCursorOnNewValidKey()
  434.                     self.moveCursor(self.getCursorPos() - 1)
  435.                     if self.getCursorPos() < self.getCurrentPromptStart():
  436.                         self.moveCursor(self.getCurrentPromptStart())
  437.                     
  438.                     return True
  439.             elif event.GetKeyCode() == wx.WXK_BACK:
  440.                 self.moveCursorOnNewValidKey()
  441.                 if self.getCursorPos() > self.getCurrentPromptStart():
  442.                     event.Skip()
  443.                 
  444.                 return True
  445.             if skip:
  446.                 if event.GetKeyCode() not in [
  447.                     wx.WXK_PAGEUP,
  448.                     wx.WXK_PAGEDOWN] and event.Modifiers == wx.MOD_NONE:
  449.                     self.moveCursorOnNewValidKey()
  450.                 
  451.                 event.Skip()
  452.                 return True
  453.             return False
  454.         event.Skip()
  455.  
  456.     
  457.     def OnUpdateUI(self, evt):
  458.         braceAtCaret = -1
  459.         braceOpposite = -1
  460.         charBefore = None
  461.         caretPos = self.GetCurrentPos()
  462.         if caretPos > 0:
  463.             charBefore = self.GetCharAt(caretPos - 1)
  464.             styleBefore = self.GetStyleAt(caretPos - 1)
  465.         
  466.         if charBefore and chr(charBefore) in '[]{}()' and styleBefore == stc.STC_P_OPERATOR:
  467.             braceAtCaret = caretPos - 1
  468.         
  469.         if braceAtCaret < 0:
  470.             charAfter = self.GetCharAt(caretPos)
  471.             styleAfter = self.GetStyleAt(caretPos)
  472.             if charAfter and chr(charAfter) in '[]{}()' and styleAfter == stc.STC_P_OPERATOR:
  473.                 braceAtCaret = caretPos
  474.             
  475.         
  476.         if braceAtCaret >= 0:
  477.             braceOpposite = self.BraceMatch(braceAtCaret)
  478.         
  479.         if braceAtCaret != -1 and braceOpposite == -1:
  480.             self.BraceBadLight(braceAtCaret)
  481.         else:
  482.             self.BraceHighlight(braceAtCaret, braceOpposite)
  483.  
  484.  
  485.  
  486. class IPShellWidget(wx.Panel):
  487.     
  488.     def __init__(self, parent, intro = None, background_color = 'BLACK', add_button_handler = None, wx_ip_shell = None, user_ns = { }, user_global_ns = None):
  489.         wx.Panel.__init__(self, parent, wx.ID_ANY)
  490.         self.parent = parent
  491.         self.cout = StringIO()
  492.         self.add_button_handler = add_button_handler
  493.         if wx_ip_shell is not None:
  494.             self.IP = wx_ip_shell
  495.         else:
  496.             self.IP = WxNonBlockingIPShell(self, cout = self.cout, cerr = self.cout, ask_exit_handler = self.askExitCallback)
  497.         if intro is None:
  498.             welcome_text = 'Welcome to WxIPython Shell.\n\n'
  499.             welcome_text += self.IP.get_banner()
  500.             welcome_text += '!command  -> Execute command in shell\n'
  501.             welcome_text += 'TAB       -> Autocompletion\n'
  502.         else:
  503.             welcome_text = intro
  504.         self.text_ctrl = WxConsoleView(self, self.IP.get_prompt(), intro = welcome_text, background_color = background_color)
  505.         option_text = wx.StaticText(self, -1, 'Options:')
  506.         self.completion_option = wx.CheckBox(self, -1, 'Scintilla Completion')
  507.         self.completion_option.SetToolTip(wx.ToolTip('Selects the completion type:\nEither Ipython default style or Scintilla one'))
  508.         self.background_option = wx.CheckBox(self, -1, 'White Background')
  509.         self.background_option.SetToolTip(wx.ToolTip('Selects the back ground color: BLACK or WHITE'))
  510.         self.threading_option = wx.CheckBox(self, -1, 'Execute in thread')
  511.         self.threading_option.SetToolTip(wx.ToolTip("Use threading: infinite loop don't freeze the GUI and commands can be breaked\nNo  threading: maximum compatibility"))
  512.         self.options = {
  513.             'completion': {
  514.                 'value': 'IPYTHON',
  515.                 'checkbox': self.completion_option,
  516.                 'STC': True,
  517.                 'IPYTHON': False,
  518.                 'setfunc': self.text_ctrl.setCompletionMethod },
  519.             'background_color': {
  520.                 'value': 'BLACK',
  521.                 'checkbox': self.background_option,
  522.                 'WHITE': True,
  523.                 'BLACK': False,
  524.                 'setfunc': self.text_ctrl.setBackgroundColor },
  525.             'threading': {
  526.                 'value': 'True',
  527.                 'checkbox': self.threading_option,
  528.                 'True': True,
  529.                 'False': False,
  530.                 'setfunc': self.IP.set_threading } }
  531.         self.cout.write = self.text_ctrl.asyncWrite
  532.         self.reloadOptions(self.options)
  533.         self.text_ctrl.Bind(wx.EVT_KEY_DOWN, self.keyPress)
  534.         self.completion_option.Bind(wx.EVT_CHECKBOX, self.evtCheckOptionCompletion)
  535.         self.background_option.Bind(wx.EVT_CHECKBOX, self.evtCheckOptionBackgroundColor)
  536.         self.threading_option.Bind(wx.EVT_CHECKBOX, self.evtCheckOptionThreading)
  537.         sizer = wx.BoxSizer(wx.VERTICAL)
  538.         sizer.Add(self.text_ctrl, 1, wx.EXPAND)
  539.         option_sizer = wx.BoxSizer(wx.HORIZONTAL)
  540.         sizer.Add(option_sizer, 0)
  541.         option_sizer.AddMany([
  542.             (10, 20),
  543.             (option_text, 0, wx.ALIGN_CENTER_VERTICAL),
  544.             (5, 5),
  545.             (self.completion_option, 0, wx.ALIGN_CENTER_VERTICAL),
  546.             (8, 8),
  547.             (self.background_option, 0, wx.ALIGN_CENTER_VERTICAL),
  548.             (8, 8),
  549.             (self.threading_option, 0, wx.ALIGN_CENTER_VERTICAL)])
  550.         self.SetAutoLayout(True)
  551.         sizer.Fit(self)
  552.         sizer.SetSizeHints(self)
  553.         self.SetSizer(sizer)
  554.         self.SetFocus()
  555.         self.setCurrentState('IDLE')
  556.         self.pager_state = 'DONE'
  557.         self.raw_input_current_line = 0
  558.  
  559.     
  560.     def askExitCallback(self, event):
  561.         self.askExitHandler(event)
  562.  
  563.     
  564.     def stateDoExecuteLine(self):
  565.         lines = self.text_ctrl.getCurrentLine()
  566.         self.text_ctrl.write('\n')
  567.         lines_to_execute = lines.replace('\t', '    ')
  568.         lines_to_execute = lines_to_execute.replace('\r', '')
  569.         self.IP.do_execute(lines_to_execute.encode(ENCODING))
  570.         self.updateHistoryTracker(lines)
  571.         if self.text_ctrl.getCursorPos() != 0:
  572.             self.text_ctrl.removeCurrentLine()
  573.         
  574.         self.setCurrentState('WAIT_END_OF_EXECUTION')
  575.  
  576.     
  577.     def evtStateExecuteDone(self, evt):
  578.         self.doc = self.IP.get_doc_text()
  579.         self.help = self.IP.get_help_text()
  580.         if self.doc:
  581.             self.pager_lines = self.doc[7:].split('\n')
  582.             self.pager_state = 'INIT'
  583.             self.setCurrentState('SHOW_DOC')
  584.             self.pager(self.doc)
  585.         elif self.help:
  586.             self.pager_lines = self.help.split('\n')
  587.             self.pager_state = 'INIT'
  588.             self.setCurrentState('SHOW_DOC')
  589.             self.pager(self.help)
  590.         elif self.text_ctrl.getCursorPos() != 0:
  591.             self.text_ctrl.removeCurrentLine()
  592.         
  593.         self.stateShowPrompt()
  594.  
  595.     
  596.     def stateShowPrompt(self):
  597.         self.setCurrentState('SHOW_PROMPT')
  598.         self.text_ctrl.setPrompt(self.IP.get_prompt())
  599.         self.text_ctrl.setIndentation(self.IP.get_indentation())
  600.         self.text_ctrl.setPromptCount(self.IP.get_prompt_count())
  601.         self.text_ctrl.showPrompt()
  602.         self.IP.init_history_index()
  603.         self.setCurrentState('IDLE')
  604.  
  605.     
  606.     def setCurrentState(self, state):
  607.         self.cur_state = state
  608.         self.updateStatusTracker(self.cur_state)
  609.  
  610.     
  611.     def pager(self, text):
  612.         if self.pager_state == 'INIT':
  613.             self.pager_nb_lines = len(self.pager_lines)
  614.             self.pager_index = 0
  615.             self.pager_do_remove = False
  616.             self.text_ctrl.write('\n')
  617.             self.pager_state = 'PROCESS_LINES'
  618.         
  619.         if self.pager_state == 'PROCESS_LINES':
  620.             if self.pager_do_remove == True:
  621.                 self.text_ctrl.removeCurrentLine()
  622.                 self.pager_do_remove = False
  623.             
  624.             if self.pager_nb_lines > 10:
  625.                 if self.pager_index > 0:
  626.                     self.text_ctrl.write('>\x01\x1b[1;36m\x02' + self.pager_lines[self.pager_index] + '\n')
  627.                 else:
  628.                     self.text_ctrl.write('\x01\x1b[1;36m\x02 ' + self.pager_lines[self.pager_index] + '\n')
  629.                 for line in self.pager_lines[self.pager_index + 1:self.pager_index + 9]:
  630.                     self.text_ctrl.write('\x01\x1b[1;36m\x02 ' + line + '\n')
  631.                 
  632.                 self.pager_index += 10
  633.                 self.pager_nb_lines -= 10
  634.                 self.text_ctrl.write("--- Push Enter to continue or 'Q' to quit---")
  635.                 self.pager_do_remove = True
  636.                 self.pager_state = 'WAITING'
  637.                 return None
  638.             if self.pager_nb_lines > 0:
  639.                 for line in self.pager_lines[self.pager_index:]:
  640.                     self.text_ctrl.write('\x01\x1b[1;36m\x02 ' + line + '\n')
  641.                     self.pager_nb_lines = 0
  642.                 
  643.             
  644.             self.pager_state = 'DONE'
  645.             self.stateShowPrompt()
  646.         
  647.  
  648.     
  649.     def keyPress(self, event):
  650.         if event.GetKeyCode() == ord('C'):
  651.             if event.Modifiers == wx.MOD_CONTROL or event.Modifiers == wx.MOD_ALT:
  652.                 if self.cur_state == 'WAIT_END_OF_EXECUTION':
  653.                     self.IP.ce.raise_exc(KeyboardInterrupt)
  654.                     return None
  655.             
  656.         
  657.         if self.cur_state == 'COMPLETING':
  658.             if not self.text_ctrl.AutoCompActive():
  659.                 self.cur_state = 'IDLE'
  660.             else:
  661.                 event.Skip()
  662.         
  663.         if self.cur_state == 'WAITING_USER_INPUT':
  664.             event.Skip()
  665.         
  666.  
  667.     
  668.     def evtCheckOptionCompletion(self, event):
  669.         if event.IsChecked():
  670.             self.options['completion']['value'] = 'STC'
  671.         else:
  672.             self.options['completion']['value'] = 'IPYTHON'
  673.         self.text_ctrl.setCompletionMethod(self.options['completion']['value'])
  674.         self.updateOptionTracker('completion', self.options['completion']['value'])
  675.         self.text_ctrl.SetFocus()
  676.  
  677.     
  678.     def evtCheckOptionBackgroundColor(self, event):
  679.         if event.IsChecked():
  680.             self.options['background_color']['value'] = 'WHITE'
  681.         else:
  682.             self.options['background_color']['value'] = 'BLACK'
  683.         self.text_ctrl.setBackgroundColor(self.options['background_color']['value'])
  684.         self.updateOptionTracker('background_color', self.options['background_color']['value'])
  685.         self.text_ctrl.SetFocus()
  686.  
  687.     
  688.     def evtCheckOptionThreading(self, event):
  689.         if event.IsChecked():
  690.             self.options['threading']['value'] = 'True'
  691.             self.IP.set_threading(True)
  692.             self.cout.write = self.text_ctrl.asyncWrite
  693.         else:
  694.             self.options['threading']['value'] = 'False'
  695.             self.IP.set_threading(False)
  696.             self.cout.write = self.text_ctrl.write
  697.         self.updateOptionTracker('threading', self.options['threading']['value'])
  698.         self.text_ctrl.SetFocus()
  699.  
  700.     
  701.     def getOptions(self):
  702.         return self.options
  703.  
  704.     
  705.     def reloadOptions(self, options):
  706.         self.options = options
  707.         for key in self.options.keys():
  708.             value = self.options[key]['value']
  709.             self.options[key]['checkbox'].SetValue(self.options[key][value])
  710.             self.options[key]['setfunc'](value)
  711.         
  712.         if self.options['threading']['value'] == 'True':
  713.             self.IP.set_threading(True)
  714.             self.cout.write = self.text_ctrl.asyncWrite
  715.         else:
  716.             self.IP.set_threading(False)
  717.             self.cout.write = self.text_ctrl.write
  718.  
  719.     
  720.     def updateOptionTracker(self, name, value):
  721.         pass
  722.  
  723.     
  724.     def setOptionTrackerHook(self, func):
  725.         self.updateOptionTracker = func
  726.  
  727.     
  728.     def updateHistoryTracker(self, command_line):
  729.         pass
  730.  
  731.     
  732.     def setHistoryTrackerHook(self, func):
  733.         self.updateHistoryTracker = func
  734.  
  735.     
  736.     def updateStatusTracker(self, status):
  737.         pass
  738.  
  739.     
  740.     def setStatusTrackerHook(self, func):
  741.         self.updateStatusTracker = func
  742.  
  743.     
  744.     def askExitHandler(self, event):
  745.         self.text_ctrl.write('\nExit callback has not been set.')
  746.  
  747.     
  748.     def setAskExitHandler(self, func):
  749.         self.askExitHandler = func
  750.  
  751.  
  752. if __name__ == '__main__':
  753.     
  754.     class MainWindow(wx.Frame):
  755.         
  756.         def __init__(self, parent, id, title):
  757.             wx.Frame.__init__(self, parent, id, title, size = (300, 250))
  758.             self._sizer = wx.BoxSizer(wx.VERTICAL)
  759.             self.shell = IPShellWidget(self)
  760.             self._sizer.Add(self.shell, 1, wx.EXPAND)
  761.             self.SetSizer(self._sizer)
  762.             self.SetAutoLayout(1)
  763.             self.Show(True)
  764.  
  765.  
  766.     app = wx.PySimpleApp()
  767.     frame = MainWindow(None, wx.ID_ANY, 'Ipython')
  768.     frame.SetSize((780, 460))
  769.     shell = frame.shell
  770.     app.MainLoop()
  771.  
  772.