Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

cmd_user.C

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr                                                                       
00003  *cr            (C) Copyright 1995-2008 The Board of Trustees of the           
00004  *cr                        University of Illinois                       
00005  *cr                         All Rights Reserved                        
00006  *cr                                                                   
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  * RCS INFORMATION:
00011  *
00012  *      $RCSfile: cmd_user.C,v $
00013  *      $Author: johns $        $Locker:  $             $State: Exp $
00014  *      $Revision: 1.22 $       $Date: 2008/03/27 19:36:51 $
00015  *
00016  ***************************************************************************
00017  * DESCRIPTION:
00018  *
00019  * Commands to allow the user to customize the pop-up menu.
00020  *
00021  ***************************************************************************/
00022 
00023 #include "config.h"
00024 #include "Mouse.h"
00025 #include "utilities.h"
00026 #include "DisplayDevice.h"
00027 #include "JString.h"
00028 #include "VMDApp.h"
00029 #include <ctype.h>  // for toupper/tolower
00030 #include <tcl.h>
00031 
00032 static int check_canonical_form(const char *s) {
00033   if (s[0] == 'A') {
00034     // could be a letter, 'Alt-', or 'Aux-'
00035     s++;
00036     if (*s == 0) {
00037       return TRUE;
00038     }
00039 
00040     // must be an Alt- or Aux-
00041     if (*s != 'l' && *s != 'u') {
00042       return FALSE;
00043     }
00044 
00045     if (*s == 'l') {
00046       s++;
00047       if (*s++ != 't') return FALSE;
00048       if (*s++ != '-') return FALSE;
00049     } else if (*s == 'u') {
00050       s++;
00051       if (*s++ != 'x') return FALSE;
00052       if (*s++ != '-') return FALSE;
00053     }
00054   }
00055 
00056   if (s[0] == 'C') {
00057     // could be a letter, or 'Control-'
00058     s++;
00059     if (*s == 0) {
00060       return TRUE;
00061     }
00062     // must be a Control
00063     if (*s++ != 'o') return FALSE;
00064     if (*s++ != 'n') return FALSE;
00065     if (*s++ != 't') return FALSE;
00066     if (*s++ != 'r') return FALSE;
00067     if (*s++ != 'o') return FALSE;
00068     if (*s++ != 'l') return FALSE;
00069     if (*s++ != '-') return FALSE;
00070   }
00071 
00072   // must be a single character
00073   if (s[0] == 0) return FALSE; // NULL, was invalid
00074 
00075   if (s[1] == 0) return TRUE; // was a valid single character
00076 
00077   return FALSE; // was otherwise invalid
00078 }
00079 
00080 int text_cmd_user(ClientData cd, Tcl_Interp *interp, int argc,
00081                             const char *argv[]) {
00082 
00083   VMDApp *app = (VMDApp *)cd;
00084 
00085   if(argc < 3) {
00086     Tcl_SetResult(interp, 
00087       (char *)
00088       "user list keys\n"
00089       "user print keys\n"
00090       "user add key <character> <command>",
00091       TCL_STATIC);
00092     return TCL_ERROR;
00093   }
00094 
00095   if(!strupncmp(argv[1], "add", CMDLEN)) {
00096     if (!strupncmp(argv[2], "key", CMDLEN)) {
00097       if(argc < 5)
00098         return TCL_ERROR;
00099       // check this is a valid string
00100       if (check_canonical_form(argv[3])) {
00101         const char *combstr = argv[4];
00102         const char *desc = NULL;
00103         if (argc > 5) desc = argv[5];
00104         int indx = app->userKeys.typecode(argv[3]);
00105         if (indx < 0) {
00106           app->userKeys.add_name(argv[3], stringdup(combstr));
00107         } else {
00108           delete [] app->userKeys.data(indx);
00109           app->userKeys.set_data(indx, stringdup(combstr));
00110         }
00111         if (desc) {
00112           indx = app->userKeyDesc.typecode(argv[3]);
00113           if (indx < 0) {
00114             app->userKeyDesc.add_name(argv[3], stringdup(desc));
00115           } else {
00116             delete [] app->userKeyDesc.data(indx);
00117             app->userKeys.set_data(indx, stringdup(desc));
00118           }
00119         }
00120       } else {
00121         Tcl_AppendResult(interp, "user key ", argv[3], " is not valid",
00122                          NULL);
00123         return TCL_ERROR;
00124       }
00125     }  else
00126       return TCL_ERROR;
00127 
00128   } else if(!strupncmp(argv[1], "list", CMDLEN)) {
00129     // return definitions of current items
00130     if (argc != 3) {
00131       return TCL_ERROR;
00132     }
00133     if (!strcmp(argv[2], "keys")) {
00134       int num = app->userKeys.num();
00135       for (int i=0; i<num; i++) {
00136         // return tuples of {keystroke command description}
00137         Tcl_AppendResult(interp, i==0?"":" ", "{", NULL);
00138         Tcl_AppendElement(interp,  app->userKeys.name(i));
00139         Tcl_AppendElement(interp, 
00140            (const char *) app->userKeys.data(i));
00141         int desc_typecode = app->userKeyDesc.typecode(app->userKeys.name(i));
00142         if (desc_typecode >= 0) {
00143           Tcl_AppendElement(interp,
00144              (const char *) app->userKeyDesc.data(i));
00145         } else {
00146           Tcl_AppendElement(interp, "");
00147         } 
00148         Tcl_AppendResult(interp, "}", NULL);
00149       }
00150       return TCL_OK;
00151     } else {
00152       return TCL_ERROR;
00153     }
00154     // will never get here
00155 
00156   } else if(!strupncmp(argv[1], "print", CMDLEN)) {
00157     // print out definitions of current items
00158     Tcl_AppendResult(interp, 
00159         "Keyboard shortcuts:\n",
00160         "-------------------\n", NULL);
00161     for (int i=0; i<app->userKeys.num(); i++) {
00162       const char *key = app->userKeys.name(i);
00163       Tcl_AppendResult(interp, "'", key, "' : ", app->userKeys.data(i), "\n",
00164           NULL);
00165       if (app->userKeyDesc.typecode(key) >= 0) {
00166         Tcl_AppendResult(interp, "     Description: ", 
00167             app->userKeyDesc.data(key), NULL);
00168       }
00169     }
00170   } else
00171     return TCL_ERROR;
00172     
00173   // if here, everything worked out ok
00174   return TCL_OK;
00175 }
00176 

Generated on Mon Oct 13 01:27:23 2008 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002