Step 3 - Writing your own Callbacks

It is also possible to write your own callbacks in GSL.

A callback written in GSL must have three arguments.

  • The first argument is a string argument which will contain the name of the object generating the callback.
  • The second argument will contain the callback data (if it has been specified). It can be a string or a float.
  • The third argument will contain a float specifying the reason the callback was made. Each GUI object has a fixed number of reasons that could generate a callback. For example, a button could be activated (clicked on) and the canvas could receive a single click, a double click or a drag. Gsharp has an enumerated value for each reason. For example, XuCR_ACTIVATE can be used instead of 10 - the value of the third argument when a button is activated. The Gsharp Applications Reference Manual contains a full list of callback reasons for each object together with their enumerated values.

Copy the following code into myapp.gsa

function RunExampleCB(string o, string d, float r)
  echo("Running BarCharts.gsl");
  exec("$UNIDIR/example/Gsharp/samples/barcharts.gsl");
endfunction
create Menu gsharp_1.Menubar.Demos;
create Button gsharp_1.Menubar.Demos.BarCharts
 ( XuNguiLabel = "Bar Charts",
   XuNguiCallback = "RunExampleCB"
 );  

Save myapp.gsa and run it again. You will notice that you now have a Demos menu containing a button labelled "Bar Charts". Select this button and see that the Bar Charts example is run.

Now carry on to Step 4 - Callback Data