File: textbox.c

package info (click to toggle)
newt 0.52.17-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,676 kB
  • ctags: 2,049
  • sloc: ansic: 9,249; python: 711; makefile: 259; sh: 9
file content (510 lines) | stat: -rw-r--r-- 11,784 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
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

#include <ctype.h>
#include <slang.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <wctype.h>

#include "newt.h"
#include "newt_pr.h"

struct textbox {
    char ** lines;
    int numLines;
    int linesAlloced;
    char *blankline;
    int doWrap;
    newtComponent sb;
    int topLine;
    int textWidth;
    int isActive;
    int cs;
    int csActive;
};

static char * expandTabs(const char * text);
static void textboxDraw(newtComponent co);
static void addLine(newtComponent co, const char * s, int len);
static void doReflow(const char * text, char ** resultPtr, int width, 
		     int * badness, int * heightPtr);
static struct eventResult textboxEvent(newtComponent c,
				      struct event ev);
static void textboxDestroy(newtComponent co);
static void textboxPlace(newtComponent co, int newLeft, int newTop);
static void textboxMapped(newtComponent co, int isMapped);

static struct componentOps textboxOps = {
    textboxDraw,
    textboxEvent,
    textboxDestroy,
    textboxPlace,
    textboxMapped,
} ;

static void textboxMapped(newtComponent co, int isMapped) {
    struct textbox * tb = co->data;

    co->isMapped = isMapped;
    if (tb->sb) {
	tb->sb->ops->mapped(tb->sb, isMapped);
    }
}

static void textboxPlace(newtComponent co, int newLeft, int newTop) {
    struct textbox * tb = co->data;

    co->top = newTop;
    co->left = newLeft;

    if (tb->sb) {
	tb->sb->ops->place(tb->sb, co->left + co->width - 1, co->top);
    }
}

void newtTextboxSetHeight(newtComponent co, int height) {
    co->height = height;
}

int newtTextboxGetNumLines(newtComponent co) {
    struct textbox * tb = co->data;

    return (tb->numLines);
}

newtComponent newtTextboxReflowed(int left, int top, char * text, int width,
				  int flexDown, int flexUp, int flags) {
    newtComponent co;
    char * reflowedText;
    int actWidth, actHeight;

    reflowedText = newtReflowText(text, width, flexDown, flexUp,
				  &actWidth, &actHeight);
    
    co = newtTextbox(left, top, actWidth, actHeight, NEWT_FLAG_WRAP);
    newtTextboxSetText(co, reflowedText);
    free(reflowedText);

    return co;
}

newtComponent newtTextbox(int left, int top, int width, int height, int flags) {
    newtComponent co;
    struct textbox * tb;

    co = malloc(sizeof(*co));
    tb = malloc(sizeof(*tb));
    co->data = tb;

    if (width < 1)
	width = 1;

    co->ops = &textboxOps;

    co->isMapped = 0;
    co->height = height;
    co->top = top;
    co->left = left;
    co->takesFocus = 0;
    co->width = width;
    co->destroyCallback = NULL;

    tb->doWrap = flags & NEWT_FLAG_WRAP;
    tb->numLines = 0;
    tb->linesAlloced = 0;
    tb->lines = NULL;
    tb->topLine = 0;
    tb->textWidth = width;
    tb->isActive = 0;
    tb->cs = COLORSET_TEXTBOX;
    tb->csActive = COLORSET_ACTTEXTBOX;
    tb->blankline = malloc(width+1);
    memset(tb->blankline,' ',width);
    tb->blankline[width] = '\0';

    if (flags & NEWT_FLAG_SCROLL) {
	co->width += 2;
	tb->sb = newtVerticalScrollbar(co->left + co->width - 1, co->top, 
			   co->height, tb->cs, tb->cs);
	co->takesFocus = 1;
    } else {
	tb->sb = NULL;
    }

    return co;
}

void newtTextboxSetColors(newtComponent co, int normal, int active) {
    struct textbox * tb = co->data;

    tb->cs = normal;
    tb->csActive = active;
    textboxDraw(co);
}

static char * expandTabs(const char * text) {
    int bufAlloced = strlen(text) + 40;
    char * buf, * dest;
    const char * src;
    int bufUsed = 0;
    int linePos = 0;
    int i;

    buf = malloc(bufAlloced + 1);
    for (src = text, dest = buf; *src; src++) {
	if ((bufUsed + 10) > bufAlloced) {
	    bufAlloced += strlen(text) / 2;
	    buf = realloc(buf, bufAlloced + 1);
	    dest = buf + bufUsed;
	}
	if (*src == '\t') {
	    i = 8 - (linePos & 8);
	    memset(dest, ' ', i);
	    dest += i, bufUsed += i, linePos += i;
	} else {
	    if (*src == '\n')
		linePos = 0;
	    else
		linePos++;

	    *dest++ = *src;
	    bufUsed++;
	}
    }

    *dest = '\0';
    return buf;
}

