#include #include #include #include #include class MyMainFrame : public TGMainFrame { private: TGListBox *fListBox; TGCheckButton *fCheckMulti; TList *fSelected; public: enum ESeverity { kError, kWarning, kInfo, kPrint }; MyMainFrame(const TGWindow *p, UInt_t w, UInt_t h); virtual ~MyMainFrame(); void DoExit(); void DoSelect(); void HandleButtons(); void PrintSelected(); void PrintInListBox(const char *mess, ESeverity Type); ClassDef(MyMainFrame, 0) }; void MyMainFrame::DoSelect() { Printf("Slot DoSelect()"); } void MyMainFrame::DoExit() { Printf("Slot DoExit()"); gApplication->Terminate(0); } MyMainFrame::MyMainFrame(const TGWindow *p, UInt_t w, UInt_t h) : TGMainFrame(p, w, h) { // Create main frame fListBox = new TGListBox(this, 89); fSelected = new TList; char tmp[20]; for (int i = 0; i < 5; ++i) { sprintf(tmp, "Entry %i", i+1); fListBox->AddEntry(tmp, i+1); } fListBox->Resize(100,150); AddFrame(fListBox, new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX | kLHintsExpandY, 5, 5, 5, 5)); fCheckMulti = new TGCheckButton(this, "&Mutliple selection", 10); AddFrame(fCheckMulti, new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 5)); fCheckMulti->Connect("Clicked()", "MyMainFrame", this, "HandleButtons()"); // Create a horizontal frame containing button(s) TGHorizontalFrame *hframe = new TGHorizontalFrame(this, 150, 20, kFixedWidth); TGTextButton *show = new TGTextButton(hframe, "&Show"); show->SetToolTipText("Click here to print the selection you made"); show->Connect("Pressed()", "MyMainFrame", this, "PrintSelected()"); hframe->AddFrame(show, new TGLayoutHints(kLHintsExpandX, 5, 5, 3, 4)); TGTextButton *exit = new TGTextButton(hframe, "&Exit "); exit->Connect("Pressed()", "MyMainFrame", this, "DoExit()"); hframe->AddFrame(exit, new TGLayoutHints(kLHintsExpandX, 5, 5, 3, 4)); AddFrame(hframe, new TGLayoutHints(kLHintsExpandX, 2, 2, 5, 1)); // Set a name to the main frame SetWindowName("List Box"); MapSubwindows(); // Initialize the layout algorithm via Resize() Resize(GetDefaultSize()); // Map main frame MapWindow(); fListBox->Select(1); } MyMainFrame::~MyMainFrame() { // Clean up main frame... Cleanup(); if (fSelected) { fSelected->Delete(); delete fSelected; } } void MyMainFrame::HandleButtons() { // Handle check button. Int_t id; TGButton *btn = (TGButton *) gTQSender; id = btn->WidgetId(); printf("HandleButton: id = %d\n", id); if (id == 10) fListBox->SetMultipleSelections(fCheckMulti->GetState()); } void MyMainFrame::PrintSelected() { // Writes selected entries in TList if multiselection. fSelected->Clear(); if (fListBox->GetMultipleSelections()) { Printf("Selected entries are:\n"); fListBox->GetSelectedEntries(fSelected); fSelected->ls(); } else { Printf("Selected entries is: %d\n", fListBox->GetSelected()); } } void MyMainFrame::PrintInListBox(const char *mess, ESeverity Type = kPrint) { TGTextLBEntry *entry = new TGTextLBEntry(fListBox->GetContainer(), new TGString(mess), fListBox->GetNumberOfEntries()+1); if(Type == kError) entry->SetBackgroundColor((Pixel_t)0xff0000); else if(Type == kInfo) entry->SetBackgroundColor((Pixel_t)0x87a7d2); else if(Type == kWarning) entry->SetBackgroundColor((Pixel_t)0xdfdf44); else if(Type == kPrint) entry->SetBackgroundColor((Pixel_t)0x90f269); fListBox->AddEntry((TGLBEntry *)entry, new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX)); fListBox->Layout(); } void testListBox() { // Popup the GUI... MyMainFrame *main = new MyMainFrame(gClient->GetRoot(), 200, 200); main->PrintInListBox("Entry Print", MyMainFrame::kPrint); main->PrintInListBox("Entry Info", MyMainFrame::kInfo); main->PrintInListBox("Entry Warning", MyMainFrame::kWarning); main->PrintInListBox("Entry Error", MyMainFrame::kError); }