Actual source code: select.c

  1: #define PETSC_DLL
 2:  #include petsc.h
 3:  #include petscsys.h

  7: /*@C
  8:      PetscPopUpSelect - Pops up a windows with a list of choices; allows one to be chosen

 10:      Collective on MPI_Comm

 12:      Input Parameters:
 13: +    comm - MPI communicator, all processors in communicator must call this but input 
 14:             from first communicator is the only one that is used
 15: .    machine - location to run popup program or PETSC_NULL
 16: .    title - text to display above choices
 17: .    n - number of choices
 18: -    choices - array of strings

 20:      Output Parameter:
 21: .    choice - integer indicating which one was selected

 23:      Level: developer

 25:      Notes:
 26:        Uses DISPLAY variable or -display option to determine where it opens the window

 28:        Currently this uses a file ~username/.popuptmp to pass the value back from the 
 29:        xterm; hence this program must share a common file system with the machine
 30:        parameter passed in below.

 32:    Concepts: popup
 33:    Concepts: user selection
 34:    Concepts: menu

 36: @*/
 37: PetscErrorCode  PetscPopUpSelect(MPI_Comm comm,char *machine,char *title,int n,char **choices,int *choice)
 38: {
 39:   PetscMPIInt    rank;
 40:   int            i,rows = n + 2;
 41:   size_t         cols,len;
 42:   char           buffer[2048],display[256],geometry[64];
 43:   FILE           *fp;

 47:   if (!title) SETERRQ(PETSC_ERR_ARG_NULL,"Must pass in a title line");
 48:   if (n < 1) SETERRQ(PETSC_ERR_ARG_WRONG,"Must pass in at least one selection");
 49:   if (n == 1) {*choice = 0; return(0);}

 51:   PetscStrlen(title,&cols);
 52:   for (i=0; i<n; i++) {
 53:     PetscStrlen(choices[i],&len);
 54:     cols = PetscMax(cols,len);
 55:   }
 56:   cols += 4;
 57:   sprintf(geometry," -geometry %dx%d ",(int)cols,rows);
 58:   PetscStrcpy(buffer,"xterm -bw 100 -bd blue +sb -display ");
 59:   PetscGetDisplay(display,128);
 60:   PetscStrcat(buffer,display);
 61:   PetscStrcat(buffer,geometry);
 62:   PetscStrcat(buffer," -e ${PETSC_DIR}/bin/popup ");

 64:   PetscStrcat(buffer,"\"");
 65:   PetscStrcat(buffer,title);
 66:   PetscStrcat(buffer,"\" ");
 67:   for (i=0; i<n; i++) {
 68:     PetscStrcat(buffer,"\"");
 69:     PetscStrcat(buffer,choices[i]);
 70:     PetscStrcat(buffer,"\" ");
 71:   }
 72: #if defined(PETSC_HAVE_POPEN)
 73:   PetscPOpen(comm,machine,buffer,"r",&fp);
 74:   PetscPClose(comm,fp);
 75:   MPI_Comm_rank(comm,&rank);
 76:   if (!rank) {
 77:     FILE *fd;

 79:     PetscFOpen(PETSC_COMM_SELF,"${HOMEDIRECTORY}/.popuptmp","r",&fd);
 80:     fscanf(fd,"%d",choice);
 81:     *choice -= 1;
 82:     if (*choice < 0 || *choice > n-1) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Selection %d out of range",*choice);
 83:     PetscPClose(PETSC_COMM_SELF,fd);
 84:   }
 85: #else
 86:   SETERRQ(PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
 87: #endif
 88:   MPI_Bcast(choice,1,MPI_INT,0,comm);
 89:   return(0);
 90: }