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 
21 class MyMainFrame : public TGMainFrame {
22 
23 private:
24  TGListBox *fListBox;
25  TGCheckButton *fCheckMulti;
26  TList *fSelected;
27 
28 public:
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 
39 void MyMainFrame::DoSelect()
40 {
41  Printf("Slot DoSelect()");
42 }
43 
44 void MyMainFrame::DoExit()
45 {
46  Printf("Slot DoExit()");
48 }
49 
50 MyMainFrame::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 
94 MyMainFrame::~MyMainFrame()
95 {
96  // Clean up main frame...
97  Cleanup();
98  if (fSelected) {
99  fSelected->Delete();
100  delete fSelected;
101  }
102 }
103 
104 void 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 
118 void 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 
133 void listBox()
134 {
135  // Popup the GUI...
136  new MyMainFrame(gClient->GetRoot(), 200, 200);
137 }
TGButton::SetToolTipText
virtual void SetToolTipText(const char *text, Long_t delayms=400)
Set tool tip text associated with this button.
Definition: TGButton.cxx:398
TGListBox
Definition: TGListBox.h:288
TGWindow
Definition: TGWindow.h:31
kFixedWidth
@ kFixedWidth
Definition: GuiTypes.h:387
TGMainFrame
Definition: TGFrame.h:443
kLHintsTop
@ kLHintsTop
Definition: TGLayout.h:34
kLHintsLeft
@ kLHintsLeft
Definition: TGLayout.h:31
TGTextButton
Definition: TGButton.h:142
gTQSender
R__EXTERN void * gTQSender
Definition: TQObject.h:44
Int_t
int Int_t
Definition: RtypesCore.h:45
TList.h
TGHorizontalFrame
Definition: TGFrame.h:422
id
XFontStruct * id
Definition: TGX11.cxx:109
gClient
#define gClient
Definition: TGClient.h:166
TApplication.h
TGCheckButton
Definition: TGButton.h:264
h
#define h(i)
Definition: RSha256.hxx:106
TGWidget::WidgetId
Int_t WidgetId() const
Definition: TGWidget.h:78
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)
kLHintsExpandY
@ kLHintsExpandY
Definition: TGLayout.h:38
TGClient.h
TGListBox.h
Printf
void Printf(const char *fmt,...)
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:866
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
TGButton.h
kLHintsExpandX
@ kLHintsExpandX
Definition: TGLayout.h:37
TGButton
Definition: TGButton.h:68
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
TList
A doubly linked list.
Definition: TList.h:44