-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKTVScreen.cpp
More file actions
385 lines (350 loc) · 8.19 KB
/
KTVScreen.cpp
File metadata and controls
385 lines (350 loc) · 8.19 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include "KTVScreen.h"
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
void KTVScreen::InitCurses(void)
{
#ifndef KTVTESTMODE
if (!initscr()) exit(0);
cbreak();
noecho();
nonl();
leaveok(stdscr,TRUE);
intrflush(stdscr,FALSE);
keypad(stdscr,TRUE);
getmaxyx(stdscr,_height,_width);
// def_prog_mode();
#else
_height= 25;
_width= 80;
#endif
}
void KTVScreen::TerminateCurses(void)
{
#ifndef KTVTESTMODE
endwin();
// fflush(stdout);
#endif
}
void KTVScreen::RefitTerminal(void)
{
#ifndef KTVTESTMODE
endwin();
refresh();
getmaxyx(stdscr,_height,_width);
#endif
}
int KTVScreen::AddColOnRight(int col,int wid)
{
int awid= FreeWidth()-CostOfNextActiveCol();
if (awid<=0) return 0;
if (awid<wid) wid= awid;
return _ac.AddBack(col,wid);
}
int KTVScreen::AddColOnLeft(int col,int wid)
{
int awid= FreeWidth()-CostOfNextActiveCol();
if (awid<=0) return 0;
if (awid<wid) wid= awid;
return _ac.AddFront(col,wid);
}
int KTVScreen::FreezeCol(int col,int wid)
{
if (_fc.Exists(col)) return 1; // Already frozen. OK
int awid= ActiveRegionWidth() - CostOfNextFrozenCol();
if (wid>awid) wid= awid;
return _fc.AddBack(col,wid);
}
bool KTVScreen::FreezeRow(int row)
{
if (_fr.Exists(row)) return 1; // Already frozen. OK
int ahgt= ActiveRegionHeight() - CostOfNextFrozenRow();
if (ahgt<1) return 0; // No space
return (_fr.AddBack(row,1)>0);
}
int KTVScreen::ResizeRowIndex(int wid)
{
int awd= ActiveRegionWidth() + TrueRowIndexWid();
--awd; // Remove the space separator
if (wid>awd) wid= awd;
_rowindexwid= wid;
return wid;
}
bool KTVScreen::WriteToScreen(int x,int y,int wid,const char *p,bool highlight)
{
if (!p||wid<=0) return 0;
if (x<0||x>=ScreenWidth()) return 0;
if (x+wid>ScreenWidth()) wid= ScreenWidth()-x;
if (y<0||y>=ScreenHeight()) return 0;
#ifndef KTVTESTMODE
if (highlight) attron(A_REVERSE);
#endif
CMVADDNSTR(y,x,p,wid);
#ifndef KTVTESTMODE
if (highlight) attroff(A_REVERSE);
#endif
return 1;
}
bool KTVScreen::WriteToCell(int row,int col,int wid,const char *p)
{
if (row<0||row>=_fr.GetNum()+_ar.GetNum()) return 0;
if (col<0||col>=_fc.GetNum()+_ac.GetNum()) return 0;
if (!p||wid<=0) return 0;
int x= TrueRowIndexWid();
int y= TrueColIndexHgt();
if (row>=_fr.GetNum()) y+= FrozenHeight() + row - _fr.GetNum();
else y+= row;
int colwid= -1;
if (col<_fc.GetNum())
{
if (_grid&&col>0) x+= col;
KTVIdxListList::const_iterator ii;
for (ii=_fc.GetList().begin();ii!=_fc.GetList().end()&&col>0;++ii,--col)
x+= ii->second;
if (ii==_fc.GetList().end()) return 0; // Should never happen!
colwid= ii->second;
}
else
{
x+= FrozenWidth();
col-= _fc.GetNum();
if (_grid&&col>0) x+= col;
KTVIdxListList::const_iterator ii;
for (ii=_ac.GetList().begin();ii!=_ac.GetList().end()&&col>0;++ii,--col)
x+= ii->second;
if (ii==_ac.GetList().end()) return 0;
colwid= ii->second;
}
assert(wid==colwid);
CMVADDNSTR(y,x,p,wid);
return 1;
}
void KTVScreen::GenerateScreenLayout(void)
{
DrawRowNumbers();
// ClearInputRow();
DrawFrozenSeps();
DrawGrid();
DrawColNumbers();
FillUnused();
}
void KTVScreen::FillUnused(void)
{
int unusedy= FreeHeight();
int unusedx= FreeWidth();
assert(unusedx>=0);
assert(unusedy>=0);
if (unusedy>0)
{
char buf[_width];
memset(buf,_filler,_width);
for (int i=1;i<=unusedy;++i)
CMVADDNSTR(_height-1-i,0,buf,_width);
}
if (unusedx>0)
{
char buf[unusedx];
memset(buf,_filler,unusedx);
for (int i=0;i<_height-1-unusedy;++i)
CMVADDNSTR(i,_width-unusedx-1,buf,unusedx);
}
}
void KTVScreen::DrawGrid(void)
{
if (!_grid) return;
int x= TrueRowIndexWid();
bool first= 1;
for (KTVIdxListList::const_iterator ii=_fc.GetList().begin();ii!=_fc.GetList().end();++ii)
{
if (!first)
{
for (int i=0;i<_height-1;++i) CMVADDCH(i,x,_vsep);
++x;
}
x+= ii->second;
if (first) first= 0;
}
// Skip frozen section separator
if (_seps&&!_fc.IsEmpty())
{
++x;
first= 1;
}
for (KTVIdxListList::const_iterator ii=_ac.GetList().begin();ii!=_ac.GetList().end();++ii)
{
if (!first)
{
for (int i=0;i<_height-1;++i) CMVADDCH(i,x,_vsep);
++x;
}
x+= ii->second;
if (first) first= 0;
}
// Put in a special last column if needed
if (_grid&&FreeWidth()==1)
{
for (int i=0;i<_height-1;++i) CMVADDCH(i,_width-1,_vsep);
}
}
void KTVScreen::DrawColNumbers(void)
{
if (!_colindexon) return;
int x= TrueRowIndexWid();
bool first= 1;
for (KTVIdxListList::const_iterator ii=_fc.GetList().begin();ii!=_fc.GetList().end();++ii)
{
if (_grid&&!first) ++x; // Skip grid separator
char buf[ii->second+1];
snprintf(buf,ii->second+1,"%*d",ii->second,ii->first);
buf[ii->second]= 0;
CMVADDNSTR(0,x,buf,ii->second);
x+= ii->second;
if (first) first= 0;
}
// Skip frozen section separator
if (_seps&&!_fc.IsEmpty())
{
++x;
first= 1;
}
for (KTVIdxListList::const_iterator ii=_ac.GetList().begin();ii!=_ac.GetList().end();++ii)
{
if (_grid&&!first) ++x; // Skip grid separator
char buf[ii->second+1];
// fprintf(stderr,"Snprintf2 Before: %d %d\n",ii->second,ii->first);
snprintf(buf,ii->second+1,"%*d",ii->second,ii->first);
buf[ii->second]= 0;
// fprintf(stderr,"Snprintf2 After: [%s]\n",buf);
// fflush(stderr);
CMVADDNSTR(0,x,buf,ii->second);
x+= ii->second;
if (first) first= 0;
}
}
void KTVScreen::DrawRowNumbers(void)
{
// First, row indices
if (_rowindexwid<=0) return;
int w= _rowindexwid+1;
char buf[w+1];
int i=0;
// Initial empty cell if there is a column index in the first row
if (_colindexon)
{
memset(buf,_filler,w);
CMVADDNSTR(i,0,buf,w);
++i;
}
// Frozen rows
for (KTVIdxListList::const_iterator ii=_fr.GetList().begin();ii!=_fr.GetList().end();++ii)
{
snprintf(buf,w+1,"%*d ",w-1,ii->first);
buf[w]= 0;
CMVADDNSTR(i,0,buf,w);
++i;
}
// Skip frozen row sep
if (_seps&&!_fr.IsEmpty()) ++i;
// Regular rows
for (KTVIdxListList::const_iterator ii=_ar.GetList().begin();ii!=_ar.GetList().end();++ii)
{
snprintf(buf,w+1,"%*d ",w-1,ii->first);
buf[w]= 0;
CMVADDNSTR(i,0,buf,w);
++i;
}
}
void KTVScreen::ClearInputRow(void)
{
char buf[_width];
memset(buf,' ',_width);
CMVADDNSTR(_height-1,0,buf,_width);
}
void KTVScreen::DrawFrozenSeps(void)
{
if (!_seps) return;
// The horizontal separator
if (!_fr.IsEmpty())
{
char buf[_width];
memset(buf,_fsep,_width);
int y= TrueColIndexHgt() + FrozenHeight() - 1;
CMVADDNSTR(y,0,buf,_width);
}
// The vertical separator
if (!_fc.IsEmpty())
{
int x= TrueRowIndexWid() + FrozenWidth() - 1;
for (int i=0;i<_height-1;++i) CMVADDCH(i,x,_fsep);
}
}
string KTVScreen::GetCommand(char c)
{
const char blnk[200]= " ";
int nwd= _width-2;
if (nwd>180) nwd= 180;
CMVADDCH(_height-1,0,c);
CMVADDNSTR(_height-1,1,blnk,nwd);
CMVADDCH(_height-1,1,' '); // To move to correct location
#ifndef KTVTESTMODE
echo();
#endif
char buf[128];
buf[0]= 0;
CGETNSTR(buf,127);
#ifndef KTVTESTMODE
noecho();
#endif
return string(buf);
}
// Draw status message
void KTVScreen::StatusMessage(const char *x)
{
if (!x) return;
char buf[_width+1];
memset(buf,' ',_width);
buf[_width]= 0;
strncpy(buf,x,_width);
if (strlen(x)<_width) buf[strlen(x)]=' ';
ClearInputRow();
CMVADDNSTR(_height-1,0,x,_width);
CREFRESH();
}
int KTVScreen::ktestmvaddnstr(int y,int x,const char *str,int n)
{
char buf[n+1];
buf[n]= 0;
if (str) memcpy(buf,str,n);
else buf[0]= 0;
fprintf(stderr,"Curses mvaddnstr to row=%d col=%d str=[%s]\n",y,x,buf);
// printw("Curses mvaddnstr to row=%d col=%d char=[%s]\n",y,x,buf);
fflush(stderr);
return 1;
}
int KTVScreen::ktestmvaddch(int y,int x,char ch)
{
fprintf(stderr,"Curses mvaddch to row=%d col=%d char=[%c]\n",y,x,ch);
// printw("Curses mvaddch to row=%d col=%d char=[%c]\n",y,x,ch);
fflush(stderr);
return 1;
}
int KTVScreen::ktestrefresh(void)
{
fprintf(stderr,"Curses refresh command\n");
// printw("Curses refresh command\n");
fflush(stderr);
return 1;
}
int KTVScreen::ktestgetch(void)
{
char c= fgetc(stdin);
fprintf(stderr,"Curses getch obtained [%c]\n",c);
fflush(stderr);
return c;
}
int KTVScreen::ktestgetnstr(char *buf,int n)
{
if (!buf&&n<=0) return 0;
char *p= fgets(buf,n,stdin);
return p?1:0;
}