dtcalc: change from obsoleted MAXFLOAT to FLT_MAX from std C
[oweals/cde.git] / cde / examples / dtterm / term.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: term.c /main/3 1995/10/27 10:40:51 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
32 /*
33  * term.c
34  *
35  * Example code for DtTerm widget
36  *
37  * Create a terminal widget running a shell and provide
38  * push button shortcuts for shell commands. Regular shell
39  * commands can be entered also.
40  */
41
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include <Xm/MessageB.h>
46 #include <Xm/Frame.h>
47 #include <Xm/Label.h>
48 #include <Xm/PushB.h>
49 #include <Dt/Term.h>
50
51 static Widget toplevel;
52
53 static void CreateTerm(Widget, char *);
54 static void SendCommandCb(Widget, XtPointer, XtPointer);
55 static void AddCommandButton(Widget, char *, char *);
56
57 main(int argc, char **argv)
58 {
59     XtAppContext appContext;
60     Arg args[20];
61     int n;
62     XmString labelString;
63
64     Widget mainWindow, termContainer, termTitle;
65     
66     /* Initialize DtTerm widget library */
67
68     DtTermInitialize();
69
70     toplevel = XtAppInitialize(&appContext, "Term", NULL, 0, &argc, argv,
71                                 NULL, NULL, 0);
72
73     n = 0;
74     XtSetArg(args[n], XmNdialogType, XmDIALOG_TEMPLATE); n++;
75     mainWindow = XmCreateMessageBox(toplevel, "mainWindow", args, n);
76     XtManageChild(mainWindow);
77
78     n = 0;
79     XtSetArg(args[n], XmNmarginWidth, 10); n++;
80     XtSetArg(args[n], XmNmarginHeight, 10); n++;
81     termContainer = XmCreateFrame(mainWindow, "termContainer", args, n);
82     XtManageChild(termContainer);
83
84     labelString = XmStringCreateLocalized("DtTerm with date and time shortcuts");
85     n = 0;
86     XtSetArg(args[n], XmNchildType, XmFRAME_TITLE_CHILD); n++;
87     XtSetArg(args[n], XmNlabelString, labelString); n++;
88     termTitle = XmCreateLabel(termContainer, "termTitle", args, n);
89     XtManageChild(termTitle);
90     XmStringFree(labelString);
91
92     /* Create the terminal widget */
93
94     CreateTerm(termContainer, "/bin/sh");
95
96     /* Add shortcut buttons to the message box */
97
98     AddCommandButton(mainWindow, "Today", "date\n");
99     AddCommandButton(mainWindow, "Month", "cal\n");
100     AddCommandButton(mainWindow, "1994", "clear;cal 1994\n");
101     AddCommandButton(mainWindow, "1995", "clear;cal 1995\n");
102     AddCommandButton(mainWindow, "1996", "clear;cal 1996\n");
103     AddCommandButton(mainWindow, "1997", "clear;cal 1997\n");
104     AddCommandButton(mainWindow, "1998", "clear;cal 1998\n");
105     AddCommandButton(mainWindow, "1999", "clear;cal 1999\n");
106     AddCommandButton(mainWindow, "2000", "clear;cal 2000\n");
107
108     XtRealizeWidget(toplevel);
109     XtAppMainLoop(appContext);
110 }
111
112
113 /*
114  * Create a DtTerm
115  */
116
117 static void CreateTerm(Widget parent, char *cmd)
118 {
119     Widget term;
120     Arg args[20];
121     int n;
122     
123     /*
124      * Create a DtTerm widget.
125      * Pass the command to execute.
126      * Configure the window to fit a calendar year.
127      */
128
129     n = 0;
130     XtSetArg(args[n], DtNsubprocessCmd, cmd); n++;
131     XtSetArg(args[n], DtNrows, 46); n++;
132     XtSetArg(args[n], DtNcolumns, 80); n++;
133     term = DtCreateTerm(parent, "term", args, n);
134     XtManageChild(term);
135 }
136
137 static void AddCommandButton(Widget parent, char *label, char *cmd)
138 {
139     XmString labelString;
140     Arg args[1];
141     Widget button;
142
143     /* Create a pushbutton which will send a command to the terminal */
144
145     labelString = XmStringCreateLocalized(label);
146     XtSetArg(args[0], XmNlabelString, labelString);
147     button = XmCreatePushButton(parent, label, args, 1);
148     XtManageChild(button);
149     XmStringFree(labelString);
150     XtAddCallback(button, XmNactivateCallback, SendCommandCb, (XtPointer)cmd);
151 }
152
153 static void SendCommandCb(Widget w, XtPointer cd, XtPointer cb)
154 {
155     Widget term = XtNameToWidget(toplevel, "*term");
156     unsigned char *cmd = (unsigned char*)cd;
157
158     /* send the pushbutton command to the terminal widget */
159
160     DtTermSubprocSend(term, cmd, strlen((char*)cmd));
161 }
162