Actual source code: select.c
petsc-3.8.4 2018-03-24
2: #include <petscsys.h>
4: /*@C
5: PetscPopUpSelect - Pops up a windows with a list of choices; allows one to be chosen
7: Collective on MPI_Comm
9: Input Parameters:
10: + comm - MPI communicator, all processors in communicator must call this but input
11: from first communicator is the only one that is used
12: . machine - location to run popup program or NULL
13: . title - text to display above choices
14: . n - number of choices
15: - choices - array of strings
17: Output Parameter:
18: . choice - integer indicating which one was selected
20: Level: developer
22: Notes:
23: Uses DISPLAY variable or -display option to determine where it opens the window
25: Currently this uses a file ~username/.popuptmp to pass the value back from the
26: xterm; hence this program must share a common file system with the machine
27: parameter passed in below.
29: Concepts: popup
30: Concepts: user selection
31: Concepts: menu
33: @*/
34: PetscErrorCode PetscPopUpSelect(MPI_Comm comm,const char *machine,const char *title,int n,const char **choices,int *choice)
35: {
36: PetscMPIInt rank;
37: int i,rows = n + 2;
38: size_t cols,len;
39: char buffer[2048],display[256],geometry[64];
40: FILE *fp;
44: if (!title) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Must pass in a title line");
45: if (n < 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"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 ",(int)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: #if defined(PETSC_HAVE_POPEN)
70: PetscPOpen(comm,machine,buffer,"r",&fp);
71: PetscPClose(comm,fp,NULL);
72: MPI_Comm_rank(comm,&rank);
73: if (!rank) {
74: FILE *fd;
76: PetscFOpen(PETSC_COMM_SELF,"${HOMEDIRECTORY}/.popuptmp","r",&fd);
77: if (fscanf(fd,"%d",choice) != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fscanf() could not read numeric choice");
78: *choice -= 1;
79: if (*choice < 0 || *choice > n-1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Selection %d out of range",*choice);
80: PetscFClose(PETSC_COMM_SELF,fd);
81: }
82: #else
83: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
84: #endif
85: MPI_Bcast(choice,1,MPI_INT,0,comm);
86: return(0);
87: }