test/data/vclamp_jpk/README: Document sample versions
[hooke.git] / hooke / ui / gui / panel / output.py
1 # Copyright (C) 2010-2012 W. Trevor King <wking@tremily.us>
2 #
3 # This file is part of Hooke.
4 #
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
8 # later version.
9 #
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
13 # details.
14 #
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/>.
17
18 """Scrolling text buffer panel for Hooke.
19 """
20
21 import wx
22
23 from . import Panel
24
25
26 class OutputPanel (Panel, wx.TextCtrl):
27     """Scrolling text buffer panel.
28     """
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)
36
37     def write(self, text):
38         self.AppendText(text)
39         self._limit_to_buffer()
40
41     def _limit_to_buffer(self):
42         """Limit number of lines retained in the buffer to `._buffer_lines`.
43         """
44         num_lines = self.GetNumberOfLines()
45         line_index = num_lines - self._buffer_lines
46         if line_index > 0:
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)
51
52