Step 5 - File Selection

For our second example we are going to create an application which can read in file and analyse the line lengths and number of spaces on each line.

We'll start by creating our main interface and saving it as mysecondapp.gsa

gsharp_1.XuNguiTitle = "MySecondApp";
create Menubar gsharp_1.Menubar;
create Menu gsharp_1.Menubar.File;
create Button gsharp_1.Menubar.File.Open
 ( XuNguiLabel = "Open ...",
   XuNguiCallback = "FileOpenCB"
 );
create Separator gsharp_1.Menubar.File.Separator1;
create Button gsharp_1.Menubar.File.PrintSetup
 ( XuNguiLabel = "Print Setup ...",
   XuNguiCallback = "GuiFilePrintSetup"
 );
create Button gsharp_1.Menubar.File.Print
 ( XuNguiLabel = "Print",
   XuNguiCallback = "GuiFilePrint"
 );
create Separator gsharp_1.Menubar.File.Separator2;
create Button gsharp_1.Menubar.File.Exit
 ( XuNguiLabel = "Exit ...",
   XuNguiCallback = "GuiFileExit"
 );
create Toolbar gsharp_1.toptoolbar
   ( XuNguiCreateFunction = "GuiSetCommandToolbarId",
     XuNguiDestroyFunction = "GuiDestroyCommandToolbar"
   );
create Icon gsharp_1.toptoolbar.Open
 ( XuNguiLabel = "Open",
   XuNguiCallback = "FileOpenCB",
   XuNguiPixmap = "open.xpm"
 );

create Canvas gsharp_1.Canvas;

Firstly we'll look at the new things in the above script

  • We've added "Print Setup ..." and "Print" buttons which use the standard Gsharp print callbacks to add support for printing to our application
  • We've created a Toolbar for containing icons.
  • We've added an "Open" button and an Open Icon that both call FileOpenCB - which we need to program ourselves.

We'll start by developing our callback to read some data - FileOpenCB. This callback needs to popup a fileSelection dialog, read the selected file with import_report(), process the data in the file and then plot it.

function FileOpenCB(string o, string d, float r)
  string filename;

  filename = input_file("Select a data file", "r", true, "*.dat",
                   "$UNIDIR/example/Gsharp");
  if (filename=undef)  return;

  if exists(WORK.Lines)  destroy WORK.Lines;
  set_messages(true, true, false, true);  #turn off info messages
  import_ascii(filename,,,,,,"WORK.Lines","text");
  set_messages(true, true, true, true);

  LineLengths = strlen(WORK.Lines);
  NumSpaces = LineLengths - strlen(WORK.Lines-" ");

  if not exists(page_1.viewport)  CreatePlot();
  repaint;
endfunction
 

In the above function the filename returned by input_file is read into a string dataset by import_ascii. The datasets LineLengths and NumSpaces are then calculated using the function strlen().

Finally we need to create a plot in Gsharp of LineLengths and NumSpaces. Start Gsharp and type the following two lines on the command line:

LineLengths = rnd(20);
NumSpaces = rnd(20);

Now create a plot of these two datasets, generate the GSL in the ScriptBuilder and then paste it into a function CreatePlot() or you could just use the one that we've made for you ...

function CreatePlot()
  create Viewport page_1.viewport
   ( XuNfirstDiagonalPoint = (10,10) %,
     XuNsecondDiagonalPoint = (88,82) %,
     XuNframe = "behind",
     XuNframeWidth = 0.1 mm
   );
  create Title page_1.viewport.title
   ( XuNtitleText = "Word Counting"
   );
  create Domain page_1.viewport.domain1
   ( XuN1st2DYAxisTicklines = "behind",
     XuN1st2DYAxisTickmarks = false
   );
  set page_1.viewport.domain1.yaxis1
   ( XuNaxisTextText = "Number of Spaces",
     XuNticklinesMinor = true,
     XuNticklinesMinorStyle = "lineStyle1"
   );
  create Graph page_1.viewport.domain1.lengths
   ( XuNgraphType = "bar",
     XuNyData = "NumSpaces"
   );
  create Domain page_1.viewport.domain2
   ( XuN2nd2DYAxisTickmarks = false
   );
  set page_1.viewport.domain2.yaxis2
   ( XuNaxisLabelsColor = 41,
     XuNaxisTextText = "Line Lengths",
     XuNaxle = true,
     XuNaxleColor = 41
   );
  create Graph page_1.viewport.domain2.spaces
   ( XuNcolor = 41,
     XuNgraphType = "line",
     XuNyData = "LineLengths"
   );
endfunction
It's now time to run your application. Try opening different files using the File/Open button.

Now continue to Step 6 - Switches