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