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