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: 
Laurent@814: import wx
surkovsv93@1556: from plcopen.plcopen import *
Laurent@814: 
andrej@1618: class FindInPouDialog(wx.Dialog):
Laurent@814: 
andrej@1490:     def _init_icon(self, parent):
andrej@1490:         if parent and parent.icon:
andrej@1490:                 self.SetIcon(parent.icon)
andrej@1490: 
andrej@1490:     
Laurent@814:     def __init__(self, parent):
andrej@1618:         wx.Dialog.__init__(self, parent, title=_("Find"),         
andrej@1618:               size=wx.Size(500, 280), style=wx.CAPTION|
Laurent@814:                                             wx.CLOSE_BOX|
Laurent@814:                                             wx.CLIP_CHILDREN|
andrej@1618:                                             wx.RESIZE_BORDER)
Laurent@814:         
andrej@1490:         self._init_icon(parent)
Laurent@814:         panel = wx.Panel(self, style=wx.TAB_TRAVERSAL)
Laurent@814:         
Laurent@814:         main_sizer = wx.FlexGridSizer(cols=1, hgap=5, rows=2, vgap=5)
Laurent@814:         main_sizer.AddGrowableCol(0)
Laurent@814:         main_sizer.AddGrowableRow(0)
Laurent@814:         
Laurent@814:         controls_sizer = wx.BoxSizer(wx.VERTICAL)
Laurent@814:         main_sizer.AddSizer(controls_sizer, border=20, 
Laurent@814:               flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
Laurent@814:         
Laurent@814:         patterns_sizer = wx.FlexGridSizer(cols=2, hgap=5, rows=1, vgap=5)
Laurent@814:         patterns_sizer.AddGrowableCol(1)
Laurent@814:         controls_sizer.AddSizer(patterns_sizer, border=5, flag=wx.GROW|wx.BOTTOM)
Laurent@814:         
Laurent@814:         find_label = wx.StaticText(panel, label=_("Find:"))
Laurent@814:         patterns_sizer.AddWindow(find_label, flag=wx.ALIGN_CENTER_VERTICAL)
Laurent@814:         
Laurent@814:         self.FindPattern = wx.TextCtrl(panel)
Laurent@814:         self.Bind(wx.EVT_TEXT, self.OnFindPatternChanged, self.FindPattern)
surkovsv93@1558:         self.Bind(wx.EVT_CHAR_HOOK, self.OnEscapeKey)
Laurent@814:         patterns_sizer.AddWindow(self.FindPattern, flag=wx.GROW)
Laurent@814:         
Laurent@814:         params_sizer = wx.BoxSizer(wx.HORIZONTAL)
Laurent@814:         controls_sizer.AddSizer(params_sizer, border=5, flag=wx.GROW|wx.BOTTOM)
Laurent@814:         
Laurent@814:         direction_staticbox = wx.StaticBox(panel, label=_("Direction"))
Laurent@814:         direction_staticboxsizer = wx.StaticBoxSizer(
Laurent@814:               direction_staticbox, wx.VERTICAL)
Laurent@814:         params_sizer.AddSizer(direction_staticboxsizer, 1, border=5, 
Laurent@814:               flag=wx.GROW|wx.RIGHT)
Laurent@814:         
Laurent@814:         self.Forward = wx.RadioButton(panel, label=_("Forward"), 
Laurent@814:               style=wx.RB_GROUP)
Laurent@814:         direction_staticboxsizer.AddWindow(self.Forward, border=5, 
Laurent@814:               flag=wx.ALL|wx.GROW)
Laurent@814:         
Laurent@814:         self.Backward = wx.RadioButton(panel, label=_("Backward"))
Laurent@814:         direction_staticboxsizer.AddWindow(self.Backward, border=5, 
Laurent@814:               flag=wx.ALL|wx.GROW)
Laurent@814:         
Laurent@814:         options_staticbox = wx.StaticBox(panel, label=_("Options"))
Laurent@814:         options_staticboxsizer = wx.StaticBoxSizer(
Laurent@814:               options_staticbox, wx.VERTICAL)
Laurent@814:         params_sizer.AddSizer(options_staticboxsizer, 1, flag=wx.GROW)
Laurent@814:         
Laurent@814:         self.CaseSensitive = wx.CheckBox(panel, label=_("Case sensitive"))
Laurent@814:         self.CaseSensitive.SetValue(True)
Laurent@814:         options_staticboxsizer.AddWindow(self.CaseSensitive, border=5, 
Laurent@814:               flag=wx.ALL|wx.GROW)
Laurent@814:         
Laurent@814:         self.WrapSearch = wx.CheckBox(panel, label=_("Wrap search"))
Laurent@814:         self.WrapSearch.SetValue(True)
Laurent@814:         options_staticboxsizer.AddWindow(self.WrapSearch, border=5, 
Laurent@814:               flag=wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.GROW)
Laurent@814:         
Laurent@814:         self.RegularExpressions = wx.CheckBox(panel, label=_("Regular expressions"))
Laurent@814:         options_staticboxsizer.AddWindow(self.RegularExpressions, border=5, 
Laurent@814:               flag=wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.GROW)
Laurent@814:         
Laurent@814:         buttons_sizer = wx.BoxSizer(wx.HORIZONTAL)
Laurent@814:         main_sizer.AddSizer(buttons_sizer, border=20, 
Laurent@814:               flag=wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.ALIGN_RIGHT)
Laurent@814:         
Laurent@814:         self.FindButton = wx.Button(panel, label=_("Find"))
Laurent@1098:         self.FindButton.SetDefault()
Laurent@814:         self.Bind(wx.EVT_BUTTON, self.OnFindButton, self.FindButton)
Laurent@814:         buttons_sizer.AddWindow(self.FindButton, border=5, flag=wx.RIGHT)
Laurent@814:         
andrej@1490:         self.CloseButton = wx.Button(panel, label=_("Close"))
Laurent@814:         self.Bind(wx.EVT_BUTTON, self.OnCloseButton, self.CloseButton)
Laurent@814:         buttons_sizer.AddWindow(self.CloseButton)
andrej@1618: 
andrej@1618:         self.StatusLabel = wx.StaticText(panel, label= "")
andrej@1618:         controls_sizer.AddWindow(self.StatusLabel, flag=wx.ALIGN_CENTER_VERTICAL)
Laurent@814:         
Laurent@814:         panel.SetSizer(main_sizer)
andrej@1620:         main_sizer.Fit(self)
Laurent@814:         
Laurent@814:         self.ParentWindow = parent
Laurent@814:         
Laurent@814:         self.Bind(wx.EVT_CLOSE, self.OnCloseFrame)
surkovsv93@1556:         self.infosPrev = {}
surkovsv93@1556:         self.criteria = {}
Laurent@1056:         self.FindPattern.SetFocus()
Laurent@814:         self.RefreshButtonsState()
Laurent@814:     
Laurent@814:     def RefreshButtonsState(self):
Laurent@814:         find_pattern = self.FindPattern.GetValue()
Laurent@814:         self.FindButton.Enable(find_pattern != "")
Laurent@814:     
Laurent@814:     def OnCloseFrame(self, event):
Laurent@814:         self.Hide()
Laurent@814:         event.Veto()
Laurent@814:         
Laurent@814:     def OnCloseButton(self, event):
Laurent@814:         self.Hide()
Laurent@814:         event.Skip()
Laurent@814: 
surkovsv93@1558:     def OnEscapeKey(self, event):
surkovsv93@1558:         keycode = event.GetKeyCode()
surkovsv93@1558:         if keycode == wx.WXK_ESCAPE:
surkovsv93@1558:             self.OnCloseButton(event)
surkovsv93@1558:         else:
surkovsv93@1558:             event.Skip()
surkovsv93@1558: 
Laurent@814:     def OnFindPatternChanged(self, event):
Laurent@814:         self.RefreshButtonsState()
Laurent@814:         event.Skip()
Laurent@814: 
andrej@1618:     def SetStatusText(self, msg):
andrej@1618:         self.StatusLabel.SetLabel(msg)
andrej@1618:         self.Layout()
andrej@1618:         
Laurent@814:     def OnFindButton(self, event):
Laurent@814:         infos = {
Laurent@814:             "find_pattern": self.FindPattern.GetValue(),
Laurent@814:             "wrap": self.WrapSearch.GetValue(),
Laurent@814:             "case_sensitive": self.CaseSensitive.GetValue(),
surkovsv93@1556:             "regular_expression": self.RegularExpressions.GetValue(),
surkovsv93@1556:             "filter": "all"}
surkovsv93@1556: 
surkovsv93@1556:         if self.infosPrev != infos:
surkovsv93@1556:             self.infosPrev = infos
surkovsv93@1556:             message = ""
surkovsv93@1556:             try:
surkovsv93@1556:                 self.criteria = infos
surkovsv93@1556:                 CompilePattern(self.criteria)
surkovsv93@1556:             except:
surkovsv93@1556:                 self.criteria.clear()
surkovsv93@1556:                 message = _("Syntax error in regular expression of pattern to search!")
surkovsv93@1556:             self.SetStatusText(message)
surkovsv93@1556:         if len(self.criteria) > 0:
surkovsv93@1556:             wx.CallAfter(self.ParentWindow.FindInPou,
surkovsv93@1556:                 {True: 1, False:-1}[self.Forward.GetValue()],
surkovsv93@1556:                 self.criteria)
Laurent@814:         event.Skip()