forked from fdorg/flashdevelop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeTip.cs
More file actions
161 lines (132 loc) · 4.91 KB
/
Copy pathCodeTip.cs
File metadata and controls
161 lines (132 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using PluginCore.Helpers;
using PluginCore.Utilities;
using ScintillaNet;
using ScintillaNet.Configuration;
using System;
using System.Drawing;
using System.Windows.Forms;
namespace PluginCore.Controls
{
public class CodeTip
{
ScintillaControl editor;
Panel codeTip;
int columnWidth;
int rowHeight;
public CodeTip(IMainForm mainForm)
{
editor = new ScintillaControl();
editor.Enabled = false;
editor.IsUseTabs = false;
codeTip = new Panel();
codeTip.BorderStyle = BorderStyle.FixedSingle;
codeTip.Controls.Add(editor);
codeTip.Visible = false;
mainForm.Controls.Add(codeTip);
}
public void Show(ScintillaControl sci, int position, string code)
{
ConfigureEditor(sci, code);
ReduceIndentation();
SizeEditor();
PositionEditor(sci, position);
codeTip.Visible = true;
codeTip.BringToFront();
}
public void Hide()
{
codeTip.Visible = false;
}
void ConfigureEditor(ScintillaControl sci, string code)
{
editor.SetMarginWidthN(1, 0);
editor.ConfigurationLanguage = sci.ConfigurationLanguage;
editor.Text = " ";
columnWidth = editor.PointXFromPosition(1) - editor.PointXFromPosition(0);
rowHeight = editor.TextHeight(0);
editor.TabWidth = sci.TabWidth;
editor.Text = code;
editor.SetProperty("lexer.cpp.track.preprocessor", "0");
Language language = GetLanguage(editor.ConfigurationLanguage);
if (language == null)
return;
UseStyle defaultStyle = null;
foreach (var useStyle in language.usestyles)
{
if (useStyle.name == "default")
{
defaultStyle = useStyle;
break;
}
}
if (defaultStyle == null)
return;
codeTip.BackColor = DataConverter.BGRToColor(defaultStyle.BackgroundColor);
}
void ReduceIndentation()
{
editor.StripTrailingSpaces();
int minIndentation = int.MaxValue;
for (var index = 0; index < editor.LineCount; index++)
{
var indentation = editor.GetLineIndentation(index);
if (indentation == 0)
{
continue;
}
minIndentation = Math.Min(minIndentation, indentation);
}
if (minIndentation < int.MaxValue)
{
for (var index = 0; index < editor.LineCount; index++)
{
var indentation = editor.GetLineIndentation(index);
if (indentation == 0)
{
continue;
}
editor.SetLineIndentation(index, indentation - minIndentation);
}
}
}
void SizeEditor()
{
var targetHeight = editor.LineCount * rowHeight;
int targetWidth = 0;
for (var index = 0; index < editor.LineCount; index++)
targetWidth = Math.Max(targetWidth, editor.LineLength(index) * columnWidth);
var editorHeight = Math.Min(ScaleHelper.Scale(500), targetHeight);
var editorWidth = Math.Min(ScaleHelper.Scale(800), targetWidth);
var padding = ScaleHelper.Scale(8);
codeTip.Height = editorHeight + (padding * 2);
codeTip.Width = editorWidth + (padding * 2);
editor.Top = padding;
editor.Left = padding;
editor.Height = editorHeight;
editor.Width = editorWidth;
editor.IsHScrollBar = false; // editorWidth < targetWidth;
editor.IsVScrollBar = false; // editorHeight < targetHeight;
}
void PositionEditor(ScintillaControl sci, int position)
{
Point p = new Point(sci.PointXFromPosition(position), sci.PointYFromPosition(position));
Form mainForm = ((Form)PluginBase.MainForm);
p = mainForm.PointToClient(sci.PointToScreen(p));
if (p.Y > codeTip.Height)
codeTip.Top = p.Y - codeTip.Height;
else
codeTip.Top = p.Y + rowHeight;
if (p.X + codeTip.Width < mainForm.ClientSize.Width)
codeTip.Left = p.X;
else
codeTip.Left = mainForm.ClientSize.Width - codeTip.Width;
}
Language GetLanguage(string name)
{
if (PluginBase.MainForm == null || PluginBase.MainForm.SciConfig == null)
return null;
Language language = PluginBase.MainForm.SciConfig.GetLanguage(name);
return language;
}
}
}