summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/ncurses/examples/tpad.pp
blob: 25d1ec39a4e223903459aa35180e42ca4c64f1ea (plain)
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
{
   Author: Vitaliy Trifonov
}
program pad_demo;

{$MODE OBJFPC}

{$IFDEF DEBUG}
{$ASSERTIONS ON}
{$OVERFLOWCHECKS ON}
{$RANGECHECKS ON}
{$CHECKPOINTER ON}
{$ENDIF}

uses
  ncurses, panel, sysutils;

type
  TNcCoord = array[0..1] of Smallint;

  TNcStr = packed record
    str: AnsiString;
    attr: attr_t;
    coord: TNcCoord;
  end;

const y = 0; x = 1;

function CTRL( ch: chtype ): chtype; inline;
begin
  CTRL := ch AND $001F
end;

function randomchar: chtype;
var
  ch: Char = #0;
begin
  while not (ch in ['0'..'9','A'..'Z','a'..'z']) do
    ch := Char(Random(123));
  randomchar := chtype(ch);
end;

function randompair: longint;
var
  pair: longint = 0;
begin
  while not (pair in [1..5]) do
    pair := Random(6);
  randompair := pair;
end;


procedure draw;
var
  y, x:  Smallint;
begin
  for y := 0 to LINES - 1 do
    for x := 0 to COLS - 1 do
      mvaddch(y, x, randomchar OR COLOR_PAIR(randompair));
end;

procedure draw_pad(win: PWINDOW);

var
  y, x, my, mx:  Smallint;
begin
  getmaxyx(win,my,mx);
  wborder(win, ACS_CKBOARD,ACS_CKBOARD,ACS_CKBOARD,ACS_CKBOARD,
          ACS_CKBOARD,ACS_CKBOARD,ACS_CKBOARD,ACS_CKBOARD);
  for y := 1 to my - 2 do
    if (y mod 5) = 1 then
      for x := 1 to mx - 2 do
        if (x mod 10) = 1 then
          mvwaddch(win, y, x, randomchar OR COLOR_PAIR(randompair))
        else
          mvwaddch(win, y, x, ACS_HLINE)
    else
      for x := 1 to mx - 2 do
        if (x mod 10) = 1 then
          mvwaddch(win, y, x, ACS_VLINE)
        else
          mvwaddch(win, y, x, chtype(' '))
end;


function st_middle(scrlen, itemlen: Smallint): Smallint; inline;
begin
  st_middle := (scrlen - itemlen) div 2;
end;

procedure print_in_middle(win: PWINDOW; var nstr: TNcStr; width: Longint);
var
  my, mx: Smallint;
begin
  getmaxyx(win, my, mx);
  mx -= nstr.coord[1];

  if (width > length(nstr.str)) OR  (width < 1) then
    width := length(nstr.str);

  if width > mx then
    width := mx;

  nstr.coord[x] += st_middle(mx,width);

  wattron(win,nstr.attr);
  mvwaddnstr(win,nstr.coord[y],nstr.coord[x],PChar(nstr.str),width);
  wattroff(win,nstr.attr);
end;

type
  TBarData = packed record
    beg, len, slen: Smallint;
  end;

  TPad = class
  private
    wyx, pyx, ppos, grid: TNcCoord;
    hbar, vbar: TBarData;
    padwin, projwin: PWINDOW;
    panel: PPANEL;
    header: TNcStr;
    changed: Boolean;
    procedure init_bars;
    procedure draw_hbar;
    procedure draw_vbar;
  public
    function scroll_right: Boolean;
    function scroll_left: Boolean;
    function scroll_down: Boolean;
    function scroll_up: Boolean;
    function  doevent: chtype;
    procedure dorefresh;
    function  move(const ncoord: array of Smallint): Boolean; inline;
    function  hide: Boolean; inline;
    function  show: Boolean; inline;
    procedure resize;
    function  resize(const nsize: array of Smallint): Boolean;
    constructor create(const parm: array of TNcCoord; const hdr: TNcStr);
    destructor destroy; override;
    property win: PWINDOW read padwin;
    property ysize: Smallint read wyx[y];
    property xsize: Smallint read wyx[x];
  end;


