# LEDCtrl 0.7 demo
#   E. A. Tacao <e.a.tacao |at| estadao.com.br>
#   http://j.domaindlx.com/elements28/wxpython/
#   29 Dez 2005, 17:00 GMT-03:00
# Distributed under the wxWidgets license.

import wx
import LEDCtrl as led

# Demo-only stuff
import time
import cStringIO
import wx.lib.buttons as buttons
import wx.lib.colourselect as csel

#-------------------------------------------------------------------------------

class StaticTitleBar(wx.Panel):
    def __init__(self, parent, id = -1, size = None,
                 label = "", style = ["horizontal"], name = "StaticTitleBar"):
        wx.Panel.__init__(self, parent, -1, name = name)

        self.style = style
        
        if "horizontal" in style:
            boxh = wx.BoxSizer(wx.HORIZONTAL)
            boxv = wx.BoxSizer(wx.VERTICAL)

            if "prepend segment" in style:
                lsize = (7, -1); lborder = 3
            else:
                lsize = (0, 0); lborder = 0
            line = wx.StaticLine(self, -1, size = lsize)
            boxv.Add(line, 1, flag = wx.ALIGN_CENTRE)
            boxh.Add(boxv, flag = wx.ALIGN_CENTRE|wx.RIGHT, border = lborder)

            self.label = wx.StaticText(self, -1, label)
            if label:
                lborder = 3
            else:
                lborder = 0
                self.label.SetSize((0, 0)); self.label.SetMinSize((0, 0))
            boxh.Add(self.label, flag = wx.ALIGN_CENTRE|wx.RIGHT,
                     border = lborder)

            boxv = wx.BoxSizer(wx.VERTICAL)
            line = wx.StaticLine(self, -1)
            line.SetMinSize((7, -1))
            boxv.Add(line, 1, flag = wx.ALIGN_CENTRE|wx.EXPAND)
            boxh.Add(boxv, 1, flag = wx.ALIGN_CENTRE)

        else:
            boxh = wx.BoxSizer(wx.VERTICAL)
            boxv = wx.BoxSizer(wx.HORIZONTAL)

            if "prepend segment" in style:
                lsize = (-1, 7); lborder = 3
            else:
                lsize = (0, 0); lborder = 0
            line = wx.StaticLine(self, -1, size = lsize, style = wx.LI_VERTICAL)
            boxv.Add(line, 1, flag = wx.ALIGN_CENTRE)
            boxh.Add(boxv, flag = wx.ALIGN_CENTRE|wx.RIGHT, border = lborder)

            self.label = wx.StaticText(self, -1, label)
            if label:
                lborder = 3
            else:
                lborder = 0
                self.label.SetSize((0, 0)); self.label.SetMinSize((0, 0))
            boxh.Add(self.label, flag = wx.ALIGN_CENTRE|wx.RIGHT,
                     border = lborder)

            boxv = wx.BoxSizer(wx.HORIZONTAL)
            line = wx.StaticLine(self, -1, style = wx.LI_VERTICAL)
            line.SetMinSize((-1, 7))
            boxv.Add(line, 1, flag = wx.ALIGN_CENTRE|wx.EXPAND)
            boxh.Add(boxv, 1, flag = wx.ALIGN_CENTRE)

        self.SetSizer(boxh)
        if size is not None:
            self.SetSize(size); self.SetMinSize(size)


    def GetValue(self):
        return self.label.GetLabel()


    def SetValue(self, v):
        self.label.SetLabel(v)
        if v: size = (-1, -1)
        else: size = (0, 0)
        self.label.SetSize(size); self.label.SetMinSize(size)


    def GetLabel(self):
        return self.GetValue()


    def SetLabel(self, v):
        self.SetValue(v)


    def GetWindowStyleFlag(self):
        return self.style

#-------------------------------------------------------------------------------

