ARB
AWT_asciiprint.cxx
Go to the documentation of this file.
1 // ================================================================ //
2 // //
3 // File : AWT_asciiprint.cxx //
4 // Purpose : //
5 // //
6 // Institute of Microbiology (Technical University Munich) //
7 // http://www.arb-home.de/ //
8 // //
9 // ================================================================ //
10 
11 #include "awt_asciiprint.hxx"
12 #include "awt.hxx"
13 
14 #include <aw_window.hxx>
15 #include <aw_root.hxx>
16 #include <aw_question.hxx>
17 #include <aw_awar.hxx>
18 #include <aw_msg.hxx>
19 #include <arbdbt.h>
20 
21 static double awt_aps_get_xy_ratio(AW_root *awr) {
22  // AWT_asciiprint_paper_size psize = (AWT_asciiprint_paper_size)awr->awar(AWAR_APRINT_PAPER_SIZE)->read_int();
24  double res = 1.0;
25  switch (ori) {
27  res = 112.0/90.0;
28  break;
30  res = 112.0/50.0;
31  break;
33  res = 103.0/90.0;
34  break;
35  }
36  return res;
37 }
38 
41  switch (ori) {
43  return 80;
45  return 60;
47  return 80;
48  }
49  return -1;
50 }
51 
52 
54  int mag = awr->awar(AWAR_APRINT_MAGNIFICATION)->read_int();
55  if (mag < 25) {
57  return;
58  }
59  if (mag > 250) {
61  return;
62  }
63 
64  int x = awr->awar(AWAR_APRINT_SX)->read_int() * mag / 100;
65  int y = awr->awar(AWAR_APRINT_SY)->read_int() * mag / 100;
66  int default_lpp = awt_aps_get_default_lines_per_page(awr);
67  double xy_ratio = awt_aps_get_xy_ratio(awr);
68  int default_cpp = int(default_lpp * xy_ratio);
69 
70  awr->awar(AWAR_APRINT_DX)->write_float(float(x)/default_cpp);
71  awr->awar(AWAR_APRINT_DY)->write_float(float(y)/default_lpp);
72  x += default_cpp-1;
73  y += default_lpp-1;
74  x /= default_cpp;
75  y /= default_lpp;
76 
77  awr->awar(AWAR_APRINT_PAGES)->write_int(x* y);
78 }
79 
80 
82  int x = awr->awar(AWAR_APRINT_SX)->read_int();
83 
84  int dx = int(awr->awar(AWAR_APRINT_DX)->read_float()+.5);
85  if (dx < 1) dx = 1;
86  if (dx > 99) dx = 99;
87 
88  int default_lpp = awt_aps_get_default_lines_per_page(awr);
89  double xy_ratio = awt_aps_get_xy_ratio(awr);
90  int default_cpp = int(default_lpp * xy_ratio);
91  int mag = 100 * default_cpp * dx / x;
94 }
95 
97  int y = awr->awar(AWAR_APRINT_SY)->read_int();
98 
99  int dy = int(awr->awar(AWAR_APRINT_DY)->read_float()+.5);
100  if (dy < 1) dy = 1;
101  if (dy > 99) dy = 99;
102 
103  int default_lpp = awt_aps_get_default_lines_per_page(awr);
104  int mag = 100 * default_lpp * dy / y;
107 }
108 
111 }
114 }
115 static void awt_aps_text_changed(AW_root *awr) {
116  char *text = awr->awar(AWAR_APRINT_TEXT)->read_string();
117  {
118  char *rtext = GBS_replace_tabs_by_spaces(text);
119  delete text;
120  text = rtext;
121  }
122  int maxx, y;
123  maxx = 1; y = 0;
124  char *s;
125  char *ns;
126  for (s = text; s; s=ns) {
127  ns = strchr(s, '\n');
128  if (ns) {
129  ns[0] = 0;
130  ns++;
131  }
132  int slen = strlen(s);
133  if (slen > maxx) {
134  maxx = slen;
135  }
136  y++;
137  }
138  if (!y) y++;
139  awr->awar(AWAR_APRINT_SX)->write_int(maxx);
140  awr->awar(AWAR_APRINT_SY)->write_int(y);
141  delete text;
143 }
144 
145 static void write_file(const char *filename, const char *file) {
146  fprintf(stderr, "Printing to ASCII file '%s'\n", filename);
147  FILE *f = fopen(filename, "r");
148  if (f) {
149  fclose(f);
150  if (aw_question("overwrite_file", GBS_global_string("File '%s' already exist", filename), "Overwrite,Cancel")) {
151  return;
152  }
153  }
154  f = fopen(filename, "w");
155  if (!f) {
156  aw_message(GBS_global_string("Cannot write to '%s'", filename));
157  return;
158  }
159  fprintf(f, "%s", file);
160  fclose(f);
161 }
162 
163 static char *printFile(AW_root *awr) {
164  const char *home = GB_getenv("HOME");
165  const char *name = awr->awar(AWAR_APRINT_FILE)->read_char_pntr();
166  return GBS_global_string_copy("%s/%s", home, name);
167 }
168 
169 static void awt_aps_go(AW_window *aww) {
170  AW_root *awr = aww->get_root();
171  char *text = awr->awar(AWAR_APRINT_TEXT)->read_string();
172 
173  freeset(text, GBS_replace_tabs_by_spaces(text));
174 
176  if (dest == AWT_APRINT_DEST_FILE_ASCII) {
177  char *file = printFile(awr);
178  write_file(file, text);
179  free(file);
180  }
181  else {
182  char *tmp_file;
183  FILE *tmpf;
184  {
185  char *name = GB_unique_filename("arb_aprint", "txt");
186  tmpf = GB_fopen_tempfile(name, "wt", &tmp_file);
187  free(name);
188  }
189 
190  GB_ERROR error = NULp;
191  if (!tmpf) {
192  error = GBS_global_string("awt_aps_go: %s", GB_await_error());
193  }
194  else {
195  char *y_begin = text;
196  int last_y = 0;
197 
198  double xy_ratio = awt_aps_get_xy_ratio(awr);
199  int mag = awr->awar(AWAR_APRINT_MAGNIFICATION)->read_int();
200 
201  int default_lpp = awt_aps_get_default_lines_per_page(awr);
202  int default_cpp = int(default_lpp * xy_ratio);
203  default_cpp = default_cpp * 100 / mag;
204  default_lpp = default_lpp * 100 / mag;
205 
206  int text_width = awr->awar(AWAR_APRINT_SX)->read_int();
207  int text_height = awr->awar(AWAR_APRINT_SY)->read_int();
208 
209  int x;
210  int y;
211 
212  for (y = 0; y < text_height; y += default_lpp) {
213  while (last_y < y) {
214  last_y ++;
215  y_begin = strchr(y_begin, '\n');
216  if (!y_begin) break;
217  y_begin++;
218  }
219  if (!y_begin) break;
220 
221  for (x = 0; x < text_width; x += default_cpp) {
222  char *line = y_begin;
223  int i;
224  for (i=0; i<default_lpp; i++) {
225  if (line) {
226  char *next_line = strchr(line, '\n');
227  int line_length;
228  if (next_line) {
229  line_length = next_line - line; // exclusive '\n'
230  next_line ++;
231  }
232  else {
233  line_length = strlen(line);
234  }
235  if (line_length > x + default_cpp) {
236  line_length = x + default_cpp;
237  }
238  if (line_length > x) {
239  fwrite(line + x, sizeof(char), line_length - x, tmpf);
240  }
241  line = next_line;
242  }
243  fprintf(tmpf, "\n");
244  }
245  }
246  }
247  fclose(tmpf);
248 
249  char *a2ps_call = NULp;
250  {
252  const char *oristring = "";
253  switch (ori) {
255  oristring = "-p -1 ";
256  break;
258  oristring = "-l -1 ";
259  break;
261  oristring = "-p -2 ";
262  break;
263  }
264  const char *header = awr->awar(AWAR_APRINT_TITLE)->read_char_pntr();
265  char *quotedHeaderArg = GBK_singlequote(GBS_global_string("-H%s", header));
266  a2ps_call = GBS_global_string_copy("arb_a2ps -ns -nP %s %s -l%i %s",
267  quotedHeaderArg, oristring, default_lpp, tmp_file);
268  free(quotedHeaderArg);
269  }
270 
271  const char *scall = NULp;
272  switch (dest) {
274  char *printer = awr->awar(AWAR_APRINT_PRINTER)->read_string();
275  scall = GBS_global_string("%s |%s; rm -f %s", a2ps_call, printer, tmp_file);
276  free(printer);
277  break;
278  }
280  char *file = printFile(awr);
281  char *quotedFile = GBK_singlequote(file);
282  fprintf(stderr, "Printing to PS file %s\n", quotedFile);
283  scall = GBS_global_string("%s >%s;rm -f %s", a2ps_call, quotedFile, tmp_file);
284  free(quotedFile);
285  free(file);
286  break;
287  }
289  char *tmp_file2;
290  {
291  char *name_only;
292  GB_split_full_path(tmp_file, NULp, NULp, &name_only, NULp);
293  tmp_file2 = GB_create_tempfile(GBS_global_string("%s.ps", name_only));
294  free(name_only);
295  }
296 
297  if (!tmp_file2) error = GB_await_error();
298  else {
299  scall = GBS_global_string("%s >%s;(%s %s;rm -f %s %s)&",
300  a2ps_call, tmp_file2,
301  GB_getenvARB_GS(), tmp_file2,
302  tmp_file, tmp_file2);
303  free(tmp_file2);
304  }
305  break;
306  }
307  default:
308  awt_assert(0);
309  break;
310  }
311 
312  if (scall) error = GBK_system(scall);
313  free(a2ps_call);
314  }
315  if (error) aw_message(error);
316  free(tmp_file);
317  }
318  free(text);
319 }
320 
321 static void cutExt(char *name, const char *removeExt) {
322  char *found = strstr(name, removeExt);
323  if (found && strcmp(found, removeExt) == 0) {
324  found[0] = 0;
325  }
326 }
327 static char *correct_extension(const char *name, const char *newExt) {
328  char *noExt = ARB_strdup(name);
329  cutExt(noExt, ".ps");
330  cutExt(noExt, ".txt");
331 
332  char *result = ARB_alloc<char>(strlen(noExt)+strlen(newExt)+1);
333  strcpy(result, noExt);
334  strcat(result, newExt);
335  if (strcmp(name, result) == 0) freenull(result);
336 
337  free(noExt);
338  return result;
339 }
340 
341 static void aps_correct_filename(AW_root *aw_root) {
342  int type = aw_root->awar(AWAR_APRINT_PRINTTO)->read_int();
343  if (type == AWT_APRINT_DEST_FILE_PS || type == AWT_APRINT_DEST_FILE_ASCII) {
344  AW_awar *awar_name = aw_root->awar(AWAR_APRINT_FILE);
345  const char *name = awar_name->read_char_pntr();
346  char *new_name = NULp;
347 
348  if (type == AWT_APRINT_DEST_FILE_PS) {
349  new_name = correct_extension(name, ".ps");
350  }
351  else {
353  new_name = correct_extension(name, ".txt");
354  }
355  if (new_name) {
356  awar_name->write_string(new_name);
357  free(new_name);
358  }
359  }
360 }
361 
362 void AWT_create_ascii_print_window(AW_root *awr, const char *text_to_print, const char *title) {
363  static AW_window_simple *aws = NULp;
364 
365  awr->awar_string(AWAR_APRINT_TEXT)->write_string(text_to_print);
366  if (title) {
368  }
369  if (aws) {
371  }
372  else {
373  aws = new AW_window_simple;
374  aws->init(awr, "PRINT", "PRINT");
375  aws->load_xfig("awt/ascii_print.fig");
378 
381  awr->awar_int(AWAR_APRINT_PAGES, 1);
382  awr->awar_int(AWAR_APRINT_SX, 1);
383  awr->awar_int(AWAR_APRINT_SY, 1);
384 
385  awr->awar_float(AWAR_APRINT_DX, 1.0);
386  awr->awar_float(AWAR_APRINT_DY, 1.0);
387 
389 
393 
394  {
395  char *print_command;
396  if (getenv("PRINTER")) {
397  print_command = GBS_eval_env("lpr -h -P$(PRINTER)");
398  }
399  else {
400  print_command = ARB_strdup("lpr -h");
401  }
402 
403  awr->awar_string(AWAR_APRINT_PRINTER, print_command);
404  free(print_command);
405  }
406 
408 
409  aws->at("close");
410  aws->callback(AW_POPDOWN);
411  aws->create_button("CLOSE", "CLOSE");
412 
413  aws->at("help");
414  aws->callback(makeHelpCallback("asciiprint.hlp"));
415  aws->create_button("HELP", "HELP");
416 
417  aws->at("go");
418  aws->callback(awt_aps_go);
419  aws->create_button("PRINT", "PRINT");
420 
421  aws->at("title");
422  aws->create_input_field(AWAR_APRINT_TITLE);
423 
424  aws->at("text");
425  aws->create_text_field(AWAR_APRINT_TEXT);
426 
427  aws->button_length(5);
428  aws->at("rows");
429  aws->create_button(NULp, AWAR_APRINT_SY);
430 
431  aws->at("columns");
432  aws->create_button(NULp, AWAR_APRINT_SX);
433 
434  aws->at("magnification");
435  aws->create_input_field(AWAR_APRINT_MAGNIFICATION, 4);
436 
437  aws->at("paper_size");
438  {
439  aws->create_toggle_field(AWAR_APRINT_PAPER_SIZE, AW_HORIZONTAL);
440  aws->insert_toggle("A4", "A", int(AWT_APRINT_PAPERSIZE_A4));
441  aws->insert_toggle("US", "U", int(AWT_APRINT_PAPERSIZE_US));
442  aws->update_toggle_field();
443  }
444 
445  aws->at("orientation");
446  {
447  aws->create_toggle_field(AWAR_APRINT_ORIENTATION, AW_HORIZONTAL);
448  aws->insert_toggle("#print/portrait.xpm", "P", int(AWT_APRINT_ORIENTATION_PORTRAIT));
449  aws->insert_toggle("#print/landscape.xpm", "P", int(AWT_APRINT_ORIENTATION_LANDSCAPE));
450  aws->update_toggle_field();
451  }
452 
453 
454  aws->at("pages");
455  aws->create_button(NULp, AWAR_APRINT_PAGES);
456 
457  aws->at("dcol");
458  aws->callback(awt_aps_set_magnification_to_fit_xpage); // @@@ used as INPUTFIELD_CB (see #559)
459  aws->create_input_field(AWAR_APRINT_DX, 4);
460 
461  aws->at("drows");
462  aws->callback(awt_aps_set_magnification_to_fit_ypage); // @@@ used as INPUTFIELD_CB (see #559)
463  aws->create_input_field(AWAR_APRINT_DY, 4);
464 
465 
466  aws->at("printto");
467  aws->create_toggle_field(AWAR_APRINT_PRINTTO);
468  aws->insert_toggle("Printer", "P", int(AWT_APRINT_DEST_PRINTER));
469  aws->insert_toggle("File (Postscript)", "F", int(AWT_APRINT_DEST_FILE_PS));
470  aws->insert_toggle("File (ASCII)", "A", int(AWT_APRINT_DEST_FILE_ASCII));
471  aws->insert_toggle("Preview", "V", int(AWT_APRINT_DEST_PREVIEW));
472  aws->update_toggle_field();
473 
474  aws->at("printer");
475  aws->create_input_field(AWAR_APRINT_PRINTER, 16);
476 
477  aws->at("filename");
478  aws->create_input_field(AWAR_APRINT_FILE, 16);
479  }
480  aws->activate();
481 }
482 
483 
484 void AWT_show_file(AW_root *awr, const char *filename) {
485  char *text = GB_read_file(filename);
486  if (!text) {
488  }
489  else {
490  AWT_create_ascii_print_window(awr, text, filename);
491  free(text);
492  }
493 }
GB_ERROR GBK_system(const char *system_command)
Definition: arb_msg.cxx:571
const char * GB_ERROR
Definition: arb_core.h:25
#define AWAR_APRINT_SY
string result
GB_TYPES type
#define AWAR_APRINT_PAPER_SIZE
static void cutExt(char *name, const char *removeExt)
static char * y[maxsp+1]
#define AWAR_APRINT_PRINTTO
#define AWAR_APRINT_PRINTER
#define AWAR_APRINT_DY
#define awt_assert(cond)
Definition: awt.hxx:28
char * ARB_strdup(const char *str)
Definition: arb_string.h:27
long read_int() const
Definition: AW_awar.cxx:184
const char * GBS_global_string(const char *templat,...)
Definition: arb_msg.cxx:203
const char * title
Definition: readseq.c:22
void AW_POPDOWN(AW_window *window)
Definition: AW_window.cxx:52
static char * correct_extension(const char *name, const char *newExt)
char * GBS_replace_tabs_by_spaces(const char *text)
Definition: adstring.cxx:923
GB_CSTR GB_getenvARB_GS(void)
Definition: adsocket.cxx:640
#define AWAR_APRINT_TITLE
AW_awar * add_callback(const RootCallback &cb)
Definition: AW_awar.cxx:231
const char * read_char_pntr() const
Definition: AW_awar.cxx:168
GB_ERROR GB_await_error()
Definition: arb_msg.cxx:342
static void awt_aps_go(AW_window *aww)
WindowCallback makeHelpCallback(const char *helpfile)
Definition: aw_window.hxx:106
char * GB_create_tempfile(const char *name)
Definition: adsocket.cxx:1222
static void awt_aps_text_changed(AW_root *awr)
AWT_asciiprint_orientation
static void write_file(const char *filename, const char *file)
AW_awar * awar_float(const char *var_name, float default_value=0.0, AW_default default_file=AW_ROOT_DEFAULT)
Definition: AW_root.cxx:560
static void error(const char *msg)
Definition: mkptypes.cxx:96
FILE * GB_fopen_tempfile(const char *filename, const char *fmode, char **res_fullname)
Definition: adsocket.cxx:1176
#define AWAR_APRINT_FILE
static void awt_aps_set_magnification_to_fit_xpage(AW_root *awr)
#define AWAR_APRINT_TEXT
static double awt_aps_get_xy_ratio(AW_root *awr)
static void awt_aps_calc_pages_needed(AW_root *awr)
char * read_string() const
Definition: AW_awar.cxx:198
AW_awar * awar(const char *awar)
Definition: AW_root.cxx:554
#define AWAR_APRINT_SX
float read_float() const
Definition: AW_awar.cxx:177
GB_CSTR GB_getenv(const char *env)
Definition: adsocket.cxx:709
char * GB_unique_filename(const char *name_prefix, const char *suffix)
Definition: adsocket.cxx:1233
#define AWAR_APRINT_MAGNIFICATION
int aw_question(const char *unique_id, const char *question, const char *buttons, bool sameSizeButtons, const char *helpfile)
Definition: AW_question.cxx:26
AW_awar * awar_int(const char *var_name, long default_value=0, AW_default default_file=AW_ROOT_DEFAULT)
Definition: AW_root.cxx:580
static char * printFile(AW_root *awr)
char * GBK_singlequote(const char *arg)
Definition: arb_msg.cxx:599
static int awt_aps_get_default_lines_per_page(AW_root *awr)
void AWT_create_ascii_print_window(AW_root *awr, const char *text_to_print, const char *title)
const char * name_only(const char *fullpath)
Definition: AWTI_import.cxx:46
#define AWAR_APRINT_PAGES
void aw_message(const char *msg)
Definition: AW_status.cxx:1142
AW_root * get_root()
Definition: aw_window.hxx:359
char * GBS_eval_env(GB_CSTR p)
Definition: adstring.cxx:212
void GB_split_full_path(const char *fullpath, char **res_dir, char **res_fullname, char **res_name_only, char **res_suffix)
Definition: adsocket.cxx:1268
static int line
Definition: arb_a2ps.c:296
#define NULp
Definition: cxxforward.h:116
static void aps_correct_filename(AW_root *aw_root)
void AWT_show_file(AW_root *awr, const char *filename)
GB_ERROR write_string(const char *aw_string)
static void awt_aps_set_magnification_to_fit_ypage(AW_root *awr)
AW_awar * awar_string(const char *var_name, const char *default_value="", AW_default default_file=AW_ROOT_DEFAULT)
Definition: AW_root.cxx:570
GB_ERROR write_float(float aw_float)
AWT_asciiprint_destination
GB_ERROR write_int(long aw_int)
#define AWAR_APRINT_ORIENTATION
char * GBS_global_string_copy(const char *templat,...)
Definition: arb_msg.cxx:194
char * GB_read_file(const char *path)
Definition: adsocket.cxx:287
#define AWAR_APRINT_DX
GB_write_int const char s
Definition: AW_awar.cxx:154