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