1 /* test-filesel.cc -- customized file selection dialog
2    Copyright (C) 2005  SEIKO EPSON Corporation
3 
4    This file is part of the `iscan' program.
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as 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 
21 #include "pisa_aleart_dialog.cc"
22 #include "pisa_error.cc"
23 #include "file-selector.cc"
24 
25 /* Tester program specific stuff.  */
26 
27 #include "pisa_enums.h"
28 
29 #ifndef HAVE_GTK_2
30 #define G_OBJECT GTK_OBJECT
31 #endif
32 
33 static int
get_option_value()34 get_option_value ()
35 {
36   int result = PISA_OP_FLATBED;
37 
38   char *env_val = getenv ("PISA_OP");
39   if (env_val)
40     {
41       if (0 == strcasecmp ("ADF", env_val))
42 	{
43 	  result = PISA_OP_ADF;
44 	}
45       if (0 == strcasecmp ("ADFDPLX", env_val))
46 	{
47 	  result = PISA_OP_ADFDPLX;
48 	}
49       if (0 == strcasecmp ("TPU", env_val))
50 	{
51 	  result = PISA_OP_TPU;
52 	}
53     }
54   return result;
55 }
56 
57 static bool
is_button_set()58 is_button_set ()
59 {
60   return (NULL != getenv ("PISA_BUTTON"));
61 }
62 
63 static file_selector *file_sel = NULL;
64 static GtkWidget     *g_widget = NULL;
65 
66 static int g_cancel = 0;
67 static int g_sheets = 1;
68 
69 static bool
scan_file(gpointer param,const gchar * filename,int * cancel=NULL,int cnt=-1)70 scan_file (gpointer param, const gchar *filename,
71 	   int *cancel = NULL, int cnt = -1)
72 {
73   bool error = false;
74 
75   if (0 > cnt)
76     {
77       g_print ("Scanning to %s ... ", filename);
78       sleep (1);
79       g_print ("done.\n");
80     }
81   else
82     {
83       if (g_sheets)
84 	{
85 	  --g_sheets;
86 	  g_print ("Scanning to %s (%d) ... ", filename, cnt);
87 	  sleep (1);
88 	  g_print ("done.\n");
89 	}
90       else
91 	{
92 	  error = true;
93 	  *cancel = (0 > --g_cancel);
94 	  if (!*cancel)
95 	    {
96 	      char *env_val = getenv ("SHEETS");
97 	      if (env_val)
98 		{
99 		  g_sheets = atoi (env_val);
100 		}
101 	      else
102 		{
103 		  g_sheets = 1;
104 		}
105 	    }
106 	}
107     }
108   return error;
109 }
110 
111 static void
do_scan_file(gpointer param,file_selector * fs=NULL,bool first_time_around=false)112 do_scan_file (gpointer param, file_selector *fs = NULL,
113 	      bool first_time_around = false)
114 {
115   if (!fs)
116     {
117       fs = file_sel;
118       fs->init ();
119 
120       int  option = get_option_value ();
121       bool button = is_button_set ();
122 
123       fs->create_window (GTK_WINDOW (gtk_widget_get_parent (g_widget)),
124 			 g_widget, (   PISA_OP_ADF     == option
125 				    || PISA_OP_ADFDPLX == option
126 				    || button));
127       fs->hide ();
128     }
129 
130   char *filename = fs->get_filename ();
131 
132   if (   PISA_OP_ADF     != get_option_value ()
133       && PISA_OP_ADFDPLX != get_option_value ()
134       && !is_button_set ())
135     {
136       fs->destroy ();
137 
138       if (!filename)
139 	{
140 	  g_print ("No file selected\n");
141 	  return;
142 	}
143 
144       scan_file (param, filename);
145     }
146   else				// consecutive scanning
147     if (filename)
148       {
149 	g_print ("%s: consecutive scan\n", __func__);
150 
151 	int  cancel = 0;
152 	// check_overwrite initialises the value of cancel, so ...
153 	bool eos = (0 != cancel);
154 	int  cnt = fs->get_sequence_number();
155 
156 	while (!eos)
157 	  {
158 	    eos = scan_file (param, filename, &cancel, cnt);
159 	    first_time_around = false;
160 	    if (!eos)
161 	      {
162 		fs->set_sequence_number (++cnt);
163 	      }
164 	  }
165 
166 	if (!cancel)
167 	  {
168 	    g_print ("%s: recursing\n", __func__);
169 	    do_scan_file (param, fs, first_time_around);
170 	    g_print ("%s: returning\n", __func__);
171 	  }
172 	g_print ("%s: consecutive scan, if branch\n", __func__);
173 	fs->destroy ();
174       }
175     else
176       {
177 	g_print ("%s: consecutive scan, else branch\n", __func__);
178 	fs->destroy ();
179       }
180   free (filename);
181 }
182 
183 /* Callback for the Scan button.  */
184 static void
callback(GtkWidget * widget,gpointer data)185 callback (GtkWidget *widget, gpointer data )
186 {
187   g_print ("%s was pressed\n", (char *) data);
188 
189   gtk_widget_set_sensitive (widget, false);
190   g_widget = widget;
191 
192   char *env_val = NULL;
193 
194   env_val = getenv ("SHEETS");
195   if (env_val)
196     {
197       g_sheets = atoi (env_val);
198     }
199   env_val = getenv ("CANCEL");
200   if (env_val)
201     {
202       g_cancel = atoi (env_val);
203     }
204 
205   do_scan_file (NULL);
206 }
207 
208 int
main(int argc,char * argv[])209 main (int argc, char *argv[])
210 {
211   if (!file_sel)
212     {
213       file_sel = new file_selector;
214     }
215   if (!file_sel)
216     {
217       abort ();
218     }
219 
220   /* GtkWidget is the storage type for widgets */
221   GtkWidget *window;
222   GtkWidget *button;
223 
224   gtk_init (&argc, &argv);
225 
226   /* Create a new window */
227   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
228 
229   gtk_window_set_title (GTK_WINDOW (window), "File Selection Tester");
230 
231   /* It's a good idea to do this for all windows. */
232   g_signal_connect (G_OBJECT (window), "destroy",
233 		    G_CALLBACK (gtk_main_quit), NULL);
234 
235   g_signal_connect (G_OBJECT (window), "delete_event",
236 		    G_CALLBACK (gtk_main_quit), NULL);
237 
238   /* Create a new button */
239   button = gtk_button_new_with_label ("Scan");
240 
241   /* Connect the "clicked" signal of the button to our callback */
242   g_signal_connect (G_OBJECT (button), "clicked",
243 		    G_CALLBACK (callback), (gpointer) "Scan button");
244 
245   gtk_widget_show (button);
246 
247   gtk_container_add (GTK_CONTAINER (window), button);
248 
249   gtk_widget_show (window);
250 
251   /* Rest in gtk_main and wait for the fun to begin! */
252   gtk_main ();
253 
254   return 0;
255 }
256