ROOT logo

From $ROOTSYS/tutorials/gui/splitbuttonTest.C

// A simple example that shows the usage of a TGSplitButton. 
// The checkbutton is used to change the split state of the button.
//
// author, Roel Aaij 13/07/2007

#include <iostream>
#include <TApplication.h>
#include <TGClient.h>
#include <TGButton.h>
#include <TGFrame.h>
#include <TGLayout.h>
#include <TGWindow.h>
#include <TGLabel.h>
#include <TString.h>
#include <TGMenu.h>

// A little class to automatically handle the generation of unique
// widget ids.
enum EMenuIds {
   ID_1,
   ID_2,
   ID_3,
   ID_4,
   ID_5
};

class IDList {
private:
   Int_t nID ;               // Generates unique widget IDs.
public:
   IDList() : nID(0) {}
   ~IDList() {}
   Int_t GetUnID(void) { return ++nID ; }
} ;


class SplitButtonTest : public TGMainFrame {

private:
   TGSplitButton *fMButton;  // Split Button
   TGPopupMenu   *fPopMenu;  // TGpopupMenu that will be attached to
                             // the button.
   IDList         IDs ;      // Generator for unique widget IDs.
   
public:
   SplitButtonTest(const TGWindow *p, UInt_t w, UInt_t h) ;
   virtual ~SplitButtonTest() ;

   void DoExit() ;
   void DoSplit(Bool_t split) ;
   void DoEnable(Bool_t on) ;
   void HandleMenu(Int_t id) ;

   ClassDef(SplitButtonTest, 0)
};
                          
SplitButtonTest::SplitButtonTest(const TGWindow *p, UInt_t w, UInt_t h) 
   : TGMainFrame(p, w, h)   
{
   SetCleanup(kDeepCleanup) ;
   
   Connect("CloseWindow()", "SplitButtonTest", this, "DoExit()") ;
   DontCallClose() ;

   TGVerticalFrame *fVL = new TGVerticalFrame(this, 100, 100) ;
   TGHorizontalFrame *fHL = new TGHorizontalFrame(fVL, 100, 40) ;

   // Create a popup menu.
   fPopMenu = new TGPopupMenu(gClient->GetRoot());
   fPopMenu->AddEntry("Button &1", ID_1);
   fPopMenu->AddEntry("Button &2", ID_2);
   fPopMenu->DisableEntry(ID_2);
   fPopMenu->AddEntry("Button &3", ID_3);
   fPopMenu->AddSeparator();
   
   // Create a split button, the menu is adopted.
   fMButton = new TGSplitButton(fHL, new TGHotString("Button &Options"), 
                                fPopMenu, IDs.GetUnID());

   // It is possible to add entries later
   fPopMenu->AddEntry("En&try with really really long name", ID_4);
   fPopMenu->AddEntry("&Exit", ID_5);   
   
   // Connect the special signal for the activation of items in a menu
   // that belongs to a split button to the slot.
   fMButton->Connect("ItemClicked(Int_t)", "SplitButtonTest", this, 
                     "HandleMenu(Int_t)");

   TGCheckButton *fCButton = new TGCheckButton(fHL, new TGHotString("Split"), 
                                               IDs.GetUnID());
   fCButton->SetState(kButtonDown);
   fCButton->Connect("Toggled(Bool_t)", "SplitButtonTest", this, "DoSplit(Bool_t)");

   // Add frames to their parent for layout.
   fHL->AddFrame(fCButton, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY, 
                                             0, 10, 0, 0)) ;        
   TGCheckButton *fEButton = new TGCheckButton(fHL, new TGHotString("Enable"), 
                                               IDs.GetUnID());
   fEButton->SetState(kButtonDown);
   fEButton->Connect("Toggled(Bool_t)", "SplitButtonTest", this, "DoEnable(Bool_t)");

   // Add frames to their parent for layout.
   fHL->AddFrame(fEButton, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY, 
                                             0, 10, 0, 0)) ;        
   fHL->AddFrame(fMButton, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY));
   fVL->AddFrame(fHL, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY)) ;
   AddFrame(fVL, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY)) ;

   SetWindowName("SplitButton Test") ;
   MapSubwindows() ;
   Resize(GetDefaultSize()) ;
   MapWindow() ;

} ;

