|
|
|
| GUI Clinic Home |
||
| What Else Can the GUI Subsystem Do? Message Boxes | Graphics Mode Chooser Dialog | File Chooser Dialog Concluding Remarks |
||
Previous: Extending the System What Else Can the GUI Subsystem Do? Well, we're almost done. There are just a few miscellaneous things left to discuss. Much of this is paraphrased from the documentation. extern DIALOG *active_dialog extern MENU *active_menu The GUI also provides a few helper functions that make life easier for its users. I'll just list them here: int do_dialog(DIALOG *dialog, int focus_obj) int popup_dialog(DIALOG *dialog, int focus_obj) int broadcast_dialog(int msg, int c) int dialog_message(DIALOG *dialog, int msg, int c, int
*obj) int gui_textout(BITMAP *bmp, unsigned char *s, int x, int y, int
color, int center) int gui_strlen(unsigned char *s) void centre_dialog(DIALOG *dialog) void set_dialog_color(DIALOG *dialog, int fg, int bg) int find_dialog_focus(DIALOG *dialog) The following functions provide lower-level access to the GUI's dialog-playing functionality. You can use them if you want to integrate dialogs into your method of controlling your program. DIALOG_PLAYER *init_dialog(DIALOG *dialog, int
focus_obj) int update_dialog(DIALOG_PLAYER *player) int shutdown_dialog(DIALOG_PLAYER *player) Using these functions, you can create your own dialog player. The documentation provides a simple example: int my_do_dialog(DIALOG *dialog, int focus_obj) {
void *player = init_dialog(dialog, focus_obj);
while (update_dialog(player))
;
return shutdown_dialog(player);
}
A few helper functions are cool enough to merit their own discussions. They are the message boxes, the graphics mode chooser, and the file selector.
All GUIs support some sort of message boxes, and Allegro's GUI isn't any different. There are two functions for providing feedback or getting user input via message boxes. int alert(char *s1, *s2, *s3, *b1, *b2, int c1, c2) int alert3(char *s1, *s2, *s3, *b1, *b2, *b3, int c1,
c2) For example, say you're trying to access a file, but it isn't found. You could do something like the following: sprintf(err_msg, "Error opening %s!", file_name);
ret = alert(err_msg, "What will you do?", NULL,
"Abort", "Retry", 0, 0);
if (ret == 1)
do_abort();
else
do_retry();
These dialogs aren't too flexible. Their main fault is that they can only display three lines of text at most. If you really find that lacking, you can always create your own.
Allegro supports many different video resolutions and color depths. Often, it is practicle to let the user choose what video mode a program should run in. Creating a dialog full of all the possible choices would be rather daunting. Thankfully, Allegro comes with a prepackaged mode-selection dialog. int gfx_mode_select(int *card, int *w, int *h)
The gfx_mode_select_ex() function reads the parameters when it initializes, so you can suggest default values if you like. Here's an example: int c, w, h, depth;
int ret, mode_failure;
/* supply defaults */
c = GFX_AUTODETECT;
w = 800;
h = 600;
depth = 24;
/* ask the user */
ret = gfx_mode_select_ex(&c, &w, &h, &depth);
if (ret) {
/* try to set the mode */
set_color_depth(depth);
mode_failure = set_gfx_mode(c, w, h, 0, 0);
/* if it didn't work */
if (mode_failure) {
allegro_exit();
fprintf(stderr, "Couldn't set mode: %s",
allegro_error);
exit(1);
}
}
Make sure you always check the return value from these functions (and set_gfx_mode() as well)!
Finally, we come to the file chooser dialog. This provides an easy way for Allegro programs to provide file names for loading or saving files. int file_select(char *message, char *path, char *ext) The filename chosen is returned in the path string, so there had better be enough room. Make it at least 80 characters long. Here's an example: char path[] = "c:/djgpp/allegro/examples";
/* show only .PCX, .BMP, and .TGA files */
ret = file_select("Save As...", path, "PCX;BMP;TGA");
if (ret)
save_file(path);
else
do_not_save_file();
One of the main problems with this (and the gfx_mode_select dialog) is that it only uses the default Allegro GUI objects. There is currently no way to hook into the dialogs and change their appearances, aside from chopping the code out of the Allegro source and using your own GUI objects.
Well, you've survivied the tutorial. I hope it wasn't too boring and verbose. I started out with the idea that this would be a real "clinic" of sorts. Hence the name of the tutorial. But, I never got around to giving Doctor Inanis a voice, and he pretty much ended up on the title page and stayed there. He never did anything. I suppose he's like the Beatles' Sergeant Pepper that way. Oh, well. I like his picture. If you have any questions, comments, suggestions, or the like, please contact me. The tutorial in earnest is over, but as a bonus, I've take the three GUI examples from the Allegro distribution and annotated them. Sometimes, just having the source in front of you isn't enough. Hopefully, these appendices will make things clearer. Again, thanks for reading, and Happy Programming! Next: Annotated Example 13 |