class ToggleButton(buttons.GenBitmapToggleButton):
    def __init__(self, *args, **kwargs):
         buttons.GenBitmapToggleButton.__init__(self, *args, **kwargs)

         self.SetSize((21, 21))
         self.SetUseFocusIndicator(False)
         self.SetBezelWidth(1)

#-------------------------------------------------------------------------------

class ToggleGroup:
    def __init__(self, parent, bu_list):
        self.bu_list = bu_list

        for bu in bu_list:
            parent.Bind(wx.EVT_BUTTON, self.OnButton, bu)


    def OnButton(self, evt = None, obj = None):
        if obj is None:
            ebu = evt.GetEventObject()
        else:
            ebu = obj; ebu.SetValue(True)
            
        ev = ebu.GetValue()

        if ev:
            for bu in self.bu_list:
                f = bu == ebu; v = bu.GetValue()
                if v <> f:
                    bu.SetValue(f)
        else:
           ebu.SetValue(True)

        if evt:
            evt.Skip()

#-------------------------------------------------------------------------------

class Images:    # generated by encode_bitmaps.py
    def getzfillData(self):
        return \
'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x10\x00\x00\x00\x10\x08\x06\
\x00\x00\x00\x1f\xf3\xffa\x00\x00\x00\x04sBIT\x08\x08\x08\x08|\x08d\x88\x00\
\x00\x00\\IDAT8\x8dcddbf\xa0\x040Q\xa4\x9b\xea\x06\xfc\xff\xf7\xf7?)|\x14\
\x03\xfe\xff\xfb\xfb\x9f\x91\x89\x99\x11\xa6\x88\x10\x9f6^ \x070"G#\xcc\x99\
\xc4\xf21\x0c \x07P;\x1ak1\xa2\x89\x10\x80{\x81T\xcd\x8cL\xcd\x8cP\x063\x1c3\
04\xfcG\xe6\x13\x85\x07W \x92\x03\x00\x18\xe69`\x8bF\xc1\xce\x00\x00\x00\x00\
IEND\xaeB`\x82'

    def getzfillBitmap(self):
        return wx.BitmapFromImage(self.getzfillImage())

    def getzfillImage(self):
        stream = cStringIO.StringIO(self.getzfillData())
        return wx.ImageFromStream(stream)

    #----------------------------------------------------------------------
    def getslantData(self):
        return \
'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x10\x00\x00\x00\x10\x08\x06\
\x00\x00\x00\x1f\xf3\xffa\x00\x00\x00\x04sBIT\x08\x08\x08\x08|\x08d\x88\x00\
\x00\x00LIDAT8\x8d\xc5\x92\xc1\n\x00 \x08C\x9b\xf6\xff\x7f\xac\xeb\x14t)\x0b\
\t\xbd\xca\x9e\x9b\x0c\x10m\x99\x91\x94:\x02\xd0\x8dt\xe3W\x07(\xffA\xdf-\
\xd6\xec\x10\xc5\xb3\x83\x93\xe8\n0\x1dD\xa0?=X\xaf\xa7z@7F\x11\xea{P\x0f\
\x18\x14\xee\x1c$\\\x97BK\x00\x00\x00\x00IEND\xaeB`\x82'

    def getslantBitmap(self):
        return wx.BitmapFromImage(self.getslantImage())

    def getslantImage(self):
        stream = cStringIO.StringIO(self.getslantData())
        return wx.ImageFromStream(stream)

    #----------------------------------------------------------------------
    def getdraw_fadedData(self):
        return \
