home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / clib / progs / haswinlib / info / example < prev    next >
Encoding:
Text File  |  1991-02-04  |  11.6 KB  |  300 lines

  1.  
  2.                       The HASWIN window library.
  3.                      ===========================
  4.                       Copyright H.A. Shaw 1990.
  5.                      ===========================
  6.  
  7. Examples of code fragments.
  8. ----------------------------
  9.  
  10.         Below is a simple user program that might form the basis of a
  11. RISC OS application program running under the HASWIN library.  It is
  12. commented and variable names are such that it should be understandable.
  13. It only scrapes the surface of what can be done by informing HASWIN of the
  14. relationships between items such as icons, windows and menus.
  15.  
  16. #include <haswin.h>
  17.  
  18. /*
  19.  *      some global state variables
  20.  */
  21. int     started = HASWIN_FALSE;
  22. int     xcoord = 0;
  23. int     ycoord = 0;
  24.  
  25. /*
  26.  *      definitions of the user routines that HASWIN will use
  27.  */
  28. int savedata(char *name, buffer *buff) {
  29.         /*
  30.          *      save the program's data to a file called "name" if
  31.          *      buff != 0.  buff->i[10] is the filetype of "name"
  32.          */
  33.         if (!buff) {
  34.                 /*
  35.                  *      we might have processing to do when the Filer
  36.                  *      acknowledges the end of the load, such as removing
  37.                  *      the Hourglass.
  38.                  */
  39.                 return(HASWIN_TRUE);
  40.         }
  41.         /*
  42.          *      save the data
  43.          */
  44.         return(HASWIN_TRUE);
  45. }
  46.  
  47. int loaddata(char *name, buffer *buff) {
  48.         /*
  49.          *      load the program's data from a file called "name" if
  50.          *      buff != 0.  buff->i[10] is the filetype of "name"
  51.          */
  52.         if (!buff) {
  53.                 /*
  54.                  *      we might have processing to do when the Filer
  55.                  *      acknowledges the end of the load, such as removing
  56.                  *      the Hourglass.
  57.                  */
  58.                 return(HASWIN_TRUE);
  59.         }
  60.         if (haswin_getfileoptions() & HASWIN_OPTION_ADD) {
  61.                 /*
  62.                  *      add the data into that existing already
  63.                  */
  64.         } else {
  65.                 /*
  66.                  *      replace the existing data with the file
  67.                  */
  68.         }
  69.         return(HASWIN_TRUE);
  70. }
  71.  
  72. /*
  73.  *      this routine is called to create menus.  Since it is only attached
  74.  *      to one menu we don't use "mu" much.  If we had attached this routine
  75.  *      to lots of menus then we could use "mu" to find out the window/icon
  76.  *      the menu is required for ("mu->wfrom" and "mu->ifrom" are the
  77.  *      required window and icon handles)
  78.  */
  79. int makebarmenu(menu *mu, buffer *buff) {
  80.  
  81.         char    str[256];       /* can be any length of course! */
  82.         /*
  83.          *      put the text for the menu into "str".  Can use anything
  84.          *      to create the string, sprintf() is usual
  85.          *
  86.          *      menu looks like this...
  87.  title                <- Default colours, HASWIN calculates width/hieght
  88.   Go      ->          <- "Go" has tick if started flag true
  89.              Go Now?
  90.     .         Start   <- "Start" selectable if started flag false
  91.     .         Stop    <- "Stop" selectable if started flag true
  92.     .
  93. X-coord   ->
  94.               Value   <- title is white (colour 0) on black (colour 7)
  95.                          minimum width is 12 characters
  96.     .         X.XXX   <- current X value, writable so X can be altered
  97.     .         Reset   <- RED lettering (colour 11) on yellow (colour 9)
  98. Y-coord   ->
  99.               Value   <- title is white (colour 0) on black (colour 7)
  100.                          minimum width is 12 characters
  101.     .         Y.YYY   <- current Y value, writable so Y can be altered
  102.     .         Reset   <- RED lettering (colour 11) on yellow (colour 9)
  103.          *
  104.          *      See the layout of the sprintf() call below.  It is the only
  105.          *      way of not getting completly confused!!
  106.          */
  107.  
  108.         sprintf(str,
  109. "title.(Go Now?.%s:Start.%s:Stop)%s:Go.\
  110. (<12 &7 $0 Value.+16 :'%.3f'.&9 @11 :Reset):X-coord.\
  111. (<12 &7 $0 Value.+16 :'%.3f'.&9 @11 :Reset):Y-coord",
  112.                ((started) ? "" : "!"), ((started) ? "!" : ""),
  113.                ((started) ? "/" : ""),
  114.                xcoord, ycoord);
  115.  
  116.         /*
  117.          *     create the menu in mu, from the string and then return
  118.          *     HASWIN_TRUE to allow the menu to open.
  119.          */
  120.         haswin_makemenu(mu, str, 0, 0, 0);
  121.         return(HASWIN_TRUE);
  122. }
  123.  
  124. /*
  125.  *      this routine is called to handle menu selections.  Since it is only
  126.  *      attached to one menu we don't use "mu".  If we had attached this
  127.  *      routine to lots of menus then we could use "mu" to find out the
  128.  *      window/icon the menu came from ("mu->wfrom" and "mu->ifrom" are the
  129.  *      required window and icon handles)
  130.  */
  131. int dobarmenu(char *sel, menu *mu) {
  132.  
  133.         if (!strcmp(sel, "Go.Start")) {
  134.                 /* selection was "Start" from submenu along "Go" */
  135.                 started = HASWIN_TRUE;
  136.                 return(HASWIN_TRUE);
  137.         } else if (!strcmp(sel, "Go.Stop")) {
  138.                 /* selection was "Stop" from submenu along "Go" */
  139.                 started = HASWIN_FALSE;
  140.                 return(HASWIN_TRUE);
  141.         } else if (!strcmp(sel, "X-coord.Reset")) {
  142.                 /* note: we do this BEFORE "X-coord.<number>" */
  143.                 xcoord = 0;
  144.                 return(HASWIN_TRUE);
  145.         } else if (!strncmp(sel, "X-coord.", 7)) {
  146.                 /* we got X-coord.something, assume a number (could check) */
  147.                 sel += 7;
  148.                 xcoord = atoi(sel);
  149.                 return(HASWIN_TRUE);
  150.         } else if (!strcmp(sel, "Y-coord.Reset")) {
  151.                 /* note: we do this BEFORE "Y-coord.<number>" */
  152.                 ycoord = 0;
  153.                 return(HASWIN_TRUE);
  154.         } else if (!strncmp(sel, "Y-coord.", 7)) {
  155.                 /* we got Y-coord.something, assume a number (could check) */
  156.                 sel += 7;
  157.                 ycoord = atoi(sel);
  158.                 return(HASWIN_TRUE);
  159.         }
  160.         /* got some unknown menu selection, just ignore it */
  161.         return(HASWIN_FALSE);
  162. }
  163.  
  164. main() {
  165.         window  *mywindow1, *mywindow2, *mywindow3, *mywindow4;
  166.         icon    *baricon;
  167.         icon    *ic;            /* a temp for later on */
  168.  
  169.         /*
  170.          *      the user program is a task called "mytask".
  171.          *      It will respond to requests from the !Help application
  172.          *      It requires at least 384K to operate in.
  173.          */
  174.         if (!haswin_initialise("mytask", HASWIN_FLAGS_HELPFUL, 384*1024)) {
  175.                 haswin_errorbox("can't initialise");
  176.                 haswin_exit(1);
  177.         }
  178.  
  179.         /*
  180.          *      create an information window for icons on the icon bar.
  181.          *      The program name is "task name".
  182.          *      The purpose is      "Does things".
  183.          *      The author is       "Me".
  184.          *      The version is      "1.00".
  185.          */
  186.         haswin_setinfowindow("task name", "Does things", "Me", "1.00");
  187.  
  188.         /*
  189.          *       the program uses sprites in the file "MySprites"
  190.          */
  191.         if (!haswin_loadusersprites("MySprites")) {
  192.                 haswin_errorbox("can't load sprites");
  193.                 haswin_exit(1);
  194.         }
  195.  
  196.         /*
  197.          *      the program uses some windows held in the template file
  198.          *      "MyTemplates"
  199.          *      The windows identifiers are ident1, ident2, help1, help2
  200.          *      All these windows use sprites help in the user area
  201.          */
  202.         if (!haswin_pushtemplate("MyTemplates")) {
  203.                 haswin_errorbox("can't load templates");
  204.                 haswin_exit(1);
  205.         }
  206.         mywindow1 = haswin_loadwindow("ident1", SPRITE_USER_AREA);
  207.         mywindow2 = haswin_loadwindow("ident2", SPRITE_USER_AREA);
  208.         mywindow3 = haswin_loadwindow("help1", SPRITE_USER_AREA);
  209.         mywindow4 = haswin_loadwindow("help2", SPRITE_USER_AREA);
  210.         if ((!mywindow1) || (!mywindow2) || (!mywindow3) || (!mywindow4)) { 
  211.                 haswin_errorbox("can't load my windows from the template");
  212.                 haswin_exit(1);
  213.         }
  214.         haswin_poptemplate();
  215.  
  216.         /*
  217.          *       create an icon on the right hand side of the icon bar.  
  218.          *       The icon is "QBFS:mysprite"
  219.          *                         mysprite - the name of the sprite
  220.          *                       S          - a sprite
  221.          *                      F           - Filled background
  222.          *                     B            - a border
  223.          *                    Q             - add a "Quit" to its menu
  224.          *
  225.          *       Open the window "mywindow1" on a SELECT on the icon
  226.          *       Create a menu for the icon...
  227.          *               use makebarmenu() as the menu creation routine
  228.          *               use   dobarmenu() as the menu selection routine
  229.          *               There is no message routine
  230.          *       The window "mywindow3" displays help information for this
  231.          *       icon.
  232.          *       If a help request message comes in for the icon then reply
  233.          *       with the string "This is help for the !Help application".
  234.          */
  235.         myicon = haswin_createicon(ICONBAR_RIGHT, "QBFS:mysprite",
  236.                                    forecolour, backcolour, 0 /*esg*/,
  237.                                    xmin, ymin, xmax, ymax);
  238.         if (!myicon) {
  239.                 haswin_errorbox("can't create my iconbar icon");
  240.                 haswin_exit(1);
  241.         }
  242.         myicon->window = mywindow1;
  243.         myicon->menu = haswin_makemenu(0, 0, makebarmenu, dobarmenu, 0);
  244.         myicon->help = mywindow3;
  245.         myicon->helpmsg = "This is help for the !Help application";
  246.  
  247.         /*
  248.          *      The program can load and save files.  The routines
  249.          *      "loaddata()" and "savedata()" will do the actual work.
  250.          *      There is a filetype "MyType" that is the default filetype
  251.          *      and the default file leaf name is "Default".
  252.          */
  253.         haswin_fileinitialise();
  254.         haswin_setsavefileroutine(savedata, "Default", "MyType");
  255.         haswin_setloadfileroutine(loaddata, "Default", "MyType");
  256.  
  257.         /*
  258.          *      The window "mywindow1" contains icons who's text is
  259.          *      "Direct Save" and "Direct Load".  Find these and make HASWIN
  260.          *      open the file save or load windows when these icons are
  261.          *      clicked with the SELECT button of the mouse.  Find the
  262.          *      icon who's text is "Next Window" in mywindow1 and make
  263.          *      HASWIN open mywindow2 when it is clicked with the SELECT
  264.          *      mouse button.
  265.          */
  266.         ic = haswin_findiconname(mywindow1, "Direct Save");
  267.         ic->window = haswin_findwindowname("file_save");
  268.         ic = haswin_findiconname(mywindow1, "Direct Load");
  269.         ic->window = haswin_findwindowname("file_load");
  270.         ic = haswin_findiconname(mywindow1, "Next Window");
  271.         ic->window = mywindow2;
  272.  
  273.         /*
  274.          *      "mywindow2" has a pointer that is automatically selected
  275.          *      when the mouse goes into the window area.  The pointer is
  276.          *      a sprite "my_pointer" and the active point is (5,5) within
  277.          *      the sprite.
  278.          */
  279.         mywindow2->pointer = haswin_makepointer("my_pointer", 5, 5);
  280.  
  281.         /*
  282.          *      now everything is ready we poll
  283.          */
  284.         for (;;) {
  285.                 haswin_poll();
  286.                 /*
  287.                  *      we can put any processing here.
  288.                  */
  289.                 if (started) {
  290.                         /*
  291.                          *      This piece of code is run only if the
  292.                          *      flag "selected" is true.  It is turned on
  293.                          *      or off by menu selections we don't care
  294.                          *      about.
  295.                          */
  296.                 }
  297.         }
  298. }
  299.  
  300.