dtcreate: warning prevention.
[oweals/cde.git] / cde / programs / dtcreate / main.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 /* $TOG: main.c /main/11 1999/09/17 17:22:06 mgreess $ */
24 /*****************************************************************************/
25 /*                                                                           */
26 /*      main.c                                                               */
27 /*                                                                           */
28 /*****************************************************************************/
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <fcntl.h>
33 #include <sys/types.h>
34 #include <locale.h>
35 #include <sys/stat.h>
36 #include <sys/param.h>
37 #include <X11/Intrinsic.h>
38 #include <X11/StringDefs.h>
39 #include <Xm/XmAll.h>
40 #include <Xm/Xm.h>
41 #include <Xm/Protocols.h>
42 #include <Xm/VendorSEP.h>
43 #include <Dt/Icon.h>
44 #include <Dt/EnvControlP.h>
45 #include <Dt/UserMsg.h>
46
47 #define GETXMSTRING(s, m, d)    XmStringCreateLocalized(GETMESSAGE(s,m,d))
48 #define CLASS_NAME              "Dtcreate"
49
50 /* Copied from Xm/BaseClassI.h */
51 extern XmWidgetExtData _XmGetWidgetExtData( 
52                         Widget widget,
53 #if NeedWidePrototypes
54                         unsigned int extType) ;
55 #else
56                         unsigned char extType) ;
57 #endif /* NeedWidePrototypes */
58
59 #define NOEXTERN
60 #include "dtcreate.h"
61 #include "ca_aux.h"
62 #include "cmnrtns.h"
63
64 #ifdef __TOOLTALK
65 #include <Tt/tttk.h>
66 void DieFromToolTalkError(Widget, char*, Tt_status);
67 Tt_message ProcessToolTalkMessage( );
68 #endif
69
70 #if !defined(NL_CAT_LOCALE)
71 #define NL_CAT_LOCALE 0
72 #endif
73
74
75 /*----------------------------------------------------
76  * UxXt.h needs to be included only when compiling a
77  * stand-alone application.
78  *---------------------------------------------------*/
79 #ifndef DESIGN_TIME
80 #include "UxXt.h"
81 #endif /* DESIGN_TIME */
82
83 XtAppContext    UxAppContext;
84 Widget          UxTopLevel;
85 Display         *UxDisplay;
86 int             UxScreen;
87
88 /*----------------------------------------------
89  * Insert application global declarations here
90  *---------------------------------------------*/
91
92 extern Dimension wintypeheight;
93
94 /*  Structure used on a save session to see if a dt is iconic  */
95 typedef struct {
96    int state;
97    Window icon;
98 } WM_STATE;
99
100 /*****************************************************************************/
101 /*                                                                           */
102 /* Routine:   GetSessionInfo                                                 */
103 /*                                                                           */
104 /* Purpose:   get session management saved information                       */
105 /*                                                                           */
106 /*****************************************************************************/
107 void GetSessionInfo( void )
108 {
109     XrmDatabase        db;
110     char               *tmpStr, *tmpStr2;
111     char               *path;
112     XrmName            xrm_name[5];
113     XrmRepresentation  rep_type;
114     XrmValue           value;
115     char               *msgPtr;
116     char               *fmtPtr;
117     char               *errPtr;
118
119 #ifdef DEBUG
120   if (debug)
121     stat_out("GetSessionInfo\n");
122 #endif
123
124     /*****************************************/
125     /***  Open the resource database file  ***/
126     /*****************************************/
127
128     /************************************************************************/
129     /*  UxTopLevel is used because CreateActionAppShell isn't created yet...*/
130     /*  okay because it only uses it to get a display, not a window         */
131     /************************************************************************/
132     if (DtSessionRestorePath(UxTopLevel, &path, sessiondata.pszSessionFile) == False)
133         path = sessiondata.pszSessionFile;
134 #ifdef DEBUG
135     printf("session file = %s\n", path);
136 #endif
137     if ((db = XrmGetFileDatabase (path)) == NULL)
138     {
139        msgPtr = GETMESSAGE(5, 162, "Could not restore session from file:");
140        fmtPtr = "%s\n %s\n";
141        errPtr = XtMalloc((strlen(msgPtr) + strlen(fmtPtr) +
142                           strlen(path) + 1) * sizeof(char));
143        sprintf(errPtr, fmtPtr, msgPtr, path);
144        display_error_message(UxTopLevel, errPtr);
145        XtFree(errPtr);
146        if (path != sessiondata.pszSessionFile) {
147           XtFree(path);
148        }
149        sessiondata.useSession = False;
150        return;
151     }
152     if (path != sessiondata.pszSessionFile) {
153        XtFree(path);
154     }
155
156     /*** now get the information we want from the database ***/
157     /*** make sure values are at least somewhat reasonable ***/
158
159     xrm_name[1] = '\0';
160
161     /*******************************/
162     /* get x position              */
163     /*******************************/
164     xrm_name[0] = XrmStringToQuark ("x");
165     if (XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value))
166         sessiondata.x = (Position)atoi((char *)value.addr);
167     else
168         sessiondata.x = 100;
169     if (sessiondata.x < 0) sessiondata.x = 0;
170
171     /*******************************/
172     /* get y position              */
173     /*******************************/
174     xrm_name[0] = XrmStringToQuark ("y");
175     if (XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value))
176         sessiondata.y = (Position)atoi((char *)value.addr);
177     else
178         sessiondata.y = 100;
179     if (sessiondata.y < 0) sessiondata.y = 0;
180
181     /*******************************/
182     /* get width                   */
183     /*******************************/
184     xrm_name[0] = XrmStringToQuark ("width");
185     if (XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value))
186         sessiondata.width = (Dimension)atoi((char *)value.addr);
187     /*
188     else
189         sessiondata.width = DEFAULT_WIDTH;
190     if (sessiondata.width < DEFAULT_WIDTH) sessiondata.width = DEFAULT_WIDTH;
191     */
192
193     /*******************************/
194     /* get height                  */
195     /*******************************/
196     xrm_name[0] = XrmStringToQuark ("height");
197     if (XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value))
198         sessiondata.height = (Dimension)atoi((char *)value.addr);
199     /*
200     else
201         sessiondata.height = DEFAULT_HEIGHT;
202     if (sessiondata.height < DEFAULT_HEIGHT) sessiondata.height = DEFAULT_HEIGHT;
203     */
204
205     /*******************************/
206     /* get iconic state            */
207     /*******************************/
208     xrm_name[0] = XrmStringToQuark ("iconic");
209     if (XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value))
210     {
211         if ( value.addr!=NULL && strcmp((char *)value.addr, "True")==0 )
212             sessiondata.iconicState = IconicState;
213         else
214             sessiondata.iconicState = NormalState;
215     }
216
217     /*******************************/
218     /* get expert status           */
219     /*******************************/
220     xrm_name[0] = XrmStringToQuark ("expert");
221     if (XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value))
222     {
223         sessiondata.showExpert = ((value.addr != NULL) &&
224                                   (strcmp((char *)value.addr, "True") == 0));
225     }
226
227     /*******************************/
228     /* get workspace #             */
229     /*******************************/
230     xrm_name[0] = XrmStringToQuark ("workspace");
231     if (XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value))
232     {
233         if ( value.addr!=NULL)
234             strcpy(sessiondata.workspaces, value.addr);
235         else
236             sessiondata.workspaces[0] = '\0';
237     }
238
239     /*******************************/
240     /* get file name               */
241     /*******************************/
242     xrm_name[0] = XrmStringToQuark ("file");
243     if (XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value)) {
244 #ifdef DEBUG
245        printf("file to edit = '%s'\n", value.addr);
246 #endif
247        pszFileToEdit = (char *)XtMalloc(strlen(value.addr) + 1);
248        strcpy(pszFileToEdit, value.addr);
249     }
250 }
251 /*****************************************************************************/
252 /*                                                                           */
253 /* Routine:   SaveSession                                                    */
254 /*                                                                           */
255 /* Purpose:   save state information for session management                  */
256 /*                                                                           */
257 /*****************************************************************************/
258 void SaveSession( void )
259 {
260     char                    *path, *name;
261     int                     fd, n;
262     char                    *xa_CommandStr[3];
263     char                    *tmpStr, *tmpStr2;
264     Position                x,y;
265     Dimension               width, height;
266     char                    *msgPtr, *fmtPtr, *errPtr;
267     XmVendorShellExtObject  vendorExt;
268     XmWidgetExtData         extData;
269     WM_STATE                *wmState;
270     Atom                    wmStateAtom, actualType;
271     Atom                    *ws_presence = NULL;
272     int                     actualFormat, j;
273     unsigned long           nitems, leftover, num_workspaces=0;
274     char                    all_workspace_names[256];
275     char                    *workspace_name;
276     Display                 *display = XtDisplay(CreateActionAppShell);
277     FILE                    *fp;
278
279     DtSessionSavePath(CreateActionAppShell, &path, &name);
280 #ifdef DEBUG
281     printf("path = %s\n", path);
282     printf("name = %s\n", name);
283 #endif
284
285     /*  Create the session file  */
286     if (((fd = creat(path, S_IRUSR | S_IRGRP | S_IWUSR | S_IWGRP)) == -1) ||
287         ((fp = fdopen(fd, "w")) == (FILE *)NULL))
288     {
289        msgPtr = GETMESSAGE(5, 160, "Could not save session to file:");
290        fmtPtr = "%s\n %s\n";
291        errPtr = XtMalloc((strlen(msgPtr) + strlen(fmtPtr) +
292                           strlen(path) + 1) * sizeof(char));
293        sprintf(errPtr, fmtPtr, msgPtr, path);
294        display_error_message(CreateActionAppShell, errPtr);
295        XtFree(errPtr);
296        if (fd != -1)
297        {
298            close(fd);
299            unlink(path);
300        }
301        XtFree ((char *)path);
302        XtFree ((char *)name);
303        return;
304     }
305
306     /*  Getting the WM_STATE property to see if iconified or not */
307 #ifdef DEBUG
308     printf("getting wm state\n");
309 #endif
310     wmStateAtom = XInternAtom(display, "WM_STATE", False);
311
312 #ifdef DEBUG
313     printf("getting window property\n");
314 #endif
315     XGetWindowProperty (XtDisplay(CreateActionAppShell),
316                         XtWindow(CreateActionAppShell), wmStateAtom, 0L,
317                         (long)BUFSIZ, False, wmStateAtom, &actualType,
318                         &actualFormat, &nitems, &leftover,
319                         (unsigned char **) &wmState);
320
321     if (wmState->state == IconicState)
322         fprintf(fp, "*iconic: True\n");
323     else
324         fprintf(fp, "*iconic: False\n");
325
326     fprintf(fp, "*expert: %s\n",
327             XtIsManaged(XprtOptionForm) ? "True" : "False");
328
329     /*** Get and write out the geometry info for our Window ***/
330
331 #ifdef DEBUG
332     printf("getting geometry info\n");
333 #endif
334
335     x = XtX(CreateActionAppShell);
336     y = XtY(CreateActionAppShell);
337     width = XtWidth(CreateActionAppShell);
338     height = XtHeight(CreateActionAppShell);
339
340     /*  Modify x & y to take into account window mgr frames
341      *  This is pretty bogus, but I don't know a better way to do it.
342      */
343     extData = _XmGetWidgetExtData(CreateActionAppShell, XmSHELL_EXTENSION);
344     vendorExt = (XmVendorShellExtObject)extData->widget;
345     x -= vendorExt->vendor.xOffset;
346     y -= vendorExt->vendor.yOffset;
347
348     fprintf(fp, "*x: %d\n", x);
349     fprintf(fp, "*y: %d\n", y);
350     fprintf(fp, "*width: %d\n", width);
351     fprintf(fp, "*height: %d\n", height);
352
353 #ifdef DEBUG
354     printf("getting workspaces this window is present \n");
355 #endif
356
357     if (DtWsmGetWorkspacesOccupied (display,
358                                     XtWindow(CreateActionAppShell),
359                                     &ws_presence, &num_workspaces) == Success)
360     {
361         for (j = 0; j < num_workspaces; j++)
362         {
363             workspace_name = XGetAtomName (display, ws_presence[j]);
364             if (j == 0)
365                 (void) strcpy(all_workspace_names, workspace_name);
366             else
367             {
368                 (void) strcat(all_workspace_names, "*");
369                 (void) strcat(all_workspace_names, workspace_name);
370             }
371             XFree ((char *) workspace_name);
372         }
373         XtFree((char *)ws_presence);
374         fprintf(fp, "*workspace: %s\n", all_workspace_names);
375     }
376
377     /*  This will be used to indicate what file is currently being edited. */
378     if (pszFileToEdit != NULL) {
379         fprintf(fp, "*file: %s\n", pszFileToEdit);
380     }
381
382     fflush(fp);
383     fclose(fp);
384
385     n = 0;
386     xa_CommandStr[n] = pszExecName; n++;
387     xa_CommandStr[n] =  "-s";       n++;
388     xa_CommandStr[n] = name;        n++;
389
390     XSetCommand(XtDisplay(CreateActionAppShell), XtWindow(CreateActionAppShell), xa_CommandStr, n);
391     XtFree ((char *)path);
392     XtFree ((char *)name);
393
394     /*  Don't exit yet, SM needs time to get the new commandStr.*/
395 }
396
397 /*****************************************************************************/
398 /*                                                                           */
399 /* SaveSessionCB                                                             */
400 /*                                                                           */
401 /* Session is ending, save session information                               */
402 /*                                                                           */
403 /*****************************************************************************/
404 static void
405 SaveSessionCB (
406         Widget w,
407         XtPointer clientData,
408         XtPointer callbackArg )
409 {
410   char      *xa_CommandStr[3];
411   int       i;
412
413 #if 0
414   i = 0;
415   xa_CommandStr[i] = pszExecName; i++;
416   XSetCommand(XtDisplay(CreateActionAppShell), XtWindow(CreateActionAppShell), xa_CommandStr, i);
417 #endif
418
419   SaveSession();
420 }
421
422 /*****************************************************************************/
423 /*                                                                           */
424 /*  GetIconDataFromWid                                                       */
425 /*                                                                           */
426 /*                                                                           */
427 /*****************************************************************************/
428 IconData *GetIconDataFromWid( Widget wid )
429 {
430   int       i;
431   IconData  *pIconData;
432
433   for (i=0; i < ICON_NUMBER; i++) {
434      if ((IconDataList[i]) && (IconDataList[i]->wid == wid)) {
435         return(IconDataList[i]);
436      }
437   }
438   return((IconData *)NULL);
439 }
440
441 /*****************************************************************************/
442 /*                                                                           */
443 /*  GetIconDataFromMsgID                                                     */
444 /*                                                                           */
445 /*                                                                           */
446 /*****************************************************************************/
447 IconData *GetIconDataFromMsgID( char *msgID )
448 {
449   int       i;
450   IconData  *pIconData;
451
452   for (i=0; i < ICON_NUMBER; i++) {
453      if (IconDataList[i]->pmMsgID) {
454         if (!strcmp(IconDataList[i]->pmMsgID, msgID)) {
455            return(IconDataList[i]);
456         }
457      }
458      if (IconDataList[i]->bmMsgID) {
459         if (!strcmp(IconDataList[i]->bmMsgID, msgID)) {
460            return(IconDataList[i]);
461         }
462      }
463   }
464   /*
465   for (i=0; i < ICON_NUMBER; i++) {
466      if ( ((IconDataList[i]->pmMsgID) &&
467            (!strcmp(IconDataList[i]->pmMsgID, msgID))) ||
468           ((IconDataList[i]->bmMsgID) &&
469            (!strcmp(IconDataList[i]->bmMsgID, msgID))) ) {
470         return(IconDataList[i]);
471      }
472   }
473   */
474   return((IconData *)NULL);
475 }
476
477 /*****************************************************************************/
478 /*                                                                           */
479 /*  RemoveTmpIconFiles                                                       */
480 /*                                                                           */
481 /*                                                                           */
482 /*****************************************************************************/
483 void RemoveTmpIconFiles( void )
484 {
485   int       i;
486   IconData  *pIconData;
487   char      maskFile[MAXFILENAME];
488
489   for (i=0; i < ICON_NUMBER; i++) {
490      if ( (IconDataList[i]->pmDirtyBit) &&
491           (IconDataList[i]->pmFileName) &&
492           (strlen(IconDataList[i]->pmFileName)) ) {
493 #ifdef DEBUG
494         printf("RemoveTmpIconFiles: unlink '%s'\n", IconDataList[i]->pmFileName);  /* debug */
495 #endif
496         unlink(IconDataList[i]->pmFileName);
497         /* This is bogus.  But just in case the user specified a bitmap */
498         /* rather than a pixmap during the edit process, this will make */
499         /* sure that file gets removed.  The chance of this occurring   */
500         /* is very unlikely.                                            */
501         strcpy(maskFile, IconDataList[i]->pmFileName);
502         strcat(maskFile, "_m");
503
504 #ifdef DEBUG
505         printf("Remove maskFile = %s\n", maskFile); /* debug */
506 #endif
507         unlink(maskFile);
508      }
509      if ( (IconDataList[i]->bmDirtyBit) &&
510           (IconDataList[i]->bmFileName) &&
511           (strlen(IconDataList[i]->bmFileName)) ) {
512 #ifdef DEBUG
513         printf("RemoveTmpIconFiles: unlink '%s'\n", IconDataList[i]->bmFileName);  /* debug */
514 #endif
515         unlink(IconDataList[i]->bmFileName);
516         strcpy(maskFile, IconDataList[i]->bmFileName);
517         strcat(maskFile, "_m");
518 #ifdef DEBUG
519         printf("Remove maskFile = %s\n", maskFile); /* debug */
520 #endif
521         unlink(maskFile);
522      }
523   }
524 }
525
526 #ifdef __TOOLTALK
527 /*****************************************************************************/
528 /*                                                                           */
529 /*  DieFromToolTalkError                                                     */
530 /*                                                                           */
531 /*                                                                           */
532 /*****************************************************************************/
533 static void
534 ExitCB (Widget dialog, XtPointer client_data, XtPointer call_data)
535 {
536     exit((int) client_data);
537 }
538
539 void
540 DieFromToolTalkError(Widget parent, char *errfmt, Tt_status status)
541 {
542     Arg          args[10];
543     Widget       dialog, dialogShell;
544     char        *errmsg, *statmsg, *title;
545     XmString     xms_errmsg, xms_ok, xms_title;
546     int          n;
547
548     if (! tt_is_err(status)) return;
549
550     statmsg = tt_status_message(status);
551     /* Solaris dtcreate ignores this so we should too */
552     if(status == TT_ERR_PTYPE)
553             return;
554     errmsg = XtMalloc(strlen(errfmt) + strlen(statmsg) + 2);
555     sprintf(errmsg, errfmt, statmsg);
556
557     xms_ok = GETXMSTRING(13, 3, "OK");
558     xms_errmsg = XmStringCreateLocalized(errmsg);
559     xms_title = GETXMSTRING(13, 1, "Create Action - Error");
560
561     n = 0;
562     XtSetArg(args[n], XmNautoUnmanage, False); n++;
563     XtSetArg(args[n], XmNokLabelString, xms_ok); n++;
564     XtSetArg(args[n], XmNdialogTitle, xms_title); n++;
565     XtSetArg(args[n], XmNmessageString, xms_errmsg); n++;
566     XtSetArg(args[n], XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL); n++;
567
568     dialog = XmCreateErrorDialog(parent, "IconEditorError", args, n);
569     XtAddCallback(dialog, XmNokCallback, ExitCB, (XtPointer) status);
570     XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_CANCEL_BUTTON));
571     XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON));
572
573     /*
574      * Disable the frame menu from dialog since we don't want the user
575      * to be able to close dialogs with the frame menu
576      */
577     dialogShell = XtParent(dialog);
578     n = 0;
579     XtSetArg(args[n], XmNmwmDecorations, MWM_DECOR_ALL | MWM_DECOR_MENU); n++;
580     XtSetValues(dialogShell, args, n);
581     XtManageChild(dialog);
582     XtRealizeWidget(dialogShell);
583
584     _DtSimpleError(CLASS_NAME, DtFatalError, NULL, errmsg);
585
586     XtFree(errmsg);
587     XmStringFree(xms_ok);
588     XmStringFree(xms_errmsg);
589     XmStringFree(xms_title);
590
591     while (TRUE)
592       XtAppProcessEvent(XtWidgetToApplicationContext(dialog), XtIMAll);
593 }
594
595 /*****************************************************************************/
596 /*                                                                           */
597 /*  ProcessToolTalkMessage                                                   */
598 /*                                                                           */
599 /*                                                                           */
600 /*****************************************************************************/
601 Tt_message ProcessToolTalkMessage(Tt_message msg,
602                                   void* cdata,
603                                   Tt_message contract )
604 {
605
606   int ttMark = tt_mark();
607
608   if( 0 == strcmp( tt_message_op( msg ), "Quit" ) ) {
609           /*
610            * Do the necessary cleanup, call tt_message_reply(),
611            * and exit.  Since this is not yet fully implemented, just
612            * call tt_message_reply and exit.
613            */
614
615 #ifdef DEBUG
616           printf("ProcessTTMsg: Quit\n");  /* debug */
617 #endif
618           tt_message_reply(msg);
619           RemoveTmpIconFiles();
620           exit(1);
621   } else {
622           /*
623            * Any other desktop message ops that one would want
624            * to implement would go here.  If
625            * a mapped, realized, top-level widget were passed into
626            * ttdt_session_join instead of (Widget) 0, then the
627            * messages that interact with the X environment
628            * Get_Geometry, etc.) would be handled automatically.
629            */
630
631 #ifdef DEBUG
632           printf("ProcessTTMsg: Other\n");
633 #endif
634
635   }
636   tt_release( ttMark );
637
638   return (Tt_message) 0;
639 }
640
641 /*****************************************************************************/
642 /* NAME:        IconEdit_tt_handler                                          */
643 /* DESCRIPTION: Handler for Tooltalk conversation about editing icon.        */
644 /* PARAMETERS:                                                               */
645 /* RETURN:      none                                                         */
646 /* CREATION:    02/21/94                                                     */
647 /* REVISIONS:                                                                */
648 /*****************************************************************************/
649 Tt_callback_action IconEdit_tt_handler( Tt_message m, Tt_pattern p )
650 {
651   int             ttMark;
652   int             blen;
653   int             wlen;
654   int             tmpfd = -1;
655   Tt_state        mstate;
656   char            *msgPtr, *fmtPtr, *errPtr;
657   char            tmpbuf[MAXBUFSIZE];
658   char*           ttbuf;
659   char*           op;
660   char            *tmpIconFile;
661   char            *msgID;
662   IconData        *pIconData;
663   Boolean         bIsNewFile = False;
664
665   char            *tmp = (char *)NULL;
666   char            *tmp1 = (char *)NULL;
667   int             tmplen;
668
669   ttMark = tt_mark( );
670
671   op = tt_message_op(m);
672   /************************************************************************/
673   /* Edit message...                                                      */
674   /************************************************************************/
675   if (!strcmp(op, "Edit")) {
676 #ifdef DEBUG
677      printf("IconEdit_tt_handler: Edit tt msg = %p\n", m);  /* debug */
678 #endif
679      mstate = tt_message_state( m );
680      switch ((int)mstate) {
681          case TT_FAILED:
682              TurnOffHourGlassAllWindows();
683
684              /* INTERNATIONALIZE */
685              msgPtr = "Failed ToolTalk msg";
686              fmtPtr = "%s = %p";
687              errPtr = XtMalloc((strlen(msgPtr) + strlen(fmtPtr) +
688                                 20) * sizeof(char));
689              sprintf(errPtr, fmtPtr, msgPtr, m);
690              display_error_message(CreateActionAppShell, errPtr);
691              XtFree(errPtr);
692 #ifdef DEBUG
693              printf( "Failed ToolTalk msg = %p\n", m);   /* debug */
694 #endif
695              tt_message_destroy( m );
696              break;
697          case TT_HANDLED:
698              TurnOffHourGlassAllWindows();
699 #ifdef DEBUG
700              printf( "Handled ToolTalk msg = %p\n", m);  /* debug */
701 #endif
702              tt_message_destroy( m );
703              break;
704 #ifdef DEBUG
705          case TT_STARTED:
706              printf( "Started ToolTalk msg = %p\n", m);  /* debug */
707 #endif
708      }
709   } else {
710 #ifdef DEBUG
711      printf("IconEdit_tt_handler: Saved tt msg = %p\n", m);  /* debug */
712 #endif
713      msgID = tt_message_arg_val(m, 1);
714      pIconData = GetIconDataFromMsgID(msgID);
715 #ifdef DEBUG
716      printf("msgID = '%s'\n", msgID);  /* debug */
717 #endif
718
719      /*********************************************************************/
720      /* Saved message...                                                  */
721      /*********************************************************************/
722      if ( (pIconData) && (!strcmp(op, "Saved")) ) {
723         tt_message_arg_bval(m, 0, (unsigned char **)&ttbuf, &blen);
724
725         if ( (pIconData->pmMsgID) && (!strcmp(pIconData->pmMsgID, msgID)) ) {
726            if (pIconData->pmDirtyBit) {
727               tmpIconFile = pIconData->pmFileName;
728            } else {
729               tmpnam(pIconData->pmFileName);
730               bIsNewFile = True;
731               tmpIconFile = pIconData->pmFileName;
732            }
733         } else {
734            if (pIconData->bmDirtyBit) {
735               tmpIconFile = pIconData->bmFileName;
736            } else {
737               tmpnam(pIconData->bmFileName);
738               bIsNewFile = True;
739               tmpIconFile = pIconData->bmFileName;
740            }
741         }
742         if (bIsNewFile) {
743             tmpfd = open(tmpIconFile, O_CREAT | O_WRONLY | O_NDELAY, 0666);
744         } else {
745            tmpfd = open(tmpIconFile, O_TRUNC | O_WRONLY | O_NDELAY, 0666);
746         }
747         if (tmpfd == -1) {
748            msgPtr = GETMESSAGE(5, 60, "Could not open the following file:");
749            fmtPtr = "%s\n   %s";
750            errPtr = XtMalloc((strlen(msgPtr) + strlen(fmtPtr) +
751                               strlen(tmpIconFile) + 1) * sizeof(char));
752            sprintf(errPtr, fmtPtr, msgPtr, tmpIconFile);
753            display_error_message(XtParent(pIconData->wid), errPtr);
754            XtFree(errPtr);
755            tt_release( ttMark );
756            return;
757         }
758
759         /*******************************************************************/
760         /* Determine if this is a pixmap or bitmap.  If it is a pixmap,    */
761         /* handle as a one file buffer.  If a bitmap, then it may be       */
762         /* double buffered including the mask.                             */
763         /*******************************************************************/
764
765         /*******************************************************************/
766         /* Check for Pixmap first.                                         */
767         /*******************************************************************/
768         if ( (ttbuf) && (!strncmp(ttbuf, "/* XPM */", 9)) ) {
769 #ifdef DEBUG
770            printf("This is a XPM file\n");  /* debug */
771 #endif
772            tmp = strstr(ttbuf, "};");
773            if (tmp) {
774               tmp += 2;                        /* Go one char past the ";" */
775               /*
776               tmplen = blen - strlen(tmp) + 1;
777               printf("Amro tmplen = %d\n", tmplen);
778               */
779               tmplen = (char *)tmp - (char *)ttbuf;
780 #ifdef DEBUG
781               printf("Troy tmplen = %d\n", tmplen); /* debug */
782 #endif
783            } else {
784               tmplen = blen;
785            }
786            if ((wlen = write(tmpfd, ttbuf, tmplen)) == tmplen) {
787               if (tmpfd > -1) close(tmpfd);
788            } else {
789               /* INTERNATIONALIZE */
790               msgPtr = "Could not write to the following file:";
791               fmtPtr = "%s\n   %s";
792               errPtr = XtMalloc((strlen(msgPtr) + strlen(fmtPtr) +
793                                  strlen(tmpIconFile) + 1) * sizeof(char));
794               sprintf(errPtr, fmtPtr, msgPtr, tmpIconFile);
795               display_error_message(XtParent(pIconData->wid), errPtr);
796               XtFree(errPtr);
797               tt_release( ttMark );
798               if (tmpfd > -1) close(tmpfd);
799               return;
800            }
801
802         /*******************************************************************/
803         /* Must be a Bitmap.                                               */
804         /*******************************************************************/
805         } else {
806 #ifdef DEBUG
807            printf("This is a XBM file\n");  /* debug */
808            printf("ttbuffer size = %d\n", blen); /* debug */
809 #endif
810            /****************************************************************/
811            /* Determine size of first bitmap.                              */
812            /****************************************************************/
813            tmp = strstr(ttbuf, "};");
814            if (tmp) {
815               tmp += 2;                        /* Go one char past the ";" */
816               /*
817               tmplen = blen - strlen(tmp) + 1;
818               printf("Amro tmplen = %d\n", tmplen);
819               */
820               tmplen = (char *)tmp - (char *)ttbuf;
821 #ifdef DEBUG
822               printf("Troy tmplen = %d\n", tmplen); /* debug */
823 #endif
824            } else {
825               tmplen = blen;
826            }
827 #ifdef DEBUG
828            printf("File 1 size = %d\n", tmplen); /* debug */
829 #endif
830
831            /****************************************************************/
832            /* Write out first bitmap file.                                 */
833            /****************************************************************/
834 #ifdef DEBUG
835            printf("Writing first bitmap file\n"); /* debug */
836 #endif
837            if ((wlen = write(tmpfd, ttbuf, tmplen)) == tmplen) {
838               if (tmpfd > -1) close(tmpfd);
839            } else {
840               /* INTERNATIONALIZE */
841               msgPtr = "Could not write to the following file:";
842               fmtPtr = "%s\n   %s";
843               errPtr = XtMalloc((strlen(msgPtr) + strlen(fmtPtr) +
844                                  strlen(tmpIconFile) + 1) * sizeof(char));
845               sprintf(errPtr, fmtPtr, msgPtr, tmpIconFile);
846               display_error_message(XtParent(pIconData->wid), errPtr);
847               XtFree(errPtr);
848               tt_release( ttMark );
849               if (tmpfd > -1) close(tmpfd);
850               return;
851            }
852
853            /****************************************************************/
854            /* Now determine if there is a second bitmap and its size.      */
855            /****************************************************************/
856            if (tmp) {
857               tmp1 = strstr(tmp, "};");
858            }
859            sprintf(tmpbuf, "%s_m", tmpIconFile);
860            if (tmp1) {
861               tmp1 += 2;                       /* Go one char past the ";" */
862 #ifdef DEBUG
863               printf("Mask file name = '%s'\n", tmpbuf); /* debug */
864 #endif
865               tmpfd = open(tmpbuf, O_CREAT | O_WRONLY | O_NDELAY, 0666);
866               if (tmpfd == -1) {
867                  msgPtr =
868                      GETMESSAGE(5, 60, "Could not open the following file:");
869                  fmtPtr = "%s\n   %s";
870                  errPtr = XtMalloc((strlen(msgPtr) + strlen(fmtPtr) +
871                                     strlen(tmpbuf) + 1) * sizeof(char));
872                  sprintf(errPtr, fmtPtr, msgPtr, tmpbuf);
873                  display_error_message(XtParent(pIconData->wid), errPtr);
874                  XtFree(errPtr);
875                  tt_release( ttMark );
876                  return;
877               }
878
879 #ifdef DEBUG
880               printf("writing mask file \n");
881 #endif
882               /*
883               tmplen = strlen(tmp);
884               printf("Amro tmplen = %d\n", tmplen);
885               */
886               tmplen = (char *)tmp1 - (char *)tmp;
887 #ifdef DEBUG
888               printf("Troy tmplen = %d\n", tmplen); /* debug */
889 #endif
890               if ((wlen = write(tmpfd, tmp, tmplen)) == tmplen) {
891                  if (tmpfd > -1) close(tmpfd);
892               } else {
893                  /* INTERNATIONALIZE */
894                  msgPtr = "Could not write to the following file:";
895                  fmtPtr = "%s\n   %s";
896                  errPtr = XtMalloc((strlen(msgPtr) + strlen(fmtPtr) +
897                                     strlen(tmpbuf) + 1) * sizeof(char));
898                  sprintf(errPtr, fmtPtr, msgPtr, tmpbuf);
899                  display_error_message(XtParent(pIconData->wid), errPtr);
900                  XtFree(errPtr);
901                  tt_release( ttMark );
902                  if (tmpfd > -1) close(tmpfd);
903                  return;
904               }
905
906            /****************************************************************/
907            /* Must not have a mask, so lets clean up any old mask file.    */
908            /****************************************************************/
909            } else {
910 #ifdef DEBUG
911               printf("Remove maskFile = %s\n", tmpbuf); /* debug */
912 #endif
913               unlink(tmpbuf);
914            }
915         }
916
917         /****************************************************************/
918         /* Set the icon gadget with new icon, and flag the dirty bit.   */
919         /****************************************************************/
920         if ( ( (pIconData->pmMsgID) &&
921                (!strcmp(pIconData->pmMsgID, msgID)) &&
922                (bShowPixmaps) )
923            ||
924              ( (pIconData->bmMsgID) &&
925                (!strcmp(pIconData->bmMsgID, msgID)) &&
926                (!bShowPixmaps) ) ) {
927            XmeFlushIconFileCache(NULL);
928 #ifdef DEBUG
929            printf("Change icon image for widget = %p\n", pIconData->wid); /* debug */
930 #endif
931            SET_ICONGADGET_ICON(pIconData->wid, tmpIconFile);
932         }
933         if ((pIconData->pmMsgID) && (!strcmp(pIconData->pmMsgID, msgID))) {
934            pIconData->pmDirtyBit = True;
935         } else {
936            pIconData->bmDirtyBit = True;
937         }
938 #ifdef DEBUG
939         printf("**********tmp file name = %s\n", tmpIconFile);  /* debug */
940 #endif
941      }
942   }
943   tt_release( ttMark );
944   return( TT_CALLBACK_PROCESSED );
945 }
946
947 /*****************************************************************************/
948 /* NAME:        UxDoEditPixmap                                               */
949 /* DESCRIPTION: Loads the icon editor.                                       */
950 /* PARAMETERS:  none                                                         */
951 /* RETURN:      none                                                         */
952 /* CREATION:    02/11/94                                                     */
953 /* REVISIONS:                                                                */
954 /*****************************************************************************/
955 void UxDoEditPixmap(Widget wid, char *fname)
956 {
957   Tt_message     m;         /* ToolTalk message */
958   Tt_pattern     p;         /* ToolTalk dynamic pattern */
959   Tt_status      ttRc;      /* ToolTalk status  */
960
961   int            fd1 = -1;
962   int            fd2 = -1;
963
964   int            lenFile1 = 0, rlenFile1 = 0;
965   int            lenFile2 = 0, rlenFile2 = 0;
966
967   int            mark;
968   char           *msgID;
969   char           **ppMsgID;
970   IconData       *pIconData;
971   unsigned char  *buffer;
972   char           *ptr;
973
974   char           tmpbuf[MAXBUFSIZE];
975   char           *errPtr;
976   char           *msgPtr;
977   char           *fmtPtr;
978   char           *mname = (char *)NULL;
979   char           *type_name, *size_name;
980
981
982 #ifdef DEBUG
983   if (fname) printf("Edit filename: '%s'\n", fname);  /* debug */
984 #endif
985
986   mark = tt_mark();
987
988   /*********************************************************************/
989   /* Create request                                                    */
990   /*********************************************************************/
991   m = tt_prequest_create( TT_SESSION, "Edit" );
992   msgID = tt_message_id(m);
993   pIconData = GetIconDataFromWid(wid);
994   if (bShowPixmaps) {
995      ppMsgID = &(pIconData->pmMsgID);
996   } else {
997      ppMsgID = &(pIconData->bmMsgID);
998   }
999   if (*ppMsgID) {
1000      free(*ppMsgID);
1001   }
1002   if (msgID && strlen(msgID)) {
1003      *ppMsgID = (char *)malloc(strlen(msgID) + 1);
1004      strcpy(*ppMsgID, msgID);
1005   }
1006
1007 #ifdef DEBUG
1008   printf("msgID = '%s'\n", msgID);  /* debug */
1009   printf("Create Edit msg = %p\n", m);  /* debug */
1010 #endif
1011
1012   /*********************************************************************/
1013   /* Add arguments to message                                          */
1014   /*********************************************************************/
1015   if (fname) {
1016      fd1 = open(fname, O_RDONLY);
1017      if (fd1 == -1) {
1018         /* Error opening icon file! */
1019         msgPtr = GETMESSAGE(5, 60, "Could not open the following file:");
1020         fmtPtr = "%s\n   %s";
1021         errPtr = XtMalloc((strlen(msgPtr) + strlen(fmtPtr) +
1022                            strlen(fname) + 1) * sizeof(char));
1023         sprintf(errPtr, fmtPtr, msgPtr, fname);
1024         display_error_message(XtParent(wid), errPtr);
1025         XtFree(errPtr);
1026         return;
1027      } else {
1028         /***************************************/
1029         /* Find out how big a buffer is needed */
1030         /***************************************/
1031         lenFile1 = lseek(fd1, 0, SEEK_END);
1032         (void) lseek(fd1, 0, SEEK_SET);
1033
1034         /*************************************************************/
1035         /* Read first few bytes to determine what kind of file it is */
1036         /*************************************************************/
1037         read(fd1, tmpbuf, 10);
1038         (void) lseek(fd1, 0, SEEK_SET);
1039
1040         /******************************************************************/
1041         /* If the file is a bitmap, then check for its mask file and add. */
1042         /******************************************************************/
1043         if ( strncmp(tmpbuf, "/* XPM */", 9) ) {
1044
1045            mname = CreateMaskName(fname);
1046
1047            /***************************************************************/
1048            /* Check if mask file exists.                                  */
1049            /***************************************************************/
1050            if ((mname) && (check_file_exists(mname))) {
1051               fd2 = open(mname, O_RDONLY);
1052               if (fd2 == -1) {
1053                  /* Error opening icon file! */
1054                  msgPtr =
1055                      GETMESSAGE(5, 60, "Could not open the following file:");
1056                  fmtPtr = "%s\n   %s";
1057                  errPtr = XtMalloc((strlen(msgPtr) + strlen(fmtPtr) +
1058                                     strlen(mname) + 1) * sizeof(char));
1059                  sprintf(errPtr, fmtPtr, msgPtr, mname);
1060                  display_error_message(XtParent(wid), errPtr);
1061                  XtFree(errPtr);
1062                  XtFree(mname);
1063                  return;
1064               } else {
1065                  /***************************************/
1066                  /* Find out how big a buffer is needed */
1067                  /***************************************/
1068                  lenFile2 = lseek(fd2, 0, SEEK_END);
1069                  (void) lseek(fd2, 0, SEEK_SET);
1070               }
1071            }
1072         }
1073         /******************************************************************/
1074         /* Create 1 big buffer                                            */
1075         /******************************************************************/
1076 #ifdef DEBUG
1077         printf("Size of file 1 = %d\n", lenFile1); /* debug */
1078         printf("Size of file 2 = %d\n", lenFile2); /* debug */
1079 #endif
1080         buffer = (unsigned char *)XtMalloc((Cardinal)(lenFile1 + lenFile2 + 1));
1081         memset(buffer, 0, (lenFile1 + lenFile2 + 1));
1082         if (buffer) {
1083            /***************************************/
1084            /* Read file1 into buffer.             */
1085            /***************************************/
1086            if ((rlenFile1 = read(fd1, buffer, lenFile1)) != lenFile1) {
1087               /* didn't read whole file! */
1088
1089               /* INTERNATIONALIZE */
1090               msgPtr = "Could not read the entire file:";
1091               fmtPtr = "%s\n   %s";
1092               errPtr = XtMalloc((strlen(msgPtr) + strlen(fmtPtr) +
1093                                  strlen(fname) + 1) * sizeof(char));
1094               sprintf(errPtr, fmtPtr, msgPtr, fname);
1095               display_error_message(XtParent(wid), errPtr);
1096               XtFree(errPtr);
1097               if (mname) XtFree(mname);
1098               XtFree((char *) buffer);
1099               return;
1100            }
1101            if (lenFile2) {
1102 #ifdef DEBUG
1103               printf("buffer = %p\n", buffer);
1104 #endif
1105             /*ptr = (char *)&(buffer[lenFile1 + 1]);*/
1106               ptr = (char *)&(buffer[lenFile1]);
1107 #ifdef DEBUG
1108               printf("ptr = %p\n", ptr);
1109 #endif
1110               if ((rlenFile2 = read(fd2, ptr, lenFile2)) != lenFile2) {
1111                  /* didn't read whole file! */
1112
1113                  /* INTERNATIONALIZE */
1114                  msgPtr = "Could not read the entire file:";
1115                  fmtPtr = "%s\n   %s";
1116                  errPtr = XtMalloc((strlen(msgPtr) + strlen(fmtPtr) +
1117                                     strlen(fname) + 1) * sizeof(char));
1118                  sprintf(errPtr, fmtPtr, msgPtr, mname);
1119                  display_error_message(XtParent(wid), errPtr);
1120                  XtFree(errPtr);
1121                  XtFree((char *) buffer);
1122                  if (mname) XtFree(mname);
1123                  return;
1124               }
1125            }
1126            if (fd1 > -1) close(fd1);
1127            if (fd2 > -1) close(fd2);
1128
1129 #ifdef DEBUG
1130            printf("final buffer = '%s'\n", buffer); /* debug */
1131            printf("Buffer = '%s'\n", buffer); /* debug */
1132 #endif
1133            tt_message_barg_add( m, TT_INOUT, "XPM", buffer, lenFile1 + lenFile2 + 1);
1134            if (mname) XtFree(mname);
1135            XtFree((char *) buffer);
1136         }
1137      }
1138   } else {
1139      tt_message_barg_add( m, TT_INOUT, "XPM", (const unsigned char *)NULL, 0);
1140   }
1141   tt_message_arg_add(m, TT_IN, "messageID", msgID);
1142
1143   /******************************************************************/
1144   /* Create a pattern and add a callback to get notifications       */
1145   /* of periodic saves                                              */
1146   /******************************************************************/
1147   p = tt_pattern_create();
1148   tt_pattern_category_set(p, TT_OBSERVE);
1149   tt_pattern_scope_add(p, TT_SESSION);
1150   tt_pattern_session_add(p, tt_default_session());
1151   tt_pattern_address_add(p, TT_HANDLER);
1152   tt_pattern_op_add(p, "Saved");
1153   tt_pattern_callback_add(p, IconEdit_tt_handler);
1154   tt_pattern_register(p);
1155
1156   /******************************************************************/
1157   /* Add callback to notify when change complete                    */
1158   /******************************************************************/
1159   tt_message_callback_add( m, IconEdit_tt_handler );
1160
1161   /******************************************************************/
1162   /* Send message                                                   */
1163   /******************************************************************/
1164   TurnOnHourGlassAllWindows();
1165   ttRc = tt_message_send( m );
1166   DieFromToolTalkError( UxTopLevel, "tt_message_send", ttRc );
1167 }
1168 #endif  /* __TOOLTALK */
1169
1170 /*****************************************************************************/
1171 /*                                                                           */
1172 /*                                                                           */
1173 /*    main                                                                   */
1174 /*                                                                           */
1175 /*                                                                           */
1176 /*                                                                           */
1177 /*****************************************************************************/
1178 main(int argc, char *argv[])
1179 {
1180         /*-----------------------------------------------------------
1181          * Declarations.
1182          * The default identifier - mainIface will only be declared
1183          * if the interface function is global and of type swidget.
1184          * To change the identifier to a different name, modify the
1185          * string mainIface in the file "xtmain.dat". If "mainIface"
1186          * is declared, it will be used below where the return value
1187          * of  PJ_INTERFACE_FUNCTION_CALL will be assigned to it.
1188          *----------------------------------------------------------*/
1189 #ifdef __TOOLTALK
1190         char * procId;
1191         int ttMark, ttFd;
1192         Tt_pattern *ttPatt;
1193         Tt_status ttRc;
1194         char * errfmt;
1195         char * sessionString;
1196         char * ttsession;
1197 #endif
1198
1199         Widget          mainIface;
1200         extern swidget  create_AddFiletype();
1201         extern swidget  create_FileCharacteristics();
1202         extern swidget  create_OpenFile();
1203         extern swidget  create_Confirmed();
1204         extern Widget   create_ErrorDialog();
1205         int             rc;
1206         Atom            xa_WM_SAVE_YOURSELF;
1207         char            *ptr;
1208
1209         /*---------------------------------
1210          * Interface function declaration
1211          *--------------------------------*/
1212         Widget  create_applicationShell1(swidget);
1213         swidget UxParent = NULL;
1214         int i;
1215
1216         /*---------------------
1217          * Initialize program
1218          *--------------------*/
1219
1220         _DtEnvControl(DT_ENV_SET);
1221         XtSetLanguageProc(NULL, NULL, NULL);
1222         pszFileToEdit = (char *)NULL;
1223         memset(&sessiondata, 0, sizeof(SessionData));
1224         memset(IconDataList, 0, sizeof(IconDataList));
1225
1226         /********************************************************************/
1227         /* Parse the command line arguments.                                */
1228         /********************************************************************/
1229         pszExecName = argv[0];
1230
1231         if (argc > 1) {
1232           for (i=1; i<argc; i++) {
1233             if ((argv[i][0] == '-') && (argv[i][1] == '?')) {
1234                setlocale(LC_ALL, "");
1235                nlmsg_fd = catopen("dtcreate", NL_CAT_LOCALE);
1236                printf("%s\n",
1237                       GETMESSAGE(5, 110, "Usage: dtcreate [<filename>] [-?]"));
1238                exit(0);
1239             } else {
1240                if (argv[i][0] == '-') {
1241                   if (!strcmp(argv[i], "-s")) {
1242                      i+=1;
1243                      sessiondata.pszSessionFile = (char *)XtMalloc(strlen(argv[i]) + 1);
1244                      strcpy(sessiondata.pszSessionFile, argv[i]);
1245                      sessiondata.useSession = True;
1246                   } else {
1247                      i+=1;
1248                   }
1249                } else {
1250                   pszFileToEdit = (char *)XtMalloc(strlen(argv[i]) + 1);
1251                   strcpy(pszFileToEdit, argv[i]);
1252                }
1253             }
1254           }
1255         }
1256
1257
1258         UxTopLevel = XtAppInitialize(&UxAppContext, CLASS_NAME,
1259                                      NULL, 0, &argc, argv, NULL, NULL, 0);
1260
1261         nlmsg_fd = catopen("dtcreate", NL_CAT_LOCALE);
1262
1263         UxDisplay = XtDisplay(UxTopLevel);
1264         UxScreen = XDefaultScreen(UxDisplay);
1265
1266         if (sessiondata.useSession) {
1267            GetSessionInfo();
1268         }
1269
1270         /*---------------------
1271          * Determine resolution
1272          *--------------------*/
1273         if (_DtGetDisplayResolution(UxDisplay, UxScreen) == 1) {
1274            bLowRes = True;
1275         } else {
1276            bLowRes = False;
1277         }
1278
1279 #ifdef DEBUG_LOWRES
1280         bLowRes = True;
1281 #endif
1282
1283         /*-------------------------------------------------------
1284          * Insert initialization code for your application here
1285          *------------------------------------------------------*/
1286 #ifdef __TOOLTALK
1287 #ifdef DEBUG
1288         printf("display string = '%s'\n", DisplayString(UxDisplay));
1289 #endif
1290         ttsession = getenv("TT_SESSION");
1291         if (!ttsession || strlen(ttsession)==0) {
1292            char *ttxsession = tt_X_session(XDisplayString(UxDisplay));
1293            ttRc = tt_default_session_set(ttxsession);
1294         }
1295
1296         ttMark = tt_mark( );
1297
1298         procId = tt_open( );
1299         ttRc = tt_ptr_error(procId);
1300         errfmt =
1301           GETMESSAGE(13, 2, "Could not connect to ToolTalk:\n%s\nExiting ...");
1302         DieFromToolTalkError( UxTopLevel, errfmt, ttRc );
1303
1304         ttFd = tt_fd( );
1305         DieFromToolTalkError( UxTopLevel, "tt_fd", tt_int_error( ttFd ) );
1306
1307         ttRc = tt_ptype_declare( "dtcreate" );
1308         DieFromToolTalkError( UxTopLevel, "tt_ptype_declare", ttRc );
1309
1310         ttRc = tt_session_join( tt_default_session( ));
1311         DieFromToolTalkError( UxTopLevel, "tt_session_join", ttRc );
1312
1313         XtAppAddInput( UxAppContext, ttFd, (XtPointer)XtInputReadMask,
1314                             (XtInputCallbackProc)ProcessToolTalkMessage, 0 );
1315 #endif
1316
1317         if (!pszFileToEdit) {
1318            initAD(&AD);
1319         }
1320
1321
1322         /*----------------------------------------------------------------
1323          * Create and popup the first window of the interface.  The
1324          * return value can be used in the popdown or destroy functions.
1325          * The Widget return value of  PJ_INTERFACE_FUNCTION_CALL will
1326          * be assigned to "mainIface" from  PJ_INTERFACE_RETVAL_TYPE.
1327          *---------------------------------------------------------------*/
1328
1329         mainIface = create_applicationShell1(UxParent);
1330         AddFiletype = create_AddFiletype(mainIface);
1331         FileCharacteristics = create_FileCharacteristics(AddFiletype);
1332         OpenFile = create_OpenFile();
1333
1334         UxPopupInterface(mainIface, no_grab);
1335         if (pszFileToEdit) {
1336            if (!OpenDefinitionFile(pszFileToEdit, &AD)) {
1337               writeCAToGUI(&AD);
1338            }
1339         }
1340         XtVaGetValues (CA_WindowTypeArea, XmNheight, &wintypeheight, NULL);
1341
1342         /*------------------------------------------------------------------*/
1343         /*   Register for save and restore.                                 */
1344         /*------------------------------------------------------------------*/
1345         xa_WM_SAVE_YOURSELF = XInternAtom(XtDisplay(CreateActionAppShell), "WM_SAVE_YOURSELF", False);
1346         XmAddWMProtocols(CreateActionAppShell, &xa_WM_SAVE_YOURSELF, 1);
1347         XmAddWMProtocolCallback(CreateActionAppShell, xa_WM_SAVE_YOURSELF,
1348                                 SaveSessionCB, (XtPointer)CreateActionAppShell);
1349
1350         /*------------------------------------------------------------------*/
1351         /* Make a copy of the AD data structure.  This will be used later   */
1352         /* when the user wants to exit to determine if the latest data has  */
1353         /* been saved or not.                                               */
1354         /*------------------------------------------------------------------*/
1355         pMyCopyAD = copyAD(&AD);
1356
1357 #ifdef __TOOLTALK
1358         DtDbLoad();
1359         ttMark = tt_mark( );
1360
1361         procId = ttdt_open( &ttFd, "dtcreate", "CDE", "1.0", 1 );
1362         ttRc = tt_ptr_error(procId);
1363         errfmt =
1364           GETMESSAGE(13, 2, "Could not connect to ToolTalk:\n%s\nExiting ...");
1365         DieFromToolTalkError( UxTopLevel, errfmt, ttRc );
1366
1367         ttFd = tt_fd( );
1368         ttRc = tt_int_error( ttFd );
1369         DieFromToolTalkError( UxTopLevel, "tt_fd", ttRc );
1370
1371         sessionString = tt_default_session();
1372         ttRc = tt_ptr_error( sessionString );
1373         DieFromToolTalkError( UxTopLevel, "tt_default_session", ttRc );
1374
1375         ttPatt = ttdt_session_join( sessionString, ProcessToolTalkMessage,
1376                                     (Widget) 0, (void *) 0, 1 );
1377         ttRc = tt_ptr_error(ttPatt);
1378         DieFromToolTalkError( UxTopLevel, "ttdt_session_join", ttRc );
1379
1380         XtAppAddInput( UxAppContext, ttFd, (XtPointer)XtInputReadMask,
1381                             tttk_Xt_input_handler, 0 );
1382 #endif
1383
1384         /*-----------------------
1385          * Enter the event loop
1386          *----------------------*/
1387
1388         XtAppMainLoop (UxAppContext);
1389
1390 #ifdef __TOOLTALK
1391         ttdt_session_quit(0, 0, 1);
1392         tt_close( );
1393         tt_release( ttMark );
1394         RemoveTmpIconFiles();
1395 #endif
1396 }