listBox.C

Go to the documentation of this file.
00001 //
00002 // Author: Ilka Antcheva   1/12/2006
00003 
00004 // This macro gives an example of how to create a list box
00005 // and how to set and use its multiple selection feature.
00006 // To run it do either:
00007 // .x listBox.C
00008 // .x listBox.C++
00009 
00010 #include <TApplication.h>
00011 #include <TGClient.h>
00012 #include <TGButton.h>
00013 #include <TGListBox.h>
00014 #include <TList.h>
00015 
00016 class MyMainFrame : public TGMainFrame {
00017 
00018 private:
00019    TGListBox           *fListBox;
00020    TGCheckButton       *fCheckMulti;
00021    TList               *fSelected;   
00022 
00023 public:
00024    MyMainFrame(const TGWindow *p, UInt_t w, UInt_t h);
00025    virtual ~MyMainFrame();
00026    void DoExit();
00027    void DoSelect();
00028    void HandleButtons();
00029    void PrintSelected();
00030 
00031    ClassDef(MyMainFrame, 0)
00032 };
00033 
00034 void MyMainFrame::DoSelect()
00035 {
00036    Printf("Slot DoSelect()");
00037 }
00038 
00039 void MyMainFrame::DoExit()
00040 {
00041    Printf("Slot DoExit()");
00042    gApplication->Terminate(0);
00043 }
00044 
00045 MyMainFrame::MyMainFrame(const TGWindow *p, UInt_t w, UInt_t h) :
00046    TGMainFrame(p, w, h)
00047 {
00048    // Create main frame
00049    
00050    fListBox = new TGListBox(this, 89);
00051    fSelected = new TList;
00052    char tmp[20];
00053    for (int i = 0; i < 20; ++i) {
00054       sprintf(tmp, "Entry %i", i+1);
00055       fListBox->AddEntry(tmp, i+1);
00056    }
00057    fListBox->Resize(100,150);
00058    AddFrame(fListBox, new TGLayoutHints(kLHintsTop | kLHintsLeft |
00059                                         kLHintsExpandX | kLHintsExpandY, 
00060                                         5, 5, 5, 5));
00061                                                          
00062    fCheckMulti = new TGCheckButton(this, "&Mutliple selection", 10);
00063    AddFrame(fCheckMulti, new TGLayoutHints(kLHintsTop | kLHintsLeft,
00064                                            5, 5, 5, 5));
00065    fCheckMulti->Connect("Clicked()", "MyMainFrame", this, "HandleButtons()"); 
00066    // Create a horizontal frame containing button(s)
00067    TGHorizontalFrame *hframe = new TGHorizontalFrame(this, 150, 20, kFixedWidth);
00068    TGTextButton *show = new TGTextButton(hframe, "&Show");
00069    show->SetToolTipText("Click here to print the selection you made");
00070    show->Connect("Pressed()", "MyMainFrame", this, "PrintSelected()");
00071    hframe->AddFrame(show, new TGLayoutHints(kLHintsExpandX, 5, 5, 3, 4));
00072    TGTextButton *exit = new TGTextButton(hframe, "&Exit ");
00073    exit->Connect("Pressed()", "MyMainFrame", this, "DoExit()");
00074    hframe->AddFrame(exit, new TGLayoutHints(kLHintsExpandX, 5, 5, 3, 4));
00075    AddFrame(hframe, new TGLayoutHints(kLHintsExpandX, 2, 2, 5, 1));
00076 
00077    // Set a name to the main frame   
00078    SetWindowName("List Box");
00079    MapSubwindows();
00080 
00081    // Initialize the layout algorithm via Resize()
00082    Resize(GetDefaultSize());
00083 
00084    // Map main frame
00085    MapWindow();
00086    fListBox->Select(1);
00087 }
00088 
00089 MyMainFrame::~MyMainFrame()
00090 {
00091    // Clean up main frame...
00092    Cleanup();
00093    if (fSelected) {
00094       fSelected->Delete();
00095       delete fSelected;
00096    }
00097 }
00098 
00099 void MyMainFrame::HandleButtons()
00100 {
00101    // Handle check button.
00102    Int_t id;
00103    TGButton *btn = (TGButton *) gTQSender;
00104    id = btn->WidgetId();
00105 
00106    printf("HandleButton: id = %d\n", id);
00107 
00108    if (id == 10)  
00109       fListBox->SetMultipleSelections(fCheckMulti->GetState());
00110 }
00111 
00112 
00113 void MyMainFrame::PrintSelected()
00114 {
00115    // Writes selected entries in TList if multiselection.
00116 
00117    fSelected->Clear();
00118 
00119    if (fListBox->GetMultipleSelections()) {
00120       Printf("Selected entries are:\n");
00121       fListBox->GetSelectedEntries(fSelected);
00122       fSelected->ls();
00123    } else {
00124       Printf("Selected entries is: %d\n", fListBox->GetSelected());
00125    }
00126 }
00127 
00128 void listBox()
00129 {
00130    // Popup the GUI...
00131    new MyMainFrame(gClient->GetRoot(), 200, 200);
00132 }

Generated on Tue Jul 5 15:44:25 2011 for ROOT_528-00b_version by  doxygen 1.5.1