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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ShiftOS.Engine;
using ShiftOS.Frontend.GraphicsSubsystem;
using static ShiftOS.Engine.SkinEngine;
namespace ShiftOS.Frontend.Apps
{
[FileHandler("Shell script", ".trm", "fileicontrm")]
[Launcher("{TITLE_TERMINAL}", false, null, "{AL_UTILITIES}")]
[WinOpen("{WO_TERMINAL}")]
[DefaultTitle("{TITLE_TERMINAL}")]
[DefaultIcon("iconTerminal")]
public class Terminal : GUI.Control, IShiftOSWindow
{
private TerminalControl _terminal = null;
public Terminal()
{
Width = 493;
Height = 295;
}
public void OnLoad()
{
_terminal = new Apps.TerminalControl();
_terminal.Dock = GUI.DockStyle.Fill;
AddControl(_terminal);
_terminal.Layout();
AppearanceManager.ConsoleOut = _terminal;
AppearanceManager.StartConsoleOut();
}
protected override void OnLayout()
{
if (ContainsFocusedControl || IsFocusedControl)
AppearanceManager.ConsoleOut = _terminal;
}
public void OnSkinLoad()
{
}
public bool OnUnload()
{
return true;
}
public void OnUpgrade()
{
}
}
public class TerminalControl : GUI.TextInput, ITerminalWidget
{
public string[] Lines
{
get
{
return Text.Split(new[] { "\n" }, StringSplitOptions.None);
}
}
public void Clear()
{
Text = "";
Index = 0;
Invalidate();
}
public void SelectBottom()
{
Index = Text.Length - 1;
RecalculateLayout();
InvalidateTopLevel();
}
public void Write(string text)
{
Engine.Desktop.InvokeOnWorkerThread(() =>
{
SelectBottom();
Text = Text.Insert(Index, text);
Index += text.Length;
RecalculateLayout();
InvalidateTopLevel();
});
}
public void WriteLine(string text)
{
Write(text + Environment.NewLine);
}
public int GetCurrentLine()
{
int line = 0;
for(int i = 0; i < Text.Length; i++)
{
if(Text[i]=='\n')
{
line++;
continue;
}
if (i == Index)
return line;
}
return 0;
}
float _vertOffset = 0.0f;
protected void RecalculateLayout()
{
if(!string.IsNullOrEmpty(Text))
using (var gfx = Graphics.FromImage(new Bitmap(1, 1)))
{
var cursorpos = GetPointAtIndex(gfx);
var caretSize = gfx.SmartMeasureString(Text.ToString(), LoadedSkin.TerminalFont, Width - 4);
float initial = ((caretSize.Height) - cursorpos.Y) - _vertOffset;
if (initial < 0)
{
float difference = initial - Height;
_vertOffset += initial + difference;
}
if (initial > Height)
{
float difference = initial - Height;
_vertOffset -= initial - difference;
}
}
}
/// <summary>
/// Gets the X and Y coordinates (in pixels) of the caret.
/// </summary>
/// <param name="gfx">A <see cref="System.Drawing.Graphics"/> object used for font measurements</param>
/// <returns>An absolute fucking mess. Seriously, can someone fix this method so it uhh WORKS PROPERLY?</returns>
public PointF GetPointAtIndex(Graphics gfx)
{
float vertMeasure = 2.0f;
float horizMeasure = 2.0f;
int lineindexes = 0;
for (int l = 0; l <= GetCurrentLine(); l++)
{
var measure = gfx.SmartMeasureString(Lines[l], LoadedSkin.TerminalFont, Width - 4);
vertMeasure += measure.Width;
if(l == GetCurrentLine())
{
string _linetext = Text.Substring(lineindexes, Index - lineindexes);
var lMeasure = gfx.SmartMeasureString(_linetext, LoadedSkin.TerminalFont);
horizMeasure = lMeasure.Width;
if (horizMeasure > Width - 4)
horizMeasure -= (Width-4);
}
else
{
lineindexes += Lines[l].Length;
}
}
return new PointF(horizMeasure, vertMeasure);
}
protected override void OnKeyEvent(KeyEvent e)
{
if (e.Key == Microsoft.Xna.Framework.Input.Keys.Enter)
{
Text = Text.Insert(Index, "\r\n");
Index++;
}
base.OnKeyEvent(e);
RecalculateLayout();
InvalidateTopLevel();
}
protected override void OnPaint(Graphics gfx)
{
RecalculateLayout();
gfx.Clear(LoadedSkin.TerminalBackColorCC.ToColor());
if (!string.IsNullOrEmpty(Text))
{
//Draw the caret.
var caretPos = GetPointAtIndex(gfx);
var caretSize = gfx.SmartMeasureString(Text[Index - 1].ToString(), LoadedSkin.TerminalFont);
if (IsFocusedControl)
{
gfx.FillRectangle(new SolidBrush(LoadedSkin.TerminalForeColorCC.ToColor()), new RectangleF(new PointF(caretPos.X, caretPos.Y - _vertOffset), new SizeF(2, caretSize.Height)));
}//Draw the text
var textMeasure = gfx.MeasureString(Text, LoadedSkin.TerminalFont, Width - 4);
gfx.DrawString(Text, LoadedSkin.TerminalFont, new SolidBrush(LoadedSkin.TerminalForeColorCC.ToColor()), 2, 2 - _vertOffset);
}
}
}
public static class ConsoleColorExtensions
{
public static Color ToColor(this ConsoleColor cc)
{
switch (cc)
{
case ConsoleColor.Black:
return Color.Black;
case ConsoleColor.Blue:
return Color.Blue;
case ConsoleColor.Cyan:
return Color.Cyan;
case ConsoleColor.DarkBlue:
return Color.DarkBlue;
case ConsoleColor.DarkCyan:
return Color.DarkCyan;
case ConsoleColor.DarkGray:
return Color.DarkGray;
case ConsoleColor.DarkGreen:
return Color.DarkGreen;
case ConsoleColor.DarkMagenta:
return Color.DarkMagenta;
case ConsoleColor.DarkRed:
return Color.DarkRed;
case ConsoleColor.DarkYellow:
return Color.Orange;
case ConsoleColor.Gray:
return Color.Gray;
case ConsoleColor.Green:
return Color.Green;
case ConsoleColor.Magenta:
return Color.Magenta;
case ConsoleColor.Red:
return Color.Red;
case ConsoleColor.White:
return Color.White;
case ConsoleColor.Yellow:
return Color.Yellow;
}
return Color.Empty;
}
}
public static class GraphicsExtensions
{
public static SizeF SmartMeasureString(this Graphics gfx, string s, Font font, int width)
{
if (string.IsNullOrEmpty(s))
s = " ";
var textformat = new StringFormat(StringFormat.GenericTypographic);
textformat.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
textformat.Trimming = StringTrimming.None;
return gfx.MeasureString(s, font, width, textformat);
}
public static SizeF SmartMeasureString(this Graphics gfx, string s, Font font)
{
if (string.IsNullOrEmpty(s))
s = " ";
var textformat = new StringFormat(StringFormat.GenericTypographic);
textformat.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
textformat.Trimming = StringTrimming.None;
return gfx.MeasureString(s, font, int.MaxValue, textformat);
}
}
}
|