Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtcm / dtcm / cmtt.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 **
25 **  cmtt.c
26 **
27 **  $TOG: cmtt.c /main/4 1999/09/20 10:29:41 mgreess $
28 **
29 **  RESTRICTED CONFIDENTIAL INFORMATION:
30 **
31 **  The information in this document is subject to special
32 **  restrictions in a confidential disclosure agreement between
33 **  HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
34 **  document outside HP, IBM, Sun, USL, SCO, or Univel without
35 **  Sun's specific written approval.  This document and all copies
36 **  and derivative works thereof must be returned or destroyed at
37 **  Sun's request.
38 **
39 **  Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
40 **
41 *******************************************************************************/
42
43 /*                                                                      *
44  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
45  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
46  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
47  * (c) Copyright 1993, 1994 Novell, Inc.                                *
48  */
49
50 #ifndef lint
51 static  char sccsid[] = "@(#)cmtt.c 1.13 95/05/01 Copyr 1993 Sun Microsystems, Inc.";
52 #endif
53 #include <EUSCompat.h>
54 #include <sys/stat.h>
55 #include <unistd.h>
56 #include <stdio.h>
57 #include <string.h>
58 #include <stdlib.h>
59 #include <Xm/XmAll.h>
60 #include <Tt/tttk.h>
61 #include <Dt/UserMsg.h>
62 #include "calendar.h"
63 #include "props.h"
64 #include "misc.h"
65 #include "cmfns.h"
66 #include "debug.h"
67
68
69 static Tt_message load_cb();
70
71 #ifdef FNS_DEMO
72 static const char *ptype = "Sun_DtCm";
73
74 static void
75 cm_iconic(
76         Widget  win,
77         int     iconic)
78
79 {
80         if (iconic) {
81                 XIconifyWindow(XtDisplay(win),
82                                 XtWindow(win),
83                                 XScreenNumberOfScreen(XtScreen(win)));
84         } else {
85                 XtPopup(win, XtGrabNone);
86                 XMapWindow(XtDisplay(win), XtWindow(win));
87         }
88
89         return;
90 }
91 #endif
92
93 static void
94 OKCB (Widget dialog, XtPointer client_data, XtPointer call_data)
95 {
96     XtUnmanageChild((Widget) client_data);
97 }
98
99 void
100 DieFromToolTalkError(Calendar *c, char *errfmt, Tt_status status)
101 {
102     Arg          args[10];
103     Widget       dialog, dialogShell;
104     char        *errmsg, *statmsg, *title;
105     XmString     xms_errmsg, xms_ok, xms_title;
106     int          n;
107
108     if (! tt_is_err(status)) return;
109
110     statmsg = tt_status_message(status);
111     errmsg = XtMalloc(strlen(errfmt) + strlen(statmsg) + 2);
112     sprintf(errmsg, errfmt, statmsg);
113
114     xms_ok = XmStringCreateLocalized(catgets(c->DT_catd, 2, 3, "OK"));
115     xms_errmsg = XmStringCreateLocalized(errmsg);
116     xms_title = XmStringCreateLocalized(catgets(c->DT_catd, 2, 1,
117                         "Calendar - Warning"));
118
119     n = 0;
120     XtSetArg(args[n], XmNautoUnmanage, False); n++;
121     XtSetArg(args[n], XmNokLabelString, xms_ok); n++;
122     XtSetArg(args[n], XmNdialogTitle, xms_title); n++;
123     XtSetArg(args[n], XmNmessageString, xms_errmsg); n++;
124     XtSetArg(args[n], XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL); n++;
125
126     dialog = XmCreateWarningDialog(c->frame, "IconEditorError", args, n);
127     XtAddCallback(dialog, XmNokCallback, OKCB, (XtPointer) dialog);
128     XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_CANCEL_BUTTON));
129     XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON));
130
131     /*
132      * Disable the frame menu from dialog since we don't want the user
133      * to be able to close dialogs with the frame menu
134      */
135     dialogShell = XtParent(dialog);
136     n = 0;
137     XtSetArg(args[n], XmNmwmDecorations, MWM_DECOR_ALL | MWM_DECOR_MENU); n++;
138     XtSetValues(dialogShell, args, n);
139     XtManageChild(dialog);
140     XtRealizeWidget(dialogShell);
141
142     _DtSimpleError("Dtcm", DtWarning, NULL, errmsg);
143
144     XtFree(errmsg);
145     XmStringFree(xms_ok);
146     XmStringFree(xms_errmsg);
147     XmStringFree(xms_title);
148 }
149
150
151 /* 
152  * Initialize tooltalk.  Can be called multiple times: the first call
153  * initializes tooltalk, subsequent calls are no-ops.
154  *
155  * Returns
156  *              -1      Error.  Tooltalk not initialized
157  *              0       Tooltalk already initialized
158  *              1       Tooltalk succussfully intialized
159  */
160 Tt_status
161 cmtt_init(
162         char            *toolname,
163         Calendar        *c,
164         XtAppContext    context,
165         Widget           shell)
166
167 {
168         int             ttfd;
169         Tt_status       status;
170         char            *ttenv;
171         char            *session;
172
173         if (c->tt_procid) {
174                 return 0;
175         }
176
177         ttenv = (char *)getenv("TT_SESSION");
178         if (!ttenv || strlen(ttenv) == 0) {
179                 session = tt_X_session(XDisplayString(c->xcontext->display));
180                 tt_default_session_set(session);
181                 tt_free(session);
182         }
183
184         c->tt_procid = ttdt_open(&ttfd, toolname, "SunSoft", "%I", 1);
185         status = tt_ptr_error(c->tt_procid);
186         if (tt_is_err(status)) {
187                 c->tt_procid = NULL;
188                 return status;
189         }
190
191 #ifdef FNS_DEMO
192         /*
193          * Declare our ptype, and register the callback to handle
194          * Edit/Display/Compose requests
195          */
196         status = ttmedia_ptype_declare(ptype, 0, load_cb, (void *)c, 1);
197         if (tt_is_err(status)) {
198                 fprintf(stderr, "cmtt_init could not declare ptype: %s\n",
199                         tt_status_message(status));
200                 return status;
201         }
202 #endif
203
204         ttdt_session_join(0, NULL, shell, c, 1);
205
206         XtAppAddInput(context, ttfd, (XtPointer)XtInputReadMask,
207                         tttk_Xt_input_handler, c->tt_procid);
208
209         /* Process the message that started us, if any */
210         tttk_Xt_input_handler( 0, 0, 0 );
211
212         return TT_OK;
213 }
214
215 #ifdef FNS_DEMO
216 static Tt_message
217 contract_cb(
218         Tt_message      msg,
219         Tttk_op         op,
220         Widget          shell,
221         void            *clientdata,
222         Tt_message      contract)
223 {
224
225         DP(("contract_cb(msg=%d, op=%d, shell=%d, data=%d, cntrct=%d)\n",
226                 msg, op, shell, clientdata, contract));
227
228         switch (op) {
229         case TTDT_QUIT:
230         case TTDT_GET_STATUS:
231         case TTDT_PAUSE:
232         case TTDT_RESUME:
233                 break;
234         }
235         /* For now do nothing */
236         return msg;
237 }
238
239 /*
240  * Handle Edit, Display and Compose requests
241  */
242 static Tt_message
243 load_cb(
244         Tt_message      msg,
245         void            *clientdata,
246         Tttk_op         op,
247         Tt_status       diagnosis,
248         unsigned char   *contents,
249         int             len,
250         char            *file,
251         char            *docname
252 )
253
254 {
255         Tt_status status;
256         char    *p;
257         char    addr_buf[256];
258         Calendar        *c;
259         Tt_pattern      *contract_pats;
260
261         DP(("load_cb(msg=%d, op=%d, diag=%d, contents=%d, len=%d, file=%s, \
262 docname=%s, data=%d)\n",
263                 msg, op, diagnosis, contents, len, file ? file : "<nil>",
264                 docname ? docname : "<nil>", clientdata));
265
266         c = (Calendar *)clientdata;
267
268         if (diagnosis != TT_OK) {
269                 if (tt_message_status(msg) == TT_WRN_START_MESSAGE) {
270                         /*
271                          * Error in start message!  we may want to exit
272                          * here, but for now let toolkit handle error
273                          */
274                          return msg;
275                 }
276
277                 /* Let toolkit handle the error */
278                 return msg;
279         }
280
281         contract_pats = ttdt_message_accept(msg, contract_cb, clientdata,
282                                                 c->frame, 1, 1);
283
284         if ((status = tt_ptr_error(contract_pats)) != TT_OK) {
285                 fprintf(stderr, "dtcm: load_cb could not accept message: %s\n",
286                         tt_status_message(status));
287         } else {
288         /*
289                 tttk_patterns_destroy(contract_pats);
290         */
291         }
292
293         if (file != NULL) {
294                 p = file;
295         } else if (len > 0) {
296                 p = (char *)contents;
297         } else {
298                 tttk_message_fail( msg, TT_DESKTOP_ENODATA, 0, 1 );
299                 p = NULL;
300         }
301
302         if (p) {
303                 if (cmfns_lookup_calendar(p, addr_buf, sizeof(addr_buf)) == 1) {
304                         cm_iconic(c->frame, 0);
305                         switch_it(c, addr_buf, main_win);
306                 }
307         }
308
309         tt_message_reply(msg);
310
311         tt_free((caddr_t)contents);
312         tt_free(file);
313         tt_free(docname);
314
315         return 0;
316 }
317 #endif