Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

test-Gui.cxx

Go to the documentation of this file.
00001 #include <Midad/Gui/GuiButton.h>
00002 #include <Midad/Gui/GuiTable.h>
00003 #include <Midad/Gui/GuiMainWindow.h>
00004 #include <Midad/Gui/GuiMenu.h>
00005 #include <Midad/Gui/GuiBox.h>
00006 #include <Midad/Gui/GuiTab.h>
00007 #include <Midad/Gui/GuiFrame.h>
00008 #include <Midad/Gui/GuiCanvas.h>
00009 #include <Midad/Gui/GuiStatusBar.h>
00010 #include <Midad/Gui/GuiSlider.h>
00011 #include <Midad/Gui/GuiTextEntry.h>
00012 //#include "popup/MyClass.h"
00013 
00014 #include "TROOT.h"
00015 #include "TApplication.h"
00016 #include "TGTableLayout.h"
00017 #include "TCanvas.h"
00018 
00019 #include <sigc++/signal.h>
00020 #include <sigc++/bind.h>
00021 #include <sigc++/object_slot.h>
00022 
00023 #include <iostream>
00024 #include <string>
00025 #include <sstream>
00026 using namespace std;
00027 
00028 class Buttons : public GuiTable
00029 {
00030 
00031 public:
00032 
00033     Buttons(TGWindow& parent) : GuiTable(parent,1,4) {
00034         fPrev = SigC::manage(new GuiTextButton(*this,"<-Prev"));
00035         this->Attach(*fPrev,0,1,0,1);
00036         
00037         fRerun = SigC::manage(new GuiTextButton(*this,"Rerun"));
00038         this->Attach(*fRerun,1,2,0,1);
00039         
00040         fNext = SigC::manage(new GuiTextButton(*this,"Next->"));
00041         this->Attach(*fNext,2,3,0,1);
00042 
00043         GuiPicButton* picb = 
00044             SigC::manage(new GuiPicButton(*this,"root_s.xpm"));
00045         this->Attach(*picb,3,4,0,1);
00046     }
00047 
00048     ~Buttons() {}
00049 
00050     GuiTextButton& GetPrev() { return *fPrev; }
00051     GuiTextButton& GetRerun() { return *fRerun; }
00052     GuiTextButton& GetNext() { return *fNext; }
00053 
00054 private:
00055 
00056     SigC::Ptr<GuiTextButton> fPrev,fRerun,fNext;
00057 
00058 };
00059 
00060 
00061 void handler(string what)
00062 {
00063     cout << what << endl;
00064 }
00065 void handler_menu(string item, int ncalls)
00066 {
00067     cout << "[" << ncalls << "]" << item << endl;
00068 }
00069 
00070 void add_menu(GuiMenu* menu)
00071 {
00072     static int count = 0;
00073     ++count;
00074     stringstream ss;
00075     ss << "add " << count;
00076     menu->Add(ss.str().c_str(),SigC::bind(SigC::slot(add_menu),menu));
00077 }
00078 void insert_menu(GuiMenu* menu, GuiMenu::GuiMenuList::iterator gmit)
00079 {
00080     static int count = 0;
00081     ++count;
00082     stringstream ss;
00083     ss << "insert " << count;
00084     menu->Insert(gmit,ss.str().c_str(),SigC::bind(SigC::slot(insert_menu),
00085                                                   menu,gmit));
00086 }
00087 void say_hello()
00088 {
00089     cerr << "Hello!\n";
00090 }
00091 void fill_test_menu(GuiMenuBar* gmb);
00092 void clear_menu(GuiMenu* menu, GuiMenuBar* gmb)
00093 {
00094     menu->ClearAll();
00095     gmb->RemoveMenu("test");
00096     fill_test_menu(gmb);
00097     gmb->MapSubwindows();
00098     gmb->Layout();
00099     gmb->MapWindow();
00100 
00101 }
00102 void fill_test_menu(GuiMenuBar* gmb)
00103 {
00104     using namespace SigC;
00105 
00106     GuiMenu* menu = gmb->MakeAddMenu("test");
00107 
00108     menu->Add("clear",bind(slot(clear_menu),menu,gmb));
00109     GuiMenu* submenu = manage(new GuiMenu);
00110     menu->Add("submenu",*submenu);
00111     submenu->Add("hello",slot(say_hello));
00112 
00113     GuiMenu::GuiMenuList::iterator gmit
00114         = menu->Add("add",bind(slot(add_menu),menu));
00115     menu->Add("insert",bind(slot(insert_menu),menu,gmit));
00116 }
00117 
00118 void fill_mydisplay_menu(GuiMenuBar* gmb)
00119 {
00120     using SigC::bind;
00121     using SigC::slot;
00122 
00123     static int ncalls = 0;
00124     ++ncalls;
00125 
00126     static const char* menu_name[] = {
00127         "Menu1",
00128         "Menu2",
00129         "Menu3",
00130         0
00131     };
00132     static const char* menu_item_name[] = {
00133         "Item1",
00134         "Item2",
00135         "Item3",
00136         0
00137     };
00138 
00139     int m=0;
00140     while (menu_name[m]) {
00141         GuiMenu* menu;
00142         if (ncalls==1) menu = gmb->MakeAddMenu(menu_name[m]);
00143         else menu = gmb->GetMenu(menu_name[m]);
00144         if (!menu) {
00145             cerr << "No menu!\n";
00146             ++m;
00147             continue;
00148         }
00149         int i=0;
00150         while (menu_item_name[i]) {
00151             menu->Add(menu_item_name[i],
00152                       bind(slot(handler_menu),menu_item_name[i],ncalls));
00153             ++i;
00154         }
00155         ++m;
00156     }
00157 
00158     fill_test_menu(gmb);
00159 }
00160 void fill_menu(GuiMenuBar* gmb)
00161 {
00162     using SigC::bind;
00163     using SigC::slot;
00164 
00165     static int ncalls = 0;
00166     ++ncalls;
00167 
00168     static const char* menu_name[] = {
00169         "Menu1",
00170         "Menu2",
00171         "Menu3",
00172         0
00173     };
00174     static const char* menu_item_name[] = {
00175         "Item1",
00176         "Item2",
00177         "Item3",
00178         0
00179     };
00180 
00181     int m=0;
00182     while (menu_name[m]) {
00183         GuiMenu* menu;
00184         if (ncalls==1) menu = gmb->MakeAddMenu(menu_name[m]);
00185         else menu = gmb->GetMenu(menu_name[m]);
00186         if (!menu) {
00187             cerr << "No menu!\n";
00188             ++m;
00189             continue;
00190         }
00191         int i=0;
00192         while (menu_item_name[i]) {
00193             menu->Add(menu_item_name[i],
00194                       bind(slot(handler_menu),menu_item_name[i],ncalls));
00195             ++i;
00196         }
00197         ++m;
00198     }
00199 
00200     fill_test_menu(gmb);
00201 }
00202 
00203 
00204 void refill_menu(GuiMenuBar* gmb)
00205 {
00206     fill_menu(gmb);
00207     gmb->MapSubwindows();
00208     gmb->Layout();
00209     gmb->MapWindow();
00210 }
00211 
00212 void build_table(GuiVBox& vbox) 
00213 {
00214     using SigC::bind;
00215     using SigC::slot;
00216     const int ncols = 5;
00217     const int nrows = 1;
00218 
00219     GuiTable* table = manage(new GuiTable(vbox,nrows,ncols));
00220     table->SetLayoutHints(table->GetLayoutHints() & ~kLHintsExpandY);
00221     vbox.Add(*table);
00222 
00223     int row = 0;
00224 
00225     if (true) {
00226         // Add buttons
00227         for (int i=0; i<ncols; ++i) {
00228             char buf[128];
00229             
00230             sprintf(buf,"[%d]",i+1);
00231             GuiTextButton* gb = SigC::manage(new GuiTextButton(*table,buf));
00232             table->Attach(*gb,i,i+1,row,row+1);
00233 
00234             sprintf(buf,"click (%d)",i+1);
00235             gb->clicked.connect(bind(slot(handler),buf));
00236             sprintf(buf,"press (%d)",i+1);
00237             gb->pressed.connect(bind(slot(handler),buf));
00238             sprintf(buf,"release (%d)",i+1);
00239             gb->released.connect(bind(slot(handler),buf));
00240         }
00241 
00242         ++row;
00243     }
00244 
00245 
00246     Buttons* gc;
00247     if (row < nrows) {
00248         cerr << "Putting Buttons in table\n";
00249         gc = manage(new Buttons(*table));
00250         table->Attach(*gc,0,ncols,row,row+1);
00251     }
00252     else {
00253         cerr << "Putting Buttons in vbox\n";
00254         gc = manage(new Buttons(vbox));
00255         gc->SetLayoutHints(gc->GetLayoutHints() & ~kLHintsExpandY);
00256         vbox.Add(*gc);
00257     }
00258 
00259     gc->GetPrev().clicked.connect(bind(slot(handler),"Previous"));
00260     gc->GetRerun().clicked.connect(bind(slot(handler),"Rerun"));
00261     gc->GetNext().clicked.connect(bind(slot(handler),"Next"));
00262 }
00263 
00264 void tab_chirp(int id, GuiTab* gt)
00265 {
00266 
00267     GuiBase::ChildrenList cl = gt->GuisAt(id);
00268     cerr << "Tab " << id << " selected with "
00269          << cl.size() << " children\n";
00270     GuiBase::ChildrenList::iterator it, done = cl.end();
00271     for (it=cl.begin(); it != done; ++it) {
00272         cerr << "\t" << (*it)->GetName() << endl;
00273     }
00274 }
00275 
00276 void add_canvas(GuiVBox& vbox)
00277 {
00278 #if 1
00279     GuiTab* gt = manage(new GuiTab(vbox));
00280     vbox.Add(*gt);
00281 
00282     gt->selected.connect(bind(slot(tab_chirp),gt));
00283 
00284     GuiFrame* gf = gt->Add("Canvas");
00285 
00286     cerr << "Canvas frame: "  << (void*)gf << endl;
00287 
00288     GuiCanvas* gc = manage(new GuiCanvas(*gf->GetFrame()));
00289     gf->Add(*gc);
00290 
00291     gf->GetFrame()->SetWidth(100); gf->GetFrame()->SetHeight(100);
00292     gc->SetWidth(100); gc->SetHeight(100);
00293     gt->SetWidth(100); gt->SetHeight(100);
00294 
00295     TCanvas* canvas = gc->GetCanvas();
00296     canvas->cd();
00297 //    MyClass* mc = new MyClass();
00298 //    mc->Draw();
00299 
00300     gf = gt->Add("button");
00301     cerr << "Button frame: "  << (void*)gf << endl;
00302     GuiTextButton* gtb = manage(new GuiTextButton(*gf->GetFrame(),"click me"));
00303     gf->Add(*gtb);
00304     
00305 #else
00306     GuiCanvas* gc = manage(new GuiCanvas(vbox));
00307     vbox.Add(*gc);
00308 #endif
00309 }
00310 
00311 class AWindow : public GuiMainWindow
00312 {
00313 public:
00314 
00315     AWindow() : GuiMainWindow(250,50) {
00316         cerr << "Spawing a box\n";
00317 
00318         using SigC::bind;
00319         using SigC::slot;
00320 
00321         GuiVBox* vbox = manage(new GuiVBox(*this));
00322         this->Add(*vbox);
00323 
00324         GuiMenuBar* gmb = manage(new GuiMenuBar(*vbox));
00325         vbox->Add(*gmb);        
00326         fill_test_menu(gmb);
00327 
00328         GuiHBox* box = manage(new GuiHBox(*vbox));
00329         vbox->Add(*box);
00330         GuiTextButton* close = manage(new GuiTextButton(*box,"Close"));
00331         box->Add(*close);
00332         GuiTextButton* quit = manage(new GuiTextButton(*box,"quit"));
00333         box->Add(*quit);
00334         GuiTextButton* menu = manage(new GuiTextButton(*box,"redo menus"));
00335         box->Add(*menu);
00336 
00337         close->clicked.connect(slot(*this,&AWindow::SendCloseMessage));
00338         quit->clicked.connect(slot(*this,&AWindow::Quit));
00339         close_window.connect(slot(*this,&AWindow::KillMe));
00340         menu->clicked.connect(bind(slot(refill_menu),gmb));
00341     }
00342     ~AWindow() {
00343         cerr << "~AWindow()\n"; 
00344     }
00345 
00346 //    void CloseMe() {
00347 //        cerr << "CloseMe\n";
00348 //        this->SendCloseMessage();
00349 //    }
00350 
00351     void KillMe() {
00352         delete this;
00353     }
00354     void Quit() {
00355         cerr << "Quitting!\n"; 
00356         gApplication->Terminate(0);
00357     }
00358 
00359 };
00360 
00361 
00362 void spawn_a_window()
00363 {
00364     AWindow* aw = new AWindow();
00365     aw->ShowAll();
00366 }
00367 
00368 class Display : public GuiBox
00369 {
00370 public:
00371     Display(TGWindow& parent, int nrows, int ncols)
00372         : GuiBox(parent,kVerticalFrame,100,100) {
00373         // Menu Bar
00374         fMenuBar = SigC::manage(new GuiMenuBar(*this));
00375         this->Add(*fMenuBar);
00376         
00377         // Main box
00378         fMainBox = SigC::manage(new GuiTable(*this,nrows,ncols));
00379         this->Add(*fMainBox);
00380         
00381         // Status bar
00382         fStatusBar = SigC::manage(new GuiStatusBar(*this));
00383         this->Add(*fStatusBar);
00384     }
00385     virtual ~Display() {}
00386     // Get GUI elements
00387     GuiMenuBar& GetMenuBar() { return *fMenuBar; }
00388     GuiTable& GetMainBox() { return *fMainBox; }
00389     GuiStatusBar& GetStatusBar() { return *fStatusBar; }
00390 private:
00391     GuiMenuBar* fMenuBar;
00392     GuiTable* fMainBox;
00393     GuiStatusBar* fStatusBar;
00394 };                              // end of class Display
00395 
00396 class MyDisplay : public Display
00397 {
00398 
00399 public:
00400 
00401     MyDisplay(TGWindow& parent) : Display(parent,3,3){
00402         GuiTable& main_box = this->GetMainBox();
00403 
00404         GuiCanvas* gc = manage(new GuiCanvas(main_box,100,100));
00405         main_box.Attach(*gc, 1,2, 1,2);
00406 
00407         GuiSlider* xslider = manage(new GuiSlider(main_box,kHorizontalFrame));
00408         main_box.Attach(*xslider, 1,2, 2,3);
00409         
00410         GuiSlider* yslider = manage(new GuiSlider(main_box,kVerticalFrame));
00411         main_box.Attach(*yslider, 2,3, 1,2);
00412     
00413         GuiMenuBar& menubar = this->GetMenuBar();
00414         fill_mydisplay_menu(&menubar);
00415     }
00416     virtual ~MyDisplay() {}
00417 };                              // end of class MyDisplay
00418 
00419 void spawn_mydisplay()
00420 {
00421     GuiMainWindow* gmw = new GuiMainWindow();
00422 
00423     MyDisplay* disp = manage(new MyDisplay(*gmw));
00424     gmw->Add(*disp);
00425 
00426     gmw->ShowAll();
00427     gmw->ConnectClose(true);     // delete on close
00428 }
00429 
00430 void add_button(GuiVBox* vbox)
00431 {
00432     GuiTextButton* gb = manage(new GuiTextButton(*vbox,"add"));
00433     gb->SetLayoutHints(kLHintsExpandX | kLHintsTop | kLHintsLeft);
00434     vbox->Add(*gb);
00435     gb->clicked.connect(bind(slot(add_button),vbox));
00436 }
00437 
00438 void add_spawner(GuiVBox& vbox)
00439 {
00440     using namespace SigC;
00441 
00442     GuiTextButton* gb = manage(new GuiTextButton(vbox,"spawn"));
00443     gb->SetLayoutHints(kLHintsExpandX | kLHintsTop | kLHintsLeft);
00444     vbox.Add(*gb);
00445     gb->clicked.connect(SigC::slot(spawn_a_window));
00446 
00447 
00448     gb = manage(new GuiTextButton(vbox,"test"));
00449     gb->SetLayoutHints(kLHintsExpandX | kLHintsTop | kLHintsLeft);
00450     vbox.Add(*gb);
00451     gb->clicked.connect(SigC::slot(spawn_mydisplay));
00452 
00453     add_button(&vbox);
00454 }
00455 
00456 void echo_text_entry(GuiTextEntry* entry)
00457 {
00458     cerr << entry->GetText() << endl;
00459 }
00460 
00461 void echo(const char* text)
00462 {
00463     cerr << text << endl;
00464 }
00465 int main (int argc, char *argv[])
00466 {
00467     TApplication theApp("App", &argc, argv);
00468 
00469     GuiMainWindow* mw = manage(new GuiMainWindow(300,300));
00470 
00471     GuiVBox* vbox = manage(new GuiVBox(*mw));
00472     vbox->SetLayoutHints(vbox->GetLayoutHints() | kLHintsExpandX);
00473     mw->Add(*vbox);
00474 
00475     GuiHBox* hbox = manage(new GuiHBox(*vbox));
00476     vbox->Add(*hbox);
00477 
00478     GuiMenuBar* gmb = manage(new GuiMenuBar(*hbox));
00479     hbox->Add(*gmb);
00480     fill_menu(gmb);
00481 
00482     GuiTextEntry* entry = manage(new GuiTextEntry(*vbox,"<enter something>"));
00483     vbox->Add(*entry);
00484     entry->activated.connect(SigC::bind(SigC::slot(echo_text_entry),entry));
00485     entry->text_changed.connect(SigC::slot(echo));
00486 
00487     build_table(*vbox);
00488     add_spawner(*vbox);
00489     add_canvas(*vbox);
00490 
00491     mw->ShowAll();
00492     mw->SetMinSize();
00493     mw->ConnectClose();
00494 
00495     mw->close_window.connect(SigC::slot(*mw,&GuiMainWindow::KillMe));
00496 
00497     theApp.Run();
00498     return 0;
00499 } // end of main()

Generated on Mon Nov 23 05:28:32 2009 for loon by  doxygen 1.3.9.1