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

mouse.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 #ifndef _MOUSE_H
00023 #define _MOUSE_H
00024 
00025 #include <wftk/point.h>
00026 #include <wftk/resources.h>
00027 #include <wftk/marshal.h>
00028 
00029 #include <sigc++/marshal.h>
00030 #if SIGC_MAJOR_VERSION == 1 && SIGC_MINOR_VERSION == 0
00031 #include <sigc++/signal_system.h>
00032 #else
00033 #include <sigc++/signal.h>
00034 #include <sigc++/object.h>
00035 #endif
00036 
00037 #include <SDL/SDL_mouse.h>
00038 #include <SDL/SDL_events.h>
00039 
00040 namespace wftk {
00041 
00042 class Surface;
00043 
00045 class Pointer : virtual public SigC::Object
00046 {
00047   friend class Mouse;
00048 
00049  public:
00050   Pointer() {}
00051   virtual ~Pointer() {}
00052 
00053   // can't load pointers, have to create from surfaces
00054   struct ResLoad {
00055     std::pair<Pointer*,bool> operator()(const std::string&)
00056     {
00057       assert(false);
00058       return std::pair<Pointer*,bool>(0, false);
00059     }
00060   };
00061   struct ResInval {
00062     typedef const Pointer* OutType;
00063     OutType operator()(const std::string&) const;
00064   };
00066   static ResourceRegistry<Pointer*,ResLoad,ResInval> registry;
00068   typedef Resource<Pointer*> Resource;
00069 
00070  private:
00071   Pointer(const Pointer&);
00072   Pointer& operator=(const Pointer&);
00073 
00074  protected:
00076   virtual void show() = 0;
00078   virtual void hide() = 0;
00079 };
00080 
00085 class Mouse : virtual public SigC::Object
00086 {
00087  public:
00089   Mouse();
00091   ~Mouse();
00093   static bool isInit() {return instance_ != NULL;}
00095   static Mouse* instance() {return isInit() ? instance_ : new Mouse();}
00096 
00098   enum Button // SDL_BUTTON_FOO is button #, SDL_BUTTON() converts to bitmask
00099   { 
00100         // Rasta ASCII Pacman required so doxygen picks these up
00101     LEFT   = SDL_BUTTON(SDL_BUTTON_LEFT), 
00102     RIGHT  = SDL_BUTTON(SDL_BUTTON_RIGHT), 
00103     MIDDLE = SDL_BUTTON(SDL_BUTTON_MIDDLE), 
00104     // these two were in uta, but don't appear to match anything in SDL
00105     WHEEL_UP = SDL_BUTTON(4), 
00106     WHEEL_DOWN = SDL_BUTTON(5) 
00107   };
00108 
00110   SigC::Signal3<bool,const Point&,const Point&,Button,BoolMarshal> mouseMove;
00112   SigC::Signal3<bool,Button,bool,const Point&,BoolMarshal> mouseClick;
00114   SigC::Signal0<bool,BoolMarshal> lostMouse;
00116   SigC::Signal0<bool,BoolMarshal> gotMouse;
00117 
00119   void warp(const Point& p) {SDL_WarpMouse(p.x, p.y);}
00121   const Point& position() const {return pos_;}
00123   Button buttons() const {return (Button) SDL_GetMouseState(0, 0);}
00125   bool hidden() const {return !visible_;}
00126 
00128   void setPointer(const Surface&, const Point& hotspot);
00130   void setPointer(Pointer::Resource*);
00132   void setPointer(const std::string& name) {setPointer(Pointer::registry.get(name));}
00133 
00135   const Pointer* getPointer() const {return ptr_->res();}
00136 
00138   void hidePointer() {ptr_->res()->hide(); visible_ = false; update();}
00140   void showPointer() {ptr_->res()->show(); visible_ = true; update();}
00141 
00143   bool handleEvent(const SDL_Event*);
00144 
00145  private:
00146   Mouse(const Mouse&);
00147   Mouse& operator=(const Mouse&);
00148 
00149   void update();
00150 
00151   void destroy() {delete this;} // callback handler
00152 
00154   Point pos_;
00155 
00157   bool visible_;
00158 
00160   Pointer::Resource* ptr_;
00161 
00163   static Mouse* instance_;
00164 }; 
00165 
00166 } // namespace
00167 
00168 #endif
00169 

Generated Fri Mar 5 08:11:00 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.