File: scroll.c

package info (click to toggle)
felt 3.06-9
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 17,512 kB
  • ctags: 7,946
  • sloc: ansic: 85,476; fortran: 3,614; yacc: 2,803; lex: 1,178; makefile: 305; sh: 302
file content (168 lines) | stat: -rw-r--r-- 4,779 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
/*
    This file is part of the FElt finite element analysis package.
    Copyright (C) 1993-2000 Jason I. Gobat and Darren C. Atkinson

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

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

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/************************************************************************
 * File:	scroll.c						*
 *									*
 * Description:	This file contains the public and private function	*
 *		and variable declarations for the scrollable text	*
 *		mechanism.						*
 ************************************************************************/

# include <stdio.h>
# include <X11/IntrinsicP.h>
# include <X11/StringDefs.h>
# include <X11/Xaw/TextP.h>
# include "scroll.h"
# include "util.h"


static String scroll_table =
"<KeyUp>:           ScrollText()\n\
 <ConfigureNotify>: ScrollText()";

static XtTranslations scroll_translations;


/************************************************************************
 * Function:	ScrollText						*
 *									*
 * Description:	Action procedure interface to ScrollToInsertionPoint().	*
 ************************************************************************/

static void ScrollText (w, event, params, num_params)
    Widget    w;
    XEvent   *event;
    String   *params;
    Cardinal *num_params;
{
    ScrollToInsertionPoint (w);
}


/************************************************************************
 * Function:	AddScrollableTextTranslations				*
 *									*
 * Description:	Adds translations to the specified text widget to	*
 *		enable a primitive character-based scrolling.		*
 ************************************************************************/

void AddScrollableTextTranslations (text)
    Widget text;
{
    static XtAppContext	app_context = NULL;
    static XtActionsRec	actions [ ] = {{"ScrollText", ScrollText}};


    /* Add the actions to the application context if necessary. */

    if (app_context == NULL) {
	app_context = XtWidgetToApplicationContext (text);
	XtAppAddActions (app_context, actions, XtNumber (actions));
	scroll_translations = XtParseTranslationTable (scroll_table);
    }

    XtVaSetValues (text, XtNrightMargin, 0, NULL);
    XtOverrideTranslations (text, scroll_translations);


    /* The SetValues really should do this for us. */

    ((TextWidget) text) -> text.margin.right = 0;
}


/************************************************************************
 * Function:	ScrollToInsertionPoint					*
 *									*
 * Description:	Scrolls the text so the insertion point is always	*
 *		visible.  The scrolling is accomplished by updating the	*
 *		display position of the string and is rather primitive.	*
 ************************************************************************/

void ScrollToInsertionPoint (text)
    Widget text;
{
    int		 ins_pos;
    int		 disp_pos;
    int		 length;
    String	 ptr;
    String	 value;
    Boolean	 caret;
    Position	 l_margin;
    Position	 r_margin;
    Dimension	 width;
    XFontStruct	*font;


    XtVaGetValues (text, XtNdisplayPosition, &disp_pos,
			 XtNinsertPosition,  &ins_pos,
			 XtNdisplayCaret,    &caret,
			 XtNrightMargin,     &r_margin,
			 XtNleftMargin,      &l_margin,
			 XtNstring,	     &value,
			 XtNwidth,	     &width,
			 XtNfont,	     &font, NULL);


    /* Check if the translations have been installed. */

    if (r_margin != 0)
	return;


    /* Scroll the text string to the right. */

    if (ins_pos <= disp_pos) {
	disp_pos = ins_pos - 1;
	if (disp_pos < 0)
	    disp_pos = 0;


    /* Scroll the text string to the left. */

    } else {
	width -= l_margin + r_margin;
	width -= GetTextWidth (font, "W", 1);	/* the widest character? */

	while (1) {
	    ptr = &value [disp_pos];
	    length = 0;

	    while (ptr [length] && GetTextWidth (font, ptr, length) < width)
		length ++;

	    if (ins_pos > disp_pos + length)
		disp_pos ++;
	    else
		break;
	}
    }


    /* Scroll to the new position, disabling the caret temporarily. */

    if (caret == True)
	XtVaSetValues (text, XtNdisplayCaret, False, NULL);

    XtVaSetValues (text, XtNdisplayPosition, disp_pos, NULL);

    if (caret == True)
	XtVaSetValues (text, XtNdisplayCaret, True, NULL);
}