home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / toollib_2 / Examples / TextDemo / Builds / c / main
Encoding:
Text File  |  1996-06-14  |  4.4 KB  |  192 lines

  1. /*** Text ***/
  2. /* A demonstration of Toolbox programming using OSLib
  3.  * (c) Paul Field
  4.  */
  5.  
  6. /* Notes:
  7.  *
  8.  * OSLib refers to toolbox events as 'actions' and, for consistancy, we follow
  9.  * this convention.
  10.  *
  11.  * The NOT_USED(var) macro is used to supress a compiler warnings about 'var'
  12.  * being declared but not being used. This warning occurs when the Acorn compiler
  13.  * features "ah" are enabled. You should activate these features because they
  14.  * enable useful warnings which can indicate bugs in your program.
  15.  */
  16.  
  17. #include <assert.h>
  18. #include <stdio.h>
  19.  
  20. #include "colourtrans.h"
  21. #include "window.h"
  22.  
  23. #include "applicatio.h"
  24. #include "exception.h"
  25. #include "nev_wimp.h"
  26. #include "text_plot.h"
  27.  
  28.  
  29. /* Width of the viewer's extent in OS units */
  30. static int viewer_width;
  31.  
  32.  
  33.  
  34. static void rectangle_fill(int x, int y, unsigned width, unsigned height)
  35.  { os_plot(os_MOVE_TO, x, y);
  36.    os_plot(os_PLOT_RECTANGLE + os_PLOT_BY, width, height);
  37.  }
  38.  
  39.  
  40.  
  41.  
  42. static void test_width(const char* s, int x, int y)
  43.  { int width;
  44.  
  45.    width = text_plot_width(s);
  46.    text_plot(s, x, y);
  47.    text_plot("*", x+width, y);
  48.  }
  49.  
  50.  
  51.  
  52.  
  53. static void write_message(int x, int y)
  54.  { enum
  55.     { spacing = 4,     /* spacing between lines */
  56.       line_height = wimp_CHAR_YSIZE + spacing
  57.     };
  58.  
  59.    /* (x,y) is top left of window */
  60.  
  61.    /* Move 'y' to position of first line */
  62.    y -= line_height;
  63.  
  64.    text_plot_set_colours(os_COLOUR_BLACK, os_COLOUR_VERY_LIGHT_GREY);
  65.    text_plot("This is a test of ToolLib's text_plot facilities", x, y);
  66.  
  67.    y -= line_height*2;
  68.  
  69.    colourtrans_set_gcol(os_COLOUR_BLACK, colourtrans_SET_FG, os_ACTION_OVERWRITE, NULL);
  70.    rectangle_fill(x, y - text_plot_baseline_adjustment - spacing/2, viewer_width, line_height);
  71.  
  72.    text_plot_set_colours(os_COLOUR_WHITE, os_COLOUR_BLACK);
  73.    text_plot("Let's try white text on a black background", x, y);
  74.  
  75.  
  76.    y -= line_height*2;
  77.  
  78.    text_plot_set_colours(os_COLOUR_BLACK, os_COLOUR_VERY_LIGHT_GREY);
  79.    text_plot("Let's test text_plot_width by placing '*' at the end of these pieces of text:", x, y);
  80.  
  81.    y -= line_height;
  82.    test_width("Hello", x, y);
  83.  
  84.    y -= line_height;
  85.    test_width("", x, y);
  86.  
  87.    y -= line_height;
  88.    test_width("Here is a longer bit of text", x, y);
  89.  
  90.  
  91.    y -= line_height*2;
  92.    text_plot("Finally, right-justified text:", x, y);
  93.  
  94.    y -= line_height;
  95.    text_plot_right_justified("Hello", x + viewer_width, y);
  96.  
  97.    y -= line_height;
  98.    text_plot_right_justified("Hello again", x + viewer_width, y);
  99.  }
  100.  
  101.  
  102.  
  103. static nevent_result viewer_redraw(wimp_event_no event_code, wimp_block *event, toolbox_block *id_block, void *handle)
  104.  { bool more;
  105.    int  origin_x;
  106.    int  origin_y;
  107.  
  108.    assert(event_code == wimp_REDRAW_WINDOW_REQUEST);
  109.    NOT_USED(event_code);
  110.    NOT_USED(event);
  111.    NOT_USED(handle);
  112.  
  113.    more = wimp_redraw_window(&event->redraw);
  114.  
  115.    /* Calculate where the work area origin is on screen (or off screen) */
  116.    origin_x = event->redraw.box.x0 - event->redraw.xscroll;
  117.    origin_y = event->redraw.box.y1 - event->redraw.yscroll;
  118.  
  119.    try
  120.     { while (more)
  121.        { write_message(origin_x, origin_y);
  122.          more = wimp_get_rectangle(&event->redraw);
  123.        }
  124.     }
  125.    catch
  126.     { /* If there's an error during redraw then we could run into an infinite loop
  127.        * if the error report window covers up the window and so causes another redraw and another error.
  128.        * To avoid this happening, we hide the window
  129.        */
  130.       toolbox_hide_object(0, id_block->this_obj);
  131.       throw();
  132.     }
  133.    catch_end
  134.  
  135.    return nevent_HANDLED;
  136.  }
  137.  
  138.  
  139.  
  140.  
  141. static void text_initialise(int argc, char *argv[])
  142.  { toolbox_o viewer_id;
  143.    os_box    extent;
  144.  
  145.    NOT_USED(argc);
  146.    NOT_USED(argv);
  147.  
  148. #ifndef NDEBUG
  149.    /* Redirect error stream */
  150.    freopen("pipe:$.debug", "w", stderr);
  151. #endif
  152.  
  153.    nevent_wimp_initialise();
  154.  
  155.    viewer_id = toolbox_create_object(0, (toolbox_id)"Viewer");
  156.    nevent_wimp_register_handler(viewer_id, wimp_REDRAW_WINDOW_REQUEST, viewer_redraw, NULL);
  157.  
  158.    window_get_extent(0, viewer_id, &extent);
  159.    assert(extent.x0 == 0);
  160.    assert(extent.y1 == 0);
  161.  
  162.    viewer_width = extent.x1;
  163.  }
  164.  
  165.  
  166.  
  167.  
  168. /* Respond to all actions and messages */
  169. static const int toolbox_actions[] =
  170.  { 0
  171.  };
  172.  
  173.  
  174. #if 0
  175. /* no messages, so NULL is used in the this_application struture */
  176. static const int wimp_messages[] =
  177.  {
  178.  };
  179. #endif
  180.  
  181.  
  182. /* The variable which the application system required */
  183. application this_application =
  184.  { "<Text$Dir>",
  185.    310,
  186.    toolbox_actions,
  187.    NULL, /*wimp_messages,*/
  188.    3,
  189.    text_initialise,
  190.    NULL
  191.  };
  192.