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

cmd_menu.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 #include <string.h>
00010 #include <ctype.h>
00011 #include <stdlib.h>
00012 #include <tcl.h>
00013 #include "config.h"
00014 #include "utilities.h"
00015 #include "VMDApp.h"
00016 
00017 #ifdef VMDTK
00018 #include "VMDTkMenu.h"
00019 #endif
00020 
00021 #define TCL_HELP 99 //internal code to generate error AND display menu command help
00022 
00023 int text_cmd_menu(ClientData cd, Tcl_Interp *interp, int argc,
00024                      const char *argv[]) {
00025 
00026   VMDApp *app = (VMDApp *)cd;
00027   int retval = TCL_OK;
00028 
00029   // make sure a menu was named
00030   if (argc < 2) retval = TCL_HELP;
00031 
00032   if (argc == 2 && !strupncmp(argv[1], "list", CMDLEN)) {
00033       // return a list of the available menus
00034       for (int i=0; i<app->num_menus(); i++) 
00035         Tcl_AppendElement(interp,  app->menu_name(i));
00036   }
00037 #ifdef VMDTK
00038   else if (argc > 1 && !strupncmp(argv[1], "tk", CMDLEN)) {
00039 #ifndef MACVMD
00040     if ((argc == 5 || argc == 6 ) && !strupncmp(argv[2], "add", CMDLEN)) {
00041       VMDMenu *menu = new VMDTkMenu(argv[3], argv[4], app, interp);
00042       if (!app->add_menu(menu)) {
00043         delete menu;
00044         char buf[50];
00045         sprintf(buf, "Unable to add menu %s.\n", argv[3]);
00046         Tcl_SetResult(interp, buf, TCL_VOLATILE);
00047         retval = TCL_ERROR;
00048       } else {
00049         // tell VMD that this is a menu extension
00050         if (argc==6) app->menu_add_extension(argv[3],argv[5]);
00051         else app->menu_add_extension(argv[3],argv[3]);
00052   
00053         // tell Tcl that a new menu extension has been added
00054         Tcl_SetVar(interp, "vmd_menu_extension", argv[3], TCL_GLOBAL_ONLY);
00055       }
00056     } else if ((argc == 5 || argc == 6 ) && !strupncmp(argv[2], "register", CMDLEN)) {
00057       VMDTkMenu *menu = new VMDTkMenu(argv[3], NULL, app, interp);
00058       menu->register_proc(argv[4]);
00059       if (!app->add_menu(menu)) {
00060         delete menu;
00061         char buf[50];
00062         sprintf(buf, "Unable to add menu %s\n", argv[3]);
00063         Tcl_SetResult(interp, buf, TCL_VOLATILE);
00064         retval = TCL_ERROR;
00065       } else {
00066         // tell VMD that this is a menu extension
00067         if (argc==6) app->menu_add_extension(argv[3],argv[5]);
00068         else app->menu_add_extension(argv[3],argv[3]);
00069   
00070         // tell Tcl that a new menu extension has been added
00071         Tcl_SetVar(interp, "vmd_menu_extension", argv[3], TCL_GLOBAL_ONLY);
00072       }
00073     } else if (argc == 4 && !strupncmp(argv[2], "remove", CMDLEN)) {
00074       if (!app->remove_menu(argv[3])) {
00075         char buf[50];
00076         sprintf(buf, "Unable to remove menu %s\n", argv[3]);
00077         Tcl_SetResult(interp, buf, TCL_VOLATILE);
00078         retval = TCL_ERROR;
00079       }
00080       else app->menu_remove_extension(argv[3]);
00081     }
00082     else retval = TCL_HELP;
00083 #else
00084     /* MACVMD just eats it, and does nothing presently */
00085 #endif
00086   }
00087 #endif
00088   else if (argc == 4 && !strupncmp(argv[2], "selectmol", CMDLEN)) {
00089     // undocumented command for internal use only!
00090     int ind;
00091     if (Tcl_GetInt(interp, argv[3], &ind) != TCL_OK) retval = TCL_HELP;
00092     else app->menu_select_mol(argv[1], ind);
00093   }
00094   else if(argc == 3) {
00095     if(!strupncmp(argv[2],"on",CMDLEN))
00096       app->menu_show(argv[1], 1);
00097     else if (!strupncmp(argv[2],"off",CMDLEN))
00098       app->menu_show(argv[1], 0);
00099     else if (!strupncmp(argv[2],"loc",CMDLEN)) {
00100       int x, y;
00101       if (app->menu_location(argv[1], x, y)) {
00102         char buf[20];
00103         sprintf(buf, "%d %d", x, y);
00104         Tcl_SetResult(interp, buf, TCL_VOLATILE);
00105       } 
00106       else {
00107         Tcl_AppendResult(interp, "menu loc: menu '", argv[1], 
00108           "' does not exist.", NULL);
00109         retval = TCL_ERROR;
00110       }
00111     } 
00112     else if (!strupncmp(argv[2], "status", CMDLEN))
00113       Tcl_AppendResult(interp, app->menu_status(argv[1]) ? "on" : "off", NULL);
00114     else  retval = TCL_HELP;
00115   }
00116   else if (argc == 5 && !strupncmp(argv[2],"move",CMDLEN))
00117     app->menu_move(argv[1], atoi(argv[3]), atoi(argv[4]));
00118   else
00119     retval = TCL_HELP;
00120   
00121   if (retval == TCL_HELP) {
00122     Tcl_SetResult(interp, 
00123       (char *) "Usage:\n"
00124       "\tmenu list                 -- returns list of available menus\n"
00125       "\tmenu <name> on            -- turn menu with given name on\n"
00126       "\tmenu <name> off           -- turn menu with given name off\n"
00127       "\tmenu <name> status         -- returns 'on' or 'off'\n"
00128       "\tmenu <name> loc           -- returns current position of menu\n"
00129       "\tmenu <name> move x y      -- move menu to given position\n"
00130       "\tmenu tk add <name> <tk window> [<menu path>]\n"
00131       "\t      -- add Tk menu to Extensions\n"
00132       "\tmenu tk register <name> <procname> [<menu path>]\n" 
00133       "\t      -- same as 'add', but <procname> returns tk window handle.\n"
00134       "\tmenu tk remove <name> -- remove menu from Extensions\n"
00135       "The Tk 'menu' command is also available.",
00136       TCL_STATIC);
00137     retval = TCL_ERROR;
00138   }
00139  
00140   return retval;
00141 }
00142 

Generated on Thu Jul 24 01:27:05 2008 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002