summaryrefslogtreecommitdiff
path: root/interpret.c
blob: 3978ed332b25b0c061058dadd1d36b1f8822b682 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
** interpret.c
**
** interprets and executes lines in the Xgc syntax.
*/

#include <stdio.h>
#include <stdlib.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include "xgc.h"
#include "tile"


/* interpret(string)
** -----------------
** Takes string, which is a line written in the xgc syntax, figures
** out what it means, and passes the buck to the right procedure.
** That procedure gets called with feedback set to FALSE; interpret()
** is only called if the user is selecting things interactively.
**
** This procedure will go away when I can figure out how to make yacc
** and lex read from strings as well as files.
*/

void
interpret(const char *string)
{
  char word1[20], word2[80];
  int i;

  sscanf(string,"%s",word1);
  if (!strcmp(word1,"run")) run_test();

  else {
    sscanf(string,"%s %s",word1,word2);
    print_if_recording(string);

    /* So word1 is the first word on the line and word2 is the second.
       Now the fun begins... */
    
    if (!strcmp(word1,TestStuff.choice.text))  {
      for (i=0;i<NUM_TESTS;++i) {
	if (!strcmp(word2,(TestStuff.data)[i].text)) {
	  change_test((TestStuff.data)[i].code,FALSE);
	  break;
	}
      }
    }
    else if (!strcmp(word1,FunctionStuff.choice.text)) {
      for (i=0;i<NUM_FUNCTIONS;++i) {
	if (!strcmp(word2,(FunctionStuff.data)[i].text)) {
	  GC_change_function((FunctionStuff.data)[i].code,FALSE);
	  break;
	}
      }
    }
    else if (!strcmp(word1,LinestyleStuff.choice.text)) {
      for (i=0;i<NUM_LINESTYLES;++i) {
	if (!strcmp(word2,(LinestyleStuff.data)[i].text)) {
	  GC_change_linestyle((LinestyleStuff.data)[i].code,FALSE);
	  break;
	}
      }
    }
    else if (!strcmp(word1,"linewidth"))
      GC_change_linewidth(atoi(word2),FALSE);
    else if (!strcmp(word1,CapstyleStuff.choice.text)) {
      for (i=0;i<NUM_CAPSTYLES;++i) {
	if (!strcmp(word2,(CapstyleStuff.data)[i].text)) {
	  GC_change_capstyle((CapstyleStuff.data)[i].code,FALSE);
	  break;
	}
      }
    }
    else if (!strcmp(word1,JoinstyleStuff.choice.text)) {
      for (i=0;i<NUM_JOINSTYLES;++i) {
	if (!strcmp(word2,(JoinstyleStuff.data)[i].text)) {
	  GC_change_joinstyle((JoinstyleStuff.data)[i].code,FALSE);
	  break;
	}
      }
    }
    else if (!strcmp(word1,FillstyleStuff.choice.text)) {
      for (i=0;i<NUM_FILLSTYLES;++i) {
	if (!strcmp(word2,(FillstyleStuff.data)[i].text)) {
	  GC_change_fillstyle((FillstyleStuff.data)[i].code,FALSE);
	  break;
	}
      }
    }
    else if (!strcmp(word1,FillruleStuff.choice.text)) {
      for (i=0;i<NUM_FILLRULES;++i) {
	if (!strcmp(word2,(FillruleStuff.data)[i].text)) {
	  GC_change_fillrule((FillruleStuff.data)[i].code,FALSE);
	  break;
	}
      }
    }
    else if (!strcmp(word1,ArcmodeStuff.choice.text)) {
      for (i=0;i<NUM_ARCMODES;++i) {
	if (!strcmp(word2,(ArcmodeStuff.data)[i].text)) {
	  GC_change_arcmode((ArcmodeStuff.data)[i].code,FALSE);
	  break;
	}
      }
    }
    else if (!strcmp(word1,"planemask")) 
      GC_change_planemask((unsigned long) atoi(word2),FALSE);
    else if (!strcmp(word1,"dashlist"))
      GC_change_dashlist(atoi(word2),FALSE);
    else if (!strcmp(word1,"font"))
      GC_change_font(word2,FALSE);
    else if (!strcmp(word1,"foreground"))
      GC_change_foreground((unsigned long) atoi(word2),FALSE);
    else if (!strcmp(word1,"background"))
      GC_change_background((unsigned long) atoi(word2),FALSE);
    else if (!strcmp(word1,"percent"))
      change_percent(atoi(word2), FALSE);
    else fprintf(stderr,"Ack... %s %s\n",word1,word2);
  }
}