procedure TPad.init_bars;

function get_scrl_len(blen, wsz, psz: Smallint): Smallint; inline;
begin
  get_scrl_len := (blen * wsz) div psz;
end;

begin
  hbar.beg  := 4;
  hbar.len  := wyx[x] - hbar.beg * 2;
  hbar.slen := get_scrl_len(hbar.len, wyx[x], pyx[x]);

  vbar.beg  := 2;
  vbar.len  := wyx[y] - vbar.beg * 2;
  vbar.slen := get_scrl_len(vbar.len, wyx[y], pyx[y]);
end;

function get_scrl_beg(ind, slen, blen, wsz, psz, bbeg: Smallint): Smallint;
begin
  if psz <> wsz then
    get_scrl_beg := (ind * (blen - slen)) div (psz - wsz) + bbeg
  else
    get_scrl_beg := bbeg;
end;

procedure TPad.draw_hbar;
var
  i, sbeg: Smallint;
begin
  with hbar do
  begin
    sbeg := get_scrl_beg(ppos[x],hbar.slen,hbar.len,wyx[x], pyx[x],hbar.beg);
    wattron(projwin,header.attr);
    for i :=  beg to beg + len - 1 do
    if (i < sbeg) OR (i > sbeg + slen) then
      mvwaddch(projwin,wyx[y]-1,i  ,ACS_CKBOARD)
    else
      mvwaddch(projwin,wyx[y]-1,i,ACS_BLOCK);
    wattroff(projwin,header.attr);
  end
end;

procedure TPad.draw_vbar;
var
  i, sbeg: Smallint;
begin
  with vbar do
  begin
    sbeg := get_scrl_beg(ppos[y],vbar.slen,vbar.len,wyx[y], pyx[y],vbar.beg);
    wattron(projwin,header.attr);
    for i :=  beg to beg + len - 1 do
    if (i < sbeg) OR (i > sbeg + slen) then
      mvwaddch(projwin,i,wyx[x]-1,ACS_CKBOARD)
    else
      mvwaddch(projwin,i,wyx[x]-1,ACS_BLOCK);
    wattroff(projwin,header.attr);
  end
end;

function TPad.scroll_right: Boolean;
begin
  if ppos[x] > 0 then
  begin
    if (ppos[x] < grid[x]) then
      ppos[x] := 0
    else
      ppos[x] -= grid[x];
    draw_hbar;
    changed := true;
    scroll_right := true
  end
  else
    scroll_right := false
end;

function TPad.scroll_left: Boolean;
var
  dwidth: Longint;
begin
  dwidth := pyx[x] - wyx[x] + 2;
  if ppos[x] < dwidth then
  begin
    if ppos[x] > (dwidth - grid[x]) then
      ppos[x] := dwidth
    else
      ppos[x] += grid[x];
    draw_hbar;
    changed := true;
    scroll_left := true
  end
  else
    scroll_left := false
end;

function TPad.scroll_down: Boolean;
begin
  if ppos[y] > 0 then
  begin
    if ppos[y] < grid[y] then
      ppos[y] := 0
    else
      ppos[y] -= grid[y];
    draw_vbar;
    changed := true;
    scroll_down := true
  end
  else
    scroll_down := false
end;

function TPad.scroll_up: Boolean;
var
  dheight: Longint;
begin
  dheight := pyx[y] - wyx[y] + 2;
  if ppos[y] < dheight then
  begin
    if ppos[y] > (dheight - grid[x]) then
      ppos[y] := dheight
    else
      ppos[y] += grid[x];
    draw_vbar;
    changed := true;
    scroll_up := true
  end
  else
    scroll_up := false
end;

function  TPad.doevent: chtype;
var
  ch: chtype;
  rval: Boolean = true;
