Logo ROOT  
Reference Guide
listBox.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_gui
3/// This macro gives an example of how to create a list box and how to set and use its multiple selection feature.
4/// To run it do either:
5/// ~~~
6/// .x listBox.C
7/// .x listBox.C++
8/// ~~~
9///
10/// \macro_code
11///
12/// \author Ilka Antcheva 1/12/2006
13
14
15#include <TApplication.h>
16#include <TGClient.h>
17#include <TGButton.h>
18#include <TGListBox.h>
19#include <TList.h>
20
21class MyMainFrame : public TGMainFrame {
22
23private:
24 TGListBox *fListBox;
25 TGCheckButton *fCheckMulti;
26 TList *fSelected;
27
28public:
29 MyMainFrame(const TGWindow *p, UInt_t w, UInt_t h);
30 virtual ~MyMainFrame();
31 void DoExit();
32 void DoSelect();
33 void HandleButtons();
34 void PrintSelected();
35
36 ClassDef(MyMainFrame, 0)
37};
38
39void MyMainFrame::DoSelect()
40{
41 Printf("Slot DoSelect()");
42}
43
44void MyMainFrame::DoExit()
45{
46 Printf("Slot DoExit()");
48}
49
50MyMainFrame::MyMainFrame(const TGWindow *p, UInt_t w, UInt_t h) :
51 TGMainFrame(p, w, h)
52{
53 // Create main frame
54
55 fListBox = new TGListBox(this, 89);
56 fSelected = new TList;
57 char tmp[20];
58 for (int i = 0; i < 20; ++i) {
59 sprintf(tmp, "Entry %i", i+1);
60 fListBox->AddEntry(tmp, i+1);
61 }
62 fListBox->Resize(100,150);
63 AddFrame(fListBox, new TGLayoutHints(kLHintsTop | kLHintsLeft |
65 5, 5, 5, 5));
66
67 fCheckMulti = new TGCheckButton(this, "&Mutliple selection", 10);
68 AddFrame(fCheckMulti, new TGLayoutHints(kLHintsTop | kLHintsLeft,
69 5, 5, 5, 5));
70 fCheckMulti->Connect("Clicked()", "MyMainFrame", this, "HandleButtons()");
71 // Create a horizontal frame containing button(s)
72 TGHorizontalFrame *hframe = new TGHorizontalFrame(this, 150, 20, kFixedWidth);
73 TGTextButton *show = new TGTextButton(hframe, "&Show");
74 show->SetToolTipText("Click here to print the selection you made");
75 show->Connect("Pressed()", "MyMainFrame", this, "PrintSelected()");
76 hframe->AddFrame(show, new TGLayoutHints(kLHintsExpandX, 5, 5, 3, 4));
77 TGTextButton *exit = new TGTextButton(hframe, "&Exit ");
78 exit->Connect("Pressed()", "MyMainFrame", this, "DoExit()");
79 hframe->AddFrame(exit, new TGLayoutHints(kLHintsExpandX, 5, 5, 3, 4));
80 AddFrame(hframe, new TGLayoutHints(kLHintsExpandX, 2, 2, 5, 1));
81
82 // Set a name to the main frame
83 SetWindowName("List Box");
84 MapSubwindows();
85
86 // Initialize the layout algorithm via Resize()
87 Resize(GetDefaultSize());
88
89 // Map main frame
90 MapWindow();
91 fListBox->Select(1);
92}
93
94MyMainFrame::~MyMainFrame()
95{
96 // Clean up main frame...
97 Cleanup();
98 if (fSelected) {
99 fSelected->Delete();
100 delete fSelected;
101 }
102}
103
104void MyMainFrame::HandleButtons()
105{
106 // Handle check button.
107 Int_t id;
108 TGButton *btn = (TGButton *) gTQSender;
109 id = btn->WidgetId();
110
111 printf("HandleButton: id = %d\n", id);
112
113 if (id == 10)
114 fListBox->SetMultipleSelections(fCheckMulti->GetState());
115}
116
117
118void MyMainFrame::PrintSelected()
119{
120 // Writes selected entries in TList if multiselection.
121
122 fSelected->Clear();
123
124 if (fListBox->GetMultipleSelections()) {
125 Printf("Selected entries are:\n");
126 fListBox->GetSelectedEntries(fSelected);
127 fSelected->ls();
128 } else {
129 Printf("Selected entries is: %d\n", fListBox->GetSelected());
130 }
131}
132
133void listBox()
134{
135 // Popup the GUI...
136 new MyMainFrame(gClient->GetRoot(), 200, 200);
137}
@ kFixedWidth
Definition: GuiTypes.h:387
#define h(i)
Definition: RSha256.hxx:106
int Int_t
Definition: RtypesCore.h:43
unsigned int UInt_t
Definition: RtypesCore.h:44
#define ClassDef(name, id)
Definition: Rtypes.h:322
R__EXTERN TApplication * gApplication
Definition: TApplication.h:166
#define gClient
Definition: TGClient.h:166
@ kLHintsExpandY
Definition: TGLayout.h:38
@ kLHintsLeft
Definition: TGLayout.h:31
@ kLHintsTop
Definition: TGLayout.h:34
@ kLHintsExpandX
Definition: TGLayout.h:37
XFontStruct * id
Definition: TGX11.cxx:108
R__EXTERN void * gTQSender
Definition: TQObject.h:44
void Printf(const char *fmt,...)
virtual void Terminate(Int_t status=0)
Terminate the application by call TSystem::Exit() unless application has been told to return from Run...
virtual void SetToolTipText(const char *text, Long_t delayms=400)
Set tool tip text associated with this button.
Definition: TGButton.cxx:397
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=0)
Add frame to the composite frame using the specified layout hints.
Definition: TGFrame.cxx:1101
Int_t WidgetId() const
Definition: TGWidget.h:80
A doubly linked list.
Definition: TList.h:44
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:866