File: getstr.cxx

package info (click to toggle)
curves 0.8.19
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, sarge, squeeze, wheezy
  • size: 724 kB
  • ctags: 1,016
  • sloc: cpp: 6,284; ansic: 535; makefile: 292; sh: 192; fortran: 149
file content (192 lines) | stat: -rw-r--r-- 4,717 bytes parent folder | download
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
/* getstr.cxx
     $Id: getstr.cxx,v 1.12 2001/11/27 23:57:36 elf Exp $

   written by Marc Singer
   3 October 1996

   This file is part of the project CurVeS.  See the file README for
   more information.

   Copyright (C) 1996 Marc Singer

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   with your Debian GNU/Linux system, in
   /usr/share/common-licenses/GPL, or with the Debian GNU/Linux hello
   source package as the file COPYING. If not, write to the Free
   Software Foundation, Inc., 59 Temple Place -Suite 330, MA
   02111-1307, USA.

   -----------
   DESCRIPTION
   -----------

   User input function(s) for well protected interactions with the user.  

*/


#include "std.h"
#include "window.h"
#include "termcap.h"


/* LWindow::getstr

   fetches a string from the user in a nice, emacs sort of way.  The
   return valuse is zero if the user input is accepted.  It is !0 if
   the user pressed escape.

*/

int LWindow::getstr (int x, int y, char* szPrompt, 
		     char* szResponse, int cchResponseMax, 
		     const char* szDefault)
{
  if (szDefault) {
    if (szResponse != szDefault)
      strcpy (szResponse, szDefault);
  }
  else
    *szResponse = 0;

  off (A_REVERSE);
  printfxy (x, y, szPrompt);
  x += strlen (szPrompt);

  int ibSelect = 0, cbSelect = strlen (szResponse);
  on (A_REVERSE);
  printfxy (x, y, szResponse);
  off (A_REVERSE);

  int point = 0;
  int cch = strlen (szResponse);
  int ch;
  int scroll = 0;
  int scrollDelta = (dx () - x)/2;
  int scrollZone = 10;
  while ((ch = getch (point + x - scroll, y)) != '\n') {
    switch (ch) {
    case -1:			// Getstr happening off screen
      assert_ (ch != -1);
      break;
    case 27:			// ESC
    case 7:			// ^G -- abort
      return -1;
    case 1:			// ^A -- beginning of line
      point = 0;
      cbSelect = 0;
      break;
    case 5:			// ^E -- end of line
      point = cch;
      cbSelect = 0; 
      break;
    case 4:			// ^D -- delete
    case KEYCODE (termcapKeyDelete):
      if (point < cch) {
	memmove (szResponse + point, szResponse + point + 1, 
		 cch - point);
	--cch;
      }
      break;

    case 8:			// ^H -- backspace
    case KEYCODE (termcapKeyBackspace):
      if (cbSelect) {
	*szResponse = 0;
	cbSelect = 0;
	point = 0;
	cch = 0;
      }
      else if (point) {
	memmove (szResponse + point - 1, szResponse + point,
		 cch - point + 1);
	--point;
	--cch;
      }
      break;

    case 11:			// ^K -- delete to end of line
      if (cbSelect) {
	memmove (szResponse + ibSelect,
		 szResponse + ibSelect + cbSelect,
		 cch - ibSelect - cbSelect + 1);
	cch -= cbSelect;
	point = ibSelect;
	cbSelect = 0;
      }
      else {
	szResponse[point] = 0;
	cch = point;
      }
      break;

    case 2:			// ^B -- backward character
    case KEYCODE (termcapKeyLeft):
      if (point)
	--point;
      cbSelect = 0;
      break;

    case 6:			// ^F -- forward character
    case KEYCODE (termcapKeyRight):
      if (point < cch)
	++point;
      cbSelect = 0;
      break;

    default:
      if (isprint (ch)) {
	if (cbSelect) {		// Replace selection
	  memmove (szResponse + ibSelect + 1, 
		   szResponse + ibSelect + cbSelect,
		   cch - ibSelect - cbSelect + 1);
	  cch -= cbSelect;
	  point = ibSelect;
	  cbSelect = 0;
	}
	else {
	  if (cch >= cchResponseMax - 1) // Leave room for nul, right?
	    continue;		// FIXME: how about a beep here
	  if (point != cch)
	    memmove (szResponse + point + 1, szResponse + point,
		     cch - point + 1);
	  else {
	    assert_ (point < cchResponseMax - 1);
	    szResponse[point + 1] = 0;
	  }
	}
	szResponse[point++] = ch;
	++cch;
      }
      break;			// FIXME: how about a beep on bad input
    }
    // FIXME: We need to break-up the display so we show the
    //        highlighted portion AND the unhighlighted portion.
    if (cbSelect)
      on (A_REVERSE);
    else
      off (A_REVERSE);
				// Check for scroll changes
    while (point - scroll >= dx () - x)
      scroll += scrollDelta;
    while (scroll && point < scroll + scrollZone)
      scroll -= scrollDelta;

    printfxy (x, y, "%-*.*s", dx () - x, dx () - x, szResponse + scroll);
    if (cbSelect)
      off (A_REVERSE);
  }
  return 0;
}