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