Debian Codegraph @

printPrefs.cxx

raw ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /*
2 * $Id: printPrefs.cxx,v 4.6 2008/10/27 15:34:11 hut66au Exp $
3 *
4 * Imview, the portable image analysis application
5 * http://www.cmis.csiro.au/Hugues.Talbot/imview
6 * ----------------------------------------------------------
7 *
8 * Imview is an attempt to provide an image display application
9 * suitable for professional image analysis. It was started in
10 * 1997 and is mostly the result of the efforts of Hugues Talbot,
11 * Image Analysis Project, CSIRO Mathematical and Information
12 * Sciences, with help from others (see the CREDITS files for
13 * more information)
14 *
15 * Imview is Copyrighted (C) 1997-2001 by Hugues Talbot and was
16 * supported in parts by the Australian Commonwealth Science and
17 * Industry Research Organisation. Please see the COPYRIGHT file
18 * for full details. Imview also includes the contributions of
19 * many others. Please see the CREDITS file for full details.
20 *
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation; either version 2 of the License, or
24 * (at your option) any later version.
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 *
31 * You should have received a copy of the GNU General Public License
32 * along with this program; if not, write to the Free Software
33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
34 * */
35
36 /*------------------------------------------------------------------------
37 *
38 * A print preference panel using fluid.
39 *
40 * Hugues Talbot 21 Apr 1998
41 *
42 *-----------------------------------------------------------------------*/
43
44 #include "imnmspc.hxx"
45
46 #include <string.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include "imunistd.h"
50 #include <errno.h>
51 #include <string> // needed by nocase.H
52 #include <FL/Fl_File_Chooser.H>
53 #include <FL/fl_ask.H>
54 #include "imview.hxx"
55 #include "nocase.hxx"
56 #include "io/imSystem.hxx"
57 #include "imageIO.hxx"
58 #include "imageViewer.hxx"
59 #include "menubar.hxx"
60 #include "printPrefs.hxx"
61 #include "io/newprefio.hxx"
62 #include "io/cpostscript.hxx"
63 #include "machine.hxx"
64
65 extern char appName[];
66 extern imageIO *IOBlackBox;
67 extern imageViewer *mainViewer;
68 extern imViewMenuBar *mainMenuBar;
69 extern imprefs *fileprefs;
70 extern printprefs *printPrefsPanel;
71
72 // the `broken pipe' signal handler
73 static void brokenPipeHandler(int sig);
74
75 // the siglongjmp buffer
76 static im_jmp_buf bpenv;
77
78 // print panel methods
79
80 printprefs::printprefs()
81 {
82 dbgprintf("Print panel constructed\n");
83 prefWindow = 0; // fluid depend on that...
84 return;
85 }
86
87 void printprefs::setDefaults()
88 {
89 static char buff[100];
90 char *myenv;
91
92 // we print to a real printer by default
93 printerCheck->set();
94
95 // default printing output
96 if ((myenv = getenv("PRINTER")) != 0)
97 sprintf(buff, "lpr -P%s", myenv);
98 else
99 strcpy(buff, "lpr");
100 commandInput->value((const char *)buff);
101
102 // default value for file
103 sprintf(buff, "%s.ps", appName);
104 filenameInput->value(buff);
105
106 // empty title is fine...
107
108 // orientation should be portrait
109 portraitCheck->set();
110
111 // Paper size should be A4
112 // A4Check->set();
113 papertype_ = PAGESIZE_A4;
114 papersizeChoice->value(papertype_);
115
116 // language should be PS
117 psCheck->set();
118
119 // by default we only print the image that is showing now
120 oneImageCheck->set();
121
122 // number of copies should be just one
123 nbcopiesInput->value("1");
124
125 // By default we print the whole image
126 wholeCheck->set();
127
128 // that's it
129
130 return;
131 }
132
133 // this method switches between the `File' and the `Printer' defaults
134 void printprefs::toggleToFile()
135 {
136 // disable the Print command
137 commandInput->deactivate();
138 commandInput->color(47); // got that from fluid...
139 commandInput->textcolor(31);
140 commandInput->labelcolor(31);
141 // enable the "file" command
142 filenameInput->activate();
143 filenameInput->color(43);
144 filenameInput->textcolor(FL_BLACK);
145 filenameInput->labelcolor(FL_BLACK);
146 // enable the browse button
147 browseButton->activate();
148 browseButton->labelcolor(FL_BLACK);
149
150 prefWindow->redraw();
151
152 return;
153 }
154
155 void printprefs::toggleToPrinter()
156 {
157 // disable the Print command
158 filenameInput->deactivate();
159 filenameInput->color(47); // got that from fluid...
160 filenameInput->textcolor(31);
161 filenameInput->labelcolor(31);
162 // enable the "file" command
163 commandInput->activate();
164 commandInput->color(43);
165 commandInput->textcolor(FL_BLACK);
166 commandInput->labelcolor(FL_BLACK);
167 // disable the browse button
168 browseButton->deactivate();
169 browseButton->labelcolor(31);
170
171 prefWindow->redraw();
172
173 return;
174 }
175
176 void printprefs::allowTitle(bool b)
177 {
178 if (b) {
179 // setting the title is allowed
180 titleInput->activate();
181 titleInput->color(43); // fluid...
182 titleInput->textcolor(FL_BLACK);
183 titleInput->labelcolor(FL_BLACK);
184 } else {
185 titleInput->deactivate();
186 titleInput->color(47);
187 titleInput->textcolor(31);
188 titleInput->textcolor(31);
189 }
190
191 prefWindow->redraw();
192
193 return;
194 }
195
196 void printprefs::setTitle(const char *t)
197 {
198 titleInput->value(t);
199 return;
200 }
201
202 void printprefs::printFileName(const char *fn)
203 {
204 // this copies it to something more permanent.
205 filenameInput->value(fn);
206 return;
207 }
208
209 void printprefs::setMargins(long whichItem)
210 {
211 switch ((margin_size)whichItem) {
212 case MARGIN_SIZE_STANDARD:
213 case MARGIN_SIZE_SMALL:
214 case MARGIN_SIZE_LARGE:
215 case MARGIN_SIZE_ZERO:
216 postscript_set_margin_index((margin_size)whichItem);
217 // then set valus for margin inputs
218 break;
219 case MARGIN_SIZE_CUSTOM:
220 postscript_set_margin_index((margin_size)whichItem);
221 // need to set custom margins too
222 // then GET value from margin inputs
223 break;
224 default:
225 warnprintf("Incorrect size option %ld\n", whichItem);
226 break;
227 }
228 return;
229 }
230
231 static void brokenPipeHandler(int sig)
232 {
233 // reset the signal handler
234 signal(sig, brokenPipeHandler);
235 im_longjmp(bpenv, 1);
236 }
237
238 // the following is a tiny bit hackish.
239 // the vp=&... are seemingly useless statements that
240 // prevent the compiler from optimizing the automatic variable
241 // into registers, thus putting the longjmp in jeopardy.
242 // in fact there is no problem, but that's the easiest way
243 // I found for stopping gcc warning me about the possibility.
244 int printprefs::doThePrinting(bool preview)
245 {
246 HIMAGE *tbp; // don't you love C?
247 volatile void *vp;
248 int retval = 0; vp = &retval;
249 byte win_type; vp = &win_type;
250 byte orientation; vp = &orientation;
251 bool usePipe = false; vp = &usePipe;
252 int nbcopies, nbitems, i; vp = &nbcopies; vp = &nbitems; vp = &i;
253 char *title, *filename, *pipeCommand; vp = &title; vp = &filename; vp = &pipeCommand;
254 char *fileList, *tmpPreview; vp = &fileList; vp = &tmpPreview;
255 uchar *buf0, *buf1, *buf2; vp = &buf0;vp = &buf1; vp = &buf2;
256 FILE *outfile; vp = &outfile;
257
258 // how many images are we going to save?
259 if (wholeListCheck->value() == 1) {
260 fileList = mainMenuBar->getItemListContent(IMAGE_LIST_ID, nbitems);
261 } else {
262 fileList = 0;
263 nbitems = 1;
264 }
265
266 tbp = new HIMAGE[nbitems];
267
268 dbgprintf("about to print %d items\n", nbitems);
269
270 for (i = 0 ; i < nbitems ; i++) {
271 // fill up what we can from the outset
272 if (IOBlackBox->getCurrImgPixType() == IM_BINARY)
273 tbp[i].type = PS_BINARY;
274 else
275 tbp[i].type = PS_CHAR; // we don't care, but not binary
276
277 buf0 = buf1 = buf2 = 0;
278
279 // first question: are we printing the whole image or only a subset?
280 if (wholeCheck->value() == 1) {
281 // the whole thing
282 tbp[i].nbrows = IOBlackBox->imageHeight();
283 tbp[i].nbcols = IOBlackBox->imageWidth();
284 if ((tbp[i].nbcomp = IOBlackBox->wholeImage(&buf0, &buf1, &buf2)) != 0) {
285 tbp[i].buf0 = buf0;
286 tbp[i].buf1 = buf1;
287 tbp[i].buf2 = buf2;
288 } else {
289 errprintf("Can't print images with %d samples per pixel\n",
290 IOBlackBox->imageDepth());
291 return(10);
292 }
293 } else {
294 // the subset currently displayed
295 int bx, by, bw, bh; // dimensions of the subset...
296
297 if ((tbp[i].nbcomp = IOBlackBox->subsetImage(&buf0, &buf1, &buf2,
298 bx, by, bw, bh)) != 0) {
299
300 tbp[i].nbrows = bh;
301 tbp[i].nbcols = bw;
302 tbp[i].buf0 = buf0;
303 tbp[i].buf1 = buf1;
304 tbp[i].buf2 = buf2;
305 } else {
306 errprintf("Can't print images with %d samples per pixel\n",
307 IOBlackBox->imageDepth());
308 return(11);
309 }
310 }
311 // display the next image
312 if (nbitems > 1) {
313 dbgprintf("Showing the next image\n");
314 nextimage_cb(0,0);
315 }
316 }
317
318
319
320 // get all the options now
321 // paper type selection with Choice menu now.
322 // if (A3Check->value()) papertype = A3_FMT;
323 // else if (letterCheck->value()) papertype = LE_FMT;
324 // else papertype = A4_FMT;
325
326 if (epsCheck->value()) win_type = WIN_EPS;
327 else win_type = WIN_ALL;
328
329 if (landscapeCheck->value()) orientation = ORI_LDS;
330 else orientation = ORI_PRT;
331
332 nbcopies = MYMAX(atoi(nbcopiesInput->value()), 1);
333
334 title = (char *)(titleInput->value());
335 if ((title != 0) && (title[0] == '\0'))
336 title = 0; // empty string
337
338 // open the pipe!
339
340
341 if (printerCheck->value() && !preview) {
342 // we pipe to a command, which hopefully will print
343 // (not our problem if it doesn't...
344
345 pipeCommand = (char *)commandInput->value();
346 if ((pipeCommand != 0) && (pipeCommand[0] == '\0')
347 || (pipeCommand == 0)) {
348 errprintf("Please specify a print command\n");
349 retval = 1;
350 } else {
351 // 2:set the `broken pipe' signal handler
352 outfile = 0;
353 im_signal(SIGPIPE, brokenPipeHandler);
354 if (im_setjmp(bpenv, 1) == 0) { // save the mask buffer
355 // 3: try to open the pipe for writing
356 if ((outfile = popen(pipeCommand, "w")) == 0) {
357 errprintf("Cannot open pipe to command %s\n%s",
358 pipeCommand,
359 strerror(errno));
360 retval = 2;
361 }
362 } else {
363 // we got there only through the signal handler
364 if (outfile != 0)
365 fclose(outfile);
366 if (pipeCommand != 0)
367 errprintf("Broken pipe: invalid command %s\n", pipeCommand);
368 retval = 3;
369 }
370 usePipe = true; // we'll have to use pclose later on
371 }
372 } else if ((printerCheck->value() == 0) && !preview) {
373 // we send to a file
374 filename = (char *)filenameInput->value();
375 if ((filename != 0) && (filename[0] == '\0')
376 || (filename == 0)) {
377 errprintf("Please specify a file to print to\n");
378 retval = 4;
379 } else {
380 if (access(filename, W_OK) == 0) {
381 // the file aready exist, do we really want to do this?
382 static char alrtmsg[1024];
383 sprintf(alrtmsg,
384 "File %s already exists\n"
385 "Do you want to overwrite?\n",
386 filename);
387 if (fl_choice(alrtmsg, "Cancel", "Overwrite", NULL) == 0) {
388 retval = 6;
389 goto error;
390 }
391 }
392 if ((outfile = im_fopen(filename, "wb")) == 0) {
393 errprintf("Can't open file %s\n%s",
394 filename,
395 strerror(errno));
396 retval = 5;
397 }
398 }
399 } else {
400 // preview // 1: get the preview command
401 tmpPreview = tempnam(0, "ImViu");
402
403 if ((outfile = im_fopen(tmpPreview, "wb")) == 0) {
404 errprintf("Can't open temporary file %s\n%s",
405 tmpPreview,
406 strerror(errno));
407 free(tmpPreview);
408 retval = 5;
409 }
410 }
411
412 if (retval == 0) {
413 // all is fine, we can send the postscript code away.
414 dbgprintf("Composing PS code\n");
415 compose_PostScript_code(tbp,nbitems,1.0,
416 papertype_,
417 win_type,
418 orientation,
419 nbcopies,
420 0,0,
421 title,
422 "Times-Roman",0, 1,
423 outfile);
424 if (preview) {
425 int gslen = strlen(fileprefs->gvpath()) + 2*strlen(tmpPreview);
426 char *previewCommand = new char[gslen+20];
427
428 sprintf(previewCommand, "(%s %s ; rm -f %s) &",
429 fileprefs->gvpath(),
430 tmpPreview, tmpPreview);
431
432 fflush(outfile); // send Everything, but do not close.
433 system(previewCommand);
434
435 delete[] previewCommand;
436 free(tmpPreview); // we can get rid of this now.
437 }
438
439
440 if (usePipe) { // if we are previewing, pclose will have to occur later
441 pclose(outfile);
442 outfile = 0;
443 } else { // even if we are previewing
444 fclose(outfile);
445 outfile = 0;
446 }
447 }
448
449 error:
450 // probably deleting 0 would be acceptable, but hey.
451 for (i = 0 ; i < nbitems ; i++) {
452 if (tbp[i].buf0 != 0) delete[] tbp[i].buf0;
453 if (tbp[i].buf1 != 0) delete[] tbp[i].buf1;
454 if (tbp[i].buf2 != 0) delete[] tbp[i].buf2;
455 }
456
457 if (fileList != 0) delete[] fileList;
458 delete[] tbp;
459
460 return retval;
461 }
462
463 void printprefs::show()
464 {
465 prefWindow->show();
466 return;
467 }
468
469 void printprefs::hide()
470 {
471 prefWindow->hide();
472 return;
473 }
474
475
476
477 // print panel associated callbacks
478
479 void printercheck_cb(Fl_Check_Button *, printprefs *panel)
480 {
481 dbgprintf("Printer check callback\n");
482 panel->toggleToPrinter();
483
484 return;
485 }
486
487 void filecheck_cb(Fl_Check_Button *, printprefs *panel)
488 {
489 dbgprintf("File check callback\n");
490 panel->toggleToFile();
491
492
493 return;
494 }
495
496 void commandInput_cb(Fl_Input *, printprefs *)
497 {
498 dbgprintf("Command input callback\n");
499
500 return;
501 }
502
503 void filenameInput_cb(Fl_Input *, printprefs *)
504 {
505 dbgprintf("filename input callback\n");
506
507 return;
508 }
509
510 void browsebutton_cb(Fl_Button *, printprefs *panel)
511 {
512 const char *currentfile;
513 static const char *p = 0;
514
515 dbgprintf("Browse Button pressed on print prefs panel\n");
516
517 currentfile = panel->filenameInputValue();
518
519 p = fl_file_chooser("Print to file", "*[pP][sS]", currentfile);
520
521 if (p) panel->printFileName(p);
522
523 return;
524 }
525
526
527 void titleInput_cb(Fl_Input *, printprefs *)
528 {
529 dbgprintf("title input callback\n");
530
531 return;
532 }
533
534 void portraitcheck_cb(Fl_Check_Button *, printprefs *)
535 {
536 dbgprintf("Portrait check callback\n");
537
538 return;
539 }
540
541 void landscapecheck_cb(Fl_Check_Button *, printprefs *)
542 {
543 dbgprintf("Landscape check callback\n");
544
545 return;
546 }
547
548 void papersize_cb(Fl_Choice *c, printprefs *s)
549 {
550 dbgprintf("Paper choice = %d\n", c->value());
551 s->setpapertype(c->value());
552 }
553
554 void a4check_cb(Fl_Check_Button *, printprefs *)
555 {
556 dbgprintf("A4 check callback\n");
557
558 return;
559 }
560
561 void lettercheck_cb(Fl_Check_Button *, printprefs *)
562 {
563 dbgprintf("Letter check callback\n");
564
565 return;
566 }
567
568 void a3check_cb(Fl_Check_Button *, printprefs *)
569 {
570 dbgprintf("A3 check callback\n");
571
572 return;
573 }
574
575 void pscheck_cb(Fl_Check_Button *, printprefs *panel)
576 {
577 dbgprintf("PS check callback\n");
578 panel->allowTitle(true);
579
580 return;
581 }
582
583 void epscheck_cb(Fl_Check_Button *, printprefs *panel)
584 {
585 dbgprintf("EPS check callback\n");
586 panel->allowTitle(false);
587
588 return;
589 }
590
591 void nbcopies_cb(Fl_Input *, printprefs *)
592 {
593 dbgprintf("Nb of copies callback\n");
594 return;
595 }
596
597
598 void oneimagecheck_cb(Fl_Check_Button *, printprefs *)
599 {
600 dbgprintf("One image check callback\n");
601
602 return;
603 }
604
605 void wholelistcheck_cb(Fl_Check_Button *, printprefs *)
606 {
607 dbgprintf("Whole list check callback\n");
608
609 return;
610 }
611
612 void wholecheck_cb(Fl_Check_Button *, printprefs *)
613 {
614 dbgprintf("Whole check callback\n");
615
616 return;
617 }
618
619 void subsetcheck_cb(Fl_Check_Button *, printprefs *)
620 {
621 dbgprintf("Subset check callback\n");
622
623 return;
624 }
625
626 // 2- Margin tab callbacks
627 void marginchoice_cb(Fl_Choice *, printprefs *panel)
628 {
629 dbgprintf("Margin dimensions callback\n");
630 return;
631 }
632
633 void chosenmargin_cb(Fl_Menu_ *, void *value)
634 {
635 long v = (long)value;
636 dbgprintf("Margin chosen = %d\n", v);
637 printPrefsPanel->setMargins(v);
638 return;
639 }
640
641 void cancelbutton_cb(Fl_Button *, printprefs *panel)
642 {
643 dbgprintf("Cancel Button pressed on print prefs panel\n");
644 // hide the window
645 panel->hide();
646 return;
647 }
648
649 void previewbutton_cb(Fl_Button *bt, printprefs *panel)
650 {
651 dbgprintf("Preview button pressed\n");
652 panel->doThePrinting(true); // this is the preview switch
653 return;
654 }
655
656 // This is really the `print' button...
657 void okbutton_cb(Fl_Return_Button *, printprefs *panel)
658 {
659 dbgprintf("Print Button pressed on print prefs panel\n");
660 if (panel->doThePrinting() == 0) {
661 // hide the window
662 panel->hide();
663 } // else leave the panel around for the user to correct things
664 return;
665 }
666
667