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