COOPY » Guide  version 0.6.5
/home/paulfitz/cvs/coopy_scm/coopy/src/libcoopy_core/Highlighter.cpp
Go to the documentation of this file.
00001 
00002 #include <coopy/Highlighter.h>
00003 #include <coopy/DiffRender.h>
00004 
00005 #define FULL_COLOR (65535)
00006 #define HALF_COLOR (65535/2)
00007 
00008 using namespace coopy::store;
00009 using namespace coopy::light;
00010 using namespace std;
00011 
00012 class HighlighterTable : public Table {
00013 private:
00014   DataSheet& sheet;
00015 public:
00016 
00017   HighlighterTable(DataSheet& sheet) : sheet(sheet) {}
00018 
00019   virtual int width() const { return sheet.width(); }
00020   virtual int height() const { return sheet.height(); }
00021 
00022   virtual Cell get_cell(int x, int y) const {
00023     Cell cell = { "", false };
00024     SheetCell cell2 = sheet.cellSummary(x,y);
00025     cell.txt = cell2.text;
00026     cell.is_null = cell2.escaped;
00027     return cell;
00028   }
00029 };
00030 
00031 
00032 class HighlighterDiffRender : public DiffRender {
00033 private:
00034   int x, y;
00035   string row_mode;
00036   DataSheet& sheet;
00037 public:
00038   HighlighterDiffRender(DataSheet& sheet) : sheet(sheet) {}
00039 
00040   virtual bool begin_table() { 
00041     x = y = 0;
00042     return true; 
00043   }
00044 
00045   virtual bool begin_row(const std::string& mode) {
00046     row_mode = mode;
00047     bool bold = false;
00048     int rr = 0, gg = 0, bb = 0;
00049     if (row_mode=="+++") {
00050       rr = HALF_COLOR;
00051       gg = FULL_COLOR;
00052       bb = HALF_COLOR;
00053       bold = true;
00054     } else if (row_mode=="---") {
00055       rr = FULL_COLOR;
00056       gg = HALF_COLOR;
00057       bb = HALF_COLOR;
00058     } else if (row_mode=="!") {
00059       rr = HALF_COLOR;
00060       gg = HALF_COLOR;
00061       bb = HALF_COLOR;
00062     } else if (row_mode=="@@") {
00063       rr = HALF_COLOR;
00064       gg = FULL_COLOR;
00065       bb = FULL_COLOR;
00066       bold = true;
00067     }
00068     if (rr!=0||gg!=0||bb!=0) {
00069       Poly<Appearance> appear = sheet.getRowAppearance(y);
00070       if (appear.isValid()) {
00071         appear->begin();
00072         appear->setBackgroundRgb16(rr,gg,bb,
00073                                    AppearanceRange::full());
00074         if (bold) {
00075           appear->setWeightBold(true,AppearanceRange::full());
00076         }
00077         appear->end();
00078       }
00079     }
00080     return true;
00081   }
00082 
00083   virtual bool insert_cell(const Cell& cell,
00084                            const std::string& mode,
00085                            const std::string& separator) {
00086     bool bold = false;
00087     int rr = 0, gg = 0, bb = 0;
00088     if (mode=="+++") {
00089       rr = HALF_COLOR;
00090       gg = FULL_COLOR;
00091       bb = HALF_COLOR;
00092       bold = true;
00093     } else if (mode=="---") {
00094       rr = FULL_COLOR;
00095       gg = HALF_COLOR;
00096       bb = HALF_COLOR;
00097     } else if (mode=="->") {
00098       rr = HALF_COLOR;
00099       gg = HALF_COLOR;
00100       bb = FULL_COLOR;
00101       bold = true;
00102     }
00103     if (rr!=0||gg!=0||bb!=0) {
00104       Poly<Appearance> appear = sheet.getCellAppearance(x,y);
00105       if (appear.isValid()) {
00106         appear->begin();
00107         appear->setBackgroundRgb16(rr,gg,bb,
00108                                    AppearanceRange::full());
00109         if (bold) {
00110           appear->setWeightBold(true,AppearanceRange::full());
00111         }
00112         appear->end();
00113       }
00114     }
00115     x++;
00116     return true;
00117   }
00118 
00119   virtual bool end_row() {
00120     x = 0;
00121     y++;
00122     return true;
00123   }
00124 
00125   virtual bool end_table() {
00126     return true;
00127   }
00128 
00129   virtual std::string to_string() const {
00130     return "";
00131   }
00132 };
00133 
00134 
00135 bool Highlighter::apply(DataSheet& sheet) {
00136   HighlighterTable table(sheet);
00137   HighlighterDiffRender render(sheet);
00138   return render.render(table);
00139 }
00140 
00141 bool Highlighter::apply(TextBook& book) {
00142   bool ok = true;
00143   int len = book.getSheetCount();
00144   for (int i=0; i<len; i++) {
00145     PolySheet sheet = book.readSheetByIndex(i);
00146     ok = ok && apply(sheet);
00147   }
00148   return ok;
00149 }
00150 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines