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) 2007: 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@814: import wx
Laurent@814: 
Laurent@814: from dialogs.DurationEditorDialog import DurationEditorDialog
Laurent@814: 
andrej@1736: 
Laurent@814: class DurationCellControl(wx.PyControl):
andrej@1730: 
Laurent@814:     '''
Laurent@814:     Custom cell editor control with a text box and a button that launches
Laurent@814:     the DurationEditorDialog.
Laurent@814:     '''
Laurent@814:     def __init__(self, parent):
andrej@1836:         wx.PyControl.__init__(self, parent)
andrej@1730: 
Laurent@814:         main_sizer = wx.FlexGridSizer(cols=2, hgap=0, rows=1, vgap=0)
Laurent@814:         main_sizer.AddGrowableCol(0)
Laurent@814:         main_sizer.AddGrowableRow(0)
andrej@1730: 
Laurent@814:         # create location text control
andrej@1730:         self.Duration = wx.TextCtrl(self, size=wx.Size(0, -1),
andrej@1768:                                     style=wx.TE_PROCESS_ENTER)
Laurent@814:         self.Duration.Bind(wx.EVT_KEY_DOWN, self.OnDurationChar)
Laurent@814:         main_sizer.AddWindow(self.Duration, flag=wx.GROW)
andrej@1730: 
Laurent@814:         # create browse button
Laurent@814:         self.EditButton = wx.Button(self, label='...', size=wx.Size(30, -1))
Laurent@814:         self.Bind(wx.EVT_BUTTON, self.OnEditButtonClick, self.EditButton)
Laurent@814:         main_sizer.AddWindow(self.EditButton, flag=wx.GROW)
andrej@1730: 
Laurent@814:         self.Bind(wx.EVT_SIZE, self.OnSize)
andrej@1730: 
Laurent@814:         self.SetSizer(main_sizer)
andrej@1730: 
Laurent@814:         self.Default = None
andrej@1730: 
Laurent@814:     def SetValue(self, value):
Laurent@814:         self.Default = value
Laurent@814:         self.Duration.SetValue(value)
andrej@1730: 
Laurent@814:     def GetValue(self):
Laurent@814:         return self.Duration.GetValue()
Laurent@814: 
Laurent@814:     def OnSize(self, event):
Laurent@814:         self.Layout()
Laurent@814: 
Laurent@814:     def OnEditButtonClick(self, event):
Laurent@814:         # pop up the Duration Editor dialog
Laurent@814:         dialog = DurationEditorDialog(self)
Laurent@814:         dialog.SetDuration(self.Duration.GetValue())
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             # set the duration
Laurent@814:             self.Duration.SetValue(dialog.GetDuration())
Laurent@814: 
Laurent@814:         dialog.Destroy()
Laurent@814: 
Laurent@814:         self.Duration.SetFocus()
Laurent@814: 
Laurent@814:     def OnDurationChar(self, event):
Laurent@814:         keycode = event.GetKeyCode()
Laurent@814:         if keycode in [wx.WXK_RETURN, wx.WXK_TAB]:
Laurent@814:             self.Parent.Parent.ProcessEvent(event)
Laurent@814:         elif keycode == wx.WXK_ESCAPE:
Laurent@814:             self.Duration.SetValue(self.Default)
Laurent@814:             self.Parent.Parent.CloseEditControl()
Laurent@814:         else:
Laurent@814:             event.Skip()
Laurent@814: 
Laurent@814:     def SetInsertionPoint(self, i):
Laurent@814:         self.Duration.SetInsertionPoint(i)
andrej@1730: 
Laurent@814:     def SetFocus(self):
Laurent@814:         self.Duration.SetFocus()
Laurent@814: 
andrej@1736: 
Laurent@814: class DurationCellEditor(wx.grid.PyGridCellEditor):
Laurent@814:     '''
Laurent@814:     Grid cell editor that uses DurationCellControl to display an edit button.
Laurent@814:     '''
Laurent@1275:     def __init__(self, table, colname):
Laurent@814:         wx.grid.PyGridCellEditor.__init__(self)
andrej@1730: 
Laurent@814:         self.Table = table
Laurent@1275:         self.Colname = colname
andrej@1730: 
Laurent@814:     def __del__(self):
Laurent@814:         self.CellControl = None
andrej@1730: 
Laurent@814:     def Create(self, parent, id, evt_handler):
Laurent@814:         self.CellControl = DurationCellControl(parent)
Laurent@814:         self.SetControl(self.CellControl)
Laurent@814:         if evt_handler:
Laurent@814:             self.CellControl.PushEventHandler(evt_handler)
Laurent@814: 
Laurent@814:     def BeginEdit(self, row, col, grid):
Laurent@814:         self.CellControl.Enable()
Laurent@1275:         self.CellControl.SetValue(self.Table.GetValueByName(row, self.Colname))
Laurent@814:         self.CellControl.SetFocus()
Laurent@814: 
andrej@1499:     def EndEditInternal(self, row, col, grid, old_duration):
Laurent@814:         duration = self.CellControl.GetValue()
Laurent@814:         changed = duration != old_duration
Laurent@814:         if changed:
Laurent@1275:             self.Table.SetValueByName(row, self.Colname, duration)
Laurent@814:         self.CellControl.Disable()
Laurent@814:         return changed
Laurent@814: 
andrej@1499:     if wx.VERSION >= (3, 0, 0):
andrej@1499:         def EndEdit(self, row, col, grid, oldval):
andrej@1553:             return self.EndEditInternal(row, col, grid, oldval)
andrej@1499:     else:
andrej@1499:         def EndEdit(self, row, col, grid):
andrej@1730:             oldval = self.Table.GetValueByName(row, self.Colname)
andrej@1730:             return self.EndEditInternal(row, col, grid, oldval)
andrej@1499: 
Laurent@814:     def SetSize(self, rect):
Laurent@814:         self.CellControl.SetDimensions(rect.x + 1, rect.y,
andrej@1767:                                        rect.width, rect.height,
andrej@1767:                                        wx.SIZE_ALLOW_MINUS_ONE)
Laurent@814: 
Laurent@814:     def Clone(self):
andrej@1865:         return DurationCellEditor(self.Table, self.Colname)