#ifdef notdef
void
interpret(const char *instring)
{
  FILE *inend;
  
  print_if_recording(instring);
  yyin = outend;
  inend = fdopen(fildes[1],"w");
  fprintf(inend,"%s",instring);
  fclose(inend);
  yyparse();
}
#endif

#define select_correct_button(which,number) \
  select_button(GCdescs[(which)],(number));

/* GC_change_blahzee(foo,feedback)
** ---------------------
** Changes the blahzee field in xgc's GC to foo.  If feedback is TRUE,
** changes the display to reflect this (makes it look like the user
** selected the button, or typed in the text, or whatever).
*/

void
GC_change_function(int function, Boolean feedback)
{
  XSetFunction(X.dpy,X.gc,function);
  X.gcv.function = function;
  if (feedback) select_correct_button(CFunction,function);
}

void
GC_change_foreground(unsigned long foreground, Boolean feedback)
{
  char text[40];

  XSetForeground(X.dpy,X.miscgc,foreground);
  XCopyPlane(X.dpy,X.stipple,X.tile,X.miscgc,0,0,tile_width,tile_height,0,0,1);
  XSetForeground(X.dpy,X.gc,foreground);
  X.gcv.foreground = foreground;
  XSetTile(X.dpy,X.gc,X.tile);
  XSetTile(X.dpy,X.miscgc,X.tile);
  if (feedback) {
    snprintf(text, sizeof text, "%lu",foreground);
    change_text(foregroundtext,text);
  }
}

void
GC_change_background(unsigned long background, Boolean feedback)
{
  char text[40];

  XSetBackground(X.dpy,X.miscgc,background);
  XCopyPlane(X.dpy,X.stipple,X.tile,X.miscgc,0,0,tile_width,tile_height,0,0,1);
  XSetBackground(X.dpy,X.gc,background);
  X.gcv.background = background;
  XSetTile(X.dpy,X.gc,X.tile);
  XSetTile(X.dpy,X.miscgc,X.tile);

  /* Update the background of the test window NOW. */

  XSetWindowBackground(X.dpy,XtWindow(test),background);
  XClearWindow(X.dpy,XtWindow(test));

  if (feedback) {
    snprintf(text, sizeof text, "%lu",background);
    change_text(backgroundtext,text);
  }
}

void
GC_change_linewidth(int linewidth, Boolean feedback)
{
  char text[40];

  X.gcv.line_width = linewidth;
  XChangeGC(X.dpy,X.gc,GCLineWidth,&X.gcv);
  if (feedback) {
    snprintf(text, sizeof text, "%d",linewidth);
    change_text(linewidthtext,text);
  }
}

void
GC_change_linestyle(int linestyle, Boolean feedback)
{
  X.gcv.line_style = linestyle;
  XChangeGC(X.dpy,X.gc,GCLineStyle,&X.gcv);
  if (feedback) select_correct_button(CLinestyle,linestyle);
}

void
GC_change_capstyle(int capstyle, Boolean feedback)
{
  X.gcv.cap_style = capstyle;
  XChangeGC(X.dpy,X.gc,GCCapStyle,&X.gcv);
  if (feedback) select_correct_button(CCapstyle,capstyle);
}

