laurent@703: #!/usr/bin/env python laurent@703: # -*- coding: utf-8 -*- laurent@703: laurent@703: #This file is part of Beremiz, a Integrated Development Environment for laurent@703: #programming IEC 61131-3 automates supporting plcopen standard and CanFestival. laurent@703: # laurent@703: #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD laurent@703: # laurent@703: #See COPYING file for copyrights details. laurent@703: # laurent@703: #This library is free software; you can redistribute it and/or laurent@703: #modify it under the terms of the GNU General Public laurent@703: #License as published by the Free Software Foundation; either laurent@703: #version 2.1 of the License, or (at your option) any later version. laurent@703: # laurent@703: #This library is distributed in the hope that it will be useful, laurent@703: #but WITHOUT ANY WARRANTY; without even the implied warranty of laurent@703: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU laurent@703: #General Public License for more details. laurent@703: # laurent@703: #You should have received a copy of the GNU General Public laurent@703: #License along with this library; if not, write to the Free Software laurent@703: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA laurent@703: laurent@703: import wx laurent@703: laurent@703: [ID_BROWSEVALUESLIBRARYDIALOG, ID_BROWSEVALUESLIBRARYDIALOGSTATICTEXT1, laurent@703: ID_BROWSEVALUESLIBRARYDIALOGVALUESLIBRARY laurent@703: ] = [wx.NewId() for _init_ctrls in range(3)] laurent@703: laurent@703: class BrowseValuesLibraryDialog(wx.Dialog): laurent@703: laurent@703: if wx.VERSION < (2, 6, 0): laurent@703: def Bind(self, event, function, id = None): laurent@703: if id is not None: laurent@703: event(self, id, function) laurent@703: else: laurent@703: event(self, function) laurent@703: laurent@703: def _init_coll_flexGridSizer1_Items(self, parent): laurent@703: parent.AddWindow(self.staticText1, 0, border=20, flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT) laurent@703: parent.AddWindow(self.ValuesLibrary, 0, border=20, flag=wx.GROW|wx.LEFT|wx.RIGHT) laurent@703: parent.AddSizer(self.ButtonSizer, 0, border=20, flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT) laurent@703: laurent@703: def _init_coll_flexGridSizer1_Growables(self, parent): laurent@703: parent.AddGrowableCol(0) laurent@703: parent.AddGrowableRow(1) laurent@703: laurent@703: def _init_sizers(self): laurent@703: self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10) laurent@703: laurent@703: self._init_coll_flexGridSizer1_Items(self.flexGridSizer1) laurent@703: self._init_coll_flexGridSizer1_Growables(self.flexGridSizer1) laurent@703: laurent@703: self.SetSizer(self.flexGridSizer1) laurent@703: laurent@703: def _init_ctrls(self, prnt, name): laurent@703: wx.Dialog.__init__(self, id=ID_BROWSEVALUESLIBRARYDIALOG, laurent@703: name='BrowseValueDialog', parent=prnt, laurent@703: size=wx.Size(600, 400), style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER, laurent@703: title=_('Browse %s library') % name) laurent@703: self.SetClientSize(wx.Size(600, 400)) laurent@703: laurent@703: self.staticText1 = wx.StaticText(id=ID_BROWSEVALUESLIBRARYDIALOGSTATICTEXT1, laurent@703: label=_('Choose a %s:') % name, name='staticText1', parent=self, laurent@703: pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) laurent@703: laurent@703: self.ValuesLibrary = wx.TreeCtrl(id=ID_BROWSEVALUESLIBRARYDIALOGVALUESLIBRARY, laurent@703: name='ValuesLibrary', parent=self, pos=wx.Point(0, 0), laurent@703: size=wx.Size(0, 0), style=wx.TR_HAS_BUTTONS|wx.TR_SINGLE|wx.SUNKEN_BORDER|wx.TR_HIDE_ROOT|wx.TR_LINES_AT_ROOT) laurent@703: laurent@703: self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE) laurent@703: if wx.VERSION >= (2, 5, 0): laurent@703: self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetAffirmativeButton().GetId()) laurent@703: else: laurent@703: self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetChildren()[0].GetSizer().GetChildren()[0].GetWindow().GetId()) laurent@703: laurent@703: self._init_sizers() laurent@703: laurent@703: def __init__(self, parent, name, library, default=None): laurent@703: self._init_ctrls(parent, name) laurent@703: laurent@703: root = self.ValuesLibrary.AddRoot("") laurent@703: self.GenerateValuesLibraryBranch(root, library, default) laurent@703: laurent@703: def GenerateValuesLibraryBranch(self, root, children, default): laurent@703: for infos in children: laurent@703: item = self.ValuesLibrary.AppendItem(root, infos["name"]) laurent@703: self.ValuesLibrary.SetPyData(item, infos["infos"]) laurent@703: if infos["infos"] is not None and infos["infos"] == default: laurent@703: self.ValuesLibrary.SelectItem(item) laurent@703: self.ValuesLibrary.EnsureVisible(item) laurent@703: self.GenerateValuesLibraryBranch(item, infos["children"], default) laurent@703: laurent@703: def GetValueInfos(self): laurent@703: selected = self.ValuesLibrary.GetSelection() laurent@703: return self.ValuesLibrary.GetPyData(selected) laurent@703: laurent@703: def OnOK(self, event): laurent@703: selected = self.ValuesLibrary.GetSelection() laurent@703: if not selected.IsOk() or self.ValuesLibrary.GetPyData(selected) is None: laurent@703: message = wx.MessageDialog(self, _("No valid value selected!"), _("Error"), wx.OK|wx.ICON_ERROR) laurent@703: message.ShowModal() laurent@703: message.Destroy() laurent@703: else: laurent@703: self.EndModal(wx.ID_OK) laurent@703: