home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / desktop / t / utils / !Logger_C_RISCOSFE < prev    next >
Encoding:
Text File  |  1993-05-31  |  10.9 KB  |  486 lines

  1. /*
  2.  *
  3.  *      Title     : Risc-OS Front End
  4.  *      System    : Any */
  5. #define Version     "2.0"
  6. /*      Copyright : (c) John H. Winters
  7.  *      Date      : 9th October, 1992
  8.  *      Author    : John H. Winters
  9.  *
  10.  *      Function  : Handles the Risc-OS front end.
  11.  *
  12.  *
  13.  *      Modification history.
  14.  *
  15.  *      Version   : 1.1
  16.  *      Date      : 24th October, 1992
  17.  *      Author    : John H. Winters
  18.  *      Changes   : Enhanced FE_PutText to cope with multiple nested calls.
  19.  *
  20.  *      Version   : 1.2
  21.  *      Date      : 13th November, 1992
  22.  *      Author    : John H. Winters
  23.  *      Changes   : Added the option of a flashing cursor.
  24.  *
  25.  *      Version   : 1.3
  26.  *      Date      : 14th November, 1992
  27.  *      Author    : John H. Winters
  28.  *      Changes   : Added the ability to save the options.
  29.  *
  30.  *      Version   : 2.0
  31.  *      Date      : 31st May, 1993
  32.  *      Author    : John H. Winters
  33.  *      Changes   : Updated for RISC OS 3.1
  34.  *
  35.  *      Version   :
  36.  *      Date      :
  37.  *      Author    :
  38.  *      Changes   :
  39.  *
  40.  */
  41.  
  42. #include <stdio.h>
  43. #include <string.h>
  44. #include <stdlib.h>
  45. #include <time.h>
  46.  
  47. #include "global.h"
  48. #include "logging.h"
  49. #include "winapp.h"
  50. #include "fe.h"
  51. #include "text.h"
  52.  
  53. /*
  54.  *============================================================================
  55.  *
  56.  *  Hash defines.
  57.  *
  58.  *============================================================================
  59.  */
  60.  
  61. #define INFO_NAME   "ProgInfo"
  62. #define MINFO       0
  63. #define MDISPLAY    1
  64. #define MSAVE       2
  65. #define MAUTO       3
  66. #define MCURSOR     4
  67. #define MVISIBLE    5
  68. #define MQUIT       6
  69. #define OPTION_FILE "<Logger$Dir>.Options"
  70. #define TFN_VERSION 4
  71.  
  72. /*
  73.  *============================================================================
  74.  *
  75.  *  Type definitions.
  76.  *
  77.  *============================================================================
  78.  */
  79.  
  80. typedef struct {
  81.     uint AutoDisplay ;
  82.     uint Cursor ;
  83.     uint StartVisible ;
  84. } t_options ;
  85.  
  86. /*
  87.  *============================================================================
  88.  *
  89.  *  Local data.
  90.  *
  91.  *============================================================================
  92.  */
  93.  
  94. static const t_options  DefaultOptions = {TRUE, FALSE, TRUE} ;
  95. static t_dbox           InfoDbox ;
  96. static t_menu_block    *MainMenu ;
  97. static t_options        Options ;
  98. static t_TXT_Handle     TextHandle ;
  99. const u8                WA_resource_dir [] = "Logger" ;
  100. const u8                WA_task_name []    = "New logger" ;
  101. static uint             WindowReady        = FALSE ;
  102.  
  103. /*
  104.  *============================================================================
  105.  *
  106.  *  Forward declarations.
  107.  *
  108.  *============================================================================
  109.  */
  110.  
  111. static unsigned int ClickHandler (
  112.     t_poll_block *poll_block,
  113.     void         *reference) ;
  114.  
  115. static void LoadOptions (void) ;
  116.  
  117. static void MenuHandler (
  118.     void      *reference,
  119.     const int *hit) ;
  120.  
  121. static void SaveOptions (void) ;
  122.  
  123. /*
  124.  *============================================================================
  125.  *
  126.  *  Externally visible routines.
  127.  *
  128.  *============================================================================
  129.  */
  130.  
  131. uint FE_BeginProcessing (void)
  132.  
  133. /*
  134.  *  Function :
  135.  *                  Begin processing under Risc-OS
  136.  *
  137.  *  Parameters :
  138.  *                  None.
  139.  *
  140.  *  Returns :
  141.  *                  TRUE for success, FALSE for failure.
  142.  *
  143.  */
  144.  
  145. {
  146.     t_menu_item *menu_items ;
  147.  
  148.     MainMenu = WA_BuildMenu ("Logger",
  149.                               ">Info, Display, Save options| Auto Display, Cursor, Start Visible| Quit") ;
  150.     if (MainMenu == NULL)
  151.     {
  152.         LOG_Error ("Failed to create a menu.\n") ;
  153.     }
  154.     else
  155.     {
  156.         /*
  157.          *  Set option ticks.
  158.          */
  159.         menu_items = (t_menu_item *) (MainMenu + 1) ;
  160.         menu_items [MAUTO].menu_flags.m.ticked    = Options.AutoDisplay ;
  161.         menu_items [MCURSOR].menu_flags.m.ticked  = Options.Cursor ;
  162.         menu_items [MVISIBLE].menu_flags.m.ticked = Options.StartVisible ;
  163.         InfoDbox = WA_CreateDbox (INFO_NAME, FALSE, NULL, NULL) ;
  164.         if (InfoDbox == NULL)
  165.         {
  166.             LOG_Error ("Can't create information dbox.\n") ;
  167.         }
  168.         else
  169.         {
  170.             WA_SetDboxField (InfoDbox,
  171.                              TFN_VERSION,
  172.                              "2.2") ;
  173.             TextHandle = TXT_CreateWindow (40, 128) ;
  174.             if (TextHandle != NULL)
  175.             {
  176.                 if (Options.Cursor)
  177.                 {
  178.                     TXT_CursorOn (TextHandle) ;
  179.                 }
  180.                 if ((!Options.StartVisible) || (TXT_DisplayWindow (TextHandle)))
  181.                 {
  182.                     WindowReady = TRUE ;
  183.                     return ((WA_IconToBar ("iconbar", TRUE)) &&
  184.                             (WA_ClaimEvent (ET_MouseClick,
  185.                                             ICON_BAR,
  186.                                             DONT_CARE,
  187.                                             ClickHandler,
  188.                                             NULL)) &&
  189.                             (WA_AttachMenu (ICON_BAR,
  190.                                             MainMenu,
  191.                                             MenuHandler,
  192.                                             NULL))) ;
  193.                 }
  194.             }
  195.         }
  196.     }
  197.     return (FALSE) ;
  198. }
  199.  
  200.  
  201. uint FE_LoadResources (void)
  202.  
  203. /*
  204.  *  Function :
  205.  *                  Load our resources as required.
  206.  *
  207.  *  Parameters :
  208.  *                  None.
  209.  *
  210.  *  Returns :
  211.  *                  TRUE for success, FALSE for failure.
  212.  *
  213.  */
  214.  
  215. {
  216.     if ((TXT_LoadResources ()) &&
  217.         (WA_LoadSprites ("Sprites")) &&
  218.         (WA_LoadTemplate ("Templates", INFO_NAME)))
  219.     {
  220.         LoadOptions () ;
  221.         return (TRUE) ;
  222.     }
  223.     else
  224.     {
  225.         return (FALSE) ;
  226.     }
  227. }
  228.  
  229.  
  230. void FE_PutText (
  231.     const U8 *text)
  232.  
  233. /*
  234.  *  Function :
  235.  *                  Put text to our window.
  236.  *
  237.  *  Parameters :
  238.  *                  text    The text to put.
  239.  *
  240.  *  Returns :
  241.  *                  None.
  242.  *
  243.  */
  244.  
  245. {
  246.     _kernel_oserror eb ;
  247.     t_error_flags   ef ;
  248.     uint            result ;
  249.     static uint     WindowInUse = FALSE ;
  250.  
  251.     if ((WindowReady) &&
  252.         (!WindowInUse))
  253.     {
  254.         WindowInUse = TRUE ;
  255.         if (Options.AutoDisplay)
  256.         {
  257.             TXT_DisplayWindow (TextHandle) ;
  258.         }
  259.         TXT_PutText (TextHandle, text) ;
  260.         WindowInUse = FALSE ;
  261.     }
  262.     else
  263.     {
  264.         /*
  265.          *  Getting here implies a problem has occured before we could get our window
  266.          *  ready, or that a problem occured whilst updating the window.  Use an error
  267.          *  box instead of trying to squirt more text at it.
  268.          */
  269.         eb.errnum  = 1 ;
  270.         strncpy (eb.errmess,
  271.                  text + 33,
  272.                  251) ;
  273.         eb.errmess [251] = '\0' ;
  274.         ef.value = 0 ;
  275.         ef.m.ok  = TRUE ;
  276.         WIMP_ReportError (&eb, ef, "LOGGER", &result) ;
  277.     }
  278. }
  279.  
  280. /*
  281.  *============================================================================
  282.  *
  283.  *  Local routines.
  284.  *
  285.  *============================================================================
  286.  */
  287.  
  288. static unsigned int ClickHandler (
  289.     t_poll_block *poll_block,
  290.     void         *reference)
  291.  
  292. /*
  293.  *  Function :
  294.  *                  Click handler.
  295.  *
  296.  *  Parameters :
  297.  *                  poll_block  Pointer to block describing the event.
  298.  *                  reference   Ignored.
  299.  *
  300.  *  Returns :
  301.  *                  TRUE if we handled the event, FALSE otherwise.
  302.  *
  303.  */
  304.  
  305. {
  306.     reference = reference ;
  307.  
  308.     /*
  309.      *  Check for left or right.
  310.      */
  311.     if ((poll_block->data.mc.button_state.m.adjust) ||
  312.         (poll_block->data.mc.button_state.m.select))
  313.     {
  314.         TXT_DisplayWindow (TextHandle) ;
  315.         return (TRUE) ;
  316.     }
  317.     else
  318.     {
  319.         return (FALSE) ;
  320.     }
  321. }
  322.  
  323.  
  324. static void LoadOptions (void)
  325.  
  326. /*
  327.  *  Function :
  328.  *                  Load the options from a file.  If this fails, set up defaults.
  329.  *
  330.  *  Parameters :
  331.  *                  None.
  332.  *
  333.  *  Returns :
  334.  *                  None, but guarantees to set up Options.
  335.  *
  336.  */
  337.  
  338. {
  339.     FILE      *handle ;
  340.     t_options  local ;
  341.  
  342.     handle = fopen (OPTION_FILE, "rb") ;
  343.     if (handle == NULL)
  344.     {
  345.         Options = DefaultOptions ;
  346.     }
  347.     else
  348.     {
  349.         if (fread (&local, sizeof (t_options), 1, handle) == 1)
  350.         {
  351.             Options = local ;
  352.         }
  353.         else
  354.         {
  355.             Options = DefaultOptions ;
  356.         }
  357.         fclose (handle) ;
  358.     }
  359. }
  360.  
  361.  
  362. static void MenuHandler (
  363.     void      *reference,
  364.     const int *hit)
  365.  
  366. /*
  367.  *  Function :
  368.  *                  Menu event handler.
  369.  *
  370.  *  Parameters :
  371.  *                  reference  Ignored.
  372.  *                  hit        Indication of the menu hit.
  373.  *
  374.  *  Returns :
  375.  *                  None.
  376.  *
  377.  */
  378.  
  379. {
  380.     t_menu_item *menu_items ;
  381.  
  382.     reference = reference ;
  383.     /*
  384.      *  See which menu entry has been chosen.
  385.      */
  386.     menu_items = (t_menu_item *) (MainMenu + 1) ;
  387.     switch (hit[0])
  388.     {
  389.         case MINFO :
  390.             WA_DisplayDbox (InfoDbox) ;
  391.             break ;
  392.  
  393.         case MDISPLAY :
  394.             TXT_DisplayWindow (TextHandle) ;
  395.             break ;
  396.  
  397.         case MSAVE :
  398.             SaveOptions () ;
  399.             break ;
  400.  
  401.         case MAUTO :
  402.             if (Options.AutoDisplay)
  403.             {
  404.                 menu_items [MAUTO].menu_flags.m.ticked = FALSE ;
  405.                 Options.AutoDisplay = FALSE ;
  406.             }
  407.             else
  408.             {
  409.                 menu_items [MAUTO].menu_flags.m.ticked = TRUE ;
  410.                 Options.AutoDisplay = TRUE ;
  411.             }
  412.             break ;
  413.  
  414.         case MCURSOR :
  415.             if (Options.Cursor)
  416.             {
  417.                 menu_items [MCURSOR].menu_flags.m.ticked = FALSE ;
  418.                 Options.Cursor = FALSE ;
  419.                 TXT_CursorOff (TextHandle) ;
  420.             }
  421.             else
  422.             {
  423.                 menu_items [MCURSOR].menu_flags.m.ticked = TRUE ;
  424.                 Options.Cursor = TRUE ;
  425.                 TXT_CursorOn (TextHandle) ;
  426.             }
  427.             break ;
  428.  
  429.         case MVISIBLE :
  430.             if (Options.StartVisible)
  431.             {
  432.                 menu_items [MVISIBLE].menu_flags.m.ticked = FALSE ;
  433.                 Options.StartVisible = FALSE ;
  434.             }
  435.             else
  436.             {
  437.                 menu_items [MVISIBLE].menu_flags.m.ticked = TRUE ;
  438.                 Options.StartVisible = TRUE ;
  439.             }
  440.             break ;
  441.  
  442.         case MQUIT :
  443.             WA_WindUp () ;
  444.             break ;
  445.  
  446.     }
  447. }
  448.  
  449.  
  450. static void SaveOptions (void)
  451.  
  452. /*
  453.  *  Function :
  454.  *                  Save the options to a file.
  455.  *
  456.  *  Parameters :
  457.  *                  None.
  458.  *
  459.  *  Returns :
  460.  *                  None.
  461.  *
  462.  */
  463.  
  464. {
  465.     FILE      *handle ;
  466.  
  467.     handle = fopen (OPTION_FILE, "wb") ;
  468.     if (handle == NULL)
  469.     {
  470.         LOG_Error ("Failed to open \"%s\" to save options.\n", OPTION_FILE) ;
  471.     }
  472.     else
  473.     {
  474.         if (fwrite (&Options, sizeof (t_options), 1, handle) == 1)
  475.         {
  476.             LOG_Info ("Options saved.\n") ;
  477.         }
  478.         else
  479.         {
  480.             LOG_Error ("Failed to write options to \"%s\".\n",
  481.                        OPTION_FILE) ;
  482.         }
  483.         fclose (handle) ;
  484.     }
  485. }
  486.