SplitButtonTest::~SplitButtonTest()
{
   // Destructor
   Cleanup() ;
}

void SplitButtonTest::DoExit()
{
   // Exit this application via the Exit button or Window Manager.
   // Use one of the both lines according to your needs.
   // Please note to re-run this macro in the same ROOT session,
   // you have to compile it to get signals/slots 'on place'.
   
   //DeleteWindow();            // to stay in the ROOT session
   gApplication->Terminate();   // to exit and close the ROOT session   
}

void SplitButtonTest::DoSplit(Bool_t split)
{
   fMButton->SetSplit(split);
}

void SplitButtonTest::DoEnable(Bool_t on)
{
   if (on)
      fMButton->SetState(kButtonUp);
   else
      fMButton->SetState(kButtonDisabled);
}

void SplitButtonTest::HandleMenu(Int_t id) 
{
   // Activation of menu items in the popup menu are handled in a user
   // defined slot to which the ItemClicked(Int_t) signal is
   // connected.

   switch (id) {
   case ID_1:
      std::cout << "Button 1 was activated" << std::endl;
      break;
   case ID_2:
      std::cout << "Button 2 was activated" << std::endl;
      break;
   case ID_3:
      std::cout << "Button 3 was activated" << std::endl;
      break;
   case ID_4:
      std::cout << "Button with a really really long name was activated" 
                << std::endl;
      break;
   case ID_5:
      DoExit();
      break;
   }
}
void splitbuttonTest() 
{
   new SplitButtonTest(gClient->GetRoot(),100,100);
}

 splitbuttonTest.C:1
 splitbuttonTest.C:2
 splitbuttonTest.C:3
 splitbuttonTest.C:4
 splitbuttonTest.C:5
 splitbuttonTest.C:6
 splitbuttonTest.C:7
 splitbuttonTest.C:8
 splitbuttonTest.C:9
 splitbuttonTest.C:10
 splitbuttonTest.C:11
 splitbuttonTest.C:12
 splitbuttonTest.C:13
 splitbuttonTest.C:14
 splitbuttonTest.C:15
 splitbuttonTest.C:16
 splitbuttonTest.C:17
 splitbuttonTest.C:18
 splitbuttonTest.C:19
 splitbuttonTest.C:20
 splitbuttonTest.C:21
 splitbuttonTest.C:22
 splitbuttonTest.C:23
 splitbuttonTest.C:24
 splitbuttonTest.C:25
 splitbuttonTest.C:26
 splitbuttonTest.C:27
 splitbuttonTest.C:28
 splitbuttonTest.C:29
 splitbuttonTest.C:30
 splitbuttonTest.C:31
 splitbuttonTest.C:32
 splitbuttonTest.C:33
 splitbuttonTest.C:34
 splitbuttonTest.C:35
 splitbuttonTest.C:36
 splitbuttonTest.C:37
 splitbuttonTest.C:38
 splitbuttonTest.C:39
 splitbuttonTest.C:40
 splitbuttonTest.C:41
 splitbuttonTest.C:42
 splitbuttonTest.C:43
 splitbuttonTest.C:44
 splitbuttonTest.C:45
 splitbuttonTest.C:46
 splitbuttonTest.C:47
 splitbuttonTest.C:48
 splitbuttonTest.C:49
 splitbuttonTest.C:50
 splitbuttonTest.C:51
 splitbuttonTest.C:52
 splitbuttonTest.C:53
 splitbuttonTest.C:54
 splitbuttonTest.C:55
 splitbuttonTest.C:56
 splitbuttonTest.C:57
 splitbuttonTest.C:58
 splitbuttonTest.C:59
 splitbuttonTest.C:60
 splitbuttonTest.C:61
 splitbuttonTest.C:62
 splitbuttonTest.C:63
 splitbuttonTest.C:64
 splitbuttonTest.C:65
 splitbuttonTest.C:66
 splitbuttonTest.C:67
 splitbuttonTest.C:68
 splitbuttonTest.C:69
 splitbuttonTest.C:70
 splitbuttonTest.C:71
 splitbuttonTest.C:72
 splitbuttonTest.C:73
 splitbuttonTest.C:74
 splitbuttonTest.C:75
 splitbuttonTest.C:76
 splitbuttonTest.C:77
 splitbuttonTest.C:78
 splitbuttonTest.C:79
 splitbuttonTest.C:80
 splitbuttonTest.C:81
 splitbuttonTest.C:82
 splitbuttonTest.C:83
 splitbuttonTest.C:84
 splitbuttonTest.C:85
 splitbuttonTest.C:86
 splitbuttonTest.C:87
 splitbuttonTest.C:88
 splitbuttonTest.C:89
 splitbuttonTest.C:90
 splitbuttonTest.C:91
 splitbuttonTest.C:92
 splitbuttonTest.C:93
 splitbuttonTest.C:94
 splitbuttonTest.C:95
 splitbuttonTest.C:96
 splitbuttonTest.C:97
 splitbuttonTest.C:98
 splitbuttonTest.C:99
 splitbuttonTest.C:100
 splitbuttonTest.C:101
 splitbuttonTest.C:102
 splitbuttonTest.C:103
 splitbuttonTest.C:104
 splitbuttonTest.C:105
 splitbuttonTest.C:106
 splitbuttonTest.C:107
 splitbuttonTest.C:108
 splitbuttonTest.C:109
 splitbuttonTest.C:110
 splitbuttonTest.C:111
 splitbuttonTest.C:112
 splitbuttonTest.C:113
 splitbuttonTest.C:114
 splitbuttonTest.C:115
 splitbuttonTest.C:116
 splitbuttonTest.C:117
 splitbuttonTest.C:118
 splitbuttonTest.C:119
 splitbuttonTest.C:120
 splitbuttonTest.C:121
 splitbuttonTest.C:122
 splitbuttonTest.C:123
 splitbuttonTest.C:124
 splitbuttonTest.C:125
 splitbuttonTest.C:126
 splitbuttonTest.C:127
 splitbuttonTest.C:128
 splitbuttonTest.C:129
 splitbuttonTest.C:130
 splitbuttonTest.C:131
 splitbuttonTest.C:132
 splitbuttonTest.C:133
 splitbuttonTest.C:134
 splitbuttonTest.C:135
 splitbuttonTest.C:136
 splitbuttonTest.C:137
 splitbuttonTest.C:138
 splitbuttonTest.C:139
 splitbuttonTest.C:140
 splitbuttonTest.C:141
 splitbuttonTest.C:142
 splitbuttonTest.C:143
 splitbuttonTest.C:144
 splitbuttonTest.C:145
 splitbuttonTest.C:146
 splitbuttonTest.C:147
 splitbuttonTest.C:148
 splitbuttonTest.C:149
 splitbuttonTest.C:150
 splitbuttonTest.C:151
 splitbuttonTest.C:152
 splitbuttonTest.C:153
 splitbuttonTest.C:154
 splitbuttonTest.C:155
 splitbuttonTest.C:156
 splitbuttonTest.C:157
 splitbuttonTest.C:158
 splitbuttonTest.C:159
 splitbuttonTest.C:160
 splitbuttonTest.C:161
 splitbuttonTest.C:162
 splitbuttonTest.C:163
 splitbuttonTest.C:164
 splitbuttonTest.C:165
 splitbuttonTest.C:166
 splitbuttonTest.C:167
 splitbuttonTest.C:168
 splitbuttonTest.C:169
 splitbuttonTest.C:170
 splitbuttonTest.C:171
 splitbuttonTest.C:172
 splitbuttonTest.C:173
 splitbuttonTest.C:174
 splitbuttonTest.C:175
 splitbuttonTest.C:176