layout/xul/nsResizerFrame.cpp

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "nsCOMPtr.h"
michael@0 7 #include "nsIServiceManager.h"
michael@0 8 #include "nsResizerFrame.h"
michael@0 9 #include "nsIContent.h"
michael@0 10 #include "nsIDocument.h"
michael@0 11 #include "nsIDOMNodeList.h"
michael@0 12 #include "nsGkAtoms.h"
michael@0 13 #include "nsNameSpaceManager.h"
michael@0 14 #include "nsIDOMElementCSSInlineStyle.h"
michael@0 15 #include "nsIDOMCSSStyleDeclaration.h"
michael@0 16
michael@0 17 #include "nsPresContext.h"
michael@0 18 #include "nsFrameManager.h"
michael@0 19 #include "nsIDocShell.h"
michael@0 20 #include "nsIDocShellTreeOwner.h"
michael@0 21 #include "nsIBaseWindow.h"
michael@0 22 #include "nsPIDOMWindow.h"
michael@0 23 #include "mozilla/MouseEvents.h"
michael@0 24 #include "nsContentUtils.h"
michael@0 25 #include "nsMenuPopupFrame.h"
michael@0 26 #include "nsIScreenManager.h"
michael@0 27 #include "mozilla/dom/Element.h"
michael@0 28 #include "nsError.h"
michael@0 29 #include <algorithm>
michael@0 30
michael@0 31 using namespace mozilla;
michael@0 32
michael@0 33 //
michael@0 34 // NS_NewResizerFrame
michael@0 35 //
michael@0 36 // Creates a new Resizer frame and returns it
michael@0 37 //
michael@0 38 nsIFrame*
michael@0 39 NS_NewResizerFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
michael@0 40 {
michael@0 41 return new (aPresShell) nsResizerFrame(aPresShell, aContext);
michael@0 42 }
michael@0 43
michael@0 44 NS_IMPL_FRAMEARENA_HELPERS(nsResizerFrame)
michael@0 45
michael@0 46 nsResizerFrame::nsResizerFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
michael@0 47 :nsTitleBarFrame(aPresShell, aContext)
michael@0 48 {
michael@0 49 }
michael@0 50
michael@0 51 nsresult
michael@0 52 nsResizerFrame::HandleEvent(nsPresContext* aPresContext,
michael@0 53 WidgetGUIEvent* aEvent,
michael@0 54 nsEventStatus* aEventStatus)
michael@0 55 {
michael@0 56 NS_ENSURE_ARG_POINTER(aEventStatus);
michael@0 57 if (nsEventStatus_eConsumeNoDefault == *aEventStatus) {
michael@0 58 return NS_OK;
michael@0 59 }
michael@0 60
michael@0 61 nsWeakFrame weakFrame(this);
michael@0 62 bool doDefault = true;
michael@0 63
michael@0 64 switch (aEvent->message) {
michael@0 65 case NS_TOUCH_START:
michael@0 66 case NS_MOUSE_BUTTON_DOWN: {
michael@0 67 if (aEvent->eventStructType == NS_TOUCH_EVENT ||
michael@0 68 (aEvent->eventStructType == NS_MOUSE_EVENT &&
michael@0 69 aEvent->AsMouseEvent()->button == WidgetMouseEvent::eLeftButton)) {
michael@0 70 nsCOMPtr<nsIBaseWindow> window;
michael@0 71 nsIPresShell* presShell = aPresContext->GetPresShell();
michael@0 72 nsIContent* contentToResize =
michael@0 73 GetContentToResize(presShell, getter_AddRefs(window));
michael@0 74 if (contentToResize) {
michael@0 75 nsIFrame* frameToResize = contentToResize->GetPrimaryFrame();
michael@0 76 if (!frameToResize)
michael@0 77 break;
michael@0 78
michael@0 79 // cache the content rectangle for the frame to resize
michael@0 80 // GetScreenRectInAppUnits returns the border box rectangle, so
michael@0 81 // adjust to get the desired content rectangle.
michael@0 82 nsRect rect = frameToResize->GetScreenRectInAppUnits();
michael@0 83 switch (frameToResize->StylePosition()->mBoxSizing) {
michael@0 84 case NS_STYLE_BOX_SIZING_CONTENT:
michael@0 85 rect.Deflate(frameToResize->GetUsedPadding());
michael@0 86 case NS_STYLE_BOX_SIZING_PADDING:
michael@0 87 rect.Deflate(frameToResize->GetUsedBorder());
michael@0 88 default:
michael@0 89 break;
michael@0 90 }
michael@0 91
michael@0 92 mMouseDownRect = rect.ToNearestPixels(aPresContext->AppUnitsPerDevPixel());
michael@0 93 doDefault = false;
michael@0 94 }
michael@0 95 else {
michael@0 96 // If there is no window, then resizing isn't allowed.
michael@0 97 if (!window)
michael@0 98 break;
michael@0 99
michael@0 100 doDefault = false;
michael@0 101
michael@0 102 // ask the widget implementation to begin a resize drag if it can
michael@0 103 Direction direction = GetDirection();
michael@0 104 nsresult rv = aEvent->widget->BeginResizeDrag(aEvent,
michael@0 105 direction.mHorizontal, direction.mVertical);
michael@0 106 // for native drags, don't set the fields below
michael@0 107 if (rv != NS_ERROR_NOT_IMPLEMENTED)
michael@0 108 break;
michael@0 109
michael@0 110 // if there's no native resize support, we need to do window
michael@0 111 // resizing ourselves
michael@0 112 window->GetPositionAndSize(&mMouseDownRect.x, &mMouseDownRect.y,
michael@0 113 &mMouseDownRect.width, &mMouseDownRect.height);
michael@0 114 }
michael@0 115
michael@0 116 // remember current mouse coordinates
michael@0 117 nsIntPoint refPoint;
michael@0 118 if (!GetEventPoint(aEvent, refPoint))
michael@0 119 return NS_OK;
michael@0 120 mMouseDownPoint = refPoint + aEvent->widget->WidgetToScreenOffset();
michael@0 121
michael@0 122 // we're tracking
michael@0 123 mTrackingMouseMove = true;
michael@0 124
michael@0 125 nsIPresShell::SetCapturingContent(GetContent(), CAPTURE_IGNOREALLOWED);
michael@0 126 }
michael@0 127 }
michael@0 128 break;
michael@0 129
michael@0 130 case NS_TOUCH_END:
michael@0 131 case NS_MOUSE_BUTTON_UP: {
michael@0 132 if (aEvent->eventStructType == NS_TOUCH_EVENT ||
michael@0 133 (aEvent->eventStructType == NS_MOUSE_EVENT &&
michael@0 134 aEvent->AsMouseEvent()->button == WidgetMouseEvent::eLeftButton)) {
michael@0 135 // we're done tracking.
michael@0 136 mTrackingMouseMove = false;
michael@0 137
michael@0 138 nsIPresShell::SetCapturingContent(nullptr, 0);
michael@0 139
michael@0 140 doDefault = false;
michael@0 141 }
michael@0 142 }
michael@0 143 break;
michael@0 144
michael@0 145 case NS_TOUCH_MOVE:
michael@0 146 case NS_MOUSE_MOVE: {
michael@0 147 if (mTrackingMouseMove)
michael@0 148 {
michael@0 149 nsCOMPtr<nsIBaseWindow> window;
michael@0 150 nsIPresShell* presShell = aPresContext->GetPresShell();
michael@0 151 nsCOMPtr<nsIContent> contentToResize =
michael@0 152 GetContentToResize(presShell, getter_AddRefs(window));
michael@0 153
michael@0 154 // check if the returned content really is a menupopup
michael@0 155 nsMenuPopupFrame* menuPopupFrame = nullptr;
michael@0 156 if (contentToResize) {
michael@0 157 menuPopupFrame = do_QueryFrame(contentToResize->GetPrimaryFrame());
michael@0 158 }
michael@0 159
michael@0 160 // both MouseMove and direction are negative when pointing to the
michael@0 161 // top and left, and positive when pointing to the bottom and right
michael@0 162
michael@0 163 // retrieve the offset of the mousemove event relative to the mousedown.
michael@0 164 // The difference is how much the resize needs to be
michael@0 165 nsIntPoint refPoint;
michael@0 166 if (!GetEventPoint(aEvent, refPoint))
michael@0 167 return NS_OK;
michael@0 168 nsIntPoint screenPoint(refPoint + aEvent->widget->WidgetToScreenOffset());
michael@0 169 nsIntPoint mouseMove(screenPoint - mMouseDownPoint);
michael@0 170
michael@0 171 // Determine which direction to resize by checking the dir attribute.
michael@0 172 // For windows and menus, ensure that it can be resized in that direction.
michael@0 173 Direction direction = GetDirection();
michael@0 174 if (window || menuPopupFrame) {
michael@0 175 if (menuPopupFrame) {
michael@0 176 menuPopupFrame->CanAdjustEdges(
michael@0 177 (direction.mHorizontal == -1) ? NS_SIDE_LEFT : NS_SIDE_RIGHT,
michael@0 178 (direction.mVertical == -1) ? NS_SIDE_TOP : NS_SIDE_BOTTOM, mouseMove);
michael@0 179 }
michael@0 180 }
michael@0 181 else if (!contentToResize) {
michael@0 182 break; // don't do anything if there's nothing to resize
michael@0 183 }
michael@0 184
michael@0 185 nsIntRect rect = mMouseDownRect;
michael@0 186
michael@0 187 // Check if there are any size constraints on this window.
michael@0 188 widget::SizeConstraints sizeConstraints;
michael@0 189 if (window) {
michael@0 190 nsCOMPtr<nsIWidget> widget;
michael@0 191 window->GetMainWidget(getter_AddRefs(widget));
michael@0 192 sizeConstraints = widget->GetSizeConstraints();
michael@0 193 }
michael@0 194
michael@0 195 AdjustDimensions(&rect.x, &rect.width, sizeConstraints.mMinSize.width,
michael@0 196 sizeConstraints.mMaxSize.width, mouseMove.x, direction.mHorizontal);
michael@0 197 AdjustDimensions(&rect.y, &rect.height, sizeConstraints.mMinSize.height,
michael@0 198 sizeConstraints.mMaxSize.height, mouseMove.y, direction.mVertical);
michael@0 199
michael@0 200 // Don't allow resizing a window or a popup past the edge of the screen,
michael@0 201 // so adjust the rectangle to fit within the available screen area.
michael@0 202 if (window) {
michael@0 203 nsCOMPtr<nsIScreen> screen;
michael@0 204 nsCOMPtr<nsIScreenManager> sm(do_GetService("@mozilla.org/gfx/screenmanager;1"));
michael@0 205 if (sm) {
michael@0 206 nsIntRect frameRect = GetScreenRect();
michael@0 207 // ScreenForRect requires display pixels, so scale from device pix
michael@0 208 double scale;
michael@0 209 window->GetUnscaledDevicePixelsPerCSSPixel(&scale);
michael@0 210 sm->ScreenForRect(NSToIntRound(frameRect.x / scale),
michael@0 211 NSToIntRound(frameRect.y / scale), 1, 1,
michael@0 212 getter_AddRefs(screen));
michael@0 213 if (screen) {
michael@0 214 nsIntRect screenRect;
michael@0 215 screen->GetRect(&screenRect.x, &screenRect.y,
michael@0 216 &screenRect.width, &screenRect.height);
michael@0 217 rect.IntersectRect(rect, screenRect);
michael@0 218 }
michael@0 219 }
michael@0 220 }
michael@0 221 else if (menuPopupFrame) {
michael@0 222 nsRect frameRect = menuPopupFrame->GetScreenRectInAppUnits();
michael@0 223 nsIFrame* rootFrame = aPresContext->PresShell()->FrameManager()->GetRootFrame();
michael@0 224 nsRect rootScreenRect = rootFrame->GetScreenRectInAppUnits();
michael@0 225
michael@0 226 nsPopupLevel popupLevel = menuPopupFrame->PopupLevel();
michael@0 227 nsRect screenRect = menuPopupFrame->GetConstraintRect(frameRect, rootScreenRect, popupLevel);
michael@0 228 // round using ToInsidePixels as it's better to be a pixel too small
michael@0 229 // than be too large. If the popup is too large it could get flipped
michael@0 230 // to the opposite side of the anchor point while resizing.
michael@0 231 nsIntRect screenRectPixels = screenRect.ToInsidePixels(aPresContext->AppUnitsPerDevPixel());
michael@0 232 rect.IntersectRect(rect, screenRectPixels);
michael@0 233 }
michael@0 234
michael@0 235 if (contentToResize) {
michael@0 236 // convert the rectangle into css pixels. When changing the size in a
michael@0 237 // direction, don't allow the new size to be less that the resizer's
michael@0 238 // size. This ensures that content isn't resized too small as to make
michael@0 239 // the resizer invisible.
michael@0 240 nsRect appUnitsRect = rect.ToAppUnits(aPresContext->AppUnitsPerDevPixel());
michael@0 241 if (appUnitsRect.width < mRect.width && mouseMove.x)
michael@0 242 appUnitsRect.width = mRect.width;
michael@0 243 if (appUnitsRect.height < mRect.height && mouseMove.y)
michael@0 244 appUnitsRect.height = mRect.height;
michael@0 245 nsIntRect cssRect = appUnitsRect.ToInsidePixels(nsPresContext::AppUnitsPerCSSPixel());
michael@0 246
michael@0 247 nsIntRect oldRect;
michael@0 248 nsWeakFrame weakFrame(menuPopupFrame);
michael@0 249 if (menuPopupFrame) {
michael@0 250 nsCOMPtr<nsIWidget> widget = menuPopupFrame->GetWidget();
michael@0 251 if (widget)
michael@0 252 widget->GetScreenBounds(oldRect);
michael@0 253
michael@0 254 // convert the new rectangle into outer window coordinates
michael@0 255 nsIntPoint clientOffset = widget->GetClientOffset();
michael@0 256 rect.x -= clientOffset.x;
michael@0 257 rect.y -= clientOffset.y;
michael@0 258 }
michael@0 259
michael@0 260 SizeInfo sizeInfo, originalSizeInfo;
michael@0 261 sizeInfo.width.AppendInt(cssRect.width);
michael@0 262 sizeInfo.height.AppendInt(cssRect.height);
michael@0 263 ResizeContent(contentToResize, direction, sizeInfo, &originalSizeInfo);
michael@0 264 MaybePersistOriginalSize(contentToResize, originalSizeInfo);
michael@0 265
michael@0 266 // Move the popup to the new location unless it is anchored, since
michael@0 267 // the position shouldn't change. nsMenuPopupFrame::SetPopupPosition
michael@0 268 // will instead ensure that the popup's position is anchored at the
michael@0 269 // right place.
michael@0 270 if (weakFrame.IsAlive() &&
michael@0 271 (oldRect.x != rect.x || oldRect.y != rect.y) &&
michael@0 272 (!menuPopupFrame->IsAnchored() ||
michael@0 273 menuPopupFrame->PopupLevel() != ePopupLevelParent)) {
michael@0 274
michael@0 275 rect.x = aPresContext->DevPixelsToIntCSSPixels(rect.x);
michael@0 276 rect.y = aPresContext->DevPixelsToIntCSSPixels(rect.y);
michael@0 277 menuPopupFrame->MoveTo(rect.x, rect.y, true);
michael@0 278 }
michael@0 279 }
michael@0 280 else {
michael@0 281 window->SetPositionAndSize(rect.x, rect.y, rect.width, rect.height, true); // do the repaint.
michael@0 282 }
michael@0 283
michael@0 284 doDefault = false;
michael@0 285 }
michael@0 286 }
michael@0 287 break;
michael@0 288
michael@0 289 case NS_MOUSE_CLICK: {
michael@0 290 WidgetMouseEvent* mouseEvent = aEvent->AsMouseEvent();
michael@0 291 if (mouseEvent->IsLeftClickEvent()) {
michael@0 292 MouseClicked(aPresContext, mouseEvent);
michael@0 293 }
michael@0 294 break;
michael@0 295 }
michael@0 296 case NS_MOUSE_DOUBLECLICK:
michael@0 297 if (aEvent->AsMouseEvent()->button == WidgetMouseEvent::eLeftButton) {
michael@0 298 nsCOMPtr<nsIBaseWindow> window;
michael@0 299 nsIPresShell* presShell = aPresContext->GetPresShell();
michael@0 300 nsIContent* contentToResize =
michael@0 301 GetContentToResize(presShell, getter_AddRefs(window));
michael@0 302 if (contentToResize) {
michael@0 303 nsMenuPopupFrame* menuPopupFrame = do_QueryFrame(contentToResize->GetPrimaryFrame());
michael@0 304 if (menuPopupFrame)
michael@0 305 break; // Don't restore original sizing for menupopup frames until
michael@0 306 // we handle screen constraints here. (Bug 357725)
michael@0 307
michael@0 308 RestoreOriginalSize(contentToResize);
michael@0 309 }
michael@0 310 }
michael@0 311 break;
michael@0 312 }
michael@0 313
michael@0 314 if (!doDefault)
michael@0 315 *aEventStatus = nsEventStatus_eConsumeNoDefault;
michael@0 316
michael@0 317 if (doDefault && weakFrame.IsAlive())
michael@0 318 return nsTitleBarFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
michael@0 319
michael@0 320 return NS_OK;
michael@0 321 }
michael@0 322
michael@0 323 nsIContent*
michael@0 324 nsResizerFrame::GetContentToResize(nsIPresShell* aPresShell, nsIBaseWindow** aWindow)
michael@0 325 {
michael@0 326 *aWindow = nullptr;
michael@0 327
michael@0 328 nsAutoString elementid;
michael@0 329 mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::element, elementid);
michael@0 330 if (elementid.IsEmpty()) {
michael@0 331 // If the resizer is in a popup, resize the popup's widget, otherwise
michael@0 332 // resize the widget associated with the window.
michael@0 333 nsIFrame* popup = GetParent();
michael@0 334 while (popup) {
michael@0 335 nsMenuPopupFrame* popupFrame = do_QueryFrame(popup);
michael@0 336 if (popupFrame) {
michael@0 337 return popupFrame->GetContent();
michael@0 338 }
michael@0 339 popup = popup->GetParent();
michael@0 340 }
michael@0 341
michael@0 342 // don't allow resizing windows in content shells
michael@0 343 nsCOMPtr<nsIDocShellTreeItem> dsti = aPresShell->GetPresContext()->GetDocShell();
michael@0 344 if (!dsti || dsti->ItemType() != nsIDocShellTreeItem::typeChrome) {
michael@0 345 // don't allow resizers in content shells, except for the viewport
michael@0 346 // scrollbar which doesn't have a parent
michael@0 347 nsIContent* nonNativeAnon = mContent->FindFirstNonChromeOnlyAccessContent();
michael@0 348 if (!nonNativeAnon || nonNativeAnon->GetParent()) {
michael@0 349 return nullptr;
michael@0 350 }
michael@0 351 }
michael@0 352
michael@0 353 // get the document and the window - should this be cached?
michael@0 354 nsPIDOMWindow *domWindow = aPresShell->GetDocument()->GetWindow();
michael@0 355 if (domWindow) {
michael@0 356 nsCOMPtr<nsIDocShell> docShell = domWindow->GetDocShell();
michael@0 357 if (docShell) {
michael@0 358 nsCOMPtr<nsIDocShellTreeOwner> treeOwner;
michael@0 359 docShell->GetTreeOwner(getter_AddRefs(treeOwner));
michael@0 360 if (treeOwner) {
michael@0 361 CallQueryInterface(treeOwner, aWindow);
michael@0 362 }
michael@0 363 }
michael@0 364 }
michael@0 365
michael@0 366 return nullptr;
michael@0 367 }
michael@0 368
michael@0 369 if (elementid.EqualsLiteral("_parent")) {
michael@0 370 // return the parent, but skip over native anonymous content
michael@0 371 nsIContent* parent = mContent->GetParent();
michael@0 372 return parent ? parent->FindFirstNonChromeOnlyAccessContent() : nullptr;
michael@0 373 }
michael@0 374
michael@0 375 return aPresShell->GetDocument()->GetElementById(elementid);
michael@0 376 }
michael@0 377
michael@0 378 void
michael@0 379 nsResizerFrame::AdjustDimensions(int32_t* aPos, int32_t* aSize,
michael@0 380 int32_t aMinSize, int32_t aMaxSize,
michael@0 381 int32_t aMovement, int8_t aResizerDirection)
michael@0 382 {
michael@0 383 int32_t oldSize = *aSize;
michael@0 384
michael@0 385 *aSize += aResizerDirection * aMovement;
michael@0 386 // use one as a minimum size or the element could disappear
michael@0 387 if (*aSize < 1)
michael@0 388 *aSize = 1;
michael@0 389
michael@0 390 // Constrain the size within the minimum and maximum size.
michael@0 391 *aSize = std::max(aMinSize, std::min(aMaxSize, *aSize));
michael@0 392
michael@0 393 // For left and top resizers, the window must be moved left by the same
michael@0 394 // amount that the window was resized.
michael@0 395 if (aResizerDirection == -1)
michael@0 396 *aPos += oldSize - *aSize;
michael@0 397 }
michael@0 398
michael@0 399 /* static */ void
michael@0 400 nsResizerFrame::ResizeContent(nsIContent* aContent, const Direction& aDirection,
michael@0 401 const SizeInfo& aSizeInfo, SizeInfo* aOriginalSizeInfo)
michael@0 402 {
michael@0 403 // for XUL elements, just set the width and height attributes. For
michael@0 404 // other elements, set style.width and style.height
michael@0 405 if (aContent->IsXUL()) {
michael@0 406 if (aOriginalSizeInfo) {
michael@0 407 aContent->GetAttr(kNameSpaceID_None, nsGkAtoms::width,
michael@0 408 aOriginalSizeInfo->width);
michael@0 409 aContent->GetAttr(kNameSpaceID_None, nsGkAtoms::height,
michael@0 410 aOriginalSizeInfo->height);
michael@0 411 }
michael@0 412 // only set the property if the element could have changed in that direction
michael@0 413 if (aDirection.mHorizontal) {
michael@0 414 aContent->SetAttr(kNameSpaceID_None, nsGkAtoms::width, aSizeInfo.width, true);
michael@0 415 }
michael@0 416 if (aDirection.mVertical) {
michael@0 417 aContent->SetAttr(kNameSpaceID_None, nsGkAtoms::height, aSizeInfo.height, true);
michael@0 418 }
michael@0 419 }
michael@0 420 else {
michael@0 421 nsCOMPtr<nsIDOMElementCSSInlineStyle> inlineStyleContent =
michael@0 422 do_QueryInterface(aContent);
michael@0 423 if (inlineStyleContent) {
michael@0 424 nsCOMPtr<nsIDOMCSSStyleDeclaration> decl;
michael@0 425 inlineStyleContent->GetStyle(getter_AddRefs(decl));
michael@0 426
michael@0 427 if (aOriginalSizeInfo) {
michael@0 428 decl->GetPropertyValue(NS_LITERAL_STRING("width"),
michael@0 429 aOriginalSizeInfo->width);
michael@0 430 decl->GetPropertyValue(NS_LITERAL_STRING("height"),
michael@0 431 aOriginalSizeInfo->height);
michael@0 432 }
michael@0 433
michael@0 434 // only set the property if the element could have changed in that direction
michael@0 435 if (aDirection.mHorizontal) {
michael@0 436 nsAutoString widthstr(aSizeInfo.width);
michael@0 437 if (!widthstr.IsEmpty() &&
michael@0 438 !Substring(widthstr, widthstr.Length() - 2, 2).EqualsLiteral("px"))
michael@0 439 widthstr.AppendLiteral("px");
michael@0 440 decl->SetProperty(NS_LITERAL_STRING("width"), widthstr, EmptyString());
michael@0 441 }
michael@0 442 if (aDirection.mVertical) {
michael@0 443 nsAutoString heightstr(aSizeInfo.height);
michael@0 444 if (!heightstr.IsEmpty() &&
michael@0 445 !Substring(heightstr, heightstr.Length() - 2, 2).EqualsLiteral("px"))
michael@0 446 heightstr.AppendLiteral("px");
michael@0 447 decl->SetProperty(NS_LITERAL_STRING("height"), heightstr, EmptyString());
michael@0 448 }
michael@0 449 }
michael@0 450 }
michael@0 451 }
michael@0 452
michael@0 453 /* static */ void
michael@0 454 nsResizerFrame::MaybePersistOriginalSize(nsIContent* aContent,
michael@0 455 const SizeInfo& aSizeInfo)
michael@0 456 {
michael@0 457 nsresult rv;
michael@0 458
michael@0 459 aContent->GetProperty(nsGkAtoms::_moz_original_size, &rv);
michael@0 460 if (rv != NS_PROPTABLE_PROP_NOT_THERE)
michael@0 461 return;
michael@0 462
michael@0 463 nsAutoPtr<SizeInfo> sizeInfo(new SizeInfo(aSizeInfo));
michael@0 464 rv = aContent->SetProperty(nsGkAtoms::_moz_original_size, sizeInfo.get(),
michael@0 465 nsINode::DeleteProperty<nsResizerFrame::SizeInfo>);
michael@0 466 if (NS_SUCCEEDED(rv))
michael@0 467 sizeInfo.forget();
michael@0 468 }
michael@0 469
michael@0 470 /* static */ void
michael@0 471 nsResizerFrame::RestoreOriginalSize(nsIContent* aContent)
michael@0 472 {
michael@0 473 nsresult rv;
michael@0 474 SizeInfo* sizeInfo =
michael@0 475 static_cast<SizeInfo*>(aContent->GetProperty(nsGkAtoms::_moz_original_size,
michael@0 476 &rv));
michael@0 477 if (NS_FAILED(rv))
michael@0 478 return;
michael@0 479
michael@0 480 NS_ASSERTION(sizeInfo, "We set a null sizeInfo!?");
michael@0 481 Direction direction = {1, 1};
michael@0 482 ResizeContent(aContent, direction, *sizeInfo, nullptr);
michael@0 483 aContent->DeleteProperty(nsGkAtoms::_moz_original_size);
michael@0 484 }
michael@0 485
michael@0 486 /* returns a Direction struct containing the horizontal and vertical direction
michael@0 487 */
michael@0 488 nsResizerFrame::Direction
michael@0 489 nsResizerFrame::GetDirection()
michael@0 490 {
michael@0 491 static const nsIContent::AttrValuesArray strings[] =
michael@0 492 {&nsGkAtoms::topleft, &nsGkAtoms::top, &nsGkAtoms::topright,
michael@0 493 &nsGkAtoms::left, &nsGkAtoms::right,
michael@0 494 &nsGkAtoms::bottomleft, &nsGkAtoms::bottom, &nsGkAtoms::bottomright,
michael@0 495 &nsGkAtoms::bottomstart, &nsGkAtoms::bottomend,
michael@0 496 nullptr};
michael@0 497
michael@0 498 static const Direction directions[] =
michael@0 499 {{-1, -1}, {0, -1}, {1, -1},
michael@0 500 {-1, 0}, {1, 0},
michael@0 501 {-1, 1}, {0, 1}, {1, 1},
michael@0 502 {-1, 1}, {1, 1}
michael@0 503 };
michael@0 504
michael@0 505 if (!GetContent())
michael@0 506 return directions[0]; // default: topleft
michael@0 507
michael@0 508 int32_t index = GetContent()->FindAttrValueIn(kNameSpaceID_None,
michael@0 509 nsGkAtoms::dir,
michael@0 510 strings, eCaseMatters);
michael@0 511 if(index < 0)
michael@0 512 return directions[0]; // default: topleft
michael@0 513 else if (index >= 8 && StyleVisibility()->mDirection == NS_STYLE_DIRECTION_RTL) {
michael@0 514 // Directions 8 and higher are RTL-aware directions and should reverse the
michael@0 515 // horizontal component if RTL.
michael@0 516 Direction direction = directions[index];
michael@0 517 direction.mHorizontal *= -1;
michael@0 518 return direction;
michael@0 519 }
michael@0 520 return directions[index];
michael@0 521 }
michael@0 522
michael@0 523 void
michael@0 524 nsResizerFrame::MouseClicked(nsPresContext* aPresContext,
michael@0 525 WidgetMouseEvent* aEvent)
michael@0 526 {
michael@0 527 // Execute the oncommand event handler.
michael@0 528 nsContentUtils::DispatchXULCommand(mContent,
michael@0 529 aEvent && aEvent->mFlags.mIsTrusted);
michael@0 530 }

mercurial