ListBox

Hi
I have the following macro for a simple listbox.

#include <TROOT.h>
#include <TGClient.h>
#include <TGFrame.h>
#include <TGLabel.h>
#include <TGComboBox.h>
#include <TGListBox.h>
#include <RQ_OBJECT.h>

class MainFrame
{
	RQ_OBJECT("MainFrame")
	private:
		TGMainFrame		*fMain;
		TGCompositeFrame	*fF1;
		TGListBox  		*fListBox;
		TGLayoutHints		*fL1,*fL2;
		TGCheckButton 		*fCheckMulti;
		
	public:
		MainFrame(const TGWindow *p,UInt_t w,UInt_t h);
		virtual ~MainFrame();
		void MultiSelect();
};

MainFrame::MainFrame(const TGWindow *p,UInt_t w,UInt_t h)
{
	fMain = new TGMainFrame(p,w,h);
	fMain->SetWindowName("ListBox"); 
	fMain->ChangeOptions((fMain->GetOptions() & ~kVerticalFrame) | kHorizontalFrame);

	fF1 = new TGCompositeFrame(fMain, 100, 20, kVerticalFrame);
			
	//define frames/text/button layout
	fL1 = new TGLayoutHints(kLHintsTop | kLHintsLeft,20,20,20,20);
	fL2 = new TGLayoutHints(kLHintsTop | kLHintsLeft,20,20,30,20);
	
	fF1->AddFrame(new TGLabel(fF1, new TGString("List of Fruits")),
		      new TGLayoutHints(kLHintsTop | kLHintsLeft,20,20,0,5));	
	
	fF1->AddFrame(fListBox = new TGListBox(fF1, 1));
	fListBox->AddEntry("- Apples", 100);
	fListBox->AddEntry("- Oranges", 101);
	fListBox->AddEntry("- Strawbery", 102);
	fListBox->AddEntry("- Grapes", 103);
	fListBox->AddEntry("- Melon", 104);
	fListBox->AddEntry("- Olive", 105);
	fListBox->AddEntry("- Guava", 106);
	fListBox->AddEntry("- Peach", 107);
	fListBox->AddEntry("- Mango", 108);
	fListBox->AddEntry("- Lemon", 109);
	
	fListBox->Resize(100, 100);

	fF1->AddFrame(fCheckMulti = new TGCheckButton(fMain, "&Multi-Selectable", 10), fL2);
	fCheckMulti->Connect("Clicked()", "MainFrame", this, "MultiSelect()");
	
	fMain->AddFrame(fF1,fL1);

	fMain->MapSubwindows();
	fMain->Resize(140,200);
	fMain->MapWindow();
}

MainFrame::~MainFrame()
{
	fMain->Cleanup();
 	delete fMain;
}

void MainFrame::MultiSelect()
{
   	fListBox->SetMultipleSelections(fCheckMulti->GetState());
}

void listbox()
{
   	new MainFrame(gClient->GetRoot(), 200, 100);
}

I’m stuck with how to display on the console(using std::cout) the listbox entries that are selected. That is I want to output them every time they are selected. The only solution that I can think of is by making a series of if…else statements that go through all the entries checking whether they are selected, but I want to avoid that.
Any suggestions of how I can tackle this?

Cheers

That is what I will do.
You can see the changes to implement to have single click or double click.
Cheers!

[code]

#include <TROOT.h>
#include <TGClient.h>
#include <TGFrame.h>
#include <TGLabel.h>
#include <TGComboBox.h>
#include <TGListBox.h>
#include <RQ_OBJECT.h>

class MainFrame
{
RQ_OBJECT(“MainFrame”)
private:
TGMainFrame *fMain;
TGCompositeFrame *fF1;
TGListBox *fListBox;
TGLayoutHints *fL1,*fL2;
TGCheckButton *fCheckMulti;

public:
MainFrame(const TGWindow *p,UInt_t w,UInt_t h);
virtual ~MainFrame();
void MultiSelect();
};

MainFrame::MainFrame(const TGWindow *p,UInt_t w,UInt_t h)
{
fMain = new TGMainFrame(p,w,h);
fMain->SetWindowName(“ListBox”);
fMain->ChangeOptions((fMain->GetOptions() & ~kVerticalFrame) | kHorizontalFrame);

fF1 = new TGCompositeFrame(fMain, 100, 20, kVerticalFrame);

//define frames/text/button layout
fL1 = new TGLayoutHints(kLHintsTop | kLHintsLeft,20,20,20,20);
fL2 = new TGLayoutHints(kLHintsTop | kLHintsLeft,20,20,30,20);

fF1->AddFrame(new TGLabel(fF1, new TGString(“List of Fruits”)),
new TGLayoutHints(kLHintsTop | kLHintsLeft,20,20,0,5));

fF1->AddFrame(fListBox = new TGListBox(fF1, 1));
fListBox->AddEntry("- Apples", 100);
fListBox->AddEntry("- Oranges", 101);
fListBox->AddEntry("- Strawbery", 102);
fListBox->AddEntry("- Grapes", 103);
fListBox->AddEntry("- Melon", 104);
fListBox->AddEntry("- Olive", 105);
fListBox->AddEntry("- Guava", 106);
fListBox->AddEntry("- Peach", 107);
fListBox->AddEntry("- Mango", 108);
fListBox->AddEntry("- Lemon", 109);

fListBox->Resize(100, 100);

//#########USE CLICK OR DOUBLE CLICK
//This next prints single peak information based on a single mouse click
fListBox->Connect(“Selected(Int_t)”,“MainFrame”,this,“PrintPeakEntry(Int_t)”);

//This next prints single peak information based on a double mouse click
//fListBox->GetContainer()->Connect(“DoubleClicked(TGFrame*, Int_t)”, “MainFrame”,this, “PrintPeakEntry(TGFrame*, Int_t)”);

fF1->AddFrame(fCheckMulti = new TGCheckButton(fMain, “&Multi-Selectable”, 10), fL2);
fCheckMulti->Connect(“Clicked()”, “MainFrame”, this, “MultiSelect()”);

fMain->AddFrame(fF1,fL1);

fMain->MapSubwindows();
fMain->Resize(140,200);
fMain->MapWindow();
}

MainFrame::~MainFrame()
{
fMain->Cleanup();
delete fMain;
}

void MainFrame::MultiSelect()
{
fListBox->SetMultipleSelections(fCheckMulti->GetState());
}

void MainFrame::PrintPeakEntry(Int_t selectedID){
TList *fSelected = new TList;
char str[128];

MainFrame::fListBox->GetSelectedEntries(fSelected);
if(fSelected->GetSize()!=0){

 if (fListBox->GetMultipleSelections()) {
   Printf("Selected entries are:\n");
   fSelected->ls();
 } 
 else {
   strcpy(str,((TGTextLBEntry*)fListBox->GetEntry(fListBox->GetSelected()))->GetText()->GetString());
   Printf("Selected entry is: %d", fListBox->GetSelected());       
   Printf("Value is %s\n",str);
 }

}

delete fSelected;

}

void MainFrame::PrintPeakEntry(TGFrame *f, Int_t btn){

//// HandleContainer slot
TGTextLBEntry *entry = (TGTextLBEntry *)f;
Printf(“Slot HandleContainer DBL Clicked (%s, %d)”, entry->GetTitle(), btn);
//MainFrame::PrintPeakEntry(entry->EntryId());
}

void test()
{
new MainFrame(gClient->GetRoot(), 200, 100);
}[/code]

Thank you very much.