'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x10\x00\x00\x00\x10\x08\x06\
\x00\x00\x00\x1f\xf3\xffa\x00\x00\x00\x04sBIT\x08\x08\x08\x08|\x08d\x88\x00\
\x00\x00~IDAT8\x8d\xad\x92A\x0e\xc30\x08\x04w\xa1\xdfn\xe3\xb4\xefN\xc8%mlTc\
$g%\x0e @0@\x8abF2U\r\xe0Q;\xb6oV\xfb\x14e\xba\x81\xed\x9b\xf9\x82\x7f1\xaf{\
W\xe8iy=\x9b\xd5\xd6\xf7\xe7\x9a\x8a\xa2?\x03`\xb5Q\x14\xa5\x14\xabs|l\xc8`4\
\xdd4\x03~\x1f)\xbaB\x8a\x01\xce\x9d=\x934\x03\x8a\xb2\xf3H!\x87\xe6\x8c\x99\
\xcf\xf3\xba\x0fb\xa4\x08b\xaaA\xa4\x03\xd1\xa8=\xbd\xab\x92|\xef\x00\x00\
\x00\x00IEND\xaeB`\x82'

    def getdraw_fadedBitmap(self):
        return wx.BitmapFromImage(self.getdraw_fadedImage())

    def getdraw_fadedImage(self):
        stream = cStringIO.StringIO(self.getdraw_fadedData())
        return wx.ImageFromStream(stream)

    #----------------------------------------------------------------------
    def getalign_rightData(self):
        return \
'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x10\x00\x00\x00\x10\x08\x06\
\x00\x00\x00\x1f\xf3\xffa\x00\x00\x00\x04sBIT\x08\x08\x08\x08|\x08d\x88\x00\
\x00\x009IDAT8\x8dcddbf\xa0\x040Q\xa4\x9b\x1a\x06\xb00000\xfc\xff\xf7\xf7?9\
\x9a\x19\x99\x98\x19\x19)\r\x03\x16\\\x12\xc4\xb8\x8az.\x18\r\x83\x11\x1f\
\x06\x14g&\x00\xd9\xf5\x18P\xc8\xf0\x96]\x00\x00\x00\x00IEND\xaeB`\x82'

    def getalign_rightBitmap(self):
        return wx.BitmapFromImage(self.getalign_rightImage())

    def getalign_rightImage(self):
        stream = cStringIO.StringIO(self.getalign_rightData())
        return wx.ImageFromStream(stream)

    #----------------------------------------------------------------------
    def getalign_centreData(self):
        return \
'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x10\x00\x00\x00\x10\x08\x06\
\x00\x00\x00\x1f\xf3\xffa\x00\x00\x00\x04sBIT\x08\x08\x08\x08|\x08d\x88\x00\
\x00\x00?IDAT8\x8dcddbf\xa0\x040Q\xa4\x9b\x1a\x06\xb00000\xfc\xff\xf7\xf7?9\
\x9a\x19\x99\x98\x19\x19)\r\x03\x16t\x01B\xaeadbfD\xe1S\xc5\x05\xa3a0\xe2\
\xc3\x80\xe2\xcc\x04\x00:X\x18P\xdf\xe3\xf5*\x00\x00\x00\x00IEND\xaeB`\x82' 

    def getalign_centreBitmap(self):
        return wx.BitmapFromImage(self.getalign_centreImage())

    def getalign_centreImage(self):
        stream = cStringIO.StringIO(self.getalign_centreData())
        return wx.ImageFromStream(stream)

    #----------------------------------------------------------------------
    def getalign_leftData(self):
        return \
'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x10\x00\x00\x00\x10\x08\x06\
\x00\x00\x00\x1f\xf3\xffa\x00\x00\x00\x04sBIT\x08\x08\x08\x08|\x08d\x88\x00\
\x00\x00?IDAT8\x8dcddbf\xa0\x040Q\xa4\x9b\x1a\x06\xb00000\xfc\xff\xf7\xf7?9\
\x9a\x19\x99\x98\x19\x19)\r\x03\xa2]\xc0\xc8\xc4\xcc\x88U\x9cn.\xc0j\xfbh\
\x18\x0c\x970\xa083\x01\x00\xca\x04\x18Pw\x06\xe6\xdd\x00\x00\x00\x00IEND\
\xaeB`\x82'

    def getalign_leftBitmap(self):
        return wx.BitmapFromImage(self.getalign_leftImage())

    def getalign_leftImage(self):
        stream = cStringIO.StringIO(self.getalign_leftData())
        return wx.ImageFromStream(stream)


    #----------------------------------------------------------------------
    def getcolonData(self):
        return \
