Back to index

lightning-sunbird  0.9+nobinonly
nsDOMSVGZoomEvent.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla SVG Project code.
00016  *
00017  * The Initial Developer of the Original Code is Jonathan Watt.
00018  * Portions created by the Initial Developer are Copyright (C) 2005
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Jonathan Watt <jonathan.watt@strath.ac.uk> (original author)
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsDOMSVGZoomEvent.h"
00039 #include "nsContentUtils.h"
00040 #include "nsSVGRect.h"
00041 #include "nsSVGPoint.h"
00042 #include "nsISVGSVGElement.h"
00043 #include "nsIDOMSVGSVGElement.h"
00044 #include "nsIContent.h"
00045 #include "nsIPresShell.h"
00046 #include "nsIDocument.h"
00047 
00048 //----------------------------------------------------------------------
00049 // Implementation
00050 
00051 nsDOMSVGZoomEvent::nsDOMSVGZoomEvent(nsPresContext* aPresContext,
00052                                      nsGUIEvent* aEvent)
00053   : nsDOMUIEvent(aPresContext,
00054                  aEvent ? aEvent : new nsGUIEvent(PR_FALSE, NS_SVG_ZOOM, 0))
00055 {
00056   if (aEvent) {
00057     mEventIsInternal = PR_FALSE;
00058   }
00059   else {
00060     mEventIsInternal = PR_TRUE;
00061     mEvent->eventStructType = NS_SVGZOOM_EVENT;
00062     mEvent->time = PR_Now();
00063   }
00064 
00065   mEvent->flags |= NS_EVENT_FLAG_CANT_CANCEL;
00066 
00067   // We must store the "Previous" and "New" values before this event is
00068   // dispatched. Reading the values from the root 'svg' element after we've
00069   // been dispatched is not an option since event handler code may change
00070   // currentScale and currentTranslate in response to this event.
00071   nsIPresShell *presShell;
00072   if (mPresContext && (presShell = mPresContext->GetPresShell())) {
00073     nsIDocument *doc = presShell->GetDocument();
00074     if (doc) {
00075       nsIContent *rootContent = doc->GetRootContent();
00076       if (rootContent) {
00077         // If the root element isn't an SVG 'svg' element these QIs will fail
00078         // (e.g. if this event was created by calling createEvent on a
00079         // non-SVGDocument). In these circumstances the "New" and "Previous"
00080         // properties will be left null which is probably what we want.
00081         nsCOMPtr<nsIDOMSVGSVGElement> svgElement = do_QueryInterface(rootContent);
00082         if (svgElement) {
00083           svgElement->GetCurrentScale(&mNewScale);
00084           float x, y;
00085           nsCOMPtr<nsIDOMSVGPoint> currentTranslate;
00086           svgElement->GetCurrentTranslate(getter_AddRefs(currentTranslate));
00087           currentTranslate->GetX(&x);
00088           currentTranslate->GetY(&y);
00089           NS_NewSVGReadonlyPoint(getter_AddRefs(mNewTranslate), x, y);
00090         }
00091         nsCOMPtr<nsISVGSVGElement> privSVGElement = do_QueryInterface(rootContent);
00092         if (svgElement) {
00093           mPreviousScale = privSVGElement->GetPreviousScale();
00094           NS_NewSVGReadonlyPoint(getter_AddRefs(mPreviousTranslate),
00095                                  privSVGElement->GetPreviousTranslate_x(),
00096                                  privSVGElement->GetPreviousTranslate_y());
00097           // Important: we call RecordCurrentST() here to make sure that
00098           // scripts that create an SVGZoomEvent won't get our "Previous" data
00099           privSVGElement->RecordCurrentScaleTranslate();
00100         }
00101       }
00102     }
00103   }
00104 }
00105 
00106 
00107 //----------------------------------------------------------------------
00108 // nsISupports methods:
00109 
00110 NS_IMPL_ADDREF_INHERITED(nsDOMSVGZoomEvent, nsDOMEvent)
00111 NS_IMPL_RELEASE_INHERITED(nsDOMSVGZoomEvent, nsDOMEvent)
00112 
00113 NS_INTERFACE_MAP_BEGIN(nsDOMSVGZoomEvent)
00114   NS_INTERFACE_MAP_ENTRY(nsIDOMSVGZoomEvent)
00115   NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(SVGZoomEvent)
00116 NS_INTERFACE_MAP_END_INHERITING(nsDOMEvent)
00117 
00118 
00119 //----------------------------------------------------------------------
00120 // nsIDOMSVGZoomEvent methods:
00121 
00122 /* readonly attribute SVGRect zoomRectScreen; */
00123 NS_IMETHODIMP nsDOMSVGZoomEvent::GetZoomRectScreen(nsIDOMSVGRect **aZoomRectScreen)
00124 {
00125   // The spec says about this attribute:
00126   //
00127   //   The specified zoom rectangle in screen units.
00128   //   The object itself and its contents are both readonly.
00129   //
00130   // This is so badly underspecified we don't implement it. It was probably
00131   // thrown in without much thought as a way of finding the zoom box ASV style
00132   // zooming uses. I don't see how this is useful though since SVGZoom event's
00133   // get dispatched *after* the zoom level has changed.
00134   //
00135   // Be sure to use NS_NewSVGReadonlyRect and not NS_NewSVGRect if we
00136   // eventually do implement this!
00137 
00138   *aZoomRectScreen = nsnull;
00139   NS_NOTYETIMPLEMENTED("nsDOMSVGZoomEvent::GetZoomRectScreen");
00140   return NS_ERROR_NOT_IMPLEMENTED;
00141 }
00142 
00143 /* readonly attribute float previousScale; */
00144 NS_IMETHODIMP
00145 nsDOMSVGZoomEvent::GetPreviousScale(float *aPreviousScale)
00146 {
00147   *aPreviousScale = mPreviousScale;
00148   return NS_OK;
00149 }
00150 
00151 /* readonly attribute SVGPoint previousTranslate; */
00152 NS_IMETHODIMP
00153 nsDOMSVGZoomEvent::GetPreviousTranslate(nsIDOMSVGPoint **aPreviousTranslate)
00154 {
00155   *aPreviousTranslate = mPreviousTranslate;
00156   NS_IF_ADDREF(*aPreviousTranslate);
00157   return NS_OK;
00158 }
00159 
00160 /* readonly attribute float newScale; */
00161 NS_IMETHODIMP nsDOMSVGZoomEvent::GetNewScale(float *aNewScale)
00162 {
00163   *aNewScale = mNewScale;
00164   return NS_OK;
00165 }
00166 
00167 /* readonly attribute SVGPoint newTranslate; */
00168 NS_IMETHODIMP
00169 nsDOMSVGZoomEvent::GetNewTranslate(nsIDOMSVGPoint **aNewTranslate)
00170 {
00171   *aNewTranslate = mNewTranslate;
00172   NS_IF_ADDREF(*aNewTranslate);
00173   return NS_OK;
00174 }
00175 
00176 
00178 // Exported creation functions:
00179 
00180 nsresult
00181 NS_NewDOMSVGZoomEvent(nsIDOMEvent** aInstancePtrResult,
00182                       nsPresContext* aPresContext,
00183                       nsGUIEvent *aEvent)
00184 {
00185   nsDOMSVGZoomEvent* it = new nsDOMSVGZoomEvent(aPresContext, aEvent);
00186   if (!it)
00187     return NS_ERROR_OUT_OF_MEMORY;
00188 
00189   return CallQueryInterface(it, aInstancePtrResult);
00190 }