File: textconsole.py

package info (click to toggle)
pyjamas 0.7~%2Bpre2-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 10,656 kB
  • ctags: 12,331
  • sloc: python: 74,493; php: 805; sh: 291; makefile: 59; xml: 9
file content (62 lines) | stat: -rw-r--r-- 1,731 bytes parent folder | download
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
from pyjamas.ui.AbsolutePanel import AbsolutePanel
from pyjamas.ui.Label import Label
from pyjamas import DOM
from pyjamas import log
import math

class TextWindow(AbsolutePanel):

    def __init__(self, cols, rows, width, height):

        AbsolutePanel.__init__(self)
        self.rows = rows
        self.cols = cols
        self.setStyleName("gwt-TextWindow")

        DOM.setStyleAttribute(self.getElement(), 'fontFamily', 'monospace')

        self.setHeight(height)
        self.setWidth(width)

        self.text = {}
        for x in range(self.cols):
            self.text[x] = {}

    def _get_label(self, x, y):

        if not self.text[x].has_key(y):
            xpos = x * self.fontsize
            ypos = y * self.fontheight
            txt = Label(' ')
            self.add(txt, xpos, ypos)
            self.text[x][y] = txt
            
        return self.text[x][y]

    def setChar(self, x, y, char):

        label = self._get_label(x, y)
        label.setText(char)

    def setWidth(self, width):

        self.fontsize = math.floor(width / self.cols)
        AbsolutePanel.setWidth(self, "%dpx" % (self.cols*self.fontsize))

        ratio = self.fontsize / self.fontheight 
        DOM.setStyleAttribute(self.getElement(), 'fontSizeAdjust', str(ratio))
        #log.writebr(str(ratio))

    def setHeight(self, height):

        self.fontheight = math.floor(height / self.rows)
        AbsolutePanel.setHeight(self, "%dpx" % (self.rows*self.fontheight))

        DOM.setStyleAttribute(self.getElement(), 'fontSize', "%dpx" % self.fontheight)
        #log.writebr(str(self.fontheight))

    def setText(self, x, y, string):

        for i in range(len(string)):
            self.setChar(x+i, y, string[i])