'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x10\x00\x00\x00\x10\x08\x06\
\x00\x00\x00\x1f\xf3\xffa\x00\x00\x00\x04sBIT\x08\x08\x08\x08|\x08d\x88\x00\
\x00\x00*IDAT8\x8dcddbf\xa0\x040Q\xa4{\xf0\x1b\xf0\xff\xdf\xdf\xff\xff\xff\
\xfd\xfdOS\x170\x0e\xf3X\x18\r\xc4\xa1b\x00\x00\xc2s\x10\x14\xe8\x1d!+\x00\
\x00\x00\x00IEND\xaeB`\x82'

    def getcolonBitmap(self):
        return wx.BitmapFromImage(self.getcolonImage())

    def getcolonImage(self):
        stream = cStringIO.StringIO(self.getcolonData())
        return wx.ImageFromStream(stream)

#-------------------------------------------------------------------------------

class TipWindow(wx.PopupTransientWindow):
    def __init__(self, parent, valid_chars = "", text = ""):
        wx.PopupTransientWindow.__init__(self, parent, style = wx.SIMPLE_BORDER)

        self.SetBackgroundColour((255, 255, 225))

        bmp = wx.StaticBitmap(self, -1,
                              wx.ArtProvider_GetBitmap(wx.ART_WARNING,
                                                       wx.ART_TOOLBAR,
                                                       (16, 16)))

        top  = wx.StaticText(self, -1, "You typed invalid data!")
        font = top.GetFont(); font.SetWeight(wx.BOLD); top.SetFont(font)

        if text == "":
            body = wx.StaticText(self, -1, "\nLEDCtrl supports only\n"
                                           "whitespaces and the\n"
                                           "following characters:\n")
        else:
            body = wx.StaticText(self, -1, text)

        vchr = wx.StaticText(self, -1, "%s\n" % valid_chars)

        sizer = wx.GridBagSizer()

        sizer.Add(bmp,  pos = (1, 1))
        sizer.Add(top,  pos = (1, 2), span = (1, 4))
        sizer.Add(body, pos = (2, 1), span = (1, 5), flag = wx.ALIGN_CENTRE)
        sizer.Add(vchr, pos = (3, 0), span = (1, 7), flag = wx.ALIGN_CENTRE)

        sizer.Layout()
        self.SetSizerAndFit(sizer)

        for w in [self] + self.GetChildren():
            w.Bind(wx.EVT_LEFT_DOWN, self.ProcessLeftDown)


    def ProcessLeftDown(self, evt):
        self.Show(False)
        self.Destroy()

        
    def ShowTip(self, obj):
        self.Position(obj.ClientToScreen((0, 0)),
                      (0, obj.GetSize().GetHeight()))
        self.Popup()

#---------------------------------------------------------------------------------

