source: tags/arb-6.0/AWT/AWT_asciiprint.cxx

Last change on this file was 10863, checked in by westram, 11 years ago
  • replaced usage of untyped AW_POPUP_HELP
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.3 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(double(x)/default_cpp);
71    awr->awar(AWAR_APRINT_DY)->write_float(double(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 AWT_write_file(const char *filename, const char *file) {
145    FILE *f = fopen(filename, "r");
146    if (f) {
147        fclose(f);
148        if (aw_question("overwrite_file", GBS_global_string("File '%s' already exist", filename), "Overwrite,Cancel")) {
149            return;
150        }
151    }
152    f = fopen(filename, "w");
153    if (!f) {
154        aw_message(GBS_global_string("Cannot write to '%s'", filename));
155        return;
156    }
157    fprintf(f, "%s", file);
158    fclose(f);
159}
160
161static void awt_aps_go(AW_window *aww) {
162    AW_root *awr  = aww->get_root();
163    char    *text = awr->awar(AWAR_APRINT_TEXT)->read_string();
164
165    freeset(text, GBS_replace_tabs_by_spaces(text));
166
167    AWT_asciiprint_destination dest = (AWT_asciiprint_destination)awr->awar(AWAR_APRINT_PRINTTO)->read_int();
168    if (dest == AWT_APRINT_DEST_AFILE) {
169        char *file = awr->awar(AWAR_APRINT_FILE)->read_string();
170        AWT_write_file(file, text);
171        free(file);
172    }
173    else {
174        char *tmp_file;
175        FILE *tmpf;
176        {
177            char *name = GB_unique_filename("arb_aprint", "txt");
178            tmpf = GB_fopen_tempfile(name, "wt", &tmp_file);
179            free(name);
180        }
181
182        GB_ERROR error = NULL;
183        if (!tmpf) {
184            error = GBS_global_string("awt_aps_go: %s", GB_await_error());
185        }
186        else {
187            char *y_begin = text;
188            int last_y = 0;
189
190            double xy_ratio = awt_aps_get_xy_ratio(awr);
191            int    mag      = awr->awar(AWAR_APRINT_MAGNIFICATION)->read_int();
192
193            int default_lpp = awt_aps_get_default_lines_per_page(awr);
194            int default_cpp = int(default_lpp * xy_ratio);
195            default_cpp     = default_cpp * 100 / mag;
196            default_lpp     = default_lpp * 100 / mag;
197
198            int text_width  = awr->awar(AWAR_APRINT_SX)->read_int();
199            int text_height = awr->awar(AWAR_APRINT_SY)->read_int();
200
201            int x;
202            int y;
203
204            for (y = 0; y < text_height; y += default_lpp) {
205                while (last_y < y) {
206                    last_y ++;
207                    y_begin = strchr(y_begin, '\n');
208                    if (!y_begin) break;
209                    y_begin++;
210                }
211                if (!y_begin) break;
212
213                for (x = 0; x < text_width; x += default_cpp) {
214                    char *line = y_begin;
215                    int i;
216                    for (i=0; i<default_lpp; i++) {
217                        if (line) {
218                            char *next_line = strchr(line, '\n');
219                            int line_length;
220                            if (next_line) {
221                                line_length = next_line - line; // exclusive '\n'
222                                next_line ++;
223                            }
224                            else {
225                                line_length = strlen(line);
226                            }
227                            if (line_length > x + default_cpp) {
228                                line_length = x + default_cpp;
229                            }
230                            if (line_length > x) {
231                                fwrite(line + x, sizeof(char), line_length - x, tmpf);
232                            }
233                            line = next_line;
234                        }
235                        fprintf(tmpf, "\n");
236                    }
237                }
238            }
239            fclose(tmpf);
240
241            char *a2ps_call = 0;
242            {
243                AWT_asciiprint_orientation ori = AWT_asciiprint_orientation(awr->awar(AWAR_APRINT_ORIENTATION)->read_int());
244                const char *oristring = "";
245                switch (ori) {
246                    case AWT_APRINT_ORIENTATION_PORTRAIT:
247                        oristring = "-p -1 ";
248                        break;
249                    case AWT_APRINT_ORIENTATION_LANDSCAPE:
250                        oristring = "-l -1 ";
251                        break;
252                    case AWT_APRINT_ORIENTATION_DOUBLE_PORTRAIT:
253                        oristring = "-p -2 ";
254                        break;
255                }
256                char *header = awr->awar(AWAR_APRINT_TITLE)->read_string();
257                a2ps_call = GBS_global_string_copy("arb_a2ps -ns -nP '-H%s' %s -l%i %s",
258                                                   header, oristring, default_lpp, tmp_file);
259                free(header);
260            }
261
262            const char *scall = 0;
263            switch (dest) {
264                case AWT_APRINT_DEST_PRINTER: {
265                    char *printer = awr->awar(AWAR_APRINT_PRINTER)->read_string();
266                    scall = GBS_global_string("%s |%s; rm -f %s", a2ps_call, printer, tmp_file);
267                    free(printer);
268                    break;
269                }
270                case AWT_APRINT_DEST_FILE: {
271                    char *file = awr->awar(AWAR_APRINT_FILE)->read_string();
272                    scall = GBS_global_string("%s >%s;rm -f %s", a2ps_call, file, tmp_file);
273                    free(file);
274                    break;
275                }
276                case AWT_APRINT_DEST_PREVIEW: {
277                    char *tmp_file2;
278                    {
279                        char *name_only;
280                        GB_split_full_path(tmp_file, NULL, NULL, &name_only, NULL);
281                        tmp_file2 = GB_create_tempfile(GBS_global_string("%s.ps", name_only));
282                        free(name_only);
283                    }
284
285                    if (!tmp_file2) error = GB_await_error();
286                    else {
287                        scall = GBS_global_string("%s >%s;(%s %s;rm -f %s %s)&",
288                                                  a2ps_call, tmp_file2,
289                                                  GB_getenvARB_GS(), tmp_file2,
290                                                  tmp_file, tmp_file2);
291                        free(tmp_file2);
292                    }
293                    break;
294                }
295                default:
296                    awt_assert(0);
297                    break;
298            }
299
300            if (scall) {
301                GB_informationf("executing '%s'", scall);
302                if (system(scall) != 0) error = GBS_global_string("Error while calling '%s'", scall);
303            }
304
305            free(a2ps_call);
306        }
307        if (error) aw_message(error);
308        free(tmp_file);
309    }
310    free(text);
311}
312
313void AWT_create_ascii_print_window(AW_root *awr, const char *text_to_print, const char *title) {
314    static AW_window_simple *aws = 0;
315
316    awr->awar_string(AWAR_APRINT_TEXT)->write_string(text_to_print);
317    if (title) {
318        awr->awar_string(AWAR_APRINT_TITLE)->write_string(title);
319    }
320    if (aws) {
321        awr->awar_float(AWAR_APRINT_DX)->write_float(1.0);
322    }
323    else {
324        aws = new AW_window_simple;
325        aws->init(awr, "PRINT", "PRINT");
326        aws->load_xfig("awt/ascii_print.fig");
327        awr->awar_string(AWAR_APRINT_TITLE);
328        awr->awar_string(AWAR_APRINT_TEXT)                                  ->add_callback(awt_aps_text_changed);
329
330        awr->awar_int(AWAR_APRINT_PAPER_SIZE, (int)AWT_APRINT_PAPERSIZE_A4) ->add_callback(awt_aps_set_magnification_to_fit_xpage);
331        awr->awar_int(AWAR_APRINT_MAGNIFICATION, 100)                       ->add_callback(awt_aps_calc_pages_needed);
332        awr->awar_int(AWAR_APRINT_PAGES, 1);
333        awr->awar_int(AWAR_APRINT_SX, 1);
334        awr->awar_int(AWAR_APRINT_SY, 1);
335
336        awr->awar_float(AWAR_APRINT_DX, 1.0);
337        awr->awar_float(AWAR_APRINT_DY, 1.0);
338
339        awr->awar_int(AWAR_APRINT_ORIENTATION, (int)AWT_APRINT_ORIENTATION_PORTRAIT)->add_callback(awt_aps_set_magnification_to_fit_xpage);
340        awr->awar_int(AWAR_APRINT_PRINTTO, int(AWT_APRINT_DEST_PRINTER));
341        {
342            char *print_command;
343            if (getenv("PRINTER")) {
344                print_command = GBS_eval_env("lpr -h -P$(PRINTER)");
345            }
346            else {
347                print_command = strdup("lpr -h");
348            }
349
350            awr->awar_string(AWAR_APRINT_PRINTER, print_command);
351            free(print_command);
352        }
353        awr->awar_string(AWAR_APRINT_FILE, "print.ps");
354
355        awt_aps_text_changed(awr);
356
357        aws->at("close");
358        aws->callback(AW_POPDOWN);
359        aws->create_button("CLOSE", "CLOSE");
360
361
362        aws->at("help");
363        aws->callback(makeHelpCallback("asciiprint.hlp"));
364        aws->create_button("HELP", "HELP");
365
366        aws->at("go");
367        aws->callback(awt_aps_go);
368        aws->create_button("PRINT", "PRINT");
369
370        aws->at("title");
371        aws->create_input_field(AWAR_APRINT_TITLE);
372
373        aws->at("text");
374        aws->create_text_field(AWAR_APRINT_TEXT);
375
376        aws->button_length(5);
377        aws->at("rows");
378        aws->create_button(0, AWAR_APRINT_SY);
379
380        aws->at("columns");
381        aws->create_button(0, AWAR_APRINT_SX);
382
383        aws->at("magnification");
384        aws->create_input_field(AWAR_APRINT_MAGNIFICATION, 4);
385
386        aws->at("paper_size");
387        {
388            aws->create_toggle_field(AWAR_APRINT_PAPER_SIZE, 1);
389            aws->insert_toggle("A4", "A", int(AWT_APRINT_PAPERSIZE_A4));
390            aws->insert_toggle("US", "U", int(AWT_APRINT_PAPERSIZE_US));
391            aws->update_toggle_field();
392        }
393
394        aws->at("orientation");
395        {
396            aws->create_toggle_field(AWAR_APRINT_ORIENTATION, 1);
397            aws->insert_toggle("#print/portrait.xpm", "P", int(AWT_APRINT_ORIENTATION_PORTRAIT));
398            aws->insert_toggle("#print/landscape.xpm", "P", int(AWT_APRINT_ORIENTATION_LANDSCAPE));
399            aws->update_toggle_field();
400        }
401
402
403        aws->at("pages");
404        aws->create_button(0, AWAR_APRINT_PAGES);
405
406        aws->at("dcol");
407        aws->callback(awt_aps_set_magnification_to_fit_xpage);
408        aws->create_input_field(AWAR_APRINT_DX, 4);
409
410        aws->at("drows");
411        aws->callback(awt_aps_set_magnification_to_fit_ypage);
412        aws->create_input_field(AWAR_APRINT_DY, 4);
413
414
415        aws->at("printto");
416        aws->create_toggle_field(AWAR_APRINT_PRINTTO);
417        aws->insert_toggle("Printer", "P", int(AWT_APRINT_DEST_PRINTER));
418        aws->insert_toggle("File (Postscript)", "F", int(AWT_APRINT_DEST_FILE));
419        aws->insert_toggle("File (ASCII)", "A", int(AWT_APRINT_DEST_AFILE));
420        aws->insert_toggle("Preview", "V", int(AWT_APRINT_DEST_PREVIEW));
421        aws->update_toggle_field();
422
423        aws->at("printer");
424        aws->create_input_field(AWAR_APRINT_PRINTER, 16);
425
426        aws->at("filename");
427        aws->create_input_field(AWAR_APRINT_FILE, 16);
428    }
429    aws->activate();
430}
431
432
433void AWT_show_file(AW_root *awr, const char *filename) {
434    char *text = GB_read_file(filename);
435    if (!text) {
436        aw_message(GB_await_error());
437    }
438    else {
439        AWT_create_ascii_print_window(awr, text, filename);
440        free(text);
441    }
442}
Note: See TracBrowser for help on using the repository browser.