CMS 3D CMS Logo

CSGAction.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Core
4 // Class : CSGAction
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: Chris Jones
10 // Created: Thu May 29 20:58:11 CDT 2008
11 //
12 
13 // system include files
14 #include <TString.h>
15 #include <TGResourcePool.h>
16 #include <TQObject.h>
17 #include <KeySymbols.h>
18 #include <TGMenu.h>
19 #include <TVirtualX.h>
20 
21 // user include files
26 
27 //
28 // constants, enums and typedefs
29 //
30 
31 //
32 // static data member definitions
33 //
34 
35 //
36 // constructors and destructor
37 //
38 CSGAction::CSGAction(CSGActionSupervisor* supervisor, const char* name) : m_connector(nullptr) {
39  m_enabled = true;
40  m_globalEnabled = true;
41  m_supervisor = supervisor;
42  m_name = name;
43  m_toolTip = "";
44  m_menu = nullptr;
45  m_toolBar = nullptr;
46  m_tools = nullptr;
50  m_keycode = 0;
51  m_modcode = 0;
52  m_windowID = -1;
53 }
54 // CSGAction::CSGAction(const CSGAction& rhs)
55 // {
56 // // do actual copying here;
57 // }
58 
60  delete m_connector;
61  //Don't delete GUI parts since they are owned by their GUI parent
62 }
63 
64 //
65 // assignment operators
66 //
67 // const CSGAction& CSGAction::operator=(const CSGAction& rhs)
68 // {
69 // //An exception safe implementation is
70 // CSGAction temp(rhs);
71 // swap(rhs);
72 //
73 // return *this;
74 // }
75 
76 //
77 // member functions
78 //
79 const std::string& CSGAction::getName() const { return m_name; }
80 
81 const std::string& CSGAction::getToolTip() const { return m_toolTip; }
82 
83 TString CSGAction::getSCCombo() const { return m_scCombo; }
84 
86  // Does not update menu yet
87  m_name = name;
88 
89  for (std::vector<TGButton*>::iterator it = m_buttons.begin(), itEnd = m_buttons.end(); it != itEnd; ++it) {
90  TGTextButton* tb = dynamic_cast<TGTextButton*>(*it);
91  if (tb) {
92  (tb)->SetText(name.c_str());
93  gClient->NeedRedraw(tb);
94  }
95  }
96 }
97 
99  if (m_menu) {
100  m_menu->GetEntry(m_entry)->GetLabel()->SetString(label.c_str());
101  }
102 }
103 
105  m_toolTip = tip;
106  for (std::vector<TGButton*>::iterator it = m_buttons.begin(), itEnd = m_buttons.end(); it != itEnd; ++it) {
107  (*it)->SetToolTipText(tip.c_str(), m_supervisor->getToolTipDelay());
108  }
109  if (m_tools != nullptr)
110  m_tools->fTipText = tip.c_str();
111 }
112 
114  TGCompositeFrame* p, TGLayoutHints* l, Int_t id, GContext_t norm, FontStruct_t font, UInt_t option) {
115  TGTextButton* textButton = new TGTextButton(p, m_name.c_str(), id, norm, font, option);
116  if (!m_toolTip.empty())
117  textButton->SetToolTipText(m_toolTip.c_str(), m_supervisor->getToolTipDelay());
118  p->AddFrame(textButton, l);
119  TQObject::Connect(textButton, "Clicked()", "CSGAction", this, "activate()");
120  m_buttons.push_back(textButton);
121  if (!isEnabled()) {
122  textButton->SetEnabled(kFALSE);
123  }
124 }
125 
127  TGCompositeFrame* p, TGLayoutHints* l, Bool_t state, Int_t id, GContext_t norm, FontStruct_t font) {
128  TGCheckButton* checkButton = new TGCheckButton(p, m_name.c_str(), id, norm, font);
129  if (!m_toolTip.empty())
130  checkButton->SetToolTipText(m_toolTip.c_str(), m_supervisor->getToolTipDelay());
131  p->AddFrame(checkButton, l);
132 
133  if (state)
134  checkButton->SetState(kButtonDown, false);
135  TQObject::Connect(checkButton, "Clicked()", "CSGAction", this, "activate()");
136  m_buttons.push_back(checkButton);
137  if (!isEnabled()) {
138  checkButton->SetEnabled(kFALSE);
139  }
140 }
141 
143  TGCompositeFrame* p, const TGPicture* pic, TGLayoutHints* l, Int_t id, GContext_t norm, UInt_t option) {
144  TGPictureButton* picButton = new TGPictureButton(p, pic, id, norm, option);
145  if (!m_toolTip.empty())
146  picButton->SetToolTipText(m_toolTip.c_str(), m_supervisor->getToolTipDelay());
147  p->AddFrame(picButton, l);
148  TQObject::Connect(picButton, "Clicked()", "CSGAction", this, "activate()");
149  m_buttons.push_back(picButton);
150  if (!isEnabled()) {
151  picButton->SetEnabled(kFALSE);
152  }
153 }
154 
156  const TGPicture* upPic,
157  const TGPicture* downPic,
158  const TGPicture* disabledPic,
159  TGLayoutHints* l,
160  Int_t id,
161  GContext_t norm,
162  UInt_t option) {
163  FWCustomIconsButton* picButton = new FWCustomIconsButton(p, upPic, downPic, disabledPic, nullptr, id, norm, option);
164  if (!m_toolTip.empty())
165  picButton->SetToolTipText(m_toolTip.c_str(), m_supervisor->getToolTipDelay());
166  p->AddFrame(picButton, l);
167  TQObject::Connect(picButton, "Clicked()", "CSGAction", this, "activate()");
168  m_buttons.push_back(picButton);
169  if (!isEnabled()) {
170  picButton->SetEnabled(kFALSE);
171  }
172  return picButton;
173 }
174 
175 void CSGAction::createShortcut(UInt_t key, const char* mod, int windowID) {
176  Int_t keycode = gVirtualX->KeysymToKeycode((int)key);
177  m_windowID = windowID;
178  Int_t modcode;
179  TString scText;
180  if (strcmp(mod, "CTRL") == 0) {
181  modcode = kKeyControlMask;
182  scText = "<ctrl> ";
183  } else if (strcmp(mod, "CTRL+SHIFT") == 0) {
184  modcode = kKeyControlMask | kKeyShiftMask;
185  scText = "<ctrl> <shift> ";
186  } else {
187  // Default to ALT for now
188  modcode = kKeyMod1Mask;
189  scText = "<alt> ";
190  }
191  scText += keycodeToString(keycode);
192  m_scCombo = scText;
193 
194  gVirtualX->GrabKey(m_windowID, keycode, modcode, kTRUE);
195  gVirtualX->GrabKey(m_windowID, keycode, modcode | kKeyMod2Mask, kTRUE);
196  gVirtualX->GrabKey(m_windowID, keycode, modcode | kKeyLockMask, kTRUE);
197  gVirtualX->GrabKey(m_windowID, keycode, modcode | kKeyMod2Mask | kKeyLockMask, kTRUE);
198 
199  m_keycode = keycode;
200  m_modcode = modcode;
201  if (m_menu != nullptr)
202  addSCToMenu();
203 }
204 
205 void CSGAction::createMenuEntry(TGPopupMenu* menu) {
206  m_menu = menu;
207  if (!(menu->HasConnection("Activated(Int_t)")))
208  TQObject::Connect(menu, "Activated(Int_t)", "CSGConnector", m_connector, "handleMenu(Int_t)");
209  menu->AddEntry(m_name.c_str(), m_entry);
210  if (m_keycode != 0)
211  addSCToMenu();
212  if (!isEnabled()) {
213  m_menu->DisableEntry(m_entry);
214  }
215 }
216 
218  Bool_t widthChanged = resizeMenuEntry();
219  if (widthChanged)
221 }
222 
224  FontStruct_t font = gClient->GetResourcePool()->GetMenuHiliteFont()->GetFontStruct();
225  Bool_t widthChanged = kTRUE;
226  UInt_t width = m_menu->GetWidth();
227  TString realName(m_name);
228  if (realName.Contains("->")) {
229  // Should make function to do this and store in member data...
230  while (!(realName.BeginsWith("->")) && realName.Length() > 0) {
231  realName.Replace(0, 1, nullptr, 0);
232  }
233  realName.Replace(0, 2, nullptr, 0);
234  }
235  TString scText = m_scCombo;
236  while (gVirtualX->TextWidth(font, realName.Data(), realName.Length()) +
237  gVirtualX->TextWidth(font, scText.Data(), scText.Length()) + 53 <
238  (Int_t)width) {
239  widthChanged = kFALSE;
240  realName += " ";
241  }
242  realName += "\t";
243  realName += scText;
244  TIter next(m_menu->GetListOfEntries());
245  TGMenuEntry* current;
246  while (nullptr != (current = (TGMenuEntry*)next())) {
247  if (current == m_menu->GetEntry(m_entry)) {
248  break;
249  }
250  }
251  current = (TGMenuEntry*)next();
252  m_menu->DeleteEntry(m_entry);
253  m_menu->AddEntry(realName, m_entry, nullptr, nullptr, current);
254  return widthChanged;
255 }
256 
257 TGPopupMenu* CSGAction::getMenu() const { return m_menu; }
258 
259 int CSGAction::getMenuEntry() const { return m_entry; }
260 
261 Int_t CSGAction::getKeycode() const { return m_keycode; }
262 
263 Int_t CSGAction::getModcode() const { return m_modcode; }
264 
265 ToolBarData_t* CSGAction::getToolBarData() const { return m_tools; }
266 
267 TGToolBar* CSGAction::getToolBar() const { return m_toolBar; }
268 
270  m_enabled = true;
271  enableImp();
272 }
273 
275  m_enabled = false;
276  disableImp();
277 }
278 
280  m_globalEnabled = true;
281  enableImp();
282 }
283 
285  m_globalEnabled = false;
286  disableImp();
287 }
288 
289 Bool_t CSGAction::isEnabled() const { return m_enabled && m_globalEnabled; }
290 
292  if (isEnabled()) {
293  if (m_menu != nullptr)
294  m_menu->EnableEntry(m_entry);
295  for (std::vector<TGButton*>::iterator it = m_buttons.begin(), itEnd = m_buttons.end(); it != itEnd; ++it) {
296  (*it)->SetEnabled(kTRUE);
297  }
298 
299  if (m_toolBar != nullptr)
300  m_toolBar->GetButton(m_entry)->SetEnabled(kTRUE);
301  if (m_keycode != 0) {
302  gVirtualX->GrabKey(m_windowID, m_keycode, m_modcode, kTRUE);
303  gVirtualX->GrabKey(m_windowID, m_keycode, m_modcode | kKeyMod2Mask, kTRUE);
304  gVirtualX->GrabKey(m_windowID, m_keycode, m_modcode | kKeyLockMask, kTRUE);
305  gVirtualX->GrabKey(m_windowID, m_keycode, m_modcode | kKeyMod2Mask | kKeyLockMask, kTRUE);
306  }
307  }
308 }
309 
311  if (!isEnabled()) {
312  if (m_menu != nullptr)
313  m_menu->DisableEntry(m_entry);
314  for (std::vector<TGButton*>::iterator it = m_buttons.begin(), itEnd = m_buttons.end(); it != itEnd; ++it) {
315  (*it)->SetEnabled(kFALSE);
316  }
317  if (m_toolBar != nullptr)
318  m_toolBar->GetButton(m_entry)->SetEnabled(kFALSE);
319  if (m_keycode != 0) {
320  gVirtualX->GrabKey(m_windowID, m_keycode, m_modcode, kFALSE);
321  gVirtualX->GrabKey(m_windowID, m_keycode, m_modcode | kKeyMod2Mask, kFALSE);
322  gVirtualX->GrabKey(m_windowID, m_keycode, m_modcode | kKeyLockMask, kFALSE);
323  gVirtualX->GrabKey(m_windowID, m_keycode, m_modcode | kKeyMod2Mask | kKeyLockMask, kFALSE);
324  }
325  }
326 }
327 
328 //
329 // static member functions
330 //
331 
332 TString CSGAction::keycodeToString(Int_t keycode) {
333  int i;
334  char letter;
335  TString rep;
336  for (i = kKey_a; i < kKey_a + 26; i++) {
337  if (gVirtualX->KeysymToKeycode(i) == keycode) {
338  letter = (char)(i - kKey_a + 'a');
339  rep = TString(letter);
340  return rep;
341  }
342  }
343  for (i = kKey_A; i < kKey_A + 26; i++) {
344  if (gVirtualX->KeysymToKeycode(i) == keycode) {
345  letter = (char)(i - kKey_A + 'a');
346  rep = TString(letter);
347  return rep;
348  }
349  }
350  if (keycode == gVirtualX->KeysymToKeycode(kKey_Left)) {
351  rep = TString("<-");
352  return rep;
353  }
354  if (keycode == gVirtualX->KeysymToKeycode(kKey_Right)) {
355  rep = TString("->");
356  return rep;
357  }
358  if (keycode == gVirtualX->KeysymToKeycode(kKey_Space)) {
359  rep = TString("space");
360  return rep;
361  }
362  rep = TString("");
363  return rep;
364 }
TGToolBar * getToolBar() const
Definition: CSGAction.cc:267
int m_entry
Definition: CSGAction.h:118
Int_t m_modcode
Definition: CSGAction.h:116
TString m_scCombo
Definition: CSGAction.h:113
int getMenuEntry() const
Definition: CSGAction.cc:259
virtual ~CSGAction()
Definition: CSGAction.cc:59
ToolBarData_t * m_tools
Definition: CSGAction.h:120
void setMenuLabel(const std::string &label)
Definition: CSGAction.cc:98
const std::vector< CSGAction * > & getListOfActions() const
void enable()
Definition: CSGAction.cc:269
void disableImp()
Definition: CSGAction.cc:310
void addSCToMenu()
Definition: CSGAction.cc:217
CSGConnector * m_connector
Definition: CSGAction.h:121
void resizeMenu(TGPopupMenu *menu)
ToolBarData_t * getToolBarData() const
Definition: CSGAction.cc:265
TGToolBar * m_toolBar
Definition: CSGAction.h:119
TGPopupMenu * m_menu
Definition: CSGAction.h:117
int m_windowID
Definition: CSGAction.h:124
void disable()
Definition: CSGAction.cc:274
Bool_t m_enabled
Definition: CSGAction.h:122
virtual void globalDisable()
Definition: CSGAction.cc:284
void createTextButton(TGCompositeFrame *p, TGLayoutHints *l=nullptr, Int_t id=-1, GContext_t norm=TGButton::GetDefaultGC()(), FontStruct_t font=TGTextButton::GetDefaultFontStruct(), UInt_t option=kRaisedFrame|kDoubleBorder)
Definition: CSGAction.cc:113
const std::string & getName() const
Definition: CSGAction.cc:79
void addToActionMap(CSGAction *action)
char const * label
void createPictureButton(TGCompositeFrame *p, const TGPicture *pic, TGLayoutHints *l=nullptr, Int_t id=-1, GContext_t norm=TGButton::GetDefaultGC()(), UInt_t option=kRaisedFrame|kDoubleBorder)
Definition: CSGAction.cc:142
Int_t getKeycode() const
Definition: CSGAction.cc:261
void setToolTip(const std::string &tip)
Definition: CSGAction.cc:104
static TString keycodeToString(Int_t keycode)
Definition: CSGAction.cc:332
Bool_t m_globalEnabled
Definition: CSGAction.h:123
TGPopupMenu * getMenu() const
Definition: CSGAction.cc:257
CSGActionSupervisor * m_supervisor
Definition: CSGAction.h:110
void setName(const std::string &name)
Definition: CSGAction.cc:85
const std::string & getToolTip() const
Definition: CSGAction.cc:81
void enableImp()
Definition: CSGAction.cc:291
rep
Definition: cuy.py:1189
void createMenuEntry(TGPopupMenu *menu)
Definition: CSGAction.cc:205
Int_t getModcode() const
Definition: CSGAction.cc:263
void createCheckButton(TGCompositeFrame *p, TGLayoutHints *l=nullptr, Bool_t state=true, Int_t id=-1, GContext_t norm=TGButton::GetDefaultGC()(), FontStruct_t font=TGTextButton::GetDefaultFontStruct())
Definition: CSGAction.cc:126
CSGAction(CSGActionSupervisor *supervisor, const char *name)
Definition: CSGAction.cc:38
virtual void globalEnable()
Definition: CSGAction.cc:279
std::vector< TGButton * > m_buttons
Definition: CSGAction.h:114
virtual Bool_t isEnabled() const
Definition: CSGAction.cc:289
Long_t getToolTipDelay() const
FWCustomIconsButton * createCustomIconsButton(TGCompositeFrame *p, const TGPicture *upPic, const TGPicture *downPic, const TGPicture *disabledPic, TGLayoutHints *l=nullptr, Int_t id=-1, GContext_t norm=TGButton::GetDefaultGC()(), UInt_t option=0)
Definition: CSGAction.cc:155
std::string m_toolTip
Definition: CSGAction.h:112
void createShortcut(UInt_t key, const char *mod, int windowID)
Definition: CSGAction.cc:175
TString getSCCombo() const
Definition: CSGAction.cc:83
std::string m_name
Definition: CSGAction.h:111
Bool_t resizeMenuEntry()
Definition: CSGAction.cc:223
T mod(const T &a, const T &b)
Definition: ecalDccMap.h:4
Int_t m_keycode
Definition: CSGAction.h:115