forked from fdorg/flashdevelop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUITools.cs
More file actions
437 lines (387 loc) · 15.6 KB
/
UITools.cs
File metadata and controls
437 lines (387 loc) · 15.6 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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
using System;
using System.Drawing;
using System.Windows.Forms;
using PluginCore.Managers;
using ScintillaNet;
using WeifenLuo.WinFormsUI.Docking;
namespace PluginCore.Controls
{
public class UITools : IMessageFilter, IEventHandler
{
public delegate void CharAddedHandler(ScintillaControl sender, int value);
public delegate void TextChangedHandler(ScintillaControl sender, int position, int length, int linesAdded);
public delegate void MouseHoverHandler(ScintillaControl sender, int position);
public delegate void LineEventHandler(ScintillaControl sender, int line);
#region Singleton Instance
static private UITools manager;
static public UITools Manager
{
get {
if (manager == null)
{
manager = new UITools();
}
return manager;
}
}
static public RichToolTip Tip
{
get { return manager.simpleTip; }
}
static public MethodCallTip CallTip
{
get { return manager.callTip; }
}
static public void Init()
{
if (manager == null)
{
manager = new UITools();
}
}
#endregion
#region Initialization
public event MouseHoverHandler OnMouseHover;
public event MouseHoverHandler OnMouseHoverEnd;
public event CharAddedHandler OnCharAdded;
public event TextChangedHandler OnTextChanged;
public event LineEventHandler OnMarkerChanged;
public bool DisableEvents;
/// <summary>
/// Option: show detailed information in tips.
/// </summary>
/// <remarks>
/// Default value is defined in the main settings.
/// State is switched using F1 key when a tip is visible.
/// </remarks>
public bool ShowDetails
{
get { return showDetails; }
set { showDetails = value; }
}
private EventType eventMask =
EventType.Keys |
EventType.FileSave |
EventType.Command |
EventType.FileSwitch;
private RichToolTip simpleTip;
private MethodCallTip callTip;
private bool ignoreKeys;
private bool showDetails;
private UITools()
{
showDetails = PluginBase.Settings.ShowDetails;
//
// CONTROLS
//
try
{
CompletionList.CreateControl(PluginBase.MainForm);
simpleTip = new RichToolTip(PluginBase.MainForm);
callTip = new MethodCallTip(PluginBase.MainForm);
}
catch(Exception ex)
{
ErrorManager.ShowError(/*"Error while creating editor controls.",*/ ex);
}
//
// Events
//
PluginBase.MainForm.IgnoredKeys.Add(Keys.Space | Keys.Control); // complete member
PluginBase.MainForm.IgnoredKeys.Add(Keys.Space | Keys.Control | Keys.Shift); // complete method
PluginBase.MainForm.DockPanel.ActivePaneChanged += new EventHandler(DockPanel_ActivePaneChanged);
EventManager.AddEventHandler(this, eventMask);
}
#endregion
private WeakReference lockedSciControl;
private Point lastMousePos = new Point(0,0);
#region SciControls & MainForm Events
private void DockPanel_ActivePaneChanged(object sender, EventArgs e)
{
if (PluginBase.MainForm.DockPanel.ActivePane != null
&& PluginBase.MainForm.DockPanel.ActivePane != PluginBase.MainForm.DockPanel.ActiveDocumentPane)
{
OnUIRefresh(null);
}
}
public void HandleEvent(object sender, NotifyEvent e, HandlingPriority priority)
{
switch (e.Type)
{
case EventType.Keys:
e.Handled = HandleKeys(((KeyEvent)e).Value);
return;
case EventType.FileSave:
MessageBar.HideWarning();
return;
case EventType.Command:
string cmd = (e as DataEvent).Action;
if (cmd.StartsWith("ProjectManager") || cmd.IndexOf("Changed") > 0 || cmd.IndexOf("Context") > 0)
return; // ignore notifications
break;
}
// most of the time, an event should hide the list
OnUIRefresh(null);
}
/// <summary>
/// Reserved to MainForm
/// </summary>
public void ListenTo(ScintillaControl sci)
{
// hook scintilla events
sci.MouseDwellTime = PluginBase.MainForm.Settings.HoverDelay;
sci.DwellStart += new DwellStartHandler(HandleDwellStart);
sci.DwellEnd += new DwellEndHandler(HandleDwellEnd);
sci.CharAdded += new ScintillaNet.CharAddedHandler(OnChar);
sci.UpdateUI += new UpdateUIHandler(OnUIRefresh);
sci.TextInserted += new TextInsertedHandler(OnTextInserted);
sci.TextDeleted += new TextDeletedHandler(OnTextDeleted);
}
/// <summary>
/// Notify all listeners that document markers were changed
/// </summary>
public void MarkerChanged(ScintillaControl sender, int line)
{
if (OnMarkerChanged != null) OnMarkerChanged(sender, line);
}
private void HandleDwellStart(ScintillaControl sci, int position)
{
if (OnMouseHover == null || sci == null || DisableEvents) return;
try
{
// check mouse over the editor
if ((position < 0) || simpleTip.Visible || CompletionList.HasMouseIn) return;
Point mousePos = (PluginBase.MainForm as Form).PointToClient(Cursor.Position);
if (mousePos.X == lastMousePos.X && mousePos.Y == lastMousePos.Y)
return;
lastMousePos = mousePos;
Rectangle bounds = GetWindowBounds(sci);
if (!bounds.Contains(mousePos)) return;
// check no panel is over the editor
DockPanel panel = PluginBase.MainForm.DockPanel;
DockContentCollection panels = panel.Contents;
foreach (DockContent content in panels)
{
if (content.IsHidden || content.Bounds.Height == 0 || content.Bounds.Width == 0
|| content.GetType().ToString() == "FlashDevelop.Docking.TabbedDocument")
continue;
bounds = GetWindowBounds(content);
if (bounds.Contains(mousePos))
return;
}
if (OnMouseHover != null) OnMouseHover(sci, position);
}
catch (Exception ex)
{
ErrorManager.ShowError(ex);
// disable this feature completely
OnMouseHover = null;
}
}
private Rectangle GetWindowBounds(Control ctrl)
{
while (ctrl.Parent != null && !(ctrl is DockWindow)) ctrl = ctrl.Parent;
if (ctrl != null) return ctrl.Bounds;
else return new Rectangle();
}
private Point GetMousePosIn(Control ctrl)
{
Point ctrlPos = ctrl.PointToScreen(new Point());
Point pos = Cursor.Position;
return new Point(pos.X - ctrlPos.X, pos.Y - ctrlPos.Y);
}
private void HandleDwellEnd(ScintillaControl sci, int position)
{
simpleTip.Hide();
if (OnMouseHoverEnd != null) OnMouseHoverEnd(sci, position);
}
#endregion
#region Scintilla Hook
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == Win32.WM_MOUSEWHEEL) // capture all MouseWheel events
{
if (!callTip.CallTipActive || !callTip.Focused)
{
if (Win32.ShouldUseWin32())
{
Win32.SendMessage((IntPtr)CompletionList.GetHandle(), m.Msg, (Int32)m.WParam, (Int32)m.LParam);
return true;
}
else return false;
}
else return false;
}
else if (m.Msg == Win32.WM_KEYDOWN)
{
if ((int)m.WParam == 17) // Ctrl
{
if (CompletionList.Active) CompletionList.FadeOut();
if (callTip.CallTipActive && !callTip.Focused) callTip.FadeOut();
}
}
else if (m.Msg == Win32.WM_KEYUP)
{
if ((int)m.WParam == 17 || (int)m.WParam == 18) // Ctrl / AltGr
{
if (CompletionList.Active) CompletionList.FadeIn();
if (callTip.CallTipActive) callTip.FadeIn();
}
}
return false;
}
public void LockControl(ScintillaControl sci)
{
if (lockedSciControl != null && lockedSciControl.IsAlive && lockedSciControl.Target == sci)
return;
UnlockControl();
sci.IgnoreAllKeys = true;
lockedSciControl = new WeakReference(sci);
Application.AddMessageFilter(this);
}
public void UnlockControl()
{
if (CompletionList.Active || CallTip.CallTipActive)
return;
Application.RemoveMessageFilter(this);
if (lockedSciControl != null && lockedSciControl.IsAlive)
{
ScintillaControl sci = (ScintillaControl)lockedSciControl.Target;
sci.IgnoreAllKeys = false;
}
lockedSciControl = null;
}
private void OnUIRefresh(ScintillaControl sci)
{
if (sci != null && sci.IsFocus)
{
int position = sci.CurrentPos;
if (CompletionList.Active && CompletionList.CheckPosition(position)) return;
if (callTip.CallTipActive && callTip.CheckPosition(position)) return;
}
callTip.Hide();
CompletionList.Hide();
simpleTip.Hide();
}
private void OnTextInserted(ScintillaControl sci, int position, int length, int linesAdded)
{
if (OnTextChanged != null && !DisableEvents)
OnTextChanged(sci, position, length, linesAdded);
}
private void OnTextDeleted(ScintillaControl sci, int position, int length, int linesAdded)
{
if (OnTextChanged != null && !DisableEvents)
OnTextChanged(sci, position, -length, linesAdded);
}
private void OnChar(ScintillaControl sci, int value)
{
if (sci == null || DisableEvents) return;
if (!CompletionList.Active && !callTip.CallTipActive)
{
SendChar(sci, value);
return;
}
if (lockedSciControl != null && lockedSciControl.IsAlive) sci = (ScintillaControl)lockedSciControl.Target;
else
{
callTip.Hide();
CompletionList.Hide();
SendChar(sci, value);
return;
}
if (callTip.CallTipActive) callTip.OnChar(sci, value);
if (CompletionList.Active) CompletionList.OnChar(sci, value);
else SendChar(sci, value);
return;
}
public void SendChar(ScintillaControl sci, int value)
{
if (OnCharAdded != null) OnCharAdded(sci, value);
}
private bool HandleKeys(Keys key)
{
// UITools is currently broadcasting a shortcut, ignore!
if (ignoreKeys || DisableEvents) return false;
// list/tip shortcut dispatching
if ((key == (Keys.Control | Keys.Space)) || (key == (Keys.Shift | Keys.Control | Keys.Space)))
{
/*if (CompletionList.Active || callTip.CallTipActive)
{
UnlockControl();
CompletionList.Hide();
callTip.Hide();
}*/
// offer to handle the shortcut
ignoreKeys = true;
KeyEvent ke = new KeyEvent(EventType.Keys, key);
EventManager.DispatchEvent(this, ke);
ignoreKeys = false;
// if not handled - show snippets
if (!ke.Handled && PluginBase.MainForm.CurrentDocument.IsEditable
&& !PluginBase.MainForm.CurrentDocument.SciControl.IsSelectionRectangle)
{
PluginBase.MainForm.CallCommand("InsertSnippet", "null");
}
return true;
}
// toggle "long-description" for the hover tooltip
if (key == Keys.F1 && Tip.Visible && !CompletionList.Active)
{
showDetails = !showDetails;
simpleTip.UpdateTip(PluginBase.MainForm.CurrentDocument.SciControl);
return true;
}
// are we currently displaying something?
if (!CompletionList.Active && !callTip.CallTipActive) return false;
// hide if pressing Esc or Ctrl+Key combination
if (lockedSciControl == null || !lockedSciControl.IsAlive || key == Keys.Escape
|| ((Control.ModifierKeys & Keys.Control) != 0 && Control.ModifierKeys != (Keys.Control|Keys.Alt)) )
{
if (key == (Keys.Control | Keys.C) || key == (Keys.Control | Keys.A))
return false; // let text copy in tip
UnlockControl();
CompletionList.Hide((char)27);
callTip.Hide();
return false;
}
ScintillaControl sci = (ScintillaControl)lockedSciControl.Target;
// chars
string ks = key.ToString();
if (ks.Length == 1 || (ks.EndsWith(", Shift") && ks.IndexOf(',') == 1) || ks.StartsWith("NumPad"))
{
return false;
}
// toggle "long-description"
if (key == Keys.F1)
{
showDetails = !showDetails;
if (callTip.CallTipActive) callTip.UpdateTip(sci);
else CompletionList.UpdateTip(null, null);
return true;
}
// switches
else if ((key & Keys.ShiftKey) == Keys.ShiftKey || (key & Keys.ControlKey) == Keys.ControlKey || (key & Keys.Menu) == Keys.Menu)
{
return false;
}
// handle special keys
bool handled = false;
if (callTip.CallTipActive) handled |= callTip.HandleKeys(sci, key);
if (CompletionList.Active) handled |= CompletionList.HandleKeys(sci, key);
return handled;
}
/// <summary>
/// Compute current editor line height
/// </summary>
public int LineHeight(ScintillaControl sci)
{
if (sci == null) return 0;
// evaluate the font size
Font tempFont = new Font(sci.Font.Name, sci.Font.Size+sci.ZoomLevel);
Graphics g = ((Control)sci).CreateGraphics();
SizeF textSize = g.MeasureString("S", tempFont);
return (int)Math.Ceiling(textSize.Height);
}
#endregion
}
}