1 | // CSharp Editor Example with Code Completion
|
---|
2 | // Copyright (c) 2006, Daniel Grunwald
|
---|
3 | // All rights reserved.
|
---|
4 | //
|
---|
5 | // Redistribution and use in source and binary forms, with or without modification, are
|
---|
6 | // permitted provided that the following conditions are met:
|
---|
7 | //
|
---|
8 | // - Redistributions of source code must retain the above copyright notice, this list
|
---|
9 | // of conditions and the following disclaimer.
|
---|
10 | //
|
---|
11 | // - Redistributions in binary form must reproduce the above copyright notice, this list
|
---|
12 | // of conditions and the following disclaimer in the documentation and/or other materials
|
---|
13 | // provided with the distribution.
|
---|
14 | //
|
---|
15 | // - Neither the name of the ICSharpCode team nor the names of its contributors may be used to
|
---|
16 | // endorse or promote products derived from this software without specific prior written
|
---|
17 | // permission.
|
---|
18 | //
|
---|
19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &AS IS& AND ANY EXPRESS
|
---|
20 | // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
---|
21 | // AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
---|
22 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
23 | // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
---|
25 | // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
---|
26 | // OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 |
|
---|
28 | using System;
|
---|
29 |
|
---|
30 | using ICSharpCode.TextEditor;
|
---|
31 | using ICSharpCode.TextEditor.Gui.CompletionWindow;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.CodeEditor {
|
---|
34 | class CodeCompletionKeyHandler {
|
---|
35 | CodeEditor codeEditor;
|
---|
36 | TextEditorControl editor;
|
---|
37 | CodeCompletionWindow codeCompletionWindow;
|
---|
38 |
|
---|
39 | private CodeCompletionKeyHandler(CodeEditor codeEditor, TextEditorControl editor) {
|
---|
40 | this.codeEditor = codeEditor;
|
---|
41 | this.editor = editor;
|
---|
42 | }
|
---|
43 |
|
---|
44 | public static CodeCompletionKeyHandler Attach(CodeEditor codeEditor, TextEditorControl editor) {
|
---|
45 | CodeCompletionKeyHandler h = new CodeCompletionKeyHandler(codeEditor, editor);
|
---|
46 |
|
---|
47 | editor.ActiveTextAreaControl.TextArea.KeyEventHandler += h.TextAreaKeyEventHandler;
|
---|
48 |
|
---|
49 | // When the editor is disposed, close the code completion window
|
---|
50 | editor.Disposed += h.CloseCodeCompletionWindow;
|
---|
51 |
|
---|
52 | return h;
|
---|
53 | }
|
---|
54 |
|
---|
55 | /// <summary>
|
---|
56 | /// Return true to handle the keypress, return false to let the text area handle the keypress
|
---|
57 | /// </summary>
|
---|
58 | bool TextAreaKeyEventHandler(char key) {
|
---|
59 | if (codeCompletionWindow != null) {
|
---|
60 | // If completion window is open and wants to handle the key, don't let the text area
|
---|
61 | // handle it
|
---|
62 | if (codeCompletionWindow.ProcessKeyEvent(key))
|
---|
63 | return true;
|
---|
64 | }
|
---|
65 | if (key == '.') {
|
---|
66 | ICompletionDataProvider completionDataProvider = new CodeCompletionProvider(codeEditor);
|
---|
67 |
|
---|
68 | codeCompletionWindow = CodeCompletionWindow.ShowCompletionWindow(
|
---|
69 | codeEditor.ParentForm, // The parent window for the completion window
|
---|
70 | editor, // The text editor to show the window for
|
---|
71 | CodeEditor.DummyFileName, // Filename - will be passed back to the provider
|
---|
72 | completionDataProvider, // Provider to get the list of possible completions
|
---|
73 | key // Key pressed - will be passed to the provider
|
---|
74 | );
|
---|
75 | if (codeCompletionWindow != null) {
|
---|
76 | // ShowCompletionWindow can return null when the provider returns an empty list
|
---|
77 | codeCompletionWindow.Closed += new EventHandler(CloseCodeCompletionWindow);
|
---|
78 | }
|
---|
79 | }
|
---|
80 | return false;
|
---|
81 | }
|
---|
82 |
|
---|
83 | void CloseCodeCompletionWindow(object sender, EventArgs e) {
|
---|
84 | if (codeCompletionWindow != null) {
|
---|
85 | codeCompletionWindow.Closed -= new EventHandler(CloseCodeCompletionWindow);
|
---|
86 | codeCompletionWindow.Dispose();
|
---|
87 | codeCompletionWindow = null;
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|