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.6 2008/12/04 20:38:26 chrjones 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_validator(0)
40 {
41  m_popup = new TGComboBoxPopup(fClient->GetDefaultRoot(), 100, 100, kVerticalFrame);
42  m_list = new TGListBox(m_popup, 1 /*widget id*/, kChildFrame);
43  m_list->Resize(100,100);
44  m_list->Associate(this);
45  m_list->GetScrollBar()->GrabPointer(kFALSE);
46  m_popup->AddFrame(m_list, new TGLayoutHints(kLHintsExpandX| kLHintsExpandY));
47  m_popup->MapSubwindows();
48  m_popup->Resize(m_popup->GetDefaultSize());
49  m_list->GetContainer()->AddInput(kButtonPressMask | kButtonReleaseMask | kPointerMotionMask);
50  m_list->SetEditDisabled(kEditDisable);
51  m_list->GetContainer()->Connect("KeyPressed(TGFrame*,UInt_t,UInt_t)",
52  "FWGUIValidatingTextEntry", this,
53  "keyPressedInPopup(TGFrame*,UInt_t,UInt_t)");
54  m_list->GetContainer()->SetEditDisabled(kEditDisable);
55  Connect("TabPressed()", "FWGUIValidatingTextEntry", this, "showOptions()");
56 
57 }
58 
59 // FWGUIValidatingTextEntry::FWGUIValidatingTextEntry(const FWGUIValidatingTextEntry& rhs)
60 // {
61 // // do actual copying here;
62 // }
63 
65 {
66 }
67 
68 //
69 // assignment operators
70 //
71 // const FWGUIValidatingTextEntry& FWGUIValidatingTextEntry::operator=(const FWGUIValidatingTextEntry& rhs)
72 // {
73 // //An exception safe implementation is
74 // FWGUIValidatingTextEntry temp(rhs);
75 // swap(rhs);
76 //
77 // return *this;
78 // }
79 
80 //
81 // member functions
82 //
83 void
85 {
86  m_validator = iValidator;
87 }
88 
89 
90 Bool_t
91 FWGUIValidatingTextEntry::ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2)
92 {
93  //STOLEN FROM TGComboBox.cxx
94  switch (GET_MSG(msg)) {
95  case kC_COMMAND:
96  switch (GET_SUBMSG(msg)) {
97  case kCM_LISTBOX:
98  RequestFocus();
99  insertTextOption(m_options[m_list->GetSelected()].second);
100  hideOptions();
101  break;
102  }
103  break;
104 
105  default:
106  break;
107  }
108  return kTRUE;
109 
110 }
111 
112 void
114 {
115  switch(keysym) {
116  case kKey_Tab:
117  case kKey_Escape:
118  RequestFocus();
119  hideOptions();
120  break;
121  case kKey_Return:
122  RequestFocus();
123  //NOTE: If chosen from the keyboard, m_list->GetSelected() does not work, however
124  // m_list->GetSelectedEntries does work
125  TList selected;
126  m_list->GetSelectedEntries(&selected);
127  assert(selected.GetEntries() == 1);
128  const TGLBEntry* entry = dynamic_cast<TGLBEntry*> (selected.First());
129  assert(0!=entry);
130  insertTextOption(m_options[entry->EntryId()].second);
131  hideOptions();
132  break;
133  }
134 }
135 
136 namespace {
137  class ChangeFocusTimer : public TTimer {
138 public:
139  ChangeFocusTimer(TGWindow* iWindow) :
140  TTimer(100),
141  m_window(iWindow) {
142  }
143  virtual Bool_t Notify() {
144  TurnOff();
145  m_window->RequestFocus();
146  return kTRUE;
147  }
148 private:
149  TGWindow* m_window;
150  };
151 }
152 
153 
154 void
156 
157  if(0!=m_validator) {
158  const char* text = GetText();
159  std::string subText(text,text+GetCursorPosition());
160  //std::cout <<subText<<std::endl;
161 
162  typedef std::vector<std::pair<boost::shared_ptr<std::string>, std::string> > Options;
163  m_validator->fillOptions(text, text+GetCursorPosition(), m_options);
164  if(m_options.empty()) { return;}
165  if(m_options.size()==1) {
166  insertTextOption(m_options.front().second);
167  return;
168  }
169  m_list->RemoveAll();
170  int index = 0;
171  for(Options::iterator it = m_options.begin(), itEnd = m_options.end();
172  it != itEnd; ++it,++index) {
173  m_list->AddEntry(it->first->c_str(),index);
174  }
175  {
176  unsigned int h = m_list->GetNumberOfEntries()*
177  m_list->GetItemVsize();
178  if(h && (h<100)) {
179  m_list->Resize(m_list->GetWidth(),h);
180  } else {
181  m_list->Resize(m_list->GetWidth(),100);
182  }
183  }
184  m_list->Select(0,kTRUE);
185 
186  int ax,ay;
187  Window_t wdummy;
188  gVirtualX->TranslateCoordinates(GetId(), m_popup->GetParent()->GetId(),
189  0, GetHeight(), ax, ay, wdummy);
190 
191  //Wait to change focus for when the popup has already openned
192  std::auto_ptr<TTimer> timer( new ChangeFocusTimer(m_list->GetContainer()) );
193  timer->TurnOn();
194  //NOTE: this call has its own internal GUI event loop and will not return
195  // until the popup has been shut down
196  m_popup->PlacePopup(ax, ay,
197  GetWidth()-2, m_popup->GetDefaultHeight());
198  }
199 }
200 
201 void
203  m_popup->EndPopup();
204  fClient->NeedRedraw(this);
205 }
206 
207 void
209 {
210  long pos = GetCursorPosition();
211  InsertText(iOption.c_str(), pos);
212  SetCursorPosition(pos + iOption.size());
213 
214 }
215 
216 //
217 // const member functions
218 //
219 
220 //
221 // static member functions
222 //
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 *)
tuple text
Definition: runonSM.py:42
unsigned int UInt_t
Definition: FUTypes.h:12
std::vector< boost::shared_ptr< fireworks::OptionNode > > Options
virtual Bool_t ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2)
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
void keyPressedInPopup(TGFrame *, UInt_t keysym, UInt_t mask)