class LEDDemoPanelGUI(wx.Panel, Images):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1)

        sizer = wx.GridBagSizer(3, 3)
        sizer.SetEmptyCellSize((2, 2))

        value = "3.14159"; digits = 12; fgColour = (255, 0, 0)
        agg = True; agg_enable = led._use_agg; factor = 33

        p = StaticTitleBar(self, -1, label = "General", style = ['horizontal'])
        sizer.Add(p, pos = (0, 0), span = (1, 5),
                  flag = wx.EXPAND|wx.LEFT|wx.TOP|wx.ALIGN_CENTRE, border = 5)

        p = StaticTitleBar(self, -1, label = "", style = ['vertical'])
        sizer.Add(p, pos = (0, 5), span = (10, 1),
                  flag = wx.EXPAND|wx.TOP|wx.LEFT|wx.RIGHT|wx.ALIGN_CENTRE,
                  border = 5)

        p = StaticTitleBar(self, -1, label = "Styles", style = ['horizontal'])
        sizer.Add(p, pos = (0, 6), span = (1, 7),
                  flag = wx.EXPAND|wx.TOP|wx.ALIGN_CENTRE, border = 5)

        p = wx.StaticText(self, -1, label = "Digits:")
        sizer.Add(p, pos = (1, 1), flag = wx.ALIGN_CENTRE_VERTICAL)

        p = wx.SpinCtrl(self, -1, size = (75, -1), min = 1, max = 50,
                        value = "%s" % digits)
        self.digits = p
        sizer.Add(p, pos = (1, 2), span = (1, 3),
                  flag = wx.ALIGN_CENTRE_VERTICAL|wx.LEFT, border = 3)

        p = wx.StaticText(self, -1, label = "LED Styles:")
        sizer.Add(p, pos = (1, 7), span = (1, 2),
                  flag = wx.ALIGN_CENTRE_VERTICAL)

        bmp = self.getalign_leftBitmap()
        p = ToggleButton(self, -1, size = (21, 21), bitmap = bmp,
                         name = "align_left")
        self.align_left = p
        p.SetToolTipString("LED_ALIGN_LEFT")
        sizer.Add(p, pos = (1, 9), flag = wx.ALIGN_RIGHT)

        bmp = self.getalign_centreBitmap()
        p = ToggleButton(self, -1, size = (21, 21), bitmap = bmp,
                         name = "align_centre")
        self.align_centre = p
        p.SetToolTipString("LED_ALIGN_CENTRE")
        sizer.Add(p, pos = (1, 10), flag = wx.ALIGN_RIGHT)

        bmp = self.getalign_rightBitmap()
        p = ToggleButton(self, -1, size = (21, 21), bitmap = bmp,
                         name = "align_right")
        p.SetValue(True)
        self.align_right = p
        p.SetToolTipString("LED_ALIGN_RIGHT")
        sizer.Add(p, pos = (1, 11), flag = wx.ALIGN_RIGHT)

        p = wx.StaticText(self, -1, label = "Value:")
        sizer.Add(p, pos = (2, 1), flag = wx.ALIGN_CENTRE_VERTICAL)

        p = wx.TextCtrl(self, -1, value = value, style = wx.TE_PROCESS_ENTER)
        self.value = p
        sizer.Add(p, pos = (2, 2), span = (1, 3),
                  flag = wx.ALIGN_CENTRE_VERTICAL|wx.EXPAND|wx.LEFT, border = 3)

        bmp = self.getcolonBitmap()
        p = ToggleButton(self, -1, size = (21, 21), bitmap = bmp,
                         name = "allow_colons")
        p.SetToolTipString("LED_ALLOW_COLONS")
        sizer.Add(p, pos = (2, 8), flag = wx.ALIGN_RIGHT)

        bmp = self.getslantBitmap()
        p = ToggleButton(self, -1, size = (21, 21), bitmap = bmp,
                         name = "slant")
        p.SetToolTipString("LED_SLANT")
        sizer.Add(p, pos = (2, 9), flag = wx.ALIGN_RIGHT)

        bmp = self.getzfillBitmap()
        p = ToggleButton(self, -1, size = (21, 21), bitmap = bmp,
                         name = "zfill")
        p.SetToolTipString("LED_ZFILL")
        sizer.Add(p, pos = (2, 10), flag = wx.ALIGN_RIGHT)

        bmp = self.getdraw_fadedBitmap()
        p = ToggleButton(self, -1, size = (21, 21), bitmap = bmp,
                         name = "draw_faded")
        p.SetValue(True)
        p.SetToolTipString("LED_DRAW_FADED")
        sizer.Add(p, pos = (2, 11), flag = wx.ALIGN_RIGHT)

        p = wx.CheckBox(self, -1, label = "Use aggdraw", name = "agg")
        p.SetToolTipString("LED_AGG")
        if agg_enable:
            p.SetValue(agg)
        else:
            p.Disable()
        sizer.Add(p, pos = (5, 7), span = (1, 5),
                  flag = wx.ALIGN_CENTRE_VERTICAL|wx.LEFT, border = 5)

        p = StaticTitleBar(self, -1, label = "Colours", style = ['horizontal'])
        sizer.Add(p, pos = (4, 0), span = (1, 5),
                  flag = wx.EXPAND|wx.LEFT|wx.ALIGN_CENTRE, border = 5)

        p = wx.StaticText(self, -1, label = "Foreground:")
        sizer.Add(p, pos = (5, 1), span = (1, 2),
                  flag = wx.ALIGN_CENTRE_VERTICAL)

        p = csel.ColourSelect(self, -1, "", fgColour, size = (21, 21))
        self.fg = p
        sizer.Add(p, pos = (5, 3), span = (1, 2),
                  flag = wx.ALIGN_CENTRE_VERTICAL|wx.LEFT, border = 3)

        p = wx.StaticText(self, -1, label = "Window Styles:")
        sizer.Add(p, pos = (7, 7), span = (1, 2),
                  flag = wx.ALIGN_CENTRE_VERTICAL)

        p = wx.StaticText(self, -1, label = "Background:")
        sizer.Add(p, pos = (6, 1), span = (1, 2),
                  flag = wx.ALIGN_CENTRE_VERTICAL)

        p = csel.ColourSelect(self, -1, "", wx.BLACK, size = (21, 21))
        self.bg = p
        sizer.Add(p, pos = (6, 3), span = (1, 2),
                  flag = wx.ALIGN_CENTRE_VERTICAL|wx.LEFT, border = 3)

        p = wx.StaticText(self, -1, label = "Fading:")
        sizer.Add(p, pos = (7, 1), span = (1, 2),
                  flag = wx.ALIGN_CENTRE_VERTICAL)

        p = csel.ColourSelect(self, -1, "", (255, 0, 0), size = (21, 21))
        self.fd = p
        sizer.Add(p, pos = (7, 3), span = (1, 2),
                  flag = wx.ALIGN_CENTRE_VERTICAL|wx.LEFT, border = 3)

        p = wx.StaticText(self, -1, label = "Fading Factor:")
        sizer.Add(p, pos = (8, 1), span = (1, 2),
                  flag = wx.ALIGN_CENTRE_VERTICAL)

        p = wx.StaticText(self, -1, "%s" % factor,
                          size = self.GetTextExtent("100"),
                          style = wx.ALIGN_RIGHT|wx.ST_NO_AUTORESIZE)
        self.fdLabel = p
        sizer.Add(p, pos = (8, 3), flag = wx.ALIGN_CENTRE, border = 3)

        p = wx.Slider(self, -1, size = (-1, 20), minValue = 0, maxValue = 100,
                      style = wx.SL_HORIZONTAL|wx.SL_AUTOTICKS)
        p.SetValue(factor)
        p.SetTickFreq(10, 1)
        self.fdSlider = p
        sizer.Add(p, pos = (9, 1), span = (1, 4),
                  flag = wx.ALIGN_CENTRE_VERTICAL|wx.LEFT|wx.EXPAND, border = 3)

        choices = ['wx.SIMPLE_BORDER', 'wx.DOUBLE_BORDER', 'wx.SUNKEN_BORDER',
                   'wx.RAISED_BORDER', 'wx.STATIC_BORDER', 'wx.NO_BORDER']
        p = wx.Choice(self, -1, choices = choices)
        p.SetStringSelection('wx.RAISED_BORDER')
        self.winstyle = p
        sizer.Add(p, pos = (8, 7), span = (1, 5),
                  flag = wx.ALIGN_CENTRE_VERTICAL|wx.EXPAND|wx.LEFT,
                  border = 5)

        p = wx.StaticLine(self, -1)
        sizer.Add(p, pos = (10, 0), span = (1, 13),
                  flag = wx.EXPAND|wx.ALIGN_CENTRE)

        p = led.LEDCtrl(self, -1, size = (326, 36), style = wx.RAISED_BORDER,
                        value = value, digits = digits)
        self.led = p
        p.SetForegroundColour(fgColour)
        sizer.Add(p, pos = (11, 0), span = (1, 13),
                  flag = wx.ALIGN_CENTRE|wx.SHAPED|wx.TOP, border = 4)

        sizer.AddGrowableRow(11)
        sizer.AddGrowableCol(12)
        self.SetSizer(sizer)

