diff options
author | Kohei Yoshida <kohei.yoshida@collabora.com> | 2014-06-26 13:33:09 -0400 |
---|---|---|
committer | Kohei Yoshida <kohei.yoshida@collabora.com> | 2014-06-26 13:35:33 -0400 |
commit | f15e440d148ed021091ef9b20f3aed2488f0fde3 (patch) | |
tree | 99a1f7c54ffd0c63588d2599075d2be7eebc79da /svl | |
parent | b7de76d825b02005ab8cf02b3977e4be441200c5 (diff) |
Move SheetPrinter to svl and rename it to GridPrinter.
I need to use this outside of sc.
Change-Id: I153863d6c5c31e5ab5f25da2dba81bd4d4b6d3fe
Diffstat (limited to 'svl')
-rw-r--r-- | svl/Library_svl.mk | 1 | ||||
-rw-r--r-- | svl/source/misc/gridprinter.cxx | 141 |
2 files changed, 142 insertions, 0 deletions
diff --git a/svl/Library_svl.mk b/svl/Library_svl.mk index 2b337ff88f1f..d00505eb110a 100644 --- a/svl/Library_svl.mk +++ b/svl/Library_svl.mk @@ -107,6 +107,7 @@ $(eval $(call gb_Library_add_exception_objects,svl,\ svl/source/misc/filenotation \ svl/source/misc/fstathelper \ svl/source/misc/getstringresource \ + svl/source/misc/gridprinter \ svl/source/misc/inethist \ svl/source/misc/inettype \ svl/source/misc/lngmisc \ diff --git a/svl/source/misc/gridprinter.cxx b/svl/source/misc/gridprinter.cxx new file mode 100644 index 000000000000..c36de35fcf44 --- /dev/null +++ b/svl/source/misc/gridprinter.cxx @@ -0,0 +1,141 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <svl/gridprinter.hxx> +#include <rtl/ustrbuf.hxx> + +#include <mdds/multi_type_vector_types.hpp> +#include <mdds/multi_type_vector_trait.hpp> +#include <mdds/multi_type_vector_custom_func1.hpp> +#include <mdds/multi_type_matrix.hpp> + +#include <iostream> + +using namespace std; + +namespace svl { + +// String ID +const mdds::mtv::element_t element_type_string = mdds::mtv::element_type_user_start; +// String block +typedef mdds::mtv::default_element_block<element_type_string, OUString> string_block; + +struct custom_string_trait +{ + typedef OUString string_type; + typedef string_block string_element_block; + + static const mdds::mtv::element_t string_type_identifier = element_type_string; + + typedef mdds::mtv::custom_block_func1<string_block> element_block_func; +}; + +} + +namespace rtl { + +// Callbacks for the string block. This needs to be in the same namespace as +// OUString for argument dependent lookup. +MDDS_MTV_DEFINE_ELEMENT_CALLBACKS(OUString, svl::element_type_string, OUString(), svl::string_block) + +} + +namespace svl { + +typedef mdds::multi_type_matrix<custom_string_trait> MatrixImplType; + +struct GridPrinter::Impl +{ + MatrixImplType maMatrix; + bool mbPrint; + + Impl( size_t nRows, size_t nCols, bool bPrint ) : + maMatrix(nRows, nCols), mbPrint(bPrint) {} +}; + +GridPrinter::GridPrinter( size_t nRows, size_t nCols, bool bPrint ) : + mpImpl(new Impl(nRows, nCols, bPrint)) {} + +GridPrinter::~GridPrinter() +{ + delete mpImpl; +} + +void GridPrinter::set( size_t nRow, size_t nCol, const OUString& rStr ) +{ + mpImpl->maMatrix.set(nRow, nCol, rStr); +} + +void GridPrinter::print( const char* pHeader ) const +{ + if (!mpImpl->mbPrint) + return; + + if (pHeader) + cout << pHeader << endl; + + MatrixImplType::size_pair_type ns = mpImpl->maMatrix.size(); + vector<sal_Int32> aColWidths(ns.column, 0); + + // Calculate column widths first. + for (size_t row = 0; row < ns.row; ++row) + { + for (size_t col = 0; col < ns.column; ++col) + { + OUString aStr = mpImpl->maMatrix.get_string(row, col); + if (aColWidths[col] < aStr.getLength()) + aColWidths[col] = aStr.getLength(); + } + } + + // Make the row separator string. + OUStringBuffer aBuf; + aBuf.appendAscii("+"); + for (size_t col = 0; col < ns.column; ++col) + { + aBuf.appendAscii("-"); + for (sal_Int32 i = 0; i < aColWidths[col]; ++i) + aBuf.append(sal_Unicode('-')); + aBuf.appendAscii("-+"); + } + + OUString aSep = aBuf.makeStringAndClear(); + + // Now print to stdout. + cout << aSep << endl; + for (size_t row = 0; row < ns.row; ++row) + { + cout << "| "; + for (size_t col = 0; col < ns.column; ++col) + { + OUString aStr = mpImpl->maMatrix.get_string(row, col); + size_t nPadding = aColWidths[col] - aStr.getLength(); + aBuf.append(aStr); + for (size_t i = 0; i < nPadding; ++i) + aBuf.append(sal_Unicode(' ')); + cout << aBuf.makeStringAndClear() << " | "; + } + cout << endl; + cout << aSep << endl; + } +} + +void GridPrinter::clear() +{ + mpImpl->maMatrix.clear(); +} + +void GridPrinter::resize( size_t nRows, size_t nCols ) +{ + mpImpl->maMatrix.resize(nRows, nCols); +} + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |