source: tags/ms_r16q2/AWT/AWT_asciiprint.cxx

Last change on this file was 14741, checked in by westram, 8 years ago
  • mark more unwanted widget-callbacks (#559)
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.1 KB
Line 
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
21static 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();
23    AWT_asciiprint_orientation ori = AWT_asciiprint_orientation(awr->awar(AWAR_APRINT_ORIENTATION)->read_int());
24    double res = 1.0;
25    switch (ori) {
26        case AWT_APRINT_ORIENTATION_PORTRAIT:
27            res = 112.0/90.0;
28            break;
29        case AWT_APRINT_ORIENTATION_LANDSCAPE:
30            res = 112.0/50.0;
31            break;
32        case AWT_APRINT_ORIENTATION_DOUBLE_PORTRAIT:
33            res = 103.0/90.0;
34            break;
35    }
36    return res;
37}
38
39static int awt_aps_get_default_lines_per_page(AW_root *awr) {
40    AWT_asciiprint_orientation ori = AWT_asciiprint_orientation(awr->awar(AWAR_APRINT_ORIENTATION)->read_int());
41    switch (ori) {
42        case AWT_APRINT_ORIENTATION_PORTRAIT:
43            return 80;
44        case AWT_APRINT_ORIENTATION_LANDSCAPE:
45            return 60;
46        case AWT_APRINT_ORIENTATION_DOUBLE_PORTRAIT:
47            return 80;
48    }
49    return -1;
50}
51
52
53static void awt_aps_calc_pages_needed(AW_root *awr) {
54    int mag = awr->awar(AWAR_APRINT_MAGNIFICATION)->read_int();
55    if (mag < 25) {
56        awr->awar(AWAR_APRINT_MAGNIFICATION)->write_int(mag*2);
57        return;
58    }
59    if (mag > 250) {
60        awr->awar(AWAR_APRINT_MAGNIFICATION)->write_int(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
81static void awt_aps_set_magnification_to_fit_xpage(AW_root *awr) {
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;
92    awr->awar(AWAR_APRINT_MAGNIFICATION)->write_int(mag);
93    awt_aps_calc_pages_needed(awr);
94}
95
96static void awt_aps_set_magnification_to_fit_ypage(AW_root *awr) {
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;
105    awr->awar(AWAR_APRINT_MAGNIFICATION)->write_int(mag);
106    awt_aps_calc_pages_needed(awr);
107}
108
109static void awt_aps_set_magnification_to_fit_xpage(AW_window *aww) {
110    awt_aps_set_magnification_to_fit_xpage(aww->get_root());
111}
112static void awt_aps_set_magnification_to_fit_ypage(AW_window *aww) {
113    awt_aps_set_magnification_to_fit_ypage(aww->get_root());
114}
115static 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    }    int maxx, y;
122    maxx = 1; y = 0;
123    char *s;
124    char *ns;
125    for (s = text; s; s=ns) {
126        ns = strchr(s, '\n');
127        if (ns) {
128            ns[0] = 0;
129            ns++;
130        }
131        int slen = strlen(s);
132        if (slen > maxx) {
133            maxx = slen;
134        }
135        y++;
136    }
137    if (!y) y++;
138    awr->awar(AWAR_APRINT_SX)->write_int(maxx);
139    awr->awar(AWAR_APRINT_SY)->write_int(y);
140    delete text;
141    awt_aps_set_magnification_to_fit_xpage(awr);
142}
143
144static void write_file(const char *filename, const char *file) {
145    fprintf(stderr, "Printing to ASCII file '%s'\n", filename);
146    FILE *f = fopen(filename, "r");
147    if (f) {
148        fclose(f);
149        if (aw_question("overwrite_file", GBS_global_string("File '%s' already exist", filename), "Overwrite,Cancel")) {
150            return;
151        }
152    }
153    f = fopen(filename, "w");
154    if (!f) {
155        aw_message(GBS_global_string("Cannot write to '%s'", filename));
156        return;
157    }
158    fprintf(f, "%s", file);
159    fclose(f);
160}
161
162static char *printFile(AW_root *awr) {
163    const char *home = GB_getenv("HOME");
164    const char *name = awr->awar(AWAR_APRINT_FILE)->read_char_pntr();
165    return GBS_global_string_copy("%s/%s", home, name);
166}
167
168static void awt_aps_go(AW_window *aww) {
169    AW_root *awr  = aww->get_root();
170    char    *text = awr->awar(AWAR_APRINT_TEXT)->read_string();
171
172    freeset(text, GBS_replace_tabs_by_spaces(text));
173
174    AWT_asciiprint_destination dest = (AWT_asciiprint_destination)awr->awar(AWAR_APRINT_PRINTTO)->read_int();
175    if (dest == AWT_APRINT_DEST_FILE_ASCII) {
176        char *file = printFile(awr);
177        write_file(file, text);
178        free(file);
179    }
180    else {
181        char *tmp_file;
182        FILE *tmpf;
183        {
184            char *name = GB_unique_filename("arb_aprint", "txt");
185            tmpf = GB_fopen_tempfile(name, "wt", &tmp_file);
186            free(name);
187        }
188
189        GB_ERROR error = NULL;
190        if (!tmpf) {
191            error = GBS_global_string("awt_aps_go: %s", GB_await_error());
192        }
193        else {
194            char *y_begin = text;
195            int last_y = 0;
196
197            double xy_ratio = awt_aps_get_xy_ratio(awr);
198            int    mag      = awr->awar(AWAR_APRINT_MAGNIFICATION)->read_int();
199
200            int default_lpp = awt_aps_get_default_lines_per_page(awr);
201            int default_cpp = int(default_lpp * xy_ratio);
202            default_cpp     = default_cpp * 100 / mag;
203            default_lpp     = default_lpp * 100 / mag;
204
205            int text_width  = awr->awar(AWAR_APRINT_SX)->read_int();
206            int text_height = awr->awar(AWAR_APRINT_SY)->read_int();
207
208            int x;
209            int y;
210
211            for (y = 0; y < text_height; y += default_lpp) {
212                while (last_y < y) {
213                    last_y ++;
214                    y_begin = strchr(y_begin, '\n');
215                    if (!y_begin) break;
216                    y_begin++;
217                }
218                if (!y_begin) break;
219
220                for (x = 0; x < text_width; x += default_cpp) {
221                    char *line = y_begin;
222                    int i;
223                    for (i=0; i<default_lpp; i++) {
224                        if (line) {
225                            char *next_line = strchr(line, '\n');
226                            int line_length;
227                            if (next_line) {
228                                line_length = next_line - line; // exclusive '\n'
229                                next_line ++;
230                            }
231                            else {
232                                line_length = strlen(line);
233                            }
234                            if (line_length > x + default_cpp) {
235                                line_length = x + default_cpp;
236                            }
237                            if (line_length > x) {
238                                fwrite(line + x, sizeof(char), line_length - x, tmpf);
239                            }
240                            line = next_line;
241                        }
242                        fprintf(tmpf, "\n");
243                    }
244                }
245            }
246            fclose(tmpf);
247
248            char *a2ps_call = 0;
249            {
250                AWT_asciiprint_orientation ori = AWT_asciiprint_orientation(awr->awar(AWAR_APRINT_ORIENTATION)->read_int());
251                const char *oristring = "";
252                switch (ori) {
253                    case AWT_APRINT_ORIENTATION_PORTRAIT:
254                        oristring = "-p -1 ";
255                        break;
256                    case AWT_APRINT_ORIENTATION_LANDSCAPE:
257                        oristring = "-l -1 ";
258                        break;
259                    case AWT_APRINT_ORIENTATION_DOUBLE_PORTRAIT:
260                        oristring = "-p -2 ";
261                        break;
262                }
263                const char *header          = awr->awar(AWAR_APRINT_TITLE)->read_char_pntr();
264                char       *quotedHeaderArg = GBK_singlequote(GBS_global_string("-H%s", header));
265                a2ps_call                   = GBS_global_string_copy("arb_a2ps -ns -nP %s %s -l%i %s",
266                                                                     quotedHeaderArg, oristring, default_lpp, tmp_file);
267                free(quotedHeaderArg);
268            }
269
270            const char *scall = 0;
271            switch (dest) {
272                case AWT_APRINT_DEST_PRINTER: {
273                    char *printer = awr->awar(AWAR_APRINT_PRINTER)->read_string();
274                    scall = GBS_global_string("%s |%s; rm -f %s", a2ps_call, printer, tmp_file);
275                    free(printer);
276                    break;
277                }
278                case AWT_APRINT_DEST_FILE_PS: {
279                    char *file       = printFile(awr);
280                    char *quotedFile = GBK_singlequote(file);
281                    fprintf(stderr, "Printing to PS file %s\n", quotedFile);
282                    scall            = GBS_global_string("%s >%s;rm -f %s", a2ps_call, quotedFile, tmp_file);
283                    free(quotedFile);
284                    free(file);
285                    break;
286                }
287                case AWT_APRINT_DEST_PREVIEW: {
288                    char *tmp_file2;
289                    {
290                        char *name_only;
291                        GB_split_full_path(tmp_file, NULL, NULL, &name_only, NULL);
292                        tmp_file2 = GB_create_tempfile(GBS_global_string("%s.ps", name_only));
293                        free(name_only);
294                    }
295
296                    if (!tmp_file2) error = GB_await_error();
297                    else {
298                        scall = GBS_global_string("%s >%s;(%s %s;rm -f %s %s)&",
299                                                  a2ps_call, tmp_file2,
300                                                  GB_getenvARB_GS(), tmp_file2,
301                                                  tmp_file, tmp_file2);
302                        free(tmp_file2);
303                    }
304                    break;
305                }
306                default:
307                    awt_assert(0);
308                    break;
309            }
310
311            if (scall) error = GBK_system(scall);
312            free(a2ps_call);
313        }
314        if (error) aw_message(error);
315        free(tmp_file);
316    }
317    free(text);
318}
319
320static void cutExt(char *name, const char *removeExt) {
321    char *found = strstr(name, removeExt);
322    if (found && strcmp(found, removeExt) == 0) {
323        found[0] = 0;
324    }
325}
326static char *correct_extension(const char *name, const char *newExt) {
327    char *noExt = strdup(name);
328    cutExt(noExt, ".ps");
329    cutExt(noExt, ".txt");
330
331    char *result = (char*)malloc(strlen(noExt)+strlen(newExt)+1);
332    strcpy(result, noExt);
333    strcat(result, newExt);
334    if (strcmp(name, result) == 0) freenull(result);
335
336    free(noExt);
337    return result;
338}
339
340static void aps_correct_filename(AW_root *aw_root) {
341    int type = aw_root->awar(AWAR_APRINT_PRINTTO)->read_int();
342    if (type == AWT_APRINT_DEST_FILE_PS || type == AWT_APRINT_DEST_FILE_ASCII) {
343        AW_awar    *awar_name = aw_root->awar(AWAR_APRINT_FILE);
344        const char *name      = awar_name->read_char_pntr();
345        char       *new_name  = NULL;
346
347        if (type == AWT_APRINT_DEST_FILE_PS) {
348            new_name = correct_extension(name, ".ps");
349        }
350        else {
351            awt_assert((type == AWT_APRINT_DEST_FILE_ASCII));
352            new_name = correct_extension(name, ".txt");
353        }
354        if (new_name) {
355            awar_name->write_string(new_name);
356            free(new_name);
357        }
358    }
359}
360
361void AWT_create_ascii_print_window(AW_root *awr, const char *text_to_print, const char *title) {
362    static AW_window_simple *aws = 0;
363
364    awr->awar_string(AWAR_APRINT_TEXT)->write_string(text_to_print);
365    if (title) {
366        awr->awar_string(AWAR_APRINT_TITLE)->write_string(title);
367    }
368    if (aws) {
369        awr->awar_float(AWAR_APRINT_DX)->write_float(1.0);
370    }
371    else {
372        aws = new AW_window_simple;
373        aws->init(awr, "PRINT", "PRINT");
374        aws->load_xfig("awt/ascii_print.fig");
375        awr->awar_string(AWAR_APRINT_TITLE);
376        awr->awar_string(AWAR_APRINT_TEXT)                                  ->add_callback(awt_aps_text_changed);
377
378        awr->awar_int(AWAR_APRINT_PAPER_SIZE, (int)AWT_APRINT_PAPERSIZE_A4) ->add_callback(awt_aps_set_magnification_to_fit_xpage);
379        awr->awar_int(AWAR_APRINT_MAGNIFICATION, 100)                       ->add_callback(awt_aps_calc_pages_needed);
380        awr->awar_int(AWAR_APRINT_PAGES, 1);
381        awr->awar_int(AWAR_APRINT_SX, 1);
382        awr->awar_int(AWAR_APRINT_SY, 1);
383
384        awr->awar_float(AWAR_APRINT_DX, 1.0);
385        awr->awar_float(AWAR_APRINT_DY, 1.0);
386
387        awr->awar_string(AWAR_APRINT_FILE, "print.ps")->add_callback(aps_correct_filename);
388
389        awr->awar_int(AWAR_APRINT_ORIENTATION, (int)AWT_APRINT_ORIENTATION_PORTRAIT)->add_callback(awt_aps_set_magnification_to_fit_xpage);
390        awr->awar_int(AWAR_APRINT_PRINTTO, int(AWT_APRINT_DEST_PRINTER))->add_callback(aps_correct_filename);
391        aps_correct_filename(awr);
392
393        {
394            char *print_command;
395            if (getenv("PRINTER")) {
396                print_command = GBS_eval_env("lpr -h -P$(PRINTER)");
397            }
398            else {
399                print_command = strdup("lpr -h");
400            }
401
402            awr->awar_string(AWAR_APRINT_PRINTER, print_command);
403            free(print_command);
404        }
405
406        awt_aps_text_changed(awr);
407
408        aws->at("close");
409        aws->callback(AW_POPDOWN);
410        aws->create_button("CLOSE", "CLOSE");
411
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(0, AWAR_APRINT_SY);
430
431        aws->at("columns");
432        aws->create_button(0, 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, 1);
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, 1);
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(0, 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
484void AWT_show_file(AW_root *awr, const char *filename) {
485    char *text = GB_read_file(filename);
486    if (!text) {
487        aw_message(GB_await_error());
488    }
489    else {
490        AWT_create_ascii_print_window(awr, text, filename);
491        free(text);
492    }
493}
Note: See TracBrowser for help on using the repository browser.