#---------------------------------------------------------------------------------

class LEDDemoPanelBase(LEDDemoPanelGUI):
    def __init__(self, parent):
        LEDDemoPanelGUI.__init__(self, parent)

        self.tg = ToggleGroup(self, (self.align_left,
                              self.align_centre,
                              self.align_right))

        self.digits.Bind(wx.EVT_SPINCTRL, self.OnDigits)
        self.digits.Bind(wx.EVT_TEXT_ENTER, self.OnDigits)
        self.value.Bind(wx.EVT_TEXT_ENTER, self.OnValue)
        self.value.Bind(wx.EVT_TEXT, self.OnValue)
        self.fdSlider.Bind(wx.EVT_COMMAND_SCROLL, self.OnFdValue)
        self.winstyle.Bind(wx.EVT_CHOICE, self.OnWinstyle)
        self.Bind(wx.EVT_BUTTON, self.OnLedstyles)
        self.Bind(wx.EVT_CHECKBOX, self.OnLedstyles)
        self.Bind(csel.EVT_COLOURSELECT, self.OnSelectColour)

        self.aligns = ["align_left", "align_centre", "align_right"]
        self.effects = ["slant", "zfill", "draw_faded", "agg", "allow_colons"]

        self.valuecolours = {"ok" : self.value.GetBackgroundColour(),
                             "error" : (255, 255, 0)}
                             
        self.fd.SetColour(self.led.GetFadeColour())


    def OnDigits(self, evt):
        self.led.SetDigits(self.digits.GetValue())
        evt.Skip()


    def OnValue(self, evt):
        try:
            self.led.SetValue(self.value.GetValue())
            self.value.SetBackgroundColour(self.valuecolours["ok"])

        except ValueError:
            self.led.SetValue("error")
            self.value.SetBackgroundColour(self.valuecolours["error"])

            TipWindow(self,
                      "".join(self.led.GetValidChars())).ShowTip(self.value)

        self.value.Refresh()
        evt.Skip()


    def OnFdValue(self, evt):
        val = self.fdSlider.GetValue()
        self.fdLabel.SetLabel("%s" % val)
        self.led.SetFadeFactor(val)
        self.fd.SetColour(self.led.GetFadeColour())
        evt.Skip()


    def OnLedstyles(self, evt):
        obj = evt.GetEventObject()

        if obj not in [self.bg, self.fg, self.fd]:
            name = obj.GetName(); val = not obj.GetValue()
            flag = getattr(led, "LED_" + name.upper())
            style = self.led.GetLedStyle()

            if name in self.aligns:
                for pname in self.aligns:
                    pflag = getattr(led, "LED_" + pname.upper())
                    if style & pflag:
                        style -= pflag

            for pname in self.aligns + self.effects:
                if name == pname:
                    if val:
                        if style & flag:
                            self.led.SetLedStyle(style - flag)
                    else:
                        self.led.SetLedStyle(style|flag)
                    break

        evt.Skip()


    def OnWinstyle(self, evt):
        size = self.led.GetSize()
        self.led.Freeze()
        self.led.SetWindowStyle(eval(evt.GetString()))
        self.led.SetSize((10, 10))
        self.led.SetSize(size)
        self.led.Thaw()


    def OnSelectColour(self, evt):
        _id = evt.GetId(); colour = evt.GetValue()

        if _id == self.bg.GetId():
            self.led.SetBackgroundColour(colour)
            self.fd.SetColour(self.led.GetFadeColour())

        elif _id == self.fg.GetId():
            self.led.SetForegroundColour(colour)
            self.fd.SetColour(self.led.GetFadeColour())

        elif _id == self.fd.GetId():
            self.led.SetFadeColour(colour)

        self.led.Refresh()

