ROVLib Tutorial - Loading© Jason Tribbeck 2002 |
![]() |
In this tutorial, we will be waiting for a DrawFile to be dropped on the canvas or the icon.
/** * Loading.c - Waiting for a DrawFile */ #include <stdlib.h> #include <string.h> #include <stdio.h> #include <ctype.h> #include "kernel.h" #include "main.h" #include "canvas.h" #include "menu.h" #include "baricon.h" #include "extras.h" #include "draw.h" void click(int icn); void choose(menu m, int item); void menu_click(canvas c); void redraw(canvas c, int x0, int y0, int x1, int y1); /* Prototype the load event handler */ void load(canvas c, char *filename, int type, int size); static canvas c; static menu iconm; int main(int argc, char **argv) { canvas c_proginfo; main_init("My Application"); canvas_init(); menu_init(); c = canvas_create(640, 480, "My Application"); canvas_addhandler(c, CANVAS_REDRAWHANDLER, redraw); /* Attach the load event handler */ canvas_addhandler(c, CANVAS_LOADHANDLER, load); canvas_colour(c, 0); canvas_make(c); baricon("file_ff8", click); canvas_addhandler(CANVAS_ICONBAR, CANVAS_MENUHANDLER, menu_click); /* Also attach the load event handler to the icon bar */ canvas_addhandler(CANVAS_ICONBAR, CANVAS_LOADHANDLER, load); c_proginfo = create_infobox("My Application", "An example ROVLib application", "© Jason Tribbeck", "1.00 (14 Feb 2002)"); iconm = menu_create("Menu test", 2, choose, NULL); menu_add(iconm, "Info"); menu_add(iconm, "Quit"); menu_attachpane(iconm, 0, c_proginfo); canvas_opencentre(c); main_poll(); } void click(int icn) { canvas_reopen(c); } void menu_click(canvas c) { menu_displayiconbar(iconm); } void choose(menu m, int item) { switch(item) { case 1 : exit(0); break; } } void redraw(canvas c, int x0, int y0, int x1, int y1) { draw_setcolour(0x00000000); draw_move(0, 0); draw_line(640, 480); draw_setcolour(0x0000ff00); draw_move(0, 480); draw_line(640, 0); draw_setcolour(0xff000000); draw_circlefill(320, 240, 15); } void load(canvas c, char *filename, int type, int size) { if(type == 0xaff) { swerr(FALSE, "I can load '%s' - it's a DrawFile", filename); } else { swerr(FALSE, "I cannot load '%s' as it is not a DrawFile", filename); } }
We attach a load event handler to both the canvas and the icon bar. This function
(called "load
") checks the file type being dragged onto it, and
produces an error box saying that it can either load the file (if it is a DrawFile)
or that it cannot load the file (if it is something else).
Dropping a DrawFile will produce an error box saying "I can load
'ADFS::HardDisc4.$.DrawFile' - it's a DrawFile
". If you drop another file
type (or even directory), then you will get "I cannot load
'ADFS::HardDisc4.$.SpriteFile' as it is not a DrawFile
".
You have seen how a load event handler works, and how to check the file type. You should also have seen that you can attach the same handler function to different canvasses. This allows you to have several canvasses open, each one using common code with different data. An efficient way to do this will come in a later tutorial.