1 /* Copyright (c) 2006 MetaCarta, Inc., published under the BSD license.
 
   2  * See http://svn.openlayers.org/trunk/openlayers/license.txt for the full
 
   3  * text of the license. */
 
   4 // @require: OpenLayers/Control.js
 
   5 OpenLayers.Control.MouseToolbar = Class.create();
 
   6 OpenLayers.Control.MouseToolbar.X = 6;
 
   7 OpenLayers.Control.MouseToolbar.Y = 300;
 
   8 OpenLayers.Control.MouseToolbar.prototype = 
 
   9   Object.extend( new OpenLayers.Control(), {
 
  15     direction: "vertical",
 
  17     initialize: function(position, direction) {
 
  18         OpenLayers.Control.prototype.initialize.apply(this, arguments);
 
  19         this.position = new OpenLayers.Pixel(OpenLayers.Control.MouseToolbar.X,
 
  20                                              OpenLayers.Control.MouseToolbar.Y);
 
  22             this.position = position;
 
  25             this.direction = direction; 
 
  27         this.measureDivs = [];
 
  31         OpenLayers.Control.prototype.draw.apply(this, arguments);
 
  32         this.buttons = new Object();
 
  33         this.map.events.register( "dblclick", this, this.defaultDblClick );
 
  34         this.map.events.register( "mousedown", this, this.defaultMouseDown );
 
  35         this.map.events.register( "mouseup", this, this.defaultMouseUp );
 
  36         this.map.events.register( "mousemove", this, this.defaultMouseMove );
 
  37         this.map.events.register( "mouseout", this, this.defaultMouseOut );
 
  38         var sz = new OpenLayers.Size(28,28);
 
  39         var centered = this.position;
 
  40         this._addButton("zoombox", "drag-rectangle-off.png", "drag-rectangle-on.png", centered, sz, "Shift->Drag to zoom to area");
 
  41         centered = centered.add((this.direction == "vertical" ? 0 : sz.w), (this.direction == "vertical" ? sz.h : 0));
 
  42         this._addButton("pan", "panning-hand-off.png", "panning-hand-on.png", centered, sz, "Drag the map to pan.");
 
  43         centered = centered.add((this.direction == "vertical" ? 0 : sz.w), (this.direction == "vertical" ? sz.h : 0));
 
  44         this._addButton("measure", "measuring-stick-off.png", "measuring-stick-on.png", centered, sz, "Hold alt when clicking to show distance between selected points");
 
  45         this.switchModeTo("pan");
 
  46         this.map.events.register("zoomend", this, function() { this.switchModeTo("pan"); });
 
  51     _addButton:function(id, img, activeImg, xy, sz, title) {
 
  52         var imgLocation = OpenLayers.Util.getImagesLocation() + img;
 
  53         var activeImgLocation = OpenLayers.Util.getImagesLocation() + activeImg;
 
  54         // var btn = new ol.AlphaImage("_"+id, imgLocation, xy, sz);
 
  55         var btn = OpenLayers.Util.createAlphaImageDiv(
 
  56                                     "OpenLayers_Control_MouseToolbar_" + id, 
 
  57                                     xy, sz, imgLocation, "absolute");
 
  59         //we want to add the outer div
 
  60         this.div.appendChild(btn);
 
  61         btn.imgLocation = imgLocation;
 
  62         btn.activeImgLocation = activeImgLocation;
 
  64         btn.events = new OpenLayers.Events(this, btn);
 
  65         btn.events.register("mousedown", this, this.buttonClick); 
 
  66         btn.events.register("mouseup", this, Event.stop);
 
  72         //we want to remember/reference the outer div
 
  73         this.buttons[id] = btn;
 
  77     buttonClick: function(evt) {
 
  78         if (!Event.isLeftClick(evt)) return;
 
  79         this.switchModeTo(evt.div.action);
 
  86     defaultDblClick: function (evt) {
 
  87         this.switchModeTo("pan");
 
  88         var newCenter = this.map.getLonLatFromViewPortPx( evt.xy ); 
 
  89         this.map.setCenter(newCenter, this.map.zoom + 2);
 
  95     defaultMouseDown: function (evt) {
 
  96         if (!Event.isLeftClick(evt)) return;
 
  97         this.mouseDragStart = evt.xy.copyOf();
 
  98         if (evt.shiftKey && this.mode !="zoombox") {
 
  99             this.switchModeTo("zoombox");
 
 100         } else if (evt.altKey && this.mode !="measure") {
 
 101             this.switchModeTo("measure");
 
 102         } else if (!this.mode) {
 
 103             this.switchModeTo("pan");
 
 108                 this.map.div.style.cursor = "crosshair";
 
 109                 this.zoomBox = OpenLayers.Util.createDiv('zoomBox',
 
 115                 this.zoomBox.style.backgroundColor = "white";
 
 116                 this.zoomBox.style.filter = "alpha(opacity=50)"; // IE
 
 117                 this.zoomBox.style.opacity = "0.50";
 
 118                 this.zoomBox.style.zIndex = this.map.Z_INDEX_BASE["Popup"] - 1;
 
 119                 this.map.viewPortDiv.appendChild(this.zoomBox);
 
 123                 if (this.measureStart) {
 
 124                     measureEnd = this.map.getLonLatFromViewPortPx(this.mouseDragStart);
 
 125                     distance = OpenLayers.Util.distVincenty(this.measureStart, measureEnd);
 
 126                     distance = Math.round(distance * 100) / 100;
 
 127                     distance = distance + "km";
 
 128                     this.measureStartBox = this.measureBox;
 
 130                 this.measureStart = this.map.getLonLatFromViewPortPx(this.mouseDragStart);;
 
 131                 this.measureBox = OpenLayers.Util.createDiv(null,
 
 132                                                          this.mouseDragStart.add(
 
 133                                                            -2-parseInt(this.map.layerContainerDiv.style.left),
 
 134                                                            -2-parseInt(this.map.layerContainerDiv.style.top)),
 
 138                 this.measureBox.style.width="4px";
 
 139                 this.measureBox.style.height="4px";
 
 140                 this.measureBox.style.backgroundColor="red";
 
 141                 this.measureBox.style.zIndex = this.map.Z_INDEX_BASE["Popup"] - 1;
 
 142                 this.map.layerContainerDiv.appendChild(this.measureBox);
 
 144                     this.measureBoxDistance = OpenLayers.Util.createDiv(null,
 
 145                                                          this.mouseDragStart.add(
 
 146                                                            -2-parseInt(this.map.layerContainerDiv.style.left),
 
 147                                                            2-parseInt(this.map.layerContainerDiv.style.top)),
 
 152                     this.measureBoxDistance.innerHTML = distance;
 
 153                     this.measureBoxDistance.style.zIndex = this.map.Z_INDEX_BASE["Popup"] - 1;
 
 154                     this.map.layerContainerDiv.appendChild(this.measureBoxDistance);
 
 155                     this.measureDivs.append(this.measureBoxDistance);
 
 157                 this.measureBox.style.zIndex = this.map.Z_INDEX_BASE["Popup"] - 1;
 
 158                 this.map.layerContainerDiv.appendChild(this.measureBox);
 
 159                 this.measureDivs.append(this.measureBox);
 
 162                 this.map.div.style.cursor = "move";
 
 165         document.onselectstart = function() { return false; } 
 
 169     switchModeTo: function(mode) {
 
 170         if (mode != this.mode) {
 
 172                 OpenLayers.Util.modifyAlphaImageDiv(this.buttons[this.mode], null, null, null, this.buttons[this.mode].imgLocation);
 
 174             if (this.mode == "measure" && mode != "measure") {
 
 175                 for(var i = 0; i < this.measureDivs.length; i++) {
 
 176                     if (this.measureDivs[i]) { 
 
 177                         this.map.layerContainerDiv.removeChild(this.measureDivs[i]);
 
 180                 this.measureDivs = [];
 
 181                 this.measureStart = null;
 
 184             OpenLayers.Util.modifyAlphaImageDiv(this.buttons[mode], null, null, null, this.buttons[mode].activeImgLocation);
 
 188     leaveMode: function() {
 
 189         this.switchModeTo("pan");
 
 195     defaultMouseMove: function (evt) {
 
 196         if (this.mouseDragStart != null) {
 
 199                     var deltaX = Math.abs(this.mouseDragStart.x - evt.xy.x);
 
 200                     var deltaY = Math.abs(this.mouseDragStart.y - evt.xy.y);
 
 201                     this.zoomBox.style.width = deltaX+"px";
 
 202                     this.zoomBox.style.height = deltaY+"px";
 
 203                     if (evt.xy.x < this.mouseDragStart.x) {
 
 204                         this.zoomBox.style.left = evt.xy.x+"px";
 
 206                     if (evt.xy.y < this.mouseDragStart.y) {
 
 207                         this.zoomBox.style.top = evt.xy.y+"px";
 
 211                     var deltaX = this.mouseDragStart.x - evt.xy.x;
 
 212                     var deltaY = this.mouseDragStart.y - evt.xy.y;
 
 213                     var size = this.map.getSize();
 
 214                     var newXY = new OpenLayers.Pixel(size.w / 2 + deltaX,
 
 215                                                      size.h / 2 + deltaY);
 
 216                     var newCenter = this.map.getLonLatFromViewPortPx( newXY ); 
 
 217                     this.map.setCenter(newCenter, null, true);
 
 218                     this.mouseDragStart = evt.xy.copyOf();
 
 226     defaultMouseUp: function (evt) {
 
 227         if (!Event.isLeftClick(evt)) return;
 
 230                 var start = this.map.getLonLatFromViewPortPx( this.mouseDragStart ); 
 
 231                 var end = this.map.getLonLatFromViewPortPx( evt.xy );
 
 232                 var top = Math.max(start.lat, end.lat);
 
 233                 var bottom = Math.min(start.lat, end.lat);
 
 234                 var left = Math.min(start.lon, end.lon);
 
 235                 var right = Math.max(start.lon, end.lon);
 
 236                 var bounds = new OpenLayers.Bounds(left, bottom, right, top);
 
 237                 var zoom = this.map.getZoomForExtent(bounds);
 
 238                 this.map.setCenter(new OpenLayers.LonLat(
 
 239                   (start.lon + end.lon) / 2,
 
 240                   (start.lat + end.lat) / 2
 
 242                 this.map.viewPortDiv.removeChild(document.getElementById("zoomBox"));
 
 247                 this.map.setCenter(this.map.center);
 
 250         document.onselectstart = null;
 
 251         this.mouseDragStart = null;
 
 252         this.map.div.style.cursor = "default";
 
 255     defaultMouseOut: function (evt) {
 
 256         if (this.mouseDragStart != null
 
 257             && OpenLayers.Util.mouseLeft(evt, this.map.div)) {
 
 258                 this.defaultMouseUp(evt);