Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2695_dataset-ids/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Search/SearchCommands.cs

Last change on this file was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 4.9 KB
Line 
1// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4// software and associated documentation files (the "Software"), to deal in the Software
5// without restriction, including without limitation the rights to use, copy, modify, merge,
6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7// to whom the Software is furnished to do so, subject to the following conditions:
8//
9// The above copyright notice and this permission notice shall be included in all copies or
10// substantial portions of the Software.
11//
12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17// DEALINGS IN THE SOFTWARE.
18
19using System;
20using System.Collections.Generic;
21using System.Linq;
22using System.Windows;
23using System.Windows.Controls;
24using System.Windows.Input;
25using System.Windows.Media;
26using System.Windows.Threading;
27using ICSharpCode.AvalonEdit.Document;
28using ICSharpCode.AvalonEdit.Editing;
29using ICSharpCode.AvalonEdit.Rendering;
30
31namespace ICSharpCode.AvalonEdit.Search
32{
33  /// <summary>
34  /// Search commands for AvalonEdit.
35  /// </summary>
36  public static class SearchCommands
37  {
38    /// <summary>
39    /// Finds the next occurrence in the file.
40    /// </summary>
41    public static readonly RoutedCommand FindNext = new RoutedCommand(
42      "FindNext", typeof(SearchPanel),
43      new InputGestureCollection { new KeyGesture(Key.F3) }
44    );
45   
46    /// <summary>
47    /// Finds the previous occurrence in the file.
48    /// </summary>
49    public static readonly RoutedCommand FindPrevious = new RoutedCommand(
50      "FindPrevious", typeof(SearchPanel),
51      new InputGestureCollection { new KeyGesture(Key.F3, ModifierKeys.Shift) }
52    );
53   
54    /// <summary>
55    /// Closes the SearchPanel.
56    /// </summary>
57    public static readonly RoutedCommand CloseSearchPanel = new RoutedCommand(
58      "CloseSearchPanel", typeof(SearchPanel),
59      new InputGestureCollection { new KeyGesture(Key.Escape) }
60    );
61  }
62 
63  /// <summary>
64  /// TextAreaInputHandler that registers all search-related commands.
65  /// </summary>
66  public class SearchInputHandler : TextAreaInputHandler
67  {
68    /// <summary>
69    /// Creates a new SearchInputHandler and registers the search-related commands.
70    /// </summary>
71    [Obsolete("Use SearchPanel.Install instead")]
72    public SearchInputHandler(TextArea textArea)
73      : base(textArea)
74    {
75      RegisterCommands(this.CommandBindings);
76      panel = SearchPanel.Install(textArea);
77    }
78   
79    internal SearchInputHandler(TextArea textArea, SearchPanel panel)
80      : base(textArea)
81    {
82      RegisterCommands(this.CommandBindings);
83      this.panel = panel;
84    }
85   
86    void RegisterCommands(ICollection<CommandBinding> commandBindings)
87    {
88      commandBindings.Add(new CommandBinding(ApplicationCommands.Find, ExecuteFind));
89      commandBindings.Add(new CommandBinding(SearchCommands.FindNext, ExecuteFindNext, CanExecuteWithOpenSearchPanel));
90      commandBindings.Add(new CommandBinding(SearchCommands.FindPrevious, ExecuteFindPrevious, CanExecuteWithOpenSearchPanel));
91      commandBindings.Add(new CommandBinding(SearchCommands.CloseSearchPanel, ExecuteCloseSearchPanel, CanExecuteWithOpenSearchPanel));
92    }
93   
94    SearchPanel panel;
95   
96    void ExecuteFind(object sender, ExecutedRoutedEventArgs e)
97    {
98      panel.Open();
99      if (!(TextArea.Selection.IsEmpty || TextArea.Selection.IsMultiline))
100        panel.SearchPattern = TextArea.Selection.GetText();
101      Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Input, (Action)delegate { panel.Reactivate(); });
102    }
103
104    void CanExecuteWithOpenSearchPanel(object sender, CanExecuteRoutedEventArgs e)
105    {
106      if (panel.IsClosed) {
107        e.CanExecute = false;
108        // Continue routing so that the key gesture can be consumed by another component.
109        e.ContinueRouting = true;
110      } else {
111        e.CanExecute = true;
112        e.Handled = true;
113      }
114    }
115   
116    void ExecuteFindNext(object sender, ExecutedRoutedEventArgs e)
117    {
118      if (!panel.IsClosed) {
119        panel.FindNext();
120        e.Handled = true;
121      }
122    }
123   
124    void ExecuteFindPrevious(object sender, ExecutedRoutedEventArgs e)
125    {
126      if (!panel.IsClosed) {
127        panel.FindPrevious();
128        e.Handled = true;
129      }
130    }
131   
132    void ExecuteCloseSearchPanel(object sender, ExecutedRoutedEventArgs e)
133    {
134      if (!panel.IsClosed) {
135        panel.Close();
136        e.Handled = true;
137      }
138    }
139   
140    /// <summary>
141    /// Fired when SearchOptions are modified inside the SearchPanel.
142    /// </summary>
143    public event EventHandler<SearchOptionsChangedEventArgs> SearchOptionsChanged {
144      add { panel.SearchOptionsChanged += value; }
145      remove { panel.SearchOptionsChanged -= value; }
146    }
147  }
148}
Note: See TracBrowser for help on using the repository browser.