Logo ROOT  
Reference Guide
splitbuttonTest.C File Reference

Detailed Description

A simple example that shows the usage of a TGSplitButton. The checkbutton is used to change the split state of the button.

#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.
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.
0, 10, 0, 0)) ;
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);
}
Author
Roel Aaij 13/07/2007

Definition in file splitbuttonTest.C.

TGWindow
Definition: TGWindow.h:31
kLHintsCenterX
@ kLHintsCenterX
Definition: TGLayout.h:38
TGCheckButton::SetState
virtual void SetState(EButtonState state, Bool_t emit=kFALSE)
Set check button state.
Definition: TGButton.cxx:1203
TGMainFrame
Definition: TGFrame.h:444
kButtonDown
@ kButtonDown
Definition: TGButton.h:60
TGLayout.h
TGHotString
Definition: TGString.h:52
kButtonUp
@ kButtonUp
Definition: TGButton.h:59
Int_t
int Int_t
Definition: RtypesCore.h:45
TGHorizontalFrame
Definition: TGFrame.h:423
Bool_t
bool Bool_t
Definition: RtypesCore.h:63
TString.h
TGWindow.h
gClient
#define gClient
Definition: TGClient.h:166
TApplication.h
TGCheckButton
Definition: TGButton.h:264
kLHintsCenterY
@ kLHintsCenterY
Definition: TGLayout.h:41
h
#define h(i)
Definition: RSha256.hxx:124
TGVerticalFrame
Definition: TGFrame.h:412
TGFrame.h
TGLayoutHints
Definition: TGLayout.h:57
gApplication
R__EXTERN TApplication * gApplication
Definition: TApplication.h:166
UInt_t
unsigned int UInt_t
Definition: RtypesCore.h:46
TApplication::Terminate
virtual void Terminate(Int_t status=0)
TGClient.h
TGLabel.h
TQObject::Connect
Bool_t Connect(const char *signal, const char *receiver_class, void *receiver, const char *slot)
Non-static method is used to connect from the signal of this object to the receiver slot.
Definition: TQObject.cxx:864
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
TGButton.h
TGPopupMenu
Definition: TGMenu.h:123
kDeepCleanup
@ kDeepCleanup
Definition: TGFrame.h:51
TGSplitButton
Definition: TGButton.h:379
TGCompositeFrame::AddFrame
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=0)
Add frame to the composite frame using the specified layout hints.
Definition: TGFrame.cxx:1102
TGMenu.h
kButtonDisabled
@ kButtonDisabled
Definition: TGButton.h:62