COOPY » Guide  version 0.6.5
/home/paulfitz/cvs/coopy_scm/coopy/src/libgnumeric/GnumericSheet.cpp
Go to the documentation of this file.
00001 #include <coopy/Dbg.h>
00002 
00003 #include <coopy/GnumericSheet.h>
00004 
00005 extern "C" {
00006 #include <coopy/gnumeric_link.h>
00007 }
00008 
00009 using namespace coopy::store;
00010 using namespace coopy::store::gnumeric;
00011 
00012 using namespace std;
00013 
00014 #define SHEET(x) ((GnumericSheetPtr)(x))
00015 
00016 GnumericSheet::GnumericSheet(void *sheet) {
00017   w = h = 0;
00018   implementation = sheet;
00019   if (implementation!=NULL) {
00020     gnumeric_sheet_get_size(SHEET(implementation),&w,&h);
00021     dbg_printf("Sheet size is %d %d\n", w, h);
00022   }
00023 }
00024 
00025 GnumericSheet::~GnumericSheet() {
00026 }
00027 
00028 
00029 SheetCell GnumericSheet::cellSummary(int x, int y) const {
00030   SheetCell cell;
00031   GSheetCell gscell;
00032   gsheetcell_zero(&gscell);
00033   int r = gnumeric_sheet_get_cell(SHEET(implementation),
00034                                   x, y,
00035                                   &gscell);
00036   if (r!=0) return SheetCell();
00037   if (!gscell.is_url) {
00038     if (gscell.all==NULL) {
00039       gsheetcell_free(&gscell);
00040       cell.text = "NULL";
00041       return cell;
00042     }
00043     cell.text = gscell.all;
00044     cell.escaped = false;
00045     gsheetcell_free(&gscell);
00046     return cell;
00047   }
00048   SheetCellSimpleMeta *meta = new SheetCellSimpleMeta;
00049   COOPY_ASSERT(meta);
00050   if (gscell.all) {
00051     cell.text = gscell.all;
00052   }
00053   if (gscell.url) {
00054     meta->url = gscell.url;
00055   }
00056   if (gscell.txt) {
00057     meta->txt = gscell.txt;
00058   }
00059   gsheetcell_free(&gscell);
00060   cell.meta = Poly<SheetCellMeta>(meta,true);
00061   cell.escaped = false;
00062   return cell;
00063 }
00064 
00065 bool GnumericSheet::cellSummary(int x, int y, const SheetCell& c) {
00066   if (!c.meta.isValid()) {
00067     if (!c.escaped) {
00068       return gnumeric_sheet_set_cell_as_string(SHEET(implementation),
00069                                                x, y,
00070                                                c.text.c_str()) == 0;
00071     }
00072     return gnumeric_sheet_remove_cell(SHEET(implementation),
00073                                       x, y) == 0;
00074   }
00075   //printf("SET a URL: %s\n", c.toString().c_str());
00076   GSheetCell gscell;
00077   gsheetcell_zero(&gscell);
00078   gscell.all = (char*)c.text.c_str();
00079   SheetCellMeta *meta = c.meta.getContent();
00080   if (meta->isUrl()) {
00081     gscell.is_url = 1;
00082     gscell.url = (char*)meta->getUrl().c_str();
00083     gscell.txt = (char*)meta->getText().c_str();
00084   }
00085   bool ok = gnumeric_sheet_set_cell(SHEET(implementation),
00086                                     x, y, &gscell) == 0;
00087   //printf("REREAD: %s\n", cellSummary(x,y).toString().c_str());
00088   return ok;
00089 }
00090 
00091 ColumnRef GnumericSheet::moveColumn(const ColumnRef& src, 
00092                                     const ColumnRef& base) {
00093   int i0 = src.getIndex();
00094   int i1 = base.getIndex();
00095   if (i0<0) return ColumnRef();
00096   if (i1<0) i1 = width();
00097   int i2 = i1;
00098   if (i2>i0) {
00099     i2--;
00100   }
00101   gnumeric_move_column(SHEET(implementation),i0,i2);
00102 
00103   return ColumnRef(i2);
00104 }
00105 
00106 
00107 bool GnumericSheet::deleteColumn(const ColumnRef& column) {
00108   int index = column.getIndex();
00109   if (index>=w) return true;
00110   bool ok = gnumeric_delete_column(SHEET(implementation),index) == 0;
00111   if (!ok) return false;
00112   w--;
00113   return true;
00114 }
00115 
00116 ColumnRef GnumericSheet::insertColumn(const ColumnRef& base) {
00117   return insertColumn(base,ColumnInfo());
00118 }
00119 
00120 
00121 ColumnRef GnumericSheet::insertColumn(const ColumnRef& base, 
00122                                       const ColumnInfo& info) {
00123   int index = base.getIndex();
00124   if (index==-1) { 
00125     index = width();
00126   }
00127   if (index>=w) {
00128     w = index+1;
00129     return ColumnRef(index);
00130   }
00131   bool ok = gnumeric_insert_column(SHEET(implementation),index) == 0;
00132   if (!ok) { printf("fail\n"); return ColumnRef(); }
00133   w++;
00134   if (info.hasName()) {
00135     string n = info.getName();
00136     if (n[0]!='{') {
00137       if (height()>0) {
00138         cellString(index,0,info.getName());
00139       }
00140     }
00141   }
00142   return ColumnRef(index);
00143 }
00144 
00145 bool GnumericSheet::modifyColumn(const ColumnRef& base, 
00146                                  const ColumnInfo& info) {
00147   if (info.hasName()) {
00148     if (height()>0) {
00149       cellString(base.getIndex(),0,info.getName().c_str());
00150       return true;
00151     }
00152   }
00153   return false;
00154 }
00155 
00156 
00157 RowRef GnumericSheet::insertRow(const RowRef& base) {
00158   //printf("inserting row\n");
00159   int index = base.getIndex();
00160   if (index==-1) {
00161     h++;
00162     return RowRef(h-1);
00163   }
00164   if (index>=h) {
00165     h = index+1;
00166     return RowRef(index);
00167   }
00168   bool ok = gnumeric_insert_row(SHEET(implementation),index) == 0;
00169   //if (!ok) { return RowRef(); }
00170   h++;
00171   //printf("inserted row\n");
00172   return RowRef(index);  
00173 }
00174 
00175 bool GnumericSheet::deleteRow(const RowRef& src) {
00176   int index = src.getIndex();
00177   if (index>=h) return true;
00178   bool ok = gnumeric_delete_row(SHEET(implementation),index) == 0;
00179   if (!ok) return false;
00180   h--;
00181   return true;
00182 }
00183 
00184 bool GnumericSheet::deleteRows(const RowRef& first, const RowRef& last) {
00185   int index0 = first.getIndex();
00186   int index1 = last.getIndex();
00187   if (index0>=h) return true;
00188   if (index1>=h) index1 = h-1;
00189   int rem = gnumeric_delete_rows(SHEET(implementation),index0,index1);
00190   h -= rem;
00191   return (rem==(index1-index0+1));
00192 }
00193 
00194 
00195 RowRef GnumericSheet::moveRow(const RowRef& src, const RowRef& base) {
00196   int i0 = src.getIndex();
00197   int i1 = base.getIndex();
00198   if (i0<0) return RowRef();
00199   if (i1<0) i1 = height();
00200   int i2 = i1;
00201   if (i2>i0) {
00202     i2--;
00203   }
00204   gnumeric_move_row(SHEET(implementation),i0,i2);
00205 
00206   return RowRef(i2);
00207 }
00208 
00209 bool GnumericSheet::deleteData(int offset) {
00210   //bool ok = gnumeric_delete_data(SHEET(implementation)) == 0;
00211   bool ok = gnumeric_delete_rows(SHEET(implementation),offset,height()) == 0;
00212   h = 0;
00213   return ok;
00214 }
00215 
00216 
00217 #define APPEAR_CELL 0
00218 #define APPEAR_ROW 1
00219 #define APPEAR_COL 2
00220 
00221 class GnumericAppearance : public Appearance {
00222 public:
00223   GnumericSheetPtr sheet;
00224   int x, y;
00225   int mode;
00226   GnumericStylePtr style;
00227 
00228   static GnumericAppearance *forCell(GnumericSheetPtr sheet, int x, int y) {
00229     GnumericAppearance *r = new GnumericAppearance(sheet,x,y,APPEAR_CELL);
00230     COOPY_ASSERT(r!=NULL);
00231     return r;
00232   }
00233 
00234   static GnumericAppearance *forRow(GnumericSheetPtr sheet, int y) {
00235     GnumericAppearance *r = new GnumericAppearance(sheet,0,y,APPEAR_ROW);
00236     COOPY_ASSERT(r!=NULL);
00237     return r;
00238   }
00239 
00240   static GnumericAppearance *forCol(GnumericSheetPtr sheet, int x) {
00241     GnumericAppearance *r = new GnumericAppearance(sheet,x,0,APPEAR_COL);
00242     COOPY_ASSERT(r!=NULL);
00243     return r;
00244   }
00245 
00246   GnumericAppearance(GnumericSheetPtr sheet, int x, int y, int mode) : 
00247     sheet(sheet),x(x), y(y), mode(mode) {
00248     style = 0/*NULL*/;
00249   }
00250 
00251   bool begin() {
00252     style = gnumeric_sheet_get_style(sheet,x,y);
00253     return true;
00254   }
00255 
00256   virtual bool setForegroundRgb16(int r, int g, int b, 
00257                                   const AppearanceRange& range) {    
00258     gnumeric_style_set_font_color(style,r,g,b);
00259     return true;
00260   }
00261   
00262   virtual bool setBackgroundRgb16(int r, int g, int b, 
00263                                   const AppearanceRange& range) {
00264     gnumeric_style_set_back_color(style,r,g,b);
00265     return true;
00266   }
00267 
00268   virtual bool setWeightBold(bool flag, const AppearanceRange& range) {
00269     gnumeric_style_set_font_bold(style,flag);
00270     return true;
00271   }
00272 
00273   virtual bool setStrikethrough(bool flag, const AppearanceRange& range) {
00274     gnumeric_style_set_font_strike(style,flag);
00275     return true;
00276   }
00277 
00278   bool end() {
00279     switch (mode) {
00280     case APPEAR_CELL:
00281       gnumeric_sheet_set_style(sheet,style,x,y);
00282       break;
00283     case APPEAR_ROW:
00284       gnumeric_sheet_set_row_style(sheet,style,y);
00285       break;
00286     case APPEAR_COL:
00287       gnumeric_sheet_set_col_style(sheet,style,x);
00288       break;
00289     }
00290     return true;
00291   }
00292 };
00293 
00294 Poly<Appearance> GnumericSheet::getCellAppearance(int x, int y) {
00295   return Poly<Appearance>(GnumericAppearance::forCell(SHEET(implementation),x,y),
00296                           true);
00297 }
00298 
00299 Poly<Appearance> GnumericSheet::getRowAppearance(int y) {
00300   return Poly<Appearance>(GnumericAppearance::forRow(SHEET(implementation),y),
00301                           true);
00302 }
00303 
00304 Poly<Appearance> GnumericSheet::getColAppearance(int x) {
00305   return Poly<Appearance>(GnumericAppearance::forCol(SHEET(implementation),x),
00306                           true);
00307 }
00308 
00309 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines