-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKTVColRowManager.cpp
More file actions
573 lines (531 loc) · 13 KB
/
KTVColRowManager.cpp
File metadata and controls
573 lines (531 loc) · 13 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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
#include "KTVColRowManager.h"
#include "KTVFileManager.h"
#include <string.h>
void KTVColRowManager::MoveToRow(int row)
{
if (!_sc||!_fm) return;
#ifdef KTVTESTMODE
printf("Move to Row %d\n",row);
#endif
if (row<BaseRow()) row=BaseRow();
_sc->ClearRows();
int minrow= -1;
int maxrow= -1;
// Find row range needed
for (int i=row;;++i)
{
if (_fm->EOFKnown()&&i>_fm->GetEndRow())
{
if (_fm->IsExpandable()) SeekEnd();; // In case it has changed
if (i>_fm->GetEndRow())
{
MoveToLastRow();
return;
}
}
if (_sc->IsFrozenRow(i)) continue;
if (!_sc->AddRowOnBottom(i)) break;
if (minrow<0) minrow= i;
maxrow= i;
}
if (minrow<0||_sc->FreeHeight()>0) { MoveToLastRow(); return; }
if (_scanandaddcols(minrow,maxrow)) MoveToCol(_col1); // Just in case screen has to be readjusted
if (_fm->GetLastRowRead()<maxrow)
{
if (_fm->EOFKnown()) { MoveToLastRow(); return; }
else assert(0); // Should never happen
}
_row1= minrow;
_row2= maxrow;
_rf= 1;
#ifdef KTVTESTMODE
printf("Move concludes with Rows: [%d - %d] and Cols:[%d - %d]\n",_row1,_row2,_col1,_col2);
#endif
}
void KTVColRowManager::MoveToLastRow(void)
{
if (!_sc||!_fm) return;
#ifdef KTVTESTMODE
printf("CR Move to Last Row\n");
#endif
_sc->ClearRows();
if (_fm->IsExpandable()||!_fm->EOFKnown()) SeekEnd();
if (!_fm->EOFKnown()) return; // Error, so return to prevent infinite loop
int minrow= -1;
int maxrow= -1;
for (int i=_fm->GetEndRow();i>=0;--i)
{
if (_sc->IsFrozenRow(i)) continue;
if (!_sc->AddRowOnTop(i)) break;
if (maxrow<0) maxrow= i;
minrow= i;
}
if (maxrow<0||minrow<0) return; // No rows!
if (_scanandaddcols(minrow,maxrow)) MoveToCol(_col1); // Just in case screen has to be readjusted
if (_fm->GetLastRowRead()<maxrow) assert(0);
_row1= minrow;
_row2= maxrow;
_rf= 1;
#ifdef KTVTESTMODE
printf("Move concludes with Rows: [%d - %d] and Cols:[%d - %d]\n",_row1,_row2,_col1,_col2);
#endif
}
void KTVColRowManager::SeekEnd(void)
{
vector<int> newwids;
_fm->SeekEnd(newwids,_cols.size());
for (vector<int>::iterator ii=newwids.begin();ii!=newwids.end();++ii)
{
int nwid= _defwidth;
if (_aligntofirst&&(*ii)>0) nwid= (*ii);
AddCol(nwid,_defrjust);
}
}
// Returns 1 if any new cols have been added
bool KTVColRowManager::_scanandaddcols(int minrow,int maxrow)
{
vector<int> newwids= _fm->Extract(minrow,maxrow,_cols.size());
// Note that when past the first read row, additional cols are at default unless aligntofirst
for (vector<int>::iterator ii=newwids.begin();ii!=newwids.end();++ii)
{
int nwid= _defwidth;
if (_aligntofirst&&(*ii)>0) nwid= (*ii);
AddCol(nwid,_defrjust);
}
return (!newwids.empty());
}
void KTVColRowManager::MoveToCol(int col)
{
if (!_sc) return;
#ifdef KTVTESTMODE
printf("CR Move to Col %d\n",col);
#endif
if (col<0) col= 0;
if (col>=_cols.size()) { MoveToLastCol(); return; }
_sc->ClearCols();
// First, scan forward from col and try to fill screen
for (int i=col;i<_cols.size();++i)
{
if (_cols[i]._frozen||_cols[i]._hidden) continue;
int w= _sc->AddColOnRight(i,_cols[i]._width);
if (w<_cols[i]._width) break;
}
if (_sc->FreeWidth()>(_sc->HasGrid()?1:0)) { MoveToLastCol(); return; }
_col1= _sc->GetColList().empty()?-1:_sc->GetColList().front().first;
_col2= _sc->GetColList().empty()?-1:_sc->GetColList().back().first;
_rf= 1;
#ifdef KTVTESTMODE
printf("Move concludes with Rows: [%d - %d] and Cols:[%d - %d]\n",_row1,_row2,_col1,_col2);
#endif
}
void KTVColRowManager::MoveToLastCol(void)
{
if (!_sc) return;
#ifdef KTVTESTMODE
printf("CR Move to Last Col\n");
#endif
_sc->ClearCols();
for (int i=_cols.size()-1;i>=0;--i)
{
if (_cols[i]._frozen||_cols[i]._hidden) continue;
int w= _sc->AddColOnLeft(i,_cols[i]._width);
if (w<_cols[i]._width) break;
}
_col1= _sc->GetColList().empty()?-1:_sc->GetColList().front().first;
_col2= _sc->GetColList().empty()?-1:_sc->GetColList().back().first;
_rf= 1;
#ifdef KTVTESTMODE
printf("Move concludes with Rows: [%d - %d] and Cols:[%d - %d]\n",_row1,_row2,_col1,_col2);
#endif
}
void KTVColRowManager::Draw(void)
{
if (!_sc||!_fm) return;
int row=0;
KTVIdxListList::const_iterator ii= _sc->GetFrozenRows().begin();
while (ii!=_sc->GetRowList().end())
{
if (ii==_sc->GetFrozenRows().end())
{
ii= _sc->GetRowList().begin();
continue;
}
KTVRow *r= _fm->GetRow(ii->first);
if (r)
{
int col=0;
KTVIdxListList::const_iterator jj= _sc->GetFrozenCols().begin();
while (jj!=_sc->GetColList().end())
{
if (jj==_sc->GetFrozenCols().end())
{
jj= _sc->GetColList().begin();
continue;
}
bool rjustify= _cols[jj->first]._rightjust;
int cwid= jj->second;
if (jj->first<r->size()) WriteCell(row,col,(*r)[jj->first].c_str(),cwid,rjustify,_sc->GetFiller());
else
{
char buf[cwid+1];
memset(buf,_sc->GetFiller(),cwid);
buf[cwid]= 0;
WriteCell(row,col,buf,cwid,rjustify,_sc->GetFiller());
}
++jj;
++col;
}
++row;
}
++ii;
}
_sc->GenerateScreenLayout();
_sc->RefreshScreen();
ClearFlag();
}
void KTVColRowManager::WriteCell(int row,int col,const char *x,int wid,bool rjustify,char filler)
{
char buf[wid+1];
buf[wid]= 0;
if (!_sc) return;
if (!x) memset(buf,filler,wid);
else
{
int slen= strlen(x);
if (rjustify)
{
if (slen>=wid) memcpy(buf,x+slen-wid,wid);
else
{
if (slen>0) memcpy(buf+wid-slen,x,slen);
memset(buf,filler,wid-slen);
}
}
else
{
if (slen>=wid) memcpy(buf,x,wid);
else
{
if (slen>0) memcpy(buf,x,slen);
memset(buf+slen,filler,wid-slen);
}
}
}
_sc->WriteToCell(row,col,wid,buf);
}
int KTVColRowManager::NextActiveCol(int i) const
{
if (i<-1) i=-1;
int j=i+1;
for (;j<_cols.size();++j)
if (!_cols[j]._frozen&&!_cols[j]._hidden) return j;
return j; // OK that past the end!
}
int KTVColRowManager::NextActiveRow(int i) const
{
if (i<-1) i=-1;
int j= i+1;
while (_sc->IsFrozenRow(j)) ++j;
return j;
}
int KTVColRowManager::PrevActiveCol(int i) const
{
int j= i-1;
for (;j>=0;--j)
if (!_cols[j]._frozen&&!_cols[j]._hidden) return j;
return -1;
}
int KTVColRowManager::PrevActiveRow(int i) const
{
if (i>_cols.size()) i= _cols.size();
int j= i-1;
for (;j>=0&&_sc->IsFrozenRow(j);--j);
return j; // Correctly returns -1 if none
}
void KTVColRowManager::FreezeCol(int n)
{
if (n>=0&&n<_cols.size()&&!_cols[n]._frozen)
{
_sc->FreezeCol(n,_cols[n]._width);
_cols[n].Freeze(1);
if (n!=_col1) MoveToCol(_col1);
else MoveToCol(NextActiveCol(n));
}
}
void KTVColRowManager::ToggleFreezeCol(int n)
{
if (n<0||n>=_cols.size()) return;
else if (_cols[n]._frozen) UnFreezeCol(n);
else FreezeCol(n);
}
void KTVColRowManager::HideCol(int n)
{
if (n>=0&&n<_cols.size())
{
_cols[n].Hide(1);
if (n!=_col1) MoveToCol(_col1);
else MoveToCol(NextActiveCol(n));
}
}
void KTVColRowManager::UnHideCol(int n)
{
if (n>=0&&n<_cols.size())
{
_cols[n].Hide(0);
MoveToCol(_col1);
}
}
void KTVColRowManager::ToggleHideCol(int n)
{
if (n<0||n>=_cols.size()) return;
else if (_cols[n]._hidden) UnHideCol(n);
else HideCol(n);
}
void KTVColRowManager::FreezeRow(int n)
{
if (!_fm->FreezeRow(n)) return;
if (n>=0)
{
_sc->FreezeRow(n);
if (n!=_row1) MoveToRow(_row1);
else MoveToRow(NextActiveRow(n));
}
}
void KTVColRowManager::UnFreezeRow(int n)
{
_sc->UnFreezeRow(n);
MoveToRow(_row1);
}
void KTVColRowManager::ToggleFreezeRow(int n)
{
if (_sc->IsFrozenRow(n)) UnFreezeRow(n);
else FreezeRow(n);
}
void KTVColRowManager::UnFreezeCol(int n)
{
if (n>=0&&n<_cols.size()&&_cols[n]._frozen)
{
_sc->UnFreezeCol(n);
_cols[n].Freeze(0);
MoveToCol(_col1);
}
}
int KTVColRowManager::GetMinColFittingFromRight(int n)
{
if (!_sc) return -1;
if (n<0) return -1;
n= PrevActiveCol(n+1);
if (n<0) return -1;
int wid= _sc->ActiveRegionWidth()-_cols[n]._width;
if (wid<=0) return n; // Special case
while (1)
{
int m= PrevActiveCol(n);
if (m<0) return n;
wid-=_cols[m]._width;
if (_sc->HasGrid()) --wid;
if (wid==0) return m;
else if (wid<0) return n;
else n=m;
}
return -1; // Should never reach this!
}
void KTVColRowManager::AddReplaceMark(char c)
{
map<char,pair<int,int> >::iterator ii=_marks.find(c);
if (ii!=_marks.end()) ii->second = pair<int,int>(_row1,_col1);
else _marks.insert(map<char,pair<int,int> >::value_type(c,pair<int,int>(_row1,_col1)));
}
void KTVColRowManager::RestoreMark(char c)
{
map<char,pair<int,int> >::iterator ii=_marks.find(c);
if (ii!=_marks.end())
{
MoveToRow(ii->second.first);
MoveToCol(ii->second.second);
DemandRedraw();
}
}
// Fails if reach max
bool KTVColRowManager::AddCol(int w,bool rjust)
{
if (_maxcols>0&&_cols.size()>=_maxcols) return 0;
_cols.push_back(KTVColSpec(w,rjust));
#ifdef KTVTESTMODE
printf("Added Column %ld with width %d and rjust=%d\n",_cols.size()-1,w,rjust);
#endif
return 1;
}
void KTVColRowManager::Find(const KTVSearch &s,bool prev,bool allowcurr)
{
if (!_fm||!_sc) return;
string x= "";
if (!prev&&s.IsByRow()) x= _findnextbyrow(s,allowcurr);
else if (!prev) x= _findnextbycol(s,allowcurr);
else if (s.IsByRow()) x= _findprevbyrow(s,allowcurr);
else x= _findprevbycol(s,allowcurr);
// Display cell on message line!!
_sc->ClearInputRow();
if (x!="") _sc->StatusMessage(x.c_str());
DemandRedraw();
}
string KTVColRowManager::_findnextbyrow(const KTVSearch &s,bool allowcurr)
{
int c= (!allowcurr)?_col1+1:_col1;
int r= _row1;
bool eflag= 0;
while (1)
{
if (_fm->EOFKnown()&&r>_fm->GetEndRow())
{
if (_fm->IsExpandable()) SeekEnd(); // In case it changed
if (r>_fm->GetEndRow()) return "No Match";
}
KTVRow *x=_fm->GetRow(r);
if (!x)
{
if (eflag) { assert(0); return "Search Error. EOF should have been reached."; }
_scanandaddcols(r,r);
eflag= 1;
continue; // Restart loop since now acquired!
}
eflag= 0;
for (;c<_cols.size()&&c<x->size();++c)
{
if (_cols[c]._hidden) continue;
if (s.TestString((*x)[c]))
{
char buf[200];
buf[199]= 0;
snprintf(buf,200,"Match %d:%d %s",r,c,(*x)[c].c_str());
Move(r,c);
return buf;
}
}
c= 0;
++r;
}
return "Search Error.";
}
string KTVColRowManager::_findprevbyrow(const KTVSearch &s,bool allowcurr)
{
int c= (!allowcurr)?_col1-1:_col1;
int r= _row1;
while (r>=0)
{
KTVRow *x=_fm->GetRow(r);
if (!x)
{
_scanandaddcols(r,r);
x= _fm->GetRow(r);
if (!x) { assert(0); return "Search Error. Failed to read row."; }
}
for (;c>=0;--c)
{
if (c>=x->size()) continue;
if (_cols[c]._hidden) continue;
if (s.TestString((*x)[c]))
{
char buf[200];
buf[199]= 0;
snprintf(buf,200,"Match %d:%d %s",r,c,(*x)[c].c_str());
Move(r,c);
return buf;
}
}
c= _cols.size()-1;
--r;
}
return "No Match.";
}
/*
Unlike for findnext/prev by row, the use of allowcurr makes things ambiguous. Whatever choice we make must allow for consistent repetition. We choose to scan from our current point down and to the right. The region to the left is omitted.
For next search only, We must be careful to check frozen cols first or we will never be on top of the frozen cols so they never get searched.
*/
string KTVColRowManager::_findnextbycol(const KTVSearch &s,bool allowcurr)
{
int c= _col1;
int r= (!allowcurr)?_row1+1:_row1;
SeekEnd(); // Just so we know that we have acquired it!
if (!_fm->EOFKnown()) return "Search Error: Unable to Acquire EOF";
vector<int> foo;
for (KTVIdxListList::const_iterator ii=_sc->GetFrozenCols().begin();ii!=_sc->GetFrozenCols().end();++ii) foo.push_back(ii->first);
for (int i=c;i<_cols.size();++i)
if (!_cols[i]._hidden&&!_cols[i]._frozen) foo.push_back(i);
int msize= _cols.size();
for (vector<int>::iterator ii=foo.begin();ii!=foo.end();++ii)
{
c= *ii;
for (;r<=_fm->GetEndRow();++r)
{
KTVRow *x=_fm->GetRow(r);
if (!x)
{
_scanandaddcols(r,r);
if (_cols.size()>msize) // Accommodate any new cols
{
for (int i=msize;i<_cols.size();++i) foo.push_back(i);
msize= _cols.size();
}
x= _fm->GetRow(r);
if (!x)
{
assert(0);
return "Search Error. Failed to Scan Row.";
}
}
if (c>=x->size()) continue; // Protect against this
if (s.TestString((*x)[c]))
{
char buf[200];
buf[199]= 0;
snprintf(buf,200,"Match %d:%d %s",r,c,(*x)[c].c_str());
Move(r,c);
return buf;
}
}
r= 0;
}
return "No Match.";
}
string KTVColRowManager::_findprevbycol(const KTVSearch &s,bool allowcurr)
{
int c= _col1;
int r= (!allowcurr)?_row1-1:_row1;
SeekEnd();; // Just so we know that we have acquired it!
if (!_fm->EOFKnown()) return "Search Error: Unable to Acquire EOF";
while (c>=0) // This may change as we scan rows, so we keep it dynamic
{
if (_cols[c]._hidden)
{
--c;
continue;
}
for (;r>=0;--r)
{
KTVRow *x=_fm->GetRow(r);
if (!x)
{
_scanandaddcols(r,r);
x= _fm->GetRow(r);
if (!x)
{
assert(0);
return "Search Error. Failed to Scan Row.";
}
}
if (c>=x->size()) continue; // Protect against this
if (s.TestString((*x)[c]))
{
char buf[200];
buf[199]= 0;
snprintf(buf,200,"Match %d:%d %s",r,c,(*x)[c].c_str());
Move(r,c);
return buf;
}
}
r= _fm->GetEndRow();
--c;
}
return "No Match.";
}