Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveHiveEngine/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/HeuristicLab.Netron-3.0.2672.12446/TextEditor.cs @ 11301

Last change on this file since 11301 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 4.5 KB
Line 
1#region License Information
2//This end-user license agreement applies to the following software;
3
4//The Netron Diagramming Library
5//Cobalt.IDE
6//Xeon webserver
7//Neon UI Library
8
9//Copyright (C) 2007, Francois M.Vanderseypen, The Netron Project & The Orbifold
10
11//This program is free software; you can redistribute it and/or
12//modify it under the terms of the GNU General Public License
13//as published by the Free Software Foundation; either version 2
14//of the License, or (at your option) any later version.
15
16//This program is distributed in the hope that it will be useful,
17//but WITHOUT ANY WARRANTY; without even the implied warranty of
18//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19//GNU General Public License for more details.
20
21//You should have received a copy of the GNU General Public License
22//along with this program; if not, write to the Free Software
23//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
24
25
26//http://www.fsf.org/licensing/licenses/gpl.html
27
28//http://www.fsf.org/licensing/licenses/gpl-faq.html
29#endregion
30
31using System;
32using System.Drawing;
33using System.Windows.Forms;
34using Netron.Diagramming.Core;
35
36namespace HeuristicLab.Netron {
37  public static class TextEditor {
38
39    private static TextEditorControl editor = null;
40    private static ITextProvider myTextProvider;
41    private static IDiagramControl diagramControl;
42
43    private static EventHandler<MouseEventArgs> onescape =
44           new EventHandler<MouseEventArgs>(Controller_OnMouseDown);
45
46    private static ITextProvider TextProvider {
47      get { return myTextProvider; }
48    }
49
50    private static TextEditorControl Editor {
51      get {
52        if (editor == null)
53          editor = new TextEditorControl();
54        return editor;
55      }
56    }
57
58    public static void Init(DiagramControlBase parent) {
59      diagramControl = parent;
60      parent.Controls.Add(Editor);
61      Editor.Visible = false;
62      Editor.BackColor = Color.White;
63    }
64
65    public static TextEditorControl GetEditor(ITextProvider textProvider) {
66      if (textProvider == null) {
67        throw new InconsistencyException(
68            "Cannot assign an editor to a 'null' text provider.");
69      }
70
71      // Adjust the editor's location and size by the current scroll
72      // position and zoom factor.
73      Point location = Point.Round(diagramControl.View.WorldToView(
74          textProvider.TextArea.Location));
75
76      Size size = Size.Round(diagramControl.View.WorldToView(
77          textProvider.TextArea.Size));
78
79      myTextProvider = textProvider;
80      Editor.Location = location;
81      Editor.Width = size.Width;
82      Editor.Height = size.Height;
83      Editor.Font = textProvider.TextStyle.Font;
84      Editor.Visible = false;
85      return Editor;
86    }
87
88    public static void Show() {
89      if (myTextProvider == null)
90        return;
91      diagramControl.Controller.Model.Selection.Clear();
92      diagramControl.View.ResetTracker();
93      diagramControl.Controller.SuspendAllTools();
94      diagramControl.Controller.Enabled = false;
95      diagramControl.Controller.OnMouseDown += onescape;
96      Editor.Visible = true;
97      Editor.Text = myTextProvider.Text;
98
99      if (myTextProvider.TextStyle.HorizontalAlignment == StringAlignment.Center)
100        Editor.TextAlign = HorizontalAlignment.Center;
101      else if (myTextProvider.TextStyle.HorizontalAlignment == StringAlignment.Far)
102        Editor.TextAlign = HorizontalAlignment.Right;
103      else
104        Editor.TextAlign = HorizontalAlignment.Left;
105
106      Editor.SelectionLength = Editor.Text.Length;
107      Editor.ScrollToCaret();
108      Editor.Focus();
109    }
110
111    static void Controller_OnMouseDown(object sender, MouseEventArgs e) {
112      Hide();
113      diagramControl.Controller.OnMouseDown -= onescape;
114    }
115
116    public static void Hide() {
117      if (myTextProvider == null)
118        return;
119      diagramControl.Controller.Enabled = true;
120      diagramControl.Focus();
121      Editor.Visible = false;
122      myTextProvider.Text = Editor.Text;
123      diagramControl.Controller.UnsuspendAllTools();
124      myTextProvider = null;
125    }
126
127    public class TextEditorControl : TextBox {
128      public TextEditorControl()
129        : base() {
130        this.BorderStyle = BorderStyle.FixedSingle;
131        this.Multiline = true;
132        this.ScrollBars = ScrollBars.None;
133        this.WordWrap = true;
134        this.BackColor = Color.White;
135      }
136
137    }
138
139  }
140}
Note: See TracBrowser for help on using the repository browser.