Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / examples / dtterm / term.c
1 /* $XConsortium: term.c /main/3 1995/10/27 10:40:51 rswiston $ */
2 /*
3  * (c) Copyright 1993, 1994 Hewlett-Packard Company     
4  * (c) Copyright 1993, 1994 International Business Machines Corp.
5  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.
6  * (c) Copyright 1993, 1994 Novell, Inc.
7  */
8
9
10 /*
11  * term.c
12  *
13  * Example code for DtTerm widget
14  *
15  * Create a terminal widget running a shell and provide
16  * push button shortcuts for shell commands. Regular shell
17  * commands can be entered also.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <Xm/MessageB.h>
24 #include <Xm/Frame.h>
25 #include <Xm/Label.h>
26 #include <Xm/PushB.h>
27 #include <Dt/Term.h>
28
29 static Widget toplevel;
30
31 static void CreateTerm(Widget, char *);
32 static void SendCommandCb(Widget, XtPointer, XtPointer);
33 static void AddCommandButton(Widget, char *, char *);
34
35 main(int argc, char **argv)
36 {
37     XtAppContext appContext;
38     Arg args[20];
39     int n;
40     XmString labelString;
41
42     Widget mainWindow, termContainer, termTitle;
43     
44     /* Initialize DtTerm widget library */
45
46     DtTermInitialize();
47
48     toplevel = XtAppInitialize(&appContext, "Term", NULL, 0, &argc, argv,
49                                 NULL, NULL, 0);
50
51     n = 0;
52     XtSetArg(args[n], XmNdialogType, XmDIALOG_TEMPLATE); n++;
53     mainWindow = XmCreateMessageBox(toplevel, "mainWindow", args, n);
54     XtManageChild(mainWindow);
55
56     n = 0;
57     XtSetArg(args[n], XmNmarginWidth, 10); n++;
58     XtSetArg(args[n], XmNmarginHeight, 10); n++;
59     termContainer = XmCreateFrame(mainWindow, "termContainer", args, n);
60     XtManageChild(termContainer);
61
62     labelString = XmStringCreateLocalized("DtTerm with date and time shortcuts");
63     n = 0;
64     XtSetArg(args[n], XmNchildType, XmFRAME_TITLE_CHILD); n++;
65     XtSetArg(args[n], XmNlabelString, labelString); n++;
66     termTitle = XmCreateLabel(termContainer, "termTitle", args, n);
67     XtManageChild(termTitle);
68     XmStringFree(labelString);
69
70     /* Create the terminal widget */
71
72     CreateTerm(termContainer, "/bin/sh");
73
74     /* Add shortcut buttons to the message box */
75
76     AddCommandButton(mainWindow, "Today", "date\n");
77     AddCommandButton(mainWindow, "Month", "cal\n");
78     AddCommandButton(mainWindow, "1994", "clear;cal 1994\n");
79     AddCommandButton(mainWindow, "1995", "clear;cal 1995\n");
80     AddCommandButton(mainWindow, "1996", "clear;cal 1996\n");
81     AddCommandButton(mainWindow, "1997", "clear;cal 1997\n");
82     AddCommandButton(mainWindow, "1998", "clear;cal 1998\n");
83     AddCommandButton(mainWindow, "1999", "clear;cal 1999\n");
84     AddCommandButton(mainWindow, "2000", "clear;cal 2000\n");
85
86     XtRealizeWidget(toplevel);
87     XtAppMainLoop(appContext);
88 }
89
90
91 /*
92  * Create a DtTerm
93  */
94
95 static void CreateTerm(Widget parent, char *cmd)
96 {
97     Widget term;
98     Arg args[20];
99     int n;
100     
101     /*
102      * Create a DtTerm widget.
103      * Pass the command to execute.
104      * Configure the window to fit a calendar year.
105      */
106
107     n = 0;
108     XtSetArg(args[n], DtNsubprocessCmd, cmd); n++;
109     XtSetArg(args[n], DtNrows, 46); n++;
110     XtSetArg(args[n], DtNcolumns, 80); n++;
111     term = DtCreateTerm(parent, "term", args, n);
112     XtManageChild(term);
113 }
114
115 static void AddCommandButton(Widget parent, char *label, char *cmd)
116 {
117     XmString labelString;
118     Arg args[1];
119     Widget button;
120
121     /* Create a pushbutton which will send a command to the terminal */
122
123     labelString = XmStringCreateLocalized(label);
124     XtSetArg(args[0], XmNlabelString, labelString);
125     button = XmCreatePushButton(parent, label, args, 1);
126     XtManageChild(button);
127     XmStringFree(labelString);
128     XtAddCallback(button, XmNactivateCallback, SendCommandCb, (XtPointer)cmd);
129 }
130
131 static void SendCommandCb(Widget w, XtPointer cd, XtPointer cb)
132 {
133     Widget term = XtNameToWidget(toplevel, "*term");
134     unsigned char *cmd = (unsigned char*)cd;
135
136     /* send the pushbutton command to the terminal widget */
137
138     DtTermSubprocSend(term, cmd, strlen((char*)cmd));
139 }
140