core/vgui/vgui.cxx

Go to the documentation of this file.
00001 // This is core/vgui/vgui.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 //  \file
00007 // \author Philip C. Pritchett, RRG, University of Oxford
00008 // \date   30 Sep 99
00009 // \brief  See vgui.h for a description of this file.
00010 
00011 #include "vgui.h"
00012 #include <vcl_cassert.h>
00013 #include <vcl_cstring.h>
00014 #include <vcl_cstdlib.h> // abort()
00015 #include <vcl_iostream.h>
00016 
00017 #include <vgui/vgui_macro.h>
00018 #include <vgui/vgui_window.h>
00019 #include <vgui/vgui_tableau.h>
00020 #include <vgui/vgui_adaptor.h>
00021 #include <vgui/vgui_tag.h>
00022 #include <vgui/vgui_toolkit.h>
00023 #include <vgui/internals/vgui_accelerate.h>
00024 #include <vgui/internals/vgui_dialog_impl.h>
00025 #include <vgui/internals/vgui_dialog_extensions_impl.h>
00026 
00027 // static data
00028 vgui_toolkit *vgui::instance_ = 0;
00029 bool vgui::init_called = false;
00030 bool vgui::quit_called = false;
00031 
00032 
00033 vcl_ostream vgui::out(vcl_cout.rdbuf());
00034 
00035 
00036 bool vgui_emulate_overlays = false;
00037 bool vgui_glerrors_are_bad = false;
00038 bool vgui_mfc_use_bitmap = true;
00039 
00040 
00041 // make sure that vgui::uninit is called before the application exits.
00042 struct vgui_uninit_caller
00043 {
00044   ~vgui_uninit_caller() { vgui::uninit(); }
00045 };
00046 
00047 vgui_uninit_caller vgui_the_uniniter_caller_;
00048 
00049 //----------------------------------------------------------------------------
00050 //: Remove an argument from a command line argument vec*tor :
00051 static void vgui_remove_arg(unsigned index, int &argc, char **argv)
00052 {
00053   // NB ISO says argv[argc] is required to be 0, so argv[i+1] is right.
00054   for (int i=index; i<argc; ++i)
00055     argv[i]=argv[i+1];
00056   --argc;
00057 }
00058 
00059 //----------------------------------------------------------------------------
00060 // [*] Note on vgui_tag_call():
00061 // This may be the first method on vgui to be called so we
00062 // should call the tag functions now. It is not a problem if
00063 // vgui_tag_call() gets invoked multiple times because (a)
00064 // the tag function are supposed to be idempotent and (b) a
00065 // tag function is called at most once per registration.
00066 bool vgui::exists(char const *toolkit)
00067 {
00068   vgui_tag_call(); // see [*] above.
00069 
00070   vcl_vector<vgui_toolkit*> *tk = vgui_toolkit::registry();
00071   for (unsigned int i=0; i<tk->size(); ++i)
00072     if ( (*tk)[i]->name() == toolkit )
00073       return true;
00074   return false;
00075 }
00076 
00077 //----------------------------------------------------------------------------
00078 //: Method for selecting a specific toolkit.
00079 //  This will abort() if given a toolkit which is not available.
00080 void vgui::select(char const *toolkit)
00081 {
00082   vgui_tag_call(); // see [*] above.
00083 
00084   vcl_vector<vgui_toolkit*> *tk = vgui_toolkit::registry();
00085   for (unsigned int i=0; i<tk->size(); ++i) {
00086     if ( (*tk)[i]->name() == toolkit ) {
00087       instance_ = (*tk)[i];
00088       return;
00089     }
00090   }
00091   vgui_macro_warning << "no such toolkit \'" << toolkit << "\' -- vcl_abort()ing\n";
00092   vcl_abort();
00093 }
00094 
00095 //----------------------------------------------------------------------------
00096 //: Select a toolkit from command line arguments and environment variables.
00097 bool vgui::select(int &argc, char **argv)
00098 {
00099   vgui_tag_call(); // see [*] above.
00100 
00101   // look for --factory=name
00102   for (int i=1; i<argc; ) {
00103     if (vcl_strncmp(argv[i],"--factory=",10) == 0) {
00104       instance_ = vgui_toolkit::lookup(argv[i]+10);
00105       vgui_remove_arg(i, argc, argv);
00106     }
00107     else
00108       ++ i;
00109   }
00110 
00111   // if there is no instance set (with --factory), check the environment
00112   // variable 'vgui' :
00113   if (! instance_) {
00114     char const *env_name = getenv("vgui");
00115     if (env_name)
00116       instance_ = vgui_toolkit::lookup(env_name);
00117   }
00118 
00119   if (instance_)
00120     return true;
00121   else
00122     return false;
00123 }
00124 
00125 
00126 //----------------------------------------------------------------------------
00127 //: Initialise the selected toolkit passing it the given command line.
00128 void vgui::init(int &argc, char **argv)
00129 {
00130   vgui_tag_call(); // see [*] above.
00131 
00132   // avoid double init.
00133   assert(! init_called);
00134   init_called = true;
00135 
00136   // list the toolkits registered :
00137   vcl_cerr << "vgui : registered ";
00138   for (unsigned i=0; i<vgui_toolkit::registry()->size(); ++i)
00139     vcl_cerr << '\'' << (*vgui_toolkit::registry())[i]->name() << "\' ";
00140   vcl_cerr << vcl_endl;
00141 
00142   // if no toolkit was selected, try using the command line arguments.
00143   if (! instance_)
00144     select(argc, argv);
00145 
00146   // if there is still no instance, take the first one registered :
00147   if (! instance_) {
00148     if (! vgui_toolkit::registry()->empty())
00149       instance_ = vgui_toolkit::registry()->front();
00150   }
00151 
00152   // abort if no toolkit has been selected.
00153   if (! instance_) {
00154     vgui_macro_warning << "failed to find a toolkit implementation - vcl_abort()ing.\n";
00155     vcl_abort();
00156   }
00157   assert(instance_); // need an instance.
00158 
00159   // Look for command line options.
00160   for (int i=1; i<argc; )
00161   {
00162     if (vcl_strncmp(argv[i],"--factory=",10) == 0)
00163     {
00164       // --factory=<name>
00165       vgui_macro_warning << "superfluous command line argument \'"
00166                          << argv[i] << "\' ignored\n";
00167       vgui_remove_arg(i, argc, argv);
00168     }
00169     else if (vcl_strncmp(argv[i],"--no-accel",10) == 0)
00170     {
00171       // matches --no-accel*
00172       vgui_accelerate::vgui_no_acceleration = true;
00173       vgui_remove_arg(i, argc, argv);
00174     }
00175     else if (vcl_strcmp(argv[i],"--mfc-use-bitmap") == 0)
00176     {
00177       vgui_mfc_use_bitmap = true;
00178       vgui_remove_arg(i, argc, argv);
00179     }
00180     else if (vcl_strcmp(argv[i],"--mfc-use-gl") == 0)
00181     {
00182       vgui_mfc_use_bitmap = false;
00183       vgui_remove_arg(i, argc, argv);
00184     }
00185     else if (vcl_strcmp(argv[i],"--emulate-overlays") == 0) {
00186       vgui_emulate_overlays = true;
00187       vgui_remove_arg(i, argc, argv);
00188     }
00189     else if (vcl_strcmp(argv[i],"--glerrors-are-bad") == 0) {
00190       vgui_glerrors_are_bad = true;
00191       vgui_remove_arg(i, argc, argv);
00192     }
00193     else
00194       ++i;
00195   }
00196 
00197   // print a message prior to initializing the toolkit.
00198   vcl_cerr << "vgui : initialize \'" << instance_->name() << "\'\n";
00199   instance_->init(argc, argv);
00200 }
00201 
00202 
00203 void vgui::uninit()
00204 {
00205   vcl_cout << "vgui::uninit called" << vcl_endl;
00206   // make sure uninit does something only once
00207   static bool uninit_called = false;
00208   if ( !uninit_called && init_called && instance_ )
00209     instance_->uninit();
00210   uninit_called = true;
00211 }
00212 
00213 //----------------------------------------------------------------------------
00214 //: Produce window with menubar.
00215 vgui_window *vgui::produce_window(int width,
00216                                   int height,
00217                                   vgui_menu const &menubar,
00218                                   vcl_string const &title)
00219 {
00220   if (instance_)
00221     return instance_->produce_window(width, height, menubar, title.c_str());
00222   else {
00223     vgui_macro_warning << "no toolkit selected\n";
00224     return 0;
00225   }
00226 }
00227 
00228 //----------------------------------------------------------------------------
00229 //: Produce window without menubar.
00230 vgui_window *vgui::produce_window(int width,
00231                                   int height,
00232                                   vcl_string const &title)
00233 {
00234   if (instance_)
00235     return instance_->produce_window(width, height, title.c_str());
00236   else {
00237     vgui_macro_warning << "no toolkit selected\n";
00238     return 0;
00239   }
00240 }
00241 
00242 //----------------------------------------------------------------------------
00243 //: Produce dialog box.
00244 vgui_dialog_impl *vgui::produce_dialog(vcl_string const &name)
00245 {
00246   if (instance_)
00247     return instance_->produce_dialog(name.c_str());
00248   else {
00249     vgui_macro_warning << "no toolkit selected\n";
00250     return 0;
00251   }
00252 }
00253 
00254 //----------------------------------------------------------------------------
00255 //: Produce dialog box.
00256 vgui_dialog_extensions_impl *vgui::produce_extension_dialog(vcl_string const &name)
00257 {
00258   if (instance_)
00259     return instance_->produce_dialog_extension(name.c_str());
00260   else {
00261     vgui_macro_warning << "no toolkit selected\n";
00262     return 0;
00263   }
00264 }
00265 //----------------------------------------------------------------------------
00266 //: Quit application.
00267 void vgui::quit()
00268 {
00269   quit_called = true;
00270   if (instance_)
00271     instance_->quit();
00272   else {
00273     vgui_macro_warning << "no instance_ to call quit() on\n";
00274     //exit(1);
00275   }
00276 }
00277 
00278 
00279 bool vgui::quit_was_called()
00280 {
00281   return quit_called;
00282 }
00283 
00284 
00285 //----------------------------------------------------------------------------
00286 //: Run until quit is called.
00287 int vgui::run()
00288 {
00289   if (instance_) {
00290     instance_->run();
00291     return 0;
00292   }
00293   else {
00294     vgui_macro_warning << "no toolkit selected\n";
00295     return 1;
00296   }
00297 }
00298 
00299 //----------------------------------------------------------------------------
00300 //: Run the next event in the event queue.
00301 void vgui::run_one_event()
00302 {
00303   if (instance_)
00304     instance_->run_one_event();
00305   else
00306     vgui_macro_warning << "no toolkit selected\n";
00307 }
00308 
00309 //----------------------------------------------------------------------------
00310 //: Run all events in the event queue.
00311 void vgui::run_till_idle()
00312 {
00313   if (instance_)
00314     instance_->run_till_idle();
00315   else
00316     vgui_macro_warning << "no toolkit selected\n";
00317 }
00318 
00319 //----------------------------------------------------------------------------
00320 //: Remove all events from the event queue.
00321 void vgui::flush()
00322 {
00323   if (instance_)
00324     instance_->flush();
00325   else
00326     vgui_macro_warning << "no toolkit selected\n";
00327 }
00328 
00329 //-----------------------------------------------------------------------------
00330 //: Add event to the event queue.
00331 void vgui::add_event(vgui_event const& e)
00332 {
00333   if (instance_)
00334     instance_->add_event(e);
00335   else
00336     vgui_macro_warning << "no toolkit selected\n";
00337 }
00338 
00339 //-----------------------------------------------------------------------------
00340 //: Display this tableau and run till dead (no menubar).
00341 int vgui::run(vgui_tableau_sptr const& tableau, int width, int height,
00342               vcl_string const &title)
00343 {
00344   adapt(tableau, width, height, title);
00345   return vgui::run();
00346 }
00347 
00348 //-----------------------------------------------------------------------------
00349 //: Display this tableau and run till dead (with menubar).
00350 int vgui::run(vgui_tableau_sptr const& tableau, int width, int height,
00351               vgui_menu const &menubar, vcl_string const &title)
00352 {
00353   adapt(tableau, width, height, menubar, title);
00354   return vgui::run();
00355 }
00356 
00357 //-----------------------------------------------------------------------------
00358 //: Create the vgui_window but don't run it (no menubar).
00359 vgui_window *vgui::adapt(vgui_tableau_sptr const& tableau, int width,
00360                          int height, vcl_string const &title)
00361 {
00362   vgui_window *win = vgui::produce_window(width, height, title);
00363   win->get_adaptor()->set_tableau(tableau);
00364   win->show();
00365   return win;
00366 }
00367 
00368 //-----------------------------------------------------------------------------
00369 //: Create the vgui_window but don't run it (with menubar).
00370 vgui_window *vgui::adapt(vgui_tableau_sptr const& tableau, int width,
00371                          int height, vgui_menu const &mb, vcl_string const &title)
00372 {
00373   vgui_window *win = vgui::produce_window(width, height, mb, title);
00374   win->get_adaptor()->set_tableau(tableau);
00375   win->show();
00376   return win;
00377 }

Generated on Thu Nov 20 05:10:01 2008 for core/vgui by  doxygen 1.5.1