actions.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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: 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. #include <Xm/XmAll.h>
  31. #include <Dt/Dt.h>
  32. #include <Dt/Action.h>
  33. #define ApplicationClass "Dtaction"
  34. static Widget shell;
  35. static XtAppContext appContext;
  36. static Widget actionText;
  37. static Widget fileText;
  38. static void CreateWidgets(Widget);
  39. static void InvokeActionCb(Widget, XtPointer, XtPointer);
  40. static void InvokeAction(char*, char*);
  41. static void DbReloadProc(XtPointer);
  42. void main(int argc, char **argv)
  43. {
  44. Arg args[20];
  45. int n=0;
  46. int numArgs = 0;
  47. shell = XtAppInitialize(&appContext , ApplicationClass, NULL, 0,
  48. &argc, argv, NULL, args, n);
  49. CreateWidgets(shell);
  50. if (DtInitialize(XtDisplay(shell), shell, argv[0], ApplicationClass)==False) {
  51. /* DtInitialize() has already logged an appropriate error msg */
  52. exit(-1);
  53. }
  54. /* Load the filetype/action databases */
  55. DtDbLoad();
  56. /* Notice changes to the database without needing to restart application */
  57. DtDbReloadNotify(DbReloadProc, NULL);
  58. XtRealizeWidget(shell);
  59. XmProcessTraversal(actionText, XmTRAVERSE_CURRENT);
  60. XtAppMainLoop(appContext);
  61. }
  62. static void CreateWidgets(Widget shell)
  63. {
  64. Widget messageBox, workArea, w;
  65. Arg args[20];
  66. int n;
  67. XmString labelString;
  68. labelString = XmStringCreateLocalized("Invoke");
  69. n = 0;
  70. XtSetArg(args[n], XmNdialogType, XmDIALOG_TEMPLATE); n++;
  71. XtSetArg(args[n], XmNokLabelString, labelString); n++;
  72. messageBox = XmCreateMessageBox(shell, "messageBox", args, n);
  73. XtManageChild(messageBox);
  74. XmStringFree(labelString);
  75. XtAddCallback(messageBox, XmNokCallback, InvokeActionCb, NULL);
  76. n = 0;
  77. XtSetArg(args[n], XmNorientation, XmVERTICAL); n++;
  78. XtSetArg(args[n], XmNpacking, XmPACK_COLUMN); n++;
  79. XtSetArg(args[n], XmNnumColumns, 2); n++;
  80. XtSetArg(args[n], XmNentryAlignment, XmALIGNMENT_END); n++;
  81. workArea = XmCreateWorkArea(messageBox, "workArea", args, n);
  82. XtManageChild(workArea);
  83. labelString = XmStringCreateLocalized("Invoke Action:");
  84. n = 0;
  85. XtSetArg(args[n], XmNlabelString, labelString); n++;
  86. w = XmCreateLabel(workArea, "actionLabel", args, n);
  87. XtManageChild(w);
  88. XmStringFree(labelString);
  89. labelString = XmStringCreateLocalized("On File:");
  90. n = 0;
  91. XtSetArg(args[n], XmNlabelString, labelString); n++;
  92. w = XmCreateLabel(workArea, "fileLabel", args, n);
  93. XtManageChild(w);
  94. XmStringFree(labelString);
  95. n = 0;
  96. XtSetArg(args[n], XmNcolumns, 12); n++;
  97. actionText = XmCreateTextField(workArea, "actionText", args, n);
  98. XtManageChild(actionText);
  99. n = 0;
  100. XtSetArg(args[n], XmNcolumns, 12); n++;
  101. fileText = XmCreateTextField(workArea, "fileText", args, n);
  102. XtManageChild(fileText);
  103. }
  104. static void DbReloadProc(XtPointer cd)
  105. {
  106. /* Pick up any dynamic changes to the database files */
  107. DtDbLoad();
  108. }
  109. static void InvokeActionCb(Widget w, XtPointer cd, XtPointer cb)
  110. {
  111. char *action;
  112. char *file;
  113. action = XmTextFieldGetString(actionText);
  114. if (action == NULL) return;
  115. if (strlen(action) == 0) {
  116. XtFree(action);
  117. return;
  118. }
  119. file = XmTextFieldGetString(fileText);
  120. InvokeAction(action, file);
  121. XtFree(action);
  122. XtFree(file);
  123. XmTextFieldSetString(actionText, "");
  124. XmTextFieldSetString(fileText, "");
  125. XmProcessTraversal(actionText, XmTRAVERSE_CURRENT);
  126. }
  127. static void InvokeAction(char *action, char *file)
  128. {
  129. DtActionArg *ap = NULL;
  130. int nap = 0;
  131. DtActionInvocationID actionId;
  132. /* If a file was specified, build the file argument list */
  133. printf("%s(%s)\n",action,file);
  134. if (file != NULL && strlen(file) != 0) {
  135. ap = (DtActionArg*) XtCalloc(1, sizeof(DtActionArg));
  136. ap[0].argClass = DtACTION_FILE;
  137. ap[0].u.file.name = file;
  138. nap = 1;
  139. }
  140. /* Invoke the specified action */
  141. actionId = DtActionInvoke(shell,action,ap,nap,NULL,NULL,NULL,True,NULL,NULL);
  142. }