// @(#)root/eve:$Id$
// Author: Matevz Tadel 2007

/*************************************************************************
 * Copyright (C) 1995-2007, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#include "TEveSecondarySelectable.h"
#include "TEveElement.h"

#include "TGLSelectRecord.h"

//______________________________________________________________________________
//
// Semi-abstract interface for classes supporting secondary-selection.
//
// Element class that inherits from this, should also implement the
// following virtuals from TEveElement:
//    virtual void UnSelected();
//    virtual void UnHighlighted();
// and clear corresponding selection-set from there.
//
// To support tooltips for sub-elements, implement:
//    virtual TString TEveElement::GetHighlightTooltip();
// and return tooltip for the entry in the fHighlightedSet.
// There should always be a single entry there.
// See TEveDigitSet for an example.


ClassImp(TEveSecondarySelectable);

//______________________________________________________________________________
TEveSecondarySelectable::TEveSecondarySelectable() :
   fAlwaysSecSelect(kFALSE)
{
   // Constructor.
}

//______________________________________________________________________________
void TEveSecondarySelectable::ProcessGLSelection(TGLSelectRecord& rec)
{
   // Process secondary GL selection and populate selected set accordingly.

   if (rec.GetHighlight())
      ProcessGLSelectionInternal(rec, fHighlightedSet);
   else
      ProcessGLSelectionInternal(rec, fSelectedSet);
}

//______________________________________________________________________________
void TEveSecondarySelectable::ProcessGLSelectionInternal(TGLSelectRecord& rec,
                                                         SelectionSet_t& sset)
{
   // Process secondary GL selection and populate given set accordingly.

   Int_t id = (rec.GetN() > 1) ? (Int_t) rec.GetItem(1) : -1;

   if (sset.empty())
   {
      if (id >= 0)
      {
         sset.insert(id);
         rec.SetSecSelResult(TGLSelectRecord::kEnteringSelection);
      }  
   }
   else
   {
      if (id >= 0)
      {
         if (rec.GetMultiple())
         {
            if (sset.find(id) == sset.end())
            {
               sset.insert(id);
               rec.SetSecSelResult(TGLSelectRecord::kModifyingInternalSelection);
            }
            else
            {
               sset.erase(id);
               if (sset.empty())
                  rec.SetSecSelResult(TGLSelectRecord::kLeavingSelection);
               else
                  rec.SetSecSelResult(TGLSelectRecord::kModifyingInternalSelection);
           }
         }
         else
         {
            if (sset.size() != 1 || sset.find(id) == sset.end())
            {
               sset.clear();
               sset.insert(id);
               rec.SetSecSelResult(TGLSelectRecord::kModifyingInternalSelection);
            }
         }
      }
      else
      {
         if (!rec.GetMultiple())
         {
            sset.clear();
            rec.SetSecSelResult(TGLSelectRecord::kLeavingSelection);
         }
      }
   }

   if (rec.GetSecSelResult() != TGLSelectRecord::kNone)
   {
      dynamic_cast<TEveElement*>(this)->StampColorSelection();
   }
}
 TEveSecondarySelectable.cxx:1
 TEveSecondarySelectable.cxx:2
 TEveSecondarySelectable.cxx:3
 TEveSecondarySelectable.cxx:4
 TEveSecondarySelectable.cxx:5
 TEveSecondarySelectable.cxx:6
 TEveSecondarySelectable.cxx:7
 TEveSecondarySelectable.cxx:8
 TEveSecondarySelectable.cxx:9
 TEveSecondarySelectable.cxx:10
 TEveSecondarySelectable.cxx:11
 TEveSecondarySelectable.cxx:12
 TEveSecondarySelectable.cxx:13
 TEveSecondarySelectable.cxx:14
 TEveSecondarySelectable.cxx:15
 TEveSecondarySelectable.cxx:16
 TEveSecondarySelectable.cxx:17
 TEveSecondarySelectable.cxx:18
 TEveSecondarySelectable.cxx:19
 TEveSecondarySelectable.cxx:20
 TEveSecondarySelectable.cxx:21
 TEveSecondarySelectable.cxx:22
 TEveSecondarySelectable.cxx:23
 TEveSecondarySelectable.cxx:24
 TEveSecondarySelectable.cxx:25
 TEveSecondarySelectable.cxx:26
 TEveSecondarySelectable.cxx:27
 TEveSecondarySelectable.cxx:28
 TEveSecondarySelectable.cxx:29
 TEveSecondarySelectable.cxx:30
 TEveSecondarySelectable.cxx:31
 TEveSecondarySelectable.cxx:32
 TEveSecondarySelectable.cxx:33
 TEveSecondarySelectable.cxx:34
 TEveSecondarySelectable.cxx:35
 TEveSecondarySelectable.cxx:36
 TEveSecondarySelectable.cxx:37
 TEveSecondarySelectable.cxx:38
 TEveSecondarySelectable.cxx:39
 TEveSecondarySelectable.cxx:40
 TEveSecondarySelectable.cxx:41
 TEveSecondarySelectable.cxx:42
 TEveSecondarySelectable.cxx:43
 TEveSecondarySelectable.cxx:44
 TEveSecondarySelectable.cxx:45
 TEveSecondarySelectable.cxx:46
 TEveSecondarySelectable.cxx:47
 TEveSecondarySelectable.cxx:48
 TEveSecondarySelectable.cxx:49
 TEveSecondarySelectable.cxx:50
 TEveSecondarySelectable.cxx:51
 TEveSecondarySelectable.cxx:52
 TEveSecondarySelectable.cxx:53
 TEveSecondarySelectable.cxx:54
 TEveSecondarySelectable.cxx:55
 TEveSecondarySelectable.cxx:56
 TEveSecondarySelectable.cxx:57
 TEveSecondarySelectable.cxx:58
 TEveSecondarySelectable.cxx:59
 TEveSecondarySelectable.cxx:60
 TEveSecondarySelectable.cxx:61
 TEveSecondarySelectable.cxx:62
 TEveSecondarySelectable.cxx:63
 TEveSecondarySelectable.cxx:64
 TEveSecondarySelectable.cxx:65
 TEveSecondarySelectable.cxx:66
 TEveSecondarySelectable.cxx:67
 TEveSecondarySelectable.cxx:68
 TEveSecondarySelectable.cxx:69
 TEveSecondarySelectable.cxx:70
 TEveSecondarySelectable.cxx:71
 TEveSecondarySelectable.cxx:72
 TEveSecondarySelectable.cxx:73
 TEveSecondarySelectable.cxx:74
 TEveSecondarySelectable.cxx:75
 TEveSecondarySelectable.cxx:76
 TEveSecondarySelectable.cxx:77
 TEveSecondarySelectable.cxx:78
 TEveSecondarySelectable.cxx:79
 TEveSecondarySelectable.cxx:80
 TEveSecondarySelectable.cxx:81
 TEveSecondarySelectable.cxx:82
 TEveSecondarySelectable.cxx:83
 TEveSecondarySelectable.cxx:84
 TEveSecondarySelectable.cxx:85
 TEveSecondarySelectable.cxx:86
 TEveSecondarySelectable.cxx:87
 TEveSecondarySelectable.cxx:88
 TEveSecondarySelectable.cxx:89
 TEveSecondarySelectable.cxx:90
 TEveSecondarySelectable.cxx:91
 TEveSecondarySelectable.cxx:92
 TEveSecondarySelectable.cxx:93
 TEveSecondarySelectable.cxx:94
 TEveSecondarySelectable.cxx:95
 TEveSecondarySelectable.cxx:96
 TEveSecondarySelectable.cxx:97
 TEveSecondarySelectable.cxx:98
 TEveSecondarySelectable.cxx:99
 TEveSecondarySelectable.cxx:100
 TEveSecondarySelectable.cxx:101
 TEveSecondarySelectable.cxx:102
 TEveSecondarySelectable.cxx:103
 TEveSecondarySelectable.cxx:104
 TEveSecondarySelectable.cxx:105
 TEveSecondarySelectable.cxx:106
 TEveSecondarySelectable.cxx:107
 TEveSecondarySelectable.cxx:108
 TEveSecondarySelectable.cxx:109
 TEveSecondarySelectable.cxx:110
 TEveSecondarySelectable.cxx:111
 TEveSecondarySelectable.cxx:112
 TEveSecondarySelectable.cxx:113
 TEveSecondarySelectable.cxx:114