SimpleXlsx  r0.19
 All Classes Namespaces Functions Variables Enumerations Pages
Workbook.h
1 #ifndef __WORKBOOK_H__
2 #define __WORKBOOK_H__
3 
4 #include "Worksheet.h"
5 #include "Chartsheet.h"
6 #include "SimpleXlsxDef.h"
7 
8 namespace xmlw {
9  class XmlStream;
10 }
11 
12 namespace SimpleXlsx {
13 bool MakeDirectory(const _tstring& dirName);
14 
16 class StyleList {
17 public:
18  static const size_t BUILT_IN_STYLES_NUMBER = 164;
19  static const size_t STYLE_LINK_NUMBER = 4;
20 
21  enum {
22  STYLE_LINK_BORDER = 0,
23  STYLE_LINK_FONT,
24  STYLE_LINK_FILL,
25  STYLE_LINK_NUM_FORMAT
26  };
27 
28 private:
29  size_t fmtLastId;
30 
31  std::vector<Border> borders;
32  std::vector<Font> fonts;
33  std::vector<Fill> fills;
34  std::vector<NumFormat> nums;
35  std::vector<std::vector<size_t> > styleIndexes;
36 
37 
38 
39 
40  std::vector< std::pair<std::pair<EAlignHoriz, EAlignVert>, bool> > stylePos;
41 
42 
43 
44 public:
46  fmtLastId = BUILT_IN_STYLES_NUMBER;
47 
48  borders.clear();
49  fonts.clear();
50  fills.clear();
51  nums.clear();
52  styleIndexes.clear();
53  stylePos.clear();
54  }
55 
57  std::vector<Border> GetBorders()const { return borders; }
59  std::vector<Font> GetFonts() const { return fonts; }
61  std::vector<Fill> GetFills() const { return fills; }
63  std::vector<NumFormat> GetNumFormats() const { return nums; }
65  std::vector<std::vector<size_t> > GetIndexes() const { return styleIndexes; }
67  std::vector< std::pair<std::pair<EAlignHoriz, EAlignVert>, bool> > GetPositions() const { return stylePos; }
68 
75  size_t Add(const Style& style) {
76  std::vector<size_t> styleLinks(STYLE_LINK_NUMBER);
77 
78  // Check border existance
79  bool addItem = true;
80  for (size_t i = 0; i < borders.size(); i++) {
81  if (borders[i] == style.border) {
82  addItem = false;
83  styleLinks[STYLE_LINK_BORDER] = i;
84  break;
85  }
86  }
87 
88  // Add border if it is not in collection yet
89  if (addItem) {
90  borders.push_back(style.border);
91  styleLinks[STYLE_LINK_BORDER] = borders.size() - 1;
92  }
93 
94  // Check font existance
95  addItem = true;
96  for (size_t i = 0; i < fonts.size(); i++) {
97  if (fonts[i] == style.font) {
98  addItem = false;
99  styleLinks[STYLE_LINK_FONT] = i;
100  break;
101  }
102  }
103 
104  // Add font if it is not in collection yet
105  if (addItem) {
106  fonts.push_back(style.font);
107  styleLinks[STYLE_LINK_FONT] = fonts.size() - 1;
108  }
109 
110  // Check fill existance
111  addItem = true;
112  for (size_t i = 0; i < fills.size(); i++) {
113  if (fills[i] == style.fill) {
114  addItem = false;
115  styleLinks[STYLE_LINK_FILL] = i;
116  break;
117  }
118  }
119 
120  // Add fill if it is not in collection yet
121  if (addItem) {
122  fills.push_back(style.fill);
123  styleLinks[STYLE_LINK_FILL] = fills.size() - 1;
124  }
125 
126  // Check number format existance
127  addItem = true;
128  for (size_t i = 0; i < nums.size(); i++) {
129  if (nums[i] == style.numFormat) {
130  addItem = false;
131  styleLinks[STYLE_LINK_NUM_FORMAT] = i;
132  break;
133  }
134  }
135 
136  // Add number format if it is not in collection yet
137  if (addItem) {
138  if (style.numFormat.id >= BUILT_IN_STYLES_NUMBER) {
139  styleLinks[STYLE_LINK_NUM_FORMAT] = fmtLastId;
140  style.numFormat.id = fmtLastId++;
141  }
142  else {
143  styleLinks[STYLE_LINK_NUM_FORMAT] = nums.size();
144  }
145 
146  nums.push_back(style.numFormat);
147  }
148 
149  // Check style combination existance
150  for (size_t i = 0; i < styleIndexes.size(); i++) {
151  if (styleIndexes[i] == styleLinks &&
152  stylePos[i].first.first == style.horizAlign &&
153  stylePos[i].first.second == style.vertAlign &&
154  stylePos[i].second == style.wrapText)
155  return i;
156  }
157 
158  std::pair<std::pair<EAlignHoriz, EAlignVert>, bool> pos;
159  pos.first.first = style.horizAlign;
160  pos.first.second = style.vertAlign;
161  pos.second = style.wrapText;
162  stylePos.push_back(pos);
163 
164  styleIndexes.push_back(styleLinks);
165  return styleIndexes.size() - 1;
166  }
167 };
168 
169 // ****************************************************************************
171 // ****************************************************************************
172 class CWorkbook {
173  _tstring m_temp_path;
174  std::vector<_tstring> m_contentFiles;
175  std::vector<CWorksheet*> m_worksheets;
176  std::vector<CChartsheet*> m_charts;
177  std::map<_tstring, uint64_t>m_sharedStrings;
178  std::vector<Comment> m_comments;
179 
180  mutable size_t m_commLastId;
181 
182 public:
183  // @section Constructors / destructor
184  CWorkbook();
185  virtual ~CWorkbook();
186 
187  // @section User interface
188  StyleList m_styleList;
189 
190  CWorksheet& AddSheet(const _tstring& title);
191  CWorksheet& AddSheet(const _tstring& title, std::vector<ColumnWidth>& colWidths);
192  CWorksheet& AddSheet(const _tstring& title, uint32_t frozenWidth, uint32_t frozenHeight);
193  CWorksheet& AddSheet(const _tstring& title, uint32_t frozenWidth, uint32_t frozenHeight, std::vector<ColumnWidth>& colWidths);
194 
195  CChartsheet& AddChart(const _tstring& title);
196  CChartsheet& AddChart(const _tstring& title, EChartTypes type);
197 
198  bool Save(const _tstring& name);
199 
200 private:
201  void Init();
202 
203  bool SaveCore() const;
204  bool SaveContentType();
205  bool SaveApp() const;
206  bool SaveTheme() const;
207  bool SaveStyles() const;
208  bool SaveChain();
209  bool SaveComments();
210  bool SaveSharedStrings();
211  bool SaveWorkbook() const;
212 
213  bool SaveCommentList(std::vector<Comment*> &comments);
214  void AddComment(xmlw::XmlStream &xml_stream, const Comment &comment) const;
215  void AddCommentDrawing(xmlw::XmlStream &xml_stream, const Comment &comment) const;
216  void AddNumberFormats(xmlw::XmlStream& stream) const;
217  void AddFonts(xmlw::XmlStream& stream) const;
218  void AddFills(xmlw::XmlStream& stream) const;
219  void AddBorders(xmlw::XmlStream& stream) const;
220  void AddBorder(xmlw::XmlStream& stream, const TCHAR *borderName, Border::BorderItem border) const;
221 
222  void ClearTemp();
223 
224  static _tstring GetFormatCodeString(const NumFormat &fmt);
225  static _tstring GetFormatCodeColor(ENumericStyleColor color);
226 };
227 
228 } // namespace SimpleXlsx
229 
230 #endif // __WORKBOOK_H__