Actual source code: select.c

  1: /*$Id: select.c,v 1.10 2001/03/23 23:20:45 balay Exp $*/
 2:  #include petsc.h
 3:  #include petscsys.h

  5: #undef __FUNCT__  
  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: int PetscPopUpSelect(MPI_Comm comm,char *machine,char *title,int n,char **choices,int *choice)
 38: {
 39:   int  i,ierr,rank,rows = n + 2,cols,len;
 40:   char buffer[2048],display[128],geometry[64];
 41:   FILE *fp;

 44:   if (!title) SETERRQ(1,"Must pass in a title line");
 45:   if (n < 1) SETERRQ(1,"Must pass in at least one selection");
 46:   if (n == 1) {*choice = 0; return(0);}

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

 61:   PetscStrcat(buffer,""");
 62:   PetscStrcat(buffer,title);
 63:   PetscStrcat(buffer,"" ");
 64:   for (i=0; i<n; i++) {
 65:     PetscStrcat(buffer,""");
 66:     PetscStrcat(buffer,choices[i]);
 67:     PetscStrcat(buffer,"" ");
 68:   }
 69:   PetscPOpen(comm,machine,buffer,"r",&fp);
 70:   PetscPClose(comm,fp);

 72:   MPI_Comm_rank(comm,&rank);
 73:   if (!rank) {
 74:     FILE *fd;

 76:     PetscFOpen(PETSC_COMM_SELF,"${HOMEDIRECTORY}/.popuptmp","r",&fd);
 77:     fscanf(fd,"%d",choice);
 78:     *choice -= 1;
 79:     if (*choice < 0 || *choice > n-1) SETERRQ1(1,"Selection %d out of range",*choice);
 80:     PetscFClose(PETSC_COMM_SELF,fd);
 81:   }
 82:   MPI_Bcast(choice,1,MPI_INT,0,comm);

 84:   return(0);
 85: }