Advertisement
nux95

Listview from scratch

Dec 2nd, 2011
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.22 KB | None | 0 0
  1.  
  2. import c4d
  3. import c4d.gui  as gui
  4.  
  5. DATA = [ 'One',
  6.          'Two',
  7.          'Three' ]
  8.  
  9. class ListViewElement(object):
  10.  
  11.     COLOR_01    = c4d.Vector(.34)
  12.     COLOR_02    = c4d.Vector(.38)
  13.     COLOR_TEXT  = c4d.Vector(.85)
  14.  
  15.     TEXT_X      = 10
  16.     TEXT_X_ABS  = True
  17.     TEXT_Y      = 0.5
  18.     TEXT_Y_ABS  = False
  19.     TEXT_Y_CNTR = True
  20.  
  21.     def __init__(self, index, text = ''):
  22.         self.text = text
  23.         if not index % 2:
  24.             self.color = self.COLOR_01
  25.         else:
  26.             self.color = self.COLOR_02
  27.  
  28.     def render(self, area, x1, y1, x2, y2):
  29.         width  = x2 - x1
  30.         height = y2 - y1
  31.  
  32.         if self.TEXT_X_ABS:
  33.             x  = x1 + self.TEXT_X
  34.         else:
  35.             x  = x1 + width * self.TEXT_X
  36.  
  37.         if self.TEXT_Y_ABS:
  38.             y  = y1 + self.TEXT_Y
  39.         else:
  40.             y  = y1 + height * self.TEXT_Y
  41.  
  42.         if self.TEXT_Y_CNTR:
  43.             th = area.DrawGetFontHeight()
  44.             y  = y - (th / 2.)
  45.  
  46.         x = int(round(x))
  47.         y = int(round(y))
  48.  
  49.         area.DrawSetPen(self.color)
  50.         area.DrawSetTextCol(self.COLOR_TEXT, c4d.COLOR_TRANS)
  51.  
  52.         area.DrawRectangle(x1, y1, x2, y2)
  53.         area.DrawText(self.text, x, y)
  54.  
  55. class ListViewArea(gui.GeUserArea):
  56.  
  57.     COLOR_BG   = c4d.Vector(.29)
  58.     ELEM_HEIGT = 23
  59.  
  60.     minSize    = 200, 120
  61.  
  62.     def __init__(self):
  63.         self.elems = [ListViewElement(i, e) for i, e in enumerate(DATA)]
  64.  
  65.     def DrawMsg(self, x1, y1, x2, y2, msg):
  66.         self.OffScreenOn(x1, y1, x2, y2) # enable double buffer
  67.  
  68.         self.DrawSetPen(self.COLOR_BG)
  69.         self.DrawRectangle(x1, y1, x2, y2)
  70.  
  71.         prevY = x1
  72.         for elem in self.elems:
  73.             elem.render(self, x1, prevY, x2, prevY + self.ELEM_HEIGT)
  74.             prevY = prevY + self.ELEM_HEIGT
  75.  
  76.     def GetMinSize(self):
  77.         return self.minSize
  78.  
  79. class Dialog(gui.GeDialog):
  80.  
  81.     def __init__(self, area):
  82.         self.area = area
  83.  
  84.     def CreateLayout(self):
  85.         self.AddUserArea(1000, c4d.BFH_SCALEFIT | c4d.BFV_SCALEFIT)
  86.         self.AttachUserArea(self.area, 1000)
  87.         return True
  88.  
  89. def main():
  90.     area = ListViewArea()
  91.     dlg  = Dialog(area)
  92.     dlg.Open(c4d.DLG_TYPE_MODAL_RESIZEABLE)
  93.  
  94. main()
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement