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: /*@C
6: PetscPopUpSelect - Pops up a windows with a list of choices; allows one to be chosen
8: Collective on MPI_Comm
10: Input Parameters:
11: + comm - MPI communicator, all processors in communicator must call this but input
12: from first communicator is the only one that is used
13: . machine - location to run popup program or PETSC_NULL
14: . title - text to display above choices
15: . n - number of choices
16: - choices - array of strings
18: Output Parameter:
19: . choice - integer indicating which one was selected
21: Level: developer
23: Notes:
24: Uses DISPLAY variable or -display option to determine where it opens the window
26: Currently this uses a file ~username/.popuptmp to pass the value back from the
27: xterm; hence this program must share a common file system with the machine
28: parameter passed in below.
30: Concepts: popup
31: Concepts: user selection
32: Concepts: menu
34: @*/
35: int PetscPopUpSelect(MPI_Comm comm,char *machine,char *title,int n,char **choices,int *choice)
36: {
37: int i,ierr,rank,rows = n + 2,cols,len;
38: char buffer[2048],display[128],geometry[64];
39: FILE *fp;
42: if (!title) SETERRQ(1,"Must pass in a title line");
43: if (n < 1) SETERRQ(1,"Must pass in at least one selection");
44: if (n == 1) {*choice = 0; return(0);}
46: PetscStrlen(title,&cols);
47: for (i=0; i<n; i++) {
48: PetscStrlen(choices[i],&len);
49: cols = PetscMax(cols,len);
50: }
51: cols += 4;
52: sprintf(geometry," -geometry %dx%d ",cols,rows);
53: PetscStrcpy(buffer,"xterm -bw 100 -bd blue +sb -display ");
54: PetscGetDisplay(display,128);
55: PetscStrcat(buffer,display);
56: PetscStrcat(buffer,geometry);
57: PetscStrcat(buffer," -e ${PETSC_DIR}/bin/popup ");
59: PetscStrcat(buffer,""");
60: PetscStrcat(buffer,title);
61: PetscStrcat(buffer,"" ");
62: for (i=0; i<n; i++) {
63: PetscStrcat(buffer,""");
64: PetscStrcat(buffer,choices[i]);
65: PetscStrcat(buffer,"" ");
66: }
67: PetscPOpen(comm,machine,buffer,"r",&fp);
68: PetscPClose(comm,fp);
70: MPI_Comm_rank(comm,&rank);
71: if (!rank) {
72: FILE *fd;
74: PetscFOpen(PETSC_COMM_SELF,"${HOMEDIRECTORY}/.popuptmp","r",&fd);
75: fscanf(fd,"%d",choice);
76: *choice -= 1;
77: if (*choice < 0 || *choice > n-1) SETERRQ1(1,"Selection %d out of range",*choice);
78: PetscFClose(PETSC_COMM_SELF,fd);
79: }
80: MPI_Bcast(choice,1,MPI_INT,0,comm);
82: return(0);
83: }