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