42#include <wx/printdlg.h>
43#include <wx/artprov.h>
44#include <wx/stdpaths.h>
46#include <wx/listctrl.h>
47#include <wx/aui/aui.h>
49#include <wx/progdlg.h>
52#include <wx/tokenzr.h>
54#if wxCHECK_VERSION(2, 9, 0)
74#include "printtable.h"
78void PrintCell::Init(
const wxString& _content, wxDC* _dc,
int _width,
79 int _cellpadding,
bool _bold_font) {
80 bold_font = _bold_font;
83 cellpadding = _cellpadding;
89void PrintCell::Adjust() {
90 wxFont orig_font = dc->GetFont();
91 wxFont _font = orig_font;
93 _font.SetWeight(wxFONTWEIGHT_BOLD);
96 vector<wxString> list;
97 list.push_back(wxString());
98 wxString separator = wxT(
" ");
99 wxStringTokenizer tokenizer(content, separator, wxTOKEN_RET_DELIMS);
100 int words_number = 0;
101 while (tokenizer.HasMoreTokens()) {
102 wxString token = tokenizer.GetNextToken();
105 wxString tmp = list[list.size() - 1];
106 wxString tmp2 = tmp + token;
108 dc->GetMultiLineTextExtent(tmp2, &w, &h);
109 if ((w < width - 2 * cellpadding) || words_number == 1) {
110 list[list.size() - 1] = tmp2;
112 list.push_back(wxString());
116 for (
size_t i = 0; i < list.size() - 1; i++) {
117 modified_content = modified_content + list[i] + _T(
'\n');
120 modified_content = modified_content + list[list.size() - 1];
124 dc->GetMultiLineTextExtent(modified_content, &w, &h);
127 dc->SetFont(orig_font);
134 state = TABLE_SETUP_WIDTHS;
135 create_next_row =
true;
139 for (vector<vector<wxString> >::iterator iter = data.begin();
140 iter != data.end(); iter++) {
147 if (create_next_row) {
149 create_next_row =
false;
153void Table::NewRow() {
154 vector<wxString> empty_row;
155 data.push_back(empty_row);
158Table& Table::operator<<(
const double& cellcontent) {
159 if (state == TABLE_SETUP_WIDTHS) {
160 widths.push_back(cellcontent);
163 if (state == TABLE_FILL_DATA) {
166 string _cellcontent = sstr.str();
168 wxString _str(_cellcontent.c_str(), wxConvUTF8);
169 data[data.size() - 1].push_back(_str);
174Table& Table::operator<<(
const wxString& cellcontent) {
176 if (state == TABLE_FILL_HEADER) {
178 header.push_back(cellcontent);
181 if (state == TABLE_SETUP_WIDTHS) {
183 state = TABLE_FILL_DATA;
186 if ((cellcontent ==
"\n")) {
187 create_next_row =
true;
190 data[data.size() - 1].push_back(cellcontent);
194Table& Table::operator<<(
const int& cellcontent) {
195 if (state == TABLE_SETUP_WIDTHS) {
196 widths.push_back((
double)cellcontent);
199 if (state == TABLE_FILL_DATA) {
202 string _cellcontent = sstr.str();
204 wxString _str(_cellcontent.c_str(), wxConvUTF8);
205 data[data.size() - 1].push_back(_str);
210ostream& operator<<(ostream& out,
Table& table) {
211 vector<vector<wxString> >& data = table.GetData();
213 for (vector<vector<wxString> >::iterator iter = data.begin();
214 iter != data.end(); iter++) {
215 vector<wxString> row = (*iter);
216 for (vector<wxString>::iterator rowiter = row.begin(); rowiter != row.end();
218 out << (*rowiter).fn_str() <<
" ";
225PrintTable::PrintTable() :
Table() { rows_heights.clear(); }
227void PrintTable::AdjustCells(wxDC* dc,
int marginX,
int marginY) {
228 number_of_pages = -1;
231 for (
size_t j = 0; j < widths.size(); j++) {
238 double scale_x, scale_y;
239 dc->GetUserScale(&scale_x, &scale_y);
243 int width = w - 4 * marginX;
245 for (
size_t j = 0; j < header.size(); j++) {
246 int cell_width = (int)((
double)width * widths[j] / sum);
248 cell_content.Init(header[j], dc, cell_width, 10,
true);
249 header_content.push_back(cell_content);
250 header_height = std::max(header_height, cell_content.GetHeight());
253 for (
size_t i = 0; i < data.size(); i++) {
254 vector<wxString> row = data[i];
255 vector<PrintCell> contents_row;
257 for (
size_t j = 0; j < row.size(); j++) {
258 int cell_width = (int)((
double)width * widths[j] / sum);
260 cell_content.Init(row[j], dc, cell_width, 10);
261 contents_row.push_back(cell_content);
262 max_height = std::max(max_height, cell_content.GetHeight());
264 rows_heights.push_back(max_height);
265 contents.push_back(contents_row);
268 int stripped_page = h - 4 * marginY - header_height;
269 int current_page = 1;
271 for (
size_t i = 0; i < data.size(); i++) {
272 int row_height = rows_heights[i];
273 if (row_height + current_y > stripped_page) {
275 current_y = row_height;
277 current_y += row_height;
279 int row_page = current_page;
280 vector<PrintCell>& contents_row = contents[i];
281 for (
size_t j = 0; j < contents_row.size(); j++) {
282 contents_row[j].SetPage(row_page);
283 contents_row[j].SetHeight(row_height);
285 number_of_pages = std::max(row_page, number_of_pages);
This class takes multilined string and modifies it to fit into given width for given device.
Represents a NxM simple table with captions.