OpenCores
URL https://opencores.org/ocsvn/pcie_ds_dma/pcie_ds_dma/trunk

Subversion Repositories pcie_ds_dma

[/] [pcie_ds_dma/] [trunk/] [soft/] [linux/] [common/] [console/] [nctable.cpp] - Blame information for rev 8

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 8 v.karak
 
2
#include <stdio.h>
3
#include <stdarg.h>
4
#include <stdlib.h>
5
#include <string.h>
6
#include <unistd.h>
7
#include <fcntl.h>
8
#include <ncurses.h>
9
#include <signal.h>
10
#include <vector>
11
 
12
#ifndef __NCTABLE_H__
13
#include "nctable.h"
14
#endif
15
 
16
//-----------------------------------------------------------------------------
17
 
18
void table::init()
19
{
20
    initscr();
21
    cbreak();
22
    noecho();
23
    keypad(stdscr, TRUE);
24
    nonl();
25
}
26
 
27
//-----------------------------------------------------------------------------
28
 
29
table::table(int cell_width, int cell_height)
30
{
31
    init();
32
 
33
    m_row = 0;
34
    m_colomn = 0;
35
    m_CW = cell_width;
36
    m_CH = cell_height;
37
    m_table.clear();
38
 
39
    getmaxyx(stdscr,m_maxH,m_maxW);
40
 
41
    m_W0 = m_maxW/2;
42
    m_H0 = m_maxH/2;
43
 
44
    m_header.w = NULL;
45
    m_header.X0 = m_header.Y0 = m_header.W = m_header.H = 0;
46
    m_status.clear();
47
    //m_status.w = NULL;
48
    //m_status.X0 = m_status.Y0 = m_status.W = m_status.H = 0;
49
}
50
 
51
//-----------------------------------------------------------------------------
52
 
53
table::table(int colomn_number, int cell_width, int cell_height)
54
{
55
    init();
56
 
57
    m_row = 0;
58
    m_colomn = colomn_number;
59
    m_CW = cell_width;
60
    m_CH = cell_height;
61
    m_table.clear();
62
 
63
    getmaxyx(stdscr,m_maxH,m_maxW);
64
 
65
    m_W0 = m_maxW/2;
66
    m_H0 = m_maxH/2;
67
 
68
    m_header.w = NULL;
69
    m_header.X0 = m_header.Y0 = m_header.W = m_header.H = 0;
70
    m_status.clear();
71
    //m_status.w = NULL;
72
    //m_status.X0 = m_status.Y0 = m_status.W = m_status.H = 0;
73
}
74
 
75
//-----------------------------------------------------------------------------
76
 
77
table::table(int row, int col, int cell_width, int cell_height)
78
{
79
    init();
80
 
81
    m_row = row;
82
    m_colomn = col;
83
    m_CW = cell_width;
84
    m_CH = cell_height;
85
 
86
    getmaxyx(stdscr,m_maxH,m_maxW);
87
 
88
    m_W0 = m_maxW/2;
89
    m_H0 = m_maxH/2;
90
 
91
    m_header.w = NULL;
92
    m_header.X0 = m_header.Y0 = m_header.W = m_header.H = 0;
93
    m_status.clear();
94
    //m_status.w = NULL;
95
    //m_status.X0 = m_status.Y0 = m_status.W = m_status.H = 0;
96
 
97
    create_table(row, col);
98
}
99
 
100
//-----------------------------------------------------------------------------
101
 
102
table::~table()
103
{
104
    clear_table();
105
    clear_header();
106
    clear_status();
107
    endwin();
108
}
109
 
110
//-----------------------------------------------------------------------------
111
 
112
void table::clear_table()
113
{
114
    for(unsigned i=0; i<m_table.size(); i++) {
115
        row_t row = m_table.at(i);
116
        for(unsigned j=0; j<row.w.size(); j++) {
117
            WINDOW *w = row.w.at(j);
118
            delwin(w);
119
        }
120
        row.w.clear();
121
    }
122
    m_table.clear();
123
}
124
 
125
//-----------------------------------------------------------------------------
126
 
127
int table::create_table(int nrow, int ncol)
128
{
129
    clear_table();
130
 
131
    int dX = m_CW * ncol/2;
132
    int dY = m_CH * nrow/2;
133
 
134
    for(int row = 0; row < nrow; row++) {
135
 
136
        int x0 = m_W0 - dX;
137
        int y0 = m_H0 + m_CH * row - dY;
138
 
139
        row_t new_row;
140
 
141
        new_row.X0 = x0;
142
        new_row.Y0 = y0;
143
 
144
        for(int col = 0; col < ncol; col++) {
145
 
146
            int xn = x0 + m_CW * col;
147
            int yn = y0;
148
 
149
            WINDOW *w = newwin(m_CH, m_CW, yn, xn);
150
            if(!w)
151
                break;
152
            box(w, 0 , 0);
153
            wrefresh(w);
154
            new_row.w.push_back(w);
155
        }
156
 
157
        m_table.push_back(new_row);
158
    }
159
 
160
    return m_table.size();
161
}
162
 
163
//-----------------------------------------------------------------------------
164
 
