File: printtable.h

package info (click to toggle)
opencpn 5.2.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 101,540 kB
  • sloc: ansic: 414,598; cpp: 253,008; xml: 83,748; sh: 409; python: 353; makefile: 110; javascript: 87; perl: 83
file content (274 lines) | stat: -rw-r--r-- 6,812 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/******************************************************************************
 *
 * Project:  OpenCPN
 * Purpose:  OpenCPN Route table printout
 * Author:   Pavel Saviankou
 *
 ***************************************************************************
 *   Copyright (C) 2010 by David S. Register                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,  USA.         *
 ***************************************************************************
 *
 *
 */
#include <iostream>
#include <vector>

#ifndef __PRINTTABLE_H__
#define __PRINTTABLE_H__

#include <wx/print.h>
#include <wx/datetime.h>
#include <wx/cmdline.h>
#include <wx/string.h>
#ifdef __WXMSW__
#include <wx/msw/private.h>
#endif

#include "ocpn_types.h"
#include "navutil.h"

/**
 * \brief
 *  Enumeration is used to notice the state of the table.
 *
 *  Different states are used to signalize different semanic of the data in
 *  the operator << of the class Table.
 *  If the state is "setup columns widths" -> then the data is used to store
 *  the width of the columns.
 *  If the state is "fill with data" -> then the data is the cell content.
 */
enum TableState { TABLE_SETUP_WIDTHS = 0, TABLE_FILL_DATA, TABLE_FILL_HEADER };


/**
 *  \brief Represents a NxM simple table with captions.
 *
 *  Input operator is "<<"
 *  Number of columns and rows are given dynamically by the input data.
 *  Captions are given by first input line.
 *  Every cell is given column by column.
 *  Next row is given by "<< '\n'" (or << endl)
 *
 *
 *
 */
class Table {
protected:
    int nrows;
    int ncols;

    bool create_next_row;

    std::vector< std::vector < wxString > > data;
    std::vector< double > widths;
    std::vector< wxString > header;
    TableState state;

    void Start();

    void NewRow();

public:
    Table();
    ~Table();

    Table& operator<<( const int& );
    Table& operator<<( const double& );
    Table& operator<<( const wxString& );

    std::vector< std::vector < wxString > > & GetData()
    {
        return data;
    };

    void StartFillData()
    {
        state = TABLE_FILL_DATA;
    };

    void StartFillHeader()
    {
        state = TABLE_FILL_HEADER;
    };

    void StartFillWidths()
    {
        state = TABLE_SETUP_WIDTHS;
    };

    int GetRowHeight( int i )
    {
        return widths[ i ];
    };
};

std::ostream& operator<<( std::ostream&,
            Table& );



/**
 *  \brief This class takes multilined string and modifies it to fit into given width
 *         for given device. If it is too wide for given DC (by class PrintTable )
 *        it introduces new lines between words
 *
 */

class PrintCell {
protected:
    // Copy of printing device
    wxDC* dc;

    // Target width
    int width;

    // Target height
    int height;

    // Cellpadding
    int cellpadding;

    // Content of a cell
    wxString content;

    // Result of modification
    wxString modified_content;

    // Rect for printing of modified string
    wxRect rect;

    // Stores page, where this cell will be printed
    int page;

    // Stores, if one has to ovveride property "weight" of the font with the value "bold" - used to print header of the table.
    bool bold_font;


    // Adjust text
    void Adjust();


public:

    // Constructor with content to print and device
    PrintCell ()
    {
    };

    // Constructor with content to print and device
    void Init( const wxString& _content,
          wxDC*           _dc,
          int             _width,
          int             _cellpadding,
          bool            bold_font = false );

    // Returns rect for printing
    wxRect GetRect()
    {
        return rect;
    };

    // Returns modified cell content
    wxString GetText()
    {
        return modified_content;
    };

    // Returns height of the cell
    int GetHeight()
    {
        return height;
    };

    // Returns width of the cell
    int GetWidth()
    {
        return width;
    };

    // sets the page to print
    void SetPage( int _page )
    {
        page = _page;
    };

    // sets the height
    void SetHeight( int _height )
    {
        height = _height;
    };

    // Returns the page, where this element should be painted
    int GetPage()
    {
        return page;
    };
};



/**
 *  \brief Extension of a class Table with printing into dc.
 *
 *   It takes all elements, takes DC as a printing device, takes  a maximal
 *   possible table width,  calculate width of every column.
 *   For printing of every cell it modifies its content so, that it fits into cell by inserting
 *   new lines.
 *
 */
class PrintTable : public Table {
protected:
    std::vector< std::vector < PrintCell > > contents;
    std::vector < PrintCell >           header_content;
    std::vector< int >                  rows_heights;
    int                            header_height;


    int number_of_pages;                // stores the number of pages for printing of this table. It is set by AdjustCells(...)

public:
    PrintTable();

    // creates internally std::vector of PrintCell's, to calculate columns widths and row sizes
    void AdjustCells( wxDC* _dc,
                 int   marginX,
                 int   marginY );

    // delivers content of the table
    std::vector< std::vector < PrintCell > >& GetContent()
    {
        return contents;
    };
    // delivers header  of the table
    std::vector < PrintCell > & GetHeader()
    {
        return header_content;
    };
    // returns the total number of needed pages;
    int GetNumberPages()
    {
        return number_of_pages;
    };

    // Returns the height of the header
    int GetHeaderHeight()
    {
        return header_height;
    };
};
#endif