dtcalc: change from obsoleted MAXFLOAT to FLT_MAX from std C
[oweals/cde.git] / cde / examples / dtaction / actions.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 /* $XConsortium: actions.c /main/3 1995/10/27 10:37:37 rswiston $ */
24 /*
25  * (c) Copyright 1993, 1994 Hewlett-Packard Company
26  * (c) Copyright 1993, 1994 International Business Machines Corp.
27  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.
28  * (c) Copyright 1993, 1994 Novell, Inc.
29  */
30
31 #include <Xm/XmAll.h>
32 #include <Dt/Dt.h>
33 #include <Dt/Action.h>
34
35 #define ApplicationClass "Dtaction"
36
37 static Widget shell;
38 static XtAppContext appContext;
39 static Widget actionText;
40 static Widget fileText;
41
42 static void CreateWidgets(Widget);
43 static void InvokeActionCb(Widget, XtPointer, XtPointer);
44 static void InvokeAction(char*, char*);
45 static void DbReloadProc(XtPointer);
46
47 void main(int argc, char **argv) 
48 {
49     Arg args[20];
50     int n=0;
51     int numArgs = 0;
52
53     shell = XtAppInitialize(&appContext , ApplicationClass, NULL, 0, 
54                                                 &argc, argv, NULL, args, n);  
55
56     CreateWidgets(shell);
57
58     if (DtInitialize(XtDisplay(shell), shell, argv[0], ApplicationClass)==False) {
59         /* DtInitialize() has already logged an appropriate error msg */
60         exit(-1);
61     }
62
63     /* Load the filetype/action databases */
64     DtDbLoad();
65
66     /* Notice changes to the database without needing to restart application */
67     DtDbReloadNotify(DbReloadProc, NULL);
68
69     XtRealizeWidget(shell);
70     XmProcessTraversal(actionText, XmTRAVERSE_CURRENT);
71
72     XtAppMainLoop(appContext);
73 }
74
75 static void CreateWidgets(Widget shell)
76 {
77     Widget messageBox, workArea, w;
78     Arg args[20];
79     int n;
80     XmString labelString;
81
82     labelString = XmStringCreateLocalized("Invoke");
83
84     n = 0;
85     XtSetArg(args[n], XmNdialogType, XmDIALOG_TEMPLATE); n++;
86     XtSetArg(args[n], XmNokLabelString, labelString); n++;
87     messageBox = XmCreateMessageBox(shell, "messageBox", args, n);
88     XtManageChild(messageBox);
89     XmStringFree(labelString);
90     XtAddCallback(messageBox, XmNokCallback, InvokeActionCb, NULL);
91
92     n = 0;
93     XtSetArg(args[n], XmNorientation, XmVERTICAL); n++;
94     XtSetArg(args[n], XmNpacking, XmPACK_COLUMN); n++;
95     XtSetArg(args[n], XmNnumColumns, 2); n++;
96     XtSetArg(args[n], XmNentryAlignment, XmALIGNMENT_END); n++;
97     workArea = XmCreateWorkArea(messageBox, "workArea", args, n);
98     XtManageChild(workArea);
99
100     labelString = XmStringCreateLocalized("Invoke Action:");
101     n = 0;
102     XtSetArg(args[n], XmNlabelString, labelString); n++;
103     w = XmCreateLabel(workArea, "actionLabel", args, n);
104     XtManageChild(w);
105     XmStringFree(labelString);
106
107     labelString = XmStringCreateLocalized("On File:");
108     n = 0;
109     XtSetArg(args[n], XmNlabelString, labelString); n++;
110     w = XmCreateLabel(workArea, "fileLabel", args, n);
111     XtManageChild(w);
112     XmStringFree(labelString);
113
114     n = 0;
115     XtSetArg(args[n], XmNcolumns, 12); n++;
116     actionText = XmCreateTextField(workArea, "actionText", args, n);
117     XtManageChild(actionText);
118
119     n = 0;
120     XtSetArg(args[n], XmNcolumns, 12); n++;
121     fileText = XmCreateTextField(workArea, "fileText", args, n);
122     XtManageChild(fileText);
123 }
124
125 static void DbReloadProc(XtPointer cd)
126 {
127     /* Pick up any dynamic changes to the database files */
128     DtDbLoad();
129 }
130
131 static void InvokeActionCb(Widget w, XtPointer cd, XtPointer cb)
132 {
133     char *action;
134     char *file;
135
136     action = XmTextFieldGetString(actionText);
137
138     if (action == NULL) return;
139     if (strlen(action) == 0) {
140         XtFree(action);
141         return;
142     }
143
144     file = XmTextFieldGetString(fileText);
145
146     InvokeAction(action, file);
147
148     XtFree(action);
149     XtFree(file);
150
151     XmTextFieldSetString(actionText, "");
152     XmTextFieldSetString(fileText, "");
153
154     XmProcessTraversal(actionText, XmTRAVERSE_CURRENT);
155 }
156
157 static void InvokeAction(char *action, char *file)
158 {
159     DtActionArg *ap = NULL;
160     int nap = 0;
161     DtActionInvocationID actionId;
162   
163     /* If a file was specified, build the file argument list */
164
165 printf("%s(%s)\n",action,file);
166     if (file != NULL && strlen(file) != 0) {
167         ap = (DtActionArg*) XtCalloc(1, sizeof(DtActionArg));
168         ap[0].argClass = DtACTION_FILE;
169         ap[0].u.file.name = file;
170         nap = 1;
171     }
172
173     /* Invoke the specified action */
174
175     actionId = DtActionInvoke(shell,action,ap,nap,NULL,NULL,NULL,True,NULL,NULL);
176 }
177