void
GC_change_joinstyle(int joinstyle, Boolean feedback)
{
  X.gcv.join_style = joinstyle;
  XChangeGC(X.dpy,X.gc,GCJoinStyle,&X.gcv);
  if (feedback) select_correct_button(CJoinstyle,joinstyle);
}

void
GC_change_fillstyle(int fillstyle, Boolean feedback)
{
  XSetFillStyle(X.dpy,X.gc,fillstyle);
  X.gcv.fill_style = fillstyle;
  if (feedback) select_correct_button(CFillstyle,fillstyle);
}

void
GC_change_fillrule(int fillrule, Boolean feedback)
{
  XSetFillRule(X.dpy,X.gc,fillrule);
  X.gcv.fill_rule = fillrule;
  if (feedback) select_correct_button(CFillrule,fillrule);
}

void
GC_change_arcmode(int arcmode, Boolean feedback)
{
  XSetArcMode(X.dpy,X.gc,arcmode);
  X.gcv.arc_mode = arcmode;
  if (feedback) select_correct_button(CArcmode,arcmode);
}

/* GC_change_dashlist(dashlist)
** ----------------------------
** Now this one's a bit tricky.  dashlist gets passed in as an int, but we
** want to change it to an array of chars, like the GC likes it.
** For example:
**     119 => XXX_XXX_ => [3,1,3,1]
*/

void
GC_change_dashlist(int dashlist, Boolean feedback) 
{
  char dasharray[DASHLENGTH];	/* what we're going to pass to XSetDashes */
  int dashnumber = 0;		/* which element of dasharray we're currently
				   modifying */
  int i;			/* which bit of the dashlist we're on */
  int state = 1;		/* whether the list bit we checked was
				   on (1) or off (0) */
				  
  /* Initialize the dasharray */

  for (i = 0; i < DASHLENGTH; ++i) dasharray[i] = 0;

  if (dashlist == 0) return;	/* having no dashes at all is bogus */

  /* XSetDashes expects the dashlist to start with an on bit, so if it
  ** doesn't, we keep on rotating it until it does */

  while (!(dashlist&1)) dashlist /= 2;

  /* Go through all the bits in dashlist, and update the dasharray
  ** accordingly */

  for (i = 0; i < DASHLENGTH; ++i) {
    /* the following if statements checks to see if the bit we're looking
    ** at as the same on or offness as the one before it (state).  If
    ** so, we increment the length of the current dash. */

    if (((dashlist&1<<i) && state) || (!(dashlist&1<<i) && !state))
      ++dasharray[dashnumber];
    else {			
      state = state^1;		/* reverse the state */
      ++dasharray[++dashnumber]; /* start a new dash */
    }
  } 

  XSetDashes(X.dpy,X.gc,0,dasharray,dashnumber+1);
  X.gcv.dashes = dashlist;

  if (feedback) update_dashlist(dashlist);
}

void
GC_change_planemask(unsigned long planemask, Boolean feedback)
{
  XSetPlaneMask(X.dpy,X.gc,planemask);
  X.gcv.plane_mask = planemask;
  if (feedback) update_planemask((long)planemask);
}

void
change_test(int test, Boolean feedback) 
{
  X.test = test;
  if (feedback) select_button(testchoicedesc,test);
}

void
GC_change_font(char *str, Boolean feedback)
{
  int num_fonts;		/* number of fonts that match the string */

  XListFonts(X.dpy,str,1,&num_fonts); /* see if the font exists */

  if (num_fonts) {
    XSetFont(X.dpy,X.gc,XLoadFont(X.dpy,str));
    if (feedback) change_text(fonttext,str);
  }
}

void
change_percent(int percent, Boolean feedback)
{
  /* Make sure that percent is valid */

  if (percent < 1 || percent > 100) return;

  X.percent = (float) percent / 100.0;

  if (feedback) update_slider(percent);
}