tuikit/driver.py
changeset 82 2bead23b1262
parent 81 5cd40c30c4f2
child 83 ebe732b9ef19
equal deleted inserted replaced
81:5cd40c30c4f2 82:2bead23b1262
     1 # -*- coding: utf-8 -*-
       
     2 '''Tuikit driver base.'''
       
     3 
       
     4 from tuikit.common import Size, ClipStack, UnicodeGraphics
       
     5 
       
     6 
       
     7 class Driver:
       
     8 
       
     9     '''Abstract driver interface. Use as base for drivers.'''
       
    10 
       
    11     def __init__(self):
       
    12         '''Initialize instance attributes.'''
       
    13         #: Screen size.
       
    14         self.size = Size()
       
    15         #: Clipping region stack.
       
    16         self.clipstack = ClipStack()
       
    17         #: Unicode graphics characters.
       
    18         self.unigraph = UnicodeGraphics()
       
    19         #: Stack of color prefixes.
       
    20         self.colorprefix = []
       
    21 
       
    22 
       
    23     ## drawing ##
       
    24 
       
    25     def puts(self, x, y, s):
       
    26         '''Output string of characters.'''
       
    27         for c in s:
       
    28             self.putch(x, y, c)
       
    29             x += 1
       
    30 
       
    31     def hline(self, x, y, w, c=' '):
       
    32         '''Draw horizontal line.'''
       
    33         if isinstance(c, str):
       
    34             s = c*w
       
    35         else:
       
    36             s = [c]*w
       
    37         self.puts(x, y, s)
       
    38 
       
    39     def vline(self, x, y, h, c=' '):
       
    40         '''Draw vertical line.'''
       
    41         for i in range(h):
       
    42             self.putch(x, y+i, c)
       
    43 
       
    44     def fill_clip(self, c=' '):
       
    45         """Fill current clip region."""
       
    46         rect = self.clipstack.top()
       
    47         self.fill(rect.x, rect.y, rect.w, rect.h, c)
       
    48 
       
    49     def fill(self, x, y, w, h, c=' '):
       
    50         '''Fill rectangular area.'''
       
    51         for i in range(h):
       
    52             self.hline(x, y + i, w, c)
       
    53 
       
    54     def frame(self, x, y, w, h):
       
    55         '''Draw rectangular frame using line-drawing characters.'''
       
    56         self.putch(x, y, self.unigraph.ULCORNER)
       
    57         self.putch(x+w-1, y, self.unigraph.URCORNER)
       
    58         self.putch(x, y+h-1, self.unigraph.LLCORNER)
       
    59         self.putch(x+w-1, y+h-1, self.unigraph.LRCORNER)
       
    60         self.hline(x+1, y, w-2, self.unigraph.HLINE)
       
    61         self.hline(x+1, y+h-1, w-2, self.unigraph.HLINE)
       
    62         self.vline(x, y+1, h-2, self.unigraph.VLINE)
       
    63         self.vline(x+w-1, y+1, h-2, self.unigraph.VLINE)
       
    64 
       
    65 
       
    66     ## colors ##
       
    67 
       
    68     def pushcolorprefix(self, name):
       
    69         self.colorprefix.append(name)
       
    70 
       
    71     def popcolorprefix(self):
       
    72         self.colorprefix.pop()
       
    73