0025448: Coding - remove useless header Quantity_Color_1.hxx
[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
18 #include <AIS_DataMapIteratorOfDataMapOfIOStatus.hxx>
19 #include <AIS_GlobalStatus.hxx>
20 #include <AIS_InteractiveContext.hxx>
21 #include <AIS_InteractiveObject.hxx>
22 #include <AIS_LocalContext.hxx>
23 #include <AIS_MapIteratorOfMapOfInteractive.hxx>
24 #include <AIS_MapOfInteractive.hxx>
25 #include <AIS_Selection.hxx>
26 #include <AIS_Shape.hxx>
27 #include <AIS_StatusOfDetection.hxx>
28 #include <AIS_StatusOfPick.hxx>
29 #include <Aspect_Grid.hxx>
30 #include <Prs3d_BasicAspect.hxx>
31 #include <Prs3d_LineAspect.hxx>
32 #include <Prs3d_Presentation.hxx>
33 #include <Quantity_Color.hxx>
34 #include <SelectBasics_SensitiveEntity.hxx>
35 #include <SelectMgr_EntityOwner.hxx>
36 #include <SelectMgr_Filter.hxx>
37 #include <SelectMgr_OrFilter.hxx>
38 #include <SelectMgr_Selection.hxx>
39 #include <SelectMgr_SelectionManager.hxx>
40 #include <Standard_Transient.hxx>
41 #include <StdSelect_BRepOwner.hxx>
42 #include <StdSelect_ViewerSelector3d.hxx>
43 #include <TCollection_AsciiString.hxx>
44 #include <TCollection_ExtendedString.hxx>
45 #include <TColStd_ListIteratorOfListOfInteger.hxx>
46 #include <TopLoc_Location.hxx>
47 #include <V3d_AmbientLight.hxx>
48 #include <V3d_DirectionalLight.hxx>
49 #include <V3d_Light.hxx>
50 #include <V3d_PositionalLight.hxx>
51 #include <V3d_SpotLight.hxx>
52 #include <V3d_View.hxx>
53 #include <V3d_Viewer.hxx>
54
55 typedef NCollection_DataMap<Handle(AIS_InteractiveObject), NCollection_Handle<SelectMgr_SequenceOfOwner> > AIS_MapOfObjSelectedOwners;
56
57 namespace
58 {
59   TopoDS_Shape AIS_myDummyShape;
60 }
61
62 //=======================================================================
63 //function : highlightWithColor
64 //purpose  :
65 //=======================================================================
66 void AIS_InteractiveContext::highlightWithColor (const Handle(SelectMgr_EntityOwner)& theOwner,
67                                                  const Quantity_NameOfColor theColor,
68                                                  const Handle(V3d_Viewer)& theViewer)
69 {
70   const Handle(AIS_InteractiveObject) anObj =
71     Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
72   if (anObj.IsNull())
73     return;
74   const Standard_Integer aHiMode = anObj->HasHilightMode() ? anObj->HilightMode() : 0;
75
76   myMainPM->BeginImmediateDraw();
77   theOwner->HilightWithColor (myMainPM, theColor, aHiMode);
78   myMainPM->EndImmediateDraw (theViewer.IsNull() ? myMainVwr : theViewer);
79 }
80
81 //=======================================================================
82 //function : highlightSelected
83 //purpose  :
84 //=======================================================================
85 void AIS_InteractiveContext::highlightSelected (const Handle(SelectMgr_EntityOwner)& theOwner,
86                                                 const Quantity_NameOfColor theSelColor)
87 {
88   const Handle(AIS_InteractiveObject) anObj =
89     Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
90   if (anObj.IsNull())
91     return;
92   const Standard_Integer aHiMode = anObj->HasHilightMode() ? anObj->HilightMode() : 0;
93
94   if (!theOwner->IsAutoHilight())
95   {
96     SelectMgr_SequenceOfOwner aSeq;
97     for (mySelection->Init(); mySelection->More(); mySelection->Next())
98     {
99       const Handle(SelectMgr_EntityOwner) aSelOwnr =
100         Handle(SelectMgr_EntityOwner)::DownCast (mySelection->Value());
101       if (aSelOwnr->Selectable() != anObj)
102         continue;
103       aSeq.Append (aSelOwnr);
104     }
105     anObj->HilightSelected (myMainPM, aSeq);
106   }
107   else
108   {
109     theOwner->HilightWithColor (myMainPM, theSelColor, aHiMode);
110   }
111 }
112
113 //=======================================================================
114 //function : unhighlightSelected
115 //purpose  :
116 //=======================================================================
117 void AIS_InteractiveContext::unhighlightSelected (const Standard_Boolean theIsToHilightSubIntensity)
118 {
119   NCollection_IndexedMap<Handle(AIS_InteractiveObject)> anObjToClear;
120   for (mySelection->Init(); mySelection->More(); mySelection->Next())
121   {
122     const Handle(SelectMgr_EntityOwner) anOwner =
123       Handle(SelectMgr_EntityOwner)::DownCast (mySelection->Value());
124     if (anOwner.IsNull() || !anOwner->HasSelectable())
125       continue;
126
127     const Handle(AIS_InteractiveObject) anInteractive =
128       Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
129     if (anOwner->IsAutoHilight())
130     {
131       const Standard_Integer aHiMode = anInteractive->HasHilightMode() ? anInteractive->HilightMode() : 0;
132       anOwner->Unhilight (myMainPM, aHiMode);
133       if (theIsToHilightSubIntensity)
134       {
135         if (myObjects.IsBound (anInteractive) && myObjects (anInteractive)->IsSubIntensityOn())
136         {
137           anOwner->HilightWithColor (myMainPM, mySubIntensity, aHiMode);
138         }
139       }
140     }
141     else
142     {
143       if (!anObjToClear.Contains (anInteractive))
144         anObjToClear.Add (anInteractive);
145     }
146     anOwner->State (0);
147     if (anOwner == anInteractive->GlobalSelOwner())
148     {
149       myObjects.ChangeFind (anInteractive)->SetHilightStatus (Standard_False);
150     }
151   }
152   for (NCollection_IndexedMap<Handle(AIS_InteractiveObject)>::Iterator anIter (anObjToClear); anIter.More(); anIter.Next())
153   {
154     const Handle(AIS_InteractiveObject)& anObj = anIter.Value();
155     const Standard_Integer aHiMode = anObj->HasHilightMode() ? anObj->HilightMode() : 0;
156     myMainPM->Unhighlight (anObj, aHiMode);
157     anObj->ClearSelected();
158   }
159 }
160
161 //=======================================================================
162 //function : MoveTo
163 //purpose  :
164 //=======================================================================
165 AIS_StatusOfDetection AIS_InteractiveContext::MoveTo (const Standard_Integer  theXPix,
166                                                       const Standard_Integer  theYPix,
167                                                       const Handle(V3d_View)& theView,
168                                                       const Standard_Boolean  theToRedrawOnUpdate)
169 {
170   if (HasOpenedContext())
171   {
172     myWasLastMain = Standard_True;
173     return myLocalContexts (myCurLocalIndex)->MoveTo (theXPix, theYPix, theView, theToRedrawOnUpdate);
174   }
175
176   myAISCurDetected = 0;
177   myAISDetectedSeq.Clear();
178
179   if (theView->Viewer() != myMainVwr)
180   {
181     return AIS_SOD_Error;
182   }
183
184   // preliminaires
185   myLastPicked  = myLastinMain;
186   myWasLastMain = Standard_True;
187   AIS_StatusOfDetection aStatus        = AIS_SOD_Nothing;
188   Standard_Boolean      toUpdateViewer = Standard_False;
189
190   myFilters->SetDisabledObjects (theView->View()->HiddenObjects());
191   myMainSel->Pick (theXPix, theYPix, theView);
192
193   // filling of myAISDetectedSeq sequence storing information about detected AIS objects
194   // (the objects must be AIS_Shapes)
195   const Standard_Integer aDetectedNb = myMainSel->NbPicked();
196   Standard_Integer aNewDetected = 0;
197   for (Standard_Integer aDetIter = 1; aDetIter <= aDetectedNb; ++aDetIter)
198   {
199     Handle(SelectMgr_EntityOwner) anOwner = myMainSel->Picked (aDetIter);
200     if (anOwner.IsNull()
201      || !myFilters->IsOk (anOwner))
202     {
203       continue;
204     }
205
206     if (aNewDetected < 1)
207     {
208       aNewDetected = aDetIter;
209     }
210     Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
211     if (!anObj.IsNull())
212     {
213       myAISDetectedSeq.Append (anObj);
214     }
215   }
216
217   if (aNewDetected >= 1)
218   {
219     // Does nothing if previously detected object is equal to the current one.
220     // However in advanced selection modes the owners comparison
221     // is not effective because in that case only one owner manage the
222     // selection in current selection mode. It is necessary to check the current detected
223     // entity and hilight it only if the detected entity is not the same as
224     // previous detected (IsForcedHilight call)
225     Handle(SelectMgr_EntityOwner) aNewPickedOwner = myMainSel->Picked (aNewDetected);
226     if (aNewPickedOwner == myLastPicked && !aNewPickedOwner->IsForcedHilight())
227     {
228       return myLastPicked->IsSelected()
229            ? AIS_SOD_Selected
230            : AIS_SOD_OnlyOneDetected;
231     }
232  
233     // Previously detected object is unhilighted if it is not selected or hilighted 
234     // with selection color if it is selected. Such highlighting with selection color 
235     // is needed only if myToHilightSelected flag is true. In this case previously detected
236     // object has been already highlighted with myHilightColor during previous MoveTo() 
237     // method call. As result it is necessary to rehighligt it with mySelectionColor.
238     if (!myLastPicked.IsNull() && myLastPicked->HasSelectable())
239     {
240       myMainPM->ClearImmediateDraw();
241       const Handle(AIS_InteractiveObject) aLastPickedAIS = Handle(AIS_InteractiveObject)::DownCast (myLastPicked->Selectable());
242       const Standard_Integer aHiMod = aLastPickedAIS->HasHilightMode() ? aLastPickedAIS->HilightMode() : 0;
243       if (!myLastPicked->IsSelected())
244       {
245         myLastPicked->Unhilight (myMainPM, aHiMod);
246         toUpdateViewer = Standard_True;
247       }
248       else if (myToHilightSelected)
249       {
250         highlightWithColor (aNewPickedOwner, mySelectionColor, theView->Viewer());
251         toUpdateViewer = Standard_True;
252       }
253     }
254
255     // initialize myLastPicked field with currently detected object
256     myLastPicked = aNewPickedOwner;
257     myLastinMain = myLastPicked;
258
259     // highlight detected object if it is not selected or myToHilightSelected flag is true
260     if (myLastPicked->HasSelectable())
261     {
262       if (!myLastPicked->IsSelected() || myToHilightSelected)
263       {
264         highlightWithColor (myLastPicked, myHilightColor, theView->Viewer());
265         toUpdateViewer = Standard_True;
266       }
267
268       aStatus = myLastPicked->IsSelected()
269               ? AIS_SOD_Selected
270               : AIS_SOD_OnlyOneDetected;
271     }
272   }
273   else
274   {
275     // previously detected object is unhilighted if it is not selected or hilighted
276     // with selection color if it is selected
277     aStatus = AIS_SOD_Nothing;
278     if (!myLastPicked.IsNull() && myLastPicked->HasSelectable())
279     {
280       myMainPM->ClearImmediateDraw();
281       const Handle(AIS_InteractiveObject) aLastPickedAIS = Handle(AIS_InteractiveObject)::DownCast (myLastPicked->Selectable());
282       Standard_Integer aHiMod = aLastPickedAIS->HasHilightMode() ? aLastPickedAIS->HilightMode() : 0;
283       if (!myLastPicked->IsSelected())
284       {
285         if (myLastPicked->IsAutoHilight())
286         {
287           myLastPicked->Unhilight (myMainPM, aHiMod);
288         }
289         toUpdateViewer = Standard_True;
290       }
291       else if (myToHilightSelected)
292       {
293         highlightSelected (myLastPicked, mySelectionColor);
294         toUpdateViewer = Standard_True;
295       }
296     }
297
298     myLastinMain.Nullify();
299     myLastPicked.Nullify();
300   }
301
302   if (toUpdateViewer
303    && theToRedrawOnUpdate)
304   {
305     theView->Viewer()->Update();
306   }
307
308   mylastmoveview = theView;
309   return aStatus;
310 }
311
312 //=======================================================================
313 //function : AddSelect
314 //purpose  : 
315 //=======================================================================
316 AIS_StatusOfPick AIS_InteractiveContext::AddSelect (const Handle(Standard_Transient)& theObject)
317 {
318   if (HasOpenedContext())
319   {
320     return myLocalContexts(myCurLocalIndex)->AddSelect (theObject);
321   }
322   mySelection->AddSelect (theObject);
323
324   Standard_Integer aSelNum = NbSelected();
325   return (aSelNum == 0) ? AIS_SOP_NothingSelected
326                         : (aSelNum == 1) ? AIS_SOP_OneSelected
327                                          : AIS_SOP_SeveralSelected;
328 }
329
330 //=======================================================================
331 //function : Select
332 //purpose  : 
333 //=======================================================================
334 AIS_StatusOfPick AIS_InteractiveContext::Select (const Standard_Integer  theXPMin,
335                                                  const Standard_Integer  theYPMin,
336                                                  const Standard_Integer  theXPMax,
337                                                  const Standard_Integer  theYPMax,
338                                                  const Handle(V3d_View)& theView,
339                                                  const Standard_Boolean  toUpdateViewer)
340 {
341   // all objects detected by the selector are taken, previous current objects are emptied,
342   // new objects are put...
343
344   if (HasOpenedContext())
345   {
346     return myLocalContexts(myCurLocalIndex)->Select (theXPMin, theYPMin,
347                                                      theXPMax, theYPMax,
348                                                      theView, toUpdateViewer);
349   }
350
351   ClearSelected (Standard_False);
352
353   Handle(StdSelect_ViewerSelector3d) aSelector;
354
355   if (theView->Viewer() == myMainVwr)
356   {
357     aSelector = myMainSel;
358     myWasLastMain = Standard_True;
359   }
360
361   aSelector->Pick (theXPMin, theYPMin, theXPMax, theYPMax, theView);
362
363   for (aSelector->Init(); aSelector->More(); aSelector->Next())
364   {
365     const Handle(SelectMgr_EntityOwner)& aCurOwner = aSelector->Picked();
366     if (aCurOwner.IsNull() || !aCurOwner->HasSelectable() || !myFilters->IsOk (aCurOwner))
367       continue;
368
369     mySelection->Select (aCurOwner);
370     aCurOwner->State (1);
371   }
372
373   HilightSelected (toUpdateViewer);
374
375   Standard_Integer aSelNum = NbSelected();
376
377   return (aSelNum == 0) ? AIS_SOP_NothingSelected
378                         : (aSelNum == 1) ? AIS_SOP_OneSelected
379                                          : AIS_SOP_SeveralSelected;
380   
381 }
382
383 //=======================================================================
384 //function : Select
385 //purpose  : Selection by polyline
386 //=======================================================================
387 AIS_StatusOfPick AIS_InteractiveContext::Select (const TColgp_Array1OfPnt2d& thePolyline,
388                                                  const Handle(V3d_View)&     theView,
389                                                  const Standard_Boolean      toUpdateViewer)
390 {
391   // all objects detected by the selector are taken, previous current objects are emptied,
392   // new objects are put...
393
394   if (HasOpenedContext())
395   {
396     return myLocalContexts(myCurLocalIndex)->Select (thePolyline, theView, toUpdateViewer);
397   }
398
399   ClearSelected (Standard_False);
400
401   Handle(StdSelect_ViewerSelector3d) aSelector;
402
403   if (theView->Viewer() == myMainVwr)
404   {
405     aSelector = myMainSel;
406     myWasLastMain = Standard_True;
407   }
408
409   aSelector->Pick (thePolyline, theView);
410
411   for (aSelector->Init(); aSelector->More(); aSelector->Next())
412   {
413     const Handle(SelectMgr_EntityOwner) anOwner = aSelector->Picked();
414     if (anOwner.IsNull() || !anOwner->HasSelectable() || !myFilters->IsOk (anOwner))
415       continue;
416
417     mySelection->Select (anOwner);
418     anOwner->State (1);
419   }
420
421   HilightSelected (toUpdateViewer);
422
423   Standard_Integer aSelNum = NbSelected();
424
425   return (aSelNum == 0) ? AIS_SOP_NothingSelected
426                         : (aSelNum == 1) ? AIS_SOP_OneSelected
427                                          : AIS_SOP_SeveralSelected;
428   
429 }
430
431 //=======================================================================
432 //function : Select
433 //purpose  : 
434 //=======================================================================
435 AIS_StatusOfPick AIS_InteractiveContext::Select (const Standard_Boolean toUpdateViewer)
436 {
437   if (HasOpenedContext())
438   {
439     if(myWasLastMain)
440     {
441       return myLocalContexts(myCurLocalIndex)->Select (toUpdateViewer);
442     }
443     else
444     {
445       myLocalContexts(myCurLocalIndex)->SetSelected (Handle(AIS_InteractiveObject)::DownCast (myLastPicked->Selectable()), toUpdateViewer);
446       return AIS_SOP_OneSelected;
447     }
448   }
449
450   myMainPM->ClearImmediateDraw();
451   if (myWasLastMain && !myLastinMain.IsNull())
452   {
453     if(!myLastinMain->IsSelected())
454     {
455       SetSelected (myLastinMain, Standard_False);
456       if(toUpdateViewer)
457       {
458         UpdateCurrentViewer();
459       }
460     }
461   }
462   else
463   {
464     unhighlightSelected (Standard_True);
465
466     mySelection->Select();
467     if (toUpdateViewer && myWasLastMain)
468     {
469         UpdateCurrentViewer();
470     }
471   }
472
473   Standard_Integer aSelNum = NbSelected();
474
475   return (aSelNum == 0) ? AIS_SOP_NothingSelected
476                         : (aSelNum == 1) ? AIS_SOP_OneSelected
477                                          : AIS_SOP_SeveralSelected;
478 }
479
480 //=======================================================================
481 //function : ShiftSelect
482 //purpose  : 
483 //=======================================================================
484 AIS_StatusOfPick AIS_InteractiveContext::ShiftSelect (const Standard_Boolean toUpdateViewer)
485 {
486   if (HasOpenedContext())
487   {
488     if(myWasLastMain)
489     {
490       return myLocalContexts (myCurLocalIndex)->ShiftSelect (toUpdateViewer);
491     }
492     else
493     {
494       myLocalContexts (myCurLocalIndex)->AddOrRemoveSelected (myLastPicked, toUpdateViewer);
495
496       Standard_Integer aSelNum = NbSelected();
497       return (aSelNum == 0) ? AIS_SOP_NothingSelected
498                             : (aSelNum == 1) ? AIS_SOP_OneSelected
499                                              : AIS_SOP_SeveralSelected;
500     }
501   }
502
503   myMainPM->ClearImmediateDraw();
504   if (myWasLastMain && !myLastinMain.IsNull())
505   {
506     AddOrRemoveSelected (myLastinMain, toUpdateViewer);
507   }
508
509   Standard_Integer aSelNum = NbSelected();
510
511   return (aSelNum == 0) ? AIS_SOP_NothingSelected
512                         : (aSelNum == 1) ? AIS_SOP_OneSelected
513                         : AIS_SOP_SeveralSelected;
514 }
515
516 //=======================================================================
517 //function : ShiftSelect
518 //purpose  : 
519 //=======================================================================
520 AIS_StatusOfPick AIS_InteractiveContext::ShiftSelect (const Standard_Integer theXPMin,
521                                                       const Standard_Integer theYPMin,
522                                                       const Standard_Integer theXPMax,
523                                                       const Standard_Integer theYPMax,
524                                                       const Handle(V3d_View)& theView,
525                                                       const Standard_Boolean toUpdateViewer)
526 {
527   if (HasOpenedContext())
528   {
529     return myLocalContexts(myCurLocalIndex)->ShiftSelect (theXPMin, theYPMin, theXPMax, theYPMax,
530                                                           theView, toUpdateViewer);
531   }
532
533   UnhilightSelected (Standard_False);
534
535   Handle(StdSelect_ViewerSelector3d) aSelector;
536   if (theView->Viewer() == myMainVwr)
537   {
538     aSelector = myMainSel;
539     myWasLastMain = Standard_True;
540   }
541   else
542   {
543     return AIS_SOP_NothingSelected;
544   }
545
546   aSelector->Pick (theXPMin, theYPMin, theXPMax, theYPMax, theView);
547   for (aSelector->Init(); aSelector->More(); aSelector->Next())
548   {
549     const Handle(SelectMgr_EntityOwner) anOwner = aSelector->Picked();
550     if (anOwner.IsNull() || !anOwner->HasSelectable() || !myFilters->IsOk (anOwner))
551       continue;
552
553     AIS_SelectStatus aSelStatus = mySelection->Select (anOwner);
554     Standard_Integer aState = (aSelStatus == AIS_SS_Added) ? 1 : 0;
555     anOwner->State (aState);
556   }
557
558   HilightSelected (toUpdateViewer);
559
560   Standard_Integer aSelNum = NbSelected();
561
562   return (aSelNum == 0) ? AIS_SOP_NothingSelected
563                         : (aSelNum == 1) ? AIS_SOP_OneSelected
564                                          : AIS_SOP_SeveralSelected;
565
566 }
567
568 //=======================================================================
569 //function : ShiftSelect
570 //purpose  : 
571 //=======================================================================
572 AIS_StatusOfPick AIS_InteractiveContext::ShiftSelect (const TColgp_Array1OfPnt2d& thePolyline,
573                                                       const Handle(V3d_View)& theView,
574                                                       const Standard_Boolean toUpdateViewer)
575 {
576   if (HasOpenedContext())
577   {
578     return myLocalContexts(myCurLocalIndex)->ShiftSelect (thePolyline, theView, toUpdateViewer);
579   }
580
581   UnhilightSelected (Standard_False);
582
583   Handle(StdSelect_ViewerSelector3d) aSelector;
584
585   if (theView->Viewer() == myMainVwr)
586   {
587     aSelector= myMainSel;
588     myWasLastMain = Standard_True;
589   }
590   else
591   {
592     return AIS_SOP_NothingSelected;
593   }
594
595   aSelector->Pick (thePolyline, theView);
596
597   for (aSelector->Init(); aSelector->More(); aSelector->Next())
598   {
599     const Handle(SelectMgr_EntityOwner) anOwner = aSelector->Picked();
600     if (anOwner.IsNull() || !anOwner->HasSelectable() || !myFilters->IsOk (anOwner))
601       continue;
602
603     AIS_SelectStatus aSelStatus = mySelection->Select (anOwner);
604     Standard_Integer aState = (aSelStatus == AIS_SS_Added) ? 1 : 0;
605     anOwner->State (aState);
606   }
607
608   HilightSelected (toUpdateViewer);
609
610   Standard_Integer aSelNum = NbSelected();
611
612   return (aSelNum == 0) ? AIS_SOP_NothingSelected
613                         : (aSelNum == 1) ? AIS_SOP_OneSelected
614                                          : AIS_SOP_SeveralSelected;
615 }
616
617 //=======================================================================
618 //function : SetCurrentObject
619 //purpose  : OBSOLETE, please use SetSelected() instead
620 //TODO     : Remove in process of local context deletion
621 //=======================================================================
622 void AIS_InteractiveContext::SetCurrentObject (const Handle(AIS_InteractiveObject)& theObject,
623                                                const Standard_Boolean theToUpdateViewer)
624 {
625   if (HasOpenedContext())
626     return;
627
628   SetSelected (theObject, theToUpdateViewer);
629 }
630
631 //=======================================================================
632 //function : AddOrRemoveCurrentObject
633 //purpose  : OBSOLETE, please use AddOrRemoveSelected() instead
634 //TODO     : Remove in process of local context deletion
635 //=======================================================================
636 void AIS_InteractiveContext::AddOrRemoveCurrentObject (const Handle(AIS_InteractiveObject)& theObj,
637                                                        const Standard_Boolean theIsToUpdateViewer)
638 {
639   if (HasOpenedContext())
640     return;
641
642   AddOrRemoveSelected (theObj, theIsToUpdateViewer);
643 }
644 //=======================================================================
645 //function : UpdateCurrent
646 //purpose  : OBSOLETE, please use UpdateSelected() instead
647 //TODO     : Remove in process of local context deletion
648 //=======================================================================
649 void AIS_InteractiveContext::UpdateCurrent()
650 {
651   UpdateSelected();
652 }
653
654 //=======================================================================
655 //function : IsCurrent
656 //purpose  : OBSOLETE, please use IsSelected() instead
657 //TODO     : Remove in process of local context deletion
658 //=======================================================================
659 Standard_Boolean AIS_InteractiveContext::IsCurrent (const Handle(AIS_InteractiveObject)& theObject) const
660 {
661   return IsSelected (theObject);
662 }
663
664 //=======================================================================
665 //function : InitCurrent
666 //purpose  : OBSOLETE, please use InitSelected() instead
667 //TODO     : Remove in process of local context deletion
668 //=======================================================================
669 void AIS_InteractiveContext::InitCurrent()
670 {
671   if (HasOpenedContext())
672     return;
673
674   InitSelected();
675 }
676
677 //=======================================================================
678 //function : MoreCurrent
679 //purpose  : OBSOLETE, please use MoreSelected() instead
680 //TODO     : Remove in process of local context deletion
681 //=======================================================================
682 Standard_Boolean AIS_InteractiveContext::MoreCurrent() const 
683 {
684   return !HasOpenedContext() && MoreSelected();
685 }
686
687 //=======================================================================
688 //function : NextCurrent
689 //purpose  : OBSOLETE, please use NextSelected() instead
690 //TODO     : Remove in process of local context deletion
691 //=======================================================================
692 void AIS_InteractiveContext::NextCurrent()
693 {
694   if (HasOpenedContext())
695     return;
696
697   NextSelected();
698 }
699
700 //=======================================================================
701 //function : Current
702 //purpose  : OBSOLETE, please use SelectedInteractive() instead
703 //TODO     : Remove in process of local context deletion
704 //=======================================================================
705 Handle(AIS_InteractiveObject) AIS_InteractiveContext::Current() const 
706 {
707   return HasOpenedContext() ? NULL : SelectedInteractive();
708 }
709
710 //=======================================================================
711 //function : NbCurrents
712 //purpose  : OBSOLETE, please use NbSelected() instead
713 //TODO     : Remove in process of local context deletion
714 //=======================================================================
715 Standard_Integer AIS_InteractiveContext::NbCurrents()
716 {
717   return HasOpenedContext() ? -1 : NbSelected();
718 }
719
720 //=======================================================================
721 //function : HilightCurrents
722 //purpose  : OBSOLETE, please use HilightSelected() instead
723 //TODO     : Remove in process of local context deletion
724 //=======================================================================
725 void AIS_InteractiveContext::HilightCurrents (const Standard_Boolean theToUpdateViewer)
726 {
727   if (HasOpenedContext())
728     return;
729
730   HilightSelected (theToUpdateViewer);
731 }
732
733 //=======================================================================
734 //function : UnhilightCurrents
735 //purpose  : OBSOLETE, please use UnhilightSelected() instead
736 //TODO     : Remove in process of local context deletion
737 //=======================================================================
738 void AIS_InteractiveContext::UnhilightCurrents (const Standard_Boolean theToUpdateViewer)
739 {
740   if (HasOpenedContext())
741     return;
742
743   UnhilightSelected (theToUpdateViewer);
744 }
745
746 //=======================================================================
747 //function : ClearCurrents
748 //purpose  : OBSOLETE, please use ClearCurrents() instead
749 //TODO     : Remove in process of local context deletion
750 //=======================================================================
751 void AIS_InteractiveContext::ClearCurrents(const Standard_Boolean theToUpdateViewer)
752 {
753   if (HasOpenedContext())
754     return;
755
756   ClearSelected (theToUpdateViewer);
757 }
758
759
760 //=======================================================================
761 //function : HilightSelected
762 //purpose  :
763 //=======================================================================
764 void AIS_InteractiveContext::HilightSelected (const Standard_Boolean theToUpdateViewer)
765 {
766   if (HasOpenedContext())
767   {
768     return myLocalContexts (myCurLocalIndex)->HilightPicked (theToUpdateViewer);
769   }
770
771   // In case of selection without using local context
772   myMainPM->ClearImmediateDraw();
773   AIS_MapOfObjSelectedOwners anObjOwnerMap;
774   for (mySelection->Init(); mySelection->More(); mySelection->Next())
775   {
776     const Handle(SelectMgr_EntityOwner) anOwner =
777       Handle(SelectMgr_EntityOwner)::DownCast (mySelection->Value());
778     if (!anOwner.IsNull() && anOwner->HasSelectable())
779     {
780       const Handle(AIS_InteractiveObject) anObj =
781         Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
782       if (anOwner == anObj->GlobalSelOwner())
783       {
784         Handle(AIS_GlobalStatus)& aState = myObjects.ChangeFind (anObj);
785         aState->SetHilightStatus (Standard_True);
786         aState->SetHilightColor (mySelectionColor);
787       }
788       anOwner->State (1);
789       if (!anOwner->IsAutoHilight())
790       {
791         NCollection_Handle<SelectMgr_SequenceOfOwner> aSeq;
792         if (anObjOwnerMap.Find (anObj, aSeq))
793         {
794           aSeq->Append (anOwner);
795         }
796         else
797         {
798           aSeq = new SelectMgr_SequenceOfOwner();
799           aSeq->Append (anOwner);
800           anObjOwnerMap.Bind (anObj, aSeq);
801         }
802       }
803       else
804       {
805         const Standard_Integer aHiMode = anObj->HasHilightMode() ? anObj->HilightMode() : 0;
806         anOwner->HilightWithColor (myMainPM, mySelectionColor, aHiMode);
807       }
808     }
809   }
810
811   if (!anObjOwnerMap.IsEmpty())
812   {
813     for (AIS_MapOfObjSelectedOwners::Iterator anIter (anObjOwnerMap); anIter.More(); anIter.Next())
814     {
815       anIter.Key()->HilightSelected (myMainPM, *anIter.Value());
816     }
817     anObjOwnerMap.Clear();
818   }
819
820   if (theToUpdateViewer)
821     UpdateCurrentViewer();
822 }
823
824 //=======================================================================
825 //function : UnhilightSelected
826 //purpose  :
827 //=======================================================================
828 void AIS_InteractiveContext::UnhilightSelected (const Standard_Boolean theToUpdateViewer)
829 {
830   if (HasOpenedContext())
831   {
832     return myLocalContexts (myCurLocalIndex)->UnhilightPicked (theToUpdateViewer);
833   }
834
835   for (mySelection->Init(); mySelection->More(); mySelection->Next())
836   {
837     const Handle(SelectMgr_EntityOwner) anOwner =
838       Handle(SelectMgr_EntityOwner)::DownCast (mySelection->Value());
839     if (!anOwner.IsNull() && anOwner->HasSelectable())
840     {
841       const Handle(AIS_InteractiveObject) anObj =
842         Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
843       if (anOwner == anObj->GlobalSelOwner())
844       {
845         myObjects.ChangeFind (anObj)->SetHilightStatus (Standard_False);
846       }
847       anOwner->State (0);
848       const Standard_Integer aHiMode = anObj->HasHilightMode() ? anObj->HasHilightMode() : 0;
849       anOwner->Unhilight (myMainPM, aHiMode);
850     }
851   }
852
853   if (theToUpdateViewer)
854     UpdateCurrentViewer();
855 }
856
857
858 //=======================================================================
859 //function : ClearSelected
860 //purpose  :
861 //=======================================================================
862 void AIS_InteractiveContext::ClearSelected (const Standard_Boolean theToUpdateViewer)
863 {
864   if (HasOpenedContext())
865     return myLocalContexts (myCurLocalIndex)->ClearSelected (theToUpdateViewer);
866
867   if (NbSelected() == 0)
868     return;
869
870   unhighlightSelected();
871
872   mySelection->Select();
873   myMainPM->ClearImmediateDraw();
874
875   if (theToUpdateViewer)
876     UpdateCurrentViewer();
877 }
878
879 //=======================================================================
880 //function : UpdateSelected
881 //purpose  :
882 //=======================================================================
883 void AIS_InteractiveContext::UpdateSelected (const Standard_Boolean theToUpdateViewer)
884 {
885   if (HasOpenedContext())
886   {
887     return myLocalContexts(myCurLocalIndex)->UpdateSelected (theToUpdateViewer);
888   }
889
890   HilightSelected (theToUpdateViewer);
891 }
892
893 //=======================================================================
894 //function : SetSelected
895 //purpose  : Sets the whole object as selected and highlights it with selection color
896 //=======================================================================
897 void AIS_InteractiveContext::SetSelected (const Handle(AIS_InteractiveObject)& theObject,
898                                           const Standard_Boolean theToUpdateViewer)
899 {
900   if(HasOpenedContext())
901   {
902     return myLocalContexts (myCurLocalIndex)->SetSelected (theObject, theToUpdateViewer);
903   }
904
905   if (theObject.IsNull())
906     return;
907   if(!myObjects.IsBound (theObject))
908     Display (theObject, Standard_False);
909   if (theObject->HasSelection (0))
910     return;
911
912   if (NbSelected() == 1 && myObjects (theObject)->IsHilighted())
913   {
914     Quantity_NameOfColor aHiCol;
915     Standard_Boolean hasHiCol = Standard_False;
916     if (IsHilighted (theObject, hasHiCol, aHiCol))
917     {
918       if (hasHiCol && aHiCol!= mySelectionColor)
919       {
920         HilightWithColor (theObject, mySelectionColor, theToUpdateViewer);
921       }
922     }
923     return;
924   }
925
926   for (mySelection->Init(); mySelection->More(); mySelection->Next())
927   {
928     const Handle(SelectMgr_EntityOwner) anOwner =
929       Handle(SelectMgr_EntityOwner)::DownCast (mySelection->Value());
930     if (anOwner.IsNull() || !anOwner->HasSelectable() || !myFilters->IsOk (anOwner))
931       continue;
932
933     Handle(AIS_InteractiveObject) aSelectable =
934       Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
935     Unhilight (aSelectable, Standard_False);
936     anOwner->State (0);
937     if (anOwner == aSelectable->GlobalSelOwner())
938     {
939       myObjects.ChangeFind (aSelectable)->SetHilightStatus (Standard_False);
940     }
941   }
942
943   // added to avoid untimely viewer update...
944   const Handle(SelectMgr_Selection)& aSel = theObject->Selection (0);
945   if (aSel->IsEmpty())
946     return;
947   aSel->Init();
948   Handle(SelectMgr_EntityOwner) anOwner =
949     Handle(SelectMgr_EntityOwner)::DownCast (aSel->Sensitive()->BaseSensitive()->OwnerId());
950   mySelection->ClearAndSelect (anOwner);
951   anOwner->State (1);
952   if (anOwner == theObject->GlobalSelOwner())
953   {
954     Handle(AIS_GlobalStatus)& aState = myObjects.ChangeFind (theObject);
955     aState->SetHilightStatus (Standard_True);
956     aState->SetHilightColor (mySelectionColor);
957   }
958   Quantity_NameOfColor aHiCol;
959   Standard_Boolean hasHiCol = Standard_False;
960   if (IsHilighted (theObject, hasHiCol, aHiCol))
961   {
962     if (hasHiCol && aHiCol!= mySelectionColor)
963     {
964       HilightWithColor (theObject, mySelectionColor, Standard_False);
965     }
966   }
967   else
968   {
969     HilightWithColor (theObject, mySelectionColor, Standard_False);
970   }
971
972   if (theToUpdateViewer)
973     UpdateCurrentViewer();
974 }
975
976 //=======================================================================
977 //function : SetSelected
978 //purpose  : Sets the whole object as selected and highlights it with selection color
979 //=======================================================================
980 void AIS_InteractiveContext::SetSelected (const Handle(SelectMgr_EntityOwner)& theOwner,
981                                           const Standard_Boolean theToUpdateViewer)
982 {
983   if (theOwner.IsNull() || !theOwner->HasSelectable() || !myFilters->IsOk (theOwner))
984     return;
985
986   const Handle(AIS_InteractiveObject) anObject =
987     Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
988
989   if (NbSelected() == 1 && theOwner->IsSelected())
990   {
991     Quantity_NameOfColor aCustomColor;
992     Standard_Boolean isCustomColorSet;
993     if (IsHilighted (theOwner, isCustomColorSet, aCustomColor))
994     {
995       if (isCustomColorSet && aCustomColor != mySelectionColor)
996       {
997         const Standard_Integer aHiMode = anObject->HasHilightMode() ? anObject->HilightMode() : 0;
998         theOwner->HilightWithColor (myMainPM, mySelectionColor, aHiMode);
999       }
1000     }
1001     return;
1002   }
1003
1004   if (!myObjects.IsBound (anObject))
1005     Display (anObject, Standard_False);
1006
1007   unhighlightSelected();
1008
1009   mySelection->ClearAndSelect (theOwner);
1010   theOwner->State (1);
1011   Quantity_NameOfColor aCustomColor;
1012   Standard_Boolean isCustomColorSet;
1013   if (!IsHilighted (theOwner, isCustomColorSet, aCustomColor) || (isCustomColorSet && aCustomColor!= mySelectionColor))
1014   {
1015     highlightSelected (theOwner, mySelectionColor);
1016   }
1017
1018   if (theOwner == anObject->GlobalSelOwner())
1019   {
1020     Handle(AIS_GlobalStatus)& aState = myObjects.ChangeFind (anObject);
1021     aState->SetHilightStatus (Standard_True);
1022     aState->SetHilightColor (mySelectionColor);
1023   }
1024
1025   if (theToUpdateViewer)
1026     UpdateCurrentViewer();
1027 }
1028
1029 //=======================================================================
1030 //function : AddOrRemoveSelected
1031 //purpose  : Adds or removes current object from AIS selection and highlights/unhighlights it.
1032 //           Since this method makes sence only for neutral point selection of a whole object,
1033 //           if 0 selection of the object is empty this method simply does nothing.
1034 //=======================================================================
1035 void AIS_InteractiveContext::AddOrRemoveSelected (const Handle(AIS_InteractiveObject)& theObject,
1036                                                   const Standard_Boolean theToUpdateViewer)
1037 {
1038   if (theObject.IsNull())
1039     return;
1040
1041   if (HasOpenedContext())
1042     return myLocalContexts (myCurLocalIndex)->AddOrRemoveSelected (theObject, theToUpdateViewer);
1043
1044   const Standard_Integer aGlobalSelMode = theObject->GlobalSelectionMode();
1045   if (!myObjects.IsBound (theObject) || !theObject->HasSelection (aGlobalSelMode))
1046     return;
1047
1048   if (!theObject->HasInteractiveContext())
1049     theObject->SetContext (this);
1050
1051   const Handle(SelectMgr_EntityOwner) anOwner = theObject->GlobalSelOwner();
1052
1053   if (anOwner.IsNull() || !anOwner->HasSelectable())
1054     return;
1055
1056   AddOrRemoveSelected (anOwner, theToUpdateViewer);
1057 }
1058 //=======================================================================
1059 //function : AddOrRemoveSelected
1060 //purpose  : 
1061 //=======================================================================
1062
1063 void AIS_InteractiveContext::AddOrRemoveSelected (const TopoDS_Shape& aShap,
1064                                             const Standard_Boolean updateviewer)
1065
1066   if(!HasOpenedContext()) {
1067 #ifdef OCCT_DEBUG
1068     cout<<" Attempt to remove a selected shape with no opened local context"<<endl;
1069 #endif
1070     return;
1071   }
1072   
1073   myLocalContexts(myCurLocalIndex)->AddOrRemoveSelected(aShap,updateviewer);
1074   if(updateviewer) UpdateCurrentViewer();
1075   
1076 }
1077
1078 //=======================================================================
1079 //function : AddOrRemoveSelected
1080 //purpose  : Allows to highlight or unhighlight the owner given depending on
1081 //           its selection status
1082 //=======================================================================
1083 void AIS_InteractiveContext::AddOrRemoveSelected (const Handle(SelectMgr_EntityOwner)& theOwner,
1084                                                   const Standard_Boolean theToUpdateViewer)
1085 {
1086   if (HasOpenedContext())
1087     return myLocalContexts(myCurLocalIndex)->AddOrRemoveSelected (theOwner, theToUpdateViewer);
1088
1089   if (theOwner.IsNull() || !theOwner->HasSelectable())
1090     return;
1091
1092   AIS_SelectStatus aSelStat = mySelection->Select (theOwner);
1093
1094   Standard_Integer aState = aSelStat == AIS_SS_Added ?  1 : 0;
1095   theOwner->State (aState);
1096   const Handle(AIS_InteractiveObject) anObj =
1097     Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
1098   const Standard_Boolean isGlobal = anObj->GlobalSelOwner() == theOwner;
1099   Handle(AIS_GlobalStatus)& aStatus = myObjects.ChangeFind (anObj);
1100   const Standard_Integer aHiMode = anObj->HasHilightMode() ? anObj->HilightMode() : 0;
1101   if (aState == 1)
1102   {
1103     highlightSelected (theOwner, mySelectionColor);
1104     if (isGlobal)
1105     {
1106       aStatus->SetHilightStatus (Standard_True);
1107       aStatus->SetHilightColor (mySelectionColor);
1108     }
1109   }
1110   else
1111   {
1112     if (theOwner->IsAutoHilight())
1113       theOwner->Unhilight (myMainPM, aHiMode);
1114     else
1115       anObj->ClearSelected();
1116     aStatus->SetHilightStatus (Standard_False);
1117     aStatus->SetHilightColor (Quantity_NOC_WHITE);
1118   }
1119
1120   if (theToUpdateViewer)
1121     UpdateCurrentViewer();
1122 }
1123
1124
1125 //=======================================================================
1126 //function : IsSelected
1127 //purpose  :
1128 //=======================================================================
1129 Standard_Boolean AIS_InteractiveContext::IsSelected (const Handle(AIS_InteractiveObject)& theObj) const
1130 {
1131   if(HasOpenedContext())
1132     return myLocalContexts(myCurLocalIndex)->IsSelected (theObj);
1133
1134   if (theObj.IsNull() || !myObjects.IsBound (theObj))
1135     return Standard_False;
1136
1137   const Standard_Integer aGlobalSelMode = theObj->GlobalSelectionMode();
1138   const TColStd_ListOfInteger& anActivatedModes = myObjects (theObj)->SelectionModes();
1139   Standard_Boolean isGlobalModeActivated = Standard_False;
1140   for (TColStd_ListIteratorOfListOfInteger aModeIter (anActivatedModes); aModeIter.More(); aModeIter.Next())
1141   {
1142     if (aModeIter.Value() == aGlobalSelMode)
1143     {
1144       isGlobalModeActivated = Standard_True;
1145       break;
1146     }
1147   }
1148   if (!theObj->HasSelection (aGlobalSelMode) || !isGlobalModeActivated || theObj->GlobalSelOwner().IsNull())
1149     return Standard_False;
1150
1151   return theObj->GlobalSelOwner()->State() == 1;
1152 }
1153
1154 //=======================================================================
1155 //function : IsSelected
1156 //purpose  : Returns true is the owner given is selected
1157 //=======================================================================
1158 Standard_Boolean AIS_InteractiveContext::IsSelected (const Handle(SelectMgr_EntityOwner)& theOwner) const
1159 {
1160   if (HasOpenedContext())
1161     return myLocalContexts(myCurLocalIndex)->IsSelected (theOwner);
1162
1163   if (theOwner.IsNull())
1164     return Standard_False;
1165
1166   return theOwner->IsSelected();
1167 }
1168
1169 //=======================================================================
1170 //function : InitSelected
1171 //purpose  :
1172 //=======================================================================
1173 void AIS_InteractiveContext::InitSelected()
1174 {
1175   if (HasOpenedContext())
1176   {
1177     myLocalContexts (myCurLocalIndex)->InitSelected();
1178     return;
1179   }
1180
1181   mySelection->Init();
1182 }
1183
1184 //=======================================================================
1185 //function : MoreSelected
1186 //purpose  :
1187 //=======================================================================
1188 Standard_Boolean AIS_InteractiveContext::MoreSelected() const
1189 {
1190   if (HasOpenedContext())
1191     return myLocalContexts (myCurLocalIndex)->MoreSelected();
1192
1193   return mySelection->More();
1194 }
1195
1196 //=======================================================================
1197 //function : NextSelected
1198 //purpose  :
1199 //=======================================================================
1200 void AIS_InteractiveContext::NextSelected()
1201 {
1202   if(HasOpenedContext())
1203   {
1204     return myLocalContexts (myCurLocalIndex)->NextSelected();
1205     return;
1206   }
1207
1208   mySelection->Next();
1209 }
1210
1211 //=======================================================================
1212 //function : HasSelectedShape
1213 //purpose  :
1214 //=======================================================================
1215 Standard_Boolean AIS_InteractiveContext::HasSelectedShape() const
1216 {
1217   if(HasOpenedContext())
1218   {
1219     return myLocalContexts(myCurLocalIndex)->HasSelectedShape();
1220   }
1221
1222   Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast (SelectedInteractive());
1223   return !aShape.IsNull();
1224 }
1225
1226 //=======================================================================
1227 //function : SelectedShape
1228 //purpose  :
1229 //=======================================================================
1230 TopoDS_Shape AIS_InteractiveContext::SelectedShape() const
1231 {
1232   if (HasOpenedContext())
1233   {
1234     return myLocalContexts (myCurLocalIndex)->SelectedShape();
1235   }
1236
1237   if (mySelection->Extent() == 0)
1238     return TopoDS_Shape();
1239
1240   const Handle(StdSelect_BRepOwner) anOwner =
1241     Handle(StdSelect_BRepOwner)::DownCast (mySelection->Value());
1242   if (!anOwner->HasSelectable())
1243     return TopoDS_Shape();
1244
1245   return anOwner->Shape().Located (anOwner->Location() * anOwner->Shape().Location());
1246 }
1247
1248 //=======================================================================
1249 //function : SelectedInteractive
1250 //purpose  :
1251 //=======================================================================
1252 Handle(AIS_InteractiveObject) AIS_InteractiveContext::SelectedInteractive() const 
1253 {
1254   if (HasOpenedContext())
1255   {
1256     return myLocalContexts(myCurLocalIndex)->SelectedInteractive();
1257   }
1258
1259   const Handle(SelectMgr_EntityOwner) anOwner =
1260     Handle(SelectMgr_EntityOwner)::DownCast (mySelection->Value());
1261   if (anOwner.IsNull() || !anOwner->HasSelectable())
1262     return NULL;
1263
1264   return Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
1265 }
1266 //=======================================================================
1267 //function : SelectedOwner
1268 //purpose  :
1269 //=======================================================================
1270 Handle(SelectMgr_EntityOwner) AIS_InteractiveContext::SelectedOwner() const
1271 {
1272   if(HasOpenedContext())
1273   {
1274     return myLocalContexts(myCurLocalIndex)->SelectedOwner();
1275   }
1276
1277   return mySelection->Extent() > 0 ?
1278     Handle(SelectMgr_EntityOwner)::DownCast (mySelection->Value()) : NULL;
1279 }
1280
1281 //=======================================================================
1282 //function : EntityOwners
1283 //purpose  : 
1284 //=======================================================================
1285 void AIS_InteractiveContext::EntityOwners(Handle(SelectMgr_IndexedMapOfOwner)& theOwners,
1286                                           const Handle(AIS_InteractiveObject)& theIObj,
1287                                           const Standard_Integer theMode) const 
1288 {
1289   if ( theIObj.IsNull() )
1290       return;
1291
1292   TColStd_ListOfInteger aModes;
1293   if ( theMode == -1 )
1294     ActivatedModes( theIObj, aModes );
1295   else
1296     aModes.Append( theMode );
1297
1298   if (theOwners.IsNull())
1299     theOwners = new SelectMgr_IndexedMapOfOwner();
1300
1301   TColStd_ListIteratorOfListOfInteger anItr( aModes );
1302   for (; anItr.More(); anItr.Next() )
1303   {
1304     int aMode = anItr.Value();
1305     if ( !theIObj->HasSelection( aMode ) )
1306       continue;
1307
1308     Handle(SelectMgr_Selection) aSel = theIObj->Selection(aMode);
1309
1310     for ( aSel->Init(); aSel->More(); aSel->Next() )
1311     {
1312       Handle(SelectBasics_SensitiveEntity) aEntity = aSel->Sensitive()->BaseSensitive();
1313       if ( aEntity.IsNull() )
1314         continue;
1315
1316       Handle(SelectMgr_EntityOwner) aOwner =
1317         Handle(SelectMgr_EntityOwner)::DownCast(aEntity->OwnerId());
1318       if ( !aOwner.IsNull() )
1319         theOwners->Add( aOwner );
1320     }
1321   }
1322 }
1323
1324 //=======================================================================
1325 //function : NbSelected
1326 //purpose  :
1327 //=======================================================================
1328 Standard_Integer AIS_InteractiveContext::NbSelected()
1329 {
1330   Standard_Integer aNbSelected = 0;
1331   for (InitSelected(); MoreSelected(); NextSelected())
1332   {
1333     aNbSelected++;
1334   }
1335
1336   return aNbSelected;
1337 }
1338
1339 //=======================================================================
1340 //function : HasApplicative
1341 //purpose  :
1342 //=======================================================================
1343   Standard_Boolean AIS_InteractiveContext::HasApplicative() const 
1344 {
1345   return SelectedInteractive()->HasOwner();
1346 }
1347
1348 //=======================================================================
1349 //function : Applicative
1350 //purpose  :
1351 //=======================================================================
1352 Handle(Standard_Transient) AIS_InteractiveContext::Applicative() const 
1353 {
1354   return SelectedInteractive()->GetOwner();
1355 }
1356
1357 //==================================================
1358 // Function: HasDetected
1359 // Purpose :
1360 //==================================================
1361 Standard_Boolean AIS_InteractiveContext::HasDetected() const
1362 {
1363   if(HasOpenedContext())
1364     return myLocalContexts(myCurLocalIndex)->HasDetected();
1365
1366   return !myLastPicked.IsNull();
1367 }
1368
1369 //=======================================================================
1370 //function : HasDetectedShape
1371 //purpose  : 
1372 //=======================================================================
1373
1374 Standard_Boolean AIS_InteractiveContext::HasDetectedShape() const 
1375 {
1376   if(HasOpenedContext())
1377     return myLocalContexts(myCurLocalIndex)->HasDetectedShape();
1378   return Standard_False;
1379 }
1380
1381 //=======================================================================
1382 //function : DetectedShape
1383 //purpose  : 
1384 //=======================================================================
1385
1386 const TopoDS_Shape&
1387 AIS_InteractiveContext::DetectedShape() const
1388 {
1389   return myLocalContexts(myCurLocalIndex)->DetectedShape();
1390 }                                           
1391
1392 //=======================================================================
1393 //function : DetectedInteractive
1394 //purpose  :
1395 //=======================================================================
1396 Handle(AIS_InteractiveObject) AIS_InteractiveContext::DetectedInteractive() const
1397 {
1398   if (HasOpenedContext())
1399     return myLocalContexts(myCurLocalIndex)->DetectedInteractive();
1400
1401   return Handle(AIS_InteractiveObject)::DownCast (myLastPicked->Selectable());
1402 }
1403
1404
1405 Standard_Boolean AIS_InteractiveContext::HasNextDetected() const 
1406 {
1407   if(!HasOpenedContext())
1408     return Standard_False; // temporaire
1409   else
1410     return myLocalContexts(myCurLocalIndex)->HasNextDetected();
1411   
1412 }
1413
1414
1415 //=======================================================================
1416 //function : DetectedOwner
1417 //purpose  : 
1418 //=======================================================================
1419 Handle(SelectMgr_EntityOwner) AIS_InteractiveContext::DetectedOwner() const
1420 {
1421   if (HasOpenedContext())
1422     return myLocalContexts(myCurLocalIndex)->DetectedOwner();
1423
1424   return myLastPicked;
1425 }
1426
1427 //=======================================================================
1428 //function : HilightNextDetected
1429 //purpose  :
1430 //=======================================================================
1431 Standard_Integer AIS_InteractiveContext::HilightNextDetected (const Handle(V3d_View)& theView,
1432                                                               const Standard_Boolean  theToRedrawImmediate)
1433 {
1434   return HasOpenedContext()
1435        ? myLocalContexts (myCurLocalIndex)->HilightNextDetected (theView, theToRedrawImmediate)
1436        : 0;
1437     
1438 }
1439
1440 //=======================================================================
1441 //function : HilightNextDetected
1442 //purpose  :
1443 //=======================================================================
1444 Standard_Integer AIS_InteractiveContext::HilightPreviousDetected (const Handle(V3d_View)& theView,
1445                                                                   const Standard_Boolean  theToRedrawImmediate)
1446 {
1447   return HasOpenedContext()
1448        ? myLocalContexts (myCurLocalIndex)->HilightPreviousDetected (theView, theToRedrawImmediate)
1449        : 0;
1450     
1451 }
1452
1453 //=======================================================================
1454 //function : InitDetected
1455 //purpose  :
1456 //=======================================================================
1457 void AIS_InteractiveContext::InitDetected()
1458 {
1459   if (HasOpenedContext())
1460   {
1461     myLocalContexts(myCurLocalIndex)->InitDetected();
1462     return;
1463   }
1464
1465   if(myAISDetectedSeq.Length() != 0)
1466   {
1467     myAISCurDetected = 1;
1468   }
1469 }
1470
1471 //=======================================================================
1472 //function : MoreDetected
1473 //purpose  :
1474 //=======================================================================
1475 Standard_Boolean AIS_InteractiveContext::MoreDetected() const
1476 {
1477   if (HasOpenedContext())
1478   {
1479     return myLocalContexts(myCurLocalIndex)->MoreDetected();
1480   }
1481
1482   return (myAISCurDetected > 0 && myAISCurDetected <= myAISDetectedSeq.Length()) ?
1483           Standard_True : Standard_False;
1484 }
1485
1486 //=======================================================================
1487 //function : NextDetected
1488 //purpose  :
1489 //=======================================================================
1490 void AIS_InteractiveContext::NextDetected()
1491 {
1492   if(HasOpenedContext())
1493   {
1494     myLocalContexts(myCurLocalIndex)->NextDetected();
1495     return;
1496   }
1497
1498   myAISCurDetected++;
1499 }
1500
1501 //=======================================================================
1502 //function : DetectedCurrentShape
1503 //purpose  :
1504 //=======================================================================
1505 const TopoDS_Shape& AIS_InteractiveContext::DetectedCurrentShape() const
1506 {
1507   if (HasOpenedContext())
1508   {
1509     return myLocalContexts(myCurLocalIndex)->DetectedCurrentShape();
1510   }
1511
1512   Handle(AIS_Shape) aCurrentShape = Handle(AIS_Shape)::DownCast (DetectedCurrentObject());
1513
1514   if (aCurrentShape.IsNull())
1515   {
1516     return AIS_myDummyShape;
1517   }
1518
1519   return aCurrentShape->Shape();
1520 }
1521
1522 //=======================================================================
1523 //function : DetectedCurrentObject
1524 //purpose  :
1525 //=======================================================================
1526 Handle(AIS_InteractiveObject) AIS_InteractiveContext::DetectedCurrentObject() const
1527 {
1528   if (HasOpenedContext())
1529   {
1530     return myLocalContexts(myCurLocalIndex)->DetectedCurrentObject();
1531   }
1532
1533   return MoreDetected() ? myAISDetectedSeq(myAISCurDetected) : NULL;
1534 }
1535
1536 //=======================================================================
1537 //function : FirstSelectedObject
1538 //purpose  :
1539 //=======================================================================
1540 Handle(AIS_InteractiveObject) AIS_InteractiveContext::FirstSelectedObject()
1541 {
1542   Handle(AIS_InteractiveObject) anObject;
1543
1544   if (HasOpenedContext())
1545     return anObject;
1546
1547   InitSelected();
1548   if (MoreSelected())
1549   {
1550     return SelectedInteractive();
1551   }
1552   return anObject;
1553 }
1554
1555 //=======================================================================
1556 //function : RedrawImmediate
1557 //purpose  : Redisplays immediate strucures of the viewer given according to their visibility
1558 //=======================================================================
1559 void AIS_InteractiveContext::RedrawImmediate (const Handle(V3d_Viewer)& theViewer)
1560 {
1561   myMainPM->RedrawImmediate (theViewer);
1562 }