# ImageButton.py : Button-like class that draws directional triangles
#
# Distributed under the terms described in the LICENCE file.
#
# Developed by akuchlin@mems-exchange.org
#
# This class simulates a button, but instead of only rendering a simple text
# string, it draws a triangle pointing up, down, left, or right.  It's used
# for the buttons surrounding the microscope display.

__revision__ = "$Id: ImageButton.py,v 1.14 2002/01/04 15:57:50 akuchlin Exp $"

from java import awt
from javax import swing

class ImageButton(swing.JButton):
    UP = 'Up'
    DOWN = 'Down'
    LEFT = 'Left'
    RIGHT = 'Right'

    def __init__ (self, client, orientation, moveable=1):
        swing.JButton.__init__(self)
        self.client = client
        self.moveable = moveable
        self.orientation = orientation

        self.mousePressed = self.pressed
        self.mouseReleased = self.released
        self.isPressed = 0
        self.setSize(awt.Dimension(32,32))
            
    def pressed (self, e):
        # First, check that button 1 was pressed
        if ((e.getModifiers() & awt.event.InputEvent.BUTTON2_MASK)
             or (e.getModifiers() & awt.event.InputEvent.BUTTON3_MASK)):
            return
        self.isPressed = 1
        self.repaint()

    def released (self, e):
        # First, check that button 1 was pressed
        if ((e.getModifiers() & awt.event.InputEvent.BUTTON2_MASK)
             or (e.getModifiers() & awt.event.InputEvent.BUTTON3_MASK)):
            return
        self.isPressed = 0
        self.repaint()
        e = awt.event.ActionEvent(self, awt.event.ActionEvent.ACTION_PERFORMED,
                                  self.orientation)
        self.client.actionPerformed(e)

    def paint (self, gc):
        size = self.getSize()
        w,h = size.width, size.height
        gc.setColor(awt.Color.lightGray)
        gc.fillRect(0,0, w,h)
        gc.setColor(awt.Color.black)
        gc.drawRect(0,0, w-1,h-1)

        width = 25
        if self.orientation == ImageButton.UP:
            width = (h-2 - 2) /2
            xCoordList, yCoordList = [w/2, w/2+width, w/2-width], [2, h-2, h-2]
        elif self.orientation == ImageButton.DOWN:
            width = (h-2 - 2) /2
            xCoordList, yCoordList = [w/2, w/2+width, w/2-width], [h-2, 2, 2]
        elif self.orientation == ImageButton.LEFT:
            width = (w-2 - 2) /2
            xCoordList, yCoordList = [2, w-2, w-2], [h/2, h/2-width, h/2+width]
        elif self.orientation == ImageButton.RIGHT:
            width = (w-2 - 2) /2
            xCoordList, yCoordList = [2,2,w-2], [h/2-width, h/2+width, h/2]
        
        if self.isPressed and self.moveable:
            gc.setColor(awt.Color.black)
            gc.drawLine(0, 0, w - 1, 0)
            gc.drawLine(0, 0, 0, h - 1)
            gc.setColor(awt.Color.gray)
            gc.drawLine(1, 1, w - 2, 1)
            gc.drawLine(1, 1, 1, h - 2)
            gc.setColor(awt.Color.lightGray)
            gc.drawLine(1, h - 2, w - 2, h - 2)
            gc.drawLine(w - 2, h - 2, w - 2, 1)
            gc.setColor(awt.Color.white)
            gc.drawLine(0, h - 1, w - 1, h - 1)
            gc.drawLine(w - 1, h - 1, w - 1, 0)
            gc.clipRect(2, 2, w - 4, h - 4)

            xCoordList = map(lambda x: x+1, xCoordList)
            yCoordList = map(lambda y: y+1, yCoordList)

        else:
            gc.setColor(awt.Color.white)
            gc.drawLine(0, 0, w - 1, 0)
            gc.drawLine(0, 0, 0, h - 1)
            gc.drawLine(1, 1, w - 2, 1)
            gc.drawLine(1, 1, 1, h - 2)
            gc.setColor(awt.Color.black)
            gc.drawLine(0, h - 1, w - 1, h - 1)
            gc.drawLine(w - 1, h - 1, w - 1, 0)
            gc.setColor(awt.Color.gray)
            gc.drawLine(1, h - 2, w - 2, h - 2)
            gc.drawLine(w - 2, h - 2, w - 2, 1)
            gc.clipRect(2, 2, w - 4, h - 4)

        gc.setColor(awt.Color.gray)
        gc.fillPolygon(xCoordList, yCoordList, 3)
        gc.setColor(awt.Color.black)
        gc.drawPolygon(xCoordList, yCoordList, 3)

        def getPreferredSize (self):
            return awt.Dimension(w+4, self.im_h+4)
        getMinimumSize = getMaximumSize = getPreferredSize

if __name__ == '__main__':
    f = swing.JFrame("ImageButton demo")
    t = f.getToolkit()
    class dummyClient:
        def actionPerformed (self, e):
            print 'Button clicked; event=', repr(e)
            
    b = ImageButton(dummyClient(), ImageButton.UP)
    b.validate()
    f.getContentPane().add(b)
    f.pack()
    f.show()
    
