2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
24 #ifdef VERBOSE_REV_INFO
25 static char rcs_id[] = "$TOG: TermPrimRender.c /main/3 1999/07/20 17:34:54 mgreess $";
26 #endif /* VERBOSE_REV_INFO */
30 * (c) Copyright 1993, 1994, 1996 Hewlett-Packard Company *
31 * (c) Copyright 1993, 1994, 1996 International Business Machines Corp. *
32 * (c) Copyright 1993, 1994, 1996 Sun Microsystems, Inc. *
33 * (c) Copyright 1993, 1994, 1996 Novell, Inc. *
34 * (c) Copyright 1996 Digital Equipment Corporation. *
35 * (c) Copyright 1996 FUJITSU LIMITED. *
36 * (c) Copyright 1996 Hitachi. *
39 #include "TermHeader.h"
40 #include "TermPrimDebug.h"
41 #include "TermPrimP.h"
42 #include "TermPrimI.h"
43 #include "TermPrimData.h"
44 #include "TermPrimLineDraw.h"
45 #include "TermPrimBufferP.h"
46 #include "TermPrimRenderP.h"
47 #include "TermPrimSelectP.h"
48 #include "TermPrimMessageCatI.h"
53 _termSetRenderFont(Widget w, TermFont *termFont)
55 DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
56 struct termData *tpd = tw->term.tpd;
59 /* DKS: ** alert ** alert ** alert ** alert ** alert **
61 * The following opens up a memory leak that needs to be closed.
62 * we need to free up both the list and any created entries...
65 /* do initialization... */
66 if (!tpd->renderTermFontsNum) {
67 tpd->renderTermFontsNum = 4;
68 tpd->renderTermFonts = (TermFont **) malloc((unsigned)
69 tpd->renderTermFontsNum * sizeof(TermFont *));
70 (void) memset(tpd->renderTermFonts, '\0',
71 tpd->renderTermFontsNum * sizeof(TermFont *));
74 /* replace the font if we already have one defined, else add it... */
75 for (i = 0; i < tpd->renderTermFontsNum; i++) {
76 if (tpd->renderTermFonts[i] && (tpd->renderTermFonts[i]->id ==
78 /* found an existing font with this id... */
82 if (i >= tpd->renderTermFontsNum) {
83 /* we didn't find one, so let's add this one to the list... */
84 for (i = 0; i < tpd->renderTermFontsNum; i++) {
85 if (!tpd->renderTermFonts[i])
89 /* check for list full... */
90 if (i >= tpd->renderTermFontsNum) {
91 (void) fprintf(stderr,
92 "setRenderFont: list full can't add font to widget 0x%lx\n",
96 /* we found an empty spot and need to malloc storage... */
97 tpd->renderTermFonts[i] = (TermFont *) malloc(sizeof(TermFont));
98 /* we don't need to initialize it as we will do a copy below... */
101 (void) memcpy(tpd->renderTermFonts[i], termFont, sizeof(TermFont));
102 if ((0 == tpd->cellWidth) || ('@' == termFont->id)) {
103 tpd->cellWidth = termFont->width;
104 tpd->cellHeight = termFont->height;
106 tw->term.widthInc = termFont->width;
107 tw->term.heightInc = termFont->height;
108 tw->term.ascent = termFont->ascent;
114 _DtTermPrimBell(Widget w)
116 DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
117 struct termData *tpd = tw->term.tpd;
120 if (tw->term.visualBell) {
121 /* the speed of this operation is not critical, so we will just
122 * use the standard text rendering GC and restore it after we
125 if (tpd->renderGC.foreground !=
126 tw->primitive.foreground ^ tw->core.background_pixel) {
127 tpd->renderGC.foreground =
128 tw->primitive.foreground ^ tw->core.background_pixel;
129 (void) XSetForeground(XtDisplay(w), tpd->renderGC.gc,
130 tpd->renderGC.foreground);
132 (void) XSetFunction(XtDisplay(w), tpd->renderGC.gc, GXxor);
133 for (i = 0; i < 2; i++) {
134 (void) XFillRectangle(XtDisplay(w), /* Display */
135 XtWindow(w), /* Drawable */
136 tpd->renderGC.gc, /* GC */
137 tpd->offsetX, /* x */
138 tpd->offsetY, /* y */
139 tw->term.columns * tpd->cellWidth,
141 tw->term.rows * tpd->cellHeight);
143 (void) XSync(XtDisplay(w), 0);
145 /* restore the GC... */
146 (void) XSetFunction(XtDisplay(w), tpd->renderGC.gc, GXcopy);
148 (void) XBell(XtDisplay(w), 0);
153 _DtTermPrimRefreshText(Widget w, short startColumn, short startRow,
154 short endColumn, short endRow)
156 DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
157 struct termData *tpd = tw->term.tpd;
158 TermBuffer tBuffer = tpd->termBuffer;
160 unsigned char *linePtr;
162 int currentColorPair = 0;
163 int currentVideo = 0;
164 short chunkStartColumn;
166 enhValues enhancements;
169 unsigned long valueMask;
172 TermEnhInfoRec enhInfo;
173 Boolean checkSelection = False;
176 XmTextPosition begin, end;
178 DebugF('t', 0, fprintf(stderr, ">>_DtTermPrimRefreshText() starting\n"));
179 DebugF('t', 0, fprintf(stderr,
180 ">>_DtTermPrimRefreshText() startCol=%hd startRow=%hd endCol=%hd endRow=%hd\n",
181 startColumn, startRow, endColumn, endRow));
183 if (tpd->mbCurMax > 1)
185 _DtTermPrimRefreshTextWc(w, startColumn, startRow, endColumn, endRow);
189 /* clip start/end x/y... */
190 if (startColumn <= 0)
194 if (endColumn >= tw->term.columns)
195 endColumn = tw->term.columns - 1;
196 if (endRow >= tw->term.rows)
197 endRow = tw->term.rows - 1;
200 ** don't display if we are in jump scroll and in the process
201 ** of scrolling and inside the scroll region...
203 if (tw->term.jumpScroll && tpd->scroll.jump.scrolled) {
204 /* set all the scrollRefreshRows flags... */
205 for (; startRow <= endRow; startRow++) {
206 tpd->scrollRefreshRows[startRow] = True;
211 ">>_DtTermPrimRefreshText() jump scroll in progress, no render\n"));
215 if (!tpd->renderGC.gc) {
216 /* get a drawImageString GC... */
220 /***********************************************************
223 /* set the GC fg and bg... */
224 values.foreground = tw->term.reverseVideo ?
225 tw->core.background_pixel : tw->primitive.foreground;
226 values.background = tw->term.reverseVideo ?
227 tw->primitive.foreground : tw->core.background_pixel;
229 tpd->renderGC.gc = XCreateGC(XtDisplay(w), XtWindow(w),
230 GCForeground | GCBackground, &values);
232 /* set the GC cache values... */
233 tpd->renderGC.foreground = values.foreground;
234 tpd->renderGC.background = values.background;
235 tpd->renderGC.fid = (Font) 0;
237 /***********************************************************
240 values.foreground = tw->term.reverseVideo ?
241 tw->primitive.foreground : tw->core.background_pixel;
242 values.background = tw->term.reverseVideo ?
243 tw->core.background_pixel : tw->primitive.foreground;
245 tpd->renderReverseGC.gc = XCreateGC(XtDisplay(w), XtWindow(w),
246 GCForeground | GCBackground, &values);
248 /* set the GC cache values... */
249 tpd->renderReverseGC.foreground = values.foreground;
250 tpd->renderReverseGC.background = values.background;
251 tpd->renderReverseGC.fid = (Font) 0;
253 /***********************************************************
256 values.foreground = tw->term.reverseVideo ?
257 tw->primitive.foreground : tw->core.background_pixel;
258 values.background = tw->term.reverseVideo ?
259 tw->core.background_pixel : tw->primitive.foreground;
261 tpd->clearGC.gc = XCreateGC(XtDisplay(w), XtWindow(w),
262 GCForeground | GCBackground, &values);
264 /* set the GC cache values... */
265 tpd->clearGC.foreground = values.foreground;
266 tpd->clearGC.background = values.background;
267 tpd->clearGC.fid = (Font) 0;
270 #ifdef SUN_MOTIF_PERF
271 /* use the clear GC... */
272 gc = tpd->clearGC.gc;
274 valueMask = (unsigned long) 0;
275 if (tpd->clearGC.foreground !=
276 tw->term.reverseVideo ?
277 tw->core.background_pixel : tw->primitive.foreground) {
279 tw->term.reverseVideo ?
280 tw->primitive.foreground :
281 tw->core.background_pixel;
282 tpd->clearGC.foreground = values.foreground;
283 valueMask |= GCForeground;
285 if (tpd->clearGC.background !=
286 tw->term.reverseVideo ?
287 tw->primitive.foreground :
288 tw->core.background_pixel) {
290 tw->term.reverseVideo ?
291 tw->core.background_pixel :
292 tw->primitive.foreground;
293 tpd->clearGC.background = values.background;
294 valueMask |= GCBackground;
297 (void) XChangeGC(XtDisplay(w), tpd->clearGC.gc,
300 (void) XFillRectangle(XtDisplay(w), /* Display */
301 XtWindow(w), /* Drawable */
303 startColumn * tpd->cellWidth + tpd->offsetX,
305 startRow * tpd->cellHeight + tpd->offsetY,
307 (endColumn - startColumn + 1) * tpd->cellWidth,
309 (endRow - startRow + 1) * tpd->cellHeight);
311 #endif /* SUN_MOTIF_PERF */
313 for (; startRow <= endRow; startRow++) {
314 /* if we are refreshing a full line, then we can clear the
315 * scrollRefreshRows flag for this line...
317 if ((startColumn == 0) && (endColumn >= tw->term.columns - 1)) {
318 tpd->scrollRefreshRows[startRow] = False;
321 lineNum = startRow + tpd->topRow;
322 if (!tw->term.jumpScroll && tpd->scroll.nojump.pendingScroll) {
323 if (lineNum != tpd->scrollLockBottomRow)
325 lineNum -= tpd->scroll.nojump.pendingScrollLines;
329 /* are we in the selected area?... */
330 if (tpd->useHistoryBuffer)
332 if (_DtTermPrimSelectGetSelection(w, &begin, &end) &&
334 (lineNum >= (begin / (tpd->selectInfo->columns + 1)) -
335 tpd->lastUsedHistoryRow) &&
336 (lineNum <= (end / (tpd->selectInfo->columns + 1)) -
337 tpd->lastUsedHistoryRow)) {
338 checkSelection = True;
340 checkSelection = False;
345 if (_DtTermPrimSelectGetSelection(w, &begin, &end) &&
347 (lineNum >= (begin / (tpd->selectInfo->columns + 1))) &&
348 (lineNum <= (end / (tpd->selectInfo->columns + 1)))) {
349 checkSelection = True;
351 checkSelection = False;
355 chunkStartColumn = startColumn;
356 if (startColumn > endColumn) {
357 /* nothing to render on this line... */
361 if (lineNum >= tpd->lastUsedRow) {
362 /* we are pointing to empty screen space below the last used
363 * line of the display...
367 } else if (lineNum < 0) {
368 if ((tpd->useHistoryBuffer) &&
369 (-lineNum <= tpd->lastUsedHistoryRow)) {
370 /* get a line out of the history buffer... */
371 lineWidth = MAX(0, MIN(endColumn - startColumn + 1,
372 _DtTermPrimBufferGetLineWidth(tpd->historyBuffer,
373 tpd->lastUsedHistoryRow + lineNum) - startColumn));
375 _DtTermPrimBufferGetCharacterPointer(tpd->historyBuffer,
376 tpd->lastUsedHistoryRow + lineNum, startColumn);
378 /* we are above the history buffer. Should not happen, but...
384 /* get the line width and a pointer to the data... */
385 lineWidth = MAX(0, MIN(endColumn - startColumn + 1,
386 _DtTermPrimBufferGetLineWidth(tBuffer, lineNum) -
388 linePtr = _DtTermPrimBufferGetCharacterPointer(tBuffer, lineNum,
392 while (lineWidth > 0) {
393 /* get the enhancement values for the first chunk of the
397 (void) _DtTermPrimBufferGetEnhancement(tBuffer,
400 chunkStartColumn, /* col */
401 &enhancements, /* enhancements */
402 &chunkWidth, /* width */
403 countAll); /* countWhich */
405 /* get it from the history buffer... */
406 (void) _DtTermPrimBufferGetEnhancement(tpd->historyBuffer,
408 tpd->lastUsedHistoryRow + lineNum,
410 chunkStartColumn, /* col */
411 &enhancements, /* enhancements */
412 &chunkWidth, /* width */
413 countAll); /* countWhich */
416 /* clip chunkWidth... */
417 if (chunkWidth > lineWidth)
418 chunkWidth = lineWidth;
420 /* set reasonable defaults for our render info... */
421 enhInfo.fg = tw->primitive.foreground;
422 enhInfo.bg = tw->core.background_pixel;
423 enhInfo.font = tpd->defaultTermFont;
424 enhInfo.flags = (unsigned long) 0;
426 /* set our font and color from the enhancements... */
427 if (ENH_PROC(tBuffer)) {
428 (void) (*(ENH_PROC(tBuffer)))(w, enhancements, &enhInfo);
431 /* if we are in reverse video mode... */
432 if (tw->term.reverseVideo) {
433 /* flip fg and bg... */
434 tmpPixel = enhInfo.fg;
435 enhInfo.fg = enhInfo.bg;
436 enhInfo.bg = tmpPixel;
439 /* are we in the selection area?... */
440 if (checkSelection &&
441 _DtTermPrimSelectIsInSelection(w, lineNum, chunkStartColumn,
442 chunkWidth, &chunkWidth)) {
444 /* flip fg and bg... */
445 tmpPixel = enhInfo.fg;
446 enhInfo.fg = enhInfo.bg;
447 enhInfo.bg = tmpPixel;
450 /* if secure, we will use a XFillRectangle, and we need
451 * foreground set to the background...
454 if (TermIS_SECURE(enhInfo.flags)) {
455 /* render secure video locally...
457 /* set the renderReverseGC... */
458 valueMask = (unsigned long) 0;
459 if (tpd->renderReverseGC.foreground != enhInfo.bg) {
460 tpd->renderReverseGC.foreground = enhInfo.bg;
461 values.foreground = enhInfo.bg;
462 valueMask |= GCForeground;
465 (void) XChangeGC(XtDisplay(w), tpd->renderReverseGC.gc,
468 (void) XFillRectangle(XtDisplay(w),
470 tpd->renderReverseGC.gc,
471 chunkStartColumn * tpd->cellWidth + tpd->offsetX,
472 startRow * tpd->cellHeight + tpd->offsetY,
473 tpd->cellWidth * chunkWidth,
476 /* underline as well... */
477 if (TermIS_UNDERLINE(enhInfo.flags)) {
478 valueMask = (unsigned long) 0;
479 if (tpd->renderGC.foreground != enhInfo.fg) {
480 tpd->renderGC.foreground = enhInfo.fg;
481 values.foreground = enhInfo.fg;
482 valueMask |= GCForeground;
485 (void) XChangeGC(XtDisplay(w), tpd->renderGC.gc,
488 (void) XDrawLine(XtDisplay(w),
490 XtWindow(w), /* Drawable */
491 tpd->renderGC.gc, /* GC */
492 chunkStartColumn * tpd->cellWidth + tpd->offsetX,
494 startRow * tpd->cellHeight + tpd->offsetY +
497 (chunkStartColumn + chunkWidth) * tpd->cellWidth +
498 tpd->offsetX, /* X2 */
499 startRow * tpd->cellHeight + tpd->offsetY +
500 tpd->cellHeight - 1);
504 (void) _DtTermPrimRenderText(
506 enhInfo.font, /* TermFont */
507 enhInfo.fg, /* fg Pixel */
508 enhInfo.bg, /* bg Pixel */
509 enhInfo.flags, /* flags */
510 chunkStartColumn * tpd->cellWidth + tpd->offsetX,
512 startRow * tpd->cellHeight + tpd->offsetY,
514 linePtr, /* string */
515 chunkWidth); /* width */
517 chunkStartColumn += chunkWidth;
518 lineWidth -= chunkWidth;
519 linePtr += chunkWidth;
522 /* clear any extra space in the line. chunkStartColumn now points to
523 * the end of the line, and lineWidth == 0...
525 while (endColumn - chunkStartColumn >= 0) {
526 chunkWidth = endColumn - chunkStartColumn + 1;
527 if (checkSelection &&
528 _DtTermPrimSelectIsInSelection(w, lineNum, chunkStartColumn,
529 chunkWidth, &chunkWidth)) {
530 /* use the render gc set to the fg color... */
531 gc = tpd->renderReverseGC.gc;
533 valueMask = (unsigned long) 0;
534 if (tpd->renderReverseGC.foreground !=
535 tw->term.reverseVideo ?
536 tw->core.background_pixel : tw->primitive.foreground) {
538 tw->term.reverseVideo ?
539 tw->core.background_pixel :
540 tw->primitive.foreground;
541 tpd->renderReverseGC.foreground = values.foreground;
542 valueMask |= GCForeground;
545 (void) XChangeGC(XtDisplay(w), tpd->renderReverseGC.gc,
548 #ifndef SUN_MOTIF_PERF
550 /* use the clear GC... */
551 gc = tpd->clearGC.gc;
553 valueMask = (unsigned long) 0;
554 if (tpd->clearGC.foreground !=
555 tw->term.reverseVideo ?
556 tw->core.background_pixel : tw->primitive.foreground) {
558 tw->term.reverseVideo ?
559 tw->primitive.foreground :
560 tw->core.background_pixel;
561 tpd->clearGC.foreground = values.foreground;
562 valueMask |= GCForeground;
564 if (tpd->clearGC.background !=
565 tw->term.reverseVideo ?
566 tw->primitive.foreground :
567 tw->core.background_pixel) {
569 tw->term.reverseVideo ?
570 tw->core.background_pixel :
571 tw->primitive.foreground;
572 tpd->clearGC.background = values.background;
573 valueMask |= GCBackground;
576 (void) XChangeGC(XtDisplay(w), tpd->clearGC.gc,
580 #endif /* not SUN_MOTIF_PERF */
582 if (isDebugFSet('t', 1)) {
586 /* Fill in the clear area so we can see what is going to
589 (void) XFillRectangle(XtDisplay(w),
592 chunkStartColumn * tpd->cellWidth + tpd->offsetX,
593 startRow * tpd->cellHeight + tpd->offsetY,
594 chunkWidth * tpd->cellWidth,
596 (void) XSync(XtDisplay(w), False);
597 (void) shortSleep(100000);
599 (void) XFillRectangle(XtDisplay(w),
601 XtWindow(w), /* Drawable */
603 chunkStartColumn * tpd->cellWidth + tpd->offsetX,
605 startRow * tpd->cellHeight + tpd->offsetY,
607 chunkWidth * tpd->cellWidth,
609 tpd->cellHeight); /* height */
610 #ifdef SUN_MOTIF_PERF
612 #endif /* SUN_MOTIF_PERF */
613 chunkStartColumn += chunkWidth;
616 DebugF('t', 0, fprintf(stderr, ">>_DtTermPrimRefreshText() finished\n"));
620 /**************************************************************************
622 * _DtTermPrimExposeText(): expose (refresh) the text in rectangular area
625 * Widget w: widget to expose
626 * int x: starting x pixel in window
627 * int y: starting y pixel in window
628 * int width: width in pixels
629 * int height: height in pixels
630 * Boolean isExposeEvent: true, deal with copyarea / expose overlap
636 * This function will redisplay the text in a rectangular exposure
637 * area. The x, y, width, and height are in absolute pixels. The
638 * internal x and y offsets will be accounted for, and the minimum
639 * number of characters will be displayed. The algorithm has been
640 * tuned (somewhat) and no longer exposes an extra character at the
641 * end of the refresh lines and an extra line at the bottom of the
642 * refresh area. This can be verified by using the 't' and 'e'
647 _DtTermPrimExposeText(Widget w, int x, int y, int width, int height,
648 Boolean isExposeEvent)
650 struct termData *tpd = ((DtTermPrimitiveWidget) w)->term.tpd;
652 DebugF('e', 0, fprintf(stderr, ">>exposeText() starting\n"));
653 DebugF('e', 0, fprintf(stderr,
654 ">>exposeText() startX=%d startY=%d width=%d height=%d\n",
655 x, y, width, height));
656 DebugF('e', 0, fprintf(stderr,
657 ">> offsetX=%d offsetY=%d cellHeight=%d cellWidth=%d\n",
658 tpd->offsetX, tpd->offsetY, tpd->cellHeight, tpd->cellWidth));
660 /* The following "hack" takes care of the problem of an exposure event
661 * from the server and a copy area from the client crossing. The
662 * combination of these two events can cause a race condition which
663 * manifests itself by leaving a hole in the terminal window. What
666 * A window is partially obscured. The terminal emulator does a copy
667 * area (scroll) which includes part of obscured area. Before the
668 * server processes the copy area, the window is unobscured, and the
669 * server sends an exposure event back to the client.
671 * - The window is partially obscured.
673 * - The terminal emulator does a copy area (scroll) which includes a
674 * portion of the obscured area. Normally, the server will generate
675 * a graphics exposure event for the obscured portion that it can't
676 * copy which will allow the terminal emulator to update the area.
678 * - Before the server receives the copy area request, the server
679 * unobscures the window and sends an exposure event for the exposed
680 * area. (This is where the hack comes into play and refreshes the
681 * scrolled portion of this area (and possibly some extra space as
684 * - The server processes the copy area. Since the area in question is
685 * no longer obscured, the server will copy blank space and not
686 * generate a graphics exposure event.
688 * - The terminal emulator processes the exposure event and refreshes
689 * the exposed area. (This is the hack extends the exposure to cover
692 * - You now have a blank chunk of the terminal window where the
693 * obscured area was scrolled (without the hack).
695 * Our fix is similar to the one used in xterm. The Motif text widget
696 * uses a more brute force method and simply extends the exposure event
697 * to the full height (or width) of the screen in the direction of the
700 if (isExposeEvent && tpd->scrollInProgress) {
706 bothX1 = MAX(tpd->scrollSrcX, x);
707 bothY1 = MAX(tpd->scrollSrcY, y);
708 bothX2 = MIN(tpd->scrollSrcX + tpd->scrollWidth, x + width - 1);
709 bothY2 = MIN(tpd->scrollSrcY + tpd->scrollHeight, y + height - 1);
711 if ((bothX2 > bothX1) && (bothY2 > bothY1)) {
712 (void) _DtTermPrimRefreshText(w,
713 (x - tpd->offsetX + tpd->scrollDestX - tpd->scrollSrcX) /
715 (y - tpd->offsetY + tpd->scrollDestY - tpd->scrollSrcY) /
717 (x + width - 1 - tpd->offsetX + tpd->scrollDestX -
718 tpd->scrollSrcX) / tpd->cellWidth,
719 (y + height - 1 - tpd->offsetY + tpd->scrollDestY -
720 tpd->scrollSrcY) / tpd->cellHeight);
724 /* render the text... */
725 DebugF('t', 0, fprintf(stderr, ">>exposeText() calling _DtTermPrimRefreshText()\n"));
726 (void) _DtTermPrimRefreshText(w, (x - tpd->offsetX) / tpd->cellWidth,
727 (y - tpd->offsetY) / tpd->cellHeight,
728 (x + width - 1 - tpd->offsetX) / tpd->cellWidth,
729 (y + height - 1 - tpd->offsetY) / tpd->cellHeight);
731 DebugF('e', 0, fprintf(stderr, ">>exposeText() finished\n"));
735 _DtTermPrimFillScreenGap(Widget w)
737 DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
738 struct termData *tpd = tw->term.tpd;
739 TermBuffer tBuffer = tpd->termBuffer;
742 int historyLinesNeeded;
744 TermLineSelection selectionFlag;
746 /* before we insert any text, we need to insure that the cursor is
747 * on a valid buffer row. If not, we will need to fill in any gap
748 * between the lastUsedRow and the current cursor postion with 0
749 * width lines, and get a buffer row for the current cursor row...
751 if (tpd->topRow + tpd->cursorRow >= tpd->lastUsedRow) {
752 /* if there are lines left in the buffer, just use them...
754 /* this does not effect the position of the selection... */
755 for (; (tpd->lastUsedRow < tpd->bufferRows) &&
756 (tpd->topRow + tpd->cursorRow >= tpd->lastUsedRow); ) {
757 /* all we need to do is clear the line... */
758 _DtTermPrimBufferClearLine(tBuffer, tpd->lastUsedRow++, 0);
761 /* if we still need more lines, then we will need to steal from
762 * the top of the buffer...
764 if (tpd->topRow + tpd->cursorRow >= tpd->lastUsedRow) {
765 if (tpd->memoryLockMode == SCROLL_LOCKprotect) {
766 if (!tpd->warningDialogMapped) {
767 (void) _DtTermPrimWarningDialog(w,
768 GETMESSAGE(NL_SETN_PrimRend, 1,"MEMORY FULL\nPress OK to clear"));
771 /* figure out how many lines are needed... */
772 linesNeeded = tpd->topRow + tpd->cursorRow -
773 (tpd->lastUsedRow - 1);
775 /* before we drop them off of the top, if we are
776 * using a history buffer, we need to transfer them
777 * into the history buffer...
779 if (tpd->useHistoryBuffer &&
780 (tpd->scrollLockTopRow == 0) &&
781 (tpd->scrollLockBottomRow >= (tw->term.rows - 1))) {
782 /* do we have enough lines in the history buffer, or
783 * do we need to steal some from the top?...
785 historyLinesNeeded = linesNeeded -
786 (tpd->historyBufferRows - tpd->lastUsedHistoryRow);
787 if ((historyLinesNeeded > 0) &&
788 (tpd->historyBufferRows > 0)) {
789 /* take them from the top... */
790 /* we are effectively deleting the lines from the
791 * top of the combined buffers...
793 (void) _DtTermPrimSelectDeleteLines(w, 0,
795 /* move the lines from the top to the bottom... */
796 (void) _DtTermPrimBufferInsertLineFromTB(
797 tpd->historyBuffer, tpd->historyBufferRows - 1,
798 historyLinesNeeded, insertFromTop);
799 /* the lines are now freed, adjust the used count... */
800 tpd->lastUsedHistoryRow -= historyLinesNeeded;
801 if (tpd->lastUsedHistoryRow < 0) {
802 historyLinesNeeded += tpd->lastUsedHistoryRow;
803 tpd->lastUsedHistoryRow = 0;
807 /* copy the lines over... */
808 for (i1 = 0; (i1 < linesNeeded) &&
809 (tpd->lastUsedHistoryRow < tpd->historyBufferRows);
813 termChar *overflowChars;
816 /* get the line from the active buffer... */
817 length = _DtTermPrimBufferGetLineLength(tBuffer,
821 ** stuff it into the history buffer...
822 ** (but only if there is something to copy)
825 unsigned char eIndex;
828 enhValue *eValues = (enhValue *)NULL;
830 overflowChars = (termChar *) XtMalloc(BUFSIZ * sizeof (termChar));
831 c1 = _DtTermPrimBufferGetCharacterPointer(
834 /* Perpetuate the enhancements. */
835 for (eCol = 0; eCol < length; eCol += eCount)
837 if (_DtTermPrimBufferGetEnhancement(tBuffer,
841 if ((eValues == (enhValue *)NULL) ||
846 eIndex < NUM_ENH_FIELDS(tBuffer);
849 _DtTermPrimBufferSetEnhancement(
851 tpd->lastUsedHistoryRow,
852 eCol, eIndex, eValues[eIndex]);
855 (void) _DtTermPrimBufferInsert(
857 tpd->lastUsedHistoryRow,
859 False, &overflowChars,
867 /* Clear out enhancement values if necessary */
868 if (eValues != (enhValue *)NULL)
871 eIndex < NUM_ENH_FIELDS(tBuffer);
874 _DtTermPrimBufferSetEnhancement(
876 tpd->lastUsedHistoryRow,
880 (void) _DtTermPrimBufferInsert(
882 tpd->lastUsedHistoryRow,
885 False, &overflowChars,
889 (void) XtFree((char *) overflowChars);
891 (void) _DtTermPrimBufferClearLine(
893 tpd->lastUsedHistoryRow, 0);
895 /* perpetuate the state of the wrap flag... */
896 (void) _DtTermPrimBufferSetLineWrapFlag(
898 tpd->lastUsedHistoryRow,
899 _DtTermPrimBufferTestLineWrapFlag(tBuffer, i1));
900 /* perpetuate the state of the inSelection flag...
904 _DtTermPrimBufferGetInSelectionFlag(
906 (void) _DtTermPrimBufferSetInSelectionFlag(
908 tpd->lastUsedHistoryRow,
911 /* clear the inselection flag before we scroll
912 * the lines off or we will end up releasing the
915 (void) _DtTermPrimBufferSetInSelectionFlag(
916 tBuffer, i1, (TermLineSelection) 0);
917 (void) tpd->lastUsedHistoryRow++;
918 (void) linesCopied++;
922 /* take them from the top. If we are about to take lines
923 * from the top without first copying them into the
924 * history buffer, we need to adjust the selection
927 if (linesNeeded > linesCopied) {
928 (void) _DtTermPrimSelectDeleteLines(w,
929 tpd->lastUsedHistoryRow + linesCopied,
930 linesNeeded - linesCopied);
932 (void) _DtTermPrimBufferInsertLineFromTB(tBuffer,
934 linesNeeded, insertFromTop);
935 /* adjust everything... */
936 tpd->topRow -= linesNeeded;
943 DoInsert(Widget w, unsigned char *buffer, int length, Boolean *wrapped)
945 DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
946 struct termData *tpd = tw->term.tpd;
947 TermBuffer tBuffer = tpd->termBuffer;
950 termChar *returnChars;
953 /* before we insert any text, we need to insure that the cursor is on
954 * a valid buffer row...
956 if (tpd->topRow + tpd->cursorRow >= tpd->lastUsedRow) {
957 (void) _DtTermPrimFillScreenGap(w);
960 /* insert the text... */
961 returnChars = (termChar *) XtMalloc(BUFSIZ * sizeof (termChar));
962 newWidth = _DtTermPrimBufferInsert(tBuffer, /* TermBuffer */
963 tpd->topRow + tpd->cursorRow, /* row */
964 tpd->cursorColumn, /* column */
965 (termChar *) buffer, /* newChars */
966 length, /* numChars */
967 tpd->insertCharMode != DtTERM_INSERT_CHAR_OFF,
969 &returnChars, /* return char ptr */
970 &returnCount); /* return count ptr */
972 if ((tpd->insertCharMode != DtTERM_INSERT_CHAR_ON_WRAP) || (returnCount <= 0)) {
973 (void) XtFree((char *) returnChars);
977 /* we are in insert char mode with wrap and we wrapped text off of
982 /* wrap the inserted characters into the following line... */
983 if (tpd->topRow + tpd->cursorRow + 1 >= tpd->lastUsedRow) {
984 /* fake cursorRow... */
985 (void) tpd->cursorRow++;
986 (void) _DtTermPrimFillScreenGap(w);
987 (void) tpd->cursorRow--;
990 /* we will allocate a temporary buffer so we don't have to worry
991 * about _DtTermPrimBufferInsert tromping over its overflow buffer...
993 length = returnCount;
994 buffer = (unsigned char *) XtMalloc(length);
995 (void) memcpy(buffer, returnChars, length);
997 /* insert the text into the next line... */
998 newWidth = _DtTermPrimBufferInsert(tBuffer, /* TermBuffer */
999 tpd->topRow + tpd->cursorRow + 1,
1002 (termChar *) buffer, /* newChars */
1003 length, /* numChars */
1004 tpd->insertCharMode != DtTERM_INSERT_CHAR_OFF,
1006 &returnChars, /* return char ptr */
1007 &returnCount); /* return count ptr */
1009 (void) XtFree((char *) buffer);
1010 (void) XtFree((char *) returnChars);
1015 _DtTermPrimInsertText(Widget w, unsigned char *buffer, int length)
1017 DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
1018 struct termData *tpd = tw->term.tpd;
1019 TermBuffer tBuffer = tpd->termBuffer;
1024 short insertCharCount;
1026 Boolean needToRender = False;
1027 Boolean wrapped = False;
1030 if (tpd->mbCurMax > 1)
1040 ** It would be nice if the calling function supplied us with a count
1041 ** of the number of mb characters in the buffer, then we wouldn't
1042 ** have to count them again.
1045 ** we could use this if the multi-byte buffer was null terminated
1047 wcBufferLen = mbstowcs((wchar_t *)NULL, (char *)buffer, length);
1050 pmb = (char *)buffer;
1052 ** we should never need more than length * sizeof(wchar_t)
1053 ** bytes to store the wide char equivalent of the incoming mb string
1055 wcBuffer = (wchar_t *)XtMalloc(length * sizeof(wchar_t));
1060 switch (mbLen = mbtowc(pwc, pmb, MIN(((int)MB_CUR_MAX), length - i)))
1063 if ((int)MB_CUR_MAX <= length - i) {
1064 /* we have a bogus multi-byte character. Throw away
1065 * the first byte and rescan (TM 12/14/93)...
1068 ** in this case, we move the remaining length - i - 1
1069 ** bytes one byte to the left (to overwrite the bogus
1072 memmove(pmb, pmb + 1, length - i - 1);
1079 ** treat null character same as any other character...
1090 i = _DtTermPrimInsertTextWc(w, wcBuffer, wcBufferLen);
1091 /* convert back from a wide character count to a multibyte
1092 * character count...
1094 pmb = (char *)buffer;
1098 while (i < wcBufferLen)
1100 switch (mbLen = mblen(pmb, MIN(((int)MB_CUR_MAX), length - i)))
1105 ** treat null character same as any other character...
1113 XtFree((char *)wcBuffer);
1114 return(pmb - (char *) buffer);
1117 /* turn off the cursor... */
1118 if (CURSORoff != tpd->cursorState) {
1119 (void) _DtTermPrimCursorOff(w);
1122 /* we support two different types of autowrap. The HP style one is where
1123 * you display the character and then wrap if you are at the end of the
1124 * line. The ANSI style one is where you insert the character at the end
1125 * of the line and don't autowrap until you try to insert another
1129 renderStartX = tpd->cursorColumn;
1130 renderEndX = tpd->cursorColumn;
1131 insertStartX = tpd->cursorColumn;
1132 insertCharCount = 0;
1134 for (i = 0; (i < length) && tpd->ptyInputId; i++) {
1135 /* the following code performs two functions. If we are in
1136 * autowrap, it performs a sanity check on the insert position.
1137 * if we are not in autowrap, it will insure that characters
1138 * inserted after the last position will replace the last
1141 if ((tpd->cursorColumn >= tw->term.columns) &&
1142 !(tpd->autoWrapRight && !tpd->wrapRightAfterInsert)) {
1143 /* blow away the previous character...
1145 tpd->cursorColumn = tw->term.columns - 1;
1146 renderStartX = MIN(renderStartX, tpd->cursorColumn);
1147 tpd->wrapState = WRAPpastRightMargin;
1150 /* for emulations that wrap after inserting the character, we
1151 * will insert the character and then check for wrap...
1153 if (tpd->wrapRightAfterInsert) {
1154 if (insertCharCount == 0) {
1157 (void) insertCharCount++;
1160 if (((tpd->cursorColumn + insertCharCount) >= tw->term.columns) ||
1161 ((tpd->cursorColumn + insertCharCount) ==
1162 tpd->rightMargin + 1)) {
1163 if (tpd->autoWrapRight) {
1164 /* perform an auto wrap...
1166 /* we need to insert any pending characters, and
1169 if (insertCharCount) {
1170 newWidth = DoInsert(w, &buffer[insertStartX],
1171 insertCharCount, &wrapped);
1172 tpd->cursorColumn += insertCharCount;
1173 insertCharCount = 0;
1174 if (tpd->insertCharMode != DtTERM_INSERT_CHAR_OFF) {
1175 renderEndX = newWidth;
1177 renderEndX = MAX(renderEndX, tpd->cursorColumn);
1179 needToRender = True;
1182 DebugF('t', 0, fprintf(stderr,
1183 ">>termInsertText() calling[2] _DtTermPrimRefreshText()\n"));
1184 (void) _DtTermPrimRefreshText(w, renderStartX, tpd->cursorRow,
1185 wrapped ? tw->term.columns : MAX(renderEndX, 0),
1187 if (wrapped && (tpd->cursorRow + 1 < tw->term.rows)) {
1188 (void) _DtTermPrimRefreshText(w, 0, tpd->cursorRow + 1,
1189 renderEndX, tpd->cursorRow + 1);
1192 needToRender = False;
1194 tpd->cursorColumn = tpd->leftMargin;
1195 tpd->wrapState = WRAPbetweenMargins;
1198 _DtTermPrimBufferSetLineWrapFlag(tBuffer,
1199 tpd->topRow + tpd->cursorRow,
1202 if (tpd->cursorRow == tpd->scrollLockBottomRow) {
1203 /* scroll at the bottom of the lock area... */
1204 (void) _DtTermPrimScrollText(w, 1);
1205 (void) _DtTermPrimFillScreenGap(w);
1206 } else if (++tpd->cursorRow >= tw->term.rows) {
1207 /* we are at the bottom row of the locked region.
1208 * Wrap to the beginning of this line...
1210 tpd->cursorRow = tw->term.rows - 1;
1212 /* we are not at the bottom row. We already have
1213 * wrapped down one line...
1217 renderStartX = tpd->cursorColumn;
1219 if (tpd->scroll.nojump.pendingScroll) {
1220 /* If we wrapped the screen, bail out now and we
1221 * will take care of this character when we
1222 * finish the scroll. If we are in wrap after,
1223 * then we need to skip past this character so
1224 * that it doesn't get processed twice...
1226 if (tpd->wrapRightAfterInsert)
1231 /* overwrite the last character(s) on the line... */
1232 if (insertCharCount > 0) {
1233 newWidth = DoInsert(w, &buffer[insertStartX],
1234 insertCharCount, &wrapped);
1235 tpd->cursorColumn += insertCharCount;
1236 insertCharCount = 0;
1237 if (tpd->insertCharMode != DtTERM_INSERT_CHAR_OFF) {
1238 renderEndX = newWidth;
1240 renderEndX = MAX(renderEndX, tpd->cursorColumn);
1242 needToRender = True;
1245 if (tpd->cursorColumn == tpd->rightMargin + 1)
1246 tpd->cursorColumn = tpd->rightMargin;
1248 tpd->cursorColumn = tw->term.columns - 1;
1252 /* for emulations that wrap before inserting the character, we
1253 * will insert the character and then check for wrap...
1255 if (!tpd->wrapRightAfterInsert) {
1256 /* before we insert any text, we need to insure that the
1257 * cursor is on a valid buffer row...
1259 if (insertCharCount == 0) {
1262 (void) insertCharCount++;
1265 /* insert and render any remaining text... */
1266 if (insertCharCount > 0) {
1267 newWidth = DoInsert(w, &buffer[insertStartX],
1268 insertCharCount, &wrapped);
1269 tpd->cursorColumn += insertCharCount;
1270 insertCharCount = 0;
1271 if (tpd->insertCharMode != DtTERM_INSERT_CHAR_OFF) {
1272 renderEndX = newWidth;
1274 renderEndX = MAX(renderEndX, tpd->cursorColumn);
1276 needToRender = True;
1279 renderEndX = MAX(renderEndX, tpd->cursorColumn);
1280 DebugF('t', 0, fprintf(stderr,
1281 ">>termInsertText() calling _DtTermPrimRefreshText()\n"));
1282 (void) _DtTermPrimRefreshText(w, renderStartX, tpd->cursorRow,
1283 wrapped ? tw->term.columns : MAX(renderEndX, 0),
1285 if (wrapped && (tpd->cursorRow + 1 < tw->term.rows)) {
1286 (void) _DtTermPrimRefreshText(w, 0, tpd->cursorRow + 1,
1287 renderEndX, tpd->cursorRow + 1);
1290 needToRender = False;
1299 unsigned char *buffer,
1301 unsigned char *mbPartialChar,
1302 int *mbPartialCharLen,
1304 unsigned char **dangleBuffer,
1305 int *dangleBufferLen
1308 /* malloc our dangle buffer... */
1309 *dangleBufferLen = bufferLen + *mbPartialCharLen - writeLen;
1310 *dangleBuffer = (unsigned char *) XtMalloc(*dangleBufferLen);
1312 /* copy over the unwritten part of the orignal buffer... */
1313 (void) memmove(*dangleBuffer, buffer + writeLen, bufferLen - writeLen);
1314 if (*mbPartialCharLen) {
1315 (void) memmove(*dangleBuffer + bufferLen - writeLen, mbPartialChar,
1317 *mbPartialCharLen = 0;
1323 _DtTermPrimParseInput
1326 unsigned char *buffer,
1328 unsigned char **dangleBuffer,
1329 int *dangleBufferLen
1332 DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
1333 struct termData *tpd = tw->term.tpd;
1334 DtTermPrimitiveClassPart *termClassPart = &(((DtTermPrimitiveClassRec *)
1335 (tw->core.widget_class))->term_primitive_class);
1336 TermBuffer tBuffer = tpd->termBuffer;
1339 short insertByteCount;
1341 Boolean turnCursorOn = False;
1342 unsigned char *tmpBuffer = (unsigned char *) 0;
1343 unsigned char mbChar[MB_LEN_MAX];
1345 static Boolean *preParseTable = (Boolean *) 0;
1347 *dangleBuffer = (unsigned char *) 0;
1349 insertByteCount = 0;
1351 /* initialize the preParseTable if necessary... */
1352 _DtTermProcessLock();
1353 if (!preParseTable) {
1354 preParseTable = (Boolean *) XtMalloc(256 * sizeof(Boolean));
1355 (void) memset(preParseTable, '\0', 256 * sizeof(Boolean));
1357 for (i = 0x00; i <= 0x1f; i++) {
1358 preParseTable[i] = True;
1360 for (i = 0x80; i <= 0x9f; i++) {
1361 preParseTable[i] = True;
1364 _DtTermProcessUnlock();
1366 /* check for partial multibyte character... */
1367 if (tpd->mbPartialCharLen > 0) {
1368 tmpBuffer = (unsigned char *) XtMalloc(len + tpd->mbPartialCharLen);
1369 (void) memcpy(tmpBuffer, tpd->mbPartialChar, tpd->mbPartialCharLen);
1370 (void) memcpy(tmpBuffer + tpd->mbPartialCharLen, buffer, len);
1372 len += tpd->mbPartialCharLen;
1373 tpd->mbPartialCharLen = 0;
1376 /* turn off the cursor... */
1377 if (CURSORoff != tpd->cursorState) {
1378 (void) _DtTermPrimCursorOff(w);
1379 turnCursorOn = True;
1383 for (i = 0; (i < len) && tpd->ptyInputId; ) {
1384 if (tpd->mbCurMax > 1) {
1386 mblen((char *) &buffer[i], MIN(((int)MB_CUR_MAX), len - i)))
1389 if ((int)MB_CUR_MAX <= len - i)
1391 /* we have a bogus multi-byte character. Throw away
1392 * the first byte and rescan (TM 12/14/93)...
1394 /* dump what we know we want to insert... */
1395 if (insertByteCount > 0) {
1396 returnLen = (*(termClassPart->term_insert_proc))(w,
1397 &buffer[insertStart], insertByteCount);
1398 if (returnLen != insertByteCount) {
1399 (void) buildDangleBuffer(buffer, len,
1401 &tpd->mbPartialCharLen,
1402 insertStart + returnLen,
1403 dangleBuffer, dangleBufferLen);
1405 insertByteCount = 0;
1408 insertByteCount = 0;
1410 /* skip over the bogus char's first byte... */
1415 /* we have a dangling partial multi-byte character... */
1416 (void) memmove(tpd->mbPartialChar, &buffer[i], len - i);
1417 tpd->mbPartialCharLen = len - i;
1418 /* remove the partial char from the buffer and adjust
1433 if (((mbCharLen == 1) && preParseTable[buffer[i]]) ||
1434 tpd->parserNotInStartState) {
1436 ** It's either a control code or we are not in the start state.
1437 ** If we have any text to insert, insert
1438 ** the text, display any added text, then send it down through
1441 if (insertByteCount > 0) {
1442 returnLen = (*(termClassPart->term_insert_proc))(w,
1443 &buffer[insertStart], insertByteCount);
1444 if (returnLen != insertByteCount) {
1445 (void) buildDangleBuffer(buffer, len,
1447 &tpd->mbPartialCharLen,
1448 insertStart + returnLen,
1449 dangleBuffer, dangleBufferLen);
1450 insertByteCount = 0;
1453 insertByteCount = 0;
1456 tpd->parserNotInStartState = _DtTermPrimParse(w, &buffer[i],
1461 /* queue up text to insert into the buffer... */
1462 insertByteCount += mbCharLen;
1467 if (insertByteCount > 0)
1469 returnLen = (*(termClassPart->term_insert_proc))(w,
1470 &buffer[insertStart],
1472 if (returnLen != insertByteCount)
1474 (void) buildDangleBuffer(buffer, len,
1476 &tpd->mbPartialCharLen,
1477 insertStart + returnLen,
1478 dangleBuffer, dangleBufferLen);
1484 ** insertByteCount <= 0, check to make sure we haven't
1485 ** already saved any remaining text in the dangleBuffer...
1487 if (i < len && !*dangleBuffer)
1489 (void) buildDangleBuffer(buffer, len,
1491 &tpd->mbPartialCharLen,
1492 i, dangleBuffer, dangleBufferLen);
1496 /* if we turned the cursor off, turn the cursor back on... */
1498 (void) _DtTermPrimCursorOn(w);
1502 (void) XtFree((char *) tmpBuffer);
1504 if (*dangleBuffer) {
1511 ** Pad the current line from the current end of line up to (and
1512 ** including) the current cursor column.
1515 _DtTermPrimRenderPadLine
1520 DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
1521 struct termData *tpd = tw->term.tpd;
1522 TermBuffer tBuffer = tpd->termBuffer;
1525 /* before we pad this line, we need to insure that cursor is on a
1526 * valid buffer row...
1528 (void) _DtTermPrimFillScreenGap(w);
1530 currentWidth = _DtTermPrimBufferGetLineWidth(tBuffer,
1531 tpd->topRow + tpd->cursorRow);
1532 if (tpd->cursorColumn >= currentWidth)
1535 ** Pad the line and refresh it.
1537 if (tpd->mbCurMax > 1) {
1538 _DtTermPrimBufferPadLineWc(tBuffer, tpd->topRow + tpd->cursorRow,
1539 tpd->cursorColumn + 1);
1541 _DtTermPrimBufferPadLine(tBuffer, tpd->topRow + tpd->cursorRow,
1542 tpd->cursorColumn + 1);
1544 _DtTermPrimRefreshText(w, currentWidth, tpd->cursorRow, tpd->cursorColumn,
1550 _DtTermPrimDestroyFont(
1555 if (font) (void) (*(font->destroyFunction))(w, font);
1560 _DtTermPrimRenderText(
1565 unsigned long flags,
1568 unsigned char *string,
1572 (void) (*(font->renderFunction))(w, font, fg, bg, flags, x, y, string, len);