File: console.py

package info (click to toggle)
pigment-python 0.3.12-2
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 2,820 kB
  • ctags: 2,955
  • sloc: python: 11,664; sh: 10,131; makefile: 251; ansic: 97
file content (115 lines) | stat: -rw-r--r-- 3,137 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# -*- mode: python; coding: utf-8 -*-
#
# Pigment Python tools
#
# Copyright © 2006, 2007, 2008 Fluendo Embedded S.L.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import pgm
from pgm.graph.text import Text

class Console(Text):

    def __init__(self):
        Text.__init__(self)

        self._lines = [""]
        self._current_line = 0
        self._visible_lines = 5

        # default style
        self.wrap = pgm.TEXT_WRAP_CHAR
        self.font_height = self.height / self._visible_lines * 0.7
        self.size = (2.0, 1.0)
        self.fg_color = (200, 200, 200, 255)
        self.bg_color = (50, 50, 50, 255)

    def append_to_current_line(self, text):
        self._lines[self._current_line] += text
        self._refresh()

    def delete_previous_character(self):
        pass

    def next_line(self):
        self._current_line += 1
        self._lines.append("")
        self._refresh()

    def clear(self):
        pass

    def clear_current_line(self):
        pass

    def set_visible_lines(self, lines):
        self.font_height = self.height / lines
        self._visible_lines = lines
        self._refresh()

    def _refresh(self):
        buffer = ""
        for line in self._lines[-self._visible_lines:]:
            buffer += line
            buffer += "\n"
        self.label = buffer

if __name__ == "__main__":
    import pgm
    import gobject

    def on_key_press(viewport, event, gl, widget):
        if event.type == pgm.KEY_PRESS:
            if event.keyval == pgm.keysyms.Escape:
                pgm.main_quit()

            elif event.keyval == pgm.keysyms.Return:
                widget.next_line()

            else:
                widget.append_to_current_line(unichr(event.keyval))


    def on_delete(viewport, event):
        pgm.main_quit()


    # OpenGL viewport creation
    factory = pgm.ViewportFactory('opengl')
    gl = factory.create()
    gl.title = 'Console widget test'

    # Canvas and image drawable creation
    canvas = pgm.Canvas()

    # Bind the canvas to the OpenGL viewport
    gl.set_canvas(canvas)
    gl.show()

    console_widget = Console()
    console_widget.position = (0.2, 0.5, 0.0)
    console_widget.visible = True

    canvas.add(pgm.DRAWABLE_MIDDLE, console_widget)

    # Let's start a mainloop
    gl.connect('key-press-event',
               on_key_press,
               gl,
               console_widget)
    gl.connect('delete-event', on_delete)
    pgm.main()