term.c 4.7 KB

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