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
using CppEngine.Outlining;
using CppProjectEngine.CodeExplorer;
using Sysprogs.Core.Tools;
using Sysprogs.GUI.Portable.Controls.RuleBased;
using SysprogsDevTools.ELFParser;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
namespace CppProjectEngine.GUI.WPF.Bars
{
public class AdvancedScopePresenter : Control
{
public OutlineScope Scope
{
get { return (OutlineScope)GetValue(ScopeProperty); }
set { SetValue(ScopeProperty, value); }
}
public static readonly DependencyProperty ScopeProperty = DependencyProperty.Register("Scope", typeof(OutlineScope), typeof(AdvancedScopePresenter), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender));
public Brush LinkForeground
{
get { return (Brush)GetValue(LinkForegroundProperty); }
set { SetValue(LinkForegroundProperty, value); }
}
public static readonly DependencyProperty LinkForegroundProperty = DependencyProperty.Register("LinkForeground", typeof(Brush), typeof(AdvancedScopePresenter), new PropertyMetadata(null));
public double SecondaryItemOpacity { get; set; } = 0.5;
public ScopeItemHitTestMode HitTestMode
{
get { return (ScopeItemHitTestMode)GetValue(HitTestModeProperty); }
set { SetValue(HitTestModeProperty, value); }
}
public static readonly DependencyProperty HitTestModeProperty = DependencyProperty.Register("HitTestMode", typeof(ScopeItemHitTestMode), typeof(AdvancedScopePresenter), new PropertyMetadata(ScopeItemHitTestMode.Always));
public double SoftWidthLimitFraction
{
get { return (double)GetValue(SoftWidthLimitFractionProperty); }
set { SetValue(SoftWidthLimitFractionProperty, value); }
}
public static readonly DependencyProperty SoftWidthLimitFractionProperty = DependencyProperty.Register("SoftWidthLimitFraction", typeof(double), typeof(AdvancedScopePresenter), new FrameworkPropertyMetadata(double.NaN, FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsArrange));
public object SoftWidthReference
{
get { return (object)GetValue(SoftWidthReferenceProperty); }
set { SetValue(SoftWidthReferenceProperty, value); }
}
public static readonly DependencyProperty SoftWidthReferenceProperty =
DependencyProperty.Register("SoftWidthReference", typeof(object), typeof(AdvancedScopePresenter), new PropertyMetadata(null));
public Geometry SeparatorGlyph { get; set; }
public Geometry ArrowGlyph { get; set; }
abstract class PlacedComponent
{
public int X;
public bool CanHighlight => Item?.ClickHandler != null;
public AugmentedOutline.ScopeItem Item;
public abstract int Width { get; }
public bool IsGrayedOut;
public int Right => X + Width;
protected PlacedComponent()
{
}
public abstract void Draw(AdvancedScopePresenter presenter, DrawingContext ctx, PlacedComponent highlight);
public class Text : PlacedComponent
{
FormattedText _Text;
bool _IsHighlighted;
public Text(FormattedText text, AugmentedOutline.ScopeItem item = null)
{
_Text = text;
Item = item;
}
public override int Width => (int)_Text.Width;
public override void Draw(AdvancedScopePresenter presenter, DrawingContext ctx, PlacedComponent highlight)
{
bool isHighlighted = Item != null && Item == (highlight as Text)?.Item;
if (isHighlighted != _IsHighlighted && CanHighlight)
{
_IsHighlighted = isHighlighted;
if (isHighlighted)
_Text.SetTextDecorations(TextDecorations.Underline);
else
_Text.SetTextDecorations(null);
if (isHighlighted && presenter.LinkForeground != null)
_Text.SetForegroundBrush(presenter.LinkForeground);
else
_Text.SetForegroundBrush(presenter.Foreground);
}
ctx.DrawText(_Text, new Point(X, 0));
}
}
public class Glyph : PlacedComponent
{
private Pen _Pen;
Geometry _Glyph;
const int XPadding = 3;
public Glyph(Geometry glyph, Pen pen)
{
_Pen = pen;
_Glyph = glyph;
}
public override int Width => (int)_Glyph.Bounds.Width + XPadding * 2;
public override void Draw(AdvancedScopePresenter presenter, DrawingContext ctx, PlacedComponent highlight)
{
ctx.PushTransform(new TranslateTransform(X + XPadding, 1 + Math.Round((presenter.ActualHeight - _Glyph.Bounds.Height) / 2)));
ctx.DrawGeometry(null, _Pen, _Glyph);
ctx.Pop();
}
}
public bool ContainsX(int x) => x >= X && x <= (X + Width);
}
List<PlacedComponent> _Components = new List<PlacedComponent>();
private OutlineScope _Scope;
private PlacedComponent _HighlightedComponent;
protected override void OnRender(DrawingContext ctx)
{
base.OnRender(ctx);
try
{
ctx.PushClip(new RectangleGeometry(new Rect(0, 0, ActualWidth, ActualHeight)));
foreach (var c in _Components)
{
ctx.DrawRectangle(Background, null, new Rect(c.X, 0, c.Width, ActualHeight));
if (c.IsGrayedOut)
ctx.PushOpacity(SecondaryItemOpacity);
c.Draw(this, ctx, _HighlightedComponent);
if (c.IsGrayedOut)
ctx.Pop();
}
}
finally
{
ctx.Pop();
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
int x = (int)e.GetPosition(this).X;
var matchingComponent = _Components.FirstOrDefault(c => c.CanHighlight && c.ContainsX(x));
if (_HighlightedComponent != matchingComponent)
{
_HighlightedComponent = matchingComponent;
Cursor = (matchingComponent != null) ? Cursors.Hand : null;
InvalidateVisual();
}
}
protected override void OnMouseLeave(MouseEventArgs e)
{
base.OnMouseLeave(e);
if (_HighlightedComponent != null)
{
_HighlightedComponent = null;
Cursor = null;
InvalidateVisual();
}
}
public event EventHandler<ScopeItemClickedEventArgs> ScopeItemClicked;
protected override void OnMouseDown(MouseButtonEventArgs e)
{
base.OnMouseDown(e);
if (_HighlightedComponent?.Item is AugmentedOutline.ScopeItem item)
ScopeItemClicked?.Invoke(this, new ScopeItemClickedEventArgs(item));
}
bool ShouldSkipHitTest()
{
switch (HitTestMode)
{
case ScopeItemHitTestMode.Never:
return false;
case ScopeItemHitTestMode.Always:
return true;
case ScopeItemHitTestMode.WithoutCtrl:
return Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl);
case ScopeItemHitTestMode.WithCtrl:
return !Keyboard.IsKeyDown(Key.LeftCtrl) && !Keyboard.IsKeyDown(Key.RightCtrl);
}
return false;
}
protected override HitTestResult HitTestCore(PointHitTestParameters hitTestParameters)
{
if (ShouldSkipHitTest())
return null;
return base.HitTestCore(hitTestParameters);
}
protected override GeometryHitTestResult HitTestCore(GeometryHitTestParameters hitTestParameters)
{
if (ShouldSkipHitTest())
return null;
return base.HitTestCore(hitTestParameters);
}
FormattedText MakeText(string text) => new FormattedText(text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(FontFamily, FontStyle, FontWeight, FontStretch), FontSize, Foreground);
class PlacedItem
{
private readonly Pen _Pen;
private AdvancedScopePresenter _Presenter;
public AugmentedOutline.ScopeItem Item;
public PlacedComponent Prefix, Body;
public PlacedItem(AugmentedOutline.ScopeItem item, AdvancedScopePresenter presenter, Pen pen)
{
_Pen = pen;
_Presenter = presenter;
Item = item;
GenerateComponents();
}
private void GenerateComponents()
{
Prefix = Body = null;
if (Item == null)
return;
if (Item.ScopePrefix == AugmentedOutline.ScopePrefixType.Block && _Presenter.SeparatorGlyph != null)
Prefix = new PlacedComponent.Glyph(_Presenter.SeparatorGlyph, _Pen) { IsGrayedOut = Item.IsGrayedOut };
else if (Item.ScopePrefix == AugmentedOutline.ScopePrefixType.Arrow && _Presenter.ArrowGlyph != null)
Prefix = new PlacedComponent.Glyph(_Presenter.ArrowGlyph, _Pen) { IsGrayedOut = Item.IsGrayedOut };
else if (Item.ScopePrefix == AugmentedOutline.ScopePrefixType.Namespace)
Prefix = new PlacedComponent.Text(_Presenter.MakeText("::")) { IsGrayedOut = Item.IsGrayedOut };
else if (Item.ScopePrefix == AugmentedOutline.ScopePrefixType.Colon)
Prefix = new PlacedComponent.Text(_Presenter.MakeText(": ")) { IsGrayedOut = Item.IsGrayedOut };
else if (Item.ScopePrefix == AugmentedOutline.ScopePrefixType.Equals)
Prefix = new PlacedComponent.Text(_Presenter.MakeText(" = ")) { IsGrayedOut = Item.IsGrayedOut };
else if (Item.ScopePrefix == AugmentedOutline.ScopePrefixType.Space)
Prefix = new PlacedComponent.Text(_Presenter.MakeText(" ")) { IsGrayedOut = Item.IsGrayedOut };
Body = new PlacedComponent.Text(_Presenter.MakeText(Item.Text ?? ""), Item) { IsGrayedOut = Item.IsGrayedOut };
}
public void UpdateNextPriority(int currentPrio, ref int nextPrio)
{
if (Item != null && Item.Priority < currentPrio)
nextPrio = Math.Max(nextPrio, Item.Priority);
else if (Item?.ShorterVersion != null && Item.ShorterVersion.Priority < currentPrio)
nextPrio = Math.Max(nextPrio, Item.ShorterVersion.Priority);
}
public void ApplyPriority(int currentPrio)
{
if (Item == null || Item.Priority <= currentPrio)
return;
Item = Item.ShorterVersion;
GenerateComponents();
}
public IEnumerable<PlacedComponent> GetComponents(bool isFirst)
{
if (!isFirst && Prefix != null)
yield return Prefix;
if (Body != null)
yield return Body;
}
}
protected override Size ArrangeOverride(Size arrangeBounds)
{
_Components = PlaceComponents(arrangeBounds.Width, out _);
return arrangeBounds;
}
private List<PlacedComponent> PlaceComponents(double maxWidth, out int maxX)
{
Pen pen = new Pen(Foreground, 1);
var placedItems = (_Scope?.Items).EnsureNotNull().ArraySelect(i => new PlacedItem(i, this, pen));
const int spacing = 2;
int x = 0;
List<PlacedComponent> components = new List<PlacedComponent>();
if (placedItems.Length > 0)
{
var prio = placedItems.Max(p => p.Item?.Priority ?? 0);
for (; ; )
{
components.Clear();
x = 0;
for (int i = 0; i < placedItems.Length; i++)
{
foreach (var c in placedItems[i].GetComponents(components.Count == 0))
{
components.Add(c);
c.X = x;
x += c.Width + spacing;
}
}
if (x <= maxWidth)
break;
var nextPrio = 0;
foreach (var pi in placedItems)
pi.UpdateNextPriority(prio, ref nextPrio);
if (nextPrio == prio)
break;
foreach (var pi in placedItems)
pi.ApplyPriority(nextPrio);
prio = nextPrio;
}
}
maxX = x;
return components;
}
protected override Size MeasureOverride(Size constraint)
{
if (double.IsInfinity(constraint.Width))
{
int widthLimit = int.MaxValue;
var frac = SoftWidthLimitFraction;
if (!double.IsNaN(frac) && SoftWidthReference is FrameworkElement fwe && fwe.ActualWidth > 0)
widthLimit = (int)(fwe.ActualWidth * frac);
PlaceComponents(widthLimit, out var maxX)?.LastOrDefault();
return new Size(maxX, 0);
}
return new Size(0, 0);
}
protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
base.OnPropertyChanged(e);
if (e.Property == ScopeProperty)
{
_Scope = e.NewValue as OutlineScope;
}
}
}
public class ScopeItemClickedEventArgs : EventArgs
{
public AugmentedOutline.ScopeItem Item;
public ScopeItemClickedEventArgs(AugmentedOutline.ScopeItem item)
{
Item = item;
}
}
public enum ScopeItemHitTestMode
{
Always,
Never,
WithCtrl,
WithoutCtrl,
}
}