Actual source code: select.c
petsc-3.7.3 2016-08-01
2: #include <petscsys.h> /*I "petscsys.h" I*/
6: /*@C
7: PetscPopUpSelect - Pops up a windows with a list of choices; allows one to be chosen
9: Collective on MPI_Comm
11: Input Parameters:
12: + comm - MPI communicator, all processors in communicator must call this but input
13: from first communicator is the only one that is used
14: . machine - location to run popup program or NULL
15: . title - text to display above choices
16: . n - number of choices
17: - choices - array of strings
19: Output Parameter:
20: . choice - integer indicating which one was selected
22: Level: developer
24: Notes:
25: Uses DISPLAY variable or -display option to determine where it opens the window
27: Currently this uses a file ~username/.popuptmp to pass the value back from the
28: xterm; hence this program must share a common file system with the machine
29: parameter passed in below.
31: Concepts: popup
32: Concepts: user selection
33: Concepts: menu
35: @*/
36: PetscErrorCode PetscPopUpSelect(MPI_Comm comm,const char *machine,const char *title,int n,const char **choices,int *choice)
37: {
38: PetscMPIInt rank;
39: int i,rows = n + 2;
40: size_t cols,len;
41: char buffer[2048],display[256],geometry[64];
42: FILE *fp;
46: if (!title) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Must pass in a title line");
47: if (n < 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Must pass in at least one selection");
48: if (n == 1) {*choice = 0; return(0);}
50: PetscStrlen(title,&cols);
51: for (i=0; i<n; i++) {
52: PetscStrlen(choices[i],&len);
53: cols = PetscMax(cols,len);
54: }
55: cols += 4;
56: sprintf(geometry," -geometry %dx%d ",(int)cols,rows);
57: PetscStrcpy(buffer,"xterm -bw 100 -bd blue +sb -display ");
58: PetscGetDisplay(display,128);
59: PetscStrcat(buffer,display);
60: PetscStrcat(buffer,geometry);
61: PetscStrcat(buffer," -e ${PETSC_DIR}/bin/popup ");
63: PetscStrcat(buffer,"\"");
64: PetscStrcat(buffer,title);
65: PetscStrcat(buffer,"\" ");
66: for (i=0; i<n; i++) {
67: PetscStrcat(buffer,"\"");
68: PetscStrcat(buffer,choices[i]);
69: PetscStrcat(buffer,"\" ");
70: }
71: #if defined(PETSC_HAVE_POPEN)
72: PetscPOpen(comm,machine,buffer,"r",&fp);
73: PetscPClose(comm,fp,NULL);
74: MPI_Comm_rank(comm,&rank);
75: if (!rank) {
76: FILE *fd;
78: PetscFOpen(PETSC_COMM_SELF,"${HOMEDIRECTORY}/.popuptmp","r",&fd);
79: if (fscanf(fd,"%d",choice) != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fscanf() could not read numeric choice");
80: *choice -= 1;
81: if (*choice < 0 || *choice > n-1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Selection %d out of range",*choice);
82: PetscFClose(PETSC_COMM_SELF,fd);
83: }
84: #else
85: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
86: #endif
87: MPI_Bcast(choice,1,MPI_INT,0,comm);
88: return(0);
89: }