forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnsViewManager.cpp
2434 lines (2061 loc) · 76.8 KB
/
nsViewManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Patrick C. Beard <[email protected]>
* Kevin McCluskey <[email protected]>
* Robert O'Callahan <[email protected]>
* Roland Mainz <[email protected]>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#define PL_ARENA_CONST_ALIGN_MASK (sizeof(void*)-1)
#include "plarena.h"
#include "nsAutoPtr.h"
#include "nsViewManager.h"
#include "nsIRenderingContext.h"
#include "nsIDeviceContext.h"
#include "nsGfxCIID.h"
#include "nsIScrollableView.h"
#include "nsView.h"
#include "nsISupportsArray.h"
#include "nsCOMPtr.h"
#include "nsIServiceManager.h"
#include "nsGUIEvent.h"
#include "nsIPrefBranch.h"
#include "nsIPrefService.h"
#include "nsRegion.h"
#include "nsInt64.h"
#include "nsScrollPortView.h"
#include "nsHashtable.h"
#include "nsCOMArray.h"
#include "nsThreadUtils.h"
#include "nsContentUtils.h"
#include "gfxContext.h"
#define NS_STATIC_FOCUS_SUPPRESSOR
#include "nsIFocusEventSuppressor.h"
static NS_DEFINE_IID(kBlenderCID, NS_BLENDER_CID);
static NS_DEFINE_IID(kRegionCID, NS_REGION_CID);
static NS_DEFINE_IID(kRenderingContextCID, NS_RENDERING_CONTEXT_CID);
/**
XXX TODO XXX
DeCOMify newly private methods
Optimize view storage
*/
/**
A note about platform assumptions:
We assume all native widgets are opaque.
We assume that a widget is z-ordered on top of its parent.
We do NOT assume anything about the relative z-ordering of sibling widgets. Even though
we ask for a specific z-order, we don't assume that widget z-ordering actually works.
*/
#define NSCOORD_NONE PR_INT32_MIN
#ifdef NS_VM_PERF_METRICS
#include "nsITimeRecorder.h"
#endif
#ifdef DEBUG_smaug
#define DEBUG_FOCUS_SUPPRESSION
#endif
//-------------- Begin Invalidate Event Definition ------------------------
class nsInvalidateEvent : public nsViewManagerEvent {
public:
nsInvalidateEvent(nsViewManager *vm) : nsViewManagerEvent(vm) {}
NS_IMETHOD Run() {
if (mViewManager)
mViewManager->ProcessInvalidateEvent();
return NS_OK;
}
};
//-------------- End Invalidate Event Definition ---------------------------
static PRBool IsViewVisible(nsView *aView)
{
for (nsIView *view = aView; view; view = view->GetParent()) {
// We don't check widget visibility here because in the future (with
// the better approach to this that's in attachment 160801 on bug
// 227361), callers of the equivalent to this function should be able
// to rely on being notified when the result of this function changes.
if (view->GetVisibility() == nsViewVisibility_kHide)
return PR_FALSE;
}
// Find out if the root view is visible by asking the view observer
// (this won't be needed anymore if we link view trees across chrome /
// content boundaries in DocumentViewerImpl::MakeWindow).
nsIViewObserver* vo = aView->GetViewManager()->GetViewObserver();
return vo && vo->IsVisible();
}
void
nsViewManager::PostInvalidateEvent()
{
NS_ASSERTION(IsRootVM(), "Caller screwed up");
if (!mInvalidateEvent.IsPending()) {
nsRefPtr<nsViewManagerEvent> ev = new nsInvalidateEvent(this);
if (NS_FAILED(NS_DispatchToCurrentThread(ev))) {
NS_WARNING("failed to dispatch nsInvalidateEvent");
} else {
mInvalidateEvent = ev;
}
}
}
#undef DEBUG_MOUSE_LOCATION
PRInt32 nsViewManager::mVMCount = 0;
nsIRenderingContext* nsViewManager::gCleanupContext = nsnull;
// Weakly held references to all of the view managers
nsVoidArray* nsViewManager::gViewManagers = nsnull;
PRUint32 nsViewManager::gLastUserEventTime = 0;
nsViewManager::nsViewManager()
: mMouseLocation(NSCOORD_NONE, NSCOORD_NONE)
, mDelayedResize(NSCOORD_NONE, NSCOORD_NONE)
, mRootViewManager(this)
{
if (gViewManagers == nsnull) {
NS_ASSERTION(mVMCount == 0, "View Manager count is incorrect");
// Create an array to hold a list of view managers
gViewManagers = new nsVoidArray;
}
if (gCleanupContext == nsnull) {
/* XXX: This should use a device to create a matching |nsIRenderingContext| object */
CallCreateInstance(kRenderingContextCID, &gCleanupContext);
NS_ASSERTION(gCleanupContext,
"Wasn't able to create a graphics context for cleanup");
}
gViewManagers->AppendElement(this);
if (++mVMCount == 1) {
NS_AddFocusSuppressorCallback(&nsViewManager::SuppressFocusEvents);
}
// NOTE: we use a zeroing operator new, so all data members are
// assumed to be cleared here.
mDefaultBackgroundColor = NS_RGBA(0, 0, 0, 0);
mHasPendingUpdates = PR_FALSE;
mRecursiveRefreshPending = PR_FALSE;
mUpdateBatchFlags = 0;
}
nsViewManager::~nsViewManager()
{
if (mRootView) {
// Destroy any remaining views
mRootView->Destroy();
mRootView = nsnull;
}
// Make sure to revoke pending events for all viewmanagers, since some events
// are posted by a non-root viewmanager.
mInvalidateEvent.Revoke();
mSynthMouseMoveEvent.Revoke();
if (!IsRootVM()) {
// We have a strong ref to mRootViewManager
NS_RELEASE(mRootViewManager);
}
mRootScrollable = nsnull;
NS_ASSERTION((mVMCount > 0), "underflow of viewmanagers");
--mVMCount;
#ifdef DEBUG
PRBool removed =
#endif
gViewManagers->RemoveElement(this);
NS_ASSERTION(removed, "Viewmanager instance not was not in the global list of viewmanagers");
if (0 == mVMCount) {
// There aren't any more view managers so
// release the global array of view managers
NS_ASSERTION(gViewManagers != nsnull, "About to delete null gViewManagers");
delete gViewManagers;
gViewManagers = nsnull;
// Cleanup all of the offscreen drawing surfaces if the last view manager
// has been destroyed and there is something to cleanup
// Note: A global rendering context is needed because it is not possible
// to create a nsIRenderingContext during the shutdown of XPCOM. The last
// viewmanager is typically destroyed during XPCOM shutdown.
NS_IF_RELEASE(gCleanupContext);
}
mObserver = nsnull;
mContext = nsnull;
}
NS_IMPL_ISUPPORTS1(nsViewManager, nsIViewManager)
nsresult
nsViewManager::CreateRegion(nsIRegion* *result)
{
nsresult rv;
if (!mRegionFactory) {
mRegionFactory = do_GetClassObject(kRegionCID, &rv);
if (NS_FAILED(rv)) {
*result = nsnull;
return rv;
}
}
nsIRegion* region = nsnull;
rv = CallCreateInstance(mRegionFactory.get(), ®ion);
if (NS_SUCCEEDED(rv)) {
rv = region->Init();
*result = region;
}
return rv;
}
// We don't hold a reference to the presentation context because it
// holds a reference to us.
NS_IMETHODIMP nsViewManager::Init(nsIDeviceContext* aContext)
{
NS_PRECONDITION(nsnull != aContext, "null ptr");
if (nsnull == aContext) {
return NS_ERROR_NULL_POINTER;
}
if (nsnull != mContext) {
return NS_ERROR_ALREADY_INITIALIZED;
}
mContext = aContext;
mRefreshEnabled = PR_TRUE;
mMouseGrabber = nsnull;
return NS_OK;
}
NS_IMETHODIMP_(nsIView *)
nsViewManager::CreateView(const nsRect& aBounds,
const nsIView* aParent,
nsViewVisibility aVisibilityFlag)
{
nsView *v = new nsView(this, aVisibilityFlag);
if (v) {
v->SetPosition(aBounds.x, aBounds.y);
nsRect dim(0, 0, aBounds.width, aBounds.height);
v->SetDimensions(dim, PR_FALSE);
v->SetParent(static_cast<nsView*>(const_cast<nsIView*>(aParent)));
}
return v;
}
NS_IMETHODIMP_(nsIScrollableView *)
nsViewManager::CreateScrollableView(const nsRect& aBounds,
const nsIView* aParent)
{
nsScrollPortView *v = new nsScrollPortView(this);
if (v) {
v->SetPosition(aBounds.x, aBounds.y);
nsRect dim(0, 0, aBounds.width, aBounds.height);
v->SetDimensions(dim, PR_FALSE);
v->SetParent(static_cast<nsView*>(const_cast<nsIView*>(aParent)));
}
return v;
}
NS_IMETHODIMP nsViewManager::GetRootView(nsIView *&aView)
{
aView = mRootView;
return NS_OK;
}
NS_IMETHODIMP nsViewManager::SetRootView(nsIView *aView)
{
nsView* view = static_cast<nsView*>(aView);
NS_PRECONDITION(!view || view->GetViewManager() == this,
"Unexpected viewmanager on root view");
// Do NOT destroy the current root view. It's the caller's responsibility
// to destroy it
mRootView = view;
if (mRootView) {
nsView* parent = mRootView->GetParent();
if (parent) {
// Calling InsertChild on |parent| will InvalidateHierarchy() on us, so
// no need to set mRootViewManager ourselves here.
parent->InsertChild(mRootView, nsnull);
} else {
InvalidateHierarchy();
}
mRootView->SetZIndex(PR_FALSE, 0, PR_FALSE);
}
// Else don't touch mRootViewManager
return NS_OK;
}
NS_IMETHODIMP nsViewManager::GetWindowDimensions(nscoord *aWidth, nscoord *aHeight)
{
if (nsnull != mRootView) {
if (mDelayedResize == nsSize(NSCOORD_NONE, NSCOORD_NONE)) {
nsRect dim;
mRootView->GetDimensions(dim);
*aWidth = dim.width;
*aHeight = dim.height;
} else {
*aWidth = mDelayedResize.width;
*aHeight = mDelayedResize.height;
}
}
else
{
*aWidth = 0;
*aHeight = 0;
}
return NS_OK;
}
NS_IMETHODIMP nsViewManager::SetWindowDimensions(nscoord aWidth, nscoord aHeight)
{
if (mRootView) {
if (IsViewVisible(mRootView)) {
mDelayedResize.SizeTo(NSCOORD_NONE, NSCOORD_NONE);
DoSetWindowDimensions(aWidth, aHeight);
} else {
mDelayedResize.SizeTo(aWidth, aHeight);
}
}
return NS_OK;
}
NS_IMETHODIMP nsViewManager::FlushDelayedResize()
{
if (mDelayedResize != nsSize(NSCOORD_NONE, NSCOORD_NONE)) {
DoSetWindowDimensions(mDelayedResize.width, mDelayedResize.height);
mDelayedResize.SizeTo(NSCOORD_NONE, NSCOORD_NONE);
}
return NS_OK;
}
static void ConvertNativeRegionToAppRegion(nsIRegion* aIn, nsRegion* aOut,
nsIDeviceContext* context)
{
nsRegionRectSet* rects = nsnull;
aIn->GetRects(&rects);
if (!rects)
return;
PRInt32 p2a = context->AppUnitsPerDevPixel();
PRUint32 i;
for (i = 0; i < rects->mNumRects; i++) {
const nsRegionRect& inR = rects->mRects[i];
nsRect outR;
outR.x = NSIntPixelsToAppUnits(inR.x, p2a);
outR.y = NSIntPixelsToAppUnits(inR.y, p2a);
outR.width = NSIntPixelsToAppUnits(inR.width, p2a);
outR.height = NSIntPixelsToAppUnits(inR.height, p2a);
aOut->Or(*aOut, outR);
}
aIn->FreeRects(rects);
}
static nsView* GetDisplayRootFor(nsView* aView)
{
nsView *displayRoot = aView;
for (;;) {
nsView *displayParent = displayRoot->GetParent();
if (!displayParent)
return displayRoot;
if (displayRoot->GetFloating() && !displayParent->GetFloating())
return displayRoot;
displayRoot = displayParent;
}
}
/**
aRegion is given in device coordinates!!
*/
void nsViewManager::Refresh(nsView *aView, nsIRenderingContext *aContext,
nsIRegion *aRegion, PRUint32 aUpdateFlags)
{
NS_ASSERTION(aRegion != nsnull, "Null aRegion");
if (! IsRefreshEnabled())
return;
nsRect viewRect;
aView->GetDimensions(viewRect);
nsPoint vtowoffset = aView->ViewToWidgetOffset();
// damageRegion is the damaged area, in twips, relative to the view origin
nsRegion damageRegion;
// convert pixels-relative-to-widget-origin to twips-relative-to-widget-origin
ConvertNativeRegionToAppRegion(aRegion, &damageRegion, mContext);
// move it from widget coordinates into view coordinates
damageRegion.MoveBy(viewRect.TopLeft() - vtowoffset);
if (damageRegion.IsEmpty()) {
#ifdef DEBUG_roc
nsRect damageRect = damageRegion.GetBounds();
printf("XXX Damage rectangle (%d,%d,%d,%d) does not intersect the widget's view (%d,%d,%d,%d)!\n",
damageRect.x, damageRect.y, damageRect.width, damageRect.height,
viewRect.x, viewRect.y, viewRect.width, viewRect.height);
#endif
return;
}
#ifdef NS_VM_PERF_METRICS
MOZ_TIMER_DEBUGLOG(("Reset nsViewManager::Refresh(region), this=%p\n", this));
MOZ_TIMER_RESET(mWatch);
MOZ_TIMER_DEBUGLOG(("Start: nsViewManager::Refresh(region)\n"));
MOZ_TIMER_START(mWatch);
#endif
NS_ASSERTION(!IsPainting(), "recursive painting not permitted");
if (IsPainting()) {
RootViewManager()->mRecursiveRefreshPending = PR_TRUE;
return;
}
{
nsAutoScriptBlocker scriptBlocker;
SetPainting(PR_TRUE);
nsCOMPtr<nsIRenderingContext> localcx;
NS_ASSERTION(aView->GetWidget(),
"Must have a widget to calculate coordinates correctly");
if (nsnull == aContext)
{
localcx = CreateRenderingContext(*aView);
//couldn't get rendering context. this is ok at init time atleast
if (nsnull == localcx) {
SetPainting(PR_FALSE);
return;
}
} else {
// plain assignment grabs another reference.
localcx = aContext;
}
PRInt32 p2a = mContext->AppUnitsPerDevPixel();
nsRefPtr<gfxContext> ctx = localcx->ThebesContext();
ctx->Save();
ctx->Translate(gfxPoint(gfxFloat(vtowoffset.x) / p2a,
gfxFloat(vtowoffset.y) / p2a));
ctx->Translate(gfxPoint(-gfxFloat(viewRect.x) / p2a,
-gfxFloat(viewRect.y) / p2a));
nsRegion opaqueRegion;
AddCoveringWidgetsToOpaqueRegion(opaqueRegion, mContext, aView);
damageRegion.Sub(damageRegion, opaqueRegion);
RenderViews(aView, *localcx, damageRegion);
ctx->Restore();
SetPainting(PR_FALSE);
}
if (RootViewManager()->mRecursiveRefreshPending) {
// Unset this flag first, since if aUpdateFlags includes NS_VMREFRESH_IMMEDIATE
// we'll reenter this code from the UpdateAllViews call.
RootViewManager()->mRecursiveRefreshPending = PR_FALSE;
UpdateAllViews(aUpdateFlags);
}
#ifdef NS_VM_PERF_METRICS
MOZ_TIMER_DEBUGLOG(("Stop: nsViewManager::Refresh(region), this=%p\n", this));
MOZ_TIMER_STOP(mWatch);
MOZ_TIMER_LOG(("vm2 Paint time (this=%p): ", this));
MOZ_TIMER_PRINT(mWatch);
#endif
}
// aRect is in app units and relative to the top-left of the aView->GetWidget()
void nsViewManager::DefaultRefresh(nsView* aView, nsIRenderingContext *aContext, const nsRect* aRect)
{
NS_PRECONDITION(aView, "Must have a view to work with!");
nsIWidget* widget = aView->GetNearestWidget(nsnull);
if (! widget)
return;
nsCOMPtr<nsIRenderingContext> context = aContext;
if (! aContext)
context = CreateRenderingContext(*aView);
if (! context)
return;
nscolor bgcolor = mDefaultBackgroundColor;
if (NS_GET_A(mDefaultBackgroundColor) == 0) {
NS_WARNING("nsViewManager: Asked to paint a default background, but no default background color is set!");
return;
}
context->SetColor(bgcolor);
context->FillRect(*aRect);
}
void nsViewManager::AddCoveringWidgetsToOpaqueRegion(nsRegion &aRgn, nsIDeviceContext* aContext,
nsView* aRootView) {
NS_PRECONDITION(aRootView, "Must have root view");
// We accumulate the bounds of widgets obscuring aRootView's widget into opaqueRgn.
// In OptimizeDisplayList, display list elements which lie behind obscuring native
// widgets are dropped.
// This shouldn't really be necessary, since the GFX/Widget toolkit should remove these
// covering widgets from the clip region passed into the paint command. But right now
// they only give us a paint rect and not a region, so we can't access that information.
// It's important to identifying areas that are covered by native widgets to avoid
// painting their views many times as we process invalidates from the root widget all the
// way down to the nested widgets.
//
// NB: we must NOT add widgets that correspond to floating views!
// We may be required to paint behind them
aRgn.SetEmpty();
nsIWidget* widget = aRootView->GetNearestWidget(nsnull);
if (!widget) {
return;
}
for (nsIWidget* childWidget = widget->GetFirstChild();
childWidget;
childWidget = childWidget->GetNextSibling()) {
PRBool widgetVisible;
childWidget->IsVisible(widgetVisible);
if (widgetVisible) {
nsView* view = nsView::GetViewFor(childWidget);
if (view && view->GetVisibility() == nsViewVisibility_kShow
&& !view->GetFloating()) {
nsRect bounds = view->GetBounds();
if (bounds.width > 0 && bounds.height > 0) {
nsView* viewParent = view->GetParent();
while (viewParent && viewParent != aRootView) {
viewParent->ConvertToParentCoords(&bounds.x, &bounds.y);
viewParent = viewParent->GetParent();
}
// maybe we couldn't get the view into the coordinate
// system of aRootView (maybe it's not a descendant
// view of aRootView?); if so, don't use it
if (viewParent) {
aRgn.Or(aRgn, bounds);
}
}
}
}
}
}
// aRC and aRegion are in view coordinates
void nsViewManager::RenderViews(nsView *aView, nsIRenderingContext& aRC,
const nsRegion& aRegion)
{
if (mObserver) {
nsView* displayRoot = GetDisplayRootFor(aView);
nsPoint offsetToRoot = aView->GetOffsetTo(displayRoot);
nsRegion damageRegion(aRegion);
damageRegion.MoveBy(offsetToRoot);
aRC.PushState();
aRC.Translate(-offsetToRoot.x, -offsetToRoot.y);
mObserver->Paint(displayRoot, &aRC, damageRegion);
aRC.PopState();
}
}
void nsViewManager::ProcessPendingUpdates(nsView* aView, PRBool aDoInvalidate)
{
NS_ASSERTION(IsRootVM(), "Updates will be missed");
// Protect against a null-view.
if (!aView) {
return;
}
if (aView->HasWidget()) {
aView->ResetWidgetBounds(PR_FALSE, PR_FALSE, PR_TRUE);
}
// process pending updates in child view.
for (nsView* childView = aView->GetFirstChild(); childView;
childView = childView->GetNextSibling()) {
ProcessPendingUpdates(childView, aDoInvalidate);
}
if (aDoInvalidate && aView->HasNonEmptyDirtyRegion()) {
// Push out updates after we've processed the children; ensures that
// damage is applied based on the final widget geometry
NS_ASSERTION(mRefreshEnabled, "Cannot process pending updates with refresh disabled");
nsRegion* dirtyRegion = aView->GetDirtyRegion();
if (dirtyRegion) {
UpdateWidgetArea(aView, *dirtyRegion, nsnull);
dirtyRegion->SetEmpty();
}
}
}
NS_IMETHODIMP nsViewManager::Composite()
{
if (!IsRootVM()) {
return RootViewManager()->Composite();
}
if (UpdateCount() > 0)
{
ForceUpdate();
ClearUpdateCount();
}
return NS_OK;
}
NS_IMETHODIMP nsViewManager::UpdateView(nsIView *aView, PRUint32 aUpdateFlags)
{
// Mark the entire view as damaged
nsView* view = static_cast<nsView*>(aView);
nsRect bounds = view->GetBounds();
view->ConvertFromParentCoords(&bounds.x, &bounds.y);
return UpdateView(view, bounds, aUpdateFlags);
}
// This method accumulates the intersectons of all dirty regions attached to
// descendants of aSourceView with the cliprect of aTargetView into the dirty
// region of aTargetView, after offseting said intersections by aOffset.
static void
AccumulateIntersectionsIntoDirtyRegion(nsView* aTargetView,
nsView* aSourceView,
const nsPoint& aOffset)
{
if (aSourceView->HasNonEmptyDirtyRegion()) {
// In most cases, aSourceView is an ancestor of aTargetView, since most
// commonly we have dirty rects on the root view.
nsPoint offset = aTargetView->GetOffsetTo(aSourceView);
nsRegion intersection;
intersection = *aSourceView->GetDirtyRegion();
if (!intersection.IsEmpty()) {
nsRegion* targetRegion = aTargetView->GetDirtyRegion();
if (targetRegion) {
intersection.MoveBy(-offset + aOffset);
targetRegion->Or(*targetRegion, intersection);
// Random simplification number...
targetRegion->SimplifyOutward(20);
}
}
}
if (aSourceView == aTargetView) {
// No need to do this with kids of aTargetView
return;
}
for (nsView* kid = aSourceView->GetFirstChild();
kid;
kid = kid->GetNextSibling()) {
AccumulateIntersectionsIntoDirtyRegion(aTargetView, kid, aOffset);
}
}
nsresult
nsViewManager::WillBitBlit(nsView* aView, nsPoint aScrollAmount)
{
if (!IsRootVM()) {
RootViewManager()->WillBitBlit(aView, aScrollAmount);
return NS_OK;
}
NS_PRECONDITION(aView, "Must have a view");
NS_PRECONDITION(!aView->NeedsInvalidateFrameOnScroll(), "We shouldn't be BitBlting.");
NS_PRECONDITION(aView->HasWidget(), "View must have a widget");
++mScrollCnt;
// Since the view is actually moving the widget by -aScrollAmount, that's the
// offset we want to use when accumulating dirty rects.
AccumulateIntersectionsIntoDirtyRegion(aView, GetRootView(), -aScrollAmount);
return NS_OK;
}
// Invalidate all widgets which overlap the view, other than the view's own widgets.
void
nsViewManager::UpdateViewAfterScroll(nsView *aView, const nsRegion& aUpdateRegion)
{
NS_ASSERTION(RootViewManager()->mScrollCnt > 0,
"Someone forgot to call WillBitBlit()");
// Look at the view's clipped rect. It may be that part of the view is clipped out
// in which case we don't need to worry about invalidating the clipped-out part.
nsRect damageRect = aView->GetDimensions();
if (damageRect.IsEmpty()) {
// Don't forget to undo mScrollCnt!
--RootViewManager()->mScrollCnt;
return;
}
nsView* displayRoot = GetDisplayRootFor(aView);
nsPoint offset = aView->GetOffsetTo(displayRoot);
damageRect.MoveBy(offset);
UpdateWidgetArea(displayRoot, nsRegion(damageRect), aView);
if (!aUpdateRegion.IsEmpty()) {
// XXX We should update the region, not the bounds rect, but that requires
// a little more work. Fix this when we reshuffle this code.
nsRegion update(aUpdateRegion);
update.MoveBy(offset);
UpdateWidgetArea(displayRoot, update, nsnull);
// FlushPendingInvalidates();
}
Composite();
--RootViewManager()->mScrollCnt;
}
/**
* @param aDamagedRegion this region, relative to aWidgetView, is invalidated in
* every widget child of aWidgetView, plus aWidgetView's own widget
* @param aIgnoreWidgetView if non-null, the aIgnoreWidgetView's widget and its
* children are not updated.
*/
void
nsViewManager::UpdateWidgetArea(nsView *aWidgetView, const nsRegion &aDamagedRegion,
nsView* aIgnoreWidgetView)
{
if (!IsRefreshEnabled()) {
// accumulate this rectangle in the view's dirty region, so we can
// process it later.
nsRegion* dirtyRegion = aWidgetView->GetDirtyRegion();
if (!dirtyRegion) return;
dirtyRegion->Or(*dirtyRegion, aDamagedRegion);
// Don't let dirtyRegion grow beyond 8 rects
dirtyRegion->SimplifyOutward(8);
nsViewManager* rootVM = RootViewManager();
rootVM->mHasPendingUpdates = PR_TRUE;
rootVM->IncrementUpdateCount();
return;
// this should only happen at the top level, and this result
// should not be consumed by top-level callers, so it doesn't
// really matter what we return
}
// If the bounds don't overlap at all, there's nothing to do
nsRegion intersection;
intersection.And(aWidgetView->GetDimensions(), aDamagedRegion);
if (intersection.IsEmpty()) {
return;
}
// If the widget is hidden, it don't cover nothing
if (nsViewVisibility_kHide == aWidgetView->GetVisibility()) {
#ifdef DEBUG
// Assert if view is hidden but widget is visible
nsIWidget* widget = aWidgetView->GetNearestWidget(nsnull);
if (widget) {
PRBool visible;
widget->IsVisible(visible);
NS_ASSERTION(!visible, "View is hidden but widget is visible!");
}
#endif
return;
}
if (aWidgetView == aIgnoreWidgetView) {
// the widget for aIgnoreWidgetView (and its children) should be treated as already updated.
return;
}
nsIWidget* widget = aWidgetView->GetNearestWidget(nsnull);
if (!widget) {
// The root view or a scrolling view might not have a widget
// (for example, during printing). We get here when we scroll
// during printing to show selected options in a listbox, for example.
return;
}
// Update all child widgets with the damage. In the process,
// accumulate the union of all the child widget areas, or at least
// some subset of that.
nsRegion children;
for (nsIWidget* childWidget = widget->GetFirstChild();
childWidget;
childWidget = childWidget->GetNextSibling()) {
nsView* view = nsView::GetViewFor(childWidget);
NS_ASSERTION(view != aWidgetView, "will recur infinitely");
if (view && view->GetVisibility() == nsViewVisibility_kShow) {
// Don't mess with views that are in completely different view
// manager trees
if (view->GetViewManager()->RootViewManager() == RootViewManager()) {
// get the damage region into 'view's coordinate system
nsRegion damage = intersection;
nsPoint offset = view->GetOffsetTo(aWidgetView);
damage.MoveBy(-offset);
UpdateWidgetArea(view, damage, aIgnoreWidgetView);
children.Or(children, view->GetDimensions() + offset);
children.SimplifyInward(20);
}
}
}
nsRegion leftOver;
leftOver.Sub(intersection, children);
if (!leftOver.IsEmpty()) {
NS_ASSERTION(IsRefreshEnabled(), "Can only get here with refresh enabled, I hope");
const nsRect* r;
for (nsRegionRectIterator iter(leftOver); (r = iter.Next());) {
nsRect bounds = *r;
ViewToWidget(aWidgetView, aWidgetView, bounds);
widget->Invalidate(bounds, PR_FALSE);
}
}
}
NS_IMETHODIMP nsViewManager::UpdateView(nsIView *aView, const nsRect &aRect, PRUint32 aUpdateFlags)
{
NS_PRECONDITION(nsnull != aView, "null view");
nsView* view = static_cast<nsView*>(aView);
nsRect damagedRect(aRect);
// If the rectangle is not visible then abort
// without invalidating. This is a performance
// enhancement since invalidating a native widget
// can be expensive.
// This also checks for silly request like damagedRect.width = 0 or damagedRect.height = 0
nsRectVisibility rectVisibility;
GetRectVisibility(view, damagedRect, 0, &rectVisibility);
if (rectVisibility != nsRectVisibility_kVisible) {
return NS_OK;
}
// if this is a floating view, it isn't covered by any widgets other than
// its children. In that case we walk up to its parent widget and use
// that as the root to update from. This also means we update areas that
// may be outside the parent view(s), which is necessary for floats.
if (view->GetFloating()) {
nsView* widgetParent = view;
while (!widgetParent->HasWidget()) {
widgetParent->ConvertToParentCoords(&damagedRect.x, &damagedRect.y);
widgetParent = widgetParent->GetParent();
}
UpdateWidgetArea(widgetParent, nsRegion(damagedRect), nsnull);
} else {
// Propagate the update to the root widget of the root view manager, since
// iframes, for example, can overlap each other and be translucent. So we
// have to possibly invalidate our rect in each of the widgets we have
// lying about.
damagedRect.MoveBy(ComputeViewOffset(view));
UpdateWidgetArea(RootViewManager()->GetRootView(), nsRegion(damagedRect), nsnull);
}
RootViewManager()->IncrementUpdateCount();
if (!IsRefreshEnabled()) {
return NS_OK;
}
// See if we should do an immediate refresh or wait
if (aUpdateFlags & NS_VMREFRESH_IMMEDIATE) {
Composite();
}
return NS_OK;
}
NS_IMETHODIMP nsViewManager::UpdateAllViews(PRUint32 aUpdateFlags)
{
if (RootViewManager() != this) {
return RootViewManager()->UpdateAllViews(aUpdateFlags);
}
UpdateViews(mRootView, aUpdateFlags);
return NS_OK;
}
void nsViewManager::UpdateViews(nsView *aView, PRUint32 aUpdateFlags)
{
// update this view.
UpdateView(aView, aUpdateFlags);
// update all children as well.
nsView* childView = aView->GetFirstChild();
while (nsnull != childView) {
UpdateViews(childView, aUpdateFlags);
childView = childView->GetNextSibling();
}
}
nsView *nsViewManager::sCurrentlyFocusView = nsnull;
nsView *nsViewManager::sViewFocusedBeforeSuppression = nsnull;
PRBool nsViewManager::sFocusSuppressed = PR_FALSE;
void nsViewManager::SuppressFocusEvents(PRBool aSuppress)
{
if (aSuppress) {
sFocusSuppressed = PR_TRUE;
SetViewFocusedBeforeSuppression(GetCurrentlyFocusedView());
return;
}
sFocusSuppressed = PR_FALSE;
if (GetCurrentlyFocusedView() == GetViewFocusedBeforeSuppression()) {
return;
}
// We're turning off suppression, synthesize LOSTFOCUS/GOTFOCUS.
nsIWidget *widget = nsnull;
nsEventStatus status;
// Backup what is focused before we send the blur. If the
// blur causes a focus change, keep that new focus change,
// don't overwrite with the old "currently focused view".
nsIView *currentFocusBeforeBlur = GetCurrentlyFocusedView();
// Send NS_LOSTFOCUS to widget that was focused before
// focus/blur suppression.
if (GetViewFocusedBeforeSuppression()) {
widget = GetViewFocusedBeforeSuppression()->GetWidget();
if (widget) {
#ifdef DEBUG_FOCUS_SUPPRESSION
printf("*** 0 INFO TODO [CPEARCE] Unsuppressing, dispatching NS_LOSTFOCUS\n");
#endif
nsGUIEvent event(PR_TRUE, NS_LOSTFOCUS, widget);
widget->DispatchEvent(&event, status);
}
}
// Send NS_GOTFOCUS to the widget that we think should be focused.
if (GetCurrentlyFocusedView() &&
currentFocusBeforeBlur == GetCurrentlyFocusedView())
{
widget = GetCurrentlyFocusedView()->GetWidget();