style.h

00001 // style.h   -*-c++-*-
00002 //
00003 //   Copyright (C) 2005 Daniel Burrows
00004 //
00005 //   This program is free software; you can redistribute it and/or
00006 //   modify it under the terms of the GNU General Public License as
00007 //   published by the Free Software Foundation; either version 2 of
00008 //   the License, or (at your option) any later version.
00009 //
00010 //   This program 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 //   General Public License for more details.
00014 //
00015 //   You should have received a copy of the GNU General Public License
00016 //   along with this program; see the file COPYING.  If not, write to
00017 //   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018 //   Boston, MA 02111-1307, USA.
00019 
00020 #ifndef STYLE_H
00021 #define STYLE_H
00022 
00023 #include <ncursesw/curses.h>
00024 
00025 #include <cwidget/curses++.h>
00026 
00027 #include <string>
00028 
00029 #include <cwidget/config/colors.h>
00030 
00031 #include <cwidget/generic/util/eassert.h>
00032 
00033 namespace cwidget
00034 {
00051   class style
00052   {
00054     short fg;
00058     short bg;
00059 
00061     attr_t set_attrs;
00063     attr_t clear_attrs;
00067     attr_t flip_attrs;
00068 
00069     // Note: it is assumed that set_attrs and clear_attrs are pairwise
00070     // disjoint.
00071 
00072   public:
00074     style():fg(-1), bg(-2), set_attrs(0), clear_attrs(0), flip_attrs(0)
00075     {
00076     }
00077 
00081     void set_fg(short _fg) {if(_fg >= 0) fg=_fg;}
00082 
00086     void set_bg(short _bg) {if(_bg >= -1) bg = _bg;}
00087 
00089     void attrs_on(attr_t attrs)
00090     {
00091       set_attrs|=attrs;
00092       clear_attrs&=~attrs;
00093       flip_attrs&=~attrs;
00094     }
00095 
00097     void attrs_off(attr_t attrs)
00098     {
00099       clear_attrs|=attrs;
00100       set_attrs&=~attrs;
00101       flip_attrs&=~attrs;
00102     }
00103 
00105     void attrs_flip(attr_t attrs)
00106     {
00107       flip_attrs^=attrs;
00108     }
00109 
00113     void apply_style(const style &other)
00114     {
00115       set_fg(other.fg);
00116       set_bg(other.bg);
00117       attrs_on(other.set_attrs);
00118       attrs_off(other.clear_attrs);
00119       attrs_flip(other.flip_attrs);
00120     }
00121 
00125     style operator+(const style &other) const
00126     {
00127       style rval(*this);
00128       rval+=other;
00129       return rval;
00130     }
00131 
00133     style &operator+=(const style &other)
00134     {
00135       apply_style(other);
00136       return *this;
00137     }
00138 
00139     bool operator==(const style &other) const
00140     {
00141       return fg == other.fg && bg == other.bg &&
00142         set_attrs == other.set_attrs && clear_attrs == other.clear_attrs &&
00143         flip_attrs == other.flip_attrs;
00144     }
00145 
00146     bool operator!=(const style &other) const
00147     {
00148       return fg != other.fg || bg != other.bg ||
00149         set_attrs != other.set_attrs || clear_attrs != other.clear_attrs ||
00150         flip_attrs != other.flip_attrs;
00151     }
00152 
00154     short get_fg() const {return fg<0?0:fg;}
00156     short get_bg() const {return bg<0?0:bg;}
00158     attr_t get_attrs() const
00159     {
00160       attr_t rval=0;
00161       rval |=  set_attrs;
00162       rval &= ~clear_attrs;
00163       rval ^=  flip_attrs;
00164       rval |=  config::mix_color(0, fg, bg);
00165       if(fg == bg)
00166         rval |= A_INVIS;
00167       return rval;
00168     }
00169 
00171     chtype apply_to(chtype ch) const
00172     {
00173       // Relies somewhat on the bitwise representation of attributes;
00174       // the multicharacter-capable stuff needed for utf8 will make this
00175       // go away (for better or for worse..)
00176       return (ch & A_CHARTEXT) |
00177         config::mix_color(ch, fg, bg) |
00178         ((((ch & ~ (A_CHARTEXT | A_COLOR)) | set_attrs) & ~clear_attrs) ^ flip_attrs);
00179     }
00180 
00182     wchtype apply_to(wchtype ch) const
00183     {
00184       // Relies somewhat on the bitwise representation of attributes;
00185       // the multicharacter-capable stuff needed for utf8 will make this
00186       // go away (for better or for worse..)
00187       return wchtype(ch.ch,
00188                      config::mix_color(ch.attrs, fg, bg) |
00189                      ((((ch.attrs & ~ A_COLOR) | set_attrs) & ~clear_attrs) ^ flip_attrs));
00190     }
00191   };
00192 
00193   // To allow styles to be built functionally, the following
00194   // 'constructors' are provided.  The main idea here is to make
00195   // default-setting more compact and less obscure.
00196 
00200   inline style style_fg(short fg)
00201   {
00202     style rval;
00203     rval.set_fg(fg);
00204     return rval;
00205   }
00206 
00210   inline style style_bg(short bg)
00211   {
00212     style rval;
00213     rval.set_bg(bg);
00214     return rval;
00215   }
00216 
00219   inline style style_attrs_on(attr_t attrs)
00220   {
00221     style rval;
00222     rval.attrs_on(attrs);
00223     return rval;
00224   }
00225 
00227   inline style style_attrs_off(attr_t attrs)
00228   {
00229     style rval;
00230     rval.attrs_off(attrs);
00231     return rval;
00232   }
00233 
00235   inline style style_attrs_flip(attr_t attrs)
00236   {
00237     style rval;
00238     rval.attrs_flip(attrs);
00239     return rval;
00240   }
00241 
00246   const style &get_style(const std::string &name);
00247 
00249   void set_style(const std::string &name, const style &style);
00250 }
00251 
00252 #endif // STYLE_H

Generated on Mon Jul 7 11:11:54 2008 for cwidget by  doxygen 1.5.6