home *** CD-ROM | disk | FTP | other *** search
- /*
- *
- * Title : Risc-OS Front End
- * System : Any */
- #define Version "2.0"
- /* Copyright : (c) John H. Winters
- * Date : 9th October, 1992
- * Author : John H. Winters
- *
- * Function : Handles the Risc-OS front end.
- *
- *
- * Modification history.
- *
- * Version : 1.1
- * Date : 24th October, 1992
- * Author : John H. Winters
- * Changes : Enhanced FE_PutText to cope with multiple nested calls.
- *
- * Version : 1.2
- * Date : 13th November, 1992
- * Author : John H. Winters
- * Changes : Added the option of a flashing cursor.
- *
- * Version : 1.3
- * Date : 14th November, 1992
- * Author : John H. Winters
- * Changes : Added the ability to save the options.
- *
- * Version : 2.0
- * Date : 31st May, 1993
- * Author : John H. Winters
- * Changes : Updated for RISC OS 3.1
- *
- * Version :
- * Date :
- * Author :
- * Changes :
- *
- */
-
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <time.h>
-
- #include "global.h"
- #include "logging.h"
- #include "winapp.h"
- #include "fe.h"
- #include "text.h"
-
- /*
- *============================================================================
- *
- * Hash defines.
- *
- *============================================================================
- */
-
- #define INFO_NAME "ProgInfo"
- #define MINFO 0
- #define MDISPLAY 1
- #define MSAVE 2
- #define MAUTO 3
- #define MCURSOR 4
- #define MVISIBLE 5
- #define MQUIT 6
- #define OPTION_FILE "<Logger$Dir>.Options"
- #define TFN_VERSION 4
-
- /*
- *============================================================================
- *
- * Type definitions.
- *
- *============================================================================
- */
-
- typedef struct {
- uint AutoDisplay ;
- uint Cursor ;
- uint StartVisible ;
- } t_options ;
-
- /*
- *============================================================================
- *
- * Local data.
- *
- *============================================================================
- */
-
- static const t_options DefaultOptions = {TRUE, FALSE, TRUE} ;
- static t_dbox InfoDbox ;
- static t_menu_block *MainMenu ;
- static t_options Options ;
- static t_TXT_Handle TextHandle ;
- const u8 WA_resource_dir [] = "Logger" ;
- const u8 WA_task_name [] = "New logger" ;
- static uint WindowReady = FALSE ;
-
- /*
- *============================================================================
- *
- * Forward declarations.
- *
- *============================================================================
- */
-
- static unsigned int ClickHandler (
- t_poll_block *poll_block,
- void *reference) ;
-
- static void LoadOptions (void) ;
-
- static void MenuHandler (
- void *reference,
- const int *hit) ;
-
- static void SaveOptions (void) ;
-
- /*
- *============================================================================
- *
- * Externally visible routines.
- *
- *============================================================================
- */
-
- uint FE_BeginProcessing (void)
-
- /*
- * Function :
- * Begin processing under Risc-OS
- *
- * Parameters :
- * None.
- *
- * Returns :
- * TRUE for success, FALSE for failure.
- *
- */
-
- {
- t_menu_item *menu_items ;
-
- MainMenu = WA_BuildMenu ("Logger",
- ">Info, Display, Save options| Auto Display, Cursor, Start Visible| Quit") ;
- if (MainMenu == NULL)
- {
- LOG_Error ("Failed to create a menu.\n") ;
- }
- else
- {
- /*
- * Set option ticks.
- */
- menu_items = (t_menu_item *) (MainMenu + 1) ;
- menu_items [MAUTO].menu_flags.m.ticked = Options.AutoDisplay ;
- menu_items [MCURSOR].menu_flags.m.ticked = Options.Cursor ;
- menu_items [MVISIBLE].menu_flags.m.ticked = Options.StartVisible ;
- InfoDbox = WA_CreateDbox (INFO_NAME, FALSE, NULL, NULL) ;
- if (InfoDbox == NULL)
- {
- LOG_Error ("Can't create information dbox.\n") ;
- }
- else
- {
- WA_SetDboxField (InfoDbox,
- TFN_VERSION,
- "2.2") ;
- TextHandle = TXT_CreateWindow (40, 128) ;
- if (TextHandle != NULL)
- {
- if (Options.Cursor)
- {
- TXT_CursorOn (TextHandle) ;
- }
- if ((!Options.StartVisible) || (TXT_DisplayWindow (TextHandle)))
- {
- WindowReady = TRUE ;
- return ((WA_IconToBar ("iconbar", TRUE)) &&
- (WA_ClaimEvent (ET_MouseClick,
- ICON_BAR,
- DONT_CARE,
- ClickHandler,
- NULL)) &&
- (WA_AttachMenu (ICON_BAR,
- MainMenu,
- MenuHandler,
- NULL))) ;
- }
- }
- }
- }
- return (FALSE) ;
- }
-
-
- uint FE_LoadResources (void)
-
- /*
- * Function :
- * Load our resources as required.
- *
- * Parameters :
- * None.
- *
- * Returns :
- * TRUE for success, FALSE for failure.
- *
- */
-
- {
- if ((TXT_LoadResources ()) &&
- (WA_LoadSprites ("Sprites")) &&
- (WA_LoadTemplate ("Templates", INFO_NAME)))
- {
- LoadOptions () ;
- return (TRUE) ;
- }
- else
- {
- return (FALSE) ;
- }
- }
-
-
- void FE_PutText (
- const U8 *text)
-
- /*
- * Function :
- * Put text to our window.
- *
- * Parameters :
- * text The text to put.
- *
- * Returns :
- * None.
- *
- */
-
- {
- _kernel_oserror eb ;
- t_error_flags ef ;
- uint result ;
- static uint WindowInUse = FALSE ;
-
- if ((WindowReady) &&
- (!WindowInUse))
- {
- WindowInUse = TRUE ;
- if (Options.AutoDisplay)
- {
- TXT_DisplayWindow (TextHandle) ;
- }
- TXT_PutText (TextHandle, text) ;
- WindowInUse = FALSE ;
- }
- else
- {
- /*
- * Getting here implies a problem has occured before we could get our window
- * ready, or that a problem occured whilst updating the window. Use an error
- * box instead of trying to squirt more text at it.
- */
- eb.errnum = 1 ;
- strncpy (eb.errmess,
- text + 33,
- 251) ;
- eb.errmess [251] = '\0' ;
- ef.value = 0 ;
- ef.m.ok = TRUE ;
- WIMP_ReportError (&eb, ef, "LOGGER", &result) ;
- }
- }
-
- /*
- *============================================================================
- *
- * Local routines.
- *
- *============================================================================
- */
-
- static unsigned int ClickHandler (
- t_poll_block *poll_block,
- void *reference)
-
- /*
- * Function :
- * Click handler.
- *
- * Parameters :
- * poll_block Pointer to block describing the event.
- * reference Ignored.
- *
- * Returns :
- * TRUE if we handled the event, FALSE otherwise.
- *
- */
-
- {
- reference = reference ;
-
- /*
- * Check for left or right.
- */
- if ((poll_block->data.mc.button_state.m.adjust) ||
- (poll_block->data.mc.button_state.m.select))
- {
- TXT_DisplayWindow (TextHandle) ;
- return (TRUE) ;
- }
- else
- {
- return (FALSE) ;
- }
- }
-
-
- static void LoadOptions (void)
-
- /*
- * Function :
- * Load the options from a file. If this fails, set up defaults.
- *
- * Parameters :
- * None.
- *
- * Returns :
- * None, but guarantees to set up Options.
- *
- */
-
- {
- FILE *handle ;
- t_options local ;
-
- handle = fopen (OPTION_FILE, "rb") ;
- if (handle == NULL)
- {
- Options = DefaultOptions ;
- }
- else
- {
- if (fread (&local, sizeof (t_options), 1, handle) == 1)
- {
- Options = local ;
- }
- else
- {
- Options = DefaultOptions ;
- }
- fclose (handle) ;
- }
- }
-
-
- static void MenuHandler (
- void *reference,
- const int *hit)
-
- /*
- * Function :
- * Menu event handler.
- *
- * Parameters :
- * reference Ignored.
- * hit Indication of the menu hit.
- *
- * Returns :
- * None.
- *
- */
-
- {
- t_menu_item *menu_items ;
-
- reference = reference ;
- /*
- * See which menu entry has been chosen.
- */
- menu_items = (t_menu_item *) (MainMenu + 1) ;
- switch (hit[0])
- {
- case MINFO :
- WA_DisplayDbox (InfoDbox) ;
- break ;
-
- case MDISPLAY :
- TXT_DisplayWindow (TextHandle) ;
- break ;
-
- case MSAVE :
- SaveOptions () ;
- break ;
-
- case MAUTO :
- if (Options.AutoDisplay)
- {
- menu_items [MAUTO].menu_flags.m.ticked = FALSE ;
- Options.AutoDisplay = FALSE ;
- }
- else
- {
- menu_items [MAUTO].menu_flags.m.ticked = TRUE ;
- Options.AutoDisplay = TRUE ;
- }
- break ;
-
- case MCURSOR :
- if (Options.Cursor)
- {
- menu_items [MCURSOR].menu_flags.m.ticked = FALSE ;
- Options.Cursor = FALSE ;
- TXT_CursorOff (TextHandle) ;
- }
- else
- {
- menu_items [MCURSOR].menu_flags.m.ticked = TRUE ;
- Options.Cursor = TRUE ;
- TXT_CursorOn (TextHandle) ;
- }
- break ;
-
- case MVISIBLE :
- if (Options.StartVisible)
- {
- menu_items [MVISIBLE].menu_flags.m.ticked = FALSE ;
- Options.StartVisible = FALSE ;
- }
- else
- {
- menu_items [MVISIBLE].menu_flags.m.ticked = TRUE ;
- Options.StartVisible = TRUE ;
- }
- break ;
-
- case MQUIT :
- WA_WindUp () ;
- break ;
-
- }
- }
-
-
- static void SaveOptions (void)
-
- /*
- * Function :
- * Save the options to a file.
- *
- * Parameters :
- * None.
- *
- * Returns :
- * None.
- *
- */
-
- {
- FILE *handle ;
-
- handle = fopen (OPTION_FILE, "wb") ;
- if (handle == NULL)
- {
- LOG_Error ("Failed to open \"%s\" to save options.\n", OPTION_FILE) ;
- }
- else
- {
- if (fwrite (&Options, sizeof (t_options), 1, handle) == 1)
- {
- LOG_Info ("Options saved.\n") ;
- }
- else
- {
- LOG_Error ("Failed to write options to \"%s\".\n",
- OPTION_FILE) ;
- }
- fclose (handle) ;
- }
- }
-