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   return (bool)instance_;
00120 }
00121 
00122 
00123 //----------------------------------------------------------------------------
00124 //: Initialise the selected toolkit passing it the given command line.
00125 void vgui::init(int &argc, char **argv)
00126 {
00127   vgui_tag_call(); // see [*] above.
00128 
00129   // avoid double init.
00130   assert(! init_called);
00131   init_called = true;
00132 
00133   // list the toolkits registered :
00134   vcl_cerr << "vgui : registered ";
00135   for (unsigned i=0; i<vgui_toolkit::registry()->size(); ++i)
00136     vcl_cerr << '\'' << (*vgui_toolkit::registry())[i]->name() << "\' ";
00137   vcl_cerr << vcl_endl;
00138 
00139   // if no toolkit was selected, try using the command line arguments.
00140   if (! instance_)
00141     select(argc, argv);
00142 
00143   // if there is still no instance, take the first one registered :
00144   if (! instance_) {
00145     if (! vgui_toolkit::registry()->empty())
00146       instance_ = vgui_toolkit::registry()->front();
00147   }
00148 
00149   // abort if no toolkit has been selected.
00150   if (! instance_) {
00151     vgui_macro_warning << "failed to find a toolkit implementation - vcl_abort()ing.\n";
00152     vcl_abort();
00153   }
00154   assert(instance_); // need an instance.
00155 
00156   // Look for command line options.
00157   for (int i=1; i<argc; )
00158   {
00159     if (vcl_strncmp(argv[i],"--factory=",10) == 0)
00160     {
00161       // --factory=<name>
00162       vgui_macro_warning << "superfluous command line argument \'"
00163                          << argv[i] << "\' ignored\n";
00164       vgui_remove_arg(i, argc, argv);
00165     }
00166     else if (vcl_strncmp(argv[i],"--no-accel",10) == 0)
00167     {
00168       // matches --no-accel*
00169       vgui_accelerate::vgui_no_acceleration = true;
00170       vgui_remove_arg(i, argc, argv);
00171     }
00172     else if (vcl_strcmp(argv[i],"--mfc-use-bitmap") == 0)
00173     {
00174       vgui_mfc_use_bitmap = true;
00175       vgui_remove_arg(i, argc, argv);
00176     }
00177     else if (vcl_strcmp(argv[i],"--mfc-use-gl") == 0)
00178     {
00179       vgui_mfc_use_bitmap = false;
00180       vgui_remove_arg(i, argc, argv);
00181     }
00182     else if (vcl_strcmp(argv[i],"--emulate-overlays") == 0) {
00183       vgui_emulate_overlays = true;
00184       vgui_remove_arg(i, argc, argv);
00185     }
00186     else if (vcl_strcmp(argv[i],"--glerrors-are-bad") == 0) {
00187       vgui_glerrors_are_bad = true;
00188       vgui_remove_arg(i, argc, argv);
00189     }
00190     else
00191       ++i;
00192   }
00193 
00194   // print a message prior to initializing the toolkit.
00195   vcl_cerr << "vgui : initialize \'" << instance_->name() << "\'\n";
00196   instance_->init(argc, argv);
00197 }
00198 
00199 
00200 void vgui::uninit()
00201 {
00202   vcl_cout << "vgui::uninit called" << vcl_endl;
00203   // make sure uninit does something only once
00204   static bool uninit_called = false;
00205   if ( !uninit_called && init_called && instance_ )
00206     instance_->uninit();
00207   uninit_called = true;
00208 }
00209 
00210 //----------------------------------------------------------------------------
00211 //: Produce window with menubar.
00212 vgui_window *vgui::produce_window(int width,
00213                                   int height,
00214                                   vgui_menu const &menubar,
00215                                   vcl_string const &title)
00216 {
00217   if (instance_)
00218     return instance_->produce_window(width, height, menubar, title.c_str());
00219   else {
00220     vgui_macro_warning << "no toolkit selected\n";
00221     return 0;
00222   }
00223 }
00224 
00225 //----------------------------------------------------------------------------
00226 //: Produce window without menubar.
00227 vgui_window *vgui::produce_window(int width,
00228                                   int height,
00229                                   vcl_string const &title)
00230 {
00231   if (instance_)
00232     return instance_->produce_window(width, height, title.c_str());
00233   else {
00234     vgui_macro_warning << "no toolkit selected\n";
00235     return 0;
00236   }
00237 }
00238 
00239 //----------------------------------------------------------------------------
00240 //: Produce dialog box.
00241 vgui_dialog_impl *vgui::produce_dialog(vcl_string const &name)
00242 {
00243   if (instance_)
00244     return instance_->produce_dialog(name.c_str());
00245   else {
00246     vgui_macro_warning << "no toolkit selected\n";
00247     return 0;
00248   }
00249 }
00250 
00251 //----------------------------------------------------------------------------
00252 //: Produce dialog box.
00253 vgui_dialog_extensions_impl *vgui::produce_extension_dialog(vcl_string const &name)
00254 {
00255   if (instance_)
00256     return instance_->produce_dialog_extension(name.c_str());
00257   else {
00258     vgui_macro_warning << "no toolkit selected\n";
00259     return 0;
00260   }
00261 }
00262 //----------------------------------------------------------------------------
00263 //: Quit application.
00264 void vgui::quit()
00265 {
00266   quit_called = true;
00267   if (instance_)
00268     instance_->quit();
00269   else {
00270     vgui_macro_warning << "no instance_ to call quit() on\n";
00271     //exit(1);
00272   }
00273 }
00274 
00275 
00276 bool vgui::quit_was_called()
00277 {
00278   return quit_called;
00279 }
00280 
00281 
00282 //----------------------------------------------------------------------------
00283 //: Run until quit is called.
00284 int vgui::run()
00285 {
00286   if (instance_) {
00287     instance_->run();
00288     return 0;
00289   }
00290   else {
00291     vgui_macro_warning << "no toolkit selected\n";
00292     return 1;
00293   }
00294 }
00295 
00296 //----------------------------------------------------------------------------
00297 //: Run the next event in the event queue.
00298 void vgui::run_one_event()
00299 {
00300   if (instance_)
00301     instance_->run_one_event();
00302   else
00303     vgui_macro_warning << "no toolkit selected\n";
00304 }
00305 
00306 //----------------------------------------------------------------------------
00307 //: Run all events in the event queue.
00308 void vgui::run_till_idle()
00309 {
00310   if (instance_)
00311     instance_->run_till_idle();
00312   else
00313     vgui_macro_warning << "no toolkit selected\n";
00314 }
00315 
00316 //----------------------------------------------------------------------------
00317 //: Remove all events from the event queue.
00318 void vgui::flush()
00319 {
00320   if (instance_)
00321     instance_->flush();
00322   else
00323     vgui_macro_warning << "no toolkit selected\n";
00324 }
00325 
00326 //-----------------------------------------------------------------------------
00327 //: Add event to the event queue.
00328 void vgui::add_event(vgui_event const& e)
00329 {
00330   if (instance_)
00331     instance_->add_event(e);
00332   else
00333     vgui_macro_warning << "no toolkit selected\n";
00334 }
00335 
00336 //-----------------------------------------------------------------------------
00337 //: Display this tableau and run till dead (no menubar).
00338 int vgui::run(vgui_tableau_sptr const& tableau, int width, int height,
00339               vcl_string const &title)
00340 {
00341   adapt(tableau, width, height, title);
00342   return vgui::run();
00343 }
00344 
00345 //-----------------------------------------------------------------------------
00346 //: Display this tableau and run till dead (with menubar).
00347 int vgui::run(vgui_tableau_sptr const& tableau, int width, int height,
00348               vgui_menu const &menubar, vcl_string const &title)
00349 {
00350   adapt(tableau, width, height, menubar, title);
00351   return vgui::run();
00352 }
00353 
00354 //-----------------------------------------------------------------------------
00355 //: Create the vgui_window but don't run it (no menubar).
00356 vgui_window *vgui::adapt(vgui_tableau_sptr const& tableau, int width,
00357                          int height, vcl_string const &title)
00358 {
00359   vgui_window *win = vgui::produce_window(width, height, title);
00360   win->get_adaptor()->set_tableau(tableau);
00361   win->show();
00362   return win;
00363 }
00364 
00365 //-----------------------------------------------------------------------------
00366 //: Create the vgui_window but don't run it (with menubar).
00367 vgui_window *vgui::adapt(vgui_tableau_sptr const& tableau, int width,
00368                          int height, vgui_menu const &mb, vcl_string const &title)
00369 {
00370   vgui_window *win = vgui::produce_window(width, height, mb, title);
00371   win->get_adaptor()->set_tableau(tableau);
00372   win->show();
00373   return win;
00374 }