papyrus logo

renderable.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2004 by Rick L. Vinyard, Jr.                            *
00003  *   rvinyard@cs.nmsu.edu                                                  *
00004  *                                                                         *
00005  *   This file is part of the papyrus library.                             *
00006  *                                                                         *
00007  *   papyrus is free software; you can redistribute it and/or modify       *
00008  *   it under the terms of the GNU Lesser General Public License           *
00009  *   version 3.0 as published by the Free Software Foundation.             *
00010  *                                                                         *
00011  *   papyrus 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         *
00014  *   GNU Lesser General Public License version 3.0 for more details.       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with the papyrus library. If not, see                   *
00018  *   <http://www.gnu.org/licenses/>.                                       *
00019  ***************************************************************************/
00020 #ifndef PAPYRUSRENDERABLE_H
00021 #define PAPYRUSRENDERABLE_H
00022 
00023 #include <map>
00024 #include <glibmm/ustring.h>
00025 #include <papyrus/pointer.h>
00026 
00027 #include <papyrus/object.h>
00028 #include <papyrus/utility.h>
00029 #include <papyrus/matrix.h>
00030 #include <cairomm/cairomm.h>
00031 
00032 namespace Papyrus
00033 {
00034   
00035   struct Context {
00036     
00037       Context(): flags(0x00) { }
00038       
00039       Context(Cairo::RefPtr<Cairo::Context> c, uint32_t f=0x00):
00040           cairo(c), flags(f) { }
00041       
00042       bool is_flag_set(RenderFlags rf) { return flags & rf; }
00043       
00044       bool is_flag_unset(RenderFlags rf) { return not(flags & rf); }
00045       
00054       bool set_flag(RenderFlags rf) { 
00055         bool old = flags&rf; 
00056         flags |= rf; 
00057         return old; 
00058       }
00059       
00068       bool unset_flag(RenderFlags rf) { 
00069         bool old = flags&rf;
00070         flags = flags & (~rf); 
00071         return old;
00072       }
00073       
00074       void set_flag(RenderFlags rf, bool value) {
00075         if ( value ) set_flag(rf);
00076         else unset_flag(rf);
00077       }
00078       
00079       void clear_flags() { flags = 0x00; }
00080       
00081       operator Cairo::RefPtr<Cairo::Context>() const { return cairo; }
00082       
00083       Cairo::RefPtr<Cairo::Context> operator*() const { return cairo; }
00084       
00085       Cairo::RefPtr<Cairo::Context> operator->() const { return cairo; }
00086       
00087       operator bool() const { return static_cast<bool>(cairo); }
00088       
00089       Context& operator=(Cairo::RefPtr<Cairo::Context> c)
00090       {
00091         cairo = c;
00092         return *this;
00093       }
00094       
00095       void save() { 
00096         if (is_flag_unset(RENDER_SUPPRESS_CAIRO_SAVE_RESTORE) and cairo)
00097           cairo->save(); 
00098       }
00099        
00100       void transform(const Matrix& matrix) {
00101         if (is_flag_unset(RENDER_SUPPRESS_MATRIX) and cairo)
00102           cairo->transform(matrix);
00103       }
00104       
00105       void restore() { 
00106         if (is_flag_unset(RENDER_SUPPRESS_CAIRO_SAVE_RESTORE) and cairo)
00107           cairo->restore(); 
00108       }
00109       
00110       void fill() { 
00111         if (is_flag_unset(RENDER_SUPPRESS_CAIRO_FILL) and cairo)
00112           cairo->fill(); 
00113       }
00114        
00115       void paint() { 
00116         if (is_flag_unset(RENDER_SUPPRESS_CAIRO_FILL) and cairo)
00117           cairo->paint(); 
00118       }
00119        
00120       void paint_with_alpha(double alpha) { 
00121         if (is_flag_unset(RENDER_SUPPRESS_CAIRO_FILL) and cairo)
00122           cairo->paint_with_alpha(alpha); 
00123       }
00124        
00125       void stroke() { 
00126         if (is_flag_unset(RENDER_SUPPRESS_CAIRO_STROKE) and cairo)
00127           cairo->stroke(); 
00128       }
00129        
00131       void clear() {
00132         cairo.clear();
00133         flags = 0x00;
00134       }
00135 
00136        
00137       Cairo::RefPtr<Cairo::Context> cairo;
00138       uint32_t flags;
00139   };
00140 
00165   class Renderable: public Object
00166   {
00167     protected:
00168       Renderable ( const Glib::ustring& id ) : Object ( id )
00169       { }
00170 
00171     public:
00172       typedef PapyrusPointer<Renderable> pointer;
00173 
00174       virtual ~Renderable()
00175       { }
00176 
00177       virtual void render ( Context& cairo ) const = 0;
00178 
00179       virtual void render( Context& cairo, double x, double y, double w, double h ) const { this->render(cairo); }
00180 
00181       void render ( Cairo::RefPtr<Cairo::Context> cairo ) const
00182       { Context ctx(cairo); this->render(ctx); }
00183 
00184       void render( Cairo::RefPtr<Cairo::Context> cairo, double x, double y, double w, double h ) const 
00185       { Context ctx(cairo); this->render(ctx, x, y, w, h); }
00186 
00190       virtual Glib::ustring svg(unsigned depth=0) /*= 0;*/ { return Glib::ustring(); }
00191 
00192       PAPYRUS_CLASS_NAME ( "Renderable" );
00193 
00194     protected:
00195   
00196       Glib::ustring svg_spacing(unsigned depth)
00197       {
00198         Glib::ustring s;
00199         for ( unsigned i = 0; i < depth; i++ )
00200           s += "  ";
00201         return s;
00202       }
00203 
00204       Glib::ustring svg_id()
00205       {
00206         Glib::ustring s;
00207         if ( ! m_id.empty() )
00208           s += "id=\"" + m_id + "\" ";
00209         return s;
00210       }
00211 
00212 
00213   };
00214 
00215 };
00216 
00217 #endif

Generated on Fri Apr 16 12:40:11 2010 for papyrus by doxygen 1.6.1