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