nsView.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  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 "nsView.h"
  6. #include "mozilla/Attributes.h"
  7. #include "mozilla/BasicEvents.h"
  8. #include "mozilla/DebugOnly.h"
  9. #include "mozilla/IntegerPrintfMacros.h"
  10. #include "mozilla/Likely.h"
  11. #include "mozilla/Poison.h"
  12. #include "nsIWidget.h"
  13. #include "nsViewManager.h"
  14. #include "nsIFrame.h"
  15. #include "nsPresArena.h"
  16. #include "nsXULPopupManager.h"
  17. #include "nsIWidgetListener.h"
  18. #include "nsContentUtils.h" // for nsAutoScriptBlocker
  19. #include "mozilla/TimelineConsumers.h"
  20. #include "mozilla/CompositeTimelineMarker.h"
  21. using namespace mozilla;
  22. nsView::nsView(nsViewManager* aViewManager, nsViewVisibility aVisibility)
  23. {
  24. MOZ_COUNT_CTOR(nsView);
  25. mVis = aVisibility;
  26. // Views should be transparent by default. Not being transparent is
  27. // a promise that the view will paint all its pixels opaquely. Views
  28. // should make this promise explicitly by calling
  29. // SetViewContentTransparency.
  30. mVFlags = 0;
  31. mViewManager = aViewManager;
  32. mDirtyRegion = nullptr;
  33. mWidgetIsTopLevel = false;
  34. }
  35. void nsView::DropMouseGrabbing()
  36. {
  37. nsIPresShell* presShell = mViewManager->GetPresShell();
  38. if (presShell)
  39. presShell->ClearMouseCaptureOnView(this);
  40. }
  41. nsView::~nsView()
  42. {
  43. MOZ_COUNT_DTOR(nsView);
  44. while (GetFirstChild())
  45. {
  46. nsView* child = GetFirstChild();
  47. if (child->GetViewManager() == mViewManager) {
  48. child->Destroy();
  49. } else {
  50. // just unhook it. Someone else will want to destroy this.
  51. RemoveChild(child);
  52. }
  53. }
  54. if (mViewManager)
  55. {
  56. DropMouseGrabbing();
  57. nsView *rootView = mViewManager->GetRootView();
  58. if (rootView)
  59. {
  60. // Root views can have parents!
  61. if (mParent)
  62. {
  63. mViewManager->RemoveChild(this);
  64. }
  65. if (rootView == this)
  66. {
  67. // Inform the view manager that the root view has gone away...
  68. mViewManager->SetRootView(nullptr);
  69. }
  70. }
  71. else if (mParent)
  72. {
  73. mParent->RemoveChild(this);
  74. }
  75. mViewManager = nullptr;
  76. }
  77. else if (mParent)
  78. {
  79. mParent->RemoveChild(this);
  80. }
  81. if (mPreviousWindow) {
  82. mPreviousWindow->SetPreviouslyAttachedWidgetListener(nullptr);
  83. }
  84. // Destroy and release the widget
  85. DestroyWidget();
  86. delete mDirtyRegion;
  87. }
  88. class DestroyWidgetRunnable : public Runnable {
  89. public:
  90. NS_DECL_NSIRUNNABLE
  91. explicit DestroyWidgetRunnable(nsIWidget* aWidget) : mWidget(aWidget) {}
  92. private:
  93. nsCOMPtr<nsIWidget> mWidget;
  94. };
  95. NS_IMETHODIMP DestroyWidgetRunnable::Run()
  96. {
  97. mWidget->Destroy();
  98. mWidget = nullptr;
  99. return NS_OK;
  100. }
  101. void nsView::DestroyWidget()
  102. {
  103. if (mWindow)
  104. {
  105. // If we are not attached to a base window, we're going to tear down our
  106. // widget here. However, if we're attached to somebody elses widget, we
  107. // want to leave the widget alone: don't reset the client data or call
  108. // Destroy. Just clear our event view ptr and free our reference to it.
  109. if (mWidgetIsTopLevel) {
  110. mWindow->SetAttachedWidgetListener(nullptr);
  111. }
  112. else {
  113. mWindow->SetWidgetListener(nullptr);
  114. nsCOMPtr<nsIRunnable> widgetDestroyer =
  115. new DestroyWidgetRunnable(mWindow);
  116. // Don't leak if we happen to arrive here after the main thread
  117. // has disappeared.
  118. nsCOMPtr<nsIThread> mainThread = do_GetMainThread();
  119. if (mainThread) {
  120. mainThread->Dispatch(widgetDestroyer.forget(), NS_DISPATCH_NORMAL);
  121. }
  122. }
  123. mWindow = nullptr;
  124. }
  125. }
  126. nsView* nsView::GetViewFor(nsIWidget* aWidget)
  127. {
  128. NS_PRECONDITION(nullptr != aWidget, "null widget ptr");
  129. nsIWidgetListener* listener = aWidget->GetWidgetListener();
  130. if (listener) {
  131. nsView* view = listener->GetView();
  132. if (view)
  133. return view;
  134. }
  135. listener = aWidget->GetAttachedWidgetListener();
  136. return listener ? listener->GetView() : nullptr;
  137. }
  138. void nsView::Destroy()
  139. {
  140. this->~nsView();
  141. mozWritePoison(this, sizeof(*this));
  142. nsView::operator delete(this);
  143. }
  144. void nsView::SetPosition(nscoord aX, nscoord aY)
  145. {
  146. mDimBounds.x += aX - mPosX;
  147. mDimBounds.y += aY - mPosY;
  148. mPosX = aX;
  149. mPosY = aY;
  150. NS_ASSERTION(GetParent() || (aX == 0 && aY == 0),
  151. "Don't try to move the root widget to something non-zero");
  152. ResetWidgetBounds(true, false);
  153. }
  154. void nsView::ResetWidgetBounds(bool aRecurse, bool aForceSync)
  155. {
  156. if (mWindow) {
  157. if (!aForceSync) {
  158. // Don't change widget geometry synchronously, since that can
  159. // cause synchronous painting.
  160. mViewManager->PostPendingUpdate();
  161. } else {
  162. DoResetWidgetBounds(false, true);
  163. }
  164. return;
  165. }
  166. if (aRecurse) {
  167. // reposition any widgets under this view
  168. for (nsView* v = GetFirstChild(); v; v = v->GetNextSibling()) {
  169. v->ResetWidgetBounds(true, aForceSync);
  170. }
  171. }
  172. }
  173. bool nsView::IsEffectivelyVisible()
  174. {
  175. for (nsView* v = this; v; v = v->mParent) {
  176. if (v->GetVisibility() == nsViewVisibility_kHide)
  177. return false;
  178. }
  179. return true;
  180. }
  181. LayoutDeviceIntRect nsView::CalcWidgetBounds(nsWindowType aType)
  182. {
  183. int32_t p2a = mViewManager->AppUnitsPerDevPixel();
  184. nsRect viewBounds(mDimBounds);
  185. nsView* parent = GetParent();
  186. nsIWidget* parentWidget = nullptr;
  187. if (parent) {
  188. nsPoint offset;
  189. parentWidget = parent->GetNearestWidget(&offset, p2a);
  190. // make viewBounds be relative to the parent widget, in appunits
  191. viewBounds += offset;
  192. if (parentWidget && aType == eWindowType_popup &&
  193. IsEffectivelyVisible()) {
  194. // put offset into screen coordinates. (based on client area origin)
  195. LayoutDeviceIntPoint screenPoint = parentWidget->WidgetToScreenOffset();
  196. viewBounds += nsPoint(NSIntPixelsToAppUnits(screenPoint.x, p2a),
  197. NSIntPixelsToAppUnits(screenPoint.y, p2a));
  198. }
  199. }
  200. // Compute widget bounds in device pixels
  201. LayoutDeviceIntRect newBounds =
  202. LayoutDeviceIntRect::FromUnknownRect(viewBounds.ToNearestPixels(p2a));
  203. #if (MOZ_WIDGET_GTK == 3)
  204. // GTK 3 rounds widget coordinates to the nearest global "display
  205. // pixel" integer value. So we avoid fractional display pixel values by
  206. // rounding to the nearest value that won't yield a fractional display pixel.
  207. nsIWidget* widget = parentWidget ? parentWidget : mWindow.get();
  208. uint32_t round;
  209. if (aType == eWindowType_popup && widget &&
  210. ((round = widget->RoundsWidgetCoordinatesTo()) > 1)) {
  211. LayoutDeviceIntSize pixelRoundedSize = newBounds.Size();
  212. // round the top left and bottom right to the nearest round pixel
  213. newBounds.x = NSToIntRoundUp(NSAppUnitsToDoublePixels(viewBounds.x, p2a) / round) * round;
  214. newBounds.y = NSToIntRoundUp(NSAppUnitsToDoublePixels(viewBounds.y, p2a) / round) * round;
  215. newBounds.width =
  216. NSToIntRoundUp(NSAppUnitsToDoublePixels(viewBounds.XMost(), p2a) / round) * round - newBounds.x;
  217. newBounds.height =
  218. NSToIntRoundUp(NSAppUnitsToDoublePixels(viewBounds.YMost(), p2a) / round) * round - newBounds.y;
  219. // but if that makes the widget larger then our frame may not paint the
  220. // extra pixels, so reduce the size to the nearest round value
  221. if (newBounds.width > pixelRoundedSize.width) {
  222. newBounds.width -= round;
  223. }
  224. if (newBounds.height > pixelRoundedSize.height) {
  225. newBounds.height -= round;
  226. }
  227. }
  228. #endif
  229. // Compute where the top-left of our widget ended up relative to the parent
  230. // widget, in appunits.
  231. nsPoint roundedOffset(NSIntPixelsToAppUnits(newBounds.x, p2a),
  232. NSIntPixelsToAppUnits(newBounds.y, p2a));
  233. // mViewToWidgetOffset is added to coordinates relative to the view origin
  234. // to get coordinates relative to the widget.
  235. // The view origin, relative to the parent widget, is at
  236. // (mPosX,mPosY) - mDimBounds.TopLeft() + viewBounds.TopLeft().
  237. // Our widget, relative to the parent widget, is roundedOffset.
  238. mViewToWidgetOffset = nsPoint(mPosX, mPosY)
  239. - mDimBounds.TopLeft() + viewBounds.TopLeft() - roundedOffset;
  240. return newBounds;
  241. }
  242. void nsView::DoResetWidgetBounds(bool aMoveOnly,
  243. bool aInvalidateChangedSize) {
  244. // The geometry of a root view's widget is controlled externally,
  245. // NOT by sizing or positioning the view
  246. if (mViewManager->GetRootView() == this) {
  247. return;
  248. }
  249. NS_PRECONDITION(mWindow, "Why was this called??");
  250. // Hold this ref to make sure it stays alive.
  251. nsCOMPtr<nsIWidget> widget = mWindow;
  252. // Stash a copy of these and use them so we can handle this being deleted (say
  253. // from sync painting/flushing from Show/Move/Resize on the widget).
  254. LayoutDeviceIntRect newBounds;
  255. RefPtr<nsDeviceContext> dx = mViewManager->GetDeviceContext();
  256. nsWindowType type = widget->WindowType();
  257. LayoutDeviceIntRect curBounds = widget->GetClientBounds();
  258. bool invisiblePopup = type == eWindowType_popup &&
  259. ((curBounds.IsEmpty() && mDimBounds.IsEmpty()) ||
  260. mVis == nsViewVisibility_kHide);
  261. if (invisiblePopup) {
  262. // We're going to hit the early exit below, avoid calling CalcWidgetBounds.
  263. } else {
  264. newBounds = CalcWidgetBounds(type);
  265. }
  266. bool curVisibility = widget->IsVisible();
  267. bool newVisibility = IsEffectivelyVisible();
  268. if (curVisibility && !newVisibility) {
  269. widget->Show(false);
  270. }
  271. if (invisiblePopup) {
  272. // Don't manipulate empty or hidden popup widgets. For example there's no
  273. // point moving hidden comboboxes around, or doing X server roundtrips
  274. // to compute their true screen position. This could mean that WidgetToScreen
  275. // operations on these widgets don't return up-to-date values, but popup
  276. // positions aren't reliable anyway because of correction to be on or off-screen.
  277. return;
  278. }
  279. bool changedPos = curBounds.TopLeft() != newBounds.TopLeft();
  280. bool changedSize = curBounds.Size() != newBounds.Size();
  281. // Child views are never attached to top level widgets, this is safe.
  282. // Coordinates are converted to desktop pixels for window Move/Resize APIs,
  283. // because of the potential for device-pixel coordinate spaces for mixed
  284. // hidpi/lodpi screens to overlap each other and result in bad placement
  285. // (bug 814434).
  286. DesktopToLayoutDeviceScale scale = dx->GetDesktopToDeviceScale();
  287. DesktopRect deskRect = newBounds / scale;
  288. if (changedPos) {
  289. if (changedSize && !aMoveOnly) {
  290. widget->ResizeClient(deskRect.x, deskRect.y,
  291. deskRect.width, deskRect.height,
  292. aInvalidateChangedSize);
  293. } else {
  294. widget->MoveClient(deskRect.x, deskRect.y);
  295. }
  296. } else {
  297. if (changedSize && !aMoveOnly) {
  298. widget->ResizeClient(deskRect.width, deskRect.height,
  299. aInvalidateChangedSize);
  300. } // else do nothing!
  301. }
  302. if (!curVisibility && newVisibility) {
  303. widget->Show(true);
  304. }
  305. }
  306. void nsView::SetDimensions(const nsRect& aRect, bool aPaint, bool aResizeWidget)
  307. {
  308. nsRect dims = aRect;
  309. dims.MoveBy(mPosX, mPosY);
  310. // Don't use nsRect's operator== here, since it returns true when
  311. // both rects are empty even if they have different widths and we
  312. // have cases where that sort of thing matters to us.
  313. if (mDimBounds.TopLeft() == dims.TopLeft() &&
  314. mDimBounds.Size() == dims.Size()) {
  315. return;
  316. }
  317. mDimBounds = dims;
  318. if (aResizeWidget) {
  319. ResetWidgetBounds(false, false);
  320. }
  321. }
  322. void nsView::NotifyEffectiveVisibilityChanged(bool aEffectivelyVisible)
  323. {
  324. if (!aEffectivelyVisible)
  325. {
  326. DropMouseGrabbing();
  327. }
  328. SetForcedRepaint(true);
  329. if (nullptr != mWindow)
  330. {
  331. ResetWidgetBounds(false, false);
  332. }
  333. for (nsView* child = mFirstChild; child; child = child->mNextSibling) {
  334. if (child->mVis == nsViewVisibility_kHide) {
  335. // It was effectively hidden and still is
  336. continue;
  337. }
  338. // Our child is visible if we are
  339. child->NotifyEffectiveVisibilityChanged(aEffectivelyVisible);
  340. }
  341. }
  342. void nsView::SetVisibility(nsViewVisibility aVisibility)
  343. {
  344. mVis = aVisibility;
  345. NotifyEffectiveVisibilityChanged(IsEffectivelyVisible());
  346. }
  347. void nsView::SetFloating(bool aFloatingView)
  348. {
  349. if (aFloatingView)
  350. mVFlags |= NS_VIEW_FLAG_FLOATING;
  351. else
  352. mVFlags &= ~NS_VIEW_FLAG_FLOATING;
  353. }
  354. void nsView::InvalidateHierarchy()
  355. {
  356. if (mViewManager->GetRootView() == this)
  357. mViewManager->InvalidateHierarchy();
  358. for (nsView *child = mFirstChild; child; child = child->GetNextSibling())
  359. child->InvalidateHierarchy();
  360. }
  361. void nsView::InsertChild(nsView *aChild, nsView *aSibling)
  362. {
  363. NS_PRECONDITION(nullptr != aChild, "null ptr");
  364. if (nullptr != aChild)
  365. {
  366. if (nullptr != aSibling)
  367. {
  368. #ifdef DEBUG
  369. NS_ASSERTION(aSibling->GetParent() == this, "tried to insert view with invalid sibling");
  370. #endif
  371. //insert after sibling
  372. aChild->SetNextSibling(aSibling->GetNextSibling());
  373. aSibling->SetNextSibling(aChild);
  374. }
  375. else
  376. {
  377. aChild->SetNextSibling(mFirstChild);
  378. mFirstChild = aChild;
  379. }
  380. aChild->SetParent(this);
  381. // If we just inserted a root view, then update the RootViewManager
  382. // on all view managers in the new subtree.
  383. nsViewManager *vm = aChild->GetViewManager();
  384. if (vm->GetRootView() == aChild)
  385. {
  386. aChild->InvalidateHierarchy();
  387. }
  388. }
  389. }
  390. void nsView::RemoveChild(nsView *child)
  391. {
  392. NS_PRECONDITION(nullptr != child, "null ptr");
  393. if (nullptr != child)
  394. {
  395. nsView* prevKid = nullptr;
  396. nsView* kid = mFirstChild;
  397. DebugOnly<bool> found = false;
  398. while (nullptr != kid) {
  399. if (kid == child) {
  400. if (nullptr != prevKid) {
  401. prevKid->SetNextSibling(kid->GetNextSibling());
  402. } else {
  403. mFirstChild = kid->GetNextSibling();
  404. }
  405. child->SetParent(nullptr);
  406. found = true;
  407. break;
  408. }
  409. prevKid = kid;
  410. kid = kid->GetNextSibling();
  411. }
  412. NS_ASSERTION(found, "tried to remove non child");
  413. // If we just removed a root view, then update the RootViewManager
  414. // on all view managers in the removed subtree.
  415. nsViewManager *vm = child->GetViewManager();
  416. if (vm->GetRootView() == child)
  417. {
  418. child->InvalidateHierarchy();
  419. }
  420. }
  421. }
  422. // Native widgets ultimately just can't deal with the awesome power of
  423. // CSS2 z-index. However, we set the z-index on the widget anyway
  424. // because in many simple common cases the widgets do end up in the
  425. // right order. We set each widget's z-index to the z-index of the
  426. // nearest ancestor that has non-auto z-index.
  427. static void UpdateNativeWidgetZIndexes(nsView* aView, int32_t aZIndex)
  428. {
  429. if (aView->HasWidget()) {
  430. nsIWidget* widget = aView->GetWidget();
  431. if (widget->GetZIndex() != aZIndex) {
  432. widget->SetZIndex(aZIndex);
  433. }
  434. } else {
  435. for (nsView* v = aView->GetFirstChild(); v; v = v->GetNextSibling()) {
  436. if (v->GetZIndexIsAuto()) {
  437. UpdateNativeWidgetZIndexes(v, aZIndex);
  438. }
  439. }
  440. }
  441. }
  442. static int32_t FindNonAutoZIndex(nsView* aView)
  443. {
  444. while (aView) {
  445. if (!aView->GetZIndexIsAuto()) {
  446. return aView->GetZIndex();
  447. }
  448. aView = aView->GetParent();
  449. }
  450. return 0;
  451. }
  452. struct DefaultWidgetInitData : public nsWidgetInitData {
  453. DefaultWidgetInitData() : nsWidgetInitData()
  454. {
  455. mWindowType = eWindowType_child;
  456. clipChildren = true;
  457. clipSiblings = true;
  458. }
  459. };
  460. nsresult nsView::CreateWidget(nsWidgetInitData *aWidgetInitData,
  461. bool aEnableDragDrop,
  462. bool aResetVisibility)
  463. {
  464. AssertNoWindow();
  465. MOZ_ASSERT(!aWidgetInitData ||
  466. aWidgetInitData->mWindowType != eWindowType_popup,
  467. "Use CreateWidgetForPopup");
  468. DefaultWidgetInitData defaultInitData;
  469. bool initDataPassedIn = !!aWidgetInitData;
  470. aWidgetInitData = aWidgetInitData ? aWidgetInitData : &defaultInitData;
  471. defaultInitData.mListenForResizes =
  472. (!initDataPassedIn && GetParent() &&
  473. GetParent()->GetViewManager() != mViewManager);
  474. LayoutDeviceIntRect trect = CalcWidgetBounds(aWidgetInitData->mWindowType);
  475. nsIWidget* parentWidget =
  476. GetParent() ? GetParent()->GetNearestWidget(nullptr) : nullptr;
  477. if (!parentWidget) {
  478. NS_ERROR("nsView::CreateWidget without suitable parent widget??");
  479. return NS_ERROR_FAILURE;
  480. }
  481. // XXX: using aForceUseIWidgetParent=true to preserve previous
  482. // semantics. It's not clear that it's actually needed.
  483. mWindow = parentWidget->CreateChild(trect, aWidgetInitData, true);
  484. if (!mWindow) {
  485. return NS_ERROR_FAILURE;
  486. }
  487. InitializeWindow(aEnableDragDrop, aResetVisibility);
  488. return NS_OK;
  489. }
  490. nsresult nsView::CreateWidgetForParent(nsIWidget* aParentWidget,
  491. nsWidgetInitData *aWidgetInitData,
  492. bool aEnableDragDrop,
  493. bool aResetVisibility)
  494. {
  495. AssertNoWindow();
  496. MOZ_ASSERT(!aWidgetInitData ||
  497. aWidgetInitData->mWindowType != eWindowType_popup,
  498. "Use CreateWidgetForPopup");
  499. MOZ_ASSERT(aParentWidget, "Parent widget required");
  500. DefaultWidgetInitData defaultInitData;
  501. aWidgetInitData = aWidgetInitData ? aWidgetInitData : &defaultInitData;
  502. LayoutDeviceIntRect trect = CalcWidgetBounds(aWidgetInitData->mWindowType);
  503. mWindow = aParentWidget->CreateChild(trect, aWidgetInitData);
  504. if (!mWindow) {
  505. return NS_ERROR_FAILURE;
  506. }
  507. InitializeWindow(aEnableDragDrop, aResetVisibility);
  508. return NS_OK;
  509. }
  510. nsresult nsView::CreateWidgetForPopup(nsWidgetInitData *aWidgetInitData,
  511. nsIWidget* aParentWidget,
  512. bool aEnableDragDrop,
  513. bool aResetVisibility)
  514. {
  515. AssertNoWindow();
  516. MOZ_ASSERT(aWidgetInitData, "Widget init data required");
  517. MOZ_ASSERT(aWidgetInitData->mWindowType == eWindowType_popup,
  518. "Use one of the other CreateWidget methods");
  519. LayoutDeviceIntRect trect = CalcWidgetBounds(aWidgetInitData->mWindowType);
  520. // XXX/cjones: having these two separate creation cases seems ... um
  521. // ... unnecessary, but it's the way the old code did it. Please
  522. // unify them by first finding a suitable parent nsIWidget, then
  523. // getting rid of aForceUseIWidgetParent.
  524. if (aParentWidget) {
  525. // XXX: using aForceUseIWidgetParent=true to preserve previous
  526. // semantics. It's not clear that it's actually needed.
  527. mWindow = aParentWidget->CreateChild(trect, aWidgetInitData, true);
  528. }
  529. else {
  530. nsIWidget* nearestParent = GetParent() ? GetParent()->GetNearestWidget(nullptr)
  531. : nullptr;
  532. if (!nearestParent) {
  533. // Without a parent, we can't make a popup. This can happen
  534. // when printing
  535. return NS_ERROR_FAILURE;
  536. }
  537. mWindow = nearestParent->CreateChild(trect, aWidgetInitData);
  538. }
  539. if (!mWindow) {
  540. return NS_ERROR_FAILURE;
  541. }
  542. InitializeWindow(aEnableDragDrop, aResetVisibility);
  543. return NS_OK;
  544. }
  545. void
  546. nsView::InitializeWindow(bool aEnableDragDrop, bool aResetVisibility)
  547. {
  548. MOZ_ASSERT(mWindow, "Must have a window to initialize");
  549. mWindow->SetWidgetListener(this);
  550. if (aEnableDragDrop) {
  551. mWindow->EnableDragDrop(true);
  552. }
  553. // propagate the z-index to the widget.
  554. UpdateNativeWidgetZIndexes(this, FindNonAutoZIndex(this));
  555. //make sure visibility state is accurate
  556. if (aResetVisibility) {
  557. SetVisibility(GetVisibility());
  558. }
  559. }
  560. void
  561. nsView::SetNeedsWindowPropertiesSync()
  562. {
  563. mNeedsWindowPropertiesSync = true;
  564. if (mViewManager) {
  565. mViewManager->PostPendingUpdate();
  566. }
  567. }
  568. // Attach to a top level widget and start receiving mirrored events.
  569. nsresult nsView::AttachToTopLevelWidget(nsIWidget* aWidget)
  570. {
  571. NS_PRECONDITION(nullptr != aWidget, "null widget ptr");
  572. /// XXXjimm This is a temporary workaround to an issue w/document
  573. // viewer (bug 513162).
  574. nsIWidgetListener* listener = aWidget->GetAttachedWidgetListener();
  575. if (listener) {
  576. nsView *oldView = listener->GetView();
  577. if (oldView) {
  578. oldView->DetachFromTopLevelWidget();
  579. }
  580. }
  581. // Note, the previous device context will be released. Detaching
  582. // will not restore the old one.
  583. aWidget->AttachViewToTopLevel(!nsIWidget::UsePuppetWidgets());
  584. mWindow = aWidget;
  585. mWindow->SetAttachedWidgetListener(this);
  586. mWindow->EnableDragDrop(true);
  587. mWidgetIsTopLevel = true;
  588. // Refresh the view bounds
  589. CalcWidgetBounds(mWindow->WindowType());
  590. return NS_OK;
  591. }
  592. // Detach this view from an attached widget.
  593. nsresult nsView::DetachFromTopLevelWidget()
  594. {
  595. NS_PRECONDITION(mWidgetIsTopLevel, "Not attached currently!");
  596. NS_PRECONDITION(mWindow, "null mWindow for DetachFromTopLevelWidget!");
  597. mWindow->SetAttachedWidgetListener(nullptr);
  598. nsIWidgetListener* listener = mWindow->GetPreviouslyAttachedWidgetListener();
  599. if (listener && listener->GetView()) {
  600. // Ensure the listener doesn't think it's being used anymore
  601. listener->GetView()->SetPreviousWidget(nullptr);
  602. }
  603. // If the new view's frame is paint suppressed then the window
  604. // will want to use us instead until that's done
  605. mWindow->SetPreviouslyAttachedWidgetListener(this);
  606. mPreviousWindow = mWindow;
  607. mWindow = nullptr;
  608. mWidgetIsTopLevel = false;
  609. return NS_OK;
  610. }
  611. void nsView::SetZIndex(bool aAuto, int32_t aZIndex)
  612. {
  613. bool oldIsAuto = GetZIndexIsAuto();
  614. mVFlags = (mVFlags & ~NS_VIEW_FLAG_AUTO_ZINDEX) | (aAuto ? NS_VIEW_FLAG_AUTO_ZINDEX : 0);
  615. mZIndex = aZIndex;
  616. if (HasWidget() || !oldIsAuto || !aAuto) {
  617. UpdateNativeWidgetZIndexes(this, FindNonAutoZIndex(this));
  618. }
  619. }
  620. void nsView::AssertNoWindow()
  621. {
  622. // XXX: it would be nice to make this a strong assert
  623. if (MOZ_UNLIKELY(mWindow)) {
  624. NS_ERROR("We already have a window for this view? BAD");
  625. mWindow->SetWidgetListener(nullptr);
  626. mWindow->Destroy();
  627. mWindow = nullptr;
  628. }
  629. }
  630. //
  631. // internal window creation functions
  632. //
  633. void nsView::AttachWidgetEventHandler(nsIWidget* aWidget)
  634. {
  635. #ifdef DEBUG
  636. NS_ASSERTION(!aWidget->GetWidgetListener(), "Already have a widget listener");
  637. #endif
  638. aWidget->SetWidgetListener(this);
  639. }
  640. void nsView::DetachWidgetEventHandler(nsIWidget* aWidget)
  641. {
  642. NS_ASSERTION(!aWidget->GetWidgetListener() ||
  643. aWidget->GetWidgetListener()->GetView() == this, "Wrong view");
  644. aWidget->SetWidgetListener(nullptr);
  645. }
  646. #ifdef DEBUG
  647. void nsView::List(FILE* out, int32_t aIndent) const
  648. {
  649. int32_t i;
  650. for (i = aIndent; --i >= 0; ) fputs(" ", out);
  651. fprintf(out, "%p ", (void*)this);
  652. if (nullptr != mWindow) {
  653. nscoord p2a = mViewManager->AppUnitsPerDevPixel();
  654. LayoutDeviceIntRect rect = mWindow->GetClientBounds();
  655. nsRect windowBounds = LayoutDeviceIntRect::ToAppUnits(rect, p2a);
  656. rect = mWindow->GetBounds();
  657. nsRect nonclientBounds = LayoutDeviceIntRect::ToAppUnits(rect, p2a);
  658. nsrefcnt widgetRefCnt = mWindow.get()->AddRef() - 1;
  659. mWindow.get()->Release();
  660. int32_t Z = mWindow->GetZIndex();
  661. fprintf(out, "(widget=%p[%" PRIuPTR "] z=%d pos={%d,%d,%d,%d}) ",
  662. (void*)mWindow, widgetRefCnt, Z,
  663. nonclientBounds.x, nonclientBounds.y,
  664. windowBounds.width, windowBounds.height);
  665. }
  666. nsRect brect = GetBounds();
  667. fprintf(out, "{%d,%d,%d,%d}",
  668. brect.x, brect.y, brect.width, brect.height);
  669. fprintf(out, " z=%d vis=%d frame=%p <\n",
  670. mZIndex, mVis, static_cast<void*>(mFrame));
  671. for (nsView* kid = mFirstChild; kid; kid = kid->GetNextSibling()) {
  672. NS_ASSERTION(kid->GetParent() == this, "incorrect parent");
  673. kid->List(out, aIndent + 1);
  674. }
  675. for (i = aIndent; --i >= 0; ) fputs(" ", out);
  676. fputs(">\n", out);
  677. }
  678. #endif // DEBUG
  679. nsPoint nsView::GetOffsetTo(const nsView* aOther) const
  680. {
  681. return GetOffsetTo(aOther, GetViewManager()->AppUnitsPerDevPixel());
  682. }
  683. nsPoint nsView::GetOffsetTo(const nsView* aOther, const int32_t aAPD) const
  684. {
  685. MOZ_ASSERT(GetParent() || !aOther || aOther->GetParent() || this == aOther,
  686. "caller of (outer) GetOffsetTo must not pass unrelated views");
  687. // We accumulate the final result in offset
  688. nsPoint offset(0, 0);
  689. // The offset currently accumulated at the current APD
  690. nsPoint docOffset(0, 0);
  691. const nsView* v = this;
  692. nsViewManager* currVM = v->GetViewManager();
  693. int32_t currAPD = currVM->AppUnitsPerDevPixel();
  694. const nsView* root = nullptr;
  695. for ( ; v != aOther && v; root = v, v = v->GetParent()) {
  696. nsViewManager* newVM = v->GetViewManager();
  697. if (newVM != currVM) {
  698. int32_t newAPD = newVM->AppUnitsPerDevPixel();
  699. if (newAPD != currAPD) {
  700. offset += docOffset.ScaleToOtherAppUnits(currAPD, aAPD);
  701. docOffset.x = docOffset.y = 0;
  702. currAPD = newAPD;
  703. }
  704. currVM = newVM;
  705. }
  706. docOffset += v->GetPosition();
  707. }
  708. offset += docOffset.ScaleToOtherAppUnits(currAPD, aAPD);
  709. if (v != aOther) {
  710. // Looks like aOther wasn't an ancestor of |this|. So now we have
  711. // the root-VM-relative position of |this| in |offset|. Get the
  712. // root-VM-relative position of aOther and subtract it.
  713. nsPoint negOffset = aOther->GetOffsetTo(root, aAPD);
  714. offset -= negOffset;
  715. }
  716. return offset;
  717. }
  718. nsPoint nsView::GetOffsetToWidget(nsIWidget* aWidget) const
  719. {
  720. nsPoint pt;
  721. // Get the view for widget
  722. nsView* widgetView = GetViewFor(aWidget);
  723. if (!widgetView) {
  724. return pt;
  725. }
  726. // Get the offset to the widget view in the widget view's APD
  727. // We get the offset in the widget view's APD first and then convert to our
  728. // APD afterwards so that we can include the widget view's ViewToWidgetOffset
  729. // in the sum in its native APD, and then convert the whole thing to our APD
  730. // so that we don't have to convert the APD of the relatively small
  731. // ViewToWidgetOffset by itself with a potentially large relative rounding
  732. // error.
  733. pt = -widgetView->GetOffsetTo(this);
  734. // Add in the offset to the widget.
  735. pt += widgetView->ViewToWidgetOffset();
  736. // Convert to our appunits.
  737. int32_t widgetAPD = widgetView->GetViewManager()->AppUnitsPerDevPixel();
  738. int32_t ourAPD = GetViewManager()->AppUnitsPerDevPixel();
  739. pt = pt.ScaleToOtherAppUnits(widgetAPD, ourAPD);
  740. return pt;
  741. }
  742. nsIWidget* nsView::GetNearestWidget(nsPoint* aOffset) const
  743. {
  744. return GetNearestWidget(aOffset, GetViewManager()->AppUnitsPerDevPixel());
  745. }
  746. nsIWidget* nsView::GetNearestWidget(nsPoint* aOffset, const int32_t aAPD) const
  747. {
  748. // aOffset is based on the view's position, which ignores any chrome on
  749. // attached parent widgets.
  750. // We accumulate the final result in pt
  751. nsPoint pt(0, 0);
  752. // The offset currently accumulated at the current APD
  753. nsPoint docPt(0,0);
  754. const nsView* v = this;
  755. nsViewManager* currVM = v->GetViewManager();
  756. int32_t currAPD = currVM->AppUnitsPerDevPixel();
  757. for ( ; v && !v->HasWidget(); v = v->GetParent()) {
  758. nsViewManager* newVM = v->GetViewManager();
  759. if (newVM != currVM) {
  760. int32_t newAPD = newVM->AppUnitsPerDevPixel();
  761. if (newAPD != currAPD) {
  762. pt += docPt.ScaleToOtherAppUnits(currAPD, aAPD);
  763. docPt.x = docPt.y = 0;
  764. currAPD = newAPD;
  765. }
  766. currVM = newVM;
  767. }
  768. docPt += v->GetPosition();
  769. }
  770. if (!v) {
  771. if (aOffset) {
  772. pt += docPt.ScaleToOtherAppUnits(currAPD, aAPD);
  773. *aOffset = pt;
  774. }
  775. return nullptr;
  776. }
  777. // pt is now the offset from v's origin to this view's origin.
  778. // We add the ViewToWidgetOffset to get the offset to the widget.
  779. if (aOffset) {
  780. docPt += v->ViewToWidgetOffset();
  781. pt += docPt.ScaleToOtherAppUnits(currAPD, aAPD);
  782. *aOffset = pt;
  783. }
  784. return v->GetWidget();
  785. }
  786. bool nsView::IsRoot() const
  787. {
  788. NS_ASSERTION(mViewManager != nullptr," View manager is null in nsView::IsRoot()");
  789. return mViewManager->GetRootView() == this;
  790. }
  791. nsRect
  792. nsView::GetBoundsInParentUnits() const
  793. {
  794. nsView* parent = GetParent();
  795. nsViewManager* VM = GetViewManager();
  796. if (this != VM->GetRootView() || !parent) {
  797. return mDimBounds;
  798. }
  799. int32_t ourAPD = VM->AppUnitsPerDevPixel();
  800. int32_t parentAPD = parent->GetViewManager()->AppUnitsPerDevPixel();
  801. return mDimBounds.ScaleToOtherAppUnitsRoundOut(ourAPD, parentAPD);
  802. }
  803. nsPoint
  804. nsView::ConvertFromParentCoords(nsPoint aPt) const
  805. {
  806. const nsView* parent = GetParent();
  807. if (parent) {
  808. aPt = aPt.ScaleToOtherAppUnits(
  809. parent->GetViewManager()->AppUnitsPerDevPixel(),
  810. GetViewManager()->AppUnitsPerDevPixel());
  811. }
  812. aPt -= GetPosition();
  813. return aPt;
  814. }
  815. static bool
  816. IsPopupWidget(nsIWidget* aWidget)
  817. {
  818. return (aWidget->WindowType() == eWindowType_popup);
  819. }
  820. nsIPresShell*
  821. nsView::GetPresShell()
  822. {
  823. return GetViewManager()->GetPresShell();
  824. }
  825. bool
  826. nsView::WindowMoved(nsIWidget* aWidget, int32_t x, int32_t y)
  827. {
  828. nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
  829. if (pm && IsPopupWidget(aWidget)) {
  830. pm->PopupMoved(mFrame, nsIntPoint(x, y));
  831. return true;
  832. }
  833. return false;
  834. }
  835. bool
  836. nsView::WindowResized(nsIWidget* aWidget, int32_t aWidth, int32_t aHeight)
  837. {
  838. // The root view may not be set if this is the resize associated with
  839. // window creation
  840. SetForcedRepaint(true);
  841. if (this == mViewManager->GetRootView()) {
  842. RefPtr<nsDeviceContext> devContext = mViewManager->GetDeviceContext();
  843. // ensure DPI is up-to-date, in case of window being opened and sized
  844. // on a non-default-dpi display (bug 829963)
  845. devContext->CheckDPIChange();
  846. int32_t p2a = devContext->AppUnitsPerDevPixel();
  847. mViewManager->SetWindowDimensions(NSIntPixelsToAppUnits(aWidth, p2a),
  848. NSIntPixelsToAppUnits(aHeight, p2a));
  849. nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
  850. if (pm) {
  851. nsIPresShell* presShell = mViewManager->GetPresShell();
  852. if (presShell && presShell->GetDocument()) {
  853. pm->AdjustPopupsOnWindowChange(presShell);
  854. }
  855. }
  856. return true;
  857. }
  858. else if (IsPopupWidget(aWidget)) {
  859. nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
  860. if (pm) {
  861. pm->PopupResized(mFrame, LayoutDeviceIntSize(aWidth, aHeight));
  862. return true;
  863. }
  864. }
  865. return false;
  866. }
  867. bool
  868. nsView::RequestWindowClose(nsIWidget* aWidget)
  869. {
  870. if (mFrame && IsPopupWidget(aWidget) &&
  871. mFrame->GetType() == nsGkAtoms::menuPopupFrame) {
  872. nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
  873. if (pm) {
  874. pm->HidePopup(mFrame->GetContent(), false, true, false, false);
  875. return true;
  876. }
  877. }
  878. return false;
  879. }
  880. void
  881. nsView::WillPaintWindow(nsIWidget* aWidget)
  882. {
  883. RefPtr<nsViewManager> vm = mViewManager;
  884. vm->WillPaintWindow(aWidget);
  885. }
  886. bool
  887. nsView::PaintWindow(nsIWidget* aWidget, LayoutDeviceIntRegion aRegion)
  888. {
  889. NS_ASSERTION(this == nsView::GetViewFor(aWidget), "wrong view for widget?");
  890. RefPtr<nsViewManager> vm = mViewManager;
  891. bool result = vm->PaintWindow(aWidget, aRegion);
  892. return result;
  893. }
  894. void
  895. nsView::DidPaintWindow()
  896. {
  897. RefPtr<nsViewManager> vm = mViewManager;
  898. vm->DidPaintWindow();
  899. }
  900. void
  901. nsView::DidCompositeWindow(uint64_t aTransactionId,
  902. const TimeStamp& aCompositeStart,
  903. const TimeStamp& aCompositeEnd)
  904. {
  905. nsIPresShell* presShell = mViewManager->GetPresShell();
  906. if (presShell) {
  907. nsAutoScriptBlocker scriptBlocker;
  908. nsPresContext* context = presShell->GetPresContext();
  909. nsRootPresContext* rootContext = context->GetRootPresContext();
  910. MOZ_ASSERT(rootContext, "rootContext must be valid.");
  911. rootContext->NotifyDidPaintForSubtree(nsIPresShell::PAINT_COMPOSITE, aTransactionId,
  912. aCompositeEnd);
  913. // If the two timestamps are identical, this was likely a fake composite
  914. // event which wouldn't be terribly useful to display.
  915. if (aCompositeStart == aCompositeEnd) {
  916. return;
  917. }
  918. nsIDocShell* docShell = context->GetDocShell();
  919. RefPtr<TimelineConsumers> timelines = TimelineConsumers::Get();
  920. if (timelines && timelines->HasConsumer(docShell)) {
  921. timelines->AddMarkerForDocShell(docShell,
  922. MakeUnique<CompositeTimelineMarker>(aCompositeStart, MarkerTracingType::START));
  923. timelines->AddMarkerForDocShell(docShell,
  924. MakeUnique<CompositeTimelineMarker>(aCompositeEnd, MarkerTracingType::END));
  925. }
  926. }
  927. }
  928. void
  929. nsView::RequestRepaint()
  930. {
  931. nsIPresShell* presShell = mViewManager->GetPresShell();
  932. if (presShell) {
  933. presShell->ScheduleViewManagerFlush();
  934. }
  935. }
  936. nsEventStatus
  937. nsView::HandleEvent(WidgetGUIEvent* aEvent,
  938. bool aUseAttachedEvents)
  939. {
  940. NS_PRECONDITION(nullptr != aEvent->mWidget, "null widget ptr");
  941. nsEventStatus result = nsEventStatus_eIgnore;
  942. nsView* view;
  943. if (aUseAttachedEvents) {
  944. nsIWidgetListener* listener = aEvent->mWidget->GetAttachedWidgetListener();
  945. view = listener ? listener->GetView() : nullptr;
  946. }
  947. else {
  948. view = GetViewFor(aEvent->mWidget);
  949. }
  950. if (view) {
  951. RefPtr<nsViewManager> vm = view->GetViewManager();
  952. vm->DispatchEvent(aEvent, view, &result);
  953. }
  954. return result;
  955. }
  956. bool
  957. nsView::IsPrimaryFramePaintSuppressed()
  958. {
  959. return mFrame ? mFrame->PresContext()->PresShell()->IsPaintingSuppressed() : false;
  960. }