Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtinfo / dtinfo / src / Managers / MessageMgr.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: MessageMgr.C /main/13 1998/07/27 14:20:49 mgreess $
25  *
26  * Copyright (c) 1991 HaL Computer Systems, Inc.  All rights reserved.
27  * UNPUBLISHED -- rights reserved under the Copyright Laws of the United
28  * States.  Use of a copyright notice is precautionary only and does not
29  * imply publication or disclosure.
30  * 
31  * This software contains confidential information and trade secrets of HaL
32  * Computer Systems, Inc.  Use, disclosure, or reproduction is prohibited
33  * without the prior express written permission of HaL Computer Systems, Inc.
34  * 
35  *                         RESTRICTED RIGHTS LEGEND
36  * Use, duplication, or disclosure by the Government is subject to
37  * restrictions as set forth in subparagraph (c)(l)(ii) of the Rights in
38  * Technical Data and Computer Software clause at DFARS 252.227-7013.
39  *                        HaL Computer Systems, Inc.
40  *                  1315 Dell Avenue, Campbell, CA  95008
41  * 
42  */
43
44 # include "UAS.hh"
45
46 #define C_MessageAgent
47 #define C_VersionAgent
48 #define C_Agent
49 #define L_Agents
50
51 #define C_MessageMgr
52 #define C_Manager
53 #define C_MarkMgr
54 #define L_Managers
55
56 #include "Prelude.h"
57
58 #include "Managers/CatMgr.hh"
59 #include "Registration.hh"
60 #include "Other/XmStringLocalized.hh"
61
62 #include <stdio.h>
63 #include <iostream.h>
64
65 LONG_LIVED_CC (MessageMgr,message_mgr);
66
67 #define CLASS MessageMgr
68
69 STATIC_SENDER_CC(UAS_CancelRetrievalMsg);
70
71 //static int progressDisplayed = 0;
72
73 class DisplayDocumentError : public UAS_Receiver<UAS_ErrorMsg>
74 {
75 public:
76   DisplayDocumentError()
77     { UAS_Common::request (this); }
78 private:
79   void receive (UAS_ErrorMsg &message, void *client_data);
80 };
81
82 void
83 DisplayDocumentError::receive (UAS_ErrorMsg &message, void *client_data)
84 {
85   const char *text = message.fErrorMsg;
86
87   if (text == NULL)
88     text = (char*)UAS_String(CATGETS(Set_Messages, 35, "File a Bug"));
89
90   message_mgr().error_dialog ((char *) text);
91 }
92
93 static DisplayDocumentError error_displayer;
94
95
96 class ReportSearchMsg : public UAS_Receiver<UAS_SearchMsg>
97 {
98 public:
99   ReportSearchMsg() { UAS_SearchEngine::request (this); }
100 private:
101   void receive (UAS_SearchMsg &message, void *client_data);
102 };
103
104 void
105 ReportSearchMsg::receive (UAS_SearchMsg &message, void *client_data)
106 {
107   if (message.fSearchMsg) {
108     bool cont = message_mgr().question_dialog(message.fSearchMsg);
109     message.fContFlag = cont? 1 : 0;
110   }
111   else {
112     message_mgr().error_dialog(
113         (char*)UAS_String(CATGETS(Set_Messages, 36, "File a Bug")));
114   }
115 }
116
117 static ReportSearchMsg g_search_msg_reporter;
118
119 class DisplayDocumentProgress: public UAS_Receiver<UAS_StatusMsg>,
120                                public UAS_Receiver<UAS_DocumentRetrievedMsg> {
121     public:
122         DisplayDocumentProgress () {
123             UAS_Common::request ((UAS_Receiver<UAS_StatusMsg> *) this);
124             UAS_Common::request ((UAS_Receiver<UAS_DocumentRetrievedMsg> *) this);
125         }
126
127     public:
128         void receive (UAS_StatusMsg &msg, void *client_data);
129         void receive (UAS_DocumentRetrievedMsg &msg, void *client_data);
130
131 };
132
133 void
134 DisplayDocumentProgress::receive (UAS_StatusMsg &msg, void *client_data) {
135     message_mgr().progress_dialog (msg.fText);
136 }
137
138 void
139 DisplayDocumentProgress::receive (UAS_DocumentRetrievedMsg &, void *client_data) {
140     message_mgr().progress_dialog_done ();
141 }
142
143
144 static DisplayDocumentProgress progress_displayer;
145
146
147 // /////////////////////////////////////////////////////////////////
148 // class constructor
149 // /////////////////////////////////////////////////////////////////
150
151 MessageMgr::MessageMgr() :
152  f_booklist_message (*(new MessageAgent())),
153  f_nodeview_message (*(new MessageAgent())),
154  f_booklist_process(0),
155  f_nodeview_process(0),
156  f_popped_up(False),
157  f_pressed_ok(False),
158  f_exit_flag(False),
159  f_dialog_active(False)
160
161 {
162     MessageAgent::request ((UAS_Receiver<CancelOperation> *) this);
163 }
164
165
166 // /////////////////////////////////////////////////////////////////
167 // class destructor
168 // /////////////////////////////////////////////////////////////////
169
170 MessageMgr::~MessageMgr()
171 {
172   g_message_mgr = NULL;
173 }
174
175 void
176 okCB(Widget, XtPointer client_data, XtPointer)
177 {
178   MessageMgr* mgr = (MessageMgr*)client_data;
179   mgr->ok_cb();
180 }
181
182 void
183 cancelCB(Widget, XtPointer client_data, XtPointer)
184 {
185   MessageMgr* mgr = (MessageMgr*)client_data;
186   mgr->cancel_cb();
187 }
188
189 void
190 MessageMgr::ok_cb()
191 {
192   if (f_exit_flag) {
193     mark_mgr().finalize ();
194     UAS_Common::finalize ();
195     ::exit (0);
196   }
197
198   f_pressed_ok = True;
199   f_popped_up = False;
200 }
201
202 void
203 MessageMgr::cancel_cb()
204 {
205   f_pressed_ok = False;
206   f_popped_up = False;
207 }
208
209 void
210 MessageMgr::show_it(Widget dialog)
211 {
212   Widget parent, shell;
213
214   XtManageChild(dialog);
215
216   if (dialog) parent = XtParent(dialog);
217   if (parent) shell = XtParent(parent);
218   if (NULL == shell) shell = parent;
219   XtPopup(shell, XtGrabNone);
220   XMapRaised(XtDisplay(shell), XtWindow(shell));
221
222   f_popped_up = True;
223   f_pressed_ok = False;
224  
225   XEvent event;
226   while(f_popped_up)
227   {
228     XtAppNextEvent (window_system().app_context(), &event);
229     XtDispatchEvent (&event);
230   }
231 }
232
233 void
234 MessageMgr::destroy_it(Widget dialog)
235 {
236   Widget shell = XtParent(dialog);
237   XtPopdown(shell);
238   XtUnmanageChild(dialog);
239   XtDestroyWidget(dialog);
240 }
241
242 Widget
243 MessageMgr::create_dialog (unsigned char dialog_type,
244                            char *message_text,
245                            Widget parent)
246 {
247   Widget dialog;
248
249   // determine the parent of the dialog shell
250   if(parent == NULL)
251   {
252     Window window;
253     int revert;
254     XGetInputFocus(window_system().display(), &window, &revert);
255     parent = XtWindowToWidget(window_system().display(), window);
256     if(parent == NULL)
257       parent = window_system().toplevel();
258   }
259
260   dialog = XmCreateMessageDialog(parent, "dialog", NULL, 0);
261
262   WXmString wxms = message_text;
263   XtVaSetValues(dialog,
264                 XmNdialogType, dialog_type,
265                 XmNmessageAlignment, XmALIGNMENT_CENTER,
266                 XmNmessageString, (XmString)wxms,
267                 NULL);
268
269   // add callbacks
270   XtAddCallback(dialog, XmNokCallback, okCB, this);
271   XtAddCallback(dialog, XmNcancelCallback, cancelCB, this);
272
273   return dialog;
274 }
275
276 // /////////////////////////////////////////////////////////////////
277 // quit dialog
278 // /////////////////////////////////////////////////////////////////
279
280 void
281 MessageMgr::quit_dialog (char *message_text, Widget parent)
282 {
283   //if(parent == NULL)
284     //parent = (Widget)window_system().toplevel();
285   //Widget dialog = XmCreateQuestionDialog(parent, "dialog", NULL, 0);
286   Widget dialog = create_dialog(XmDIALOG_QUESTION, message_text, parent);
287
288   XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON));
289   XtVaSetValues(dialog,
290                 XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL,
291                 XmNmessageAlignment, XmALIGNMENT_CENTER,
292                 NULL);
293
294   // Set labels on buttons
295   XmStringLocalized mtfstring;
296   mtfstring = CATGETS(Set_AgentLabel, 244, "Yes");
297   XtVaSetValues(XmMessageBoxGetChild(dialog, XmDIALOG_OK_BUTTON),
298                 XmNlabelString, (XmString)mtfstring,
299                 NULL);
300   mtfstring = CATGETS(Set_AgentLabel, 245, "No");
301   XtVaSetValues(XmMessageBoxGetChild(dialog, XmDIALOG_CANCEL_BUTTON),
302                 XmNlabelString, (XmString)mtfstring,
303                 NULL);
304
305   // Set the window title
306   Widget shell = XtParent(dialog);
307   String string = CATGETS(Set_MessageAgent, 5, "Dtinfo: Confirm Quit");
308   XtVaSetValues(shell,
309                 XmNminWidth, 200,
310                 XmNtitle, (XmString)string,
311                 NULL);
312
313   // Set the flag for the OK button
314   f_exit_flag = True;
315
316   show_it(dialog);
317
318   f_exit_flag = False;
319
320   destroy_it(dialog);
321 #if 0
322   if(parent == NULL)
323     f_booklist_message.displayQuit(message_text, window_system().toplevel());
324   else
325     f_nodeview_message.displayQuit(message_text, parent);
326   //f_message_agent.displayQuit (message_text, parent);
327 #endif
328 }
329
330
331 // /////////////////////////////////////////////////////////////////
332 // error dialog
333 // /////////////////////////////////////////////////////////////////
334 void
335 MessageMgr::error_dialog (char *message_text, Widget parent)
336 {
337   Widget dialog = create_dialog(XmDIALOG_ERROR, message_text, parent);
338   //Widget dialog = XmCreateErrorDialog(parent, "dialog", NULL, 0);
339
340   XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON));
341   XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_CANCEL_BUTTON));
342   XtVaSetValues(dialog,
343                 XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL,
344                 XmNmessageAlignment, XmALIGNMENT_BEGINNING,
345                 NULL);
346
347   // Set the window title
348   Widget shell = XtParent(dialog);
349   String string = CATGETS(Set_MessageAgent, 1, "Dtinfo: Error");
350   XtVaSetValues(shell, XmNtitle, string, NULL);
351
352   show_it(dialog);
353
354   destroy_it(dialog);
355
356 #if 0
357   if(parent == NULL)
358     f_booklist_message.displayError(message_text, window_system().toplevel());
359   else
360     f_nodeview_message.displayError(message_text, parent);
361   //f_message_agent.displayError (message_text, parent);
362 #endif
363 }
364
365 // /////////////////////////////////////////////////////////////////
366 // warning dialog
367 // /////////////////////////////////////////////////////////////////
368
369 void
370 MessageMgr::warning_dialog (char *message_text, Widget parent)
371 {
372   Widget dialog = create_dialog(XmDIALOG_WARNING, message_text, parent);
373   //Widget dialog = XmCreateWarningDialog(parent, "dialog", NULL, 0);
374
375   XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON));
376   XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_CANCEL_BUTTON));
377   XtVaSetValues(dialog,
378                 XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL,
379                 XmNmessageAlignment, XmALIGNMENT_BEGINNING,
380                 NULL);
381
382   // Set the window title
383   Widget shell = XtParent(dialog);
384   String string = CATGETS(Set_MessageAgent, 2, "Dtinfo: Warning");
385   XtVaSetValues(shell, XmNtitle, string, NULL);
386
387   show_it(dialog);
388
389   XtPopdown(shell);
390   XtUnmanageChild(dialog);
391   XtDestroyWidget(dialog);
392
393 #if 0
394   if(parent == NULL)
395     f_booklist_message.displayWarning(message_text, window_system().toplevel());
396   else
397     f_nodeview_message.displayWarning(message_text, parent);
398   //f_message_agent.displayWarning (message_text, parent);
399 #endif
400 }
401
402 // /////////////////////////////////////////////////////////////////
403 // information dialog
404 // /////////////////////////////////////////////////////////////////
405
406 void
407 MessageMgr::info_dialog (char *message_text, Widget parent)
408 {
409   Widget dialog = create_dialog(XmDIALOG_INFORMATION, message_text, parent);
410   XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON));
411   XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_CANCEL_BUTTON));
412
413   // Set the window title
414   Widget shell = XtParent(dialog);
415   String string = CATGETS(Set_MessageAgent, 3, "Dtinfo: Information");
416   XtVaSetValues(shell, XmNtitle, string, NULL);
417
418   show_it(dialog);
419   destroy_it(dialog);
420
421 #if 0
422   if(parent == NULL)
423     f_booklist_message.displayInformation(message_text, window_system().toplevel());
424   else
425     f_nodeview_message.displayInformation(message_text, parent);
426   //f_message_agent.displayInformation (message_text, parent);
427 #endif
428 }
429
430 // /////////////////////////////////////////////////////////////////
431 // progress dialog
432 // /////////////////////////////////////////////////////////////////
433
434 void
435 MessageMgr::progress_dialog (char *message_text, Widget parent) {
436 //    MessageAgent::request ((UAS_Receiver<CancelOperation> *) this);
437   //progressDisplayed = 1;
438   if(parent == NULL)
439   {
440     f_booklist_message.displayProgress (
441                          message_text, window_system().toplevel());
442     f_booklist_process = 1;
443   }
444   else
445   {
446     f_nodeview_message.displayProgress (message_text, parent);
447     f_nodeview_process = 1;
448   }
449 }
450
451 // /////////////////////////////////////////////////////////////////
452 // progress dialog done
453 // /////////////////////////////////////////////////////////////////
454
455 void
456 MessageMgr::progress_dialog_done () {
457 //    MessageAgent::unrequest ((UAS_Receiver<CancelOperation> *) this);
458     if (f_booklist_process)
459         f_booklist_message.undisplayProgress ();
460     if (f_nodeview_process)
461         f_nodeview_message.undisplayProgress ();
462 }
463
464 // /////////////////////////////////////////////////////////////////
465 // question dialog
466 // /////////////////////////////////////////////////////////////////
467
468 bool
469 MessageMgr::question_dialog (char *message_text, Widget parent)
470 {
471   if(parent == NULL)
472     parent = (Widget)window_system().toplevel();
473   Widget dialog = XmCreateInformationDialog(parent, "dialog", NULL, 0);
474   XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON));
475   WXmString wxms = message_text;
476   XtVaSetValues(dialog,
477                 XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL,
478                 XmNmessageAlignment, XmALIGNMENT_CENTER,
479                 XmNmessageString, (XmString)wxms, 
480                 NULL);
481
482   // Set labels on buttons
483   XmStringLocalized mtfstring;
484   mtfstring = CATGETS(Set_AgentLabel, 244, "Yes");
485   XtVaSetValues(XmMessageBoxGetChild(dialog, XmDIALOG_OK_BUTTON),
486                 XmNlabelString, (XmString)mtfstring,
487                 NULL);
488   mtfstring = CATGETS(Set_AgentLabel, 245, "No");
489   XtVaSetValues(XmMessageBoxGetChild(dialog, XmDIALOG_CANCEL_BUTTON),
490                 XmNlabelString, (XmString)mtfstring,
491                 NULL);
492
493   XtAddCallback(dialog, XmNokCallback, okCB, this);
494   XtAddCallback(dialog, XmNcancelCallback, cancelCB, this);
495
496   // Set the window title
497   Widget shell = XtParent(dialog);
498   String string = CATGETS(Set_MessageAgent, 3, "Dtinfo: Information");
499   XtVaSetValues(shell, XmNtitle, string, NULL);
500
501   show_it(dialog);
502
503   XtPopdown(shell);
504   XtUnmanageChild(dialog);
505   XtDestroyWidget(dialog);
506
507   return(f_pressed_ok);
508
509 #if 0
510   if(parent == NULL)
511     return(f_booklist_message.displayQuestion(
512                                 message_text, window_system().toplevel()));
513   else
514     return(f_nodeview_message.displayQuestion(message_text, parent));
515   //return (f_message_agent.displayQuestion (message_text, parent));
516 #endif
517 }
518
519 int
520 MessageMgr::get_integer(const char *msg, const char* title,
521                         int default_value, Widget parent)
522 {
523   if(parent == NULL)
524     return (f_booklist_message.get_integer (msg, title, default_value, window_system().toplevel()));
525   else
526     return (f_nodeview_message.get_integer (msg, title, default_value, parent));
527 }
528
529 const char *
530 MessageMgr::get_string (const char* msg, const char* title,
531                         const char *default_value, Widget parent)
532 {
533   if(parent == NULL)
534     return (f_booklist_message.get_string (msg, title, default_value, window_system().toplevel()));
535   else
536     return (f_nodeview_message.get_string (msg, title, default_value, parent));
537 }
538
539
540 // /////////////////////////////////////////////////////////////////
541 // assert_failed
542 // /////////////////////////////////////////////////////////////////
543
544 void
545 MessageMgr::assert_failed (char *statement, char *file,
546                            int line, Widget parent)
547 {
548   char buffer[512];
549
550   sprintf (buffer,
551 "An internal error has occured.\n\
552 The current operation cannot be completed.\n\
553 Please file a bug report with this information:\n\
554 Assertion failed: %s\n\
555 File \"%s,\" line %d.",
556            statement, file, line);
557               
558   if(parent == NULL)
559     f_booklist_message.displayError (buffer, window_system().toplevel());
560   else
561     f_nodeview_message.displayError (buffer, parent);
562   quit_dialog ("We strongly suggest you quit now, Ok?", parent);
563 }
564
565 // /////////////////////////////////////////////////////////////////////////
566 // demo_failure
567 //
568 // put up error message. quit application when dialog returns
569 // 
570 // /////////////////////////////////////////////////////////////////////////
571
572 void
573 MessageMgr::demo_failure(demoException &, Widget parent)
574 {
575   error_dialog(
576         (char*)UAS_String(CATGETS(Set_Messages, 38, "File a Bug")),
577         parent);
578   ::exit(0);
579 }
580 void
581 MessageMgr::receive (CancelOperation &, void *client_data) {
582 //    MessageAgent::unrequest ((UAS_Receiver<CancelOperation> *) this);
583 //
584 //  SWM -- FOR NOW: Don't pass any document (just 0). We will need
585 //  to change this in the future.
586 //
587     UAS_CancelRetrievalMsg cancelMsg(0);
588     send_message(cancelMsg, client_data);
589 }
590 void
591 MessageMgr::set_max_length(int length, Widget parent)
592 {
593   if(parent == NULL)
594     f_booklist_message.setMaxLength (length, window_system().toplevel());
595   else
596     f_nodeview_message.setMaxLength (length, parent);
597 }