CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/Fireworks/Core/src/CSGAction.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     Core
00004 // Class  :     CSGAction
00005 //
00006 // Implementation:
00007 //     <Notes on implementation>
00008 //
00009 // Original Author:  Chris Jones
00010 //         Created:  Thu May 29 20:58:11 CDT 2008
00011 // $Id: CSGAction.cc,v 1.28 2010/06/18 10:17:14 yana Exp $
00012 //
00013 
00014 // system include files
00015 #include <TString.h>
00016 #include <TGResourcePool.h>
00017 #include <TQObject.h>
00018 #include <KeySymbols.h>
00019 #include <TGMenu.h>
00020 
00021 // user include files
00022 #include "Fireworks/Core/interface/CSGAction.h"
00023 #include "Fireworks/Core/src/CSGConnector.h"
00024 #include "Fireworks/Core/interface/CSGActionSupervisor.h"
00025 #include "Fireworks/Core/interface/FWCustomIconsButton.h"
00026 
00027 //
00028 // constants, enums and typedefs
00029 //
00030 
00031 //
00032 // static data member definitions
00033 //
00034 
00035 //
00036 // constructors and destructor
00037 //
00038 CSGAction::CSGAction(CSGActionSupervisor *supervisor, const char *name) :
00039    m_connector(0)
00040  {
00041    m_enabled = true;
00042    m_globalEnabled = true;
00043    m_supervisor = supervisor;
00044    m_name = name;
00045    m_toolTip = "";
00046    m_menu = 0;
00047    m_toolBar = 0;
00048    m_tools = 0;
00049    m_connector = new CSGConnector(this, m_supervisor);
00050    m_supervisor->addToActionMap(this);
00051    m_entry = m_supervisor->getListOfActions().size();
00052    m_keycode = 0;
00053    m_modcode = 0;
00054    m_windowID = -1;
00055 }
00056 // CSGAction::CSGAction(const CSGAction& rhs)
00057 // {
00058 //    // do actual copying here;
00059 // }
00060 
00061 CSGAction::~CSGAction()
00062 {
00063    delete m_connector;
00064    //Don't delete GUI parts since they are owned by their GUI parent
00065 }
00066 
00067 //
00068 // assignment operators
00069 //
00070 // const CSGAction& CSGAction::operator=(const CSGAction& rhs)
00071 // {
00072 //   //An exception safe implementation is
00073 //   CSGAction temp(rhs);
00074 //   swap(rhs);
00075 //
00076 //   return *this;
00077 // }
00078 
00079 //
00080 // member functions
00081 //
00082 const std::string& CSGAction::getName() const {
00083    return m_name;
00084 }
00085 
00086 const std::string& CSGAction::getToolTip() const {
00087    return m_toolTip;
00088 }
00089 
00090 TString CSGAction::getSCCombo() const {
00091    return m_scCombo;
00092 }
00093 
00094 void CSGAction::setName(const std::string& name) {
00095    // Does not update menu yet
00096    m_name = name;
00097 }
00098 
00099 void 
00100 CSGAction::setMenuLabel(const std::string& label) {
00101    if(m_menu) {
00102       m_menu->GetEntry(m_entry)->GetLabel()->SetString(label.c_str());
00103    }
00104 }
00105 
00106 void CSGAction::setToolTip(const std::string& tip) {
00107    m_toolTip = tip;
00108    for(std::vector<TGButton*>::iterator it = m_buttons.begin(), itEnd = m_buttons.end();
00109        it != itEnd;
00110        ++it) {
00111       (*it)->SetToolTipText(tip.c_str(), m_supervisor->getToolTipDelay());
00112    }
00113    if (m_tools != 0) m_tools->fTipText = tip.c_str();
00114 }
00115 
00116 void CSGAction::createTextButton(TGCompositeFrame* p, TGLayoutHints* l, Int_t id, GContext_t norm, FontStruct_t font, UInt_t option) {
00117    TGTextButton* textButton = new TGTextButton(p, m_name.c_str(), id, norm, font, option);
00118    if (m_toolTip != "") textButton->SetToolTipText(m_toolTip.c_str(), m_supervisor->getToolTipDelay());
00119    p->AddFrame(textButton, l);
00120    TQObject::Connect(textButton, "Clicked()", "CSGAction", this, "activate()");
00121    m_buttons.push_back(textButton);
00122    if(!isEnabled()) {
00123       textButton->SetEnabled(kFALSE);
00124    }
00125 }
00126 
00127 void CSGAction::createCheckButton(TGCompositeFrame* p, TGLayoutHints* l, Bool_t state, Int_t id, GContext_t norm, FontStruct_t font) {
00128    TGCheckButton* checkButton = new TGCheckButton(p, m_name.c_str(), id, norm, font);
00129    if (m_toolTip != "") checkButton->SetToolTipText(m_toolTip.c_str(), m_supervisor->getToolTipDelay());
00130    p->AddFrame(checkButton, l);
00131 
00132    if (state)   checkButton->SetState(kButtonDown, false);
00133    TQObject::Connect(checkButton, "Clicked()", "CSGAction", this, "activate()");
00134    m_buttons.push_back(checkButton);
00135    if(!isEnabled()) {
00136       checkButton->SetEnabled(kFALSE);
00137    }
00138 }
00139 
00140 void CSGAction::createPictureButton(TGCompositeFrame* p, const TGPicture* pic, TGLayoutHints* l, Int_t id, GContext_t norm, UInt_t option) {
00141    TGPictureButton* picButton = new TGPictureButton(p, pic, id, norm, option);
00142    if (m_toolTip != "") picButton->SetToolTipText(m_toolTip.c_str(), m_supervisor->getToolTipDelay());
00143    p->AddFrame(picButton, l);
00144    TQObject::Connect(picButton, "Clicked()", "CSGAction", this, "activate()");
00145    m_buttons.push_back(picButton);
00146    if(!isEnabled()) {
00147       picButton->SetEnabled(kFALSE);
00148    }
00149 }
00150 
00151 FWCustomIconsButton*
00152 CSGAction::createCustomIconsButton(TGCompositeFrame* p,
00153                                    const TGPicture* upPic,
00154                                    const TGPicture* downPic,
00155                                    const TGPicture* disabledPic,
00156                                    TGLayoutHints* l,
00157                                    Int_t id,
00158                                    GContext_t norm,
00159                                    UInt_t option)
00160 {
00161    FWCustomIconsButton* picButton = new FWCustomIconsButton(p, upPic, downPic, disabledPic, 0, id, norm, option);
00162    if (m_toolTip != "") picButton->SetToolTipText(m_toolTip.c_str(), m_supervisor->getToolTipDelay());
00163    p->AddFrame(picButton, l);
00164    TQObject::Connect(picButton, "Clicked()", "CSGAction", this, "activate()");
00165    m_buttons.push_back(picButton);
00166    if(!isEnabled()) {
00167       picButton->SetEnabled(kFALSE);
00168    }
00169    return picButton;
00170 }
00171 
00172 void CSGAction::createShortcut(UInt_t key, const char *mod, int windowID) {
00173    Int_t keycode = gVirtualX->KeysymToKeycode((int)key);
00174    m_windowID = windowID;
00175    Int_t modcode;
00176    TString scText;
00177    if (strcmp(mod, "CTRL") == 0) {
00178       modcode = kKeyControlMask;
00179       scText = "<ctrl> ";
00180    }
00181    else if (strcmp(mod, "CTRL+SHIFT") == 0) {
00182       modcode = kKeyControlMask | kKeyShiftMask;
00183       scText = "<ctrl> <shift> ";
00184    }
00185    else {
00186       // Default to ALT for now
00187       modcode = kKeyMod1Mask;
00188       scText = "<alt> ";
00189    }
00190    scText += keycodeToString(keycode);
00191    m_scCombo = scText;
00192 
00193    gVirtualX->GrabKey(m_windowID, keycode, modcode, kTRUE);
00194    gVirtualX->GrabKey(m_windowID, keycode, modcode | kKeyMod2Mask, kTRUE);
00195    gVirtualX->GrabKey(m_windowID, keycode, modcode | kKeyLockMask, kTRUE);
00196    gVirtualX->GrabKey(m_windowID, keycode, modcode | kKeyMod2Mask | kKeyLockMask, kTRUE);
00197 
00198    m_keycode = keycode;
00199    m_modcode = modcode;
00200    if (m_menu != 0) addSCToMenu();
00201 }
00202 
00203 void CSGAction::createMenuEntry(TGPopupMenu *menu) {
00204    m_menu = menu;
00205    if (!(menu->HasConnection("Activated(Int_t)"))) TQObject::Connect(menu, "Activated(Int_t)", "CSGConnector", m_connector, "handleMenu(Int_t)");
00206    menu->AddEntry(m_name.c_str(), m_entry);
00207    if (m_keycode != 0) addSCToMenu();
00208    if(!isEnabled()) {
00209       m_menu->DisableEntry(m_entry);
00210    }
00211 }
00212 
00213 void CSGAction::addSCToMenu() {
00214    Bool_t widthChanged = resizeMenuEntry();
00215    if (widthChanged) m_supervisor->resizeMenu(m_menu);
00216 }
00217 
00218 Bool_t CSGAction::resizeMenuEntry() {
00219    FontStruct_t font = gClient->GetResourcePool()->GetMenuHiliteFont()->GetFontStruct();
00220    Bool_t widthChanged = kTRUE;
00221    UInt_t width = m_menu->GetWidth();
00222    TString realName(m_name);
00223    if (realName.Contains("->")) {
00224       // Should make function to do this and store in member data...
00225       while (!(realName.BeginsWith("->")) && realName.Length() > 0) {
00226          realName.Replace(0,1,0,0);
00227       }
00228       realName.Replace(0,2,0,0);
00229    }
00230    TString scText = m_scCombo;
00231    while (gVirtualX->TextWidth(font, realName.Data(), realName.Length()) + gVirtualX->TextWidth(font, scText.Data(), scText.Length()) + 53 < (Int_t)width) {
00232       widthChanged = kFALSE;
00233       realName += " ";
00234    }
00235    realName += "\t";
00236    realName += scText;
00237    TIter next(m_menu->GetListOfEntries());
00238    TGMenuEntry *current;
00239    while (0 != (current = (TGMenuEntry *)next())) {
00240       if (current == m_menu->GetEntry(m_entry)) {
00241          break;
00242       }
00243    }
00244    current = (TGMenuEntry *)next();
00245    m_menu->DeleteEntry(m_entry);
00246    m_menu->AddEntry(realName, m_entry, 0, 0, current);
00247    return widthChanged;
00248 }
00249 
00250 TGPopupMenu* CSGAction::getMenu() const {
00251    return m_menu;
00252 }
00253 
00254 int CSGAction::getMenuEntry() const {
00255    return m_entry;
00256 }
00257 
00258 Int_t CSGAction::getKeycode() const {
00259    return m_keycode;
00260 }
00261 
00262 Int_t CSGAction::getModcode() const {
00263    return m_modcode;
00264 }
00265 
00266 ToolBarData_t* CSGAction::getToolBarData() const {
00267    return m_tools;
00268 }
00269 
00270 TGToolBar* CSGAction::getToolBar() const {
00271    return m_toolBar;
00272 }
00273 
00274 void CSGAction::enable() {
00275    m_enabled = true;
00276    enableImp();
00277 }
00278 
00279 void CSGAction::disable() {
00280    m_enabled = false;
00281    disableImp();
00282 }
00283 
00284 void
00285 CSGAction::globalEnable()
00286 {
00287    m_globalEnabled=true;
00288    enableImp();
00289 }
00290 
00291 void
00292 CSGAction::globalDisable()
00293 {
00294    m_globalEnabled=false;
00295    disableImp();
00296 }
00297 
00298 Bool_t CSGAction::isEnabled() const {
00299    return m_enabled && m_globalEnabled;
00300 }
00301 
00302 void CSGAction::enableImp() {
00303    if(isEnabled()) {
00304       if (m_menu != 0) m_menu->EnableEntry(m_entry);
00305       for(std::vector<TGButton*>::iterator it = m_buttons.begin(), itEnd = m_buttons.end();
00306           it != itEnd;
00307           ++it) {
00308          (*it)->SetEnabled(kTRUE);
00309       }
00310 
00311       if (m_toolBar != 0) m_toolBar->GetButton(m_entry)->SetEnabled(kTRUE);
00312       if (m_keycode != 0) {
00313          gVirtualX->GrabKey(m_windowID, m_keycode, m_modcode, kTRUE);
00314          gVirtualX->GrabKey(m_windowID, m_keycode, m_modcode | kKeyMod2Mask, kTRUE);
00315          gVirtualX->GrabKey(m_windowID, m_keycode, m_modcode | kKeyLockMask, kTRUE);
00316          gVirtualX->GrabKey(m_windowID, m_keycode, m_modcode | kKeyMod2Mask | kKeyLockMask, kTRUE);
00317       }
00318    }
00319 }
00320 
00321 void CSGAction::disableImp() {
00322    if(!isEnabled()) {
00323       if (m_menu != 0) m_menu->DisableEntry(m_entry);
00324       for(std::vector<TGButton*>::iterator it = m_buttons.begin(), itEnd = m_buttons.end();
00325           it != itEnd;
00326           ++it) {
00327          (*it)->SetEnabled(kFALSE);
00328       }
00329       if (m_toolBar != 0) m_toolBar->GetButton(m_entry)->SetEnabled(kFALSE);
00330       if (m_keycode != 0) {
00331          gVirtualX->GrabKey(m_windowID, m_keycode, m_modcode, kFALSE);
00332          gVirtualX->GrabKey(m_windowID, m_keycode, m_modcode | kKeyMod2Mask, kFALSE);
00333          gVirtualX->GrabKey(m_windowID, m_keycode, m_modcode | kKeyLockMask, kFALSE);
00334          gVirtualX->GrabKey(m_windowID, m_keycode, m_modcode | kKeyMod2Mask | kKeyLockMask, kFALSE);
00335       }
00336    }
00337 }
00338 
00339 
00340 //
00341 // static member functions
00342 //
00343 
00344 TString
00345 CSGAction::keycodeToString(Int_t keycode) {
00346    int i;
00347    char letter;
00348    TString rep;
00349    for (i = kKey_a; i < kKey_a + 26; i++) {
00350       if (gVirtualX->KeysymToKeycode(i) == keycode) {
00351          letter = (char)(i - kKey_a + 'a');
00352          rep = TString(letter);
00353          return rep;
00354       }
00355    }
00356    for (i = kKey_A; i < kKey_A + 26; i++) {
00357       if(gVirtualX->KeysymToKeycode(i) == keycode) {
00358          letter = (char)(i - kKey_A + 'a');
00359          rep = TString(letter);
00360          return rep;
00361       }
00362    }
00363    if (keycode == gVirtualX->KeysymToKeycode(kKey_Left)) {
00364       rep = TString("<-");
00365       return rep;
00366    }
00367    if (keycode == gVirtualX->KeysymToKeycode(kKey_Right)) {
00368       rep = TString("->");
00369       return rep;
00370    }
00371    rep = TString("");
00372    return rep;
00373 }
00374