1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using Netron.Diagramming.Core;
|
---|
6 | using System.Windows.Forms;
|
---|
7 | using System.Drawing;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.Netron {
|
---|
10 | public static class TextEditor {
|
---|
11 |
|
---|
12 | private static TextEditorControl editor = null;
|
---|
13 | private static ITextProvider myTextProvider;
|
---|
14 | private static IDiagramControl diagramControl;
|
---|
15 |
|
---|
16 | private static EventHandler<MouseEventArgs> onescape =
|
---|
17 | new EventHandler<MouseEventArgs>(Controller_OnMouseDown);
|
---|
18 |
|
---|
19 | private static ITextProvider TextProvider {
|
---|
20 | get { return myTextProvider; }
|
---|
21 | }
|
---|
22 |
|
---|
23 | private static TextEditorControl Editor {
|
---|
24 | get {
|
---|
25 | if (editor == null)
|
---|
26 | editor = new TextEditorControl();
|
---|
27 | return editor;
|
---|
28 | }
|
---|
29 | }
|
---|
30 |
|
---|
31 | public static void Init(DiagramControlBase parent) {
|
---|
32 | diagramControl = parent;
|
---|
33 | parent.Controls.Add(Editor);
|
---|
34 | Editor.Visible = false;
|
---|
35 | Editor.BackColor = Color.White;
|
---|
36 | }
|
---|
37 |
|
---|
38 | public static TextEditorControl GetEditor(ITextProvider textProvider) {
|
---|
39 | if (textProvider == null) {
|
---|
40 | throw new InconsistencyException(
|
---|
41 | "Cannot assign an editor to a 'null' text provider.");
|
---|
42 | }
|
---|
43 |
|
---|
44 | // Adjust the editor's location and size by the current scroll
|
---|
45 | // position and zoom factor.
|
---|
46 | Point location = Point.Round(diagramControl.View.WorldToView(
|
---|
47 | textProvider.TextArea.Location));
|
---|
48 |
|
---|
49 | Size size = Size.Round(diagramControl.View.WorldToView(
|
---|
50 | textProvider.TextArea.Size));
|
---|
51 |
|
---|
52 | myTextProvider = textProvider;
|
---|
53 | Editor.Location = location;
|
---|
54 | Editor.Width = size.Width;
|
---|
55 | Editor.Height = size.Height;
|
---|
56 | Editor.Font = textProvider.TextStyle.Font;
|
---|
57 | Editor.Visible = false;
|
---|
58 | return Editor;
|
---|
59 | }
|
---|
60 |
|
---|
61 | public static void Show() {
|
---|
62 | if (myTextProvider == null)
|
---|
63 | return;
|
---|
64 | Selection.Clear();
|
---|
65 | diagramControl.View.ResetTracker();
|
---|
66 | diagramControl.Controller.SuspendAllTools();
|
---|
67 | diagramControl.Controller.Enabled = false;
|
---|
68 | diagramControl.Controller.OnMouseDown += onescape;
|
---|
69 | Editor.Visible = true;
|
---|
70 | Editor.Text = myTextProvider.Text;
|
---|
71 |
|
---|
72 | if (myTextProvider.TextStyle.HorizontalAlignment == StringAlignment.Center)
|
---|
73 | Editor.TextAlign = HorizontalAlignment.Center;
|
---|
74 | else if (myTextProvider.TextStyle.HorizontalAlignment == StringAlignment.Far)
|
---|
75 | Editor.TextAlign = HorizontalAlignment.Right;
|
---|
76 | else
|
---|
77 | Editor.TextAlign = HorizontalAlignment.Left;
|
---|
78 |
|
---|
79 | Editor.SelectionLength = Editor.Text.Length;
|
---|
80 | Editor.ScrollToCaret();
|
---|
81 | Editor.Focus();
|
---|
82 | }
|
---|
83 |
|
---|
84 | static void Controller_OnMouseDown(object sender, MouseEventArgs e) {
|
---|
85 | Hide();
|
---|
86 | diagramControl.Controller.OnMouseDown -= onescape;
|
---|
87 | }
|
---|
88 |
|
---|
89 | public static void Hide() {
|
---|
90 | if (myTextProvider == null)
|
---|
91 | return;
|
---|
92 | diagramControl.Controller.Enabled = true;
|
---|
93 | diagramControl.Focus();
|
---|
94 | Editor.Visible = false;
|
---|
95 | myTextProvider.Text = Editor.Text;
|
---|
96 | diagramControl.Controller.UnsuspendAllTools();
|
---|
97 | myTextProvider = null;
|
---|
98 | }
|
---|
99 |
|
---|
100 | public class TextEditorControl : TextBox {
|
---|
101 | public TextEditorControl()
|
---|
102 | : base() {
|
---|
103 | this.BorderStyle = BorderStyle.FixedSingle;
|
---|
104 | this.Multiline = true;
|
---|
105 | this.ScrollBars = ScrollBars.None;
|
---|
106 | this.WordWrap = true;
|
---|
107 | this.BackColor = Color.White;
|
---|
108 | }
|
---|
109 |
|
---|
110 | }
|
---|
111 |
|
---|
112 | }
|
---|
113 | }
|
---|