Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / programs / dtmail / dtmail / DmxPrintSetup.C
1 /* $TOG: DmxPrintSetup.C /main/17 1997/09/03 17:34:59 mgreess $ */
2
3 /*
4  *+SNOTICE
5  *
6  *      $:$
7  *
8  *      RESTRICTED CONFIDENTIAL INFORMATION:
9  *      
10  *      The information in this document is subject to special
11  *      restrictions in a confidential disclosure agreement between
12  *      HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
13  *      document outside HP, IBM, Sun, USL, SCO, or Univel without
14  *      Sun's specific written approval.  This document and all copies
15  *      and derivative works thereof must be returned or destroyed at
16  *      Sun's request.
17  *
18  *      Copyright 1994 Sun Microsystems, Inc.  All rights reserved.
19  *
20  *+ENOTICE
21  */
22
23 /*
24  *                   Common Desktop Environment
25  *
26  *   (c) Copyright 1993, 1994, 1995 Hewlett-Packard Company
27  *   (c) Copyright 1993, 1994, 1995 International Business Machines Corp.
28  *   (c) Copyright 1993, 1994, 1995 Sun Microsystems, Inc.
29  *   (c) Copyright 1993, 1994, 1995 Novell, Inc.
30  *   (c) Copyright 1995 Digital Equipment Corp.
31  *   (c) Copyright 1995 Fujitsu Limited
32  *   (c) Copyright 1995 Hitachi, Ltd.
33  *                                                                   
34  *
35  *                     RESTRICTED RIGHTS LEGEND                              
36  *
37  *Use, duplication, or disclosure by the U.S. Government is subject to
38  *restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
39  *Technical Data and Computer Software clause in DFARS 252.227-7013.  Rights
40  *for non-DOD U.S. Government Departments and Agencies are as set forth in
41  *FAR 52.227-19(c)(1,2).
42
43  *Hewlett-Packard Company, 3000 Hanover Street, Palo Alto, CA 94304 U.S.A.
44  *International Business Machines Corp., Route 100, Somers, NY 10589 U.S.A. 
45  *Sun Microsystems, Inc., 2550 Garcia Avenue, Mountain View, CA 94043 U.S.A.
46  *Novell, Inc., 190 River Road, Summit, NJ 07901 U.S.A.
47  *Digital Equipment Corp., 111 Powdermill Road, Maynard, MA 01754, U.S.A.
48  *Fujitsu Limited, 1015, Kamikodanaka Nakahara-Ku, Kawasaki 211, Japan
49  *Hitachi, Ltd., 6, Kanda Surugadai 4-Chome, Chiyoda-ku, Tokyo 101, Japan
50  */
51 #include <stdio.h>
52 #include <sys/param.h>
53
54 #include <Dt/Print.h>
55 #include <Xm/Form.h>
56 #include <Xm/PushB.h>
57 #include <Xm/ToggleB.h>
58 #include <Xm/ToggleBG.h>
59
60 #include "Dmx.h"
61 #include "DmxPrintOptions.h"
62 #include "DmxPrintSetup.h"
63 #include "DtMailHelp.hh"
64 #include "MailMsg.h"
65 #include "OptCmd.h"
66 #include "RoamApp.h"
67 #include "RoamMenuWindow.h"
68
69 static DtMailBoolean    DmxPrintSetup_print_separately = DTM_FALSE;
70 static DtMailBoolean    DmxPrintSetup_use_word_wrap = DTM_TRUE;
71 static DtMailBoolean    DmxPrintSetup_print_to_file = DTM_FALSE;
72 static char             *DmxPrintSetup_printer_name = NULL;
73 #ifdef REUSE_PRINT_SETUP_DIALOGS
74 static Widget           DmxPrintSetup_default_dtprint_setup = NULL;
75 #endif
76
77 DmxPrintSetup::DmxPrintSetup (
78                 Widget window,
79                 XtCallbackProc printCB, XtPointer printClosure,
80                 XtCallbackProc cancelCB, XtPointer cancelClosure,
81                 XtCallbackProc closeDisplayCB, XtPointer closeDisplayClosure,
82                 XtCallbackProc pdmSetupCB, XtPointer pdmSetupClosure)
83 {
84     _parent = window;
85     _dtprint_setup = (Widget) NULL;
86     _widgets = (PrintSetupWidgets *) NULL;
87     _use_word_wrap = DmxPrintSetup_use_word_wrap;
88     _print_separately = DmxPrintSetup_print_separately;
89     _print_to_file = DmxPrintSetup_print_to_file;
90     if (NULL != DmxPrintSetup_printer_name)
91       _printer_name = strdup(DmxPrintSetup_printer_name);
92     else
93       _printer_name = NULL;
94
95     _filename = (char*) malloc(MAXPATHLEN+1);
96     if (NULL != _filename)
97       sprintf(_filename, "%s/dtmail_messages.ps", getenv("HOME"));
98
99     _printCB = printCB;
100     _cancelCB = cancelCB;
101     _closeDisplayCB = closeDisplayCB;
102     _pdmSetupCB = pdmSetupCB;
103
104     _printClosure = printClosure;
105     _cancelClosure = cancelClosure;
106     _closeDisplayClosure = closeDisplayClosure;
107     _pdmSetupClosure = pdmSetupClosure;
108
109     attachPrintSetupDialog();
110     XtRealizeWidget(_dtprint_setup);
111 }
112
113 DmxPrintSetup::~DmxPrintSetup (void)
114 {
115     if (NULL != _printer_name)
116       free(_printer_name);
117
118     if (NULL != _filename)
119       free(_filename);
120
121     detachPrintSetupDialog();
122 }
123
124 void
125 DmxPrintSetup::setPrintToFileName (char *filename)
126 {
127     if (NULL != _filename)
128       free(_filename);
129     
130     _filename = strdup(filename);
131
132     if (NULL != _filename)
133       XtVaSetValues(_dtprint_setup, DtNfileName, _filename, NULL);
134 }
135
136 void
137 DmxPrintSetup::display (void)
138 {
139     if (_dtprint_setup == NULL) return;
140
141 #ifdef REUSE_PRINT_SETUP_DIALOGS
142     Position x1, y1, x2, y2;
143     Dimension w1, h1, w2, h2;
144     XtVaGetValues(
145                 XtParent(_dtprint_setup),
146                 XmNx, &x1, XmNy, &y1,
147                 XmNwidth, &w1, XmNheight, &h1,
148                 NULL);
149     XtVaGetValues(
150                 _parent,
151                 XmNx, &x2, XmNy, &y2,
152                 XmNwidth, &w2, XmNheight, &h2,
153                 NULL);
154     XtVaSetValues(
155                 XtParent(_dtprint_setup),
156                 XmNx, x2 + (w2 - w1) / 2, XmNy, y2 + (h2 - h1) / 2,
157                 NULL);
158 #endif
159
160     XtManageChild(_dtprint_setup);
161
162     if (NULL != _printer_name)
163       XtVaSetValues(_widgets->printer_name_tf, XmNvalue, _printer_name, NULL);
164 }
165
166 DtMailBoolean
167 DmxPrintSetup::getDefaultPrintData (DtPrintSetupData *print_data)
168 {
169     if (_dtprint_setup == NULL || print_data == NULL) return DTM_FALSE;
170     if (DtPRINT_SUCCESS == DtPrintFillSetupData(_dtprint_setup, print_data))
171       return DTM_TRUE;
172     else
173       return DTM_FALSE;
174 }
175
176 DtMailBoolean
177 DmxPrintSetup::printSeparately (void)
178 {
179     if (DTM_TRUE != _print_to_file)
180       return _print_separately;
181
182     return DTM_FALSE;
183 }
184
185 DtMailBoolean
186 DmxPrintSetup::useWordWrap (void)
187 {
188     return _use_word_wrap;
189 }
190
191 void
192 DmxPrintSetup::attachPrintSetupDialog (void)
193 {
194     unsigned char is_set;
195     Widget dialog;
196
197 #ifdef REUSE_PRINT_SETUP_DIALOGS
198     if (DmxPrintSetup_default_dtprint_setup != NULL)
199     {
200         //
201         // Attempt to reuse the last print setup and print display connection.
202         //
203         _dtprint_setup = DmxPrintSetup_default_dtprint_setup;
204         DmxPrintSetup_default_dtprint_setup = NULL;
205     }
206     else
207       _dtprint_setup = createPrintSetupDialog(theRoamApp.baseWidget());
208 #else
209     _dtprint_setup = createPrintSetupDialog(_parent);
210 #endif
211
212     //
213     // Copy the information about the widgets into the setup.
214     //
215     XtVaGetValues(_dtprint_setup, XmNuserData, &_widgets, NULL);
216
217     //
218     // Add the setup-specific callbacks
219     //
220     XtAddCallback(
221                 _dtprint_setup,
222                 DtNprintCallback,
223                 DmxPrintSetup::printCB,
224                 (XtPointer) this);
225     XtAddCallback(
226                 _dtprint_setup,
227                 DtNcancelCallback,
228                 _cancelCB,
229                 (XtPointer) _cancelClosure);
230     XtAddCallback(
231                 _dtprint_setup,
232                 DtNclosePrintDisplayCallback,
233                 _closeDisplayCB,
234                 (XtPointer) _closeDisplayClosure);
235     XtAddCallback(
236                 _dtprint_setup,
237                 DtNsetupCallback,
238                 _pdmSetupCB,
239                 (XtPointer) _pdmSetupClosure);
240
241     is_set = (_print_separately) ? XmSET : XmUNSET;
242     XtVaSetValues(_widgets->print_separately_tb, XmNset, is_set, NULL);
243
244     is_set = (_use_word_wrap) ? XmSET : XmUNSET;
245     XtVaSetValues(_widgets->use_word_wrap_tb, XmNset, is_set, NULL);
246
247     if (DTM_TRUE == _print_to_file)
248       XtVaSetValues(
249                 _dtprint_setup,
250                 DtNprintDestination, DtPRINT_TO_FILE,
251                 NULL);
252     else
253       XtVaSetValues(
254                 _dtprint_setup,
255                 DtNprintDestination, DtPRINT_TO_PRINTER,
256                 NULL);
257
258     if (NULL != _printer_name)
259       XtVaSetValues(_dtprint_setup, DtNprinterName, _printer_name, NULL);
260
261     if (NULL != _filename)
262       XtVaSetValues(_dtprint_setup, DtNfileName, _filename, NULL);
263
264     dialog = XtParent(_dtprint_setup);
265     if (NULL != dialog && XtIsShell(dialog))
266     {
267         char *title = (char*) GETMSG(DT_catd, 21, 21, "Mailer - Print Setup");
268         XtVaSetValues(dialog, XmNtitle, title, NULL);
269     }
270     else
271     {
272         fprintf(stderr, "Print Setup box is not parented to a shell\n");
273     }
274 }
275
276
277 Widget
278 DmxPrintSetup::createPrintSetupDialog (Widget parent)
279 {
280     PrintSetupWidgets   *widgets;
281     XmString            xms;
282
283     //
284     // Create the app-specific widgets for the Setup Dialog.
285     widgets = (PrintSetupWidgets *) XtMalloc(sizeof(PrintSetupWidgets));
286
287     //
288     // Create the DtPrintSetupDialog and specify that the application
289     // specific area be located below the generic area.
290     // Save the PrintSetupWidgets record as UserData so it can be
291     // retrieved later.
292     //
293     widgets->dtprint_setup =
294       DtCreatePrintSetupDialog(parent, "Setup", NULL, 0);
295
296     XtVaSetValues(
297                 widgets->dtprint_setup,
298                 DtNworkAreaLocation, DtWORK_AREA_BOTTOM,
299                 XmNuserData, widgets,
300                 NULL);
301
302     XtAddCallback(
303                 widgets->dtprint_setup,
304                 XmNhelpCallback,
305                 HelpCB,
306                 DTMAILPRINTSETUPDIALOG);
307
308     XtAddCallback(
309                 widgets->dtprint_setup,
310                 XmNdestroyCallback,
311                 &DmxPrintSetup::destroyPrintSetupDialogCB,
312                 (XtPointer) widgets);
313
314     widgets->form = XtVaCreateWidget(
315                 "PrintSetupForm",
316                 xmFormWidgetClass,
317                 widgets->dtprint_setup,
318                 NULL);
319                  
320     xms = XmStringCreateLocalized(GETMSG(DT_catd, 21, 12, "Print Separately"));
321     widgets->print_separately_tb = XtVaCreateManagedWidget(
322                                 "PrintSeparatelyTB",
323                                 xmToggleButtonWidgetClass,
324                                 widgets->form,
325                                 XmNalignment, XmALIGNMENT_BEGINNING,
326                                 XmNlabelString, xms,
327                                 XmNleftAttachment, XmATTACH_FORM,
328                                 XmNtopAttachment, XmATTACH_FORM,
329                                 NULL);
330     XmStringFree(xms);
331
332     xms = XmStringCreateLocalized(GETMSG(DT_catd, 21, 13, "Use Word Wrap"));
333     widgets->use_word_wrap_tb = XtVaCreateManagedWidget(
334                                 "UseWordWrapTB",
335                                 xmToggleButtonWidgetClass,
336                                 widgets->form,
337                                 XmNalignment, XmALIGNMENT_BEGINNING,
338                                 XmNlabelString, xms,
339                                 XmNleftAttachment, XmATTACH_FORM,
340                                 XmNrightAttachment, XmATTACH_FORM,
341                                 XmNtopAttachment, XmATTACH_WIDGET,
342                                 XmNtopWidget, widgets->print_separately_tb,
343                                 NULL);
344     XmStringFree(xms);
345
346     xms = XmStringCreateLocalized(GETMSG(DT_catd, 21, 20, "More ..."));
347     widgets->more_options_pb = XtVaCreateManagedWidget(
348                                 "PrintOptionsPB",
349                                 xmPushButtonWidgetClass,
350                                 widgets->form,
351                                 XmNalignment, XmALIGNMENT_BEGINNING,
352                                 XmNlabelString, xms,
353                                 XmNleftAttachment, XmATTACH_NONE,
354                                 XmNrightAttachment, XmATTACH_FORM,
355                                 XmNtopAttachment, XmATTACH_WIDGET,
356                                 XmNtopWidget, widgets->use_word_wrap_tb,
357                                 NULL);
358     XmStringFree(xms);
359
360     XtAddCallback(
361                 widgets->more_options_pb,
362                 XmNactivateCallback,
363                 &DmxPrintSetup::moreOptionsCB,
364                 (XtPointer) NULL);
365
366     widgets->checkbox_tb =
367       XtNameToWidget(widgets->dtprint_setup, "DestRadioBox.button_1");
368     widgets->printer_name_tf = XtNameToWidget(widgets->dtprint_setup, "Name");
369
370     if (NULL != widgets->checkbox_tb)
371       XtAddCallback(
372                 widgets->checkbox_tb,
373                 XmNvalueChangedCallback,
374                 &DmxPrintSetup::destinationChangedCB,
375                 (XtPointer) widgets);
376
377     XtManageChild(widgets->form);
378     return(widgets->dtprint_setup);
379 }
380
381
382 void
383 DmxPrintSetup::detachPrintSetupDialog (void)
384 {
385     XtRemoveCallback(
386                 _dtprint_setup,
387                 DtNprintCallback,
388                 DmxPrintSetup::printCB,
389                 (XtPointer) this);
390     XtRemoveCallback(
391                 _dtprint_setup,
392                 DtNcancelCallback,
393                 _cancelCB,
394                 (XtPointer) _cancelClosure);
395     XtRemoveCallback(
396                 _dtprint_setup,
397                 DtNclosePrintDisplayCallback,
398                 _closeDisplayCB,
399                 (XtPointer) _closeDisplayClosure);
400     XtRemoveCallback(
401                 _dtprint_setup,
402                 DtNsetupCallback,
403                 _pdmSetupCB,
404                 (XtPointer) _pdmSetupClosure);
405
406     //
407     // Leave the Print Setup Dialog for the next print job.
408     //
409 #ifdef REUSE_PRINT_SETUP_DIALOGS
410     if (DmxPrintSetup_default_dtprint_setup != NULL)
411     {
412         XtDestroyWidget(DmxPrintSetup_default_dtprint_setup);
413         DmxPrintSetup_default_dtprint_setup = NULL;
414     }
415     DmxPrintSetup_default_dtprint_setup = _dtprint_setup;
416 #else
417     XtDestroyWidget(_dtprint_setup);
418 #endif
419 }
420
421
422 void
423 DmxPrintSetup::savePrintSetupOptions(DtPrintSetupData *print_data)
424 {
425     unsigned char       is_set;
426
427     XtVaGetValues(_widgets->print_separately_tb, XmNset, &is_set, NULL);
428     _print_separately = (is_set == XmSET) ? DTM_TRUE : DTM_FALSE;
429     DmxPrintSetup_print_separately = _print_separately;
430
431     XtVaGetValues(_widgets->use_word_wrap_tb, XmNset, &is_set, NULL);
432     _use_word_wrap = (is_set == XmSET) ? DTM_TRUE : DTM_FALSE;
433     DmxPrintSetup_use_word_wrap = _use_word_wrap;
434     
435     _print_to_file =
436         (DtPRINT_TO_FILE == print_data->destination) ? DTM_TRUE : DTM_FALSE;
437     DmxPrintSetup_print_to_file = _print_to_file;
438
439     if (NULL != _printer_name)
440       free(_printer_name);
441     _printer_name = strdup(print_data->printer_name);
442
443     if (NULL != DmxPrintSetup_printer_name)
444       free(DmxPrintSetup_printer_name);
445     DmxPrintSetup_printer_name = strdup(_printer_name);
446
447     if (NULL != _filename)
448       free(_filename);
449     _filename = strdup(print_data->dest_info);
450 }
451
452
453 void
454 DmxPrintSetup::destinationChangedCB(
455                                 Widget checkbox_tb,
456                                 XtPointer client_data,
457                                 XtPointer)
458 {
459     PrintSetupWidgets   *widgets = (PrintSetupWidgets*) client_data;
460     Boolean             toggleFlag;
461
462     if (NULL == checkbox_tb) return;
463
464     XtVaGetValues(checkbox_tb, XmNset, &toggleFlag, NULL);
465     if(toggleFlag)
466       XtVaSetValues(
467                 widgets->print_separately_tb,
468                 XmNsensitive, False,
469                 XmNset, False,
470                 NULL);
471     else
472       XtVaSetValues(widgets->print_separately_tb, XmNsensitive, True, NULL);
473 }
474
475
476 void
477 DmxPrintSetup::destroyPrintSetupDialogCB(Widget, XtPointer widgets, XtPointer)
478 {
479     XtFree((char *) widgets);
480 }
481
482
483 void
484 DmxPrintSetup::moreOptionsCB(Widget, XtPointer, XtPointer)
485 {
486     OptCmd              *optCmd = (OptCmd *) theRoamApp.mailOptions();
487
488     optCmd->displayPrintingOptionsPane();
489 }
490
491 void
492 DmxPrintSetup::printCB(Widget w, XtPointer client_data, XtPointer call_data)
493 {
494     DmxPrintSetup               *thisSetup = (DmxPrintSetup*) client_data;
495     DtPrintSetupCallbackStruct  *pbs = (DtPrintSetupCallbackStruct*) call_data;
496
497     thisSetup->_printCB(w, thisSetup->_printClosure, call_data);
498     thisSetup->savePrintSetupOptions(pbs->print_data);
499 }