1 #define PERF //GG_270298
5 #define PRO19042 //GG_261199 Avoid to have black buffer because the ClearBuffer() method
6 // clear also the current buffer attributes !
7 // Under WNT the buffer contains also the attributes.
12 #include <Graphic2d_Buffer.ixx>
14 #include <Aspect_WindowDriver.hxx>
15 #include <Aspect_WidthMap.hxx>
16 #include <Aspect_WidthMapEntry.hxx>
18 #include <Graphic2d_Line.hxx>
19 #include <Graphic2d_Text.hxx>
20 #include <Graphic2d_Drawer.hxx>
22 Graphic2d_Buffer::Graphic2d_Buffer (const Handle(Graphic2d_View)& aView, const Quantity_Length aPivotX, const Quantity_Length aPivotY, const Standard_Integer aWidthIndex, const Standard_Integer aColorIndex, const Standard_Integer aFontIndex, const Aspect_TypeOfDrawMode aDrawMode):
24 myBufferIsPosted (Standard_False),
25 myPivotX (Standard_ShortReal (aPivotX)),
26 myPivotY (Standard_ShortReal (aPivotY)),
27 myWidthIndex (aWidthIndex),
28 myColorIndex (aColorIndex),
29 myFontIndex (aFontIndex),
30 myDrawMode(aDrawMode),
31 myPView(aView.operator->()) {
34 void Graphic2d_Buffer::Destroy () {
36 if( !myDriver.IsNull() ) {
37 myDriver->CloseBuffer(myBufferId) ;
41 void Graphic2d_Buffer::Add (const Handle(Graphic2d_GraphicObject)& anObject) {
44 myObjects.Append(anObject) ;
45 for( i=1 ; i<=anObject->Length() ; i++ ) {
46 myPrimitives.Append(anObject->Primitive(i)) ;
48 if( myBufferIsPosted ) {
49 this->ReLoad(Standard_False) ;
53 void Graphic2d_Buffer::Add (const Handle(Graphic2d_Primitive)& aPrimitive) {
55 myPrimitives.Append(aPrimitive) ;
56 if( myBufferIsPosted ) {
57 this->ReLoad(Standard_False) ;
61 void Graphic2d_Buffer::Remove (const Handle(Graphic2d_GraphicObject)& anObject) {
62 Standard_Integer i,j ;
64 for( i=1 ; i<=myObjects.Length() ; i++ ) {
65 if( anObject == myObjects.Value(i) ) break ;
68 if( i <= myObjects.Length() ) {
70 // not very clever, but who will use Remove (anObject) ?
71 for( j=1 ; j<=anObject->Length() ; j++ ) {
72 Remove(anObject->Primitive(j)) ;
74 if( myBufferIsPosted ) {
75 this->ReLoad(Standard_False) ;
80 void Graphic2d_Buffer::Remove (const Handle(Graphic2d_Primitive)& aPrimitive) {
83 for( i=1 ; i<=myPrimitives.Length() ; i++ ) {
84 if( aPrimitive == myPrimitives.Value(i) ) break ;
87 if( i <= myPrimitives.Length() ) {
88 myPrimitives.Remove(i) ;
89 if( myBufferIsPosted ) {
90 this->ReLoad(Standard_False) ;
95 void Graphic2d_Buffer::Clear () {
97 printf(" Graphic2d_Buffer::Clear()%d,%d\n",myBufferId,myBufferIsPosted);
100 myPrimitives.Clear() ;
102 if( myBufferIsPosted ) {
103 myDriver->ClearBuffer(myBufferId);
107 void Graphic2d_Buffer::SetAttrib (const Standard_Integer aWidthIndex,
108 const Standard_Integer aColorIndex,
109 const Standard_Integer aFontIndex,
110 const Aspect_TypeOfDrawMode aDrawMode) {
112 if( myColorIndex != aColorIndex || myFontIndex != aFontIndex ||
113 myWidthIndex != aWidthIndex || myDrawMode != aDrawMode ) {
115 myWidthIndex = aWidthIndex ;
116 myColorIndex = aColorIndex ;
117 myFontIndex = aFontIndex ;
118 myDrawMode = aDrawMode ;
120 if( myBufferIsPosted ) {
121 this->ReLoad(Standard_False) ;
126 void Graphic2d_Buffer::SetPivot () {
128 if( !myDriver.IsNull() ) {
129 Handle(Graphic2d_Drawer) theDrawer = myPView->Drawer() ;
130 Standard_ShortReal xpivot,ypivot;
132 myDriver->PositionOfBuffer(myBufferId,xpivot,ypivot) ;
133 theDrawer->UnMapFromTo(xpivot,ypivot,myPivotX,myPivotY) ;
137 void Graphic2d_Buffer::SetPivot ( const Quantity_Length aPivotX, const Quantity_Length aPivotY) {
139 myPivotX = Standard_ShortReal( aPivotX );
140 myPivotY = Standard_ShortReal( aPivotY );
142 if( myBufferIsPosted ) {
143 this->ReLoad(Standard_False) ;
147 void Graphic2d_Buffer::Move (const Quantity_Length aDeltaX, const Quantity_Length aDeltaY) {
149 if( myBufferIsPosted ) {
150 Handle(Graphic2d_Drawer) theDrawer = myPView->Drawer();
151 Standard_ShortReal xpivot, ypivot;
152 theDrawer->GetMapFromTo(Standard_ShortReal(aDeltaX),
153 Standard_ShortReal(aDeltaY),xpivot,ypivot);
154 myDriver->MoveBuffer(myBufferId, xpivot, ypivot);
158 void Graphic2d_Buffer::Rotate (const Quantity_PlaneAngle anAngle) {
160 if( myBufferIsPosted ) {
161 myDriver->RotateBuffer(myBufferId,anAngle) ;
165 void Graphic2d_Buffer::Scale (const Quantity_Factor aFactor) {
167 if( myBufferIsPosted ) {
168 myDriver->ScaleBuffer(myBufferId,aFactor,aFactor) ;
172 Standard_Boolean Graphic2d_Buffer::IsEmpty () const {
174 if( !myDriver.IsNull() ) {
175 return myDriver->BufferIsEmpty(myBufferId) ;
177 return Standard_True ;
181 Standard_Boolean Graphic2d_Buffer::IsIn (const Handle(Graphic2d_GraphicObject)& anObject) const {
182 Standard_Integer i,length = myObjects.Length() ;
184 for( i=1 ; i<=length ; i++ ) {
185 if( anObject == myObjects.Value(i) ) break ;
188 if( i <= length ) return Standard_True ;
189 else return Standard_False;
192 Standard_Boolean Graphic2d_Buffer::IsIn (const Handle(Graphic2d_Primitive)& aPrimitive) const {
193 Standard_Integer i,length = myPrimitives.Length() ;
195 for( i=1 ; i<=length ; i++ ) {
196 if( aPrimitive == myPrimitives.Value(i) ) break ;
199 if( i <= length ) return Standard_True ;
200 else return Standard_False;
203 void Graphic2d_Buffer::Post () {
204 Handle(Graphic2d_Drawer) theDrawer = myPView->Drawer() ;
205 Standard_Boolean reset = Standard_True;
207 printf(" Graphic2d_Buffer::Post()%d\n",myBufferIsPosted);
210 if( theDrawer->IsWindowDriver() ) {
211 if( myBufferIsPosted ) {
212 reset = Standard_False;
215 myDriver = theDrawer->WindowDriver() ;
216 myBufferIsPosted = Standard_True ;
217 this->ReLoad(reset) ;
222 void Graphic2d_Buffer::Post ( const Handle(Aspect_WindowDriver)& aDriver,
223 const Handle(Graphic2d_ViewMapping)& aViewMapping,
224 const Standard_Real aXPosition,
225 const Standard_Real aYPosition,
226 const Standard_Real aScale) {
228 Handle(Graphic2d_Drawer) theDrawer = myPView->Drawer() ;
229 Standard_Boolean reset = Standard_True;
231 printf(" Graphic2d_Buffer::Post(%lx,%lx,%f,%f,%f)%d\n",
232 aDriver,aViewMapping,aXPosition,aYPosition,aScale,myBufferIsPosted);
235 Standard_Real XCenter, YCenter, Size;
237 if( myBufferIsPosted && (aDriver == myDriver) ) {
238 reset = Standard_False;
242 aViewMapping->ViewMapping(XCenter, YCenter, Size);
243 theDrawer->SetDriver(aDriver);
244 theDrawer->SetValues (XCenter, YCenter, Size,
245 aXPosition, aYPosition, aScale, aViewMapping->Zoom());
247 myBufferIsPosted = Standard_True ;
248 this->ReLoad(reset) ;
252 void Graphic2d_Buffer::UnPost () {
254 printf(" Graphic2d_Buffer::UnPost()%d\n",myBufferIsPosted);
257 if( myBufferIsPosted ) {
259 myBufferIsPosted = Standard_False ;
260 myPView->Remove(this);
264 Standard_Boolean Graphic2d_Buffer::IsPosted () const {
266 return myBufferIsPosted ;
269 Standard_Boolean Graphic2d_Buffer::IsPosted ( const Handle(Aspect_WindowDriver)& aDriver) const {
271 return (myBufferIsPosted && (aDriver == myDriver)) ;
274 void Graphic2d_Buffer::Erase () {
276 printf(" Graphic2d_Buffer::Erase()%d\n",myBufferIsPosted);
279 if( myBufferIsPosted ) {
280 myDriver->EraseBuffer(myBufferId) ;
284 void Graphic2d_Buffer::Draw () {
286 printf(" Graphic2d_Buffer::Draw()%d\n",myBufferIsPosted);
289 if( myBufferIsPosted ) {
290 Handle(Graphic2d_Drawer) theDrawer = myPView->Drawer() ;
291 Standard_Integer i,length = myPrimitives.Length() ;
292 theDrawer->SetRejection(Standard_False);
293 myDriver->BeginDraw(Standard_False,myBufferId);
294 for( i=1 ; i<=length ; i++ ) {
295 myPrimitives.Value(i)->Draw(theDrawer);
297 myDriver->EndDraw() ;
298 theDrawer->SetRejection(Standard_True);
302 void Graphic2d_Buffer::ReLoad (const Standard_Boolean ResetPosition) {
304 Standard_Boolean theStatus = Standard_False;
305 Standard_Integer theColorIndex = myColorIndex;
306 Standard_Integer theWidthIndex = myWidthIndex;
307 Standard_Integer theFontIndex = myFontIndex;
310 printf(" Graphic2d_Buffer::ReLoad(%d)\n",ResetPosition);
312 if( !myDriver.IsNull() ) {
314 myBufferId = this->HashCode(IntegerLast());
316 // Maximum depth of primitive lines
317 // contained in the buffer is required
318 if (theWidthIndex < 0) {
319 Standard_Real theWidthMax;
320 // There are no primitive lines => thickness by default
321 if (! MaxWidth (theWidthMax, theWidthIndex))
325 // The font of the 1st primitive text from the buffer is required
326 if( theFontIndex < 0) {
328 Handle(Graphic2d_Primitive) agp;
329 for (i=1; i<=myPrimitives.Length(); i++) {
330 agp = myPrimitives.Value (i);
332 if (agp->Family() == Graphic2d_TOP_TEXT) {
334 if (agp->IsKind (STANDARD_TYPE (Graphic2d_Text))) {
337 (*(Handle(Graphic2d_Text ) *) &agp)->FontIndex ();
342 if( theFontIndex < 0 ) theFontIndex = 0;
345 // The color of the 1st primitive from the buffer is required
346 if( theColorIndex < 0) {
348 for (i=1; i<=myPrimitives.Length(); i++) {
349 theColorIndex = myPrimitives.Value (i)->ColorIndex ();
353 if( theColorIndex < 0 ) theColorIndex = 0;
356 Handle(Graphic2d_Drawer) theDrawer = myPView->Drawer() ;
357 Standard_ShortReal xpivot,ypivot;
359 theDrawer->GetMapFromTo(myPivotX,myPivotY,xpivot,ypivot) ;
360 theStatus = myDriver->OpenBuffer(myBufferId,xpivot,ypivot,
361 theWidthIndex,theColorIndex,theFontIndex,myDrawMode) ;
364 Standard_ShortReal xpos,ypos;
366 myDriver->PositionOfBuffer(myBufferId,xpos,ypos) ;
367 #ifndef PRO19042 // Under WNT the buffer is cleared at open time
368 myDriver->ClearBuffer(myBufferId) ;
371 if( ResetPosition ) {
372 myDriver->DrawBuffer(myBufferId);
374 myDriver->MoveBuffer(myBufferId,xpos,ypos) ;
380 myBufferIsPosted = Standard_False ;
385 Graphic2d_Buffer::MaxWidth (
386 Quantity_Length& theWidth, Standard_Integer& theIndex) const {
387 Standard_Boolean Result = Standard_False;
389 if( !myDriver.IsNull() ) {
390 Handle(Aspect_WidthMap) theWidthMap = myDriver->WidthMap ();
391 Standard_Integer i,size = theWidthMap->Size(),
392 length = myPrimitives.Length();
394 Quantity_Length WidthCur = 0.;
395 Standard_Integer IndexCur = 0;
400 Handle(Graphic2d_Primitive) agp;
401 for (i=1; i<=length; i++) {
402 agp = myPrimitives.Value (i);
404 if (agp->Family() == Graphic2d_TOP_LINE ) {
406 if (agp->IsKind (STANDARD_TYPE (Graphic2d_Line))) {
408 // There are lines in the Buffer
409 // and not by fracture on the line
410 Result = Standard_True;
412 (*(Handle(Graphic2d_Line) *) &agp)->WidthIndex ();
413 if ((IndexCur <= 0) || (IndexCur > size)) {
414 // Case when thicknesses are not precised, IndexCur == 0
415 // or there is a really huge problem !
418 // +1 because in the table there is 1 default entry
419 // which defines default thickness
420 WidthCur = (theWidthMap->Entry (IndexCur+1)).Width ();
421 if (theWidth < WidthCur) {
432 Handle(Graphic2d_View) Graphic2d_Buffer::View () const {
434 return ((Graphic2d_View*) myPView);
438 Quantity_PlaneAngle Graphic2d_Buffer::Angle () const {
439 Quantity_PlaneAngle angle = 0. ;
441 if( !myDriver.IsNull() ) {
442 myDriver->AngleOfBuffer(myBufferId,angle) ;
448 Quantity_Factor Graphic2d_Buffer::Scale () const {
449 Quantity_Factor xscale = 1. ;
450 Quantity_Factor yscale = 1. ;
452 if( !myDriver.IsNull() ) {
453 myDriver->ScaleOfBuffer(myBufferId,xscale,yscale) ;
456 return (xscale+yscale)/2. ;
459 Quantity_Length Graphic2d_Buffer::Xpivot () const {
460 Handle(Graphic2d_Drawer) theDrawer = myPView->Drawer() ;
461 Standard_ShortReal pivotx = myPivotX ;
462 Standard_ShortReal pivoty = myPivotY ;
464 if( !myDriver.IsNull() ) {
465 Standard_ShortReal xpivot,ypivot ;
466 myDriver->PositionOfBuffer(myBufferId,xpivot,ypivot) ;
467 theDrawer->UnMapFromTo(xpivot,ypivot,pivotx,pivoty) ;
470 return Quantity_Length(pivotx);
473 Quantity_Length Graphic2d_Buffer::Ypivot() const {
474 Handle(Graphic2d_Drawer) theDrawer = myPView->Drawer() ;
475 Standard_ShortReal pivotx = myPivotX ;
476 Standard_ShortReal pivoty = myPivotY ;
478 if( !myDriver.IsNull() ) {
479 Standard_ShortReal xpivot,ypivot ;
480 myDriver->PositionOfBuffer(myBufferId,xpivot,ypivot) ;
481 theDrawer->UnMapFromTo(xpivot,ypivot,pivotx,pivoty) ;
484 return Quantity_Length(pivoty);