home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / program / dde / datatoaof / Examples / Example2 / !HideTest / c / hidetest
Encoding:
Text File  |  1992-11-14  |  6.5 KB  |  236 lines

  1. /* Title:   hidetest.c
  2.  *
  3.  * Purpose: An example program to illustrate the use of the HiddenLib
  4.  *          replacement resspr functions.
  5.  *
  6.  * This program is essentially just a rip off of WExample by Acorn. I have
  7.  * altered it just to include my new replacement resspr functions, to
  8.  * demonstrate just how easy it is to hide a Sprites file inside your
  9.  * !RunImage file.  
  10.  *
  11.  * When started it sets up an icon on the icon bar. Clicking on the icon
  12.  * opens a window. The icon has a menu for 'Info' and 'Quit'. Only one window
  13.  * may be opened. This program isn't much use in itself, but it can be used
  14.  * as a skeleton for developing other applications.
  15.  */
  16.  
  17. #include "wimp.h"
  18. #include "wimpt.h"
  19. #include "win.h"
  20. #include "event.h"
  21. #include "baricon.h"
  22. #include "sprite.h"
  23. #include "res.h"
  24. #include "resspr+.h"
  25. #include "menu.h"
  26. #include "template.h"
  27. #include "dbox.h"
  28. #include "werr.h"
  29.  
  30. #include <stdlib.h>
  31.  
  32. /********************************* CONSTANTS ********************************/
  33.  
  34. /* Menu items */
  35. #define HideTest_menu_info     1
  36. #define HideTest_menu_quit     2
  37.  
  38. /* Info box field for the version string */
  39. #define HideTest_info_field    4
  40.  
  41. /******************************** GLOBAL DATA *******************************/
  42.  
  43. /* Application version */
  44. static char *HideTest_Version_String = "1.00 (24 April 1989)";
  45.  
  46. /* The top of the menu tree */
  47. static menu HideTest_menu;
  48.  
  49. /* Handle for the HideTest window */
  50. static wimp_w HideTest_win_handle;
  51.  
  52. /* Flag - is the window open */
  53. static BOOL HideTest_window_open = FALSE;
  54.  
  55. extern sprite_area *sprites;
  56. extern int          sprites_length;
  57.  
  58. /***************************** WINDOW FUNCTIONS *****************************/
  59.  
  60. /*--- Create the window, yielding its handle. Return TRUE if ok. ---*/
  61. static BOOL HideTest_create_window(char *name, wimp_w *handle)
  62. {
  63.   wimp_wind *window;    /* Pointer to window definition */
  64.  
  65.   /* Find template for the window */
  66.     window = template_syshandle(name);
  67.     if (window == 0)
  68.       return FALSE;
  69.  
  70.   /* Create the window, dealing with errors */
  71.   return (wimpt_complain(wimp_create_wind(window, handle)) == 0);
  72. }
  73.  
  74. /*--- Individual event routines for the window ---*/
  75. static void HideTest_redraw_window(wimp_w handle)
  76. {
  77.   /* Redrawing the window here does nothing - just go through the loop */
  78.   int            more;
  79.   wimp_redrawstr r;
  80.  
  81.   /* Start the redraw */
  82.   r.w = handle;
  83.   wimpt_noerr(wimp_redraw_wind(&r, &more));
  84.  
  85.   /* Do the redraw loop */
  86.   while (more)
  87.   {
  88.     wimp_get_rectangle(&r, &more);
  89.   }
  90. }
  91.  
  92. static void HideTest_open_window(wimp_openstr *o)
  93. {
  94.   /* Just pass the open request on to the wimp */
  95.   wimpt_noerr(wimp_open_wind(o));
  96. }
  97.  
  98. /****************************** EVENT HANDLERS ******************************/
  99.  
  100. /*--- Event handler called on a left click on the icon. ---*/
  101. static void HideTest_iconclick(wimp_i icon)
  102. {
  103.   icon = icon; /* We don't need the handle: this stops compiler warning */
  104.  
  105.   /* Open the window - only one allowed */
  106.   if (HideTest_window_open)
  107.     werr(FALSE, "Only one window may be opened");
  108.   else
  109.   {
  110.     wimp_wstate  state;
  111.  
  112.     /* Get the state of the window */
  113.     if (wimpt_complain(wimp_get_wind_state(HideTest_win_handle, &state)) == 0)
  114.     {
  115.       state.o.behind = -1;          /* Make sure window is opened in front */
  116.       wimpt_noerr(wimp_open_wind(&state.o));
  117.       HideTest_window_open = TRUE;
  118.     }
  119.   }
  120. }
  121.  
  122. /*--- Display the program info box - called from the menu processor. ---*/
  123. static void HideTest_info_about_program(void)
  124. {
  125.   dbox  d;  /* Dialogue box handle */
  126.  
  127.   /* Create the dialogue box */
  128.   if (d = dbox_new("ProgInfo"), d != NULL)
  129.   {
  130.     /* Fill in the version number */
  131.     dbox_setfield(d, HideTest_info_field, HideTest_Version_String);
  132.  
  133.     /* Show the dialogue box */
  134.     dbox_show(d);
  135.  
  136.     /* Keep it on the screen as long as needed */
  137.     dbox_fillin(d);
  138.  
  139.     /* Dispose of the dialogue box */
  140.     dbox_dispose(&d);
  141.   }
  142. }
  143.  
  144. /*--- Event handler for the menu. ---*/
  145. static void HideTest_menuproc(void *handle, char *hit)
  146. {
  147.   handle = handle; /* We don't need handle: this stops compiler warning */
  148.  
  149.   /* Find which menu item was hit and take action as appropriate */
  150.   switch (hit[0])
  151.   {
  152.     case HideTest_menu_info:
  153.       HideTest_info_about_program();
  154.       break;
  155.  
  156.     case HideTest_menu_quit:
  157.       /* Exit from the program. The wimp gets rid of the window and icon */
  158.       exit(0);
  159.   }
  160. }
  161.  
  162. /*--- Event handler for window. ---*/
  163. static void HideTest_event_handler(wimp_eventstr *e, void *handle)
  164. {
  165.   handle = handle; /* We don't need handle: this stops compiler warning */
  166.  
  167.   /* Deal with event */
  168.   switch (e->e)
  169.   {
  170.     case wimp_EREDRAW:
  171.       HideTest_redraw_window(e->data.o.w);
  172.       break;
  173.  
  174.     case wimp_EOPEN:
  175.       HideTest_open_window(&e->data.o);
  176.       break;
  177.  
  178.     case wimp_ECLOSE:  /* Pass on close request */
  179.       wimpt_noerr(wimp_close_wind(e->data.o.w));
  180.       HideTest_window_open = FALSE;
  181.  
  182.     default:   /* Ignore any other event */
  183.       break;
  184.   }
  185. }
  186.  
  187. /****************************** INITIALISATION ******************************/
  188.  
  189. /*--- Initialise the program, returning TRUE if it was all OK. ---*/
  190. static BOOL HideTest_initialise(void)
  191. {
  192.   /* RISC_OSlib initialisation */
  193.   wimpt_init("HiddenLib HideTest");     /* Main Wimp initialisation */
  194.   res_init("HideTest");                 /* Resources */
  195.  
  196.  
  197.   resspr_init(sprites, sprites_length); /* This line initialises your hidden       */
  198.                                         /* Sprite area. sprites and sprites_length */
  199.                                         /* are external                            */
  200.  
  201.   template_init();                      /* Templates */
  202.   dbox_init();                          /* Dialogue boxes */
  203.  
  204.   /* Create the main window, and declare its event handler */
  205.   if (!HideTest_create_window("MainWindow", &HideTest_win_handle))
  206.     return FALSE; /* Window creation failed */
  207.   win_register_event_handler(HideTest_win_handle, HideTest_event_handler, 0);
  208.  
  209.   /* Create the menu tree */
  210.   if (HideTest_menu = menu_new("HideTest", ">Info,Quit"), HideTest_menu == NULL)
  211.     return FALSE; /* Menu create failed */
  212.  
  213.   /* Set up the icon on the icon bar, and declare its event handlers */
  214.   baricon("!test", (int)resspr_area(), HideTest_iconclick);
  215.   if (!event_attachmenu(win_ICONBAR, HideTest_menu, HideTest_menuproc, 0))
  216.     return FALSE; /* Unable to attach menu */
  217.  
  218.   /* All went ok */
  219.   return TRUE;
  220. }
  221.  
  222. /******************************* MAIN PROGRAM ********************************/
  223.  
  224. /*--- Main entry point. ---*/
  225. int main()
  226. {
  227.   if (HideTest_initialise())
  228.   {
  229.     /* The main event loop */
  230.     while (TRUE)
  231.       event_process();
  232.   }
  233.  
  234.   return 0;
  235. }
  236.