splitbuttonTest.C

Go to the documentation of this file.
00001 // A simple example that shows the usage of a TGSplitButton. 
00002 // The checkbutton is used to change the split state of the button.
00003 //
00004 // author, Roel Aaij 13/07/2007
00005 
00006 #include <iostream>
00007 #include <TApplication.h>
00008 #include <TGClient.h>
00009 #include <TGButton.h>
00010 #include <TGFrame.h>
00011 #include <TGLayout.h>
00012 #include <TGWindow.h>
00013 #include <TGLabel.h>
00014 #include <TString.h>
00015 #include <TGMenu.h>
00016 
00017 // A little class to automatically handle the generation of unique
00018 // widget ids.
00019 enum EMenuIds {
00020    ID_1,
00021    ID_2,
00022    ID_3,
00023    ID_4,
00024    ID_5
00025 };
00026 
00027 class IDList {
00028 private:
00029    Int_t nID ;               // Generates unique widget IDs.
00030 public:
00031    IDList() : nID(0) {}
00032    ~IDList() {}
00033    Int_t GetUnID(void) { return ++nID ; }
00034 } ;
00035 
00036 
00037 class SplitButtonTest : public TGMainFrame {
00038 
00039 private:
00040    TGSplitButton *fMButton;  // Split Button
00041    TGPopupMenu   *fPopMenu;  // TGpopupMenu that will be attached to
00042                              // the button.
00043    IDList         IDs ;      // Generator for unique widget IDs.
00044    
00045 public:
00046    SplitButtonTest(const TGWindow *p, UInt_t w, UInt_t h) ;
00047    virtual ~SplitButtonTest() ;
00048 
00049    void DoExit() ;
00050    void DoSplit(Bool_t split) ;
00051    void DoEnable(Bool_t on) ;
00052    void HandleMenu(Int_t id) ;
00053 
00054    ClassDef(SplitButtonTest, 0)
00055 };
00056                           
00057 SplitButtonTest::SplitButtonTest(const TGWindow *p, UInt_t w, UInt_t h) 
00058    : TGMainFrame(p, w, h)   
00059 {
00060    SetCleanup(kDeepCleanup) ;
00061    
00062    Connect("CloseWindow()", "SplitButtonTest", this, "DoExit()") ;
00063    DontCallClose() ;
00064 
00065    TGVerticalFrame *fVL = new TGVerticalFrame(this, 100, 100) ;
00066    TGHorizontalFrame *fHL = new TGHorizontalFrame(fVL, 100, 40) ;
00067 
00068    // Create a popup menu.
00069    fPopMenu = new TGPopupMenu(gClient->GetRoot());
00070    fPopMenu->AddEntry("Button &1", ID_1);
00071    fPopMenu->AddEntry("Button &2", ID_2);
00072    fPopMenu->DisableEntry(ID_2);
00073    fPopMenu->AddEntry("Button &3", ID_3);
00074    fPopMenu->AddSeparator();
00075    
00076    // Create a split button, the menu is adopted.
00077    fMButton = new TGSplitButton(fHL, new TGHotString("Button &Options"), 
00078                                 fPopMenu, IDs.GetUnID());
00079 
00080    // It is possible to add entries later
00081    fPopMenu->AddEntry("En&try with really really long name", ID_4);
00082    fPopMenu->AddEntry("&Exit", ID_5);   
00083    
00084    // Connect the special signal for the activation of items in a menu
00085    // that belongs to a split button to the slot.
00086    fMButton->Connect("ItemClicked(Int_t)", "SplitButtonTest", this, 
00087                      "HandleMenu(Int_t)");
00088 
00089    TGCheckButton *fCButton = new TGCheckButton(fHL, new TGHotString("Split"), 
00090                                                IDs.GetUnID());
00091    fCButton->SetState(kButtonDown);
00092    fCButton->Connect("Toggled(Bool_t)", "SplitButtonTest", this, "DoSplit(Bool_t)");
00093 
00094    // Add frames to their parent for layout.
00095    fHL->AddFrame(fCButton, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY, 
00096                                              0, 10, 0, 0)) ;        
00097    TGCheckButton *fEButton = new TGCheckButton(fHL, new TGHotString("Enable"), 
00098                                                IDs.GetUnID());
00099    fEButton->SetState(kButtonDown);
00100    fEButton->Connect("Toggled(Bool_t)", "SplitButtonTest", this, "DoEnable(Bool_t)");
00101 
00102    // Add frames to their parent for layout.
00103    fHL->AddFrame(fEButton, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY, 
00104                                              0, 10, 0, 0)) ;        
00105    fHL->AddFrame(fMButton, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY));
00106    fVL->AddFrame(fHL, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY)) ;
00107    AddFrame(fVL, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY)) ;
00108 
00109    SetWindowName("SplitButton Test") ;
00110    MapSubwindows() ;
00111    Resize(GetDefaultSize()) ;
00112    MapWindow() ;
00113 
00114 } ;
00115 
00116 SplitButtonTest::~SplitButtonTest()
00117 {
00118    // Destructor
00119    Cleanup() ;
00120 }
00121 
00122 void SplitButtonTest::DoExit()
00123 {
00124    // Exit this application via the Exit button or Window Manager.
00125    // Use one of the both lines according to your needs.
00126    // Please note to re-run this macro in the same ROOT session,
00127    // you have to compile it to get signals/slots 'on place'.
00128    
00129    //DeleteWindow();            // to stay in the ROOT session
00130    gApplication->Terminate();   // to exit and close the ROOT session   
00131 }
00132 
00133 void SplitButtonTest::DoSplit(Bool_t split)
00134 {
00135    fMButton->SetSplit(split);
00136 }
00137 
00138 void SplitButtonTest::DoEnable(Bool_t on)
00139 {
00140    if (on)
00141       fMButton->SetState(kButtonUp);
00142    else
00143       fMButton->SetState(kButtonDisabled);
00144 }
00145 
00146 void SplitButtonTest::HandleMenu(Int_t id) 
00147 {
00148    // Activation of menu items in the popup menu are handled in a user
00149    // defined slot to which the ItemClicked(Int_t) signal is
00150    // connected.
00151 
00152    switch (id) {
00153    case ID_1:
00154       std::cout << "Button 1 was activated" << std::endl;
00155       break;
00156    case ID_2:
00157       std::cout << "Button 2 was activated" << std::endl;
00158       break;
00159    case ID_3:
00160       std::cout << "Button 3 was activated" << std::endl;
00161       break;
00162    case ID_4:
00163       std::cout << "Button with a really really long name was activated" 
00164                 << std::endl;
00165       break;
00166    case ID_5:
00167       DoExit();
00168       break;
00169    }
00170 }
00171 void splitbuttonTest() 
00172 {
00173    new SplitButtonTest(gClient->GetRoot(),100,100);
00174 }
00175 

Generated on Tue Jul 5 15:44:26 2011 for ROOT_528-00b_version by  doxygen 1.5.1