nsResizerFrame.cpp 19 KB

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