File: util.cc

package info (click to toggle)
nitpic 0.1-9
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 212 kB
  • ctags: 298
  • sloc: cpp: 2,212; ansic: 408; asm: 144; makefile: 50; sh: 3
file content (240 lines) | stat: -rw-r--r-- 5,379 bytes parent folder | download | duplicates (11)
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
/*
 * util.cc -- various utility routines
 */
#include "picsim.hh"

void
redraw( void )
{
	ic_refresh( W.ic, NULL, NULL, NULL );
	info_refresh( W.info, NULL, NULL, NULL );
	instr_refresh( W.instr, NULL, NULL, NULL );
	reg_refresh( W.regs, NULL, NULL, NULL );
}

void
load_file( Widget w, XEvent *ev, String *prms, Cardinal *nprm )
{
	dialog( "Filename?", 1, "OK" );
}

static void		 dialog_format(char*text, XFontStruct*finfo, Dimension width);
static Widget	 dialog_box = None;
static Widget	 dialog_form;
static Widget	 dialog_text;
static Widget	*dialog_buttons = NULL;
static int		 dialog_nbuttons = 0;

static void
dialog_ok( Widget w, XtPointer closure, XtPointer call_data )
{
	int	 i;
	
	XtPopdown( dialog_box );

	XtUnrealizeWidget( dialog_box );

	XtUnmanageChildren( dialog_buttons, dialog_nbuttons );
	XtUnmanageChild( dialog_text );
	XtUnmanageChild( dialog_form );
	XtUnmanageChild( dialog_box );

	for (i = 0; i < dialog_nbuttons; i++) XtDestroyWidget( dialog_buttons[i] );

	XtFree( (char *)dialog_buttons );
	dialog_buttons = NULL;

	fprintf( stderr, "button %d\n", closure );
}

int
dialog( const char *text, int n, ... )
{
	va_list			 buttons;
	int				 i, j;
	Dimension		 bwidth;
	Dimension		 bheight;
	Dimension		 twidth;
	Dimension		 theight;
	int				 fgap;
	Widget			 tsink;
	Dimension		 width;
	Dimension		 height;
	Window			 root;
	Window			 child;
	int				 root_x, root_y;
	int				 win_x, win_y;
	unsigned int	 mask;
	char			*fmttext;
	XFontStruct		*finfo;

	fmttext = (char *)XtMalloc( strlen( text ) + 1 );
	strcpy( fmttext, text );

	va_start( buttons, n );

	if (dialog_box == None) {
		dialog_box = XtVaCreateWidget( "dialog",
									  overrideShellWidgetClass,
									  W.toplevel,
									  NULL );
		dialog_form = XtVaCreateWidget( "form", formWidgetClass,
									   dialog_box,
									   NULL );
		dialog_text = XtVaCreateWidget( "text", asciiTextWidgetClass,
									   dialog_form,
									   XtNautoFill, (XtPointer)True,
									   XtNscrollVertical,
									   (XtPointer)XawtextScrollWhenNeeded,
									   XtNeditType,
									   (XtPointer)XawtextRead,
									   XtNdisplayCaret,
									   (XtPointer)False,
									   XtNresizable,
									   (XtPointer)True,
									   NULL );
	}

	XawFormDoLayout( dialog_form, False );

	dialog_buttons = (Widget *)XtMalloc( n * sizeof(Widget) );

	for (i = 0; i < n; i++) {
		Widget	 b;
		char	*name;

		name = va_arg(buttons, char *);

		if (!i)
			b = XtVaCreateWidget( name, commandWidgetClass,
								 dialog_form,
								 XtNlabel, name,
								 XtNfromVert, dialog_text,
								 XtNresizable, (XtPointer)True,
								 NULL );
		else
			b = XtVaCreateWidget( name, commandWidgetClass,
								 dialog_form,
								 XtNlabel, name,
								 XtNfromVert, dialog_text,
								 XtNfromHoriz,
								 (XtPointer)dialog_buttons[i-1],
								 XtNresizable, (XtPointer)True,
								 NULL );
		
		XtAddCallback( b, "callback", dialog_ok, (XtPointer)i );
		dialog_buttons[i] = b;
	}

	dialog_nbuttons = n;

	XtVaGetValues( dialog_buttons[0],
				  XtNwidth, &bwidth,
				  XtNheight, &bheight,
				  NULL );

	XtVaGetValues( dialog_form,
				  XtNdefaultDistance, &fgap,
				  NULL );

	if (n == 1) {
		twidth = bwidth * 2;
		XtVaSetValues( dialog_buttons[0],
					  XtNhorizDistance, twidth / 4 + fgap,
					  NULL );
	} else {
		twidth = (bwidth + 4) * n + 4;
	}
	theight = twidth * 2 / 3;

	XtVaGetValues( dialog_text,
				  XtNtextSink, &tsink,
				  XtNfont, &finfo,
				  NULL );

	dialog_format( fmttext, finfo, twidth );

	for (i = 2, j = 0; fmttext[j]; j++) if (fmttext[j] == '\n') ++i;
	j = XawTextSinkMaxHeight( tsink, i );

	if (j < theight) {
		theight = j;
	} else {
		strcpy( fmttext, text );
		dialog_format( fmttext, finfo, twidth - 18 );
	}

	XtVaSetValues( dialog_text,
				  XtNwidth, twidth,
				  XtNheight, theight,
				  XtNstring, fmttext,
				  NULL );

	XtVaSetValues( dialog_form,
				  XtNwidth, twidth + fgap * 2,
				  XtNheight, theight + bheight + fgap * 3,
				  NULL );

	XawFormDoLayout( dialog_form, True );
	
	XtVaGetValues( dialog_form,
				  XtNwidth, &width,
				  XtNheight, &height,
				  NULL );

	XQueryPointer( XtDisplay(W.toplevel), XtWindow(W.toplevel),
				  &root, &child,
				  &root_x, &root_y, &win_x, &win_y, &mask );

	root_x = (root_x < width / 2) ? 0 : root_x - width / 2;
	root_y = (root_y < height - bheight / 2) ? 0 : root_y-height+bheight/2;

	XtVaSetValues( dialog_box,
				  XtNwidth, width,
				  XtNheight, height,
				  XtNx, root_x,
				  XtNy, root_y,
				  NULL );

	XtManageChild( dialog_text );
	XtManageChildren( dialog_buttons, dialog_nbuttons );
	XtManageChild( dialog_form );
	XtManageChild( dialog_box );
	
	XtRealizeWidget( dialog_box );

	XtPopup( dialog_box, XtGrabExclusive );

	XtFree( fmttext );

	return 0;
}

/*
 * format the text -- put in newlines whenever the current line gets
 * too long.
 */
static void
dialog_format( char *text, XFontStruct *finfo, Dimension width )
{
	int	 i, j, k;
	

	for (i = j = 0, k = 1; text[k]; ++k) {
		if (isspace(text[k])) {
			if (XTextWidth( finfo, text + i, k - i ) >= width) {
				if (isspace(text[j])) {
					text[j++] = '\n';
					i = j;
				} else {
					text[k] = '\n';
				}
			} else {
				j = k;
			}
			if (text[k] == '\n')  i = j = k + 1;
		}
	}
	if (XTextWidth( finfo, text + i, k - i ) >= width && isspace(text[j]))
		text[j] = '\n';
}