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