forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapBox.cs
More file actions
2729 lines (2341 loc) · 97.3 KB
/
MapBox.cs
File metadata and controls
2729 lines (2341 loc) · 97.3 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
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
// Copyright 2008-, SharpMapTeam
//
// This file is part of SharpMap.
// SharpMap is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SharpMap is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public License
// along with SharpMap; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#define EnableMetafileClipboardSupport
/*
* Note:
*
* If you want to use MapBox control along with MapImage controls
* you have to define the compile time constant 'UseMapBox' in the
* properties dialog of this project. As a result you will have the
* MapImage control and the MapBox control included in your SharpMap.UI
* assembly.
*
* If you want to use MapBox control as a replacement of MapImage
* control you have to define the compile time constant 'UseMapBoxAsMapImage'.
* in the * properties dialog of this project. As a result you will have a
* MapImage control in your SharpMap.UI assembly which is actually this
* MapBox control.
*
* If you don't define any of the two compile time constants this control
* is omitted.
*
* FObermaier
*/
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using GeoAPI.Geometries;
using SharpMap.Forms.Tools;
using SharpMap.Layers;
using System.Drawing.Imaging;
using IGeometry = GeoAPI.Geometries.IGeometry;
using System.Threading;
using Common.Logging;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using SharpMap.Forms.ImageGenerator;
namespace SharpMap.Forms
{
/// <summary>
/// MapBox Class - MapBox control for Windows forms
/// </summary>
/// <remarks>
/// The ExtendedMapImage control adds more than basic functionality to a Windows Form, such as dynamic pan, widow zoom and data query.
/// </remarks>
[DesignTimeVisible(true)]
// ReSharper disable once PartialTypeWithSinglePart
public partial class MapBox : Control
{
/// <summary>
/// A tolerance value
/// </summary>
private const double PrecisionTolerance = 0.00000001;
/// <summary>
/// The map image generation function to use when creating new <see cref="MapBox"/> instances
/// </summary>
private static Func<MapBox, ProgressBar, IMapBoxImageRenderer> _mapImageGeneratorFunction;
/// <summary>
/// Gets or sets the map image generation function to assign when creating new MapBox instances.
/// </summary>
public static Func<MapBox, ProgressBar, IMapBoxImageRenderer> MapImageGeneratorFunction
{
get => _mapImageGeneratorFunction ?? LegacyMapImageGenerator;
set => _mapImageGeneratorFunction = value;
}
/// <summary>
/// Creates a <see cref="IMapBoxImageRenderer"/> that mimics legacy image generation for <see cref="MapBox"/> control.
/// </summary>
/// <param name="mapBox">The map control</param>
/// <param name="progressBar">The progress bar</param>
/// <returns>An image generator for <see cref="MapBox"/> control</returns>
public static IMapBoxImageRenderer LegacyMapImageGenerator(MapBox mapBox, ProgressBar progressBar)
{
return new LegacyMapBoxImageGenerator(mapBox, progressBar);
}
/// <summary>
/// Creates a <see cref="IMapBoxImageRenderer"/> for <see cref="MapBox"/> control that works on a list of layers.
/// </summary>
/// <param name="mapBox">The map control</param>
/// <param name="progressBar">The progress bar</param>
/// <returns>An image generator for <see cref="MapBox"/> control</returns>
public static IMapBoxImageRenderer LayerListImageGenerator(MapBox mapBox, ProgressBar progressBar)
{
return new LayerListImageRenderer(mapBox, progressBar);
}
private static readonly ILog _logger = LogManager.GetLogger(typeof (MapBox));
static MapBox() { Map.Configure(); }
#region PreviewModes enumerator
// ReSharper disable UnusedMember.Local
/// <summary>
/// Preview modes
/// </summary>
[Obsolete("Not used anywhere")]
public enum PreviewModes
{
/// <summary>
/// Best preview mode
/// </summary>
Best,
/// <summary>
/// Fast preview mode
/// </summary>
Fast
}
#endregion
#region Position enumerators
/// <summary>
/// Horizontal alignment enumeration
/// </summary>
private enum XPosition
{
Center = 0,
Right = 1,
Left = -1
}
/// <summary>
/// Vertical alignment enumeration
/// </summary>
private enum YPosition
{
Center = 0,
Top = -1,
Bottom = 1
}
// ReSharper restore UnusedMember.Local
#endregion
#region Tools enumerator
/// <summary>
/// Map tools enumeration
/// </summary>
public enum Tools
{
/// <summary>
/// Pan
/// </summary>
Pan,
/// <summary>
/// Zoom in
/// </summary>
ZoomIn,
/// <summary>
/// Zoom out
/// </summary>
ZoomOut,
/// <summary>
/// Query bounding boxes for intersection
/// </summary>
QueryBox,
/// <summary>
/// Query tool
/// </summary>
[Obsolete ("Use QueryBox")]
Query = QueryBox,
/// <summary>
/// Attempt true intersection query on geometry
/// </summary>
QueryPoint,
/// <summary>
/// Attempt true intersection query on geometry
/// </summary>
[Obsolete ("Use QueryPoint")]
QueryGeometry = QueryPoint,
///// <summary>
///// Attempt true intersection query on polygonal geometry
///// </summary>
//QueryPolygon,
/// <summary>
/// Zoom window tool
/// </summary>
ZoomWindow,
/// <summary>
/// Define Point on Map
/// </summary>
DrawPoint,
/// <summary>
/// Define Line on Map
/// </summary>
DrawLine,
/// <summary>
/// Define Polygon on Map
/// </summary>
DrawPolygon,
/// <summary>
/// No active tool
/// </summary>
None,
/// <summary>
/// Custom tool, implementing <see cref="IMapTool"/>
/// </summary>
Custom
}
/// <summary>
/// Enumeration of map query types
/// </summary>
public enum MapQueryType
{
/// <summary>
/// Layer set in QueryLayerIndex is the only layers Queried (Default)
/// </summary>
LayerByIndex,
/// <summary>
/// All layers are queried
/// </summary>
AllLayers,
/// <summary>
/// All visible layers are queried
/// </summary>
VisibleLayers,
/// <summary>
/// Visible layers are queried from Top and down until a layer with an intersecting feature is found
/// </summary>
TopMostLayer
};
#endregion
#region Events
/// <summary>
/// MouseEventtype fired from the MapImage control
/// </summary>
/// <param name="worldPos"></param>
/// <param name="imagePos"></param>
public delegate void MouseEventHandler(Coordinate worldPos, MouseEventArgs imagePos);
/// <summary>
/// Fires when mouse moves over the map
/// </summary>
public new event MouseEventHandler MouseMove;
/// <summary>
/// Fires when map received a mouseclick
/// </summary>
public new event MouseEventHandler MouseDown;
/// <summary>
/// Fires when mouse is released
/// </summary>
public new event MouseEventHandler MouseUp;
/// <summary>
/// Fired when mouse is dragging
/// </summary>
public event MouseEventHandler MouseDrag;
/// <summary>
/// Fired when the map has been refreshed
/// </summary>
public event EventHandler MapRefreshed;
/// <summary>
/// Fired when the map is about to change
/// </summary>
public event CancelEventHandler MapChanging;
/// <summary>
/// Fired when the map has been changed
/// </summary>
public event EventHandler MapChanged;
/// <summary>
/// Eventtype fired when the zoom was or are being changed
/// </summary>
/// <param name="zoom"></param>
public delegate void MapZoomHandler(double zoom);
/// <summary>
/// Fired when the zoom value has changed
/// </summary>
public event MapZoomHandler MapZoomChanged;
/// <summary>
/// Fired when the map is being zoomed
/// </summary>
public event MapZoomHandler MapZooming;
/// <summary>
/// Eventtype fired when the map is queried
/// </summary>
/// <param name="data"></param>
public delegate void MapQueryHandler(Data.FeatureDataTable data);
/// <summary>
/// Fired when the map is queried
///
/// Will be fired one time for each layer selected for query depending on QueryLayerIndex and QuerySettings
/// </summary>
public event MapQueryHandler MapQueried;
/// <summary>
/// Fired when Map is Queried before the first MapQueried event is fired for that query
/// </summary>
public event EventHandler MapQueryStarted;
/// <summary>
/// Fired when Map is Queried after the last MapQueried event is fired for that query
/// </summary>
public event EventHandler MapQueryDone;
/// <summary>
/// Eventtype fired when the center has changed
/// </summary>
/// <param name="center"></param>
public delegate void MapCenterChangedHandler(Coordinate center);
/// <summary>
/// Fired when the center of the map has changed
/// </summary>
public event MapCenterChangedHandler MapCenterChanged;
/// <summary>
/// Eventtype fired befor the active map tool change
/// </summary>
/// <param name="toolPre">pre-tool</param>
/// <param name="toolNew">new tool</param>
/// <param name="cea">a cancel indicator</param>
public delegate void ActiveToolChangingHandler(Tools toolPre, Tools toolNew, CancelEventArgs cea);
/// <summary>
/// Fired befor the active map tool change
/// </summary>
public event ActiveToolChangingHandler ActiveToolChanging;
/// <summary>
/// Eventtype fired when the map tool is changed
/// </summary>
/// <param name="tool"></param>
public delegate void ActiveToolChangedHandler(Tools tool);
/// <summary>
/// Fired when the active map tool has changed
/// </summary>
public event ActiveToolChangedHandler ActiveToolChanged;
/// <summary>
/// Eventtype fired when a new geometry has been defined
/// </summary>
/// <param name="geometry">New Geometry</param>
public delegate void GeometryDefinedHandler(IGeometry geometry);
/// <summary>
/// Fired when a new polygon has been defined
/// </summary>
public event GeometryDefinedHandler GeometryDefined;
#endregion
private readonly IMapBoxImageRenderer _miRenderer;
private static int m_defaultColorIndex;
private static readonly Color[] _defaultColors ={
Color.DarkRed,
Color.DarkGreen,
Color.DarkBlue,
Color.Orange,
Color.Cyan,
Color.Black,
Color.Purple,
Color.Yellow,
Color.LightBlue,
Color.Fuchsia
};
/*
private const float MinDragScalingBeforeRegen = 0.3333f;
private const float MaxDragScalingBeforeRegen = 3f;
*/
private readonly ProgressBar _progressBar;
#if DEBUG
private readonly Stopwatch _watch = new Stopwatch();
#endif
//private bool m_IsCtrlPressed;
private IMapTool _currentTool;
private double _wheelZoomMagnitude = -2;
private Tools _activeTool;
private double _fineZoomFactor = 10;
private Map _map;
private int _queryLayerIndex;
private Point _dragStartPoint;
private Point _dragEndPoint;
//private Bitmap _dragImage;
private Rectangle _rectangle = Rectangle.Empty;
private bool _dragging;
private readonly SolidBrush _rectangleBrush = new SolidBrush(Color.FromArgb(210, 244, 244, 244));
private readonly Pen _rectanglePen = new Pen(Color.FromArgb(244, 244, 244), 1);
private float _scaling;
//private Image _image = new Bitmap(1, 1);
//private Image _imageBackground = new Bitmap(1, 1);
//private Image _imageStatic = new Bitmap(1, 1);
//private Image _imageVariable = new Bitmap(1, 1);
//private Envelope _imageEnvelope = new Envelope(0, 1, 0, 1);
//private int _imageGeneration;
private readonly object _mapLocker = new object();
private int _needToRefreshAfterWheel;
[Obsolete]
private PreviewModes _previewMode;
//private bool _isRefreshing;
private List<Coordinate> _pointArray = new List<Coordinate>();
private bool _showProgress;
private bool _zoomToPointer = true;
private bool _setActiveToolNoneDuringRedraw;
private bool _shiftButtonDragRectangleZoom = true;
private bool _focusOnHover;
private bool _panOnClick = true;
private float _queryGrowFactor = 5f;
private MapQueryType _mapQueryMode = MapQueryType.LayerByIndex;
private readonly IMessageFilter _mousePreviewFilter;
/// <summary>
/// Assigns a random color to a vector layers style
/// </summary>
/// <param name="layer"></param>
public static void RandomizeLayerColors(VectorLayer layer)
{
layer.Style.EnableOutline = true;
layer.Style.Fill = new SolidBrush(Color.FromArgb(80, _defaultColors[m_defaultColorIndex%_defaultColors.Length]));
layer.Style.Outline =
new Pen(
Color.FromArgb(100,
_defaultColors[
(m_defaultColorIndex + ((int) (_defaultColors.Length*0.5)))%_defaultColors.Length]),
1f);
m_defaultColorIndex++;
}
/// <summary>
/// Gets a value indicating the name of the Image generator
/// </summary>
[Description("The name of the image renderer used")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string ImageRendererName
{
get => MapImageGeneratorFunction.GetType().Name;
}
/// <summary>
/// Gets a value indicating the image renderer for this MapBox
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public IMapBoxImageRenderer ImageRenderer { get => _miRenderer; }
/// <summary>
/// Gets or sets a value on whether to report progress of map generation
/// </summary>
[Description("Define if the progress Bar is shown")]
[Category("Appearance")]
public bool ShowProgressUpdate
{
get { return _showProgress; }
set
{
_showProgress = value;
_progressBar.Visible = _showProgress;
}
}
/// <summary>
/// Gets or sets whether the "go-to-cursor-on-click" feature is enabled or not (even if enabled it works only if the active tool is Pan)
/// </summary>
[Description(
"Sets whether the \"go-to-cursor-on-click\" feature is enabled or not (even if enabled it works only if the active tool is Pan)"
)]
[DefaultValue(true)]
[Category("Behavior")]
public bool PanOnClick
{
get { return _panOnClick; }
set
{
ActiveTool = Tools.Pan;
_panOnClick = value;
}
}
/// <summary>
/// Sets whether the mapcontrol should automatically grab focus when mouse is hovering the control
/// </summary>
[Description("Sets whether the mapcontrol should automatically grab focus when mouse is hovering the control")]
[DefaultValue(false)]
[Category("Behavior")]
public bool TakeFocusOnHover
{
get { return _focusOnHover; }
set { _focusOnHover = value; }
}
/// <summary>
/// Sets whether the mouse wheel should zoom to the pointer location
/// </summary>
[Description("Sets whether the mouse wheel should zoom to the pointer location")]
[DefaultValue(true)]
[Category("Behavior")]
public bool ZoomToPointer
{
get { return _zoomToPointer; }
set { _zoomToPointer = value; }
}
/// <summary>
/// Sets ActiveTool to None (and changing cursor) while redrawing the map
/// </summary>
[Description("Sets ActiveTool to None (and changing cursor) while redrawing the map")]
[DefaultValue(false)]
[Category("Behavior")]
public bool SetToolsNoneWhileRedrawing
{
get { return _setActiveToolNoneDuringRedraw; }
set { _setActiveToolNoneDuringRedraw = value; }
}
/// <summary>
/// Gets or sets the number of pixels by which a bounding box around the query point should be "grown" prior to perform the query
/// </summary>
/// <remarks>Does not apply when querying against boxes.</remarks>
[Description(
"Gets or sets the number of pixels by which a bounding box around the query point should be \"grown\" prior to perform the query"
)]
[DefaultValue(5)]
[Category("Behavior")]
public float QueryGrowFactor
{
get { return _queryGrowFactor; }
set
{
if (value < 0) value = 0;
//if (value > 10)
_queryGrowFactor = value;
}
}
/// <summary>
/// Gets or sets the value of the back color for the selection rectangle
/// </summary>
[Description("The color of selecting rectangle.")]
[Category("Appearance")]
public Color SelectionBackColor
{
get { return _rectangleBrush.Color; }
set
{
//if (value != m_RectangleBrush.Color)
_rectangleBrush.Color = value;
}
}
/// <summary>
/// Gets or sets the value of the border color for the selection rectangle
/// </summary>
[Description("The color of selectiong rectangle frame.")]
[Category("Appearance")]
public Color SelectionForeColor
{
get { return _rectanglePen.Color; }
set
{
//if (value != m_RectanglePen.Color)
_rectanglePen.Color = value;
}
}
/// <summary>
/// Gets the current map image
/// </summary>
[Description("The map image currently visualized.")]
[Category("Appearance")]
public Image Image
{
get
{
return _miRenderer.Image;
//GetImagesAsyncEnd(null);
//return _image;
}
}
/// <summary>
/// Gets or sets the amount which a single movement of the mouse wheel zooms by.
/// </summary>
[Description(
"The amount which a single movement of the mouse wheel zooms by. (Negative values are similar as OpenLayers/Google, positive are like ArcMap"
)]
[DefaultValue(-2)]
[Category("Behavior")]
public double WheelZoomMagnitude
{
get { return _wheelZoomMagnitude; }
set { _wheelZoomMagnitude = value; }
}
/// <summary>
/// Gets or sets the mode used to create preview image while panning or zooming.
/// </summary>
[Description("Mode used to create preview image while panning or zooming.")]
#pragma warning disable
[DefaultValue(PreviewModes.Best)]
#pragma warning restore
[Category("Behavior")]
[Obsolete("Not used anywhere")]
public PreviewModes PreviewMode
{
get { return _previewMode; }
set
{
if (!_dragging)
_previewMode = value;
}
}
/// <summary>
/// Gets or sets a value indicating the amount which the WheelZoomMagnitude is divided by
/// when the Control key is pressed. A number greater than 1 decreases
/// the zoom, and less than 1 increases it. A negative number reverses it.
/// </summary>
[Description("The amount which the WheelZoomMagnitude is divided by " +
"when the Control key is pressed. A number greater than 1 decreases " +
"the zoom, and less than 1 increases it. A negative number reverses it.")]
[DefaultValue(10)]
[Category("Behavior")]
public double FineZoomFactor
{
get { return _fineZoomFactor; }
set { _fineZoomFactor = value; }
}
/// <summary>
/// Gets or sets a value that enables shortcut to rectangle-zoom by holding down shift-button and drag rectangle
/// </summary>
[Description("Enables shortcut to rectangle-zoom by holding down shift-button and drag rectangle")]
[DefaultValue(true)]
[Category("Behavior")]
public bool EnableShiftButtonDragRectangleZoom
{
get { return _shiftButtonDragRectangleZoom; }
set { _shiftButtonDragRectangleZoom = value; }
}
/// <summary>
/// Gets or sets the map reference
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Map Map
{
get { return _map; }
set
{
if (value != _map)
{
var cea = new CancelEventArgs(false);
OnMapChanging(cea);
if (cea.Cancel) return;
_map = value;
using(var g = Graphics.FromImage(Image))
g.Clear(_map?.BackColor ?? SystemColors.Control);
OnMapChanged(EventArgs.Empty);
}
}
}
/// <summary>
/// Gets or sets the index of the active query layer
/// </summary>
public int QueryLayerIndex
{
get { return _queryLayerIndex; }
set { _queryLayerIndex = value; }
}
/// <summary>
/// Gets or sets the mapquerying mode
/// </summary>
public MapQueryType MapQueryMode
{
get { return _mapQueryMode; }
set { _mapQueryMode = value; }
}
/// <summary>
/// Sets the active map tool
/// </summary>
public Tools ActiveTool
{
get { return _activeTool; }
set
{
var cea = new CancelEventArgs(false);
OnActiveToolChanging(ActiveTool, value, cea);
if (cea.Cancel)
{
if (CustomTool != null)
CustomTool.Enabled = true;
return;
}
_activeTool = value;
SetCursor();
_pointArray = null;
OnActiveToolChanged(value);
}
}
/// <summary>
/// Event invoker for the <see cref="ActiveToolChanging"/>
/// </summary>
/// <param name="toolPre">pre-tool</param>
/// <param name="toolNew">new tool</param>
/// <param name="cea">a cancel indicator</param>
protected virtual void OnActiveToolChanging(Tools toolPre, Tools toolNew, CancelEventArgs cea)
{
if (CustomTool != null)
CustomTool.Enabled = false;
var handler = ActiveToolChanging;
if (handler != null)
handler(toolPre, toolNew, cea);
}
/// <summary>
/// Event invoker for the <see cref="ActiveToolChanged"/> event
/// </summary>
/// <param name="activeTool">The tool</param>
protected virtual void OnActiveToolChanged(Tools activeTool)
{
if (CustomTool != null)
CustomTool.Enabled = true;
var handler = ActiveToolChanged;
if (handler != null)
handler(activeTool);
}
/// <summary>
/// Gets or sets a value indicating the currently active custom tool
/// </summary>
public IMapTool CustomTool
{
get { return _currentTool; }
set
{
if (value == _currentTool)
return;
var raiseActiveToolChanged = ActiveTool == Tools.Custom && value != null;
_currentTool = value;
ActiveTool = _currentTool != null
? Tools.Custom
: Tools.None;
if (_currentTool != null)
_currentTool.Map = _map;
if (raiseActiveToolChanged)
OnActiveToolChanged(ActiveTool);
}
}
#pragma warning disable 1587
#if DEBUG
/// <summary>
/// TimeSpan for refreshing maps
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public TimeSpan LastRefreshTime { get; set; }
#endif
/// <summary>
/// Initializes a new map
/// </summary>
public MapBox()
#pragma warning restore 1587
{
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint, true);
base.DoubleBuffered = true;
_map = new Map(ClientSize);
//_map.VariableLayers.VariableLayerCollectionRequery += HandleVariableLayersRequery;
//_map.RefreshNeeded += HandleRefreshNeeded;
//_map.MapNewTileAvaliable += HandleMapNewTileAvaliable;
_progressBar = new ProgressBar
{
Style = ProgressBarStyle.Marquee,
Location = new Point(2, 2),
Size = new Size(50, 10)
};
Controls.Add(_progressBar);
_miRenderer = MapImageGeneratorFunction(this, _progressBar); // new LayerListImageGenerator(this, _progressBar);
_activeTool = Tools.None;
LostFocus += HandleMapBoxLostFocus;
_progressBar.Visible = ShowProgressUpdate;
_mousePreviewFilter = new MouseWheelGrabber(this);
Application.AddMessageFilter(_mousePreviewFilter);
}
/// <inheritdoc/>
protected override void OnSizeChanged(EventArgs e)
{
if (Map != null)
{
if (Size != Map.Size)
{
Map.Size = Size;
Refresh();
}
}
base.OnSizeChanged(e);
}
/// <summary>
/// Dispose method
/// </summary>
/// <param name="disposing">A parameter indicating that this method is called from either a call to <see cref="IDisposable.Dispose()"/> (<c>true</c>)
/// or the finalizer (<c>false</c>)</param>
protected override void Dispose(bool disposing)
{
if (_miRenderer.IsDisposed || IsDisposed)
return;
LostFocus -= HandleMapBoxLostFocus;
if (_mousePreviewFilter != null)
Application.RemoveMessageFilter(_mousePreviewFilter);
if (_map != null)
{
// special handling to prevent spurious VariableLayers events
_map.VariableLayers.Interval = 0;
//_map.VariableLayers.VariableLayerCollectionRequery -= HandleVariableLayersRequery;
//_map.MapNewTileAvaliable -= HandleMapNewTileAvaliable;
//_map.RefreshNeeded -= HandleRefreshNeeded;
}
lock (_mapLocker)
{
_map.VariableLayers.Pause = true;
_miRenderer.Dispose();
_map = null;
//if (_imageStatic != null)
//{
// _imageStatic.Dispose();
// _imageStatic = null;
//}
//if (_imageBackground != null)
//{
// _imageBackground.Dispose();
// _imageBackground = null;
//}
//if (_imageVariable != null)
//{
// _imageVariable.Dispose();
// _imageVariable = null;
//}
//if (_image != null)
//{
// _image.Dispose();
// _image = null;
//}
//if (_dragImage != null)
//{
// _dragImage.Dispose();
// _dragImage = null;
//}
if (_rectanglePen != null)
{
_rectanglePen.Dispose();
}
if (_rectangleBrush != null)
{
_rectangleBrush.Dispose();
}
base.Dispose(disposing);
}
}
#region event handling
/// <summary>
/// Handles LostFocus event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void HandleMapBoxLostFocus(object sender, EventArgs e)
{
if (!_dragging) return;
_dragging = false;
Invalidate(ClientRectangle);
}
///// <summary>
///// Handles need to requery of variable layers
///// </summary>
///// <param name="sender"></param>
///// <param name="e"></param>
//private void HandleVariableLayersRequery(object sender, EventArgs e)
//{
// if (IsDisposed || _isDisposed)
// return;
// Image oldRef;
// lock (_mapLocker)
// {
// if (_dragging) return;
// oldRef = _imageVariable;
// _imageVariable = GetMap(_map, _map.VariableLayers, LayerCollectionType.Variable, _map.Envelope);
// }
// UpdateImage(false);
// if (oldRef != null)
// oldRef.Dispose();
// Invalidate();
// Application.DoEvents();
//}
// private void HandleMapNewTileAvaliable(ITileAsyncLayer sender, Envelope box, Bitmap bm, int sourceWidth,
// int sourceHeight, ImageAttributes imageAttributes)
// {
// lock (_backgroundImagesLocker)
// {
// try
// {