home *** CD-ROM | disk | FTP | other *** search
/ Archive Magazine 1996 / ARCHIVE_96.iso / discs / mag_discs / volume_6 / issue_06 / cfrombasic / c / cexample
Text File  |  1993-01-24  |  6KB  |  172 lines

  1. /* C Example version 1.00 by Thomas Down */
  2.  
  3. /* Tell system which parts of libraries you want to use */
  4.                    
  5. /* From standard library */
  6.  
  7. #include <stdlib.h>   /* Standard functions */
  8.  
  9. /* For RISC OS Libraries */
  10.  
  11. #include "wimp.h"     /* Low level WIMP access, equivalent to SYS's in Basic */
  12. #include "event.h"    /* Event handling */
  13. #include "win.h"      /* Window class, and more event handling */
  14. #include "dbox.h"     /* Dialog boxes */
  15. #include "menu.h"     /* Menu creation */
  16. #include "res.h"      /* Resource handling */       
  17. #include "sprite.h"   /* Sprite handling */
  18. #include "resspr.h"   /* Sprite resource handling */
  19. #include "template.h" /* Window resource handling */
  20. #include "baricon.h"  /* To put an icon on the iconbar! */
  21. #include "wimpt.h"    /* Wimp task handling */
  22. #include "werr.h"     /* WIMP error boxes */
  23.  
  24. /* This is a list of 'Function prototypes', telling the compiler about functions later */
  25. /* in the program. Doing this means that C does not have to be a two pass compiler */
  26.  
  27. void init(void);
  28. void bar_click(wimp_i i);
  29. void barmenu_select(void *,char *);
  30. void box_click(dbox,void *);
  31.  
  32. /* These are the declarations for global variables (variables which can be used by all the */
  33. /* differant functions). All variables in C must be declared before they can be used */
  34.  
  35. menu barmen;
  36. dbox box;
  37. BOOL boxon;
  38. BOOL hello=FALSE;
  39.  
  40. /* Main program, the equivalent of the first, non-procedure, part of a BASIC program */
  41.  
  42. int main(void)
  43. {
  44.   /* Initialise */
  45.   
  46.   init();
  47.   
  48.   /* Polling loop */
  49.  
  50.   for(;;)
  51.     event_process();
  52.     
  53.   return(0); /* Included for correctness. Never needed */
  54. }
  55.  
  56. /* Initialise */
  57.  
  58. void init(void)
  59.   /* Initialise library modules */
  60.   
  61.   wimpt_init("C Example"); /* Do WIMP_Initialise then initialise window module */
  62.   res_init("cexam");       /* Inform system that resources can be found at <cexam$dir>. */ 
  63.   resspr_init();           /* Load Sprites file */
  64.   template_init();         /* Load Templates file */
  65.   dbox_init();
  66.   
  67.   /* The next line installs an icon on the iconbar. !cáexample is the sprite name, */
  68.   /* (int)resspr_area() gives a pointer to the sprite pool, */
  69.   /* bar_click is the name of the function to call when the iconbar is clicked on */
  70.   
  71.   baricon("!cáexample",(int)resspr_area(),bar_click);
  72.   
  73.   /* The next two lines create a menu and attach it to the icon bar. barmenu_select */
  74.   /* is the function to call when an option is chosen from the menu. */
  75.   
  76.   barmen=menu_new("C Example",">Info,Quit");
  77.   event_attachmenu(win_ICONBAR,barmen,barmenu_select,0);
  78.   
  79.   /* Create a dialog box from the template "mainbox" */
  80.   
  81.   box=dbox_new("mainbox");
  82.   boxon=FALSE;
  83. }
  84.  
  85. /* This function is called when the iconbar icon is clicked. It is passed a handle for */
  86. /* the iconbar icon. It is not used, so the line i=i prevents the compiler complaining */
  87.  
  88. void bar_click(wimp_i i)
  89. {
  90.   i=i;
  91.   
  92.   if(boxon==FALSE)
  93.   {
  94.     dbox_showstatic(box);                /* Show dialog box 'box' */
  95.     dbox_eventhandler(box,box_click,0);  /* Install an event handler for it */
  96.     boxon=TRUE;
  97.   }
  98. }
  99.  
  100. /* This function is called whenever a selection is made from the iconbar menu. */
  101. /* char hit[], means it takes an array of chars (small integers) */
  102. /* C uses [] instead of () for arrays */
  103.  
  104. void barmenu_select(void *v,char hit[])
  105. {     
  106.   dbox proginfo;
  107.   
  108.   v=v;
  109.  
  110.   switch(hit[0]) {
  111.     case 1:
  112.       /* This creates the information dialog box, displays it, waits, then removes it */ 
  113.       proginfo=dbox_new("progInfo");
  114.       dbox_show(proginfo);
  115.       dbox_fillin(proginfo);
  116.       dbox_dispose(&proginfo);
  117.       break;
  118.     case 2:
  119.       /* The next line is C's equivalent of END in a Basic program. Note that no */
  120.       /* Wimp_CloseDown is used. If wimpt_init() is used to start the task, an exit */
  121.       /* handler is defined which automatically tidies up before the program shuts down */
  122.       exit(0); 
  123.       break;
  124.   }
  125. }
  126.  
  127. /* This function is called is the mouse is clicked in the dialog box */
  128.  
  129. void box_click(dbox d,void *v)
  130. {     
  131.   char string[20];
  132.   dbox_field df;
  133.   
  134.   v=v;
  135.   df=dbox_get(d);   /* Find out which icon has been clicked on */
  136.   
  137.   switch(df) {
  138.     case -1:
  139.       /* Field -1 is always the close icon, so hide the dialog box */
  140.       dbox_hide(d);
  141.       boxon=FALSE;
  142.       break;
  143.     case 1:
  144.       hello=!hello;
  145.       dbox_setnumeric(d,1,hello);
  146.       break;
  147.     case 2:
  148.       /* Get the text from icon 0 into char array 'string' */
  149.       dbox_getfield(d,0,string,16);
  150.       
  151.       /* werr(0,...) displays a non-fatal error or message. It is used in a similar way */
  152.       /* to the C PRINT function printf(). The first argument is werr() specific and is */
  153.       /* a flag specifying whether the error is fatal. The next argument is the so called */
  154.       /* formatting string. It is the string which is octually output. Variables are */
  155.       /* merged into the format string by CONVERSION CODES, which are a '%' followed by */
  156.       /* a letter representing variable type, 's' for string, 'd' for decimal integer etc. */
  157.       /* Conversion codes are replaced by any subsequent arguments to the function. In */
  158.       /* this case, there are two conversion arguments. The second is simply a char array */
  159.       /* (string). The first is more interesting, and is an example of the useful */
  160.       /* x ? y : z operator in C. Basically, the expression 'x' is evaluated. It it is */
  161.       /* TRUE, the whole sequence is equal to 'y', otherwise it is equal to 'z' */
  162.       
  163.       werr(0,"box on, %s, %s",(hello==TRUE) ? "Hello" : "Goodbye",string);
  164.       break;
  165.   }
  166. }
  167.  
  168.  
  169.  
  170.  
  171.