#---------------------------------------------------------------------------------

class LEDDemoPanelCustom(LEDDemoPanelBase):
    def __init__(self, parent):
        LEDDemoPanelBase.__init__(self, parent)

#---------------------------------------------------------------------------------

class LEDDemoPanelCustomGeometry(LEDDemoPanelBase):
    def __init__(self, parent):
        LEDDemoPanelBase.__init__(self, parent)

        geometry = led.LEDGeometry(hv = 40)
        self.led.SetGeometry(geometry)

#---------------------------------------------------------------------------------

class LEDDemoPanelClock(LEDDemoPanelBase):
    def __init__(self, parent):
        LEDDemoPanelBase.__init__(self, parent)

        self.value.Bind(wx.EVT_TEXT_ENTER, None)
        self.value.Bind(wx.EVT_TEXT, None)
        self.value.SetValue("Digital Clock")
        self.value.Disable()

        style = led.LED_DRAW_FADED|   \
                led.LED_ALIGN_CENTRE| \
                led.LED_AGG|          \
                led.LED_ALLOW_COLONS| \
                led.LED_SLANT
        
        self.led.SetLedStyle(style)

        self.tg.OnButton(obj = self.FindWindowByName("align_centre"))
        self.FindWindowByName("allow_colons").SetValue(True)
        self.FindWindowByName("slant").SetValue(True)
        
        fg = (22, 253, 240)
        self.led.SetForegroundColour(fg)
        self.fg.SetColour(fg)
        self.fd.SetColour(self.led.GetFadeColour())
        
        size = self.led.GetSize()
        self.led.Freeze()
        self.led.SetWindowStyle(wx.SUNKEN_BORDER)
        self.led.SetSize((10, 10))
        self.led.SetSize(size)
        self.led.Thaw()
        self.winstyle.SetStringSelection("wx.SUNKEN_BORDER")

        self.tc = -1
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.OnTimer)


    def StartTimer(self):
        self.timer.Start(500)


    def StopTimer(self):
        self.timer.Stop()


    def OnTimer(self, evt):
        self.tc = -self.tc
        t = time.localtime(time.time())
        if self.tc > 0:
            st = time.strftime("%H:%M:%S", t)
        else:
            st = time.strftime("%H%M%S", t)
        self.led.SetValue(st)

