Debian Codegraph @

libsrc/tw/tvcursor.c

raw
1 /* @(#)tvcursor.c 19.1 (ES0-DMD) 02/25/03 13:58:14 */
2 /*===========================================================================
3 Copyright (C) 1995 European Southern Observatory (ESO)
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of
8 the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public
16 License along with this program; if not, write to the Free
17 Software Foundation, Inc., 675 Massachusetss Ave, Cambridge,
18 MA 02139, USA.
19
20 Corresponding concerning ESO-MIDAS should be addressed as follows:
21 Internet e-mail: [email protected]
22 Postal address: European Southern Observatory
23 Data Management Division
24 Karl-Schwarzschild-Strasse 2
25 D 85748 Garching bei Muenchen
26 GERMANY
27 ===========================================================================*/
28
29 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30
31 .TYPE Module
32 .IDENTIFICATION tvcursor.c
33 .AUTHOR Francois Ochsenbein [ESO-IPG]
34 .LANGUAGE C
35 .KEYWORDS Terminal Independant i/o Package.
36 .ENVIRONMENT TermWindows
37 .COMMENTS This module includes functions to define and perform
38 scrolling, and cursor movements.
39 \begin{TeX}
40 Used Capabilities:
41 $$\begin{tabular}{|lp{30em}|} \hline
42 {\tt cm} & Move cursor to specified position \\
43 {\tt ff} & Form-feed \\
44 {\tt ho} & Move cursor to home (\ie [0,0]) position\\
45 {\tt cs} & Define a scrolling region \\
46 {\tt sf} & Scroll forwards (up) \\
47 {\tt sr} & Scroll reverse (down) \\
48 \hline
49 \end{tabular}$$
50 \end{TeX}
51 .VERSION 1.0 07-Jul-1986: Creation
52 .VERSION 1.1 23-Oct-1986 Flag the move to a newline, to ensure
53 that correct attributes are set even with HP's
54 (attributes end with end-of-line).
55 Take care of cursor move if a scrolling region
56 exists: reset first the scrolling region.
57 (added tv_sr0 to do this)
58
59 .VERSION 1.2 26-Nov-1986 Remove bug in tv_sr0
60
61 .VERSION 2.0 03-Dec-1986 New terminal-independant graphic characters
62 with output buffering version.
63 .VERSION 2.1 11-Jun-1987 Version '2'. Improvements for hard terminals.
64 .VERSION 2.2 23-Jun-1987 Adapted to UNIX and Minimal Implementation.
65 .VERSION 2.3 04-Dec-1987 Save and Get Cursor also save Attribute
66 .VERSION 3.0 20-Mar-1988 Version '3' of TermWindows.
67 .VERSION 3.1 06-Oct-1989 Be sure that tv_sr0 RESETS the scrolling region.
68 .VERSION 3.2 23-May-1990 Hard terminals up movement does nothing...
69 ----------------------------------------------------------------------------*/
70
71 #define DEBUG 0 /* For debugging only */
72
73 #define PM_LEVEL LEVEL_TV
74
75 #define TW_MACROS 0 /* Don't use TermWindows Macros */
76 #define TW_import 0
77 #define TW_STRUCT 0 /* Do not use window Structures */
78 #include <twset.h>
79
80 MID_EXTERN TERM *terms ;
81
82 #define SEND(p) tv_send(p,1)
83
84 #if DEBUG
85 # define TERMTEST if(!terms->version) tv_gopen();\
86 tv_ed(terms)
87 #else
88 # define TERMTEST if(!terms->version) tv_gopen()
89 #endif
90
91 #define FINISH goto FIN
92 #define ENTER_TV(x) static int state_buffer; \
93 ENTER(x); TERMTEST; \
94 state_buffer = tv_buffer(1);
95 #define EXIT_TV(f) FIN: \
96 tv_buffer(state_buffer); \
97 EXIT(f)
98
99 #if DEBUG
100 MID_STATIC char ch=EOS;
101 MID_STATIC int type=0;
102 #endif
103
104 /* Macros just to improve readibility */
105 #define newl new_pos[0]
106 #define newc new_pos[1]
107 #define oldl old_pos[0]
108 #define oldc old_pos[1]
109 #define curl (terms->pos[0])
110 #define curc (terms->pos[1])
111 #define diml (terms->dim[0])
112 #define dimc (terms->dim[1])
113 #define BUFS (terms->bufs)
114
115
116 MONITOR (ZTCURSOR);
117
118 /*==========================================================================
119 * tv_cm(line, col)
120 *==========================================================================*/
121 static int tv_cm(line, col)
122 /*+++
123 .PURPOSE Position the cursor using direct cursor addressing
124 .RETURNS OK / NOK (no direct addressing)
125 .REMARKS NO DELAY IS SENT HERE (for optimization).
126 NO TEST on validy of line and col.
127 *** For internal use ***. Not Traced.
128 ------------------*/
129 int line; /* IN: Line number to move to */
130 int col; /* IN: Column number to move to */
131 {
132 short int new_pos[2];
133
134 newl = line;
135 newc = col;
136
137 status = tv_out(BUFS, tu_format(BUFS, terms->tt_cm, new_pos));
138 /* tv_delay(terms->tt_cm); */
139
140 return(status);
141 }
142
143 /*==========================================================================
144 * tv_range
145 *==========================================================================*/
146 int tv_range(coo, limits)
147 /*++++++++++++++
148 .PURPOSE Verify and updates the cursor position versus limits.
149 Lines are numbered from 0 at the top of the screen; columns are
150 numbered from 0 at the left margin of the screen.
151 .RETURNS OK / NOK (cursor position modified)
152 .REMARKS Not traced.
153 ----------------*/
154 short int coo[2]; /* MOD: Cursor position (line, col) to verify */
155 short int limits[2]; /* IN: Limits of the screen */
156 {
157 register int s;
158
159 s = OK;
160
161 if(coo[0] < 0) { coo[0] = 0; s = NOK;}
162 if(coo[1] < 0) { coo[1] = 0; s = NOK;}
163
164 if(coo[0] >= limits[0]) { coo[0] = limits[0]-1; s = NOK;}
165 if(coo[1] >= limits[1]) { coo[1] = limits[1]-1; s = NOK;}
166
167 return(s);
168 }
169
170 /*==========================================================================
171 * tv_pos
172 *==========================================================================*/
173 int tv_pos()
174 /*++++++++++++++
175 .PURPOSE Returns the cursor position, as a number of characters
176 from the top left.
177 .RETURNS Position, in range [0, lines*cols]
178 .REMARKS Not traced.
179 ----------------*/
180 {
181 return (terms->pos[0]*terms->dim[1] + terms->pos[1]);
182 }
183
184 /*==========================================================================
185 * tv_cur()
186 *==========================================================================*/
187 int tv_cur(saved)
188 /*+++++++++++
189 .PURPOSE Restore the cursor position.
190 .RETURNS OK
191 .REMARKS
192 -----------------------------------------------------------------------*/
193 TVSAVE *saved; /* IN: Old position + attribute */
194 {
195 ENTER_TV("tv_cur");
196
197 status = tv_goto(saved->pos[0], saved->pos[1]);
198 tv_attr(saved->attr);
199 FINISH;
200
201 EXIT_TV(status);
202 }
203
204 /*==========================================================================
205 * tv_cus()
206 *==========================================================================*/
207 int tv_cus(saved)
208 /*++++++++++++++
209 .PURPOSE Save the current cursor position.
210 .RETURNS OK
211 .REMARKS Not traced.
212 ---------------*/
213 TVSAVE *saved; /* OUT: Current position + attribute */
214 {
215 saved->pos[0] = terms->pos[0];
216 saved->pos[1] = terms->pos[1];
217 saved->attr = terms->attr;
218
219 return(OK);
220 }
221
222 /*==========================================================================
223 * tv_agoto
224 *==========================================================================*/
225 int tv_agoto(pos)
226 /*+++++++++++
227 .PURPOSE Position the cursor to a specified byte position.
228 Position are numbered from 0 at the top left corner to N-1 at bottom
229 right corner.
230 .RETURNS Actual position
231 .REMARKS
232 ------------*/
233 int pos; /* IN: Position to move to */
234 {
235 int J, N, n;
236
237 J = terms->dim[1];
238 N = J * terms->dim[0];
239 n = MAX(pos, 0);
240 if (n >= N) n = N-1;
241 tv_goto(n/J, n%J);
242 return(n);
243 }
244
245 /*==========================================================================
246 * tv_goto
247 *==========================================================================*/
248 int tv_goto(line, col)
249 /*+++++++++++
250 .PURPOSE Position the cursor to a specified line and column position on the screen.
251 Lines are numbered from 0 at the top of the screen; columns are
252 numbered from 0 at the left margin of the screen.
253 If full screen is not asked for, no test is performed on the
254 validity of line and column.
255 .RETURNS OK / NOK (not possible, e.g. up on a hard terminal)
256 .REMARKS An attempt to position the cursor off the screen will place it
257 on an edge.
258 NO DELAY IS SENT HERE. (for optimization)
259 ------------*/
260 int line; /* IN: Line number to move to */
261 int col; /* IN: Column number to move to */
262 {
263 short int new_pos[2];
264 short int old_pos[2];
265 register int n;
266
267 ENTER_TV("tv_goto");
268
269 status = OK;
270 tv_where(old_pos); /* Save current position */
271 newl = line;
272 newc = col;
273 tv_range(new_pos, terms->dim); /* Verify cursor within screen */
274
275 #if DEBUG
276 TRACE_ED_I("Old line ",oldl);
277 TRACE_ED_I(" ... col ",oldc);
278 TRACE_ED_I("Moving to line ",line);
279 TRACE_ED_I(" ... col ",col);
280 #endif
281
282 /* Optimize for move on the same line */
283
284 if (newl == oldl)
285 { n = newc - oldc; /* Displacement */
286 if ( n == 0 ) FINISH; /* No move */
287 if ( (n > -3) && (n < 3) )
288 { status = tv_mvc(_RIGHT_, n);
289 FINISH;
290 }
291 }
292 else terms->standout |= ChangedLine;
293
294 if ( (newl < terms->scregion[0]) || (newl > terms->scregion[1]) )
295 tv_sr0(); /* Reset scrolling region */
296
297
298 /* Optimize for a move to the left margin */
299 if (newc == 0 )
300 { if (oldl == newl)
301 { status = tv_out("\r",1);
302 curc = 0;
303 FINISH;
304 }
305 if (newl == (oldl+1))
306 { status = tv_nl();
307 FINISH;
308 }
309 }
310
311 /* Cursor addressing capability MUST exist in Minimal Implementation */
312
313 #if (TW_LEVEL == 0)
314 status = tv_cm(newl, newc);
315 curl = newl;
316 curc = newc;
317 #else
318 if (*terms->tt_cm)
319 { status = tv_cm(newl, newc);
320 curl = newl;
321 curc = newc;
322 FINISH;
323 }
324
325 /* Unknown terminal: use right-left, up-down mouvements ---
326 * but Optimize for move on the same line to the right */
327
328 if (newl == oldl)
329 { n = newc - oldc; /* Displacement */
330 if ( n > 0 )
331 { status = tv_mvc(_RIGHT_, n);
332 FINISH;
333 }
334 }
335
336 status = tv_out("\r",1); /* Go first to left margin */
337 curc = 0; oldc = 0;
338
339 n = newl - oldl; /* ... then move down ... */
340
341 while(n>0)
342 { tv_nl();
343 n--;
344 }
345 /* ... move up if required... */
346 if(n) status = tv_mvc(_UP_, -n);
347 /* ... finally, move right */
348 status = tv_mvc(_RIGHT_, newc);
349 #endif
350
351 EXIT_TV(status);
352 }
353
354 /*======================================================================
355 * tv_home
356 *======================================================================*/
357 int tv_home()
358 /*+++
359 .PURPOSE Move the cursor to home position
360 .RETURNS OK
361 .REMARKS Minimal implementation: use goto(0,0)
362 ---*/
363 {
364 #if (TW_LEVEL > 0)
365 MID_REGISTER char *p;
366 #endif
367
368 ENTER_TV("tv_home");
369
370 if (terms->scregion[0]) /* Reset scrolling region */
371 tv_sr0();
372
373 #if (TW_LEVEL == 0) /* Minimal Implementation */
374 status = tv_cm(0,0);
375 curl = 0; curc = 0;
376 #else /* --Full Implementation */
377 /* Look if "ho" possibility exists */
378 p = SearchCap("ho");
379 if (p)
380 { status = SEND(p);
381 curl = 0;
382 curc = 0;
383 FINISH;
384 }
385
386 /* If up movements not available,
387 simply issue a newline
388 */
389
390 if(*terms->tt_ptr[_UP_] == 0)
391 tv_nl();
392
393 /* Use up movements . Note that zt_mvc takes care of the
394 * existance or not of up movements. */
395
396 tv_out("\r",1); /* Go first to left margin */
397 curc = 0;
398 status = tv_mvc(_UP_, curl);
399 #endif
400
401 EXIT_TV(status);
402 }
403
404 /*======================================================================
405 * tv_mvc
406 *======================================================================*/
407 int tv_mvc(direction,times)
408 /*+++
409 .PURPOSE Move the cursor n times in the specified direction: up, down,
410 right, left or newline.
411 .RETURNS OK / NOK (not possible, e.g. up on a hard terminal)
412 .REMARKS A negative number of times asks for the opposite direction.
413 If full screen is not asked for, no test on the number of
414 of cursor mouvements is performed.
415 --- Minimal Implementation: cursor movements ALWAYS exist.
416 ---*/
417 int direction; /* IN: The direction of the displacement */
418 int times; /* IN: Number of times to move */
419 {
420 short int old_pos[2];
421 MID_RSTATIC short int step[2] = {-1, 1};
422 int k, n, init_attr;
423
424 ENTER_TV("tv_mvc");
425
426 #if DEBUG
427 TRACE_ED_I("Moving to direction: ",direction);
428 TRACE_ED_I(".... times: ",times);
429 #endif
430
431 status = OK;
432
433 if (times == 0) FINISH;
434
435 tv_where(old_pos); /* Save current position */
436 init_attr = terms->attr_init;
437
438 tv_range(terms->pos, terms->dim); /* Test the cursor position */
439
440 k = direction >> 1; /* Lead to Horiz., Vert. or */
441
442 switch(k)
443 { case _VERTICAL_:
444 terms->standout |= ChangedLine;
445 case _HORIZONTAL_:
446 n = times * step[direction & 1];
447 terms->pos[k] += n;
448 tv_range(terms->pos, terms->dim);
449 if (k == _HORIZONTAL_) n = curc - oldc;
450 break;
451 default: /* _NEWLINE_ is assumed */
452 if ( (n = times) < 0) FINISH;
453 while (--n>=0) tv_nl();
454 FINISH;
455 }
456 k <<= 1; /* Final Direction */
457 if (n < 0) n = -n;
458 else k++;
459
460
461 #if (TW_LEVEL > 0) /* ==> Full Implementation */
462 switch(k)
463 { case _UP_ : /* New line for hard terminals: Nothing */
464 if(*terms->tt_ptr[_UP_] == 0)
465 { /* tv_nl(); */
466 status = NOK;
467 FINISH;
468 }
469 break;
470
471 case _DOWN_ : /* Use newlines if cap' doesn't exist */
472 if (*terms->tt_ptr[_DOWN_] == 0)
473 { curl = oldl;
474 curc = oldc;
475 while (--n >= 0) tv_nl();
476 n = oldc;
477 curc = n;
478 k = _RIGHT_;
479 }
480 break;
481
482 case _LEFT_ : /* When capability doesn't exist, use \r
483 * then move to the right */
484 if (*terms->tt_ptr[_LEFT_] == 0)
485 { tv_out("\r", 1);
486 n = curc; /* Blanks to move */
487 k = _RIGHT_; /* Next op. is RIGHT */
488 }
489 else break;
490
491 case _RIGHT_: /* Test if blank is used --- if so, don't
492 * forget to use standard attribute ! */
493 if (*(terms->tt_ptr[_RIGHT_] + 4) == ' ')
494 init_attr = terms->attr, tv_attr((int)terms->attr_init);
495 break;
496 }
497
498 #endif
499
500 /* Send n times the sequence */
501 status = tv_send(terms->tt_ptr[k], n);
502
503 if (terms->attr_init != init_attr) /* If blanks used for _RIGHT_ */
504 tv_attr(init_attr);
505
506 EXIT_TV(status);
507 }
508
509 /*==========================================================================
510 * tv_nl
511 *==========================================================================*/
512 int tv_nl()
513 /*+++++++++++++++
514 .PURPOSE New line, i.e. position the cursor to the beginning of the next line.
515 .RETURNS OK
516 .REMARKS The cursor position is at the beginning of the next line.
517 The insert mode is switched off.
518 -----------*/
519 {
520 register char *p;
521 unsigned char old_attr;
522
523 ENTER_TV("tv_nl");
524
525 status = OK;
526
527 #if (TW_LEVEL > 0)
528 /* First, switch off the insert mode if set */
529 tv_imode(0);
530 #endif
531
532
533 curc = 0; /* The beginning of a line */
534 p = terms->tt_ptr[_NEWLINE_];
535 old_attr = terms->attr;
536 tv_attr(terms->attr_init);
537
538 curl++;
539 if (curl >= diml) curl = diml -1;
540 status = SEND(p);
541 tv_attr(old_attr);
542 FINISH;
543
544 EXIT_TV(status);
545 }
546
547 /*======================================================================
548 * tv_scroll
549 *======================================================================*/
550 int tv_scroll(direction, times)
551 /*+++
552 .PURPOSE Scroll n lines in the specified (_UP_ or _DOWN_) direction.
553 .RETURNS OK / NOK (always NOK in minimal implementation)
554 .REMARKS A negative number of times asks for the opposite direction. _UP_
555 is the default.
556 ---*/
557 int direction; /* IN: The direction of the displacement */
558 int times; /* IN: Number of lines to scroll */
559 {
560 #if (TW_LEVEL == 0) /* ===> Minimal Implementation */
561 return(NOK);
562 #else /* ===> FULL Implementation */
563 int dir, n;
564 char *p;
565 TVSAVE saved;
566
567 ENTER_TV("tv_scroll");
568
569 #if DEBUG
570 TRACE_ED_I("Scrolling lines: ",times);
571 tv_getc(&ch,&type);
572 #endif
573
574 status = OK;
575 if (times == 0) FINISH;
576
577 dir = direction & 1;
578 n = times;
579 if (times < 0) /* Change direction */
580 { dir = 1^direction;
581 n = -times;
582 }
583 tv_cus(&saved); /* Save cursor position */
584
585 /* If no scrolling capability exist: move
586 to top or bottom of screen, and use
587 - down mouvements to scroll up
588 - insert lines to scroll down */
589
590 if (dir == _DOWN_)
591 { tv_home();
592 p = "sr";
593 p = SearchCap(p);
594 if (p) status = tv_send(p,n);
595 else status = tv_il(n);
596 }
597 else
598 { tv_goto(terms->dim[0]-1, 0);
599 p = "sf";
600 p = SearchCap(p);
601 if (p) status = tv_send(p,n);
602 else status = tv_send(terms->tt_ptr[_DOWN_], n);
603 }
604
605 tv_cur(&saved); /* Restore Cursor */
606
607 EXIT_TV(status);
608 #endif
609 }
610
611 /*==========================================================================
612 * tv_sr
613 *==========================================================================*/
614 int tv_sr(l1, l2)
615 /*+++++++++++++
616 .PURPOSE Defines a scrolling region on the terminal between
617 lines l1 and l2.
618 Lines are numbered from 0 at the top of the screen.
619 .RETURNS OK, or NOK (capability does not exist)
620 .REMARKS On return, the cursor is at the left margin within the
621 scrolling region.
622 It does not move if NOK is returned, or if the scrolling
623 region did not change.
624 ----------------*/
625 int l1; /* IN: Upper line of the scrolling region */
626 int l2; /* IN: Lower line of the scrolling region */
627 {
628 register char *p;
629 short int scroll_lim[2];
630
631 ENTER_TV("tv_sr");
632
633 status = OK;
634 p = SearchCap("cs"); /* Null pointer if terminal doesn't allow to
635 define scrolling regions */
636
637 if_not(p)
638 { status = NOK;
639 terms->scregion[0] = 0; terms->scregion[1] = diml-1;
640 FINISH;
641 }
642
643 scroll_lim[0] = MIN(l1,l2);
644 scroll_lim[1] = MAX(l1,l2);
645
646 if(scroll_lim[0] < 0) scroll_lim[0] = 0;
647 if(scroll_lim[1] >= diml) scroll_lim[1] = diml-1;
648
649 if ( (terms->scregion[0] != scroll_lim[0]) ||
650 (terms->scregion[1] != scroll_lim[1]) )
651 {
652 if_not(status = tv_out(BUFS,tu_format(BUFS,p,scroll_lim)))
653 FINISH;
654 if_not(status = tv_delay(p)) FINISH;
655 terms->scregion[0] = scroll_lim[0];
656 terms->scregion[1] = scroll_lim[1];
657
658 if (curl < scroll_lim[0]) curl = scroll_lim[0];
659 if (curl > scroll_lim[1]) curl = scroll_lim[1];
660 curc = 0;
661 if (*terms->tt_cm) /* If cursor addressing exists, use it */
662 tv_cm(curl, 0);
663 }
664
665
666 EXIT_TV(status);
667 }
668
669 /*==========================================================================
670 * tv_sr0
671 *==========================================================================*/
672 int tv_sr0()
673 /*+++++++++++++++
674 .PURPOSE Redefines the complete screen as the scrolling region.
675 .RETURNS OK / NOK (no change in scrolling region)
676 .REMARKS On return, cursor is at home position if OK returned,
677 and remains if NOK.
678 ----------------*/
679 {
680 ENTER_TV("tv_sr0");
681
682 /* Set the current scrolling region to (0,0) to force
683 a change in the tv_sr routine */
684
685 terms->scregion[0] = 0, terms->scregion[1] = 0;
686
687 status = tv_sr(0, diml-1);
688
689 FINISH;
690 EXIT_TV(status);
691 }
692
693 /*==========================================================================
694 * tv_where
695 *==========================================================================*/
696 int tv_where(coo)
697 /*+++++++++++
698 .PURPOSE Returns the current cursor position.
699 .RETURNS OK
700 .REMARKS The position is adjusted within the screen limits.
701 Not traced.
702 ------------*/
703 short int coo[2]; /* OUT: current position: line (0=top), col (0=left) */
704 {
705 coo[0] = curl;
706 coo[1] = curc;
707 /* if (terms->flags & TERM_am);
708 else tv_range(coo, terms->dim);
709 */
710 tv_range(coo, terms->dim);
711 return(OK);
712 }
713