00001 /* 00002 libwt - Vassilis Virvilis Toolkit - a widget library 00003 Copyright (C) 2006 Vassilis Virvilis <vasvir2@fastmail.fm> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Lesser General Public 00007 License as published by the Free Software Foundation; either 00008 version 2.1 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Lesser General Public License for more details. 00014 00015 You should have received a copy of the GNU Lesser General Public 00016 License along with this library; if not, write to the 00017 Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00018 Boston, MA 02111-1307, SA. 00019 */ 00020 00021 #ifndef WT_PAINTER_H 00022 #define WT_PAINTER_H 00023 00024 #include <stack> 00025 00026 #include <wt/color.h> 00027 #include <wt/font.h> 00028 #include <wt/pixmap.h> 00029 00030 namespace Wt { 00031 00032 class Point; 00033 class Rect; 00034 class Region; 00035 00036 class Pen { 00037 public: 00038 Pen() : 00039 color_("white") {} 00040 00041 Pen(const Color& color) : color_(color) {} 00042 00043 ~Pen() {} 00044 00045 const Color& color() const { 00046 return color_; 00047 } 00048 void setColor(const Color& color) { 00049 color_ = color; 00050 } 00051 00052 private: 00053 Color color_; 00054 /// \todo Pen width, Style, CapStyle, JoinStyle 00055 }; 00056 00057 class Brush { 00058 public: 00059 typedef enum { 00060 NoBrush, 00061 SolidPattern 00062 } Style; 00063 00064 Brush() 00065 : style_(NoBrush), color_("black") {} 00066 00067 Brush(const Color& color, Style style = SolidPattern) 00068 : style_(style), color_(color) {} 00069 00070 ~Brush() {} 00071 00072 Style style() const { 00073 return style_; 00074 } 00075 00076 void setStyle(Style style) { 00077 style_ = style; 00078 } 00079 00080 const Color& color() const { 00081 return color_; 00082 } 00083 00084 void setColor(const Color& color) { 00085 color_ = color; 00086 } 00087 00088 private: 00089 Style style_; 00090 Color color_; 00091 /// \todo more styles, pixmap 00092 }; 00093 00094 /// class that allows drawing graphics primitives onto a Pixmap 00095 class Painter { 00096 public: 00097 /// constructor - binds this Painter to the \p pixmap 00098 Painter(const Pixmap* pixmap = 0, bool clipping = true); 00099 ~Painter(); 00100 00101 bool begin(const Pixmap* pixmap, bool clipping = true); 00102 bool begin(); 00103 bool end(); 00104 00105 Pixmap* pixmap() const { 00106 return pixmap_; 00107 } 00108 bool isActive () const; 00109 00110 const Font& font() const { 00111 return state.font; 00112 } 00113 00114 void setFont(const Font& font) { 00115 state.font = font; 00116 } 00117 00118 const Pen& pen() const { 00119 return state.pen; 00120 } 00121 00122 void setPen(const Pen& pen) { 00123 state.pen = pen; 00124 state.font.setForegroundColor(pen.color()); 00125 } 00126 00127 const Brush& brush () const { 00128 return state.brush; 00129 } 00130 00131 void setBrush(const Brush& brush) { 00132 state.brush = brush; 00133 } 00134 00135 const Color& backgroundColor() const { 00136 return state.bg_color; 00137 } 00138 00139 void setBackgroundColor(const Color& color) { 00140 state.bg_color = color; 00141 state.font.setBackgroundColor(color); 00142 } 00143 00144 void save() { 00145 state_stack.push(state); 00146 } 00147 00148 void restore() { 00149 assert(!state_stack.empty()); 00150 state = state_stack.top(); 00151 state_stack.pop(); 00152 } 00153 00154 void flush() {} 00155 00156 typedef enum { 00157 CopyROP, /// dst = src 00158 OrROP, /// dst = src | dst 00159 AndROP, /// dst = src & dst 00160 XorROP /// dst = src ^ dst 00161 } RasterOp; 00162 00163 RasterOp rasterOp() const { 00164 return state.raster_op; 00165 } 00166 00167 void setRasterOp(RasterOp raster_op) { 00168 state.raster_op = raster_op; 00169 } 00170 00171 void setClipping(bool enable) { 00172 state.clipping = enable; 00173 } 00174 00175 bool clipping() const { 00176 return state.clipping; 00177 } 00178 00179 /// \deprecated 00180 bool hasClipping() const { 00181 return clipping(); 00182 } 00183 00184 void drawPoint(int x, int y); 00185 void drawPoint(const Point& p); 00186 00187 void drawPoints(const PointArray& points, int start = 0, int end = -1); 00188 00189 void drawHorizontalLine(int x1, int x2, int y); 00190 void drawVerticalLine(int x, int y1, int y2); 00191 00192 void drawLine(int x1, int y1, int x2, int y2); 00193 void drawLine(const Point& p1, const Point& p2); 00194 00195 void fillRect(const Rect& r, const Brush& brush); 00196 void fillRect(int x, int y, int w, int h, const Brush& brush); 00197 void fillRegion(const Region& r, const Brush& brush); 00198 void blendRect(const Rect& r, const Brush& brush); 00199 void blendRect(int x, int y, int w, int h, const Brush& brush); 00200 void blendRegion(const Region& r, const Brush& brush); 00201 void eraseRect(const Rect& r); 00202 void eraseRect(int x, int y, int w, int h); 00203 void eraseRegion(const Region& r); 00204 void drawRect(int x, int y, int w, int h); 00205 void drawRect(const Rect& r); 00206 00207 void drawLineSegments(const PointArray& points, 00208 int start = 0, int end = -1); 00209 void drawPolyline(const PointArray& points, int start = 0, int end = -1); 00210 void drawPolygon(const PointArray& points, int start = 0, int end = -1); 00211 00212 void drawPixmap(const Point& p, const Pixmap& src, const Rect& sr); 00213 void drawPixmap(const Point& p, const Pixmap& src); 00214 void drawPixmap(int x, int y, const Pixmap& src, 00215 int sx = 0, int sy = 0, int sw = -1, int sh = -1); 00216 void drawPixmap(const Rect& r, const Pixmap& src); 00217 00218 void drawTiledPixmap(const Rect& r, 00219 const Pixmap& src, const Point& sp); 00220 void drawTiledPixmap(const Rect& r, const Pixmap& src); 00221 void drawTiledPixmap(int x, int y, int w, int h, 00222 const Pixmap& src, int sx = 0, int sy = 0); 00223 /// \todo missing functionality: paragraphs, multi line text, text aligining (low priority) 00224 void drawText(int x, int y, const std::string& text, int len = -1); 00225 void drawText(const Point& p, const std::string& text, int len = -1); 00226 void drawText(int x, int y, const std::string& text, int pos, int len); 00227 void drawText(const Point& p, const std::string& text, int pos, int len); 00228 Rect boundingRect(const std::string& text, int pos = 0, int len = -1); 00229 00230 protected: 00231 bool isClipped(int x, int y) const; 00232 bool isClipped(const Point& p) const; 00233 00234 void rop(int pixeladdr, int pixel); 00235 00236 private: 00237 bool active_; 00238 Pixmap* pixmap_; 00239 00240 class State { 00241 public: 00242 State() : 00243 pen("white"), 00244 brush("gray80"), 00245 bg_color("black"), 00246 raster_op(CopyROP), 00247 clipping(true) {} 00248 00249 Pen pen; 00250 Brush brush; 00251 Color bg_color; 00252 RasterOp raster_op; 00253 bool clipping; 00254 Font font; 00255 }; 00256 00257 std::stack<State> state_stack; 00258 State state; 00259 00260 /// \todo missing: ellipse 00261 }; 00262 00263 } 00264 00265 #endif // !WT_PAINTER_H
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.