File: stringinput.c

package info (click to toggle)
gramofile 1.6-11
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 1,436 kB
  • ctags: 1,125
  • sloc: ansic: 11,252; makefile: 60
file content (118 lines) | stat: -rw-r--r-- 2,582 bytes parent folder | download | duplicates (2)
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
/* String input

 * Copyright (C) 1998 J.A. Bezemer
 *
 * Licensed under the terms of the GNU General Public License.
 * ABSOLUTELY NO WARRANTY.
 * See the file `COPYING' in this directory.
 */

#include "stringinput.h"
#include <string.h>
#ifndef OLD_CURSES
#include <ncurses.h>
#else
#include <curses.h>
#endif


void
stringinput_display (stringinput_t * data)
{
  int i;

  if (data->cursorpos < 0)
    data->cursorpos = 0;
  if (data->cursorpos > strlen (data->string))
    data->cursorpos = strlen (data->string);

  if (data->cursorpos < data->firstcharonscreen + 2)
    data->firstcharonscreen = data->cursorpos - 2;
  if (data->cursorpos >= data->firstcharonscreen + data->w)
    data->firstcharonscreen = data->cursorpos - data->w + 1;

  if (data->firstcharonscreen < 0)
    data->firstcharonscreen = 0;

  move (data->y, data->x);
  i = data->firstcharonscreen;
  while (i < strlen (data->string) && i < data->firstcharonscreen + data->w)
    {
      addch (data->string[i]);
      i++;
    }

  while (i < data->firstcharonscreen + data->w)
    {
      addch (' ');
      i++;
    }

  move (data->y, data->x + data->cursorpos - data->firstcharonscreen);
}

void
stringinput_stdkeys (int key, stringinput_t * data)
{
  char helpstring[500];

  switch (key)
    {
    case KEY_LEFT:
      data->cursorpos--;
      break;

    case KEY_RIGHT:
      data->cursorpos++;
      break;

    case KEY_HOME:
      data->cursorpos = 0;
      break;

    case KEY_END:
      data->cursorpos = strlen (data->string);
      break;

    case KEY_BACKSPACE:
#ifdef TREAT_DEL_AS_BACKSPACE
    case 127:
#endif
      if (data->cursorpos > 0)
	{
	  strcpy (helpstring, data->string);
	  strcpy (helpstring + data->cursorpos - 1,
		  data->string + data->cursorpos);
	  strcpy (data->string, helpstring);
	  data->cursorpos--;
	}
      break;

#ifndef TREAT_DEL_AS_BACKSPACE
    case 127:			/* DEL */
      if (data->cursorpos < strlen (data->string))
	{
	  strcpy (helpstring, data->string);
	  strcpy (helpstring + data->cursorpos,
		  data->string + data->cursorpos + 1);
	  strcpy (data->string, helpstring);
	}
      break;
#endif
    }

  if (key >= 32 && key <= 126)	/* insert */
    {
      strcpy (helpstring, data->string);
      strcpy (helpstring + data->cursorpos + 1,
	      data->string + data->cursorpos);
      helpstring[data->cursorpos] = (char) key;
      strcpy (data->string, helpstring);
      data->cursorpos++;
    }

  if (data->cursorpos < 0)
    data->cursorpos = 0;
  if (data->cursorpos > strlen (data->string))
    data->cursorpos = strlen (data->string);
}