1 # Copyright (C) 2010-2012 W. Trevor King <wking@drexel.edu>
3 # This file is part of Hooke.
5 # Hooke is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
10 # Hooke is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
13 # Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with Hooke. If not, see
17 # <http://www.gnu.org/licenses/>.
19 """Scrolling text buffer panel for Hooke.
27 class OutputPanel (Panel, wx.TextCtrl):
28 """Scrolling text buffer panel.
30 def __init__(self, name=None, callbacks=None, buffer_lines=1000, **kwargs):
31 self._buffer_lines = buffer_lines
32 if (kwargs.get('style') & wx.TE_READONLY == 0):
33 raise NotImplementedError('%s assumes a readonly TextCtrl'
34 % self.__class__.__name__)
35 super(OutputPanel, self).__init__(
36 name='output', callbacks=callbacks, **kwargs)
38 def write(self, text):
40 self._limit_to_buffer()
42 def _limit_to_buffer(self):
43 """Limit number of lines retained in the buffer to `._buffer_lines`.
45 num_lines = self.GetNumberOfLines()
46 line_index = num_lines - self._buffer_lines
48 first_pos = 0 # character index for the first character to keep
49 for i in range(line_index):
50 first_pos += self.GetLineLength(i) + 1 # +1 for '\n'
51 self.Remove(0, first_pos)