File indexing completed on 2024-04-06 12:11:35
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <TString.h>
0015 #include <TGResourcePool.h>
0016 #include <TQObject.h>
0017 #include <KeySymbols.h>
0018 #include <TGMenu.h>
0019 #include <TVirtualX.h>
0020
0021
0022 #include "Fireworks/Core/interface/CSGAction.h"
0023 #include "Fireworks/Core/src/CSGConnector.h"
0024 #include "Fireworks/Core/interface/CSGActionSupervisor.h"
0025 #include "Fireworks/Core/interface/FWCustomIconsButton.h"
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038 CSGAction::CSGAction(CSGActionSupervisor* supervisor, const char* name) : m_connector(nullptr) {
0039 m_enabled = true;
0040 m_globalEnabled = true;
0041 m_supervisor = supervisor;
0042 m_name = name;
0043 m_toolTip = "";
0044 m_menu = nullptr;
0045 m_toolBar = nullptr;
0046 m_tools = nullptr;
0047 m_connector = new CSGConnector(this, m_supervisor);
0048 m_supervisor->addToActionMap(this);
0049 m_entry = m_supervisor->getListOfActions().size();
0050 m_keycode = 0;
0051 m_modcode = 0;
0052 m_windowID = -1;
0053 }
0054
0055
0056
0057
0058
0059 CSGAction::~CSGAction() {
0060 delete m_connector;
0061
0062 }
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079 const std::string& CSGAction::getName() const { return m_name; }
0080
0081 const std::string& CSGAction::getToolTip() const { return m_toolTip; }
0082
0083 TString CSGAction::getSCCombo() const { return m_scCombo; }
0084
0085 void CSGAction::setName(const std::string& name) {
0086
0087 m_name = name;
0088
0089 for (std::vector<TGButton*>::iterator it = m_buttons.begin(), itEnd = m_buttons.end(); it != itEnd; ++it) {
0090 TGTextButton* tb = dynamic_cast<TGTextButton*>(*it);
0091 if (tb) {
0092 (tb)->SetText(name.c_str());
0093 gClient->NeedRedraw(tb);
0094 }
0095 }
0096 }
0097
0098 void CSGAction::setMenuLabel(const std::string& label) {
0099 if (m_menu) {
0100 m_menu->GetEntry(m_entry)->GetLabel()->SetString(label.c_str());
0101 }
0102 }
0103
0104 void CSGAction::setToolTip(const std::string& tip) {
0105 m_toolTip = tip;
0106 for (std::vector<TGButton*>::iterator it = m_buttons.begin(), itEnd = m_buttons.end(); it != itEnd; ++it) {
0107 (*it)->SetToolTipText(tip.c_str(), m_supervisor->getToolTipDelay());
0108 }
0109 if (m_tools != nullptr)
0110 m_tools->fTipText = tip.c_str();
0111 }
0112
0113 void CSGAction::createTextButton(
0114 TGCompositeFrame* p, TGLayoutHints* l, Int_t id, GContext_t norm, FontStruct_t font, UInt_t option) {
0115 TGTextButton* textButton = new TGTextButton(p, m_name.c_str(), id, norm, font, option);
0116 if (!m_toolTip.empty())
0117 textButton->SetToolTipText(m_toolTip.c_str(), m_supervisor->getToolTipDelay());
0118 p->AddFrame(textButton, l);
0119 TQObject::Connect(textButton, "Clicked()", "CSGAction", this, "activate()");
0120 m_buttons.push_back(textButton);
0121 if (!isEnabled()) {
0122 textButton->SetEnabled(kFALSE);
0123 }
0124 }
0125
0126 void CSGAction::createCheckButton(
0127 TGCompositeFrame* p, TGLayoutHints* l, Bool_t state, Int_t id, GContext_t norm, FontStruct_t font) {
0128 TGCheckButton* checkButton = new TGCheckButton(p, m_name.c_str(), id, norm, font);
0129 if (!m_toolTip.empty())
0130 checkButton->SetToolTipText(m_toolTip.c_str(), m_supervisor->getToolTipDelay());
0131 p->AddFrame(checkButton, l);
0132
0133 if (state)
0134 checkButton->SetState(kButtonDown, false);
0135 TQObject::Connect(checkButton, "Clicked()", "CSGAction", this, "activate()");
0136 m_buttons.push_back(checkButton);
0137 if (!isEnabled()) {
0138 checkButton->SetEnabled(kFALSE);
0139 }
0140 }
0141
0142 void CSGAction::createPictureButton(
0143 TGCompositeFrame* p, const TGPicture* pic, TGLayoutHints* l, Int_t id, GContext_t norm, UInt_t option) {
0144 TGPictureButton* picButton = new TGPictureButton(p, pic, id, norm, option);
0145 if (!m_toolTip.empty())
0146 picButton->SetToolTipText(m_toolTip.c_str(), m_supervisor->getToolTipDelay());
0147 p->AddFrame(picButton, l);
0148 TQObject::Connect(picButton, "Clicked()", "CSGAction", this, "activate()");
0149 m_buttons.push_back(picButton);
0150 if (!isEnabled()) {
0151 picButton->SetEnabled(kFALSE);
0152 }
0153 }
0154
0155 FWCustomIconsButton* CSGAction::createCustomIconsButton(TGCompositeFrame* p,
0156 const TGPicture* upPic,
0157 const TGPicture* downPic,
0158 const TGPicture* disabledPic,
0159 TGLayoutHints* l,
0160 Int_t id,
0161 GContext_t norm,
0162 UInt_t option) {
0163 FWCustomIconsButton* picButton = new FWCustomIconsButton(p, upPic, downPic, disabledPic, nullptr, id, norm, option);
0164 if (!m_toolTip.empty())
0165 picButton->SetToolTipText(m_toolTip.c_str(), m_supervisor->getToolTipDelay());
0166 p->AddFrame(picButton, l);
0167 TQObject::Connect(picButton, "Clicked()", "CSGAction", this, "activate()");
0168 m_buttons.push_back(picButton);
0169 if (!isEnabled()) {
0170 picButton->SetEnabled(kFALSE);
0171 }
0172 return picButton;
0173 }
0174
0175 void CSGAction::createShortcut(UInt_t key, const char* mod, int windowID) {
0176 Int_t keycode = gVirtualX->KeysymToKeycode((int)key);
0177 m_windowID = windowID;
0178 Int_t modcode;
0179 TString scText;
0180 if (strcmp(mod, "CTRL") == 0) {
0181 modcode = kKeyControlMask;
0182 scText = "<ctrl> ";
0183 } else if (strcmp(mod, "CTRL+SHIFT") == 0) {
0184 modcode = kKeyControlMask | kKeyShiftMask;
0185 scText = "<ctrl> <shift> ";
0186 } else {
0187
0188 modcode = kKeyMod1Mask;
0189 scText = "<alt> ";
0190 }
0191 scText += keycodeToString(keycode);
0192 m_scCombo = scText;
0193
0194 gVirtualX->GrabKey(m_windowID, keycode, modcode, kTRUE);
0195 gVirtualX->GrabKey(m_windowID, keycode, modcode | kKeyMod2Mask, kTRUE);
0196 gVirtualX->GrabKey(m_windowID, keycode, modcode | kKeyLockMask, kTRUE);
0197 gVirtualX->GrabKey(m_windowID, keycode, modcode | kKeyMod2Mask | kKeyLockMask, kTRUE);
0198
0199 m_keycode = keycode;
0200 m_modcode = modcode;
0201 if (m_menu != nullptr)
0202 addSCToMenu();
0203 }
0204
0205 void CSGAction::createMenuEntry(TGPopupMenu* menu) {
0206 m_menu = menu;
0207 if (!(menu->HasConnection("Activated(Int_t)")))
0208 TQObject::Connect(menu, "Activated(Int_t)", "CSGConnector", m_connector, "handleMenu(Int_t)");
0209 menu->AddEntry(m_name.c_str(), m_entry);
0210 if (m_keycode != 0)
0211 addSCToMenu();
0212 if (!isEnabled()) {
0213 m_menu->DisableEntry(m_entry);
0214 }
0215 }
0216
0217 void CSGAction::addSCToMenu() {
0218 Bool_t widthChanged = resizeMenuEntry();
0219 if (widthChanged)
0220 m_supervisor->resizeMenu(m_menu);
0221 }
0222
0223 Bool_t CSGAction::resizeMenuEntry() {
0224 FontStruct_t font = gClient->GetResourcePool()->GetMenuHiliteFont()->GetFontStruct();
0225 Bool_t widthChanged = kTRUE;
0226 UInt_t width = m_menu->GetWidth();
0227 TString realName(m_name);
0228 if (realName.Contains("->")) {
0229
0230 while (!(realName.BeginsWith("->")) && realName.Length() > 0) {
0231 realName.Replace(0, 1, nullptr, 0);
0232 }
0233 realName.Replace(0, 2, nullptr, 0);
0234 }
0235 TString scText = m_scCombo;
0236 while (gVirtualX->TextWidth(font, realName.Data(), realName.Length()) +
0237 gVirtualX->TextWidth(font, scText.Data(), scText.Length()) + 53 <
0238 (Int_t)width) {
0239 widthChanged = kFALSE;
0240 realName += " ";
0241 }
0242 realName += "\t";
0243 realName += scText;
0244 TIter next(m_menu->GetListOfEntries());
0245 TGMenuEntry* current;
0246 while (nullptr != (current = (TGMenuEntry*)next())) {
0247 if (current == m_menu->GetEntry(m_entry)) {
0248 break;
0249 }
0250 }
0251 current = (TGMenuEntry*)next();
0252 m_menu->DeleteEntry(m_entry);
0253 m_menu->AddEntry(realName, m_entry, nullptr, nullptr, current);
0254 return widthChanged;
0255 }
0256
0257 TGPopupMenu* CSGAction::getMenu() const { return m_menu; }
0258
0259 int CSGAction::getMenuEntry() const { return m_entry; }
0260
0261 Int_t CSGAction::getKeycode() const { return m_keycode; }
0262
0263 Int_t CSGAction::getModcode() const { return m_modcode; }
0264
0265 ToolBarData_t* CSGAction::getToolBarData() const { return m_tools; }
0266
0267 TGToolBar* CSGAction::getToolBar() const { return m_toolBar; }
0268
0269 void CSGAction::enable() {
0270 m_enabled = true;
0271 enableImp();
0272 }
0273
0274 void CSGAction::disable() {
0275 m_enabled = false;
0276 disableImp();
0277 }
0278
0279 void CSGAction::globalEnable() {
0280 m_globalEnabled = true;
0281 enableImp();
0282 }
0283
0284 void CSGAction::globalDisable() {
0285 m_globalEnabled = false;
0286 disableImp();
0287 }
0288
0289 Bool_t CSGAction::isEnabled() const { return m_enabled && m_globalEnabled; }
0290
0291 void CSGAction::enableImp() {
0292 if (isEnabled()) {
0293 if (m_menu != nullptr)
0294 m_menu->EnableEntry(m_entry);
0295 for (std::vector<TGButton*>::iterator it = m_buttons.begin(), itEnd = m_buttons.end(); it != itEnd; ++it) {
0296 (*it)->SetEnabled(kTRUE);
0297 }
0298
0299 if (m_toolBar != nullptr)
0300 m_toolBar->GetButton(m_entry)->SetEnabled(kTRUE);
0301 if (m_keycode != 0) {
0302 gVirtualX->GrabKey(m_windowID, m_keycode, m_modcode, kTRUE);
0303 gVirtualX->GrabKey(m_windowID, m_keycode, m_modcode | kKeyMod2Mask, kTRUE);
0304 gVirtualX->GrabKey(m_windowID, m_keycode, m_modcode | kKeyLockMask, kTRUE);
0305 gVirtualX->GrabKey(m_windowID, m_keycode, m_modcode | kKeyMod2Mask | kKeyLockMask, kTRUE);
0306 }
0307 }
0308 }
0309
0310 void CSGAction::disableImp() {
0311 if (!isEnabled()) {
0312 if (m_menu != nullptr)
0313 m_menu->DisableEntry(m_entry);
0314 for (std::vector<TGButton*>::iterator it = m_buttons.begin(), itEnd = m_buttons.end(); it != itEnd; ++it) {
0315 (*it)->SetEnabled(kFALSE);
0316 }
0317 if (m_toolBar != nullptr)
0318 m_toolBar->GetButton(m_entry)->SetEnabled(kFALSE);
0319 if (m_keycode != 0) {
0320 gVirtualX->GrabKey(m_windowID, m_keycode, m_modcode, kFALSE);
0321 gVirtualX->GrabKey(m_windowID, m_keycode, m_modcode | kKeyMod2Mask, kFALSE);
0322 gVirtualX->GrabKey(m_windowID, m_keycode, m_modcode | kKeyLockMask, kFALSE);
0323 gVirtualX->GrabKey(m_windowID, m_keycode, m_modcode | kKeyMod2Mask | kKeyLockMask, kFALSE);
0324 }
0325 }
0326 }
0327
0328
0329
0330
0331
0332 TString CSGAction::keycodeToString(Int_t keycode) {
0333 int i;
0334 char letter;
0335 TString rep;
0336 for (i = kKey_a; i < kKey_a + 26; i++) {
0337 if (gVirtualX->KeysymToKeycode(i) == keycode) {
0338 letter = (char)(i - kKey_a + 'a');
0339 rep = TString(letter);
0340 return rep;
0341 }
0342 }
0343 for (i = kKey_A; i < kKey_A + 26; i++) {
0344 if (gVirtualX->KeysymToKeycode(i) == keycode) {
0345 letter = (char)(i - kKey_A + 'a');
0346 rep = TString(letter);
0347 return rep;
0348 }
0349 }
0350 if (keycode == gVirtualX->KeysymToKeycode(kKey_Left)) {
0351 rep = TString("<-");
0352 return rep;
0353 }
0354 if (keycode == gVirtualX->KeysymToKeycode(kKey_Right)) {
0355 rep = TString("->");
0356 return rep;
0357 }
0358 if (keycode == gVirtualX->KeysymToKeycode(kKey_Space)) {
0359 rep = TString("space");
0360 return rep;
0361 }
0362 rep = TString("");
0363 return rep;
0364 }