Diana Software
QGDropDownBox.cc
Go to the documentation of this file.
1 #include "QGDropDownBox.hh"
2 #include "QGTextEntry.hh"
3 
5 
6 using std::string;
7 using std::vector;
8 
9 QGDropDownBox::QGDropDownBox(const TGWindow *p) :
10 TGComboBox(p)
11 {
12  EnableTextInput(kTRUE);
13  GetTextEntry()->Connect("ProcessedEvent(Event_t*)", "QGDropDownBox", this,
14  "QGHandleEvent(Event_t*)");
15 }
16 
18 {
19 }
20 
21 void QGDropDownBox::AddEntryS(const string& entry)
22 {
23  TGComboBox::AddEntry(entry.c_str(), GetNumberOfEntries());
24  fEntries.push_back(entry);
25 }
26 
28 {
29  fTextEntry->Connect("TabPressed()", "QGDropDownBox", this, "TabComplete()");
30 }
31 
32 vector<string> QGDropDownBox::FindMatches(const string& s)
33 {
34  vector<string> matches;
35  vector<string>::const_iterator iter;
36  for (iter = fEntries.begin(); iter != fEntries.end(); ++iter) {
37  if ((*iter).find(s) == 0) matches.push_back(*iter);
38  }
39  return matches;
40 }
41 
42 string QGDropDownBox::GetCommonText(const vector<string>& v)
43 {
44  string commonText = v[0];
45  string::size_type N = commonText.size();
46  for (unsigned int i = N; i != 0; --i) {
47  bool common = true;
48  commonText = commonText.substr(0, i);
49  vector<string>::const_iterator iter;
50  for (iter = v.begin(); iter != v.end(); ++iter) {
51  if (iter->find(commonText) != 0) common = false;
52  }
53  if (common) break;
54  }
55  return commonText;
56 }
57 
59 {
60  if (fReturnTarget) {
61  fReturnTarget->SetFocus();
62  }
63 }
64 
65 void QGDropDownBox::QGHandleEvent(Event_t *event)
66 {
67  if (event->fType == kFocusOut) {
68  GetTextEntry()->Deselect();
69  }
70 }
71 
72 void QGDropDownBox::SetReturnTarget(TGTextEntry *textBox)
73 {
74  if (textBox) {
75  fReturnTarget = textBox;
76  GetTextEntry()->Disconnect("ReturnPressed()");
77  GetTextEntry()->Connect("ReturnPressed()", "QGDropDownBox", this,
78  "HandleReturnPressed()");
79  }
80 
81  if ( QGTextEntry *returnTarget
82  = dynamic_cast<QGTextEntry*>(fReturnTarget) ) {
83  returnTarget->SetShiftTabTarget( GetTextEntry() );
84  }
85 }
86 
88 {
89  if (fTextEntry->GetBuffer()->GetTextLength() > 0) {
90  string currentText = fTextEntry->GetText();
91 
92  // Parse for last word
93  string separators = " ()*/+-&|=!";
94  string::size_type pos = currentText.find_last_of(separators);
95  if (pos != string::npos && pos != currentText.size() - 1) {
96  currentText = currentText.substr(pos+1);
97  }
98 
99  vector<string> matches = FindMatches(currentText);
100  if (matches.size() > 0) {
101  string commonText = GetCommonText(matches);
102  fTextEntry->AppendText(commonText.substr(currentText.size()).c_str());
103  }
104  }
105 }
int N
Definition: CheckOF.C:24
ClassImp(QObject)
Drop down box with tab-complete.
void QGHandleEvent(Event_t *event)
Handle event.
TGTextEntry * fReturnTarget
Text box to change to when return is pressed.
void SetReturnTarget(TGTextEntry *textBox)
Set return target.
QGDropDownBox(const TGWindow *p=0)
Constructor.
Definition: QGDropDownBox.cc:9
std::vector< std::string > FindMatches(const std::string &s)
Find a match to a string.
virtual ~QGDropDownBox()
Destructor.
void AddEntryS(const std::string &entry)
Add entry.
std::vector< std::string > fEntries
Collection of entries.
void TabComplete()
Tab complete.
std::string GetCommonText(const std::vector< std::string > &v)
Get common text.
void EnableTabCompletion()
Enable tab completion.
void HandleReturnPressed()
Handle tab pressed.
Extension of ROOT's basic text box TGTextEntry.
Definition: QGTextEntry.hh:13