CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FWGUIValidatingTextEntry.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Core
4 // Class : FWGUIValidatingTextEntry
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: Chris Jones
10 // Created: Fri Aug 22 18:13:39 EDT 2008
11 // $Id: FWGUIValidatingTextEntry.cc,v 1.9 2011/07/20 20:17:54 amraktad Exp $
12 //
13 
14 // system include files
15 #include <iostream>
16 #include "TGComboBox.h"
17 #include "KeySymbols.h"
18 #include "TTimer.h"
19 #include "TGWindow.h"
20 
21 // user include files
24 
25 
26 //
27 // constants, enums and typedefs
28 //
29 
30 //
31 // static data member definitions
32 //
33 
34 //
35 // constructors and destructor
36 //
37 FWGUIValidatingTextEntry::FWGUIValidatingTextEntry(const TGWindow *parent, const char *text, Int_t id ) :
38  TGTextEntry(parent,text,id),
39  m_popup(0),
40  m_list(0),
41  m_validator(0),
42  m_listHeight(100)
43 {
44  m_popup = new TGComboBoxPopup(fClient->GetDefaultRoot(), 100, 100, kVerticalFrame);
45  m_list = new TGListBox(m_popup, 1 /*widget id*/, kChildFrame);
46  m_list->Resize(100,m_listHeight);
47  m_list->Associate(this);
48  m_list->GetScrollBar()->GrabPointer(kFALSE);
49  m_popup->AddFrame(m_list, new TGLayoutHints(kLHintsExpandX| kLHintsExpandY));
50  m_popup->MapSubwindows();
51  m_popup->Resize(m_popup->GetDefaultSize());
52  m_list->GetContainer()->AddInput(kButtonPressMask | kButtonReleaseMask | kPointerMotionMask);
53  m_list->SetEditDisabled(kEditDisable);
54  m_list->GetContainer()->Connect("KeyPressed(TGFrame*,UInt_t,UInt_t)",
55  "FWGUIValidatingTextEntry", this,
56  "keyPressedInPopup(TGFrame*,UInt_t,UInt_t)");
57  m_list->GetContainer()->SetEditDisabled(kEditDisable);
58  Connect("TabPressed()", "FWGUIValidatingTextEntry", this, "showOptions()");
59 
60 }
61 
62 // FWGUIValidatingTextEntry::FWGUIValidatingTextEntry(const FWGUIValidatingTextEntry& rhs)
63 // {
64 // // do actual copying here;
65 // }
66 
68 {
69 }
70 
71 //
72 // assignment operators
73 //
74 // const FWGUIValidatingTextEntry& FWGUIValidatingTextEntry::operator=(const FWGUIValidatingTextEntry& rhs)
75 // {
76 // //An exception safe implementation is
77 // FWGUIValidatingTextEntry temp(rhs);
78 // swap(rhs);
79 //
80 // return *this;
81 // }
82 
83 //
84 // member functions
85 //
86 void
88 {
89  m_validator = iValidator;
90 }
91 
92 
93 Bool_t
94 FWGUIValidatingTextEntry::ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2)
95 {
96  //STOLEN FROM TGComboBox.cxx
97  switch (GET_MSG(msg)) {
98  case kC_COMMAND:
99  switch (GET_SUBMSG(msg)) {
100  case kCM_LISTBOX:
101  RequestFocus();
102  insertTextOption(m_options[m_list->GetSelected()].second);
103  hideOptions();
104  break;
105  }
106  break;
107 
108  default:
109  break;
110  }
111  return kTRUE;
112 
113 }
114 
115 void
117 {
118  switch(keysym) {
119  case kKey_Tab:
120  case kKey_Escape:
121  RequestFocus();
122  hideOptions();
123  break;
124  case kKey_Return:
125  RequestFocus();
126  //NOTE: If chosen from the keyboard, m_list->GetSelected() does not work, however
127  // m_list->GetSelectedEntries does work
128 
129  //AMT NOTE: TGListEntry does not select entry on key return event, it has to be selected here.
130  // Code stolen from TGComboBox::KeyPressed
131 
132  const TGLBEntry* entry = dynamic_cast<TGLBEntry*> (f);
133  if (entry)
134  {
135  insertTextOption(m_options[entry->EntryId()].second);
136  m_list->Selected(entry->EntryId());
137  }
138  hideOptions();
139  break;
140  }
141 }
142 
143 namespace {
144  class ChangeFocusTimer : public TTimer {
145 public:
146  ChangeFocusTimer(TGWindow* iWindow) :
147  TTimer(100),
148  m_window(iWindow) {
149  }
150  virtual Bool_t Notify() {
151  TurnOff();
152  m_window->RequestFocus();
153  return kTRUE;
154  }
155 private:
156  TGWindow* m_window;
157  };
158 }
159 
160 
161 void
163 
164  if(0!=m_validator) {
165  const char* text = GetText();
166  std::string subText(text,text+GetCursorPosition());
167  //std::cout <<subText<<std::endl;
168 
169  typedef std::vector<std::pair<boost::shared_ptr<std::string>, std::string> > Options;
170  m_validator->fillOptions(text, text+GetCursorPosition(), m_options);
171  if(m_options.empty()) { return;}
172  if(m_options.size()==1) {
173  insertTextOption(m_options.front().second);
174  return;
175  }
176  m_list->RemoveAll();
177  int index = 0;
178  for(Options::iterator it = m_options.begin(), itEnd = m_options.end();
179  it != itEnd; ++it,++index) {
180  m_list->AddEntry(it->first->c_str(),index);
181  }
182  {
183  unsigned int h = m_list->GetNumberOfEntries()*
184  m_list->GetItemVsize();
185  if(h && (h<m_listHeight)) {
186  m_list->Resize(m_list->GetWidth(),h);
187  } else {
188  m_list->Resize(m_list->GetWidth(),m_listHeight);
189  }
190  }
191  m_list->Select(0,kTRUE);
192 
193  int ax,ay;
194  Window_t wdummy;
195  gVirtualX->TranslateCoordinates(GetId(), m_popup->GetParent()->GetId(),
196  0, GetHeight(), ax, ay, wdummy);
197 
198  //Wait to change focus for when the popup has already openned
199  std::auto_ptr<TTimer> timer( new ChangeFocusTimer(m_list->GetContainer()) );
200  timer->TurnOn();
201  //NOTE: this call has its own internal GUI event loop and will not return
202  // until the popup has been shut down
203  m_popup->PlacePopup(ax, ay,
204  GetWidth()-2, m_popup->GetDefaultHeight());
205  }
206 }
207 
208 void
210  m_popup->EndPopup();
211  fClient->NeedRedraw(this);
212 }
213 
214 void
216 {
217  long pos = GetCursorPosition();
218  InsertText(iOption.c_str(), pos);
219  SetCursorPosition(pos + iOption.size());
220 }
221 
222 //
223 // const member functions
224 //
225 
226 //
227 // static member functions
228 //
list parent
Definition: dbtoconf.py:74
std::vector< std::pair< boost::shared_ptr< std::string >, std::string > > m_options
virtual void fillOptions(const char *iBegin, const char *iEnd, std::vector< std::pair< boost::shared_ptr< std::string >, std::string > > &oOptions) const =0
void insertTextOption(const std::string &)
FWGUIValidatingTextEntry(const TGWindow *parent=0, const char *text=0, Int_t id=-1)
std::pair< std::string, MonitorElement * > entry
Definition: ME_MAP.h:8
void setValidator(FWValidatorBase *)
double f[11][100]
tuple text
Definition: runonSM.py:42
unsigned int UInt_t
Definition: FUTypes.h:12
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
std::vector< boost::shared_ptr< fireworks::OptionNode > > Options
virtual Bool_t ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2)
void keyPressedInPopup(TGFrame *, UInt_t keysym, UInt_t mask)