-
Notifications
You must be signed in to change notification settings - Fork 1
/
ZoomAndPanControl.cs
1021 lines (874 loc) · 40.3 KB
/
ZoomAndPanControl.cs
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
namespace ZoomAndPan
{
/// <summary>
/// A class that wraps up zooming and panning of it's content.
/// </summary>
public partial class ZoomAndPanControl : ContentControl, IScrollInfo
{
#region Internal Data Members
/// <summary>
/// Reference to the underlying content, which is named PART_Content in the template.
/// </summary>
private FrameworkElement content = null;
/// <summary>
/// The transform that is applied to the content to scale it by 'ContentScale'.
/// </summary>
private ScaleTransform contentScaleTransform = null;
/// <summary>
/// The transform that is applied to the content to offset it by 'ContentOffsetX' and 'ContentOffsetY'.
/// </summary>
private TranslateTransform contentOffsetTransform = null;
/// <summary>
/// Enable the update of the content offset as the content scale changes.
/// This enabled for zooming about a point (Google-maps style zooming) and zooming to a rect.
/// </summary>
private bool enableContentOffsetUpdateFromScale = false;
/// <summary>
/// Used to disable synchronization between IScrollInfo interface and ContentOffsetX/ContentOffsetY.
/// </summary>
private bool disableScrollOffsetSync = false;
/// <summary>
/// Normally when content offsets changes the content focus is automatically updated.
/// This synchronization is disabled when 'disableContentFocusSync' is set to 'true'.
/// When we are zooming in or out we 'disableContentFocusSync' is set to 'true' because
/// we are zooming in or out relative to the content focus we don't want to update the focus.
/// </summary>
private bool disableContentFocusSync = false;
/// <summary>
/// The width of the viewport in content coordinates, clamped to the width of the content.
/// </summary>
private double constrainedContentViewportWidth = 0.0;
/// <summary>
/// The height of the viewport in content coordinates, clamped to the height of the content.
/// </summary>
private double constrainedContentViewportHeight = 0.0;
#endregion Internal Data Members
#region IScrollInfo Data Members
//
// These data members are for the implementation of the IScrollInfo interface.
// This interface works with the ScrollViewer such that when ZoomAndPanControl is
// wrapped (in XAML) with a ScrollViewer the IScrollInfo interface allows the ZoomAndPanControl to
// handle the the scrollbar offsets.
//
// The IScrollInfo properties and member functions are implemented in ZoomAndPanControl_IScrollInfo.cs.
//
// There is a good series of articles showing how to implement IScrollInfo starting here:
// http://blogs.msdn.com/bencon/archive/2006/01/05/509991.aspx
//
/// <summary>
/// Set to 'true' when the vertical scrollbar is enabled.
/// </summary>
private bool canVerticallyScroll = false;
/// <summary>
/// Set to 'true' when the vertical scrollbar is enabled.
/// </summary>
private bool canHorizontallyScroll = false;
/// <summary>
/// Records the un-scaled extent of the content.
/// This is calculated during the measure and arrange.
/// </summary>
private Size unScaledExtent = new Size(0, 0);
/// <summary>
/// Records the size of the viewport (in viewport coordinates) onto the content.
/// This is calculated during the measure and arrange.
/// </summary>
private Size viewport = new Size(0, 0);
/// <summary>
/// Reference to the ScrollViewer that is wrapped (in XAML) around the ZoomAndPanControl.
/// Or set to null if there is no ScrollViewer.
/// </summary>
private ScrollViewer scrollOwner = null;
#endregion IScrollInfo Data Members
#region Dependency Property Definitions
//
// Definitions for dependency properties.
//
public static readonly DependencyProperty ContentScaleProperty =
DependencyProperty.Register("ContentScale", typeof(double), typeof(ZoomAndPanControl),
new FrameworkPropertyMetadata(1.0, ContentScale_PropertyChanged, ContentScale_Coerce));
public static readonly DependencyProperty MinContentScaleProperty =
DependencyProperty.Register("MinContentScale", typeof(double), typeof(ZoomAndPanControl),
new FrameworkPropertyMetadata(0.01, MinOrMaxContentScale_PropertyChanged));
public static readonly DependencyProperty MaxContentScaleProperty =
DependencyProperty.Register("MaxContentScale", typeof(double), typeof(ZoomAndPanControl),
new FrameworkPropertyMetadata(10.0, MinOrMaxContentScale_PropertyChanged));
public static readonly DependencyProperty ContentOffsetXProperty =
DependencyProperty.Register("ContentOffsetX", typeof(double), typeof(ZoomAndPanControl),
new FrameworkPropertyMetadata(0.0, ContentOffsetX_PropertyChanged, ContentOffsetX_Coerce));
public static readonly DependencyProperty ContentOffsetYProperty =
DependencyProperty.Register("ContentOffsetY", typeof(double), typeof(ZoomAndPanControl),
new FrameworkPropertyMetadata(0.0, ContentOffsetY_PropertyChanged, ContentOffsetY_Coerce));
public static readonly DependencyProperty AnimationDurationProperty =
DependencyProperty.Register("AnimationDuration", typeof(double), typeof(ZoomAndPanControl),
new FrameworkPropertyMetadata(0.4));
public static readonly DependencyProperty ContentZoomFocusXProperty =
DependencyProperty.Register("ContentZoomFocusX", typeof(double), typeof(ZoomAndPanControl),
new FrameworkPropertyMetadata(0.0));
public static readonly DependencyProperty ContentZoomFocusYProperty =
DependencyProperty.Register("ContentZoomFocusY", typeof(double), typeof(ZoomAndPanControl),
new FrameworkPropertyMetadata(0.0));
public static readonly DependencyProperty ViewportZoomFocusXProperty =
DependencyProperty.Register("ViewportZoomFocusX", typeof(double), typeof(ZoomAndPanControl),
new FrameworkPropertyMetadata(0.0));
public static readonly DependencyProperty ViewportZoomFocusYProperty =
DependencyProperty.Register("ViewportZoomFocusY", typeof(double), typeof(ZoomAndPanControl),
new FrameworkPropertyMetadata(0.0));
public static readonly DependencyProperty ContentViewportWidthProperty =
DependencyProperty.Register("ContentViewportWidth", typeof(double), typeof(ZoomAndPanControl),
new FrameworkPropertyMetadata(0.0));
public static readonly DependencyProperty ContentViewportHeightProperty =
DependencyProperty.Register("ContentViewportHeight", typeof(double), typeof(ZoomAndPanControl),
new FrameworkPropertyMetadata(0.0));
public static readonly DependencyProperty IsMouseWheelScrollingEnabledProperty =
DependencyProperty.Register("IsMouseWheelScrollingEnabled", typeof(bool), typeof(ZoomAndPanControl),
new FrameworkPropertyMetadata(false));
#endregion Dependency Property Definitions
/// <summary>
/// Get/set the X offset (in content coordinates) of the view on the content.
/// </summary>
public double ContentOffsetX
{
get
{
return (double)GetValue(ContentOffsetXProperty);
}
set
{
SetValue(ContentOffsetXProperty, value);
}
}
/// <summary>
/// Event raised when the ContentOffsetX property has changed.
/// </summary>
public event EventHandler ContentOffsetXChanged;
/// <summary>
/// Get/set the Y offset (in content coordinates) of the view on the content.
/// </summary>
public double ContentOffsetY
{
get
{
return (double)GetValue(ContentOffsetYProperty);
}
set
{
SetValue(ContentOffsetYProperty, value);
}
}
/// <summary>
/// Event raised when the ContentOffsetY property has changed.
/// </summary>
public event EventHandler ContentOffsetYChanged;
/// <summary>
/// Get/set the current scale (or zoom factor) of the content.
/// </summary>
public double ContentScale
{
get { return (double)GetValue(ContentScaleProperty); }
set { SetValue(ContentScaleProperty, value); }
}
/// <summary>
/// Event raised when the ContentScale property has changed.
/// </summary>
public event EventHandler ContentScaleChanged;
/// <summary>
/// The scale of the content as it fits perfectly in the control.
/// </summary>
public double ContentScaleNormal
{
get { return _ContentScaleNormal; }
private set { _ContentScaleNormal = value; }
}
double _ContentScaleNormal;
/// <summary>
/// Get/set the minimum value for 'ContentScale'.
/// </summary>
public double MinContentScale
{
get
{
return (double)GetValue(MinContentScaleProperty);
}
set
{
SetValue(MinContentScaleProperty, value);
}
}
/// <summary>
/// Get/set the maximum value for 'ContentScale'.
/// </summary>
public double MaxContentScale
{
get
{
return (double)GetValue(MaxContentScaleProperty);
}
set
{
SetValue(MaxContentScaleProperty, value);
}
}
/// <summary>
/// The X coordinate of the content focus, this is the point that we are focusing on when zooming.
/// </summary>
public double ContentZoomFocusX
{
get { return (double)GetValue(ContentZoomFocusXProperty); }
set { SetValue(ContentZoomFocusXProperty, value); }
}
/// <summary>
/// The Y coordinate of the content focus, this is the point that we are focusing on when zooming.
/// </summary>
public double ContentZoomFocusY
{
get { return (double)GetValue(ContentZoomFocusYProperty); }
set { SetValue(ContentZoomFocusYProperty, value); }
}
/// <summary>
/// The X coordinate of the viewport focus, this is the point in the viewport (in viewport coordinates)
/// that the content focus point is locked to while zooming in.
/// </summary>
public double ViewportZoomFocusX
{
get { return (double)GetValue(ViewportZoomFocusXProperty); }
set { SetValue(ViewportZoomFocusXProperty, value); }
}
/// <summary>
/// The Y coordinate of the viewport focus, this is the point in the viewport (in viewport coordinates)
/// that the content focus point is locked to while zooming in.
/// </summary>
public double ViewportZoomFocusY
{
get { return (double)GetValue(ViewportZoomFocusYProperty); }
set { SetValue(ViewportZoomFocusYProperty, value); }
}
/// <summary>
/// The duration of the animations (in seconds) started by calling AnimatedZoomTo and the other animation methods.
/// </summary>
public double AnimationDuration
{
get { return (double)GetValue(AnimationDurationProperty); }
set { SetValue(AnimationDurationProperty, value); }
}
/// <summary>
/// Get the viewport width, in content coordinates.
/// </summary>
public double ContentViewportWidth
{
get { return (double)GetValue(ContentViewportWidthProperty); }
set { SetValue(ContentViewportWidthProperty, value); }
}
/// <summary>
/// Get the viewport height, in content coordinates.
/// </summary>
public double ContentViewportHeight
{
get { return (double)GetValue(ContentViewportHeightProperty); }
set { SetValue(ContentViewportHeightProperty, value); }
}
/// <summary>
/// Set to 'true' to enable the mouse wheel to scroll the zoom and pan control.
/// This is set to 'false' by default.
/// </summary>
public bool IsMouseWheelScrollingEnabled
{
get
{
return (bool)GetValue(IsMouseWheelScrollingEnabledProperty);
}
set
{
SetValue(IsMouseWheelScrollingEnabledProperty, value);
}
}
/// <summary>
/// Do an animated zoom to view a specific scale and rectangle (in content coordinates).
/// </summary>
public void AnimatedZoomTo(double newScale, Rect contentRect)
{
AnimatedZoomPointToViewportCenter(newScale, new Point(contentRect.X + (contentRect.Width / 2), contentRect.Y + (contentRect.Height / 2)),
delegate(object sender, EventArgs e)
{
//
// At the end of the animation, ensure that we are snapped to the specified content offset.
// Due to zooming in on the content focus point and rounding errors, the content offset may
// be slightly off what we want at the end of the animation and this bit of code corrects it.
//
this.ContentOffsetX = contentRect.X;
this.ContentOffsetY = contentRect.Y;
});
}
/// <summary>
/// Do an animated zoom to the specified rectangle (in content coordinates).
/// </summary>
public void AnimatedZoomTo(Rect contentRect)
{
double newScale = ScaleOfRect(contentRect);
AnimatedZoomPointToViewportCenter(newScale, new Point(contentRect.X + (contentRect.Width / 2), contentRect.Y + (contentRect.Height / 2)), null);
}
/// <summary>
/// Instantly zoom to the specified rectangle (in content coordinates).
/// </summary>
public void ZoomTo(Rect contentRect)
{
double newScale = ScaleOfRect(contentRect);
ZoomPointToViewportCenter(newScale, new Point(contentRect.X + (contentRect.Width / 2), contentRect.Y + (contentRect.Height / 2)));
}
/// <summary>
/// Get the scale needed to fit the rectangle perfectly in the control (zoomed to it)
/// </summary>
/// <param name="contentRect"></param>
/// <returns></returns>
public double ScaleOfRect(Rect contentRect)
{
double scaleX = this.ContentViewportWidth / contentRect.Width;
double scaleY = this.ContentViewportHeight / contentRect.Height;
return this.ContentScale * Math.Min(scaleX, scaleY);
}
/// <summary>
/// Instantly center the view on the specified point (in content coordinates).
/// </summary>
public void SnapContentOffsetTo(Point contentOffset)
{
AnimationHelper.CancelAnimation(this, ContentOffsetXProperty);
AnimationHelper.CancelAnimation(this, ContentOffsetYProperty);
this.ContentOffsetX = contentOffset.X;
this.ContentOffsetY = contentOffset.Y;
}
/// <summary>
/// Instantly center the view on the specified point (in content coordinates).
/// </summary>
public void SnapTo(Point contentPoint)
{
AnimationHelper.CancelAnimation(this, ContentOffsetXProperty);
AnimationHelper.CancelAnimation(this, ContentOffsetYProperty);
this.ContentOffsetX = contentPoint.X - (this.ContentViewportWidth / 2);
this.ContentOffsetY = contentPoint.Y - (this.ContentViewportHeight / 2);
}
/// <summary>
/// Use animation to center the view on the specified point (in content coordinates).
/// </summary>
public void AnimatedSnapTo(Point contentPoint)
{
double newX = contentPoint.X - (this.ContentViewportWidth / 2);
double newY = contentPoint.Y - (this.ContentViewportHeight / 2);
AnimationHelper.StartAnimation(this, ContentOffsetXProperty, newX, AnimationDuration);
AnimationHelper.StartAnimation(this, ContentOffsetYProperty, newY, AnimationDuration);
}
/// <summary>
/// Zoom in/out centered on the specified point (in content coordinates).
/// The focus point is kept locked to it's on screen position (ala Google maps).
/// </summary>
public void AnimatedZoomAboutPoint(double newContentScale, Point contentZoomFocus)
{
newContentScale = Math.Min(Math.Max(newContentScale, MinContentScale), MaxContentScale);
AnimationHelper.CancelAnimation(this, ContentZoomFocusXProperty);
AnimationHelper.CancelAnimation(this, ContentZoomFocusYProperty);
AnimationHelper.CancelAnimation(this, ViewportZoomFocusXProperty);
AnimationHelper.CancelAnimation(this, ViewportZoomFocusYProperty);
ContentZoomFocusX = contentZoomFocus.X;
ContentZoomFocusY = contentZoomFocus.Y;
ViewportZoomFocusX = (ContentZoomFocusX - ContentOffsetX) * ContentScale;
ViewportZoomFocusY = (ContentZoomFocusY - ContentOffsetY) * ContentScale;
//
// When zooming about a point make updates to ContentScale also update content offset.
//
enableContentOffsetUpdateFromScale = true;
AnimationHelper.StartAnimation(this, ContentScaleProperty, newContentScale, AnimationDuration,
delegate(object sender, EventArgs e)
{
enableContentOffsetUpdateFromScale = false;
ResetViewportZoomFocus();
});
}
/// <summary>
/// Zoom in/out centered on the specified point (in content coordinates).
/// The focus point is kept locked to it's on screen position (ala Google maps).
/// </summary>
public void ZoomAboutPoint(double newContentScale, Point contentZoomFocus)
{
newContentScale = Math.Min(Math.Max(newContentScale, MinContentScale), MaxContentScale);
double screenSpaceZoomOffsetX = (contentZoomFocus.X - ContentOffsetX) * ContentScale;
double screenSpaceZoomOffsetY = (contentZoomFocus.Y - ContentOffsetY) * ContentScale;
double contentSpaceZoomOffsetX = screenSpaceZoomOffsetX / newContentScale;
double contentSpaceZoomOffsetY = screenSpaceZoomOffsetY / newContentScale;
double newContentOffsetX = contentZoomFocus.X - contentSpaceZoomOffsetX;
double newContentOffsetY = contentZoomFocus.Y - contentSpaceZoomOffsetY;
AnimationHelper.CancelAnimation(this, ContentScaleProperty);
AnimationHelper.CancelAnimation(this, ContentOffsetXProperty);
AnimationHelper.CancelAnimation(this, ContentOffsetYProperty);
this.ContentScale = newContentScale;
this.ContentOffsetX = newContentOffsetX;
this.ContentOffsetY = newContentOffsetY;
}
/// <summary>
/// Zoom in/out centered on the viewport center.
/// </summary>
public void AnimatedZoomTo(double contentScale)
{
Point zoomCenter = new Point(ContentOffsetX + (ContentViewportWidth / 2), ContentOffsetY + (ContentViewportHeight / 2));
AnimatedZoomAboutPoint(contentScale, zoomCenter);
}
/// <summary>
/// Zoom in/out centered on the viewport center.
/// </summary>
public void ZoomTo(double contentScale)
{
Point zoomCenter = new Point(ContentOffsetX + (ContentViewportWidth / 2), ContentOffsetY + (ContentViewportHeight / 2));
ZoomAboutPoint(contentScale, zoomCenter);
}
/// <summary>
/// Do animation that scales the content so that it fits completely in the control.
/// </summary>
public void AnimatedScaleToFit()
{
if (content == null)
{
throw new ApplicationException("PART_Content was not found in the ZoomAndPanControl visual template!");
}
AnimatedZoomTo(new Rect(0, 0, content.ActualWidth, content.ActualHeight));
}
/// <summary>
/// Instantly scale the content so that it fits completely in the control.
/// </summary>
public void ScaleToFit()
{
if (content == null)
{
throw new ApplicationException("PART_Content was not found in the ZoomAndPanControl visual template!");
}
ZoomTo(new Rect(0, 0, content.ActualWidth, content.ActualHeight));
}
#region Internal Methods
/// <summary>
/// Static constructor to define metadata for the control (and link it to the style in Generic.xaml).
/// </summary>
static ZoomAndPanControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ZoomAndPanControl), new FrameworkPropertyMetadata(typeof(ZoomAndPanControl)));
}
/// <summary>
/// Called when a template has been applied to the control.
/// </summary>
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
content = this.Template.FindName("PART_Content", this) as FrameworkElement;
if (content != null)
{
//
// Setup the transform on the content so that we can scale it by 'ContentScale'.
//
this.contentScaleTransform = new ScaleTransform(this.ContentScale, this.ContentScale);
//
// Setup the transform on the content so that we can translate it by 'ContentOffsetX' and 'ContentOffsetY'.
//
this.contentOffsetTransform = new TranslateTransform();
UpdateTranslationX();
UpdateTranslationY();
//
// Setup a transform group to contain the translation and scale transforms, and then
// assign this to the content's 'RenderTransform'.
//
TransformGroup transformGroup = new TransformGroup();
transformGroup.Children.Add(this.contentOffsetTransform);
transformGroup.Children.Add(this.contentScaleTransform);
content.RenderTransform = transformGroup;
}
}
protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
base.OnRenderSizeChanged(sizeInfo);
//
// Determine scale needed to fit content perfectly in the control
//
ContentScaleNormal = ScaleOfRect(new Rect(0, 0, content.ActualWidth, content.ActualHeight));
}
/// <summary>
/// Zoom to the specified scale and move the specified focus point to the center of the viewport.
/// </summary>
private void AnimatedZoomPointToViewportCenter(double newContentScale, Point contentZoomFocus, EventHandler callback)
{
newContentScale = Math.Min(Math.Max(newContentScale, MinContentScale), MaxContentScale);
AnimationHelper.CancelAnimation(this, ContentZoomFocusXProperty);
AnimationHelper.CancelAnimation(this, ContentZoomFocusYProperty);
AnimationHelper.CancelAnimation(this, ViewportZoomFocusXProperty);
AnimationHelper.CancelAnimation(this, ViewportZoomFocusYProperty);
ContentZoomFocusX = contentZoomFocus.X;
ContentZoomFocusY = contentZoomFocus.Y;
ViewportZoomFocusX = (ContentZoomFocusX - ContentOffsetX) * ContentScale;
ViewportZoomFocusY = (ContentZoomFocusY - ContentOffsetY) * ContentScale;
//
// When zooming about a point make updates to ContentScale also update content offset.
//
enableContentOffsetUpdateFromScale = true;
AnimationHelper.StartAnimation(this, ContentScaleProperty, newContentScale, AnimationDuration,
delegate(object sender, EventArgs e)
{
enableContentOffsetUpdateFromScale = false;
if (callback != null)
{
callback(this, EventArgs.Empty);
}
});
AnimationHelper.StartAnimation(this, ViewportZoomFocusXProperty, ViewportWidth / 2, AnimationDuration);
AnimationHelper.StartAnimation(this, ViewportZoomFocusYProperty, ViewportHeight / 2, AnimationDuration);
}
/// <summary>
/// Zoom to the specified scale and move the specified focus point to the center of the viewport.
/// </summary>
private void ZoomPointToViewportCenter(double newContentScale, Point contentZoomFocus)
{
newContentScale = Math.Min(Math.Max(newContentScale, MinContentScale), MaxContentScale);
AnimationHelper.CancelAnimation(this, ContentScaleProperty);
AnimationHelper.CancelAnimation(this, ContentOffsetXProperty);
AnimationHelper.CancelAnimation(this, ContentOffsetYProperty);
this.ContentScale = newContentScale;
this.ContentOffsetX = contentZoomFocus.X - (ContentViewportWidth / 2);
this.ContentOffsetY = contentZoomFocus.Y - (ContentViewportHeight / 2);
}
/// <summary>
/// Event raised when the 'ContentScale' property has changed value.
/// </summary>
private static void ContentScale_PropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
ZoomAndPanControl c = (ZoomAndPanControl)o;
if (c.contentScaleTransform != null)
{
//
// Update the content scale transform whenever 'ContentScale' changes.
//
c.contentScaleTransform.ScaleX = c.ContentScale;
c.contentScaleTransform.ScaleY = c.ContentScale;
}
//
// Update the size of the viewport in content coordinates.
//
c.UpdateContentViewportSize();
if (c.enableContentOffsetUpdateFromScale)
{
try
{
//
// Disable content focus synchronization. We are about to update content offset whilst zooming
// to ensure that the viewport is focused on our desired content focus point. Setting this
// to 'true' stops the automatic update of the content focus when content offset changes.
//
c.disableContentFocusSync = true;
//
// Whilst zooming in or out keep the content offset up-to-date so that the viewport is always
// focused on the content focus point (and also so that the content focus is locked to the
// viewport focus point - this is how the Google maps style zooming works).
//
double viewportOffsetX = c.ViewportZoomFocusX - (c.ViewportWidth / 2);
double viewportOffsetY = c.ViewportZoomFocusY - (c.ViewportHeight / 2);
double contentOffsetX = viewportOffsetX / c.ContentScale;
double contentOffsetY = viewportOffsetY / c.ContentScale;
c.ContentOffsetX = (c.ContentZoomFocusX - (c.ContentViewportWidth / 2)) - contentOffsetX;
c.ContentOffsetY = (c.ContentZoomFocusY - (c.ContentViewportHeight / 2)) - contentOffsetY;
}
finally
{
c.disableContentFocusSync = false;
}
}
if (c.ContentScaleChanged != null)
{
c.ContentScaleChanged(c, EventArgs.Empty);
}
if (c.scrollOwner != null)
{
c.scrollOwner.InvalidateScrollInfo();
}
}
/// <summary>
/// Method called to clamp the 'ContentScale' value to its valid range.
/// </summary>
private static object ContentScale_Coerce(DependencyObject d, object baseValue)
{
ZoomAndPanControl c = (ZoomAndPanControl)d;
double value = (double)baseValue;
value = Math.Min(Math.Max(value, c.MinContentScale), c.MaxContentScale);
return value;
}
/// <summary>
/// Event raised 'MinContentScale' or 'MaxContentScale' has changed.
/// </summary>
private static void MinOrMaxContentScale_PropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
ZoomAndPanControl c = (ZoomAndPanControl)o;
c.ContentScale = Math.Min(Math.Max(c.ContentScale, c.MinContentScale), c.MaxContentScale);
}
/// <summary>
/// Event raised when the 'ContentOffsetX' property has changed value.
/// </summary>
private static void ContentOffsetX_PropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
ZoomAndPanControl c = (ZoomAndPanControl)o;
c.UpdateTranslationX();
if (!c.disableContentFocusSync)
{
//
// Normally want to automatically update content focus when content offset changes.
// Although this is disabled using 'disableContentFocusSync' when content offset changes due to in-progress zooming.
//
c.UpdateContentZoomFocusX();
}
if (c.ContentOffsetXChanged != null)
{
//
// Raise an event to let users of the control know that the content offset has changed.
//
c.ContentOffsetXChanged(c, EventArgs.Empty);
}
if (!c.disableScrollOffsetSync && c.scrollOwner != null)
{
//
// Notify the owning ScrollViewer that the scrollbar offsets should be updated.
//
c.scrollOwner.InvalidateScrollInfo();
}
}
/// <summary>
/// Method called to clamp the 'ContentOffsetX' value to its valid range.
/// </summary>
private static object ContentOffsetX_Coerce(DependencyObject d, object baseValue)
{
ZoomAndPanControl c = (ZoomAndPanControl)d;
double value = (double)baseValue;
double minOffsetX = 0.0;
double maxOffsetX = Math.Max(0.0, c.unScaledExtent.Width - c.constrainedContentViewportWidth);
value = Math.Min(Math.Max(value, minOffsetX), maxOffsetX);
return value;
}
/// <summary>
/// Event raised when the 'ContentOffsetY' property has changed value.
/// </summary>
private static void ContentOffsetY_PropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
ZoomAndPanControl c = (ZoomAndPanControl)o;
c.UpdateTranslationY();
if (!c.disableContentFocusSync)
{
//
// Normally want to automatically update content focus when content offset changes.
// Although this is disabled using 'disableContentFocusSync' when content offset changes due to in-progress zooming.
//
c.UpdateContentZoomFocusY();
}
if (c.ContentOffsetYChanged != null)
{
//
// Raise an event to let users of the control know that the content offset has changed.
//
c.ContentOffsetYChanged(c, EventArgs.Empty);
}
if (!c.disableScrollOffsetSync && c.scrollOwner != null)
{
//
// Notify the owning ScrollViewer that the scrollbar offsets should be updated.
//
c.scrollOwner.InvalidateScrollInfo();
}
}
/// <summary>
/// Method called to clamp the 'ContentOffsetY' value to its valid range.
/// </summary>
private static object ContentOffsetY_Coerce(DependencyObject d, object baseValue)
{
ZoomAndPanControl c = (ZoomAndPanControl)d;
double value = (double)baseValue;
double minOffsetY = 0.0;
double maxOffsetY = Math.Max(0.0, c.unScaledExtent.Height - c.constrainedContentViewportHeight);
value = Math.Min(Math.Max(value, minOffsetY), maxOffsetY);
return value;
}
/// <summary>
/// Reset the viewport zoom focus to the center of the viewport.
/// </summary>
private void ResetViewportZoomFocus()
{
ViewportZoomFocusX = ViewportWidth / 2;
ViewportZoomFocusY = ViewportHeight / 2;
}
/// <summary>
/// Update the viewport size from the specified size.
/// </summary>
private void UpdateViewportSize(Size newSize)
{
if (viewport == newSize)
{
//
// The viewport is already the specified size.
//
return;
}
viewport = newSize;
//
// Update the viewport size in content coordinates.
//
UpdateContentViewportSize();
//
// Initialize the content zoom focus point.
//
UpdateContentZoomFocusX();
UpdateContentZoomFocusY();
//
// Reset the viewport zoom focus to the center of the viewport.
//
ResetViewportZoomFocus();
//
// Update content offset from itself when the size of the viewport changes.
// This ensures that the content offset remains properly clamped to its valid range.
//
this.ContentOffsetX = this.ContentOffsetX;
this.ContentOffsetY = this.ContentOffsetY;
if (scrollOwner != null)
{
//
// Tell that owning ScrollViewer that scrollbar data has changed.
//
scrollOwner.InvalidateScrollInfo();
}
}
/// <summary>
/// Update the size of the viewport in content coordinates after the viewport size or 'ContentScale' has changed.
/// </summary>
private void UpdateContentViewportSize()
{
ContentViewportWidth = ViewportWidth / ContentScale;
ContentViewportHeight = ViewportHeight / ContentScale;
constrainedContentViewportWidth = Math.Min(ContentViewportWidth, unScaledExtent.Width);
constrainedContentViewportHeight = Math.Min(ContentViewportHeight, unScaledExtent.Height);
UpdateTranslationX();
UpdateTranslationY();
}
/// <summary>
/// Update the X coordinate of the translation transformation.
/// </summary>
private void UpdateTranslationX()
{
if (this.contentOffsetTransform != null)
{
double scaledContentWidth = this.unScaledExtent.Width * this.ContentScale;
if (scaledContentWidth < this.ViewportWidth)
{
//
// When the content can fit entirely within the viewport, center it.
//
this.contentOffsetTransform.X = (this.ContentViewportWidth - this.unScaledExtent.Width) / 2;
}
else
{
this.contentOffsetTransform.X = -this.ContentOffsetX;
}
}
}
/// <summary>
/// Update the Y coordinate of the translation transformation.
/// </summary>
private void UpdateTranslationY()
{
if (this.contentOffsetTransform != null)
{
double scaledContentHeight = this.unScaledExtent.Height * this.ContentScale;
if (scaledContentHeight < this.ViewportHeight)
{
//
// When the content can fit entirely within the viewport, center it.
//
this.contentOffsetTransform.Y = (this.ContentViewportHeight - this.unScaledExtent.Height) / 2;
}
else
{
this.contentOffsetTransform.Y = -this.ContentOffsetY;
}
}
}
/// <summary>
/// Update the X coordinate of the zoom focus point in content coordinates.
/// </summary>
private void UpdateContentZoomFocusX()
{
ContentZoomFocusX = ContentOffsetX + (constrainedContentViewportWidth / 2);
}
/// <summary>
/// Update the Y coordinate of the zoom focus point in content coordinates.
/// </summary>
private void UpdateContentZoomFocusY()
{
ContentZoomFocusY = ContentOffsetY + (constrainedContentViewportHeight / 2);
}
/// <summary>
/// Measure the control and it's children.
/// </summary>
protected override Size MeasureOverride(Size constraint)
{
Size infiniteSize = new Size(double.PositiveInfinity, double.PositiveInfinity);
Size childSize = base.MeasureOverride(infiniteSize);
if (childSize != unScaledExtent)
{
//
// Use the size of the child as the un-scaled extent content.
//
unScaledExtent = childSize;
if (scrollOwner != null)
{
scrollOwner.InvalidateScrollInfo();
}
}
//
// Update the size of the viewport onto the content based on the passed in 'constraint'.
//
UpdateViewportSize(constraint);
double width = constraint.Width;
double height = constraint.Height;
if (double.IsInfinity(width))
{
//
// Make sure we don't return infinity!
//
width = childSize.Width;
}
if (double.IsInfinity(height))
{
//
// Make sure we don't return infinity!
//
height = childSize.Height;
}
UpdateTranslationX();
UpdateTranslationY();
return new Size(width, height);
}
/// <summary>
/// Arrange the control and it's children.
/// </summary>
protected override Size ArrangeOverride(Size arrangeBounds)
{
Size size = base.ArrangeOverride(this.DesiredSize);
if (content.DesiredSize != unScaledExtent)
{
//