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
andrej@1696: # Copyright (C) 2017: Andrey Skvortsov <andrej.skvortzov@gmail.com>
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
surkovsv93@1556: from plcopen.plcopen import *
Laurent@814: 
andrej@1736: 
andrej@1618: class FindInPouDialog(wx.Dialog):
Laurent@814: 
andrej@1490:     def _init_icon(self, parent):
andrej@1490:         if parent and parent.icon:
andrej@1838:             self.SetIcon(parent.icon)
andrej@1490: 
Laurent@814:     def __init__(self, parent):
andrej@1768:         wx.Dialog.__init__(
andrej@1768:             self, parent, title=_("Find"),
andrej@1768:             style=wx.CAPTION | wx.CLOSE_BOX | wx.CLIP_CHILDREN | wx.RESIZE_BORDER)
andrej@1730: 
andrej@1490:         self._init_icon(parent)
Laurent@814:         panel = wx.Panel(self, style=wx.TAB_TRAVERSAL)
andrej@1730: 
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)
andrej@1730: 
Laurent@814:         controls_sizer = wx.BoxSizer(wx.VERTICAL)
andrej@1730:         main_sizer.AddSizer(controls_sizer, border=20,
andrej@1768:                             flag=wx.GROW | wx.TOP | wx.LEFT | wx.RIGHT)
andrej@1730: 
Laurent@814:         patterns_sizer = wx.FlexGridSizer(cols=2, hgap=5, rows=1, vgap=5)
Laurent@814:         patterns_sizer.AddGrowableCol(1)
andrej@1745:         controls_sizer.AddSizer(patterns_sizer, border=5, flag=wx.GROW | wx.BOTTOM)
andrej@1730: 
Laurent@814:         find_label = wx.StaticText(panel, label=_("Find:"))
Laurent@814:         patterns_sizer.AddWindow(find_label, flag=wx.ALIGN_CENTER_VERTICAL)
andrej@1730: 
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)
andrej@1730: 
Laurent@814:         params_sizer = wx.BoxSizer(wx.HORIZONTAL)
andrej@1745:         controls_sizer.AddSizer(params_sizer, border=5, flag=wx.GROW | wx.BOTTOM)
andrej@1730: 
Laurent@814:         direction_staticbox = wx.StaticBox(panel, label=_("Direction"))
Laurent@814:         direction_staticboxsizer = wx.StaticBoxSizer(
andrej@1878:             direction_staticbox, wx.VERTICAL)
andrej@1730:         params_sizer.AddSizer(direction_staticboxsizer, 1, border=5,
andrej@1768:                               flag=wx.GROW | wx.RIGHT)
andrej@1730: 
andrej@1730:         self.Forward = wx.RadioButton(panel, label=_("Forward"),
andrej@1768:                                       style=wx.RB_GROUP)
andrej@1730:         direction_staticboxsizer.AddWindow(self.Forward, border=5,
andrej@1768:                                            flag=wx.ALL | wx.GROW)
andrej@1730: 
Laurent@814:         self.Backward = wx.RadioButton(panel, label=_("Backward"))
andrej@1730:         direction_staticboxsizer.AddWindow(self.Backward, border=5,
andrej@1768:                                            flag=wx.ALL | wx.GROW)
andrej@1730: 
Laurent@814:         options_staticbox = wx.StaticBox(panel, label=_("Options"))
Laurent@814:         options_staticboxsizer = wx.StaticBoxSizer(
andrej@1878:             options_staticbox, wx.VERTICAL)
Laurent@814:         params_sizer.AddSizer(options_staticboxsizer, 1, flag=wx.GROW)
andrej@1730: 
Laurent@814:         self.CaseSensitive = wx.CheckBox(panel, label=_("Case sensitive"))
Laurent@814:         self.CaseSensitive.SetValue(True)
andrej@1730:         options_staticboxsizer.AddWindow(self.CaseSensitive, border=5,
andrej@1768:                                          flag=wx.ALL | wx.GROW)
andrej@1730: 
Laurent@814:         self.WrapSearch = wx.CheckBox(panel, label=_("Wrap search"))
Laurent@814:         self.WrapSearch.SetValue(True)
andrej@1730:         options_staticboxsizer.AddWindow(self.WrapSearch, border=5,
andrej@1768:                                          flag=wx.LEFT | wx.RIGHT | wx.BOTTOM | wx.GROW)
andrej@1730: 
Laurent@814:         self.RegularExpressions = wx.CheckBox(panel, label=_("Regular expressions"))
andrej@1730:         options_staticboxsizer.AddWindow(self.RegularExpressions, border=5,
andrej@1768:                                          flag=wx.LEFT | wx.RIGHT | wx.BOTTOM | wx.GROW)
andrej@1730: 
Laurent@814:         buttons_sizer = wx.BoxSizer(wx.HORIZONTAL)
andrej@1730:         main_sizer.AddSizer(buttons_sizer, border=20,
andrej@1768:                             flag=wx.LEFT | wx.RIGHT | wx.BOTTOM | wx.ALIGN_RIGHT)
andrej@1730: 
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)
andrej@1730: 
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@1696:         # set the longest message here, to use it length to calculate
andrej@1696:         # optimal size of dialog window
andrej@1696:         self.RegExpSyntaxErrMsg = _("Syntax error in regular expression of pattern to search!")
andrej@1744:         self.StatusLabel = wx.StaticText(panel, label=self.RegExpSyntaxErrMsg)
andrej@1618:         controls_sizer.AddWindow(self.StatusLabel, flag=wx.ALIGN_CENTER_VERTICAL)
andrej@1730: 
Laurent@814:         panel.SetSizer(main_sizer)
andrej@1620:         main_sizer.Fit(self)
andrej@1696: 
andrej@1696:         # clear message after dialog size calculation
andrej@1696:         self.SetStatusText("")
andrej@1696: 
Laurent@814:         self.ParentWindow = parent
andrej@1730: 
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()
andrej@1730: 
Laurent@814:     def RefreshButtonsState(self):
Laurent@814:         find_pattern = self.FindPattern.GetValue()
Laurent@814:         self.FindButton.Enable(find_pattern != "")
andrej@1730: 
Laurent@814:     def OnCloseFrame(self, event):
Laurent@814:         self.Hide()
Laurent@814:         event.Veto()
andrej@1730: 
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@1730: 
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)
andrej@1780:             except Exception:
surkovsv93@1556:                 self.criteria.clear()
andrej@1696:                 message = self.RegExpSyntaxErrMsg
surkovsv93@1556:             self.SetStatusText(message)
surkovsv93@1556:         if len(self.criteria) > 0:
surkovsv93@1556:             wx.CallAfter(self.ParentWindow.FindInPou,
andrej@1768:                          {True: 1, False: -1}[self.Forward.GetValue()],
andrej@1768:                          self.criteria)
Laurent@814:         event.Skip()