165
int table::add_row()
166
{
167
    int x0 = 0;
168
    int y0 = 0;
169
 
170
    if(m_table.size()) {
171
 
172
        x0 = m_W0 - m_CW * m_colomn/2;
173
        y0 = m_H0 - m_H0/2 + m_CH * m_table.size();
174
 
175
    } else {
176
 
177
        x0 = m_W0 - m_CW * m_colomn/2;
178
        y0 = m_H0 - m_H0/2;
179
    }
180
 
181
    row_t new_row;
182
 
183
    new_row.X0 = x0;
184
    new_row.Y0 = y0;
185
 
186
    for(int col = 0; col < m_colomn; col++) {
187
 
188
        int xn = x0 + m_CW * col;
189
        int yn = y0;
190
 
191
        WINDOW *w = newwin(m_CH, m_CW, yn, xn);
192
        if(!w)
193
            break;
194
            box(w, 0 , 0);
195
            wrefresh(w);
196
            new_row.w.push_back(w);
197
    }
198
 
199
    m_table.push_back(new_row);
200
 
201
    return m_table.size();
202
}
203
 
204
//-----------------------------------------------------------------------------
205
 
206
int table::set_cell_text(unsigned nrow, unsigned ncol, const char *fmt, ...)
207
{
208
    if(nrow >= m_table.size())
209
        return -1;
210
 
211
    row_t& row = m_table.at(nrow);
212
 
213
    if(ncol >= row.w.size())
214
        return -2;
215
 
216
    WINDOW *w = row.w.at(ncol);
217
 
218
    if(!w)
219
        return -3;
220
 
221
    va_list argptr;
222
    va_start(argptr, fmt);
223
    char msg[256];
224
    vsprintf(msg, fmt, argptr);
225
    wmove(w, m_CH/2, m_CW/2-strlen(msg)/2);
226
    wprintw(w, "%s", msg);
227
    wrefresh(w);
228
 
229
    return 0;
230
}
231
 
232
//-----------------------------------------------------------------------------
233
 
234
bool table::create_header()
235
{
236
    if(m_header.w)
237
        return true;
238
 
239
    if(m_table.empty())
240
        return false;
241
 
242
    row_t row0 = m_table.at(0);
243
 
244
    m_header.X0 = row0.X0;
245
    m_header.Y0 = row0.Y0-m_CH;
246
 
247
    m_header.H = m_CH;
248
    m_header.W = m_CW*row0.w.size();
249
 
250
    WINDOW *w = newwin(m_header.H, m_header.W, m_header.Y0, m_header.X0);
251
    if(!w) {
252
        return false;
253
    }
254
 
255
    m_header.w = w;
256
    box(w, 0 , 0);
257
 
258
    wrefresh(w);
259
 
260
    return true;
261
}
262
 
263
//-----------------------------------------------------------------------------
264
 
265
bool table::create_status()
266
{
267
    if(m_table.empty())
268
        return false;
269
 
270
    struct header_t status_bar;
271
 
272
    if(m_status.empty()) {
273
 
274
        row_t rowN = m_table.at(m_table.size()-1);
275
 
276
        status_bar.X0 = rowN.X0;
277
        status_bar.Y0 = rowN.Y0+m_CH;
278
 
279
        status_bar.H = m_CH;
280
        status_bar.W = m_CW*rowN.w.size();
281
 
282
    } else {
283
 
284
        struct header_t last_bar = m_status.at(m_status.size()-1);
285
 
286
        status_bar.X0 = last_bar.X0;
287
        status_bar.Y0 = last_bar.Y0+m_CH;
288
 
289
        status_bar.H = m_CH;
290
        status_bar.W = last_bar.W;
291
    }
292
 
293
    WINDOW *w = newwin(status_bar.H, status_bar.W, status_bar.Y0, status_bar.X0);
294
    if(!w) {
295
        return false;
296
    }
297
 
298
    status_bar.w = w;
299
    box(status_bar.w, 0 , 0);
300
    wrefresh(w);
301
 
302
    m_status.push_back(status_bar);
303
 
304
    return true;
305
}
306
 
307
//-----------------------------------------------------------------------------
308
 
309
int table::set_header_text(const char *fmt, ...)
310
{
311
    if(!m_header.w)
312
        return -1;
313
 
314
    va_list argptr;
315
    va_start(argptr, fmt);
316
    char msg[256];
317
    vsprintf(msg, fmt, argptr);
318
    wmove(m_header.w, m_header.H/2, m_header.W/2-strlen(msg)/2);
319
    wprintw(m_header.w, "%s", msg);
320
    wrefresh(m_header.w);
321
 
322
    return 0;
323
}
324
 
325
//-----------------------------------------------------------------------------
326
 
327
int table::set_status_text(unsigned id, const char *fmt, ...)
328
{
329
    if(id >= m_status.size())
330
        return -1;
331
 
332
    struct header_t status_bar = m_status.at(id);
333
    if(!status_bar.w)
334
        return -1;
335
 
336
    va_list argptr;
337
    va_start(argptr, fmt);
338
    char msg[256];
339
    vsprintf(msg, fmt, argptr);
340
    wmove(status_bar.w, status_bar.H/2, status_bar.W/2-strlen(msg)/2);
341
    wprintw(status_bar.w, "%s", msg);
342
    wrefresh(status_bar.w);
343
 
344
    return 0;
345
}
346
 
347
//-----------------------------------------------------------------------------
348
 
349
void table::clear_status()
350
{
351
    for(unsigned i=0; i<m_status.size(); i++) {
352
        struct header_t status_bar = m_status.at(i);
353
        if(status_bar.w) {
354
            delwin(status_bar.w);
355
            status_bar.w = NULL;
356
        }
357
    }
358
    m_status.clear();
359
}
360
 
361
//-----------------------------------------------------------------------------
362
 
363
void table::clear_header()
364
{
365
    if(m_header.w) {
366
        delwin(m_header.w);
367
        m_header.w = NULL;
368
    }
369
}
370
 
371
//-----------------------------------------------------------------------------
372
 
373
//-----------------------------------------------------------------------------

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.