static void doReflow(const char * text, char ** resultPtr, int width, 
		     int * badness, int * heightPtr) {
    char * result = NULL;
    const char * chptr, * end;
    int i;
    int howbad = 0;
    int height = 0;
    wchar_t tmp;
    mbstate_t ps;

    if (resultPtr) {
	if (width > 1) {
	    /* use width - 1 for double width characters
	       that won't fit at end of line */
	    result = malloc(strlen(text) + (strlen(text) / (width - 1)) + 2);
	} else
	    result = malloc(strlen(text) * 2 + 2);
	*result = '\0';
    }
	
    memset(&ps,0,sizeof(mbstate_t));
    while (*text) {
	end = strchr(text, '\n');
	if (!end)
	    end = text + strlen(text);

	while (*text && text <= end) {
	    int len;
		
	    len = wstrlen(text, end - text);
	    if (len <= width) {
		if (result) {
		    strncat(result, text, end - text);
		    strcat(result, "\n");
		    height++;
		}

		if (len < (width / 2)) {
#ifdef DEBUG_WRAP		    
		fprintf(stderr,"adding %d\n",((width / 2) - (len)) / 2);
#endif					
		    howbad += ((width / 2) - (len)) / 2;
		}
		text = end;
		if (*text) text++;
	    } else {
		const char *spcptr = NULL;
	        int spc = 0, w2 = 0, x, w;

	        chptr = text;
		i = 0;
		while (1) {
			if ((x=mbrtowc(&tmp,chptr,end-chptr,&ps))<=0)
				break;
		        if (spc && !iswspace(tmp))
				spc = 0;
			else if (!spc && iswspace(tmp)) {
				spc = 1;
				spcptr = chptr;
				w2 = i;
			}
			w = wcwidth(tmp);
			if (w < 0)
				w = 0;
			if (i && w + i > width)
				break;
			chptr += x;
			i += w;
		}
		howbad += width - w2 + 1;
#ifdef DEBUG_WRAP		    
		fprintf(stderr,"adding %d\n",width - w2 + 1, chptr);
#endif					
		if (spcptr) chptr = spcptr;
		if (result) {
		    strncat(result, text, chptr - text );
		    strcat(result, "\n");
		    height++;
		}

		text = chptr;
		while (1) {
			if ((x=mbrtowc(&tmp,text,end-text,NULL))<=0)
				break;
			if (!iswspace(tmp)) break;
			text += x;
		}
	    }
	}
    }

    if (badness) *badness = howbad;
    if (resultPtr) *resultPtr = result;
    if (heightPtr) *heightPtr = height;
#ifdef DEBUG_WRAP
    fprintf(stderr, "width %d, badness %d, height %d\n",width, howbad, height);
#endif
}

char * newtReflowText(char * text, int width, int flexDown, int flexUp,
		      int * actualWidth, int * actualHeight) {
    int min, max;
    int i;
    char * result;
    int minbad, minbadwidth, howbad;
    char * expandedText;

    if (width < 1)
	width = 1;

    expandedText = expandTabs(text);

    if (flexDown || flexUp) {
	min = width - flexDown;
	max = width + flexUp;

	minbad = -1;
	minbadwidth = width;

	for (i = min; i >= 1 && i <= max; i++) {
	    doReflow(expandedText, NULL, i, &howbad, NULL);

	    if (minbad == -1 || howbad < minbad) {
		minbad = howbad;
		minbadwidth = i;
	    }
 	}

	width = minbadwidth;
    }

    doReflow(expandedText, &result, width, NULL, actualHeight);
    free(expandedText);
    if (actualWidth) *actualWidth = width;
    return result;
}

void newtTextboxSetText(newtComponent co, const char * text) {
    const char * start, * end;
    struct textbox * tb = co->data;
    char * reflowed, * expanded;
    int badness, height;

    if (tb->lines) {
	int i;

	for (i = 0; i < tb->numLines; i++) 
	    free(tb->lines[i]);
	free(tb->lines);
	tb->linesAlloced = tb->numLines = tb->topLine = 0;
    }

    expanded = expandTabs(text);

    if (tb->doWrap) {
	doReflow(expanded, &reflowed, tb->textWidth, &badness, &height);
	free(expanded);
	expanded = reflowed;
    }

    for (start = expanded; *start; start++)
	if (*start == '\n') tb->linesAlloced++;

    /* This ++ leaves room for an ending line w/o a \n */
    tb->linesAlloced++;
    tb->lines = malloc(sizeof(char *) * tb->linesAlloced);

    start = expanded;
    while ((end = strchr(start, '\n'))) {
	addLine(co, start, end - start);
	start = end + 1;
    }

    if (*start)
	addLine(co, start, strlen(start));

    free(expanded);
    
    textboxDraw(co);

    newtTrashScreen();    
}

