source: tags/ms_r18q1/AWT/AWT_asciiprint.cxx

Last change on this file was 16766, checked in by westram, 6 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.2 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    }
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;
142    awt_aps_set_magnification_to_fit_xpage(awr);
143}
144
145static 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
163static 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
169static 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
175    AWT_asciiprint_destination dest = (AWT_asciiprint_destination)awr->awar(AWAR_APRINT_PRINTTO)->read_int();
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            {
251                AWT_asciiprint_orientation ori = AWT_asciiprint_orientation(awr->awar(AWAR_APRINT_ORIENTATION)->read_int());
252                const char *oristring = "";
253                switch (ori) {
254                    case AWT_APRINT_ORIENTATION_PORTRAIT:
255                        oristring = "-p -1 ";
256                        break;
257                    case AWT_APRINT_ORIENTATION_LANDSCAPE:
258                        oristring = "-l -1 ";
259                        break;
260                    case AWT_APRINT_ORIENTATION_DOUBLE_PORTRAIT:
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) {
273                case AWT_APRINT_DEST_PRINTER: {
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                }
279                case AWT_APRINT_DEST_FILE_PS: {
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                }
288                case AWT_APRINT_DEST_PREVIEW: {
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
321static 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}
327static 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
341static 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 {
352            awt_assert((type == AWT_APRINT_DEST_FILE_ASCII));
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
362void 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) {
367        awr->awar_string(AWAR_APRINT_TITLE)->write_string(title);
368    }
369    if (aws) {
370        awr->awar_float(AWAR_APRINT_DX)->write_float(1.0);
371    }
372    else {
373        aws = new AW_window_simple;
374        aws->init(awr, "PRINT", "PRINT");
375        aws->load_xfig("awt/ascii_print.fig");
376        awr->awar_string(AWAR_APRINT_TITLE);
377        awr->awar_string(AWAR_APRINT_TEXT)                                  ->add_callback(awt_aps_text_changed);
378
379        awr->awar_int(AWAR_APRINT_PAPER_SIZE, (int)AWT_APRINT_PAPERSIZE_A4) ->add_callback(awt_aps_set_magnification_to_fit_xpage);
380        awr->awar_int(AWAR_APRINT_MAGNIFICATION, 100)                       ->add_callback(awt_aps_calc_pages_needed);
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
388        awr->awar_string(AWAR_APRINT_FILE, "print.ps")->add_callback(aps_correct_filename);
389
390        awr->awar_int(AWAR_APRINT_ORIENTATION, (int)AWT_APRINT_ORIENTATION_PORTRAIT)->add_callback(awt_aps_set_magnification_to_fit_xpage);
391        awr->awar_int(AWAR_APRINT_PRINTTO, int(AWT_APRINT_DEST_PRINTER))->add_callback(aps_correct_filename);
392        aps_correct_filename(awr);
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
407        awt_aps_text_changed(awr);
408
409        aws->at("close");
410        aws->callback(AW_POPDOWN);
411        aws->create_button("CLOSE", "CLOSE");
412
413
414        aws->at("help");
415        aws->callback(makeHelpCallback("asciiprint.hlp"));
416        aws->create_button("HELP", "HELP");
417
418        aws->at("go");
419        aws->callback(awt_aps_go);
420        aws->create_button("PRINT", "PRINT");
421
422        aws->at("title");
423        aws->create_input_field(AWAR_APRINT_TITLE);
424
425        aws->at("text");
426        aws->create_text_field(AWAR_APRINT_TEXT);
427
428        aws->button_length(5);
429        aws->at("rows");
430        aws->create_button(NULp, AWAR_APRINT_SY);
431
432        aws->at("columns");
433        aws->create_button(NULp, AWAR_APRINT_SX);
434
435        aws->at("magnification");
436        aws->create_input_field(AWAR_APRINT_MAGNIFICATION, 4);
437
438        aws->at("paper_size");
439        {
440            aws->create_toggle_field(AWAR_APRINT_PAPER_SIZE, 1);
441            aws->insert_toggle("A4", "A", int(AWT_APRINT_PAPERSIZE_A4));
442            aws->insert_toggle("US", "U", int(AWT_APRINT_PAPERSIZE_US));
443            aws->update_toggle_field();
444        }
445
446        aws->at("orientation");
447        {
448            aws->create_toggle_field(AWAR_APRINT_ORIENTATION, 1);
449            aws->insert_toggle("#print/portrait.xpm", "P", int(AWT_APRINT_ORIENTATION_PORTRAIT));
450            aws->insert_toggle("#print/landscape.xpm", "P", int(AWT_APRINT_ORIENTATION_LANDSCAPE));
451            aws->update_toggle_field();
452        }
453
454
455        aws->at("pages");
456        aws->create_button(NULp, AWAR_APRINT_PAGES);
457
458        aws->at("dcol");
459        aws->callback(awt_aps_set_magnification_to_fit_xpage); // @@@ used as INPUTFIELD_CB (see #559)
460        aws->create_input_field(AWAR_APRINT_DX, 4);
461
462        aws->at("drows");
463        aws->callback(awt_aps_set_magnification_to_fit_ypage); // @@@ used as INPUTFIELD_CB (see #559)
464        aws->create_input_field(AWAR_APRINT_DY, 4);
465
466
467        aws->at("printto");
468        aws->create_toggle_field(AWAR_APRINT_PRINTTO);
469        aws->insert_toggle("Printer", "P", int(AWT_APRINT_DEST_PRINTER));
470        aws->insert_toggle("File (Postscript)", "F", int(AWT_APRINT_DEST_FILE_PS));
471        aws->insert_toggle("File (ASCII)", "A", int(AWT_APRINT_DEST_FILE_ASCII));
472        aws->insert_toggle("Preview", "V", int(AWT_APRINT_DEST_PREVIEW));
473        aws->update_toggle_field();
474
475        aws->at("printer");
476        aws->create_input_field(AWAR_APRINT_PRINTER, 16);
477
478        aws->at("filename");
479        aws->create_input_field(AWAR_APRINT_FILE, 16);
480    }
481    aws->activate();
482}
483
484
485void AWT_show_file(AW_root *awr, const char *filename) {
486    char *text = GB_read_file(filename);
487    if (!text) {
488        aw_message(GB_await_error());
489    }
490    else {
491        AWT_create_ascii_print_window(awr, text, filename);
492        free(text);
493    }
494}
Note: See TracBrowser for help on using the repository browser.