0025266: Debug statements in the source are getting flushed on to the console
[occt.git] / src / AIS / AIS_InteractiveContext_1.cxx
1 // Created on: 1997-01-29
2 // Created by: Robert COUBLANC
3 // Copyright (c) 1997-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #define UKI60826        //GG_161199     Use the requested selection color instead of default
18
19 #define IMP280200       //GG Don't returns a NULL shape in SelectedShape()
20 //                      method when the current selected is a shape !
21
22 #define BUC60814        //GG_300101     Idem UKI60826
23
24 #define OCC138          //VTN Avoding infinit loop in AddOrRemoveCurrentObject method.
25
26 #define OCC9657
27
28 #include <AIS_InteractiveContext.jxx>
29 #include <SelectMgr_EntityOwner.hxx>
30 #include <AIS_Selection.hxx>
31 #include <AIS_StatusOfDetection.hxx>
32 #include <AIS_StatusOfPick.hxx>
33
34 #include <AIS_DataMapIteratorOfDataMapOfIOStatus.hxx>
35 #include <AIS_LocalContext.hxx>
36 #include <AIS_GlobalStatus.hxx>
37 #include <AIS_Shape.hxx>
38
39 #include <Aspect_Grid.hxx>
40
41 #include <V3d_Light.hxx>
42 #include <V3d_PositionalLight.hxx>
43 #include <V3d_SpotLight.hxx>
44 #include <V3d_DirectionalLight.hxx>
45 #include <V3d_AmbientLight.hxx>
46
47 #include <TColStd_ListIteratorOfListOfInteger.hxx>
48 #include <SelectMgr_Selection.hxx>
49 #include <SelectBasics_SensitiveEntity.hxx>
50
51 #include <Prs3d_Presentation.hxx>
52
53 #ifdef OCC9657
54 #include <AIS_MapOfInteractive.hxx>
55 #include <AIS_MapIteratorOfMapOfInteractive.hxx>
56 #endif
57
58 //=======================================================================
59 //function : MoveTo
60 //purpose  :
61 //=======================================================================
62 AIS_StatusOfDetection AIS_InteractiveContext::MoveTo (const Standard_Integer  theXPix,
63                                                       const Standard_Integer  theYPix,
64                                                       const Handle(V3d_View)& theView,
65                                                       const Standard_Boolean  theToRedrawOnUpdate)
66 {
67   if (HasOpenedContext())
68   {
69     myWasLastMain = Standard_True;
70     return myLocalContexts (myCurLocalIndex)->MoveTo (theXPix, theYPix, theView, theToRedrawOnUpdate);
71   }
72
73   myAISCurDetected = 0;
74   myAISDetectedSeq.Clear();
75
76   if (theView->Viewer() != myMainVwr)
77   {
78     return AIS_SOD_Error;
79   }
80
81   // preliminaires
82   myLastPicked  = myLastinMain;
83   myWasLastMain = Standard_True;
84   AIS_StatusOfDetection aStatus        = AIS_SOD_Nothing;
85   Standard_Boolean      toUpdateViewer = Standard_False;
86
87   // allonzy
88   myMainSel->Pick (theXPix, theYPix, theView);
89
90   // filling of myAISDetectedSeq sequence storing information about detected AIS objects
91   // (the objects must be AIS_Shapes)
92   const Standard_Integer aDetectedNb = myMainSel->NbPicked();
93   Standard_Integer aNewDetected = 0;
94   for (Standard_Integer aDetIter = 1; aDetIter <= aDetectedNb; ++aDetIter)
95   {
96     Handle(SelectMgr_EntityOwner) anOwner = myMainSel->Picked (aDetIter);
97     if (anOwner.IsNull()
98      || !myFilters->IsOk (anOwner))
99     {
100       continue;
101     }
102
103     if (aNewDetected < 1)
104     {
105       aNewDetected = aDetIter;
106     }
107     Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
108     if (!anObj.IsNull())
109     {
110       myAISDetectedSeq.Append (anObj);
111     }
112   }
113
114   if (aNewDetected >= 1)
115   {
116     // does nothing if previously detected object is equal to the current one
117     Handle(SelectMgr_EntityOwner) aNewPickedOwner = myMainSel->Picked (aNewDetected);
118     if (aNewPickedOwner->Selectable() == myLastPicked)
119     {
120       return myLastPicked->State() == 1
121            ? AIS_SOD_Selected
122            : AIS_SOD_OnlyOneDetected;
123     }
124  
125     // Previously detected object is unhilighted if it is not selected or hilighted 
126     // with selection color if it is selected. Such highlighting with selection color 
127     // is needed only if myToHilightSelected flag is true. In this case previously detected
128     // object has been already highlighted with myHilightColor during previous MoveTo() 
129     // method call. As result it is necessary to rehighligt it with mySelectionColor.
130     if (!myLastPicked.IsNull())
131     {
132       const Standard_Integer aHiMod = myLastPicked->HasHilightMode() ? myLastPicked->HilightMode() : 0;
133       if (myLastPicked->State() != 1)
134       {
135         myMainPM->Unhighlight (myLastPicked, aHiMod);
136         toUpdateViewer = Standard_True;
137       }
138       else if (myToHilightSelected)
139       {
140         myMainPM->Color (myLastPicked, mySelectionColor, aHiMod);
141         toUpdateViewer = Standard_True;
142       }
143     }
144
145     // initialize myLastPicked field with currently detected object
146     myLastPicked = Handle(AIS_InteractiveObject)::DownCast (aNewPickedOwner->Selectable());
147     myLastinMain = myLastPicked;
148
149     // highlight detected object if it is not selected or myToHilightSelected flag is true
150     if (!myLastPicked.IsNull())
151     {
152       if (myLastPicked->State() != 1 || myToHilightSelected)
153       {
154         const Standard_Integer aHiMod = myLastPicked->HasHilightMode() ? myLastPicked->HilightMode() : 0;
155         myMainPM->Color (myLastPicked, myHilightColor, aHiMod);
156         toUpdateViewer = Standard_True;
157       }
158
159       aStatus = myLastPicked->State() == 1
160               ? AIS_SOD_Selected
161               : AIS_SOD_OnlyOneDetected;
162     }
163   }
164   else 
165   {
166     // previously detected object is unhilighted if it is not selected or hilighted
167     // with selection color if it is selected
168     aStatus = AIS_SOD_Nothing;
169     if (!myLastPicked.IsNull())
170     {
171       Standard_Integer aHiMod = myLastPicked->HasHilightMode() ? myLastPicked->HilightMode() : 0;
172       if (myLastPicked->State() != 1)
173       {
174         myMainPM->Unhighlight (myLastPicked, aHiMod);
175         toUpdateViewer = Standard_True;
176       }
177       else if (myToHilightSelected)
178       {
179         myMainPM->Color (myLastPicked, mySelectionColor, aHiMod);
180         toUpdateViewer = Standard_True;
181       }
182     }
183
184     myLastinMain.Nullify();
185     myLastPicked.Nullify();
186   }
187
188   if (toUpdateViewer)
189   {
190     if (theToRedrawOnUpdate)
191     {
192       theView->Viewer()->Update();
193     }
194     else
195     {
196       theView->Viewer()->Invalidate();
197     }
198   }
199
200   mylastmoveview = theView;
201   return aStatus;
202 }
203
204 //=======================================================================
205 //function : Select
206 //purpose  : 
207 //=======================================================================
208 AIS_StatusOfPick AIS_InteractiveContext::Select (const Standard_Integer  theXPMin,
209                                                  const Standard_Integer  theYPMin,
210                                                  const Standard_Integer  theXPMax,
211                                                  const Standard_Integer  theYPMax,
212                                                  const Handle(V3d_View)& theView,
213                                                  const Standard_Boolean  toUpdateViewer)
214 {
215   // all objects detected by the selector are taken, previous current objects are emptied,
216   // new objects are put...
217
218   if (HasOpenedContext())
219   {
220     return myLocalContexts(myCurLocalIndex)->Select (theXPMin, theYPMin,
221                                                      theXPMax, theYPMax,
222                                                      theView, toUpdateViewer);
223   }
224
225   ClearCurrents (Standard_False);
226
227   Handle(StdSelect_ViewerSelector3d) aSelector;
228
229   if (theView->Viewer() == myMainVwr)
230   {
231     aSelector = myMainSel;
232     myWasLastMain = Standard_True;
233   }
234
235   aSelector->Pick (theXPMin, theYPMin, theXPMax, theYPMax, theView);
236   AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
237
238   AIS_MapOfInteractive anObjectsToSelect;
239   for (aSelector->Init(); aSelector->More(); aSelector->Next())
240   {
241     Handle(AIS_InteractiveObject) anObj = 
242       Handle(AIS_InteractiveObject)::DownCast (aSelector->Picked()->Selectable());
243     if (!anObj.IsNull())
244     {
245       anObjectsToSelect.Add (anObj);
246     }
247   }
248
249   AIS_MapIteratorOfMapOfInteractive anIt (anObjectsToSelect);
250   for ( ; anIt.More(); anIt.Next())
251   {
252     AIS_Selection::Select (anIt.Key());
253     anIt.Key()->State (1);
254   }
255
256   HilightCurrents (toUpdateViewer);
257
258   Standard_Integer aSelNum = NbCurrents();
259
260   return (aSelNum == 0) ? AIS_SOP_NothingSelected
261                         : (aSelNum == 1) ? AIS_SOP_OneSelected
262                                          : AIS_SOP_SeveralSelected;
263   
264 }
265
266 //=======================================================================
267 //function : Select
268 //purpose  : Selection by polyline
269 //=======================================================================
270 AIS_StatusOfPick AIS_InteractiveContext::Select (const TColgp_Array1OfPnt2d& thePolyline,
271                                                  const Handle(V3d_View)&     theView,
272                                                  const Standard_Boolean      toUpdateViewer)
273 {
274   // all objects detected by the selector are taken, previous current objects are emptied,
275   // new objects are put...
276
277   if (HasOpenedContext())
278   {
279     return myLocalContexts(myCurLocalIndex)->Select (thePolyline, theView, toUpdateViewer);
280   }
281
282   ClearCurrents (Standard_False);
283
284   Handle(StdSelect_ViewerSelector3d) aSelector;
285
286   if (theView->Viewer() == myMainVwr)
287   {
288     aSelector = myMainSel;
289     myWasLastMain = Standard_True;
290   }
291
292   aSelector->Pick (thePolyline, theView);
293   AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
294
295   AIS_MapOfInteractive anObjectsToSelect;
296   for (aSelector->Init(); aSelector->More(); aSelector->Next())
297   {
298     Handle(AIS_InteractiveObject) anObj = 
299       Handle( AIS_InteractiveObject )::DownCast (aSelector->Picked()->Selectable());
300     if (!anObj.IsNull())
301     {
302       anObjectsToSelect.Add (anObj);
303     }
304   }
305
306   AIS_MapIteratorOfMapOfInteractive anIt (anObjectsToSelect);
307   for ( ; anIt.More(); anIt.Next())
308   {
309     AIS_Selection::Select (anIt.Key());
310     anIt.Key()->State (1);
311   }
312
313   HilightCurrents (toUpdateViewer);
314
315   Standard_Integer aSelNum = NbCurrents();
316
317   return (aSelNum == 0) ? AIS_SOP_NothingSelected
318                         : (aSelNum == 1) ? AIS_SOP_OneSelected
319                                          : AIS_SOP_SeveralSelected;
320   
321 }
322
323 //=======================================================================
324 //function : Select
325 //purpose  : 
326 //=======================================================================
327 AIS_StatusOfPick AIS_InteractiveContext::Select (const Standard_Boolean toUpdateViewer)
328 {
329   if (HasOpenedContext())
330   {
331     if(myWasLastMain)
332     {
333       return myLocalContexts(myCurLocalIndex)->Select (toUpdateViewer);
334     }
335     else
336     {
337       myLocalContexts(myCurLocalIndex)->SetSelected (myLastPicked, toUpdateViewer);
338       return AIS_SOP_OneSelected;
339     }
340   }
341
342   if (myWasLastMain && !myLastinMain.IsNull())
343   {
344     if(myLastinMain->State() != 1)
345     {
346       SetCurrentObject (myLastinMain,Standard_False);
347       if(toUpdateViewer)
348       {
349         UpdateCurrentViewer();
350       }
351     }
352   }
353   else
354   {
355     AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
356     Handle(AIS_Selection) aSelection = AIS_Selection::CurrentSelection();
357     Handle(AIS_InteractiveObject) anObj;
358     for (aSelection->Init(); aSelection->More(); aSelection->Next())
359     {
360       anObj = Handle(AIS_InteractiveObject)::DownCast (aSelection->Value());
361       anObj->State (0);
362       Unhilight (anObj,Standard_False);
363       if (myObjects.IsBound (anObj) && myObjects(anObj)->IsSubIntensityOn())
364       {
365         HilightWithColor (anObj, mySubIntensity, Standard_False);
366       }
367     }
368
369     AIS_Selection::Select();
370     if (toUpdateViewer && myWasLastMain)
371     {
372         UpdateCurrentViewer();
373     }
374   }
375
376   Standard_Integer aSelNum = NbCurrents();
377
378   return (aSelNum == 0) ? AIS_SOP_NothingSelected
379                         : (aSelNum == 1) ? AIS_SOP_OneSelected
380                                          : AIS_SOP_SeveralSelected;
381 }
382
383 //=======================================================================
384 //function : ShiftSelect
385 //purpose  : 
386 //=======================================================================
387 AIS_StatusOfPick AIS_InteractiveContext::ShiftSelect (const Standard_Boolean toUpdateViewer)
388 {
389   if (HasOpenedContext())
390   {
391     if(myWasLastMain)
392     {
393       return myLocalContexts (myCurLocalIndex)->ShiftSelect (toUpdateViewer);
394     }
395     else
396     {
397       myLocalContexts (myCurLocalIndex)->AddOrRemoveSelected (myLastPicked, toUpdateViewer);
398
399       Standard_Integer aSelNum = NbSelected();
400       return (aSelNum == 0) ? AIS_SOP_NothingSelected
401                             : (aSelNum == 1) ? AIS_SOP_OneSelected
402                                              : AIS_SOP_SeveralSelected;
403     }
404   }
405
406   if (myWasLastMain && !myLastinMain.IsNull())
407   {
408     AddOrRemoveCurrentObject (myLastinMain, toUpdateViewer);
409   }
410
411   Standard_Integer aCurrentSelNum = NbCurrents();
412
413   return (aCurrentSelNum == 0) ? AIS_SOP_NothingSelected
414                                : (aCurrentSelNum == 1) ? AIS_SOP_OneSelected
415                                                        : AIS_SOP_SeveralSelected;
416 }
417
418 //=======================================================================
419 //function : ShiftSelect
420 //purpose  : 
421 //=======================================================================
422 AIS_StatusOfPick AIS_InteractiveContext::ShiftSelect (const Standard_Integer theXPMin,
423                                                       const Standard_Integer theYPMin,
424                                                       const Standard_Integer theXPMax,
425                                                       const Standard_Integer theYPMax,
426                                                       const Handle(V3d_View)& theView,
427                                                       const Standard_Boolean toUpdateViewer)
428 {
429   if (HasOpenedContext())
430   {
431     return myLocalContexts(myCurLocalIndex)->ShiftSelect (theXPMin, theYPMin, theXPMax, theYPMax,
432                                                           theView, toUpdateViewer);
433   }
434
435   UnhilightCurrents (Standard_False);
436
437   Handle(StdSelect_ViewerSelector3d) aSelector;
438   if (theView->Viewer() == myMainVwr)
439   {
440     aSelector = myMainSel;
441     myWasLastMain = Standard_True;
442   }
443   else
444   {
445     return AIS_SOP_NothingSelected;
446   }
447
448   aSelector->Pick (theXPMin, theYPMin, theXPMax, theYPMax, theView);
449   AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
450   for (aSelector->Init(); aSelector->More(); aSelector->Next())
451   {
452     Handle(AIS_InteractiveObject) anObjToSelect =  Handle(AIS_InteractiveObject)::DownCast (aSelector->Picked()->Selectable());
453     if (!anObjToSelect.IsNull())
454     {
455       AIS_SelectStatus aSelStatus = AIS_Selection::Select (anObjToSelect);
456       Standard_Integer aState = (aSelStatus == AIS_SS_Added) ? 1 : 0;
457       anObjToSelect->State (aState);
458     }
459   }
460
461   HilightCurrents (toUpdateViewer);
462
463   Standard_Integer aSelNum = NbCurrents();
464
465   return (aSelNum == 0) ? AIS_SOP_NothingSelected
466                         : (aSelNum == 1) ? AIS_SOP_OneSelected
467                                          : AIS_SOP_SeveralSelected;
468
469 }
470
471 //=======================================================================
472 //function : ShiftSelect
473 //purpose  : 
474 //=======================================================================
475 AIS_StatusOfPick AIS_InteractiveContext::ShiftSelect (const TColgp_Array1OfPnt2d& thePolyline,
476                                                       const Handle(V3d_View)& theView,
477                                                       const Standard_Boolean toUpdateViewer)
478 {
479   if (HasOpenedContext())
480   {
481     return myLocalContexts(myCurLocalIndex)->ShiftSelect (thePolyline, theView, toUpdateViewer);
482   }
483
484   UnhilightCurrents (Standard_False);
485
486   Handle(StdSelect_ViewerSelector3d) aSelector;
487
488   if (theView->Viewer() == myMainVwr)
489   {
490     aSelector= myMainSel;
491     myWasLastMain = Standard_True;
492   }
493   else
494   {
495     return AIS_SOP_NothingSelected;
496   }
497
498   aSelector->Pick (thePolyline, theView);
499
500   AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
501   for (aSelector->Init(); aSelector->More(); aSelector->Next())
502   {
503     Handle(AIS_InteractiveObject) anObjToSelect = Handle(AIS_InteractiveObject)::DownCast (aSelector->Picked()->Selectable());
504     if (!anObjToSelect.IsNull())
505     {
506       AIS_SelectStatus aSelStatus = AIS_Selection::Select (anObjToSelect);
507       Standard_Integer aState = (aSelStatus == AIS_SS_Added ) ? 1 : 0;
508       anObjToSelect->State (aState);
509     }
510   }
511
512   HilightCurrents (toUpdateViewer);
513
514   Standard_Integer aSelNum = NbCurrents();
515
516   return (aSelNum == 0) ? AIS_SOP_NothingSelected
517                         : (aSelNum == 1) ? AIS_SOP_OneSelected
518                                          : AIS_SOP_SeveralSelected;
519 }
520
521 //=======================================================================
522 //function : SetCurrentObject
523 //purpose  : 
524 //=======================================================================
525 void AIS_InteractiveContext::SetCurrentObject(const Handle(AIS_InteractiveObject)& anIObj,
526                                               const Standard_Boolean updateviewer)
527 {
528   // single case processing...
529   if(NbCurrents()==1 && anIObj->State()==1){
530     Quantity_NameOfColor HiCol;
531     Standard_Boolean HasHiCol;
532     if(IsHilighted(anIObj,HasHiCol,HiCol)){
533       if(HasHiCol && HiCol!= mySelectionColor) {
534 #ifdef UKI60826
535         HilightWithColor(anIObj,mySelectionColor,updateviewer);
536 #else
537         Hilight(anIObj,updateviewer);
538 #endif
539       }
540     }
541     return;
542   }
543
544
545   // 
546   if(!HasOpenedContext()) {
547     if(anIObj.IsNull()) return;
548     if(!myObjects.IsBound(anIObj)) 
549       Display(anIObj,Standard_False);
550     
551     AIS_Selection::SetCurrentSelection(myCurrentName.ToCString());
552     Handle(AIS_Selection) sel = AIS_Selection::Selection(myCurrentName.ToCString());
553     Handle(Standard_Transient) TR;
554     Handle(AIS_InteractiveObject) IO;
555     sel->Init();
556     while (sel->More()) {
557       TR = sel->Value();
558       IO = *((Handle(AIS_InteractiveObject)*)&TR);
559       Unhilight(IO,Standard_False);
560       IO->State(0);
561       sel->Next();
562     }
563     
564     // added to avoid untimely viewer update...
565     AIS_Selection::ClearAndSelect(anIObj);
566     anIObj->State(1);
567     Quantity_NameOfColor HiCol;
568     Standard_Boolean HasHiCol;
569     if(IsHilighted(anIObj,HasHiCol,HiCol)){
570       if(HasHiCol && HiCol!= mySelectionColor) {
571 #ifdef UKI60826
572           HilightWithColor(anIObj,mySelectionColor,Standard_False);
573 #else
574           Hilight(anIObj,Standard_False);
575 #endif
576       }
577     }
578     else{
579 #ifdef UKI60826
580         HilightWithColor(anIObj,mySelectionColor,Standard_False);
581 #else
582       Hilight(anIObj,Standard_False);
583 #endif
584     }
585     if (updateviewer) 
586       UpdateCurrentViewer();
587   }
588   else{
589 #ifdef AIS_DEB
590     cout<<"Nothing Done : Opened Local Context"<<endl;
591 #endif
592   }
593 }
594
595 //=======================================================================
596 //function : AddOrRemoveCurrentObject
597 //purpose  : 
598 //=======================================================================
599
600 void AIS_InteractiveContext::
601 AddOrRemoveCurrentObject(const Handle(AIS_InteractiveObject)& anIObj,
602                          const Standard_Boolean updateviewer)
603 {
604   if(anIObj.IsNull()) return;
605 //OCC5682 to remove an object from neutral point even if Local Context is opened (Remove method)
606   if(!HasOpenedContext() /*OCC5682*/|| myObjects.IsBound(anIObj)) {
607     
608     AIS_Selection::SetCurrentSelection(myCurrentName.ToCString());
609     Handle(AIS_Selection) sel = AIS_Selection::Selection(myCurrentName.ToCString());
610
611     AIS_SelectStatus SelStat = AIS_Selection::Select(anIObj);
612     
613     Standard_Integer mod = (SelStat==AIS_SS_Added) ? 1 : 0;
614     anIObj->State(mod);
615     if(mod==1)
616 #ifdef BUC60814
617       HilightWithColor(anIObj,mySelectionColor,Standard_False);
618 #else
619       Hilight(anIObj,Standard_False);
620 #endif
621     else 
622       Unhilight(anIObj,Standard_False);
623     
624     
625     if(updateviewer) 
626       UpdateCurrentViewer();
627   }
628   else{
629 #ifdef AIS_DEB
630     cout<<"Nothing Done : Opened Local Context"<<endl;
631 #endif
632   }
633 }
634 //=======================================================================
635 //function : UpdateCurrent
636 //purpose  : 
637 //=======================================================================
638
639 void AIS_InteractiveContext::UpdateCurrent()
640 {
641   HilightCurrents();
642 }
643
644 //=======================================================================
645 //function : IsCurrent
646 //purpose  : 
647 //=======================================================================
648
649 Standard_Boolean AIS_InteractiveContext::IsCurrent(const Handle(AIS_InteractiveObject)& anIObj) const 
650 {
651
652   if (anIObj.IsNull()) return Standard_False;
653   return (anIObj->State()==1);
654   
655   //   Handle(AIS_Selection) sel = AIS_Selection::Selection(myCurrentName.ToCString());
656   //   sel->Init();
657   //   while (sel->More()) {
658   //     if (anIObj == sel->Value()) return Standard_True;
659   //     sel->Next();
660   //   }
661   //   return Standard_False;
662
663 }
664
665 //=======================================================================
666 //function : InitCurrent
667 //purpose  : 
668 //=======================================================================
669
670 void AIS_InteractiveContext::InitCurrent()
671 {
672   AIS_Selection::Selection(myCurrentName.ToCString())->Init();
673 }
674
675 //=======================================================================
676 //function : MoreCurrent
677 //purpose  : 
678 //=======================================================================
679
680 Standard_Boolean AIS_InteractiveContext::MoreCurrent() const 
681 {
682   return AIS_Selection::Selection(myCurrentName.ToCString())->More();
683
684 }
685
686 //=======================================================================
687 //function : NextCurrent
688 //purpose  : 
689 //=======================================================================
690
691 void AIS_InteractiveContext::NextCurrent()
692 {
693   AIS_Selection::Selection(myCurrentName.ToCString())->Next();
694 }
695
696 //=======================================================================
697 //function : Current
698 //purpose  : 
699 //=======================================================================
700
701 Handle(AIS_InteractiveObject) AIS_InteractiveContext::Current() const 
702 {
703   Handle(Standard_Transient) TR = AIS_Selection::Selection(myCurrentName.ToCString())->Value();
704   Handle(AIS_InteractiveObject) IO = *((Handle(AIS_InteractiveObject)*)&TR);
705   return IO;
706 }
707
708 //=======================================================================
709 //function : FirstCurrentObject
710 //purpose  : 
711 //=======================================================================
712 Handle(AIS_InteractiveObject) AIS_InteractiveContext::FirstCurrentObject()  
713 {
714   Handle(AIS_InteractiveObject) IO ;
715   InitCurrent();
716   if(MoreCurrent()){
717     return Current();
718   }
719   return IO;
720 }
721
722 //=======================================================================
723 //function : NbCurrents
724 //purpose  : 
725 //=======================================================================
726 Standard_Integer AIS_InteractiveContext::NbCurrents()
727 {
728   Standard_Integer i(0);
729   for(InitCurrent();MoreCurrent();NextCurrent())
730     {i++;}
731   return i;
732 }
733
734 //=======================================================================
735 //function : HilightCurrents
736 //purpose  : 
737 //=======================================================================
738 void AIS_InteractiveContext::HilightCurrents(const Standard_Boolean updateviewer)
739 {
740   AIS_Selection::SetCurrentSelection(myCurrentName.ToCString());
741   Handle(AIS_Selection) sel = AIS_Selection::Selection(myCurrentName.ToCString());
742   Handle(Standard_Transient) TR;
743   Handle(AIS_InteractiveObject) IO;
744   sel->Init();
745   while (sel->More()) {
746     TR = sel->Value();
747     IO = *((Handle(AIS_InteractiveObject)*)&TR);
748 #ifdef BUC60814
749     HilightWithColor(IO,mySelectionColor,Standard_False);
750 #else
751     Hilight(IO,Standard_False);
752 #endif
753     sel->Next();
754   }
755   if(updateviewer) 
756     UpdateCurrentViewer();
757 }
758
759 //=======================================================================
760 //function : UnhilightCurrents
761 //purpose  : 
762 //=======================================================================
763
764 void AIS_InteractiveContext::UnhilightCurrents(const Standard_Boolean updateviewer)
765 {
766   AIS_Selection::SetCurrentSelection(myCurrentName.ToCString());
767   Handle(AIS_Selection) sel = AIS_Selection::Selection(myCurrentName.ToCString());
768   Handle(Standard_Transient) TR;
769   Handle(AIS_InteractiveObject) IO;
770   sel->Init();
771   while (sel->More()) {
772     TR = sel->Value();
773     IO = *((Handle(AIS_InteractiveObject)*)&TR);
774     Unhilight(IO,Standard_False);
775     sel->Next();
776   }
777   if(updateviewer) 
778     UpdateCurrentViewer();
779 }
780
781 //=======================================================================
782 //function : ClearCurrents
783 //purpose  : 
784 //=======================================================================
785
786 void AIS_InteractiveContext::ClearCurrents(const Standard_Boolean updateviewer)
787 {
788   if(NbCurrents()==0) return;
789   AIS_Selection::SetCurrentSelection(myCurrentName.ToCString());
790   Handle(AIS_Selection) S = AIS_Selection::CurrentSelection();
791   Handle(Standard_Transient) Tr;
792   Handle(AIS_InteractiveObject) IO;
793   for(S->Init();S->More();S->Next()){
794     Tr = S->Value();
795     IO = (*((Handle(AIS_InteractiveObject)*)&Tr));
796     IO->State(0);
797     Unhilight(IO,Standard_False);
798   }
799   AIS_Selection::Select();
800   if(updateviewer)
801     UpdateCurrentViewer();
802 }
803
804
805 //=======================================================================
806 //function : HilightSelected
807 //purpose  : 
808 //=======================================================================
809
810 void AIS_InteractiveContext::HilightSelected(const Standard_Boolean updateviewer)
811 {
812   if(!HasOpenedContext()){
813     AIS_Selection::SetCurrentSelection(mySelectionName.ToCString());
814     Handle(AIS_Selection) sel = AIS_Selection::Selection(myCurrentName.ToCString());
815     Handle(Standard_Transient) TR;
816     Handle(AIS_InteractiveObject) IO;
817     sel->Init();
818     while (sel->More()) {
819       TR = sel->Value();
820       IO = *((Handle(AIS_InteractiveObject)*)&TR);
821 #ifdef BUC60814
822       HilightWithColor(IO,mySelectionColor,Standard_False);
823 #else
824       Hilight(IO,Standard_False);
825 #endif
826       sel->Next();
827     }
828   }
829   else
830     {
831       myLocalContexts(myCurLocalIndex)->HilightPicked(updateviewer);
832     }
833   if(updateviewer)
834     UpdateCurrentViewer();
835 }
836
837 //=======================================================================
838 //function : UnhilightSelected
839 //purpose  : 
840 //=======================================================================
841
842 void AIS_InteractiveContext::UnhilightSelected(const Standard_Boolean updateviewer)
843 {
844   if(!HasOpenedContext()){
845     AIS_Selection::SetCurrentSelection(mySelectionName.ToCString());
846     Handle(AIS_Selection) sel = AIS_Selection::Selection(myCurrentName.ToCString());
847     Handle(Standard_Transient) TR;
848     Handle(AIS_InteractiveObject) IO;
849     sel->Init();
850     while (sel->More()) {
851       TR = sel->Value();
852       IO = *((Handle(AIS_InteractiveObject)*)&TR);
853       Unhilight(IO,Standard_False);
854       sel->Next();
855     }
856     if(updateviewer)
857       UpdateCurrentViewer();
858   }
859   else
860     {
861       myLocalContexts(myCurLocalIndex)->UnhilightPicked(updateviewer);
862     }
863 }
864
865
866 //=======================================================================
867 //function : ClearSelected
868 //purpose  : 
869 //=======================================================================
870
871 void AIS_InteractiveContext::ClearSelected(const Standard_Boolean updateviewer)
872 {
873   if(!HasOpenedContext())
874     ClearCurrents(updateviewer);
875   else 
876     myLocalContexts(myCurLocalIndex)->ClearSelected(updateviewer);
877   
878 }
879
880
881 //=======================================================================
882 //function : SetSelectedCurrent
883 //purpose  : 
884 //=======================================================================
885
886 void AIS_InteractiveContext::SetSelectedCurrent()
887 {
888
889
890 #ifdef AIS_DEB
891   cout<<"Not Yet Implemented"<<endl;
892 #endif
893 }
894
895 //=======================================================================
896 //function : UpdateSelection
897 //purpose  : 
898 //=======================================================================
899
900 void AIS_InteractiveContext::UpdateSelected(const Standard_Boolean updateviewer)
901 {
902   if(!HasOpenedContext()) {
903     HilightSelected();
904 #ifndef BUG
905     return;
906 #endif
907   }
908   myLocalContexts(myCurLocalIndex)->UpdateSelected(updateviewer);
909 }
910
911 //=======================================================================
912 //function : SetCurrentObject
913 //purpose  : 
914 //=======================================================================
915 void AIS_InteractiveContext::SetSelected(const Handle(AIS_InteractiveObject)& anIObj,const Standard_Boolean updateviewer)
916 {
917   if(!HasOpenedContext()) 
918     SetCurrentObject(anIObj,updateviewer);
919   else
920     myLocalContexts(myCurLocalIndex)->SetSelected(anIObj,updateviewer);
921 }
922
923 //=======================================================================
924 //function : AddOrRemoveSelected
925 //purpose  : 
926 //=======================================================================
927
928 void AIS_InteractiveContext::AddOrRemoveSelected(const Handle(AIS_InteractiveObject)& anIObj,
929                                                  const Standard_Boolean updateviewer)
930 {
931   if(!anIObj->HasInteractiveContext()) anIObj->SetContext(this);
932   if(!HasOpenedContext())
933     AddOrRemoveCurrentObject(anIObj,updateviewer);
934   else
935     myLocalContexts(myCurLocalIndex)->AddOrRemoveSelected(anIObj,updateviewer);
936   
937 }
938 //=======================================================================
939 //function : AddOrRemoveSelected
940 //purpose  : 
941 //=======================================================================
942
943 void AIS_InteractiveContext::AddOrRemoveSelected(const TopoDS_Shape& aShap,
944                                             const Standard_Boolean updateviewer)
945
946   if(!HasOpenedContext()) {
947 #ifdef AIS_DEB
948     cout<<" Attempt to remove a selected shape with no opened local context"<<endl;
949 #endif
950     return;
951   }
952   
953   myLocalContexts(myCurLocalIndex)->AddOrRemoveSelected(aShap,updateviewer);
954   if(updateviewer) UpdateCurrentViewer();
955   
956 }
957 //=======================================================================
958 //function : AddOrRemoveSelected
959 //purpose  : 
960 //=======================================================================
961
962 void AIS_InteractiveContext::AddOrRemoveSelected(const Handle(SelectMgr_EntityOwner)& Own,
963                                                  const Standard_Boolean updateviewer)
964
965   if(!HasOpenedContext()) {
966 #ifdef AIS_DEB
967     cout<<" Attempt to remove a selected ownr with no opened local context"<<endl;
968 #endif
969     return;
970   }
971   
972   myLocalContexts(myCurLocalIndex)->AddOrRemoveSelected(Own,Standard_False);
973   if(updateviewer) UpdateCurrentViewer();
974   
975 }
976
977
978
979 //=======================================================================
980 //function : IsSelected
981 //purpose  : 
982 //=======================================================================
983
984 Standard_Boolean AIS_InteractiveContext::
985 IsSelected(const Handle(AIS_InteractiveObject)& anIObj) const 
986 {
987   if (anIObj.IsNull()) return Standard_False;
988   
989   if(!HasOpenedContext()) 
990     return (anIObj->State()==1);
991   else 
992     return myLocalContexts(myCurLocalIndex)->IsSelected(anIObj);
993 }
994
995
996 //=======================================================================
997 //function : InitSelected
998 //purpose  : 
999 //=======================================================================
1000
1001 void AIS_InteractiveContext::InitSelected()
1002 {
1003   if(!HasOpenedContext())
1004     AIS_Selection::Selection(myCurrentName.ToCString())->Init();
1005   else
1006     myLocalContexts(myCurLocalIndex)->InitSelected();
1007 }
1008
1009 //=======================================================================
1010 //function : MoreSelected
1011 //purpose  : 
1012 //=======================================================================
1013
1014 Standard_Boolean AIS_InteractiveContext::MoreSelected() const 
1015 {
1016   if(!HasOpenedContext())
1017     return AIS_Selection::Selection(myCurrentName.ToCString())->More();
1018   return myLocalContexts(myCurLocalIndex)->MoreSelected();
1019 }
1020
1021 //=======================================================================
1022 //function : NextSelected
1023 //purpose  : 
1024 //=======================================================================
1025
1026 void AIS_InteractiveContext::NextSelected()
1027 {
1028   if(!HasOpenedContext())
1029     AIS_Selection::Selection(myCurrentName.ToCString())->Next();
1030   else
1031     myLocalContexts(myCurLocalIndex)->NextSelected();
1032 }
1033
1034 //=======================================================================
1035 //function : HasSelectedShape
1036 //purpose  : 
1037 //=======================================================================
1038
1039 Standard_Boolean AIS_InteractiveContext::HasSelectedShape() const 
1040 {
1041   if(!HasOpenedContext()) {
1042 #ifdef IMP280200
1043     Handle(AIS_Shape) shape = 
1044         Handle(AIS_Shape)::DownCast(SelectedInteractive());
1045     if( !shape.IsNull() ) return Standard_True;
1046 #endif
1047     return Standard_False;
1048   }
1049   return myLocalContexts(myCurLocalIndex)->HasShape();
1050   
1051   
1052 }
1053
1054 //=======================================================================
1055 //function : SelectedShape
1056 //purpose  : 
1057 //=======================================================================
1058
1059 TopoDS_Shape AIS_InteractiveContext::SelectedShape() const 
1060 {
1061   if (!HasOpenedContext())
1062   {
1063     TopoDS_Shape aResShape;
1064     Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast (SelectedInteractive());
1065     if (!aShape.IsNull())
1066     {
1067       aResShape = aShape->Shape().Located (TopLoc_Location (SelectedInteractive()->Transformation()) * aShape->Shape().Location());
1068     }
1069
1070     return aResShape;
1071   } 
1072   else
1073   {
1074     return myLocalContexts (myCurLocalIndex)->SelectedShape();
1075   }
1076 }
1077
1078 //=======================================================================
1079 //function : Interactive
1080 //purpose  : 
1081 //=======================================================================
1082
1083 Handle(AIS_InteractiveObject) AIS_InteractiveContext::Interactive() const 
1084 {
1085   return SelectedInteractive();
1086 }
1087
1088 Handle(AIS_InteractiveObject) AIS_InteractiveContext::SelectedInteractive() const 
1089 {
1090   if(!HasOpenedContext()){
1091     Handle(Standard_Transient) TR  =AIS_Selection::Selection(myCurrentName.ToCString())->Value();
1092     Handle(AIS_InteractiveObject) IO = *((Handle(AIS_InteractiveObject)*)&TR);
1093     return IO;}
1094   
1095   return  myLocalContexts(myCurLocalIndex)->SelectedInteractive();
1096   
1097 }
1098 //=======================================================================
1099 //function : Interactive
1100 //purpose  : 
1101 //=======================================================================
1102
1103 Handle(SelectMgr_EntityOwner) AIS_InteractiveContext::SelectedOwner() const 
1104 {
1105   if(!HasOpenedContext()){
1106     Handle(SelectMgr_EntityOwner) Ownr;
1107     return Ownr;
1108   }
1109   return  myLocalContexts(myCurLocalIndex)->SelectedOwner();
1110   
1111 }
1112
1113 //=======================================================================
1114 //function : EntityOwners
1115 //purpose  : 
1116 //=======================================================================
1117 void AIS_InteractiveContext::EntityOwners(SelectMgr_IndexedMapOfOwner& theOwners,
1118                                           const Handle(AIS_InteractiveObject)& theIObj,
1119                                           const Standard_Integer theMode) const 
1120 {
1121   if ( theIObj.IsNull() )
1122       return;
1123
1124   TColStd_ListOfInteger aModes;
1125   if ( theMode == -1 )
1126     ActivatedModes( theIObj, aModes );
1127   else
1128     aModes.Append( theMode );
1129
1130   TColStd_ListIteratorOfListOfInteger anItr( aModes );
1131   for (; anItr.More(); anItr.Next() )
1132   {
1133     int aMode = anItr.Value();
1134     if ( !theIObj->HasSelection( aMode ) )
1135       continue;
1136
1137     Handle(SelectMgr_Selection) aSel = theIObj->Selection( aMode );
1138
1139     for ( aSel->Init(); aSel->More(); aSel->Next() )
1140     {
1141       Handle(SelectBasics_SensitiveEntity) aEntity = aSel->Sensitive();
1142       if ( aEntity.IsNull() )
1143         continue;
1144
1145       Handle(SelectMgr_EntityOwner) aOwner =
1146         Handle(SelectMgr_EntityOwner)::DownCast(aEntity->OwnerId());
1147       if ( !aOwner.IsNull() )
1148         theOwners.Add( aOwner );
1149     }
1150   }
1151 }
1152
1153 Standard_Integer AIS_InteractiveContext::NbSelected() 
1154 {
1155   Standard_Integer i(0);
1156   for(InitSelected();MoreSelected();NextSelected())
1157     {i++;}
1158   return i;
1159 }
1160
1161 //=======================================================================
1162 //function : HasApplicative
1163 //purpose  : 
1164 //=======================================================================
1165
1166   Standard_Boolean AIS_InteractiveContext::HasApplicative() const 
1167 {
1168   return Interactive()->HasOwner();
1169 }
1170
1171 //=======================================================================
1172 //function : Applicative
1173 //purpose  : 
1174 //=======================================================================
1175
1176 Handle(Standard_Transient) AIS_InteractiveContext::Applicative() const 
1177 {
1178   return Interactive()->GetOwner();
1179 }
1180
1181
1182
1183 //==================================================
1184 // Function: 
1185 // Purpose :
1186 //==================================================
1187 Standard_Boolean AIS_InteractiveContext::HasDetected() const 
1188 {
1189   if(HasOpenedContext())
1190     return myLocalContexts(myCurLocalIndex)->HasDetected();
1191   else
1192     return !myLastPicked.IsNull();
1193 }
1194
1195 //=======================================================================
1196 //function : HasDetectedShape
1197 //purpose  : 
1198 //=======================================================================
1199
1200 Standard_Boolean AIS_InteractiveContext::HasDetectedShape() const 
1201 {
1202   if(HasOpenedContext())
1203     return myLocalContexts(myCurLocalIndex)->HasDetectedShape();
1204   return Standard_False;
1205 }
1206
1207 //=======================================================================
1208 //function : DetectedShape
1209 //purpose  : 
1210 //=======================================================================
1211
1212 const TopoDS_Shape&
1213 AIS_InteractiveContext::DetectedShape() const
1214 {
1215   return myLocalContexts(myCurLocalIndex)->DetectedShape();
1216 }                                           
1217
1218 //=======================================================================
1219 //function : DetectedInteractive
1220 //purpose  : 
1221 //=======================================================================
1222
1223 Handle(AIS_InteractiveObject) 
1224 AIS_InteractiveContext::DetectedInteractive() const 
1225 {
1226   if(HasOpenedContext())
1227     return myLocalContexts(myCurLocalIndex)->DetectedInteractive();
1228   return myLastPicked;
1229 }
1230
1231
1232 Standard_Boolean AIS_InteractiveContext::HasNextDetected() const 
1233 {
1234   if(!HasOpenedContext())
1235     return Standard_False; // temporaire
1236   else
1237     return myLocalContexts(myCurLocalIndex)->HasNextDetected();
1238   
1239 }
1240
1241
1242 //=======================================================================
1243 //function : DetectedOwner
1244 //purpose  : 
1245 //=======================================================================
1246 Handle(SelectMgr_EntityOwner) AIS_InteractiveContext::DetectedOwner() const
1247 {
1248   if(HasOpenedContext())
1249     return myLocalContexts(myCurLocalIndex)->DetectedOwner();
1250   Handle(SelectMgr_EntityOwner) Ownr;
1251   return Ownr;
1252 }
1253
1254
1255 //=======================================================================
1256 //function : HilightNextDetected
1257 //purpose  :
1258 //=======================================================================
1259 Standard_Integer AIS_InteractiveContext::HilightNextDetected (const Handle(V3d_View)& theView,
1260                                                               const Standard_Boolean  theToRedrawImmediate)
1261 {
1262   return HasOpenedContext()
1263        ? myLocalContexts (myCurLocalIndex)->HilightNextDetected (theView, theToRedrawImmediate)
1264        : 0;
1265     
1266 }
1267
1268 //=======================================================================
1269 //function : HilightNextDetected
1270 //purpose  :
1271 //=======================================================================
1272 Standard_Integer AIS_InteractiveContext::HilightPreviousDetected (const Handle(V3d_View)& theView,
1273                                                                   const Standard_Boolean  theToRedrawImmediate)
1274 {
1275   return HasOpenedContext()
1276        ? myLocalContexts (myCurLocalIndex)->HilightPreviousDetected (theView, theToRedrawImmediate)
1277        : 0;
1278     
1279 }
1280
1281 //=======================================================================
1282 //function : InitDetected
1283 //purpose  :
1284 //=======================================================================
1285 void AIS_InteractiveContext::InitDetected()
1286 {
1287   if (HasOpenedContext())
1288   {
1289     myLocalContexts(myCurLocalIndex)->InitDetected();
1290     return;
1291   }
1292
1293   if(myAISDetectedSeq.Length() != 0)
1294   {
1295     myAISCurDetected = 1;
1296   }
1297 }
1298
1299 //=======================================================================
1300 //function : MoreDetected
1301 //purpose  :
1302 //=======================================================================
1303 Standard_Boolean AIS_InteractiveContext::MoreDetected() const
1304 {
1305   if (HasOpenedContext())
1306   {
1307     return myLocalContexts(myCurLocalIndex)->MoreDetected();
1308   }
1309
1310   return (myAISCurDetected > 0 && myAISCurDetected <= myAISDetectedSeq.Length()) ?
1311           Standard_True : Standard_False;
1312 }
1313
1314 //=======================================================================
1315 //function : NextDetected
1316 //purpose  :
1317 //=======================================================================
1318 void AIS_InteractiveContext::NextDetected()
1319 {
1320   if(HasOpenedContext())
1321   {
1322     myLocalContexts(myCurLocalIndex)->NextDetected();
1323     return;
1324   }
1325
1326   myAISCurDetected++;
1327 }
1328
1329 //=======================================================================
1330 //function : DetectedCurrentShape
1331 //purpose  :
1332 //=======================================================================
1333 const TopoDS_Shape& AIS_InteractiveContext::DetectedCurrentShape() const
1334 {
1335   if (HasOpenedContext())
1336   {
1337     return myLocalContexts(myCurLocalIndex)->DetectedCurrentShape();
1338   }
1339
1340   static TopoDS_Shape aDummyShape;
1341
1342   Handle(AIS_Shape) aCurrentShape = Handle(AIS_Shape)::DownCast (DetectedCurrentObject());
1343
1344   if (aCurrentShape.IsNull())
1345   {
1346     return aDummyShape;
1347   }
1348
1349   return aCurrentShape->Shape();
1350 }
1351
1352 //=======================================================================
1353 //function : DetectedCurrentObject
1354 //purpose  :
1355 //=======================================================================
1356 Handle(AIS_InteractiveObject) AIS_InteractiveContext::DetectedCurrentObject() const
1357 {
1358   if (HasOpenedContext())
1359   {
1360     return myLocalContexts(myCurLocalIndex)->DetectedCurrentObject();
1361   }
1362
1363   return MoreDetected() ? myAISDetectedSeq(myAISCurDetected) : NULL;
1364 }