ROVLib Tutorial - A canvas

© Jason Tribbeck 2002

Creating a canvas

In this tutorial, we will be creating a canvas (which is ROVLib speak for window), and making the application quit when the canvas is closed. We will also be modifying the icon bar click function so that the canvas is opened at the top of the WIMP stack when the icon is clicked on.

The source code

/**
 * Canvas.c - Create a canvas (ROVLib speak for window)
 */

/* Include 4 standard C header files */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>

/* Include the RISC OS kernel header file */
#include "kernel.h"

/* Include the ROVLib header files */
#include "main.h"
#include "canvas.h"
#include "baricon.h"
#include "extras.h"

/* Prototype the click function */
void click(int icn);

/* Prototype the canvas close function */
void close(canvas c);

/* A variable to hold the canvas */
static canvas c;

int main(int argc, char **argv) {
  /* Initialise the ROVLib system */
  main_init("Canvas test");

  /* Initialise the ROVLib canvas core */
  canvas_init();

  /* Create the canvas */
  c = canvas_create(640, 480, "An example canvas");

  /* Add the close handler for the canvas */
  canvas_addhandler(c, CANVAS_CLOSEHANDLER, close);

  /* Make the canvas as a window */
  canvas_make(c);

  /* Create an icon on the icon bar */
  baricon("file_ff8", click);

  /* Open the canvas in the centre of the screen */
  canvas_opencentre(c);

  /* Poll forever (or at least until told to quit!) */
  main_poll();
}

/* Actually perform the click function */
void click(int icn) {
  canvas_reopen(c);
}

/* Actually perform the close function */
void close(canvas c) {
  exit(0);
}

Once again, all important changes have been highlighted in red.

Creating a canvas

Creating a canvas is usually performed in the following steps:

  1. Create the canvas, giving its size and title
  2. Modify the canvas state (such as creating scroll bars)
  3. Add handlers for various canvas events
  4. Make the canvas into a window

In this case, we are not performing step 2, as we are creating a very basic canvas. In addition, the only canvas event we are interested in is the close event.

The canvas we have created is 640x480 OS units in size, and has a title of "An example canvas". The close event handler is simply called "close", and once we've added the event handler, we make the canvas into a WIMP window.

After creating the icon on the icon bar (as before), we open the window in the centre of the screen, and then start polling.

The icon bar click handler has been altered to reopen the canvas. Note that the canvas_reopen function will open the given canvas at the top of the WIMP stack.

Running the application.

Running the !Canvas Obey file will open the canvas on the screen, as well as display the icon on the icon bar. The canvas will look like:

As ROVLib performs common tedious operations for you, you will notice that you can drag the canvas around the screen - even you haven't written any code to do this!

If you open a window in front of the canvas, you can simply click on the icon on the icon bar to bring the canvas back to the front. This is similar to other applications, and it is showing that we are beginning to produce application-standard code.

Closing the window will cause the application to exit. Note that there is no warning.

Epilogue

You have seen the following in the tutorial:

  1. How to create canvasses
  2. How to add event handlers to canvasses
  3. How to open canvasses

You have also seen that ROVLib performs many of the standard functions that applications require, such as the movement of windows. As an experiment, you can try commenting out the line that registers the canvas close event handler. This will show you that ROVLib will close the window as well. Note that you will have to use the Task Manager in order to kill the application!