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 "TermPrimRender.h"
47 #include "TermPrimRenderP.h"
48 #include "TermPrimSelect.h"
49 #include "TermPrimSelectP.h"
50 #include "TermPrimMessageCatI.h"
55 _termSetRenderFont(Widget w, TermFont *termFont)
57 DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
58 struct termData *tpd = tw->term.tpd;
61 /* DKS: ** alert ** alert ** alert ** alert ** alert **
63 * The following opens up a memory leak that needs to be closed.
64 * we need to free up both the list and any created entries...
67 /* do initialization... */
68 if (!tpd->renderTermFontsNum) {
69 tpd->renderTermFontsNum = 4;
70 tpd->renderTermFonts = (TermFont **) malloc((unsigned)
71 tpd->renderTermFontsNum * sizeof(TermFont *));
72 (void) memset(tpd->renderTermFonts, '\0',
73 tpd->renderTermFontsNum * sizeof(TermFont *));
76 /* replace the font if we already have one defined, else add it... */
77 for (i = 0; i < tpd->renderTermFontsNum; i++) {
78 if (tpd->renderTermFonts[i] && (tpd->renderTermFonts[i]->id ==
80 /* found an existing font with this id... */
84 if (i >= tpd->renderTermFontsNum) {
85 /* we didn't find one, so let's add this one to the list... */
86 for (i = 0; i < tpd->renderTermFontsNum; i++) {
87 if (!tpd->renderTermFonts[i])
91 /* check for list full... */
92 if (i >= tpd->renderTermFontsNum) {
93 (void) fprintf(stderr,
94 "setRenderFont: list full can't add font to widget 0x%lx\n",
98 /* we found an empty spot and need to malloc storage... */
99 tpd->renderTermFonts[i] = (TermFont *) malloc(sizeof(TermFont));
100 /* we don't need to initialize it as we will do a copy below... */
103 (void) memcpy(tpd->renderTermFonts[i], termFont, sizeof(TermFont));
104 if ((0 == tpd->cellWidth) || ('@' == termFont->id)) {
105 tpd->cellWidth = termFont->width;
106 tpd->cellHeight = termFont->height;
108 tw->term.widthInc = termFont->width;
109 tw->term.heightInc = termFont->height;
110 tw->term.ascent = termFont->ascent;
116 _DtTermPrimBell(Widget w)
118 DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
119 struct termData *tpd = tw->term.tpd;
122 if (tw->term.visualBell) {
123 /* the speed of this operation is not critical, so we will just
124 * use the standard text rendering GC and restore it after we
127 if (tpd->renderGC.foreground !=
128 (tw->primitive.foreground ^ tw->core.background_pixel)) {
129 tpd->renderGC.foreground =
130 tw->primitive.foreground ^ tw->core.background_pixel;
131 (void) XSetForeground(XtDisplay(w), tpd->renderGC.gc,
132 tpd->renderGC.foreground);
134 (void) XSetFunction(XtDisplay(w), tpd->renderGC.gc, GXxor);
135 for (i = 0; i < 2; i++) {
136 (void) XFillRectangle(XtDisplay(w), /* Display */
137 XtWindow(w), /* Drawable */
138 tpd->renderGC.gc, /* GC */
139 tpd->offsetX, /* x */
140 tpd->offsetY, /* y */
141 tw->term.columns * tpd->cellWidth,
143 tw->term.rows * tpd->cellHeight);
145 (void) XSync(XtDisplay(w), 0);
147 /* restore the GC... */
148 (void) XSetFunction(XtDisplay(w), tpd->renderGC.gc, GXcopy);
150 (void) XBell(XtDisplay(w), 0);
155 _DtTermPrimRefreshText(Widget w, short startColumn, short startRow,
156 short endColumn, short endRow)
158 DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
159 struct termData *tpd = tw->term.tpd;
160 TermBuffer tBuffer = tpd->termBuffer;
162 unsigned char *linePtr;
164 int currentColorPair = 0;
165 int currentVideo = 0;
166 short chunkStartColumn;
168 enhValues enhancements;
171 unsigned long valueMask;
174 TermEnhInfoRec enhInfo;
175 Boolean checkSelection = False;
178 XmTextPosition begin, end;
180 DebugF('t', 0, fprintf(stderr, ">>_DtTermPrimRefreshText() starting\n"));
181 DebugF('t', 0, fprintf(stderr,
182 ">>_DtTermPrimRefreshText() startCol=%hd startRow=%hd endCol=%hd endRow=%hd\n",
183 startColumn, startRow, endColumn, endRow));
185 if (tpd->mbCurMax > 1)
187 _DtTermPrimRefreshTextWc(w, startColumn, startRow, endColumn, endRow);
191 /* clip start/end x/y... */
192 if (startColumn <= 0)
196 if (endColumn >= tw->term.columns)
197 endColumn = tw->term.columns - 1;
198 if (endRow >= tw->term.rows)
199 endRow = tw->term.rows - 1;
202 ** don't display if we are in jump scroll and in the process
203 ** of scrolling and inside the scroll region...
205 if (tw->term.jumpScroll && tpd->scroll.jump.scrolled) {
206 /* set all the scrollRefreshRows flags... */
207 for (; startRow <= endRow; startRow++) {
208 tpd->scrollRefreshRows[startRow] = True;
213 ">>_DtTermPrimRefreshText() jump scroll in progress, no render\n"));
217 if (!tpd->renderGC.gc) {
218 /* get a drawImageString GC... */
222 /***********************************************************
225 /* set the GC fg and bg... */
226 values.foreground = tw->term.reverseVideo ?
227 tw->core.background_pixel : tw->primitive.foreground;
228 values.background = tw->term.reverseVideo ?
229 tw->primitive.foreground : tw->core.background_pixel;
231 tpd->renderGC.gc = XCreateGC(XtDisplay(w), XtWindow(w),
232 GCForeground | GCBackground, &values);
234 /* set the GC cache values... */
235 tpd->renderGC.foreground = values.foreground;
236 tpd->renderGC.background = values.background;
237 tpd->renderGC.fid = (Font) 0;
239 /***********************************************************
242 values.foreground = tw->term.reverseVideo ?
243 tw->primitive.foreground : tw->core.background_pixel;
244 values.background = tw->term.reverseVideo ?
245 tw->core.background_pixel : tw->primitive.foreground;
247 tpd->renderReverseGC.gc = XCreateGC(XtDisplay(w), XtWindow(w),
248 GCForeground | GCBackground, &values);
250 /* set the GC cache values... */
251 tpd->renderReverseGC.foreground = values.foreground;
252 tpd->renderReverseGC.background = values.background;
253 tpd->renderReverseGC.fid = (Font) 0;
255 /***********************************************************
258 values.foreground = tw->term.reverseVideo ?
259 tw->primitive.foreground : tw->core.background_pixel;
260 values.background = tw->term.reverseVideo ?
261 tw->core.background_pixel : tw->primitive.foreground;
263 tpd->clearGC.gc = XCreateGC(XtDisplay(w), XtWindow(w),
264 GCForeground | GCBackground, &values);
266 /* set the GC cache values... */
267 tpd->clearGC.foreground = values.foreground;
268 tpd->clearGC.background = values.background;
269 tpd->clearGC.fid = (Font) 0;
272 #ifdef SUN_MOTIF_PERF
273 /* use the clear GC... */
274 gc = tpd->clearGC.gc;
276 valueMask = (unsigned long) 0;
277 if (tpd->clearGC.foreground !=
278 tw->term.reverseVideo ?
279 tw->core.background_pixel : tw->primitive.foreground) {
281 tw->term.reverseVideo ?
282 tw->primitive.foreground :
283 tw->core.background_pixel;
284 tpd->clearGC.foreground = values.foreground;
285 valueMask |= GCForeground;
287 if (tpd->clearGC.background !=
288 tw->term.reverseVideo ?
289 tw->primitive.foreground :
290 tw->core.background_pixel) {
292 tw->term.reverseVideo ?
293 tw->core.background_pixel :
294 tw->primitive.foreground;
295 tpd->clearGC.background = values.background;
296 valueMask |= GCBackground;
299 (void) XChangeGC(XtDisplay(w), tpd->clearGC.gc,
302 (void) XFillRectangle(XtDisplay(w), /* Display */
303 XtWindow(w), /* Drawable */
305 startColumn * tpd->cellWidth + tpd->offsetX,
307 startRow * tpd->cellHeight + tpd->offsetY,
309 (endColumn - startColumn + 1) * tpd->cellWidth,
311 (endRow - startRow + 1) * tpd->cellHeight);
313 #endif /* SUN_MOTIF_PERF */
315 for (; startRow <= endRow; startRow++) {
316 /* if we are refreshing a full line, then we can clear the
317 * scrollRefreshRows flag for this line...
319 if ((startColumn == 0) && (endColumn >= tw->term.columns - 1)) {
320 tpd->scrollRefreshRows[startRow] = False;
323 lineNum = startRow + tpd->topRow;
324 if (!tw->term.jumpScroll && tpd->scroll.nojump.pendingScroll) {
325 if (lineNum != tpd->scrollLockBottomRow)
327 lineNum -= tpd->scroll.nojump.pendingScrollLines;
331 /* are we in the selected area?... */
332 if (tpd->useHistoryBuffer)
334 if (_DtTermPrimSelectGetSelection(w, &begin, &end) &&
336 (lineNum >= (begin / (tpd->selectInfo->columns + 1)) -
337 tpd->lastUsedHistoryRow) &&
338 (lineNum <= (end / (tpd->selectInfo->columns + 1)) -
339 tpd->lastUsedHistoryRow)) {
340 checkSelection = True;
342 checkSelection = False;
347 if (_DtTermPrimSelectGetSelection(w, &begin, &end) &&
349 (lineNum >= (begin / (tpd->selectInfo->columns + 1))) &&
350 (lineNum <= (end / (tpd->selectInfo->columns + 1)))) {
351 checkSelection = True;
353 checkSelection = False;
357 chunkStartColumn = startColumn;
358 if (startColumn > endColumn) {
359 /* nothing to render on this line... */
363 if (lineNum >= tpd->lastUsedRow) {
364 /* we are pointing to empty screen space below the last used
365 * line of the display...
369 } else if (lineNum < 0) {
370 if ((tpd->useHistoryBuffer) &&
371 (-lineNum <= tpd->lastUsedHistoryRow)) {
372 /* get a line out of the history buffer... */
373 lineWidth = MAX(0, MIN(endColumn - startColumn + 1,
374 _DtTermPrimBufferGetLineWidth(tpd->historyBuffer,
375 tpd->lastUsedHistoryRow + lineNum) - startColumn));
377 _DtTermPrimBufferGetCharacterPointer(tpd->historyBuffer,
378 tpd->lastUsedHistoryRow + lineNum, startColumn);
380 /* we are above the history buffer. Should not happen, but...
386 /* get the line width and a pointer to the data... */
387 lineWidth = MAX(0, MIN(endColumn - startColumn + 1,
388 _DtTermPrimBufferGetLineWidth(tBuffer, lineNum) -
390 linePtr = _DtTermPrimBufferGetCharacterPointer(tBuffer, lineNum,
394 while (lineWidth > 0) {
395 /* get the enhancement values for the first chunk of the
399 (void) _DtTermPrimBufferGetEnhancement(tBuffer,
402 chunkStartColumn, /* col */
403 &enhancements, /* enhancements */
404 &chunkWidth, /* width */
405 countAll); /* countWhich */
407 /* get it from the history buffer... */
408 (void) _DtTermPrimBufferGetEnhancement(tpd->historyBuffer,
410 tpd->lastUsedHistoryRow + lineNum,
412 chunkStartColumn, /* col */
413 &enhancements, /* enhancements */
414 &chunkWidth, /* width */
415 countAll); /* countWhich */
418 /* clip chunkWidth... */
419 if (chunkWidth > lineWidth)
420 chunkWidth = lineWidth;
422 /* set reasonable defaults for our render info... */
423 enhInfo.fg = tw->primitive.foreground;
424 enhInfo.bg = tw->core.background_pixel;
425 enhInfo.font = tpd->defaultTermFont;
426 enhInfo.flags = (unsigned long) 0;
428 /* set our font and color from the enhancements... */
429 if (ENH_PROC(tBuffer)) {
430 (void) (*(ENH_PROC(tBuffer)))(w, enhancements, &enhInfo);
433 /* if we are in reverse video mode... */
434 if (tw->term.reverseVideo) {
435 /* flip fg and bg... */
436 tmpPixel = enhInfo.fg;
437 enhInfo.fg = enhInfo.bg;
438 enhInfo.bg = tmpPixel;
441 /* are we in the selection area?... */
442 if (checkSelection &&
443 _DtTermPrimSelectIsInSelection(w, lineNum, chunkStartColumn,
444 chunkWidth, &chunkWidth)) {
446 /* flip fg and bg... */
447 tmpPixel = enhInfo.fg;
448 enhInfo.fg = enhInfo.bg;
449 enhInfo.bg = tmpPixel;
452 /* if secure, we will use a XFillRectangle, and we need
453 * foreground set to the background...
456 if (TermIS_SECURE(enhInfo.flags)) {
457 /* render secure video locally...
459 /* set the renderReverseGC... */
460 valueMask = (unsigned long) 0;
461 if (tpd->renderReverseGC.foreground != enhInfo.bg) {
462 tpd->renderReverseGC.foreground = enhInfo.bg;
463 values.foreground = enhInfo.bg;
464 valueMask |= GCForeground;
467 (void) XChangeGC(XtDisplay(w), tpd->renderReverseGC.gc,
470 (void) XFillRectangle(XtDisplay(w),
472 tpd->renderReverseGC.gc,
473 chunkStartColumn * tpd->cellWidth + tpd->offsetX,
474 startRow * tpd->cellHeight + tpd->offsetY,
475 tpd->cellWidth * chunkWidth,
478 /* underline as well... */
479 if (TermIS_UNDERLINE(enhInfo.flags)) {
480 valueMask = (unsigned long) 0;
481 if (tpd->renderGC.foreground != enhInfo.fg) {
482 tpd->renderGC.foreground = enhInfo.fg;
483 values.foreground = enhInfo.fg;
484 valueMask |= GCForeground;
487 (void) XChangeGC(XtDisplay(w), tpd->renderGC.gc,
490 (void) XDrawLine(XtDisplay(w),
492 XtWindow(w), /* Drawable */
493 tpd->renderGC.gc, /* GC */
494 chunkStartColumn * tpd->cellWidth + tpd->offsetX,
496 startRow * tpd->cellHeight + tpd->offsetY +
499 (chunkStartColumn + chunkWidth) * tpd->cellWidth +
500 tpd->offsetX, /* X2 */
501 startRow * tpd->cellHeight + tpd->offsetY +
502 tpd->cellHeight - 1);
506 (void) _DtTermPrimRenderText(
508 enhInfo.font, /* TermFont */
509 enhInfo.fg, /* fg Pixel */
510 enhInfo.bg, /* bg Pixel */
511 enhInfo.flags, /* flags */
512 chunkStartColumn * tpd->cellWidth + tpd->offsetX,
514 startRow * tpd->cellHeight + tpd->offsetY,
516 linePtr, /* string */
517 chunkWidth); /* width */
519 chunkStartColumn += chunkWidth;
520 lineWidth -= chunkWidth;
521 linePtr += chunkWidth;
524 /* clear any extra space in the line. chunkStartColumn now points to
525 * the end of the line, and lineWidth == 0...
527 while (endColumn - chunkStartColumn >= 0) {
528 chunkWidth = endColumn - chunkStartColumn + 1;
529 if (checkSelection &&
530 _DtTermPrimSelectIsInSelection(w, lineNum, chunkStartColumn,
531 chunkWidth, &chunkWidth)) {
532 /* use the render gc set to the fg color... */
533 gc = tpd->renderReverseGC.gc;
535 valueMask = (unsigned long) 0;
536 if (tpd->renderReverseGC.foreground !=
537 tw->term.reverseVideo ?
538 tw->core.background_pixel : tw->primitive.foreground) {
540 tw->term.reverseVideo ?
541 tw->core.background_pixel :
542 tw->primitive.foreground;
543 tpd->renderReverseGC.foreground = values.foreground;
544 valueMask |= GCForeground;
547 (void) XChangeGC(XtDisplay(w), tpd->renderReverseGC.gc,
550 #ifndef SUN_MOTIF_PERF
552 /* use the clear GC... */
553 gc = tpd->clearGC.gc;
555 valueMask = (unsigned long) 0;
556 if (tpd->clearGC.foreground !=
557 tw->term.reverseVideo ?
558 tw->core.background_pixel : tw->primitive.foreground) {
560 tw->term.reverseVideo ?
561 tw->primitive.foreground :
562 tw->core.background_pixel;
563 tpd->clearGC.foreground = values.foreground;
564 valueMask |= GCForeground;
566 if (tpd->clearGC.background !=
567 tw->term.reverseVideo ?
568 tw->primitive.foreground :
569 tw->core.background_pixel) {
571 tw->term.reverseVideo ?
572 tw->core.background_pixel :
573 tw->primitive.foreground;
574 tpd->clearGC.background = values.background;
575 valueMask |= GCBackground;
578 (void) XChangeGC(XtDisplay(w), tpd->clearGC.gc,
582 #endif /* not SUN_MOTIF_PERF */
584 if (isDebugFSet('t', 1)) {
588 /* Fill in the clear area so we can see what is going to
591 (void) XFillRectangle(XtDisplay(w),
594 chunkStartColumn * tpd->cellWidth + tpd->offsetX,
595 startRow * tpd->cellHeight + tpd->offsetY,
596 chunkWidth * tpd->cellWidth,
598 (void) XSync(XtDisplay(w), False);
599 (void) shortSleep(100000);
601 (void) XFillRectangle(XtDisplay(w),
603 XtWindow(w), /* Drawable */
605 chunkStartColumn * tpd->cellWidth + tpd->offsetX,
607 startRow * tpd->cellHeight + tpd->offsetY,
609 chunkWidth * tpd->cellWidth,
611 tpd->cellHeight); /* height */
612 #ifdef SUN_MOTIF_PERF
614 #endif /* SUN_MOTIF_PERF */
615 chunkStartColumn += chunkWidth;
618 DebugF('t', 0, fprintf(stderr, ">>_DtTermPrimRefreshText() finished\n"));
622 /**************************************************************************
624 * _DtTermPrimExposeText(): expose (refresh) the text in rectangular area
627 * Widget w: widget to expose
628 * int x: starting x pixel in window
629 * int y: starting y pixel in window
630 * int width: width in pixels
631 * int height: height in pixels
632 * Boolean isExposeEvent: true, deal with copyarea / expose overlap
638 * This function will redisplay the text in a rectangular exposure
639 * area. The x, y, width, and height are in absolute pixels. The
640 * internal x and y offsets will be accounted for, and the minimum
641 * number of characters will be displayed. The algorithm has been
642 * tuned (somewhat) and no longer exposes an extra character at the
643 * end of the refresh lines and an extra line at the bottom of the
644 * refresh area. This can be verified by using the 't' and 'e'
649 _DtTermPrimExposeText(Widget w, int x, int y, int width, int height,
650 Boolean isExposeEvent)
652 struct termData *tpd = ((DtTermPrimitiveWidget) w)->term.tpd;
654 DebugF('e', 0, fprintf(stderr, ">>exposeText() starting\n"));
655 DebugF('e', 0, fprintf(stderr,
656 ">>exposeText() startX=%d startY=%d width=%d height=%d\n",
657 x, y, width, height));
658 DebugF('e', 0, fprintf(stderr,
659 ">> offsetX=%d offsetY=%d cellHeight=%d cellWidth=%d\n",
660 tpd->offsetX, tpd->offsetY, tpd->cellHeight, tpd->cellWidth));
662 /* The following "hack" takes care of the problem of an exposure event
663 * from the server and a copy area from the client crossing. The
664 * combination of these two events can cause a race condition which
665 * manifests itself by leaving a hole in the terminal window. What
668 * A window is partially obscured. The terminal emulator does a copy
669 * area (scroll) which includes part of obscured area. Before the
670 * server processes the copy area, the window is unobscured, and the
671 * server sends an exposure event back to the client.
673 * - The window is partially obscured.
675 * - The terminal emulator does a copy area (scroll) which includes a
676 * portion of the obscured area. Normally, the server will generate
677 * a graphics exposure event for the obscured portion that it can't
678 * copy which will allow the terminal emulator to update the area.
680 * - Before the server receives the copy area request, the server
681 * unobscures the window and sends an exposure event for the exposed
682 * area. (This is where the hack comes into play and refreshes the
683 * scrolled portion of this area (and possibly some extra space as
686 * - The server processes the copy area. Since the area in question is
687 * no longer obscured, the server will copy blank space and not
688 * generate a graphics exposure event.
690 * - The terminal emulator processes the exposure event and refreshes
691 * the exposed area. (This is the hack extends the exposure to cover
694 * - You now have a blank chunk of the terminal window where the
695 * obscured area was scrolled (without the hack).
697 * Our fix is similar to the one used in xterm. The Motif text widget
698 * uses a more brute force method and simply extends the exposure event
699 * to the full height (or width) of the screen in the direction of the
702 if (isExposeEvent && tpd->scrollInProgress) {
708 bothX1 = MAX(tpd->scrollSrcX, x);
709 bothY1 = MAX(tpd->scrollSrcY, y);
710 bothX2 = MIN(tpd->scrollSrcX + tpd->scrollWidth, x + width - 1);
711 bothY2 = MIN(tpd->scrollSrcY + tpd->scrollHeight, y + height - 1);
713 if ((bothX2 > bothX1) && (bothY2 > bothY1)) {
714 (void) _DtTermPrimRefreshText(w,
715 (x - tpd->offsetX + tpd->scrollDestX - tpd->scrollSrcX) /
717 (y - tpd->offsetY + tpd->scrollDestY - tpd->scrollSrcY) /
719 (x + width - 1 - tpd->offsetX + tpd->scrollDestX -
720 tpd->scrollSrcX) / tpd->cellWidth,
721 (y + height - 1 - tpd->offsetY + tpd->scrollDestY -
722 tpd->scrollSrcY) / tpd->cellHeight);
726 /* render the text... */
727 DebugF('t', 0, fprintf(stderr, ">>exposeText() calling _DtTermPrimRefreshText()\n"));
728 (void) _DtTermPrimRefreshText(w, (x - tpd->offsetX) / tpd->cellWidth,
729 (y - tpd->offsetY) / tpd->cellHeight,
730 (x + width - 1 - tpd->offsetX) / tpd->cellWidth,
731 (y + height - 1 - tpd->offsetY) / tpd->cellHeight);
733 DebugF('e', 0, fprintf(stderr, ">>exposeText() finished\n"));
737 _DtTermPrimFillScreenGap(Widget w)
739 DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
740 struct termData *tpd = tw->term.tpd;
741 TermBuffer tBuffer = tpd->termBuffer;
744 int historyLinesNeeded;
746 TermLineSelection selectionFlag;
748 /* before we insert any text, we need to insure that the cursor is
749 * on a valid buffer row. If not, we will need to fill in any gap
750 * between the lastUsedRow and the current cursor postion with 0
751 * width lines, and get a buffer row for the current cursor row...
753 if (tpd->topRow + tpd->cursorRow >= tpd->lastUsedRow) {
754 /* if there are lines left in the buffer, just use them...
756 /* this does not effect the position of the selection... */
757 for (; (tpd->lastUsedRow < tpd->bufferRows) &&
758 (tpd->topRow + tpd->cursorRow >= tpd->lastUsedRow); ) {
759 /* all we need to do is clear the line... */
760 _DtTermPrimBufferClearLine(tBuffer, tpd->lastUsedRow++, 0);
763 /* if we still need more lines, then we will need to steal from
764 * the top of the buffer...
766 if (tpd->topRow + tpd->cursorRow >= tpd->lastUsedRow) {
767 if (tpd->memoryLockMode == SCROLL_LOCKprotect) {
768 if (!tpd->warningDialogMapped) {
769 (void) _DtTermPrimWarningDialog(w,
770 GETMESSAGE(NL_SETN_PrimRend, 1,"MEMORY FULL\nPress OK to clear"));
773 /* figure out how many lines are needed... */
774 linesNeeded = tpd->topRow + tpd->cursorRow -
775 (tpd->lastUsedRow - 1);
777 /* before we drop them off of the top, if we are
778 * using a history buffer, we need to transfer them
779 * into the history buffer...
781 if (tpd->useHistoryBuffer &&
782 (tpd->scrollLockTopRow == 0) &&
783 (tpd->scrollLockBottomRow >= (tw->term.rows - 1))) {
784 /* do we have enough lines in the history buffer, or
785 * do we need to steal some from the top?...
787 historyLinesNeeded = linesNeeded -
788 (tpd->historyBufferRows - tpd->lastUsedHistoryRow);
789 if ((historyLinesNeeded > 0) &&
790 (tpd->historyBufferRows > 0)) {
791 /* take them from the top... */
792 /* we are effectively deleting the lines from the
793 * top of the combined buffers...
795 (void) _DtTermPrimSelectDeleteLines(w, 0,
797 /* move the lines from the top to the bottom... */
798 (void) _DtTermPrimBufferInsertLineFromTB(
799 tpd->historyBuffer, tpd->historyBufferRows - 1,
800 historyLinesNeeded, insertFromTop);
801 /* the lines are now freed, adjust the used count... */
802 tpd->lastUsedHistoryRow -= historyLinesNeeded;
803 if (tpd->lastUsedHistoryRow < 0) {
804 historyLinesNeeded += tpd->lastUsedHistoryRow;
805 tpd->lastUsedHistoryRow = 0;
809 /* copy the lines over... */
810 for (i1 = 0; (i1 < linesNeeded) &&
811 (tpd->lastUsedHistoryRow < tpd->historyBufferRows);
815 termChar *overflowChars;
818 /* get the line from the active buffer... */
819 length = _DtTermPrimBufferGetLineLength(tBuffer,
823 ** stuff it into the history buffer...
824 ** (but only if there is something to copy)
827 unsigned char eIndex;
830 enhValue *eValues = (enhValue *)NULL;
832 overflowChars = (termChar *) XtMalloc(BUFSIZ * sizeof (termChar));
833 c1 = _DtTermPrimBufferGetCharacterPointer(
836 /* Perpetuate the enhancements. */
837 for (eCol = 0; eCol < length; eCol += eCount)
839 if (_DtTermPrimBufferGetEnhancement(tBuffer,
843 if ((eValues == (enhValue *)NULL) ||
848 eIndex < NUM_ENH_FIELDS(tBuffer);
851 _DtTermPrimBufferSetEnhancement(
853 tpd->lastUsedHistoryRow,
854 eCol, eIndex, eValues[eIndex]);
857 (void) _DtTermPrimBufferInsert(
859 tpd->lastUsedHistoryRow,
861 False, &overflowChars,
869 /* Clear out enhancement values if necessary */
870 if (eValues != (enhValue *)NULL)
873 eIndex < NUM_ENH_FIELDS(tBuffer);
876 _DtTermPrimBufferSetEnhancement(
878 tpd->lastUsedHistoryRow,
882 (void) _DtTermPrimBufferInsert(
884 tpd->lastUsedHistoryRow,
887 False, &overflowChars,
891 (void) XtFree((char *) overflowChars);
893 (void) _DtTermPrimBufferClearLine(
895 tpd->lastUsedHistoryRow, 0);
897 /* perpetuate the state of the wrap flag... */
898 (void) _DtTermPrimBufferSetLineWrapFlag(
900 tpd->lastUsedHistoryRow,
901 _DtTermPrimBufferTestLineWrapFlag(tBuffer, i1));
902 /* perpetuate the state of the inSelection flag...
906 _DtTermPrimBufferGetInSelectionFlag(
908 (void) _DtTermPrimBufferSetInSelectionFlag(
910 tpd->lastUsedHistoryRow,
913 /* clear the inselection flag before we scroll
914 * the lines off or we will end up releasing the
917 (void) _DtTermPrimBufferSetInSelectionFlag(
918 tBuffer, i1, (TermLineSelection) 0);
919 (void) tpd->lastUsedHistoryRow++;
920 (void) linesCopied++;
924 /* take them from the top. If we are about to take lines
925 * from the top without first copying them into the
926 * history buffer, we need to adjust the selection
929 if (linesNeeded > linesCopied) {
930 (void) _DtTermPrimSelectDeleteLines(w,
931 tpd->lastUsedHistoryRow + linesCopied,
932 linesNeeded - linesCopied);
934 (void) _DtTermPrimBufferInsertLineFromTB(tBuffer,
936 linesNeeded, insertFromTop);
937 /* adjust everything... */
938 tpd->topRow -= linesNeeded;
945 DoInsert(Widget w, unsigned char *buffer, int length, Boolean *wrapped)
947 DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
948 struct termData *tpd = tw->term.tpd;
949 TermBuffer tBuffer = tpd->termBuffer;
952 termChar *returnChars;
955 /* before we insert any text, we need to insure that the cursor is on
956 * a valid buffer row...
958 if (tpd->topRow + tpd->cursorRow >= tpd->lastUsedRow) {
959 (void) _DtTermPrimFillScreenGap(w);
962 /* insert the text... */
963 returnChars = (termChar *) XtMalloc(BUFSIZ * sizeof (termChar));
964 newWidth = _DtTermPrimBufferInsert(tBuffer, /* TermBuffer */
965 tpd->topRow + tpd->cursorRow, /* row */
966 tpd->cursorColumn, /* column */
967 (termChar *) buffer, /* newChars */
968 length, /* numChars */
969 tpd->insertCharMode != DtTERM_INSERT_CHAR_OFF,
971 &returnChars, /* return char ptr */
972 &returnCount); /* return count ptr */
974 if ((tpd->insertCharMode != DtTERM_INSERT_CHAR_ON_WRAP) || (returnCount <= 0)) {
975 (void) XtFree((char *) returnChars);
979 /* we are in insert char mode with wrap and we wrapped text off of
984 /* wrap the inserted characters into the following line... */
985 if (tpd->topRow + tpd->cursorRow + 1 >= tpd->lastUsedRow) {
986 /* fake cursorRow... */
987 (void) tpd->cursorRow++;
988 (void) _DtTermPrimFillScreenGap(w);
989 (void) tpd->cursorRow--;
992 /* we will allocate a temporary buffer so we don't have to worry
993 * about _DtTermPrimBufferInsert tromping over its overflow buffer...
995 length = returnCount;
996 buffer = (unsigned char *) XtMalloc(length);
997 (void) memcpy(buffer, returnChars, length);
999 /* insert the text into the next line... */
1000 newWidth = _DtTermPrimBufferInsert(tBuffer, /* TermBuffer */
1001 tpd->topRow + tpd->cursorRow + 1,
1004 (termChar *) buffer, /* newChars */
1005 length, /* numChars */
1006 tpd->insertCharMode != DtTERM_INSERT_CHAR_OFF,
1008 &returnChars, /* return char ptr */
1009 &returnCount); /* return count ptr */
1011 (void) XtFree((char *) buffer);
1012 (void) XtFree((char *) returnChars);
1017 _DtTermPrimInsertText(Widget w, unsigned char *buffer, int length)
1019 DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
1020 struct termData *tpd = tw->term.tpd;
1021 TermBuffer tBuffer = tpd->termBuffer;
1026 short insertCharCount;
1028 Boolean needToRender = False;
1029 Boolean wrapped = False;
1032 if (tpd->mbCurMax > 1)
1042 ** It would be nice if the calling function supplied us with a count
1043 ** of the number of mb characters in the buffer, then we wouldn't
1044 ** have to count them again.
1047 ** we could use this if the multi-byte buffer was null terminated
1049 wcBufferLen = mbstowcs((wchar_t *)NULL, (char *)buffer, length);
1052 pmb = (char *)buffer;
1054 ** we should never need more than length * sizeof(wchar_t)
1055 ** bytes to store the wide char equivalent of the incoming mb string
1057 wcBuffer = (wchar_t *)XtMalloc(length * sizeof(wchar_t));
1062 switch (mbLen = mbtowc(pwc, pmb, MIN(((int)MB_CUR_MAX), length - i)))
1065 if ((int)MB_CUR_MAX <= length - i) {
1066 /* we have a bogus multi-byte character. Throw away
1067 * the first byte and rescan (TM 12/14/93)...
1070 ** in this case, we move the remaining length - i - 1
1071 ** bytes one byte to the left (to overwrite the bogus
1074 memmove(pmb, pmb + 1, length - i - 1);
1081 ** treat null character same as any other character...
1092 i = _DtTermPrimInsertTextWc(w, wcBuffer, wcBufferLen);
1093 /* convert back from a wide character count to a multibyte
1094 * character count...
1096 pmb = (char *)buffer;
1100 while (i < wcBufferLen)
1102 switch (mbLen = mblen(pmb, MIN(((int)MB_CUR_MAX), length - i)))
1107 ** treat null character same as any other character...
1115 XtFree((char *)wcBuffer);
1116 return(pmb - (char *) buffer);
1119 /* turn off the cursor... */
1120 if (CURSORoff != tpd->cursorState) {
1121 (void) _DtTermPrimCursorOff(w);
1124 /* we support two different types of autowrap. The HP style one is where
1125 * you display the character and then wrap if you are at the end of the
1126 * line. The ANSI style one is where you insert the character at the end
1127 * of the line and don't autowrap until you try to insert another
1131 renderStartX = tpd->cursorColumn;
1132 renderEndX = tpd->cursorColumn;
1133 insertStartX = tpd->cursorColumn;
1134 insertCharCount = 0;
1136 for (i = 0; (i < length) && tpd->ptyInputId; i++) {
1137 /* the following code performs two functions. If we are in
1138 * autowrap, it performs a sanity check on the insert position.
1139 * if we are not in autowrap, it will insure that characters
1140 * inserted after the last position will replace the last
1143 if ((tpd->cursorColumn >= tw->term.columns) &&
1144 !(tpd->autoWrapRight && !tpd->wrapRightAfterInsert)) {
1145 /* blow away the previous character...
1147 tpd->cursorColumn = tw->term.columns - 1;
1148 renderStartX = MIN(renderStartX, tpd->cursorColumn);
1149 tpd->wrapState = WRAPpastRightMargin;
1152 /* for emulations that wrap after inserting the character, we
1153 * will insert the character and then check for wrap...
1155 if (tpd->wrapRightAfterInsert) {
1156 if (insertCharCount == 0) {
1159 (void) insertCharCount++;
1162 if (((tpd->cursorColumn + insertCharCount) >= tw->term.columns) ||
1163 ((tpd->cursorColumn + insertCharCount) ==
1164 tpd->rightMargin + 1)) {
1165 if (tpd->autoWrapRight) {
1166 /* perform an auto wrap...
1168 /* we need to insert any pending characters, and
1171 if (insertCharCount) {
1172 newWidth = DoInsert(w, &buffer[insertStartX],
1173 insertCharCount, &wrapped);
1174 tpd->cursorColumn += insertCharCount;
1175 insertCharCount = 0;
1176 if (tpd->insertCharMode != DtTERM_INSERT_CHAR_OFF) {
1177 renderEndX = newWidth;
1179 renderEndX = MAX(renderEndX, tpd->cursorColumn);
1181 needToRender = True;
1184 DebugF('t', 0, fprintf(stderr,
1185 ">>termInsertText() calling[2] _DtTermPrimRefreshText()\n"));
1186 (void) _DtTermPrimRefreshText(w, renderStartX, tpd->cursorRow,
1187 wrapped ? tw->term.columns : MAX(renderEndX, 0),
1189 if (wrapped && (tpd->cursorRow + 1 < tw->term.rows)) {
1190 (void) _DtTermPrimRefreshText(w, 0, tpd->cursorRow + 1,
1191 renderEndX, tpd->cursorRow + 1);
1194 needToRender = False;
1196 tpd->cursorColumn = tpd->leftMargin;
1197 tpd->wrapState = WRAPbetweenMargins;
1200 _DtTermPrimBufferSetLineWrapFlag(tBuffer,
1201 tpd->topRow + tpd->cursorRow,
1204 if (tpd->cursorRow == tpd->scrollLockBottomRow) {
1205 /* scroll at the bottom of the lock area... */
1206 (void) _DtTermPrimScrollText(w, 1);
1207 (void) _DtTermPrimFillScreenGap(w);
1208 } else if (++tpd->cursorRow >= tw->term.rows) {
1209 /* we are at the bottom row of the locked region.
1210 * Wrap to the beginning of this line...
1212 tpd->cursorRow = tw->term.rows - 1;
1214 /* we are not at the bottom row. We already have
1215 * wrapped down one line...
1219 renderStartX = tpd->cursorColumn;
1221 if (tpd->scroll.nojump.pendingScroll) {
1222 /* If we wrapped the screen, bail out now and we
1223 * will take care of this character when we
1224 * finish the scroll. If we are in wrap after,
1225 * then we need to skip past this character so
1226 * that it doesn't get processed twice...
1228 if (tpd->wrapRightAfterInsert)
1233 /* overwrite the last character(s) on the line... */
1234 if (insertCharCount > 0) {
1235 newWidth = DoInsert(w, &buffer[insertStartX],
1236 insertCharCount, &wrapped);
1237 tpd->cursorColumn += insertCharCount;
1238 insertCharCount = 0;
1239 if (tpd->insertCharMode != DtTERM_INSERT_CHAR_OFF) {
1240 renderEndX = newWidth;
1242 renderEndX = MAX(renderEndX, tpd->cursorColumn);
1244 needToRender = True;
1247 if (tpd->cursorColumn == tpd->rightMargin + 1)
1248 tpd->cursorColumn = tpd->rightMargin;
1250 tpd->cursorColumn = tw->term.columns - 1;
1254 /* for emulations that wrap before inserting the character, we
1255 * will insert the character and then check for wrap...
1257 if (!tpd->wrapRightAfterInsert) {
1258 /* before we insert any text, we need to insure that the
1259 * cursor is on a valid buffer row...
1261 if (insertCharCount == 0) {
1264 (void) insertCharCount++;
1267 /* insert and render any remaining text... */
1268 if (insertCharCount > 0) {
1269 newWidth = DoInsert(w, &buffer[insertStartX],
1270 insertCharCount, &wrapped);
1271 tpd->cursorColumn += insertCharCount;
1272 insertCharCount = 0;
1273 if (tpd->insertCharMode != DtTERM_INSERT_CHAR_OFF) {
1274 renderEndX = newWidth;
1276 renderEndX = MAX(renderEndX, tpd->cursorColumn);
1278 needToRender = True;
1281 renderEndX = MAX(renderEndX, tpd->cursorColumn);
1282 DebugF('t', 0, fprintf(stderr,
1283 ">>termInsertText() calling _DtTermPrimRefreshText()\n"));
1284 (void) _DtTermPrimRefreshText(w, renderStartX, tpd->cursorRow,
1285 wrapped ? tw->term.columns : MAX(renderEndX, 0),
1287 if (wrapped && (tpd->cursorRow + 1 < tw->term.rows)) {
1288 (void) _DtTermPrimRefreshText(w, 0, tpd->cursorRow + 1,
1289 renderEndX, tpd->cursorRow + 1);
1292 needToRender = False;
1301 unsigned char *buffer,
1303 unsigned char *mbPartialChar,
1304 int *mbPartialCharLen,
1306 unsigned char **dangleBuffer,
1307 int *dangleBufferLen
1310 /* malloc our dangle buffer... */
1311 *dangleBufferLen = bufferLen + *mbPartialCharLen - writeLen;
1312 *dangleBuffer = (unsigned char *) XtMalloc(*dangleBufferLen);
1314 /* copy over the unwritten part of the orignal buffer... */
1315 (void) memmove(*dangleBuffer, buffer + writeLen, bufferLen - writeLen);
1316 if (*mbPartialCharLen) {
1317 (void) memmove(*dangleBuffer + bufferLen - writeLen, mbPartialChar,
1319 *mbPartialCharLen = 0;
1325 _DtTermPrimParseInput
1328 unsigned char *buffer,
1330 unsigned char **dangleBuffer,
1331 int *dangleBufferLen
1334 DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
1335 struct termData *tpd = tw->term.tpd;
1336 DtTermPrimitiveClassPart *termClassPart = &(((DtTermPrimitiveClassRec *)
1337 (tw->core.widget_class))->term_primitive_class);
1338 TermBuffer tBuffer = tpd->termBuffer;
1341 short insertByteCount;
1343 Boolean turnCursorOn = False;
1344 unsigned char *tmpBuffer = (unsigned char *) 0;
1345 unsigned char mbChar[MB_LEN_MAX];
1347 static Boolean *preParseTable = (Boolean *) 0;
1349 *dangleBuffer = (unsigned char *) 0;
1351 insertByteCount = 0;
1353 /* initialize the preParseTable if necessary... */
1354 _DtTermProcessLock();
1355 if (!preParseTable) {
1356 preParseTable = (Boolean *) XtMalloc(256 * sizeof(Boolean));
1357 (void) memset(preParseTable, '\0', 256 * sizeof(Boolean));
1359 for (i = 0x00; i <= 0x1f; i++) {
1360 preParseTable[i] = True;
1362 for (i = 0x80; i <= 0x9f; i++) {
1363 preParseTable[i] = True;
1366 _DtTermProcessUnlock();
1368 /* check for partial multibyte character... */
1369 if (tpd->mbPartialCharLen > 0) {
1370 tmpBuffer = (unsigned char *) XtMalloc(len + tpd->mbPartialCharLen);
1371 (void) memcpy(tmpBuffer, tpd->mbPartialChar, tpd->mbPartialCharLen);
1372 (void) memcpy(tmpBuffer + tpd->mbPartialCharLen, buffer, len);
1374 len += tpd->mbPartialCharLen;
1375 tpd->mbPartialCharLen = 0;
1378 /* turn off the cursor... */
1379 if (CURSORoff != tpd->cursorState) {
1380 (void) _DtTermPrimCursorOff(w);
1381 turnCursorOn = True;
1385 for (i = 0; (i < len) && tpd->ptyInputId; ) {
1386 if (tpd->mbCurMax > 1) {
1388 mblen((char *) &buffer[i], MIN(((int)MB_CUR_MAX), len - i)))
1391 if ((int)MB_CUR_MAX <= len - i)
1393 /* we have a bogus multi-byte character. Throw away
1394 * the first byte and rescan (TM 12/14/93)...
1396 /* dump what we know we want to insert... */
1397 if (insertByteCount > 0) {
1398 returnLen = (*(termClassPart->term_insert_proc))(w,
1399 &buffer[insertStart], insertByteCount);
1400 if (returnLen != insertByteCount) {
1401 (void) buildDangleBuffer(buffer, len,
1403 &tpd->mbPartialCharLen,
1404 insertStart + returnLen,
1405 dangleBuffer, dangleBufferLen);
1407 insertByteCount = 0;
1410 insertByteCount = 0;
1412 /* skip over the bogus char's first byte... */
1417 /* we have a dangling partial multi-byte character... */
1418 (void) memmove(tpd->mbPartialChar, &buffer[i], len - i);
1419 tpd->mbPartialCharLen = len - i;
1420 /* remove the partial char from the buffer and adjust
1435 if (((mbCharLen == 1) && preParseTable[buffer[i]]) ||
1436 tpd->parserNotInStartState) {
1438 ** It's either a control code or we are not in the start state.
1439 ** If we have any text to insert, insert
1440 ** the text, display any added text, then send it down through
1443 if (insertByteCount > 0) {
1444 returnLen = (*(termClassPart->term_insert_proc))(w,
1445 &buffer[insertStart], insertByteCount);
1446 if (returnLen != insertByteCount) {
1447 (void) buildDangleBuffer(buffer, len,
1449 &tpd->mbPartialCharLen,
1450 insertStart + returnLen,
1451 dangleBuffer, dangleBufferLen);
1452 insertByteCount = 0;
1455 insertByteCount = 0;
1458 tpd->parserNotInStartState = _DtTermPrimParse(w, &buffer[i],
1463 /* queue up text to insert into the buffer... */
1464 insertByteCount += mbCharLen;
1469 if (insertByteCount > 0)
1471 returnLen = (*(termClassPart->term_insert_proc))(w,
1472 &buffer[insertStart],
1474 if (returnLen != insertByteCount)
1476 (void) buildDangleBuffer(buffer, len,
1478 &tpd->mbPartialCharLen,
1479 insertStart + returnLen,
1480 dangleBuffer, dangleBufferLen);
1486 ** insertByteCount <= 0, check to make sure we haven't
1487 ** already saved any remaining text in the dangleBuffer...
1489 if (i < len && !*dangleBuffer)
1491 (void) buildDangleBuffer(buffer, len,
1493 &tpd->mbPartialCharLen,
1494 i, dangleBuffer, dangleBufferLen);
1498 /* if we turned the cursor off, turn the cursor back on... */
1500 (void) _DtTermPrimCursorOn(w);
1504 (void) XtFree((char *) tmpBuffer);
1506 if (*dangleBuffer) {
1513 ** Pad the current line from the current end of line up to (and
1514 ** including) the current cursor column.
1517 _DtTermPrimRenderPadLine
1522 DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
1523 struct termData *tpd = tw->term.tpd;
1524 TermBuffer tBuffer = tpd->termBuffer;
1527 /* before we pad this line, we need to insure that cursor is on a
1528 * valid buffer row...
1530 (void) _DtTermPrimFillScreenGap(w);
1532 currentWidth = _DtTermPrimBufferGetLineWidth(tBuffer,
1533 tpd->topRow + tpd->cursorRow);
1534 if (tpd->cursorColumn >= currentWidth)
1537 ** Pad the line and refresh it.
1539 if (tpd->mbCurMax > 1) {
1540 _DtTermPrimBufferPadLineWc(tBuffer, tpd->topRow + tpd->cursorRow,
1541 tpd->cursorColumn + 1);
1543 _DtTermPrimBufferPadLine(tBuffer, tpd->topRow + tpd->cursorRow,
1544 tpd->cursorColumn + 1);
1546 _DtTermPrimRefreshText(w, currentWidth, tpd->cursorRow, tpd->cursorColumn,
1552 _DtTermPrimDestroyFont(
1557 if (font) (void) (*(font->destroyFunction))(w, font);
1562 _DtTermPrimRenderText(
1567 unsigned long flags,
1570 unsigned char *string,
1574 (void) (*(font->renderFunction))(w, font, fg, bg, flags, x, y, string, len);