[2659] | 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 | using System.Collections.Generic;
|
---|
| 30 | using System.Drawing;
|
---|
| 31 | using System.Windows.Forms;
|
---|
| 32 | using System.IO;
|
---|
| 33 | using System.Threading;
|
---|
| 34 |
|
---|
| 35 | using ICSharpCode.TextEditor;
|
---|
| 36 | using ICSharpCode.TextEditor.Gui;
|
---|
| 37 | using ICSharpCode.TextEditor.Gui.CompletionWindow;
|
---|
| 38 |
|
---|
| 39 | namespace HeuristicLab.CodeEditor {
|
---|
| 40 | class CodeCompletionKeyHandler {
|
---|
| 41 | CodeEditor codeEditor;
|
---|
| 42 | TextEditorControl editor;
|
---|
| 43 | CodeCompletionWindow codeCompletionWindow;
|
---|
| 44 |
|
---|
| 45 | private CodeCompletionKeyHandler(CodeEditor codeEditor, TextEditorControl editor) {
|
---|
| 46 | this.codeEditor = codeEditor;
|
---|
| 47 | this.editor = editor;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | public static CodeCompletionKeyHandler Attach(CodeEditor codeEditor, TextEditorControl editor) {
|
---|
| 51 | CodeCompletionKeyHandler h = new CodeCompletionKeyHandler(codeEditor, editor);
|
---|
| 52 |
|
---|
| 53 | editor.ActiveTextAreaControl.TextArea.KeyEventHandler += h.TextAreaKeyEventHandler;
|
---|
| 54 |
|
---|
| 55 | // When the editor is disposed, close the code completion window
|
---|
| 56 | editor.Disposed += h.CloseCodeCompletionWindow;
|
---|
| 57 |
|
---|
| 58 | return h;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | /// <summary>
|
---|
| 62 | /// Return true to handle the keypress, return false to let the text area handle the keypress
|
---|
| 63 | /// </summary>
|
---|
| 64 | bool TextAreaKeyEventHandler(char key) {
|
---|
| 65 | if (codeCompletionWindow != null) {
|
---|
| 66 | // If completion window is open and wants to handle the key, don't let the text area
|
---|
| 67 | // handle it
|
---|
| 68 | if (codeCompletionWindow.ProcessKeyEvent(key))
|
---|
| 69 | return true;
|
---|
| 70 | }
|
---|
| 71 | if (key == '.') {
|
---|
| 72 | ICompletionDataProvider completionDataProvider = new CodeCompletionProvider(codeEditor);
|
---|
| 73 |
|
---|
| 74 | codeCompletionWindow = CodeCompletionWindow.ShowCompletionWindow(
|
---|
| 75 | codeEditor.ParentForm, // The parent window for the completion window
|
---|
| 76 | editor, // The text editor to show the window for
|
---|
| 77 | CodeEditor.DummyFileName, // Filename - will be passed back to the provider
|
---|
| 78 | completionDataProvider, // Provider to get the list of possible completions
|
---|
| 79 | key // Key pressed - will be passed to the provider
|
---|
| 80 | );
|
---|
| 81 | if (codeCompletionWindow != null) {
|
---|
| 82 | // ShowCompletionWindow can return null when the provider returns an empty list
|
---|
| 83 | codeCompletionWindow.Closed += new EventHandler(CloseCodeCompletionWindow);
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 | return false;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | void CloseCodeCompletionWindow(object sender, EventArgs e) {
|
---|
| 90 | if (codeCompletionWindow != null) {
|
---|
| 91 | codeCompletionWindow.Closed -= new EventHandler(CloseCodeCompletionWindow);
|
---|
| 92 | codeCompletionWindow.Dispose();
|
---|
| 93 | codeCompletionWindow = null;
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | }
|
---|