home *** CD-ROM | disk | FTP | other *** search
-
- The HASWIN window library.
- ===========================
- Copyright H.A. Shaw 1990.
- ===========================
-
- Examples of code fragments.
- ----------------------------
-
- Below is a simple user program that might form the basis of a
- RISC OS application program running under the HASWIN library. It is
- commented and variable names are such that it should be understandable.
- It only scrapes the surface of what can be done by informing HASWIN of the
- relationships between items such as icons, windows and menus.
-
- #include <haswin.h>
-
- /*
- * some global state variables
- */
- int started = HASWIN_FALSE;
- int xcoord = 0;
- int ycoord = 0;
-
- /*
- * definitions of the user routines that HASWIN will use
- */
- int savedata(char *name, buffer *buff) {
- /*
- * save the program's data to a file called "name" if
- * buff != 0. buff->i[10] is the filetype of "name"
- */
- if (!buff) {
- /*
- * we might have processing to do when the Filer
- * acknowledges the end of the load, such as removing
- * the Hourglass.
- */
- return(HASWIN_TRUE);
- }
- /*
- * save the data
- */
- return(HASWIN_TRUE);
- }
-
- int loaddata(char *name, buffer *buff) {
- /*
- * load the program's data from a file called "name" if
- * buff != 0. buff->i[10] is the filetype of "name"
- */
- if (!buff) {
- /*
- * we might have processing to do when the Filer
- * acknowledges the end of the load, such as removing
- * the Hourglass.
- */
- return(HASWIN_TRUE);
- }
- if (haswin_getfileoptions() & HASWIN_OPTION_ADD) {
- /*
- * add the data into that existing already
- */
- } else {
- /*
- * replace the existing data with the file
- */
- }
- return(HASWIN_TRUE);
- }
-
- /*
- * this routine is called to create menus. Since it is only attached
- * to one menu we don't use "mu" much. If we had attached this routine
- * to lots of menus then we could use "mu" to find out the window/icon
- * the menu is required for ("mu->wfrom" and "mu->ifrom" are the
- * required window and icon handles)
- */
- int makebarmenu(menu *mu, buffer *buff) {
-
- char str[256]; /* can be any length of course! */
- /*
- * put the text for the menu into "str". Can use anything
- * to create the string, sprintf() is usual
- *
- * menu looks like this...
- title <- Default colours, HASWIN calculates width/hieght
- Go -> <- "Go" has tick if started flag true
- Go Now?
- . Start <- "Start" selectable if started flag false
- . Stop <- "Stop" selectable if started flag true
- .
- X-coord ->
- Value <- title is white (colour 0) on black (colour 7)
- minimum width is 12 characters
- . X.XXX <- current X value, writable so X can be altered
- . Reset <- RED lettering (colour 11) on yellow (colour 9)
- Y-coord ->
- Value <- title is white (colour 0) on black (colour 7)
- minimum width is 12 characters
- . Y.YYY <- current Y value, writable so Y can be altered
- . Reset <- RED lettering (colour 11) on yellow (colour 9)
- *
- * See the layout of the sprintf() call below. It is the only
- * way of not getting completly confused!!
- */
-
- sprintf(str,
- "title.(Go Now?.%s:Start.%s:Stop)%s:Go.\
- (<12 &7 $0 Value.+16 :'%.3f'.&9 @11 :Reset):X-coord.\
- (<12 &7 $0 Value.+16 :'%.3f'.&9 @11 :Reset):Y-coord",
- ((started) ? "" : "!"), ((started) ? "!" : ""),
- ((started) ? "/" : ""),
- xcoord, ycoord);
-
- /*
- * create the menu in mu, from the string and then return
- * HASWIN_TRUE to allow the menu to open.
- */
- haswin_makemenu(mu, str, 0, 0, 0);
- return(HASWIN_TRUE);
- }
-
- /*
- * this routine is called to handle menu selections. Since it is only
- * attached to one menu we don't use "mu". If we had attached this
- * routine to lots of menus then we could use "mu" to find out the
- * window/icon the menu came from ("mu->wfrom" and "mu->ifrom" are the
- * required window and icon handles)
- */
- int dobarmenu(char *sel, menu *mu) {
-
- if (!strcmp(sel, "Go.Start")) {
- /* selection was "Start" from submenu along "Go" */
- started = HASWIN_TRUE;
- return(HASWIN_TRUE);
- } else if (!strcmp(sel, "Go.Stop")) {
- /* selection was "Stop" from submenu along "Go" */
- started = HASWIN_FALSE;
- return(HASWIN_TRUE);
- } else if (!strcmp(sel, "X-coord.Reset")) {
- /* note: we do this BEFORE "X-coord.<number>" */
- xcoord = 0;
- return(HASWIN_TRUE);
- } else if (!strncmp(sel, "X-coord.", 7)) {
- /* we got X-coord.something, assume a number (could check) */
- sel += 7;
- xcoord = atoi(sel);
- return(HASWIN_TRUE);
- } else if (!strcmp(sel, "Y-coord.Reset")) {
- /* note: we do this BEFORE "Y-coord.<number>" */
- ycoord = 0;
- return(HASWIN_TRUE);
- } else if (!strncmp(sel, "Y-coord.", 7)) {
- /* we got Y-coord.something, assume a number (could check) */
- sel += 7;
- ycoord = atoi(sel);
- return(HASWIN_TRUE);
- }
- /* got some unknown menu selection, just ignore it */
- return(HASWIN_FALSE);
- }
-
- main() {
- window *mywindow1, *mywindow2, *mywindow3, *mywindow4;
- icon *baricon;
- icon *ic; /* a temp for later on */
-
- /*
- * the user program is a task called "mytask".
- * It will respond to requests from the !Help application
- * It requires at least 384K to operate in.
- */
- if (!haswin_initialise("mytask", HASWIN_FLAGS_HELPFUL, 384*1024)) {
- haswin_errorbox("can't initialise");
- haswin_exit(1);
- }
-
- /*
- * create an information window for icons on the icon bar.
- * The program name is "task name".
- * The purpose is "Does things".
- * The author is "Me".
- * The version is "1.00".
- */
- haswin_setinfowindow("task name", "Does things", "Me", "1.00");
-
- /*
- * the program uses sprites in the file "MySprites"
- */
- if (!haswin_loadusersprites("MySprites")) {
- haswin_errorbox("can't load sprites");
- haswin_exit(1);
- }
-
- /*
- * the program uses some windows held in the template file
- * "MyTemplates"
- * The windows identifiers are ident1, ident2, help1, help2
- * All these windows use sprites help in the user area
- */
- if (!haswin_pushtemplate("MyTemplates")) {
- haswin_errorbox("can't load templates");
- haswin_exit(1);
- }
- mywindow1 = haswin_loadwindow("ident1", SPRITE_USER_AREA);
- mywindow2 = haswin_loadwindow("ident2", SPRITE_USER_AREA);
- mywindow3 = haswin_loadwindow("help1", SPRITE_USER_AREA);
- mywindow4 = haswin_loadwindow("help2", SPRITE_USER_AREA);
- if ((!mywindow1) || (!mywindow2) || (!mywindow3) || (!mywindow4)) {
- haswin_errorbox("can't load my windows from the template");
- haswin_exit(1);
- }
- haswin_poptemplate();
-
- /*
- * create an icon on the right hand side of the icon bar.
- * The icon is "QBFS:mysprite"
- * mysprite - the name of the sprite
- * S - a sprite
- * F - Filled background
- * B - a border
- * Q - add a "Quit" to its menu
- *
- * Open the window "mywindow1" on a SELECT on the icon
- * Create a menu for the icon...
- * use makebarmenu() as the menu creation routine
- * use dobarmenu() as the menu selection routine
- * There is no message routine
- * The window "mywindow3" displays help information for this
- * icon.
- * If a help request message comes in for the icon then reply
- * with the string "This is help for the !Help application".
- */
- myicon = haswin_createicon(ICONBAR_RIGHT, "QBFS:mysprite",
- forecolour, backcolour, 0 /*esg*/,
- xmin, ymin, xmax, ymax);
- if (!myicon) {
- haswin_errorbox("can't create my iconbar icon");
- haswin_exit(1);
- }
- myicon->window = mywindow1;
- myicon->menu = haswin_makemenu(0, 0, makebarmenu, dobarmenu, 0);
- myicon->help = mywindow3;
- myicon->helpmsg = "This is help for the !Help application";
-
- /*
- * The program can load and save files. The routines
- * "loaddata()" and "savedata()" will do the actual work.
- * There is a filetype "MyType" that is the default filetype
- * and the default file leaf name is "Default".
- */
- haswin_fileinitialise();
- haswin_setsavefileroutine(savedata, "Default", "MyType");
- haswin_setloadfileroutine(loaddata, "Default", "MyType");
-
- /*
- * The window "mywindow1" contains icons who's text is
- * "Direct Save" and "Direct Load". Find these and make HASWIN
- * open the file save or load windows when these icons are
- * clicked with the SELECT button of the mouse. Find the
- * icon who's text is "Next Window" in mywindow1 and make
- * HASWIN open mywindow2 when it is clicked with the SELECT
- * mouse button.
- */
- ic = haswin_findiconname(mywindow1, "Direct Save");
- ic->window = haswin_findwindowname("file_save");
- ic = haswin_findiconname(mywindow1, "Direct Load");
- ic->window = haswin_findwindowname("file_load");
- ic = haswin_findiconname(mywindow1, "Next Window");
- ic->window = mywindow2;
-
- /*
- * "mywindow2" has a pointer that is automatically selected
- * when the mouse goes into the window area. The pointer is
- * a sprite "my_pointer" and the active point is (5,5) within
- * the sprite.
- */
- mywindow2->pointer = haswin_makepointer("my_pointer", 5, 5);
-
- /*
- * now everything is ready we poll
- */
- for (;;) {
- haswin_poll();
- /*
- * we can put any processing here.
- */
- if (started) {
- /*
- * This piece of code is run only if the
- * flag "selected" is true. It is turned on
- * or off by menu selections we don't care
- * about.
- */
- }
- }
- }
-
-