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