File: textwind.c

package info (click to toggle)
irsim 9.7.104-1.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,964 kB
  • sloc: ansic: 24,763; sh: 7,499; makefile: 418; csh: 269; tcl: 88
file content (288 lines) | stat: -rw-r--r-- 6,057 bytes parent folder | download | duplicates (7)
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
/*
 *     ********************************************************************* 
 *     * Copyright (C) 1988, 1990 Stanford University.                     * 
 *     * Permission to use, copy, modify, and distribute this              * 
 *     * software and its documentation for any purpose and without        * 
 *     * fee is hereby granted, provided that the above copyright          * 
 *     * notice appear in all copies.  Stanford University                 * 
 *     * makes no representations about the suitability of this            * 
 *     * software for any purpose.  It is provided "as is" without         * 
 *     * express or implied warranty.  Export of this software outside     * 
 *     * of the United States of America may require an export license.    * 
 *     *********************************************************************
 */

#include <stdio.h>
#include <stdarg.h>
#include <string.h>

#include "ana.h"
#include "ana_glob.h"
#include "graphics.h"

/* Note:  The functions in this file need to be redefined for Tcl/Tk */

#define	BUFFSIZE	256


private char    buffer[ BUFFSIZE ];	/* text widow buffer */
private int     txtPos = 0;		/* postion to print next character */
private	char    txt_cursor[] = " ";	/* cursor: a space in inverse video */
private	int     cursor_on = FALSE;	/* TRUE when cursor on */
private	int     maxPos;


#define	XPOS( Pos )		( ((Pos) * CHARWIDTH) + 2 )
#define	YPOS			( textBox.bot - 1 - descent )
#define	CWIDTH( X )		( (X) * CHARWIDTH )
#define	TXT_HEIGHT		( textBox.bot - textBox.top )


#define	PTEXT( Text, N, Len )			XDrawImageString \
    ( display, window, gcs.black, XPOS( N ), YPOS, Text, Len )

#define	PCURSOR()				XDrawImageString \
    ( display, window, gcs.white, XPOS( txtPos ), YPOS, txt_cursor, 1 )

#define	ERASE_CURSOR()							\
    PTEXT( txt_cursor, txtPos, 1 )


public void RedrawText()
  {
    if (window == 0) return;

    maxPos = textBox.right / CHARWIDTH;
    if( maxPos > BUFFSIZE )
	maxPos = BUFFSIZE;
    FillBox( window, textBox, gcs.white );
    HLine( window, textBox.left, textBox.right, textBox.top, gcs.black );
    if( txtPos )
	PTEXT( buffer, 0, txtPos );
    if( cursor_on )
	PCURSOR();
  }


public void PRINT( s )
  char  *s;
  {
    int  len;

    if (window == 0) return;

    if( *s == '\n' )
      {
	if( txtPos > 0 )
	    FillAREA( window, XPOS( 0 ), textBox.top + 1, CWIDTH( txtPos ),
	     TXT_HEIGHT, gcs.white );
	txtPos = 0;
	s++;
      }
    len = strlen( s );
    if( txtPos + len >= BUFFSIZE )
      {
	len = BUFFSIZE - 1 - txtPos;
      }
    if( len > 0 )
      {
	bcopy( s, buffer + txtPos, len );
	PTEXT( s, txtPos, len );
	txtPos += len;
      }
  }


/* VARARGS */

public void PRINTF(char *format, ...)
  {
    va_list  args;
    char     *s;
    int      len;

    if (window == 0) return;

    va_start(args, format);

    if( *format == '\n' )
      {
	if( txtPos > 0 )
	    FillAREA( window, XPOS( 0 ), textBox.top + 1, CWIDTH( txtPos ),
	     TXT_HEIGHT, gcs.white );
	txtPos = 0;
	format++;
	s = buffer;
      }
    else
	s = buffer + txtPos;

    (void) vsprintf(s, format, args);

    va_end(args);

    len = strlen(s);
    PTEXT(s, txtPos, len);
    txtPos += len;
  }


private	Func    FQuerying;
private	char    *inpStart;
private	void    StrInput();


public void Query( prompt, func )
  char   *prompt;
  Func   func;
  {
    if (window == 0) return;

    PRINT( prompt );
    inpStart = buffer + txtPos;
    cursor_on = TRUE;
    FQuerying = func;
    PCURSOR();
    SendEventTo( StrInput );
  }



	/* define some interesting control characters */

#define	BACKSPACE	'\b'
#define DELETE		'\177'
#define	TAB		'\t'
#define	RETURN		'\r'
#define	NEWLINE		'\n'
#define	CTRL_C		'\003'
#define	CTRL_U		'\025'
#define	CTRL_W		'\027'


private int Cancel()
  {
    int  left;

    if (window == 0) return -1;

    bcopy( "(canceled)", inpStart, 11 );
    left = XPOS( inpStart - buffer + 10 );
    FillAREA( window, left, textBox.top + 1, textBox.right - left + 1,
     TXT_HEIGHT, gcs.white );
    txtPos = inpStart - buffer;
    inpStart = NULL;
    return( txtPos + 10 );
  }


private void StrInput( ev )
  XKeyEvent  *ev;
  {
    char           buff[ 40 ];
    register char  *iPtr, *p;
    register int   newPos, len;
    int            nChars, finish = FALSE;

    if (window == 0) return;

    if( ev == NULL )
      {
	cursor_on = FALSE;
	ERASE_CURSOR();
	txtPos = 0;
	(*FQuerying)( NULL );
	return;
      }
	
    if( ev->type != KeyPress )
      {
	XBell( display, 0 );
	return;
      }

    newPos = txtPos;
    iPtr = buffer + newPos;
    nChars = XLookupString( ev, buff, sizeof( buff ), NULL, NULL );

    for( p = buff, len = nChars; len > 0 and not finish; p++, len-- )
      {
        switch( *p )
	  {
	    case BACKSPACE :
	    case DELETE :
		if( iPtr > inpStart )
		  {
		    newPos--;
		    iPtr--;
		  }
		break;
	    case RETURN :
	    case NEWLINE :
		*iPtr = '\0';
		finish = TRUE;
		break;

	    case TAB :
		*iPtr++ = ' ';
		newPos++;
		break;

	    case CTRL_C :
		newPos = Cancel();
		finish = TRUE;
		break;

	    case CTRL_U :
		if( iPtr > inpStart )
		  {
		    newPos = inpStart - buffer;
		    iPtr = inpStart;
		  }
		 break;

	    case CTRL_W :
		if( iPtr > inpStart )
		  {
		    iPtr--;
		    while( iPtr > inpStart and *iPtr == ' ' )
			iPtr--;
		    while( iPtr > inpStart and *iPtr != ' ' )
			iPtr--;
		    if( iPtr != inpStart )
			iPtr++;
		    newPos = iPtr - buffer;
		  }
		break;
	    default :
		if( *p >= ' ' )		/* displayable character */
		  {
		    if( newPos < maxPos )
		      {
			*iPtr++ = *p;
			newPos++;
		      }
		    else
			XBell( display, 0 );
		  }
	  }
      }

    if( newPos < txtPos )
	FillAREA( window, XPOS( newPos ), textBox.top + 1, 
	 CWIDTH( txtPos - newPos + 1 ), TXT_HEIGHT, gcs. white );
    else if( newPos > txtPos )
	PTEXT( buffer + txtPos, txtPos, newPos - txtPos );

    txtPos = newPos;

    if( finish )
      {
	SendEventTo( NULL );
	cursor_on = FALSE;
	ERASE_CURSOR();
	(*FQuerying)( inpStart );
      }
    else
	PCURSOR();
  }