Laurent@814: #!/usr/bin/env python
Laurent@814: # -*- coding: utf-8 -*-
Laurent@814: 
andrej@1571: # This file is part of Beremiz, a Integrated Development Environment for
andrej@1571: # programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
Laurent@814: #
andrej@1571: # Copyright (C) 2012: Edouard TISSERANT and Laurent BESSARD
Laurent@814: #
andrej@1571: # See COPYING file for copyrights details.
Laurent@814: #
andrej@1571: # This program is free software; you can redistribute it and/or
andrej@1571: # modify it under the terms of the GNU General Public License
andrej@1571: # as published by the Free Software Foundation; either version 2
andrej@1571: # of the License, or (at your option) any later version.
Laurent@814: #
andrej@1571: # This program is distributed in the hope that it will be useful,
andrej@1571: # but WITHOUT ANY WARRANTY; without even the implied warranty of
andrej@1571: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
andrej@1571: # GNU General Public License for more details.
Laurent@814: #
andrej@1571: # You should have received a copy of the GNU General Public License
andrej@1571: # along with this program; if not, write to the Free Software
andrej@1571: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Laurent@814: 
andrej@1881: 
andrej@1881: from __future__ import absolute_import
Laurent@1199: import wx
Laurent@814: 
Laurent@814: from util.BitmapLibrary import GetBitmap
Laurent@814: 
andrej@1782: # -------------------------------------------------------------------------------
Laurent@1199: #                        Custom button for Graphic Viewer Class
andrej@1782: # -------------------------------------------------------------------------------
Laurent@814: 
Laurent@1198: 
andrej@1831: class GraphButton(object):
andrej@1736:     """
andrej@1736:     Class that implements a custom button for graphic Viewer
andrej@1736:     """
andrej@1730: 
Laurent@1198:     def __init__(self, x, y, bitmap, callback):
Laurent@1199:         """
Laurent@1199:         Constructor
Laurent@1199:         @param x: X coordinate of Button in Graphic Viewer
Laurent@1199:         @param y: Y coordinate of Button in Graphic Viewer
Laurent@1199:         @param bitmap: Name of bitmap to use for button
Laurent@1199:         @param callback: Reference to function to call when button is pressed
Laurent@1199:         """
Laurent@1199:         # Save button position
Laurent@1199:         self.SetPosition(x, y)
Laurent@1267:         # Set button bitmap
Laurent@1267:         self.SetBitmap(bitmap)
andrej@1730: 
Laurent@1209:         # By default button is hide and enabled
Laurent@1209:         self.Shown = False
Laurent@1198:         self.Enabled = True
andrej@1730: 
Laurent@1199:         # Save reference to callback function
Laurent@1198:         self.Callback = callback
andrej@1730: 
Laurent@1198:     def __del__(self):
Laurent@1199:         """
Laurent@1199:         Destructor
Laurent@1199:         """
Laurent@1199:         # Remove reference to callback function
Laurent@1198:         self.callback = None
andrej@1730: 
Laurent@1267:     def SetBitmap(self, bitmap):
Laurent@1267:         """
Laurent@1267:         Set bitmap to use for button
Laurent@1267:         @param bitmap: Name of bitmap to use for button
Laurent@1267:         """
Laurent@1267:         # Get wx.Bitmap object corresponding to bitmap
Laurent@1267:         self.Bitmap = GetBitmap(bitmap)
andrej@1730: 
Laurent@1198:     def GetSize(self):
Laurent@1199:         """
Laurent@1199:         Return size of button
Laurent@1199:         @return: wx.Size object containing button size
Laurent@1199:         """
Laurent@1199:         # Button size is size of bitmap
Laurent@1198:         return self.Bitmap.GetSize()
andrej@1730: 
Laurent@1198:     def SetPosition(self, x, y):
Laurent@1199:         """
Laurent@1199:         Set button position
Laurent@1199:         @param x: X coordinate of Button in Graphic Viewer
Laurent@1199:         @param y: Y coordinate of Button in Graphic Viewer
Laurent@1199:         """
Laurent@1198:         self.Position = wx.Point(x, y)
andrej@1730: 
Laurent@1199:     def Show(self, show=True):
Laurent@1199:         """
Laurent@1199:         Mark if button to be displayed in Graphic Viewer
Laurent@1199:         @param show: True if button to be displayed in Graphic Viewer
Laurent@1199:         (default True)
Laurent@1199:         """
Laurent@1199:         self.Shown = show
andrej@1730: 
Laurent@1198:     def Hide(self):
Laurent@1199:         """
Laurent@1199:         Hide button from Graphic Viewer
Laurent@1199:         """
Laurent@1199:         self.Show(False)
andrej@1730: 
Laurent@1198:     def IsShown(self):
Laurent@1199:         """
Laurent@1199:         Return if button is displayed in Graphic Viewer
Laurent@1199:         @return: True if button is displayed in Graphic Viewer
Laurent@1199:         """
Laurent@1198:         return self.Shown
andrej@1730: 
Laurent@1199:     def Enable(self, enable=True):
Laurent@1199:         """
Laurent@1199:         Mark if button is active in Graphic Viewer
Laurent@1199:         @param enable: True if button is active in Graphic Viewer
Laurent@1199:         (default True)
Laurent@1199:         """
Laurent@1200:         self.Enabled = enable
andrej@1730: 
Laurent@1198:     def Disable(self):
Laurent@1199:         """
Laurent@1199:         Deactivate button in Graphic Viewer
Laurent@1199:         """
Laurent@1198:         self.Enabled = False
andrej@1730: 
Laurent@1198:     def IsEnabled(self):
Laurent@1199:         """
Laurent@1199:         Return if button is active in Graphic Viewer
Laurent@1199:         @return: True if button is active in Graphic Viewer
Laurent@1199:         """
Laurent@1198:         return self.Enabled
andrej@1730: 
Laurent@1198:     def HitTest(self, x, y):
Laurent@1199:         """
Laurent@1199:         Test if point is inside button
Laurent@1199:         @param x: X coordinate of point
Laurent@1199:         @param y: Y coordinate of point
Laurent@1199:         @return: True if button is active and displayed and point is inside
andrej@1730:         button
Laurent@1199:         """
Laurent@1199:         # Return immediately if button is hidden or inactive
Laurent@1199:         if not (self.IsShown() and self.IsEnabled()):
Laurent@1199:             return False
andrej@1730: 
Laurent@1199:         # Test if point is inside button
Laurent@1199:         w, h = self.Bitmap.GetSize()
Laurent@1199:         rect = wx.Rect(self.Position.x, self.Position.y, w, h)
Laurent@1199:         return rect.InsideXY(x, y)
andrej@1730: 
Laurent@1198:     def ProcessCallback(self):
Laurent@1199:         """
Laurent@1199:         Call callback function if defined
Laurent@1199:         """
Laurent@1198:         if self.Callback is not None:
Laurent@1200:             self.Callback()
andrej@1730: 
Laurent@1198:     def Draw(self, dc):
Laurent@1199:         """
Laurent@1199:         Draw button in Graphic Viewer
Laurent@1199:         @param dc: wx.DC object corresponding to Graphic Viewer device context
Laurent@1199:         """
Laurent@1199:         # Only draw button if button is active and displayed
Laurent@1198:         if self.Shown and self.Enabled:
Laurent@1198:             dc.DrawBitmap(self.Bitmap, self.Position.x, self.Position.y, True)