CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/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.29 2012/07/26 06:07:18 amraktad 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    for(std::vector<TGButton*>::iterator it = m_buttons.begin(), itEnd = m_buttons.end();
00099        it != itEnd;
00100        ++it) {
00101       TGTextButton* tb = dynamic_cast<TGTextButton*>(*it);
00102       if (tb)
00103       {
00104          (tb)->SetText(name.c_str());
00105           gClient->NeedRedraw(tb);
00106       }
00107    }
00108 }
00109 
00110 void 
00111 CSGAction::setMenuLabel(const std::string& label) {
00112    if(m_menu) {
00113       m_menu->GetEntry(m_entry)->GetLabel()->SetString(label.c_str());
00114    }
00115 }
00116 
00117 void CSGAction::setToolTip(const std::string& tip) {
00118    m_toolTip = tip;
00119    for(std::vector<TGButton*>::iterator it = m_buttons.begin(), itEnd = m_buttons.end();
00120        it != itEnd;
00121        ++it) {
00122       (*it)->SetToolTipText(tip.c_str(), m_supervisor->getToolTipDelay());
00123    }
00124    if (m_tools != 0) m_tools->fTipText = tip.c_str();
00125 }
00126 
00127 void CSGAction::createTextButton(TGCompositeFrame* p, TGLayoutHints* l, Int_t id, GContext_t norm, FontStruct_t font, UInt_t option) {
00128    TGTextButton* textButton = new TGTextButton(p, m_name.c_str(), id, norm, font, option);
00129    if (m_toolTip != "") textButton->SetToolTipText(m_toolTip.c_str(), m_supervisor->getToolTipDelay());
00130    p->AddFrame(textButton, l);
00131    TQObject::Connect(textButton, "Clicked()", "CSGAction", this, "activate()");
00132    m_buttons.push_back(textButton);
00133    if(!isEnabled()) {
00134       textButton->SetEnabled(kFALSE);
00135    }
00136 }
00137 
00138 void CSGAction::createCheckButton(TGCompositeFrame* p, TGLayoutHints* l, Bool_t state, Int_t id, GContext_t norm, FontStruct_t font) {
00139    TGCheckButton* checkButton = new TGCheckButton(p, m_name.c_str(), id, norm, font);
00140    if (m_toolTip != "") checkButton->SetToolTipText(m_toolTip.c_str(), m_supervisor->getToolTipDelay());
00141    p->AddFrame(checkButton, l);
00142 
00143    if (state)   checkButton->SetState(kButtonDown, false);
00144    TQObject::Connect(checkButton, "Clicked()", "CSGAction", this, "activate()");
00145    m_buttons.push_back(checkButton);
00146    if(!isEnabled()) {
00147       checkButton->SetEnabled(kFALSE);
00148    }
00149 }
00150 
00151 void CSGAction::createPictureButton(TGCompositeFrame* p, const TGPicture* pic, TGLayoutHints* l, Int_t id, GContext_t norm, UInt_t option) {
00152    TGPictureButton* picButton = new TGPictureButton(p, pic, id, norm, option);
00153    if (m_toolTip != "") picButton->SetToolTipText(m_toolTip.c_str(), m_supervisor->getToolTipDelay());
00154    p->AddFrame(picButton, l);
00155    TQObject::Connect(picButton, "Clicked()", "CSGAction", this, "activate()");
00156    m_buttons.push_back(picButton);
00157    if(!isEnabled()) {
00158       picButton->SetEnabled(kFALSE);
00159    }
00160 }
00161 
00162 FWCustomIconsButton*
00163 CSGAction::createCustomIconsButton(TGCompositeFrame* p,
00164                                    const TGPicture* upPic,
00165                                    const TGPicture* downPic,
00166                                    const TGPicture* disabledPic,
00167                                    TGLayoutHints* l,
00168                                    Int_t id,
00169                                    GContext_t norm,
00170                                    UInt_t option)
00171 {
00172    FWCustomIconsButton* picButton = new FWCustomIconsButton(p, upPic, downPic, disabledPic, 0, id, norm, option);
00173    if (m_toolTip != "") picButton->SetToolTipText(m_toolTip.c_str(), m_supervisor->getToolTipDelay());
00174    p->AddFrame(picButton, l);
00175    TQObject::Connect(picButton, "Clicked()", "CSGAction", this, "activate()");
00176    m_buttons.push_back(picButton);
00177    if(!isEnabled()) {
00178       picButton->SetEnabled(kFALSE);
00179    }
00180    return picButton;
00181 }
00182 
00183 void CSGAction::createShortcut(UInt_t key, const char *mod, int windowID) {
00184    Int_t keycode = gVirtualX->KeysymToKeycode((int)key);
00185    m_windowID = windowID;
00186    Int_t modcode;
00187    TString scText;
00188    if (strcmp(mod, "CTRL") == 0) {
00189       modcode = kKeyControlMask;
00190       scText = "<ctrl> ";
00191    }
00192    else if (strcmp(mod, "CTRL+SHIFT") == 0) {
00193       modcode = kKeyControlMask | kKeyShiftMask;
00194       scText = "<ctrl> <shift> ";
00195    }
00196    else {
00197       // Default to ALT for now
00198       modcode = kKeyMod1Mask;
00199       scText = "<alt> ";
00200    }
00201    scText += keycodeToString(keycode);
00202    m_scCombo = scText;
00203 
00204    gVirtualX->GrabKey(m_windowID, keycode, modcode, kTRUE);
00205    gVirtualX->GrabKey(m_windowID, keycode, modcode | kKeyMod2Mask, kTRUE);
00206    gVirtualX->GrabKey(m_windowID, keycode, modcode | kKeyLockMask, kTRUE);
00207    gVirtualX->GrabKey(m_windowID, keycode, modcode | kKeyMod2Mask | kKeyLockMask, kTRUE);
00208 
00209    m_keycode = keycode;
00210    m_modcode = modcode;
00211    if (m_menu != 0) addSCToMenu();
00212 }
00213 
00214 void CSGAction::createMenuEntry(TGPopupMenu *menu) {
00215    m_menu = menu;
00216    if (!(menu->HasConnection("Activated(Int_t)"))) TQObject::Connect(menu, "Activated(Int_t)", "CSGConnector", m_connector, "handleMenu(Int_t)");
00217    menu->AddEntry(m_name.c_str(), m_entry);
00218    if (m_keycode != 0) addSCToMenu();
00219    if(!isEnabled()) {
00220       m_menu->DisableEntry(m_entry);
00221    }
00222 }
00223 
00224 void CSGAction::addSCToMenu() {
00225    Bool_t widthChanged = resizeMenuEntry();
00226    if (widthChanged) m_supervisor->resizeMenu(m_menu);
00227 }
00228 
00229 Bool_t CSGAction::resizeMenuEntry() {
00230    FontStruct_t font = gClient->GetResourcePool()->GetMenuHiliteFont()->GetFontStruct();
00231    Bool_t widthChanged = kTRUE;
00232    UInt_t width = m_menu->GetWidth();
00233    TString realName(m_name);
00234    if (realName.Contains("->")) {
00235       // Should make function to do this and store in member data...
00236       while (!(realName.BeginsWith("->")) && realName.Length() > 0) {
00237          realName.Replace(0,1,0,0);
00238       }
00239       realName.Replace(0,2,0,0);
00240    }
00241    TString scText = m_scCombo;
00242    while (gVirtualX->TextWidth(font, realName.Data(), realName.Length()) + gVirtualX->TextWidth(font, scText.Data(), scText.Length()) + 53 < (Int_t)width) {
00243       widthChanged = kFALSE;
00244       realName += " ";
00245    }
00246    realName += "\t";
00247    realName += scText;
00248    TIter next(m_menu->GetListOfEntries());
00249    TGMenuEntry *current;
00250    while (0 != (current = (TGMenuEntry *)next())) {
00251       if (current == m_menu->GetEntry(m_entry)) {
00252          break;
00253       }
00254    }
00255    current = (TGMenuEntry *)next();
00256    m_menu->DeleteEntry(m_entry);
00257    m_menu->AddEntry(realName, m_entry, 0, 0, current);
00258    return widthChanged;
00259 }
00260 
00261 TGPopupMenu* CSGAction::getMenu() const {
00262    return m_menu;
00263 }
00264 
00265 int CSGAction::getMenuEntry() const {
00266    return m_entry;
00267 }
00268 
00269 Int_t CSGAction::getKeycode() const {
00270    return m_keycode;
00271 }
00272 
00273 Int_t CSGAction::getModcode() const {
00274    return m_modcode;
00275 }
00276 
00277 ToolBarData_t* CSGAction::getToolBarData() const {
00278    return m_tools;
00279 }
00280 
00281 TGToolBar* CSGAction::getToolBar() const {
00282    return m_toolBar;
00283 }
00284 
00285 void CSGAction::enable() {
00286    m_enabled = true;
00287    enableImp();
00288 }
00289 
00290 void CSGAction::disable() {
00291    m_enabled = false;
00292    disableImp();
00293 }
00294 
00295 void
00296 CSGAction::globalEnable()
00297 {
00298    m_globalEnabled=true;
00299    enableImp();
00300 }
00301 
00302 void
00303 CSGAction::globalDisable()
00304 {
00305    m_globalEnabled=false;
00306    disableImp();
00307 }
00308 
00309 Bool_t CSGAction::isEnabled() const {
00310    return m_enabled && m_globalEnabled;
00311 }
00312 
00313 void CSGAction::enableImp() {
00314    if(isEnabled()) {
00315       if (m_menu != 0) m_menu->EnableEntry(m_entry);
00316       for(std::vector<TGButton*>::iterator it = m_buttons.begin(), itEnd = m_buttons.end();
00317           it != itEnd;
00318           ++it) {
00319          (*it)->SetEnabled(kTRUE);
00320       }
00321 
00322       if (m_toolBar != 0) m_toolBar->GetButton(m_entry)->SetEnabled(kTRUE);
00323       if (m_keycode != 0) {
00324          gVirtualX->GrabKey(m_windowID, m_keycode, m_modcode, kTRUE);
00325          gVirtualX->GrabKey(m_windowID, m_keycode, m_modcode | kKeyMod2Mask, kTRUE);
00326          gVirtualX->GrabKey(m_windowID, m_keycode, m_modcode | kKeyLockMask, kTRUE);
00327          gVirtualX->GrabKey(m_windowID, m_keycode, m_modcode | kKeyMod2Mask | kKeyLockMask, kTRUE);
00328       }
00329    }
00330 }
00331 
00332 void CSGAction::disableImp() {
00333    if(!isEnabled()) {
00334       if (m_menu != 0) m_menu->DisableEntry(m_entry);
00335       for(std::vector<TGButton*>::iterator it = m_buttons.begin(), itEnd = m_buttons.end();
00336           it != itEnd;
00337           ++it) {
00338          (*it)->SetEnabled(kFALSE);
00339       }
00340       if (m_toolBar != 0) m_toolBar->GetButton(m_entry)->SetEnabled(kFALSE);
00341       if (m_keycode != 0) {
00342          gVirtualX->GrabKey(m_windowID, m_keycode, m_modcode, kFALSE);
00343          gVirtualX->GrabKey(m_windowID, m_keycode, m_modcode | kKeyMod2Mask, kFALSE);
00344          gVirtualX->GrabKey(m_windowID, m_keycode, m_modcode | kKeyLockMask, kFALSE);
00345          gVirtualX->GrabKey(m_windowID, m_keycode, m_modcode | kKeyMod2Mask | kKeyLockMask, kFALSE);
00346       }
00347    }
00348 }
00349 
00350 
00351 //
00352 // static member functions
00353 //
00354 
00355 TString
00356 CSGAction::keycodeToString(Int_t keycode) {
00357    int i;
00358    char letter;
00359    TString rep;
00360    for (i = kKey_a; i < kKey_a + 26; i++) {
00361       if (gVirtualX->KeysymToKeycode(i) == keycode) {
00362          letter = (char)(i - kKey_a + 'a');
00363          rep = TString(letter);
00364          return rep;
00365       }
00366    }
00367    for (i = kKey_A; i < kKey_A + 26; i++) {
00368       if(gVirtualX->KeysymToKeycode(i) == keycode) {
00369          letter = (char)(i - kKey_A + 'a');
00370          rep = TString(letter);
00371          return rep;
00372       }
00373    }
00374    if (keycode == gVirtualX->KeysymToKeycode(kKey_Left)) {
00375       rep = TString("<-");
00376       return rep;
00377    }
00378    if (keycode == gVirtualX->KeysymToKeycode(kKey_Right)) {
00379       rep = TString("->");
00380       return rep;
00381    }
00382    rep = TString("");
00383    return rep;
00384 }
00385