1 /* NIGHTFALL Light Curve Synthesis Program                                 */
2 /* Copyright (C) 1998 Rainer Wichmann                                      */
3 /*                                                                         */
4 /*  This program is free software; you can redistribute it                 */
5 /*  and/or modify                                                          */
6 /*  it under the terms of the GNU General Public License as                */
7 /*  published by                                                           */
8 /*  the Free Software Foundation; either version 2 of the License, or      */
9 /*  (at your option) any later version.                                    */
10 /*                                                                         */
11 /*  This program is distributed in the hope that it will be useful,        */
12 /*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
13 /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
14 /*  GNU General Public License for more details.                           */
15 /*                                                                         */
16 /*  You should have received a copy of the GNU General Public License      */
17 /*  along with this program; if not, write to the Free Software            */
18 /*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
19 
20 #include <math.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 
25 #include "Light.h"
26 
27 #ifdef  HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30 
31 
32 #ifdef _WITH_GTK
33 
34 
35 /****************************************************************************
36  @package   nightfall
37  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
38  @version   1.0
39  @short     Destroy data sheet
40  @param     (GtkWidget) *widget  Discarded
41  @param     (gpointer) *fs       Widget to destroy
42  @return    (void)
43  @heading   Data display
44  ****************************************************************************/
close_data(GtkWidget * widget,gpointer * fs)45 void close_data( GtkWidget *widget, gpointer *fs )
46 {
47   gtk_widget_destroy (GTK_WIDGET (fs));
48 }
49 
50 /****************************************************************************
51  @package   nightfall
52  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
53  @version   1.0
54  @short     Hardcopy data sheet
55  @param     (GtkWidget) *widget  Discarded
56  @param     (gpointer) *fs       Discarded
57  @return    (void)
58  @heading   Data display
59  ****************************************************************************/
hard_data(GtkWidget * widget,gpointer * fs)60 void hard_data( GtkWidget *widget, gpointer *fs )
61 {
62 #ifdef HAVE_A2PS
63   FILE *pipe_out;
64   char command[512];
65   char title[32];
66 
67   strncpy(title, Orbit.Name, sizeof(title)-1);
68   command[0] = '\0';
69 
70   sprintf(command,
71 	  "cat - | sed -e 's@<@ @g' | sed -e 's@>@ @g' | sed -e 's@#@ @g' | a2ps --output=- -1 --portrait --quiet --center-title=%c%s%c > NightfallData.ps",
72 	  34, title, 34);
73   /*
74   sprintf(command,
75 	  "cat -  > NightfallData.ps");
76   */
77   if ( (pipe_out = popen(command, "w")) != NULL) {
78     OutfileHeader(pipe_out);
79     if (-1 == pclose(pipe_out))
80       perror("pclose");
81   } else {
82     perror(_("popen"));
83   }
84 #else
85   WARNING(_("no postscript driver (a2ps)"));
86 #endif
87   return;
88 
89 }
90 
91 
92 /****************************************************************************
93  @package   nightfall
94  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
95  @version   1.0
96  @short     Show data for present binary system configuration
97  @param     (GtkWidget) *widget  Discarded
98  @param     (GtkEvent)  *event   Discarded
99  @param     (gpointer) *data     Discarded
100  @return    (void)
101  @heading   Data display
102  ****************************************************************************/
showdata_event(GtkWidget * widget,gpointer * data)103 void showdata_event (GtkWidget *widget, gpointer *data)
104 {
105 
106   FILE        *inFile;                     /* filehandler for input file    */
107   char        tmpfile[1024];               /* temporary file on disk        */
108   char        inStrg[8];                   /* read from file, put in widget */
109   int         ErrCode;                     /* exit status                   */
110   int         c;                           /* for fgetc                     */
111 
112   GtkWidget *showdata_window;
113   GtkWidget *hbox;
114   GtkWidget *vbox;
115   GtkWidget *separator;
116   GtkWidget *button;
117   GtkWidget *text;
118 #ifdef USING_GTK2
119   GtkTextBuffer *tgbuffer;
120   GtkWidget     *scroll;
121   GtkTextIter iter;
122 #else
123   GtkWidget *vscrollbar;
124   GdkColormap *cmap;
125   GdkColor colour;
126   GdkFont *fixed_font;
127 #endif
128 
129   if      (getenv("TEMPDIR") != NULL)
130     strncpy(tmpfile, getenv("TEMPDIR"), 960);
131   else if (getenv("TMPDIR")  != NULL)
132     strncpy(tmpfile, getenv("TMPDIR"),  960);
133   else
134     strcpy(tmpfile, "/tmp");
135   sprintf(tmpfile + strlen(tmpfile), "/nightfall.data");
136 
137   /* >>>>>>>>>>>>>>>>>  define geometry <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<     */
138 
139   /* -------- validate input data        ---------------------              */
140 
141   check_data();
142 
143   /* elliptic orbit                                                  */
144   if (Flags.elliptic == ON)
145     {
146       FindPeriastron();
147       Orbit.Dist = 1.0;
148     }
149 
150   if (Flags.fill == OFF) {
151      /* THE PRIMARY */
152      ErrCode = DefineParam(Primary);
153      /* THE SECONDARY */
154      DefineParam(Secondary);
155   } else {
156      /* Overcontact system */
157      ErrCode = DefineParamOver();
158   }
159 
160   if (ErrCode > 0) {
161     make_dialog (_(errmsg[12]));
162     return;
163   }
164 
165   LightSetupTests();
166 
167   /* >>>>>>>>>>>>>>>>>>>>>>>>>>  open tmp file   <<<<<<<<<<<<<<<<<<<<<<<<<< */
168 
169 #ifdef  HAVE_UNISTD_H
170   sprintf(tmpfile + strlen(tmpfile), ".%d", getpid() );
171 #endif
172 
173 
174   if ((inFile = fopen (tmpfile, "w+")) == NULL) {
175       make_dialog (_(errmsg[13]));
176       return;
177   }
178 
179   OutfileHeader(inFile);
180 
181   /* >>>>>>>>>>>>>>>>>>>  read tmp file   <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<  */
182 
183   /* rewind file                                                            */
184   (void) fseek(inFile, 0L, SEEK_SET);
185 
186   showdata_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
187   gtk_widget_set_usize (showdata_window, 640, 500);
188   gtk_window_set_policy (GTK_WINDOW(showdata_window), TRUE, TRUE, FALSE);
189   gtk_signal_connect (GTK_OBJECT (showdata_window), "destroy",
190                         (GtkSignalFunc) close_data,
191                          GTK_WIDGET (showdata_window));
192   gtk_window_set_title (GTK_WINDOW (showdata_window), _("Data Sheet") );
193   gtk_container_set_border_width (GTK_CONTAINER (showdata_window), 0);
194 
195 
196   vbox = gtk_vbox_new (FALSE, 0);
197   gtk_container_add (GTK_CONTAINER (showdata_window), vbox);
198   gtk_widget_show (vbox);
199 
200   hbox = gtk_hbox_new (FALSE, 0);
201   gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
202   gtk_widget_show (hbox);
203 
204   /* Create the GtkText widget                                              */
205 
206 #ifdef USING_GTK2
207   tgbuffer = gtk_text_buffer_new(NULL);
208   text     = gtk_text_view_new_with_buffer (tgbuffer);
209 
210   /* Create tags associated with the buffer.                      */
211 
212   /* Tag with font fixed and tag name "fixed".                    */
213   gtk_text_buffer_create_tag (tgbuffer, "fixed",
214                               "font", "fixed",
215                               NULL);
216 
217   /* Tag with font fixed and tag name "fixed".                    */
218   gtk_text_buffer_create_tag (tgbuffer, "red",
219  			      "foreground", "red",
220                               NULL);
221 
222   scroll  = gtk_scrolled_window_new (NULL, NULL);
223   gtk_container_add (GTK_CONTAINER(scroll), GTK_WIDGET(text));
224 
225   gtk_box_pack_start (GTK_BOX (hbox), scroll, TRUE, TRUE, 0);
226   gtk_widget_show (scroll);
227   gtk_widget_show (text);
228 
229   gtk_text_buffer_get_iter_at_offset(tgbuffer, &iter, 0);
230 #else
231   text = gtk_text_new (NULL, NULL);
232   gtk_text_set_editable (GTK_TEXT (text), FALSE);
233   gtk_box_pack_start (GTK_BOX (hbox), text, TRUE, TRUE, 0);
234   gtk_widget_show (text);
235 
236   /* Add a vertical scrollbar to the GtkText widget                         */
237 
238   vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
239   gtk_box_pack_start(GTK_BOX(hbox), vscrollbar, FALSE, FALSE, 0);
240   gtk_widget_show (vscrollbar);
241 
242   /* Realizing a widget creates a window for it, ready to insert some text  */
243 
244   gtk_widget_realize (text);
245 
246   /* Freeze the text widget, ready for multiple updates                     */
247 
248   gtk_text_freeze (GTK_TEXT (text));
249 
250   /* Load a fixed font                                                      */
251 
252   fixed_font = gdk_font_load ("-*-fixed-*-r-*-*-*-140-*-*-*-*-iso8859-1");
253   if (!fixed_font) {
254     WARNING (_("Could not load font -*-fixed-*-r-*-*-*-140-*-*-*-*-iso8859-1"));
255   }
256 
257   /* Get the system colour map and allocate the colour red                  */
258 
259   cmap = gdk_colormap_get_system();
260   colour.red = 0xffff;
261   colour.green = 0;
262   colour.blue = 0;
263   if (!gdk_color_alloc(cmap, &colour)) {
264     WARNING (_("Could not allocate colour"));
265   }
266 
267 #endif
268 
269   /* >>>>>>>>>>>>>>>>>>> parse tmp file   <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<  */
270 
271   inStrg[1] = '\0';
272   inStrg[2] = '\0';
273   inStrg[3] = '\0';
274   inStrg[4] = '\0';
275 
276   while (1) {
277     c = fgetc (inFile);
278 
279     /* End of file, we are done                                             */
280     if (c == EOF)
281       break;
282 
283     inStrg[1] = '\0';
284     inStrg[2] = '\0';
285     inStrg[3] = '\0';
286     inStrg[0] = (char) c;
287 
288 #ifdef USING_GTK2
289     if (!g_ascii_isgraph(c) && !g_ascii_isspace(c))
290       {
291 	c = fgetc (inFile);
292 	if (c == EOF)
293 	  break;
294 	inStrg[1] = (char) c;
295 	if (!g_ascii_isgraph(c) && !g_ascii_isspace(c))
296 	  {
297 	    c = fgetc (inFile);
298 	    if (c == EOF)
299 	      break;
300 	    inStrg[2] = (char) c;
301 	    if (!g_ascii_isgraph(c) && !g_ascii_isspace(c))
302 	      {
303 		c = fgetc (inFile);
304 		if (c == EOF)
305 		  break;
306 		inStrg[3] = (char) c;
307 	      }
308 	  }
309       }
310 #endif
311 
312     /* not special, print                                                   */
313     if (inStrg[0] != '<') {
314       if (inStrg[0] == '#') inStrg[0] = ' ';
315 #ifdef USING_GTK2
316       gtk_text_buffer_insert_with_tags_by_name (tgbuffer, &iter, inStrg, -1,
317 						"fixed", NULL);
318 #else
319       gtk_text_insert (GTK_TEXT (text), fixed_font, &text->style->black, NULL,
320                           inStrg, 1);
321 #endif
322       continue;
323     }
324 
325     /* tagged word                                                          */
326     while (c != EOF) {
327      c = fgetc (inFile);
328      inStrg[1] = '\0';
329      inStrg[2] = '\0';
330      inStrg[3] = '\0';
331      inStrg[0] =  (char) c;
332      if (inStrg[0] == '>') break;
333      if (inStrg[0] == '#') inStrg[0] = ' ';
334 
335 #ifdef USING_GTK2
336      if (!g_ascii_isgraph(c) && !g_ascii_isspace(c))
337        {
338 	 c = fgetc (inFile);
339 	 if (c == EOF)
340 	   break;
341 	 inStrg[1] = (char) c;
342 	 if (!g_ascii_isgraph(c) && !g_ascii_isspace(c))
343 	  {
344 	    c = fgetc (inFile);
345 	    if (c == EOF)
346 	      break;
347 	    inStrg[2] = (char) c;
348 	    if (!g_ascii_isgraph(c) && !g_ascii_isspace(c))
349 	      {
350 		c = fgetc (inFile);
351 		if (c == EOF)
352 		  break;
353 		inStrg[3] = (char) c;
354 	      }
355 	  }
356       }
357 
358      gtk_text_buffer_insert_with_tags_by_name (tgbuffer, &iter, inStrg, -1,
359 					       "fixed", "red", NULL);
360 #else
361      gtk_text_insert (GTK_TEXT (text), fixed_font, &colour, NULL,
362                           inStrg, 1);
363 #endif
364     }
365   }
366 
367   /* >>>>>>>>>>>>>>>>>>> end parse file   <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<  */
368 
369   fclose(inFile);
370 
371   remove(tmpfile);
372 
373   /* Thaw the text widget, allowing the updates to become visible           */
374 
375 #ifndef USING_GTK2
376   gtk_text_thaw (GTK_TEXT (text));
377 #endif
378 
379   separator = gtk_hseparator_new ();
380   gtk_box_pack_start (GTK_BOX (vbox), separator, FALSE, TRUE, 0);
381   gtk_widget_show (separator);
382 
383   hbox = gtk_hbox_new (FALSE, 0);
384   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, TRUE, 0);
385   gtk_widget_show (hbox);
386 
387 #ifdef HAVE_A2PS
388 #ifdef USING_GTK2
389   button = gtk_button_new_with_label (_("Postscript") );
390   gtk_button_set_image (GTK_BUTTON(button),
391 			gtk_image_new_from_stock (GTK_STOCK_PRINT,
392 						  GTK_ICON_SIZE_SMALL_TOOLBAR)
393 			);
394 #else
395   button = gtk_button_new_with_label (_("Postscript"));
396 #endif
397   gtk_signal_connect (GTK_OBJECT (button), "clicked",
398                       (GtkSignalFunc) hard_data,
399                       GTK_WIDGET (showdata_window));
400   gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 5);
401   (GTK_WIDGET_FLAGS (button)  |= (GTK_CAN_DEFAULT));
402   gtk_widget_grab_default (button);
403   gtk_widget_show (button);
404 #endif
405 
406 #ifdef USING_GTK2
407   button = gtk_button_new_from_stock(GTK_STOCK_OK);
408 #else
409   button = gtk_button_new_with_label (_("Close") );
410 #endif
411   gtk_signal_connect (GTK_OBJECT (button), "clicked",
412                       (GtkSignalFunc) close_data,
413                       GTK_WIDGET (showdata_window));
414   gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
415   (GTK_WIDGET_FLAGS (button)  |= (GTK_CAN_DEFAULT));
416   gtk_widget_grab_default (button);
417   gtk_widget_show (button);
418 
419   gtk_widget_show (showdata_window);
420 
421 }
422 
423 #endif
424