! driver.MBS
! Driver for VARKON project "hershey"
! Display the Hershey glyphs in VARKON
!    (as VARKON drawings, not using the VARKON font mechanism) and
! Call this from the Active Module; do clear_pm() and clear_gm() there first.

global drawing module driver(
int verbosity);

! verbosity levels
!    0 = no warning/debugging info at all
!   >0 = all fatal errors
!    5 = tracing
!    9 = everything

! Window element identifiers
int   main_id;                                ! main window
int   event_id;                               ! events in main window
int   button_table_plain, button_table_boxed; ! buttons
int   button_range_plain, button_range_boxed;
int   button_glyph_plain, button_glyph_boxed;
int   button_plot_all;
int   button_quit;
float main_window_width;                      ! sizes and heights
float main_window_height;
float A_height;                               ! capital-A text height
float A_height_default;
float button_height;
float button_width_long;
float button_frame;                           ! width of frame around a button
float button_separation;                      ! separation between buttons
float column_1_x, column_2_x;                 ! button positions
float row_1_y, row_2_y, row_3_y, row_4_y;

! input values as strings
string table_number*2;
string range_number*3;
string glyph_number*4;

! toplevel values
! Each of the three types of display (table, range, glyph) can be
! called directly from the driver.  If so (if at the "top level")
! then they should produce "crop marks" around the image they create.
! However, the architecture of this project is such that tables
! call ranges and ranges call glyphs.  Ranges and glyphs should
! produce crop marks only when they are at the top level.
! If they produce crop marks when called from higher levels,
! the display gets altogether too crowded.
constant int toplevel_false = 0;
constant int toplevel_true  = 1;

! box_visibility values
constant int plain = 0;
constant int boxed = 1;

beginmodule

! Use a listing window for debugging messages.
if verbosity > 0 then
   lst_ini("Hershey Project Messages");
endif;

! Set button and window dimensions and positions (in mm).
! tsize (height of "A" for text-in-window) default is 5, 
!    but this gives button sizes which are too small.
!    Empirically, 8 seems to work.
A_height          := 8;
A_height_default  := 5;
set (tsize=A_height);
                   ! this is the longest string, and thus sets the button width
button_width_long  := textl("Show single glyph, boxed glyphs");
button_height      := A_height * 4;     ! empirical adjustment
button_frame       := A_height / 1.5;   ! empirical adjustment
button_separation  := A_height * 3;
column_1_x         := button_separation;
column_2_x         := column_1_x + button_width_long + button_separation;
main_window_width  := column_2_x + button_width_long + button_separation;
row_1_y            := button_separation;
row_2_y            := row_1_y + button_height + button_separation;
row_3_y            := row_2_y + button_height + button_separation;
row_4_y            := row_3_y + button_height + button_separation;
main_window_height := row_4_y + button_height + button_separation;
set (tsize=A_height_default); ! reset tsize to default

! Create the interactive button window.
main_id:=cre_win(vec(100,200),
                 main_window_width,main_window_height,
                 "Choose an Action");

button_table_plain     := cre_button(main_id,vec(column_1_x, row_1_y),
                             button_width_long, button_height, button_frame,
                             "Show table, plain glyphs","please wait");
button_table_boxed     := cre_button(main_id,vec(column_2_x, row_1_y),
                             button_width_long, button_height, button_frame,
                             "Show table, boxed glyphs","please wait");

button_range_plain     := cre_button(main_id,vec(column_1_x, row_2_y),
                             button_width_long, button_height, button_frame,
                             "Show range, plain glyphs","please wait");
button_range_boxed     := cre_button(main_id,vec(column_2_x, row_2_y),
                             button_width_long, button_height, button_frame,
                             "Show range, boxed glyphs","please wait");

button_glyph_plain     := cre_button(main_id,vec(column_1_x, row_3_y),
                             button_width_long, button_height, button_frame,
                             "Show single glyph, plain glyph","please wait");
button_glyph_boxed     := cre_button(main_id,vec(column_2_x, row_3_y),
                             button_width_long, button_height, button_frame,
                             "Show single glyph, boxed glyphs","please wait");

button_plot_all        := cre_button(main_id,vec(column_1_x, row_4_y),
                             button_width_long, button_height, button_frame,
                             "Plot All","please wait");
button_quit            := cre_button(main_id,vec(column_2_x, row_4_y),
                             button_width_long, button_height, button_frame,
                             "Quit","done");
show_win(main_id);



! main loop
loop: event_id := wait_win(main_id);

   if event_id = button_table_plain then 
      table_number := inpmt("table number:", "0", 2);
      csys_1p(#10,"table",vec(0,0):blank=1);
      part(#11, show_one_table(ival(table_number), 
                               toplevel_true, plain, verbosity), #10);
   elif event_id = button_table_boxed then 
      table_number := inpmt("table number:", "0", 2);
      csys_1p(#12,"table",vec(0,0):blank=1);
      part(#13, show_one_table(ival(table_number),
                               toplevel_true,  boxed, verbosity), #12);

   elif event_id = button_range_plain then 
      range_number := inpmt("range number:", "0", 3);
      csys_1p(#20,"range",vec(0,0):blank=1);
      part(#21, show_one_range(ival(range_number),
                               toplevel_true,  plain, verbosity), #20);
   elif event_id = button_range_boxed then 
      range_number := inpmt("range number:", "0", 3);
      csys_1p(#22,"range",vec(0,0):blank=1);
      part(#23, show_one_range(ival(range_number),
                               toplevel_true,  boxed, verbosity), #22);

   ! While tables, ranges, and fonts are numbered from 0,
   ! Dr. Hershey's glyph numbers start at 1.
   elif event_id = button_glyph_plain then 
      glyph_number := inpmt("glyph number:", "1", 4);
      csys_1p(#30,"glyph",vec(0,0):blank=1);
      part(#31, show_one_glyph(ival(glyph_number),
                               toplevel_true,  plain, verbosity), #30);
   elif event_id = button_glyph_boxed then 
      glyph_number := inpmt("glyph number:", "1", 4);
      csys_1p(#32,"glyph",vec(0,0):blank=1);
      part(#33, show_one_glyph(ival(glyph_number),
                               toplevel_true,  boxed, verbosity), #32);

   elif event_id = button_plot_all then 
      part(#40, plot_all(verbosity));
   elif event_id = button_quit then
      goto quit;
   else
      goto loop;
  endif;

quit:

del_win(main_id);

if verbosity > 0 then
   lst_exi();
endif;

endmodule

! Copyright 2003-2005 by David M. MacMillan

! This work is free software; you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation; either version 2 of the License, or
! (at your option) any later version.

! NOTICE OF DISCLAIMER OF WARRANTY AND LIABILITY:

! This work is distributed WITHOUT ANY WARRANTY;
! without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
! See the GNU General Public License for more details.

! In no event will the author(s), editor(s), or publisher(s) of this work
! be liable to you or to any other party for damages,
! including but not limited to any general, special, incidental
! or consequential damages arising out of your use of or inability to use this
! work or the information contained in it, even if you have been advised
! of the possibility of such damages.

! In no event will the author(s), editor(s), or publisher(s) of this work
! be liable to you or to any other party for any injury, death,
! disfigurement, or other personal damage arising out of your use of
! or inability to use this work or the information
! contained in it, even if you have been advised of the
! possibility of such injury, death, disfigurement, or other
! personal damage.

! You should have received a copy of the GNU General Public License
! along with this work; if not, write to the
! Free Software Foundation, Inc., 59 Temple Place - Suite 330,
! Boston, MA  02111-1307, USA.

