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