context: #if !UseMapBoxAsMapImage
  // Copyright 2005, 2006 - Morten Nielsen (www.iter.dk)
  //
  // 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 
  
  using System;
  using System.ComponentModel;
  using System.Diagnostics;
  using System.Drawing;
  using System.Windows.Forms;
  using GeoAPI.Geometries;
  using SharpMap.Data;
  using SharpMap.Layers;
  using Point = GeoAPI.Geometries.Coordinate;
  using Common.Logging;
  
  namespace SharpMap.Forms
  {
      /// <summary>
      /// MapImage Class - MapImage control for Windows forms
      /// </summary>
      /// <remarks>
      /// The MapImage control adds basic functionality to a Windows Form, such as dynamic pan, zoom and data query.
      /// </remarks>
      [DesignTimeVisible(true)]
      [Obsolete("Use MapBox instead, MapImage will be removed after 1.0 release")]
      public class MapImage : PictureBox
      {
          static ILog logger = LogManager.GetLogger(typeof(MapImage));
          #region Tools enum
  
          /// <summary>
          /// Map tools enumeration
          /// </summary>
          public enum Tools
          {
              /// <summary>
              /// Pan
              /// </summary>
              Pan,
              /// <summary>
              /// Zoom in
              /// </summary>
              ZoomIn,
              /// <summary>
              /// Zoom out
              /// </summary>
              ZoomOut,
              /// <summary>
              /// Query tool
              /// </summary>
              Query,
              /// <summary>
              /// Pan on drag, Query on click
              /// </summary>
              PanOrQuery,
              /// <summary>
              /// No active tool
              /// </summary>
              None
          }
  
          #endregion
  
          private Tools _activetool;
          private double _fineZoomFactor = 10;
          private bool _isCtrlPressed;
          private Map _map;
          private int _queryLayerIndex;
  
          private double _wheelZoomMagnitude = 2;
          private System.Drawing.Point _mousedrag;
          private bool _mousedragging;
          private Image _mousedragImg;
          private bool _panOnClick;
          private bool _zoomOnDblClick;
          private Image _dragImg1, _dragImg2, _dragImgSupp;
  
          private Bitmap _staticMap;
          private Bitmap _variableMap;
  
          private bool _panOrQueryIsPan;
          private bool _zoomToPointer = false;
  
          /// <summary>
          /// Initializes a new map
          /// </summary>
          public MapImage()
          {
              _map = new Map(Size);
              _activetool = Tools.None;
              base.MouseMove += new System.Windows.Forms.MouseEventHandler(MapImage_MouseMove);
              base.MouseUp += new System.Windows.Forms.MouseEventHandler(MapImage_MouseUp);
              base.MouseDown += new System.Windows.Forms.MouseEventHandler(MapImage_MouseDown);
              base.MouseWheel += new System.Windows.Forms.MouseEventHandler(MapImage_Wheel);
              base.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(MapImage_DblClick);
              VariableLayerCollection.VariableLayerCollectionRequery += this.VariableLayersRequery;
              Cursor = Cursors.Cross;
              DoubleBuffered = true;
          }
  
          protected override void Dispose(bool disposing)        {            VariableLayerCollection.VariableLayerCollectionRequery -= this.VariableLayersRequery;            base.Dispose(disposing);        }        [Description("The amount which a single movement of the mouse wheel zooms by.")]
          [DefaultValue(-2)]
          [Category("Behavior")]
          public double WheelZoomMagnitude
          {
              get { return _wheelZoomMagnitude; }
              set { _wheelZoomMagnitude = value; }
          }
  
          [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>
          /// 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(false)]
          [Category("Behavior")]
          public bool ZoomToPointer
          {
              get { return _zoomToPointer; }
              set { _zoomToPointer = value; }
          }
  
          /// <summary>
          /// Map reference
          /// </summary>
          [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
          public Map Map
          {
              get { return _map; }
              set
              {
                  _map = value;
                  if (_map != null)
                  {
                      VariableLayerCollection.VariableLayerCollectionRequery += new VariableLayerCollectionRequeryHandler(VariableLayersRequery);
                      Refresh();
                  }
              }
          }
  
          /// <summary>
          /// Handles need to requery of variable layers
          /// </summary>
          /// <param name="sender"></param>
          /// <param name="e"></param>
          private void VariableLayersRequery(object sender, EventArgs e)
          {
              lock (_map)
              {
                  if (_mousedragging) return;
                  _variableMap = GetMap(_map.VariableLayers, LayerCollectionType.Variable);
              }
              UpdateImage();
          }
  
          /// <summary>
          /// Gets or sets the index of the active query layer 
          /// </summary>
          public int QueryLayerIndex
          {
              get { return _queryLayerIndex; }
              set { _queryLayerIndex = value; }
          }
  
  
          /// <summary>
          /// Sets the active map tool
          /// </summary>
          public Tools ActiveTool
          {
              get { return _activetool; }
              set
              {
                  bool fireevent = (value != _activetool);
                  _activetool = value;
                  if (value == Tools.Pan)
                      Cursor = Cursors.Hand;
                  else
                      Cursor = Cursors.Cross;
                  if (fireevent)
                      if (ActiveToolChanged != null)
                          ActiveToolChanged(value);
  
                  // Check if settings collide
                  if (value != Tools.None && ZoomOnDblClick)
                      ZoomOnDblClick = false;
                  if (value != Tools.Pan && PanOnClick)
                      PanOnClick = false;
              }
          }
  
          ///// <summary>
          ///// Sets the direction of wheel for zoom-in operation
          ///// </summary>
          //public bool WheelZoomForward
          //{
          //    get { return _wheelZoomDirection==-1; }
          //    set { _wheelZoomDirection = (value == true) ? -1 : 1; }
          //}
  
          /// <summary>
          /// 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 "go-to-cursor-and-zoom-in-on-double-click" feature is enable or not
          /// </summary>
          [Description("Sets whether the \"go-to-cursor-and-zoom-in-on-double-click\" feature is enable or not. This only works if no tool is currently active.")]
          [DefaultValue(true)]
          [Category("Behavior")]
          public bool ZoomOnDblClick
          {
              get { return _zoomOnDblClick; }
              set { 
                  if (value)
                      ActiveTool = Tools.None;
                  _zoomOnDblClick = value;
              }
          }
  
          /// <summary>
          /// Refreshes the map
          /// </summary>
          public override void Refresh()
          {
              if (_map != null)
              {
                  _map.Size = Size;
                  _staticMap = GetMap(_map.Layers, LayerCollectionType.Static);
                  _variableMap = GetMap(_map.VariableLayers, LayerCollectionType.Variable);
  
                  UpdateImage();
                  base.Refresh();
                  if (MapRefreshed != null)
                      MapRefreshed(this, null);
              }
          }
  
          private Bitmap GetMap(LayerCollection layers, LayerCollectionType layerCollectionType)
          {
  
gt:             if ((layers == null || layers.Count == 0))