CMS 3D CMS Logo

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