begin
  ch := wgetch(projwin);
  case ch of
    KEY_DOWN:  rval := scroll_up;
    KEY_UP:    rval := scroll_down;
    KEY_LEFT:  rval := scroll_right;
    KEY_RIGHT: rval := scroll_left;
  end;
  if not rval then
  begin
    ncurses.beep();
    flash();
  end;
  doevent := ch
end;

procedure TPad.dorefresh;
var
  rval: Longint = OK;
begin
  if changed then
  begin
    rval := copywin(padwin,projwin,ppos[y],ppos[x],1,1,wyx[y]-2,wyx[x]-2, 0);
    assert(rval=OK,'copywin error');
    if rval = OK then
      changed := false;
  end
end;

function TPad.move(const ncoord: array of Smallint): Boolean;
begin
  move :=  move_panel(panel, ncoord[y], ncoord[x]) = OK
end;

function TPad.hide: Boolean;
begin
  hide := hide_panel(panel) = OK
end;

function TPad.show: Boolean;
begin
  show := show_panel(panel) = OK
end;

procedure TPad.resize;
var
  nsize: TNcCoord;
  doresize: Boolean = false;
begin
  getbegyx(projwin,nsize[y],nsize[x]);

  nsize[y] += wyx[y];
  nsize[x] += wyx[x];

  if nsize[y] > LINES then
  begin
    nsize[y] := LINES; doresize := true
  end
  else
    nsize[y] := wyx[y];

  if nsize[x] > COLS then
  begin
    nsize[x] := COLS; doresize := true
  end
  else
    nsize[x] := wyx[x];

  if doresize then
    resize(nsize)
end;

function TPad.resize(const nsize: array of Smallint): Boolean;
var
  by, bx: Smallint;
  domove: Boolean = false;
  tcoord: TNcCoord;
begin

  if (nsize[y] <= LINES)AND(nsize[x] <= COLS) then
  begin
    if nsize[y] > pyx[y] + 2 then
      tcoord[y] := pyx[y] + 2
    else
      tcoord[y] := nsize[y];

    if nsize[x] > pyx[x] + 2 then
      tcoord[x] := pyx[x] + 2
    else
      tcoord[x] := nsize[x];


    getbegyx(projwin, by, bx);

    if tcoord[y] + by >= LINES then
    begin
      by := LINES - tcoord[y]; domove := true
    end;

    if tcoord[x] + bx >= COLS then
    begin
      bx := COLS - tcoord[x]; domove := true
    end;

    if tcoord[x] > (pyx[x] - ppos[x]) then
      scroll_right;
    if tcoord[y] > (pyx[y] - ppos[y]) then
      scroll_down;

    hide_panel(panel);
    wresize(projwin, tcoord[y], tcoord[x]);

    if domove then
      move_panel(panel, by, bx);
    show_panel(panel);

    box(projwin, ACS_VLINE, ACS_HLINE);

    getmaxyx(projwin,wyx[y],wyx[x]);
    header.coord[y] := 0; header.coord[x] := 0;

    print_in_middle(projwin, header, 0);
    init_bars;
    draw_hbar;
    draw_vbar;

    changed := true;
    resize := true
  end
  else
    resize := false
end;

constructor TPad.create(const parm: array of TNcCoord; const hdr: TNcStr);
{$IFDEF DEBUG}
var
  tysz, txsz: Smallint;
{$ENDIF}
begin
  if parm[0,y] >= parm[1,y] + 2 then
    wyx[y] := parm[1,y] + 2
  else
    wyx[y] := parm[0,y];

  if parm[0,x] >= parm[1,x] + 2  then
    wyx[x] := parm[1,x] + 2
  else
    wyx[x] := parm[0,x];

  projwin := newwin(wyx[y], wyx[x], (LINES - wyx[y]) div 2, (COLS - wyx[x]) div 2);
  intrflush(projwin, FALSE);
  keypad(projwin, TRUE);
  box(projwin, ACS_VLINE, ACS_HLINE);

  panel := new_panel(projwin);
  padwin := newpad(parm[1,y], parm[1,x]);

  header := hdr;
  pyx := parm[1];
  grid := parm[2];

