Layer.cpp revision 182db85c0c66d7a260844e6b63e5925bfd465cd6
1//------------------------------------------------------------------------------
2//	Copyright (c) 2001-2005, Haiku, Inc.
3//
4//	Permission is hereby granted, free of charge, to any person obtaining a
5//	copy of this software and associated documentation files (the "Software"),
6//	to deal in the Software without restriction, including without limitation
7//	the rights to use, copy, modify, merge, publish, distribute, sublicense,
8//	and/or sell copies of the Software, and to permit persons to whom the
9//	Software is furnished to do so, subject to the following conditions:
10//
11//	The above copyright notice and this permission notice shall be included in
12//	all copies or substantial portions of the Software.
13//
14//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19//	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20//	DEALINGS IN THE SOFTWARE.
21//
22//	File Name:		Layer.cpp
23//	Author:			DarkWyrm <bpmagic@columbus.rr.com>
24//					Adi Oanca <adioanca@cotty.iren.ro>
25//					Stephan A��mus <superstippi@gmx.de>
26//	Description:	Class used for rendering to the frame buffer. One layer per
27//					view on screen and also for window decorators
28//
29//------------------------------------------------------------------------------
30#include <string.h>
31#include <stdio.h>
32#include <stdlib.h>
33
34#include <AppDefs.h>
35#include <Message.h>
36#include <Region.h>
37#include <View.h>
38
39#include "DebugInfoManager.h"
40#include "DisplayDriver.h"
41#include "LayerData.h"
42#include "PortLink.h"
43#include "RootLayer.h"
44#include "ServerProtocol.h"
45#include "ServerWindow.h"
46#include "WinBorder.h"
47#include "Layer.h"
48#include "ServerBitmap.h"
49
50//#define DEBUG_LAYER
51#ifdef DEBUG_LAYER
52#	define STRACE(x) printf x
53#else
54#	define STRACE(x) ;
55#endif
56
57//#define DEBUG_LAYER_REBUILD
58#ifdef DEBUG_LAYER_REBUILD
59#	define RBTRACE(x) printf x
60#else
61#	define RBTRACE(x) ;
62#endif
63
64Layer::Layer(BRect frame, const char* name, int32 token,
65			 uint32 resize, uint32 flags, DisplayDriver* driver)
66	:
67	fFrame(frame), // in parent coordinates
68//	fBoundsLeftTop(0.0, 0.0),
69
70	// Layer does not start out as a part of the tree
71	fOwner(NULL),
72
73	fParent(NULL),
74	fPreviousSibling(NULL),
75	fNextSibling(NULL),
76	fFirstChild(NULL),
77	fLastChild(NULL),
78
79	fCurrent(NULL),
80
81	// all regions (fVisible, fFullVisible, fFull) start empty
82#ifndef NEW_CLIPPING
83	fVisible(),
84	fFullVisible(),
85	fFull(),
86	fFrameAction(B_LAYER_ACTION_NONE),
87	fClipReg(&fVisible),
88#else
89	fVisible2(),
90	fFullVisible2(),
91	fClipReg(&fVisible2),
92#endif
93
94	fServerWin(NULL),
95	fName(name),
96	fViewToken(token),
97
98	fFlags(flags),
99	fResizeMode(resize),
100	fEventMask(0UL),
101	fEventOptions(0UL),
102	fHidden(false),
103	fIsTopLayer(false),
104
105	fAdFlags(0),
106
107	fDriver(driver),
108	fLayerData(new LayerData()),
109
110	fRootLayer(NULL),
111
112	fViewColor(255, 255, 255, 255),
113	fBackgroundBitmap(NULL),
114	fOverlayBitmap(NULL)
115{
116	if (!frame.IsValid()) {
117char helper[1024];
118sprintf(helper, "Layer::Layer(BRect(%.1f, %.1f, %.1f, %.1f), name: %s, token: %ld) - frame is invalid\n",
119		frame.left, frame.top, frame.right, frame.bottom, name, token);
120CRITICAL(helper);
121		fFrame.Set(0, 0, 1, 1);
122	}
123
124	if (!fDriver)
125		CRITICAL("You MUST have a valid driver to init a Layer object\n");
126
127	// NOTE: This flag is forwarded to a LayerData setting, even
128	// though it is actually not part of a "state". However,
129	// it is an important detail of a graphics context, and we
130	// have no other means to pass this setting on to the DisplayDriver
131	// other than through the LayerData. If we ever add a flag
132	// B_ANTI_ALIASING to the view flags, it would have to be passed
133	// in the same way. Though when breaking binary compatibility,
134	// we might want to make this an actual part of a "state" (with
135	// a different API to set these).
136	// Note that the flag is also tested (updated) in Push/PopState and
137	// SetFlags().
138	fLayerData->SetSubPixelPrecise(fFlags & B_SUBPIXEL_PRECISE);
139
140	STRACE(("Layer(%s) successfuly created\n", Name()));
141}
142
143//! Destructor frees all allocated heap space
144Layer::~Layer()
145{
146	delete fLayerData;
147
148	// TODO: uncomment!
149	//PruneTree();
150}
151
152/*!
153	\brief Adds a child layer to the current one
154	\param layer a new child layer
155	\param serverWin the serverwindow to which the layer will belong
156
157	Unlike the BView version, if the layer already belongs to another, then
158	it spits an error to stdout and returns.
159*/
160void
161Layer::AddChild(Layer* layer, ServerWindow* serverWin)
162{
163	STRACE(("Layer(%s)::AddChild(%s) START\n", Name(), layer->Name()));
164
165	if (layer->fParent != NULL) {
166		printf("ERROR: AddChild(): Layer already has a parent\n");
167		return;
168	}
169
170	// 1) attach layer to the tree structure
171	layer->fParent = this;
172
173	// if we have children already, bump the current last child back one and
174	// make the new child the last layer
175	if (fLastChild) {
176		layer->fPreviousSibling = fLastChild;
177		fLastChild->fNextSibling = layer;
178	} else {
179		fFirstChild = layer;
180	}
181	fLastChild = layer;
182
183	// if we have no RootLayer yet, then there is no need to set any parameters --
184	// they will be set when the RootLayer for this tree will be added
185	// to the main tree structure.
186	if (!fRootLayer) {
187		STRACE(("Layer(%s)::AddChild(%s) END\n", Name(), layer->Name()));
188		return;
189	}
190
191	// 2) Iterate over the newly-added layer and all its children, setting the
192	//	root layer and server window and also rebuilding the full-size region
193	//	for every descendant of the newly-added layer
194
195	//c = short for: current
196	Layer* c = layer;
197	Layer* stop = layer;
198	while (true) {
199		// action block
200
201		// 2.1) set the RootLayer for this object.
202		c->SetRootLayer(c->fParent->fRootLayer);
203
204		// 2.2) this Layer must know if it has a ServerWindow object attached.
205		c->fServerWin=serverWin;
206
207		// 2.3) we are attached to the main tree so build our full region.
208#ifndef NEW_CLIPPING
209		c->RebuildFullRegion();
210#endif
211		// tree parsing algorithm
212		if (c->fFirstChild) {
213			// go deep
214			c = c->fFirstChild;
215		} else {
216			// go right or up
217
218			if (c == stop) // our trip is over
219				break;
220
221			if (c->fNextSibling) {
222				// go right
223				c = c->fNextSibling;
224			} else {
225				// go up
226				while (!c->fParent->fNextSibling && c->fParent != stop)
227					c = c->fParent;
228
229				if (c->fParent == stop) // that's enough!
230					break;
231
232				c = c->fParent->fNextSibling;
233			}
234		}
235	}
236
237	STRACE(("Layer(%s)::AddChild(%s) END\n", Name(), layer->Name()));
238}
239
240/*!
241	\brief Removes a child layer from the current one
242	\param layer the layer to remove
243
244	If the layer does not belong to the the current layer, then this function
245	spits out an error to stdout and returns
246*/
247void
248Layer::RemoveChild(Layer *layer)
249{
250	STRACE(("Layer(%s)::RemoveChild(%s) START\n", Name(), layer->Name()));
251
252	if (!layer->fParent) {
253		printf("ERROR: RemoveChild(): Layer doesn't have a parent\n");
254		return;
255	}
256
257	if (layer->fParent != this) {
258		printf("ERROR: RemoveChild(): Layer is not a child of this layer\n");
259		return;
260	}
261
262	// 1) remove this layer from the main tree.
263
264	// Take care of fParent
265	layer->fParent = NULL;
266
267	if (fFirstChild == layer)
268		fFirstChild = layer->fNextSibling;
269
270	if (fLastChild == layer)
271		fLastChild = layer->fPreviousSibling;
272
273	// Take care of siblings
274	if (layer->fPreviousSibling != NULL)
275		layer->fPreviousSibling->fNextSibling	= layer->fNextSibling;
276
277	if (layer->fNextSibling != NULL)
278		layer->fNextSibling->fPreviousSibling = layer->fPreviousSibling;
279
280	layer->fPreviousSibling = NULL;
281	layer->fNextSibling = NULL;
282
283#ifdef NEW_CLIPPING
284	layer->clear_visible_regions();
285#endif
286
287	// 2) Iterate over all of the removed-layer's descendants and unset the
288	//	root layer, server window, and all redraw-related regions
289
290	Layer* c = layer; //c = short for: current
291	Layer* stop = layer;
292
293	while (true) {
294		// action block
295		{
296			// 2.1) set the RootLayer for this object.
297			c->SetRootLayer(NULL);
298			// 2.2) this Layer must know if it has a ServerWindow object attached.
299			c->fServerWin = NULL;
300			// 2.3) we were removed from the main tree so clear our full region.
301#ifndef NEW_CLIPPING
302			c->fFull.MakeEmpty();
303			// 2.4) clear fullVisible region.
304			c->fFullVisible.MakeEmpty();
305			// 2.5) we don't have a visible region anymore.
306			c->fVisible.MakeEmpty();
307#endif
308		}
309
310		// tree parsing algorithm
311		if (c->fFirstChild) {
312			// go deep
313			c = c->fFirstChild;
314		} else {
315			// go right or up
316			if (c == stop) // out trip is over
317				break;
318
319			if (c->fNextSibling) {
320				// go right
321				c = c->fNextSibling;
322			} else {
323				// go up
324				while(!c->fParent->fNextSibling && c->fParent != stop)
325					c = c->fParent;
326
327				if (c->fParent == stop) // that enough!
328					break;
329
330				c = c->fParent->fNextSibling;
331			}
332		}
333	}
334	STRACE(("Layer(%s)::RemoveChild(%s) END\n", Name(), layer->Name()));
335}
336
337//! Removes the calling layer from the tree
338void
339Layer::RemoveSelf()
340{
341	// A Layer removes itself from the tree (duh)
342	if (fParent == NULL) {
343		printf("ERROR: RemoveSelf(): Layer doesn't have a parent\n");
344		return;
345	}
346	fParent->RemoveChild(this);
347}
348
349/*!
350	\return true if the child is owned by this Layer, false if not
351*/
352bool
353Layer::HasChild(Layer* layer)
354{
355	for (Layer* child = FirstChild(); child; child = NextChild()) {
356		if (child == layer)
357			return true;
358	}
359	return false;
360}
361
362//! Returns the number of children
363uint32
364Layer::CountChildren() const
365{
366	uint32 count = 0;
367	Layer* child = FirstChild();
368	while (child != NULL) {
369		child = NextChild();
370		count++;
371	}
372	return count;
373}
374
375/*!
376	\brief Finds a child of the caller based on its token ID
377	\param token ID of the layer to find
378	\return Pointer to the layer or NULL if not found
379*/
380Layer*
381Layer::FindLayer(const int32 token)
382{
383	// (recursive) search for a layer based on its view token
384
385	// iterate only over direct child layers first
386	for (Layer* child = FirstChild(); child; child = NextChild()) {
387		if (child->fViewToken == token)
388			return child;
389	}
390
391	// try a recursive search
392	for (Layer* child = FirstChild(); child; child = NextChild()) {
393		if (Layer* layer = child->FindLayer(token))
394			return layer;
395	}
396
397	return NULL;
398}
399
400/*!
401	\brief Returns the layer at the given point
402	\param pt The point to look the layer at
403	\return The layer containing the point or NULL if no layer found
404*/
405Layer*
406Layer::LayerAt(const BPoint &pt, bool recursive)
407{
408	//printf("%p:%s:LayerAt(x = %g, y = %g)\n", this, Name(), pt.x, pt.y);
409	if (!recursive)	{
410		if (VisibleRegion().Contains(pt))
411			return this;
412
413		for (Layer* child = LastChild(); child; child = PreviousChild())
414			if (child->FullVisible().Contains(pt))
415				return child;
416
417		return NULL;
418	}
419
420#ifndef NEW_CLIPPING
421	if (fVisible.Contains(pt))
422		return this;
423
424	if (fFullVisible.Contains(pt)) {
425		for (Layer* child = LastChild(); child; child = PreviousChild()) {
426			if (Layer* layer = child->LayerAt(pt))
427				return layer;
428		}
429	}
430#else
431	if (fVisible2.Contains(pt))
432		return this;
433
434	if (fFullVisible2.Contains(pt)) {
435		for (Layer* child = LastChild(); child; child = PreviousChild()) {
436			if (Layer* layer = child->LayerAt(pt))
437				return layer;
438		}
439	}
440#endif
441	return NULL;
442}
443
444// FirstChild
445Layer*
446Layer::FirstChild() const
447{
448	fCurrent = fFirstChild;
449	return fCurrent;
450}
451
452// NextChild
453Layer*
454Layer::NextChild() const
455{
456	fCurrent = fCurrent->fNextSibling;
457	return fCurrent;
458}
459
460// PreviousChild
461Layer*
462Layer::PreviousChild() const
463{
464	fCurrent = fCurrent->fPreviousSibling;
465	return fCurrent;
466}
467
468// LastChild
469Layer*
470Layer::LastChild() const
471{
472	fCurrent = fLastChild;
473	return fCurrent;
474}
475
476// SetName
477void
478Layer::SetName(const char* name)
479{
480	fName.SetTo(name);
481}
482
483// SetFlags
484void
485Layer::SetFlags(uint32 flags)
486{
487	fFlags = flags;
488	fLayerData->SetSubPixelPrecise(fFlags & B_SUBPIXEL_PRECISE);
489}
490
491#ifndef NEW_CLIPPING
492
493//! Rebuilds the layer's "completely visible" region
494void
495Layer::RebuildFullRegion(void)
496{
497	STRACE(("Layer(%s)::RebuildFullRegion()\n", Name()));
498
499	if (fParent)
500		fFull.Set(fParent->ConvertToTop(fFrame ));
501	else
502		fFull.Set(fFrame);
503
504	// TODO: restrict to screen coordinates
505
506	// TODO: Convert to screen coordinates
507
508	LayerData *ld;
509	ld = fLayerData;
510	do {
511		// clip to user region
512		if (const BRegion* userClipping = ld->ClippingRegion())
513			fFull.IntersectWith(userClipping);
514
515	} while ((ld = ld->prevState));
516}
517
518// StartRebuildRegions
519void
520Layer::StartRebuildRegions( const BRegion& reg, Layer *target, uint32 action, BPoint& pt)
521{
522	STRACE(("Layer(%s)::StartRebuildRegions() START\n", Name()));
523	RBTRACE(("\n\nLayer(%s)::StartRebuildRegions() START\n", Name()));
524	if (!fParent)
525		fFullVisible = fFull;
526
527	BRegion oldVisible = fVisible;
528
529	fVisible = fFullVisible;
530
531	// Rebuild regions for children...
532	for (Layer *lay = LastChild(); lay; lay = PreviousChild()) {
533		if (lay == target)
534			lay->RebuildRegions(reg, action, pt, BPoint(0.0f, 0.0f));
535		else
536			lay->RebuildRegions(reg, B_LAYER_NONE, pt, BPoint(0.0f, 0.0f));
537	}
538
539	#ifdef DEBUG_LAYER_REBUILD
540		printf("\nSRR: Layer(%s) ALMOST done regions:\n", Name());
541		printf("\tVisible Region:\n");
542		fVisible.PrintToStream();
543		printf("\tFull Visible Region:\n");
544		fFullVisible.PrintToStream();
545	#endif
546
547	BRegion redrawReg(fVisible);
548
549	// if this is the first time
550	if (oldVisible.CountRects() > 0)
551		redrawReg.Exclude(&oldVisible);
552
553	if (redrawReg.CountRects() > 0)
554		fRootLayer->fRedrawReg.Include(&redrawReg);
555
556	#ifdef DEBUG_LAYER_REBUILD
557		printf("\nLayer(%s)::StartRebuildRegions() DONE. Results:\n", Name());
558		printf("\tRedraw Region:\n");
559		fRootLayer->fRedrawReg.PrintToStream();
560		printf("\tCopy Region:\n");
561		for (int32 k=0; k<fRootLayer->fCopyRegList.CountItems(); k++) {
562			((BRegion*)(fRootLayer->fCopyRegList.ItemAt(k)))->PrintToStream();
563			((BPoint*)(fRootLayer->fCopyList.ItemAt(k)))->PrintToStream();
564		}
565		printf("\n");
566	#endif
567
568	STRACE(("Layer(%s)::StartRebuildRegions() END\n", Name()));
569	RBTRACE(("Layer(%s)::StartRebuildRegions() END\n", Name()));
570}
571
572// RebuildRegions
573void
574Layer::RebuildRegions( const BRegion& reg, uint32 action, BPoint pt, BPoint ptOffset)
575{
576	STRACE(("Layer(%s)::RebuildRegions() START\n", Name()));
577
578	// TODO:/NOTE: this method must be executed as quickly as possible.
579
580	// Currently SendView[Moved/Resized]Msg() simply constructs a message and calls
581	// ServerWindow::SendMessageToClient(). This involves the alternative use of
582	// kernel and this code in the CPU, so there are a lot of context switches.
583	// This is NOT good at all!
584
585	// One alternative would be the use of a BMessageQueue per ServerWindows OR only
586	// one for app_server which will be emptied as soon as this critical operation ended.
587	// Talk to DW, Gabe.
588
589	BRegion	oldRegion;
590	uint32 newAction = action;
591	BPoint newPt = pt;
592	BPoint newOffset = ptOffset; // used for resizing only
593
594	BPoint dummyNewLocation;
595
596	RRLabel1:
597	switch(action) {
598		case B_LAYER_NONE: {
599			RBTRACE(("1) Layer(%s): Action B_LAYER_NONE\n", Name()));
600			STRACE(("1) Layer(%s): Action B_LAYER_NONE\n", Name()));
601			oldRegion = fVisible;
602			break;
603		}
604		case B_LAYER_MOVE: {
605			RBTRACE(("1) Layer(%s): Action B_LAYER_MOVE\n", Name()));
606			STRACE(("1) Layer(%s): Action B_LAYER_MOVE\n", Name()));
607			oldRegion = fFullVisible;
608			fFrame.OffsetBy(pt.x, pt.y);
609			fFull.OffsetBy(pt.x, pt.y);
610
611			// TODO: investigate combining frame event messages for efficiency
612			//SendViewMovedMsg();
613			AddToViewsWithInvalidCoords();
614
615			newAction	= B_LAYER_SIMPLE_MOVE;
616			break;
617		}
618		case B_LAYER_SIMPLE_MOVE: {
619			RBTRACE(("1) Layer(%s): Action B_LAYER_SIMPLE_MOVE\n", Name()));
620			STRACE(("1) Layer(%s): Action B_LAYER_SIMPLE_MOVE\n", Name()));
621			fFull.OffsetBy(pt.x, pt.y);
622
623			break;
624		}
625		case B_LAYER_RESIZE: {
626			RBTRACE(("1) Layer(%s): Action B_LAYER_RESIZE\n", Name()));
627			STRACE(("1) Layer(%s): Action B_LAYER_RESIZE\n", Name()));
628			oldRegion	= fVisible;
629
630			fFrame.right	+= pt.x;
631			fFrame.bottom	+= pt.y;
632			RebuildFullRegion();
633
634			// TODO: investigate combining frame event messages for efficiency
635			//SendViewResizedMsg();
636			AddToViewsWithInvalidCoords();
637
638			newAction = B_LAYER_MASK_RESIZE;
639			break;
640		}
641		case B_LAYER_MASK_RESIZE: {
642			RBTRACE(("1) Layer(%s): Action B_LAYER_MASK_RESIZE\n", Name()));
643			STRACE(("1) Layer(%s): Action B_LAYER_MASK_RESIZE\n", Name()));
644			oldRegion = fVisible;
645
646			BPoint offset, rSize;
647			BPoint coords[2];
648
649			ResizeOthers(pt.x, pt.y, coords, NULL);
650			offset = coords[0];
651			rSize = coords[1];
652			newOffset = offset + ptOffset;
653
654			if (!(rSize.x == 0.0f && rSize.y == 0.0f)) {
655				fFrame.OffsetBy(offset);
656				fFrame.right += rSize.x;
657				fFrame.bottom += rSize.y;
658				RebuildFullRegion();
659
660				// TODO: investigate combining frame event messages for efficiency
661				//SendViewResizedMsg();
662				AddToViewsWithInvalidCoords();
663
664				newAction = B_LAYER_MASK_RESIZE;
665				newPt = rSize;
666				dummyNewLocation = newOffset;
667			} else {
668				if (!(offset.x == 0.0f && offset.y == 0.0f)) {
669					pt = newOffset;
670					action = B_LAYER_MOVE;
671					newPt = pt;
672					goto RRLabel1;
673				} else {
674					pt = ptOffset;
675					action = B_LAYER_MOVE;
676					newPt = pt;
677					goto RRLabel1;
678				}
679			}
680			break;
681		}
682	}
683
684	if (!IsHidden()) {
685		#ifdef DEBUG_LAYER_REBUILD
686			printf("Layer(%s) real action START\n", Name());
687			fFull.PrintToStream();
688		#endif
689		fFullVisible.MakeEmpty();
690		fVisible = fFull;
691
692		if (fParent && fVisible.CountRects() > 0) {
693			// not the usual case, but support fot this is needed.
694			if (fParent->fAdFlags & B_LAYER_CHILDREN_DEPENDANT) {
695				#ifdef DEBUG_LAYER_REBUILD
696					printf("   B_LAYER_CHILDREN_DEPENDANT Parent\n");
697				#endif
698
699				// because we're skipping one level, we need to do out
700				// parent business as well.
701
702				// our visible area is relative to our parent's parent.
703				if (fParent->fParent)
704					fVisible.IntersectWith(&(fParent->fParent->fVisible));
705
706				// exclude parent's visible area which could be composed by
707				// prior siblings' visible areas.
708				if (fVisible.CountRects() > 0)
709					fVisible.Exclude(&(fParent->fVisible));
710
711				// we have a final visible area. Include it to our parent's one,
712				// exclude from parent's parent.
713				if (fVisible.CountRects() > 0) {
714					fParent->fFullVisible.Include(&fVisible);
715
716					if (fParent->fParent)
717						fParent->fParent->fVisible.Exclude(&fVisible);
718				}
719			} else {
720				// for 95+% of cases
721
722				#ifdef DEBUG_LAYER_REBUILD
723					printf("   (!)B_LAYER_CHILDREN_DEPENDANT Parent\n");
724				#endif
725
726				// the visible area is the one common with parent's one.
727				fVisible.IntersectWith(&(fParent->fVisible));
728
729				// exclude from parent's visible area. we're the owners now.
730				if (fVisible.CountRects() > 0)
731					fParent->fVisible.Exclude(&fVisible);
732			}
733		}
734		fFullVisible = fVisible;
735	}
736
737	// Rebuild regions for children...
738	for(Layer *lay = LastChild(); lay != NULL; lay = PreviousChild())
739		lay->RebuildRegions(reg, newAction, newPt, newOffset);
740
741	#ifdef DEBUG_LAYER_REBUILD
742		printf("\nLayer(%s) ALMOST done regions:\n", Name());
743		printf("\tVisible Region:\n");
744		fVisible.PrintToStream();
745		printf("\tFull Visible Region:\n");
746		fFullVisible.PrintToStream();
747	#endif
748
749	if(!IsHidden()) {
750		switch(action) {
751			case B_LAYER_NONE: {
752				RBTRACE(("2) Layer(%s): Action B_LAYER_NONE\n", Name()));
753				BRegion r(fVisible);
754				if (oldRegion.CountRects() > 0)
755					r.Exclude(&oldRegion);
756
757				if(r.CountRects() > 0)
758					fRootLayer->fRedrawReg.Include(&r);
759				break;
760			}
761			case B_LAYER_MOVE: {
762				RBTRACE(("2) Layer(%s): Action B_LAYER_MOVE\n", Name()));
763				BRegion redrawReg;
764				BRegion	*copyReg = new BRegion();
765				BRegion	screenReg(fRootLayer->Bounds());
766
767				oldRegion.OffsetBy(pt.x, pt.y);
768				oldRegion.IntersectWith(&fFullVisible);
769
770				*copyReg = oldRegion;
771				copyReg->IntersectWith(&screenReg);
772				if (copyReg->CountRects() > 0 && !(pt.x == 0.0f && pt.y == 0.0f)) {
773					copyReg->OffsetBy(-pt.x, -pt.y);
774					BPoint		*point = new BPoint(pt);
775					fRootLayer->fCopyRegList.AddItem(copyReg);
776					fRootLayer->fCopyList.AddItem(point);
777				} else {
778					delete copyReg;
779				}
780
781				redrawReg	= fFullVisible;
782				redrawReg.Exclude(&oldRegion);
783				if (redrawReg.CountRects() > 0 && !(pt.x == 0.0f && pt.y == 0.0f)) {
784					fRootLayer->fRedrawReg.Include(&redrawReg);
785				}
786
787				break;
788			}
789			case B_LAYER_RESIZE: {
790				RBTRACE(("2) Layer(%s): Action B_LAYER_RESIZE\n", Name()));
791				BRegion redrawReg;
792
793				redrawReg = fVisible;
794				redrawReg.Exclude(&oldRegion);
795				if(redrawReg.CountRects() > 0)
796					fRootLayer->fRedrawReg.Include(&redrawReg);
797
798				break;
799			}
800			case B_LAYER_MASK_RESIZE: {
801				RBTRACE(("2) Layer(%s): Action B_LAYER_MASK_RESIZE\n", Name()));
802				BRegion redrawReg;
803				BRegion	*copyReg = new BRegion();
804
805				oldRegion.OffsetBy(dummyNewLocation.x, dummyNewLocation.y);
806
807				redrawReg	= fVisible;
808				redrawReg.Exclude(&oldRegion);
809				if (redrawReg.CountRects() > 0)
810					fRootLayer->fRedrawReg.Include(&redrawReg);
811
812				*copyReg = fVisible;
813				copyReg->IntersectWith(&oldRegion);
814				copyReg->OffsetBy(-dummyNewLocation.x, -dummyNewLocation.y);
815				if (copyReg->CountRects() > 0
816					&& !(dummyNewLocation.x == 0.0f && dummyNewLocation.y == 0.0f)) {
817					fRootLayer->fCopyRegList.AddItem(copyReg);
818					fRootLayer->fCopyList.AddItem(new BPoint(dummyNewLocation));
819				}
820
821				break;
822			}
823			default:
824				RBTRACE(("2) Layer(%s): Action default\n", Name()));
825				break;
826		}
827	}
828/*	if (IsHidden()) {
829		fFullVisible.MakeEmpty();
830		fVisible.MakeEmpty();
831	}
832*/
833
834	STRACE(("Layer(%s)::RebuildRegions() END\n", Name()));
835}
836
837// ResizeOthers
838uint32
839Layer::ResizeOthers(float x, float y, BPoint coords[], BPoint *ptOffset)
840{
841	STRACE(("Layer(%s)::ResizeOthers() START\n", Name()));
842	uint32 rmask = fResizeMode;
843
844	// offset
845	coords[0].x	= 0.0f;
846	coords[0].y	= 0.0f;
847
848	// resize by width/height
849	coords[1].x	= 0.0f;
850	coords[1].y	= 0.0f;
851
852	if ((rmask & 0x00000f00UL)>>8 == _VIEW_LEFT_ &&
853		(rmask & 0x0000000fUL)>>0 == _VIEW_RIGHT_) {
854		coords[1].x		= x;
855	} else if ((rmask & 0x00000f00UL)>>8 == _VIEW_LEFT_) {
856	} else if ((rmask & 0x0000000fUL)>>0 == _VIEW_RIGHT_) {
857		coords[0].x		= x;
858	} else if ((rmask & 0x00000f00UL)>>8 == _VIEW_CENTER_) {
859		coords[0].x		= x/2;
860	} else {
861		// illegal flag. Do nothing.
862	}
863
864
865	if ((rmask & 0x0000f000UL)>>12 == _VIEW_TOP_ &&
866		(rmask & 0x000000f0UL)>>4 == _VIEW_BOTTOM_) {
867		coords[1].y		= y;
868	} else if ((rmask & 0x0000f000UL)>>12 == _VIEW_TOP_) {
869	} else if ((rmask & 0x000000f0UL)>>4 == _VIEW_BOTTOM_) {
870		coords[0].y		= y;
871	} else if ((rmask & 0x0000f000UL)>>12 == _VIEW_CENTER_) {
872		coords[0].y		= y/2;
873	} else {
874		// illegal flag. Do nothing.
875	}
876
877	STRACE(("Layer(%s)::ResizeOthers() END\n", Name()));
878	return 0UL;
879}
880
881#endif
882
883// Redraw
884void
885Layer::Redraw(const BRegion& reg, Layer *startFrom)
886{
887	STRACE(("Layer(%s)::Redraw();\n", Name()));
888	if (IsHidden())
889		// this layer has nothing visible on screen, so bail out.
890		return;
891
892	BRegion *pReg = const_cast<BRegion*>(&reg);
893
894	if (pReg->CountRects() > 0)
895		RequestDraw(reg, startFrom);
896
897	STRACE(("Layer(%s)::Redraw() ENDED\n", Name()));
898}
899
900// Draw
901void
902Layer::Draw(const BRect &rect)
903{
904#ifdef DEBUG_LAYER
905	printf("Layer(%s)::Draw: ", Name());
906	rect.PrintToStream();
907#endif
908
909	if (!ViewColor().IsTransparentMagic())
910		fDriver->FillRect(rect, ViewColor());
911}
912
913#ifndef NEW_CLIPPING
914// EmptyGlobals
915void
916Layer::EmptyGlobals()
917{
918	fRootLayer->fRedrawReg.MakeEmpty();
919
920	int32 count = fRootLayer->fCopyRegList.CountItems();
921	for (int32 i = 0; i < count; i++)
922		delete (BRegion*)fRootLayer->fCopyRegList.ItemAt(i);
923	fRootLayer->fCopyRegList.MakeEmpty();
924
925	count = fRootLayer->fCopyList.CountItems();
926	for (int32 i = 0; i < count; i++)
927		delete (BPoint*)fRootLayer->fCopyList.ItemAt(i);
928	fRootLayer->fCopyList.MakeEmpty();
929}
930#endif
931
932/*!
933	\brief Shows the layer
934	\param invalidate Invalidate the region when showing the layer. defaults to true
935*/
936void
937Layer::Show(bool invalidate)
938{
939	STRACE(("Layer(%s)::Show()\n", Name()));
940	if(!IsHidden()) {
941		// an ancestor may be hidden. OK, we're not visible,
942		// but we're changing our visibility state
943		fHidden	= false;
944		return;
945	}
946
947	fHidden	= false;
948
949	SendViewCoordUpdateMsg();
950
951// NOTE: I added this here and it solves the invalid region problem
952// for Windows that have been resized before they were shown. -Stephan
953#ifndef NEW_CLIPPING
954RebuildFullRegion();
955
956	if (invalidate)
957		GetRootLayer()->GoInvalidate(this, fFull);
958#else
959	if (invalidate) {
960		// compute the region this layer wants for itself
961		BRegion	invalidRegion;
962		_GetWantedRegion(invalidRegion);
963		if (invalidRegion.CountRects() > 0)
964			GetRootLayer()->GoInvalidate(this, invalidRegion);
965	}
966#endif
967}
968
969/*!
970	\brief Shows the layer
971	\param invalidate Invalidate the region when hiding the layer. defaults to true
972*/
973void
974Layer::Hide(bool invalidate)
975{
976	STRACE(("Layer(%s)::Hide()\n", Name()));
977	if (IsHidden()) {
978		// an ancestor may be hidden. OK, we're not visible,
979		// but we're changing our visibility state
980		fHidden	= true;
981		return;
982	}
983
984	fHidden	= true;
985#ifndef NEW_CLIPPING
986	if (invalidate)
987		GetRootLayer()->GoInvalidate(this, fFullVisible);
988#else
989	if (invalidate && fFullVisible2.CountRects() > 0) {
990		GetRootLayer()->GoInvalidate(this, fFullVisible2);
991	}
992#endif
993}
994
995//! Returns true if the layer is hidden
996bool
997Layer::IsHidden(void) const
998{
999	if (fHidden)
1000		return true;
1001
1002	if (fParent)
1003			return fParent->IsHidden();
1004
1005	return fHidden;
1006}
1007
1008
1009void
1010Layer::PushState()
1011{
1012	LayerData *data = new LayerData(fLayerData);
1013	fLayerData = data;
1014
1015	fLayerData->SetSubPixelPrecise(fFlags & B_SUBPIXEL_PRECISE);
1016}
1017
1018
1019void
1020Layer::PopState()
1021{
1022	if (fLayerData->prevState == NULL) {
1023		fprintf(stderr, "WARNING: User called BView(%s)::PopState(), but there is NO state on stack!\n", Name());
1024		return;
1025	}
1026
1027	LayerData *data = fLayerData;
1028	fLayerData = fLayerData->prevState;
1029	data->prevState = NULL;
1030	delete data;
1031
1032	fLayerData->SetSubPixelPrecise(fFlags & B_SUBPIXEL_PRECISE);
1033}
1034
1035
1036//! Matches the BView call of the same name
1037BRect
1038Layer::Bounds(void) const
1039{
1040	BRect r(fFrame);
1041//	r.OffsetTo(fBoundsLeftTop);
1042	r.OffsetTo(BoundsOrigin());
1043	return r;
1044}
1045
1046//! Matches the BView call of the same name
1047BRect
1048Layer::Frame(void) const
1049{
1050	return fFrame;
1051}
1052
1053//! Moves the layer by specified values, complete with redraw
1054void
1055Layer::MoveBy(float x, float y)
1056{
1057	STRACE(("Layer(%s)::MoveBy() START\n", Name()));
1058	if (!fParent) {
1059		CRITICAL("ERROR: in Layer::MoveBy()! - No parent!\n");
1060		return;
1061	}
1062
1063	GetRootLayer()->Lock();
1064#ifndef NEW_CLIPPING
1065	move_layer(x, y);
1066#else
1067	do_MoveBy(x, y);
1068#endif
1069	GetRootLayer()->Unlock();
1070
1071	STRACE(("Layer(%s)::MoveBy() END\n", Name()));
1072}
1073
1074//! Resize the layer by the specified amount, complete with redraw
1075void
1076Layer::ResizeBy(float x, float y)
1077{
1078	STRACE(("Layer(%s)::ResizeBy() START\n", Name()));
1079
1080	if (!fParent) {
1081		printf("ERROR: in Layer::ResizeBy()! - No parent!\n");
1082		return;
1083	}
1084
1085	GetRootLayer()->Lock();
1086#ifndef NEW_CLIPPING
1087	resize_layer(x, y);
1088#else
1089	do_ResizeBy(x, y);
1090#endif
1091	GetRootLayer()->Unlock();
1092
1093	STRACE(("Layer(%s)::ResizeBy() END\n", Name()));
1094}
1095
1096//! scrolls the layer by the specified amount, complete with redraw
1097void
1098Layer::ScrollBy(float x, float y)
1099{
1100	STRACE(("Layer(%s)::ScrollBy() START\n", Name()));
1101
1102	GetRootLayer()->Lock();
1103#ifndef NEW_CLIPPING
1104	// nothing
1105#else
1106	do_ScrollBy(x, y);
1107#endif
1108	GetRootLayer()->Unlock();
1109
1110	STRACE(("Layer(%s)::ScrollBy() END\n", Name()));
1111}
1112
1113void
1114Layer::MouseDown(const PointerEvent& evt)
1115{
1116	if (Window() && !IsTopLayer()) {
1117		BMessage msg;
1118		msg.what = B_MOUSE_DOWN;
1119		msg.AddInt64("when", evt.when);
1120		msg.AddPoint("where", evt.where);
1121		msg.AddInt32("modifiers", evt.modifiers);
1122		msg.AddInt32("buttons", evt.buttons);
1123		msg.AddInt32("clicks", evt.clicks);
1124
1125		Window()->SendMessageToClient(&msg, fViewToken, false);
1126	}
1127}
1128
1129void
1130Layer::MouseUp(const PointerEvent& evt)
1131{
1132	if (Window() && !IsTopLayer()) {
1133		BMessage upmsg(B_MOUSE_UP);
1134		upmsg.AddInt64("when",evt.when);
1135		upmsg.AddPoint("where",evt.where);
1136		upmsg.AddInt32("modifiers",evt.modifiers);
1137
1138		Window()->SendMessageToClient(&upmsg, fViewToken, false);
1139	}
1140}
1141
1142void
1143Layer::MouseMoved(const PointerEvent& evt, uint32 transit)
1144{
1145	if (Window() && !IsTopLayer()) {
1146		BMessage movemsg(B_MOUSE_MOVED);
1147		movemsg.AddInt64("when", evt.when);
1148		movemsg.AddPoint("where", evt.where);
1149		movemsg.AddInt32("buttons", evt.buttons);
1150		movemsg.AddInt32("transit", transit);
1151
1152		Window()->SendMessageToClient(&movemsg, fViewToken, false);
1153	}
1154}
1155
1156void
1157Layer::MouseWheelChanged(const PointerEvent& evt)
1158{
1159	if (Window() && !IsTopLayer()) {
1160		BMessage wheelmsg(B_MOUSE_WHEEL_CHANGED);
1161		wheelmsg.AddInt64("when", evt.when);
1162		wheelmsg.AddFloat("be:wheel_delta_x",evt.wheel_delta_x);
1163		wheelmsg.AddFloat("be:wheel_delta_y",evt.wheel_delta_y);
1164
1165		Window()->SendMessageToClient(&wheelmsg, fViewToken, false);
1166	}
1167}
1168
1169void
1170Layer::WorkspaceActivated(int32 index, bool active)
1171{
1172	// Empty
1173}
1174
1175void
1176Layer::WorkspacesChanged(uint32 oldWorkspaces, uint32 newWorkspaces)
1177{
1178	// Empty
1179}
1180
1181// BoundsOrigin
1182BPoint
1183Layer::BoundsOrigin() const
1184{
1185	BPoint origin(0,0);
1186	float scale = Scale();
1187
1188	LayerData *ld = fLayerData;
1189	do {
1190		origin += ld->Origin();
1191	} while ((ld = ld->prevState));
1192
1193	origin.x *= scale;
1194	origin.y *= scale;
1195
1196	return origin;
1197}
1198
1199float
1200Layer::Scale() const
1201{
1202	float scale = 1.0f;
1203
1204	LayerData *ld = fLayerData;
1205	do {
1206		scale *= ld->Scale();
1207	} while ((ld = ld->prevState));
1208
1209	return scale;
1210}
1211
1212//! Converts the passed point to parent coordinates
1213BPoint
1214Layer::ConvertToParent(BPoint pt)
1215{
1216#ifndef NEW_CLIPPING
1217	pt -= BoundsOrigin();
1218	pt += fFrame.LeftTop();
1219	return pt;
1220#else
1221	ConvertToParent2(&pt);
1222	return pt;
1223#endif
1224}
1225
1226//! Converts the passed rectangle to parent coordinates
1227BRect
1228Layer::ConvertToParent(BRect rect)
1229{
1230#ifndef NEW_CLIPPING
1231//	rect.OffsetBy(fFrame.LeftTop());
1232//	return rect;
1233	rect.OffsetBy(-BoundsOrigin().x, -BoundsOrigin().y);
1234	rect.OffsetBy(fFrame.LeftTop());
1235	return rect;
1236#else
1237	ConvertToParent2(&rect);
1238	return rect;
1239#endif
1240}
1241
1242//! Converts the passed region to parent coordinates
1243BRegion
1244Layer::ConvertToParent(BRegion* reg)
1245{
1246#ifndef NEW_CLIPPING
1247	// TODO: wouldn't it be more efficient to use the copy
1248	// constructor for BRegion and then call OffsetBy()?
1249	BRegion newreg;
1250	for (int32 i = 0; i < reg->CountRects(); i++)
1251		newreg.Include(ConvertToParent(reg->RectAt(i)));
1252	return newreg;
1253#else
1254	BRegion newReg(*reg);
1255	ConvertToParent2(&newReg);
1256	return newReg;
1257#endif
1258}
1259
1260//! Converts the passed point from parent coordinates
1261BPoint
1262Layer::ConvertFromParent(BPoint pt)
1263{
1264#ifndef NEW_CLIPPING
1265//	return pt - fFrame.LeftTop();
1266	pt -= fFrame.LeftTop();
1267	pt += BoundsOrigin();
1268	return pt;
1269#else
1270	ConvertFromParent2(&pt);
1271	return pt;
1272#endif
1273}
1274
1275//! Converts the passed rectangle from parent coordinates
1276BRect
1277Layer::ConvertFromParent(BRect rect)
1278{
1279#ifndef NEW_CLIPPING
1280//	rect.OffsetBy(-fFrame.left, -fFrame.top);
1281//	return rect;
1282	rect.OffsetBy(-fFrame.left, -fFrame.top);
1283	rect.OffsetBy(BoundsOrigin());
1284	return rect;
1285#else
1286	ConvertFromParent2(&rect);
1287	return rect;
1288#endif
1289}
1290
1291//! Converts the passed region from parent coordinates
1292BRegion
1293Layer::ConvertFromParent(BRegion *reg)
1294{
1295#ifndef NEW_CLIPPING
1296	BRegion newreg;
1297	for(int32 i=0; i<reg->CountRects();i++)
1298		newreg.Include(ConvertFromParent(reg->RectAt(i)));
1299	return newreg;
1300#else
1301	BRegion newReg(*reg);
1302	ConvertFromParent2(&newReg);
1303	return newReg;
1304#endif
1305}
1306
1307// ConvertToTop
1308BPoint
1309Layer::ConvertToTop(BPoint pt)
1310{
1311#ifndef NEW_CLIPPING
1312	if (fParent) {
1313//		return (fParent->ConvertToTop(pt + fFrame.LeftTop()));
1314		pt = ConvertToParent(pt);
1315		return fParent->ConvertToTop(pt);
1316	} else
1317		return pt;
1318#else
1319	ConvertToScreen2(&pt);
1320	return pt;
1321#endif
1322}
1323
1324//! Converts the passed rectangle to screen coordinates
1325BRect
1326Layer::ConvertToTop(BRect rect)
1327{
1328#ifndef NEW_CLIPPING
1329	if (fParent) {
1330//		return fParent->ConvertToTop(rect.OffsetByCopy(fFrame.LeftTop()));
1331		rect = ConvertToParent(rect);
1332		return fParent->ConvertToTop(rect);
1333	} else
1334		return rect;
1335#else
1336	ConvertToScreen2(&rect);
1337	return rect;
1338#endif
1339}
1340
1341//! Converts the passed region to screen coordinates
1342BRegion
1343Layer::ConvertToTop(BRegion *reg)
1344{
1345#ifndef NEW_CLIPPING
1346	BRegion newreg;
1347	for (int32 i = 0; i < reg->CountRects();i++)
1348		newreg.Include(ConvertToTop(reg->RectAt(i)));
1349	return newreg;
1350#else
1351	BRegion newReg(*reg);
1352	ConvertToScreen2(&newReg);
1353	return newReg;
1354#endif
1355}
1356
1357// ConvertFromTop
1358BPoint
1359Layer::ConvertFromTop(BPoint pt)
1360{
1361#ifndef NEW_CLIPPING
1362	if (fParent) {
1363//		return fParent->ConvertFromTop(pt-fFrame.LeftTop());
1364		pt = ConvertFromParent(pt);
1365		return fParent->ConvertFromTop(pt);
1366	} else
1367		return pt;
1368#else
1369	ConvertFromScreen2(&pt);
1370	return pt;
1371#endif
1372}
1373
1374//! Converts the passed rectangle from screen coordinates
1375BRect
1376Layer::ConvertFromTop(BRect rect)
1377{
1378#ifndef NEW_CLIPPING
1379	if (fParent) {
1380//		return fParent->ConvertFromTop(rect.OffsetByCopy(-fFrame.LeftTop().x,
1381//														 -fFrame.LeftTop().y));
1382		rect = ConvertFromParent(rect);
1383		return fParent->ConvertFromTop(rect);
1384	} else
1385		return rect;
1386#else
1387	ConvertFromScreen2(&rect);
1388	return rect;
1389#endif
1390}
1391
1392//! Converts the passed region from screen coordinates
1393BRegion
1394Layer::ConvertFromTop(BRegion *reg)
1395{
1396#ifndef NEW_CLIPPING
1397	BRegion newreg;
1398
1399	for (int32 i = 0; i < reg->CountRects(); i++)
1400		newreg.Include(ConvertFromTop(reg->RectAt(i)));
1401
1402	return newreg;
1403#else
1404	BRegion newReg(*reg);
1405	ConvertFromScreen2(&newReg);
1406	return newReg;
1407#endif
1408}
1409
1410//! Recursively deletes all children of the calling layer
1411void
1412Layer::PruneTree(void)
1413{
1414
1415	Layer* lay;
1416	Layer* nextlay;
1417
1418	lay = fFirstChild;
1419	fFirstChild = NULL;
1420
1421	while (lay != NULL) {
1422		if (lay->fFirstChild != NULL)
1423			lay->PruneTree();
1424
1425		nextlay = lay->fNextSibling;
1426		lay->fNextSibling = NULL;
1427
1428		delete lay;
1429		lay = nextlay;
1430	}
1431	// Man, this thing is short. Elegant, ain't it? :P
1432}
1433
1434//! Prints information about the layer's current state
1435void
1436Layer::PrintToStream()
1437{
1438	printf("\n *** Layer %s:\n", Name());
1439	printf("\t Parent: %s", fParent ? fParent->Name() : "<no parent>");
1440
1441	printf("\t us: %s\t ls: %s\n",
1442		fPreviousSibling ? fPreviousSibling->Name() : "<none>",
1443		fNextSibling ? fNextSibling->Name() : "<none>");
1444
1445	printf("\t topChild: %s\t bottomChild: %s\n",
1446		fFirstChild ? fFirstChild->Name() : "<none>",
1447		fLastChild ? fLastChild->Name() : "<none>");
1448
1449	printf("Frame: (%f, %f, %f, %f)\n", fFrame.left, fFrame.top, fFrame.right, fFrame.bottom);
1450	printf("LocalOrigin: (%f, %f)\n", BoundsOrigin().x, BoundsOrigin().y);
1451	printf("Token: %ld\n", fViewToken);
1452	printf("Hidden - direct: %s ", fHidden?"true":"false");
1453	printf("Hidden - indirect: %s\n", IsHidden()?"true":"false");
1454	printf("ResizingMode: %lx ", fResizeMode);
1455	printf("Flags: %lx\n", fFlags);
1456
1457	if (fLayerData)
1458		fLayerData->PrintToStream();
1459	else
1460		printf(" NO LayerData valid pointer\n");
1461}
1462
1463//! Prints pointer info kept by the current layer
1464void
1465Layer::PrintNode()
1466{
1467	printf("-----------\nLayer %s\n", Name());
1468	if (fParent)
1469		printf("Parent: %s (%p)\n", fParent->Name(), fParent);
1470	else
1471		printf("Parent: NULL\n");
1472
1473	if (fPreviousSibling)
1474		printf("Upper sibling: %s (%p)\n", fPreviousSibling->Name(), fPreviousSibling);
1475	else
1476		printf("Upper sibling: NULL\n");
1477
1478	if (fNextSibling)
1479		printf("Lower sibling: %s (%p)\n", fNextSibling->Name(), fNextSibling);
1480	else
1481		printf("Lower sibling: NULL\n");
1482
1483	if (fFirstChild)
1484		printf("Top child: %s (%p)\n", fFirstChild->Name(), fFirstChild);
1485	else
1486		printf("Top child: NULL\n");
1487
1488	if (fLastChild)
1489		printf("Bottom child: %s (%p)\n", fLastChild->Name(), fLastChild);
1490	else
1491		printf("Bottom child: NULL\n");
1492#ifndef NEW_CLIPPING
1493	printf("Visible Areas: "); fVisible.PrintToStream();
1494#endif
1495}
1496
1497//! Prints the tree hierarchy from the current layer down
1498void
1499Layer::PrintTree()
1500{
1501	printf("\n Tree structure:\n");
1502	printf("\t%s\t%s\n", Name(), IsHidden()? "Hidden": "NOT hidden");
1503	for(Layer *lay = LastChild(); lay != NULL; lay = PreviousChild())
1504		printf("\t%s\t%s\n", lay->Name(), lay->IsHidden()? "Hidden": "NOT hidden");
1505}
1506
1507// RequestDraw
1508void
1509Layer::RequestDraw(const BRegion &reg, Layer *startFrom)
1510{
1511	STRACE(("Layer(%s)::RequestDraw()\n", Name()));
1512
1513	// do not redraw any child until you must
1514	int redraw = false;
1515	if (!startFrom)
1516		redraw = true;
1517
1518#ifndef NEW_CLIPPING
1519	if (HasClient() && IsTopLayer()) {
1520		// calculate the minimum region/rectangle to be updated with
1521		// a single message to the client.
1522		BRegion	updateReg(fFullVisible);
1523
1524		if (fFlags & B_FULL_UPDATE_ON_RESIZE
1525			&& fFrameAction	== B_LAYER_ACTION_RESIZE)
1526		{
1527			// do nothing
1528		} else {
1529			updateReg.IntersectWith(&reg);
1530		}
1531		if (updateReg.CountRects() > 0) {
1532			fOwner->fCumulativeRegion.Include(&updateReg);
1533			if (!fOwner->InUpdate() && !fOwner->fRequestSent) {
1534				fOwner->fInUpdateRegion = fOwner->fCumulativeRegion;
1535fOwner->cnt++;
1536if (fOwner->cnt != 1)
1537	CRITICAL("Layer::RequestDraw(): fOwner->cnt != 1 -> Not Allowed!");
1538				fOwner->fCumulativeRegion.MakeEmpty();
1539				fOwner->fRequestSent = true;
1540				SendUpdateMsg(fOwner->fInUpdateRegion);
1541			}
1542		}
1543	}
1544
1545	if (fVisible.CountRects() > 0) {
1546		BRegion	updateReg(fVisible);
1547		// calculate the update region
1548		if (fFlags & B_FULL_UPDATE_ON_RESIZE && fFrameAction == B_LAYER_ACTION_RESIZE) {
1549			// do nothing
1550		} else {
1551			updateReg.IntersectWith(&reg);
1552		}
1553
1554		if (updateReg.CountRects() > 0) {
1555			fDriver->ConstrainClippingRegion(&updateReg);
1556			Draw(updateReg.Frame());
1557			fDriver->ConstrainClippingRegion(NULL);
1558		}
1559	}
1560
1561	for (Layer *lay = LastChild(); lay != NULL; lay = PreviousChild()) {
1562		if (lay == startFrom)
1563			redraw = true;
1564
1565		if (redraw && !(lay->IsHidden())) {
1566			// no need to go deeper if not even the FullVisible region intersects
1567			// Update one.
1568			BRegion common(lay->fFullVisible);
1569			common.IntersectWith(&reg);
1570
1571			if (common.CountRects() > 0)
1572				lay->RequestDraw(reg, NULL);
1573		}
1574	}
1575#else
1576	if (HasClient() && IsTopLayer()) {
1577		// calculate the minimum region/rectangle to be updated with
1578		// a single message to the client.
1579		BRegion	updateReg(fFullVisible2);
1580
1581		updateReg.IntersectWith(&reg);
1582
1583		if (updateReg.CountRects() > 0) {
1584			fOwner->fCumulativeRegion.Include(&updateReg);
1585			if (!fOwner->InUpdate() && !fOwner->fRequestSent) {
1586				fOwner->fInUpdateRegion = fOwner->fCumulativeRegion;
1587fOwner->cnt++;
1588if (fOwner->cnt != 1)
1589	CRITICAL("Layer::RequestDraw(): fOwner->cnt != 1 -> Not Allowed!");
1590				fOwner->fCumulativeRegion.MakeEmpty();
1591				fOwner->fRequestSent = true;
1592				SendUpdateMsg(fOwner->fInUpdateRegion);
1593			}
1594		}
1595	}
1596
1597	if (fVisible2.CountRects() > 0) {
1598		BRegion	updateReg(fVisible2);
1599			updateReg.IntersectWith(&reg);
1600
1601		if (updateReg.CountRects() > 0) {
1602			fDriver->ConstrainClippingRegion(&updateReg);
1603			Draw(updateReg.Frame());
1604			fDriver->ConstrainClippingRegion(NULL);
1605		}
1606	}
1607
1608	for (Layer *lay = LastChild(); lay != NULL; lay = PreviousChild()) {
1609		if (lay == startFrom)
1610			redraw = true;
1611
1612		if (redraw && !(lay->IsHidden())) {
1613			// no need to go deeper if not even the FullVisible region intersects
1614			// Update one.
1615			BRegion common(lay->fFullVisible2);
1616			common.IntersectWith(&reg);
1617
1618			if (common.CountRects() > 0)
1619				lay->RequestDraw(reg, NULL);
1620		}
1621	}
1622#endif
1623}
1624
1625#ifndef NEW_CLIPPING
1626
1627// move_layer
1628void
1629Layer::move_layer(float x, float y)
1630{
1631/*	if (fClassID == AS_WINBORDER_CLASS) {
1632		WinBorder	*wb = (WinBorder*)this;
1633		wb->fCumulativeRegion.OffsetBy(x, y);
1634		wb->fInUpdateRegion.OffsetBy(x, y);
1635		wb->fSavedForUpdateRegion.OffsetBy(x, y);
1636	}*/
1637
1638	fFrameAction = B_LAYER_ACTION_MOVE;
1639
1640	BPoint pt(x, y);
1641	BRect rect(fFull.Frame().OffsetByCopy(pt));
1642
1643if (!fParent) {
1644printf("no parent in Layer::move_layer() (%s)\n", Name());
1645fFrameAction = B_LAYER_ACTION_NONE;
1646return;
1647}
1648
1649	fParent->StartRebuildRegions(BRegion(rect), this, B_LAYER_MOVE, pt);
1650
1651	fDriver->CopyRegionList(&fRootLayer->fCopyRegList,
1652							&fRootLayer->fCopyList,
1653							fRootLayer->fCopyRegList.CountItems(),
1654							&fFullVisible);
1655
1656	fParent->Redraw(fRootLayer->fRedrawReg, this);
1657
1658	// redraw workspaces layer
1659	if (dynamic_cast<WinBorder*>(this) != NULL && fRootLayer->WorkspacesLayer() != NULL) {
1660		fRootLayer->GoRedraw(fRootLayer->WorkspacesLayer(), fRootLayer->WorkspacesLayer()->fVisible);
1661	}
1662
1663	SendViewCoordUpdateMsg();
1664
1665	EmptyGlobals();
1666
1667	fFrameAction = B_LAYER_ACTION_NONE;
1668}
1669
1670// resize_layer
1671void
1672Layer::resize_layer(float x, float y)
1673{
1674	fFrameAction = B_LAYER_ACTION_RESIZE;
1675
1676	BPoint pt(x,y);
1677
1678	BRect rect(fFull.Frame());
1679	rect.right += x;
1680	rect.bottom += y;
1681
1682if (!fParent) {
1683printf("no parent in Layer::resize_layer() (%s)\n", Name());
1684fFrameAction = B_LAYER_ACTION_NONE;
1685return;
1686}
1687
1688	fParent->StartRebuildRegions(BRegion(rect), this, B_LAYER_RESIZE, pt);
1689
1690	fDriver->CopyRegionList(&fRootLayer->fCopyRegList, &fRootLayer->fCopyList, fRootLayer->fCopyRegList.CountItems(), &fFullVisible);
1691
1692	fParent->Redraw(fRootLayer->fRedrawReg, this);
1693
1694	// redraw workspaces layer
1695	if (dynamic_cast<WinBorder*>(this) != NULL && fRootLayer->WorkspacesLayer() != NULL) {
1696		fRootLayer->GoRedraw(fRootLayer->WorkspacesLayer(), fRootLayer->WorkspacesLayer()->fVisible);
1697	}
1698
1699	SendViewCoordUpdateMsg();
1700
1701	EmptyGlobals();
1702
1703	fFrameAction = B_LAYER_ACTION_NONE;
1704}
1705
1706// FullInvalidate
1707void
1708Layer::FullInvalidate(const BRect &rect)
1709{
1710	FullInvalidate(BRegion(rect));
1711}
1712
1713// FullInvalidate
1714void
1715Layer::FullInvalidate(const BRegion& region)
1716{
1717	STRACE(("Layer(%s)::FullInvalidate():\n", Name()));
1718
1719#ifdef DEBUG_LAYER
1720	region.PrintToStream();
1721	printf("\n");
1722#endif
1723
1724	BPoint pt(0,0);
1725	StartRebuildRegions(region, NULL,/* B_LAYER_INVALIDATE, pt); */B_LAYER_NONE, pt);
1726
1727	Redraw(fRootLayer->fRedrawReg);
1728
1729	EmptyGlobals();
1730}
1731
1732// Invalidate
1733void
1734Layer::Invalidate(const BRegion& region)
1735{
1736	STRACE(("Layer(%s)::Invalidate():\n", Name()));
1737#ifdef DEBUG_LAYER
1738	region.PrintToStream();
1739	printf("\n");
1740#endif
1741
1742	fRootLayer->fRedrawReg	= region;
1743
1744	Redraw(fRootLayer->fRedrawReg);
1745
1746	EmptyGlobals();
1747}
1748
1749#endif // 5 methods
1750
1751
1752/*!
1753	\brief Returns the layer's ServerWindow
1754
1755	If the layer's ServerWindow has not been assigned, it attempts to find
1756	the owning ServerWindow in the tree.
1757*/
1758ServerWindow*
1759Layer::SearchForServerWindow()
1760{
1761	if (!fServerWin)
1762		fServerWin=fParent->SearchForServerWindow();
1763
1764	return fServerWin;
1765}
1766
1767//! Sends an _UPDATE_ message to the client BWindow
1768void
1769Layer::SendUpdateMsg(BRegion& reg)
1770{
1771	BMessage msg;
1772	msg.what = _UPDATE_;
1773#ifndef NEW_CLIPPING
1774	msg.AddRect("_rect", ConvertFromTop(reg.Frame()));
1775#else
1776	BRect	rect(reg.Frame());
1777	ConvertFromScreen2(&rect);
1778	msg.AddRect("_rect", rect );
1779#endif
1780	msg.AddRect("debug_rect", reg.Frame());
1781//	msg.AddInt32("_token",fViewToken);
1782
1783	fOwner->Window()->SendMessageToClient(&msg);
1784}
1785
1786// AddToViewsWithInvalidCoords
1787void
1788Layer::AddToViewsWithInvalidCoords() const
1789{
1790	if (fServerWin) {
1791		fServerWin->ClientViewsWithInvalidCoords().AddInt32("_token", fViewToken);
1792		fServerWin->ClientViewsWithInvalidCoords().AddPoint("where", fFrame.LeftTop());
1793		fServerWin->ClientViewsWithInvalidCoords().AddFloat("width", fFrame.Width());
1794		fServerWin->ClientViewsWithInvalidCoords().AddFloat("height", fFrame.Height());
1795	}
1796}
1797
1798// SendViewCoordUpdateMsg
1799void
1800Layer::SendViewCoordUpdateMsg() const
1801{
1802	if (fServerWin && !fServerWin->ClientViewsWithInvalidCoords().IsEmpty()) {
1803		fServerWin->SendMessageToClient(&fServerWin->ClientViewsWithInvalidCoords());
1804		fServerWin->ClientViewsWithInvalidCoords().MakeEmpty();
1805	}
1806}
1807
1808// SetViewColor
1809void
1810Layer::SetViewColor(const RGBColor& color)
1811{
1812	fViewColor = color;
1813}
1814
1815// SetBackgroundBitmap
1816void
1817Layer::SetBackgroundBitmap(const ServerBitmap* bitmap)
1818{
1819	// TODO: What about reference counting?
1820	// "Release" old fBackgroundBitmap and "Aquire" new one?
1821	fBackgroundBitmap = bitmap;
1822}
1823
1824// SetOverlayBitmap
1825void
1826Layer::SetOverlayBitmap(const ServerBitmap* bitmap)
1827{
1828	// TODO: What about reference counting?
1829	// "Release" old fOverlayBitmap and "Aquire" new one?
1830	fOverlayBitmap = bitmap;
1831}
1832
1833void
1834Layer::CopyBits(BRect& src, BRect& dst, int32 xOffset, int32 yOffset) {
1835
1836	GetRootLayer()->Lock();
1837	do_CopyBits(src, dst, xOffset, yOffset);
1838	GetRootLayer()->Unlock();
1839}
1840
1841void
1842Layer::do_CopyBits(BRect& src, BRect& dst, int32 xOffset, int32 yOffset) {
1843	// NOTE: The correct behaviour is this:
1844	// * The region that is copied is the
1845	//   src rectangle, no matter if it fits
1846	//   into the dst rectangle. It is copied
1847	//   by the offset dst.LeftTop() - src.LeftTop()
1848	// * The dst rectangle is used for invalidation:
1849	//   Any area in the dst rectangle that could
1850	//   not be copied from src (because either the
1851	//   src rectangle was not big enough, or because there
1852	//   were parts cut off by the current layer clipping),
1853	//   are triggering BView::Draw() to be called
1854	//   and for these parts only.
1855
1856	// TODO: having moved this into Layer broke
1857	// offscreen windows (bitmaps)
1858	// -> move back into ServerWindow...
1859	if (!GetRootLayer())
1860		return;
1861
1862#ifndef NEW_CLIPPING
1863
1864	// the region that is going to be copied
1865	BRegion copyRegion(src);
1866	// apply the current clipping of the layer
1867	copyRegion.IntersectWith(&fVisible);
1868	// don't scroll regions that are pending for
1869	// (or already in) an update
1870	// NOTE: this fixes the visible glitches that resulted from
1871	// scrolling, but it brings up a new problem, which looks
1872	// like the the client receives multiple update messages for
1873	// the same area
1874	// NOTE2: if you don't include both regions, it doesn't work
1875	copyRegion.Exclude(&fOwner->CulmulatedUpdateRegion());
1876	copyRegion.Exclude(&fOwner->RegionToBeUpdated());
1877
1878	// offset the region to the destination
1879	// and apply the current clipping there as well
1880	copyRegion.OffsetBy(xOffset, yOffset);
1881	copyRegion.IntersectWith(&fVisible);
1882
1883	// the region at the destination that needs invalidation
1884	GetRootLayer()->fRedrawReg.Set(dst);
1885	// exclude the region drawn by the copy operation
1886	GetRootLayer()->fRedrawReg.Exclude(&copyRegion);
1887	// apply the current clipping as well
1888	GetRootLayer()->fRedrawReg.IntersectWith(&fVisible);
1889
1890	// move the region back for the actual operation
1891	copyRegion.OffsetBy(-xOffset, -yOffset);
1892
1893	GetDisplayDriver()->CopyRegion(&copyRegion, xOffset, yOffset);
1894
1895	// trigger the redraw
1896	GetRootLayer()->RequestDraw(GetRootLayer()->fRedrawReg, NULL);
1897#else
1898	// the region that is going to be copied
1899	BRegion copyRegion(src);
1900	// apply the current clipping of the layer
1901
1902	copyRegion.IntersectWith(&fVisible2);
1903
1904	// offset the region to the destination
1905	// and apply the current clipping there as well
1906	copyRegion.OffsetBy(xOffset, yOffset);
1907	copyRegion.IntersectWith(&fVisible2);
1908
1909	// the region at the destination that needs invalidation
1910	GetRootLayer()->fRedrawReg.Set(dst);
1911	// exclude the region drawn by the copy operation
1912	GetRootLayer()->fRedrawReg.Exclude(&copyRegion);
1913	// apply the current clipping as well
1914	GetRootLayer()->fRedrawReg.IntersectWith(&fVisible2);
1915
1916	// move the region back for the actual operation
1917	copyRegion.OffsetBy(-xOffset, -yOffset);
1918
1919	GetDisplayDriver()->CopyRegion(&copyRegion, xOffset, yOffset);
1920
1921	// trigger the redraw
1922	GetRootLayer()->RequestDraw(GetRootLayer()->fRedrawReg, NULL);
1923#endif
1924
1925}
1926
1927#ifdef NEW_CLIPPING
1928
1929void
1930Layer::MovedByHook(float dx, float dy)
1931{
1932	if (Window() && Flags() & B_FRAME_EVENTS && !IsTopLayer()) {
1933		BMessage msg(B_VIEW_MOVED);
1934		msg.AddInt64("when", system_time());
1935		msg.AddPoint("where", Frame().LeftTop());
1936		Window()->SendMessageToClient(&msg, fViewToken, false);
1937	}
1938}
1939
1940void
1941Layer::ResizedByHook(float dx, float dy, bool automatic)
1942{
1943	if (Window() && Flags() & B_FRAME_EVENTS && !IsTopLayer()) {
1944		BMessage msg(B_VIEW_RESIZED);
1945		msg.AddInt64("when", system_time());
1946		msg.AddFloat("width", Frame().Width());
1947		msg.AddFloat("height", Frame().Height());
1948		msg.AddPoint("where", Frame().LeftTop());
1949		Window()->SendMessageToClient(&msg, fViewToken, false);
1950	}
1951}
1952
1953void
1954Layer::ScrolledByHook(float dx, float dy)
1955{
1956	// empty.
1957}
1958
1959
1960void
1961Layer::GetWantedRegion(BRegion& reg) const
1962{
1963	// this is the same as get_user_region.
1964	// because get_user_region modifies nothing.
1965	const_cast<Layer*>(this)->Layer::_GetWantedRegion(reg);
1966}
1967
1968//! converts a point from local to parent's coordinate system
1969void
1970Layer::ConvertToParent2(BPoint* pt) const
1971{
1972	if (fParent) {
1973		BPoint origin = BoundsOrigin();
1974		pt->x -= origin.x;
1975		pt->y -= origin.y;
1976		pt->x += fFrame.left;
1977		pt->y += fFrame.top;
1978	}
1979}
1980
1981//! converts a rect from local to parent's coordinate system
1982void
1983Layer::ConvertToParent2(BRect* rect) const
1984{
1985	if (fParent) {
1986		BPoint origin = BoundsOrigin();
1987		rect->OffsetBy(-origin.x, -origin.y);
1988		rect->OffsetBy(fFrame.left, fFrame.top);
1989	}
1990}
1991
1992//! converts a region from local to parent's coordinate system
1993void
1994Layer::ConvertToParent2(BRegion* reg) const
1995{
1996	if (fParent) {
1997		BPoint origin = BoundsOrigin();
1998		reg->OffsetBy(-origin.x, -origin.y);
1999		reg->OffsetBy(fFrame.left, fFrame.top);
2000	}
2001}
2002
2003//! converts a point from parent's to local coordinate system
2004void
2005Layer::ConvertFromParent2(BPoint* pt) const
2006{
2007	if (fParent) {
2008		BPoint origin = BoundsOrigin();
2009		pt->x += origin.x;
2010		pt->y += origin.y;
2011		pt->x -= fFrame.left;
2012		pt->y -= fFrame.top;
2013	}
2014}
2015
2016//! converts a rect from parent's to local coordinate system
2017void
2018Layer::ConvertFromParent2(BRect* rect) const
2019{
2020	if (fParent) {
2021		BPoint origin = BoundsOrigin();
2022		rect->OffsetBy(origin.x, origin.y);
2023		rect->OffsetBy(-fFrame.left, -fFrame.top);
2024	}
2025}
2026
2027//! converts a region from parent's to local coordinate system
2028void
2029Layer::ConvertFromParent2(BRegion* reg) const
2030{
2031	if (fParent) {
2032		BPoint origin = BoundsOrigin();
2033		reg->OffsetBy(origin.x, origin.y);
2034		reg->OffsetBy(-fFrame.left, -fFrame.top);
2035	}
2036}
2037
2038//! converts a point from local to screen coordinate system
2039void
2040Layer::ConvertToScreen2(BPoint* pt) const
2041{
2042	if (GetRootLayer())
2043		if (fParent) {
2044			BPoint origin = BoundsOrigin();
2045			pt->x -= origin.x;
2046			pt->y -= origin.y;
2047			pt->x += fFrame.left;
2048			pt->y += fFrame.top;
2049
2050			fParent->ConvertToScreen2(pt);
2051		}
2052}
2053
2054//! converts a rect from local to screen coordinate system
2055void
2056Layer::ConvertToScreen2(BRect* rect) const
2057{
2058	if (GetRootLayer())
2059		if (fParent) {
2060			BPoint origin = BoundsOrigin();
2061			rect->OffsetBy(-origin.x, -origin.y);
2062			rect->OffsetBy(fFrame.left, fFrame.top);
2063
2064			fParent->ConvertToScreen2(rect);
2065		}
2066}
2067
2068//! converts a region from local to screen coordinate system
2069void
2070Layer::ConvertToScreen2(BRegion* reg) const
2071{
2072	if (GetRootLayer())
2073		if (fParent) {
2074			BPoint origin = BoundsOrigin();
2075			reg->OffsetBy(-origin.x, -origin.y);
2076			reg->OffsetBy(fFrame.left, fFrame.top);
2077
2078			fParent->ConvertToScreen2(reg);
2079		}
2080}
2081
2082//! converts a point from screen to local coordinate system
2083void
2084Layer::ConvertFromScreen2(BPoint* pt) const
2085{
2086	if (GetRootLayer())
2087		if (fParent) {
2088			BPoint origin = BoundsOrigin();
2089			pt->x += origin.x;
2090			pt->y += origin.y;
2091			pt->x -= fFrame.left;
2092			pt->y -= fFrame.top;
2093
2094			fParent->ConvertToScreen2(pt);
2095		}
2096}
2097
2098//! converts a rect from screen to local coordinate system
2099void
2100Layer::ConvertFromScreen2(BRect* rect) const
2101{
2102	if (GetRootLayer())
2103		if (fParent) {
2104			BPoint origin = BoundsOrigin();
2105			rect->OffsetBy(origin.x, origin.y);
2106			rect->OffsetBy(-fFrame.left, -fFrame.top);
2107
2108			fParent->ConvertFromScreen2(rect);
2109		}
2110}
2111
2112//! converts a region from screen to local coordinate system
2113void
2114Layer::ConvertFromScreen2(BRegion* reg) const
2115{
2116	if (GetRootLayer())
2117		if (fParent) {
2118			BPoint origin = BoundsOrigin();
2119			reg->OffsetBy(origin.x, origin.y);
2120			reg->OffsetBy(-fFrame.left, -fFrame.top);
2121
2122			fParent->ConvertFromScreen2(reg);
2123		}
2124}
2125
2126
2127void
2128Layer::do_Hide()
2129{
2130	fHidden = true;
2131
2132	if (fParent && !fParent->IsHidden() && GetRootLayer()) {
2133		// save fullVisible so we know what to invalidate
2134		BRegion invalid(fFullVisible2);
2135
2136		clear_visible_regions();
2137
2138		if (invalid.Frame().IsValid())
2139			fParent->do_Invalidate(invalid, this);
2140	}
2141}
2142
2143void
2144Layer::do_Show()
2145{
2146	fHidden = false;
2147
2148	if (fParent && !fParent->IsHidden() && GetRootLayer()) {
2149		BRegion invalid;
2150
2151		_GetWantedRegion(invalid);
2152
2153		if (invalid.CountRects() > 0)
2154			fParent->do_Invalidate(invalid, this);
2155	}
2156}
2157
2158void
2159Layer::do_Invalidate(const BRegion &invalid, const Layer *startFrom)
2160{
2161	BRegion		localVisible(fFullVisible2);
2162	localVisible.IntersectWith(&invalid);
2163	rebuild_visible_regions(invalid, localVisible,
2164		startFrom? startFrom: LastChild());
2165
2166	// add localVisible to our RootLayer's redraw region.
2167//	GetRootLayer()->fRedrawReg.Include(&localVisible);
2168	GetRootLayer()->fRedrawReg = localVisible;
2169	GetRootLayer()->RequestDraw(GetRootLayer()->fRedrawReg, NULL);
2170//	GetRootLayer()->RequestRedraw(); // TODO: what if we pass (fParent, startFromTHIS, &redrawReg)?
2171}
2172
2173void
2174Layer::do_Redraw(const BRegion &invalid, const Layer *startFrom)
2175{
2176	BRegion		localVisible(fFullVisible2);
2177	localVisible.IntersectWith(&invalid);
2178
2179	// add localVisible to our RootLayer's redraw region.
2180//	GetRootLayer()->fRedrawReg.Include(&localVisible);
2181	GetRootLayer()->fRedrawReg = localVisible;
2182	GetRootLayer()->RequestDraw(GetRootLayer()->fRedrawReg, NULL);
2183//	GetRootLayer()->RequestRedraw(); // TODO: what if we pass (fParent, startFromTHIS, &redrawReg)?
2184}
2185
2186inline void
2187Layer::resize_layer_frame_by(float x, float y)
2188{
2189	uint16		rm = fResizeMode & 0x0000FFFF;
2190	BRect		newFrame = fFrame;
2191
2192	if ((rm & 0x0F00U) == _VIEW_LEFT_ << 8)
2193		newFrame.left += 0.0f;
2194	else if ((rm & 0x0F00U) == _VIEW_RIGHT_ << 8)
2195		newFrame.left += x;
2196	else if ((rm & 0x0F00U) == _VIEW_CENTER_ << 8)
2197		newFrame.left += x/2;
2198
2199	if ((rm & 0x000FU) == _VIEW_LEFT_)
2200		newFrame.right += 0.0f;
2201	else if ((rm & 0x000FU) == _VIEW_RIGHT_)
2202		newFrame.right += x;
2203	else if ((rm & 0x000FU) == _VIEW_CENTER_)
2204		newFrame.right += x/2;
2205
2206	if ((rm & 0xF000U) == _VIEW_TOP_ << 12)
2207		newFrame.top += 0.0f;
2208	else if ((rm & 0xF000U) == _VIEW_BOTTOM_ << 12)
2209		newFrame.top += y;
2210	else if ((rm & 0xF000U) == _VIEW_CENTER_ << 12)
2211		newFrame.top += y/2;
2212
2213	if ((rm & 0x00F0U) == _VIEW_TOP_ << 4)
2214		newFrame.bottom += 0.0f;
2215	else if ((rm & 0x00F0U) == _VIEW_BOTTOM_ << 4)
2216		newFrame.bottom += y;
2217	else if ((rm & 0x00F0U) == _VIEW_CENTER_ << 4)
2218		newFrame.bottom += y/2;
2219
2220	if (newFrame != fFrame) {
2221		float		dx, dy;
2222
2223		dx	= newFrame.Width() - fFrame.Width();
2224		dy	= newFrame.Height() - fFrame.Height();
2225
2226		fFrame	= newFrame;
2227
2228		if (dx != 0.0f || dy != 0.0f) {
2229			// call hook function
2230			ResizedByHook(dx, dy, true); // automatic
2231
2232			for (Layer *lay = LastChild(); lay; lay = PreviousChild())
2233				lay->resize_layer_frame_by(dx, dy);
2234		}
2235		else
2236			MovedByHook(dx, dy);
2237	}
2238}
2239
2240inline void
2241Layer::rezize_layer_redraw_more(BRegion &reg, float dx, float dy)
2242{
2243	if (dx == 0 && dy == 0)
2244		return;
2245
2246	for (Layer *lay = LastChild(); lay; lay = PreviousChild()) {
2247		uint16		rm = lay->fResizeMode & 0x0000FFFF;
2248
2249		if ((rm & 0x0F0F) == (uint16)B_FOLLOW_LEFT_RIGHT || (rm & 0xF0F0) == (uint16)B_FOLLOW_TOP_BOTTOM) {
2250			// NOTE: this is not exactly corect, but it works :-)
2251			// Normaly we shoud've used the lay's old, required region - the one returned
2252			// from get_user_region() with the old frame, and the current one. lay->Bounds()
2253			// works for the moment so we leave it like this.
2254
2255			// calculate the old bounds.
2256			BRect	oldBounds(lay->Bounds());
2257			if ((rm & 0x0F0F) == (uint16)B_FOLLOW_LEFT_RIGHT)
2258				oldBounds.right -=dx;
2259			if ((rm & 0xF0F0) == (uint16)B_FOLLOW_TOP_BOTTOM)
2260				oldBounds.bottom -=dy;
2261
2262			// compute the region that became visible because we got bigger OR smaller.
2263			BRegion	regZ(lay->Bounds());
2264			regZ.Include(oldBounds);
2265			regZ.Exclude(oldBounds&lay->Bounds());
2266
2267			lay->ConvertToScreen2(&regZ);
2268
2269			// intersect that with this'(not lay's) fullVisible region
2270			regZ.IntersectWith(&fFullVisible2);
2271			reg.Include(&regZ);
2272
2273			lay->rezize_layer_redraw_more(reg,
2274				(rm & 0x0F0F) == (uint16)B_FOLLOW_LEFT_RIGHT? dx: 0,
2275				(rm & 0xF0F0) == (uint16)B_FOLLOW_TOP_BOTTOM? dy: 0);
2276
2277			// above, OR this:
2278			// reg.Include(&lay->fFullVisible2);
2279		}
2280		else
2281		if (((rm & 0x0F0F) == (uint16)B_FOLLOW_RIGHT && dx != 0) ||
2282			((rm & 0x0F0F) == (uint16)B_FOLLOW_H_CENTER && dx != 0) ||
2283			((rm & 0xF0F0) == (uint16)B_FOLLOW_BOTTOM && dy != 0)||
2284			((rm & 0xF0F0) == (uint16)B_FOLLOW_V_CENTER && dy != 0))
2285		{
2286			reg.Include(&lay->fFullVisible2);
2287		}
2288	}
2289}
2290
2291inline void
2292Layer::resize_layer_full_update_on_resize(BRegion &reg, float dx, float dy)
2293{
2294	if (dx == 0 && dy == 0)
2295		return;
2296
2297	for (Layer *lay = LastChild(); lay; lay = PreviousChild()) {
2298		uint16		rm = lay->fResizeMode & 0x0000FFFF;
2299
2300		if ((rm & 0x0F0F) == (uint16)B_FOLLOW_LEFT_RIGHT || (rm & 0xF0F0) == (uint16)B_FOLLOW_TOP_BOTTOM) {
2301			if (lay->fFlags & B_FULL_UPDATE_ON_RESIZE && lay->fVisible2.CountRects() > 0)
2302				reg.Include(&lay->fVisible2);
2303
2304			lay->resize_layer_full_update_on_resize(reg,
2305				(rm & 0x0F0F) == (uint16)B_FOLLOW_LEFT_RIGHT? dx: 0,
2306				(rm & 0xF0F0) == (uint16)B_FOLLOW_TOP_BOTTOM? dy: 0);
2307		}
2308	}
2309}
2310
2311void
2312Layer::do_ResizeBy(float dx, float dy)
2313{
2314	fFrame.Set(fFrame.left, fFrame.top, fFrame.right+dx, fFrame.bottom+dy);
2315
2316	// resize children using their resize_mask.
2317	for (Layer *lay = LastChild(); lay; lay = PreviousChild())
2318			lay->resize_layer_frame_by(dx, dy);
2319
2320	// call hook function
2321	if (dx != 0.0f || dy != 0.0f)
2322		ResizedByHook(dx, dy, false); // manual
2323
2324	if (!IsHidden() && GetRootLayer()) {
2325		BRegion oldFullVisible(fFullVisible2);
2326		// this is required to invalidate the old border
2327		BRegion oldVisible(fVisible2);
2328
2329		// in case they moved, bottom, right and center aligned layers must be redrawn
2330		BRegion redrawMore;
2331		rezize_layer_redraw_more(redrawMore, dx, dy);
2332
2333		// we'll invalidate the old area and the new, maxmial one.
2334		BRegion invalid;
2335		_GetWantedRegion(invalid);
2336		invalid.Include(&fFullVisible2);
2337
2338		clear_visible_regions();
2339
2340		fParent->do_RebuildVisibleRegions(invalid, this);
2341
2342		// done rebuilding regions, now redraw regions that became visible
2343
2344		// what's invalid, are the differences between to old and the new fullVisible region
2345		// 1) in case we grow.
2346		BRegion		redrawReg(fFullVisible2);
2347		redrawReg.Exclude(&oldFullVisible);
2348		// 2) in case we shrink
2349		BRegion		redrawReg2(oldFullVisible);
2350		redrawReg2.Exclude(&fFullVisible2);
2351		// 3) combine.
2352		redrawReg.Include(&redrawReg2);
2353
2354		// for center, right and bottom alligned layers, redraw their old positions
2355		redrawReg.Include(&redrawMore);
2356
2357		// layers that had their frame modified must be entirely redrawn.
2358		rezize_layer_redraw_more(redrawReg, dx, dy);
2359
2360		// add redrawReg to our RootLayer's redraw region.
2361//		GetRootLayer()->fRedrawReg.Include(&redrawReg);
2362		GetRootLayer()->fRedrawReg = redrawReg;
2363		// include layer's visible region in case we want a full update on resize
2364		if (fFlags & B_FULL_UPDATE_ON_RESIZE && fVisible2.Frame().IsValid()) {
2365			resize_layer_full_update_on_resize(GetRootLayer()->fRedrawReg, dx, dy);
2366
2367			GetRootLayer()->fRedrawReg.Include(&fVisible2);
2368			GetRootLayer()->fRedrawReg.Include(&oldVisible);
2369		}
2370		// clear canvas and set invalid regions for affected WinBorders
2371		GetRootLayer()->RequestDraw(GetRootLayer()->fRedrawReg, NULL);
2372//	GetRootLayer()->RequestRedraw(); // TODO: what if we pass (fParent, startFromTHIS, &redrawReg)?
2373	}
2374}
2375
2376void Layer::do_MoveBy(float dx, float dy)
2377{
2378	if (dx == 0.0f && dy == 0.0f)
2379		return;
2380
2381//	fFrame.Set(fFrame.left+dx, fFrame.top+dy, fFrame.right+dx, fFrame.bottom+dy);
2382	fFrame.OffsetBy(dx, dy);
2383
2384	// call hook function
2385	MovedByHook(dx, dy);
2386
2387	if (!IsHidden() && GetRootLayer()) {
2388		BRegion oldFullVisible(fFullVisible2);
2389
2390		// we'll invalidate the old position and the new, maxmial one.
2391		BRegion invalid;
2392		_GetWantedRegion(invalid);
2393		invalid.Include(&fFullVisible2);
2394
2395		clear_visible_regions();
2396
2397		fParent->do_RebuildVisibleRegions(invalid, this);
2398
2399		// done rebuilding regions, now copy common parts and redraw regions that became visible
2400
2401		// include the actual and the old fullVisible regions. later, we'll exclude the common parts.
2402		BRegion		redrawReg(fFullVisible2);
2403		redrawReg.Include(&oldFullVisible);
2404
2405		// offset to layer's new location so that we can calculate the common region.
2406		oldFullVisible.OffsetBy(dx, dy);
2407
2408		// finally we have the region that needs to be redrawn.
2409		redrawReg.Exclude(&oldFullVisible);
2410
2411		// by intersecting the old fullVisible offseted to layer's new location, with the current
2412		// fullVisible, we'll have the common region which can be copied using HW acceleration.
2413		oldFullVisible.IntersectWith(&fFullVisible2);
2414
2415		// offset back and instruct the HW to do the actual copying.
2416		oldFullVisible.OffsetBy(-dx, -dy);
2417		GetDisplayDriver()->CopyRegion(&oldFullVisible, dx, dy);
2418
2419		// add redrawReg to our RootLayer's redraw region.
2420//		GetRootLayer()->fRedrawReg.Include(&redrawReg);
2421		GetRootLayer()->fRedrawReg = redrawReg;
2422		GetRootLayer()->RequestDraw(GetRootLayer()->fRedrawReg, NULL);
2423//	GetRootLayer()->RequestRedraw(); // TODO: what if we pass (fParent, startFromTHIS, &redrawReg)?
2424	}
2425}
2426
2427void
2428Layer::do_ScrollBy(float dx, float dy)
2429{
2430	fLayerData->OffsetOrigin(BPoint(dx, dy));
2431//	fOrigin.Set(fOrigin.x + dx, fOrigin.y + dy);
2432
2433	if (!IsHidden() && GetRootLayer()) {
2434		// set the region to be invalidated.
2435		BRegion		invalid(fFullVisible2);
2436
2437		clear_visible_regions();
2438
2439		rebuild_visible_regions(invalid, invalid, LastChild());
2440
2441		// for the moment we say that the whole surface needs to be redraw.
2442		BRegion		redrawReg(fFullVisible2);
2443
2444		// offset old region so that we can start comparing.
2445		invalid.OffsetBy(dx, dy);
2446
2447		// compute the common region. we'll use HW acc to copy this to the new location.
2448		invalid.IntersectWith(&fFullVisible2);
2449		GetDisplayDriver()->CopyRegion(&invalid, -dx, -dy);
2450
2451		// common region goes back to its original location. then, by excluding
2452		// it from curent fullVisible we'll obtain the region that needs to be redrawn.
2453		invalid.OffsetBy(-dx, -dy);
2454		redrawReg.Exclude(&invalid);
2455
2456//		GetRootLayer()->fRedrawReg.Include(&redrawReg);
2457		GetRootLayer()->fRedrawReg = redrawReg;
2458		GetRootLayer()->RequestDraw(GetRootLayer()->fRedrawReg, NULL);
2459//	GetRootLayer()->RequestRedraw(); // TODO: what if we pass (fParent, startFromTHIS, &redrawReg)?
2460	}
2461
2462	if (dx != 0.0f || dy != 0.0f)
2463		ScrolledByHook(dx, dy);
2464}
2465
2466void
2467Layer::_GetWantedRegion(BRegion &reg)
2468{
2469	// 1) set to frame in screen coords
2470	BRect screenFrame(Bounds());
2471	ConvertToScreen2(&screenFrame);
2472	reg.Set(screenFrame);
2473
2474	// 2) intersect with screen region
2475	BRegion screenReg(GetRootLayer()->Bounds());
2476	reg.IntersectWith(&screenReg);
2477
2478
2479	// 3) impose user constrained regions
2480	LayerData *stackData = fLayerData;
2481	while (stackData) {
2482		if (stackData->ClippingRegion()) {
2483			// transform in screen coords
2484// NOTE: Already is in screen coords, but I leave this here in
2485// case we change it
2486//			BRegion screenReg(*stackData->ClippingRegion());
2487//			ConvertToScreen2(&screenReg);
2488//			reg.IntersectWith(&screenReg);
2489			reg.IntersectWith(stackData->ClippingRegion());
2490		}
2491		stackData = stackData->prevState;
2492	}
2493}
2494
2495void
2496Layer::do_RebuildVisibleRegions(const BRegion &invalid, const Layer *startFrom)
2497{
2498	BRegion		localVisible(fFullVisible2);
2499	localVisible.IntersectWith(&invalid);
2500	rebuild_visible_regions(invalid, localVisible, startFrom);
2501}
2502
2503void
2504Layer::rebuild_visible_regions(const BRegion &invalid,
2505								const BRegion &parentLocalVisible,
2506								const Layer *startFrom)
2507{
2508	// no point in continuing if this layer is hidden.
2509	if (fHidden)
2510		return;
2511
2512	// no need to go deeper if the parent doesn't have a visible region anymore
2513	// and our fullVisible region is also empty.
2514	if (!parentLocalVisible.Frame().IsValid() && !(fFullVisible2.CountRects() > 0))
2515		return;
2516
2517	bool fullRebuild = false;
2518
2519	// intersect maximum wanted region with the invalid region
2520	BRegion common;
2521	_GetWantedRegion(common);
2522	common.IntersectWith(&invalid);
2523
2524	// if the resulted region is not valid, this layer is not in the catchment area
2525	// of the region being invalidated
2526	if (!common.CountRects() > 0)
2527		return;
2528
2529	// now intersect with parent's visible part of the region that was/is invalidated
2530	common.IntersectWith(&parentLocalVisible);
2531
2532	// exclude the invalid region
2533	fFullVisible2.Exclude(&invalid);
2534	fVisible2.Exclude(&invalid);
2535
2536	// put in what's really visible
2537	fFullVisible2.Include(&common);
2538
2539	// allow this layer to hide some parts from its children
2540	_ReserveRegions(common);
2541
2542	for (Layer *lay = LastChild(); lay; lay = PreviousChild()) {
2543		if (lay == startFrom)
2544			fullRebuild = true;
2545
2546		if (fullRebuild)
2547			lay->rebuild_visible_regions(invalid, common, lay->LastChild());
2548
2549		// to let children know much they can take from parent's visible region
2550		common.Exclude(&lay->fFullVisible2);
2551	}
2552
2553	// include what's left after all children took what they could.
2554	fVisible2.Include(&common);
2555}
2556
2557void
2558Layer::_ReserveRegions(BRegion &reg)
2559{
2560	// Empty for Layer objects
2561}
2562
2563void
2564Layer::clear_visible_regions()
2565{
2566	// OPT: maybe we should uncomment these lines for performance
2567	//if (fFullVisible2.CountRects() <= 0)
2568	//	return;
2569
2570	fVisible2.MakeEmpty();
2571	fFullVisible2.MakeEmpty();
2572	for (Layer *child = LastChild(); child; child = PreviousChild())
2573		child->clear_visible_regions();
2574}
2575
2576#endif
2577
2578