Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | Related Pages

application.h

00001 /*
00002   libwftk - Worldforge Toolkit - a widget library
00003   Copyright (C) 2002 Malcolm Walker <malcolm@worldforge.org>
00004   Based on code copyright  (C) 1999-2002  Karsten Laux
00005 
00006   This library is free software; you can redistribute it and/or
00007   modify it under the terms of the GNU Lesser General Public
00008   License as published by the Free Software Foundation; either
00009   version 2.1 of the License, or (at your option) any later version.
00010   
00011   This library is distributed in the hope that it will be useful,
00012   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014   Lesser General Public License for more details.
00015   
00016   You should have received a copy of the GNU Lesser General Public
00017   License along with this library; if not, write to the
00018   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019   Boston, MA  02111-1307, SA.
00020 */
00021 
00022 
00023 #ifndef _APP_H
00024 #define _APP_H
00025 
00026 #include <string>
00027 #include <exception>
00028 #include <queue>
00029 #include <map>
00030 
00031 #if SIGC_MAJOR_VERSION == 1 && SIGC_MINOR_VERSION == 0
00032 #include <sigc++/signal_system.h>
00033 #else
00034 #include <sigc++/signal.h>
00035 #include <sigc++/object.h>
00036 #endif
00037 
00038 #include <SDL/SDL_types.h>
00039 #if 0
00040 #include <SDL/SDL_keysym.h> // for SDLK_LAST
00041 #endif
00042 
00043 #include <wftk/debug.h>
00044 
00045 namespace wftk {
00046 
00048 class Fatal : public std::exception
00049 {
00050  public:
00052   Fatal(const std::string& reason);
00053   virtual ~Fatal() throw() {}
00054 
00056   virtual const char* what() const throw() {return reason_.c_str();}
00057  private:
00058   std::string reason_;
00059 };
00060 
00065 class Application : virtual public SigC::Object
00066 {
00067  public:
00068   typedef std::map<std::string,Debug::Mask> DebugMap;
00069 
00071   Application(int &argc, char **&argv,
00072     const DebugMap& other_flags = DebugMap(), Uint32 update = 50);
00074   ~Application();
00075 
00077   SigC::Signal0<void> update;
00079   SigC::Signal0<void> draw;
00081   SigC::Signal0<void> destroyed;
00082   
00084   int exec();
00086   virtual void quit(int exitcode = 0);
00088   SigC::Slot0<void> quitSlot(int exitcode = 0);
00090   void abort();
00091 
00093   bool running() {return running_;}
00094 
00096 
00101   static Application* instance() {return instance_;}
00102 
00104   bool keepAlive();
00105 
00107   void waitFor(bool& var, bool wait_val = true);
00109   void waitFor(SigC::Slot0<bool> func, bool wait_val = true);
00110 
00112   void handleEvent(bool can_block = false);
00113 
00120   void setIdleTime(Uint32 n) {event_pump_ = n;}
00121 
00122   class Event
00123   {
00124    public:
00125     virtual ~Event() {}
00126 
00127     virtual void operator()() = 0;
00128   };
00129 
00130   // An event which is associated with data that may
00131   // be destroyed before it goes off. To use it,
00132   // your class C should have a currentEvent_ member
00133   // which it initializes to zero. Make FloatingEvent<C>
00134   // a friend of C, and call FloatingEvent::clear() in
00135   // your destructor if currentEvent_ is non-null.
00136   // Any particular piece of data can only have one of
00137   // these events at a time.
00138   template<class C>
00139   class FloatingEvent : public Event
00140   {
00141    public:
00142     FloatingEvent(C& c) : c_(&c) {c.currentEvent_ = this;}
00143     virtual ~FloatingEvent() {if(c_) c_->currentEvent_ = 0;}
00144 
00145     void clear() {c_ = 0;}
00146 
00147     C* data() const {return c_;}
00148 
00149    private:
00150     C* c_;
00151   };
00152 
00154   void pushEvent(Event* event) {if(event) queue_.push(event);}
00155 
00156  protected:
00157 
00158 #if 0
00159 
00160   enum { 
00161     KEY_TABLE_SIZE = SDLK_LAST , 
00162     KEY_DELAY = 10, // rough 500ms
00163     KEY_REPEAT = -1 // 0 means 100ms, -1 means 50ms
00164   };
00165 #endif
00166 
00167  private:
00168 
00169   typedef std::queue<Event*> EventQueue;
00170   EventQueue queue_;
00171 
00172   // list of event sources to poll
00173   enum Source {
00174     POLLING,
00175     TIMERS,
00176     GUI, // SDL events
00177     UPDATE,
00178     DRAW,
00179     NUM_SOURCES,
00180     FIRST_SOURCE = 0 // for initialization, looping
00181   } next_source_;
00182 
00183   // functions called in the constructor
00184 
00186   void parseArgs(int& argc, char**& argv, const DebugMap&);
00188   void loadResources();
00189 
00191   Uint32 event_pump_;
00193   int exitcode_;
00195   bool running_;
00196 
00197   static Application* instance_;
00198 };
00199 
00200 }
00201   
00202 #endif // !_APP_H
00203   

Generated Tue Oct 26 19:02:12 2004.
Copyright © 1998-2003 by the respective authors.

This document is licensed under the terms of the GNU Free Documentation License and may be freely distributed under the conditions given by this license.