1/*
2** xgc
3**
4** record.c
5*/
6
7#include <X11/Intrinsic.h>
8#include <X11/StringDefs.h>
9#include <X11/Xaw/Label.h>
10#include <X11/Xaw/Command.h>
11#include <X11/Xaw/Form.h>
12#include <X11/Shell.h>
13#include <X11/Xaw/AsciiText.h>
14#include <stdio.h>
15
16#include "xgc.h"
17
18static void start_recording(void);
19static void stop_recording(void);
20static void print_out_gc_values(void);
21static void chose_playback_filename(void);
22static void cancel_playback(void);
23static void done_choosing_filename(void) ;
24static void cancel_record(void);
25
26FILE *recordfile;		/* the file we're recording to */
27FILE *playbackfile;		/* the file we're playing back from */
28
29/* toggle_recordbutton(w,closure,call_data)
30** ----------------------------------------
31** This function is called when the user presses the "Record"
32** command button.  If we're not recording, we start; if we are,
33** we stop.  Also change the label to reflect the change in the
34** function of the button.
35*/
36
37/*ARGSUSED*/
38void
39toggle_recordbutton(Widget w, caddr_t closure, caddr_t call_data)
40{
41  /* ArgList for changing the label */
42  static Arg recordargs[] = {
43    {XtNlabel,        (XtArgVal) NULL}
44  };
45
46  char tmp[20];			/* new label */
47
48  if (!recording) {
49    start_recording();
50  }
51  else {
52    recording = FALSE;
53    stop_recording();
54    snprintf(tmp, sizeof tmp, "Record");
55    recordargs[0].value = (XtArgVal) tmp;
56  }
57
58  XtSetValues(recordbutton,recordargs,XtNumber(recordargs));
59}
60
61/* start_recording()
62** -----------------
63** Get the name of the file the user wants to record into, and
64** start recording into it if he doesn't cancel.
65*/
66
67static void
68start_recording(void)
69{
70  get_filename(done_choosing_filename,cancel_record);
71}
72
73/* stop_recording()
74** ----------------
75** Close the output file.
76*/
77
78static void
79stop_recording(void)
80{
81  fclose(recordfile);
82}
83
84/* cancel_record()
85** ---------------
86** What to do if the if the user canceled recording, i.e. nothing.
87*/
88
89static void
90cancel_record(void)
91{
92}
93
94/* done_choosing_filename()
95** ------------------------
96** What to do after the user's chosen a file.  Change the label on the
97** command button, open the file, and dump the current contents of the
98** GC into it.
99*/
100
101static void
102done_choosing_filename(void)
103{
104  static Arg recordargs[] = {
105    {XtNlabel,        (XtArgVal) NULL},
106    {XtNresize,       (XtArgVal) True}
107  };
108  Arg args[1];
109  char tmp[20], *filename;
110
111  XtSetArg(args[0], XtNstring, &filename);
112  XtGetValues(filename_text_widget, args, (Cardinal) 1);
113
114  if ((recordfile = fopen(filename,"w")) != NULL) {
115    recording = TRUE;
116    snprintf(tmp, sizeof tmp, "End Record");
117    recordargs[0].value = (XtArgVal) tmp;
118    XtSetValues(recordbutton,recordargs,XtNumber(recordargs));
119
120    print_out_gc_values();
121  }
122}
123
124/* print_if_recording(str)
125** -----------------------
126** If we're recording to a file, put str in it.
127*/
128
129void
130print_if_recording(const char *str)
131{
132  if (recording)
133    fprintf(recordfile,"%s",str);
134}
135
136/* close_file_if_recording()
137** -------------------------
138** If we're recording, stop.
139*/
140
141void
142close_file_if_recording(void)
143{
144  if (recording)
145    fclose(recordfile);
146}
147
148/* print_out_gc_values()
149** ---------------------
150** Dump the contents of the GC to the file, so that when the file gets
151** played back, it will be correctly initialized.
152*/
153
154static void
155print_out_gc_values(void)
156{
157  int i;
158  for (i=0;i<NUM_TESTS;++i) {
159    if ((TestStuff.data)[i].code == X.test) {
160      fprintf(recordfile,"%s %s\n",
161	      TestStuff.choice.text,(TestStuff.data)[i].text);
162      break;
163    }
164  }
165  for (i=0;i<NUM_FUNCTIONS;++i) {
166    if ((FunctionStuff.data)[i].code == X.gcv.function) {
167      fprintf(recordfile,"%s %s\n",
168	      FunctionStuff.choice.text,(FunctionStuff.data)[i].text);
169      break;
170    }
171  }
172  for (i=0;i<NUM_LINESTYLES;++i) {
173    if ((LinestyleStuff.data)[i].code == X.gcv.line_style) {
174      fprintf(recordfile,"%s %s\n",
175	      LinestyleStuff.choice.text,(LinestyleStuff.data)[i].text);
176      break;
177    }
178  }
179  for (i=0;i<NUM_CAPSTYLES;++i) {
180    if ((CapstyleStuff.data)[i].code == X.gcv.cap_style) {
181      fprintf(recordfile,"%s %s\n",
182	      CapstyleStuff.choice.text,(CapstyleStuff.data)[i].text);
183      break;
184    }
185  }
186  for (i=0;i<NUM_JOINSTYLES;++i) {
187    if ((JoinstyleStuff.data)[i].code == X.gcv.join_style) {
188      fprintf(recordfile,"%s %s\n",
189	      JoinstyleStuff.choice.text,(JoinstyleStuff.data)[i].text);
190      break;
191    }
192  }
193  for (i=0;i<NUM_FILLSTYLES;++i) {
194    if ((FillstyleStuff.data)[i].code == X.gcv.fill_style) {
195      fprintf(recordfile,"%s %s\n",
196	      FillstyleStuff.choice.text,(FillstyleStuff.data)[i].text);
197      break;
198    }
199  }
200  for (i=0;i<NUM_FILLRULES;++i) {
201    if ((FillruleStuff.data)[i].code == X.gcv.fill_rule) {
202      fprintf(recordfile,"%s %s\n",
203	      FillruleStuff.choice.text,(FillruleStuff.data)[i].text);
204      break;
205    }
206  }
207  for (i=0;i<NUM_ARCMODES;++i) {
208    if ((ArcmodeStuff.data)[i].code == X.gcv.arc_mode) {
209      fprintf(recordfile,"%s %s\n",
210	      ArcmodeStuff.choice.text,(ArcmodeStuff.data)[i].text);
211      break;
212    }
213  }
214  fprintf(recordfile,"linewidth %d\n",X.gcv.line_width);
215  fprintf(recordfile,"foreground %ld\n",X.gcv.foreground);
216  fprintf(recordfile,"background %ld\n",X.gcv.background);
217  fprintf(recordfile,"planemask %ld\n",X.gcv.plane_mask);
218  fprintf(recordfile,"dashlist %d\n",X.gcv.dashes);
219  fprintf(recordfile,"font %s\n",X.fontname);
220}
221
222/********************************************/
223
224/* start_playback()
225** ----------------
226** This gets called if the user wants to playback from a file.
227** Get the file name and do the appropriate thing.
228*/
229
230void
231start_playback(void)
232{
233  get_filename(chose_playback_filename,cancel_playback);
234}
235
236/* cancel_playback()
237** -----------------
238** What to do if the user canceled the playback request.
239*/
240
241static void
242cancel_playback(void)
243{
244}
245
246/* chose_playback_filename()
247** -------------------------
248** What to do once the user's selected a filename to playback.
249** Play it back.
250*/
251
252static void
253chose_playback_filename(void)
254{
255  Arg args[1];
256  char *filename;
257
258  XtSetArg(args[0], XtNstring, &filename);
259  XtGetValues(filename_text_widget, args, (Cardinal) 1);
260
261  if ((playbackfile = fopen(filename,"r")) != NULL) {
262    yyin = playbackfile;
263    yyparse();
264  }
265}
266
267/* read_from_keyboard()
268** --------------------
269** Do a playback from the keyboard.
270*/
271
272void
273read_from_keyboard(void)
274{
275  yyin = stdin;
276  yyparse();
277}
278