home *** CD-ROM | disk | FTP | other *** search
- /* C Example version 1.00 by Thomas Down */
-
- /* Tell system which parts of libraries you want to use */
-
- /* From standard library */
-
- #include <stdlib.h> /* Standard functions */
-
- /* For RISC OS Libraries */
-
- #include "wimp.h" /* Low level WIMP access, equivalent to SYS's in Basic */
- #include "event.h" /* Event handling */
- #include "win.h" /* Window class, and more event handling */
- #include "dbox.h" /* Dialog boxes */
- #include "menu.h" /* Menu creation */
- #include "res.h" /* Resource handling */
- #include "sprite.h" /* Sprite handling */
- #include "resspr.h" /* Sprite resource handling */
- #include "template.h" /* Window resource handling */
- #include "baricon.h" /* To put an icon on the iconbar! */
- #include "wimpt.h" /* Wimp task handling */
- #include "werr.h" /* WIMP error boxes */
-
- /* This is a list of 'Function prototypes', telling the compiler about functions later */
- /* in the program. Doing this means that C does not have to be a two pass compiler */
-
- void init(void);
- void bar_click(wimp_i i);
- void barmenu_select(void *,char *);
- void box_click(dbox,void *);
-
- /* These are the declarations for global variables (variables which can be used by all the */
- /* differant functions). All variables in C must be declared before they can be used */
-
- menu barmen;
- dbox box;
- BOOL boxon;
- BOOL hello=FALSE;
-
- /* Main program, the equivalent of the first, non-procedure, part of a BASIC program */
-
- int main(void)
- {
- /* Initialise */
-
- init();
-
- /* Polling loop */
-
- for(;;)
- event_process();
-
- return(0); /* Included for correctness. Never needed */
- }
-
- /* Initialise */
-
- void init(void)
- {
- /* Initialise library modules */
-
- wimpt_init("C Example"); /* Do WIMP_Initialise then initialise window module */
- res_init("cexam"); /* Inform system that resources can be found at <cexam$dir>. */
- resspr_init(); /* Load Sprites file */
- template_init(); /* Load Templates file */
- dbox_init();
-
- /* The next line installs an icon on the iconbar. !c example is the sprite name, */
- /* (int)resspr_area() gives a pointer to the sprite pool, */
- /* bar_click is the name of the function to call when the iconbar is clicked on */
-
- baricon("!c example",(int)resspr_area(),bar_click);
-
- /* The next two lines create a menu and attach it to the icon bar. barmenu_select */
- /* is the function to call when an option is chosen from the menu. */
-
- barmen=menu_new("C Example",">Info,Quit");
- event_attachmenu(win_ICONBAR,barmen,barmenu_select,0);
-
- /* Create a dialog box from the template "mainbox" */
-
- box=dbox_new("mainbox");
- boxon=FALSE;
- }
-
- /* This function is called when the iconbar icon is clicked. It is passed a handle for */
- /* the iconbar icon. It is not used, so the line i=i prevents the compiler complaining */
-
- void bar_click(wimp_i i)
- {
- i=i;
-
- if(boxon==FALSE)
- {
- dbox_showstatic(box); /* Show dialog box 'box' */
- dbox_eventhandler(box,box_click,0); /* Install an event handler for it */
- boxon=TRUE;
- }
- }
-
- /* This function is called whenever a selection is made from the iconbar menu. */
- /* char hit[], means it takes an array of chars (small integers) */
- /* C uses [] instead of () for arrays */
-
- void barmenu_select(void *v,char hit[])
- {
- dbox proginfo;
-
- v=v;
-
- switch(hit[0]) {
- case 1:
- /* This creates the information dialog box, displays it, waits, then removes it */
- proginfo=dbox_new("progInfo");
- dbox_show(proginfo);
- dbox_fillin(proginfo);
- dbox_dispose(&proginfo);
- break;
- case 2:
- /* The next line is C's equivalent of END in a Basic program. Note that no */
- /* Wimp_CloseDown is used. If wimpt_init() is used to start the task, an exit */
- /* handler is defined which automatically tidies up before the program shuts down */
- exit(0);
- break;
- }
- }
-
- /* This function is called is the mouse is clicked in the dialog box */
-
- void box_click(dbox d,void *v)
- {
- char string[20];
- dbox_field df;
-
- v=v;
- df=dbox_get(d); /* Find out which icon has been clicked on */
-
- switch(df) {
- case -1:
- /* Field -1 is always the close icon, so hide the dialog box */
- dbox_hide(d);
- boxon=FALSE;
- break;
- case 1:
- hello=!hello;
- dbox_setnumeric(d,1,hello);
- break;
- case 2:
- /* Get the text from icon 0 into char array 'string' */
- dbox_getfield(d,0,string,16);
-
- /* werr(0,...) displays a non-fatal error or message. It is used in a similar way */
- /* to the C PRINT function printf(). The first argument is werr() specific and is */
- /* a flag specifying whether the error is fatal. The next argument is the so called */
- /* formatting string. It is the string which is octually output. Variables are */
- /* merged into the format string by CONVERSION CODES, which are a '%' followed by */
- /* a letter representing variable type, 's' for string, 'd' for decimal integer etc. */
- /* Conversion codes are replaced by any subsequent arguments to the function. In */
- /* this case, there are two conversion arguments. The second is simply a char array */
- /* (string). The first is more interesting, and is an example of the useful */
- /* x ? y : z operator in C. Basically, the expression 'x' is evaluated. It it is */
- /* TRUE, the whole sequence is equal to 'y', otherwise it is equal to 'z' */
-
- werr(0,"box on, %s, %s",(hello==TRUE) ? "Hello" : "Goodbye",string);
- break;
- }
- }
-
-
-
-
-