| 1 | #include <stdio.h> |
|---|
| 2 | #include <stdlib.h> |
|---|
| 3 | #include <string.h> |
|---|
| 4 | |
|---|
| 5 | #include <arbdb.h> |
|---|
| 6 | #include <arbdbt.h> |
|---|
| 7 | |
|---|
| 8 | #include <aw_root.hxx> |
|---|
| 9 | #include <aw_device.hxx> |
|---|
| 10 | #include <aw_window.hxx> |
|---|
| 11 | #include "awt_canvas.hxx" |
|---|
| 12 | #include "awt.hxx" |
|---|
| 13 | |
|---|
| 14 | // -------------------------------------------------------------------------------- |
|---|
| 15 | |
|---|
| 16 | enum PrintDest { |
|---|
| 17 | PDEST_PRINTER = 0, |
|---|
| 18 | PDEST_POSTSCRIPT = 1, |
|---|
| 19 | PDEST_PREVIEW = 2, |
|---|
| 20 | }; |
|---|
| 21 | |
|---|
| 22 | // -------------------------------------------------------------------------------- |
|---|
| 23 | |
|---|
| 24 | static void awt_print_tree_check_size(void *, AW_CL cl_ntw) { |
|---|
| 25 | AWT_canvas *ntw = (AWT_canvas*)cl_ntw; |
|---|
| 26 | GB_transaction dummy2(ntw->gb_main); |
|---|
| 27 | AW_world size; |
|---|
| 28 | long what = ntw->aww->get_root()->awar(AWAR_PRINT_TREE_CLIP)->read_int(); |
|---|
| 29 | |
|---|
| 30 | AW_device *size_device = ntw->aww->get_size_device(AW_MIDDLE_AREA); |
|---|
| 31 | |
|---|
| 32 | if (what){ |
|---|
| 33 | size_device->reset(); |
|---|
| 34 | size_device->zoom(ntw->trans_to_fit); |
|---|
| 35 | size_device->set_filter(AW_SCREEN); |
|---|
| 36 | ntw->tree_disp->show(size_device); |
|---|
| 37 | size_device->get_size_information(&size); |
|---|
| 38 | }else{ |
|---|
| 39 | size_device->get_area_size(&size); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | ntw->aww->get_root()->awar(AWAR_PRINT_TREE_GSIZEX)->write_float((size.r-size.l + 30)/80); |
|---|
| 43 | ntw->aww->get_root()->awar(AWAR_PRINT_TREE_GSIZEY)->write_float((size.b-size.t + 30)/80); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | inline int xy2pages(double sx, double sy) { |
|---|
| 47 | return (int(sx+0.99)*int(sy+0.99)); |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | void awt_page_size_check_cb(AW_root *awr) { |
|---|
| 51 | bool landscape = awr->awar(AWAR_PRINT_TREE_LANDSCAPE)->read_int(); |
|---|
| 52 | double px = awr->awar(AWAR_PRINT_TREE_PSIZEX)->read_float(); |
|---|
| 53 | double py = awr->awar(AWAR_PRINT_TREE_PSIZEY)->read_float(); |
|---|
| 54 | |
|---|
| 55 | if (landscape != (px>py)) { |
|---|
| 56 | awr->awar(AWAR_PRINT_TREE_PSIZEX)->write_float(py); // recalls this function |
|---|
| 57 | awr->awar(AWAR_PRINT_TREE_PSIZEY)->write_float(px); |
|---|
| 58 | return; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | long magnification = awr->awar(AWAR_PRINT_TREE_MAGNIFICATION)->read_int(); |
|---|
| 62 | double gx = awr->awar(AWAR_PRINT_TREE_GSIZEX)->read_float(); |
|---|
| 63 | double gy = awr->awar(AWAR_PRINT_TREE_GSIZEY)->read_float(); |
|---|
| 64 | |
|---|
| 65 | double ox = (gx*magnification)/100; // resulting size of output in inches |
|---|
| 66 | double oy = (gy*magnification)/100; |
|---|
| 67 | |
|---|
| 68 | double sx = 0; // resulting pages |
|---|
| 69 | double sy = 0; |
|---|
| 70 | |
|---|
| 71 | bool useOverlap = awr->awar(AWAR_PRINT_TREE_OVERLAP)->read_int(); |
|---|
| 72 | if (useOverlap) { |
|---|
| 73 | double overlapAmount = awr->awar(AWAR_PRINT_TREE_OVERLAP_AMOUNT)->read_float(); |
|---|
| 74 | |
|---|
| 75 | if (overlapAmount >= px || overlapAmount >= py) { |
|---|
| 76 | aw_message("Overlap amount bigger than paper size. Please fix!"); |
|---|
| 77 | } |
|---|
| 78 | else { |
|---|
| 79 | while (ox>px) { ox = ox-px+overlapAmount; sx += 1.0; } |
|---|
| 80 | while (oy>py) { oy = oy-py+overlapAmount; sy += 1.0; } |
|---|
| 81 | sx += ox/px; |
|---|
| 82 | sy += oy/py; |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | else { |
|---|
| 86 | sx = ox/px; |
|---|
| 87 | sy = oy/py; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | awr->awar(AWAR_PRINT_TREE_SIZEX)->write_float(sx); |
|---|
| 91 | awr->awar(AWAR_PRINT_TREE_SIZEY)->write_float(sy); |
|---|
| 92 | |
|---|
| 93 | awr->awar(AWAR_PRINT_TREE_PAGES)->write_int(xy2pages(sx, sy)); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | // -------------------------------------------------------------------------------- |
|---|
| 97 | |
|---|
| 98 | static bool export_awars_created = false; |
|---|
| 99 | static bool print_awars_created = false; |
|---|
| 100 | |
|---|
| 101 | static void create_export_awars(AW_root *awr) { |
|---|
| 102 | if (!export_awars_created) { |
|---|
| 103 | AW_default def = AW_ROOT_DEFAULT; |
|---|
| 104 | |
|---|
| 105 | awr->awar_int(AWAR_PRINT_TREE_CLIP, 0, def); |
|---|
| 106 | awr->awar_int(AWAR_PRINT_TREE_HANDLES, 1, def); |
|---|
| 107 | awr->awar_int(AWAR_PRINT_TREE_COLOR, 1, def); |
|---|
| 108 | |
|---|
| 109 | awr->awar_string(AWAR_PRINT_TREE_FILE_NAME, "print.fig", def); |
|---|
| 110 | awr->awar_string(AWAR_PRINT_TREE_FILE_DIR, "", def); |
|---|
| 111 | awr->awar_string(AWAR_PRINT_TREE_FILE_FILTER, "fig", def); |
|---|
| 112 | |
|---|
| 113 | awr->awar_int(AWAR_PRINT_TREE_LANDSCAPE, 0, def); |
|---|
| 114 | awr->awar_int(AWAR_PRINT_TREE_MAGNIFICATION, 100, def); |
|---|
| 115 | |
|---|
| 116 | // constraints: |
|---|
| 117 | |
|---|
| 118 | awr->awar(AWAR_PRINT_TREE_MAGNIFICATION)->set_minmax(1, 10000); |
|---|
| 119 | |
|---|
| 120 | export_awars_created = true; |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | static void resetFiletype(AW_root *awr, const char *filter, const char *defaultFilename) { |
|---|
| 125 | AW_awar *awar_filter = awr->awar(AWAR_PRINT_TREE_FILE_FILTER); |
|---|
| 126 | char *current_filter = awar_filter->read_string(); |
|---|
| 127 | |
|---|
| 128 | if (strcmp(current_filter, filter) != 0) { |
|---|
| 129 | awar_filter->write_string(filter); |
|---|
| 130 | awr->awar(AWAR_PRINT_TREE_FILE_NAME)->write_string(defaultFilename); |
|---|
| 131 | } |
|---|
| 132 | free(current_filter); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | static void create_print_awars(AW_root *awr, AWT_canvas *ntw) { |
|---|
| 136 | create_export_awars(awr); |
|---|
| 137 | |
|---|
| 138 | if (!print_awars_created) { |
|---|
| 139 | AW_default def = AW_ROOT_DEFAULT; |
|---|
| 140 | |
|---|
| 141 | awr->awar_int(AWAR_PRINT_TREE_PAGES, 1, def); |
|---|
| 142 | awr->awar_int(AWAR_PRINT_TREE_OVERLAP, 1, def); |
|---|
| 143 | awr->awar_float(AWAR_PRINT_TREE_OVERLAP_AMOUNT, 0.82, def); |
|---|
| 144 | |
|---|
| 145 | awr->awar_float(AWAR_PRINT_TREE_GSIZEX); |
|---|
| 146 | awr->awar_float(AWAR_PRINT_TREE_GSIZEY); |
|---|
| 147 | |
|---|
| 148 | // default paper size (A4 = 8.27*11.69) |
|---|
| 149 | // using 'preview' determined the following values (fitting 1 page!): |
|---|
| 150 | awr->awar_float(AWAR_PRINT_TREE_PSIZEX, 7.5); |
|---|
| 151 | awr->awar_float(AWAR_PRINT_TREE_PSIZEY, 10.5); |
|---|
| 152 | |
|---|
| 153 | awr->awar_float(AWAR_PRINT_TREE_SIZEX); |
|---|
| 154 | awr->awar_float(AWAR_PRINT_TREE_SIZEY); |
|---|
| 155 | |
|---|
| 156 | awr->awar_int(AWAR_PRINT_TREE_DEST); |
|---|
| 157 | |
|---|
| 158 | { |
|---|
| 159 | char *print_command; |
|---|
| 160 | if (getenv("PRINTER")){ |
|---|
| 161 | print_command = GBS_eval_env("lpr -h -P$(PRINTER)"); |
|---|
| 162 | }else print_command = strdup("lpr -h"); |
|---|
| 163 | |
|---|
| 164 | awr->awar_string(AWAR_PRINT_TREE_PRINTER, print_command, def); |
|---|
| 165 | free(print_command); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | // constraints and automatics: |
|---|
| 169 | |
|---|
| 170 | awr->awar(AWAR_PRINT_TREE_PSIZEX)->set_minmax(0.1, 100); |
|---|
| 171 | awr->awar(AWAR_PRINT_TREE_PSIZEY)->set_minmax(0.1, 100); |
|---|
| 172 | |
|---|
| 173 | awt_print_tree_check_size(0, (AW_CL)ntw); |
|---|
| 174 | |
|---|
| 175 | awr->awar(AWAR_PRINT_TREE_CLIP)->add_callback((AW_RCB1)awt_print_tree_check_size, (AW_CL)ntw); |
|---|
| 176 | |
|---|
| 177 | { // add callbacks for page recalculation |
|---|
| 178 | const char *checked_awars[] = { |
|---|
| 179 | AWAR_PRINT_TREE_MAGNIFICATION, AWAR_PRINT_TREE_LANDSCAPE, |
|---|
| 180 | AWAR_PRINT_TREE_OVERLAP, AWAR_PRINT_TREE_OVERLAP_AMOUNT, |
|---|
| 181 | AWAR_PRINT_TREE_PSIZEX, AWAR_PRINT_TREE_PSIZEY, |
|---|
| 182 | AWAR_PRINT_TREE_GSIZEX, AWAR_PRINT_TREE_GSIZEY, |
|---|
| 183 | 0 |
|---|
| 184 | }; |
|---|
| 185 | for (int ca = 0; checked_awars[ca]; ca++) { |
|---|
| 186 | awr->awar(checked_awars[ca])->add_callback(awt_page_size_check_cb); |
|---|
| 187 | } |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | awt_page_size_check_cb(awr); |
|---|
| 191 | |
|---|
| 192 | print_awars_created = true; |
|---|
| 193 | } |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | // -------------------------------------------------------------------------------- |
|---|
| 197 | |
|---|
| 198 | const char *AWT_print_tree_to_file(AW_window *aww, AWT_canvas * ntw) { |
|---|
| 199 | // export to xfig |
|---|
| 200 | GB_transaction ta(ntw->gb_main); |
|---|
| 201 | |
|---|
| 202 | AW_root *awr = aww->get_root(); |
|---|
| 203 | char *dest = awt_get_selected_fullname(awr, AWAR_PRINT_TREE_FILE_BASE); |
|---|
| 204 | GB_ERROR error = 0; |
|---|
| 205 | |
|---|
| 206 | if (dest[0] == 0) { |
|---|
| 207 | error = "Please enter a file name"; |
|---|
| 208 | } |
|---|
| 209 | else { |
|---|
| 210 | long what = awr->awar(AWAR_PRINT_TREE_CLIP)->read_int(); |
|---|
| 211 | long handles = awr->awar(AWAR_PRINT_TREE_HANDLES)->read_int(); |
|---|
| 212 | int use_color = awr->awar(AWAR_PRINT_TREE_COLOR)->read_int(); |
|---|
| 213 | |
|---|
| 214 | AW_device *device = ntw->aww->get_print_device(AW_MIDDLE_AREA); |
|---|
| 215 | AW_device *size_device = ntw->aww->get_size_device(AW_MIDDLE_AREA); |
|---|
| 216 | |
|---|
| 217 | device->reset(); |
|---|
| 218 | device->set_color_mode(use_color==1); |
|---|
| 219 | error = device->open(dest); |
|---|
| 220 | device->line(0, 0, 0, 1, -1); // dummy point upper left corner |
|---|
| 221 | |
|---|
| 222 | if (what) { // draw all |
|---|
| 223 | AW_world size; |
|---|
| 224 | size_device->reset(); |
|---|
| 225 | size_device->zoom(ntw->trans_to_fit); |
|---|
| 226 | size_device->set_filter(AW_SCREEN); |
|---|
| 227 | ntw->tree_disp->show(size_device); |
|---|
| 228 | size_device->get_size_information(&size); |
|---|
| 229 | size.l -= 50; |
|---|
| 230 | size.t -= 40; // expand pic |
|---|
| 231 | size.r += 20; |
|---|
| 232 | size.b += 20; |
|---|
| 233 | device->set_offset(AW::Vector(size.l, size.t) / -ntw->trans_to_fit); |
|---|
| 234 | device->set_bottom_clip_border((int)(size.b-size.t), true); |
|---|
| 235 | device->set_right_clip_border((int)(size.r-size.l), true); |
|---|
| 236 | device->zoom(ntw->trans_to_fit); |
|---|
| 237 | } |
|---|
| 238 | else { |
|---|
| 239 | ntw->init_device(device); // draw screen |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | if (!error) { |
|---|
| 243 | if (handles) { |
|---|
| 244 | device->set_filter(AW_PRINTER | AW_PRINTER_EXT); |
|---|
| 245 | } |
|---|
| 246 | else { |
|---|
| 247 | device->set_filter(AW_PRINTER); |
|---|
| 248 | } |
|---|
| 249 | ntw->tree_disp->show(device); |
|---|
| 250 | device->close(); |
|---|
| 251 | awr->awar(AWAR_PRINT_TREE_FILE_DIR)->touch(); // reload dir !!! |
|---|
| 252 | } |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | if (error) aw_message(error); |
|---|
| 256 | |
|---|
| 257 | free(dest); |
|---|
| 258 | |
|---|
| 259 | return error; |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | void AWT_print_tree_to_file_xfig(AW_window *aww, AW_CL cl_ntw){ |
|---|
| 263 | AWT_canvas * ntw = (AWT_canvas*)cl_ntw; |
|---|
| 264 | AW_root *awr = aww->get_root(); |
|---|
| 265 | const char *error = AWT_print_tree_to_file(aww, ntw); |
|---|
| 266 | if (!error) { |
|---|
| 267 | char *dest = awt_get_selected_fullname(awr, AWAR_PRINT_TREE_FILE_BASE); |
|---|
| 268 | system(GBS_global_string("xfig %s &", dest)); |
|---|
| 269 | free(dest); |
|---|
| 270 | } |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | void AWT_popup_tree_export_window(AW_window *parent_win, AW_CL cl_canvas, AW_CL) { |
|---|
| 274 | static AW_window_simple *aws = 0; |
|---|
| 275 | |
|---|
| 276 | AW_root *awr = parent_win->get_root(); |
|---|
| 277 | create_export_awars(awr); |
|---|
| 278 | resetFiletype(awr, "fig", "print.fig"); |
|---|
| 279 | |
|---|
| 280 | if (!aws) { |
|---|
| 281 | aws = new AW_window_simple; |
|---|
| 282 | aws->init(awr, "EXPORT_TREE_AS_XFIG", "EXPORT TREE TO XFIG"); |
|---|
| 283 | aws->load_xfig("awt/export.fig"); |
|---|
| 284 | |
|---|
| 285 | aws->at("close");aws->callback((AW_CB0)AW_POPDOWN); |
|---|
| 286 | aws->create_button("CLOSE", "CLOSE", "C"); |
|---|
| 287 | |
|---|
| 288 | aws->at("help");aws->callback(AW_POPUP_HELP, (AW_CL)"tree2file.hlp"); |
|---|
| 289 | aws->create_button("HELP", "HELP", "H"); |
|---|
| 290 | |
|---|
| 291 | aws->label_length(15); |
|---|
| 292 | |
|---|
| 293 | awt_create_selection_box((AW_window *)aws, AWAR_PRINT_TREE_FILE_BASE); |
|---|
| 294 | |
|---|
| 295 | aws->at("what"); |
|---|
| 296 | aws->label("Clip at Screen"); |
|---|
| 297 | aws->create_toggle_field(AWAR_PRINT_TREE_CLIP, 1); |
|---|
| 298 | aws->insert_toggle("#print/clipscreen.bitmap", "S", 0); |
|---|
| 299 | aws->insert_toggle("#print/clipall.bitmap", "A", 1); |
|---|
| 300 | aws->update_toggle_field(); |
|---|
| 301 | |
|---|
| 302 | aws->at("remove_root"); |
|---|
| 303 | aws->label("Show Handles"); |
|---|
| 304 | aws->create_toggle_field(AWAR_PRINT_TREE_HANDLES, 1); |
|---|
| 305 | aws->insert_toggle("#print/nohandles.bitmap", "S", 0); |
|---|
| 306 | aws->insert_toggle("#print/handles.bitmap", "A", 1); |
|---|
| 307 | aws->update_toggle_field(); |
|---|
| 308 | |
|---|
| 309 | aws->at("color"); |
|---|
| 310 | aws->label("Export colors"); |
|---|
| 311 | aws->create_toggle(AWAR_PRINT_TREE_COLOR); |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | aws->at("xfig");aws->callback(AWT_print_tree_to_file_xfig, cl_canvas); |
|---|
| 315 | aws->create_button("START_XFIG", "GO XFIG", "X"); |
|---|
| 316 | |
|---|
| 317 | aws->at("cancel");aws->callback((AW_CB0)AW_POPDOWN); |
|---|
| 318 | aws->create_button("CLOSE", "CANCEL", "C"); |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | aws->activate(); |
|---|
| 322 | } |
|---|
| 323 | /*------------------------------------- to export secondary structure to XFIG ---------------------------------------------*/ |
|---|
| 324 | |
|---|
| 325 | void AWT_popup_sec_export_window(AW_window *parent_win, AW_CL cl_canvas, AW_CL) { |
|---|
| 326 | static AW_window_simple *aws = 0; |
|---|
| 327 | |
|---|
| 328 | AW_root *awr = parent_win->get_root(); |
|---|
| 329 | create_export_awars(awr); |
|---|
| 330 | resetFiletype(awr, "fig", "print.fig"); |
|---|
| 331 | |
|---|
| 332 | if (!aws) { |
|---|
| 333 | aws = new AW_window_simple; |
|---|
| 334 | aws->init(awr, "EXPORT_TREE_AS_XFIG", "EXPORT STRUCTURE TO XFIG"); |
|---|
| 335 | aws->load_xfig("awt/secExport.fig"); |
|---|
| 336 | |
|---|
| 337 | aws->at("help");aws->callback(AW_POPUP_HELP, (AW_CL)"tree2file.hlp"); |
|---|
| 338 | aws->create_button("HELP", "HELP", "H"); |
|---|
| 339 | |
|---|
| 340 | aws->label_length(15); |
|---|
| 341 | awt_create_selection_box((AW_window *)aws, AWAR_PRINT_TREE_FILE_BASE); |
|---|
| 342 | |
|---|
| 343 | aws->at("what"); |
|---|
| 344 | aws->label("Clip Options"); |
|---|
| 345 | aws->create_option_menu(AWAR_PRINT_TREE_CLIP); |
|---|
| 346 | aws->insert_option("Export screen only", "s", 0); |
|---|
| 347 | aws->insert_default_option("Export complete structure", "c", 1); |
|---|
| 348 | aws->update_option_menu(); |
|---|
| 349 | |
|---|
| 350 | aws->at("color"); |
|---|
| 351 | aws->label("Export colors"); |
|---|
| 352 | aws->create_toggle(AWAR_PRINT_TREE_COLOR); |
|---|
| 353 | |
|---|
| 354 | aws->at("xfig"); aws->callback(AWT_print_tree_to_file_xfig, cl_canvas); |
|---|
| 355 | aws->create_button("START_XFIG", "EXPORT to XFIG", "X"); |
|---|
| 356 | |
|---|
| 357 | aws->at("close");aws->callback((AW_CB0)AW_POPDOWN); |
|---|
| 358 | aws->create_button("CLOSE", "CLOSE", "C"); |
|---|
| 359 | |
|---|
| 360 | aws->at("cancel");aws->callback((AW_CB0)AW_POPDOWN); |
|---|
| 361 | aws->create_button("CLOSE", "CANCEL", "C"); |
|---|
| 362 | } |
|---|
| 363 | |
|---|
| 364 | aws->activate(); |
|---|
| 365 | } |
|---|
| 366 | /*------------------------------------------------------------------------------------------------------------------------------------------*/ |
|---|
| 367 | |
|---|
| 368 | void AWT_print_tree_to_printer(AW_window *aww, AW_CL cl_ntw) { |
|---|
| 369 | AWT_canvas *ntw = (AWT_canvas*)cl_ntw; |
|---|
| 370 | GB_transaction ta(ntw->gb_main); |
|---|
| 371 | AW_root *awr = aww->get_root(); |
|---|
| 372 | GB_ERROR error = 0; |
|---|
| 373 | char *dest = 0; |
|---|
| 374 | PrintDest printdest = (PrintDest)awr->awar(AWAR_PRINT_TREE_DEST)->read_int(); |
|---|
| 375 | |
|---|
| 376 | switch (printdest) { |
|---|
| 377 | case PDEST_POSTSCRIPT: { |
|---|
| 378 | dest = awt_get_selected_fullname(awr, AWAR_PRINT_TREE_FILE_BASE); |
|---|
| 379 | FILE *out = fopen(dest, "w"); |
|---|
| 380 | if (!out) error = GB_export_IO_error("writing", dest); |
|---|
| 381 | else fclose(out); |
|---|
| 382 | break; |
|---|
| 383 | } |
|---|
| 384 | default: { |
|---|
| 385 | char *name = GB_unique_filename("arb_print", "ps"); |
|---|
| 386 | dest = GB_create_tempfile(name); |
|---|
| 387 | free(name); |
|---|
| 388 | |
|---|
| 389 | if (!dest) error = GB_await_error(); |
|---|
| 390 | break; |
|---|
| 391 | } |
|---|
| 392 | } |
|---|
| 393 | |
|---|
| 394 | if (!error) { |
|---|
| 395 | AW_device *device= ntw->aww->get_print_device(AW_MIDDLE_AREA); |
|---|
| 396 | |
|---|
| 397 | char *xfig; |
|---|
| 398 | { |
|---|
| 399 | char *name = GB_unique_filename("arb_print", "xfig"); |
|---|
| 400 | xfig = GB_create_tempfile(name); |
|---|
| 401 | free(name); |
|---|
| 402 | } |
|---|
| 403 | |
|---|
| 404 | aw_openstatus("Printing"); |
|---|
| 405 | |
|---|
| 406 | if (!xfig) error = GB_await_error(); |
|---|
| 407 | else { |
|---|
| 408 | device->reset(); |
|---|
| 409 | ntw->init_device(device); // draw screen |
|---|
| 410 | |
|---|
| 411 | aw_status("Get Picture Size"); |
|---|
| 412 | device->reset(); |
|---|
| 413 | |
|---|
| 414 | device->set_color_mode(awr->awar(AWAR_PRINT_TREE_COLOR)->read_int() == 1); |
|---|
| 415 | error = device->open(xfig); |
|---|
| 416 | } |
|---|
| 417 | |
|---|
| 418 | if (!error) { |
|---|
| 419 | device->line(0, 0, 0, 1, -1); // dummy point upper left corner |
|---|
| 420 | |
|---|
| 421 | if (awr->awar(AWAR_PRINT_TREE_CLIP)->read_int()) { // draw all |
|---|
| 422 | AW_world size; |
|---|
| 423 | AW_device *size_device = ntw->aww->get_size_device(AW_MIDDLE_AREA); |
|---|
| 424 | size_device->reset(); |
|---|
| 425 | size_device->zoom(ntw->trans_to_fit); |
|---|
| 426 | size_device->set_filter(AW_SCREEN); |
|---|
| 427 | ntw->tree_disp->show(size_device); |
|---|
| 428 | size_device->get_size_information(&size); |
|---|
| 429 | size.l -= 50; |
|---|
| 430 | size.t -= 40; // expand pic |
|---|
| 431 | size.r += 20; |
|---|
| 432 | size.b += 20; |
|---|
| 433 | device->set_offset(AW::Vector(size.l, size.t) / -ntw->trans_to_fit); |
|---|
| 434 | device->set_bottom_clip_border((int)(size.b-size.t), true); |
|---|
| 435 | device->set_right_clip_border((int)(size.r-size.l), true); |
|---|
| 436 | device->zoom(ntw->trans_to_fit); |
|---|
| 437 | } |
|---|
| 438 | else { |
|---|
| 439 | ntw->init_device(device); // draw screen |
|---|
| 440 | } |
|---|
| 441 | |
|---|
| 442 | // ---------------------------------------- |
|---|
| 443 | aw_status("Exporting Data"); |
|---|
| 444 | |
|---|
| 445 | device->set_filter(awr->awar(AWAR_PRINT_TREE_HANDLES)->read_int() |
|---|
| 446 | ? AW_PRINTER | AW_PRINTER_EXT |
|---|
| 447 | : AW_PRINTER); |
|---|
| 448 | |
|---|
| 449 | ntw->tree_disp->show(device); |
|---|
| 450 | device->close(); |
|---|
| 451 | |
|---|
| 452 | gb_assert(GB_is_privatefile(xfig, GB_TRUE)); |
|---|
| 453 | |
|---|
| 454 | // ---------------------------------------- |
|---|
| 455 | aw_status("Converting to Postscript"); |
|---|
| 456 | |
|---|
| 457 | { |
|---|
| 458 | bool landscape = awr->awar(AWAR_PRINT_TREE_LANDSCAPE)->read_int(); |
|---|
| 459 | bool useOverlap = awr->awar(AWAR_PRINT_TREE_OVERLAP)->read_int(); |
|---|
| 460 | double magnification = awr->awar(AWAR_PRINT_TREE_MAGNIFICATION)->read_int() * 0.01; |
|---|
| 461 | |
|---|
| 462 | char *cmd_fig2ps = GBS_global_string_copy("fig2dev -L ps -M %s -m %f %s %s %s", |
|---|
| 463 | (useOverlap ? "-O" : ""), |
|---|
| 464 | magnification, |
|---|
| 465 | (landscape ? "-l 0" : "-p 0"), |
|---|
| 466 | xfig, |
|---|
| 467 | dest); |
|---|
| 468 | |
|---|
| 469 | error = GB_system(cmd_fig2ps); |
|---|
| 470 | free(cmd_fig2ps); |
|---|
| 471 | } |
|---|
| 472 | |
|---|
| 473 | // if user saves to .ps -> no special file permissions are required |
|---|
| 474 | gb_assert(printdest == PDEST_POSTSCRIPT || GB_is_privatefile(dest, GB_FALSE)); |
|---|
| 475 | |
|---|
| 476 | if (!error) { |
|---|
| 477 | aw_status("Printing"); |
|---|
| 478 | |
|---|
| 479 | switch(printdest) { |
|---|
| 480 | case PDEST_PREVIEW: { |
|---|
| 481 | GB_CSTR gs = GB_getenvARB_GS(); |
|---|
| 482 | GB_CSTR command = GBS_global_string("(%s %s;rm -f %s) &", gs, dest, dest); |
|---|
| 483 | error = GB_system(command); |
|---|
| 484 | break; |
|---|
| 485 | } |
|---|
| 486 | case PDEST_POSTSCRIPT: |
|---|
| 487 | break; |
|---|
| 488 | |
|---|
| 489 | case PDEST_PRINTER: { |
|---|
| 490 | char *prt = awr->awar(AWAR_PRINT_TREE_PRINTER)->read_string(); |
|---|
| 491 | error = GB_system(GBS_global_string("%s %s", prt, dest)); |
|---|
| 492 | free(prt); |
|---|
| 493 | GB_unlink_or_warn(dest, &error); |
|---|
| 494 | break; |
|---|
| 495 | } |
|---|
| 496 | } |
|---|
| 497 | } |
|---|
| 498 | } |
|---|
| 499 | aw_closestatus(); |
|---|
| 500 | GB_unlink_or_warn(xfig, &error); |
|---|
| 501 | free(xfig); |
|---|
| 502 | } |
|---|
| 503 | |
|---|
| 504 | free(dest); |
|---|
| 505 | |
|---|
| 506 | if (error) aw_message(error); |
|---|
| 507 | } |
|---|
| 508 | |
|---|
| 509 | |
|---|
| 510 | static long calc_mag_from_psize(AW_root *awr, double papersize, double gfxsize, double wantedpages) { |
|---|
| 511 | bool useOverlap = awr->awar(AWAR_PRINT_TREE_OVERLAP)->read_int(); |
|---|
| 512 | long mag = -1; |
|---|
| 513 | double usableSize = 0; |
|---|
| 514 | |
|---|
| 515 | if (useOverlap) { |
|---|
| 516 | double overlapAmount = awr->awar(AWAR_PRINT_TREE_OVERLAP_AMOUNT)->read_float(); |
|---|
| 517 | if (wantedpages>1.0) { |
|---|
| 518 | while (wantedpages>1.0) { |
|---|
| 519 | usableSize += papersize-overlapAmount; |
|---|
| 520 | wantedpages -= 1.0; |
|---|
| 521 | } |
|---|
| 522 | if (usableSize<0.1) aw_message("Usable size very low. Wrong overlap amount?"); |
|---|
| 523 | } |
|---|
| 524 | usableSize += papersize * wantedpages; // add (partial) page (don't subtract overlapAmount) |
|---|
| 525 | } |
|---|
| 526 | else { |
|---|
| 527 | usableSize = wantedpages*papersize; |
|---|
| 528 | } |
|---|
| 529 | |
|---|
| 530 | mag = (long)(usableSize*100/gfxsize); |
|---|
| 531 | |
|---|
| 532 | #if defined(DEBUG) |
|---|
| 533 | fprintf(stderr, "usableSize=%f mag=%li\n", usableSize, mag); |
|---|
| 534 | #endif // DEBUG |
|---|
| 535 | |
|---|
| 536 | return mag; |
|---|
| 537 | } |
|---|
| 538 | |
|---|
| 539 | // called when resulting pages x-factor was changed by users |
|---|
| 540 | void awt_calc_mag_from_psizex(AW_window *aww) { |
|---|
| 541 | AW_root *awr = aww->get_root(); |
|---|
| 542 | double papersize = awr->awar(AWAR_PRINT_TREE_PSIZEX)->read_float(); |
|---|
| 543 | double gfxsize = awr->awar(AWAR_PRINT_TREE_GSIZEX)->read_float(); |
|---|
| 544 | double wantedpages = awr->awar(AWAR_PRINT_TREE_SIZEX)->read_float(); |
|---|
| 545 | long mag = calc_mag_from_psize(awr, papersize, gfxsize, wantedpages); |
|---|
| 546 | |
|---|
| 547 | awr->awar(AWAR_PRINT_TREE_MAGNIFICATION)->write_int(mag); |
|---|
| 548 | } |
|---|
| 549 | |
|---|
| 550 | // called when resulting pages y-factor was changed by users |
|---|
| 551 | void awt_calc_mag_from_psizey(AW_window *aww) { |
|---|
| 552 | AW_root *awr = aww->get_root(); |
|---|
| 553 | double papersize = awr->awar(AWAR_PRINT_TREE_PSIZEY)->read_float(); |
|---|
| 554 | double gfxsize = awr->awar(AWAR_PRINT_TREE_GSIZEY)->read_float(); |
|---|
| 555 | double wantedpages = awr->awar(AWAR_PRINT_TREE_SIZEY)->read_float(); |
|---|
| 556 | long mag = calc_mag_from_psize(awr, papersize, gfxsize, wantedpages); |
|---|
| 557 | |
|---|
| 558 | awr->awar(AWAR_PRINT_TREE_MAGNIFICATION)->write_int(mag); |
|---|
| 559 | } |
|---|
| 560 | |
|---|
| 561 | void awt_calc_best_fit(AW_window *aww) { |
|---|
| 562 | int best_orientation = -1; |
|---|
| 563 | const char *best_zoom_awar_name = 0; |
|---|
| 564 | float best_zoom = 0; |
|---|
| 565 | int best_magnification = 0; |
|---|
| 566 | int best_pages = 0; |
|---|
| 567 | AW_root *awr = aww->get_root(); |
|---|
| 568 | int wanted_pages = awr->awar(AWAR_PRINT_TREE_PAGES)->read_int(); |
|---|
| 569 | |
|---|
| 570 | for (int o = 0; o <= 1; ++o) { |
|---|
| 571 | awr->awar(AWAR_PRINT_TREE_LANDSCAPE)->write_int(o); // set orientation (calls awt_page_size_check_cb) |
|---|
| 572 | |
|---|
| 573 | for (int xy = 0; xy <= 1; ++xy) { |
|---|
| 574 | const char *awar_name = xy == 0 ? AWAR_PRINT_TREE_SIZEX : AWAR_PRINT_TREE_SIZEY; |
|---|
| 575 | |
|---|
| 576 | for (int pcount = 1; pcount <= wanted_pages; pcount++) { |
|---|
| 577 | double zoom = pcount*1.0; |
|---|
| 578 | awr->awar(awar_name)->write_float(zoom); // set zoom (x or y) |
|---|
| 579 | |
|---|
| 580 | // calculate magnification : |
|---|
| 581 | if (xy == 0) awt_calc_mag_from_psizex(aww); |
|---|
| 582 | else awt_calc_mag_from_psizey(aww); |
|---|
| 583 | |
|---|
| 584 | int magnification = awr->awar(AWAR_PRINT_TREE_MAGNIFICATION)->read_int(); // read calculated magnification |
|---|
| 585 | double sx = awr->awar(AWAR_PRINT_TREE_SIZEX)->read_float(); |
|---|
| 586 | double sy = awr->awar(AWAR_PRINT_TREE_SIZEY)->read_float(); |
|---|
| 587 | int pages = xy2pages(sx, sy); |
|---|
| 588 | |
|---|
| 589 | if (pages>wanted_pages) break; // pcount-loop |
|---|
| 590 | |
|---|
| 591 | #if defined(DEBUG) |
|---|
| 592 | fprintf(stderr, "pages=%i sx=%f sy=%f mag=%i awar_name=%s landscape='%i'", |
|---|
| 593 | pages, sx, sy, magnification, awar_name, o); |
|---|
| 594 | #endif // DEBUG |
|---|
| 595 | |
|---|
| 596 | if (pages <= wanted_pages && pages >= best_pages && magnification>best_magnification) { |
|---|
| 597 | // fits on wanted_pages and is best result yet |
|---|
| 598 | best_magnification = magnification; |
|---|
| 599 | best_orientation = o; |
|---|
| 600 | best_zoom_awar_name = awar_name; |
|---|
| 601 | best_zoom = zoom; |
|---|
| 602 | best_pages = pages; |
|---|
| 603 | #if defined(DEBUG) |
|---|
| 604 | fprintf(stderr, " [best yet]"); |
|---|
| 605 | #endif // DEBUG |
|---|
| 606 | } |
|---|
| 607 | #if defined(DEBUG) |
|---|
| 608 | fprintf(stderr, "\n"); |
|---|
| 609 | #endif // DEBUG |
|---|
| 610 | } |
|---|
| 611 | } |
|---|
| 612 | } |
|---|
| 613 | |
|---|
| 614 | if (best_orientation != -1) { |
|---|
| 615 | awt_assert(best_zoom_awar_name); |
|---|
| 616 | |
|---|
| 617 | // take the best found values : |
|---|
| 618 | awr->awar(AWAR_PRINT_TREE_LANDSCAPE)->write_int(best_orientation); |
|---|
| 619 | awr->awar(best_zoom_awar_name)->write_float(best_zoom); |
|---|
| 620 | awr->awar(AWAR_PRINT_TREE_PAGES)->write_int(best_pages); |
|---|
| 621 | awr->awar(AWAR_PRINT_TREE_MAGNIFICATION)->write_int(best_magnification); |
|---|
| 622 | |
|---|
| 623 | if (best_pages != wanted_pages) { |
|---|
| 624 | aw_message(GBS_global_string("That didn't fit on %i page(s) - using %i page(s)", |
|---|
| 625 | wanted_pages, best_pages)); |
|---|
| 626 | } |
|---|
| 627 | } |
|---|
| 628 | else { |
|---|
| 629 | aw_message(GBS_global_string("That didn't fit on %i page(s)", wanted_pages)); |
|---|
| 630 | } |
|---|
| 631 | } |
|---|
| 632 | |
|---|
| 633 | void AWT_popup_print_window(AW_window *parent_win, AW_CL cl_canvas, AW_CL) { |
|---|
| 634 | AW_root *awr = parent_win->get_root(); |
|---|
| 635 | AWT_canvas *ntw = (AWT_canvas*)cl_canvas; |
|---|
| 636 | static AW_window_simple *aws = 0; |
|---|
| 637 | |
|---|
| 638 | create_print_awars(awr, ntw); |
|---|
| 639 | resetFiletype(awr, "ps", "print.ps"); |
|---|
| 640 | |
|---|
| 641 | if (!aws) { |
|---|
| 642 | aws = new AW_window_simple; |
|---|
| 643 | aws->init(awr, "PRINT_CANVAS", "PRINT GRAPHIC"); |
|---|
| 644 | aws->load_xfig("awt/print.fig"); |
|---|
| 645 | |
|---|
| 646 | aws->at("close");aws->callback((AW_CB0)AW_POPDOWN); |
|---|
| 647 | aws->create_button("CLOSE", "CLOSE", "C"); |
|---|
| 648 | |
|---|
| 649 | aws->at("help");aws->callback(AW_POPUP_HELP, (AW_CL)"tree2prt.hlp"); |
|---|
| 650 | aws->create_button("HELP", "HELP", "H"); |
|---|
| 651 | |
|---|
| 652 | |
|---|
| 653 | aws->at("orientation"); |
|---|
| 654 | aws->create_toggle_field(AWAR_PRINT_TREE_LANDSCAPE, 1); |
|---|
| 655 | aws->insert_toggle("#print/landscape.bitmap", "L", 1); |
|---|
| 656 | aws->insert_toggle("#print/portrait.bitmap", "P", 0); |
|---|
| 657 | aws->update_toggle_field(); |
|---|
| 658 | aws->label_length(15); |
|---|
| 659 | |
|---|
| 660 | aws->at("magnification"); |
|---|
| 661 | aws->create_input_field(AWAR_PRINT_TREE_MAGNIFICATION, 4); |
|---|
| 662 | |
|---|
| 663 | aws->at("what"); |
|---|
| 664 | aws->label("Clip at Screen"); |
|---|
| 665 | aws->create_toggle_field(AWAR_PRINT_TREE_CLIP, 1); |
|---|
| 666 | aws->insert_toggle("#print/clipscreen.bitmap", "S", 0); |
|---|
| 667 | aws->insert_toggle("#print/clipall.bitmap", "A", 1); |
|---|
| 668 | aws->update_toggle_field(); |
|---|
| 669 | |
|---|
| 670 | aws->at("remove_root"); |
|---|
| 671 | aws->label("Show Handles"); |
|---|
| 672 | aws->create_toggle_field(AWAR_PRINT_TREE_HANDLES, 1); |
|---|
| 673 | aws->insert_toggle("#print/nohandles.bitmap", "S", 0); |
|---|
| 674 | aws->insert_toggle("#print/handles.bitmap", "A", 1); |
|---|
| 675 | aws->update_toggle_field(); |
|---|
| 676 | |
|---|
| 677 | aws->at("color"); |
|---|
| 678 | aws->label("Export colors"); |
|---|
| 679 | aws->create_toggle(AWAR_PRINT_TREE_COLOR); |
|---|
| 680 | |
|---|
| 681 | aws->button_length(6); |
|---|
| 682 | aws->at("gsizex"); aws->create_button(0, AWAR_PRINT_TREE_GSIZEX); |
|---|
| 683 | aws->at("gsizey"); aws->create_button(0, AWAR_PRINT_TREE_GSIZEY); |
|---|
| 684 | |
|---|
| 685 | aws->button_length(8); |
|---|
| 686 | aws->at("psizex"); aws->create_input_field(AWAR_PRINT_TREE_PSIZEX, 4); |
|---|
| 687 | aws->at("psizey"); aws->create_input_field(AWAR_PRINT_TREE_PSIZEY, 4); |
|---|
| 688 | |
|---|
| 689 | aws->at("sizex"); |
|---|
| 690 | aws->callback(awt_calc_mag_from_psizex); |
|---|
| 691 | aws->create_input_field(AWAR_PRINT_TREE_SIZEX, 4); |
|---|
| 692 | aws->at("sizey"); |
|---|
| 693 | aws->callback(awt_calc_mag_from_psizey); |
|---|
| 694 | aws->create_input_field(AWAR_PRINT_TREE_SIZEY, 4); |
|---|
| 695 | |
|---|
| 696 | aws->at("best_fit"); |
|---|
| 697 | aws->callback(awt_calc_best_fit); |
|---|
| 698 | aws->create_autosize_button(0, "Fit on"); |
|---|
| 699 | |
|---|
| 700 | aws->at("pages"); |
|---|
| 701 | aws->create_input_field(AWAR_PRINT_TREE_PAGES, 3); |
|---|
| 702 | |
|---|
| 703 | aws->at("overlap"); |
|---|
| 704 | aws->create_toggle(AWAR_PRINT_TREE_OVERLAP); |
|---|
| 705 | |
|---|
| 706 | aws->at("amount"); |
|---|
| 707 | aws->create_input_field(AWAR_PRINT_TREE_OVERLAP_AMOUNT, 4); |
|---|
| 708 | |
|---|
| 709 | aws->at("printto"); |
|---|
| 710 | aws->label_length(12); |
|---|
| 711 | aws->label("Destination"); |
|---|
| 712 | aws->create_toggle_field(AWAR_PRINT_TREE_DEST); |
|---|
| 713 | aws->insert_toggle("Printer", "P", PDEST_PRINTER); |
|---|
| 714 | aws->insert_toggle("File (Postscript)", "F", PDEST_POSTSCRIPT); |
|---|
| 715 | aws->insert_toggle("Preview", "V", PDEST_PREVIEW); |
|---|
| 716 | aws->update_toggle_field(); |
|---|
| 717 | |
|---|
| 718 | aws->at("printer"); |
|---|
| 719 | aws->create_input_field(AWAR_PRINT_TREE_PRINTER, 16); |
|---|
| 720 | |
|---|
| 721 | aws->at("filename"); |
|---|
| 722 | aws->create_input_field(AWAR_PRINT_TREE_FILE_NAME, 16); |
|---|
| 723 | |
|---|
| 724 | aws->at("go"); |
|---|
| 725 | aws->highlight(); |
|---|
| 726 | aws->callback(AWT_print_tree_to_printer, (AW_CL)ntw); |
|---|
| 727 | aws->create_button("PRINT", "PRINT", "P"); |
|---|
| 728 | |
|---|
| 729 | aws->button_length(0); |
|---|
| 730 | |
|---|
| 731 | aws->at("getsize"); |
|---|
| 732 | aws->callback((AW_CB1)awt_print_tree_check_size, (AW_CL)ntw); |
|---|
| 733 | aws->create_button(0, "Get Graphic Size"); |
|---|
| 734 | } |
|---|
| 735 | |
|---|
| 736 | awt_print_tree_check_size(0, (AW_CL)ntw); |
|---|
| 737 | aws->activate(); |
|---|
| 738 | } |
|---|