PopupBoxObject.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 "nsCOMPtr.h"
  6. #include "nsIRootBox.h"
  7. #include "nsIPresShell.h"
  8. #include "nsIContent.h"
  9. #include "nsNameSpaceManager.h"
  10. #include "nsGkAtoms.h"
  11. #include "nsMenuPopupFrame.h"
  12. #include "nsView.h"
  13. #include "mozilla/AppUnits.h"
  14. #include "mozilla/dom/DOMRect.h"
  15. #include "mozilla/dom/PopupBoxObject.h"
  16. #include "mozilla/dom/Element.h"
  17. #include "mozilla/dom/Event.h"
  18. #include "mozilla/dom/PopupBoxObjectBinding.h"
  19. namespace mozilla {
  20. namespace dom {
  21. NS_IMPL_ADDREF_INHERITED(PopupBoxObject, BoxObject)
  22. NS_IMPL_RELEASE_INHERITED(PopupBoxObject, BoxObject)
  23. NS_INTERFACE_MAP_BEGIN(PopupBoxObject)
  24. NS_INTERFACE_MAP_END_INHERITING(BoxObject)
  25. PopupBoxObject::PopupBoxObject()
  26. {
  27. }
  28. PopupBoxObject::~PopupBoxObject()
  29. {
  30. }
  31. nsIContent* PopupBoxObject::GetParentObject() const
  32. {
  33. return BoxObject::GetParentObject();
  34. }
  35. JSObject* PopupBoxObject::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  36. {
  37. return PopupBoxObjectBinding::Wrap(aCx, this, aGivenProto);
  38. }
  39. nsPopupSetFrame*
  40. PopupBoxObject::GetPopupSetFrame()
  41. {
  42. nsIRootBox* rootBox = nsIRootBox::GetRootBox(GetPresShell(false));
  43. if (!rootBox)
  44. return nullptr;
  45. return rootBox->GetPopupSetFrame();
  46. }
  47. void
  48. PopupBoxObject::HidePopup(bool aCancel)
  49. {
  50. nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
  51. if (pm && mContent) {
  52. pm->HidePopup(mContent, false, true, false, aCancel);
  53. }
  54. }
  55. void
  56. PopupBoxObject::ShowPopup(Element* aAnchorElement,
  57. Element& aPopupElement,
  58. int32_t aXPos, int32_t aYPos,
  59. const nsAString& aPopupType,
  60. const nsAString& aAnchorAlignment,
  61. const nsAString& aPopupAlignment)
  62. {
  63. nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
  64. if (pm && mContent) {
  65. nsCOMPtr<nsIContent> anchorContent(do_QueryInterface(aAnchorElement));
  66. nsAutoString popupType(aPopupType);
  67. nsAutoString anchor(aAnchorAlignment);
  68. nsAutoString align(aPopupAlignment);
  69. pm->ShowPopupWithAnchorAlign(mContent, anchorContent, anchor, align,
  70. aXPos, aYPos,
  71. popupType.EqualsLiteral("context"));
  72. }
  73. }
  74. void
  75. PopupBoxObject::OpenPopup(Element* aAnchorElement,
  76. const nsAString& aPosition,
  77. int32_t aXPos, int32_t aYPos,
  78. bool aIsContextMenu,
  79. bool aAttributesOverride,
  80. Event* aTriggerEvent)
  81. {
  82. nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
  83. if (pm && mContent) {
  84. nsCOMPtr<nsIContent> anchorContent(do_QueryInterface(aAnchorElement));
  85. pm->ShowPopup(mContent, anchorContent, aPosition, aXPos, aYPos,
  86. aIsContextMenu, aAttributesOverride, false, aTriggerEvent);
  87. }
  88. }
  89. void
  90. PopupBoxObject::OpenPopupAtScreen(int32_t aXPos, int32_t aYPos,
  91. bool aIsContextMenu,
  92. Event* aTriggerEvent)
  93. {
  94. nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
  95. if (pm && mContent)
  96. pm->ShowPopupAtScreen(mContent, aXPos, aYPos, aIsContextMenu, aTriggerEvent);
  97. }
  98. void
  99. PopupBoxObject::OpenPopupAtScreenRect(const nsAString& aPosition,
  100. int32_t aXPos, int32_t aYPos,
  101. int32_t aWidth, int32_t aHeight,
  102. bool aIsContextMenu,
  103. bool aAttributesOverride,
  104. Event* aTriggerEvent)
  105. {
  106. nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
  107. if (pm && mContent) {
  108. pm->ShowPopupAtScreenRect(mContent, aPosition,
  109. nsIntRect(aXPos, aYPos, aWidth, aHeight),
  110. aIsContextMenu, aAttributesOverride, aTriggerEvent);
  111. }
  112. }
  113. void
  114. PopupBoxObject::MoveTo(int32_t aLeft, int32_t aTop)
  115. {
  116. nsMenuPopupFrame *menuPopupFrame = mContent ? do_QueryFrame(mContent->GetPrimaryFrame()) : nullptr;
  117. if (menuPopupFrame) {
  118. menuPopupFrame->MoveTo(CSSIntPoint(aLeft, aTop), true);
  119. }
  120. }
  121. void
  122. PopupBoxObject::MoveToAnchor(Element* aAnchorElement,
  123. const nsAString& aPosition,
  124. int32_t aXPos, int32_t aYPos,
  125. bool aAttributesOverride)
  126. {
  127. if (mContent) {
  128. nsCOMPtr<nsIContent> anchorContent(do_QueryInterface(aAnchorElement));
  129. nsMenuPopupFrame *menuPopupFrame = do_QueryFrame(mContent->GetPrimaryFrame());
  130. if (menuPopupFrame && menuPopupFrame->IsVisible()) {
  131. menuPopupFrame->MoveToAnchor(anchorContent, aPosition, aXPos, aYPos, aAttributesOverride);
  132. }
  133. }
  134. }
  135. void
  136. PopupBoxObject::SizeTo(int32_t aWidth, int32_t aHeight)
  137. {
  138. if (!mContent)
  139. return;
  140. nsAutoString width, height;
  141. width.AppendInt(aWidth);
  142. height.AppendInt(aHeight);
  143. nsCOMPtr<nsIContent> content = mContent;
  144. // We only want to pass aNotify=true to SetAttr once, but must make sure
  145. // we pass it when a value is being changed. Thus, we check if the height
  146. // is the same and if so, pass true when setting the width.
  147. bool heightSame = content->AttrValueIs(kNameSpaceID_None, nsGkAtoms::height, height, eCaseMatters);
  148. content->SetAttr(kNameSpaceID_None, nsGkAtoms::width, width, heightSame);
  149. content->SetAttr(kNameSpaceID_None, nsGkAtoms::height, height, true);
  150. }
  151. bool
  152. PopupBoxObject::AutoPosition()
  153. {
  154. nsMenuPopupFrame *menuPopupFrame = mContent ? do_QueryFrame(mContent->GetPrimaryFrame()) : nullptr;
  155. if (menuPopupFrame) {
  156. return menuPopupFrame->GetAutoPosition();
  157. }
  158. return true;
  159. }
  160. void
  161. PopupBoxObject::SetAutoPosition(bool aShouldAutoPosition)
  162. {
  163. nsMenuPopupFrame *menuPopupFrame = mContent ? do_QueryFrame(mContent->GetPrimaryFrame()) : nullptr;
  164. if (menuPopupFrame) {
  165. menuPopupFrame->SetAutoPosition(aShouldAutoPosition);
  166. }
  167. }
  168. void
  169. PopupBoxObject::EnableRollup(bool aShouldRollup)
  170. {
  171. // this does nothing now
  172. }
  173. void
  174. PopupBoxObject::SetConsumeRollupEvent(uint32_t aConsume)
  175. {
  176. nsMenuPopupFrame *menuPopupFrame = do_QueryFrame(GetFrame(false));
  177. if (menuPopupFrame) {
  178. menuPopupFrame->SetConsumeRollupEvent(aConsume);
  179. }
  180. }
  181. void
  182. PopupBoxObject::EnableKeyboardNavigator(bool aEnableKeyboardNavigator)
  183. {
  184. if (!mContent)
  185. return;
  186. // Use ignorekeys="true" on the popup instead of using this function.
  187. if (aEnableKeyboardNavigator)
  188. mContent->UnsetAttr(kNameSpaceID_None, nsGkAtoms::ignorekeys, true);
  189. else
  190. mContent->SetAttr(kNameSpaceID_None, nsGkAtoms::ignorekeys,
  191. NS_LITERAL_STRING("true"), true);
  192. }
  193. void
  194. PopupBoxObject::GetPopupState(nsString& aState)
  195. {
  196. // set this here in case there's no frame for the popup
  197. aState.AssignLiteral("closed");
  198. nsMenuPopupFrame *menuPopupFrame = mContent ? do_QueryFrame(mContent->GetPrimaryFrame()) : nullptr;
  199. if (menuPopupFrame) {
  200. switch (menuPopupFrame->PopupState()) {
  201. case ePopupShown:
  202. aState.AssignLiteral("open");
  203. break;
  204. case ePopupShowing:
  205. case ePopupPositioning:
  206. case ePopupOpening:
  207. case ePopupVisible:
  208. aState.AssignLiteral("showing");
  209. break;
  210. case ePopupHiding:
  211. case ePopupInvisible:
  212. aState.AssignLiteral("hiding");
  213. break;
  214. case ePopupClosed:
  215. break;
  216. default:
  217. NS_NOTREACHED("Bad popup state");
  218. break;
  219. }
  220. }
  221. }
  222. nsINode*
  223. PopupBoxObject::GetTriggerNode() const
  224. {
  225. nsMenuPopupFrame *menuPopupFrame = mContent ? do_QueryFrame(mContent->GetPrimaryFrame()) : nullptr;
  226. return nsMenuPopupFrame::GetTriggerContent(menuPopupFrame);
  227. }
  228. Element*
  229. PopupBoxObject::GetAnchorNode() const
  230. {
  231. nsMenuPopupFrame *menuPopupFrame = mContent ? do_QueryFrame(mContent->GetPrimaryFrame()) : nullptr;
  232. if (!menuPopupFrame) {
  233. return nullptr;
  234. }
  235. nsIContent* anchor = menuPopupFrame->GetAnchor();
  236. return anchor && anchor->IsElement() ? anchor->AsElement() : nullptr;
  237. }
  238. already_AddRefed<DOMRect>
  239. PopupBoxObject::GetOuterScreenRect()
  240. {
  241. RefPtr<DOMRect> rect = new DOMRect(mContent);
  242. // Return an empty rectangle if the popup is not open.
  243. nsMenuPopupFrame *menuPopupFrame = do_QueryFrame(GetFrame(false));
  244. if (!menuPopupFrame || !menuPopupFrame->IsOpen()) {
  245. return rect.forget();
  246. }
  247. nsView* view = menuPopupFrame->GetView();
  248. if (view) {
  249. nsIWidget* widget = view->GetWidget();
  250. if (widget) {
  251. LayoutDeviceIntRect screenRect = widget->GetScreenBounds();
  252. int32_t pp = menuPopupFrame->PresContext()->AppUnitsPerDevPixel();
  253. rect->SetLayoutRect(LayoutDeviceIntRect::ToAppUnits(screenRect, pp));
  254. }
  255. }
  256. return rect.forget();
  257. }
  258. void
  259. PopupBoxObject::GetAlignmentPosition(nsString& positionStr)
  260. {
  261. positionStr.Truncate();
  262. // This needs to flush layout.
  263. nsMenuPopupFrame *menuPopupFrame = do_QueryFrame(GetFrame(true));
  264. if (!menuPopupFrame)
  265. return;
  266. int8_t position = menuPopupFrame->GetAlignmentPosition();
  267. switch (position) {
  268. case POPUPPOSITION_AFTERSTART:
  269. positionStr.AssignLiteral("after_start");
  270. break;
  271. case POPUPPOSITION_AFTEREND:
  272. positionStr.AssignLiteral("after_end");
  273. break;
  274. case POPUPPOSITION_BEFORESTART:
  275. positionStr.AssignLiteral("before_start");
  276. break;
  277. case POPUPPOSITION_BEFOREEND:
  278. positionStr.AssignLiteral("before_end");
  279. break;
  280. case POPUPPOSITION_STARTBEFORE:
  281. positionStr.AssignLiteral("start_before");
  282. break;
  283. case POPUPPOSITION_ENDBEFORE:
  284. positionStr.AssignLiteral("end_before");
  285. break;
  286. case POPUPPOSITION_STARTAFTER:
  287. positionStr.AssignLiteral("start_after");
  288. break;
  289. case POPUPPOSITION_ENDAFTER:
  290. positionStr.AssignLiteral("end_after");
  291. break;
  292. case POPUPPOSITION_OVERLAP:
  293. positionStr.AssignLiteral("overlap");
  294. break;
  295. case POPUPPOSITION_AFTERPOINTER:
  296. positionStr.AssignLiteral("after_pointer");
  297. break;
  298. case POPUPPOSITION_SELECTION:
  299. positionStr.AssignLiteral("selection");
  300. break;
  301. default:
  302. // Leave as an empty string.
  303. break;
  304. }
  305. }
  306. int32_t
  307. PopupBoxObject::AlignmentOffset()
  308. {
  309. nsMenuPopupFrame *menuPopupFrame = do_QueryFrame(GetFrame(false));
  310. if (!menuPopupFrame)
  311. return 0;
  312. int32_t pp = mozilla::AppUnitsPerCSSPixel();
  313. // Note that the offset might be along either the X or Y axis, but for the
  314. // sake of simplicity we use a point with only the X axis set so we can
  315. // use ToNearestPixels().
  316. nsPoint appOffset(menuPopupFrame->GetAlignmentOffset(), 0);
  317. nsIntPoint popupOffset = appOffset.ToNearestPixels(pp);
  318. return popupOffset.x;
  319. }
  320. void
  321. PopupBoxObject::SetConstraintRect(dom::DOMRectReadOnly& aRect)
  322. {
  323. nsMenuPopupFrame *menuPopupFrame = do_QueryFrame(GetFrame(false));
  324. if (menuPopupFrame) {
  325. menuPopupFrame->SetOverrideConstraintRect(
  326. LayoutDeviceIntRect::Truncate(aRect.Left(), aRect.Top(), aRect.Width(), aRect.Height()));
  327. }
  328. }
  329. } // namespace dom
  330. } // namespace mozilla
  331. // Creation Routine ///////////////////////////////////////////////////////////////////////
  332. nsresult
  333. NS_NewPopupBoxObject(nsIBoxObject** aResult)
  334. {
  335. *aResult = new mozilla::dom::PopupBoxObject();
  336. NS_ADDREF(*aResult);
  337. return NS_OK;
  338. }