/* This assumes the buffer is allocated properly! */
static void addLine(newtComponent co, const char * s, int len) {
    struct textbox * tb = co->data;

    while (wstrlen(s,len) > tb->textWidth) {
	    len--;
    }
    tb->lines[tb->numLines] = malloc(len + 1);
    memcpy(tb->lines[tb->numLines], s, len);
    tb->lines[tb->numLines++][len] = '\0';
}

static void textboxDraw(newtComponent c) {
    int i;
    struct textbox * tb = c->data;
    int size;
    char dir = 'N';
    int newdir = 1;
    int dw = 0;

    if (!c->isMapped)
	    return;
    if (tb->sb) {
	size = tb->numLines - c->height;
	newtScrollbarSet(tb->sb, tb->topLine, size ? size : 0);
	newtScrollbarSetColors(tb->sb, tb->isActive ? tb->csActive :
			tb->cs, tb->cs);
    }

    SLsmg_set_color(tb->cs);

 
    /* find direction of first visible paragraph */
    for (i = 0; i < tb->topLine; i++) {
       if (!*tb->lines[i]) {
           /* new line: new paragraph starts at the next line */
          newdir = 1;
          dir = 'N';
      }
      else if (newdir) {
          /* get current paragraph direction, if possible */
          dir = get_text_direction(tb->lines[i]);
            newdir = (dir == 'N') ? 1 : 0;
      }
    }
    
     for (i = 0; (i + tb->topLine) < tb->numLines && i < c->height; i++) {
        newtGotorc(c->top + i, c->left);
        SLsmg_write_string(tb->blankline);
        newtGotorc(c->top + i, c->left);
        /* BIDI: we need *nstring* here to properly align lines */
        write_nstring_int(tb->lines[i + tb->topLine], c->width - dw, &dir);
        /* Does new paragraph follow? */
        if (!*tb->lines[i + tb->topLine])
              dir = 'N';
    }

    /* put cursor at beginning of text for better accessibility */
    newtGotorc(c->top, c->left);
}

static struct eventResult textboxEvent(newtComponent co, 
				      struct event ev) {
    struct textbox * tb = co->data;
    struct eventResult er;

    er.result = ER_IGNORED;

    if (!tb->sb || ev.when == EV_EARLY || ev.when == EV_LATE)
	return er;

    switch(ev.event) {
      case EV_KEYPRESS:
	newtTrashScreen();
	switch (ev.u.key) {
	  case NEWT_KEY_UP:
	    if (tb->topLine) tb->topLine--;
	    textboxDraw(co);
	    er.result = ER_SWALLOWED;
	    break;

	  case NEWT_KEY_DOWN:
	    if (tb->topLine < (tb->numLines - co->height)) tb->topLine++;
	    textboxDraw(co);
	    er.result = ER_SWALLOWED;
	    break;

	  case NEWT_KEY_PGDN:
	    tb->topLine += co->height;
	    if (tb->topLine > (tb->numLines - co->height)) {
		tb->topLine = tb->numLines - co->height;
		if (tb->topLine < 0) tb->topLine = 0;
	    }
	    textboxDraw(co);
	    er.result = ER_SWALLOWED;
	    break;

	  case NEWT_KEY_PGUP:
	    tb->topLine -= co->height;
	    if (tb->topLine < 0) tb->topLine = 0;
	    textboxDraw(co);
	    er.result = ER_SWALLOWED;
	    break;
	}
	break;
      case EV_MOUSE:
	/* Top scroll arrow */
	if (ev.u.mouse.x == co->width && ev.u.mouse.y == co->top) {
	    if (tb->topLine) tb->topLine--;
	    textboxDraw(co);
	    
	    er.result = ER_SWALLOWED;
	}
	/* Bottom scroll arrow */
	if (ev.u.mouse.x == co->width &&
	    ev.u.mouse.y == co->top + co->height - 1) {
	    if (tb->topLine < (tb->numLines - co->height)) tb->topLine++;
	    textboxDraw(co);
	    
	    er.result = ER_SWALLOWED;
	}
	break;
      case EV_FOCUS:
	tb->isActive = 1;
	textboxDraw(co);
	er.result = ER_SWALLOWED;
	break;
      case EV_UNFOCUS:
	tb->isActive = 0;
	textboxDraw(co);
	er.result = ER_SWALLOWED;
	break;
    }
    return er;
}

static void textboxDestroy(newtComponent co) {
    int i;
    struct textbox * tb = co->data;

    if (tb->blankline) free(tb->blankline);
    if (tb->sb)
	tb->sb->ops->destroy(tb->sb);
    for (i = 0; i < tb->numLines; i++) 
	free(tb->lines[i]);
    free(tb->lines);
    free(tb);
    free(co);
}