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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
|
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace Tango.Editors
{
/// <summary>
/// A ruler el which displays ruler in pixels.
/// In order to use it vertically, change the <see cref="Marks">Marks</see> property to <c>Up</c> and rotate it ninety degrees.
/// </summary>
/// <remarks>
/// Rewritten by: Sebestyen Murancsik
///
/// Contributions from <see
/// cref="http://www.orbifold.net/default/?p=2295&cpage=1#comment-61500">Raf
/// Lenfers</see>
/// <seealso>http://visualizationtools.net/default/wpf-ruler/</seealso>
/// </remarks>
internal class PixelRuler : Control
{
internal enum RulerOrientationEnum
{
Horizontal,
Vertical
}
#region Fields
private double SegmentHeight;
private Pen p = new Pen(Brushes.Orange, 1.0);
private Pen markerPen = new Pen(Brushes.Orange, 0.5);
private Pen BorderPen = new Pen(Brushes.Orange, 1.0);
private Pen RedPen = new Pen(Brushes.Red, 2.0);
#endregion
#region Properties
public RulerOrientationEnum Orientation
{
get { return (RulerOrientationEnum)GetValue(OrientationProperty); }
set { SetValue(OrientationProperty, value); }
}
// Using a DependencyProperty as the backing store for Orientation. This enables animation, styling, binding, etc...
public static readonly DependencyProperty OrientationProperty =
DependencyProperty.Register("Orientation", typeof(RulerOrientationEnum), typeof(PixelRuler), new PropertyMetadata(RulerOrientationEnum.Horizontal));
#region Length
/// <summary>
/// Gets or sets the length of the ruler. If the <see cref="AutoSize"/> property is set to false (default) this
/// is a fixed length. Otherwise the length is calculated based on the actual width of the ruler.
/// </summary>
public double Length
{
get
{
if (this.AutoSize)
{
if (Orientation == RulerOrientationEnum.Horizontal)
{
return ActualWidth / Zoom;
}
else
{
return ActualHeight / Zoom;
}
}
else
{
return (double)GetValue(LengthProperty);
}
}
set
{
SetValue(LengthProperty, value);
}
}
/// <summary>
/// Identifies the Length dependency property.
/// </summary>
public static readonly DependencyProperty LengthProperty =
DependencyProperty.Register(
"Length",
typeof(double),
typeof(PixelRuler),
new FrameworkPropertyMetadata(20D, FrameworkPropertyMetadataOptions.AffectsRender));
#endregion
#region AutoSize
/// <summary>
/// Gets or sets the AutoSize behavior of the ruler.
/// false (default): the lenght of the ruler results from the <see cref="Length"/> property. If the window size is changed, e.g. wider
/// than the rulers length, free space is shown at the end of the ruler. No rescaling is done.
/// true : the length of the ruler is always adjusted to its actual width. This ensures that the ruler is shown
/// for the actual width of the window.
/// </summary>
public bool AutoSize
{
get
{
return (bool)GetValue(AutoSizeProperty);
}
set
{
SetValue(AutoSizeProperty, value);
this.InvalidateVisual();
}
}
/// <summary>
/// Identifies the AutoSize dependency property.
/// </summary>
public static readonly DependencyProperty AutoSizeProperty =
DependencyProperty.Register(
"AutoSize",
typeof(bool),
typeof(PixelRuler),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
#endregion
#region Zoom
/// <summary>
/// Gets or sets the zoom factor for the ruler. The default value is 1.0.
/// </summary>
public double Zoom
{
get
{
return (double)GetValue(ZoomProperty);
}
set
{
SetValue(ZoomProperty, value);
this.InvalidateVisual();
}
}
/// <summary>
/// Identifies the Zoom dependency property.
/// </summary>
public static readonly DependencyProperty ZoomProperty =
DependencyProperty.Register("Zoom", typeof(double), typeof(PixelRuler),
new FrameworkPropertyMetadata((double)1.0,
FrameworkPropertyMetadataOptions.AffectsRender));
#endregion
#region SmallStep
/// <summary>
/// Gets or sets the small step for the ruler. The default value is 25.0.
/// </summary>
public double SmallStep
{
get
{
return (double)GetValue(SmallStepProperty);
}
set
{
SetValue(SmallStepProperty, value);
}
}
/// <summary>
/// Identifies the Zoom dependency property.
/// </summary>
public static readonly DependencyProperty SmallStepProperty =
DependencyProperty.Register("SmallStep", typeof(double), typeof(PixelRuler),
new FrameworkPropertyMetadata((double)25.0,
FrameworkPropertyMetadataOptions.AffectsRender));
#endregion
#region Step
/// <summary>
/// Gets or sets the step for the ruler. The default value is 100.0.
/// </summary>
public double Step
{
get
{
return (double)GetValue(StepProperty);
}
set
{
SetValue(StepProperty, value);
}
}
/// <summary>
/// Identifies the Zoom dependency property.
/// </summary>
public static readonly DependencyProperty StepProperty =
DependencyProperty.Register("Step", typeof(double), typeof(PixelRuler),
new FrameworkPropertyMetadata((double)100.0,
FrameworkPropertyMetadataOptions.AffectsRender));
#endregion
#region Chip
/// <summary>
/// Chip Dependency Property
/// </summary>
public static readonly DependencyProperty ChipProperty =
DependencyProperty.Register("Chip", typeof(double), typeof(PixelRuler),
new FrameworkPropertyMetadata((double)-1000,
FrameworkPropertyMetadataOptions.AffectsRender));
/// <summary>
/// Sets the location of the chip in the units of the ruler.
/// So, to set the chip to 100px units the chip needs to be set to 100.
/// Use the <see cref="DipHelper"/> class for conversions.
/// </summary>
public double Chip
{
get { return (double)GetValue(ChipProperty); }
set { SetValue(ChipProperty, value); }
}
#endregion
#region CountShift
/// <summary>
/// CountShift Dependency Property
/// </summary>
public static readonly DependencyProperty CountShiftProperty =
DependencyProperty.Register("CountShift", typeof(int), typeof(PixelRuler),
new FrameworkPropertyMetadata(0,
FrameworkPropertyMetadataOptions.AffectsRender));
/// <summary>
/// By default the counting of numbers starts at zero, this property allows you to shift
/// the counting.
/// </summary>
public int CountShift
{
get { return (int)GetValue(CountShiftProperty); }
set { SetValue(CountShiftProperty, value); }
}
#endregion
#region Marks
/// <summary>
/// Marks Dependency Property
/// </summary>
public static readonly DependencyProperty MarksProperty =
DependencyProperty.Register("Marks", typeof(MarksLocation), typeof(PixelRuler),
new FrameworkPropertyMetadata(MarksLocation.Up,
FrameworkPropertyMetadataOptions.AffectsRender));
/// <summary>
/// Gets or sets where the marks are shown in the ruler.
/// </summary>
public MarksLocation Marks
{
get { return (MarksLocation)GetValue(MarksProperty); }
set { SetValue(MarksProperty, value); }
}
#endregion
#endregion
#region Constructors
public PixelRuler()
{
}
#endregion
#region Methods
private void SetSteps()
{
var value = Zoom;
if (value >= 4.8)
{
SmallStep = 5;
Step = 10;
}
else if (value >= 4)
{
SmallStep = 5;
Step = 10;
}
else if (value >= 3)
{
SmallStep = 10;
Step = 20;
}
else if (value >= 2)
{
SmallStep = (12.5) / 2;
Step = 25;
}
else if (value >= 1.5)
{
SmallStep = 12.5;
Step = 50;
}
else if (value >= 1)
{
SmallStep = 25;
Step = 100;
}
else if (value <= 0.2)
{
SmallStep = 100;
Step = 400;
}
else if (value <= 0.5)
{
SmallStep = 50;
Step = 200;
}
}
/// <summary>
/// Participates in rendering operations.
/// </summary>
/// <param name="drawingContext">The drawing instructions for a specific element. This context is provided to the layout system.</param>
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
SetSteps();
var markersPen = new Pen(Foreground, 1);
SegmentHeight = ActualHeight - 10;
if (Orientation == RulerOrientationEnum.Horizontal)
{
double xDest = Length * Zoom;
//drawingContext.DrawRectangle(RulerBackground, BorderPen, new Rect(new Point(0.0, 0.0), new Point(xDest, Height)));
//drawingContext.DrawLine(RedPen, new Point(Chip, 0), new Point(Chip, Height));
for (double dUnit = 0; dUnit < Length; dUnit += SmallStep)
{
double d = dUnit * this.Zoom;
var startHeight = ActualHeight;
var endHeight = ((dUnit % Step == 0) ? (this.ActualHeight / 5) * 1.4 : this.ActualHeight / 5);
if (Marks == MarksLocation.Down)
{
drawingContext.DrawLine(markersPen, new Point(d, startHeight), new Point(d, (startHeight - endHeight)));
}
else
{
drawingContext.DrawLine(markersPen, new Point(d, 0), new Point(d, endHeight));
}
if ((dUnit != 0.0) && (dUnit % Step == 0) && (dUnit < Length))
{
FormattedText ft = new FormattedText((dUnit + CountShift).ToString(CultureInfo.CurrentCulture),
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface(FontFamily.ToString()),
FontSize,
Foreground);
ft.SetFontWeight(FontWeight);
ft.TextAlignment = TextAlignment.Center;
//drawingContext.PushTransform(new RotateTransform(180));
if (Marks == MarksLocation.Down)
{
drawingContext.DrawText(ft, new Point(d, (ActualHeight / 2) - (ft.Height / 2) - 2));
}
else
{
drawingContext.DrawText(ft, new Point(d, (ActualHeight / 2) - (ft.Height / 2) + 2));
}
//drawingContext.PushTransform(new RotateTransform(0));
}
}
}
else
{
double xDest = Length * Zoom;
//drawingContext.DrawRectangle(RulerBackground, BorderPen, new Rect(new Point(0.0, 0.0), new Point(Width, xDest)));
//drawingContext.DrawLine(RedPen, new Point(Chip, 0), new Point(Chip, ActualHeight));
for (double dUnit = 0; dUnit < Length; dUnit += SmallStep)
{
double d = dUnit * this.Zoom;
double startHeight;
double endHeight;
if (Marks == MarksLocation.Up)
{
startHeight = 0;
// Main step or small step?
endHeight = ((dUnit % Step == 0) ? SegmentHeight : SegmentHeight / 2);
}
else
{
startHeight = Height;
// Main step or small step?
endHeight = ((dUnit % Step == 0) ? SegmentHeight : SegmentHeight * 1.5);
}
drawingContext.DrawLine(markersPen, new Point(21, d), new Point(endHeight + 8, d));
if ((dUnit != 0.0) && (dUnit % Step == 0) && (dUnit < Length))
{
FormattedText ft = new FormattedText((dUnit + CountShift).ToString(CultureInfo.CurrentCulture),
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface(FontFamily.ToString()),
FontSize,
Foreground);
ft.SetFontWeight(FontWeight);
ft.TextAlignment = TextAlignment.Center;
drawingContext.DrawText(ft, new Point(d, (ActualHeight / 2) - (ft.Height / 2) - 2));
}
}
}
}
#endregion
}
internal enum MarksLocation
{
Up, Down
}
/// <summary>
/// A helper class for DIP (Device Independent Pixels) conversion and scaling operations.
/// </summary>
internal static class DipHelper
{
/// <summary>
/// Converts font points to DIP (Device Independant Pixels).
/// </summary>
/// <param name="pt">A font point value.</param>
/// <returns>A DIP value.</returns>
public static double PtToDip(double pt)
{
return (pt * 96.0 / 72.0);
}
/// <summary>
/// Gets the system DPI scale factor (compared to 96 dpi).
/// From http://blogs.msdn.com/jaimer/archive/2007/03/07/getting-system-dpi-in-wpf-app.aspx
/// Should not be called before the Loaded event (else XamlException mat throw)
/// </summary>
/// <returns>A Point object containing the X- and Y- scale factor.</returns>
private static Point GetSystemDpiFactor()
{
PresentationSource source = PresentationSource.FromVisual(Application.Current.MainWindow);
Matrix m = source.CompositionTarget.TransformToDevice;
return new Point(m.M11, m.M22);
}
private const double DpiBase = 96.0;
/// <summary>
/// Gets the system configured DPI.
/// </summary>
/// <returns>A Point object containing the X- and Y- DPI.</returns>
public static Point GetSystemDpi()
{
Point sysDpiFactor = GetSystemDpiFactor();
return new Point(
sysDpiFactor.X * DpiBase,
sysDpiFactor.Y * DpiBase);
}
}
}
|