{$IFDEF DEBUG}
  getmaxyx(projwin,tysz, txsz);
  assert((wyx[y]=tysz)AND(wyx[x]=txsz), 'Invalid window');

  getmaxyx(padwin,tysz, txsz);
  assert((pyx[y]=tysz)AND(pyx[x]=txsz), 'Invalid pad');
{$ENDIF}
  FmtStr(header.str, '%s, pad: h=%d w=%d, win: h=%d w=%d', [hdr.str,pyx[y],pyx[x],wyx[y],wyx[x]]);


  print_in_middle(projwin, header, 0);

  init_bars;
  draw_hbar;
  draw_vbar;

  changed := true;
end;

destructor TPad.destroy;
begin
  del_panel(panel);
  delwin(padwin);
  delwin(projwin);
end;

procedure init_stdscr;
begin
  draw;
  attron(COLOR_PAIR(7));
  mvaddstr(LINES - 3, 0,'press "+" "-" to resize              ');
  mvaddstr(LINES - 2, 0,'press UP, DOWN, LEFT, RIGHT to scroll');
  mvaddstr(LINES - 1, 0,'press F10 or q to exit               ');
  attroff(COLOR_PAIR(7));
end;



var
  ch: chtype;
  ncpad: TPad;
  my_bg: Smallint = COLOR_BLACK;
  wnd, pad, grid: TNcCoord;
  code: Word;
  header: TNcStr = (str:'Pad demo';attr:A_NORMAL;coord:(0,0));
begin
  try
    initscr();
    noecho();
    clear();
    cbreak();
    curs_set(0);
    keypad(stdscr, TRUE);
    meta(stdscr, TRUE);
    mousemask(1, nil);

   if has_colors() then
   begin
     start_color();
     if (use_default_colors() = OK) then
       my_bg := -1
     else
       my_bg := COLOR_BLACK;

     init_pair(1, COLOR_YELLOW, my_bg);
     init_pair(2, COLOR_MAGENTA, my_bg);
     init_pair(3, COLOR_WHITE, my_bg);
     init_pair(4, COLOR_CYAN, my_bg);
     init_pair(5, COLOR_GREEN, my_bg);
     init_pair(6, COLOR_WHITE, COLOR_BLUE);
     init_pair(7, COLOR_BLACK, COLOR_YELLOW);
   end;

    init_stdscr;
    //refresh();

    wnd[y]  := LINES - 6;
    wnd[x]  := COLS - 12;
    pad[y]  := wnd[y] + 6;
    pad[x]  := wnd[x] + 6;
    grid[y] := 3;
    grid[x] := 3;


    if paramcount > 1 then
    begin
      val(ParamStr(1),pad[y],code);
      val(ParamStr(2),pad[x],code);
    end;

    if paramcount > 3 then
    begin
      val(ParamStr(3),wnd[y],code);
      val(ParamStr(4),wnd[x],code);
    end;

    header.attr := COLOR_PAIR(6);
    ncpad := TPad.create([wnd,pad,grid],header);
    draw_pad(ncpad.win);
    ncpad.dorefresh;
    update_panels();
    doupdate();

    repeat
      ch := ncpad.doevent;
      case ch of
        chtype('+'): ncpad.resize([ncpad.ysize + 1,ncpad.xsize + 1]);
        chtype('='): ncpad.resize([ncpad.ysize + 1,ncpad.xsize + 1]);
        chtype('-'): ncpad.resize([ncpad.ysize - 1,ncpad.xsize - 1]);
        chtype(' '): ncpad.resize([wnd[y],wnd[x]]);
        KEY_RESIZE:
        begin
          flash();
          init_stdscr;
          ncpad.resize;
        end;
      end;
      ncpad.dorefresh;
      update_panels();
      doupdate();
    until (ch = chtype('q')) OR (ch = KEY_F(10));
  finally
    ncpad.destroy;
    curs_set(1);
    endwin();
  end;
end.