#---------------------------------------------------------------------------------


class LEDDemoFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "LEDCtrl Demo")

        nb = self.nb = wx.Notebook(wx.Panel(self, -1), -1)

        p = LEDDemoPanelCustom(nb); p.Fit()
        nb.AddPage(p, "Custom Values")

        p = self.clock = LEDDemoPanelClock(nb); p.Fit()
        nb.AddPage(p, "Digital Clock")

        p = LEDDemoPanelCustomGeometry(nb); p.Fit()
        nb.AddPage(p, "Taller Digits")

        nb.Fit()

        self.SetClientSize(nb.GetSize())
        self.SetMinSize(self.GetSize())

        self.Bind(wx.EVT_SIZE, self.OnSize)
        self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.OnPageChanged)


    def OnSize(self, evt):
        self.nb.SetSize(self.GetClientSize())
        evt.Skip()
        
        
    def OnPageChanged(self, evt):
        if self.nb.GetCurrentPage() == self.clock:
            self.clock.StartTimer()
        else:
            self.clock.StopTimer()
        evt.Skip()

#---------------------------------------------------------------------------------

class LEDDemoApp(wx.App):
    def OnInit(self):
        frame = LEDDemoFrame()
        frame.Show()
        return True

#---------------------------------------------------------------------------------

if __name__ == '__main__':
    app = LEDDemoApp(0)
    app.MainLoop()


#
#
### eof