Graphical Interfaces: Dialog Boxes
Suppose you don't want to deal with command line interpretation, but you still want your program to be able to change the values of certain variables in an interactive way. You could simply program in a series printf/scanf lines to quiz the user about their preferences:
.
.
.
printf("Please enter the value of n: ");
scanf("%d", &n);
printf("Please enter the value of x: ");
scanf("%f", &x);
.
.
.
and so on, but this won't work well if your program is to be used as part of a pipeline (see the UNIX primer), for example using ther graphics program plot_data, since the questions and answers will get mixed up with the data stream.
A convenient alternative is to use a simple graphical interface which generates a dialog box, offering you the option of varying key parameters in your program. Our graphics package provides a number of easy-to-use tools for constructing and using such boxes. The simplest way to set the integer variable n and the float variable x (i.e. to perform the same effect as the above lines of code) using a dialog box is as follows:
/* Simple program to illustrate use of a dialog box */
main()
{
/* Define default values: */
int n = 0;
float x = 0.0;
/* Define contents of dialog window */
create_int_dialog_entry("n", &n);
create_float_dialog_entry("x", &x);
/* Create window with name "Setup" and top-left corner at (0,0) */
set_up_dialog("Setup", 0, 0);
/* Display the window and read the results */
read_dialog_window();
/* Print out the new values */
printf("n = %d, x = %f\n", n, x);
}
Compile this program using the alias Cgfx to link in all necessary libraries. The two create lines define the entries in the box and the variables to be associated with them, set_up_dialog names the box and defines its location. Finally, read_dialog_window pops up a window and allows you to change the values of the variables. When the program runs, you will see a box that looks something like this:
Modify the numbers shown, click "OK" (or just hit carriage return), and the changes are made. That's all there is to it! The great advantage of this approach is that it operates independently of the flow of data through stdin/stdout. In principle, you could even control the operation of every stage in a pipeline of many chained commands, using a separate dialog box for each.
1. A First Program
2. Let's Compute
3. Loops
4. Symbolic Constants
5. Conditionals
6. Pointers
7. Arrays
8. Character Arrays
9. I/O Capabilities
10. Functions
11. Command-line Arguments
12. Graphical Interfaces: Dialog Boxes
No comments:
Post a Comment