| 1 |
/* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */ |
| 2 |
|
| 3 |
/* libmwaw |
| 4 |
* Version: MPL 2.0 / LGPLv2+ |
| 5 |
* |
| 6 |
* The contents of this file are subject to the Mozilla Public License Version |
| 7 |
* 2.0 (the "License"); you may not use this file except in compliance with |
| 8 |
* the License or as specified alternatively below. You may obtain a copy of |
| 9 |
* the License at http://www.mozilla.org/MPL/ |
| 10 |
* |
| 11 |
* Software distributed under the License is distributed on an "AS IS" basis, |
| 12 |
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 13 |
* for the specific language governing rights and limitations under the |
| 14 |
* License. |
| 15 |
* |
| 16 |
* Major Contributor(s): |
| 17 |
* Copyright (C) 2002 William Lachance ([email protected]) |
| 18 |
* Copyright (C) 2002,2004 Marc Maurer ([email protected]) |
| 19 |
* Copyright (C) 2004-2006 Fridrich Strba ([email protected]) |
| 20 |
* Copyright (C) 2006, 2007 Andrew Ziem |
| 21 |
* Copyright (C) 2011, 2012 Alonso Laurent ([email protected]) |
| 22 |
* |
| 23 |
* |
| 24 |
* All Rights Reserved. |
| 25 |
* |
| 26 |
* For minor contributions see the git repository. |
| 27 |
* |
| 28 |
* Alternatively, the contents of this file may be used under the terms of |
| 29 |
* the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"), |
| 30 |
* in which case the provisions of the LGPLv2+ are applicable |
| 31 |
* instead of those above. |
| 32 |
*/ |
| 33 |
|
| 34 |
/** \file MWAWCell.hxx |
| 35 |
* Defines MWAWCell (cell content and format) |
| 36 |
*/ |
| 37 |
|
| 38 |
#ifndef MWAW_CELL_H |
| 39 |
# define MWAW_CELL_H |
| 40 |
|
| 41 |
#include <string> |
| 42 |
#include <vector> |
| 43 |
|
| 44 |
#include "libmwaw_internal.hxx" |
| 45 |
|
| 46 |
#include "MWAWEntry.hxx" |
| 47 |
#include "MWAWFont.hxx" |
| 48 |
|
| 49 |
class MWAWTable; |
| 50 |
|
| 51 |
/** a structure used to define a cell and its format */ |
| 52 |
class MWAWCell |
| 53 |
{ |
| 54 |
public: |
| 55 |
/** the different format of a cell's content */ |
| 56 |
enum FormatType { F_TEXT, F_BOOLEAN, F_NUMBER, F_DATE, F_TIME, F_UNKNOWN }; |
| 57 |
/** the different number format of a cell's content */ |
| 58 |
enum NumberType { F_NUMBER_CURRENCY, F_NUMBER_DECIMAL, F_NUMBER_FRACTION, F_NUMBER_GENERIC, F_NUMBER_SCIENTIFIC, F_NUMBER_PERCENT, F_NUMBER_UNKNOWN }; |
| 59 |
/** a structure uses to define the format of a cell content */ |
| 60 |
struct Format { |
| 61 |
//! constructor |
| 62 |
Format() |
| 63 |
: m_format(F_UNKNOWN) |
| 64 |
, m_numberFormat(F_NUMBER_UNKNOWN) |
| 65 |
, m_digits(-1000) |
| 66 |
, m_integerDigits(-1) |
| 67 |
, m_numeratorDigits(-1) |
| 68 |
, m_denominatorDigits(-1) |
| 69 |
, m_thousandHasSeparator(false) |
| 70 |
, m_parenthesesForNegative(false) |
| 71 |
, m_currencySymbol("$") |
| 72 |
, m_DTFormat("") |
| 73 |
{ |
| 74 |
} |
| 75 |
Format(Format const &)=default; |
| 76 |
Format &operator=(Format const &)=default; |
| 77 |
Format &operator=(Format &&)=default; |
| 78 |
//! destructor |
| 79 |
virtual ~Format(); |
| 80 |
//! returns true if this is a basic format style |
| 81 |
bool hasBasicFormat() const |
| 82 |
{ |
| 83 |
return m_format==F_TEXT || m_format==F_UNKNOWN; |
| 84 |
} |
| 85 |
//! returns a value type |
| 86 |
std::string getValueType() const; |
| 87 |
//! get the numbering style |
| 88 |
bool getNumberingProperties(librevenge::RVNGPropertyList &propList) const; |
| 89 |
//! operator<< |
| 90 |
friend std::ostream &operator<<(std::ostream &o, Format const &format); |
| 91 |
//! a comparison function |
| 92 |
int compare(Format const &format) const; |
| 93 |
|
| 94 |
//! the cell format : by default unknown |
| 95 |
FormatType m_format; |
| 96 |
//! the numeric format |
| 97 |
NumberType m_numberFormat; |
| 98 |
//! the number of digits |
| 99 |
int m_digits; |
| 100 |
//! the number of main digits |
| 101 |
int m_integerDigits; |
| 102 |
//! the number of numerator digits |
| 103 |
int m_numeratorDigits; |
| 104 |
//! the number of denominator digits |
| 105 |
int m_denominatorDigits; |
| 106 |
//! true if we must separate the thousand |
| 107 |
bool m_thousandHasSeparator; |
| 108 |
//! true if we use parenthese to print negative number |
| 109 |
bool m_parenthesesForNegative; |
| 110 |
//! the currency symbol ( default '$') |
| 111 |
std::string m_currencySymbol; |
| 112 |
//! a date/time format ( using a subset of strftime format ) |
| 113 |
std::string m_DTFormat; |
| 114 |
}; |
| 115 |
//! a comparaison structure used to store data |
| 116 |
struct CompareFormat { |
| 117 |
//! constructor |
| 118 |
CompareFormat() {} |
| 119 |
//! comparaison function |
| 120 |
bool operator()(Format const &c1, Format const &c2) const |
| 121 |
{ |
| 122 |
return c1.compare(c2) < 0; |
| 123 |
} |
| 124 |
}; |
| 125 |
/** the default horizontal alignment. |
| 126 |
|
| 127 |
\note actually mainly used for table/spreadsheet cell, FULL is not yet implemented */ |
| 128 |
enum HorizontalAlignment { HALIGN_LEFT, HALIGN_RIGHT, HALIGN_CENTER, |
| 129 |
HALIGN_FULL, HALIGN_DEFAULT |
| 130 |
}; |
| 131 |
|
| 132 |
/** the default vertical alignment. |
| 133 |
\note actually mainly used for table/spreadsheet cell, not yet implemented */ |
| 134 |
enum VerticalAlignment { VALIGN_TOP, VALIGN_CENTER, VALIGN_BOTTOM, VALIGN_DEFAULT }; |
| 135 |
|
| 136 |
//! an enum to defined potential internal line: E_Line1=TL to RB, E_Line2=BL to RT |
| 137 |
enum ExtraLine { E_None, E_Line1, E_Line2, E_Cross }; |
| 138 |
|
| 139 |
//! constructor |
| 140 |
MWAWCell() |
| 141 |
: m_position(0,0) |
| 142 |
, m_numberCellSpanned(1,1) |
| 143 |
, m_bdBox() |
| 144 |
, m_bdSize() |
| 145 |
, m_format() |
| 146 |
, m_font(3,12) |
| 147 |
, m_fontSet(false) |
| 148 |
, m_hAlign(HALIGN_DEFAULT) |
| 149 |
, m_vAlign(VALIGN_DEFAULT) |
| 150 |
, m_rotation(0) |
| 151 |
, m_backgroundColor(MWAWColor::white()) |
| 152 |
, m_protected(false) |
| 153 |
, m_bordersList() |
| 154 |
, m_extraLine(E_None) |
| 155 |
, m_extraLineType() { } |
| 156 |
MWAWCell(MWAWCell const &)=default; |
| 157 |
MWAWCell &operator=(MWAWCell const &)=default; |
| 158 |
MWAWCell &operator=(MWAWCell &&)=default; |
| 159 |
//! destructor |
| 160 |
virtual ~MWAWCell() {} |
| 161 |
|
| 162 |
/** adds to the propList*/ |
| 163 |
void addTo(librevenge::RVNGPropertyList &propList, std::shared_ptr<MWAWFontConverter> fontConverter) const; |
| 164 |
|
| 165 |
//! operator<< |
| 166 |
friend std::ostream &operator<<(std::ostream &o, MWAWCell const &cell); |
| 167 |
|
| 168 |
// interface with MWAWTable: |
| 169 |
|
| 170 |
/** function called when a cell is send by MWAWTable to send a cell to a |
| 171 |
listener. |
| 172 |
|
| 173 |
By default: calls openTableCell(*this), sendContent and then closeTableCell() */ |
| 174 |
virtual bool send(MWAWListenerPtr listener, MWAWTable &table); |
| 175 |
/** function called when the content of a cell must be send to the listener, |
| 176 |
ie. when MWAWTable::sendTable or MWAWTable::sendAsText is called. |
| 177 |
|
| 178 |
\note default behavior: does nothing and prints an error in debug mode.*/ |
| 179 |
virtual bool sendContent(MWAWListenerPtr listener, MWAWTable &table); |
| 180 |
|
| 181 |
// position |
| 182 |
|
| 183 |
//! position accessor |
| 184 |
MWAWVec2i const &position() const |
| 185 |
{ |
| 186 |
return m_position; |
| 187 |
} |
| 188 |
//! set the cell positions : 0,0 -> A1, 0,1 -> A2 |
| 189 |
void setPosition(MWAWVec2i posi) |
| 190 |
{ |
| 191 |
m_position = posi; |
| 192 |
} |
| 193 |
|
| 194 |
//! returns the number of spanned cells |
| 195 |
MWAWVec2i const &numSpannedCells() const |
| 196 |
{ |
| 197 |
return m_numberCellSpanned; |
| 198 |
} |
| 199 |
//! sets the number of spanned cells : MWAWVec2i(1,1) means 1 cellule |
| 200 |
void setNumSpannedCells(MWAWVec2i numSpanned) |
| 201 |
{ |
| 202 |
m_numberCellSpanned=numSpanned; |
| 203 |
} |
| 204 |
|
| 205 |
//! bdbox accessor |
| 206 |
MWAWBox2f const &bdBox() const |
| 207 |
{ |
| 208 |
return m_bdBox; |
| 209 |
} |
| 210 |
//! set the bdbox (unit point) |
| 211 |
void setBdBox(MWAWBox2f box) |
| 212 |
{ |
| 213 |
m_bdBox = box; |
| 214 |
} |
| 215 |
|
| 216 |
//! bdbox size accessor |
| 217 |
MWAWVec2f const &bdSize() const |
| 218 |
{ |
| 219 |
return m_bdSize; |
| 220 |
} |
| 221 |
//! set the bdbox size(unit point) |
| 222 |
void setBdSize(MWAWVec2f sz) |
| 223 |
{ |
| 224 |
m_bdSize = sz; |
| 225 |
} |
| 226 |
//! return the name of a cell (given row and column) : 0,0 -> A1, 0,1 -> A2... |
| 227 |
static std::string getBasicCellName(MWAWVec2i const &pos); |
| 228 |
//! return the name of a cell (given row and column) : 0,0 -> [.A1], 0,1 -> [.A2] |
| 229 |
static std::string getCellName(MWAWVec2i const &pos, MWAWVec2b const &absolute); |
| 230 |
|
| 231 |
//! return the column name |
| 232 |
static std::string getColumnName(int col); |
| 233 |
|
| 234 |
// format |
| 235 |
|
| 236 |
//! returns the cell format |
| 237 |
Format const &getFormat() const |
| 238 |
{ |
| 239 |
return m_format; |
| 240 |
} |
| 241 |
//! set the cell format |
| 242 |
void setFormat(Format const &format) |
| 243 |
{ |
| 244 |
m_format=format; |
| 245 |
} |
| 246 |
|
| 247 |
//! returns true if the font has been set |
| 248 |
bool isFontSet() const |
| 249 |
{ |
| 250 |
return m_fontSet; |
| 251 |
} |
| 252 |
//! returns the font |
| 253 |
MWAWFont getFont() const |
| 254 |
{ |
| 255 |
return m_font; |
| 256 |
} |
| 257 |
//! sets the fonts |
| 258 |
void setFont(MWAWFont const &font, bool isDefault=false) |
| 259 |
{ |
| 260 |
m_font=font; |
| 261 |
m_fontSet=!isDefault; |
| 262 |
} |
| 263 |
|
| 264 |
//! returns true if the cell is protected |
| 265 |
bool isProtected() const |
| 266 |
{ |
| 267 |
return m_protected; |
| 268 |
} |
| 269 |
//! sets the cell's protected flag |
| 270 |
void setProtected(bool fl) |
| 271 |
{ |
| 272 |
m_protected = fl; |
| 273 |
} |
| 274 |
|
| 275 |
//! returns the horizontal alignment |
| 276 |
HorizontalAlignment hAlignment() const |
| 277 |
{ |
| 278 |
return m_hAlign; |
| 279 |
} |
| 280 |
//! sets the horizontal alignment |
| 281 |
void setHAlignment(HorizontalAlignment align) |
| 282 |
{ |
| 283 |
m_hAlign = align; |
| 284 |
} |
| 285 |
|
| 286 |
//! returns the vertical alignment |
| 287 |
VerticalAlignment vAlignment() const |
| 288 |
{ |
| 289 |
return m_vAlign; |
| 290 |
} |
| 291 |
//! sets the vertical alignment |
| 292 |
void setVAlignment(VerticalAlignment align) |
| 293 |
{ |
| 294 |
m_vAlign = align; |
| 295 |
} |
| 296 |
//! returns the rotation angle in degree |
| 297 |
double rotation() const |
| 298 |
{ |
| 299 |
return m_rotation; |
| 300 |
} |
| 301 |
//! sets the rotation angle |
| 302 |
void setRotation(double angle) |
| 303 |
{ |
| 304 |
m_rotation = angle; |
| 305 |
} |
| 306 |
|
| 307 |
//! return true if the cell has some border |
| 308 |
bool hasBorders() const |
| 309 |
{ |
| 310 |
return m_bordersList.size() != 0; |
| 311 |
} |
| 312 |
//! return the cell border: libmwaw::Left | ... |
| 313 |
std::vector<MWAWBorder> const &borders() const |
| 314 |
{ |
| 315 |
return m_bordersList; |
| 316 |
} |
| 317 |
|
| 318 |
//! reset the border |
| 319 |
void resetBorders() |
| 320 |
{ |
| 321 |
m_bordersList.resize(0); |
| 322 |
} |
| 323 |
//! sets the cell border: wh=libmwaw::LeftBit|... |
| 324 |
void setBorders(int wh, MWAWBorder const &border); |
| 325 |
|
| 326 |
//! returns the background color |
| 327 |
MWAWColor backgroundColor() const |
| 328 |
{ |
| 329 |
return m_backgroundColor; |
| 330 |
} |
| 331 |
//! sets the background color |
| 332 |
void setBackgroundColor(MWAWColor color) |
| 333 |
{ |
| 334 |
m_backgroundColor = color; |
| 335 |
} |
| 336 |
//! returns true if we have some extra lines |
| 337 |
bool hasExtraLine() const |
| 338 |
{ |
| 339 |
return m_extraLine!=E_None && !m_extraLineType.isEmpty(); |
| 340 |
} |
| 341 |
//! returns the extra lines |
| 342 |
ExtraLine extraLine() const |
| 343 |
{ |
| 344 |
return m_extraLine; |
| 345 |
} |
| 346 |
//! returns the extra line border |
| 347 |
MWAWBorder const &extraLineType() const |
| 348 |
{ |
| 349 |
return m_extraLineType; |
| 350 |
} |
| 351 |
//! sets the extraline |
| 352 |
void setExtraLine(ExtraLine extrLine, MWAWBorder const &type=MWAWBorder()) |
| 353 |
{ |
| 354 |
m_extraLine = extrLine; |
| 355 |
m_extraLineType=type; |
| 356 |
} |
| 357 |
protected: |
| 358 |
//! the cell row and column : 0,0 -> A1, 0,1 -> A2 |
| 359 |
MWAWVec2i m_position; |
| 360 |
//! the cell spanned : by default (1,1) |
| 361 |
MWAWVec2i m_numberCellSpanned; |
| 362 |
/** the cell bounding box (unit in point)*/ |
| 363 |
MWAWBox2f m_bdBox; |
| 364 |
/** the cell bounding size : unit point */ |
| 365 |
MWAWVec2f m_bdSize; |
| 366 |
|
| 367 |
//! the cell format |
| 368 |
Format m_format; |
| 369 |
//! the cell font |
| 370 |
MWAWFont m_font; |
| 371 |
//! a flag to know if the font has been set |
| 372 |
bool m_fontSet; |
| 373 |
//! the cell alignment : by default nothing |
| 374 |
HorizontalAlignment m_hAlign; |
| 375 |
//! the vertical cell alignment : by default nothing |
| 376 |
VerticalAlignment m_vAlign; |
| 377 |
//! the content's rotation in degree |
| 378 |
double m_rotation; |
| 379 |
//! the backgroung color |
| 380 |
MWAWColor m_backgroundColor; |
| 381 |
//! cell protected |
| 382 |
bool m_protected; |
| 383 |
|
| 384 |
//! the cell border MWAWBorder::Pos |
| 385 |
std::vector<MWAWBorder> m_bordersList; |
| 386 |
/** extra line */ |
| 387 |
ExtraLine m_extraLine; |
| 388 |
/** extra line type */ |
| 389 |
MWAWBorder m_extraLineType; |
| 390 |
}; |
| 391 |
|
| 392 |
//! small class use to define a sheet cell content |
| 393 |
class MWAWCellContent |
| 394 |
{ |
| 395 |
public: |
| 396 |
//! small class use to define a formula instruction |
| 397 |
struct FormulaInstruction { |
| 398 |
enum Type { F_Operator, F_Function, F_Cell, F_CellList, F_Long, F_Double, F_Text, F_Unicode }; |
| 399 |
//! constructor |
| 400 |
FormulaInstruction() |
| 401 |
: m_type(F_Text) |
| 402 |
, m_content("") |
| 403 |
, m_longValue(0) |
| 404 |
, m_doubleValue(0) |
| 405 |
, m_fileName() |
| 406 |
{ |
| 407 |
for (auto &pos : m_position) pos=MWAWVec2i(0,0); |
| 408 |
for (auto &rel : m_positionRelative) rel=MWAWVec2b(false,false); |
| 409 |
} |
| 410 |
/** returns a proplist corresponding to a instruction using a font converter to send the text */ |
| 411 |
librevenge::RVNGPropertyList getPropertyList(MWAWFontConverter &fontConverter, int fontId) const; |
| 412 |
//! operator<< |
| 413 |
friend std::ostream &operator<<(std::ostream &o, FormulaInstruction const &inst); |
| 414 |
//! the type |
| 415 |
Type m_type; |
| 416 |
//! the content ( if type == F_Operator or type = F_Function or type==F_Text) |
| 417 |
std::string m_content; |
| 418 |
//! value ( if type==F_Long ) |
| 419 |
double m_longValue; |
| 420 |
//! value ( if type==F_Double ) |
| 421 |
double m_doubleValue; |
| 422 |
//! cell position ( if type==F_Cell or F_CellList ) |
| 423 |
MWAWVec2i m_position[2]; |
| 424 |
//! relative cell position ( if type==F_Cell or F_CellList ) |
| 425 |
MWAWVec2b m_positionRelative[2]; |
| 426 |
//! the sheet names (if not empty) |
| 427 |
librevenge::RVNGString m_sheet[2]; |
| 428 |
//! the file name (if not empty) |
| 429 |
librevenge::RVNGString m_fileName; |
| 430 |
}; |
| 431 |
|
| 432 |
/** the different types of cell's field */ |
| 433 |
enum Type { C_NONE, C_TEXT, C_NUMBER, C_FORMULA, C_UNKNOWN }; |
| 434 |
/// constructor |
| 435 |
MWAWCellContent() |
| 436 |
: m_contentType(C_UNKNOWN) |
| 437 |
, m_value(0.0) |
| 438 |
, m_valueSet(false) |
| 439 |
, m_textEntry() |
| 440 |
, m_formula() { } |
| 441 |
MWAWCellContent(MWAWCellContent const &)=default; |
| 442 |
MWAWCellContent &operator=(MWAWCellContent const &)=default; |
| 443 |
MWAWCellContent &operator=(MWAWCellContent &&)=default; |
| 444 |
/// destructor |
| 445 |
~MWAWCellContent() {} |
| 446 |
//! operator<< |
| 447 |
friend std::ostream &operator<<(std::ostream &o, MWAWCellContent const &cell); |
| 448 |
|
| 449 |
//! returns true if the cell has no content |
| 450 |
bool empty() const |
| 451 |
{ |
| 452 |
if (m_contentType == C_NUMBER) return false; |
| 453 |
if (m_contentType == C_TEXT && m_textEntry.valid()) return false; |
| 454 |
if (m_contentType == C_FORMULA && (m_formula.size() || isValueSet())) return false; |
| 455 |
return true; |
| 456 |
} |
| 457 |
//! sets the double value |
| 458 |
void setValue(double value) |
| 459 |
{ |
| 460 |
m_value = value; |
| 461 |
m_valueSet = true; |
| 462 |
} |
| 463 |
//! returns true if the value has been setted |
| 464 |
bool isValueSet() const |
| 465 |
{ |
| 466 |
return m_valueSet; |
| 467 |
} |
| 468 |
//! returns true if the text is set |
| 469 |
bool hasText() const |
| 470 |
{ |
| 471 |
return m_textEntry.valid(); |
| 472 |
} |
| 473 |
/** conversion beetween double days since 1900 and a date, ie val=0 |
| 474 |
corresponds to 1/1/1900, val=365 to 1/1/1901, ... */ |
| 475 |
static bool double2Date(double val, int &Y, int &M, int &D); |
| 476 |
/** conversion beetween double: second since 0:00 and time */ |
| 477 |
static bool double2Time(double val, int &H, int &M, int &S); |
| 478 |
/** conversion of the value in string knowing the cell format */ |
| 479 |
static bool double2String(double val, MWAWCell::Format const &format, std::string &str); |
| 480 |
/** conversion beetween date and double days since 1900 date */ |
| 481 |
static bool date2Double(int Y, int M, int D, double &val); |
| 482 |
//! the content type ( by default unknown ) |
| 483 |
Type m_contentType; |
| 484 |
//! the cell value |
| 485 |
double m_value; |
| 486 |
//! true if the value has been set |
| 487 |
bool m_valueSet; |
| 488 |
//! the cell string |
| 489 |
MWAWEntry m_textEntry; |
| 490 |
//! the formula list of instruction |
| 491 |
std::vector<FormulaInstruction> m_formula; |
| 492 |
}; |
| 493 |
|
| 494 |
#endif |
| 495 |
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |