Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtinfo / dtinfo / src / Agents / PrintPanelAgentMotif.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 librararies 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 /*
24  *  $TOG: PrintPanelAgentMotif.C /main/35 1997/08/01 15:17:10 samborn $
25  *
26  * (c) Copyright 1996 Digital Equipment Corporation.
27  * (c) Copyright 1996 Hewlett-Packard Company.
28  * (c) Copyright 1996 International Business Machines Corp.
29  * (c) Copyright 1996 Sun Microsystems, Inc.
30  * (c) Copyright 1996 Novell, Inc. 
31  * (c) Copyright 1996 FUJITSU LIMITED.
32  * (c) Copyright 1996 Hitachi.
33  *
34  * Copyright (c) 1992 HAL Computer Systems International, Ltd.
35  * All rights reserved.  Unpublished -- rights reserved under
36  * the Copyright Laws of the United States.  USE OF A COPYRIGHT
37  * NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
38  * OR DISCLOSURE.
39  * 
40  * THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE
41  * SECRETS OF HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.  USE,
42  * DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE
43  * PRIOR EXPRESS WRITTEN PERMISSION OF HAL COMPUTER SYSTEMS
44  * INTERNATIONAL, LTD. 
45  * 
46  *                         RESTRICTED RIGHTS LEGEND
47  * Use, duplication, or disclosure by the Government is subject
48  * to the restrictions as set forth in subparagraph (c)(l)(ii)
49  * of the Rights in Technical Data and Computer Software clause
50  * at DFARS 252.227-7013.
51  *
52  *          HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
53
54  *                  1315 Dell Avenue
55  *                  Campbell, CA  95008
56  * 
57  */
58
59 //------------- Includes -----------------------------------
60
61 #define C_xList
62 #define L_Support
63
64 #define C_TOC_Element
65 #define C_NodeViewInfo
66 #define L_Basic
67
68 #define C_Printgr
69 #define C_PrintMgr
70 #define C_PrefMgr
71 #define C_MessageMgr
72 #define L_Managers
73
74 #define C_PrintPanelAgent
75 #define C_HelpAgent
76 #define L_Agents
77
78 #include <X11/IntrinsicP.h>
79 #include <X11/CoreP.h>
80
81 #include <Xm/MessageB.h>
82 #include <Xm/PushB.h>
83 #include <Xm/Scale.h>
84
85 #include <DtI/DisplayAreaP.h>
86 #include <DtI/DisplayAreaI.h>
87
88 #include <Prelude.h>
89
90 #include "Other/XmStringLocalized.hh"
91 #include "Managers/CatMgr.hh"
92
93 #include "Registration.hh"
94
95 #include <WWL/WXmDialogShell.h>
96 #include <WWL/WXmForm.h>
97 #include <WWL/WXmFrame.h>
98 #include <WWL/WXmPushButton.h>
99 #include <WWL/WXmSeparator.h>
100 #include <stream.h>
101
102 #define CLASS PrintPanelAgent
103
104 #include "create_macros.hh"
105
106 #include <sys/types.h>
107 #include <sys/stat.h>
108 #include <unistd.h>
109 #include <fcntl.h>
110 #include <errno.h>
111
112
113 #define AM WAutoManage
114 #define WXmToggleBtn WXmToggleButton
115 #define WXmPushBtn WXmPushButton
116
117 #if 0
118 #  define RCS_DEBUG(statement) cerr << statement << endl
119 #else
120 #  define RCS_DEBUG(statement)
121 #endif
122
123 //-------- External References ---------------------------------
124
125 extern Widget footer_book_name;
126 extern Widget footer_page_no;
127 extern DtHelpDispAreaStruct *gHelpDisplayArea;
128
129 //------------ Global Variables -----------------------------
130
131 Widget f_print_shell = NULL;
132 Widget f_scrollbar = NULL;
133 Widget f_dialog = NULL;
134 const char *f_printOrientation = NULL;
135 Boolean print_server_error = False;
136
137 //------------ Private Variables -----------------------------
138
139 //  Prefixed with an l to distinguish from f_AppPrintData in 
140 //  AppPrintData class.  
141 AppPrintData *  l_AppPrintData; 
142
143 static  WXmForm                 f_print_panel;
144 static  WXmLabel                f_selected_field;
145 static  WXmLabel                f_to_print_field;
146 static  WXmToggleButton         f_print_nodes;
147 static  WXmToggleButton         f_print_hierarchy;
148
149 static Boolean print_hierarchy;  // keep track of hierarchy vs section
150
151 static void PrintEverything(AppPrintData *p);
152 static void PrintOneUASCommon(UAS_Pointer<UAS_Common> &doc, Widget pshell, int *cur_pageP);
153
154 PrintPanelAgent::PrintPanelAgent()
155 {
156     RCS_DEBUG("PrintPanelAgent constructor entered.\n");
157
158     l_AppPrintData = new AppPrintData();
159     cout << "overloaded PrintPanelAgenct constructor called.\n";   
160
161     RCS_DEBUG("PrintPanelAgent constructor exiting.\n");   
162 }
163
164 PrintPanelAgent::PrintPanelAgent(AppPrintData *p)
165 {
166     l_AppPrintData = p;
167     cout << "overloaded PrintPanelAgenct constructor called.\n";   
168 }
169
170 PrintPanelAgent::~PrintPanelAgent()
171 {
172     cout << "PrintPanelAgenct destructor called.\n";
173 }
174
175 /*
176  * ------------------------------------------------------------------------
177  * Name:  reset_ui
178  *
179  * Description:
180  *     Resets the number of nodes and what to print buttons.
181  *
182  * Return value:
183  *     None.
184  *
185  */
186 void
187 reset_ui(AppPrintData *p)
188 {
189    RCS_DEBUG("reset_ui called.\n");   
190
191     xList<UAS_Pointer<UAS_Common> > &print_list = *(p->f_print_list);
192     Xassert (print_list.length() > 0);
193     
194     // Set up the "Number of Nodes" labels. 
195     static char buffer[24];
196     
197     sprintf (buffer, "%d", print_list.length());
198     f_selected_field.LabelString (WXmString (buffer));
199     f_to_print_field.LabelString (WXmString (buffer));
200     
201     // Set up "What to print" buttons based on command line 
202     // value set in Window System 
203
204     print_hierarchy = window_system().videoShell()->hierarchy;
205
206     f_print_hierarchy.Set(print_hierarchy);
207     f_print_nodes.Set(!print_hierarchy);
208     
209     Boolean hierarchy_sensitive = False;
210     
211     List_Iterator<UAS_Pointer<UAS_Common> > l (print_list);
212
213     // NOTE: Could fail if toc() is NULL. 
214     UAS_List<UAS_Common> kids = l.item()->children();
215     if ((!strncmp(l.item()->locator(), "mmdb:", 5)) && print_list.length() == 1 && kids.length() > 0) {
216         hierarchy_sensitive = True;
217     }
218     
219     // If one is insensitive, the other one must be as well. 
220     f_print_nodes.SetSensitive (hierarchy_sensitive);
221     f_print_hierarchy.SetSensitive (hierarchy_sensitive);
222
223 }
224
225
226 /*
227  * ------------------------------------------------------------------------
228  * Name: customizePrintSetupBox
229  *
230  * Description:
231  *     Adds application specific items to the passed print setup box.
232  *
233  * Return value:
234  *
235  *     None.
236  *
237  */
238 void
239 customizePrintSetupBox(AppPrintData *p)
240 {
241     XmString label;
242
243     Widget print_dialog = p->f_print_dialog;
244     RCS_DEBUG("customizePrintSetupBox called.\n");   
245
246     Widget row, how_many_frame, how_many_title, how_many_form, selected_label, print_panel;
247     Widget to_print_label, what_frame, what_title, what_form, selected_field, to_print_field;
248     Widget print_nodes;
249
250     //  Create the app-specific top work area
251
252     f_print_panel = print_panel = XtVaCreateManagedWidget("workarea",
253                                             xmFormWidgetClass,
254                                             print_dialog,
255                                             XmNorientation, XmHORIZONTAL,
256                                             NULL);
257
258     // How many?
259     
260     how_many_frame = XtVaCreateManagedWidget("how_many_frame",
261                                              xmFrameWidgetClass,
262                                              print_panel, 
263                                              XmNleftAttachment, XmATTACH_FORM,
264                                              XmNrightAttachment, XmATTACH_POSITION,
265                                              XmNtopAttachment, XmATTACH_FORM,
266                                              XmNrightPosition, 49,
267                                              XmNtopOffset, 2,
268                                              XmNleftOffset, 5,
269                                              XmNmarginHeight, 2,
270                                              NULL);
271
272     label = XmStringGenerate(CATGETS(Set_AgentLabel, 205, "Number of Sections"), 
273                              NULL, XmCHARSET_TEXT, NULL);
274
275     how_many_title = XtVaCreateManagedWidget("how_many_title",
276                                              xmLabelWidgetClass,
277                                              how_many_frame,
278                                              XmNchildType, XmFRAME_TITLE_CHILD,
279                                              XmNlabelString, label,
280                                              NULL);
281
282     XmStringFree(label);
283
284     how_many_form = XtVaCreateManagedWidget("how_many_frame",
285                                             xmFormWidgetClass,
286                                             how_many_frame,
287                                             NULL);
288
289     label = XmStringGenerate(CATGETS(Set_AgentLabel, 206, "Selected:"),
290                              NULL, XmCHARSET_TEXT, NULL);
291
292     selected_label = XtVaCreateManagedWidget("selected_label", 
293                                              xmLabelWidgetClass, 
294                                              how_many_form, 
295                                              XmNlabelString, label,
296                                              XmNtopAttachment, XmATTACH_FORM,
297                                              XmNleftAttachment, XmATTACH_FORM,
298                                              XmNleftOffset, 4,
299                                              XmNtopOffset, 2,
300                                              NULL);
301     XmStringFree(label);
302
303     f_selected_field = selected_field = XtVaCreateManagedWidget("selected_field",
304                                                xmLabelWidgetClass, 
305                                                how_many_form, 
306                                                XmNlabelString, label,
307                                                XmNalignment, XmALIGNMENT_END,
308                                                XmNrightAttachment, XmATTACH_FORM,
309                                                XmNrightOffset, 2,
310                                                XmNtopAttachment, XmATTACH_FORM,
311                                                XmNtopOffset, 2,
312                                                NULL);
313
314     label = XmStringGenerate(CATGETS(Set_AgentLabel, 207, "To Be Printed:"),
315                              NULL, XmCHARSET_TEXT, NULL);
316     
317     to_print_label = XtVaCreateManagedWidget("to_print_label",
318                                              xmLabelWidgetClass,
319                                              how_many_form, 
320                                              XmNlabelString, label,
321                                              XmNtopAttachment, XmATTACH_WIDGET,
322                                              XmNtopWidget, selected_label,
323                                              XmNleftAttachment, XmATTACH_FORM,
324                                              XmNbottomAttachment, XmATTACH_FORM,
325                                              XmNleftOffset, 4,
326                                              XmNbottomOffset, 5,
327                                              XmNtopOffset, 8, 
328                                              NULL);
329     XmStringFree(label);
330
331     f_to_print_field = to_print_field = XtVaCreateManagedWidget("to_print_field",
332                                                xmLabelWidgetClass,
333                                                how_many_form, 
334                                                XmNalignment, XmALIGNMENT_END,
335                                                XmNtopAttachment, XmATTACH_WIDGET,
336                                                XmNtopWidget, selected_field,
337                                                XmNtopOffset, 8,
338                                                XmNrightAttachment, XmATTACH_FORM,
339                                                XmNrightOffset, 2,
340                                                XmNbottomAttachment, XmATTACH_FORM,
341                                                XmNbottomOffset, 5,
342                                                NULL);
343
344     // What?
345
346     what_frame = XtVaCreateManagedWidget("what_frame",
347                                          xmFrameWidgetClass,
348                                          print_panel,
349                                          XmNleftAttachment, XmATTACH_POSITION,
350                                          XmNleftPosition, 51,
351                                          XmNrightAttachment, XmATTACH_FORM,
352                                          XmNtopAttachment, XmATTACH_FORM,
353                                          XmNtopOffset, 2,
354                                          XmNrightOffset, 5,
355                                          NULL);
356
357     label = XmStringGenerate(CATGETS(Set_AgentLabel, 209, "What to Print"),
358                              NULL, XmCHARSET_TEXT, NULL);
359
360     what_title = XtVaCreateManagedWidget("what_title",
361                                          xmLabelWidgetClass,
362                                          what_frame,
363                                          XmNlabelString, label,
364                                          XmNchildType, XmFRAME_TITLE_CHILD,
365                                          NULL);
366     XmStringFree(label);
367     
368     what_form = XtVaCreateManagedWidget("what_form",
369                                         xmFormWidgetClass,
370                                         what_frame,
371                                         NULL);
372
373     label = XmStringGenerate(CATGETS(Set_AgentLabel, 210, "Sections"),
374                              NULL, XmCHARSET_TEXT, NULL);
375
376     f_print_nodes = print_nodes = XtVaCreateManagedWidget("print_nodes",
377                                             xmToggleButtonWidgetClass,
378                                             what_form,
379                                             XmNlabelString, label,
380                                             XmNset, TRUE,
381                                             XmNalignment, XmALIGNMENT_BEGINNING,
382                                             XmNindicatorType, XmONE_OF_MANY,
383                                             XmNleftAttachment, XmATTACH_FORM,
384                                             XmNrightAttachment, XmATTACH_FORM,
385                                             XmNtopAttachment, XmATTACH_FORM,
386                                             XmNleftOffset, 4,
387                                             XmNrightOffset, 4,
388                                             NULL);
389
390     XmStringFree(label);
391     XtAddCallback(print_nodes, XmNvalueChangedCallback, ToggleWhatCB, p);
392
393     label = XmStringGenerate(CATGETS(Set_AgentLabel, 211, "Hierarchy"),
394                              NULL, XmCHARSET_TEXT, NULL);
395         
396     f_print_hierarchy = XtVaCreateManagedWidget("print_hierarchy",
397                                                 xmToggleButtonWidgetClass,
398                                                 what_form,
399                                                 XmNlabelString, label,
400                                                 XmNalignment, XmALIGNMENT_BEGINNING,
401                                                 XmNindicatorType, XmONE_OF_MANY,
402                                                 XmNtopAttachment, XmATTACH_WIDGET,
403                                                 XmNtopWidget, print_nodes,
404                                                 XmNbottomAttachment, XmATTACH_FORM,
405                                                 XmNbottomOffset, 3,
406                                                 XmNleftAttachment, XmATTACH_FORM,
407                                                 XmNrightAttachment, XmATTACH_FORM,
408                                                 XmNleftOffset, 4,
409                                                 XmNrightOffset, 4,
410                                                 NULL);
411
412     XtAddCallback(f_print_hierarchy, XmNvalueChangedCallback, ToggleWhatCB, p);
413
414     XmStringFree(label);
415
416     RCS_DEBUG("customizePrintSetupBox exiting.\n");   
417
418 }
419
420 /*
421  * ------------------------------------------------------------------------
422  * Name: PdmNotifyCB
423  *
424  * Description:
425  *
426  *     Called when the PDM is up, or down.
427  *
428  */
429 void 
430 PdmNotifyCB(Widget pr_shell, XtPointer client_data, XtPointer call_data)
431 {
432     RCS_DEBUG("PdmNotifyCB called.\n");   
433     char *msg;
434
435     XmPrintShellCallbackStruct* pr_cbs = 
436         (XmPrintShellCallbackStruct*) call_data;
437     AppPrintData * p = (AppPrintData *) client_data ;
438     
439     if ((pr_cbs->reason == XmCR_PDM_NONE) ||
440         (pr_cbs->reason == XmCR_PDM_START_ERROR) ||
441         (pr_cbs->reason == XmCR_PDM_START_VXAUTH) ||
442         (pr_cbs->reason == XmCR_PDM_START_PXAUTH))
443     {
444         msg = CATGETS(Set_PrintPanelAgent, 12, "Print Dialog Manager error - setup failed.");
445         message_mgr().error_dialog(msg);
446     }
447  
448     RCS_DEBUG("PdmNotifyCB exiting.\n");   
449
450 }
451
452 /*
453  * ------------------------------------------------------------------------
454  * Name: CreatePrintShell
455  *
456  * Description:
457  *
458  *     Called when the user selects the "Print" or the "Setup..." button
459  *       in the setupbox.
460  *
461  */
462 void 
463 CreatePrintShell(Widget widget, AppPrintData* p)
464 {
465     char buf[BUFSIZ];
466     
467     RCS_DEBUG("CreatePrintShell called.\n");   
468
469     /*
470      * create a print_shell if none available.  the print dialog callback
471      * always provides valid printer context and print display initialized: 
472      * XpInitContext called, attributes set.
473      */
474     if (!p->f_print_shell) {
475         
476         p->f_print_shell = XmPrintSetup(widget, 
477                                         XpGetScreenOfContext(p->f_print_data->print_display,
478                                                              p->f_print_data->print_context),
479                                         "PrintShell", NULL, 0);
480
481
482         //  set the global variable to this value so that the create_ui function in 
483         //  NodePrintAgent will know what to parent it's form to
484         f_print_shell = p->f_print_shell;
485
486         //  set the print display in the window manager so the fontcache can connect
487         //  to the correct display
488         window_system().setPrintDisplay(f_print_shell);
489         
490         // XtAddCallback(p->f_print_shell, XmNpageSetupCallback, PageSetupCB, (XtPointer)p);
491         XtAddCallback(p->f_print_shell, XmNpdmNotificationCallback, PdmNotifyCB, (XtPointer)p);
492         
493         // set default print medium if specified on command line
494         
495         if (window_system().videoShell()->paper_size != NULL) {
496             sprintf(buf, "*default-medium: %s\n", window_system().videoShell()->paper_size);
497             XpSetAttributes(
498                 p->f_print_data->print_display, 
499                 p->f_print_data->print_context, 
500                 XPDocAttr, 
501                 buf, 
502                 XPAttrMerge);
503         }
504
505     }
506
507     RCS_DEBUG("CreatePrintShell exiting.\n");   
508
509 }
510
511 /*
512  * ------------------------------------------------------------------------
513  * Name: PrintSetupCB
514  *
515  * Description:
516  *
517  *     Called when the user presses the setup box "Setup..." button.
518  *
519  */
520 void 
521 PrintSetupCB(Widget print_dialog, XtPointer client_data, XtPointer call_data)
522 {
523     char *msg;
524
525     RCS_DEBUG("PrintSetupCB called.\n");   
526
527
528     AppPrintData *p = (AppPrintData*)client_data;
529     DtPrintSetupCallbackStruct *pbs = (DtPrintSetupCallbackStruct*)call_data;
530
531     // copy the setup data into our space 
532
533     if (p->f_print_data->print_display != NULL)
534         DtPrintFreeSetupData(p->f_print_data);
535     DtPrintCopySetupData(p->f_print_data, pbs->print_data);
536
537     // create a print shell if not already done 
538
539     CreatePrintShell(print_dialog, p);
540
541     // pop up the PDM 
542     if (XmPrintPopupPDM(p->f_print_shell, print_dialog) 
543         != XmPDM_NOTIFY_SUCCESS) {
544         /* post a message error dialog */
545         msg = CATGETS(Set_PrintPanelAgent, 6, "Unable to display the Print Dialog Manager");
546         message_mgr().error_dialog(msg);
547         RCS_DEBUG("XmPrintPopupPDM failed\n");
548     }   
549
550     // Free the setup data - use fresh data when Print button pressed.
551     DtPrintFreeSetupData(p->f_print_data);
552
553     RCS_DEBUG("PrintSetupCB exiting.\n");   
554 }
555
556 /*
557  * ------------------------------------------------------------------------
558  * Name: CancelCB
559  *
560  * Description:
561  *
562  *     Called when the user cancels out of the Print Setup dialog.
563  *     If gui-less printing, causes the program to exit.
564  *
565  */
566 static void 
567 CancelCB(Widget print_dialog, XtPointer client_data, XtPointer call_data)
568 {
569         
570     // if gui-less command line printing, exit now that job is canceled
571     
572     if (window_system().videoShell()->print_only) {
573         _exit(1);
574     }
575
576 }
577
578 /*
579  * ------------------------------------------------------------------------
580  * Name: CreatePrintSetup
581  *
582  * Description:
583  *
584  *     Creates a DtPrintSetupBox dialog.
585  *
586  */
587 void
588 CreatePrintSetup(Widget parent, AppPrintData* p)
589 {
590     RCS_DEBUG("CreatePrintSetup called.\n");   
591
592     //  Only create one PrintSetupBox 
593
594     if (p->f_print_dialog == NULL)
595     {
596         Cardinal n = 0;
597         Arg args[10];
598
599         // resources common to silent and noisy printing cases
600
601         XtSetArg(args[n], DtNcopies, window_system().videoShell()->copies); n++;
602         
603         // set print to file if file name given 
604         if ((char *)window_system().videoShell()->file_name != NULL) {
605             XtSetArg(args[n], DtNfileName, (char *)window_system().videoShell()->file_name); n++;
606             XtSetArg(args[n], DtNprintDestination, DtPRINT_TO_FILE); n++;
607         }
608
609         // printer name
610         if ((char *)window_system().videoShell()->printer != NULL) {
611             XtSetArg(args[n], DtNprinterName, (char *)window_system().videoShell()->printer); n++;
612         }
613
614         // if silent printing we won't need a dialog
615
616         if (window_system().videoShell()->silent) {
617
618             // if parent widget is an application shell, use it, otherwise use
619             // the grandparent
620
621             if (XtIsApplicationShell(parent)) {
622                 p->f_print_dialog =
623                     DtCreatePrintSetupBox(parent, "PrintSetup", args, n);
624             }
625             else {      
626                 p->f_print_dialog =
627                     DtCreatePrintSetupBox(XtParent(parent), "PrintSetup", args, n);
628             }
629         }    
630
631         //  not silent printing so we will need a dialog
632
633         else
634         {
635             // additional dialog resources
636
637             XmString title = XmStringCreateLocalized(
638                 CATGETS(Set_PrintPanelAgent, 1, "Dtinfo: Print"));
639
640             XtSetArg(args[n], XmNdialogTitle, title); n++;
641             XtSetArg(args[n], DtNworkAreaLocation, DtWORK_AREA_BOTTOM); n++;
642  
643             // if parent widget is an application shell, use it, otherwise use
644             // the grandparent
645
646             if (XtIsApplicationShell(parent)) {
647                 p->f_print_dialog =
648                     DtCreatePrintSetupDialog(parent, "PrintSetup", args, n); 
649             }
650             else {
651                 p->f_print_dialog =
652                     DtCreatePrintSetupDialog(XtParent(parent), "PrintSetup", args, n); 
653             }
654 //          XmStringFree(title);
655         }
656
657         // customize the print setup box
658
659         customizePrintSetupBox(p);
660         
661         // add  callbacks
662
663         XtAddCallback(p->f_print_dialog, DtNclosePrintDisplayCallback, PrintCloseDisplayCB, 
664                       (XtPointer)p);
665
666         XtAddCallback(p->f_print_dialog, DtNsetupCallback,
667                       PrintSetupCB, 
668                       (XtPointer)p);
669
670         XtAddCallback(p->f_print_dialog, DtNprintCallback, PrintCB, (XtPointer)p);
671         XtAddCallback(p->f_print_dialog, DtNcancelCallback, CancelCB, (XtPointer)p);
672
673         // add context help for dialog
674
675         help_agent().add_activate_help(p->f_print_dialog, "print_panel_help");
676     }
677
678     //  update message area
679     reset_ui(p);
680
681     RCS_DEBUG("CreatePrintSetup exiting.\n");   
682
683 }
684
685 /*
686  * ------------------------------------------------------------------------
687  * Name: PrintCB
688  *
689  * Description:
690  *
691  *     Called when the user presses the setup box "Print" button.
692  *
693  */
694 void 
695 PrintCB(Widget print_dialog, XtPointer client_data, XtPointer call_data)
696 {
697     RCS_DEBUG("PrintCB called.\n");   
698
699     AppPrintData *p = (AppPrintData*)client_data;
700     DtPrintSetupCallbackStruct *pbs = (DtPrintSetupCallbackStruct*)call_data;
701
702     /*
703      * get the new printer data from the DtPrintSetupBox, and copy it
704      * into our AppPrint data
705      */
706     if (p->f_print_data->print_display != NULL)
707          DtPrintFreeSetupData(p->f_print_data);
708     DtPrintCopySetupData(p->f_print_data, pbs->print_data);
709    
710     DoPrint(print_dialog, p);
711
712     RCS_DEBUG("PrintCB exiting.\n");   
713 }
714
715 /*
716  * ------------------------------------------------------------------------
717  * Name: QuickPrintCB
718  *
719  * Description:
720  *
721  *     Called when the user hits "Print" quick button.
722  *     not sure of this functions role in the "new order" ???? rCs
723  */
724 void 
725 QuickPrintCB(Widget pr_button, XtPointer client_data, XtPointer call_data)
726 {
727     char *msg;
728
729     RCS_DEBUG("QuickPrintCB called.\n");   
730
731     AppPrintData *p = (AppPrintData*)client_data;
732
733     CreatePrintSetup(pr_button, p);
734
735
736     // check if the DtPrintSetupBox ("Print...") has been called yet 
737
738     if(p->f_print_data->print_display == (Display*)NULL)
739     {
740         
741         // first time thru print setup, so get default data 
742         
743         if (DtPrintFillSetupData(p->f_print_dialog, p->f_print_data)
744             != DtPRINT_SUCCESS) {
745             // NOTE: DtPrintFillSetupData() already posts an error
746             // dialog on failure - no need to post our own.
747             RCS_DEBUG("DtPrintFillSetupData failed\n");
748             return ;
749         }
750     }
751  
752     DoPrint(pr_button, p) ;
753
754     RCS_DEBUG("QuickPrintCB exiting.\n");   
755
756 }
757
758 /*
759  * ------------------------------------------------------------------------
760  * Name: FinishPrintToFile
761  *
762  * Description:
763  *
764  *     App-specific print data holder allocate function.
765  *
766  */
767 void 
768 FinishPrintToFile(Display *display,
769                   XPContext context,
770                   XPGetDocStatus status,
771                   XPointer client_data)
772 {
773     char *msg;
774
775     RCS_DEBUG("FinishPrintToFile called.\n");   
776
777
778     if (status != XPGetDocFinished) {
779
780         msg = CATGETS(Set_PrintPanelAgent, 8, "Error printing to file");
781         message_mgr().error_dialog(msg);
782
783         RCS_DEBUG("Something went wrong with XmPrintToFile...\n");
784     }
785     else {
786
787         msg = CATGETS(Set_PrintPanelAgent, 9, "Print to file completed.");
788         message_mgr().info_dialog(msg);
789
790         RCS_DEBUG("XmPrintToFile completed OK\n");
791     }
792     RCS_DEBUG("FinishPrintToFile exiting.\n");   
793
794     return;
795
796 }
797
798 /*
799  * ------------------------------------------------------------------------
800  * Name: DoPrint
801  *
802  * Description:
803  *     
804  *     Routine used from the "Print" button and from the OK button of the
805  *     "Print..." dialog.  
806  *
807  */
808 void 
809 DoPrint(Widget widget, AppPrintData * p) 
810 {
811     int save_data = XPSpool;
812     char *msg;
813
814     xList<UAS_Pointer<UAS_Common> > &print_list = *(p->f_print_list);
815     Xassert (print_list.length() > 0);    
816
817     RCS_DEBUG("DoPrint called.\n");   
818
819     // create print shell, if not done yet 
820     CreatePrintShell(widget, p);
821
822     if (p->f_print_data->destination == DtPRINT_TO_FILE)
823     {
824         save_data = XPGetData;
825     }
826
827     // start job must precede XpGetDocumentData in XmPrintToFile 
828
829     XpStartJob(XtDisplay(p->f_print_shell), save_data);  
830
831     // maintain the sequence
832     // between startjob and getdocument 
833
834     XFlush(XtDisplay(p->f_print_shell));  
835
836     // setup print to file 
837
838     if (p->f_print_data->destination == DtPRINT_TO_FILE)
839     {
840         if (!XmPrintToFile(XtDisplay(p->f_print_shell), 
841                            p->f_print_data->dest_info, 
842                            (XPFinishProc)FinishPrintToFile, 
843                            NULL))
844         {
845             // Add real error message here.
846             // printf("XmPrintToFile: Unable to print to file %s\n",
847             //        p->print_data->dest_info);
848             
849             XpCancelJob(XtDisplay(p->f_print_shell), False);
850             
851             // we can go back to the event loop as if we had never printed
852             return;
853         }
854     }
855     
856     if (!(window_system().videoShell()->silent)) {
857
858         XtRealizeWidget(p->f_print_shell);
859         XtPopup(p->f_print_shell, XtGrabNone);
860     }
861
862     PrintEverything(p);
863
864     XpEndJob(XtDisplay(p->f_print_shell));
865
866     // synch the print display (i am hoping this will get
867     // the xlib error handler called, if need be
868
869     XSync(XtDisplay(p->f_print_shell), False);
870
871     // if a print server error occured then notify the user
872
873     if (print_server_error) {
874
875         print_server_error = False; // reset for next time
876
877         // Post an error dialog 
878
879         msg = CATGETS(Set_PrintPanelAgent, 
880                       11, 
881                       "Print Job Failed.\n\nThe X Print Server is Temporarily Out of Resources.");
882         message_mgr().error_dialog(msg);
883         
884     }
885
886     if (!(window_system().videoShell()->silent)) {
887         XtPopdown(p->f_print_shell);
888     }
889
890     // if we are in print only mode flush the output
891     // buffer to force the print and then exit
892
893     if (window_system().videoShell()->print_only) {
894
895         XSync(XtDisplay(p->f_print_shell), False);
896
897         // call this function to avoid invoking destructors
898         _exit(0);
899     }
900
901     //  have the print manager destroy the print agent
902     //  used to print this job
903
904     print_mgr().destroy_agent();
905
906     // destroy print shell as well
907     
908     if (p->f_print_shell)
909     {
910         XtDestroyWidget(p->f_print_shell);
911         p->f_print_shell = (Widget)NULL ;
912         f_print_shell = (Widget)NULL ;
913
914     }
915
916     RCS_DEBUG("DoPrint exiting.\n");   
917
918 }
919
920 /*
921  * ------------------------------------------------------------------------
922  * Name: PrintEverything
923  *
924  * Description:
925  *
926  *     Called when the print shell receives the XP events. Contains the 
927  *     logic for printing all topics.
928  *
929  */
930 static void 
931 PrintEverything(AppPrintData *p)
932 {
933     int cur_page = 0;
934     
935     RCS_DEBUG("PrintEverything called.\n");
936
937     xList<UAS_Pointer<UAS_Common> > &print_list = *(p->f_print_list);
938     Xassert (print_list.length() > 0);
939
940     l_AppPrintData = p;
941     Widget pshell = p->f_print_shell;
942
943     List_Iterator<UAS_Pointer<UAS_Common> > l (print_list);
944
945     while (l)
946     {
947         PrintOneUASCommon(l.item(), pshell, &cur_page);
948         l++;
949     }
950
951     RCS_DEBUG("PrintEverything exiting.\n");
952 }
953
954 static void
955 PrintOneUASCommon(UAS_Pointer<UAS_Common> &doc, Widget pshell, int *cur_pageP)
956 {
957     Arg args[15];
958     int n;
959     XmString label;
960     char buf[BUFSIZ];
961
962     // Retrieve the topic from the doc
963     // The retrieve function will send a UAS_DocumentRetrievedMsg to the
964     // print manager which will render the canvas with the appropriate topic
965
966     doc->retrieve((void *)1);
967
968     // set print orientation to either landscape or portrait (if set)
969
970     if (f_printOrientation != NULL) {
971         sprintf(buf, "*content-orientation: %s\n", f_printOrientation);
972         XpSetAttributes(XtDisplay(pshell), XpGetContext(XtDisplay(pshell)), XPPageAttr, buf, XPAttrMerge);
973     }
974     
975     for (gHelpDisplayArea->firstVisible = gHelpDisplayArea->nextNonVisible = 0;
976          gHelpDisplayArea->nextNonVisible >= 0;
977          gHelpDisplayArea->firstVisible = gHelpDisplayArea->nextNonVisible + 1)
978     {
979         (*cur_pageP)++;
980
981         XpStartPage(XtDisplay(pshell), XtWindow(pshell));
982
983         sprintf(buf, "%d", *cur_pageP);
984         label = XmStringCreateLocalized(buf);
985
986         n = 0;
987         XtSetArg(args[n], XmNlabelString, label); n++;
988         XtSetValues(footer_page_no, args, n);
989         XmStringFree(label);
990
991         // Process exposures so label is updated.
992         XmUpdateDisplay(footer_page_no);
993
994         // _DtHelpCleanAndDrawWholeCanvas((XtPointer)gHelpDisplayArea);
995
996         XpEndPage(XtDisplay(pshell));
997     }
998
999     // Print chidren if appropriate
1000     if (print_hierarchy)
1001     {
1002         int i;
1003         UAS_List<UAS_Common> kids = doc->children();
1004
1005         for (i = 0; i < kids.length(); i++)
1006         {
1007             PrintOneUASCommon(kids[i], pshell, cur_pageP);
1008         }
1009     }
1010 }
1011
1012 /*
1013  * ------------------------------------------------------------------------
1014  * Name: PrintCloseDisplayCB
1015  *
1016  * Description:
1017  *
1018  *     Called when the print setup box is about to close the print
1019  *     display (in response to a new printer on a different display, or
1020  *     when the setup box is destroyed).
1021  */
1022 void
1023 PrintCloseDisplayCB(
1024     Widget widget,
1025     XtPointer client_data,
1026     XtPointer call_data)
1027 {
1028     RCS_DEBUG("PrintCloseDisplayCB called.\n");   
1029
1030     DtPrintSetupCallbackStruct *pbs = (DtPrintSetupCallbackStruct*)call_data;
1031     AppPrintData *p = (AppPrintData*)client_data;
1032
1033     if (p->f_print_shell)
1034     {
1035         XtDestroyWidget(p->f_print_shell);
1036         p->f_print_shell = (Widget)NULL ;
1037         f_print_shell = (Widget)NULL ;
1038
1039     }
1040
1041     DtPrintFreeSetupData(p->f_print_data);
1042
1043     // reset gHelpDisplayArea so that it will be recreated.
1044     // ??? should i also destroy the canvas here?
1045     gHelpDisplayArea = 0;
1046     
1047     RCS_DEBUG("PrintCloseDisplayCB exiting.\n");   
1048
1049 }
1050
1051
1052 // /////////////////////////////////////////////////////////////////
1053 // ToggleWhatCB
1054 // /////////////////////////////////////////////////////////////////
1055
1056 void
1057 ToggleWhatCB(
1058     Widget widget,
1059     XtPointer client_data,
1060     XtPointer call_data)
1061 {
1062     RCS_DEBUG("ToggleWhatCB called.\n");   
1063
1064     XmToggleButtonCallbackStruct * cbs = (XmToggleButtonCallbackStruct *) call_data;
1065     AppPrintData * p = (AppPrintData *)client_data;
1066     
1067     xList<UAS_Pointer<UAS_Common> > &print_list = *(p->f_print_list);
1068     
1069     static char buffer[24];
1070     WXmToggleButton other (NULL), active(widget);
1071     
1072     ON_DEBUG (printf ("PrintPanel::ToggleWhatCB (%p)\n", widget));
1073     
1074     Xassert ( (widget == (Widget)f_print_nodes) || (widget == (Widget)f_print_hierarchy));
1075     
1076     // Figure out which one is the other one (ie: not clicked on). 
1077     if (active == f_print_nodes) 
1078     {
1079         other = f_print_hierarchy;
1080     }
1081     else 
1082     {
1083         other = f_print_nodes;
1084     }
1085     
1086     active.Set (True);
1087     other.Set (False);
1088     
1089     if (active == f_print_nodes)
1090     {
1091         sprintf (buffer, "%d", print_list.length());
1092         print_hierarchy = False;
1093     }
1094     else // active == f_print_hierarchy 
1095     {
1096         List_Iterator<UAS_Pointer<UAS_Common> > l (print_list);
1097         int subtree_size = l.item()->subtree_size ();
1098
1099         if (subtree_size != 0)
1100         {
1101             sprintf (buffer, "%d", subtree_size);
1102         }
1103         else
1104         {
1105             strcpy (buffer, "?");
1106         }
1107         print_hierarchy = True;
1108     }
1109     
1110     if (*buffer == '0') 
1111     {
1112         *buffer = '?';
1113     }
1114     f_to_print_field.LabelString (WXmString (buffer));
1115
1116     RCS_DEBUG("ToggleWhatCBB exiting.\n");   
1117
1118 }