tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / svl / source / misc / gridprinter.cxx
blobc6a1bdd5f9daf6a5542a25d75bc8b6949089cfbd
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <svl/gridprinter.hxx>
11 #include <rtl/ustrbuf.hxx>
13 #include <mdds/multi_type_vector/types.hpp>
14 #include <mdds/multi_type_vector/macro.hpp>
15 #include <mdds/multi_type_matrix.hpp>
17 #include <iostream>
19 namespace svl {
21 // String ID
22 const mdds::mtv::element_t element_type_string = mdds::mtv::element_type_user_start;
23 // String block
24 typedef mdds::mtv::default_element_block<element_type_string, OUString> string_block;
26 namespace {
28 struct matrix_traits
30 typedef string_block string_element_block;
31 typedef mdds::mtv::uint16_element_block integer_element_block;
38 namespace rtl {
40 // Callbacks for the string block. This needs to be in the same namespace as
41 // OUString for argument dependent lookup.
42 MDDS_MTV_DEFINE_ELEMENT_CALLBACKS(OUString, svl::element_type_string, OUString(), svl::string_block)
46 namespace svl {
48 typedef mdds::multi_type_matrix<matrix_traits> MatrixImplType;
50 struct GridPrinter::Impl
52 MatrixImplType maMatrix;
53 bool mbPrint;
55 Impl( size_t nRows, size_t nCols, bool bPrint ) :
56 maMatrix(nRows, nCols, OUString()), mbPrint(bPrint) {}
59 GridPrinter::GridPrinter( size_t nRows, size_t nCols, bool bPrint ) :
60 mpImpl(new Impl(nRows, nCols, bPrint)) {}
62 GridPrinter::~GridPrinter()
66 void GridPrinter::set( size_t nRow, size_t nCol, const OUString& rStr )
68 #if defined __GNUC__ && !defined __clang__ && __GNUC__ == 12 && __cplusplus == 202002L
69 #pragma GCC diagnostic push
70 #pragma GCC diagnostic ignored "-Warray-bounds"
71 #endif
72 mpImpl->maMatrix.set(nRow, nCol, rStr);
73 #if defined __GNUC__ && !defined __clang__ && __GNUC__ == 12 && __cplusplus == 202002L
74 #pragma GCC diagnostic pop
75 #endif
78 void GridPrinter::print( const char* pHeader ) const
80 if (!mpImpl->mbPrint)
81 return;
83 if (pHeader)
84 std::cout << pHeader << std::endl;
86 MatrixImplType::size_pair_type ns = mpImpl->maMatrix.size();
87 std::vector<sal_Int32> aColWidths(ns.column, 0);
89 // Calculate column widths first.
90 for (size_t row = 0; row < ns.row; ++row)
92 for (size_t col = 0; col < ns.column; ++col)
94 OUString aStr = mpImpl->maMatrix.get_string(row, col);
95 if (aColWidths[col] < aStr.getLength())
96 aColWidths[col] = aStr.getLength();
100 // Make the row separator string.
101 OUStringBuffer aBuf("+");
102 for (size_t col = 0; col < ns.column; ++col)
104 aBuf.append("-");
105 for (sal_Int32 i = 0; i < aColWidths[col]; ++i)
106 aBuf.append(u'-');
107 aBuf.append("-+");
110 OUString aSep = aBuf.makeStringAndClear();
112 // Now print to stdout.
113 std::cout << aSep << std::endl;
114 for (size_t row = 0; row < ns.row; ++row)
116 std::cout << "| ";
117 for (size_t col = 0; col < ns.column; ++col)
119 OUString aStr = mpImpl->maMatrix.get_string(row, col);
120 size_t nPadding = aColWidths[col] - aStr.getLength();
121 aBuf.append(aStr);
122 for (size_t i = 0; i < nPadding; ++i)
123 aBuf.append(u' ');
124 std::cout << aBuf.makeStringAndClear() << " | ";
126 std::cout << std::endl;
127 std::cout << aSep << std::endl;
133 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */