-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPPrintWindow.cxx
557 lines (493 loc) · 18.5 KB
/
PPrintWindow.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
//==============================================================================
// File: PPrintWindow.cxx
//
// Copyright (c) 2017, Phil Harvey, Queen's University
//==============================================================================
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#ifndef NO_FORK
#include <unistd.h>
#endif
#include <sys/types.h>
#include <sys/wait.h>
#include <Xm/Form.h>
#include <Xm/ToggleB.h>
#include <Xm/Label.h>
#include <Xm/Text.h>
#include <Xm/PushB.h>
#include <Xm/MessageB.h>
#include "PPrintWindow.h"
#include "PImageWindow.h"
#include "PResourceManager.h"
#include "ImageData.h"
#include "PUtils.h"
// definitions for capturing and printing images
// (these can be redefined for different system types if necessary)
#ifndef NO_FORK
const short kNumCaptureArgs = 2;
// Note: This array must have space for 2 additional entries
static char *sCaptureCommand[kNumCaptureArgs+2] = { "import", "-silent", NULL, NULL };
#endif
// static string definitions
static char * sPrintTitle[] = { "Print Postscript Image (Vector)",
"Print Window (Raster)" };
static char * sPrintLabel[] = { "Print Image to:", "Print Window to:" };
static char * sPrintMessage[][2] = { { "Printer Command:",
"Filename (.ps or .eps):" },
{ "Printer Command:",
"Filename (.gif, .jpg, .ps, .eps, etc...):" } };
PPrintWindow::PPrintWindow(ImageData *data, EPrintType printType)
: PWindow(data)
{
int n;
XmString str;
Arg wargs[16];
Widget w, but;
mWarnDialog = NULL;
#ifdef NO_FORK
// VAX doesn't support Print Window or printing to device
mPrintType = kPrintImage;
mToFile = 1;
#else
mPrintType = printType; // kPrintImage or kPrintWindow
mToFile = data->print_to;
#endif
n = 0;
mSaveCol = data->image_col;
mSaveLabel = data->show_label;
XtSetArg(wargs[n], XmNtitle, sPrintTitle[printType]); ++n;
XtSetArg(wargs[n], XmNx, 350); ++n;
XtSetArg(wargs[n], XmNy, 350); ++n;
XtSetArg(wargs[n], XmNminWidth, 320); ++n;
XtSetArg(wargs[n], XmNminHeight, 180); ++n;
SetShell(CreateShell("printPop",data->toplevel,wargs,n));
w = XtCreateManagedWidget("agedForm",xmFormWidgetClass,GetShell(),NULL,0);
SetMainPane(w);
n = 0;
XtSetArg(wargs[n], XmNx, 20); ++n;
XtSetArg(wargs[n], XmNy, 140); ++n;
XtSetArg(wargs[n], XmNwidth, 80); ++n;
XtSetArg(wargs[n], XmNrecomputeSize, FALSE); ++n;
print_button = XtCreateManagedWidget(mToFile ? "Save" : "Print",xmPushButtonWidgetClass,w,wargs,n);
XtAddCallback(print_button, XmNactivateCallback, (XtCallbackProc)PrintProc, this);
n = 0;
XtSetArg(wargs[n], XmNx, 220); ++n;
XtSetArg(wargs[n], XmNy, 140); ++n;
XtSetArg(wargs[n], XmNwidth, 80); ++n;
cancel_button = XtCreateManagedWidget("Cancel",xmPushButtonWidgetClass,w,wargs,n);
XtAddCallback(cancel_button, XmNactivateCallback, (XtCallbackProc)CancelProc, this);
n = 0;
XtSetArg(wargs[n], XmNx, 20); ++n;
XtSetArg(wargs[n], XmNy, 14); ++n;
target_label = XtCreateManagedWidget(sPrintLabel[printType], xmLabelWidgetClass, w, wargs, n);
n = 0;
XtSetArg(wargs[n], XmNx, 154); ++n;
XtSetArg(wargs[n], XmNy, 10 + RADIO_OFFSET); ++n;
XtSetArg(wargs[n], XmNmarginHeight, 2); ++n;
XtSetArg(wargs[n], XmNindicatorType, XmONE_OF_MANY); ++n;
XtSetArg(wargs[n], XmNset, mToFile == 0); ++n;
but = XtCreateManagedWidget("Printer",xmToggleButtonWidgetClass,w,wargs,n);
XtAddCallback(but,XmNvalueChangedCallback,(XtCallbackProc)ToPrinterProc, this);
target_radio[0] = but;
n = 0;
XtSetArg(wargs[n], XmNx, 244); ++n;
XtSetArg(wargs[n], XmNy, 10 + RADIO_OFFSET); ++n;
XtSetArg(wargs[n], XmNmarginHeight, 2); ++n;
XtSetArg(wargs[n], XmNindicatorType, XmONE_OF_MANY); ++n;
XtSetArg(wargs[n], XmNset, mToFile == 1); ++n;
but = XtCreateManagedWidget("File",xmToggleButtonWidgetClass,w,wargs,n);
XtAddCallback(but,XmNvalueChangedCallback,(XtCallbackProc)ToFileProc, this);
target_radio[1] = but;
n = 0;
XtSetArg(wargs[n], XmNx, 20); ++n;
XtSetArg(wargs[n], XmNy, 42); ++n;
XtSetArg(wargs[n], XmNalignment, XmALIGNMENT_BEGINNING); ++n;
str = XmStringCreateLtoR(sPrintMessage[printType][mToFile],XmFONTLIST_DEFAULT_TAG);
XtSetArg(wargs[n], XmNlabelString, str); ++n;
cmd_label = XtCreateManagedWidget("StringLabel", xmLabelWidgetClass, w, wargs, n);
XmStringFree(str);
n = 0;
XtSetArg(wargs[n], XmNx, 20); ++n;
XtSetArg(wargs[n], XmNy, 63); ++n;
XtSetArg(wargs[n], XmNwidth, 280); ++n;
print_text = XtCreateManagedWidget("printtext", xmTextWidgetClass, w, wargs, n);
XtAddCallback(print_text,XmNactivateCallback,(XtCallbackProc)PrintProc,this);
setTextString(print_text, data->print_string[mToFile]);
n = 0;
XtSetArg(wargs[n], XmNx, 20); ++n;
XtSetArg(wargs[n], XmNy, 105); ++n;
XtSetArg(wargs[n], XmNset, data->print_label != 0); ++n;
label_toggle = XtCreateManagedWidget("Label",xmToggleButtonWidgetClass,w,wargs,n);
XtAddCallback(label_toggle, XmNvalueChangedCallback, (XtCallbackProc)PrintLabelProc, this);
n = 0;
XtSetArg(wargs[n], XmNx, 95); ++n;
XtSetArg(wargs[n], XmNy, 105); ++n;
XtSetArg(wargs[n], XmNset, (data->print_col&0x01) != 0); ++n;
col_toggle = XtCreateManagedWidget("White Bkg",xmToggleButtonWidgetClass,w,wargs,n);
XtAddCallback(col_toggle, XmNvalueChangedCallback, (XtCallbackProc)ColoursProc, this);
n = 0;
XtSetArg(wargs[n], XmNx, 203); ++n;
XtSetArg(wargs[n], XmNy, 105); ++n;
XtSetArg(wargs[n], XmNset, (data->print_col&0x02) != 0); ++n;
grey_toggle = XtCreateManagedWidget("Greyscale",xmToggleButtonWidgetClass,w,wargs,n);
XtAddCallback(grey_toggle, XmNvalueChangedCallback, (XtCallbackProc)GreyProc, this);
SetColours(data->print_col);
ShowLabels(data->print_label);
data->mSpeaker->AddListener(this);
}
PPrintWindow::~PPrintWindow()
{
SaveSettings(); // save our settings
// restore original color/label settings
SetColours(mSaveCol);
ShowLabels(mSaveLabel);
}
void PPrintWindow::Listen(int message, void *dataPt)
{
ImageData *data;
switch (message) {
case kMessageColoursChanged:
// the outside colours changed
mSaveCol = GetData()->image_col;
setToggle(col_toggle, (mSaveCol & 0x01) != 0);
setToggle(grey_toggle, (mSaveCol & 0x02) != 0);
break;
case kMessageShowLabelChanged:
// the label has been changed externally
mSaveLabel = GetData()->show_label;
setToggle(label_toggle, mSaveLabel);
break;
case kMessageWillWriteSettings:
SaveSettings(); // make sure the settings are updated
// temporarily restore original color/label settings
data = GetData();
data->image_col = mSaveCol;
data->show_label = mSaveLabel;
break;
case kMessageWriteSettingsDone:
// return the color/label settings to their proper values
data = GetData();
data->image_col = data->print_col;
data->show_label = data->print_label;
break;
}
}
void PPrintWindow::SaveSettings()
{
ImageData *data = GetData();
// save current print settings
data->print_col = data->image_col;
data->print_label = data->show_label;
// save our print string
char *print_string = data->print_string[data->print_to];
char *str = XmTextGetString(print_text);
strncpy(print_string, str, FILELEN-1);
XtFree(str);
}
void PPrintWindow::SetPrintType(EPrintType printType)
{
if (mPrintType != printType) {
mPrintType = printType;
SetTitle(sPrintTitle[printType]);
setLabelString(cmd_label, sPrintMessage[printType][mData->print_to]);
setLabelString(target_label, sPrintLabel[printType]);
}
Raise();
}
void PPrintWindow::SetColours(int colourSet)
{
Ignore(); // ignore messages generated by ourself
PResourceManager::SetColours(colourSet);
Ignore(0);
}
void PPrintWindow::ShowLabels(int on)
{
ImageData *data = GetData();
Ignore(); // ignore messages generated by ourself
setLabel(data, on);
Ignore(0);
}
void PPrintWindow::SetTarget(int to_file)
{
ImageData *data = GetData();
if (mToFile != to_file) {
// turn off other radio button
setToggle(target_radio[data->print_to], 0);
setLabelString(cmd_label, sPrintMessage[mPrintType][to_file]);
// save our current settings
SaveSettings();
// change print string
setTextString(print_text, data->print_string[to_file]);
// change print button label
if (to_file)
setLabelString(print_button, "Save");
else
setLabelString(print_button, "Print");
// finally, update the print target variable in our ImageData
mToFile = data->print_to = to_file;
} else {
// must turn radio button back on
setToggle(target_radio[mToFile], 1);
}
}
// NotifyRaised - callback made when another window is raised
void PPrintWindow::NotifyRaised(PImageWindow *theWindow)
{
// continue with printing
ContinuePrinting(theWindow);
}
// PromptToClick - prompt the user to click on a window for printing
void PPrintWindow::PromptToClick()
{
char buff[256];
static char *type_str[] = { "image", "window" };
static char *to_str[] = { "print", "save" };
// hide all other widgets
XtUnmapWidget(print_button);
XtUnmapWidget(cancel_button);
XtUnmapWidget(print_text);
XtUnmapWidget(col_toggle);
XtUnmapWidget(grey_toggle);
XtUnmapWidget(label_toggle);
XtUnmapWidget(target_label);
XtUnmapWidget(target_radio[0]);
XtUnmapWidget(target_radio[1]);
// change the command label to a prompt
sprintf(buff," Now click on the %s to %s.", type_str[mPrintType], to_str[mToFile]);
if (mPrintType == kPrintWindow) {
char *pt = strchr(buff,'\0');
sprintf(pt,"\n\n Or click and drag to define an area\n"
" of the screen to %s.", to_str[mToFile]);
}
setLabelString(cmd_label, buff);
// write similar message to console output
Printf("Click on the %s to %s...\n", type_str[mPrintType], to_str[mToFile]);
// must update this window now for "Print Window"
// because we won't handle any more events until
// our exec returns after printing is done.
if (mPrintType == kPrintWindow) {
XmUpdateDisplay(GetMainPane());
}
}
void PPrintWindow::DoPrint()
{
char *home = getenv("HOME");
ImageData *data = GetData();
static char *delim = " \t\n\r";
// save current settings
SaveSettings();
// make a working copy of the text
char *print_string = data->print_string[mToFile];
strcpy(mPrintName, print_string);
if (home) {
strcpy(mTempFilename,home);
strcat(mTempFilename,"/");
} else {
mTempFilename[0] = '\0';
}
strcat(mTempFilename,"aged_tmp.eps");
// create argument list for printer
mPrintFlags = 0;
mArgc = 0;
mArgs[mArgc] = strtok(mPrintName, delim);
while (mArgs[mArgc]) {
if (mPrintType == kPrintImage) { // check for "Print Image" options
if (!strcmp(mArgs[mArgc], "-landscape")) {
mPrintFlags |= kPrintLandscape; // set landscape mode
--mArgc; // eat this argument
}
}
// leave room for one additional argument (filename) plus NULL termination
if (++mArgc >= kMaxPrintArgs-2) break;
// get the next argument
mArgs[mArgc] = strtok(NULL, delim);
}
// make sure we have at least one argument
if (!mArgs[0] || !*mArgs[0]) {
Printf("Print syntax error\x07\n");
return;
}
if (!mToFile) {
// add temporary filename to list of arguments for print command
mArgs[mArgc++] = mTempFilename;
// argument list must be NULL terminated
mArgs[mArgc] = NULL;
} else {
// argument list must be NULL terminated
mArgs[mArgc] = NULL;
// check to see if output file exists
FILE *fp = fopen(mArgs[mArgc-1],"r");
if (fp) {
fclose(fp);
// open warning dialog
if (mWarnDialog) {
XtDestroyWidget(mWarnDialog);
mWarnDialog = NULL;
}
XmString str;
Arg wargs[10];
int n;
str = XmStringCreateLocalized("File exists. Overwrite it? ");
n = 0;
XtSetArg(wargs[n], XmNtitle, "Warning"); ++n;
XtSetArg(wargs[n], XmNmessageString, str); ++n;
XtSetArg(wargs[n], XmNdefaultButtonType, XmDIALOG_CANCEL_BUTTON); ++n;
mWarnDialog = XmCreateWarningDialog(GetShell(), "agedWarn",wargs,n);
XmStringFree(str); // must free the string
XtUnmanageChild(XmMessageBoxGetChild(mWarnDialog,XmDIALOG_HELP_BUTTON));
XtAddCallback(mWarnDialog,XmNcancelCallback,(XtCallbackProc)WarnCancelProc,this);
XtAddCallback(mWarnDialog,XmNokCallback,(XtCallbackProc)WarnOKProc,this);
XtAddCallback(mWarnDialog,XtNdestroyCallback,(XtCallbackProc)WarnDestroyProc,this);
XtManageChild(mWarnDialog);
return;
}
}
ContinuePrinting(NULL);
}
/* continue printing of specified window (or select window if aWindow==NULL) */
void PPrintWindow::ContinuePrinting(PImageWindow *aWindow)
{
int delete_this = 0;
pid_t pid;
#ifndef NO_FORK
if (mPrintType == kPrintWindow) {
pid = fork();
} else
#endif
if (aWindow) {
// print the image to file
if (!aWindow->GetImage()->Print(mArgs[mArgc-1], mPrintFlags)) {
Printf("Error writing image to %s\n",mArgs[mArgc-1]);
return;
}
// set bogus pid to bypass fork logic
pid = 1;
} else {
PromptToClick();
// wait for a window to be raised
ArmNotifyRaised();
return;
}
if (pid == -1) {
Printf("Fork 1 error while capturing image\x07\n");
} else if (pid) {
if (mPrintType == kPrintWindow) {
PromptToClick();
waitpid(pid, NULL, 0); // wait for capture program to finish
}
if (mToFile) {
Printf("Done printing %s to file.\n", mPrintType ? "window" : "image");
delete_this = 1; // delete the print window
}
#ifndef NO_FORK
else {
pid = fork();
if (pid == -1) {
Printf("Fork 2 error while capturing image\x07\n");
delete_this = 1; // delete the print window
} else if (pid) {
waitpid(pid, NULL, 0); // wait for print program to finish
Printf("Done writing image to %s.\n",mArgs[0]);
unlink(mTempFilename); // erase the temporary file
delete_this = 1; // delete the print window
} else {
// print the temporary image file
execvp(mArgs[0], mArgs);
Printf("%s error executing %s\x07\n",strerror(errno),mArgs[0]);
// this form of exit does not call cleanup routines
// - we don't want it to -- otherwise settings will be saved, etc.
_exit(1);
}
}
#endif // NO_FORK
}
#ifndef NO_FORK
else {
if (mToFile) {
int i;
// make room for additional capture arguments
for (i=kMaxPrintArgs-1; i>=kNumCaptureArgs; --i) {
mArgs[i] = mArgs[i-kNumCaptureArgs];
}
while (i >= 0) {
mArgs[i] = sCaptureCommand[i];
--i;
}
execvp(mArgs[0], mArgs); // execute capture program with specified options
} else {
// add temporary file to list of capture arguments
// (note, capture argument list is already NULL terminated)
sCaptureCommand[kNumCaptureArgs] = mTempFilename;
// execute capture program
execvp(sCaptureCommand[0], sCaptureCommand);
}
Printf("Error executing %s\x07\n",sCaptureCommand[0]);
_exit(1);
}
#endif // NO_FORK
// delete this print window when we are all done
if (delete_this) {
delete this;
}
}
void PPrintWindow::CancelProc(Widget w, PPrintWindow *printWin, caddr_t call_data)
{
// delete the print dialog
delete printWin;
}
void PPrintWindow::ToPrinterProc(Widget w, PPrintWindow *printWin, caddr_t call_data)
{
#ifdef NO_FORK
Printf("Sorry, Print to device not supported by this version of Aged\x07\n");
setToggle(printWin->target_radio[0], 0); // turn "Printer" radio back off
#else
printWin->SetTarget(0);
#endif
}
void PPrintWindow::ToFileProc(Widget w, PPrintWindow *printWin, caddr_t call_data)
{
printWin->SetTarget(1);
}
void PPrintWindow::ColoursProc(Widget w, PPrintWindow *printWin, caddr_t call_data)
{
ImageData *data = printWin->GetData();
printWin->SetColours(data->image_col ^ kWhiteBkg);
}
void PPrintWindow::GreyProc(Widget w, PPrintWindow *printWin, caddr_t call_data)
{
ImageData *data = printWin->GetData();
printWin->SetColours(data->image_col ^ kGreyscale);
}
void PPrintWindow::PrintLabelProc(Widget w, PPrintWindow *printWin, caddr_t call_data)
{
ImageData *data = printWin->GetData();
printWin->ShowLabels(data->show_label ^ 0x01);
}
// print image to file or printer
void PPrintWindow::PrintProc(Widget w, PPrintWindow *printWin, caddr_t call_data)
{
printWin->DoPrint();
}
void PPrintWindow::WarnCancelProc(Widget w, PPrintWindow *printWin, caddr_t call_data)
{
// delete the warning dialog
XtDestroyWidget(printWin->mWarnDialog);
printWin->mWarnDialog = NULL;
}
void PPrintWindow::WarnOKProc(Widget w, PPrintWindow *printWin, caddr_t call_data)
{
XtDestroyWidget(printWin->mWarnDialog);
printWin->mWarnDialog = NULL;
// continue with printing
printWin->ContinuePrinting(NULL);
}
// the warning dialog was destroyed
void PPrintWindow::WarnDestroyProc(Widget w, PPrintWindow *printWin, caddr_t call_data)
{
// must verify that it is the current dialog being destroyed
// (could a delayed callback from one we destroyed ourself already)
if (printWin->mWarnDialog == w) {
printWin->mWarnDialog = NULL;
}
}